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,327 @@
1
+ /**
2
+ * 消息处理器类
3
+ * 根据消息类型调用对应的 admin 方法
4
+ */
5
+ import { getContext } from './src/shared/context.js';
6
+ import { wrapAdminCall } from './src/shared/wrapper.js';
7
+ import { getAgents, getConfig } from './src/admin/methods/admin.js';
8
+ import { createAgent, deleteAgent, listAgents, updateAgent } from './src/admin/methods/agents-extended.js';
9
+ import { listSessions } from './src/admin/methods/sessions.js';
10
+ import { getSession, prepareMessage, attachSkill } from './src/admin/methods/sessions-extended.js';
11
+ import { getModels } from './src/admin/methods/models.js';
12
+ import { updateModels } from './src/admin/methods/models-extended.js';
13
+ import { getUsagePageSummary, getUsageTimeseries, getUsageBreakdown, getUsageSummary, } from './src/admin/methods/usage.js';
14
+ import { listInstalledSkills, installSkillFromNpm } from './src/admin/methods/skills.js';
15
+ import { getInstalledSkill, uninstallSkill } from './src/admin/methods/skills-extended.js';
16
+ import { toggleSkill } from './src/admin/methods/skills-toggle.js';
17
+ import { listMemoryFiles, getMemoryFile, backupMemory, exportMemoryZip, getMemoryPresignedPost, createMemoryBackupRecord, importMemoryZip, } from './src/admin/methods/memory.js';
18
+ import { restart, stop, doctorFix, logs } from './src/admin/methods/system.js';
19
+ export class MessageHandler {
20
+ /**
21
+ * 示例方法:处理 ping 类型的消息
22
+ * @param data - 消息数据
23
+ * @returns 处理结果
24
+ */
25
+ async ping(data) {
26
+ return {
27
+ message: "pong",
28
+ timestamp: Date.now(),
29
+ received: data,
30
+ };
31
+ }
32
+ /**
33
+ * 获取 agents 配置
34
+ */
35
+ async agentsGet(data) {
36
+ return wrapAdminCall(async () => {
37
+ const context = getContext();
38
+ return await getAgents(data, context);
39
+ });
40
+ }
41
+ async agentsList(data) {
42
+ return wrapAdminCall(async () => {
43
+ const context = getContext();
44
+ return await listAgents(data, context);
45
+ });
46
+ }
47
+ async agentsCreate(data) {
48
+ return wrapAdminCall(async () => {
49
+ const context = getContext();
50
+ return await createAgent(data, context);
51
+ });
52
+ }
53
+ async agentsDelete(data) {
54
+ return wrapAdminCall(async () => {
55
+ const context = getContext();
56
+ return await deleteAgent(data, context);
57
+ });
58
+ }
59
+ /**
60
+ * 更新 agent 配置
61
+ */
62
+ async agentsUpdate(data) {
63
+ return wrapAdminCall(async () => {
64
+ const context = getContext();
65
+ return await updateAgent(data, context);
66
+ });
67
+ }
68
+ /**
69
+ * 获取配置
70
+ */
71
+ async configGet(data) {
72
+ return wrapAdminCall(async () => {
73
+ const context = getContext();
74
+ return await getConfig(data, context);
75
+ });
76
+ }
77
+ /**
78
+ * 列出 sessions
79
+ */
80
+ async sessionsList(data) {
81
+ return wrapAdminCall(async () => {
82
+ const context = getContext();
83
+ return await listSessions(data, context);
84
+ });
85
+ }
86
+ /**
87
+ * 获取单个 session 详情和消息记录
88
+ */
89
+ async sessionsGet(data) {
90
+ return wrapAdminCall(async () => {
91
+ const context = getContext();
92
+ return await getSession(data, context);
93
+ });
94
+ }
95
+ /**
96
+ * 准备向 session 发送的消息
97
+ * 注意:这只是准备消息数据,实际发送需要通过 MQTT sender 消息
98
+ */
99
+ async sessionsPrepareMessage(data) {
100
+ return wrapAdminCall(async () => {
101
+ const context = getContext();
102
+ return await prepareMessage(data, context);
103
+ });
104
+ }
105
+ /**
106
+ * 附加 skill 到消息
107
+ */
108
+ async sessionsAttachSkill(data) {
109
+ return wrapAdminCall(async () => {
110
+ const context = getContext();
111
+ return await attachSkill(data, context);
112
+ });
113
+ }
114
+ /**
115
+ * 获取 models 配置
116
+ */
117
+ async modelsGet(data) {
118
+ return wrapAdminCall(async () => {
119
+ const context = getContext();
120
+ return await getModels(data, context);
121
+ });
122
+ }
123
+ /**
124
+ * 更新 models 配置
125
+ */
126
+ async modelsUpdate(data) {
127
+ return wrapAdminCall(async () => {
128
+ const context = getContext();
129
+ return await updateModels(data, context);
130
+ });
131
+ }
132
+ /**
133
+ * 获取 usage summary
134
+ */
135
+ async usageSummary(data) {
136
+ return wrapAdminCall(async () => {
137
+ const context = getContext();
138
+ return await getUsageSummary(data, context);
139
+ });
140
+ }
141
+ /**
142
+ * 获取 usage page summary
143
+ */
144
+ async usagePageSummary(data) {
145
+ return wrapAdminCall(async () => {
146
+ const context = getContext();
147
+ return await getUsagePageSummary(data, context);
148
+ });
149
+ }
150
+ /**
151
+ * 获取 usage timeseries
152
+ */
153
+ async usageTimeseries(data) {
154
+ return wrapAdminCall(async () => {
155
+ const context = getContext();
156
+ return await getUsageTimeseries(data, context);
157
+ });
158
+ }
159
+ /**
160
+ * 获取 usage breakdown
161
+ */
162
+ async usageBreakdown(data) {
163
+ return wrapAdminCall(async () => {
164
+ const context = getContext();
165
+ return await getUsageBreakdown(data, context);
166
+ });
167
+ }
168
+ /**
169
+ * 列出已安装的 skills
170
+ */
171
+ async skillsListInstalled(data) {
172
+ return wrapAdminCall(async () => {
173
+ const context = getContext();
174
+ return await listInstalledSkills(data, context);
175
+ });
176
+ }
177
+ /**
178
+ * 从 npm 安装 skill
179
+ */
180
+ async skillsInstallFromNpm(data) {
181
+ return wrapAdminCall(async () => {
182
+ const context = getContext();
183
+ return await installSkillFromNpm(data, context);
184
+ });
185
+ }
186
+ /**
187
+ * 获取已安装 skill 详情
188
+ */
189
+ async skillsGetInstalled(data) {
190
+ return wrapAdminCall(async () => {
191
+ const context = getContext();
192
+ return await getInstalledSkill(data, context);
193
+ });
194
+ }
195
+ /**
196
+ * 卸载 skill
197
+ */
198
+ async skillsUninstall(data) {
199
+ return wrapAdminCall(async () => {
200
+ const context = getContext();
201
+ return await uninstallSkill(data, context);
202
+ });
203
+ }
204
+ /**
205
+ * 启用或停用 skill
206
+ */
207
+ async skillsToggle(data) {
208
+ return wrapAdminCall(async () => {
209
+ const context = getContext();
210
+ return await toggleSkill(data, context);
211
+ });
212
+ }
213
+ /**
214
+ * 列出 memory 文件
215
+ */
216
+ async memoryListFiles(data) {
217
+ return wrapAdminCall(async () => {
218
+ const context = getContext();
219
+ return await listMemoryFiles(data, context);
220
+ });
221
+ }
222
+ /**
223
+ * 获取 memory 文件内容
224
+ */
225
+ async memoryGetFile(data) {
226
+ return wrapAdminCall(async () => {
227
+ const context = getContext();
228
+ return await getMemoryFile(data, context);
229
+ });
230
+ }
231
+ /**
232
+ * 备份 memory
233
+ */
234
+ async memoryBackup(data) {
235
+ return wrapAdminCall(async () => {
236
+ const context = getContext();
237
+ return await backupMemory(data, context);
238
+ });
239
+ }
240
+ /**
241
+ * 导出 memory zip
242
+ */
243
+ async memoryExportZip(data) {
244
+ return wrapAdminCall(async () => {
245
+ const context = getContext();
246
+ return await exportMemoryZip(data, context);
247
+ });
248
+ }
249
+ async memoryGetPresignedPost(data) {
250
+ return wrapAdminCall(async () => {
251
+ const context = getContext();
252
+ return await getMemoryPresignedPost(data, context);
253
+ });
254
+ }
255
+ async memoryCreateBackupRecord(data) {
256
+ return wrapAdminCall(async () => {
257
+ const context = getContext();
258
+ return await createMemoryBackupRecord(data, context);
259
+ });
260
+ }
261
+ /**
262
+ * 导入 memory zip
263
+ */
264
+ async memoryImportZip(data) {
265
+ return wrapAdminCall(async () => {
266
+ const context = getContext();
267
+ return await importMemoryZip(data, context);
268
+ });
269
+ }
270
+ /**
271
+ * 重启 OpenClaw Gateway
272
+ */
273
+ async systemRestart(data) {
274
+ return wrapAdminCall(async () => {
275
+ const context = getContext();
276
+ return await restart(data, context);
277
+ });
278
+ }
279
+ /**
280
+ * 停止 OpenClaw Gateway
281
+ */
282
+ async systemStop(data) {
283
+ return wrapAdminCall(async () => {
284
+ const context = getContext();
285
+ return await stop(data, context);
286
+ });
287
+ }
288
+ /**
289
+ * 运行诊断并自动修复
290
+ */
291
+ async systemDoctorFix(data) {
292
+ return wrapAdminCall(async () => {
293
+ const context = getContext();
294
+ return await doctorFix(data, context);
295
+ });
296
+ }
297
+ /**
298
+ * 获取最近100条日志
299
+ */
300
+ async systemLogs(data) {
301
+ return wrapAdminCall(async () => {
302
+ const context = getContext();
303
+ return await logs(data, context);
304
+ });
305
+ }
306
+ /**
307
+ * 示例方法:处理 status 类型的消息
308
+ */
309
+ async status(data) {
310
+ return {
311
+ status: "running",
312
+ uptime: process.uptime(),
313
+ memory: process.memoryUsage(),
314
+ };
315
+ }
316
+ /**
317
+ * 示例方法:处理 echo 类型的消息
318
+ */
319
+ async echo(data) {
320
+ return {
321
+ echoed: true,
322
+ data: data,
323
+ };
324
+ }
325
+ }
326
+ // 导出单例实例
327
+ export const messageHandler = new MessageHandler();
@@ -0,0 +1,43 @@
1
+ import { getOpenClawRoot, getProjectRoot } from './lib/paths.ts';
2
+ import { getMethod } from './methods/index.ts';
3
+ import { failure, JsonRpcException, JSON_RPC_ERRORS, parseRequest, serialize, success } from './jsonrpc.ts';
4
+ async function main() {
5
+ const raw = await readStdin();
6
+ const request = parseRequest(raw);
7
+ const context = {
8
+ projectRoot: getProjectRoot(),
9
+ openclawRoot: getOpenClawRoot()
10
+ };
11
+ try {
12
+ const handler = getMethod(request.method);
13
+ const result = await handler(request.params, context);
14
+ process.stdout.write(serialize(success(normalizeId(request.id), result)));
15
+ }
16
+ catch (error) {
17
+ process.stdout.write(serialize(toErrorResponse(normalizeId(request.id), error)));
18
+ process.exitCode = 1;
19
+ }
20
+ }
21
+ async function readStdin() {
22
+ const chunks = [];
23
+ for await (const chunk of process.stdin) {
24
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
25
+ }
26
+ const raw = Buffer.concat(chunks).toString('utf8').trim();
27
+ if (!raw) {
28
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidRequest, 'Request body is empty');
29
+ }
30
+ return raw;
31
+ }
32
+ function normalizeId(value) {
33
+ return value ?? null;
34
+ }
35
+ function toErrorResponse(id, error) {
36
+ if (error instanceof JsonRpcException) {
37
+ return failure(id, error.code, error.message, error.data);
38
+ }
39
+ return failure(id, JSON_RPC_ERRORS.internalError, 'Internal error', {
40
+ detail: error instanceof Error ? error.message : String(error)
41
+ });
42
+ }
43
+ await main();
@@ -0,0 +1,60 @@
1
+ export const JSON_RPC_VERSION = '2.0';
2
+ export const JSON_RPC_ERRORS = {
3
+ parseError: -32700,
4
+ invalidRequest: -32600,
5
+ methodNotFound: -32601,
6
+ invalidParams: -32602,
7
+ internalError: -32603
8
+ };
9
+ export class JsonRpcException extends Error {
10
+ constructor(code, message, data) {
11
+ super(message);
12
+ this.code = code;
13
+ this.data = data;
14
+ }
15
+ }
16
+ export function parseRequest(raw) {
17
+ let parsed;
18
+ try {
19
+ parsed = JSON.parse(raw);
20
+ }
21
+ catch (error) {
22
+ throw new JsonRpcException(JSON_RPC_ERRORS.parseError, 'Parse error', {
23
+ detail: error instanceof Error ? error.message : String(error)
24
+ });
25
+ }
26
+ if (!isRequest(parsed)) {
27
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidRequest, 'Invalid Request');
28
+ }
29
+ return parsed;
30
+ }
31
+ export function success(id, result) {
32
+ return {
33
+ jsonrpc: JSON_RPC_VERSION,
34
+ id,
35
+ result: result
36
+ };
37
+ }
38
+ export function failure(id, code, message, data) {
39
+ return {
40
+ jsonrpc: JSON_RPC_VERSION,
41
+ id,
42
+ error: {
43
+ code,
44
+ message,
45
+ ...(data === undefined ? {} : { data: data })
46
+ }
47
+ };
48
+ }
49
+ export function serialize(response) {
50
+ return `${JSON.stringify(response, null, 2)}\n`;
51
+ }
52
+ function isRequest(value) {
53
+ if (!value || typeof value !== 'object') {
54
+ return false;
55
+ }
56
+ const candidate = value;
57
+ return (candidate.jsonrpc === JSON_RPC_VERSION &&
58
+ typeof candidate.method === 'string' &&
59
+ candidate.method.length > 0);
60
+ }
@@ -0,0 +1,30 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ export async function readJsonFile(filePath) {
4
+ const raw = await fs.readFile(filePath, 'utf8');
5
+ return JSON.parse(raw);
6
+ }
7
+ export async function writeJsonFile(filePath, data) {
8
+ const json = JSON.stringify(data, null, 2);
9
+ await fs.writeFile(filePath, json, 'utf8');
10
+ }
11
+ export async function pathExists(targetPath) {
12
+ try {
13
+ await fs.access(targetPath);
14
+ return true;
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
20
+ export async function ensureDir(targetDir) {
21
+ await fs.mkdir(targetDir, { recursive: true });
22
+ }
23
+ export async function copyIfExists(sourcePath, destPath) {
24
+ if (!(await pathExists(sourcePath))) {
25
+ return false;
26
+ }
27
+ await ensureDir(path.dirname(destPath));
28
+ await fs.copyFile(sourcePath, destPath);
29
+ return true;
30
+ }
@@ -0,0 +1,46 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.ts';
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+ export function getProjectRoot() {
7
+ // __dirname 是 /path/to/rol-websocket-channel/src/admin/lib
8
+ // 需要返回 /path/to/rol-websocket-channel
9
+ const root = path.resolve(__dirname, '..', '..', '..');
10
+ console.log('[paths] __dirname:', __dirname);
11
+ console.log('[paths] getProjectRoot:', root);
12
+ return root;
13
+ }
14
+ export function getOpenClawRoot() {
15
+ if (process.env.OPENCLAW_HOME) {
16
+ console.log('[paths] getOpenClawRoot from env:', process.env.OPENCLAW_HOME);
17
+ return path.resolve(process.env.OPENCLAW_HOME);
18
+ }
19
+ // 插件在 ~/.openclaw/extensions/rol-websocket-channel
20
+ // 需要返回 ~/.openclaw
21
+ const projectRoot = getProjectRoot();
22
+ const parentDir = path.dirname(projectRoot);
23
+ const parentName = path.basename(parentDir);
24
+ console.log('[paths] projectRoot:', projectRoot);
25
+ console.log('[paths] parentDir:', parentDir);
26
+ console.log('[paths] parentName:', parentName);
27
+ if (parentName === 'extensions') {
28
+ // parentDir 是 ~/.openclaw/extensions
29
+ // 返回 ~/.openclaw
30
+ const openclawRoot = path.dirname(parentDir);
31
+ console.log('[paths] getOpenClawRoot (extensions):', openclawRoot);
32
+ return openclawRoot;
33
+ }
34
+ // 兜底:假设插件在 openclaw 根目录的子目录
35
+ const fallbackRoot = path.resolve(projectRoot, '..');
36
+ console.log('[paths] getOpenClawRoot (fallback):', fallbackRoot);
37
+ return fallbackRoot;
38
+ }
39
+ export function ensureInside(parentDir, targetPath) {
40
+ const parent = path.resolve(parentDir);
41
+ const target = path.resolve(targetPath);
42
+ if (target !== parent && !target.startsWith(`${parent}${path.sep}`)) {
43
+ throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Path escapes allowed directory', { parent, target });
44
+ }
45
+ return target;
46
+ }
@@ -0,0 +1,60 @@
1
+ import path from 'node:path';
2
+ import { readJsonFile } from '../lib/fs.ts';
3
+ export const getAgents = async (_params, context) => {
4
+ const configPath = path.join(context.openclawRoot, 'openclaw.json');
5
+ const config = await readJsonFile(configPath);
6
+ return {
7
+ sourceConfigFile: configPath,
8
+ defaultAgentConfig: config.agents?.defaults ?? {},
9
+ namedAgents: config.agents?.list ?? [],
10
+ activeToolProfile: config.tools?.profile ?? null
11
+ };
12
+ };
13
+ export const getConfig = async (params, context) => {
14
+ const configPath = path.join(context.openclawRoot, 'openclaw.json');
15
+ const config = await readJsonFile(configPath);
16
+ const objectParams = isObject(params) ? params : {};
17
+ const section = typeof objectParams.section === 'string' ? objectParams.section : null;
18
+ if (!section) {
19
+ return {
20
+ sourceConfigFile: configPath,
21
+ config: redactSecrets(config)
22
+ };
23
+ }
24
+ const value = config[section];
25
+ return {
26
+ sourceConfigFile: configPath,
27
+ section,
28
+ sectionValue: value === undefined ? null : redactSecrets(value)
29
+ };
30
+ };
31
+ function isObject(value) {
32
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
33
+ }
34
+ function redactSecrets(value) {
35
+ if (Array.isArray(value)) {
36
+ return value.map(redactSecrets);
37
+ }
38
+ if (!value || typeof value !== 'object') {
39
+ return value;
40
+ }
41
+ const result = {};
42
+ for (const [key, nestedValue] of Object.entries(value)) {
43
+ if (isSecretKey(key) && typeof nestedValue === 'string') {
44
+ result[key] = redactString(nestedValue);
45
+ continue;
46
+ }
47
+ result[key] = redactSecrets(nestedValue);
48
+ }
49
+ return result;
50
+ }
51
+ function isSecretKey(key) {
52
+ const normalized = key.toLowerCase();
53
+ return normalized.includes('apikey') || normalized.includes('token') || normalized.includes('secret');
54
+ }
55
+ function redactString(value) {
56
+ if (value.length <= 8) {
57
+ return '********';
58
+ }
59
+ return `${value.slice(0, 4)}***${value.slice(-4)}`;
60
+ }