跳到正文

SDK

在你自己的脚本、CI 任务和工具中驱动 OpenCode。SDK 在你的进程内运行 OpenCode,使用你的配置和模型。带类型的客户端可以创建会话、发送提示词并流式传输每个事件。

功能

用代码修复 CI

一段简短的脚本让 OpenCode 修复失败的测试,然后重新运行。

跟踪每一步

流式传输事件,实时查看 agent 调用的每个工具。

构建自己的工具

只需几行代码,就能把 OpenCode 接入机器人、仪表盘和工作流。

在你的进程内

在你的进程内运行 OpenCode,使用你的配置和模型。

  • TypeScript 客户端带类型的请求和事件,值与 @opencode/client 相同。
  • 用代码创建会话在任意脚本中创建会话并发送提示词。
  • 任意模型使用你 OpenCode 配置中已有的模型。
  • npm 或 bun使用 npm i @opencode/sdk 或 bun add @opencode/sdk 安装。
  • 进程内宿主在内存中运行 OpenCode,无需经过网络。
  • 你的插件创建进程内宿主时传入你自己的插件。
  • Promise 或 EffectPromise API 和 Effect API,返回相同的值。
  • Cloudflare Workers在 Cloudflare Workers 的 Durable Object 中运行 OpenCode。

  • npmnpm i @opencode/sdk
  • bunbun add @opencode/sdk
阅读 SDK 文档

更新日志

  1. v2.4.5

    Run an Automation Now

    Click Run now next to Pause on an active timer automation in Desktop to run it right away. It counts as a normal run, and its schedule doesn't move.#68034

    查看 v2.4.5 的全部亮点

    改进

    • SDKServer plugins can list, create, read, answer and cancel session forms through session.form.#67863
    • SDKExperimental: run a timer automation now with client.experimental.automation.run, and filter the blob list by mime type with mime.#68034#68315
    • SDKExperimental push callbacks accept any https relay. The SDK's experimental.push.relays option and OPENCODE_EXPERIMENTAL_PUSH_RELAYS were removed.#68153

    修复

    • SDKA duplicate permission request ID returns 409 Conflict instead of 500, and importing a session with existing message IDs returns 409.#68073#68074
    • SDKThe client reports server startup failures even when a child process keeps stderr open.#68281
  2. v2.4.3

    Improvements and fixes

    查看 v2.4.3 的全部亮点

    改进

    • SDKOpenCodeWorkerd (@opencode/sdk/workerd and @opencode/sdk/workerd/effect) now exposes fetch, password, cors, workspace, machine-link WebSocket upgrades, Durable Object WebSocket hibernation handlers, and a host imageResizer hook, with demand-driven background timers so Durable Objects can hibernate.#67022#67023#67026#67037

    修复

    • SDKTreated an empty XDG_STATE_HOME environment variable as unset in @opencode/client, and fixed @opencode/client/solid dropping location-less catalog *.updated events. Thanks @JorgeRojas2712.#61288#66813
  3. v2.4.2

    @opencode/sdk/workerd

    @opencode/sdk/workerd: forward your Durable Object's alarm() to opencode.alarm() so idle remote workspaces suspend.#66183#66240

    查看 v2.4.2 的全部亮点
查看全部更新