server.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { spawn } from "node:child_process"
  2. import { type Config } from "./gen/types.gen.js"
  3. export type ServerOptions = {
  4. hostname?: string
  5. port?: number
  6. signal?: AbortSignal
  7. timeout?: number
  8. config?: Config
  9. }
  10. export type TuiOptions = {
  11. project?: string
  12. model?: string
  13. session?: string
  14. agent?: string
  15. signal?: AbortSignal
  16. config?: Config
  17. }
  18. export async function createOpencodeServer(options?: ServerOptions) {
  19. options = Object.assign(
  20. {
  21. hostname: "127.0.0.1",
  22. port: 4096,
  23. timeout: 5000,
  24. },
  25. options ?? {},
  26. )
  27. const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`]
  28. if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`)
  29. const proc = spawn(`opencode`, args, {
  30. signal: options.signal,
  31. env: {
  32. ...process.env,
  33. OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
  34. },
  35. })
  36. const url = await new Promise<string>((resolve, reject) => {
  37. const id = setTimeout(() => {
  38. reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`))
  39. }, options.timeout)
  40. let output = ""
  41. proc.stdout?.on("data", (chunk) => {
  42. output += chunk.toString()
  43. const lines = output.split("\n")
  44. for (const line of lines) {
  45. if (line.startsWith("opencode server listening")) {
  46. const match = line.match(/on\s+(https?:\/\/[^\s]+)/)
  47. if (!match) {
  48. throw new Error(`Failed to parse server url from output: ${line}`)
  49. }
  50. clearTimeout(id)
  51. resolve(match[1]!)
  52. return
  53. }
  54. }
  55. })
  56. proc.stderr?.on("data", (chunk) => {
  57. output += chunk.toString()
  58. })
  59. proc.on("exit", (code) => {
  60. clearTimeout(id)
  61. let msg = `Server exited with code ${code}`
  62. if (output.trim()) {
  63. msg += `\nServer output: ${output}`
  64. }
  65. reject(new Error(msg))
  66. })
  67. proc.on("error", (error) => {
  68. clearTimeout(id)
  69. reject(error)
  70. })
  71. if (options.signal) {
  72. options.signal.addEventListener("abort", () => {
  73. clearTimeout(id)
  74. reject(new Error("Aborted"))
  75. })
  76. }
  77. })
  78. return {
  79. url,
  80. close() {
  81. proc.kill()
  82. },
  83. }
  84. }
  85. export function createOpencodeTui(options?: TuiOptions) {
  86. const args = []
  87. if (options?.project) {
  88. args.push(`--project=${options.project}`)
  89. }
  90. if (options?.model) {
  91. args.push(`--model=${options.model}`)
  92. }
  93. if (options?.session) {
  94. args.push(`--session=${options.session}`)
  95. }
  96. if (options?.agent) {
  97. args.push(`--agent=${options.agent}`)
  98. }
  99. const proc = spawn(`opencode`, args, {
  100. signal: options?.signal,
  101. stdio: "inherit",
  102. env: {
  103. ...process.env,
  104. OPENCODE_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}),
  105. },
  106. })
  107. return {
  108. close() {
  109. proc.kill()
  110. },
  111. }
  112. }