@tanstack/ai-claude-code 0.2.3 → 0.3.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/esm/adapters/policy-map.js +44 -46
- package/dist/esm/adapters/policy-map.js.map +1 -1
- package/dist/esm/adapters/projection.js +146 -81
- package/dist/esm/adapters/projection.js.map +1 -1
- package/dist/esm/adapters/text.js +271 -296
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.js +3 -13
- package/dist/esm/messages/prompt.js +28 -33
- package/dist/esm/messages/prompt.js.map +1 -1
- package/dist/esm/model-meta.js +20 -13
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/stream/translate.js +361 -359
- package/dist/esm/stream/translate.js.map +1 -1
- package/package.json +8 -8
- package/src/adapters/text.ts +92 -18
- package/dist/esm/index.js.map +0 -1
|
@@ -1,368 +1,370 @@
|
|
|
1
1
|
import { EventType, buildBaseUsage } from "@tanstack/ai";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
//#region src/stream/translate.ts
|
|
3
|
+
/** Name of the CUSTOM event carrying the Claude Code session id. */
|
|
4
|
+
var SESSION_ID_EVENT = "claude-code.session-id";
|
|
5
|
+
/** Server name used for bridged TanStack tools (model sees `mcp__tanstack__<name>`). */
|
|
6
|
+
var BRIDGED_MCP_SERVER_NAME = "tanstack";
|
|
7
|
+
var BRIDGED_MCP_PREFIX = `mcp__${BRIDGED_MCP_SERVER_NAME}__`;
|
|
8
|
+
/**
|
|
9
|
+
* Strip the bridged MCP server prefix so tool-call events match the TanStack
|
|
10
|
+
* tool names the application registered. Built-in harness tools (Bash, Read,
|
|
11
|
+
* Edit, ...) and foreign MCP tools pass through verbatim.
|
|
12
|
+
*/
|
|
5
13
|
function stripMcpPrefix(name) {
|
|
6
|
-
|
|
14
|
+
return name.startsWith(BRIDGED_MCP_PREFIX) ? name.slice(BRIDGED_MCP_PREFIX.length) : name;
|
|
7
15
|
}
|
|
8
16
|
function stringifyToolResultContent(content) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
if (content === void 0) return "";
|
|
18
|
+
if (typeof content === "string") return content;
|
|
19
|
+
return content.map((block) => typeof block.text === "string" ? block.text : "").join("");
|
|
12
20
|
}
|
|
13
21
|
function buildUsage(usage, totalCostUsd) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (totalCostUsd !== void 0) {
|
|
32
|
-
result.providerUsageDetails = { totalCostUsd };
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
22
|
+
if (!usage) return void 0;
|
|
23
|
+
const promptTokens = usage.input_tokens ?? 0;
|
|
24
|
+
const completionTokens = usage.output_tokens ?? 0;
|
|
25
|
+
const result = buildBaseUsage({
|
|
26
|
+
promptTokens,
|
|
27
|
+
completionTokens,
|
|
28
|
+
totalTokens: promptTokens + completionTokens
|
|
29
|
+
});
|
|
30
|
+
const cacheWrite = usage.cache_creation_input_tokens;
|
|
31
|
+
const cacheRead = usage.cache_read_input_tokens;
|
|
32
|
+
const promptTokensDetails = {
|
|
33
|
+
...cacheWrite ? { cacheWriteTokens: cacheWrite } : {},
|
|
34
|
+
...cacheRead ? { cachedTokens: cacheRead } : {}
|
|
35
|
+
};
|
|
36
|
+
if (Object.keys(promptTokensDetails).length > 0) result.promptTokensDetails = promptTokensDetails;
|
|
37
|
+
if (totalCostUsd !== void 0) result.providerUsageDetails = { totalCostUsd };
|
|
38
|
+
return result;
|
|
35
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Translate a Claude Code Agent SDK message stream into AG-UI StreamChunk
|
|
42
|
+
* events.
|
|
43
|
+
*
|
|
44
|
+
* The harness runs its own agent loop and executes its own tools, so the
|
|
45
|
+
* translation always ends with `finishReason: 'stop'` (or `'length'` /
|
|
46
|
+
* RUN_ERROR) — never `'tool_calls'`. Harness tool activity is emitted as
|
|
47
|
+
* already-resolved TOOL_CALL_START/ARGS/END + TOOL_CALL_RESULT sequences so
|
|
48
|
+
* UIs can render it, while the TanStack engine never tries to execute them.
|
|
49
|
+
*
|
|
50
|
+
* Invariant: every TOOL_CALL_START is eventually paired with a
|
|
51
|
+
* TOOL_CALL_RESULT (synthesized as `{"status":"interrupted"}` when the run
|
|
52
|
+
* ends or aborts before the harness reported one) so the engine's
|
|
53
|
+
* pending-tool-call scan on the next request never force-executes them.
|
|
54
|
+
*/
|
|
36
55
|
async function* translateSdkStream(sdkMessages, ctx) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
} else if (sdkMessage.type === "assistant") {
|
|
348
|
-
if (sdkMessage.parent_tool_use_id !== null) continue;
|
|
349
|
-
yield* handleAssistant(sdkMessage);
|
|
350
|
-
} else if (sdkMessage.type === "user") {
|
|
351
|
-
if (sdkMessage.parent_tool_use_id !== null) continue;
|
|
352
|
-
yield* handleUser(sdkMessage);
|
|
353
|
-
} else if (sdkMessage.type === "result") {
|
|
354
|
-
yield* handleResult(sdkMessage);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
} catch (error) {
|
|
358
|
-
yield* synthesizeUnresolvedResults();
|
|
359
|
-
throw error;
|
|
360
|
-
}
|
|
56
|
+
const { model, runId, threadId, genId } = ctx;
|
|
57
|
+
const now = () => Date.now();
|
|
58
|
+
let runStarted = false;
|
|
59
|
+
/** Tool calls started but with no result yet. */
|
|
60
|
+
const unresolvedToolCalls = /* @__PURE__ */ new Set();
|
|
61
|
+
/** Anthropic message ids whose text/thinking already streamed via partials. */
|
|
62
|
+
const streamedMessageIds = /* @__PURE__ */ new Set();
|
|
63
|
+
let partialMessageId = null;
|
|
64
|
+
let partialBlockType = null;
|
|
65
|
+
let partialTextMessageId = null;
|
|
66
|
+
let partialTextContent = "";
|
|
67
|
+
let partialTextStarted = false;
|
|
68
|
+
let partialReasoningId = null;
|
|
69
|
+
function* startRun() {
|
|
70
|
+
if (runStarted) return;
|
|
71
|
+
runStarted = true;
|
|
72
|
+
yield {
|
|
73
|
+
type: EventType.RUN_STARTED,
|
|
74
|
+
runId,
|
|
75
|
+
threadId,
|
|
76
|
+
model,
|
|
77
|
+
timestamp: now(),
|
|
78
|
+
...ctx.parentRunId !== void 0 && { parentRunId: ctx.parentRunId }
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function* synthesizeUnresolvedResults() {
|
|
82
|
+
for (const toolCallId of unresolvedToolCalls) yield {
|
|
83
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
84
|
+
toolCallId,
|
|
85
|
+
messageId: genId(),
|
|
86
|
+
model,
|
|
87
|
+
timestamp: now(),
|
|
88
|
+
content: JSON.stringify({ status: "interrupted" })
|
|
89
|
+
};
|
|
90
|
+
unresolvedToolCalls.clear();
|
|
91
|
+
}
|
|
92
|
+
function* closePartialText() {
|
|
93
|
+
if (partialTextStarted && partialTextMessageId) yield {
|
|
94
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
95
|
+
messageId: partialTextMessageId,
|
|
96
|
+
model,
|
|
97
|
+
timestamp: now()
|
|
98
|
+
};
|
|
99
|
+
partialTextStarted = false;
|
|
100
|
+
partialTextMessageId = null;
|
|
101
|
+
partialTextContent = "";
|
|
102
|
+
}
|
|
103
|
+
function* closePartialReasoning() {
|
|
104
|
+
if (partialReasoningId) {
|
|
105
|
+
yield {
|
|
106
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
107
|
+
messageId: partialReasoningId,
|
|
108
|
+
model,
|
|
109
|
+
timestamp: now()
|
|
110
|
+
};
|
|
111
|
+
yield {
|
|
112
|
+
type: EventType.REASONING_END,
|
|
113
|
+
messageId: partialReasoningId,
|
|
114
|
+
model,
|
|
115
|
+
timestamp: now()
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
partialReasoningId = null;
|
|
119
|
+
}
|
|
120
|
+
function* emitToolUse(block) {
|
|
121
|
+
const toolCallName = stripMcpPrefix(block.name);
|
|
122
|
+
const args = JSON.stringify(block.input ?? {});
|
|
123
|
+
yield {
|
|
124
|
+
type: EventType.TOOL_CALL_START,
|
|
125
|
+
toolCallId: block.id,
|
|
126
|
+
toolCallName,
|
|
127
|
+
toolName: toolCallName,
|
|
128
|
+
model,
|
|
129
|
+
timestamp: now()
|
|
130
|
+
};
|
|
131
|
+
yield {
|
|
132
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
133
|
+
toolCallId: block.id,
|
|
134
|
+
model,
|
|
135
|
+
timestamp: now(),
|
|
136
|
+
delta: args,
|
|
137
|
+
args
|
|
138
|
+
};
|
|
139
|
+
yield {
|
|
140
|
+
type: EventType.TOOL_CALL_END,
|
|
141
|
+
toolCallId: block.id,
|
|
142
|
+
toolCallName,
|
|
143
|
+
toolName: toolCallName,
|
|
144
|
+
model,
|
|
145
|
+
timestamp: now(),
|
|
146
|
+
input: block.input ?? {}
|
|
147
|
+
};
|
|
148
|
+
unresolvedToolCalls.add(block.id);
|
|
149
|
+
}
|
|
150
|
+
function* handleAssistant(message) {
|
|
151
|
+
const alreadyStreamed = message.message.id !== void 0 && streamedMessageIds.has(message.message.id);
|
|
152
|
+
for (const block of message.message.content) if (block.type === "text") {
|
|
153
|
+
if (alreadyStreamed) continue;
|
|
154
|
+
const messageId = message.message.id ?? genId();
|
|
155
|
+
const text = block.text;
|
|
156
|
+
yield {
|
|
157
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
158
|
+
messageId,
|
|
159
|
+
model,
|
|
160
|
+
timestamp: now(),
|
|
161
|
+
role: "assistant"
|
|
162
|
+
};
|
|
163
|
+
yield {
|
|
164
|
+
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
165
|
+
messageId,
|
|
166
|
+
model,
|
|
167
|
+
timestamp: now(),
|
|
168
|
+
delta: text,
|
|
169
|
+
content: text
|
|
170
|
+
};
|
|
171
|
+
yield {
|
|
172
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
173
|
+
messageId,
|
|
174
|
+
model,
|
|
175
|
+
timestamp: now()
|
|
176
|
+
};
|
|
177
|
+
} else if (block.type === "thinking") {
|
|
178
|
+
if (alreadyStreamed) continue;
|
|
179
|
+
const reasoningId = genId();
|
|
180
|
+
const thinking = block.thinking;
|
|
181
|
+
yield {
|
|
182
|
+
type: EventType.REASONING_START,
|
|
183
|
+
messageId: reasoningId,
|
|
184
|
+
model,
|
|
185
|
+
timestamp: now()
|
|
186
|
+
};
|
|
187
|
+
yield {
|
|
188
|
+
type: EventType.REASONING_MESSAGE_START,
|
|
189
|
+
messageId: reasoningId,
|
|
190
|
+
role: "reasoning",
|
|
191
|
+
model,
|
|
192
|
+
timestamp: now()
|
|
193
|
+
};
|
|
194
|
+
yield {
|
|
195
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
196
|
+
messageId: reasoningId,
|
|
197
|
+
delta: thinking,
|
|
198
|
+
model,
|
|
199
|
+
timestamp: now()
|
|
200
|
+
};
|
|
201
|
+
yield {
|
|
202
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
203
|
+
messageId: reasoningId,
|
|
204
|
+
model,
|
|
205
|
+
timestamp: now()
|
|
206
|
+
};
|
|
207
|
+
yield {
|
|
208
|
+
type: EventType.REASONING_END,
|
|
209
|
+
messageId: reasoningId,
|
|
210
|
+
model,
|
|
211
|
+
timestamp: now()
|
|
212
|
+
};
|
|
213
|
+
} else if (block.type === "tool_use") yield* emitToolUse(block);
|
|
214
|
+
}
|
|
215
|
+
function* handleUser(message) {
|
|
216
|
+
const content = message.message.content;
|
|
217
|
+
if (typeof content === "string") return;
|
|
218
|
+
for (const block of content) {
|
|
219
|
+
if (block.type !== "tool_result") continue;
|
|
220
|
+
const toolResult = block;
|
|
221
|
+
unresolvedToolCalls.delete(toolResult.tool_use_id);
|
|
222
|
+
yield {
|
|
223
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
224
|
+
toolCallId: toolResult.tool_use_id,
|
|
225
|
+
messageId: genId(),
|
|
226
|
+
model,
|
|
227
|
+
timestamp: now(),
|
|
228
|
+
content: stringifyToolResultContent(toolResult.content),
|
|
229
|
+
...toolResult.is_error === true && { state: "output-error" }
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function* handleResult(message) {
|
|
234
|
+
yield* closePartialText();
|
|
235
|
+
yield* closePartialReasoning();
|
|
236
|
+
yield* synthesizeUnresolvedResults();
|
|
237
|
+
const usage = buildUsage(message.usage, message.total_cost_usd);
|
|
238
|
+
if (message.subtype === "success") yield {
|
|
239
|
+
type: EventType.RUN_FINISHED,
|
|
240
|
+
runId,
|
|
241
|
+
threadId,
|
|
242
|
+
model,
|
|
243
|
+
timestamp: now(),
|
|
244
|
+
finishReason: "stop",
|
|
245
|
+
...usage !== void 0 && { usage }
|
|
246
|
+
};
|
|
247
|
+
else if (message.subtype === "error_max_turns") yield {
|
|
248
|
+
type: EventType.RUN_FINISHED,
|
|
249
|
+
runId,
|
|
250
|
+
threadId,
|
|
251
|
+
model,
|
|
252
|
+
timestamp: now(),
|
|
253
|
+
finishReason: "length",
|
|
254
|
+
...usage !== void 0 && { usage }
|
|
255
|
+
};
|
|
256
|
+
else {
|
|
257
|
+
const errorMessage = message.errors && message.errors.length > 0 ? message.errors.join("; ") : `Claude Code run failed: ${message.subtype}`;
|
|
258
|
+
yield {
|
|
259
|
+
type: EventType.RUN_ERROR,
|
|
260
|
+
model,
|
|
261
|
+
timestamp: now(),
|
|
262
|
+
message: errorMessage,
|
|
263
|
+
code: message.subtype,
|
|
264
|
+
error: {
|
|
265
|
+
message: errorMessage,
|
|
266
|
+
code: message.subtype
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function* handleStreamEvent(message) {
|
|
272
|
+
const event = message.event;
|
|
273
|
+
if (event.type === "message_start") {
|
|
274
|
+
partialMessageId = event.message.id ?? genId();
|
|
275
|
+
streamedMessageIds.add(partialMessageId);
|
|
276
|
+
} else if (event.type === "content_block_start") {
|
|
277
|
+
partialBlockType = event.content_block.type;
|
|
278
|
+
if (partialBlockType === "text") {
|
|
279
|
+
partialTextMessageId = partialMessageId ?? genId();
|
|
280
|
+
partialTextContent = "";
|
|
281
|
+
if (!partialTextStarted) {
|
|
282
|
+
partialTextStarted = true;
|
|
283
|
+
yield {
|
|
284
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
285
|
+
messageId: partialTextMessageId,
|
|
286
|
+
model,
|
|
287
|
+
timestamp: now(),
|
|
288
|
+
role: "assistant"
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
} else if (partialBlockType === "thinking") {
|
|
292
|
+
partialReasoningId = genId();
|
|
293
|
+
yield {
|
|
294
|
+
type: EventType.REASONING_START,
|
|
295
|
+
messageId: partialReasoningId,
|
|
296
|
+
model,
|
|
297
|
+
timestamp: now()
|
|
298
|
+
};
|
|
299
|
+
yield {
|
|
300
|
+
type: EventType.REASONING_MESSAGE_START,
|
|
301
|
+
messageId: partialReasoningId,
|
|
302
|
+
role: "reasoning",
|
|
303
|
+
model,
|
|
304
|
+
timestamp: now()
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
} else if (event.type === "content_block_delta") {
|
|
308
|
+
if (event.delta.type === "text_delta" && partialTextStarted && partialTextMessageId && typeof event.delta.text === "string") {
|
|
309
|
+
partialTextContent += event.delta.text;
|
|
310
|
+
yield {
|
|
311
|
+
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
312
|
+
messageId: partialTextMessageId,
|
|
313
|
+
model,
|
|
314
|
+
timestamp: now(),
|
|
315
|
+
delta: event.delta.text,
|
|
316
|
+
content: partialTextContent
|
|
317
|
+
};
|
|
318
|
+
} else if (event.delta.type === "thinking_delta" && partialReasoningId && typeof event.delta.thinking === "string") yield {
|
|
319
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
320
|
+
messageId: partialReasoningId,
|
|
321
|
+
delta: event.delta.thinking,
|
|
322
|
+
model,
|
|
323
|
+
timestamp: now()
|
|
324
|
+
};
|
|
325
|
+
} else if (event.type === "content_block_stop") {
|
|
326
|
+
if (partialBlockType === "text") yield* closePartialText();
|
|
327
|
+
else if (partialBlockType === "thinking") yield* closePartialReasoning();
|
|
328
|
+
partialBlockType = null;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
for await (const sdkMessage of sdkMessages) {
|
|
333
|
+
ctx.onSdkMessage?.(sdkMessage);
|
|
334
|
+
if (sdkMessage.type === "system" && sdkMessage.subtype === "init") {
|
|
335
|
+
yield* startRun();
|
|
336
|
+
ctx.onSessionId?.(sdkMessage.session_id);
|
|
337
|
+
yield {
|
|
338
|
+
type: EventType.CUSTOM,
|
|
339
|
+
model,
|
|
340
|
+
timestamp: now(),
|
|
341
|
+
name: SESSION_ID_EVENT,
|
|
342
|
+
value: {
|
|
343
|
+
sessionId: sdkMessage.session_id,
|
|
344
|
+
model: sdkMessage.model,
|
|
345
|
+
tools: sdkMessage.tools
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
yield* startRun();
|
|
351
|
+
if (sdkMessage.type === "stream_event") {
|
|
352
|
+
if (sdkMessage.parent_tool_use_id !== null) continue;
|
|
353
|
+
yield* handleStreamEvent(sdkMessage);
|
|
354
|
+
} else if (sdkMessage.type === "assistant") {
|
|
355
|
+
if (sdkMessage.parent_tool_use_id !== null) continue;
|
|
356
|
+
yield* handleAssistant(sdkMessage);
|
|
357
|
+
} else if (sdkMessage.type === "user") {
|
|
358
|
+
if (sdkMessage.parent_tool_use_id !== null) continue;
|
|
359
|
+
yield* handleUser(sdkMessage);
|
|
360
|
+
} else if (sdkMessage.type === "result") yield* handleResult(sdkMessage);
|
|
361
|
+
}
|
|
362
|
+
} catch (error) {
|
|
363
|
+
yield* synthesizeUnresolvedResults();
|
|
364
|
+
throw error;
|
|
365
|
+
}
|
|
361
366
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
translateSdkStream
|
|
367
|
-
};
|
|
368
|
-
//# sourceMappingURL=translate.js.map
|
|
367
|
+
//#endregion
|
|
368
|
+
export { BRIDGED_MCP_SERVER_NAME, SESSION_ID_EVENT, stripMcpPrefix, translateSdkStream };
|
|
369
|
+
|
|
370
|
+
//# sourceMappingURL=translate.js.map
|