plugin-agent-orchestrator 1.0.32 → 1.0.35
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/README.md +114 -114
- package/dist/externalVersion.js +6 -6
- package/dist/server/collections/skill-executions.js +21 -0
- package/dist/server/plugin.js +132 -3
- package/dist/server/resources/agent-loop.js +1 -2
- package/dist/server/services/AgentLoopController.js +39 -1
- package/dist/server/services/AgentLoopService.js +3 -0
- package/dist/server/services/AgentRuntimeLifecycle.js +70 -0
- package/dist/server/services/FileManager.js +12 -0
- package/dist/server/services/NativeSubAgentObserver.js +20 -0
- package/dist/server/services/PlanningPromptService.js +95 -0
- package/dist/server/services/SandboxRunner.js +251 -56
- package/dist/server/services/TokenTracker.js +20 -0
- package/dist/server/services/WorkerEnvManager.js +76 -10
- package/dist/server/services/pythonGuardTemplate.js +105 -0
- package/dist/server/services/sandboxCapabilities.js +286 -0
- package/dist/server/skill-hub/plugin.js +98 -24
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +53 -11
- package/dist/server/utils/skill-settings.js +1 -1
- package/package.json +47 -47
- package/src/client-v2/components/AgentRunsTab.tsx +494 -494
- package/src/client-v2/components/TracingTab.tsx +432 -432
- package/src/client-v2/hooks/useApiRequest.ts +121 -121
- package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -0
- package/src/server/__tests__/planning-prompt-service.test.ts +23 -0
- package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -0
- package/src/server/collections/skill-executions.ts +21 -0
- package/src/server/plugin.ts +151 -0
- package/src/server/resources/agent-loop.ts +1 -2
- package/src/server/resources/agent-monitor.ts +482 -482
- package/src/server/services/AgentLoopController.ts +50 -1
- package/src/server/services/AgentLoopService.ts +4 -0
- package/src/server/services/AgentRuntimeLifecycle.ts +64 -0
- package/src/server/services/ExecutionSpanService.ts +110 -110
- package/src/server/services/FileManager.ts +16 -1
- package/src/server/services/NativeSubAgentObserver.ts +528 -507
- package/src/server/services/PlanningPromptService.ts +72 -0
- package/src/server/services/SandboxRunner.ts +341 -74
- package/src/server/services/TokenTracker.ts +26 -0
- package/src/server/services/WorkerEnvManager.ts +95 -9
- package/src/server/services/pythonGuardTemplate.ts +87 -0
- package/src/server/services/sandboxCapabilities.ts +348 -0
- package/src/server/skill-hub/plugin.ts +106 -23
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +51 -6
- package/src/server/utils/skill-settings.ts +1 -0
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import { useApp } from '@nocobase/client-v2';
|
|
2
|
-
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* client-v2 adapters for the two data-layer hooks the v1 admin tabs relied on
|
|
6
|
-
* (`useAPIClient` and `useRequest` from `@nocobase/client`). client-v2 exposes
|
|
7
|
-
* neither, so these reimplement just the slice the orchestrator UI uses on top
|
|
8
|
-
* of `useApp().apiClient`, keeping the call sites unchanged.
|
|
9
|
-
*
|
|
10
|
-
* v1 contract preserved here:
|
|
11
|
-
* - `useRequest` resolves `data` to the response BODY (`response.data`), so a
|
|
12
|
-
* list endpoint returning `{ data: rows, meta }` is read as `result.data.data`
|
|
13
|
-
* / `result.data.meta` — identical to the v1 tabs.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export type ApiRequestService =
|
|
17
|
-
| {
|
|
18
|
-
url: string;
|
|
19
|
-
method?: string;
|
|
20
|
-
params?: Record<string, unknown>;
|
|
21
|
-
data?: unknown;
|
|
22
|
-
}
|
|
23
|
-
| (() => Promise<unknown>);
|
|
24
|
-
|
|
25
|
-
export type ApiRequestOptions = {
|
|
26
|
-
/** Do not fire on mount; caller triggers via `run`. */
|
|
27
|
-
manual?: boolean;
|
|
28
|
-
/** Re-run whenever any dependency changes (mirrors ahooks refreshDeps). */
|
|
29
|
-
refreshDeps?: unknown[];
|
|
30
|
-
onSuccess?: (data: unknown) => void;
|
|
31
|
-
onError?: (error: unknown) => void;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type ApiRequestResult<TData = unknown> = {
|
|
35
|
-
data: TData | undefined;
|
|
36
|
-
loading: boolean;
|
|
37
|
-
error: unknown;
|
|
38
|
-
refresh: () => void;
|
|
39
|
-
run: (overrideParams?: Record<string, unknown>) => Promise<TData | undefined>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/** Returns the v2 application's API client (replacement for v1 `useAPIClient`). */
|
|
43
|
-
export function useApiClient() {
|
|
44
|
-
return useApp().apiClient;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Minimal `useRequest` replacement. Supports object services
|
|
49
|
-
* (`{ url, method, params }`) and function services, plus `manual`,
|
|
50
|
-
* `refreshDeps`, and `onSuccess`/`onError` — the only options the tabs use.
|
|
51
|
-
*/
|
|
52
|
-
export function useRequest<TData = unknown>(
|
|
53
|
-
service: ApiRequestService,
|
|
54
|
-
options: ApiRequestOptions = {},
|
|
55
|
-
): ApiRequestResult<TData> {
|
|
56
|
-
const api = useApiClient();
|
|
57
|
-
const { manual = false, refreshDeps = [], onSuccess, onError } = options;
|
|
58
|
-
|
|
59
|
-
const [data, setData] = useState<TData | undefined>(undefined);
|
|
60
|
-
const [loading, setLoading] = useState<boolean>(!manual);
|
|
61
|
-
const [error, setError] = useState<unknown>(undefined);
|
|
62
|
-
|
|
63
|
-
// Keep the latest service/callbacks without forcing them into refreshDeps.
|
|
64
|
-
const serviceRef = useRef(service);
|
|
65
|
-
serviceRef.current = service;
|
|
66
|
-
const onSuccessRef = useRef(onSuccess);
|
|
67
|
-
onSuccessRef.current = onSuccess;
|
|
68
|
-
const onErrorRef = useRef(onError);
|
|
69
|
-
onErrorRef.current = onError;
|
|
70
|
-
|
|
71
|
-
const run = useCallback(
|
|
72
|
-
async (overrideParams?: Record<string, unknown>): Promise<TData | undefined> => {
|
|
73
|
-
const current = serviceRef.current;
|
|
74
|
-
setLoading(true);
|
|
75
|
-
setError(undefined);
|
|
76
|
-
try {
|
|
77
|
-
let body: TData;
|
|
78
|
-
if (typeof current === 'function') {
|
|
79
|
-
body = (await current()) as TData;
|
|
80
|
-
} else {
|
|
81
|
-
const response = await api.request({
|
|
82
|
-
url: current.url,
|
|
83
|
-
method: current.method || 'get',
|
|
84
|
-
params: { ...(current.params || {}), ...(overrideParams || {}) },
|
|
85
|
-
data: current.data,
|
|
86
|
-
});
|
|
87
|
-
const rawData = response?.data;
|
|
88
|
-
body = (rawData &&
|
|
89
|
-
typeof rawData === 'object' &&
|
|
90
|
-
rawData.data &&
|
|
91
|
-
typeof rawData.data === 'object' &&
|
|
92
|
-
'data' in rawData.data
|
|
93
|
-
? rawData.data
|
|
94
|
-
: rawData) as TData;
|
|
95
|
-
}
|
|
96
|
-
setData(body);
|
|
97
|
-
onSuccessRef.current?.(body);
|
|
98
|
-
return body;
|
|
99
|
-
} catch (err) {
|
|
100
|
-
setError(err);
|
|
101
|
-
onErrorRef.current?.(err);
|
|
102
|
-
return undefined;
|
|
103
|
-
} finally {
|
|
104
|
-
setLoading(false);
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
[api],
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const refresh = useCallback(() => {
|
|
111
|
-
run();
|
|
112
|
-
}, [run]);
|
|
113
|
-
|
|
114
|
-
useEffect(() => {
|
|
115
|
-
if (manual) return;
|
|
116
|
-
run();
|
|
117
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
118
|
-
}, refreshDeps);
|
|
119
|
-
|
|
120
|
-
return { data, loading, error, refresh, run };
|
|
121
|
-
}
|
|
1
|
+
import { useApp } from '@nocobase/client-v2';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* client-v2 adapters for the two data-layer hooks the v1 admin tabs relied on
|
|
6
|
+
* (`useAPIClient` and `useRequest` from `@nocobase/client`). client-v2 exposes
|
|
7
|
+
* neither, so these reimplement just the slice the orchestrator UI uses on top
|
|
8
|
+
* of `useApp().apiClient`, keeping the call sites unchanged.
|
|
9
|
+
*
|
|
10
|
+
* v1 contract preserved here:
|
|
11
|
+
* - `useRequest` resolves `data` to the response BODY (`response.data`), so a
|
|
12
|
+
* list endpoint returning `{ data: rows, meta }` is read as `result.data.data`
|
|
13
|
+
* / `result.data.meta` — identical to the v1 tabs.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export type ApiRequestService =
|
|
17
|
+
| {
|
|
18
|
+
url: string;
|
|
19
|
+
method?: string;
|
|
20
|
+
params?: Record<string, unknown>;
|
|
21
|
+
data?: unknown;
|
|
22
|
+
}
|
|
23
|
+
| (() => Promise<unknown>);
|
|
24
|
+
|
|
25
|
+
export type ApiRequestOptions = {
|
|
26
|
+
/** Do not fire on mount; caller triggers via `run`. */
|
|
27
|
+
manual?: boolean;
|
|
28
|
+
/** Re-run whenever any dependency changes (mirrors ahooks refreshDeps). */
|
|
29
|
+
refreshDeps?: unknown[];
|
|
30
|
+
onSuccess?: (data: unknown) => void;
|
|
31
|
+
onError?: (error: unknown) => void;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ApiRequestResult<TData = unknown> = {
|
|
35
|
+
data: TData | undefined;
|
|
36
|
+
loading: boolean;
|
|
37
|
+
error: unknown;
|
|
38
|
+
refresh: () => void;
|
|
39
|
+
run: (overrideParams?: Record<string, unknown>) => Promise<TData | undefined>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Returns the v2 application's API client (replacement for v1 `useAPIClient`). */
|
|
43
|
+
export function useApiClient() {
|
|
44
|
+
return useApp().apiClient;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Minimal `useRequest` replacement. Supports object services
|
|
49
|
+
* (`{ url, method, params }`) and function services, plus `manual`,
|
|
50
|
+
* `refreshDeps`, and `onSuccess`/`onError` — the only options the tabs use.
|
|
51
|
+
*/
|
|
52
|
+
export function useRequest<TData = unknown>(
|
|
53
|
+
service: ApiRequestService,
|
|
54
|
+
options: ApiRequestOptions = {},
|
|
55
|
+
): ApiRequestResult<TData> {
|
|
56
|
+
const api = useApiClient();
|
|
57
|
+
const { manual = false, refreshDeps = [], onSuccess, onError } = options;
|
|
58
|
+
|
|
59
|
+
const [data, setData] = useState<TData | undefined>(undefined);
|
|
60
|
+
const [loading, setLoading] = useState<boolean>(!manual);
|
|
61
|
+
const [error, setError] = useState<unknown>(undefined);
|
|
62
|
+
|
|
63
|
+
// Keep the latest service/callbacks without forcing them into refreshDeps.
|
|
64
|
+
const serviceRef = useRef(service);
|
|
65
|
+
serviceRef.current = service;
|
|
66
|
+
const onSuccessRef = useRef(onSuccess);
|
|
67
|
+
onSuccessRef.current = onSuccess;
|
|
68
|
+
const onErrorRef = useRef(onError);
|
|
69
|
+
onErrorRef.current = onError;
|
|
70
|
+
|
|
71
|
+
const run = useCallback(
|
|
72
|
+
async (overrideParams?: Record<string, unknown>): Promise<TData | undefined> => {
|
|
73
|
+
const current = serviceRef.current;
|
|
74
|
+
setLoading(true);
|
|
75
|
+
setError(undefined);
|
|
76
|
+
try {
|
|
77
|
+
let body: TData;
|
|
78
|
+
if (typeof current === 'function') {
|
|
79
|
+
body = (await current()) as TData;
|
|
80
|
+
} else {
|
|
81
|
+
const response = await api.request({
|
|
82
|
+
url: current.url,
|
|
83
|
+
method: current.method || 'get',
|
|
84
|
+
params: { ...(current.params || {}), ...(overrideParams || {}) },
|
|
85
|
+
data: current.data,
|
|
86
|
+
});
|
|
87
|
+
const rawData = response?.data;
|
|
88
|
+
body = (rawData &&
|
|
89
|
+
typeof rawData === 'object' &&
|
|
90
|
+
rawData.data &&
|
|
91
|
+
typeof rawData.data === 'object' &&
|
|
92
|
+
'data' in rawData.data
|
|
93
|
+
? rawData.data
|
|
94
|
+
: rawData) as TData;
|
|
95
|
+
}
|
|
96
|
+
setData(body);
|
|
97
|
+
onSuccessRef.current?.(body);
|
|
98
|
+
return body;
|
|
99
|
+
} catch (err) {
|
|
100
|
+
setError(err);
|
|
101
|
+
onErrorRef.current?.(err);
|
|
102
|
+
return undefined;
|
|
103
|
+
} finally {
|
|
104
|
+
setLoading(false);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
[api],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const refresh = useCallback(() => {
|
|
111
|
+
run();
|
|
112
|
+
}, [run]);
|
|
113
|
+
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (manual) return;
|
|
116
|
+
run();
|
|
117
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
118
|
+
}, refreshDeps);
|
|
119
|
+
|
|
120
|
+
return { data, loading, error, refresh, run };
|
|
121
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AgentRuntimeLifecycle } from '../services/AgentRuntimeLifecycle';
|
|
2
|
+
|
|
3
|
+
describe('AgentRuntimeLifecycle', () => {
|
|
4
|
+
it('runs registered hooks in registration order and supports cleanup', async () => {
|
|
5
|
+
const lifecycle = new AgentRuntimeLifecycle();
|
|
6
|
+
const calls: string[] = [];
|
|
7
|
+
const cleanup = lifecycle.registerBeforeRunHook('first', () => calls.push('first'));
|
|
8
|
+
lifecycle.registerBeforeRunHook('second', () => calls.push('second'));
|
|
9
|
+
const context = {
|
|
10
|
+
ctx: {},
|
|
11
|
+
source: 'api' as const,
|
|
12
|
+
employee: {},
|
|
13
|
+
sessionId: 'session-1',
|
|
14
|
+
messages: [],
|
|
15
|
+
metadata: {},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
await lifecycle.runBeforeHooks(context);
|
|
19
|
+
cleanup();
|
|
20
|
+
await lifecycle.runBeforeHooks(context);
|
|
21
|
+
|
|
22
|
+
expect(calls).toEqual(['first', 'second', 'second']);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('isolates a failed hook and continues with later hooks', async () => {
|
|
26
|
+
const failures: string[] = [];
|
|
27
|
+
const calls: string[] = [];
|
|
28
|
+
const lifecycle = new AgentRuntimeLifecycle((hookType, name) => failures.push(`${hookType}:${name}`));
|
|
29
|
+
lifecycle.registerBeforeRunHook('broken', () => {
|
|
30
|
+
throw new Error('boom');
|
|
31
|
+
});
|
|
32
|
+
lifecycle.registerBeforeRunHook('healthy', () => calls.push('healthy'));
|
|
33
|
+
|
|
34
|
+
await lifecycle.runBeforeHooks({
|
|
35
|
+
ctx: {},
|
|
36
|
+
source: 'api',
|
|
37
|
+
employee: {},
|
|
38
|
+
sessionId: 'session-1',
|
|
39
|
+
messages: [],
|
|
40
|
+
metadata: {},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(failures).toEqual(['beforeRun:broken']);
|
|
44
|
+
expect(calls).toEqual(['healthy']);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PlanningPromptService } from '../services/PlanningPromptService';
|
|
2
|
+
|
|
3
|
+
describe('PlanningPromptService', () => {
|
|
4
|
+
it('adds planning instructions as a system message without modifying the user request', () => {
|
|
5
|
+
const service = new PlanningPromptService();
|
|
6
|
+
const messages = [{ role: 'user', content: { type: 'text', content: 'Create a plan for the rollout' } }];
|
|
7
|
+
|
|
8
|
+
expect(service.applyPlanningContext(messages)).toBe(true);
|
|
9
|
+
expect(messages[0]).toMatchObject({ role: 'system' });
|
|
10
|
+
expect(messages[1]).toEqual({
|
|
11
|
+
role: 'user',
|
|
12
|
+
content: { type: 'text', content: 'Create a plan for the rollout' },
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('does not add planning context for a simple request', () => {
|
|
17
|
+
const service = new PlanningPromptService();
|
|
18
|
+
const messages = [{ role: 'user', content: 'Show the current status' }];
|
|
19
|
+
|
|
20
|
+
expect(service.applyPlanningContext(messages)).toBe(false);
|
|
21
|
+
expect(messages).toHaveLength(1);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { shouldRunSkillSandbox } from '../skill-hub/plugin';
|
|
2
|
+
|
|
3
|
+
describe('Skill Hub sandbox worker mode', () => {
|
|
4
|
+
it.each([
|
|
5
|
+
[{}, true],
|
|
6
|
+
[{ SKILL_HUB_SANDBOX: 'false' }, false],
|
|
7
|
+
[{ WORKER_MODE: 'main' }, false],
|
|
8
|
+
[{ WORKER_MODE: 'workflow:process' }, false],
|
|
9
|
+
[{ WORKER_MODE: 'skill-hub:sandbox' }, true],
|
|
10
|
+
[{ WORKER_MODE: 'skill-hub.task' }, true],
|
|
11
|
+
[{ WORKER_MODE: '*' }, true],
|
|
12
|
+
])('evaluates %o as %s', (env, expected) => {
|
|
13
|
+
expect(shouldRunSkillSandbox(env)).toBe(expected);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -50,6 +50,27 @@ export default {
|
|
|
50
50
|
name: 'durationMs',
|
|
51
51
|
type: 'integer',
|
|
52
52
|
},
|
|
53
|
+
{
|
|
54
|
+
name: 'startedAt',
|
|
55
|
+
type: 'date',
|
|
56
|
+
allowNull: true,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'heartbeatAt',
|
|
60
|
+
type: 'date',
|
|
61
|
+
allowNull: true,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'workerId',
|
|
65
|
+
type: 'string',
|
|
66
|
+
length: 200,
|
|
67
|
+
allowNull: true,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'retryCount',
|
|
71
|
+
type: 'integer',
|
|
72
|
+
defaultValue: 0,
|
|
73
|
+
},
|
|
53
74
|
{
|
|
54
75
|
name: 'sessionId',
|
|
55
76
|
type: 'string',
|
package/src/server/plugin.ts
CHANGED
|
@@ -3,10 +3,15 @@ import path from 'path';
|
|
|
3
3
|
import { createExternalRagSearchTool } from './tools/external-rag-search';
|
|
4
4
|
import { registerTracingResource } from './resources/tracing';
|
|
5
5
|
import { registerAgentMonitorResource } from './resources/agent-monitor';
|
|
6
|
+
import { registerAgentLoopResource } from './resources/agent-loop';
|
|
6
7
|
import SkillHubSubFeature from './skill-hub/plugin';
|
|
7
8
|
import { NativeSubAgentObserver } from './services/NativeSubAgentObserver';
|
|
9
|
+
import { AgentLoopService } from './services/AgentLoopService';
|
|
10
|
+
import { createAgentLoopTools } from './tools/agent-loop';
|
|
11
|
+
import { PlanningPromptService } from './services/PlanningPromptService';
|
|
8
12
|
import { asObject, isAdminUser, currentUserId } from './utils/ctx-utils';
|
|
9
13
|
import { getAIToolsManager } from './utils/ai-manager';
|
|
14
|
+
import { AgentRuntimeLifecycle } from './services/AgentRuntimeLifecycle';
|
|
10
15
|
|
|
11
16
|
function normalizeOptionalString(value: unknown) {
|
|
12
17
|
return typeof value === 'string' ? value.trim() : '';
|
|
@@ -123,6 +128,7 @@ async function resolveTracingRetentionDays(plugin: { db: any; app: any }) {
|
|
|
123
128
|
export class PluginAgentOrchestratorServer extends Plugin {
|
|
124
129
|
skillHub: SkillHubSubFeature;
|
|
125
130
|
nativeObserver: NativeSubAgentObserver;
|
|
131
|
+
agentRuntimeLifecycle: AgentRuntimeLifecycle;
|
|
126
132
|
private readonly installNativeObserver = () => {
|
|
127
133
|
this.nativeObserver?.install();
|
|
128
134
|
};
|
|
@@ -130,6 +136,13 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
130
136
|
async afterAdd() {
|
|
131
137
|
this.skillHub = new SkillHubSubFeature(this);
|
|
132
138
|
this.nativeObserver = new NativeSubAgentObserver(this);
|
|
139
|
+
this.agentRuntimeLifecycle = new AgentRuntimeLifecycle((hookType, name, error) => {
|
|
140
|
+
this.app.logger.warn(`[AgentOrchestrator] ${hookType} hook "${name}" failed; continuing agent execution.`, {
|
|
141
|
+
error,
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
(this.app as typeof this.app & { agentRuntimeLifecycle: AgentRuntimeLifecycle }).agentRuntimeLifecycle =
|
|
145
|
+
this.agentRuntimeLifecycle;
|
|
133
146
|
}
|
|
134
147
|
|
|
135
148
|
async beforeLoad() {
|
|
@@ -182,6 +195,7 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
182
195
|
this.app.acl.allow('skillHub', 'download', 'loggedIn');
|
|
183
196
|
this.app.acl.allow('skillHub', 'listTemplates', 'loggedIn');
|
|
184
197
|
this.app.acl.allow('agentMonitor', ['list', 'get'], 'loggedIn');
|
|
198
|
+
this.app.acl.allow('agentLoops', ['list', 'get'], 'loggedIn');
|
|
185
199
|
|
|
186
200
|
// Data scoping for skillExecutions: a logged-in non-admin user may only
|
|
187
201
|
// read their own executions. Rows hold inputArgs / stdout / output files,
|
|
@@ -214,6 +228,31 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
214
228
|
{ tag: 'orchestrator-agent-memory-context-policy', after: 'acl' },
|
|
215
229
|
);
|
|
216
230
|
|
|
231
|
+
// --- Planning Prompt Injection ---
|
|
232
|
+
// When a user message contains plan-related keywords, prepend planning
|
|
233
|
+
// instructions so the AI agent creates a plan via agent_loop_start before
|
|
234
|
+
// executing.
|
|
235
|
+
const planningPrompt = new PlanningPromptService();
|
|
236
|
+
this.agentRuntimeLifecycle.registerBeforeRunHook('orchestrator-planning', (context) => {
|
|
237
|
+
if (planningPrompt.applyPlanningContext(context.messages)) {
|
|
238
|
+
context.metadata.planningPromptInjected = true;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
this.app.resourceManager.use(
|
|
242
|
+
async (ctx, next) => {
|
|
243
|
+
const { resourceName, actionName } = ctx.action || {};
|
|
244
|
+
if (resourceName === 'aiConversations' && actionName === 'sendMessages') {
|
|
245
|
+
const values = ctx.action.params.values || {};
|
|
246
|
+
const messages = values.messages;
|
|
247
|
+
if (Array.isArray(messages)) {
|
|
248
|
+
planningPrompt.applyPlanningContext(messages);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
await next();
|
|
252
|
+
},
|
|
253
|
+
{ tag: 'orchestrator-planning-prompt', after: 'acl' },
|
|
254
|
+
);
|
|
255
|
+
|
|
217
256
|
// --- Register External Tool Only ---
|
|
218
257
|
// Native @nocobase/plugin-ai owns sub-agent dispatch. The orchestrator no
|
|
219
258
|
// longer registers delegate_* / dispatch_subagents_* tools or controller
|
|
@@ -221,11 +260,21 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
221
260
|
const toolsManager = getAIToolsManager(this.app);
|
|
222
261
|
toolsManager.registerTools(createExternalRagSearchTool(this));
|
|
223
262
|
|
|
263
|
+
// --- Register Agent Loop Tools & Resource ---
|
|
264
|
+
const agentLoopService = new AgentLoopService(this);
|
|
265
|
+
toolsManager.registerTools(createAgentLoopTools(this, agentLoopService));
|
|
266
|
+
registerAgentLoopResource(this, agentLoopService);
|
|
267
|
+
|
|
224
268
|
// --- Native plugin-ai Monitor Resource ---
|
|
225
269
|
registerAgentMonitorResource(this);
|
|
226
270
|
this.installNativeObserver();
|
|
227
271
|
this.app.on?.('afterStart', this.installNativeObserver);
|
|
228
272
|
|
|
273
|
+
// --- Crash Recovery ---
|
|
274
|
+
// On startup, scan for steps/runs left in "running" state from a previous
|
|
275
|
+
// crash and reset them so they can be retried.
|
|
276
|
+
this.app.on?.('afterStart', this.recoverAfterCrash);
|
|
277
|
+
|
|
229
278
|
// --- Register Tracing Resource (Phase 5) ---
|
|
230
279
|
// Custom read-only resource for the Swarm Tracing admin page.
|
|
231
280
|
registerTracingResource(this);
|
|
@@ -277,9 +326,111 @@ export class PluginAgentOrchestratorServer extends Plugin {
|
|
|
277
326
|
async beforeStop() {
|
|
278
327
|
this.app.off?.('afterStart', this.installNativeObserver);
|
|
279
328
|
this.app.removeListener?.('afterStart', this.installNativeObserver);
|
|
329
|
+
this.app.off?.('afterStart', this.recoverAfterCrash);
|
|
330
|
+
this.app.removeListener?.('afterStart', this.recoverAfterCrash);
|
|
280
331
|
this.nativeObserver?.uninstall();
|
|
281
332
|
await this.skillHub.beforeStop();
|
|
282
333
|
}
|
|
334
|
+
|
|
335
|
+
// --- Crash Recovery ---
|
|
336
|
+
// Scans for agentLoopSteps stuck in "running" status from a previous crash
|
|
337
|
+
// and resets them to "pending". Also cleans up orphaned agentLoopRuns where
|
|
338
|
+
// all steps have reached terminal states but the run itself wasn't finalized.
|
|
339
|
+
private readonly recoverAfterCrash = async () => {
|
|
340
|
+
const timeoutMs = Number(process.env.ORCHESTRATOR_CRASH_RECOVERY_TIMEOUT_MS || 600000); // 10 min default
|
|
341
|
+
const cutoff = new Date(Date.now() - timeoutMs);
|
|
342
|
+
|
|
343
|
+
try {
|
|
344
|
+
const stepsRepo = this.db.getRepository('agentLoopSteps');
|
|
345
|
+
const runsRepo = this.db.getRepository('agentLoopRuns');
|
|
346
|
+
|
|
347
|
+
// 1. Reset stale running steps back to pending
|
|
348
|
+
if (stepsRepo) {
|
|
349
|
+
const staleSteps = await stepsRepo.find({
|
|
350
|
+
filter: {
|
|
351
|
+
status: 'running',
|
|
352
|
+
startedAt: { $lt: cutoff.toISOString() },
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
for (const step of staleSteps) {
|
|
357
|
+
const stepId = readModelValue(step, 'id');
|
|
358
|
+
const runId = readModelValue(step, 'runId');
|
|
359
|
+
await stepsRepo.update({
|
|
360
|
+
filterByTk: stepId,
|
|
361
|
+
values: {
|
|
362
|
+
status: 'pending',
|
|
363
|
+
startedAt: null,
|
|
364
|
+
error: `Recovered after crash at ${new Date().toISOString()}`,
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
this.app.logger?.warn?.(
|
|
368
|
+
`[AgentOrchestrator] Crash recovery: reset step ${stepId} (run ${runId}) from running to pending`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (staleSteps.length > 0) {
|
|
373
|
+
this.app.logger?.info?.(
|
|
374
|
+
`[AgentOrchestrator] Crash recovery: reset ${staleSteps.length} stale running step(s)`,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// 2. Finalize orphaned runs — runs marked "running" where all steps are terminal
|
|
380
|
+
if (runsRepo) {
|
|
381
|
+
const runningRuns = await runsRepo.find({
|
|
382
|
+
filter: { status: 'running' },
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
for (const run of runningRuns) {
|
|
386
|
+
const runId = readModelValue(run, 'id');
|
|
387
|
+
const steps = await stepsRepo?.find({
|
|
388
|
+
filter: { runId },
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
if (!steps || steps.length === 0) {
|
|
392
|
+
// No steps at all — orphaned, mark as failed
|
|
393
|
+
await runsRepo.update({
|
|
394
|
+
filterByTk: runId,
|
|
395
|
+
values: {
|
|
396
|
+
status: 'failed',
|
|
397
|
+
endedAt: new Date(),
|
|
398
|
+
summary: 'Run was orphaned after server crash (no steps found).',
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
this.app.logger?.warn?.(
|
|
402
|
+
`[AgentOrchestrator] Crash recovery: marked orphaned run ${runId} as failed (no steps)`,
|
|
403
|
+
);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const allTerminal = steps.every((s: any) => {
|
|
408
|
+
const st = readModelValue(s, 'status');
|
|
409
|
+
return st === 'succeeded' || st === 'failed' || st === 'skipped';
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
if (!allTerminal) continue;
|
|
413
|
+
|
|
414
|
+
const anyFailed = steps.some((s: any) => readModelValue(s, 'status') === 'failed');
|
|
415
|
+
const finalStatus = anyFailed ? 'failed' : 'succeeded';
|
|
416
|
+
|
|
417
|
+
await runsRepo.update({
|
|
418
|
+
filterByTk: runId,
|
|
419
|
+
values: {
|
|
420
|
+
status: finalStatus,
|
|
421
|
+
endedAt: new Date(),
|
|
422
|
+
summary: `Run finalized as ${finalStatus} after server crash recovery.`,
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
this.app.logger?.warn?.(
|
|
426
|
+
`[AgentOrchestrator] Crash recovery: finalized orphaned run ${runId} as ${finalStatus}`,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
} catch (error) {
|
|
431
|
+
this.app.logger?.error?.('[AgentOrchestrator] Crash recovery scan failed', error);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
283
434
|
}
|
|
284
435
|
|
|
285
436
|
export default PluginAgentOrchestratorServer;
|
|
@@ -97,9 +97,8 @@ export function registerAgentLoopResource(plugin: Plugin, service: AgentLoopServ
|
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
ctx.body = {
|
|
100
|
-
data: await service.
|
|
100
|
+
data: await service.approvePlan(runId, {
|
|
101
101
|
userId: currentUserId(ctx),
|
|
102
|
-
ctx,
|
|
103
102
|
reason: body.reason,
|
|
104
103
|
}),
|
|
105
104
|
};
|