11// @effect-diagnostics nodeBuiltinImport:off -- workflow fixtures use files and the fake Claude CLI uses Node streams
2+ import { PDFDocument } from "pdf-lib";
23import * as NodeEvents from "node:events";
34import * as NodeFS from "node:fs";
45import * as NodeOS from "node:os";
@@ -24,6 +25,7 @@ import type {
2425import * as NodeServices from "@effect/platform-node/NodeServices";
2526import {
2627 ChatAttachmentId,
28+ type ChatAttachment,
2729 ChatFileAttachment,
2830 ChatImageAttachment,
2931 CheckpointId,
@@ -1601,7 +1603,7 @@ describe("ClaudeAdapterV2 resume compaction", () => {
16011603});
16021604
16031605describe("ClaudeAdapterV2 attachments", () => {
1604- it.effect("forwards images and references generic files on sends and steering ", () =>
1606+ it.effect("sends native PDFs and images with path fallbacks ", () =>
16051607 Effect.scoped(
16061608 Effect.gen(function* () {
16071609 const fileSystem = yield* FileSystem.FileSystem;
@@ -1677,9 +1679,12 @@ describe("ClaudeAdapterV2 attachments", () => {
16771679 path.join(attachmentsDir, attachmentRelativePath(attachment)!),
16781680 Uint8Array.from([1, 2, 3, 4]),
16791681 );
1682+ const pdf = yield* Effect.promise(() => PDFDocument.create());
1683+ pdf.addPage();
1684+ const pdfBytes = yield* Effect.promise(() => pdf.save());
16801685 yield* fileSystem.writeFile(
16811686 path.join(attachmentsDir, attachmentRelativePath(document)!),
1682- Uint8Array.from([5, 6, 7, 8]) ,
1687+ pdfBytes ,
16831688 );
16841689 const attemptId = RunAttemptId.make("attempt-claude-attachments");
16851690 const now = yield* DateTime.now;
@@ -1703,13 +1708,22 @@ describe("ClaudeAdapterV2 attachments", () => {
17031708 data: "AQIDBA==",
17041709 },
17051710 } as const;
1711+ const expectedDocumentBlock = {
1712+ type: "document",
1713+ source: {
1714+ type: "base64",
1715+ media_type: "application/pdf",
1716+ data: Buffer.from(pdfBytes).toString("base64"),
1717+ },
1718+ } as const;
17061719 const expectedAttachmentPath = path.join(
17071720 attachmentsDir,
17081721 attachmentRelativePath(attachment)!,
17091722 );
17101723 const expectedDocumentPath = path.join(attachmentsDir, attachmentRelativePath(document)!);
17111724 assert.deepEqual(offeredMessages[0]?.message.content, [
17121725 expectedImageBlock,
1726+ expectedDocumentBlock,
17131727 {
17141728 type: "text",
17151729 text: `Ultrathink:\nWhat's in this image?\n\n[Attached image "diagram.png" is saved at: ${expectedAttachmentPath}]\n\n[Attached file "requirements.pdf" is saved at: ${expectedDocumentPath}]`,
@@ -1737,11 +1751,69 @@ describe("ClaudeAdapterV2 attachments", () => {
17371751 assert.equal(offeredMessages[1]?.priority, "now");
17381752 assert.deepEqual(offeredMessages[1]?.message.content, [
17391753 expectedImageBlock,
1754+ expectedDocumentBlock,
17401755 {
17411756 type: "text",
17421757 text: `Ultrathink:\nFocus on the diagram labels.\n\n[Attached image "diagram.png" is saved at: ${expectedAttachmentPath}]\n\n[Attached file "requirements.pdf" is saved at: ${expectedDocumentPath}]`,
17431758 },
17441759 ]);
1760+ const documentPath = path.join(attachmentsDir, attachmentRelativePath(document)!);
1761+ for (const scenario of [
1762+ "pages",
1763+ "bytes",
1764+ "invalid",
1765+ "aggregate-pages",
1766+ "pasted-text",
1767+ "text-only",
1768+ ]) {
1769+ let attachments: ReadonlyArray<ChatAttachment> = [document];
1770+ if (scenario === "pages" || scenario === "aggregate-pages") {
1771+ const large = yield* Effect.promise(() => PDFDocument.create());
1772+ for (let i = 0; i < (scenario === "pages" ? 101 : 60); i++) large.addPage();
1773+ yield* fileSystem.writeFile(documentPath, yield* Effect.promise(() => large.save()));
1774+ if (scenario === "aggregate-pages") {
1775+ const second = {
1776+ ...document,
1777+ id: ChatAttachmentId.make("second-pdf"),
1778+ name: "second.pdf",
1779+ };
1780+ yield* fileSystem.copyFile(
1781+ documentPath,
1782+ path.join(attachmentsDir, attachmentRelativePath(second)!),
1783+ );
1784+ attachments = [document, second];
1785+ }
1786+ } else if (scenario === "bytes") {
1787+ yield* fileSystem.truncate(documentPath, 20 * 1024 * 1024 + 1);
1788+ } else if (scenario === "invalid") {
1789+ yield* fileSystem.writeFileString(documentPath, "This is not a PDF");
1790+ } else if (scenario === "pasted-text") {
1791+ yield* fileSystem.writeFile(documentPath, pdfBytes);
1792+ attachments = [{ ...document, source: { _tag: "pasted-text" } }];
1793+ } else {
1794+ attachments = [];
1795+ yield* fileSystem.remove(documentPath);
1796+ }
1797+ yield* runtime.steerTurn({
1798+ threadId,
1799+ runId: RunId.make("run-claude-attachments"),
1800+ providerThread,
1801+ providerTurnId,
1802+ message: {
1803+ createdBy: "user",
1804+ creationSource: "web",
1805+ messageId: MessageId.make(`message-${scenario}`),
1806+ text: "Continue.",
1807+ attachments,
1808+ },
1809+ });
1810+ const sent = offeredMessages.at(-1)!.message.content;
1811+ const documents =
1812+ typeof sent === "string" ? [] : sent.filter((block) => block.type === "document");
1813+ assert.lengthOf(documents, scenario === "aggregate-pages" ? 1 : 0, scenario);
1814+ if (scenario === "text-only") assert.equal(sent, "Ultrathink:\nContinue.");
1815+ else assert.include(JSON.stringify(sent), documentPath);
1816+ }
17451817 }).pipe(
17461818 Effect.provide(
17471819 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
0 commit comments