Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): keep image and blob bytes out of tool result fallback text
xpeng5278-webf6206cce50b3
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex fa0281afa..13e6595d0 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -47,7 +47,9 @@ image blocks are passed to vision-capable models and retained when tool historyis restored; malformed image entries are ignored without failing the tool call.Embedded MCP text resources contribute their `resource.text`, and MCP resourcelinks contribute a one-line `[resource link]` reference, to the model-visible-text alongside sibling text blocks; binary resources are not inlined.+text alongside sibling text blocks; binary resources are not inlined. When no+text block is available, the JSON fallback likewise keeps image `data` and+resource `blob` bytes out of the model-visible text.## 4. Runtime API (package-level)diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex e805a324a..492abda02 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -11445,6 +11445,140 @@ describe("toolResultFromUi image restoration (issue #1073)", () => {await runtime.dispose();});+ it.each([+ [+ "MCP content property",+ {+ content: [+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },+ ],+ isError: false,+ },+ ],+ [+ "plugin bare content-block array",+ [{ type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" }],+ ],+ ])(+ "keeps image bytes out of the text fallback for an image-only %s",+ async (_shape, rawContent) => {+ const host = {+ call: vi.fn().mockImplementation((method: string) =>+ Promise.resolve(+ method === "project.instructions.resolve"+ ? { entries: [] }+ : { ok: true, content: rawContent },+ ),+ ),+ };+ const runtime = createRuntime({ host });+ try {+ const read = (runtime as any).toolCatalog.get("Read");+ const result = await read.execute("image-only-result", { path: "screenshot.png" });+ const textBlocks = result.content.filter(+ (block: { type: string }) => block.type === "text",+ );+ expect(textBlocks.length).toBeGreaterThan(0);+ for (const block of textBlocks) {+ expect(block.text).not.toContain("cG5nLWJ5dGVz");+ }+ expect(result.content).toContainEqual({+ type: "image",+ data: "cG5nLWJ5dGVz",+ mimeType: "image/png",+ });+ expect(result.details).toMatchObject({ imageCount: 1 });+ } finally {+ await runtime.dispose();+ }+ },+ );++ it("keeps blob bytes out of the text fallback for an MCP resource", async () => {+ const rawContent = {+ content: [+ {+ type: "resource",+ resource: {+ uri: "file:///a.bin",+ mimeType: "application/octet-stream",+ blob: "QkxPQi1CWVRFUw==",+ },+ },+ ],+ isError: false,+ };+ const host = {+ call: vi.fn().mockImplementation((method: string) =>+ Promise.resolve(+ method === "project.instructions.resolve"+ ? { entries: [] }+ : { ok: true, content: rawContent },+ ),+ ),+ };+ const runtime = createRuntime({ host });+ try {+ const read = (runtime as any).toolCatalog.get("Read");+ const result = await read.execute("blob-resource-result", { path: "a.bin" });+ const textBlocks = result.content.filter(+ (block: { type: string }) => block.type === "text",+ );+ expect(textBlocks.length).toBeGreaterThan(0);+ for (const block of textBlocks) {+ expect(block.text).not.toContain("QkxPQi1CWVRFUw==");+ expect(block.text).toContain("file:///a.bin");+ expect(block.text).toContain("application/octet-stream");+ }+ } finally {+ await runtime.dispose();+ }+ });++ it("keeps image bytes out of the text fallback for a non-vision model", async () => {+ const rawContent = {+ content: [+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },+ ],+ isError: false,+ };+ const host = {+ call: vi.fn().mockImplementation((method: string) =>+ Promise.resolve(+ method === "project.instructions.resolve"+ ? { entries: [] }+ : { ok: true, content: rawContent },+ ),+ ),+ };+ const runtime = createRuntime({+ host,+ provider: {+ ...provider,+ modelConfig: {+ ...provider.modelConfig!,+ input: ["text"],+ },+ },+ });+ try {+ const read = (runtime as any).toolCatalog.get("Read");+ const result = await read.execute("image-only-text-model", { path: "screenshot.png" });+ const textBlocks = result.content.filter(+ (block: { type: string }) => block.type === "text",+ );+ expect(textBlocks.length).toBeGreaterThan(0);+ for (const block of textBlocks) {+ expect(block.text).not.toContain("cG5nLWJ5dGVz");+ }+ expect(result.content.some((block: { type: string }) => block.type === "image")).toBe(+ false,+ );+ } finally {+ await runtime.dispose();+ }+ });+it("keeps embedded MCP text resources and resource links next to a status text block", async () => {// github-mcp-server `get_file_contents` answers with a status line plus the// file body as an embedded resource, and with a resource link for large files.diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex ffcb21a31..727baab51 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -1529,6 +1529,31 @@ function hostContentBlockText(block: unknown): string | undefined {return undefined;}+/**+ * JSON text fallback for a host result that produced no text blocks. Image+ * `data` and resource `blob` bytes stay off the text channel; type, mimeType,+ * and uri remain so the model still sees what was attached.+ */+function withoutInlineBinary(value: unknown): unknown {+ if (Array.isArray(value)) return value.map((entry) => withoutInlineBinary(entry));+ if (!isRecord(value)) return value;+ const next: Record<string, unknown> = {};+ for (const [key, entry] of Object.entries(value)) {+ if (key === "data" && value.type === "image") continue;+ if (key === "resource" && value.type === "resource" && isRecord(entry)) {+ const resource: Record<string, unknown> = {};+ for (const [resourceKey, resourceValue] of Object.entries(entry)) {+ if (resourceKey === "blob") continue;+ resource[resourceKey] = withoutInlineBinary(resourceValue);+ }+ next.resource = resource;+ continue;+ }+ next[key] = withoutInlineBinary(entry);+ }+ return next;+}+/** 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. Exported for tests. */@@ -3576,7 +3601,9 @@ Do not invent objections or turn speculative risks into blockers. Stop when theif (blockText !== undefined) textParts.push(blockText);}collectImageBlocks(rawContent);- text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);+ text =+ textParts.join("\n") ||+ JSON.stringify(withoutInlineBinary(rawContent), null, 2);details = { blocks: rawContent, imageCount: imageBlocks.length };} else if (isRecord(rawContent) && Array.isArray(rawContent.content)) {// MCP tools return `content: [{ type: "image" | "text", ... }]`.@@ -3587,7 +3614,9 @@ Do not invent objections or turn speculative risks into blockers. Stop when theif (blockText !== undefined) textParts.push(blockText);}collectImageBlocks(contentBlocks);- text = textParts.join("\n") || JSON.stringify(rawContent, null, 2);+ text =+ textParts.join("\n") ||+ JSON.stringify(withoutInlineBinary(rawContent), null, 2);details = { ...rawContent, imageCount: imageBlocks.length };} else if (isRecord(rawContent) && Array.isArray(rawContent.images)) {text =