跳到正文

SDK

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

功能

用代码修复 CI

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

跟踪每一步

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

构建自己的工具

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

在您的进程内

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

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

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

更新日志

  1. v2.3.1

    Timed-Out Shell Commands Keep Running

    A shell command still running when its timeout expires now moves to the background instead of being killed, so long builds and test runs finish and report back. A foreground timeout: 0 now means the default 120 seconds.#63699

    Compaction Without the Wait

    As a session nears automatic compaction, the summary is now written in the background, so your next prompt doesn't pause for it. Set compaction.background: false to turn it off; a background summary that ends up unused still costs tokens.#64290

    查看 v2.3.1 的全部亮点

    改进

    • Core, SDKServer requests that crash now return a 500 with a readable JSON UnknownError body instead of an empty one. Storage problems name the cause (for example "Storage is full" or "Storage is read-only"); other crashes include a ref you can find in the server log.#64022
    • Core, SDKExperimental schedules are now automations, behind the automations flag (the old schedules flag still works). An automation runs on a time, an interval, a cron or a pushed MCP event, and either sends a prompt (to the creating session, another session or a new subagent) or runs a Code Mode program that decides whether to send one. Use /automation or the automation_* tools. Active or paused plain prompt schedules carry over; schedules with a condition or run limit, and finished ones, are dropped.#64160

    修复

    • SDKApps embedding the SDK with an in-memory database no longer import the user's legacy auth.json credentials. Only a database file opened by path imports them.#63716
  2. v2.3.0

    Event subscriptions can now receive experimental.mcp.event events from MCP event subscriptions

    查看 v2.3.0 的全部亮点

    改进

    • SDKModel capability schemas now advertise supported freeform tool-input formats.
    • SDKSession throughput is now measured from the first streamed token.
    • SDKSession creation documentation now clarifies the parentID field.
  3. v2.2.7

    session.generate returns tokens and cost along with text

    查看 v2.2.7 的全部亮点

    破坏性变更

    SDK: fewer endpoints wait for plugins to load

    On a Location that is still loading plugins, agent.list returns the agents loaded so far, and integration.list, usage.limits, vcs.init, rpc.call and artifact.* no longer wait up to 5 seconds. Entries and actions that a plugin provides appear when it finishes loading, so refresh on the matching *.updated event. Model, provider, generate.*, voice.* and websearch.query endpoints still wait.

    #62796#63040

    改进

    • SDKcredential.updated includes the integrationID and credentialID of the login that changed.#62762
    • SDKPredicate questions in client.experimental.generate.decisions accept optional criteria: { true?, false? }.#62819
查看全部更新