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.
- package/README.md +189 -113
- package/bin/api-server.js +1 -4
- package/bin/cli.js +1 -50
- package/config/constitution.yaml +33 -107
- package/dashboard/dist/assets/{icons-B4FfLfBA.js → icons-B5rs8uNb.js} +85 -80
- package/dashboard/dist/assets/index-0YzLw2ga.css +1 -0
- package/dashboard/dist/assets/index-B9py3ybr.js +154 -0
- package/dashboard/dist/index.html +3 -3
- package/lib/bootstrap.js +5 -31
- package/lib/cli/SetupService.js +16 -14
- package/lib/core/capability/CapabilityProbe.js +8 -6
- package/lib/core/constitution/Constitution.js +13 -4
- package/lib/core/constitution/ConstitutionValidator.js +106 -211
- package/lib/core/gateway/Gateway.js +34 -98
- package/lib/core/gateway/GatewayActionRegistry.js +12 -1
- package/lib/core/permission/PermissionManager.js +2 -2
- package/lib/external/mcp/McpServer.js +4 -7
- package/lib/external/mcp/handlers/bootstrap.js +13 -1
- package/lib/external/mcp/handlers/browse.js +0 -7
- package/lib/external/mcp/handlers/candidate.js +1 -1
- package/lib/external/mcp/handlers/guard.js +11 -0
- package/lib/external/mcp/handlers/skill.js +186 -18
- package/lib/external/mcp/tools.js +40 -1
- package/lib/http/middleware/roleResolver.js +1 -1
- package/lib/http/routes/auth.js +2 -2
- package/lib/http/routes/commands.js +58 -3
- package/lib/http/routes/monitoring.js +4 -4
- package/lib/http/routes/recipes.js +96 -4
- package/lib/http/routes/search.js +34 -35
- package/lib/injection/ServiceContainer.js +21 -40
- package/lib/service/candidate/CandidateService.js +12 -1
- package/lib/service/chat/ChatAgent.js +171 -30
- package/lib/service/chat/Memory.js +104 -0
- package/lib/service/chat/tools.js +244 -10
- package/lib/service/guard/GuardCheckEngine.js +9 -1
- package/lib/service/knowledge/KnowledgeGraphService.js +20 -9
- package/lib/service/recipe/RecipeService.js +8 -0
- package/lib/service/skills/SkillHooks.js +126 -0
- package/package.json +1 -1
- package/scripts/init-db.js +1 -2
- package/templates/constitution.yaml +29 -85
- package/dashboard/dist/assets/index-ChxJxX4B.js +0 -154
- package/dashboard/dist/assets/index-DwAp1mx5.css +0 -1
- package/lib/core/session/SessionManager.js +0 -232
- package/lib/infrastructure/logging/ReasoningLogger.js +0 -269
- package/lib/infrastructure/monitoring/RoleDriftMonitor.js +0 -259
- package/lib/infrastructure/quality/ComplianceEvaluator.js +0 -326
package/README.md
CHANGED
|
@@ -1,168 +1,244 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# AutoSnippet
|
|
4
|
+
|
|
5
|
+
**Project Knowledge Engine for iOS / Swift Teams**
|
|
6
|
+
|
|
7
|
+
将团队的代码模式、最佳实践沉淀为 AI 可检索的知识库,<br>
|
|
8
|
+
让 Cursor、Copilot 和 Xcode 都按你的项目规范生成代码。
|
|
4
9
|
|
|
5
10
|
[](https://www.npmjs.com/package/autosnippet)
|
|
6
11
|
[](https://github.com/GxFn/AutoSnippet/blob/main/LICENSE)
|
|
12
|
+
[](https://nodejs.org)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
7
15
|
|
|
8
16
|
---
|
|
9
17
|
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
57
|
+
> **注意**:始终在**你的项目目录**中执行 `asd` 命令,而非 AutoSnippet 源码仓库。
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
## 工作流
|
|
34
60
|
|
|
35
|
-
|
|
36
|
-
cd /path/to/your-project # 进入你的项目
|
|
61
|
+
### 知识沉淀闭环
|
|
37
62
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
###
|
|
78
|
+
### 三种使用方式
|
|
46
79
|
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
88
|
+
**② Dashboard Web UI**:可视化管理一切——Recipe 编辑器、Candidate 审核、知识图谱、Guard 审查、冷启动向导。
|
|
52
89
|
|
|
53
|
-
|
|
90
|
+
**③ 命令行 CLI**:`asd` 命令覆盖全部操作,适合自动化和脚本集成。
|
|
54
91
|
|
|
92
|
+
## Dashboard
|
|
55
93
|
|
|
56
|
-
|
|
94
|
+
`asd ui` 启动后访问 Web 管理后台:
|
|
57
95
|
|
|
58
|
-
|
|
96
|
+

|
|
59
97
|
|
|
60
|
-
|
|
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
|
-
|
|
107
|
+
## IDE 集成
|
|
69
108
|
|
|
70
|
-
Cursor
|
|
109
|
+
### Cursor(推荐)
|
|
71
110
|
|
|
72
|
-
|
|
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
|
-
|
|
117
|
+
```bash
|
|
118
|
+
asd install:cursor-skill --mcp # 安装 Skills + MCP 配置
|
|
119
|
+
```
|
|
77
120
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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` |
|
|
94
|
-
| `asd ui` | 启动 Dashboard +
|
|
95
|
-
| `asd status` |
|
|
96
|
-
| `asd
|
|
97
|
-
| `asd
|
|
98
|
-
| `asd
|
|
99
|
-
| `asd
|
|
100
|
-
| `asd
|
|
101
|
-
| `asd
|
|
102
|
-
| `asd
|
|
103
|
-
| `asd
|
|
104
|
-
| `asd
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
|
121
|
-
|
|
|
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
|
-
|
|
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
|
-
|
|
216
|
+
## 技术栈
|
|
159
217
|
|
|
160
|
-
- **
|
|
161
|
-
-
|
|
162
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 };
|
package/config/constitution.yaml
CHANGED
|
@@ -1,69 +1,33 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
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: "
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- id:
|
|
40
|
-
|
|
41
|
-
|
|
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: "
|
|
65
|
-
name: "
|
|
66
|
-
description: "
|
|
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
|
-
- "
|
|
76
|
-
- "
|
|
77
|
-
- "
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
- "
|
|
89
|
-
- "
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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"
|