@workskills/getskill 2.0.0 → 2.1.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/dist/lib/state.js CHANGED
@@ -7,6 +7,7 @@ exports.normalizeSkillName = normalizeSkillName;
7
7
  exports.getRemoteSkillPath = getRemoteSkillPath;
8
8
  exports.normalizeCliSkillArgument = normalizeCliSkillArgument;
9
9
  exports.collectSkillMarkdownFiles = collectSkillMarkdownFiles;
10
+ exports.migrateLegacyInstallState = migrateLegacyInstallState;
10
11
  exports.readInstallState = readInstallState;
11
12
  exports.writeInstallState = writeInstallState;
12
13
  exports.updateInstallState = updateInstallState;
@@ -16,6 +17,7 @@ exports.replaceDirectoryAtomically = replaceDirectoryAtomically;
16
17
  exports.copySkillFiles = copySkillFiles;
17
18
  exports.listLocalSkills = listLocalSkills;
18
19
  exports.listInstalledSkillNames = listInstalledSkillNames;
20
+ exports.pruneInstallState = pruneInstallState;
19
21
  exports.getInstalledSkillInfo = getInstalledSkillInfo;
20
22
  exports.removeInstalledSkill = removeInstalledSkill;
21
23
  const fs_1 = __importDefault(require("fs"));
@@ -23,6 +25,7 @@ const path_1 = __importDefault(require("path"));
23
25
  const config_1 = require("./config");
24
26
  const logger_1 = __importDefault(require("./logger"));
25
27
  const system_1 = require("./system");
28
+ const unzip_1 = require("./unzip");
26
29
  function normalizeSkillName(skillName) {
27
30
  return skillName.replace(/^skills\//, '').replace(/[\\/]/g, '_');
28
31
  }
@@ -65,13 +68,8 @@ function collectSkillMarkdownFiles(dirPath, baseDir = dirPath, result = []) {
65
68
  });
66
69
  return result;
67
70
  }
68
- function readInstallState() {
69
- const stateFilePath = (0, config_1.getStateFilePath)();
70
- if (!fs_1.default.existsSync(stateFilePath)) {
71
- return { skills: {} };
72
- }
71
+ function parseInstallState(content) {
73
72
  try {
74
- const content = fs_1.default.readFileSync(stateFilePath, 'utf8');
75
73
  const parsed = JSON.parse(content);
76
74
  if (!parsed || typeof parsed !== 'object') {
77
75
  return { skills: {} };
@@ -86,6 +84,47 @@ function readInstallState() {
86
84
  return { skills: {} };
87
85
  }
88
86
  }
87
+ /**
88
+ * 兼容 1.x/2.0 版本:状态文件曾存放在 `<agent 根目录>/getskill/state.json`。
89
+ * 首次读取时自动迁移到 `~/.getskill/state.json`。
90
+ */
91
+ function migrateLegacyInstallState() {
92
+ const stateFilePath = (0, config_1.getStateFilePath)();
93
+ if (fs_1.default.existsSync(stateFilePath)) {
94
+ return null;
95
+ }
96
+ for (const legacyPath of (0, config_1.getLegacyStateFilePaths)()) {
97
+ if (!fs_1.default.existsSync(legacyPath)) {
98
+ continue;
99
+ }
100
+ try {
101
+ const content = fs_1.default.readFileSync(legacyPath, 'utf8');
102
+ (0, system_1.ensureDirectoryExists)(path_1.default.dirname(stateFilePath));
103
+ fs_1.default.writeFileSync(stateFilePath, content, 'utf8');
104
+ logger_1.default.info(`已迁移历史安装记录: ${legacyPath} -> ${stateFilePath}`);
105
+ return legacyPath;
106
+ }
107
+ catch (error) {
108
+ logger_1.default.debug(`迁移历史安装记录失败 (${legacyPath}): ${error instanceof Error ? error.message : String(error)}`);
109
+ }
110
+ }
111
+ return null;
112
+ }
113
+ function readInstallState() {
114
+ const stateFilePath = (0, config_1.getStateFilePath)();
115
+ if (!fs_1.default.existsSync(stateFilePath)) {
116
+ migrateLegacyInstallState();
117
+ }
118
+ if (!fs_1.default.existsSync(stateFilePath)) {
119
+ return { skills: {} };
120
+ }
121
+ try {
122
+ return parseInstallState(fs_1.default.readFileSync(stateFilePath, 'utf8'));
123
+ }
124
+ catch {
125
+ return { skills: {} };
126
+ }
127
+ }
89
128
  function writeInstallState(state) {
90
129
  const stateFilePath = (0, config_1.getStateFilePath)();
91
130
  (0, system_1.ensureDirectoryExists)(path_1.default.dirname(stateFilePath));
@@ -108,7 +147,9 @@ async function updateInstallState(skillName, repoPath, installDir, action, reque
108
147
  updatedAt: now,
109
148
  gitCommitHash: previous?.gitCommitHash || null,
110
149
  requestedVersion: requestedVersion ?? previous?.requestedVersion ?? null,
111
- version: version ?? previous?.version ?? null
150
+ version: version ?? previous?.version ?? null,
151
+ agent: (0, config_1.getAgentName)(),
152
+ skillsDir: (0, config_1.getSkillsDirectory)()
112
153
  };
113
154
  writeInstallState(state);
114
155
  }
@@ -201,9 +242,27 @@ function listLocalSkills() {
201
242
  .filter((entry) => entry.isDirectory())
202
243
  .map((entry) => entry.name);
203
244
  }
245
+ function recordSkillsDir(record) {
246
+ if (record.skillsDir) {
247
+ return record.skillsDir;
248
+ }
249
+ // 2.0 及更早版本的记录未保存技能目录,用安装目录的父目录推断
250
+ if (record.installDir) {
251
+ return path_1.default.dirname(record.installDir);
252
+ }
253
+ return null;
254
+ }
255
+ function recordMatchesCurrentSkillsDir(record) {
256
+ const recordDir = recordSkillsDir(record);
257
+ if (!recordDir) {
258
+ return true;
259
+ }
260
+ return (0, config_1.isSamePath)(recordDir, (0, config_1.getSkillsDirectory)());
261
+ }
204
262
  function listInstalledSkillNames() {
205
263
  const state = readInstallState();
206
264
  const stateNames = Object.values(state.skills)
265
+ .filter(recordMatchesCurrentSkillsDir)
207
266
  .map((item) => item.inputSkillName || item.localSkillName)
208
267
  .filter(Boolean);
209
268
  if (stateNames.length > 0) {
@@ -211,13 +270,52 @@ function listInstalledSkillNames() {
211
270
  }
212
271
  return listLocalSkills();
213
272
  }
273
+ /**
274
+ * 清理状态文件中的失效记录。
275
+ * 不传参数时清理不属于当前技能目录(Agent)的记录,
276
+ * 传入技能名时只清理指定记录。返回被清理的技能名列表。
277
+ */
278
+ function pruneInstallState(skillNames = []) {
279
+ const state = readInstallState();
280
+ const targets = new Set(skillNames.map((name) => normalizeSkillName(name)).filter(Boolean));
281
+ const removed = [];
282
+ Object.keys(state.skills).forEach((localSkillName) => {
283
+ const record = state.skills[localSkillName];
284
+ const shouldRemove = targets.size > 0
285
+ ? targets.has(localSkillName)
286
+ : !recordMatchesCurrentSkillsDir(record);
287
+ if (shouldRemove) {
288
+ delete state.skills[localSkillName];
289
+ removed.push(record.inputSkillName || localSkillName);
290
+ }
291
+ });
292
+ if (removed.length > 0) {
293
+ writeInstallState(state);
294
+ }
295
+ return removed;
296
+ }
297
+ /**
298
+ * 解析技能的实际解压目录:
299
+ * 优先使用安装记录中的 repoPath,其次解析缓存目录中的单层包装目录。
300
+ */
301
+ function resolveCachePaths(localSkillName, record) {
302
+ const cacheDir = path_1.default.join((0, config_1.getSkillsCacheDirectory)(), localSkillName);
303
+ if (record?.repoPath && fs_1.default.existsSync(record.repoPath)) {
304
+ return { cacheDir, repoPath: record.repoPath };
305
+ }
306
+ if (fs_1.default.existsSync(cacheDir)) {
307
+ return { cacheDir, repoPath: (0, unzip_1.resolveSkillRoot)(cacheDir) };
308
+ }
309
+ return { cacheDir, repoPath: cacheDir };
310
+ }
214
311
  function getInstalledSkillInfo(skillName) {
215
312
  const normalizedSkillName = normalizeCliSkillArgument(skillName);
216
313
  const localSkillName = normalizeSkillName(normalizedSkillName);
217
314
  const state = readInstallState();
218
315
  const record = state.skills[localSkillName] || null;
219
- const skillDir = path_1.default.join((0, config_1.getSkillsDirectory)(), localSkillName);
220
- const repoPath = path_1.default.join((0, config_1.getSkillsCacheDirectory)(), localSkillName);
316
+ const skillsDir = (0, config_1.getSkillsDirectory)();
317
+ const skillDir = path_1.default.join(skillsDir, localSkillName);
318
+ const { cacheDir, repoPath } = resolveCachePaths(localSkillName, record);
221
319
  return {
222
320
  skillName: normalizedSkillName,
223
321
  localSkillName,
@@ -225,6 +323,10 @@ function getInstalledSkillInfo(skillName) {
225
323
  cached: fs_1.default.existsSync(repoPath),
226
324
  skillDir,
227
325
  repoPath,
326
+ cacheDir,
327
+ skillsDir,
328
+ agent: (0, config_1.getAgentName)(),
329
+ agentLabel: (0, config_1.getAgentLabel)(),
228
330
  state: record
229
331
  };
230
332
  }
@@ -232,17 +334,27 @@ function removeInstalledSkill(skillName, options = {}) {
232
334
  const normalizedSkillName = normalizeCliSkillArgument(skillName);
233
335
  const localSkillName = normalizeSkillName(normalizedSkillName);
234
336
  const removeCache = options.removeCache !== false;
337
+ const state = readInstallState();
338
+ const record = state.skills[localSkillName] || null;
235
339
  const skillDir = path_1.default.join((0, config_1.getSkillsDirectory)(), localSkillName);
236
- const repoPath = path_1.default.join((0, config_1.getSkillsCacheDirectory)(), localSkillName);
340
+ const { cacheDir, repoPath } = resolveCachePaths(localSkillName, record);
237
341
  let removedInstall = false;
238
342
  let removedCache = false;
239
343
  if (fs_1.default.existsSync(skillDir)) {
240
344
  fs_1.default.rmSync(skillDir, { recursive: true, force: true });
241
345
  removedInstall = true;
242
346
  }
243
- if (removeCache && fs_1.default.existsSync(repoPath)) {
244
- fs_1.default.rmSync(repoPath, { recursive: true, force: true });
245
- removedCache = true;
347
+ if (removeCache) {
348
+ if (fs_1.default.existsSync(cacheDir)) {
349
+ fs_1.default.rmSync(cacheDir, { recursive: true, force: true });
350
+ removedCache = true;
351
+ }
352
+ if (!(0, config_1.isSamePath)(repoPath, cacheDir) &&
353
+ !repoPath.startsWith(`${cacheDir}${path_1.default.sep}`) &&
354
+ fs_1.default.existsSync(repoPath)) {
355
+ fs_1.default.rmSync(repoPath, { recursive: true, force: true });
356
+ removedCache = true;
357
+ }
246
358
  }
247
359
  removeInstallState(normalizedSkillName);
248
360
  return {
@@ -251,6 +363,6 @@ function removeInstalledSkill(skillName, options = {}) {
251
363
  removedInstall,
252
364
  removedCache,
253
365
  skillDir,
254
- repoPath
366
+ repoPath: cacheDir
255
367
  };
256
368
  }
@@ -63,6 +63,8 @@ export interface SkillStateRecord {
63
63
  gitCommitHash: string | null;
64
64
  requestedVersion?: string | null;
65
65
  version?: string | null;
66
+ agent?: string | null;
67
+ skillsDir?: string | null;
66
68
  }
67
69
  export interface InstallState {
68
70
  skills: Record<string, SkillStateRecord>;
@@ -78,9 +80,15 @@ export interface DownloadResult {
78
80
  export interface DownloadOptions {
79
81
  version?: string;
80
82
  }
83
+ export interface UpdateAllOptions {
84
+ /** 同时清理索引中已不存在的历史安装记录。 */
85
+ prune?: boolean;
86
+ }
81
87
  export interface UpdateAllResult {
82
88
  skillName: string;
83
89
  ok: boolean;
90
+ /** 索引中已不存在该技能(历史记录失效),非失败。 */
91
+ skipped?: boolean;
84
92
  result?: DownloadResult;
85
93
  error?: string;
86
94
  }
@@ -90,7 +98,13 @@ export interface InstalledSkillInfo {
90
98
  installed: boolean;
91
99
  cached: boolean;
92
100
  skillDir: string;
101
+ /** 解压缓存目录(ZIP 解压根目录)。 */
93
102
  repoPath: string;
103
+ /** 缓存根目录下的技能目录,可能不存在。 */
104
+ cacheDir: string;
105
+ skillsDir: string;
106
+ agent: string;
107
+ agentLabel: string;
94
108
  state: SkillStateRecord | null;
95
109
  }
96
110
  export interface RemoveInstalledSkillResult {
package/dist/main.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  #!/usr/bin/env node
2
- import { getBaseUrl, getSkillsCacheDirectory, getSkillsDirectory, setBaseUrl } from './lib/config';
2
+ import { getAgentLabel, getAgentName, getBaseUrl, getConfigFilePath, getConfigSummary, getSkillsCacheDirectory, getSkillsDirectory, getStateFilePath, listAgents, setAgent, setBaseUrl, setCacheDir, setConfigValue, setSkillsDir, unsetConfigValue } from './lib/config';
3
3
  import { repositoryHasSkillFile, searchSkills } from './lib/network';
4
- import { copySkillFiles, getInstalledSkillInfo, listLocalSkills } from './lib/state';
5
- import { cloneOrUpdateRepo, downloadSkill, uninstallSkill, updateAllSkills, updateSkill } from './lib/skill-manager';
4
+ import { copySkillFiles, getInstalledSkillInfo, listLocalSkills, pruneInstallState } from './lib/state';
5
+ import { cloneOrUpdateRepo, downloadSkill, restartAgentGateway, uninstallSkill, updateAllSkills, updateSkill } from './lib/skill-manager';
6
6
  export declare function printHelp(): void;
7
7
  export declare function getCliVersion(): string;
8
+ export declare function printConfigUsage(): void;
9
+ export declare function printConfigValues(key?: string): void;
10
+ export declare function printAgents(): void;
11
+ export declare function printPaths(): void;
12
+ export declare function handleConfigCommand(args: string[]): void;
8
13
  export declare function cli(): Promise<void>;
9
- export { searchSkills, downloadSkill, updateSkill, updateAllSkills, uninstallSkill, listLocalSkills, getInstalledSkillInfo, getSkillsDirectory, getSkillsCacheDirectory, cloneOrUpdateRepo, copySkillFiles, repositoryHasSkillFile, setBaseUrl, getBaseUrl };
14
+ export { searchSkills, downloadSkill, updateSkill, updateAllSkills, uninstallSkill, listLocalSkills, getInstalledSkillInfo, pruneInstallState, restartAgentGateway, getSkillsDirectory, getSkillsCacheDirectory, getConfigSummary, getConfigFilePath, getStateFilePath, listAgents, getAgentName, getAgentLabel, setConfigValue, unsetConfigValue, setBaseUrl, setAgent, setSkillsDir, setCacheDir, getBaseUrl, cloneOrUpdateRepo, copySkillFiles, repositoryHasSkillFile };