@adversity/coding-tool-x 2.4.0 → 2.4.2

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.
@@ -17,11 +17,11 @@ const CONFIG_VERSION = '1.0.0';
17
17
  */
18
18
  function exportAllConfigs() {
19
19
  try {
20
- // 获取所有权限模板(只导出自定义模板)
20
+ // 获取所有权限模板(只导出自定义模板)
21
21
  const allPermissionTemplates = permissionTemplatesService.getAllTemplates();
22
22
  const customPermissionTemplates = allPermissionTemplates.filter(t => !t.isBuiltin);
23
23
 
24
- // 获取所有配置模板(只导出自定义模板)
24
+ // 获取所有配置模板(只导出自定义模板)
25
25
  const allConfigTemplates = configTemplatesService.getAllTemplates();
26
26
  const customConfigTemplates = allConfigTemplates.filter(t => !t.isBuiltin);
27
27
 
@@ -29,13 +29,65 @@ function exportAllConfigs() {
29
29
  const channelsData = channelsService.getAllChannels();
30
30
  const channels = channelsData?.channels || [];
31
31
 
32
+ // 获取工作区配置
33
+ const workspaceService = require('./workspace-service');
34
+ const workspaces = workspaceService.loadWorkspaces();
35
+
36
+ // 获取收藏配置
37
+ const favoritesService = require('./favorites');
38
+ const favorites = favoritesService.loadFavorites();
39
+
40
+ // 获取 Agents 配置
41
+ const agentsService = require('./agents-service');
42
+ const agents = agentsService.getAllAgents();
43
+
44
+ // 获取 Skills 配置
45
+ const skillService = require('./skill-service');
46
+ const skills = skillService.getAllSkills();
47
+
48
+ // 获取 Commands 配置
49
+ const commandsService = require('./commands-service');
50
+ const commands = commandsService.getAllCommands();
51
+
52
+ // 获取 Rules 配置
53
+ const rulesService = require('./rules-service');
54
+ const rules = rulesService.getAllRules();
55
+
56
+ // 获取 MCP 配置
57
+ const mcpService = require('./mcp-service');
58
+ const mcpServers = mcpService.getAllServers();
59
+
60
+ // 读取 Markdown 配置文件
61
+ const { PATHS } = require('../../config/paths');
62
+ const markdownFiles = {};
63
+ const mdFileNames = ['CLAUDE.md', 'AGENTS.md', 'GEMINI.md'];
64
+
65
+ for (const fileName of mdFileNames) {
66
+ const filePath = path.join(PATHS.base, fileName);
67
+ if (fs.existsSync(filePath)) {
68
+ try {
69
+ markdownFiles[fileName] = fs.readFileSync(filePath, 'utf8');
70
+ } catch (err) {
71
+ console.warn(`无法读取 ${fileName}:`, err.message);
72
+ }
73
+ }
74
+ }
75
+
32
76
  const exportData = {
33
77
  version: CONFIG_VERSION,
34
78
  exportedAt: new Date().toISOString(),
35
79
  data: {
36
80
  permissionTemplates: customPermissionTemplates,
37
81
  configTemplates: customConfigTemplates,
38
- channels: channels || []
82
+ channels: channels || [],
83
+ workspaces: workspaces || { workspaces: [] },
84
+ favorites: favorites || { favorites: [] },
85
+ agents: agents || [],
86
+ skills: skills || [],
87
+ commands: commands || [],
88
+ rules: rules || [],
89
+ mcpServers: mcpServers || [],
90
+ markdownFiles: markdownFiles
39
91
  }
40
92
  };
41
93
 
@@ -59,11 +111,19 @@ function exportAllConfigs() {
59
111
  * @returns {Object} 导入结果
60
112
  */
61
113
  function importConfigs(importData, options = {}) {
62
- const { overwrite = false } = options;
114
+ const { overwrite = true } = options; // 默认覆盖模式
63
115
  const results = {
64
116
  permissionTemplates: { success: 0, failed: 0, skipped: 0 },
65
117
  configTemplates: { success: 0, failed: 0, skipped: 0 },
66
- channels: { success: 0, failed: 0, skipped: 0 }
118
+ channels: { success: 0, failed: 0, skipped: 0 },
119
+ workspaces: { success: 0, failed: 0, skipped: 0 },
120
+ favorites: { success: 0, failed: 0, skipped: 0 },
121
+ agents: { success: 0, failed: 0, skipped: 0 },
122
+ skills: { success: 0, failed: 0, skipped: 0 },
123
+ commands: { success: 0, failed: 0, skipped: 0 },
124
+ rules: { success: 0, failed: 0, skipped: 0 },
125
+ mcpServers: { success: 0, failed: 0, skipped: 0 },
126
+ markdownFiles: { success: 0, failed: 0, skipped: 0 }
67
127
  };
68
128
 
69
129
  try {
@@ -72,7 +132,19 @@ function importConfigs(importData, options = {}) {
72
132
  throw new Error('无效的导入数据格式');
73
133
  }
74
134
 
75
- const { permissionTemplates = [], configTemplates = [], channels = [] } = importData.data;
135
+ const {
136
+ permissionTemplates = [],
137
+ configTemplates = [],
138
+ channels = [],
139
+ workspaces = null,
140
+ favorites = null,
141
+ agents = [],
142
+ skills = [],
143
+ commands = [],
144
+ rules = [],
145
+ mcpServers = [],
146
+ markdownFiles = {}
147
+ } = importData.data;
76
148
 
77
149
  // 导入权限模板
78
150
  for (const template of permissionTemplates) {
@@ -87,7 +159,6 @@ function importConfigs(importData, options = {}) {
87
159
  if (existing && overwrite) {
88
160
  permissionTemplatesService.updateTemplate(template.id, template);
89
161
  } else {
90
- // 创建新模板(使用原ID)
91
162
  const newTemplate = {
92
163
  ...template,
93
164
  isBuiltin: false,
@@ -144,7 +215,6 @@ function importConfigs(importData, options = {}) {
144
215
  if (existing && overwrite) {
145
216
  channelsService.updateChannel(channel.id, channel);
146
217
  } else {
147
- // createChannel 需要单独的参数,不是一个对象
148
218
  const { name, baseUrl, apiKey, websiteUrl, ...extraConfig } = channel;
149
219
  channelsService.createChannel(name, baseUrl, apiKey, websiteUrl, extraConfig);
150
220
  }
@@ -155,6 +225,136 @@ function importConfigs(importData, options = {}) {
155
225
  }
156
226
  }
157
227
 
228
+ // 导入工作区配置
229
+ if (workspaces && overwrite) {
230
+ try {
231
+ const workspaceService = require('./workspace-service');
232
+ workspaceService.saveWorkspaces(workspaces);
233
+ results.workspaces.success = workspaces.workspaces?.length || 0;
234
+ } catch (err) {
235
+ console.error('[ConfigImport] 导入工作区失败:', err);
236
+ results.workspaces.failed++;
237
+ }
238
+ }
239
+
240
+ // 导入收藏配置
241
+ if (favorites && overwrite) {
242
+ try {
243
+ const favoritesService = require('./favorites');
244
+ favoritesService.saveFavorites(favorites);
245
+ const count = Object.values(favorites).reduce((sum, arr) => sum + (Array.isArray(arr) ? arr.length : 0), 0);
246
+ results.favorites.success = count;
247
+ } catch (err) {
248
+ console.error('[ConfigImport] 导入收藏失败:', err);
249
+ results.favorites.failed++;
250
+ }
251
+ }
252
+
253
+ // 导入 Agents
254
+ if (agents && agents.length > 0 && overwrite) {
255
+ try {
256
+ const agentsService = require('./agents-service');
257
+ for (const agent of agents) {
258
+ try {
259
+ agentsService.saveAgent(agent);
260
+ results.agents.success++;
261
+ } catch (err) {
262
+ console.error(`[ConfigImport] 导入 Agent 失败: ${agent.name}`, err);
263
+ results.agents.failed++;
264
+ }
265
+ }
266
+ } catch (err) {
267
+ console.error('[ConfigImport] 导入 Agents 失败:', err);
268
+ }
269
+ }
270
+
271
+ // 导入 Skills
272
+ if (skills && skills.length > 0 && overwrite) {
273
+ try {
274
+ const skillService = require('./skill-service');
275
+ for (const skill of skills) {
276
+ try {
277
+ skillService.saveSkill(skill);
278
+ results.skills.success++;
279
+ } catch (err) {
280
+ console.error(`[ConfigImport] 导入 Skill 失败: ${skill.name}`, err);
281
+ results.skills.failed++;
282
+ }
283
+ }
284
+ } catch (err) {
285
+ console.error('[ConfigImport] 导入 Skills 失败:', err);
286
+ }
287
+ }
288
+
289
+ // 导入 Commands
290
+ if (commands && commands.length > 0 && overwrite) {
291
+ try {
292
+ const commandsService = require('./commands-service');
293
+ for (const command of commands) {
294
+ try {
295
+ commandsService.saveCommand(command);
296
+ results.commands.success++;
297
+ } catch (err) {
298
+ console.error(`[ConfigImport] 导入 Command 失败: ${command.name}`, err);
299
+ results.commands.failed++;
300
+ }
301
+ }
302
+ } catch (err) {
303
+ console.error('[ConfigImport] 导入 Commands 失败:', err);
304
+ }
305
+ }
306
+
307
+ // 导入 Rules
308
+ if (rules && rules.length > 0 && overwrite) {
309
+ try {
310
+ const rulesService = require('./rules-service');
311
+ for (const rule of rules) {
312
+ try {
313
+ rulesService.saveRule(rule);
314
+ results.rules.success++;
315
+ } catch (err) {
316
+ console.error(`[ConfigImport] 导入 Rule 失败: ${rule.name}`, err);
317
+ results.rules.failed++;
318
+ }
319
+ }
320
+ } catch (err) {
321
+ console.error('[ConfigImport] 导入 Rules 失败:', err);
322
+ }
323
+ }
324
+
325
+ // 导入 MCP Servers
326
+ if (mcpServers && mcpServers.length > 0 && overwrite) {
327
+ try {
328
+ const mcpService = require('./mcp-service');
329
+ for (const server of mcpServers) {
330
+ try {
331
+ mcpService.saveServer(server);
332
+ results.mcpServers.success++;
333
+ } catch (err) {
334
+ console.error(`[ConfigImport] 导入 MCP Server 失败: ${server.name}`, err);
335
+ results.mcpServers.failed++;
336
+ }
337
+ }
338
+ } catch (err) {
339
+ console.error('[ConfigImport] 导入 MCP Servers 失败:', err);
340
+ }
341
+ }
342
+
343
+ // 导入 Markdown 文件
344
+ if (markdownFiles && Object.keys(markdownFiles).length > 0 && overwrite) {
345
+ const { PATHS } = require('../../config/paths');
346
+ for (const [fileName, content] of Object.entries(markdownFiles)) {
347
+ try {
348
+ const filePath = path.join(PATHS.base, fileName);
349
+ fs.writeFileSync(filePath, content, 'utf8');
350
+ results.markdownFiles.success++;
351
+ } catch (err) {
352
+ console.error(`[ConfigImport] 导入 ${fileName} 失败:`, err);
353
+ results.markdownFiles.failed++;
354
+ }
355
+ }
356
+ }
357
+
158
358
  return {
159
359
  success: true,
160
360
  results,
@@ -176,31 +376,37 @@ function importConfigs(importData, options = {}) {
176
376
  function generateImportSummary(results) {
177
377
  const parts = [];
178
378
 
179
- if (results.permissionTemplates.success > 0) {
180
- parts.push(`权限模板: ${results.permissionTemplates.success}成功`);
181
- }
182
- if (results.configTemplates.success > 0) {
183
- parts.push(`配置模板: ${results.configTemplates.success}成功`);
184
- }
185
- if (results.channels.success > 0) {
186
- parts.push(`频道: ${results.channels.success}成功`);
379
+ const types = [
380
+ { key: 'permissionTemplates', label: '权限模板' },
381
+ { key: 'configTemplates', label: '配置模板' },
382
+ { key: 'channels', label: '频道' },
383
+ { key: 'workspaces', label: '工作区' },
384
+ { key: 'favorites', label: '收藏' },
385
+ { key: 'agents', label: 'Agents' },
386
+ { key: 'skills', label: 'Skills' },
387
+ { key: 'commands', label: 'Commands' },
388
+ { key: 'rules', label: 'Rules' },
389
+ { key: 'mcpServers', label: 'MCP服务器' },
390
+ { key: 'markdownFiles', label: 'Markdown文件' }
391
+ ];
392
+
393
+ for (const { key, label } of types) {
394
+ if (results[key] && results[key].success > 0) {
395
+ parts.push(`${label}: ${results[key].success}成功`);
396
+ }
187
397
  }
188
398
 
189
- const totalSkipped = results.permissionTemplates.skipped +
190
- results.configTemplates.skipped +
191
- results.channels.skipped;
399
+ const totalSkipped = Object.values(results).reduce((sum, r) => sum + (r.skipped || 0), 0);
192
400
  if (totalSkipped > 0) {
193
401
  parts.push(`${totalSkipped}项已跳过`);
194
402
  }
195
403
 
196
- const totalFailed = results.permissionTemplates.failed +
197
- results.configTemplates.failed +
198
- results.channels.failed;
404
+ const totalFailed = Object.values(results).reduce((sum, r) => sum + (r.failed || 0), 0);
199
405
  if (totalFailed > 0) {
200
406
  parts.push(`${totalFailed}项失败`);
201
407
  }
202
408
 
203
- return parts.join(', ');
409
+ return parts.join(', ') || '无数据导入';
204
410
  }
205
411
 
206
412
  module.exports = {