autosnippet 2.0.2 → 2.4.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.
Files changed (47) hide show
  1. package/README.md +189 -113
  2. package/bin/api-server.js +1 -4
  3. package/bin/cli.js +1 -50
  4. package/config/constitution.yaml +33 -107
  5. package/dashboard/dist/assets/{icons-B4FfLfBA.js → icons-B5rs8uNb.js} +85 -80
  6. package/dashboard/dist/assets/index-0YzLw2ga.css +1 -0
  7. package/dashboard/dist/assets/index-B9py3ybr.js +154 -0
  8. package/dashboard/dist/index.html +3 -3
  9. package/lib/bootstrap.js +5 -31
  10. package/lib/cli/SetupService.js +16 -14
  11. package/lib/core/capability/CapabilityProbe.js +8 -6
  12. package/lib/core/constitution/Constitution.js +13 -4
  13. package/lib/core/constitution/ConstitutionValidator.js +106 -211
  14. package/lib/core/gateway/Gateway.js +34 -98
  15. package/lib/core/gateway/GatewayActionRegistry.js +12 -1
  16. package/lib/core/permission/PermissionManager.js +2 -2
  17. package/lib/external/mcp/McpServer.js +4 -7
  18. package/lib/external/mcp/handlers/bootstrap.js +13 -1
  19. package/lib/external/mcp/handlers/browse.js +0 -7
  20. package/lib/external/mcp/handlers/candidate.js +1 -1
  21. package/lib/external/mcp/handlers/guard.js +11 -0
  22. package/lib/external/mcp/handlers/skill.js +186 -18
  23. package/lib/external/mcp/tools.js +40 -1
  24. package/lib/http/middleware/roleResolver.js +1 -1
  25. package/lib/http/routes/auth.js +2 -2
  26. package/lib/http/routes/commands.js +58 -3
  27. package/lib/http/routes/monitoring.js +4 -4
  28. package/lib/http/routes/recipes.js +96 -4
  29. package/lib/http/routes/search.js +34 -35
  30. package/lib/injection/ServiceContainer.js +21 -40
  31. package/lib/service/candidate/CandidateService.js +12 -1
  32. package/lib/service/chat/ChatAgent.js +171 -30
  33. package/lib/service/chat/Memory.js +104 -0
  34. package/lib/service/chat/tools.js +244 -10
  35. package/lib/service/guard/GuardCheckEngine.js +9 -1
  36. package/lib/service/knowledge/KnowledgeGraphService.js +20 -9
  37. package/lib/service/recipe/RecipeService.js +8 -0
  38. package/lib/service/skills/SkillHooks.js +126 -0
  39. package/package.json +1 -1
  40. package/scripts/init-db.js +1 -2
  41. package/templates/constitution.yaml +29 -85
  42. package/dashboard/dist/assets/index-ChxJxX4B.js +0 -154
  43. package/dashboard/dist/assets/index-DwAp1mx5.css +0 -1
  44. package/lib/core/session/SessionManager.js +0 -232
  45. package/lib/infrastructure/logging/ReasoningLogger.js +0 -269
  46. package/lib/infrastructure/monitoring/RoleDriftMonitor.js +0 -259
  47. package/lib/infrastructure/quality/ComplianceEvaluator.js +0 -326
package/README.md CHANGED
@@ -1,168 +1,244 @@
1
- # AutoSnippet — Project Knowledge Engine
1
+ <div align="center">
2
2
 
3
- 连接开发者、AI 与项目知识库:人工审核沉淀标准,知识库存储 Recipe + Snippet,AI 按规范生成代码。基于 SPM,打通 Xcode 补全与 Cursor 按需检索。
3
+ # AutoSnippet
4
+
5
+ **Project Knowledge Engine for iOS / Swift Teams**
6
+
7
+ 将团队的代码模式、最佳实践沉淀为 AI 可检索的知识库,<br>
8
+ 让 Cursor、Copilot 和 Xcode 都按你的项目规范生成代码。
4
9
 
5
10
  [![npm version](https://img.shields.io/npm/v/autosnippet.svg?style=flat-square)](https://www.npmjs.com/package/autosnippet)
6
11
  [![License](https://img.shields.io/npm/l/autosnippet.svg?style=flat-square)](https://github.com/GxFn/AutoSnippet/blob/main/LICENSE)
12
+ [![Node](https://img.shields.io/badge/node-%E2%89%A520-brightgreen?style=flat-square)](https://nodejs.org)
13
+
14
+ </div>
7
15
 
8
16
  ---
9
17
 
10
- ### 开发者、AI 与知识库
18
+ ## 为什么需要 AutoSnippet?
11
19
 
12
- | 角色 | 职责 | 能力 |
13
- |------|------|------|
14
- | **开发者** | 审核与决策;维护项目标准 | Dashboard 审核 Candidate,保存 Recipe;使用 Snippet 补全、`ass` 快捷联想或 `// as:search` 插入;运行 `asd embed`、`asd ui` |
15
- | **Cursor Agent** | 按规范生成代码;检索与提交 | Skills 理解规范;MCP 按需检索、打开新建 Recipe 页;`autosnippet_submit_candidates` 批量提交候选供人工审核;不直接改 Knowledge |
16
- | **项目内 AI** | 提取、摘要、扫描、审查 | `asd ais` 扫描;Use Copied Code 分析填充;Guard 审查;Dashboard RAG;深度扫描结果可算相似度。由 `.env` 配置 |
17
- | **知识库** | 存储与提供项目标准 | Recipes、Snippets、语义向量索引;Guard、搜索、质量评估、相似度分析;两种 AI 的上下文均依赖此 |
20
+ AI 编码助手生成的代码往往脱离项目上下文——不知道团队约定、不了解架构模式、也不遵守代码规范。AutoSnippet 在你的项目中建立一个**活的知识库**,让所有 AI 工具都能检索并遵循团队沉淀的最佳实践。
18
21
 
19
- ---
22
+ ```
23
+ 你的项目代码 ──→ AI 扫描提取 ──→ 人工审核 ──→ 知识库 (Recipe)
24
+
25
+ ┌───────────────────────────────────────┘
26
+
27
+ Cursor / Copilot / Xcode ──→ 按规范生成代码
28
+ ```
20
29
 
21
- ## 安装与快速开始
30
+ ## 核心概念
22
31
 
23
- ### 1. 安装 AutoSnippet
32
+ | 概念 | 说明 |
33
+ |------|------|
34
+ | **Recipe** | 知识库的基本单元——一段代码模式 + 使用说明 + 元数据,存储在数据库中,可导出为 Markdown 到 `AutoSnippet/recipes/` |
35
+ | **Candidate** | 待审核的候选知识——来自 AI 扫描、手动提交或剪贴板,经 Dashboard 人工审核后晋升为 Recipe |
36
+ | **Dashboard** | Web 管理后台(`asd ui`),管理 Recipe / Candidate / Guard / 知识图谱等 |
37
+ | **Guard** | 代码审查引擎——基于知识库中的规则对代码做合规检查 |
38
+ | **Skills** | Cursor Agent 技能文件——告诉 AI 何时、如何使用 AutoSnippet 的能力 |
24
39
 
25
- > **要求**:Node.js ≥ 20
40
+ ## 快速开始
26
41
 
27
42
  ```bash
43
+ # 1. 全局安装
28
44
  npm install -g autosnippet
45
+
46
+ # 2. 在你的项目目录初始化
47
+ cd /path/to/your-project
48
+ asd setup # 创建 AutoSnippet/ 目录,配置 VSCode / Cursor
49
+
50
+ # 3. 安装 IDE 集成(Skills + MCP + Cursor Rules)
51
+ asd install:full
52
+
53
+ # 4. 启动 Dashboard
54
+ asd ui # 启动 Web 后台 + 文件监听 + 语义索引
29
55
  ```
30
56
 
31
- ### 2. 在你的项目中初始化
57
+ > **注意**:始终在**你的项目目录**中执行 `asd` 命令,而非 AutoSnippet 源码仓库。
32
58
 
33
- **重要**:在**你的项目目录**(不是 AutoSnippet 源码目录)执行:
59
+ ## 工作流
34
60
 
35
- ```bash
36
- cd /path/to/your-project # 进入你的项目
61
+ ### 知识沉淀闭环
37
62
 
38
- asd setup # 一键初始化
39
- # ✅ 创建 AutoSnippet/ 目录和配置
40
- # ✅ 自动配置 VSCode (.vscode/settings.json)
41
- # 自动配置 Cursor (.cursor/mcp.json)
42
- # 放置 Recipe 模板
63
+ ```
64
+ ┌─────────────────────────────────────────────────────────────┐
65
+ │ │
66
+ │ ① 扫描提取 ② 人工审核 │
67
+ │ Cursor AI 扫描 Target ──→ Candidates ──→ Recipe 入库 │
68
+ │ asd ais <Target> Dashboard │
69
+ │ 剪贴板 / 手动提交 │
70
+ │ │
71
+ │ ③ AI 按规范生成 ④ 持续沉淀 │
72
+ │ Cursor/Copilot 检索 Recipe ──→ 生成代码 ──→ 好代码再入库 │
73
+ │ MCP 工具 / Xcode Snippet │
74
+ │ │
75
+ └─────────────────────────────────────────────────────────────┘
43
76
  ```
44
77
 
45
- ### 3. 启动 Dashboard
78
+ ### 三种使用方式
46
79
 
47
- ```bash
48
- asd ui # 启动 Dashboard + watch
80
+ **① Cursor AI(推荐)**:自然语言驱动,Cursor 通过 13 个 Skills + 34 个 MCP 工具与知识库交互。
81
+
82
+ ```
83
+ 用户:「扫描 NetworkModule 这个 Target,提取最佳实践」
84
+ Cursor → autosnippet_get_targets → autosnippet_get_target_files → 逐文件提取 → autosnippet_submit_candidates
85
+ → Dashboard Candidates 页面审核 → 保存为 Recipe
49
86
  ```
50
87
 
51
- `asd ui` 会启动 Web 管理后台并后台 watch;首次运行若前端不存在会自动构建。浏览器会自动打开 Dashboard。
88
+ **② Dashboard Web UI**:可视化管理一切——Recipe 编辑器、Candidate 审核、知识图谱、Guard 审查、冷启动向导。
52
89
 
53
- ![Dashboard 概览](https://cdn.jsdelivr.net/gh/GxFn/blog-images@main/autosnippet-manual/20260205232116_66_167.png)
90
+ **③ 命令行 CLI**:`asd` 命令覆盖全部操作,适合自动化和脚本集成。
54
91
 
92
+ ## Dashboard
55
93
 
56
- ## 核心流程
94
+ `asd ui` 启动后访问 Web 管理后台:
57
95
 
58
- **智能 AI 优先 → 前端操作 → 命令行补充**
96
+ ![Dashboard](./resources/ASImage01.png)
59
97
 
60
- 1. **Cursor AI 智能扫描**(推荐):在 Cursor 中输入自然语言(如「扫描这个 Target」、「批量提取代码候选」),AI 智能触发 `autosnippet-candidates` Skill,通过 MCP 工具 `autosnippet_get_targets` → `autosnippet_get_target_files` → 按文件提取 → `autosnippet_submit_candidates` 一键批量扫描,自动提交候选到 Dashboard Candidates
61
- 2. **前端审核与入库**:Dashboard Candidates 页面人工审核 → 保存 Recipe 入库(优先前端操作,无需命令行)
62
- 3. **依赖关系**(可选):Dashboard 刷新自动分析,或使用 `asd spm-map` 命令行更新
63
- 4. **语义索引**(自动):`asd ui` 启动时自动 embed;也可手动 `asd embed`
64
- 5. **Cursor 集成**(首次):`asd install:cursor-skill --mcp`(安装 Skills + Cursor 规则 `.cursor/rules/` + MCP;MCP 工具使用时需 `asd ui` 运行)
98
+ **主要功能**:
65
99
 
66
- ### 闭环
100
+ - **Recipes**:浏览、编辑、发布、弃用知识条目;支持代码高亮编辑器
101
+ - **Candidates**:审核 AI/手动提交的候选,一键入库或批量操作
102
+ - **知识图谱**:可视化 Recipe 之间的关系(依赖 / 扩展 / 冲突等),AI 自动发现关系,按 category 分组布局
103
+ - **Guard**:代码合规审查,查看违规记录与修复建议
104
+ - **Xcode 模拟器**:在浏览器中模拟 `as:search` / `as:create` / `as:audit` 指令
105
+ - **冷启动**:Bootstrap 向导——选择 SPM Target 逐个扫描,AI 提取代码模式生成 Candidate
67
106
 
68
- **AI 扫描 → Dashboard 审核 → 知识库沉淀 → Cursor/AI 按规范生成 → 再沉淀**:Cursor AI 通过自然语言指令智能扫描并批量提交候选,完整代码通过 Skill 智能提交,开发者也可通过 Dashboard Use Copied Code 或剪切板提交候选,所有候选经 Dashboard 人工审核进入知识库;知识库内 Recipe 为第一公民,拥有最高优先级。
107
+ ## IDE 集成
69
108
 
70
- Cursor 在编辑器内通过自然语言交互触发 Skill,使用 MCP 工具检索知识库,用 Recipe 作为生成代码的上下文标准;AI 生成的代码遵循 Recipe 规范,过审后成为新 Recipe,持续提升 AI 的开发标准化程度。
109
+ ### Cursor(推荐)
71
110
 
72
- 知识库随人工审核持续更新,AI 始终基于最新上下文,Recipe 会在使用中获得评级调整。
111
+ AutoSnippet Cursor 提供完整的 MCP + Skills 集成:
73
112
 
74
- ## 编辑器内指令
113
+ - **34 个 MCP 工具**:搜索、Guard 检查、候选提交、知识图谱查询、Bootstrap 等
114
+ - **13 个 Agent Skills**:`autosnippet-candidates`、`autosnippet-guard`、`autosnippet-coldstart` 等,引导 AI 正确使用工具
115
+ - **写操作 Gateway 保护**:7 个写操作经过权限 / 宪法 / 审计三重检查
75
116
 
76
- 需先运行 `asd watch` 或 `asd ui`。在源码中写入并保存:
117
+ ```bash
118
+ asd install:cursor-skill --mcp # 安装 Skills + MCP 配置
119
+ ```
77
120
 
78
- | 指令 | 作用 |
79
- |------|------|
80
- | `ass` / `// as:search` [关键词] | 从知识库检索并插入 Recipe/Snippet;`ass` 是最快捷的联想方式,优先推荐 |
81
- | `asc` / `// as:create` | 无选项时只打开 Dashboard(路径已填),由用户点 Scan File 或 Use Copied Code。`-c` 强制用剪切板(静默创建或打开);`-f` 强制用路径(打开 Dashboard 并自动执行 Scan File) |
82
- | `asa` / `// as:audit` [关键词或规模] | 按知识库 AI 审查;无后缀时仅检查当前文件;后缀 **file** / **target** / **project** 可扩大范围(target=当前 Target 内所有源文件,project=项目内所有源文件);其他为检索关键词 |
83
- | `// as:include` / `// as:import` | Snippet 内头文件/模块标记,保存时自动注入 |
121
+ ### VSCode Copilot
122
+
123
+ ```bash
124
+ asd install:vscode-copilot # 配置 MCP Copilot 指令
125
+ ```
126
+
127
+ ### Xcode
128
+
129
+ 通过 Xcode Code Snippet 触发:
84
130
 
85
- **Snippet 生效**:`ass`、`asc`、`asa` Xcode Snippet,需要执行 `asd setup` 将其注册到 Xcode 后,**彻底关闭 Xcode 并重启**才能生效。重启后在源码中输入首字母即可触发自动完成菜单。
86
- **静默候选**:在 Cursor 内用户提出保存案例,Cursor 生成草案;后台用草案静默创建候选,无需打开浏览器,到 Dashboard **Candidates** 页审核即可。在 Xcode 等编辑器内也可写 `asc -c`、复制代码后保存,剪贴板内容同样静默入库。
87
- **快捷联想**:输入 `ass`(AutoSnippet Search)触发 Snippet 快捷联想,或使用 `// as:search` / `// as:s` 在编辑器内弹窗选择,即选即插,无需跳转 Dashboard,不打断当前编辑。
131
+ | 触发关键词 | 作用 |
132
+ |-----------|------|
133
+ | `ass` | 搜索知识库并插入代码(最快捷的联想方式) |
134
+ | `asc` | 创建候选——打开 Dashboard 或从剪贴板静默提交 |
135
+ | `asa` | 按知识库审查当前代码 |
88
136
 
89
- ## 常用命令
137
+ > 执行 `asd setup` 注册 Snippet 后,需**重启 Xcode** 才生效。
138
+
139
+ ## CLI 命令参考
90
140
 
91
141
  | 命令 | 说明 |
92
142
  |------|------|
93
- | `asd setup` | 初始化项目根(创建 AutoSnippet/AutoSnippet.boxspec.json) |
94
- | `asd ui` | 启动 Dashboard + watch |
95
- | `asd status` | 环境自检(含项目根、AI、索引、Dashboard/Watch、Native UI) |
96
- | `asd create --clipboard` | 从剪贴板创建 Recipe/Snippet |
97
- | `asd candidate` | 从剪贴板创建候选(Dashboard 审核) |
98
- | `asd extract` / `asd e` | 同步 Snippets 到 Xcode |
99
- | `asd ais [Target]` | AI 扫描 Target → Candidates |
100
- | `asd search [keyword] --copy` | 搜索并复制第一条到剪贴板 |
101
- | `asd search [keyword] --pick` | 交互选择后复制/插入 |
102
- | `asd sync` | 增量同步 `recipes/*.md` → DB(.md = Source of Truth) |
103
- | `asd compliance` | 生成宪法合规评估报告(P1-P4 加权评分) |
104
- | `asd upgrade` | 升级 IDE 集成文件(MCP/Skills/Cursor Rules/Copilot) |
105
- | `asd install:cursor-skill --mcp` | 安装 Skills、Cursor 规则(`.cursor/rules/*.mdc`)并配置 MCP。配置时可运行;MCP 工具使用时需 `asd ui` 已启动 |
106
- | `asd install:full` | 全量安装(Skills、MCP、Native UI) |
107
- | `asd embed` | 手动构建语义向量索引(`asd ui` 启动时也会自动执行) |
108
- | `asd spm-map` | 刷新 SPM 依赖映射(依赖关系图数据来源) |
143
+ | `asd setup` | 初始化项目(创建 AutoSnippet/ 目录和配置) |
144
+ | `asd ui` | 启动 Dashboard + 文件监听 + 语义索引 |
145
+ | `asd status` | 环境自检(项目根、AI、索引、Dashboard 状态) |
146
+ | `asd ais [Target]` | AI 扫描 Target → 生成 Candidates |
147
+ | `asd search <keyword>` | 搜索知识库(`--copy` 复制 / `--pick` 交互选择) |
148
+ | `asd embed` | 手动构建语义向量索引 |
149
+ | `asd spm-map` | 刷新 SPM 依赖映射 |
150
+ | `asd extract` | 同步 Snippet 到 Xcode |
151
+ | `asd sync` | 增量同步 `recipes/*.md` DB |
152
+ | `asd compliance` | 生成宪法合规评估报告 |
153
+ | `asd upgrade` | 升级 IDE 集成文件 |
154
+ | `asd install:full` | 全量安装(Skills + MCP + Native UI) |
109
155
 
110
156
  ## 配置
111
157
 
112
- - **AI**:项目根 `.env`,参考 `.env.example` 配置 `ASD_GOOGLE_API_KEY` 等。可选 `ASD_AI_PROVIDER`、代理等。
113
- - **Native UI**(可选):macOS 上 `npm install` 会尝试构建 `resources/native-ui/native-ui`(需本机 Swift);未构建时回退到 AppleScript/inquirer,功能正常。
114
- - **权限设置**(可选):写权限探针(在子仓库执行 `git push --dry-run`,通过才允许保存 Recipe/Snippet,否则 403)+ 完整性校验(`asd` 启动前关键文件 SHA-256)。详见 [权限设置说明](docs/权限设置说明.md)。
158
+ ### AI Provider
159
+
160
+ 在项目根目录创建 `.env` 文件(参考 `.env.example`):
161
+
162
+ ```env
163
+ ASD_AI_PROVIDER=gemini # gemini / openai / anthropic
164
+ ASD_GOOGLE_API_KEY=your-key # Gemini API Key
165
+ ```
166
+
167
+ ### 项目目录结构
168
+
169
+ ```
170
+ your-project/
171
+ ├── AutoSnippet/ # 知识库目录(建议整体作为 Git 子仓库)
172
+ │ ├── recipes/ # Recipe Markdown 导出
173
+ │ └── .autosnippet/ # 数据库、索引、Guard 配置等
174
+ ├── .cursor/
175
+ │ ├── mcp.json # MCP 配置(asd setup 自动生成)
176
+ │ ├── rules/ # Cursor Rules(asd install 生成)
177
+ │ └── skills/ # Agent Skills(asd install 生成)
178
+ ├── .vscode/
179
+ │ └── settings.json # VSCode MCP 配置
180
+ └── .env # AI Provider 配置
181
+ ```
115
182
 
116
- ## 术语
183
+ ### Git 策略建议
117
184
 
118
- | 术语 | 说明 |
185
+ | 路径 | 建议 |
119
186
  |------|------|
120
- | **Recipe** | `AutoSnippet/recipes/` 下的 Markdown 知识(配方):含代码块 + 使用说明,供 AI 检索、Guard、搜索,**默认位置,用户可通过 boxspec `knowledgeBase.dir` 配置改成 `Knowledge/` 等**
121
- | **Snippet** | Xcode 代码片段,通过 trigger(默认 `@`)补全,可与 Recipe 关联 |
122
- | **Candidate(候选)** | 待审核入库的项;来自 `as:create`、MCP 提交、`asd ais` 扫描等,经 Dashboard 审核后保存为 Recipe/Snippet |
123
- | **Knowledge(AutoSnippet)** | 项目知识库目录,包含 `recipes/`、`.autosnippet/`(索引、candidates、guard 配置等);Snippet 配置在 root spec 的 list 中。|
124
- | **Dashboard** | Web 管理后台(`asd ui` 启动),含 Recipes、Candidates、Guard、Snippets 等页面 |
125
- | **watch** | 文件监听进程(`asd ui` 或 `asd watch` 启动),保存时触发 `as:create`、`as:audit`、`as:search` |
126
- | **Guard** | 按 Recipe 知识库对代码做 AI 审查;`// as:audit` 触发 |
127
- | **embed** | 语义向量索引构建;`asd embed` 或 `asd ui` 启动时自动执行,供语义检索与 MCP 使用 |
128
- | **MCP** | Model Context Protocol;Cursor 通过 MCP 调用 31 个工具(7 个写操作经 Gateway 权限保护) |
129
- | **Skills** | Cursor Agent Skills(`.cursor/skills/`),描述何时用、如何用 AutoSnippet 能力 |
130
- | **trigger** | Snippet 触发前缀,默认 `@`,输入后 Xcode 联想补全 |
131
- | **Gateway** | V2 控制平面,统一调度 27 个 Action:validate → permission → constitution → plugin → dispatch → audit |
132
- | **Constitution** | 宪法体系:6 角色、P1-P4 四优先级、能力探测,见 `config/constitution.yaml` |
133
- | **项目根** | 含 `AutoSnippetRoot.boxspec.json` 的目录 |
134
- | **Target** | SPM 模块/编译单元;`asd ais <Target>` 扫描该 Target 下的源码提取候选 |
135
-
136
- **详细介绍**:启动 `asd ui` 后访问 Dashboard → **使用说明** 页;
137
-
138
- ## AutoSnippet 目录与 Git
139
-
140
- AutoSnippet 下各路径与版本控制的关系建议如下(可按项目需要调整):
141
-
142
- | 路径 | 说明 | 建议 |
143
- |------|------|------|
144
- | **AutoSnippet/recipes/** | Recipe 的 Markdown 文件 | **Git 子仓库**:单独建远程仓库并 `git submodule add <url> AutoSnippet/recipes`,用于权限拦截(仅能 push 子仓库的人可保存/上传 Recipe)。详见 [权限设置说明](docs/权限设置说明.md) 中「只把 AutoSnippet/recipes 作为子仓库」。 |
145
- | **AutoSnippet/.autosnippet/** | Guard 规则、违反记录、candidates、recipe-stats、context 配置等 | **跟随主仓库 Git**:规则与配置建议提交到主仓库,便于团队共享。 |
146
- | **AutoSnippet/.autosnippet/context/index/** | 语义向量索引(embed 生成) | **不跟随 Git**:体积大、机器相关,建议加入 `.gitignore`(如 `AutoSnippet/.autosnippet/context/index/`)。 |
147
- | **AutoSnippet/.autosnippet/candidates/**(若存在) | 候选数据等 | 视需要:若仅本地缓存可不提交;若团队共享可跟随主仓库或单独子仓库。 |
148
- | **AutoSnippet/AutoSnippet.spmmap.json**(若存在) | SPM 依赖映射 | **跟随主仓库 Git**:便于依赖关系图一致。 |
149
-
150
- - **跟随主仓库 Git**:由主项目 `git add/commit/push` 管理,所有人按主仓库权限读写。
151
- - **Git 子仓库**:`AutoSnippet/recipes` 为单独仓库(submodule),Recipe 上传(git push)由 Git 服务端权限拦截。配合 `.env` 中 `ASD_RECIPES_WRITE_DIR=AutoSnippet/recipes` 是为了保识管理员(有 push 权限者)能够正确提交 Recipe:探针目录与 Recipe 写入目录一致,保存后可正常推送。
152
- - **不跟随 Git**:在 `.gitignore` 中忽略,不提交、不推送。
187
+ | `AutoSnippet/` | **整体作为 Git 子仓库**——独立权限控制,写权限探针(`git push --dry-run`)在此目录执行,仅知识管理员可 push |
188
+ | `AutoSnippet/.autosnippet/context/index/` | 加入 `.gitignore`——体积大、机器相关 |
153
189
 
154
- ---
190
+ ## 架构概览
155
191
 
156
- ## 为什么推荐 Cursor
192
+ ```
193
+ ┌────────────────────────────────────────────────────────┐
194
+ │ IDE Layer │
195
+ │ Cursor (Skills + MCP) │ VSCode (Copilot) │ Xcode │
196
+ └────────────┬───────────────────┬───────────────────────┘
197
+ │ MCP (stdio) │ HTTP API
198
+ ┌────────────┴───────────────────┴───────────────────────┐
199
+ │ AutoSnippet Core │
200
+ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌───────────┐ │
201
+ │ │ Gateway │ │ ChatAgent│ │ Bootstrap│ │ Dashboard │ │
202
+ │ │ (权限/ │ │ (ReAct + │ │ (SPM + │ │ (React + │ │
203
+ │ │ 宪法/ │ │ DAG │ │ AST + │ │ Vite) │ │
204
+ │ │ 审计) │ │ Pipeline│ │ AI) │ │ │ │
205
+ │ └──────────┘ └──────────┘ └──────────┘ └───────────┘ │
206
+ │ ┌──────────────────────────────────────────────────┐ │
207
+ │ │ Services: Recipe │ Candidate │ Guard │ Search │ │
208
+ │ │ Knowledge Graph │ SPM │ Indexing │ Compliance │ │
209
+ │ └──────────────────────────────────────────────────┘ │
210
+ │ ┌──────────────────────────────────────────────────┐ │
211
+ │ │ Storage: SQLite (better-sqlite3) + 向量索引 │ │
212
+ │ └──────────────────────────────────────────────────┘ │
213
+ └────────────────────────────────────────────────────────┘
214
+ ```
157
215
 
158
- **Cursor 优先**的原因:
216
+ ## 技术栈
159
217
 
160
- - **MCP & Skills 完整支持**:Cursor 对 MCP(Model Context Protocol)的集成完善,Skills 能够正常触发和运行,支持自然语言指令流畅地调用 `autosnippet_get_targets`、`autosnippet_get_target_files`、`autosnippet_submit_candidates` 等工具
161
- - **候选提交体验流畅**:整个从扫描 提取 提交的工作流无缝衔接,用户无需手动介入,一键完成批量操作
162
- - **VSCode Copilot 限制**:目前 VSCode Copilot MCP 集成存在问题,无法直接调用 MCP 工具。降级方案是存储候选为 Draft(本地),然后通过 API 提交,增加了用户额外操作步骤
218
+ - **Runtime**:Node.js 20,ESM
219
+ - **后端**:Express + better-sqlite3 + MCP SDK
220
+ - **前端**:React + TypeScript + Vite + Tailwind CSS
221
+ - **AI**:Gemini / OpenAI / Anthropic(通过 AiProvider 抽象层)
222
+ - **AST**:Tree-sitter(Swift / ObjC / JS / TS)
223
+ - **索引**:语义向量 + BM25 + 关键词三模式搜索
163
224
 
164
- 建议优先在 Cursor 中使用 AutoSnippet;如需 VSCode Copilot 支持,欢迎 [Issue](https://github.com/GxFn/AutoSnippet/issues) 讨论降级方案的适配。
225
+ ## Xcode 深度集成
165
226
 
166
- ---
227
+ AutoSnippet 不依赖 Xcode 插件,通过 **AppleScript + FileWatcher + 原生 macOS UI** 实现深度集成。
228
+
229
+ | 能力 | 说明 |
230
+ |------|------|
231
+ | **保存即触发** | FileWatcher 监听源码目录;在代码中写入 `// as:search`、`// as:create`、`// as:audit` 后按 `⌘S`,自动执行对应操作 |
232
+ | **AppleScript 自动化** | 通过 `osascript` 驱动 Xcode——行号跳转、行选中、剪切/粘贴替换、前台检测;搜索结果直接替换触发行 |
233
+ | **原生 macOS UI** | Swift 原生弹窗展示搜索结果列表(降级为 AppleScript `choose from list`);系统通知反馈操作结果 |
234
+ | **智能 import 注入** | 插入代码时自动分析所需 `import`,检查 SPM 模块可达性,确认后通过 AppleScript 注入头文件 |
235
+ | **三层防误触** | Self-write 冷却 + 内容哈希去重 + Xcode 焦点检测,区分手动保存与自动保存 |
236
+ | **Code Snippet** | `ass`(搜索插入)、`asc`(创建候选)、`asa`(代码审查),`asd setup` 注册后重启 Xcode 生效 |
237
+
238
+ ## 贡献
239
+
240
+ 欢迎 [Issue](https://github.com/GxFn/AutoSnippet/issues) 与 [PR](https://github.com/GxFn/AutoSnippet/pulls)。
241
+
242
+ ## License
167
243
 
168
- 欢迎 [Issue](https://github.com/GxFn/AutoSnippet/issues) 与 [PR](https://github.com/GxFn/AutoSnippet/pulls)。MIT 许可证。
244
+ [MIT](LICENSE)
package/bin/api-server.js CHANGED
@@ -33,10 +33,7 @@ async function main() {
33
33
  db: components.db,
34
34
  auditLogger: components.auditLogger,
35
35
  gateway: components.gateway,
36
- reasoningLogger: components.reasoningLogger,
37
- roleDriftMonitor: components.roleDriftMonitor,
38
- complianceEvaluator: components.complianceEvaluator,
39
- sessionManager: components.sessionManager,
36
+ constitution: components.constitution,
40
37
  });
41
38
  logger.info('Service container initialized successfully');
42
39
 
package/bin/cli.js CHANGED
@@ -251,52 +251,6 @@ program
251
251
  }
252
252
  });
253
253
 
254
- // ─────────────────────────────────────────────────────
255
- // compliance 命令
256
- // ─────────────────────────────────────────────────────
257
- program
258
- .command('compliance')
259
- .description('运行合规评估')
260
- .option('--json', '以 JSON 格式输出')
261
- .action(async (opts) => {
262
- try {
263
- const { bootstrap, container } = await initContainer();
264
- const complianceEvaluator = container.get('complianceEvaluator');
265
-
266
- if (!complianceEvaluator) {
267
- console.error('ComplianceEvaluator not available');
268
- process.exit(1);
269
- }
270
-
271
- const report = complianceEvaluator.evaluate();
272
-
273
- if (opts.json) {
274
- console.log(JSON.stringify(report, null, 2));
275
- } else {
276
- console.log('\n📊 Compliance Report\n');
277
- console.log(` Overall Score: ${(report.overallScore * 100).toFixed(1)}%`);
278
- console.log(` Grade: ${report.grade || 'N/A'}\n`);
279
-
280
- for (const [key, metric] of Object.entries(report.priorities || {})) {
281
- const pct = ((metric.score || 0) * 100).toFixed(1);
282
- console.log(` ${key}: ${pct}%`);
283
- }
284
-
285
- if (report.recommendations?.length) {
286
- console.log('\n Recommendations:');
287
- for (const rec of report.recommendations.slice(0, 5)) {
288
- console.log(` • ${rec}`);
289
- }
290
- }
291
- }
292
-
293
- await bootstrap.shutdown();
294
- } catch (err) {
295
- console.error('Error:', err.message);
296
- process.exit(1);
297
- }
298
- });
299
-
300
254
  // ─────────────────────────────────────────────────────
301
255
  // server 命令
302
256
  // ─────────────────────────────────────────────────────
@@ -601,10 +555,7 @@ async function initContainer(opts = {}) {
601
555
  db: bootstrap.components.db,
602
556
  auditLogger: bootstrap.components.auditLogger,
603
557
  gateway: bootstrap.components.gateway,
604
- reasoningLogger: bootstrap.components.reasoningLogger,
605
- roleDriftMonitor: bootstrap.components.roleDriftMonitor,
606
- complianceEvaluator: bootstrap.components.complianceEvaluator,
607
- sessionManager: bootstrap.components.sessionManager,
558
+ constitution: bootstrap.components.constitution,
608
559
  projectRoot,
609
560
  });
610
561
  return { bootstrap, container };
@@ -1,69 +1,33 @@
1
- # ═══════════════════════════════════════════════════════════
2
- # AutoSnippet Constitution — 权限宪法
3
- # ═══════════════════════════════════════════════════════════
4
- #
5
- # 三层权限架构:
6
- # ① 能力层 (capabilities) — git push --dry-run 探测物理写权限
7
- # ② 角色层 (roles) — 角色权限矩阵 (action:resource)
8
- # ③ 治理层 (priorities) — 业务规则引擎
9
- #
10
- # 双路径模式:
11
- # AUTH_ENABLED=false → 子仓库探针自动决定角色(能力层驱动)
12
- # AUTH_ENABLED=true → 登录后根据用户配置角色(角色层驱动)
13
- # ═══════════════════════════════════════════════════════════
1
+ # AutoSnippet Constitution
2
+ version: "3.0"
3
+ effective_date: "2026-02-13"
14
4
 
15
- version: "2.0"
16
- effective_date: "2026-02-10"
17
-
18
- # ─── 能力探测 ─────────────────────────────────────────────
19
5
  capabilities:
20
6
  git_write:
21
- description: "子仓库 git push 权限"
7
+ description: "subrepo git push"
22
8
  probe: "git push --dry-run"
23
- # 无子仓库 → 个人项目, 视为 admin (全权限)
24
9
  no_subrepo: "allow"
25
- # 有子仓库但无 remote → 本地开发, 视为 admin
26
10
  no_remote: "allow"
27
11
  cache_ttl: 86400
28
12
 
29
- # ─── 治理优先级 ───────────────────────────────────────────
30
- priorities:
31
- - id: 1
32
- name: "Data Integrity"
33
- description: "保护用户项目数据不被破坏"
34
- rules:
35
- - "所有写操作必须经过备份"
36
- - "删除操作必须有确认步骤"
37
- - "关键文件修改需要记录审计日志"
38
-
39
- - id: 2
40
- name: "Human Oversight"
41
- description: "保持人类对 AI 决策的监督"
42
- rules:
43
- - "AI 生成的 Candidate 必须经人工审核"
44
- - "Guard 规则修改需要人工批准"
45
- - "批量操作需要明确授权"
46
-
47
- - id: 3
48
- name: "AI Transparency"
49
- description: "AI 决策过程必须可追溯"
50
- rules:
51
- - "Candidate 必须包含 Reasoning 信息"
52
- - "Guard 规则必须关联来源 Recipe"
53
- - "所有 AI 操作记录到审计日志"
54
-
55
- - id: 4
56
- name: "Helpfulness"
57
- description: "在保证安全前提下尽可能帮助用户"
58
- rules:
59
- - "提供充分的错误信息和修复建议"
60
- - "优先推荐高质量 Recipe"
61
- - "自动优化常见操作"
13
+ rules:
14
+ - id: "destructive_confirm"
15
+ description: "delete needs confirmation"
16
+ check: "destructive_needs_confirmation"
17
+ - id: "content_required"
18
+ description: "create candidate/recipe needs content"
19
+ check: "creation_needs_content"
20
+ - id: "ai_no_direct_recipe"
21
+ description: "AI cannot directly create/approve recipe"
22
+ check: "ai_cannot_approve_recipe"
23
+ - id: "batch_authorized"
24
+ description: "batch ops need authorization"
25
+ check: "batch_needs_authorization"
62
26
 
63
27
  roles:
64
- - id: "cursor_agent"
65
- name: "Cursor / Copilot Agent"
66
- description: "IDE 中运行的 AI 助手"
28
+ - id: "external_agent"
29
+ name: "External Agent"
30
+ description: "IDE AI agent (Cursor / Copilot / Claude Code)"
67
31
  permissions:
68
32
  - "read:recipes"
69
33
  - "read:guard_rules"
@@ -71,64 +35,26 @@ roles:
71
35
  - "submit:candidates"
72
36
  - "read:audit_logs:self"
73
37
  - "knowledge:bootstrap"
38
+ - "create:skills"
74
39
  constraints:
75
- - "不能直接修改 Recipe"
76
- - "不能修改 Guard 规则"
77
- - "不能删除任何数据"
78
-
79
- - id: "asd_ais"
80
- name: "ASD AI审查"
81
- description: "AutoSnippet 内置的 AI 审查系统"
40
+ - "cannot modify Recipe directly"
41
+ - "cannot modify Guard rules"
42
+ - "cannot delete any data"
43
+ - id: "chat_agent"
44
+ name: "ChatAgent"
45
+ description: "built-in AI agent (Dashboard chat / programmatic)"
82
46
  permissions:
83
47
  - "read:recipes"
84
48
  - "read:candidates"
85
49
  - "create:candidates"
86
50
  - "read:guard_rules"
87
51
  constraints:
88
- - "生成的 Candidate 必须包含完整 Reasoning"
89
- - "不能绕过 Guard 检查"
90
-
91
- - id: "guard_engine"
92
- name: "Guard 检查引擎"
93
- description: "代码质量检查系统"
94
- permissions:
95
- - "read:candidates"
96
- - "read:guard_rules"
97
- - "write:audit_logs"
98
- constraints:
99
- - "只能读取,不能修改数据"
100
-
101
- - id: "developer_admin"
102
- name: "开发者(管理员)"
103
- description: "项目管理员"
52
+ - "candidate must include reasoning"
53
+ - "cannot bypass Guard check"
54
+ - id: "developer"
55
+ name: "Developer"
56
+ description: "project owner, full access"
104
57
  permissions:
105
- - "*" # 全部权限
58
+ - "*"
106
59
  requires_capability:
107
60
  - "git_write"
108
-
109
- - id: "developer_contributor"
110
- name: "开发者(贡献者)"
111
- description: "项目贡献者"
112
- permissions:
113
- - "read:*"
114
- - "approve:candidates"
115
- - "reject:candidates"
116
- - "create:recipes"
117
- constraints:
118
- - "不能删除 Recipe"
119
- - "不能禁用 Guard 规则"
120
- requires_capability:
121
- - "git_write"
122
-
123
- - id: "visitor"
124
- name: "访问者"
125
- description: "只读访问(无子仓库写权限或未登录)"
126
- permissions:
127
- - "read:recipes"
128
- - "read:snippets"
129
- - "read:candidates"
130
- - "read:guard_rules"
131
- - "search:query"
132
- constraints:
133
- - "只能查看,不能修改任何数据"
134
- - "不能创建 Candidate 或 Recipe"