乐于分享
好东西不私藏

OpenCode技术原理深度剖析:AI编程助手背后的逻辑完全解密

OpenCode技术原理深度剖析:AI编程助手背后的逻辑完全解密

“AI编程助手真的只是’调用大模型’那么简单?”

“为什么它能理解你的代码?又怎么知道该调用哪些工具?”

“这篇文章,带你深入看穿OpenCode的底层逻辑。”


01. 整体架构:四层楼

OpenCode 的架构可以分为四个层次:

┌──────────────────────────────────────────────────────────────┐│                    用户界面层 (TUI/CLI)                      ││              终端界面,接收用户输入,展示结果                  │├──────────────────────────────────────────────────────────────┤│                    会话管理层 (Session)                       ││              上下文管理、状态跟踪、Prompt构建                  │├──────────────────────────────────────────────────────────────┤│                    Agent系统层 (Agent)                       ││           任务规划、工具选择、执行控制、多Agent协作            │├──────────────────────────────────────────────────────────────┤│                    工具层 (Tools + MCP)                      ││           文件操作、命令执行、浏览器控制、外部服务集成          │└──────────────────────────────────────────────────────────────┘

每一层都有其独特的职责,层层配合才能让 AI 真正”智能”起来。


02. 会话管理:Prompt 是怎么构建的?

这是最核心的部分:AI 到底看到了什么?

2.1 Prompt 构建流程

OpenCode 的 Prompt 构建由 prompt.ts统一协调,每次迭代会调用多个模块:

// 简化版的 Prompt 构建流程asyncfunctionbuildPrompt({// 1. 系统环境信息const env = await system.ts.getEnvironment()// 2. 加载 AGENTS.md / CLAUDE.md 规则const instructions = await instruction.ts.loadRules()// 3. 获取可用工具定义const tools = await tools.ts.getDefinitions()// 4. 历史对话上下文const history = await session.getHistory()// 5. 组装成最终 Promptreturn assemblePrompt({    system: env,    instructions,    tools,    history  })}

2.2 Prompt 包含哪些内容?

你输入 “帮我修复这个 Bug”,AI 看到的远不止这几个字:

模块
内容
作用
System Prompt
角色定义、能力边界、行为规则
告诉 AI 你是谁
Environment
模型名、工作目录、平台、日期
上下文信息
Instructions
AGENTS.md 里的自定义规则
项目级配置
Tool Definitions
所有可用工具的 JSON Schema
告诉 AI 能用什么
Conversation History
之前的对话和执行结果
记忆
Current Task
用户的最新指令
要干什么

2.3 工具定义示例

{"name""Read","description""读取文件内容","parameters": {"type""object","properties": {"filePath": {"type""string","description""要读取的文件路径"      },"offset": {"type""number""description""起始行号"      },"limit": {"type""number","description""读取行数"      }    },"required": ["filePath"]  }}

这就是为什么 AI 能”恰当地”调用工具——它看到的不是代码,而是一个结构化的工具说明书


03. Agent 系统:任务是怎么执行的?

3.1 三种 Agent 类型

OpenCode 内置了三种 Agent:

enum AgentType {  BUILD = "build",    // 全功能开发  PLAN = "plan",      // 只读分析  GENERAL = "general"// 复杂任务协调}
Agent
权限
用途
Build
读、写、执行
实际开发工作
Plan
只读
代码分析、安全审查
General
取决于子任务
后台任务协调

3.2 Agent 执行循环

OpenCode 的核心是一个 Prompt Loop

asyncfunctionpromptLoop(session: Session{while (!session.isDone()) {// 1. 构建 Promptconst prompt = await buildPrompt(session)// 2. 调用 LLMconst response = await llm.complete(prompt)// 3. 解析响应(可能是工具调用)const actions = parseResponse(response)// 4. 执行 Actionfor (const action of actions) {const result = await executeAction(action)// 5. 将结果添加到历史await session.addToHistory(action, result)    }  }}

3.3 ReAct 模式

AI 的”思考”采用的是 ReAct (Reasoning + Acting)模式:

Thought: 用户让我修复登录Bug,需要先定位问题Action: Grep 搜索 "login" 相关代码Observation: 找到 5 个相关文件Thought: 需要查看 login.service.ts 的具体实现Action: Read 文件 login.service.tsObservation: 发现第 42 行有空指针风险Action: Edit 修复该问题...

每一个循环都是:思考 → 行动 → 观察 → 思考 → …


04. 工具系统:AI 的”手脚”

4.1 内置工具一览

OpenCode 内置了丰富的工具:

工具
功能
Read
读取文件
Write/Edit
写入/编辑文件
Glob
按模式查找文件
Grep
全文搜索
Bash
执行 Shell 命令
LSP
语言服务器协议
WebFetch
获取网页内容

4.2 工具执行流程

asyncfunctionexecuteTool(toolCall: ToolCall{const { name, arguments } = toolCall// 1. 权限检查if (!hasPermission(name)) {thrownewError("Permission denied")  }// 2. 参数验证const params = validateParams(name, arguments)// 3. 执行const result = await tools[name].execute(params)// 4. 结果格式化return formatResult(result)}

4.3 权限控制

工具执行有严格的权限控制:

const PERMISSIONS = {  read: {    glob: "allow",    grep: "allow"    read: "allow"  },  write: {    edit: "confirm",   // 需要确认    write: "confirm",    bash: "confirm"  },  dangerous: {    bash: {"rm -rf""deny",    // 危险命令直接拒绝"sudo""deny"    }  }}

05. MCP:AI 的 USB 接口

5.1 MCP 协议原理

MCP (Model Context Protocol) 是标准化的 AI 与外部工具交互协议:

┌─────────┐      MCP       ┌─────────────┐│   LLM   │ ←─────────────→ │  MCP Server │└─────────┘   JSON-RPC     └─────────────┘       ↑                           ↓       │                    ┌─────────────┐       └────────────────────│  外部工具   │            Tool Call       │ (浏览器/DB) │                          └─────────────┘

5.2 MCP 核心概念

MCP 有三个核心概念:

概念
说明
Resources
AI 可以读取的数据(文件、API响应)
Tools
AI 可以调用的函数
Prompts
预定义的 Prompt 模板

5.3 MCP 工具定义

// MCP 工具的 JSON Schema 定义{"name""browser_navigate","description""导航到指定URL","inputSchema": {"type""object","properties": {"url": {"type""string","description""目标URL"      },"type": {"type""string""enum": ["url""back""forward""reload"]      }    },"required": ["url"]  }}

5.4 常用 MCP 服务器

MCP
功能
chrome-devtools-mcp
浏览器控制
filesystem-mcp
文件系统操作
github-mcp
GitHub API
sqlite-mcp
数据库操作
context7-mcp
知识库搜索

06. 代码理解:LSP 语言服务器

AI 之所以能”看懂”代码,靠的是 LSP (Language Server Protocol)

6.1 为什么需要 LSP?

普通 AI 只能做语法分析(这行代码是什么格式)

LSP 提供语义分析(这个变量是什么类型、这个函数在哪里定义)

6.2 LSP 能做什么?

能力
示例
Go to Definition
跳转到变量定义处
Find References
查找变量所有引用
Auto Complete
代码自动补全
Hover Information
悬停显示类型信息
Diagnostics
编译错误提示

6.3 OpenCode 中的 LSP 集成

// LSP 工具调用示例{"name""lsp_goto_definition","arguments": {"filePath""src/user.service.ts","line"42,"character"10  }}

返回结果会包含定义位置,AI 就能知道这个变量在哪儿定义的。


07. 多 Agent 协作:Agent Teams

这是 OpenCode 的进阶功能:多个 AI Agent 协同工作

7.1 消息传递机制

OpenCode 使用 Inbox + Session Injection模式:

// 消息发送流程asyncfunctionsendMessage(from, to, text{// 1. 写入收件箱(持久化)await Inbox.write(teamName, to, {    id: messageId(),from,    text,    timestamp: Date.now(),    read: false  })// 2. 注入到会话await Session.inject(to, {type"user_message",    content: text,from  })// 3. 自动唤醒接收方if (receiver.isIdle()) {    autoWake(receiver)  }}

7.2 两种状态机

OpenCode 用两个状态机跟踪 Agent 状态:

1. Member Status(成员状态)

ready → busy → shutdown_requested → shutdown         ↓ error

2. Execution Status(执行状态)

idle → thinking → executing → waiting → ...

7.3 Spawn 机制

创建子 Agent 的核心逻辑:

asyncfunctionspawnSubAgent(config{// 1. 创建新会话const session = await Session.create({    ...config,    parent: currentSession  })// 2. 继承父 Agent 上下文await session.inheritContext(parentSession)// 3. 启动 Prompt Loop(fire-and-forget)Promise.resolve()    .then(() => SessionPrompt.loop({ sessionID: session.id }))    .then(result => notifyLead(result))    .catch(err => handleError(err))return { sessionID: session.id }}

08. 上下文管理:AI 的记忆

8.1 上下文包含什么?

类型
内容
大小
System Prompt
角色定义
~2KB
Instructions
规则文件
~5KB
Tool Definitions
工具说明
~50KB
Conversation
对话历史
可变
Results
执行结果
可变

8.2 上下文压缩

当上下文太长时,OpenCode 会压缩

asyncfunctioncompressContext(context{// 1. 提取关键信息const summary = await llm.summarize(context)// 2. 保留关键结果const keyResults = extractKeyResults(context)// 3. 裁剪历史const trimmedHistory = context.history.slice(-10)return {    summary,    keyResults,    history: trimmedHistory  }}

8.3 截断策略

const CONTEXT_LIMITS = {// 超过这个大小开始压缩  compressThreshold: 80000,// 压缩后保留的最大 token 数  maxAfterCompress: 40000,// 历史保留最近 N 轮  historyKeepLast: 10}

09. 安全机制

9.1 权限层级

enum PermissionLevel {  DENY = 0,      // 禁止  READ = 1,      // 只读  WRITE = 2,      // 可写  EXECUTE = 3,    // 可执行  ADMIN = 4// 完全控制}

9.2 危险命令拦截

const DANGEROUS_PATTERNS = [/rm\s+-rf/,/sudo\s+rm/,/curl.*\|\s*sh/,/:\s*!\s*.*>/,/fork\(\)/]functioncheckDangerous(command{for (const pattern of DANGEROUS_PATTERNS) {if (pattern.test(command)) {return { allowed: false, reason: "危险命令" }    }  }return { allowed: true }}

9.3 Plan 模式

不确定要干什么?用 Plan 模式

Agent: Build  ←→  Plan              ↑           切换模式              ↓         只分析,不执行

10. 完整执行流程图

用户输入: "帮我修复登录Bug"     ↓┌─────────────────────────────────────┐│  1. Session 构建 Prompt             ││     - System + Instructions         ││     - Tools + History              │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  2. LLM 生成响应                    ││     - 分析任务                      ││     - 决定调用工具                  │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  3. 解析工具调用                    ││     - Grep("login")                ││     - Read(file)                   ││     - Edit(...)                    │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  4. 权限检查                        ││     - 工具权限验证                  ││     - 危险命令拦截                  │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  5. 执行工具                        ││     - 操作系统调用                  ││     - 返回结果                      │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  6. 结果处理                        ││     - 添加到历史                    ││     - 检查是否需要压缩              │└─────────────────────────────────────┘     ↓┌─────────────────────────────────────┐│  7. 下一轮 Loop                     ││     - 构建新 Prompt                 ││     - 继续执行或结束                │└─────────────────────────────────────┘

11. 总结

现在你应该明白,OpenCode 不是一个简单的”调用大模型”工具,而是一个复杂的系统

层次
核心技术
Prompt 构建
System Prompt + Tools Schema + Context Assembly
Agent 系统
ReAct 模式 + 状态机 + 多 Agent 协作
工具层
MCP 协议 + 权限控制 + LSP 集成
上下文
压缩 + 截断 + 持久化
安全
权限层级 + 危险命令拦截 + Plan 模式

理解这些原理,才能真正用好 AI 编程助手。


参考资料:

  • OpenCode 官方文档– https://opencode.ai/docs/
  • OpenCode Prompt 构建机制– https://gist.github.com/rmk40/cde7a98c1c90614a27478216cc01551f
  • OpenCode 多 Agent 协作架构– https://dev.to/uenyioha/porting-claude-codes-agent-teams-to-opencode-4hol
  • MCP 协议官方文档– https://modelcontextprotocol.io/
  • OpenCode 核心概念解析– https://www.ququ123.top/2026/02/opencode-core-concepts/