diff --git a/packages/opencode/src/cli/cmd/tui/attach.ts b/packages/opencode/src/cli/cmd/tui/attach.ts index 5d1a4ded206..17161bb2684 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,48 @@ 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", }), 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, + }, }) }, }) diff --git a/packages/web/src/content/docs/cli.mdx b/packages/web/src/content/docs/cli.mdx index e4e40ac7a4c..548e7e64816 100644 --- a/packages/web/src/content/docs/cli.mdx +++ b/packages/web/src/content/docs/cli.mdx @@ -79,6 +79,43 @@ opencode agent list --- +### 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 | + +--- + ### auth Command to manage credentials and login for providers.