更新 lib/skill-admin.js
This commit is contained in:
parent
9f662609a3
commit
241cc7679a
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* Local skill management, built into the plugin: list, enable/disable,
|
* 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
|
* 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
|
* 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
|
* mistaken for deletions, and a parked cloud skill still receives its
|
||||||
* cloud updates in place.
|
* cloud updates in place.
|
||||||
*
|
*
|
||||||
* Deletes are never final here: removing a skill moves it into the
|
* Delete is a real delete: the folder is removed recursively. The
|
||||||
* plugin trash (`$DSH_HOME/cloud-memory-trash/`) with an index entry,
|
* cloud copy is unaffected — local deletion never propagates to the
|
||||||
* and restore puts it back exactly where it was.
|
* 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 { 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 { basename, join } from 'node:path';
|
||||||
import { parseSkillFrontmatter } from "./skill-sync.js";
|
import { parseSkillFrontmatter } from "./skill-sync.js";
|
||||||
import { dshHome } from "./store.js";
|
import { dshHome } from "./store.js";
|
||||||
|
|
@ -27,10 +28,6 @@ import { dshHome } from "./store.js";
|
||||||
export function skillsHoldingDir() {
|
export function skillsHoldingDir() {
|
||||||
return join(dshHome(), 'skills-disabled');
|
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. */
|
/** A management failure with a stable machine-readable code. */
|
||||||
export class SkillAdminError extends Error {
|
export class SkillAdminError extends Error {
|
||||||
code;
|
code;
|
||||||
|
|
@ -134,7 +131,7 @@ export class SkillAdmin {
|
||||||
files: await countFiles(dir),
|
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() {
|
async list() {
|
||||||
const uploaded = await this.syncUploaded();
|
const uploaded = await this.syncUploaded();
|
||||||
const skills = [];
|
const skills = [];
|
||||||
|
|
@ -150,7 +147,7 @@ export class SkillAdmin {
|
||||||
await scan(this.deps.skillsRoot(), true);
|
await scan(this.deps.skillsRoot(), true);
|
||||||
await scan(this.deps.holdingDir(), false);
|
await scan(this.deps.holdingDir(), false);
|
||||||
skills.sort((a, b) => (a.folder < b.folder ? -1 : 1));
|
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. */
|
/** Locate a skill folder across the root and the parking area. */
|
||||||
async locate(folder) {
|
async locate(folder) {
|
||||||
|
|
@ -181,69 +178,16 @@ export class SkillAdmin {
|
||||||
this.deps.log(`skill ${enabled ? 'enabled' : 'disabled'}: ${folder}`);
|
this.deps.log(`skill ${enabled ? 'enabled' : 'disabled'}: ${folder}`);
|
||||||
return { folder, enabled };
|
return { folder, enabled };
|
||||||
}
|
}
|
||||||
async readTrashIndex() {
|
/** Delete one skill for real: its folder is removed recursively. */
|
||||||
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). */
|
|
||||||
async remove(folderInput) {
|
async remove(folderInput) {
|
||||||
this.guardSync();
|
this.guardSync();
|
||||||
const folder = checkFolder(folderInput);
|
const folder = checkFolder(folderInput);
|
||||||
const found = await this.locate(folder);
|
const found = await this.locate(folder);
|
||||||
if (found === undefined)
|
if (found === undefined)
|
||||||
fail('not_found', `没有找到技能:${folder}`);
|
fail('not_found', `没有找到技能:${folder}`);
|
||||||
const described = await this.describe(folder, found.dir, found.area === 'root', {});
|
await rm(found.dir, { recursive: true, force: true });
|
||||||
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
this.deps.log(`skill deleted: ${folder}`);
|
||||||
const id = `${folder.replace(/[<>:"/\\|?*]/g, '_')}--${stamp}`;
|
return { folder };
|
||||||
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 };
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Install every skill found in a git repository (https:// or file://).
|
* Install every skill found in a git repository (https:// or file://).
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue