diff --git a/lib/skill-groups.js b/lib/skill-groups.js index 793ca06..7b453cb 100644 --- a/lib/skill-groups.js +++ b/lib/skill-groups.js @@ -18,10 +18,10 @@ */ import { execFile } from 'node:child_process'; import { randomUUID } from 'node:crypto'; -import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'; +import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync } from 'node:fs'; import { readdir, readFile } from 'node:fs/promises'; import { homedir } from 'node:os'; -import { join, resolve } from 'node:path'; +import { join, resolve, sep } from 'node:path'; import { dshHome, migrateLegacyFile } from "./store.js"; const MAX_GROUP_NAME = 40; const MAX_GROUPS_PER_SOURCE = 40; @@ -290,6 +290,50 @@ export class SkillGroupManager { }; } /** Create one category inside a source. */ + /** + * Import one skill from another tool's source into the DSH skills root + * (the card's 「从其他软件导入」). The source directory itself is never + * modified: the source side is vetted here (real directory, no symlink, + * stays inside its own root, carries a real SKILL.md) and the copy is + * delegated to the admin, which owns the target-side rules. + */ + async importSkill(sourceInput, folderInput) { + const source = this.source(sourceInput); + if (source.writable) { + throw new SkillGroupError('import_from_self', 'DSH 来源就是导入目标,不需要导入。'); + } + const folder = typeof folderInput === 'string' ? folderInput : ''; + if (folder === '' || folder.length > 128 || folder.startsWith('.') || folder.includes('/') || folder.includes('\\') || folder.includes('\0')) { + throw new SkillGroupError('invalid_name', `技能目录名无效:${String(folderInput)}`); + } + const dir = join(source.root, folder); + let stat; + try { + stat = lstatSync(dir); + } + catch { + throw new SkillGroupError('not_found', `来源里没有这个技能:${folder}`); + } + if (!stat.isDirectory() || stat.isSymbolicLink()) { + throw new SkillGroupError('invalid_skill', `来源技能不是普通目录:${folder}`); + } + const rootReal = realpathSync(source.root); + const dirReal = realpathSync(dir); + if (!dirReal.startsWith(`${rootReal}${sep}`)) { + throw new SkillGroupError('invalid_skill', `来源技能越出来源目录:${folder}`); + } + let marker; + try { + marker = lstatSync(join(dir, 'SKILL.md')); + } + catch { + throw new SkillGroupError('invalid_skill', `来源技能缺少 SKILL.md:${folder}`); + } + if (!marker.isFile() || marker.isSymbolicLink()) { + throw new SkillGroupError('invalid_skill', `来源技能的 SKILL.md 不是普通文件:${folder}`); + } + return this.deps.admin.importFolder(dirReal, folder); + } async createGroup(sourceInput, nameInput) { const source = this.source(sourceInput); const name = cleanGroupName(nameInput);