mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
feat: AI SDK compatibility - Accept header & JSON markdown stripping
- Respect Accept: application/json header to return non-streaming JSON instead of SSE, fixing AI SDK generateObject/generateText compatibility - Strip markdown code block markers (```json...```) from Claude non-streaming responses to prevent JSON parse errors Cherry-picked and adapted from PR #290 by @rothnic https://github.com/decolua/9router/pull/290 Made-with: Cursor
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user