Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

perf(transport): 复用设备压缩并压缩模型大请求

Dash295a0678a62d
diff --git a/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts b/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
index f9b2d0d784..04b3f0b92a 100644
--- a/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
@@ -1,11 +1,17 @@
import { describe, expect, it } from 'vitest';
import { encodeInvokeResultTransfer, InvokeResultCache } from '@cindy/device-link';
import { desktopTransferCodec } from '../transferCodec';
+import { decodeGzipBase64TextBounded } from '../../../../../mobile/src/utils/gzipBase64';
describe('desktop device-link transfer codec', () => {
- it('round-trips a large result between a desktop host and a desktop controller', () => {
+ // Protocol regression: the shared Node compressor must retain both desktop
+ // zlib and mobile pako compatibility. No native Mobile dependency changes.
+ it.each([
+ ['desktop', desktopTransferCodec],
+ ['mobile', { gunzipBase64: decodeGzipBase64TextBounded }],
+ ] as const)('round-trips a large result between a desktop host and a %s controller', (_name, codec) => {
const result = Array.from({ length: 300 }, (_, i) => ({ id: `s-${i}`, title: '远程任务 ✓' }));
- const controller = new InvokeResultCache(desktopTransferCodec);
+ const controller = new InvokeResultCache(codec);
const request = controller.prepare('desktop-b', { channel: 'local-db:sessions:list', args: [] });
const wire = encodeInvokeResultTransfer(request.payload.transfer!, { ok: true, result }, desktopTransferCodec);
expect(wire.ok && wire.transfer?.gzipBase64).toEqual(expect.any(String));
diff --git a/apps/desktop/src/main/device-link/transferCodec.ts b/apps/desktop/src/main/device-link/transferCodec.ts
index 29170d86ec..e78c5a8199 100644
--- a/apps/desktop/src/main/device-link/transferCodec.ts
+++ b/apps/desktop/src/main/device-link/transferCodec.ts
@@ -1,18 +1,20 @@
import { createHash } from 'node:crypto';
-import { constants, gunzipSync, gzipSync } from 'node:zlib';
+import { compressGzipSync, gunzipBase64TextBounded } from '@cindy/anthropic-compat-proxy/compression';
import type { DeviceLinkTransferCodec } from '@cindy/device-link';
+import { createLogger } from '../logger';
+
+const log = createLogger('device-link:compression');
/**
* Desktop 既是被控端也是控制端(桌面远控桌面),两个方向共用一份编码器。
- * 压缩级别取速度优先:大结果在主进程同步压缩,几百 KB 的会话列表约几毫秒。
+ * 保留 level 4 和同步发送错误契约;HTTP 大请求复用同一工具的异步版本。
*/
export const desktopTransferCodec: DeviceLinkTransferCodec = {
- gzipBase64: (text) => gzipSync(Buffer.from(text, 'utf8'), { level: constants.Z_BEST_SPEED + 3 }).toString('base64'),
- gunzipBase64: (base64, maxChars) => {
- // One UTF-16 unit never needs more than 3 UTF-8 bytes; zlib stops at the cap.
- const text = gunzipSync(Buffer.from(base64, 'base64'), { maxOutputLength: maxChars * 3 }).toString('utf8');
- if (text.length > maxChars) throw new RangeError('decompressed invoke result exceeds limit');
- return text;
+ gzipBase64: (text) => {
+ const { compressed, stats } = compressGzipSync(Buffer.from(text, 'utf8'), 4, true);
+ log.debug('gzip candidate', stats);
+ return compressed.toString('base64');
},
+ gunzipBase64: gunzipBase64TextBounded,
contentTag: (text) => createHash('sha256').update(text).digest('base64url'),
};
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 30342424d1..0b0208c3f6 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -176,7 +176,7 @@ known 是消息 ID 与主机提供的 SHA-256 正文指纹对。支持的主机
控制端 `invoke` payload 可选携带 `transfer: { gzip?: 1, ifNoneMatch?: string }`:注入了解压
能力就声明 `gzip: 1`;本机持有同一被控端、同一 channel 与 args 的上一份大结果时,带上它的
-内容标签。被控端照常执行 handler,只改写发出的 ok 结果:序列化后不小于 4 KiB 的结果附带
+内容标签。被控端照常执行 handler,只改写发出的 ok 结果:序列化后不小于 4096 个 UTF-16 code unit 的结果附带
`transfer.tag`(SHA-256);标签与 `ifNoneMatch` 相同时回 `{ result: null, transfer: { tag,
notModified: true } }`;否则在控制端声明可解且压缩确有收益时回 `{ result: null, transfer:
{ tag, gzipBase64 } }`。共享 DeviceLinkClient 在交给调用方之前还原成普通结果,并只用发请求
@@ -193,6 +193,28 @@ relay 不解析隧道 payload,无服务端改动、数据库 migration 或部
`packages/device-link/src/invokeTransfer.ts`,回归见同目录 `__tests__/invokeTransfer.test.ts`
与 `client.test.ts`「invoke 结果传输编码」。
+Desktop 的压缩保持 gzip level 4 与同步发送/错误处理契约,手机与电脑、电脑与电脑
+共用这一实现。收益按 gzip 的 base64 长度与原始 JSON 的 UTF-8 **字节数**比较,缩小超过
+10% 才采用;大小门槛、解压上限与缓存预算仍按原有字符数计。Node 编解码及数字统计复用
+`@cindy/anthropic-compat-proxy/compression` 独立入口,不把 Node 依赖引入 Mobile/device-link。
+
+## 模型 HTTP 代理的出站压缩
+
+`anthropic-compat-proxy` 在路由、正文转换与请求头改写之后,仅对最终目标为
+`https://api.anthropic.com:443/v1/messages` 的 JSON POST 启用 gzip:正文至少 64 KiB、
+没有既存 `Content-Encoding`,使用异步 level 1,压缩字节数减少超过 10% 才发送。
+目标按精确协议、主机、端口和路径判断,不按供应商名或兼容 API 格式猜测;本地端点、
+Cindy 网关与未经确认的第三方默认保持原样。原生订阅直连、WebSocket 与 localHandler
+自己发出的请求不经过这个出口,不因本改动强制压缩。
+
+保留逻辑 JSON 给观察器与已有语义错误恢复,只对 wire 副本压缩并重算 Content-Length。
+压缩等待后重新检查客户端连接与原有派发授权。压缩本身失败可在首次发送前回退原文;
+上游拒绝压缩不触发自动重放或支持探测,避免重复推理。成功响应仍流式透传。
+两类链路共用 Node gzip 原语与原始/gzip/wire 字节、压缩耗时的统计口径,业务门槛
+和封装各自保留。DEBUG 统计只记数字,不记录正文、凭证或完整目标 URL。
+回归见 `packages/anthropic-compat-proxy/src/request-compression.test.ts` 与 Desktop
+`device-link/__tests__/transferCodec.test.ts`(实际 zlib/pako 解码)。
+
## 群执行终态与错误展示
发送前拒绝、SDK 错误、正常完成和宿主超时进入同一个执行收尾入口;首个终态及提交正文固定,
diff --git a/packages/anthropic-compat-proxy/package.json b/packages/anthropic-compat-proxy/package.json
index c30581cbd3..2af6bcba95 100644
--- a/packages/anthropic-compat-proxy/package.json
+++ b/packages/anthropic-compat-proxy/package.json
@@ -7,7 +7,8 @@
"main": "./src/index.ts",
"types": "./src/index.ts",
"exports": {
- ".": "./src/index.ts"
+ ".": "./src/index.ts",
+ "./compression": "./src/compression.ts"
},
"scripts": {
"build": "tsc --noEmit",
diff --git a/packages/anthropic-compat-proxy/src/compression.ts b/packages/anthropic-compat-proxy/src/compression.ts
new file mode 100644
index 0000000000..c4554be703
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/compression.ts
@@ -0,0 +1,43 @@
+/** Node-only transport primitives. Policies and wire envelopes belong to callers. */
+import { performance } from 'node:perf_hooks';
+import { promisify } from 'node:util';
+import { gzip, gzipSync, gunzipSync } from 'node:zlib';
+
+const gzipAsync = promisify(gzip);
+
+export interface CompressionStats {
+ originalBytes: number;
+ gzipBytes: number;
+ wireBytes: number;
+ compressionMs: number;
+}
+
+function result(input: Buffer, compressed: Buffer, started: number, base64: boolean) {
+ const stats: CompressionStats = {
+ originalBytes: input.length,
+ gzipBytes: compressed.length,
+ wireBytes: base64 ? 4 * Math.ceil(compressed.length / 3) : compressed.length,
+ compressionMs: performance.now() - started,
+ };
+ return { compressed, stats };
+}
+
+/** Large HTTP uploads use libuv rather than blocking the main event loop. */
+export async function compressGzip(input: Buffer, level: number, base64 = false) {
+ const started = performance.now();
+ return result(input, await gzipAsync(input, { level }), started, base64);
+}
+
+/** Retains synchronous send/error contracts in existing device-link transports. */
+export function compressGzipSync(input: Buffer, level: number, base64 = false) {
+ const started = performance.now();
+ return result(input, gzipSync(input, { level }), started, base64);
+}
+
+export function gunzipBase64TextBounded(base64: string, maxChars: number): string {
+ // A UTF-16 code unit needs at most three UTF-8 bytes. Bound allocation in zlib,
+ // then enforce the existing character-based device-link wire contract.
+ const text = gunzipSync(Buffer.from(base64, 'base64'), { maxOutputLength: maxChars * 3 }).toString('utf8');
+ if (text.length > maxChars) throw new RangeError('decompressed text exceeds limit');
+ return text;
+}
diff --git a/packages/anthropic-compat-proxy/src/request-compression.test.ts b/packages/anthropic-compat-proxy/src/request-compression.test.ts
new file mode 100644
index 0000000000..a45298c862
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/request-compression.test.ts
@@ -0,0 +1,193 @@
+import { createServer, request as httpRequest, type IncomingHttpHeaders, type RequestOptions } from 'node:http';
+import { gunzipSync, gzipSync } from 'node:zlib';
+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { compressGzip } from './compression.js';
+import { createAnthropicCompatProxy } from './server.js';
+import { shouldCompressRequest } from './request-compression.js';
+import { listenOnAvailableLoopbackPort } from './test-loopback-server.js';
+import type { ProxyHandle, ProxyOptions } from './types.js';
+
+// Value: the production proxy must gzip only its final supported HTTP upload,
+// preserving recovery, cancellation and dispatch authorization. Existing tests
+// only use plain loopback targets, which deliberately never enable compression.
+// Minimal input: >=64 KiB JSON routed to the first-party Messages endpoint.
+// Tier: unit, one loopback fixture; HTTPS is redirected here, never to the internet.
+const transport = vi.hoisted(() => ({ port: 0 }));
+vi.mock('node:https', async (original) => ({
+ ...await original<typeof import('node:https')>(),
+ request: (options: RequestOptions) => httpRequest({
+ ...options, hostname: '127.0.0.1', port: transport.port, agent: undefined,
+ }),
+}));
+vi.mock('./compression.js', async (original) => {
+ const actual = await original<typeof import('./compression.js')>();
+ return { ...actual, compressGzip: vi.fn(actual.compressGzip) };
+});
+
+const records: Array<{ body: Buffer; headers: IncomingHttpHeaders }> = [];
+let responses: number[] = [];
+const upstream = createServer((req, res) => {
+ const chunks: Buffer[] = [];
+ req.on('data', (chunk: Buffer) => chunks.push(chunk));
+ req.on('end', () => {
+ records.push({ body: Buffer.concat(chunks), headers: req.headers });
+ res.writeHead(responses.shift() ?? 200, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ message: 'remove forbidden' }));
+ });
+});
+let proxy: ProxyHandle | undefined;
+beforeAll(async () => { transport.port = await listenOnAvailableLoopbackPort(upstream); });
+beforeEach(() => { records.length = 0; responses = []; vi.mocked(compressGzip).mockClear(); });
+afterEach(async () => { await proxy?.dispose(); proxy = undefined; });
+afterAll(async () => {
+ upstream.closeAllConnections();
+ await new Promise<void>((resolve) => upstream.close(() => resolve()));
+});
+
+const large = { model: 'claude-test', messages: [{ role: 'user', content: '中文代码🙂'.repeat(12_000) }] };
+async function start(options: Partial<ProxyOptions> = {}) {
+ proxy = await createAnthropicCompatProxy({
+ upstream: 'https://api.anthropic.com', transformRequest: [], ...options,
+ });
+ return proxy;
+}
+function send(body: string | Buffer = JSON.stringify(large), headers = {}, path = '/v1/messages') {
+ return fetch(`${proxy!.url}${path}`, {
+ method: 'POST', headers: { 'content-type': 'application/json', ...headers },
+ body: typeof body === 'string' ? body : new Uint8Array(body),
+ });
+}
+const decoded = (index = 0) => JSON.parse(gunzipSync(records[index].body).toString('utf8'));
+
+describe('final model request compression', () => {
+ it('compresses after routing/transforms; preserves JSON for observers and reports byte metrics only', async () => {
+ const observer = vi.fn();
+ const debug = vi.fn();
+ await start({
+ upstream: 'http://unused.invalid',
+ routingTransform: () => ({ upstreamOverride: 'https://api.anthropic.com' }),
+ transformRequest: [(body) => ({ ...(body as Record<string, unknown>), model: 'claude-rewritten' })],
+ responseObserver: observer,
+ logger: { debug },
+ });
+ const response = await send();
+ expect(response.status).toBe(200);
+ await response.text();
+ expect(decoded()).toEqual({ ...large, model: 'claude-rewritten' });
+ expect(records[0].headers['content-encoding']).toBe('gzip');
+ expect(Number(records[0].headers['content-length'])).toBe(records[0].body.length);
+ expect(records[0].body.length).toBeLessThan(Buffer.byteLength(JSON.stringify(large)) / 4);
+ expect(JSON.parse(observer.mock.calls[0][0].requestBody.toString())).toEqual(decoded());
+ expect(debug.mock.calls.find(([message]) => message === 'request gzip')?.[1]).toEqual({
+ reqId: expect.any(Number), originalBytes: expect.any(Number), gzipBytes: records[0].body.length,
+ wireBytes: records[0].body.length, compressionMs: expect.any(Number), used: true,
+ });
+ });
+
+ it.each([
+ ['small', JSON.stringify({ model: 'claude-test' }), {}, '/v1/messages'],
+ ['already encoded', gzipSync(JSON.stringify(large)), { 'Content-Encoding': 'gzip' }, '/v1/messages'],
+ ['other media', JSON.stringify(large), { 'content-type': 'text/plain' }, '/v1/messages'],
+ ['other endpoint', JSON.stringify(large), {}, '/v1/other'],
+ ])('passes %s requests through without another encoding', async (_name, body, headers, path) => {
+ await start();
+ await (await send(body as string | Buffer, headers, path as string)).text();
+ expect(records[0].body).toEqual(Buffer.from(body as string));
+ expect(compressGzip).not.toHaveBeenCalled();
+ });
+
+ it('uses the final route: a compatible third-party or local target is not sufficient', async () => {
+ await start({ routingTransform: () => ({ upstreamOverride: `http://127.0.0.1:${transport.port}` }) });
+ await (await send()).text();
+ expect(records[0].headers['content-encoding']).toBeUndefined();
+ expect(JSON.parse(records[0].body.toString())).toEqual(large);
+ expect(compressGzip).not.toHaveBeenCalled();
+ });
+
+ it('keeps the original body for semantic recovery and compresses the corrected retry once', async () => {
+ responses = [400, 200];
+ await start({ recoveryRules: [{
+ id: 'test-forbidden', enabled: () => true, matches: (error) => error.includes('remove forbidden'),
+ strip: (body) => {
+ const parsed = JSON.parse(body.toString());
+ delete parsed.forbidden;
+ return Buffer.from(JSON.stringify(parsed));
+ },
+ }] });
+ expect((await send(JSON.stringify({ ...large, forbidden: true }))).status).toBe(200);
+ expect(decoded(0).forbidden).toBe(true);
+ expect(decoded(1)).toEqual(large);
+ expect(records).toHaveLength(2);
+ });
+
+ it('does not retry an upstream rejection to guess gzip support', async () => {
+ responses = [415];
+ await start();
+ expect((await send()).status).toBe(415);
+ expect(records).toHaveLength(1);
+ });
+
+ it('falls back before sending if local compression fails or has insufficient savings', async () => {
+ await start();
+ vi.mocked(compressGzip).mockRejectedValueOnce(new Error('local zlib failure'));
+ await (await send()).text();
+ const body = Buffer.from(JSON.stringify(large));
+ vi.mocked(compressGzip).mockResolvedValueOnce({ compressed: body, stats: {
+ originalBytes: body.length, gzipBytes: body.length, wireBytes: body.length, compressionMs: 0,
+ } });
+ await (await send()).text();
+ expect(records).toHaveLength(2);
+ for (const record of records) {
+ expect(record.body).toEqual(body);
+ expect(record.headers['content-encoding']).toBeUndefined();
+ }
+ });
+
+ it.each(['owner-changed', 'client-disconnected'] as const)('does not dispatch after %s during compression', async (reason) => {
+ let release!: () => void;
+ let started!: () => void;
+ const entered = new Promise<void>((resolve) => { started = resolve; });
+ const blocked = new Promise<void>((resolve) => { release = resolve; });
+ let valid = true;
+ vi.mocked(compressGzip).mockImplementationOnce(async (body) => {
+ started(); await blocked;
+ const compressed = gzipSync(body);
+ return { compressed, stats: { originalBytes: body.length, gzipBytes: compressed.length,
+ wireBytes: compressed.length, compressionMs: 0 } };
+ });
+ await start({ routingTransform: () => ({ dispatchGenerationValid: () => valid }) });
+ const abort = new AbortController();
+ const pending = fetch(`${proxy!.url}/v1/messages`, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(large), signal: abort.signal,
+ });
+ await entered;
+ if (reason === 'owner-changed') valid = false;
+ else {
+ abort.abort();
+ await expect(pending).rejects.toThrow();
+ // dispose closes the server-side response before the queued gzip finishes.
+ await proxy!.dispose(); proxy = undefined;
+ }
+ release();
+ if (reason === 'owner-changed') expect((await pending).status).toBe(503);
+ // Drain the deferred continuation, without a timing-based sleep.
+ await blocked;
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ expect(records).toHaveLength(0);
+ });
+
+ it('requires an exact supported destination, JSON and a 64 KiB byte threshold', () => {
+ const target = { protocol: 'https:', hostname: 'api.anthropic.com', port: 443 };
+ const headers = { 'Content-Type': 'application/json; charset=utf-8' };
+ const eligible = (t = target, bytes = 65536, h = headers) => shouldCompressRequest(t, 'POST', '/v1/messages', h, bytes);
+ expect(eligible()).toBe(true);
+ expect(eligible(target, 65535)).toBe(false);
+ for (const hostname of ['localhost', '127.0.0.1', '::1', 'api.openai.com', 'api.anthropic.com.example.com']) {
+ expect(eligible({ ...target, hostname })).toBe(false);
+ }
+ expect(eligible({ ...target, protocol: 'http:' })).toBe(false);
+ expect(eligible({ ...target, port: 8443 })).toBe(false);
+ expect(eligible(target, 65536, { ...headers, 'Content-Encoding': 'identity' } as typeof headers)).toBe(false);
+ });
+});
diff --git a/packages/anthropic-compat-proxy/src/request-compression.ts b/packages/anthropic-compat-proxy/src/request-compression.ts
new file mode 100644
index 0000000000..d68d7fad6c
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/request-compression.ts
@@ -0,0 +1,20 @@
+import { headerValue } from './headers.js';
+
+/**
+ * Only the first-party Messages endpoint is known to support gzip request bodies
+ * (also targeted by Claude Code's native gzip transport). An Anthropic-compatible
+ * API shape does not imply support. No probes, retries, or provider-name guesses.
+ */
+export function shouldCompressRequest(
+ target: { protocol: string; hostname: string; port: number },
+ method: string,
+ pathname: string,
+ headers: Readonly<Record<string, string>>,
+ bytes: number,
+): boolean {
+ return target.protocol === 'https:' && target.hostname === 'api.anthropic.com' && target.port === 443
+ && method === 'POST' && pathname === '/v1/messages'
+ && bytes >= 64 * 1024
+ && headerValue(headers, 'content-type').split(';', 1)[0].trim().toLowerCase() === 'application/json'
+ && !Object.keys(headers).some((key) => key.toLowerCase() === 'content-encoding');
+}
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index e820b7835b..32387a95c1 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -14,6 +14,8 @@
*/
import { installResponseGuard } from './response-guard.js';
+import { compressGzip } from './compression.js';
+import { shouldCompressRequest } from './request-compression.js';
import { collectRecoverableBody } from './oversized-attachments.js';
import { createHash } from 'node:crypto';
import { createServer, request as httpRequest, type ClientRequest, type IncomingMessage, type RequestOptions, type Server, type ServerResponse } from 'node:http';
@@ -905,7 +907,19 @@ function decodeBodyForLog(buf: Buffer, contentEncoding: string | undefined): Buf
*
* 请求 body 在调用 forward 前已被上层在 debug 级别下 dump 过,这里不重复。
*/
-function forward(
+function forward(...args: Parameters<typeof forwardRequest>): void {
+ void forwardRequest(...args).catch(() => {
+ const res = args[5];
+ // Preparation/dispatch failure must settle the request, never leave an
+ // unhandled rejection or replay inference to discover compression support.
+ if (res.destroyed || res.writableEnded) return;
+ if (res.headersSent) { res.destroy(); return; }
+ res.writeHead(502, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ error: { type: 'proxy_error', message: 'upstream request preparation failed' } }));
+ });
+}
+
+async function forwardRequest(
target: UpstreamTarget,
method: string,
path: string,
@@ -945,13 +959,13 @@ function forward(
// 为 true 时启用 2xx 成功响应的流式有效性门(#2242):空 2xx / 非 SSE 2xx /
// 零事件 SSE 不再原样透传给客户端,转成结构化 502。false 保持字节级透传。
requestDeclaredStream = false,
- // 透明重试前再跑同一条 dispatch gate。返回 false = 已改道(典型 503),
+ // 异步压缩完成后、透明重试前再跑同一条 dispatch gate。返回 false = 已改道(典型 503),
// 不得带着旧 headerOverride 再发一次。省略 = 与扩展前一样直接重发。
beforeRetry?: () => Promise<boolean>,
// Optional request-local operational observer. It never receives raw request/response data.
forwardLifecycle?: ForwardLifecycleObserver | null,
recoverUpstreamOversized?: (body: Buffer, signal: AbortSignal) => Promise<Buffer>,
-): void {
+): Promise<void> {
// Diagnostics are a strict side channel: callback failures must never change forwarding.
const notifyForwardLifecycle = (notify: () => void): void => {
try {
@@ -989,6 +1003,29 @@ function forward(
const upstreamQuery = [actualTarget.baseQuery, routedQuery].filter(Boolean).join('&');
const upstreamPath = upstreamQuery ? `${upstreamPathname}?${upstreamQuery}` : upstreamPathname;
+ // Keep the logical body for observers and existing semantic recovery rules.
+ // Compress only the final wire copy, after routing, transforms and headers.
+ let wireBody = body;
+ if (shouldCompressRequest(actualTarget, method, upstreamPathname, actualHeaders, body.length)) {
+ try {
+ const { compressed, stats } = await compressGzip(body, 1);
+ const used = stats.wireBytes < stats.originalBytes * 0.9;
+ if (used) {
+ wireBody = compressed;
+ actualHeaders = { ...actualHeaders, 'content-encoding': 'gzip' };
+ }
+ logger.debug?.('request gzip', { reqId, ...stats, used });
+ } catch {
+ // Nothing has been sent yet. Local compression failure can safely retain
+ // the original body; an upstream error must never trigger a gzip retry.
+ logger.debug?.('request gzip unavailable; sending identity', { reqId });
+ }
+ if (clientRes.destroyed || clientRes.writableEnded) return;
+ // Compression yields to the event loop: credentials/owner may have changed.
+ if (beforeRetry && !(await beforeRetry())) return;
+ if (clientRes.destroyed || clientRes.writableEnded) return;
+ }
+
// http.request 会把 options 原样透传给 agent.createConnection → net.connect,
// 所以 socket 级 connect 选项运行时有效;但 @types/node 的 RequestOptions 没收录
// autoSelectFamilyAttemptTimeout,用交叉类型显式补上。
@@ -1000,7 +1037,7 @@ function forward(
headers: {
...actualHeaders,
host: actualTarget.hostname,
- 'content-length': String(body.length),
+ 'content-length': String(wireBody.length),
},
timeout: UPSTREAM_SOCKET_TIMEOUT_MS,
autoSelectFamilyAttemptTimeout: UPSTREAM_CONNECT_ATTEMPT_TIMEOUT_MS,
@@ -1889,7 +1926,7 @@ function forward(
upstreamReq.destroy(new Error('upstream socket timeout'));
});
- upstreamReq.end(body);
+ upstreamReq.end(wireBody);
}
/**
diff --git a/packages/anthropic-compat-proxy/src/types.ts b/packages/anthropic-compat-proxy/src/types.ts
index 66b2a489bf..88611e21d3 100644
--- a/packages/anthropic-compat-proxy/src/types.ts
+++ b/packages/anthropic-compat-proxy/src/types.ts
@@ -168,6 +168,7 @@ export interface ResponseObserverCtx {
*/
readonly outboundHeaders?: Readonly<Record<string, string>>;
readonly responseHeaders: Readonly<Record<string, string>>;
+ /** Logical outbound body, before optional transport gzip (observers parse JSON). */
readonly requestBody: Buffer;
}
diff --git a/packages/device-link/src/__tests__/invokeTransfer.test.ts b/packages/device-link/src/__tests__/invokeTransfer.test.ts
index ddc956aeec..9da9689d30 100644
--- a/packages/device-link/src/__tests__/invokeTransfer.test.ts
+++ b/packages/device-link/src/__tests__/invokeTransfer.test.ts
@@ -57,6 +57,25 @@ describe('invoke result transfer encoding', () => {
.toEqual({ ok: true, result: null, transfer: { tag, notModified: true } });
});
+ // Regression: UTF-16 length understates Chinese wire bytes; an otherwise useful
+ // gzip result was discarded. Unit tier, real encoder/decoder, bounded fixture.
+ it('judges Chinese compression savings by UTF-8 bytes including base64 overhead', () => {
+ let seed = 1;
+ const text = Array.from({ length: 6000 }, () => {
+ seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
+ return String.fromCharCode(0x4e00 + ((seed >>> 16) % 1024));
+ }).join('');
+ const json = JSON.stringify(text);
+ const base64 = codec.gzipBase64(json);
+ expect(base64.length).toBeGreaterThan(json.length);
+ expect(base64.length).toBeLessThan(Buffer.byteLength(json) * 0.9);
+ const cache = new InvokeResultCache(codec);
+ const request = cache.prepare('desktop', { channel: 'c', args: [] });
+ const wire = encodeInvokeResultTransfer(request.payload.transfer!, { ok: true, result: text }, codec);
+ expect(wire.ok && wire.transfer?.gzipBase64).toBe(base64);
+ expect(request.decode(wire)).toEqual({ ok: true, result: text });
+ });
+
it('controller decodes against the snapshot fixed at request time', () => {
const cache = new InvokeResultCache(codec);
const request = { channel: 'maker:provider:list', args: [] };
diff --git a/packages/device-link/src/invokeTransfer.ts b/packages/device-link/src/invokeTransfer.ts
index 2e982c6c40..cb111fafff 100644
--- a/packages/device-link/src/invokeTransfer.ts
+++ b/packages/device-link/src/invokeTransfer.ts
@@ -1,4 +1,5 @@
import type { InvokePayload, InvokeResultPayload } from './protocol.js';
+import { byteLength } from './transport.js';
/**
* invoke 结果的传输编码(弱网减流量),与业务 channel 无关:
@@ -91,7 +92,9 @@ export function encodeInvokeResultTransfer(
}
if (options.gzip && codec.gzipBase64) {
const gzipBase64 = codec.gzipBase64(text);
- if (gzipBase64.length < text.length * 0.9) {
+ // Base64 is ASCII, whereas Chinese/emoji JSON takes multiple UTF-8 bytes
+ // per character. Compare transmitted bytes, not JS string lengths.
+ if (gzipBase64.length < byteLength(text) * 0.9) {
return { ok: true, result: null, transfer: { ...(tag ? { tag } : {}), gzipBase64 } };
}
}