improve startup speed

This commit is contained in:
Dax Raad
2025-11-08 20:17:46 -05:00
parent 7af3380455
commit 4bb7ea9127
15 changed files with 203 additions and 192 deletions

View File

@@ -2,10 +2,9 @@ import { cmd } from "@/cli/cmd/cmd"
import { tui } from "./app"
import { Rpc } from "@/util/rpc"
import { type rpc } from "./worker"
import { Session } from "@/session"
import { bootstrap } from "@/cli/bootstrap"
import path from "path"
import { UI } from "@/cli/ui"
import { iife } from "@/util/iife"
declare global {
const OPENCODE_WORKER_PATH: string
@@ -32,8 +31,8 @@ export const TuiThreadCommand = cmd({
})
.option("session", {
alias: ["s"],
describe: "session id to continue",
type: "string",
describe: "session id to continue",
})
.option("prompt", {
alias: ["p"],
@@ -55,12 +54,6 @@ export const TuiThreadCommand = cmd({
default: "127.0.0.1",
}),
handler: async (args) => {
const prompt = await (async () => {
const piped = !process.stdin.isTTY ? await Bun.stdin.text() : undefined
if (!args.prompt) return piped
return piped ? piped + "\n" + args.prompt : args.prompt
})()
// Resolve relative paths against PWD to preserve behavior when using --cwd flag
const baseCwd = process.env.PWD ?? process.cwd()
const cwd = args.project ? path.resolve(baseCwd, args.project) : process.cwd()
@@ -76,56 +69,41 @@ export const TuiThreadCommand = cmd({
return
}
await bootstrap(cwd, async () => {
const sessionID = await (async () => {
if (args.continue) {
const it = Session.list()
try {
for await (const s of it) {
if (s.parentID === undefined) {
return s.id
}
}
return
} finally {
await it.return()
}
}
if (args.session) {
return args.session
}
return undefined
})()
const worker = new Worker(workerPath, {
env: Object.fromEntries(
Object.entries(process.env).filter(
(entry): entry is [string, string] => entry[1] !== undefined,
),
const worker = new Worker(workerPath, {
env: Object.fromEntries(
Object.entries(process.env).filter(
(entry): entry is [string, string] => entry[1] !== undefined,
),
})
worker.onerror = console.error
const client = Rpc.client<typeof rpc>(worker)
process.on("uncaughtException", (e) => {
console.error(e)
})
process.on("unhandledRejection", (e) => {
console.error(e)
})
const server = await client.call("server", {
port: args.port,
hostname: args.hostname,
})
await tui({
url: server.url,
sessionID,
model: args.model,
),
})
worker.onerror = console.error
const client = Rpc.client<typeof rpc>(worker)
process.on("uncaughtException", (e) => {
console.error(e)
})
process.on("unhandledRejection", (e) => {
console.error(e)
})
const server = await client.call("server", {
port: args.port,
hostname: args.hostname,
})
const prompt = await iife(async () => {
const piped = !process.stdin.isTTY ? await Bun.stdin.text() : undefined
if (!args.prompt) return piped
return piped ? piped + "\n" + args.prompt : args.prompt
})
await tui({
url: server.url,
args: {
continue: args.continue,
sessionID: args.session,
agent: args.agent,
prompt,
onExit: async () => {
await client.call("shutdown", undefined)
},
})
},
onExit: async () => {
await client.call("shutdown", undefined)
},
})
},
})