Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions packages/sdk/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"devDependencies": {
"@hey-api/openapi-ts": "0.90.10",
"@tsconfig/node22": "catalog:",
"@types/cross-spawn": "6.0.6",
"@types/node": "catalog:",
"typescript": "catalog:",
"@typescript/native-preview": "catalog:"
"@typescript/native-preview": "catalog:",
"typescript": "catalog:"
},
"dependencies": {}
"dependencies": {
"cross-spawn": "^7.0.6"
}
}
49 changes: 49 additions & 0 deletions packages/sdk/js/src/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { type ChildProcess } from "node:child_process"
import { once } from "node:events"
import launch from "cross-spawn"

export namespace Process {
export type Child = ChildProcess & { exited: Promise<void> }

export type Options = {
signal?: AbortSignal
env?: NodeJS.ProcessEnv
stdio?: "inherit" | "pipe"
}

export function spawn(cmd: string, args: string[], opts: Options = {}): Child {
const proc = launch(cmd, args, {
signal: opts.signal,
env: opts.env,
stdio: opts.stdio,
windowsHide: process.platform === "win32",
})
const exited = once(proc, "exit").then(() => {})
void exited.catch(() => undefined)

const child = proc as Child
child.exited = exited
return child
Comment thread
Hona marked this conversation as resolved.
Outdated
}

export async function stop(proc: Child) {
if (proc.exitCode !== null || proc.signalCode !== null) return

if (process.platform === "win32" && proc.pid) {
const task = launch("taskkill", ["/pid", String(proc.pid), "/T", "/F"], {
windowsHide: true,
})
const code = await new Promise<number>((resolve) => {
task.once("exit", (code, signal) => resolve(code ?? (signal ? 1 : 0)))
task.once("error", () => resolve(1))
})
if (code === 0) {
await proc.exited
return
}
}

proc.kill()
Comment thread
Hona marked this conversation as resolved.
Outdated
await proc.exited
}
}
14 changes: 7 additions & 7 deletions packages/sdk/js/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process"
import { type Config } from "./gen/types.gen.js"
import { Process } from "./process.js"

export type ServerOptions = {
hostname?: string
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function createOpencodeServer(options?: ServerOptions) {
const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`]
if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`)

const proc = spawn(`opencode`, args, {
const proc = Process.spawn(`opencode`, args, {
signal: options.signal,
env: {
...process.env,
Expand Down Expand Up @@ -84,8 +84,8 @@ export async function createOpencodeServer(options?: ServerOptions) {

return {
url,
close() {
proc.kill()
async close() {
await Process.stop(proc)
},
}
Comment thread
Hona marked this conversation as resolved.
}
Expand All @@ -106,7 +106,7 @@ export function createOpencodeTui(options?: TuiOptions) {
args.push(`--agent=${options.agent}`)
}

const proc = spawn(`opencode`, args, {
const proc = Process.spawn(`opencode`, args, {
signal: options?.signal,
stdio: "inherit",
env: {
Expand All @@ -116,8 +116,8 @@ export function createOpencodeTui(options?: TuiOptions) {
})

return {
close() {
proc.kill()
async close() {
await Process.stop(proc)
},
}
}
14 changes: 7 additions & 7 deletions packages/sdk/js/src/v2/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process"
import { type Config } from "./gen/types.gen.js"
import { Process } from "../process.js"

export type ServerOptions = {
hostname?: string
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function createOpencodeServer(options?: ServerOptions) {
const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`]
if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`)

const proc = spawn(`opencode`, args, {
const proc = Process.spawn(`opencode`, args, {
signal: options.signal,
env: {
...process.env,
Expand Down Expand Up @@ -84,8 +84,8 @@ export async function createOpencodeServer(options?: ServerOptions) {

return {
url,
close() {
proc.kill()
async close() {
await Process.stop(proc)
},
}
Comment thread
Hona marked this conversation as resolved.
}
Expand All @@ -106,7 +106,7 @@ export function createOpencodeTui(options?: TuiOptions) {
args.push(`--agent=${options.agent}`)
}

const proc = spawn(`opencode`, args, {
const proc = Process.spawn(`opencode`, args, {
signal: options?.signal,
stdio: "inherit",
env: {
Expand All @@ -116,8 +116,8 @@ export function createOpencodeTui(options?: TuiOptions) {
})

return {
close() {
proc.kill()
async close() {
await Process.stop(proc)
},
}
}
Loading