plugin-agent-orchestrator 1.1.0 → 1.1.3

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 (162) hide show
  1. package/dist/externalVersion.js +6 -6
  2. package/dist/locale/en-US.json +7 -1
  3. package/dist/locale/vi-VN.json +7 -1
  4. package/dist/locale/zh-CN.json +7 -1
  5. package/dist/node_modules/ajv/.tonic_example.js +20 -0
  6. package/dist/node_modules/ajv/LICENSE +22 -0
  7. package/dist/node_modules/ajv/dist/ajv.bundle.js +7189 -0
  8. package/dist/node_modules/ajv/dist/ajv.min.js +3 -0
  9. package/dist/node_modules/ajv/lib/ajv.d.ts +397 -0
  10. package/dist/node_modules/ajv/lib/ajv.js +3 -0
  11. package/dist/node_modules/ajv/lib/cache.js +26 -0
  12. package/dist/node_modules/ajv/lib/compile/async.js +90 -0
  13. package/dist/node_modules/ajv/lib/compile/equal.js +5 -0
  14. package/dist/node_modules/ajv/lib/compile/error_classes.js +34 -0
  15. package/dist/node_modules/ajv/lib/compile/formats.js +142 -0
  16. package/dist/node_modules/ajv/lib/compile/index.js +387 -0
  17. package/dist/node_modules/ajv/lib/compile/resolve.js +270 -0
  18. package/dist/node_modules/ajv/lib/compile/rules.js +66 -0
  19. package/dist/node_modules/ajv/lib/compile/schema_obj.js +9 -0
  20. package/dist/node_modules/ajv/lib/compile/ucs2length.js +20 -0
  21. package/dist/node_modules/ajv/lib/compile/util.js +239 -0
  22. package/dist/node_modules/ajv/lib/data.js +49 -0
  23. package/dist/node_modules/ajv/lib/definition_schema.js +37 -0
  24. package/dist/node_modules/ajv/lib/dot/_limit.jst +113 -0
  25. package/dist/node_modules/ajv/lib/dot/_limitItems.jst +12 -0
  26. package/dist/node_modules/ajv/lib/dot/_limitLength.jst +12 -0
  27. package/dist/node_modules/ajv/lib/dot/_limitProperties.jst +12 -0
  28. package/dist/node_modules/ajv/lib/dot/allOf.jst +32 -0
  29. package/dist/node_modules/ajv/lib/dot/anyOf.jst +46 -0
  30. package/dist/node_modules/ajv/lib/dot/coerce.def +51 -0
  31. package/dist/node_modules/ajv/lib/dot/comment.jst +9 -0
  32. package/dist/node_modules/ajv/lib/dot/const.jst +11 -0
  33. package/dist/node_modules/ajv/lib/dot/contains.jst +55 -0
  34. package/dist/node_modules/ajv/lib/dot/custom.jst +191 -0
  35. package/dist/node_modules/ajv/lib/dot/defaults.def +47 -0
  36. package/dist/node_modules/ajv/lib/dot/definitions.def +203 -0
  37. package/dist/node_modules/ajv/lib/dot/dependencies.jst +79 -0
  38. package/dist/node_modules/ajv/lib/dot/enum.jst +30 -0
  39. package/dist/node_modules/ajv/lib/dot/errors.def +194 -0
  40. package/dist/node_modules/ajv/lib/dot/format.jst +106 -0
  41. package/dist/node_modules/ajv/lib/dot/if.jst +73 -0
  42. package/dist/node_modules/ajv/lib/dot/items.jst +98 -0
  43. package/dist/node_modules/ajv/lib/dot/missing.def +39 -0
  44. package/dist/node_modules/ajv/lib/dot/multipleOf.jst +22 -0
  45. package/dist/node_modules/ajv/lib/dot/not.jst +43 -0
  46. package/dist/node_modules/ajv/lib/dot/oneOf.jst +54 -0
  47. package/dist/node_modules/ajv/lib/dot/pattern.jst +14 -0
  48. package/dist/node_modules/ajv/lib/dot/properties.jst +245 -0
  49. package/dist/node_modules/ajv/lib/dot/propertyNames.jst +52 -0
  50. package/dist/node_modules/ajv/lib/dot/ref.jst +85 -0
  51. package/dist/node_modules/ajv/lib/dot/required.jst +108 -0
  52. package/dist/node_modules/ajv/lib/dot/uniqueItems.jst +62 -0
  53. package/dist/node_modules/ajv/lib/dot/validate.jst +276 -0
  54. package/dist/node_modules/ajv/lib/dotjs/_limit.js +163 -0
  55. package/dist/node_modules/ajv/lib/dotjs/_limitItems.js +80 -0
  56. package/dist/node_modules/ajv/lib/dotjs/_limitLength.js +85 -0
  57. package/dist/node_modules/ajv/lib/dotjs/_limitProperties.js +80 -0
  58. package/dist/node_modules/ajv/lib/dotjs/allOf.js +42 -0
  59. package/dist/node_modules/ajv/lib/dotjs/anyOf.js +73 -0
  60. package/dist/node_modules/ajv/lib/dotjs/comment.js +14 -0
  61. package/dist/node_modules/ajv/lib/dotjs/const.js +56 -0
  62. package/dist/node_modules/ajv/lib/dotjs/contains.js +81 -0
  63. package/dist/node_modules/ajv/lib/dotjs/custom.js +228 -0
  64. package/dist/node_modules/ajv/lib/dotjs/dependencies.js +168 -0
  65. package/dist/node_modules/ajv/lib/dotjs/enum.js +66 -0
  66. package/dist/node_modules/ajv/lib/dotjs/format.js +150 -0
  67. package/dist/node_modules/ajv/lib/dotjs/if.js +103 -0
  68. package/dist/node_modules/ajv/lib/dotjs/index.js +33 -0
  69. package/dist/node_modules/ajv/lib/dotjs/items.js +140 -0
  70. package/dist/node_modules/ajv/lib/dotjs/multipleOf.js +80 -0
  71. package/dist/node_modules/ajv/lib/dotjs/not.js +84 -0
  72. package/dist/node_modules/ajv/lib/dotjs/oneOf.js +73 -0
  73. package/dist/node_modules/ajv/lib/dotjs/pattern.js +75 -0
  74. package/dist/node_modules/ajv/lib/dotjs/properties.js +335 -0
  75. package/dist/node_modules/ajv/lib/dotjs/propertyNames.js +81 -0
  76. package/dist/node_modules/ajv/lib/dotjs/ref.js +124 -0
  77. package/dist/node_modules/ajv/lib/dotjs/required.js +270 -0
  78. package/dist/node_modules/ajv/lib/dotjs/uniqueItems.js +86 -0
  79. package/dist/node_modules/ajv/lib/dotjs/validate.js +482 -0
  80. package/dist/node_modules/ajv/lib/keyword.js +146 -0
  81. package/dist/node_modules/ajv/lib/refs/data.json +17 -0
  82. package/dist/node_modules/ajv/lib/refs/json-schema-draft-04.json +149 -0
  83. package/dist/node_modules/ajv/lib/refs/json-schema-draft-06.json +154 -0
  84. package/dist/node_modules/ajv/lib/refs/json-schema-draft-07.json +168 -0
  85. package/dist/node_modules/ajv/lib/refs/json-schema-secure.json +94 -0
  86. package/dist/node_modules/ajv/node_modules/json-schema-traverse/.eslintrc.yml +27 -0
  87. package/dist/node_modules/ajv/node_modules/json-schema-traverse/.travis.yml +8 -0
  88. package/dist/node_modules/ajv/node_modules/json-schema-traverse/index.js +89 -0
  89. package/dist/node_modules/ajv/node_modules/json-schema-traverse/package.json +43 -0
  90. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/.eslintrc.yml +6 -0
  91. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/fixtures/schema.js +125 -0
  92. package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/index.spec.js +171 -0
  93. package/dist/node_modules/ajv/package.json +1 -0
  94. package/dist/node_modules/ajv/scripts/.eslintrc.yml +3 -0
  95. package/dist/node_modules/ajv/scripts/bundle.js +61 -0
  96. package/dist/node_modules/ajv/scripts/compile-dots.js +73 -0
  97. package/dist/node_modules/ajv/scripts/info +10 -0
  98. package/dist/node_modules/ajv/scripts/prepare-tests +12 -0
  99. package/dist/node_modules/ajv/scripts/publish-built-version +32 -0
  100. package/dist/node_modules/ajv/scripts/travis-gh-pages +23 -0
  101. package/dist/server/collections/skill-definitions.js +31 -0
  102. package/dist/server/collections/skill-executions.js +19 -0
  103. package/dist/server/collections/skill-registry-installations.js +70 -0
  104. package/dist/server/migrations/20260727010000-backfill-skill-tool-names.js +66 -0
  105. package/dist/server/plugin.js +96 -1
  106. package/dist/server/services/AgentExecutionContext.js +45 -0
  107. package/dist/server/services/CodeValidator.js +1 -0
  108. package/dist/server/services/ExecutionSpanService.js +10 -2
  109. package/dist/server/services/InputSchemaValidator.js +83 -0
  110. package/dist/server/services/NativeSubAgentObserver.js +55 -8
  111. package/dist/server/services/SandboxRunner.js +6 -2
  112. package/dist/server/services/SkillAccessService.js +109 -0
  113. package/dist/server/services/SkillManager.js +2 -0
  114. package/dist/server/services/SkillRepositoryService.js +70 -0
  115. package/dist/server/skill-hub/actions/git-import.js +479 -216
  116. package/dist/server/skill-hub/mcp/McpController.js +8 -5
  117. package/dist/server/skill-hub/middlewares/git-import-request-method-policy.js +69 -0
  118. package/dist/server/skill-hub/plugin.js +68 -14
  119. package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +249 -0
  120. package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +228 -0
  121. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +15 -3
  122. package/dist/server/tools/skill-execute.js +23 -2
  123. package/dist/server/utils/ctx-utils.js +2 -2
  124. package/dist/server/utils/skill-tool-name.js +73 -0
  125. package/package.json +9 -3
  126. package/src/locale/en-US.json +7 -1
  127. package/src/locale/vi-VN.json +7 -1
  128. package/src/locale/zh-CN.json +7 -1
  129. package/src/server/__tests__/agent-execution-context.test.ts +18 -0
  130. package/src/server/__tests__/input-schema-validator.test.ts +26 -0
  131. package/src/server/__tests__/native-sub-agent-observer.test.ts +52 -2
  132. package/src/server/__tests__/skill-access-service.test.ts +64 -0
  133. package/src/server/__tests__/skill-execute.test.ts +37 -0
  134. package/src/server/__tests__/skill-tool-name.test.ts +23 -0
  135. package/src/server/collections/skill-definitions.ts +31 -0
  136. package/src/server/collections/skill-executions.ts +19 -0
  137. package/src/server/collections/skill-registry-installations.ts +41 -0
  138. package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -0
  139. package/src/server/plugin.ts +104 -1
  140. package/src/server/services/AgentExecutionContext.ts +23 -0
  141. package/src/server/services/CodeValidator.ts +1 -0
  142. package/src/server/services/ExecutionSpanService.ts +119 -110
  143. package/src/server/services/InputSchemaValidator.ts +44 -0
  144. package/src/server/services/NativeSubAgentObserver.ts +64 -10
  145. package/src/server/services/SandboxRunner.ts +6 -0
  146. package/src/server/services/SkillAccessService.ts +100 -0
  147. package/src/server/services/SkillManager.ts +2 -0
  148. package/src/server/services/SkillRepositoryService.ts +98 -2
  149. package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -0
  150. package/src/server/skill-hub/actions/__tests__/git-import.test.ts +549 -0
  151. package/src/server/skill-hub/actions/git-import.ts +682 -236
  152. package/src/server/skill-hub/mcp/McpController.ts +8 -14
  153. package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -0
  154. package/src/server/skill-hub/plugin.ts +79 -13
  155. package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +281 -0
  156. package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +239 -0
  157. package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -0
  158. package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +63 -0
  159. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +14 -2
  160. package/src/server/tools/skill-execute.ts +24 -2
  161. package/src/server/utils/ctx-utils.ts +1 -1
  162. package/src/server/utils/skill-tool-name.ts +58 -0
@@ -1,9 +1,9 @@
1
- import { Context } from '@nocobase/actions';
2
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
3
- import * as path from 'path';
4
- import { parseJsonText, stringifyJsonText, parseJsonLike, parseSkillMarkdown } from '../utils/json-fields';
1
+ import type { Context } from '@nocobase/actions';
5
2
 
6
- type SkillManifestEntry = Record<string, any> & {
3
+ import { stringifyJsonText, parseJsonLike, parseSkillMarkdown } from '../utils/json-fields';
4
+ import { assertSkillToolNameAvailable, buildSkillToolName } from '../../utils/skill-tool-name';
5
+
6
+ type SkillManifestEntry = Record<string, unknown> & {
7
7
  folder?: string;
8
8
  name?: string;
9
9
  title?: string;
@@ -13,6 +13,30 @@ type SkillManifestEntry = Record<string, any> & {
13
13
  pluginSource?: string;
14
14
  codeTemplate?: string;
15
15
  codeFile?: string;
16
+ timeoutSeconds?: unknown;
17
+ maxOutputSizeMb?: unknown;
18
+ toolScope?: string;
19
+ enabled?: unknown;
20
+ autoCall?: unknown;
21
+ packages?: unknown;
22
+ inputSchema?: unknown;
23
+ interactionSchema?: unknown;
24
+ };
25
+
26
+ type SkillDefinitionValues = Record<string, unknown> & {
27
+ name: string;
28
+ toolName?: string;
29
+ title: string;
30
+ description: string;
31
+ language: string;
32
+ storageType: string;
33
+ storageUrl: string;
34
+ timeoutSeconds: number;
35
+ maxOutputSizeMb: number;
36
+ toolScope: string;
37
+ enabled: boolean;
38
+ autoCall: boolean;
39
+ packages: string;
16
40
  };
17
41
 
18
42
  type SkillManifest = {
@@ -22,15 +46,99 @@ type SkillManifest = {
22
46
  initializedAt?: string;
23
47
  };
24
48
 
49
+ type GitManagerAccessContext = {
50
+ kind: 'user';
51
+ roles: string[];
52
+ };
53
+
54
+ type GitTreeEntry = {
55
+ type: 'blob' | 'tree';
56
+ path: string;
57
+ size: number;
58
+ };
59
+
60
+ interface GitManagerContentService {
61
+ contractVersion?: number;
62
+ capabilities?: readonly string[];
63
+ resolveCommit(repositoryId: string | number, ref: string, access: GitManagerAccessContext): Promise<string>;
64
+ listTree(
65
+ input: {
66
+ repositoryId: string | number;
67
+ commitSha: string;
68
+ rootPath: string;
69
+ recursive: boolean;
70
+ },
71
+ access: GitManagerAccessContext,
72
+ ): Promise<GitTreeEntry[]>;
73
+ readFile(
74
+ input: {
75
+ repositoryId: string | number;
76
+ commitSha: string;
77
+ filePath: string;
78
+ maxBytes?: number;
79
+ },
80
+ access: GitManagerAccessContext,
81
+ ): Promise<Buffer>;
82
+ }
83
+
84
+ type GitManagerPlugin = {
85
+ skillHubContentService?: unknown;
86
+ };
87
+
88
+ type GitManagerPluginManager = {
89
+ get(name: string): unknown;
90
+ };
91
+
92
+ type GitImportErrorCode =
93
+ | 'GIT_MANAGER_CONTENT_UNAVAILABLE'
94
+ | 'SKILL_HUB_REPOSITORY_ACCESS_DENIED'
95
+ | 'SKILL_HUB_SKILL_NOT_LISTED'
96
+ | 'REGISTRY_CONTENT_LIMIT_EXCEEDED'
97
+ | 'REGISTRY_GIT_FILE_NOT_FOUND';
98
+
99
+ type GitImportTranslation = {
100
+ key: string;
101
+ values?: Record<string, string | number>;
102
+ };
103
+
104
+ class GitImportError extends Error {
105
+ constructor(
106
+ readonly code: GitImportErrorCode,
107
+ readonly status: number,
108
+ message: string,
109
+ readonly translation?: GitImportTranslation,
110
+ ) {
111
+ super(message);
112
+ this.name = 'GitImportError';
113
+ }
114
+ }
115
+
116
+ const GIT_MANAGER_CONTENT_CAPABILITY = 'skill-hub-content-with-actor';
117
+ const GIT_MANAGER_CONTENT_CONTRACT_VERSION = 2;
118
+ const MISSING_GIT_FILE_CODE = 'REGISTRY_GIT_FILE_NOT_FOUND';
119
+ const ACCESS_DENIED_CODE = 'SKILL_HUB_REPOSITORY_ACCESS_DENIED';
120
+ const CONTENT_LIMIT_CODE = 'REGISTRY_CONTENT_LIMIT_EXCEEDED';
121
+ const GIT_IMPORT_NAMESPACE = 'plugin-agent-orchestrator';
122
+
25
123
  const CODE_FILES = [
26
124
  ['index.py', 'python'],
27
125
  ['index.js', 'node'],
28
126
  ['main.py', 'python'],
29
127
  ] as const;
30
128
 
129
+ export const GIT_SKILL_MARKDOWN_LIMITS = Object.freeze({
130
+ maxFiles: 64,
131
+ maxBytes: 2 * 1024 * 1024,
132
+ });
133
+
31
134
  /**
32
135
  * Git integration actions for skill-hub.
33
136
  *
137
+ * All repository reads go through Git Manager's actor-aware content service.
138
+ * This keeps the repository-level scope used by Git Manager HTTP actions in
139
+ * force for Skill Hub imports; Skill Hub must never read a Git Manager clone
140
+ * directly from disk.
141
+ *
34
142
  * Supported manifest shape:
35
143
  * {
36
144
  * "skills": [
@@ -47,57 +155,51 @@ export async function gitListSkills(ctx: Context, next: () => Promise<void>) {
47
155
  ctx.throw(400, 'repositoryId and rootFolder are required');
48
156
  }
49
157
 
50
- const gitPlugin = ctx.app.pm.get('plugin-git-manager') as any;
51
- if (!gitPlugin) {
52
- ctx.throw(400, 'plugin-git-manager is not installed');
53
- }
54
-
55
158
  try {
56
- const repo = await ctx.db.getRepository('gitRepositories').findOne({ filterByTk: repositoryId });
57
- if (!repo) ctx.throw(404, 'Repository not found');
58
-
59
- const simpleGit = require('simple-git').default;
60
- const localPath = getLocalPath(repo.get('localPath'));
61
- const git = simpleGit(localPath);
159
+ const service = getGitManagerContentService(ctx);
160
+ const access = accessFromContext(ctx);
161
+ const commitSha = await service.resolveCommit(repositoryId, ref, access);
62
162
  const configPath = joinGitPath(rootFolder, 'skills.json');
63
-
64
- const loaded = await loadSkillsManifest(git, ref, localPath, configPath);
65
- let config = loaded.config;
66
- let initialized = false;
67
-
68
- if (!loaded.exists) {
69
- const discovered = await discoverSkills(git, ref, rootFolder);
70
- config = createSkillsManifest(rootFolder, discovered);
71
- initialized = initSkillsManifestFile(localPath, configPath, config);
72
- }
163
+ const skillsRoot = await resolveSkillsRoot(service, repositoryId, commitSha, access, rootFolder);
164
+ const loaded = await loadSkillsManifest(service, repositoryId, commitSha, access, configPath);
165
+ const config = loaded.exists
166
+ ? loaded.config
167
+ : createSkillsManifest(
168
+ rootFolder,
169
+ await discoverSkills(service, repositoryId, commitSha, access, rootFolder, skillsRoot),
170
+ );
73
171
 
74
172
  const manifestSkills = Array.isArray(config.skills) ? config.skills.filter((skill) => getSkillKey(skill)) : [];
75
173
  const enriched = manifestSkills.map((skill) => enrichListedSkill(skill, prefix));
76
174
 
77
175
  const existingSkills = enriched.length
78
176
  ? await ctx.db.getRepository('skillDefinitions').find({
79
- filter: { name: { $in: enriched.map((s) => s.name) } },
177
+ filter: { name: { $in: enriched.map((skill) => skill.name) } },
80
178
  fields: ['name'],
81
179
  })
82
180
  : [];
83
- const existingNames = new Set(existingSkills.map((s: any) => s.get('name')));
181
+ const existingNames = new Set(
182
+ existingSkills.map((skill: { get(attribute: string): unknown }) => skill.get('name')),
183
+ );
84
184
 
85
185
  ctx.body = {
86
- data: enriched.map((s) => ({
87
- ...s,
88
- existsInDb: existingNames.has(s.name),
186
+ data: enriched.map((skill) => ({
187
+ ...skill,
188
+ existsInDb: existingNames.has(skill.name),
89
189
  })),
90
190
  config: {
91
191
  name: config.name || rootFolder || 'skills',
92
192
  description: config.description || '',
93
193
  rootFolder,
94
194
  path: configPath,
95
- initializedSkillsJson: initialized,
195
+ // An absent manifest is intentionally virtual. Writing a new file to
196
+ // Git Manager's checkout would reintroduce a cross-plugin disk bypass.
197
+ initializedSkillsJson: false,
96
198
  },
97
199
  };
98
200
  await next();
99
- } catch (err: any) {
100
- ctx.throw(400, err.message);
201
+ } catch (error) {
202
+ throwGitImportError(ctx, error);
101
203
  }
102
204
  }
103
205
 
@@ -116,220 +218,483 @@ export async function gitSyncSkills(ctx: Context, next: () => Promise<void>) {
116
218
  if (!repositoryId || !Array.isArray(selectedKeys) || selectedKeys.length === 0) {
117
219
  ctx.throw(400, 'repositoryId, rootFolder and skills[] are required');
118
220
  }
221
+ const selectedSkillKeys = selectedKeys.filter(
222
+ (key): key is string => typeof key === 'string' && key.trim().length > 0,
223
+ );
224
+ if (selectedSkillKeys.length !== selectedKeys.length) {
225
+ ctx.throw(400, translateGitImportMessage(ctx, 'skills[] must contain non-empty string keys'));
226
+ }
119
227
 
120
- const repo = await ctx.db.getRepository('gitRepositories').findOne({ filterByTk: repositoryId });
121
- if (!repo) ctx.throw(404, 'Repository not found');
228
+ try {
229
+ const service = getGitManagerContentService(ctx);
230
+ const access = accessFromContext(ctx);
231
+ const commitSha = await service.resolveCommit(repositoryId, ref, access);
232
+ const configPath = joinGitPath(rootFolder, 'skills.json');
233
+ const skillsRoot = await resolveSkillsRoot(service, repositoryId, commitSha, access, rootFolder);
234
+ const loaded = await loadSkillsManifest(service, repositoryId, commitSha, access, configPath);
235
+ const config = loaded.exists
236
+ ? loaded.config
237
+ : createSkillsManifest(
238
+ rootFolder,
239
+ await discoverSkills(service, repositoryId, commitSha, access, rootFolder, skillsRoot),
240
+ );
241
+
242
+ const manifest = new Map<string, SkillManifestEntry>();
243
+ for (const entry of config.skills || []) {
244
+ const key = getSkillKey(entry);
245
+ if (key) {
246
+ manifest.set(key, entry);
247
+ }
248
+ }
249
+ if (loaded.exists) {
250
+ const unlistedKeys = selectedSkillKeys.filter((key) => !manifest.has(key));
251
+ if (unlistedKeys.length > 0) {
252
+ const skills = unlistedKeys.join(', ');
253
+ throw new GitImportError(
254
+ 'SKILL_HUB_SKILL_NOT_LISTED',
255
+ 400,
256
+ `Selected skills are not listed in the explicit skills.json manifest: ${skills}`,
257
+ {
258
+ key: 'Selected skills are not listed in the explicit skills.json manifest: {{skills}}',
259
+ values: { skills },
260
+ },
261
+ );
262
+ }
263
+ }
122
264
 
123
- const simpleGit = require('simple-git').default;
124
- const localPath = getLocalPath(repo.get('localPath'));
125
- const git = simpleGit(localPath);
126
- const configPath = joinGitPath(rootFolder, 'skills.json');
265
+ const results: Array<Record<string, unknown>> = [];
266
+ const skillRepo = ctx.db.getRepository('skillDefinitions');
267
+
268
+ for (const key of selectedSkillKeys) {
269
+ const meta: SkillManifestEntry = manifest.get(key) || { folder: key, name: key };
270
+ const isPluginSkill = isPluginStorage(meta);
271
+ const rawName = meta.name || meta.pluginSource || meta.folder || key;
272
+ const skillName = `${prefix}${rawName}`;
273
+ const skillsSubDir = meta.folder ? joinGitPath(skillsRoot.path, meta.folder) : '';
274
+ const skillBaseDir = skillsSubDir || rootFolder;
275
+
276
+ try {
277
+ let frontmatter: SkillManifestEntry = {};
278
+ let instructions = '';
279
+
280
+ if (skillBaseDir || !meta.folder) {
281
+ const skillMarkdown = await readOptionalGitFile(
282
+ service,
283
+ repositoryId,
284
+ commitSha,
285
+ access,
286
+ joinGitPath(skillBaseDir, 'SKILL.md'),
287
+ );
288
+ if (skillMarkdown) {
289
+ try {
290
+ const parsed = parseSkillMarkdown(skillMarkdown);
291
+ frontmatter = parsed.metadata;
292
+ instructions = parsed.body;
293
+
294
+ const otherMarkdown = await fetchAllMarkdownInFolder(
295
+ service,
296
+ repositoryId,
297
+ commitSha,
298
+ access,
299
+ skillBaseDir,
300
+ );
301
+ if (otherMarkdown) instructions += otherMarkdown;
302
+ } catch (error) {
303
+ if (isFatalGitContentError(error)) {
304
+ throw error;
305
+ }
306
+ // SKILL.md is optional; malformed optional metadata must not
307
+ // prevent a manifest-defined skill from being imported.
308
+ }
309
+ }
310
+ }
127
311
 
128
- const loaded = await loadSkillsManifest(git, ref, localPath, configPath);
129
- let config = loaded.config;
130
- if (!loaded.exists) {
131
- const discovered = await discoverSkills(git, ref, rootFolder);
132
- config = createSkillsManifest(rootFolder, discovered);
133
- initSkillsManifestFile(localPath, configPath, config);
134
- }
312
+ const storageType = normalizeStorageType(
313
+ frontmatter.storageType || meta.storageType || (isPluginSkill ? 'plugin' : 'git'),
314
+ );
315
+ const pluginSource =
316
+ frontmatter.pluginSource || meta.pluginSource || (storageType === 'plugin' ? rawName : undefined);
317
+
318
+ let detectedLanguage = frontmatter.language || meta.language || 'python';
319
+ let codeTemplate = frontmatter.codeTemplate || meta.codeTemplate || '';
320
+
321
+ if (!codeTemplate && meta.codeFile) {
322
+ codeTemplate = await readOptionalGitFile(
323
+ service,
324
+ repositoryId,
325
+ commitSha,
326
+ access,
327
+ joinGitPath(skillBaseDir, meta.codeFile),
328
+ );
329
+ }
135
330
 
136
- const manifest = new Map<string, SkillManifestEntry>();
137
- for (const entry of config.skills || []) {
138
- const key = getSkillKey(entry);
139
- if (key) {
140
- manifest.set(key, entry);
141
- }
142
- }
331
+ if (!codeTemplate && storageType !== 'plugin') {
332
+ const codeFile = await readConventionalCodeFile(service, repositoryId, commitSha, access, skillBaseDir);
333
+ if (codeFile) {
334
+ codeTemplate = codeFile.content;
335
+ detectedLanguage = codeFile.language;
336
+ }
337
+ }
143
338
 
144
- const results: any[] = [];
145
- const skillRepo = ctx.db.getRepository('skillDefinitions');
146
-
147
- for (const key of selectedKeys) {
148
- const meta: SkillManifestEntry = manifest.get(key) || { folder: key, name: key };
149
- const isPluginSkill = isPluginStorage(meta);
150
- const rawName = meta.name || meta.pluginSource || meta.folder || key;
151
- const skillName = `${prefix}${rawName}`;
152
- const skillsSubDir = meta.folder ? joinGitPath(rootFolder, 'skills', meta.folder) : '';
153
- const skillBaseDir = skillsSubDir || rootFolder;
154
-
155
- try {
156
- let frontmatter: Record<string, any> = {};
157
- let instructions = '';
158
-
159
- if (skillBaseDir || !meta.folder) {
160
- try {
161
- const skillMd = await git.show([`${ref}:${joinGitPath(skillBaseDir, 'SKILL.md')}`]);
162
- const parsed = parseSkillMarkdown(skillMd);
163
- frontmatter = parsed.metadata;
164
- instructions = parsed.body;
165
-
166
- const otherMds = await fetchAllMarkdownInFolder(git, ref, skillBaseDir);
167
- if (otherMds) instructions += otherMds;
168
- } catch {
169
- // SKILL.md is optional; manifest data is enough.
339
+ const toolName = buildSkillToolName(skillName);
340
+ const merged: SkillDefinitionValues = {
341
+ name: skillName,
342
+ toolName,
343
+ title: frontmatter.title || meta.title || rawName,
344
+ description: frontmatter.description || meta.description || '',
345
+ language: detectedLanguage,
346
+ storageType,
347
+ storageUrl:
348
+ storageType === 'plugin'
349
+ ? `git://${repositoryId}/${rootFolder || ''}@${commitSha}#${pluginSource || rawName}`
350
+ : `git://${repositoryId}/${skillsSubDir || rootFolder || ''}@${commitSha}`,
351
+ timeoutSeconds: parseInteger(frontmatter.timeoutSeconds ?? meta.timeoutSeconds, 60),
352
+ maxOutputSizeMb: parseInteger(frontmatter.maxOutputSizeMb ?? meta.maxOutputSizeMb, 50),
353
+ toolScope: frontmatter.toolScope || meta.toolScope || 'CUSTOM',
354
+ enabled: parseBoolean(frontmatter.enabled ?? meta.enabled, true),
355
+ autoCall: parseBoolean(frontmatter.autoCall ?? meta.autoCall, false),
356
+ packages: stringifyJsonText(parseJsonLike(meta.packages ?? frontmatter.packages, []), []),
357
+ };
358
+
359
+ if (codeTemplate) {
360
+ merged.codeTemplate = codeTemplate;
361
+ }
362
+ if (pluginSource) {
363
+ merged.pluginSource = pluginSource;
364
+ }
365
+ if (instructions) {
366
+ merged.instructions = instructions.trim();
170
367
  }
171
- }
172
368
 
173
- const storageType = normalizeStorageType(
174
- frontmatter.storageType || meta.storageType || (isPluginSkill ? 'plugin' : 'git'),
175
- );
176
- const pluginSource =
177
- frontmatter.pluginSource || meta.pluginSource || (storageType === 'plugin' ? rawName : undefined);
369
+ const inputSchema = parseJsonLike(frontmatter.inputSchema ?? meta.inputSchema, null);
370
+ if (inputSchema) {
371
+ merged.inputSchema = stringifyJsonText(inputSchema);
372
+ }
178
373
 
179
- let detectedLanguage = frontmatter.language || meta.language || 'python';
180
- let codeTemplate = frontmatter.codeTemplate || meta.codeTemplate || '';
374
+ const interactionSchema = parseJsonLike(frontmatter.interactionSchema ?? meta.interactionSchema, null);
375
+ if (interactionSchema) {
376
+ merged.interactionSchema = stringifyJsonText(interactionSchema);
377
+ }
181
378
 
182
- if (!codeTemplate && meta.codeFile) {
183
- codeTemplate = await readOptionalGitFile(git, ref, joinGitPath(skillBaseDir, meta.codeFile));
184
- }
379
+ const existing = await skillRepo.findOne({ filter: { name: skillName } });
380
+ if (existing && !overwrite) {
381
+ results.push({ folder: key, name: skillName, status: 'skipped', reason: 'Already exists' });
382
+ continue;
383
+ }
185
384
 
186
- if (!codeTemplate && storageType !== 'plugin') {
187
- const codeFile = await readConventionalCodeFile(git, ref, skillBaseDir);
188
- if (codeFile) {
189
- codeTemplate = codeFile.content;
190
- detectedLanguage = codeFile.language;
385
+ if (existing) {
386
+ delete merged.toolName;
387
+ await skillRepo.update({ filterByTk: existing.get('id'), values: merged });
388
+ results.push({ folder: key, name: skillName, status: 'updated' });
389
+ } else {
390
+ await assertSkillToolNameAvailable(ctx.db, toolName);
391
+ await skillRepo.create({ values: merged });
392
+ results.push({ folder: key, name: skillName, status: 'created' });
393
+ }
394
+ } catch (error) {
395
+ if (isFatalGitContentError(error)) {
396
+ throw error;
191
397
  }
398
+ results.push({ folder: key, name: skillName, status: 'error', reason: errorMessage(error) });
192
399
  }
400
+ }
193
401
 
194
- const merged: Record<string, any> = {
195
- name: skillName,
196
- title: frontmatter.title || meta.title || rawName,
197
- description: frontmatter.description || meta.description || '',
198
- language: detectedLanguage,
199
- storageType,
200
- storageUrl:
201
- storageType === 'plugin'
202
- ? `git://${repositoryId}/${rootFolder || ''}@${ref}#${pluginSource || rawName}`
203
- : `git://${repositoryId}/${skillsSubDir || rootFolder || ''}@${ref}`,
204
- timeoutSeconds: parseInteger(frontmatter.timeoutSeconds ?? meta.timeoutSeconds, 60),
205
- maxOutputSizeMb: parseInteger(frontmatter.maxOutputSizeMb ?? meta.maxOutputSizeMb, 50),
206
- toolScope: frontmatter.toolScope || meta.toolScope || 'CUSTOM',
207
- enabled: parseBoolean(frontmatter.enabled ?? meta.enabled, true),
208
- autoCall: parseBoolean(frontmatter.autoCall ?? meta.autoCall, false),
209
- packages: stringifyJsonText(parseJsonLike(meta.packages ?? frontmatter.packages, []), []),
210
- };
211
-
212
- if (codeTemplate) {
213
- merged.codeTemplate = codeTemplate;
214
- }
215
- if (pluginSource) {
216
- merged.pluginSource = pluginSource;
217
- }
218
- if (instructions) {
219
- merged.instructions = instructions.trim();
220
- }
402
+ ctx.body = { data: results };
403
+ await next();
404
+ } catch (error) {
405
+ throwGitImportError(ctx, error);
406
+ }
407
+ }
221
408
 
222
- const inputSchema = parseJsonLike(frontmatter.inputSchema ?? meta.inputSchema, null);
223
- if (inputSchema) {
224
- merged.inputSchema = stringifyJsonText(inputSchema);
225
- }
409
+ function getGitManagerContentService(ctx: Context): GitManagerContentService {
410
+ const pluginManager = (ctx.app as unknown as { pm?: GitManagerPluginManager }).pm;
411
+ const plugin = pluginManager?.get('plugin-git-manager');
412
+ if (!plugin || typeof plugin !== 'object') {
413
+ throw new GitImportError(
414
+ 'GIT_MANAGER_CONTENT_UNAVAILABLE',
415
+ 424,
416
+ 'Git Manager is not enabled for Skill Hub imports.',
417
+ );
418
+ }
226
419
 
227
- const interactionSchema = parseJsonLike(frontmatter.interactionSchema ?? meta.interactionSchema, null);
228
- if (interactionSchema) {
229
- merged.interactionSchema = stringifyJsonText(interactionSchema);
230
- }
420
+ const service = (plugin as GitManagerPlugin).skillHubContentService;
421
+ if (!service || typeof service !== 'object') {
422
+ throw new GitImportError(
423
+ 'GIT_MANAGER_CONTENT_UNAVAILABLE',
424
+ 424,
425
+ 'Git Manager must support actor-aware content reads for Skill Hub imports.',
426
+ );
427
+ }
231
428
 
232
- const existing = await skillRepo.findOne({ filter: { name: skillName } });
233
- if (existing && !overwrite) {
234
- results.push({ folder: key, name: skillName, status: 'skipped', reason: 'Already exists' });
235
- continue;
236
- }
429
+ const candidate = service as Partial<GitManagerContentService>;
430
+ const contractVersion = candidate.contractVersion;
431
+ const capabilities = candidate.capabilities;
432
+ const hasCompatibleContract =
433
+ typeof contractVersion === 'number' &&
434
+ Number.isSafeInteger(contractVersion) &&
435
+ contractVersion >= GIT_MANAGER_CONTENT_CONTRACT_VERSION;
436
+ if (
437
+ !hasCompatibleContract ||
438
+ !Array.isArray(capabilities) ||
439
+ !capabilities.includes(GIT_MANAGER_CONTENT_CAPABILITY) ||
440
+ typeof candidate.resolveCommit !== 'function' ||
441
+ typeof candidate.listTree !== 'function' ||
442
+ typeof candidate.readFile !== 'function'
443
+ ) {
444
+ throw new GitImportError(
445
+ 'GIT_MANAGER_CONTENT_UNAVAILABLE',
446
+ 424,
447
+ 'Git Manager must support actor-aware content reads for Skill Hub imports.',
448
+ );
449
+ }
450
+ return service as GitManagerContentService;
451
+ }
237
452
 
238
- if (existing) {
239
- await skillRepo.update({ filterByTk: existing.get('id'), values: merged });
240
- results.push({ folder: key, name: skillName, status: 'updated' });
241
- } else {
242
- await skillRepo.create({ values: merged });
243
- results.push({ folder: key, name: skillName, status: 'created' });
244
- }
245
- } catch (err: any) {
246
- results.push({ folder: key, name: skillName, status: 'error', reason: err.message });
247
- }
453
+ function accessFromContext(ctx: Context): GitManagerAccessContext {
454
+ const currentRoles = (ctx.state as unknown as { currentRoles?: unknown } | undefined)?.currentRoles;
455
+ if (!Array.isArray(currentRoles) || !currentRoles.every((role): role is string => typeof role === 'string')) {
456
+ throw new GitImportError(
457
+ 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
458
+ 403,
459
+ 'A valid user role context is required to import skills from Git Manager.',
460
+ );
248
461
  }
462
+ return { kind: 'user', roles: currentRoles };
463
+ }
249
464
 
250
- ctx.body = { data: results };
251
- await next();
465
+ function errorCode(error: unknown): string | undefined {
466
+ if (!error || typeof error !== 'object') return undefined;
467
+ const code = (error as { code?: unknown }).code;
468
+ return typeof code === 'string' ? code : undefined;
252
469
  }
253
470
 
254
- async function loadSkillsManifest(git: any, ref: string, localPath: string, configPath: string) {
255
- let raw: string | null = null;
471
+ function errorMessage(error: unknown): string {
472
+ return error instanceof Error ? error.message : String(error);
473
+ }
256
474
 
257
- try {
258
- raw = await git.show([`${ref}:${configPath}`]);
259
- } catch {
260
- const filePath = resolveRepoFile(localPath, configPath);
261
- if (existsSync(filePath)) {
262
- raw = readFileSync(filePath, 'utf8');
263
- }
475
+ function isMissingGitFile(error: unknown): boolean {
476
+ return errorCode(error) === MISSING_GIT_FILE_CODE;
477
+ }
478
+
479
+ function isFatalGitContentError(error: unknown): boolean {
480
+ if (error instanceof GitImportError) return true;
481
+ const code = errorCode(error);
482
+ return code === ACCESS_DENIED_CODE || code === CONTENT_LIMIT_CODE;
483
+ }
484
+
485
+ function throwGitImportError(ctx: Context, error: unknown): never {
486
+ if (error instanceof GitImportError) {
487
+ return throwHttpError(ctx, error.status, error.message, error.translation);
264
488
  }
265
489
 
266
- if (!raw) {
267
- return { exists: false, config: {} as SkillManifest };
490
+ switch (errorCode(error)) {
491
+ case ACCESS_DENIED_CODE:
492
+ return throwHttpError(ctx, 403, errorMessage(error));
493
+ case CONTENT_LIMIT_CODE:
494
+ return throwHttpError(ctx, 422, errorMessage(error));
495
+ case MISSING_GIT_FILE_CODE:
496
+ return throwHttpError(ctx, 404, errorMessage(error));
497
+ default:
498
+ return throwHttpError(ctx, 502, 'Git Manager could not read the requested repository content.');
499
+ }
500
+ }
501
+
502
+ function translateGitImportMessage(
503
+ ctx: Context,
504
+ key: string,
505
+ values?: Record<string, string | number>,
506
+ fallback = key,
507
+ ): string {
508
+ const translate = (ctx as unknown as { t?: unknown }).t;
509
+ if (typeof translate !== 'function') {
510
+ return fallback;
511
+ }
512
+ const translated = translate(key, { ns: GIT_IMPORT_NAMESPACE, ...values });
513
+ return typeof translated === 'string' ? translated : fallback;
514
+ }
515
+
516
+ function throwHttpError(ctx: Context, status: number, message: string, translation?: GitImportTranslation): never {
517
+ const localizedMessage = translation
518
+ ? translateGitImportMessage(ctx, translation.key, translation.values, message)
519
+ : message;
520
+ return (ctx as unknown as { throw(status: number, message: string): never }).throw(status, localizedMessage);
521
+ }
522
+
523
+ async function loadSkillsManifest(
524
+ service: GitManagerContentService,
525
+ repositoryId: string | number,
526
+ commitSha: string,
527
+ access: GitManagerAccessContext,
528
+ configPath: string,
529
+ ): Promise<{ exists: boolean; config: SkillManifest }> {
530
+ let raw: string;
531
+ try {
532
+ raw = (await service.readFile({ repositoryId, commitSha, filePath: configPath }, access)).toString('utf8');
533
+ } catch (error) {
534
+ if (isMissingGitFile(error)) {
535
+ return { exists: false, config: {} };
536
+ }
537
+ throw error;
268
538
  }
269
539
 
270
540
  try {
271
541
  return { exists: true, config: JSON.parse(raw) as SkillManifest };
272
- } catch (err: any) {
273
- throw new Error(`Invalid ${configPath}: ${err.message}`);
542
+ } catch (error) {
543
+ throw new Error(`Invalid ${configPath}: ${errorMessage(error)}`);
274
544
  }
275
545
  }
276
546
 
277
- async function discoverSkills(git: any, ref: string, rootFolder: string): Promise<SkillManifestEntry[]> {
278
- const folderSkills = await discoverSkillFolders(git, ref, rootFolder);
279
- if (folderSkills.length > 0) {
280
- return folderSkills;
547
+ async function discoverSkills(
548
+ service: GitManagerContentService,
549
+ repositoryId: string | number,
550
+ commitSha: string,
551
+ access: GitManagerAccessContext,
552
+ rootFolder: string,
553
+ skillsRoot: ResolvedSkillsRoot,
554
+ ): Promise<SkillManifestEntry[]> {
555
+ if (skillsRoot.folders.length > 0) {
556
+ return skillsRoot.folders;
281
557
  }
282
558
 
283
- const pluginSkill = await discoverPluginSkill(git, ref, rootFolder);
559
+ const pluginSkill = await discoverPluginSkill(service, repositoryId, commitSha, access, rootFolder);
284
560
  return pluginSkill ? [pluginSkill] : [];
285
561
  }
286
562
 
287
- async function discoverSkillFolders(git: any, ref: string, rootFolder: string): Promise<SkillManifestEntry[]> {
288
- const baseDir = joinGitPath(rootFolder, 'skills');
563
+ type ResolvedSkillsRoot = {
564
+ path: string;
565
+ folders: SkillManifestEntry[];
566
+ };
567
+
568
+ async function resolveSkillsRoot(
569
+ service: GitManagerContentService,
570
+ repositoryId: string | number,
571
+ commitSha: string,
572
+ access: GitManagerAccessContext,
573
+ rootFolder: string,
574
+ ): Promise<ResolvedSkillsRoot> {
575
+ const configuredRoot = normalizeRootFolder(rootFolder);
576
+ const rootEntries = await service.listTree(
577
+ { repositoryId, commitSha, rootPath: configuredRoot, recursive: false },
578
+ access,
579
+ );
580
+ const configuredRootIsSkillsDirectory = configuredRoot === 'skills' || configuredRoot.endsWith('/skills');
581
+ const directFolders = await discoverSkillFoldersAtRoot(
582
+ service,
583
+ repositoryId,
584
+ commitSha,
585
+ access,
586
+ configuredRoot,
587
+ rootEntries,
588
+ );
589
+ if (directFolders.length > 0 || configuredRootIsSkillsDirectory) {
590
+ return { path: configuredRoot, folders: directFolders };
591
+ }
289
592
 
593
+ const hasSkillsSubfolder = rootEntries.some((entry) => entry.type === 'tree' && entry.path === 'skills');
594
+ if (!hasSkillsSubfolder) {
595
+ return { path: configuredRoot, folders: directFolders };
596
+ }
597
+
598
+ const fallbackRoot = joinGitPath(configuredRoot, 'skills');
599
+ return {
600
+ path: fallbackRoot,
601
+ folders: await discoverSkillFoldersAtRoot(service, repositoryId, commitSha, access, fallbackRoot),
602
+ };
603
+ }
604
+
605
+ async function discoverSkillFoldersAtRoot(
606
+ service: GitManagerContentService,
607
+ repositoryId: string | number,
608
+ commitSha: string,
609
+ access: GitManagerAccessContext,
610
+ baseDir: string,
611
+ rootEntries?: GitTreeEntry[],
612
+ ): Promise<SkillManifestEntry[]> {
290
613
  try {
291
- const dirs = await git.raw(['ls-tree', '-d', '--name-only', `${ref}:${baseDir}/`]);
292
- return dirs
293
- .trim()
294
- .split('\n')
295
- .filter(Boolean)
296
- .map((folder: string) => ({
297
- folder,
298
- name: folder,
299
- title: folder,
300
- storageType: 'git',
301
- }));
302
- } catch {
303
- return [];
614
+ const entries =
615
+ rootEntries || (await service.listTree({ repositoryId, commitSha, rootPath: baseDir, recursive: false }, access));
616
+ const folders = entries.filter((entry) => entry.type === 'tree').map((entry) => entry.path);
617
+ const discovered: SkillManifestEntry[] = [];
618
+ const batchSize = 16;
619
+ for (let offset = 0; offset < folders.length; offset += batchSize) {
620
+ const batch = folders.slice(offset, offset + batchSize);
621
+ const results = await Promise.all(
622
+ batch.map(async (folder) => {
623
+ const folderEntries = await service.listTree(
624
+ { repositoryId, commitSha, rootPath: joinGitPath(baseDir, folder), recursive: false },
625
+ access,
626
+ );
627
+ return folderEntries.some((entry) => entry.type === 'blob' && entry.path === 'SKILL.md') ? folder : null;
628
+ }),
629
+ );
630
+ for (const folder of results) {
631
+ if (folder) {
632
+ discovered.push({ folder, name: folder, title: folder, storageType: 'git' });
633
+ }
634
+ }
635
+ }
636
+ return discovered;
637
+ } catch (error) {
638
+ if (isMissingGitFile(error)) {
639
+ return [];
640
+ }
641
+ throw error;
304
642
  }
305
643
  }
306
644
 
307
- async function discoverPluginSkill(git: any, ref: string, rootFolder: string): Promise<SkillManifestEntry | null> {
645
+ async function discoverPluginSkill(
646
+ service: GitManagerContentService,
647
+ repositoryId: string | number,
648
+ commitSha: string,
649
+ access: GitManagerAccessContext,
650
+ rootFolder: string,
651
+ ): Promise<SkillManifestEntry | null> {
308
652
  const packageJsonPath = joinGitPath(rootFolder, 'package.json');
309
- let packageJson: any;
653
+ const rawPackageJson = await readOptionalGitFile(service, repositoryId, commitSha, access, packageJsonPath);
654
+ if (!rawPackageJson) {
655
+ return null;
656
+ }
310
657
 
658
+ let packageJson: Record<string, unknown>;
311
659
  try {
312
- packageJson = JSON.parse(await git.show([`${ref}:${packageJsonPath}`]));
660
+ const parsed: unknown = JSON.parse(rawPackageJson);
661
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
662
+ return null;
663
+ }
664
+ packageJson = parsed as Record<string, unknown>;
313
665
  } catch {
314
666
  return null;
315
667
  }
316
668
 
317
669
  const definition =
318
- (await readOptionalGitFile(git, ref, joinGitPath(rootFolder, 'src/server/skill-definition.ts'))) ||
319
- (await readOptionalGitFile(git, ref, joinGitPath(rootFolder, 'src/server/skill-definition.js')));
670
+ (await readOptionalGitFile(
671
+ service,
672
+ repositoryId,
673
+ commitSha,
674
+ access,
675
+ joinGitPath(rootFolder, 'src/server/skill-definition.ts'),
676
+ )) ||
677
+ (await readOptionalGitFile(
678
+ service,
679
+ repositoryId,
680
+ commitSha,
681
+ access,
682
+ joinGitPath(rootFolder, 'src/server/skill-definition.js'),
683
+ ));
320
684
  const templateName = definition ? extractStringProperty(definition, 'name') : null;
321
685
  const title = definition ? extractStringProperty(definition, 'title') : null;
322
686
  const language = definition ? extractStringProperty(definition, 'language') : null;
323
687
 
324
- const pluginSource = templateName || packageJson.name;
688
+ const packageName = stringValue(packageJson.name);
689
+ const pluginSource = templateName || packageName;
325
690
  if (!pluginSource) {
326
691
  return null;
327
692
  }
328
693
 
329
694
  return {
330
695
  name: pluginSource,
331
- title: title || packageJson.displayName || packageJson.name || pluginSource,
332
- description: packageJson.description || '',
696
+ title: title || stringValue(packageJson.displayName) || packageName || pluginSource,
697
+ description: stringValue(packageJson.description) || '',
333
698
  language: language || 'python',
334
699
  storageType: 'plugin',
335
700
  pluginSource,
@@ -345,20 +710,21 @@ function createSkillsManifest(rootFolder: string, skills: SkillManifestEntry[]):
345
710
  };
346
711
  }
347
712
 
348
- function initSkillsManifestFile(localPath: string, configPath: string, config: SkillManifest) {
349
- const filePath = resolveRepoFile(localPath, configPath);
350
- if (existsSync(filePath)) {
351
- return false;
352
- }
353
-
354
- mkdirSync(path.dirname(filePath), { recursive: true });
355
- writeFileSync(filePath, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
356
- return true;
357
- }
358
-
359
- async function readConventionalCodeFile(git: any, ref: string, skillsSubDir: string) {
713
+ async function readConventionalCodeFile(
714
+ service: GitManagerContentService,
715
+ repositoryId: string | number,
716
+ commitSha: string,
717
+ access: GitManagerAccessContext,
718
+ skillsSubDir: string,
719
+ ) {
360
720
  for (const [file, language] of CODE_FILES) {
361
- const content = await readOptionalGitFile(git, ref, joinGitPath(skillsSubDir, file));
721
+ const content = await readOptionalGitFile(
722
+ service,
723
+ repositoryId,
724
+ commitSha,
725
+ access,
726
+ joinGitPath(skillsSubDir, file),
727
+ );
362
728
  if (content) {
363
729
  return { content, language };
364
730
  }
@@ -366,11 +732,23 @@ async function readConventionalCodeFile(git: any, ref: string, skillsSubDir: str
366
732
  return null;
367
733
  }
368
734
 
369
- async function readOptionalGitFile(git: any, ref: string, filePath: string): Promise<string> {
735
+ async function readOptionalGitFile(
736
+ service: GitManagerContentService,
737
+ repositoryId: string | number,
738
+ commitSha: string,
739
+ access: GitManagerAccessContext,
740
+ filePath: string,
741
+ maxBytes?: number,
742
+ ): Promise<string> {
370
743
  try {
371
- return await git.show([`${ref}:${filePath}`]);
372
- } catch {
373
- return '';
744
+ const input =
745
+ maxBytes === undefined ? { repositoryId, commitSha, filePath } : { repositoryId, commitSha, filePath, maxBytes };
746
+ return (await service.readFile(input, access)).toString('utf8');
747
+ } catch (error) {
748
+ if (isMissingGitFile(error)) {
749
+ return '';
750
+ }
751
+ throw error;
374
752
  }
375
753
  }
376
754
 
@@ -402,7 +780,7 @@ function isPluginStorage(skill: SkillManifestEntry) {
402
780
  return normalizeStorageType(skill.storageType) === 'plugin' || !!skill.pluginSource;
403
781
  }
404
782
 
405
- function normalizeStorageType(storageType: any) {
783
+ function normalizeStorageType(storageType: unknown) {
406
784
  const value = typeof storageType === 'string' ? storageType.toLowerCase() : '';
407
785
  if (value === 'plugin') return 'plugin';
408
786
  if (value === 'local') return 'local';
@@ -410,12 +788,12 @@ function normalizeStorageType(storageType: any) {
410
788
  return 'git';
411
789
  }
412
790
 
413
- function parseInteger(value: any, fallback: number) {
414
- const parsed = parseInt(value, 10);
791
+ function parseInteger(value: unknown, fallback: number) {
792
+ const parsed = Number.parseInt(String(value), 10);
415
793
  return Number.isFinite(parsed) ? parsed : fallback;
416
794
  }
417
795
 
418
- function parseBoolean(value: any, fallback: boolean) {
796
+ function parseBoolean(value: unknown, fallback: boolean) {
419
797
  if (value === undefined || value === null || value === '') return fallback;
420
798
  if (typeof value === 'boolean') return value;
421
799
  if (typeof value === 'string') return !['false', '0', 'no'].includes(value.toLowerCase());
@@ -423,11 +801,11 @@ function parseBoolean(value: any, fallback: boolean) {
423
801
  }
424
802
 
425
803
  function extractStringProperty(source: string, property: string) {
426
- const match = source.match(new RegExp(`${property}\\s*:\\s*(['"\`])([\\s\\S]*?)\\1`));
804
+ const match = source.match(new RegExp(property + '\\s*:\\s*([\'"\\x60])([\\s\\S]*?)\\1'));
427
805
  return match ? match[2] : null;
428
806
  }
429
807
 
430
- function normalizeRootFolder(rootFolder: any) {
808
+ function normalizeRootFolder(rootFolder: unknown) {
431
809
  if (rootFolder === undefined || rootFolder === null) {
432
810
  throw new Error('repositoryId and rootFolder are required');
433
811
  }
@@ -441,6 +819,10 @@ function normalizeRootFolder(rootFolder: any) {
441
819
  return normalized;
442
820
  }
443
821
 
822
+ function stringValue(value: unknown): string | undefined {
823
+ return typeof value === 'string' && value.trim() ? value.trim() : undefined;
824
+ }
825
+
444
826
  function joinGitPath(...parts: Array<string | undefined>) {
445
827
  return parts
446
828
  .filter((part) => part !== undefined && part !== '')
@@ -448,37 +830,101 @@ function joinGitPath(...parts: Array<string | undefined>) {
448
830
  .replace(/\/+/g, '/');
449
831
  }
450
832
 
451
- function resolveRepoFile(localPath: string, gitPath: string): string {
452
- const basePath = path.resolve(localPath);
453
- const resolved = path.resolve(basePath, ...gitPath.split('/').filter(Boolean));
454
- if (resolved !== basePath && !resolved.startsWith(basePath + path.sep)) {
455
- throw new Error('Invalid file path');
456
- }
457
- return resolved;
833
+ function markdownContentHeader(path: string): string {
834
+ return `\n\n--- Content from ${path} ---\n\n`;
458
835
  }
459
836
 
460
- function getLocalPath(localPath: string): string {
461
- const basePath = process.env.GIT_REPOS_BASE_PATH || path.join(process.cwd(), 'storage', 'git-repos');
462
- if (path.isAbsolute(localPath)) return localPath;
463
- return path.resolve(basePath, localPath);
837
+ function markdownContentLimitError(message: string, translation?: GitImportTranslation): GitImportError {
838
+ return new GitImportError('REGISTRY_CONTENT_LIMIT_EXCEEDED', 422, message, translation);
464
839
  }
465
840
 
466
- async function fetchAllMarkdownInFolder(git: any, ref: string, folderPath: string): Promise<string> {
467
- let combined = '';
841
+ async function fetchAllMarkdownInFolder(
842
+ service: GitManagerContentService,
843
+ repositoryId: string | number,
844
+ commitSha: string,
845
+ access: GitManagerAccessContext,
846
+ folderPath: string,
847
+ ): Promise<string> {
468
848
  try {
469
- const filesOut = await git.raw(['ls-tree', '-r', '--name-only', `${ref}:${folderPath}/`]);
470
- const files = filesOut.trim().split('\n').filter(Boolean);
471
-
472
- for (const file of files) {
473
- if (file.toLowerCase().endsWith('.md') && file.toUpperCase() !== 'SKILL.md') {
474
- const content = await readOptionalGitFile(git, ref, joinGitPath(folderPath, file));
475
- if (content) {
476
- combined += `\n\n--- Content from ${file} ---\n\n${content}`;
849
+ const entries = await service.listTree({ repositoryId, commitSha, rootPath: folderPath, recursive: true }, access);
850
+ const markdownEntries = entries
851
+ .filter(
852
+ (entry) =>
853
+ entry.type === 'blob' && entry.path.toLowerCase().endsWith('.md') && entry.path.toUpperCase() !== 'SKILL.MD',
854
+ )
855
+ .sort((left, right) => left.path.localeCompare(right.path));
856
+ if (markdownEntries.length > GIT_SKILL_MARKDOWN_LIMITS.maxFiles) {
857
+ throw markdownContentLimitError(
858
+ `Git skill contains more than ${GIT_SKILL_MARKDOWN_LIMITS.maxFiles} supplemental Markdown files.`,
859
+ {
860
+ key: 'Git skill contains more than {{count}} supplemental Markdown files.',
861
+ values: { count: GIT_SKILL_MARKDOWN_LIMITS.maxFiles },
862
+ },
863
+ );
864
+ }
865
+
866
+ let declaredBytes = 0;
867
+ for (const entry of markdownEntries) {
868
+ if (!Number.isSafeInteger(entry.size) || entry.size < 0) {
869
+ throw markdownContentLimitError('Git skill contains a supplemental Markdown file with an invalid size.', {
870
+ key: 'Git skill contains a supplemental Markdown file with an invalid size.',
871
+ });
872
+ }
873
+ declaredBytes += Buffer.byteLength(markdownContentHeader(entry.path), 'utf8') + entry.size;
874
+ if (declaredBytes > GIT_SKILL_MARKDOWN_LIMITS.maxBytes) {
875
+ throw markdownContentLimitError(
876
+ `Git skill supplemental Markdown exceeds ${GIT_SKILL_MARKDOWN_LIMITS.maxBytes} bytes.`,
877
+ {
878
+ key: 'Git skill supplemental Markdown exceeds {{maxBytes}} bytes.',
879
+ values: { maxBytes: GIT_SKILL_MARKDOWN_LIMITS.maxBytes },
880
+ },
881
+ );
882
+ }
883
+ }
884
+
885
+ let combined = '';
886
+ let combinedBytes = 0;
887
+ for (const entry of markdownEntries) {
888
+ const header = markdownContentHeader(entry.path);
889
+ const remainingContentBytes =
890
+ GIT_SKILL_MARKDOWN_LIMITS.maxBytes - combinedBytes - Buffer.byteLength(header, 'utf8');
891
+ if (remainingContentBytes <= 0) {
892
+ throw markdownContentLimitError(
893
+ `Git skill supplemental Markdown exceeds ${GIT_SKILL_MARKDOWN_LIMITS.maxBytes} bytes.`,
894
+ {
895
+ key: 'Git skill supplemental Markdown exceeds {{maxBytes}} bytes.',
896
+ values: { maxBytes: GIT_SKILL_MARKDOWN_LIMITS.maxBytes },
897
+ },
898
+ );
899
+ }
900
+ const content = await readOptionalGitFile(
901
+ service,
902
+ repositoryId,
903
+ commitSha,
904
+ access,
905
+ joinGitPath(folderPath, entry.path),
906
+ remainingContentBytes,
907
+ );
908
+ if (content) {
909
+ const contentBytes = Buffer.byteLength(content, 'utf8');
910
+ if (contentBytes > remainingContentBytes) {
911
+ throw markdownContentLimitError(
912
+ `Git skill supplemental Markdown exceeds ${GIT_SKILL_MARKDOWN_LIMITS.maxBytes} bytes.`,
913
+ {
914
+ key: 'Git skill supplemental Markdown exceeds {{maxBytes}} bytes.',
915
+ values: { maxBytes: GIT_SKILL_MARKDOWN_LIMITS.maxBytes },
916
+ },
917
+ );
477
918
  }
919
+ combined += header + content;
920
+ combinedBytes += Buffer.byteLength(header, 'utf8') + contentBytes;
478
921
  }
479
922
  }
480
- } catch (err) {
481
- // Optional folder expansion can fail for missing paths or refs; keep import best-effort.
923
+ return combined;
924
+ } catch (error) {
925
+ if (isMissingGitFile(error)) {
926
+ return '';
927
+ }
928
+ throw error;
482
929
  }
483
- return combined;
484
930
  }