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): restore MCP resource text in toolResultFromUi (#1878)
xpeng5278-web9944b39943eb
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex 749085f03..55bafca1e 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -11919,6 +11919,132 @@ describe("toolResultFromUi image restoration (issue #1073)", () => {]);});+ // github-mcp-server `get_file_contents` persists the file body as an embedded+ // text resource (and a resource link for large files). Restoring that row+ // must keep the same text the live path shows.+ const embeddedResource = {+ type: "resource" as const,+ resource: {+ uri: "file://x.ts",+ mimeType: "text/plain",+ text: "export const answer = 42;\n",+ },+ };+ const statusAndResourceBlocks = [+ { type: "text" as const, text: "successfully downloaded text file (SHA: abc123)" },+ {+ type: "resource" as const,+ resource: {+ uri: "repo://acme/app/contents/src/index.ts",+ mimeType: "text/plain",+ text: "export const answer = 42;\n",+ },+ },+ {+ type: "resource_link" as const,+ uri: "https://raw.githubusercontent.com/acme/app/main/big.json",+ name: "big.json",+ },+ { type: "image" as const, data: "cG5nLWJ5dGVz", mimeType: "image/png" },+ ];++ it.each([+ ["MCP content property", { content: [embeddedResource] }],+ ["plugin bare content-block array", [embeddedResource]],+ ])(+ "restores a text resource from a %s instead of the missing-result placeholder",+ (_shape, toolResult) => {+ const restored = toolResultFromUi(+ {+ id: "call-resource-only",+ role: "tool",+ content: "",+ createdAt: new Date(timestamp).toISOString(),+ toolCallId: "call-resource-only",+ toolName: "get_file_contents",+ toolResult,+ toolStatus: "success",+ isError: false,+ },+ timestamp,+ );+ expect(+ restored.content.some(+ (block) => block.type === "text" && block.text.includes("export const answer = 42"),+ ),+ ).toBe(true);+ expect(+ restored.content.every(+ (block) => !(block.type === "text" && block.text === "[no tool result recorded]"),+ ),+ ).toBe(true);+ },+ );++ it.each([+ ["MCP content property", { content: statusAndResourceBlocks, isError: false }],+ ["plugin bare content-block array", statusAndResourceBlocks],+ ])(+ "restores status text, resource body, resource link, and image from a %s",+ (_shape, toolResult) => {+ const restored = toolResultFromUi(+ {+ id: "call-resource-mix",+ role: "tool",+ content: "",+ createdAt: new Date(timestamp).toISOString(),+ toolCallId: "call-resource-mix",+ toolName: "get_file_contents",+ toolResult,+ toolStatus: "success",+ isError: false,+ },+ timestamp,+ );+ expect(restored.content).toEqual([+ { type: "text", text: "successfully downloaded text file (SHA: abc123)" },+ { type: "text", text: "export const answer = 42;\n" },+ {+ type: "text",+ text: "[resource link] big.json: https://raw.githubusercontent.com/acme/app/main/big.json",+ },+ { type: "image", data: "cG5nLWJ5dGVz", mimeType: "image/png" },+ ]);+ },+ );++ it("ignores a binary MCP resource that has no text", () => {+ const restored = toolResultFromUi(+ {+ id: "call-resource-blob",+ role: "tool",+ content: "",+ createdAt: new Date(timestamp).toISOString(),+ toolCallId: "call-resource-blob",+ toolName: "get_file_contents",+ toolResult: {+ content: [+ {+ type: "resource",+ resource: {+ uri: "file:///a.bin",+ mimeType: "application/octet-stream",+ blob: "QkxPQi1CWVRFUw==",+ },+ },+ ],+ },+ toolStatus: "success",+ isError: false,+ },+ timestamp,+ );+ expect(restored.content).toEqual([+ { type: "text", text: "[no tool result recorded]" },+ ]);+ expect(JSON.stringify(restored.content)).not.toContain("QkxPQi1CWVRFUw==");+ });+it.each([["MCP content property",diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex 683007315..ac4c71bb0 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -1603,8 +1603,12 @@ export function toolResultFromUi(if (rawBlocks) {for (const b of rawBlocks) {if (!isRecord(b)) continue;- if (b.type === "text" && typeof b.text === "string") {- blocks.push({ type: "text", text: b.text });+ // Same text rule as live execution: embedded MCP resources and resource+ // links stay visible after a session restore. Binary resources have no+ // text and stay out.+ const blockText = hostContentBlockText(b);+ if (blockText !== undefined) {+ blocks.push({ type: "text", text: blockText });} else if (b.type === "image" &&typeof b.data === "string" &&@@ -1618,8 +1622,9 @@ export function toolResultFromUi(// 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 });+ const blockText = hostContentBlockText(b);+ if (blockText !== undefined) {+ blocks.push({ type: "text", text: blockText });} else if (b.type === "image" &&typeof b.data === "string" &&