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,546 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import { execFile } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
|
|
7
|
+
import { copyIfExists, ensureDir, pathExists } from '../lib/fs.ts';
|
|
8
|
+
import { ensureInside } from '../lib/paths.ts';
|
|
9
|
+
import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.ts';
|
|
10
|
+
import type { JsonValue, MethodHandler } from '../types.ts';
|
|
11
|
+
|
|
12
|
+
const execFileAsync = promisify(execFile);
|
|
13
|
+
|
|
14
|
+
interface MemoryGetParams {
|
|
15
|
+
path: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface MemoryExportZipParams {
|
|
19
|
+
outputDir?: string;
|
|
20
|
+
zipFileName?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface MemoryImportZipParams {
|
|
24
|
+
zipPath: string;
|
|
25
|
+
createBackup?: boolean;
|
|
26
|
+
includePaths?: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface MemoryPresignedPostParams {
|
|
30
|
+
body: Record<string, JsonValue>;
|
|
31
|
+
baseUrl?: string;
|
|
32
|
+
authToken?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface MemoryCreateBackupRecordParams {
|
|
36
|
+
body: Record<string, JsonValue>;
|
|
37
|
+
baseUrl?: string;
|
|
38
|
+
authToken?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface OpenClawConfig {
|
|
42
|
+
plugins?: {
|
|
43
|
+
entries?: Record<string, {
|
|
44
|
+
config?: {
|
|
45
|
+
apiCoreBot?: {
|
|
46
|
+
baseUrl?: string;
|
|
47
|
+
authToken?: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
}>;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const WORKSPACE_ROOT_FILES = [
|
|
55
|
+
'AGENTS.md',
|
|
56
|
+
'HEARTBEAT.md',
|
|
57
|
+
'IDENTITY.md',
|
|
58
|
+
'MEMORY.md',
|
|
59
|
+
'SoUL.md',
|
|
60
|
+
'TooLS.md',
|
|
61
|
+
'USER.md'
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
export const listMemoryFiles: MethodHandler = async (_params, context): Promise<JsonValue> => {
|
|
65
|
+
const workspaceDir = path.join(context.openclawRoot, 'workspace');
|
|
66
|
+
const items: JsonValue[] = [];
|
|
67
|
+
|
|
68
|
+
for (const fileName of WORKSPACE_ROOT_FILES) {
|
|
69
|
+
const filePath = path.join(workspaceDir, fileName);
|
|
70
|
+
if (!(await pathExists(filePath))) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const stat = await fs.stat(filePath);
|
|
75
|
+
items.push({
|
|
76
|
+
path: `workspace/${fileName}`,
|
|
77
|
+
size: stat.size,
|
|
78
|
+
updatedAt: stat.mtime.toISOString()
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const rootConfigPath = path.join(context.openclawRoot, 'openclaw.json');
|
|
83
|
+
if (await pathExists(rootConfigPath)) {
|
|
84
|
+
const stat = await fs.stat(rootConfigPath);
|
|
85
|
+
items.push({
|
|
86
|
+
path: 'openclaw.json',
|
|
87
|
+
size: stat.size,
|
|
88
|
+
updatedAt: stat.mtime.toISOString()
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const credentialsDir = path.join(context.openclawRoot, 'credentials');
|
|
93
|
+
if (await pathExists(credentialsDir)) {
|
|
94
|
+
const credentialEntries = await fs.readdir(credentialsDir, { withFileTypes: true });
|
|
95
|
+
for (const entry of credentialEntries) {
|
|
96
|
+
if (!entry.isFile()) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const fullPath = path.join(credentialsDir, entry.name);
|
|
101
|
+
const stat = await fs.stat(fullPath);
|
|
102
|
+
items.push({
|
|
103
|
+
path: `credentials/${entry.name}`,
|
|
104
|
+
size: stat.size,
|
|
105
|
+
updatedAt: stat.mtime.toISOString()
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
count: items.length,
|
|
112
|
+
items
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const getMemoryFile: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
117
|
+
const objectParams = expectObject(params);
|
|
118
|
+
const filePath = expectString(objectParams.path, 'path');
|
|
119
|
+
const resolved = resolveMemoryPath(context.openclawRoot, filePath);
|
|
120
|
+
const content = await fs.readFile(resolved, 'utf8');
|
|
121
|
+
const stat = await fs.stat(resolved);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
path: filePath,
|
|
125
|
+
size: stat.size,
|
|
126
|
+
updatedAt: stat.mtime.toISOString(),
|
|
127
|
+
content
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const backupMemory: MethodHandler = async (_params, context): Promise<JsonValue> => {
|
|
132
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
133
|
+
const backupRoot = path.join(context.projectRoot, 'backups', 'memory', stamp);
|
|
134
|
+
const manifest: {
|
|
135
|
+
createdAt: string;
|
|
136
|
+
files: string[];
|
|
137
|
+
} = {
|
|
138
|
+
createdAt: new Date().toISOString(),
|
|
139
|
+
files: []
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
await ensureDir(backupRoot);
|
|
143
|
+
|
|
144
|
+
if (await copyIfExists(
|
|
145
|
+
path.join(context.openclawRoot, 'openclaw.json'),
|
|
146
|
+
path.join(backupRoot, 'openclaw.json')
|
|
147
|
+
)) {
|
|
148
|
+
manifest.files.push('openclaw.json');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
for (const fileName of WORKSPACE_ROOT_FILES) {
|
|
152
|
+
const source = path.join(context.openclawRoot, 'workspace', fileName);
|
|
153
|
+
const dest = path.join(backupRoot, 'workspace', fileName);
|
|
154
|
+
if (await copyIfExists(source, dest)) {
|
|
155
|
+
manifest.files.push(path.relative(backupRoot, dest).replaceAll('\\', '/'));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const credentialsDir = path.join(context.openclawRoot, 'credentials');
|
|
160
|
+
if (await pathExists(credentialsDir)) {
|
|
161
|
+
const copiedCredentialFiles = await copyDirectoryTree(
|
|
162
|
+
credentialsDir,
|
|
163
|
+
path.join(backupRoot, 'credentials')
|
|
164
|
+
);
|
|
165
|
+
manifest.files.push(...copiedCredentialFiles.map((file) => `credentials/${file}`));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
await fs.writeFile(
|
|
169
|
+
path.join(backupRoot, 'manifest.json'),
|
|
170
|
+
JSON.stringify(manifest, null, 2),
|
|
171
|
+
'utf8'
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
backupId: stamp,
|
|
176
|
+
backupPath: backupRoot,
|
|
177
|
+
files: manifest.files
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export const exportMemoryZip: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
182
|
+
const objectParams = ((params ? expectObject(params) : {}) as unknown) as MemoryExportZipParams;
|
|
183
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
184
|
+
const outputDirInput = objectParams.outputDir ?? path.join(context.projectRoot, 'exports', 'memory');
|
|
185
|
+
const outputDir = path.resolve(outputDirInput);
|
|
186
|
+
const zipFileName = sanitizeZipFileName(objectParams.zipFileName ?? `memory-backup-${stamp}.zip`);
|
|
187
|
+
|
|
188
|
+
await ensureDir(outputDir);
|
|
189
|
+
|
|
190
|
+
const snapshot = await backupMemory(undefined, context);
|
|
191
|
+
const snapshotRoot = expectString((snapshot as Record<string, JsonValue>).backupPath, 'backupPath');
|
|
192
|
+
const zipPath = path.join(outputDir, zipFileName);
|
|
193
|
+
|
|
194
|
+
await zipDirectory(snapshotRoot, zipPath);
|
|
195
|
+
const stat = await fs.stat(zipPath);
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
ok: true,
|
|
199
|
+
zipPath,
|
|
200
|
+
size: stat.size,
|
|
201
|
+
snapshot
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export const importMemoryZip: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
206
|
+
const objectParams = expectObject(params) as unknown as MemoryImportZipParams;
|
|
207
|
+
const zipPath = path.resolve(expectString(objectParams.zipPath, 'zipPath'));
|
|
208
|
+
const createBackup = objectParams.createBackup !== false;
|
|
209
|
+
const includePaths = Array.isArray(objectParams.includePaths)
|
|
210
|
+
? objectParams.includePaths.filter((item): item is string => typeof item === 'string' && item.trim().length > 0)
|
|
211
|
+
: undefined;
|
|
212
|
+
|
|
213
|
+
if (!(await pathExists(zipPath))) {
|
|
214
|
+
throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Zip file does not exist', { zipPath });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
let backupResult: JsonValue | null = null;
|
|
218
|
+
if (createBackup) {
|
|
219
|
+
backupResult = await backupMemory(undefined, context);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const extractRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'openclaw-ts-memory-import-'));
|
|
223
|
+
try {
|
|
224
|
+
await unzipArchive(zipPath, extractRoot);
|
|
225
|
+
const importRoot = await resolveImportedRoot(extractRoot);
|
|
226
|
+
const restoredFiles = await restoreImportedMemory(importRoot, context.openclawRoot, includePaths);
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
ok: true,
|
|
230
|
+
zipPath,
|
|
231
|
+
restoredFiles,
|
|
232
|
+
backup: backupResult
|
|
233
|
+
};
|
|
234
|
+
} finally {
|
|
235
|
+
await fs.rm(extractRoot, { recursive: true, force: true });
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const getMemoryPresignedPost: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
240
|
+
const objectParams = expectObject(params) as unknown as MemoryPresignedPostParams;
|
|
241
|
+
const body = isObject(objectParams.body as any) ? objectParams.body : null;
|
|
242
|
+
if (!body) {
|
|
243
|
+
throw new JsonRpcException(
|
|
244
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
245
|
+
'Missing required parameter: body'
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const endpointConfig = await resolveApiCoreBotEndpoint(context.openclawRoot, objectParams.baseUrl, objectParams.authToken);
|
|
250
|
+
const response = await postJson(
|
|
251
|
+
`${endpointConfig.baseUrl}/api-core-bot/front/s3/get-presigned-post`,
|
|
252
|
+
body,
|
|
253
|
+
endpointConfig.authToken
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
ok: true,
|
|
258
|
+
endpoint: '/api-core-bot/front/s3/get-presigned-post',
|
|
259
|
+
data: response
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export const createMemoryBackupRecord: MethodHandler = async (params, context): Promise<JsonValue> => {
|
|
264
|
+
const objectParams = expectObject(params) as unknown as MemoryCreateBackupRecordParams;
|
|
265
|
+
const body = isObject(objectParams.body as any) ? objectParams.body : null;
|
|
266
|
+
if (!body) {
|
|
267
|
+
throw new JsonRpcException(
|
|
268
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
269
|
+
'Missing required parameter: body'
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const endpointConfig = await resolveApiCoreBotEndpoint(context.openclawRoot, objectParams.baseUrl, objectParams.authToken);
|
|
274
|
+
const response = await postJson(
|
|
275
|
+
`${endpointConfig.baseUrl}/api-core-bot/front/file/backup/create`,
|
|
276
|
+
body,
|
|
277
|
+
endpointConfig.authToken
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
ok: true,
|
|
282
|
+
endpoint: '/api-core-bot/front/file/backup/create',
|
|
283
|
+
data: response
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
function expectObject(value: JsonValue | undefined): Record<string, JsonValue> {
|
|
288
|
+
if (!value || Array.isArray(value) || typeof value !== 'object') {
|
|
289
|
+
throw new JsonRpcException(JSON_RPC_ERRORS.invalidParams, 'Params must be an object');
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return value as Record<string, JsonValue>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function expectString(value: JsonValue | undefined, fieldName: string): string {
|
|
296
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
297
|
+
throw new JsonRpcException(
|
|
298
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
299
|
+
`Field '${fieldName}' must be a non-empty string`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return value;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function resolveMemoryPath(openclawRoot: string, relativePath: string): string {
|
|
307
|
+
const workspaceDir = path.join(openclawRoot, 'workspace');
|
|
308
|
+
const allowedRoots = [
|
|
309
|
+
workspaceDir,
|
|
310
|
+
path.join(openclawRoot, 'credentials'),
|
|
311
|
+
openclawRoot
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
const candidate = path.join(openclawRoot, relativePath);
|
|
315
|
+
|
|
316
|
+
for (const allowedRoot of allowedRoots) {
|
|
317
|
+
try {
|
|
318
|
+
return ensureInside(allowedRoot, candidate);
|
|
319
|
+
} catch {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
throw new JsonRpcException(
|
|
325
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
326
|
+
'Only workspace memory files are readable',
|
|
327
|
+
{ path: relativePath }
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function sanitizeZipFileName(fileName: string): string {
|
|
332
|
+
return fileName.endsWith('.zip') ? fileName : `${fileName}.zip`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function resolveApiCoreBotEndpoint(
|
|
336
|
+
openclawRoot: string,
|
|
337
|
+
overrideBaseUrl?: string,
|
|
338
|
+
overrideAuthToken?: string
|
|
339
|
+
): Promise<{ baseUrl: string; authToken?: string }> {
|
|
340
|
+
const config = await readJsonFile<OpenClawConfig>(path.join(openclawRoot, 'openclaw.json'));
|
|
341
|
+
const pluginConfig = config.plugins?.entries?.['rol-websocket-channel']?.config?.apiCoreBot ?? {};
|
|
342
|
+
const baseUrl = (overrideBaseUrl && overrideBaseUrl.trim()) || pluginConfig.baseUrl;
|
|
343
|
+
|
|
344
|
+
if (!baseUrl || !baseUrl.trim()) {
|
|
345
|
+
throw new JsonRpcException(
|
|
346
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
347
|
+
'apiCoreBot.baseUrl is not configured'
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const authToken = (overrideAuthToken && overrideAuthToken.trim()) || pluginConfig.authToken;
|
|
352
|
+
return {
|
|
353
|
+
baseUrl: baseUrl.replace(/\/+$/, ''),
|
|
354
|
+
authToken
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async function postJson(
|
|
359
|
+
url: string,
|
|
360
|
+
body: Record<string, JsonValue>,
|
|
361
|
+
authToken?: string
|
|
362
|
+
): Promise<unknown> {
|
|
363
|
+
const headers: Record<string, string> = {
|
|
364
|
+
'Content-Type': 'application/json'
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
if (authToken && authToken.trim()) {
|
|
368
|
+
headers.Authorization = `Bearer ${authToken.trim()}`;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const response = await fetch(url, {
|
|
372
|
+
method: 'POST',
|
|
373
|
+
headers,
|
|
374
|
+
body: JSON.stringify(body)
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const payload = await response.json().catch(async () => await response.text());
|
|
378
|
+
if (!response.ok) {
|
|
379
|
+
throw new JsonRpcException(
|
|
380
|
+
JSON_RPC_ERRORS.internalError,
|
|
381
|
+
`Request failed: ${response.status}`,
|
|
382
|
+
{
|
|
383
|
+
url,
|
|
384
|
+
status: response.status,
|
|
385
|
+
payload
|
|
386
|
+
}
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return payload;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async function zipDirectory(sourceDir: string, zipPath: string): Promise<void> {
|
|
394
|
+
await fs.rm(zipPath, { force: true });
|
|
395
|
+
|
|
396
|
+
if (process.platform === 'win32') {
|
|
397
|
+
await execFileAsync('powershell.exe', [
|
|
398
|
+
'-NoProfile',
|
|
399
|
+
'-Command',
|
|
400
|
+
`Compress-Archive -Path '${sourceDir}\\*' -DestinationPath '${zipPath}' -Force`
|
|
401
|
+
]);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
await execFileAsync('zip', ['-r', zipPath, '.'], {
|
|
406
|
+
cwd: sourceDir
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function unzipArchive(zipPath: string, extractRoot: string): Promise<void> {
|
|
411
|
+
if (process.platform === 'win32') {
|
|
412
|
+
await execFileAsync('powershell.exe', [
|
|
413
|
+
'-NoProfile',
|
|
414
|
+
'-Command',
|
|
415
|
+
`Expand-Archive -Path '${zipPath}' -DestinationPath '${extractRoot}' -Force`
|
|
416
|
+
]);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
await execFileAsync('unzip', ['-o', zipPath, '-d', extractRoot]);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async function resolveImportedRoot(extractRoot: string): Promise<string> {
|
|
424
|
+
const directWorkspace = path.join(extractRoot, 'workspace');
|
|
425
|
+
const directConfig = path.join(extractRoot, 'openclaw.json');
|
|
426
|
+
if ((await pathExists(directWorkspace)) || (await pathExists(directConfig))) {
|
|
427
|
+
return extractRoot;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const entries = await fs.readdir(extractRoot, { withFileTypes: true });
|
|
431
|
+
for (const entry of entries) {
|
|
432
|
+
if (!entry.isDirectory()) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const candidate = path.join(extractRoot, entry.name);
|
|
437
|
+
if ((await pathExists(path.join(candidate, 'workspace'))) || (await pathExists(path.join(candidate, 'openclaw.json')))) {
|
|
438
|
+
return candidate;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
throw new JsonRpcException(
|
|
443
|
+
JSON_RPC_ERRORS.invalidParams,
|
|
444
|
+
'Imported zip does not contain a valid memory snapshot'
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async function restoreImportedMemory(
|
|
449
|
+
importRoot: string,
|
|
450
|
+
openclawRoot: string,
|
|
451
|
+
includePaths?: string[]
|
|
452
|
+
): Promise<string[]> {
|
|
453
|
+
const restored: string[] = [];
|
|
454
|
+
const workspaceRoot = path.join(openclawRoot, 'workspace');
|
|
455
|
+
const importedCredentialsDir = path.join(importRoot, 'credentials');
|
|
456
|
+
|
|
457
|
+
if (shouldRestorePath('openclaw.json', includePaths) && await copyIfExists(
|
|
458
|
+
path.join(importRoot, 'openclaw.json'),
|
|
459
|
+
path.join(openclawRoot, 'openclaw.json')
|
|
460
|
+
)) {
|
|
461
|
+
restored.push('openclaw.json');
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (const fileName of WORKSPACE_ROOT_FILES) {
|
|
465
|
+
const source = path.join(importRoot, 'workspace', fileName);
|
|
466
|
+
const dest = path.join(workspaceRoot, fileName);
|
|
467
|
+
const logicalPath = `workspace/${fileName}`;
|
|
468
|
+
if (shouldRestorePath(logicalPath, includePaths) && await copyIfExists(source, dest)) {
|
|
469
|
+
restored.push(`workspace/${fileName}`);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if ((shouldRestorePath('credentials/', includePaths) || hasNestedInclude('credentials/', includePaths)) && await pathExists(importedCredentialsDir)) {
|
|
474
|
+
const restoredCredentialFiles = await copyDirectoryTree(
|
|
475
|
+
importedCredentialsDir,
|
|
476
|
+
path.join(openclawRoot, 'credentials'),
|
|
477
|
+
'',
|
|
478
|
+
includePaths
|
|
479
|
+
);
|
|
480
|
+
restored.push(...restoredCredentialFiles.map((file) => `credentials/${file}`));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return restored;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async function copyDirectoryTree(
|
|
487
|
+
sourceDir: string,
|
|
488
|
+
destDir: string,
|
|
489
|
+
relativePrefix = '',
|
|
490
|
+
includePaths?: string[]
|
|
491
|
+
): Promise<string[]> {
|
|
492
|
+
await ensureDir(destDir);
|
|
493
|
+
const copied: string[] = [];
|
|
494
|
+
const entries = await fs.readdir(sourceDir, { withFileTypes: true });
|
|
495
|
+
|
|
496
|
+
for (const entry of entries) {
|
|
497
|
+
const source = path.join(sourceDir, entry.name);
|
|
498
|
+
const dest = path.join(destDir, entry.name);
|
|
499
|
+
const relativePath = relativePrefix ? `${relativePrefix}/${entry.name}` : entry.name;
|
|
500
|
+
const logicalPath = `credentials/${relativePath.replaceAll('\\', '/')}`;
|
|
501
|
+
|
|
502
|
+
if (entry.isDirectory()) {
|
|
503
|
+
if (!hasNestedInclude(`${logicalPath}/`, includePaths) && !shouldRestorePath(`${logicalPath}/`, includePaths)) {
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
const nested = await copyDirectoryTree(source, dest, relativePath, includePaths);
|
|
507
|
+
copied.push(...nested);
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (!entry.isFile()) {
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (!shouldRestorePath(logicalPath, includePaths) && !shouldRestorePath('credentials/', includePaths)) {
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
await copyIfExists(source, dest);
|
|
520
|
+
copied.push(relativePath.replaceAll('\\', '/'));
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return copied;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function shouldRestorePath(targetPath: string, includePaths?: string[]): boolean {
|
|
527
|
+
if (!includePaths || includePaths.length === 0) {
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const normalizedTarget = normalizeLogicalPath(targetPath);
|
|
532
|
+
return includePaths.some((value) => normalizeLogicalPath(value) === normalizedTarget);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function hasNestedInclude(prefix: string, includePaths?: string[]): boolean {
|
|
536
|
+
if (!includePaths || includePaths.length === 0) {
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const normalizedPrefix = normalizeLogicalPath(prefix).replace(/\/?$/, '/');
|
|
541
|
+
return includePaths.some((value) => normalizeLogicalPath(value).startsWith(normalizedPrefix));
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function normalizeLogicalPath(value: string): string {
|
|
545
|
+
return value.replaceAll('\\', '/').replace(/^\/+/, '').replace(/\/+$/, '');
|
|
546
|
+
}
|