@lll9p/pi-anyrouter 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +251 -0
  3. package/index.ts +1221 -0
  4. package/package.json +53 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 zhenliangzhang
4
+ Copyright (c) 2026 xifan2333
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,251 @@
1
+ # pi-anyrouter
2
+
3
+ A pi provider extension that adapts requests to the client-specific shapes accepted by AnyRouter.
4
+
5
+ It currently supports both **Claude Code / Claude Agent SDK** requests and **Codex Responses Lite** requests. It does not require a separate local relay process.
6
+
7
+ ## Status
8
+
9
+ Confirmed working with provider `anyrouter` using Claude and Codex model routes:
10
+
11
+ ```bash
12
+ pi --model anyrouter/claude-opus-4-8 --no-session --no-tools -p "Reply with exactly OK"
13
+ pi --model anyrouter/gpt-5.6-sol --no-session --no-tools -p "Reply with exactly OK"
14
+ ```
15
+
16
+ Expected output:
17
+
18
+ ```text
19
+ OK
20
+ ```
21
+
22
+ ## What this package fixes
23
+
24
+ Some AnyRouter Claude endpoints, especially `claude-opus-4-6`, reject older or more generic Claude-style requests with errors like:
25
+
26
+ ```json
27
+ {"error":{"type":"new_api_error","message":"invalid claude code request (...)"},"type":"error"}
28
+ ```
29
+
30
+ This package selects an adapter by model family:
31
+
32
+ - Claude models use `POST /v1/messages?beta=true` with Claude Code headers, system blocks, metadata, and Anthropic SSE conversion.
33
+ - Codex models use `POST /v1/responses` with the Codex Responses Lite headers/body shape and OpenAI Responses SSE conversion.
34
+
35
+ ## Features
36
+
37
+ - Registers provider: `anyrouter`
38
+ - Supports Claude Code and Codex Responses Lite request validation
39
+ - No local relay/proxy required
40
+ - Converts tool names to Claude Code naming
41
+ - Supports reasoning via `thinking` + `output_config`
42
+ - Built-in request/response debug dump
43
+ - Retries transient upstream failures like HTTP 520/502/503/504 automatically
44
+ - Packaged so it can be shared as a pi package
45
+
46
+ ## Install
47
+
48
+ ## Option A: local development checkout
49
+
50
+ Clone into a normal directory:
51
+
52
+ ```bash
53
+ git clone https://github.com/xifan2333/pi-anyrouter.git
54
+ cd pi-anyrouter
55
+ ```
56
+
57
+ Install as a pi package from the local path:
58
+
59
+ ```bash
60
+ pi install .
61
+ ```
62
+
63
+ Or install directly from a path later:
64
+
65
+ ```bash
66
+ pi install /absolute/path/to/pi-anyrouter
67
+ ```
68
+
69
+ ## Option B: direct git install
70
+
71
+ Once pushed to GitHub, install with:
72
+
73
+ ```bash
74
+ pi install git:github.com/xifan2333/pi-anyrouter
75
+ ```
76
+
77
+ Or pin a ref/tag:
78
+
79
+ ```bash
80
+ pi install git:github.com/xifan2333/pi-anyrouter@<tag>
81
+ ```
82
+
83
+ ## Option C: manual extension placement
84
+
85
+ If you only want the extension file layout, place it at either:
86
+
87
+ - `~/.pi/agent/extensions/anyrouter/index.ts`
88
+ - `.pi/extensions/anyrouter/index.ts`
89
+
90
+ Then reload pi:
91
+
92
+ ```text
93
+ /reload
94
+ ```
95
+
96
+ ## Config
97
+
98
+ Create:
99
+
100
+ - `~/.pi/agent/anyrouter.json`
101
+
102
+ Example:
103
+
104
+ ```json
105
+ {
106
+ "baseUrl": "https://anyrouter.top",
107
+ "apiKey": "YOUR_ANYROUTER_KEY_OR_ENV_NAME",
108
+ "models": [
109
+ {
110
+ "id": "claude-opus-4-8",
111
+ "name": "Claude Opus 4.8",
112
+ "reasoning": true,
113
+ "input": ["text"],
114
+ "cost": {
115
+ "input": 0,
116
+ "output": 0,
117
+ "cacheRead": 0,
118
+ "cacheWrite": 0
119
+ },
120
+ "contextWindow": 200000,
121
+ "maxTokens": 32000
122
+ }
123
+ ]
124
+ }
125
+ ```
126
+
127
+ You can also override config values with environment variables:
128
+
129
+ - `PI_ANYROUTER_CC_CONFIG`
130
+ - `PI_ANYROUTER_CC_BASE_URL`
131
+ - `PI_ANYROUTER_CC_API_KEY`
132
+ - `PI_ANYROUTER_CC_MAX_RETRIES` (default: `10`)
133
+ - `PI_ANYROUTER_CC_STREAM_MODE` (default: `force`)
134
+
135
+ Notes:
136
+
137
+ - `apiKey` can be a literal key or an environment variable name.
138
+ - shell-command values like `"!command"` are intentionally not supported.
139
+ - this package reads `~/.pi/agent/anyrouter.json` by default rather than `models.json`.
140
+ - `PI_ANYROUTER_CC_STREAM_MODE=auto` tries real SSE streaming first and falls back to the old non-stream JSON path if streaming fails before any content arrives.
141
+ - `PI_ANYROUTER_CC_STREAM_MODE=force` uses SSE only; `PI_ANYROUTER_CC_STREAM_MODE=off` disables SSE.
142
+
143
+ ## Use
144
+
145
+ After installation/configuration:
146
+
147
+ ```text
148
+ /reload
149
+ /model
150
+ ```
151
+
152
+ Choose a configured model, for example:
153
+
154
+ - `anyrouter / claude-opus-4-8`
155
+ - `anyrouter / gpt-5.6-sol`
156
+
157
+ Or run directly:
158
+
159
+ ```bash
160
+ pi --model anyrouter/gpt-5.6-sol
161
+ ```
162
+
163
+ ## Debugging
164
+
165
+ Enable request/response dumps:
166
+
167
+ ```bash
168
+ PI_ANYROUTER_CC_DEBUG=1 pi --model anyrouter/gpt-5.6-sol -p "Reply with exactly OK"
169
+ ```
170
+
171
+ Optional custom debug directory:
172
+
173
+ ```bash
174
+ PI_ANYROUTER_CC_DEBUG=1 \
175
+ PI_ANYROUTER_CC_DEBUG_DIR=/tmp/anyrouter-cc-debug \
176
+ pi --model anyrouter/gpt-5.6-sol -p "Reply with exactly OK"
177
+ ```
178
+
179
+ Default debug output directory:
180
+
181
+ - `.pi/anyrouter-cc-debug/`
182
+
183
+ If AnyRouter intermittently returns `HTTP 520` / `Origin Error`, this package retries transient upstream failures automatically with exponential backoff. You can tune the retry count with `PI_ANYROUTER_CC_MAX_RETRIES`.
184
+
185
+ The runtime uses SSE by default (`PI_ANYROUTER_CC_STREAM_MODE=force`). For troubleshooting, you can force old behavior with:
186
+
187
+ ```bash
188
+ PI_ANYROUTER_CC_STREAM_MODE=off pi --model anyrouter/claude-opus-4-8 -p "Reply with exactly OK"
189
+ ```
190
+
191
+ ## Share as a pi package
192
+
193
+ This repo is already structured as a pi package through `package.json`:
194
+
195
+ ```json
196
+ {
197
+ "keywords": ["pi-package"],
198
+ "pi": {
199
+ "extensions": ["./index.ts"]
200
+ }
201
+ }
202
+ ```
203
+
204
+ That means users can install it with:
205
+
206
+ ```bash
207
+ pi install git:github.com/xifan2333/pi-anyrouter
208
+ ```
209
+
210
+ ## Publish checklist
211
+
212
+ ### Publish to GitHub
213
+
214
+ ```bash
215
+ git init
216
+ git add .
217
+ git commit -m "feat: add AnyRouter Claude and Codex adapters"
218
+ git branch -M main
219
+ git remote add origin git@github.com:xifan2333/pi-anyrouter.git
220
+ git push -u origin main
221
+ ```
222
+
223
+ ### Optional npm publish
224
+
225
+ If you want npm distribution too:
226
+
227
+ ```bash
228
+ npm publish
229
+ ```
230
+
231
+ Then users can install with:
232
+
233
+ ```bash
234
+ pi install npm:pi-anyrouter
235
+ ```
236
+
237
+ ## Important behavior
238
+
239
+ This package implements the Claude Code / Claude Agent SDK and Codex Responses Lite request shapes currently accepted by AnyRouter.
240
+
241
+ If AnyRouter changes its validation again, enable debug dumps and compare the request shape against fresh official client captures.
242
+
243
+ ## Credits
244
+
245
+ Based on [pi-anyrouter-cc](https://github.com/phy-zhangzl/pi-anyrouter-cc) by zhenliangzhang, licensed under the MIT License.
246
+
247
+ This is an unofficial community project and is not affiliated with Anthropic, OpenAI, or AnyRouter.
248
+
249
+ ## License
250
+
251
+ MIT. See [LICENSE](LICENSE) for the original and modification copyright notices.