@nyte-ai/plugin 0.0.21 → 0.0.24
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/THIRD-PARTY-NOTICES.md +2 -38
- package/dist/chunks/{billing-2v3g7rd9.js → bash-description-2v3g7rd9.js} +1 -1
- package/dist/chunks/{billing-3j77ss7y.js → bash-description-3j77ss7y.js} +1 -1
- package/dist/chunks/{billing-bv5zr32b.js → bash-description-dnvgk30y.js} +2 -2
- package/dist/chunks/{billing-hg64tqf1.js → bash-description-hg64tqf1.js} +1 -1
- package/dist/chunks/{billing-wqjabjnc.js → bash-description-necnb2k7.js} +18 -23
- package/dist/chunks/{billing-mvc33pv6.js → bash-description-qnwggxq3.js} +1 -1
- package/dist/chunks/{billing-svrhh5a5.js → bash-description-svrhh5a5.js} +1 -1
- package/dist/chunks/{billing-wbe8w0te.js → bash-description-wbe8w0te.js} +1 -1
- package/dist/chunks/{billing-wmcbg6fw.js → bash-description-wmcbg6fw.js} +1 -1
- package/dist/chunks/{billing-sxc70w72.js → bash-description-yyd9jznf.js} +1 -1
- package/dist/chunks/{billing-kn3d30b4.js → bash-description-zz15raxk.js} +3 -3
- package/dist/src/acp/connection.js +3 -3
- package/dist/src/acp/index.js +6 -6
- package/dist/src/acp/registry.js +2 -2
- package/dist/src/acp/thread.d.ts +1 -1
- package/dist/src/acp/thread.js +2 -2
- package/dist/src/acp/transcript.d.ts +1 -1
- package/dist/src/acp/transcript.js +2 -2
- package/dist/src/acp/translate.d.ts +1 -1
- package/dist/src/acp/translate.js +1 -1
- package/dist/src/approvals/approvals.js +1 -1
- package/dist/src/approvals/schema.js +1 -1
- package/dist/src/canvas/canvas-compile.js +3 -3
- package/dist/src/canvas/canvas-libraries.js +1 -1
- package/dist/src/canvas/canvas-preview.js +3 -3
- package/dist/src/canvas/canvas-protocol.d.ts +1 -1
- package/dist/src/canvas/canvas-protocol.js +2 -2
- package/dist/src/canvas/canvas.js +5 -5
- package/dist/src/canvas/schema.js +3 -3
- package/dist/src/claude-code-sdk/index.js +6 -6
- package/dist/src/claude-code-sdk/session.js +2 -2
- package/dist/src/claude-code-sdk/transcript.d.ts +3 -1
- package/dist/src/claude-code-sdk/transcript.js +2 -2
- package/dist/src/claude-code-sdk/translate.js +1 -1
- package/dist/src/find-on-path.js +1 -1
- package/dist/src/question/client/questions-tui.js +1 -1
- package/dist/src/question/client/questions.js +1 -1
- package/dist/src/question/question.js +2 -2
- package/dist/src/question/schema.js +1 -1
- package/package.json +19 -23
- package/dist/chunks/billing-0y7dfkp2.js +0 -146
- package/dist/chunks/billing-3scqmkzb.js +0 -559
- package/dist/chunks/billing-8rjk35gr.js +0 -444
- package/dist/chunks/billing-9qc0eqh4.js +0 -174
- package/dist/chunks/billing-a9vqn66p.js +0 -351
- package/dist/chunks/billing-cy1rg619.js +0 -302
- package/dist/chunks/billing-ejx2hv52.js +0 -323
- package/dist/chunks/billing-j643d12p.js +0 -197
- package/dist/chunks/billing-s4rrspsk.js +0 -7
- package/dist/chunks/billing-t1qrxe16.js +0 -41
- package/dist/chunks/billing-tzr3cebf.js +0 -20
- package/dist/examples/claude-code/LICENSE +0 -21
- package/dist/examples/claude-code/billing.d.ts +0 -7
- package/dist/examples/claude-code/billing.js +0 -11
- package/dist/examples/claude-code/claude-cli.d.ts +0 -35
- package/dist/examples/claude-code/claude-cli.js +0 -15
- package/dist/examples/claude-code/env.d.ts +0 -1
- package/dist/examples/claude-code/env.js +0 -6
- package/dist/examples/claude-code/index.d.ts +0 -24
- package/dist/examples/claude-code/index.js +0 -149
- package/dist/examples/claude-code/inert-mcp.d.ts +0 -1
- package/dist/examples/claude-code/inert-mcp.js +0 -6
- package/dist/examples/claude-code/model-catalog.d.ts +0 -27
- package/dist/examples/claude-code/model-catalog.js +0 -12
- package/dist/examples/claude-code/process.d.ts +0 -22
- package/dist/examples/claude-code/process.js +0 -25
- package/dist/examples/claude-code/replay.d.ts +0 -4
- package/dist/examples/claude-code/replay.js +0 -11
- package/dist/examples/claude-code/stream.d.ts +0 -3
- package/dist/examples/claude-code/stream.js +0 -16
- package/dist/examples/claude-code/transcript.d.ts +0 -83
- package/dist/examples/claude-code/transcript.js +0 -23
- package/dist/examples/claude-code/turn-relay.d.ts +0 -48
- package/dist/examples/claude-code/turn-relay.js +0 -9
- package/dist/examples/claude-code/wire.d.ts +0 -372
- package/dist/examples/claude-code/wire.js +0 -18
- /package/dist/chunks/{billing-4vz9pxyx.js → bash-description-4vz9pxyx.js} +0 -0
- /package/dist/chunks/{billing-6z2kgtyt.js → bash-description-6z2kgtyt.js} +0 -0
- /package/dist/chunks/{billing-9nn3yp18.js → bash-description-9nn3yp18.js} +0 -0
- /package/dist/chunks/{billing-h13r0byk.js → bash-description-h13r0byk.js} +0 -0
- /package/dist/chunks/{billing-ye3wk5r3.js → bash-description-ye3wk5r3.js} +0 -0
- /package/dist/chunks/{billing-zfpm2rt0.js → bash-description-zfpm2rt0.js} +0 -0
|
@@ -1,444 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseRecord2
|
|
3
|
-
} from "./billing-0y7dfkp2.js";
|
|
4
|
-
import {
|
|
5
|
-
describeUsageCreditsRejection2
|
|
6
|
-
} from "./billing-tzr3cebf.js";
|
|
7
|
-
import {
|
|
8
|
-
TOOL_PREFIX2,
|
|
9
|
-
NATIVE_TOOL_CHANGES_BETA2,
|
|
10
|
-
getInitialUserPrefix2,
|
|
11
|
-
getInitialSystemMessages2,
|
|
12
|
-
prepareFrames2,
|
|
13
|
-
prepareTools2,
|
|
14
|
-
prepareNativeTranscript2,
|
|
15
|
-
buildExtraBody2,
|
|
16
|
-
makeReplayDiagnostic2
|
|
17
|
-
} from "./billing-ejx2hv52.js";
|
|
18
|
-
import {
|
|
19
|
-
runClaudeRequest2
|
|
20
|
-
} from "./billing-3scqmkzb.js";
|
|
21
|
-
|
|
22
|
-
// examples/claude-code/stream.ts
|
|
23
|
-
import {
|
|
24
|
-
calculateCost,
|
|
25
|
-
collapseSystemMessages,
|
|
26
|
-
createAssistantMessageEventStream,
|
|
27
|
-
getCurrentSystemPrompt,
|
|
28
|
-
getCurrentTools,
|
|
29
|
-
getInitialSystemMessage,
|
|
30
|
-
getSystemMessageText
|
|
31
|
-
} from "@nyte-ai/ai";
|
|
32
|
-
function mapStopReason(reason) {
|
|
33
|
-
switch (reason) {
|
|
34
|
-
case "end_turn":
|
|
35
|
-
case "pause_turn":
|
|
36
|
-
case "stop_sequence":
|
|
37
|
-
return { stopReason: "stop" };
|
|
38
|
-
case "max_tokens":
|
|
39
|
-
case "model_context_window_exceeded":
|
|
40
|
-
return { stopReason: "length" };
|
|
41
|
-
case "tool_use":
|
|
42
|
-
return { stopReason: "toolUse" };
|
|
43
|
-
case "refusal":
|
|
44
|
-
return { stopReason: "error", errorMessage: "The model refused to complete the request" };
|
|
45
|
-
case "sensitive":
|
|
46
|
-
return { stopReason: "error", errorMessage: "Provider stopped with: sensitive" };
|
|
47
|
-
default:
|
|
48
|
-
return {
|
|
49
|
-
stopReason: "error",
|
|
50
|
-
errorMessage: `Unhandled Claude Code stop reason: ${reason ?? "missing"}`
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function parseArguments(raw, fallback) {
|
|
55
|
-
if (!raw.trim())
|
|
56
|
-
return fallback;
|
|
57
|
-
try {
|
|
58
|
-
return parseRecord2(raw);
|
|
59
|
-
} catch {
|
|
60
|
-
return fallback;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
function updateUsage(output, model, usage) {
|
|
64
|
-
if (!usage)
|
|
65
|
-
return;
|
|
66
|
-
if (typeof usage.input_tokens === "number")
|
|
67
|
-
output.usage.input = usage.input_tokens;
|
|
68
|
-
if (typeof usage.output_tokens === "number")
|
|
69
|
-
output.usage.output = usage.output_tokens;
|
|
70
|
-
if (typeof usage.cache_read_input_tokens === "number")
|
|
71
|
-
output.usage.cacheRead = usage.cache_read_input_tokens;
|
|
72
|
-
if (typeof usage.cache_creation_input_tokens === "number")
|
|
73
|
-
output.usage.cacheWrite = usage.cache_creation_input_tokens;
|
|
74
|
-
const oneHour = usage.cache_creation?.ephemeral_1h_input_tokens;
|
|
75
|
-
if (typeof oneHour === "number")
|
|
76
|
-
output.usage.cacheWrite1h = oneHour;
|
|
77
|
-
const thinking = usage.output_tokens_details?.thinking_tokens;
|
|
78
|
-
if (typeof thinking === "number")
|
|
79
|
-
output.usage.reasoning = thinking;
|
|
80
|
-
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
81
|
-
calculateCost(model, output.usage);
|
|
82
|
-
}
|
|
83
|
-
function comparableContent(content) {
|
|
84
|
-
return content.map((block) => {
|
|
85
|
-
if (block.type === "text")
|
|
86
|
-
return { type: "text", text: block.text };
|
|
87
|
-
if (block.type === "thinking") {
|
|
88
|
-
return {
|
|
89
|
-
type: "thinking",
|
|
90
|
-
thinking: block.thinking,
|
|
91
|
-
thinkingSignature: block.thinkingSignature,
|
|
92
|
-
redacted: block.redacted
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
return { type: "toolCall", id: block.id, name: block.name, arguments: block.arguments };
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
function nativeContent(content, toolNames) {
|
|
99
|
-
const converted = [];
|
|
100
|
-
for (const block of content) {
|
|
101
|
-
if (block.type === "text") {
|
|
102
|
-
converted.push({ type: "text", text: block.text });
|
|
103
|
-
} else if (block.type === "thinking") {
|
|
104
|
-
converted.push({
|
|
105
|
-
type: "thinking",
|
|
106
|
-
thinking: block.thinking,
|
|
107
|
-
thinkingSignature: block.signature
|
|
108
|
-
});
|
|
109
|
-
} else if (block.type === "redacted_thinking") {
|
|
110
|
-
converted.push({
|
|
111
|
-
type: "thinking",
|
|
112
|
-
thinking: "[Reasoning redacted]",
|
|
113
|
-
thinkingSignature: block.data,
|
|
114
|
-
redacted: true
|
|
115
|
-
});
|
|
116
|
-
} else if (block.type === "tool_use") {
|
|
117
|
-
if (!block.name.startsWith(TOOL_PREFIX2))
|
|
118
|
-
throw new Error(`Claude Code returned unexpected tool ${block.name}`);
|
|
119
|
-
const name = block.name.slice(TOOL_PREFIX2.length);
|
|
120
|
-
if (!toolNames.has(name))
|
|
121
|
-
throw new Error(`Claude Code returned unavailable tool ${name}`);
|
|
122
|
-
converted.push({ type: "toolCall", id: block.id, name, arguments: block.input });
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return converted;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
class EventTranslator {
|
|
129
|
-
nativeToContent = new Map;
|
|
130
|
-
argumentsByIndex = new Map;
|
|
131
|
-
sawContentEvent = false;
|
|
132
|
-
model;
|
|
133
|
-
output;
|
|
134
|
-
stream;
|
|
135
|
-
toolNames;
|
|
136
|
-
constructor(model, output, stream, toolNames) {
|
|
137
|
-
this.model = model;
|
|
138
|
-
this.output = output;
|
|
139
|
-
this.stream = stream;
|
|
140
|
-
this.toolNames = toolNames;
|
|
141
|
-
}
|
|
142
|
-
event(event) {
|
|
143
|
-
switch (event.type) {
|
|
144
|
-
case "message_start":
|
|
145
|
-
if (event.message.id)
|
|
146
|
-
this.output.responseId = event.message.id;
|
|
147
|
-
updateUsage(this.output, this.model, event.message.usage);
|
|
148
|
-
break;
|
|
149
|
-
case "content_block_start":
|
|
150
|
-
this.startBlock(event.index, event.content_block);
|
|
151
|
-
break;
|
|
152
|
-
case "content_block_delta":
|
|
153
|
-
this.delta(event.index, event.delta);
|
|
154
|
-
break;
|
|
155
|
-
case "content_block_stop":
|
|
156
|
-
this.stopBlock(event.index);
|
|
157
|
-
break;
|
|
158
|
-
case "message_delta": {
|
|
159
|
-
if (event.delta.stop_reason) {
|
|
160
|
-
this.output.rawStopReason = event.delta.stop_reason;
|
|
161
|
-
const mapped = mapStopReason(event.delta.stop_reason);
|
|
162
|
-
this.output.stopReason = mapped.stopReason;
|
|
163
|
-
this.output.errorMessage = mapped.errorMessage;
|
|
164
|
-
}
|
|
165
|
-
updateUsage(this.output, this.model, event.usage);
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
finish(message, usage, initialUserPrefix, initialSystemMessages) {
|
|
171
|
-
const authoritative = nativeContent(message.content, this.toolNames);
|
|
172
|
-
if (!this.sawContentEvent) {
|
|
173
|
-
this.emitComplete(authoritative);
|
|
174
|
-
} else if (JSON.stringify(comparableContent(this.output.content)) !== JSON.stringify(comparableContent(authoritative))) {
|
|
175
|
-
throw new Error("Claude Code final response differs from its incremental stream");
|
|
176
|
-
}
|
|
177
|
-
if (message.id)
|
|
178
|
-
this.output.responseId = message.id;
|
|
179
|
-
updateUsage(this.output, this.model, usage);
|
|
180
|
-
this.output.rawStopReason = message.stop_reason ?? undefined;
|
|
181
|
-
const mapped = mapStopReason(message.stop_reason);
|
|
182
|
-
this.output.stopReason = mapped.stopReason;
|
|
183
|
-
this.output.errorMessage = mapped.errorMessage;
|
|
184
|
-
this.output.diagnostics = [
|
|
185
|
-
...this.output.diagnostics ?? [],
|
|
186
|
-
makeReplayDiagnostic2(message, this.output, initialUserPrefix, initialSystemMessages)
|
|
187
|
-
];
|
|
188
|
-
}
|
|
189
|
-
startBlock(nativeIndex, native) {
|
|
190
|
-
this.sawContentEvent = true;
|
|
191
|
-
const contentIndex = this.output.content.length;
|
|
192
|
-
this.nativeToContent.set(nativeIndex, contentIndex);
|
|
193
|
-
if (native.type === "text") {
|
|
194
|
-
this.output.content.push({ type: "text", text: native.text ?? "" });
|
|
195
|
-
this.stream.push({ type: "text_start", contentIndex, partial: this.output });
|
|
196
|
-
} else if (native.type === "thinking") {
|
|
197
|
-
this.output.content.push({
|
|
198
|
-
type: "thinking",
|
|
199
|
-
thinking: native.thinking ?? "",
|
|
200
|
-
thinkingSignature: native.signature ?? ""
|
|
201
|
-
});
|
|
202
|
-
this.stream.push({ type: "thinking_start", contentIndex, partial: this.output });
|
|
203
|
-
} else if (native.type === "redacted_thinking") {
|
|
204
|
-
this.output.content.push({
|
|
205
|
-
type: "thinking",
|
|
206
|
-
thinking: "[Reasoning redacted]",
|
|
207
|
-
thinkingSignature: native.data,
|
|
208
|
-
redacted: true
|
|
209
|
-
});
|
|
210
|
-
this.stream.push({ type: "thinking_start", contentIndex, partial: this.output });
|
|
211
|
-
} else if (native.type === "tool_use") {
|
|
212
|
-
if (!native.name.startsWith(TOOL_PREFIX2))
|
|
213
|
-
throw new Error(`Claude Code returned unexpected tool ${native.name}`);
|
|
214
|
-
const name = native.name.slice(TOOL_PREFIX2.length);
|
|
215
|
-
if (!this.toolNames.has(name))
|
|
216
|
-
throw new Error(`Claude Code returned unavailable tool ${name}`);
|
|
217
|
-
this.output.content.push({
|
|
218
|
-
type: "toolCall",
|
|
219
|
-
id: native.id,
|
|
220
|
-
name,
|
|
221
|
-
arguments: native.input ?? {}
|
|
222
|
-
});
|
|
223
|
-
this.stream.push({ type: "toolcall_start", contentIndex, partial: this.output });
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
delta(nativeIndex, delta) {
|
|
227
|
-
const contentIndex = this.nativeToContent.get(nativeIndex);
|
|
228
|
-
if (contentIndex === undefined)
|
|
229
|
-
return;
|
|
230
|
-
const block = this.output.content[contentIndex];
|
|
231
|
-
if (!block)
|
|
232
|
-
return;
|
|
233
|
-
if (delta.type === "text_delta" && block.type === "text") {
|
|
234
|
-
block.text += delta.text;
|
|
235
|
-
this.stream.push({
|
|
236
|
-
type: "text_delta",
|
|
237
|
-
contentIndex,
|
|
238
|
-
delta: delta.text,
|
|
239
|
-
partial: this.output
|
|
240
|
-
});
|
|
241
|
-
} else if (delta.type === "thinking_delta" && block.type === "thinking") {
|
|
242
|
-
block.thinking += delta.thinking;
|
|
243
|
-
this.stream.push({
|
|
244
|
-
type: "thinking_delta",
|
|
245
|
-
contentIndex,
|
|
246
|
-
delta: delta.thinking,
|
|
247
|
-
partial: this.output
|
|
248
|
-
});
|
|
249
|
-
} else if (delta.type === "signature_delta" && block.type === "thinking") {
|
|
250
|
-
block.thinkingSignature = (block.thinkingSignature ?? "") + delta.signature;
|
|
251
|
-
} else if (delta.type === "input_json_delta" && block.type === "toolCall") {
|
|
252
|
-
const json = (this.argumentsByIndex.get(nativeIndex) ?? "") + delta.partial_json;
|
|
253
|
-
this.argumentsByIndex.set(nativeIndex, json);
|
|
254
|
-
block.arguments = parseArguments(json, block.arguments);
|
|
255
|
-
this.stream.push({
|
|
256
|
-
type: "toolcall_delta",
|
|
257
|
-
contentIndex,
|
|
258
|
-
delta: delta.partial_json,
|
|
259
|
-
partial: this.output
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
stopBlock(nativeIndex) {
|
|
264
|
-
const contentIndex = this.nativeToContent.get(nativeIndex);
|
|
265
|
-
if (contentIndex === undefined)
|
|
266
|
-
return;
|
|
267
|
-
const block = this.output.content[contentIndex];
|
|
268
|
-
if (!block)
|
|
269
|
-
return;
|
|
270
|
-
if (block.type === "text") {
|
|
271
|
-
this.stream.push({
|
|
272
|
-
type: "text_end",
|
|
273
|
-
contentIndex,
|
|
274
|
-
content: block.text,
|
|
275
|
-
partial: this.output
|
|
276
|
-
});
|
|
277
|
-
} else if (block.type === "thinking") {
|
|
278
|
-
this.stream.push({
|
|
279
|
-
type: "thinking_end",
|
|
280
|
-
contentIndex,
|
|
281
|
-
content: block.thinking,
|
|
282
|
-
partial: this.output
|
|
283
|
-
});
|
|
284
|
-
} else {
|
|
285
|
-
block.arguments = parseArguments(this.argumentsByIndex.get(nativeIndex) ?? "", block.arguments);
|
|
286
|
-
this.argumentsByIndex.delete(nativeIndex);
|
|
287
|
-
this.stream.push({
|
|
288
|
-
type: "toolcall_end",
|
|
289
|
-
contentIndex,
|
|
290
|
-
toolCall: block,
|
|
291
|
-
partial: this.output
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
emitComplete(content) {
|
|
296
|
-
for (const source of content) {
|
|
297
|
-
const contentIndex = this.output.content.length;
|
|
298
|
-
if (source.type === "text") {
|
|
299
|
-
this.output.content.push({ ...source });
|
|
300
|
-
this.stream.push({ type: "text_start", contentIndex, partial: this.output });
|
|
301
|
-
if (source.text)
|
|
302
|
-
this.stream.push({
|
|
303
|
-
type: "text_delta",
|
|
304
|
-
contentIndex,
|
|
305
|
-
delta: source.text,
|
|
306
|
-
partial: this.output
|
|
307
|
-
});
|
|
308
|
-
this.stream.push({
|
|
309
|
-
type: "text_end",
|
|
310
|
-
contentIndex,
|
|
311
|
-
content: source.text,
|
|
312
|
-
partial: this.output
|
|
313
|
-
});
|
|
314
|
-
} else if (source.type === "thinking") {
|
|
315
|
-
this.output.content.push({ ...source });
|
|
316
|
-
this.stream.push({ type: "thinking_start", contentIndex, partial: this.output });
|
|
317
|
-
if (source.thinking && !source.redacted) {
|
|
318
|
-
this.stream.push({
|
|
319
|
-
type: "thinking_delta",
|
|
320
|
-
contentIndex,
|
|
321
|
-
delta: source.thinking,
|
|
322
|
-
partial: this.output
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
this.stream.push({
|
|
326
|
-
type: "thinking_end",
|
|
327
|
-
contentIndex,
|
|
328
|
-
content: source.thinking,
|
|
329
|
-
partial: this.output
|
|
330
|
-
});
|
|
331
|
-
} else {
|
|
332
|
-
this.output.content.push({ ...source });
|
|
333
|
-
this.stream.push({ type: "toolcall_start", contentIndex, partial: this.output });
|
|
334
|
-
const json = JSON.stringify(source.arguments);
|
|
335
|
-
if (json)
|
|
336
|
-
this.stream.push({
|
|
337
|
-
type: "toolcall_delta",
|
|
338
|
-
contentIndex,
|
|
339
|
-
delta: json,
|
|
340
|
-
partial: this.output
|
|
341
|
-
});
|
|
342
|
-
this.stream.push({
|
|
343
|
-
type: "toolcall_end",
|
|
344
|
-
contentIndex,
|
|
345
|
-
toolCall: source,
|
|
346
|
-
partial: this.output
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
function normalizeError(error) {
|
|
353
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
354
|
-
const usageCredits = describeUsageCreditsRejection2(message);
|
|
355
|
-
if (usageCredits)
|
|
356
|
-
return usageCredits;
|
|
357
|
-
if (/prompt is too long|context(?:_| )length|context window exceeded|too many input tokens/iu.test(message)) {
|
|
358
|
-
return `context_length_exceeded: ${message}`;
|
|
359
|
-
}
|
|
360
|
-
return message;
|
|
361
|
-
}
|
|
362
|
-
function streamClaudeCode2(model, context, options, catalog) {
|
|
363
|
-
const stream = createAssistantMessageEventStream();
|
|
364
|
-
const output = {
|
|
365
|
-
role: "assistant",
|
|
366
|
-
content: [],
|
|
367
|
-
api: model.api,
|
|
368
|
-
provider: model.provider,
|
|
369
|
-
model: model.id,
|
|
370
|
-
usage: {
|
|
371
|
-
input: 0,
|
|
372
|
-
output: 0,
|
|
373
|
-
cacheRead: 0,
|
|
374
|
-
cacheWrite: 0,
|
|
375
|
-
totalTokens: 0,
|
|
376
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
377
|
-
},
|
|
378
|
-
stopReason: "pending",
|
|
379
|
-
timestamp: Date.now()
|
|
380
|
-
};
|
|
381
|
-
(async () => {
|
|
382
|
-
try {
|
|
383
|
-
const nativeTranscript = prepareNativeTranscript2(context.messages, catalog.supportsNativeToolChanges(model.id));
|
|
384
|
-
const transcript = nativeTranscript ? context : collapseSystemMessages(context);
|
|
385
|
-
const initialSystem = getInitialSystemMessage(transcript.messages);
|
|
386
|
-
const systemPrompt = nativeTranscript ? initialSystem ? getSystemMessageText(initialSystem) : "" : getCurrentSystemPrompt(transcript.messages);
|
|
387
|
-
const tools = getCurrentTools(transcript.messages);
|
|
388
|
-
const frames = prepareFrames2(transcript.messages);
|
|
389
|
-
const initialUserPrefix = getInitialUserPrefix2(transcript.messages);
|
|
390
|
-
const initialSystemMessages = getInitialSystemMessages2(transcript.messages);
|
|
391
|
-
const preparedTools = prepareTools2(tools);
|
|
392
|
-
const extraBody = buildExtraBody2(nativeTranscript?.requestTools ?? preparedTools.nativeTools, options, catalog.supportsAdaptiveThinking(model.id));
|
|
393
|
-
const translator = new EventTranslator(model, output, stream, preparedTools.names);
|
|
394
|
-
stream.push({ type: "start", partial: output });
|
|
395
|
-
const response = await runClaudeRequest2({
|
|
396
|
-
model: catalog.nativeModel(model.id),
|
|
397
|
-
cwd: process.cwd(),
|
|
398
|
-
frames,
|
|
399
|
-
systemPrompt,
|
|
400
|
-
manifest: preparedTools.manifest,
|
|
401
|
-
extraBody,
|
|
402
|
-
toolNames: preparedTools.names,
|
|
403
|
-
signal: options?.signal,
|
|
404
|
-
timeoutMs: options?.timeoutMs,
|
|
405
|
-
env: options?.env,
|
|
406
|
-
initialUserPrefix,
|
|
407
|
-
initialSystemMessages,
|
|
408
|
-
nativeSystemInjections: nativeTranscript?.systemInjections,
|
|
409
|
-
betas: nativeTranscript ? [NATIVE_TOOL_CHANGES_BETA2] : undefined,
|
|
410
|
-
sessionId: options?.cacheRetention === "none" ? undefined : options?.sessionId,
|
|
411
|
-
onEvent: (event) => translator.event(event)
|
|
412
|
-
});
|
|
413
|
-
translator.finish(response.message, response.usage, response.initialUserPrefix, response.initialSystemMessages);
|
|
414
|
-
output.diagnostics = [
|
|
415
|
-
...output.diagnostics ?? [],
|
|
416
|
-
{
|
|
417
|
-
type: "claude-code.turn-relay",
|
|
418
|
-
timestamp: Date.now(),
|
|
419
|
-
details: response.relay
|
|
420
|
-
},
|
|
421
|
-
...response.rateLimit ? [
|
|
422
|
-
{
|
|
423
|
-
type: "claude-code.rate-limit",
|
|
424
|
-
timestamp: Date.now(),
|
|
425
|
-
details: response.rateLimit
|
|
426
|
-
}
|
|
427
|
-
] : []
|
|
428
|
-
];
|
|
429
|
-
if (output.stopReason === "error" || output.stopReason === "aborted" || output.stopReason === "pending") {
|
|
430
|
-
throw new Error(output.errorMessage || `Claude Code ended with ${output.stopReason}`);
|
|
431
|
-
}
|
|
432
|
-
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
433
|
-
stream.end();
|
|
434
|
-
} catch (error) {
|
|
435
|
-
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
436
|
-
output.errorMessage = normalizeError(error);
|
|
437
|
-
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
438
|
-
stream.end();
|
|
439
|
-
}
|
|
440
|
-
})();
|
|
441
|
-
return stream;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
export { streamClaudeCode2 };
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
// examples/claude-code/model-catalog.ts
|
|
2
|
-
import { Type } from "typebox";
|
|
3
|
-
var PROVIDER_ID2 = "anthropic";
|
|
4
|
-
var API_ID2 = "claude-code-stream-json";
|
|
5
|
-
var discoveredModelSchema2 = Type.Object({
|
|
6
|
-
value: Type.String(),
|
|
7
|
-
resolvedModel: Type.Optional(Type.String()),
|
|
8
|
-
displayName: Type.String(),
|
|
9
|
-
description: Type.String(),
|
|
10
|
-
supportsEffort: Type.Optional(Type.Boolean()),
|
|
11
|
-
supportedEffortLevels: Type.Optional(Type.Array(Type.Union([
|
|
12
|
-
Type.Literal("low"),
|
|
13
|
-
Type.Literal("medium"),
|
|
14
|
-
Type.Literal("high"),
|
|
15
|
-
Type.Literal("xhigh"),
|
|
16
|
-
Type.Literal("max")
|
|
17
|
-
]))),
|
|
18
|
-
supportsAdaptiveThinking: Type.Optional(Type.Boolean()),
|
|
19
|
-
supportsFastMode: Type.Optional(Type.Boolean()),
|
|
20
|
-
supportsAutoMode: Type.Optional(Type.Boolean())
|
|
21
|
-
});
|
|
22
|
-
function withoutContextSuffix(model) {
|
|
23
|
-
return model.replace(/\[1m\]$/u, "");
|
|
24
|
-
}
|
|
25
|
-
function fromBuiltin(id, builtin) {
|
|
26
|
-
const {
|
|
27
|
-
api: _api,
|
|
28
|
-
provider: _provider,
|
|
29
|
-
baseUrl: _baseUrl,
|
|
30
|
-
compat: _compat,
|
|
31
|
-
...metadata
|
|
32
|
-
} = builtin;
|
|
33
|
-
return {
|
|
34
|
-
...metadata,
|
|
35
|
-
id,
|
|
36
|
-
name: id === "default" ? `Default (${builtin.name})` : builtin.name,
|
|
37
|
-
api: API_ID2,
|
|
38
|
-
provider: PROVIDER_ID2,
|
|
39
|
-
baseUrl: "process://claude-code"
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function createClaudeModelCatalog2() {
|
|
43
|
-
const BUILTIN_ANTHROPIC_MODELS = new Map;
|
|
44
|
-
function restoreAnthropicModels(models) {
|
|
45
|
-
BUILTIN_ANTHROPIC_MODELS.clear();
|
|
46
|
-
for (const model of models)
|
|
47
|
-
BUILTIN_ANTHROPIC_MODELS.set(model.id, model);
|
|
48
|
-
}
|
|
49
|
-
const models = new Map;
|
|
50
|
-
const FALLBACK_DISCOVERY = [
|
|
51
|
-
{
|
|
52
|
-
value: "default",
|
|
53
|
-
resolvedModel: "claude-opus-5-5[1m]",
|
|
54
|
-
displayName: "Default (recommended)",
|
|
55
|
-
description: "Opus 5.5 with 1M context · Best for everyday, complex tasks",
|
|
56
|
-
supportsEffort: true,
|
|
57
|
-
supportedEffortLevels: ["low", "medium", "high", "xhigh", "max"],
|
|
58
|
-
supportsAdaptiveThinking: true,
|
|
59
|
-
supportsFastMode: true,
|
|
60
|
-
supportsAutoMode: true
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
value: "opus[1m]",
|
|
64
|
-
resolvedModel: "claude-opus-5-5[1m]",
|
|
65
|
-
displayName: "Opus (1M context)",
|
|
66
|
-
description: "Opus 5.5 with 1M context · Best for everyday, complex tasks",
|
|
67
|
-
supportsEffort: true,
|
|
68
|
-
supportedEffortLevels: ["low", "medium", "high", "xhigh", "max"],
|
|
69
|
-
supportsAdaptiveThinking: true,
|
|
70
|
-
supportsFastMode: true,
|
|
71
|
-
supportsAutoMode: true
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
value: "claude-fable-5-1[1m]",
|
|
75
|
-
resolvedModel: "claude-fable-5-1",
|
|
76
|
-
displayName: "Fable",
|
|
77
|
-
description: "Fable 5.1 · Most capable for the hardest and longest-running tasks",
|
|
78
|
-
supportsEffort: true,
|
|
79
|
-
supportedEffortLevels: ["low", "medium", "high", "xhigh", "max"],
|
|
80
|
-
supportsAdaptiveThinking: true,
|
|
81
|
-
supportsAutoMode: true
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
value: "sonnet",
|
|
85
|
-
resolvedModel: "claude-sonnet-5",
|
|
86
|
-
displayName: "Sonnet",
|
|
87
|
-
description: "Sonnet 5 · Efficient for routine tasks",
|
|
88
|
-
supportsEffort: true,
|
|
89
|
-
supportedEffortLevels: ["low", "medium", "high", "xhigh", "max"],
|
|
90
|
-
supportsAdaptiveThinking: true,
|
|
91
|
-
supportsAutoMode: true
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
value: "haiku",
|
|
95
|
-
resolvedModel: "claude-haiku-4-5-20251001",
|
|
96
|
-
displayName: "Haiku",
|
|
97
|
-
description: "Haiku 4.5 · Fastest for quick answers"
|
|
98
|
-
}
|
|
99
|
-
];
|
|
100
|
-
const ALIASES = {
|
|
101
|
-
sonnet: "claude-sonnet-5",
|
|
102
|
-
haiku: "claude-haiku-4-5-20251001",
|
|
103
|
-
"claude-haiku-4-5": "claude-haiku-4-5-20251001",
|
|
104
|
-
opus: "claude-opus-5-5",
|
|
105
|
-
fable: "claude-fable-5-1"
|
|
106
|
-
};
|
|
107
|
-
function canonicalModelId(info) {
|
|
108
|
-
if (info.value === "default")
|
|
109
|
-
return "default";
|
|
110
|
-
return withoutContextSuffix(info.resolvedModel || ALIASES[withoutContextSuffix(info.value)] || info.value);
|
|
111
|
-
}
|
|
112
|
-
function fallbackModel(info, id) {
|
|
113
|
-
const supported = info.supportedEffortLevels;
|
|
114
|
-
const thinkingLevelMap = supported?.length ? Object.fromEntries(supported.map((level) => [level, level])) : undefined;
|
|
115
|
-
return {
|
|
116
|
-
id,
|
|
117
|
-
name: `${info.displayName} (Claude Code)`,
|
|
118
|
-
api: API_ID2,
|
|
119
|
-
provider: PROVIDER_ID2,
|
|
120
|
-
baseUrl: "process://claude-code",
|
|
121
|
-
reasoning: info.supportsAdaptiveThinking === true || info.supportsEffort === true,
|
|
122
|
-
...thinkingLevelMap ? { thinkingLevelMap } : {},
|
|
123
|
-
input: ["text", "image"],
|
|
124
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
125
|
-
contextWindow: /\b1M context\b/iu.test(info.description) ? 1e6 : 200000,
|
|
126
|
-
maxTokens: 64000
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
function modelFromDiscovery(info) {
|
|
130
|
-
const id = canonicalModelId(info);
|
|
131
|
-
const builtinId = id === "default" ? withoutContextSuffix(info.resolvedModel || "") : id;
|
|
132
|
-
const builtin = BUILTIN_ANTHROPIC_MODELS.get(builtinId);
|
|
133
|
-
models.set(id, {
|
|
134
|
-
route: info.value,
|
|
135
|
-
nativeToolChanges: builtin?.compat?.supportsMidConvoToolChanges === true,
|
|
136
|
-
adaptiveThinking: info.supportsAdaptiveThinking === true
|
|
137
|
-
});
|
|
138
|
-
return builtin ? fromBuiltin(id, builtin) : fallbackModel(info, id);
|
|
139
|
-
}
|
|
140
|
-
function fallbackClaudeModels() {
|
|
141
|
-
return FALLBACK_DISCOVERY.map(modelFromDiscovery);
|
|
142
|
-
}
|
|
143
|
-
function nativeModel(model) {
|
|
144
|
-
const direct = models.get(model)?.route;
|
|
145
|
-
if (direct)
|
|
146
|
-
return direct;
|
|
147
|
-
if (model === "default")
|
|
148
|
-
return model;
|
|
149
|
-
const canonical = canonicalRoute(model);
|
|
150
|
-
return model.endsWith("[1m]") ? `${canonical}[1m]` : canonical;
|
|
151
|
-
}
|
|
152
|
-
function canonicalRoute(model) {
|
|
153
|
-
const withoutSuffix = withoutContextSuffix(model);
|
|
154
|
-
return ALIASES[withoutSuffix] ?? withoutSuffix;
|
|
155
|
-
}
|
|
156
|
-
function supportsAdaptiveThinking(model) {
|
|
157
|
-
const canonical = canonicalRoute(model);
|
|
158
|
-
return models.get(model)?.adaptiveThinking ?? models.get(canonical)?.adaptiveThinking ?? true;
|
|
159
|
-
}
|
|
160
|
-
function supportsNativeToolChanges(model) {
|
|
161
|
-
const canonical = canonicalRoute(model);
|
|
162
|
-
return models.get(model)?.nativeToolChanges ?? models.get(canonical)?.nativeToolChanges ?? BUILTIN_ANTHROPIC_MODELS.get(canonical)?.compat?.supportsMidConvoToolChanges === true;
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
restoreAnthropicModels,
|
|
166
|
-
modelFromDiscovery,
|
|
167
|
-
fallbackClaudeModels,
|
|
168
|
-
nativeModel,
|
|
169
|
-
supportsAdaptiveThinking,
|
|
170
|
-
supportsNativeToolChanges
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export { PROVIDER_ID2, API_ID2, discoveredModelSchema2, createClaudeModelCatalog2 };
|