diff --git a/libs/providers/langchain-anthropic/src/tests/chat_models.test.ts b/libs/providers/langchain-anthropic/src/tests/chat_models.test.ts index 445032b03e20..7faafdafd8e6 100644 --- a/libs/providers/langchain-anthropic/src/tests/chat_models.test.ts +++ b/libs/providers/langchain-anthropic/src/tests/chat_models.test.ts @@ -240,3 +240,117 @@ test("Drop content blocks that we don't know how to handle", async () => { system: undefined, }); }); + +test("Can properly format messages with bash_code_execution_tool_result blocks", async () => { + const messageHistory = [ + new AIMessage({ + content: [ + { + type: "server_tool_use", + id: "bash_call", + name: "bash_code_execution", + input: { command: "echo 'hello'" }, + }, + { + type: "bash_code_execution_tool_result", + tool_use_id: "bash_call", + content: { + type: "bash_code_execution_result", + stdout: "hello\n", + stderr: "", + return_code: 0, + content: [], + }, + }, + ], + }), + ]; + + const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory); + + expect(formattedMessages).toEqual({ + messages: [ + { + role: "assistant", + content: [ + { + type: "server_tool_use", + id: "bash_call", + name: "bash_code_execution", + input: { command: "echo 'hello'" }, + }, + { + type: "bash_code_execution_tool_result", + tool_use_id: "bash_call", + content: { + type: "bash_code_execution_result", + stdout: "hello\n", + stderr: "", + return_code: 0, + content: [], + }, + }, + ], + }, + ], + system: undefined, + }); +}); + +test("Can properly format messages with text_editor_code_execution_tool_result blocks", async () => { + const messageHistory = [ + new AIMessage({ + content: [ + { + type: "server_tool_use", + id: "editor_call", + name: "text_editor_code_execution", + input: { command: "view", path: "/tmp/test.txt" }, + }, + { + type: "text_editor_code_execution_tool_result", + tool_use_id: "editor_call", + content: { + type: "text_editor_code_execution_view_result", + file_type: "text", + content: "file contents here", + num_lines: 1, + start_line: 1, + total_lines: 1, + }, + }, + ], + }), + ]; + + const formattedMessages = _convertMessagesToAnthropicPayload(messageHistory); + + expect(formattedMessages).toEqual({ + messages: [ + { + role: "assistant", + content: [ + { + type: "server_tool_use", + id: "editor_call", + name: "text_editor_code_execution", + input: { command: "view", path: "/tmp/test.txt" }, + }, + { + type: "text_editor_code_execution_tool_result", + tool_use_id: "editor_call", + content: { + type: "text_editor_code_execution_view_result", + file_type: "text", + content: "file contents here", + num_lines: 1, + start_line: 1, + total_lines: 1, + }, + }, + ], + }, + ], + system: undefined, + }); +}); diff --git a/libs/providers/langchain-anthropic/src/utils/message_inputs.ts b/libs/providers/langchain-anthropic/src/utils/message_inputs.ts index 8eedf3eaec90..c12a5bef2a75 100644 --- a/libs/providers/langchain-anthropic/src/utils/message_inputs.ts +++ b/libs/providers/langchain-anthropic/src/utils/message_inputs.ts @@ -155,12 +155,14 @@ function* _formatContentBlocks( content: ContentBlock[] ): Generator { const toolTypes = [ - "tool_use", - "tool_result", + "bash_code_execution_tool_result", "input_json_delta", "server_tool_use", - "web_search_tool_result", + "text_editor_code_execution_tool_result", + "tool_result", + "tool_use", "web_search_result", + "web_search_tool_result", ]; const textTypes = ["text", "text_delta"]; for (const contentPart of content) {