diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index bd9d29b4deb..ee63e8bc90c 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -83,6 +83,10 @@ export const RunCommand = cmd({ type: "string", describe: "attach to a running opencode server (e.g., http://localhost:4096)", }) + .option("dir", { + type: "string", + describe: "directory to run in (only used with --attach)", + }) .option("port", { type: "number", describe: "port for the local server (defaults to random port if no value provided)", @@ -276,7 +280,10 @@ export const RunCommand = cmd({ } if (args.attach) { - const sdk = createOpencodeClient({ baseUrl: args.attach }) + const sdk = createOpencodeClient({ + baseUrl: args.attach, + directory: args.dir, + }) const sessionID = await (async () => { if (args.continue) { diff --git a/packages/opencode/test/cli/run-args.test.ts b/packages/opencode/test/cli/run-args.test.ts new file mode 100644 index 00000000000..c3b66d19d52 --- /dev/null +++ b/packages/opencode/test/cli/run-args.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, test } from "bun:test" +import { createOpencodeClient } from "@opencode-ai/sdk/v2" + +describe("cli.run --dir flag", () => { + test("SDK adds x-opencode-directory header when directory is provided", async () => { + let captured: Request | undefined + + const mockFetch = async (input: Request) => { + captured = input + return new Response(JSON.stringify({ data: { id: "test-session" } }), { + status: 200, + headers: { "content-type": "application/json" }, + }) + } + + const client = createOpencodeClient({ + baseUrl: "http://localhost:4096", + directory: "/custom/project/path", + fetch: mockFetch as typeof fetch, + }) + + await client.session.create() + + expect(captured).toBeDefined() + expect(captured!.headers.get("x-opencode-directory")).toBe("/custom/project/path") + }) + + test("SDK does not add x-opencode-directory header when directory is not provided", async () => { + let captured: Request | undefined + + const mockFetch = async (input: Request) => { + captured = input + return new Response(JSON.stringify({ data: { id: "test-session" } }), { + status: 200, + headers: { "content-type": "application/json" }, + }) + } + + const client = createOpencodeClient({ + baseUrl: "http://localhost:4096", + fetch: mockFetch as typeof fetch, + }) + + await client.session.create() + + expect(captured).toBeDefined() + expect(captured!.headers.get("x-opencode-directory")).toBeNull() + }) +})