@adversity/coding-tool-x 2.4.0 → 2.4.1
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/CHANGELOG.md +17 -0
- package/dist/web/assets/icons-Dom8a0SN.js +1 -0
- package/dist/web/assets/index-CQeUIH7E.css +41 -0
- package/dist/web/assets/index-YrjlFzC4.js +14 -0
- package/dist/web/assets/naive-ui-BjMHakwv.js +1 -0
- package/dist/web/assets/vendors-DtJKdpSj.js +7 -0
- package/dist/web/assets/vue-vendor-VFuFB5f4.js +44 -0
- package/dist/web/index.html +6 -2
- package/package.json +2 -2
- package/src/commands/export-config.js +205 -0
- package/src/server/api/config-sync.js +155 -0
- package/src/server/api/projects.js +2 -2
- package/src/server/api/sessions.js +70 -70
- package/src/server/api/skills.js +206 -0
- package/src/server/api/terminal.js +26 -0
- package/src/server/index.js +3 -0
- package/src/server/services/config-export-service.js +229 -23
- package/src/server/services/config-sync-service.js +515 -0
- package/src/server/services/enhanced-cache.js +196 -0
- package/src/server/services/pty-manager.js +35 -1
- package/src/server/services/sessions.js +122 -44
- package/src/server/services/skill-service.js +252 -2
- package/src/server/services/workspace-service.js +36 -18
- package/src/server/websocket-server.js +4 -1
- package/dist/web/assets/index-Bu1oPcKu.js +0 -4009
- package/dist/web/assets/index-XSok7-mN.css +0 -41
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置同步服务
|
|
3
|
+
*
|
|
4
|
+
* 支持 skills, rules, agents, commands 的在工作区和全局之间同步
|
|
5
|
+
*
|
|
6
|
+
* 配置位置:
|
|
7
|
+
* - 全局: ~/.claude/
|
|
8
|
+
* - skills/
|
|
9
|
+
* - rules/
|
|
10
|
+
* - agents/
|
|
11
|
+
* - commands/
|
|
12
|
+
* - 工作区: <project>/.claude/
|
|
13
|
+
* - rules/
|
|
14
|
+
* - agents/
|
|
15
|
+
* - commands/
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const os = require('os');
|
|
21
|
+
|
|
22
|
+
// 全局配置目录
|
|
23
|
+
const GLOBAL_CONFIG_DIR = path.join(os.homedir(), '.claude');
|
|
24
|
+
|
|
25
|
+
// 配置类型定义
|
|
26
|
+
const CONFIG_TYPES = {
|
|
27
|
+
skills: {
|
|
28
|
+
globalDir: 'skills',
|
|
29
|
+
projectDir: null, // skills 不支持项目级
|
|
30
|
+
isDirectory: true, // skills 是目录结构
|
|
31
|
+
markerFile: 'SKILL.md'
|
|
32
|
+
},
|
|
33
|
+
rules: {
|
|
34
|
+
globalDir: 'rules',
|
|
35
|
+
projectDir: 'rules',
|
|
36
|
+
isDirectory: false,
|
|
37
|
+
fileExtension: '.md'
|
|
38
|
+
},
|
|
39
|
+
agents: {
|
|
40
|
+
globalDir: 'agents',
|
|
41
|
+
projectDir: 'agents',
|
|
42
|
+
isDirectory: false,
|
|
43
|
+
fileExtension: '.md'
|
|
44
|
+
},
|
|
45
|
+
commands: {
|
|
46
|
+
globalDir: 'commands',
|
|
47
|
+
projectDir: 'commands',
|
|
48
|
+
isDirectory: false,
|
|
49
|
+
fileExtension: '.md'
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 确保目录存在
|
|
55
|
+
*/
|
|
56
|
+
function ensureDir(dirPath) {
|
|
57
|
+
if (!fs.existsSync(dirPath)) {
|
|
58
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 递归复制目录
|
|
64
|
+
*/
|
|
65
|
+
function copyDirRecursive(src, dest) {
|
|
66
|
+
ensureDir(dest);
|
|
67
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
68
|
+
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const srcPath = path.join(src, entry.name);
|
|
71
|
+
const destPath = path.join(dest, entry.name);
|
|
72
|
+
|
|
73
|
+
if (entry.isDirectory()) {
|
|
74
|
+
copyDirRecursive(srcPath, destPath);
|
|
75
|
+
} else {
|
|
76
|
+
fs.copyFileSync(srcPath, destPath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 配置同步服务类
|
|
83
|
+
*/
|
|
84
|
+
class ConfigSyncService {
|
|
85
|
+
constructor() {
|
|
86
|
+
this.globalConfigDir = GLOBAL_CONFIG_DIR;
|
|
87
|
+
ensureDir(this.globalConfigDir);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 获取可用的配置项列表
|
|
92
|
+
* @param {string} source - 'global' 或 'workspace'
|
|
93
|
+
* @param {string} projectPath - 工作区项目路径(source 为 workspace 时必需)
|
|
94
|
+
* @returns {Object} 各类型的配置项列表
|
|
95
|
+
*/
|
|
96
|
+
getAvailableConfigs(source, projectPath = null) {
|
|
97
|
+
const result = {
|
|
98
|
+
skills: [],
|
|
99
|
+
rules: [],
|
|
100
|
+
agents: [],
|
|
101
|
+
commands: []
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
for (const [type, config] of Object.entries(CONFIG_TYPES)) {
|
|
105
|
+
let dir;
|
|
106
|
+
|
|
107
|
+
if (source === 'global') {
|
|
108
|
+
dir = path.join(this.globalConfigDir, config.globalDir);
|
|
109
|
+
} else if (source === 'workspace' && projectPath) {
|
|
110
|
+
if (!config.projectDir) {
|
|
111
|
+
// skills 不支持项目级
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
dir = path.join(projectPath, '.claude', config.projectDir);
|
|
115
|
+
} else {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!fs.existsSync(dir)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (config.isDirectory) {
|
|
124
|
+
// Skills: 扫描目录
|
|
125
|
+
result[type] = this._scanSkillsDir(dir);
|
|
126
|
+
} else {
|
|
127
|
+
// Rules/Agents/Commands: 扫描 md 文件
|
|
128
|
+
result[type] = this._scanMdFiles(dir, config.fileExtension);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 扫描 skills 目录
|
|
137
|
+
*/
|
|
138
|
+
_scanSkillsDir(dir) {
|
|
139
|
+
const skills = [];
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
143
|
+
|
|
144
|
+
for (const entry of entries) {
|
|
145
|
+
if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
|
|
146
|
+
|
|
147
|
+
const skillPath = path.join(dir, entry.name);
|
|
148
|
+
const skillMdPath = path.join(skillPath, 'SKILL.md');
|
|
149
|
+
|
|
150
|
+
if (fs.existsSync(skillMdPath)) {
|
|
151
|
+
const content = fs.readFileSync(skillMdPath, 'utf-8');
|
|
152
|
+
const metadata = this._parseSkillMetadata(content);
|
|
153
|
+
|
|
154
|
+
// 获取文件列表
|
|
155
|
+
const files = this._getDirectoryFiles(skillPath);
|
|
156
|
+
|
|
157
|
+
skills.push({
|
|
158
|
+
name: metadata.name || entry.name,
|
|
159
|
+
directory: entry.name,
|
|
160
|
+
description: metadata.description || '',
|
|
161
|
+
files: files.length,
|
|
162
|
+
size: this._getDirSize(skillPath)
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} catch (err) {
|
|
167
|
+
console.error('[ConfigSync] Scan skills error:', err.message);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return skills;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 扫描 md 文件(rules/agents/commands)
|
|
175
|
+
*/
|
|
176
|
+
_scanMdFiles(dir, extension = '.md') {
|
|
177
|
+
const items = [];
|
|
178
|
+
|
|
179
|
+
const scan = (currentDir, relativePath = '') => {
|
|
180
|
+
try {
|
|
181
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
182
|
+
|
|
183
|
+
for (const entry of entries) {
|
|
184
|
+
if (entry.name.startsWith('.')) continue;
|
|
185
|
+
|
|
186
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
187
|
+
const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
|
|
188
|
+
|
|
189
|
+
if (entry.isDirectory()) {
|
|
190
|
+
scan(fullPath, relPath);
|
|
191
|
+
} else if (entry.name.endsWith(extension)) {
|
|
192
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
193
|
+
const metadata = this._parseFrontmatter(content);
|
|
194
|
+
const stats = fs.statSync(fullPath);
|
|
195
|
+
|
|
196
|
+
items.push({
|
|
197
|
+
name: metadata.name || path.basename(entry.name, extension),
|
|
198
|
+
path: relPath,
|
|
199
|
+
description: metadata.description || '',
|
|
200
|
+
size: stats.size
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} catch (err) {
|
|
205
|
+
// 忽略读取错误
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
scan(dir);
|
|
210
|
+
return items;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* 解析 SKILL.md 元数据
|
|
215
|
+
*/
|
|
216
|
+
_parseSkillMetadata(content) {
|
|
217
|
+
const result = { name: null, description: null };
|
|
218
|
+
|
|
219
|
+
// 匹配 YAML frontmatter
|
|
220
|
+
const match = content.match(/^---\s*\n([\s\S]*?)\n---/);
|
|
221
|
+
if (match) {
|
|
222
|
+
const yaml = match[1];
|
|
223
|
+
const nameMatch = yaml.match(/name:\s*["']?([^"'\n]+)["']?/);
|
|
224
|
+
const descMatch = yaml.match(/description:\s*["']?([^"'\n]+)["']?/);
|
|
225
|
+
|
|
226
|
+
if (nameMatch) result.name = nameMatch[1].trim();
|
|
227
|
+
if (descMatch) result.description = descMatch[1].trim();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 解析 frontmatter
|
|
235
|
+
*/
|
|
236
|
+
_parseFrontmatter(content) {
|
|
237
|
+
const result = { name: null, description: null };
|
|
238
|
+
|
|
239
|
+
const match = content.match(/^---\s*\n([\s\S]*?)\n---/);
|
|
240
|
+
if (match) {
|
|
241
|
+
const yaml = match[1];
|
|
242
|
+
const nameMatch = yaml.match(/name:\s*["']?([^"'\n]+)["']?/);
|
|
243
|
+
const descMatch = yaml.match(/description:\s*["']?([^"'\n]+)["']?/);
|
|
244
|
+
|
|
245
|
+
if (nameMatch) result.name = nameMatch[1].trim();
|
|
246
|
+
if (descMatch) result.description = descMatch[1].trim();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 获取目录下的文件列表
|
|
254
|
+
*/
|
|
255
|
+
_getDirectoryFiles(dir) {
|
|
256
|
+
const files = [];
|
|
257
|
+
|
|
258
|
+
const scan = (currentDir, relativePath = '') => {
|
|
259
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
260
|
+
|
|
261
|
+
for (const entry of entries) {
|
|
262
|
+
const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
|
|
263
|
+
|
|
264
|
+
if (entry.isDirectory()) {
|
|
265
|
+
scan(path.join(currentDir, entry.name), relPath);
|
|
266
|
+
} else {
|
|
267
|
+
files.push(relPath);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
scan(dir);
|
|
273
|
+
return files;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 获取目录大小
|
|
278
|
+
*/
|
|
279
|
+
_getDirSize(dir) {
|
|
280
|
+
let size = 0;
|
|
281
|
+
|
|
282
|
+
const scan = (currentDir) => {
|
|
283
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
284
|
+
|
|
285
|
+
for (const entry of entries) {
|
|
286
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
287
|
+
|
|
288
|
+
if (entry.isDirectory()) {
|
|
289
|
+
scan(fullPath);
|
|
290
|
+
} else {
|
|
291
|
+
size += fs.statSync(fullPath).size;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
scan(dir);
|
|
297
|
+
return size;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* 预览同步结果
|
|
302
|
+
* @param {Object} options
|
|
303
|
+
* @param {string} options.source - 'global' 或 'workspace'
|
|
304
|
+
* @param {string} options.target - 'global' 或 'workspace'
|
|
305
|
+
* @param {string[]} options.configTypes - 要同步的配置类型
|
|
306
|
+
* @param {string} options.projectPath - 工作区路径
|
|
307
|
+
* @param {Object} options.selectedItems - 选中的项目 { skills: [], rules: [], ... }
|
|
308
|
+
* @returns {Object} 预览结果
|
|
309
|
+
*/
|
|
310
|
+
previewSync(options) {
|
|
311
|
+
const { source, target, configTypes = [], projectPath, selectedItems = {} } = options;
|
|
312
|
+
|
|
313
|
+
const preview = {
|
|
314
|
+
willCreate: [],
|
|
315
|
+
willOverwrite: [],
|
|
316
|
+
willSkip: [],
|
|
317
|
+
errors: []
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// 验证参数
|
|
321
|
+
if (source === target) {
|
|
322
|
+
preview.errors.push('源和目标不能相同');
|
|
323
|
+
return preview;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (target === 'workspace' && !projectPath) {
|
|
327
|
+
preview.errors.push('同步到工作区需要指定项目路径');
|
|
328
|
+
return preview;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
for (const type of configTypes) {
|
|
332
|
+
const config = CONFIG_TYPES[type];
|
|
333
|
+
if (!config) continue;
|
|
334
|
+
|
|
335
|
+
// Skills 只支持全局
|
|
336
|
+
if (type === 'skills' && target === 'workspace') {
|
|
337
|
+
preview.errors.push('Skills 不支持同步到工作区级别');
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const items = selectedItems[type] || [];
|
|
342
|
+
|
|
343
|
+
for (const item of items) {
|
|
344
|
+
const targetPath = this._getTargetPath(type, item, target, projectPath);
|
|
345
|
+
|
|
346
|
+
if (fs.existsSync(targetPath)) {
|
|
347
|
+
preview.willOverwrite.push({
|
|
348
|
+
type,
|
|
349
|
+
name: item.name || item.directory || item.path,
|
|
350
|
+
targetPath
|
|
351
|
+
});
|
|
352
|
+
} else {
|
|
353
|
+
preview.willCreate.push({
|
|
354
|
+
type,
|
|
355
|
+
name: item.name || item.directory || item.path,
|
|
356
|
+
targetPath
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return preview;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 获取目标路径
|
|
367
|
+
*/
|
|
368
|
+
_getTargetPath(type, item, target, projectPath) {
|
|
369
|
+
const config = CONFIG_TYPES[type];
|
|
370
|
+
let baseDir;
|
|
371
|
+
|
|
372
|
+
if (target === 'global') {
|
|
373
|
+
baseDir = path.join(this.globalConfigDir, config.globalDir);
|
|
374
|
+
} else {
|
|
375
|
+
baseDir = path.join(projectPath, '.claude', config.projectDir);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (config.isDirectory) {
|
|
379
|
+
// Skills
|
|
380
|
+
return path.join(baseDir, item.directory);
|
|
381
|
+
} else {
|
|
382
|
+
// Rules/Agents/Commands
|
|
383
|
+
return path.join(baseDir, item.path);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 执行同步
|
|
389
|
+
* @param {Object} options
|
|
390
|
+
* @param {string} options.source - 'global' 或 'workspace'
|
|
391
|
+
* @param {string} options.target - 'global' 或 'workspace'
|
|
392
|
+
* @param {string[]} options.configTypes - 要同步的配置类型
|
|
393
|
+
* @param {string} options.projectPath - 工作区路径
|
|
394
|
+
* @param {Object} options.selectedItems - 选中的项目
|
|
395
|
+
* @param {boolean} options.overwrite - 是否覆盖已存在的
|
|
396
|
+
* @returns {Object} 同步结果
|
|
397
|
+
*/
|
|
398
|
+
executeSync(options) {
|
|
399
|
+
const { source, target, configTypes = [], projectPath, selectedItems = {}, overwrite = false } = options;
|
|
400
|
+
|
|
401
|
+
const result = {
|
|
402
|
+
success: [],
|
|
403
|
+
failed: [],
|
|
404
|
+
skipped: []
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
for (const type of configTypes) {
|
|
408
|
+
const config = CONFIG_TYPES[type];
|
|
409
|
+
if (!config) continue;
|
|
410
|
+
|
|
411
|
+
// Skills 只支持全局
|
|
412
|
+
if (type === 'skills' && target === 'workspace') {
|
|
413
|
+
result.failed.push({
|
|
414
|
+
type,
|
|
415
|
+
name: 'skills',
|
|
416
|
+
error: 'Skills 不支持同步到工作区级别'
|
|
417
|
+
});
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const items = selectedItems[type] || [];
|
|
422
|
+
|
|
423
|
+
for (const item of items) {
|
|
424
|
+
try {
|
|
425
|
+
const sourcePath = this._getSourcePath(type, item, source, projectPath);
|
|
426
|
+
const targetPath = this._getTargetPath(type, item, target, projectPath);
|
|
427
|
+
|
|
428
|
+
// 检查目标是否存在
|
|
429
|
+
if (fs.existsSync(targetPath) && !overwrite) {
|
|
430
|
+
result.skipped.push({
|
|
431
|
+
type,
|
|
432
|
+
name: item.name || item.directory || item.path,
|
|
433
|
+
reason: '已存在'
|
|
434
|
+
});
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// 确保目标目录存在
|
|
439
|
+
ensureDir(path.dirname(targetPath));
|
|
440
|
+
|
|
441
|
+
// 执行复制
|
|
442
|
+
if (config.isDirectory) {
|
|
443
|
+
copyDirRecursive(sourcePath, targetPath);
|
|
444
|
+
} else {
|
|
445
|
+
fs.copyFileSync(sourcePath, targetPath);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
result.success.push({
|
|
449
|
+
type,
|
|
450
|
+
name: item.name || item.directory || item.path
|
|
451
|
+
});
|
|
452
|
+
} catch (err) {
|
|
453
|
+
result.failed.push({
|
|
454
|
+
type,
|
|
455
|
+
name: item.name || item.directory || item.path,
|
|
456
|
+
error: err.message
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return result;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* 获取源路径
|
|
467
|
+
*/
|
|
468
|
+
_getSourcePath(type, item, source, projectPath) {
|
|
469
|
+
const config = CONFIG_TYPES[type];
|
|
470
|
+
let baseDir;
|
|
471
|
+
|
|
472
|
+
if (source === 'global') {
|
|
473
|
+
baseDir = path.join(this.globalConfigDir, config.globalDir);
|
|
474
|
+
} else {
|
|
475
|
+
baseDir = path.join(projectPath, '.claude', config.projectDir);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (config.isDirectory) {
|
|
479
|
+
// Skills
|
|
480
|
+
return path.join(baseDir, item.directory);
|
|
481
|
+
} else {
|
|
482
|
+
// Rules/Agents/Commands
|
|
483
|
+
return path.join(baseDir, item.path);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* 获取同步统计信息
|
|
489
|
+
*/
|
|
490
|
+
getStats(projectPath = null) {
|
|
491
|
+
const globalConfigs = this.getAvailableConfigs('global');
|
|
492
|
+
const workspaceConfigs = projectPath
|
|
493
|
+
? this.getAvailableConfigs('workspace', projectPath)
|
|
494
|
+
: { skills: [], rules: [], agents: [], commands: [] };
|
|
495
|
+
|
|
496
|
+
return {
|
|
497
|
+
global: {
|
|
498
|
+
skills: globalConfigs.skills.length,
|
|
499
|
+
rules: globalConfigs.rules.length,
|
|
500
|
+
agents: globalConfigs.agents.length,
|
|
501
|
+
commands: globalConfigs.commands.length
|
|
502
|
+
},
|
|
503
|
+
workspace: {
|
|
504
|
+
rules: workspaceConfigs.rules.length,
|
|
505
|
+
agents: workspaceConfigs.agents.length,
|
|
506
|
+
commands: workspaceConfigs.commands.length
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
module.exports = {
|
|
513
|
+
ConfigSyncService,
|
|
514
|
+
CONFIG_TYPES
|
|
515
|
+
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Cache Manager
|
|
3
|
+
*
|
|
4
|
+
* 提供全局缓存管理,支持TTL、自动清理、LRU策略
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class CacheManager {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.caches = new Map();
|
|
10
|
+
this.ttls = new Map();
|
|
11
|
+
this.accessTimes = new Map();
|
|
12
|
+
this.maxSize = options.maxSize || 1000; // 最大缓存条目数
|
|
13
|
+
this.defaultTTL = options.defaultTTL || 300000; // 默认5分钟
|
|
14
|
+
this.cleanupInterval = options.cleanupInterval || 60000; // 1分钟清理一次
|
|
15
|
+
|
|
16
|
+
// 启动自动清理
|
|
17
|
+
this.startCleanup();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 设置缓存
|
|
22
|
+
* @param {string} key - 缓存键
|
|
23
|
+
* @param {any} value - 缓存值
|
|
24
|
+
* @param {number} ttl - 过期时间(毫秒),默认使用defaultTTL
|
|
25
|
+
*/
|
|
26
|
+
set(key, value, ttl) {
|
|
27
|
+
const expiryTime = Date.now() + (ttl || this.defaultTTL);
|
|
28
|
+
|
|
29
|
+
// 如果缓存已满,清理最少使用的条目
|
|
30
|
+
if (this.caches.size >= this.maxSize) {
|
|
31
|
+
this.evictLRU();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.caches.set(key, value);
|
|
35
|
+
this.ttls.set(key, expiryTime);
|
|
36
|
+
this.accessTimes.set(key, Date.now());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 获取缓存
|
|
41
|
+
* @param {string} key - 缓存键
|
|
42
|
+
* @returns {any|null} 缓存值,不存在或过期返回null
|
|
43
|
+
*/
|
|
44
|
+
get(key) {
|
|
45
|
+
if (!this.caches.has(key)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 检查是否过期
|
|
50
|
+
if (Date.now() > this.ttls.get(key)) {
|
|
51
|
+
this.delete(key);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 更新访问时间(LRU)
|
|
56
|
+
this.accessTimes.set(key, Date.now());
|
|
57
|
+
return this.caches.get(key);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 删除缓存
|
|
62
|
+
* @param {string} key - 缓存键
|
|
63
|
+
*/
|
|
64
|
+
delete(key) {
|
|
65
|
+
this.caches.delete(key);
|
|
66
|
+
this.ttls.delete(key);
|
|
67
|
+
this.accessTimes.delete(key);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 清空所有缓存
|
|
72
|
+
*/
|
|
73
|
+
clear() {
|
|
74
|
+
this.caches.clear();
|
|
75
|
+
this.ttls.clear();
|
|
76
|
+
this.accessTimes.clear();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 检查缓存是否存在且有效
|
|
81
|
+
* @param {string} key - 缓存键
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
has(key) {
|
|
85
|
+
return this.get(key) !== null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 获取或设置缓存(如果不存在则调用工厂函数)
|
|
90
|
+
* @param {string} key - 缓存键
|
|
91
|
+
* @param {Function} factory - 工厂函数,返回值或Promise
|
|
92
|
+
* @param {number} ttl - 过期时间
|
|
93
|
+
* @returns {any|Promise<any>}
|
|
94
|
+
*/
|
|
95
|
+
async getOrSet(key, factory, ttl) {
|
|
96
|
+
const cached = this.get(key);
|
|
97
|
+
if (cached !== null) {
|
|
98
|
+
return cached;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const value = await factory();
|
|
102
|
+
this.set(key, value, ttl);
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* LRU驱逐策略:移除最久未使用的条目
|
|
108
|
+
*/
|
|
109
|
+
evictLRU() {
|
|
110
|
+
let oldestKey = null;
|
|
111
|
+
let oldestTime = Infinity;
|
|
112
|
+
|
|
113
|
+
for (const [key, accessTime] of this.accessTimes) {
|
|
114
|
+
if (accessTime < oldestTime) {
|
|
115
|
+
oldestTime = accessTime;
|
|
116
|
+
oldestKey = key;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (oldestKey) {
|
|
121
|
+
this.delete(oldestKey);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 启动自动清理过期缓存
|
|
127
|
+
*/
|
|
128
|
+
startCleanup() {
|
|
129
|
+
this.cleanupTimer = setInterval(() => {
|
|
130
|
+
const now = Date.now();
|
|
131
|
+
const expiredKeys = [];
|
|
132
|
+
|
|
133
|
+
for (const [key, expiryTime] of this.ttls) {
|
|
134
|
+
if (now > expiryTime) {
|
|
135
|
+
expiredKeys.push(key);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
expiredKeys.forEach(key => this.delete(key));
|
|
140
|
+
|
|
141
|
+
if (expiredKeys.length > 0) {
|
|
142
|
+
console.log(`[CacheManager] Cleaned up ${expiredKeys.length} expired entries`);
|
|
143
|
+
}
|
|
144
|
+
}, this.cleanupInterval);
|
|
145
|
+
|
|
146
|
+
// 确保Node.js进程退出时清理定时器
|
|
147
|
+
if (this.cleanupTimer.unref) {
|
|
148
|
+
this.cleanupTimer.unref();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 停止自动清理
|
|
154
|
+
*/
|
|
155
|
+
stopCleanup() {
|
|
156
|
+
if (this.cleanupTimer) {
|
|
157
|
+
clearInterval(this.cleanupTimer);
|
|
158
|
+
this.cleanupTimer = null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 获取缓存统计信息
|
|
164
|
+
* @returns {object}
|
|
165
|
+
*/
|
|
166
|
+
getStats() {
|
|
167
|
+
return {
|
|
168
|
+
size: this.caches.size,
|
|
169
|
+
maxSize: this.maxSize,
|
|
170
|
+
keys: Array.from(this.caches.keys())
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 创建全局缓存实例
|
|
176
|
+
const globalCache = new CacheManager({
|
|
177
|
+
maxSize: 1000,
|
|
178
|
+
defaultTTL: 300000, // 5分钟
|
|
179
|
+
cleanupInterval: 60000 // 1分钟
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// 预定义的缓存键前缀
|
|
183
|
+
const CacheKeys = {
|
|
184
|
+
PROJECTS: 'projects:',
|
|
185
|
+
SESSIONS: 'sessions:',
|
|
186
|
+
SKILLS: 'skills:',
|
|
187
|
+
CONFIG_TEMPLATES: 'config-templates:',
|
|
188
|
+
REPOS: 'repos:',
|
|
189
|
+
HAS_MESSAGES: 'has-messages:'
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
module.exports = {
|
|
193
|
+
CacheManager,
|
|
194
|
+
globalCache,
|
|
195
|
+
CacheKeys
|
|
196
|
+
};
|