|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import { bridgeToResponsesSSE, buildResponseJSON } from "../src/bridge"; |
3 | 3 | import { createResponsesPassthroughAdapter as createResponsesPassthroughAdapterProduction } from "../src/adapters/openai-responses"; |
| 4 | +import { createTranslatorBudget } from "../src/lib/translator-budget"; |
4 | 5 | import { CODEX_FORWARD_BASE_URL } from "../src/providers/openai-tiers"; |
5 | 6 | import { parseRequest } from "../src/responses/parser"; |
6 | 7 | import { |
@@ -165,6 +166,70 @@ describe("buildResponseJSON compaction mode", () => { |
165 | 166 | }); |
166 | 167 | }); |
167 | 168 |
|
| 169 | +describe("native Responses compaction passthrough", () => { |
| 170 | + const provider = { |
| 171 | + adapter: "openai-responses", |
| 172 | + baseUrl: "https://responses.example/v1", |
| 173 | + authMode: "key" as const, |
| 174 | + apiKey: "test-key", |
| 175 | + }; |
| 176 | + |
| 177 | + test("buffered ciphertext-only completion yields done without a text delta", async () => { |
| 178 | + const adapter = createResponsesPassthroughAdapterProduction(provider); |
| 179 | + const encryptedContent = "gAAAAABm-native-buffered-ciphertext"; |
| 180 | + const budget = createTranslatorBudget(); |
| 181 | + try { |
| 182 | + const events = await adapter.parseResponse!(Response.json({ |
| 183 | + status: "completed", |
| 184 | + output: [{ type: "compaction", encrypted_content: encryptedContent }], |
| 185 | + }), budget); |
| 186 | + |
| 187 | + expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]); |
| 188 | + } finally { |
| 189 | + budget.dispose(); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + test("streaming ciphertext is charged before the compaction item takes ownership", async () => { |
| 194 | + const adapter = createResponsesPassthroughAdapterProduction(provider); |
| 195 | + const encryptedContent = "gAAAAABm-native-streaming-ciphertext"; |
| 196 | + const budget = createTranslatorBudget(); |
| 197 | + try { |
| 198 | + const events: AdapterEvent[] = []; |
| 199 | + const stream = [ |
| 200 | + "event: response.completed", |
| 201 | + `data: ${JSON.stringify({ |
| 202 | + type: "response.completed", |
| 203 | + response: { |
| 204 | + status: "completed", |
| 205 | + output: [{ type: "compaction", encrypted_content: encryptedContent }], |
| 206 | + }, |
| 207 | + })}`, |
| 208 | + "", |
| 209 | + "", |
| 210 | + ].join("\n"); |
| 211 | + for await (const event of adapter.parseStream(new Response(stream, { |
| 212 | + headers: { "content-type": "text/event-stream" }, |
| 213 | + }), budget)) events.push(event); |
| 214 | + |
| 215 | + expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]); |
| 216 | + expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(encryptedContent)); |
| 217 | + |
| 218 | + const json = buildResponseJSON(events, "test/model", { |
| 219 | + compaction: true, |
| 220 | + translatorBudget: budget, |
| 221 | + }) as { output: Array<{ type: string; encrypted_content?: string }> }; |
| 222 | + expect(json.output).toEqual([expect.objectContaining({ |
| 223 | + type: "compaction", |
| 224 | + encrypted_content: encryptedContent, |
| 225 | + })]); |
| 226 | + expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(JSON.stringify(json.output[0]))); |
| 227 | + } finally { |
| 228 | + budget.dispose(); |
| 229 | + } |
| 230 | + }); |
| 231 | +}); |
| 232 | + |
168 | 233 | describe("COMPACT_PROMPT", () => { |
169 | 234 | test("mirrors the codex-rs checkpoint instruction", () => { |
170 | 235 | expect(COMPACT_PROMPT).toContain("CONTEXT CHECKPOINT COMPACTION"); |
|
0 commit comments