diff --git a/artifacts/api-server/src/routes/proxy.ts b/artifacts/api-server/src/routes/proxy.ts index 77e48c7..1f91771 100644 --- a/artifacts/api-server/src/routes/proxy.ts +++ b/artifacts/api-server/src/routes/proxy.ts @@ -72,9 +72,26 @@ interface ChatCompletionRequest { frequency_penalty?: number; presence_penalty?: number; user?: string; + reasoning_effort?: "minimal" | "low" | "medium" | "high"; + thinking?: { type?: string; budget_tokens?: number } | null; [key: string]: unknown; } +function effortToBudget(effort: string | undefined): number | null { + switch (effort) { + case "minimal": + return 1024; + case "low": + return 2048; + case "medium": + return 8000; + case "high": + return 16000; + default: + return null; + } +} + function isOpenAIModel(model: string): boolean { return /^(gpt|o\d|chatgpt)/i.test(model); } @@ -240,19 +257,41 @@ async function handleAnthropic( stream: boolean, ): Promise { const { system, messages } = convertMessagesForAnthropic(body.messages); - const maxTokens = + let maxTokens = body.max_completion_tokens ?? body.max_tokens ?? 8192; + let thinkingParam: { type: "enabled"; budget_tokens: number } | null = null; + if (body.thinking && (body.thinking.type === "enabled" || typeof body.thinking.budget_tokens === "number")) { + const budget = Math.max(1024, body.thinking.budget_tokens ?? 4096); + thinkingParam = { type: "enabled", budget_tokens: budget }; + } else { + const budget = effortToBudget(body.reasoning_effort); + if (budget !== null) { + thinkingParam = { type: "enabled", budget_tokens: budget }; + } + } + + if (thinkingParam) { + if (maxTokens <= thinkingParam.budget_tokens) { + maxTokens = thinkingParam.budget_tokens + 4096; + } + } + const baseParams: Record = { model: body.model, max_tokens: maxTokens, messages, }; if (system) baseParams["system"] = system; - if (typeof body.temperature === "number") baseParams["temperature"] = body.temperature; - if (typeof body.top_p === "number") baseParams["top_p"] = body.top_p; + if (thinkingParam) { + baseParams["thinking"] = thinkingParam; + // Anthropic requires temperature=1 (or unset) and rejects top_p when thinking is enabled. + } else { + if (typeof body.temperature === "number") baseParams["temperature"] = body.temperature; + if (typeof body.top_p === "number") baseParams["top_p"] = body.top_p; + } if (body.stop !== undefined) { baseParams["stop_sequences"] = Array.isArray(body.stop) ? body.stop : [body.stop]; } @@ -262,10 +301,15 @@ async function handleAnthropic( const message = await anthropic.messages.create( baseParams as Parameters[0], ); - const text = (message.content ?? []) - .map((b) => (b.type === "text" ? b.text : "")) - .join(""); + let text = ""; + let reasoning = ""; + for (const b of message.content ?? []) { + if (b.type === "text") text += b.text; + else if (b.type === "thinking") reasoning += (b as { thinking?: string }).thinking ?? ""; + } const finishReason = mapAnthropicStopReason(message.stop_reason ?? null); + const assistantMessage: Record = { role: "assistant", content: text }; + if (reasoning) assistantMessage["reasoning_content"] = reasoning; res.json({ id: message.id, object: "chat.completion", @@ -274,7 +318,7 @@ async function handleAnthropic( choices: [ { index: 0, - message: { role: "assistant", content: text }, + message: assistantMessage, finish_reason: finishReason, }, ], @@ -323,29 +367,29 @@ async function handleAnthropic( for await (const event of upstream) { if (clientGone) break; - if ( - event.type === "content_block_delta" && - event.delta.type === "text_delta" - ) { - const chunk = { - id, - object: "chat.completion.chunk", - created, - model: body.model, - choices: [ - { - index: 0, - delta: { content: event.delta.text }, - finish_reason: null, - }, - ], - }; - res.write(`data: ${JSON.stringify(chunk)}\n\n`); - res.flush?.(); + if (event.type === "content_block_delta") { + let delta: Record | null = null; + if (event.delta.type === "text_delta") { + delta = { content: event.delta.text }; + } else if (event.delta.type === "thinking_delta") { + const td = event.delta as unknown as { thinking?: string }; + if (td.thinking) delta = { reasoning_content: td.thinking }; + } + if (delta) { + const chunk = { + id, + object: "chat.completion.chunk", + created, + model: body.model, + choices: [{ index: 0, delta, finish_reason: null }], + }; + res.write(`data: ${JSON.stringify(chunk)}\n\n`); + res.flush?.(); + } } else if (event.type === "message_delta") { - const delta = event as unknown as { delta?: { stop_reason?: string | null } }; - if (delta.delta?.stop_reason) { - stopReason = delta.delta.stop_reason; + const md = event as unknown as { delta?: { stop_reason?: string | null } }; + if (md.delta?.stop_reason) { + stopReason = md.delta.stop_reason; } } }