model-transform-tool-media.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 tool result media projection", () => {
  5. it("accepts case-insensitive data URL schemes for image tool results", () => {
  6. const messages = toAiSdkMessages(
  7. [
  8. {
  9. role: "tool",
  10. content: [
  11. {
  12. type: "image",
  13. mediaType: "image/png",
  14. dataUrl: "DATA:image/png;base64,aW1hZ2U=",
  15. },
  16. ],
  17. toolCallId: "call_uppercase_data",
  18. toolName: "image_tool",
  19. },
  20. ],
  21. { providerKind: "anthropic", supportsImages: true },
  22. );
  23. expect((messages[0] as any).content[0].output.value).toEqual([
  24. { type: "image-data", data: "aW1hZ2U=", mediaType: "image/png" },
  25. ]);
  26. });
  27. it("preserves a real Anthropic CUA observation result in order", () => {
  28. const frameRef = JSON.stringify({
  29. image_ref: { frame_id: "frame-complete", width: 1, height: 1, actionable: true },
  30. });
  31. const state = "state_id=state-complete mode=full\nSTATE_MARKER";
  32. const appInstructions = "<app_specific_instructions>APP_MARKER</app_specific_instructions>";
  33. const messages = toAiSdkMessages(
  34. [
  35. {
  36. role: "tool",
  37. content: [
  38. {
  39. type: "image",
  40. mediaType: "image/png",
  41. dataUrl: "data:image/png;base64,aW1hZ2U=",
  42. },
  43. { type: "text", text: frameRef },
  44. { type: "text", text: state },
  45. { type: "text", text: appInstructions },
  46. ],
  47. toolCallId: "call_complete",
  48. toolName: "mcp__computer-use__get_app_state",
  49. },
  50. ],
  51. { providerKind: "anthropic", supportsImages: true },
  52. );
  53. expect(messages).toHaveLength(1);
  54. expect((messages[0] as any).content[0].output).toEqual({
  55. type: "content",
  56. value: [
  57. { type: "image-data", data: "aW1hZ2U=", mediaType: "image/png" },
  58. { type: "text", text: frameRef },
  59. { type: "text", text: state },
  60. { type: "text", text: appInstructions },
  61. ],
  62. });
  63. });
  64. it("preserves a real Anthropic CUA action result in order", () => {
  65. const note = "Action dispatched. (settled state below)";
  66. const state = "state_id=state-action mode=full\nACTION_STATE";
  67. const outcome = JSON.stringify({
  68. schema_version: "zcode-cua-action-outcome-v1",
  69. state_sync_status: "synced",
  70. post_state_id: "state-action",
  71. state_available: true,
  72. });
  73. const messages = toAiSdkMessages(
  74. [
  75. {
  76. role: "tool",
  77. content: [
  78. { type: "text", text: note },
  79. { type: "text", text: state },
  80. { type: "text", text: outcome },
  81. ],
  82. toolCallId: "call_action",
  83. toolName: "mcp__computer-use__left_click",
  84. },
  85. ],
  86. { providerKind: "anthropic" },
  87. );
  88. expect((messages[0] as any).content[0].output.value).toEqual([
  89. { type: "text", text: note },
  90. { type: "text", text: state },
  91. { type: "text", text: outcome },
  92. ]);
  93. });
  94. it("preserves text around media across consecutive Anthropic tool results", () => {
  95. const messages = toAiSdkMessages(
  96. [
  97. {
  98. role: "tool",
  99. content: [
  100. { type: "text", text: "BEFORE_IMAGE" },
  101. {
  102. type: "image",
  103. mediaType: "image/png",
  104. dataUrl: "data:image/png;base64,aW1hZ2U=",
  105. },
  106. { type: "text", text: "AFTER_IMAGE" },
  107. ],
  108. toolCallId: "call_mixed",
  109. toolName: "mixed_result",
  110. },
  111. {
  112. role: "tool",
  113. content: "SECOND_RESULT",
  114. toolCallId: "call_second",
  115. toolName: "second_result",
  116. },
  117. ],
  118. { providerKind: "anthropic", supportsImages: true },
  119. );
  120. expect(messages).toHaveLength(2);
  121. expect((messages[0] as any).content[0].output.value).toEqual([
  122. { type: "text", text: "BEFORE_IMAGE" },
  123. { type: "image-data", data: "aW1hZ2U=", mediaType: "image/png" },
  124. { type: "text", text: "AFTER_IMAGE" },
  125. ]);
  126. expect((messages[1] as any).content[0].output).toEqual({
  127. type: "text",
  128. value: "SECOND_RESULT",
  129. });
  130. });
  131. it("keeps every text marker in an Anthropic error result", () => {
  132. const messages = toAiSdkMessages(
  133. [
  134. {
  135. role: "tool",
  136. content: [
  137. { type: "text", text: "ERROR_MARKER_A" },
  138. { type: "text", text: "ERROR_MARKER_B" },
  139. ],
  140. isError: true,
  141. toolCallId: "call_error",
  142. toolName: "mcp__computer-use__get_app_state",
  143. },
  144. ],
  145. { providerKind: "anthropic" },
  146. );
  147. const output = (messages[0] as any).content[0].output;
  148. expect(output.type).toBe("error-text");
  149. expect(output.value).toContain("ERROR_MARKER_A");
  150. expect(output.value).toContain("ERROR_MARKER_B");
  151. });
  152. it("projects OpenAI-compatible tool images through a follow-up user media message", () => {
  153. const toolCallId = "call_read_image";
  154. const messages = toAiSdkMessages(
  155. [
  156. {
  157. role: "tool",
  158. content: [
  159. {
  160. type: "image",
  161. mediaType: "image/jpeg",
  162. dataUrl: "data:image/jpeg;base64,cmVhZC1pbWFnZQ==",
  163. source: { id: "read-image", kind: "inline", placeholder: "Read image" },
  164. },
  165. ],
  166. toolCallId,
  167. toolName: "Read",
  168. },
  169. ],
  170. { providerKind: "openai-compatible" },
  171. );
  172. expect(messages).toEqual([
  173. {
  174. role: "tool",
  175. content: [
  176. {
  177. type: "tool-result",
  178. toolCallId,
  179. toolName: "Read",
  180. output: {
  181. type: "text",
  182. value: "[Attached image/jpeg: Read image]",
  183. },
  184. },
  185. ],
  186. },
  187. {
  188. role: "user",
  189. content: [
  190. { type: "text", text: "Tool result media from Read:" },
  191. { type: "image", image: "cmVhZC1pbWFnZQ==", mediaType: "image/jpeg" },
  192. ],
  193. },
  194. ]);
  195. });
  196. it("keeps PDF page results structured for Anthropic and Responses, and textifies only Chat", () => {
  197. const content = [
  198. { type: "text" as const, text: "PDF pages extracted: 2 page(s) from /tmp/report.pdf (2KB)" },
  199. {
  200. type: "image" as const,
  201. mediaType: "image/jpeg",
  202. dataUrl: "data:image/jpeg;base64,cGFnZS0x",
  203. },
  204. {
  205. type: "image" as const,
  206. mediaType: "image/jpeg",
  207. dataUrl: "data:image/jpeg;base64,cGFnZS0y",
  208. },
  209. ];
  210. const input = [{ role: "tool" as const, content, toolCallId: "call_pdf", toolName: "Read" }];
  211. const structuredOutput = {
  212. type: "content",
  213. value: [
  214. { type: "text", text: "PDF pages extracted: 2 page(s) from /tmp/report.pdf (2KB)" },
  215. { type: "image-data", data: "cGFnZS0x", mediaType: "image/jpeg" },
  216. { type: "image-data", data: "cGFnZS0y", mediaType: "image/jpeg" },
  217. ],
  218. };
  219. const anthropic = toAiSdkMessages(input, {
  220. apiFormat: "anthropic-messages",
  221. providerKind: "anthropic",
  222. supportsImages: true,
  223. });
  224. expect(anthropic).toHaveLength(1);
  225. expect((anthropic[0] as any).content[0].output).toEqual(structuredOutput);
  226. const responses = toAiSdkMessages(input, {
  227. apiFormat: "openai-responses",
  228. providerKind: "openai",
  229. supportsImages: true,
  230. });
  231. expect(responses).toHaveLength(1);
  232. expect((responses[0] as any).content[0].output).toEqual(structuredOutput);
  233. const chat = toAiSdkMessages(input, {
  234. apiFormat: "openai-chat-completions",
  235. providerKind: "openai",
  236. supportsImages: true,
  237. });
  238. expect(chat).toEqual([
  239. {
  240. role: "tool",
  241. content: [
  242. {
  243. type: "tool-result",
  244. toolCallId: "call_pdf",
  245. toolName: "Read",
  246. output: {
  247. type: "text",
  248. value:
  249. "PDF pages extracted: 2 page(s) from /tmp/report.pdf (2KB)\n\n[Attached image/jpeg]\n\n[Attached image/jpeg]",
  250. },
  251. },
  252. ],
  253. },
  254. {
  255. role: "user",
  256. content: [
  257. { type: "text", text: "Tool result media from Read:" },
  258. { type: "image", image: "cGFnZS0x", mediaType: "image/jpeg" },
  259. { type: "image", image: "cGFnZS0y", mediaType: "image/jpeg" },
  260. ],
  261. },
  262. ]);
  263. });
  264. it("keeps OpenAI Responses tool media structured when apiFormat is omitted", () => {
  265. const messages = toAiSdkMessages(
  266. [
  267. {
  268. role: "tool",
  269. content: [
  270. { type: "text", text: "PDF pages extracted: 1 page(s) from /tmp/report.pdf (1KB)" },
  271. {
  272. type: "image",
  273. mediaType: "image/jpeg",
  274. dataUrl: "data:image/jpeg;base64,cGFnZS0x",
  275. },
  276. ],
  277. toolCallId: "call_pdf_responses",
  278. toolName: "Read",
  279. },
  280. ],
  281. { providerKind: "openai", supportsImages: true },
  282. );
  283. expect(messages).toHaveLength(1);
  284. expect((messages[0] as any).content[0].output).toEqual({
  285. type: "content",
  286. value: [
  287. { type: "text", text: "PDF pages extracted: 1 page(s) from /tmp/report.pdf (1KB)" },
  288. { type: "image-data", data: "cGFnZS0x", mediaType: "image/jpeg" },
  289. ],
  290. });
  291. });
  292. it("keeps follow-up media after a contiguous OpenAI-compatible tool result block", () => {
  293. const messages = toAiSdkMessages(
  294. [
  295. {
  296. role: "tool",
  297. content: [
  298. {
  299. type: "image",
  300. mediaType: "image/png",
  301. dataUrl: "data:image/png;base64,aW1hZ2U=",
  302. },
  303. ],
  304. toolCallId: "call_image",
  305. toolName: "Read",
  306. },
  307. {
  308. role: "tool",
  309. content: "ok",
  310. toolCallId: "call_text",
  311. toolName: "Read",
  312. },
  313. ],
  314. { providerKind: "openai-compatible" },
  315. );
  316. expect(messages.map((message) => message.role)).toEqual(["tool", "tool", "user"]);
  317. expect((messages[0] as any).content[0].output).toEqual({
  318. type: "text",
  319. value: "[Attached image/png]",
  320. });
  321. expect((messages[1] as any).content[0].output).toEqual({
  322. type: "text",
  323. value: "ok",
  324. });
  325. expect((messages[2] as any).content).toEqual([
  326. { type: "text", text: "Tool result media from Read:" },
  327. { type: "image", image: "aW1hZ2U=", mediaType: "image/png" },
  328. ]);
  329. });
  330. it("does not project OpenAI-compatible tool images when images are explicitly unsupported", () => {
  331. const messages = toAiSdkMessages(
  332. [
  333. {
  334. role: "tool",
  335. content: [
  336. {
  337. type: "image",
  338. mediaType: "image/png",
  339. dataUrl: "data:image/png;base64,aW1hZ2U=",
  340. },
  341. ],
  342. toolCallId: "call_image",
  343. toolName: "Read",
  344. },
  345. ],
  346. {
  347. providerKind: "openai-compatible",
  348. inputFormat: createTestInputFormat({ supportsImage: false }),
  349. },
  350. );
  351. expect(messages).toHaveLength(1);
  352. expect((messages[0] as any).content[0].output).toEqual({
  353. type: "text",
  354. value:
  355. "[Attached image/png]\n[Media omitted from provider request because the selected model does not support image input.]",
  356. });
  357. });
  358. it("textifies OpenAI-compatible structured tool text instead of JSON content", () => {
  359. const messages = toAiSdkMessages(
  360. [
  361. {
  362. role: "tool",
  363. content: [{ type: "text", text: "plain result" }],
  364. toolCallId: "call_text_block",
  365. toolName: "Example",
  366. },
  367. ],
  368. { providerKind: "openai-compatible" },
  369. );
  370. expect((messages[0] as any).content[0].output).toEqual({
  371. type: "text",
  372. value: "plain result",
  373. });
  374. expect(messages).toHaveLength(1);
  375. });
  376. it("does not pair a text block that is separated from the media by another block", () => {
  377. const messages = toAiSdkMessages(
  378. [
  379. {
  380. role: "tool",
  381. content: [
  382. { type: "text", text: "summary before state" },
  383. { type: "reasoning" as never, text: "" },
  384. { type: "text", text: "captured state" },
  385. {
  386. type: "image",
  387. mediaType: "image/png",
  388. dataUrl: "data:image/png;base64,c3RhdGU=",
  389. },
  390. ],
  391. toolCallId: "call_sep",
  392. toolName: "inspect",
  393. },
  394. ],
  395. { providerKind: "openai-compatible" },
  396. );
  397. // 谓词化后:普通文本不是媒体凭证,无论是否紧邻都不配对、不延后
  398. // (配对只对 producer 签发的帧凭证生效)。
  399. const toolOutput = (messages[0] as any).content[0].output.value as string;
  400. expect(toolOutput).toContain("summary before state");
  401. expect(toolOutput).toContain("captured state");
  402. const mediaTexts = ((messages[1] as any).content as Array<Record<string, unknown>>)
  403. .filter((part) => part.type === "text")
  404. .map((part) => part.text);
  405. expect(mediaTexts.some((text) => String(text).includes("captured state"))).toBe(false);
  406. });
  407. it("keeps a non-paired description text in the tool output when it is followed by another text before the image (SG-01)", () => {
  408. const messages = toAiSdkMessages(
  409. [
  410. {
  411. role: "tool",
  412. content: [
  413. { type: "text", text: "处理失败:超时" },
  414. { type: "text", text: "附下方截图供诊断" },
  415. {
  416. type: "image",
  417. mediaType: "image/png",
  418. dataUrl: "data:image/png;base64,ZGlhZw==",
  419. },
  420. ],
  421. toolCallId: "call_plain_tool",
  422. toolName: "plain_check",
  423. },
  424. ],
  425. { providerKind: "openai-compatible" },
  426. );
  427. // 谓词化后:普通 caption 不是媒体凭证,留在 tool result 不迁移;
  428. // 通用工具的媒体语义保持 pre-CUA 行为(只有帧凭证才配对)。
  429. const toolOutput = (messages[0] as any).content[0].output.value as string;
  430. expect(toolOutput).toContain("处理失败:超时");
  431. expect(toolOutput).toContain("附下方截图供诊断");
  432. const mediaTexts = ((messages[1] as any).content as Array<Record<string, unknown>>)
  433. .filter((part) => part.type === "text")
  434. .map((part) => String(part.text));
  435. expect(mediaTexts.some((text) => text.includes("附下方截图供诊断"))).toBe(false);
  436. });
  437. it("defers a plain screenshot caption with its image for any structured tool (generic pairing)", () => {
  438. const messages = toAiSdkMessages(
  439. [
  440. {
  441. role: "tool",
  442. content: [
  443. { type: "text", text: "Chart of weekly commits:" },
  444. {
  445. type: "image",
  446. mediaType: "image/svg+xml",
  447. dataUrl: "data:image/svg+xml;base64,Y2hhcnQ=",
  448. },
  449. ],
  450. toolCallId: "call_chart",
  451. toolName: "render_chart",
  452. },
  453. ],
  454. { providerKind: "openai-compatible" },
  455. );
  456. // 谓词化后:普通 chart 说明不是媒体凭证,留在 tool result;图片字节
  457. // 原样延后(配对只对 producer 签发的帧凭证生效,通用语义回归 pre-CUA)。
  458. const toolOutput = (messages[0] as any).content[0].output.value as string;
  459. expect(toolOutput).toContain("Chart of weekly commits:");
  460. const mediaContent = (messages[1] as any).content as Array<Record<string, unknown>>;
  461. expect(
  462. mediaContent.some(
  463. (part) => part.type === "text" && String(part.text).includes("Chart of weekly commits:"),
  464. ),
  465. ).toBe(false);
  466. expect(mediaContent.some((part) => part.type === "image" && part.image === "Y2hhcnQ=")).toBe(
  467. true,
  468. );
  469. });
  470. it("keeps standalone text untouched when the tool result has no media at all", () => {
  471. const messages = toAiSdkMessages(
  472. [
  473. {
  474. role: "tool",
  475. content: [
  476. { type: "text", text: "3 files changed" },
  477. { type: "text", text: "no media here" },
  478. ],
  479. toolCallId: "call_summary",
  480. toolName: "diff_summary",
  481. },
  482. ],
  483. { providerKind: "gateway" },
  484. );
  485. // 无 media:没有任何配对,全部文本原样留在 tool result。
  486. expect(messages).toHaveLength(1);
  487. const toolOutput = (messages[0] as any).content[0].output.value as string;
  488. expect(toolOutput).toContain("3 files changed");
  489. expect(toolOutput).toContain("no media here");
  490. });
  491. it("keeps the caption in the tool text when the model cannot receive that media kind (no orphan deferred text)", () => {
  492. const messages = toAiSdkMessages(
  493. [
  494. {
  495. role: "tool",
  496. content: [
  497. { type: "text", text: "CAPTION_BEFORE_IMAGE" },
  498. { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,aGVsbG8=" },
  499. ],
  500. toolCallId: "call_novision",
  501. toolName: "screenshot",
  502. },
  503. ],
  504. {
  505. providerKind: "openai-compatible",
  506. inputFormat: createTestInputFormat({ supportsImage: false }),
  507. },
  508. );
  509. // 媒体不可投递时不产生延后消息;配对文本留在 tool result(带省略占位说明)。
  510. expect(messages).toHaveLength(1);
  511. const toolOutput = (messages[0] as any).content[0].output.value as string;
  512. expect(toolOutput).toContain("CAPTION_BEFORE_IMAGE");
  513. expect(toolOutput).toContain("does not support image input");
  514. });
  515. it("does not drop paired text when stripMedia suppresses deferral entirely (SG-02)", () => {
  516. const messages = toAiSdkMessages(
  517. [
  518. {
  519. role: "tool",
  520. content: [
  521. { type: "text", text: "CAPTION" },
  522. { type: "image", mediaType: "image/png", dataUrl: "data:image/png;base64,aGVsbG8=" },
  523. ],
  524. toolCallId: "c3",
  525. toolName: "plain_tool",
  526. },
  527. ],
  528. { providerKind: "openai-compatible", stripMedia: true },
  529. );
  530. // stripMedia:无延后消息;配对文本不从 tool 输出消失(两头守卫一致)。
  531. expect(messages).toHaveLength(1);
  532. const output = (messages[0] as any).content[0].output;
  533. expect(output.type === "text" ? output.value : JSON.stringify(output)).toContain("CAPTION");
  534. });
  535. });