更新 lib/skill-admin.js

This commit is contained in:
QQ 2026-11-11 11:33:46 +08:00
parent 9f662609a3
commit 241cc7679a
1 changed files with 12 additions and 68 deletions

View File

@ -1,6 +1,6 @@
/**
* Local skill management, built into the plugin: list, enable/disable,
* delete-to-trash with restore, and install from a git repository.
* real delete, and install from a git repository.
*
* This covers the everyday subset of a full skills manager. It manages
* only the DSH skills root (plus its own parking and trash areas) — the
@ -14,12 +14,13 @@
* mistaken for deletions, and a parked cloud skill still receives its
* cloud updates in place.
*
* Deletes are never final here: removing a skill moves it into the
* plugin trash (`$DSH_HOME/cloud-memory-trash/`) with an index entry,
* and restore puts it back exactly where it was.
* Delete is a real delete: the folder is removed recursively. The
* cloud copy is unaffected — local deletion never propagates to the
* center (the sync engine remembers the removal, so the same cloud
* version is not pulled back down either).
*/
import { execFile } from 'node:child_process';
import { cp, mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises';
import { cp, mkdir, readFile, readdir, rename, rm, stat } from 'node:fs/promises';
import { basename, join } from 'node:path';
import { parseSkillFrontmatter } from "./skill-sync.js";
import { dshHome } from "./store.js";
@ -27,10 +28,6 @@ import { dshHome } from "./store.js";
export function skillsHoldingDir() {
return join(dshHome(), 'skills-disabled');
}
/** The plugin trash directory. */
export function skillsTrashDir() {
return join(dshHome(), 'cloud-memory-trash');
}
/** A management failure with a stable machine-readable code. */
export class SkillAdminError extends Error {
code;
@ -134,7 +131,7 @@ export class SkillAdmin {
files: await countFiles(dir),
};
}
/** Inventory of the skills root, the parking area, and the trash. */
/** Inventory of the skills root and the parking area. */
async list() {
const uploaded = await this.syncUploaded();
const skills = [];
@ -150,7 +147,7 @@ export class SkillAdmin {
await scan(this.deps.skillsRoot(), true);
await scan(this.deps.holdingDir(), false);
skills.sort((a, b) => (a.folder < b.folder ? -1 : 1));
return { skills, trash: (await this.readTrashIndex()).entries };
return { skills };
}
/** Locate a skill folder across the root and the parking area. */
async locate(folder) {
@ -181,69 +178,16 @@ export class SkillAdmin {
this.deps.log(`skill ${enabled ? 'enabled' : 'disabled'}: ${folder}`);
return { folder, enabled };
}
async readTrashIndex() {
try {
const parsed = JSON.parse(await readFile(join(this.deps.trashDir(), 'index.json'), 'utf8'));
if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
const entries = parsed.entries;
if (Array.isArray(entries))
return { entries: entries };
}
}
catch {
// No trash yet.
}
return { entries: [] };
}
async writeTrashIndex(index) {
await mkdir(this.deps.trashDir(), { recursive: true });
await writeFile(join(this.deps.trashDir(), 'index.json'), JSON.stringify(index, null, 2));
}
/** Move one skill into the trash (never a hard delete). */
/** Delete one skill for real: its folder is removed recursively. */
async remove(folderInput) {
this.guardSync();
const folder = checkFolder(folderInput);
const found = await this.locate(folder);
if (found === undefined)
fail('not_found', `没有找到技能:${folder}`);
const described = await this.describe(folder, found.dir, found.area === 'root', {});
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
const id = `${folder.replace(/[<>:"/\\|?*]/g, '_')}--${stamp}`;
await mkdir(this.deps.trashDir(), { recursive: true });
await rename(found.dir, join(this.deps.trashDir(), id));
const entry = {
id,
folder,
name: described?.name ?? folder,
from: found.area,
removedAt: new Date().toISOString(),
};
const index = await this.readTrashIndex();
index.entries.push(entry);
await this.writeTrashIndex(index);
this.deps.log(`skill trashed: ${folder}`);
return entry;
}
/** Put one trashed skill back where it was removed from. */
async restore(idInput) {
this.guardSync();
if (typeof idInput !== 'string' || idInput === '' || idInput.includes('/') || idInput.includes('\\')) {
fail('invalid_name', '回收站条目无效。');
}
const index = await this.readTrashIndex();
const entry = index.entries.find((item) => item.id === idInput);
if (entry === undefined)
fail('not_found', '回收站里没有这条记录。');
const targetRoot = entry.from === 'holding' ? this.deps.holdingDir() : this.deps.skillsRoot();
await mkdir(targetRoot, { recursive: true });
const target = join(targetRoot, entry.folder);
if (await isDirectory(target))
fail('collision', `已有同名技能占用了位置:${entry.folder}`);
await rename(join(this.deps.trashDir(), entry.id), target);
index.entries = index.entries.filter((item) => item.id !== entry.id);
await this.writeTrashIndex(index);
this.deps.log(`skill restored: ${entry.folder}`);
return { folder: entry.folder };
await rm(found.dir, { recursive: true, force: true });
this.deps.log(`skill deleted: ${folder}`);
return { folder };
}
/**
* Install every skill found in a git repository (https:// or file://).