@moxxy/plugin-provider-claude-code 0.43.0 → 0.45.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.
Files changed (117) hide show
  1. package/dist/account-limit.d.ts +8 -0
  2. package/dist/account-limit.d.ts.map +1 -0
  3. package/dist/account-limit.js +23 -0
  4. package/dist/account-limit.js.map +1 -0
  5. package/dist/agent-provider.d.ts +61 -0
  6. package/dist/agent-provider.d.ts.map +1 -0
  7. package/dist/agent-provider.js +195 -0
  8. package/dist/agent-provider.js.map +1 -0
  9. package/dist/agent-session.d.ts +145 -0
  10. package/dist/agent-session.d.ts.map +1 -0
  11. package/dist/agent-session.js +700 -0
  12. package/dist/agent-session.js.map +1 -0
  13. package/dist/background-work.d.ts +42 -0
  14. package/dist/background-work.d.ts.map +1 -0
  15. package/dist/background-work.js +140 -0
  16. package/dist/background-work.js.map +1 -0
  17. package/dist/constants.d.ts +2 -2
  18. package/dist/constants.d.ts.map +1 -1
  19. package/dist/constants.js +2 -2
  20. package/dist/constants.js.map +1 -1
  21. package/dist/executable.d.ts +15 -0
  22. package/dist/executable.d.ts.map +1 -0
  23. package/dist/executable.js +30 -0
  24. package/dist/executable.js.map +1 -0
  25. package/dist/index.d.ts +4 -3
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +55 -16
  28. package/dist/index.js.map +1 -1
  29. package/dist/login.d.ts +3 -0
  30. package/dist/login.d.ts.map +1 -1
  31. package/dist/login.js +8 -1
  32. package/dist/login.js.map +1 -1
  33. package/dist/offered-models.d.ts +13 -0
  34. package/dist/offered-models.d.ts.map +1 -0
  35. package/dist/offered-models.js +45 -0
  36. package/dist/offered-models.js.map +1 -0
  37. package/dist/own-settings.d.ts +12 -0
  38. package/dist/own-settings.d.ts.map +1 -0
  39. package/dist/own-settings.js +31 -0
  40. package/dist/own-settings.js.map +1 -0
  41. package/dist/questions.d.ts +6 -0
  42. package/dist/questions.d.ts.map +1 -0
  43. package/dist/questions.js +38 -0
  44. package/dist/questions.js.map +1 -0
  45. package/dist/scheduled-wakeups.d.ts +10 -0
  46. package/dist/scheduled-wakeups.d.ts.map +1 -0
  47. package/dist/scheduled-wakeups.js +30 -0
  48. package/dist/scheduled-wakeups.js.map +1 -0
  49. package/dist/sdk-install.d.ts +25 -0
  50. package/dist/sdk-install.d.ts.map +1 -0
  51. package/dist/sdk-install.js +94 -0
  52. package/dist/sdk-install.js.map +1 -0
  53. package/dist/sdk-types.d.ts +310 -0
  54. package/dist/sdk-types.d.ts.map +1 -0
  55. package/dist/sdk-types.js +8 -0
  56. package/dist/sdk-types.js.map +1 -0
  57. package/dist/subagents.d.ts +39 -0
  58. package/dist/subagents.d.ts.map +1 -0
  59. package/dist/subagents.js +114 -0
  60. package/dist/subagents.js.map +1 -0
  61. package/dist/task-list.d.ts +21 -0
  62. package/dist/task-list.d.ts.map +1 -0
  63. package/dist/task-list.js +68 -0
  64. package/dist/task-list.js.map +1 -0
  65. package/dist/translate.d.ts +57 -0
  66. package/dist/translate.d.ts.map +1 -0
  67. package/dist/translate.js +276 -0
  68. package/dist/translate.js.map +1 -0
  69. package/package.json +14 -9
  70. package/src/account-limit.ts +26 -0
  71. package/src/agent-cli.test.ts +83 -0
  72. package/src/agent-provider.test.ts +1156 -0
  73. package/src/agent-provider.ts +223 -0
  74. package/src/agent-session.ts +790 -0
  75. package/src/background-work.ts +163 -0
  76. package/src/constants.ts +2 -3
  77. package/src/event-coverage.test.ts +105 -0
  78. package/src/executable.test.ts +60 -0
  79. package/src/executable.ts +34 -0
  80. package/src/fake-claude.testkit.ts +376 -0
  81. package/src/fixtures/claude-agent-sdk-0.3.296.json +43 -0
  82. package/src/fixtures/claude-supported-models-0.3.296.json +218 -0
  83. package/src/index.test.ts +49 -0
  84. package/src/index.ts +61 -18
  85. package/src/login.ts +10 -1
  86. package/src/offered-models.test.ts +73 -0
  87. package/src/offered-models.ts +57 -0
  88. package/src/own-settings.test.ts +68 -0
  89. package/src/own-settings.ts +33 -0
  90. package/src/questions.test.ts +68 -0
  91. package/src/questions.ts +45 -0
  92. package/src/scheduled-wakeups.test.ts +41 -0
  93. package/src/scheduled-wakeups.ts +34 -0
  94. package/src/sdk-install.test.ts +124 -0
  95. package/src/sdk-install.ts +107 -0
  96. package/src/sdk-types.ts +277 -0
  97. package/src/subagents.ts +123 -0
  98. package/src/task-list.ts +65 -0
  99. package/src/translate.test.ts +439 -0
  100. package/src/translate.ts +280 -0
  101. package/dist/process.d.ts +0 -34
  102. package/dist/process.d.ts.map +0 -1
  103. package/dist/process.js +0 -197
  104. package/dist/process.js.map +0 -1
  105. package/dist/protocol.d.ts +0 -30
  106. package/dist/protocol.d.ts.map +0 -1
  107. package/dist/protocol.js +0 -368
  108. package/dist/protocol.js.map +0 -1
  109. package/dist/provider.d.ts +0 -53
  110. package/dist/provider.d.ts.map +0 -1
  111. package/dist/provider.js +0 -229
  112. package/dist/provider.js.map +0 -1
  113. package/src/process.test.ts +0 -208
  114. package/src/process.ts +0 -224
  115. package/src/protocol.ts +0 -390
  116. package/src/provider.test.ts +0 -671
  117. package/src/provider.ts +0 -267
package/src/provider.ts DELETED
@@ -1,267 +0,0 @@
1
- import type { LLMProvider, ModelDescriptor, ProviderEvent, ProviderRequest } from '@moxxy/sdk';
2
- import { estimateTextTokens } from '@moxxy/sdk';
3
- import { CLAUDE_CODE_PROVIDER_ID } from './constants.js';
4
- import { ClaudeProcessError, runClaudeProcess, type ClaudeSpawn } from './process.js';
5
- import { createProtocolState, parseClaudeRecord, serializeClaudePrompt } from './protocol.js';
6
-
7
- export const CLAUDE_CODE_DEFAULT_MODEL = 'claude-sonnet-5-5';
8
- const NON_TEXT_BLOCK_TOKENS = 256;
9
-
10
- /**
11
- * Models offered by Claude Code subscriptions through the installed CLI.
12
- *
13
- * This is deliberately independent of the Anthropic API catalog: the CLI has
14
- * its own availability policy, and this adapter only preserves streamed text.
15
- * moxxy tools ride a text protocol (see protocol.ts). Unsupported capabilities
16
- * are explicit so every host sees the same contract rather than relying on how
17
- * it interprets absent optional flags.
18
- */
19
- const textOnlyCapabilities = {
20
- supportsTools: true,
21
- supportsStreaming: true,
22
- supportsImages: false,
23
- supportsDocuments: false,
24
- supportsAudio: false,
25
- supportsReasoning: false,
26
- hostedTools: ['web_search'],
27
- } as const;
28
-
29
- export const claudeCodeModels: ReadonlyArray<ModelDescriptor> = [
30
- { id: 'claude-fable-5-1', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
31
- { id: 'claude-opus-5-5', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
32
- { id: CLAUDE_CODE_DEFAULT_MODEL, contextWindow: 1_000_000, maxOutputTokens: 128_000, ...textOnlyCapabilities },
33
- { id: 'claude-haiku-4-5', contextWindow: 200_000, maxOutputTokens: 64_000, ...textOnlyCapabilities },
34
- ];
35
-
36
- export interface ClaudeCodeProviderConfig {
37
- /** Installed Claude Code executable. Defaults to the command resolved from PATH. */
38
- readonly executable?: string;
39
- /** Persisted provider-item model (`plugins.provider.items.claude-code.model`). */
40
- readonly model?: string;
41
- /** Transport mode. Native tools are opt-in; omitted defaults to text-only. */
42
- readonly mode?: 'text' | 'native-tools';
43
- /** Enable Claude Code's native WebSearch in text mode. Defaults to true. */
44
- readonly webSearch?: boolean;
45
- /** Claude CLI permission mode. Omitted uses the CLI's safe default. */
46
- readonly permissionMode?: 'acceptEdits' | 'plan' | 'dontAsk' | 'bypassPermissions';
47
- /** Optional allow-list of Claude native tool names. */
48
- readonly allowedTools?: ReadonlyArray<string>;
49
- /** Runner workspace. Supplied by CLI activation rather than user configuration. */
50
- readonly cwd?: string;
51
- /** Optional default model override (kept for programmatic callers). */
52
- readonly defaultModel?: string;
53
- /** Process test seam; production callers should leave this unset. */
54
- readonly spawn?: ClaudeSpawn;
55
- /** Time allowed for the first stream record. Defaults to 30 seconds. */
56
- readonly startupTimeoutMs?: number;
57
- /** Maximum silence between stream records. Defaults to 120 seconds. */
58
- readonly idleTimeoutMs?: number;
59
- /** Maximum captured stderr characters. Defaults to 2048. */
60
- readonly maxStderrChars?: number;
61
- /** Maximum characters in one stdout stream-json record. Defaults to 64 KiB. */
62
- readonly maxRecordChars?: number;
63
- }
64
-
65
- export class ClaudeCodeProvider implements LLMProvider {
66
- readonly name = CLAUDE_CODE_PROVIDER_ID;
67
- readonly models: ReadonlyArray<ModelDescriptor>;
68
-
69
- private readonly executable: string;
70
- private readonly defaultModel: string;
71
- private readonly nativeTools: boolean;
72
- private readonly webSearch: boolean;
73
- private readonly permissionMode?: string;
74
- private readonly allowedTools: ReadonlyArray<string>;
75
- private readonly cwd: string;
76
- private readonly spawn?: ClaudeSpawn;
77
- private readonly startupTimeoutMs?: number;
78
- private readonly idleTimeoutMs?: number;
79
- private readonly maxStderrChars?: number;
80
- private readonly maxRecordChars?: number;
81
-
82
- constructor(config: ClaudeCodeProviderConfig = {}) {
83
- this.executable = config.executable ?? 'claude';
84
- this.defaultModel = config.defaultModel ?? CLAUDE_CODE_DEFAULT_MODEL;
85
- this.nativeTools = config.mode === 'native-tools';
86
- // Native-tools mode hands the task to Claude's own tools; moxxy tools are
87
- // only offered through the text transport.
88
- this.models = this.nativeTools
89
- ? claudeCodeModels.map((model) => ({ ...model, supportsTools: false }))
90
- : claudeCodeModels;
91
- this.webSearch = config.webSearch !== false;
92
- if (config.permissionMode) this.permissionMode = config.permissionMode;
93
- this.allowedTools = config.allowedTools ?? [];
94
- this.cwd = config.cwd ?? process.cwd();
95
- if (config.spawn) this.spawn = config.spawn;
96
- if (config.startupTimeoutMs) this.startupTimeoutMs = config.startupTimeoutMs;
97
- if (config.idleTimeoutMs) this.idleTimeoutMs = config.idleTimeoutMs;
98
- if (config.maxStderrChars) this.maxStderrChars = config.maxStderrChars;
99
- if (config.maxRecordChars) this.maxRecordChars = config.maxRecordChars;
100
- }
101
-
102
- async *stream(req: ProviderRequest): AsyncIterable<ProviderEvent> {
103
- const model = req.model || this.defaultModel;
104
- yield { type: 'message_start', model };
105
-
106
- // Native WebSearch replaces moxxy's web_search fallback when it is enabled.
107
- const tools = this.webSearch
108
- ? req.tools?.filter((tool) => tool.hosted?.type !== 'web_search')
109
- : req.tools;
110
- let prompt: string;
111
- try {
112
- assertSupportedModel(model);
113
- assertTextOnlyRequest(req, this.nativeTools);
114
- prompt = serializeClaudePrompt({ ...req, ...(tools ? { tools } : {}) });
115
- } catch (error) {
116
- yield nonRetryableError(error);
117
- return;
118
- }
119
-
120
- const state = createProtocolState({ toolCalls: (tools?.length ?? 0) > 0 });
121
- let terminal = false;
122
- let lines: ReturnType<typeof runClaudeProcess> | undefined;
123
- try {
124
- lines = runClaudeProcess({
125
- executable: this.executable,
126
- model,
127
- prompt,
128
- cwd: this.cwd,
129
- nativeTools: this.nativeTools,
130
- webSearch: this.webSearch,
131
- allowedTools: this.allowedTools,
132
- ...(this.permissionMode ? { permissionMode: this.permissionMode } : {}),
133
- ...(req.signal ? { signal: req.signal } : {}),
134
- ...(this.spawn ? { spawn: this.spawn } : {}),
135
- ...(this.startupTimeoutMs ? { startupTimeoutMs: this.startupTimeoutMs } : {}),
136
- ...(this.idleTimeoutMs ? { idleTimeoutMs: this.idleTimeoutMs } : {}),
137
- ...(this.maxStderrChars ? { maxStderrChars: this.maxStderrChars } : {}),
138
- ...(this.maxRecordChars ? { maxRecordChars: this.maxRecordChars } : {}),
139
- });
140
- while (true) {
141
- const next = await lines.next();
142
- if (next.done) {
143
- if (next.value.exitCode !== 0 && !terminal) {
144
- yield classifyClaudeFailure(
145
- model,
146
- next.value.stderr || `Claude CLI exited unexpectedly with code ${next.value.exitCode}`,
147
- );
148
- return;
149
- }
150
- break;
151
- }
152
- for (const event of parseClaudeRecord(next.value, state)) {
153
- if (event.type === 'message_end') {
154
- terminal = true;
155
- yield event;
156
- return;
157
- }
158
- if (event.type === 'error') {
159
- terminal = true;
160
- yield classifyClaudeFailure(model, event.message);
161
- return;
162
- }
163
- yield event;
164
- }
165
- }
166
- if (!terminal) {
167
- yield { type: 'error', message: 'Claude CLI ended without a terminal result record', retryable: false };
168
- }
169
- } catch (error) {
170
- if (error instanceof ClaudeProcessError) {
171
- yield {
172
- type: 'error',
173
- message: error.message,
174
- retryable: error.failure === 'startup_timeout' || error.failure === 'idle_timeout',
175
- };
176
- } else {
177
- yield classifySpawnFailure(error, this.executable);
178
- }
179
- } finally {
180
- await lines?.return({ exitCode: 0, stderr: '' });
181
- }
182
- }
183
-
184
- async countTokens(
185
- req: Pick<ProviderRequest, 'model' | 'messages' | 'system' | 'tools'>,
186
- ): Promise<number> {
187
- let text = req.system ?? '';
188
- let nonTextBlocks = 0;
189
- for (const message of req.messages) {
190
- for (const block of message.content) {
191
- if (block.type === 'text') text += block.text;
192
- else nonTextBlocks += 1;
193
- }
194
- }
195
- for (const tool of req.tools ?? []) text += tool.name + tool.description;
196
- return estimateTextTokens(text) + nonTextBlocks * NON_TEXT_BLOCK_TOKENS;
197
- }
198
- }
199
-
200
- export function createClaudeCodeClient(config: ClaudeCodeProviderConfig = {}): LLMProvider {
201
- return new ClaudeCodeProvider(config);
202
- }
203
-
204
- function assertSupportedModel(model: string): void {
205
- if (claudeCodeModels.some((candidate) => candidate.id === model)) return;
206
- throw new Error(
207
- `Claude Code model "${model}" is not supported by this adapter. ` +
208
- `Select a supported model: ${claudeCodeModels.map((candidate) => candidate.id).join(', ')}.`,
209
- );
210
- }
211
-
212
- function assertTextOnlyRequest(req: ProviderRequest, nativeTools: boolean): void {
213
- if (nativeTools && req.tools && req.tools.length > 0) {
214
- throw new Error('Claude CLI native-tools transport does not support moxxy tools');
215
- }
216
- if (req.reasoning) {
217
- throw new Error('Claude CLI text transport does not support reasoning');
218
- }
219
- }
220
-
221
- function classifyClaudeFailure(model: string, detail: string): ProviderEvent {
222
- if (/model|unknown model|invalid model/i.test(detail)) {
223
- return {
224
- type: 'error',
225
- message: `Claude Code rejected model "${model}": ${detail}. Select a supported model: ${claudeCodeModels.map((candidate) => candidate.id).join(', ')}.`,
226
- retryable: false,
227
- };
228
- }
229
- if (/not logged in|signed out|login required|authenticate|authentication|unauthorized|invalid.*(?:token|credential)|\b401\b/i.test(detail)) {
230
- return {
231
- type: 'error',
232
- message: `Claude CLI is signed out or its authentication was rejected: ${detail}. Run \`moxxy login claude-code\` to sign in again.`,
233
- retryable: false,
234
- };
235
- }
236
- if (/permission|denied|not allowed|forbidden|\b403\b/i.test(detail)) {
237
- return {
238
- type: 'error',
239
- message: `Claude Code permission denied: ${detail}. Review plugins.provider.items.claude-code.config.permissionMode and allowedTools.`,
240
- retryable: false,
241
- };
242
- }
243
- if (/rate.?limit|too many requests|\b429\b|overload|temporar|unavailable|service error|\b5\d\d\b/i.test(detail)) {
244
- return { type: 'error', message: `Claude Code service failure: ${detail}`, retryable: true };
245
- }
246
- return { type: 'error', message: `Claude CLI failed: ${detail}`, retryable: false };
247
- }
248
-
249
- function classifySpawnFailure(error: unknown, executable: string): ProviderEvent {
250
- const nodeError = error as NodeJS.ErrnoException;
251
- if (nodeError?.code === 'ENOENT') {
252
- return {
253
- type: 'error',
254
- 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.`,
255
- retryable: false,
256
- };
257
- }
258
- return nonRetryableError(error);
259
- }
260
-
261
- function nonRetryableError(error: unknown): ProviderEvent {
262
- return {
263
- type: 'error',
264
- message: error instanceof Error ? error.message : String(error),
265
- retryable: false,
266
- };
267
- }