plugin-agent-orchestrator 1.0.16 → 1.0.18
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/dist/client/AgentRunsTab.d.ts +2 -0
- package/dist/client/HarnessProfilesTab.d.ts +2 -0
- package/dist/client/index.js +1 -1
- package/dist/client/skill-hub/components/LoopSettings.d.ts +2 -0
- package/dist/client/skill-hub/index.d.ts +2 -1
- package/dist/client/skill-hub/tools/InteractionSchemasProvider.d.ts +1 -14
- package/dist/client/skill-hub/tools/loopTemplates.d.ts +22 -0
- package/dist/client/skill-hub/tools/registerSkillLoopCards.d.ts +1 -0
- package/dist/client/tools/PlanApprovalCard.d.ts +3 -0
- package/dist/client/tools/registerOrchestratorCards.d.ts +1 -0
- package/dist/externalVersion.js +6 -6
- package/dist/server/collections/agent-harness-profiles.d.ts +2 -0
- package/dist/server/collections/agent-harness-profiles.js +89 -0
- package/dist/server/collections/agent-loop-events.d.ts +2 -0
- package/dist/server/collections/agent-loop-events.js +101 -0
- package/dist/server/collections/agent-loop-runs.d.ts +2 -0
- package/dist/server/collections/agent-loop-runs.js +188 -0
- package/dist/server/collections/agent-loop-steps.d.ts +2 -0
- package/dist/server/collections/agent-loop-steps.js +174 -0
- package/dist/server/collections/orchestrator-config.js +7 -0
- package/dist/server/collections/skill-executions.js +12 -0
- package/dist/server/collections/skill-loop-configs.d.ts +3 -0
- package/dist/server/collections/skill-loop-configs.js +94 -0
- package/dist/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.d.ts +7 -0
- package/dist/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.js +55 -0
- package/dist/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.d.ts +12 -0
- package/dist/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.js +162 -0
- package/dist/server/plugin.d.ts +2 -0
- package/dist/server/plugin.js +13 -0
- package/dist/server/resources/agent-loop.d.ts +3 -0
- package/dist/server/resources/agent-loop.js +205 -0
- package/dist/server/services/AgentHarness.d.ts +42 -0
- package/dist/server/services/AgentHarness.js +565 -0
- package/dist/server/services/AgentLoopController.d.ts +205 -0
- package/dist/server/services/AgentLoopController.js +940 -0
- package/dist/server/services/AgentLoopRepository.d.ts +20 -0
- package/dist/server/services/AgentLoopRepository.js +210 -0
- package/dist/server/services/AgentLoopService.d.ts +149 -0
- package/dist/server/services/AgentLoopService.js +133 -0
- package/dist/server/services/AgentPlanValidator.d.ts +4 -0
- package/dist/server/services/AgentPlanValidator.js +99 -0
- package/dist/server/services/AgentPlannerService.d.ts +8 -0
- package/dist/server/services/AgentPlannerService.js +119 -0
- package/dist/server/services/AgentRegistryService.d.ts +13 -0
- package/dist/server/services/AgentRegistryService.js +178 -0
- package/dist/server/services/ExecutionSpanService.d.ts +2 -0
- package/dist/server/skill-hub/plugin.d.ts +3 -0
- package/dist/server/skill-hub/plugin.js +137 -54
- package/dist/server/tools/agent-loop.d.ts +235 -0
- package/dist/server/tools/agent-loop.js +406 -0
- package/dist/server/tools/delegate-task.js +37 -350
- package/dist/server/tools/orchestrator-plan.d.ts +205 -0
- package/dist/server/tools/orchestrator-plan.js +291 -0
- package/dist/server/tools/skill-execute.js +2 -0
- package/package.json +2 -2
- package/src/client/AgentRunsTab.tsx +764 -0
- package/src/client/HarnessProfilesTab.tsx +247 -0
- package/src/client/OrchestratorSettings.tsx +40 -2
- package/src/client/RulesTab.tsx +103 -6
- package/src/client/plugin.tsx +27 -54
- package/src/client/skill-hub/components/LoopSettings.tsx +331 -0
- package/src/client/skill-hub/index.tsx +51 -75
- package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +56 -16
- package/src/client/skill-hub/tools/SkillHubCard.tsx +35 -4
- package/src/client/skill-hub/tools/loopTemplates.ts +52 -0
- package/src/client/skill-hub/tools/registerSkillLoopCards.ts +58 -0
- package/src/client/tools/PlanApprovalCard.tsx +175 -0
- package/src/client/tools/registerOrchestratorCards.ts +7 -0
- package/src/server/collections/agent-harness-profiles.ts +59 -0
- package/src/server/collections/agent-loop-events.ts +71 -0
- package/src/server/collections/agent-loop-runs.ts +158 -0
- package/src/server/collections/agent-loop-steps.ts +144 -0
- package/src/server/collections/orchestrator-config.ts +7 -0
- package/src/server/collections/skill-executions.ts +63 -51
- package/src/server/collections/skill-loop-configs.ts +65 -0
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +30 -0
- package/src/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.ts +142 -0
- package/src/server/plugin.ts +15 -0
- package/src/server/resources/agent-loop.ts +183 -0
- package/src/server/services/AgentHarness.ts +663 -0
- package/src/server/services/AgentLoopController.ts +1128 -0
- package/src/server/services/AgentLoopRepository.ts +194 -0
- package/src/server/services/AgentLoopService.ts +161 -0
- package/src/server/services/AgentPlanValidator.ts +73 -0
- package/src/server/services/AgentPlannerService.ts +93 -0
- package/src/server/services/AgentRegistryService.ts +169 -0
- package/src/server/services/ExecutionSpanService.ts +2 -0
- package/src/server/skill-hub/plugin.ts +881 -771
- package/src/server/tools/agent-loop.ts +399 -0
- package/src/server/tools/delegate-task.ts +48 -463
- package/src/server/tools/orchestrator-plan.ts +279 -0
- package/src/server/tools/skill-execute.ts +68 -64
|
@@ -32,11 +32,9 @@ __export(delegate_task_exports, {
|
|
|
32
32
|
module.exports = __toCommonJS(delegate_task_exports);
|
|
33
33
|
var import_zod = require("zod");
|
|
34
34
|
var import_crypto = require("crypto");
|
|
35
|
-
var import_prebuilt = require("@langchain/langgraph/prebuilt");
|
|
36
|
-
var import_tools = require("@langchain/core/tools");
|
|
37
|
-
var import_messages = require("@langchain/core/messages");
|
|
38
35
|
var import_ExecutionSpanService = require("../services/ExecutionSpanService");
|
|
39
36
|
const ORCHESTRATOR_DEPTH_KEY = "__orchestratorDepth";
|
|
37
|
+
const ORCHESTRATOR_PATH_KEY = "__orchestratorPath";
|
|
40
38
|
const MAX_DISPATCH_CONCURRENCY = 5;
|
|
41
39
|
const MAX_DISPATCH_TASKS = 20;
|
|
42
40
|
const MAX_TOOL_NAME_LENGTH = 64;
|
|
@@ -96,7 +94,7 @@ function createDelegateToolOptions(plugin, options) {
|
|
|
96
94
|
return {
|
|
97
95
|
scope: "CUSTOM",
|
|
98
96
|
execution: "backend",
|
|
99
|
-
defaultPermission: "
|
|
97
|
+
defaultPermission: "ASK",
|
|
100
98
|
silence: false,
|
|
101
99
|
introduction: {
|
|
102
100
|
title: `[${leaderUsername}] ${subAgentEmployee.nickname || subAgentUsername}${legacyAlias ? " (legacy)" : ""}`,
|
|
@@ -205,7 +203,7 @@ ${subAgentList}`
|
|
|
205
203
|
return {
|
|
206
204
|
scope: "CUSTOM",
|
|
207
205
|
execution: "backend",
|
|
208
|
-
defaultPermission: "
|
|
206
|
+
defaultPermission: "ASK",
|
|
209
207
|
silence: false,
|
|
210
208
|
introduction: {
|
|
211
209
|
title: `[${leaderUsername}] Dispatch sub-agents`,
|
|
@@ -554,7 +552,6 @@ function invalidateDelegateToolsCache() {
|
|
|
554
552
|
registeredDelegateNamesByPlugin = /* @__PURE__ */ new WeakMap();
|
|
555
553
|
}
|
|
556
554
|
async function invokeDelegateTask(ctx, plugin, options) {
|
|
557
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
558
555
|
const {
|
|
559
556
|
leaderUsername,
|
|
560
557
|
subAgentUsername,
|
|
@@ -573,7 +570,9 @@ async function invokeDelegateTask(ctx, plugin, options) {
|
|
|
573
570
|
} = options;
|
|
574
571
|
const ctxSnapshot = captureCtxSnapshot(ctx);
|
|
575
572
|
const currentDepth = ctx[ORCHESTRATOR_DEPTH_KEY] ?? 0;
|
|
576
|
-
|
|
573
|
+
const currentPath = ctx[ORCHESTRATOR_PATH_KEY] ?? [leaderUsername];
|
|
574
|
+
if (currentPath.includes(subAgentUsername)) {
|
|
575
|
+
const loopChain = [...currentPath, subAgentUsername].join(" -> ");
|
|
577
576
|
await logDelegation(ctx, plugin, {
|
|
578
577
|
leaderUsername,
|
|
579
578
|
subAgentUsername,
|
|
@@ -584,275 +583,16 @@ async function invokeDelegateTask(ctx, plugin, options) {
|
|
|
584
583
|
status: "error",
|
|
585
584
|
depth: currentDepth,
|
|
586
585
|
durationMs: 0,
|
|
587
|
-
error: `
|
|
586
|
+
error: `Circular delegation detected: ${loopChain}.`,
|
|
588
587
|
snapshot: ctxSnapshot
|
|
589
588
|
});
|
|
590
589
|
return {
|
|
591
590
|
status: "error",
|
|
592
|
-
content: `
|
|
591
|
+
content: `Circular delegation detected: ${loopChain}. Execution aborted to prevent infinite reasoning loops.`
|
|
593
592
|
};
|
|
594
593
|
}
|
|
595
|
-
|
|
596
|
-
const upstreamTraceContext = (0, import_ExecutionSpanService.getOrchestratorTraceContext)(ctx);
|
|
597
|
-
const rootRunId = providedRootRunId || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.rootRunId) || createRootRunId(`${leaderUsername}:${subAgentUsername}`);
|
|
598
|
-
const parentSpanId = providedParentSpanId || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.spanId) || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.parentSpanId);
|
|
599
|
-
const startTime = Date.now();
|
|
600
|
-
const trace = [
|
|
601
|
-
{
|
|
602
|
-
type: "start",
|
|
603
|
-
at: nowIso(),
|
|
604
|
-
title: `Delegation started: ${leaderUsername} -> ${subAgentUsername}`,
|
|
605
|
-
content: task
|
|
606
|
-
}
|
|
607
|
-
];
|
|
608
|
-
const executionSpan = await spanService.create({
|
|
609
|
-
rootRunId,
|
|
610
|
-
parentSpanId,
|
|
611
|
-
type: "sub_agent",
|
|
612
|
-
status: "running",
|
|
613
|
-
leaderUsername,
|
|
614
|
-
employeeUsername: subAgentUsername,
|
|
615
|
-
title: `Delegation: ${leaderUsername} -> ${subAgentUsername}`,
|
|
616
|
-
input: { task, context },
|
|
617
|
-
metadata: {
|
|
618
|
-
depth: currentDepth,
|
|
619
|
-
maxDepth,
|
|
620
|
-
toolName,
|
|
621
|
-
recursionLimit,
|
|
622
|
-
llmOverride: llmService && model ? { llmService, model } : void 0
|
|
623
|
-
},
|
|
624
|
-
userId: ctxSnapshot.userId
|
|
625
|
-
});
|
|
626
|
-
const executionSpanId = (executionSpan == null ? void 0 : executionSpan.id) ? String(executionSpan.id) : void 0;
|
|
627
|
-
const logRecord = await logDelegation(ctx, plugin, {
|
|
628
|
-
leaderUsername,
|
|
629
|
-
subAgentUsername,
|
|
630
|
-
toolName,
|
|
631
|
-
task,
|
|
632
|
-
context,
|
|
633
|
-
result: "",
|
|
634
|
-
status: "running",
|
|
635
|
-
depth: currentDepth,
|
|
636
|
-
durationMs: 0,
|
|
637
|
-
trace,
|
|
638
|
-
snapshot: ctxSnapshot
|
|
639
|
-
});
|
|
640
|
-
if (executionSpanId && (logRecord == null ? void 0 : logRecord.id)) {
|
|
641
|
-
await spanService.update(executionSpanId, { orchestratorLogId: logRecord.id });
|
|
642
|
-
}
|
|
643
|
-
try {
|
|
644
|
-
const aiPlugin = ctx.app.pm.get("ai");
|
|
645
|
-
if (!aiPlugin) {
|
|
646
|
-
throw new Error("Plugin AI is not installed or enabled");
|
|
647
|
-
}
|
|
648
|
-
let modelSettings = hasModelSettings(subAgentEmployee.modelSettings) ? subAgentEmployee.modelSettings : void 0;
|
|
649
|
-
if (llmService && model) {
|
|
650
|
-
modelSettings = { llmService, model };
|
|
651
|
-
}
|
|
652
|
-
if (!hasModelSettings(modelSettings)) {
|
|
653
|
-
const leaderEmployee = await plugin.db.getRepository("aiEmployees").findOne({
|
|
654
|
-
filter: { username: leaderUsername }
|
|
655
|
-
});
|
|
656
|
-
const dynamicModel = (_c = (_b = (_a = ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.values) == null ? void 0 : _c.model;
|
|
657
|
-
modelSettings = hasModelSettings(leaderEmployee == null ? void 0 : leaderEmployee.modelSettings) ? leaderEmployee.modelSettings : hasModelSettings(dynamicModel) ? dynamicModel : void 0;
|
|
658
|
-
if (!hasModelSettings(modelSettings)) {
|
|
659
|
-
throw new Error(
|
|
660
|
-
`Sub-agent "${subAgentUsername}" has no LLM model configured (and leader fallback failed). Please configure a model in the Orchestrator Config or AI Employee settings.`
|
|
661
|
-
);
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
const { provider } = await aiPlugin.aiManager.getLLMService({
|
|
665
|
-
llmService: modelSettings.llmService,
|
|
666
|
-
model: modelSettings.model
|
|
667
|
-
});
|
|
668
|
-
const chatModel = provider.createModel();
|
|
669
|
-
const coreToolsManager = ctx.app.aiManager.toolsManager;
|
|
670
|
-
const allTools = await coreToolsManager.listTools();
|
|
671
|
-
const employeeSkills = (((_d = subAgentEmployee.skillSettings) == null ? void 0 : _d.skills) ?? []).map(
|
|
672
|
-
(s) => typeof s === "string" ? { name: s, autoCall: false } : { name: s == null ? void 0 : s.name, autoCall: (s == null ? void 0 : s.autoCall) === true }
|
|
673
|
-
).filter((s) => Boolean(s.name));
|
|
674
|
-
const employeeSkillMap = new Map(employeeSkills.map((skill) => [skill.name, skill]));
|
|
675
|
-
const langchainTools = [];
|
|
676
|
-
for (const toolEntry of allTools) {
|
|
677
|
-
const entryName = toolEntry.definition.name;
|
|
678
|
-
if (!entryName) continue;
|
|
679
|
-
const employeeSkill = employeeSkillMap.get(entryName);
|
|
680
|
-
if (!employeeSkill || isDelegateToolName(plugin, entryName) || employeeSkill.autoCall !== true || toolEntry.defaultPermission !== "ALLOW") {
|
|
681
|
-
continue;
|
|
682
|
-
}
|
|
683
|
-
langchainTools.push(
|
|
684
|
-
new import_tools.DynamicStructuredTool({
|
|
685
|
-
name: entryName.replace(/[^a-zA-Z0-9_-]/g, "_"),
|
|
686
|
-
description: toolEntry.definition.description || entryName,
|
|
687
|
-
schema: toolEntry.definition.schema || import_zod.z.object({}),
|
|
688
|
-
func: async (toolArgs) => {
|
|
689
|
-
var _a2;
|
|
690
|
-
const invokeCtx = Object.create(ctx);
|
|
691
|
-
invokeCtx[ORCHESTRATOR_DEPTH_KEY] = currentDepth + 1;
|
|
692
|
-
const toolStartedAt = Date.now();
|
|
693
|
-
const isSkillHubTool = entryName === "skill_hub_execute" || entryName.startsWith("skill_hub_");
|
|
694
|
-
const toolSpan = await spanService.create({
|
|
695
|
-
rootRunId,
|
|
696
|
-
parentSpanId: executionSpanId,
|
|
697
|
-
type: isSkillHubTool ? "skill" : "tool",
|
|
698
|
-
status: "running",
|
|
699
|
-
leaderUsername,
|
|
700
|
-
employeeUsername: subAgentUsername,
|
|
701
|
-
toolName: toolEntry.definition.name,
|
|
702
|
-
title: isSkillHubTool ? `Skill: ${toolEntry.definition.name}` : `Tool: ${toolEntry.definition.name}`,
|
|
703
|
-
input: toolArgs,
|
|
704
|
-
metadata: {
|
|
705
|
-
depth: currentDepth + 1,
|
|
706
|
-
toolCallId: `orch-${toolCallId}`,
|
|
707
|
-
defaultPermission: toolEntry.defaultPermission
|
|
708
|
-
},
|
|
709
|
-
userId: ctxSnapshot.userId
|
|
710
|
-
});
|
|
711
|
-
const toolSpanId = (toolSpan == null ? void 0 : toolSpan.id) ? String(toolSpan.id) : void 0;
|
|
712
|
-
(0, import_ExecutionSpanService.setOrchestratorTraceContext)(invokeCtx, {
|
|
713
|
-
rootRunId,
|
|
714
|
-
spanId: toolSpanId,
|
|
715
|
-
parentSpanId: executionSpanId,
|
|
716
|
-
toolCallId: `orch-${toolCallId}`,
|
|
717
|
-
leaderUsername,
|
|
718
|
-
employeeUsername: subAgentUsername,
|
|
719
|
-
toolName: toolEntry.definition.name
|
|
720
|
-
});
|
|
721
|
-
trace.push({
|
|
722
|
-
type: "tool_call",
|
|
723
|
-
at: nowIso(),
|
|
724
|
-
title: `Calling tool: ${toolEntry.definition.name}`,
|
|
725
|
-
toolName: toolEntry.definition.name,
|
|
726
|
-
args: toolArgs
|
|
727
|
-
});
|
|
728
|
-
try {
|
|
729
|
-
const res = await toolEntry.invoke(invokeCtx, toolArgs, `orch-${toolCallId}`);
|
|
730
|
-
const output = truncateText((res == null ? void 0 : res.content) ?? (res == null ? void 0 : res.result) ?? res, 5e4);
|
|
731
|
-
trace.push({
|
|
732
|
-
type: "tool_result",
|
|
733
|
-
at: nowIso(),
|
|
734
|
-
title: `Tool finished: ${toolEntry.definition.name}`,
|
|
735
|
-
toolName: toolEntry.definition.name,
|
|
736
|
-
status: (res == null ? void 0 : res.status) || "success",
|
|
737
|
-
content: truncateText(output, 2e3)
|
|
738
|
-
});
|
|
739
|
-
if ((res == null ? void 0 : res.status) === "error") {
|
|
740
|
-
await spanService.finish(toolSpanId, "error", toolStartedAt, {
|
|
741
|
-
output,
|
|
742
|
-
error: truncateText(res.content || output, 1e4)
|
|
743
|
-
});
|
|
744
|
-
throw new Error(`Tool <${toolEntry.definition.name}> failed: ${res.content}`);
|
|
745
|
-
}
|
|
746
|
-
await spanService.finish(toolSpanId, "success", toolStartedAt, {
|
|
747
|
-
output,
|
|
748
|
-
skillExecutionId: ((_a2 = res == null ? void 0 : res.result) == null ? void 0 : _a2.execId) || (res == null ? void 0 : res.execId)
|
|
749
|
-
});
|
|
750
|
-
return typeof (res == null ? void 0 : res.content) === "string" ? res.content : JSON.stringify(res);
|
|
751
|
-
} catch (e) {
|
|
752
|
-
trace.push({
|
|
753
|
-
type: "tool_error",
|
|
754
|
-
at: nowIso(),
|
|
755
|
-
title: `Tool failed: ${toolEntry.definition.name}`,
|
|
756
|
-
toolName: toolEntry.definition.name,
|
|
757
|
-
status: "error",
|
|
758
|
-
content: e.message
|
|
759
|
-
});
|
|
760
|
-
await spanService.finish(toolSpanId, "error", toolStartedAt, {
|
|
761
|
-
error: truncateText(e.message, 1e4)
|
|
762
|
-
});
|
|
763
|
-
throw e;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
})
|
|
767
|
-
);
|
|
768
|
-
}
|
|
769
|
-
const abortController = new AbortController();
|
|
770
|
-
const executor = (0, import_prebuilt.createReactAgent)({
|
|
771
|
-
llm: chatModel,
|
|
772
|
-
tools: langchainTools
|
|
773
|
-
});
|
|
774
|
-
let systemPrompt = ((_e = subAgentEmployee.chatSettings) == null ? void 0 : _e.systemPrompt) || subAgentEmployee.bio || `You are an AI assistant named "${subAgentEmployee.nickname || subAgentUsername}". ${subAgentEmployee.about || ""}`;
|
|
775
|
-
try {
|
|
776
|
-
const kbPlugin = ctx.app.pm.get("plugin-knowledge-base");
|
|
777
|
-
if (kbPlugin == null ? void 0 : kbPlugin.sessionContext) {
|
|
778
|
-
const sessionId = ((_h = (_g = (_f = ctx.action) == null ? void 0 : _f.params) == null ? void 0 : _g.values) == null ? void 0 : _h.sessionId) || ((_j = (_i = ctx.action) == null ? void 0 : _i.params) == null ? void 0 : _j.sessionId) || ((_k = ctx.state) == null ? void 0 : _k.sessionId);
|
|
779
|
-
const contextSummary = await kbPlugin.sessionContext.buildSummary(
|
|
780
|
-
{ rootRunId, ...sessionId ? { sessionId } : {} },
|
|
781
|
-
6e3
|
|
782
|
-
);
|
|
783
|
-
if (contextSummary) {
|
|
784
|
-
systemPrompt += `
|
|
785
|
-
|
|
786
|
-
<shared_context>
|
|
787
|
-
The following context was shared by other agents in this workflow. Use it to avoid redundant work:
|
|
788
|
-
${contextSummary}
|
|
789
|
-
</shared_context>`;
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
} catch (e) {
|
|
793
|
-
(_m = (_l = ctx.app.log) == null ? void 0 : _l.debug) == null ? void 0 : _m.call(_l, `[AgentOrchestrator] Shared context injection skipped: ${e.message}`);
|
|
794
|
-
}
|
|
795
|
-
const combinedTask = context ? `Task: ${task}
|
|
796
|
-
|
|
797
|
-
Context Provided:
|
|
798
|
-
${context}` : `Task: ${task}`;
|
|
799
|
-
const effectiveRecursionLimit = Number.isFinite(recursionLimit) && recursionLimit > 0 ? recursionLimit : 50;
|
|
800
|
-
const invokePromise = executeAgent(
|
|
801
|
-
executor,
|
|
802
|
-
systemPrompt,
|
|
803
|
-
combinedTask,
|
|
804
|
-
abortController.signal,
|
|
805
|
-
effectiveRecursionLimit
|
|
806
|
-
);
|
|
807
|
-
const timeoutHandle = createTimeout(timeout, subAgentUsername, abortController);
|
|
808
|
-
let result;
|
|
809
|
-
try {
|
|
810
|
-
result = await Promise.race([invokePromise, timeoutHandle.promise]);
|
|
811
|
-
} finally {
|
|
812
|
-
timeoutHandle.cancel();
|
|
813
|
-
}
|
|
814
|
-
const content = result.content || "Sub-agent completed the task but produced no output.";
|
|
815
|
-
trace.push({
|
|
816
|
-
type: "finish",
|
|
817
|
-
at: nowIso(),
|
|
818
|
-
title: `Delegation finished: ${subAgentUsername}`,
|
|
819
|
-
status: "success",
|
|
820
|
-
content: truncateText(content, 2e3)
|
|
821
|
-
});
|
|
822
|
-
await logDelegation(ctx, plugin, {
|
|
823
|
-
id: logRecord == null ? void 0 : logRecord.id,
|
|
824
|
-
leaderUsername,
|
|
825
|
-
subAgentUsername,
|
|
826
|
-
toolName,
|
|
827
|
-
task,
|
|
828
|
-
context,
|
|
829
|
-
result: content,
|
|
830
|
-
status: "success",
|
|
831
|
-
depth: currentDepth,
|
|
832
|
-
durationMs: Date.now() - startTime,
|
|
833
|
-
trace,
|
|
834
|
-
messages: result.messages,
|
|
835
|
-
snapshot: ctxSnapshot
|
|
836
|
-
});
|
|
837
|
-
await spanService.finish(executionSpanId, "success", startTime, {
|
|
838
|
-
output: content,
|
|
839
|
-
metadata: {
|
|
840
|
-
depth: currentDepth,
|
|
841
|
-
maxDepth,
|
|
842
|
-
toolName,
|
|
843
|
-
recursionLimit,
|
|
844
|
-
messages: result.messages,
|
|
845
|
-
traceCount: trace.length
|
|
846
|
-
}
|
|
847
|
-
});
|
|
848
|
-
return {
|
|
849
|
-
status: "success",
|
|
850
|
-
content
|
|
851
|
-
};
|
|
852
|
-
} catch (e) {
|
|
853
|
-
plugin.app.log.error(`[AgentOrchestrator] Sub-agent ${subAgentUsername} failed`, e);
|
|
594
|
+
if (currentDepth >= maxDepth) {
|
|
854
595
|
await logDelegation(ctx, plugin, {
|
|
855
|
-
id: logRecord == null ? void 0 : logRecord.id,
|
|
856
596
|
leaderUsername,
|
|
857
597
|
subAgentUsername,
|
|
858
598
|
toolName,
|
|
@@ -861,37 +601,40 @@ ${context}` : `Task: ${task}`;
|
|
|
861
601
|
result: "",
|
|
862
602
|
status: "error",
|
|
863
603
|
depth: currentDepth,
|
|
864
|
-
durationMs:
|
|
865
|
-
error:
|
|
866
|
-
trace: [
|
|
867
|
-
...trace,
|
|
868
|
-
{
|
|
869
|
-
type: "error",
|
|
870
|
-
at: nowIso(),
|
|
871
|
-
title: `Delegation failed: ${subAgentUsername}`,
|
|
872
|
-
status: "error",
|
|
873
|
-
content: e.message
|
|
874
|
-
}
|
|
875
|
-
],
|
|
604
|
+
durationMs: 0,
|
|
605
|
+
error: `Delegation depth limit reached (${currentDepth}/${maxDepth}).`,
|
|
876
606
|
snapshot: ctxSnapshot
|
|
877
|
-
}).catch((logErr) => {
|
|
878
|
-
plugin.app.log.warn("[AgentOrchestrator] Failed to save error log for delegation", logErr);
|
|
879
|
-
});
|
|
880
|
-
await spanService.finish(executionSpanId, "error", startTime, {
|
|
881
|
-
error: truncateText(e.message, 1e4),
|
|
882
|
-
metadata: {
|
|
883
|
-
depth: currentDepth,
|
|
884
|
-
maxDepth,
|
|
885
|
-
toolName,
|
|
886
|
-
recursionLimit,
|
|
887
|
-
traceCount: trace.length + 1
|
|
888
|
-
}
|
|
889
607
|
});
|
|
890
608
|
return {
|
|
891
609
|
status: "error",
|
|
892
|
-
content: `Sub-agent "${subAgentUsername}"
|
|
610
|
+
content: `Delegation depth limit reached (${currentDepth}/${maxDepth}). Sub-agent "${subAgentUsername}" cannot delegate further.`
|
|
893
611
|
};
|
|
894
612
|
}
|
|
613
|
+
const upstreamTraceContext = (0, import_ExecutionSpanService.getOrchestratorTraceContext)(ctx);
|
|
614
|
+
const rootRunId = providedRootRunId || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.rootRunId) || createRootRunId(`${leaderUsername}:${subAgentUsername}`);
|
|
615
|
+
const parentSpanId = providedParentSpanId || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.spanId) || (upstreamTraceContext == null ? void 0 : upstreamTraceContext.parentSpanId);
|
|
616
|
+
const agentLoopRunId = upstreamTraceContext == null ? void 0 : upstreamTraceContext.agentLoopRunId;
|
|
617
|
+
const agentLoopStepId = upstreamTraceContext == null ? void 0 : upstreamTraceContext.agentLoopStepId;
|
|
618
|
+
return plugin.agentLoopService.harness.runSubAgent(ctx, {
|
|
619
|
+
leaderUsername,
|
|
620
|
+
subAgentUsername,
|
|
621
|
+
subAgentEmployee,
|
|
622
|
+
task,
|
|
623
|
+
context,
|
|
624
|
+
currentDepth,
|
|
625
|
+
currentPath,
|
|
626
|
+
maxDepth,
|
|
627
|
+
timeout,
|
|
628
|
+
toolCallId,
|
|
629
|
+
toolName,
|
|
630
|
+
llmService,
|
|
631
|
+
model,
|
|
632
|
+
recursionLimit,
|
|
633
|
+
rootRunId,
|
|
634
|
+
parentSpanId,
|
|
635
|
+
agentLoopRunId,
|
|
636
|
+
agentLoopStepId
|
|
637
|
+
});
|
|
895
638
|
}
|
|
896
639
|
async function logDelegation(ctx, plugin, data) {
|
|
897
640
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -942,62 +685,6 @@ async function logDelegation(ctx, plugin, data) {
|
|
|
942
685
|
plugin.app.log.warn("[AgentOrchestrator] Failed to log delegation event", e);
|
|
943
686
|
}
|
|
944
687
|
}
|
|
945
|
-
async function executeAgent(executor, systemPrompt, task, signal, recursionLimit = 50) {
|
|
946
|
-
const config = { recursionLimit };
|
|
947
|
-
if (signal) {
|
|
948
|
-
config.signal = signal;
|
|
949
|
-
}
|
|
950
|
-
const finalState = await executor.invoke(
|
|
951
|
-
{
|
|
952
|
-
messages: [new import_messages.SystemMessage(systemPrompt), new import_messages.HumanMessage(task)]
|
|
953
|
-
},
|
|
954
|
-
config
|
|
955
|
-
);
|
|
956
|
-
const messages = (finalState == null ? void 0 : finalState.messages) || [];
|
|
957
|
-
const lastAIMessage = [...messages].reverse().find((m) => m.getType() === "ai");
|
|
958
|
-
if (!lastAIMessage || !lastAIMessage.content) {
|
|
959
|
-
return { content: "", messages: serializeMessages(messages) };
|
|
960
|
-
}
|
|
961
|
-
let content = "";
|
|
962
|
-
if (typeof lastAIMessage.content === "string") {
|
|
963
|
-
content = lastAIMessage.content;
|
|
964
|
-
} else if (Array.isArray(lastAIMessage.content)) {
|
|
965
|
-
content = lastAIMessage.content.map((c) => c.text || JSON.stringify(c)).join("\n");
|
|
966
|
-
} else {
|
|
967
|
-
content = String(lastAIMessage.content);
|
|
968
|
-
}
|
|
969
|
-
return { content, messages: serializeMessages(messages) };
|
|
970
|
-
}
|
|
971
|
-
function serializeMessages(messages) {
|
|
972
|
-
return (messages || []).map((message, index) => {
|
|
973
|
-
const type = typeof message.getType === "function" ? message.getType() : message.type;
|
|
974
|
-
return {
|
|
975
|
-
index,
|
|
976
|
-
type,
|
|
977
|
-
name: message.name,
|
|
978
|
-
content: truncateText(message.content, 1e4),
|
|
979
|
-
toolCalls: message.tool_calls || message.toolCalls || [],
|
|
980
|
-
toolCallId: message.tool_call_id,
|
|
981
|
-
additionalKwargs: message.additional_kwargs,
|
|
982
|
-
responseMetadata: message.response_metadata
|
|
983
|
-
};
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
function createTimeout(ms, agentName, abortController) {
|
|
987
|
-
let timer;
|
|
988
|
-
const promise = new Promise((_resolve, reject) => {
|
|
989
|
-
timer = setTimeout(() => {
|
|
990
|
-
abortController == null ? void 0 : abortController.abort();
|
|
991
|
-
reject(new Error(`Sub-agent "${agentName}" timed out after ${ms / 1e3}s`));
|
|
992
|
-
}, ms);
|
|
993
|
-
});
|
|
994
|
-
return {
|
|
995
|
-
promise,
|
|
996
|
-
cancel: () => {
|
|
997
|
-
if (timer) clearTimeout(timer);
|
|
998
|
-
}
|
|
999
|
-
};
|
|
1000
|
-
}
|
|
1001
688
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1002
689
|
0 && (module.exports = {
|
|
1003
690
|
createDelegateToolsProvider,
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { AgentLoopService } from '../services/AgentLoopService';
|
|
3
|
+
export declare function createOrchestratorPlanTools(plugin: any, service: AgentLoopService): ({
|
|
4
|
+
scope: "CUSTOM";
|
|
5
|
+
execution: "backend";
|
|
6
|
+
defaultPermission: "ALLOW";
|
|
7
|
+
introduction: {
|
|
8
|
+
title: string;
|
|
9
|
+
about: string;
|
|
10
|
+
};
|
|
11
|
+
definition: {
|
|
12
|
+
name: string;
|
|
13
|
+
description: string;
|
|
14
|
+
schema: z.ZodObject<{
|
|
15
|
+
goal: z.ZodString;
|
|
16
|
+
runId: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
|
|
17
|
+
leaderUsername: z.ZodOptional<z.ZodString>;
|
|
18
|
+
sessionId: z.ZodOptional<z.ZodString>;
|
|
19
|
+
messageId: z.ZodOptional<z.ZodString>;
|
|
20
|
+
harnessTag: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
21
|
+
targetAgent: z.ZodOptional<z.ZodString>;
|
|
22
|
+
plannerModel: z.ZodOptional<z.ZodString>;
|
|
23
|
+
policy: z.ZodOptional<z.ZodObject<{
|
|
24
|
+
maxIterations: z.ZodOptional<z.ZodNumber>;
|
|
25
|
+
maxStepAttempts: z.ZodOptional<z.ZodNumber>;
|
|
26
|
+
allowReplan: z.ZodOptional<z.ZodBoolean>;
|
|
27
|
+
requireVerification: z.ZodOptional<z.ZodBoolean>;
|
|
28
|
+
stopOnApprovalRequired: z.ZodOptional<z.ZodBoolean>;
|
|
29
|
+
}, "strip", z.ZodTypeAny, {
|
|
30
|
+
maxIterations?: number;
|
|
31
|
+
maxStepAttempts?: number;
|
|
32
|
+
allowReplan?: boolean;
|
|
33
|
+
requireVerification?: boolean;
|
|
34
|
+
stopOnApprovalRequired?: boolean;
|
|
35
|
+
}, {
|
|
36
|
+
maxIterations?: number;
|
|
37
|
+
maxStepAttempts?: number;
|
|
38
|
+
allowReplan?: boolean;
|
|
39
|
+
requireVerification?: boolean;
|
|
40
|
+
stopOnApprovalRequired?: boolean;
|
|
41
|
+
}>>;
|
|
42
|
+
metadata: z.ZodOptional<z.ZodAny>;
|
|
43
|
+
plan: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
44
|
+
id: z.ZodOptional<z.ZodString>;
|
|
45
|
+
key: z.ZodOptional<z.ZodString>;
|
|
46
|
+
planKey: z.ZodOptional<z.ZodString>;
|
|
47
|
+
parentStepId: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
|
|
48
|
+
title: z.ZodOptional<z.ZodString>;
|
|
49
|
+
description: z.ZodOptional<z.ZodString>;
|
|
50
|
+
type: z.ZodOptional<z.ZodEnum<["reasoning", "skill", "tool", "sub_agent", "verification"]>>;
|
|
51
|
+
target: z.ZodOptional<z.ZodString>;
|
|
52
|
+
input: z.ZodOptional<z.ZodAny>;
|
|
53
|
+
dependsOn: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
54
|
+
dependencyPolicy: z.ZodOptional<z.ZodEnum<["require_success", "allow_skipped"]>>;
|
|
55
|
+
maxAttempts: z.ZodOptional<z.ZodNumber>;
|
|
56
|
+
metadata: z.ZodOptional<z.ZodAny>;
|
|
57
|
+
}, "strip", z.ZodTypeAny, {
|
|
58
|
+
type?: "reasoning" | "skill" | "tool" | "sub_agent" | "verification";
|
|
59
|
+
key?: string;
|
|
60
|
+
title?: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
metadata?: any;
|
|
63
|
+
input?: any;
|
|
64
|
+
planKey?: string;
|
|
65
|
+
target?: string;
|
|
66
|
+
dependsOn?: string[];
|
|
67
|
+
id?: string;
|
|
68
|
+
parentStepId?: string | number;
|
|
69
|
+
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
70
|
+
maxAttempts?: number;
|
|
71
|
+
}, {
|
|
72
|
+
type?: "reasoning" | "skill" | "tool" | "sub_agent" | "verification";
|
|
73
|
+
key?: string;
|
|
74
|
+
title?: string;
|
|
75
|
+
description?: string;
|
|
76
|
+
metadata?: any;
|
|
77
|
+
input?: any;
|
|
78
|
+
planKey?: string;
|
|
79
|
+
target?: string;
|
|
80
|
+
dependsOn?: string[];
|
|
81
|
+
id?: string;
|
|
82
|
+
parentStepId?: string | number;
|
|
83
|
+
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
84
|
+
maxAttempts?: number;
|
|
85
|
+
}>, "many">>;
|
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
|
87
|
+
leaderUsername?: string;
|
|
88
|
+
metadata?: any;
|
|
89
|
+
runId?: string | number;
|
|
90
|
+
sessionId?: string;
|
|
91
|
+
goal?: string;
|
|
92
|
+
messageId?: string;
|
|
93
|
+
policy?: {
|
|
94
|
+
maxIterations?: number;
|
|
95
|
+
maxStepAttempts?: number;
|
|
96
|
+
allowReplan?: boolean;
|
|
97
|
+
requireVerification?: boolean;
|
|
98
|
+
stopOnApprovalRequired?: boolean;
|
|
99
|
+
};
|
|
100
|
+
harnessTag?: string;
|
|
101
|
+
targetAgent?: string;
|
|
102
|
+
plannerModel?: string;
|
|
103
|
+
plan?: {
|
|
104
|
+
type?: "reasoning" | "skill" | "tool" | "sub_agent" | "verification";
|
|
105
|
+
key?: string;
|
|
106
|
+
title?: string;
|
|
107
|
+
description?: string;
|
|
108
|
+
metadata?: any;
|
|
109
|
+
input?: any;
|
|
110
|
+
planKey?: string;
|
|
111
|
+
target?: string;
|
|
112
|
+
dependsOn?: string[];
|
|
113
|
+
id?: string;
|
|
114
|
+
parentStepId?: string | number;
|
|
115
|
+
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
116
|
+
maxAttempts?: number;
|
|
117
|
+
}[];
|
|
118
|
+
}, {
|
|
119
|
+
leaderUsername?: string;
|
|
120
|
+
metadata?: any;
|
|
121
|
+
runId?: string | number;
|
|
122
|
+
sessionId?: string;
|
|
123
|
+
goal?: string;
|
|
124
|
+
messageId?: string;
|
|
125
|
+
policy?: {
|
|
126
|
+
maxIterations?: number;
|
|
127
|
+
maxStepAttempts?: number;
|
|
128
|
+
allowReplan?: boolean;
|
|
129
|
+
requireVerification?: boolean;
|
|
130
|
+
stopOnApprovalRequired?: boolean;
|
|
131
|
+
};
|
|
132
|
+
harnessTag?: string;
|
|
133
|
+
targetAgent?: string;
|
|
134
|
+
plannerModel?: string;
|
|
135
|
+
plan?: {
|
|
136
|
+
type?: "reasoning" | "skill" | "tool" | "sub_agent" | "verification";
|
|
137
|
+
key?: string;
|
|
138
|
+
title?: string;
|
|
139
|
+
description?: string;
|
|
140
|
+
metadata?: any;
|
|
141
|
+
input?: any;
|
|
142
|
+
planKey?: string;
|
|
143
|
+
target?: string;
|
|
144
|
+
dependsOn?: string[];
|
|
145
|
+
id?: string;
|
|
146
|
+
parentStepId?: string | number;
|
|
147
|
+
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
148
|
+
maxAttempts?: number;
|
|
149
|
+
}[];
|
|
150
|
+
}>;
|
|
151
|
+
};
|
|
152
|
+
invoke: (ctx: any, args: any) => Promise<{
|
|
153
|
+
status: "success" | "error";
|
|
154
|
+
content: string;
|
|
155
|
+
}>;
|
|
156
|
+
} | {
|
|
157
|
+
scope: "CUSTOM";
|
|
158
|
+
execution: "backend";
|
|
159
|
+
defaultPermission: "ALLOW";
|
|
160
|
+
introduction: {
|
|
161
|
+
title: string;
|
|
162
|
+
about: string;
|
|
163
|
+
};
|
|
164
|
+
definition: {
|
|
165
|
+
name: string;
|
|
166
|
+
description: string;
|
|
167
|
+
schema: z.ZodObject<{
|
|
168
|
+
runId: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
|
|
169
|
+
}, "strip", z.ZodTypeAny, {
|
|
170
|
+
runId?: string | number;
|
|
171
|
+
}, {
|
|
172
|
+
runId?: string | number;
|
|
173
|
+
}>;
|
|
174
|
+
};
|
|
175
|
+
invoke: (_ctx: any, args: any) => Promise<{
|
|
176
|
+
status: "success" | "error";
|
|
177
|
+
content: string;
|
|
178
|
+
}>;
|
|
179
|
+
} | {
|
|
180
|
+
scope: "CUSTOM";
|
|
181
|
+
execution: "backend";
|
|
182
|
+
defaultPermission: "ASK";
|
|
183
|
+
introduction: {
|
|
184
|
+
title: string;
|
|
185
|
+
about: string;
|
|
186
|
+
};
|
|
187
|
+
definition: {
|
|
188
|
+
name: string;
|
|
189
|
+
description: string;
|
|
190
|
+
schema: z.ZodObject<{
|
|
191
|
+
runId: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
|
|
192
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
193
|
+
}, "strip", z.ZodTypeAny, {
|
|
194
|
+
runId?: string | number;
|
|
195
|
+
reason?: string;
|
|
196
|
+
}, {
|
|
197
|
+
runId?: string | number;
|
|
198
|
+
reason?: string;
|
|
199
|
+
}>;
|
|
200
|
+
};
|
|
201
|
+
invoke: (ctx: any, args: any) => Promise<{
|
|
202
|
+
status: "success" | "error";
|
|
203
|
+
content: string;
|
|
204
|
+
}>;
|
|
205
|
+
})[];
|