diff --git a/packages/opencode/src/cli/cmd/tui/attach.ts b/packages/opencode/src/cli/cmd/tui/attach.ts index 5d1a4ded2..1285fadb1 100644 --- a/packages/opencode/src/cli/cmd/tui/attach.ts +++ b/packages/opencode/src/cli/cmd/tui/attach.ts @@ -1,5 +1,6 @@ import { cmd } from "../cmd" import { tui } from "./app" +import { iife } from "@/util/iife" export const AttachCommand = cmd({ command: "attach ", @@ -15,16 +16,58 @@ export const AttachCommand = cmd({ type: "string", description: "directory to run in", }) + .option("model", { + type: "string", + alias: ["m"], + describe: "model to use in the format of provider/model", + }) + .option("continue", { + alias: ["c"], + describe: "continue the last session", + type: "boolean", + }) .option("session", { alias: ["s"], type: "string", describe: "session id to continue", + }) + .option("prompt", { + alias: ["p"], + type: "string", + describe: "prompt to use", + }) + .option("agent", { + type: "string", + describe: "agent to use", + }) + .option("title", { + type: "string", + describe: "title for the session (uses truncated prompt if no value provided)", + }) + .option("command", { + type: "string", + describe: "the command to run, use prompt for args", }), handler: async (args) => { if (args.dir) process.chdir(args.dir) + + 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: args.url, - args: { sessionID: args.session }, + args: { + continue: args.continue, + sessionID: args.session, + agent: args.agent, + model: args.model, + prompt, + title: args.title, + command: args.command, + }, }) }, }) diff --git a/packages/opencode/src/cli/cmd/tui/context/args.tsx b/packages/opencode/src/cli/cmd/tui/context/args.tsx index ffd43009a..afc295368 100644 --- a/packages/opencode/src/cli/cmd/tui/context/args.tsx +++ b/packages/opencode/src/cli/cmd/tui/context/args.tsx @@ -6,6 +6,8 @@ export interface Args { prompt?: string continue?: boolean sessionID?: string + title?: string + command?: string } export const { use: useArgs, provider: ArgsProvider } = createSimpleContext({ diff --git a/packages/opencode/src/cli/cmd/tui/routes/home.tsx b/packages/opencode/src/cli/cmd/tui/routes/home.tsx index ecdf93c43..a53ec8e0e 100644 --- a/packages/opencode/src/cli/cmd/tui/routes/home.tsx +++ b/packages/opencode/src/cli/cmd/tui/routes/home.tsx @@ -51,7 +51,9 @@ export function Home() { const args = useArgs() onMount(() => { if (once) return - if (route.initialPrompt) { + if (args.command) { + prompt.set({ input: `/${args.command} ${args.prompt || ""}`.trim(), parts: [] }) + } else if (route.initialPrompt) { prompt.set(route.initialPrompt) once = true } else if (args.prompt) { diff --git a/packages/web/src/content/docs/cli.mdx b/packages/web/src/content/docs/cli.mdx index c13712d10..0bbc89ad5 100644 --- a/packages/web/src/content/docs/cli.mdx +++ b/packages/web/src/content/docs/cli.mdx @@ -69,6 +69,45 @@ This command will guide you through creating a new agent with a custom system pr --- +### attach + +Attach to a running opencode server with a full terminal interface. + +```bash +opencode attach +``` + +This command connects to an existing `opencode serve` or `opencode spawn` instance and provides the full TUI experience, allowing you to interact with the server as if you started opencode directly. + +```bash +# Start a headless server in one terminal +opencode serve + +# In another terminal, attach with the TUI +opencode attach http://localhost:4096 +``` + +This is useful when you want to: + +- Connect to a long-running opencode server +- Avoid MCP server cold boot times +- Share a server instance across multiple sessions + +#### Flags + +| Flag | Short | Description | +| ------------ | ----- | ------------------------------------------------------------------ | +| `--dir` | | Directory to run in | +| `--model` | `-m` | Model to use in the form of provider/model | +| `--continue` | `-c` | Continue the last session | +| `--session` | `-s` | Session ID to continue | +| `--prompt` | `-p` | Prompt to use | +| `--agent` | | Agent to use | +| `--title` | | Title for the session (uses truncated prompt if no value provided) | +| `--command` | | The command to run, use prompt for args | + +--- + ### auth Command to manage credentials and login for providers.