import { h as isOpenAICompatibleThinkingEnabled, o as createPayloadPatchStreamWrapper } from "./provider-stream-shared-CXQ8xXG4.js";
import { u as normalizeProviderId } from "./provider-model-shared-BWWtjgcB.js";
//#region extensions/qwen/stream.ts
function isQwenProviderId(providerId) {
	const normalized = normalizeProviderId(providerId);
	return normalized === "qwen" || normalized === "qwen-oauth" || normalized === "qwen-portal" || normalized === "qwen-cli" || normalized === "modelstudio" || normalized === "qwencloud" || normalized === "dashscope";
}
function isQwenOAuthProviderId(providerId) {
	const normalized = normalizeProviderId(providerId);
	return normalized === "qwen-oauth" || normalized === "qwen-portal" || normalized === "qwen-cli";
}
function normalizeQwenOAuthContent(content) {
	if (typeof content === "string") return [{
		type: "text",
		text: content
	}];
	if (!Array.isArray(content)) return content;
	const normalized = content.map((part) => {
		if (typeof part === "string") return {
			type: "text",
			text: part
		};
		return part && typeof part === "object" ? part : void 0;
	}).filter((part) => Boolean(part));
	return normalized.length > 0 ? normalized : content;
}
function patchQwenOAuthPayload(payload) {
	const messages = payload.messages;
	if (!Array.isArray(messages)) return;
	for (const message of messages) {
		if (!message || typeof message !== "object") continue;
		const record = message;
		record.content = normalizeQwenOAuthContent(record.content);
		if (record.role !== "system" || !Array.isArray(record.content) || record.content.length === 0) continue;
		const last = record.content[record.content.length - 1];
		if (last && typeof last === "object") last.cache_control = { type: "ephemeral" };
	}
	payload.vl_high_resolution_images = true;
}
function setQwenChatTemplateThinking(payload, enabled) {
	const existing = payload.chat_template_kwargs;
	if (existing && typeof existing === "object" && !Array.isArray(existing)) {
		const next = {
			...existing,
			enable_thinking: enabled
		};
		if (!Object.hasOwn(next, "preserve_thinking")) next.preserve_thinking = true;
		payload.chat_template_kwargs = next;
		return;
	}
	payload.chat_template_kwargs = {
		enable_thinking: enabled,
		preserve_thinking: true
	};
}
function readQwenThinkingFormatFromModel(model) {
	if (model.api !== "openai-completions") return;
	const compat = model.compat && typeof model.compat === "object" ? model.compat : void 0;
	return typeof compat?.thinkingFormat === "string" ? compat.thinkingFormat : void 0;
}
function createQwenThinkingWrapper(baseStreamFn, thinkingLevel, thinkingFormat) {
	return createPayloadPatchStreamWrapper(baseStreamFn, ({ payload: payloadObj, model, options }) => {
		const enableThinking = isOpenAICompatibleThinkingEnabled({
			thinkingLevel,
			options
		});
		if ((thinkingFormat ?? readQwenThinkingFormatFromModel(model)) === "qwen-chat-template") {
			setQwenChatTemplateThinking(payloadObj, enableThinking);
			delete payloadObj.enable_thinking;
		} else payloadObj.enable_thinking = enableThinking;
		delete payloadObj.reasoning_effort;
		delete payloadObj.reasoningEffort;
		delete payloadObj.reasoning;
	}, { shouldPatch: ({ model }) => model.api === "openai-completions" && model.reasoning });
}
function wrapQwenProviderStream(ctx) {
	if (!isQwenProviderId(ctx.provider) || ctx.model && ctx.model.api !== "openai-completions") return;
	const streamFn = createQwenThinkingWrapper(ctx.streamFn, ctx.thinkingLevel, ctx.model ? readQwenThinkingFormatFromModel(ctx.model) : void 0);
	if (!isQwenOAuthProviderId(ctx.provider)) return streamFn;
	return createPayloadPatchStreamWrapper(streamFn, ({ payload, model }) => {
		if (model.api === "openai-completions") patchQwenOAuthPayload(payload);
	});
}
//#endregion
export { wrapQwenProviderStream as n, createQwenThinkingWrapper as t };
