Skip to content

Commit 369a296

Browse files
committed
fix(cli): clear screen on boot, show bootstrap spinner, fix orphaned tools (#57, #85, #86)
1 parent ab9fad5 commit 369a296

2 files changed

Lines changed: 113 additions & 92 deletions

File tree

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

Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,8 @@ import { Command } from "../../command"
1818
import { select } from "./select"
1919
import type { ChatChunk } from "./session"
2020
import { createLiveBlock } from "./liveblock"
21-
22-
export function summarizeInput(tool: string, input?: Record<string, unknown>): string {
23-
if (!input) return ""
24-
const str = (key: string) => String(input[key] || "")
25-
26-
if (tool === "bash") return str("command").slice(0, 60)
27-
if (tool === "read") return str("filePath").split("/").slice(-2).join("/")
28-
if (tool === "write" || tool === "edit") return str("filePath").split("/").slice(-2).join("/")
29-
if (tool === "rg" || tool === "grep") {
30-
const pattern = str("pattern").slice(0, 30)
31-
const include = str("include")
32-
return include ? `"${pattern}" in ${include}` : `"${pattern}"`
33-
}
34-
if (tool === "glob") return str("pattern").slice(0, 60)
35-
if (tool === "task") {
36-
const agent = str("subagent_type")
37-
const desc = str("description").slice(0, 40)
38-
return agent ? `@${agent}: ${desc}` : desc
39-
}
40-
if (tool === "todowrite" || tool === "todoread") return "todo list"
41-
42-
const first = Object.values(input).find((v) => typeof v === "string")
43-
return first ? String(first).slice(0, 60) : ""
44-
}
21+
import { summarizeInput } from "./summary"
22+
export { summarizeInput }
4523

4624
// UI State
4725
let tasksVisible = false
@@ -83,88 +61,104 @@ async function main() {
8361
process.exit(0)
8462
})
8563

64+
// Clear screen and show header
65+
write("\x1b[2J\x1b[H")
66+
write(`${fg.brightCyan}${style.bold}oclite${style.reset} ${fg.gray}v0.1.0${style.reset}\n`)
67+
68+
// Start bootstrap spinner (Spinner.start() hides cursor internally)
69+
const setup = new Spinner("Setting up environment")
70+
setup.start()
71+
8672
// Bootstrap with Instance context for current directory
87-
await bootstrap(process.cwd(), async () => {
88-
// Initialize default agent
89-
currentAgent = await Agent.defaultAgent()
90-
const agentList = await Agent.list()
73+
try {
74+
await bootstrap(process.cwd(), async () => {
75+
setup.stop(true)
76+
write(`${fg.green}✓${style.reset} Ready\n\n`)
9177

92-
// Setup
78+
// Initialize default agent
79+
currentAgent = await Agent.defaultAgent()
80+
const agentList = await Agent.list()
9381

94-
// Header
95-
write(`${fg.brightCyan}${style.bold}oclite${style.reset} ${fg.gray}v0.1.0${style.reset}\n`)
96-
write(`${fg.gray}Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${style.reset}\n\n`)
82+
// Show help hint after Ready
83+
write(`${fg.gray}Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${style.reset}\n\n`)
9784

98-
// Input setup
99-
const editor = new LineEditor()
100-
process.stdin.setRawMode(true)
101-
process.stdin.resume()
85+
// Input setup
86+
const editor = new LineEditor()
87+
process.stdin.setRawMode(true)
88+
process.stdin.resume()
10289

103-
// Render prompt
104-
editor.render(renderPrompt(currentAgent))
90+
// Render prompt
91+
editor.render(renderPrompt(currentAgent))
10592

106-
// Handle input
107-
process.stdin.on("data", async (data: Buffer) => {
108-
const key = parseKey(data)
93+
// Handle input
94+
process.stdin.on("data", async (data: Buffer) => {
95+
const key = parseKey(data)
10996

110-
// Ctrl+C to exit
111-
if (key.ctrl && key.name === "c") {
112-
cleanup()
113-
process.exit(0)
114-
}
97+
// Ctrl+C to exit
98+
if (key.ctrl && key.name === "c") {
99+
cleanup()
100+
process.exit(0)
101+
}
115102

116-
// Shift+Tab to cycle agents
117-
if (key.name === "shift_tab") {
118-
const available = agentList.filter((a) => a.mode !== "subagent" && !a.hidden)
119-
const index = available.findIndex((a) => a.name === currentAgent)
120-
const next = index === -1 ? 0 : (index + 1) % available.length
121-
currentAgent = available[next].name
122-
write(`\r${clear.line}`)
123-
editor.render(renderPrompt(currentAgent))
124-
return
125-
}
103+
// Shift+Tab to cycle agents
104+
if (key.name === "shift_tab") {
105+
const available = agentList.filter((a) => a.mode !== "subagent" && !a.hidden)
106+
const index = available.findIndex((a) => a.name === currentAgent)
107+
const next = index === -1 ? 0 : (index + 1) % available.length
108+
currentAgent = available[next].name
109+
write(`\r${clear.line}`)
110+
editor.render(renderPrompt(currentAgent))
111+
return
112+
}
126113

127-
// Escape to cancel ongoing operation
128-
if (key.name === "escape" && isOperationInProgress && currentSessionID) {
129-
block.freeze()
130-
SessionPrompt.cancel(currentSessionID)
131-
isOperationInProgress = false
132-
currentSessionID = null
133-
write("\n")
134-
editor.line = ""
135-
editor.cursor = 0
136-
editor.render(renderPrompt(currentAgent))
137-
return
138-
}
114+
// Escape to cancel ongoing operation
115+
if (key.name === "escape" && isOperationInProgress && currentSessionID) {
116+
block.freeze()
117+
SessionPrompt.cancel(currentSessionID)
118+
isOperationInProgress = false
119+
currentSessionID = null
120+
write("\n")
121+
editor.line = ""
122+
editor.cursor = 0
123+
editor.render(renderPrompt(currentAgent))
124+
return
125+
}
139126

140-
// Block further input during operation (except Escape and Ctrl+C which are handled above)
141-
if (isOperationInProgress) {
142-
return
143-
}
127+
// Block further input during operation (except Escape and Ctrl+C which are handled above)
128+
if (isOperationInProgress) {
129+
return
130+
}
144131

145-
// Ctrl+T to toggle task panel
146-
if (key.ctrl && key.name === "t") {
147-
tasksVisible = !tasksVisible
148-
editor.render(renderPrompt(currentAgent))
149-
}
132+
// Ctrl+T to toggle task panel
133+
if (key.ctrl && key.name === "t") {
134+
tasksVisible = !tasksVisible
135+
editor.render(renderPrompt(currentAgent))
136+
}
150137

151-
const result = editor.handle(key)
138+
const result = editor.handle(key)
152139

153-
if (result !== null) {
154-
write("\n")
140+
if (result !== null) {
141+
write("\n")
155142

156-
if (result.startsWith("/")) {
157-
await handleCommand(result)
158-
} else if (result.trim()) {
159-
await handleMessage(result)
160-
}
143+
if (result.startsWith("/")) {
144+
await handleCommand(result)
145+
} else if (result.trim()) {
146+
await handleMessage(result)
147+
}
161148

162-
editor.render(renderPrompt(currentAgent))
163-
} else {
164-
editor.render(renderPrompt(currentAgent))
165-
}
149+
editor.render(renderPrompt(currentAgent))
150+
} else {
151+
editor.render(renderPrompt(currentAgent))
152+
}
153+
})
166154
})
167-
})
155+
} catch (err) {
156+
setup.stop(false)
157+
write(cursor.show)
158+
const msg = err instanceof Error ? err.message : String(err)
159+
write(`\n${fg.red}Error: ${msg}${style.reset}\n`)
160+
process.exit(1)
161+
}
168162
}
169163

170164
async function handleCommand(cmd: string) {
@@ -360,6 +354,7 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
360354
let lastToolId = ""
361355
let lastToolKey = ""
362356
let dedupCount = 0
357+
const idMap = new Map<string, string>()
363358
const md = createMarkdownRenderer()
364359

365360
try {
@@ -394,12 +389,14 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
394389
dedupCount++
395390
const label = dedupCount > 1 ? `${summary} (×${dedupCount})` : summary
396391
block.toolStart(lastToolId, tool, label)
392+
if (chunk.callID) idMap.set(chunk.callID, lastToolId)
397393
} else {
398394
const id = chunk.callID || `${tool}-${++toolCounter}`
399395
lastToolId = id
400396
lastToolKey = key
401397
dedupCount = 1
402398
block.toolStart(id, tool, summary)
399+
if (chunk.callID) idMap.set(chunk.callID, id)
403400
}
404401
lastChunkType = "tool"
405402
}
@@ -410,11 +407,12 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
410407
const summary = arg || ""
411408
const key = `${tool}:${summary}`
412409

410+
const id = (chunk.callID && idMap.get(chunk.callID)) || chunk.callID || `${tool}-${toolCounter}`
411+
413412
if (key === lastToolKey) {
414413
const label = dedupCount > 1 ? `${summary} (×${dedupCount})` : summary
415-
block.toolEnd(lastToolId, tool, label)
414+
block.toolEnd(id, tool, label)
416415
} else {
417-
const id = chunk.callID || `${tool}-${toolCounter}`
418416
block.toolEnd(id, tool, summary)
419417
}
420418
lastChunkType = "tool"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function summarizeInput(tool: string, input?: Record<string, unknown>): string {
2+
if (!input) return ""
3+
const str = (key: string) => String(input[key] || "")
4+
5+
if (tool === "bash") return str("command").slice(0, 60)
6+
if (tool === "read") return str("filePath").split("/").slice(-2).join("/")
7+
if (tool === "write" || tool === "edit") return str("filePath").split("/").slice(-2).join("/")
8+
if (tool === "rg" || tool === "grep") {
9+
const pattern = str("pattern").slice(0, 30)
10+
const include = str("include")
11+
return include ? `"${pattern}" in ${include}` : `"${pattern}"`
12+
}
13+
if (tool === "glob") return str("pattern").slice(0, 60)
14+
if (tool === "task") {
15+
const agent = str("subagent_type")
16+
const desc = str("description").slice(0, 40)
17+
return agent ? `@${agent}: ${desc}` : desc
18+
}
19+
if (tool === "todowrite" || tool === "todoread") return "todo list"
20+
21+
const first = Object.values(input).find((v) => typeof v === "string")
22+
return first ? String(first).slice(0, 60) : ""
23+
}

0 commit comments

Comments
 (0)