diff --git a/open-sse/handlers/chatCore.js b/open-sse/handlers/chatCore.js index 16f01c03..af62dbae 100644 --- a/open-sse/handlers/chatCore.js +++ b/open-sse/handlers/chatCore.js @@ -39,7 +39,16 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred const clientRequestedStreaming = body.stream === true || sourceFormat === FORMATS.ANTIGRAVITY || sourceFormat === FORMATS.GEMINI || sourceFormat === FORMATS.GEMINI_CLI; const providerRequiresStreaming = provider === "openai" || provider === "codex"; - const stream = providerRequiresStreaming ? true : (body.stream !== false); + let stream = providerRequiresStreaming ? true : (body.stream !== false); + + // Check client Accept header preference for non-streaming requests + // This fixes AI SDK compatibility where clients send Accept: application/json + const acceptHeader = clientRawRequest?.headers?.accept || ""; + const clientPrefersJson = acceptHeader.includes("application/json"); + const clientPrefersSSE = acceptHeader.includes("text/event-stream"); + if (clientPrefersJson && !clientPrefersSSE && body.stream !== true) { + stream = false; + } const reqLogger = await createRequestLogger(sourceFormat, targetFormat, model); if (clientRawRequest) reqLogger.logClientRawRequest(clientRawRequest.endpoint, clientRawRequest.body, clientRawRequest.headers); diff --git a/open-sse/handlers/chatCore/nonStreamingHandler.js b/open-sse/handlers/chatCore/nonStreamingHandler.js index 62efc03d..4d202354 100644 --- a/open-sse/handlers/chatCore/nonStreamingHandler.js +++ b/open-sse/handlers/chatCore/nonStreamingHandler.js @@ -77,8 +77,11 @@ export function translateNonStreamingResponse(responseBody, targetFormat, source const toolCalls = []; for (const block of responseBody.content) { - if (block.type === "text") textContent += block.text; - else if (block.type === "thinking") thinkingContent += block.thinking || ""; + if (block.type === "text") { + // Strip markdown code block markers (e.g. kimi wraps JSON in ```json...```) + const text = block.text.replace(/^\s*```\s*json\s*\n?/i, "").replace(/\n?\s*```\s*$/i, ""); + textContent += text; + } else if (block.type === "thinking") thinkingContent += block.thinking || ""; else if (block.type === "tool_use") { toolCalls.push({ id: block.id, type: "function", function: { name: block.name, arguments: JSON.stringify(block.input || {}) } }); }