fix(sdk): handle Windows opencode spawn and shutdown (#20772)
This commit is contained in:
parent
e89527c9f0
commit
7b8dc8065e
8 changed files with 103 additions and 35 deletions
31
packages/sdk/js/src/process.ts
Normal file
31
packages/sdk/js/src/process.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { type ChildProcess, spawnSync } from "node:child_process"
|
||||
|
||||
// Duplicated from `packages/opencode/src/util/process.ts` because the SDK cannot
|
||||
// import `opencode` without creating a cycle (`opencode` depends on `@opencode-ai/sdk`).
|
||||
export function stop(proc: ChildProcess) {
|
||||
if (proc.exitCode !== null || proc.signalCode !== null) return
|
||||
if (process.platform === "win32" && proc.pid) {
|
||||
const out = spawnSync("taskkill", ["/pid", String(proc.pid), "/T", "/F"], { windowsHide: true })
|
||||
if (!out.error && out.status === 0) return
|
||||
}
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
export function bindAbort(proc: ChildProcess, signal?: AbortSignal, onAbort?: () => void) {
|
||||
if (!signal) return () => {}
|
||||
const abort = () => {
|
||||
clear()
|
||||
stop(proc)
|
||||
onAbort?.()
|
||||
}
|
||||
const clear = () => {
|
||||
signal.removeEventListener("abort", abort)
|
||||
proc.off("exit", clear)
|
||||
proc.off("error", clear)
|
||||
}
|
||||
signal.addEventListener("abort", abort, { once: true })
|
||||
proc.on("exit", clear)
|
||||
proc.on("error", clear)
|
||||
if (signal.aborted) abort()
|
||||
return clear
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue