diff --git a/CHANGELOG.md b/CHANGELOG.md index e97c33ec46..7e8e1e9806 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Features +- **Native Google (Gemini) wire support: `generateContent` / `streamGenerateContent` conversations are tracked, folded and compressed (#866, PR #867)**: bili terminated TLS for `generativelanguage.googleapis.com` and forwarded POSTs verbatim, because protocol detection matched only `/chat/completions`, `/v1/messages` and `/responses` — Gemini's `/v1beta/models/:streamGenerateContent` matched nothing, so `protocol` stayed `null`, the kernel path was skipped, the request logged as `[unknown] forward POST →`, and no conversation identity was minted: ACP compression never fired while the plugin cancels the client's own compaction, and a live omp session reached 339k tokens. The kernel now carries a full Gemini codec (`src/wire/google.ts`: `googleToCore` / `coreToGoogle` / `googleSystemText` / `conversationSignalGoogle`, flat `{name,description,parameters}` function declarations, `thoughtSignature` and `thought` sidecars, and same-side run merging because Gemini rejects non-alternating roles); the proxy detects the wire from the URL (`googlePathKind`, `googleModelFromPath`), hoists system text out of the fold space into `systemInstruction`, resolves identity by prefix affinity over `contents` (Gemini clients send no session header, so the header chain cannot apply), maps usage as input = `promptTokenCount` − `cachedContentTokenCount` and output = `candidatesTokenCount` + `thoughtsTokenCount`, treats `generationConfig.maxOutputTokens` as the output budget, replays thinking signatures on re-issued reasoning, and rejects the OpenAI-compatibility shim on purpose: it drops `thoughtSignature` / `extra_content`, which Gemini 3 function calling requires. Two defects that only a live wire exposed were fixed on the same branch: preflight summaries posted to the client's own SSE endpoint (`:streamGenerateContent` answers SSE whatever the request says) were parsed only when `preflightStreamSummary` was set, so every summary read as empty (`carried N SSE event(s) but no summary text`, 8 attempts, then a 502 and a 300s dead-end cooldown) — the response shape now decides the parser (`SSE_DATA_LINE_RE`, `extractSummaryFromSse` vs `extractSummaryText`); and the proxy's synthesized tool result carried only `toolCallId`, so the wire emitted `functionResponse{name:"unknown"}` answering `functionCall{name:"compress"}` and Google rejected the whole request with 400 `INVALID_ARGUMENT` (Gemini pairs calls by name, OpenAI/Anthropic by id). Covered by `tests/wire-google.test.ts`, `tests/adapter-google.test.ts`, `tests/preflight-google-summary.test.ts` and google cases in `tests/preflight-sse-extraction.test.ts`, with `tests/e2e-google-compress.test.ts` carrying a strict Gemini shape validator (alternating roles, one payload per part, signatures only on model parts, `functionResponse` named AND answering a call seen earlier) that reports nine `functionResponse name=unknown answers no call` violations when the name fix is reverted; verified live against `gemini-3.8-flash`, where a proxy-owned `compress` call executed, folded ~97k tokens and was accepted. - **Image billing modes (`imageBilling` / `BILI_IMAGE_BILLING`): pixel-tile upstreams no longer get permanently 502'd by byte-based image estimates (#767)**: follow-up to #496/#500 — on official Codex/OpenAI/Anthropic upstreams (pixel-tile billing), once a session's historical `lastInputTokens` baseline sits above the window, the default `base64 length / 4` image cost closes BOTH safe-forward paths at once (#496 forward-once requires a sub-window baseline; #300 stale-baseline fit requires `payloadEstimate < limit`), so preflight fails 502 `preflight_compress_failed` ("Images alone account for ~N tokens") forever even though the images bill only a few thousand tokens upstream (repro: two screenshots, byte estimate ~300K vs real billing ~4.4K). New global + per-provider `imageBilling` (`"auto" | "pixels" | "bytes"`; env `BILI_IMAGE_BILLING` overrides both, live-read per request): `pixels` parses the image header (PNG/JPEG/WebP/GIF/BMP, bounded decode — never the full body) and charges first-party high-detail tile billing (512px tiles, short side scaled up to 768px, long side capped at 2048px → 765–2805 tokens per image; unparsable formats fall back to a flat 16384); `bytes` keeps the conservative default so byte-counting relays retain their #488/#496 protection unchanged. `auto` classifies by upstream host (`openai.com` / `openai.azure.com` / `chatgpt.com` / `api.anthropic.com` suffixes → pixels; everything else → bytes). Remote URLs stay flat 4096 in both modes; `BILI_IMAGE_TOKEN_CAP` still clamps on top of both. A stale baseline now self-heals within one turn of pixel-billed traffic (usage reports replace the baseline absolutely), while #496's one-rejection-then-block guarantee for genuine byte-relay overflow is preserved — pinned by regression tests: recovery e2e (stale 696789 baseline + two large screenshots forwards and recovers under `pixels`), bytes-mode control (same payload still fails fast with zero images forwarded), learned-limit-only variant, and env-precedence check. - **Session-file encryption at rest behind `BILI_ENCRYPTION_KEY` (#708, PR #711)**: for deployments on untrusted nodes, persisted session files (block summaries plus up to ~16k tokens of folded conversation per session — effectively full conversation content) can now be stored as `BILIENC1` AES-256-GCM over zstd-compressed JSON (zstd via built-in node:zlib on Node ≥ 22.15, raw mode byte on older runtimes; compression also shrinks files ~5–10×). The key is env-only — deliberately NO key-file support, since a key file would sit on the same untrusted filesystem as the data — exactly 32 bytes hex or base64, never written to disk or logs; an invalid value aborts startup (fail fast instead of silently running unencrypted). When enabled, boot takes over legacy plaintext files once: each is re-encoded and atomically replaced in place (the rename IS the old-file deletion; a crash mid-migration self-heals on the next boot), covering `.fb.json` spill files too; unreadable files are left in place with a warning. Wrong key at boot → GCM auth failure → affected sessions skip as corrupt (logged, no crash). Mechanism is the kernel's optional `StateStore` codec hook (acp-kernel#249 → 0.0.64); unset key = zero behavior change. - **Per-route `passthrough` option for providers (#661, PR #662)**: `"providers": { "mitm://zcode.z.ai": { "passthrough": true } }` forwards matching requests byte-for-byte — no kernel round-trip, no render tags, no `prompt_cache_key` removal, response piped verbatim, no session state — the scoped sibling of the global `passthrough` switch. `mitm://` keys target only MITM (login-client) traffic; plain `https://` host keys cover both MITM and `/bili/` traffic. diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 49826ead5a..ff16eee872 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -15,10 +15,12 @@ import { ABSORB_TOOL_OPENAI, RULE_TOOL_NAME, SEARCH_CONTEXT_TOOL, + SEARCH_CONTEXT_TOOL_GOOGLE, SEARCH_CONTEXT_TOOL_OPENAI, SEARCH_CONTEXT_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, ACP_TOOLS_ANTHROPIC, + ACP_TOOLS_GOOGLE, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, ACP_READONLY_TOOLS_RESPONSES, @@ -42,18 +44,23 @@ export { COMPRESS_TOOL, COMPRESS_TOOL_OPENAI, COMPRESS_TOOL_RESPONSES, + COMPRESS_TOOL_GOOGLE, DECOMPRESS_TOOL, DECOMPRESS_TOOL_OPENAI, DECOMPRESS_TOOL_RESPONSES, + DECOMPRESS_TOOL_GOOGLE, SEARCH_CONTEXT_TOOL, SEARCH_CONTEXT_TOOL_OPENAI, SEARCH_CONTEXT_TOOL_RESPONSES, + SEARCH_CONTEXT_TOOL_GOOGLE, ACP_STATUS_TOOL, ACP_STATUS_TOOL_OPENAI, ACP_STATUS_TOOL_RESPONSES, + ACP_STATUS_TOOL_GOOGLE, ACP_TOOLS_OPENAI, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_RESPONSES, + ACP_TOOLS_GOOGLE, ACP_READONLY_TOOLS_RESPONSES, buildCompressSystemPrompt, buildCompressTextSystemPrompt, @@ -61,6 +68,7 @@ export { ABSORB_TOOL_NAME, ABSORB_TOOL, ABSORB_TOOL_OPENAI, + ABSORB_TOOL_GOOGLE, buildAbsorbSystemPrompt, RULE_TOOL_NAME, } from "acp-kernel"; @@ -104,9 +112,16 @@ export const BILI_SEARCH_CONTEXT_TOOL_RESPONSES = { parameters: withConversationId(SEARCH_CONTEXT_TOOL_RESPONSES.parameters), }; +export const BILI_SEARCH_CONTEXT_TOOL_GOOGLE = { + name: SEARCH_CONTEXT_TOOL_GOOGLE.name, + description: SEARCH_CONTEXT_TOOL_GOOGLE.description, + parameters: withConversationId(SEARCH_CONTEXT_TOOL_GOOGLE.parameters), +}; + export const BILI_ACP_TOOLS_ANTHROPIC = ACP_TOOLS_ANTHROPIC.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL : t)); export const BILI_ACP_TOOLS_OPENAI = ACP_TOOLS_OPENAI.map((t) => (t.function.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_OPENAI : t)); export const BILI_ACP_TOOLS_RESPONSES = ACP_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)); +export const BILI_ACP_TOOLS_GOOGLE = ACP_TOOLS_GOOGLE.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_GOOGLE : t)); export const BILI_ACP_READONLY_TOOLS_RESPONSES = ACP_READONLY_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)); // The kernel ships no Responses-format absorb const (the four ACP tools have @@ -120,8 +135,8 @@ export const ABSORB_TOOL_RESPONSES = { }; // The reconciled kernel (acp-kernel#332) ships RULE_TOOL_NAME + the rule state -// helpers but no wire tool objects. Synthesize all three shapes here so every -// injection point (wire helpers, plugin manifest) serves one definition. +// helpers but no wire tool objects. Synthesize all four wire shapes here so +// every injection point (wire helpers, plugin manifest) serves one definition. const RULE_TOOL_DESCRIPTION = "Record a short, principle-level reminder so it survives context compression — the call and its result are protected and stay in context. Record when: the user calls out or repeatedly emphasizes a lesson; the user asks you to remember or follow a behavior; you personally hit a major pitfall worth remembering long-term. Keep each rule to one short line. Omit the rule argument to list recorded rules."; const RULE_PARAM_SCHEMA = { type: "object", @@ -132,6 +147,7 @@ const RULE_PARAM_SCHEMA = { export const RULE_TOOL = { name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, input_schema: RULE_PARAM_SCHEMA }; export const RULE_TOOL_OPENAI = { type: "function" as const, function: { name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA } }; export const RULE_TOOL_RESPONSES = { type: "function" as const, name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA }; +export const RULE_TOOL_GOOGLE = { name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA }; export function parseCompressInput(input: unknown, callId?: string) { const parsed = parseCompressArgs(input, { callId }); diff --git a/src/config.ts b/src/config.ts index 7e573d0de5..4307c67782 100644 --- a/src/config.ts +++ b/src/config.ts @@ -256,6 +256,7 @@ const CONTEXT_LIMIT_TABLE: Array<{ match: RegExp; limit: number }> = [ { match: /^gpt-4o/i, limit: 128_000 }, { match: /^gpt-4-turbo/i, limit: 128_000 }, { match: /^o[13]-/i, limit: 200_000 }, + { match: /^gemini-3/i, limit: 1_048_576 }, { match: /^gemini-2\.5/i, limit: 1_000_000 }, { match: /^gemini-1\.5/i, limit: 1_000_000 }, { match: /^glm-4\.6/i, limit: 128_000 }, diff --git a/src/fake-completion.ts b/src/fake-completion.ts index 00cb3e5194..97a55257d5 100644 --- a/src/fake-completion.ts +++ b/src/fake-completion.ts @@ -40,6 +40,9 @@ export function fakeBufCap(): number { const ANTHROPIC_TOOL_BLOCK = /"type"\s*:\s*"tool_use"/; const OPENAI_TOOL_BLOCK = /"tool_calls"\s*:\s*\[\s*\{/; const RESPONSES_TOOL_BLOCK = /"type"\s*:\s*"function_call"/; +// Gemini has no `type` discriminator: a real invocation is a functionCall PART +// (`"functionCall":{"name":…}`) inside candidates[*].content.parts. +const GOOGLE_TOOL_BLOCK = /"functionCall"\s*:\s*\{/; export function hasToolBlock(protocol: WireProtocol, rawText: string): boolean { switch (protocol) { @@ -49,6 +52,8 @@ export function hasToolBlock(protocol: WireProtocol, rawText: string): boolean { return OPENAI_TOOL_BLOCK.test(rawText); case "responses": return RESPONSES_TOOL_BLOCK.test(rawText); + case "google": + return GOOGLE_TOOL_BLOCK.test(rawText); } } diff --git a/src/image-tokens.ts b/src/image-tokens.ts index 5d5c41bb4a..61b0ae53a4 100644 --- a/src/image-tokens.ts +++ b/src/image-tokens.ts @@ -208,9 +208,27 @@ function urlOf(v: unknown): string | undefined { return undefined; } -export function imageTokensInParsedBody(protocol: "anthropic" | "openai" | "responses", body: unknown, billing: ResolvedImageBilling = "bytes"): number { +export function imageTokensInParsedBody(protocol: "anthropic" | "openai" | "responses" | "google", body: unknown, billing: ResolvedImageBilling = "bytes"): number { if (!isObj(body)) return 0; let total = 0; + if (protocol === "google") { + // Gemini carries images as `inlineData` (raw base64 in `data`) or + // `fileData` (a remote `fileUri`) PARTS of `contents[].parts` — there is + // no separate content-array layer and no `type` discriminator. + const contents = body.contents; + if (!Array.isArray(contents)) return 0; + for (const c of contents) { + if (!isObj(c) || !Array.isArray(c.parts)) continue; + for (const part of c.parts) { + if (!isObj(part)) continue; + const inline = part.inlineData; + if (isObj(inline) && typeof inline.data === "string") total += base64ImageCost(inline.data, billing); + const file = part.fileData; + if (isObj(file) && typeof file.fileUri === "string") total += costForUrl(file.fileUri, billing); + } + } + return total; + } if (protocol === "responses") { const input = body.input; if (!Array.isArray(input)) return 0; @@ -250,10 +268,11 @@ export function imageTokensInParsedBody(protocol: "anthropic" | "openai" | "resp // Cheap gate: most bodies carry no images — skip the JSON parse entirely then. // prepared.body is bili's own compact JSON.stringify, but client raw buffers // may carry spaces, so probe both forms. -export function imageTokensInRawBody(protocol: "anthropic" | "openai" | "responses", raw: string | Buffer, billing: ResolvedImageBilling = "bytes"): number { +export function imageTokensInRawBody(protocol: "anthropic" | "openai" | "responses" | "google", raw: string | Buffer, billing: ResolvedImageBilling = "bytes"): number { const s = typeof raw === "string" ? raw : raw.toString("utf8"); const probe = - protocol === "responses" ? s.includes("input_image") + protocol === "google" ? s.includes("inlineData") || s.includes("fileData") + : protocol === "responses" ? s.includes("input_image") : protocol === "openai" ? s.includes("image_url") : s.includes('"type":"image"') || s.includes('"type": "image"'); if (!probe) return 0; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts new file mode 100644 index 0000000000..046605f305 --- /dev/null +++ b/src/loop/adapter-google.ts @@ -0,0 +1,568 @@ +import type { CoreMessage } from "acp-kernel"; +import { coreToGoogle } from "acp-kernel/wire"; +import type { GooglePart } from "acp-kernel/wire"; +import { buildVisibilityMarker } from "../compress-loop.js"; +import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; +import { degenerateTurnWarning } from "../degenerate-turn.js"; +import { log as loggerLog } from "../logger.js"; + +import type { + CompressLoopAdapter, + EmitCompletionOpts, + ParsedStreamEvent, + ToolCallEmit, +} from "./core.js"; + +const PROXY_TOOL_SET = new Set([ + "compress", "decompress", "search_context", "acp_status", + "bili_compress", "bili_decompress", "bili_search_context", "bili_status", +]); + +// Gemini's own finish vocabulary. The loop hands emitCompletion whatever the +// round's `done` event carried, which may be another wire's word ("length" +// from a proxy-side truncation, "tool_calls" from the openai adapter) — every +// value must land on a real Gemini reason because the client THROWS on a +// stream that ends without one. +const GOOGLE_FINISH_REASONS: Record = { + STOP: true, + MAX_TOKENS: true, + SAFETY: true, + RECITATION: true, + OTHER: true, + BLOCKLIST: true, + PROHIBITED_CONTENT: true, + SPII: true, + MALFORMED_FUNCTION_CALL: true, + IMAGE_SAFETY: true, + LANGUAGE: true, + FINISH_REASON_UNSPECIFIED: true, +}; + +function googleFinishReason(reason: string | undefined): string { + if (!reason) return "STOP"; + const upper = reason.toUpperCase(); + if (GOOGLE_FINISH_REASONS[upper] === true) return upper; + if (upper === "LENGTH") return "MAX_TOKENS"; + if (upper === "END_TURN" || upper === "TOOL_CALLS" || upper === "TOOL_USE" || upper === "COMPLETED" || upper === "STOP_SEQUENCE") return "STOP"; + return "OTHER"; +} + +function readUsageMetadata(usage: Record): { + inputTokens?: number; + outputTokens?: number; + cachedTokens?: number; +} { + const num = (v: unknown): number | undefined => (typeof v === "number" && Number.isFinite(v) ? v : undefined); + const candidates = num(usage.candidatesTokenCount); + const thoughts = num(usage.thoughtsTokenCount); + return { + inputTokens: num(usage.promptTokenCount), + cachedTokens: num(usage.cachedContentTokenCount), + // Thinking tokens are billed as output; the client's own accounting + // reads a single output number, so they are folded in here. + outputTokens: candidates === undefined && thoughts === undefined ? undefined : (candidates ?? 0) + (thoughts ?? 0), + }; +} + +function sseFrame(parsed: Record): Buffer { + return Buffer.from(`data: ${JSON.stringify(parsed)}\n\n`, "utf8"); +} + +// One chunk is edited in place — its parts replaced, and/or an early +// finishReason dropped. A forwarded chunk must never carry a finishReason +// unless it IS the round's completion: the loop may run another upstream round +// after this one (core.ts reRequest), and a leaked early finish makes every +// later token "content after the finish reason" to the client. +function cloneChunk( + parsed: Record, + edit: { parts?: GooglePart[]; dropFinishReason?: boolean }, +): Record { + const candidates = parsed.candidates as Array> | undefined; + if (!candidates || candidates.length === 0) return parsed; + const candidate = { ...candidates[0] }; + if (edit.dropFinishReason) delete candidate.finishReason; + if (edit.parts) { + candidate.content = { role: "model", ...(candidate.content as Record | undefined), parts: edit.parts }; + } + return { ...parsed, candidates: [candidate, ...candidates.slice(1)] }; +} + +interface FunctionCallBuffer { + index: number; + id: string; + name: string; + args: string; + signature: string; +} + +// A decision per buffered chunk that carried functionCall parts: "keep" = every +// call in it is real (replay verbatim, preserving signatures/ids/order), null = +// only proxy calls (drop — the proxy executes them server-side), or a rewritten +// copy keeping the chunk's other parts plus its real calls. +function filterRealCallChunk( + parsed: Record, + realIndexes: Set, + callIndexes: number[], +): "keep" | null | Record { + const candidates = parsed.candidates as Array> | undefined; + const parts = (candidates?.[0]?.content as { parts?: GooglePart[] } | undefined)?.parts; + if (!Array.isArray(parts)) return "keep"; + let anyReal = false; + let anyProxy = false; + const keep: boolean[] = []; + let call = 0; + for (const part of parts) { + if (part && typeof part === "object" && part.functionCall) { + const real = realIndexes.has(callIndexes[call] ?? -1); + call += 1; + keep.push(real); + if (real) anyReal = true; + else anyProxy = true; + } else { + keep.push(true); + } + } + if (!anyProxy) return "keep"; + if (!anyReal) return null; + return cloneChunk(parsed, { parts: parts.filter((_, i) => keep[i]) }); +} + +function dataLine(frame: string): string | null { + for (const line of frame.split("\n")) { + if (line.startsWith("data:")) return line.slice(5).trim(); + } + return null; +} + +interface GoogleChunk { + /** The chunk's own SSE text, or a synthesized one in JSON-array mode. */ + frame: string; + json: string; +} + +// Gemini streams either as SSE (`?alt=sse`, what the real client asks for) or, +// without that query parameter, as one JSON array whose elements are the same +// chunk objects. Both forms are read here: the first non-whitespace byte of the +// body decides which, and array elements are emitted as they complete so text +// still reaches the client incrementally. +async function* iterGoogleChunks(stream: ReadableStream): AsyncGenerator { + const reader = stream.getReader(); + const decoder = new TextDecoder(); + let buf = ""; + let mode: "unknown" | "sse" | "array" = "unknown"; + let cursor = 0; + let started = false; + let elementStart = -1; + let depth = 0; + let inString = false; + let escaped = false; + let arrayDone = false; + try { + while (true) { + let done: boolean; + let value: Uint8Array | undefined; + try { + ({ done, value } = await reader.read()); + } catch { + break; + } + if (done) break; + buf += decoder.decode(value, { stream: true }); + if (mode === "unknown") { + const lead = buf.replace(/^[\uFEFF\s]+/, ""); + if (lead.length === 0) continue; + if (lead.startsWith("[")) { + mode = "array"; + cursor = buf.indexOf("[") + 1; + } else { + mode = "sse"; + } + } + if (mode === "sse") { + buf = buf.replace(/\r\n|\r/g, "\n"); + let idx: number; + while ((idx = buf.indexOf("\n\n")) >= 0) { + const raw = buf.slice(0, idx); + buf = buf.slice(idx + 2); + const json = dataLine(raw); + if (json !== null) yield { frame: raw, json }; + } + continue; + } + for (; cursor < buf.length && !arrayDone; cursor++) { + const ch = buf[cursor]; + if (inString) { + if (escaped) escaped = false; + else if (ch === "\\") escaped = true; + else if (ch === '"') inString = false; + continue; + } + if (!started) { + if (ch === "," || /\s/.test(ch)) continue; + if (ch === "]") { + arrayDone = true; + break; + } + started = true; + elementStart = cursor; + } + if (ch === '"') inString = true; + else if (ch === "{" || ch === "[") depth += 1; + else if (ch === "}" || ch === "]") { + depth -= 1; + if (depth === 0) { + const json = buf.slice(elementStart, cursor + 1); + started = false; + elementStart = -1; + yield { frame: `data: ${json}`, json }; + } + } + } + } + if (mode !== "array") { + buf = buf.replace(/\r\n|\r/g, "\n"); + const json = dataLine(buf); + if (json !== null) yield { frame: buf, json }; + } + } finally { + reader.releaseLock(); + } +} + +export function createGoogleAdapter( + requestBody: Record, + clientSystem?: string, + absorbName?: string, + model?: string, +): CompressLoopAdapter { + // Gemini carries the model in the URL path, not the body, so the server + // hands it in; the body lookup only covers hand-built requests. + const modelVersion = model ?? (typeof requestBody.model === "string" ? requestBody.model : undefined); + + const buildChunk = ( + parts: GooglePart[], + extra: { candidate?: Record; top?: Record } = {}, + ): Buffer => + sseFrame({ + ...(modelVersion ? { modelVersion } : {}), + candidates: [{ content: { role: "model", parts }, index: 0, ...(extra.candidate ?? {}) }], + ...(extra.top ?? {}), + }); + + const buildToolArgs = (args: string): unknown => { + if (args.length === 0) return {}; + try { + const parsed: unknown = JSON.parse(args); + return parsed === null || typeof parsed !== "object" ? {} : parsed; + } catch { + loggerLog("warn", "[google-adapter] tool-call arguments were not valid JSON; emitting an empty args object"); + return {}; + } + }; + + return { + buildRequest(coreMessages, systemPrompt, body) { + const rebuilt: Record = { ...body, contents: coreToGoogle(coreMessages) }; + // The model lives in the URL path, `alt=sse` in the query, and the + // output cap in generationConfig.maxOutputTokens — a body carrying + // any of these keys is rejected upstream. + delete rebuilt.model; + delete rebuilt.stream; + delete rebuilt.max_tokens; + const parts = [clientSystem, systemPrompt] + .filter((p): p is string => typeof p === "string" && p.length > 0) + .map((text) => ({ text })); + if (parts.length > 0) rebuilt.systemInstruction = { parts }; + return rebuilt; + }, + + async *parseStream(upstream, _round) { + const pending = new Map(); + const rawCallChunks: { json: string; parsed: Record; callIndexes: number[] }[] = []; + // #206/#717: strip model-imitated render tags and forged ACP + // confirmation markers from text parts; both filters may hold back a + // short tail, flushed on finish (and at stream end). + const tagFilter = composeStreamFilters( + createTagEchoFilter((snippet) => { + loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + }), + createMarkerLineFilter((snippet) => { + loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + }), + ); + const flushFilter = function* (): Generator { + const tail = tagFilter.flush(); + if (tail.length > 0) { + yield { kind: "text", delta: tail, raw: buildChunk([{ text: tail }]) } as ParsedStreamEvent; + } + }; + let callIndex = 0; + let sawReasoning = false; + let toolCallsEmitted = 0; + let degenerateWarned = false; + let lastUsage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number } | null = null; + let usageEmitted = false; + let sawDone = false; + let sawRealToolCall = false; + const maybeWarnDegenerate = (reason: string | undefined) => { + if (degenerateWarned) return; + const msg = degenerateTurnWarning({ + reason, + terminalReason: "STOP", + toolCalls: toolCallsEmitted, + text: tagFilter.stats(), + sawThinking: sawReasoning, + wire: "google", + }); + if (msg) { + degenerateWarned = true; + loggerLog("warn", msg); + } + }; + const usageEvent = (): ParsedStreamEvent => ({ kind: "usage", ...(lastUsage ?? {}) }) as ParsedStreamEvent; + const flushPendingAsStructured = function* (): Generator { + for (const [, tc] of pending) { + if (tc.name.length > 0 || tc.id.length > 0) { + toolCallsEmitted++; + yield { + kind: "tool_call", + name: tc.name, + callId: tc.id, + arguments: tc.args, + ...(tc.signature ? { signature: tc.signature } : {}), + } as ParsedStreamEvent; + } + } + pending.clear(); + }; + // Decide proxy-vs-real from the buffered calls and emit events: real + // calls → raw part replay (verbatim parts, original signatures/ids) + // + passthrough-flagged structured events so the loop counts them; + // proxy calls → structured events (executed server-side). + const settleToolCalls = function* (): Generator { + const realIndexes = new Set(); + for (const [idx, tc] of pending) { + // absorb joins PROXY_TOOL_SET dynamically: its name is + // configurable per session, and misclassifying it as real + // would raw-replay its part to the client. + if (tc.name.length > 0 && !PROXY_TOOL_SET.has(tc.name) && tc.name !== absorbName) realIndexes.add(idx); + } + sawRealToolCall = realIndexes.size > 0; + if (!sawRealToolCall) { + yield* flushPendingAsStructured(); + return; + } + for (const [idx, tc] of pending) { + if (realIndexes.has(idx) && tc.name.length > 0) { + toolCallsEmitted++; + yield { + kind: "tool_call", + name: tc.name, + callId: tc.id, + arguments: tc.args, + passthrough: true, + ...(tc.signature ? { signature: tc.signature } : {}), + } as ParsedStreamEvent; + } + } + for (const { json, parsed, callIndexes } of rawCallChunks) { + const filtered = filterRealCallChunk(parsed, realIndexes, callIndexes); + if (filtered === "keep") { + yield { kind: "meta", chunk: Buffer.from(`data: ${json}\n\n`, "utf8") } as ParsedStreamEvent; + } else if (filtered !== null) { + yield { kind: "meta", chunk: sseFrame(filtered) } as ParsedStreamEvent; + } + } + for (const [idx, tc] of pending) { + if (!realIndexes.has(idx) && tc.name.length > 0) { + toolCallsEmitted++; + yield { + kind: "tool_call", + name: tc.name, + callId: tc.id, + arguments: tc.args, + ...(tc.signature ? { signature: tc.signature } : {}), + } as ParsedStreamEvent; + } + } + pending.clear(); + }; + + for await (const { frame, json } of iterGoogleChunks(upstream)) { + let parsed: Record; + try { + parsed = JSON.parse(json) as Record; + } catch { + continue; + } + const rawBuf = Buffer.from(`${frame}\n\n`, "utf8"); + const candidates = parsed.candidates as Array> | undefined; + const candidate = candidates?.[0]; + const content = candidate?.content as { parts?: GooglePart[] } | undefined; + const parts = Array.isArray(content?.parts) ? content.parts : []; + const finishReason = typeof candidate?.finishReason === "string" ? candidate.finishReason : undefined; + const feedback = parsed.promptFeedback as { blockReason?: unknown } | undefined; + const blockReason = typeof feedback?.blockReason === "string" ? feedback.blockReason : undefined; + const usageMetadata = parsed.usageMetadata as Record | undefined; + if (usageMetadata) lastUsage = readUsageMetadata(usageMetadata); + + // A prompt the safety filter refused up front carries no + // candidate at all; the client needs the reason as a terminal. + if (blockReason && !candidate && !sawDone) { + yield* flushFilter(); + sawDone = true; + yield { kind: "meta", chunk: rawBuf } as ParsedStreamEvent; + yield { kind: "done", finishReason: blockReason } as ParsedStreamEvent; + continue; + } + if (!candidate) { + if (usageMetadata && !usageEmitted) { + usageEmitted = true; + yield usageEvent(); + } else if (!usageMetadata) { + yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; + } + continue; + } + + const callIndexes: number[] = []; + // Whether THIS chunk carries a functionCall part anywhere, not + // only before the part being forwarded: a chunk that will be + // replayed whole at settle must not also hand the client its + // text or reasoning now, and its sibling call parts must not + // ride along in an edited copy (a proxy call would leak there + // even when settle drops the chunk). + const hasCallPart = parts.some((p) => p && typeof p === "object" && p.functionCall !== undefined); + let emitted = false; + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; + if (!part || typeof part !== "object") continue; + const fc = part.functionCall as { name?: unknown; args?: unknown; id?: unknown } | undefined; + if (fc) { + const idx = callIndex++; + callIndexes.push(idx); + pending.set(idx, { + index: idx, + id: typeof fc.id === "string" ? fc.id : "", + name: typeof fc.name === "string" ? fc.name : "", + args: typeof fc.args === "string" ? fc.args : JSON.stringify(fc.args ?? {}), + signature: typeof part.thoughtSignature === "string" ? part.thoughtSignature : "", + }); + continue; + } + if (typeof part.text !== "string" || part.text.length === 0) continue; + if (part.thought === true) { + sawReasoning = true; + emitted = true; + const reasoningRaw = !hasCallPart ? (finishReason ? sseFrame(cloneChunk(parsed, { dropFinishReason: true })) : rawBuf) : undefined; + yield { + kind: "reasoning", + delta: part.text, + ...(reasoningRaw ? { raw: reasoningRaw } : {}), + ...(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0 + ? { signature: part.thoughtSignature } + : {}), + } as ParsedStreamEvent; + continue; + } + const clean = tagFilter.push(part.text); + if (clean.length === 0) continue; + emitted = true; + let raw: Buffer | undefined; + if (clean === part.text) { + // A chunk that also carries functionCall parts is replayed + // whole at settle, so its text must not be forwarded twice. + if (!hasCallPart) raw = finishReason ? sseFrame(cloneChunk(parsed, { dropFinishReason: true })) : rawBuf; + } else { + raw = sseFrame(cloneChunk(parsed, { + parts: parts + .map((p, j) => (j === i ? { ...p, text: clean } : p)) + .filter((p) => !p || typeof p !== "object" || p.functionCall === undefined), + dropFinishReason: true, + })); + } + yield { kind: "text", delta: clean, ...(raw ? { raw } : {}) } as ParsedStreamEvent; + } + if (callIndexes.length > 0) rawCallChunks.push({ json, parsed, callIndexes }); + if (!emitted && callIndexes.length === 0 && !finishReason && parts.length === 0) { + yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; + } + + if (finishReason && !sawDone) { + yield* flushFilter(); + yield* settleToolCalls(); + if (lastUsage && !usageEmitted) { + usageEmitted = true; + yield usageEvent(); + } + sawDone = true; + maybeWarnDegenerate(finishReason); + const truncated = finishReason === "MAX_TOKENS"; + if (sawRealToolCall) { + // A finish chunk that also carried the functionCall was + // already replayed by settleToolCalls; re-sending it + // verbatim would duplicate the call on the wire. + const finishChunk = parts.some((p) => p && typeof p === "object" && p.functionCall !== undefined) + ? sseFrame(cloneChunk(parsed, { parts: [] })) + : rawBuf; + yield { kind: "meta", chunk: finishChunk } as ParsedStreamEvent; + yield { kind: "done", finishReason, suppressCompletion: true, ...(truncated ? { truncated: true } : {}) } as ParsedStreamEvent; + } else { + yield { kind: "done", finishReason, ...(truncated ? { truncated: true } : {}) } as ParsedStreamEvent; + } + } + } + + // A stream that died mid-flight still has to hand the client the + // usage it reported, and a held-back filter tail must not be lost. + yield* flushFilter(); + if (!sawDone && lastUsage && !usageEmitted) { + usageEmitted = true; + yield usageEvent(); + } + }, + + emitText(delta) { + return buildChunk([{ text: delta }]); + }, + + emitReasoning(delta) { + return buildChunk([{ text: delta, thought: true }]); + }, + + emitToolCall(call: ToolCallEmit) { + return buildChunk([{ + functionCall: { + name: call.name, + args: buildToolArgs(call.arguments), + ...(call.callId ? { id: call.callId } : {}), + }, + ...(call.signature ? { thoughtSignature: call.signature } : {}), + }]); + }, + + emitMarker(toolName, result) { + return buildChunk([{ text: buildVisibilityMarker(toolName, result) }]); + }, + + emitCompletion(opts?: EmitCompletionOpts) { + const usage = opts?.usage + ? { + usageMetadata: { + promptTokenCount: opts.usage.inputTokens ?? 0, + candidatesTokenCount: opts.usage.outputTokens ?? 0, + totalTokenCount: (opts.usage.inputTokens ?? 0) + (opts.usage.outputTokens ?? 0), + ...(typeof opts.usage.cachedTokens === "number" + ? { cachedContentTokenCount: opts.usage.cachedTokens } + : {}), + }, + } + : {}; + return buildChunk([], { candidate: { finishReason: googleFinishReason(opts?.finishReason) }, top: usage }); + }, + + emitError(message) { + return sseFrame({ error: { code: 500, message, status: "INTERNAL" } }); + }, + }; +} diff --git a/src/loop/core.ts b/src/loop/core.ts index 616786d6f1..1a1eacba9f 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -114,7 +114,7 @@ export interface RequestOptions { export type ParsedStreamEvent = | { kind: "text"; delta: string; raw?: Buffer } | { kind: "reasoning"; delta: string; raw?: Buffer; signature?: string; blockEnd?: boolean } - | { kind: "tool_call"; name: string; callId: string; arguments: string; passthrough?: boolean } + | { kind: "tool_call"; name: string; callId: string; arguments: string; passthrough?: boolean; signature?: string } | { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number } | { kind: "done"; finishReason?: string; suppressCompletion?: boolean; truncated?: boolean; thinking?: boolean } | { kind: "error"; message: string } @@ -130,6 +130,10 @@ export interface ToolCallEmit { callId: string; arguments: string; passthrough?: boolean; + /** Protocol signature for the call part (Gemini thoughtSignature). A + * reconstructed functionCall replayed without it is rejected by Gemini 3, + * so it rides the call through to the re-request and to emitToolCall. */ + signature?: string; } export interface ExtractedTextTriggers { @@ -351,7 +355,7 @@ export async function* runCompressLoop( } } } else if (ev.kind === "tool_call") { - calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough }); + calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough, signature: ev.signature }); } else if (ev.kind === "usage") { usage = { inputTokens: ev.inputTokens, @@ -567,7 +571,7 @@ export async function* runCompressLoop( let realCalls = 0; const realToolCalls: ToolCallEmit[] = []; - const proxyResults: { name: string; callId: string; result: string; arguments: string }[] = []; + const proxyResults: { name: string; callId: string; result: string; arguments: string; signature?: string }[] = []; for (const call of allCalls) { if (isProxyToolFor(call.name, ctx.session, ctx.config)) { @@ -578,7 +582,7 @@ export async function* runCompressLoop( parsedArgs = {}; } const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, parsedArgs, ctx, call.callId)); - proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments }); + proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments, signature: call.signature }); if (ctx.visibilityMarkers !== false) yield adapter.emitMarker(call.name, result); } else { realToolCalls.push(call); @@ -604,15 +608,17 @@ export async function* runCompressLoop( // never in coreMessages), and hideConsumedCompressCalls runs each // round so consumed compress records cannot re-prime the model. if (proxyResults.length > 0) { - // #539: only Anthropic verifies thinking+signature pairs, so it alone - // needs a signature to replay a thinking block. OpenAI/DeepSeek and - // Responses echo reasoning_content back verbatim and emit no signature - // — gating on one dropped every such round's reasoning from the - // re-request, leaving the proxy-tool assistant message without - // reasoning_content (DeepSeek 400 invalid_request_error: "reasoning_ - // content ... must be passed back"). Relies on the invariant that the - // single production call site (server.ts) always populates ctx.protocol. - const requiresThinkingSignature = ctx.protocol === "anthropic"; + // #539: only wires that VERIFY thinking+signature pairs need a + // signature to replay a thinking block — Anthropic and Gemini 3 + // (its thoughtSignature is rejected when dropped, same fatality). + // OpenAI/DeepSeek and Responses echo reasoning_content back + // verbatim and emit no signature — gating on one dropped every + // such round's reasoning from the re-request, leaving the + // proxy-tool assistant message without reasoning_content + // (DeepSeek 400 invalid_request_error: "reasoning_content ... + // must be passed back"). Relies on the invariant that the single + // production call site (server.ts) always populates ctx.protocol. + const requiresThinkingSignature = ctx.protocol === "anthropic" || ctx.protocol === "google"; if (reasoningSegments.length > 0) { for (let i = 0; i < reasoningSegments.length; i++) { const seg = reasoningSegments[i]; @@ -623,7 +629,11 @@ export async function* runCompressLoop( contentType: "reasoning", text: seg.text, reasoningContent: seg.text, - ...(seg.signature.length > 0 ? { thinkingSignature: seg.signature } : {}), + ...(seg.signature.length > 0 + ? ctx.protocol === "google" + ? { googleThoughtSignature: seg.signature } + : { thinkingSignature: seg.signature } + : {}), }; coreMessages.push(reasoningMsg); } @@ -645,11 +655,13 @@ export async function* runCompressLoop( toolName: pr.name, toolCallId: pr.callId, text: pr.arguments, + ...(ctx.protocol === "google" && pr.signature ? { googleThoughtSignature: pr.signature } : {}), }); coreMessages.push({ id: `acp_loop_r${round}_tool_${pr.callId}`, role: "tool", contentType: "tool-result", + toolName: pr.name, toolCallId: pr.callId, text: pr.result, }); @@ -795,16 +807,18 @@ export async function* runCompressLoop( respResult = await fetchUpstream(newBody); } catch (e) { // #539 follow-up: stripping replayed thinking only recovers a - // backend where thinking is OPTIONAL (Anthropic). On - // OpenAI/DeepSeek thinking mode reasoning_content is MANDATORY, - // so a strip-retry there guarantees another 400 plus a - // misleading log — gate the degraded retry to Anthropic. + // backend where thinking is OPTIONAL (Anthropic, and Gemini — + // its thoughtSignature check is what a stripped replay drops). + // On OpenAI/DeepSeek thinking mode reasoning_content is + // MANDATORY, so a strip-retry there guarantees another 400 plus + // a misleading log — gate the degraded retry to the wires that + // verify signatures. if ( !(e instanceof UpstreamHttpError) || e.status < 400 || e.status >= 500 || degradedRetried || - ctx.protocol !== "anthropic" || + (ctx.protocol !== "anthropic" && ctx.protocol !== "google") || !coreMessages.some(isLoopThinking) ) { throw e; diff --git a/src/loop/index.ts b/src/loop/index.ts index c8af79e24a..838df51f16 100644 --- a/src/loop/index.ts +++ b/src/loop/index.ts @@ -11,24 +11,28 @@ export type { export { createResponsesAdapter } from "./adapter-responses.js"; export { createOpenaiAdapter } from "./adapter-openai.js"; export { createAnthropicAdapter } from "./adapter-anthropic.js"; +export { createGoogleAdapter } from "./adapter-google.js"; import { createResponsesAdapter } from "./adapter-responses.js"; import { createOpenaiAdapter } from "./adapter-openai.js"; import { createAnthropicAdapter } from "./adapter-anthropic.js"; +import { createGoogleAdapter } from "./adapter-google.js"; import type { CompressLoopAdapter } from "./core.js"; import type { ResponsesProjection } from "acp-kernel/wire"; import type { AnthropicRequestBody } from "acp-kernel/wire"; export function pickAdapter( - protocol: "responses" | "openai" | "anthropic", + protocol: "responses" | "openai" | "anthropic" | "google", requestBody: Record, textProtocol?: boolean, responsesProjection?: ResponsesProjection, anthropicSystem?: AnthropicRequestBody["system"], openaiSystem?: string, absorbName?: string, + google?: { system?: string; model?: string }, ): CompressLoopAdapter { if (protocol === "responses") return createResponsesAdapter(textProtocol, responsesProjection, absorbName); if (protocol === "openai") return createOpenaiAdapter(requestBody, openaiSystem, absorbName); if (protocol === "anthropic") return createAnthropicAdapter(requestBody, anthropicSystem); + if (protocol === "google") return createGoogleAdapter(requestBody, google?.system, absorbName, google?.model); throw new Error(`[acp-loop] unknown protocol: ${protocol}`); } diff --git a/src/persist.ts b/src/persist.ts index e89b463057..3137aa0b28 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -9,6 +9,7 @@ import { createSessionCodec, ENCRYPT_MAGIC, parseEncryptionKey } from "./encrypt import { PersistEpermAlert } from "./persist-eperm.js"; import { createInitialState, defaultCountTokens, prune, type CompressionState, type CoreMessage } from "acp-kernel"; import type { Session, BlockContent, BlockView } from "./session.js"; +import type { WireProtocol } from "./util.js"; /** * On-disk persistence for proxy sessions. @@ -80,7 +81,7 @@ interface PersistedSession { /** Identity / descriptive metadata (v2+). Absent on v1 files; read via the * flat fallbacks below. */ meta?: { - protocol?: "anthropic" | "openai" | "responses"; + protocol?: WireProtocol; upstreamOrigin?: string; label?: string; title?: string; @@ -103,7 +104,7 @@ interface PersistedSession { createdAt: number; // Legacy flat fields (v1). Kept optional only so buildSession can read // older files; v2 records emit grouped meta/stats instead. - protocol?: "anthropic" | "openai" | "responses"; + protocol?: WireProtocol; upstreamOrigin?: string; label?: string; requests?: number; diff --git a/src/plugin.ts b/src/plugin.ts index 4be1ae227f..e799ffbb5c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -10,7 +10,7 @@ import { effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; import { effectiveRulesConfig } from "./rules-feature.js"; import { executeProxyTool } from "./loop/core.js"; import { normalizeSseLineEndings } from "./sse-util.js"; -import { composeStreamFilters, containsMarkerLineText, containsRenderTagText, createMarkerLineFilter, createTagEchoFilter, mayStartMarkerLine, mayStartRenderTag, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js"; +import { composeStreamFilters, containsMarkerLineText, containsRenderTagText, createMarkerLineFilter, createTagEchoFilter, mayStartMarkerLine, mayStartRenderTag, stripAcpTags, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js"; import { log as loggerLog } from "./logger.js"; import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; import { degenerateTurnWarning } from "./degenerate-turn.js"; @@ -801,6 +801,23 @@ function num(v: unknown): number | undefined { return typeof v === "number" && Number.isFinite(v) ? v : undefined; } +/** Gemini reports usage in a top-level `usageMetadata` object — identically on + * an SSE chunk and on a non-streaming body: `promptTokenCount` is the whole + * context (the `cachedContentTokenCount` prefix included) and thinking tokens + * are billed ON TOP of the candidate's, so both count as output. */ +function googleUsageSample(obj: Record): UsageSample | undefined { + const meta = obj["usageMetadata"]; + if (!meta || typeof meta !== "object") return undefined; + const u = meta as Record; + const cand = num(u["candidatesTokenCount"]); + const thoughts = num(u["thoughtsTokenCount"]); + return { + inputTokens: num(u["promptTokenCount"]), + outputTokens: cand === undefined && thoughts === undefined ? undefined : (cand ?? 0) + (thoughts ?? 0), + cachedTokens: num(u["cachedContentTokenCount"]), + }; +} + function usageFromSseEvent(obj: Record): UsageSample | undefined { const type = obj["type"]; if (type === "message_start") { @@ -848,6 +865,8 @@ function usageFromSseEvent(obj: Record): UsageSample | undefine cachedTokens: num((usage["input_tokens_details"] as Record | undefined)?.["cached_tokens"]), }; } + const google = googleUsageSample(obj); + if (google) return google; const usage = obj["usage"] as Record | undefined; if (usage && (num(usage["prompt_tokens"]) !== undefined || num(usage["completion_tokens"]) !== undefined)) { return { @@ -1001,6 +1020,21 @@ export async function pipePluginChatWithStrip( const deltaType = s.field === "text" ? "text_delta" : s.field === "thinking" ? "thinking_delta" : "input_json_delta"; return `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: s.index, delta: { type: deltaType, [s.field]: tail } })}\n\n`; } + if (protocol === "google") { + // The tail rides a synthesized candidate chunk. It must REPEAT the + // finishReason observed so far (lastChunkMeta, set by processGoogle) + // because the Gemini client throws when the stream's final chunk + // carries none — flushing a held tail after the finishReason frame + // would otherwise end the stream reason-less. + const part = s.field === "thinking" ? { thought: true, text: tail } : { text: tail }; + const candidate = { + index: s.index, + content: { role: "model", parts: [part] }, + ...(typeof lastChunkMeta["finishReason"] === "string" ? { finishReason: lastChunkMeta["finishReason"] } : {}), + }; + const frame = typeof lastChunkMeta["modelVersion"] === "string" ? { modelVersion: lastChunkMeta["modelVersion"], candidates: [candidate] } : { candidates: [candidate] }; + return `data: ${JSON.stringify(frame)}\n\n`; + } if (s.toolIndex !== undefined) { return `data: ${JSON.stringify({ ...lastChunkMeta, object: "chat.completion.chunk", choices: [{ index: s.choice ?? 0, delta: { tool_calls: [{ index: s.toolIndex, function: { arguments: tail } }] } }] })}\n\n`; } @@ -1157,7 +1191,7 @@ export async function pipePluginChatWithStrip( } const msg = degenerateTurnWarning({ reason: finalFinishReason, - terminalReason: protocol === "anthropic" ? "end_turn" : "stop", + terminalReason: protocol === "anthropic" ? "end_turn" : protocol === "google" ? "STOP" : "stop", toolCalls: sawToolUse ? 1 : 0, text: { inputChars, outputChars: visibleTextChars, dropped }, sawThinking, @@ -1303,6 +1337,101 @@ export async function pipePluginChatWithStrip( if (clean.length === 0 && Object.keys(d ?? {}).length <= 2) return ""; return rebuildEvent(rawEvent, { ...ev, delta: { ...d, [field]: clean } }); }; + /** Tag-echo state machine for the Gemini wire: prose AND reasoning live in + * `candidates[*].content.parts[*].text` (a `thought:true` part is the + * thinking). Multi-candidate streams and frames interleaving thought/text + * parts are handled the way the OpenAI/Anthropic processors handle + * choices/content blocks — one filter keyed per (candidate index, field) — + * and a `finishReason` chunk is this wire's terminal event. */ + const processGoogle = (ev: Record, rawEvent: string): string => { + if (typeof ev["modelVersion"] === "string") lastChunkMeta = { ...lastChunkMeta, modelVersion: ev["modelVersion"] }; + // A top-level `error` object is Gemini's other terminal shape (HTTP-level + // failures arrive mid-stream too): the client throws on it, so the turn + // is over and must not also be reported as an upstream truncation. + if (ev["error"] !== undefined) sawTerminal = true; + const candidates = ev["candidates"]; + if (!Array.isArray(candidates)) { + return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n"; + } + let rebuilt: Record | null = null; + let droppedText = false; + let keptText = false; + let hadText = false; + for (let ci = 0; ci < candidates.length; ci++) { + const cand = candidates[ci] as Record | null; + if (!cand || typeof cand !== "object") continue; + if (typeof cand["finishReason"] === "string") { + // Gemini's terminal event IS this chunk — the client throws when + // a stream ends without one, so `lastChunkMeta` carries it for + // the synthetic tail (see syntheticTail). + finalFinishReason = cand["finishReason"] as string; + sawTerminal = true; + lastChunkMeta = { ...lastChunkMeta, finishReason: finalFinishReason }; + } + const content = cand["content"]; + if (!content || typeof content !== "object") continue; + const parts = (content as Record)["parts"]; + if (!Array.isArray(parts)) continue; + const index = typeof cand["index"] === "number" ? cand["index"] : ci; + for (let pi = 0; pi < parts.length; pi++) { + const p = parts[pi] as Record | null; + if (!p || typeof p !== "object") continue; + if (p["functionCall"] !== undefined) sawToolUse = true; + if (p["thought"] === true) sawThinking = true; + const raw = p["text"]; + if (typeof raw !== "string") continue; + hadText = true; + // A reasoning part streams through the same machine under its own + // field, so an interleaved thought/text pair in one frame never + // shares held-back state. + const field = p["thought"] === true ? "thinking" : "text"; + if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !anyPending()) { + if (raw.length > 0) keptText = true; + if (field === "text") visibleTextChars += raw.length; + continue; + } + const [clean, changed] = pushField(field, index, raw); + if (clean.length === 0) droppedText = true; + else { + keptText = true; + if (field === "text") visibleTextChars += clean.length; + } + if (changed) { + // Deep enough clone of the frame's candidates that the + // rebuilt part's text can be replaced without mutating the + // parsed event (callers keep `ev` for logs/usage). + if (!rebuilt) { + rebuilt = { + ...ev, + candidates: candidates.map((c) => { + if (!c || typeof c !== "object") return c; + const cand = { ...(c as Record) }; + const cont = cand["content"]; + if (cont && typeof cont === "object" && Array.isArray((cont as Record)["parts"])) { + const cc = cont as Record; + cand["content"] = { ...cc, parts: (cc["parts"] as unknown[]).map((q) => (q && typeof q === "object" ? { ...(q as Record) } : q)) }; + } + return cand; + }), + }; + } + const rparts = (((rebuilt["candidates"] as Record[])[ci]["content"] as Record)["parts"] as Record[]); + rparts[pi] = { ...rparts[pi], text: clean }; + } + } + } + if (rebuilt) { + // Text carried was entirely stripped away: drop the whole chunk + // instead of forwarding an empty text part — but only when EVERY + // managed part emptied out and the frame carries nothing else (a + // finishReason / functionCall / usageMetadata sibling must survive, + // the chat processor's #463 rule). + if (droppedText && !keptText && !googleFrameHasNonText(rebuilt)) return ""; + return rebuildEvent(rawEvent, rebuilt); + } + if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n"; + return rawEvent + "\n\n"; + }; try { for (;;) { const { done, value } = await reader.read(); @@ -1360,7 +1489,9 @@ export async function pipePluginChatWithStrip( // the uncompressed baseline. Patch `ev` BEFORE the tag-echo // processors run and only rebuild when they return the event // verbatim — otherwise their render-tag stripping is lost. - const out = protocol === "anthropic" ? processAnthropic(ev, rawEvent) : processOpenai(ev, rawEvent); + const out = protocol === "anthropic" ? processAnthropic(ev, rawEvent) + : protocol === "google" ? processGoogle(ev, rawEvent) + : processOpenai(ev, rawEvent); // The gate runs AFTER this event is processed: a coalesced chunk // can carry content AND the finish reason, so its own text has to // count before the turn may be called empty. The event's output is @@ -1436,6 +1567,32 @@ function hadTextOtherThanTextFields(choices: unknown): boolean { return false; } +/** The Gemini counterpart of hadTextOtherThanTextFields: does a frame whose + * text parts were all stripped still carry something the client needs? A + * finishReason, a functionCall/functionResponse part, usageMetadata or + * promptFeedback keeps the chunk alive; a chunk that only ever held + * render-tag echo is dropped whole. */ +function googleFrameHasNonText(ev: Record): boolean { + if (ev["usageMetadata"] !== undefined || ev["promptFeedback"] !== undefined) return true; + const candidates = ev["candidates"]; + if (!Array.isArray(candidates)) return true; + for (const c of candidates) { + if (!c || typeof c !== "object") continue; + const cand = c as Record; + if (typeof cand["finishReason"] === "string") return true; + const content = cand["content"]; + const parts = content && typeof content === "object" ? (content as Record)["parts"] : undefined; + if (!Array.isArray(parts)) continue; + for (const p of parts) { + if (!p || typeof p !== "object") continue; + for (const k of Object.keys(p as Record)) { + if (k !== "text" && k !== "thought" && k !== "thoughtSignature") return true; + } + } + } + return false; +} + /** * Plugin-mode Responses passthrough with render-tag stripping (#206 parity * for VERBATIM plugin streams). In plugin mode the native tool loop owns the @@ -1909,6 +2066,50 @@ function rebuildEvent(rawEvent: string, ev: Record): string { return replaced ? out.join("\n") + "\n\n" : `data: ${JSON.stringify(ev)}\n\n`; } +// Plugin-passthrough parity for the Gemini wire: strip the model prose that +// lives in `candidates[*].content.parts[*].text` — both plain text parts and +// `thought:true` reasoning parts share that one field. Mirrors +// stripOpenaiChatText / stripAnthropicText; mutates in place (the returned +// reference is the input's). +function stripGoogleChunk(obj: T): T { + if (!obj || typeof obj !== "object") return obj; + const o = obj as Record; + const candidates = o["candidates"]; + if (!Array.isArray(candidates)) return obj; + o["candidates"] = candidates.map((c) => { + if (!c || typeof c !== "object") return c; + const cand = c as Record; + const content = cand["content"]; + if (!content || typeof content !== "object") return c; + const cont = content as Record; + if (!Array.isArray(cont["parts"])) return c; + return { + ...cand, + content: { + ...cont, + parts: (cont["parts"] as unknown[]).map((p) => + p && typeof p === "object" && typeof (p as Record)["text"] === "string" + ? { ...(p as Record), text: stripAcpTags((p as Record)["text"] as string) } + : p, + ), + }, + }; + }); + return obj; +} + +// Without `alt=sse` a Gemini streaming response is a JSON ARRAY of the same +// chunk objects, so the non-stream strip applies to each element as well. +function stripGoogleText(obj: T): T { + if (Array.isArray(obj)) { + obj.forEach((c) => { + stripGoogleChunk(c); + }); + return obj; + } + return stripGoogleChunk(obj); +} + export async function pipePluginJson( stream: ReadableStream, res: import("node:http").ServerResponse, @@ -1959,6 +2160,17 @@ export async function pipePluginJson( markDirty(session); } } + // Gemini reports its usage in a top-level `usageMetadata` instead of + // `usage` (same object on a non-streaming body as on an SSE chunk) — + // feed it too, so a `:generateContent` turn also anchors + // lastInputTokens for the nudge/fit decisions. + if (session && !usage) { + const sample = googleUsageSample(json); + if (sample && sample.inputTokens !== undefined) { + applyUsageSample(session, sample, protocol); + markDirty(session); + } + } } catch { /* non-JSON body — forward verbatim */ } if (json && (containsRenderTagText(text) || containsMarkerLineText(text))) { // #206 parity for the non-streaming plugin path: the compress loop's @@ -1966,7 +2178,10 @@ export async function pipePluginJson( // JSON response would re-feed the model's tag echoes. Strips mutate in // place, so this composes with the #408 usage backfill above — one // parse, one reserialize. - json = protocol === "responses" ? stripResponsesText(json) : protocol === "anthropic" ? stripAnthropicText(json) : stripOpenaiChatText(json); + json = protocol === "responses" ? stripResponsesText(json) + : protocol === "anthropic" ? stripAnthropicText(json) + : protocol === "google" ? stripGoogleText(json) + : stripOpenaiChatText(json); mutated = true; } if (mutated && json) { diff --git a/src/preflight.ts b/src/preflight.ts index da327b8d59..df7b50f5bf 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -56,7 +56,7 @@ export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16; // incident class behind #868 (a 1.39x-window payload); it is a fixed depth, // not scaled to the overshoot — scaling it is a separate design question. -export type PreflightProtocol = "anthropic" | "openai" | "responses"; +export type PreflightProtocol = "anthropic" | "openai" | "responses" | "google"; export interface PreflightDeps { core: CompressionCore; @@ -300,6 +300,17 @@ function summaryPayload(protocol: PreflightProtocol, model: string, system: stri if (protocol === "openai") { return { model, max_tokens: maxOutputTokens, messages: [{ role: "system", content: system }, { role: "user", content }], stream }; } + if (protocol === "google") { + // Gemini carries the model in the request PATH (never in the body) and + // has no `stream` field either — the `:streamGenerateContent` path + // decides. The summary call therefore carries only the conversation + // shape: contents + the system channel, with the output cap living in + // generationConfig (there is no top-level max_tokens). + const payload: Record = { contents: [{ role: "user", parts: [{ text: content }] }] }; + if (system) payload.systemInstruction = { parts: [{ text: system }] }; + if (includeMaxOutputTokens) payload.generationConfig = { maxOutputTokens: MAX_SUMMARY_OUTPUT_TOKENS }; + return payload; + } // #488: codex relays reject Responses calls without store:false ("Store must be set to false"). // #663: max_output_tokens is optional — omit it once the upstream has // rejected the parameter (learned per URL+model); the model's default @@ -363,6 +374,30 @@ function summaryHeaders(deps: PreflightDeps): Record { return headers; } +// Gemini carries the summary text in candidates[0].content.parts[].text. A +// `thought:true` part is the model's reasoning (thinkingConfig), not summary +// output, so it is skipped; several candidates only occur when n>1 is +// requested, which the summary call never is — first candidate wins, mirroring +// the OpenAI/Anthropic extractors. +function googleChunkText(chunk: Record): string { + const candidates = chunk.candidates; + if (!Array.isArray(candidates) || candidates.length === 0) return ""; + const first = candidates[0]; + if (!first || typeof first !== "object") return ""; + const content = (first as Record).content; + if (!content || typeof content !== "object") return ""; + const parts = (content as Record).parts; + if (!Array.isArray(parts)) return ""; + let out = ""; + for (const p of parts) { + if (!p || typeof p !== "object") continue; + const part = p as Record; + if (part.thought === true) continue; + if (typeof part.text === "string") out += part.text; + } + return out; +} + // #780: extraction carries a validity contract — it must separate "the stream // delivered a complete summary" from "the stream died mid-delivery". The naive // accumulator conflated the two: a gateway truncation (#764: half-line data, @@ -377,8 +412,9 @@ function summaryHeaders(deps: PreflightDeps): Record { // seen it is trusted as-is (no framing check on top, so gateways that close // right after the final event without a trailing blank line are safe) // - anthropic/openai do NOT require finish_reason/[DONE]/message_stop (#764: -// real gateways omit these occasionally); the body must at least end on a -// frame boundary (\n\n, CRLF-tolerant), else it may have been cut mid-frame +// real gateways omit these occasionally), and the Gemini wire requires no +// finishReason either; the body must at least end on a frame boundary +// (\n\n, CRLF-tolerant), else it may have been cut mid-frame // A rejected stream returns "" so requestSummary routes it into // diagnoseEmptySummary + the #726 halving/cooldown chain. export function extractSummaryFromSse(protocol: PreflightProtocol, text: string): string { @@ -429,6 +465,8 @@ export function extractSummaryFromSse(protocol: PreflightProtocol, text: string) const delta = (choices[0] as Record).delta as Record | undefined; if (delta && typeof delta.content === "string") out += delta.content; } + } else if (protocol === "google") { + out += googleChunkText(o); } else { if (type === "response.output_text.delta" && typeof o.delta === "string") { out += o.delta; @@ -469,6 +507,12 @@ function extractSummaryText(protocol: PreflightProtocol, json: Record (c && typeof c === "object" ? googleChunkText(c as Record) : "")).join(""); + } if (typeof json.output_text === "string") return json.output_text; const output = json.output; if (!Array.isArray(output)) return ""; @@ -690,6 +734,16 @@ async function requestSummaryBody(deps: PreflightDeps, body: string): Promise { const text = await requestSummaryBody(deps, JSON.stringify(summaryPayload(deps.protocol, deps.model, system, content, stream, includeMaxOutputTokens, safeHost(deps.url), deps.config.modelContextLimit))); let json: unknown; @@ -699,13 +753,15 @@ async function requestSummary(deps: PreflightDeps, system: string, content: stri json = null; } // Streaming bodies are SSE, but a non-conforming upstream may answer a - // stream:true call with plain JSON — accept either shape. + // stream:true call with plain JSON — accept either shape, and likewise for + // a non-stream call answered with SSE (#829). + const sseBody = SSE_DATA_LINE_RE.test(text); const summary = (json && typeof json === "object" ? extractSummaryText(deps.protocol, json as Record) - : stream + : stream || sseBody ? extractSummaryFromSse(deps.protocol, text) : "").trim(); - if (!json && !stream) { + if (!json && !stream && !sseBody) { deps.log("warn", `[preflight] summary response was not JSON: ${text.slice(0, 200)}`); } if (summary.length < MIN_SUMMARY_CHARS) { diff --git a/src/server.ts b/src/server.ts index b31ada215b..d51e3021e1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -48,7 +48,19 @@ import { responsesToCoreWithToolImages as responsesToCore, patchResponsesInputWi import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages, applyCompactionArchive, detectUnannouncedHistoryRewrite, markCompactionBoundary, ensureCanonicalId, storeEffectiveConfig } from "./session.js"; import { detectStaleInstall } from "./update.js"; import { PACKAGE_NAME, VERSION } from "./version.js"; -import { ABSORB_TOOL, ABSORB_TOOL_OPENAI, ABSORB_TOOL_RESPONSES, COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, RULE_TOOL, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js"; +import { + coreToGoogle, + googleToCore, + googleSystemText, + injectGoogleSystem, + conversationSignalGoogle, + type GoogleContent, + type GoogleFunctionDeclaration, + type GoogleRequestBody, + type GoogleSystemInstruction, + type GoogleTool, +} from "acp-kernel/wire"; +import { ABSORB_TOOL, ABSORB_TOOL_GOOGLE, ABSORB_TOOL_OPENAI, ABSORB_TOOL_RESPONSES, COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js"; import { applyAbsorbView, absorbEnabled, absorbToolName, storeEffectiveAbsorb } from "./absorb.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; import { rewriteJsonResponse, type RewriteCtx } from "./stream.js"; @@ -75,6 +87,7 @@ import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeRe import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; import { stripAcpPanelMessages, stripAcpPanelResponsesInput } from "./acp-panel.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; +import { rewriteGoogleJsonResponse } from "./stream-google.js"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitPreflightError, emitStreamError } from "./stream-error.js"; @@ -84,7 +97,7 @@ import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } f import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRuntimeInfoFor, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream, getBlindTunnelStats } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; -import { BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageTotals, type WireProtocol } from "./util.js"; +import { BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageOutputTotal, usageTotals, type WireProtocol } from "./util.js"; import { BILI_TUNNEL_HEADER, checkTunnelDestination, tunnelAllowlistFromEnv } from "./tunnel-guard.js"; import { dumpRejectedBody } from "./error-dump.js"; @@ -98,7 +111,7 @@ import { isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, estimateInputTokens, estimateWireOverhead } from "./server/budget.js"; import { bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./server/stream-io.js"; -export function resolveUpstream(_opts: ProxyOptions, reqUrl: string, req?: http.IncomingMessage): { upstream: string; rewrittenUrl: string; explicitProtocol?: "openai" | "anthropic" | "responses"; tunnel?: boolean } | undefined { +export function resolveUpstream(_opts: ProxyOptions, reqUrl: string, req?: http.IncomingMessage): { upstream: string; rewrittenUrl: string; explicitProtocol?: WireProtocol; tunnel?: boolean } | undefined { // MITM mode: the request arrived over a CONNECT tunnel we terminated // locally (client set HTTP_PROXY and issued CONNECT host:443). The socket // carries the real upstream origin; the request path has no /bili/ prefix @@ -124,10 +137,10 @@ export function resolveUpstream(_opts: ProxyOptions, reqUrl: string, req?: http. // client-side billion-context extensions (billion-context-pi / opencode-acp) // can detect it in their own baseUrl and self-disable, avoiding double // compression. - const KNOWN_PROTOCOLS = ["responses", "anthropic", "openai"] as const; + const KNOWN_PROTOCOLS = ["responses", "anthropic", "openai", "google"] as const; if (reqUrl.startsWith("/bili/")) { let rest = reqUrl.slice(6); - let explicitProtocol: "openai" | "anthropic" | "responses" | undefined; + let explicitProtocol: WireProtocol | undefined; for (const p of KNOWN_PROTOCOLS) { const prefix = `${p}/`; if (rest.startsWith(prefix + "http://") || rest.startsWith(prefix + "https://")) { @@ -233,6 +246,36 @@ function armRequestWatchdog(req: http.IncomingMessage, res: http.ServerResponse, arm(); } +/** Classify a Gemini native request path. The model and the method both live + * in the path, never in the body: `/v1beta/models/:streamGenerateContent` + * (streaming, usually with `alt=sse`), `:generateContent` (single shot) and + * `:countTokens`. Returns null for every other path (model listing, files, + * cachedContents, the OpenAI-compatible `/v1beta/openai/...` mirror). */ +export type GooglePathKind = "stream-generate" | "generate" | "count-tokens"; + +export function googlePathKind(urlPath: string): GooglePathKind | null { + if (urlPath.includes(":streamGenerateContent")) return "stream-generate"; + if (urlPath.includes(":generateContent")) return "generate"; + if (urlPath.includes(":countTokens")) return "count-tokens"; + return null; +} + +/** The Gemini request carries the model in the URL path, never in the body + * (`POST /v1beta/models/gemini-3.8-flash:streamGenerateContent`). Every + * model-keyed decision (window resolution, thresholds, the summarization + * adapter, degenerate-turn analysis) reads it from here instead of + * `parsed.model`. Returns undefined when the path carries no model or an + * undecodable one. */ +export function googleModelFromPath(urlPath: string): string | undefined { + const m = /\/models\/([^/:?]+):(?:streamGenerateContent|generateContent|countTokens)\b/.exec(urlPath); + if (!m || !m[1]) return undefined; + try { + return decodeURIComponent(m[1]); + } catch { + return m[1]; + } +} + export async function startServer(opts: ProxyOptions): Promise { // Configure the tee logger (file + stderr) BEFORE any logging so the very // first line (persist status) lands in the file too. @@ -513,7 +556,7 @@ type Prepared = { * text (collectBlockContent reads message text by id); processedMessages * has compressed messages replaced with placeholders → empty content. */ originalMessages: CoreMessage[]; - protocol: "anthropic" | "openai" | "responses"; + protocol: WireProtocol; stream: boolean; compressInjected: boolean; /** True when the session is driven by a cooperative agent-side plugin @@ -529,6 +572,11 @@ type Prepared = { * openai hoist (0.0.37). The fold space no longer carries it, so every * rebuilt payload and compress-loop round must re-inject it. */ openaiSystemText?: string; + /** Google wire: the client's own `systemInstruction` text (the kernel hoists + * it out of the fold space) and the path-derived model. Both are needed to + * rebuild `systemInstruction` and to synthesize chunks on every compress + * loop round. */ + google?: { system?: string; model?: string }; nudge?: NudgeDecision; /** Render strategy the prepare used for processTurn ("none" for codex * compaction triggers / ACP_RENDER_NONE). The #422 fold-refresh hook in @@ -824,7 +872,10 @@ async function handle( let responsesCompact: boolean; let route: ReturnType; let upstreamOrigin: string; - let protocol: "anthropic" | "openai" | "responses" | null; + let protocol: WireProtocol | null; + /** Gemini's model, resolved from the request path (its body never carries + * one). Undefined for every other protocol. */ + let googleModel: string | undefined; // #903: cost clock starts BEFORE the body read — local= covers body // reception + parse + processTurn + rebuild/serialize, i.e. everything bili // does before handing off. inboundBytes stays the raw wire size (pre-decode). @@ -864,8 +915,14 @@ async function handle( ? "anthropic" : urlPath.endsWith("/responses") || responsesCompact ? "responses" - : null + : googlePathKind(urlPath) !== null + ? "google" + : null : null); + // Gemini carries the model in the PATH, not the body — resolve it here so + // the window/config block below and every later model-keyed decision see + // it on a request whose body has no `model` field (#google). + googleModel = protocol === "google" ? googleModelFromPath(urlPath) : undefined; // Issue #99: decode body only for known protocols — passthrough requests // (e.g. GET /models) must forward raw bytes without content-encoding decode. if (protocol !== null && bodyBuffer.length > 0) { @@ -958,6 +1015,10 @@ async function handle( return; } } + // The effective model for this request. Every wire carries it in the body + // except Gemini, whose URL path holds it (`/v1beta/models/:…`). + const bodyModel = parsed && typeof parsed === "object" && "model" in parsed && typeof parsed.model === "string" ? parsed.model : undefined; + const requestModel = bodyModel ?? googleModel; // Capture the CLIENT's raw incoming request (before bili rebuilds) to // resolve whether codex sends previous_response_id + full input vs delta. if (opts.debug && parsed && typeof parsed === "object") { @@ -1007,7 +1068,9 @@ async function handle( let wsSourceForLog: string | undefined; let reqModelId: string | undefined; if (parsed && typeof parsed === "object") { - const model = (parsed as { model?: string }).model; + // Gemini's model lives in the request path, every other wire carries it + // in the body — either way the window/config block below needs one. + const model = requestModel; reqModelId = typeof model === "string" ? model : undefined; if (model) { const embeddedUrl = route?.rewrittenUrl; @@ -1207,7 +1270,24 @@ async function handle( parsed as { prompt_cache_key?: unknown }, ) : undefined; - const conversation = protocol === "anthropic" + // Gemini native wire: no conversation-header convention, and no + // prompt_cache_key to promote (a Gemini body has no such field, so the + // omp plugin cannot stamp one). Identity therefore rests on the + // client's own conversation header when it sends one, and on content + // prefix affinity over `contents` otherwise (anonymous branch, #309). + const googleSignal = protocol === "google" + ? conversationSignalGoogle(parsed as GoogleRequestBody, convHeader) + : ""; + const googleIdentity = protocol === "google" + ? { + value: googleSignal, + source: convHeader ? ("header" as const) : ("content-fingerprint" as const), + clientProvided: !!convHeader, + } + : undefined; + const conversation = protocol === "google" + ? (googleIdentity?.value ?? googleSignal) + : protocol === "anthropic" ? // #970: a subagent's conversation value gets its own // `|sub:` namespace so it lands on its own session // (own lock chain, own compression state) instead of queueing @@ -1243,7 +1323,9 @@ async function handle( ? (openaiIdentity?.clientProvided ?? false) : protocol === "anthropic" ? (anthropicIdentity?.clientProvided ?? false) - : !!convHeader; + : protocol === "google" + ? (googleIdentity?.clientProvided ?? false) + : !!convHeader; // Anonymous fallback (#309): clients with no identity signal at all // (no headers, no session_id/prompt_cache_key) still replay their full // history — resolve them by longest-prefix affinity instead of the @@ -1254,8 +1336,10 @@ async function handle( let anonAffinity: AnonymousAffinity | null = null; if (!clientProvided) { const anonMessages = protocol === "responses" - ? (parsed as { input?: unknown }).input ?? [] - : (parsed as { messages?: unknown }).messages ?? []; + ? ((parsed as { input?: unknown }).input ?? []) + : protocol === "google" + ? ((parsed as GoogleRequestBody).contents ?? []) + : ((parsed as { messages?: unknown }).messages ?? []); anonAffinity = prefixAffinity.resolve(Array.isArray(anonMessages) ? anonMessages : []); if (!anonAffinity) { log("warn", `400: no stable conversation identity on ${protocol} request → ${upstreamOrigin}; refusing to create a content-fingerprint session (#286)`); @@ -1287,7 +1371,7 @@ async function handle( // body.session_id) — never the synthetic one — so a user can tell // at a glance which client owns a session. pi sends nothing, so its // label stays empty (shown as "—" in the UI). - const bodyIdentity = responsesIdentity ?? openaiIdentity ?? anthropicIdentity; + const bodyIdentity = responsesIdentity ?? openaiIdentity ?? anthropicIdentity ?? googleIdentity; const affinity = affinityToken(bodyIdentity ?? { value: clientConv ?? conversation, source: clientConv ? "header" : "generated", @@ -1400,7 +1484,7 @@ async function handle( // guaranteed upstream 400 (and title-gen/probe clients re-issue it, // hammering the upstream). Gate on the raw body estimate and fail // fast locally instead of forwarding (#301 precedent). - const reqModel = (parsed as { model?: string }).model; + const reqModel = requestModel; const armedForGuard = session.stats.lastInputTokensSource === "usage" ? session.stats.lastInputTokens : 0; const guard = sideRequestGuard(parsed, protocol, reqConfig.modelContextLimit, imageBillingFor(opts, route?.rewrittenUrl ?? upstreamOrigin), headroomCap, armedForGuard); if (guard.blocked) { @@ -1568,7 +1652,7 @@ async function handle( session, async (): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { const runPrepare = (): Prepared => { - const cs = resolveCompress(opts.routes, route?.rewrittenUrl, (parsed as { model?: string }).model, opts.compress); + const cs = resolveCompress(opts.routes, route?.rewrittenUrl, requestModel, opts.compress); const reasoningCfg = cs.reasoning; const keepRecent = cs.stripImagesKeepRecent ?? DEFAULT_STRIP_IMAGES_KEEP_RECENT; const stripped = cs.stripImages @@ -1578,19 +1662,28 @@ async function handle( log("info", `[debug] strip-images: dropped ${stripped.removed} historical image part(s), kept last ${keepRecent} (session=${session.id})`); } const work = stripped.body; - return countTokens - ? prepareCountTokens(work as AnthropicRequestBody, core, reqConfig, log, session) - : protocol === "anthropic" - ? prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg) - : protocol === "openai" - ? prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl) - : responsesCompact - // #618 review nit: when no bili compaction item is present, - // prepareResponsesCompact falls back to the raw bodyBuffer — forward - // the re-serialized post-strip work instead so dropped images don't - // ride along. Unchanged bodies keep the original buffer byte-identical. - ? prepareResponsesCompact(stripped.removed > 0 ? Buffer.from(JSON.stringify(work)) : bodyBuffer, work as ResponsesRequestBody, session, req, core, reqConfig, log) - : prepareResponses(work as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl); + if (countTokens) { + return protocol === "google" + ? prepareGoogleCountTokens(work as GoogleRequestBody, core, reqConfig, log, session) + : prepareCountTokens(work as AnthropicRequestBody, core, reqConfig, log, session); + } + if (protocol === "google") { + // Both the model and the stream flag live in the URL path + // for this wire (the body carries neither), so they are + // derived here instead of read off `work`. + return prepareGoogle(work as GoogleRequestBody, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, nativeWindow, googleModel, googlePathKind(urlPath) === "stream-generate"); + } + return protocol === "anthropic" + ? prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg) + : protocol === "openai" + ? prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl) + : responsesCompact + // #618 review nit: when no bili compaction item is present, + // prepareResponsesCompact falls back to the raw bodyBuffer — forward + // the re-serialized post-strip work instead so dropped images don't + // ride along. Unchanged bodies keep the original buffer byte-identical. + ? prepareResponsesCompact(stripped.removed > 0 ? Buffer.from(JSON.stringify(work)) : bodyBuffer, work as ResponsesRequestBody, session, req, core, reqConfig, log) + : prepareResponses(work as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, route?.rewrittenUrl); }; // #332: codex's native remote-compaction request (trigger form) // is dispatched BEFORE prepare/preflight. When it is not @@ -1636,7 +1729,7 @@ async function handle( core, reqConfig, nativeWindow, - (parsed as { model?: string }).model, + requestModel, resolvedNativeWindow, windowShrinkReason, route, @@ -2302,6 +2395,186 @@ function prepareOpenai( return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; } +/** Append the ephemeral nudge to a Gemini `contents` array. Gemini is + * strict about role alternation, so a trailing user turn is merged into + * rather than appended to (the nudge then reads as the model's last input, + * which is where it belongs); a model-final history gets a fresh user turn + * because a request must not end on the model side. */ +function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleContent[] { + const last = contents[contents.length - 1]; + if (last && (last.role ?? "user") !== "model") { + const parts = Array.isArray(last.parts) ? last.parts : []; + return [...contents.slice(0, -1), { ...last, parts: [...parts, { text }] }]; + } + return [...contents, { role: "user", parts: [{ text }] }]; +} + +/** Gemini native wire (`POST /v1beta/models/:generateContent| + * :streamGenerateContent`): the OpenAI branch's twin — hoist the system + * dimension out of the fold space, fold, re-inject — with three wire-specific + * differences: + * - the model, and whether the reply streams, live in the URL PATH, so the + * caller passes both in (the body carries neither); + * - the system rides in `systemInstruction`, never inside `contents`; + * - Gemini rejects non-alternating roles, which coreToGoogle enforces by + * merging same-side core runs, so the nudge merges into the trailing user + * turn instead of starting a new one. + * #651's reasoning drop deliberately does NOT apply here: Gemini 3 validates + * the `thoughtSignature` of replayed parts, and dropping a thought part takes + * its signature with it (400 INVALID_ARGUMENT). */ +function prepareGoogle( + parsed: GoogleRequestBody, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + prompts: Prompts, + surface: PackSurface, + log: (level: string, msg: string) => void, + session: Session, + pluginMode: boolean, + nativeWindow: number, + model: string | undefined, + stream: boolean, +): Prepared { + const sessionId = session.id; + ++session.stats.requests; + let googleClientSystem = ""; + let googleOutboundSystem: string | undefined; + let systemInstruction: GoogleSystemInstruction | undefined = parsed.systemInstruction; + let processedMessages: CoreMessage[] = []; + let originalMessages: CoreMessage[] = []; + let nudge: NudgeDecision | undefined; + let rebuiltContents: GoogleContent[] = Array.isArray(parsed.contents) ? parsed.contents : []; + let toolsOut: GoogleTool[] | undefined = parsed.tools; + + const genConfig = parsed.generationConfig; + const declaredMax = genConfig ? genConfig.maxOutputTokens : undefined; + const maxTokens = typeof declaredMax === "number" ? declaredMax : 8192; + // Title-generation requests (tiny budget) get no compress tooling — same + // heuristic and same prefix-cache rationale as prepareOpenai. + const isTitleGen = maxTokens <= 200; + const shouldInject = opts.compress.injectTool && !isTitleGen; + const injectTools = shouldInject && !pluginMode; + + try { + const { msgs, systemText } = googleToCore(parsed); + googleClientSystem = systemText; + originalMessages = msgs; + const tokenCount = effectiveTokenCount(session, msgs); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + const absorbActive = absorbEnabled(config) && shouldInject; + // acp_rule has no processTurn side effect (no markers/instructions are + // ever injected into messages), so unlike absorb it needs no loop- + // config stripping — only tool availability matters. + const rulesActive = rulesEnabled(config) && shouldInject; + const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only" }); + session.state = turn.state; + // The fold from last turn's compress has materialized in state — future + // usage reports are post-fold reality, drop the credit. + session.stats.compressCreditTokens = 0; + storeEffectiveAbsorb(session, loopConfig); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // Drop sub-viability fragments before any consumer sees them (the + // kernel validates a compress batch atomically). + if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); + nudge = turn.nudge; + session.stats.contextTokens = tokenCount; + if (!session.meta.title) { + const t = deriveTitle(msgs); + if (t) session.meta.title = t; + } + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); + log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge)); + processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); + reapOrphanBlocks(session, msgs, deactivateBlock); + rebuiltContents = coreToGoogle(processedMessages as BiliMessage[]); + + // ONLY the static compress prompt joins the client's system text — the + // system instruction is the prefix-cache anchor and must stay + // byte-stable across turns. The per-turn nudge is appended to the + // trailing user content below (see prepareOpenai for the rationale). + const sysParts: string[] = []; + if (systemText) sysParts.push(systemText); + if (shouldInject) sysParts.push(withMarkerIntegrityNote(buildCompressSystemPrompt(prompts, surface?.promptSections))); + if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(config))); + googleOutboundSystem = sysParts.join("\n\n"); + // Untouched when nothing was added beyond the client's own text: the + // original `systemInstruction` object then rides through byte-identical + // instead of being re-serialized into a new shape. + const extraSystemParts = sysParts.slice(systemText ? 1 : 0); + systemInstruction = extraSystemParts.length > 0 ? { parts: sysParts.map((text) => ({ text })) } : parsed.systemInstruction; + if (injectTools) { + toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_GOOGLE] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : [])], surface?.toolPrompts); + } + if (willInjectNudge && turn.nudge) { + try { + const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); + if (rendered.text) { + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withStagedCompressGuidance(rendered.text))); + } + } catch { + } + } + } catch (err) { + log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); + processedMessages = []; + } + + const rebuilt: GoogleRequestBody = { ...parsed, contents: rebuiltContents, tools: toolsOut, systemInstruction }; + clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("google", rebuilt) }, sessionId, log); + // #532: title-gen side requests carry their own tiny system — skip them. + if (!isTitleGen && googleOutboundSystem !== undefined) { + session.metadata.systemPromptTokens = countSystemAndToolsTokens(googleOutboundSystem, toolsOut); + } + snapshotMessages(session, originalMessages); + markDirty(session); + return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, google: { system: googleClientSystem, model }, renderTags: "text-only" } as Prepared; +} + +/** `POST /v1beta/models/:countTokens` — the fold-prune twin of + * prepareCountTokens: the client measures the payload the proxy would + * actually forward. Gemini's endpoint reads `contents`/`systemInstruction` + * and answers `{totalTokens}`, so only the contents array is rewritten. */ +export function prepareGoogleCountTokens( + parsed: GoogleRequestBody, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, + session: Session, +): Prepared { + const sessionId = session.id; + try { + const { msgs } = googleToCore(parsed); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: "text-only" }); + const stripped = stripKernelSummaries(turn.messages, turn.state); + const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; + log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); + return { + body: JSON.stringify(rebuilt), + session, + processedMessages: [], + originalMessages: msgs, + protocol: "google", + stream: false, + compressInjected: false, + }; + } catch (err) { + log("warn", `[${sessionId}] countTokens prune failed, forwarding unchanged: ${String(err)}`); + return { + body: JSON.stringify({ ...parsed }), + session, + processedMessages: [], + originalMessages: [], + protocol: "google", + stream: false, + compressInjected: false, + }; + } +} + function prepareResponses( parsed: ResponsesRequestBody, req: http.IncomingMessage, @@ -2596,7 +2869,7 @@ export function isCountTokensRequest(method: string, urlPath: string, hasBody: b method === "POST" && hasBody && process.env.ACP_COUNT_TOKENS_PASSTHROUGH !== "1" && - urlPath.endsWith("/messages/count_tokens") + (urlPath.endsWith("/messages/count_tokens") || googlePathKind(urlPath) === "count-tokens") ); } @@ -2823,6 +3096,25 @@ function injectOpenaiTool(tools: OpenAITool[] | undefined, extras?: readonly Ope return [...kept, ...acp, ...list]; } +/** Merge the ACP declarations into the client's Gemini `tools` array. Gemini + * nests declarations one level deeper than the OpenAI shape + * (`tools[].functionDeclarations[]`), so presence is collected across every + * entry and the missing declarations are appended as one new entry. */ +function injectGoogleTool(tools: GoogleTool[] | undefined, extra?: { name: string }[], toolPrompts?: ToolPrompts): GoogleTool[] { + const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_GOOGLE, toolPrompts) as GoogleFunctionDeclaration[]; + const wanted: { name: string }[] = extra ? [...acp, ...extra] : [...acp]; + if (!Array.isArray(tools)) return [{ functionDeclarations: wanted as GoogleFunctionDeclaration[] }]; + const present = new Set(); + for (const tool of tools) { + for (const decl of tool?.functionDeclarations ?? []) { + if (typeof decl?.name === "string") present.add(decl.name); + } + } + const missing = wanted.filter((t) => !present.has(t.name)); + if (missing.length === 0) return tools; + return [...tools, { functionDeclarations: missing as GoogleFunctionDeclaration[] }]; +} + /** When true, the Responses path teaches compression via a text trigger * instead of a function tool. Used for hosts (OpenAI Codex code_mode) whose * server-side tools are disabled the moment any `tools` entry is declared. @@ -2874,10 +3166,11 @@ function logUpstreamProxyDecision(opts: ProxyOptions, upstreamUrl: string | unde /** Infer the wire protocol from the request path for compat-role rewrites on * requests the pipeline did not prepare (passthrough). Mirrors the path * checks in handleRequest; returns null when unknown (no rewrite). */ -function inferWireProtocol(path: string): "openai" | "responses" | null { +function inferWireProtocol(path: string): "openai" | "responses" | "google" | null { const p = path.split("?", 2)[0]; if (p.endsWith("/chat/completions") || p.endsWith("/llm_raw_chat")) return "openai"; if (p.endsWith("/responses") || p.endsWith("/responses/compact")) return "responses"; + if (googlePathKind(p) !== null) return "google"; return null; } @@ -4062,7 +4355,7 @@ async function forward( ? `\n\n---\n\n${buildAbsorbSystemPrompt(absorbToolName(loopConfig))}` : ""; const systemPrompt = withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections))) + absorbSection; - const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined); + const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, () => { // #422: mirror the prepare's fold with the post-compress state so @@ -4155,9 +4448,12 @@ async function forward( // Anthropic: input_tokens / cache_read_input_tokens / output_tokens // OpenAI: prompt_tokens / prompt_tokens_details.cached_tokens / completion_tokens // Responses: input_tokens / input_tokens_details.cached_tokens / output_tokens + // Google: usageMetadata — promptTokenCount / cachedContentTokenCount / + // candidatesTokenCount + thoughtsTokenCount // usageTotals() normalizes the per-protocol semantics so // `total` is always the true context size (see util.ts). - const u = (json.usage ?? {}) as Record; + const rawUsage = prepared.protocol === "google" ? json.usageMetadata ?? json.usage : json.usage; + const u = (rawUsage ?? {}) as Record; const { total, cached } = usageTotals(prepared.protocol, u); if (typeof total === "number") { prepared.session.stats.inputTokens += total; @@ -4173,13 +4469,15 @@ async function forward( prepared.session.stats.cachedTokens += cached; prepared.session.stats.cacheSamples += 1; } - const out = u.completion_tokens ?? u.output_tokens; + const out = usageOutputTotal(prepared.protocol, u); if (typeof out === "number") prepared.session.stats.outputTokens += out; } if (prepared.protocol === "openai") { await withSessionLock(prepared.session, () => rewriteOpenaiJsonResponse(json, ctx)); } else if (prepared.protocol === "responses") { await withSessionLock(prepared.session, () => rewriteResponsesJsonResponse(json, ctx)); + } else if (prepared.protocol === "google") { + await withSessionLock(prepared.session, () => rewriteGoogleJsonResponse(json, ctx)); } else { await withSessionLock(prepared.session, () => rewriteJsonResponse(json, ctx)); } diff --git a/src/server/budget.ts b/src/server/budget.ts index c10f0f08a6..f087c47b1d 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -1,5 +1,7 @@ import { defaultCountTokens, type CoreMessage, type NudgeDecision } from "acp-kernel"; +import { googleSystemText, type GoogleRequestBody } from "acp-kernel/wire"; import { estimateCoreMessages } from "../preflight.js"; +import { readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; // #453 hard backstop: cap the forwarded output budget so input+output can never // exceed the window on request-rebuilding upstreams (vLLM rejects an oversized @@ -42,14 +44,18 @@ export function estimateInputTokens(processedMessages: CoreMessage[], systemText * the preflight trigger fires ~10-20K late on agent clients with big tool * manifests: text alone "fits" while the real billed input already overflows * the window. Same term estimateInputTokens applies to the output clamp (#467). */ -export function estimateWireOverhead(protocol: "anthropic" | "openai" | "responses", body: string | Buffer): number { +export function estimateWireOverhead(protocol: "anthropic" | "openai" | "responses" | "google", body: string | Buffer): number { let parsed: Record; try { parsed = JSON.parse(typeof body === "string" ? body : body.toString("utf8")) as Record; } catch { return 0; } - const sysRaw = protocol === "responses" ? parsed.instructions : parsed.system; + const sysRaw = protocol === "responses" + ? parsed.instructions + : protocol === "google" + ? googleSystemText(parsed as GoogleRequestBody) + : parsed.system; let sysText = ""; if (typeof sysRaw === "string") { sysText = sysRaw; @@ -117,19 +123,19 @@ export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalati export function clampOutgoingOutput( rebuilt: Record, - field: "max_tokens" | "max_completion_tokens" | "max_output_tokens", + field: OutputBudgetField, ctx: { systemText: string; tools: unknown; processedMessages: CoreMessage[]; lastInputTokens: number; nativeWindow: number; imageTokens: number }, sessionId: string, log: (level: string, msg: string) => void, ): void { - const raw = rebuilt[field]; + const raw = readOutputBudget(rebuilt, field); if (typeof raw !== "number") return; // #488: images ride along in the rebuilt body but are invisible to the text model — // without them the cap is too generous and input+output can still overflow. const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens) + ctx.imageTokens; const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow); if (capped !== undefined) { - rebuilt[field] = capped; + writeOutputBudget(rebuilt, field, capped); log("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)`); } } diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 320831bd6e..001af30f68 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -16,21 +16,51 @@ export function isSideRequest(parsed: unknown): boolean { if (!parsed || typeof parsed !== "object") return false; const p = parsed as Record; if (Array.isArray(p.tools) && p.tools.length > 0) return false; - const raw = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; + const field = outputBudgetField(parsed); + if (!field) return false; + const raw = readOutputBudget(p, field); return typeof raw === "number" && raw > 0 && raw <= SIDE_REQUEST_MAX_TOKENS; } -export type OutputBudgetField = "max_tokens" | "max_completion_tokens" | "max_output_tokens"; +export type OutputBudgetField = "max_tokens" | "max_completion_tokens" | "max_output_tokens" | "generationConfig.maxOutputTokens"; +/** The declared output budget, proto-agnostically. Gemini nests it under + * `generationConfig` (the dotted field name above), the OpenAI/Anthropic + * families keep it flat, so every reader goes through these two accessors. */ export function outputBudgetField(parsed: unknown): OutputBudgetField | null { if (!parsed || typeof parsed !== "object") return null; const p = parsed as Record; for (const field of ["max_tokens", "max_completion_tokens", "max_output_tokens"] as const) { if (typeof p[field] === "number" && (p[field] as number) > 0) return field; } + const gen = p.generationConfig; + if (gen && typeof gen === "object") { + const v = (gen as Record).maxOutputTokens; + if (typeof v === "number" && v > 0) return "generationConfig.maxOutputTokens"; + } return null; } +export function readOutputBudget(parsed: Record, field: OutputBudgetField): number | undefined { + if (field !== "generationConfig.maxOutputTokens") { + const v = parsed[field]; + return typeof v === "number" ? v : undefined; + } + const gen = parsed.generationConfig; + if (!gen || typeof gen !== "object") return undefined; + const v = (gen as Record).maxOutputTokens; + return typeof v === "number" ? v : undefined; +} + +export function writeOutputBudget(parsed: Record, field: OutputBudgetField, value: number): void { + if (field !== "generationConfig.maxOutputTokens") { + parsed[field] = value; + return; + } + const gen = parsed.generationConfig; + parsed.generationConfig = { ...(gen && typeof gen === "object" ? (gen as Record) : {}), maxOutputTokens: value }; +} + /** #546: clients that derive the output budget from their RAW (uncompressed) * history drive it down to <=200 tokens on long sessions, then truncate every * reply mid-thought — the model cannot even emit a compress tool call, so the @@ -46,7 +76,8 @@ export function restoreOutputBudget( const field = outputBudgetField(parsed); if (!field) return; const p = parsed as Record; - const value = p[field] as number; + const value = readOutputBudget(p, field); + if (value === undefined) return; if (value > SIDE_REQUEST_MAX_TOKENS) { session.metadata.outputBudgetHighWater = value; return; @@ -54,7 +85,7 @@ export function restoreOutputBudget( if (!Array.isArray(p.tools) || p.tools.length === 0) return; const highWater = session.metadata.outputBudgetHighWater; if (typeof highWater === "number" && highWater > SIDE_REQUEST_MAX_TOKENS) { - p[field] = highWater; + writeOutputBudget(p, field, highWater); log("info", `[${session.id}] output budget restored ${value} -> ${highWater} (#546: client shrank it from its raw-history estimate)`); } } @@ -91,7 +122,7 @@ export function sideRequestGuard( // turn's usage overwrites it); it never re-centers the declared window. if (armedLimit > 0 && (limit <= 0 || armedLimit < limit)) limit = armedLimit; const field = outputBudgetField(parsed); - const maxOut = field ? ((parsed as Record)[field] as number) : 0; + const maxOut = (field ? readOutputBudget(parsed as Record, field) : undefined) ?? 0; if (limit > 0 && shouldReserveOutputHeadroom(protocol)) limit = reserveOutputHeadroom(limit, maxOut, headroomCap); const estimate = estimateRawBodyTokens(parsed) + imageTokensInParsedBody(protocol, parsed, imageBilling); return { blocked: limit > 0 && estimate >= limit * SIDE_REQUEST_GUARD_TOLERANCE, estimate, limit }; diff --git a/src/session.ts b/src/session.ts index f5df85c584..945d6259e4 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,6 +1,7 @@ import { createInitialState, type CompressionState, type Config, type CoreMessage } from "acp-kernel"; import { createHash } from "node:crypto"; import { getStore } from "./persist.js"; +import type { WireProtocol } from "./util.js"; export type BlockView = { text: string; count: number }; @@ -45,7 +46,7 @@ export type Session = { * be namespaced by protocol/provider (e.g. * sessions/anthropic/bailian_.json) and a human can tell * sessions apart at a glance. */ - protocol?: "anthropic" | "openai" | "responses"; + protocol?: WireProtocol; /** Upstream origin URL this session routes to. */ upstreamOrigin?: string; /** Human-readable conversation label (the affinity token), e.g. diff --git a/src/stream-error.ts b/src/stream-error.ts index 14e92a3634..24d94d8663 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -16,12 +16,15 @@ import type http from "node:http"; * "stop"` chunk, then `data: [DONE]`. * - anthropic: a `content_block_delta` text_delta, then `message_stop`. * - responses: `response.output_text.delta`, then `response.completed`. + * - google: an in-stream `{"error":{code,message,status}}` frame — Gemini's + * own error channel, which its SDK throws on; the stream then + * ends (there is no separate terminal byte to synthesize). * * Best-effort: if writing the error itself throws (client already gone), we * still attempt res.end(). Never throws. */ -type Protocol = "anthropic" | "openai" | "responses"; +type Protocol = "anthropic" | "openai" | "responses" | "google"; function safeWrite(res: http.ServerResponse, chunk: string): void { try { @@ -54,6 +57,10 @@ export function emitStreamError(res: http.ServerResponse, protocol: Protocol, me safeWrite(res, `event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: oi, part: { type: "output_text", text: visible } })}\n\n`); safeWrite(res, `event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: oi, item: errorItem })}\n\n`); safeWrite(res, `event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { status: "completed", output: [errorItem] } })}\n\n`); + } else if (protocol === "google") { + // Gemini's error object is `{code: number, message, status}` — a + // numeric code + gRPC-style status, no free-form `type`. + safeWrite(res, `data: ${JSON.stringify({ error: { code: 500, message: visible, status: "INTERNAL" } })}\n\n`); } else { // anthropic safeWrite(res, `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", delta: { type: "text_delta", text: visible } })}\n\n`); @@ -82,17 +89,24 @@ export function emitStreamError(res: http.ServerResponse, protocol: Protocol, me * lost → synthesize just that byte (silent completion). * - finished=false: true mid-generation cut → protocol-native in-band error * event, same shapes as emitPreflightError (#568): openai top-level `error` - * data frame + [DONE]; anthropic/responses `event: error`. A fabricated + * data frame + [DONE]; anthropic/responses `event: error`; google an + * `{"error":{code,message,status}}` frame. A fabricated * response.failed lifecycle object is deliberately avoided — the pipe does * not track the full output-item list, and strict clients (codex) crash on * half-consistent terminal payloads (see emitStreamError). The responses * wire has no separate finish-reason concept (terminal events carry the - * status), so !sawTerminal ⇒ finished=false always there. + * status), so !sawTerminal ⇒ finished=false always there. Gemini has the + * mirror-image property: its terminal event IS the finishReason chunk, so + * finished=true leaves nothing to synthesize (and finished=false is the + * only reachable case from a pipe that treats that chunk as terminal). * Never throws. */ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Protocol, finished: boolean, log?: (msg: string) => void): void { const message = "upstream stream ended before a completion event; this turn may be incomplete"; - log?.(`[acp-proxy: upstream stream truncated (${protocol})${finished ? " — finish reason seen, synthesizing missing terminal byte" : " — emitting in-band error"}]`); + const action = finished + ? protocol === "google" ? "finish reason already delivered, stream complete" : "finish reason seen, synthesizing missing terminal byte" + : "emitting in-band error"; + log?.(`[acp-proxy: upstream stream truncated (${protocol}) — ${action}]`); try { if (protocol === "openai") { if (finished) { @@ -102,6 +116,12 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto } } else if (protocol === "responses") { safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message })}\n\n`); + } else if (protocol === "google") { + // The finishReason chunk IS Gemini's stream terminator: when it was + // delivered, the client is already done and an extra terminal + // object would read as a second answer. Only the mid-flight cut + // needs the error frame (503/UNAVAILABLE — an upstream-cut stream). + if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE" } })}\n\n`); } else { if (finished) { safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`); @@ -128,6 +148,9 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto * - openai: top-level `error` in a data frame, then `[DONE]`. * - anthropic: `event: error` (the SDK's standard API-error channel). * - responses: `event: error` (Responses SSE spec). + * - google: an `{"error":{code,message,status}}` frame; the retryability + * survives as the numeric-code/status pair (503 UNAVAILABLE vs + * 500 INTERNAL), Gemini having no free-form `type` field. * Never throws. */ export function emitPreflightError(res: http.ServerResponse, protocol: Protocol, error: { message: string; retryable: boolean }, log?: (msg: string) => void): void { @@ -138,6 +161,8 @@ export function emitPreflightError(res: http.ServerResponse, protocol: Protocol, safeWrite(res, `data: ${JSON.stringify({ error: err })}\n\ndata: [DONE]\n\n`); } else if (protocol === "responses") { safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: err.code, message: err.message })}\n\n`); + } else if (protocol === "google") { + safeWrite(res, `data: ${JSON.stringify({ error: { code: error.retryable ? 503 : 500, message: error.message, status: error.retryable ? "UNAVAILABLE" : "INTERNAL" } })}\n\n`); } else { safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: err.code, message: err.message } })}\n\n`); } diff --git a/src/stream-google.ts b/src/stream-google.ts new file mode 100644 index 0000000000..789e6bdbbb --- /dev/null +++ b/src/stream-google.ts @@ -0,0 +1,49 @@ +import type { CompressionCore, Config, CoreMessage } from "acp-kernel"; +import type { GooglePart } from "acp-kernel/wire"; +import type { Session } from "./session.js"; +import { isProxyToolFor } from "./absorb.js"; +import { executeProxyTool } from "./loop/core.js"; +import type { RewriteCtx } from "./stream.js"; +import { containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js"; + +export function rewriteGoogleJsonResponse(body: unknown, ctx: RewriteCtx): unknown { + if (!body || typeof body !== "object") return body; + const b = body as { + candidates?: Array<{ content?: { role?: string; parts?: GooglePart[] }; finishReason?: string }>; + }; + const candidate = b.candidates?.[0]; + const content = candidate?.content; + const parts = content?.parts; + if (!candidate || !content || !Array.isArray(parts)) return body; + let converted = false; + let sawReal = false; + const noteParts: string[] = []; + const keptParts: GooglePart[] = []; + for (const part of parts) { + if (!part || typeof part !== "object") continue; + const fc = part.functionCall as { name?: unknown; args?: unknown; id?: unknown } | undefined; + if (fc && typeof fc.name === "string" && isProxyToolFor(fc.name, ctx.session, ctx.config)) { + converted = true; + const args = fc.args !== null && typeof fc.args === "object" ? (fc.args as Record) : {}; + noteParts.push(executeProxyTool(fc.name, args, ctx, typeof fc.id === "string" ? fc.id : undefined)); + continue; + } + if (fc) sawReal = true; + if (typeof part.text === "string" && (containsRenderTagText(part.text) || containsMarkerLineText(part.text))) { + ctx.log(`[warn: tag echo] non-stream google output contains ACP echo (render tags/markers), stripped: ${part.text.slice(0, 120).replace(/\n/g, " ")}`); + part.text = stripAcpTags(part.text); + } + keptParts.push(part); + } + if (!converted) return body; + // The tool result is a NEW text part: an existing text part may carry a + // thoughtSignature the client echoes back, and appending to its text would + // leave the signature describing a string that no longer exists. + content.parts = [...keptParts, { text: noteParts.join("\n") }]; + if (!sawReal) { + candidate.finishReason = "STOP"; + } + return body; +} + +export type { CompressionCore, Config, CoreMessage, Session }; diff --git a/src/util.ts b/src/util.ts index a2979d0a94..161413b911 100644 --- a/src/util.ts +++ b/src/util.ts @@ -47,7 +47,7 @@ export function isLoopbackAddress(addr: string | undefined): boolean { return !!addr && (addr.startsWith("127.") || addr === "::1" || addr.startsWith("::ffff:127.")); } -export type WireProtocol = "anthropic" | "openai" | "responses"; +export type WireProtocol = "anthropic" | "openai" | "responses" | "google"; /** * Compute the true TOTAL input-token count and the cached subset from a @@ -63,6 +63,8 @@ export type WireProtocol = "anthropic" | "openai" | "responses"; * normalized here, #779). * - Responses: `input_tokens` is the TOTAL — it ALREADY includes the * `input_tokens_details.cached_tokens` subset. + * - Google: `promptTokenCount` is the TOTAL of the `usageMetadata` object — + * it ALREADY includes the `cachedContentTokenCount` subset. * * The nudge decision (context size) and the cache-hit ratio both need the * TOTAL. The previous code computed `prompt + cached` uniformly, which is only @@ -96,6 +98,17 @@ export function usageTotals( cached, }; } + if (protocol === "google") { + // Gemini reports the whole context as promptTokenCount, already + // including the cachedContentTokenCount prefix (cached implicit + // caching) — the OpenAI/Responses shape, not Anthropic's split one. + const prompt = num(usage["promptTokenCount"]); + const cached = num(usage["cachedContentTokenCount"]); + return { + total: prompt !== undefined ? promptInputTotal("google", prompt, cached) : undefined, + cached, + }; + } // responses return { total: num(usage["input_tokens"]), @@ -121,7 +134,7 @@ export function promptInputTotal( creation?: number, ): number { if (input === undefined) return 0; - const includesCached = protocol === "openai" || protocol === "responses"; + const includesCached = protocol === "openai" || protocol === "responses" || protocol === "google"; const splitSemantics = !includesCached || (typeof cached === "number" && input < cached); const additive = (splitSemantics && typeof cached === "number" ? cached : 0) + @@ -129,6 +142,23 @@ export function promptInputTotal( return input + additive; } +/** Output-side counterpart of usageTotals: the tokens the upstream billed for + * the reply. Google splits the number — `candidatesTokenCount` (visible text) + * plus `thoughtsTokenCount` (thinking), both billed — while every other wire + * reports a single field (kept permissive: some OpenAI-wire upstreams answer + * with `output_tokens`). Returns undefined when nothing was reported. */ +export function usageOutputTotal(protocol: WireProtocol, usage: Record): number | undefined { + const num = (v: unknown): number | undefined => + typeof v === "number" && Number.isFinite(v) ? v : undefined; + if (protocol === "google") { + const candidates = num(usage["candidatesTokenCount"]); + const thoughts = num(usage["thoughtsTokenCount"]); + if (candidates === undefined && thoughts === undefined) return undefined; + return (candidates ?? 0) + (thoughts ?? 0); + } + return num(usage["completion_tokens"]) ?? num(usage["output_tokens"]); +} + /** Result of inspecting an upstream response for a "context too long" error. */ export interface ContextOverflowInfo { /** True if the response looks like an upstream context-overflow error. */ @@ -164,6 +194,9 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ // side requests that bypass preflight; its number arms the emergency shrink. // #570: its body also carries the real window, see parseOverflowWindow. /exceed[_\s]?context[_\s]?size/i, + // Gemini (generativelanguage.googleapis.com) 400 INVALID_ARGUMENT: + // "The input token count (N) exceeds the maximum number of tokens allowed (W)." + /exceeds the maximum number of tokens/i, ]; function toTokenNumber(s: string): number | undefined { @@ -185,6 +218,11 @@ function parseOverflowWindow(text: string): number | undefined { // the parens is the limit. m = text.match(/\(\s*\d[\d,]*\s*\/\s*\d[\d,]*\s*>\s*(\d[\d,]+)\s*\)/); if (m) return toTokenNumber(m[1]); + // Gemini: "The input token count (1012345) exceeds the maximum number of + // tokens allowed (1048576)." — the number after "allowed" is the window; the + // first parenthesized number is the rejected payload and must not be used. + m = text.match(/maximum number of tokens allowed\s*\((\d[\d,]*)\)/i); + if (m) return toTokenNumber(m[1]); m = text.match(/maximum context length is (\d[\d,]*)/i) ?? text.match(/maximum context length of (\d[\d,]*)/i) ?? diff --git a/src/wire-body.ts b/src/wire-body.ts index 27ad36180d..b6e7ba7753 100644 --- a/src/wire-body.ts +++ b/src/wire-body.ts @@ -34,6 +34,22 @@ export function appendTrailingUserText(protocol: WireProtocol, body: string | Bu } return JSON.stringify(obj); } + if (protocol === "google") { + // Gemini has no `messages` array: the conversation is `contents`, and a + // trailing user turn is `{role:"user", parts:[{text}]}`. Merge into the + // last user content when there is one (back-to-back user contents break + // the client's own replay grouping), else start a new one. + const contents = obj.contents; + if (!Array.isArray(contents)) return null; + const arr = contents as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user" && Array.isArray(last.parts)) { + last.parts = [...(last.parts as unknown[]), { text }]; + } else { + arr.push({ role: "user", parts: [{ text }] }); + } + return JSON.stringify(obj); + } const messages = obj.messages; if (!Array.isArray(messages)) return null; const arr = messages as Record[]; diff --git a/tests/adapter-google.test.ts b/tests/adapter-google.test.ts new file mode 100644 index 0000000000..ac3c7f6d3c --- /dev/null +++ b/tests/adapter-google.test.ts @@ -0,0 +1,351 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createGoogleAdapter } from "../src/loop/index.ts"; +import type { CompressLoopAdapter, ParsedStreamEvent } from "../src/loop/index.ts"; + +function sse(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +async function collect(adapter: CompressLoopAdapter, body: string): Promise { + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(new Response(body, { status: 200 }).body!, 1)) events.push(ev); + return events; +} + +function frameJson(buf: Buffer | undefined): Record { + assert.ok(buf, "frame buffer present"); + const m = /^data: ([\s\S]*)\n\n$/.exec(buf.toString("utf8")); + assert.ok(m, `frame is an SSE data frame: ${buf.toString("utf8").slice(0, 80)}`); + return JSON.parse(m[1]) as Record; +} + +function candidateOf(parsed: Record): Record { + const candidates = parsed.candidates as Array>; + assert.equal(candidates.length, 1, "exactly one candidate"); + return candidates[0]; +} + +function partsOf(parsed: Record): Array> { + const content = candidateOf(parsed).content as { role: string; parts: Array> }; + assert.equal(content.role, "model", "synthesized chunks are model content"); + return content.parts; +} + +const CORE_MESSAGES: CoreMessage[] = [ + { id: "u1", role: "user", contentType: "text", text: "fold the early history" }, + { id: "a1", role: "assistant", contentType: "text", text: "working on it" }, +]; + +const REQUEST_BODY: Record = { + contents: [{ role: "user", parts: [{ text: "original" }] }], + systemInstruction: { parts: [{ text: "client system" }] }, + tools: [{ functionDeclarations: [{ name: "compress", description: "fold ranges" }] }], + toolConfig: { functionCallingConfig: { mode: "AUTO" } }, + generationConfig: { temperature: 0.2, maxOutputTokens: 8192 }, + model: "gemini-3-pro-preview", + stream: true, + max_tokens: 4096, +}; + +test("google buildRequest: contents + merged systemInstruction, request fields kept, host keys dropped", () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-3-pro-preview"); + const body = adapter.buildRequest(CORE_MESSAGES, "compress prompt", REQUEST_BODY); + + const contents = body.contents as Array>; + assert.ok(Array.isArray(contents), "contents is an array"); + assert.ok(contents.length > 0, "contents rebuilt from the core messages"); + const wire = JSON.stringify(contents); + assert.match(wire, /fold the early history/, "user text reached the wire"); + assert.match(wire, /working on it/, "assistant text reached the wire"); + assert.deepEqual( + body.systemInstruction, + { parts: [{ text: "client system" }, { text: "compress prompt" }] }, + "client system first, then the compress prompt", + ); + assert.deepEqual(body.tools, REQUEST_BODY.tools, "tools preserved"); + assert.deepEqual(body.toolConfig, REQUEST_BODY.toolConfig, "toolConfig preserved"); + assert.deepEqual(body.generationConfig, REQUEST_BODY.generationConfig, "generationConfig preserved"); + assert.equal("model" in body, false, "model travels in the URL path, never the body"); + assert.equal("stream" in body, false, "alt=sse travels in the query, never the body"); + assert.equal("max_tokens" in body, false, "the output cap lives in generationConfig"); +}); + +test("google parseStream: SSE frames yield text, reasoning, tool_call, usage, done in order", async () => { + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + sse({ candidates: [{ content: { role: "model", parts: [{ text: "Hello " }] }, index: 0 }] }) + + sse({ + candidates: [{ + content: { role: "model", parts: [{ thought: true, text: "weighing options", thoughtSignature: "sig-thinking" }] }, + index: 0, + }], + }) + + sse({ + candidates: [{ + content: { + role: "model", + parts: [{ + functionCall: { + name: "compress", + args: { content: [{ startId: "m00001", endId: "m00010", summary: "gist" }] }, + id: "call_1", + }, + }], + }, + index: 0, + }], + }) + + sse({ + candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }], + usageMetadata: { + promptTokenCount: 100, + cachedContentTokenCount: 40, + candidatesTokenCount: 7, + thoughtsTokenCount: 3, + }, + }), + ); + + assert.deepEqual( + events.map((e) => e.kind), + ["text", "reasoning", "tool_call", "usage", "done"], + "all five event kinds in wire order", + ); + const text = events[0]; + assert.ok(text.kind === "text" && text.delta === "Hello ", "text delta intact"); + const call = events[2]; + assert.ok(call.kind === "tool_call", "tool call event"); + if (call.kind === "tool_call") { + assert.equal(call.name, "compress"); + assert.equal(call.callId, "call_1"); + assert.deepEqual(JSON.parse(call.arguments), { + content: [{ startId: "m00001", endId: "m00010", summary: "gist" }], + }); + assert.notEqual(call.passthrough, true, "a proxy call is executed server-side, not replayed"); + } + const usage = events[3]; + assert.ok(usage.kind === "usage", "usage event"); + if (usage.kind === "usage") { + assert.equal(usage.inputTokens, 100); + assert.equal(usage.cachedTokens, 40); + assert.equal(usage.outputTokens, 10, "candidates + thoughts tokens"); + } + const done = events[4]; + assert.ok(done.kind === "done" && done.finishReason === "STOP", "done carries the wire finish reason"); + if (done.kind === "done") assert.notEqual(done.truncated, true, "STOP is not a truncation"); +}); + +test("google parseStream: thought parts reach reasoning with their signature", async () => { + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + sse({ + candidates: [{ + content: { role: "model", parts: [{ thought: true, text: "weighing options", thoughtSignature: "sig-thinking" }] }, + index: 0, + }], + }), + ); + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.ok(reasoning, "thought part surfaced as reasoning"); + if (reasoning && reasoning.kind === "reasoning") { + assert.equal(reasoning.delta, "weighing options"); + assert.equal(reasoning.signature, "sig-thinking", "signature rides the event so a re-request can echo it"); + } + const text = events.find((e) => e.kind === "text"); + assert.equal(text, undefined, "thinking is never emitted as visible text"); +}); + +test("google parseStream: a real tool call replays its verbatim part as a meta frame", async () => { + const callFrame = sse({ + candidates: [{ + content: { + role: "model", + parts: [{ functionCall: { name: "read_file", args: { path: "a.ts" } }, thoughtSignature: "sig-real" }], + }, + index: 0, + }], + }); + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + callFrame + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const call = events.find((e) => e.kind === "tool_call"); + assert.ok(call, "real call surfaced"); + if (call && call.kind === "tool_call") { + assert.equal(call.name, "read_file"); + assert.equal(call.passthrough, true, "the loop must not emit a second copy of a replayed call"); + } + const replay = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("read_file")); + assert.ok(replay, "the original call frame was replayed to the client"); + if (replay && replay.kind === "meta") { + const parsed = frameJson(replay.chunk); + const part = partsOf(parsed)[0]; + assert.deepEqual(part.functionCall, { name: "read_file", args: { path: "a.ts" } }, "call replayed verbatim"); + assert.equal(part.thoughtSignature, "sig-real", "the part's signature survives the replay"); + } + const done = events.find((e) => e.kind === "done"); + assert.ok(done && done.kind === "done" && done.suppressCompletion === true, "the replayed finish chunk IS the completion"); +}); + +test("google parseStream: an echoed render tag is stripped and the raw frame is re-serialized filtered", async () => { + const echoed = `before \x3cacp tokens="177" type="text">m00155\x3c/acp> after`; + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + sse({ + modelVersion: "gemini-3-pro-preview", + candidates: [{ content: { role: "model", parts: [{ text: echoed }] }, index: 0 }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text", "text event emitted"); + if (text && text.kind === "text") { + assert.equal(text.delta, "before after", "the imitated render tag never reaches the client"); + const raw = frameJson(text.raw); + const part = partsOf(raw)[0]; + assert.equal(part.text, "before after", "the replayed frame carries the filtered text, not the echo"); + assert.equal(raw.modelVersion, "gemini-3-pro-preview", "the rewritten frame keeps the chunk's other fields"); + assert.equal(candidateOf(raw).finishReason, undefined, "a forwarded chunk never carries a finish reason"); + } +}); + +test("google parseStream: a blocked prompt ends the stream with the block reason", async () => { + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + sse({ promptFeedback: { blockReason: "SAFETY" } }), + ); + const done = events.find((e) => e.kind === "done"); + assert.ok(done && done.kind === "done", "blocked prompt still terminates the stream"); + if (done && done.kind === "done") assert.equal(done.finishReason, "SAFETY", "the block reason is the terminal reason"); + assert.equal(events.filter((e) => e.kind === "done").length, 1, "exactly one terminal"); +}); + +test("google parseStream: MAX_TOKENS finishes as a truncation", async () => { + const events = await collect( + createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), + sse({ + candidates: [{ content: { role: "model", parts: [{ text: "half a sen" }] }, finishReason: "MAX_TOKENS", index: 0 }], + usageMetadata: { promptTokenCount: 5, candidatesTokenCount: 4 }, + }), + ); + const done = events.find((e) => e.kind === "done"); + assert.ok(done && done.kind === "done", "done emitted"); + if (done && done.kind === "done") { + assert.equal(done.finishReason, "MAX_TOKENS"); + assert.equal(done.truncated, true, "core.ts keys its zero-side-effect retry off this flag"); + } +}); + +test("google parseStream: the JSON-array streaming form yields the same events", async () => { + const body = JSON.stringify([ + { candidates: [{ content: { role: "model", parts: [{ text: "array form" }] }, index: 0 }] }, + { candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }, + ]); + const events = await collect(createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"), body); + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text" && text.delta === "array form", "text came through the array form"); + const done = events.find((e) => e.kind === "done"); + assert.ok(done && done.kind === "done" && done.finishReason === "STOP", "finish reason came through the array form"); +}); + +test("google emit*: text, tool call and completion frames carry the documented shape", () => { + const adapter = createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"); + + const textParts = partsOf(frameJson(adapter.emitText("delta"))); + assert.deepEqual(textParts, [{ text: "delta" }], "emitText emits one text part"); + + const callParts = partsOf(frameJson(adapter.emitToolCall({ + name: "search_context", + callId: "call_9", + arguments: '{"query":"kv cache"}', + signature: "sig-call", + }))); + assert.deepEqual(callParts, [{ + functionCall: { name: "search_context", args: { query: "kv cache" }, id: "call_9" }, + thoughtSignature: "sig-call", + }], "emitToolCall emits a functionCall part with object args, id and signature"); + + const completion = frameJson(adapter.emitCompletion({ + finishReason: "length", + usage: { inputTokens: 10, outputTokens: 2, cachedTokens: 4 }, + })); + assert.equal(candidateOf(completion).finishReason, "MAX_TOKENS", "a proxy-side 'length' maps onto Gemini's MAX_TOKENS"); + assert.deepEqual(completion.usageMetadata, { + promptTokenCount: 10, + candidatesTokenCount: 2, + totalTokenCount: 12, + cachedContentTokenCount: 4, + }, "completion carries usage"); + + const plain = frameJson(adapter.emitCompletion()); + assert.equal(candidateOf(plain).finishReason, "STOP", "a completion always carries a finish reason"); + + const error = frameJson(adapter.emitError("upstream exploded")); + assert.deepEqual(error, { error: { code: 500, message: "upstream exploded", status: "INTERNAL" } }); +}); + +// Frames reach the client from two fields: `raw` on the streaming events, and +// `chunk` on meta events — the settle replay of a buffered call chunk arrives as +// a meta, so a collector that reads only `raw` sees none of it. +function framesOf(events: ParsedStreamEvent[]): string[] { + const frames: string[] = []; + for (const e of events) { + if ("raw" in e && Buffer.isBuffer(e.raw)) frames.push(e.raw.toString("utf8")); + if ("chunk" in e && Buffer.isBuffer(e.chunk)) frames.push(e.chunk.toString("utf8")); + } + return frames; +} + +// A chunk may carry text and a functionCall part together. It is replayed whole +// at settle (the call needs its signature, id and order), so the immediate text +// frame must not also carry the chunk: the client would see the same content +// twice. The guard has to look at the WHOLE chunk, because a call that follows +// the text part in iteration order is still a call in this chunk. +test("google parseStream: a chunk holding text and a call does not forward its text twice", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-3-pro-preview"); + const events = await collect( + adapter, + sse({ candidates: [{ content: { role: "model", parts: [{ text: "checking " }, { functionCall: { name: "read", args: { path: "src/plugin.ts" } } }] }, index: 0 }] }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }], usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 2 } }), + ); + const frames = framesOf(events).join(""); + assert.equal((frames.match(/checking /g) ?? []).length, 1, `the text reaches the client once, got: ${frames}`); + assert.equal((frames.match(/"name":"read"/g) ?? []).length, 1, `the call reaches the client once, got: ${frames}`); +}); + +// The edited copy an emptied tag leaves behind must not carry the chunk's +// sibling call parts: settle drops a proxy call, so letting it ride along in the +// immediate frame hands the client a proxy call that the proxy executes itself. +test("google parseStream: an edited text frame carries no sibling proxy call", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-3-pro-preview"); + const TAG = "\x3cacp tokens=\"1\" type=\"text\"\x3e"; + const CLOSE = "\x3c/acp\x3e"; + const events = await collect( + adapter, + sse({ candidates: [{ content: { role: "model", parts: [{ text: `${TAG}${CLOSE}kept prose` }, { functionCall: { name: "bili_absorb", args: {} } }] }, index: 0 }] }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + const frames = framesOf(events); + const leaked = frames.filter((f) => f.includes("kept prose") && f.includes("bili_absorb")); + assert.equal(leaked.length, 0, `no frame may carry the prose and the proxy call together, got: ${leaked.join(" | ")}`); + assert.equal(frames.filter((f) => f.includes("kept prose")).length, 1, `the surviving prose still arrives once, got: ${frames.join(" | ")}`); +}); + +// A malformed upstream chunk can carry a null where a part object belongs. The +// edited copy filters sibling call parts out of the chunk, so it has to tolerate +// a null rather than dereference it: before the guard, this threw mid-stream and +// took the turn down with it. +test("google parseStream: a null sibling part does not break the edited frame", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-3-pro-preview"); + const TAG = "\x3cacp tokens=\"1\" type=\"text\"\x3e"; + const CLOSE = "\x3c/acp\x3e"; + const events = await collect( + adapter, + sse({ candidates: [{ content: { role: "model", parts: [{ text: `${TAG}m00155${CLOSE} kept prose` }, null] } }] }), + ); + const frames = framesOf(events); + assert.ok(frames.some((f) => f.includes("kept prose")), `the prose still reaches the client, got: ${frames.join(" | ")}`); +}); diff --git a/tests/e2e-google-compress.test.ts b/tests/e2e-google-compress.test.ts new file mode 100644 index 0000000000..92897cbcb3 --- /dev/null +++ b/tests/e2e-google-compress.test.ts @@ -0,0 +1,456 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { listSessions } from "../src/session.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// Gemini native wire end-to-end: the client replays a growing `contents` array +// against `/v1beta/models/:streamGenerateContent?alt=sse`, the mock +// upstream demands compression through a `functionCall` part, and the proxy must +// (a) recognize the wire, (b) inject the ACP tool declarations, (c) execute the +// compress call server-side, and (d) forward a FOLDED, bounded payload while the +// client history keeps growing. +const TURNS = 60; +const LIVE_BYTES_THRESHOLD = 24 * 1024; +const CHARS_PER_TOKEN = 4; +const MSG_TOKENS = 120; +const SUMMARY_TEXT = "summary of the compressed middle segment: the conversation grew turn by turn while the proxy folded the middle of the history into this block"; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + const { promise, resolve, reject } = Promise.withResolvers(); + server.close((error) => (error ? reject(error) : resolve())); + return promise; +} + +function userText(i: number): string { + const filler = "the quick brown fox jumps over the lazy dog. "; + return `user turn ${i}: ${filler.repeat((MSG_TOKENS * CHARS_PER_TOKEN) / 46 | 0)}`; +} + +function assistantText(i: number): string { + const filler = "lorem ipsum dolor sit amet consectetur adipiscing elit "; + return `assistant reply ${i}: ${filler.repeat((MSG_TOKENS * CHARS_PER_TOKEN) / 50 | 0)}`; +} + +type GooglePart = { text?: string; thought?: boolean; thoughtSignature?: string; functionCall?: { name: string; args?: unknown; id?: string } }; + +function googleFrame(parts: GooglePart[], finishReason?: string, usage?: Record): string { + const candidate: Record = { content: { role: "model", parts }, index: 0 }; + if (finishReason) candidate.finishReason = finishReason; + const frame: Record = { candidates: [candidate], modelVersion: "gemini-test" }; + if (usage) frame.usageMetadata = usage; + return `data: ${JSON.stringify(frame)}\n\n`; +} + +const USAGE = { promptTokenCount: 1000, cachedContentTokenCount: 0, candidatesTokenCount: 50, thoughtsTokenCount: 0, totalTokenCount: 1050 }; + +type RelayState = { + upstreamReqs: { bytes: number; contents: number; body: string }[]; + compressCalls: number; + failedCompressions: number; + lastDemandBytes: number; + sinceDemand: number; + toolsInjected: number; + systemForwarded: number; + foldedReqs: number; + shapeViolations: string[]; +}; + +/** Narrows parsed JSON to an object for checked member access. */ +function asRecord(value: unknown): Record | undefined { + return value !== null && typeof value === "object" && !Array.isArray(value) ? (value as Record) : undefined; +} + +/** Gemini's request contract, to the depth the wire can violate it. The real + * API answers a violation with a generic `400 INVALID_ARGUMENT`, so the mock + * has to be the strict one: a nameless or mismatched `functionResponse`, an + * empty `parts` array, a repeated role, or a signature on a user part all + * make the whole request unusable — exactly what a lenient mock hides. */ +function geminiShapeViolations(body: string): string[] { + const problems: string[] = []; + let parsed: unknown; + try { + parsed = JSON.parse(body); + } catch { + return ["body is not JSON"]; + } + const root = asRecord(parsed); + const contents = root && Array.isArray(root.contents) ? root.contents : undefined; + if (!contents) return ["contents is not an array"]; + const callNames = new Set(); + let previousRole: string | undefined; + contents.forEach((entry, i) => { + const content = asRecord(entry); + if (!content) { + problems.push(`content[${i}] is not an object`); + return; + } + const role = typeof content.role === "string" ? content.role : ""; + if (role !== "user" && role !== "model") problems.push(`content[${i}] role=${JSON.stringify(content.role)}`); + if (role === previousRole) problems.push(`content[${i}] repeats role ${role} — Gemini needs alternating roles`); + previousRole = role; + const parts = Array.isArray(content.parts) ? content.parts : undefined; + if (!parts) { + problems.push(`content[${i}] has no parts array`); + return; + } + if (parts.length === 0) problems.push(`content[${i}] has an empty parts array`); + parts.forEach((entry2, j) => { + const part = asRecord(entry2); + if (!part) { + problems.push(`content[${i}].parts[${j}] is not an object`); + return; + } + const payloads = ["text", "inlineData", "fileData", "functionCall", "functionResponse"].filter((k) => k in part); + if (payloads.length === 0) problems.push(`content[${i}].parts[${j}] carries no payload`); + if (part.thoughtSignature !== undefined && role !== "model") problems.push(`content[${i}].parts[${j}] carries a thoughtSignature on a ${role} part`); + const call = asRecord(part.functionCall); + if (call) { + if (typeof call.name !== "string" || call.name.length === 0) problems.push(`content[${i}].parts[${j}] functionCall without a name`); + else callNames.add(call.name); + } + const response = asRecord(part.functionResponse); + if (response) { + if (typeof response.name !== "string" || response.name.length === 0) problems.push(`content[${i}].parts[${j}] functionResponse without a name`); + else if (!callNames.has(response.name)) problems.push(`content[${i}].parts[${j}] functionResponse name=${response.name} answers no call (calls seen: ${[...callNames].join(",") || "none"})`); + if (asRecord(response.response) === undefined) problems.push(`content[${i}].parts[${j}] functionResponse.response is not an object`); + } + }); + }); + return problems; +} + +function parseRefIds(body: string): string[] { + const ids: string[] = []; + const re = /<(?:acp|dcp-message-id)[^>]*>\s*(m\d+)\s*<\/(?:acp|dcp-message-id)>/g; + let m: RegExpExecArray | null; + while ((m = re.exec(body)) !== null) ids.push(m[1]!); + return ids; +} + +/** Mock Gemini upstream: SSE frames in the real shape, demanding a `compress` + * function call whenever the replayed payload has grown past the threshold. */ +function startMockGemini(state: RelayState, threshold: number = LIVE_BYTES_THRESHOLD): http.Server { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + const bytes = Buffer.byteLength(body); + let contents = 0; + try { + const parsed = JSON.parse(body) as { contents?: unknown[] }; + contents = Array.isArray(parsed.contents) ? parsed.contents.length : 0; + } catch { + contents = 0; + } + for (const problem of geminiShapeViolations(body)) state.shapeViolations.push(`req ${state.upstreamReqs.length + 1}: ${problem}`); + if (body.includes("Compression FAILED")) state.failedCompressions++; + if (body.includes('"functionDeclarations"') && body.includes('"compress"')) state.toolsInjected++; + if (body.includes("you are a test assistant")) state.systemForwarded++; + if (body.includes(SUMMARY_TEXT)) state.foldedReqs++; + state.upstreamReqs.push({ bytes, contents, body }); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const refIds = parseRefIds(body); + state.sinceDemand++; + const noShrinkAfterDemand = state.sinceDemand <= 2 && bytes >= state.lastDemandBytes * 0.9; + const shouldCompress = bytes > threshold && refIds.length >= 12 && !noShrinkAfterDemand; + if (shouldCompress) { + state.lastDemandBytes = bytes; + state.sinceDemand = 0; + state.compressCalls++; + const callId = `fc_compress_${state.compressCalls}`; + res.write( + googleFrame([ + { + functionCall: { + id: callId, + name: "compress", + args: { + content: [ + { + startId: refIds[2]!, + endId: refIds[refIds.length - 10]!, + topic: "google grow compress e2e", + summary: `${SUMMARY_TEXT} — ${refIds[2]!}..${refIds[refIds.length - 10]!}`, + }, + ], + }, + }, + }, + ]), + ); + res.write(googleFrame([], "STOP", USAGE)); + } else { + res.write(googleFrame([{ text: assistantText(state.upstreamReqs.length) }])); + res.write(googleFrame([], "STOP", USAGE)); + } + res.end(); + }); + }); + server.listen(0, "127.0.0.1"); + return server; +} + +/** Client-side view of the Gemini SSE stream: text parts plus the terminal marker + * the omp client requires (it throws when a stream ends without a finishReason). */ +function readGoogleStream(raw: string): { text: string; finishReasons: string[]; toolCalls: string[] } { + let text = ""; + const finishReasons: string[] = []; + const toolCalls: string[] = []; + for (const block of raw.split("\n\n")) { + const line = block.split("\n").find((l) => l.startsWith("data:")); + if (!line) continue; + let ev: { candidates?: { content?: { parts?: GooglePart[] }; finishReason?: string }[] }; + try { + ev = JSON.parse(line.slice(5).trim()) as typeof ev; + } catch { + continue; + } + for (const cand of ev.candidates ?? []) { + for (const part of cand.content?.parts ?? []) { + if (typeof part.text === "string" && part.thought !== true) text += part.text; + } + for (const part of cand.content?.parts ?? []) { + if (part.functionCall?.name) toolCalls.push(part.functionCall.name); + } + if (typeof cand.finishReason === "string") finishReasons.push(cand.finishReason); + } + } + return { text, finishReasons, toolCalls }; +} + +function proxyOptions(upstreamPort: number): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gemini-test": { context: 1_000_000 } } } } as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000, { + preserveRecentMessages: 3, + preserveRecentTokens: 800, + compress: { minCompressRange: 1000, maxSummaryLength: 20000, minSummaryLength: 50 }, + }), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; +} + +type TurnResult = { text: string; finishReasons: string[] }; + +/** Drives TURNS turns of a growing Gemini conversation against the proxy. */ +async function runConversation(url: string, headers: Record, state: RelayState): Promise<{ clientBytes: number; replies: TurnResult[] }> { + const contents: { role: string; parts: GooglePart[] }[] = []; + const replies: TurnResult[] = []; + for (let i = 1; i <= TURNS; i++) { + contents.push({ role: "user", parts: [{ text: userText(i) }] }); + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", ...headers }, + body: JSON.stringify({ contents, systemInstruction: { parts: [{ text: "you are a test assistant" }] }, generationConfig: { maxOutputTokens: 4096 } }), + }); + if (!res.ok) assert.fail(`turn ${i}: HTTP ${res.status}: ${await res.text()}`); + let raw = ""; + for await (const chunk of res.body!) raw += Buffer.from(chunk).toString("utf8"); + const view = readGoogleStream(raw); + replies.push({ text: view.text, finishReasons: view.finishReasons }); + contents.push({ role: "model", parts: [{ text: view.text }] }); + } + return { clientBytes: Buffer.byteLength(JSON.stringify({ contents })), replies }; +} + +test("e2e google: session-identified Gemini stream compresses and keeps the upstream payload bounded", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const state: RelayState = { upstreamReqs: [], compressCalls: 0, failedCompressions: 0, lastDemandBytes: Infinity, sinceDemand: 99, toolsInjected: 0, systemForwarded: 0, foldedReqs: 0, shapeViolations: [] }; + const upstream = startMockGemini(state); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:streamGenerateContent?alt=sse`; + + try { + const { clientBytes, replies } = await runConversation(url, { "x-acp-session": "google-grow-1" }, state); + const maxUpstreamBytes = Math.max(...state.upstreamReqs.map((r) => r.bytes)); + const maxContents = Math.max(...state.upstreamReqs.map((r) => r.contents)); + + // A compress cycle costs an extra upstream round-trip (the model answers + // the compress call, the proxy re-requests), so the count sits above TURNS. + assert.ok(state.upstreamReqs.length >= TURNS, `expected at least one upstream request per turn, got ${state.upstreamReqs.length}`); + assert.ok(state.upstreamReqs.length < TURNS * 2, `compress rounds must not spiral (${state.upstreamReqs.length} requests for ${TURNS} turns)`); + assert.equal(state.failedCompressions, 0, "compress tool calls must not fail"); + assert.equal(state.shapeViolations.length, 0, `every rebuilt Gemini request must satisfy the API contract: ${state.shapeViolations.slice(0, 4).join(" | ")}`); + assert.equal(state.toolsInjected, state.upstreamReqs.length, "the ACP tool declarations must be injected on every request"); + assert.equal(state.systemForwarded, state.upstreamReqs.length, "the client's systemInstruction must survive every rebuild"); + assert.ok(state.compressCalls >= 2, `expected >= 2 compress cycles, got ${state.compressCalls}`); + assert.ok(state.foldedReqs >= 1, "the folded summary must reach the upstream on a later turn"); + for (const [i, reply] of replies.entries()) { + assert.ok(reply.text.length > 0, `turn ${i + 1}: the model reply must not be empty`); + assert.ok(reply.finishReasons.includes("STOP"), `turn ${i + 1}: the client must see a finishReason (omp throws without one)`); + } + assert.ok(maxUpstreamBytes < LIVE_BYTES_THRESHOLD * 2, `upstream body must stay bounded (max ${maxUpstreamBytes}B vs threshold ${LIVE_BYTES_THRESHOLD}B)`); + assert.ok(maxContents < TURNS, `upstream contents must stay below the client history (max ${maxContents} vs ${TURNS} turns)`); + assert.ok(clientBytes > maxUpstreamBytes * 2, `client history (${clientBytes}B) should far exceed the max upstream body (${maxUpstreamBytes}B)`); + } finally { + await close(proxy); + await close(upstream); + } +}); + +test("e2e google: an anonymous Gemini client (no headers, omp's shape) attaches by prefix affinity and still compresses", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const state: RelayState = { upstreamReqs: [], compressCalls: 0, failedCompressions: 0, lastDemandBytes: Infinity, sinceDemand: 99, toolsInjected: 0, systemForwarded: 0, foldedReqs: 0, shapeViolations: [] }; + const upstream = startMockGemini(state); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:streamGenerateContent?alt=sse`; + + try { + const { replies } = await runConversation(url, {}, state); + assert.ok(state.upstreamReqs.length >= TURNS && state.upstreamReqs.length < TURNS * 2, `request count must reflect the compress rounds (${state.upstreamReqs.length} for ${TURNS} turns)`); + assert.equal(state.failedCompressions, 0, "compress tool calls must not fail"); + assert.equal(state.shapeViolations.length, 0, `every rebuilt Gemini request must satisfy the API contract: ${state.shapeViolations.slice(0, 4).join(" | ")}`); + assert.ok(state.compressCalls >= 2, `expected >= 2 compress cycles without any client identity signal, got ${state.compressCalls}`); + for (const [i, reply] of replies.entries()) { + assert.ok(reply.text.length > 0, `turn ${i + 1}: the model reply must not be empty`); + assert.ok(reply.finishReasons.includes("STOP"), `turn ${i + 1}: the client must see a finishReason`); + } + } finally { + await close(proxy); + await close(upstream); + } +}); + +const PLUGIN_TURNS = 8; + +test("e2e google: plugin mode (omp) keeps the agent's tool call, credits usage and ends the stream cleanly", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const state: RelayState = { upstreamReqs: [], compressCalls: 0, failedCompressions: 0, lastDemandBytes: Infinity, sinceDemand: 99, toolsInjected: 0, systemForwarded: 0, foldedReqs: 0, shapeViolations: [] }; + const upstream = startMockGemini(state, 2 * 1024); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:streamGenerateContent?alt=sse`; + + const headers = { "x-bili-plugin": "omp", "x-bili-plugin-conversation": "omp-gemini-1" }; + const contents: { role: string; parts: GooglePart[] }[] = []; + try { + for (let i = 1; i <= PLUGIN_TURNS; i++) { + contents.push({ role: "user", parts: [{ text: userText(i) }] }); + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", ...headers }, + body: JSON.stringify({ contents, generationConfig: { maxOutputTokens: 4096 } }), + }); + if (!res.ok) assert.fail(`turn ${i}: HTTP ${res.status}: ${await res.text()}`); + let raw = ""; + for await (const chunk of res.body!) raw += Buffer.from(chunk).toString("utf8"); + const view = readGoogleStream(raw); + // #721 contract on the Gemini wire: a stream that delivered its + // finishReason must NOT be reported to the agent as truncated. + assert.ok(!raw.includes("upstream stream truncated"), `turn ${i}: no spurious truncation signal`); + assert.ok(!/"error"\s*:/.test(raw), `turn ${i}: no in-band error frame`); + assert.ok(view.finishReasons.includes("STOP"), `turn ${i}: terminal finishReason must reach the agent`); + if (view.toolCalls.includes("compress")) { + // plugin mode: the agent owns compression, so the proxy must hand + // the call through instead of executing it. + state.compressCalls++; + assert.ok(!state.upstreamReqs.some((r) => r.body.includes(SUMMARY_TEXT)), "the proxy must not fold a plugin-owned tool call"); + } + contents.push({ role: "model", parts: [{ text: view.text }] }); + } + + assert.ok(state.compressCalls >= 1, "the agent's compress call must reach the client"); + // Plugin mode suppresses wire tool injection: the agent registered the + // ACP tools host-side, so the upstream never sees them. + assert.equal(state.toolsInjected, 0, "plugin mode must not inject ACP tools into the wire body"); + assert.equal(state.failedCompressions, 0, "no compress failure marker"); + assert.equal(state.shapeViolations.length, 0, `plugin-mode bodies must satisfy the API contract too: ${state.shapeViolations.slice(0, 4).join(" | ")}`); + // Usage sniffing is what keeps lastInputTokens (and therefore the nudge) + // alive on this wire: the mock always reports promptTokenCount=1000. + const googleSession = listSessions().find((s) => s.meta.protocol === "google"); + assert.ok(googleSession, "a google session must exist"); + assert.equal(googleSession.stats.lastInputTokens, 1000, "usageMetadata must be credited to the session"); + } finally { + await close(proxy); + await close(upstream); + } +}); + + +test("e2e google: a non-streaming generateContent turn stays JSON, passes the reply through and rebuilds the request", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const calls: { body: string }[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + calls.push({ body: Buffer.concat(chunks).toString("utf8") }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + candidates: [{ content: { role: "model", parts: [{ text: "non-streaming reply" }] }, finishReason: "STOP", index: 0 }], + usageMetadata: USAGE, + modelVersion: "gemini-test", + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:generateContent`; + + try { + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "google-nonstream-1" }, + body: JSON.stringify({ + contents: [{ role: "user", parts: [{ text: userText(1) }] }], + systemInstruction: { parts: [{ text: "you are a test assistant" }] }, + generationConfig: { maxOutputTokens: 4096 }, + }), + }); + assert.ok(res.headers.get("content-type")?.includes("application/json"), "a non-streaming Gemini turn must stay JSON"); + const body = (await res.json()) as { candidates?: { content?: { parts?: GooglePart[] }; finishReason?: string }[] }; + assert.equal(body.candidates?.[0]?.content?.parts?.[0]?.text, "non-streaming reply", "the reply must pass through unchanged"); + assert.equal(body.candidates?.[0]?.finishReason, "STOP", "the finishReason must survive the rewrite"); + assert.equal(calls.length, 1, "exactly one upstream request"); + assert.ok(calls[0]!.body.includes("you are a test assistant"), "the client systemInstruction must survive the rebuild"); + assert.ok(calls[0]!.body.includes('"functionDeclarations"') && calls[0]!.body.includes('"compress"'), "the ACP tools must be injected on the non-streaming path"); + const session = listSessions().find((s) => s.meta.protocol === "google"); + assert.ok(session, "a google session must exist"); + assert.equal(session.stats.lastInputTokens, 1000, "usageMetadata must be credited on the non-streaming path"); + } finally { + await close(proxy); + await close(upstream); + } +}); diff --git a/tests/preflight-google-summary.test.ts b/tests/preflight-google-summary.test.ts new file mode 100644 index 0000000000..4be5fb5fc8 --- /dev/null +++ b/tests/preflight-google-summary.test.ts @@ -0,0 +1,183 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// The Gemini wire breaks the #626 assumption that a summary call can be made +// non-streaming: the summarization request is posted to the client's own URL, +// `…:streamGenerateContent?alt=sse`, which answers SSE whatever the request +// says (the Gemini payload has no `stream` field to turn it off). A preflight +// on an over-window session therefore must parse an SSE summary reply that it +// never asked to be streamed — otherwise the summary reads as empty, the +// per-request call budget is burned and the turn fail-fasts with "context +// exceeds the model window" (observed on a 1.39M-token resumed session). + +const SUMMARY_TEXT = + "GEMINI SUMMARY: the segment held a deterministic load-growth payload across a dozen turns; every raw marker is derivable from the seed and none carries unique state, so the folded view loses nothing of value for continued work."; + +type Call = { summary: boolean; raw: string }; + +function sse(data: unknown): string { + return `data: ${JSON.stringify(data)}\n\n`; +} + +function geminiText(text: string, thought = false): unknown { + return { + candidates: [ + { + index: 0, + content: { role: "model", parts: [{ text, ...(thought ? { thought: true } : {}) }] }, + finishReason: "STOP", + }, + ], + usageMetadata: { promptTokenCount: 100, candidatesTokenCount: 5, totalTokenCount: 105 }, + }; +} + +// A Gemini upstream: the summary call and the forwarded call both arrive at +// `:streamGenerateContent` and both are answered the way Google answers — +// SSE, with no regard for any stream flag. The summary call is recognised by +// the summarization instruction in its body. +function makeGeminiUpstream(calls: Call[]): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + const isSummary = /TASK: The conversation segment below/.test(raw); + calls.push({ summary: isSummary, raw }); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (isSummary) { + // Thinking first, then the summary text — the extractor must + // take the text part and skip the reasoning part. + res.write(sse(geminiText("let me compress this segment", true))); + res.write(sse(geminiText(SUMMARY_TEXT.slice(0, 40)))); + res.write(sse(geminiText(SUMMARY_TEXT.slice(40)))); + } else { + res.write(sse(geminiText("forwarded to the model"))); + } + res.end(); + }); + }); +} + +function longGeminiContents() { + const contents: { role: string; parts: { text: string }[] }[] = []; + for (let i = 0; i < 12; i++) { + contents.push({ role: i % 2 === 0 ? "user" : "model", parts: [{ text: `Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }] }); + } + return contents; +} + +function startProxy(upstreamPort: number): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + return startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gemini-test": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + compress: { injectTool: true, injectNudge: true }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); +} + +async function driveGeminiPreflight(proxyPort: number, upstreamPort: number, session: string, path = "streamGenerateContent?alt=sse"): Promise<{ status: number; body: string }> { + const resp = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:${path}`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": session }, + body: JSON.stringify({ contents: longGeminiContents(), systemInstruction: { parts: [{ text: "You are a test assistant." }] }, generationConfig: { maxOutputTokens: 4096 } }), + }); + return { status: resp.status, body: await resp.text() }; +} + +test("e2e preflight (#829): a Gemini summary reply that streams although the call was not streamed is still read", async () => { + const calls: Call[] = []; + const upstream = makeGeminiUpstream(calls); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + const { status, body } = await driveGeminiPreflight(proxyPort, upstreamPort, "pf-google-1"); + assert.equal(status, 200, `an over-window Gemini turn must be compressed and forwarded, not fail-fasted (got HTTP ${status}: ${body.slice(0, 300)})`); + + const summaries = calls.filter((c) => c.summary); + assert.ok(summaries.length >= 1, "preflight must have asked Gemini for a summary"); + + // The preflight request shape is Gemini's: contents + systemInstruction, + // never the responses `instructions`/`input` pair. + const first: unknown = JSON.parse(summaries[0]!.raw); + assert.ok(first !== null && typeof first === "object" && "contents" in first && Array.isArray(first.contents), "the summary call carries Gemini contents"); + assert.equal(first !== null && typeof first === "object" && "instructions" in first, false, "no responses-shaped instructions leak into a Gemini call"); + + // The turn reached the model: a real Gemini completion came back. + assert.ok(body.includes("forwarded to the model"), `the forwarded model reply must reach the client, got: ${body.slice(0, 300)}`); + assert.ok(body.includes("STOP"), "the client sees a terminated Gemini stream"); + + // The fold applied: the rebuilt payload carries the summary text in + // place of at least one raw range. + const forwarded = calls.filter((c) => !c.summary).map((c) => c.raw); + assert.ok(forwarded.length >= 1, "the rebuilt payload was forwarded"); + assert.ok(forwarded.some((r) => r.includes(SUMMARY_TEXT.slice(0, 40))), "the summary replaced a raw range in the forwarded payload"); + const markers = forwarded.reduce((n, r) => n + (r.match(/MARKER_\d+_content_/g) ?? []).length, 0); + assert.ok(markers < 12 * 250, `the folded payload must drop raw marker text, still carried ${markers}`); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); + +test("e2e preflight (#829): the JSON summary of a non-streaming Gemini call is read too", async () => { + const calls: Call[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + const isSummary = /TASK: The conversation segment below/.test(raw); + calls.push({ summary: isSummary, raw }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(geminiText(isSummary ? SUMMARY_TEXT : "forwarded to the model"))); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + const { status, body } = await driveGeminiPreflight(proxyPort, upstreamPort, "pf-google-json", "generateContent"); + assert.equal(status, 200, `the JSON-summary shape must compress as well (got HTTP ${status}: ${body.slice(0, 300)})`); + assert.ok(calls.filter((c) => c.summary).length >= 1, "a summary was requested"); + assert.ok(body.includes("forwarded to the model"), `the model reply reached the client, got: ${body.slice(0, 300)}`); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); diff --git a/tests/preflight-sse-extraction.test.ts b/tests/preflight-sse-extraction.test.ts index 402104786a..77d25022db 100644 --- a/tests/preflight-sse-extraction.test.ts +++ b/tests/preflight-sse-extraction.test.ts @@ -139,6 +139,30 @@ test("#780 openai: in-stream bare error object invalidates accumulated text", () assert.equal(extractSummaryFromSse("openai", body), ""); }); +// Gemini carries summary text in candidates[0].content.parts[].text, and a +// `thought:true` part is the model's reasoning rather than summary output, so +// it must not reach the summary. The wire reached production with no case here +// at all — every one of these shapes was unverified (#829). +const G_TEXT = (text: string, thought = false): string => + `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ text, ...(thought ? { thought: true } : {}) }] }, finishReason: "STOP" }] })}\n\n`; + +test("#780 google: text parts accumulate and thinking parts are skipped", () => { + assert.equal(extractSummaryFromSse("google", G_TEXT("weighing the segment", true) + G_TEXT("the ") + G_TEXT("summary")), "the summary"); +}); + +test("#780 google: cleanly framed stream without a finishReason is tolerated (#764)", () => { + const bare = `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ text: "lenient gemini text" }] } }] })}\n\n`; + assert.equal(extractSummaryFromSse("google", bare), "lenient gemini text"); +}); + +test("#780 google: mid-frame truncation is rejected", () => { + assert.equal(extractSummaryFromSse("google", G_TEXT("partial ") + 'data: {"candidates":[{"content":{"parts":[{"tex'), ""); +}); + +test("#780 google: in-stream error object invalidates accumulated text", () => { + assert.equal(extractSummaryFromSse("google", G_TEXT("kept ") + `data: ${JSON.stringify({ error: { code: 429, message: "quota exceeded" } })}\n\n`), ""); +}); + test("#780 CRLF-framed bodies are accepted", () => { const lf = R_DELTA("crlf ") + R_DELTA("ok") + R_COMPLETED("crlf ok"); assert.equal(extractSummaryFromSse("responses", lf.replace(/\n/g, "\r\n")), "crlf ok");