const require_responses = require("../converters/responses.cjs");
let _langchain_core_language_models_compat = require("@langchain/core/language_models/compat");
//#region src/utils/responses_stream_events.ts
async function* convertOpenAIResponsesStream(source, options = {}) {
	const shouldStreamUsage = options.streamUsage ?? true;
	const provider = options.provider ?? "openai";
	const blockAccumulators = /* @__PURE__ */ new Map();
	const blockKeyToIndex = /* @__PURE__ */ new Map();
	let nextBlockIndex = 0;
	let messageStarted = false;
	let messageId;
	let usageSnapshot;
	let finishReason;
	let responseMetadata;
	const finalizedBlockIndices = /* @__PURE__ */ new Set();
	const getOrCreateBlockIndex = (key, initial) => {
		const existing = blockKeyToIndex.get(key);
		if (existing !== void 0) return {
			index: existing,
			isNew: false
		};
		const index = nextBlockIndex++;
		blockKeyToIndex.set(key, index);
		blockAccumulators.set(index, { ...initial });
		return {
			index,
			isNew: true
		};
	};
	const ensureMessageStart = function* () {
		if (!messageStarted) {
			messageStarted = true;
			yield {
				event: "message-start",
				id: messageId
			};
		}
	};
	const finalizeBlock = function* (index) {
		if (finalizedBlockIndices.has(index)) return;
		const acc = blockAccumulators.get(index);
		if (!acc) return;
		finalizedBlockIndices.add(index);
		yield {
			event: "content-block-finish",
			index,
			content: (0, _langchain_core_language_models_compat.finalizeContentBlock)(acc)
		};
	};
	for await (const event of source) {
		if (event.type === "response.created") {
			messageId = event.response.id;
			yield* ensureMessageStart();
			yield {
				event: "provider",
				provider,
				name: "response.created",
				payload: {
					model: event.response.model,
					id: event.response.id
				}
			};
			continue;
		}
		if (event.type === "response.output_text.delta") {
			yield* ensureMessageStart();
			const { index, isNew } = getOrCreateBlockIndex(`text:${event.output_index}:${event.content_index}`, {
				type: "text",
				text: ""
			});
			if (isNew) yield {
				event: "content-block-start",
				index,
				content: {
					type: "text",
					text: ""
				}
			};
			const acc = blockAccumulators.get(index);
			acc.text = (acc.text ?? "") + event.delta;
			yield {
				event: "content-block-delta",
				index,
				delta: {
					type: "text-delta",
					text: event.delta
				}
			};
			continue;
		}
		if (event.type === "response.reasoning_summary_text.delta") {
			yield* ensureMessageStart();
			const { index, isNew } = getOrCreateBlockIndex(`reasoning:${event.output_index}:${event.summary_index}`, {
				type: "reasoning",
				reasoning: ""
			});
			if (isNew) yield {
				event: "content-block-start",
				index,
				content: {
					type: "reasoning",
					reasoning: ""
				}
			};
			const acc = blockAccumulators.get(index);
			acc.reasoning = (acc.reasoning ?? "") + event.delta;
			yield {
				event: "content-block-delta",
				index,
				delta: {
					type: "reasoning-delta",
					reasoning: event.delta
				}
			};
			continue;
		}
		if (event.type === "response.output_item.added" && (event.item.type === "function_call" || event.item.type === "custom_tool_call")) {
			yield* ensureMessageStart();
			const key = `tool:${event.output_index}`;
			const isCustom = event.item.type === "custom_tool_call";
			const initialArgs = event.item.type === "function_call" ? event.item.arguments ?? "" : event.item.input ?? "";
			const { index, isNew } = getOrCreateBlockIndex(key, {
				type: "tool_call_chunk",
				id: event.item.call_id,
				name: event.item.name,
				args: initialArgs,
				index: event.output_index,
				...isCustom ? { isCustomTool: true } : {}
			});
			if (isNew) yield {
				event: "content-block-start",
				index,
				content: {
					type: "tool_call_chunk",
					id: event.item.call_id,
					name: event.item.name,
					args: initialArgs,
					index: event.output_index
				}
			};
			if (initialArgs) {
				const acc = blockAccumulators.get(index);
				yield {
					event: "content-block-delta",
					index,
					delta: {
						type: "block-delta",
						fields: {
							type: "tool_call_chunk",
							...acc.id != null ? { id: acc.id } : {},
							...acc.name != null ? { name: acc.name } : {},
							args: acc.args
						}
					}
				};
			}
			continue;
		}
		if (event.type === "response.function_call_arguments.delta" || event.type === "response.custom_tool_call_input.delta") {
			yield* ensureMessageStart();
			const { index, isNew } = getOrCreateBlockIndex(`tool:${event.output_index}`, {
				type: "tool_call_chunk",
				args: "",
				index: event.output_index,
				...event.type === "response.custom_tool_call_input.delta" ? { isCustomTool: true } : {}
			});
			if (isNew) yield {
				event: "content-block-start",
				index,
				content: {
					type: "tool_call_chunk",
					args: "",
					index: event.output_index
				}
			};
			const acc = blockAccumulators.get(index);
			acc.args = (acc.args ?? "") + event.delta;
			yield {
				event: "content-block-delta",
				index,
				delta: {
					type: "block-delta",
					fields: {
						type: "tool_call_chunk",
						...acc.id != null ? { id: acc.id } : {},
						...acc.name != null ? { name: acc.name } : {},
						args: acc.args
					}
				}
			};
			continue;
		}
		if (event.type === "response.output_item.done" && (event.item.type === "function_call" || event.item.type === "custom_tool_call")) {
			yield* ensureMessageStart();
			const key = `tool:${event.output_index}`;
			const args = event.item.type === "function_call" ? event.item.arguments ?? "" : event.item.input ?? "";
			const { index, isNew } = getOrCreateBlockIndex(key, {
				type: "tool_call_chunk",
				id: event.item.call_id,
				name: event.item.name,
				args,
				index: event.output_index
			});
			if (isNew) yield {
				event: "content-block-start",
				index,
				content: {
					type: "tool_call_chunk",
					id: event.item.call_id,
					name: event.item.name,
					args,
					index: event.output_index
				}
			};
			else {
				const acc = blockAccumulators.get(index);
				acc.args = args;
				acc.id = event.item.call_id;
				acc.name = event.item.name;
			}
			yield* finalizeBlock(index);
			continue;
		}
		if (event.type === "response.completed" || event.type === "response.incomplete") {
			yield* ensureMessageStart();
			messageId = event.response.id;
			finishReason = mapResponseStatusToFinishReason(event.response.status, event.type);
			responseMetadata = {
				model_provider: provider,
				id: event.response.id,
				model: event.response.model,
				status: event.response.status
			};
			if (shouldStreamUsage && event.response.usage) {
				usageSnapshot = require_responses.convertResponsesUsageToUsageMetadata(event.response.usage);
				yield {
					event: "usage",
					usage: usageSnapshot
				};
			}
			continue;
		}
		if (event.type === "response.image_generation_call.partial_image") continue;
		yield* ensureMessageStart();
		yield {
			event: "provider",
			provider,
			name: event.type,
			payload: event
		};
	}
	if (!messageStarted) yield { event: "message-start" };
	for (const [index] of blockAccumulators) if (!finalizedBlockIndices.has(index)) yield* finalizeBlock(index);
	yield {
		event: "message-finish",
		reason: finishReason,
		...usageSnapshot ? { usage: usageSnapshot } : {},
		...responseMetadata ? { responseMetadata } : {}
	};
}
function mapResponseStatusToFinishReason(status, eventType) {
	if (eventType === "response.incomplete") return "length";
	if (status === "completed") return "stop";
	if (status === "incomplete") return "length";
	return "stop";
}
//#endregion
exports.convertOpenAIResponsesStream = convertOpenAIResponsesStream;

//# sourceMappingURL=responses_stream_events.cjs.map