summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-10-05 07:00:29 -0400
committerDax Raad <[email protected]>2025-10-05 07:00:29 -0400
commitf3c2d1b6c23f50aae9e60a61f283ac9eaf5bccb6 (patch)
tree3ebcd281c51be0a6e214c69ac762e1183caf3439
parent71a7e8ef3668a94325ba5d641b84051d7cfe5163 (diff)
downloadopencode-f3c2d1b6c23f50aae9e60a61f283ac9eaf5bccb6.tar.gz
opencode-f3c2d1b6c23f50aae9e60a61f283ac9eaf5bccb6.zip
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
-rw-r--r--packages/sdk/js/src/index.ts18
-rw-r--r--packages/web/src/content/docs/sdk.mdx44
-rwxr-xr-xscript/publish.ts13
3 files changed, 44 insertions, 31 deletions
diff --git a/packages/sdk/js/src/index.ts b/packages/sdk/js/src/index.ts
index 03cf66634..3df2b29d6 100644
--- a/packages/sdk/js/src/index.ts
+++ b/packages/sdk/js/src/index.ts
@@ -1,2 +1,20 @@
export * from "./client.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,
+ }
+}
diff --git a/packages/web/src/content/docs/sdk.mdx b/packages/web/src/content/docs/sdk.mdx
index 1e65064f4..2b90bd9bc 100644
--- a/packages/web/src/content/docs/sdk.mdx
+++ b/packages/web/src/content/docs/sdk.mdx
@@ -25,17 +25,16 @@ npm install @opencode-ai/sdk
## Create client
-Create a client instance to connect to your server:
+Create an instance of opencode:
```javascript
-import { createOpencodeClient } from "@opencode-ai/sdk"
+import { createOpencode } from "@opencode-ai/sdk"
-const client = createOpencodeClient({
- baseUrl: "http://localhost:4096",
- responseStyle: "data",
-})
+const { client } = await createOpencode()
```
+This starts both a server and a client
+
#### Options
| Option | Type | Description | Default |
@@ -48,39 +47,36 @@ 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
-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,
+ config: {
+ model: "anthropic/claude-3-5-sonnet-20241022",
+ },
})
-console.log(`Server running at ${server.url}`)
+console.log(`Server running at ${opencode.server.url}`)
-server.close()
+opencode.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:
+## Client only
+
+If you aready have a running instance of opencode, you can create a client instance to connect to it:
```javascript
-import { createOpencodeServer } from "@opencode-ai/sdk"
+import { createOpencodeClient } from "@opencode-ai/sdk"
-const server = await createOpencodeServer({
- hostname: "127.0.0.1",
- port: 4096,
- config: {
- model: "anthropic/claude-3-5-sonnet-20241022",
- },
+const client = createOpencodeClient({
+ baseUrl: "http://localhost:4096",
})
-
-console.log(`Server running at ${server.url}`)
-
-server.close()
```
#### Options
diff --git a/script/publish.ts b/script/publish.ts
index 142981171..6d162d66f 100755
--- a/script/publish.ts
+++ b/script/publish.ts
@@ -1,12 +1,12 @@
#!/usr/bin/env bun
import { $ } from "bun"
-import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk"
+import { createOpencode } from "@opencode-ai/sdk"
if (process.versions.bun !== "1.2.21") {
throw new Error("This script requires [email protected]")
}
-let notes = []
+const notes = [] as string[]
console.log("=== publishing ===\n")
@@ -35,11 +35,10 @@ if (!snapshot) {
})
.then((data) => data.tag_name)
- const server = await createOpencodeServer()
- const client = createOpencodeClient({ baseUrl: server.url })
- const session = await client.session.create()
+ const opencode = await createOpencode()
+ const session = await opencode.client.session.create()
console.log("generating changelog since " + previous)
- const raw = await client.session
+ const raw = await opencode.client.session
.prompt({
path: {
id: session.data!.id,
@@ -85,7 +84,7 @@ if (!snapshot) {
}
}
console.log(notes)
- server.close()
+ opencode.server.close()
}
const pkgjsons = await Array.fromAsync(