Skip to content
Draft
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
11 changes: 9 additions & 2 deletions packages/opencode/src/cli/cmd/serve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Server } from "../../server/server"
import { cmd } from "./cmd"
import { withNetworkOptions, resolveNetworkOptions } from "../network"
import path from "path"

export const ServeCommand = cmd({
command: "serve",
builder: (yargs) => withNetworkOptions(yargs),
builder: (yargs) =>
withNetworkOptions(yargs).option("cwd", {
describe: "working directory",
type: "string",
}),
describe: "starts a headless opencode server",
handler: async (args) => {
const opts = await resolveNetworkOptions(args)
const server = Server.listen(opts)
const base = process.env.PWD ?? process.cwd()
const cwd = args.cwd ? path.resolve(base, args.cwd) : undefined
const server = Server.listen({ ...opts, directory: cwd })
console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
await new Promise(() => {})
await server.stop()
Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/project/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const context = Context.create<Context>("instance")
const cache = new Map<string, Promise<Context>>()

export const Instance = {
async provide<R>(input: { directory: string; init?: () => Promise<any>; fn: () => R }): Promise<R> {
async provide<R>(input: { directory: string; root?: string; init?: () => Promise<any>; fn: () => R }): Promise<R> {
let existing = cache.get(input.directory)
if (!existing) {
Log.Default.info("creating instance", { directory: input.directory })
existing = iife(async () => {
const { project, sandbox } = await Project.fromDirectory(input.directory)
const { project, sandbox } = await Project.fromDirectory(input.directory, { root: input.root })
const ctx = {
directory: input.directory,
worktree: sandbox,
Expand Down
18 changes: 16 additions & 2 deletions packages/opencode/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { existsSync } from "fs"

export namespace Project {
const log = Log.create({ service: "project" })
type Root = {
root?: string
}
export const Info = z
.object({
id: z.string(),
Expand Down Expand Up @@ -44,8 +47,9 @@ export namespace Project {
Updated: BusEvent.define("project.updated", Info),
}

export async function fromDirectory(directory: string) {
log.info("fromDirectory", { directory })
export async function fromDirectory(directory: string, input?: Root) {
const root = input?.root ? path.resolve(input.root) : undefined
log.info("fromDirectory", { directory, root })

const { id, sandbox, worktree, vcs } = await iife(async () => {
const matches = Filesystem.up({ targets: [".git"], start: directory })
Expand Down Expand Up @@ -161,6 +165,16 @@ export namespace Project {
}
}

if (root) {
const id = `root-${Bun.hash.xxHash32(root)}`
return {
id,
worktree: root,
sandbox: root,
vcs: Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS),
}
}

return {
id: "global",
worktree: "/",
Expand Down
12 changes: 10 additions & 2 deletions packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export namespace Server {

let _url: URL | undefined
let _corsWhitelist: string[] = []
let _defaultDirectory: string | undefined
let _defaultRoot: string | undefined

export function url(): URL {
return _url ?? new URL("http://localhost:4096")
Expand Down Expand Up @@ -246,9 +248,13 @@ export namespace Server {
},
)
.use(async (c, next) => {
const directory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
const queryDirectory = c.req.query("directory")
const headerDirectory = c.req.header("x-opencode-directory")
const directory = queryDirectory || headerDirectory || _defaultDirectory || process.cwd()
const root = queryDirectory || headerDirectory ? directory : _defaultRoot
return Instance.provide({
directory,
root,
init: InstanceBootstrap,
async fn() {
return next()
Expand Down Expand Up @@ -2829,8 +2835,10 @@ export namespace Server {
return result
}

export function listen(opts: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
export function listen(opts: { port: number; hostname: string; mdns?: boolean; cors?: string[]; directory?: string }) {
_corsWhitelist = opts.cors ?? []
_defaultDirectory = opts.directory ?? process.cwd()
_defaultRoot = opts.directory ? _defaultDirectory : undefined

const args = {
hostname: opts.hostname,
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/js/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type ServerOptions = {
port?: number
signal?: AbortSignal
timeout?: number
cwd?: string
config?: Config
}

Expand All @@ -29,6 +30,7 @@ export async function createOpencodeServer(options?: ServerOptions) {
)

const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`]
if (options.cwd) args.push(`--cwd=${options.cwd}`)
if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`)

const proc = spawn(`opencode`, args, {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/js/src/v2/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type ServerOptions = {
port?: number
signal?: AbortSignal
timeout?: number
cwd?: string
config?: Config
}

Expand All @@ -29,6 +30,7 @@ export async function createOpencodeServer(options?: ServerOptions) {
)

const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`]
if (options.cwd) args.push(`--cwd=${options.cwd}`)
if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`)

const proc = spawn(`opencode`, args, {
Expand Down