词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!
/opencode 或 /oc,OpenCode 将在你的 GitHub Actions runner 中执行任务。
.github/workflows/opencode.yml 添加以下内容:注意:if 条件使用 startsWith和contains(' /oc')的组合,确保触发短语在行首或前面有空格,避免误匹配 URL 或代码中的内容。
| 选项 | 必填 | 默认值 | 说明 |
|---|---|---|---|
model | 是 | - | 使用的模型,格式为 provider/model |
agent | 否 | config 中的 default_agent 或 "build" | 使用的代理,必须是 primary 代理 |
share | 否 | 公开仓库 true | 是否分享会话链接 |
prompt | 否 | - | 自定义提示,覆盖默认行为(schedule/workflow_dispatch/issues 事件必填) |
use_github_token | 否 | false | 使用 GITHUB_TOKEN 替代 OpenCode App 令牌交换,跳过 OIDC |
mentions | 否 | /opencode,/oc | 自定义触发短语(逗号分隔,不区分大小写) |
oidc_base_url | 否 | https://api.opencode.ai | 自定义 OIDC 令牌交换 API 地址,仅运行私有 GitHub App 时需要 |
opencode/github/action.yml:7-35use_github_token: true 可跳过 OIDC 令牌交换,直接使用 GitHub Action runner 的内置 GITHUB_TOKEN:| 类别 | 事件类型 | 特点 |
|---|---|---|
| 用户事件 | issue_comment、pull_request_review_comment、issues、pull_request | 有触发者信息,可以在 Issue/PR 上添加评论和 reaction |
| 仓库事件 | schedule、workflow_dispatch | 无 Issue/PR 上下文,输出仅记录到日志或创建 PR |
opencode/packages/opencode/src/cli/cmd/github.ts:141-1431. 事件触发
↓
2. 检查触发短语 (/opencode 或 /oc)
↓
3. 获取访问令牌 (OIDC 交换 或 GITHUB_TOKEN)
↓
4. 权限验证 (仅用户事件,需要 admin 或 write 权限)
↓
5. 添加 👀 reaction (仅用户事件,表示正在处理)
↓
6. 根据事件类型处理:
- Issue: 创建新分支 → 执行任务 → 提交 → 创建 PR
- 本地 PR: 检出分支 → 执行任务 → 提交到同一 PR
- Fork PR: 添加 fork remote → 执行任务 → 推送到 fork
- 仓库事件: 创建新分支 → 执行任务 → 创建 PR
↓
7. 创建评论并移除 reaction| 场景 | 分支名格式 | 示例 |
|---|---|---|
| Issue 修复 | opencode/issue{ID}-{timestamp} | opencode/issue42-20250108120000 |
| PR 操作 | opencode/pr{ID}-{timestamp} | opencode/pr15-20250108120000 |
| 定时任务 | opencode/schedule-{hex}-{timestamp} | opencode/schedule-a1b2c3-20250108120000 |
| 手动触发 | opencode/dispatch-{hex}-{timestamp} | opencode/dispatch-d4e5f6-20250108120000 |
github.ts:1047-1059Fix authentication issue
Co-authored-by: username <username@users.noreply.github.com>注意: schedule事件没有触发者,因此不会添加 Co-author 信息。
github.ts:1061-1100提示:使用 OpenCode GitHub App 时,权限由 App 控制。使用 use_github_token: true时,需要在 workflow 中明确授予权限。
| 事件类型 | 触发方式 | 说明 |
|---|---|---|
issue_comment | Issue 或 PR 上的评论 | 在评论中提及 /opencode 或 /oc |
pull_request_review_comment | PR 中特定代码行的评论 | 代码审查时提及触发短语 |
issues | Issue 创建或编辑 | 需要 prompt 输入 |
pull_request | PR 创建或更新 | 用于自动审查 |
schedule | 基于 cron 的定时任务 | 需要 prompt 输入,无评论输出 |
workflow_dispatch | 从 GitHub UI 手动触发 | 需要 prompt 输入 |
注意:定时事件需要 prompt输入,因为没有评论可提取指令。输出记录到 Actions 日志,如有代码更改会创建 PR。
pull_request 事件,如果未提供 prompt,OpenCode 默认审查 PR。mentions 参数自定义触发短语:/ai、/bot 或 /help 触发 OpenCode。注意:触发短语匹配不区分大小写,多个短语用逗号分隔。
| 对比项 | 本地 PR | Fork PR |
|---|---|---|
| 分支来源 | 同一仓库 | Fork 仓库 |
| 检出方式 | git fetch origin && git checkout | git remote add fork && git fetch fork |
| 推送目标 | 原分支 | Fork 仓库的分支 |
| 分支名 | 保持原分支名 | 创建新的本地分支 opencode/pr{ID}-{timestamp} |
headRepository 是否与 baseRepository 不同github.ts:1035-1045注意:Fork PR 需要 Fork 仓库的维护者允许上游仓库推送更改(在 PR 页面勾选 "Allow edits from maintainers")。
pr/<PR号>https://opncd.ai/s/abc123),命令会自动导入会话历史,让你继续之前的对话上下文。.github/workflows/opencode.yml 文件| 环境变量/参数 | 说明 |
|---|---|
MODEL | 使用的模型,格式 provider/model |
ANTHROPIC_API_KEY | 模型提供商 API 密钥 |
GITHUB_RUN_ID | 模拟 GitHub Actions 环境,本地测试可设为 dummy |
--token | GitHub 个人访问令牌,用于验证权限和操作仓库 |
--event | 模拟的 GitHub 事件 JSON |
{
"eventName": "issue_comment",
"repo": {"owner": "owner", "repo": "repo-name"},
"actor": "username",
"payload": {
"issue": {"number": 42},
"comment": {"id": 1, "body": "/opencode 解释这个问题"}
}
}{
"eventName": "issue_comment",
"repo": {"owner": "owner", "repo": "repo-name"},
"actor": "username",
"payload": {
"issue": {"number": 15, "pull_request": {}},
"comment": {"id": 1, "body": "/opencode 优化这段代码"}
}
}{
"eventName": "pull_request_review_comment",
"repo": {"owner": "owner", "repo": "repo-name"},
"actor": "username",
"payload": {
"pull_request": {"number": 15},
"comment": {
"id": 1,
"body": "/opencode 添加错误处理",
"path": "src/utils/api.ts",
"diff_hunk": "@@ -10,6 +10,8 @@\n async function fetchData() {\n- return fetch(url)\n+ const response = await fetch(url)\n+ return response.json()\n }",
"line": 12,
"original_line": 10,
"position": 5,
"commit_id": "abc123",
"original_commit_id": "def456"
}
}
}/opencode explain this issue/opencode fix thisDelete the attachment from S3 when the note is removed /oc[在 Files 标签页的特定行上评论]
/oc add error handling here| 现象 | 原因 | 解决 |
|---|---|---|
报错 Could not fetch an OIDC token | workflow 缺少 id-token: write 权限 | 添加 permissions: id-token: write |
/opencode 没有触发 | 评论中的触发短语格式不对(如在 URL 中间) | 确保触发短语 在行首或前面有空格 |
| Fork PR 无法推送更改 | Fork 维护者未允许上游推送 | 联系 Fork 维护者开启 "Allow edits from maintainers" |
| Schedule 事件没有输出评论 | 定时任务无 Issue/PR 上下文 | 这是预期行为,输出记录到 Actions 日志 |
报错 User xxx does not have write permissions | 触发者没有仓库写入权限 | 只有 admin 或 write 权限的协作者才能触发 |
| 自定义 mentions 不生效 | 多个短语未正确用逗号分隔 | 使用 mentions: "/ai,/bot" 格式 |
使用 use_github_token 时权限不足 | 未授予必要的 workflow 权限 | 添加 contents: write、pull-requests: write 等权限 |