File size: 24,243 Bytes
3d97d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8d62b
3d97d52
 
 
 
 
 
 
 
 
db8d62b
 
 
3d97d52
 
 
 
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
 
 
 
 
 
 
 
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8d62b
3d97d52
 
 
 
 
 
db8d62b
 
 
 
 
 
 
3d97d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8d62b
3d97d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db8d62b
 
 
 
3d97d52
db8d62b
 
 
3d97d52
db8d62b
3d97d52
db8d62b
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
3d97d52
 
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
3d97d52
db8d62b
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
 
db8d62b
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
3d97d52
db8d62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d97d52
db8d62b
 
 
 
 
3d97d52
 
 
 
 
db8d62b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
import { type Response as ExpressResponse } from "express";
import { type ValidatedRequest } from "../middleware/validation.js";
import type { CreateResponseParams, McpServerParams, McpApprovalRequestParams } from "../schemas.js";
import { generateUniqueId } from "../lib/generateUniqueId.js";
import { InferenceClient } from "@huggingface/inference";
import type {
	ChatCompletionInputMessage,
	ChatCompletionInputMessageChunkType,
	ChatCompletionInput,
} from "@huggingface/tasks";

import type {
	Response,
	ResponseStreamEvent,
	ResponseContentPartAddedEvent,
	ResponseOutputMessage,
	ResponseFunctionToolCall,
	ResponseOutputItem,
} from "openai/resources/responses/responses";
import type { ChatCompletionInputTool } from "@huggingface/tasks/dist/commonjs/tasks/chat-completion/inference.js";
import { callMcpTool, connectMcpServer } from "../mcp.js";

class StreamingError extends Error {
	constructor(message: string) {
		super(message);
		this.name = "StreamingError";
	}
}

type IncompleteResponse = Omit<Response, "incomplete_details" | "output_text" | "parallel_tool_calls">;
const SEQUENCE_NUMBER_PLACEHOLDER = -1;

export const postCreateResponse = async (
	req: ValidatedRequest<CreateResponseParams>,
	res: ExpressResponse
): Promise<void> => {
	// To avoid duplicated code, we run all requests as stream.
	const events = runCreateResponseStream(req, res);

	// Then we return in the correct format depending on the user 'stream' flag.
	if (req.body.stream) {
		res.setHeader("Content-Type", "text/event-stream");
		res.setHeader("Connection", "keep-alive");
		console.debug("Stream request");
		for await (const event of events) {
			console.debug(`Event #${event.sequence_number}: ${event.type}`);
			res.write(`data: ${JSON.stringify(event)}\n\n`);
		}
		res.end();
	} else {
		console.debug("Non-stream request");
		for await (const event of events) {
			if (event.type === "response.completed" || event.type === "response.failed") {
				console.debug(event.type);
				res.json(event.response);
			}
		}
	}
};

/*
 * Top-level stream.
 *
 * Handles response lifecycle + execute inner logic (MCP list tools, MCP tool calls, LLM call, etc.).
 * Handles sequenceNumber by overwriting it in the events.
 */
async function* runCreateResponseStream(
	req: ValidatedRequest<CreateResponseParams>,
	res: ExpressResponse
): AsyncGenerator<ResponseStreamEvent> {
	let sequenceNumber = 0;
	// Prepare response object that will be iteratively populated
	const responseObject: IncompleteResponse = {
		created_at: Math.floor(new Date().getTime() / 1000),
		error: null,
		id: generateUniqueId("resp"),
		instructions: req.body.instructions,
		max_output_tokens: req.body.max_output_tokens,
		metadata: req.body.metadata,
		model: req.body.model,
		object: "response",
		output: [],
		// parallel_tool_calls: req.body.parallel_tool_calls,
		status: "in_progress",
		text: req.body.text,
		tool_choice: req.body.tool_choice ?? "auto",
		tools: req.body.tools ?? [],
		temperature: req.body.temperature,
		top_p: req.body.top_p,
		usage: {
			input_tokens: 0,
			input_tokens_details: { cached_tokens: 0 },
			output_tokens: 0,
			output_tokens_details: { reasoning_tokens: 0 },
			total_tokens: 0,
		},
	};

	// Response created event
	yield {
		type: "response.created",
		response: responseObject as Response,
		sequence_number: sequenceNumber++,
	};

	// Response in progress event
	yield {
		type: "response.in_progress",
		response: responseObject as Response,
		sequence_number: sequenceNumber++,
	};

	// Any events (LLM call, MCP call, list tools, etc.)
	try {
		for await (const event of innerRunStream(req, res, responseObject)) {
			yield { ...event, sequence_number: sequenceNumber++ };
		}
	} catch (error) {
		// Error event => stop
		console.error("Error in stream:", error);
		const message =
			typeof error === "object" && error && "message" in error && typeof error.message === "string"
				? error.message
				: "An error occurred in stream";
		responseObject.status = "failed";
		responseObject.error = {
			code: "server_error",
			message,
		};
		yield {
			type: "response.failed",
			response: responseObject as Response,
			sequence_number: sequenceNumber++,
		};
		return;
	}

	// Response completed event
	yield {
		type: "response.completed",
		response: responseObject as Response,
		sequence_number: sequenceNumber++,
	};
}

async function* innerRunStream(
	req: ValidatedRequest<CreateResponseParams>,
	res: ExpressResponse,
	responseObject: IncompleteResponse
): AsyncGenerator<ResponseStreamEvent> {
	// Retrieve API key from headers
	const apiKey = req.headers.authorization?.split(" ")[1];
	if (!apiKey) {
		res.status(401).json({
			success: false,
			error: "Unauthorized",
		});
		return;
	}

	// List MCP tools from server (if required) + prepare tools for the LLM
	let tools: ChatCompletionInputTool[] | undefined = [];
	const mcpToolsMapping: Record<string, McpServerParams> = {};
	if (req.body.tools) {
		for (const tool of req.body.tools) {
			switch (tool.type) {
				case "function":
					tools?.push({
						type: tool.type,
						function: {
							name: tool.name,
							parameters: tool.parameters,
							description: tool.description,
							strict: tool.strict,
						},
					});
					break;
				case "mcp": {
					let mcpListTools: ResponseOutputItem.McpListTools | undefined;

					// If MCP list tools is already in the input, use it
					if (Array.isArray(req.body.input)) {
						for (const item of req.body.input) {
							if (item.type === "mcp_list_tools" && item.server_label === tool.server_label) {
								mcpListTools = item;
								console.debug(`Using MCP list tools from input for server '${tool.server_label}'`);
								break;
							}
						}
					}
					// Otherwise, list tools from MCP server
					if (!mcpListTools) {
						for await (const event of listMcpToolsStream(tool, responseObject)) {
							yield event;
						}
						mcpListTools = responseObject.output.at(-1) as ResponseOutputItem.McpListTools;
					}

					// Only allowed tools are forwarded to the LLM
					const allowedTools = tool.allowed_tools
						? Array.isArray(tool.allowed_tools)
							? tool.allowed_tools
							: tool.allowed_tools.tool_names
						: [];
					if (mcpListTools?.tools) {
						for (const mcpTool of mcpListTools.tools) {
							if (allowedTools.length === 0 || allowedTools.includes(mcpTool.name)) {
								tools?.push({
									type: "function" as const,
									function: {
										name: mcpTool.name,
										parameters: mcpTool.input_schema,
										description: mcpTool.description ?? undefined,
									},
								});
							}
							mcpToolsMapping[mcpTool.name] = tool;
						}
						break;
					}
				}
			}
		}
	}
	if (tools.length === 0) {
		tools = undefined;
	}

	// If MCP approval requests => execute them and return (no LLM call)
	if (Array.isArray(req.body.input)) {
		for (const item of req.body.input) {
			// Note: currently supporting only 1 mcp_approval_response per request
			let shouldStop = false;
			if (item.type === "mcp_approval_response" && item.approve) {
				const approvalRequest = req.body.input.find(
					(i) => i.type === "mcp_approval_request" && i.id === item.approval_request_id
				) as McpApprovalRequestParams | undefined;
				for await (const event of callApprovedMCPToolStream(
					item.approval_request_id,
					approvalRequest,
					mcpToolsMapping,
					responseObject
				)) {
					yield event;
				}
				shouldStop = true;
			}
			if (shouldStop) {
				// stop if at least one approval request is processed
				break;
			}
		}
	}

	// At this point, we have all tools and we know we want to call the LLM
	// Let's prepare the payload and make the call!

	// Resolve model and provider
	const model = req.body.model.includes("@") ? req.body.model.split("@")[1] : req.body.model;
	const provider = req.body.model.includes("@") ? req.body.model.split("@")[0] : undefined;

	// Format input to Chat Completion format
	const messages: ChatCompletionInputMessage[] = req.body.instructions
		? [{ role: "system", content: req.body.instructions }]
		: [];
	if (Array.isArray(req.body.input)) {
		messages.push(
			...req.body.input
				.map((item) => {
					switch (item.type) {
						case "function_call":
							return {
								// hacky but best fit for now
								role: "assistant",
								name: `function_call ${item.name} ${item.call_id}`,
								content: item.arguments,
							};
						case "function_call_output":
							return {
								// hacky but best fit for now
								role: "assistant",
								name: `function_call_output ${item.call_id}`,
								content: item.output,
							};
						case "message":
							return {
								role: item.role,
								content:
									typeof item.content === "string"
										? item.content
										: item.content
												.map((content) => {
													switch (content.type) {
														case "input_image":
															return {
																type: "image_url" as ChatCompletionInputMessageChunkType,
																image_url: {
																	url: content.image_url,
																},
															};
														case "output_text":
															return content.text
																? {
																		type: "text" as ChatCompletionInputMessageChunkType,
																		text: content.text,
																	}
																: undefined;
														case "refusal":
															return undefined;
														case "input_text":
															return {
																type: "text" as ChatCompletionInputMessageChunkType,
																text: content.text,
															};
													}
												})
												.filter((item) => item !== undefined),
							};
						case "mcp_list_tools": {
							// Hacky: will be dropped by filter since tools are passed as separate objects
							return {
								role: "assistant",
								name: "mcp_list_tools",
								content: "",
							};
						}
						case "mcp_call": {
							return {
								role: "assistant",
								name: "mcp_call",
								content: `MCP call (${item.id}). Server: '${item.server_label}'. Tool: '${item.name}'. Arguments: '${item.arguments}'.`,
							};
						}
						case "mcp_approval_request": {
							return {
								role: "assistant",
								name: "mcp_approval_request",
								content: `MCP approval request (${item.id}). Server: '${item.server_label}'. Tool: '${item.name}'. Arguments: '${item.arguments}'.`,
							};
						}
						case "mcp_approval_response": {
							return {
								role: "assistant",
								name: "mcp_approval_response",
								content: `MCP approval response (${item.id}). Approved: ${item.approve}. Reason: ${item.reason}.`,
							};
						}
					}
				})
				.filter((message) => message.content?.length !== 0)
		);
	} else {
		messages.push({ role: "user", content: req.body.input });
	}

	// Prepare payload for the LLM
	const payload: ChatCompletionInput = {
		// main params
		model: model,
		provider: provider,
		messages: messages,
		stream: req.body.stream,
		// options
		max_tokens: req.body.max_output_tokens === null ? undefined : req.body.max_output_tokens,
		response_format: req.body.text?.format
			? {
					type: req.body.text.format.type,
					json_schema:
						req.body.text.format.type === "json_schema"
							? {
									description: req.body.text.format.description,
									name: req.body.text.format.name,
									schema: req.body.text.format.schema,
									strict: req.body.text.format.strict,
								}
							: undefined,
				}
			: undefined,
		temperature: req.body.temperature,
		tool_choice:
			typeof req.body.tool_choice === "string"
				? req.body.tool_choice
				: req.body.tool_choice
					? {
							type: "function",
							function: {
								name: req.body.tool_choice.name,
							},
						}
					: undefined,
		tools,
		top_p: req.body.top_p,
	};

	// Call LLM
	for await (const event of callLLMStream(apiKey, payload, responseObject, mcpToolsMapping)) {
		yield event;
	}

	// Handle MCP tool calls if any
	for await (const event of handleMCPToolCallsAfterLLM(responseObject, mcpToolsMapping)) {
		yield event;
	}
}

async function* listMcpToolsStream(
	tool: McpServerParams,
	responseObject: IncompleteResponse
): AsyncGenerator<ResponseStreamEvent> {
	yield {
		type: "response.mcp_list_tools.in_progress",
		sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
	};

	try {
		const mcp = await connectMcpServer(tool);
		const mcpTools = await mcp.listTools();
		yield {
			type: "response.mcp_list_tools.completed",
			sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
		};
		responseObject.output.push({
			id: generateUniqueId("mcp_list_tools"),
			type: "mcp_list_tools",
			server_label: tool.server_label,
			tools: mcpTools.tools.map((mcpTool) => ({
				input_schema: mcpTool.inputSchema,
				name: mcpTool.name,
				annotations: mcpTool.annotations,
				description: mcpTool.description,
			})),
		});
	} catch (error) {
		const errorMessage = `Failed to list tools from MCP server '${tool.server_label}': ${error instanceof Error ? error.message : "Unknown error"}`;
		console.error(errorMessage);
		yield {
			type: "response.mcp_list_tools.failed",
			sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
		};
		throw new Error(errorMessage);
	}
}

/*
 * Call LLM and stream the response.
 */
async function* callLLMStream(
	apiKey: string | undefined,
	payload: ChatCompletionInput,
	responseObject: IncompleteResponse,
	mcpToolsMapping: Record<string, McpServerParams>
): AsyncGenerator<ResponseStreamEvent> {
	const stream = new InferenceClient(apiKey).chatCompletionStream(payload);

	for await (const chunk of stream) {
		if (chunk.usage) {
			// Overwrite usage with the latest chunk's usage
			responseObject.usage = {
				input_tokens: chunk.usage.prompt_tokens,
				input_tokens_details: { cached_tokens: 0 },
				output_tokens: chunk.usage.completion_tokens,
				output_tokens_details: { reasoning_tokens: 0 },
				total_tokens: chunk.usage.total_tokens,
			};
		}

		const delta = chunk.choices[0].delta;
		if (delta.content) {
			let currentOutputItem = responseObject.output.at(-1);

			// If start of a new message, create it
			if (currentOutputItem?.type !== "message" || currentOutputItem?.status !== "in_progress") {
				const outputObject: ResponseOutputMessage = {
					id: generateUniqueId("msg"),
					type: "message",
					role: "assistant",
					status: "in_progress",
					content: [],
				};
				responseObject.output.push(outputObject);

				// Response output item added event
				yield {
					type: "response.output_item.added",
					output_index: 0,
					item: outputObject,
					sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
				};
			}

			// If start of a new content part, create it
			currentOutputItem = responseObject.output.at(-1) as ResponseOutputMessage;
			if (currentOutputItem.content.length === 0) {
				// Response content part added event
				const contentPart: ResponseContentPartAddedEvent["part"] = {
					type: "output_text",
					text: "",
					annotations: [],
				};
				currentOutputItem.content.push(contentPart);

				yield {
					type: "response.content_part.added",
					item_id: currentOutputItem.id,
					output_index: responseObject.output.length - 1,
					content_index: currentOutputItem.content.length - 1,
					part: contentPart,
					sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
				};
			}

			const contentPart = currentOutputItem.content.at(-1);
			if (!contentPart || contentPart.type !== "output_text") {
				throw new StreamingError(
					`Not implemented: only output_text is supported in response.output[].content[].type. Got ${contentPart?.type}`
				);
			}

			// Add text delta
			contentPart.text += delta.content;
			yield {
				type: "response.output_text.delta",
				item_id: currentOutputItem.id,
				output_index: responseObject.output.length - 1,
				content_index: currentOutputItem.content.length - 1,
				delta: delta.content,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
		} else if (delta.tool_calls && delta.tool_calls.length > 0) {
			if (delta.tool_calls.length > 1) {
				throw new StreamingError("Not implemented: multiple tool calls are not supported.");
			}

			let currentOutputItem = responseObject.output.at(-1);
			if (currentOutputItem?.type !== "mcp_call" && currentOutputItem?.type !== "function_call") {
				if (!delta.tool_calls[0].function.name) {
					throw new StreamingError("Tool call function name is required when starting a new tool call.");
				}

				const newOutputObject: ResponseOutputItem.McpCall | ResponseFunctionToolCall =
					delta.tool_calls[0].function.name in mcpToolsMapping
						? {
								type: "mcp_call",
								id: generateUniqueId("mcp_call"),
								name: delta.tool_calls[0].function.name,
								server_label: mcpToolsMapping[delta.tool_calls[0].function.name].server_label,
								arguments: "",
							}
						: {
								type: "function_call",
								id: generateUniqueId("fc"),
								call_id: delta.tool_calls[0].id,
								name: delta.tool_calls[0].function.name,
								arguments: "",
							};

				// Response output item added event
				responseObject.output.push(newOutputObject);
				yield {
					type: "response.output_item.added",
					output_index: responseObject.output.length - 1,
					item: newOutputObject,
					sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
				};
			}

			// Current item is necessarily a tool call
			currentOutputItem = responseObject.output.at(-1) as ResponseOutputItem.McpCall | ResponseFunctionToolCall;
			currentOutputItem.arguments += delta.tool_calls[0].function.arguments;
			yield {
				type:
					currentOutputItem.type === "mcp_call"
						? "response.mcp_call.arguments_delta"
						: "response.function_call_arguments.delta",
				item_id: currentOutputItem.id as string,
				output_index: responseObject.output.length - 1,
				delta: delta.tool_calls[0].function.arguments,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
		}
	}

	const lastOutputItem = responseObject.output.at(-1);
	if (lastOutputItem) {
		if (lastOutputItem?.type === "message") {
			const contentPart = lastOutputItem.content.at(-1);
			if (contentPart?.type === "output_text") {
				yield {
					type: "response.output_text.done",
					item_id: lastOutputItem.id,
					output_index: responseObject.output.length - 1,
					content_index: lastOutputItem.content.length - 1,
					text: contentPart.text,
					sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
				};

				yield {
					type: "response.content_part.done",
					item_id: lastOutputItem.id,
					output_index: responseObject.output.length - 1,
					content_index: lastOutputItem.content.length - 1,
					part: contentPart,
					sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
				};
			} else {
				throw new StreamingError("Not implemented: only output_text is supported in streaming mode.");
			}

			// Response output item done event
			lastOutputItem.status = "completed";
			yield {
				type: "response.output_item.done",
				output_index: responseObject.output.length - 1,
				item: lastOutputItem,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
		} else if (lastOutputItem?.type === "function_call") {
			yield {
				type: "response.function_call_arguments.done",
				item_id: lastOutputItem.id as string,
				output_index: responseObject.output.length - 1,
				arguments: lastOutputItem.arguments,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};

			lastOutputItem.status = "completed";
			yield {
				type: "response.output_item.done",
				output_index: responseObject.output.length - 1,
				item: lastOutputItem,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
		} else if (lastOutputItem?.type === "mcp_call") {
			yield {
				type: "response.mcp_call.arguments_done",
				item_id: lastOutputItem.id as string,
				output_index: responseObject.output.length - 1,
				arguments: lastOutputItem.arguments,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
			yield {
				type: "response.output_item.done",
				output_index: responseObject.output.length - 1,
				item: lastOutputItem,
				sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
			};
		} else {
			throw new StreamingError(
				`Not implemented: expected message, function_call, or mcp_call, got ${lastOutputItem?.type}`
			);
		}
	}
}

/*
 * Perform an approved MCP tool call and stream the response.
 */
async function* callApprovedMCPToolStream(
	approval_request_id: string,
	approvalRequest: McpApprovalRequestParams | undefined,
	mcpToolsMapping: Record<string, McpServerParams>,
	responseObject: IncompleteResponse
): AsyncGenerator<ResponseStreamEvent> {
	if (!approvalRequest) {
		throw new Error(`MCP approval request '${approval_request_id}' not found`);
	}

	const outputObject: ResponseOutputItem.McpCall = {
		type: "mcp_call",
		id: generateUniqueId("mcp_call"),
		name: approvalRequest.name,
		server_label: approvalRequest.server_label,
		arguments: approvalRequest.arguments,
	};
	responseObject.output.push(outputObject);

	// Response output item added event
	yield {
		type: "response.output_item.added",
		output_index: responseObject.output.length - 1,
		item: outputObject,
		sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
	};

	yield {
		type: "response.mcp_call.in_progress",
		item_id: outputObject.id,
		output_index: responseObject.output.length - 1,
		sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
	};

	const toolParams = mcpToolsMapping[approvalRequest.name];
	const toolResult = await callMcpTool(toolParams, approvalRequest.name, approvalRequest.arguments);

	if (toolResult.error) {
		outputObject.error = toolResult.error;
		yield {
			type: "response.mcp_call.failed",
			sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
		};
		throw new Error(outputObject.error);
	}

	outputObject.output = toolResult.output;
	yield {
		type: "response.mcp_call.completed",
		sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
	};
	yield {
		type: "response.output_item.done",
		output_index: responseObject.output.length - 1,
		item: outputObject,
		sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
	};
}

async function* handleMCPToolCallsAfterLLM(
	responseObject: IncompleteResponse,
	mcpToolsMapping: Record<string, McpServerParams>
): AsyncGenerator<ResponseStreamEvent> {
	for (let output_index = 0; output_index < responseObject.output.length; output_index++) {
		const outputItem = responseObject.output[output_index];
		if (outputItem.type === "mcp_call") {
			const toolCall = outputItem as ResponseOutputItem.McpCall;
			const toolParams = mcpToolsMapping[toolCall.name];
			if (toolParams) {
				const approvalRequired =
					toolParams.require_approval === "always"
						? true
						: toolParams.require_approval === "never"
							? false
							: toolParams.require_approval.always?.tool_names?.includes(toolCall.name)
								? true
								: toolParams.require_approval.never?.tool_names?.includes(toolCall.name)
									? false
									: true; // behavior is undefined in specs, let's default to

				if (approvalRequired) {
					const approvalRequest: ResponseOutputItem.McpApprovalRequest = {
						type: "mcp_approval_request",
						id: generateUniqueId("mcp_approval_request"),
						name: toolCall.name,
						server_label: toolParams.server_label,
						arguments: toolCall.arguments,
					};
					responseObject.output.push(approvalRequest);
					yield {
						type: "response.output_item.added",
						output_index: responseObject.output.length,
						item: approvalRequest,
						sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
					};
				} else {
					responseObject.output.push;
					yield {
						type: "response.mcp_call.in_progress",
						item_id: toolCall.id,
						sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
						output_index,
					};
					const toolResult = await callMcpTool(toolParams, toolCall.name, toolCall.arguments);
					if (toolResult.error) {
						toolCall.error = toolResult.error;
						yield {
							type: "response.mcp_call.failed",
							sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
						};
						throw new Error(toolCall.error);
					} else {
						toolCall.output = toolResult.output;
						yield {
							type: "response.mcp_call.completed",
							sequence_number: SEQUENCE_NUMBER_PLACEHOLDER,
						};
					}
				}
			}
		}
	}
}