mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-18 20:03:33 +00:00
fix(ai): apply cache_control to string user messages
This commit is contained in:
parent
df5b0f76c0
commit
111a31e4db
2 changed files with 36 additions and 1 deletions
|
|
@ -674,7 +674,6 @@ function convertMessages(
|
||||||
if (cacheControl && params.length > 0) {
|
if (cacheControl && params.length > 0) {
|
||||||
const lastMessage = params[params.length - 1];
|
const lastMessage = params[params.length - 1];
|
||||||
if (lastMessage.role === "user") {
|
if (lastMessage.role === "user") {
|
||||||
// Add cache control to the last content block
|
|
||||||
if (Array.isArray(lastMessage.content)) {
|
if (Array.isArray(lastMessage.content)) {
|
||||||
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
||||||
if (
|
if (
|
||||||
|
|
@ -683,6 +682,14 @@ function convertMessages(
|
||||||
) {
|
) {
|
||||||
(lastBlock as any).cache_control = cacheControl;
|
(lastBlock as any).cache_control = cacheControl;
|
||||||
}
|
}
|
||||||
|
} else if (typeof lastMessage.content === "string") {
|
||||||
|
lastMessage.content = [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: lastMessage.content,
|
||||||
|
cache_control: cacheControl,
|
||||||
|
},
|
||||||
|
] as any;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,34 @@ describe("Cache Retention (PI_CACHE_RETENTION)", () => {
|
||||||
expect(capturedPayload.system[0].cache_control).toBeUndefined();
|
expect(capturedPayload.system[0].cache_control).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should add cache_control to string user messages", async () => {
|
||||||
|
const baseModel = getModel("anthropic", "claude-3-5-haiku-20241022");
|
||||||
|
let capturedPayload: any = null;
|
||||||
|
|
||||||
|
const { streamAnthropic } = await import("../src/providers/anthropic.js");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const s = streamAnthropic(baseModel, context, {
|
||||||
|
apiKey: "fake-key",
|
||||||
|
onPayload: (payload) => {
|
||||||
|
capturedPayload = payload;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const event of s) {
|
||||||
|
if (event.type === "error") break;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Expected to fail
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(capturedPayload).not.toBeNull();
|
||||||
|
const lastMessage = capturedPayload.messages[capturedPayload.messages.length - 1];
|
||||||
|
expect(Array.isArray(lastMessage.content)).toBe(true);
|
||||||
|
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
||||||
|
expect(lastBlock.cache_control).toEqual({ type: "ephemeral" });
|
||||||
|
});
|
||||||
|
|
||||||
it("should set 1h cache TTL when cacheRetention is long", async () => {
|
it("should set 1h cache TTL when cacheRetention is long", async () => {
|
||||||
const baseModel = getModel("anthropic", "claude-3-5-haiku-20241022");
|
const baseModel = getModel("anthropic", "claude-3-5-haiku-20241022");
|
||||||
let capturedPayload: any = null;
|
let capturedPayload: any = null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue