pi-typesafe-router

September 20, 2026 ยท View on GitHub

Use Jev to classify requests and route them to the right model for the task.

Setup

Do not enable this alongside another automatic router.

Install via:

`pi install npm:pi-typesafe-router`

Choose a real provider/model in Pi first, then run:

/typesafe-router setup typesafe

This creates ~/.pi/agent/typesafe-router.json, initially off, using your selected model for all three routes. Edit the mappings, set TYPESAFE_API_KEY in the environment that launches Pi, and run /reload.

pi install npm:pi-typesafe-router.

Usage

Verify your configuration, then enable routing:

/typesafe-router doctor
/typesafe-router on

Doctor tests the classifier and configured models with synthetic requests, without sending your conversation, using tools, or changing the selected model. These checks may incur charges.

Run /typesafe-router help for a table explaining every command. Successful doctor verification is stored in the current Pi session and restored after /reload when the configuration, models, and credential references still match.

Configuration

Configuration examples are provided for TypeSafe, Cloudflare, Vercel, and OpenRouter.

To use one:

/typesafe-router setup [typesafe|cloudflare|vercel|openrouter]
BackendCredentialModel
TypeSafeTYPESAFE_API_KEYjev-1.13.0
Cloudflare AI GatewayCLOUDFLARE_API_TOKEN, account and gateway IDs in configtypesafe/jev
Vercel AI GatewayAI_GATEWAY_API_KEYtypesafe-ai/jev
OpenRouterOPENROUTER_API_KEYtypesafe/jev-1.13

Project structure

  • src/classifier.ts: transport adapters and response validation.
  • src/config.ts, context.ts, routing.ts: schema, bounded text projection, and deterministic policy.
  • src/index.ts, settings.ts: Pi lifecycle, commands, cancellation, and global settings.
  • tests/: synthetic transport, policy, lifecycle, and real Pi SDK tests. No paid API calls.
  • infra/github/: Pulumi-managed repository settings and main ruleset.
  • docs/: architecture decision records (ADRs), recording implemented decisions and their tradeoffs.

Testing

Run npm run check for offline tests, lint, formatting, and typechecking.

npm install also installs the Lefthook pre-commit hook. Each commit formats and lints its staged files, applying safe fixes before the commit is created. CI runs the same formatting and lint checks as a required job in parallel with the test matrix.

E2E tests require the following variables to be set:

  • OPENROUTER_API_KEY
  • TYPESAFE_API_KEY
  • CLOUDFLARE_API_TOKEN
  • CLOUDFLARE_ACCOUNT_ID
  • CLOUDFLARE_GATEWAY_ID
  • AI_GATEWAY_API_KEY

Then:

TYPESAFE_ROUTER_LIVE_E2E=1 npm run test:e2e

License

MIT. Third-party models, services, and dependencies retain their own terms.