@adversity/coding-tool-x 2.3.0 → 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.
@@ -0,0 +1,339 @@
1
+ /**
2
+ * Permission Templates Service
3
+ *
4
+ * 管理权限配置模版的 CRUD 操作
5
+ * 支持内置模版和自定义模版
6
+ */
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+ const { PATHS } = require('../../config/paths');
11
+
12
+ // 权限模版存储文件
13
+ const TEMPLATES_FILE = path.join(PATHS.config, 'permission-templates.json');
14
+
15
+ // 内置权限模版
16
+ const BUILTIN_TEMPLATES = [
17
+ {
18
+ id: 'safe',
19
+ name: '安全模式',
20
+ description: '仅允许只读命令,危险操作需要确认',
21
+ permissions: {
22
+ allow: [
23
+ 'Bash(cat:*)',
24
+ 'Bash(ls:*)',
25
+ 'Bash(pwd)',
26
+ 'Bash(echo:*)',
27
+ 'Bash(head:*)',
28
+ 'Bash(tail:*)',
29
+ 'Bash(grep:*)',
30
+ 'Read(*)'
31
+ ],
32
+ deny: [
33
+ 'Bash(rm:*)',
34
+ 'Bash(sudo:*)',
35
+ 'Bash(git push:*)',
36
+ 'Bash(git reset --hard:*)',
37
+ 'Bash(chmod:*)',
38
+ 'Bash(chown:*)',
39
+ 'Edit(*)'
40
+ ]
41
+ },
42
+ isBuiltin: true
43
+ },
44
+ {
45
+ id: 'balanced',
46
+ name: '平衡模式',
47
+ description: '允许常用开发命令,危险操作需要确认',
48
+ permissions: {
49
+ allow: [
50
+ 'Bash(cat:*)',
51
+ 'Bash(ls:*)',
52
+ 'Bash(pwd)',
53
+ 'Bash(echo:*)',
54
+ 'Bash(head:*)',
55
+ 'Bash(tail:*)',
56
+ 'Bash(grep:*)',
57
+ 'Bash(find:*)',
58
+ 'Bash(git status)',
59
+ 'Bash(git diff:*)',
60
+ 'Bash(git log:*)',
61
+ 'Bash(npm run:*)',
62
+ 'Bash(pnpm:*)',
63
+ 'Bash(yarn:*)',
64
+ 'Read(*)',
65
+ 'Edit(*)'
66
+ ],
67
+ deny: [
68
+ 'Bash(rm -rf:*)',
69
+ 'Bash(sudo:*)',
70
+ 'Bash(git push --force:*)',
71
+ 'Bash(git reset --hard:*)'
72
+ ]
73
+ },
74
+ isBuiltin: true
75
+ },
76
+ {
77
+ id: 'permissive',
78
+ name: '宽松模式',
79
+ description: '允许大多数命令,仅阻止极度危险的操作',
80
+ permissions: {
81
+ allow: [
82
+ 'Bash(*)',
83
+ 'Read(*)',
84
+ 'Edit(*)'
85
+ ],
86
+ deny: [
87
+ 'Bash(rm -rf /*)',
88
+ 'Bash(sudo rm -rf:*)'
89
+ ]
90
+ },
91
+ isBuiltin: true
92
+ },
93
+ {
94
+ id: 'mcp-full',
95
+ name: 'MCP 全功能',
96
+ description: '允许所有 MCP 工具和常用命令,适合使用 MCP 服务器的项目',
97
+ permissions: {
98
+ allow: [
99
+ 'Read(*)',
100
+ 'Edit(*)',
101
+ 'Bash(cat:*)',
102
+ 'Bash(ls:*)',
103
+ 'Bash(find:*)',
104
+ 'Bash(grep:*)',
105
+ 'Bash(tree:*)',
106
+ 'Bash(git:*)',
107
+ 'Bash(npm:*)',
108
+ 'Bash(pnpm:*)',
109
+ 'Bash(yarn:*)',
110
+ 'WebSearch',
111
+ 'mcp__Serena__*',
112
+ 'mcp__fetch__fetch',
113
+ 'mcp__memory__*',
114
+ 'mcp__github__*',
115
+ 'mcp__context7__*'
116
+ ],
117
+ deny: [
118
+ 'Bash(rm -rf:*)',
119
+ 'Bash(sudo:*)'
120
+ ]
121
+ },
122
+ isBuiltin: true
123
+ }
124
+ ];
125
+
126
+ /**
127
+ * 确保配置目录存在
128
+ */
129
+ function ensureDir(dirPath) {
130
+ if (!fs.existsSync(dirPath)) {
131
+ fs.mkdirSync(dirPath, { recursive: true });
132
+ }
133
+ }
134
+
135
+ /**
136
+ * 加载所有模版(内置 + 自定义)
137
+ */
138
+ /**
139
+ * 加载所有模版(内置 + 自定义)
140
+ * 内置模版可被用户覆盖修改
141
+ */
142
+ /**
143
+ * 加载所有模版(内置 + 自定义)
144
+ * 内置模版可被用户覆盖修改或隐藏
145
+ */
146
+ function loadTemplates() {
147
+ try {
148
+ if (fs.existsSync(TEMPLATES_FILE)) {
149
+ const content = fs.readFileSync(TEMPLATES_FILE, 'utf8');
150
+ const data = JSON.parse(content);
151
+
152
+ // 分离自定义模版和隐藏标记
153
+ const customTemplates = (data.custom || []).filter(t => !t._hidden);
154
+ const hiddenIds = (data.custom || []).filter(t => t._hidden).map(t => t.id);
155
+
156
+ const customIds = customTemplates.map(t => t.id);
157
+
158
+ // 过滤掉已被自定义覆盖或隐藏的内置模版
159
+ const effectiveBuiltin = BUILTIN_TEMPLATES.filter(t =>
160
+ !customIds.includes(t.id) && !hiddenIds.includes(t.id)
161
+ );
162
+
163
+ return {
164
+ builtin: effectiveBuiltin,
165
+ custom: customTemplates,
166
+ all: [...customTemplates, ...effectiveBuiltin] // 自定义优先
167
+ };
168
+ }
169
+ } catch (error) {
170
+ console.error('[PermissionTemplates] 加载模版失败:', error.message);
171
+ }
172
+
173
+ return {
174
+ builtin: BUILTIN_TEMPLATES,
175
+ custom: [],
176
+ all: BUILTIN_TEMPLATES
177
+ };
178
+ }
179
+
180
+ /**
181
+ * 保存自定义模版
182
+ */
183
+ function saveCustomTemplates(customTemplates) {
184
+ ensureDir(PATHS.config);
185
+ fs.writeFileSync(TEMPLATES_FILE, JSON.stringify({ custom: customTemplates }, null, 2), 'utf8');
186
+ }
187
+
188
+ /**
189
+ * 获取所有模版
190
+ */
191
+ /**
192
+ * 获取所有模版(合并后的最终列表)
193
+ */
194
+ function getAllTemplates() {
195
+ const { all } = loadTemplates();
196
+ return all;
197
+ }
198
+
199
+ /**
200
+ * 根据 ID 获取模版
201
+ */
202
+ function getTemplateById(id) {
203
+ const templates = getAllTemplates();
204
+ return templates.find(t => t.id === id) || null;
205
+ }
206
+
207
+ /**
208
+ * 创建自定义模版
209
+ */
210
+ function createTemplate(template) {
211
+ const { custom } = loadTemplates();
212
+
213
+ // 验证必填字段
214
+ if (!template.name || !template.name.trim()) {
215
+ throw new Error('模版名称不能为空');
216
+ }
217
+
218
+ // 生成唯一 ID
219
+ const id = `custom-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
220
+
221
+ const newTemplate = {
222
+ id,
223
+ name: template.name.trim(),
224
+ description: template.description || '',
225
+ permissions: {
226
+ allow: template.permissions?.allow || [],
227
+ deny: template.permissions?.deny || []
228
+ },
229
+ isBuiltin: false,
230
+ createdAt: new Date().toISOString()
231
+ };
232
+
233
+ custom.push(newTemplate);
234
+ saveCustomTemplates(custom);
235
+
236
+ return newTemplate;
237
+ }
238
+
239
+ /**
240
+ * 更新自定义模版
241
+ */
242
+ /**
243
+ * 更新权限模版(包括内置模版)
244
+ * 编辑内置模版时,会在自定义列表中创建覆盖版本
245
+ */
246
+ function updateTemplate(id, updates) {
247
+ const { custom, all } = loadTemplates();
248
+
249
+ // 查找模版(先查自定义,再查所有)
250
+ const existingTemplate = all.find(t => t.id === id);
251
+ if (!existingTemplate) {
252
+ throw new Error('模版不存在');
253
+ }
254
+
255
+ // 验证名称
256
+ if (updates.name !== undefined && !updates.name.trim()) {
257
+ throw new Error('模版名称不能为空');
258
+ }
259
+
260
+ const updated = {
261
+ ...existingTemplate,
262
+ name: updates.name?.trim() ?? existingTemplate.name,
263
+ description: updates.description ?? existingTemplate.description,
264
+ permissions: updates.permissions ?? existingTemplate.permissions,
265
+ isBuiltin: false, // 编辑后标记为自定义(覆盖内置)
266
+ updatedAt: new Date().toISOString()
267
+ };
268
+
269
+ // 如果是编辑内置模版,则添加到自定义列表中(覆盖)
270
+ const customIndex = custom.findIndex(t => t.id === id);
271
+ if (customIndex !== -1) {
272
+ custom[customIndex] = updated;
273
+ } else {
274
+ custom.push(updated);
275
+ }
276
+
277
+ saveCustomTemplates(custom);
278
+ return updated;
279
+ }
280
+
281
+ /**
282
+ * 删除自定义模版
283
+ */
284
+ /**
285
+ * 删除权限模版(包括内置模版)
286
+ * 删除内置模版时,会在自定义列表中添加隐藏标记
287
+ */
288
+ /**
289
+ * 删除权限模版(包括内置模版)
290
+ * 删除内置模版时,会在自定义列表中添加隐藏标记
291
+ */
292
+ function deleteTemplate(id) {
293
+ try {
294
+ if (!fs.existsSync(TEMPLATES_FILE)) {
295
+ ensureDir(PATHS.config);
296
+ fs.writeFileSync(TEMPLATES_FILE, JSON.stringify({ custom: [] }, null, 2), 'utf8');
297
+ }
298
+
299
+ const content = fs.readFileSync(TEMPLATES_FILE, 'utf8');
300
+ const data = JSON.parse(content);
301
+ const customList = data.custom || [];
302
+
303
+ // 检查是否为内置模版
304
+ const isBuiltin = BUILTIN_TEMPLATES.some(t => t.id === id);
305
+
306
+ if (isBuiltin) {
307
+ // 内置模版:添加隐藏标记
308
+ const hideMarker = {
309
+ id,
310
+ _hidden: true,
311
+ deletedAt: new Date().toISOString()
312
+ };
313
+ customList.push(hideMarker);
314
+ fs.writeFileSync(TEMPLATES_FILE, JSON.stringify({ custom: customList }, null, 2), 'utf8');
315
+ } else {
316
+ // 自定义模版:直接删除
317
+ const index = customList.findIndex(t => t.id === id && !t._hidden);
318
+ if (index === -1) {
319
+ throw new Error('模版不存在');
320
+ }
321
+ customList.splice(index, 1);
322
+ fs.writeFileSync(TEMPLATES_FILE, JSON.stringify({ custom: customList }, null, 2), 'utf8');
323
+ }
324
+
325
+ return true;
326
+ } catch (error) {
327
+ if (error.message === '模版不存在') throw error;
328
+ throw new Error('删除模版失败: ' + error.message);
329
+ }
330
+ }
331
+
332
+ module.exports = {
333
+ getAllTemplates,
334
+ getTemplateById,
335
+ createTemplate,
336
+ updateTemplate,
337
+ deleteTemplate,
338
+ BUILTIN_TEMPLATES
339
+ };
@@ -4,6 +4,7 @@ const path = require('path');
4
4
  const { execSync } = require('child_process');
5
5
  const { PATHS } = require('../../config/paths');
6
6
  const configTemplatesService = require('./config-templates-service');
7
+ const permissionTemplatesService = require('./permission-templates-service');
7
8
 
8
9
  // 工作区配置文件路径
9
10
  const WORKSPACES_CONFIG = path.join(PATHS.base, 'workspaces.json');
@@ -299,73 +300,14 @@ function createWorkspace(options) {
299
300
  }
300
301
  }
301
302
 
302
- // 应用权限模板(如果指定且不是 'none')
303
+ // 应用权限模板(如果指定)
303
304
  let permissionInfo = null;
304
- if (permissionTemplate && permissionTemplate !== 'none') {
305
+ if (permissionTemplate) {
305
306
  try {
306
- const permissionTemplates = {
307
- safe: {
308
- allow: [
309
- 'Bash(cat:*)',
310
- 'Bash(ls:*)',
311
- 'Bash(pwd)',
312
- 'Bash(echo:*)',
313
- 'Bash(head:*)',
314
- 'Bash(tail:*)',
315
- 'Bash(grep:*)',
316
- 'Read(*)'
317
- ],
318
- deny: [
319
- 'Bash(rm:*)',
320
- 'Bash(sudo:*)',
321
- 'Bash(git push:*)',
322
- 'Bash(git reset --hard:*)',
323
- 'Bash(chmod:*)',
324
- 'Bash(chown:*)',
325
- 'Edit(*)'
326
- ]
327
- },
328
- balanced: {
329
- allow: [
330
- 'Bash(cat:*)',
331
- 'Bash(ls:*)',
332
- 'Bash(pwd)',
333
- 'Bash(echo:*)',
334
- 'Bash(head:*)',
335
- 'Bash(tail:*)',
336
- 'Bash(grep:*)',
337
- 'Bash(find:*)',
338
- 'Bash(git status)',
339
- 'Bash(git diff:*)',
340
- 'Bash(git log:*)',
341
- 'Bash(npm run:*)',
342
- 'Bash(pnpm:*)',
343
- 'Bash(yarn:*)',
344
- 'Read(*)',
345
- 'Edit(*)'
346
- ],
347
- deny: [
348
- 'Bash(rm -rf:*)',
349
- 'Bash(sudo:*)',
350
- 'Bash(git push --force:*)',
351
- 'Bash(git reset --hard:*)'
352
- ]
353
- },
354
- permissive: {
355
- allow: [
356
- 'Bash(*)',
357
- 'Read(*)',
358
- 'Edit(*)'
359
- ],
360
- deny: [
361
- 'Bash(rm -rf /*)',
362
- 'Bash(sudo rm -rf:*)'
363
- ]
364
- }
365
- };
307
+ // 从权限模板服务获取模板
308
+ const template = permissionTemplatesService.getTemplateById(permissionTemplate);
366
309
 
367
- const permSettings = permissionTemplates[permissionTemplate];
368
- if (permSettings) {
310
+ if (template && template.permissions) {
369
311
  // 为工作区中的每个项目应用权限设置
370
312
  for (const proj of workspaceProjects) {
371
313
  const projSettingsDir = path.join(proj.targetPath, '.claude');
@@ -388,8 +330,8 @@ function createWorkspace(options) {
388
330
 
389
331
  // 更新权限设置
390
332
  settings.permissions = {
391
- allow: permSettings.allow,
392
- deny: permSettings.deny
333
+ allow: template.permissions.allow || [],
334
+ deny: template.permissions.deny || []
393
335
  };
394
336
 
395
337
  // 保存设置