07-实战案例 OpenCode 操作指导书七实战案例适用版本OpenCode v1.18.3本篇目标通过 6 个可照做的真实案例把前六篇的能力串成肌肉记忆。每个案例含目标 / 步骤 / 提示词 / 要点。案例 1解释陌生代码库目标接手新项目快速理解认证逻辑。cd/path/to/unknown-project opencode /init# 先生成 AGENTS.md对话How is authentication handled in packages/functions/src/api/index.ts? Walk me through the request lifecycle from entry to DB.要点用文件精准引用可连续追问那错误如何处理。案例 2新增功能Plan → Build 工作流目标给笔记应用加回收站功能。Plan 模式描述需求OpenCode 输出计划你反馈/补充切回 Build 执行权限确认 y完成 展示 diff步骤Tab切到Plan模式。输入需求When a user deletes a note, flag it as deleted in the database. Then create a screen that shows all recently deleted notes, where the user can undelete or permanently delete.可拖入设计稿图片作为参考追加“Use this design as reference.”满意后Tab切回Build执行Sounds good! Go ahead and make the changes.对权限提示按y允许review diff。案例 3调试与重构目标定位并修复一个性能问题再重构。Theres a slow query in packages/functions/src/notes.ts listNotes(). Profile it and suggest a fix, then implement it.重构练习Refactor the function in packages/functions/src/api/index.ts to reduce nesting. Show me the diff before committing.回退/undo若结果不如预期。案例 4为代码补充单元测试目标给核心模块加测试。Plan 模式Write unit tests for the validation logic in src/utils/validate.ts using the projects existing test framework. Cover edge cases.Build 执行后/compact压缩上下文再让其补充边界用例。要点让 OpenCode 先看项目里已有测试的风格tests/...保持一致。案例 5GitHub 评论触发自动化目标在 Issue 中让 OpenCode 自动调研并开 PR。安装opencode github install引导配置 Actions 与 Secret。在 Issue 评论/opencode investigate this bug and open a PR with a fixOpenCode 会在新分支修复并提 PR支持事件issue_comment、pull_request_review_comment、issues、pull_request、schedule。工作流示意Issue 评论 /ocGitHub Actions 触发OpenCode 新建分支修复自动提交 PR案例 6批处理脚本调用CI / 自动化目标用run对一批文件做说明生成写入文档。# 启动无头服务避免每次冷启动 MCPopencode serveforfinsrc/*.ts;doopencode run--attachhttp://localhost:4096\Summarize the public API of $fin one paragraph\api-docs.mddone或单次非交互opencode runList all TODO comments in the repo and group by file--formatjson综合练习清单案例用到的能力对应篇章1 解释代码引用//init042 新功能Plan/Build / 权限043 调试重构diff //undo044 单测风格对齐04/055 GitHubgithub install05/066 批处理run/serve06做完这 6 个你已基本熟练。最后查漏补缺见08-常见问题与最佳实践.md。本篇为 OpenCode 操作指导书系列之一版本 v1.18.3。