|
|
@@ -4,6 +4,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
|
|
|
import OpenAI from "openai"
|
|
|
|
|
|
import { convertToOpenAiMessages } from "../openai-format"
|
|
|
+import { normalizeMistralToolCallId } from "../mistral-format"
|
|
|
|
|
|
describe("convertToOpenAiMessages", () => {
|
|
|
it("should convert simple text messages", () => {
|
|
|
@@ -70,7 +71,7 @@ describe("convertToOpenAiMessages", () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- it("should handle assistant messages with tool use", () => {
|
|
|
+ it("should handle assistant messages with tool use (no normalization without normalizeToolCallId)", () => {
|
|
|
const anthropicMessages: Anthropic.Messages.MessageParam[] = [
|
|
|
{
|
|
|
role: "assistant",
|
|
|
@@ -97,7 +98,7 @@ describe("convertToOpenAiMessages", () => {
|
|
|
expect(assistantMessage.content).toBe("Let me check the weather.")
|
|
|
expect(assistantMessage.tool_calls).toHaveLength(1)
|
|
|
expect(assistantMessage.tool_calls![0]).toEqual({
|
|
|
- id: "weather-123",
|
|
|
+ id: "weather-123", // Not normalized without normalizeToolCallId function
|
|
|
type: "function",
|
|
|
function: {
|
|
|
name: "get_weather",
|
|
|
@@ -106,7 +107,7 @@ describe("convertToOpenAiMessages", () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- it("should handle user messages with tool results", () => {
|
|
|
+ it("should handle user messages with tool results (no normalization without normalizeToolCallId)", () => {
|
|
|
const anthropicMessages: Anthropic.Messages.MessageParam[] = [
|
|
|
{
|
|
|
role: "user",
|
|
|
@@ -125,7 +126,102 @@ describe("convertToOpenAiMessages", () => {
|
|
|
|
|
|
const toolMessage = openAiMessages[0] as OpenAI.Chat.ChatCompletionToolMessageParam
|
|
|
expect(toolMessage.role).toBe("tool")
|
|
|
- expect(toolMessage.tool_call_id).toBe("weather-123")
|
|
|
+ expect(toolMessage.tool_call_id).toBe("weather-123") // Not normalized without normalizeToolCallId function
|
|
|
expect(toolMessage.content).toBe("Current temperature in London: 20°C")
|
|
|
})
|
|
|
+
|
|
|
+ it("should normalize tool call IDs when normalizeToolCallId function is provided", () => {
|
|
|
+ const anthropicMessages: Anthropic.Messages.MessageParam[] = [
|
|
|
+ {
|
|
|
+ role: "assistant",
|
|
|
+ content: [
|
|
|
+ {
|
|
|
+ type: "tool_use",
|
|
|
+ id: "call_5019f900a247472bacde0b82",
|
|
|
+ name: "read_file",
|
|
|
+ input: { path: "test.ts" },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ role: "user",
|
|
|
+ content: [
|
|
|
+ {
|
|
|
+ type: "tool_result",
|
|
|
+ tool_use_id: "call_5019f900a247472bacde0b82",
|
|
|
+ content: "file contents",
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]
|
|
|
+
|
|
|
+ // With normalizeToolCallId function - should normalize
|
|
|
+ const openAiMessages = convertToOpenAiMessages(anthropicMessages, {
|
|
|
+ normalizeToolCallId: normalizeMistralToolCallId,
|
|
|
+ })
|
|
|
+
|
|
|
+ const assistantMessage = openAiMessages[0] as OpenAI.Chat.ChatCompletionAssistantMessageParam
|
|
|
+ expect(assistantMessage.tool_calls![0].id).toBe(normalizeMistralToolCallId("call_5019f900a247472bacde0b82"))
|
|
|
+
|
|
|
+ const toolMessage = openAiMessages[1] as OpenAI.Chat.ChatCompletionToolMessageParam
|
|
|
+ expect(toolMessage.tool_call_id).toBe(normalizeMistralToolCallId("call_5019f900a247472bacde0b82"))
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should not normalize tool call IDs when normalizeToolCallId function is not provided", () => {
|
|
|
+ const anthropicMessages: Anthropic.Messages.MessageParam[] = [
|
|
|
+ {
|
|
|
+ role: "assistant",
|
|
|
+ content: [
|
|
|
+ {
|
|
|
+ type: "tool_use",
|
|
|
+ id: "call_5019f900a247472bacde0b82",
|
|
|
+ name: "read_file",
|
|
|
+ input: { path: "test.ts" },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ role: "user",
|
|
|
+ content: [
|
|
|
+ {
|
|
|
+ type: "tool_result",
|
|
|
+ tool_use_id: "call_5019f900a247472bacde0b82",
|
|
|
+ content: "file contents",
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]
|
|
|
+
|
|
|
+ // Without normalizeToolCallId function - should NOT normalize
|
|
|
+ const openAiMessages = convertToOpenAiMessages(anthropicMessages, {})
|
|
|
+
|
|
|
+ const assistantMessage = openAiMessages[0] as OpenAI.Chat.ChatCompletionAssistantMessageParam
|
|
|
+ expect(assistantMessage.tool_calls![0].id).toBe("call_5019f900a247472bacde0b82")
|
|
|
+
|
|
|
+ const toolMessage = openAiMessages[1] as OpenAI.Chat.ChatCompletionToolMessageParam
|
|
|
+ expect(toolMessage.tool_call_id).toBe("call_5019f900a247472bacde0b82")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should use custom normalization function when provided", () => {
|
|
|
+ const anthropicMessages: Anthropic.Messages.MessageParam[] = [
|
|
|
+ {
|
|
|
+ role: "assistant",
|
|
|
+ content: [
|
|
|
+ {
|
|
|
+ type: "tool_use",
|
|
|
+ id: "toolu_123",
|
|
|
+ name: "test_tool",
|
|
|
+ input: {},
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]
|
|
|
+
|
|
|
+ // Custom normalization function that prefixes with "custom_"
|
|
|
+ const customNormalizer = (id: string) => `custom_${id}`
|
|
|
+ const openAiMessages = convertToOpenAiMessages(anthropicMessages, { normalizeToolCallId: customNormalizer })
|
|
|
+
|
|
|
+ const assistantMessage = openAiMessages[0] as OpenAI.Chat.ChatCompletionAssistantMessageParam
|
|
|
+ expect(assistantMessage.tool_calls![0].id).toBe("custom_toolu_123")
|
|
|
+ })
|
|
|
})
|