ss-support-widget 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chat-bot-widget.js +444 -0
- package/index.html +16 -0
- package/package.json +29 -0
- package/readme.md +7 -0
- package/src/App.tsx +94 -0
- package/src/ChatWidget.tsx +395 -0
- package/src/MsgDelta.tsx +6 -0
- package/src/element.tsx +87 -0
- package/src/hooks.ts +12 -0
- package/src/service.ts +26 -0
- package/src/session-storage.ts +7 -0
- package/src/sse.ts +55 -0
- package/tsconfig.json +13 -0
- package/vite.config.ts +19 -0
package/src/sse.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { MsgDelta } from "./MsgDelta";
|
|
2
|
+
|
|
3
|
+
type StreamArgs = {
|
|
4
|
+
url: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
clientId?: string;
|
|
7
|
+
threadId?: string;
|
|
8
|
+
body: string;
|
|
9
|
+
onDelta: (delta: MsgDelta) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export async function streamChat(a: StreamArgs): Promise<void> {
|
|
13
|
+
const res = await fetch(a.url, {
|
|
14
|
+
method: "POST",
|
|
15
|
+
headers: {
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
...(a.token ? { Authorization: "Bearer " + a.token } : {}),
|
|
18
|
+
...(a.clientId ? { "X-Client-Id": a.clientId } : {}),
|
|
19
|
+
...(a.threadId ? { "X-Thread-Id": a.threadId } : {}),
|
|
20
|
+
},
|
|
21
|
+
body: JSON.stringify(a.body),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!res.ok) {
|
|
25
|
+
throw new Error("HTTP " + res.status);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!res.body) {
|
|
29
|
+
throw new Error("ReadableStream missing");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const reader = res.body.getReader();
|
|
33
|
+
const decoder = new TextDecoder("utf-8");
|
|
34
|
+
let buffer = "";
|
|
35
|
+
|
|
36
|
+
while (true) {
|
|
37
|
+
const { value, done } = await reader.read();
|
|
38
|
+
if (done) break;
|
|
39
|
+
|
|
40
|
+
buffer += decoder.decode(value, { stream: true });
|
|
41
|
+
|
|
42
|
+
let idx;
|
|
43
|
+
while ((idx = buffer.indexOf("\n")) !== -1) {
|
|
44
|
+
const line = buffer.slice(0, idx).trim();
|
|
45
|
+
buffer = buffer.slice(idx + 1);
|
|
46
|
+
|
|
47
|
+
if (!line) continue;
|
|
48
|
+
|
|
49
|
+
const obj = JSON.parse(line) as MsgDelta;
|
|
50
|
+
a.onDelta(obj);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// a.onDelta({ id: "", threadId: "", text: decoder.decode() });
|
|
55
|
+
}
|
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
import react from "@vitejs/plugin-react";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
plugins: [react()],
|
|
6
|
+
build: {
|
|
7
|
+
lib: {
|
|
8
|
+
entry: "src/element.tsx",
|
|
9
|
+
name: "ChatBotWidget",
|
|
10
|
+
formats: ["iife"],
|
|
11
|
+
fileName: () => "chat-bot-widget.js",
|
|
12
|
+
},
|
|
13
|
+
rollupOptions: {
|
|
14
|
+
output: {
|
|
15
|
+
inlineDynamicImports: true,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|