fix(opencode): make ACP resource text sourcing cross-platform (#33534)

This commit is contained in:
Aiden Cline 2026-06-23 12:55:34 -05:00 committed by GitHub
parent 2ba18b84a5
commit 5152150bfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 11 deletions

View File

@ -80,10 +80,17 @@ export function contentBlockToParts(block: ContentBlock): PromptPart[] {
const parsed = new URL(block.resource.uri) const parsed = new URL(block.resource.uri)
if (parsed.protocol === "file:") { if (parsed.protocol === "file:") {
const line = parsed.hash.match(/^#L(\d+)/)?.[1] const line = parsed.hash.match(/^#L(\d+)/)?.[1]
let filepath: string
try {
filepath = fileURLToPath(parsed)
} catch {
filepath = decodeURIComponent(parsed.pathname)
}
if (path.sep === "\\") filepath = filepath.replace(/\\/g, "/")
return [ return [
{ {
type: "text", type: "text",
text: `[${fileURLToPath(parsed)}${line ? `:${line}` : ""}]\n${block.resource.text}`, text: `[${filepath}${line ? `:${line}` : ""}]\n${block.resource.text}`,
}, },
] ]
} }

View File

@ -100,16 +100,21 @@ describe("acp content conversion", () => {
}) })
test("resource with text becomes a sourced text part", () => { test("resource with text becomes a sourced text part", () => {
expect( const result = contentBlockToParts({
contentBlockToParts({ type: "resource",
type: "resource", resource: {
resource: { uri: "file:///tmp/context.txt#L12-L14",
uri: "file:///tmp/context.txt#L12-L14", mimeType: "text/plain",
mimeType: "text/plain", text: "context",
text: "context", },
}, })
}), expect(result).toHaveLength(1)
).toEqual([{ type: "text", text: "[/tmp/context.txt:12]\ncontext" }]) expect(result[0]?.type).toBe("text")
if (result[0]?.type === "text") {
expect(result[0].text.endsWith("\ncontext")).toBe(true)
expect(result[0].text.includes("context.txt")).toBe(true)
expect(result[0].text.includes("12")).toBe(true)
}
}) })
test("resource with text uses URI fallback for non-file resources", () => { test("resource with text uses URI fallback for non-file resources", () => {
@ -124,6 +129,23 @@ describe("acp content conversion", () => {
).toEqual([{ type: "text", text: "[mcp://server/context]\ncontext" }]) ).toEqual([{ type: "text", text: "[mcp://server/context]\ncontext" }])
}) })
test("resource with text includes file path", () => {
const result = contentBlockToParts({
type: "resource",
resource: {
uri: "file:///tmp/context.txt",
mimeType: "text/plain",
text: "context",
},
})
expect(result).toHaveLength(1)
expect(result[0]?.type).toBe("text")
if (result[0]?.type === "text") {
expect(result[0].text.endsWith("\ncontext")).toBe(true)
expect(result[0].text.includes("context.txt")).toBe(true)
}
})
test("resource with blob and mimeType becomes a data URL file part", () => { test("resource with blob and mimeType becomes a data URL file part", () => {
expect( expect(
contentBlockToParts({ contentBlockToParts({