diff --git a/app/api/common.ts b/app/api/common.ts index b7e41fa26..c37ebfe69 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -90,6 +90,14 @@ export async function requestOpenai(req: NextRequest) { const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`); console.log("fetchUrl", fetchUrl); + + let payload = await req.text(); + if (baseUrl.includes("openrouter.ai")) { + const body = JSON.parse(payload); + body["include_reasoning"] = true; + payload = JSON.stringify(body); + } + const fetchOptions: RequestInit = { headers: { "Content-Type": "application/json", @@ -100,7 +108,7 @@ export async function requestOpenai(req: NextRequest) { }), }, method: req.method, - body: req.body, + body: payload, // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body redirect: "manual", // @ts-ignore @@ -111,10 +119,7 @@ export async function requestOpenai(req: NextRequest) { // #1815 try to refuse gpt4 request if (serverConfig.customModels && req.body) { try { - const clonedBody = await req.text(); - fetchOptions.body = clonedBody; - - const jsonBody = JSON.parse(clonedBody) as { model?: string }; + const jsonBody = JSON.parse(payload) as { model?: string }; // not undefined and is false if ( diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 0de723515..abdbb82d4 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -296,8 +296,7 @@ export class ChatGPTApi implements LLMApi { // console.log("getAsTools", tools, funcs); // Add "include_reasoning" for OpenRouter: https://openrouter.ai/announcements/reasoning-tokens-for-thinking-models - const isOpenRouter = chatPath.includes("openrouter.ai"); - if (isOpenRouter) { + if (chatPath.includes("openrouter.ai")) { // @ts-ignore requestPayload["include_reasoning"] = true; } @@ -344,9 +343,9 @@ export class ChatGPTApi implements LLMApi { } } - const reasoning = isOpenRouter - ? choices[0]?.delta?.reasoning - : choices[0]?.delta?.reasoning_content; + const reasoning = + choices[0]?.delta?.reasoning_content || + choices[0]?.delta?.reasoning; const content = choices[0]?.delta?.content; // Skip if both content and reasoning_content are empty or null