feat: implement explicit context caching for main agent with stable SI hashing

This commit is contained in:
Aishanee Shah
2026-05-12 20:15:52 +00:00
parent 5dda532573
commit 62e97b14a2
25 changed files with 948 additions and 49 deletions
@@ -90,6 +90,22 @@ describe('apiConversionUtils', () => {
expect(result['generationConfig']).toBeUndefined();
});
it('omits systemInstruction when cachedContent is present', () => {
const req: GenerateContentParameters = {
model: 'gemini-3-flash',
contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
config: {
systemInstruction: 'Original instruction',
cachedContent: 'cached-content-id',
},
};
const result = convertToRestPayload(req);
expect(result['cachedContent']).toBe('cached-content-id');
expect(result['systemInstruction']).toBeUndefined();
});
it('retains pure hyperparameters in generationConfig', () => {
const req: GenerateContentParameters = {
model: 'gemini-3-flash',
@@ -46,12 +46,16 @@ export function convertToRestPayload(
}
// Assign extracted capabilities to the root level.
if (restSystemInstruction)
// CRITICAL: systemInstruction and cachedContent are mutually exclusive in the API.
if (sdkCachedContent) {
restPayload['cachedContent'] = sdkCachedContent;
} else if (restSystemInstruction) {
restPayload['systemInstruction'] = restSystemInstruction;
}
if (sdkTools) restPayload['tools'] = sdkTools;
if (sdkToolConfig) restPayload['toolConfig'] = sdkToolConfig;
if (sdkSafetySettings) restPayload['safetySettings'] = sdkSafetySettings;
if (sdkCachedContent) restPayload['cachedContent'] = sdkCachedContent;
return restPayload;
}
@@ -92,6 +92,8 @@ describe('checkNextSpeaker', () => {
generateContentStream: vi.fn(),
countTokens: vi.fn(),
embedContent: vi.fn(),
createCachedContent: vi.fn(),
updateCachedContent: vi.fn(),
} as ContentGenerator,
mockConfig,
);