OpenCode 2.0发布:重构架构与代码模式

开源代码工具OpenCode发布2.0版本,重构架构与API体系。新版本采用单一后台进程模式,所有前端通过中央进程通信,并基于数百万用户经验重新设计API,以适配各类代理需求。

OpenCode 2.0:架构重构与核心功能升级

开源代码工具 OpenCode 发布 2.0 版本,此次更新被视为重大突破。新版本借鉴了数百万日常用户的使用经验,旨在构建一个能够适应各类代理(Agent)需求的系统。除了常规的功能增强、性能改进和 Bug 修复外,以下面向用户的重大变化值得关注。

OpenCode 2.0 引入代码模式

服务架构:单一后台进程模式

OpenCode 自初代起便采用客户端/服务器架构,但该模式此前主要服务于高级用户。在 2.0 版本中,服务器默认作为单个后台进程自动运行于本地机器上。无论是终端界面(TUI)、网页界面还是桌面应用程序,所有前端均通过该中央进程进行通信。

TerminalFix flaky testWebFix flaky testDesktopAdd dark modereview.tsReview changesopencodepid 48213StoppedStartingRunningSessionsFix flaky testAdd dark modeReview changes

开发者可使用程序化客户端 @opencode/client 编写脚本,特别适用于计划任务场景。此外,也可通过 CLI 调用开源 API 命令实现自动化操作。新架构支持将服务暴露给其他主机,用户可通过 /pair 或 opencode pair 命令连接至其他应用程序。目前,团队正在开发利用此流程的本地应用程序。

API 体系:从零重构以适配代理需求

随着对“代理”概念认知的深化,OpenCode 团队基于数百万用户的实际使用案例,从零开始重新设计了 API 体系,使其能够支持绝大多数常见及特殊场景。值得注意的是,OpenCode 自身也深度集成并调用其 API,以便在执行新任务时优化内部逻辑。

SessionNew sessionYOUStart rate limiting uploads in a new worktree.execute{ code }›opencode.worktree_create[name=rate-limit]›opencode.session_rename[title=Rate limit uploads]›opencode.session_moveYOUThe flaky test session is stuck. Stop it, have it quarantine the test, and close my finished tabs.execute{ code }›opencode.session_list›opencode.session_context[sessionID=ses_7c41e09a]└ "Retrying uploads.test.ts again…"execute{ code }›opencode.session_interrupt[sessionID=ses_7c41e09a]›opencode.session_prompt[sessionID=ses_7c41e09a, text=Quarantine uploads.test.ts]›opencode.tab_close[sessionID=ses_2f9d61b4]Stopped it, told it to quarantine the test, closed the config tab.OpenCode~/appFix flaky upload testinterruptedMigrate config loadertab closedNew sessionthis session

官方发布了新的客户端包 @opencode/client,提供 Vanilla JavaScript 和 Effect 两种实现方式。以下是创建会话并发送提示词的代码示例:

没有。
import { OpenCode } from "@opencode/client"
import { Service } from 

const endpoint = await Service.ensure()
const client = OpenCode.make({
  baseUrl: endpoint.url,
  headers: Service.headers(endpoint),
})

const session = await client.session.create({
  location: { directory: process.cwd() },
})

await client.session.prompt({
  sessionID: session.id,
  text: "Review the current changes",
})

代码模式(Code Mode):内置轻量级解释器

鉴于大模型擅长生成代码的特性,OpenCode 2.0 引入了 @opencode/codemode 包,其中包含一个最小化的 JavaScript 解释器。该机制允许代理直接编写并执行脚本,而非逐一调用工具接口。

SessionYOUFile GitHub bugs in Linear.execute{ code }tools.github.list_issuestools.linear.save_issueexecute{ code }["OC-412", "OC-413", "OC-414"]Code Modereturn [search({ query: "list github issues" }), search({ query: "create linear issue" })]const { issues } = await tools.github.list_issues({ owner: "anomalyco", repo: "opencode", labels: ["bug"], state: "OPEN",})const filed = await Promise.all(issues.map(({ title }) => tools.linear.save_issue({ team: "OC", title })))return filed.map(issue => issue.identifier)MCP serversgithublinearsentry

该系统将 MCP 服务器或 OpenAPI 端点转化为代理可在脚本中调用的函数。它无需依赖 Node.js 虚拟机或嵌入 QuickJS 等复杂方案即可在本地运行脚本。代理可以通过编写脚本完成诸如调用工具、映射结果并再次调用工具等操作。在 2.0 中,所有 MCP 服务器默认启用代码模式,这意味着即使添加大量字符串配置,代理也能自动生成相应的执行脚本。

背景任务:并行处理与子代理调度

为了充分发挥最新一代模型在乐器(此处原文可能指特定硬件或隐喻并发能力,保留原意语境下的“并行/后台”含义)上的运行优势,OpenCode 2.0 增强了后台任务处理能力。

SessionYOUSpawn a background fable subagent to review this code.subagentreview this codefablebackground2 comments$shellbun testbackground148 passededitsrc/retry.ts+12$$shellbun run buildbackgrounddonePress to move running work to the backgroundYOUUpdate the changelog too.editCHANGELOG.md+3All done. The review left 2 comments, 148 tests passed, the build succeeded and the changelog is updated.Backgroundreview this codefablebun testbun run build

首先,系统引入了后台子代理(Background Subagents)。主代理可将部分工作推送给另一个代理在后台执行,同时继续当前任务。这一过程可自动发生,也可手动触发,甚至支持指定不同的模型。例如,用户可以指令:“发出一个后台 Fable 子代理来检查这段代码。”

其次,新增了带命令的后台外壳(Background Shell),允许同时调用 Shell 工具,避免长时间运行的任务阻塞主代理。任务完成后会收到通知。若前台命令或子代理耗时超出预期,用户可通过快捷键将其移至后台。

插件系统:全面内部化与热重载

尽管 OpenCode 1.0 已具备插件系统,但关注度不足。为解决这一问题,团队将 OpenCode 2.0 的几乎每一项功能都重构为内部插件,目前已有超过 80 个插件。这种设计确保了插件 API 的稳定性和维护性。

TUI 插件已从实验性功能升级为稳定版,支持构建各类自定义组件。此外,2.0 版本实现了全内容的热重载能力。OpenCode 可以在会话中自行编写插件并立即生效,这一设计已在之前的博客文章中详细介绍。用户可以让 OpenCode 构建所需的任何插件,相关文档现已公开。

SDK:库形式集成与云端部署

OpenCode 现在可作为 @opencode/sdk 软件包以库的形式使用,其底层代码与 CLI 相同。该 SDK 支持在服务器上运行,也可部署在 Durable Objects 等特殊环境中。由于使用标准的 OpenCode 服务器协议,任何 OpenCode 客户端均可与其连接。SDK 还提供了可选的基于 Effect 的 API。虽然该模块仍在持续开发中,但目前已可供使用。

升级指南与兼容性说明

用户可通过运行 opencode upgrade --major 命令进行升级。桌面应用程序需下载最新版本。

本次更新包含若干破坏性变更:

  • 1.0 版本的插件无法在 2.0 中直接使用。
  • 服务器 API 结构完全改变。
  • TUI 配置文件格式从 tui.json 迁移至新的 JSON 格式。

不过,系统提供了向后兼容的迁移工具,可协助用户完成上述配置的转换。

评论 0

0/500

评论需审核后展示,请文明发言

💬
还没有评论,来说两句

相关阅读