@nyte-ai/plugin 0.0.0-stage → 0.0.18
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/THIRD-PARTY-NOTICES.md +800 -0
- package/dist/chunks/billing-0y7dfkp2.js +146 -0
- package/dist/chunks/billing-2v3g7rd9.js +190 -0
- package/dist/chunks/billing-3j77ss7y.js +86 -0
- package/dist/chunks/billing-3scqmkzb.js +559 -0
- package/dist/chunks/billing-6z2kgtyt.js +51 -0
- package/dist/chunks/billing-8rjk35gr.js +444 -0
- package/dist/chunks/billing-9mfedmsh.js +373 -0
- package/dist/chunks/billing-9nn3yp18.js +176 -0
- package/dist/chunks/billing-9qc0eqh4.js +174 -0
- package/dist/chunks/billing-a9vqn66p.js +351 -0
- package/dist/chunks/billing-bv5zr32b.js +369 -0
- package/dist/chunks/billing-cy1rg619.js +302 -0
- package/dist/chunks/billing-ejx2hv52.js +323 -0
- package/dist/chunks/billing-h13r0byk.js +80 -0
- package/dist/chunks/billing-j643d12p.js +197 -0
- package/dist/chunks/billing-k2p1t25n.js +109 -0
- package/dist/chunks/billing-kn3d30b4.js +276 -0
- package/dist/chunks/billing-mvc33pv6.js +36 -0
- package/dist/chunks/billing-s4rrspsk.js +7 -0
- package/dist/chunks/billing-svrhh5a5.js +307 -0
- package/dist/chunks/billing-sxc70w72.js +161 -0
- package/dist/chunks/billing-t1qrxe16.js +41 -0
- package/dist/chunks/billing-tzr3cebf.js +20 -0
- package/dist/chunks/billing-wmcbg6fw.js +52 -0
- package/dist/chunks/billing-wmkxkypm.js +527 -0
- package/dist/chunks/billing-ye3wk5r3.js +205 -0
- package/dist/chunks/billing-zfpm2rt0.js +29 -0
- package/dist/examples/claude-code/LICENSE +21 -0
- package/dist/examples/claude-code/billing.d.ts +7 -0
- package/dist/examples/claude-code/billing.js +11 -0
- package/dist/examples/claude-code/claude-cli.d.ts +35 -0
- package/dist/examples/claude-code/claude-cli.js +15 -0
- package/dist/examples/claude-code/env.d.ts +1 -0
- package/dist/examples/claude-code/env.js +6 -0
- package/dist/examples/claude-code/index.d.ts +24 -0
- package/dist/examples/claude-code/index.js +149 -0
- package/dist/examples/claude-code/inert-mcp.d.ts +1 -0
- package/dist/examples/claude-code/inert-mcp.js +6 -0
- package/dist/examples/claude-code/model-catalog.d.ts +27 -0
- package/dist/examples/claude-code/model-catalog.js +12 -0
- package/dist/examples/claude-code/process.d.ts +22 -0
- package/dist/examples/claude-code/process.js +25 -0
- package/dist/examples/claude-code/replay.d.ts +4 -0
- package/dist/examples/claude-code/replay.js +11 -0
- package/dist/examples/claude-code/stream.d.ts +3 -0
- package/dist/examples/claude-code/stream.js +16 -0
- package/dist/examples/claude-code/transcript.d.ts +83 -0
- package/dist/examples/claude-code/transcript.js +23 -0
- package/dist/examples/claude-code/turn-relay.d.ts +48 -0
- package/dist/examples/claude-code/turn-relay.js +9 -0
- package/dist/examples/claude-code/wire.d.ts +372 -0
- package/dist/examples/claude-code/wire.js +18 -0
- package/dist/src/acp/connection.d.ts +24 -0
- package/dist/src/acp/connection.js +8 -0
- package/dist/src/acp/index.d.ts +54 -0
- package/dist/src/acp/index.js +1004 -0
- package/dist/src/acp/registry.d.ts +18 -0
- package/dist/src/acp/registry.js +8 -0
- package/dist/src/acp/thread.d.ts +46 -0
- package/dist/src/acp/thread.js +7 -0
- package/dist/src/acp/transcript.d.ts +8 -0
- package/dist/src/acp/transcript.js +11 -0
- package/dist/src/acp/translate.d.ts +33 -0
- package/dist/src/acp/translate.js +20 -0
- package/dist/src/approvals/approvals.d.ts +189 -0
- package/dist/src/approvals/approvals.js +143 -0
- package/dist/src/approvals/schema.d.ts +169 -0
- package/dist/src/approvals/schema.js +18 -0
- package/dist/src/bash-description.d.ts +30 -0
- package/dist/src/bash-description.js +18 -0
- package/dist/src/browser-tools.d.ts +25 -0
- package/dist/src/browser-tools.js +246 -0
- package/dist/src/canvas/canvas-compile.d.ts +48 -0
- package/dist/src/canvas/canvas-compile.js +8 -0
- package/dist/src/canvas/canvas-libraries.d.ts +32 -0
- package/dist/src/canvas/canvas-libraries.js +6 -0
- package/dist/src/canvas/canvas-preview.d.ts +107 -0
- package/dist/src/canvas/canvas-preview.js +12 -0
- package/dist/src/canvas/canvas-protocol.d.ts +184 -0
- package/dist/src/canvas/canvas-protocol.js +75 -0
- package/dist/src/canvas/canvas.d.ts +50 -0
- package/dist/src/canvas/canvas.js +221 -0
- package/dist/src/canvas/schema.d.ts +47 -0
- package/dist/src/canvas/schema.js +8 -0
- package/dist/src/claude-code-sdk/index.d.ts +35 -0
- package/dist/src/claude-code-sdk/index.js +368 -0
- package/dist/src/claude-code-sdk/session.d.ts +48 -0
- package/dist/src/claude-code-sdk/session.js +7 -0
- package/dist/src/claude-code-sdk/transcript.d.ts +19 -0
- package/dist/src/claude-code-sdk/transcript.js +15 -0
- package/dist/src/claude-code-sdk/translate.d.ts +18 -0
- package/dist/src/claude-code-sdk/translate.js +20 -0
- package/dist/src/client/mount.d.ts +44 -0
- package/dist/src/client/mount.js +252 -0
- package/dist/src/client/notifications.d.ts +35 -0
- package/dist/src/client/notifications.js +26 -0
- package/dist/src/client/presentation.d.ts +336 -0
- package/dist/src/client/presentation.js +373 -0
- package/dist/src/client/transcript-tui.d.ts +23 -0
- package/dist/src/client/transcript-tui.js +20 -0
- package/dist/src/client/transcript.d.ts +23 -0
- package/dist/src/client/transcript.js +51 -0
- package/dist/src/client.d.ts +53 -0
- package/dist/src/client.js +22 -0
- package/dist/src/codemode-runtime.d.ts +11 -0
- package/dist/src/codemode-runtime.js +10 -0
- package/dist/src/codemode-worker.d.ts +1 -0
- package/dist/src/codemode-worker.js +2 -0
- package/dist/src/codemode.d.ts +21 -0
- package/dist/src/codemode.js +170 -0
- package/dist/src/core.d.ts +43 -0
- package/dist/src/core.js +11 -0
- package/dist/src/openai-astra-context.d.ts +23 -0
- package/dist/src/openai-astra-context.js +14 -0
- package/dist/src/openai-compaction.d.ts +15 -0
- package/dist/src/openai-compaction.js +55 -0
- package/dist/src/question/client/questions-tui.d.ts +9 -0
- package/dist/src/question/client/questions-tui.js +19 -0
- package/dist/src/question/client/questions.d.ts +9 -0
- package/dist/src/question/client/questions.js +160 -0
- package/dist/src/question/question.d.ts +63 -0
- package/dist/src/question/question.js +9 -0
- package/dist/src/question/schema.d.ts +101 -0
- package/dist/src/question/schema.js +14 -0
- package/dist/src/rename.d.ts +78 -0
- package/dist/src/rename.js +78 -0
- package/dist/src/telemetry.d.ts +36 -0
- package/dist/src/telemetry.js +84 -0
- package/dist/src/web-search.d.ts +78 -0
- package/dist/src/web-search.js +219 -0
- package/package.json +190 -3
- package/README.md +0 -3
|
@@ -0,0 +1,444 @@
|
|
|
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 };
|