Skip to content

Commit e38767c

Browse files
authored
Fix chunked SSE parsing in OpenCode plugin (#4)
1 parent cfe4956 commit e38767c

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

opencode/glance.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ describe("opencode glance plugin", () => {
191191
)
192192
})
193193

194+
it("parses image events split across SSE chunks", async () => {
195+
const expiresAt = Date.now() + 60_000
196+
197+
vi.stubGlobal(
198+
"fetch",
199+
routedFetch({
200+
session: { id: "sess-chunked", url: "/s/sess-chunked" },
201+
sseEvents: [
202+
'event: image\ndata: {"url":"https://glance.sh/chunked.png",',
203+
`"expiresAt":${expiresAt}}\n\n`,
204+
],
205+
}),
206+
)
207+
208+
const GlancePlugin = await loadPlugin()
209+
const plugin = await GlancePlugin(mockClient())
210+
211+
await plugin.tool.glance.execute({})
212+
213+
const ctx = mockContext()
214+
const result = await plugin.tool.glance_wait.execute({}, ctx)
215+
216+
expect(result).toContain("https://glance.sh/chunked.png")
217+
})
218+
194219
it("returns timeout message when aborted", async () => {
195220
vi.stubGlobal(
196221
"fetch",

opencode/glance.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ async function listenForImages(
120120
const reader = res.body.getReader()
121121
const decoder = new TextDecoder()
122122
let buffer = ""
123+
let eventType = ""
124+
let dataLines: string[] = []
123125

124126
const timeout = setTimeout(() => {
125127
reader.cancel()
@@ -140,9 +142,6 @@ async function listenForImages(
140142
const lines = buffer.split("\n")
141143
buffer = lines.pop() ?? ""
142144

143-
let eventType = ""
144-
let dataLines: string[] = []
145-
146145
for (const line of lines) {
147146
if (line.startsWith("event: ")) {
148147
eventType = line.slice(7).trim()

0 commit comments

Comments
 (0)