| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 |
- import { describe, expect, it } from "vitest";
- import { toAiSdkMessages } from "../src/model/index.js";
- import { createTestInputFormat } from "./test-model-format.js";
- describe("AI SDK model message transform", () => {
- it("uses a fixed wire-only fallback for empty user content", () => {
- expect(
- toAiSdkMessages([
- { role: "user", content: "" },
- { role: "assistant", content: "attachment context acknowledged" },
- { role: "user", content: [] },
- { role: "user", content: [{ type: "text", text: "" }] },
- { role: "user", content: " " },
- { role: "user", content: "\n" },
- { role: "user", content: " " },
- ]),
- ).toEqual([
- { role: "user", content: "(no content)" },
- { role: "assistant", content: "attachment context acknowledged" },
- { role: "user", content: "(no content)" },
- { role: "user", content: "(no content)" },
- { role: "user", content: " " },
- { role: "user", content: "\n" },
- { role: "user", content: " " },
- ]);
- });
- it("does not append the empty-user fallback to media or resource-only content", () => {
- const messages = toAiSdkMessages([
- {
- role: "user",
- content: [
- {
- type: "image",
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,aW1hZ2U=",
- },
- ],
- },
- {
- role: "user",
- content: [
- {
- type: "file",
- mediaType: "application/pdf",
- name: "report.pdf",
- dataUrl: "data:application/pdf;base64,cGRm",
- },
- ],
- },
- {
- role: "user",
- content: [{ type: "resource_link", uri: "https://example.com/ref" }],
- },
- ]);
- expect(messages.map((message) => message.content)).toEqual([
- [{ type: "image", image: "aW1hZ2U=", mediaType: "image/png" }],
- [
- {
- type: "file",
- data: "cGRm",
- filename: "report.pdf",
- mediaType: "application/pdf",
- },
- ],
- [{ type: "text", text: "[Resource: https://example.com/ref]" }],
- ]);
- expect(JSON.stringify(messages)).not.toContain("(no content)");
- });
- it("concatenates self-delimited leading system messages for OpenAI-compatible providers", () => {
- const messages = toAiSdkMessages(
- [
- { role: "system", content: "cli prefix" },
- {
- role: "system",
- content: [{ type: "text", text: "\nstable context" }],
- cacheControl: { type: "ephemeral" },
- },
- {
- role: "system",
- content: "\n\ndynamic context\n",
- cacheControl: { type: "ephemeral", ttl: "1h" },
- },
- { role: "user", content: "first question" },
- { role: "assistant", content: "first answer" },
- { role: "user", content: "follow-up" },
- ],
- { providerKind: "openai-compatible" },
- );
- expect(messages).toEqual([
- {
- role: "system",
- content: "cli prefix\nstable context\n\ndynamic context\n",
- providerOptions: {
- anthropic: {
- cacheControl: { type: "ephemeral", ttl: "1h" },
- },
- },
- },
- { role: "user", content: "first question" },
- { role: "assistant", content: "first answer" },
- { role: "user", content: "follow-up" },
- ]);
- });
- it("keeps zero or one leading system message unchanged for OpenAI-compatible providers", () => {
- expect(
- toAiSdkMessages([{ role: "user", content: "question" }], {
- providerKind: "openai-compatible",
- }),
- ).toEqual([{ role: "user", content: "question" }]);
- expect(
- toAiSdkMessages(
- [
- { role: "system", content: "single system" },
- { role: "user", content: "question" },
- ],
- { providerKind: "openai-compatible" },
- ),
- ).toEqual([
- { role: "system", content: "single system" },
- { role: "user", content: "question" },
- ]);
- });
- it("preserves non-leading system messages for OpenAI-compatible providers", () => {
- const messages = toAiSdkMessages(
- [
- { role: "system", content: "first leading system" },
- { role: "system", content: "second leading system" },
- { role: "user", content: "question" },
- { role: "system", content: "mid-conversation system" },
- { role: "assistant", content: "answer" },
- ],
- { providerKind: "openai-compatible" },
- );
- expect(messages).toEqual([
- {
- role: "system",
- content: "first leading systemsecond leading system",
- },
- { role: "user", content: "question" },
- { role: "system", content: "mid-conversation system" },
- { role: "assistant", content: "answer" },
- ]);
- });
- it("preserves multiple system messages for non-OpenAI-compatible providers", () => {
- const messages = toAiSdkMessages(
- [
- { role: "system", content: "first system" },
- { role: "system", content: "second system" },
- { role: "user", content: "question" },
- ],
- { providerKind: "anthropic" },
- );
- expect(messages).toEqual([
- { role: "system", content: "first system" },
- { role: "system", content: "second system" },
- { role: "user", content: "question" },
- ]);
- });
- it("preserves provider tool call ids across assistant calls and tool results", () => {
- const toolCallId = "call_-7666958008559069619";
- const messages = toAiSdkMessages([
- {
- role: "assistant",
- content: "",
- toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
- },
- {
- role: "tool",
- content: "ok",
- toolCallId,
- toolName: "Read",
- },
- ]);
- const assistantToolCall = (messages[0] as any).content[0];
- const toolResult = (messages[1] as any).content[0];
- expect(assistantToolCall.toolCallId).toBe(toolCallId);
- expect(toolResult.toolCallId).toBe(toolCallId);
- });
- it("drops OpenAI Responses stored reasoning item references on stateless replay", () => {
- const toolCallId = "call_read";
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "stored reasoning",
- providerOptions: { openai: { itemId: "rs_1" } },
- },
- { type: "text", text: "checking" },
- ],
- toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
- },
- ],
- {
- providerKind: "openai",
- providerOptions: { apiFormat: "openai-responses" },
- },
- );
- expect((messages[0] as any).content).toEqual([
- { type: "text", text: "checking" },
- {
- type: "tool-call",
- toolCallId,
- toolName: "Read",
- input: { file_path: "README.md" },
- },
- ]);
- });
- it("preserves OpenAI Responses stored reasoning item references with previousResponseId", () => {
- const toolCallId = "call_read";
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "stored reasoning",
- providerOptions: { openai: { itemId: "rs_1" } },
- },
- { type: "text", text: "checking" },
- ],
- toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
- },
- ],
- {
- providerKind: "openai",
- providerOptions: {
- apiFormat: "openai-responses",
- openai: { previousResponseId: "resp_1" },
- },
- },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: "stored reasoning",
- providerOptions: { openai: { itemId: "rs_1" } },
- },
- { type: "text", text: "checking" },
- {
- type: "tool-call",
- toolCallId,
- toolName: "Read",
- input: { file_path: "README.md" },
- },
- ]);
- });
- it("does not drop stored reasoning references for OpenAI-compatible providers", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "compatible reasoning",
- providerOptions: { openai: { itemId: "rs_1" } },
- },
- { type: "text", text: "done" },
- ],
- },
- ],
- {
- providerKind: "openai-compatible",
- providerOptions: { apiFormat: "openai-responses" },
- },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: "compatible reasoning",
- providerOptions: { openai: { itemId: "rs_1" } },
- },
- { type: "text", text: "done" },
- ]);
- });
- it("translates image tool results into structured media output", () => {
- const toolCallId = "call_read_image";
- const messages = toAiSdkMessages([
- {
- role: "tool",
- content: [
- {
- type: "image",
- mediaType: "image/jpeg",
- dataUrl: "data:image/jpeg;base64,cmVhZC1pbWFnZQ==",
- source: { id: "read-image", kind: "inline", placeholder: "Read image" },
- },
- ],
- toolCallId,
- toolName: "Read",
- },
- ]);
- expect((messages[0] as any).content[0]).toEqual({
- type: "tool-result",
- toolCallId,
- toolName: "Read",
- output: {
- type: "content",
- value: [
- {
- type: "image-data",
- data: "cmVhZC1pbWFnZQ==",
- mediaType: "image/jpeg",
- },
- ],
- },
- });
- });
- it("does not send empty reasoning_content for a plain assistant history", () => {
- const messages = toAiSdkMessages([{ role: "assistant", content: "done" }], {});
- expect(messages).toEqual([{ role: "assistant", content: "done" }]);
- });
- it("removes an empty reasoning block without configured replay metadata", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "" },
- { type: "text", text: "done" },
- ],
- },
- ],
- {},
- );
- expect(messages).toEqual([{ role: "assistant", content: [{ type: "text", text: "done" }] }]);
- });
- it("removes a sole empty reasoning block without configured replay metadata", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [{ type: "reasoning", text: "" }],
- },
- ],
- {},
- );
- expect(messages).toEqual([{ role: "assistant", content: [] }]);
- });
- it("preserves signed reasoning blocks on Anthropic-compatible transport", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- {
- type: "reasoning",
- text: "anthropic thinking",
- providerOptions: { anthropic: { signature: "sig_1" } },
- },
- { type: "text", text: "done" },
- ],
- },
- ],
- {
- providerKind: "anthropic",
- },
- );
- expect((messages[0] as any).providerOptions).toBeUndefined();
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: "anthropic thinking",
- providerOptions: { anthropic: { signature: "sig_1" } },
- },
- { type: "text", text: "done" },
- ]);
- });
- it("preserves unsigned reasoning blocks on Anthropic transports", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "thinking before a tool" },
- { type: "text", text: "done" },
- ],
- },
- ],
- {
- providerKind: "anthropic",
- },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: "thinking before a tool",
- providerOptions: { anthropic: { signature: "" } },
- },
- { type: "text", text: "done" },
- ]);
- });
- it.each([
- ["missing provider metadata", undefined],
- ["empty provider metadata", {}],
- ] as const)("removes exact-empty reasoning stubs with %s", (_name, providerOptions) => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "", providerOptions },
- { type: "text", text: "done" },
- ],
- },
- ],
- { providerKind: "anthropic" },
- );
- expect((messages[0] as any).content).toEqual([{ type: "text", text: "done" }]);
- });
- it("preserves whitespace-only reasoning without provider metadata", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: " \n" },
- { type: "text", text: "done" },
- ],
- },
- ],
- { providerKind: "anthropic" },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: " \n",
- providerOptions: { anthropic: { signature: "" } },
- },
- { type: "text", text: "done" },
- ]);
- });
- it.each([
- [
- "an empty Anthropic signature",
- { anthropic: { signature: "" } },
- { providerKind: "anthropic" },
- ],
- [
- "Anthropic redacted data",
- { anthropic: { redactedData: "opaque" } },
- { providerKind: "anthropic" },
- ],
- [
- "an OpenAI Responses item id",
- { openai: { itemId: "rs_empty" } },
- {
- providerKind: "openai",
- providerOptions: {
- apiFormat: "openai-responses",
- openai: { previousResponseId: "resp_1" },
- },
- },
- ],
- [
- "OpenAI Responses encrypted content",
- { openai: { reasoningEncryptedContent: "encrypted" } },
- {
- providerKind: "openai",
- providerOptions: { apiFormat: "openai-responses", openai: { store: false } },
- },
- ],
- [
- "unknown provider metadata",
- { futureProvider: { opaque: "value" } },
- { providerKind: "custom" },
- ],
- ] as const)("preserves exact-empty reasoning carrying %s", (_name, providerOptions, options) => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "", providerOptions },
- { type: "text", text: "done" },
- ],
- },
- ],
- options,
- );
- expect((messages[0] as any).content[0]).toEqual({
- type: "reasoning",
- text: "",
- providerOptions,
- });
- });
- it("adds empty Anthropic signature for unsigned reasoning blocks", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "thinking before a tool" },
- { type: "text", text: "done" },
- ],
- },
- ],
- {
- providerKind: "anthropic",
- },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "reasoning",
- text: "thinking before a tool",
- providerOptions: { anthropic: { signature: "" } },
- },
- { type: "text", text: "done" },
- ]);
- });
- it("does not apply reasoning replay without explicit compatibility", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "legacy deepseek reasoning" },
- { type: "text", text: "done" },
- ],
- },
- ],
- {
- providerKind: "openai-compatible",
- },
- );
- expect((messages[0] as any).providerOptions).toBeUndefined();
- expect((messages[0] as any).content).toEqual([
- { type: "reasoning", text: "legacy deepseek reasoning" },
- { type: "text", text: "done" },
- ]);
- });
- it("preserves reasoning blocks for assistant history without replay compatibility", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: [
- { type: "reasoning", text: "private scratchpad" },
- { type: "text", text: "done" },
- ],
- },
- ],
- {},
- );
- expect((messages[0] as any).content).toEqual([
- { type: "reasoning", text: "private scratchpad" },
- { type: "text", text: "done" },
- ]);
- });
- it("leaves plain assistant messages unchanged", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "assistant",
- content: "done",
- },
- ],
- {},
- );
- expect((messages[0] as any).providerOptions).toBeUndefined();
- expect((messages[0] as any).content).toBe("done");
- });
- it("translates provider-neutral cache control to Anthropic provider options", () => {
- const messages = toAiSdkMessages([
- {
- role: "system",
- content: "stable context",
- cacheControl: { type: "ephemeral" },
- },
- {
- role: "user",
- content: "prompt",
- cacheControl: { type: "ephemeral", ttl: "1h" },
- },
- {
- role: "tool",
- content: "tool result",
- toolCallId: "tool-1",
- toolName: "Read",
- cacheControl: { type: "ephemeral" },
- },
- ]);
- expect((messages[0] as any).providerOptions).toEqual({
- anthropic: {
- cacheControl: { type: "ephemeral" },
- },
- });
- expect((messages[1] as any).providerOptions).toEqual({
- anthropic: {
- cacheControl: { type: "ephemeral", ttl: "1h" },
- },
- });
- expect((messages[2] as any).providerOptions).toBeUndefined();
- expect((messages[2] as any).content[0].providerOptions).toEqual({
- anthropic: {
- cacheControl: { type: "ephemeral" },
- },
- });
- });
- it("translates user image content blocks to AI SDK image parts", () => {
- const messages = toAiSdkMessages([
- {
- role: "user",
- content: [
- { type: "text", text: "what is in this image?" },
- {
- type: "image",
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,aW1hZ2U=",
- source: { id: "image-1", kind: "inline", placeholder: "[Image 1]" },
- },
- ],
- },
- ]);
- expect((messages[0] as any).content).toEqual([
- { type: "text", text: "what is in this image?" },
- { type: "image", image: "aW1hZ2U=", mediaType: "image/png" },
- ]);
- });
- it("can strip media blocks to stable text placeholders", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "user",
- content: [
- {
- type: "image",
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,aW1hZ2U=",
- source: { id: "image-1", kind: "inline", placeholder: "[Image 1]" },
- },
- ],
- },
- ],
- { stripMedia: true },
- );
- expect((messages[0] as any).content).toEqual([
- { type: "text", text: "[Attached image/png: [Image 1]]" },
- ]);
- });
- it("turns unsupported image input into an explicit model-visible error", () => {
- const messages = toAiSdkMessages(
- [
- {
- role: "user",
- content: [
- {
- type: "image",
- mediaType: "image/png",
- dataUrl: "data:image/png;base64,aW1hZ2U=",
- },
- ],
- },
- ],
- { inputFormat: createTestInputFormat({ supportsImage: false }) },
- );
- expect((messages[0] as any).content).toEqual([
- {
- type: "text",
- text: "[Attached image/png]\n[Media omitted from provider request because the selected model does not support image input.]",
- },
- ]);
- });
- it("translates PDF file blocks to AI SDK file parts", () => {
- const messages = toAiSdkMessages([
- {
- role: "user",
- content: [
- {
- type: "file",
- mediaType: "application/pdf",
- name: "report.pdf",
- dataUrl: "data:application/pdf;base64,cGRm",
- },
- ],
- },
- ]);
- expect((messages[0] as any).content).toEqual([
- {
- type: "file",
- data: "cGRm",
- filename: "report.pdf",
- mediaType: "application/pdf",
- },
- ]);
- });
- });
|