Skip to content

Commit ce74e69

Browse files
committed
feat(cli): improve UX with spinner, task progress, and theme colors (#93, #95, #96)
- Show spinner immediately for custom commands before blocking work - Add elapsed time ticker for running subagent tasks - Show nested child tool status below running tasks - Add theme-based color coding (completed tools dimmed, tasks cyan) - Fix memory leaks in Bus subscription and task tracking maps
1 parent 51540b1 commit ce74e69

4 files changed

Lines changed: 179 additions & 29 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export async function handleCustomCommand(
229229
state: State,
230230
setOperationInProgress: (inProgress: boolean) => void,
231231
freezeBlock: () => void,
232+
spinner?: { stop: (success: boolean) => void },
232233
) {
233234
const options = {
234235
model: state.currentModel || undefined,
@@ -239,8 +240,10 @@ export async function handleCustomCommand(
239240
setOperationInProgress(true)
240241
try {
241242
const source = command(name, args, options)
243+
if (spinner) spinner.stop(true)
242244
await streamResponse(source, options)
243245
} catch (err) {
246+
if (spinner) spinner.stop(false)
244247
freezeBlock()
245248
const msg = err instanceof Error ? err.message : String(err)
246249
write(`\n${fg.red}Error: ${msg}${style.reset}\n\n`)

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

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { wrap } from "./wrap"
2323
import * as Panel from "./panel"
2424
import * as Commands from "./commands"
2525
import { setBlockFreeze } from "./select"
26+
import { Bus } from "../../bus"
27+
import { MessageV2 } from "../../session/message-v2"
2628
export { summarizeInput }
2729

2830
// UI Constants
@@ -49,6 +51,12 @@ const block = createLiveBlock()
4951
// Set block freeze function for select
5052
setBlockFreeze(() => block.freeze())
5153

54+
// Track task tool ID to child session ID mapping
55+
const taskToChildSession = new Map<string, string>()
56+
57+
// Store Bus subscription cleanup function
58+
let busUnsubscribe: (() => void) | undefined
59+
5260
async function main() {
5361
if (!process.stdin.isTTY) {
5462
console.error("oclite requires a TTY")
@@ -63,6 +71,7 @@ async function main() {
6371
})
6472

6573
function cleanup() {
74+
if (busUnsubscribe) busUnsubscribe()
6675
block.freeze()
6776
write(cursor.show)
6877
process.stdin.setRawMode(false)
@@ -80,6 +89,31 @@ async function main() {
8089
const setup = new Spinner("Setting up environment")
8190
setup.start()
8291

92+
// Subscribe to Bus events for child session tools
93+
busUnsubscribe = Bus.subscribe(MessageV2.Event.PartUpdated, (event) => {
94+
const part = event.properties.part
95+
if (part.type !== "tool") return
96+
97+
// Find which task this child session belongs to
98+
let taskId: string | null = null
99+
for (const [id, childSessionID] of taskToChildSession.entries()) {
100+
if (childSessionID === part.sessionID) {
101+
taskId = id
102+
break
103+
}
104+
}
105+
106+
if (!taskId) return
107+
108+
// Update task child tool display
109+
if (part.state.status === "running") {
110+
const arg = summarizeInput(part.tool, part.state.input)
111+
block.setTaskChildTool(taskId, part.tool, arg || "")
112+
} else if (part.state.status === "completed" || part.state.status === "error") {
113+
block.clearTaskChildTool(taskId)
114+
}
115+
})
116+
83117
try {
84118
await bootstrap(process.cwd(), async () => {
85119
await Promise.all([Provider.list(), Agent.list(), MCP.clients(), Command.list()])
@@ -305,21 +339,32 @@ async function handleCommand(cmd: string) {
305339
return
306340
}
307341

308-
const custom = await Command.get(name)
309-
if (custom) {
310-
const args = cmd.slice(1 + name.length).trim()
311-
await Commands.handleCustomCommand(
312-
custom.name,
313-
args,
314-
command,
315-
streamResponse,
316-
{ currentSessionID, currentModel, currentAgent },
317-
(inProgress: boolean) => {
318-
isOperationInProgress = inProgress
319-
},
320-
() => block.freeze(),
321-
)
322-
return
342+
const spinner = new Spinner(`Processing /${name}`)
343+
spinner.start()
344+
345+
try {
346+
const custom = await Command.get(name)
347+
if (custom) {
348+
const args = cmd.slice(1 + name.length).trim()
349+
await Commands.handleCustomCommand(
350+
custom.name,
351+
args,
352+
command,
353+
streamResponse,
354+
{ currentSessionID, currentModel, currentAgent },
355+
(inProgress: boolean) => {
356+
isOperationInProgress = inProgress
357+
},
358+
() => block.freeze(),
359+
spinner,
360+
)
361+
return
362+
}
363+
364+
spinner.stop(true)
365+
} catch (err) {
366+
spinner.stop(false)
367+
throw err
323368
}
324369

325370
write(`${fg.red}Unknown command: ${cmd}${style.reset}\n\n`)
@@ -413,6 +458,15 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
413458
dedupCount = 1
414459
block.toolStart(id, tool, summary)
415460
if (chunk.callID) idMap.set(chunk.callID, id)
461+
462+
// For task tools, track as a task
463+
if (tool === "task" && chunk.input && typeof chunk.input === "object") {
464+
const agent = (chunk.input as any).agent || ""
465+
const description = (chunk.input as any).description || ""
466+
if (agent && description) {
467+
block.taskStart(id, agent, description)
468+
}
469+
}
416470
}
417471
lastChunkType = "tool"
418472
}
@@ -466,7 +520,10 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
466520
const childSessionID = chunk.metadata.sessionId as string | undefined
467521
if (childSessionID && typeof childSessionID === "string") {
468522
Panel.addChild(childSessionID)
523+
taskToChildSession.set(id, childSessionID)
469524
}
525+
block.taskEnd(id)
526+
taskToChildSession.delete(id)
470527
}
471528
}
472529

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

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logUpdate from "log-update"
22
import { fg, style } from "./terminal"
3+
import { theme } from "./theme"
34

45
export interface Tool {
56
id: string
@@ -14,6 +15,9 @@ export interface Task {
1415
description: string
1516
elapsed: number
1617
status: "running" | "done"
18+
startTime: number
19+
childTool?: { name: string; summary: string }
20+
childSessionID?: string
1721
}
1822

1923
export interface Todo {
@@ -39,26 +43,50 @@ export function createLiveBlock() {
3943

4044
// Render active + recently completed tools
4145
for (const tool of tools.values()) {
42-
const icon =
43-
tool.status === "running"
44-
? `${fg.gray}◇${style.reset}`
45-
: tool.status === "done"
46-
? `${fg.green}✓${style.reset}`
47-
: `${fg.red}✗${style.reset}`
4846
const sep = tool.summary ? " " : ""
4947
const maxLen = Math.max(0, cols - tool.name.length - 6)
5048
const showEllipsis = tool.summary.length > maxLen
51-
const text = showEllipsis ? `${tool.summary.slice(0, maxLen)}…` : tool.summary
52-
const display = `${icon} ${fg.cyan}${tool.name}${style.reset}${fg.gray}${sep}${text}${style.reset}`
53-
lines.push(` ${display}`)
49+
const summary = showEllipsis ? `${tool.summary.slice(0, maxLen)}…` : tool.summary
50+
51+
const isTask = tool.name === "task"
52+
53+
if (tool.status === "done") {
54+
const colors = isTask ? theme.task.done : theme.tool.done
55+
const icon = `${colors.icon}✓${style.reset}`
56+
lines.push(` ${icon} ${colors.text}${tool.name}${sep}${summary}${style.reset}`)
57+
}
58+
59+
if (tool.status === "running") {
60+
const colors = isTask ? theme.task.running : theme.tool.running
61+
const icon = `${colors.icon}◇${style.reset}`
62+
lines.push(` ${icon} ${colors.text}${tool.name}${sep}${summary}${style.reset}`)
63+
}
64+
65+
if (tool.status === "error") {
66+
const colors = theme.tool.error
67+
const icon = `${colors.icon}✗${style.reset}`
68+
lines.push(` ${icon} ${colors.text}${tool.name}${sep}${summary}${style.reset}`)
69+
}
5470
}
5571

5672
// Render running tasks
5773
for (const task of tasks.values()) {
74+
const colors = task.status === "running" ? theme.task.running : theme.task.done
5875
const spinner =
59-
task.status === "running" ? `${fg.cyan}${frames[frame]}${style.reset}` : `${fg.green}✓${style.reset}`
60-
const time = task.elapsed > 0 ? ` ${fg.gray}(${task.elapsed}s)${style.reset}` : ""
61-
lines.push(` ${spinner} ${fg.gray}@${task.agent}:${style.reset} ${task.description}${time}`)
76+
task.status === "running" ? `${colors.icon}${frames[frame]}${style.reset}` : `${colors.icon}✓${style.reset}`
77+
const elapsed = task.status === "running" ? Math.floor((Date.now() - task.startTime) / 1000) : task.elapsed
78+
const time = elapsed > 0 ? ` ${fg.gray}(${elapsed}s)${style.reset}` : ""
79+
lines.push(` ${spinner} ${colors.text}@${task.agent}:${style.reset} ${task.description}${time}`)
80+
81+
// Render nested child tool if present
82+
if (task.status === "running" && task.childTool) {
83+
const childColors = theme.tool.running
84+
const childIcon = `${childColors.icon}◇${style.reset}`
85+
const maxLen = Math.max(0, cols - task.childTool.name.length - 15)
86+
const showEllipsis = task.childTool.summary.length > maxLen
87+
const summary = showEllipsis ? `${task.childTool.summary.slice(0, maxLen)}…` : task.childTool.summary
88+
lines.push(` └─ ${childIcon} ${childColors.text}${task.childTool.name} ${summary}${style.reset}`)
89+
}
6290
}
6391

6492
// Render todos if any
@@ -116,8 +144,8 @@ export function createLiveBlock() {
116144
render()
117145
},
118146

119-
taskStart(id: string, agent: string, description: string) {
120-
tasks.set(id, { id, agent, description, elapsed: 0, status: "running" })
147+
taskStart(id: string, agent: string, description: string, childSessionID?: string) {
148+
tasks.set(id, { id, agent, description, elapsed: 0, status: "running", startTime: Date.now(), childSessionID })
121149
if (!active) {
122150
active = true
123151
startAnimation()
@@ -128,7 +156,25 @@ export function createLiveBlock() {
128156
taskEnd(id: string) {
129157
const task = tasks.get(id)
130158
if (task) {
159+
task.elapsed = Math.floor((Date.now() - task.startTime) / 1000)
131160
task.status = "done"
161+
task.childTool = undefined
162+
render()
163+
}
164+
},
165+
166+
setTaskChildTool(taskId: string, childName: string, childSummary: string) {
167+
const task = tasks.get(taskId)
168+
if (task && task.status === "running") {
169+
task.childTool = { name: childName, summary: childSummary }
170+
render()
171+
}
172+
},
173+
174+
clearTaskChildTool(taskId: string) {
175+
const task = tasks.get(taskId)
176+
if (task) {
177+
task.childTool = undefined
132178
render()
133179
}
134180
},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { style, fg } from "./terminal"
2+
3+
export const theme = {
4+
// Tool display
5+
tool: {
6+
done: {
7+
icon: fg.green,
8+
text: style.dim,
9+
},
10+
running: {
11+
icon: fg.yellow,
12+
text: style.dim,
13+
},
14+
error: {
15+
icon: fg.red,
16+
text: fg.red,
17+
},
18+
},
19+
20+
// Task (subagent) display
21+
task: {
22+
done: {
23+
icon: fg.green,
24+
text: fg.cyan,
25+
},
26+
running: {
27+
icon: fg.yellow,
28+
text: fg.cyan,
29+
},
30+
},
31+
32+
// Text content
33+
prose: {
34+
text: "", // No color override, use terminal default
35+
},
36+
37+
// Status indicators
38+
status: {
39+
success: fg.green,
40+
error: fg.red,
41+
warning: fg.yellow,
42+
info: fg.cyan,
43+
},
44+
}

0 commit comments

Comments
 (0)