@lvce-editor/chat-message-parsing-worker 1.5.0 → 1.7.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/dist/chatMessageParsingWorkerMain.js +277 -58
- package/package.json +1 -1
|
@@ -267,45 +267,6 @@ const readyMessage = 'ready';
|
|
|
267
267
|
const getData$2 = event => {
|
|
268
268
|
return event.data;
|
|
269
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
270
|
const listen$7 = () => {
|
|
310
271
|
// @ts-ignore
|
|
311
272
|
if (typeof WorkerGlobalScope === 'undefined') {
|
|
@@ -407,6 +368,100 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
|
407
368
|
listen: listen$6,
|
|
408
369
|
wrap: wrap$e
|
|
409
370
|
};
|
|
371
|
+
const addListener = (emitter, type, callback) => {
|
|
372
|
+
if ('addEventListener' in emitter) {
|
|
373
|
+
emitter.addEventListener(type, callback);
|
|
374
|
+
} else {
|
|
375
|
+
emitter.on(type, callback);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const removeListener = (emitter, type, callback) => {
|
|
379
|
+
if ('removeEventListener' in emitter) {
|
|
380
|
+
emitter.removeEventListener(type, callback);
|
|
381
|
+
} else {
|
|
382
|
+
emitter.off(type, callback);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
386
|
+
const {
|
|
387
|
+
promise,
|
|
388
|
+
resolve
|
|
389
|
+
} = Promise.withResolvers();
|
|
390
|
+
const listenerMap = Object.create(null);
|
|
391
|
+
const cleanup = value => {
|
|
392
|
+
for (const event of Object.keys(eventMap)) {
|
|
393
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
394
|
+
}
|
|
395
|
+
resolve(value);
|
|
396
|
+
};
|
|
397
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
398
|
+
const listener = event => {
|
|
399
|
+
cleanup({
|
|
400
|
+
event,
|
|
401
|
+
type
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
addListener(eventEmitter, event, listener);
|
|
405
|
+
listenerMap[event] = listener;
|
|
406
|
+
}
|
|
407
|
+
return promise;
|
|
408
|
+
};
|
|
409
|
+
const Message$1 = 3;
|
|
410
|
+
const create$5$1 = async ({
|
|
411
|
+
isMessagePortOpen,
|
|
412
|
+
messagePort
|
|
413
|
+
}) => {
|
|
414
|
+
if (!isMessagePort(messagePort)) {
|
|
415
|
+
throw new IpcError('port must be of type MessagePort');
|
|
416
|
+
}
|
|
417
|
+
if (isMessagePortOpen) {
|
|
418
|
+
return messagePort;
|
|
419
|
+
}
|
|
420
|
+
const eventPromise = getFirstEvent(messagePort, {
|
|
421
|
+
message: Message$1
|
|
422
|
+
});
|
|
423
|
+
messagePort.start();
|
|
424
|
+
const {
|
|
425
|
+
event,
|
|
426
|
+
type
|
|
427
|
+
} = await eventPromise;
|
|
428
|
+
if (type !== Message$1) {
|
|
429
|
+
throw new IpcError('Failed to wait for ipc message');
|
|
430
|
+
}
|
|
431
|
+
if (event.data !== readyMessage) {
|
|
432
|
+
throw new IpcError('unexpected first message');
|
|
433
|
+
}
|
|
434
|
+
return messagePort;
|
|
435
|
+
};
|
|
436
|
+
const signal$1 = messagePort => {
|
|
437
|
+
messagePort.start();
|
|
438
|
+
};
|
|
439
|
+
class IpcParentWithMessagePort extends Ipc {
|
|
440
|
+
getData = getData$2;
|
|
441
|
+
send(message) {
|
|
442
|
+
this._rawIpc.postMessage(message);
|
|
443
|
+
}
|
|
444
|
+
sendAndTransfer(message) {
|
|
445
|
+
const transfer = getTransferrables(message);
|
|
446
|
+
this._rawIpc.postMessage(message, transfer);
|
|
447
|
+
}
|
|
448
|
+
dispose() {
|
|
449
|
+
this._rawIpc.close();
|
|
450
|
+
}
|
|
451
|
+
onMessage(callback) {
|
|
452
|
+
this._rawIpc.addEventListener('message', callback);
|
|
453
|
+
}
|
|
454
|
+
onClose(callback) {}
|
|
455
|
+
}
|
|
456
|
+
const wrap$5 = messagePort => {
|
|
457
|
+
return new IpcParentWithMessagePort(messagePort);
|
|
458
|
+
};
|
|
459
|
+
const IpcParentWithMessagePort$1 = {
|
|
460
|
+
__proto__: null,
|
|
461
|
+
create: create$5$1,
|
|
462
|
+
signal: signal$1,
|
|
463
|
+
wrap: wrap$5
|
|
464
|
+
};
|
|
410
465
|
|
|
411
466
|
class CommandNotFoundError extends Error {
|
|
412
467
|
constructor(command) {
|
|
@@ -431,10 +486,10 @@ const execute = (command, ...args) => {
|
|
|
431
486
|
|
|
432
487
|
const Two$1 = '2.0';
|
|
433
488
|
const callbacks = Object.create(null);
|
|
434
|
-
const get = id => {
|
|
489
|
+
const get$1 = id => {
|
|
435
490
|
return callbacks[id];
|
|
436
491
|
};
|
|
437
|
-
const remove = id => {
|
|
492
|
+
const remove$1 = id => {
|
|
438
493
|
delete callbacks[id];
|
|
439
494
|
};
|
|
440
495
|
class JsonRpcError extends Error {
|
|
@@ -580,14 +635,14 @@ const warn = (...args) => {
|
|
|
580
635
|
console.warn(...args);
|
|
581
636
|
};
|
|
582
637
|
const resolve = (id, response) => {
|
|
583
|
-
const fn = get(id);
|
|
638
|
+
const fn = get$1(id);
|
|
584
639
|
if (!fn) {
|
|
585
640
|
console.log(response);
|
|
586
641
|
warn(`callback ${id} may already be disposed`);
|
|
587
642
|
return;
|
|
588
643
|
}
|
|
589
644
|
fn(response);
|
|
590
|
-
remove(id);
|
|
645
|
+
remove$1(id);
|
|
591
646
|
};
|
|
592
647
|
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
593
648
|
const getErrorType = prettyError => {
|
|
@@ -643,7 +698,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
|
643
698
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
644
699
|
return create$1$1(id, errorProperty);
|
|
645
700
|
};
|
|
646
|
-
const create$
|
|
701
|
+
const create$8 = (message, result) => {
|
|
647
702
|
return {
|
|
648
703
|
id: message.id,
|
|
649
704
|
jsonrpc: Two$1,
|
|
@@ -652,7 +707,7 @@ const create$5 = (message, result) => {
|
|
|
652
707
|
};
|
|
653
708
|
const getSuccessResponse = (message, result) => {
|
|
654
709
|
const resultProperty = result ?? null;
|
|
655
|
-
return create$
|
|
710
|
+
return create$8(message, resultProperty);
|
|
656
711
|
};
|
|
657
712
|
const getErrorResponseSimple = (id, error) => {
|
|
658
713
|
return {
|
|
@@ -746,7 +801,7 @@ const handleJsonRpcMessage = async (...args) => {
|
|
|
746
801
|
|
|
747
802
|
const Two = '2.0';
|
|
748
803
|
|
|
749
|
-
const create$
|
|
804
|
+
const create$7 = (method, params) => {
|
|
750
805
|
return {
|
|
751
806
|
jsonrpc: Two,
|
|
752
807
|
method,
|
|
@@ -754,7 +809,7 @@ const create$4 = (method, params) => {
|
|
|
754
809
|
};
|
|
755
810
|
};
|
|
756
811
|
|
|
757
|
-
const create$
|
|
812
|
+
const create$6 = (id, method, params) => {
|
|
758
813
|
const message = {
|
|
759
814
|
id,
|
|
760
815
|
jsonrpc: Two,
|
|
@@ -765,12 +820,12 @@ const create$3 = (id, method, params) => {
|
|
|
765
820
|
};
|
|
766
821
|
|
|
767
822
|
let id = 0;
|
|
768
|
-
const create$
|
|
823
|
+
const create$5 = () => {
|
|
769
824
|
return ++id;
|
|
770
825
|
};
|
|
771
826
|
|
|
772
827
|
const registerPromise = map => {
|
|
773
|
-
const id = create$
|
|
828
|
+
const id = create$5();
|
|
774
829
|
const {
|
|
775
830
|
promise,
|
|
776
831
|
resolve
|
|
@@ -787,7 +842,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
|
|
|
787
842
|
id,
|
|
788
843
|
promise
|
|
789
844
|
} = registerPromise(callbacks);
|
|
790
|
-
const message = create$
|
|
845
|
+
const message = create$6(id, method, params);
|
|
791
846
|
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
792
847
|
ipc.sendAndTransfer(message);
|
|
793
848
|
} else {
|
|
@@ -823,7 +878,7 @@ const createRpc = ipc => {
|
|
|
823
878
|
* @deprecated
|
|
824
879
|
*/
|
|
825
880
|
send(method, ...params) {
|
|
826
|
-
const message = create$
|
|
881
|
+
const message = create$7(method, params);
|
|
827
882
|
ipc.send(message);
|
|
828
883
|
}
|
|
829
884
|
};
|
|
@@ -863,21 +918,84 @@ const listen$1 = async (module, options) => {
|
|
|
863
918
|
return ipc;
|
|
864
919
|
};
|
|
865
920
|
|
|
866
|
-
const create$
|
|
921
|
+
const create$4 = async ({
|
|
867
922
|
commandMap,
|
|
923
|
+
isMessagePortOpen = true,
|
|
868
924
|
messagePort
|
|
869
925
|
}) => {
|
|
870
926
|
// TODO create a commandMap per rpc instance
|
|
871
927
|
register(commandMap);
|
|
872
|
-
const
|
|
873
|
-
|
|
928
|
+
const rawIpc = await IpcParentWithMessagePort$1.create({
|
|
929
|
+
isMessagePortOpen,
|
|
930
|
+
messagePort
|
|
874
931
|
});
|
|
932
|
+
const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
|
|
875
933
|
handleIpc(ipc);
|
|
876
934
|
const rpc = createRpc(ipc);
|
|
935
|
+
messagePort.start();
|
|
877
936
|
return rpc;
|
|
878
937
|
};
|
|
879
938
|
|
|
880
|
-
const create = async ({
|
|
939
|
+
const create$3 = async ({
|
|
940
|
+
commandMap,
|
|
941
|
+
isMessagePortOpen,
|
|
942
|
+
send
|
|
943
|
+
}) => {
|
|
944
|
+
const {
|
|
945
|
+
port1,
|
|
946
|
+
port2
|
|
947
|
+
} = new MessageChannel();
|
|
948
|
+
await send(port1);
|
|
949
|
+
return create$4({
|
|
950
|
+
commandMap,
|
|
951
|
+
isMessagePortOpen,
|
|
952
|
+
messagePort: port2
|
|
953
|
+
});
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
const createSharedLazyRpc = factory => {
|
|
957
|
+
let rpcPromise;
|
|
958
|
+
const getOrCreate = () => {
|
|
959
|
+
if (!rpcPromise) {
|
|
960
|
+
rpcPromise = factory();
|
|
961
|
+
}
|
|
962
|
+
return rpcPromise;
|
|
963
|
+
};
|
|
964
|
+
return {
|
|
965
|
+
async dispose() {
|
|
966
|
+
const rpc = await getOrCreate();
|
|
967
|
+
await rpc.dispose();
|
|
968
|
+
},
|
|
969
|
+
async invoke(method, ...params) {
|
|
970
|
+
const rpc = await getOrCreate();
|
|
971
|
+
return rpc.invoke(method, ...params);
|
|
972
|
+
},
|
|
973
|
+
async invokeAndTransfer(method, ...params) {
|
|
974
|
+
const rpc = await getOrCreate();
|
|
975
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
976
|
+
},
|
|
977
|
+
async send(method, ...params) {
|
|
978
|
+
const rpc = await getOrCreate();
|
|
979
|
+
rpc.send(method, ...params);
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
const create$2 = async ({
|
|
985
|
+
commandMap,
|
|
986
|
+
isMessagePortOpen,
|
|
987
|
+
send
|
|
988
|
+
}) => {
|
|
989
|
+
return createSharedLazyRpc(() => {
|
|
990
|
+
return create$3({
|
|
991
|
+
commandMap,
|
|
992
|
+
isMessagePortOpen,
|
|
993
|
+
send
|
|
994
|
+
});
|
|
995
|
+
});
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
const create$1 = async ({
|
|
881
999
|
commandMap
|
|
882
1000
|
}) => {
|
|
883
1001
|
// TODO create a commandMap per rpc instance
|
|
@@ -889,12 +1007,17 @@ const create = async ({
|
|
|
889
1007
|
};
|
|
890
1008
|
|
|
891
1009
|
const handleMessagePort = async port => {
|
|
892
|
-
await create$
|
|
1010
|
+
await create$4({
|
|
893
1011
|
commandMap: commandMap,
|
|
1012
|
+
isMessagePortOpen: true,
|
|
894
1013
|
messagePort: port
|
|
895
1014
|
});
|
|
896
1015
|
};
|
|
897
1016
|
|
|
1017
|
+
const initialize = async (_, port) => {
|
|
1018
|
+
await handleMessagePort(port);
|
|
1019
|
+
};
|
|
1020
|
+
|
|
898
1021
|
const windowsAbsolutePathRegex = /^[a-zA-Z]:[\\/]/;
|
|
899
1022
|
const pathSeparatorRegex$1 = /[\\/]/;
|
|
900
1023
|
const isPathTraversalAttempt = path => {
|
|
@@ -1852,15 +1975,111 @@ const parseMessageContents = rawMessages => {
|
|
|
1852
1975
|
};
|
|
1853
1976
|
|
|
1854
1977
|
const commandMap = {
|
|
1978
|
+
'ChatMessageParsing.parseMessageContent': parseMessageContent,
|
|
1979
|
+
'ChatMessageParsing.parseMessageContents': parseMessageContents,
|
|
1855
1980
|
'ChatParser.parseMessageContent': parseMessageContent,
|
|
1856
1981
|
'ChatParser.parseMessageContents': parseMessageContents,
|
|
1857
|
-
'HandleMessagePort.handleMessagePort': handleMessagePort
|
|
1982
|
+
'HandleMessagePort.handleMessagePort': handleMessagePort,
|
|
1983
|
+
initialize: initialize
|
|
1984
|
+
};
|
|
1985
|
+
|
|
1986
|
+
const createMockRpc = ({
|
|
1987
|
+
commandMap
|
|
1988
|
+
}) => {
|
|
1989
|
+
const invocations = [];
|
|
1990
|
+
const invoke = (method, ...params) => {
|
|
1991
|
+
invocations.push([method, ...params]);
|
|
1992
|
+
const command = commandMap[method];
|
|
1993
|
+
if (!command) {
|
|
1994
|
+
throw new Error(`command ${method} not found`);
|
|
1995
|
+
}
|
|
1996
|
+
return command(...params);
|
|
1997
|
+
};
|
|
1998
|
+
const mockRpc = {
|
|
1999
|
+
invocations,
|
|
2000
|
+
invoke,
|
|
2001
|
+
invokeAndTransfer: invoke
|
|
2002
|
+
};
|
|
2003
|
+
return mockRpc;
|
|
2004
|
+
};
|
|
2005
|
+
|
|
2006
|
+
const rpcs = Object.create(null);
|
|
2007
|
+
const set$1 = (id, rpc) => {
|
|
2008
|
+
rpcs[id] = rpc;
|
|
2009
|
+
};
|
|
2010
|
+
const get = id => {
|
|
2011
|
+
return rpcs[id];
|
|
2012
|
+
};
|
|
2013
|
+
const remove = id => {
|
|
2014
|
+
delete rpcs[id];
|
|
2015
|
+
};
|
|
2016
|
+
|
|
2017
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
2018
|
+
const create = rpcId => {
|
|
2019
|
+
return {
|
|
2020
|
+
async dispose() {
|
|
2021
|
+
const rpc = get(rpcId);
|
|
2022
|
+
await rpc.dispose();
|
|
2023
|
+
},
|
|
2024
|
+
// @ts-ignore
|
|
2025
|
+
invoke(method, ...params) {
|
|
2026
|
+
const rpc = get(rpcId);
|
|
2027
|
+
// @ts-ignore
|
|
2028
|
+
return rpc.invoke(method, ...params);
|
|
2029
|
+
},
|
|
2030
|
+
// @ts-ignore
|
|
2031
|
+
invokeAndTransfer(method, ...params) {
|
|
2032
|
+
const rpc = get(rpcId);
|
|
2033
|
+
// @ts-ignore
|
|
2034
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
2035
|
+
},
|
|
2036
|
+
registerMockRpc(commandMap) {
|
|
2037
|
+
const mockRpc = createMockRpc({
|
|
2038
|
+
commandMap
|
|
2039
|
+
});
|
|
2040
|
+
set$1(rpcId, mockRpc);
|
|
2041
|
+
// @ts-ignore
|
|
2042
|
+
mockRpc[Symbol.dispose] = () => {
|
|
2043
|
+
remove(rpcId);
|
|
2044
|
+
};
|
|
2045
|
+
// @ts-ignore
|
|
2046
|
+
return mockRpc;
|
|
2047
|
+
},
|
|
2048
|
+
set(rpc) {
|
|
2049
|
+
set$1(rpcId, rpc);
|
|
2050
|
+
}
|
|
2051
|
+
};
|
|
2052
|
+
};
|
|
2053
|
+
|
|
2054
|
+
const {
|
|
2055
|
+
set
|
|
2056
|
+
} = create(6007);
|
|
2057
|
+
|
|
2058
|
+
const RendererWorker = 1;
|
|
2059
|
+
|
|
2060
|
+
const {
|
|
2061
|
+
invokeAndTransfer} = create(RendererWorker);
|
|
2062
|
+
const sendMessagePortToChatMathWorker$1 = async (port, rpcId) => {
|
|
2063
|
+
const command = 'HandleMessagePort.handleMessagePort';
|
|
2064
|
+
await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToChatMathWorker', port, command, rpcId);
|
|
2065
|
+
};
|
|
2066
|
+
|
|
2067
|
+
const sendMessagePortToChatMathWorker = async port => {
|
|
2068
|
+
await sendMessagePortToChatMathWorker$1(port, 0);
|
|
2069
|
+
};
|
|
2070
|
+
const initializeChatMathWorker = async () => {
|
|
2071
|
+
const rpc = await create$2({
|
|
2072
|
+
commandMap: {},
|
|
2073
|
+
send: sendMessagePortToChatMathWorker
|
|
2074
|
+
});
|
|
2075
|
+
set(rpc);
|
|
1858
2076
|
};
|
|
1859
2077
|
|
|
1860
2078
|
const listen = async () => {
|
|
1861
|
-
await create({
|
|
2079
|
+
await create$1({
|
|
1862
2080
|
commandMap: commandMap
|
|
1863
2081
|
});
|
|
2082
|
+
await initializeChatMathWorker();
|
|
1864
2083
|
};
|
|
1865
2084
|
|
|
1866
2085
|
const main = async () => {
|