@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.
@@ -1,368 +1,370 @@
1
1
  import { EventType, buildBaseUsage } from "@tanstack/ai";
2
- const SESSION_ID_EVENT = "claude-code.session-id";
3
- const BRIDGED_MCP_SERVER_NAME = "tanstack";
4
- const BRIDGED_MCP_PREFIX = `mcp__${BRIDGED_MCP_SERVER_NAME}__`;
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
- return name.startsWith(BRIDGED_MCP_PREFIX) ? name.slice(BRIDGED_MCP_PREFIX.length) : name;
14
+ return name.startsWith(BRIDGED_MCP_PREFIX) ? name.slice(BRIDGED_MCP_PREFIX.length) : name;
7
15
  }
8
16
  function stringifyToolResultContent(content) {
9
- if (content === void 0) return "";
10
- if (typeof content === "string") return content;
11
- return content.map((block) => typeof block.text === "string" ? block.text : "").join("");
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
- if (!usage) return void 0;
15
- const promptTokens = usage.input_tokens ?? 0;
16
- const completionTokens = usage.output_tokens ?? 0;
17
- const result = buildBaseUsage({
18
- promptTokens,
19
- completionTokens,
20
- totalTokens: promptTokens + completionTokens
21
- });
22
- const cacheWrite = usage.cache_creation_input_tokens;
23
- const cacheRead = usage.cache_read_input_tokens;
24
- const promptTokensDetails = {
25
- ...cacheWrite ? { cacheWriteTokens: cacheWrite } : {},
26
- ...cacheRead ? { cachedTokens: cacheRead } : {}
27
- };
28
- if (Object.keys(promptTokensDetails).length > 0) {
29
- result.promptTokensDetails = promptTokensDetails;
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
- const { model, runId, threadId, genId } = ctx;
38
- const now = () => Date.now();
39
- let runStarted = false;
40
- const unresolvedToolCalls = /* @__PURE__ */ new Set();
41
- const streamedMessageIds = /* @__PURE__ */ new Set();
42
- let partialMessageId = null;
43
- let partialBlockType = null;
44
- let partialTextMessageId = null;
45
- let partialTextContent = "";
46
- let partialTextStarted = false;
47
- let partialReasoningId = null;
48
- function* startRun() {
49
- if (runStarted) return;
50
- runStarted = true;
51
- yield {
52
- type: EventType.RUN_STARTED,
53
- runId,
54
- threadId,
55
- model,
56
- timestamp: now(),
57
- ...ctx.parentRunId !== void 0 && { parentRunId: ctx.parentRunId }
58
- };
59
- }
60
- function* synthesizeUnresolvedResults() {
61
- for (const toolCallId of unresolvedToolCalls) {
62
- yield {
63
- type: EventType.TOOL_CALL_RESULT,
64
- toolCallId,
65
- messageId: genId(),
66
- model,
67
- timestamp: now(),
68
- content: JSON.stringify({ status: "interrupted" })
69
- };
70
- }
71
- unresolvedToolCalls.clear();
72
- }
73
- function* closePartialText() {
74
- if (partialTextStarted && partialTextMessageId) {
75
- yield {
76
- type: EventType.TEXT_MESSAGE_END,
77
- messageId: partialTextMessageId,
78
- model,
79
- timestamp: now()
80
- };
81
- }
82
- partialTextStarted = false;
83
- partialTextMessageId = null;
84
- partialTextContent = "";
85
- }
86
- function* closePartialReasoning() {
87
- if (partialReasoningId) {
88
- yield {
89
- type: EventType.REASONING_MESSAGE_END,
90
- messageId: partialReasoningId,
91
- model,
92
- timestamp: now()
93
- };
94
- yield {
95
- type: EventType.REASONING_END,
96
- messageId: partialReasoningId,
97
- model,
98
- timestamp: now()
99
- };
100
- }
101
- partialReasoningId = null;
102
- }
103
- function* emitToolUse(block) {
104
- const toolCallName = stripMcpPrefix(block.name);
105
- const args = JSON.stringify(block.input ?? {});
106
- yield {
107
- type: EventType.TOOL_CALL_START,
108
- toolCallId: block.id,
109
- toolCallName,
110
- toolName: toolCallName,
111
- model,
112
- timestamp: now()
113
- };
114
- yield {
115
- type: EventType.TOOL_CALL_ARGS,
116
- toolCallId: block.id,
117
- model,
118
- timestamp: now(),
119
- delta: args,
120
- args
121
- };
122
- yield {
123
- type: EventType.TOOL_CALL_END,
124
- toolCallId: block.id,
125
- toolCallName,
126
- toolName: toolCallName,
127
- model,
128
- timestamp: now(),
129
- input: block.input ?? {}
130
- };
131
- unresolvedToolCalls.add(block.id);
132
- }
133
- function* handleAssistant(message) {
134
- const alreadyStreamed = message.message.id !== void 0 && streamedMessageIds.has(message.message.id);
135
- for (const block of message.message.content) {
136
- if (block.type === "text") {
137
- if (alreadyStreamed) continue;
138
- const messageId = message.message.id ?? genId();
139
- const text = block.text;
140
- yield {
141
- type: EventType.TEXT_MESSAGE_START,
142
- messageId,
143
- model,
144
- timestamp: now(),
145
- role: "assistant"
146
- };
147
- yield {
148
- type: EventType.TEXT_MESSAGE_CONTENT,
149
- messageId,
150
- model,
151
- timestamp: now(),
152
- delta: text,
153
- content: text
154
- };
155
- yield {
156
- type: EventType.TEXT_MESSAGE_END,
157
- messageId,
158
- model,
159
- timestamp: now()
160
- };
161
- } else if (block.type === "thinking") {
162
- if (alreadyStreamed) continue;
163
- const reasoningId = genId();
164
- const thinking = block.thinking;
165
- yield {
166
- type: EventType.REASONING_START,
167
- messageId: reasoningId,
168
- model,
169
- timestamp: now()
170
- };
171
- yield {
172
- type: EventType.REASONING_MESSAGE_START,
173
- messageId: reasoningId,
174
- role: "reasoning",
175
- model,
176
- timestamp: now()
177
- };
178
- yield {
179
- type: EventType.REASONING_MESSAGE_CONTENT,
180
- messageId: reasoningId,
181
- delta: thinking,
182
- model,
183
- timestamp: now()
184
- };
185
- yield {
186
- type: EventType.REASONING_MESSAGE_END,
187
- messageId: reasoningId,
188
- model,
189
- timestamp: now()
190
- };
191
- yield {
192
- type: EventType.REASONING_END,
193
- messageId: reasoningId,
194
- model,
195
- timestamp: now()
196
- };
197
- } else if (block.type === "tool_use") {
198
- yield* emitToolUse(
199
- block
200
- );
201
- }
202
- }
203
- }
204
- function* handleUser(message) {
205
- const content = message.message.content;
206
- if (typeof content === "string") return;
207
- for (const block of content) {
208
- if (block.type !== "tool_result") continue;
209
- const toolResult = block;
210
- unresolvedToolCalls.delete(toolResult.tool_use_id);
211
- yield {
212
- type: EventType.TOOL_CALL_RESULT,
213
- toolCallId: toolResult.tool_use_id,
214
- messageId: genId(),
215
- model,
216
- timestamp: now(),
217
- content: stringifyToolResultContent(toolResult.content),
218
- ...toolResult.is_error === true && { state: "output-error" }
219
- };
220
- }
221
- }
222
- function* handleResult(message) {
223
- yield* closePartialText();
224
- yield* closePartialReasoning();
225
- yield* synthesizeUnresolvedResults();
226
- const usage = buildUsage(message.usage, message.total_cost_usd);
227
- if (message.subtype === "success") {
228
- yield {
229
- type: EventType.RUN_FINISHED,
230
- runId,
231
- threadId,
232
- model,
233
- timestamp: now(),
234
- finishReason: "stop",
235
- ...usage !== void 0 && { usage }
236
- };
237
- } else if (message.subtype === "error_max_turns") {
238
- yield {
239
- type: EventType.RUN_FINISHED,
240
- runId,
241
- threadId,
242
- model,
243
- timestamp: now(),
244
- finishReason: "length",
245
- ...usage !== void 0 && { usage }
246
- };
247
- } else {
248
- const errorMessage = message.errors && message.errors.length > 0 ? message.errors.join("; ") : `Claude Code run failed: ${message.subtype}`;
249
- yield {
250
- type: EventType.RUN_ERROR,
251
- model,
252
- timestamp: now(),
253
- message: errorMessage,
254
- code: message.subtype,
255
- error: { message: errorMessage, code: message.subtype }
256
- };
257
- }
258
- }
259
- function* handleStreamEvent(message) {
260
- const event = message.event;
261
- if (event.type === "message_start") {
262
- partialMessageId = event.message.id ?? genId();
263
- streamedMessageIds.add(partialMessageId);
264
- } else if (event.type === "content_block_start") {
265
- partialBlockType = event.content_block.type;
266
- if (partialBlockType === "text") {
267
- partialTextMessageId = partialMessageId ?? genId();
268
- partialTextContent = "";
269
- if (!partialTextStarted) {
270
- partialTextStarted = true;
271
- yield {
272
- type: EventType.TEXT_MESSAGE_START,
273
- messageId: partialTextMessageId,
274
- model,
275
- timestamp: now(),
276
- role: "assistant"
277
- };
278
- }
279
- } else if (partialBlockType === "thinking") {
280
- partialReasoningId = genId();
281
- yield {
282
- type: EventType.REASONING_START,
283
- messageId: partialReasoningId,
284
- model,
285
- timestamp: now()
286
- };
287
- yield {
288
- type: EventType.REASONING_MESSAGE_START,
289
- messageId: partialReasoningId,
290
- role: "reasoning",
291
- model,
292
- timestamp: now()
293
- };
294
- }
295
- } else if (event.type === "content_block_delta") {
296
- if (event.delta.type === "text_delta" && partialTextStarted && partialTextMessageId && typeof event.delta.text === "string") {
297
- partialTextContent += event.delta.text;
298
- yield {
299
- type: EventType.TEXT_MESSAGE_CONTENT,
300
- messageId: partialTextMessageId,
301
- model,
302
- timestamp: now(),
303
- delta: event.delta.text,
304
- content: partialTextContent
305
- };
306
- } else if (event.delta.type === "thinking_delta" && partialReasoningId && typeof event.delta.thinking === "string") {
307
- yield {
308
- type: EventType.REASONING_MESSAGE_CONTENT,
309
- messageId: partialReasoningId,
310
- delta: event.delta.thinking,
311
- model,
312
- timestamp: now()
313
- };
314
- }
315
- } else if (event.type === "content_block_stop") {
316
- if (partialBlockType === "text") {
317
- yield* closePartialText();
318
- } else if (partialBlockType === "thinking") {
319
- yield* closePartialReasoning();
320
- }
321
- partialBlockType = null;
322
- }
323
- }
324
- try {
325
- for await (const sdkMessage of sdkMessages) {
326
- ctx.onSdkMessage?.(sdkMessage);
327
- if (sdkMessage.type === "system" && sdkMessage.subtype === "init") {
328
- yield* startRun();
329
- ctx.onSessionId?.(sdkMessage.session_id);
330
- yield {
331
- type: EventType.CUSTOM,
332
- model,
333
- timestamp: now(),
334
- name: SESSION_ID_EVENT,
335
- value: {
336
- sessionId: sdkMessage.session_id,
337
- model: sdkMessage.model,
338
- tools: sdkMessage.tools
339
- }
340
- };
341
- continue;
342
- }
343
- yield* startRun();
344
- if (sdkMessage.type === "stream_event") {
345
- if (sdkMessage.parent_tool_use_id !== null) continue;
346
- yield* handleStreamEvent(sdkMessage);
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
- export {
363
- BRIDGED_MCP_SERVER_NAME,
364
- SESSION_ID_EVENT,
365
- stripMcpPrefix,
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