Skip to content

Commit 9a31863

Browse files
committed
allow importing sdk from @opencode-ai/sdk/server and @opencode-ai/sdk/client
1 parent b1e584c commit 9a31863

4 files changed

Lines changed: 46 additions & 39 deletions

File tree

‎packages/sdk/js/package.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
".": {
1111
"development": "./src/index.ts",
1212
"import": "./dist/index.js"
13+
},
14+
"./client": {
15+
"development": "./src/client.ts",
16+
"import": "./dist/client.js"
17+
},
18+
"./server": {
19+
"development": "./src/server.ts",
20+
"import": "./dist/server.js"
1321
}
1422
},
1523
"files": [

‎packages/sdk/js/src/client.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createClient } from "./gen/client/client.js"
2+
import { type Config } from "./gen/client/types.js"
3+
import { OpencodeClient } from "./gen/sdk.gen.js"
4+
export * from "./gen/types.gen.js"
5+
export { type Config, OpencodeClient }
6+
7+
export function createOpencodeClient(config?: Config) {
8+
const client = createClient(config)
9+
return new OpencodeClient({ client })
10+
}

‎packages/sdk/js/src/index.ts‎

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,2 @@
1-
import { createClient } from "./gen/client/client.js"
2-
import { type Config } from "./gen/client/types.js"
3-
import { OpencodeClient } from "./gen/sdk.gen.js"
4-
export * from "./gen/types.gen.js"
5-
export {
6-
type Config,
7-
OpencodeClient
8-
}
9-
import { spawn } from "child_process"
10-
11-
export function createOpencodeClient(config?: Config) {
12-
const client = createClient(config)
13-
return new OpencodeClient({ client })
14-
}
15-
16-
export type ServerConfig = {
17-
host?: string
18-
port?: number
19-
}
20-
21-
export async function createOpencodeServer(config?: ServerConfig) {
22-
config = Object.assign(
23-
{
24-
host: "127.0.0.1",
25-
port: 4096,
26-
},
27-
config ?? {},
28-
)
29-
30-
const proc = spawn(`opencode`, [`serve`, `--host=${config.host}`, `--port=${config.port}`])
31-
const url = `http://${config.host}:${config.port}`
32-
33-
return {
34-
url,
35-
close() {
36-
proc.kill()
37-
},
38-
}
39-
}
1+
export * from "./client.js"
2+
export * from "./server.js"

‎packages/sdk/js/src/server.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { spawn } from "node:child_process"
2+
3+
export type ServerConfig = {
4+
host?: string
5+
port?: number
6+
}
7+
8+
export async function createOpencodeServer(config?: ServerConfig) {
9+
config = Object.assign(
10+
{
11+
host: "127.0.0.1",
12+
port: 4096,
13+
},
14+
config ?? {},
15+
)
16+
17+
const proc = spawn(`opencode`, [`serve`, `--host=${config.host}`, `--port=${config.port}`])
18+
const url = `http://${config.host}:${config.port}`
19+
20+
return {
21+
url,
22+
close() {
23+
proc.kill()
24+
},
25+
}
26+
}

0 commit comments

Comments
 (0)