|
| 1 | +import { test, expect, describe } from "bun:test"; |
| 2 | +import { |
| 3 | + createOpenAIResponsesAdapter, |
| 4 | + OPENAI_SESSION_ID_OPTION, |
| 5 | +} from "../../src/provider/openai-responses-adapter.js"; |
| 6 | +import { BEARER_CREDENTIAL_SENTINEL } from "@intx/inference"; |
| 7 | +import type { ConversationTurn, InferenceOptions, LastCycleSource } from "@intx/types/runtime"; |
| 8 | + |
| 9 | +const SOURCE: LastCycleSource = { |
| 10 | + sourceId: "go/default", |
| 11 | + provider: "openai-responses", |
| 12 | + model: "gpt-5.6-luna", |
| 13 | +}; |
| 14 | + |
| 15 | +function adapter() { |
| 16 | + return createOpenAIResponsesAdapter(SOURCE); |
| 17 | +} |
| 18 | + |
| 19 | +function userTurn(text: string): ConversationTurn { |
| 20 | + return { role: "user", content: [{ type: "text", text }], timestamp: 0 }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("openai-responses buildRequest", () => { |
| 24 | + test("targets the Responses path with store off and streaming on", () => { |
| 25 | + const req = adapter().buildRequest([userTurn("hi")], "gpt-5.6-luna", {}); |
| 26 | + expect(req.url).toBe("/responses"); |
| 27 | + expect(req.headers["authorization"]).toBe(BEARER_CREDENTIAL_SENTINEL); |
| 28 | + expect(req.headers["accept"]).toBe("text/event-stream"); |
| 29 | + const body = JSON.parse(req.body) as Record<string, unknown>; |
| 30 | + expect(body["model"]).toBe("gpt-5.6-luna"); |
| 31 | + expect(body["stream"]).toBe(true); |
| 32 | + expect(body["store"]).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + test("sets prompt_cache_key from the session id, stable across builds", () => { |
| 36 | + const options: InferenceOptions = { |
| 37 | + providerOptions: { [OPENAI_SESSION_ID_OPTION]: "sess-1" }, |
| 38 | + }; |
| 39 | + const first = JSON.parse( |
| 40 | + adapter().buildRequest([userTurn("a")], "gpt-5.6-luna", options).body, |
| 41 | + ) as Record<string, unknown>; |
| 42 | + const second = JSON.parse( |
| 43 | + adapter().buildRequest([userTurn("b")], "gpt-5.6-luna", options).body, |
| 44 | + ) as Record<string, unknown>; |
| 45 | + expect(first["prompt_cache_key"]).toBe("sess-1"); |
| 46 | + expect(second["prompt_cache_key"]).toBe("sess-1"); |
| 47 | + }); |
| 48 | + |
| 49 | + test("distinct session ids yield distinct prompt_cache_keys", () => { |
| 50 | + const bodyFor = (sessionId: string): Record<string, unknown> => |
| 51 | + JSON.parse( |
| 52 | + adapter().buildRequest([userTurn("hi")], "gpt-5.6-luna", { |
| 53 | + providerOptions: { [OPENAI_SESSION_ID_OPTION]: sessionId }, |
| 54 | + }).body, |
| 55 | + ) as Record<string, unknown>; |
| 56 | + expect(bodyFor("sess-1")["prompt_cache_key"]).toBe("sess-1"); |
| 57 | + expect(bodyFor("sess-2")["prompt_cache_key"]).toBe("sess-2"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("omits prompt_cache_key when no session id is present", () => { |
| 61 | + const body = JSON.parse( |
| 62 | + adapter().buildRequest([userTurn("hi")], "gpt-5.6-luna", {}).body, |
| 63 | + ) as Record<string, unknown>; |
| 64 | + expect(body).not.toHaveProperty("prompt_cache_key"); |
| 65 | + }); |
| 66 | +}); |
0 commit comments