Skip to content

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

fix(agent-runtime): restore Read image results as image content blocks

yexisu4bf5687cc2ed
diff --git a/crates/host-core/src/tools/mod.rs b/crates/host-core/src/tools/mod.rs
index 39a5402d7..fdac4fdb4 100644
--- a/crates/host-core/src/tools/mod.rs
+++ b/crates/host-core/src/tools/mod.rs
@@ -1245,7 +1245,10 @@ fn read_image_block(
}
let bytes = std::fs::read(resolved).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
- ("FILE_NOT_FOUND".into(), format!("File not found: {display}"))
+ (
+ "FILE_NOT_FOUND".into(),
+ format!("File not found: {display}"),
+ )
} else {
("TOOL_FAILED".into(), format!("read failed: {e}"))
}
@@ -3430,7 +3433,11 @@ mod tests {
5_000,
)
.await;
- assert!(result.ok, "png should read as an image block: {:?}", result.error_code);
+ assert!(
+ result.ok,
+ "png should read as an image block: {:?}",
+ result.error_code
+ );
let content = result.content.as_object().expect("object result");
let images = content["images"].as_array().expect("images array");
assert_eq!(images.len(), 1);
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index 3a082d600..a511a7060 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -15,6 +15,7 @@ import {
DesktopAgentRuntime,
PATH_INSTRUCTION_RESOLUTION_TIMEOUT_MS,
looksLikePseudoToolCall,
+ toolResultFromUi,
type CompactionStrategy,
type PluginToolDef,
type RuntimeMatchConfig,
@@ -10399,3 +10400,50 @@ describe("DesktopAgentRuntime summary conversation key", () => {
}
});
});
+
+describe("toolResultFromUi image restoration (issue #1073)", () => {
+ const timestamp = 1;
+
+ it("restores a host Read image result as real image content blocks", () => {
+ const row = {
+ id: "call-1",
+ role: "tool" as const,
+ content: "",
+ createdAt: new Date(timestamp).toISOString(),
+ toolCallId: "call-1",
+ toolName: "Read",
+ // Host tool_read returns a top-level images array, not content blocks.
+ toolResult: {
+ path: "shot.png",
+ text: "Image file shot.png (17 bytes, image/png); the image is attached to this result.",
+ images: [{ data: "cG5nLWJ5dGVz", mimeType: "image/png" }],
+ },
+ toolStatus: "success" as const,
+ isError: false,
+ };
+ const restored = toolResultFromUi(row, timestamp);
+ expect(restored.content).toEqual([
+ { type: "text", text: expect.stringContaining("shot.png") },
+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },
+ ]);
+ });
+
+ it("drops malformed image entries instead of failing the restore", () => {
+ const row = {
+ id: "call-2",
+ role: "tool" as const,
+ content: "",
+ createdAt: new Date(timestamp).toISOString(),
+ toolCallId: "call-2",
+ toolName: "Read",
+ toolResult: {
+ path: "broken.png",
+ images: [{ data: 42 }, "not-an-object", { data: "ok", mimeType: 7 }],
+ },
+ toolStatus: "success" as const,
+ isError: false,
+ };
+ const restored = toolResultFromUi(row, timestamp);
+ expect(restored.content).toEqual([{ type: "text", text: expect.stringContaining("broken.png") }]);
+ });
+});
diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.ts
index 2ca10f5f5..86bea2319 100644
--- a/packages/agent-runtime/src/runtime.ts
+++ b/packages/agent-runtime/src/runtime.ts
@@ -1467,8 +1467,8 @@ function selectRetainedUserMessages(
/** Rebuild a pi-ai tool result from a persisted tool row. Rows that never
* finished (app quit / abort mid-tool) restore as errored results so the
- * model knows the call produced nothing. */
-function toolResultFromUi(
+ * model knows the call produced nothing. Exported for tests. */
+export function toolResultFromUi(
m: UiMessage,
timestamp: number,
): ToolResultMessage {
@@ -1496,7 +1496,26 @@ function toolResultFromUi(
} else if (typeof raw === "string" && raw.trim()) {
blocks.push({ type: "text", text: raw });
} else if (raw !== undefined && raw !== null) {
- blocks.push({ type: "text", text: safeJson(raw) });
+ // A host Read of an image file returns a top-level `images` array rather
+ // than content blocks; restore those as real image content so the model
+ // sees the picture across a restart, not just the JSON text (#1073).
+ const images =
+ isRecord(raw) && Array.isArray(raw.images) ? raw.images : undefined;
+ if (images) {
+ const { images: _images, ...rest } = raw;
+ blocks.push({ type: "text", text: safeJson(rest) });
+ for (const image of images) {
+ if (
+ isRecord(image) &&
+ typeof image.data === "string" &&
+ typeof image.mimeType === "string"
+ ) {
+ blocks.push({ type: "image", data: image.data, mimeType: image.mimeType });
+ }
+ }
+ } else {
+ blocks.push({ type: "text", text: safeJson(raw) });
+ }
}
const interrupted = m.toolStatus === "running";
const rawRecord: Record<string, unknown> | undefined = isRecord(raw)