@michengai/dsh-agency-agents 1.2.21 → 1.2.23
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 +15 -0
- package/CHANGELOG.zh-CN.md +15 -0
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/lib/client.js +24 -24
- package/lib/index.js +1 -1
- package/lib/remote.js +1 -1
- package/lib/{src-C-tLCf9B.js → src-CrPezVot.js} +401 -62
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as unquote, I as ZH_NAME, S as truncate, _ as resolveCatalogRoot, a as SUMMON_EXPERTS_MAX, b as stripBom, c as createAgencyPersonaSource, d as loadCatalog, f as mapPool, g as readLocalizedExpertPrompt, h as readExpertPrompt, i as SUMMON_EXPERTS_CONCURRENCY, l as inject, m as parseFrontmatter, n as Config, o as SUMMON_TASK_MAX_CHARS, p as name, r as DEFAULT_DIVISIONS, s as apply, t as AGENCY_PERSONA_SERVICE, u as liveSchemaField, v as resolveExpert, w as validateSummonSpecs, x as toSummonItemResult, y as sanitize } from "./src-
|
|
1
|
+
import { C as unquote, I as ZH_NAME, S as truncate, _ as resolveCatalogRoot, a as SUMMON_EXPERTS_MAX, b as stripBom, c as createAgencyPersonaSource, d as loadCatalog, f as mapPool, g as readLocalizedExpertPrompt, h as readExpertPrompt, i as SUMMON_EXPERTS_CONCURRENCY, l as inject, m as parseFrontmatter, n as Config, o as SUMMON_TASK_MAX_CHARS, p as name, r as DEFAULT_DIVISIONS, s as apply, t as AGENCY_PERSONA_SERVICE, u as liveSchemaField, v as resolveExpert, w as validateSummonSpecs, x as toSummonItemResult, y as sanitize } from "./src-CrPezVot.js";
|
|
2
2
|
export { AGENCY_PERSONA_SERVICE, Config, DEFAULT_DIVISIONS, SUMMON_EXPERTS_CONCURRENCY, SUMMON_EXPERTS_MAX, SUMMON_TASK_MAX_CHARS, ZH_NAME, apply, createAgencyPersonaSource, inject, liveSchemaField, loadCatalog, mapPool, name, parseFrontmatter, readExpertPrompt, readLocalizedExpertPrompt, resolveCatalogRoot, resolveExpert, sanitize, stripBom, toSummonItemResult, truncate, unquote, validateSummonSpecs };
|
package/lib/remote.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as settingsNamespaceCompat, B as teamText, D as storedProposalSchema, E as latestProposalSchema, F as expertEditSchema, L as AGENCY_TEAM_SERVICE, M as CUSTOM_EXPERT_SLUG, N as catalogSnapshotSchema, O as formatHost, P as customExpertInputSchema, R as teamInputSchema, T as nativeTeamMemberName, g as readLocalizedExpertPrompt, h as readExpertPrompt, j as AGENCY_LIBRARY_SERVICE, k as readHostLocale, t as AGENCY_PERSONA_SERVICE, z as teamSnapshotSchema } from "./src-
|
|
1
|
+
import { A as settingsNamespaceCompat, B as teamText, D as storedProposalSchema, E as latestProposalSchema, F as expertEditSchema, L as AGENCY_TEAM_SERVICE, M as CUSTOM_EXPERT_SLUG, N as catalogSnapshotSchema, O as formatHost, P as customExpertInputSchema, R as teamInputSchema, T as nativeTeamMemberName, g as readLocalizedExpertPrompt, h as readExpertPrompt, j as AGENCY_LIBRARY_SERVICE, k as readHostLocale, t as AGENCY_PERSONA_SERVICE, z as teamSnapshotSchema } from "./src-CrPezVot.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
4
4
|
//#region src/remote-contract.ts
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
4
|
+
import { existsSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { open, readFile, readdir, stat } from "node:fs/promises";
|
|
6
6
|
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
7
7
|
import schema from "@deepseek-ai/schemastery";
|
|
@@ -58,7 +58,7 @@ const TEAM_EN = {
|
|
|
58
58
|
"{0}\n\n完整任务与专家职责以队友收到的本次任务消息为准。": "{0}\n\nThe current task message sent to the teammate contains the full task and responsibilities.",
|
|
59
59
|
"你是专家队友 {0}。以下为本次专家职责与任务要求,必须服从宿主权限及系统约束。": "You are expert teammate {0}. The following assignment is subject to host permissions and system constraints.",
|
|
60
60
|
"本次任务编号:{0}。使用 team_task_update 领取此任务,再阅读与你职责相关的材料并完成分析。": "Task ID: {0}. Claim this task using team_task_update, then read the relevant material and complete your analysis.",
|
|
61
|
-
"完成后先用 send_message 将完整结论发送给 lead
|
|
61
|
+
"完成后先用 send_message 将完整结论发送给 lead,再将任务标记完成。任务缺少资料时报告具体缺口,不伪造结论。严禁使用任何专家召唤工具(如 summon_expert、summon_experts、list_experts、summon_expert_team 等)或 spawn_teammate,不创建额外子代理、不自动追加轮次。": "Send the complete findings to lead with send_message before marking the task complete. Report specific missing materials instead of inventing conclusions. Strictly refrain from using any expert summoning tools (such as summon_expert, summon_experts, list_experts, summon_expert_team, etc.) or spawn_teammate, and do not create additional subagents or start more rounds.",
|
|
62
62
|
"原生队友创建失败": "Native teammate creation failed",
|
|
63
63
|
"委派已接受,等待成员结果。": "Dispatch accepted; waiting for member results.",
|
|
64
64
|
"委派失败且任务 {0} 清理失败,请检查任务板。": "Dispatch and cleanup of task {0} failed. Check the task board.",
|
|
@@ -1459,6 +1459,7 @@ schema.object({
|
|
|
1459
1459
|
customExperts: schema.array(schema.any()).default([]),
|
|
1460
1460
|
customTeams: schema.array(schema.any()).default([]),
|
|
1461
1461
|
enabledTeams: schema.array(schema.string()).default([]),
|
|
1462
|
+
entryLocation: schema.string(),
|
|
1462
1463
|
sidebarEntry: schema.boolean(),
|
|
1463
1464
|
onlineUpdate: schema.boolean(),
|
|
1464
1465
|
composerRecommend: schema.boolean().default(false)
|
|
@@ -1721,15 +1722,18 @@ function hasAgencySettingsData(value) {
|
|
|
1721
1722
|
/** 从插件配置读取启用名单。volatile 引用每次调用都重新取值,并复制冻结快照。 */
|
|
1722
1723
|
function readAgencySettings(config, locale = "zh") {
|
|
1723
1724
|
const record = config;
|
|
1725
|
+
const rawLocation = unwrapLive(record.entryLocation);
|
|
1724
1726
|
const rawSidebar = unwrapLive(record.sidebarEntry);
|
|
1725
1727
|
const rawUpdate = unwrapLive(record.onlineUpdate);
|
|
1726
1728
|
const rawRecommend = unwrapLive(record.composerRecommend);
|
|
1729
|
+
const location = rawLocation === "both" || rawLocation === "sidebar" || rawLocation === "settings" ? rawLocation : rawSidebar === true ? "both" : "settings";
|
|
1727
1730
|
const value = {
|
|
1728
1731
|
enabled: stringList(unwrapLive(record.enabled)),
|
|
1729
1732
|
customExperts: objectList(unwrapLive(record.customExperts)),
|
|
1730
1733
|
customTeams: objectList(unwrapLive(record.customTeams)),
|
|
1731
1734
|
enabledTeams: stringList(unwrapLive(record.enabledTeams)),
|
|
1732
|
-
|
|
1735
|
+
entryLocation: location,
|
|
1736
|
+
sidebarEntry: typeof rawSidebar === "boolean" ? rawSidebar : location !== "settings",
|
|
1733
1737
|
onlineUpdate: typeof rawUpdate === "boolean" ? rawUpdate : void 0,
|
|
1734
1738
|
composerRecommend: typeof rawRecommend === "boolean" ? rawRecommend : void 0
|
|
1735
1739
|
};
|
|
@@ -1754,6 +1758,7 @@ function agencySettingsFromLegacyDocument(document) {
|
|
|
1754
1758
|
};
|
|
1755
1759
|
}
|
|
1756
1760
|
function profileHome(ctx) {
|
|
1761
|
+
if (!ctx) return void 0;
|
|
1757
1762
|
try {
|
|
1758
1763
|
const profile = ctx.get?.("profileContext");
|
|
1759
1764
|
if (typeof profile?.home === "string" && profile.home.trim() !== "") return profile.home;
|
|
@@ -1846,19 +1851,77 @@ async function recoverImportedAgencySettings(ctx, namespace, current, isCancelle
|
|
|
1846
1851
|
], revision);
|
|
1847
1852
|
});
|
|
1848
1853
|
}
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
const
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1854
|
+
let inMemoryState = {};
|
|
1855
|
+
function getLocalStateFile(ctx) {
|
|
1856
|
+
const home = ctx ? profileHome(ctx) : void 0;
|
|
1857
|
+
if (!home) return void 0;
|
|
1858
|
+
return join(home, "agency-settings.json");
|
|
1859
|
+
}
|
|
1860
|
+
function readLocalAgencyState(ctx) {
|
|
1861
|
+
const file = getLocalStateFile(ctx);
|
|
1862
|
+
if (file && existsSync(file)) try {
|
|
1863
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
1864
|
+
if (parsed && typeof parsed === "object") inMemoryState = Object.assign({}, inMemoryState, parsed);
|
|
1865
|
+
} catch {}
|
|
1866
|
+
return inMemoryState;
|
|
1867
|
+
}
|
|
1868
|
+
function writeLocalAgencyState(ctx, state) {
|
|
1869
|
+
inMemoryState = Object.assign({}, inMemoryState, state);
|
|
1870
|
+
const file = getLocalStateFile(ctx);
|
|
1871
|
+
if (file) try {
|
|
1872
|
+
writeFileSync(file, JSON.stringify(inMemoryState, null, 2), "utf8");
|
|
1873
|
+
} catch (err) {
|
|
1874
|
+
console.warn("[agency-agents] 写入 agency-settings.json 失败:", err);
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
function getAgencyEntryLocation(settings, ctx) {
|
|
1878
|
+
const local = readLocalAgencyState(ctx).entryLocation;
|
|
1879
|
+
if (local === "both" || local === "sidebar" || local === "settings") return local;
|
|
1880
|
+
const loc = unwrapLive(settings?.entryLocation);
|
|
1881
|
+
if (loc === "both" || loc === "sidebar" || loc === "settings") return loc;
|
|
1882
|
+
return unwrapLive(settings?.sidebarEntry) === true ? "both" : "settings";
|
|
1883
|
+
}
|
|
1884
|
+
async function setAgencyEntryLocation(ctx, namespace, location) {
|
|
1885
|
+
if (location !== "both" && location !== "sidebar" && location !== "settings") throw new TypeError("location must be one of 'both', 'sidebar', or 'settings'");
|
|
1886
|
+
const sidebarEnabled = location !== "settings";
|
|
1887
|
+
writeLocalAgencyState(ctx, {
|
|
1888
|
+
entryLocation: location,
|
|
1889
|
+
sidebarEntry: sidebarEnabled
|
|
1860
1890
|
});
|
|
1861
|
-
|
|
1891
|
+
try {
|
|
1892
|
+
const settings = ctx.settings;
|
|
1893
|
+
const revision = settings?.describe?.().find((item) => item.ns === namespace)?.revision;
|
|
1894
|
+
await writeLiveSettings(ctx, async () => {
|
|
1895
|
+
if (typeof settings?.mutate === "function") try {
|
|
1896
|
+
await settings.mutate(namespace, [{
|
|
1897
|
+
op: "set",
|
|
1898
|
+
path: ["entryLocation"],
|
|
1899
|
+
value: location
|
|
1900
|
+
}, {
|
|
1901
|
+
op: "set",
|
|
1902
|
+
path: ["sidebarEntry"],
|
|
1903
|
+
value: sidebarEnabled
|
|
1904
|
+
}], revision);
|
|
1905
|
+
} catch {
|
|
1906
|
+
await settings.mutate(namespace, [{
|
|
1907
|
+
op: "set",
|
|
1908
|
+
path: ["sidebarEntry"],
|
|
1909
|
+
value: sidebarEnabled
|
|
1910
|
+
}]);
|
|
1911
|
+
}
|
|
1912
|
+
else if (typeof settings?.update === "function") try {
|
|
1913
|
+
await settings.update(namespace, {
|
|
1914
|
+
entryLocation: location,
|
|
1915
|
+
sidebarEntry: sidebarEnabled
|
|
1916
|
+
}, revision);
|
|
1917
|
+
} catch {
|
|
1918
|
+
await settings.update(namespace, { sidebarEntry: sidebarEnabled });
|
|
1919
|
+
}
|
|
1920
|
+
});
|
|
1921
|
+
} catch (err) {
|
|
1922
|
+
console.warn("[agency-agents] 宿主 live settings 同步受阻,已通过本地配置持久化保持:", err);
|
|
1923
|
+
}
|
|
1924
|
+
return location;
|
|
1862
1925
|
}
|
|
1863
1926
|
async function setAgencyOnlineUpdate(ctx, namespace, enabled) {
|
|
1864
1927
|
if (typeof enabled !== "boolean") throw new TypeError("enabled must be a boolean");
|
|
@@ -2436,6 +2499,30 @@ function validateTeamProposal(input, availableExperts) {
|
|
|
2436
2499
|
}
|
|
2437
2500
|
//#endregion
|
|
2438
2501
|
//#region src/team-engine.ts
|
|
2502
|
+
const EXPERT_TOOL_DENY = [
|
|
2503
|
+
"summon_expert",
|
|
2504
|
+
"summon_experts",
|
|
2505
|
+
"list_experts",
|
|
2506
|
+
"list_expert_teams",
|
|
2507
|
+
"get_expert_team",
|
|
2508
|
+
"summon_expert_team"
|
|
2509
|
+
];
|
|
2510
|
+
const AGENCY_TEAMMATE_NAME_PATTERN = /^agency-[a-z0-9-]+-[0-9a-f]{10}$/u;
|
|
2511
|
+
function isAgencyTeammateName(name) {
|
|
2512
|
+
return AGENCY_TEAMMATE_NAME_PATTERN.test(name);
|
|
2513
|
+
}
|
|
2514
|
+
const MAX_NATIVE_SETUPS = 128;
|
|
2515
|
+
const nativeTeammateSetups = /* @__PURE__ */ new Map();
|
|
2516
|
+
function registerNativeTeammateSetup(name, setup) {
|
|
2517
|
+
if (nativeTeammateSetups.size >= MAX_NATIVE_SETUPS) {
|
|
2518
|
+
const oldest = nativeTeammateSetups.keys().next().value;
|
|
2519
|
+
if (oldest !== void 0) nativeTeammateSetups.delete(oldest);
|
|
2520
|
+
}
|
|
2521
|
+
nativeTeammateSetups.set(name, setup);
|
|
2522
|
+
}
|
|
2523
|
+
function getNativeTeammateSetup(name) {
|
|
2524
|
+
return nativeTeammateSetups.get(name);
|
|
2525
|
+
}
|
|
2439
2526
|
const methods = [
|
|
2440
2527
|
"tryMembership",
|
|
2441
2528
|
"listMembers",
|
|
@@ -2461,7 +2548,7 @@ const delegationTools = /* @__PURE__ */ new Set([
|
|
|
2461
2548
|
function blocksNativeDelegation(service, agent, tool) {
|
|
2462
2549
|
if (!agent || !delegationTools.has(tool) || !isNativeTeamService(service)) return false;
|
|
2463
2550
|
const member = service.tryMembership(agent);
|
|
2464
|
-
return member?.role === "teammate" &&
|
|
2551
|
+
return member?.role === "teammate" && isAgencyTeammateName(member.name);
|
|
2465
2552
|
}
|
|
2466
2553
|
function isNativeTeamService(value) {
|
|
2467
2554
|
return value !== null && typeof value === "object" && methods.every((key) => typeof value[key] === "function");
|
|
@@ -2543,6 +2630,7 @@ async function dispatchNativeTeam(args) {
|
|
|
2543
2630
|
run: async (member) => {
|
|
2544
2631
|
args.signal.throwIfAborted();
|
|
2545
2632
|
const name = nativeTeamMemberName(args.team.id, member.slug);
|
|
2633
|
+
registerNativeTeammateSetup(name, { toolFilter: { deny: [...EXPERT_TOOL_DENY] } });
|
|
2546
2634
|
const existing = args.service.listMembers(args.agent).find((item) => item.name === name);
|
|
2547
2635
|
if (existing && !["idle", "inactive"].includes(existing.status)) throw new Error(tx("专家队友仍在运行或不可恢复:{0}", [existing.status]));
|
|
2548
2636
|
const task = await args.service.createTask(args.agent, {
|
|
@@ -2561,7 +2649,7 @@ async function dispatchNativeTeam(args) {
|
|
|
2561
2649
|
member.persona,
|
|
2562
2650
|
member.prompt,
|
|
2563
2651
|
tx("本次任务编号:{0}。使用 team_task_update 领取此任务,再阅读与你职责相关的材料并完成分析。", [task.id]),
|
|
2564
|
-
tx("完成后先用 send_message 将完整结论发送给 lead
|
|
2652
|
+
tx("完成后先用 send_message 将完整结论发送给 lead,再将任务标记完成。任务缺少资料时报告具体缺口,不伪造结论。严禁使用任何专家召唤工具(如 summon_expert、summon_experts、list_experts、summon_expert_team 等)或 spawn_teammate,不创建额外子代理、不自动追加轮次。")
|
|
2565
2653
|
].join("\n\n");
|
|
2566
2654
|
if (existing) await args.service.sendMessage(args.agent, {
|
|
2567
2655
|
target: name,
|
|
@@ -2634,6 +2722,214 @@ async function dispatchNativeTeam(args) {
|
|
|
2634
2722
|
activeLeads.delete(args.agent);
|
|
2635
2723
|
}
|
|
2636
2724
|
}
|
|
2725
|
+
//#endregion
|
|
2726
|
+
//#region src/subagent-registry.ts
|
|
2727
|
+
/** 热文件:只保存「哪个子代理用了哪个专家」的引用,体积与专家正文无关。 */
|
|
2728
|
+
const CHILDREN_FILE = "agency-subagents.json";
|
|
2729
|
+
/** 冷文件:每个专家只保存一份正文,供冷恢复解析。 */
|
|
2730
|
+
const EXPERT_FILE = "agency-expert-personas.json";
|
|
2731
|
+
/** 门禁清单文件:只存 id,允许远大于配置上限的条目数。 */
|
|
2732
|
+
const IDS_FILE = "agency-subagent-ids.json";
|
|
2733
|
+
const MAX_PERSISTED_CHILDREN = 128;
|
|
2734
|
+
const MAX_PERSISTED_IDS = 2048;
|
|
2735
|
+
const expertPersonas = /* @__PURE__ */ new Map();
|
|
2736
|
+
const childRecords = /* @__PURE__ */ new Map();
|
|
2737
|
+
const managedIds = /* @__PURE__ */ new Set();
|
|
2738
|
+
let personasDirty = false;
|
|
2739
|
+
let idsDirty = false;
|
|
2740
|
+
/** 热文件存在却解析失败:此时内存里的 children 不代表真实历史,必须暂停孤儿清理。 */
|
|
2741
|
+
let childrenUnreadable = false;
|
|
2742
|
+
let flushTimer;
|
|
2743
|
+
let pendingFlushCtx;
|
|
2744
|
+
/** 缓存最近一次解析成功的 profile 目录:卸载/退出时 profileContext 可能已不可用。 */
|
|
2745
|
+
let lastHome;
|
|
2746
|
+
function resolveHome(ctx) {
|
|
2747
|
+
const home = profileHome(ctx);
|
|
2748
|
+
if (home) lastHome = home;
|
|
2749
|
+
return home ?? lastHome;
|
|
2750
|
+
}
|
|
2751
|
+
/** 区分「文件不存在」与「存在但读不出来」:前者可以正常清理,后者必须保守处理。 */
|
|
2752
|
+
function readJsonFile(file) {
|
|
2753
|
+
if (!existsSync(file)) return { state: "missing" };
|
|
2754
|
+
try {
|
|
2755
|
+
return {
|
|
2756
|
+
state: "ok",
|
|
2757
|
+
value: JSON.parse(readFileSync(file, "utf8"))
|
|
2758
|
+
};
|
|
2759
|
+
} catch (error) {
|
|
2760
|
+
console.warn(`[agency-agents] 读取 ${file} 失败:`, error);
|
|
2761
|
+
return { state: "unreadable" };
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
/** 临时文件 + rename 原子替换;失败时清掉临时文件,绝不留残缺目标文件。 */
|
|
2765
|
+
function writeJsonAtomic(file, payload) {
|
|
2766
|
+
let tmpFile;
|
|
2767
|
+
try {
|
|
2768
|
+
tmpFile = `${file}.${randomUUID()}.tmp`;
|
|
2769
|
+
writeFileSync(tmpFile, JSON.stringify(payload, null, 2), "utf8");
|
|
2770
|
+
renameSync(tmpFile, file);
|
|
2771
|
+
tmpFile = void 0;
|
|
2772
|
+
return true;
|
|
2773
|
+
} catch (error) {
|
|
2774
|
+
console.warn(`[agency-agents] 写入 ${file} 失败:`, error);
|
|
2775
|
+
return false;
|
|
2776
|
+
} finally {
|
|
2777
|
+
if (tmpFile !== void 0 && existsSync(tmpFile)) try {
|
|
2778
|
+
unlinkSync(tmpFile);
|
|
2779
|
+
} catch {}
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
function adoptChildRecord(id, config) {
|
|
2783
|
+
if (!config || typeof config !== "object") return;
|
|
2784
|
+
childRecords.set(id, {
|
|
2785
|
+
slug: typeof config.slug === "string" ? config.slug : void 0,
|
|
2786
|
+
persona: typeof config.persona === "string" ? config.persona : void 0,
|
|
2787
|
+
toolFilter: Array.isArray(config.toolFilter?.deny) ? { deny: config.toolFilter.deny } : void 0,
|
|
2788
|
+
updatedAt: typeof config.updatedAt === "number" ? config.updatedAt : Date.now()
|
|
2789
|
+
});
|
|
2790
|
+
managedIds.add(id);
|
|
2791
|
+
}
|
|
2792
|
+
function initSubagentRegistry(ctx) {
|
|
2793
|
+
const home = resolveHome(ctx);
|
|
2794
|
+
if (!home) return;
|
|
2795
|
+
const expertsRead = readJsonFile(join(home, EXPERT_FILE));
|
|
2796
|
+
if (expertsRead.state === "ok") {
|
|
2797
|
+
const doc = expertsRead.value;
|
|
2798
|
+
if (doc && typeof doc === "object" && doc.experts && typeof doc.experts === "object") {
|
|
2799
|
+
for (const [slug, persona] of Object.entries(doc.experts)) if (typeof persona === "string") expertPersonas.set(slug, persona);
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
const idsRead = readJsonFile(join(home, IDS_FILE));
|
|
2803
|
+
if (idsRead.state === "ok") {
|
|
2804
|
+
const doc = idsRead.value;
|
|
2805
|
+
if (doc && typeof doc === "object" && Array.isArray(doc.ids)) {
|
|
2806
|
+
for (const id of doc.ids) if (typeof id === "string" && id !== "") managedIds.add(id);
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
const childrenRead = readJsonFile(join(home, CHILDREN_FILE));
|
|
2810
|
+
if (childrenRead.state === "unreadable") {
|
|
2811
|
+
childrenUnreadable = true;
|
|
2812
|
+
return;
|
|
2813
|
+
}
|
|
2814
|
+
if (childrenRead.state === "missing") return;
|
|
2815
|
+
const doc = childrenRead.value;
|
|
2816
|
+
if (!doc || typeof doc !== "object") return;
|
|
2817
|
+
const versioned = doc.version === 1 || doc.version === 2;
|
|
2818
|
+
const legacyExperts = versioned ? doc.experts : void 0;
|
|
2819
|
+
if (legacyExperts && typeof legacyExperts === "object") {
|
|
2820
|
+
for (const [slug, persona] of Object.entries(legacyExperts)) if (typeof persona === "string" && !expertPersonas.has(slug)) expertPersonas.set(slug, persona);
|
|
2821
|
+
personasDirty = true;
|
|
2822
|
+
}
|
|
2823
|
+
const rows = versioned ? doc.children ?? {} : doc;
|
|
2824
|
+
for (const [id, config] of Object.entries(rows)) adoptChildRecord(id, config);
|
|
2825
|
+
if (idsRead.state !== "ok" && managedIds.size > 0) idsDirty = true;
|
|
2826
|
+
}
|
|
2827
|
+
/** 丢弃不再被任何子代理引用的专家正文,避免冷文件随历史召唤无限增长。 */
|
|
2828
|
+
function pruneUnreferencedExperts() {
|
|
2829
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
2830
|
+
for (const child of childRecords.values()) if (child.slug) referenced.add(child.slug);
|
|
2831
|
+
for (const slug of [...expertPersonas.keys()]) {
|
|
2832
|
+
if (referenced.has(slug)) continue;
|
|
2833
|
+
expertPersonas.delete(slug);
|
|
2834
|
+
personasDirty = true;
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
function flushSubagentRegistrySync(ctx) {
|
|
2838
|
+
if (flushTimer !== void 0) {
|
|
2839
|
+
clearTimeout(flushTimer);
|
|
2840
|
+
flushTimer = void 0;
|
|
2841
|
+
}
|
|
2842
|
+
const targetCtx = ctx ?? pendingFlushCtx;
|
|
2843
|
+
pendingFlushCtx = void 0;
|
|
2844
|
+
const home = resolveHome(targetCtx);
|
|
2845
|
+
if (!home) return;
|
|
2846
|
+
if (!childrenUnreadable) pruneUnreferencedExperts();
|
|
2847
|
+
let expertsSaved = true;
|
|
2848
|
+
if (personasDirty) {
|
|
2849
|
+
const expertsPayload = {
|
|
2850
|
+
version: 1,
|
|
2851
|
+
experts: Object.fromEntries(expertPersonas.entries())
|
|
2852
|
+
};
|
|
2853
|
+
expertsSaved = writeJsonAtomic(join(home, EXPERT_FILE), expertsPayload);
|
|
2854
|
+
}
|
|
2855
|
+
if (idsDirty) {
|
|
2856
|
+
const idsPayload = {
|
|
2857
|
+
version: 1,
|
|
2858
|
+
ids: [...managedIds]
|
|
2859
|
+
};
|
|
2860
|
+
if (writeJsonAtomic(join(home, IDS_FILE), idsPayload)) idsDirty = false;
|
|
2861
|
+
}
|
|
2862
|
+
const inlineExperts = personasDirty && !expertsSaved ? { experts: Object.fromEntries(expertPersonas.entries()) } : {};
|
|
2863
|
+
const childrenPayload = {
|
|
2864
|
+
version: 2,
|
|
2865
|
+
children: Object.fromEntries(childRecords.entries()),
|
|
2866
|
+
...inlineExperts
|
|
2867
|
+
};
|
|
2868
|
+
writeJsonAtomic(join(home, CHILDREN_FILE), childrenPayload);
|
|
2869
|
+
if (personasDirty && expertsSaved) personasDirty = false;
|
|
2870
|
+
}
|
|
2871
|
+
function recordSubagentConfig(id, config, ctx) {
|
|
2872
|
+
if (!managedIds.has(id)) {
|
|
2873
|
+
managedIds.add(id);
|
|
2874
|
+
if (managedIds.size > MAX_PERSISTED_IDS) {
|
|
2875
|
+
const oldest = managedIds.values().next().value;
|
|
2876
|
+
if (oldest !== void 0 && oldest !== id) managedIds.delete(oldest);
|
|
2877
|
+
}
|
|
2878
|
+
idsDirty = true;
|
|
2879
|
+
}
|
|
2880
|
+
if (childRecords.size >= MAX_PERSISTED_CHILDREN) {
|
|
2881
|
+
const oldest = childRecords.keys().next().value;
|
|
2882
|
+
if (oldest !== void 0) childRecords.delete(oldest);
|
|
2883
|
+
}
|
|
2884
|
+
if (config.slug && config.persona) {
|
|
2885
|
+
if (expertPersonas.get(config.slug) !== config.persona) {
|
|
2886
|
+
expertPersonas.set(config.slug, config.persona);
|
|
2887
|
+
personasDirty = true;
|
|
2888
|
+
}
|
|
2889
|
+
childRecords.set(id, {
|
|
2890
|
+
slug: config.slug,
|
|
2891
|
+
toolFilter: config.toolFilter,
|
|
2892
|
+
updatedAt: Date.now()
|
|
2893
|
+
});
|
|
2894
|
+
} else childRecords.set(id, {
|
|
2895
|
+
persona: config.persona,
|
|
2896
|
+
toolFilter: config.toolFilter,
|
|
2897
|
+
updatedAt: Date.now()
|
|
2898
|
+
});
|
|
2899
|
+
scheduleFlush(ctx);
|
|
2900
|
+
}
|
|
2901
|
+
/**
|
|
2902
|
+
* 撤销一条尚未真正建立的子代理记录:召唤失败时不能让幻影条目占住条目配额或门禁清单。
|
|
2903
|
+
* 已经跑过的子代理不受影响(调用方只在创建失败时回滚)。
|
|
2904
|
+
*/
|
|
2905
|
+
function forgetSubagentConfig(id, ctx) {
|
|
2906
|
+
const droppedRecord = childRecords.delete(id);
|
|
2907
|
+
const droppedId = managedIds.delete(id);
|
|
2908
|
+
if (droppedId) idsDirty = true;
|
|
2909
|
+
if (!droppedRecord && !droppedId) return;
|
|
2910
|
+
scheduleFlush(ctx);
|
|
2911
|
+
}
|
|
2912
|
+
function scheduleFlush(ctx) {
|
|
2913
|
+
pendingFlushCtx = ctx;
|
|
2914
|
+
if (flushTimer === void 0) flushTimer = setTimeout(() => {
|
|
2915
|
+
flushTimer = void 0;
|
|
2916
|
+
flushSubagentRegistrySync();
|
|
2917
|
+
}, 50);
|
|
2918
|
+
}
|
|
2919
|
+
function getSubagentConfig(id) {
|
|
2920
|
+
const child = childRecords.get(id);
|
|
2921
|
+
if (!child) return void 0;
|
|
2922
|
+
const persona = (child.slug ? expertPersonas.get(child.slug) : void 0) ?? child.persona;
|
|
2923
|
+
return {
|
|
2924
|
+
slug: child.slug,
|
|
2925
|
+
persona,
|
|
2926
|
+
toolFilter: child.toolFilter,
|
|
2927
|
+
updatedAt: child.updatedAt
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
function isManagedSubagentId(id) {
|
|
2931
|
+
return managedIds.has(id);
|
|
2932
|
+
}
|
|
2637
2933
|
const PLUGIN_UPDATE_IPC = "apply-plugin-updates";
|
|
2638
2934
|
function header$1(request, name) {
|
|
2639
2935
|
const value = request.headers?.[name];
|
|
@@ -3202,6 +3498,7 @@ const configFields = {
|
|
|
3202
3498
|
maxDepth: schema$1.natural().min(1),
|
|
3203
3499
|
updater: schema$1.boolean(),
|
|
3204
3500
|
onlineUpdate: schema$1.boolean(),
|
|
3501
|
+
entryLocation: schema$1.string(),
|
|
3205
3502
|
sidebarEntry: schema$1.boolean(),
|
|
3206
3503
|
composerRecommend: schema$1.boolean()
|
|
3207
3504
|
};
|
|
@@ -3210,6 +3507,7 @@ for (const [key, field] of [
|
|
|
3210
3507
|
["customExperts", schema$1.array(schema$1.any()).default([])],
|
|
3211
3508
|
["customTeams", schema$1.array(schema$1.any()).default([])],
|
|
3212
3509
|
["enabledTeams", schema$1.array(schema$1.string()).default([])],
|
|
3510
|
+
["entryLocation", schema$1.string()],
|
|
3213
3511
|
["sidebarEntry", schema$1.boolean()],
|
|
3214
3512
|
["onlineUpdate", schema$1.boolean()],
|
|
3215
3513
|
["composerRecommend", schema$1.boolean()]
|
|
@@ -3563,11 +3861,38 @@ function apply(ctx, config) {
|
|
|
3563
3861
|
ctx.reflect.provide("agencyAgentsTeamEngine", () => resolveTeamEngine(ctx, void 0, maxDepth).status);
|
|
3564
3862
|
const proposalRegistry = createProposalRegistry();
|
|
3565
3863
|
ctx.reflect.provide("agencyProposalRegistry", proposalRegistry);
|
|
3864
|
+
initSubagentRegistry(ctx);
|
|
3865
|
+
if (typeof ctx.effect === "function") ctx.effect(() => () => flushSubagentRegistrySync(ctx), "agency-agents: flush subagent registry");
|
|
3866
|
+
ctx.on?.("agent/created", (event) => {
|
|
3867
|
+
const agent = event.agent;
|
|
3868
|
+
let config = getSubagentConfig(agent.id);
|
|
3869
|
+
if (!config) {
|
|
3870
|
+
const membership = (ctx.get?.("agentTeams"))?.tryMembership?.(agent);
|
|
3871
|
+
if (membership?.role === "teammate" && isAgencyTeammateName(membership.name)) config = { toolFilter: getNativeTeammateSetup(membership.name)?.toolFilter ?? { deny: [...EXPERT_TOOL_DENY] } };
|
|
3872
|
+
}
|
|
3873
|
+
if (!config) return void 0;
|
|
3874
|
+
const childCtx = agent.ctx;
|
|
3875
|
+
if (childCtx?.systemPrompt && config.persona) childCtx.systemPrompt.section({
|
|
3876
|
+
name: PERSONA_PREFIX_SECTION,
|
|
3877
|
+
order: childCtx.systemPrompt.getSectionOrder("DEPLOYMENT_PERSONA_PREFIX"),
|
|
3878
|
+
text: config.persona
|
|
3879
|
+
});
|
|
3880
|
+
if (childCtx?.tools && config.toolFilter) try {
|
|
3881
|
+
childCtx.tools.restrict(config.toolFilter);
|
|
3882
|
+
} catch (error) {
|
|
3883
|
+
console.warn?.("[agency-agents] tools.restrict failed for agent %s:", agent.id, error);
|
|
3884
|
+
}
|
|
3885
|
+
});
|
|
3566
3886
|
ctx.on?.("tools/pre-execute", async (exec, next) => {
|
|
3567
3887
|
if (blocksNativeDelegation(ctx.get("agentTeams"), exec.agent, exec.name)) return {
|
|
3568
3888
|
kind: "deny",
|
|
3569
3889
|
reason: teamTx("专家团成员不能继续创建子代理或扩展专家团,请将缺口交给主理人。")
|
|
3570
3890
|
};
|
|
3891
|
+
const agentId = exec.agent?.id;
|
|
3892
|
+
if (agentId && isManagedSubagentId(agentId) && EXPERT_TOOL_DENY.includes(exec.name)) return {
|
|
3893
|
+
kind: "deny",
|
|
3894
|
+
reason: teamTx("专家成员不能继续召唤专家团。")
|
|
3895
|
+
};
|
|
3571
3896
|
return next();
|
|
3572
3897
|
});
|
|
3573
3898
|
const retryCleanup = () => {
|
|
@@ -3623,32 +3948,62 @@ function apply(ctx, config) {
|
|
|
3623
3948
|
handler: async (request, response) => {
|
|
3624
3949
|
if (!checkAuthAndMutation(request, response)) return;
|
|
3625
3950
|
if (request.method === "GET" || request.method === "HEAD") {
|
|
3951
|
+
const location = getAgencyEntryLocation(settingsSource(), ctx);
|
|
3626
3952
|
response.writeHead(200, {
|
|
3627
3953
|
"content-type": "application/json; charset=utf-8",
|
|
3628
3954
|
"cache-control": "no-store"
|
|
3629
3955
|
});
|
|
3630
3956
|
response.end(request.method === "HEAD" ? void 0 : JSON.stringify({
|
|
3631
3957
|
ok: true,
|
|
3632
|
-
|
|
3958
|
+
location,
|
|
3959
|
+
enabled: location !== "settings"
|
|
3633
3960
|
}));
|
|
3634
3961
|
return;
|
|
3635
3962
|
}
|
|
3636
3963
|
if (request.method === "POST") {
|
|
3637
3964
|
const bodyRes = await readJsonBody(request);
|
|
3638
|
-
if (!bodyRes.ok || !bodyRes.data
|
|
3965
|
+
if (!bodyRes.ok || !bodyRes.data) {
|
|
3639
3966
|
response.writeHead(bodyRes.status ?? 400, { "content-type": "application/json; charset=utf-8" });
|
|
3640
3967
|
response.end(JSON.stringify({
|
|
3641
3968
|
ok: false,
|
|
3642
|
-
error: bodyRes.error ?? "
|
|
3969
|
+
error: bodyRes.error ?? "请求体不能为空"
|
|
3643
3970
|
}));
|
|
3644
3971
|
return;
|
|
3645
3972
|
}
|
|
3646
3973
|
try {
|
|
3647
|
-
|
|
3648
|
-
|
|
3974
|
+
if (typeof bodyRes.data.location === "string") {
|
|
3975
|
+
const loc = bodyRes.data.location;
|
|
3976
|
+
if (loc !== "both" && loc !== "sidebar" && loc !== "settings") {
|
|
3977
|
+
response.writeHead(400, { "content-type": "application/json; charset=utf-8" });
|
|
3978
|
+
response.end(JSON.stringify({
|
|
3979
|
+
ok: false,
|
|
3980
|
+
error: "location 必须是 both、sidebar 或 settings"
|
|
3981
|
+
}));
|
|
3982
|
+
return;
|
|
3983
|
+
}
|
|
3984
|
+
const location = await setAgencyEntryLocation(ctx, settingsNamespace, loc);
|
|
3985
|
+
response.writeHead(200, { "content-type": "application/json; charset=utf-8" });
|
|
3986
|
+
response.end(JSON.stringify({
|
|
3987
|
+
ok: true,
|
|
3988
|
+
location,
|
|
3989
|
+
enabled: location !== "settings"
|
|
3990
|
+
}));
|
|
3991
|
+
return;
|
|
3992
|
+
}
|
|
3993
|
+
if (typeof bodyRes.data.enabled === "boolean") {
|
|
3994
|
+
const location = await setAgencyEntryLocation(ctx, settingsNamespace, bodyRes.data.enabled ? "both" : "settings");
|
|
3995
|
+
response.writeHead(200, { "content-type": "application/json; charset=utf-8" });
|
|
3996
|
+
response.end(JSON.stringify({
|
|
3997
|
+
ok: true,
|
|
3998
|
+
location,
|
|
3999
|
+
enabled: location !== "settings"
|
|
4000
|
+
}));
|
|
4001
|
+
return;
|
|
4002
|
+
}
|
|
4003
|
+
response.writeHead(400, { "content-type": "application/json; charset=utf-8" });
|
|
3649
4004
|
response.end(JSON.stringify({
|
|
3650
|
-
ok:
|
|
3651
|
-
|
|
4005
|
+
ok: false,
|
|
4006
|
+
error: "缺少合法的 location 或 enabled 参数"
|
|
3652
4007
|
}));
|
|
3653
4008
|
} catch (err) {
|
|
3654
4009
|
response.writeHead(500, { "content-type": "application/json; charset=utf-8" });
|
|
@@ -3904,30 +4259,16 @@ function apply(ctx, config) {
|
|
|
3904
4259
|
}
|
|
3905
4260
|
}
|
|
3906
4261
|
async function startSubagentRun(providerName, options) {
|
|
3907
|
-
const { persona, toolFilter, label, signal, prompt, parent, maxDepth } = options;
|
|
4262
|
+
const { slug, persona, toolFilter, label, signal, prompt, parent, maxDepth } = options;
|
|
3908
4263
|
const service = ctx.subagents;
|
|
3909
4264
|
const runSignal = signal ?? new AbortController().signal;
|
|
3910
|
-
if (typeof service.start === "function") return service.start(providerName, {
|
|
3911
|
-
label,
|
|
3912
|
-
prompt,
|
|
3913
|
-
parent,
|
|
3914
|
-
signal: runSignal,
|
|
3915
|
-
...maxDepth === void 0 ? {} : { maxDepth },
|
|
3916
|
-
persona,
|
|
3917
|
-
toolFilter
|
|
3918
|
-
});
|
|
3919
4265
|
if (typeof service.startActivation === "function") {
|
|
3920
4266
|
const childId = SessionId(randomUUID());
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
order: childCtx.systemPrompt.getSectionOrder("DEPLOYMENT_PERSONA_PREFIX"),
|
|
3927
|
-
text: persona
|
|
3928
|
-
});
|
|
3929
|
-
if (childCtx.tools) childCtx.tools.restrict(toolFilter);
|
|
3930
|
-
});
|
|
4267
|
+
recordSubagentConfig(childId, {
|
|
4268
|
+
slug,
|
|
4269
|
+
persona,
|
|
4270
|
+
toolFilter
|
|
4271
|
+
}, ctx);
|
|
3931
4272
|
try {
|
|
3932
4273
|
return await service.startActivation({
|
|
3933
4274
|
provider: providerName,
|
|
@@ -3941,10 +4282,20 @@ function apply(ctx, config) {
|
|
|
3941
4282
|
signal: runSignal,
|
|
3942
4283
|
delivery: "caller"
|
|
3943
4284
|
});
|
|
3944
|
-
}
|
|
3945
|
-
|
|
4285
|
+
} catch (error) {
|
|
4286
|
+
forgetSubagentConfig(childId, ctx);
|
|
4287
|
+
throw error;
|
|
3946
4288
|
}
|
|
3947
4289
|
}
|
|
4290
|
+
if (typeof service.start === "function") return service.start(providerName, {
|
|
4291
|
+
label,
|
|
4292
|
+
prompt,
|
|
4293
|
+
parent,
|
|
4294
|
+
signal: runSignal,
|
|
4295
|
+
...maxDepth === void 0 ? {} : { maxDepth },
|
|
4296
|
+
persona,
|
|
4297
|
+
toolFilter
|
|
4298
|
+
});
|
|
3948
4299
|
throw new Error(formatHost(activeLocale(), "error.subagentsUnavailable"));
|
|
3949
4300
|
}
|
|
3950
4301
|
async function runExpert(query, task, exec) {
|
|
@@ -3963,6 +4314,7 @@ function apply(ctx, config) {
|
|
|
3963
4314
|
if (!enabledSet().has(expert.slug)) throw new Error(formatHost(locale, "error.expertDisabled", { name: localizedExpertName(expert, locale) }));
|
|
3964
4315
|
const { prompt: persona } = await personaSource.getPrompt(expert.slug, expert.division, locale);
|
|
3965
4316
|
const result = await awaitSubagentRun(await startSubagentRun(config.provider, {
|
|
4317
|
+
slug: expert.slug,
|
|
3966
4318
|
label: localizedExpertName(expert, locale),
|
|
3967
4319
|
prompt: [{
|
|
3968
4320
|
type: "text",
|
|
@@ -3970,14 +4322,7 @@ function apply(ctx, config) {
|
|
|
3970
4322
|
}],
|
|
3971
4323
|
parent: exec.agent,
|
|
3972
4324
|
persona: sanitize(persona),
|
|
3973
|
-
toolFilter: { deny: [
|
|
3974
|
-
"summon_expert",
|
|
3975
|
-
"summon_experts",
|
|
3976
|
-
"list_experts",
|
|
3977
|
-
"list_expert_teams",
|
|
3978
|
-
"get_expert_team",
|
|
3979
|
-
"summon_expert_team"
|
|
3980
|
-
] },
|
|
4325
|
+
toolFilter: { deny: [...EXPERT_TOOL_DENY] },
|
|
3981
4326
|
...maxDepth === void 0 ? {} : { maxDepth },
|
|
3982
4327
|
signal: exec.signal
|
|
3983
4328
|
}), exec.signal);
|
|
@@ -4211,6 +4556,7 @@ function apply(ctx, config) {
|
|
|
4211
4556
|
run: async (member) => {
|
|
4212
4557
|
exec.signal?.throwIfAborted();
|
|
4213
4558
|
const value = await awaitSubagentRun(await startSubagentRun(config.provider, {
|
|
4559
|
+
slug: member.slug,
|
|
4214
4560
|
label: member.name,
|
|
4215
4561
|
parent: exec.agent,
|
|
4216
4562
|
prompt: [{
|
|
@@ -4218,14 +4564,7 @@ function apply(ctx, config) {
|
|
|
4218
4564
|
text: member.prompt
|
|
4219
4565
|
}],
|
|
4220
4566
|
persona: sanitize(member.persona),
|
|
4221
|
-
toolFilter: { deny: [
|
|
4222
|
-
"summon_expert",
|
|
4223
|
-
"summon_experts",
|
|
4224
|
-
"list_experts",
|
|
4225
|
-
"list_expert_teams",
|
|
4226
|
-
"get_expert_team",
|
|
4227
|
-
"summon_expert_team"
|
|
4228
|
-
] },
|
|
4567
|
+
toolFilter: { deny: [...EXPERT_TOOL_DENY] },
|
|
4229
4568
|
...maxDepth === void 0 ? {} : { maxDepth },
|
|
4230
4569
|
signal: exec.signal
|
|
4231
4570
|
}), exec.signal);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@michengai/dsh-agency-agents",
|
|
3
3
|
"description": "DSH Agency Agents — 为 DeepSeek Harness 提供 331 名可召唤的专业智能体 · 331 summonable domain experts for DeepSeek Harness",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.23",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"packageManager": "pnpm@11.22.0",
|
|
7
7
|
"engines": {
|