@suifei/dsh-ai-team 0.4.123 → 0.4.125
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/CHANGELOG.md +43 -0
- package/README.md +20 -6
- package/client.js +292 -12
- package/index.js +96 -2
- package/lib/domain/catalog.js +303 -0
- package/lib/host/default-roster.js +124 -143
- package/lib/host/project-runtime.js +2 -2
- package/lib/host/service.js +17 -0
- package/lib/ledger/fold.js +6 -0
- package/lib/runtime/core-skill.js +16 -22
- package/lib/runtime/skill-loader.js +4 -2
- package/package.json +5 -2
- package/talents/algo-reimplementer/skills/reimplementation/SKILL.md +14 -0
- package/talents/algo-reimplementer/talent.yml +9 -0
- package/talents/analyst/skills/analysis-synthesis/SKILL.md +14 -0
- package/talents/analyst/talent.yml +9 -0
- package/talents/architect/skills/architecture-design/SKILL.md +14 -0
- package/talents/architect/talent.yml +9 -0
- package/talents/binary-analyst/skills/static-analysis/SKILL.md +14 -0
- package/talents/binary-analyst/talent.yml +9 -0
- package/talents/biz-analyst/skills/business-analysis/SKILL.md +14 -0
- package/talents/biz-analyst/talent.yml +9 -0
- package/talents/code-reviewer/skills/code-review/SKILL.md +14 -0
- package/talents/code-reviewer/talent.yml +9 -0
- package/talents/content-author/skills/question-authoring/SKILL.md +14 -0
- package/talents/content-author/talent.yml +9 -0
- package/talents/content-strategist/talent.yml +8 -0
- package/talents/copywriter/skills/copywriting/SKILL.md +14 -0
- package/talents/copywriter/talent.yml +9 -0
- package/talents/creative-director/talent.yml +8 -0
- package/talents/curriculum-designer/skills/curriculum-design/SKILL.md +14 -0
- package/talents/curriculum-designer/talent.yml +9 -0
- package/talents/data-analyst/skills/statistics/SKILL.md +14 -0
- package/talents/data-analyst/talent.yml +9 -0
- package/talents/deobfuscator/skills/deobfuscation/SKILL.md +14 -0
- package/talents/deobfuscator/talent.yml +9 -0
- package/talents/diagrammer/skills/diagramming/SKILL.md +14 -0
- package/talents/diagrammer/talent.yml +9 -0
- package/talents/dynamic-debugger/skills/dynamic-debugging/SKILL.md +14 -0
- package/talents/dynamic-debugger/talent.yml +9 -0
- package/talents/editor/skills/copyediting/SKILL.md +14 -0
- package/talents/editor/talent.yml +9 -0
- package/talents/editorial-lead/talent.yml +8 -0
- package/talents/education-lead/talent.yml +8 -0
- package/talents/engineer/skills/engineering/SKILL.md +15 -0
- package/talents/engineer/talent.yml +9 -0
- package/talents/executor/skills/delivery-checklist/SKILL.md +15 -0
- package/talents/executor/talent.yml +9 -0
- package/talents/experiment-designer/skills/experiment-design/SKILL.md +14 -0
- package/talents/experiment-designer/talent.yml +9 -0
- package/talents/fact-checker/skills/fact-check/SKILL.md +14 -0
- package/talents/fact-checker/talent.yml +9 -0
- package/talents/game-artist/skills/game-art/SKILL.md +14 -0
- package/talents/game-artist/talent.yml +9 -0
- package/talents/game-designer/skills/game-design/SKILL.md +14 -0
- package/talents/game-designer/talent.yml +9 -0
- package/talents/game-director/talent.yml +8 -0
- package/talents/game-tester/skills/playtesting/SKILL.md +14 -0
- package/talents/game-tester/talent.yml +9 -0
- package/talents/gameplay-programmer/skills/gameplay-programming/SKILL.md +14 -0
- package/talents/gameplay-programmer/talent.yml +9 -0
- package/talents/mobile-re/skills/mobile-re/SKILL.md +14 -0
- package/talents/mobile-re/talent.yml +9 -0
- package/talents/paper-writer/skills/paper-writing/SKILL.md +14 -0
- package/talents/paper-writer/talent.yml +9 -0
- package/talents/peer-reviewer/skills/peer-review/SKILL.md +14 -0
- package/talents/peer-reviewer/talent.yml +9 -0
- package/talents/pmo-lead/talent.yml +8 -0
- package/talents/principal-investigator/talent.yml +8 -0
- package/talents/qa-acceptor/skills/acceptance-testing/SKILL.md +14 -0
- package/talents/qa-acceptor/talent.yml +9 -0
- package/talents/re-lead/talent.yml +8 -0
- package/talents/re-verifier/skills/behavior-diff/SKILL.md +14 -0
- package/talents/re-verifier/talent.yml +9 -0
- package/talents/research-lead/talent.yml +8 -0
- package/talents/researcher/skills/source-research/SKILL.md +14 -0
- package/talents/researcher/talent.yml +9 -0
- package/talents/scriptwriter/skills/scriptwriting/SKILL.md +14 -0
- package/talents/scriptwriter/talent.yml +9 -0
- package/talents/security-auditor/skills/security-audit/SKILL.md +14 -0
- package/talents/security-auditor/talent.yml +9 -0
- package/talents/seo-specialist/skills/seo/SKILL.md +14 -0
- package/talents/seo-specialist/talent.yml +9 -0
- package/talents/storyboard-artist/skills/storyboarding/SKILL.md +14 -0
- package/talents/storyboard-artist/talent.yml +9 -0
- package/talents/tech-writer/skills/technical-writing/SKILL.md +14 -0
- package/talents/tech-writer/talent.yml +9 -0
- package/talents/ux-designer/skills/ux-research/SKILL.md +14 -0
- package/talents/ux-designer/talent.yml +9 -0
- package/talents/video-editor/skills/video-editing/SKILL.md +14 -0
- package/talents/video-editor/talent.yml +9 -0
- package/teams/content/team.yml +12 -0
- package/teams/default/team.yml +35 -0
- package/teams/docs/team.yml +12 -0
- package/teams/education/team.yml +13 -0
- package/teams/game/team.yml +12 -0
- package/teams/research/team.yml +11 -0
- package/teams/reverse/team.yml +17 -0
- package/teams/science/team.yml +12 -0
- package/teams/video/team.yml +12 -0
- /package/{skills → teams/default/skills}/ai-team-core/SKILL.md +0 -0
- /package/{skills → teams/default/skills}/ai-team-executor/SKILL.md +0 -0
- /package/{skills → teams/default/skills}/ai-team-qa/SKILL.md +0 -0
package/index.js
CHANGED
|
@@ -71,6 +71,7 @@ import { isInformationalThreadMention } from './lib/domain/inbox.js';
|
|
|
71
71
|
import { messageContent, normalizeAttachments } from './lib/domain/rich-message.js';
|
|
72
72
|
import { readAttachment, uploadAttachment } from './lib/host/attachment-routes.js';
|
|
73
73
|
import { createDiagLogger, defaultDiagDir, installDiagSink } from './lib/diaglog.js';
|
|
74
|
+
import { DEFAULT_TEAM, catalogRoots, deleteTalent, installSkill, listInstallableSkills, listTalents, loadCatalog, readSkillText, resolveTeam, talentIdFrom, userCatalogDir, writeTalent } from './lib/domain/catalog.js';
|
|
74
75
|
|
|
75
76
|
export const name = 'ai-team';
|
|
76
77
|
|
|
@@ -122,8 +123,28 @@ export const Config = z.object({
|
|
|
122
123
|
// 有界自主:Human 每次操作后,PMO 最多自主推进(无 active Task 时开会、自拟下一批卡)几轮;
|
|
123
124
|
// 用尽即暂停并在主流告知,等 Human 续。0 = 从不自主推进。Issue #1:一句「在吗」跑 7 小时。
|
|
124
125
|
pmoMaxRounds: z.number().step(1).min(0).max(100).default(3),
|
|
126
|
+
// 新项目的初始团队:内置 teams/<团队>/,或 ~/.dsh/ai-team/catalog/user/teams/<团队>/(同名覆盖内置)。
|
|
127
|
+
teamPreset: z.string().default('default'),
|
|
125
128
|
});
|
|
126
129
|
|
|
130
|
+
/** 人才市场目录(用户自建 + 插件内置;每次现读,改了文件不用重启)。见 lib/domain/catalog.js。 */
|
|
131
|
+
function talentCatalog(diag) {
|
|
132
|
+
return loadCatalog({ roots: catalogRoots({ dshHome: dshHome() }), diag });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 成员的技能先在它的人才目录里找(talents/<人才>/skills/),再按调用方给的根找。 */
|
|
136
|
+
function memberCatalogDirs(member, diag) {
|
|
137
|
+
const talent = typeof member?.talent === 'string' ? talentCatalog(diag).talents.get(member.talent) : undefined;
|
|
138
|
+
return talent === undefined ? [] : [talent.dir];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 新项目用哪个团队。 */
|
|
142
|
+
function teamPresetFor(config, diag) {
|
|
143
|
+
const preset = resolveTeam(talentCatalog(diag), config?.teamPreset || DEFAULT_TEAM, { diag });
|
|
144
|
+
diag?.append?.({ ev: 'catalog.team', name: preset.id, source: preset.source, dir: preset.dir, members: preset.team.members.length });
|
|
145
|
+
return preset;
|
|
146
|
+
}
|
|
147
|
+
|
|
127
148
|
/**
|
|
128
149
|
* 惰性 shell 服务代理:插件注入时机可能早于 shell 服务就绪,注册与 tick 都
|
|
129
150
|
* 必须在调用时现取,避免「boot 时未就绪 → 整条技能链静默跳过」。
|
|
@@ -238,6 +259,7 @@ async function boot(diag, config = {}) {
|
|
|
238
259
|
workspacePath: project.workspacePath ?? '',
|
|
239
260
|
diag,
|
|
240
261
|
pmoMaxRounds: config.pmoMaxRounds,
|
|
262
|
+
teamPreset: teamPresetFor(config, diag),
|
|
241
263
|
});
|
|
242
264
|
runtimes.set(project.id, runtime);
|
|
243
265
|
}
|
|
@@ -2112,14 +2134,16 @@ export function apply(ctx, config = {}) {
|
|
|
2112
2134
|
// #65:激活时若带着 Human 消息的图片/文件,一并放进首条消息。
|
|
2113
2135
|
prompt: messageContent(`${memberIdentity(activationMember)}\n\n${typeof body.payload?.prompt === 'string'
|
|
2114
2136
|
? body.payload.prompt
|
|
2115
|
-
: '你被加入 AI Team。先读取项目简报与收件箱,再处理分配给你的任务。'}${await formatSkillCatalog(skillsService, { cwd: effectiveDir })}${await loadSkillContext(activationMember, [join(effectiveDir, '.dsh'), dshHome(), projectRuntime.dir], {
|
|
2137
|
+
: '你被加入 AI Team。先读取项目简报与收件箱,再处理分配给你的任务。'}${await formatSkillCatalog(skillsService, { cwd: effectiveDir })}${await loadSkillContext(activationMember, [...memberCatalogDirs(activationMember, diag), join(effectiveDir, '.dsh'), dshHome(), projectRuntime.dir], {
|
|
2116
2138
|
vars: {
|
|
2117
2139
|
GOAL: goalForProject(projectRuntime),
|
|
2118
2140
|
PROJECT: projectRuntime.service.state.projects?.[projectRuntime.id]?.name ?? '',
|
|
2119
2141
|
MEMBER: activationMember.name ?? '',
|
|
2120
2142
|
WORKSPACE: effectiveDir,
|
|
2121
2143
|
},
|
|
2122
|
-
//
|
|
2144
|
+
// 成员来自哪个团队就用哪个团队的岗位规程与技能(老成员没有记录 → 默认团队)。
|
|
2145
|
+
preset: resolveTeam(talentCatalog(diag), activationMember.team ?? DEFAULT_TEAM, { diag }),
|
|
2146
|
+
// 按身份装配岗位规程:项目主理人拿审查规程,验收拿复跑规程,其余拿执行规程。
|
|
2123
2147
|
ownerId: projectRuntime.service.state.projects?.[projectRuntime.id]?.ownerMemberId
|
|
2124
2148
|
?? projectRuntime.service.state.projects?.[projectRuntime.id]?.ownerId,
|
|
2125
2149
|
})}`, body.payload?.attachments),
|
|
@@ -2272,6 +2296,7 @@ export function apply(ctx, config = {}) {
|
|
|
2272
2296
|
workspacePath,
|
|
2273
2297
|
diag,
|
|
2274
2298
|
pmoMaxRounds: config.pmoMaxRounds,
|
|
2299
|
+
teamPreset: teamPresetFor(config, diag),
|
|
2275
2300
|
});
|
|
2276
2301
|
booted.runtimes.set(nextProjectId, next);
|
|
2277
2302
|
attachRuntime(next);
|
|
@@ -2337,6 +2362,75 @@ export function apply(ctx, config = {}) {
|
|
|
2337
2362
|
get: await skillsService.get('distilly', {}).then((skill) => skill?.name ?? null).catch(() => null),
|
|
2338
2363
|
});
|
|
2339
2364
|
}
|
|
2365
|
+
// 人才市场:「添加成员」弹窗里可招聘的人才(用户自建优先于内置)。
|
|
2366
|
+
if (req.method === 'GET' && url.pathname === '/ai-team/api/talents') {
|
|
2367
|
+
return send(res, { talents: listTalents(talentCatalog(diag)) });
|
|
2368
|
+
}
|
|
2369
|
+
// 可安装的技能清单(每个人才自带的技能),给「从已有装」挑。
|
|
2370
|
+
if (req.method === 'GET' && url.pathname === '/ai-team/api/skills-install') {
|
|
2371
|
+
return send(res, { skills: listInstallableSkills(talentCatalog(diag)) });
|
|
2372
|
+
}
|
|
2373
|
+
// 某人才自带技能的正文(预览 / fork 前读)。
|
|
2374
|
+
if (req.method === 'GET' && url.pathname === '/ai-team/api/skills-install/content') {
|
|
2375
|
+
const cat = talentCatalog(diag);
|
|
2376
|
+
const t = cat.talents.get(url.searchParams.get('talent') ?? '');
|
|
2377
|
+
if (t === undefined) return send(res, { error: { code: 'NOT_FOUND', message: 'talent not found' } }, 404);
|
|
2378
|
+
try {
|
|
2379
|
+
return send(res, { markdown: readSkillText(t.dir, url.searchParams.get('name') ?? '') });
|
|
2380
|
+
} catch (error) {
|
|
2381
|
+
return send(res, { error: { code: 'NOT_FOUND', message: error.message } }, 404);
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
// 新建 / 覆盖一个用户自定义人才(只写用户根;内置只读)。
|
|
2385
|
+
if (req.method === 'POST' && url.pathname === '/ai-team/api/talents') {
|
|
2386
|
+
const body = await readJson(req);
|
|
2387
|
+
// 显式 id 优先,其次按名字派生;中文名派生为空时生成兜底 id(纯中文名也能建)。
|
|
2388
|
+
let id = talentIdFrom(body?.id) || talentIdFrom(body?.name);
|
|
2389
|
+
if (id === '' && typeof body?.name === 'string' && body.name.trim() !== '') id = 'talent-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 5);
|
|
2390
|
+
if (id === '') return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: '人才需要一个名字(用于生成 id)' } }, 400);
|
|
2391
|
+
try {
|
|
2392
|
+
const saved = writeTalent(userCatalogDir(dshHome()), id, body ?? {});
|
|
2393
|
+
diag?.append({ ev: 'talent.write', id, source: 'user' });
|
|
2394
|
+
return send(res, { ok: true, talent: { id: saved.id, source: saved.source, version: saved.version, name: saved.name, role: saved.role, duty: saved.duty, category: saved.category, tags: saved.tags, description: saved.description, personality: saved.personality, skills: saved.skills, model: saved.model } });
|
|
2395
|
+
} catch (error) {
|
|
2396
|
+
return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: error.message } }, 400);
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
// 删一个用户自定义人才(只删用户根的)。
|
|
2400
|
+
if (req.method === 'POST' && url.pathname === '/ai-team/api/talents/delete') {
|
|
2401
|
+
const body = await readJson(req);
|
|
2402
|
+
const cat = talentCatalog(diag);
|
|
2403
|
+
const t = cat.talents.get(talentIdFrom(body?.id ?? ''));
|
|
2404
|
+
if (t === undefined) return send(res, { error: { code: 'NOT_FOUND', message: 'talent not found' } }, 404);
|
|
2405
|
+
if (t.source !== 'user') return send(res, { error: { code: 'E_READONLY', message: '内置人才只读,不能删除;它只会被同名的自定义人才遮盖' } }, 409);
|
|
2406
|
+
deleteTalent(userCatalogDir(dshHome()), t.id);
|
|
2407
|
+
diag?.append({ ev: 'talent.delete', id: t.id });
|
|
2408
|
+
return send(res, { ok: true });
|
|
2409
|
+
}
|
|
2410
|
+
// 往某用户人才装一份技能:自写(markdown)或从现成人才 fork(fromTalent + fromSkill)。
|
|
2411
|
+
const talentSkillRoute = url.pathname.match(/^\/ai-team\/api\/talents\/([^/]+)\/skills$/);
|
|
2412
|
+
if (req.method === 'POST' && talentSkillRoute !== null) {
|
|
2413
|
+
const body = await readJson(req);
|
|
2414
|
+
const cat = talentCatalog(diag);
|
|
2415
|
+
const t = cat.talents.get(decodeURIComponent(talentSkillRoute[1]));
|
|
2416
|
+
if (t === undefined) return send(res, { error: { code: 'NOT_FOUND', message: 'talent not found' } }, 404);
|
|
2417
|
+
if (t.source !== 'user') return send(res, { error: { code: 'E_READONLY', message: '内置人才只读;先把它存为自己的人才再装技能' } }, 409);
|
|
2418
|
+
let markdown = typeof body?.markdown === 'string' ? body.markdown : null;
|
|
2419
|
+
if (markdown === null && typeof body?.fromTalent === 'string' && typeof body?.fromSkill === 'string') {
|
|
2420
|
+
const src = cat.talents.get(body.fromTalent);
|
|
2421
|
+
if (src === undefined) return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: '来源人才不存在' } }, 400);
|
|
2422
|
+
try { markdown = readSkillText(src.dir, body.fromSkill); } catch (error) { return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: error.message } }, 400); }
|
|
2423
|
+
}
|
|
2424
|
+
if (markdown === null) return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: '需要 markdown 正文,或 fromTalent + fromSkill' } }, 400);
|
|
2425
|
+
try {
|
|
2426
|
+
const name = installSkill(t.dir, body?.name, markdown);
|
|
2427
|
+
const saved = writeTalent(userCatalogDir(dshHome()), t.id, { ...t, skills: [...new Set([...t.skills, name])] });
|
|
2428
|
+
diag?.append({ ev: 'talent.skill.install', id: t.id, skill: name, from: body?.fromTalent ?? null });
|
|
2429
|
+
return send(res, { ok: true, skill: name, talent: { id: saved.id, source: saved.source, skills: saved.skills } });
|
|
2430
|
+
} catch (error) {
|
|
2431
|
+
return send(res, { error: { code: 'E_INVALID_ARGUMENT', message: error.message } }, 400);
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2340
2434
|
if (req.method === 'GET' && url.pathname === '/ai-team/api/models') {
|
|
2341
2435
|
const catalog = await buildModelCatalog(serviceOf(ctx, 'llm'), serviceOf(ctx, 'agentDefaultModel'));
|
|
2342
2436
|
return send(res, catalog);
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 人才市场目录:人才 → 团队 → 项目成员。
|
|
3
|
+
*
|
|
4
|
+
* talents/<人才>/talent.yml 一个能干特定事的智能体:职责、性格、技能、推荐模型
|
|
5
|
+
* talents/<人才>/skills/<技能>/SKILL.md 人才自带的技能
|
|
6
|
+
* teams/<团队>/team.yml 从人才里挑人组队(只引用人才),选团队的岗位规程
|
|
7
|
+
* teams/<团队>/skills/<规程>/SKILL.md 团队的工作方式(主理人 / 验收 / 执行三份岗位规程)
|
|
8
|
+
*
|
|
9
|
+
* 同一格式有多处来源,按顺序覆盖(同名先到先得):用户自建 `~/.dsh/ai-team/catalog/user/`
|
|
10
|
+
* → (第 3 步)官方在线库 → 插件内置。招进项目的成员是账本里的实例,记下来源人才与版本;
|
|
11
|
+
* 人才文件之后再变,不会悄悄改动已招成员的性格与职责。
|
|
12
|
+
* 文件写坏不阻断:记一行 `.fail` 诊断,跳过它(更低优先级的同名条目顶上)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
18
|
+
import { parse, stringify } from 'yaml';
|
|
19
|
+
|
|
20
|
+
/** 插件内置目录(包根,下有 talents/ 与 teams/)。 */
|
|
21
|
+
export const BUNDLED_CATALOG_DIR = fileURLToPath(new URL('../../', import.meta.url));
|
|
22
|
+
export const DEFAULT_TEAM = 'default';
|
|
23
|
+
/** 三种职责,各对应一份岗位规程。 */
|
|
24
|
+
export const DUTIES = Object.freeze(['lead', 'qa', 'executor']);
|
|
25
|
+
export const DEFAULT_PROCEDURES = Object.freeze({ lead: 'ai-team-core', qa: 'ai-team-qa', executor: 'ai-team-executor' });
|
|
26
|
+
|
|
27
|
+
const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
28
|
+
const MEMBER_ID = /^mem_[a-z0-9_]{1,60}$/;
|
|
29
|
+
const SKILL_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
30
|
+
const VERSION = /^\d+\.\d+\.\d+$/;
|
|
31
|
+
const text = (value, max) => (typeof value === 'string' ? value.trim().slice(0, max) : '');
|
|
32
|
+
|
|
33
|
+
function readYaml(source, label) {
|
|
34
|
+
let doc;
|
|
35
|
+
try {
|
|
36
|
+
doc = parse(source);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(`${label}: YAML 解析失败:${error.message}`);
|
|
39
|
+
}
|
|
40
|
+
if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) throw new Error(`${label}: 顶层必须是对象`);
|
|
41
|
+
return doc;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function skillName(value, at) {
|
|
45
|
+
const name = text(value, 64);
|
|
46
|
+
if (!SKILL_NAME.test(name) || name.includes('..')) throw new Error(`${at} 不是合法的技能名:${String(value)}`);
|
|
47
|
+
return name;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function skillList(value, at) {
|
|
51
|
+
if (value === undefined || value === null) return [];
|
|
52
|
+
if (!Array.isArray(value)) throw new Error(`${at} 必须是列表`);
|
|
53
|
+
return [...new Set(value.map((skill, i) => skillName(skill, `${at}[${i}]`)))];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function duty(value, at) {
|
|
57
|
+
if (value === undefined || value === null) return null;
|
|
58
|
+
if (!DUTIES.includes(value)) throw new Error(`${at} 只认 ${DUTIES.join(' / ')}`);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* talent.yml → 规范化人才;不合法抛错(说明哪一处)。
|
|
64
|
+
* @param {string} source
|
|
65
|
+
* @param {string} [label]
|
|
66
|
+
*/
|
|
67
|
+
export function parseTalent(source, label = 'talent') {
|
|
68
|
+
const doc = readYaml(source, label);
|
|
69
|
+
const name = text(doc.name, 40);
|
|
70
|
+
const role = text(doc.role, 40);
|
|
71
|
+
if (name === '') throw new Error(`${label}: name 不能为空`);
|
|
72
|
+
if (role === '') throw new Error(`${label}: role 不能为空`);
|
|
73
|
+
const version = doc.version === undefined ? '1.0.0' : text(String(doc.version), 32);
|
|
74
|
+
if (!VERSION.test(version)) throw new Error(`${label}: version 必须形如 1.2.3`);
|
|
75
|
+
if (doc.tags !== undefined && !Array.isArray(doc.tags)) throw new Error(`${label}: tags 必须是列表`);
|
|
76
|
+
let model = null;
|
|
77
|
+
if (doc.model !== undefined && doc.model !== null) {
|
|
78
|
+
if (typeof doc.model !== 'object' || Array.isArray(doc.model)) throw new Error(`${label}: model 必须是对象(provider / model / reasoningEffort)`);
|
|
79
|
+
model = {
|
|
80
|
+
provider: text(doc.model.provider, 80),
|
|
81
|
+
model: text(doc.model.model, 160),
|
|
82
|
+
reasoningEffort: text(doc.model.reasoningEffort, 20),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
version,
|
|
87
|
+
name,
|
|
88
|
+
role,
|
|
89
|
+
duty: duty(doc.duty, `${label}: duty`),
|
|
90
|
+
category: text(doc.category, 20),
|
|
91
|
+
tags: (doc.tags ?? []).map((tag) => text(String(tag), 20)).filter(Boolean).slice(0, 12),
|
|
92
|
+
description: text(doc.description, 400),
|
|
93
|
+
personality: text(doc.personality, 2000),
|
|
94
|
+
skills: skillList(doc.skills, `${label}: skills`),
|
|
95
|
+
model,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* team.yml → 规范化团队(成员仍是对人才的引用,见 resolveTeam);不合法抛错。
|
|
101
|
+
* @param {string} source
|
|
102
|
+
* @param {string} [label]
|
|
103
|
+
*/
|
|
104
|
+
export function parseTeam(source, label = 'team') {
|
|
105
|
+
const doc = readYaml(source, label);
|
|
106
|
+
if (!Array.isArray(doc.members) || doc.members.length === 0) throw new Error(`${label}: members 至少要有一位成员`);
|
|
107
|
+
const procedures = { ...DEFAULT_PROCEDURES };
|
|
108
|
+
if (doc.procedures !== undefined && doc.procedures !== null) {
|
|
109
|
+
if (typeof doc.procedures !== 'object' || Array.isArray(doc.procedures)) throw new Error(`${label}: procedures 必须是对象`);
|
|
110
|
+
for (const [kind, name] of Object.entries(doc.procedures)) {
|
|
111
|
+
if (!DUTIES.includes(kind)) throw new Error(`${label}: procedures.${kind} 不是职责(只认 ${DUTIES.join(' / ')})`);
|
|
112
|
+
procedures[kind] = skillName(name, `${label}: procedures.${kind}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const ids = new Set();
|
|
116
|
+
const members = doc.members.map((raw, index) => {
|
|
117
|
+
const at = `${label}: members[${index}]`;
|
|
118
|
+
if (raw === null || typeof raw !== 'object') throw new Error(`${at} 必须是对象`);
|
|
119
|
+
const talent = text(raw.talent, 64);
|
|
120
|
+
if (!SLUG.test(talent)) throw new Error(`${at}.talent 必须是人才 id(小写字母、数字、连字符)`);
|
|
121
|
+
const id = raw.id === undefined ? `mem_${talent.replace(/-/g, '_')}` : text(raw.id, 64);
|
|
122
|
+
if (!MEMBER_ID.test(id)) throw new Error(`${at}.id 必须形如 mem_xxx(小写字母、数字、下划线)`);
|
|
123
|
+
if (ids.has(id)) throw new Error(`${at}.id 重复:${id}(同一人才招两位时给每位写 id)`);
|
|
124
|
+
ids.add(id);
|
|
125
|
+
return {
|
|
126
|
+
id,
|
|
127
|
+
talent,
|
|
128
|
+
name: text(raw.name, 40),
|
|
129
|
+
role: text(raw.role, 40),
|
|
130
|
+
duty: duty(raw.duty, `${at}.duty`),
|
|
131
|
+
personality: text(raw.personality, 2000),
|
|
132
|
+
skills: skillList(raw.skills, `${at}.skills`),
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
const lead = doc.lead === undefined || doc.lead === null ? members[0].id : text(doc.lead, 64);
|
|
136
|
+
if (!ids.has(lead)) throw new Error(`${label}: lead「${lead}」不在 members 的 id 里`);
|
|
137
|
+
return { name: text(doc.name, 60), description: text(doc.description, 400), lead, procedures, members };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 用户自建目录(可写),自定义人才与自建团队都落在这里。 */
|
|
141
|
+
export function userCatalogDir(dshHome) {
|
|
142
|
+
if (typeof dshHome !== 'string' || dshHome === '') throw new Error('userCatalogDir: 需要 dshHome');
|
|
143
|
+
return join(dshHome, 'ai-team', 'catalog', 'user');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 用户自建目录 + 插件内置目录(第 3 步在两者之间插入官方在线库)。 */
|
|
147
|
+
export function catalogRoots({ dshHome, bundledDir = BUNDLED_CATALOG_DIR } = {}) {
|
|
148
|
+
return [
|
|
149
|
+
...(typeof dshHome === 'string' && dshHome !== '' ? [{ source: 'user', dir: userCatalogDir(dshHome) }] : []),
|
|
150
|
+
{ source: 'bundled', dir: bundledDir },
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function scan(roots, kind, file, parser, diag) {
|
|
155
|
+
const found = new Map();
|
|
156
|
+
for (const { source, dir } of roots) {
|
|
157
|
+
const base = join(dir, kind);
|
|
158
|
+
if (!existsSync(base)) continue;
|
|
159
|
+
for (const id of readdirSync(base)) {
|
|
160
|
+
if (!SLUG.test(id) || found.has(id)) continue;
|
|
161
|
+
const path = join(base, id, file);
|
|
162
|
+
if (!existsSync(path)) continue;
|
|
163
|
+
try {
|
|
164
|
+
found.set(id, { id, source, dir: join(base, id), ...parser(readFileSync(path, 'utf8'), path) });
|
|
165
|
+
} catch (error) {
|
|
166
|
+
diag?.append?.({ ev: 'catalog.load.fail', kind, id, source, message: error.message });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return found;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 读出全部人才与团队(每次现读:改了文件不用重启)。
|
|
175
|
+
* @param {{ roots?: Array<{source: string, dir: string}>, diag?: { append?: (row: object) => void } }} [options]
|
|
176
|
+
*/
|
|
177
|
+
export function loadCatalog({ roots = catalogRoots(), diag } = {}) {
|
|
178
|
+
return {
|
|
179
|
+
talents: scan(roots, 'talents', 'talent.yml', parseTalent, diag),
|
|
180
|
+
teams: scan(roots, 'teams', 'team.yml', parseTeam, diag),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** 人才市场列表(给「添加成员」弹窗):按分类、名字排序。 */
|
|
185
|
+
export function listTalents(catalog) {
|
|
186
|
+
return [...catalog.talents.values()]
|
|
187
|
+
.map(({ id, source, version, name, role, duty: d, category, tags, description, personality, skills, model }) => ({ id, source, version, name, role, duty: d, category, tags, description, personality, skills, model }))
|
|
188
|
+
.sort((a, b) => a.category.localeCompare(b.category, 'zh') || a.name.localeCompare(b.name, 'zh'));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 团队 → 可直接建成员的名单:每位成员按引用的人才补齐名字、岗位、职责、性格与技能
|
|
193
|
+
* (团队里写了的覆盖人才的)。团队缺失、写坏或引用了不存在的人才时退回默认团队并记 `.fail`。
|
|
194
|
+
* @returns {{ id: string, source: string, dir: string, team: { name: string, description: string, lead: string, procedures: Record<string, string>, members: object[] } }}
|
|
195
|
+
*/
|
|
196
|
+
export function resolveTeam(catalog, name = DEFAULT_TEAM, { diag } = {}) {
|
|
197
|
+
const wanted = typeof name === 'string' && SLUG.test(name) ? name : DEFAULT_TEAM;
|
|
198
|
+
if (wanted !== name) diag?.append?.({ ev: 'catalog.team.fail', name, reason: 'invalid team name' });
|
|
199
|
+
const entry = catalog.teams.get(wanted);
|
|
200
|
+
try {
|
|
201
|
+
if (entry === undefined) throw new Error(`team ${wanted} not found`);
|
|
202
|
+
const members = entry.members.map((member) => {
|
|
203
|
+
const talent = catalog.talents.get(member.talent);
|
|
204
|
+
if (talent === undefined) throw new Error(`team ${wanted}: talent ${member.talent} not found`);
|
|
205
|
+
return {
|
|
206
|
+
id: member.id,
|
|
207
|
+
name: member.name || talent.name,
|
|
208
|
+
role: member.role || talent.role,
|
|
209
|
+
duty: member.duty ?? talent.duty,
|
|
210
|
+
personality: member.personality || talent.personality,
|
|
211
|
+
skills: [...new Set([...talent.skills, ...member.skills])],
|
|
212
|
+
talent: { id: talent.id, version: talent.version, source: talent.source },
|
|
213
|
+
model: talent.model,
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
return { id: wanted, source: entry.source, dir: entry.dir, team: { name: entry.name || wanted, description: entry.description, lead: entry.lead, procedures: entry.procedures, members } };
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (wanted === DEFAULT_TEAM) throw error; // 内置默认团队有测试守着;坏了说明安装包坏了。
|
|
219
|
+
diag?.append?.({ ev: 'catalog.team.fail', name: wanted, message: error.message });
|
|
220
|
+
return resolveTeam(catalog, DEFAULT_TEAM, { diag });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 某职责的岗位规程正文(含 front-matter,调用方负责剥掉):先找团队自己的 skills/,
|
|
226
|
+
* 团队没带就退回内置默认团队的同职责规程。
|
|
227
|
+
*/
|
|
228
|
+
export function procedureText(preset, kind, { bundledDir = BUNDLED_CATALOG_DIR } = {}) {
|
|
229
|
+
const d = DUTIES.includes(kind) ? kind : 'executor';
|
|
230
|
+
const candidates = [];
|
|
231
|
+
if (preset?.dir) candidates.push(join(preset.dir, 'skills', preset.team?.procedures?.[d] ?? DEFAULT_PROCEDURES[d], 'SKILL.md'));
|
|
232
|
+
candidates.push(join(bundledDir, 'teams', DEFAULT_TEAM, 'skills', DEFAULT_PROCEDURES[d], 'SKILL.md'));
|
|
233
|
+
for (const file of candidates) if (existsSync(file)) return readFileSync(file, 'utf8');
|
|
234
|
+
throw new Error(`bundled procedure missing: ${candidates.at(-1)}`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// —— 写层(第 2 步 2a:安装 skill + 新建自定义人才)——
|
|
238
|
+
// 只往用户根写,内置永不触碰;同名用户条目遮盖内置即「覆盖」。校验复用 parseTalent:
|
|
239
|
+
// 先 stringify 再 parse,验不过就抛、绝不落盘,保证盘上的文件一定能被 loadCatalog 读回。
|
|
240
|
+
|
|
241
|
+
/** 规范人才 id:小写字母、数字、连字符,≤64 位(与扫描用的 SLUG 同一把尺)。 */
|
|
242
|
+
export function talentIdFrom(value) {
|
|
243
|
+
const id = text(value, 64).toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '').replace(/-{2,}/g, '-');
|
|
244
|
+
return SLUG.test(id) ? id : '';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function talentDoc(input) {
|
|
248
|
+
const doc = { version: text(String(input?.version ?? '1.0.0'), 32) || '1.0.0', name: text(input?.name, 40), role: text(input?.role, 40) };
|
|
249
|
+
if (input?.duty) doc.duty = input.duty;
|
|
250
|
+
if (text(input?.category, 20)) doc.category = text(input.category, 20);
|
|
251
|
+
const tags = Array.isArray(input?.tags) ? input.tags.map((t) => text(String(t), 20)).filter(Boolean).slice(0, 12) : [];
|
|
252
|
+
if (tags.length) doc.tags = tags;
|
|
253
|
+
if (text(input?.description, 400)) doc.description = text(input.description, 400);
|
|
254
|
+
if (text(input?.personality, 2000)) doc.personality = text(input.personality, 2000);
|
|
255
|
+
const skills = skillList(input?.skills, 'skills');
|
|
256
|
+
if (skills.length) doc.skills = skills;
|
|
257
|
+
const m = input?.model;
|
|
258
|
+
if (m && (text(m.provider, 80) || text(m.model, 160))) doc.model = { provider: text(m.provider, 80), model: text(m.model, 160), reasoningEffort: text(m.reasoningEffort, 20) };
|
|
259
|
+
return doc;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 写一个用户自定义人才:`<userRoot>/talents/<id>/talent.yml`。校验不过时抛错、不落盘。
|
|
264
|
+
* @returns {{ id: string, source: 'user', dir: string } & ReturnType<typeof parseTalent>}
|
|
265
|
+
*/
|
|
266
|
+
export function writeTalent(userRoot, id, input) {
|
|
267
|
+
if (typeof userRoot !== 'string' || userRoot === '') throw new Error('writeTalent: 需要 userRoot');
|
|
268
|
+
if (typeof id !== 'string' || !SLUG.test(id)) throw new Error('人才 id 必须是小写字母、数字、连字符,≤64 位');
|
|
269
|
+
const yaml = stringify(talentDoc(input), { lineWidth: 0 });
|
|
270
|
+
const talent = parseTalent(yaml, `talent ${id}`); // 先验后写
|
|
271
|
+
const dir = join(userRoot, 'talents', id);
|
|
272
|
+
mkdirSync(dir, { recursive: true });
|
|
273
|
+
writeFileSync(join(dir, 'talent.yml'), yaml, 'utf8');
|
|
274
|
+
return { id, source: 'user', dir, ...talent };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** 删一个用户自定义人才(连同自带技能)。只删用户根下的。 */
|
|
278
|
+
export function deleteTalent(userRoot, id) {
|
|
279
|
+
if (typeof id !== 'string' || !SLUG.test(id)) throw new Error('人才 id 不合法');
|
|
280
|
+
rmSync(join(userRoot, 'talents', id), { recursive: true, force: true });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** 把一份技能装进某人才自己的文件夹:`<talentDir>/skills/<name>/SKILL.md`。返回规范后的技能名。 */
|
|
284
|
+
export function installSkill(talentDir, name, markdown) {
|
|
285
|
+
if (typeof talentDir !== 'string' || talentDir === '') throw new Error('installSkill: 需要 talentDir');
|
|
286
|
+
const safe = skillName(name, '技能名');
|
|
287
|
+
const dir = join(talentDir, 'skills', safe);
|
|
288
|
+
mkdirSync(dir, { recursive: true });
|
|
289
|
+
writeFileSync(join(dir, 'SKILL.md'), typeof markdown === 'string' ? markdown : '', 'utf8');
|
|
290
|
+
return safe;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** 读某人才自带技能的正文(给预览与 fork)。 */
|
|
294
|
+
export function readSkillText(talentDir, name) {
|
|
295
|
+
return readFileSync(join(talentDir, 'skills', skillName(name, '技能名'), 'SKILL.md'), 'utf8');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 列出可安装的技能:当前目录里每个人才自带的技能(名字 + 来源人才),给「从已有装」挑。 */
|
|
299
|
+
export function listInstallableSkills(catalog) {
|
|
300
|
+
const out = [];
|
|
301
|
+
for (const t of catalog.talents.values()) for (const name of t.skills) out.push({ name, talent: t.id, talentName: t.name, source: t.source });
|
|
302
|
+
return out.sort((a, b) => a.name.localeCompare(b.name) || a.talent.localeCompare(b.talent));
|
|
303
|
+
}
|