Files
pi_harness/packages/ai/test/openrouter-cache-write-repro.test.ts
T
Mario Zechner 8a0903ebf2 feat(ai): compat entrypoint, core-only root barrel (phase 5)
The root barrel is now core-only and side-effect free: types,
createModels/createProvider, auth substrate, lazyStream/lazyApi, faux,
utils. Generated catalogs, api-registry, env-api-keys, images, global
stream functions, and per-API lazy wrappers leave the root.

New @earendil-works/pi-ai/compat preserves the old surface verbatim as
a strict superset of the root: api-dispatch stream/complete with env
key injection, the builtin registration side effect (skip-if-present so
it cannot clobber earlier overrides), deprecated getModel/getModels/
getProviders aliases of the new getBuiltin* reads in providers/all,
lazy api wrappers + setBedrockProviderModule, and image generation.
Compat dies with the coding-agent ModelManager migration.

Packaging: exports map gains ./compat, ./providers/*, ./api/*;
sideEffects array lists only the effectful modules.

Old-global imports across agent/coding-agent/examples and pi-ai tests
switch to /compat (path-only; compat is a superset). The coding-agent
extension loader resolves the pi-ai ROOT specifier to compat, so
existing user extensions using the old global API keep working at
runtime until compat is removed. vitest configs alias /compat to src;
browser smoke imports old globals from /compat.
2026-06-10 21:17:12 +02:00

78 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { completeSimple, getModel } from "../src/compat.ts";
function createLongSystemPrompt(): string {
const nonce = `${Date.now()}-${Math.random()}`;
return `You are a concise assistant.\nCache nonce: ${nonce}\n\n${Array(80)
.fill(
"Prompt-caching probe content. Keep this exact text stable across requests so the provider can reuse prefix tokens and report cache read and cache write usage.",
)
.join("\n\n")}`;
}
describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter cache_write repro E2E", () => {
it(
"regression: preserves cache_write_tokens on openai-completions stream path",
{ retry: 2, timeout: 90000 },
async () => {
const model = getModel("openrouter", "google/gemini-2.5-flash");
const context = {
systemPrompt: createLongSystemPrompt(),
messages: [
{
role: "user" as const,
content: "Reply with exactly: OK",
timestamp: Date.now(),
},
],
};
const options = {
apiKey: process.env.OPENROUTER_API_KEY!,
maxTokens: 32,
temperature: 0,
onPayload: (payload: unknown) => {
const params = payload as {
messages?: Array<{
role?: string;
content?: string | Array<{ type?: string; text?: string; cache_control?: { type: string } }>;
}>;
};
const messages = params.messages;
if (!Array.isArray(messages)) return payload;
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg.role !== "user") continue;
if (typeof msg.content === "string") {
msg.content = [{ type: "text", text: msg.content, cache_control: { type: "ephemeral" } }];
break;
}
if (!Array.isArray(msg.content)) continue;
for (let j = msg.content.length - 1; j >= 0; j--) {
const part = msg.content[j];
if (part.type === "text") {
part.cache_control = { type: "ephemeral" };
break;
}
}
break;
}
return payload;
},
};
const first = await completeSimple(model, context, options);
expect(first.stopReason, first.errorMessage).toBe("stop");
const second = await completeSimple(model, context, options);
expect(second.stopReason, second.errorMessage).toBe("stop");
// Regression expectation: cache_write_tokens from provider usage must be preserved.
// With the cache_control marker above, at least one of the two calls should create cache.
const hasCacheWrite = first.usage.cacheWrite > 0 || second.usage.cacheWrite > 0;
expect(hasCacheWrite).toBe(true);
},
);
});