ai-spec-dev 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,387 @@
1
+ # ai-spec
2
+
3
+ > AI 驱动的功能开发编排工具 — 从一句话需求到可运行代码的完整流水线
4
+
5
+ ```
6
+ 需求描述 → 项目感知 → Spec 生成 → 交互式润色 → Git 隔离 → 代码生成 → AI 代码审查
7
+ ```
8
+
9
+ ---
10
+
11
+ ## 目录
12
+
13
+ - [快速开始](#快速开始)
14
+ - [支持的模型](#支持的模型)
15
+ - [命令总览](#命令总览)
16
+ - [工作流详解](#工作流详解)
17
+ - [配置文件](#配置文件)
18
+ - [全局安装](#全局安装)
19
+
20
+ ---
21
+
22
+ ## 快速开始
23
+
24
+ ```bash
25
+ # 1. 安装依赖 & 构建
26
+ npm install
27
+ npm run build
28
+
29
+ # 2. 设置 API Key(以 Gemini 为例)
30
+ export GEMINI_API_KEY=your_key_here
31
+
32
+ # 3. 运行
33
+ node dist/cli/index.js create "给用户模块增加登录功能"
34
+ ```
35
+
36
+ 运行后会依次经历以下步骤(全部自动化):
37
+
38
+ ```
39
+ [1/6] Loading project context... ← 读取 package.json / prisma / 路由文件
40
+ [2/6] Generating spec with gemini... ← AI 生成结构化 Markdown Spec
41
+ [3/6] Interactive spec refinement... ← 终端交互:编辑 / AI 润色 / 确认
42
+ [4/6] Setting up git worktree... ← 创建隔离的 feature 分支工作区
43
+ [5/6] Spec saved: specs/feature-xxx.md
44
+ [6/6] Code generation (mode: api)... ← AI 生成代码文件 / 启动 Claude Code
45
+ [7/7] Automated code review... ← AI 对照 Spec 审查 git diff
46
+ ```
47
+
48
+ ---
49
+
50
+ ## 支持的模型
51
+
52
+ | Provider | 关键词 | API Key 环境变量 | 默认模型 |
53
+ |---|---|---|---|
54
+ | Google Gemini | `gemini` | `GEMINI_API_KEY` | `gemini-2.5-flash` |
55
+ | Anthropic Claude | `claude` | `ANTHROPIC_API_KEY` | `claude-sonnet-4-6` |
56
+ | OpenAI | `openai` | `OPENAI_API_KEY` | `gpt-4o` |
57
+ | 通义千问 | `qwen` | `DASHSCOPE_API_KEY` | `qwen-max` |
58
+ | MiniMax | `minimax` | `MINIMAX_API_KEY` | `MiniMax-Text-01` |
59
+ | 智谱 GLM | `glm` | `ZHIPU_API_KEY` | `glm-4-plus` |
60
+
61
+ > 国内模型(Qwen / MiniMax / GLM)均使用 OpenAI 兼容接口,无需额外依赖。
62
+
63
+ 查看所有 provider 的完整模型列表:
64
+
65
+ ```bash
66
+ ai-spec model --list
67
+ ```
68
+
69
+ ---
70
+
71
+ ## 命令总览
72
+
73
+ ```
74
+ ai-spec create [idea] 完整工作流:context → spec → refine → worktree → codegen → review
75
+ ai-spec model 交互式切换 AI provider / model,写入 .ai-spec.json
76
+ ai-spec review [file] 对当前 git diff 运行 AI 代码审查
77
+ ai-spec config 查看 / 修改 / 重置项目级配置
78
+ ```
79
+
80
+ ---
81
+
82
+ ## 命令详解
83
+
84
+ ### `ai-spec create [idea]`
85
+
86
+ 启动完整的功能开发流水线。`idea` 参数省略时会交互式询问。
87
+
88
+ ```bash
89
+ # 最简用法(使用 .ai-spec.json 或默认值)
90
+ ai-spec create "增加商品搜索功能"
91
+
92
+ # 指定 provider / model
93
+ ai-spec create "用户登录" --provider claude --model claude-opus-4-6
94
+
95
+ # Spec 用 Claude,代码生成用 Qwen
96
+ ai-spec create "购物车结算" \
97
+ --provider claude \
98
+ --codegen api \
99
+ --codegen-provider qwen \
100
+ --codegen-model qwen-max
101
+
102
+ # 只生成 Spec 和实施方案,不写代码不创建 worktree
103
+ ai-spec create "重构支付模块" --codegen plan --skip-worktree --skip-review
104
+ ```
105
+
106
+ **完整选项:**
107
+
108
+ | 选项 | 说明 | 默认值 |
109
+ |---|---|---|
110
+ | `--provider <name>` | Spec 生成使用的 provider | `gemini` |
111
+ | `--model <name>` | Spec 生成使用的模型 | provider 默认模型 |
112
+ | `-k, --key <key>` | API Key(优先级高于环境变量) | — |
113
+ | `--codegen <mode>` | 代码生成模式:`claude-code` / `api` / `plan` | `claude-code` |
114
+ | `--codegen-provider <name>` | 代码生成使用的 provider(默认同 `--provider`) | — |
115
+ | `--codegen-model <name>` | 代码生成使用的模型 | — |
116
+ | `--codegen-key <key>` | 代码生成的 API Key(provider 不同时使用) | — |
117
+ | `--skip-worktree` | 跳过 git worktree 创建 | — |
118
+ | `--skip-review` | 跳过最终代码审查 | — |
119
+
120
+ **`--codegen` 三种模式:**
121
+
122
+ | 模式 | 说明 |
123
+ |---|---|
124
+ | `claude-code` | 在 worktree 目录中交互式启动 Claude Code CLI(默认) |
125
+ | `api` | 调用 AI API 自动规划文件列表并逐文件生成代码写入磁盘 |
126
+ | `plan` | 仅输出实施方案(文件清单 + 步骤),不写任何代码 |
127
+
128
+ ---
129
+
130
+ ### `ai-spec model`
131
+
132
+ 交互式 provider / model 切换器,结果写入当前目录的 `.ai-spec.json`。
133
+
134
+ ```bash
135
+ # 启动交互式选择
136
+ ai-spec model
137
+
138
+ # 仅查看所有可用 provider 和模型,不做任何修改
139
+ ai-spec model --list
140
+ ```
141
+
142
+ **交互流程:**
143
+
144
+ ```
145
+ ? Configure model for:
146
+ ❯ Spec generation (用于 spec 生成和润色)
147
+ Code generation (--codegen api 时生效)
148
+ Both (spec 和 codegen 使用同一模型)
149
+
150
+ ? Spec — select provider:
151
+ ❯ Google Gemini Google AI Studio — Gemini 2.5 / 1.5 series
152
+ Anthropic Claude Anthropic — Claude 4.x series
153
+ 通义千问 (Qwen) 阿里云百炼 DashScope — qwen-max / plus / turbo
154
+ MiniMax MiniMax AI — MiniMax-Text-01 / abab series
155
+ 智谱 GLM (Zhipu AI) 智谱 AI — GLM-4 plus / flash / air series
156
+ ...
157
+
158
+ ? Spec — select model (通义千问):
159
+ ❯ qwen-max
160
+ qwen-max-latest
161
+ qwen-plus
162
+ qwen-turbo
163
+ ✎ Enter custom model name...
164
+
165
+ Preview:
166
+ spec → qwen/qwen-max
167
+
168
+ ? Save to .ai-spec.json? Yes
169
+ ✔ Saved to .ai-spec.json
170
+ ```
171
+
172
+ ---
173
+
174
+ ### `ai-spec review [specFile]`
175
+
176
+ 抓取当前目录的 git diff,让 AI 以架构师视角对照 Spec 进行代码审查。
177
+
178
+ ```bash
179
+ # 自动检测 specs/ 目录下最新的 Spec 文件
180
+ ai-spec review
181
+
182
+ # 指定 Spec 文件
183
+ ai-spec review specs/feature-1234567890.md
184
+
185
+ # 指定 provider
186
+ ai-spec review --provider glm --model glm-4-plus
187
+ ```
188
+
189
+ **输出结构:**
190
+
191
+ ```
192
+ ─── Review Result ───────────────────────────────
193
+ ## ✅ 优点 (What's Good)
194
+ ## ⚠️ 问题 (Issues Found)
195
+ ## 💡 改进建议 (Suggestions)
196
+ ## 📊 总体评价 (Overall Assessment) Score: X/10
197
+ ─────────────────────────────────────────────────
198
+ ```
199
+
200
+ > 提示:先执行 `git add .` 再运行 `ai-spec review`,可以让 AI 审查所有变更。
201
+
202
+ ---
203
+
204
+ ### `ai-spec config`
205
+
206
+ 管理当前项目的默认配置(`.ai-spec.json`),避免每次都传重复的参数。
207
+
208
+ ```bash
209
+ # 查看当前配置
210
+ ai-spec config --show
211
+
212
+ # 设置默认 provider 和 codegen 模式
213
+ ai-spec config --provider qwen --codegen api
214
+
215
+ # 单独设置 codegen provider(spec 和 codegen 使用不同模型)
216
+ ai-spec config --codegen-provider glm --codegen-model glm-4-plus
217
+
218
+ # 重置为空(恢复内置默认值)
219
+ ai-spec config --reset
220
+ ```
221
+
222
+ **完整选项:**
223
+
224
+ | 选项 | 说明 |
225
+ |---|---|
226
+ | `--provider <name>` | 默认 spec provider |
227
+ | `--model <name>` | 默认 spec model |
228
+ | `--codegen <mode>` | 默认代码生成模式 |
229
+ | `--codegen-provider <name>` | 默认 codegen provider |
230
+ | `--codegen-model <name>` | 默认 codegen model |
231
+ | `--show` | 打印当前配置 |
232
+ | `--reset` | 清空配置文件 |
233
+
234
+ ---
235
+
236
+ ## 工作流详解
237
+
238
+ ### Step 1 — Context Injection(项目感知)
239
+
240
+ 自动读取当前项目:
241
+
242
+ - `package.json` → 依赖列表、技术栈识别(Express / Prisma / React 等)
243
+ - `prisma/schema.prisma` → 数据库模型
244
+ - `src/**/routes/**` / `src/**/controllers/**` → 路由文件(读取前 60 行作为上下文)
245
+
246
+ 这些信息作为 System Prompt 的一部分注入给 AI,确保生成的 Spec 和代码与现有项目结构一致。
247
+
248
+ ---
249
+
250
+ ### Step 2 — Spec Generation(Spec 生成)
251
+
252
+ 调用选定的 AI provider,生成结构化的中文 Markdown Spec,模板包含:
253
+
254
+ ```
255
+ # Feature Spec: {功能名称}
256
+ ## 1. 功能概述
257
+ ## 2. 背景与动机
258
+ ## 3. 用户故事
259
+ ## 4. 功能需求(核心功能 + 边界条件)
260
+ ## 5. API 设计(接口列表 + 请求响应示例)
261
+ ## 6. 数据模型(Prisma Schema)
262
+ ## 7. 非功能性需求
263
+ ## 8. 实施要点
264
+ ```
265
+
266
+ ---
267
+
268
+ ### Step 3 — Spec Refiner(交互式润色)
269
+
270
+ 终端交互循环,每轮提供三个选项:
271
+
272
+ ```
273
+ ? What would you like to do?
274
+ ❯ ✅ Finalize — proceed to code generation
275
+ 🤖 AI Polish — let AI improve clarity & completeness
276
+ ✏️ Edit again — continue editing
277
+ ```
278
+
279
+ - **Finalize** — 确认 Spec,进入下一步
280
+ - **AI Polish** — AI 自动润色后在编辑器中打开供确认,不强制覆盖
281
+ - **Edit again** — 重新打开编辑器手动修改
282
+
283
+ ---
284
+
285
+ ### Step 4 — Git Worktree(隔离工作区)
286
+
287
+ 在项目同级目录自动创建 git worktree:
288
+
289
+ ```
290
+ 原项目: ~/code/my-app/
291
+ worktree: ~/code/my-app-add-user-login/ ← 新目录,独立分支 feature/add-user-login
292
+ ```
293
+
294
+ - 分支已存在时直接复用,不重复创建
295
+ - 不是 git 仓库时跳过,在原目录继续
296
+ - 可通过 `--skip-worktree` 强制跳过
297
+
298
+ ---
299
+
300
+ ### Step 5 — Code Generator(代码生成)
301
+
302
+ 根据 `--codegen` 参数选择模式:
303
+
304
+ **`api` 模式(推荐用于自动化)**
305
+
306
+ 两阶段生成:
307
+ 1. AI 读取 Spec,输出 JSON 格式的文件操作计划(create / modify)
308
+ 2. 对每个文件逐一调用 AI 生成完整代码,写入 worktree
309
+
310
+ ```
311
+ Plan: 3 file(s) to process
312
+ + src/controllers/userController.ts: Handle user login logic
313
+ + src/routes/client/user.ts: Register login routes
314
+ ~ src/routes/client/index.ts: Mount user router
315
+
316
+ Generating src/controllers/userController.ts... ✔
317
+ Generating src/routes/client/user.ts... ✔
318
+ Generating src/routes/client/index.ts... ✔
319
+ ```
320
+
321
+ **`claude-code` 模式(默认)**
322
+
323
+ 在 worktree 目录中启动 Claude Code CLI 交互式会话,由用户手动引导实现。找不到 `claude` CLI 时自动降级到 `plan` 模式。
324
+
325
+ **`plan` 模式**
326
+
327
+ AI 输出详细的实施方案(文件清单 + 步骤顺序),不写入任何文件。适合人工开发参考。
328
+
329
+ ---
330
+
331
+ ### Step 6 — Code Review(代码审查)
332
+
333
+ 抓取 worktree 中的 git diff,与 Spec 一起发送给 AI,以架构师身份输出结构化审查报告。
334
+
335
+ ---
336
+
337
+ ## 配置文件
338
+
339
+ `.ai-spec.json` 存放在项目根目录,所有 `create` / `review` 命令都会自动读取:
340
+
341
+ ```json
342
+ {
343
+ "provider": "qwen",
344
+ "model": "qwen-max",
345
+ "codegen": "api",
346
+ "codegenProvider": "glm",
347
+ "codegenModel": "glm-4-plus"
348
+ }
349
+ ```
350
+
351
+ **优先级(从高到低):** 命令行参数 > `.ai-spec.json` > 内置默认值
352
+
353
+ ---
354
+
355
+ ## 全局安装
356
+
357
+ ```bash
358
+ npm run build
359
+ npm link
360
+
361
+ # 之后在任意项目目录使用
362
+ ai-spec model # 切换模型
363
+ ai-spec create "增加消息通知功能" # 开始开发
364
+ ai-spec review # 代码审查
365
+ ```
366
+
367
+ ---
368
+
369
+ ## 项目结构
370
+
371
+ ```
372
+ ai-spec-dev-poc/
373
+ ├── cli/
374
+ │ └── index.ts # CLI 入口,定义所有命令
375
+ ├── core/
376
+ │ ├── spec-generator.ts # 所有 AI Provider + SpecGenerator
377
+ │ ├── context-loader.ts # 项目上下文读取
378
+ │ ├── spec-refiner.ts # 交互式 Spec 润色循环
379
+ │ ├── code-generator.ts # 三模式代码生成器
380
+ │ └── reviewer.ts # AI 代码审查
381
+ ├── git/
382
+ │ └── worktree.ts # Git Worktree 管理
383
+ ├── prompts/
384
+ │ ├── spec.prompt.ts # Spec 生成 System Prompt
385
+ │ └── codegen.prompt.ts # 代码生成 / 审查 System Prompt
386
+ └── specs/ # 生成的 Spec 文件输出目录
387
+ ```
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node