summaryrefslogtreecommitdiffhomepage
path: root/packages/sdk/js/src/index.ts
blob: a8f0a105dc16d55b2637c2d9dd6117a03165e38b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { createClient } from "./gen/client/client.js"
import { type Config } from "./gen/client/types.js"
import { OpencodeClient } from "./gen/sdk.gen.js"
export * from "./gen/types.gen.js"
import { spawn } from "child_process"

export function createOpencodeClient(config?: Config) {
  const client = createClient(config)
  return new OpencodeClient({ client })
}

export type ServerConfig = {
  host?: string
  port?: number
}

export async function createOpencodeServer(config?: ServerConfig) {
  config = Object.assign(
    {
      host: "127.0.0.1",
      port: 4096,
    },
    config ?? {},
  )

  const proc = spawn(`opencode`, [`serve`, `--host=${config.host}`, `--port=${config.port}`])
  const url = `http://${config.host}:${config.port}`

  return {
    url,
    close() {
      proc.kill()
    },
  }
}