model-transform.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. import { describe, expect, it } from "vitest";
  2. import { toAiSdkMessages } from "../src/model/index.js";
  3. import { createTestInputFormat } from "./test-model-format.js";
  4. describe("AI SDK model message transform", () => {
  5. it("uses a fixed wire-only fallback for empty user content", () => {
  6. expect(
  7. toAiSdkMessages([
  8. { role: "user", content: "" },
  9. { role: "assistant", content: "attachment context acknowledged" },
  10. { role: "user", content: [] },
  11. { role: "user", content: [{ type: "text", text: "" }] },
  12. { role: "user", content: " " },
  13. { role: "user", content: "\n" },
  14. { role: "user", content: " " },
  15. ]),
  16. ).toEqual([
  17. { role: "user", content: "(no content)" },
  18. { role: "assistant", content: "attachment context acknowledged" },
  19. { role: "user", content: "(no content)" },
  20. { role: "user", content: "(no content)" },
  21. { role: "user", content: " " },
  22. { role: "user", content: "\n" },
  23. { role: "user", content: " " },
  24. ]);
  25. });
  26. it("does not append the empty-user fallback to media or resource-only content", () => {
  27. const messages = toAiSdkMessages([
  28. {
  29. role: "user",
  30. content: [
  31. {
  32. type: "image",
  33. mediaType: "image/png",
  34. dataUrl: "data:image/png;base64,aW1hZ2U=",
  35. },
  36. ],
  37. },
  38. {
  39. role: "user",
  40. content: [
  41. {
  42. type: "file",
  43. mediaType: "application/pdf",
  44. name: "report.pdf",
  45. dataUrl: "data:application/pdf;base64,cGRm",
  46. },
  47. ],
  48. },
  49. {
  50. role: "user",
  51. content: [{ type: "resource_link", uri: "https://example.com/ref" }],
  52. },
  53. ]);
  54. expect(messages.map((message) => message.content)).toEqual([
  55. [{ type: "image", image: "aW1hZ2U=", mediaType: "image/png" }],
  56. [
  57. {
  58. type: "file",
  59. data: "cGRm",
  60. filename: "report.pdf",
  61. mediaType: "application/pdf",
  62. },
  63. ],
  64. [{ type: "text", text: "[Resource: https://example.com/ref]" }],
  65. ]);
  66. expect(JSON.stringify(messages)).not.toContain("(no content)");
  67. });
  68. it("concatenates self-delimited leading system messages for OpenAI-compatible providers", () => {
  69. const messages = toAiSdkMessages(
  70. [
  71. { role: "system", content: "cli prefix" },
  72. {
  73. role: "system",
  74. content: [{ type: "text", text: "\nstable context" }],
  75. cacheControl: { type: "ephemeral" },
  76. },
  77. {
  78. role: "system",
  79. content: "\n\ndynamic context\n",
  80. cacheControl: { type: "ephemeral", ttl: "1h" },
  81. },
  82. { role: "user", content: "first question" },
  83. { role: "assistant", content: "first answer" },
  84. { role: "user", content: "follow-up" },
  85. ],
  86. { providerKind: "openai-compatible" },
  87. );
  88. expect(messages).toEqual([
  89. {
  90. role: "system",
  91. content: "cli prefix\nstable context\n\ndynamic context\n",
  92. providerOptions: {
  93. anthropic: {
  94. cacheControl: { type: "ephemeral", ttl: "1h" },
  95. },
  96. },
  97. },
  98. { role: "user", content: "first question" },
  99. { role: "assistant", content: "first answer" },
  100. { role: "user", content: "follow-up" },
  101. ]);
  102. });
  103. it("keeps zero or one leading system message unchanged for OpenAI-compatible providers", () => {
  104. expect(
  105. toAiSdkMessages([{ role: "user", content: "question" }], {
  106. providerKind: "openai-compatible",
  107. }),
  108. ).toEqual([{ role: "user", content: "question" }]);
  109. expect(
  110. toAiSdkMessages(
  111. [
  112. { role: "system", content: "single system" },
  113. { role: "user", content: "question" },
  114. ],
  115. { providerKind: "openai-compatible" },
  116. ),
  117. ).toEqual([
  118. { role: "system", content: "single system" },
  119. { role: "user", content: "question" },
  120. ]);
  121. });
  122. it("preserves non-leading system messages for OpenAI-compatible providers", () => {
  123. const messages = toAiSdkMessages(
  124. [
  125. { role: "system", content: "first leading system" },
  126. { role: "system", content: "second leading system" },
  127. { role: "user", content: "question" },
  128. { role: "system", content: "mid-conversation system" },
  129. { role: "assistant", content: "answer" },
  130. ],
  131. { providerKind: "openai-compatible" },
  132. );
  133. expect(messages).toEqual([
  134. {
  135. role: "system",
  136. content: "first leading systemsecond leading system",
  137. },
  138. { role: "user", content: "question" },
  139. { role: "system", content: "mid-conversation system" },
  140. { role: "assistant", content: "answer" },
  141. ]);
  142. });
  143. it("preserves multiple system messages for non-OpenAI-compatible providers", () => {
  144. const messages = toAiSdkMessages(
  145. [
  146. { role: "system", content: "first system" },
  147. { role: "system", content: "second system" },
  148. { role: "user", content: "question" },
  149. ],
  150. { providerKind: "anthropic" },
  151. );
  152. expect(messages).toEqual([
  153. { role: "system", content: "first system" },
  154. { role: "system", content: "second system" },
  155. { role: "user", content: "question" },
  156. ]);
  157. });
  158. it("preserves provider tool call ids across assistant calls and tool results", () => {
  159. const toolCallId = "call_-7666958008559069619";
  160. const messages = toAiSdkMessages([
  161. {
  162. role: "assistant",
  163. content: "",
  164. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  165. },
  166. {
  167. role: "tool",
  168. content: "ok",
  169. toolCallId,
  170. toolName: "Read",
  171. },
  172. ]);
  173. const assistantToolCall = (messages[0] as any).content[0];
  174. const toolResult = (messages[1] as any).content[0];
  175. expect(assistantToolCall.toolCallId).toBe(toolCallId);
  176. expect(toolResult.toolCallId).toBe(toolCallId);
  177. });
  178. it("drops OpenAI Responses stored reasoning item references on stateless replay", () => {
  179. const toolCallId = "call_read";
  180. const messages = toAiSdkMessages(
  181. [
  182. {
  183. role: "assistant",
  184. content: [
  185. {
  186. type: "reasoning",
  187. text: "stored reasoning",
  188. providerOptions: { openai: { itemId: "rs_1" } },
  189. },
  190. { type: "text", text: "checking" },
  191. ],
  192. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  193. },
  194. ],
  195. {
  196. providerKind: "openai",
  197. providerOptions: { apiFormat: "openai-responses" },
  198. },
  199. );
  200. expect((messages[0] as any).content).toEqual([
  201. { type: "text", text: "checking" },
  202. {
  203. type: "tool-call",
  204. toolCallId,
  205. toolName: "Read",
  206. input: { file_path: "README.md" },
  207. },
  208. ]);
  209. });
  210. it("preserves OpenAI Responses stored reasoning item references with previousResponseId", () => {
  211. const toolCallId = "call_read";
  212. const messages = toAiSdkMessages(
  213. [
  214. {
  215. role: "assistant",
  216. content: [
  217. {
  218. type: "reasoning",
  219. text: "stored reasoning",
  220. providerOptions: { openai: { itemId: "rs_1" } },
  221. },
  222. { type: "text", text: "checking" },
  223. ],
  224. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  225. },
  226. ],
  227. {
  228. providerKind: "openai",
  229. providerOptions: {
  230. apiFormat: "openai-responses",
  231. openai: { previousResponseId: "resp_1" },
  232. },
  233. },
  234. );
  235. expect((messages[0] as any).content).toEqual([
  236. {
  237. type: "reasoning",
  238. text: "stored reasoning",
  239. providerOptions: { openai: { itemId: "rs_1" } },
  240. },
  241. { type: "text", text: "checking" },
  242. {
  243. type: "tool-call",
  244. toolCallId,
  245. toolName: "Read",
  246. input: { file_path: "README.md" },
  247. },
  248. ]);
  249. });
  250. it("does not drop stored reasoning references for OpenAI-compatible providers", () => {
  251. const messages = toAiSdkMessages(
  252. [
  253. {
  254. role: "assistant",
  255. content: [
  256. {
  257. type: "reasoning",
  258. text: "compatible reasoning",
  259. providerOptions: { openai: { itemId: "rs_1" } },
  260. },
  261. { type: "text", text: "done" },
  262. ],
  263. },
  264. ],
  265. {
  266. providerKind: "openai-compatible",
  267. providerOptions: { apiFormat: "openai-responses" },
  268. },
  269. );
  270. expect((messages[0] as any).content).toEqual([
  271. {
  272. type: "reasoning",
  273. text: "compatible reasoning",
  274. providerOptions: { openai: { itemId: "rs_1" } },
  275. },
  276. { type: "text", text: "done" },
  277. ]);
  278. });
  279. it("translates image tool results into structured media output", () => {
  280. const toolCallId = "call_read_image";
  281. const messages = toAiSdkMessages([
  282. {
  283. role: "tool",
  284. content: [
  285. {
  286. type: "image",
  287. mediaType: "image/jpeg",
  288. dataUrl: "data:image/jpeg;base64,cmVhZC1pbWFnZQ==",
  289. source: { id: "read-image", kind: "inline", placeholder: "Read image" },
  290. },
  291. ],
  292. toolCallId,
  293. toolName: "Read",
  294. },
  295. ]);
  296. expect((messages[0] as any).content[0]).toEqual({
  297. type: "tool-result",
  298. toolCallId,
  299. toolName: "Read",
  300. output: {
  301. type: "content",
  302. value: [
  303. {
  304. type: "image-data",
  305. data: "cmVhZC1pbWFnZQ==",
  306. mediaType: "image/jpeg",
  307. },
  308. ],
  309. },
  310. });
  311. });
  312. it("does not send empty reasoning_content for a plain assistant history", () => {
  313. const messages = toAiSdkMessages([{ role: "assistant", content: "done" }], {});
  314. expect(messages).toEqual([{ role: "assistant", content: "done" }]);
  315. });
  316. it("removes an empty reasoning block without configured replay metadata", () => {
  317. const messages = toAiSdkMessages(
  318. [
  319. {
  320. role: "assistant",
  321. content: [
  322. { type: "reasoning", text: "" },
  323. { type: "text", text: "done" },
  324. ],
  325. },
  326. ],
  327. {},
  328. );
  329. expect(messages).toEqual([{ role: "assistant", content: [{ type: "text", text: "done" }] }]);
  330. });
  331. it("removes a sole empty reasoning block without configured replay metadata", () => {
  332. const messages = toAiSdkMessages(
  333. [
  334. {
  335. role: "assistant",
  336. content: [{ type: "reasoning", text: "" }],
  337. },
  338. ],
  339. {},
  340. );
  341. expect(messages).toEqual([{ role: "assistant", content: [] }]);
  342. });
  343. it("preserves signed reasoning blocks on Anthropic-compatible transport", () => {
  344. const messages = toAiSdkMessages(
  345. [
  346. {
  347. role: "assistant",
  348. content: [
  349. {
  350. type: "reasoning",
  351. text: "anthropic thinking",
  352. providerOptions: { anthropic: { signature: "sig_1" } },
  353. },
  354. { type: "text", text: "done" },
  355. ],
  356. },
  357. ],
  358. {
  359. providerKind: "anthropic",
  360. },
  361. );
  362. expect((messages[0] as any).providerOptions).toBeUndefined();
  363. expect((messages[0] as any).content).toEqual([
  364. {
  365. type: "reasoning",
  366. text: "anthropic thinking",
  367. providerOptions: { anthropic: { signature: "sig_1" } },
  368. },
  369. { type: "text", text: "done" },
  370. ]);
  371. });
  372. it.each([
  373. ["missing provider metadata", undefined],
  374. ["empty provider metadata", {}],
  375. ] as const)("removes exact-empty reasoning stubs with %s", (_name, providerOptions) => {
  376. const messages = toAiSdkMessages(
  377. [
  378. {
  379. role: "assistant",
  380. content: [
  381. { type: "reasoning", text: "", providerOptions },
  382. { type: "text", text: "done" },
  383. ],
  384. },
  385. ],
  386. { providerKind: "anthropic" },
  387. );
  388. expect((messages[0] as any).content).toEqual([{ type: "text", text: "done" }]);
  389. });
  390. it("preserves whitespace-only reasoning without provider metadata", () => {
  391. const messages = toAiSdkMessages(
  392. [
  393. {
  394. role: "assistant",
  395. content: [
  396. { type: "reasoning", text: " \n" },
  397. { type: "text", text: "done" },
  398. ],
  399. },
  400. ],
  401. { providerKind: "anthropic" },
  402. );
  403. expect((messages[0] as any).content).toEqual([
  404. {
  405. type: "reasoning",
  406. text: " \n",
  407. providerOptions: { anthropic: { signature: "" } },
  408. },
  409. { type: "text", text: "done" },
  410. ]);
  411. });
  412. it.each([
  413. [
  414. "an empty Anthropic signature",
  415. { anthropic: { signature: "" } },
  416. { providerKind: "anthropic" },
  417. ],
  418. [
  419. "Anthropic redacted data",
  420. { anthropic: { redactedData: "opaque" } },
  421. { providerKind: "anthropic" },
  422. ],
  423. [
  424. "an OpenAI Responses item id",
  425. { openai: { itemId: "rs_empty" } },
  426. {
  427. providerKind: "openai",
  428. providerOptions: {
  429. apiFormat: "openai-responses",
  430. openai: { previousResponseId: "resp_1" },
  431. },
  432. },
  433. ],
  434. [
  435. "OpenAI Responses encrypted content",
  436. { openai: { reasoningEncryptedContent: "encrypted" } },
  437. {
  438. providerKind: "openai",
  439. providerOptions: { apiFormat: "openai-responses", openai: { store: false } },
  440. },
  441. ],
  442. [
  443. "unknown provider metadata",
  444. { futureProvider: { opaque: "value" } },
  445. { providerKind: "custom" },
  446. ],
  447. ] as const)("preserves exact-empty reasoning carrying %s", (_name, providerOptions, options) => {
  448. const messages = toAiSdkMessages(
  449. [
  450. {
  451. role: "assistant",
  452. content: [
  453. { type: "reasoning", text: "", providerOptions },
  454. { type: "text", text: "done" },
  455. ],
  456. },
  457. ],
  458. options,
  459. );
  460. expect((messages[0] as any).content[0]).toEqual({
  461. type: "reasoning",
  462. text: "",
  463. providerOptions,
  464. });
  465. });
  466. it("adds empty Anthropic signature for unsigned reasoning blocks", () => {
  467. const messages = toAiSdkMessages(
  468. [
  469. {
  470. role: "assistant",
  471. content: [
  472. { type: "reasoning", text: "thinking before a tool" },
  473. { type: "text", text: "done" },
  474. ],
  475. },
  476. ],
  477. {
  478. providerKind: "anthropic",
  479. },
  480. );
  481. expect((messages[0] as any).content).toEqual([
  482. {
  483. type: "reasoning",
  484. text: "thinking before a tool",
  485. providerOptions: { anthropic: { signature: "" } },
  486. },
  487. { type: "text", text: "done" },
  488. ]);
  489. });
  490. it("does not apply reasoning replay without explicit compatibility", () => {
  491. const messages = toAiSdkMessages(
  492. [
  493. {
  494. role: "assistant",
  495. content: [
  496. { type: "reasoning", text: "legacy deepseek reasoning" },
  497. { type: "text", text: "done" },
  498. ],
  499. },
  500. ],
  501. {
  502. providerKind: "openai-compatible",
  503. },
  504. );
  505. expect((messages[0] as any).providerOptions).toBeUndefined();
  506. expect((messages[0] as any).content).toEqual([
  507. { type: "reasoning", text: "legacy deepseek reasoning" },
  508. { type: "text", text: "done" },
  509. ]);
  510. });
  511. it("preserves reasoning blocks for assistant history without replay compatibility", () => {
  512. const messages = toAiSdkMessages(
  513. [
  514. {
  515. role: "assistant",
  516. content: [
  517. { type: "reasoning", text: "private scratchpad" },
  518. { type: "text", text: "done" },
  519. ],
  520. },
  521. ],
  522. {},
  523. );
  524. expect((messages[0] as any).content).toEqual([
  525. { type: "reasoning", text: "private scratchpad" },
  526. { type: "text", text: "done" },
  527. ]);
  528. });
  529. it("translates provider-neutral cache control to Anthropic provider options", () => {
  530. const messages = toAiSdkMessages([
  531. {
  532. role: "system",
  533. content: "stable context",
  534. cacheControl: { type: "ephemeral" },
  535. },
  536. {
  537. role: "user",
  538. content: "prompt",
  539. cacheControl: { type: "ephemeral", ttl: "1h" },
  540. },
  541. {
  542. role: "tool",
  543. content: "tool result",
  544. toolCallId: "tool-1",
  545. toolName: "Read",
  546. cacheControl: { type: "ephemeral" },
  547. },
  548. ]);
  549. expect((messages[0] as any).providerOptions).toEqual({
  550. anthropic: {
  551. cacheControl: { type: "ephemeral" },
  552. },
  553. });
  554. expect((messages[1] as any).providerOptions).toEqual({
  555. anthropic: {
  556. cacheControl: { type: "ephemeral", ttl: "1h" },
  557. },
  558. });
  559. expect((messages[2] as any).providerOptions).toBeUndefined();
  560. expect((messages[2] as any).content[0].providerOptions).toEqual({
  561. anthropic: {
  562. cacheControl: { type: "ephemeral" },
  563. },
  564. });
  565. });
  566. it("translates user image content blocks to AI SDK image parts", () => {
  567. const messages = toAiSdkMessages([
  568. {
  569. role: "user",
  570. content: [
  571. { type: "text", text: "what is in this image?" },
  572. {
  573. type: "image",
  574. mediaType: "image/png",
  575. dataUrl: "data:image/png;base64,aW1hZ2U=",
  576. source: { id: "image-1", kind: "inline", placeholder: "[Image 1]" },
  577. },
  578. ],
  579. },
  580. ]);
  581. expect((messages[0] as any).content).toEqual([
  582. { type: "text", text: "what is in this image?" },
  583. { type: "image", image: "aW1hZ2U=", mediaType: "image/png" },
  584. ]);
  585. });
  586. it("can strip media blocks to stable text placeholders", () => {
  587. const messages = toAiSdkMessages(
  588. [
  589. {
  590. role: "user",
  591. content: [
  592. {
  593. type: "image",
  594. mediaType: "image/png",
  595. dataUrl: "data:image/png;base64,aW1hZ2U=",
  596. source: { id: "image-1", kind: "inline", placeholder: "[Image 1]" },
  597. },
  598. ],
  599. },
  600. ],
  601. { stripMedia: true },
  602. );
  603. expect((messages[0] as any).content).toEqual([
  604. { type: "text", text: "[Attached image/png: [Image 1]]" },
  605. ]);
  606. });
  607. it("turns unsupported image input into an explicit model-visible error", () => {
  608. const messages = toAiSdkMessages(
  609. [
  610. {
  611. role: "user",
  612. content: [
  613. {
  614. type: "image",
  615. mediaType: "image/png",
  616. dataUrl: "data:image/png;base64,aW1hZ2U=",
  617. },
  618. ],
  619. },
  620. ],
  621. { inputFormat: createTestInputFormat({ supportsImage: false }) },
  622. );
  623. expect((messages[0] as any).content).toEqual([
  624. {
  625. type: "text",
  626. text: "[Attached image/png]\n[Media omitted from provider request because the selected model does not support image input.]",
  627. },
  628. ]);
  629. });
  630. });