@websolutespa/bom-mixer-llm 0.0.1
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/CHANGELOG.md +7 -0
- package/README.md +11 -0
- package/dist/cjs/index.css +2264 -0
- package/dist/cjs/index.js +3566 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.css +2264 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/llm/components/Background/background.js +76 -0
- package/dist/esm/llm/components/Background/background.js.map +1 -0
- package/dist/esm/llm/components/BusyIndicator/busyindicator.js +23 -0
- package/dist/esm/llm/components/BusyIndicator/busyindicator.js.map +1 -0
- package/dist/esm/llm/components/Canvas/canvas.js +223 -0
- package/dist/esm/llm/components/Canvas/canvas.js.map +1 -0
- package/dist/esm/llm/components/Chunk/chunk.js +65 -0
- package/dist/esm/llm/components/Chunk/chunk.js.map +1 -0
- package/dist/esm/llm/components/Cta/cta.js +33 -0
- package/dist/esm/llm/components/Cta/cta.js.map +1 -0
- package/dist/esm/llm/components/Event/event.js +90 -0
- package/dist/esm/llm/components/Event/event.js.map +1 -0
- package/dist/esm/llm/components/EventGroup/eventgroup.js +61 -0
- package/dist/esm/llm/components/EventGroup/eventgroup.js.map +1 -0
- package/dist/esm/llm/components/FormRecap/FormRecap.js +240 -0
- package/dist/esm/llm/components/FormRecap/FormRecap.js.map +1 -0
- package/dist/esm/llm/components/FormRecapError/FormRecapError.js +13 -0
- package/dist/esm/llm/components/FormRecapError/FormRecapError.js.map +1 -0
- package/dist/esm/llm/components/FormRecapSuccess/FormRecapSuccess.js +13 -0
- package/dist/esm/llm/components/FormRecapSuccess/FormRecapSuccess.js.map +1 -0
- package/dist/esm/llm/components/FormRequest/FormRequest.js +34 -0
- package/dist/esm/llm/components/FormRequest/FormRequest.js.map +1 -0
- package/dist/esm/llm/components/Header/header.js +51 -0
- package/dist/esm/llm/components/Header/header.js.map +1 -0
- package/dist/esm/llm/components/Intro/intro.js +77 -0
- package/dist/esm/llm/components/Intro/intro.js.map +1 -0
- package/dist/esm/llm/components/Main/main.js +118 -0
- package/dist/esm/llm/components/Main/main.js.map +1 -0
- package/dist/esm/llm/components/Poi/poi.js +78 -0
- package/dist/esm/llm/components/Poi/poi.js.map +1 -0
- package/dist/esm/llm/components/PoiGroup/poigroup.js +14 -0
- package/dist/esm/llm/components/PoiGroup/poigroup.js.map +1 -0
- package/dist/esm/llm/components/Prompt/prompt.js +86 -0
- package/dist/esm/llm/components/Prompt/prompt.js.map +1 -0
- package/dist/esm/llm/components/Prompt/suggestions.js +45 -0
- package/dist/esm/llm/components/Prompt/suggestions.js.map +1 -0
- package/dist/esm/llm/components/QuickLinks/quicklinks.js +66 -0
- package/dist/esm/llm/components/QuickLinks/quicklinks.js.map +1 -0
- package/dist/esm/llm/components/Stream/stream.js +43 -0
- package/dist/esm/llm/components/Stream/stream.js.map +1 -0
- package/dist/esm/llm/components/Text/text.js +34 -0
- package/dist/esm/llm/components/Text/text.js.map +1 -0
- package/dist/esm/llm/components/Thread/thread.js +26 -0
- package/dist/esm/llm/components/Thread/thread.js.map +1 -0
- package/dist/esm/llm/components/Trigger/blob.js +122 -0
- package/dist/esm/llm/components/Trigger/blob.js.map +1 -0
- package/dist/esm/llm/components/Trigger/trigger.js +63 -0
- package/dist/esm/llm/components/Trigger/trigger.js.map +1 -0
- package/dist/esm/llm/components/Tripadvisor/tripadvisor.js +94 -0
- package/dist/esm/llm/components/Tripadvisor/tripadvisor.js.map +1 -0
- package/dist/esm/llm/hooks/useAutosizeTextArea.js +13 -0
- package/dist/esm/llm/hooks/useAutosizeTextArea.js.map +1 -0
- package/dist/esm/llm/llm.js +48 -0
- package/dist/esm/llm/llm.js.map +1 -0
- package/dist/esm/llm/utils.js +15 -0
- package/dist/esm/llm/utils.js.map +1 -0
- package/dist/esm/node_modules/tslib/tslib.es6.js +135 -0
- package/dist/esm/node_modules/tslib/tslib.es6.js.map +1 -0
- package/dist/esm/useLlm/emitter.js +32 -0
- package/dist/esm/useLlm/emitter.js.map +1 -0
- package/dist/esm/useLlm/message.service.js +308 -0
- package/dist/esm/useLlm/message.service.js.map +1 -0
- package/dist/esm/useLlm/mock.js +687 -0
- package/dist/esm/useLlm/mock.js.map +1 -0
- package/dist/esm/useLlm/speech.js +306 -0
- package/dist/esm/useLlm/speech.js.map +1 -0
- package/dist/esm/useLlm/useLlm.js +460 -0
- package/dist/esm/useLlm/useLlm.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/llm/components/Background/background.d.ts +34 -0
- package/dist/types/llm/components/Background/background.d.ts.map +1 -0
- package/dist/types/llm/components/BusyIndicator/busyindicator.d.ts +2 -0
- package/dist/types/llm/components/BusyIndicator/busyindicator.d.ts.map +1 -0
- package/dist/types/llm/components/Canvas/canvas.d.ts +12 -0
- package/dist/types/llm/components/Canvas/canvas.d.ts.map +1 -0
- package/dist/types/llm/components/Chunk/chunk.d.ts +9 -0
- package/dist/types/llm/components/Chunk/chunk.d.ts.map +1 -0
- package/dist/types/llm/components/Cta/cta.d.ts +10 -0
- package/dist/types/llm/components/Cta/cta.d.ts.map +1 -0
- package/dist/types/llm/components/Event/event.d.ts +3 -0
- package/dist/types/llm/components/Event/event.d.ts.map +1 -0
- package/dist/types/llm/components/EventGroup/eventgroup.d.ts +3 -0
- package/dist/types/llm/components/EventGroup/eventgroup.d.ts.map +1 -0
- package/dist/types/llm/components/FormRecap/FormRecap.d.ts +10 -0
- package/dist/types/llm/components/FormRecap/FormRecap.d.ts.map +1 -0
- package/dist/types/llm/components/FormRecapError/FormRecapError.d.ts +2 -0
- package/dist/types/llm/components/FormRecapError/FormRecapError.d.ts.map +1 -0
- package/dist/types/llm/components/FormRecapSuccess/FormRecapSuccess.d.ts +2 -0
- package/dist/types/llm/components/FormRecapSuccess/FormRecapSuccess.d.ts.map +1 -0
- package/dist/types/llm/components/FormRequest/FormRequest.d.ts +4 -0
- package/dist/types/llm/components/FormRequest/FormRequest.d.ts.map +1 -0
- package/dist/types/llm/components/Header/header.d.ts +4 -0
- package/dist/types/llm/components/Header/header.d.ts.map +1 -0
- package/dist/types/llm/components/Intro/intro.d.ts +2 -0
- package/dist/types/llm/components/Intro/intro.d.ts.map +1 -0
- package/dist/types/llm/components/Main/main.d.ts +2 -0
- package/dist/types/llm/components/Main/main.d.ts.map +1 -0
- package/dist/types/llm/components/Poi/poi.d.ts +3 -0
- package/dist/types/llm/components/Poi/poi.d.ts.map +1 -0
- package/dist/types/llm/components/PoiGroup/poigroup.d.ts +3 -0
- package/dist/types/llm/components/PoiGroup/poigroup.d.ts.map +1 -0
- package/dist/types/llm/components/Prompt/prompt.d.ts +2 -0
- package/dist/types/llm/components/Prompt/prompt.d.ts.map +1 -0
- package/dist/types/llm/components/Prompt/suggestions.d.ts +2 -0
- package/dist/types/llm/components/Prompt/suggestions.d.ts.map +1 -0
- package/dist/types/llm/components/QuickLinks/quicklinks.d.ts +3 -0
- package/dist/types/llm/components/QuickLinks/quicklinks.d.ts.map +1 -0
- package/dist/types/llm/components/Stream/stream.d.ts +4 -0
- package/dist/types/llm/components/Stream/stream.d.ts.map +1 -0
- package/dist/types/llm/components/Text/text.d.ts +7 -0
- package/dist/types/llm/components/Text/text.d.ts.map +1 -0
- package/dist/types/llm/components/Thread/thread.d.ts +4 -0
- package/dist/types/llm/components/Thread/thread.d.ts.map +1 -0
- package/dist/types/llm/components/Trigger/blob.d.ts +2 -0
- package/dist/types/llm/components/Trigger/blob.d.ts.map +1 -0
- package/dist/types/llm/components/Trigger/trigger.d.ts +2 -0
- package/dist/types/llm/components/Trigger/trigger.d.ts.map +1 -0
- package/dist/types/llm/components/Tripadvisor/tripadvisor.d.ts +4 -0
- package/dist/types/llm/components/Tripadvisor/tripadvisor.d.ts.map +1 -0
- package/dist/types/llm/hooks/useAutosizeTextArea.d.ts +3 -0
- package/dist/types/llm/hooks/useAutosizeTextArea.d.ts.map +1 -0
- package/dist/types/llm/llm.d.ts +5 -0
- package/dist/types/llm/llm.d.ts.map +1 -0
- package/dist/types/llm/utils.d.ts +7 -0
- package/dist/types/llm/utils.d.ts.map +1 -0
- package/dist/types/types.d.ts +163 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/useLlm/cors.d.ts +16 -0
- package/dist/types/useLlm/cors.d.ts.map +1 -0
- package/dist/types/useLlm/emitter.d.ts +8 -0
- package/dist/types/useLlm/emitter.d.ts.map +1 -0
- package/dist/types/useLlm/message.service.d.ts +24 -0
- package/dist/types/useLlm/message.service.d.ts.map +1 -0
- package/dist/types/useLlm/mock.d.ts +5 -0
- package/dist/types/useLlm/mock.d.ts.map +1 -0
- package/dist/types/useLlm/speech.d.ts +57 -0
- package/dist/types/useLlm/speech.d.ts.map +1 -0
- package/dist/types/useLlm/useLlm.d.ts +83 -0
- package/dist/types/useLlm/useLlm.d.ts.map +1 -0
- package/package.json +86 -0
- package/src/__tests/example.test.ts +8 -0
- package/src/index.ts +6 -0
- package/src/llm/components/Background/_style.scss +24 -0
- package/src/llm/components/Background/background.tsx +89 -0
- package/src/llm/components/BusyIndicator/_style.scss +73 -0
- package/src/llm/components/BusyIndicator/busyindicator.tsx +13 -0
- package/src/llm/components/Canvas/_style.scss +10 -0
- package/src/llm/components/Canvas/canvas.tsx +257 -0
- package/src/llm/components/Chunk/chunk.tsx +80 -0
- package/src/llm/components/Cta/_style.scss +72 -0
- package/src/llm/components/Cta/cta.tsx +33 -0
- package/src/llm/components/Event/_style.scss +144 -0
- package/src/llm/components/Event/event.tsx +70 -0
- package/src/llm/components/EventGroup/_style.scss +60 -0
- package/src/llm/components/EventGroup/eventgroup.tsx +59 -0
- package/src/llm/components/FormRecap/FormRecap.tsx +221 -0
- package/src/llm/components/FormRecap/_style.scss +101 -0
- package/src/llm/components/FormRecapError/FormRecapError.tsx +10 -0
- package/src/llm/components/FormRecapError/_style.scss +11 -0
- package/src/llm/components/FormRecapSuccess/FormRecapSuccess.tsx +10 -0
- package/src/llm/components/FormRecapSuccess/_style.scss +11 -0
- package/src/llm/components/FormRequest/FormRequest.tsx +21 -0
- package/src/llm/components/FormRequest/_style.scss +21 -0
- package/src/llm/components/Header/_style.scss +72 -0
- package/src/llm/components/Header/header.tsx +38 -0
- package/src/llm/components/Intro/_style.scss +36 -0
- package/src/llm/components/Intro/intro.tsx +65 -0
- package/src/llm/components/Main/_style.scss +75 -0
- package/src/llm/components/Main/main.tsx +125 -0
- package/src/llm/components/Poi/_style.scss +127 -0
- package/src/llm/components/Poi/poi.tsx +58 -0
- package/src/llm/components/PoiGroup/_style.scss +14 -0
- package/src/llm/components/PoiGroup/poigroup.tsx +15 -0
- package/src/llm/components/Prompt/_style.scss +132 -0
- package/src/llm/components/Prompt/prompt.tsx +73 -0
- package/src/llm/components/Prompt/suggestions.tsx +39 -0
- package/src/llm/components/QuickLinks/_style.scss +61 -0
- package/src/llm/components/QuickLinks/quicklinks.tsx +58 -0
- package/src/llm/components/Stream/stream.tsx +37 -0
- package/src/llm/components/Text/_style.scss +89 -0
- package/src/llm/components/Text/text.tsx +28 -0
- package/src/llm/components/Thread/_style.scss +73 -0
- package/src/llm/components/Thread/thread.tsx +27 -0
- package/src/llm/components/Trigger/_style.scss +104 -0
- package/src/llm/components/Trigger/blob.tsx +42 -0
- package/src/llm/components/Trigger/trigger.tsx +50 -0
- package/src/llm/components/Tripadvisor/_style.scss +113 -0
- package/src/llm/components/Tripadvisor/tripadvisor.tsx +72 -0
- package/src/llm/hooks/useAutosizeTextArea.tsx +15 -0
- package/src/llm/llm.tsx +48 -0
- package/src/llm/scss/_base.scss +20 -0
- package/src/llm/scss/_breakpoints.scss +8 -0
- package/src/llm/scss/_layout.scss +39 -0
- package/src/llm/scss/_mixin.scss +67 -0
- package/src/llm/scss/_reset.scss +104 -0
- package/src/llm/scss/_spacing.scss +17 -0
- package/src/llm/scss/_typography.scss +153 -0
- package/src/llm/scss/_vars.scss +23 -0
- package/src/llm/scss/style.scss +29 -0
- package/src/llm/utils.tsx +12 -0
- package/src/types.ts +215 -0
- package/src/useLlm/cors.ts +124 -0
- package/src/useLlm/emitter.ts +29 -0
- package/src/useLlm/message.service.ts +336 -0
- package/src/useLlm/mock.ts +801 -0
- package/src/useLlm/speech.ts +363 -0
- package/src/useLlm/useLlm.tsx +474 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { isObject } from '@websolutespa/bom-core';
|
|
2
|
+
import { LlmChunk, LlmChunkItem, LlmCredentials, LlmDecodedMessage, LlmMessage, StreamResponse } from '../types';
|
|
3
|
+
|
|
4
|
+
export type MessageServiceRequest = LlmCredentials & {
|
|
5
|
+
messages: LlmMessage[];
|
|
6
|
+
test?: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type MessageServiceResponse = {
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type MessageServiceOptions = {
|
|
13
|
+
url: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class MessageService {
|
|
17
|
+
|
|
18
|
+
options: MessageServiceOptions;
|
|
19
|
+
private decoder_ = new TextDecoder('utf-8');
|
|
20
|
+
|
|
21
|
+
private lastMessage_: {
|
|
22
|
+
aborter: AbortController;
|
|
23
|
+
onEnd?: (response: StreamResponse) => void;
|
|
24
|
+
decodedChunks: LlmChunk[];
|
|
25
|
+
reader?: ReadableStreamDefaultReader<Uint8Array>;
|
|
26
|
+
} | undefined;
|
|
27
|
+
|
|
28
|
+
constructor(options: MessageServiceOptions) {
|
|
29
|
+
this.options = options;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async sendMessage(
|
|
33
|
+
request: MessageServiceRequest,
|
|
34
|
+
onMessage?: (response: StreamResponse) => void,
|
|
35
|
+
onEnd?: (response: StreamResponse) => void,
|
|
36
|
+
onChunk?: (chunks: LlmChunk[]) => void
|
|
37
|
+
) {
|
|
38
|
+
this.abort();
|
|
39
|
+
// console.log('MessageServiceService.request', request);
|
|
40
|
+
const aborter = new AbortController();
|
|
41
|
+
const url = this.options.url;
|
|
42
|
+
const decodedChunks: LlmChunk[] = [];
|
|
43
|
+
this.lastMessage_ = {
|
|
44
|
+
aborter,
|
|
45
|
+
decodedChunks,
|
|
46
|
+
onEnd,
|
|
47
|
+
};
|
|
48
|
+
let bytes = 0;
|
|
49
|
+
const signal = aborter.signal;
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(url, {
|
|
52
|
+
method: 'POST',
|
|
53
|
+
body: JSON.stringify(request),
|
|
54
|
+
headers: {
|
|
55
|
+
'Content-Type': 'application/json',
|
|
56
|
+
},
|
|
57
|
+
signal,
|
|
58
|
+
});
|
|
59
|
+
if (response.body) {
|
|
60
|
+
const reader = response.body.getReader();
|
|
61
|
+
this.lastMessage_.reader = reader;
|
|
62
|
+
for await (const chunk of readChunks(reader)) {
|
|
63
|
+
if (!signal.aborted) {
|
|
64
|
+
bytes += chunk.length;
|
|
65
|
+
const chunks = this.bytesToChunks(chunk);
|
|
66
|
+
decodedChunks.push(...chunks);
|
|
67
|
+
const response = chunksToResponse(decodedChunks);
|
|
68
|
+
if (typeof onMessage === 'function') {
|
|
69
|
+
onMessage(response);
|
|
70
|
+
}
|
|
71
|
+
if (typeof onChunk === 'function') {
|
|
72
|
+
onChunk(chunks);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!signal.aborted) {
|
|
77
|
+
if (typeof onEnd === 'function') {
|
|
78
|
+
const response = chunksToResponse(decodedChunks);
|
|
79
|
+
onEnd(response);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof TypeError) {
|
|
85
|
+
console.error('TypeError: Browser may not support async iteration', error);
|
|
86
|
+
}
|
|
87
|
+
throw (error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
abort() {
|
|
92
|
+
const lastMessage = this.lastMessage_;
|
|
93
|
+
if (lastMessage && lastMessage.aborter && !lastMessage.aborter.signal.aborted) {
|
|
94
|
+
try {
|
|
95
|
+
if (lastMessage.reader) {
|
|
96
|
+
lastMessage.reader.cancel().catch((error) => {
|
|
97
|
+
console.log('MessageService.abort.reader.error', error);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
lastMessage.aborter.abort();
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.log('MessageService.abort.error', error);
|
|
103
|
+
}
|
|
104
|
+
if (typeof lastMessage.onEnd === 'function') {
|
|
105
|
+
const response = chunksToResponse(lastMessage.decodedChunks);
|
|
106
|
+
lastMessage.onEnd(response);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
bytesToChunks(byteArray: Uint8Array): LlmChunk[] {
|
|
112
|
+
let decodedChunks: LlmChunk[] = [];
|
|
113
|
+
let decodedText: string = '';
|
|
114
|
+
try {
|
|
115
|
+
decodedText = this.decoder_.decode(byteArray);
|
|
116
|
+
decodedChunks = textToChunks(decodedText);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.log('decodedChunk.error', error, decodedText);
|
|
119
|
+
}
|
|
120
|
+
// console.log(decodedChunks, decodedText);
|
|
121
|
+
return decodedChunks;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function readChunks<T>(reader: ReadableStreamDefaultReader<T>) {
|
|
127
|
+
return {
|
|
128
|
+
async*[Symbol.asyncIterator]() {
|
|
129
|
+
let readResult = await reader.read();
|
|
130
|
+
while (!readResult.done) {
|
|
131
|
+
yield readResult.value;
|
|
132
|
+
readResult = await reader.read();
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/*
|
|
139
|
+
async function* streamAsyncIterable<T>(stream: ReadableStream<T>) {
|
|
140
|
+
const reader = stream.getReader();
|
|
141
|
+
try {
|
|
142
|
+
while (true) {
|
|
143
|
+
const { done, value } = await reader.read();
|
|
144
|
+
if (done) return;
|
|
145
|
+
yield value;
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
reader.releaseLock();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
class Iterator {
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
(Iterator as any).prototype[Symbol.asyncIterator] = async function* () {
|
|
156
|
+
const reader = this.getReader();
|
|
157
|
+
try {
|
|
158
|
+
while (true) {
|
|
159
|
+
const { done, value } = await reader.read();
|
|
160
|
+
if (done) return;
|
|
161
|
+
yield value;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
reader.releaseLock();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
export function chunksToResponse(decodedChunks: LlmChunk[]): StreamResponse {
|
|
171
|
+
const response: StreamResponse = {
|
|
172
|
+
chunks: [],
|
|
173
|
+
threadId: '',
|
|
174
|
+
};
|
|
175
|
+
response.chunks = chunksToChunkItems(decodedChunks, response);
|
|
176
|
+
return response;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function textToChunks(decodedText: string): LlmChunk[] {
|
|
180
|
+
let decodedChunks: LlmChunk[] = [];
|
|
181
|
+
try {
|
|
182
|
+
// const replacedText = decodedText.replace(/(^\[)|(,$)|(\]$)/g, '');
|
|
183
|
+
let replacedText = decodedText;
|
|
184
|
+
// replacedText = replacedText.replace(/(^,")/, '",');
|
|
185
|
+
replacedText = replacedText.replace(/(,$)/, '');
|
|
186
|
+
// replacedText = replacedText.replace(/}\s{/g, '},{');
|
|
187
|
+
/*
|
|
188
|
+
replacedText = replacedText.split(',').map(x => {
|
|
189
|
+
return x.replace(/^"([^{]*)"$/m, (m, g) => {
|
|
190
|
+
return '"' + g.replace('"', '\\"') + '"';
|
|
191
|
+
});
|
|
192
|
+
}).join(',');
|
|
193
|
+
*/
|
|
194
|
+
// console.log('textToChunks.replacedText', `[${replacedText}]`);
|
|
195
|
+
const chunks: LlmChunk[] = JSON.parse(`[${replacedText}]`);
|
|
196
|
+
// console.log('chunks', chunks);
|
|
197
|
+
/*
|
|
198
|
+
decodedChunks = chunks.map(chunk => {
|
|
199
|
+
if (typeof chunk === 'string') {
|
|
200
|
+
chunk = chunk.replace(/\r\n/g, '<br>');
|
|
201
|
+
}
|
|
202
|
+
return chunk;
|
|
203
|
+
});
|
|
204
|
+
*/
|
|
205
|
+
decodedChunks = chunks;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
console.log('textToChunks.error', error, decodedText);
|
|
208
|
+
}
|
|
209
|
+
// console.log(decodedChunks, decodedText);
|
|
210
|
+
return decodedChunks;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function chunksToChunkItems(decodedChunks: LlmChunk[], response?: StreamResponse): LlmChunkItem[] {
|
|
214
|
+
const chunks: LlmChunkItem[] = [];
|
|
215
|
+
decodedChunks.forEach(x => {
|
|
216
|
+
const lastMessage = chunks[chunks.length - 1];
|
|
217
|
+
if (isObject(x)) {
|
|
218
|
+
switch (x.type) {
|
|
219
|
+
case 'log':
|
|
220
|
+
console.log('LlmMessageService.log', x);
|
|
221
|
+
break;
|
|
222
|
+
case 'error':
|
|
223
|
+
console.error('LlmMessageService.error', x);
|
|
224
|
+
break;
|
|
225
|
+
case 'info':
|
|
226
|
+
case 'end': {
|
|
227
|
+
const { type, ...rest } = x;
|
|
228
|
+
if (response) {
|
|
229
|
+
Object.assign(response, rest);
|
|
230
|
+
}
|
|
231
|
+
// response = { ...response, ...rest };
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
case 'eventItem':
|
|
235
|
+
if (lastMessage && lastMessage.type === 'eventGroup') {
|
|
236
|
+
lastMessage.items.push(x);
|
|
237
|
+
} else {
|
|
238
|
+
chunks.push({
|
|
239
|
+
type: 'eventGroup',
|
|
240
|
+
items: [x],
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
break;
|
|
244
|
+
case 'poiItem':
|
|
245
|
+
if (lastMessage && lastMessage.type === 'poiGroup') {
|
|
246
|
+
lastMessage.items.push(x);
|
|
247
|
+
} else {
|
|
248
|
+
chunks.push({
|
|
249
|
+
type: 'poiGroup',
|
|
250
|
+
items: [x],
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
break;
|
|
254
|
+
case 'tripadvisor':
|
|
255
|
+
case 'tripadvisorItem':
|
|
256
|
+
if (lastMessage && lastMessage.type === 'tripadvisorGroup') {
|
|
257
|
+
lastMessage.items.push({ ...x, type: 'tripadvisorItem' });
|
|
258
|
+
} else {
|
|
259
|
+
chunks.push({
|
|
260
|
+
type: 'tripadvisorGroup',
|
|
261
|
+
items: [{ ...x, type: 'tripadvisorItem' }],
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
break;
|
|
265
|
+
default:
|
|
266
|
+
chunks.push(x);
|
|
267
|
+
}
|
|
268
|
+
} else if (typeof x === 'string') {
|
|
269
|
+
// console.warn('chunksToChunkItems typeof string');
|
|
270
|
+
if (lastMessage && lastMessage.type === 'string' && !lastMessage.content.includes('\n')) {
|
|
271
|
+
if (x.includes('\n')) {
|
|
272
|
+
const contents = x.split('\n');
|
|
273
|
+
contents.forEach((content, i) => {
|
|
274
|
+
const lastChar = (contents.length > i + 1) ? '\n' : '';
|
|
275
|
+
if (i === 0) {
|
|
276
|
+
lastMessage.content += content + lastChar;
|
|
277
|
+
} else if (content.length > 0) {
|
|
278
|
+
chunks.push({
|
|
279
|
+
type: 'string',
|
|
280
|
+
content: content + lastChar,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
} else {
|
|
285
|
+
lastMessage.content += x;
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
chunks.push({
|
|
289
|
+
type: 'string',
|
|
290
|
+
content: x,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return chunks;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function decodeMessages(messages: LlmMessage[]): LlmDecodedMessage[] {
|
|
299
|
+
return messages.map(x => {
|
|
300
|
+
if (x.role === 'user') {
|
|
301
|
+
return ({
|
|
302
|
+
...x,
|
|
303
|
+
chunks: [{
|
|
304
|
+
type: 'string',
|
|
305
|
+
content: x.content,
|
|
306
|
+
}],
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
return ({
|
|
310
|
+
...x,
|
|
311
|
+
chunks: chunksToChunkItems(textToChunks(x.content)),
|
|
312
|
+
/*
|
|
313
|
+
chunks: chunksToChunkItems(textToChunks(
|
|
314
|
+
x.content
|
|
315
|
+
.split('\r\n')
|
|
316
|
+
.map(x => {
|
|
317
|
+
if (['{', '['].includes(x.substring(0, 1))) {
|
|
318
|
+
// console.log('obj', x);
|
|
319
|
+
return x;
|
|
320
|
+
} else {
|
|
321
|
+
// console.log('str', `"${x.replace(/\n/g, '\\n')}"`);
|
|
322
|
+
return `"${x.replace(/\n/g, '\\n')}"`;
|
|
323
|
+
}
|
|
324
|
+
})
|
|
325
|
+
.join(',')
|
|
326
|
+
)),
|
|
327
|
+
*/
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function shouldAddFormRequest(messages: LlmDecodedMessage[]): boolean {
|
|
333
|
+
const hasFormRequest = messages.find(x => x.chunks.find(x => x.type === 'formRequest') !== undefined);
|
|
334
|
+
const importantMessages = messages.filter(x => x.chunks.find(x => ['event', 'eventItem', 'eventGroup', 'poi', 'poiItem', 'poiGroup', 'tripadvisor', 'tripadvisorItem', 'tripadvisorGroup'].includes(x.type)) !== undefined);
|
|
335
|
+
return !hasFormRequest && importantMessages.length > 1;
|
|
336
|
+
}
|