From 6f19240c38bf8012a57c92634b971731465ed71f Mon Sep 17 00:00:00 2001 From: QQ Date: Thu, 12 Nov 2026 09:23:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/skill-groups.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/skill-groups.ts | 47 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/skill-groups.ts b/src/skill-groups.ts index b6f43da..cfee418 100644 --- a/src/skill-groups.ts +++ b/src/skill-groups.ts @@ -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.ts' import type { SkillAdmin, SkillEntry } from './skill-admin.ts' @@ -363,6 +363,49 @@ 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: unknown, folderInput: unknown): Promise<{ folder: string }> { + 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: unknown, nameInput: unknown): Promise<{ groupId: string; name: string }> { const source = this.source(sourceInput) const name = cleanGroupName(nameInput)