Browse Source

allow importing sdk from @opencode-ai/sdk/server and @opencode-ai/sdk/client

Dax Raad 6 months ago
parent
commit
9a3186317b

+ 8 - 0
packages/sdk/js/package.json

@@ -10,6 +10,14 @@
     ".": {
       "development": "./src/index.ts",
       "import": "./dist/index.js"
+    },
+    "./client": {
+      "development": "./src/client.ts",
+      "import": "./dist/client.js"
+    },
+    "./server": {
+      "development": "./src/server.ts",
+      "import": "./dist/server.js"
     }
   },
   "files": [

+ 10 - 0
packages/sdk/js/src/client.ts

@@ -0,0 +1,10 @@
+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"
+export { type Config, OpencodeClient }
+
+export function createOpencodeClient(config?: Config) {
+  const client = createClient(config)
+  return new OpencodeClient({ client })
+}

+ 2 - 39
packages/sdk/js/src/index.ts

@@ -1,39 +1,2 @@
-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"
-export {
-  type Config,
-  OpencodeClient
-}
-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()
-    },
-  }
-}
+export * from "./client.js"
+export * from "./server.js"

+ 26 - 0
packages/sdk/js/src/server.ts

@@ -0,0 +1,26 @@
+import { spawn } from "node:child_process"
+
+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()
+    },
+  }
+}