Skip to content

Commit b7bf097

Browse files
committed
fix(anthropic): Add execution tool response block types
Adds `bash_code_execution_tool_result` and `text_editor_code_execution_tool_result`
1 parent 3ace0dc commit b7bf097

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

libs/providers/langchain-anthropic/src/tests/chat_models.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,117 @@ test("Can properly format anthropic messages when given two tool results", async
191191
system: undefined,
192192
});
193193
});
194+
195+
test("Can properly format messages with bash_code_execution_tool_result blocks", async () => {
196+
const messageHistory = [
197+
new AIMessage({
198+
content: [
199+
{
200+
type: "server_tool_use",
201+
id: "bash_call",
202+
name: "bash_code_execution",
203+
input: { command: "echo 'hello'" },
204+
},
205+
{
206+
type: "bash_code_execution_tool_result",
207+
tool_use_id: "bash_call",
208+
content: {
209+
type: "bash_code_execution_result",
210+
stdout: "hello\n",
211+
stderr: "",
212+
return_code: 0,
213+
content: [],
214+
},
215+
},
216+
],
217+
}),
218+
];
219+
220+
const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory);
221+
222+
expect(formattedMessages).toEqual({
223+
messages: [
224+
{
225+
role: "assistant",
226+
content: [
227+
{
228+
type: "server_tool_use",
229+
id: "bash_call",
230+
name: "bash_code_execution",
231+
input: { command: "echo 'hello'" },
232+
},
233+
{
234+
type: "bash_code_execution_tool_result",
235+
tool_use_id: "bash_call",
236+
content: {
237+
type: "bash_code_execution_result",
238+
stdout: "hello\n",
239+
stderr: "",
240+
return_code: 0,
241+
content: [],
242+
},
243+
},
244+
],
245+
},
246+
],
247+
system: undefined,
248+
});
249+
});
250+
251+
test("Can properly format messages with text_editor_code_execution_tool_result blocks", async () => {
252+
const messageHistory = [
253+
new AIMessage({
254+
content: [
255+
{
256+
type: "server_tool_use",
257+
id: "editor_call",
258+
name: "text_editor_code_execution",
259+
input: { command: "view", path: "/tmp/test.txt" },
260+
},
261+
{
262+
type: "text_editor_code_execution_tool_result",
263+
tool_use_id: "editor_call",
264+
content: {
265+
type: "text_editor_code_execution_view_result",
266+
file_type: "text",
267+
content: "file contents here",
268+
num_lines: 1,
269+
start_line: 1,
270+
total_lines: 1,
271+
},
272+
},
273+
],
274+
}),
275+
];
276+
277+
const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory);
278+
279+
expect(formattedMessages).toEqual({
280+
messages: [
281+
{
282+
role: "assistant",
283+
content: [
284+
{
285+
type: "server_tool_use",
286+
id: "editor_call",
287+
name: "text_editor_code_execution",
288+
input: { command: "view", path: "/tmp/test.txt" },
289+
},
290+
{
291+
type: "text_editor_code_execution_tool_result",
292+
tool_use_id: "editor_call",
293+
content: {
294+
type: "text_editor_code_execution_view_result",
295+
file_type: "text",
296+
content: "file contents here",
297+
num_lines: 1,
298+
start_line: 1,
299+
total_lines: 1,
300+
},
301+
},
302+
],
303+
},
304+
],
305+
system: undefined,
306+
});
307+
});

libs/providers/langchain-anthropic/src/utils/message_inputs.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ function* _formatContentBlocks(
154154
content: ContentBlock[]
155155
): Generator<Anthropic.Beta.BetaContentBlockParam> {
156156
const toolTypes = [
157-
"tool_use",
158-
"tool_result",
157+
"bash_code_execution_tool_result",
159158
"input_json_delta",
160159
"server_tool_use",
161-
"web_search_tool_result",
160+
"text_editor_code_execution_tool_result",
161+
"tool_result",
162+
"tool_use",
162163
"web_search_result",
164+
"web_search_tool_result",
163165
];
164166
const textTypes = ["text", "text_delta"];
165167
for (const contentPart of content) {

0 commit comments

Comments
 (0)