Skip to content

Commit a60cac1

Browse files
committed
move cache control into convertCbToModelMessages
1 parent b3692ac commit a60cac1

File tree

7 files changed

+344
-51
lines changed

7 files changed

+344
-51
lines changed

backend/src/llm-apis/vercel-ai-sdk/ai-sdk.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const promptAiSdkStream = async function* (
7575
agentId?: string
7676
maxRetries?: number
7777
onCostCalculated?: (credits: number) => Promise<void>
78+
includeCacheControl?: boolean
7879
} & Omit<Parameters<typeof streamText>[0], 'model'>,
7980
) {
8081
if (
@@ -233,6 +234,7 @@ export const promptAiSdk = async function (
233234
chargeUser?: boolean
234235
agentId?: string
235236
onCostCalculated?: (credits: number) => Promise<void>
237+
includeCacheControl?: boolean
236238
} & Omit<Parameters<typeof generateText>[0], 'model'>,
237239
): Promise<string> {
238240
if (
@@ -306,6 +308,7 @@ export const promptAiSdkStructured = async function <T>(options: {
306308
chargeUser?: boolean
307309
agentId?: string
308310
onCostCalculated?: (credits: number) => Promise<void>
311+
includeCacheControl?: boolean
309312
}): Promise<T> {
310313
if (
311314
!checkLiveUserInput(

backend/src/prompt-agent-stream.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const getAgentStreamFromTemplate = (params: {
1414
userId: string | undefined
1515
onCostCalculated?: (credits: number) => Promise<void>
1616
agentId?: string
17+
includeCacheControl?: boolean
1718

1819
template: AgentTemplate
1920
}) => {
@@ -24,6 +25,7 @@ export const getAgentStreamFromTemplate = (params: {
2425
userId,
2526
onCostCalculated,
2627
agentId,
28+
includeCacheControl,
2729
template,
2830
} = params
2931

@@ -44,6 +46,7 @@ export const getAgentStreamFromTemplate = (params: {
4446
userId,
4547
maxOutputTokens: 32_000,
4648
onCostCalculated,
49+
includeCacheControl,
4750
agentId,
4851
}
4952

backend/src/run-agent-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export const runAgentStep = async (
269269
)
270270
}
271271
},
272+
includeCacheControl: supportsCacheControl(agentTemplate.model),
272273
})
273274

274275
const iterationNum = agentState.messageHistory.length
@@ -287,7 +288,6 @@ export const runAgentStep = async (
287288
const agentMessages = getMessagesSubset(
288289
agentState.messageHistory,
289290
systemTokens,
290-
supportsCacheControl(agentTemplate.model),
291291
)
292292

293293
logger.debug(

backend/src/util/messages.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AssertionError } from 'assert'
22

33
import { buildArray } from '@codebuff/common/util/array'
4-
import { withCacheControl } from '@codebuff/common/util/messages'
54
import { closeXml } from '@codebuff/common/util/xml'
65

76
import { logger } from './logger'
@@ -250,7 +249,6 @@ export function trimMessagesToFitTokenLimit(
250249
export function getMessagesSubset(
251250
messages: CodebuffMessage[],
252251
otherTokens: number,
253-
includeCacheControl: boolean = true,
254252
): CodebuffMessage[] {
255253
const messagesSubset = trimMessagesToFitTokenLimit(messages, otherTokens)
256254

@@ -271,23 +269,8 @@ export function getMessagesSubset(
271269
},
272270
'No last message found in messagesSubset!',
273271
)
274-
return messagesSubset
275272
}
276273

277-
if (!includeCacheControl) {
278-
return messagesSubset
279-
}
280-
281-
// add cache control to specific messages
282-
for (const ttl of ['agentStep', 'userPrompt'] as const) {
283-
const index = messagesSubset.findIndex((m) => m.timeToLive === ttl)
284-
if (index <= 0) {
285-
continue
286-
}
287-
messagesSubset[index - 1] = withCacheControl(messagesSubset[index - 1])
288-
}
289-
messagesSubset[messagesSubset.length - 1] = withCacheControl(lastMessage)
290-
291274
return messagesSubset
292275
}
293276

common/src/types/messages/codebuff-message.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,60 @@ import {
1010
} from './content-part'
1111
import { providerMetadataSchema } from './provider-metadata'
1212

13-
export const systemCodebuffMessageSchema = z.object({
14-
role: z.literal('system'),
15-
content: z.string(),
13+
const auxiliaryDataSchema = z.object({
14+
providerOptions: providerMetadataSchema.optional(),
15+
timeToLive: z
16+
.union([z.literal('agentStep'), z.literal('userPrompt')])
17+
.optional(),
18+
keepDuringTruncation: z.boolean().optional(),
1619
})
20+
21+
export const systemCodebuffMessageSchema = z
22+
.object({
23+
role: z.literal('system'),
24+
content: z.string(),
25+
})
26+
.and(auxiliaryDataSchema)
1727
export type SystemCodebuffMessage = z.infer<typeof systemCodebuffMessageSchema>
1828

19-
export const userCodebuffMessageSchema = z.object({
20-
role: z.literal('user'),
21-
content: z.union([
22-
z.string(),
23-
z.union([textPartSchema, imagePartSchema, filePartSchema]).array(),
24-
]),
25-
})
29+
export const userCodebuffMessageSchema = z
30+
.object({
31+
role: z.literal('user'),
32+
content: z.union([
33+
z.string(),
34+
z.union([textPartSchema, imagePartSchema, filePartSchema]).array(),
35+
]),
36+
})
37+
.and(auxiliaryDataSchema)
2638
export type UserCodebuffMessage = z.infer<typeof userCodebuffMessageSchema>
2739

28-
export const assistantCodebuffMessageSchema = z.object({
29-
role: z.literal('assistant'),
30-
content: z.union([
31-
z.string(),
32-
z
33-
.union([
34-
textPartSchema,
35-
filePartSchema,
36-
reasoningPartSchema,
37-
toolCallPartSchema,
38-
toolResultPartSchema,
39-
])
40-
.array(),
41-
]),
42-
})
40+
export const assistantCodebuffMessageSchema = z
41+
.object({
42+
role: z.literal('assistant'),
43+
content: z.union([
44+
z.string(),
45+
z
46+
.union([
47+
textPartSchema,
48+
filePartSchema,
49+
reasoningPartSchema,
50+
toolCallPartSchema,
51+
toolResultPartSchema,
52+
])
53+
.array(),
54+
]),
55+
})
56+
.and(auxiliaryDataSchema)
4357
export type AssistantCodebuffMessage = z.infer<
4458
typeof assistantCodebuffMessageSchema
4559
>
4660

47-
export const toolCodebuffMessageSchema = z.object({
48-
role: z.literal('tool'),
49-
content: toolResultPartSchema.array(),
50-
})
61+
export const toolCodebuffMessageSchema = z
62+
.object({
63+
role: z.literal('tool'),
64+
content: toolResultPartSchema.array(),
65+
})
66+
.and(auxiliaryDataSchema)
5167
export type ToolCodebuffMessage = z.infer<typeof toolCodebuffMessageSchema>
5268

5369
export const codebuffMessageSchema = z

0 commit comments

Comments
 (0)