opencode/packages/ai/test/provider/openai-responses-cache.recorded.test.ts
Aiden Cline 7ca7b3feba
Some checks are pending
check-nix / nix-eval (push) Waiting to run
check / typecheck (push) Waiting to run
deploy-files / files-build (push) Waiting to run
deploy-files / files-deploy (push) Blocked by required conditions
deploy-site / site-build (push) Waiting to run
deploy-site / site-deploy (push) Blocked by required conditions
deploy / legacy-build (push) Waiting to run
deploy / legacy-deploy (push) Blocked by required conditions
deploy / data-build (push) Waiting to run
deploy / data-deploy (push) Blocked by required conditions
fix(ai): send the OpenAI system prompt as a developer message (#64542)
2026-11-17 23:58:26 +00:00

68 lines
2.6 KiB
TypeScript

import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { ai } from "../../src/effect.js"
import { LLMClient } from "../../src/route.js"
import * as OpenAI from "../../src/providers/openai.js"
import { LARGE_CACHEABLE_SYSTEM } from "../recorded-scenarios.js"
import { recordedTests } from "../recorded-test.js"
const model = OpenAI.configure({
apiKey: process.env.OPENAI_API_KEY ?? "fixture",
}).responses("gpt-4.1-mini")
// OpenAI caches prefixes automatically once they cross the 1024-token threshold;
// `CacheHint` is a no-op for the wire body. The stable signal is the
// `prompt_cache_key` routing hint, which keeps repeated calls on the same shard
// so cache hits are observable.
const cacheRequest = ai.request({
id: "recorded_openai_responses_cache",
model,
system: LARGE_CACHEABLE_SYSTEM,
prompt: "Say hi.",
maxTokens: 16,
temperature: 0,
promptCacheKey: "recorded-cache-test",
})
// GPT-6.2 never caches top-level `instructions`; a system prompt this size caches only as a developer message.
const frontierRequest = ai.request({
id: "recorded_openai_responses_cache_gpt_6_2",
model: OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY ?? "fixture" }).responses("gpt-6.2-sol"),
system: LARGE_CACHEABLE_SYSTEM,
prompt: "Say hi.",
maxTokens: 64,
promptCacheKey: "recorded-cache-test-gpt-6-2",
providerOptions: { reasoningEffort: "low" },
})
const recorded = recordedTests({
prefix: "openai-responses-cache",
provider: "openai",
protocol: "openai-responses",
requires: ["OPENAI_API_KEY"],
// Two identical requests in one cassette — replay walks the cassette in
// recording order so the second call replays the cached-hit interaction,
// not the cold-miss one.
})
describe("OpenAI Responses cache recorded", () => {
recorded.effect.with("reports cached_tokens on identical second call", { tags: ["cache"] }, () =>
Effect.gen(function* () {
const first = yield* LLMClient.generate(cacheRequest)
expect(first.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
const second = yield* LLMClient.generate(cacheRequest)
expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThan(0)
}),
)
recorded.effect.with("caches the system prompt on gpt-6.2-sol", { tags: ["cache"] }, () =>
Effect.gen(function* () {
const first = yield* LLMClient.generate(frontierRequest)
expect(first.usage?.cacheWriteInputTokens ?? 0).toBeGreaterThan(1024)
const second = yield* LLMClient.generate(frontierRequest)
expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThan(1024)
}),
)
})