stigmergy 1.2.12 → 1.3.1-beta
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 +39 -3
- package/STIGMERGY.md +3 -0
- package/config/builtin-skills.json +43 -0
- package/config/enhanced-cli-config.json +438 -0
- package/docs/CLI_TOOLS_AGENT_SKILL_ANALYSIS.md +463 -0
- package/docs/DESIGN_CLI_HELP_ANALYZER_REFACTOR.md +726 -0
- package/docs/ENHANCED_CLI_AGENT_SKILL_CONFIG.md +285 -0
- package/docs/IMPLEMENTATION_CHECKLIST_CLI_HELP_ANALYZER_REFACTOR.md +1268 -0
- package/docs/INSTALLER_ARCHITECTURE.md +257 -0
- package/docs/LESSONS_LEARNED.md +252 -0
- package/docs/SPECS_CLI_HELP_ANALYZER_REFACTOR.md +287 -0
- package/docs/SUDO_PROBLEM_AND_SOLUTION.md +529 -0
- package/docs/correct-skillsio-implementation.md +368 -0
- package/docs/development_guidelines.md +276 -0
- package/docs/independent-resume-implementation.md +198 -0
- package/docs/resumesession-final-implementation.md +195 -0
- package/docs/resumesession-usage.md +87 -0
- package/package.json +19 -9
- package/scripts/analyze-router.js +168 -0
- package/scripts/run-comprehensive-tests.js +230 -0
- package/scripts/run-quick-tests.js +90 -0
- package/scripts/test-runner.js +344 -0
- package/skills/resumesession/INDEPENDENT_SKILL.md +171 -0
- package/skills/resumesession/SKILL.md +127 -0
- package/skills/resumesession/__init__.py +33 -0
- package/skills/resumesession/implementations/simple-resume.js +13 -0
- package/src/adapters/claude/install_claude_integration.js +9 -1
- package/src/adapters/codebuddy/install_codebuddy_integration.js +3 -1
- package/src/adapters/codex/install_codex_integration.js +15 -5
- package/src/adapters/gemini/install_gemini_integration.js +3 -1
- package/src/adapters/qwen/install_qwen_integration.js +3 -1
- package/src/cli/commands/autoinstall.js +65 -0
- package/src/cli/commands/errors.js +190 -0
- package/src/cli/commands/independent-resume.js +395 -0
- package/src/cli/commands/install.js +179 -0
- package/src/cli/commands/permissions.js +108 -0
- package/src/cli/commands/project.js +485 -0
- package/src/cli/commands/scan.js +97 -0
- package/src/cli/commands/simple-resume.js +377 -0
- package/src/cli/commands/skills.js +158 -0
- package/src/cli/commands/status.js +113 -0
- package/src/cli/commands/stigmergy-resume.js +775 -0
- package/src/cli/commands/system.js +301 -0
- package/src/cli/commands/universal-resume.js +394 -0
- package/src/cli/router-beta.js +471 -0
- package/src/cli/utils/environment.js +75 -0
- package/src/cli/utils/formatters.js +47 -0
- package/src/cli/utils/skills_cache.js +92 -0
- package/src/core/cache_cleaner.js +1 -0
- package/src/core/cli_adapters.js +345 -0
- package/src/core/cli_help_analyzer.js +582 -26
- package/src/core/cli_path_detector.js +702 -709
- package/src/core/cli_tools.js +515 -160
- package/src/core/coordination/nodejs/CLIIntegrationManager.js +18 -0
- package/src/core/coordination/nodejs/HookDeploymentManager.js +242 -412
- package/src/core/coordination/nodejs/HookDeploymentManager.refactored.js +323 -0
- package/src/core/coordination/nodejs/generators/CLIAdapterGenerator.js +363 -0
- package/src/core/coordination/nodejs/generators/ResumeSessionGenerator.js +932 -0
- package/src/core/coordination/nodejs/generators/SkillsIntegrationGenerator.js +1395 -0
- package/src/core/coordination/nodejs/generators/index.js +12 -0
- package/src/core/enhanced_cli_installer.js +1208 -608
- package/src/core/enhanced_cli_parameter_handler.js +402 -0
- package/src/core/execution_mode_detector.js +222 -0
- package/src/core/installer.js +151 -106
- package/src/core/local_skill_scanner.js +732 -0
- package/src/core/multilingual/language-pattern-manager.js +1 -1
- package/src/core/skills/BuiltinSkillsDeployer.js +188 -0
- package/src/core/skills/StigmergySkillManager.js +123 -16
- package/src/core/skills/embedded-openskills/SkillParser.js +7 -3
- package/src/core/smart_router.js +291 -2
- package/src/index.js +10 -4
- package/src/utils.js +66 -7
- package/test/cli-integration.test.js +304 -0
- package/test/direct_smart_router_test.js +88 -0
- package/test/enhanced-cli-agent-skill-test.js +485 -0
- package/test/simple_test.js +82 -0
- package/test/smart_router_test_runner.js +123 -0
- package/test/smart_routing_edge_cases.test.js +284 -0
- package/test/smart_routing_simple_verification.js +139 -0
- package/test/smart_routing_verification.test.js +346 -0
- package/test/specific-cli-agent-skill-analysis.js +385 -0
- package/test/unit/smart_router.test.js +295 -0
- package/test/very_simple_test.js +54 -0
- package/src/cli/router.js +0 -1737
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
# ResumeSession 技能 - 正确的 Skills.io 规范实现
|
|
2
|
+
|
|
3
|
+
## 🎯 核心理解
|
|
4
|
+
|
|
5
|
+
**Skills.io 规范的正确理解**:
|
|
6
|
+
|
|
7
|
+
### 技能的本质
|
|
8
|
+
|
|
9
|
+
1. **技能是描述文件**(SKILL.md)
|
|
10
|
+
- 定义技能的名称、描述、用法
|
|
11
|
+
- 提供指导和最佳实践
|
|
12
|
+
- **不是可执行的程序或工具**
|
|
13
|
+
|
|
14
|
+
2. **技能由宿主 CLI 加载**
|
|
15
|
+
- 部署到宿主 CLI 的 skills 目录
|
|
16
|
+
- 例如:`~/.claude/skills/resumesession/`
|
|
17
|
+
- 宿主 CLI 读取并解析 SKILL.md
|
|
18
|
+
|
|
19
|
+
3. **实际执行由宿主 CLI 负责**
|
|
20
|
+
- 技能告诉宿主 CLI 如何执行某个功能
|
|
21
|
+
- 宿主 CLI 调用自己的代码来实现
|
|
22
|
+
- 或者宿主 CLI 调用外部工具/命令
|
|
23
|
+
|
|
24
|
+
## ❌ 错误的实现方式
|
|
25
|
+
|
|
26
|
+
### 1. 依赖 Stigmergy 的独立工具
|
|
27
|
+
- ❌ 技能文件中包含硬编码的 Stigmergy 路径
|
|
28
|
+
- ❌ 需要用户手动部署独立工具
|
|
29
|
+
- ❌ 违反了技能的定义(技能应该只是描述)
|
|
30
|
+
|
|
31
|
+
### 2. 技能本身就是可执行程序
|
|
32
|
+
- ❌ SKILL.md 包含可执行的 JavaScript 代码
|
|
33
|
+
- ❌ 技能变成了工具而不是指导
|
|
34
|
+
- ❌ 无法被宿主 CLI 正确管理和更新
|
|
35
|
+
|
|
36
|
+
## ✅ 正确的实现方式
|
|
37
|
+
|
|
38
|
+
### 方式 1:指导使用宿主 CLI 的原生功能(推荐)
|
|
39
|
+
|
|
40
|
+
**适用场景**:宿主 CLI 本身就有会话恢复功能
|
|
41
|
+
|
|
42
|
+
```markdown
|
|
43
|
+
---
|
|
44
|
+
name: resumesession
|
|
45
|
+
description: Cross-CLI session recovery and history management skill
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
# ResumeSession Skill
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Claude CLI
|
|
53
|
+
|
|
54
|
+
Claude CLI has built-in session history viewing. Use:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# View session history in Claude CLI
|
|
58
|
+
/history
|
|
59
|
+
|
|
60
|
+
# Search for specific content in history
|
|
61
|
+
/history search <keyword>
|
|
62
|
+
|
|
63
|
+
# Filter by time range
|
|
64
|
+
/history --today
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### iFlow CLI
|
|
68
|
+
|
|
69
|
+
iFlow CLI provides session management. Use:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# View iFlow session list
|
|
73
|
+
iflow sessions
|
|
74
|
+
|
|
75
|
+
# Resume a specific session
|
|
76
|
+
iflow resume <session-id>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Advanced Integration
|
|
80
|
+
|
|
81
|
+
For external tools when native capabilities are insufficient:
|
|
82
|
+
|
|
83
|
+
### External Script Integration
|
|
84
|
+
|
|
85
|
+
If you need to integrate an external session recovery tool:
|
|
86
|
+
|
|
87
|
+
1. **Create a script** in your project directory:
|
|
88
|
+
```bash
|
|
89
|
+
#!/bin/bash
|
|
90
|
+
# Custom session recovery script
|
|
91
|
+
node /path/to/external-tool.js
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
2. **Reference from skill**:
|
|
95
|
+
```bash
|
|
96
|
+
# Using Claude's native capabilities
|
|
97
|
+
bash /path/to/custom-script.sh
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
3. **Or use built-in commands**:
|
|
101
|
+
```bash
|
|
102
|
+
# Claude's built-in command
|
|
103
|
+
claude resume latest
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Notes
|
|
107
|
+
|
|
108
|
+
This skill provides guidance on session recovery methods. The actual implementation depends on:
|
|
109
|
+
- Host CLI capabilities
|
|
110
|
+
- Integration approach
|
|
111
|
+
- User requirements
|
|
112
|
+
|
|
113
|
+
**Best Practice**: Use the host CLI's native session management features whenever possible.
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**优点**:
|
|
117
|
+
- ✅ 符合 Skills.io 规范
|
|
118
|
+
- ✅ 技能只是描述和指导
|
|
119
|
+
- ✅ 利用宿主 CLI 原生功能
|
|
120
|
+
- ✅ 更好的性能和集成
|
|
121
|
+
- ✅ 易于维护和更新
|
|
122
|
+
|
|
123
|
+
**缺点**:
|
|
124
|
+
- ⚠️ 受限于宿主 CLI 的能力
|
|
125
|
+
- ⚠️ 如果宿主 CLI 功能有限,需要外部集成
|
|
126
|
+
|
|
127
|
+
### 方式 2:指导使用 Stigmergy 的跨 CLI 功能
|
|
128
|
+
|
|
129
|
+
**适用场景**:需要跨 CLI 会话比较和恢复
|
|
130
|
+
|
|
131
|
+
```markdown
|
|
132
|
+
---
|
|
133
|
+
name: resumesession
|
|
134
|
+
description: Cross-CLI session recovery and history management skill
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
# ResumeSession Skill
|
|
138
|
+
|
|
139
|
+
## Description
|
|
140
|
+
|
|
141
|
+
This skill enables session recovery across multiple CLI tools.
|
|
142
|
+
|
|
143
|
+
## Usage
|
|
144
|
+
|
|
145
|
+
### Using Stigmergy's Cross-CLI Capabilities
|
|
146
|
+
|
|
147
|
+
Stigmergy provides built-in session scanning across all supported CLIs:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
# Scan for latest session across all CLIs
|
|
151
|
+
stigmergy scan-sessions
|
|
152
|
+
|
|
153
|
+
# Find latest session from specific CLI
|
|
154
|
+
stigmergy scan-sessions --cli claude
|
|
155
|
+
|
|
156
|
+
# List all sessions with timestamps
|
|
157
|
+
stigmergy list-sessions
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Manual Session Recovery
|
|
161
|
+
|
|
162
|
+
If Stigmergy is not available or needs custom behavior:
|
|
163
|
+
|
|
164
|
+
1. **Direct File Access**:
|
|
165
|
+
```bash
|
|
166
|
+
# Claude sessions
|
|
167
|
+
ls -lt ~/.claude/projects/*/*.jsonl | head -1
|
|
168
|
+
|
|
169
|
+
# iFlow sessions
|
|
170
|
+
ls -lt ~/.iflow/projects/*/*.json | head -1
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
2. **Session Parsing**:
|
|
174
|
+
- Sessions are stored in `.jsonl` format (one JSON per line)
|
|
175
|
+
- Parse and display relevant messages
|
|
176
|
+
- Filter by date/time as needed
|
|
177
|
+
|
|
178
|
+
3. **Context Recovery**:
|
|
179
|
+
- Display session metadata (time, file name)
|
|
180
|
+
- Show recent conversation content
|
|
181
|
+
- Provide context for continuing
|
|
182
|
+
|
|
183
|
+
## Implementation Guide
|
|
184
|
+
|
|
185
|
+
### For Tool Developers
|
|
186
|
+
|
|
187
|
+
If implementing a session recovery tool:
|
|
188
|
+
|
|
189
|
+
1. **Make it CLI-friendly**:
|
|
190
|
+
```javascript
|
|
191
|
+
// Accept CLI arguments
|
|
192
|
+
// Use stdout/stderr for output
|
|
193
|
+
// Exit with appropriate codes
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
2. **Handle session formats**:
|
|
197
|
+
- JSONL (newline-delimited JSON)
|
|
198
|
+
- JSON (single session object)
|
|
199
|
+
- Session-specific formats
|
|
200
|
+
|
|
201
|
+
3. **Multi-CLI Support**:
|
|
202
|
+
- Support multiple CLI storage paths
|
|
203
|
+
- Detect available CLIs automatically
|
|
204
|
+
- Handle missing CLIs gracefully
|
|
205
|
+
|
|
206
|
+
## Notes
|
|
207
|
+
|
|
208
|
+
This skill provides methodology guidance for session recovery. Implementations may vary based on:
|
|
209
|
+
- Available CLI tools
|
|
210
|
+
- User environment
|
|
211
|
+
- Integration requirements
|
|
212
|
+
|
|
213
|
+
**Recommended**: Use Stigmergy's built-in cross-CLI session capabilities when available.
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**优点**:
|
|
217
|
+
- ✅ 完全符合 Skills.io 规范
|
|
218
|
+
- ✅ 技能只是描述和指导
|
|
219
|
+
- ✅ 灵活,可以适应不同宿主 CLI
|
|
220
|
+
- ✅ 易于维护
|
|
221
|
+
|
|
222
|
+
**缺点**:
|
|
223
|
+
- ⚠️ 需要宿主 CLI 有相关功能
|
|
224
|
+
- ⚠️ 可能需要多个技能版本(适配不同宿主)
|
|
225
|
+
|
|
226
|
+
### 方式 3:混合方式(推荐用于 Stigmergy)
|
|
227
|
+
|
|
228
|
+
**适用场景**:Stigmergy 需要提供自己的会话恢复能力
|
|
229
|
+
|
|
230
|
+
```markdown
|
|
231
|
+
---
|
|
232
|
+
name: resumesession
|
|
233
|
+
description: Cross-CLI session recovery and history management skill
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
# ResumeSession Skill for Stigmergy
|
|
237
|
+
|
|
238
|
+
## Description
|
|
239
|
+
|
|
240
|
+
This skill integrates with Stigmergy's cross-CLI session management system.
|
|
241
|
+
|
|
242
|
+
## Usage
|
|
243
|
+
|
|
244
|
+
### Stigmergy Commands
|
|
245
|
+
|
|
246
|
+
Stigmergy provides built-in session recovery commands:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# Find and recover latest session (default)
|
|
250
|
+
stigmergy resume
|
|
251
|
+
|
|
252
|
+
# List all sessions
|
|
253
|
+
stigmergy resume --list
|
|
254
|
+
|
|
255
|
+
# Show summary only
|
|
256
|
+
stigmergy resume --summary
|
|
257
|
+
|
|
258
|
+
# Filter by CLI
|
|
259
|
+
stigmergy resume --cli claude
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Integration with Stigmergy
|
|
263
|
+
|
|
264
|
+
This skill works with Stigmergy's native session recovery system. No external dependencies needed.
|
|
265
|
+
|
|
266
|
+
## Implementation
|
|
267
|
+
|
|
268
|
+
The session recovery is handled by Stigmergy's core system:
|
|
269
|
+
- Cross-CLI path detection
|
|
270
|
+
- Session file parsing
|
|
271
|
+
- Time-based comparison
|
|
272
|
+
- Content extraction and formatting
|
|
273
|
+
|
|
274
|
+
## Notes
|
|
275
|
+
|
|
276
|
+
This skill is designed specifically for Stigmergy CLI. It leverages Stigmergy's built-in cross-CLI session management capabilities.
|
|
277
|
+
|
|
278
|
+
**Advantages**:
|
|
279
|
+
- ✅ Native integration with Stigmergy
|
|
280
|
+
- ✅ No external dependencies
|
|
281
|
+
- ✅ Consistent with Stigmergy's architecture
|
|
282
|
+
- ✅ Automatic updates with Stigmergy
|
|
283
|
+
|
|
284
|
+
**Disadvantages**:
|
|
285
|
+
- ❌ Only works with Stigmergy CLI
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
**优点**:
|
|
289
|
+
- ✅ 最符合 Skills.io 规范(技能是描述)
|
|
290
|
+
- ✅ 与宿主系统(Stigmergy)完美集成
|
|
291
|
+
- ✅ 利用 Stigmergy 的原生能力
|
|
292
|
+
- ✅ 自动更新,无需用户干预
|
|
293
|
+
|
|
294
|
+
## 📋 技能文件结构(正确的 Skills.io 规范)
|
|
295
|
+
|
|
296
|
+
### 正确的技能文件
|
|
297
|
+
|
|
298
|
+
```markdown
|
|
299
|
+
---
|
|
300
|
+
name: resumesession
|
|
301
|
+
description: 技能的简短描述
|
|
302
|
+
author: 作者名
|
|
303
|
+
version: 1.0.0
|
|
304
|
+
---
|
|
305
|
+
|
|
306
|
+
# 技能标题
|
|
307
|
+
|
|
308
|
+
## Description
|
|
309
|
+
|
|
310
|
+
技能的详细描述。
|
|
311
|
+
|
|
312
|
+
## Usage
|
|
313
|
+
|
|
314
|
+
### 基本用法
|
|
315
|
+
命令示例
|
|
316
|
+
|
|
317
|
+
### 高级用法
|
|
318
|
+
高级选项和参数
|
|
319
|
+
|
|
320
|
+
## Notes
|
|
321
|
+
注意事项和最佳实践
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### 关键原则
|
|
325
|
+
|
|
326
|
+
1. **技能是描述性文件**(SKILL.md)
|
|
327
|
+
- 定义技能的用途
|
|
328
|
+
- 说明如何使用
|
|
329
|
+
- 不包含可执行代码
|
|
330
|
+
|
|
331
|
+
2. **技能被宿主 CLI 加载**
|
|
332
|
+
- 部署到 `~/.claude/skills/`
|
|
333
|
+
- 宿主 CLI 读取并解析
|
|
334
|
+
- 根据技能内容执行相应操作
|
|
335
|
+
|
|
336
|
+
3. **实际执行由宿主 CLI 负责**
|
|
337
|
+
- 技能描述如何使用宿主 CLI 的功能
|
|
338
|
+
- 或者描述如何调用外部工具
|
|
339
|
+
- 执行逻辑在宿主 CLI 的代码中
|
|
340
|
+
|
|
341
|
+
## 🎯 对比总结
|
|
342
|
+
|
|
343
|
+
| 方面 | 错误方式(独立工具) | 正确方式(Skills.io 规范)|
|
|
344
|
+
|------|---------------------|-------------------------|
|
|
345
|
+
| 技能本质 | ❌ 技能是工具 | ✅ 技能是描述文件 |
|
|
346
|
+
| 部署方式 | ❌ 单独部署 npm 包 | ✅ 部署到宿主 CLI |
|
|
347
|
+
| 执行方式 | ❌ 直接执行 JS 脚本 | ✅ 宿主 CLI 执行 |
|
|
348
|
+
| 集成度 | ❌ 无集成,独立运行 | ✅ 与宿主系统集成 |
|
|
349
|
+
| 维护性 | ❌ 需要手动更新 | ✅ 随宿主 CLI 自动更新 |
|
|
350
|
+
| 规范符合 | ❌ 不符合 | ✅ 完全符合 |
|
|
351
|
+
| 适用范围 | ⚠️ 适用于所有 CLI | ⚠️ 受限于宿主 CLI |
|
|
352
|
+
|
|
353
|
+
## 🎯 最终推荐
|
|
354
|
+
|
|
355
|
+
**推荐实现方式**:方式 3(与 Stigmergy 混合)
|
|
356
|
+
|
|
357
|
+
理由:
|
|
358
|
+
1. ✅ 符合 Skills.io 规范
|
|
359
|
+
2. ✅ 利用 Stigmergy 的原生能力
|
|
360
|
+
3. ✅ 不依赖外部工具
|
|
361
|
+
4. ✅ 自动更新和集成
|
|
362
|
+
5. ✅ 最适合当前项目架构
|
|
363
|
+
|
|
364
|
+
**实施步骤**:
|
|
365
|
+
1. 修改 `SKILL.md` 为方式 3 的格式
|
|
366
|
+
2. 确保 Stigmergy 有 `resume` 或类似的会话恢复命令
|
|
367
|
+
3. 测试跨 CLI 会话发现和恢复功能
|
|
368
|
+
4. 部署技能到目标 CLI
|
|
@@ -389,4 +389,280 @@ class CacheManager {
|
|
|
389
389
|
}
|
|
390
390
|
```
|
|
391
391
|
|
|
392
|
+
## 10. 代码生成规范
|
|
393
|
+
|
|
394
|
+
### 10.1 模板字符串中的正则表达式 ⚠️ 极其重要!
|
|
395
|
+
|
|
396
|
+
#### 🔴 致命陷阱:正则表达式字面量 vs RegExp 构造函数
|
|
397
|
+
|
|
398
|
+
**问题场景**:在模板字符串中生成包含正则表达式的代码
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
// ❌ 错误示例
|
|
402
|
+
const code = `
|
|
403
|
+
const cleanInput = input.replace(/^\\\\/?${commandName}\\s*/i, '').trim();
|
|
404
|
+
`;
|
|
405
|
+
|
|
406
|
+
// 当 commandName = "/stigmergy-resume" 时
|
|
407
|
+
// 生成:/^\\/?\/stigmergy-resume\s*/i
|
|
408
|
+
// ↑ 正则被 "/stigmergy-resume" 中的 / 提前关闭!
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
**错误原因分析**:
|
|
412
|
+
|
|
413
|
+
1. **正则字面量语法冲突**:`/pattern/` 首尾的 `/` 是定界符
|
|
414
|
+
2. **命令名中的 `/` 破坏语法**:`/^\\/?\/stigmergy-resume` 中第一个 `/` 关闭了正则
|
|
415
|
+
3. **模板字符串插值错误**:使用 `\${variable}` 会阻止插值!
|
|
416
|
+
|
|
417
|
+
#### ✅ 正确做法:使用 RegExp 构造函数
|
|
418
|
+
|
|
419
|
+
```javascript
|
|
420
|
+
// ✅ 正确示例
|
|
421
|
+
const code = `
|
|
422
|
+
const cleanInput = input.replace(
|
|
423
|
+
new RegExp('^\\\\\\\\/?' + '${commandName}' + '\\\\\s*', 'i'),
|
|
424
|
+
''
|
|
425
|
+
).trim();
|
|
426
|
+
`;
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
**转义计算公式**:
|
|
430
|
+
|
|
431
|
+
```
|
|
432
|
+
源文件中的反斜杠数 = 目标反斜杠数 × 2^(嵌套层数)
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
| 目标 | 在模板字符串中写 | 生成后 | 说明 |
|
|
436
|
+
|------|------------------|--------|------|
|
|
437
|
+
| `\s` | `\\s` | `\s` | 模板转义一次 |
|
|
438
|
+
| `\\s` | `\\\\s` | `\\s` | 模板转义一次 |
|
|
439
|
+
| `\\\s` | `\\\\\\s` | `\\s` | 字符串中为 `\\s` |
|
|
440
|
+
|
|
441
|
+
#### 📋 必须遵守的规则
|
|
442
|
+
|
|
443
|
+
1. **动态模式 → 用 RegExp 构造函数**
|
|
444
|
+
```javascript
|
|
445
|
+
new RegExp(pattern + variable + pattern, flags)
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
2. **静态模式 → 可以用正则字面量**
|
|
449
|
+
```javascript
|
|
450
|
+
const staticRegex = /\s+/; // 简单清晰
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
3. **永远不要阻止插值**
|
|
454
|
+
```javascript
|
|
455
|
+
// ❌ 错误:阻止插值
|
|
456
|
+
\${commandName} // 生成字面量 "${commandName}"
|
|
457
|
+
|
|
458
|
+
// ✅ 正确:允许插值
|
|
459
|
+
${commandName} // 生成实际值 "/stigmergy-resume"
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
4. **不要在字符串中转义 `/`**
|
|
463
|
+
```javascript
|
|
464
|
+
// ❌ 不必要
|
|
465
|
+
'\/path' // 在字符串中不需要转义 /
|
|
466
|
+
|
|
467
|
+
// ✅ 正确
|
|
468
|
+
'/path' // 字符串中的 / 不需要转义
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
#### 🛠️ 调试工具
|
|
472
|
+
|
|
473
|
+
```javascript
|
|
474
|
+
/**
|
|
475
|
+
* 验证生成的代码是否有语法错误
|
|
476
|
+
*/
|
|
477
|
+
function validateGeneratedCode(code) {
|
|
478
|
+
try {
|
|
479
|
+
new Function(code);
|
|
480
|
+
return true;
|
|
481
|
+
} catch (e) {
|
|
482
|
+
console.error('语法错误:', e.message);
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* 计算需要的反斜杠数量
|
|
489
|
+
*/
|
|
490
|
+
function calculateBackslashes(targetCount, nestingDepth = 1) {
|
|
491
|
+
return '\\'.repeat(targetCount * Math.pow(2, nestingDepth));
|
|
492
|
+
}
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
#### 📝 代码审查检查清单
|
|
496
|
+
|
|
497
|
+
涉及 **模板字符串 + 正则表达式** 的代码必须检查:
|
|
498
|
+
|
|
499
|
+
- [ ] 所有 `${variable}` 是否真的需要插值(不是 `\${variable}`)
|
|
500
|
+
- [ ] 如果模式包含动态的 `/`,是否使用了 RegExp 构造函数
|
|
501
|
+
- [ ] 反斜杠数量是否经过计算(而非猜测)
|
|
502
|
+
- [ ] 是否有语法验证测试
|
|
503
|
+
- [ ] 是否注释说明了转义的计算过程
|
|
504
|
+
|
|
505
|
+
#### 💡 永远记住
|
|
506
|
+
|
|
507
|
+
> **在模板字符串中生成包含正则的代码时,使用 RegExp 构造函数 + 字符串拼接,永远比尝试计算正确的正则字面量转义更安全!**
|
|
508
|
+
|
|
509
|
+
```javascript
|
|
510
|
+
// ✅ 安全、清晰、可维护
|
|
511
|
+
new RegExp('pattern' + variable + 'pattern', 'flags')
|
|
512
|
+
|
|
513
|
+
// ❌ 危险、复杂、易出错
|
|
514
|
+
/pattern${variable}pattern/flags
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### 10.2 代码生成最佳实践
|
|
518
|
+
|
|
519
|
+
#### 10.2.1 生成器模式
|
|
520
|
+
|
|
521
|
+
```javascript
|
|
522
|
+
class CodeGenerator {
|
|
523
|
+
/**
|
|
524
|
+
* 生成CLI集成代码
|
|
525
|
+
* @param {string} cliName - CLI工具名称
|
|
526
|
+
* @returns {string} 生成的代码
|
|
527
|
+
*/
|
|
528
|
+
generateForCLI(cliName) {
|
|
529
|
+
const commandName = this.getCommandName(cliName);
|
|
530
|
+
|
|
531
|
+
return `
|
|
532
|
+
// Auto-generated by Stigmergy - DO NOT EDIT
|
|
533
|
+
const handler = {
|
|
534
|
+
commandName: '${commandName}',
|
|
535
|
+
|
|
536
|
+
async handle(input) {
|
|
537
|
+
const cleanInput = input.replace(
|
|
538
|
+
new RegExp('^\\\\\\\\/?' + '${commandName}' + '\\\\\s*', 'i'),
|
|
539
|
+
''
|
|
540
|
+
).trim();
|
|
541
|
+
|
|
542
|
+
// 处理逻辑...
|
|
543
|
+
return { response: 'OK' };
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
module.exports = handler;
|
|
548
|
+
`;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* 获取命令名称
|
|
553
|
+
*/
|
|
554
|
+
getCommandName(cliName) {
|
|
555
|
+
const needsSlash = ['claude', 'codebuddy'].includes(cliName.toLowerCase());
|
|
556
|
+
return needsSlash ? `/stigmergy-resume` : 'stigmergy-resume';
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
#### 10.2.2 测试生成代码
|
|
562
|
+
|
|
563
|
+
```javascript
|
|
564
|
+
const { CodeGenerator } = require('./generators');
|
|
565
|
+
|
|
566
|
+
describe('CodeGenerator', () => {
|
|
567
|
+
test('should generate valid JavaScript syntax', () => {
|
|
568
|
+
const generator = new CodeGenerator();
|
|
569
|
+
const code = generator.generateForCLI('claude');
|
|
570
|
+
|
|
571
|
+
// 验证语法
|
|
572
|
+
expect(() => new Function(code)).not.toThrow();
|
|
573
|
+
|
|
574
|
+
// 验证包含关键部分
|
|
575
|
+
expect(code).toContain('new RegExp');
|
|
576
|
+
expect(code).toContain('/stigmergy-resume');
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
test('should escape regex correctly', () => {
|
|
580
|
+
const generator = new CodeGenerator();
|
|
581
|
+
const code = generator.generateForCLI('claude');
|
|
582
|
+
|
|
583
|
+
// 提取并验证正则表达式
|
|
584
|
+
const regexMatch = code.match(/new RegExp\('([^']+)',\s*'i'\)/);
|
|
585
|
+
expect(regexMatch).toBeTruthy();
|
|
586
|
+
|
|
587
|
+
// 测试生成的正则是否工作
|
|
588
|
+
const pattern = regexMatch[1];
|
|
589
|
+
const regex = new RegExp(pattern, 'i');
|
|
590
|
+
expect(regex.test('/stigmergy-resume')).toBe(true);
|
|
591
|
+
expect(regex.test('\\/stigmergy-resume')).toBe(true);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### 10.3 常见错误案例
|
|
597
|
+
|
|
598
|
+
#### 案例1:阻止插值
|
|
599
|
+
|
|
600
|
+
```javascript
|
|
601
|
+
// ❌ 错误
|
|
602
|
+
const commandName = '/stigmergy-resume';
|
|
603
|
+
const code = `input.replace(/^\\\\/?\${commandName}\\s*/i, '')`;
|
|
604
|
+
// 生成:input.replace(/^\\\\/?${commandName}\s*/i, '')
|
|
605
|
+
// ↑ 未插值!
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
```javascript
|
|
609
|
+
// ✅ 正确
|
|
610
|
+
const commandName = '/stigmergy-resume';
|
|
611
|
+
const code = `input.replace(new RegExp('^\\\\\\\\/?' + '${commandName}' + '\\\\\s*', 'i'), '')`;
|
|
612
|
+
// 生成:input.replace(new RegExp('^\\\\/?' + '/stigmergy-resume' + '\s*', 'i'), '')
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
#### 案例2:正则字面量中的 `/` 冲突
|
|
616
|
+
|
|
617
|
+
```javascript
|
|
618
|
+
// ❌ 错误
|
|
619
|
+
const commandName = '/stigmergy-resume';
|
|
620
|
+
const code = `input.replace(/\\/?${commandName}\\s*/i, '')`;
|
|
621
|
+
// 生成:input.replace(/\/?\/stigmergy-resume\s*/i, '')
|
|
622
|
+
// ↑ 第一个 / 关闭了正则
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
```javascript
|
|
626
|
+
// ✅ 正确
|
|
627
|
+
const commandName = '/stigmergy-resume';
|
|
628
|
+
const code = `input.replace(new RegExp('^\\\\\\\\/?' + '${commandName}' + '\\\\\s*', 'i'), '')`;
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
### 10.4 历史教训记录
|
|
632
|
+
|
|
633
|
+
#### 2025-12-25: ResumeSessionGenerator 正则转义错误
|
|
634
|
+
|
|
635
|
+
**问题描述**:
|
|
636
|
+
生成的 `resumesession-history.js` 文件包含语法错误,导致无法加载。
|
|
637
|
+
|
|
638
|
+
**错误代码**(Line 537, 627):
|
|
639
|
+
```javascript
|
|
640
|
+
const cleanInput = input.replace(/^\\\\/?\${commandName}\\s*/i, '').trim();
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
**错误原因**:
|
|
644
|
+
1. `\${commandName}` 阻止了模板插值
|
|
645
|
+
2. 正则字面量中的 `/` 与命令名中的 `/` 冲突
|
|
646
|
+
3. 反斜杠转义计算错误
|
|
647
|
+
|
|
648
|
+
**修复方案**:
|
|
649
|
+
```javascript
|
|
650
|
+
const cleanInput = input.replace(
|
|
651
|
+
new RegExp('^\\\\\\\\/?' + '${commandName}' + '\\\\\s*', 'i'),
|
|
652
|
+
''
|
|
653
|
+
).trim();
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
**影响范围**:
|
|
657
|
+
- 影响:所有CLI工具的ResumeSession集成
|
|
658
|
+
- 修复版本:v1.3.2-beta.3
|
|
659
|
+
- 调试时间:约2小时
|
|
660
|
+
- 失败尝试:3次
|
|
661
|
+
|
|
662
|
+
**经验总结**:
|
|
663
|
+
- 永远使用 RegExp 构造函数处理动态正则
|
|
664
|
+
- 永远不要在模板字符串中用 `\${` 阻止插值(除非真的需要字面量)
|
|
665
|
+
- 反斜杠数量必须通过公式计算,不能猜测
|
|
666
|
+
- 必须添加语法验证测试
|
|
667
|
+
|
|
392
668
|
遵循这些开发规范可以确保代码质量、可维护性和团队协作效率。
|