Skip to content

Commit d7a2686

Browse files
authored
Merge pull request #1141 from narengogi/chore/anthropic-prompt-caching-tool-result
anthropic prompt caching for tool result and tool use
2 parents 0c2b8f4 + 7332c7b commit d7a2686

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/providers/anthropic/chatComplete.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ interface AnthropicTool extends PromptCache {
4343
interface AnthropicToolResultContentItem {
4444
type: 'tool_result';
4545
tool_use_id: string;
46-
content?: string;
46+
content?:
47+
| {
48+
type: string;
49+
text?: string;
50+
cache_control?: {
51+
type: string;
52+
ttl?: number;
53+
};
54+
}[]
55+
| string;
4756
}
4857

4958
interface AnthropicBase64ImageContentItem {
@@ -138,6 +147,9 @@ const transformAssistantMessage = (msg: Message): AnthropicMessage => {
138147
input: toolCall.function.arguments?.length
139148
? JSON.parse(toolCall.function.arguments)
140149
: {},
150+
...(toolCall.cache_control && {
151+
cache_control: toolCall.cache_control,
152+
}),
141153
});
142154
});
143155
}
@@ -155,7 +167,7 @@ const transformToolMessage = (msg: Message): AnthropicMessage => {
155167
{
156168
type: 'tool_result',
157169
tool_use_id,
158-
content: msg.content as string,
170+
content: msg.content,
159171
},
160172
],
161173
};
@@ -250,6 +262,9 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
250262

251263
if (msg.role === 'assistant') {
252264
messages.push(transformAssistantMessage(msg));
265+
} else if (msg.role === 'tool') {
266+
// even though anthropic supports images in tool results, openai doesn't support it yet
267+
messages.push(transformToolMessage(msg));
253268
} else if (
254269
msg.content &&
255270
typeof msg.content === 'object' &&
@@ -275,9 +290,6 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
275290
}
276291
});
277292
messages.push(transformedMessage as AnthropicMessage);
278-
} else if (msg.role === 'tool') {
279-
// even though anthropic supports images in tool results, openai doesn't support it yet
280-
messages.push(transformToolMessage(msg));
281293
} else {
282294
messages.push({
283295
role: msg.role,

0 commit comments

Comments
 (0)