@lvce-editor/chat-message-parsing-worker 1.2.0 → 1.4.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.
|
@@ -888,180 +888,9 @@ const create = async ({
|
|
|
888
888
|
return rpc;
|
|
889
889
|
};
|
|
890
890
|
|
|
891
|
-
const getFetchErrorMessage = error => {
|
|
892
|
-
if (error instanceof Error) {
|
|
893
|
-
return error.message;
|
|
894
|
-
}
|
|
895
|
-
return 'request failed';
|
|
896
|
-
};
|
|
897
|
-
|
|
898
|
-
const getFetchThrownErrorResult = error => {
|
|
899
|
-
return {
|
|
900
|
-
headers: {},
|
|
901
|
-
response: getFetchErrorMessage(error),
|
|
902
|
-
statusCode: 0,
|
|
903
|
-
type: 'error'
|
|
904
|
-
};
|
|
905
|
-
};
|
|
906
|
-
|
|
907
|
-
const getHeadersObject = headers => {
|
|
908
|
-
const result = {};
|
|
909
|
-
for (const [key, value] of headers) {
|
|
910
|
-
result[key] = value;
|
|
911
|
-
}
|
|
912
|
-
return result;
|
|
913
|
-
};
|
|
914
|
-
|
|
915
|
-
const getRequestInit = options => {
|
|
916
|
-
return {
|
|
917
|
-
...(options.postBody === undefined ? {} : {
|
|
918
|
-
body: JSON.stringify(options.postBody)
|
|
919
|
-
}),
|
|
920
|
-
...(options.headers ? {
|
|
921
|
-
headers: options.headers
|
|
922
|
-
} : {}),
|
|
923
|
-
method: options.method
|
|
924
|
-
};
|
|
925
|
-
};
|
|
926
|
-
|
|
927
|
-
const parseResponseJson = responseText => {
|
|
928
|
-
if (!responseText) {
|
|
929
|
-
return null;
|
|
930
|
-
}
|
|
931
|
-
return JSON.parse(responseText);
|
|
932
|
-
};
|
|
933
|
-
const makeApiRequest = async options => {
|
|
934
|
-
let response;
|
|
935
|
-
try {
|
|
936
|
-
response = await fetch(options.url, getRequestInit(options));
|
|
937
|
-
} catch (error) {
|
|
938
|
-
return getFetchThrownErrorResult(error);
|
|
939
|
-
}
|
|
940
|
-
const headers = getHeadersObject(response.headers);
|
|
941
|
-
const responseText = await response.text();
|
|
942
|
-
if (!response.ok) {
|
|
943
|
-
return {
|
|
944
|
-
headers,
|
|
945
|
-
response: responseText,
|
|
946
|
-
statusCode: response.status,
|
|
947
|
-
type: 'error'
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
let parsed;
|
|
951
|
-
try {
|
|
952
|
-
parsed = parseResponseJson(responseText);
|
|
953
|
-
} catch {
|
|
954
|
-
return {
|
|
955
|
-
headers,
|
|
956
|
-
response: responseText,
|
|
957
|
-
statusCode: response.status,
|
|
958
|
-
type: 'error'
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
return {
|
|
962
|
-
body: parsed,
|
|
963
|
-
headers,
|
|
964
|
-
statusCode: response.status,
|
|
965
|
-
type: 'success'
|
|
966
|
-
};
|
|
967
|
-
};
|
|
968
|
-
|
|
969
|
-
const splitSseEvents = chunk => {
|
|
970
|
-
return chunk.split(/\r?\n\r?\n/);
|
|
971
|
-
};
|
|
972
|
-
const getSseDataLines = event => {
|
|
973
|
-
const lines = event.split(/\r?\n/);
|
|
974
|
-
const dataLines = [];
|
|
975
|
-
for (const line of lines) {
|
|
976
|
-
if (!line.startsWith('data:')) {
|
|
977
|
-
continue;
|
|
978
|
-
}
|
|
979
|
-
dataLines.push(line.slice(5).trimStart());
|
|
980
|
-
}
|
|
981
|
-
return dataLines;
|
|
982
|
-
};
|
|
983
|
-
const parseSseData = text => {
|
|
984
|
-
if (text === '[DONE]') {
|
|
985
|
-
return text;
|
|
986
|
-
}
|
|
987
|
-
try {
|
|
988
|
-
return JSON.parse(text);
|
|
989
|
-
} catch {
|
|
990
|
-
return text;
|
|
991
|
-
}
|
|
992
|
-
};
|
|
993
|
-
const parseSseFromReader = async responseBody => {
|
|
994
|
-
if (!responseBody) {
|
|
995
|
-
return [];
|
|
996
|
-
}
|
|
997
|
-
const reader = responseBody.getReader();
|
|
998
|
-
const decoder = new TextDecoder();
|
|
999
|
-
let remainder = '';
|
|
1000
|
-
const events = [];
|
|
1001
|
-
let done = false;
|
|
1002
|
-
while (!done) {
|
|
1003
|
-
const {
|
|
1004
|
-
done: streamDone,
|
|
1005
|
-
value
|
|
1006
|
-
} = await reader.read();
|
|
1007
|
-
if (streamDone) {
|
|
1008
|
-
done = true;
|
|
1009
|
-
} else if (value) {
|
|
1010
|
-
remainder += decoder.decode(value, {
|
|
1011
|
-
stream: true
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
const chunks = splitSseEvents(remainder);
|
|
1015
|
-
remainder = chunks.pop() ?? '';
|
|
1016
|
-
for (const chunk of chunks) {
|
|
1017
|
-
const dataLines = getSseDataLines(chunk);
|
|
1018
|
-
for (const line of dataLines) {
|
|
1019
|
-
events.push(parseSseData(line));
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
if (remainder) {
|
|
1024
|
-
const dataLines = getSseDataLines(remainder);
|
|
1025
|
-
for (const line of dataLines) {
|
|
1026
|
-
events.push(parseSseData(line));
|
|
1027
|
-
}
|
|
1028
|
-
}
|
|
1029
|
-
return events;
|
|
1030
|
-
};
|
|
1031
|
-
const makeStreamingApiRequest = async options => {
|
|
1032
|
-
let response;
|
|
1033
|
-
try {
|
|
1034
|
-
response = await fetch(options.url, getRequestInit(options));
|
|
1035
|
-
} catch (error) {
|
|
1036
|
-
return getFetchThrownErrorResult(error);
|
|
1037
|
-
}
|
|
1038
|
-
const headers = getHeadersObject(response.headers);
|
|
1039
|
-
if (!response.ok) {
|
|
1040
|
-
const responseText = await response.text();
|
|
1041
|
-
return {
|
|
1042
|
-
headers,
|
|
1043
|
-
response: responseText,
|
|
1044
|
-
statusCode: response.status,
|
|
1045
|
-
type: 'error'
|
|
1046
|
-
};
|
|
1047
|
-
}
|
|
1048
|
-
const body = await parseSseFromReader(response.body);
|
|
1049
|
-
return {
|
|
1050
|
-
body,
|
|
1051
|
-
headers,
|
|
1052
|
-
statusCode: response.status,
|
|
1053
|
-
type: 'success'
|
|
1054
|
-
};
|
|
1055
|
-
};
|
|
1056
|
-
|
|
1057
|
-
const networkCommandMap = {
|
|
1058
|
-
'ChatNetwork.makeApiRequest': makeApiRequest,
|
|
1059
|
-
'ChatNetwork.makeStreamingApiRequest': makeStreamingApiRequest
|
|
1060
|
-
};
|
|
1061
|
-
|
|
1062
891
|
const handleMessagePort = async port => {
|
|
1063
892
|
await create$1({
|
|
1064
|
-
commandMap:
|
|
893
|
+
commandMap: commandMap,
|
|
1065
894
|
messagePort: port
|
|
1066
895
|
});
|
|
1067
896
|
};
|
|
@@ -2018,9 +1847,13 @@ const parseMessageContent = rawMessage => {
|
|
|
2018
1847
|
return parseBlockTokens(scanBlockTokens(rawMessage));
|
|
2019
1848
|
};
|
|
2020
1849
|
|
|
1850
|
+
const parseMessageContents = rawMessages => {
|
|
1851
|
+
return rawMessages.map(parseMessageContent);
|
|
1852
|
+
};
|
|
1853
|
+
|
|
2021
1854
|
const commandMap = {
|
|
2022
|
-
...networkCommandMap,
|
|
2023
1855
|
'ChatParser.parseMessageContent': parseMessageContent,
|
|
1856
|
+
'ChatParser.parseMessageContents': parseMessageContents,
|
|
2024
1857
|
'HandleMessagePort.handleMessagePort': handleMessagePort
|
|
2025
1858
|
};
|
|
2026
1859
|
|