一、OpenCode:AI 编码执行引擎
什么是 OpenCode?
OpenCode是一个开源的、运行在终端(命令行)的AI编程助手。你可以把它想象成一个随时待命的资深开发者,它能够理解你的项目代码,直接在终端里帮你完成一系列任务,无需在浏览器和IDE之间来回切换。
它的核心作用包括:
- 编写与调试代码:根据指令生成代码片段或修复Bug。
- 理解项目:快速解释陌生模块的功能和逻辑。
- 重构与优化:分析和改进现有代码结构。
- 自动化任务:处理重复性的工作,如生成测试用例.
OpenCode vs. 传统AI助手(如 Claude Code):
| 特性 |
OpenCode |
Claude Code |
| 开源 |
完全开源,MIT 协议 |
闭源 |
| 价格 |
免费(需自备 API Key) |
需要 Claude Pro/Max 订阅 |
| 模型支持 |
支持 75+ 种模型(GPT、Claude、Gemini、DeepSeek、本地模型等) |
仅限 Claude 模型 |
| 本地模型 |
支持 Ollama 等 |
不支持 |
| 运行方式 |
CLI 终端、桌面应用、VS Code 插件 |
主要 CLI 终端 |
OpenCode CLI安装指南
OpenCode的安装非常灵活,支持多种方式,你可以根据自己的操作系统选择。安装脚本(macOS/Linux,推荐)方式,这是最简洁的方式,脚本会自动完成安装:
1
2
|
curl -fsSL https://opencode.ai/install | bash
|
OpenCode:你的终端AI编程助手
OpenCode是什么,有什么用?
OpenCode是一个开源的、运行在终端(命令行)的AI编程助手。你可以把它想象成一个随时待命的资深开发者,它能够理解你的项目代码,直接在终端里帮你完成一系列任务,无需在浏览器和IDE之间来回切换。
它的核心作用包括:
编写与调试代码:根据指令生成代码片段或修复Bug。
理解项目:快速解释陌生模块的功能和逻辑。
重构与优化:分析和改进现有代码结构。
自动化任务:处理重复性的工作,如生成测试用例。
OpenCode vs. 传统AI助手(如Claude Code):
特性 OpenCode Claude Code
开源 完全开源,MIT协议 闭源
价格 免费(需自备API Key) 需要Claude Pro/Max订阅
模型支持 支持75+种模型(GPT、Claude、Gemini、DeepSeek、本地模型等) 仅限Claude模型
本地模型 支持Ollama等 不支持
运行方式 CLI终端、桌面应用、VS Code插件 主要CLI终端
OpenCode CLI安装指南
OpenCode的安装非常灵活,支持多种方式,你可以根据自己的操作系统选择。
安装脚本(macOS/Linux,推荐)方式,这是最简洁的方式,脚本会自动完成安装:
1
|
curl -fsSL https://opencode.ai/install | bash
|
使用包管理器:
1
2
3
4
5
6
7
8
|
npm (适用于所有平台,需先安装Node.js):
npm install -g opencode-ai
Homebrew (macOS/Linux):
brew install sst/tap/opencode
Go (适用于Go开发者):
go install github.com/sst/opencode@latest
|
安装完成后,打开终端运行以下命令,如果显示版本号,则说明安装成功:
VS Code插件安装
OpenCode与VS Code的集成非常自然,它让你在编辑器里就能享受到AI助手的强大功能。
打开VS Code的扩展市场(Extensions,快捷键 Ctrl+Shift+X)。搜索“OpenCode”。点击安装。
使用技巧:安装成功后,你可以使用快捷键在编辑器中快速调出OpenCode:
- Cmd+Esc (Mac) 或 Ctrl+Esc (Windows/Linux):在当前终端分屏中打开OpenCode。
- Cmd+Shift+Esc (Mac) 或 Ctrl+Shift+Esc (Windows/Linux):启动一个新的OpenCode会话。
如何链接到GitHub Copilot账号
OpenCode的一大优势是它的“模型无关性”,它不仅可以连接OpenAI、Anthropic等厂商的API,还能直接利用你已有的GitHub Copilot订阅,让你在不增加额外成本的情况下使用OpenCode的增强功能。
配置步骤如下:
- 启动OpenCode:在你的项目目录下运行 opencode 命令。
- 运行连接命令:在OpenCode的交互界面中输入 /connect 。
- 选择提供商:在出现的提供商列表中,选择 GitHub Copilot 。
- 完成设备授权:OpenCode会在终端显示一个设备码和一个用于授权的GitHub网址(通常是 https://github.com/login/device)。
- 用浏览器打开该网址,登录你的GitHub账号,并输入终端显示的那个设备码。
- 点击“Authorize”完成授权。
配置模型(可选):授权成功后,你可以在OpenCode中通过 /models 命令查看并选择Copilot提供的模型(如GPT-4、Claude 3.5 Sonnet等)作为默认模型。
配置完成后,OpenCode就可以通过你的GitHub Copilot订阅来驱动AI了。你的认证信息会安全地存储在本地 ~/.local/share/opencode/auth.json 文件中。
OpenSpec:为AI开发引入“规范”
OpenSpec是什么,有什么用?
OpenSpec是一个面向AI辅助工作流的轻量级规范驱动开发(Spec-Driven Development, SDD)框架和CLI工具。它的核心目的是解决在AI编程中常见的“需求偏移”或“AI幻觉”问题,让开发过程从“凭感觉聊天(Vibe Coding)”转向“基于规范开发”。
简单来说,OpenSpec定义了一套工作流,让你在和AI一起写代码之前,先就“要做什么”达成一个清晰、结构化、机器可读的共识。这个共识就是“规范”(Spec)。
OpenSpec的四大核心价值:
- 单一事实来源:所有需求、设计和决策都以文件形式固化在 openspec/ 目录中,告别模糊的口头沟通。
- 可审计的变更:通过“提案-审查-实施-归档”的四阶段工作流,每一次代码变更都有迹可循。
- 约束AI行为:AI的代码生成被严格限制在已批准的规范之内,确保输出结果与预期一致,减少无用的代码生成。
- 棕地优先(Brownfield-first):OpenSpec特别擅长处理现有项目的演进,而不是仅能用于新项目。它像一个“装修团队”,能在不拆掉老房子的情况下精准地改造新功能。
OpenSpec如何工作?
它通过一个结构化的目录来管理项目和变更:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
your-project/
├── openspec/
│ ├── specs/ # 【真相来源】所有已完成的、当前系统的规范
│ │ └── [功能模块]/
│ │ └── spec.md
│ ├── changes/ # 【变更区】所有进行中的功能提案
│ │ └── [变更名称]/ # 例如 add-url-shorten
│ │ ├── proposal.md # 提案:为什么要做,做什么
│ │ ├── tasks.md # 任务:AI实施的详细清单
│ │ └── specs/ # 规范增量:本次变更要修改的规范文件
│ │ └── [功能模块]/
│ │ └── spec.md
│ ├── project.md # 项目上下文(技术栈、架构、规则等)
│ └── AGENTS.md # 指导AI如何与OpenSpec协同工作的指令
|
工作流就是围绕这个目录结构展开的:开发者提出变更 -> AI生成提案文档 -> 开发者审查 -> AI实施 -> 归档更新真相来源。
OpenSpec CLI安装
安装OpenSpec非常简单,它本身是一个Node.js CLI工具。系统要求:Node.js >= 20.19.0。
使用npm进行全局安装,这样你就可以在任何项目中使用 openspec 命令了。
1
|
npm install -g @fission-ai/openspec@latest
|
在项目中初始化
进入你的Go项目目录,运行初始化命令。这会创建我们上面提到的 openspec/ 目录结构,并引导你选择正在使用的AI工具(如OpenCode、Cursor、Copilot等)。
1
2
|
cd your-go-project
openspec init
|
初始化完成后,你可以编辑 openspec/project.md 文件,填入项目的技术栈(Go, Redis等)、架构风格等信息,为AI提供更精确的上下文。
实战:开发URL短域名服务
现在,我们将结合OpenCode和OpenSpec,从零开始开发一个URL短域名服务。我们将遵循OpenSpec的“提案-审查-实施-归档”流程。
步骤1:项目初始化与环境准备
首先,创建项目目录并初始化OpenSpec和OpenCode。
1
2
3
4
5
6
7
8
9
10
|
#创建项目目录
mkdir url-shortener
cd url-shortener
# 初始化Go模块
go mod init github.com/yourname/url-shortener
# 初始化OpenSpec (假设已全局安装)
openspec init
根据提示,选择你的AI助手,比如 'opencode'
|
接着编辑项目上下文 (openspec/project.md)
用Vim或其他编辑器打开,填入以下内容,即项目的技术栈和架构信息等,这里也可以用OpenCode来帮你生成这个文件的初始内容,再基于它进行修改得到:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# URL Shortener Server
## Overview
This project provides a URL shortener backend service. It accepts long URLs, generates short codes, and redirects clients from short URLs to their original destinations.
## Goals
- Simple HTTP API for creating short URLs and resolving them.
- Reliable, low-latency redirects.
- Safe handling of invalid or expired short codes.
## Core Behavior
- Create a short code for a given URL.
- Resolve a short code to its original URL.
- Redirect clients to the original URL using standard HTTP redirects.
## Data Model (High Level)
- Short code
- Original URL
- Creation timestamp
- Optional expiration timestamp
- Optional metadata (e.g., creator, tags)
## API Surface (High Level)
- `POST /shorten` to create a short URL.
- `GET /{code}` to resolve and redirect.
- `GET /healthz` for health checks.
## Storage
- Persistent store for code-to-URL mappings.
- Unique index on short codes.
## Non-Goals
- User authentication and billing.
- Analytics dashboards.
- Custom domains (unless added later).
## Engineering Conventions
- Keep handlers small and focused.
- Validate inputs and normalize URLs.
- Prefer explicit error responses and status codes.
- Implementation uses Go with the Gin framework.
- Use in-memory storage for code-to-URL mappings.
- Add unit tests for important functions.
|
编辑完成后执行openspec update命令更新规范库,生成AI实施所需的规范文件。
生成提案并实施
在OpenCode的交互界面中,输入斜杠命令/opsx后可以看到OpenSpec相关的命令。输入以下来生成一个新的功能提案:
1
2
3
4
5
6
7
8
9
10
11
|
/opsx:propose Create a URL Shortener Service in Golang.
Details:
- Endpoints: POST /shorten {url: string} → {short_url: string}
- GET /{short} → 301 redirect to original
- Storage: Redis if env REDIS_URL set, else in-memory sync.Map
- Short code: Base62 encoded (6-8 chars, unique)
- Validation: URL must be valid HTTP(S)
- Rate limit: Basic (optional, 10/min per IP)
- Errors: JSON {error: msg}
- Tests: Cover shorten/resolve/HTTP
Generate tasks.md with 8-12 steps: setup deps, core logic, handlers, tests, Docker.
|
OpenCode会调用AI(通过你配置的模型),分析你的 project.md,然后在 openspec/changes/ 下自动生成一个以功能命名的文件夹(如 add-url-shortening-core),并在其中生成 proposal.md(提案)、tasks.md(任务清单)和初步的 spec.md(规范增量)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
➜ changes tree -L 4
.
├── archive
└── url-shortener-service
├── design.md
├── proposal.md
├── specs
│ ├── shorten-create
│ │ └── spec.md
│ ├── shorten-rate-limit
│ │ └── spec.md
│ ├── shorten-resolve
│ │ └── spec.md
│ ├── shorten-storage
│ │ └── spec.md
│ └── shorten-testing
│ └── spec.md
└── tasks.md
|
执行apply:
1
|
/opsx:apply url-shortener-service all tasks
|
查看此时生成的代码目录:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
➜ url-shortener tree -L 4
.
├── Dockerfile
├── README.md
├── cmd
│ └── server
│ └── main.go
├── docker-compose.yml
├── go.mod
├── go.sum
├── internal
│ ├── config
│ │ └── config.go
│ ├── httpapi
│ │ ├── handlers.go
│ │ ├── handlers_test.go
│ │ └── middleware.go
│ ├── ratelimit
│ │ └── ratelimit.go
│ ├── shortener
│ │ ├── shortener.go
│ │ └── shortener_test.go
│ └── storage
│ ├── memory.go
│ ├── memory_test.go
│ ├── redis.go
│ └── storage.go
|
测试和验证
直接在opencode对话框让生成README.md,展示如何使用curl命令测试API:
1
|
write a README.md, give some curl shell command to show how to use it. And write how to go test
|
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# 运行服务
go run cmd/server/main.go
# 在另一个终端测试API
curl -X POST "http://localhost:8080/shorten" \
-H "Content-Type: application/json" \
-d '{"url":"https://kubernetes.io/docs/concepts/overview/"}'
{"short_url":"http://localhost:8080/kaQax2J"}%
curl -v "http://localhost:8080/kaQax2J"
> GET /kaQax2J HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: https://kubernetes.io/docs/concepts/overview/
< Date: Wed, 04 Mar 2026 06:32:42 GMT
< Content-Length: 80
<
<a href="https://kubernetes.io/docs/concepts/overview/">Moved Permanently</a>.
* Connection #0 to host localhost left intact
|
执行单元测试
1
2
3
4
5
6
7
|
➜ url-shortener go test ./...
? url-shortener/cmd/server [no test files]
? url-shortener/internal/config [no test files]
ok url-shortener/internal/httpapi 0.011s
? url-shortener/internal/ratelimit [no test files]
ok url-shortener/internal/shortener 0.006s
ok url-shortener/internal/storage 0.006s
|
归档
当功能开发完成并验证无误后,执行归档命令:
1
|
/opsx:archive url-shortener-service
|
这会将 changes/url-shortener-service/ 下的 proposal.md、tasks.md 和 specs/ 中的规范增量文件移动到 openspec/archive/ 下,并在 proposal.md 中记录实施结果和总结,形成一个完整的变更历史记录。第一次执行会出现:
1
2
3
4
5
6
7
8
9
|
Delta specs exist for this change, but there are no corresponding main specs in `openspec/specs/`. I can sync these new specs into main now, or archive without syncing.
1.
Sync now (Recommended)
Create main specs from the delta specs before archiving.
2.
Archive without syncing
Archive now and leave main specs unchanged.
3.
Type your own answer
|
选择第一项,OpenSpec会将变更中生成的规范增量文件(如 shorten-create/spec.md)复制到 openspec/specs/ 下的对应位置,正式将这些规范纳入“真相来源”,并完成归档。归档后的目录结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
➜ url-shortener tree -L 4
.
├── Dockerfile
├── README.md
├── cmd
│ └── server
│ └── main.go
├── docker-compose.yml
├── go.mod
├── go.sum
├── internal
│ ├── config
│ │ └── config.go
│ ├── httpapi
│ │ ├── handlers.go
│ │ ├── handlers_test.go
│ │ └── middleware.go
│ ├── ratelimit
│ │ └── ratelimit.go
│ ├── shortener
│ │ ├── shortener.go
│ │ └── shortener_test.go
│ └── storage
│ ├── memory.go
│ ├── memory_test.go
│ ├── redis.go
│ └── storage.go
└── openspec
├── changes
│ └── archive
│ └── 2026-03-04-url-shortener-service
├── config.yaml
├── project.md
└── specs
├── shorten-create
│ └── spec.md
├── shorten-rate-limit
│ └── spec.md
├── shorten-resolve
│ └── spec.md
├── shorten-storage
│ └── spec.md
└── shorten-testing
└── spec.md
|
后续其他changes(如添加统计分析功能)也可以通过同样的流程进行,形成一个持续演进的AI协同开发过程。
总结
上述代码完全是ai生成,通过opencode和openspec,实现了sdd开发,相比之前只用vscode copilot开发,多文件操作和生成更强大了,可以proposal一次提示,生成多个规范增量文件和任务清单,然后一次apply就能生成整个功能的代码骨架,极大提升了开发效率和规范性。上述生成代码demo放置在:https://github.com/yefengzhichen/url-shortener
参考