@moxxy/plugin-provider-claude-code 0.29.0 → 0.31.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/dist/provider.js CHANGED
@@ -1,22 +1,217 @@
1
+ import { estimateTextTokens } from '@moxxy/sdk';
2
+ import { CLAUDE_CODE_PROVIDER_ID } from './constants.js';
3
+ import { ClaudeProcessError, runClaudeProcess } from './process.js';
4
+ import { createProtocolState, parseClaudeRecord, serializeClaudePrompt } from './protocol.js';
5
+ export const CLAUDE_CODE_DEFAULT_MODEL = 'claude-sonnet-4-6';
6
+ const NON_TEXT_BLOCK_TOKENS = 256;
1
7
  /**
2
- * The Claude subscription provider is the standard Anthropic Messages API
3
- * with a bearer (OAuth) credential instead of an API key, so it reuses
4
- * `AnthropicProvider` in OAuth mode rather than duplicating the streaming /
5
- * tool-call event handling. This module just stamps in the Claude-specific
6
- * constants (beta headers + the required identity preamble) and forwards the
7
- * resolved token + refresh callback.
8
+ * Models offered by Claude Code subscriptions through the installed CLI.
9
+ *
10
+ * This is deliberately independent of the Anthropic API catalog: the CLI has
11
+ * its own availability policy, and this adapter only preserves streamed text.
12
+ * Unsupported capabilities are explicit so every host sees the same text-only
13
+ * contract rather than relying on how it interprets absent optional flags.
8
14
  */
9
- import { AnthropicProvider } from '@moxxy/plugin-provider-anthropic';
10
- import { CLAUDE_CODE_PROVIDER_ID, CLAUDE_CODE_SYSTEM, CLAUDE_OAUTH_BETA } from './constants.js';
15
+ const textOnlyCapabilities = {
16
+ supportsTools: false,
17
+ supportsStreaming: true,
18
+ supportsImages: false,
19
+ supportsDocuments: false,
20
+ supportsAudio: false,
21
+ supportsReasoning: false,
22
+ };
23
+ export const claudeCodeModels = [
24
+ { id: CLAUDE_CODE_DEFAULT_MODEL, contextWindow: 1_000_000, maxOutputTokens: 64_000, ...textOnlyCapabilities },
25
+ { id: 'claude-fable-5', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
26
+ { id: 'claude-opus-4-8', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
27
+ { id: 'claude-opus-4-7', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
28
+ { id: 'claude-opus-4-6', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
29
+ { id: 'claude-haiku-4-5-20251001', contextWindow: 200_000, maxOutputTokens: 64_000, ...textOnlyCapabilities },
30
+ ];
31
+ export class ClaudeCodeProvider {
32
+ name = CLAUDE_CODE_PROVIDER_ID;
33
+ models = claudeCodeModels;
34
+ executable;
35
+ defaultModel;
36
+ nativeTools;
37
+ permissionMode;
38
+ allowedTools;
39
+ cwd;
40
+ spawn;
41
+ startupTimeoutMs;
42
+ idleTimeoutMs;
43
+ maxStderrChars;
44
+ maxRecordChars;
45
+ constructor(config = {}) {
46
+ this.executable = config.executable ?? 'claude';
47
+ this.defaultModel = config.defaultModel ?? CLAUDE_CODE_DEFAULT_MODEL;
48
+ this.nativeTools = config.mode === 'native-tools';
49
+ if (config.permissionMode)
50
+ this.permissionMode = config.permissionMode;
51
+ this.allowedTools = config.allowedTools ?? [];
52
+ this.cwd = config.cwd ?? process.cwd();
53
+ if (config.spawn)
54
+ this.spawn = config.spawn;
55
+ if (config.startupTimeoutMs)
56
+ this.startupTimeoutMs = config.startupTimeoutMs;
57
+ if (config.idleTimeoutMs)
58
+ this.idleTimeoutMs = config.idleTimeoutMs;
59
+ if (config.maxStderrChars)
60
+ this.maxStderrChars = config.maxStderrChars;
61
+ if (config.maxRecordChars)
62
+ this.maxRecordChars = config.maxRecordChars;
63
+ }
64
+ async *stream(req) {
65
+ const model = req.model || this.defaultModel;
66
+ yield { type: 'message_start', model };
67
+ let prompt;
68
+ try {
69
+ assertSupportedModel(model);
70
+ assertTextOnlyRequest(req);
71
+ prompt = serializeClaudePrompt(req);
72
+ }
73
+ catch (error) {
74
+ yield nonRetryableError(error);
75
+ return;
76
+ }
77
+ const state = createProtocolState();
78
+ let terminal = false;
79
+ let lines;
80
+ try {
81
+ lines = runClaudeProcess({
82
+ executable: this.executable,
83
+ model,
84
+ prompt,
85
+ cwd: this.cwd,
86
+ nativeTools: this.nativeTools,
87
+ allowedTools: this.allowedTools,
88
+ ...(this.permissionMode ? { permissionMode: this.permissionMode } : {}),
89
+ ...(req.signal ? { signal: req.signal } : {}),
90
+ ...(this.spawn ? { spawn: this.spawn } : {}),
91
+ ...(this.startupTimeoutMs ? { startupTimeoutMs: this.startupTimeoutMs } : {}),
92
+ ...(this.idleTimeoutMs ? { idleTimeoutMs: this.idleTimeoutMs } : {}),
93
+ ...(this.maxStderrChars ? { maxStderrChars: this.maxStderrChars } : {}),
94
+ ...(this.maxRecordChars ? { maxRecordChars: this.maxRecordChars } : {}),
95
+ });
96
+ while (true) {
97
+ const next = await lines.next();
98
+ if (next.done) {
99
+ if (next.value.exitCode !== 0 && !terminal) {
100
+ yield classifyClaudeFailure(model, next.value.stderr || `Claude CLI exited unexpectedly with code ${next.value.exitCode}`);
101
+ return;
102
+ }
103
+ break;
104
+ }
105
+ for (const event of parseClaudeRecord(next.value, state)) {
106
+ if (event.type === 'message_end') {
107
+ terminal = true;
108
+ yield event;
109
+ return;
110
+ }
111
+ if (event.type === 'error') {
112
+ terminal = true;
113
+ yield classifyClaudeFailure(model, event.message);
114
+ return;
115
+ }
116
+ yield event;
117
+ }
118
+ }
119
+ if (!terminal) {
120
+ yield { type: 'error', message: 'Claude CLI ended without a terminal result record', retryable: false };
121
+ }
122
+ }
123
+ catch (error) {
124
+ if (error instanceof ClaudeProcessError) {
125
+ yield {
126
+ type: 'error',
127
+ message: error.message,
128
+ retryable: error.failure === 'startup_timeout' || error.failure === 'idle_timeout',
129
+ };
130
+ }
131
+ else {
132
+ yield classifySpawnFailure(error, this.executable);
133
+ }
134
+ }
135
+ finally {
136
+ await lines?.return({ exitCode: 0, stderr: '' });
137
+ }
138
+ }
139
+ async countTokens(req) {
140
+ let text = req.system ?? '';
141
+ let nonTextBlocks = 0;
142
+ for (const message of req.messages) {
143
+ for (const block of message.content) {
144
+ if (block.type === 'text')
145
+ text += block.text;
146
+ else
147
+ nonTextBlocks += 1;
148
+ }
149
+ }
150
+ for (const tool of req.tools ?? [])
151
+ text += tool.name + tool.description;
152
+ return estimateTextTokens(text) + nonTextBlocks * NON_TEXT_BLOCK_TOKENS;
153
+ }
154
+ }
11
155
  export function createClaudeCodeClient(config = {}) {
12
- return new AnthropicProvider({
13
- name: CLAUDE_CODE_PROVIDER_ID,
14
- oauthBeta: [...CLAUDE_OAUTH_BETA],
15
- systemPreamble: CLAUDE_CODE_SYSTEM,
16
- ...(config.oauthToken ? { oauthToken: config.oauthToken } : {}),
17
- ...(config.oauthExpiresAt !== undefined ? { oauthExpiresAt: config.oauthExpiresAt } : {}),
18
- ...(config.oauthRefresh ? { oauthRefresh: config.oauthRefresh } : {}),
19
- ...(config.defaultModel ? { defaultModel: config.defaultModel } : {}),
20
- });
156
+ return new ClaudeCodeProvider(config);
157
+ }
158
+ function assertSupportedModel(model) {
159
+ if (claudeCodeModels.some((candidate) => candidate.id === model))
160
+ return;
161
+ throw new Error(`Claude Code model "${model}" is not supported by this adapter. ` +
162
+ `Select a supported model: ${claudeCodeModels.map((candidate) => candidate.id).join(', ')}.`);
163
+ }
164
+ function assertTextOnlyRequest(req) {
165
+ if (req.tools && req.tools.length > 0) {
166
+ throw new Error('Claude CLI text transport does not support tools');
167
+ }
168
+ if (req.reasoning) {
169
+ throw new Error('Claude CLI text transport does not support reasoning');
170
+ }
171
+ }
172
+ function classifyClaudeFailure(model, detail) {
173
+ if (/model|unknown model|invalid model/i.test(detail)) {
174
+ return {
175
+ type: 'error',
176
+ message: `Claude Code rejected model "${model}": ${detail}. Select a supported model: ${claudeCodeModels.map((candidate) => candidate.id).join(', ')}.`,
177
+ retryable: false,
178
+ };
179
+ }
180
+ if (/not logged in|signed out|login required|authenticate|authentication|unauthorized|invalid.*(?:token|credential)|\b401\b/i.test(detail)) {
181
+ return {
182
+ type: 'error',
183
+ message: `Claude CLI is signed out or its authentication was rejected: ${detail}. Run \`moxxy login claude-code\` to sign in again.`,
184
+ retryable: false,
185
+ };
186
+ }
187
+ if (/permission|denied|not allowed|forbidden|\b403\b/i.test(detail)) {
188
+ return {
189
+ type: 'error',
190
+ message: `Claude Code permission denied: ${detail}. Review plugins.provider.items.claude-code.config.permissionMode and allowedTools.`,
191
+ retryable: false,
192
+ };
193
+ }
194
+ if (/rate.?limit|too many requests|\b429\b|overload|temporar|unavailable|service error|\b5\d\d\b/i.test(detail)) {
195
+ return { type: 'error', message: `Claude Code service failure: ${detail}`, retryable: true };
196
+ }
197
+ return { type: 'error', message: `Claude CLI failed: ${detail}`, retryable: false };
198
+ }
199
+ function classifySpawnFailure(error, executable) {
200
+ const nodeError = error;
201
+ if (nodeError?.code === 'ENOENT') {
202
+ return {
203
+ type: 'error',
204
+ message: `Claude CLI executable not found: ${executable}. Install it with \`npm install -g @anthropic-ai/claude-code\` or configure plugins.provider.items.claude-code.config.executable.`,
205
+ retryable: false,
206
+ };
207
+ }
208
+ return nonRetryableError(error);
209
+ }
210
+ function nonRetryableError(error) {
211
+ return {
212
+ type: 'error',
213
+ message: error instanceof Error ? error.message : String(error),
214
+ retryable: false,
215
+ };
21
216
  }
22
217
  //# sourceMappingURL=provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAahG,MAAM,UAAU,sBAAsB,CAAC,SAAmC,EAAE;IAC1E,OAAO,IAAI,iBAAiB,CAAC;QAC3B,IAAI,EAAE,uBAAuB;QAC7B,SAAS,EAAE,CAAC,GAAG,iBAAiB,CAAC;QACjC,cAAc,E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,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,sBAAsB,MAAM,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACtF,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAc,EAAE,UAAkB;IAC9D,MAAM,SAAS,GAAG,KAA8B,CAAC;IACjD,IAAI,SAAS,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO;YACL,IAAI,EAAE,OAAO;YACb,OAAO,EAAE,oCAAoC,UAAU,mIAAmI;YAC1L,SAAS,EAAE,KAAK;SACjB,CAAC;IACJ,CAAC;IACD,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,iBAAiB,CAAC,KAAc;IACvC,OAAO;QACL,IAAI,EAAE,OAAO;QACb,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;QAC/D,SAAS,EAAE,KAAK;KACjB,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moxxy/plugin-provider-claude-code",
3
- "version": "0.29.0",
4
- "description": "Claude subscription (Pro/Max) LLMProvider plugin for moxxy. Talks to the standard Anthropic Messages API using a Claude Code OAuth token from `claude setup-token` or an in-app sign-in.",
3
+ "version": "0.31.0",
4
+ "description": "Claude Pro/Max subscription provider for moxxy. Streams through the user's authenticated installed Claude CLI, with opt-in native-tool tasks and no Anthropic API key.",
5
5
  "keywords": [
6
6
  "moxxy",
7
7
  "agent",
@@ -42,12 +42,26 @@
42
42
  "plugin": {
43
43
  "entry": "./dist/index.js",
44
44
  "kind": "provider"
45
+ },
46
+ "catalog": {
47
+ "label": "Claude (Pro/Max sign-in)",
48
+ "description": "Claude models through an existing Claude Code subscription sign-in.",
49
+ "provides": [
50
+ {
51
+ "category": "provider",
52
+ "name": "claude-code"
53
+ }
54
+ ],
55
+ "provider": {
56
+ "auth": "oauth",
57
+ "defaultModel": "claude-sonnet-4-6"
58
+ }
45
59
  }
46
60
  },
47
61
  "dependencies": {
48
- "@moxxy/sdk": "0.29.0",
49
- "@moxxy/plugin-provider-anthropic": "0.29.0",
50
- "@moxxy/plugin-oauth": "0.29.0"
62
+ "@moxxy/sdk": "0.31.0",
63
+ "@moxxy/plugin-oauth": "0.31.0",
64
+ "@moxxy/plugin-provider-anthropic": "0.31.0"
51
65
  },
52
66
  "devDependencies": {
53
67
  "@types/node": "^22.10.0",
package/src/constants.ts CHANGED
@@ -1,46 +1,16 @@
1
- /**
2
- * Constants for the Claude subscription (Pro/Max) provider.
3
- *
4
- * These are the public Claude Code OAuth client parameters — the same values
5
- * the `claude` CLI uses — so a token minted here interoperates with one from
6
- * `claude setup-token`, and vice-versa. We never embed a client secret; the
7
- * flow is PKCE + an out-of-band (manual paste) authorization code.
8
- */
9
-
10
- /** Provider name in the registry / `/model` picker / `provider.name` config. */
1
+ /** Provider name in the registry / model picker / provider config. */
11
2
  export const CLAUDE_CODE_PROVIDER_ID = 'claude-code';
12
3
 
13
- /** Human-readable upstream service, shown in `moxxy login` and the init wizard. */
14
- export const CLAUDE_CODE_SERVICE_NAME = 'Claude (Pro/Max subscription)';
4
+ /** Human-readable upstream service, shown in login and setup surfaces. */
5
+ export const CLAUDE_CODE_SERVICE_NAME = 'Claude Code subscription';
15
6
 
16
- export const CLAUDE_AUTHORIZE_URL = 'https://claude.ai/oauth/authorize';
17
- export const CLAUDE_TOKEN_URL = 'https://console.anthropic.com/v1/oauth/token';
18
- export const CLAUDE_CLIENT_ID = '9d1c250a-e61b-44d9-88ed-5944d1962f5e';
19
- /**
20
- * Out-of-band redirect: after consent, Anthropic shows the user a
21
- * `code#state` string to paste back into the terminal (no loopback server).
22
- */
23
- export const CLAUDE_REDIRECT_URI = 'https://console.anthropic.com/oauth/code/callback';
24
- export const CLAUDE_SCOPES = ['org:create_api_key', 'user:profile', 'user:inference'] as const;
7
+ /** Optional executable override for login/status surfaces that do not carry provider config. */
8
+ export const CLAUDE_CODE_EXECUTABLE_ENV = 'CLAUDE_CODE_EXECUTABLE';
25
9
 
26
- /**
27
- * `anthropic-beta` values sent with every request. `oauth-2025-04-20` is the
28
- * flag that makes the Messages API accept a subscription bearer token at all;
29
- * `claude-code-20250219` matches what the real CLI sends. Sent comma-joined.
30
- */
31
- export const CLAUDE_OAUTH_BETA = ['oauth-2025-04-20', 'claude-code-20250219'] as const;
10
+ /** Supported Claude Code authentication commands. Arguments stay separate from the executable. */
11
+ export const CLAUDE_AUTH_STATUS_ARGS = ['auth', 'status'] as const;
12
+ export const CLAUDE_AUTH_LOGIN_ARGS = ['auth', 'login'] as const;
13
+ export const CLAUDE_AUTH_LOGOUT_ARGS = ['auth', 'logout'] as const;
32
14
 
33
- /**
34
- * Required first system block. Claude rejects a subscription token unless the
35
- * system prompt leads with this exact identity line; moxxy's real system
36
- * prompt is appended as the following block. (Verified against the working
37
- * opencode / claude-code-router behaviour.)
38
- */
15
+ /** Required identity line used by the CLI transport. */
39
16
  export const CLAUDE_CODE_SYSTEM = "You are Claude Code, Anthropic's official CLI for Claude.";
40
-
41
- /**
42
- * Env vars a pasted/CI token can arrive through. `CLAUDE_CODE_OAUTH_TOKEN` is
43
- * what `claude setup-token` documents; `ANTHROPIC_AUTH_TOKEN` is the
44
- * SDK-native bearer var. Checked in this order.
45
- */
46
- export const CLAUDE_TOKEN_ENV_VARS = ['CLAUDE_CODE_OAUTH_TOKEN', 'ANTHROPIC_AUTH_TOKEN'] as const;
package/src/index.ts CHANGED
@@ -1,17 +1,44 @@
1
- import { defineProvider, definePlugin } from '@moxxy/sdk';
2
- import { anthropicModels } from '@moxxy/plugin-provider-anthropic';
1
+ import { defineProvider, definePlugin, MoxxyError } from '@moxxy/sdk';
3
2
  import { CLAUDE_CODE_PROVIDER_ID, CLAUDE_CODE_SERVICE_NAME } from './constants.js';
4
- import { createClaudeCodeClient, type ClaudeCodeProviderConfig } from './provider.js';
5
- import { claudeLogin, claudeLogout, claudeStatus } from './login.js';
3
+ import {
4
+ CLAUDE_CODE_DEFAULT_MODEL,
5
+ claudeCodeModels,
6
+ createClaudeCodeClient,
7
+ type ClaudeCodeProviderConfig,
8
+ } from './provider.js';
9
+ import {
10
+ claudeLogin,
11
+ claudeLogout,
12
+ claudeStatus,
13
+ checkClaudeCliAuth,
14
+ resolveClaudeExecutable,
15
+ } from './login.js';
6
16
 
7
17
  export const claudeCodeProviderDef = defineProvider({
8
18
  name: CLAUDE_CODE_PROVIDER_ID,
9
- // Same Claude models as the API-key `anthropic` provider — it's the same
10
- // Messages API, only the credential differs.
11
- models: [...anthropicModels],
12
- createClient: (config) => createClaudeCodeClient(config as ClaudeCodeProviderConfig),
13
- // No validateKey: an OAuth bearer is validated by the request itself, and
14
- // an interactive paste/sign-in already proves the token round-trips.
19
+ models: claudeCodeModels,
20
+ createClient: (config) => createClaudeCodeClient({
21
+ ...(config as ClaudeCodeProviderConfig),
22
+ defaultModel:
23
+ typeof (config as { model?: unknown }).model === 'string'
24
+ ? (config as { model: string }).model
25
+ : (config as ClaudeCodeProviderConfig).defaultModel ?? CLAUDE_CODE_DEFAULT_MODEL,
26
+ }),
27
+ async resolveCredentials({ providerConfig, host }) {
28
+ const config = { ...providerConfig, cwd: host.cwd };
29
+ const executable = resolveClaudeExecutable(config);
30
+ const status = await checkClaudeCliAuth(executable);
31
+ if (status.state !== 'signed-in') {
32
+ throw new MoxxyError({
33
+ code: status.state === 'signed-out' ? 'AUTH_NO_CREDENTIALS' : 'AUTH_INVALID',
34
+ message: status.message,
35
+ hint: status.state === 'signed-out' ? `Run \`moxxy login ${CLAUDE_CODE_PROVIDER_ID}\`.` : undefined,
36
+ context: { provider: CLAUDE_CODE_PROVIDER_ID, executable, state: status.state },
37
+ });
38
+ }
39
+ return { ...config, executable };
40
+ },
41
+ // No validateKey: readiness is reported by the installed Claude CLI.
15
42
  auth: {
16
43
  kind: 'oauth',
17
44
  serviceName: CLAUDE_CODE_SERVICE_NAME,
@@ -33,15 +60,22 @@ export {
33
60
  CLAUDE_CODE_PROVIDER_ID,
34
61
  CLAUDE_CODE_SERVICE_NAME,
35
62
  CLAUDE_CODE_SYSTEM,
36
- CLAUDE_OAUTH_BETA,
37
- CLAUDE_TOKEN_ENV_VARS,
63
+ CLAUDE_CODE_EXECUTABLE_ENV,
38
64
  } from './constants.js';
39
65
  export {
40
66
  claudeLogin,
41
67
  claudeLogout,
42
68
  claudeStatus,
43
- ensureFreshClaudeTokens,
44
- refreshClaudeAccessToken,
45
- type FreshClaudeTokens,
69
+ checkClaudeCliAuth,
70
+ resolveClaudeExecutable,
71
+ type ClaudeCliAuthStatus,
72
+ type ClaudeCliAuthState,
73
+ type ClaudeCommandResult,
74
+ __setClaudeCommandRunner,
46
75
  } from './login.js';
47
- export { createClaudeCodeClient, type ClaudeCodeProviderConfig } from './provider.js';
76
+ export {
77
+ CLAUDE_CODE_DEFAULT_MODEL,
78
+ claudeCodeModels,
79
+ createClaudeCodeClient,
80
+ type ClaudeCodeProviderConfig,
81
+ } from './provider.js';