Skip to content

Commit 4dfb497

Browse files
committed
feat(opencode): lightweight TUI (oclite) and subagent directory fix (#51)
* fix(opencode): fix task auto-notification and remove any types - Fix critical bug: task completion events now route to parent session (changed session_id: session.id to session_id: ctx.sessionID in task.ts) - Remove 'any' type annotations from catch blocks - Replace e?.name checks with proper type guards for AbortError - Convert Global paths to getters for test isolation - Fix log directory initialization order - Clean up Tauri dependencies from lock file * fix(project): use fs.stat instead of Bun.file for directory detection Bun.file().stat() throws "Directories cannot be read like files" when called on a directory. The .git path is a directory in normal repos (only a file in worktrees). Using Node.js fs.stat() works correctly with both. Also fixed stat.isFile to stat.isFile() (method call vs property). * feat(opencode): add lightweight TUI (oclite) with raw ANSI terminal (#27) * fix(tool): inherit parent session directory in subagents (#50) * fix(provider): add comprehensive null checks in auth.ts (#52) * feat(cli): add task icons with color coding (#21) * feat(cli): add metrics formatting utilities (#20) * fix(project): handle empty commits and worktree path resolution
1 parent 638dae9 commit 4dfb497

23 files changed

Lines changed: 1676 additions & 519 deletions

File tree

‎bun.lock‎

Lines changed: 526 additions & 474 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"typecheck": "tsgo --noEmit",
1010
"test": "bun test",
1111
"build": "bun run script/build.ts",
12+
"build:lite": "bun run script/build-lite.ts",
1213
"dev": "bun run --conditions=browser ./src/index.ts",
1314
"random": "echo 'Random script updated at $(date)' && echo 'Change queued successfully' && echo 'Another change made' && echo 'Yet another change' && echo 'One more change' && echo 'Final change' && echo 'Another final change' && echo 'Yet another final change'",
1415
"clean": "echo 'Cleaning up...' && rm -rf node_modules dist",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bun
2+
import path from "path"
3+
import fs from "fs"
4+
import { $ } from "bun"
5+
6+
const ROOT = path.resolve(import.meta.dirname, "..")
7+
const DIST = path.join(ROOT, "dist", "oclite-darwin-arm64")
8+
const BIN = path.join(DIST, "bin")
9+
const HOME_BIN = path.join(process.env.HOME!, "bin")
10+
11+
async function build() {
12+
console.log("Building oclite...")
13+
14+
// Clean
15+
await fs.promises.rm(DIST, { recursive: true, force: true })
16+
await fs.promises.mkdir(BIN, { recursive: true })
17+
18+
// Build
19+
const result = await Bun.build({
20+
entrypoints: [path.join(ROOT, "src/cli/lite/index.ts")],
21+
outdir: BIN,
22+
target: "bun",
23+
minify: true,
24+
sourcemap: "none",
25+
naming: "oclite",
26+
})
27+
28+
if (!result.success) {
29+
console.error("Build failed:")
30+
for (const log of result.logs) {
31+
console.error(log)
32+
}
33+
process.exit(1)
34+
}
35+
36+
// Make executable
37+
await $`chmod +x ${path.join(BIN, "oclite")}`
38+
39+
// Copy to ~/bin
40+
await fs.promises.mkdir(HOME_BIN, { recursive: true })
41+
await fs.promises.copyFile(path.join(BIN, "oclite"), path.join(HOME_BIN, "oclite"))
42+
43+
// Sign for macOS
44+
await $`codesign --force --deep --sign - ${path.join(HOME_BIN, "oclite")}`.quiet().nothrow()
45+
await $`xattr -cr ${path.join(HOME_BIN, "oclite")}`.quiet().nothrow()
46+
47+
// Get size
48+
const stat = await fs.promises.stat(path.join(HOME_BIN, "oclite"))
49+
const sizeMB = (stat.size / 1024 / 1024).toFixed(2)
50+
51+
console.log(`✓ Built oclite (${sizeMB} MB)`)
52+
console.log(`✓ Installed to ~/bin/oclite`)
53+
console.log("\nRun with: ~/bin/oclite")
54+
}
55+
56+
build().catch((err) => {
57+
console.error(err)
58+
process.exit(1)
59+
})

‎packages/opencode/src/agent/agent.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,11 @@ export namespace Agent {
329329
for await (const part of result.fullStream) {
330330
if (part.type === "error") throw part.error
331331
}
332-
} catch (e: any) {
333-
if (e?.name === "AbortError" || (e instanceof DOMException && e.name === "AbortError")) {
332+
} catch (e) {
333+
if (typeof e === "object" && e !== null && "name" in e && e.name === "AbortError") {
334+
throw e
335+
}
336+
if (e instanceof DOMException && e.name === "AbortError") {
334337
throw e
335338
}
336339
throw e
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Task state icons and color system for terminal UI.
3+
* Provides visual feedback for task status, agent roles, and UI elements.
4+
*/
5+
6+
export namespace Icons {
7+
/**
8+
* ANSI color codes for terminal output
9+
*/
10+
export const colors = {
11+
green: "\x1b[32m",
12+
yellow: "\x1b[33m",
13+
cyan: "\x1b[36m",
14+
dim: "\x1b[2m",
15+
red: "\x1b[31m",
16+
magenta: "\x1b[35m",
17+
reset: "\x1b[0m",
18+
}
19+
20+
/**
21+
* Task status icons with their default colors
22+
*/
23+
export const icons = {
24+
taskComplete: "✓",
25+
taskPending: "□",
26+
taskRunning: "●",
27+
taskProgress: "◇",
28+
agentRole: "🟪",
29+
}
30+
31+
/**
32+
* Status colors lookup table (module-level for performance)
33+
*/
34+
const statusColors: Record<string, string> = {
35+
completed: colors.green,
36+
pending: colors.dim,
37+
running: colors.green,
38+
progress: colors.yellow,
39+
}
40+
41+
/**
42+
* Status icons lookup table (module-level for performance)
43+
*/
44+
const statusIcons: Record<string, string> = {
45+
completed: icons.taskComplete,
46+
pending: icons.taskPending,
47+
running: icons.taskRunning,
48+
progress: icons.taskProgress,
49+
}
50+
51+
/**
52+
* Returns a colored task status icon based on the given status
53+
* @param status - The task status: 'completed', 'pending', 'running', or 'progress'
54+
* @returns The colored icon as a string, or pending icon as safe fallback
55+
*/
56+
export function taskIcon(status: "completed" | "pending" | "running" | "progress"): string {
57+
const color = statusColors[status]
58+
const icon = statusIcons[status]
59+
60+
if (!color || !icon) return `${colors.dim}${icons.taskPending}${colors.reset}`
61+
return `${color}${icon}${colors.reset}`
62+
}
63+
64+
/**
65+
* Returns a colored agent name badge in cyan
66+
* @param name - The agent name
67+
* @returns The colored agent name badge, or empty string if name is empty
68+
*/
69+
export function agentBadge(name: string): string {
70+
if (!name) return ""
71+
return `${colors.cyan}${name}${colors.reset}`
72+
}
73+
74+
/**
75+
* Returns a colored role badge with a purple icon prefix
76+
* @param role - The role name
77+
* @returns The colored role badge with icon, or empty string if role is empty
78+
*/
79+
export function roleBadge(role: string): string {
80+
if (!role) return ""
81+
return `${colors.magenta}${icons.agentRole}${colors.reset} ${role}`
82+
}
83+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env bun
2+
import { cursor, clear, fg, style, write, screen } from "./terminal"
3+
import { parseKey, LineEditor } from "./input"
4+
import { Spinner } from "./spinner"
5+
import { chat } from "./session"
6+
7+
const PROMPT = `${fg.cyan}>${style.reset} `
8+
9+
async function main() {
10+
// Check TTY
11+
if (!process.stdin.isTTY) {
12+
console.error("oclite requires a TTY")
13+
process.exit(1)
14+
}
15+
16+
// Setup
17+
write(screen.alt)
18+
write(clear.screen)
19+
write(cursor.home)
20+
21+
// Header
22+
write(`${fg.brightCyan}${style.bold}oclite${style.reset} ${fg.gray}v0.1.0${style.reset}\n`)
23+
write(`${fg.gray}Type /help for commands, Ctrl+C to exit${style.reset}\n\n`)
24+
25+
// Input setup
26+
const editor = new LineEditor()
27+
process.stdin.setRawMode(true)
28+
process.stdin.resume()
29+
30+
// Render prompt
31+
editor.render(PROMPT)
32+
33+
// Handle input
34+
process.stdin.on("data", async (data: Buffer) => {
35+
const key = parseKey(data)
36+
37+
// Ctrl+C to exit
38+
if (key.ctrl && key.name === "c") {
39+
cleanup()
40+
process.exit(0)
41+
}
42+
43+
const result = editor.handle(key)
44+
45+
if (result !== null) {
46+
write("\n")
47+
48+
if (result.startsWith("/")) {
49+
handleCommand(result)
50+
} else if (result.trim()) {
51+
await handleMessage(result)
52+
}
53+
54+
editor.render(PROMPT)
55+
} else {
56+
editor.render(PROMPT)
57+
}
58+
})
59+
60+
// Cleanup on exit
61+
function cleanup() {
62+
write(cursor.show)
63+
write(screen.main)
64+
process.stdin.setRawMode(false)
65+
}
66+
67+
process.on("exit", cleanup)
68+
process.on("SIGINT", () => {
69+
cleanup()
70+
process.exit(0)
71+
})
72+
}
73+
74+
function handleCommand(cmd: string) {
75+
const command = cmd.slice(1).toLowerCase().trim()
76+
77+
if (command === "help") {
78+
write(`${fg.yellow}Commands:${style.reset}\n`)
79+
write(` /help - Show this help\n`)
80+
write(` /clear - Clear screen\n`)
81+
write(` /quit - Exit oclite\n`)
82+
write("\n")
83+
return
84+
}
85+
86+
if (command === "clear") {
87+
write(clear.screen)
88+
write(cursor.home)
89+
write(`${fg.brightCyan}${style.bold}oclite${style.reset} ${fg.gray}v0.1.0${style.reset}\n\n`)
90+
return
91+
}
92+
93+
if (command === "quit" || command === "exit") {
94+
process.exit(0)
95+
}
96+
97+
write(`${fg.red}Unknown command: ${cmd}${style.reset}\n\n`)
98+
}
99+
100+
async function handleMessage(message: string) {
101+
const spinner = new Spinner("Thinking")
102+
spinner.start()
103+
104+
let first = true
105+
try {
106+
for await (const chunk of chat(message)) {
107+
if (first) {
108+
spinner.stop(true)
109+
first = false
110+
}
111+
112+
if (chunk.type === "text" && chunk.content) {
113+
write(chunk.content)
114+
}
115+
116+
if (chunk.type === "tool_start" && chunk.tool) {
117+
write(`\n${fg.yellow}▶ ${chunk.tool}${style.reset}\n`)
118+
}
119+
120+
if (chunk.type === "tool_end" && chunk.tool) {
121+
write(`${fg.green}✓ ${chunk.tool}${style.reset}\n`)
122+
}
123+
124+
if (chunk.type === "error" && chunk.content) {
125+
write(`\n${fg.red}Error: ${chunk.content}${style.reset}\n`)
126+
}
127+
}
128+
} catch (err) {
129+
if (first) spinner.stop(false)
130+
const msg = err instanceof Error ? err.message : String(err)
131+
write(`\n${fg.red}Error: ${msg}${style.reset}\n`)
132+
}
133+
134+
write("\n")
135+
}
136+
137+
main().catch(console.error)

0 commit comments

Comments
 (0)