plugin-agent-orchestrator 1.1.6 → 1.1.7
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/index.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +7 -7
- package/dist/server/collections/agent-loop-runs.js +6 -0
- package/dist/server/collections/skill-metrics.js +66 -0
- package/dist/server/plugin.js +91 -2
- package/dist/server/resources/agent-loop-events-stream.js +1 -0
- package/dist/server/resources/agent-loop.js +1 -1
- package/dist/server/services/DurableCircuitBreakerService.js +9 -12
- package/dist/server/services/ExecutionSpanService.js +8 -3
- package/dist/server/services/HarnessProfileService.js +23 -26
- package/dist/server/services/LoopBudgetService.js +36 -39
- package/dist/server/services/LoopControlService.js +8 -11
- package/dist/server/services/LoopPatternSchema.js +51 -17
- package/dist/server/services/LoopPatternService.js +31 -11
- package/dist/server/services/LoopRunRepository.js +2 -5
- package/dist/server/services/LoopRunStateMachine.js +31 -34
- package/dist/server/services/LoopSchedulerService.js +4 -7
- package/dist/server/services/LoopTriggerService.js +11 -6
- package/dist/server/services/LoopWorkerService.js +88 -22
- package/dist/server/services/PathLockService.js +19 -22
- package/dist/server/services/PluginAiRuntimeAdapter.js +51 -8
- package/dist/server/services/RunEventBus.js +14 -2
- package/dist/server/services/SkillAccessService.js +50 -1
- package/dist/server/services/TokenTracker.js +17 -1
- package/dist/server/services/ToolLoopDetectionService.js +14 -17
- package/dist/server/services/VerificationService.js +47 -16
- package/dist/server/services/harness-runtime-policy.js +7 -12
- package/dist/server/skill-hub/plugin.js +34 -27
- package/dist/server/utils/ha-lock.js +73 -0
- package/dist/server/utils/loop-pattern-context.js +42 -5
- package/dist/server/utils/record-utils.js +44 -0
- package/package.json +1 -1
- package/src/client/plugin.tsx +1 -1
- package/src/client-v2/plugin.tsx +14 -13
- package/src/server/__tests__/tool-loop-detection.test.ts +1 -0
- package/src/server/collections/agent-loop-runs.ts +7 -0
- package/src/server/collections/skill-metrics.ts +36 -0
- package/src/server/plugin.ts +117 -1
- package/src/server/resources/agent-loop-events-stream.ts +108 -105
- package/src/server/resources/agent-loop.ts +1 -1
- package/src/server/resources/tracing.ts +267 -266
- package/src/server/services/DurableCircuitBreakerService.ts +235 -239
- package/src/server/services/ExecutionSpanService.ts +37 -12
- package/src/server/services/HarnessProfileService.ts +1 -5
- package/src/server/services/LoopBudgetService.ts +271 -275
- package/src/server/services/LoopControlService.ts +120 -124
- package/src/server/services/LoopPatternSchema.ts +60 -14
- package/src/server/services/LoopPatternService.ts +171 -143
- package/src/server/services/LoopRunRepository.ts +1 -5
- package/src/server/services/LoopRunStateMachine.ts +1 -5
- package/src/server/services/LoopSchedulerService.ts +195 -199
- package/src/server/services/LoopTriggerService.ts +7 -5
- package/src/server/services/LoopWorkerService.ts +102 -12
- package/src/server/services/PathLockService.ts +317 -321
- package/src/server/services/PluginAiRuntimeAdapter.ts +59 -4
- package/src/server/services/RunEventBus.ts +13 -0
- package/src/server/services/SkillAccessService.ts +80 -5
- package/src/server/services/TokenTracker.ts +59 -7
- package/src/server/services/ToolLoopDetectionService.ts +8 -11
- package/src/server/services/VerificationSchema.ts +31 -31
- package/src/server/services/VerificationService.ts +60 -11
- package/src/server/services/harness-runtime-policy.ts +2 -9
- package/src/server/skill-hub/plugin.ts +36 -28
- package/src/server/types.ts +5 -39
- package/src/server/utils/ensure-indexes.ts +96 -96
- package/src/server/utils/ha-lock.ts +80 -0
- package/src/server/utils/loop-pattern-context.ts +84 -20
- package/src/server/utils/record-utils.ts +19 -0
package/src/client-v2/plugin.tsx
CHANGED
|
@@ -3,13 +3,14 @@ import { Plugin, Application } from '@nocobase/client-v2';
|
|
|
3
3
|
export class PluginAgentOrchestratorClient extends Plugin<Record<string, never>, Application> {
|
|
4
4
|
async load() {
|
|
5
5
|
this.pluginSettingsManager.addMenuItem({
|
|
6
|
-
key: 'ai
|
|
6
|
+
key: 'ai-orchestrator',
|
|
7
7
|
title: this.t('Agent Orchestrator'),
|
|
8
8
|
icon: 'ApartmentOutlined',
|
|
9
|
+
aclSnippet: 'pm.ai-orchestrator',
|
|
9
10
|
});
|
|
10
11
|
|
|
11
12
|
this.pluginSettingsManager.addPageTabItem({
|
|
12
|
-
menuKey: 'ai
|
|
13
|
+
menuKey: 'ai-orchestrator',
|
|
13
14
|
key: 'index',
|
|
14
15
|
title: this.t('Native Monitor'),
|
|
15
16
|
componentLoader: () => import('./pages/RulesPage'),
|
|
@@ -17,77 +18,77 @@ export class PluginAgentOrchestratorClient extends Plugin<Record<string, never>,
|
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
this.pluginSettingsManager.addPageTabItem({
|
|
20
|
-
menuKey: 'ai
|
|
21
|
+
menuKey: 'ai-orchestrator',
|
|
21
22
|
key: 'tracing',
|
|
22
23
|
title: this.t('Execution Tracing'),
|
|
23
24
|
componentLoader: () => import('./pages/TracingPage'),
|
|
24
25
|
});
|
|
25
26
|
|
|
26
27
|
this.pluginSettingsManager.addPageTabItem({
|
|
27
|
-
menuKey: 'ai
|
|
28
|
+
menuKey: 'ai-orchestrator',
|
|
28
29
|
key: 'harness-profiles',
|
|
29
30
|
title: this.t('Policy Profiles'),
|
|
30
31
|
componentLoader: () => import('./pages/HarnessProfilesPage'),
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
this.pluginSettingsManager.addPageTabItem({
|
|
34
|
-
menuKey: 'ai
|
|
35
|
+
menuKey: 'ai-orchestrator',
|
|
35
36
|
key: 'approvals',
|
|
36
37
|
title: this.t('Approvals'),
|
|
37
38
|
componentLoader: () => import('./pages/ApprovalsPage'),
|
|
38
39
|
});
|
|
39
40
|
|
|
40
41
|
this.pluginSettingsManager.addPageTabItem({
|
|
41
|
-
menuKey: 'ai
|
|
42
|
+
menuKey: 'ai-orchestrator',
|
|
42
43
|
key: 'agent-bindings',
|
|
43
44
|
title: this.t('Agent Bindings'),
|
|
44
45
|
componentLoader: () => import('./pages/AgentBindingsPage'),
|
|
45
46
|
});
|
|
46
47
|
|
|
47
48
|
this.pluginSettingsManager.addPageTabItem({
|
|
48
|
-
menuKey: 'ai
|
|
49
|
+
menuKey: 'ai-orchestrator',
|
|
49
50
|
key: 'knowledge-access',
|
|
50
51
|
title: this.t('Knowledge Access'),
|
|
51
52
|
componentLoader: () => import('./pages/KnowledgeAccessPage'),
|
|
52
53
|
});
|
|
53
54
|
|
|
54
55
|
this.pluginSettingsManager.addPageTabItem({
|
|
55
|
-
menuKey: 'ai
|
|
56
|
+
menuKey: 'ai-orchestrator',
|
|
56
57
|
key: 'retrieval-trace',
|
|
57
58
|
title: this.t('Retrieval Trace'),
|
|
58
59
|
componentLoader: () => import('./pages/RetrievalTracePage'),
|
|
59
60
|
});
|
|
60
61
|
|
|
61
62
|
this.pluginSettingsManager.addPageTabItem({
|
|
62
|
-
menuKey: 'ai
|
|
63
|
+
menuKey: 'ai-orchestrator',
|
|
63
64
|
key: 'memory-inspector',
|
|
64
65
|
title: this.t('Memory Inspector'),
|
|
65
66
|
componentLoader: () => import('./pages/MemoryInspectorPage'),
|
|
66
67
|
});
|
|
67
68
|
|
|
68
69
|
this.pluginSettingsManager.addPageTabItem({
|
|
69
|
-
menuKey: 'ai
|
|
70
|
+
menuKey: 'ai-orchestrator',
|
|
70
71
|
key: 'skill-definitions',
|
|
71
72
|
title: this.t('Skill Hub Definitions'),
|
|
72
73
|
componentLoader: () => import('./pages/SkillDefinitionsPage'),
|
|
73
74
|
});
|
|
74
75
|
|
|
75
76
|
this.pluginSettingsManager.addPageTabItem({
|
|
76
|
-
menuKey: 'ai
|
|
77
|
+
menuKey: 'ai-orchestrator',
|
|
77
78
|
key: 'skill-executions',
|
|
78
79
|
title: this.t('Execution History'),
|
|
79
80
|
componentLoader: () => import('./pages/ExecutionHistoryPage'),
|
|
80
81
|
});
|
|
81
82
|
|
|
82
83
|
this.pluginSettingsManager.addPageTabItem({
|
|
83
|
-
menuKey: 'ai
|
|
84
|
+
menuKey: 'ai-orchestrator',
|
|
84
85
|
key: 'skill-loop-settings',
|
|
85
86
|
title: this.t('Skill Review Settings'),
|
|
86
87
|
componentLoader: () => import('./pages/LoopSettingsPage'),
|
|
87
88
|
});
|
|
88
89
|
|
|
89
90
|
this.pluginSettingsManager.addPageTabItem({
|
|
90
|
-
menuKey: 'ai
|
|
91
|
+
menuKey: 'ai-orchestrator',
|
|
91
92
|
key: 'skill-metrics',
|
|
92
93
|
title: this.t('Metrics'),
|
|
93
94
|
componentLoader: () => import('./pages/SkillMetricsPage'),
|
|
@@ -33,6 +33,7 @@ function mockDatabase(options: { steps?: Row[]; messages?: Row[] } = {}) {
|
|
|
33
33
|
return {
|
|
34
34
|
find: async ({ filter }: { filter: Row }) => steps.filter((row) => matches(row, filter)),
|
|
35
35
|
findOne: async ({ filter }: { filter: Row }) => steps.find((row) => matches(row, filter)) ?? null,
|
|
36
|
+
count: async ({ filter }: { filter: Row }) => steps.filter((row) => matches(row, filter)).length,
|
|
36
37
|
create: async ({ values }: { values: Row }) => {
|
|
37
38
|
const row = { id: nextId++, ...values };
|
|
38
39
|
steps.push(row);
|
|
@@ -301,6 +301,13 @@ export default defineCollection({
|
|
|
301
301
|
type: 'json',
|
|
302
302
|
defaultValue: {},
|
|
303
303
|
},
|
|
304
|
+
{
|
|
305
|
+
name: 'progress',
|
|
306
|
+
type: 'json',
|
|
307
|
+
defaultValue: {},
|
|
308
|
+
comment:
|
|
309
|
+
'Live progress snapshot: { currentPhase, totalPhases, completedPhases, currentStepTitle, currentRole, estimatedMsRemaining }',
|
|
310
|
+
},
|
|
304
311
|
{
|
|
305
312
|
name: 'userId',
|
|
306
313
|
type: 'bigInt',
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defineCollection } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default defineCollection({
|
|
4
|
+
name: 'skillMetrics',
|
|
5
|
+
title: 'Skill Metrics',
|
|
6
|
+
fields: [
|
|
7
|
+
{ name: 'id', type: 'bigInt', autoIncrement: true, primaryKey: true },
|
|
8
|
+
{ name: 'skillId', type: 'bigInt', allowNull: false },
|
|
9
|
+
{
|
|
10
|
+
name: 'skill',
|
|
11
|
+
type: 'belongsTo',
|
|
12
|
+
target: 'skillDefinitions',
|
|
13
|
+
foreignKey: 'skillId',
|
|
14
|
+
onDelete: 'CASCADE',
|
|
15
|
+
},
|
|
16
|
+
{ name: 'employeeUsername', type: 'string', length: 100, defaultValue: '' },
|
|
17
|
+
{ name: 'period', type: 'date', allowNull: false, comment: 'Daily aggregation date (UTC midnight).' },
|
|
18
|
+
{ name: 'executions', type: 'integer', defaultValue: 0 },
|
|
19
|
+
{ name: 'successes', type: 'integer', defaultValue: 0 },
|
|
20
|
+
{ name: 'failures', type: 'integer', defaultValue: 0 },
|
|
21
|
+
{ name: 'timeouts', type: 'integer', defaultValue: 0 },
|
|
22
|
+
{ name: 'avgDurationMs', type: 'integer', defaultValue: 0 },
|
|
23
|
+
{ name: 'p95DurationMs', type: 'integer', defaultValue: 0 },
|
|
24
|
+
{ name: 'totalDurationMs', type: 'bigInt', defaultValue: 0 },
|
|
25
|
+
{ name: 'createdAt', type: 'date' },
|
|
26
|
+
{ name: 'updatedAt', type: 'date' },
|
|
27
|
+
],
|
|
28
|
+
indexes: [
|
|
29
|
+
{
|
|
30
|
+
unique: true,
|
|
31
|
+
fields: ['skillId', 'employeeUsername', 'period'],
|
|
32
|
+
},
|
|
33
|
+
{ fields: ['period'] },
|
|
34
|
+
{ fields: ['skillId'] },
|
|
35
|
+
],
|
|
36
|
+
});
|
package/src/server/plugin.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { AgentRuntimeLifecycle } from './services/AgentRuntimeLifecycle';
|
|
|
24
24
|
import { validateInputSchemaDefinition } from './services/InputSchemaValidator';
|
|
25
25
|
import { assertSkillToolNameAvailable, buildSkillToolName, getSkillToolName } from './utils/skill-tool-name';
|
|
26
26
|
import { ensureAgentOrchestratorIndexes } from './utils/ensure-indexes';
|
|
27
|
+
import { resetRunEventBus } from './services/RunEventBus';
|
|
27
28
|
|
|
28
29
|
function normalizeOptionalString(value: unknown) {
|
|
29
30
|
return typeof value === 'string' ? value.trim() : '';
|
|
@@ -333,6 +334,21 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
333
334
|
|
|
334
335
|
await next();
|
|
335
336
|
|
|
337
|
+
// When a skill is disabled via update, cascade-remove its tool from all AI employee
|
|
338
|
+
// bindings. This prevents stale bindings from allowing execution of disabled skills.
|
|
339
|
+
if (actionName === 'update' && values.enabled === false) {
|
|
340
|
+
const tk = ctx.action?.params?.filterByTk;
|
|
341
|
+
if (tk) {
|
|
342
|
+
const skillRecord = await ctx.db.getRepository('skillDefinitions').findOne({ filterByTk: tk });
|
|
343
|
+
if (skillRecord) {
|
|
344
|
+
const toolName = getSkillToolName(skillRecord);
|
|
345
|
+
if (toolName) {
|
|
346
|
+
await this.skillHub.skillAccessService.cascadeDisableSkill(toolName);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
336
352
|
if ((actionName === 'list' || actionName === 'get') && !isAdminUser(ctx)) {
|
|
337
353
|
ctx.body = redactSkillResponse(ctx.body);
|
|
338
354
|
}
|
|
@@ -362,7 +378,7 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
362
378
|
const patterns = new LoopPatternService(this.db, employeeHarnessResolver(this), async () =>
|
|
363
379
|
worktreeCapability(this),
|
|
364
380
|
);
|
|
365
|
-
const triggers = new LoopTriggerService(this.db, patterns);
|
|
381
|
+
const triggers = new LoopTriggerService(this.db, patterns, this.app.log);
|
|
366
382
|
this.loopScheduler = new LoopSchedulerService(this, patterns, triggers);
|
|
367
383
|
this.loopWorker = new LoopWorkerService(this.app, this.db);
|
|
368
384
|
this.loopScheduler.install();
|
|
@@ -414,10 +430,109 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
414
430
|
},
|
|
415
431
|
});
|
|
416
432
|
|
|
433
|
+
// --- Skill Metrics Aggregation ---
|
|
434
|
+
// Daily roll-up of skillExecutions into per-skill/per-employee metrics for
|
|
435
|
+
// operational dashboards. Override schedule via env: SKILL_METRICS_CRON (default 03:00).
|
|
436
|
+
this.app.cronJobManager.addJob({
|
|
437
|
+
cronTime: process.env.SKILL_METRICS_CRON || '0 0 3 * * *',
|
|
438
|
+
onTick: async () => {
|
|
439
|
+
try {
|
|
440
|
+
await this.aggregateSkillMetrics();
|
|
441
|
+
} catch (e) {
|
|
442
|
+
this.app.log.error('[AgentOrchestrator] Skill metrics aggregation job failed', e);
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
});
|
|
446
|
+
|
|
417
447
|
// Native sub-agent conversations are owned by @nocobase/plugin-ai. This
|
|
418
448
|
// plugin only observes their execution spans and policy context.
|
|
419
449
|
}
|
|
420
450
|
|
|
451
|
+
private async aggregateSkillMetrics() {
|
|
452
|
+
const yesterday = new Date(Date.now() - 86400000);
|
|
453
|
+
yesterday.setUTCHours(0, 0, 0, 0);
|
|
454
|
+
const today = new Date(yesterday.getTime() + 86400000);
|
|
455
|
+
|
|
456
|
+
const repo = this.db.getRepository('skillExecutions');
|
|
457
|
+
if (!repo) return;
|
|
458
|
+
|
|
459
|
+
const rows = await repo.find({
|
|
460
|
+
filter: {
|
|
461
|
+
createdAt: {
|
|
462
|
+
$gte: yesterday.toISOString(),
|
|
463
|
+
$lt: today.toISOString(),
|
|
464
|
+
},
|
|
465
|
+
},
|
|
466
|
+
fields: ['skillId', 'aiEmployeeUsername', 'status', 'durationMs'],
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
type MetricKey = string;
|
|
470
|
+
interface MetricBucket {
|
|
471
|
+
executions: number;
|
|
472
|
+
successes: number;
|
|
473
|
+
failures: number;
|
|
474
|
+
timeouts: number;
|
|
475
|
+
durations: number[];
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const buckets = new Map<MetricKey, MetricBucket>();
|
|
479
|
+
|
|
480
|
+
for (const row of rows) {
|
|
481
|
+
const skillId = String(row.get?.('skillId') || '');
|
|
482
|
+
const username = String(row.get?.('aiEmployeeUsername') || '');
|
|
483
|
+
const status = String(row.get?.('status') || '');
|
|
484
|
+
const durationMs = Number(row.get?.('durationMs') || 0);
|
|
485
|
+
if (!skillId) continue;
|
|
486
|
+
|
|
487
|
+
const key = `${skillId}:${username}`;
|
|
488
|
+
if (!buckets.has(key)) {
|
|
489
|
+
buckets.set(key, { executions: 0, successes: 0, failures: 0, timeouts: 0, durations: [] });
|
|
490
|
+
}
|
|
491
|
+
const bucket = buckets.get(key) ?? { executions: 0, successes: 0, failures: 0, timeouts: 0, durations: [] };
|
|
492
|
+
bucket.executions += 1;
|
|
493
|
+
if (status === 'succeeded') bucket.successes += 1;
|
|
494
|
+
else if (status === 'timeout') bucket.timeouts += 1;
|
|
495
|
+
else if (status === 'failed') bucket.failures += 1;
|
|
496
|
+
if (durationMs > 0) bucket.durations.push(durationMs);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const metricsRepo = this.db.getRepository('skillMetrics');
|
|
500
|
+
if (!metricsRepo) return;
|
|
501
|
+
|
|
502
|
+
let upserted = 0;
|
|
503
|
+
for (const [key, bucket] of buckets) {
|
|
504
|
+
const firstColon = key.indexOf(':');
|
|
505
|
+
const skillId = key.substring(0, firstColon);
|
|
506
|
+
const employeeUsername = key.substring(firstColon + 1);
|
|
507
|
+
const sortedDurations = [...bucket.durations].sort((a, b) => a - b);
|
|
508
|
+
const p95Index = Math.max(0, Math.ceil(sortedDurations.length * 0.95) - 1);
|
|
509
|
+
const totalDuration = sortedDurations.reduce((sum, d) => sum + d, 0);
|
|
510
|
+
|
|
511
|
+
await metricsRepo.updateOrCreate({
|
|
512
|
+
filterKeys: ['skillId', 'employeeUsername', 'period'],
|
|
513
|
+
values: {
|
|
514
|
+
skillId: skillId,
|
|
515
|
+
employeeUsername,
|
|
516
|
+
period: yesterday.toISOString(),
|
|
517
|
+
executions: bucket.executions,
|
|
518
|
+
successes: bucket.successes,
|
|
519
|
+
failures: bucket.failures,
|
|
520
|
+
timeouts: bucket.timeouts,
|
|
521
|
+
avgDurationMs: bucket.durations.length ? Math.round(totalDuration / bucket.durations.length) : 0,
|
|
522
|
+
p95DurationMs: sortedDurations.length > 1 ? sortedDurations[p95Index] : sortedDurations[0] || 0,
|
|
523
|
+
totalDurationMs: totalDuration,
|
|
524
|
+
},
|
|
525
|
+
});
|
|
526
|
+
upserted += 1;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
this.app.log.info(
|
|
530
|
+
`[AgentOrchestrator] Skill metrics aggregated for ${yesterday
|
|
531
|
+
.toISOString()
|
|
532
|
+
.slice(0, 10)}: ${upserted} metric row(s).`,
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
421
536
|
async install() {
|
|
422
537
|
await this.skillHub.install();
|
|
423
538
|
await ensureAgentOrchestratorIndexes(this.db);
|
|
@@ -442,6 +557,7 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
442
557
|
await this.loopScheduler?.dispose();
|
|
443
558
|
this.nativeObserver?.uninstall();
|
|
444
559
|
await this.skillHub.beforeStop();
|
|
560
|
+
resetRunEventBus();
|
|
445
561
|
}
|
|
446
562
|
|
|
447
563
|
async handleSyncMessage(message: unknown) {
|
|
@@ -1,105 +1,108 @@
|
|
|
1
|
-
import type { Context } from '@nocobase/actions';
|
|
2
|
-
import type { Plugin } from '@nocobase/server';
|
|
3
|
-
import { LoopRunRepository } from '../services/LoopRunRepository';
|
|
4
|
-
import { getRunEventBus } from '../services/RunEventBus';
|
|
5
|
-
import { requestActor, throwResourceError } from './resource-helpers';
|
|
6
|
-
|
|
7
|
-
const POLL_INTERVAL_MS = 1500;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
function
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
ctx.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
1
|
+
import type { Context } from '@nocobase/actions';
|
|
2
|
+
import type { Plugin } from '@nocobase/server';
|
|
3
|
+
import { LoopRunRepository } from '../services/LoopRunRepository';
|
|
4
|
+
import { getRunEventBus } from '../services/RunEventBus';
|
|
5
|
+
import { requestActor, throwResourceError } from './resource-helpers';
|
|
6
|
+
|
|
7
|
+
const POLL_INTERVAL_MS = 1500;
|
|
8
|
+
// Send a keepalive comment every 15 seconds to prevent reverse proxies from
|
|
9
|
+
// closing idle SSE connections (nginx default: 60s).
|
|
10
|
+
const KEEPALIVE_INTERVAL_MS = 15_000;
|
|
11
|
+
|
|
12
|
+
function eventId(event: unknown) {
|
|
13
|
+
if (!event || typeof event !== 'object') return 0;
|
|
14
|
+
const id = Number((event as Record<string, unknown>).id);
|
|
15
|
+
return Number.isSafeInteger(id) && id > 0 ? id : 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function plainEvent(event: unknown) {
|
|
19
|
+
if (!event || typeof event !== 'object') return event;
|
|
20
|
+
const record = event as { toJSON?: () => unknown };
|
|
21
|
+
return typeof record.toJSON === 'function' ? record.toJSON() : event;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function writeEvent(ctx: Context, event: unknown) {
|
|
25
|
+
const id = eventId(event);
|
|
26
|
+
if (id > 0) ctx.res.write(`id: ${id}\n`);
|
|
27
|
+
ctx.res.write(`data: ${JSON.stringify(plainEvent(event))}\n\n`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function registerAgentLoopEventsStreamResource(plugin: Plugin) {
|
|
31
|
+
const repository = new LoopRunRepository(plugin.db);
|
|
32
|
+
|
|
33
|
+
plugin.app.resource({
|
|
34
|
+
name: 'agentLoopEventsStream',
|
|
35
|
+
actions: {
|
|
36
|
+
async stream(ctx, next) {
|
|
37
|
+
try {
|
|
38
|
+
const actor = requestActor(ctx);
|
|
39
|
+
const runId = Number(ctx.action.params.runId || ctx.action.params.filterByTk);
|
|
40
|
+
if (!Number.isSafeInteger(runId) || runId <= 0) ctx.throw(400, 'runId is required');
|
|
41
|
+
await repository.requireOwnedRun(runId, actor.userId, actor.isAdmin);
|
|
42
|
+
|
|
43
|
+
ctx.set({
|
|
44
|
+
'Content-Type': 'text/event-stream',
|
|
45
|
+
'Cache-Control': 'no-cache',
|
|
46
|
+
Connection: 'keep-alive',
|
|
47
|
+
});
|
|
48
|
+
ctx.status = 200;
|
|
49
|
+
|
|
50
|
+
let closed = false;
|
|
51
|
+
let lastEventId = Number(ctx.get('Last-Event-ID')) || 0;
|
|
52
|
+
const close = () => {
|
|
53
|
+
closed = true;
|
|
54
|
+
};
|
|
55
|
+
const send = (event: unknown) => {
|
|
56
|
+
const id = eventId(event);
|
|
57
|
+
if (closed || ctx.res.destroyed || ctx.res.writableEnded || (id > 0 && id <= lastEventId)) return;
|
|
58
|
+
writeEvent(ctx, event);
|
|
59
|
+
if (id > 0) lastEventId = id;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
ctx.req.once('aborted', close);
|
|
63
|
+
ctx.res.once('close', close);
|
|
64
|
+
const unsubscribe = getRunEventBus().subscribe(runId, send);
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const initial = await ctx.db.getRepository('agentLoopEvents').find({
|
|
68
|
+
filter: lastEventId > 0 ? { runId, id: { $gt: lastEventId } } : { runId },
|
|
69
|
+
sort: ['id'],
|
|
70
|
+
pageSize: 1000,
|
|
71
|
+
});
|
|
72
|
+
for (const event of initial) send(event);
|
|
73
|
+
|
|
74
|
+
await new Promise<void>((resolve) => {
|
|
75
|
+
const poll = async () => {
|
|
76
|
+
if (closed || ctx.res.destroyed || ctx.res.writableEnded) {
|
|
77
|
+
resolve();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const events = await ctx.db.getRepository('agentLoopEvents').find({
|
|
82
|
+
filter: { runId, id: { $gt: lastEventId } },
|
|
83
|
+
sort: ['id'],
|
|
84
|
+
pageSize: 1000,
|
|
85
|
+
});
|
|
86
|
+
for (const event of events) send(event);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
ctx.log.error('[AgentOrchestrator] Failed to poll loop events', error);
|
|
89
|
+
}
|
|
90
|
+
if (closed || ctx.res.destroyed || ctx.res.writableEnded) resolve();
|
|
91
|
+
else setTimeout(poll, POLL_INTERVAL_MS);
|
|
92
|
+
};
|
|
93
|
+
setTimeout(poll, POLL_INTERVAL_MS);
|
|
94
|
+
});
|
|
95
|
+
} finally {
|
|
96
|
+
unsubscribe();
|
|
97
|
+
ctx.req.off('aborted', close);
|
|
98
|
+
ctx.res.off('close', close);
|
|
99
|
+
if (!ctx.res.destroyed && !ctx.res.writableEnded) ctx.res.end();
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throwResourceError(ctx, error);
|
|
103
|
+
}
|
|
104
|
+
await next();
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
@@ -81,7 +81,7 @@ export function registerAgentLoopResource(plugin: LoopResourcePlugin) {
|
|
|
81
81
|
const patterns = new LoopPatternService(plugin.db, employeeHarnessResolver(plugin), async () =>
|
|
82
82
|
worktreeCapability(plugin),
|
|
83
83
|
);
|
|
84
|
-
const triggers = new LoopTriggerService(plugin.db, patterns);
|
|
84
|
+
const triggers = new LoopTriggerService(plugin.db, patterns, plugin.app.log);
|
|
85
85
|
|
|
86
86
|
plugin.app.resource({
|
|
87
87
|
name: 'agentLoops',
|