@v-office/website-chat 1.0.1 → 1.1.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.
@@ -1,145 +0,0 @@
1
- import { n as CHAT_PATH } from "./protocol-BuNqZAMt.js";
2
- import { jsx } from "react/jsx-runtime";
3
- //#region src/client.ts
4
- const url = (endpoint) => `${endpoint.replace(/\/+$/, "")}${CHAT_PATH}`;
5
- /** See {@link ChatAvailable}. */
6
- const chatAvailable = async (endpoint, signal) => {
7
- if (!endpoint) return false;
8
- try {
9
- const res = await fetch(url(endpoint), signal ? { signal } : {});
10
- if (!res.ok) return false;
11
- return (await res.json()).available === true;
12
- } catch {
13
- return false;
14
- }
15
- };
16
- /** See {@link ParseEvents}. Only `data:` lines matter: the event's type travels in its JSON. */
17
- const parseEvents = (buffer) => {
18
- const blocks = buffer.split("\n\n");
19
- const rest = blocks.pop() ?? "";
20
- const events = [];
21
- for (const block of blocks) {
22
- const data = block.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
23
- if (!data) continue;
24
- try {
25
- events.push(JSON.parse(data));
26
- } catch {}
27
- }
28
- return {
29
- events,
30
- rest
31
- };
32
- };
33
- /** See {@link SendMessage}. */
34
- const sendMessage = async (endpoint, request, onEvent, signal) => {
35
- const res = await fetch(url(endpoint), {
36
- method: "POST",
37
- headers: {
38
- "content-type": "application/json",
39
- accept: "text/event-stream"
40
- },
41
- body: JSON.stringify(request),
42
- ...signal ? { signal } : {}
43
- });
44
- if (!res.ok || !res.body) {
45
- onEvent({
46
- type: "unavailable",
47
- reason: res.status === 429 ? "limit" : res.status === 409 ? "busy" : "error"
48
- });
49
- return;
50
- }
51
- const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
52
- let buffer = "";
53
- let ended = false;
54
- for (;;) {
55
- const { done, value } = await reader.read();
56
- if (done) break;
57
- const parsed = parseEvents(buffer + value);
58
- buffer = parsed.rest;
59
- for (const event of parsed.events) {
60
- if (event.type === "done" || event.type === "unavailable") ended = true;
61
- onEvent(event);
62
- }
63
- }
64
- if (!ended) onEvent({
65
- type: "unavailable",
66
- reason: "error"
67
- });
68
- };
69
- //#endregion
70
- //#region src/conversation.ts
71
- const keyOf = (endpoint) => `v-office-chat:${endpoint}`;
72
- /** sessionStorage throws where a browser forbids it (some private modes): the chat then lives in the
73
- * page only, as before. */
74
- const storage = () => {
75
- try {
76
- return globalThis.sessionStorage ?? null;
77
- } catch {
78
- return null;
79
- }
80
- };
81
- /** See {@link LoadConversation}. */
82
- const loadConversation = (endpoint, now = Date.now()) => {
83
- try {
84
- const raw = storage()?.getItem(keyOf(endpoint));
85
- if (!raw) return null;
86
- const stored = JSON.parse(raw);
87
- if (!Array.isArray(stored.messages) || stored.messages.length === 0) return null;
88
- if (!(now - stored.savedAt < 72e5)) {
89
- storage()?.removeItem(keyOf(endpoint));
90
- return null;
91
- }
92
- return {
93
- sessionId: stored.sessionId ?? null,
94
- messages: stored.messages,
95
- savedAt: stored.savedAt
96
- };
97
- } catch {
98
- return null;
99
- }
100
- };
101
- /** See {@link SaveConversation}. */
102
- const saveConversation = (endpoint, conversation, savedAt = Date.now()) => {
103
- try {
104
- storage()?.setItem(keyOf(endpoint), JSON.stringify({
105
- ...conversation,
106
- savedAt
107
- }));
108
- } catch {
109
- clearConversation(endpoint);
110
- }
111
- };
112
- /** See {@link ClearConversation}. */
113
- const clearConversation = (endpoint) => {
114
- try {
115
- storage()?.removeItem(keyOf(endpoint));
116
- } catch {}
117
- };
118
- //#endregion
119
- //#region src/icons.tsx
120
- const Svg = ({ children, size = 16 }) => /* @__PURE__ */ jsx("svg", {
121
- width: size,
122
- height: size,
123
- viewBox: "0 0 24 24",
124
- fill: "none",
125
- stroke: "currentColor",
126
- strokeWidth: 2.2,
127
- strokeLinecap: "round",
128
- strokeLinejoin: "round",
129
- "aria-hidden": "true",
130
- focusable: "false",
131
- children
132
- });
133
- /** The chat's mark, in the primary colour's circle. */
134
- const Spark = () => /* @__PURE__ */ jsx("span", {
135
- className: "spark",
136
- children: /* @__PURE__ */ jsx(Svg, {
137
- size: 18,
138
- children: /* @__PURE__ */ jsx("path", { d: "M12 3v4M12 17v4M3 12h4M17 12h4M6.3 6.3l2.5 2.5M15.2 15.2l2.5 2.5M6.3 17.7l2.5-2.5M15.2 8.8l2.5-2.5" })
139
- })
140
- });
141
- const Arrow = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M5 12h14M13 6l6 6-6 6" }) });
142
- const Restart = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M3 12a9 9 0 1 0 3-6.7L3 8M3 3v5h5" }) });
143
- const Cross = () => /* @__PURE__ */ jsx(Svg, { children: /* @__PURE__ */ jsx("path", { d: "M6 6l12 12M18 6L6 18" }) });
144
- //#endregion
145
- export { clearConversation as a, chatAvailable as c, Spark as i, sendMessage as l, Cross as n, loadConversation as o, Restart as r, saveConversation as s, Arrow as t };