@trossino/chat-widget 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +116 -2
- package/dist/index.cjs +429 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +402 -0
- package/dist/index.js.map +1 -0
- package/dist/server.cjs +249 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +45 -0
- package/dist/server.d.ts +45 -0
- package/dist/server.js +246 -0
- package/dist/server.js.map +1 -0
- package/dist/styles.css +29 -0
- package/package.json +90 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
interface ChatWidgetProps {
|
|
4
|
+
/** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */
|
|
5
|
+
endpoint: string;
|
|
6
|
+
/** Extra headers sent with every request. Never put provider API keys here: they would be public. */
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
/** Input placeholder. */
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
/** Panel title. */
|
|
11
|
+
title?: string;
|
|
12
|
+
/** Message shown when the conversation is empty. Display-only: not sent to the backend. */
|
|
13
|
+
welcomeMessage?: string;
|
|
14
|
+
/** Called on network / HTTP / stream errors. */
|
|
15
|
+
onError?: (error: Error) => void;
|
|
16
|
+
/** Open the panel on mount. */
|
|
17
|
+
defaultOpen?: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare function ChatWidget({ endpoint, headers, placeholder, title, welcomeMessage, onError, defaultOpen, }: ChatWidgetProps): react.JSX.Element;
|
|
20
|
+
|
|
21
|
+
type ChatRole = 'user' | 'assistant';
|
|
22
|
+
interface ChatMessage {
|
|
23
|
+
role: ChatRole;
|
|
24
|
+
content: string;
|
|
25
|
+
}
|
|
26
|
+
/** Body sent by the client to the endpoint. */
|
|
27
|
+
interface ChatRequestBody {
|
|
28
|
+
messages: ChatMessage[];
|
|
29
|
+
}
|
|
30
|
+
/** Non-streaming JSON response expected from the endpoint. */
|
|
31
|
+
interface ChatResponseBody {
|
|
32
|
+
reply: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* One SSE event payload (`data: <json>`) for streaming responses.
|
|
36
|
+
* The stream ends with `data: [DONE]`. Errors are sent as `event: error` + `data: {"error": "..."}`.
|
|
37
|
+
*/
|
|
38
|
+
interface ChatStreamChunk {
|
|
39
|
+
delta: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface UseChatOptions {
|
|
43
|
+
/** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */
|
|
44
|
+
endpoint: string;
|
|
45
|
+
/** Extra headers sent with every request (e.g. a session token). Never put provider API keys here. */
|
|
46
|
+
headers?: Record<string, string>;
|
|
47
|
+
/** Initial conversation history. */
|
|
48
|
+
initialMessages?: ChatMessage[];
|
|
49
|
+
/** Called on network / HTTP / stream errors. */
|
|
50
|
+
onError?: (error: Error) => void;
|
|
51
|
+
}
|
|
52
|
+
interface UseChatResult {
|
|
53
|
+
messages: ChatMessage[];
|
|
54
|
+
loading: boolean;
|
|
55
|
+
error: Error | null;
|
|
56
|
+
/** Sends a user message and appends the assistant reply. Resolves when the reply is complete. */
|
|
57
|
+
send: (message: string) => Promise<void>;
|
|
58
|
+
/** Aborts the in-flight request, if any. */
|
|
59
|
+
stop: () => void;
|
|
60
|
+
/** Clears the conversation and the error. */
|
|
61
|
+
reset: () => void;
|
|
62
|
+
}
|
|
63
|
+
declare function useChat({ endpoint, headers, initialMessages, onError }: UseChatOptions): UseChatResult;
|
|
64
|
+
|
|
65
|
+
export { type ChatMessage, type ChatRequestBody, type ChatResponseBody, type ChatRole, type ChatStreamChunk, ChatWidget, type ChatWidgetProps, type UseChatOptions, type UseChatResult, useChat };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
interface ChatWidgetProps {
|
|
4
|
+
/** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */
|
|
5
|
+
endpoint: string;
|
|
6
|
+
/** Extra headers sent with every request. Never put provider API keys here: they would be public. */
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
/** Input placeholder. */
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
/** Panel title. */
|
|
11
|
+
title?: string;
|
|
12
|
+
/** Message shown when the conversation is empty. Display-only: not sent to the backend. */
|
|
13
|
+
welcomeMessage?: string;
|
|
14
|
+
/** Called on network / HTTP / stream errors. */
|
|
15
|
+
onError?: (error: Error) => void;
|
|
16
|
+
/** Open the panel on mount. */
|
|
17
|
+
defaultOpen?: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare function ChatWidget({ endpoint, headers, placeholder, title, welcomeMessage, onError, defaultOpen, }: ChatWidgetProps): react.JSX.Element;
|
|
20
|
+
|
|
21
|
+
type ChatRole = 'user' | 'assistant';
|
|
22
|
+
interface ChatMessage {
|
|
23
|
+
role: ChatRole;
|
|
24
|
+
content: string;
|
|
25
|
+
}
|
|
26
|
+
/** Body sent by the client to the endpoint. */
|
|
27
|
+
interface ChatRequestBody {
|
|
28
|
+
messages: ChatMessage[];
|
|
29
|
+
}
|
|
30
|
+
/** Non-streaming JSON response expected from the endpoint. */
|
|
31
|
+
interface ChatResponseBody {
|
|
32
|
+
reply: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* One SSE event payload (`data: <json>`) for streaming responses.
|
|
36
|
+
* The stream ends with `data: [DONE]`. Errors are sent as `event: error` + `data: {"error": "..."}`.
|
|
37
|
+
*/
|
|
38
|
+
interface ChatStreamChunk {
|
|
39
|
+
delta: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface UseChatOptions {
|
|
43
|
+
/** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */
|
|
44
|
+
endpoint: string;
|
|
45
|
+
/** Extra headers sent with every request (e.g. a session token). Never put provider API keys here. */
|
|
46
|
+
headers?: Record<string, string>;
|
|
47
|
+
/** Initial conversation history. */
|
|
48
|
+
initialMessages?: ChatMessage[];
|
|
49
|
+
/** Called on network / HTTP / stream errors. */
|
|
50
|
+
onError?: (error: Error) => void;
|
|
51
|
+
}
|
|
52
|
+
interface UseChatResult {
|
|
53
|
+
messages: ChatMessage[];
|
|
54
|
+
loading: boolean;
|
|
55
|
+
error: Error | null;
|
|
56
|
+
/** Sends a user message and appends the assistant reply. Resolves when the reply is complete. */
|
|
57
|
+
send: (message: string) => Promise<void>;
|
|
58
|
+
/** Aborts the in-flight request, if any. */
|
|
59
|
+
stop: () => void;
|
|
60
|
+
/** Clears the conversation and the error. */
|
|
61
|
+
reset: () => void;
|
|
62
|
+
}
|
|
63
|
+
declare function useChat({ endpoint, headers, initialMessages, onError }: UseChatOptions): UseChatResult;
|
|
64
|
+
|
|
65
|
+
export { type ChatMessage, type ChatRequestBody, type ChatResponseBody, type ChatRole, type ChatStreamChunk, ChatWidget, type ChatWidgetProps, type UseChatOptions, type UseChatResult, useChat };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/client/ChatWidget.tsx
|
|
4
|
+
import { useEffect as useEffect2, useId, useRef as useRef2, useState as useState2 } from "react";
|
|
5
|
+
|
|
6
|
+
// src/client/useChat.ts
|
|
7
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
8
|
+
function useChat({ endpoint, headers, initialMessages, onError }) {
|
|
9
|
+
const [messages, setMessages] = useState(initialMessages ?? []);
|
|
10
|
+
const [loading, setLoading] = useState(false);
|
|
11
|
+
const [error, setError] = useState(null);
|
|
12
|
+
const messagesRef = useRef(messages);
|
|
13
|
+
messagesRef.current = messages;
|
|
14
|
+
const headersRef = useRef(headers);
|
|
15
|
+
headersRef.current = headers;
|
|
16
|
+
const onErrorRef = useRef(onError);
|
|
17
|
+
onErrorRef.current = onError;
|
|
18
|
+
const abortRef = useRef(null);
|
|
19
|
+
useEffect(() => () => abortRef.current?.abort(), []);
|
|
20
|
+
const stop = useCallback(() => {
|
|
21
|
+
abortRef.current?.abort();
|
|
22
|
+
abortRef.current = null;
|
|
23
|
+
setLoading(false);
|
|
24
|
+
}, []);
|
|
25
|
+
const reset = useCallback(() => {
|
|
26
|
+
stop();
|
|
27
|
+
setMessages([]);
|
|
28
|
+
setError(null);
|
|
29
|
+
}, [stop]);
|
|
30
|
+
const send = useCallback(
|
|
31
|
+
async (message) => {
|
|
32
|
+
const content = message.trim();
|
|
33
|
+
if (!content || abortRef.current) return;
|
|
34
|
+
const history = [...messagesRef.current.filter((m) => m.content.trim()), { role: "user", content }];
|
|
35
|
+
setMessages(history);
|
|
36
|
+
setLoading(true);
|
|
37
|
+
setError(null);
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
abortRef.current = controller;
|
|
40
|
+
const appendToReply = (text) => {
|
|
41
|
+
setMessages((prev) => {
|
|
42
|
+
const next = prev.slice();
|
|
43
|
+
const current = next[history.length];
|
|
44
|
+
next[history.length] = { role: "assistant", content: (current?.content ?? "") + text };
|
|
45
|
+
return next;
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
const res = await fetch(endpoint, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: { "Content-Type": "application/json", Accept: "application/json, text/event-stream", ...headersRef.current },
|
|
52
|
+
body: JSON.stringify({ messages: history }),
|
|
53
|
+
signal: controller.signal
|
|
54
|
+
});
|
|
55
|
+
if (!res.ok) throw new Error(await readErrorMessage(res));
|
|
56
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
57
|
+
if (contentType.includes("text/event-stream")) {
|
|
58
|
+
if (!res.body) throw new Error("Empty response stream");
|
|
59
|
+
await readSSE(res.body, appendToReply);
|
|
60
|
+
} else {
|
|
61
|
+
const data = await res.json();
|
|
62
|
+
const reply = data && typeof data === "object" ? data.reply : void 0;
|
|
63
|
+
if (typeof reply !== "string") throw new Error("Invalid response: expected { reply: string }");
|
|
64
|
+
if (reply) appendToReply(reply);
|
|
65
|
+
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (controller.signal.aborted) return;
|
|
68
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
69
|
+
setError(e);
|
|
70
|
+
onErrorRef.current?.(e);
|
|
71
|
+
} finally {
|
|
72
|
+
if (abortRef.current === controller) {
|
|
73
|
+
abortRef.current = null;
|
|
74
|
+
setLoading(false);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
[endpoint]
|
|
79
|
+
);
|
|
80
|
+
return { messages, loading, error, send, stop, reset };
|
|
81
|
+
}
|
|
82
|
+
async function readErrorMessage(res) {
|
|
83
|
+
try {
|
|
84
|
+
const data = await res.json();
|
|
85
|
+
const msg = data && typeof data === "object" ? data.error : void 0;
|
|
86
|
+
if (typeof msg === "string" && msg) return msg;
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
return `Request failed with status ${res.status}`;
|
|
90
|
+
}
|
|
91
|
+
async function readSSE(body, onDelta) {
|
|
92
|
+
const reader = body.getReader();
|
|
93
|
+
const decoder = new TextDecoder();
|
|
94
|
+
let buffer = "";
|
|
95
|
+
const handleEvent = (raw) => {
|
|
96
|
+
let event = "message";
|
|
97
|
+
const dataLines = [];
|
|
98
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
99
|
+
if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
100
|
+
else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, ""));
|
|
101
|
+
}
|
|
102
|
+
if (dataLines.length === 0) return false;
|
|
103
|
+
const data = dataLines.join("\n");
|
|
104
|
+
if (data === "[DONE]") return true;
|
|
105
|
+
let parsed = data;
|
|
106
|
+
try {
|
|
107
|
+
parsed = JSON.parse(data);
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
if (event === "error") {
|
|
111
|
+
const msg = parsed && typeof parsed === "object" ? parsed.error : parsed;
|
|
112
|
+
throw new Error(typeof msg === "string" && msg ? msg : "Stream error");
|
|
113
|
+
}
|
|
114
|
+
if (typeof parsed === "string") onDelta(parsed);
|
|
115
|
+
else if (parsed && typeof parsed === "object") {
|
|
116
|
+
const p = parsed;
|
|
117
|
+
const text = p.delta ?? p.text ?? p.content;
|
|
118
|
+
if (typeof text === "string") onDelta(text);
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
while (true) {
|
|
124
|
+
const { done, value } = await reader.read();
|
|
125
|
+
if (done) break;
|
|
126
|
+
buffer += decoder.decode(value, { stream: true });
|
|
127
|
+
const events = buffer.split(/\r?\n\r?\n/);
|
|
128
|
+
buffer = events.pop() ?? "";
|
|
129
|
+
for (const evt of events) {
|
|
130
|
+
if (handleEvent(evt)) return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
buffer += decoder.decode();
|
|
134
|
+
if (buffer.trim()) handleEvent(buffer);
|
|
135
|
+
} finally {
|
|
136
|
+
reader.cancel().catch(() => {
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/client/ChatWidget.tsx
|
|
142
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
143
|
+
var ACCENT = "#2563eb";
|
|
144
|
+
var FONT = 'system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
|
|
145
|
+
var styles = {
|
|
146
|
+
root: {
|
|
147
|
+
position: "fixed",
|
|
148
|
+
right: 20,
|
|
149
|
+
bottom: 20,
|
|
150
|
+
zIndex: 2147483e3,
|
|
151
|
+
fontFamily: FONT,
|
|
152
|
+
fontSize: 14,
|
|
153
|
+
lineHeight: 1.45,
|
|
154
|
+
color: "#111827",
|
|
155
|
+
boxSizing: "border-box"
|
|
156
|
+
},
|
|
157
|
+
toggle: {
|
|
158
|
+
width: 56,
|
|
159
|
+
height: 56,
|
|
160
|
+
borderRadius: "50%",
|
|
161
|
+
border: "none",
|
|
162
|
+
background: ACCENT,
|
|
163
|
+
color: "#fff",
|
|
164
|
+
cursor: "pointer",
|
|
165
|
+
boxShadow: "0 6px 20px rgba(0,0,0,0.2)",
|
|
166
|
+
display: "flex",
|
|
167
|
+
alignItems: "center",
|
|
168
|
+
justifyContent: "center",
|
|
169
|
+
marginLeft: "auto",
|
|
170
|
+
padding: 0
|
|
171
|
+
},
|
|
172
|
+
panel: {
|
|
173
|
+
width: "min(370px, calc(100vw - 40px))",
|
|
174
|
+
height: "min(540px, calc(100vh - 110px))",
|
|
175
|
+
marginBottom: 12,
|
|
176
|
+
background: "#fff",
|
|
177
|
+
borderRadius: 12,
|
|
178
|
+
boxShadow: "0 12px 40px rgba(0,0,0,0.18)",
|
|
179
|
+
border: "1px solid #e5e7eb",
|
|
180
|
+
display: "flex",
|
|
181
|
+
flexDirection: "column",
|
|
182
|
+
overflow: "hidden"
|
|
183
|
+
},
|
|
184
|
+
header: {
|
|
185
|
+
display: "flex",
|
|
186
|
+
alignItems: "center",
|
|
187
|
+
justifyContent: "space-between",
|
|
188
|
+
padding: "12px 14px",
|
|
189
|
+
background: ACCENT,
|
|
190
|
+
color: "#fff"
|
|
191
|
+
},
|
|
192
|
+
title: { margin: 0, fontSize: 15, fontWeight: 600 },
|
|
193
|
+
close: {
|
|
194
|
+
background: "transparent",
|
|
195
|
+
border: "none",
|
|
196
|
+
color: "#fff",
|
|
197
|
+
cursor: "pointer",
|
|
198
|
+
fontSize: 20,
|
|
199
|
+
lineHeight: 1,
|
|
200
|
+
padding: 4
|
|
201
|
+
},
|
|
202
|
+
list: {
|
|
203
|
+
flex: 1,
|
|
204
|
+
overflowY: "auto",
|
|
205
|
+
padding: 14,
|
|
206
|
+
display: "flex",
|
|
207
|
+
flexDirection: "column",
|
|
208
|
+
gap: 8,
|
|
209
|
+
background: "#f9fafb",
|
|
210
|
+
margin: 0,
|
|
211
|
+
listStyle: "none"
|
|
212
|
+
},
|
|
213
|
+
bubble: {
|
|
214
|
+
maxWidth: "85%",
|
|
215
|
+
padding: "8px 12px",
|
|
216
|
+
borderRadius: 12,
|
|
217
|
+
whiteSpace: "pre-wrap",
|
|
218
|
+
wordBreak: "break-word"
|
|
219
|
+
},
|
|
220
|
+
user: { alignSelf: "flex-end", background: ACCENT, color: "#fff", borderBottomRightRadius: 4 },
|
|
221
|
+
assistant: { alignSelf: "flex-start", background: "#fff", border: "1px solid #e5e7eb", borderBottomLeftRadius: 4 },
|
|
222
|
+
status: { alignSelf: "flex-start", color: "#6b7280", fontStyle: "italic", padding: "0 4px" },
|
|
223
|
+
error: {
|
|
224
|
+
margin: "0 14px 8px",
|
|
225
|
+
padding: "8px 10px",
|
|
226
|
+
borderRadius: 8,
|
|
227
|
+
background: "#fef2f2",
|
|
228
|
+
color: "#b91c1c",
|
|
229
|
+
fontSize: 13
|
|
230
|
+
},
|
|
231
|
+
form: { display: "flex", gap: 8, padding: 10, borderTop: "1px solid #e5e7eb", background: "#fff" },
|
|
232
|
+
input: {
|
|
233
|
+
flex: 1,
|
|
234
|
+
resize: "none",
|
|
235
|
+
border: "1px solid #d1d5db",
|
|
236
|
+
borderRadius: 8,
|
|
237
|
+
padding: "8px 10px",
|
|
238
|
+
font: "inherit",
|
|
239
|
+
color: "inherit",
|
|
240
|
+
background: "#fff",
|
|
241
|
+
minHeight: 38,
|
|
242
|
+
maxHeight: 120,
|
|
243
|
+
boxSizing: "border-box"
|
|
244
|
+
},
|
|
245
|
+
send: {
|
|
246
|
+
border: "none",
|
|
247
|
+
borderRadius: 8,
|
|
248
|
+
background: ACCENT,
|
|
249
|
+
color: "#fff",
|
|
250
|
+
padding: "0 14px",
|
|
251
|
+
font: "inherit",
|
|
252
|
+
fontWeight: 600,
|
|
253
|
+
cursor: "pointer"
|
|
254
|
+
},
|
|
255
|
+
srOnly: {
|
|
256
|
+
position: "absolute",
|
|
257
|
+
width: 1,
|
|
258
|
+
height: 1,
|
|
259
|
+
padding: 0,
|
|
260
|
+
margin: -1,
|
|
261
|
+
overflow: "hidden",
|
|
262
|
+
clip: "rect(0,0,0,0)",
|
|
263
|
+
whiteSpace: "nowrap",
|
|
264
|
+
border: 0
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
function ChatWidget({
|
|
268
|
+
endpoint,
|
|
269
|
+
headers,
|
|
270
|
+
placeholder = "Type a message\u2026",
|
|
271
|
+
title = "Chat",
|
|
272
|
+
welcomeMessage,
|
|
273
|
+
onError,
|
|
274
|
+
defaultOpen = false
|
|
275
|
+
}) {
|
|
276
|
+
const [open, setOpen] = useState2(defaultOpen);
|
|
277
|
+
const [input, setInput] = useState2("");
|
|
278
|
+
const { messages, loading, error, send } = useChat({ endpoint, headers, onError });
|
|
279
|
+
const id = useId();
|
|
280
|
+
const panelId = `${id}-panel`;
|
|
281
|
+
const titleId = `${id}-title`;
|
|
282
|
+
const inputId = `${id}-input`;
|
|
283
|
+
const toggleRef = useRef2(null);
|
|
284
|
+
const inputRef = useRef2(null);
|
|
285
|
+
const listRef = useRef2(null);
|
|
286
|
+
useEffect2(() => {
|
|
287
|
+
if (open) inputRef.current?.focus();
|
|
288
|
+
}, [open]);
|
|
289
|
+
useEffect2(() => {
|
|
290
|
+
const el = listRef.current;
|
|
291
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
292
|
+
}, [messages, loading, open]);
|
|
293
|
+
const close = () => {
|
|
294
|
+
setOpen(false);
|
|
295
|
+
toggleRef.current?.focus();
|
|
296
|
+
};
|
|
297
|
+
const submit = (e) => {
|
|
298
|
+
e?.preventDefault();
|
|
299
|
+
if (!input.trim() || loading) return;
|
|
300
|
+
const text = input;
|
|
301
|
+
setInput("");
|
|
302
|
+
void send(text);
|
|
303
|
+
};
|
|
304
|
+
const onInputKeyDown = (e) => {
|
|
305
|
+
if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
|
|
306
|
+
e.preventDefault();
|
|
307
|
+
submit();
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const onPanelKeyDown = (e) => {
|
|
311
|
+
if (e.key === "Escape") {
|
|
312
|
+
e.stopPropagation();
|
|
313
|
+
close();
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
const last = messages[messages.length - 1];
|
|
317
|
+
const waitingFirstToken = loading && last?.role !== "assistant";
|
|
318
|
+
const canSend = input.trim().length > 0 && !loading;
|
|
319
|
+
return /* @__PURE__ */ jsxs("div", { style: styles.root, className: "tr-chat-widget", children: [
|
|
320
|
+
open && /* @__PURE__ */ jsxs(
|
|
321
|
+
"div",
|
|
322
|
+
{
|
|
323
|
+
id: panelId,
|
|
324
|
+
role: "dialog",
|
|
325
|
+
"aria-labelledby": titleId,
|
|
326
|
+
style: styles.panel,
|
|
327
|
+
className: "tr-chat-panel",
|
|
328
|
+
onKeyDown: onPanelKeyDown,
|
|
329
|
+
children: [
|
|
330
|
+
/* @__PURE__ */ jsxs("div", { style: styles.header, className: "tr-chat-header", children: [
|
|
331
|
+
/* @__PURE__ */ jsx("h2", { id: titleId, style: styles.title, children: title }),
|
|
332
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: close, "aria-label": "Close chat", style: styles.close, className: "tr-chat-close", children: "\xD7" })
|
|
333
|
+
] }),
|
|
334
|
+
/* @__PURE__ */ jsxs("ol", { ref: listRef, role: "log", "aria-live": "polite", "aria-relevant": "additions text", "aria-busy": loading, style: styles.list, className: "tr-chat-list", children: [
|
|
335
|
+
welcomeMessage && messages.length === 0 && /* @__PURE__ */ jsx("li", { style: { ...styles.bubble, ...styles.assistant }, className: "tr-chat-msg tr-chat-msg-assistant", children: welcomeMessage }),
|
|
336
|
+
messages.map((m, i) => /* @__PURE__ */ jsxs(
|
|
337
|
+
"li",
|
|
338
|
+
{
|
|
339
|
+
style: { ...styles.bubble, ...m.role === "user" ? styles.user : styles.assistant },
|
|
340
|
+
className: `tr-chat-msg tr-chat-msg-${m.role}`,
|
|
341
|
+
children: [
|
|
342
|
+
/* @__PURE__ */ jsx("span", { style: styles.srOnly, children: m.role === "user" ? "You: " : "Assistant: " }),
|
|
343
|
+
m.content
|
|
344
|
+
]
|
|
345
|
+
},
|
|
346
|
+
i
|
|
347
|
+
)),
|
|
348
|
+
waitingFirstToken && /* @__PURE__ */ jsx("li", { style: styles.status, className: "tr-chat-typing", children: "Typing\u2026" })
|
|
349
|
+
] }),
|
|
350
|
+
error && /* @__PURE__ */ jsx("div", { role: "alert", style: styles.error, className: "tr-chat-error", children: error.message || "Something went wrong." }),
|
|
351
|
+
/* @__PURE__ */ jsxs("form", { onSubmit: submit, style: styles.form, className: "tr-chat-form", children: [
|
|
352
|
+
/* @__PURE__ */ jsx("label", { htmlFor: inputId, style: styles.srOnly, children: "Message" }),
|
|
353
|
+
/* @__PURE__ */ jsx(
|
|
354
|
+
"textarea",
|
|
355
|
+
{
|
|
356
|
+
id: inputId,
|
|
357
|
+
ref: inputRef,
|
|
358
|
+
rows: 1,
|
|
359
|
+
value: input,
|
|
360
|
+
placeholder,
|
|
361
|
+
onChange: (e) => setInput(e.target.value),
|
|
362
|
+
onKeyDown: onInputKeyDown,
|
|
363
|
+
style: styles.input,
|
|
364
|
+
className: "tr-chat-input"
|
|
365
|
+
}
|
|
366
|
+
),
|
|
367
|
+
/* @__PURE__ */ jsx(
|
|
368
|
+
"button",
|
|
369
|
+
{
|
|
370
|
+
type: "submit",
|
|
371
|
+
disabled: !canSend,
|
|
372
|
+
"aria-label": "Send message",
|
|
373
|
+
style: { ...styles.send, opacity: canSend ? 1 : 0.5, cursor: canSend ? "pointer" : "not-allowed" },
|
|
374
|
+
className: "tr-chat-send",
|
|
375
|
+
children: "Send"
|
|
376
|
+
}
|
|
377
|
+
)
|
|
378
|
+
] })
|
|
379
|
+
]
|
|
380
|
+
}
|
|
381
|
+
),
|
|
382
|
+
/* @__PURE__ */ jsx(
|
|
383
|
+
"button",
|
|
384
|
+
{
|
|
385
|
+
ref: toggleRef,
|
|
386
|
+
type: "button",
|
|
387
|
+
onClick: () => open ? close() : setOpen(true),
|
|
388
|
+
"aria-label": open ? "Close chat" : "Open chat",
|
|
389
|
+
"aria-expanded": open,
|
|
390
|
+
"aria-controls": open ? panelId : void 0,
|
|
391
|
+
style: styles.toggle,
|
|
392
|
+
className: "tr-chat-toggle",
|
|
393
|
+
children: open ? /* @__PURE__ */ jsx("svg", { width: "22", height: "22", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2.2", strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M6 6l12 12M18 6L6 18" }) }) : /* @__PURE__ */ jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.6A8 8 0 1 1 21 12z" }) })
|
|
394
|
+
}
|
|
395
|
+
)
|
|
396
|
+
] });
|
|
397
|
+
}
|
|
398
|
+
export {
|
|
399
|
+
ChatWidget,
|
|
400
|
+
useChat
|
|
401
|
+
};
|
|
402
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client/ChatWidget.tsx","../src/client/useChat.ts"],"sourcesContent":["import { useEffect, useId, useRef, useState, type CSSProperties, type FormEvent, type KeyboardEvent } from 'react';\nimport { useChat } from './useChat';\n\nexport interface ChatWidgetProps {\n /** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */\n endpoint: string;\n /** Extra headers sent with every request. Never put provider API keys here: they would be public. */\n headers?: Record<string, string>;\n /** Input placeholder. */\n placeholder?: string;\n /** Panel title. */\n title?: string;\n /** Message shown when the conversation is empty. Display-only: not sent to the backend. */\n welcomeMessage?: string;\n /** Called on network / HTTP / stream errors. */\n onError?: (error: Error) => void;\n /** Open the panel on mount. */\n defaultOpen?: boolean;\n}\n\nconst ACCENT = '#2563eb';\nconst FONT = 'system-ui, -apple-system, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif';\n\nconst styles = {\n root: {\n position: 'fixed',\n right: 20,\n bottom: 20,\n zIndex: 2147483000,\n fontFamily: FONT,\n fontSize: 14,\n lineHeight: 1.45,\n color: '#111827',\n boxSizing: 'border-box',\n },\n toggle: {\n width: 56,\n height: 56,\n borderRadius: '50%',\n border: 'none',\n background: ACCENT,\n color: '#fff',\n cursor: 'pointer',\n boxShadow: '0 6px 20px rgba(0,0,0,0.2)',\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'center',\n marginLeft: 'auto',\n padding: 0,\n },\n panel: {\n width: 'min(370px, calc(100vw - 40px))',\n height: 'min(540px, calc(100vh - 110px))',\n marginBottom: 12,\n background: '#fff',\n borderRadius: 12,\n boxShadow: '0 12px 40px rgba(0,0,0,0.18)',\n border: '1px solid #e5e7eb',\n display: 'flex',\n flexDirection: 'column',\n overflow: 'hidden',\n },\n header: {\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'space-between',\n padding: '12px 14px',\n background: ACCENT,\n color: '#fff',\n },\n title: { margin: 0, fontSize: 15, fontWeight: 600 },\n close: {\n background: 'transparent',\n border: 'none',\n color: '#fff',\n cursor: 'pointer',\n fontSize: 20,\n lineHeight: 1,\n padding: 4,\n },\n list: {\n flex: 1,\n overflowY: 'auto',\n padding: 14,\n display: 'flex',\n flexDirection: 'column',\n gap: 8,\n background: '#f9fafb',\n margin: 0,\n listStyle: 'none',\n },\n bubble: {\n maxWidth: '85%',\n padding: '8px 12px',\n borderRadius: 12,\n whiteSpace: 'pre-wrap',\n wordBreak: 'break-word',\n },\n user: { alignSelf: 'flex-end', background: ACCENT, color: '#fff', borderBottomRightRadius: 4 },\n assistant: { alignSelf: 'flex-start', background: '#fff', border: '1px solid #e5e7eb', borderBottomLeftRadius: 4 },\n status: { alignSelf: 'flex-start', color: '#6b7280', fontStyle: 'italic', padding: '0 4px' },\n error: {\n margin: '0 14px 8px',\n padding: '8px 10px',\n borderRadius: 8,\n background: '#fef2f2',\n color: '#b91c1c',\n fontSize: 13,\n },\n form: { display: 'flex', gap: 8, padding: 10, borderTop: '1px solid #e5e7eb', background: '#fff' },\n input: {\n flex: 1,\n resize: 'none',\n border: '1px solid #d1d5db',\n borderRadius: 8,\n padding: '8px 10px',\n font: 'inherit',\n color: 'inherit',\n background: '#fff',\n minHeight: 38,\n maxHeight: 120,\n boxSizing: 'border-box',\n },\n send: {\n border: 'none',\n borderRadius: 8,\n background: ACCENT,\n color: '#fff',\n padding: '0 14px',\n font: 'inherit',\n fontWeight: 600,\n cursor: 'pointer',\n },\n srOnly: {\n position: 'absolute',\n width: 1,\n height: 1,\n padding: 0,\n margin: -1,\n overflow: 'hidden',\n clip: 'rect(0,0,0,0)',\n whiteSpace: 'nowrap',\n border: 0,\n },\n} satisfies Record<string, CSSProperties>;\n\nexport function ChatWidget({\n endpoint,\n headers,\n placeholder = 'Type a message…',\n title = 'Chat',\n welcomeMessage,\n onError,\n defaultOpen = false,\n}: ChatWidgetProps) {\n const [open, setOpen] = useState(defaultOpen);\n const [input, setInput] = useState('');\n const { messages, loading, error, send } = useChat({ endpoint, headers, onError });\n\n const id = useId();\n const panelId = `${id}-panel`;\n const titleId = `${id}-title`;\n const inputId = `${id}-input`;\n\n const toggleRef = useRef<HTMLButtonElement>(null);\n const inputRef = useRef<HTMLTextAreaElement>(null);\n const listRef = useRef<HTMLOListElement>(null);\n\n useEffect(() => {\n if (open) inputRef.current?.focus();\n }, [open]);\n\n useEffect(() => {\n const el = listRef.current;\n if (el) el.scrollTop = el.scrollHeight;\n }, [messages, loading, open]);\n\n const close = () => {\n setOpen(false);\n toggleRef.current?.focus();\n };\n\n const submit = (e?: FormEvent) => {\n e?.preventDefault();\n if (!input.trim() || loading) return;\n const text = input;\n setInput('');\n void send(text);\n };\n\n const onInputKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {\n if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {\n e.preventDefault();\n submit();\n }\n };\n\n const onPanelKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n if (e.key === 'Escape') {\n e.stopPropagation();\n close();\n }\n };\n\n const last = messages[messages.length - 1];\n const waitingFirstToken = loading && last?.role !== 'assistant';\n const canSend = input.trim().length > 0 && !loading;\n\n return (\n <div style={styles.root} className=\"tr-chat-widget\">\n {open && (\n <div\n id={panelId}\n role=\"dialog\"\n aria-labelledby={titleId}\n style={styles.panel}\n className=\"tr-chat-panel\"\n onKeyDown={onPanelKeyDown}\n >\n <div style={styles.header} className=\"tr-chat-header\">\n <h2 id={titleId} style={styles.title}>\n {title}\n </h2>\n <button type=\"button\" onClick={close} aria-label=\"Close chat\" style={styles.close} className=\"tr-chat-close\">\n ×\n </button>\n </div>\n\n <ol ref={listRef} role=\"log\" aria-live=\"polite\" aria-relevant=\"additions text\" aria-busy={loading} style={styles.list} className=\"tr-chat-list\">\n {welcomeMessage && messages.length === 0 && (\n <li style={{ ...styles.bubble, ...styles.assistant }} className=\"tr-chat-msg tr-chat-msg-assistant\">\n {welcomeMessage}\n </li>\n )}\n {messages.map((m, i) => (\n <li\n key={i}\n style={{ ...styles.bubble, ...(m.role === 'user' ? styles.user : styles.assistant) }}\n className={`tr-chat-msg tr-chat-msg-${m.role}`}\n >\n <span style={styles.srOnly}>{m.role === 'user' ? 'You: ' : 'Assistant: '}</span>\n {m.content}\n </li>\n ))}\n {waitingFirstToken && (\n <li style={styles.status} className=\"tr-chat-typing\">\n Typing…\n </li>\n )}\n </ol>\n\n {error && (\n <div role=\"alert\" style={styles.error} className=\"tr-chat-error\">\n {error.message || 'Something went wrong.'}\n </div>\n )}\n\n <form onSubmit={submit} style={styles.form} className=\"tr-chat-form\">\n <label htmlFor={inputId} style={styles.srOnly}>\n Message\n </label>\n <textarea\n id={inputId}\n ref={inputRef}\n rows={1}\n value={input}\n placeholder={placeholder}\n onChange={(e) => setInput(e.target.value)}\n onKeyDown={onInputKeyDown}\n style={styles.input}\n className=\"tr-chat-input\"\n />\n <button\n type=\"submit\"\n disabled={!canSend}\n aria-label=\"Send message\"\n style={{ ...styles.send, opacity: canSend ? 1 : 0.5, cursor: canSend ? 'pointer' : 'not-allowed' }}\n className=\"tr-chat-send\"\n >\n Send\n </button>\n </form>\n </div>\n )}\n\n <button\n ref={toggleRef}\n type=\"button\"\n onClick={() => (open ? close() : setOpen(true))}\n aria-label={open ? 'Close chat' : 'Open chat'}\n aria-expanded={open}\n aria-controls={open ? panelId : undefined}\n style={styles.toggle}\n className=\"tr-chat-toggle\"\n >\n {open ? (\n <svg width=\"22\" height=\"22\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" aria-hidden=\"true\">\n <path d=\"M6 6l12 12M18 6L6 18\" />\n </svg>\n ) : (\n <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n <path d=\"M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.6A8 8 0 1 1 21 12z\" />\n </svg>\n )}\n </button>\n </div>\n );\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type { ChatMessage } from '../types';\n\nexport interface UseChatOptions {\n /** URL that receives `POST { messages }` and answers with `{ reply }` JSON or an SSE stream. */\n endpoint: string;\n /** Extra headers sent with every request (e.g. a session token). Never put provider API keys here. */\n headers?: Record<string, string>;\n /** Initial conversation history. */\n initialMessages?: ChatMessage[];\n /** Called on network / HTTP / stream errors. */\n onError?: (error: Error) => void;\n}\n\nexport interface UseChatResult {\n messages: ChatMessage[];\n loading: boolean;\n error: Error | null;\n /** Sends a user message and appends the assistant reply. Resolves when the reply is complete. */\n send: (message: string) => Promise<void>;\n /** Aborts the in-flight request, if any. */\n stop: () => void;\n /** Clears the conversation and the error. */\n reset: () => void;\n}\n\nexport function useChat({ endpoint, headers, initialMessages, onError }: UseChatOptions): UseChatResult {\n const [messages, setMessages] = useState<ChatMessage[]>(initialMessages ?? []);\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n // Refs keep `send` stable and always reading the latest values.\n const messagesRef = useRef(messages);\n messagesRef.current = messages;\n const headersRef = useRef(headers);\n headersRef.current = headers;\n const onErrorRef = useRef(onError);\n onErrorRef.current = onError;\n const abortRef = useRef<AbortController | null>(null);\n\n useEffect(() => () => abortRef.current?.abort(), []);\n\n const stop = useCallback(() => {\n abortRef.current?.abort();\n abortRef.current = null;\n setLoading(false);\n }, []);\n\n const reset = useCallback(() => {\n stop();\n setMessages([]);\n setError(null);\n }, [stop]);\n\n const send = useCallback(\n async (message: string) => {\n const content = message.trim();\n if (!content || abortRef.current) return;\n\n // Drop empty turns (e.g. a reply that failed before its first token): backends reject them.\n const history: ChatMessage[] = [...messagesRef.current.filter((m) => m.content.trim()), { role: 'user', content }];\n setMessages(history);\n setLoading(true);\n setError(null);\n\n const controller = new AbortController();\n abortRef.current = controller;\n\n // Appends text to the assistant message at `history.length`, creating it on first call.\n const appendToReply = (text: string) => {\n setMessages((prev) => {\n const next = prev.slice();\n const current = next[history.length];\n next[history.length] = { role: 'assistant', content: (current?.content ?? '') + text };\n return next;\n });\n };\n\n try {\n const res = await fetch(endpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/event-stream', ...headersRef.current },\n body: JSON.stringify({ messages: history }),\n signal: controller.signal,\n });\n\n if (!res.ok) throw new Error(await readErrorMessage(res));\n\n const contentType = res.headers.get('content-type') ?? '';\n if (contentType.includes('text/event-stream')) {\n if (!res.body) throw new Error('Empty response stream');\n await readSSE(res.body, appendToReply);\n } else {\n const data: unknown = await res.json();\n const reply = data && typeof data === 'object' ? (data as { reply?: unknown }).reply : undefined;\n if (typeof reply !== 'string') throw new Error('Invalid response: expected { reply: string }');\n if (reply) appendToReply(reply);\n }\n } catch (err) {\n if (controller.signal.aborted) return;\n const e = err instanceof Error ? err : new Error(String(err));\n setError(e);\n onErrorRef.current?.(e);\n } finally {\n // Skip if `stop()` or a newer request already took over.\n if (abortRef.current === controller) {\n abortRef.current = null;\n setLoading(false);\n }\n }\n },\n [endpoint],\n );\n\n return { messages, loading, error, send, stop, reset };\n}\n\nasync function readErrorMessage(res: Response): Promise<string> {\n try {\n const data: unknown = await res.json();\n const msg = data && typeof data === 'object' ? (data as { error?: unknown }).error : undefined;\n if (typeof msg === 'string' && msg) return msg;\n } catch {\n // not JSON\n }\n return `Request failed with status ${res.status}`;\n}\n\n/** Minimal SSE reader. Accepts `data: {\"delta\": \"...\"}` (also `text`/`content`) or raw text; stops at `[DONE]`. */\nasync function readSSE(body: ReadableStream<Uint8Array>, onDelta: (text: string) => void): Promise<void> {\n const reader = body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n const handleEvent = (raw: string): boolean => {\n let event = 'message';\n const dataLines: string[] = [];\n for (const line of raw.split(/\\r?\\n/)) {\n if (line.startsWith('event:')) event = line.slice(6).trim();\n else if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));\n }\n if (dataLines.length === 0) return false;\n const data = dataLines.join('\\n');\n if (data === '[DONE]') return true;\n\n let parsed: unknown = data;\n try {\n parsed = JSON.parse(data);\n } catch {\n // raw text chunk\n }\n\n if (event === 'error') {\n const msg = parsed && typeof parsed === 'object' ? (parsed as { error?: unknown }).error : parsed;\n throw new Error(typeof msg === 'string' && msg ? msg : 'Stream error');\n }\n\n if (typeof parsed === 'string') onDelta(parsed);\n else if (parsed && typeof parsed === 'object') {\n const p = parsed as { delta?: unknown; text?: unknown; content?: unknown };\n const text = p.delta ?? p.text ?? p.content;\n if (typeof text === 'string') onDelta(text);\n }\n return false;\n };\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value, { stream: true });\n const events = buffer.split(/\\r?\\n\\r?\\n/);\n buffer = events.pop() ?? '';\n for (const evt of events) {\n if (handleEvent(evt)) return;\n }\n }\n buffer += decoder.decode();\n if (buffer.trim()) handleEvent(buffer);\n } finally {\n reader.cancel().catch(() => {});\n }\n}\n"],"mappings":";;;AAAA,SAAS,aAAAA,YAAW,OAAO,UAAAC,SAAQ,YAAAC,iBAAwE;;;ACA3G,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AA0BlD,SAAS,QAAQ,EAAE,UAAU,SAAS,iBAAiB,QAAQ,GAAkC;AACtG,QAAM,CAAC,UAAU,WAAW,IAAI,SAAwB,mBAAmB,CAAC,CAAC;AAC7E,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAC5C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AAGrD,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACtB,QAAM,aAAa,OAAO,OAAO;AACjC,aAAW,UAAU;AACrB,QAAM,aAAa,OAAO,OAAO;AACjC,aAAW,UAAU;AACrB,QAAM,WAAW,OAA+B,IAAI;AAEpD,YAAU,MAAM,MAAM,SAAS,SAAS,MAAM,GAAG,CAAC,CAAC;AAEnD,QAAM,OAAO,YAAY,MAAM;AAC7B,aAAS,SAAS,MAAM;AACxB,aAAS,UAAU;AACnB,eAAW,KAAK;AAAA,EAClB,GAAG,CAAC,CAAC;AAEL,QAAM,QAAQ,YAAY,MAAM;AAC9B,SAAK;AACL,gBAAY,CAAC,CAAC;AACd,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,QAAM,OAAO;AAAA,IACX,OAAO,YAAoB;AACzB,YAAM,UAAU,QAAQ,KAAK;AAC7B,UAAI,CAAC,WAAW,SAAS,QAAS;AAGlC,YAAM,UAAyB,CAAC,GAAG,YAAY,QAAQ,OAAO,CAAC,MAAM,EAAE,QAAQ,KAAK,CAAC,GAAG,EAAE,MAAM,QAAQ,QAAQ,C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