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.
Files changed (192) hide show
  1. package/dist/client/index.js +1 -1
  2. package/dist/client-v2/253.9f0537713cee9e6c.js +10 -0
  3. package/dist/client-v2/418.ce8f5b2b8a229d1d.js +10 -0
  4. package/dist/client-v2/53.7c29c28043e9112d.js +10 -0
  5. package/dist/client-v2/794.9bbb8d3a61b2bdf2.js +10 -0
  6. package/dist/client-v2/993.82778b6fd6db3cb1.js +10 -0
  7. package/dist/client-v2/index.js +1 -1
  8. package/dist/externalVersion.js +6 -6
  9. package/dist/locale/en-US.json +56 -2
  10. package/dist/locale/vi-VN.json +56 -2
  11. package/dist/locale/zh-CN.json +56 -2
  12. package/dist/node_modules/ajv/.tonic_example.js +20 -0
  13. package/dist/node_modules/ajv/LICENSE +22 -0
  14. package/dist/node_modules/ajv/dist/ajv.bundle.js +7189 -0
  15. package/dist/node_modules/ajv/dist/ajv.min.js +3 -0
  16. package/dist/node_modules/ajv/lib/ajv.d.ts +397 -0
  17. package/dist/node_modules/ajv/lib/ajv.js +3 -0
  18. package/dist/node_modules/ajv/lib/cache.js +26 -0
  19. package/dist/node_modules/ajv/lib/compile/async.js +90 -0
  20. package/dist/node_modules/ajv/lib/compile/equal.js +5 -0
  21. package/dist/node_modules/ajv/lib/compile/error_classes.js +34 -0
  22. package/dist/node_modules/ajv/lib/compile/formats.js +142 -0
  23. package/dist/node_modules/ajv/lib/compile/index.js +387 -0
  24. package/dist/node_modules/ajv/lib/compile/resolve.js +270 -0
  25. package/dist/node_modules/ajv/lib/compile/rules.js +66 -0
  26. package/dist/node_modules/ajv/lib/compile/schema_obj.js +9 -0
  27. package/dist/node_modules/ajv/lib/compile/ucs2length.js +20 -0
  28. package/dist/node_modules/ajv/lib/compile/util.js +239 -0
  29. package/dist/node_modules/ajv/lib/data.js +49 -0
  30. package/dist/node_modules/ajv/lib/definition_schema.js +37 -0
  31. package/dist/node_modules/ajv/lib/dot/_limit.jst +113 -0
  32. package/dist/node_modules/ajv/lib/dot/_limitItems.jst +12 -0
  33. package/dist/node_modules/ajv/lib/dot/_limitLength.jst +12 -0
  34. package/dist/node_modules/ajv/lib/dot/_limitProperties.jst +12 -0
  35. package/dist/node_modules/ajv/lib/dot/allOf.jst +32 -0
  36. package/dist/node_modules/ajv/lib/dot/anyOf.jst +46 -0
  37. package/dist/node_modules/ajv/lib/dot/coerce.def +51 -0
  38. package/dist/node_modules/ajv/lib/dot/comment.jst +9 -0
  39. package/dist/node_modules/ajv/lib/dot/const.jst +11 -0
  40. package/dist/node_modules/ajv/lib/dot/contains.jst +55 -0
  41. package/dist/node_modules/ajv/lib/dot/custom.jst +191 -0
  42. package/dist/node_modules/ajv/lib/dot/defaults.def +47 -0
  43. package/dist/node_modules/ajv/lib/dot/definitions.def +203 -0
  44. package/dist/node_modules/ajv/lib/dot/dependencies.jst +79 -0
  45. package/dist/node_modules/ajv/lib/dot/enum.jst +30 -0
  46. package/dist/node_modules/ajv/lib/dot/errors.def +194 -0
  47. package/dist/node_modules/ajv/lib/dot/format.jst +106 -0
  48. package/dist/node_modules/ajv/lib/dot/if.jst +73 -0
  49. package/dist/node_modules/ajv/lib/dot/items.jst +98 -0
  50. package/dist/node_modules/ajv/lib/dot/missing.def +39 -0
  51. package/dist/node_modules/ajv/lib/dot/multipleOf.jst +22 -0
  52. package/dist/node_modules/ajv/lib/dot/not.jst +43 -0
  53. package/dist/node_modules/ajv/lib/dot/oneOf.jst +54 -0
  54. package/dist/node_modules/ajv/lib/dot/pattern.jst +14 -0
  55. package/dist/node_modules/ajv/lib/dot/properties.jst +245 -0
  56. package/dist/node_modules/ajv/lib/dot/propertyNames.jst +52 -0
  57. package/dist/node_modules/ajv/lib/dot/ref.jst +85 -0
  58. package/dist/node_modules/ajv/lib/dot/required.jst +108 -0
  59. package/dist/node_modules/ajv/lib/dot/uniqueItems.jst +62 -0
  60. package/dist/node_modules/ajv/lib/dot/validate.jst +276 -0
  61. package/dist/node_modules/ajv/lib/dotjs/_limit.js +163 -0
  62. package/dist/node_modules/ajv/lib/dotjs/_limitItems.js +80 -0
  63. package/dist/node_modules/ajv/lib/dotjs/_limitLength.js +85 -0
  64. package/dist/node_modules/ajv/lib/dotjs/_limitProperties.js +80 -0
  65. package/dist/node_modules/ajv/lib/dotjs/allOf.js +42 -0
  66. package/dist/node_modules/ajv/lib/dotjs/anyOf.js +73 -0
  67. package/dist/node_modules/ajv/lib/dotjs/comment.js +14 -0
  68. package/dist/node_modules/ajv/lib/dotjs/const.js +56 -0
  69. package/dist/node_modules/ajv/lib/dotjs/contains.js +81 -0
  70. package/dist/node_modules/ajv/lib/dotjs/custom.js +228 -0
  71. package/dist/node_modules/ajv/lib/dotjs/dependencies.js +168 -0
  72. package/dist/node_modules/ajv/lib/dotjs/enum.js +66 -0
  73. package/dist/node_modules/ajv/lib/dotjs/format.js +150 -0
  74. package/dist/node_modules/ajv/lib/dotjs/if.js +103 -0
  75. package/dist/node_modules/ajv/lib/dotjs/index.js +33 -0
  76. package/dist/node_modules/ajv/lib/dotjs/items.js +140 -0
  77. package/dist/node_modules/ajv/lib/dotjs/multipleOf.js +80 -0
  78. package/dist/node_modules/ajv/lib/dotjs/not.js +84 -0
  79. package/dist/node_modules/ajv/lib/dotjs/oneOf.js +73 -0
  80. package/dist/node_modules/ajv/lib/dotjs/pattern.js +75 -0
  81. package/dist/node_modules/ajv/lib/dotjs/properties.js +335 -0
  82. package/dist/node_modules/ajv/lib/dotjs/propertyNames.js +81 -0
  83. package/dist/node_modules/ajv/lib/dotjs/ref.js +124 -0
  84. package/dist/node_modules/ajv/lib/dotjs/required.js +270 -0
  85. package/dist/node_modules/ajv/lib/dotjs/uniqueItems.js +86 -0
  86. package/dist/node_modules/ajv/lib/dotjs/validate.js +482 -0
  87. package/dist/node_modules/ajv/lib/keyword.js +146 -0
  88. package/dist/node_modules/ajv/lib/refs/data.json +17 -0
  89. package/dist/node_modules/ajv/lib/refs/json-schema-draft-04.json +149 -0
  90. package/dist/node_modules/ajv/lib/refs/json-schema-draft-06.json +154 -0
  91. package/dist/node_modules/ajv/lib/refs/json-schema-draft-07.json +168 -0
  92. package/dist/node_modules/ajv/lib/refs/json-schema-secure.json +94 -0
  93. package/dist/node_modules/ajv/node_modules/json-schema-traverse/.eslintrc.yml +27 -0
  94. package/dist/node_modules/ajv/node_modules/json-schema-traverse/.travis.yml +8 -0
  95. package/dist/node_modules/ajv/node_modules/json-schema-traverse/index.js +89 -0
  96. package/dist/node_modules/ajv/node_modules/json-schema-traverse/package.json +43 -0
  97. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/.eslintrc.yml +6 -0
  98. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/fixtures/schema.js +125 -0
  99. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/index.spec.js +171 -0
  100. package/dist/node_modules/ajv/package.json +1 -0
  101. package/dist/node_modules/ajv/scripts/.eslintrc.yml +3 -0
  102. package/dist/node_modules/ajv/scripts/bundle.js +61 -0
  103. package/dist/node_modules/ajv/scripts/compile-dots.js +73 -0
  104. package/dist/node_modules/ajv/scripts/info +10 -0
  105. package/dist/node_modules/ajv/scripts/prepare-tests +12 -0
  106. package/dist/node_modules/ajv/scripts/publish-built-version +32 -0
  107. package/dist/node_modules/ajv/scripts/travis-gh-pages +23 -0
  108. package/dist/server/collections/skill-definitions.js +31 -0
  109. package/dist/server/collections/skill-executions.js +19 -0
  110. package/dist/server/collections/skill-registry-installations.js +70 -0
  111. package/dist/server/migrations/20260621000000-native-policy-profile-defaults.js +0 -3
  112. package/dist/server/migrations/20260727000000-remove-unused-knowledge-scopes.js +51 -0
  113. package/dist/server/migrations/20260727010000-backfill-skill-tool-names.js +66 -0
  114. package/dist/server/plugin.js +107 -2
  115. package/dist/server/resources/agent-knowledge-insights.js +273 -0
  116. package/dist/server/services/AgentExecutionContext.js +45 -0
  117. package/dist/server/services/AgentMemoryContextService.js +57 -19
  118. package/dist/server/services/CodeValidator.js +1 -0
  119. package/dist/server/services/ExecutionSpanService.js +10 -2
  120. package/dist/server/services/InputSchemaValidator.js +83 -0
  121. package/dist/server/services/NativeSubAgentObserver.js +63 -8
  122. package/dist/server/services/SandboxRunner.js +6 -2
  123. package/dist/server/services/SkillAccessService.js +109 -0
  124. package/dist/server/services/SkillManager.js +2 -0
  125. package/dist/server/services/SkillRepositoryService.js +70 -0
  126. package/dist/server/skill-hub/actions/git-import.js +479 -216
  127. package/dist/server/skill-hub/mcp/McpController.js +8 -5
  128. package/dist/server/skill-hub/middlewares/git-import-request-method-policy.js +69 -0
  129. package/dist/server/skill-hub/plugin.js +68 -14
  130. package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +249 -0
  131. package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +228 -0
  132. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +15 -3
  133. package/dist/server/tools/external-rag-search.js +152 -12
  134. package/dist/server/tools/skill-execute.js +23 -2
  135. package/dist/server/utils/ctx-utils.js +2 -2
  136. package/dist/server/utils/skill-tool-name.js +73 -0
  137. package/package.json +9 -3
  138. package/src/client-v2/components/AgentBindingsTab.tsx +269 -0
  139. package/src/client-v2/components/HarnessProfilesTab.tsx +1 -2
  140. package/src/client-v2/components/KnowledgeAccessTab.tsx +116 -0
  141. package/src/client-v2/components/MemoryInspectorTab.tsx +125 -0
  142. package/src/client-v2/components/RetrievalTraceTab.tsx +162 -0
  143. package/src/client-v2/pages/AgentBindingsPage.tsx +13 -0
  144. package/src/client-v2/pages/KnowledgeAccessPage.tsx +10 -0
  145. package/src/client-v2/pages/MemoryInspectorPage.tsx +13 -0
  146. package/src/client-v2/pages/RetrievalTracePage.tsx +10 -0
  147. package/src/client-v2/plugin.tsx +28 -0
  148. package/src/locale/en-US.json +56 -2
  149. package/src/locale/vi-VN.json +56 -2
  150. package/src/locale/zh-CN.json +56 -2
  151. package/src/server/__tests__/agent-execution-context.test.ts +18 -0
  152. package/src/server/__tests__/agent-knowledge-insights.test.ts +123 -0
  153. package/src/server/__tests__/external-rag-search.test.ts +107 -0
  154. package/src/server/__tests__/input-schema-validator.test.ts +26 -0
  155. package/src/server/__tests__/native-sub-agent-observer.test.ts +141 -9
  156. package/src/server/__tests__/skill-access-service.test.ts +64 -0
  157. package/src/server/__tests__/skill-execute.test.ts +37 -0
  158. package/src/server/__tests__/skill-tool-name.test.ts +23 -0
  159. package/src/server/collections/skill-definitions.ts +31 -0
  160. package/src/server/collections/skill-executions.ts +19 -0
  161. package/src/server/collections/skill-registry-installations.ts +41 -0
  162. package/src/server/migrations/20260621000000-native-policy-profile-defaults.ts +0 -3
  163. package/src/server/migrations/20260727000000-remove-unused-knowledge-scopes.ts +25 -0
  164. package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -0
  165. package/src/server/plugin.ts +107 -1
  166. package/src/server/resources/agent-knowledge-insights.ts +304 -0
  167. package/src/server/services/AgentExecutionContext.ts +23 -0
  168. package/src/server/services/AgentMemoryContextService.ts +63 -19
  169. package/src/server/services/CodeValidator.ts +1 -0
  170. package/src/server/services/ExecutionSpanService.ts +119 -110
  171. package/src/server/services/InputSchemaValidator.ts +44 -0
  172. package/src/server/services/NativeSubAgentObserver.ts +592 -528
  173. package/src/server/services/SandboxRunner.ts +6 -0
  174. package/src/server/services/SkillAccessService.ts +100 -0
  175. package/src/server/services/SkillManager.ts +2 -0
  176. package/src/server/services/SkillRepositoryService.ts +98 -2
  177. package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -0
  178. package/src/server/skill-hub/actions/__tests__/git-import.test.ts +501 -0
  179. package/src/server/skill-hub/actions/git-import.ts +682 -236
  180. package/src/server/skill-hub/mcp/McpController.ts +8 -14
  181. package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -0
  182. package/src/server/skill-hub/plugin.ts +79 -13
  183. package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +281 -0
  184. package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +239 -0
  185. package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -0
  186. package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +63 -0
  187. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +14 -2
  188. package/src/server/tools/external-rag-search.ts +215 -14
  189. package/src/server/tools/skill-execute.ts +24 -2
  190. package/src/server/utils/ctx-utils.ts +1 -1
  191. package/src/server/utils/skill-tool-name.ts +58 -0
  192. 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 { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from 'fs';
2
- import { resolve, relative } from 'path';
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
+ });