Skip to content

Commit 22ec1d3

Browse files
author
huzijie.sea
committed
refactor(prompts): 重构系统提示构建为函数式入口
- 移除 SystemPrompt 类,统一使用 buildSystemPrompt 函数 - 更新相关文档和测试用例 - 优化 Plan 模式提示构建逻辑 - 添加工具权限模式支持
1 parent 323f438 commit 22ec1d3

13 files changed

Lines changed: 617 additions & 629 deletions

File tree

BLADE.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# BLADE.md
2+
3+
always respond in Chinese
4+
5+
你是一个专门帮助 Blade Code CLI 工具开发者的助手。
6+
7+
## 项目概述
8+
9+
**Blade Code** 是一个基于 TypeScript 开发的智能 AI 编程助手 CLI 工具,使用 Ink (React for CLI) 构建现代化的终端界面。项目采用模块化架构,核心是无状态的 Agent 设计,配合 Hook 系统实现可插拔的扩展机制。
10+
11+
- **语言**: TypeScript (ESM)
12+
- **运行时**: Node.js >=16.0.0
13+
- **包管理器**: Bun (主), 兼容 pnpm
14+
- **UI 框架**: Ink + React 19
15+
- **构建工具**: Bun 内置构建器
16+
- **测试框架**: Vitest
17+
18+
## 核心架构
19+
20+
### 1. Agent 系统 (`src/agent/`)
21+
- **Agent.ts**: 核心 Agent 类,无状态设计,负责 LLM 交互和工具执行
22+
- **ExecutionEngine.ts**: 执行引擎,处理工具调用循环
23+
- **LoopDetectionService.ts**: 循环检测,防止无限循环
24+
- **subagents/**: 子 Agent 注册表,支持任务分解
25+
26+
### 2. Hook 系统 (`src/hooks/`)
27+
可插拔的扩展机制,支持在工具执行前后插入自定义逻辑:
28+
- **HookManager.ts**: 单例管理器,负责 Hook 配置和执行
29+
- **HookExecutor.ts**: Hook 执行器
30+
- **Matcher.ts**: 匹配器,决定 Hook 是否触发
31+
- **SecureProcessExecutor.ts**: 安全进程执行器
32+
33+
### 3. Context 管理 (`src/context/`)
34+
统一管理会话和上下文:
35+
- **ContextManager.ts**: 核心上下文管理器,支持内存/持久化存储
36+
- **CompactionService.ts**: 上下文压缩服务,防止 token 超限
37+
- **TokenCounter.ts**: Token 计数器
38+
39+
### 4. Tool 系统 (`src/tools/`)
40+
- **builtin/**: 内置工具(文件操作、Git、网络等)
41+
- **registry/**: 工具注册表
42+
- **execution/**: 工具执行管线
43+
- **validation/**: 工具参数验证
44+
- **MCP 集成**: 支持 Model Context Protocol 扩展
45+
46+
### 5. 服务层 (`src/services/`)
47+
- **OpenAIChatService.ts**: OpenAI API 服务
48+
- **AnthropicChatService.ts**: Anthropic API 服务
49+
- **SessionService.ts**: 会话管理服务
50+
51+
### 6. Slash 命令 (`src/slash-commands/`)
52+
- **builtinCommands.ts**: 内置命令(/compact, /init, /model 等)
53+
- **UIActionMapper.ts**: UI 动作映射器
54+
55+
### 7. MCP 集成 (`src/mcp/`)
56+
- **McpRegistry.ts**: MCP 注册表
57+
- **McpClient.ts**: MCP 客户端
58+
- **loadProjectMcpConfig.ts**: 项目 MCP 配置加载
59+
60+
### 8. UI 层 (`src/ui/`)
61+
- **App.tsx**: 主应用组件
62+
- **components/**: UI 组件(BladeInterface, LoadingIndicator 等)
63+
- **contexts/**: React Context(SessionContext, AppContext 等)
64+
65+
### 9. 配置系统 (`src/config/`)
66+
- **ConfigManager.ts**: 配置管理器
67+
- 支持多层级配置(默认 → 用户 → 环境变量)
68+
69+
### 10. CLI 层 (`src/cli/`, `src/commands/`)
70+
- **blade.tsx**: 入口文件
71+
- **commands/**: 命令实现(config, doctor, mcp, update 等)
72+
73+
## 开发命令
74+
75+
### 构建与运行
76+
```bash
77+
# 开发模式(热重载)
78+
bun run dev
79+
80+
# 构建生产版本
81+
bun run build
82+
83+
# 运行构建后的版本
84+
bun run start
85+
86+
# 清理构建产物
87+
bun run clean
88+
```
89+
90+
### 测试
91+
```bash
92+
# 运行所有测试
93+
bun run test:all
94+
95+
# 单元测试
96+
bun run test:unit
97+
98+
# 集成测试
99+
bun run test:integration
100+
101+
# CLI 测试
102+
bun run test:cli
103+
104+
# 覆盖率测试
105+
bun run test:coverage
106+
107+
# 监听模式
108+
bun run test:watch
109+
110+
# CI 模式(带覆盖率)
111+
bun run test:ci
112+
```
113+
114+
### 代码质量
115+
```bash
116+
# 检查代码
117+
bun run check
118+
119+
# 自动修复
120+
bun run check:fix
121+
122+
# 格式化
123+
bun run format
124+
125+
# 格式化检查
126+
bun run format:check
127+
128+
# 类型检查
129+
bun run type-check
130+
131+
# 完整检查(类型 + lint + 格式化 + 测试)
132+
bun run check:full
133+
```
134+
135+
### 安全
136+
```bash
137+
# 安全审计
138+
bun run security:audit
139+
140+
# 安全测试
141+
bun run security:test
142+
```
143+
144+
### 发布
145+
```bash
146+
# 发布新版本(自动判断版本号)
147+
bun run release
148+
149+
# 预览发布
150+
bun run release:dry
151+
152+
# 指定版本号
153+
bun run release:major
154+
bun run release:minor
155+
bun run release:patch
156+
```
157+
158+
### MCP 相关
159+
```bash
160+
# 下载 ripgrep(用于文件搜索)
161+
bun run vendor:ripgrep
162+
163+
# 清理 ripgrep
164+
bun run vendor:ripgrep:clean
165+
```
166+
167+
### 预检(发布前运行)
168+
```bash
169+
bun run preflight
170+
```
171+
172+
## 关键设计模式
173+
174+
### 1. 无状态 Agent
175+
Agent 实例不保存会话状态,所有状态通过 context 参数传入。每次命令可以创建新实例,用完即弃。
176+
177+
### 2. Hook 系统
178+
- 支持 pre/post tool use Hook
179+
- 基于模式匹配触发
180+
- 可配置执行策略(顺序、并行、短路)
181+
182+
### 3. Context 管理
183+
- 三层存储:内存 → 缓存 → 持久化
184+
- 自动压缩防止 token 超限
185+
- 支持向量搜索(可选)
186+
187+
### 4. Tool 执行管线
188+
- 参数验证 → Hook 前置 → 执行 → Hook 后置 → 结果处理
189+
- 支持同步/异步工具
190+
- 自动错误处理
191+
192+
### 5. 配置优先级
193+
环境变量 > 用户配置 > 默认配置
194+
195+
## 开发指南
196+
197+
### 添加新工具
198+
1.`src/tools/builtin/` 创建工具文件
199+
2. 实现 Tool 接口
200+
3.`src/tools/builtin/index.ts` 注册
201+
202+
### 添加新 Hook
203+
1. 实现 Hook 接口
204+
2. 在 HookManager 中注册
205+
3. 配置触发规则
206+
207+
### 添加新 Slash 命令
208+
1.`src/slash-commands/` 创建命令文件
209+
2. 实现命令处理器
210+
3.`src/slash-commands/builtinCommands.ts` 注册
211+
212+
### 添加新 MCP 服务器
213+
1. 在项目根目录创建 `.blade/mcp.json`
214+
2. 配置服务器连接信息
215+
3. 工具自动加载
216+
217+
## 重要注意事项
218+
219+
1. **ESM 模块**: 项目使用 ESM,导入语句必须包含 `.js` 扩展名
220+
2. **类型安全**: 严格 TypeScript 配置,避免使用 `any`
221+
3. **错误处理**: 所有异步操作需要适当的错误处理
222+
4. **安全**: 工具执行需要权限验证,敏感操作需要用户确认
223+
5. **性能**: 注意 token 使用,大文件操作需要流式处理
224+
6. **测试**: 新功能必须包含测试用例
225+
7. **文档**: 公共 API 需要 JSDoc 注释
226+
227+
## 项目结构
228+
```
229+
src/
230+
├── agent/ # Agent 核心
231+
├── cli/ # CLI 配置
232+
├── commands/ # 命令实现
233+
├── config/ # 配置管理
234+
├── context/ # 上下文管理
235+
├── hooks/ # Hook 系统
236+
├── ide/ # IDE 集成
237+
├── logging/ # 日志系统
238+
├── mcp/ # MCP 集成
239+
├── prompts/ # 提示词管理
240+
├── services/ # 服务层
241+
├── slash-commands/ # Slash 命令
242+
├── tools/ # 工具系统
243+
├── ui/ # UI 组件
244+
└── utils/ # 工具函数
245+
```

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ All notable changes to this project will be documented in this file.
1313
-@vscode/ripgrep 改为可选依赖,减少包体积
1414
- 使用 picomatch 替代自制 glob 匹配实现
1515
- 添加 vendor ripgrep 支持(可选,~40-50MB)
16-
- 新增下载脚本: `npm run vendor:ripgrep`
17-
- 完整文档: `docs/development/implementation/grep-tool.md`
16+
- 新增下载脚本: `npm run vendor:ripgrep`
17+
- 完整文档: `docs/development/implementation/grep-tool.md`
18+
19+
### 🧹 移除过时组件
20+
21+
- 删除 `SystemPrompt` 类,统一改为函数式入口 `buildSystemPrompt``src/prompts/builder.ts`)。
22+
- 运行时覆盖:`--system-prompt` 完全替换,`--append-system-prompt` 追加。
23+
- Plan 模式提示:使用 `PLAN_MODE_SYSTEM_PROMPT`,并通过 `createPlanModeReminder` 注入提醒。
24+
- 影响范围:旧文档与测试已同步移除类引用,使用统一入口与配置字段。
1825

1926
### 📚 文档
2027

@@ -168,4 +175,3 @@ All notable changes to this project will be documented in this file.
168175
### 🔧 其他更改
169176

170177
- 临时禁用发布前的代码质量检查和测试 (e46031a)
171-

docs/development/planning/plan-mode-implementation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Use Markdown format for clarity.
361361
**修改位置 1**:文件顶部导入
362362

363363
```typescript
364-
import { PromptBuilder, PLAN_MODE_SYSTEM_PROMPT } from '../prompts/index.js';
364+
import { buildSystemPrompt, createPlanModeReminder } from '../prompts/index.js';
365365
```
366366

367367
**修改位置 2**:runLoop 方法(第 271-283 行)

docs/public/faq.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,17 @@ nvm use 20
196196
# 使用打印模式减少UI开销
197197
blade --print "问题"
198198

199-
# 限制上下文长度
200-
blade --max-tokens 1000 "问题"
199+
# 控制上下文与输出长度(通过配置)
200+
# 在 ~/.blade/config.json 或 .blade/config.json 中设置:
201+
{
202+
"maxContextTokens": 128000,
203+
"maxOutputTokens": 32768
204+
}
205+
206+
# 或使用 /compact 命令手动压缩上下文
207+
blade "/compact"
208+
209+
> 说明:CLI 不提供 `--max-tokens` 参数;请使用配置字段或上下文压缩命令控制长度。
201210

202211
# 清理缓存
203212
rm -rf ~/.blade/cache
@@ -273,6 +282,16 @@ blade
273282
# 然后输入自定义提示
274283
```
275284

285+
### Q: 文档里提到的 SystemPrompt 类是否还存在?
286+
287+
**A:** 已统一为函数式入口 `buildSystemPrompt`(位于 `src/prompts/builder.ts`)。
288+
289+
- 运行时覆盖:使用 CLI 参数 `--system-prompt`(完全替换)或 `--append-system-prompt`(追加)。
290+
- 程序化构建:调用 `buildSystemPrompt({ projectPath, replaceDefault, append, mode })`,返回 `{ prompt, sources }`
291+
- Plan 模式:使用 `PLAN_MODE_SYSTEM_PROMPT`,并通过 `createPlanModeReminder` 为每轮用户消息注入提醒。
292+
293+
> 迁移建议:如曾依赖“SystemPrompt 类”,直接改为调用 `buildSystemPrompt`,不再需要实例化或维护类状态。
294+
276295
## 📞 获取帮助
277296

278297
### 仍有问题?

0 commit comments

Comments
 (0)