mirror of
https://github.com/sst/opencode.git
synced 2025-12-23 10:11:41 +00:00
allow importing sdk from @opencode-ai/sdk/server and @opencode-ai/sdk/client
This commit is contained in:
parent
b1e584ca1d
commit
9a3186317b
4 changed files with 46 additions and 39 deletions
|
|
@ -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
packages/sdk/js/src/client.ts
Normal file
10
packages/sdk/js/src/client.ts
Normal file
|
|
@ -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 })
|
||||
}
|
||||
|
|
@ -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
packages/sdk/js/src/server.ts
Normal file
26
packages/sdk/js/src/server.ts
Normal file
|
|
@ -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()
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue