model-transform.test.ts 20 KB

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