media-capability.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 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. const textOnly = projectMessagesForInputFormat(withPaths, {
  119. supportsVideo: false,
  120. }).messages;
  121. expect(textOnly[0]?.content).toEqual([
  122. { type: "text", text: "describe this video" },
  123. expect.objectContaining({
  124. type: "text",
  125. text: expect.stringContaining("does not support video input"),
  126. }),
  127. { type: "text", text: "[Video: source: /tmp/video-1.mp4]" },
  128. ]);
  129. expect(JSON.stringify(textOnly)).not.toContain('"type":"video"');
  130. expect(JSON.stringify(textOnly)).not.toContain("data:video");
  131. });
  132. it("reuses a local video path without an artifact store", async () => {
  133. const messages: ModelInputMessage[] = [
  134. {
  135. role: "user",
  136. content: [
  137. {
  138. type: "video",
  139. mediaType: "video/mp4",
  140. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  141. source: {
  142. id: "video-1",
  143. kind: "local_file",
  144. path: "/workspace/video-1.mp4",
  145. },
  146. },
  147. ],
  148. },
  149. ];
  150. const projected = await projectMessagesWithMediaAttachmentPaths(messages, undefined);
  151. expect(projected[0]?.content).toEqual([
  152. expect.objectContaining({
  153. type: "video",
  154. source: expect.objectContaining({ path: "/workspace/video-1.mp4" }),
  155. }),
  156. { type: "text", text: "[Video: source: /workspace/video-1.mp4]" },
  157. ]);
  158. });
  159. it("fails before the provider boundary when a durable inline video cannot be materialized", async () => {
  160. const messages: ModelInputMessage[] = [
  161. {
  162. role: "user",
  163. content: [
  164. {
  165. type: "video",
  166. mediaType: "video/mp4",
  167. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  168. source: {
  169. id: "video-1",
  170. kind: "inline",
  171. uri: "zcode-artifact://s/video-1",
  172. },
  173. },
  174. ],
  175. },
  176. ];
  177. await expect(projectMessagesWithMediaAttachmentPaths(messages, undefined)).rejects.toThrow(
  178. "Unable to materialize video attachment path",
  179. );
  180. });
  181. it("keeps generic inline video on the existing capability path", async () => {
  182. const ensureMediaAttachmentPath = vi.fn(async () => ({
  183. status: "ready" as const,
  184. path: "/tmp/should-not-be-used.mp4",
  185. }));
  186. const artifactStore = { ensureMediaAttachmentPath } as unknown as ToolArtifactStorePort;
  187. const messages: ModelInputMessage[] = [
  188. {
  189. role: "user",
  190. content: [
  191. {
  192. type: "video",
  193. mediaType: "video/mp4",
  194. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  195. source: { id: "generic-video", kind: "inline" },
  196. },
  197. ],
  198. },
  199. ];
  200. await expect(projectMessagesWithMediaAttachmentPaths(messages, artifactStore)).resolves.toBe(
  201. messages,
  202. );
  203. expect(ensureMediaAttachmentPath).not.toHaveBeenCalled();
  204. });
  205. it("fails before the provider boundary when a durable inline image cannot be materialized", async () => {
  206. const messages: ModelInputMessage[] = [
  207. {
  208. role: "user",
  209. content: [
  210. {
  211. type: "image",
  212. mediaType: "image/png",
  213. dataUrl: "data:image/png;base64,b25l",
  214. source: {
  215. id: "image-1",
  216. kind: "inline",
  217. uri: "zcode-artifact://s/image-1",
  218. },
  219. },
  220. ],
  221. },
  222. ];
  223. await expect(projectMessagesWithMediaAttachmentPaths(messages, undefined)).rejects.toThrow(
  224. "Unable to materialize image attachment path",
  225. );
  226. });
  227. it.each([
  228. ["without a URI", { id: "generic-image", kind: "inline" as const }],
  229. [
  230. "with a data URL URI",
  231. {
  232. id: "legacy-inline-image",
  233. kind: "inline" as const,
  234. uri: "data:image/png;base64,aW1hZ2U=",
  235. },
  236. ],
  237. ])(
  238. "keeps generic inline images %s on the existing media-capability path",
  239. async (_label, source) => {
  240. const ensureMediaAttachmentPath = vi.fn(async () => ({
  241. status: "ready" as const,
  242. path: "/tmp/should-not-be-used.png",
  243. }));
  244. const artifactStore = {
  245. ensureMediaAttachmentPath,
  246. } as unknown as ToolArtifactStorePort;
  247. const messages: ModelInputMessage[] = [
  248. {
  249. role: "user",
  250. content: [
  251. { type: "text", text: "describe this" },
  252. {
  253. type: "image",
  254. mediaType: "image/png",
  255. dataUrl: "data:image/png;base64,aW1hZ2U=",
  256. source,
  257. },
  258. ],
  259. },
  260. ];
  261. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  262. expect(withPaths).toBe(messages);
  263. expect(ensureMediaAttachmentPath).not.toHaveBeenCalled();
  264. expect(projectMessagesForInputFormat(withPaths, { supportsImage: true }).messages).toBe(
  265. messages,
  266. );
  267. const textOnly = projectMessagesForInputFormat(withPaths, {
  268. supportsImage: false,
  269. }).messages;
  270. expect(textOnly[0]?.content).toEqual([
  271. { type: "text", text: "describe this" },
  272. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  273. ]);
  274. expect(JSON.stringify(textOnly)).not.toContain('"type":"image"');
  275. expect(JSON.stringify(textOnly)).not.toContain("data:image");
  276. },
  277. );
  278. it("revalidates a durable inline image instead of trusting a projected source path", async () => {
  279. const ensureMediaAttachmentPath = vi.fn(async () => ({
  280. status: "ready" as const,
  281. path: "/tmp/rebuilt-image.png",
  282. }));
  283. const artifactStore = {
  284. ensureMediaAttachmentPath,
  285. } as unknown as ToolArtifactStorePort;
  286. const messages: ModelInputMessage[] = [
  287. {
  288. role: "user",
  289. content: [
  290. {
  291. type: "image",
  292. mediaType: "image/png",
  293. dataUrl: "data:image/png;base64,b25l",
  294. source: {
  295. id: "image-1",
  296. kind: "inline",
  297. path: "/tmp/stale-image.png",
  298. uri: "zcode-artifact://s/image-1",
  299. },
  300. },
  301. ],
  302. },
  303. ];
  304. const projected = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  305. expect(ensureMediaAttachmentPath).toHaveBeenCalledWith({
  306. mediaType: "image/png",
  307. uri: "zcode-artifact://s/image-1",
  308. });
  309. expect(projected[0]?.content).toEqual([
  310. expect.objectContaining({
  311. type: "image",
  312. source: expect.objectContaining({ path: "/tmp/rebuilt-image.png" }),
  313. }),
  314. { type: "text", text: "[Image: source: /tmp/rebuilt-image.png]" },
  315. ]);
  316. });
  317. it("skips only unsupported derived paths while preserving image and text-only projection", async () => {
  318. const ensureMediaAttachmentPath = vi.fn(async ({ mediaType }: { mediaType: string }) =>
  319. mediaType === "image/bmp"
  320. ? ({ status: "unsupported" } as const)
  321. : ({ status: "ready", path: "/tmp/image-1.png" } as const),
  322. );
  323. const artifactStore = {
  324. ensureMediaAttachmentPath,
  325. } as unknown as ToolArtifactStorePort;
  326. const png = {
  327. type: "image" as const,
  328. mediaType: "image/png",
  329. dataUrl: "data:image/png;base64,b25l",
  330. source: { id: "image-1", kind: "inline" as const, uri: "zcode-artifact://s/image-1" },
  331. };
  332. const bmp = {
  333. type: "image" as const,
  334. mediaType: "image/bmp",
  335. dataUrl: "data:image/bmp;base64,dHdv",
  336. source: { id: "image-2", kind: "inline" as const, uri: "zcode-artifact://s/image-2" },
  337. };
  338. const messages: ModelInputMessage[] = [
  339. {
  340. role: "user",
  341. content: [{ type: "text", text: "compare these" }, png, bmp],
  342. },
  343. ];
  344. const withPaths = await projectMessagesWithMediaAttachmentPaths(messages, artifactStore);
  345. expect(ensureMediaAttachmentPath).toHaveBeenCalledTimes(2);
  346. expect(withPaths[0]?.content).toEqual([
  347. { type: "text", text: "compare these" },
  348. expect.objectContaining({
  349. type: "image",
  350. source: expect.objectContaining({ path: "/tmp/image-1.png" }),
  351. }),
  352. bmp,
  353. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  354. ]);
  355. const projectedContent = withPaths[0]?.content;
  356. expect(Array.isArray(projectedContent) ? projectedContent[2] : undefined).toStrictEqual(bmp);
  357. const textOnly = projectMessagesForInputFormat(withPaths, {
  358. supportsImage: false,
  359. }).messages;
  360. expect(textOnly[0]?.content).toEqual([
  361. { type: "text", text: "compare these" },
  362. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  363. expect.objectContaining({ type: "text", text: expect.stringContaining("Media omitted") }),
  364. { type: "text", text: "[Image: source: /tmp/image-1.png]" },
  365. ]);
  366. expect(JSON.stringify(textOnly)).not.toContain('"type":"image"');
  367. expect(JSON.stringify(textOnly)).not.toContain("data:image");
  368. });
  369. it("replaces user image blocks when the model explicitly does not support images", () => {
  370. const messages: ModelInputMessage[] = [
  371. {
  372. role: "user",
  373. content: [
  374. { type: "text", text: "describe this" },
  375. {
  376. type: "image",
  377. mediaType: "image/png",
  378. dataUrl: "data:image/png;base64,aW1hZ2U=",
  379. source: { id: "image-1", kind: "inline", placeholder: "[image #1]" },
  380. },
  381. ],
  382. },
  383. ];
  384. const result = projectMessagesForInputFormat(messages, { supportsImage: false });
  385. expect(result.omittedMediaCount).toBe(1);
  386. expect(result.omittedImageCount).toBe(1);
  387. expect(result.retainedMediaCount).toBe(0);
  388. expect(result.messages).not.toBe(messages);
  389. expect(result.messages[0]?.content).toEqual([
  390. { type: "text", text: "describe this" },
  391. {
  392. type: "text",
  393. text: "[Attached image/png: [image #1]]\n[Media omitted from provider request because the selected model does not support image input.]",
  394. },
  395. ]);
  396. });
  397. it("keeps image blocks when support is enabled", () => {
  398. const image = {
  399. type: "image" as const,
  400. mediaType: "image/png",
  401. dataUrl: "data:image/png;base64,aW1hZ2U=",
  402. };
  403. const messages: ModelInputMessage[] = [{ role: "user", content: [image] }];
  404. expect(projectMessagesForInputFormat(messages, { supportsImage: true }).messages).toBe(
  405. messages,
  406. );
  407. });
  408. it("replaces PDF file data when PDF support is explicitly false", () => {
  409. const messages: ModelInputMessage[] = [
  410. {
  411. role: "user",
  412. content: [
  413. {
  414. type: "file",
  415. mediaType: "application/pdf",
  416. name: "report.pdf",
  417. dataUrl: "data:application/pdf;base64,cGRm",
  418. },
  419. ],
  420. },
  421. ];
  422. const result = projectMessagesForInputFormat(messages, { supportsPdf: false });
  423. expect(result.omittedPdfCount).toBe(1);
  424. expect(result.messages[0]?.content).toEqual([
  425. {
  426. type: "text",
  427. text: "[Attached application/pdf: report.pdf]\n[Media omitted from provider request because the selected model does not support PDF input.]",
  428. },
  429. ]);
  430. });
  431. it("replaces video file data when video support is explicitly false", () => {
  432. const messages: ModelInputMessage[] = [
  433. {
  434. role: "user",
  435. content: [
  436. {
  437. type: "file",
  438. mediaType: "video/mp4",
  439. name: "demo.mp4",
  440. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  441. },
  442. ],
  443. },
  444. ];
  445. const result = projectMessagesForInputFormat(messages, { supportsVideo: false });
  446. expect(result.omittedMediaCount).toBe(1);
  447. expect(result.retainedMediaCount).toBe(0);
  448. expect(result.messages[0]?.content).toEqual([
  449. {
  450. type: "text",
  451. text: "[Attached video/mp4: demo.mp4]\n[Media omitted from provider request because the selected model does not support video input.]",
  452. },
  453. ]);
  454. });
  455. it("replaces PDF file data when extracted PDF text is empty", () => {
  456. const messages: ModelInputMessage[] = [
  457. {
  458. role: "user",
  459. content: [
  460. {
  461. type: "file",
  462. mediaType: "application/pdf",
  463. name: "empty-text.pdf",
  464. dataUrl: "data:application/pdf;base64,cGRm",
  465. text: "",
  466. },
  467. ],
  468. },
  469. ];
  470. const result = projectMessagesForInputFormat(messages, { supportsPdf: false });
  471. expect(result.omittedPdfCount).toBe(1);
  472. expect(result.retainedMediaCount).toBe(0);
  473. expect(result.messages[0]?.content).toEqual([
  474. {
  475. type: "text",
  476. text: "[Attached application/pdf: empty-text.pdf]\n[Media omitted from provider request because the selected model does not support PDF input.]",
  477. },
  478. ]);
  479. });
  480. it("counts stripped video blocks in capability observability", () => {
  481. const messages: ModelInputMessage[] = [
  482. {
  483. role: "user",
  484. content: [
  485. {
  486. type: "video",
  487. mediaType: "video/mp4",
  488. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  489. },
  490. ],
  491. },
  492. ];
  493. const result = projectMessagesForInputFormat(messages, { supportsVideo: false });
  494. expect(result.omittedMediaCount).toBe(1);
  495. expect(result.omittedVideoCount).toBe(1);
  496. expect(result.retainedMediaCount).toBe(0);
  497. expect(JSON.stringify(result.messages)).not.toContain('"type":"video"');
  498. });
  499. });