feat(agent): add prompt caching options and cache token usage reporting

This commit is contained in:
2026-05-14 11:34:04 +00:00
parent 7be67543bf
commit e6346be884
9 changed files with 281 additions and 84 deletions
+65
View File
@@ -104,6 +104,71 @@ tap.test('runAgent should forward providerOptions to streamText', async () => {
expect((model.doStreamCalls[0].providerOptions as any).openai.reasoningEffort).toEqual('xhigh');
});
tap.test('runAgent should add OpenAI cache defaults when sessionId is provided', async () => {
const model = new MockLanguageModelV3({
provider: 'openai',
modelId: 'gpt-5',
doStream: async () => createTextStreamResult('ok') as any,
});
const result = await smartagent.runAgent({
model,
prompt: 'hello',
sessionId: 'session-123',
providerOptions: {
openai: {
reasoningEffort: 'high',
},
} as any,
});
const openaiOptions = (model.doStreamCalls[0].providerOptions as any).openai;
expect(result.text).toEqual('ok');
expect(openaiOptions.store).toEqual(false);
expect(openaiOptions.promptCacheKey).toEqual('session-123');
expect(openaiOptions.promptCacheRetention).toEqual('in_memory');
expect(openaiOptions.reasoningEffort).toEqual('high');
});
tap.test('runAgent should mark Anthropic prompt cache breakpoints by default', async () => {
const model = new MockLanguageModelV3({
provider: 'anthropic',
modelId: 'claude-sonnet-4-5-20250929',
doStream: async () => createTextStreamResult('ok') as any,
});
const result = await smartagent.runAgent({
model,
system: 'stable system prompt',
prompt: 'hello',
});
const prompt = model.doStreamCalls[0].prompt as any[];
const systemMessage = prompt.find((message) => message.role === 'system');
const userMessage = prompt.find((message) => message.role === 'user');
expect(result.text).toEqual('ok');
expect(systemMessage.providerOptions?.anthropic?.cacheControl?.type).toEqual('ephemeral');
expect(userMessage.providerOptions?.anthropic?.cacheControl?.type).toEqual('ephemeral');
});
tap.test('runAgent should allow cache defaults to be disabled', async () => {
const model = new MockLanguageModelV3({
provider: 'openai',
modelId: 'gpt-5',
doStream: async () => createTextStreamResult('ok') as any,
});
await smartagent.runAgent({
model,
prompt: 'hello',
sessionId: 'session-123',
cache: false,
});
expect(model.doStreamCalls[0].providerOptions).toBeUndefined();
});
tap.test('runAgent should return final tool call records', async () => {
let streamCallCount = 0;
const callbackToolCalls: Array<{ name: string; input: unknown }> = [];