Skip to content

Commit 0e2ebde

Browse files
committed
fix(cli): improve oclite output formatting, spinner timing, and tool call deduplication (#78, #79, #80)
1 parent 1a20287 commit 0e2ebde

1 file changed

Lines changed: 92 additions & 14 deletions

File tree

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

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,17 @@ async function listSessions(): Promise<Session.Info[]> {
376376

377377
async function handleCustomCommand(name: string, args: string) {
378378
const spinner = new Spinner("Thinking")
379-
spinner.start()
380379

381380
let first = true
382381
let totalTokens = 0
383382
const startTime = Date.now()
384383
isOperationInProgress = true
385384
let lastChunkWasToolStart = false
386385
let lastToolSummary = ""
386+
let lastChunkType: string | null = null
387+
let lastToolName = ""
388+
let lastToolArg = ""
389+
let repeatCount = 0
387390
const md = createMarkdownRenderer()
388391

389392
try {
@@ -393,6 +396,8 @@ async function handleCustomCommand(name: string, args: string) {
393396
sessionID: currentSessionID || undefined,
394397
}
395398

399+
spinner.start()
400+
396401
try {
397402
for await (const chunk of command(name, args, options)) {
398403
if (chunk.type === "start" && chunk.sessionID && !currentSessionID) {
@@ -403,35 +408,67 @@ async function handleCustomCommand(name: string, args: string) {
403408
spinner.stop(true)
404409
first = false
405410
write(`${fg.gray}(Esc to cancel)${style.reset}\n`)
411+
const rule = `${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
412+
write(rule)
406413
}
407414

408415
if (chunk.type === "text" && chunk.content) {
416+
if (lastChunkWasToolStart) {
417+
write("\n")
418+
}
409419
lastChunkWasToolStart = false
420+
lastChunkType = "text"
410421
write(md.render(chunk.content))
411422
}
412423

413424
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
414425
const tool = chunk.tool.trim()
415-
lastToolSummary = summarizeInput(tool, chunk.input)
416-
const summary = lastToolSummary ? ` ${lastToolSummary}` : ""
417-
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
426+
const arg = summarizeInput(tool, chunk.input)
427+
428+
if (tool === lastToolName && arg === lastToolArg) {
429+
repeatCount += 1
430+
const count = repeatCount + 1
431+
const countText = count > 1 ? ` (×${count})` : ""
432+
write(
433+
`\x1b[1A\r\x1b[2K${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
434+
)
435+
} else {
436+
if (lastChunkType === "text") {
437+
write("\n")
438+
}
439+
lastToolName = tool
440+
lastToolArg = arg
441+
repeatCount = 0
442+
const summary = arg ? ` ${arg}` : ""
443+
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
444+
}
445+
446+
lastToolSummary = arg
418447
lastChunkWasToolStart = true
448+
lastChunkType = "tool"
419449
}
420450

421451
if (chunk.type === "tool_end" && chunk.tool?.trim()) {
422452
const tool = chunk.tool.trim()
423-
if (lastChunkWasToolStart && lastToolSummary === summarizeInput(tool, chunk.input)) {
453+
const arg = summarizeInput(tool, chunk.input)
454+
455+
if (lastChunkWasToolStart && lastToolSummary === arg) {
456+
const count = repeatCount + 1
457+
const countText = count > 1 ? ` (×${count})` : ""
424458
write(
425-
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${lastToolSummary ? ` ${lastToolSummary}` : ""}${style.reset}\n`,
459+
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
426460
)
427461
} else {
428462
write(`${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}\n`)
429463
}
464+
430465
lastChunkWasToolStart = false
466+
lastChunkType = "tool"
431467
}
432468

433469
if (chunk.type === "error" && chunk.content) {
434470
lastChunkWasToolStart = false
471+
lastChunkType = "error"
435472
const safeContent = chunk.content.replace(/\x1b\[[0-9;]*m/g, "").replace(/[\x00-\x1f\x7f]/g, "")
436473
write(`\n${fg.red}Error: ${safeContent}${style.reset}\n`)
437474
}
@@ -442,8 +479,10 @@ async function handleCustomCommand(name: string, args: string) {
442479
}
443480

444481
write(md.flush())
482+
const rule = `\n${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
483+
write(rule)
445484
const duration = Date.now() - startTime
446-
write(`\n${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
485+
write(`${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
447486
} finally {
448487
write(cursor.show)
449488
}
@@ -460,14 +499,17 @@ async function handleCustomCommand(name: string, args: string) {
460499

461500
async function handleMessage(message: string) {
462501
const spinner = new Spinner("Thinking")
463-
spinner.start()
464502

465503
let first = true
466504
let totalTokens = 0
467505
const startTime = Date.now()
468506
isOperationInProgress = true
469507
let lastChunkWasToolStart = false
470508
let lastToolSummary = ""
509+
let lastChunkType: string | null = null
510+
let lastToolName = ""
511+
let lastToolArg = ""
512+
let repeatCount = 0
471513
const md = createMarkdownRenderer()
472514

473515
try {
@@ -477,6 +519,8 @@ async function handleMessage(message: string) {
477519
sessionID: currentSessionID || undefined,
478520
}
479521

522+
spinner.start()
523+
480524
try {
481525
for await (const chunk of chat(message, options)) {
482526
if (chunk.type === "start" && chunk.sessionID && !currentSessionID) {
@@ -487,35 +531,67 @@ async function handleMessage(message: string) {
487531
spinner.stop(true)
488532
first = false
489533
write(`${fg.gray}(Esc to cancel)${style.reset}\n`)
534+
const rule = `${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
535+
write(rule)
490536
}
491537

492538
if (chunk.type === "text" && chunk.content) {
539+
if (lastChunkWasToolStart) {
540+
write("\n")
541+
}
493542
lastChunkWasToolStart = false
543+
lastChunkType = "text"
494544
write(md.render(chunk.content))
495545
}
496546

497547
if (chunk.type === "tool_start" && chunk.tool?.trim()) {
498548
const tool = chunk.tool.trim()
499-
lastToolSummary = summarizeInput(tool, chunk.input)
500-
const summary = lastToolSummary ? ` ${lastToolSummary}` : ""
501-
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
549+
const arg = summarizeInput(tool, chunk.input)
550+
551+
if (tool === lastToolName && arg === lastToolArg) {
552+
repeatCount += 1
553+
const count = repeatCount + 1
554+
const countText = count > 1 ? ` (×${count})` : ""
555+
write(
556+
`\x1b[1A\r\x1b[2K${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
557+
)
558+
} else {
559+
if (lastChunkType === "text") {
560+
write("\n")
561+
}
562+
lastToolName = tool
563+
lastToolArg = arg
564+
repeatCount = 0
565+
const summary = arg ? ` ${arg}` : ""
566+
write(`${fg.gray}◇ ${style.reset}${fg.cyan}${tool}${style.reset}${fg.gray}${summary}${style.reset}\n`)
567+
}
568+
569+
lastToolSummary = arg
502570
lastChunkWasToolStart = true
571+
lastChunkType = "tool"
503572
}
504573

505574
if (chunk.type === "tool_end" && chunk.tool?.trim()) {
506575
const tool = chunk.tool.trim()
507-
if (lastChunkWasToolStart && lastToolSummary === summarizeInput(tool, chunk.input)) {
576+
const arg = summarizeInput(tool, chunk.input)
577+
578+
if (lastChunkWasToolStart && lastToolSummary === arg) {
579+
const count = repeatCount + 1
580+
const countText = count > 1 ? ` (×${count})` : ""
508581
write(
509-
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${lastToolSummary ? ` ${lastToolSummary}` : ""}${style.reset}\n`,
582+
`\x1b[1A\r\x1b[2K${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}${fg.gray}${arg ? ` ${arg}` : ""}${countText}${style.reset}\n`,
510583
)
511584
} else {
512585
write(`${fg.green}✓${style.reset} ${fg.cyan}${tool}${style.reset}\n`)
513586
}
587+
514588
lastChunkWasToolStart = false
589+
lastChunkType = "tool"
515590
}
516591

517592
if (chunk.type === "error" && chunk.content) {
518593
lastChunkWasToolStart = false
594+
lastChunkType = "error"
519595
const safeContent = chunk.content.replace(/\x1b\[[0-9;]*m/g, "").replace(/[\x00-\x1f\x7f]/g, "")
520596
write(`\n${fg.red}Error: ${safeContent}${style.reset}\n`)
521597
}
@@ -526,8 +602,10 @@ async function handleMessage(message: string) {
526602
}
527603

528604
write(md.flush())
605+
const rule = `\n${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n`
606+
write(rule)
529607
const duration = Date.now() - startTime
530-
write(`\n${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
608+
write(`${fg.gray}${formatDuration(duration)} · ${formatTokens(totalTokens)}${style.reset}\n`)
531609
} finally {
532610
write(cursor.show)
533611
}

0 commit comments

Comments
 (0)