Learn OpenCode
从零理解 AI 编码 agent 的设计与实现,一个机制一节课
核心模式
每个 AI 编码 agent 共享同一个循环:调用模型、执行工具、回传结果。生产系统在此基础上添加策略、权限和生命周期层。
系统架构
五层架构组合成完整的 AI 编码 agent,每层构建在下一层之上
架构分层
五个正交的关注点,组合成完整的 agent
Tools & Execution
4 个章节The smallest useful agent is a loop that calls the model, runs tools, and feeds results back.
The loop stays stable while capabilities register into a dispatch table.
Dangerous actions need a harness decision point before the shell runs.
Cross-cutting behavior belongs around the loop, not tangled inside it.
Planning & Control
3 个章节Concurrency
1 个章节与 Claude Code 对比
两种 agent 共享相同的核心循环架构,但在策略层有所不同
| 机制 | Claude Code | OpenCode |
|---|---|---|
| 核心循环 | while(model responds) | while(tool_use) via Effect/Stream |
| 工具调度 | Function calling API | TOOL_MAP registry + Schema |
| 权限模型 | User confirmation prompt | Wildcard patterns · allow/deny/ask · Deferred |
| 可扩展性 | CLI flags + env vars | Plugin SDK · Hooks API · Skill system |
| 子代理 | @mentions | Named agents · clean context · task_id resume |
| 技能系统 | Custom instructions | SKILL.md · priority chain · YAML frontmatter |
| 配置 | CLAUDE.md + env | opencode.jsonc · deepMerge · JSON Schema |
参考: learn.shareai.run — Claude Code 学习站点
学习路径
10 节渐进式课程,从简单的循环到完整的 agent 系统
The Agent Loop
The smallest useful agent is a loop that calls the model, runs tools, and feeds results back.
Tool Dispatch
The loop stays stable while capabilities register into a dispatch table.
Permission Gate
Dangerous actions need a harness decision point before the shell runs.
Hook System
Cross-cutting behavior belongs around the loop, not tangled inside it.
Task Categories
Routing tasks by category decouples intent from implementation.
Subagent
Subagents give each subtask a clean context while preserving the main thread.
Skill Loading
Inject specialized knowledge only when the task actually needs it.
Plugin SDK
A well-designed hook system makes the agent infinitely extensible.
Background Tasks
The agent can keep reasoning while slow work completes elsewhere.
Config & Session
A single config file controls the entire agent harness behavior.