@lvce-editor/chat-tool-worker 5.4.0 → 7.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/README.md +2 -2
- package/dist/chatToolWorkerMain.js +1319 -106
- package/package.json +2 -2
|
@@ -1,166 +1,1379 @@
|
|
|
1
|
-
|
|
1
|
+
const normalizeLine = line => {
|
|
2
|
+
if (line.startsWith('Error: ')) {
|
|
3
|
+
return line.slice('Error: '.length);
|
|
4
|
+
}
|
|
5
|
+
if (line.startsWith('VError: ')) {
|
|
6
|
+
return line.slice('VError: '.length);
|
|
7
|
+
}
|
|
8
|
+
return line;
|
|
9
|
+
};
|
|
10
|
+
const getCombinedMessage = (error, message) => {
|
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
12
|
+
if (message) {
|
|
13
|
+
return `${message}: ${stringifiedError}`;
|
|
14
|
+
}
|
|
15
|
+
return stringifiedError;
|
|
16
|
+
};
|
|
17
|
+
const NewLine$2 = '\n';
|
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
19
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
20
|
+
};
|
|
21
|
+
const mergeStacks = (parent, child) => {
|
|
22
|
+
if (!child) {
|
|
23
|
+
return parent;
|
|
24
|
+
}
|
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
27
|
+
if (childNewLineIndex === -1) {
|
|
28
|
+
return parent;
|
|
29
|
+
}
|
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
34
|
+
return parentFirstLine + childRest;
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
};
|
|
38
|
+
class VError extends Error {
|
|
39
|
+
constructor(error, message) {
|
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
41
|
+
super(combinedMessage);
|
|
42
|
+
this.name = 'VError';
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
45
|
+
}
|
|
46
|
+
if (error.codeFrame) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
this.codeFrame = error.codeFrame;
|
|
49
|
+
}
|
|
50
|
+
if (error.code) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.code = error.code;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
2
56
|
|
|
3
|
-
const
|
|
57
|
+
const isMessagePort = value => {
|
|
58
|
+
return value && value instanceof MessagePort;
|
|
59
|
+
};
|
|
60
|
+
const isMessagePortMain = value => {
|
|
61
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
62
|
+
};
|
|
63
|
+
const isOffscreenCanvas = value => {
|
|
64
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
65
|
+
};
|
|
66
|
+
const isInstanceOf = (value, constructorName) => {
|
|
67
|
+
return value?.constructor?.name === constructorName;
|
|
68
|
+
};
|
|
69
|
+
const isSocket = value => {
|
|
70
|
+
return isInstanceOf(value, 'Socket');
|
|
71
|
+
};
|
|
72
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
73
|
+
const isTransferrable = value => {
|
|
74
|
+
for (const fn of transferrables) {
|
|
75
|
+
if (fn(value)) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
81
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
82
|
+
if (!value) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (isTransferrable(value)) {
|
|
86
|
+
transferrables.push(value);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
for (const item of value) {
|
|
91
|
+
walkValue(item, transferrables, isTransferrable);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === 'object') {
|
|
96
|
+
for (const property of Object.values(value)) {
|
|
97
|
+
walkValue(property, transferrables, isTransferrable);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const getTransferrables = value => {
|
|
103
|
+
const transferrables = [];
|
|
104
|
+
walkValue(value, transferrables, isTransferrable);
|
|
105
|
+
return transferrables;
|
|
106
|
+
};
|
|
107
|
+
const attachEvents = that => {
|
|
108
|
+
const handleMessage = (...args) => {
|
|
109
|
+
const data = that.getData(...args);
|
|
110
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
111
|
+
data
|
|
112
|
+
}));
|
|
113
|
+
};
|
|
114
|
+
that.onMessage(handleMessage);
|
|
115
|
+
const handleClose = event => {
|
|
116
|
+
that.dispatchEvent(new Event('close'));
|
|
117
|
+
};
|
|
118
|
+
that.onClose(handleClose);
|
|
119
|
+
};
|
|
120
|
+
class Ipc extends EventTarget {
|
|
121
|
+
constructor(rawIpc) {
|
|
122
|
+
super();
|
|
123
|
+
this._rawIpc = rawIpc;
|
|
124
|
+
attachEvents(this);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
128
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
129
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
130
|
+
const NewLine$1 = '\n';
|
|
131
|
+
const joinLines$1 = lines => {
|
|
132
|
+
return lines.join(NewLine$1);
|
|
133
|
+
};
|
|
134
|
+
const RE_AT = /^\s+at/;
|
|
135
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
136
|
+
const isNormalStackLine = line => {
|
|
137
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
138
|
+
};
|
|
139
|
+
const getDetails = lines => {
|
|
140
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
141
|
+
if (index === -1) {
|
|
142
|
+
return {
|
|
143
|
+
actualMessage: joinLines$1(lines),
|
|
144
|
+
rest: []
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
let lastIndex = index - 1;
|
|
148
|
+
while (++lastIndex < lines.length) {
|
|
149
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
4
153
|
return {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type: 'error'
|
|
8
|
-
}),
|
|
9
|
-
role: 'tool',
|
|
10
|
-
tool_call_id: toolCallId
|
|
154
|
+
actualMessage: lines[index - 1],
|
|
155
|
+
rest: lines.slice(index, lastIndex)
|
|
11
156
|
};
|
|
12
157
|
};
|
|
13
|
-
|
|
14
|
-
|
|
158
|
+
const splitLines$1 = lines => {
|
|
159
|
+
return lines.split(NewLine$1);
|
|
160
|
+
};
|
|
161
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
162
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
163
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
164
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
165
|
+
};
|
|
166
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
167
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
168
|
+
};
|
|
169
|
+
const getMessageCodeBlock = stderr => {
|
|
170
|
+
const lines = splitLines$1(stderr);
|
|
171
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
172
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
173
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
174
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
175
|
+
return relevantMessage;
|
|
176
|
+
};
|
|
177
|
+
const isModuleNotFoundMessage = line => {
|
|
178
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
179
|
+
};
|
|
180
|
+
const getModuleNotFoundError = stderr => {
|
|
181
|
+
const lines = splitLines$1(stderr);
|
|
182
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
183
|
+
const message = lines[messageIndex];
|
|
15
184
|
return {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
type: 'success'
|
|
19
|
-
}),
|
|
20
|
-
role: 'tool',
|
|
21
|
-
tool_call_id: toolCallId
|
|
185
|
+
code: ERR_MODULE_NOT_FOUND,
|
|
186
|
+
message
|
|
22
187
|
};
|
|
23
188
|
};
|
|
189
|
+
const isModuleNotFoundError = stderr => {
|
|
190
|
+
if (!stderr) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
194
|
+
};
|
|
195
|
+
const isModulesSyntaxError = stderr => {
|
|
196
|
+
if (!stderr) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
200
|
+
};
|
|
201
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
202
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
203
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
204
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
205
|
+
};
|
|
206
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
207
|
+
const message = getMessageCodeBlock(stderr);
|
|
208
|
+
return {
|
|
209
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE,
|
|
210
|
+
message: `Incompatible native node module: ${message}`
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
const getModuleSyntaxError = () => {
|
|
214
|
+
return {
|
|
215
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
|
|
216
|
+
message: `ES Modules are not supported in electron`
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
220
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
221
|
+
return getNativeModuleErrorMessage(stderr);
|
|
222
|
+
}
|
|
223
|
+
if (isModulesSyntaxError(stderr)) {
|
|
224
|
+
return getModuleSyntaxError();
|
|
225
|
+
}
|
|
226
|
+
if (isModuleNotFoundError(stderr)) {
|
|
227
|
+
return getModuleNotFoundError(stderr);
|
|
228
|
+
}
|
|
229
|
+
const lines = splitLines$1(stderr);
|
|
230
|
+
const {
|
|
231
|
+
actualMessage,
|
|
232
|
+
rest
|
|
233
|
+
} = getDetails(lines);
|
|
234
|
+
return {
|
|
235
|
+
code: '',
|
|
236
|
+
message: actualMessage,
|
|
237
|
+
stack: rest
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
class IpcError extends VError {
|
|
241
|
+
// @ts-ignore
|
|
242
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
243
|
+
if (stdout || stderr) {
|
|
244
|
+
// @ts-ignore
|
|
245
|
+
const {
|
|
246
|
+
code,
|
|
247
|
+
message,
|
|
248
|
+
stack
|
|
249
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
250
|
+
const cause = new Error(message);
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
cause.code = code;
|
|
253
|
+
cause.stack = stack;
|
|
254
|
+
super(cause, betterMessage);
|
|
255
|
+
} else {
|
|
256
|
+
super(betterMessage);
|
|
257
|
+
}
|
|
258
|
+
// @ts-ignore
|
|
259
|
+
this.name = 'IpcError';
|
|
260
|
+
// @ts-ignore
|
|
261
|
+
this.stdout = stdout;
|
|
262
|
+
// @ts-ignore
|
|
263
|
+
this.stderr = stderr;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const readyMessage = 'ready';
|
|
267
|
+
const getData$2 = event => {
|
|
268
|
+
return event.data;
|
|
269
|
+
};
|
|
270
|
+
const listen$8 = ({
|
|
271
|
+
port
|
|
272
|
+
}) => {
|
|
273
|
+
return port;
|
|
274
|
+
};
|
|
275
|
+
const signal$9 = port => {
|
|
276
|
+
port.postMessage(readyMessage);
|
|
277
|
+
};
|
|
278
|
+
class IpcChildWithMessagePort extends Ipc {
|
|
279
|
+
getData(event) {
|
|
280
|
+
return getData$2(event);
|
|
281
|
+
}
|
|
282
|
+
send(message) {
|
|
283
|
+
this._rawIpc.postMessage(message);
|
|
284
|
+
}
|
|
285
|
+
sendAndTransfer(message) {
|
|
286
|
+
const transfer = getTransferrables(message);
|
|
287
|
+
this._rawIpc.postMessage(message, transfer);
|
|
288
|
+
}
|
|
289
|
+
dispose() {
|
|
290
|
+
// ignore
|
|
291
|
+
}
|
|
292
|
+
onClose(callback) {
|
|
293
|
+
// ignore
|
|
294
|
+
}
|
|
295
|
+
onMessage(callback) {
|
|
296
|
+
this._rawIpc.addEventListener('message', callback);
|
|
297
|
+
this._rawIpc.start();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const wrap$g = port => {
|
|
301
|
+
return new IpcChildWithMessagePort(port);
|
|
302
|
+
};
|
|
303
|
+
const IpcChildWithMessagePort$1 = {
|
|
304
|
+
__proto__: null,
|
|
305
|
+
listen: listen$8,
|
|
306
|
+
signal: signal$9,
|
|
307
|
+
wrap: wrap$g
|
|
308
|
+
};
|
|
309
|
+
const listen$7 = () => {
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
312
|
+
throw new TypeError('module is not in web worker scope');
|
|
313
|
+
}
|
|
314
|
+
return globalThis;
|
|
315
|
+
};
|
|
316
|
+
const signal$8 = global => {
|
|
317
|
+
global.postMessage(readyMessage);
|
|
318
|
+
};
|
|
319
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
320
|
+
getData(event) {
|
|
321
|
+
return getData$2(event);
|
|
322
|
+
}
|
|
323
|
+
send(message) {
|
|
324
|
+
// @ts-ignore
|
|
325
|
+
this._rawIpc.postMessage(message);
|
|
326
|
+
}
|
|
327
|
+
sendAndTransfer(message) {
|
|
328
|
+
const transfer = getTransferrables(message);
|
|
329
|
+
// @ts-ignore
|
|
330
|
+
this._rawIpc.postMessage(message, transfer);
|
|
331
|
+
}
|
|
332
|
+
dispose() {
|
|
333
|
+
// ignore
|
|
334
|
+
}
|
|
335
|
+
onClose(callback) {
|
|
336
|
+
// ignore
|
|
337
|
+
}
|
|
338
|
+
onMessage(callback) {
|
|
339
|
+
this._rawIpc.addEventListener('message', callback);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const wrap$f = global => {
|
|
343
|
+
return new IpcChildWithModuleWorker(global);
|
|
344
|
+
};
|
|
345
|
+
const waitForFirstMessage = async port => {
|
|
346
|
+
const {
|
|
347
|
+
promise,
|
|
348
|
+
resolve
|
|
349
|
+
} = Promise.withResolvers();
|
|
350
|
+
port.addEventListener('message', resolve, {
|
|
351
|
+
once: true
|
|
352
|
+
});
|
|
353
|
+
const event = await promise;
|
|
354
|
+
// @ts-ignore
|
|
355
|
+
return event.data;
|
|
356
|
+
};
|
|
357
|
+
const listen$6 = async () => {
|
|
358
|
+
const parentIpcRaw = listen$7();
|
|
359
|
+
signal$8(parentIpcRaw);
|
|
360
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
361
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
362
|
+
if (firstMessage.method !== 'initialize') {
|
|
363
|
+
throw new IpcError('unexpected first message');
|
|
364
|
+
}
|
|
365
|
+
const type = firstMessage.params[0];
|
|
366
|
+
if (type === 'message-port') {
|
|
367
|
+
parentIpc.send({
|
|
368
|
+
id: firstMessage.id,
|
|
369
|
+
jsonrpc: '2.0',
|
|
370
|
+
result: null
|
|
371
|
+
});
|
|
372
|
+
parentIpc.dispose();
|
|
373
|
+
const port = firstMessage.params[1];
|
|
374
|
+
return port;
|
|
375
|
+
}
|
|
376
|
+
return globalThis;
|
|
377
|
+
};
|
|
378
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
379
|
+
getData(event) {
|
|
380
|
+
return getData$2(event);
|
|
381
|
+
}
|
|
382
|
+
send(message) {
|
|
383
|
+
this._rawIpc.postMessage(message);
|
|
384
|
+
}
|
|
385
|
+
sendAndTransfer(message) {
|
|
386
|
+
const transfer = getTransferrables(message);
|
|
387
|
+
this._rawIpc.postMessage(message, transfer);
|
|
388
|
+
}
|
|
389
|
+
dispose() {
|
|
390
|
+
if (this._rawIpc.close) {
|
|
391
|
+
this._rawIpc.close();
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
onClose(callback) {
|
|
395
|
+
// ignore
|
|
396
|
+
}
|
|
397
|
+
onMessage(callback) {
|
|
398
|
+
this._rawIpc.addEventListener('message', callback);
|
|
399
|
+
this._rawIpc.start();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const wrap$e = port => {
|
|
403
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
404
|
+
};
|
|
405
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
406
|
+
__proto__: null,
|
|
407
|
+
listen: listen$6,
|
|
408
|
+
wrap: wrap$e
|
|
409
|
+
};
|
|
24
410
|
|
|
25
|
-
|
|
26
|
-
|
|
411
|
+
class CommandNotFoundError extends Error {
|
|
412
|
+
constructor(command) {
|
|
413
|
+
super(`Command not found ${command}`);
|
|
414
|
+
this.name = 'CommandNotFoundError';
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const commands = Object.create(null);
|
|
418
|
+
const register = commandMap => {
|
|
419
|
+
Object.assign(commands, commandMap);
|
|
420
|
+
};
|
|
421
|
+
const getCommand = key => {
|
|
422
|
+
return commands[key];
|
|
423
|
+
};
|
|
424
|
+
const execute = (command, ...args) => {
|
|
425
|
+
const fn = getCommand(command);
|
|
426
|
+
if (!fn) {
|
|
427
|
+
throw new CommandNotFoundError(command);
|
|
428
|
+
}
|
|
429
|
+
return fn(...args);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const Two$1 = '2.0';
|
|
433
|
+
const callbacks = Object.create(null);
|
|
434
|
+
const get$1 = id => {
|
|
435
|
+
return callbacks[id];
|
|
436
|
+
};
|
|
437
|
+
const remove$1 = id => {
|
|
438
|
+
delete callbacks[id];
|
|
439
|
+
};
|
|
440
|
+
class JsonRpcError extends Error {
|
|
441
|
+
constructor(message) {
|
|
442
|
+
super(message);
|
|
443
|
+
this.name = 'JsonRpcError';
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const NewLine = '\n';
|
|
447
|
+
const DomException = 'DOMException';
|
|
448
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
449
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
450
|
+
const TypeError$1 = 'TypeError';
|
|
451
|
+
const getErrorConstructor = (message, type) => {
|
|
452
|
+
if (type) {
|
|
453
|
+
switch (type) {
|
|
454
|
+
case DomException:
|
|
455
|
+
return DOMException;
|
|
456
|
+
case ReferenceError$1:
|
|
457
|
+
return ReferenceError;
|
|
458
|
+
case SyntaxError$1:
|
|
459
|
+
return SyntaxError;
|
|
460
|
+
case TypeError$1:
|
|
461
|
+
return TypeError;
|
|
462
|
+
default:
|
|
463
|
+
return Error;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (message.startsWith('TypeError: ')) {
|
|
467
|
+
return TypeError;
|
|
468
|
+
}
|
|
469
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
470
|
+
return SyntaxError;
|
|
471
|
+
}
|
|
472
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
473
|
+
return ReferenceError;
|
|
474
|
+
}
|
|
475
|
+
return Error;
|
|
476
|
+
};
|
|
477
|
+
const constructError = (message, type, name) => {
|
|
478
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
479
|
+
if (ErrorConstructor === DOMException && name) {
|
|
480
|
+
return new ErrorConstructor(message, name);
|
|
481
|
+
}
|
|
482
|
+
if (ErrorConstructor === Error) {
|
|
483
|
+
const error = new Error(message);
|
|
484
|
+
if (name && name !== 'VError') {
|
|
485
|
+
error.name = name;
|
|
486
|
+
}
|
|
487
|
+
return error;
|
|
488
|
+
}
|
|
489
|
+
return new ErrorConstructor(message);
|
|
490
|
+
};
|
|
491
|
+
const joinLines = lines => {
|
|
492
|
+
return lines.join(NewLine);
|
|
493
|
+
};
|
|
494
|
+
const splitLines = lines => {
|
|
495
|
+
return lines.split(NewLine);
|
|
496
|
+
};
|
|
497
|
+
const getCurrentStack = () => {
|
|
498
|
+
const stackLinesToSkip = 3;
|
|
499
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
500
|
+
return currentStack;
|
|
501
|
+
};
|
|
502
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
503
|
+
return string.indexOf(NewLine, startIndex);
|
|
504
|
+
};
|
|
505
|
+
const getParentStack = error => {
|
|
506
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
507
|
+
if (parentStack.startsWith(' at')) {
|
|
508
|
+
parentStack = error.message + NewLine + parentStack;
|
|
509
|
+
}
|
|
510
|
+
return parentStack;
|
|
511
|
+
};
|
|
512
|
+
const MethodNotFound = -32601;
|
|
513
|
+
const Custom = -32001;
|
|
514
|
+
const restoreJsonRpcError = error => {
|
|
515
|
+
const currentStack = getCurrentStack();
|
|
516
|
+
if (error && error instanceof Error) {
|
|
517
|
+
if (typeof error.stack === 'string') {
|
|
518
|
+
error.stack = error.stack + NewLine + currentStack;
|
|
519
|
+
}
|
|
520
|
+
return error;
|
|
521
|
+
}
|
|
522
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
523
|
+
const restoredError = new JsonRpcError(error.message);
|
|
524
|
+
const parentStack = getParentStack(error);
|
|
525
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
526
|
+
return restoredError;
|
|
527
|
+
}
|
|
528
|
+
if (error && error.message) {
|
|
529
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
530
|
+
if (error.data) {
|
|
531
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
532
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
533
|
+
} else if (error.data.stack) {
|
|
534
|
+
restoredError.stack = error.data.stack;
|
|
535
|
+
}
|
|
536
|
+
if (error.data.codeFrame) {
|
|
537
|
+
// @ts-ignore
|
|
538
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
539
|
+
}
|
|
540
|
+
if (error.data.code) {
|
|
541
|
+
// @ts-ignore
|
|
542
|
+
restoredError.code = error.data.code;
|
|
543
|
+
}
|
|
544
|
+
if (error.data.type) {
|
|
545
|
+
// @ts-ignore
|
|
546
|
+
restoredError.name = error.data.type;
|
|
547
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
if (error.stack) {
|
|
550
|
+
const lowerStack = restoredError.stack || '';
|
|
551
|
+
// @ts-ignore
|
|
552
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
553
|
+
const parentStack = getParentStack(error);
|
|
554
|
+
// @ts-ignore
|
|
555
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
556
|
+
}
|
|
557
|
+
if (error.codeFrame) {
|
|
558
|
+
// @ts-ignore
|
|
559
|
+
restoredError.codeFrame = error.codeFrame;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return restoredError;
|
|
563
|
+
}
|
|
564
|
+
if (typeof error === 'string') {
|
|
565
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
566
|
+
}
|
|
567
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
568
|
+
};
|
|
569
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
570
|
+
if ('error' in responseMessage) {
|
|
571
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
572
|
+
throw restoredError;
|
|
573
|
+
}
|
|
574
|
+
if ('result' in responseMessage) {
|
|
575
|
+
return responseMessage.result;
|
|
576
|
+
}
|
|
577
|
+
throw new JsonRpcError('unexpected response message');
|
|
578
|
+
};
|
|
579
|
+
const warn = (...args) => {
|
|
580
|
+
console.warn(...args);
|
|
581
|
+
};
|
|
582
|
+
const resolve = (id, response) => {
|
|
583
|
+
const fn = get$1(id);
|
|
584
|
+
if (!fn) {
|
|
585
|
+
console.log(response);
|
|
586
|
+
warn(`callback ${id} may already be disposed`);
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
fn(response);
|
|
590
|
+
remove$1(id);
|
|
591
|
+
};
|
|
592
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
593
|
+
const getErrorType = prettyError => {
|
|
594
|
+
if (prettyError && prettyError.type) {
|
|
595
|
+
return prettyError.type;
|
|
596
|
+
}
|
|
597
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
598
|
+
return prettyError.constructor.name;
|
|
599
|
+
}
|
|
600
|
+
return undefined;
|
|
601
|
+
};
|
|
602
|
+
const isAlreadyStack = line => {
|
|
603
|
+
return line.trim().startsWith('at ');
|
|
604
|
+
};
|
|
605
|
+
const getStack = prettyError => {
|
|
606
|
+
const stackString = prettyError.stack || '';
|
|
607
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
608
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
609
|
+
return stackString.slice(newLineIndex + 1);
|
|
610
|
+
}
|
|
611
|
+
return stackString;
|
|
612
|
+
};
|
|
613
|
+
const getErrorProperty = (error, prettyError) => {
|
|
614
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
27
615
|
return {
|
|
28
|
-
|
|
29
|
-
|
|
616
|
+
code: MethodNotFound,
|
|
617
|
+
data: error.stack,
|
|
618
|
+
message: error.message
|
|
30
619
|
};
|
|
31
620
|
}
|
|
621
|
+
return {
|
|
622
|
+
code: Custom,
|
|
623
|
+
data: {
|
|
624
|
+
code: prettyError.code,
|
|
625
|
+
codeFrame: prettyError.codeFrame,
|
|
626
|
+
name: prettyError.name,
|
|
627
|
+
stack: getStack(prettyError),
|
|
628
|
+
type: getErrorType(prettyError)
|
|
629
|
+
},
|
|
630
|
+
message: prettyError.message
|
|
631
|
+
};
|
|
632
|
+
};
|
|
633
|
+
const create$1$1 = (id, error) => {
|
|
634
|
+
return {
|
|
635
|
+
error,
|
|
636
|
+
id,
|
|
637
|
+
jsonrpc: Two$1
|
|
638
|
+
};
|
|
639
|
+
};
|
|
640
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
641
|
+
const prettyError = preparePrettyError(error);
|
|
642
|
+
logError(error, prettyError);
|
|
643
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
644
|
+
return create$1$1(id, errorProperty);
|
|
645
|
+
};
|
|
646
|
+
const create$6 = (message, result) => {
|
|
647
|
+
return {
|
|
648
|
+
id: message.id,
|
|
649
|
+
jsonrpc: Two$1,
|
|
650
|
+
result: result ?? null
|
|
651
|
+
};
|
|
652
|
+
};
|
|
653
|
+
const getSuccessResponse = (message, result) => {
|
|
654
|
+
const resultProperty = result ?? null;
|
|
655
|
+
return create$6(message, resultProperty);
|
|
656
|
+
};
|
|
657
|
+
const getErrorResponseSimple = (id, error) => {
|
|
658
|
+
return {
|
|
659
|
+
error: {
|
|
660
|
+
code: Custom,
|
|
661
|
+
data: error,
|
|
662
|
+
// @ts-ignore
|
|
663
|
+
message: error.message
|
|
664
|
+
},
|
|
665
|
+
id,
|
|
666
|
+
jsonrpc: Two$1
|
|
667
|
+
};
|
|
668
|
+
};
|
|
669
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
32
670
|
try {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
};
|
|
671
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
672
|
+
return getSuccessResponse(message, result);
|
|
673
|
+
} catch (error) {
|
|
674
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
675
|
+
return getErrorResponseSimple(message.id, error);
|
|
39
676
|
}
|
|
677
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
const defaultPreparePrettyError = error => {
|
|
681
|
+
return error;
|
|
682
|
+
};
|
|
683
|
+
const defaultLogError = () => {
|
|
684
|
+
// ignore
|
|
685
|
+
};
|
|
686
|
+
const defaultRequiresSocket = () => {
|
|
687
|
+
return false;
|
|
688
|
+
};
|
|
689
|
+
const defaultResolve = resolve;
|
|
690
|
+
|
|
691
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
692
|
+
const normalizeParams = args => {
|
|
693
|
+
if (args.length === 1) {
|
|
694
|
+
const options = args[0];
|
|
40
695
|
return {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
696
|
+
execute: options.execute,
|
|
697
|
+
ipc: options.ipc,
|
|
698
|
+
logError: options.logError || defaultLogError,
|
|
699
|
+
message: options.message,
|
|
700
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
701
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket,
|
|
702
|
+
resolve: options.resolve || defaultResolve
|
|
48
703
|
};
|
|
49
704
|
}
|
|
705
|
+
return {
|
|
706
|
+
execute: args[2],
|
|
707
|
+
ipc: args[0],
|
|
708
|
+
logError: args[5],
|
|
709
|
+
message: args[1],
|
|
710
|
+
preparePrettyError: args[4],
|
|
711
|
+
requiresSocket: args[6],
|
|
712
|
+
resolve: args[3]
|
|
713
|
+
};
|
|
50
714
|
};
|
|
715
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
716
|
+
const options = normalizeParams(args);
|
|
717
|
+
const {
|
|
718
|
+
execute,
|
|
719
|
+
ipc,
|
|
720
|
+
logError,
|
|
721
|
+
message,
|
|
722
|
+
preparePrettyError,
|
|
723
|
+
requiresSocket,
|
|
724
|
+
resolve
|
|
725
|
+
} = options;
|
|
726
|
+
if ('id' in message) {
|
|
727
|
+
if ('method' in message) {
|
|
728
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
729
|
+
try {
|
|
730
|
+
ipc.send(response);
|
|
731
|
+
} catch (error) {
|
|
732
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
733
|
+
ipc.send(errorResponse);
|
|
734
|
+
}
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
resolve(message.id, message);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if ('method' in message) {
|
|
741
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
throw new JsonRpcError('unexpected message');
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
const Two = '2.0';
|
|
51
748
|
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
749
|
+
const create$5 = (method, params) => {
|
|
750
|
+
return {
|
|
751
|
+
jsonrpc: Two,
|
|
752
|
+
method,
|
|
753
|
+
params
|
|
754
|
+
};
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
const create$4 = (id, method, params) => {
|
|
758
|
+
const message = {
|
|
759
|
+
id,
|
|
760
|
+
jsonrpc: Two,
|
|
761
|
+
method,
|
|
762
|
+
params
|
|
763
|
+
};
|
|
764
|
+
return message;
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
let id = 0;
|
|
768
|
+
const create$3 = () => {
|
|
769
|
+
return ++id;
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
const registerPromise = map => {
|
|
773
|
+
const id = create$3();
|
|
774
|
+
const {
|
|
775
|
+
promise,
|
|
776
|
+
resolve
|
|
777
|
+
} = Promise.withResolvers();
|
|
778
|
+
map[id] = resolve;
|
|
779
|
+
return {
|
|
780
|
+
id,
|
|
781
|
+
promise
|
|
782
|
+
};
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
786
|
+
const {
|
|
787
|
+
id,
|
|
788
|
+
promise
|
|
789
|
+
} = registerPromise(callbacks);
|
|
790
|
+
const message = create$4(id, method, params);
|
|
791
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
792
|
+
ipc.sendAndTransfer(message);
|
|
793
|
+
} else {
|
|
794
|
+
ipc.send(message);
|
|
795
|
+
}
|
|
796
|
+
const responseMessage = await promise;
|
|
797
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
798
|
+
};
|
|
799
|
+
const createRpc = ipc => {
|
|
800
|
+
const callbacks = Object.create(null);
|
|
801
|
+
ipc._resolve = (id, response) => {
|
|
802
|
+
const fn = callbacks[id];
|
|
803
|
+
if (!fn) {
|
|
804
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
fn(response);
|
|
808
|
+
delete callbacks[id];
|
|
809
|
+
};
|
|
810
|
+
const rpc = {
|
|
811
|
+
async dispose() {
|
|
812
|
+
await ipc?.dispose();
|
|
62
813
|
},
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
814
|
+
invoke(method, ...params) {
|
|
815
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
816
|
+
},
|
|
817
|
+
invokeAndTransfer(method, ...params) {
|
|
818
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
819
|
+
},
|
|
820
|
+
// @ts-ignore
|
|
821
|
+
ipc,
|
|
822
|
+
/**
|
|
823
|
+
* @deprecated
|
|
824
|
+
*/
|
|
825
|
+
send(method, ...params) {
|
|
826
|
+
const message = create$5(method, params);
|
|
827
|
+
ipc.send(message);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
return rpc;
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
const requiresSocket = () => {
|
|
834
|
+
return false;
|
|
835
|
+
};
|
|
836
|
+
const preparePrettyError = error => {
|
|
837
|
+
return error;
|
|
838
|
+
};
|
|
839
|
+
const logError = () => {
|
|
840
|
+
// handled by renderer worker
|
|
841
|
+
};
|
|
842
|
+
const handleMessage = event => {
|
|
843
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
844
|
+
const actualExecute = event?.target?.execute || execute;
|
|
845
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
const handleIpc = ipc => {
|
|
849
|
+
if ('addEventListener' in ipc) {
|
|
850
|
+
ipc.addEventListener('message', handleMessage);
|
|
851
|
+
} else if ('on' in ipc) {
|
|
852
|
+
// deprecated
|
|
853
|
+
ipc.on('message', handleMessage);
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const listen$1 = async (module, options) => {
|
|
858
|
+
const rawIpc = await module.listen(options);
|
|
859
|
+
if (module.signal) {
|
|
860
|
+
module.signal(rawIpc);
|
|
861
|
+
}
|
|
862
|
+
const ipc = module.wrap(rawIpc);
|
|
863
|
+
return ipc;
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
const create$2 = async ({
|
|
867
|
+
commandMap,
|
|
868
|
+
messagePort
|
|
869
|
+
}) => {
|
|
870
|
+
// TODO create a commandMap per rpc instance
|
|
871
|
+
register(commandMap);
|
|
872
|
+
const ipc = await listen$1(IpcChildWithMessagePort$1, {
|
|
873
|
+
port: messagePort
|
|
874
|
+
});
|
|
875
|
+
handleIpc(ipc);
|
|
876
|
+
const rpc = createRpc(ipc);
|
|
877
|
+
return rpc;
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const create$1 = async ({
|
|
881
|
+
commandMap
|
|
882
|
+
}) => {
|
|
883
|
+
// TODO create a commandMap per rpc instance
|
|
884
|
+
register(commandMap);
|
|
885
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
886
|
+
handleIpc(ipc);
|
|
887
|
+
const rpc = createRpc(ipc);
|
|
888
|
+
return rpc;
|
|
889
|
+
};
|
|
890
|
+
|
|
891
|
+
const createMockRpc = ({
|
|
892
|
+
commandMap
|
|
893
|
+
}) => {
|
|
894
|
+
const invocations = [];
|
|
895
|
+
const invoke = (method, ...params) => {
|
|
896
|
+
invocations.push([method, ...params]);
|
|
897
|
+
const command = commandMap[method];
|
|
898
|
+
if (!command) {
|
|
899
|
+
throw new Error(`command ${method} not found`);
|
|
900
|
+
}
|
|
901
|
+
return command(...params);
|
|
902
|
+
};
|
|
903
|
+
const mockRpc = {
|
|
904
|
+
invocations,
|
|
905
|
+
invoke,
|
|
906
|
+
invokeAndTransfer: invoke
|
|
907
|
+
};
|
|
908
|
+
return mockRpc;
|
|
909
|
+
};
|
|
910
|
+
|
|
911
|
+
const rpcs = Object.create(null);
|
|
912
|
+
const set = (id, rpc) => {
|
|
913
|
+
rpcs[id] = rpc;
|
|
914
|
+
};
|
|
915
|
+
const get = id => {
|
|
916
|
+
return rpcs[id];
|
|
917
|
+
};
|
|
918
|
+
const remove = id => {
|
|
919
|
+
delete rpcs[id];
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
923
|
+
const create = rpcId => {
|
|
924
|
+
return {
|
|
925
|
+
async dispose() {
|
|
926
|
+
const rpc = get(rpcId);
|
|
927
|
+
await rpc.dispose();
|
|
69
928
|
},
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
css: 'string',
|
|
76
|
-
html: 'string',
|
|
77
|
-
title: 'string'
|
|
929
|
+
// @ts-ignore
|
|
930
|
+
invoke(method, ...params) {
|
|
931
|
+
const rpc = get(rpcId);
|
|
932
|
+
// @ts-ignore
|
|
933
|
+
return rpc.invoke(method, ...params);
|
|
78
934
|
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
content: 'string',
|
|
85
|
-
path: 'string'
|
|
935
|
+
// @ts-ignore
|
|
936
|
+
invokeAndTransfer(method, ...params) {
|
|
937
|
+
const rpc = get(rpcId);
|
|
938
|
+
// @ts-ignore
|
|
939
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
86
940
|
},
|
|
87
|
-
|
|
941
|
+
registerMockRpc(commandMap) {
|
|
942
|
+
const mockRpc = createMockRpc({
|
|
943
|
+
commandMap
|
|
944
|
+
});
|
|
945
|
+
set(rpcId, mockRpc);
|
|
946
|
+
// @ts-ignore
|
|
947
|
+
mockRpc[Symbol.dispose] = () => {
|
|
948
|
+
remove(rpcId);
|
|
949
|
+
};
|
|
950
|
+
// @ts-ignore
|
|
951
|
+
return mockRpc;
|
|
952
|
+
},
|
|
953
|
+
set(rpc) {
|
|
954
|
+
set(rpcId, rpc);
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
const RendererWorker = 1;
|
|
960
|
+
|
|
961
|
+
const {
|
|
962
|
+
invoke} = create(RendererWorker);
|
|
963
|
+
const readFile = async uri => {
|
|
964
|
+
return invoke('FileSystem.readFile', uri);
|
|
965
|
+
};
|
|
966
|
+
const writeFile = async (uri, text) => {
|
|
967
|
+
await invoke('FileSystem.writeFile', uri, text);
|
|
968
|
+
};
|
|
969
|
+
const getWorkspacePath = () => {
|
|
970
|
+
return invoke('Workspace.getPath');
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
const getToolErrorPayload = error => {
|
|
974
|
+
const rawStack = error && typeof error === 'object' ? Reflect.get(error, 'stack') : undefined;
|
|
975
|
+
return {
|
|
976
|
+
error: String(error),
|
|
977
|
+
...(typeof rawStack === 'string' && rawStack.trim() ? {
|
|
978
|
+
errorStack: rawStack,
|
|
979
|
+
stack: rawStack
|
|
980
|
+
} : {})
|
|
981
|
+
};
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
const executeClosePreviewTool = async (_args, _options) => {
|
|
985
|
+
try {
|
|
986
|
+
await invoke('Preview.close');
|
|
987
|
+
return {
|
|
988
|
+
ok: true
|
|
989
|
+
};
|
|
990
|
+
} catch (error) {
|
|
991
|
+
return getToolErrorPayload(error);
|
|
88
992
|
}
|
|
89
993
|
};
|
|
90
994
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
995
|
+
const executeGetWorkspaceUriTool = async (_args, _options) => {
|
|
996
|
+
try {
|
|
997
|
+
const workspaceUri = await getWorkspacePath();
|
|
94
998
|
return {
|
|
95
|
-
|
|
96
|
-
type: 'error'
|
|
999
|
+
workspaceUri
|
|
97
1000
|
};
|
|
1001
|
+
} catch (error) {
|
|
1002
|
+
return getToolErrorPayload(error);
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
const ABSOLUTE_URI_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//;
|
|
1007
|
+
const isAbsoluteUri = value => {
|
|
1008
|
+
return ABSOLUTE_URI_REGEX.test(value);
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
const executeListFilesTool = async (args, _options) => {
|
|
1012
|
+
const uri = typeof args.uri === 'string' ? args.uri : '';
|
|
1013
|
+
if (!uri || !isAbsoluteUri(uri)) {
|
|
1014
|
+
return {
|
|
1015
|
+
error: 'Invalid argument: uri must be an absolute URI.'
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
try {
|
|
1019
|
+
const entries = await invoke('FileSystem.readDirWithFileTypes', uri);
|
|
1020
|
+
return {
|
|
1021
|
+
entries,
|
|
1022
|
+
uri
|
|
1023
|
+
};
|
|
1024
|
+
} catch (error) {
|
|
1025
|
+
return {
|
|
1026
|
+
...getToolErrorPayload(error),
|
|
1027
|
+
uri
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
const executeOpenPreviewTool = async (args, _options) => {
|
|
1033
|
+
const uri = typeof args.uri === 'string' ? args.uri : '';
|
|
1034
|
+
if (!uri || !isAbsoluteUri(uri)) {
|
|
1035
|
+
return {
|
|
1036
|
+
error: 'Invalid argument: uri must be an absolute URI.'
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
try {
|
|
1040
|
+
await invoke('Preview.open', uri);
|
|
1041
|
+
return {
|
|
1042
|
+
ok: true,
|
|
1043
|
+
uri
|
|
1044
|
+
};
|
|
1045
|
+
} catch (error) {
|
|
1046
|
+
return {
|
|
1047
|
+
...getToolErrorPayload(error),
|
|
1048
|
+
uri
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
const WINDOWS_ABSOLUTE_PATH_REGEX = /^[a-zA-Z]:[\\/]/;
|
|
1054
|
+
const PATH_SEGMENT_SPLIT_REGEX$1 = /[\\/]/;
|
|
1055
|
+
const isPathTraversalAttempt = path => {
|
|
1056
|
+
if (!path) {
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
if (path.startsWith('/') || path.startsWith('\\')) {
|
|
1060
|
+
return true;
|
|
1061
|
+
}
|
|
1062
|
+
if (path.startsWith('file://')) {
|
|
1063
|
+
return true;
|
|
98
1064
|
}
|
|
99
|
-
|
|
100
|
-
|
|
1065
|
+
if (WINDOWS_ABSOLUTE_PATH_REGEX.test(path)) {
|
|
1066
|
+
return true;
|
|
1067
|
+
}
|
|
1068
|
+
const segments = path.split(PATH_SEGMENT_SPLIT_REGEX$1);
|
|
1069
|
+
return segments.includes('..');
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
const PATH_SEGMENT_SPLIT_REGEX = /[\\/]/;
|
|
1073
|
+
const normalizeRelativePath = path => {
|
|
1074
|
+
const segments = path.split(PATH_SEGMENT_SPLIT_REGEX).filter(segment => segment && segment !== '.');
|
|
1075
|
+
if (segments.length === 0) {
|
|
1076
|
+
return '.';
|
|
1077
|
+
}
|
|
1078
|
+
return segments.join('/');
|
|
1079
|
+
};
|
|
1080
|
+
|
|
1081
|
+
const executeReadFileTool = async (args, _options) => {
|
|
1082
|
+
const uri = typeof args.uri === 'string' ? args.uri : '';
|
|
1083
|
+
if (uri) {
|
|
1084
|
+
if (!isAbsoluteUri(uri)) {
|
|
101
1085
|
return {
|
|
102
|
-
|
|
103
|
-
type: 'error'
|
|
1086
|
+
error: 'Invalid argument: uri must be an absolute URI.'
|
|
104
1087
|
};
|
|
105
1088
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const expectedType = schema.properties[key];
|
|
109
|
-
if (!expectedType) {
|
|
110
|
-
if (schema.additionalProperties) {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
1089
|
+
try {
|
|
1090
|
+
const content = await readFile(uri);
|
|
113
1091
|
return {
|
|
114
|
-
|
|
115
|
-
|
|
1092
|
+
content,
|
|
1093
|
+
uri
|
|
116
1094
|
};
|
|
117
|
-
}
|
|
118
|
-
if (typeof value !== expectedType) {
|
|
1095
|
+
} catch (error) {
|
|
119
1096
|
return {
|
|
120
|
-
|
|
121
|
-
|
|
1097
|
+
...getToolErrorPayload(error),
|
|
1098
|
+
uri
|
|
122
1099
|
};
|
|
123
1100
|
}
|
|
124
1101
|
}
|
|
1102
|
+
const filePath = typeof args.path === 'string' ? args.path : '';
|
|
1103
|
+
if (!filePath || isPathTraversalAttempt(filePath)) {
|
|
1104
|
+
return {
|
|
1105
|
+
error: 'Access denied: path must be relative and stay within the open workspace folder.'
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
const normalizedPath = normalizeRelativePath(filePath);
|
|
1109
|
+
try {
|
|
1110
|
+
const content = await readFile(normalizedPath);
|
|
1111
|
+
return {
|
|
1112
|
+
content,
|
|
1113
|
+
path: normalizedPath
|
|
1114
|
+
};
|
|
1115
|
+
} catch (error) {
|
|
1116
|
+
return {
|
|
1117
|
+
...getToolErrorPayload(error),
|
|
1118
|
+
path: normalizedPath
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
|
|
1123
|
+
const maxPayloadLength = 40_000;
|
|
1124
|
+
const executeRenderHtmlTool = async (args, _options) => {
|
|
1125
|
+
const html = typeof args.html === 'string' ? args.html : '';
|
|
1126
|
+
const css = typeof args.css === 'string' ? args.css : '';
|
|
1127
|
+
const title = typeof args.title === 'string' ? args.title : '';
|
|
1128
|
+
if (!html) {
|
|
1129
|
+
return {
|
|
1130
|
+
error: 'Missing required argument: html'
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
if (html.length > maxPayloadLength || css.length > maxPayloadLength) {
|
|
1134
|
+
return {
|
|
1135
|
+
error: 'Payload too large: keep html/css under 40,000 characters each.'
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
125
1138
|
return {
|
|
126
|
-
|
|
127
|
-
|
|
1139
|
+
css,
|
|
1140
|
+
html,
|
|
1141
|
+
ok: true,
|
|
1142
|
+
title
|
|
128
1143
|
};
|
|
129
1144
|
};
|
|
130
1145
|
|
|
131
|
-
const
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
1146
|
+
const executeWriteFileTool = async (args, _options) => {
|
|
1147
|
+
const uri = typeof args.uri === 'string' ? args.uri : '';
|
|
1148
|
+
const content = typeof args.content === 'string' ? args.content : '';
|
|
1149
|
+
if (!uri || !isAbsoluteUri(uri)) {
|
|
1150
|
+
return {
|
|
1151
|
+
error: 'Invalid argument: uri must be an absolute URI.'
|
|
1152
|
+
};
|
|
135
1153
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
1154
|
+
try {
|
|
1155
|
+
new URL(uri);
|
|
1156
|
+
} catch {
|
|
1157
|
+
return {
|
|
1158
|
+
error: 'Invalid argument: invalid URL.'
|
|
1159
|
+
};
|
|
139
1160
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
1161
|
+
try {
|
|
1162
|
+
await writeFile(uri, content);
|
|
1163
|
+
return {
|
|
1164
|
+
ok: true,
|
|
1165
|
+
uri
|
|
1166
|
+
};
|
|
1167
|
+
} catch (error) {
|
|
1168
|
+
return {
|
|
1169
|
+
...getToolErrorPayload(error),
|
|
1170
|
+
uri
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
|
|
1175
|
+
const parseToolArguments = rawArguments => {
|
|
1176
|
+
if (typeof rawArguments !== 'string') {
|
|
1177
|
+
return {};
|
|
1178
|
+
}
|
|
1179
|
+
try {
|
|
1180
|
+
const parsed = JSON.parse(rawArguments);
|
|
1181
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
1182
|
+
return {};
|
|
1183
|
+
}
|
|
1184
|
+
return parsed;
|
|
1185
|
+
} catch {
|
|
1186
|
+
return {};
|
|
1187
|
+
}
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
const executeChatTool = async (name, rawArguments, options) => {
|
|
1191
|
+
const args = parseToolArguments(rawArguments);
|
|
1192
|
+
if (name === 'read_file') {
|
|
1193
|
+
return executeReadFileTool(args);
|
|
1194
|
+
}
|
|
1195
|
+
if (name === 'write_file') {
|
|
1196
|
+
return executeWriteFileTool(args);
|
|
1197
|
+
}
|
|
1198
|
+
if (name === 'list_files') {
|
|
1199
|
+
return executeListFilesTool(args);
|
|
1200
|
+
}
|
|
1201
|
+
if (name === 'getWorkspaceUri') {
|
|
1202
|
+
return executeGetWorkspaceUriTool();
|
|
1203
|
+
}
|
|
1204
|
+
if (name === 'render_html') {
|
|
1205
|
+
return executeRenderHtmlTool(args);
|
|
1206
|
+
}
|
|
1207
|
+
if (name === 'open_preview') {
|
|
1208
|
+
return executeOpenPreviewTool(args);
|
|
1209
|
+
}
|
|
1210
|
+
if (name === 'close_preview') {
|
|
1211
|
+
return executeClosePreviewTool();
|
|
1212
|
+
}
|
|
1213
|
+
return {
|
|
1214
|
+
error: `Unknown tool: ${name}`
|
|
1215
|
+
};
|
|
144
1216
|
};
|
|
145
1217
|
|
|
146
|
-
const
|
|
147
|
-
|
|
1218
|
+
const getReadFileTool = () => {
|
|
1219
|
+
return {
|
|
1220
|
+
function: {
|
|
1221
|
+
description: 'Read UTF-8 text content from a file inside the currently open workspace folder. Only pass an absolute URI.',
|
|
1222
|
+
name: 'read_file',
|
|
1223
|
+
parameters: {
|
|
1224
|
+
additionalProperties: false,
|
|
1225
|
+
properties: {
|
|
1226
|
+
uri: {
|
|
1227
|
+
description: 'Absolute file URI within the workspace (for example: file:///workspace/src/index.ts).',
|
|
1228
|
+
type: 'string'
|
|
1229
|
+
}
|
|
1230
|
+
},
|
|
1231
|
+
required: ['uri'],
|
|
1232
|
+
type: 'object'
|
|
1233
|
+
}
|
|
1234
|
+
},
|
|
1235
|
+
type: 'function'
|
|
1236
|
+
};
|
|
1237
|
+
};
|
|
1238
|
+
const getWriteFileTool = () => {
|
|
1239
|
+
return {
|
|
1240
|
+
function: {
|
|
1241
|
+
description: 'Write UTF-8 text content to a file inside the currently open workspace folder. Only pass an absolute URI.',
|
|
1242
|
+
name: 'write_file',
|
|
1243
|
+
parameters: {
|
|
1244
|
+
additionalProperties: false,
|
|
1245
|
+
properties: {
|
|
1246
|
+
content: {
|
|
1247
|
+
description: 'New UTF-8 text content to write to the file.',
|
|
1248
|
+
type: 'string'
|
|
1249
|
+
},
|
|
1250
|
+
uri: {
|
|
1251
|
+
description: 'Absolute file URI within the workspace (for example: file:///workspace/src/index.ts).',
|
|
1252
|
+
type: 'string'
|
|
1253
|
+
}
|
|
1254
|
+
},
|
|
1255
|
+
required: ['uri', 'content'],
|
|
1256
|
+
type: 'object'
|
|
1257
|
+
}
|
|
1258
|
+
},
|
|
1259
|
+
type: 'function'
|
|
1260
|
+
};
|
|
1261
|
+
};
|
|
1262
|
+
const getListFilesTool = () => {
|
|
1263
|
+
return {
|
|
1264
|
+
function: {
|
|
1265
|
+
description: 'List direct children (files and folders) for a folder URI inside the currently open workspace folder. Only pass an absolute URI.',
|
|
1266
|
+
name: 'list_files',
|
|
1267
|
+
parameters: {
|
|
1268
|
+
additionalProperties: false,
|
|
1269
|
+
properties: {
|
|
1270
|
+
uri: {
|
|
1271
|
+
description: 'Absolute folder URI within the workspace (for example: file:///workspace/src).',
|
|
1272
|
+
type: 'string'
|
|
1273
|
+
}
|
|
1274
|
+
},
|
|
1275
|
+
required: ['uri'],
|
|
1276
|
+
type: 'object'
|
|
1277
|
+
}
|
|
1278
|
+
},
|
|
1279
|
+
type: 'function'
|
|
1280
|
+
};
|
|
1281
|
+
};
|
|
1282
|
+
const getGetWorkspaceUriTool = () => {
|
|
1283
|
+
return {
|
|
1284
|
+
function: {
|
|
1285
|
+
description: 'Get the URI of the currently open workspace folder.',
|
|
1286
|
+
name: 'getWorkspaceUri',
|
|
1287
|
+
parameters: {
|
|
1288
|
+
additionalProperties: false,
|
|
1289
|
+
properties: {},
|
|
1290
|
+
type: 'object'
|
|
1291
|
+
}
|
|
1292
|
+
},
|
|
1293
|
+
type: 'function'
|
|
1294
|
+
};
|
|
1295
|
+
};
|
|
1296
|
+
const getRenderHtmlTool = () => {
|
|
1297
|
+
return {
|
|
1298
|
+
function: {
|
|
1299
|
+
description: 'Render custom HTML and optional CSS directly in the chat tool call list using native chat UI rendering. Use this for structured cards, tables, and small dashboards. After calling this tool, do not repeat the same HTML, data table, or long content again as plain text unless the user explicitly asks for a text-only version.',
|
|
1300
|
+
name: 'render_html',
|
|
1301
|
+
parameters: {
|
|
1302
|
+
additionalProperties: false,
|
|
1303
|
+
properties: {
|
|
1304
|
+
css: {
|
|
1305
|
+
description: 'Optional CSS string applied inside the preview document.',
|
|
1306
|
+
type: 'string'
|
|
1307
|
+
},
|
|
1308
|
+
html: {
|
|
1309
|
+
description: 'HTML string to render in the preview document.',
|
|
1310
|
+
type: 'string'
|
|
1311
|
+
},
|
|
1312
|
+
title: {
|
|
1313
|
+
description: 'Optional short title for the preview.',
|
|
1314
|
+
type: 'string'
|
|
1315
|
+
}
|
|
1316
|
+
},
|
|
1317
|
+
required: ['html'],
|
|
1318
|
+
type: 'object'
|
|
1319
|
+
}
|
|
1320
|
+
},
|
|
1321
|
+
type: 'function'
|
|
1322
|
+
};
|
|
1323
|
+
};
|
|
1324
|
+
const getOpenPreviewTool = () => {
|
|
1325
|
+
return {
|
|
1326
|
+
function: {
|
|
1327
|
+
description: 'Open the right-side preview for an HTML file URI inside the currently open workspace folder.',
|
|
1328
|
+
name: 'open_preview',
|
|
1329
|
+
parameters: {
|
|
1330
|
+
additionalProperties: false,
|
|
1331
|
+
properties: {
|
|
1332
|
+
uri: {
|
|
1333
|
+
description: 'Absolute HTML file URI within the workspace (for example: file:///workspace/index.html).',
|
|
1334
|
+
type: 'string'
|
|
1335
|
+
}
|
|
1336
|
+
},
|
|
1337
|
+
required: ['uri'],
|
|
1338
|
+
type: 'object'
|
|
1339
|
+
}
|
|
1340
|
+
},
|
|
1341
|
+
type: 'function'
|
|
1342
|
+
};
|
|
1343
|
+
};
|
|
1344
|
+
const getClosePreviewTool = () => {
|
|
1345
|
+
return {
|
|
1346
|
+
function: {
|
|
1347
|
+
description: 'Close the right-side preview if it is currently open.',
|
|
1348
|
+
name: 'close_preview',
|
|
1349
|
+
parameters: {
|
|
1350
|
+
additionalProperties: false,
|
|
1351
|
+
properties: {},
|
|
1352
|
+
type: 'object'
|
|
1353
|
+
}
|
|
1354
|
+
},
|
|
1355
|
+
type: 'function'
|
|
1356
|
+
};
|
|
1357
|
+
};
|
|
1358
|
+
const getBasicChatTools = () => {
|
|
1359
|
+
return [getReadFileTool(), getWriteFileTool(), getListFilesTool(), getGetWorkspaceUriTool(), getRenderHtmlTool(), getOpenPreviewTool(), getClosePreviewTool()];
|
|
148
1360
|
};
|
|
149
1361
|
|
|
150
1362
|
const handleMessagePort = async port => {
|
|
151
|
-
await
|
|
152
|
-
commandMap:
|
|
1363
|
+
await create$2({
|
|
1364
|
+
commandMap: commandMap,
|
|
153
1365
|
messagePort: port
|
|
154
1366
|
});
|
|
155
1367
|
};
|
|
156
1368
|
|
|
157
1369
|
const commandMap = {
|
|
158
|
-
|
|
1370
|
+
'ChatTool.execute': executeChatTool,
|
|
1371
|
+
'ChatTool.getTools': getBasicChatTools,
|
|
159
1372
|
'HandleMessagePort.handleMessagePort': handleMessagePort
|
|
160
1373
|
};
|
|
161
1374
|
|
|
162
1375
|
const listen = async () => {
|
|
163
|
-
await
|
|
1376
|
+
await create$1({
|
|
164
1377
|
commandMap: commandMap
|
|
165
1378
|
});
|
|
166
1379
|
};
|