Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/opencode/src/cli/cmd/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function tui(input: {
fetch?: typeof fetch
headers?: RequestInit["headers"]
events?: EventSource
stdin?: NodeJS.ReadStream
}) {
// promise to prevent immediate exit
// oxlint-disable-next-line no-async-promise-executor -- intentional: async executor used for sequential setup before resolve
Expand All @@ -189,7 +190,10 @@ export function tui(input: {
TuiAudio.dispose()
}

const renderer = await createCliRenderer(rendererConfig(input.config))
const renderer = await createCliRenderer({
...rendererConfig(input.config),
...(input.stdin ? { stdin: input.stdin } : {}),
})
// Prewarm palette before ThemeProvider mounts so `system` theme avoids a first-paint fallback flash.
void renderer.getPalette({ size: 16 }).catch(() => undefined)
const mode = (await renderer.waitForThemeMode(1000)) ?? "dark"
Expand Down
21 changes: 21 additions & 0 deletions packages/opencode/src/cli/cmd/tui/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
sanitizedProcessEnv,
} from "@opencode-ai/core/util/opencode-process"
import { validateSession } from "./validate-session"
import { INTERACTIVE_INPUT_ERROR, resolveInteractiveStdin } from "../run/runtime.stdin"

declare global {
const OPENCODE_WORKER_PATH: string
Expand Down Expand Up @@ -116,11 +117,29 @@ export const TuiThreadCommand = cmd({
// Keep ENABLE_PROCESSED_INPUT cleared even if other code flips it.
// (Important when running under `bun run` wrappers on Windows.)
const unguard = win32InstallCtrlCGuard()
let interactiveStdin: ReturnType<typeof resolveInteractiveStdin> | undefined
try {
// Must be the very first thing — disables CTRL_C_EVENT before any Worker
// spawn or async work so the OS cannot kill the process group.
win32DisableProcessedInput()

try {
interactiveStdin = resolveInteractiveStdin()
} catch (error) {
if (error instanceof Error && error.message === INTERACTIVE_INPUT_ERROR) {
UI.error(error.message)
process.exitCode = 1
return
}

throw error
}

if (!interactiveStdin) {
process.exitCode = 1
return
}

if (args.fork && !args.continue && !args.session) {
UI.error("--fork requires --continue or --session")
process.exitCode = 1
Expand Down Expand Up @@ -236,6 +255,7 @@ export const TuiThreadCommand = cmd({
const server = await client.call("snapshot", undefined)
return [tui, server]
},
stdin: interactiveStdin.stdin,
config,
directory: cwd,
fetch: transport.fetch,
Expand All @@ -253,6 +273,7 @@ export const TuiThreadCommand = cmd({
await stop()
}
} finally {
interactiveStdin?.cleanup?.()
unguard?.()
}
process.exit(0)
Expand Down
Loading