plugin-agent-orchestrator 1.1.4 → 1.1.6
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 +204 -114
- package/dist/client/index.js +1 -1
- package/dist/client-v2/117.7565cff57c39318a.js +10 -0
- package/dist/client-v2/214.106635011859dede.js +10 -0
- package/dist/client-v2/418.41ed200e7d6da332.js +10 -0
- package/dist/client-v2/53.349fff8db80b19d4.js +10 -0
- package/dist/client-v2/981.e4a1f41cfc94a856.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +7 -8
- package/dist/locale/en-US.json +226 -170
- package/dist/locale/vi-VN.json +226 -170
- package/dist/locale/zh-CN.json +226 -170
- package/dist/server/collections/agent-execution-spans.js +8 -0
- package/dist/server/collections/agent-loop-action-approvals.js +1 -0
- package/dist/server/collections/agent-loop-runs.js +7 -1
- package/dist/server/collections/agent-loop-steps.js +7 -0
- package/dist/server/collections/skill-executions.js +8 -0
- package/dist/server/plugin.js +3 -1
- package/dist/server/resources/agent-harness-profile.js +117 -0
- package/dist/server/resources/agent-knowledge-insights.js +1 -3
- package/dist/server/resources/agent-loop-approval.js +25 -0
- package/dist/server/resources/agent-loop.js +3 -1
- package/dist/server/services/AgentMemoryContextService.js +34 -0
- package/dist/server/services/HarnessCompiler.js +35 -2
- package/dist/server/services/HarnessProfileService.js +74 -9
- package/dist/server/services/HarnessSchema.js +30 -3
- package/dist/server/services/LoopPatternSchema.js +16 -1
- package/dist/server/services/LoopRunRepository.js +80 -0
- package/dist/server/services/LoopRunStateMachine.js +92 -3
- package/dist/server/services/LoopTriggerService.js +37 -0
- package/dist/server/services/LoopWorkerService.js +261 -23
- package/dist/server/services/NativeSubAgentObserver.js +165 -23
- package/dist/server/services/PluginAiRuntimeAdapter.js +16 -5
- package/dist/server/services/ToolLoopDetectionService.js +210 -0
- package/dist/server/services/VerificationService.js +66 -0
- package/dist/server/services/harness-runtime-policy.js +139 -0
- package/dist/server/skill-hub/plugin.js +36 -3
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +29 -0
- package/package.json +53 -53
- package/src/client/ApprovalsTab.tsx +2 -0
- package/src/client/OrchestratorSettings.tsx +11 -0
- package/src/client/skill-hub/components/GitSkillImport.tsx +3 -3
- package/src/client/skill-hub/components/SkillMetrics.tsx +15 -10
- package/src/client/skill-hub/index.tsx +0 -52
- package/src/client-v2/components/AgentRunsTab.tsx +576 -576
- package/src/client-v2/components/ApprovalsTab.tsx +337 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +385 -105
- package/src/client-v2/components/MemoryInspectorTab.tsx +141 -141
- package/src/client-v2/components/TracingTab.tsx +515 -515
- package/src/client-v2/hooks/useApiRequest.ts +141 -141
- package/src/client-v2/pages/ApprovalsPage.tsx +10 -0
- package/src/client-v2/plugin.tsx +6 -6
- package/src/client-v2/skill-hub/components/SkillMetrics.tsx +5 -2
- package/src/locale/en-US.json +226 -174
- package/src/locale/vi-VN.json +226 -174
- package/src/locale/zh-CN.json +226 -174
- package/src/server/__tests__/circuit-breaker.test.ts +248 -248
- package/src/server/__tests__/harness-compiler.test.ts +83 -0
- package/src/server/__tests__/harness-profile-service.test.ts +51 -0
- package/src/server/__tests__/harness-runtime-policy.test.ts +293 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +24 -0
- package/src/server/__tests__/loop-approval-decision.test.ts +512 -0
- package/src/server/__tests__/loop-approval-expiry.test.ts +258 -0
- package/src/server/__tests__/loop-budget-service.test.ts +255 -255
- package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -266
- package/src/server/__tests__/loop-pattern-schema.test.ts +18 -1
- package/src/server/__tests__/loop-pattern-service.test.ts +148 -148
- package/src/server/__tests__/loop-run-state-machine.test.ts +11 -0
- package/src/server/__tests__/loop-trigger-service.test.ts +148 -0
- package/src/server/__tests__/loop-worker-claim.test.ts +341 -341
- package/src/server/__tests__/loop-worker-execution.test.ts +82 -1
- package/src/server/__tests__/native-sub-agent-observer.test.ts +288 -4
- package/src/server/__tests__/path-lock-service.test.ts +254 -254
- package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -0
- package/src/server/__tests__/smoke.test.ts +162 -162
- package/src/server/__tests__/tool-loop-detection.test.ts +275 -0
- package/src/server/collections/agent-execution-spans.ts +8 -0
- package/src/server/collections/agent-harness-profile-versions.ts +26 -26
- package/src/server/collections/agent-harness-profiles.ts +71 -71
- package/src/server/collections/agent-loop-action-approvals.ts +1 -0
- package/src/server/collections/agent-loop-artifacts.ts +25 -25
- package/src/server/collections/agent-loop-circuit-states.ts +23 -23
- package/src/server/collections/agent-loop-control-settings.ts +21 -21
- package/src/server/collections/agent-loop-events.ts +98 -98
- package/src/server/collections/agent-loop-path-locks.ts +27 -27
- package/src/server/collections/agent-loop-patterns.ts +42 -42
- package/src/server/collections/agent-loop-runs.ts +7 -1
- package/src/server/collections/agent-loop-steps.ts +7 -0
- package/src/server/collections/agent-loop-usage-buckets.ts +22 -22
- package/src/server/collections/agent-loop-worktrees.ts +25 -25
- package/src/server/collections/skill-executions.ts +8 -0
- package/src/server/migrations/20260427000002-change-packages-to-text.ts +52 -52
- package/src/server/migrations/20260429000000-add-llm-fields.ts +47 -47
- package/src/server/migrations/20260429000001-fix-inputargs-json-to-text.ts +45 -45
- package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +33 -33
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +31 -31
- package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -293
- package/src/server/plugin.ts +3 -1
- package/src/server/resources/agent-harness-profile.ts +94 -0
- package/src/server/resources/agent-knowledge-insights.ts +5 -3
- package/src/server/resources/agent-loop-approval.ts +32 -0
- package/src/server/resources/agent-loop-artifact.ts +56 -56
- package/src/server/resources/agent-loop-events-stream.ts +105 -105
- package/src/server/resources/agent-loop.ts +3 -1
- package/src/server/resources/resource-helpers.ts +33 -33
- package/src/server/resources/tracing.ts +266 -266
- package/src/server/services/AgentMemoryContextService.ts +44 -0
- package/src/server/services/DurableCircuitBreakerService.ts +239 -239
- package/src/server/services/ExecutionSpanService.ts +2 -0
- package/src/server/services/HarnessCompiler.ts +37 -0
- package/src/server/services/HarnessProfileService.ts +92 -10
- package/src/server/services/HarnessSchema.ts +31 -0
- package/src/server/services/LoopBudgetService.ts +275 -275
- package/src/server/services/LoopControlService.ts +124 -124
- package/src/server/services/LoopPatternSchema.ts +19 -1
- package/src/server/services/LoopPatternService.ts +143 -143
- package/src/server/services/LoopRunRepository.ts +99 -0
- package/src/server/services/LoopRunStateMachine.ts +100 -3
- package/src/server/services/LoopSchedulerService.ts +199 -199
- package/src/server/services/LoopTriggerService.ts +42 -0
- package/src/server/services/LoopWorkerService.ts +327 -27
- package/src/server/services/NativeSubAgentObserver.ts +186 -21
- package/src/server/services/PathLockService.ts +321 -321
- package/src/server/services/PluginAiRuntimeAdapter.ts +24 -7
- package/src/server/services/ToolLoopDetectionService.ts +213 -0
- package/src/server/services/VerificationSchema.ts +31 -31
- package/src/server/services/VerificationService.ts +81 -2
- package/src/server/services/harness-runtime-policy.ts +141 -0
- package/src/server/skill-hub/plugin.ts +41 -3
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +235 -235
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +61 -61
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +43 -2
- package/src/server/utils/ensure-indexes.ts +96 -96
- package/src/server/utils/logging.ts +62 -62
- package/src/server/utils/loop-pattern-context.ts +20 -20
- package/dist/client-v2/214.b063f33ba044a808.js +0 -10
- package/dist/client-v2/373.f5d5292e53c4e832.js +0 -10
- package/dist/client-v2/418.58fa350f5496efd3.js +0 -10
- package/dist/client-v2/53.9a10455323390f14.js +0 -10
- package/dist/client-v2/677.a991ce0250ff5c77.js +0 -10
- package/dist/client-v2/925.e5925bf21ed68e7a.js +0 -10
- package/src/client/AIEmployeeSelect.tsx +0 -47
- package/src/client/AIEmployeesContext.tsx +0 -102
- package/src/client/RulesTab.tsx +0 -2
- package/src/client/TracingTab.tsx +0 -436
- package/src/client/skill-hub/components/ExecutionProgress.tsx +0 -51
- package/src/client-v2/components/RulesTab.tsx +0 -2
- package/src/client-v2/pages/AgentRunsPage.tsx +0 -13
|
@@ -13,6 +13,7 @@ import { gitListSkills, gitSyncSkills } from './actions/git-import';
|
|
|
13
13
|
import { createGitImportRequestMethodPolicy } from './middlewares/git-import-request-method-policy';
|
|
14
14
|
import { parseJsonText, stringifyJsonText, parseJsonLike } from './utils/json-fields';
|
|
15
15
|
import { getOrchestratorTraceContext } from '../services/ExecutionSpanService';
|
|
16
|
+
import { harnessTimeoutMs, resolveTraceHarness } from '../services/harness-runtime-policy';
|
|
16
17
|
import { tryGetAIToolsManager } from '../utils/ai-manager';
|
|
17
18
|
import type { ToolsRuntime } from '@nocobase/ai';
|
|
18
19
|
import { SkillAccessService } from '../services/SkillAccessService';
|
|
@@ -442,6 +443,40 @@ export class SkillHubSubFeature {
|
|
|
442
443
|
}
|
|
443
444
|
}
|
|
444
445
|
|
|
446
|
+
/**
|
|
447
|
+
* Resolve harness-derived enforcement overrides for one skill execution. The worker node owns
|
|
448
|
+
* the sandbox but not the harness, so the decision is made here (run snapshot for loop runs,
|
|
449
|
+
* employee harness-tag profile otherwise) and persisted on the execution row. A resolution
|
|
450
|
+
* failure must never block execution: the skill falls back to its own defaults.
|
|
451
|
+
*/
|
|
452
|
+
private async resolveRuntimePolicy(
|
|
453
|
+
traceContext: ReturnType<typeof getOrchestratorTraceContext>,
|
|
454
|
+
skill: any,
|
|
455
|
+
aiEmployeeUsername?: string,
|
|
456
|
+
): Promise<{ timeoutSeconds?: number; spillMaxInlineBytes?: number } | null> {
|
|
457
|
+
try {
|
|
458
|
+
const harness = await resolveTraceHarness((this as any).db, {
|
|
459
|
+
agentLoopRunId: traceContext?.agentLoopRunId,
|
|
460
|
+
employeeUsername: traceContext?.employeeUsername || aiEmployeeUsername,
|
|
461
|
+
});
|
|
462
|
+
if (!harness) return null;
|
|
463
|
+
|
|
464
|
+
const policy: { timeoutSeconds?: number; spillMaxInlineBytes?: number } = {};
|
|
465
|
+
const timeoutMs = harnessTimeoutMs(harness, getSkillToolName(skill));
|
|
466
|
+
if (timeoutMs) {
|
|
467
|
+
const skillTimeoutSeconds = Number(skill.timeoutSeconds || skill.get?.('timeoutSeconds') || 60);
|
|
468
|
+
policy.timeoutSeconds = Math.min(skillTimeoutSeconds, Math.ceil(timeoutMs / 1000));
|
|
469
|
+
}
|
|
470
|
+
if (harness.context.spill.maxInlineBytes) {
|
|
471
|
+
policy.spillMaxInlineBytes = harness.context.spill.maxInlineBytes;
|
|
472
|
+
}
|
|
473
|
+
return Object.keys(policy).length ? policy : null;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
(this as any).app.logger?.warn?.('[skill-hub] Failed to resolve harness runtime policy.', { error });
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
445
480
|
/**
|
|
446
481
|
* Execute skill — called by both AI tool and REST test endpoint.
|
|
447
482
|
* Dispatches to sandbox workers via PubSub, waits for result via PubSub.
|
|
@@ -456,7 +491,6 @@ export class SkillHubSubFeature {
|
|
|
456
491
|
): Promise<any> {
|
|
457
492
|
validateSkillInput(readRecordValue(skill, 'inputSchema'), inputArgs);
|
|
458
493
|
const aiEmployeeUsername = await this.skillAccessService.assertCanExecute(ctx, skill, options);
|
|
459
|
-
|
|
460
494
|
// ── Rate limiting ──
|
|
461
495
|
const userId = currentUserId(ctx);
|
|
462
496
|
if (userId) {
|
|
@@ -470,6 +504,7 @@ export class SkillHubSubFeature {
|
|
|
470
504
|
}
|
|
471
505
|
|
|
472
506
|
const traceContext = getOrchestratorTraceContext(ctx);
|
|
507
|
+
const runtimePolicy = await this.resolveRuntimePolicy(traceContext, skill, aiEmployeeUsername);
|
|
473
508
|
const execution = await (this as any).db.getRepository('skillExecutions').create({
|
|
474
509
|
values: {
|
|
475
510
|
skillId: skill.id,
|
|
@@ -485,6 +520,7 @@ export class SkillHubSubFeature {
|
|
|
485
520
|
agentLoopRunId: traceContext?.agentLoopRunId,
|
|
486
521
|
agentLoopStepId: traceContext?.agentLoopStepId,
|
|
487
522
|
triggeredById: ctx?.state?.currentUser?.id,
|
|
523
|
+
runtimePolicy,
|
|
488
524
|
},
|
|
489
525
|
});
|
|
490
526
|
|
|
@@ -532,10 +568,12 @@ export class SkillHubSubFeature {
|
|
|
532
568
|
rejectPromise = reject;
|
|
533
569
|
});
|
|
534
570
|
|
|
535
|
-
const
|
|
571
|
+
const effectiveTimeoutSeconds =
|
|
572
|
+
runtimePolicy?.timeoutSeconds || skill.timeoutSeconds || skill.get?.('timeoutSeconds') || 60;
|
|
573
|
+
const timeoutMs = (effectiveTimeoutSeconds + 15) * 1000;
|
|
536
574
|
const timeout = setTimeout(() => {
|
|
537
575
|
clearInterval(poll);
|
|
538
|
-
rejectPromise(new Error(`Skill execution timeout after ${
|
|
576
|
+
rejectPromise(new Error(`Skill execution timeout after ${effectiveTimeoutSeconds}s`));
|
|
539
577
|
}, timeoutMs);
|
|
540
578
|
const poll = setInterval(async () => {
|
|
541
579
|
try {
|
|
@@ -1,235 +1,235 @@
|
|
|
1
|
-
import { createHash } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import type { Database } from '@nocobase/database';
|
|
4
|
-
|
|
5
|
-
type JsonValue = boolean | number | string | null | JsonValue[] | { [key: string]: JsonValue };
|
|
6
|
-
|
|
7
|
-
export interface RegistrySkillSnapshotSummary {
|
|
8
|
-
id: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface RegistrySkillSnapshot {
|
|
12
|
-
id: string;
|
|
13
|
-
name: string;
|
|
14
|
-
title: string;
|
|
15
|
-
description: string;
|
|
16
|
-
language: 'python' | 'node';
|
|
17
|
-
suggestedVersion?: string;
|
|
18
|
-
inputSchema: JsonValue;
|
|
19
|
-
instructions: string;
|
|
20
|
-
files: Array<{ path: string; content: Buffer }>;
|
|
21
|
-
portable: boolean;
|
|
22
|
-
reason?: string;
|
|
23
|
-
revision: string;
|
|
24
|
-
dependencies: JsonValue;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type DefinitionModel = {
|
|
28
|
-
get(attribute: string): unknown;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
function positiveLimit(value: string | undefined, fallback: number, maximum: number): number {
|
|
32
|
-
const parsed = Number(value);
|
|
33
|
-
return Number.isSafeInteger(parsed) && parsed > 0 && parsed <= maximum ? parsed : fallback;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const MAX_REGISTRY_SKILL_SNAPSHOTS = positiveLimit(process.env.SKILL_REGISTRY_MAX_SOURCE_ITEMS, 1000, 10_000);
|
|
37
|
-
const MAX_REGISTRY_TEXT_BYTES = 10 * 1024 * 1024;
|
|
38
|
-
|
|
39
|
-
const registryExportFilter = {
|
|
40
|
-
registryPackageId: null,
|
|
41
|
-
registryVersionId: null,
|
|
42
|
-
registryInstallationId: null,
|
|
43
|
-
registryInstallStatus: null,
|
|
44
|
-
registryExportEnabled: true,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const REGISTRY_EXPORT_NOT_GRANTED = 'REGISTRY_EXPORT_NOT_GRANTED';
|
|
48
|
-
export const REGISTRY_EXPORT_LIMIT_EXCEEDED = 'REGISTRY_EXPORT_LIMIT_EXCEEDED';
|
|
49
|
-
|
|
50
|
-
export class RegistryExportNotGrantedError extends Error {
|
|
51
|
-
readonly code = REGISTRY_EXPORT_NOT_GRANTED;
|
|
52
|
-
|
|
53
|
-
constructor() {
|
|
54
|
-
super('Registry export is not enabled for this Skill Hub definition.');
|
|
55
|
-
this.name = 'RegistryExportNotGrantedError';
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class RegistryExportLimitError extends Error {
|
|
60
|
-
readonly code = REGISTRY_EXPORT_LIMIT_EXCEEDED;
|
|
61
|
-
|
|
62
|
-
constructor(message: string) {
|
|
63
|
-
super(message);
|
|
64
|
-
this.name = 'RegistryExportLimitError';
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function value(model: DefinitionModel, attribute: string): unknown {
|
|
69
|
-
return model.get(attribute);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function text(model: DefinitionModel, attribute: string, fallback = ''): string {
|
|
73
|
-
const raw = value(model, attribute);
|
|
74
|
-
return typeof raw === 'string' ? raw : raw === null || raw === undefined ? fallback : String(raw);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function parseJson(raw: unknown, fallback: JsonValue): JsonValue {
|
|
78
|
-
if (raw === null || typeof raw === 'string' || typeof raw === 'boolean') {
|
|
79
|
-
if (typeof raw !== 'string') {
|
|
80
|
-
return raw;
|
|
81
|
-
}
|
|
82
|
-
try {
|
|
83
|
-
return JSON.parse(raw) as JsonValue;
|
|
84
|
-
} catch {
|
|
85
|
-
return fallback;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (typeof raw === 'number' && Number.isFinite(raw)) {
|
|
89
|
-
return raw;
|
|
90
|
-
}
|
|
91
|
-
if (Array.isArray(raw) || (typeof raw === 'object' && raw !== null)) {
|
|
92
|
-
return raw as JsonValue;
|
|
93
|
-
}
|
|
94
|
-
return fallback;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function stableJson(value: JsonValue): string {
|
|
98
|
-
if (value === null || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
|
|
99
|
-
return JSON.stringify(value);
|
|
100
|
-
}
|
|
101
|
-
if (Array.isArray(value)) {
|
|
102
|
-
return `[${value.map(stableJson).join(',')}]`;
|
|
103
|
-
}
|
|
104
|
-
return `{${Object.keys(value)
|
|
105
|
-
.sort()
|
|
106
|
-
.map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`)
|
|
107
|
-
.join(',')}}`;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function digest(value: JsonValue): string {
|
|
111
|
-
return `sha256:${createHash('sha256').update(stableJson(value)).digest('hex')}`;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function asPortableSnapshot(model: DefinitionModel): RegistrySkillSnapshot {
|
|
115
|
-
const id = text(model, 'id');
|
|
116
|
-
const name = text(model, 'name');
|
|
117
|
-
const storageType = text(model, 'storageType', 'database');
|
|
118
|
-
const codeTemplate = text(model, 'codeTemplate');
|
|
119
|
-
const language = text(model, 'language', 'python') === 'node' ? 'node' : 'python';
|
|
120
|
-
const inputSchema = parseJson(value(model, 'inputSchema'), { type: 'object', properties: {} });
|
|
121
|
-
const dependencies = parseJson(value(model, 'packages'), []);
|
|
122
|
-
const instructions = text(model, 'instructions');
|
|
123
|
-
if (
|
|
124
|
-
Buffer.byteLength(codeTemplate, 'utf8') > MAX_REGISTRY_TEXT_BYTES ||
|
|
125
|
-
Buffer.byteLength(instructions, 'utf8') > MAX_REGISTRY_TEXT_BYTES
|
|
126
|
-
) {
|
|
127
|
-
throw new RegistryExportLimitError('Skill Hub definition exceeds the registry source-file limit.');
|
|
128
|
-
}
|
|
129
|
-
const revisionInput = {
|
|
130
|
-
id,
|
|
131
|
-
name,
|
|
132
|
-
storageType,
|
|
133
|
-
codeTemplate,
|
|
134
|
-
language,
|
|
135
|
-
inputSchema,
|
|
136
|
-
dependencies,
|
|
137
|
-
instructions,
|
|
138
|
-
title: text(model, 'title'),
|
|
139
|
-
description: text(model, 'description'),
|
|
140
|
-
};
|
|
141
|
-
if (storageType === 'plugin') {
|
|
142
|
-
return {
|
|
143
|
-
id,
|
|
144
|
-
name,
|
|
145
|
-
title: text(model, 'title', name),
|
|
146
|
-
description: text(model, 'description'),
|
|
147
|
-
language,
|
|
148
|
-
inputSchema,
|
|
149
|
-
instructions,
|
|
150
|
-
files: [],
|
|
151
|
-
portable: false,
|
|
152
|
-
reason: 'Plugin-backed skills require an explicit portable export bundle.',
|
|
153
|
-
revision: digest(revisionInput),
|
|
154
|
-
dependencies,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
if (!codeTemplate.trim()) {
|
|
158
|
-
return {
|
|
159
|
-
id,
|
|
160
|
-
name,
|
|
161
|
-
title: text(model, 'title', name),
|
|
162
|
-
description: text(model, 'description'),
|
|
163
|
-
language,
|
|
164
|
-
inputSchema,
|
|
165
|
-
instructions,
|
|
166
|
-
files: [],
|
|
167
|
-
portable: false,
|
|
168
|
-
reason: 'Skill has no inline code bundle to export.',
|
|
169
|
-
revision: digest(revisionInput),
|
|
170
|
-
dependencies,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
const entrypoint = language === 'node' ? 'src/index.js' : 'src/index.py';
|
|
174
|
-
const skillMarkdown = [
|
|
175
|
-
'---',
|
|
176
|
-
`name: ${name}`,
|
|
177
|
-
`title: ${text(model, 'title', name)}`,
|
|
178
|
-
`description: ${text(model, 'description').replace(/\r?\n/g, ' ')}`,
|
|
179
|
-
`language: ${language}`,
|
|
180
|
-
'---',
|
|
181
|
-
'',
|
|
182
|
-
instructions,
|
|
183
|
-
].join('\n');
|
|
184
|
-
return {
|
|
185
|
-
id,
|
|
186
|
-
name,
|
|
187
|
-
title: text(model, 'title', name),
|
|
188
|
-
description: text(model, 'description'),
|
|
189
|
-
language,
|
|
190
|
-
inputSchema,
|
|
191
|
-
instructions,
|
|
192
|
-
files: [
|
|
193
|
-
{ path: 'SKILL.md', content: Buffer.from(skillMarkdown, 'utf8') },
|
|
194
|
-
{ path: entrypoint, content: Buffer.from(codeTemplate, 'utf8') },
|
|
195
|
-
],
|
|
196
|
-
portable: true,
|
|
197
|
-
revision: digest(revisionInput),
|
|
198
|
-
dependencies,
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export class RegistrySkillSnapshotService {
|
|
203
|
-
constructor(private readonly database: Database) {}
|
|
204
|
-
|
|
205
|
-
async listSkillSnapshots(): Promise<RegistrySkillSnapshotSummary[]> {
|
|
206
|
-
// Registry-installed rows are local runtime projections of an immutable
|
|
207
|
-
// registry package. Exporting them back through Skill Hub would let the
|
|
208
|
-
// registry re-discover and publish its own installed artifacts.
|
|
209
|
-
const definitions = await this.database.getRepository('skillDefinitions').find({
|
|
210
|
-
fields: ['id'],
|
|
211
|
-
filter: registryExportFilter,
|
|
212
|
-
sort: ['id'],
|
|
213
|
-
limit: MAX_REGISTRY_SKILL_SNAPSHOTS + 1,
|
|
214
|
-
});
|
|
215
|
-
if (definitions.length > MAX_REGISTRY_SKILL_SNAPSHOTS) {
|
|
216
|
-
throw new RegistryExportLimitError('Skill Hub registry export exceeds the configured source-item limit.');
|
|
217
|
-
}
|
|
218
|
-
return definitions.map((definition) => ({ id: String(definition.get('id')) }));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
async getSkillSnapshot(skillDefinitionId: string): Promise<RegistrySkillSnapshot> {
|
|
222
|
-
const definition = await this.database.getRepository('skillDefinitions').findOne({
|
|
223
|
-
filter: {
|
|
224
|
-
id: skillDefinitionId,
|
|
225
|
-
...registryExportFilter,
|
|
226
|
-
},
|
|
227
|
-
});
|
|
228
|
-
if (!definition) {
|
|
229
|
-
// Use one result for missing and ungranted definitions so the internal
|
|
230
|
-
// service cannot be used to enumerate private Skill Hub content.
|
|
231
|
-
throw new RegistryExportNotGrantedError();
|
|
232
|
-
}
|
|
233
|
-
return asPortableSnapshot(definition as unknown as DefinitionModel);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
|
|
3
|
+
import type { Database } from '@nocobase/database';
|
|
4
|
+
|
|
5
|
+
type JsonValue = boolean | number | string | null | JsonValue[] | { [key: string]: JsonValue };
|
|
6
|
+
|
|
7
|
+
export interface RegistrySkillSnapshotSummary {
|
|
8
|
+
id: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface RegistrySkillSnapshot {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
title: string;
|
|
15
|
+
description: string;
|
|
16
|
+
language: 'python' | 'node';
|
|
17
|
+
suggestedVersion?: string;
|
|
18
|
+
inputSchema: JsonValue;
|
|
19
|
+
instructions: string;
|
|
20
|
+
files: Array<{ path: string; content: Buffer }>;
|
|
21
|
+
portable: boolean;
|
|
22
|
+
reason?: string;
|
|
23
|
+
revision: string;
|
|
24
|
+
dependencies: JsonValue;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type DefinitionModel = {
|
|
28
|
+
get(attribute: string): unknown;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function positiveLimit(value: string | undefined, fallback: number, maximum: number): number {
|
|
32
|
+
const parsed = Number(value);
|
|
33
|
+
return Number.isSafeInteger(parsed) && parsed > 0 && parsed <= maximum ? parsed : fallback;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const MAX_REGISTRY_SKILL_SNAPSHOTS = positiveLimit(process.env.SKILL_REGISTRY_MAX_SOURCE_ITEMS, 1000, 10_000);
|
|
37
|
+
const MAX_REGISTRY_TEXT_BYTES = 10 * 1024 * 1024;
|
|
38
|
+
|
|
39
|
+
const registryExportFilter = {
|
|
40
|
+
registryPackageId: null,
|
|
41
|
+
registryVersionId: null,
|
|
42
|
+
registryInstallationId: null,
|
|
43
|
+
registryInstallStatus: null,
|
|
44
|
+
registryExportEnabled: true,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const REGISTRY_EXPORT_NOT_GRANTED = 'REGISTRY_EXPORT_NOT_GRANTED';
|
|
48
|
+
export const REGISTRY_EXPORT_LIMIT_EXCEEDED = 'REGISTRY_EXPORT_LIMIT_EXCEEDED';
|
|
49
|
+
|
|
50
|
+
export class RegistryExportNotGrantedError extends Error {
|
|
51
|
+
readonly code = REGISTRY_EXPORT_NOT_GRANTED;
|
|
52
|
+
|
|
53
|
+
constructor() {
|
|
54
|
+
super('Registry export is not enabled for this Skill Hub definition.');
|
|
55
|
+
this.name = 'RegistryExportNotGrantedError';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class RegistryExportLimitError extends Error {
|
|
60
|
+
readonly code = REGISTRY_EXPORT_LIMIT_EXCEEDED;
|
|
61
|
+
|
|
62
|
+
constructor(message: string) {
|
|
63
|
+
super(message);
|
|
64
|
+
this.name = 'RegistryExportLimitError';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function value(model: DefinitionModel, attribute: string): unknown {
|
|
69
|
+
return model.get(attribute);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function text(model: DefinitionModel, attribute: string, fallback = ''): string {
|
|
73
|
+
const raw = value(model, attribute);
|
|
74
|
+
return typeof raw === 'string' ? raw : raw === null || raw === undefined ? fallback : String(raw);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function parseJson(raw: unknown, fallback: JsonValue): JsonValue {
|
|
78
|
+
if (raw === null || typeof raw === 'string' || typeof raw === 'boolean') {
|
|
79
|
+
if (typeof raw !== 'string') {
|
|
80
|
+
return raw;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(raw) as JsonValue;
|
|
84
|
+
} catch {
|
|
85
|
+
return fallback;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (typeof raw === 'number' && Number.isFinite(raw)) {
|
|
89
|
+
return raw;
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(raw) || (typeof raw === 'object' && raw !== null)) {
|
|
92
|
+
return raw as JsonValue;
|
|
93
|
+
}
|
|
94
|
+
return fallback;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function stableJson(value: JsonValue): string {
|
|
98
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
|
|
99
|
+
return JSON.stringify(value);
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(value)) {
|
|
102
|
+
return `[${value.map(stableJson).join(',')}]`;
|
|
103
|
+
}
|
|
104
|
+
return `{${Object.keys(value)
|
|
105
|
+
.sort()
|
|
106
|
+
.map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`)
|
|
107
|
+
.join(',')}}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function digest(value: JsonValue): string {
|
|
111
|
+
return `sha256:${createHash('sha256').update(stableJson(value)).digest('hex')}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function asPortableSnapshot(model: DefinitionModel): RegistrySkillSnapshot {
|
|
115
|
+
const id = text(model, 'id');
|
|
116
|
+
const name = text(model, 'name');
|
|
117
|
+
const storageType = text(model, 'storageType', 'database');
|
|
118
|
+
const codeTemplate = text(model, 'codeTemplate');
|
|
119
|
+
const language = text(model, 'language', 'python') === 'node' ? 'node' : 'python';
|
|
120
|
+
const inputSchema = parseJson(value(model, 'inputSchema'), { type: 'object', properties: {} });
|
|
121
|
+
const dependencies = parseJson(value(model, 'packages'), []);
|
|
122
|
+
const instructions = text(model, 'instructions');
|
|
123
|
+
if (
|
|
124
|
+
Buffer.byteLength(codeTemplate, 'utf8') > MAX_REGISTRY_TEXT_BYTES ||
|
|
125
|
+
Buffer.byteLength(instructions, 'utf8') > MAX_REGISTRY_TEXT_BYTES
|
|
126
|
+
) {
|
|
127
|
+
throw new RegistryExportLimitError('Skill Hub definition exceeds the registry source-file limit.');
|
|
128
|
+
}
|
|
129
|
+
const revisionInput = {
|
|
130
|
+
id,
|
|
131
|
+
name,
|
|
132
|
+
storageType,
|
|
133
|
+
codeTemplate,
|
|
134
|
+
language,
|
|
135
|
+
inputSchema,
|
|
136
|
+
dependencies,
|
|
137
|
+
instructions,
|
|
138
|
+
title: text(model, 'title'),
|
|
139
|
+
description: text(model, 'description'),
|
|
140
|
+
};
|
|
141
|
+
if (storageType === 'plugin') {
|
|
142
|
+
return {
|
|
143
|
+
id,
|
|
144
|
+
name,
|
|
145
|
+
title: text(model, 'title', name),
|
|
146
|
+
description: text(model, 'description'),
|
|
147
|
+
language,
|
|
148
|
+
inputSchema,
|
|
149
|
+
instructions,
|
|
150
|
+
files: [],
|
|
151
|
+
portable: false,
|
|
152
|
+
reason: 'Plugin-backed skills require an explicit portable export bundle.',
|
|
153
|
+
revision: digest(revisionInput),
|
|
154
|
+
dependencies,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
if (!codeTemplate.trim()) {
|
|
158
|
+
return {
|
|
159
|
+
id,
|
|
160
|
+
name,
|
|
161
|
+
title: text(model, 'title', name),
|
|
162
|
+
description: text(model, 'description'),
|
|
163
|
+
language,
|
|
164
|
+
inputSchema,
|
|
165
|
+
instructions,
|
|
166
|
+
files: [],
|
|
167
|
+
portable: false,
|
|
168
|
+
reason: 'Skill has no inline code bundle to export.',
|
|
169
|
+
revision: digest(revisionInput),
|
|
170
|
+
dependencies,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const entrypoint = language === 'node' ? 'src/index.js' : 'src/index.py';
|
|
174
|
+
const skillMarkdown = [
|
|
175
|
+
'---',
|
|
176
|
+
`name: ${name}`,
|
|
177
|
+
`title: ${text(model, 'title', name)}`,
|
|
178
|
+
`description: ${text(model, 'description').replace(/\r?\n/g, ' ')}`,
|
|
179
|
+
`language: ${language}`,
|
|
180
|
+
'---',
|
|
181
|
+
'',
|
|
182
|
+
instructions,
|
|
183
|
+
].join('\n');
|
|
184
|
+
return {
|
|
185
|
+
id,
|
|
186
|
+
name,
|
|
187
|
+
title: text(model, 'title', name),
|
|
188
|
+
description: text(model, 'description'),
|
|
189
|
+
language,
|
|
190
|
+
inputSchema,
|
|
191
|
+
instructions,
|
|
192
|
+
files: [
|
|
193
|
+
{ path: 'SKILL.md', content: Buffer.from(skillMarkdown, 'utf8') },
|
|
194
|
+
{ path: entrypoint, content: Buffer.from(codeTemplate, 'utf8') },
|
|
195
|
+
],
|
|
196
|
+
portable: true,
|
|
197
|
+
revision: digest(revisionInput),
|
|
198
|
+
dependencies,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export class RegistrySkillSnapshotService {
|
|
203
|
+
constructor(private readonly database: Database) {}
|
|
204
|
+
|
|
205
|
+
async listSkillSnapshots(): Promise<RegistrySkillSnapshotSummary[]> {
|
|
206
|
+
// Registry-installed rows are local runtime projections of an immutable
|
|
207
|
+
// registry package. Exporting them back through Skill Hub would let the
|
|
208
|
+
// registry re-discover and publish its own installed artifacts.
|
|
209
|
+
const definitions = await this.database.getRepository('skillDefinitions').find({
|
|
210
|
+
fields: ['id'],
|
|
211
|
+
filter: registryExportFilter,
|
|
212
|
+
sort: ['id'],
|
|
213
|
+
limit: MAX_REGISTRY_SKILL_SNAPSHOTS + 1,
|
|
214
|
+
});
|
|
215
|
+
if (definitions.length > MAX_REGISTRY_SKILL_SNAPSHOTS) {
|
|
216
|
+
throw new RegistryExportLimitError('Skill Hub registry export exceeds the configured source-item limit.');
|
|
217
|
+
}
|
|
218
|
+
return definitions.map((definition) => ({ id: String(definition.get('id')) }));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async getSkillSnapshot(skillDefinitionId: string): Promise<RegistrySkillSnapshot> {
|
|
222
|
+
const definition = await this.database.getRepository('skillDefinitions').findOne({
|
|
223
|
+
filter: {
|
|
224
|
+
id: skillDefinitionId,
|
|
225
|
+
...registryExportFilter,
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
if (!definition) {
|
|
229
|
+
// Use one result for missing and ungranted definitions so the internal
|
|
230
|
+
// service cannot be used to enumerate private Skill Hub content.
|
|
231
|
+
throw new RegistryExportNotGrantedError();
|
|
232
|
+
}
|
|
233
|
+
return asPortableSnapshot(definition as unknown as DefinitionModel);
|
|
234
|
+
}
|
|
235
|
+
}
|