mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-22 00:00:27 +00:00
Remove tool calls for which there are no results in subsequent user messages.
This commit is contained in:
parent
0e932a97df
commit
51f5448a5c
4 changed files with 169 additions and 29 deletions
82
packages/ai/test/tool-call-without-result.test.ts
Normal file
82
packages/ai/test/tool-call-without-result.test.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import { type Static, Type } from "@sinclair/typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
import type { Context, Tool } from "../src/types.js";
|
||||
|
||||
// Simple calculate tool
|
||||
const calculateSchema = Type.Object({
|
||||
expression: Type.String({ description: "The mathematical expression to evaluate" }),
|
||||
});
|
||||
|
||||
type CalculateParams = Static<typeof calculateSchema>;
|
||||
|
||||
const calculateTool: Tool = {
|
||||
name: "calculate",
|
||||
description: "Evaluate mathematical expressions",
|
||||
parameters: calculateSchema,
|
||||
};
|
||||
|
||||
describe("Tool Call Without Result Tests", () => {
|
||||
describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic Provider - Missing Tool Result", () => {
|
||||
const model = getModel("anthropic", "claude-3-5-haiku-20241022");
|
||||
|
||||
it("should filter out tool calls without corresponding tool results", async () => {
|
||||
// Step 1: Create context with the calculate tool
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant. Use the calculate tool when asked to perform calculations.",
|
||||
messages: [],
|
||||
tools: [calculateTool],
|
||||
};
|
||||
|
||||
// Step 2: Ask the LLM to make a tool call
|
||||
context.messages.push({
|
||||
role: "user",
|
||||
content: "Please calculate 25 * 18 using the calculate tool.",
|
||||
});
|
||||
|
||||
// Step 3: Get the assistant's response (should contain a tool call)
|
||||
const firstResponse = await complete(model, context);
|
||||
context.messages.push(firstResponse);
|
||||
|
||||
console.log("First response:", JSON.stringify(firstResponse, null, 2));
|
||||
|
||||
// Verify the response contains a tool call
|
||||
const hasToolCall = firstResponse.content.some((block) => block.type === "toolCall");
|
||||
expect(hasToolCall).toBe(true);
|
||||
|
||||
if (!hasToolCall) {
|
||||
throw new Error("Expected assistant to make a tool call, but none was found");
|
||||
}
|
||||
|
||||
// Step 4: Send a user message WITHOUT providing tool result
|
||||
// This simulates the scenario where a tool call was aborted/cancelled
|
||||
context.messages.push({
|
||||
role: "user",
|
||||
content: "Never mind, just tell me what is 2+2?",
|
||||
});
|
||||
|
||||
// Step 5: The fix should filter out the orphaned tool call, and the request should succeed
|
||||
const secondResponse = await complete(model, context);
|
||||
console.log("Second response:", JSON.stringify(secondResponse, null, 2));
|
||||
|
||||
// The request should succeed (not error) - that's the main thing we're testing
|
||||
expect(secondResponse.stopReason).not.toBe("error");
|
||||
|
||||
// Should have some content in the response
|
||||
expect(secondResponse.content.length).toBeGreaterThan(0);
|
||||
|
||||
// The LLM may choose to answer directly or make a new tool call - either is fine
|
||||
// The important thing is it didn't fail with the orphaned tool call error
|
||||
const textContent = secondResponse.content
|
||||
.filter((block) => block.type === "text")
|
||||
.map((block) => (block.type === "text" ? block.text : ""))
|
||||
.join(" ");
|
||||
expect(textContent.length).toBeGreaterThan(0);
|
||||
console.log("Answer:", textContent);
|
||||
|
||||
// Verify the stop reason is either "stop" or "toolUse" (new tool call)
|
||||
expect(["stop", "toolUse"]).toContain(secondResponse.stopReason);
|
||||
}, 30000);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue