plugin-agent-orchestrator 1.0.19 → 1.0.21
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/hooks/useRunEventStream.d.ts +22 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -1
- package/dist/externalVersion.js +6 -6
- package/dist/server/collections/agent-execution-spans.js +24 -0
- package/dist/server/collections/agent-loop-runs.js +36 -0
- package/dist/server/collections/orchestrator-config.js +14 -0
- package/dist/server/migrations/20260601000000-add-token-fields.d.ts +7 -0
- package/dist/server/migrations/20260601000000-add-token-fields.js +101 -0
- package/dist/server/plugin.js +47 -0
- package/dist/server/resources/agent-loop.js +33 -25
- package/dist/server/resources/tracing.js +5 -8
- package/dist/server/services/AgentHarness.d.ts +2 -0
- package/dist/server/services/AgentHarness.js +56 -90
- package/dist/server/services/AgentLoopController.d.ts +33 -20
- package/dist/server/services/AgentLoopController.js +164 -125
- package/dist/server/services/AgentLoopRepository.js +16 -34
- package/dist/server/services/AgentLoopService.d.ts +28 -18
- package/dist/server/services/AgentLoopService.js +7 -1
- package/dist/server/services/AgentPlannerService.js +5 -25
- package/dist/server/services/AgentRegistryService.d.ts +8 -0
- package/dist/server/services/AgentRegistryService.js +34 -24
- package/dist/server/services/CircuitBreaker.d.ts +40 -0
- package/dist/server/services/CircuitBreaker.js +120 -0
- package/dist/server/services/ContextAggregator.d.ts +45 -0
- package/dist/server/services/ContextAggregator.js +201 -0
- package/dist/server/services/ExecutionSpanService.js +2 -5
- package/dist/server/services/RunEventBus.d.ts +9 -0
- package/dist/server/services/RunEventBus.js +73 -0
- package/dist/server/services/TokenTracker.d.ts +62 -0
- package/dist/server/services/TokenTracker.js +173 -0
- package/dist/server/skill-hub/plugin.js +6 -6
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +6 -6
- package/dist/server/tools/agent-loop.d.ts +8 -8
- package/dist/server/tools/agent-loop.js +30 -63
- package/dist/server/tools/delegate-task.js +14 -72
- package/dist/server/tools/orchestrator-plan.d.ts +6 -6
- package/dist/server/tools/orchestrator-plan.js +10 -47
- package/dist/server/types.d.ts +47 -0
- package/dist/server/types.js +24 -0
- package/dist/server/utils/ctx-utils.d.ts +30 -0
- package/dist/server/utils/ctx-utils.js +152 -0
- package/dist/server/utils/logging.d.ts +6 -0
- package/dist/server/utils/logging.js +86 -0
- package/package.json +44 -44
- package/src/client/AgentRunsTab.tsx +764 -764
- package/src/client/HarnessProfilesTab.tsx +247 -247
- package/src/client/OrchestratorSettings.tsx +106 -106
- package/src/client/RulesTab.tsx +716 -716
- package/src/client/hooks/useRunEventStream.ts +76 -0
- package/src/client/index.tsx +2 -1
- package/src/client/plugin.tsx +27 -27
- package/src/client/skill-hub/components/LoopSettings.tsx +331 -331
- package/src/client/skill-hub/index.tsx +51 -51
- package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +99 -99
- package/src/client/skill-hub/tools/SkillHubCard.tsx +109 -109
- package/src/client/skill-hub/tools/loopTemplates.ts +52 -52
- package/src/client/skill-hub/tools/registerSkillLoopCards.ts +58 -58
- package/src/client/tools/PlanApprovalCard.tsx +175 -175
- package/src/client/tools/registerOrchestratorCards.ts +7 -7
- package/src/server/__tests__/agent-loop-controller.test.ts +375 -0
- package/src/server/__tests__/circuit-breaker.test.ts +169 -0
- package/src/server/__tests__/context-aggregator.test.ts +222 -0
- package/src/server/__tests__/parallel-execution.test.ts +318 -0
- package/src/server/__tests__/smoke.test.ts +120 -0
- package/src/server/collections/agent-execution-spans.ts +24 -0
- package/src/server/collections/agent-harness-profiles.ts +59 -59
- package/src/server/collections/agent-loop-events.ts +71 -71
- package/src/server/collections/agent-loop-runs.ts +38 -1
- package/src/server/collections/agent-loop-steps.ts +144 -144
- package/src/server/collections/orchestrator-config.ts +14 -0
- package/src/server/collections/skill-executions.ts +106 -106
- package/src/server/collections/skill-loop-configs.ts +65 -65
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +30 -30
- package/src/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.ts +142 -142
- package/src/server/migrations/20260601000000-add-token-fields.ts +89 -0
- package/src/server/plugin.ts +53 -0
- package/src/server/resources/agent-loop.ts +21 -12
- package/src/server/resources/tracing.ts +3 -7
- package/src/server/services/AgentHarness.ts +78 -116
- package/src/server/services/AgentLoopController.ts +197 -122
- package/src/server/services/AgentLoopRepository.ts +9 -25
- package/src/server/services/AgentLoopService.ts +13 -1
- package/src/server/services/AgentPlanValidator.ts +73 -73
- package/src/server/services/AgentPlannerService.ts +2 -25
- package/src/server/services/AgentRegistryService.ts +40 -31
- package/src/server/services/CircuitBreaker.ts +116 -0
- package/src/server/services/ContextAggregator.ts +239 -0
- package/src/server/services/ExecutionSpanService.ts +2 -4
- package/src/server/services/RunEventBus.ts +45 -0
- package/src/server/services/TokenTracker.ts +209 -0
- package/src/server/skill-hub/plugin.ts +898 -897
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +460 -458
- package/src/server/tools/agent-loop.ts +18 -57
- package/src/server/tools/delegate-task.ts +11 -93
- package/src/server/tools/orchestrator-plan.ts +26 -50
- package/src/server/tools/skill-execute.ts +160 -160
- package/src/server/types.ts +55 -0
- package/src/server/utils/ctx-utils.ts +118 -0
- package/src/server/utils/logging.ts +63 -0
|
@@ -31,6 +31,7 @@ __export(agent_loop_exports, {
|
|
|
31
31
|
module.exports = __toCommonJS(agent_loop_exports);
|
|
32
32
|
var import_zod = require("zod");
|
|
33
33
|
var import_ExecutionSpanService = require("../services/ExecutionSpanService");
|
|
34
|
+
var import_ctx_utils = require("../utils/ctx-utils");
|
|
34
35
|
const stepSchema = import_zod.z.object({
|
|
35
36
|
id: import_zod.z.string().optional(),
|
|
36
37
|
key: import_zod.z.string().optional(),
|
|
@@ -58,46 +59,6 @@ function toolResult(status, payload) {
|
|
|
58
59
|
content: typeof payload === "string" ? payload : JSON.stringify(payload)
|
|
59
60
|
};
|
|
60
61
|
}
|
|
61
|
-
function valuesFromCtx(ctx) {
|
|
62
|
-
var _a, _b;
|
|
63
|
-
return ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.values) || {};
|
|
64
|
-
}
|
|
65
|
-
function currentUserId(ctx) {
|
|
66
|
-
var _a, _b, _c, _d;
|
|
67
|
-
return ((_b = (_a = ctx == null ? void 0 : ctx.state) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.id) || ((_d = (_c = ctx == null ? void 0 : ctx.auth) == null ? void 0 : _c.user) == null ? void 0 : _d.id);
|
|
68
|
-
}
|
|
69
|
-
function resolveSessionId(ctx, args) {
|
|
70
|
-
var _a, _b, _c;
|
|
71
|
-
const values = valuesFromCtx(ctx);
|
|
72
|
-
return (args == null ? void 0 : args.sessionId) || values.sessionId || ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.sessionId) || ((_c = ctx == null ? void 0 : ctx.state) == null ? void 0 : _c.sessionId);
|
|
73
|
-
}
|
|
74
|
-
function resolveMessageId(ctx, args) {
|
|
75
|
-
var _a, _b;
|
|
76
|
-
const values = valuesFromCtx(ctx);
|
|
77
|
-
return (args == null ? void 0 : args.messageId) || values.messageId || ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.messageId);
|
|
78
|
-
}
|
|
79
|
-
function normalizeEmployeeUsername(raw) {
|
|
80
|
-
if (!raw) return null;
|
|
81
|
-
if (typeof raw === "string") return raw;
|
|
82
|
-
return raw.username || raw.aiEmployeeUsername || raw.name || null;
|
|
83
|
-
}
|
|
84
|
-
async function resolveLeaderUsername(ctx, plugin, args) {
|
|
85
|
-
var _a, _b, _c, _d, _e, _f;
|
|
86
|
-
const values = valuesFromCtx(ctx);
|
|
87
|
-
const direct = normalizeEmployeeUsername(
|
|
88
|
-
(args == null ? void 0 : args.leaderUsername) || (ctx == null ? void 0 : ctx._currentAIEmployee) || ((_a = ctx == null ? void 0 : ctx.state) == null ? void 0 : _a.currentAIEmployee) || ((_c = (_b = ctx == null ? void 0 : ctx.runtime) == null ? void 0 : _b.context) == null ? void 0 : _c.currentAIEmployee) || values.aiEmployee
|
|
89
|
-
);
|
|
90
|
-
if (direct) return direct;
|
|
91
|
-
const sessionId = resolveSessionId(ctx, args);
|
|
92
|
-
if (!sessionId) return void 0;
|
|
93
|
-
try {
|
|
94
|
-
const repo = ((_e = (_d = ctx == null ? void 0 : ctx.db) == null ? void 0 : _d.getRepository) == null ? void 0 : _e.call(_d, "aiConversations")) || plugin.db.getRepository("aiConversations");
|
|
95
|
-
const conversation = await repo.findOne({ filter: { sessionId } });
|
|
96
|
-
return normalizeEmployeeUsername((conversation == null ? void 0 : conversation.aiEmployeeUsername) || ((_f = conversation == null ? void 0 : conversation.get) == null ? void 0 : _f.call(conversation, "aiEmployeeUsername")));
|
|
97
|
-
} catch {
|
|
98
|
-
return void 0;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
62
|
function setLoopTraceContext(ctx, snapshot, step) {
|
|
102
63
|
if (!ctx || !(snapshot == null ? void 0 : snapshot.run)) return;
|
|
103
64
|
const existing = (0, import_ExecutionSpanService.getOrchestratorTraceContext)(ctx) || {};
|
|
@@ -136,19 +97,20 @@ function createAgentLoopTools(plugin, service) {
|
|
|
136
97
|
})
|
|
137
98
|
},
|
|
138
99
|
invoke: async (ctx, args) => {
|
|
100
|
+
var _a;
|
|
139
101
|
try {
|
|
140
|
-
const leaderUsername = await resolveLeaderUsername(ctx, plugin, args);
|
|
102
|
+
const leaderUsername = await (0, import_ctx_utils.resolveLeaderUsername)(ctx, plugin, args);
|
|
141
103
|
const snapshot = await service.createRun({
|
|
142
104
|
goal: args.goal,
|
|
143
105
|
leaderUsername,
|
|
144
|
-
sessionId: resolveSessionId(ctx, args),
|
|
145
|
-
messageId: resolveMessageId(ctx, args),
|
|
146
|
-
userId: currentUserId(ctx),
|
|
106
|
+
sessionId: (0, import_ctx_utils.resolveSessionId)(ctx, args),
|
|
107
|
+
messageId: (0, import_ctx_utils.resolveMessageId)(ctx, args),
|
|
108
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
147
109
|
policy: args.policy,
|
|
148
110
|
metadata: args.metadata,
|
|
149
111
|
plan: planFromArgs(args.plan)
|
|
150
112
|
});
|
|
151
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
113
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
152
114
|
return toolResult("success", snapshot);
|
|
153
115
|
} catch (error) {
|
|
154
116
|
return toolResult("error", (error == null ? void 0 : error.message) || String(error));
|
|
@@ -161,19 +123,20 @@ function createAgentLoopTools(plugin, service) {
|
|
|
161
123
|
defaultPermission: "ALLOW",
|
|
162
124
|
introduction: {
|
|
163
125
|
title: "Agent Loop - Status",
|
|
164
|
-
about: "Read the current run, steps, and next executable
|
|
126
|
+
about: "Read the current run, steps, and next executable steps."
|
|
165
127
|
},
|
|
166
128
|
definition: {
|
|
167
129
|
name: "agent_loop_status",
|
|
168
|
-
description: "Fetch an agent loop run. Call this
|
|
130
|
+
description: "Fetch an agent loop run. Call this to check which steps are ready for execution.",
|
|
169
131
|
schema: import_zod.z.object({
|
|
170
132
|
runId: import_zod.z.union([import_zod.z.string(), import_zod.z.number()])
|
|
171
133
|
})
|
|
172
134
|
},
|
|
173
135
|
invoke: async (ctx, args) => {
|
|
136
|
+
var _a;
|
|
174
137
|
try {
|
|
175
138
|
const snapshot = await service.getRunSnapshot(args.runId);
|
|
176
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
139
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
177
140
|
return toolResult("success", snapshot);
|
|
178
141
|
} catch (error) {
|
|
179
142
|
return toolResult("error", (error == null ? void 0 : error.message) || String(error));
|
|
@@ -203,34 +166,35 @@ function createAgentLoopTools(plugin, service) {
|
|
|
203
166
|
})
|
|
204
167
|
},
|
|
205
168
|
invoke: async (ctx, args) => {
|
|
169
|
+
var _a, _b, _c;
|
|
206
170
|
try {
|
|
207
171
|
let snapshot;
|
|
208
172
|
if (args.status === "running") {
|
|
209
173
|
snapshot = await service.startStep(args.stepId, {
|
|
210
|
-
userId: currentUserId(ctx),
|
|
174
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
211
175
|
agentExecutionSpanId: args.agentExecutionSpanId
|
|
212
176
|
});
|
|
213
177
|
const currentStep = snapshot.steps.find((step) => String(step.id) === String(args.stepId));
|
|
214
178
|
setLoopTraceContext(ctx, snapshot, currentStep);
|
|
215
179
|
} else if (args.status === "succeeded") {
|
|
216
180
|
snapshot = await service.completeStep(args.stepId, args.output === void 0 ? {} : args.output, {
|
|
217
|
-
userId: currentUserId(ctx),
|
|
181
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
218
182
|
skillExecutionId: args.skillExecutionId,
|
|
219
183
|
agentExecutionSpanId: args.agentExecutionSpanId,
|
|
220
184
|
metadata: args.metadata
|
|
221
185
|
});
|
|
222
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
186
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
223
187
|
} else if (args.status === "failed") {
|
|
224
188
|
snapshot = await service.failStep(args.stepId, args.error || "Step failed.", {
|
|
225
|
-
userId: currentUserId(ctx),
|
|
189
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
226
190
|
metadata: args.metadata
|
|
227
191
|
});
|
|
228
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
192
|
+
setLoopTraceContext(ctx, snapshot, (_b = snapshot.nextSteps) == null ? void 0 : _b[0]);
|
|
229
193
|
} else {
|
|
230
194
|
snapshot = await service.skipStep(args.stepId, args.reason || "Skipped.", {
|
|
231
|
-
userId: currentUserId(ctx)
|
|
195
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
232
196
|
});
|
|
233
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
197
|
+
setLoopTraceContext(ctx, snapshot, (_c = snapshot.nextSteps) == null ? void 0 : _c[0]);
|
|
234
198
|
}
|
|
235
199
|
return toolResult("success", snapshot);
|
|
236
200
|
} catch (error) {
|
|
@@ -257,14 +221,15 @@ function createAgentLoopTools(plugin, service) {
|
|
|
257
221
|
})
|
|
258
222
|
},
|
|
259
223
|
invoke: async (ctx, args) => {
|
|
224
|
+
var _a;
|
|
260
225
|
try {
|
|
261
226
|
await service.replan(args.runId, args.plan, {
|
|
262
227
|
reason: args.reason,
|
|
263
228
|
mode: args.mode || "replace_pending",
|
|
264
|
-
userId: currentUserId(ctx)
|
|
229
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
265
230
|
});
|
|
266
231
|
const snapshot = await service.getRunSnapshot(args.runId);
|
|
267
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
232
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
268
233
|
return toolResult("success", snapshot);
|
|
269
234
|
} catch (error) {
|
|
270
235
|
return toolResult("error", (error == null ? void 0 : error.message) || String(error));
|
|
@@ -289,12 +254,13 @@ function createAgentLoopTools(plugin, service) {
|
|
|
289
254
|
})
|
|
290
255
|
},
|
|
291
256
|
invoke: async (ctx, args) => {
|
|
257
|
+
var _a;
|
|
292
258
|
try {
|
|
293
259
|
const snapshot = await service.requestApproval(args.stepId, args.approval || {}, {
|
|
294
260
|
reason: args.reason,
|
|
295
|
-
userId: currentUserId(ctx)
|
|
261
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
296
262
|
});
|
|
297
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
263
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
298
264
|
return toolResult("success", snapshot);
|
|
299
265
|
} catch (error) {
|
|
300
266
|
return toolResult("error", (error == null ? void 0 : error.message) || String(error));
|
|
@@ -320,15 +286,16 @@ function createAgentLoopTools(plugin, service) {
|
|
|
320
286
|
})
|
|
321
287
|
},
|
|
322
288
|
invoke: async (ctx, args) => {
|
|
289
|
+
var _a;
|
|
323
290
|
try {
|
|
324
291
|
const snapshot = await service.resumeRun(args.runId, {
|
|
325
292
|
stepId: args.stepId,
|
|
326
293
|
approved: args.approved,
|
|
327
294
|
editedInput: args.editedInput,
|
|
328
|
-
userId: currentUserId(ctx),
|
|
295
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
329
296
|
ctx
|
|
330
297
|
});
|
|
331
|
-
setLoopTraceContext(ctx, snapshot, snapshot.
|
|
298
|
+
setLoopTraceContext(ctx, snapshot, (_a = snapshot.nextSteps) == null ? void 0 : _a[0]);
|
|
332
299
|
return toolResult("success", snapshot);
|
|
333
300
|
} catch (error) {
|
|
334
301
|
return toolResult("error", (error == null ? void 0 : error.message) || String(error));
|
|
@@ -360,7 +327,7 @@ function createAgentLoopTools(plugin, service) {
|
|
|
360
327
|
status: args.status || "succeeded",
|
|
361
328
|
summary: args.summary,
|
|
362
329
|
evidence: args.evidence,
|
|
363
|
-
userId: currentUserId(ctx)
|
|
330
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
364
331
|
});
|
|
365
332
|
setLoopTraceContext(ctx, snapshot);
|
|
366
333
|
return toolResult("success", snapshot);
|
|
@@ -389,7 +356,7 @@ function createAgentLoopTools(plugin, service) {
|
|
|
389
356
|
try {
|
|
390
357
|
const snapshot = await service.cancelRun(args.runId, {
|
|
391
358
|
reason: args.reason,
|
|
392
|
-
userId: currentUserId(ctx)
|
|
359
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
393
360
|
});
|
|
394
361
|
setLoopTraceContext(ctx, snapshot);
|
|
395
362
|
return toolResult("success", snapshot);
|
|
@@ -33,6 +33,8 @@ module.exports = __toCommonJS(delegate_task_exports);
|
|
|
33
33
|
var import_zod = require("zod");
|
|
34
34
|
var import_crypto = require("crypto");
|
|
35
35
|
var import_ExecutionSpanService = require("../services/ExecutionSpanService");
|
|
36
|
+
var import_ctx_utils = require("../utils/ctx-utils");
|
|
37
|
+
var import_logging = require("../utils/logging");
|
|
36
38
|
const ORCHESTRATOR_DEPTH_KEY = "__orchestratorDepth";
|
|
37
39
|
const ORCHESTRATOR_PATH_KEY = "__orchestratorPath";
|
|
38
40
|
const MAX_DISPATCH_CONCURRENCY = 5;
|
|
@@ -232,7 +234,7 @@ ${subAgentList}`
|
|
|
232
234
|
leaderUsername,
|
|
233
235
|
subAgentUsername: reportedSub,
|
|
234
236
|
toolName,
|
|
235
|
-
task:
|
|
237
|
+
task: (0, import_ctx_utils.trimText)(args.tasks ?? [], 2e3),
|
|
236
238
|
result: "",
|
|
237
239
|
status: "error",
|
|
238
240
|
depth: ctx[ORCHESTRATOR_DEPTH_KEY] ?? 0,
|
|
@@ -251,7 +253,7 @@ ${subAgentList}`
|
|
|
251
253
|
leaderUsername,
|
|
252
254
|
subAgentUsername: reportedSub,
|
|
253
255
|
toolName,
|
|
254
|
-
task:
|
|
256
|
+
task: (0, import_ctx_utils.trimText)(args.tasks ?? [], 2e3),
|
|
255
257
|
result: "",
|
|
256
258
|
status: "error",
|
|
257
259
|
depth: ctx[ORCHESTRATOR_DEPTH_KEY] ?? 0,
|
|
@@ -324,25 +326,11 @@ ${subAgentList}`
|
|
|
324
326
|
}
|
|
325
327
|
};
|
|
326
328
|
}
|
|
327
|
-
function captureCtxSnapshot(ctx) {
|
|
328
|
-
var _a, _b, _c, _d;
|
|
329
|
-
let userId;
|
|
330
|
-
try {
|
|
331
|
-
userId = ((_b = (_a = ctx.auth) == null ? void 0 : _a.user) == null ? void 0 : _b.id) || ((_d = (_c = ctx.state) == null ? void 0 : _c.currentUser) == null ? void 0 : _d.id);
|
|
332
|
-
} catch {
|
|
333
|
-
}
|
|
334
|
-
return { userId };
|
|
335
|
-
}
|
|
336
|
-
function normalizeEmployeeUsername(raw) {
|
|
337
|
-
if (!raw) return null;
|
|
338
|
-
if (typeof raw === "string") return raw;
|
|
339
|
-
return raw.username || raw.aiEmployeeUsername || raw.name || null;
|
|
340
|
-
}
|
|
341
329
|
async function resolveCallingEmployee(ctx, plugin) {
|
|
342
330
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
343
331
|
const values = ((_b = (_a = ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.values) || {};
|
|
344
332
|
const raw = ctx._currentAIEmployee || ((_c = ctx.state) == null ? void 0 : _c.currentAIEmployee) || ((_e = (_d = ctx.runtime) == null ? void 0 : _d.context) == null ? void 0 : _e.currentAIEmployee) || values.aiEmployee;
|
|
345
|
-
const direct = normalizeEmployeeUsername(raw);
|
|
333
|
+
const direct = (0, import_ctx_utils.normalizeEmployeeUsername)(raw);
|
|
346
334
|
if (direct) return direct;
|
|
347
335
|
const sessionId = values.sessionId || ((_g = (_f = ctx.action) == null ? void 0 : _f.params) == null ? void 0 : _g.sessionId);
|
|
348
336
|
if (!sessionId) return null;
|
|
@@ -351,20 +339,12 @@ async function resolveCallingEmployee(ctx, plugin) {
|
|
|
351
339
|
const conversation = await repo.findOne({
|
|
352
340
|
filter: { sessionId }
|
|
353
341
|
});
|
|
354
|
-
return normalizeEmployeeUsername((conversation == null ? void 0 : conversation.aiEmployeeUsername) || ((_j = conversation == null ? void 0 : conversation.get) == null ? void 0 : _j.call(conversation, "aiEmployeeUsername")));
|
|
342
|
+
return (0, import_ctx_utils.normalizeEmployeeUsername)((conversation == null ? void 0 : conversation.aiEmployeeUsername) || ((_j = conversation == null ? void 0 : conversation.get) == null ? void 0 : _j.call(conversation, "aiEmployeeUsername")));
|
|
355
343
|
} catch (e) {
|
|
356
344
|
plugin.app.log.warn(`[AgentOrchestrator] Failed to resolve AI employee for session "${sessionId}"`, e);
|
|
357
345
|
return null;
|
|
358
346
|
}
|
|
359
347
|
}
|
|
360
|
-
function truncateText(value, maxLen) {
|
|
361
|
-
const text = typeof value === "string" ? value : value == null ? "" : JSON.stringify(value);
|
|
362
|
-
return text.length > maxLen ? `${text.slice(0, maxLen)}
|
|
363
|
-
...[truncated]` : text;
|
|
364
|
-
}
|
|
365
|
-
function nowIso() {
|
|
366
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
367
|
-
}
|
|
368
348
|
function hasModelSettings(value) {
|
|
369
349
|
return Boolean((value == null ? void 0 : value.llmService) && (value == null ? void 0 : value.model));
|
|
370
350
|
}
|
|
@@ -568,7 +548,7 @@ async function invokeDelegateTask(ctx, plugin, options) {
|
|
|
568
548
|
rootRunId: providedRootRunId,
|
|
569
549
|
parentSpanId: providedParentSpanId
|
|
570
550
|
} = options;
|
|
571
|
-
const ctxSnapshot = captureCtxSnapshot(ctx);
|
|
551
|
+
const ctxSnapshot = (0, import_ctx_utils.captureCtxSnapshot)(ctx);
|
|
572
552
|
const currentDepth = ctx[ORCHESTRATOR_DEPTH_KEY] ?? 0;
|
|
573
553
|
const currentPath = ctx[ORCHESTRATOR_PATH_KEY] ?? [leaderUsername];
|
|
574
554
|
if (currentPath.includes(subAgentUsername)) {
|
|
@@ -637,53 +617,15 @@ async function invokeDelegateTask(ctx, plugin, options) {
|
|
|
637
617
|
});
|
|
638
618
|
}
|
|
639
619
|
async function logDelegation(ctx, plugin, data) {
|
|
640
|
-
var _a, _b, _c, _d, _e
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
}
|
|
647
|
-
let userId = (_a = data.snapshot) == null ? void 0 : _a.userId;
|
|
648
|
-
if (userId == null) {
|
|
649
|
-
try {
|
|
650
|
-
userId = ((_c = (_b = ctx.auth) == null ? void 0 : _b.user) == null ? void 0 : _c.id) || ((_e = (_d = ctx.state) == null ? void 0 : _d.currentUser) == null ? void 0 : _e.id);
|
|
651
|
-
} catch {
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
const values = {
|
|
655
|
-
leaderUsername: data.leaderUsername,
|
|
656
|
-
subAgentUsername: data.subAgentUsername,
|
|
657
|
-
toolName: data.toolName,
|
|
658
|
-
task: truncateText(data.task, 1e4),
|
|
659
|
-
context: truncateText(data.context || "", 1e4),
|
|
660
|
-
result: truncateText(data.result || "", 5e4),
|
|
661
|
-
status: data.status,
|
|
662
|
-
depth: data.depth,
|
|
663
|
-
durationMs: data.durationMs,
|
|
664
|
-
error: truncateText(data.error || "", 1e4),
|
|
665
|
-
trace: data.trace || [],
|
|
666
|
-
messages: data.messages || [],
|
|
667
|
-
userId,
|
|
668
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
669
|
-
};
|
|
670
|
-
if (data.id) {
|
|
671
|
-
await logsRepo.update({
|
|
672
|
-
filterByTk: data.id,
|
|
673
|
-
values
|
|
674
|
-
});
|
|
675
|
-
return { id: data.id };
|
|
620
|
+
var _a, _b, _c, _d, _e;
|
|
621
|
+
let userId = (_a = data.snapshot) == null ? void 0 : _a.userId;
|
|
622
|
+
if (userId == null) {
|
|
623
|
+
try {
|
|
624
|
+
userId = ((_c = (_b = ctx.auth) == null ? void 0 : _b.user) == null ? void 0 : _c.id) || ((_e = (_d = ctx.state) == null ? void 0 : _d.currentUser) == null ? void 0 : _e.id);
|
|
625
|
+
} catch {
|
|
676
626
|
}
|
|
677
|
-
const record = await logsRepo.create({
|
|
678
|
-
values: {
|
|
679
|
-
...values,
|
|
680
|
-
createdAt: /* @__PURE__ */ new Date()
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
return ((_f = record == null ? void 0 : record.toJSON) == null ? void 0 : _f.call(record)) || record;
|
|
684
|
-
} catch (e) {
|
|
685
|
-
plugin.app.log.warn("[AgentOrchestrator] Failed to log delegation event", e);
|
|
686
627
|
}
|
|
628
|
+
return (0, import_logging.logDelegation)(ctx, plugin, { ...data, userId });
|
|
687
629
|
}
|
|
688
630
|
// Annotate the CommonJS export names for ESM import in node:
|
|
689
631
|
0 && (module.exports = {
|
|
@@ -61,10 +61,10 @@ export declare function createOrchestratorPlanTools(plugin: any, service: AgentL
|
|
|
61
61
|
description?: string;
|
|
62
62
|
metadata?: any;
|
|
63
63
|
input?: any;
|
|
64
|
+
id?: string;
|
|
64
65
|
planKey?: string;
|
|
65
66
|
target?: string;
|
|
66
67
|
dependsOn?: string[];
|
|
67
|
-
id?: string;
|
|
68
68
|
parentStepId?: string | number;
|
|
69
69
|
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
70
70
|
maxAttempts?: number;
|
|
@@ -75,10 +75,10 @@ export declare function createOrchestratorPlanTools(plugin: any, service: AgentL
|
|
|
75
75
|
description?: string;
|
|
76
76
|
metadata?: any;
|
|
77
77
|
input?: any;
|
|
78
|
+
id?: string;
|
|
78
79
|
planKey?: string;
|
|
79
80
|
target?: string;
|
|
80
81
|
dependsOn?: string[];
|
|
81
|
-
id?: string;
|
|
82
82
|
parentStepId?: string | number;
|
|
83
83
|
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
84
84
|
maxAttempts?: number;
|
|
@@ -107,10 +107,10 @@ export declare function createOrchestratorPlanTools(plugin: any, service: AgentL
|
|
|
107
107
|
description?: string;
|
|
108
108
|
metadata?: any;
|
|
109
109
|
input?: any;
|
|
110
|
+
id?: string;
|
|
110
111
|
planKey?: string;
|
|
111
112
|
target?: string;
|
|
112
113
|
dependsOn?: string[];
|
|
113
|
-
id?: string;
|
|
114
114
|
parentStepId?: string | number;
|
|
115
115
|
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
116
116
|
maxAttempts?: number;
|
|
@@ -139,10 +139,10 @@ export declare function createOrchestratorPlanTools(plugin: any, service: AgentL
|
|
|
139
139
|
description?: string;
|
|
140
140
|
metadata?: any;
|
|
141
141
|
input?: any;
|
|
142
|
+
id?: string;
|
|
142
143
|
planKey?: string;
|
|
143
144
|
target?: string;
|
|
144
145
|
dependsOn?: string[];
|
|
145
|
-
id?: string;
|
|
146
146
|
parentStepId?: string | number;
|
|
147
147
|
dependencyPolicy?: "require_success" | "allow_skipped";
|
|
148
148
|
maxAttempts?: number;
|
|
@@ -191,11 +191,11 @@ export declare function createOrchestratorPlanTools(plugin: any, service: AgentL
|
|
|
191
191
|
runId: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
|
|
192
192
|
reason: z.ZodOptional<z.ZodString>;
|
|
193
193
|
}, "strip", z.ZodTypeAny, {
|
|
194
|
-
runId?: string | number;
|
|
195
194
|
reason?: string;
|
|
196
|
-
}, {
|
|
197
195
|
runId?: string | number;
|
|
196
|
+
}, {
|
|
198
197
|
reason?: string;
|
|
198
|
+
runId?: string | number;
|
|
199
199
|
}>;
|
|
200
200
|
};
|
|
201
201
|
invoke: (ctx: any, args: any) => Promise<{
|
|
@@ -30,6 +30,7 @@ __export(orchestrator_plan_exports, {
|
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(orchestrator_plan_exports);
|
|
32
32
|
var import_zod = require("zod");
|
|
33
|
+
var import_ctx_utils = require("../utils/ctx-utils");
|
|
33
34
|
const stepSchema = import_zod.z.object({
|
|
34
35
|
id: import_zod.z.string().optional(),
|
|
35
36
|
key: import_zod.z.string().optional(),
|
|
@@ -58,46 +59,6 @@ function toolResult(status, payload) {
|
|
|
58
59
|
content: typeof payload === "string" ? payload : JSON.stringify(payload)
|
|
59
60
|
};
|
|
60
61
|
}
|
|
61
|
-
function valuesFromCtx(ctx) {
|
|
62
|
-
var _a, _b, _c;
|
|
63
|
-
return ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.values) || ((_c = ctx == null ? void 0 : ctx.request) == null ? void 0 : _c.body) || {};
|
|
64
|
-
}
|
|
65
|
-
function currentUserId(ctx) {
|
|
66
|
-
var _a, _b, _c, _d;
|
|
67
|
-
return ((_b = (_a = ctx == null ? void 0 : ctx.state) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.id) || ((_d = (_c = ctx == null ? void 0 : ctx.auth) == null ? void 0 : _c.user) == null ? void 0 : _d.id);
|
|
68
|
-
}
|
|
69
|
-
function resolveSessionId(ctx, args) {
|
|
70
|
-
var _a, _b, _c;
|
|
71
|
-
const values = valuesFromCtx(ctx);
|
|
72
|
-
return (args == null ? void 0 : args.sessionId) || values.sessionId || ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.sessionId) || ((_c = ctx == null ? void 0 : ctx.state) == null ? void 0 : _c.sessionId);
|
|
73
|
-
}
|
|
74
|
-
function resolveMessageId(ctx, args) {
|
|
75
|
-
var _a, _b;
|
|
76
|
-
const values = valuesFromCtx(ctx);
|
|
77
|
-
return (args == null ? void 0 : args.messageId) || values.messageId || ((_b = (_a = ctx == null ? void 0 : ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.messageId);
|
|
78
|
-
}
|
|
79
|
-
function normalizeEmployeeUsername(raw) {
|
|
80
|
-
if (!raw) return null;
|
|
81
|
-
if (typeof raw === "string") return raw;
|
|
82
|
-
return raw.username || raw.aiEmployeeUsername || raw.name || null;
|
|
83
|
-
}
|
|
84
|
-
async function resolveLeaderUsername(ctx, plugin, args) {
|
|
85
|
-
var _a, _b, _c, _d, _e, _f;
|
|
86
|
-
const values = valuesFromCtx(ctx);
|
|
87
|
-
const direct = normalizeEmployeeUsername(
|
|
88
|
-
(args == null ? void 0 : args.leaderUsername) || (ctx == null ? void 0 : ctx._currentAIEmployee) || ((_a = ctx == null ? void 0 : ctx.state) == null ? void 0 : _a.currentAIEmployee) || ((_c = (_b = ctx == null ? void 0 : ctx.runtime) == null ? void 0 : _b.context) == null ? void 0 : _c.currentAIEmployee) || values.aiEmployee
|
|
89
|
-
);
|
|
90
|
-
if (direct) return direct;
|
|
91
|
-
const sessionId = resolveSessionId(ctx, args);
|
|
92
|
-
if (!sessionId) return void 0;
|
|
93
|
-
try {
|
|
94
|
-
const repo = ((_e = (_d = ctx == null ? void 0 : ctx.db) == null ? void 0 : _d.getRepository) == null ? void 0 : _e.call(_d, "aiConversations")) || plugin.db.getRepository("aiConversations");
|
|
95
|
-
const conversation = await repo.findOne({ filter: { sessionId } });
|
|
96
|
-
return normalizeEmployeeUsername((conversation == null ? void 0 : conversation.aiEmployeeUsername) || ((_f = conversation == null ? void 0 : conversation.get) == null ? void 0 : _f.call(conversation, "aiEmployeeUsername")));
|
|
97
|
-
} catch {
|
|
98
|
-
return void 0;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
62
|
function summarizePlan(steps) {
|
|
102
63
|
return (steps || []).map((step) => ({
|
|
103
64
|
id: step.id,
|
|
@@ -159,21 +120,23 @@ function createOrchestratorPlanTools(plugin, service) {
|
|
|
159
120
|
plannerModel: import_zod.z.string().optional(),
|
|
160
121
|
policy: policySchema,
|
|
161
122
|
metadata: import_zod.z.any().optional(),
|
|
162
|
-
plan: import_zod.z.array(stepSchema).optional().describe(
|
|
123
|
+
plan: import_zod.z.array(stepSchema).optional().describe(
|
|
124
|
+
"Draft plan steps. Tool/sub_agent steps should include target. Dependencies must reference planKey values."
|
|
125
|
+
)
|
|
163
126
|
})
|
|
164
127
|
},
|
|
165
128
|
invoke: async (ctx, args) => {
|
|
166
129
|
try {
|
|
167
|
-
const leaderUsername = await resolveLeaderUsername(ctx, plugin, args);
|
|
130
|
+
const leaderUsername = await (0, import_ctx_utils.resolveLeaderUsername)(ctx, plugin, args);
|
|
168
131
|
const targetAgent = inferTargetAgent(args);
|
|
169
132
|
const harnessTag = await resolveHarnessTag(plugin, leaderUsername, targetAgent, args);
|
|
170
133
|
const detail = await service.planGoal({
|
|
171
134
|
goal: args.goal,
|
|
172
135
|
runId: args.runId,
|
|
173
136
|
leaderUsername,
|
|
174
|
-
sessionId: resolveSessionId(ctx, args),
|
|
175
|
-
messageId: resolveMessageId(ctx, args),
|
|
176
|
-
userId: currentUserId(ctx),
|
|
137
|
+
sessionId: (0, import_ctx_utils.resolveSessionId)(ctx, args),
|
|
138
|
+
messageId: (0, import_ctx_utils.resolveMessageId)(ctx, args),
|
|
139
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
177
140
|
policy: args.policy,
|
|
178
141
|
metadata: args.metadata,
|
|
179
142
|
plan: Array.isArray(args.plan) ? args.plan : void 0,
|
|
@@ -220,7 +183,7 @@ function createOrchestratorPlanTools(plugin, service) {
|
|
|
220
183
|
invoke: async (ctx, args) => {
|
|
221
184
|
try {
|
|
222
185
|
const detail = await service.approvePlanAndExecute(args.runId, {
|
|
223
|
-
userId: currentUserId(ctx),
|
|
186
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx),
|
|
224
187
|
ctx,
|
|
225
188
|
reason: args.reason
|
|
226
189
|
});
|
|
@@ -275,7 +238,7 @@ function createOrchestratorPlanTools(plugin, service) {
|
|
|
275
238
|
"success",
|
|
276
239
|
await service.cancelRun(args.runId, {
|
|
277
240
|
reason: args.reason,
|
|
278
|
-
userId: currentUserId(ctx)
|
|
241
|
+
userId: (0, import_ctx_utils.currentUserId)(ctx)
|
|
279
242
|
})
|
|
280
243
|
);
|
|
281
244
|
} catch (error) {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface TokenUsage {
|
|
2
|
+
inputTokens: number;
|
|
3
|
+
outputTokens: number;
|
|
4
|
+
totalTokens: number;
|
|
5
|
+
cost: number;
|
|
6
|
+
}
|
|
7
|
+
export interface BudgetConfig {
|
|
8
|
+
budgetMaxTokens?: number;
|
|
9
|
+
budgetMaxCost?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface BudgetCheckResult {
|
|
12
|
+
allowed: boolean;
|
|
13
|
+
reason?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface CircuitState {
|
|
16
|
+
failures: number;
|
|
17
|
+
lastFailureTime: number;
|
|
18
|
+
state: 'closed' | 'open' | 'half-open';
|
|
19
|
+
}
|
|
20
|
+
export interface TraceEvent {
|
|
21
|
+
type: string;
|
|
22
|
+
at: string;
|
|
23
|
+
title: string;
|
|
24
|
+
content?: string;
|
|
25
|
+
toolName?: string;
|
|
26
|
+
args?: any;
|
|
27
|
+
status?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface DelegationLogData {
|
|
30
|
+
id?: number | string;
|
|
31
|
+
leaderUsername: string;
|
|
32
|
+
subAgentUsername: string;
|
|
33
|
+
toolName: string;
|
|
34
|
+
task: string;
|
|
35
|
+
context?: string;
|
|
36
|
+
result: string;
|
|
37
|
+
status: string;
|
|
38
|
+
depth: number;
|
|
39
|
+
durationMs: number;
|
|
40
|
+
error?: string;
|
|
41
|
+
trace?: TraceEvent[];
|
|
42
|
+
messages?: any[];
|
|
43
|
+
userId?: number | string;
|
|
44
|
+
}
|
|
45
|
+
export type CtxSnapshot = {
|
|
46
|
+
userId?: number;
|
|
47
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var types_exports = {};
|
|
24
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Normalize a record to a plain JS object. */
|
|
2
|
+
export declare function toPlain(record: any): any;
|
|
3
|
+
/** Coerce a value to a plain object (JSON parse if needed). */
|
|
4
|
+
export declare function asObject(value: any): any;
|
|
5
|
+
/** Coerce value to array. */
|
|
6
|
+
export declare function asArray(value: any): any[];
|
|
7
|
+
/** Trim text to max length with ellipsis suffix. */
|
|
8
|
+
export declare function trimText(value: any, max?: number): string;
|
|
9
|
+
/** Get the current user id from ctx. */
|
|
10
|
+
export declare function currentUserId(ctx: any): any;
|
|
11
|
+
/** Get action params values from ctx. */
|
|
12
|
+
export declare function valuesFromCtx(ctx: any): any;
|
|
13
|
+
/** Normalize raw employee username input (string | object → string | null). */
|
|
14
|
+
export declare function normalizeEmployeeUsername(raw: any): any;
|
|
15
|
+
/** Resolve session id from args or ctx. */
|
|
16
|
+
export declare function resolveSessionId(ctx: any, args: any): any;
|
|
17
|
+
/** Resolve message id from args or ctx. */
|
|
18
|
+
export declare function resolveMessageId(ctx: any, args: any): any;
|
|
19
|
+
/** Resolve leader employee username from args, ctx state, or conversation record. */
|
|
20
|
+
export declare function resolveLeaderUsername(ctx: any, plugin: any, args: any): Promise<any>;
|
|
21
|
+
/** Snapshot ctx user id for later use (avoids stale ctx). */
|
|
22
|
+
export declare function captureCtxSnapshot(ctx: any): {
|
|
23
|
+
userId?: number;
|
|
24
|
+
};
|
|
25
|
+
/** Normalize step type to a known value. */
|
|
26
|
+
export declare function normalizeStepType(value: any): any;
|
|
27
|
+
/** Normalize plan key from step input. */
|
|
28
|
+
export declare function normalizePlanKey(step: any, index: number): string;
|
|
29
|
+
/** Now ISO string. */
|
|
30
|
+
export declare function nowIso(): string;
|