plugin-agent-orchestrator 1.0.38 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.js +1 -1
- package/dist/client-v2/253.9f0537713cee9e6c.js +10 -0
- package/dist/client-v2/418.ce8f5b2b8a229d1d.js +10 -0
- package/dist/client-v2/53.7c29c28043e9112d.js +10 -0
- package/dist/client-v2/794.9bbb8d3a61b2bdf2.js +10 -0
- package/dist/client-v2/993.82778b6fd6db3cb1.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +6 -6
- package/dist/locale/en-US.json +56 -2
- package/dist/locale/vi-VN.json +56 -2
- package/dist/locale/zh-CN.json +56 -2
- package/dist/node_modules/ajv/.tonic_example.js +20 -0
- package/dist/node_modules/ajv/LICENSE +22 -0
- package/dist/node_modules/ajv/dist/ajv.bundle.js +7189 -0
- package/dist/node_modules/ajv/dist/ajv.min.js +3 -0
- package/dist/node_modules/ajv/lib/ajv.d.ts +397 -0
- package/dist/node_modules/ajv/lib/ajv.js +3 -0
- package/dist/node_modules/ajv/lib/cache.js +26 -0
- package/dist/node_modules/ajv/lib/compile/async.js +90 -0
- package/dist/node_modules/ajv/lib/compile/equal.js +5 -0
- package/dist/node_modules/ajv/lib/compile/error_classes.js +34 -0
- package/dist/node_modules/ajv/lib/compile/formats.js +142 -0
- package/dist/node_modules/ajv/lib/compile/index.js +387 -0
- package/dist/node_modules/ajv/lib/compile/resolve.js +270 -0
- package/dist/node_modules/ajv/lib/compile/rules.js +66 -0
- package/dist/node_modules/ajv/lib/compile/schema_obj.js +9 -0
- package/dist/node_modules/ajv/lib/compile/ucs2length.js +20 -0
- package/dist/node_modules/ajv/lib/compile/util.js +239 -0
- package/dist/node_modules/ajv/lib/data.js +49 -0
- package/dist/node_modules/ajv/lib/definition_schema.js +37 -0
- package/dist/node_modules/ajv/lib/dot/_limit.jst +113 -0
- package/dist/node_modules/ajv/lib/dot/_limitItems.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/_limitLength.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/_limitProperties.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/allOf.jst +32 -0
- package/dist/node_modules/ajv/lib/dot/anyOf.jst +46 -0
- package/dist/node_modules/ajv/lib/dot/coerce.def +51 -0
- package/dist/node_modules/ajv/lib/dot/comment.jst +9 -0
- package/dist/node_modules/ajv/lib/dot/const.jst +11 -0
- package/dist/node_modules/ajv/lib/dot/contains.jst +55 -0
- package/dist/node_modules/ajv/lib/dot/custom.jst +191 -0
- package/dist/node_modules/ajv/lib/dot/defaults.def +47 -0
- package/dist/node_modules/ajv/lib/dot/definitions.def +203 -0
- package/dist/node_modules/ajv/lib/dot/dependencies.jst +79 -0
- package/dist/node_modules/ajv/lib/dot/enum.jst +30 -0
- package/dist/node_modules/ajv/lib/dot/errors.def +194 -0
- package/dist/node_modules/ajv/lib/dot/format.jst +106 -0
- package/dist/node_modules/ajv/lib/dot/if.jst +73 -0
- package/dist/node_modules/ajv/lib/dot/items.jst +98 -0
- package/dist/node_modules/ajv/lib/dot/missing.def +39 -0
- package/dist/node_modules/ajv/lib/dot/multipleOf.jst +22 -0
- package/dist/node_modules/ajv/lib/dot/not.jst +43 -0
- package/dist/node_modules/ajv/lib/dot/oneOf.jst +54 -0
- package/dist/node_modules/ajv/lib/dot/pattern.jst +14 -0
- package/dist/node_modules/ajv/lib/dot/properties.jst +245 -0
- package/dist/node_modules/ajv/lib/dot/propertyNames.jst +52 -0
- package/dist/node_modules/ajv/lib/dot/ref.jst +85 -0
- package/dist/node_modules/ajv/lib/dot/required.jst +108 -0
- package/dist/node_modules/ajv/lib/dot/uniqueItems.jst +62 -0
- package/dist/node_modules/ajv/lib/dot/validate.jst +276 -0
- package/dist/node_modules/ajv/lib/dotjs/_limit.js +163 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitItems.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitLength.js +85 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitProperties.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/allOf.js +42 -0
- package/dist/node_modules/ajv/lib/dotjs/anyOf.js +73 -0
- package/dist/node_modules/ajv/lib/dotjs/comment.js +14 -0
- package/dist/node_modules/ajv/lib/dotjs/const.js +56 -0
- package/dist/node_modules/ajv/lib/dotjs/contains.js +81 -0
- package/dist/node_modules/ajv/lib/dotjs/custom.js +228 -0
- package/dist/node_modules/ajv/lib/dotjs/dependencies.js +168 -0
- package/dist/node_modules/ajv/lib/dotjs/enum.js +66 -0
- package/dist/node_modules/ajv/lib/dotjs/format.js +150 -0
- package/dist/node_modules/ajv/lib/dotjs/if.js +103 -0
- package/dist/node_modules/ajv/lib/dotjs/index.js +33 -0
- package/dist/node_modules/ajv/lib/dotjs/items.js +140 -0
- package/dist/node_modules/ajv/lib/dotjs/multipleOf.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/not.js +84 -0
- package/dist/node_modules/ajv/lib/dotjs/oneOf.js +73 -0
- package/dist/node_modules/ajv/lib/dotjs/pattern.js +75 -0
- package/dist/node_modules/ajv/lib/dotjs/properties.js +335 -0
- package/dist/node_modules/ajv/lib/dotjs/propertyNames.js +81 -0
- package/dist/node_modules/ajv/lib/dotjs/ref.js +124 -0
- package/dist/node_modules/ajv/lib/dotjs/required.js +270 -0
- package/dist/node_modules/ajv/lib/dotjs/uniqueItems.js +86 -0
- package/dist/node_modules/ajv/lib/dotjs/validate.js +482 -0
- package/dist/node_modules/ajv/lib/keyword.js +146 -0
- package/dist/node_modules/ajv/lib/refs/data.json +17 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-04.json +149 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-06.json +154 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-07.json +168 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-secure.json +94 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/.eslintrc.yml +27 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/.travis.yml +8 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/index.js +89 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/package.json +43 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/.eslintrc.yml +6 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/fixtures/schema.js +125 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/index.spec.js +171 -0
- package/dist/node_modules/ajv/package.json +1 -0
- package/dist/node_modules/ajv/scripts/.eslintrc.yml +3 -0
- package/dist/node_modules/ajv/scripts/bundle.js +61 -0
- package/dist/node_modules/ajv/scripts/compile-dots.js +73 -0
- package/dist/node_modules/ajv/scripts/info +10 -0
- package/dist/node_modules/ajv/scripts/prepare-tests +12 -0
- package/dist/node_modules/ajv/scripts/publish-built-version +32 -0
- package/dist/node_modules/ajv/scripts/travis-gh-pages +23 -0
- package/dist/server/collections/skill-definitions.js +31 -0
- package/dist/server/collections/skill-executions.js +19 -0
- package/dist/server/collections/skill-registry-installations.js +70 -0
- package/dist/server/migrations/20260621000000-native-policy-profile-defaults.js +0 -3
- package/dist/server/migrations/20260727000000-remove-unused-knowledge-scopes.js +51 -0
- package/dist/server/migrations/20260727010000-backfill-skill-tool-names.js +66 -0
- package/dist/server/plugin.js +107 -2
- package/dist/server/resources/agent-knowledge-insights.js +273 -0
- package/dist/server/services/AgentExecutionContext.js +45 -0
- package/dist/server/services/AgentMemoryContextService.js +57 -19
- package/dist/server/services/CodeValidator.js +1 -0
- package/dist/server/services/ExecutionSpanService.js +10 -2
- package/dist/server/services/InputSchemaValidator.js +83 -0
- package/dist/server/services/NativeSubAgentObserver.js +63 -8
- package/dist/server/services/SandboxRunner.js +6 -2
- package/dist/server/services/SkillAccessService.js +109 -0
- package/dist/server/services/SkillManager.js +2 -0
- package/dist/server/services/SkillRepositoryService.js +70 -0
- package/dist/server/skill-hub/actions/git-import.js +479 -216
- package/dist/server/skill-hub/mcp/McpController.js +8 -5
- package/dist/server/skill-hub/middlewares/git-import-request-method-policy.js +69 -0
- package/dist/server/skill-hub/plugin.js +68 -14
- package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +249 -0
- package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +228 -0
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +15 -3
- package/dist/server/tools/external-rag-search.js +152 -12
- package/dist/server/tools/skill-execute.js +23 -2
- package/dist/server/utils/ctx-utils.js +2 -2
- package/dist/server/utils/skill-tool-name.js +73 -0
- package/package.json +9 -3
- package/src/client-v2/components/AgentBindingsTab.tsx +269 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +1 -2
- package/src/client-v2/components/KnowledgeAccessTab.tsx +116 -0
- package/src/client-v2/components/MemoryInspectorTab.tsx +125 -0
- package/src/client-v2/components/RetrievalTraceTab.tsx +162 -0
- package/src/client-v2/pages/AgentBindingsPage.tsx +13 -0
- package/src/client-v2/pages/KnowledgeAccessPage.tsx +10 -0
- package/src/client-v2/pages/MemoryInspectorPage.tsx +13 -0
- package/src/client-v2/pages/RetrievalTracePage.tsx +10 -0
- package/src/client-v2/plugin.tsx +28 -0
- package/src/locale/en-US.json +56 -2
- package/src/locale/vi-VN.json +56 -2
- package/src/locale/zh-CN.json +56 -2
- package/src/server/__tests__/agent-execution-context.test.ts +18 -0
- package/src/server/__tests__/agent-knowledge-insights.test.ts +123 -0
- package/src/server/__tests__/external-rag-search.test.ts +107 -0
- package/src/server/__tests__/input-schema-validator.test.ts +26 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +141 -9
- package/src/server/__tests__/skill-access-service.test.ts +64 -0
- package/src/server/__tests__/skill-execute.test.ts +37 -0
- package/src/server/__tests__/skill-tool-name.test.ts +23 -0
- package/src/server/collections/skill-definitions.ts +31 -0
- package/src/server/collections/skill-executions.ts +19 -0
- package/src/server/collections/skill-registry-installations.ts +41 -0
- package/src/server/migrations/20260621000000-native-policy-profile-defaults.ts +0 -3
- package/src/server/migrations/20260727000000-remove-unused-knowledge-scopes.ts +25 -0
- package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -0
- package/src/server/plugin.ts +107 -1
- package/src/server/resources/agent-knowledge-insights.ts +304 -0
- package/src/server/services/AgentExecutionContext.ts +23 -0
- package/src/server/services/AgentMemoryContextService.ts +63 -19
- package/src/server/services/CodeValidator.ts +1 -0
- package/src/server/services/ExecutionSpanService.ts +119 -110
- package/src/server/services/InputSchemaValidator.ts +44 -0
- package/src/server/services/NativeSubAgentObserver.ts +592 -528
- package/src/server/services/SandboxRunner.ts +6 -0
- package/src/server/services/SkillAccessService.ts +100 -0
- package/src/server/services/SkillManager.ts +2 -0
- package/src/server/services/SkillRepositoryService.ts +98 -2
- package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -0
- package/src/server/skill-hub/actions/__tests__/git-import.test.ts +501 -0
- package/src/server/skill-hub/actions/git-import.ts +682 -236
- package/src/server/skill-hub/mcp/McpController.ts +8 -14
- package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -0
- package/src/server/skill-hub/plugin.ts +79 -13
- package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +281 -0
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +239 -0
- package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -0
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +63 -0
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +14 -2
- package/src/server/tools/external-rag-search.ts +215 -14
- package/src/server/tools/skill-execute.ts +24 -2
- package/src/server/utils/ctx-utils.ts +1 -1
- package/src/server/utils/skill-tool-name.ts +58 -0
- package/dist/client-v2/418.29e713f79131eece.js +0 -10
|
@@ -39,6 +39,8 @@ export interface ExecuteOptions {
|
|
|
39
39
|
packageWhitelist?: string[];
|
|
40
40
|
/** Optional installed/copied skill package root exposed to the runtime as SKILL_DIR. */
|
|
41
41
|
skillDir?: string;
|
|
42
|
+
/** Validated input exposed as a JSON file to avoid source-code interpolation. */
|
|
43
|
+
inputArgs?: Record<string, unknown>;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
export interface ProgressUpdate {
|
|
@@ -196,6 +198,7 @@ export class SandboxRunner {
|
|
|
196
198
|
signal,
|
|
197
199
|
packageWhitelist,
|
|
198
200
|
skillDir,
|
|
201
|
+
inputArgs,
|
|
199
202
|
} = options;
|
|
200
203
|
|
|
201
204
|
// 1. Validate code against forbidden patterns
|
|
@@ -212,6 +215,8 @@ export class SandboxRunner {
|
|
|
212
215
|
// 3. Prepare workspace
|
|
213
216
|
const workDir = this.fileManager.createExecDir(execId);
|
|
214
217
|
const outputDir = this.fileManager.getOutputDir(execId);
|
|
218
|
+
const inputFile = resolve(workDir, 'input.json');
|
|
219
|
+
writeFileSync(inputFile, JSON.stringify(inputArgs || {}, null, 2), 'utf-8');
|
|
215
220
|
|
|
216
221
|
const filename = language === 'node' ? 'script.js' : 'script.py';
|
|
217
222
|
const scriptPath = resolve(workDir, filename);
|
|
@@ -268,6 +273,7 @@ export class SandboxRunner {
|
|
|
268
273
|
// Consumed by the sitecustomize.py guard to scope file writes.
|
|
269
274
|
SKILL_HUB_WRITE_ROOTS: [workDir, outputDir, tmpdir()].join(delimiter),
|
|
270
275
|
SKILL_DIR: skillDir || '',
|
|
276
|
+
SKILL_INPUT_FILE: inputFile,
|
|
271
277
|
// DO NOT pass: DB credentials, API keys, APP_KEY, etc.
|
|
272
278
|
};
|
|
273
279
|
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { getAgentExecutionContext } from './AgentExecutionContext';
|
|
2
|
+
import { asObject, isAdminUser, normalizeEmployeeUsername, valuesFromCtx } from '../utils/ctx-utils';
|
|
3
|
+
import { getSkillToolName, normalizeSkillToolScope, readRecordValue } from '../utils/skill-tool-name';
|
|
4
|
+
|
|
5
|
+
type ToolBinding = { name?: unknown } | string;
|
|
6
|
+
|
|
7
|
+
function bindingName(binding: ToolBinding): string {
|
|
8
|
+
if (typeof binding === 'string') return binding.trim();
|
|
9
|
+
return typeof binding?.name === 'string' ? binding.name.trim() : '';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class SkillAccessService {
|
|
13
|
+
constructor(private readonly plugin: { db: { getRepository: (name: string) => any } }) {}
|
|
14
|
+
|
|
15
|
+
async resolveAgentUsername(ctx: any): Promise<string | undefined> {
|
|
16
|
+
const executionIdentity = getAgentExecutionContext();
|
|
17
|
+
if (executionIdentity?.employeeUsername) return executionIdentity.employeeUsername;
|
|
18
|
+
|
|
19
|
+
const values = valuesFromCtx(ctx);
|
|
20
|
+
const direct = normalizeEmployeeUsername(
|
|
21
|
+
ctx?._currentAIEmployee ||
|
|
22
|
+
ctx?.state?.currentAIEmployee ||
|
|
23
|
+
ctx?.runtime?.context?.currentAIEmployee ||
|
|
24
|
+
values.aiEmployee ||
|
|
25
|
+
values.aiEmployeeUsername,
|
|
26
|
+
);
|
|
27
|
+
if (direct) return direct;
|
|
28
|
+
|
|
29
|
+
const sessionId =
|
|
30
|
+
executionIdentity?.sessionId || values.sessionId || ctx?.action?.params?.sessionId || ctx?.state?.sessionId;
|
|
31
|
+
if (!sessionId) return undefined;
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const conversation = await this.plugin.db.getRepository('aiConversations').findOne({
|
|
35
|
+
filter: { sessionId },
|
|
36
|
+
});
|
|
37
|
+
return (
|
|
38
|
+
normalizeEmployeeUsername(readRecordValue(conversation, 'aiEmployeeUsername')) ||
|
|
39
|
+
normalizeEmployeeUsername(readRecordValue(conversation, 'aiEmployee')) ||
|
|
40
|
+
undefined
|
|
41
|
+
);
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async canAgentUseSkill(agentUsername: string, skill: unknown): Promise<boolean> {
|
|
48
|
+
if (normalizeSkillToolScope(readRecordValue(skill, 'toolScope')) === 'GENERAL') return true;
|
|
49
|
+
|
|
50
|
+
const employee = await this.plugin.db.getRepository('aiEmployees').findOne({
|
|
51
|
+
filter: { username: agentUsername },
|
|
52
|
+
});
|
|
53
|
+
if (!employee) return false;
|
|
54
|
+
|
|
55
|
+
const skillSettings = asObject(readRecordValue(employee, 'skillSettings'));
|
|
56
|
+
const tools = Array.isArray(skillSettings.tools) ? (skillSettings.tools as ToolBinding[]) : [];
|
|
57
|
+
const targetToolName = getSkillToolName(skill);
|
|
58
|
+
return tools.some((binding) => bindingName(binding) === targetToolName);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async assertCanExecute(
|
|
62
|
+
ctx: any,
|
|
63
|
+
skill: unknown,
|
|
64
|
+
options: { privileged?: boolean } = {},
|
|
65
|
+
): Promise<string | undefined> {
|
|
66
|
+
if (options.privileged) return this.resolveAgentUsername(ctx);
|
|
67
|
+
if (isAdminUser(ctx)) return this.resolveAgentUsername(ctx);
|
|
68
|
+
|
|
69
|
+
const agentUsername = await this.resolveAgentUsername(ctx);
|
|
70
|
+
if (!agentUsername) {
|
|
71
|
+
throw new Error('Skill execution requires an AI employee context.');
|
|
72
|
+
}
|
|
73
|
+
if (!(await this.canAgentUseSkill(agentUsername, skill))) {
|
|
74
|
+
throw new Error(`AI employee "${agentUsername}" is not bound to skill tool "${getSkillToolName(skill)}".`);
|
|
75
|
+
}
|
|
76
|
+
return agentUsername;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async filterAccessibleSkills(ctx: any, skills: unknown[]): Promise<unknown[]> {
|
|
80
|
+
if (isAdminUser(ctx)) return skills;
|
|
81
|
+
const agentUsername = await this.resolveAgentUsername(ctx);
|
|
82
|
+
if (!agentUsername) return [];
|
|
83
|
+
|
|
84
|
+
const checks = await Promise.all(skills.map((skill) => this.canAgentUseSkill(agentUsername, skill)));
|
|
85
|
+
return skills.filter((_skill, index) => checks[index]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async findAgentUsernamesUsingTool(toolName: string): Promise<string[]> {
|
|
89
|
+
const employees = await this.plugin.db.getRepository('aiEmployees').find({});
|
|
90
|
+
const usernames: string[] = [];
|
|
91
|
+
for (const employee of employees || []) {
|
|
92
|
+
const settings = asObject(readRecordValue(employee, 'skillSettings'));
|
|
93
|
+
const tools = Array.isArray(settings.tools) ? (settings.tools as ToolBinding[]) : [];
|
|
94
|
+
if (!tools.some((binding) => bindingName(binding) === toolName)) continue;
|
|
95
|
+
const username = normalizeEmployeeUsername(readRecordValue(employee, 'username'));
|
|
96
|
+
if (username) usernames.push(username);
|
|
97
|
+
}
|
|
98
|
+
return usernames;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Database } from '@nocobase/database';
|
|
2
2
|
import { stringifyJsonText } from '../skill-hub/utils/json-fields';
|
|
3
|
+
import { buildSkillToolName } from '../utils/skill-tool-name';
|
|
3
4
|
|
|
4
5
|
export class SkillManager {
|
|
5
6
|
constructor(private db: Database) {}
|
|
@@ -208,6 +209,7 @@ export class SkillManager {
|
|
|
208
209
|
await repo.create({
|
|
209
210
|
values: {
|
|
210
211
|
...seed,
|
|
212
|
+
toolName: buildSkillToolName(seed.name),
|
|
211
213
|
inputSchema: stringifyJsonText(seed.inputSchema),
|
|
212
214
|
packages: stringifyJsonText(seed.packages, []),
|
|
213
215
|
},
|
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
cpSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
statSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'fs';
|
|
12
|
+
import { dirname, relative, resolve, sep } from 'path';
|
|
3
13
|
import AdmZip from 'adm-zip';
|
|
4
14
|
import { parseSkillMarkdown } from '../skill-hub/utils/json-fields';
|
|
5
15
|
export interface SkillPackageMetadata {
|
|
@@ -9,6 +19,11 @@ export interface SkillPackageMetadata {
|
|
|
9
19
|
code: string | null;
|
|
10
20
|
}
|
|
11
21
|
|
|
22
|
+
export interface SkillPackageFile {
|
|
23
|
+
path: string;
|
|
24
|
+
content: Buffer;
|
|
25
|
+
}
|
|
26
|
+
|
|
12
27
|
export class SkillRepositoryService {
|
|
13
28
|
private baseDir: string;
|
|
14
29
|
|
|
@@ -43,6 +58,39 @@ export class SkillRepositoryService {
|
|
|
43
58
|
return resolve(this.baseDir, skillName);
|
|
44
59
|
}
|
|
45
60
|
|
|
61
|
+
readSkillPackageFiles(skillName: string): SkillPackageFile[] | null {
|
|
62
|
+
const targetDir = this.getSkillPath(skillName);
|
|
63
|
+
if (!existsSync(targetDir)) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const files: SkillPackageFile[] = [];
|
|
67
|
+
const collect = (directory: string) => {
|
|
68
|
+
for (const item of readdirSync(directory)) {
|
|
69
|
+
const fullPath = resolve(directory, item);
|
|
70
|
+
const stat = statSync(fullPath);
|
|
71
|
+
if (stat.isDirectory()) {
|
|
72
|
+
collect(fullPath);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (!stat.isFile()) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const relativePath = relative(targetDir, fullPath).replace(/\\/g, '/');
|
|
79
|
+
files.push({ path: relativePath, content: readFileSync(fullPath) });
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
collect(targetDir);
|
|
83
|
+
return files;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
removeSkillPackage(skillName: string): void {
|
|
87
|
+
const targetDir = this.getSkillPath(skillName);
|
|
88
|
+
if (!targetDir.startsWith(`${this.baseDir}${sep}`)) {
|
|
89
|
+
throw new Error(`Invalid skill package name: ${skillName}`);
|
|
90
|
+
}
|
|
91
|
+
rmSync(targetDir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
|
|
46
94
|
getSkillCode(skillName: string): string | null {
|
|
47
95
|
const dir = this.getSkillPath(skillName);
|
|
48
96
|
if (!existsSync(dir)) return null;
|
|
@@ -55,6 +103,54 @@ export class SkillRepositoryService {
|
|
|
55
103
|
this.copyDirectoryTo(srcDir, destDir);
|
|
56
104
|
}
|
|
57
105
|
|
|
106
|
+
writeSkillPackage(skillName: string, files: SkillPackageFile[]): void {
|
|
107
|
+
const targetDir = this.getSkillPath(skillName);
|
|
108
|
+
const temporaryDir = `${targetDir}.next-${Date.now()}`;
|
|
109
|
+
const backupDir = `${targetDir}.previous-${Date.now()}`;
|
|
110
|
+
const filePaths = new Set<string>();
|
|
111
|
+
|
|
112
|
+
rmSync(temporaryDir, { recursive: true, force: true });
|
|
113
|
+
mkdirSync(temporaryDir, { recursive: true });
|
|
114
|
+
try {
|
|
115
|
+
for (const file of files) {
|
|
116
|
+
const path = file.path.replace(/\\/g, '/');
|
|
117
|
+
if (
|
|
118
|
+
!path ||
|
|
119
|
+
path.includes('\0') ||
|
|
120
|
+
path.startsWith('/') ||
|
|
121
|
+
path.split('/').some((part) => !part || part === '.' || part === '..') ||
|
|
122
|
+
filePaths.has(path)
|
|
123
|
+
) {
|
|
124
|
+
throw new Error(`Invalid skill package path: ${file.path}`);
|
|
125
|
+
}
|
|
126
|
+
filePaths.add(path);
|
|
127
|
+
const outputPath = resolve(temporaryDir, path);
|
|
128
|
+
if (!outputPath.startsWith(`${temporaryDir}${sep}`)) {
|
|
129
|
+
throw new Error(`Skill package path is outside its package root: ${file.path}`);
|
|
130
|
+
}
|
|
131
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
132
|
+
writeFileSync(outputPath, file.content);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (existsSync(targetDir)) {
|
|
136
|
+
rmSync(backupDir, { recursive: true, force: true });
|
|
137
|
+
renameSync(targetDir, backupDir);
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
renameSync(temporaryDir, targetDir);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (existsSync(backupDir)) {
|
|
143
|
+
renameSync(backupDir, targetDir);
|
|
144
|
+
}
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
rmSync(backupDir, { recursive: true, force: true });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
rmSync(temporaryDir, { recursive: true, force: true });
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
58
154
|
readSkillPackage(packageDir: string): SkillPackageMetadata {
|
|
59
155
|
const rootDir = resolve(packageDir);
|
|
60
156
|
if (!existsSync(rootDir)) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { mkdtempSync, readFileSync, rmSync } from 'fs';
|
|
2
|
+
import { tmpdir } from 'os';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
import { SkillRepositoryService } from '../SkillRepositoryService';
|
|
6
|
+
|
|
7
|
+
describe('SkillRepositoryService registry package materialization', () => {
|
|
8
|
+
let storagePath = '';
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
storagePath = mkdtempSync(join(tmpdir(), 'skill-repository-service-'));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
rmSync(storagePath, { recursive: true, force: true });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('materializes all portable artifact files and rejects traversal paths', () => {
|
|
19
|
+
const repository = new SkillRepositoryService(storagePath);
|
|
20
|
+
|
|
21
|
+
repository.writeSkillPackage('registry-acme-report', [
|
|
22
|
+
{ path: 'SKILL.md', content: Buffer.from('# Report\n') },
|
|
23
|
+
{ path: 'src/index.py', content: Buffer.from('from .format import render\n') },
|
|
24
|
+
{ path: 'src/format.py', content: Buffer.from('def render():\n return "ok"\n') },
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
const packageRoot = repository.getSkillPath('registry-acme-report');
|
|
28
|
+
expect(readFileSync(join(packageRoot, 'src', 'format.py'), 'utf8')).toContain('return "ok"');
|
|
29
|
+
expect(() =>
|
|
30
|
+
repository.writeSkillPackage('registry-acme-report', [{ path: '../escape.py', content: Buffer.alloc(0) }]),
|
|
31
|
+
).toThrow('Invalid skill package path');
|
|
32
|
+
});
|
|
33
|
+
});
|