plugin-agent-orchestrator 1.2.0 → 1.2.1
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/LICENSE +201 -0
- package/dist/client/{117.2e4cb0477da3db1f.js → 117.6c69b6a87751dd18.js} +1 -1
- package/dist/client/{253.93deb9e0192e76a5.js → 253.52f62e1bc0e9520a.js} +1 -1
- package/dist/client/{404.498d2d9516dd1dce.js → 404.86593f98c9768ed5.js} +1 -1
- package/dist/client/{627.fdd60bcc0f3a9cf6.js → 453.fa2d169576262213.js} +1 -1
- package/dist/client/{53.774ff07505d521fa.js → 53.567db3ae078650c6.js} +1 -1
- package/dist/client/{619.e342b0fa467d39ca.js → 619.01713281e5e951d3.js} +1 -1
- package/dist/client/652.69da1598312b3a3f.js +10 -0
- package/dist/client/773.648965568ff476f9.js +10 -0
- package/dist/client/{981.e0546ffc34afedcd.js → 981.37e7aeecdb1ef6fa.js} +1 -1
- package/dist/client/{993.5d04b510835ab0d2.js → 993.ac89f3852990918d.js} +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client-v2/{117.ccf43ae8631f7d74.js → 117.c982d79ff68738f5.js} +1 -1
- package/dist/client-v2/{214.cdf76752053a8990.js → 214.94c4257bf2838577.js} +1 -1
- package/dist/client-v2/{253.1198df22f5baf304.js → 253.8e19956d2a44dfb8.js} +1 -1
- package/dist/client-v2/{264.78887ff88f969467.js → 264.80e8e358c8c6c2e5.js} +1 -1
- package/dist/client-v2/{404.9ba964c28a151c81.js → 404.ab032b79c95dab6f.js} +1 -1
- package/dist/client-v2/{41.b28cf7a675a603d1.js → 41.cd5610fcf7446127.js} +1 -1
- package/dist/client-v2/{627.e16f4a0dd596c3e6.js → 453.42c6d70628d56cc2.js} +1 -1
- package/dist/client-v2/{53.282081f97e4308c6.js → 53.37c08089a648f89a.js} +1 -1
- package/dist/client-v2/{619.8c03913873785fb6.js → 619.6e9618550101e819.js} +1 -1
- package/dist/client-v2/652.eeba4e0c38367321.js +10 -0
- package/dist/client-v2/{70.9166ed70906d041f.js → 70.332698e53eb59677.js} +1 -1
- package/dist/client-v2/773.4de2ba5acb07318c.js +10 -0
- package/dist/client-v2/981.76060f466e6f7c11.js +10 -0
- package/dist/client-v2/{993.ca5b28f1e2b4c1ef.js → 993.f8cdb5172566ce75.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +4 -1
- package/dist/locale/en-US.json +80 -1
- package/dist/locale/vi-VN.json +5 -1
- package/dist/locale/zh-CN.json +80 -1
- package/dist/node_modules/acorn/LICENSE +21 -0
- package/dist/node_modules/acorn/bin/acorn +4 -0
- package/dist/node_modules/acorn/dist/acorn.d.mts +857 -0
- package/dist/node_modules/acorn/dist/acorn.d.ts +857 -0
- package/dist/node_modules/acorn/dist/acorn.js +1 -0
- package/dist/node_modules/acorn/dist/bin.js +90 -0
- package/dist/node_modules/acorn/package.json +1 -0
- package/dist/node_modules/acorn-walk/LICENSE +21 -0
- package/dist/node_modules/acorn-walk/dist/walk.d.mts +177 -0
- package/dist/node_modules/acorn-walk/dist/walk.d.ts +177 -0
- package/dist/node_modules/acorn-walk/dist/walk.js +1 -0
- package/dist/node_modules/acorn-walk/node_modules/.bin/acorn +15 -0
- package/dist/node_modules/acorn-walk/node_modules/.bin/acorn.cmd +7 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/bin/acorn +4 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.d.mts +866 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.d.ts +866 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/acorn.js +6174 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/dist/bin.js +90 -0
- package/dist/node_modules/acorn-walk/node_modules/acorn/package.json +50 -0
- package/dist/node_modules/acorn-walk/package.json +1 -0
- package/dist/node_modules/adm-zip/package.json +1 -1
- package/dist/node_modules/ajv/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/collections/agent-auto-approval-checkpoints.js +47 -0
- package/dist/server/collections/agent-execution-spans.js +3 -3
- package/dist/server/collections/agent-hierarchy-invocations.js +59 -0
- package/dist/server/collections/agent-routing-graph-locks.js +40 -0
- package/dist/server/collections/agent-session-auto-approvals.js +45 -0
- package/dist/server/collections/orchestrator-config.js +17 -0
- package/dist/server/migrations/20261119000000-widen-native-employee-identifiers.js +59 -0
- package/dist/server/plugin.js +55 -0
- package/dist/server/resources/agent-auto-approval.js +80 -0
- package/dist/server/resources/hierarchical-routing.js +60 -0
- package/dist/server/services/AutoApprovalCheckpoint.js +133 -0
- package/dist/server/services/CodeValidator.js +8 -4
- package/dist/server/services/GovernanceAdmission.js +60 -0
- package/dist/server/services/HarnessCompiler.js +3 -0
- package/dist/server/services/HarnessSchema.js +13 -1
- package/dist/server/services/HierarchicalAgentRuntime.js +925 -0
- package/dist/server/services/HierarchyEnrollmentGuard.js +216 -0
- package/dist/server/services/HierarchyInvocationJournal.js +157 -0
- package/dist/server/services/HierarchyRequestCancellation.js +150 -0
- package/dist/server/services/LoopRunRepository.js +1 -1
- package/dist/server/services/LoopRunStateMachine.js +2 -2
- package/dist/server/services/LoopWorkerService.js +7 -7
- package/dist/server/services/PluginAiRuntimeAdapter.js +107 -9
- package/dist/server/services/RoutingBindingRepository.js +156 -0
- package/dist/server/services/RoutingBindingValidation.js +161 -0
- package/dist/server/services/SandboxRunner.js +19 -0
- package/dist/server/services/SessionAutoApproval.js +173 -0
- package/dist/server/services/VerificationService.js +2 -2
- package/dist/server/skill-hub/plugin.js +17 -0
- package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +2 -2
- package/dist/server/skill-hub/services/registry-node-package.js +191 -0
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +21 -6
- package/dist/server/tools/skill-execute.js +1 -0
- package/dist/server/utils/ensure-indexes.js +1 -0
- package/package.json +5 -2
- package/src/client/SkillPages.tsx +1 -8
- package/src/client/__tests__/embedded-settings.test.tsx +1 -8
- package/src/client/plugin.tsx +3 -8
- package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +21 -6
- package/src/client/skill-hub/tools/SkillHubCard.tsx +22 -16
- package/src/client/skill-hub/tools/__tests__/skill-hub-card.test.tsx +255 -0
- package/src/client/skill-hub/tools/registerSkillLoopCards.ts +12 -3
- package/src/client-v2/__tests__/embedded-settings.test.tsx +20 -11
- package/src/client-v2/__tests__/hierarchical-routing-settings.test.tsx +350 -0
- package/src/client-v2/auto-approval/AutoApprovalBridge.tsx +513 -0
- package/src/client-v2/auto-approval/AutoApprovalProvider.tsx +399 -0
- package/src/client-v2/auto-approval/__tests__/session-auto-approval.test.tsx +1676 -0
- package/src/client-v2/components/AgentBindingsTab.tsx +275 -3
- package/src/client-v2/components/AgentRunsTab.tsx +1 -8
- package/src/client-v2/components/EmployeeGovernanceTab.tsx +1 -8
- package/src/client-v2/components/HarnessProfilesTab.tsx +4 -8
- package/src/client-v2/components/HarnessSettingsForm.tsx +221 -0
- package/src/client-v2/components/UsageGovernanceTab.tsx +1 -8
- package/src/client-v2/pages/AgentBindingsPage.tsx +1 -8
- package/src/client-v2/pages/ApprovalsPage.tsx +1 -8
- package/src/client-v2/pages/EmployeeGovernancePage.tsx +1 -8
- package/src/client-v2/pages/ExecutionHistoryPage.tsx +1 -8
- package/src/client-v2/pages/HarnessProfilesPage.tsx +1 -8
- package/src/client-v2/pages/KnowledgeAccessPage.tsx +1 -8
- package/src/client-v2/pages/LoopSettingsPage.tsx +1 -8
- package/src/client-v2/pages/MemoryInspectorPage.tsx +1 -8
- package/src/client-v2/pages/RetrievalTracePage.tsx +1 -8
- package/src/client-v2/pages/RulesPage.tsx +1 -8
- package/src/client-v2/pages/SkillDefinitionsPage.tsx +1 -8
- package/src/client-v2/pages/SkillMetricsPage.tsx +1 -8
- package/src/client-v2/pages/TracingPage.tsx +1 -8
- package/src/client-v2/pages/UsageGovernancePage.tsx +1 -8
- package/src/client-v2/plugin.tsx +3 -8
- package/src/client-v2/skill-hub/tools/__tests__/interactions.test.ts +17 -1
- package/src/client-v2/skill-hub/tools/interactions.ts +12 -3
- package/src/locale/en-US.json +80 -1
- package/src/locale/vi-VN.json +5 -1
- package/src/locale/zh-CN.json +80 -1
- package/src/server/__tests__/agent-governance.integration.test.ts +1 -8
- package/src/server/__tests__/agent-monitor-acl.integration.test.ts +1 -8
- package/src/server/__tests__/agent-usage-report.integration.test.ts +1 -8
- package/src/server/__tests__/auto-approval-checkpoint.integration.test.ts +295 -0
- package/src/server/__tests__/harness-capabilities.test.ts +1 -8
- package/src/server/__tests__/harness-compiler.test.ts +64 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +1 -8
- package/src/server/__tests__/hierarchical-routing.integration.test.ts +2318 -0
- package/src/server/__tests__/hierarchy-invocation-journal.integration.test.ts +119 -0
- package/src/server/__tests__/loop-approval-decision.test.ts +59 -12
- package/src/server/__tests__/loop-budget-service.test.ts +1 -8
- package/src/server/__tests__/loop-run-repository.test.ts +1 -8
- package/src/server/__tests__/loop-trigger-service.test.ts +1 -8
- package/src/server/__tests__/loop-worker-claim.test.ts +46 -9
- package/src/server/__tests__/loop-worker-execution.test.ts +1 -8
- package/src/server/__tests__/native-agent-monitor-sync.test.ts +1 -8
- package/src/server/__tests__/native-hierarchy-probe.integration.test.ts +527 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +1 -8
- package/src/server/__tests__/native-usage-attribution.integration.test.ts +1 -8
- package/src/server/__tests__/plugin-ai-runtime-adapter.test.ts +403 -0
- package/src/server/__tests__/registry-node-package.test.ts +299 -0
- package/src/server/__tests__/routing-binding-graph.integration.test.ts +217 -0
- package/src/server/__tests__/routing-binding-validation.test.ts +256 -0
- package/src/server/__tests__/session-auto-approval.integration.test.ts +329 -0
- package/src/server/__tests__/skill-execute.test.ts +11 -0
- package/src/server/__tests__/skill-review-guard.test.ts +171 -0
- package/src/server/__tests__/token-tracker.test.ts +1 -8
- package/src/server/__tests__/tracing-retention-policy.test.ts +1 -8
- package/src/server/__tests__/usage-ledger.integration.test.ts +1 -8
- package/src/server/collections/agent-auto-approval-checkpoints.ts +18 -0
- package/src/server/collections/agent-execution-spans.ts +4 -11
- package/src/server/collections/agent-governance-audit-events.ts +1 -8
- package/src/server/collections/agent-governance-policies.ts +1 -8
- package/src/server/collections/agent-governance-policy-versions.ts +1 -8
- package/src/server/collections/agent-governance-prompt-bindings.ts +1 -8
- package/src/server/collections/agent-hierarchy-invocations.ts +30 -0
- package/src/server/collections/agent-loop-runs.ts +1 -8
- package/src/server/collections/agent-routing-graph-locks.ts +11 -0
- package/src/server/collections/agent-session-auto-approvals.ts +16 -0
- package/src/server/collections/orchestrator-config.ts +26 -0
- package/src/server/migrations/20261110000000-add-usage-pricing-snapshots.ts +1 -8
- package/src/server/migrations/20261110010000-add-usage-department-attribution.ts +1 -8
- package/src/server/migrations/20261119000000-widen-native-employee-identifiers.ts +37 -0
- package/src/server/plugin.ts +58 -8
- package/src/server/resources/agent-auto-approval.ts +51 -0
- package/src/server/resources/agent-governance.ts +1 -8
- package/src/server/resources/agent-harness-profile.ts +1 -8
- package/src/server/resources/agent-monitor.ts +1 -8
- package/src/server/resources/agent-usage.ts +1 -8
- package/src/server/resources/hierarchical-routing.ts +39 -0
- package/src/server/resources/native-monitor-format.ts +1 -8
- package/src/server/resources/tracing.ts +1 -8
- package/src/server/services/AgentGovernanceService.ts +1 -8
- package/src/server/services/AutoApprovalCheckpoint.ts +140 -0
- package/src/server/services/CodeValidator.ts +23 -4
- package/src/server/services/DepartmentAttribution.ts +1 -8
- package/src/server/services/ExecutionSpanService.ts +1 -8
- package/src/server/services/GovernanceAdmission.ts +83 -8
- package/src/server/services/HarnessCapabilities.ts +1 -8
- package/src/server/services/HarnessCompiler.ts +12 -0
- package/src/server/services/HarnessSchema.ts +30 -0
- package/src/server/services/HierarchicalAgentRuntime.ts +1052 -0
- package/src/server/services/HierarchyEnrollmentGuard.ts +211 -0
- package/src/server/services/HierarchyInvocationJournal.ts +172 -0
- package/src/server/services/HierarchyRequestCancellation.ts +132 -0
- package/src/server/services/LoopBudgetService.ts +1 -8
- package/src/server/services/LoopRunRepository.ts +2 -9
- package/src/server/services/LoopRunStateMachine.ts +3 -10
- package/src/server/services/LoopTriggerService.ts +1 -8
- package/src/server/services/LoopWorkerService.ts +8 -19
- package/src/server/services/ManagedPromptProtection.ts +1 -8
- package/src/server/services/NativeAgentMonitorSyncService.ts +1 -8
- package/src/server/services/NativeSubAgentObserver.ts +1 -8
- package/src/server/services/NativeUsageAttribution.ts +1 -8
- package/src/server/services/PluginAiRuntimeAdapter.ts +140 -9
- package/src/server/services/RoutingBindingRepository.ts +146 -0
- package/src/server/services/RoutingBindingValidation.ts +161 -0
- package/src/server/services/SandboxRunner.ts +35 -1
- package/src/server/services/SessionAutoApproval.ts +167 -0
- package/src/server/services/TokenTracker.ts +1 -8
- package/src/server/services/TracingRetentionPolicy.ts +1 -8
- package/src/server/services/VerificationService.ts +3 -10
- package/src/server/skill-hub/plugin.ts +40 -0
- package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +11 -1
- package/src/server/skill-hub/services/registry-node-package.ts +191 -0
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +34 -5
- package/src/server/tools/skill-execute.ts +10 -0
- package/src/server/utils/ensure-indexes.ts +2 -8
- package/dist/client/418.6f01da17774a5a25.js +0 -10
- package/dist/client/652.477e21bfff67bc1c.js +0 -10
- package/dist/client-v2/418.55b6b28cda8bb5f3.js +0 -10
- package/dist/client-v2/652.1a48364774d5383a.js +0 -10
- package/dist/client-v2/981.a81521f0296c7db8.js +0 -10
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Context } from '@nocobase/actions';
|
|
3
|
+
import type { Plugin } from '@nocobase/server';
|
|
4
|
+
import { autoApprovalCall, isAutoApprovalEligible, sessionAutoApprovalEnabled } from '../services/SessionAutoApproval';
|
|
5
|
+
import { valuesFromCtx } from '../utils/ctx-utils';
|
|
6
|
+
import { read } from '../utils/record-utils';
|
|
7
|
+
|
|
8
|
+
async function ownedSession(ctx: Context): Promise<{ ownerId: number; sessionId: string }> {
|
|
9
|
+
const ownerId = Number(ctx.auth?.user?.id);
|
|
10
|
+
if (!Number.isSafeInteger(ownerId) || ownerId <= 0) ctx.throw(401, ctx.t('Authentication is required.'));
|
|
11
|
+
const values = valuesFromCtx(ctx);
|
|
12
|
+
const sessionId = values.sessionId;
|
|
13
|
+
if (typeof sessionId !== 'string' || !sessionId.trim() || sessionId.length > 100) {
|
|
14
|
+
ctx.throw(400, ctx.t('A conversation is required.'));
|
|
15
|
+
}
|
|
16
|
+
const conversation = await ctx.db
|
|
17
|
+
.getRepository('aiConversations')
|
|
18
|
+
.findOne({ filter: { sessionId, userId: ownerId } });
|
|
19
|
+
if (!conversation || read(conversation, 'from') === 'sub-agent') {
|
|
20
|
+
ctx.throw(404, ctx.t('The current conversation is unavailable.'));
|
|
21
|
+
}
|
|
22
|
+
return { ownerId, sessionId };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function registerAgentAutoApprovalResource(plugin: Plugin) {
|
|
26
|
+
plugin.app.resource({
|
|
27
|
+
name: 'agentAutoApproval',
|
|
28
|
+
actions: {
|
|
29
|
+
async get(ctx, next) {
|
|
30
|
+
const { sessionId } = await ownedSession(ctx as Context);
|
|
31
|
+
ctx.body = { enabled: await sessionAutoApprovalEnabled(ctx as Context, sessionId) };
|
|
32
|
+
await next();
|
|
33
|
+
},
|
|
34
|
+
async set(ctx, next) {
|
|
35
|
+
const { ownerId, sessionId } = await ownedSession(ctx as Context);
|
|
36
|
+
const { enabled } = valuesFromCtx(ctx);
|
|
37
|
+
if (typeof enabled !== 'boolean') ctx.throw(400, ctx.t('An auto-approval setting is required.'));
|
|
38
|
+
await ctx.db.getModel('agentSessionAutoApprovals').upsert({ ownerId, sessionId, enabled });
|
|
39
|
+
ctx.body = { enabled };
|
|
40
|
+
await next();
|
|
41
|
+
},
|
|
42
|
+
async eligible(ctx, next) {
|
|
43
|
+
await ownedSession(ctx as Context);
|
|
44
|
+
const call = autoApprovalCall(valuesFromCtx(ctx));
|
|
45
|
+
if (!call) return ctx.throw(400, ctx.t('A tool call is required.'));
|
|
46
|
+
ctx.body = { eligible: await isAutoApprovalEligible(ctx as Context, call) };
|
|
47
|
+
await next();
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Context } from '@nocobase/actions';
|
|
11
4
|
import type { Plugin } from '@nocobase/server';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Plugin } from '@nocobase/server';
|
|
11
4
|
import type { Context } from '@nocobase/actions';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Plugin } from '@nocobase/server';
|
|
11
4
|
import { Op, Sequelize } from 'sequelize';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Database } from '@nocobase/database';
|
|
11
4
|
import type { Plugin } from '@nocobase/server';
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
import type { Context } from '@nocobase/actions';
|
|
11
|
+
import type { Plugin } from '@nocobase/server';
|
|
12
|
+
import { HierarchicalAgentRuntime } from '../services/HierarchicalAgentRuntime';
|
|
13
|
+
import { valuesFromCtx } from '../utils/ctx-utils';
|
|
14
|
+
import { requireAdminActor, throwResourceError } from './resource-helpers';
|
|
15
|
+
|
|
16
|
+
/** Preview uses the requesting administrator's own real session and the runtime's resolver. */
|
|
17
|
+
export function registerHierarchicalRoutingResource(plugin: Plugin) {
|
|
18
|
+
const runtime = new HierarchicalAgentRuntime(plugin);
|
|
19
|
+
plugin.app.resource({
|
|
20
|
+
name: 'agentHierarchy',
|
|
21
|
+
actions: {
|
|
22
|
+
async preview(ctx, next) {
|
|
23
|
+
const values = valuesFromCtx(ctx);
|
|
24
|
+
const username = typeof values.username === 'string' ? values.username.trim() : '';
|
|
25
|
+
const sessionId = typeof values.sessionId === 'string' ? values.sessionId.trim() : '';
|
|
26
|
+
const specialistUsername =
|
|
27
|
+
typeof values.specialistUsername === 'string' ? values.specialistUsername.trim() : '';
|
|
28
|
+
if (!username || !sessionId) ctx.throw(400, ctx.t('A conversation and target employee are required.'));
|
|
29
|
+
try {
|
|
30
|
+
requireAdminActor(ctx as Context);
|
|
31
|
+
ctx.body = { data: await runtime.preview(ctx as Context, username, specialistUsername || undefined) };
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throwResourceError(ctx as Context, error);
|
|
34
|
+
}
|
|
35
|
+
await next();
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { asObject, toPlain } from '../utils/ctx-utils';
|
|
11
4
|
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Plugin } from '@nocobase/server';
|
|
11
4
|
import type { Context as ActionContext } from '@nocobase/actions';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
11
4
|
import { createHash } from 'node:crypto';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import type { Context } from '@nocobase/actions';
|
|
4
|
+
import { asRecord, read } from '../utils/record-utils';
|
|
5
|
+
import type { AutoApprovalCall } from './SessionAutoApproval';
|
|
6
|
+
|
|
7
|
+
export type AutoApprovalCheckpoint = {
|
|
8
|
+
checkpointKey: string;
|
|
9
|
+
ownerId: number;
|
|
10
|
+
sessionId: string;
|
|
11
|
+
messageId: string;
|
|
12
|
+
interruptId: string;
|
|
13
|
+
callIds: string[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Read the checkpoint from a conversation owned by the caller. */
|
|
17
|
+
export async function autoApprovalCheckpoint(
|
|
18
|
+
ctx: Context,
|
|
19
|
+
call: AutoApprovalCall,
|
|
20
|
+
): Promise<AutoApprovalCheckpoint | null | undefined> {
|
|
21
|
+
const ownerId = Number(ctx.auth?.user?.id);
|
|
22
|
+
if (!Number.isSafeInteger(ownerId) || ownerId <= 0) return undefined;
|
|
23
|
+
const conversation = await ctx.db.getRepository('aiConversations').findOne({
|
|
24
|
+
filter: { sessionId: call.sessionId, userId: ownerId },
|
|
25
|
+
});
|
|
26
|
+
if (!conversation || read(conversation, 'from') === 'sub-agent') return undefined;
|
|
27
|
+
const username = read(conversation, 'aiEmployeeUsername');
|
|
28
|
+
if (typeof username !== 'string' || !username) return undefined;
|
|
29
|
+
const message = await ctx.db.getRepository('aiMessages').findOne({
|
|
30
|
+
filter: { sessionId: call.sessionId, messageId: call.messageId, role: username },
|
|
31
|
+
});
|
|
32
|
+
const calls = read(message, 'toolCalls');
|
|
33
|
+
if (!Array.isArray(calls) || !calls.length) return undefined;
|
|
34
|
+
const callIds = calls.map((item) => asRecord(item).id);
|
|
35
|
+
if (
|
|
36
|
+
call.messageId.length > 100 ||
|
|
37
|
+
callIds.some((id) => typeof id !== 'string' || !id) ||
|
|
38
|
+
new Set(callIds).size !== callIds.length ||
|
|
39
|
+
!callIds.includes(call.toolCallId)
|
|
40
|
+
)
|
|
41
|
+
return undefined;
|
|
42
|
+
const interruptId = asRecord(read(message, 'metadata')).interruptId;
|
|
43
|
+
if (typeof interruptId !== 'string' || !interruptId) return undefined;
|
|
44
|
+
if (callIds.length === 1) return null;
|
|
45
|
+
const checkpointKey = createHash('sha256')
|
|
46
|
+
.update(JSON.stringify([ownerId, call.sessionId, call.messageId, interruptId]))
|
|
47
|
+
.digest('hex');
|
|
48
|
+
return {
|
|
49
|
+
checkpointKey,
|
|
50
|
+
ownerId,
|
|
51
|
+
sessionId: call.sessionId,
|
|
52
|
+
messageId: call.messageId,
|
|
53
|
+
interruptId,
|
|
54
|
+
callIds: callIds as string[],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Enroll once before an automatic decision reaches the native action. */
|
|
59
|
+
export async function enrollAutoApprovalCheckpoint(ctx: Context, checkpoint: AutoApprovalCheckpoint) {
|
|
60
|
+
const model = ctx.db.getModel('agentAutoApprovalCheckpoints');
|
|
61
|
+
const where = { checkpointKey: checkpoint.checkpointKey };
|
|
62
|
+
if (await model.findOne({ where })) return;
|
|
63
|
+
try {
|
|
64
|
+
await model.create({
|
|
65
|
+
...where,
|
|
66
|
+
ownerId: checkpoint.ownerId,
|
|
67
|
+
sessionId: checkpoint.sessionId,
|
|
68
|
+
messageId: checkpoint.messageId,
|
|
69
|
+
interruptId: checkpoint.interruptId,
|
|
70
|
+
status: 'pending',
|
|
71
|
+
});
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (!(error instanceof Error) || error.name !== 'SequelizeUniqueConstraintError') throw error;
|
|
74
|
+
if (!(await model.findOne({ where }))) throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Native decisions are retained; only one callback may execute frontend tools and resume. */
|
|
79
|
+
export async function coordinateAutoApprovalDecision(
|
|
80
|
+
ctx: Context,
|
|
81
|
+
checkpoint: AutoApprovalCheckpoint,
|
|
82
|
+
call: AutoApprovalCall,
|
|
83
|
+
) {
|
|
84
|
+
const model = ctx.db.getModel('agentAutoApprovalCheckpoints');
|
|
85
|
+
const where = { checkpointKey: checkpoint.checkpointKey, ownerId: checkpoint.ownerId };
|
|
86
|
+
if (!(await model.findOne({ where }))) return;
|
|
87
|
+
const body = asRecord(ctx.body);
|
|
88
|
+
const originalUpdated = body.updated;
|
|
89
|
+
const calls = Array.isArray(body.toolCalls) ? body.toolCalls.map(asRecord) : [];
|
|
90
|
+
const ids = calls.map((item) => item.id);
|
|
91
|
+
const validSnapshot =
|
|
92
|
+
calls.length === checkpoint.callIds.length &&
|
|
93
|
+
ids.every((id) => typeof id === 'string' && checkpoint.callIds.includes(id)) &&
|
|
94
|
+
new Set(ids).size === ids.length &&
|
|
95
|
+
calls.every((item) => typeof item.willInterrupt === 'boolean');
|
|
96
|
+
const current = await autoApprovalCheckpoint(ctx, call);
|
|
97
|
+
let sameCheckpoint =
|
|
98
|
+
current?.checkpointKey === checkpoint.checkpointKey &&
|
|
99
|
+
current.callIds.length === checkpoint.callIds.length &&
|
|
100
|
+
current.callIds.every((id) => checkpoint.callIds.includes(id));
|
|
101
|
+
const interruptedCalls = calls.filter((item) => item.willInterrupt === true);
|
|
102
|
+
const ready =
|
|
103
|
+
validSnapshot &&
|
|
104
|
+
sameCheckpoint &&
|
|
105
|
+
interruptedCalls.some((item) => item.id === call.toolCallId) &&
|
|
106
|
+
interruptedCalls.every((item) => item.invokeStatus === 'waiting');
|
|
107
|
+
let canFinalize = false;
|
|
108
|
+
if (originalUpdated === 1 && ready) {
|
|
109
|
+
canFinalize = await ctx.db.sequelize.transaction(async (transaction) => {
|
|
110
|
+
const message = await ctx.db.getModel('aiMessages').findOne({
|
|
111
|
+
where: { sessionId: checkpoint.sessionId, messageId: checkpoint.messageId },
|
|
112
|
+
transaction,
|
|
113
|
+
lock: transaction.LOCK.UPDATE,
|
|
114
|
+
});
|
|
115
|
+
const messageCalls = read(message, 'toolCalls');
|
|
116
|
+
const messageIds = Array.isArray(messageCalls) ? messageCalls.map((item) => asRecord(item).id) : [];
|
|
117
|
+
sameCheckpoint =
|
|
118
|
+
asRecord(read(message, 'metadata')).interruptId === checkpoint.interruptId &&
|
|
119
|
+
messageIds.length === checkpoint.callIds.length &&
|
|
120
|
+
messageIds.every((id) => typeof id === 'string' && checkpoint.callIds.includes(id)) &&
|
|
121
|
+
new Set(messageIds).size === messageIds.length;
|
|
122
|
+
if (!sameCheckpoint) return false;
|
|
123
|
+
const [updated] = await model.update(
|
|
124
|
+
{ status: 'issued' },
|
|
125
|
+
{ where: { ...where, status: 'pending' }, transaction },
|
|
126
|
+
);
|
|
127
|
+
return updated === 1;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
ctx.body = {
|
|
131
|
+
...body,
|
|
132
|
+
updated: canFinalize ? originalUpdated : 0,
|
|
133
|
+
agentAutoApprovalCheckpoint: {
|
|
134
|
+
interruptId: checkpoint.interruptId,
|
|
135
|
+
decisionRecorded: originalUpdated === 1,
|
|
136
|
+
canFinalize,
|
|
137
|
+
validSnapshot: validSnapshot && sameCheckpoint,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -1,8 +1,21 @@
|
|
|
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
|
+
|
|
1
10
|
interface ForbiddenPattern {
|
|
2
11
|
pattern: RegExp;
|
|
3
12
|
reason: string;
|
|
4
13
|
}
|
|
5
14
|
|
|
15
|
+
const NODE_ENV_PATTERN = /process\.env(?!\s*\.OUTPUT_DIR)/;
|
|
16
|
+
const REGISTRY_NODE_ENV_PATTERN =
|
|
17
|
+
/process\.env(?!\s*\.OUTPUT_DIR)(?!\s*\.SKILL_INPUT_FILE(?![$\u200C\u200D\p{ID_Continue}\\]))/u;
|
|
18
|
+
|
|
6
19
|
const DANGEROUS_NODE_PATTERNS: ForbiddenPattern[] = [
|
|
7
20
|
{ pattern: /require\s*\(\s*['"]child_process['"]\s*\)/, reason: 'child_process module not allowed' },
|
|
8
21
|
{ pattern: /require\s*\(\s*['"]cluster['"]\s*\)/, reason: 'cluster module not allowed' },
|
|
@@ -15,7 +28,7 @@ const DANGEROUS_NODE_PATTERNS: ForbiddenPattern[] = [
|
|
|
15
28
|
{ pattern: /require\s*\(\s*['"]inspector['"]\s*\)/, reason: 'inspector module not allowed' },
|
|
16
29
|
{ pattern: /require\s*\(\s*['"]v8['"]\s*\)/, reason: 'v8 module not allowed' },
|
|
17
30
|
{ pattern: /process\.exit/, reason: 'process.exit not allowed' },
|
|
18
|
-
{ pattern:
|
|
31
|
+
{ pattern: NODE_ENV_PATTERN, reason: 'process.env access not allowed (use OUTPUT_DIR only)' },
|
|
19
32
|
{ pattern: /process\.kill/, reason: 'process.kill not allowed' },
|
|
20
33
|
{ pattern: /process\.binding\s*\(/, reason: 'process.binding not allowed' },
|
|
21
34
|
{ pattern: /\bglobalThis\b/, reason: 'globalThis access not allowed' },
|
|
@@ -142,12 +155,18 @@ export class CodeValidator {
|
|
|
142
155
|
* Check code against forbidden patterns (dangerous modules/functions).
|
|
143
156
|
* @throws CodeValidationError if a forbidden pattern is found.
|
|
144
157
|
*/
|
|
145
|
-
validate(code: string, language: 'node' | 'python'): void {
|
|
158
|
+
validate(code: string, language: 'node' | 'python', options?: { allowSkillInputFile?: boolean }): void {
|
|
146
159
|
const patterns = language === 'node' ? DANGEROUS_NODE_PATTERNS : DANGEROUS_PYTHON_PATTERNS;
|
|
147
160
|
|
|
148
161
|
for (const { pattern, reason } of patterns) {
|
|
149
|
-
|
|
150
|
-
|
|
162
|
+
const checkedPattern =
|
|
163
|
+
options?.allowSkillInputFile && pattern === NODE_ENV_PATTERN ? REGISTRY_NODE_ENV_PATTERN : pattern;
|
|
164
|
+
if (checkedPattern.test(code)) {
|
|
165
|
+
const message =
|
|
166
|
+
checkedPattern === REGISTRY_NODE_ENV_PATTERN
|
|
167
|
+
? 'process.env access not allowed (use OUTPUT_DIR or SKILL_INPUT_FILE only)'
|
|
168
|
+
: reason;
|
|
169
|
+
throw new CodeValidationError(message, checkedPattern.source);
|
|
151
170
|
}
|
|
152
171
|
}
|
|
153
172
|
}
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Database } from '@nocobase/database';
|
|
11
4
|
import { literal } from 'sequelize';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { toPlain } from '../utils/ctx-utils';
|
|
11
4
|
import { getAgentExecutionContext } from './AgentExecutionContext';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { createHash } from 'node:crypto';
|
|
11
4
|
import type { Context, Next } from '@nocobase/actions';
|
|
@@ -100,6 +93,88 @@ async function checkObservedQuota(ctx: Context, policy: Policy, userId: number,
|
|
|
100
93
|
if (checksCost && cost >= Number(settings.maxMonthlyCost)) quotaError(ctx, 'AI cost quota reached.');
|
|
101
94
|
}
|
|
102
95
|
|
|
96
|
+
/** Apply the same employee/model and managed-prompt checks to a plugin-owned child invocation. */
|
|
97
|
+
export async function authorizeDelegatedEmployee(
|
|
98
|
+
context: Context,
|
|
99
|
+
employee: Model,
|
|
100
|
+
model: ModelReference,
|
|
101
|
+
): Promise<{ userId: number; departmentId: string | number | null }> {
|
|
102
|
+
const userId = Number(context.auth?.user?.id);
|
|
103
|
+
if (!Number.isSafeInteger(userId) || userId <= 0) quotaError(context, 'Authentication is required.');
|
|
104
|
+
const employeeUsername = valueString(read(employee, 'username'));
|
|
105
|
+
if (!employeeUsername || !model.llmService || !model.model) {
|
|
106
|
+
quotaError(context, 'AI governance could not resolve the model.');
|
|
107
|
+
}
|
|
108
|
+
const service = new AgentGovernanceService(context.db);
|
|
109
|
+
let department;
|
|
110
|
+
try {
|
|
111
|
+
department = await resolveUserDepartment(context.db, userId);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const departmentPolicies = await context.db.getRepository('agentGovernancePolicies').count({
|
|
114
|
+
filter: { scope: 'department', currentVersionId: { $ne: null } },
|
|
115
|
+
});
|
|
116
|
+
if (departmentPolicies) throw error;
|
|
117
|
+
department = { departmentId: null, status: 'departments_unavailable' as const };
|
|
118
|
+
}
|
|
119
|
+
const policyRepo = context.db.getRepository('agentGovernancePolicies');
|
|
120
|
+
const configured = await policyRepo.count({ filter: { currentVersionId: { $ne: null } } });
|
|
121
|
+
const binding = await context.db
|
|
122
|
+
.getRepository('agentGovernancePromptBindings')
|
|
123
|
+
.findOne({ filter: { userId, employeeUsername } });
|
|
124
|
+
if (!configured && !binding) return { userId, departmentId: department.departmentId };
|
|
125
|
+
if (department.status === 'departments_unavailable') {
|
|
126
|
+
const departmentPolicies = await policyRepo.count({
|
|
127
|
+
filter: { scope: 'department', currentVersionId: { $ne: null } },
|
|
128
|
+
});
|
|
129
|
+
if (departmentPolicies) quotaError(context, 'AI governance cannot check department policies.');
|
|
130
|
+
}
|
|
131
|
+
const modelKey = `${model.llmService}:${model.model}`;
|
|
132
|
+
const policies = configured
|
|
133
|
+
? await service.activePolicies(userId, employeeUsername, department.departmentId, undefined, modelKey)
|
|
134
|
+
: [];
|
|
135
|
+
for (const policy of policies) {
|
|
136
|
+
if (
|
|
137
|
+
policy.scope === 'employee' &&
|
|
138
|
+
policy.settings.systemPrompt !== undefined &&
|
|
139
|
+
(read(employee, 'about') ?? null) !== policy.settings.systemPrompt
|
|
140
|
+
) {
|
|
141
|
+
quotaError(context, 'The managed AI employee prompt changed and needs administrator review.');
|
|
142
|
+
}
|
|
143
|
+
if (
|
|
144
|
+
policy.settings.allow === false ||
|
|
145
|
+
(policy.settings.allowedModels && !policy.settings.allowedModels.includes(modelKey))
|
|
146
|
+
) {
|
|
147
|
+
quotaError(context, 'Your AI employee or model is not allowed by governance policy.');
|
|
148
|
+
}
|
|
149
|
+
await checkObservedQuota(context, policy, userId, department.departmentId);
|
|
150
|
+
}
|
|
151
|
+
const prompt = await service.preview({ userId, employeeUsername });
|
|
152
|
+
if (prompt.instruction || binding) {
|
|
153
|
+
if (!prompt.instruction || prompt.promptMode !== 'default' || !binding) {
|
|
154
|
+
quotaError(context, 'Managed AI instructions need to be applied by an administrator.');
|
|
155
|
+
}
|
|
156
|
+
const expected = createHash('sha256').update(prompt.projectedPrompt).digest('hex');
|
|
157
|
+
const native = await context.db
|
|
158
|
+
.getRepository('usersAiEmployees')
|
|
159
|
+
.findOne({ filter: { userId, aiEmployee: employeeUsername } });
|
|
160
|
+
const actual = createHash('sha256')
|
|
161
|
+
.update(valueString(read(native, 'prompt')))
|
|
162
|
+
.digest('hex');
|
|
163
|
+
const versions = prompt.policies
|
|
164
|
+
.filter((policy) => policy.settings.instruction?.trim())
|
|
165
|
+
.map((policy) => ({ id: policy.id, versionId: policy.versionId }));
|
|
166
|
+
if (
|
|
167
|
+
expected !== read(binding, 'contentHash') ||
|
|
168
|
+
expected !== actual ||
|
|
169
|
+
String(prompt.departmentId ?? '') !== String(read(binding, 'departmentId') ?? '') ||
|
|
170
|
+
JSON.stringify(versions) !== JSON.stringify(read(binding, 'policyVersions'))
|
|
171
|
+
) {
|
|
172
|
+
quotaError(context, 'Managed AI instructions need to be applied by an administrator.');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { userId, departmentId: department.departmentId };
|
|
176
|
+
}
|
|
177
|
+
|
|
103
178
|
export function createGovernanceAdmission(plugin: Plugin, enabled: () => boolean) {
|
|
104
179
|
const service = new AgentGovernanceService(plugin.db);
|
|
105
180
|
return async (context: Context, next: Next) => {
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
export type NativeHarnessScope = 'chat' | 'api' | 'workflow' | 'sub-agent';
|
|
11
4
|
export type HarnessCapabilityStatus = 'enforced' | 'observe-only' | 'unsupported';
|
|
@@ -1,3 +1,12 @@
|
|
|
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
|
+
|
|
1
10
|
import { parseHarnessSettings } from './HarnessSchema';
|
|
2
11
|
import type { HarnessSettings, ToolDecision, ToolEffect } from './HarnessSchema';
|
|
3
12
|
|
|
@@ -84,6 +93,8 @@ export function compileHarness(layers: HarnessLayer[]): CompiledHarness {
|
|
|
84
93
|
}
|
|
85
94
|
const parsed = layers.map((layer) => ({ source: layer.source, settings: parseHarnessSettings(layer.settings) }));
|
|
86
95
|
const settings = parsed.map((layer) => layer.settings);
|
|
96
|
+
// Model preferences use the most specific supplied routing layer; limits still merge restrictively.
|
|
97
|
+
const routing = [...settings].reverse().find((layer) => layer.routing !== undefined)?.routing;
|
|
87
98
|
const effectNames = union(settings.map((layer) => Object.keys(layer.tools.effects)));
|
|
88
99
|
const effects = Object.fromEntries(
|
|
89
100
|
effectNames.map((name) => [
|
|
@@ -98,6 +109,7 @@ export function compileHarness(layers: HarnessLayer[]): CompiledHarness {
|
|
|
98
109
|
|
|
99
110
|
return {
|
|
100
111
|
sources: parsed.map((layer) => layer.source),
|
|
112
|
+
...(routing === undefined ? {} : { routing }),
|
|
101
113
|
tools: {
|
|
102
114
|
allow: intersectRestrictions(settings.map((layer) => layer.tools.allow)),
|
|
103
115
|
ask: union(settings.map((layer) => layer.tools.ask)),
|
|
@@ -1,3 +1,12 @@
|
|
|
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
|
+
|
|
1
10
|
import { z } from 'zod';
|
|
2
11
|
|
|
3
12
|
export const toolDecisionSchema = z.enum(['allow', 'ask', 'deny']);
|
|
@@ -6,6 +15,13 @@ export const toolEffectSchema = z.enum(['read', 'write', 'external']);
|
|
|
6
15
|
const nullableLimit = z.number().finite().nonnegative().nullable().default(null);
|
|
7
16
|
const nullablePositiveLimit = z.number().finite().positive().nullable().default(null);
|
|
8
17
|
|
|
18
|
+
const routingModelSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
llmService: z.string().trim().min(1),
|
|
21
|
+
model: z.string().trim().min(1),
|
|
22
|
+
})
|
|
23
|
+
.strict();
|
|
24
|
+
|
|
9
25
|
export const harnessSettingsSchema = z
|
|
10
26
|
.object({
|
|
11
27
|
tools: z
|
|
@@ -83,6 +99,18 @@ export const harnessSettingsSchema = z
|
|
|
83
99
|
sharing: z.enum(['full', 'feedback-only', 'disabled']).default('full'),
|
|
84
100
|
})
|
|
85
101
|
.default({}),
|
|
102
|
+
routing: z
|
|
103
|
+
.object({
|
|
104
|
+
models: z
|
|
105
|
+
.object({
|
|
106
|
+
primary: routingModelSchema.nullable().optional(),
|
|
107
|
+
fallback: z.array(routingModelSchema).max(20).optional(),
|
|
108
|
+
})
|
|
109
|
+
.strict()
|
|
110
|
+
.optional(),
|
|
111
|
+
})
|
|
112
|
+
.strict()
|
|
113
|
+
.optional(),
|
|
86
114
|
})
|
|
87
115
|
.strict();
|
|
88
116
|
|
|
@@ -121,6 +149,7 @@ export function normalizeHarnessSettings(value: unknown): unknown {
|
|
|
121
149
|
'limits',
|
|
122
150
|
'isolation',
|
|
123
151
|
'observability',
|
|
152
|
+
'routing',
|
|
124
153
|
'nativeObserverEnabled',
|
|
125
154
|
'memoryInjectionEnabled',
|
|
126
155
|
'memoryScopes',
|
|
@@ -140,6 +169,7 @@ export function normalizeHarnessSettings(value: unknown): unknown {
|
|
|
140
169
|
|
|
141
170
|
return {
|
|
142
171
|
...unknownSettings,
|
|
172
|
+
...(source.routing === undefined ? {} : { routing: source.routing }),
|
|
143
173
|
tools: {
|
|
144
174
|
...tools,
|
|
145
175
|
allow: stringArray(tools.allow),
|