From e03bd72c84187ede0a0f02dc7e042d3c46d196c2 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sat, 7 Mar 2026 12:11:40 -0500 Subject: [PATCH] Fix chunked SSE parsing in OpenCode plugin --- opencode/glance.test.ts | 25 +++++++++++++++++++++++++ opencode/glance.ts | 5 ++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/opencode/glance.test.ts b/opencode/glance.test.ts index 28ded77..e6e91c2 100644 --- a/opencode/glance.test.ts +++ b/opencode/glance.test.ts @@ -191,6 +191,31 @@ describe("opencode glance plugin", () => { ) }) + it("parses image events split across SSE chunks", async () => { + const expiresAt = Date.now() + 60_000 + + vi.stubGlobal( + "fetch", + routedFetch({ + session: { id: "sess-chunked", url: "/s/sess-chunked" }, + sseEvents: [ + 'event: image\ndata: {"url":"https://glance.sh/chunked.png",', + `"expiresAt":${expiresAt}}\n\n`, + ], + }), + ) + + const GlancePlugin = await loadPlugin() + const plugin = await GlancePlugin(mockClient()) + + await plugin.tool.glance.execute({}) + + const ctx = mockContext() + const result = await plugin.tool.glance_wait.execute({}, ctx) + + expect(result).toContain("https://glance.sh/chunked.png") + }) + it("returns timeout message when aborted", async () => { vi.stubGlobal( "fetch", diff --git a/opencode/glance.ts b/opencode/glance.ts index b3d59e9..1b26e39 100644 --- a/opencode/glance.ts +++ b/opencode/glance.ts @@ -120,6 +120,8 @@ async function listenForImages( const reader = res.body.getReader() const decoder = new TextDecoder() let buffer = "" + let eventType = "" + let dataLines: string[] = [] const timeout = setTimeout(() => { reader.cancel() @@ -140,9 +142,6 @@ async function listenForImages( const lines = buffer.split("\n") buffer = lines.pop() ?? "" - let eventType = "" - let dataLines: string[] = [] - for (const line of lines) { if (line.startsWith("event: ")) { eventType = line.slice(7).trim()