rol-websocket-channel 1.0.0

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 (58) hide show
  1. package/MQTT-API.md +967 -0
  2. package/dist/index.js +430 -0
  3. package/dist/message-handler.js +327 -0
  4. package/dist/src/admin/cli.js +43 -0
  5. package/dist/src/admin/jsonrpc.js +60 -0
  6. package/dist/src/admin/lib/fs.js +30 -0
  7. package/dist/src/admin/lib/paths.js +46 -0
  8. package/dist/src/admin/methods/admin.js +60 -0
  9. package/dist/src/admin/methods/agents-extended.js +235 -0
  10. package/dist/src/admin/methods/index.js +69 -0
  11. package/dist/src/admin/methods/memory.js +360 -0
  12. package/dist/src/admin/methods/models-extended.js +107 -0
  13. package/dist/src/admin/methods/models.js +39 -0
  14. package/dist/src/admin/methods/sessions-extended.js +207 -0
  15. package/dist/src/admin/methods/sessions.js +64 -0
  16. package/dist/src/admin/methods/skills-extended.js +157 -0
  17. package/dist/src/admin/methods/skills-toggle.js +182 -0
  18. package/dist/src/admin/methods/skills.js +384 -0
  19. package/dist/src/admin/methods/system.js +178 -0
  20. package/dist/src/admin/methods/usage.js +1170 -0
  21. package/dist/src/admin/types.js +1 -0
  22. package/dist/src/mqtt/connection-manager.js +155 -0
  23. package/dist/src/mqtt/index.js +5 -0
  24. package/dist/src/mqtt/mqtt-client.js +86 -0
  25. package/dist/src/mqtt/types.js +2 -0
  26. package/dist/src/shared/context.js +24 -0
  27. package/dist/src/shared/wrapper.js +23 -0
  28. package/index.ts +514 -0
  29. package/message-handler.ts +415 -0
  30. package/openclaw.plugin.json +84 -0
  31. package/package.json +35 -0
  32. package/readme.md +32 -0
  33. package/src/admin/cli.ts +60 -0
  34. package/src/admin/jsonrpc.ts +88 -0
  35. package/src/admin/lib/fs.ts +35 -0
  36. package/src/admin/lib/paths.ts +61 -0
  37. package/src/admin/methods/admin.ts +95 -0
  38. package/src/admin/methods/agents-extended.ts +310 -0
  39. package/src/admin/methods/index.ts +103 -0
  40. package/src/admin/methods/memory.ts +546 -0
  41. package/src/admin/methods/models-extended.ts +191 -0
  42. package/src/admin/methods/models.ts +103 -0
  43. package/src/admin/methods/sessions-extended.ts +313 -0
  44. package/src/admin/methods/sessions.ts +122 -0
  45. package/src/admin/methods/skills-extended.ts +249 -0
  46. package/src/admin/methods/skills-toggle.ts +235 -0
  47. package/src/admin/methods/skills.ts +651 -0
  48. package/src/admin/methods/system.ts +203 -0
  49. package/src/admin/methods/usage.ts +1491 -0
  50. package/src/admin/types.ts +46 -0
  51. package/src/mqtt/connection-manager.ts +188 -0
  52. package/src/mqtt/index.ts +6 -0
  53. package/src/mqtt/mqtt-client.ts +119 -0
  54. package/src/mqtt/types.ts +36 -0
  55. package/src/shared/context.ts +33 -0
  56. package/src/shared/wrapper.ts +35 -0
  57. package/tsconfig.json +16 -0
  58. package/types/openclaw.d.ts +74 -0
@@ -0,0 +1,182 @@
1
+ import { readJsonFile, writeJsonFile } from '../lib/fs.ts';
2
+ import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.ts';
3
+ import { getInstalledSkillsFromCli } from './skills.ts';
4
+ import path from 'node:path';
5
+ /**
6
+ * 启用或停用 skill
7
+ * 通过修改 agents.defaults.skills 数组来控制
8
+ */
9
+ export const toggleSkill = async (params, context) => {
10
+ const objectParams = expectObject(params);
11
+ const slug = expectString(objectParams.slug, 'slug');
12
+ const enabled = expectBoolean(objectParams.enabled, 'enabled');
13
+ const agentName = typeof objectParams.agentName === 'string' ? objectParams.agentName : 'defaults';
14
+ if (agentName !== 'defaults') {
15
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Currently only "defaults" agent is supported');
16
+ }
17
+ const allSkills = await getInstalledSkillsFromCli(context);
18
+ const runtimeSkill = allSkills.find((item) => isObject(item) && typeof item.slug === 'string' && item.slug === slug);
19
+ if (!runtimeSkill || !isObject(runtimeSkill)) {
20
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, `Skill not installed: ${slug}`);
21
+ }
22
+ const bundled = runtimeSkill.bundled === true;
23
+ const configPath = path.join(context.openclawRoot, 'openclaw.json');
24
+ const config = await readJsonFile(configPath);
25
+ if (!config.skills)
26
+ config.skills = {};
27
+ if (!config.skills.entries)
28
+ config.skills.entries = {};
29
+ if (!config.skills.entries[slug] || typeof config.skills.entries[slug] !== 'object') {
30
+ config.skills.entries[slug] = {};
31
+ }
32
+ config.skills.entries[slug].enabled = enabled;
33
+ if (bundled) {
34
+ if (!config.skills)
35
+ config.skills = {};
36
+ const currentAllowBundled = Array.isArray(config.skills.allowBundled)
37
+ ? config.skills.allowBundled
38
+ : [];
39
+ const skillIndex = currentAllowBundled.indexOf(slug);
40
+ if (enabled) {
41
+ if (skillIndex === -1) {
42
+ currentAllowBundled.push(slug);
43
+ config.skills.allowBundled = currentAllowBundled;
44
+ await writeJsonFile(configPath, config);
45
+ return {
46
+ success: true,
47
+ action: 'enabled',
48
+ slug,
49
+ bundled: true,
50
+ skillEntry: config.skills.entries[slug],
51
+ allowBundled: config.skills.allowBundled
52
+ };
53
+ }
54
+ await writeJsonFile(configPath, config);
55
+ return {
56
+ success: true,
57
+ action: 'already_enabled',
58
+ slug,
59
+ bundled: true,
60
+ skillEntry: config.skills.entries[slug],
61
+ allowBundled: currentAllowBundled
62
+ };
63
+ }
64
+ if (skillIndex !== -1) {
65
+ currentAllowBundled.splice(skillIndex, 1);
66
+ config.skills.allowBundled = currentAllowBundled;
67
+ await writeJsonFile(configPath, config);
68
+ return {
69
+ success: true,
70
+ action: 'disabled',
71
+ slug,
72
+ bundled: true,
73
+ skillEntry: config.skills.entries[slug],
74
+ allowBundled: config.skills.allowBundled
75
+ };
76
+ }
77
+ await writeJsonFile(configPath, config);
78
+ return {
79
+ success: true,
80
+ action: 'already_disabled',
81
+ slug,
82
+ bundled: true,
83
+ skillEntry: config.skills.entries[slug],
84
+ allowBundled: currentAllowBundled
85
+ };
86
+ }
87
+ const installPath = (() => {
88
+ if (!isObject(runtimeSkill))
89
+ return null;
90
+ const p = runtimeSkill.installPath ?? runtimeSkill.customInstallPath;
91
+ return typeof p === 'string' && p.length > 0 ? p : null;
92
+ })();
93
+ if (!installPath) {
94
+ const globalSkillPath = path.join(context.openclawRoot, 'skills', slug);
95
+ const workspaceSkillPath = path.join(context.openclawRoot, 'workspace', '.openclaw', 'skills', slug);
96
+ const { pathExists } = await import('../lib/fs.ts');
97
+ if (!(await pathExists(globalSkillPath)) && !(await pathExists(workspaceSkillPath))) {
98
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, `Skill not installed: ${slug}`);
99
+ }
100
+ }
101
+ if (!config.agents)
102
+ config.agents = {};
103
+ if (!config.agents.defaults)
104
+ config.agents.defaults = {};
105
+ if (!config.agents.defaults.skills)
106
+ config.agents.defaults.skills = [];
107
+ const currentSkills = config.agents.defaults.skills;
108
+ const skillIndex = currentSkills.indexOf(slug);
109
+ if (enabled) {
110
+ // 启用:如果不在列表中,添加
111
+ if (skillIndex === -1) {
112
+ currentSkills.push(slug);
113
+ await writeJsonFile(configPath, config);
114
+ return {
115
+ success: true,
116
+ action: 'enabled',
117
+ slug,
118
+ agentName,
119
+ skillEntry: config.skills.entries[slug],
120
+ currentSkills: config.agents.defaults.skills
121
+ };
122
+ }
123
+ else {
124
+ await writeJsonFile(configPath, config);
125
+ return {
126
+ success: true,
127
+ action: 'already_enabled',
128
+ slug,
129
+ agentName,
130
+ skillEntry: config.skills.entries[slug],
131
+ currentSkills: config.agents.defaults.skills
132
+ };
133
+ }
134
+ }
135
+ else {
136
+ // 停用:如果在列表中,移除
137
+ if (skillIndex !== -1) {
138
+ currentSkills.splice(skillIndex, 1);
139
+ await writeJsonFile(configPath, config);
140
+ return {
141
+ success: true,
142
+ action: 'disabled',
143
+ slug,
144
+ agentName,
145
+ skillEntry: config.skills.entries[slug],
146
+ currentSkills: config.agents.defaults.skills
147
+ };
148
+ }
149
+ else {
150
+ await writeJsonFile(configPath, config);
151
+ return {
152
+ success: true,
153
+ action: 'already_disabled',
154
+ slug,
155
+ agentName,
156
+ skillEntry: config.skills.entries[slug],
157
+ currentSkills: config.agents.defaults.skills
158
+ };
159
+ }
160
+ }
161
+ };
162
+ function expectObject(value) {
163
+ if (!value || Array.isArray(value) || typeof value !== 'object') {
164
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Params must be an object');
165
+ }
166
+ return value;
167
+ }
168
+ function expectString(value, fieldName) {
169
+ if (typeof value !== 'string' || value.trim().length === 0) {
170
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, `Field '${fieldName}' must be a non-empty string`);
171
+ }
172
+ return value.trim();
173
+ }
174
+ function expectBoolean(value, fieldName) {
175
+ if (typeof value !== 'boolean') {
176
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, `Field '${fieldName}' must be a boolean`);
177
+ }
178
+ return value;
179
+ }
180
+ function isObject(value) {
181
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
182
+ }
@@ -0,0 +1,384 @@
1
+ import fs from 'node:fs/promises';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { execFile } from 'node:child_process';
5
+ import { promisify } from 'node:util';
6
+ import { ensureDir, pathExists, readJsonFile } from '../lib/fs.ts';
7
+ import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.ts';
8
+ const execFileAsync = promisify(execFile);
9
+ export const listInstalledSkills = async (_params, context) => {
10
+ const items = await getInstalledSkillsFromCli(context);
11
+ return {
12
+ count: items.length,
13
+ items
14
+ };
15
+ };
16
+ export const installSkillFromNpm = async (params, context) => {
17
+ const objectParams = expectObject(params);
18
+ const packageSpec = expectString(objectParams.package, 'package');
19
+ const scope = normalizeScope(objectParams.scope);
20
+ const installRoot = resolveInstallRoot(context.openclawRoot, scope);
21
+ await ensureDir(installRoot);
22
+ const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'openclaw-skill-install-'));
23
+ try {
24
+ const tarballName = await npmPack(packageSpec, tempRoot);
25
+ const tarballPath = path.join(tempRoot, tarballName);
26
+ const extractRoot = path.join(tempRoot, 'extract');
27
+ await ensureDir(extractRoot);
28
+ await extractTarball(tarballPath, extractRoot);
29
+ const packageRoot = await resolvePackedPackageRoot(extractRoot);
30
+ await assertSkillPackage(packageRoot);
31
+ const skillInfo = await resolveSkillIdentity(packageRoot, packageSpec);
32
+ const targetDir = path.join(installRoot, skillInfo.dirName);
33
+ await fs.rm(targetDir, { recursive: true, force: true });
34
+ await fs.cp(packageRoot, targetDir, { recursive: true });
35
+ await fs.writeFile(path.join(targetDir, '.openclaw-admin-bridge-install.json'), JSON.stringify({
36
+ source: 'npm',
37
+ package: packageSpec,
38
+ scope,
39
+ installedAt: new Date().toISOString()
40
+ }, null, 2), 'utf8');
41
+ return {
42
+ ok: true,
43
+ scope,
44
+ package: packageSpec,
45
+ slug: skillInfo.slug,
46
+ skillName: skillInfo.displayName,
47
+ installPath: targetDir
48
+ };
49
+ }
50
+ finally {
51
+ await fs.rm(tempRoot, { recursive: true, force: true });
52
+ }
53
+ };
54
+ export async function getInstalledSkillsFromCli(context) {
55
+ const skillState = await getSkillState(context);
56
+ const cliItems = await queryOpenClawSkills(context);
57
+ const officialSkills = cliItems.map((item) => normalizeCliSkill(item, skillState));
58
+ const customSkills = await listCustomInstalledSkills(context, skillState.enabledCustomSkills);
59
+ return mergeSkillSources(officialSkills, customSkills);
60
+ }
61
+ function expectObject(value) {
62
+ if (!value || Array.isArray(value) || typeof value !== 'object') {
63
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Params must be an object');
64
+ }
65
+ return value;
66
+ }
67
+ function expectString(value, fieldName) {
68
+ if (typeof value !== 'string' || value.trim().length === 0) {
69
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, `Field '${fieldName}' must be a non-empty string`);
70
+ }
71
+ return value.trim();
72
+ }
73
+ function normalizeScope(rawScope) {
74
+ return rawScope === 'workspace' ? 'workspace' : 'global';
75
+ }
76
+ function resolveInstallRoot(openclawRoot, scope) {
77
+ return scope === 'workspace'
78
+ ? path.join(openclawRoot, 'workspace', '.openclaw', 'skills')
79
+ : path.join(openclawRoot, 'skills');
80
+ }
81
+ async function npmPack(packageSpec, cwd) {
82
+ const { stdout } = await execFileAsync('npm', ['pack', packageSpec], { cwd });
83
+ const tarballName = stdout
84
+ .split(/\r?\n/)
85
+ .map((line) => line.trim())
86
+ .filter(Boolean)
87
+ .at(-1);
88
+ if (!tarballName || !tarballName.endsWith('.tgz')) {
89
+ throw new JsonRpcException(JSON_RPC_ERRORS.internalError, 'npm pack did not return a tarball name', {
90
+ package: packageSpec,
91
+ stdout
92
+ });
93
+ }
94
+ return tarballName;
95
+ }
96
+ async function queryOpenClawSkills(context) {
97
+ const command = process.env.OPENCLAW_BIN || 'openclaw';
98
+ try {
99
+ const { stdout } = await execFileAsync(command, ['skills', 'list', '--json'], {
100
+ cwd: context.projectRoot
101
+ });
102
+ const parsed = JSON.parse(stdout);
103
+ if (Array.isArray(parsed)) {
104
+ return parsed;
105
+ }
106
+ if (isRecord(parsed)) {
107
+ const items = parsed.items;
108
+ const skills = parsed.skills;
109
+ if (Array.isArray(items))
110
+ return items;
111
+ if (Array.isArray(skills))
112
+ return skills;
113
+ }
114
+ throw new Error('Unexpected JSON shape returned by OpenClaw CLI');
115
+ }
116
+ catch (err) {
117
+ throw new JsonRpcException(JSON_RPC_ERRORS.internalError, `Failed to query OpenClaw skills list: ${err instanceof Error ? err.message : String(err)}`);
118
+ }
119
+ }
120
+ function normalizeCliSkill(skill, skillState) {
121
+ if (!isRecord(skill)) {
122
+ return {
123
+ raw: skill
124
+ };
125
+ }
126
+ const slug = pickString(skill.slug)
127
+ ?? pickString(skill.name)
128
+ ?? pickString(skill.id)
129
+ ?? 'unknown';
130
+ const bundled = pickBoolean(skill.bundled) ?? false;
131
+ const source = bundled ? 'bundled' : 'official';
132
+ const enabledFromEntry = skillState.entriesEnabled.get(slug);
133
+ const enabled = enabledFromEntry ?? (bundled
134
+ ? resolveBundledEnabled(slug, skill, skillState.allowBundled)
135
+ : (pickBoolean(skill.enabled)
136
+ ?? pickBoolean(skill.active)
137
+ ?? invertBoolean(pickBoolean(skill.disabled))
138
+ ?? skillState.enabledCustomSkills.has(slug)));
139
+ return {
140
+ ...skill,
141
+ slug,
142
+ name: pickString(skill.name) ?? slug,
143
+ installed: true,
144
+ enabled,
145
+ bundled,
146
+ custom: false,
147
+ source,
148
+ actions: {
149
+ canToggle: true,
150
+ canUninstall: !bundled,
151
+ canAttach: true
152
+ }
153
+ };
154
+ }
155
+ async function listCustomInstalledSkills(context, enabledSkills) {
156
+ const roots = [
157
+ { scope: 'global', dir: path.join(context.openclawRoot, 'skills') },
158
+ { scope: 'workspace', dir: path.join(context.openclawRoot, 'workspace', '.openclaw', 'skills') }
159
+ ];
160
+ const items = [];
161
+ for (const root of roots) {
162
+ if (!(await pathExists(root.dir))) {
163
+ continue;
164
+ }
165
+ const entries = await fs.readdir(root.dir, { withFileTypes: true });
166
+ for (const entry of entries) {
167
+ if (!entry.isDirectory()) {
168
+ continue;
169
+ }
170
+ const installPath = path.join(root.dir, entry.name);
171
+ const manifest = await readSkillManifest(installPath);
172
+ const installMeta = await readInstallMeta(installPath);
173
+ const installPackage = pickString(installMeta.package);
174
+ const fallbackBase = installPackage && installPackage.trim().length > 0 ? installPackage.trim() : entry.name;
175
+ const fallbackSegment = extractSkillSegment(fallbackBase);
176
+ const slug = pickString(manifest.slug) ?? sanitizeDirName(fallbackSegment);
177
+ const displayName = pickString(manifest.name) ?? slug;
178
+ const aliases = buildCustomSkillAliases(slug, displayName, entry.name, installPackage);
179
+ items.push({
180
+ slug,
181
+ name: displayName,
182
+ description: pickString(manifest.description) ?? '',
183
+ version: pickString(manifest.version) ?? null,
184
+ source: 'custom-npm',
185
+ bundled: false,
186
+ custom: true,
187
+ installed: true,
188
+ enabled: enabledSkills.has(slug),
189
+ eligible: true,
190
+ scope: root.scope,
191
+ installPath,
192
+ package: pickString(installMeta.package) ?? null,
193
+ aliases,
194
+ actions: {
195
+ canToggle: true,
196
+ canUninstall: true,
197
+ canAttach: true
198
+ }
199
+ });
200
+ }
201
+ }
202
+ return items;
203
+ }
204
+ async function readSkillManifest(skillDir) {
205
+ const skillJsonPath = path.join(skillDir, 'skill.json');
206
+ const packageJsonPath = path.join(skillDir, 'package.json');
207
+ if (await pathExists(skillJsonPath)) {
208
+ return await readJsonFile(skillJsonPath);
209
+ }
210
+ if (await pathExists(packageJsonPath)) {
211
+ return await readJsonFile(packageJsonPath);
212
+ }
213
+ return {};
214
+ }
215
+ async function readInstallMeta(skillDir) {
216
+ const installMetaPath = path.join(skillDir, '.openclaw-admin-bridge-install.json');
217
+ if (!(await pathExists(installMetaPath))) {
218
+ return {};
219
+ }
220
+ return await readJsonFile(installMetaPath);
221
+ }
222
+ function mergeSkillSources(officialSkills, customSkills) {
223
+ const merged = new Map();
224
+ for (const skill of officialSkills) {
225
+ const slug = typeof skill.slug === 'string' ? skill.slug : null;
226
+ if (slug) {
227
+ merged.set(slug, skill);
228
+ }
229
+ }
230
+ for (const custom of customSkills) {
231
+ const existing = merged.get(custom.slug);
232
+ if (existing && isRecord(existing)) {
233
+ merged.set(custom.slug, {
234
+ ...existing,
235
+ custom: true,
236
+ customInstallPath: custom.installPath,
237
+ customPackage: custom.package,
238
+ customAliases: custom.aliases,
239
+ actions: {
240
+ canToggle: true,
241
+ canUninstall: true,
242
+ canAttach: true
243
+ }
244
+ });
245
+ continue;
246
+ }
247
+ merged.set(custom.slug, custom);
248
+ }
249
+ return Array.from(merged.values()).sort((a, b) => {
250
+ const aSlug = isRecord(a) && typeof a.slug === 'string' ? a.slug : '';
251
+ const bSlug = isRecord(b) && typeof b.slug === 'string' ? b.slug : '';
252
+ return aSlug.localeCompare(bSlug);
253
+ });
254
+ }
255
+ async function getSkillState(context) {
256
+ const configPath = path.join(context.openclawRoot, 'openclaw.json');
257
+ if (!(await pathExists(configPath))) {
258
+ return {
259
+ enabledCustomSkills: new Set(),
260
+ allowBundled: null,
261
+ entriesEnabled: new Map()
262
+ };
263
+ }
264
+ const config = await readJsonFile(configPath);
265
+ const skills = config.agents?.defaults?.skills;
266
+ const enabledCustomSkills = new Set(Array.isArray(skills)
267
+ ? skills.filter((item) => typeof item === 'string')
268
+ : []);
269
+ const allowBundled = Array.isArray(config.skills?.allowBundled)
270
+ ? new Set(config.skills?.allowBundled.filter((item) => typeof item === 'string'))
271
+ : null;
272
+ const entriesEnabled = new Map();
273
+ if (config.skills?.entries && typeof config.skills.entries === 'object') {
274
+ for (const [key, value] of Object.entries(config.skills.entries)) {
275
+ if (value && typeof value === 'object' && typeof value.enabled === 'boolean') {
276
+ entriesEnabled.set(key, value.enabled);
277
+ }
278
+ }
279
+ }
280
+ return {
281
+ enabledCustomSkills,
282
+ allowBundled,
283
+ entriesEnabled
284
+ };
285
+ }
286
+ function isRecord(value) {
287
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
288
+ }
289
+ function pickString(value) {
290
+ return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
291
+ }
292
+ function pickBoolean(value) {
293
+ return typeof value === 'boolean' ? value : undefined;
294
+ }
295
+ function invertBoolean(value) {
296
+ return value === undefined ? undefined : !value;
297
+ }
298
+ function resolveBundledEnabled(slug, skill, allowBundled) {
299
+ if (allowBundled === null) {
300
+ return (pickBoolean(skill.enabled)
301
+ ?? pickBoolean(skill.active)
302
+ ?? invertBoolean(pickBoolean(skill.disabled))
303
+ ?? true);
304
+ }
305
+ return allowBundled.has(slug);
306
+ }
307
+ async function extractTarball(tarballPath, extractRoot) {
308
+ await execFileAsync('tar', ['-xzf', tarballPath, '-C', extractRoot]);
309
+ }
310
+ async function resolvePackedPackageRoot(extractRoot) {
311
+ const packageRoot = path.join(extractRoot, 'package');
312
+ if (await pathExists(packageRoot)) {
313
+ return packageRoot;
314
+ }
315
+ throw new JsonRpcException(JSON_RPC_ERRORS.internalError, 'Packed npm artifact did not contain package/ root', {
316
+ extractRoot
317
+ });
318
+ }
319
+ async function assertSkillPackage(packageRoot) {
320
+ if (!(await pathExists(path.join(packageRoot, 'SKILL.md')))) {
321
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'npm package is not a valid skill package: missing SKILL.md');
322
+ }
323
+ }
324
+ async function resolveSkillIdentity(packageRoot, packageSpec) {
325
+ const skillJsonPath = path.join(packageRoot, 'skill.json');
326
+ let manifest = null;
327
+ if (await pathExists(skillJsonPath)) {
328
+ manifest = await readJsonFile(skillJsonPath);
329
+ }
330
+ const fallbackBase = packageSpec && packageSpec.trim().length > 0 ? packageSpec.trim() : packageSpec;
331
+ const fallbackSegment = extractSkillSegment(fallbackBase);
332
+ const slug = pickString(manifest?.slug) ?? sanitizeDirName(fallbackSegment);
333
+ const displayName = pickString(manifest?.name) ?? slug;
334
+ const dirName = sanitizeDirName(slug);
335
+ return { slug, displayName, dirName };
336
+ }
337
+ /**
338
+ * 从包名中提取用于推断 slug 的片段:
339
+ * - scoped 包(@foo/bar)→ 取 scope 部分 foo(与 OpenClaw CLI 行为一致)
340
+ * - 普通带斜杠包(foo/bar)→ 取最后段 bar
341
+ * - 无斜杠(foo)→ 原样返回
342
+ */
343
+ function extractSkillSegment(packageName) {
344
+ if (packageName.startsWith('@') && packageName.includes('/')) {
345
+ // @foo/bar → foo
346
+ return packageName.split('/')[0].replace(/^@/, '');
347
+ }
348
+ if (packageName.includes('/')) {
349
+ // foo/bar → bar
350
+ return packageName.split('/').at(-1) ?? packageName;
351
+ }
352
+ return packageName;
353
+ }
354
+ function sanitizeDirName(value) {
355
+ return value
356
+ .replace(/^@/, '')
357
+ .replace(/[\\/]/g, '-')
358
+ .replace(/[^a-zA-Z0-9._-]+/g, '-')
359
+ .replace(/-+/g, '-')
360
+ .replace(/^-|-$/g, '')
361
+ .toLowerCase();
362
+ }
363
+ function buildCustomSkillAliases(slug, displayName, dirName, packageName) {
364
+ const values = new Set();
365
+ const add = (value) => {
366
+ if (!value)
367
+ return;
368
+ const trimmed = value.trim();
369
+ if (!trimmed)
370
+ return;
371
+ values.add(trimmed);
372
+ };
373
+ add(slug);
374
+ add(displayName);
375
+ add(dirName);
376
+ add(sanitizeDirName(dirName));
377
+ add(packageName);
378
+ if (packageName) {
379
+ const lastSegment = packageName.includes('/') ? (packageName.split('/').at(-1) ?? packageName) : packageName;
380
+ add(lastSegment);
381
+ add(sanitizeDirName(lastSegment));
382
+ }
383
+ return Array.from(values);
384
+ }