GeezerVison is showing github.com — fix(acp): preserve resource text source (#33524) · soequelle/opencode@8e2d422
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/content.ts +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { ContentBlock, ContentChunk, ResourceLink, Role } from "@agentclientprotocol/sdk" | 2 | 2 | import path from "node:path" | 3 | | -import { pathToFileURL } from "node:url" | | 3 | +import { fileURLToPath, pathToFileURL } from "node:url" | 4 | 4 | import { SessionV1 } from "@opencode-ai/core/v1/session" | 5 | 5 |
| 6 | 6 | export type PromptPart = SessionV1.TextPartInput | SessionV1.FilePartInput | @@ -76,7 +76,19 @@ export function contentBlockToParts(block: ContentBlock): PromptPart[] { | 76 | 76 |
| 77 | 77 | case "resource": | 78 | 78 | if ("text" in block.resource) { | 79 | | - return [{ type: "text", text: block.resource.text }] | | 79 | + try { | | 80 | + const parsed = new URL(block.resource.uri) | | 81 | + if (parsed.protocol === "file:") { | | 82 | + const line = parsed.hash.match(/^#L(\d+)/)?.[1] | | 83 | + return [ | | 84 | + { | | 85 | + type: "text", | | 86 | + text: `[${fileURLToPath(parsed)}${line ? `:${line}` : ""}]\n${block.resource.text}`, | | 87 | + }, | | 88 | + ] | | 89 | + } | | 90 | + } catch {} | | 91 | + return [{ type: "text", text: `[${block.resource.uri}]\n${block.resource.text}` }] | 80 | 92 | } | 81 | 93 | if (block.resource.mimeType) { | 82 | 94 | return [ | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/acp/content.test.ts +15-3Lines changed: 15 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -99,17 +99,29 @@ describe("acp content conversion", () => { | 99 | 99 | ]) | 100 | 100 | }) | 101 | 101 |
| 102 | | - test("resource with text becomes a text part", () => { | | 102 | + test("resource with text becomes a sourced text part", () => { | 103 | 103 | expect( | 104 | 104 | contentBlockToParts({ | 105 | 105 | type: "resource", | 106 | 106 | resource: { | 107 | | - uri: "file:///tmp/context.txt", | | 107 | + uri: "file:///tmp/context.txt#L12-L14", | 108 | 108 | mimeType: "text/plain", | 109 | 109 | text: "context", | 110 | 110 | }, | 111 | 111 | }), | 112 | | - ).toEqual([{ type: "text", text: "context" }]) | | 112 | + ).toEqual([{ type: "text", text: "[/tmp/context.txt:12]\ncontext" }]) | | 113 | + }) | | 114 | + | | 115 | + test("resource with text uses URI fallback for non-file resources", () => { | | 116 | + expect( | | 117 | + contentBlockToParts({ | | 118 | + type: "resource", | | 119 | + resource: { | | 120 | + uri: "mcp://server/context", | | 121 | + text: "context", | | 122 | + }, | | 123 | + }), | | 124 | + ).toEqual([{ type: "text", text: "[mcp://server/context]\ncontext" }]) | 113 | 125 | }) | 114 | 126 |
| 115 | 127 | test("resource with blob and mimeType becomes a data URL file part", () => { | |
|