Skip to content

Commit 5982d0f

Browse files
committed
fix(cli): buffer streaming text to prevent per-chunk padding (#92)
1 parent 0ce8c74 commit 5982d0f

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

‎packages/opencode/src/cli/lite/index.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
346346
let dedupCount = 0
347347
const idMap = new Map<string, string>()
348348
const md = createMarkdownRenderer()
349+
let lineBuffer = ""
350+
351+
function flushLineBuffer() {
352+
if (!lineBuffer) return
353+
const rendered = md.render(lineBuffer)
354+
const wrapped = wrap(rendered, MAX_WIDTH)
355+
write(padLines(wrapped))
356+
lineBuffer = ""
357+
}
349358

350359
try {
351360
for await (const chunk of source) {
@@ -366,12 +375,27 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
366375
lastToolKey = ""
367376
dedupCount = 0
368377
lastChunkType = "text"
369-
const rendered = md.render(chunk.content)
370-
const wrapped = wrap(rendered, MAX_WIDTH)
371-
write(padLines(wrapped))
378+
379+
lineBuffer += chunk.content
380+
381+
const parts = lineBuffer.split("\n")
382+
383+
for (let idx = 0; idx < parts.length - 1; idx++) {
384+
const line = parts[idx]
385+
const rendered = md.render(line + "\n")
386+
const wrapped = wrap(rendered, MAX_WIDTH)
387+
write(padLines(wrapped))
388+
}
389+
390+
lineBuffer = parts[parts.length - 1]
391+
392+
if (lineBuffer.length > MAX_WIDTH) {
393+
flushLineBuffer()
394+
}
372395
}
373396

374397
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
398+
flushLineBuffer()
375399
const tool = chunk.tool.trim()
376400
const arg = summarizeInput(tool, chunk.input)
377401
const summary = arg || ""
@@ -394,6 +418,7 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
394418
}
395419

396420
if (chunk.type === "tool_end" && chunk.tool?.trim()) {
421+
flushLineBuffer()
397422
const tool = chunk.tool.trim()
398423
const arg = summarizeInput(tool, chunk.input)
399424
const summary = arg || ""
@@ -446,6 +471,7 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
446471
}
447472

448473
if (chunk.type === "error" && chunk.content) {
474+
flushLineBuffer()
449475
block.freeze()
450476
lastToolKey = ""
451477
dedupCount = 0
@@ -461,15 +487,18 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
461487
totalTokens += chunk.tokens
462488
}
463489
}
464-
490+
} finally {
491+
spinner.stop(true)
492+
flushLineBuffer()
465493
block.freeze()
466-
write(md.flush())
494+
const flushed = md.flush()
495+
if (flushed) {
496+
write(padLines(flushed))
497+
}
467498
const rule = `\n${PAD}${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
468499
write(rule)
469500
const duration = Date.now() - startTime
470501
write(`${PAD}${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
471-
} finally {
472-
spinner.stop(true)
473502
}
474503

475504
write("\n")

0 commit comments

Comments
 (0)