|
1 | 1 | /** |
2 | 2 | * Main proxy handler — routes requests, injects auth, forwards, and streams responses. |
3 | 3 | * |
4 | | - * **Translation mode**: |
5 | | - * - coding-plan and start-plan both use an OpenAI-compatible upstream. |
6 | | - * - Anthropic clients are translated Anthropic → OpenAI upstream → Anthropic |
7 | | - * response. OpenAI clients are already in the upstream's native format and |
8 | | - * pass through. |
| 4 | + * **v2.6 upstream reality (post-PR #34)**: BOTH plan tiers post an |
| 5 | + * Anthropic-format upstream — coding-plan mirrors the real ZCode client |
| 6 | + * (api.z.ai/api/anthropic → ultra via endpoint routing); start-plan posts to |
| 7 | + * zcode.z.ai's Anthropic gateway with the plan JWT. Consequently: |
| 8 | + * - OpenAI clients are translated OpenAI→Anthropic on the way up and |
| 9 | + * Anthropic→OpenAI on the way down ("translation" mode). |
| 10 | + * - Anthropic clients speak the upstream's native format — requests are |
| 11 | + * forwarded with body transforms only ("passthrough" mode, |
| 12 | + * `decompress: false`). |
9 | 13 | * |
10 | 14 | * @see .omo/plans/zcode-proxy.md Task 6 |
11 | 15 | */ |
@@ -40,6 +44,7 @@ import { translateRequestAnthropicToOpenAI, translateResponseOpenAIToAnthropic } |
40 | 44 | import { anthropicSseToOpenaiSse, openaiSseToAnthropicSse } from "../translator/sse-translator.js"; |
41 | 45 | import type { OpenAIChatRequest, OpenAIChatResponse, AnthropicMessagesRequest, AnthropicMessagesResponse } from "../translator/types.js"; |
42 | 46 | import { dumpPhase, dumpHeaders, dumpBody, dumpEnabled } from "./dump.js"; |
| 47 | +import { inflateWithCap } from "./inflate.js"; |
43 | 48 |
|
44 | 49 | /** Options for the proxy handler. */ |
45 | 50 | export interface ProxyHandlerOptions { |
@@ -508,48 +513,28 @@ export class InflatedBodyTooLargeError extends Error { |
508 | 513 | } |
509 | 514 |
|
510 | 515 | async function inflateGzipBody(bytes: Uint8Array): Promise<Uint8Array> { |
511 | | - const gunzip = new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>; |
512 | | - const source = new ReadableStream<Uint8Array>({ |
513 | | - start(controller) { |
514 | | - controller.enqueue(bytes); |
515 | | - controller.close(); |
516 | | - }, |
517 | | - }); |
518 | | - const reader = source.pipeThrough(gunzip).getReader(); |
519 | | - const parts: Uint8Array[] = []; |
520 | | - let total = 0; |
521 | | - try { |
522 | | - for (;;) { |
523 | | - const { done, value } = await reader.read(); |
524 | | - if (done) break; |
525 | | - total += value.byteLength; |
526 | | - if (total > MAX_INFLATED_BODY_BYTES) { |
527 | | - await reader.cancel().catch(() => {}); |
528 | | - throw new InflatedBodyTooLargeError(MAX_INFLATED_BODY_BYTES); |
529 | | - } |
530 | | - parts.push(value); |
531 | | - } |
532 | | - } catch (err) { |
533 | | - if (err instanceof InflatedBodyTooLargeError) throw err; |
534 | | - throw new Error(`request body is marked content-encoding: gzip but failed to decompress: ${(err as Error).message}`); |
535 | | - } finally { |
536 | | - reader.releaseLock?.(); |
| 516 | + const result = await inflateWithCap(bytes, MAX_INFLATED_BODY_BYTES); |
| 517 | + if (!result.ok) { |
| 518 | + if (result.reason === "too_large") throw new InflatedBodyTooLargeError(MAX_INFLATED_BODY_BYTES); |
| 519 | + throw new Error(`request body is marked content-encoding: gzip but failed to decompress: ${result.detail}`); |
537 | 520 | } |
538 | | - return Buffer.concat(parts); |
| 521 | + return result.bytes; |
539 | 522 | } |
540 | 523 |
|
541 | 524 | /** |
542 | 525 | * Create a passthrough response that streams the upstream body to the client. |
543 | 526 | * Preserves status and the allowlisted headers, and honors the client's |
544 | 527 | * `Accept-Encoding` for gzip. |
545 | 528 | * |
546 | | - * The upstream request always advertises `accept-encoding: gzip` (see |
547 | | - * `buildUpstreamHeaderPairs`), so the upstream typically returns a gzip body. |
548 | | - * If THIS client did not advertise gzip, we decompress before forwarding and |
549 | | - * drop the now-mismatched `content-encoding`/`content-length` headers — |
550 | | - * otherwise clients whose HTTP stack does not auto-decompress (e.g. some |
551 | | - * Tauri-based clients) receive raw gzip bytes and fail to parse the JSON |
552 | | - * body with "non-JSON body" errors despite a 200 status. |
| 529 | + * The upstream request FORWARDS the client's `accept-encoding` (only |
| 530 | + * defaulting to "gzip" when the client sent none — see |
| 531 | + * `buildUpstreamHeaderPairs`), so the upstream compresses only when the |
| 532 | + * client can decode it. If THIS client did not advertise gzip but the body |
| 533 | + * arrived gzip-compressed anyway, we decompress before forwarding and drop |
| 534 | + * the now-mismatched `content-encoding`/`content-length` headers — otherwise |
| 535 | + * clients whose HTTP stack does not auto-decompress (e.g. some Tauri-based |
| 536 | + * clients) receive raw gzip bytes and fail to parse the JSON body with |
| 537 | + * "non-JSON body" errors despite a 200 status. |
553 | 538 | */ |
554 | 539 | function passthroughResponse( |
555 | 540 | upstream: Response, |
|
0 commit comments