import { i as streamSimple } from "./stream-BJE9dn5k.js";
import "./llm-Dxxf_-6H.js";
import { b as createToolStreamWrapper, s as createPlainTextToolCallCompatWrapper, t as composeProviderStreamWrappers } from "./provider-stream-shared-CXQ8xXG4.js";
//#region extensions/xai/stream.ts
const XAI_FAST_MODEL_IDS = new Map([
	["grok-3", "grok-3-fast"],
	["grok-3-mini", "grok-3-mini-fast"],
	["grok-4", "grok-4-fast"],
	["grok-4-0709", "grok-4-fast"]
]);
function resolveXaiFastModelId(modelId) {
	if (typeof modelId !== "string") return;
	return XAI_FAST_MODEL_IDS.get(modelId.trim());
}
function stripUnsupportedStrictFlag(tool) {
	if (!tool || typeof tool !== "object") return tool;
	const toolObj = tool;
	const fn = toolObj.function;
	if (!fn || typeof fn !== "object") return tool;
	const fnObj = fn;
	if (typeof fnObj.strict !== "boolean") return tool;
	const nextFunction = { ...fnObj };
	delete nextFunction.strict;
	return {
		...toolObj,
		function: nextFunction
	};
}
function supportsExplicitImageInput(model) {
	return Array.isArray(model.input) && model.input.includes("image");
}
function supportsReasoningControls(model) {
	const compat = model.compat && typeof model.compat === "object" ? model.compat : void 0;
	return model.reasoning === true && compat?.supportsReasoningEffort !== false;
}
const TOOL_RESULT_IMAGE_REPLAY_TEXT = "Attached image(s) from tool result:";
const HTML_ENTITY_RE = /&(?:amp|lt|gt|quot|apos|#39|#x[0-9a-f]+|#\d+);/i;
const NAMED_HTML_ENTITIES = new Map([
	["amp", "&"],
	["apos", "'"],
	["gt", ">"],
	["lt", "<"],
	["quot", "\""]
]);
function decodeHtmlEntities(value) {
	return value.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos|#39);/gi, (match, entity) => {
		const normalized = String(entity).toLowerCase();
		if (normalized === "#39") return "'";
		if (normalized.startsWith("#x")) return String.fromCodePoint(Number.parseInt(normalized.slice(2), 16));
		if (normalized.startsWith("#")) return String.fromCodePoint(Number.parseInt(normalized.slice(1), 10));
		return NAMED_HTML_ENTITIES.get(normalized) ?? match;
	});
}
function decodeHtmlEntitiesInObject(value) {
	switch (typeof value) {
		case "string": return HTML_ENTITY_RE.test(value) ? decodeHtmlEntities(value) : value;
		case "object":
			if (!value) return value;
			if (Array.isArray(value)) return value.map((entry) => decodeHtmlEntitiesInObject(entry));
			return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, decodeHtmlEntitiesInObject(entry)]));
		default: return value;
	}
}
function visitContentBlocks(value, visitor) {
	if (Array.isArray(value)) {
		for (const entry of value) visitContentBlocks(entry, visitor);
		return;
	}
	if (!value || typeof value !== "object") return;
	const block = value;
	visitor(block);
	if ("content" in block) visitContentBlocks(block.content, visitor);
}
function decodeToolCallArgumentsHtmlEntitiesInMessage(message) {
	visitContentBlocks(message, (block) => {
		if (block.type !== "toolCall" || !block.arguments || typeof block.arguments !== "object") return;
		block.arguments = decodeHtmlEntitiesInObject(block.arguments);
	});
}
function isReplayableInputImagePart(part) {
	if (part.type !== "input_image") return false;
	if (typeof part.image_url === "string") return true;
	if (!part.source || typeof part.source !== "object") return false;
	const source = part.source;
	if (source.type === "url") return typeof source.url === "string";
	return source.type === "base64" && typeof source.media_type === "string" && typeof source.data === "string";
}
function normalizeXaiResponsesFunctionCallOutput(item, includeImages) {
	if (!item || typeof item !== "object") return {
		normalizedItem: item,
		imageParts: []
	};
	const itemObj = item;
	if (itemObj.type !== "function_call_output" || !Array.isArray(itemObj.output)) return {
		normalizedItem: itemObj,
		imageParts: []
	};
	const outputParts = itemObj.output;
	const textOutput = outputParts.filter((part) => part.type === "input_text" && typeof part.text === "string").map((part) => part.text).join("");
	const imageParts = includeImages ? outputParts.filter((part) => isReplayableInputImagePart(part)) : [];
	const hadNonTextParts = outputParts.some((part) => part.type !== "input_text");
	return {
		normalizedItem: {
			...itemObj,
			output: textOutput || (hadNonTextParts ? "(see attached image)" : "")
		},
		imageParts
	};
}
function normalizeXaiResponsesToolResultPayload(payloadObj, model) {
	if (model.api !== "openai-responses" || !Array.isArray(payloadObj.input)) return;
	const includeImages = supportsExplicitImageInput(model);
	const normalizedInput = [];
	const collectedImageParts = [];
	for (const item of payloadObj.input) {
		const normalized = normalizeXaiResponsesFunctionCallOutput(item, includeImages);
		normalizedInput.push(normalized.normalizedItem);
		collectedImageParts.push(...normalized.imageParts);
	}
	if (collectedImageParts.length > 0) normalizedInput.push({
		type: "message",
		role: "user",
		content: [{
			type: "input_text",
			text: TOOL_RESULT_IMAGE_REPLAY_TEXT
		}, ...collectedImageParts]
	});
	payloadObj.input = normalizedInput;
}
function createXaiToolPayloadCompatibilityWrapper(baseStreamFn) {
	const underlying = baseStreamFn ?? streamSimple;
	return (model, context, options) => {
		const originalOnPayload = options?.onPayload;
		return underlying(model, context, {
			...options,
			onPayload: (payload) => {
				if (payload && typeof payload === "object") {
					const payloadObj = payload;
					if (Array.isArray(payloadObj.tools)) payloadObj.tools = payloadObj.tools.map((tool) => stripUnsupportedStrictFlag(tool));
					normalizeXaiResponsesToolResultPayload(payloadObj, model);
					if (!supportsReasoningControls(model)) {
						delete payloadObj.reasoning;
						delete payloadObj.reasoningEffort;
						delete payloadObj.reasoning_effort;
					}
				}
				return originalOnPayload?.(payload, model);
			}
		});
	};
}
function createXaiFastModeWrapper(baseStreamFn, fastMode) {
	const underlying = baseStreamFn ?? streamSimple;
	return (model, context, options) => {
		const supportsFastAliasTransport = model.api === "openai-completions" || model.api === "openai-responses";
		if (!fastMode || !supportsFastAliasTransport || model.provider !== "xai") return underlying(model, context, options);
		const fastModelId = resolveXaiFastModelId(model.id);
		if (!fastModelId) return underlying(model, context, options);
		return underlying({
			...model,
			id: fastModelId
		}, context, options);
	};
}
function transformXaiStreamEvent(value, transformMessage) {
	if (!value || typeof value !== "object") return;
	const event = value;
	transformMessage(event.partial);
	transformMessage(event.message);
}
function wrapStreamMessageObjects(stream, transformMessage) {
	const originalResult = stream.result.bind(stream);
	stream.result = async () => {
		const message = await originalResult();
		transformMessage(message);
		return message;
	};
	const originalAsyncIterator = stream[Symbol.asyncIterator].bind(stream);
	stream[Symbol.asyncIterator] = function() {
		const iterator = originalAsyncIterator();
		return {
			async next() {
				const result = await iterator.next();
				if (!result.done) transformXaiStreamEvent(result.value, transformMessage);
				return result;
			},
			async return(value) {
				return iterator.return?.(value) ?? {
					done: true,
					value: void 0
				};
			},
			async throw(error) {
				return iterator.throw?.(error) ?? {
					done: true,
					value: void 0
				};
			}
		};
	};
	return stream;
}
function createXaiToolCallArgumentDecodingWrapper(baseStreamFn) {
	const underlying = baseStreamFn ?? streamSimple;
	return (model, context, options) => {
		const maybeStream = underlying(model, context, options);
		if (maybeStream && typeof maybeStream === "object" && "then" in maybeStream) return Promise.resolve(maybeStream).then((stream) => wrapStreamMessageObjects(stream, decodeToolCallArgumentsHtmlEntitiesInMessage));
		return wrapStreamMessageObjects(maybeStream, decodeToolCallArgumentsHtmlEntitiesInMessage);
	};
}
function wrapXaiProviderStream(ctx) {
	const extraParams = ctx.extraParams;
	const fastMode = extraParams?.fastMode;
	const toolStreamEnabled = extraParams?.tool_stream !== false;
	return composeProviderStreamWrappers(ctx.streamFn, (streamFn) => {
		let wrappedStreamFn = createXaiToolPayloadCompatibilityWrapper(streamFn);
		if (typeof fastMode === "boolean") wrappedStreamFn = createXaiFastModeWrapper(wrappedStreamFn, fastMode);
		wrappedStreamFn = createXaiToolCallArgumentDecodingWrapper(wrappedStreamFn);
		wrappedStreamFn = createPlainTextToolCallCompatWrapper(wrappedStreamFn);
		return createToolStreamWrapper(wrappedStreamFn, toolStreamEnabled);
	});
}
//#endregion
export { createXaiToolPayloadCompatibilityWrapper as n, wrapXaiProviderStream as r, createXaiFastModeWrapper as t };
