@bismawy/pi-agentrouter

AgentRouter provider for pi — GPT-6 Astra, GPT-5.6 Sol, Claude Opus 4.8/5, DeepSeek V4 Flash and GLM 5.3 under one namespace, with self-healing WAF recovery (history redaction + auto-translation)

Packages

Package details

extension

Install @bismawy/pi-agentrouter from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:@bismawy/pi-agentrouter
Package
@bismawy/pi-agentrouter
Version
1.5.0
Published
Sep 22, 2026
Downloads
2,126/mo · 132/wk
Author
bismawy
License
MIT
Types
extension
Size
68.6 KB
Dependencies
0 dependencies · 1 peer
Pi manifest JSON
{
  "image": "https://raw.githubusercontent.com/bismawy/pi-agentrouter/main/assets/screenshot.webp",
  "extensions": [
    "./index.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

pi-agentrouter

Unified AgentRouter provider for the pi coding agent — routes GPT-6 Astra, GPT-5.6 Sol, Claude Opus 4.8 / 5, DeepSeek V4 Flash, and GLM 5.3 under a single agentrouter/ namespace, with self-healing WAF recovery (history redaction + on-demand translation) and payload sanitization.

pi package · npm · Issues · Changelog

npm license

What it does

  • Per-model protocol routing: GPT-6 Astra uses OpenAI Responses, Claude models use Anthropic Messages, and the rest use OpenAI Completions — all under one agentrouter/ provider.
  • WAF header & language guard: forces Pi's canonical system header to byte 0 (prevents 400 content-blocked when custom instructions precede it) and applies language framing on user turns.
  • Self-healing WAF recovery: when AgentRouter's content filter false-positives on multilingual histories, older turns are redacted in two stages and the turn is marked retryable — and when the newest turn itself is the trigger, it is translated to English instead of being dropped. The latest request survives without user disruption.
  • Payload sanitization: strips ANSI escape codes, null bytes, control characters, and orphan Unicode surrogates before dispatch.
  • Prompt cache affinity: injects sendSessionAffinityHeaders: true by default to maximize server-side cache hits.

Registered models

Model Context Input Protocol Thinking
agentrouter/gpt-6-astra 272k* text, image OpenAI Responses lowxhigh
agentrouter/gpt-5.6-sol 272k text, image OpenAI Completions lowxhigh
agentrouter/claude-opus-5 200k text, image Anthropic Messages lowxhigh
agentrouter/claude-opus-4-8 200k text, image Anthropic Messages lowxhigh
agentrouter/deepseek-v4-flash 131k text OpenAI Completions lowxhigh
agentrouter/glm-5.3 131k text OpenAI Completions lowxhigh

*Astra currently reuses Sol's configured 272,000-token context and 16,384-token output limits; these are local defaults, not confirmed AgentRouter limits. Responses API is required for function tools with reasoning enabled. Existing models keep their original protocols.

Install

pi install npm:@bismawy/pi-agentrouter

Then supply your AgentRouter API key one of three ways:

  • /login agentrouter in a Pi session
  • export AGENTROUTER_API_KEY="your-api-key"
  • providers.agentrouter.apiKey in ~/.pi/agent/models.json

No account yet? Register via AgentRouter (referral) for a $50 bonus.

Pick any model with /model (e.g. agentrouter/gpt-5.6-sol).

How it works

  1. The extension forces Pi's system prompt header to the very start of the payload.
  2. On a content-blocked / sensitive-words error, it marks the error retryable so Pi restarts the turn automatically.
  3. Stage 1 redacts older user turns ([Message withheld by local policy]); Stage 2 escalates to assistant turns if needed, keeping tool pairs intact.
  4. Stage 3 translates the newest turn: when there is nothing older left to redact, that turn is the trigger, and removing it would delete the user's actual request. It is translated to English instead — same meaning, English surface, which is what the language-ratio filter gates on. Fenced code and the session's own text are never touched.
  5. Redaction depth resets on new conversations or compaction.

The translator is a cheap model from another configured provider (ctx.modelRegistry), picked automatically (flash/lite models from your own OAuth providers first). AgentRouter itself can never be the translator — it would block the Indonesian text handed to it. Override with AGENTROUTER_TRANSLATOR="provider/modelId", disable with AGENTROUTER_TRANSLATE=0. Translations are cached in memory (bounded, FIFO eviction) and nothing is written to disk.

Text-only models (deepseek-v4-flash, glm-5.3) are declared input: ["text"] so image payloads from earlier turns don't produce AgentRouter 400 errors.

Development

Run the regression check against the actual registered extension hooks (no API key or network required):

bun ./check-framing.ts
# Or Node.js 22.18+:
npm test

Covers Responses text/image framing, empty content, function-call/result pairing, reasoning items, sticky WAF redaction, on-demand translation (trigger, cache, give-up, code-fence fidelity), and Chat Completions/Anthropic compatibility.

License

Distributed under the MIT license.

Developer

Developed and maintained by Bisma.