mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-19 16:54:22 +01:00
sdk: simplify getting started with single createOpencode function
Makes it easier for developers to get started by providing a single function that creates both server and client, removing the need to manually coordinate separate server and client creation
This commit is contained in:
@@ -1,2 +1,20 @@
|
|||||||
export * from "./client.js"
|
export * from "./client.js"
|
||||||
export * from "./server.js"
|
export * from "./server.js"
|
||||||
|
|
||||||
|
import { createOpencodeClient } from "./client.js"
|
||||||
|
import { createOpencodeServer, ServerOptions } from "./server.js"
|
||||||
|
|
||||||
|
export async function createOpencode(options?: ServerOptions) {
|
||||||
|
const server = await createOpencodeServer({
|
||||||
|
...options,
|
||||||
|
})
|
||||||
|
|
||||||
|
const client = createOpencodeClient({
|
||||||
|
baseUrl: server.url,
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
client,
|
||||||
|
server,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,17 +25,16 @@ npm install @opencode-ai/sdk
|
|||||||
|
|
||||||
## Create client
|
## Create client
|
||||||
|
|
||||||
Create a client instance to connect to your server:
|
Create an instance of opencode:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import { createOpencodeClient } from "@opencode-ai/sdk"
|
import { createOpencode } from "@opencode-ai/sdk"
|
||||||
|
|
||||||
const client = createOpencodeClient({
|
const { client } = await createOpencode()
|
||||||
baseUrl: "http://localhost:4096",
|
|
||||||
responseStyle: "data",
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This starts both a server and a client
|
||||||
|
|
||||||
#### Options
|
#### Options
|
||||||
|
|
||||||
| Option | Type | Description | Default |
|
| Option | Type | Description | Default |
|
||||||
@@ -48,29 +47,14 @@ const client = createOpencodeClient({
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Start server
|
## Config
|
||||||
|
|
||||||
You can also programmatically start an opencode server:
|
You can pass a configuration object to customize behavior. The instance still picks up your `opencode.json`, but you can override or add configuration inline:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import { createOpencodeServer } from "@opencode-ai/sdk"
|
import { createOpencode } from "@opencode-ai/sdk"
|
||||||
|
|
||||||
const server = await createOpencodeServer({
|
const opencode = await createOpencode({
|
||||||
hostname: "127.0.0.1",
|
|
||||||
port: 4096,
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log(`Server running at ${server.url}`)
|
|
||||||
|
|
||||||
server.close()
|
|
||||||
```
|
|
||||||
|
|
||||||
You can pass a configuration object to customize server behavior. The server still picks up your `opencode.json`, but you can override or add configuration inline:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import { createOpencodeServer } from "@opencode-ai/sdk"
|
|
||||||
|
|
||||||
const server = await createOpencodeServer({
|
|
||||||
hostname: "127.0.0.1",
|
hostname: "127.0.0.1",
|
||||||
port: 4096,
|
port: 4096,
|
||||||
config: {
|
config: {
|
||||||
@@ -78,9 +62,21 @@ const server = await createOpencodeServer({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(`Server running at ${server.url}`)
|
console.log(`Server running at ${opencode.server.url}`)
|
||||||
|
|
||||||
server.close()
|
opencode.server.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Client only
|
||||||
|
|
||||||
|
If you aready have a running instance of opencode, you can create a client instance to connect to it:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import { createOpencodeClient } from "@opencode-ai/sdk"
|
||||||
|
|
||||||
|
const client = createOpencodeClient({
|
||||||
|
baseUrl: "http://localhost:4096",
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Options
|
#### Options
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
import { $ } from "bun"
|
import { $ } from "bun"
|
||||||
import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk"
|
import { createOpencode } from "@opencode-ai/sdk"
|
||||||
if (process.versions.bun !== "1.2.21") {
|
if (process.versions.bun !== "1.2.21") {
|
||||||
throw new Error("This script requires bun@1.2.21")
|
throw new Error("This script requires bun@1.2.21")
|
||||||
}
|
}
|
||||||
|
|
||||||
let notes = []
|
const notes = [] as string[]
|
||||||
|
|
||||||
console.log("=== publishing ===\n")
|
console.log("=== publishing ===\n")
|
||||||
|
|
||||||
@@ -35,11 +35,10 @@ if (!snapshot) {
|
|||||||
})
|
})
|
||||||
.then((data) => data.tag_name)
|
.then((data) => data.tag_name)
|
||||||
|
|
||||||
const server = await createOpencodeServer()
|
const opencode = await createOpencode()
|
||||||
const client = createOpencodeClient({ baseUrl: server.url })
|
const session = await opencode.client.session.create()
|
||||||
const session = await client.session.create()
|
|
||||||
console.log("generating changelog since " + previous)
|
console.log("generating changelog since " + previous)
|
||||||
const raw = await client.session
|
const raw = await opencode.client.session
|
||||||
.prompt({
|
.prompt({
|
||||||
path: {
|
path: {
|
||||||
id: session.data!.id,
|
id: session.data!.id,
|
||||||
@@ -85,7 +84,7 @@ if (!snapshot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(notes)
|
console.log(notes)
|
||||||
server.close()
|
opencode.server.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
const pkgjsons = await Array.fromAsync(
|
const pkgjsons = await Array.fromAsync(
|
||||||
|
|||||||
Reference in New Issue
Block a user