| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- import { convertToOpenAICompatibleChatMessages as convertToCopilotMessages } from "@/provider/sdk/copilot/chat/convert-to-openai-compatible-chat-messages"
- import { describe, test, expect } from "bun:test"
- describe("system messages", () => {
- test("should convert system message content to string", () => {
- const result = convertToCopilotMessages([
- {
- role: "system",
- content: "You are a helpful assistant with AGENTS.md instructions.",
- },
- ])
- expect(result).toEqual([
- {
- role: "system",
- content: "You are a helpful assistant with AGENTS.md instructions.",
- },
- ])
- })
- })
- describe("user messages", () => {
- test("should convert messages with only a text part to a string content", () => {
- const result = convertToCopilotMessages([
- {
- role: "user",
- content: [{ type: "text", text: "Hello" }],
- },
- ])
- expect(result).toEqual([{ role: "user", content: "Hello" }])
- })
- test("should convert messages with image parts", () => {
- const result = convertToCopilotMessages([
- {
- role: "user",
- content: [
- { type: "text", text: "Hello" },
- {
- type: "file",
- data: Buffer.from([0, 1, 2, 3]).toString("base64"),
- mediaType: "image/png",
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Hello" },
- {
- type: "image_url",
- image_url: { url: "data:image/png;base64,AAECAw==" },
- },
- ],
- },
- ])
- })
- test("should convert messages with image parts from Uint8Array", () => {
- const result = convertToCopilotMessages([
- {
- role: "user",
- content: [
- { type: "text", text: "Hi" },
- {
- type: "file",
- data: new Uint8Array([0, 1, 2, 3]),
- mediaType: "image/png",
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Hi" },
- {
- type: "image_url",
- image_url: { url: "data:image/png;base64,AAECAw==" },
- },
- ],
- },
- ])
- })
- test("should handle URL-based images", () => {
- const result = convertToCopilotMessages([
- {
- role: "user",
- content: [
- {
- type: "file",
- data: new URL("https://example.com/image.jpg"),
- mediaType: "image/*",
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "user",
- content: [
- {
- type: "image_url",
- image_url: { url: "https://example.com/image.jpg" },
- },
- ],
- },
- ])
- })
- test("should handle multiple text parts without flattening", () => {
- const result = convertToCopilotMessages([
- {
- role: "user",
- content: [
- { type: "text", text: "Part 1" },
- { type: "text", text: "Part 2" },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Part 1" },
- { type: "text", text: "Part 2" },
- ],
- },
- ])
- })
- })
- describe("assistant messages", () => {
- test("should convert assistant text messages", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [{ type: "text", text: "Hello back!" }],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: "Hello back!",
- tool_calls: undefined,
- reasoning_text: undefined,
- reasoning_opaque: undefined,
- },
- ])
- })
- test("should handle assistant message with null content when only tool calls", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- {
- type: "tool-call",
- toolCallId: "call1",
- toolName: "calculator",
- input: { a: 1, b: 2 },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: null,
- tool_calls: [
- {
- id: "call1",
- type: "function",
- function: {
- name: "calculator",
- arguments: JSON.stringify({ a: 1, b: 2 }),
- },
- },
- ],
- reasoning_text: undefined,
- reasoning_opaque: undefined,
- },
- ])
- })
- test("should concatenate multiple text parts", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- { type: "text", text: "First part. " },
- { type: "text", text: "Second part." },
- ],
- },
- ])
- expect(result[0].content).toBe("First part. Second part.")
- })
- })
- describe("tool calls", () => {
- test("should stringify arguments to tool calls", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- {
- type: "tool-call",
- input: { foo: "bar123" },
- toolCallId: "quux",
- toolName: "thwomp",
- },
- ],
- },
- {
- role: "tool",
- content: [
- {
- type: "tool-result",
- toolCallId: "quux",
- toolName: "thwomp",
- output: { type: "json", value: { oof: "321rab" } },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: null,
- tool_calls: [
- {
- id: "quux",
- type: "function",
- function: {
- name: "thwomp",
- arguments: JSON.stringify({ foo: "bar123" }),
- },
- },
- ],
- reasoning_text: undefined,
- reasoning_opaque: undefined,
- },
- {
- role: "tool",
- tool_call_id: "quux",
- content: JSON.stringify({ oof: "321rab" }),
- },
- ])
- })
- test("should handle text output type in tool results", () => {
- const result = convertToCopilotMessages([
- {
- role: "tool",
- content: [
- {
- type: "tool-result",
- toolCallId: "call-1",
- toolName: "getWeather",
- output: { type: "text", value: "It is sunny today" },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "tool",
- tool_call_id: "call-1",
- content: "It is sunny today",
- },
- ])
- })
- test("should handle multiple tool results as separate messages", () => {
- const result = convertToCopilotMessages([
- {
- role: "tool",
- content: [
- {
- type: "tool-result",
- toolCallId: "call1",
- toolName: "api1",
- output: { type: "text", value: "Result 1" },
- },
- {
- type: "tool-result",
- toolCallId: "call2",
- toolName: "api2",
- output: { type: "text", value: "Result 2" },
- },
- ],
- },
- ])
- expect(result).toHaveLength(2)
- expect(result[0]).toEqual({
- role: "tool",
- tool_call_id: "call1",
- content: "Result 1",
- })
- expect(result[1]).toEqual({
- role: "tool",
- tool_call_id: "call2",
- content: "Result 2",
- })
- })
- test("should handle text plus multiple tool calls", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- { type: "text", text: "Checking... " },
- {
- type: "tool-call",
- toolCallId: "call1",
- toolName: "searchTool",
- input: { query: "Weather" },
- },
- { type: "text", text: "Almost there..." },
- {
- type: "tool-call",
- toolCallId: "call2",
- toolName: "mapsTool",
- input: { location: "Paris" },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: "Checking... Almost there...",
- tool_calls: [
- {
- id: "call1",
- type: "function",
- function: {
- name: "searchTool",
- arguments: JSON.stringify({ query: "Weather" }),
- },
- },
- {
- id: "call2",
- type: "function",
- function: {
- name: "mapsTool",
- arguments: JSON.stringify({ location: "Paris" }),
- },
- },
- ],
- reasoning_text: undefined,
- reasoning_opaque: undefined,
- },
- ])
- })
- })
- describe("reasoning (copilot-specific)", () => {
- test("should omit reasoning_text without reasoning_opaque", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "Let me think about this..." },
- { type: "text", text: "The answer is 42." },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: "The answer is 42.",
- tool_calls: undefined,
- reasoning_text: undefined,
- reasoning_opaque: undefined,
- },
- ])
- })
- test("should include reasoning_opaque from providerOptions", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "Thinking...",
- providerOptions: {
- copilot: { reasoningOpaque: "opaque-signature-123" },
- },
- },
- { type: "text", text: "Done!" },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: "Done!",
- tool_calls: undefined,
- reasoning_text: "Thinking...",
- reasoning_opaque: "opaque-signature-123",
- },
- ])
- })
- test("should include reasoning_opaque from text part providerOptions", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- {
- type: "text",
- text: "Done!",
- providerOptions: {
- copilot: { reasoningOpaque: "opaque-text-456" },
- },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: "Done!",
- tool_calls: undefined,
- reasoning_text: undefined,
- reasoning_opaque: "opaque-text-456",
- },
- ])
- })
- test("should handle reasoning-only assistant message", () => {
- const result = convertToCopilotMessages([
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "Just thinking, no response yet",
- providerOptions: {
- copilot: { reasoningOpaque: "sig-abc" },
- },
- },
- ],
- },
- ])
- expect(result).toEqual([
- {
- role: "assistant",
- content: null,
- tool_calls: undefined,
- reasoning_text: "Just thinking, no response yet",
- reasoning_opaque: "sig-abc",
- },
- ])
- })
- })
- describe("full conversation", () => {
- test("should convert a multi-turn conversation with reasoning", () => {
- const result = convertToCopilotMessages([
- {
- role: "system",
- content: "You are a helpful assistant.",
- },
- {
- role: "user",
- content: [{ type: "text", text: "What is 2+2?" }],
- },
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "Let me calculate 2+2...",
- providerOptions: {
- copilot: { reasoningOpaque: "sig-abc" },
- },
- },
- { type: "text", text: "2+2 equals 4." },
- ],
- },
- {
- role: "user",
- content: [{ type: "text", text: "What about 3+3?" }],
- },
- ])
- expect(result).toHaveLength(4)
- const systemMsg = result[0]
- expect(systemMsg.role).toBe("system")
- // Assistant message should have reasoning fields
- const assistantMsg = result[2] as {
- reasoning_text?: string
- reasoning_opaque?: string
- }
- expect(assistantMsg.reasoning_text).toBe("Let me calculate 2+2...")
- expect(assistantMsg.reasoning_opaque).toBe("sig-abc")
- })
- })
|