Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(runtime): deliver MCP and plugin tool images as image blocks

yexisua5f42ea5fa33
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index f4d2dec13..eac2fa163 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -10748,6 +10748,30 @@ describe("toolResultFromUi image restoration (issue #1073)", () => {
const restored = toolResultFromUi(row, timestamp);
expect(restored.content).toEqual([{ type: "text", text: expect.stringContaining("broken.png") }]);
});
+
+ it("restores a bare plugin content-block array without flattening it to JSON (#1360)", () => {
+ const row = {
+ id: "call-3",
+ role: "tool" as const,
+ content: "",
+ createdAt: new Date(timestamp).toISOString(),
+ toolCallId: "call-3",
+ toolName: "plugin_shot",
+ // A plugin tool returned a bare content-block array rather than an
+ // object with `content` or a top-level `images` field.
+ toolResult: [
+ { type: "text", text: "here is the screenshot" },
+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },
+ ],
+ toolStatus: "success" as const,
+ isError: false,
+ };
+ const restored = toolResultFromUi(row, timestamp);
+ expect(restored.content).toEqual([
+ { type: "text", text: "here is the screenshot" },
+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },
+ ]);
+ });
});
it("does not reuse stale plugin declarations when schema or permission metadata changes", async () => {
diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.ts
index a3f1cdc62..97fa98d24 100644
--- a/packages/agent-runtime/src/runtime.ts
+++ b/packages/agent-runtime/src/runtime.ts
@@ -1543,6 +1543,21 @@ export function toolResultFromUi(
blocks.push({ type: "image", data: b.data, mimeType: b.mimeType });
}
}
+ } else if (Array.isArray(raw)) {
+ // Plugin tools may return a bare content-block array; restore its text
+ // and image blocks so a restart does not flatten them into JSON (#1360).
+ for (const b of raw) {
+ if (!isRecord(b)) continue;
+ if (b.type === "text" && typeof b.text === "string") {
+ blocks.push({ type: "text", text: b.text });
+ } else if (
+ b.type === "image" &&
+ typeof b.data === "string" &&
+ typeof b.mimeType === "string"
+ ) {
+ blocks.push({ type: "image", data: b.data, mimeType: b.mimeType });
+ }
+ }
} else if (typeof raw === "string" && raw.trim()) {
blocks.push({ type: "text", text: raw });
} else if (raw !== undefined && raw !== null) {
@@ -3448,8 +3463,54 @@ Do not invent objections or turn speculative risks into blockers. Stop when the
const imageBlocks: Array<{ type: "image"; data: string; mimeType: string }> = [];
let text: string;
let details: unknown = rawContent;
+ const vision = visionFromModelConfig(this.provider.modelConfig);
+ // Collect image blocks from a content-block array; only well-formed
+ // image entries ({ type, data, mimeType }) are accepted.
+ const collectImageBlocks = (blocks: unknown[]): void => {
+ for (const block of blocks) {
+ if (
+ isRecord(block) &&
+ block.type === "image" &&
+ typeof block.data === "string" &&
+ typeof block.mimeType === "string"
+ ) {
+ if (vision) {
+ imageBlocks.push({
+ type: "image",
+ data: block.data,
+ mimeType: block.mimeType,
+ });
+ }
+ }
+ }
+ };
if (typeof rawContent === "string") {
text = rawContent;
+ } else if (Array.isArray(rawContent)) {
+ // Plugin tools may return a bare content-block array
+ // ([{ type: "text" }, { type: "image" }]); render the text blocks
+ // and hand images to the model instead of stringifying (#1360).
+ const textParts: string[] = [];
+ for (const block of rawContent) {
+ if (isRecord(block) && block.type === "text" && typeof block.text === "string") {
+ textParts.push(block.text);
+ }
+ }
+ collectImageBlocks(rawContent);
+ text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);
+ details = { blocks: rawContent, imageCount: imageBlocks.length };
+ } else if (isRecord(rawContent) && Array.isArray(rawContent.content)) {
+ // MCP tools return `content: [{ type: "image" | "text", ... }]`.
+ const contentBlocks = rawContent.content;
+ const textParts: string[] = [];
+ for (const block of contentBlocks) {
+ if (isRecord(block) && block.type === "text" && typeof block.text === "string") {
+ textParts.push(block.text);
+ }
+ }
+ collectImageBlocks(contentBlocks);
+ text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);
+ details = { ...rawContent, imageCount: imageBlocks.length };
} else if (isRecord(rawContent) && Array.isArray(rawContent.images)) {
text =
typeof rawContent.text === "string"
@@ -3459,7 +3520,6 @@ Do not invent objections or turn speculative risks into blockers. Stop when the
null,
2,
);
- const vision = visionFromModelConfig(this.provider.modelConfig);
for (const image of rawContent.images) {
if (
!isRecord(image) ||