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.
- package/MQTT-API.md +967 -0
- package/dist/index.js +430 -0
- package/dist/message-handler.js +327 -0
- package/dist/src/admin/cli.js +43 -0
- package/dist/src/admin/jsonrpc.js +60 -0
- package/dist/src/admin/lib/fs.js +30 -0
- package/dist/src/admin/lib/paths.js +46 -0
- package/dist/src/admin/methods/admin.js +60 -0
- package/dist/src/admin/methods/agents-extended.js +235 -0
- package/dist/src/admin/methods/index.js +69 -0
- package/dist/src/admin/methods/memory.js +360 -0
- package/dist/src/admin/methods/models-extended.js +107 -0
- package/dist/src/admin/methods/models.js +39 -0
- package/dist/src/admin/methods/sessions-extended.js +207 -0
- package/dist/src/admin/methods/sessions.js +64 -0
- package/dist/src/admin/methods/skills-extended.js +157 -0
- package/dist/src/admin/methods/skills-toggle.js +182 -0
- package/dist/src/admin/methods/skills.js +384 -0
- package/dist/src/admin/methods/system.js +178 -0
- package/dist/src/admin/methods/usage.js +1170 -0
- package/dist/src/admin/types.js +1 -0
- package/dist/src/mqtt/connection-manager.js +155 -0
- package/dist/src/mqtt/index.js +5 -0
- package/dist/src/mqtt/mqtt-client.js +86 -0
- package/dist/src/mqtt/types.js +2 -0
- package/dist/src/shared/context.js +24 -0
- package/dist/src/shared/wrapper.js +23 -0
- package/index.ts +514 -0
- package/message-handler.ts +415 -0
- package/openclaw.plugin.json +84 -0
- package/package.json +35 -0
- package/readme.md +32 -0
- package/src/admin/cli.ts +60 -0
- package/src/admin/jsonrpc.ts +88 -0
- package/src/admin/lib/fs.ts +35 -0
- package/src/admin/lib/paths.ts +61 -0
- package/src/admin/methods/admin.ts +95 -0
- package/src/admin/methods/agents-extended.ts +310 -0
- package/src/admin/methods/index.ts +103 -0
- package/src/admin/methods/memory.ts +546 -0
- package/src/admin/methods/models-extended.ts +191 -0
- package/src/admin/methods/models.ts +103 -0
- package/src/admin/methods/sessions-extended.ts +313 -0
- package/src/admin/methods/sessions.ts +122 -0
- package/src/admin/methods/skills-extended.ts +249 -0
- package/src/admin/methods/skills-toggle.ts +235 -0
- package/src/admin/methods/skills.ts +651 -0
- package/src/admin/methods/system.ts +203 -0
- package/src/admin/methods/usage.ts +1491 -0
- package/src/admin/types.ts +46 -0
- package/src/mqtt/connection-manager.ts +188 -0
- package/src/mqtt/index.ts +6 -0
- package/src/mqtt/mqtt-client.ts +119 -0
- package/src/mqtt/types.ts +36 -0
- package/src/shared/context.ts +33 -0
- package/src/shared/wrapper.ts +35 -0
- package/tsconfig.json +16 -0
- package/types/openclaw.d.ts +74 -0
|
@@ -0,0 +1,651 @@
|
|
|
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
|
+
|
|
7
|
+
import { ensureDir, pathExists, readJsonFile } from '../lib/fs.ts';
|
|
8
|
+
import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.ts';
|
|
9
|
+
import type { JsonValue, MethodContext, MethodHandler } from '../types.ts';
|
|
10
|
+
|
|
11
|
+
const execFileAsync = promisify(execFile);
|
|
12
|
+
|
|
13
|
+
interface SkillManifest {
|
|
14
|
+
slug?: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
version?: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
author?: string;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
[key: string]: JsonValue | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface InstallSkillParams {
|
|
24
|
+
package: string;
|
|
25
|
+
scope?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ClawHubSearchParams {
|
|
29
|
+
query?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ClawHubSkillParams {
|
|
33
|
+
slug: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ResolvedSkillIdentity {
|
|
37
|
+
slug: string;
|
|
38
|
+
displayName: string;
|
|
39
|
+
dirName: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface OpenClawConfig {
|
|
43
|
+
agents?: {
|
|
44
|
+
defaults?: {
|
|
45
|
+
skills?: string[];
|
|
46
|
+
[key: string]: JsonValue | undefined;
|
|
47
|
+
};
|
|
48
|
+
[key: string]: JsonValue | undefined;
|
|
49
|
+
};
|
|
50
|
+
skills?: {
|
|
51
|
+
allowBundled?: string[];
|
|
52
|
+
entries?: Record<string, { enabled?: boolean; [key: string]: JsonValue | undefined }>;
|
|
53
|
+
[key: string]: JsonValue | undefined;
|
|
54
|
+
};
|
|
55
|
+
[key: string]: JsonValue | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface CustomInstalledSkill {
|
|
59
|
+
slug: string;
|
|
60
|
+
name: string;
|
|
61
|
+
description: string;
|
|
62
|
+
version: string | null;
|
|
63
|
+
source: 'custom-npm';
|
|
64
|
+
bundled: false;
|
|
65
|
+
custom: true;
|
|
66
|
+
installed: true;
|
|
67
|
+
enabled: boolean;
|
|
68
|
+
eligible: boolean;
|
|
69
|
+
scope: 'global' | 'workspace';
|
|
70
|
+
installPath: string;
|
|
71
|
+
package: string | null;
|
|
72
|
+
aliases: string[];
|
|
73
|
+
actions: {
|
|
74
|
+
canToggle: true;
|
|
75
|
+
canUninstall: true;
|
|
76
|
+
canAttach: true;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const listInstalledSkills: MethodHandler = async (_params, context): Promise<JsonValue> => {
|
|
81
|
+
const items = await getInstalledSkillsFromCli(context);
|
|
82
|
+
return {
|
|
83
|
+
count: items.length,
|
|
84
|
+
items
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const installSkillFromNpm: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
89
|
+
const objectParams = expectObject(params) as unknown as InstallSkillParams;
|
|
90
|
+
const packageSpec = expectString(objectParams.package, 'package');
|
|
91
|
+
const scope = normalizeScope(objectParams.scope);
|
|
92
|
+
const installRoot = resolveInstallRoot(context.openclawRoot, scope);
|
|
93
|
+
|
|
94
|
+
await ensureDir(installRoot);
|
|
95
|
+
|
|
96
|
+
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'openclaw-skill-install-'));
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const tarballName = await npmPack(packageSpec, tempRoot);
|
|
100
|
+
const tarballPath = path.join(tempRoot, tarballName);
|
|
101
|
+
const extractRoot = path.join(tempRoot, 'extract');
|
|
102
|
+
await ensureDir(extractRoot);
|
|
103
|
+
await extractTarball(tarballPath, extractRoot);
|
|
104
|
+
|
|
105
|
+
const packageRoot = await resolvePackedPackageRoot(extractRoot);
|
|
106
|
+
await assertSkillPackage(packageRoot);
|
|
107
|
+
|
|
108
|
+
const skillInfo = await resolveSkillIdentity(packageRoot, packageSpec);
|
|
109
|
+
const targetDir = path.join(installRoot, skillInfo.dirName);
|
|
110
|
+
|
|
111
|
+
await fs.rm(targetDir, { recursive: true, force: true });
|
|
112
|
+
await fs.cp(packageRoot, targetDir, { recursive: true });
|
|
113
|
+
|
|
114
|
+
await fs.writeFile(
|
|
115
|
+
path.join(targetDir, '.openclaw-admin-bridge-install.json'),
|
|
116
|
+
JSON.stringify(
|
|
117
|
+
{
|
|
118
|
+
source: 'npm',
|
|
119
|
+
package: packageSpec,
|
|
120
|
+
scope,
|
|
121
|
+
installedAt: new Date().toISOString()
|
|
122
|
+
},
|
|
123
|
+
null,
|
|
124
|
+
2
|
|
125
|
+
),
|
|
126
|
+
'utf8'
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
ok: true,
|
|
131
|
+
scope,
|
|
132
|
+
package: packageSpec,
|
|
133
|
+
slug: skillInfo.slug,
|
|
134
|
+
skillName: skillInfo.displayName,
|
|
135
|
+
installPath: targetDir
|
|
136
|
+
};
|
|
137
|
+
} finally {
|
|
138
|
+
await fs.rm(tempRoot, { recursive: true, force: true });
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const searchClawHubSkills: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
143
|
+
const objectParams = isObject(params) ? params : {};
|
|
144
|
+
const query = typeof objectParams.query === 'string' ? objectParams.query.trim() : '';
|
|
145
|
+
const args = query ? ['skills', 'search', query, '--json'] : ['skills', 'search', '--json'];
|
|
146
|
+
const result = await runOpenClawSkillCommand(args, context.projectRoot);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
ok: true,
|
|
150
|
+
query,
|
|
151
|
+
...result
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const installSkillFromClawHub: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
156
|
+
const objectParams = expectObject(params) as unknown as ClawHubSkillParams;
|
|
157
|
+
const slug = expectString(objectParams.slug, 'slug');
|
|
158
|
+
const result = await runOpenClawSkillCommand(['skills', 'install', slug], context.projectRoot);
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
ok: true,
|
|
162
|
+
slug,
|
|
163
|
+
...result
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export const updateSkillFromClawHub: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
168
|
+
const objectParams = expectObject(params) as unknown as ClawHubSkillParams;
|
|
169
|
+
const slug = expectString(objectParams.slug, 'slug');
|
|
170
|
+
const result = await runOpenClawSkillCommand(['skills', 'update', slug], context.projectRoot);
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
ok: true,
|
|
174
|
+
slug,
|
|
175
|
+
...result
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export async function getInstalledSkillsFromCli(context: MethodContext): Promise<JsonValue[]> {
|
|
180
|
+
const skillState = await getSkillState(context);
|
|
181
|
+
const cliItems = await queryOpenClawSkills(context);
|
|
182
|
+
const officialSkills = cliItems.map((item) => normalizeCliSkill(item, skillState));
|
|
183
|
+
const customSkills = await listCustomInstalledSkills(context, skillState.enabledCustomSkills);
|
|
184
|
+
return mergeSkillSources(officialSkills, customSkills);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function expectObject(value: JsonValue | undefined): Record<string, JsonValue> {
|
|
188
|
+
if (!value || Array.isArray(value) || typeof value !== 'object') {
|
|
189
|
+
throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Params must be an object');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return value as Record<string, JsonValue>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function expectString(value: JsonValue | undefined, fieldName: string): string {
|
|
196
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
197
|
+
throw new JsonRpcException(
|
|
198
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
199
|
+
`Field '${fieldName}' must be a non-empty string`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return value.trim();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function normalizeScope(rawScope: string | undefined): 'global' | 'workspace' {
|
|
207
|
+
return rawScope === 'workspace' ? 'workspace' : 'global';
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function resolveInstallRoot(openclawRoot: string, scope: 'global' | 'workspace'): string {
|
|
211
|
+
return scope === 'workspace'
|
|
212
|
+
? path.join(openclawRoot, 'workspace', '.openclaw', 'skills')
|
|
213
|
+
: path.join(openclawRoot, 'skills');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function npmPack(packageSpec: string, cwd: string): Promise<string> {
|
|
217
|
+
const { stdout } = await execFileAsync('npm', ['pack', packageSpec], { cwd });
|
|
218
|
+
const tarballName = stdout
|
|
219
|
+
.split(/\r?\n/)
|
|
220
|
+
.map((line) => line.trim())
|
|
221
|
+
.filter(Boolean)
|
|
222
|
+
.at(-1);
|
|
223
|
+
|
|
224
|
+
if (!tarballName || !tarballName.endsWith('.tgz')) {
|
|
225
|
+
throw new JsonRpcException(JSON_RPC_ERRORS.internalError, 'npm pack did not return a tarball name', {
|
|
226
|
+
package: packageSpec,
|
|
227
|
+
stdout
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return tarballName;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function runOpenClawSkillCommand(
|
|
235
|
+
args: string[],
|
|
236
|
+
cwd: string
|
|
237
|
+
): Promise<{ stdout: string; stderr: string; parsed: JsonValue | null }> {
|
|
238
|
+
const command = process.env.OPENCLAW_BIN || 'openclaw';
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
const { stdout, stderr } = await execFileAsync(command, args, { cwd });
|
|
242
|
+
return {
|
|
243
|
+
stdout,
|
|
244
|
+
stderr,
|
|
245
|
+
parsed: parseJsonOutput(stdout)
|
|
246
|
+
};
|
|
247
|
+
} catch (err: any) {
|
|
248
|
+
throw new JsonRpcException(
|
|
249
|
+
JSON_RPC_ERRORS.internalError,
|
|
250
|
+
`OpenClaw skill command failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
251
|
+
{
|
|
252
|
+
command,
|
|
253
|
+
args,
|
|
254
|
+
stdout: typeof err?.stdout === 'string' ? err.stdout : '',
|
|
255
|
+
stderr: typeof err?.stderr === 'string' ? err.stderr : ''
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function parseJsonOutput(stdout: string): JsonValue | null {
|
|
262
|
+
const trimmed = stdout.trim();
|
|
263
|
+
if (!trimmed) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
return JSON.parse(trimmed) as JsonValue;
|
|
269
|
+
} catch {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function queryOpenClawSkills(context: MethodContext): Promise<unknown[]> {
|
|
275
|
+
const command = process.env.OPENCLAW_BIN || 'openclaw';
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
const { stdout } = await execFileAsync(command, ['skills', 'list', '--json'], {
|
|
279
|
+
cwd: context.projectRoot
|
|
280
|
+
});
|
|
281
|
+
const parsed = JSON.parse(stdout) as unknown;
|
|
282
|
+
|
|
283
|
+
if (Array.isArray(parsed)) {
|
|
284
|
+
return parsed;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (isRecord(parsed)) {
|
|
288
|
+
const items = parsed.items;
|
|
289
|
+
const skills = parsed.skills;
|
|
290
|
+
if (Array.isArray(items)) return items;
|
|
291
|
+
if (Array.isArray(skills)) return skills;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
throw new Error('Unexpected JSON shape returned by OpenClaw CLI');
|
|
295
|
+
} catch (err) {
|
|
296
|
+
throw new JsonRpcException(
|
|
297
|
+
JSON_RPC_ERRORS.internalError,
|
|
298
|
+
`Failed to query OpenClaw skills list: ${err instanceof Error ? err.message : String(err)}`
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function normalizeCliSkill(
|
|
304
|
+
skill: unknown,
|
|
305
|
+
skillState: {
|
|
306
|
+
enabledCustomSkills: Set<string>;
|
|
307
|
+
allowBundled: Set<string> | null;
|
|
308
|
+
entriesEnabled: Map<string, boolean>;
|
|
309
|
+
}
|
|
310
|
+
): Record<string, JsonValue> {
|
|
311
|
+
if (!isRecord(skill)) {
|
|
312
|
+
return {
|
|
313
|
+
raw: skill
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const slug = pickString(skill.slug)
|
|
318
|
+
?? pickString(skill.name)
|
|
319
|
+
?? pickString(skill.id)
|
|
320
|
+
?? 'unknown';
|
|
321
|
+
|
|
322
|
+
const bundled = pickBoolean(skill.bundled) ?? false;
|
|
323
|
+
const source = bundled ? 'bundled' : 'official';
|
|
324
|
+
const enabledFromEntry = skillState.entriesEnabled.get(slug);
|
|
325
|
+
const enabled = enabledFromEntry ?? (
|
|
326
|
+
bundled
|
|
327
|
+
? resolveBundledEnabled(slug, skill, skillState.allowBundled)
|
|
328
|
+
: (
|
|
329
|
+
pickBoolean(skill.enabled)
|
|
330
|
+
?? pickBoolean(skill.active)
|
|
331
|
+
?? invertBoolean(pickBoolean(skill.disabled))
|
|
332
|
+
?? skillState.enabledCustomSkills.has(slug)
|
|
333
|
+
)
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
return {
|
|
337
|
+
...skill,
|
|
338
|
+
slug,
|
|
339
|
+
name: pickString(skill.name) ?? slug,
|
|
340
|
+
installed: true,
|
|
341
|
+
enabled,
|
|
342
|
+
bundled,
|
|
343
|
+
custom: false,
|
|
344
|
+
source,
|
|
345
|
+
actions: {
|
|
346
|
+
canToggle: true,
|
|
347
|
+
canUninstall: !bundled,
|
|
348
|
+
canAttach: true
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async function listCustomInstalledSkills(
|
|
354
|
+
context: MethodContext,
|
|
355
|
+
enabledSkills: Set<string>
|
|
356
|
+
): Promise<CustomInstalledSkill[]> {
|
|
357
|
+
const roots: Array<{ scope: 'global' | 'workspace'; dir: string }> = [
|
|
358
|
+
{ scope: 'global', dir: path.join(context.openclawRoot, 'skills') },
|
|
359
|
+
{ scope: 'workspace', dir: path.join(context.openclawRoot, 'workspace', '.openclaw', 'skills') }
|
|
360
|
+
];
|
|
361
|
+
const items: CustomInstalledSkill[] = [];
|
|
362
|
+
|
|
363
|
+
for (const root of roots) {
|
|
364
|
+
if (!(await pathExists(root.dir))) {
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const entries = await fs.readdir(root.dir, { withFileTypes: true });
|
|
369
|
+
for (const entry of entries) {
|
|
370
|
+
if (!entry.isDirectory()) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const installPath = path.join(root.dir, entry.name);
|
|
375
|
+
const manifest = await readSkillManifest(installPath);
|
|
376
|
+
const installMeta = await readInstallMeta(installPath);
|
|
377
|
+
const installPackage = pickString(installMeta.package);
|
|
378
|
+
const fallbackBase = installPackage && installPackage.trim().length > 0 ? installPackage.trim() : entry.name;
|
|
379
|
+
const fallbackSegment = extractSkillSegment(fallbackBase);
|
|
380
|
+
const slug = pickString(manifest.slug) ?? sanitizeDirName(fallbackSegment);
|
|
381
|
+
const displayName = pickString(manifest.name) ?? slug;
|
|
382
|
+
const aliases = buildCustomSkillAliases(slug, displayName, entry.name, installPackage);
|
|
383
|
+
|
|
384
|
+
items.push({
|
|
385
|
+
slug,
|
|
386
|
+
name: displayName,
|
|
387
|
+
description: pickString(manifest.description) ?? '',
|
|
388
|
+
version: pickString(manifest.version) ?? null,
|
|
389
|
+
source: 'custom-npm',
|
|
390
|
+
bundled: false,
|
|
391
|
+
custom: true,
|
|
392
|
+
installed: true,
|
|
393
|
+
enabled: enabledSkills.has(slug),
|
|
394
|
+
eligible: true,
|
|
395
|
+
scope: root.scope,
|
|
396
|
+
installPath,
|
|
397
|
+
package: pickString(installMeta.package) ?? null,
|
|
398
|
+
aliases,
|
|
399
|
+
actions: {
|
|
400
|
+
canToggle: true,
|
|
401
|
+
canUninstall: true,
|
|
402
|
+
canAttach: true
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return items;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async function readSkillManifest(skillDir: string): Promise<SkillManifest> {
|
|
412
|
+
const skillJsonPath = path.join(skillDir, 'skill.json');
|
|
413
|
+
const packageJsonPath = path.join(skillDir, 'package.json');
|
|
414
|
+
|
|
415
|
+
if (await pathExists(skillJsonPath)) {
|
|
416
|
+
return await readJsonFile<SkillManifest>(skillJsonPath);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (await pathExists(packageJsonPath)) {
|
|
420
|
+
return await readJsonFile<SkillManifest>(packageJsonPath);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return {};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
async function readInstallMeta(skillDir: string): Promise<Record<string, JsonValue>> {
|
|
427
|
+
const installMetaPath = path.join(skillDir, '.openclaw-admin-bridge-install.json');
|
|
428
|
+
if (!(await pathExists(installMetaPath))) {
|
|
429
|
+
return {};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return await readJsonFile<Record<string, JsonValue>>(installMetaPath);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function mergeSkillSources(
|
|
436
|
+
officialSkills: Record<string, JsonValue>[],
|
|
437
|
+
customSkills: CustomInstalledSkill[]
|
|
438
|
+
): JsonValue[] {
|
|
439
|
+
const merged = new Map<string, JsonValue>();
|
|
440
|
+
|
|
441
|
+
for (const skill of officialSkills) {
|
|
442
|
+
const slug = typeof skill.slug === 'string' ? skill.slug : null;
|
|
443
|
+
if (slug) {
|
|
444
|
+
merged.set(slug, skill);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
for (const custom of customSkills) {
|
|
449
|
+
const existing = merged.get(custom.slug);
|
|
450
|
+
if (existing && isRecord(existing)) {
|
|
451
|
+
merged.set(custom.slug, {
|
|
452
|
+
...existing,
|
|
453
|
+
custom: true,
|
|
454
|
+
customInstallPath: custom.installPath,
|
|
455
|
+
customPackage: custom.package,
|
|
456
|
+
customAliases: custom.aliases,
|
|
457
|
+
actions: {
|
|
458
|
+
canToggle: true,
|
|
459
|
+
canUninstall: true,
|
|
460
|
+
canAttach: true
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
merged.set(custom.slug, custom as unknown as JsonValue);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return Array.from(merged.values()).sort((a, b) => {
|
|
470
|
+
const aSlug = isRecord(a) && typeof a.slug === 'string' ? a.slug : '';
|
|
471
|
+
const bSlug = isRecord(b) && typeof b.slug === 'string' ? b.slug : '';
|
|
472
|
+
return aSlug.localeCompare(bSlug);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async function getSkillState(
|
|
477
|
+
context: MethodContext
|
|
478
|
+
): Promise<{
|
|
479
|
+
enabledCustomSkills: Set<string>;
|
|
480
|
+
allowBundled: Set<string> | null;
|
|
481
|
+
entriesEnabled: Map<string, boolean>;
|
|
482
|
+
}> {
|
|
483
|
+
const configPath = path.join(context.openclawRoot, 'openclaw.json');
|
|
484
|
+
if (!(await pathExists(configPath))) {
|
|
485
|
+
return {
|
|
486
|
+
enabledCustomSkills: new Set<string>(),
|
|
487
|
+
allowBundled: null,
|
|
488
|
+
entriesEnabled: new Map<string, boolean>()
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const config = await readJsonFile<OpenClawConfig>(configPath);
|
|
493
|
+
const skills = config.agents?.defaults?.skills;
|
|
494
|
+
const enabledCustomSkills = new Set(
|
|
495
|
+
Array.isArray(skills)
|
|
496
|
+
? skills.filter((item): item is string => typeof item === 'string')
|
|
497
|
+
: []
|
|
498
|
+
);
|
|
499
|
+
const allowBundled = Array.isArray(config.skills?.allowBundled)
|
|
500
|
+
? new Set(config.skills?.allowBundled.filter((item): item is string => typeof item === 'string'))
|
|
501
|
+
: null;
|
|
502
|
+
const entriesEnabled = new Map<string, boolean>();
|
|
503
|
+
if (config.skills?.entries && typeof config.skills.entries === 'object') {
|
|
504
|
+
for (const [key, value] of Object.entries(config.skills.entries)) {
|
|
505
|
+
if (value && typeof value === 'object' && typeof value.enabled === 'boolean') {
|
|
506
|
+
entriesEnabled.set(key, value.enabled);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return {
|
|
512
|
+
enabledCustomSkills,
|
|
513
|
+
allowBundled,
|
|
514
|
+
entriesEnabled
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function isRecord(value: unknown): value is Record<string, any> {
|
|
519
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function isObject(value: JsonValue | undefined): value is Record<string, JsonValue> {
|
|
523
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function pickString(value: unknown): string | undefined {
|
|
527
|
+
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function pickBoolean(value: unknown): boolean | undefined {
|
|
531
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function invertBoolean(value: boolean | undefined): boolean | undefined {
|
|
535
|
+
return value === undefined ? undefined : !value;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function resolveBundledEnabled(
|
|
539
|
+
slug: string,
|
|
540
|
+
skill: Record<string, any>,
|
|
541
|
+
allowBundled: Set<string> | null
|
|
542
|
+
): boolean {
|
|
543
|
+
if (allowBundled === null) {
|
|
544
|
+
return (
|
|
545
|
+
pickBoolean(skill.enabled)
|
|
546
|
+
?? pickBoolean(skill.active)
|
|
547
|
+
?? invertBoolean(pickBoolean(skill.disabled))
|
|
548
|
+
?? true
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return allowBundled.has(slug);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
async function extractTarball(tarballPath: string, extractRoot: string): Promise<void> {
|
|
556
|
+
await execFileAsync('tar', ['-xzf', tarballPath, '-C', extractRoot]);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
async function resolvePackedPackageRoot(extractRoot: string): Promise<string> {
|
|
560
|
+
const packageRoot = path.join(extractRoot, 'package');
|
|
561
|
+
if (await pathExists(packageRoot)) {
|
|
562
|
+
return packageRoot;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
throw new JsonRpcException(JSON_RPC_ERRORS.internalError, 'Packed npm artifact did not contain package/ root', {
|
|
566
|
+
extractRoot
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async function assertSkillPackage(packageRoot: string): Promise<void> {
|
|
571
|
+
if (!(await pathExists(path.join(packageRoot, 'SKILL.md')))) {
|
|
572
|
+
throw new JsonRpcException(
|
|
573
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
574
|
+
'npm package is not a valid skill package: missing SKILL.md'
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
async function resolveSkillIdentity(packageRoot: string, packageSpec: string): Promise<ResolvedSkillIdentity> {
|
|
580
|
+
const skillJsonPath = path.join(packageRoot, 'skill.json');
|
|
581
|
+
let manifest: SkillManifest | null = null;
|
|
582
|
+
|
|
583
|
+
if (await pathExists(skillJsonPath)) {
|
|
584
|
+
manifest = await readJsonFile<SkillManifest>(skillJsonPath);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const fallbackBase = packageSpec && packageSpec.trim().length > 0 ? packageSpec.trim() : packageSpec;
|
|
588
|
+
const fallbackSegment = extractSkillSegment(fallbackBase);
|
|
589
|
+
const slug = pickString(manifest?.slug) ?? sanitizeDirName(fallbackSegment);
|
|
590
|
+
const displayName = pickString(manifest?.name) ?? slug;
|
|
591
|
+
const dirName = sanitizeDirName(slug);
|
|
592
|
+
|
|
593
|
+
return { slug, displayName, dirName };
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* 从包名中提取用于推断 slug 的片段:
|
|
598
|
+
* - scoped 包(@foo/bar)→ 取 scope 部分 foo(与 OpenClaw CLI 行为一致)
|
|
599
|
+
* - 普通带斜杠包(foo/bar)→ 取最后段 bar
|
|
600
|
+
* - 无斜杠(foo)→ 原样返回
|
|
601
|
+
*/
|
|
602
|
+
function extractSkillSegment(packageName: string): string {
|
|
603
|
+
if (packageName.startsWith('@') && packageName.includes('/')) {
|
|
604
|
+
// @foo/bar → foo
|
|
605
|
+
return packageName.split('/')[0].replace(/^@/, '');
|
|
606
|
+
}
|
|
607
|
+
if (packageName.includes('/')) {
|
|
608
|
+
// foo/bar → bar
|
|
609
|
+
return packageName.split('/').at(-1) ?? packageName;
|
|
610
|
+
}
|
|
611
|
+
return packageName;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function sanitizeDirName(value: string): string {
|
|
615
|
+
return value
|
|
616
|
+
.replace(/^@/, '')
|
|
617
|
+
.replace(/[\\/]/g, '-')
|
|
618
|
+
.replace(/[^a-zA-Z0-9._-]+/g, '-')
|
|
619
|
+
.replace(/-+/g, '-')
|
|
620
|
+
.replace(/^-|-$/g, '')
|
|
621
|
+
.toLowerCase();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function buildCustomSkillAliases(
|
|
625
|
+
slug: string,
|
|
626
|
+
displayName: string,
|
|
627
|
+
dirName: string,
|
|
628
|
+
packageName: string | undefined
|
|
629
|
+
): string[] {
|
|
630
|
+
const values = new Set<string>();
|
|
631
|
+
const add = (value: string | undefined) => {
|
|
632
|
+
if (!value) return;
|
|
633
|
+
const trimmed = value.trim();
|
|
634
|
+
if (!trimmed) return;
|
|
635
|
+
values.add(trimmed);
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
add(slug);
|
|
639
|
+
add(displayName);
|
|
640
|
+
add(dirName);
|
|
641
|
+
add(sanitizeDirName(dirName));
|
|
642
|
+
add(packageName);
|
|
643
|
+
|
|
644
|
+
if (packageName) {
|
|
645
|
+
const lastSegment = packageName.includes('/') ? (packageName.split('/').at(-1) ?? packageName) : packageName;
|
|
646
|
+
add(lastSegment);
|
|
647
|
+
add(sanitizeDirName(lastSegment));
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
return Array.from(values);
|
|
651
|
+
}
|