media-capability.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ModelInputFormat, ModelInputMessage, ToolArtifactStorePort } from "@zcode/contracts";
  3. import { projectMessagesWithMediaAttachmentPaths } from "../src/runtime/helpers/media-attachment-path.js";
  4. import { projectMessagesForInputFormat as projectMessages } from "../src/runtime/helpers/media-capability.js";
  5. function projectMessagesForInputFormat(
  6. messages: ModelInputMessage[],
  7. override?: Partial<ModelInputFormat>,
  8. ) {
  9. return projectMessages(messages, {
  10. supportsText: true,
  11. supportsImage: true,
  12. supportsVideo: true,
  13. supportsAudio: false,
  14. supportsPdf: true,
  15. ...override,
  16. });
  17. }
  18. describe("model request media capability projection", () => {
  19. it("appends ordered image source paths before explicit text-only stripping", async () => {
  20. const ensureMediaAttachmentPath = vi.fn(async ({ uri }: { uri: string }) => ({
  21. status: "ready" as const,
  22. path: uri.endsWith("image-1") ? "/tmp/image-1.png" : "/tmp/image-2.jpg",
  23. }));
  24. const artifactStore = {
  25. ensureMediaAttachmentPath,
  26. } as unknown as ToolArtifactStorePort;
  27. const messages: ModelInputMessage[] = [
  28. {
  29. role: "user",
  30. content: [
  31. { type: "text", text: "compare these" },
  32. {
  33. type: "image",
  34. mediaType: "image/png",
  35. dataUrl: "data:image/png;base64,b25l",
  36. source: { id: "image-1", kind: "inline", uri: "zcode-artifact://s/image-1" },
  37. },
  38. {
  39. type: "image",
  40. mediaType: "image/jpeg",
  41. dataUrl: "data:image/jpeg;base64,dHdv",
  42. source: { id: "image-2", kind: "inline", uri: "zcode-artifact://s/image-2" },
  43. },
  44. ],
  45. },
  46. ];
  47. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  48. expect(ensureMediaAttachmentPath).toHaveBeenCalledTimes(2);
  49. expect(withPaths[0]?.content).toEqual([
  50. { type: "text", text: "compare these" },
  51. expect.objectContaining({
  52. type: "image",
  53. source: expect.objectContaining({ path: "/tmp/image-1.png" }),
  54. }),
  55. expect.objectContaining({
  56. type: "image",
  57. source: expect.objectContaining({ path: "/tmp/image-2.jpg" }),
  58. }),
  59. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  60. { type: "text", text: "[Image: source: /tmp/image-2.jpg]" },
  61. ]);
  62. expect(projectMessagesForInputFormat(withPaths, { supportsVideo: undefined }).messages).toBe(
  63. withPaths,
  64. );
  65. const textOnly = projectMessagesForInputFormat(withPaths, {
  66. supportsImage: false,
  67. }).messages;
  68. expect(textOnly[0]?.content).toEqual([
  69. { type: "text", text: "compare these" },
  70. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  71. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  72. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  73. { type: "text", text: "[Image: source: /tmp/image-2.jpg]" },
  74. ]);
  75. expect(JSON.stringify(textOnly)).not.toContain('"type":"image"');
  76. expect(JSON.stringify(textOnly)).not.toContain("data:image");
  77. });
  78. it("appends a video source path for supported, unknown, and unsupported models", async () => {
  79. const ensureMediaAttachmentPath = vi.fn(async () => ({
  80. status: "ready" as const,
  81. path: "/tmp/video-1.mp4",
  82. }));
  83. const artifactStore = { ensureMediaAttachmentPath } as unknown as ToolArtifactStorePort;
  84. const messages: ModelInputMessage[] = [
  85. {
  86. role: "user",
  87. content: [
  88. { type: "text", text: "describe this video" },
  89. {
  90. type: "video",
  91. mediaType: "video/mp4",
  92. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  93. source: {
  94. id: "video-1",
  95. kind: "inline",
  96. uri: "zcode-artifact://s/video-1",
  97. },
  98. },
  99. ],
  100. },
  101. ];
  102. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  103. expect(ensureMediaAttachmentPath).toHaveBeenCalledWith({
  104. mediaType: "video/mp4",
  105. uri: "zcode-artifact://s/video-1",
  106. });
  107. expect(withPaths[0]?.content).toEqual([
  108. { type: "text", text: "describe this video" },
  109. expect.objectContaining({
  110. type: "video",
  111. source: expect.objectContaining({ path: "/tmp/video-1.mp4" }),
  112. }),
  113. { type: "text", text: "[Video: source: /tmp/video-1.mp4]" },
  114. ]);
  115. expect(projectMessagesForInputFormat(withPaths, { supportsVideo: true }).messages).toBe(
  116. withPaths,
  117. );
  118. expect(projectMessagesForInputFormat(withPaths, undefined).messages).toBe(withPaths);
  119. const textOnly = projectMessagesForInputFormat(withPaths, {
  120. supportsVideo: false,
  121. }).messages;
  122. expect(textOnly[0]?.content).toEqual([
  123. { type: "text", text: "describe this video" },
  124. expect.objectContaining({
  125. type: "text",
  126. text: expect.stringContaining("does not support video input"),
  127. }),
  128. { type: "text", text: "[Video: source: /tmp/video-1.mp4]" },
  129. ]);
  130. expect(JSON.stringify(textOnly)).not.toContain('"type":"video"');
  131. expect(JSON.stringify(textOnly)).not.toContain("data:video");
  132. });
  133. it("reuses a local video path without an artifact store", async () => {
  134. const messages: ModelInputMessage[] = [
  135. {
  136. role: "user",
  137. content: [
  138. {
  139. type: "video",
  140. mediaType: "video/mp4",
  141. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  142. source: {
  143. id: "video-1",
  144. kind: "local_file",
  145. path: "/workspace/video-1.mp4",
  146. },
  147. },
  148. ],
  149. },
  150. ];
  151. const projected = await projectMessagesWithMediaAttachmentPaths(messages, undefined);
  152. expect(projected[0]?.content).toEqual([
  153. expect.objectContaining({
  154. type: "video",
  155. source: expect.objectContaining({ path: "/workspace/video-1.mp4" }),
  156. }),
  157. { type: "text", text: "[Video: source: /workspace/video-1.mp4]" },
  158. ]);
  159. });
  160. it("fails before the provider boundary when a durable inline video cannot be materialized", async () => {
  161. const messages: ModelInputMessage[] = [
  162. {
  163. role: "user",
  164. content: [
  165. {
  166. type: "video",
  167. mediaType: "video/mp4",
  168. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  169. source: {
  170. id: "video-1",
  171. kind: "inline",
  172. uri: "zcode-artifact://s/video-1",
  173. },
  174. },
  175. ],
  176. },
  177. ];
  178. await expect(projectMessagesWithMediaAttachmentPaths(messages, undefined)).rejects.toThrow(
  179. "Unable to materialize video attachment path",
  180. );
  181. });
  182. it("keeps generic inline video on the existing capability path", async () => {
  183. const ensureMediaAttachmentPath = vi.fn(async () => ({
  184. status: "ready" as const,
  185. path: "/tmp/should-not-be-used.mp4",
  186. }));
  187. const artifactStore = { ensureMediaAttachmentPath } as unknown as ToolArtifactStorePort;
  188. const messages: ModelInputMessage[] = [
  189. {
  190. role: "user",
  191. content: [
  192. {
  193. type: "video",
  194. mediaType: "video/mp4",
  195. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  196. source: { id: "generic-video", kind: "inline" },
  197. },
  198. ],
  199. },
  200. ];
  201. await expect(projectMessagesWithMediaAttachmentPaths(messages, artifactStore)).resolves.toBe(
  202. messages,
  203. );
  204. expect(ensureMediaAttachmentPath).not.toHaveBeenCalled();
  205. });
  206. it("fails before the provider boundary when a durable inline image cannot be materialized", async () => {
  207. const messages: ModelInputMessage[] = [
  208. {
  209. role: "user",
  210. content: [
  211. {
  212. type: "image",
  213. mediaType: "image/png",
  214. dataUrl: "data:image/png;base64,b25l",
  215. source: {
  216. id: "image-1",
  217. kind: "inline",
  218. uri: "zcode-artifact://s/image-1",
  219. },
  220. },
  221. ],
  222. },
  223. ];
  224. await expect(projectMessagesWithMediaAttachmentPaths(messages, undefined)).rejects.toThrow(
  225. "Unable to materialize image attachment path",
  226. );
  227. });
  228. it.each([
  229. ["without a URI", { id: "generic-image", kind: "inline" as const }],
  230. [
  231. "with a data URL URI",
  232. {
  233. id: "legacy-inline-image",
  234. kind: "inline" as const,
  235. uri: "data:image/png;base64,aW1hZ2U=",
  236. },
  237. ],
  238. ])(
  239. "keeps generic inline images %s on the existing media-capability path",
  240. async (_label, source) => {
  241. const ensureMediaAttachmentPath = vi.fn(async () => ({
  242. status: "ready" as const,
  243. path: "/tmp/should-not-be-used.png",
  244. }));
  245. const artifactStore = {
  246. ensureMediaAttachmentPath,
  247. } as unknown as ToolArtifactStorePort;
  248. const messages: ModelInputMessage[] = [
  249. {
  250. role: "user",
  251. content: [
  252. { type: "text", text: "describe this" },
  253. {
  254. type: "image",
  255. mediaType: "image/png",
  256. dataUrl: "data:image/png;base64,aW1hZ2U=",
  257. source,
  258. },
  259. ],
  260. },
  261. ];
  262. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  263. expect(withPaths).toBe(messages);
  264. expect(ensureMediaAttachmentPath).not.toHaveBeenCalled();
  265. expect(projectMessagesForInputFormat(withPaths, { supportsImage: true }).messages).toBe(
  266. messages,
  267. );
  268. expect(projectMessagesForInputFormat(withPaths, undefined).messages).toBe(messages);
  269. const textOnly = projectMessagesForInputFormat(withPaths, {
  270. supportsImage: false,
  271. }).messages;
  272. expect(textOnly[0]?.content).toEqual([
  273. { type: "text", text: "describe this" },
  274. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  275. ]);
  276. expect(JSON.stringify(textOnly)).not.toContain('"type":"image"');
  277. expect(JSON.stringify(textOnly)).not.toContain("data:image");
  278. },
  279. );
  280. it("revalidates a durable inline image instead of trusting a projected source path", async () => {
  281. const ensureMediaAttachmentPath = vi.fn(async () => ({
  282. status: "ready" as const,
  283. path: "/tmp/rebuilt-image.png",
  284. }));
  285. const artifactStore = {
  286. ensureMediaAttachmentPath,
  287. } as unknown as ToolArtifactStorePort;
  288. const messages: ModelInputMessage[] = [
  289. {
  290. role: "user",
  291. content: [
  292. {
  293. type: "image",
  294. mediaType: "image/png",
  295. dataUrl: "data:image/png;base64,b25l",
  296. source: {
  297. id: "image-1",
  298. kind: "inline",
  299. path: "/tmp/stale-image.png",
  300. uri: "zcode-artifact://s/image-1",
  301. },
  302. },
  303. ],
  304. },
  305. ];
  306. const projected = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  307. expect(ensureMediaAttachmentPath).toHaveBeenCalledWith({
  308. mediaType: "image/png",
  309. uri: "zcode-artifact://s/image-1",
  310. });
  311. expect(projected[0]?.content).toEqual([
  312. expect.objectContaining({
  313. type: "image",
  314. source: expect.objectContaining({ path: "/tmp/rebuilt-image.png" }),
  315. }),
  316. { type: "text", text: "[Image: source: /tmp/rebuilt-image.png]" },
  317. ]);
  318. });
  319. it("skips only unsupported derived paths while preserving image and text-only projection", async () => {
  320. const ensureMediaAttachmentPath = vi.fn(async ({ mediaType }: { mediaType: string }) =>
  321. mediaType === "image/bmp"
  322. ? ({ status: "unsupported" } as const)
  323. : ({ status: "ready", path: "/tmp/image-1.png" } as const),
  324. );
  325. const artifactStore = {
  326. ensureMediaAttachmentPath,
  327. } as unknown as ToolArtifactStorePort;
  328. const png = {
  329. type: "image" as const,
  330. mediaType: "image/png",
  331. dataUrl: "data:image/png;base64,b25l",
  332. source: { id: "image-1", kind: "inline" as const, uri: "zcode-artifact://s/image-1" },
  333. };
  334. const bmp = {
  335. type: "image" as const,
  336. mediaType: "image/bmp",
  337. dataUrl: "data:image/bmp;base64,dHdv",
  338. source: { id: "image-2", kind: "inline" as const, uri: "zcode-artifact://s/image-2" },
  339. };
  340. const messages: ModelInputMessage[] = [
  341. {
  342. role: "user",
  343. content: [{ type: "text", text: "compare these" }, png, bmp],
  344. },
  345. ];
  346. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  347. expect(ensureMediaAttachmentPath).toHaveBeenCalledTimes(2);
  348. expect(withPaths[0]?.content).toEqual([
  349. { type: "text", text: "compare these" },
  350. expect.objectContaining({
  351. type: "image",
  352. source: expect.objectContaining({ path: "/tmp/image-1.png" }),
  353. }),
  354. bmp,
  355. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  356. ]);
  357. const projectedContent = withPaths[0]?.content;
  358. expect(Array.isArray(projectedContent) ? projectedContent[2] : undefined).toStrictEqual(bmp);
  359. const textOnly = projectMessagesForInputFormat(withPaths, {
  360. supportsImage: false,
  361. }).messages;
  362. expect(textOnly[0]?.content).toEqual([
  363. { type: "text", text: "compare these" },
  364. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  365. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  366. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  367. ]);
  368. expect(JSON.stringify(textOnly)).not.toContain('"type":"image"');
  369. expect(JSON.stringify(textOnly)).not.toContain("data:image");
  370. });
  371. it("replaces user image blocks when the model explicitly does not support images", () => {
  372. const messages: ModelInputMessage[] = [
  373. {
  374. role: "user",
  375. content: [
  376. { type: "text", text: "describe this" },
  377. {
  378. type: "image",
  379. mediaType: "image/png",
  380. dataUrl: "data:image/png;base64,aW1hZ2U=",
  381. source: { id: "image-1", kind: "inline", placeholder: "[image #1]" },
  382. },
  383. ],
  384. },
  385. ];
  386. const result = projectMessagesForInputFormat(messages, { supportsImage: false });
  387. expect(result.omittedMediaCount).toBe(1);
  388. expect(result.omittedImageCount).toBe(1);
  389. expect(result.retainedMediaCount).toBe(0);
  390. expect(result.messages).not.toBe(messages);
  391. expect(result.messages[0]?.content).toEqual([
  392. { type: "text", text: "describe this" },
  393. {
  394. type: "text",
  395. text: "[Attached image/png: [image #1]]\n[Media omitted from provider request because the selected model does not support image input.]",
  396. },
  397. ]);
  398. });
  399. it("keeps image blocks when support is true or unknown", () => {
  400. const image = {
  401. type: "image" as const,
  402. mediaType: "image/png",
  403. dataUrl: "data:image/png;base64,aW1hZ2U=",
  404. };
  405. const messages: ModelInputMessage[] = [{ role: "user", content: [image] }];
  406. expect(projectMessagesForInputFormat(messages, { supportsImage: true }).messages).toBe(
  407. messages,
  408. );
  409. expect(projectMessagesForInputFormat(messages, {}).messages).toBe(messages);
  410. expect(projectMessagesForInputFormat(messages, undefined).messages).toBe(messages);
  411. });
  412. it("replaces PDF file data when PDF support is explicitly false", () => {
  413. const messages: ModelInputMessage[] = [
  414. {
  415. role: "user",
  416. content: [
  417. {
  418. type: "file",
  419. mediaType: "application/pdf",
  420. name: "report.pdf",
  421. dataUrl: "data:application/pdf;base64,cGRm",
  422. },
  423. ],
  424. },
  425. ];
  426. const result = projectMessagesForInputFormat(messages, { supportsPdf: false });
  427. expect(result.omittedPdfCount).toBe(1);
  428. expect(result.messages[0]?.content).toEqual([
  429. {
  430. type: "text",
  431. text: "[Attached application/pdf: report.pdf]\n[Media omitted from provider request because the selected model does not support PDF input.]",
  432. },
  433. ]);
  434. });
  435. it("replaces video file data when video support is explicitly false", () => {
  436. const messages: ModelInputMessage[] = [
  437. {
  438. role: "user",
  439. content: [
  440. {
  441. type: "file",
  442. mediaType: "video/mp4",
  443. name: "demo.mp4",
  444. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  445. },
  446. ],
  447. },
  448. ];
  449. const result = projectMessagesForInputFormat(messages, { supportsVideo: false });
  450. expect(result.omittedMediaCount).toBe(1);
  451. expect(result.retainedMediaCount).toBe(0);
  452. expect(result.messages[0]?.content).toEqual([
  453. {
  454. type: "text",
  455. text: "[Attached video/mp4: demo.mp4]\n[Media omitted from provider request because the selected model does not support video input.]",
  456. },
  457. ]);
  458. });
  459. it("replaces PDF file data when extracted PDF text is empty", () => {
  460. const messages: ModelInputMessage[] = [
  461. {
  462. role: "user",
  463. content: [
  464. {
  465. type: "file",
  466. mediaType: "application/pdf",
  467. name: "empty-text.pdf",
  468. dataUrl: "data:application/pdf;base64,cGRm",
  469. text: "",
  470. },
  471. ],
  472. },
  473. ];
  474. const result = projectMessagesForInputFormat(messages, { supportsPdf: false });
  475. expect(result.omittedPdfCount).toBe(1);
  476. expect(result.retainedMediaCount).toBe(0);
  477. expect(result.messages[0]?.content).toEqual([
  478. {
  479. type: "text",
  480. text: "[Attached application/pdf: empty-text.pdf]\n[Media omitted from provider request because the selected model does not support PDF input.]",
  481. },
  482. ]);
  483. });
  484. it("counts stripped video blocks in capability observability", () => {
  485. const messages: ModelInputMessage[] = [
  486. {
  487. role: "user",
  488. content: [
  489. {
  490. type: "video",
  491. mediaType: "video/mp4",
  492. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  493. },
  494. ],
  495. },
  496. ];
  497. const result = projectMessagesForInputFormat(messages, { supportsVideo: false });
  498. expect(result.omittedMediaCount).toBe(1);
  499. expect(result.omittedVideoCount).toBe(1);
  500. expect(result.retainedMediaCount).toBe(0);
  501. expect(JSON.stringify(result.messages)).not.toContain('"type":"video"');
  502. });
  503. });