Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions libs/providers/langchain-anthropic/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ function* _formatContentBlocks(
content: ContentBlock[]
): Generator<Anthropic.Beta.BetaContentBlockParam> {
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) {
Expand Down