Skip to content

Commit 196152b

Browse files
authored
Handle embedded resource blobs in prompts (#202)
1 parent 090c957 commit 196152b

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/CodexAcpClient.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,24 @@ function buildPromptItems(prompt: acp.ContentBlock[]): UserInput[] {
686686
const context = `<context ref="${resource.uri}">\n${resource.text}\n</context>`;
687687
return {type: "text", text: `${link}\n${context}`, text_elements: []};
688688
}
689-
return null;
689+
if (isImageMimeType(resource.mimeType)) {
690+
return {type: "image", url: `data:${resource.mimeType};base64,${resource.blob}`};
691+
}
692+
const link = formatUriAsLink(null, resource.uri);
693+
const mimeType = resource.mimeType ?? "application/octet-stream";
694+
const context = `<context ref="${resource.uri}" mimeType="${mimeType}" encoding="base64">\n${resource.blob}\n</context>`;
695+
return {type: "text", text: `${link}\n${context}`, text_elements: []};
690696
}
691697
case "audio":
692698
return null;
693699
}
694700
}).filter((block): block is UserInput => block !== null);
695701
}
696702

703+
function isImageMimeType(mimeType: string | null | undefined): mimeType is string {
704+
return mimeType?.startsWith("image/") ?? false;
705+
}
706+
697707
function formatUriAsLink(name: string | null | undefined, uri: string): string {
698708
if (name && name.length > 0) {
699709
return `[@${name}](${uri})`;

src/__tests__/CodexACPAgent/CodexAcpClient.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ describe('ACP server test', { timeout: 40_000 }, () => {
730730
{ type: "image", mimeType: "image/png", data: "abc123", uri: "https://example.com/image.png" },
731731
{ type: "resource_link", name: "report.txt", uri: "file:///tmp/report.txt" },
732732
{ type: "resource", resource: { uri: "file:///tmp/notes.txt", text: "Notes body" } as acp.EmbeddedResourceResource },
733+
{ type: "resource", resource: { uri: "file:///tmp/pixel.png", mimeType: "image/png", blob: "iVBORw0KGgo=" } as acp.EmbeddedResourceResource },
734+
{ type: "resource", resource: { uri: "file:///tmp/archive.bin", mimeType: "application/octet-stream", blob: "AAEC" } as acp.EmbeddedResourceResource },
733735
];
734736

735737
await codexAcpAgent.prompt({ sessionId: "session-id", prompt });

src/__tests__/CodexACPAgent/data/send-attachments-turn-start.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
"type": "text",
3636
"text": "[@notes.txt](file:///tmp/notes.txt)\n<context ref=\"file:///tmp/notes.txt\">\nNotes body\n</context>",
3737
"text_elements": []
38+
},
39+
{
40+
"type": "image",
41+
"url": "data:image/png;base64,iVBORw0KGgo="
42+
},
43+
{
44+
"type": "text",
45+
"text": "[@archive.bin](file:///tmp/archive.bin)\n<context ref=\"file:///tmp/archive.bin\" mimeType=\"application/octet-stream\" encoding=\"base64\">\nAAEC\n</context>",
46+
"text_elements": []
3847
}
3948
],
4049
"approvalPolicy": "on-request",

0 commit comments

Comments
 (0)