diff --git a/cc-haha/src/server/proxy/handler.ts b/cc-haha/src/server/proxy/handler.ts index 36bf7ba..dc3dbe7 100644 --- a/cc-haha/src/server/proxy/handler.ts +++ b/cc-haha/src/server/proxy/handler.ts @@ -18,6 +18,17 @@ import { openaiChatStreamToAnthropic } from './streaming/openaiChatStreamToAnthr import { openaiResponsesStreamToAnthropic } from './streaming/openaiResponsesStreamToAnthropic.js' import type { AnthropicRequest } from './transform/types.js' +/** + * Patch thinking parameter for newer Claude models (4.x / Opus 4.7+). + * These models require `thinking.type = "adaptive"` instead of `"enabled"`. + */ +function patchThinkingForModel(body: AnthropicRequest): void { + if (!body.thinking) return + if (body.thinking.type === 'enabled') { + body.thinking.type = 'adaptive' + } +} + const providerService = new ProviderService() export async function handleProxyRequest(req: Request, url: URL): Promise { @@ -53,21 +64,6 @@ export async function handleProxyRequest(req: Request, url: URL): Promise { + const url = `${baseUrl}/v1/messages` + + const headers: Record = { + 'Content-Type': 'application/json', + 'x-api-key': apiKey, + 'anthropic-version': originalReq.headers.get('anthropic-version') || '2023-06-01', + } + const authHeader = originalReq.headers.get('authorization') + if (authHeader) { + headers['Authorization'] = authHeader + } + + const upstream = await fetch(url, { + method: 'POST', + headers, + body: JSON.stringify(body), + signal: AbortSignal.timeout(600_000), + }) + + if (isStream && upstream.ok && upstream.body) { + return new Response(upstream.body, { + status: upstream.status, + headers: { + 'Content-Type': upstream.headers.get('Content-Type') || 'text/event-stream', + 'Cache-Control': 'no-cache', + Connection: 'keep-alive', + }, + }) + } + + const text = await upstream.text() + return new Response(text, { + status: upstream.status, + headers: { 'Content-Type': 'application/json; charset=utf-8' }, + }) +} + async function handleOpenaiChat( body: AnthropicRequest, baseUrl: string, diff --git a/cc-haha/src/server/services/providerService.ts b/cc-haha/src/server/services/providerService.ts index e72208a..815f787 100644 --- a/cc-haha/src/server/services/providerService.ts +++ b/cc-haha/src/server/services/providerService.ts @@ -247,7 +247,7 @@ export class ProviderService { provider: SavedProvider, options?: { proxyPath?: string }, ): Record { - const needsProxy = provider.apiFormat != null && provider.apiFormat !== 'anthropic' + const needsProxy = provider.apiFormat != null const proxyPath = options?.proxyPath ?? '/proxy' const baseUrl = needsProxy ? `http://127.0.0.1:${ProviderService.serverPort}${proxyPath}`