@cortexkit/opencode-anthropic-auth 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +500 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +260 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +665 -0
- package/dist/prompt-context.d.ts +18 -0
- package/dist/prompt-context.js +95 -0
- package/dist/transform.d.ts +77 -0
- package/dist/transform.js +450 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ex Machina
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
# CortexKit Anthropic Auth for OpenCode and Pi
|
|
2
|
+
|
|
3
|
+
Claude Pro/Max OAuth support for both [OpenCode](https://opencode.ai) and [Pi](https://pi.dev), maintained by CortexKit.
|
|
4
|
+
|
|
5
|
+
This repo is a Bun workspace monorepo with two user-facing integrations and one shared core package. The OpenCode package is a CortexKit-maintained fork of the original `@ex-machina/opencode-anthropic-auth` plugin. The Pi package is a native Pi provider extension that overrides Pi's built-in Anthropic provider. Both integrations share the same Anthropic OAuth, fallback-account, quota, prompt-cache, relay, dump, and request-signing logic through `@cortexkit/anthropic-auth-core`.
|
|
6
|
+
|
|
7
|
+
## Packages
|
|
8
|
+
|
|
9
|
+
| Package | Agent | Purpose |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `@cortexkit/opencode-anthropic-auth` | OpenCode | OpenCode plugin and CLI for Claude OAuth, request rewriting, fallback accounts, quotas, cache controls, dumps, and relay setup. |
|
|
12
|
+
| `@cortexkit/pi-anthropic-auth` | Pi | Pi package/extension that registers a CortexKit Anthropic provider under Pi's built-in `anthropic` provider ID. |
|
|
13
|
+
| `@cortexkit/anthropic-auth-core` | Shared | Reusable OAuth, account, quota, cache, relay, dump, SSE, and request-signing logic used by both integrations. |
|
|
14
|
+
|
|
15
|
+
## Support matrix
|
|
16
|
+
|
|
17
|
+
| Capability | OpenCode | Pi |
|
|
18
|
+
| --- | --- | --- |
|
|
19
|
+
| Primary Claude Pro/Max OAuth | OpenCode `/connect anthropic` | Pi `/login anthropic` |
|
|
20
|
+
| Provider integration point | OpenCode plugin fetch/request transform | Pi `registerProvider("anthropic")` provider override |
|
|
21
|
+
| Sidecar config | `~/.config/opencode/anthropic-auth.json` | `~/.pi/agent/anthropic-auth.json` |
|
|
22
|
+
| Commands | `/claude-cache`, `/claude-quota`, `/claude-dump` | `/claude-cache`, `/claude-quota`, `/claude-dump` |
|
|
23
|
+
| Fallback accounts, quota routing, relay, dumps | Supported | Supported through the same shared core and Pi sidecar |
|
|
24
|
+
|
|
25
|
+
## What CortexKit adds over the original plugin
|
|
26
|
+
|
|
27
|
+
- **Fallback Claude accounts**: keep each agent's normal Anthropic login as the primary account, then route to ordered fallback OAuth accounts on auth/quota/rate-limit failures.
|
|
28
|
+
- **Quota-aware routing**: skip main or fallback accounts when their 5-hour or 7-day Claude quota falls below your configured minimum.
|
|
29
|
+
- **Persistent Claude cache controls**: manage Anthropic 1-hour prompt caching from `/claude-cache` with explicit, automatic, or hybrid modes.
|
|
30
|
+
- **Live quota visibility**: use `/claude-quota` to see main and fallback quota state, reset times, and refresh errors.
|
|
31
|
+
- **User-owned Cloudflare relay**: optionally provision your own Worker relay to reduce repeated client upload bytes for large OpenCode or Pi requests.
|
|
32
|
+
- **Claude-compatible request hardening**: final-body billing signing, safer token refresh persistence, replay-safe fallback retries, and subagent cache isolation.
|
|
33
|
+
|
|
34
|
+
## What these integrations do
|
|
35
|
+
|
|
36
|
+
- Let OpenCode and Pi use Claude Pro/Max OAuth credentials instead of an Anthropic API key.
|
|
37
|
+
- In OpenCode, intercept the final Anthropic request and rewrite it into the Claude-compatible shape expected by Anthropic OAuth access.
|
|
38
|
+
- In Pi, replace Pi's built-in Anthropic provider with a CortexKit provider override that uses the same Claude-compatible request path.
|
|
39
|
+
- Add Claude billing headers with stable `cc_version` and body-derived `cch` signing.
|
|
40
|
+
- Support fallback Claude accounts stored in a local per-agent sidecar file.
|
|
41
|
+
- Keep fallback OAuth tokens fresh in the background.
|
|
42
|
+
- Apply quota thresholds before routing to main or fallback accounts.
|
|
43
|
+
- Add `/claude-cache`, `/claude-quota`, and `/claude-dump` commands.
|
|
44
|
+
- Optionally relay large requests through a Cloudflare Worker owned by the user.
|
|
45
|
+
|
|
46
|
+
## Install
|
|
47
|
+
|
|
48
|
+
### OpenCode
|
|
49
|
+
|
|
50
|
+
Add the OpenCode plugin to your OpenCode configuration:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
{
|
|
54
|
+
"plugin": ["@cortexkit/opencode-anthropic-auth"]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Pinning is strongly recommended for any OpenCode plugin:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"plugin": ["@cortexkit/opencode-anthropic-auth@1.0.0"]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
After changing plugin config, restart OpenCode.
|
|
67
|
+
|
|
68
|
+
> [!TIP]
|
|
69
|
+
> If OpenCode keeps using an old build, clear OpenCode's plugin cache with `rm -rf ~/.cache/opencode` and restart.
|
|
70
|
+
|
|
71
|
+
### Pi
|
|
72
|
+
|
|
73
|
+
Install the Pi package with Pi's package manager:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pi install npm:@cortexkit/pi-anthropic-auth@1.0.0
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For an unpinned install:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pi install npm:@cortexkit/pi-anthropic-auth
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
To try it for one run without adding it to Pi settings:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pi -e npm:@cortexkit/pi-anthropic-auth
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The Pi package registers a CortexKit Anthropic provider extension under Pi's built-in `anthropic` provider ID. After installation, start or restart Pi and authenticate with Pi's normal login command:
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
/login anthropic
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Pi package state lives separately from OpenCode in:
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
~/.pi/agent/anthropic-auth.json
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Override the path with `PI_ANTHROPIC_AUTH_FILE`. The package also respects `PI_AGENT_DIR` when deriving the default sidecar path.
|
|
104
|
+
|
|
105
|
+
## Primary account authentication
|
|
106
|
+
|
|
107
|
+
Each integration keeps the host agent's normal Anthropic login as the primary account.
|
|
108
|
+
|
|
109
|
+
For OpenCode, use OpenCode's Anthropic auth flow:
|
|
110
|
+
|
|
111
|
+
```text
|
|
112
|
+
/connect anthropic
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The primary account remains OpenCode's built-in `anthropic` auth entry. The OpenCode plugin intercepts final Anthropic requests and supplies the OAuth headers and request transforms needed for Claude Pro/Max access.
|
|
116
|
+
|
|
117
|
+
OpenCode's upstream authentication options are still supported:
|
|
118
|
+
|
|
119
|
+
- Claude Pro/Max OAuth through `claude.ai`.
|
|
120
|
+
- Anthropic Console OAuth that creates an API key.
|
|
121
|
+
- Manually entered Anthropic API key.
|
|
122
|
+
|
|
123
|
+
For Pi, install the Pi package, restart Pi, then use Pi's Anthropic login flow:
|
|
124
|
+
|
|
125
|
+
```text
|
|
126
|
+
/login anthropic
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The Pi package registers under Pi's built-in `anthropic` provider ID and stores primary OAuth credentials through Pi's normal credential flow. CortexKit package state, fallback accounts, cache mode, dump mode, and relay config live in the Pi sidecar file.
|
|
130
|
+
|
|
131
|
+
## Sidecar config
|
|
132
|
+
|
|
133
|
+
OpenCode package state lives in:
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
~/.config/opencode/anthropic-auth.json
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Override the OpenCode path with `OPENCODE_ANTHROPIC_AUTH_FILE`.
|
|
140
|
+
|
|
141
|
+
Pi package state uses the same JSON shape but a separate file:
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
~/.pi/agent/anthropic-auth.json
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Override the Pi path with `PI_ANTHROPIC_AUTH_FILE`.
|
|
148
|
+
|
|
149
|
+
Example:
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"version": 1,
|
|
154
|
+
"main": { "type": "opencode", "provider": "anthropic" },
|
|
155
|
+
"fallbackOn": [401, 403, 429],
|
|
156
|
+
"refresh": {
|
|
157
|
+
"enabled": true,
|
|
158
|
+
"intervalMinutes": 10,
|
|
159
|
+
"refreshBeforeExpiryMinutes": 30
|
|
160
|
+
},
|
|
161
|
+
"quota": {
|
|
162
|
+
"enabled": true,
|
|
163
|
+
"checkIntervalMinutes": 5,
|
|
164
|
+
"minimumRemaining": {
|
|
165
|
+
"five_hour": 10,
|
|
166
|
+
"seven_day": 20
|
|
167
|
+
},
|
|
168
|
+
"failClosedOnUnknownQuota": true
|
|
169
|
+
},
|
|
170
|
+
"claudeCache": {
|
|
171
|
+
"enabled": false,
|
|
172
|
+
"mode": "explicit"
|
|
173
|
+
},
|
|
174
|
+
"dump": {
|
|
175
|
+
"enabled": false
|
|
176
|
+
},
|
|
177
|
+
"relay": {
|
|
178
|
+
"enabled": false,
|
|
179
|
+
"url": "https://opencode-anthropic-relay.example.workers.dev",
|
|
180
|
+
"token": "relay-shared-secret",
|
|
181
|
+
"transport": "http",
|
|
182
|
+
"fallbackToDirect": true
|
|
183
|
+
},
|
|
184
|
+
"accounts": []
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The `claudeCache` block controls the `/claude-cache` command's persisted mode. The `main` field identifies OpenCode's primary auth entry; Pi keeps primary OAuth credentials in Pi's own credential store, but uses the same sidecar shape for CortexKit settings and fallback accounts.
|
|
189
|
+
|
|
190
|
+
## Fallback accounts
|
|
191
|
+
|
|
192
|
+
Fallback accounts are separate Claude OAuth accounts managed by this plugin. The main account is tried first unless quota policy says it is currently unusable. Fallbacks are then tried in sidecar order when the primary request returns a configured fallback status.
|
|
193
|
+
|
|
194
|
+
Default fallback statuses:
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
[401, 403, 429]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Add and inspect OpenCode fallback accounts with the CLI:
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
bunx @cortexkit/opencode-anthropic-auth login personal-alt
|
|
204
|
+
bunx @cortexkit/opencode-anthropic-auth list
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Prefer npm? Use `npx -y @cortexkit/opencode-anthropic-auth ...` with the same subcommands.
|
|
208
|
+
|
|
209
|
+
For Pi fallback accounts, write the same account JSON shape to `~/.pi/agent/anthropic-auth.json`. The CLI helper currently lives in the OpenCode package, so you can also point it at Pi's sidecar path when logging in a fallback account:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
OPENCODE_ANTHROPIC_AUTH_FILE="$HOME/.pi/agent/anthropic-auth.json" \
|
|
213
|
+
bunx @cortexkit/opencode-anthropic-auth login personal-alt
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Fallback retries are only attempted when the request body is safely replayable. If the original body is non-replayable or already consumed, the plugin returns the primary response unchanged.
|
|
217
|
+
|
|
218
|
+
### Token refresh
|
|
219
|
+
|
|
220
|
+
Fallback OAuth tokens refresh in the background so idle accounts do not expire before they are needed. Refresh token rotation is persisted immediately. The plugin also re-reads the latest sidecar account before refreshing, which avoids using stale refresh-token snapshots when multiple background paths run close together.
|
|
221
|
+
|
|
222
|
+
If Anthropic reports `invalid_grant`, that fallback account must be logged in again.
|
|
223
|
+
|
|
224
|
+
## Quota-aware routing
|
|
225
|
+
|
|
226
|
+
When `quota.enabled` is true, the plugin checks Anthropic's OAuth usage endpoint and applies the configured remaining-quota thresholds to both main and fallback accounts.
|
|
227
|
+
|
|
228
|
+
Example:
|
|
229
|
+
|
|
230
|
+
```json
|
|
231
|
+
"minimumRemaining": {
|
|
232
|
+
"five_hour": 10,
|
|
233
|
+
"seven_day": 20
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
With this config, an account is skipped when it has less than 10% remaining in the 5-hour window or less than 20% remaining in the 7-day window. The aliases `5h` and `1w` are also accepted.
|
|
238
|
+
|
|
239
|
+
Main-account quota is cached. If the main account is known to be exhausted, the plugin skips it until the relevant reset time. If the cached main quota is stale but usable, the request proceeds and quota refresh happens in the background.
|
|
240
|
+
|
|
241
|
+
Show current quota state:
|
|
242
|
+
|
|
243
|
+
```text
|
|
244
|
+
/claude-quota
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
In OpenCode, this includes the main Anthropic account and sidecar fallback accounts. In Pi, the command reports sidecar fallback account quota state from `~/.pi/agent/anthropic-auth.json`.
|
|
248
|
+
|
|
249
|
+
Reset times are rendered as relative durations, such as `resets in 10m` or `resets in 1h 15m`.
|
|
250
|
+
|
|
251
|
+
## Claude prompt cache control
|
|
252
|
+
|
|
253
|
+
Both OpenCode and Pi packages add a slash command for Anthropic's 1-hour ephemeral prompt-cache TTL:
|
|
254
|
+
|
|
255
|
+
```text
|
|
256
|
+
/claude-cache
|
|
257
|
+
/claude-cache on
|
|
258
|
+
/claude-cache off
|
|
259
|
+
/claude-cache mode explicit
|
|
260
|
+
/claude-cache mode automatic
|
|
261
|
+
/claude-cache mode hybrid
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Without arguments, `/claude-cache` shows the current setting.
|
|
265
|
+
|
|
266
|
+
Modes:
|
|
267
|
+
|
|
268
|
+
- `explicit` keeps OpenCode's explicit cache breakpoints and adds `ttl: "1h"` to them.
|
|
269
|
+
- `automatic` removes block-level cache controls and sends a top-level `cache_control` object.
|
|
270
|
+
- `hybrid` (recommended) uses top-level automatic caching while keeping stable explicit anchors near the beginning of the request.
|
|
271
|
+
|
|
272
|
+
In OpenCode, subagent requests do not receive 1-hour TTL caching. The plugin detects child sessions through OpenCode's `x-parent-session-id` header, strips that internal header before forwarding to Anthropic, and leaves default ephemeral caching in place for those requests.
|
|
273
|
+
|
|
274
|
+
### Estimate cache savings from OpenCode history
|
|
275
|
+
|
|
276
|
+
The repo includes an OpenCode SQLite analyzer that compares estimated Claude cost under three scenarios: no prompt cache, Anthropic's default 5-minute cache, and this plugin's 1-hour cache mode.
|
|
277
|
+
|
|
278
|
+
From a repo checkout:
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
bun run analyze:cache -- --days 7
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Useful variants:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
# Restrict to one OpenCode session
|
|
288
|
+
bun run analyze:cache -- --session ses_... --days 4
|
|
289
|
+
|
|
290
|
+
# Emit machine-readable output
|
|
291
|
+
bun run analyze:cache -- --days 7 --json
|
|
292
|
+
|
|
293
|
+
# Use a non-default OpenCode DB path
|
|
294
|
+
bun run analyze:cache -- --db ~/.local/share/opencode/opencode.db --days 30
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
The script reads OpenCode usage data from `~/.local/share/opencode/opencode.db` by default. It uses recorded prompt, cache-read, cache-write, and output tokens, then estimates counterfactual 5-minute and no-cache costs from the same turns. The default 5-minute expiry threshold is 5 minutes; override it with `--idle-threshold-min <minutes>` if needed.
|
|
298
|
+
|
|
299
|
+
This analyzer is OpenCode-specific because it reads OpenCode's local message database.
|
|
300
|
+
|
|
301
|
+
## Optional Cloudflare relay
|
|
302
|
+
|
|
303
|
+
The relay is opt-in and user-owned. CortexKit does not run shared relay infrastructure for this plugin.
|
|
304
|
+
|
|
305
|
+
When enabled, the package sends large Anthropic request bodies to a Cloudflare Worker that you own. The first request for a session sends a full body; later requests send compact patches keyed by session affinity (`x-session-affinity` in OpenCode, Pi's stream `sessionId` in Pi). The Worker reconstructs the full Anthropic `/v1/messages` request and streams Anthropic's SSE response back to the client.
|
|
306
|
+
|
|
307
|
+
New relay setups default to HTTP transport:
|
|
308
|
+
|
|
309
|
+
```json
|
|
310
|
+
"transport": "http"
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
HTTP is the safest release default and still sends compact full-sync/patch payloads through your Worker. WebSocket is available as an opt-in persistent session transport:
|
|
314
|
+
|
|
315
|
+
```json
|
|
316
|
+
"transport": "websocket"
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
WebSocket mode uses protocol v2 on `/ws`, keeps one connection per OpenCode `x-session-affinity`, and serializes same-session requests until the previous stream finishes. Existing deployed Workers must be redeployed with this package's current Worker script before `"transport": "websocket"` will work; older relay Workers only understand the legacy protocol.
|
|
320
|
+
|
|
321
|
+
Set up a relay in your Cloudflare account with the OpenCode package CLI:
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
CLOUDFLARE_API_TOKEN=... CLOUDFLARE_ACCOUNT_ID=... bunx @cortexkit/opencode-anthropic-auth relay setup
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Or with npm:
|
|
328
|
+
|
|
329
|
+
```bash
|
|
330
|
+
CLOUDFLARE_API_TOKEN=... CLOUDFLARE_ACCOUNT_ID=... npx -y @cortexkit/opencode-anthropic-auth relay setup
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
The setup command:
|
|
334
|
+
|
|
335
|
+
1. Creates a Cloudflare KV namespace.
|
|
336
|
+
2. Uploads the relay Worker module.
|
|
337
|
+
3. Generates a relay shared secret.
|
|
338
|
+
4. Enables the Worker.
|
|
339
|
+
5. Writes the local `relay` block to `~/.config/opencode/anthropic-auth.json`.
|
|
340
|
+
|
|
341
|
+
For Pi, copy the generated `relay` block into `~/.pi/agent/anthropic-auth.json` if you want the Pi package to use the same user-owned Worker.
|
|
342
|
+
|
|
343
|
+
The Cloudflare API token is used only during setup and is not stored by the plugin. Re-running setup with the same Worker name uploads the current Worker script again; this is how existing relay Workers are upgraded for protocol changes such as WebSocket v2.
|
|
344
|
+
|
|
345
|
+
If relay setup or transport fails before streaming begins, the plugin falls back to direct Anthropic requests unless `fallbackToDirect` is set to `false`.
|
|
346
|
+
|
|
347
|
+
> [!NOTE]
|
|
348
|
+
> The relay reduces upload bytes from your machine to Cloudflare. Anthropic still receives the complete `/v1/messages` request from the Worker, so the relay does not reduce Anthropic input tokens or billing by itself. Use prompt caching for server-side cache benefits.
|
|
349
|
+
|
|
350
|
+
### Relay diagnostics
|
|
351
|
+
|
|
352
|
+
Relay diagnostics are written to a temp-file log:
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
tail -f "$(node -p 'require("node:os").tmpdir()')/opencode-anthropic-auth.log"
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Successful relay usage logs entries such as:
|
|
359
|
+
|
|
360
|
+
```text
|
|
361
|
+
configured transport=websocket protocol=2
|
|
362
|
+
used relay transport=websocket protocol=2 mode=patch
|
|
363
|
+
used relay transport=http protocol=1 mode=full_sync
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
Direct fallback logs `falling back direct`.
|
|
367
|
+
|
|
368
|
+
### Request dumps
|
|
369
|
+
|
|
370
|
+
For relay/cache debugging, enable exact request dumps from inside OpenCode or Pi:
|
|
371
|
+
|
|
372
|
+
```text
|
|
373
|
+
/claude-dump on
|
|
374
|
+
/claude-dump off
|
|
375
|
+
/claude-dump
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
When enabled, the plugin writes artifacts under the OS temp directory:
|
|
379
|
+
|
|
380
|
+
```bash
|
|
381
|
+
ls "$(node -p 'require("node:os").tmpdir()')/opencode-anthropic-auth-dumps"
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
Each request gets:
|
|
385
|
+
|
|
386
|
+
- `*.body.json` — final rewritten Anthropic request body.
|
|
387
|
+
- `*.relay.json` — redacted relay payload/frame metadata.
|
|
388
|
+
- `*.meta.json` — hashes, byte counts, diff ranges, model, `messages[0]` hash, later-message hash, and cache-relevant structure.
|
|
389
|
+
|
|
390
|
+
Dump state is persisted in the active sidecar config as `dump.enabled` (`~/.config/opencode/anthropic-auth.json` for OpenCode, `~/.pi/agent/anthropic-auth.json` for Pi). Dumps may contain prompt content and should be treated as sensitive local debugging artifacts.
|
|
391
|
+
|
|
392
|
+
## Environment variables
|
|
393
|
+
|
|
394
|
+
| Variable | Description |
|
|
395
|
+
| --- | --- |
|
|
396
|
+
| `ANTHROPIC_BASE_URL` | Override the Anthropic API endpoint. Must be HTTP(S). |
|
|
397
|
+
| `ANTHROPIC_INSECURE` | Set to `1` or `true` to skip TLS verification when `ANTHROPIC_BASE_URL` is set. |
|
|
398
|
+
| `OPENCODE_ANTHROPIC_AUTH_FILE` | Override the OpenCode sidecar config path. |
|
|
399
|
+
| `PI_ANTHROPIC_AUTH_FILE` | Override the Pi sidecar config path. |
|
|
400
|
+
| `PI_AGENT_DIR` | Override Pi's agent directory when deriving the default sidecar path. |
|
|
401
|
+
| `CLOUDFLARE_API_TOKEN` | Cloudflare token used by `bunx @cortexkit/opencode-anthropic-auth relay setup`. Not stored. |
|
|
402
|
+
| `CLOUDFLARE_ACCOUNT_ID` | Cloudflare account ID used by relay setup. |
|
|
403
|
+
|
|
404
|
+
## Request rewriting
|
|
405
|
+
|
|
406
|
+
For Claude Pro/Max OAuth requests, the plugin works at the final Anthropic wire-request layer:
|
|
407
|
+
|
|
408
|
+
1. Rewrites request URLs when `ANTHROPIC_BASE_URL` is configured.
|
|
409
|
+
2. Normalizes Claude-compatible OAuth headers and beta flags.
|
|
410
|
+
3. Removes OpenCode-specific identity text from system blocks.
|
|
411
|
+
4. Prepends Claude Code identity and billing-header blocks.
|
|
412
|
+
5. Rewrites cache controls according to `/claude-cache` mode.
|
|
413
|
+
6. Renames MCP tool names into Claude-compatible PascalCase form.
|
|
414
|
+
7. Computes final-body `cch` over the fully serialized request body.
|
|
415
|
+
|
|
416
|
+
The sanitizer is anchor-based: it removes paragraphs containing known OpenCode documentation or source anchors, performs a small set of inline replacements, and preserves the rest of the prompt including user/project instructions, tool policy, environment context, and file paths.
|
|
417
|
+
|
|
418
|
+
## Development
|
|
419
|
+
|
|
420
|
+
Workspace layout:
|
|
421
|
+
|
|
422
|
+
```text
|
|
423
|
+
packages/core Shared Anthropic auth core
|
|
424
|
+
packages/opencode OpenCode plugin and CLI
|
|
425
|
+
packages/pi Pi package/extension
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
Install dependencies:
|
|
429
|
+
|
|
430
|
+
```bash
|
|
431
|
+
bun install
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
Run checks:
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
bun run typecheck
|
|
438
|
+
bun run test
|
|
439
|
+
bun run build
|
|
440
|
+
bun run lint
|
|
441
|
+
bun run format:check
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
Inspect package contents:
|
|
445
|
+
|
|
446
|
+
```bash
|
|
447
|
+
bun run pack:core:dry
|
|
448
|
+
bun run pack:opencode:dry
|
|
449
|
+
bun run pack:pi:dry
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
Test a local build with OpenCode:
|
|
453
|
+
|
|
454
|
+
```bash
|
|
455
|
+
bun run dev
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
This builds the plugin, symlinks the output into `.opencode/plugins/`, and starts `tsc --watch`. Restart OpenCode after starting the dev script and after rebuilds.
|
|
459
|
+
|
|
460
|
+
Clean the local dev symlink with:
|
|
461
|
+
|
|
462
|
+
```bash
|
|
463
|
+
bun run dev:clean
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
## Release
|
|
467
|
+
|
|
468
|
+
This repo uses CortexKit's tag-driven release workflow.
|
|
469
|
+
|
|
470
|
+
Preview a release:
|
|
471
|
+
|
|
472
|
+
```bash
|
|
473
|
+
./scripts/release.sh 1.8.0 --dry
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
Create and push the release tag:
|
|
477
|
+
|
|
478
|
+
```bash
|
|
479
|
+
./scripts/release.sh 1.8.0
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
Wait for GitHub Actions:
|
|
483
|
+
|
|
484
|
+
```bash
|
|
485
|
+
./scripts/wait-release.sh v1.8.0
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
The release workflow runs checks, publishes the core, OpenCode, and Pi packages to npm with provenance, and creates the GitHub release.
|
|
489
|
+
|
|
490
|
+
## Troubleshooting
|
|
491
|
+
|
|
492
|
+
- Clear OpenCode's plugin cache after plugin config changes: `rm -rf ~/.cache/opencode`.
|
|
493
|
+
- Restart OpenCode or Pi after changing sidecar config; some settings are loaded at startup.
|
|
494
|
+
- If an OpenCode fallback account shows `invalid_grant`, run `bunx @cortexkit/opencode-anthropic-auth login <label>` again for that account.
|
|
495
|
+
- Tail relay diagnostics when debugging relay setup: `tail -f "$(node -p 'require("node:os").tmpdir()')/opencode-anthropic-auth.log"`.
|
|
496
|
+
- Use `/claude-quota` to inspect quota and refresh errors surfaced by the plugin.
|
|
497
|
+
|
|
498
|
+
## License
|
|
499
|
+
|
|
500
|
+
MIT
|
package/dist/cli.d.ts
ADDED