doccupine 0.0.146 → 0.0.148
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/README.md +11 -2
- package/dist/cli.d.ts +7 -0
- package/dist/cli.js +41 -0
- package/dist/generator/mdx-reconciliation-coordinator.d.ts +83 -0
- package/dist/generator/mdx-reconciliation-coordinator.js +471 -0
- package/dist/generator/page-renderer.js +18 -13
- package/dist/generator/secure-source-fs.js +5 -4
- package/dist/generator/site-artifacts.js +2 -2
- package/dist/generator/watch-coordinator.js +3 -2
- package/dist/index.js +8 -2
- package/dist/lib/layout.js +18 -10
- package/dist/lib/openapi-rendering.d.ts +17 -0
- package/dist/lib/openapi-rendering.js +193 -0
- package/dist/lib/openapi.d.ts +1 -7
- package/dist/lib/openapi.js +2 -192
- package/dist/lib/structures.js +2 -0
- package/dist/lib/utils.d.ts +14 -0
- package/dist/lib/utils.js +30 -8
- package/dist/mdx-to-nextjs-generator.d.ts +2 -41
- package/dist/mdx-to-nextjs-generator.js +37 -469
- package/dist/templates/components/Chat.d.ts +1 -1
- package/dist/templates/components/Chat.js +226 -1457
- package/dist/templates/components/SearchDocs.d.ts +1 -1
- package/dist/templates/components/SearchDocs.js +20 -15
- package/dist/templates/components/SearchModalContent.d.ts +1 -1
- package/dist/templates/components/SearchModalContent.js +7 -1
- package/dist/templates/components/layout/ApiPlayground.d.ts +1 -1
- package/dist/templates/components/layout/ApiPlayground.js +14 -148
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +3 -3
- package/dist/templates/components/layout/DocsNavigation.d.ts +1 -1
- package/dist/templates/components/layout/DocsNavigation.js +2 -3
- package/dist/templates/components/layout/Footer.d.ts +1 -1
- package/dist/templates/components/layout/Footer.js +2 -4
- package/dist/templates/components/layout/Header.d.ts +1 -1
- package/dist/templates/components/layout/Header.js +3 -3
- package/dist/templates/components/layout/StaticLinks.d.ts +1 -1
- package/dist/templates/components/layout/StaticLinks.js +2 -3
- package/dist/templates/env.example.d.ts +1 -1
- package/dist/templates/env.example.js +8 -0
- package/dist/templates/mdx/platform/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/analytics.mdx.js +13 -0
- package/dist/templates/mdx/platform/fonts-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.js +16 -0
- package/dist/templates/mdx/platform/publishing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/publishing.mdx.js +16 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +17 -5
- package/dist/templates/next.config.js +40 -0
- package/dist/templates/package.js +11 -10
- package/dist/templates/prettierignore.d.ts +1 -1
- package/dist/templates/prettierignore.js +11 -0
- package/dist/templates/utils/apiPlayground.d.ts +1 -0
- package/dist/templates/utils/apiPlayground.js +149 -0
- package/package.json +3 -3
|
@@ -1,1159 +1,269 @@
|
|
|
1
|
-
import { CHAT_WIDTH } from "../app/theme.js";
|
|
2
1
|
export const chatTemplate = `"use client";
|
|
3
|
-
import React, {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
import React, { createContext, useMemo } from "react";
|
|
3
|
+
import { useRouter } from "next/navigation";
|
|
4
|
+
import {
|
|
5
|
+
ChatInput,
|
|
6
|
+
ChatMessage,
|
|
7
|
+
ChatMessageList,
|
|
8
|
+
ChatPanel,
|
|
9
|
+
ChatProvider,
|
|
10
|
+
ChatSource,
|
|
11
|
+
ChatSources,
|
|
12
|
+
ChatTyping,
|
|
13
|
+
IconButton,
|
|
14
|
+
Prose,
|
|
15
|
+
useBelowBreakpoint,
|
|
16
|
+
useChat,
|
|
17
|
+
type ChatSendContext,
|
|
18
|
+
type ChatSourceData,
|
|
19
|
+
} from "cherry-styled-components";
|
|
20
|
+
import { RotateCcw } from "lucide-react";
|
|
16
21
|
import remarkGfm from "remark-gfm";
|
|
17
22
|
import rehypeHighlight from "rehype-highlight";
|
|
18
23
|
import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote";
|
|
19
24
|
import { serialize } from "next-mdx-remote/serialize";
|
|
20
|
-
import Link from "next/link";
|
|
21
|
-
import { mq, theme, Theme } from "@/app/theme";
|
|
22
25
|
import { Callout } from "@/components/layout/Callout";
|
|
23
|
-
import { useLockBodyScroll } from "@/components/LockBodyScroll";
|
|
24
26
|
import { useMDXComponents as getMDXComponents } from "@/components/MDXComponents";
|
|
25
|
-
import {
|
|
26
|
-
styledAnchor,
|
|
27
|
-
styledTable,
|
|
28
|
-
stylesLists,
|
|
29
|
-
StyledSmallButton,
|
|
30
|
-
interactiveStyles,
|
|
31
|
-
thinScrollbar,
|
|
32
|
-
} from "@/components/layout/SharedStyled";
|
|
33
27
|
|
|
34
28
|
const mdxComponents = getMDXComponents({});
|
|
35
29
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function getMobileChatSnapshot() {
|
|
45
|
-
return window.matchMedia(MOBILE_CHAT_QUERY).matches;
|
|
46
|
-
}
|
|
30
|
+
type RagSource = {
|
|
31
|
+
id: string;
|
|
32
|
+
path: string;
|
|
33
|
+
uri: string;
|
|
34
|
+
score: number;
|
|
35
|
+
};
|
|
47
36
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
37
|
+
// Map a retrieval hit onto the chip the transcript renders: docs://<slug>
|
|
38
|
+
// becomes an app-relative href, and the slug's last segment becomes a
|
|
39
|
+
// title-cased label.
|
|
40
|
+
function toSourceChip(src: RagSource): ChatSourceData {
|
|
41
|
+
const slug = src.uri.replace("docs://", "").replace(/^\\/+/, "");
|
|
42
|
+
const href = slug ? \`/\${slug}/\` : "/";
|
|
43
|
+
const label = slug
|
|
44
|
+
? slug
|
|
45
|
+
.split("/")
|
|
46
|
+
.pop()!
|
|
47
|
+
.replace(/-/g, " ")
|
|
48
|
+
.replace(/\\b\\w/g, (c: string) => c.toUpperCase())
|
|
49
|
+
: "Home";
|
|
50
|
+
return { id: src.id, label, href };
|
|
54
51
|
}
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
\`}
|
|
85
|
-
\`;
|
|
86
|
-
|
|
87
|
-
const StyledChat = styled.div<{ theme: Theme; $isVisible: boolean }>\`
|
|
88
|
-
margin: 0;
|
|
89
|
-
position: fixed;
|
|
90
|
-
top: 0;
|
|
91
|
-
right: 0;
|
|
92
|
-
width: 100%;
|
|
93
|
-
height: calc(100dvh - 90px);
|
|
94
|
-
overflow-y: scroll;
|
|
95
|
-
overflow-x: hidden;
|
|
96
|
-
z-index: 1000;
|
|
97
|
-
padding: 0 20px;
|
|
98
|
-
/* See StyledChatForm: visibility snaps open and is delayed on close, so the
|
|
99
|
-
panel is focusable as soon as it opens. */
|
|
100
|
-
transition:
|
|
101
|
-
transform 0.3s ease,
|
|
102
|
-
opacity 0.3s ease,
|
|
103
|
-
visibility 0s linear 0s;
|
|
104
|
-
transform: translateX(0);
|
|
105
|
-
background: \${({ theme }) => theme.colors.light};
|
|
106
|
-
-webkit-overflow-scrolling: touch;
|
|
107
|
-
opacity: 1;
|
|
108
|
-
visibility: visible;
|
|
109
|
-
|
|
110
|
-
&::-webkit-scrollbar {
|
|
111
|
-
display: none;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
\${({ $isVisible }) =>
|
|
115
|
-
!$isVisible &&
|
|
116
|
-
css\`
|
|
117
|
-
transform: translateX(100%);
|
|
118
|
-
opacity: 0;
|
|
119
|
-
visibility: hidden;
|
|
120
|
-
transition-delay: 0s, 0s, 0.3s;
|
|
121
|
-
\`}
|
|
122
|
-
|
|
123
|
-
@media (prefers-reduced-motion: reduce) {
|
|
124
|
-
transition: none;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
\${mq("lg")} {
|
|
128
|
-
width: ${CHAT_WIDTH}px;
|
|
129
|
-
border-left: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
130
|
-
}
|
|
131
|
-
\`;
|
|
132
|
-
|
|
133
|
-
const loadingAnimation = keyframes\`
|
|
134
|
-
0% {
|
|
135
|
-
transform: rotate(0deg);
|
|
136
|
-
}
|
|
137
|
-
100% {
|
|
138
|
-
transform: rotate(360deg);
|
|
139
|
-
}
|
|
140
|
-
\`;
|
|
141
|
-
|
|
142
|
-
const rotateGradient = keyframes\`
|
|
143
|
-
0% {
|
|
144
|
-
--gradient-angle: 0deg;
|
|
145
|
-
}
|
|
146
|
-
100% {
|
|
147
|
-
--gradient-angle: 360deg;
|
|
148
|
-
}
|
|
149
|
-
\`;
|
|
150
|
-
|
|
151
|
-
const pulseGlow = keyframes\`
|
|
152
|
-
0%, 100% {
|
|
153
|
-
opacity: 0.5;
|
|
154
|
-
filter: blur(16px);
|
|
155
|
-
}
|
|
156
|
-
50% {
|
|
157
|
-
opacity: 1;
|
|
158
|
-
filter: blur(22px);
|
|
159
|
-
}
|
|
160
|
-
\`;
|
|
161
|
-
|
|
162
|
-
const sparkleFloat = keyframes\`
|
|
163
|
-
0%, 100% {
|
|
164
|
-
opacity: 0;
|
|
165
|
-
transform: translateY(0) scale(0);
|
|
166
|
-
}
|
|
167
|
-
50% {
|
|
168
|
-
opacity: 0.9;
|
|
169
|
-
transform: translateY(-20px) scale(1);
|
|
170
|
-
}
|
|
171
|
-
\`;
|
|
172
|
-
|
|
173
|
-
const shimmer = keyframes\`
|
|
174
|
-
0% {
|
|
175
|
-
background-position: 0% center;
|
|
176
|
-
}
|
|
177
|
-
50% {
|
|
178
|
-
background-position: 100% center;
|
|
179
|
-
}
|
|
180
|
-
100% {
|
|
181
|
-
background-position: 0% center;
|
|
182
|
-
}
|
|
183
|
-
\`;
|
|
184
|
-
|
|
185
|
-
const StyledRainbowInputWrapper = styled.div<{
|
|
186
|
-
theme: Theme;
|
|
187
|
-
$isActive: boolean;
|
|
188
|
-
}>\`
|
|
189
|
-
@property --gradient-angle {
|
|
190
|
-
syntax: "<angle>";
|
|
191
|
-
initial-value: 0deg;
|
|
192
|
-
inherits: false;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
position: relative;
|
|
196
|
-
flex: 1;
|
|
197
|
-
|
|
198
|
-
&::before {
|
|
199
|
-
content: "";
|
|
200
|
-
position: absolute;
|
|
201
|
-
inset: -2px;
|
|
202
|
-
border-radius: 14px;
|
|
203
|
-
background: conic-gradient(
|
|
204
|
-
from var(--gradient-angle),
|
|
205
|
-
#cc5555,
|
|
206
|
-
#d9a745,
|
|
207
|
-
#3ab0cc,
|
|
208
|
-
#cc7fc2,
|
|
209
|
-
#4380cc,
|
|
210
|
-
#4c1fa3,
|
|
211
|
-
#cc5555
|
|
212
|
-
);
|
|
213
|
-
opacity: 0;
|
|
214
|
-
transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
|
|
215
|
-
animation: \${rotateGradient} 3s linear infinite;
|
|
216
|
-
z-index: 0;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
&::after {
|
|
220
|
-
content: "";
|
|
221
|
-
position: absolute;
|
|
222
|
-
inset: -10px;
|
|
223
|
-
border-radius: 20px;
|
|
224
|
-
background: conic-gradient(
|
|
225
|
-
from var(--gradient-angle),
|
|
226
|
-
#ff6b6b66,
|
|
227
|
-
#feca5766,
|
|
228
|
-
#48dbfb66,
|
|
229
|
-
#ff9ff366,
|
|
230
|
-
#54a0ff66,
|
|
231
|
-
#5f27cd66,
|
|
232
|
-
#ff6b6b66
|
|
233
|
-
);
|
|
234
|
-
opacity: 0;
|
|
235
|
-
transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
|
|
236
|
-
animation:
|
|
237
|
-
\${rotateGradient} 3s linear infinite,
|
|
238
|
-
\${pulseGlow} 2s ease-in-out infinite;
|
|
239
|
-
z-index: -1;
|
|
240
|
-
pointer-events: none;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
&:hover::before,
|
|
244
|
-
&:focus-within::before {
|
|
245
|
-
opacity: 1;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
&:hover::after,
|
|
249
|
-
&:focus-within::after {
|
|
250
|
-
opacity: 1;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
\${({ $isActive }) =>
|
|
254
|
-
$isActive &&
|
|
255
|
-
css\`
|
|
256
|
-
&::before {
|
|
257
|
-
opacity: 1;
|
|
258
|
-
}
|
|
259
|
-
&::after {
|
|
260
|
-
opacity: 1;
|
|
261
|
-
}
|
|
262
|
-
\`}
|
|
263
|
-
\`;
|
|
264
|
-
|
|
265
|
-
const StyledSparkleContainer = styled.div<{ $isActive: boolean }>\`
|
|
266
|
-
position: absolute;
|
|
267
|
-
inset: -30px;
|
|
268
|
-
pointer-events: none;
|
|
269
|
-
overflow: hidden;
|
|
270
|
-
border-radius: 30px;
|
|
271
|
-
z-index: -2;
|
|
272
|
-
opacity: 0;
|
|
273
|
-
transition: opacity 0.4s ease;
|
|
274
|
-
|
|
275
|
-
\${({ $isActive }) =>
|
|
276
|
-
$isActive &&
|
|
277
|
-
css\`
|
|
278
|
-
opacity: 1;
|
|
279
|
-
\`}
|
|
280
|
-
\`;
|
|
281
|
-
|
|
282
|
-
const StyledSparkle = styled.div<{
|
|
283
|
-
$color: string;
|
|
284
|
-
$left: number;
|
|
285
|
-
$top: number;
|
|
286
|
-
$delay: number;
|
|
287
|
-
}>\`
|
|
288
|
-
position: absolute;
|
|
289
|
-
width: 4px;
|
|
290
|
-
height: 4px;
|
|
291
|
-
border-radius: 50%;
|
|
292
|
-
background: \${({ $color }) => $color};
|
|
293
|
-
box-shadow: 0 0 6px \${({ $color }) => $color};
|
|
294
|
-
left: \${({ $left }) => $left}%;
|
|
295
|
-
top: \${({ $top }) => $top}%;
|
|
296
|
-
animation: \${sparkleFloat} 2s ease-in-out infinite;
|
|
297
|
-
animation-delay: \${({ $delay }) => $delay}s;
|
|
298
|
-
\`;
|
|
299
|
-
|
|
300
|
-
const StyledRainbowInput = styled.input<{ theme: Theme }>\`
|
|
301
|
-
position: relative;
|
|
302
|
-
z-index: 1;
|
|
303
|
-
width: 100%;
|
|
304
|
-
background: \${({ theme }) => theme.colors.light};
|
|
305
|
-
border: 1px solid \${({ theme }) => theme.colors.grayLight};
|
|
306
|
-
border-radius: 12px;
|
|
307
|
-
padding: 12px 18px;
|
|
308
|
-
font-size: \${({ theme }) => theme.fontSizes.text.lg};
|
|
309
|
-
font-family: inherit;
|
|
310
|
-
color: \${({ theme }) => theme.colors.dark};
|
|
311
|
-
outline: none;
|
|
312
|
-
transition:
|
|
313
|
-
border-color 0.3s ease,
|
|
314
|
-
box-shadow 0.3s ease;
|
|
315
|
-
|
|
316
|
-
\${mq("lg")} {
|
|
317
|
-
font-size: \${({ theme }) => theme.fontSizes.small.lg};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
&::placeholder {
|
|
321
|
-
color: \${({ theme }) =>
|
|
322
|
-
\`color-mix(in srgb, \${theme.colors.dark} 40%, transparent)\`};
|
|
323
|
-
transition: color 0.3s ease;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
&:focus::placeholder {
|
|
327
|
-
color: \${({ theme }) =>
|
|
328
|
-
\`color-mix(in srgb, \${theme.colors.dark} 60%, transparent)\`};
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
&:focus {
|
|
332
|
-
border-color: transparent;
|
|
333
|
-
}
|
|
334
|
-
\`;
|
|
335
|
-
|
|
336
|
-
const StyledRainbowButton = styled(Button)<{
|
|
337
|
-
theme: Theme;
|
|
338
|
-
$hasContent: boolean;
|
|
339
|
-
}>\`
|
|
340
|
-
padding-top: 10px;
|
|
341
|
-
padding-bottom: 10px;
|
|
342
|
-
position: relative;
|
|
343
|
-
overflow: hidden;
|
|
344
|
-
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
|
345
|
-
color: \${({ theme }) => theme.colors.surface};
|
|
346
|
-
|
|
347
|
-
&::before {
|
|
348
|
-
content: "";
|
|
349
|
-
position: absolute;
|
|
350
|
-
inset: 0;
|
|
351
|
-
background: linear-gradient(
|
|
352
|
-
135deg,
|
|
353
|
-
#ff6b6b,
|
|
354
|
-
#feca57,
|
|
355
|
-
#48dbfb,
|
|
356
|
-
#ff9ff3,
|
|
357
|
-
#54a0ff
|
|
358
|
-
);
|
|
359
|
-
background-size: 300% 300%;
|
|
360
|
-
opacity: 0;
|
|
361
|
-
transition: opacity 0.3s ease;
|
|
362
|
-
z-index: 0;
|
|
363
|
-
animation: \${shimmer} 3s linear infinite;
|
|
364
|
-
width: 200%;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
\${({ $hasContent }) =>
|
|
368
|
-
$hasContent &&
|
|
369
|
-
css\`
|
|
370
|
-
&::before {
|
|
371
|
-
opacity: 1;
|
|
53
|
+
// Cherry's ChatProvider owns the transcript, panel state, and streaming
|
|
54
|
+
// bookkeeping; this handler owns the transport: POST the question to
|
|
55
|
+
// /api/rag, stream the SSE frames, and patch the assistant bubble as tokens
|
|
56
|
+
// arrive. The provider bounds \`history\` to the RAG contract (20 entries,
|
|
57
|
+
// 4000 chars each), swallows aborts, and surfaces thrown errors as \`error\`.
|
|
58
|
+
async function sendToAssistant(
|
|
59
|
+
question: string,
|
|
60
|
+
{ signal, history, setAssistant }: ChatSendContext,
|
|
61
|
+
) {
|
|
62
|
+
const res = await fetch("/api/rag", {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: { "Content-Type": "application/json" },
|
|
65
|
+
body: JSON.stringify({ question, history }),
|
|
66
|
+
signal,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (!res.ok) {
|
|
70
|
+
// The body may be a proxy/gateway HTML error page (e.g. Cloudflare
|
|
71
|
+
// 524), not our JSON - never blindly JSON.parse it.
|
|
72
|
+
const contentType = res.headers.get("content-type") || "";
|
|
73
|
+
let message = "Request failed";
|
|
74
|
+
if (contentType.includes("application/json")) {
|
|
75
|
+
try {
|
|
76
|
+
const errorData = await res.json();
|
|
77
|
+
message = errorData.error || message;
|
|
78
|
+
} catch {
|
|
79
|
+
// Non-JSON despite the header - fall through to the default.
|
|
372
80
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
&:hover::before {
|
|
376
|
-
opacity: 1;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
&:hover {
|
|
380
|
-
transform: scale(1.05);
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
&:active {
|
|
384
|
-
transform: scale(0.95);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
& svg {
|
|
388
|
-
position: relative;
|
|
389
|
-
z-index: 1;
|
|
390
|
-
transition: transform 0.3s ease;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
&:disabled,
|
|
394
|
-
&:disabled:hover {
|
|
395
|
-
background: \${({ theme }) => theme.colors.primaryDark};
|
|
396
|
-
transform: none;
|
|
397
|
-
box-shadow: none;
|
|
398
|
-
|
|
399
|
-
&::before {
|
|
400
|
-
opacity: 0;
|
|
81
|
+
} else if ([502, 503, 504, 524].includes(res.status)) {
|
|
82
|
+
message = "The assistant took too long to respond. Please try again.";
|
|
401
83
|
}
|
|
84
|
+
throw new Error(message);
|
|
402
85
|
}
|
|
403
|
-
\`;
|
|
404
|
-
|
|
405
|
-
const StyledChatForm = styled.form<{ theme: Theme; $isVisible: boolean }>\`
|
|
406
|
-
display: flex;
|
|
407
|
-
gap: 10px;
|
|
408
|
-
justify-content: center;
|
|
409
|
-
align-items: center;
|
|
410
|
-
background: \${({ theme }) => theme.colors.light};
|
|
411
|
-
padding: 20px;
|
|
412
|
-
position: fixed;
|
|
413
|
-
bottom: 0;
|
|
414
|
-
right: 0;
|
|
415
|
-
z-index: 1000;
|
|
416
|
-
width: 100%;
|
|
417
|
-
border-top: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
418
|
-
/* visibility is animatable, so transitioning "all" keeps this hidden for the
|
|
419
|
-
whole slide-in - long enough for the focus() that follows opening to be a
|
|
420
|
-
silent no-op. Snap it on open (transition-delay below) and delay it on
|
|
421
|
-
close so the slide-out stays visible while it animates. */
|
|
422
|
-
transition:
|
|
423
|
-
transform 0.3s ease,
|
|
424
|
-
opacity 0.3s ease,
|
|
425
|
-
visibility 0s linear 0.3s;
|
|
426
|
-
transform: translateX(100%);
|
|
427
|
-
opacity: 0;
|
|
428
|
-
visibility: hidden;
|
|
429
86
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
87
|
+
const reader = res.body?.getReader();
|
|
88
|
+
if (!reader) {
|
|
89
|
+
throw new Error("Failed to get response reader");
|
|
433
90
|
}
|
|
434
91
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
transform: translateX(0);
|
|
440
|
-
visibility: visible;
|
|
441
|
-
transition-delay: 0s;
|
|
442
|
-
\`}
|
|
92
|
+
const decoder = new TextDecoder();
|
|
93
|
+
const contentParts: string[] = [];
|
|
94
|
+
let sources: ChatSourceData[] = [];
|
|
95
|
+
let buffer = "";
|
|
443
96
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
& .loading {
|
|
449
|
-
animation: \${loadingAnimation} 1s linear infinite;
|
|
450
|
-
}
|
|
451
|
-
\`;
|
|
97
|
+
while (true) {
|
|
98
|
+
const { done, value } = await reader.read();
|
|
99
|
+
if (done) break;
|
|
452
100
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}>\`
|
|
457
|
-
@property --gradient-angle {
|
|
458
|
-
syntax: "<angle>";
|
|
459
|
-
initial-value: 0deg;
|
|
460
|
-
inherits: false;
|
|
461
|
-
}
|
|
101
|
+
buffer += decoder.decode(value, { stream: true });
|
|
102
|
+
const parts = buffer.split("\\n");
|
|
103
|
+
buffer = parts.pop() ?? "";
|
|
462
104
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
margin-right: 0;
|
|
466
|
-
background: \${({ theme }) => theme.colors.light};
|
|
467
|
-
padding: 0;
|
|
105
|
+
for (const line of parts) {
|
|
106
|
+
if (!line.startsWith("data: ")) continue;
|
|
468
107
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
#4380cc,
|
|
480
|
-
#4c1fa3,
|
|
481
|
-
#cc5555
|
|
482
|
-
);
|
|
483
|
-
opacity: 0;
|
|
484
|
-
transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
|
|
485
|
-
animation: \${rotateGradient} 3s linear infinite;
|
|
486
|
-
z-index: -1;
|
|
487
|
-
position: absolute;
|
|
488
|
-
top: -2px;
|
|
489
|
-
left: -2px;
|
|
490
|
-
width: calc(100% + 4px);
|
|
491
|
-
height: calc(100% + 4px);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
&::after {
|
|
495
|
-
content: "";
|
|
496
|
-
position: absolute;
|
|
497
|
-
inset: -8px;
|
|
498
|
-
border-radius: 14px;
|
|
499
|
-
background: conic-gradient(
|
|
500
|
-
from var(--gradient-angle),
|
|
501
|
-
#ff6b6b66,
|
|
502
|
-
#feca5766,
|
|
503
|
-
#48dbfb66,
|
|
504
|
-
#ff9ff366,
|
|
505
|
-
#54a0ff66,
|
|
506
|
-
#5f27cd66,
|
|
507
|
-
#ff6b6b66
|
|
508
|
-
);
|
|
509
|
-
opacity: 0;
|
|
510
|
-
transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
|
|
511
|
-
animation:
|
|
512
|
-
\${rotateGradient} 3s linear infinite,
|
|
513
|
-
\${pulseGlow} 2s ease-in-out infinite;
|
|
514
|
-
z-index: -2;
|
|
515
|
-
pointer-events: none;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
&:hover::before,
|
|
519
|
-
&:hover::after {
|
|
520
|
-
opacity: 1;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
& span {
|
|
524
|
-
padding: 6px 8px;
|
|
525
|
-
display: flex;
|
|
526
|
-
background: \${({ theme }) => theme.colors.light};
|
|
527
|
-
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
528
|
-
gap: 6px;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
\${({ $hasContent }) =>
|
|
532
|
-
$hasContent &&
|
|
533
|
-
css\`
|
|
534
|
-
&::before {
|
|
535
|
-
opacity: 1;
|
|
536
|
-
}
|
|
537
|
-
&::after {
|
|
538
|
-
opacity: 1;
|
|
108
|
+
// Guard only the parse: a malformed frame is skipped, while the
|
|
109
|
+
// server's error event below must throw past this loop so it lands
|
|
110
|
+
// in the provider's error state instead of dying in a parse-error
|
|
111
|
+
// log.
|
|
112
|
+
let data;
|
|
113
|
+
try {
|
|
114
|
+
data = JSON.parse(line.slice(6));
|
|
115
|
+
} catch {
|
|
116
|
+
console.error("Failed to parse SSE data:", line);
|
|
117
|
+
continue;
|
|
539
118
|
}
|
|
540
|
-
\`}
|
|
541
|
-
\`;
|
|
542
|
-
|
|
543
|
-
const StyledError = styled.div\`
|
|
544
|
-
margin: 20px 0;
|
|
545
|
-
width: 100%;
|
|
546
|
-
\`;
|
|
547
|
-
|
|
548
|
-
const loadingDotAnimation = keyframes\`
|
|
549
|
-
0% {
|
|
550
|
-
opacity: 0;
|
|
551
|
-
}
|
|
552
|
-
50% {
|
|
553
|
-
opacity: 1;
|
|
554
|
-
}
|
|
555
|
-
100% {
|
|
556
|
-
opacity: 0;
|
|
557
|
-
}
|
|
558
|
-
\`;
|
|
559
|
-
|
|
560
|
-
const StyledLoading = styled.div<{ theme: Theme }>\`
|
|
561
|
-
overflow-x: auto;
|
|
562
|
-
\${thinScrollbar};
|
|
563
|
-
margin: 20px 0;
|
|
564
|
-
width: 100%;
|
|
565
|
-
font-weight: 600;
|
|
566
|
-
\${styledText};
|
|
567
|
-
color: \${({ theme }) => theme.colors.dark};
|
|
568
|
-
|
|
569
|
-
& span {
|
|
570
|
-
&:nth-child(1) {
|
|
571
|
-
animation: \${loadingDotAnimation} 1s ease infinite;
|
|
572
|
-
}
|
|
573
|
-
&:nth-child(2) {
|
|
574
|
-
animation: \${loadingDotAnimation} 1s ease infinite;
|
|
575
|
-
animation-delay: 0.2s;
|
|
576
|
-
}
|
|
577
|
-
&:nth-child(3) {
|
|
578
|
-
animation: \${loadingDotAnimation} 1s ease infinite;
|
|
579
|
-
animation-delay: 0.4s;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
\`;
|
|
583
|
-
|
|
584
|
-
const StyledAnswer = styled.div<{ theme: Theme; $isAnswer: boolean }>\`
|
|
585
|
-
overflow-x: auto;
|
|
586
|
-
\${thinScrollbar};
|
|
587
|
-
background: \${({ theme }) => theme.colors.primary};
|
|
588
|
-
color: \${({ theme }) => theme.colors.surface};
|
|
589
|
-
padding: 9px 14px;
|
|
590
|
-
border-radius: 16px 16px 4px 16px;
|
|
591
|
-
margin: 20px 0 20px auto;
|
|
592
|
-
width: fit-content;
|
|
593
|
-
max-width: min(560px, 88%);
|
|
594
|
-
font-weight: 500;
|
|
595
|
-
white-space: pre-wrap;
|
|
596
|
-
overflow-wrap: anywhere;
|
|
597
|
-
\${styledText};
|
|
598
|
-
|
|
599
|
-
& p {
|
|
600
|
-
\${styledText};
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
\${({ $isAnswer }) =>
|
|
604
|
-
$isAnswer &&
|
|
605
|
-
css\`
|
|
606
|
-
background: transparent;
|
|
607
|
-
color: \${({ theme }) => theme.colors.dark};
|
|
608
|
-
padding: 0;
|
|
609
|
-
width: 100%;
|
|
610
|
-
max-width: 100%;
|
|
611
|
-
margin: 20px 0;
|
|
612
|
-
border-radius: 0;
|
|
613
|
-
font-weight: inherit;
|
|
614
|
-
white-space: normal;
|
|
615
|
-
\`}
|
|
616
|
-
|
|
617
|
-
& code:not([class]) {
|
|
618
|
-
background: \${({ theme }) =>
|
|
619
|
-
\`color-mix(in srgb, \${theme.colors.primaryLight} 20%, transparent)\`};
|
|
620
|
-
color: \${({ theme }) => theme.colors.dark};
|
|
621
|
-
padding: 2px 4px;
|
|
622
|
-
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
623
|
-
white-space: pre;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
\${styledAnchor};
|
|
627
|
-
\${stylesLists};
|
|
628
|
-
\${styledTable};
|
|
629
|
-
|
|
630
|
-
& pre,
|
|
631
|
-
& .hljs {
|
|
632
|
-
margin: 10px 0;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
& .code-wrapper pre {
|
|
636
|
-
margin: 0;
|
|
637
|
-
\${styledText};
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
& > *:first-child {
|
|
641
|
-
margin-top: 0;
|
|
642
|
-
}
|
|
643
119
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
120
|
+
if (data.type === "metadata") {
|
|
121
|
+
const allSources: RagSource[] = data.data?.sources ?? [];
|
|
122
|
+
const seen = new Set<string>();
|
|
123
|
+
sources = allSources
|
|
124
|
+
.filter((s) => {
|
|
125
|
+
if (s.score < 0.4 || seen.has(s.uri)) return false;
|
|
126
|
+
seen.add(s.uri);
|
|
127
|
+
return true;
|
|
128
|
+
})
|
|
129
|
+
.map(toSourceChip);
|
|
130
|
+
} else if (data.type === "content") {
|
|
131
|
+
contentParts.push(data.data);
|
|
132
|
+
const streamedContent = contentParts.join("");
|
|
133
|
+
setAssistant(streamedContent, { text: streamedContent, sources });
|
|
134
|
+
} else if (data.type === "error") {
|
|
135
|
+
throw new Error(data.data);
|
|
136
|
+
} else if (data.type === "done") {
|
|
137
|
+
const streamedContent = contentParts.join("");
|
|
138
|
+
let mdxSource: MDXRemoteSerializeResult | null = null;
|
|
139
|
+
try {
|
|
140
|
+
mdxSource = await serialize(streamedContent, {
|
|
141
|
+
parseFrontmatter: false,
|
|
142
|
+
mdxOptions: {
|
|
143
|
+
remarkPlugins: [remarkGfm],
|
|
144
|
+
rehypePlugins: [rehypeHighlight],
|
|
145
|
+
format: "md",
|
|
146
|
+
development: false,
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
} catch (mdxError: unknown) {
|
|
150
|
+
console.error("MDX serialization error:", mdxError);
|
|
151
|
+
}
|
|
651
152
|
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
153
|
+
setAssistant(
|
|
154
|
+
mdxSource ? (
|
|
155
|
+
<MDXRemote {...mdxSource} components={mdxComponents} />
|
|
156
|
+
) : (
|
|
157
|
+
streamedContent
|
|
158
|
+
),
|
|
159
|
+
{ text: streamedContent, sources },
|
|
160
|
+
);
|
|
161
|
+
}
|
|
656
162
|
}
|
|
657
163
|
}
|
|
658
|
-
|
|
659
|
-
& img,
|
|
660
|
-
& video,
|
|
661
|
-
& iframe {
|
|
662
|
-
max-width: 100%;
|
|
663
|
-
border-radius: \${({ theme }) => theme.spacing.radius.lg};
|
|
664
|
-
margin: 10px 0;
|
|
665
|
-
display: block;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
& h1,
|
|
669
|
-
& h2,
|
|
670
|
-
& h3,
|
|
671
|
-
& h4,
|
|
672
|
-
& h5,
|
|
673
|
-
& h6 {
|
|
674
|
-
margin: 10px 0;
|
|
675
|
-
padding: 0;
|
|
676
|
-
}
|
|
677
|
-
\`;
|
|
678
|
-
|
|
679
|
-
const StyledSources = styled.div\`
|
|
680
|
-
display: flex;
|
|
681
|
-
gap: 16px;
|
|
682
|
-
flex-wrap: wrap;
|
|
683
|
-
margin: -5px 0 20px;
|
|
684
|
-
\`;
|
|
685
|
-
|
|
686
|
-
const StyledSourceLink = styled(Link)<{ theme: Theme }>\`
|
|
687
|
-
position: relative;
|
|
688
|
-
text-decoration: none;
|
|
689
|
-
font-size: \${({ theme }) => theme.fontSizes.small.lg};
|
|
690
|
-
line-height: 1;
|
|
691
|
-
color: \${({ theme }) => theme.colors.primary};
|
|
692
|
-
display: flex;
|
|
693
|
-
gap: 6px;
|
|
694
|
-
transition: all 0.3s ease;
|
|
695
|
-
font-weight: 600;
|
|
696
|
-
white-space: nowrap;
|
|
697
|
-
min-width: fit-content;
|
|
698
|
-
background: \${({ theme }) =>
|
|
699
|
-
\`color-mix(in srgb, \${theme.colors.primaryLight} 10%, transparent)\`};
|
|
700
|
-
padding: 6px 8px;
|
|
701
|
-
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
702
|
-
\${interactiveStyles};
|
|
703
|
-
|
|
704
|
-
& * {
|
|
705
|
-
margin: auto 0;
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
&:hover {
|
|
709
|
-
color: \${({ theme }) => theme.colors.accent};
|
|
710
|
-
}
|
|
711
|
-
\`;
|
|
712
|
-
|
|
713
|
-
const StyledChatTitle = styled.div<{ theme: Theme }>\`
|
|
714
|
-
display: flex;
|
|
715
|
-
flex-wrap: nowrap;
|
|
716
|
-
justify-content: space-between;
|
|
717
|
-
position: sticky;
|
|
718
|
-
margin: 0 -20px;
|
|
719
|
-
padding: 16px 20px;
|
|
720
|
-
height: 62px;
|
|
721
|
-
top: 0;
|
|
722
|
-
background: \${({ theme }) => theme.colors.light};
|
|
723
|
-
border-bottom: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
724
|
-
z-index: 1000;
|
|
725
|
-
\`;
|
|
726
|
-
|
|
727
|
-
const StyledChatTitleIconWrapper = styled.span<{ theme: Theme }>\`
|
|
728
|
-
display: flex;
|
|
729
|
-
align-items: center;
|
|
730
|
-
gap: 12px;
|
|
731
|
-
color: \${({ theme }) => theme.colors.dark};
|
|
732
|
-
\`;
|
|
733
|
-
|
|
734
|
-
type Source = {
|
|
735
|
-
id: string;
|
|
736
|
-
path: string;
|
|
737
|
-
uri: string;
|
|
738
|
-
score: number;
|
|
739
|
-
};
|
|
740
|
-
|
|
741
|
-
type Answer = {
|
|
742
|
-
text: string;
|
|
743
|
-
answer?: boolean;
|
|
744
|
-
mdx?: MDXRemoteSerializeResult;
|
|
745
|
-
sources?: Source[];
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
const SPARKLE_COLORS = [
|
|
749
|
-
"#ff6b6b",
|
|
750
|
-
"#feca57",
|
|
751
|
-
"#48dbfb",
|
|
752
|
-
"#ff9ff3",
|
|
753
|
-
"#54a0ff",
|
|
754
|
-
"#5f27cd",
|
|
755
|
-
];
|
|
756
|
-
|
|
757
|
-
// Deterministic sparkle positions to avoid hydration mismatch
|
|
758
|
-
const SPARKLE_POSITIONS = [
|
|
759
|
-
{ left: 8, top: 35 },
|
|
760
|
-
{ left: 17, top: 55 },
|
|
761
|
-
{ left: 26, top: 28 },
|
|
762
|
-
{ left: 35, top: 68 },
|
|
763
|
-
{ left: 44, top: 42 },
|
|
764
|
-
{ left: 53, top: 75 },
|
|
765
|
-
{ left: 62, top: 32 },
|
|
766
|
-
{ left: 71, top: 58 },
|
|
767
|
-
{ left: 80, top: 45 },
|
|
768
|
-
{ left: 89, top: 65 },
|
|
769
|
-
];
|
|
770
|
-
|
|
771
|
-
interface RainbowInputProps {
|
|
772
|
-
id?: string;
|
|
773
|
-
value: string;
|
|
774
|
-
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
775
|
-
placeholder?: string;
|
|
776
|
-
autoComplete?: string;
|
|
777
|
-
"aria-label"?: string;
|
|
778
|
-
inputRef?: React.Ref<HTMLInputElement>;
|
|
779
164
|
}
|
|
780
165
|
|
|
781
|
-
function
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
placeholder,
|
|
786
|
-
autoComplete,
|
|
787
|
-
"aria-label": ariaLabel,
|
|
788
|
-
inputRef,
|
|
789
|
-
}: RainbowInputProps) {
|
|
790
|
-
const [isFocused, setIsFocused] = useState(false);
|
|
791
|
-
const [isHovered, setIsHovered] = useState(false);
|
|
792
|
-
const isActive = isFocused || isHovered;
|
|
793
|
-
|
|
794
|
-
const sparkles = SPARKLE_POSITIONS.map((pos, i) => ({
|
|
795
|
-
color: SPARKLE_COLORS[i % SPARKLE_COLORS.length],
|
|
796
|
-
left: pos.left,
|
|
797
|
-
top: pos.top,
|
|
798
|
-
delay: i * 0.12,
|
|
799
|
-
}));
|
|
166
|
+
function ChatSourceLink({ source }: { source: ChatSourceData }) {
|
|
167
|
+
const { close } = useChat();
|
|
168
|
+
const isMobileChat = useBelowBreakpoint("lg");
|
|
169
|
+
const router = useRouter();
|
|
800
170
|
|
|
801
171
|
return (
|
|
802
|
-
<
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
172
|
+
<ChatSource
|
|
173
|
+
href={source.href}
|
|
174
|
+
onClick={(event) => {
|
|
175
|
+
if (
|
|
176
|
+
event.defaultPrevented ||
|
|
177
|
+
event.button !== 0 ||
|
|
178
|
+
event.metaKey ||
|
|
179
|
+
event.ctrlKey ||
|
|
180
|
+
event.shiftKey ||
|
|
181
|
+
event.altKey
|
|
182
|
+
) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// ChatSource renders a plain anchor, so route unmodified left clicks
|
|
186
|
+
// client-side to keep the transcript alive; below lg the panel is a
|
|
187
|
+
// fullscreen dialog, so close it to reveal the navigated page.
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
if (isMobileChat) close();
|
|
190
|
+
router.push(source.href);
|
|
191
|
+
}}
|
|
806
192
|
>
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
<StyledSparkle
|
|
810
|
-
key={i}
|
|
811
|
-
$color={sparkle.color}
|
|
812
|
-
$left={sparkle.left}
|
|
813
|
-
$top={sparkle.top}
|
|
814
|
-
$delay={sparkle.delay}
|
|
815
|
-
/>
|
|
816
|
-
))}
|
|
817
|
-
</StyledSparkleContainer>
|
|
818
|
-
<StyledRainbowInput
|
|
819
|
-
ref={inputRef}
|
|
820
|
-
id={id}
|
|
821
|
-
value={value}
|
|
822
|
-
onChange={onChange}
|
|
823
|
-
placeholder={placeholder}
|
|
824
|
-
autoComplete={autoComplete}
|
|
825
|
-
aria-label={ariaLabel}
|
|
826
|
-
onFocus={() => setIsFocused(true)}
|
|
827
|
-
onBlur={() => setIsFocused(false)}
|
|
828
|
-
/>
|
|
829
|
-
</StyledRainbowInputWrapper>
|
|
193
|
+
{source.label}
|
|
194
|
+
</ChatSource>
|
|
830
195
|
);
|
|
831
196
|
}
|
|
832
197
|
|
|
833
|
-
function
|
|
834
|
-
const {
|
|
198
|
+
function ChatTranscript() {
|
|
199
|
+
const { messages, loading, error } = useChat();
|
|
200
|
+
const lastMessage = messages[messages.length - 1];
|
|
835
201
|
|
|
836
202
|
return (
|
|
837
|
-
<
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
203
|
+
<ChatMessageList>
|
|
204
|
+
{messages.map((message) => (
|
|
205
|
+
<ChatMessage key={message.id} $role={message.role}>
|
|
206
|
+
{message.role === "assistant" ? (
|
|
207
|
+
// The docs' typographic context lives on the docs page wrapper,
|
|
208
|
+
// so a rendered MDX answer would arrive margin-less here. Prose
|
|
209
|
+
// $compact is Cherry's document styling for chat replies: a 10px
|
|
210
|
+
// rhythm between elements, inline-code chips, lists, and tables.
|
|
211
|
+
<Prose $compact>{message.content}</Prose>
|
|
212
|
+
) : (
|
|
213
|
+
message.content
|
|
214
|
+
)}
|
|
215
|
+
{message.role === "assistant" &&
|
|
216
|
+
message.sources &&
|
|
217
|
+
message.sources.length > 0 && (
|
|
218
|
+
<ChatSources>
|
|
219
|
+
{message.sources.map((source) => (
|
|
220
|
+
<ChatSourceLink key={source.id} source={source} />
|
|
221
|
+
))}
|
|
222
|
+
</ChatSources>
|
|
223
|
+
)}
|
|
224
|
+
</ChatMessage>
|
|
225
|
+
))}
|
|
226
|
+
{loading && lastMessage?.role !== "assistant" && <ChatTyping />}
|
|
227
|
+
{error && (
|
|
228
|
+
<Callout type="danger">
|
|
229
|
+
<p>
|
|
230
|
+
<strong>Error:</strong> {error}
|
|
231
|
+
</p>
|
|
232
|
+
</Callout>
|
|
233
|
+
)}
|
|
234
|
+
</ChatMessageList>
|
|
848
235
|
);
|
|
849
236
|
}
|
|
850
237
|
|
|
851
|
-
function
|
|
852
|
-
const {
|
|
853
|
-
isOpen,
|
|
854
|
-
question,
|
|
855
|
-
setQuestion,
|
|
856
|
-
loading,
|
|
857
|
-
error,
|
|
858
|
-
answer,
|
|
859
|
-
ask,
|
|
860
|
-
closeChat,
|
|
861
|
-
resetChat,
|
|
862
|
-
chatInputRef,
|
|
863
|
-
} = useContext(ChatContext);
|
|
864
|
-
const chatSurfaceRef = useRef<HTMLDivElement | null>(null);
|
|
865
|
-
const chatScrollRef = useRef<HTMLDivElement | null>(null);
|
|
866
|
-
const scrollFrameRef = useRef<number | null>(null);
|
|
867
|
-
const shouldAutoScrollRef = useRef(true);
|
|
868
|
-
const loadingRef = useRef(loading);
|
|
869
|
-
const isMobileChat = useIsMobileChat();
|
|
870
|
-
const isMobileModal = isOpen && isMobileChat;
|
|
871
|
-
|
|
872
|
-
useLockBodyScroll(isOpen);
|
|
873
|
-
|
|
874
|
-
useEffect(() => {
|
|
875
|
-
loadingRef.current = loading;
|
|
876
|
-
}, [loading]);
|
|
877
|
-
|
|
878
|
-
useLayoutEffect(() => {
|
|
879
|
-
if (isOpen) {
|
|
880
|
-
shouldAutoScrollRef.current = true;
|
|
881
|
-
return;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
if (scrollFrameRef.current !== null) {
|
|
885
|
-
cancelAnimationFrame(scrollFrameRef.current);
|
|
886
|
-
scrollFrameRef.current = null;
|
|
887
|
-
}
|
|
888
|
-
}, [isOpen]);
|
|
889
|
-
|
|
890
|
-
useEffect(() => {
|
|
891
|
-
if (!isOpen || !shouldAutoScrollRef.current) return;
|
|
892
|
-
if (scrollFrameRef.current !== null) return;
|
|
893
|
-
|
|
894
|
-
scrollFrameRef.current = requestAnimationFrame(() => {
|
|
895
|
-
scrollFrameRef.current = null;
|
|
896
|
-
const scrollContainer = chatScrollRef.current;
|
|
897
|
-
if (!scrollContainer || !shouldAutoScrollRef.current) return;
|
|
898
|
-
const reduceMotion = window.matchMedia(
|
|
899
|
-
"(prefers-reduced-motion: reduce)",
|
|
900
|
-
).matches;
|
|
901
|
-
scrollContainer.scrollTo({
|
|
902
|
-
top: scrollContainer.scrollHeight,
|
|
903
|
-
behavior: reduceMotion || loadingRef.current ? "auto" : "smooth",
|
|
904
|
-
});
|
|
905
|
-
});
|
|
906
|
-
}, [answer, isOpen, loading]);
|
|
907
|
-
|
|
908
|
-
useEffect(() => {
|
|
909
|
-
return () => {
|
|
910
|
-
if (scrollFrameRef.current !== null) {
|
|
911
|
-
cancelAnimationFrame(scrollFrameRef.current);
|
|
912
|
-
}
|
|
913
|
-
};
|
|
914
|
-
}, []);
|
|
915
|
-
|
|
916
|
-
useEffect(() => {
|
|
917
|
-
if (!isMobileModal || !chatSurfaceRef.current) return;
|
|
918
|
-
|
|
919
|
-
const surface = chatSurfaceRef.current;
|
|
920
|
-
const inertSiblings: Array<{
|
|
921
|
-
element: HTMLElement;
|
|
922
|
-
wasInert: boolean;
|
|
923
|
-
}> = [];
|
|
924
|
-
let current: HTMLElement = surface;
|
|
925
|
-
|
|
926
|
-
while (current.parentElement) {
|
|
927
|
-
const parent = current.parentElement;
|
|
928
|
-
for (const sibling of Array.from(parent.children)) {
|
|
929
|
-
if (sibling === current || !(sibling instanceof HTMLElement)) continue;
|
|
930
|
-
inertSiblings.push({ element: sibling, wasInert: sibling.inert });
|
|
931
|
-
sibling.inert = true;
|
|
932
|
-
}
|
|
933
|
-
if (parent === document.body) break;
|
|
934
|
-
current = parent;
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
const containFocus = (event: FocusEvent) => {
|
|
938
|
-
if (event.target instanceof Node && !surface.contains(event.target)) {
|
|
939
|
-
chatInputRef.current?.focus();
|
|
940
|
-
}
|
|
941
|
-
};
|
|
942
|
-
|
|
943
|
-
document.addEventListener("focusin", containFocus);
|
|
944
|
-
chatInputRef.current?.focus();
|
|
945
|
-
|
|
946
|
-
return () => {
|
|
947
|
-
document.removeEventListener("focusin", containFocus);
|
|
948
|
-
for (const { element, wasInert } of inertSiblings) {
|
|
949
|
-
element.inert = wasInert;
|
|
950
|
-
}
|
|
951
|
-
};
|
|
952
|
-
}, [chatInputRef, isMobileModal]);
|
|
953
|
-
|
|
954
|
-
const handleChatScroll = () => {
|
|
955
|
-
const scrollContainer = chatScrollRef.current;
|
|
956
|
-
if (!scrollContainer) return;
|
|
957
|
-
const distanceFromBottom =
|
|
958
|
-
scrollContainer.scrollHeight -
|
|
959
|
-
scrollContainer.scrollTop -
|
|
960
|
-
scrollContainer.clientHeight;
|
|
961
|
-
shouldAutoScrollRef.current =
|
|
962
|
-
distanceFromBottom <= scrollContainer.clientHeight * 0.15;
|
|
963
|
-
};
|
|
964
|
-
|
|
965
|
-
const handleSurfaceKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
966
|
-
if (event.key === "Escape" && isMobileModal) {
|
|
967
|
-
event.preventDefault();
|
|
968
|
-
event.stopPropagation();
|
|
969
|
-
closeChat();
|
|
970
|
-
return;
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
if (event.key !== "Tab" || !isMobileModal) return;
|
|
974
|
-
const focusable = Array.from(
|
|
975
|
-
chatSurfaceRef.current?.querySelectorAll<HTMLElement>(
|
|
976
|
-
'a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])',
|
|
977
|
-
) ?? [],
|
|
978
|
-
).filter(
|
|
979
|
-
(element) =>
|
|
980
|
-
element.offsetParent !== null &&
|
|
981
|
-
element.getAttribute("aria-hidden") !== "true",
|
|
982
|
-
);
|
|
983
|
-
if (focusable.length === 0) {
|
|
984
|
-
event.preventDefault();
|
|
985
|
-
return;
|
|
986
|
-
}
|
|
987
|
-
const first = focusable[0];
|
|
988
|
-
const last = focusable[focusable.length - 1];
|
|
989
|
-
if (event.shiftKey && document.activeElement === first) {
|
|
990
|
-
event.preventDefault();
|
|
991
|
-
last.focus();
|
|
992
|
-
} else if (!event.shiftKey && document.activeElement === last) {
|
|
993
|
-
event.preventDefault();
|
|
994
|
-
first.focus();
|
|
995
|
-
}
|
|
996
|
-
};
|
|
238
|
+
function ChatActions() {
|
|
239
|
+
const { reset } = useChat();
|
|
997
240
|
|
|
998
241
|
return (
|
|
999
|
-
<
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
aria-hidden={!isOpen}
|
|
1004
|
-
role={isMobileModal ? "dialog" : "complementary"}
|
|
1005
|
-
aria-modal={isMobileModal ? "true" : undefined}
|
|
1006
|
-
aria-label="AI Assistant"
|
|
1007
|
-
data-chat-surface
|
|
1008
|
-
data-markdown-ignore
|
|
1009
|
-
onKeyDownCapture={handleSurfaceKeyDown}
|
|
242
|
+
<IconButton
|
|
243
|
+
onClick={reset}
|
|
244
|
+
aria-label="Reset chat history"
|
|
245
|
+
title="Reset chat history"
|
|
1010
246
|
>
|
|
1011
|
-
<
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
>
|
|
1016
|
-
<StyledChatTitle>
|
|
1017
|
-
<StyledChatTitleIconWrapper>
|
|
1018
|
-
<Sparkles />
|
|
1019
|
-
<h3>AI Assistant</h3>
|
|
1020
|
-
</StyledChatTitleIconWrapper>
|
|
1021
|
-
<StyledChatTitleIconWrapper>
|
|
1022
|
-
<IconButton
|
|
1023
|
-
onClick={resetChat}
|
|
1024
|
-
aria-label="Reset chat history"
|
|
1025
|
-
title="Reset chat history"
|
|
1026
|
-
>
|
|
1027
|
-
<RotateCcw />
|
|
1028
|
-
</IconButton>
|
|
1029
|
-
<IconButton
|
|
1030
|
-
onClick={() => closeChat()}
|
|
1031
|
-
aria-label="Close chat"
|
|
1032
|
-
title="Close chat"
|
|
1033
|
-
>
|
|
1034
|
-
<X />
|
|
1035
|
-
</IconButton>
|
|
1036
|
-
</StyledChatTitleIconWrapper>
|
|
1037
|
-
</StyledChatTitle>
|
|
1038
|
-
{answer &&
|
|
1039
|
-
answer.map((a, i) => (
|
|
1040
|
-
<React.Fragment key={i}>
|
|
1041
|
-
<StyledAnswer $isAnswer={a.answer ?? false}>
|
|
1042
|
-
{a.answer && a.mdx ? (
|
|
1043
|
-
<MDXRemote {...a.mdx} components={mdxComponents} />
|
|
1044
|
-
) : (
|
|
1045
|
-
a.text
|
|
1046
|
-
)}
|
|
1047
|
-
</StyledAnswer>
|
|
1048
|
-
{a.answer && a.sources && a.sources.length > 0 && (
|
|
1049
|
-
<StyledSources>
|
|
1050
|
-
{a.sources.map((src) => {
|
|
1051
|
-
const slug = src.uri
|
|
1052
|
-
.replace("docs://", "")
|
|
1053
|
-
.replace(/^\\/+/, "");
|
|
1054
|
-
const href = slug ? \`/\${slug}/\` : "/";
|
|
1055
|
-
const label = slug
|
|
1056
|
-
? slug
|
|
1057
|
-
.split("/")
|
|
1058
|
-
.pop()!
|
|
1059
|
-
.replace(/-/g, " ")
|
|
1060
|
-
.replace(/\\b\\w/g, (c: string) => c.toUpperCase())
|
|
1061
|
-
: "Home";
|
|
1062
|
-
return (
|
|
1063
|
-
<StyledSourceLink
|
|
1064
|
-
key={src.id}
|
|
1065
|
-
href={href}
|
|
1066
|
-
onClick={() => {
|
|
1067
|
-
if (window.innerWidth <= 992) {
|
|
1068
|
-
closeChat();
|
|
1069
|
-
}
|
|
1070
|
-
}}
|
|
1071
|
-
>
|
|
1072
|
-
{label}
|
|
1073
|
-
</StyledSourceLink>
|
|
1074
|
-
);
|
|
1075
|
-
})}
|
|
1076
|
-
</StyledSources>
|
|
1077
|
-
)}
|
|
1078
|
-
</React.Fragment>
|
|
1079
|
-
))}
|
|
1080
|
-
{loading && !answer[answer.length - 1]?.answer && (
|
|
1081
|
-
<StyledLoading>
|
|
1082
|
-
Answering<span>.</span>
|
|
1083
|
-
<span>.</span>
|
|
1084
|
-
<span>.</span>
|
|
1085
|
-
</StyledLoading>
|
|
1086
|
-
)}
|
|
1087
|
-
{error && (
|
|
1088
|
-
<StyledError>
|
|
1089
|
-
<Callout type="danger">
|
|
1090
|
-
<p>
|
|
1091
|
-
<strong>Error:</strong> {error}
|
|
1092
|
-
</p>
|
|
1093
|
-
</Callout>
|
|
1094
|
-
</StyledError>
|
|
1095
|
-
)}
|
|
1096
|
-
</StyledChat>
|
|
247
|
+
<RotateCcw />
|
|
248
|
+
</IconButton>
|
|
249
|
+
);
|
|
250
|
+
}
|
|
1097
251
|
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
placeholder="Ask AI Assistant..."
|
|
1105
|
-
autoComplete="off"
|
|
1106
|
-
aria-label="Ask a follow-up question"
|
|
1107
|
-
/>
|
|
1108
|
-
<StyledRainbowButton
|
|
1109
|
-
type="submit"
|
|
1110
|
-
disabled={loading || question.trim() === ""}
|
|
1111
|
-
$hasContent={question.trim().length > 0}
|
|
1112
|
-
aria-label={loading ? "Loading response" : "Submit question"}
|
|
1113
|
-
>
|
|
1114
|
-
{loading ? <LoaderPinwheel className="loading" /> : <ArrowUp />}
|
|
1115
|
-
</StyledRainbowButton>
|
|
1116
|
-
</StyledChatForm>
|
|
1117
|
-
</StyledChatSurface>
|
|
252
|
+
function Chat() {
|
|
253
|
+
return (
|
|
254
|
+
<ChatPanel $title="AI Assistant" $actions={<ChatActions />}>
|
|
255
|
+
<ChatTranscript />
|
|
256
|
+
<ChatInput $glow />
|
|
257
|
+
</ChatPanel>
|
|
1118
258
|
);
|
|
1119
259
|
}
|
|
1120
260
|
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
question: string;
|
|
1127
|
-
setQuestion: (q: string) => void;
|
|
1128
|
-
loading: boolean;
|
|
1129
|
-
error: string | null;
|
|
1130
|
-
answer: Answer[];
|
|
1131
|
-
setAnswer: (answers: Answer[]) => void;
|
|
1132
|
-
ask: (e: React.FormEvent) => void;
|
|
1133
|
-
askAssistant: (question: string, returnFocusTo?: HTMLElement | null) => void;
|
|
1134
|
-
closeChat: (restoreFocus?: boolean) => HTMLElement | null;
|
|
1135
|
-
registerSearchClose: (
|
|
1136
|
-
closeSearch: (restoreFocus?: boolean) => HTMLElement | null,
|
|
1137
|
-
) => () => void;
|
|
1138
|
-
resetChat: () => void;
|
|
1139
|
-
chatInputRef: React.RefObject<HTMLInputElement | null>;
|
|
1140
|
-
}>({
|
|
1141
|
-
isOpen: false,
|
|
1142
|
-
setIsOpen: () => {},
|
|
1143
|
-
toggleChat: () => {},
|
|
261
|
+
// App-level chat context: only whether the AI assistant is enabled (an LLM
|
|
262
|
+
// provider is configured). Panel state, the transcript, and streaming all
|
|
263
|
+
// live in Cherry's ChatProvider - read those with useChat() from
|
|
264
|
+
// "cherry-styled-components".
|
|
265
|
+
const ChatContext = createContext<{ isChatActive: boolean }>({
|
|
1144
266
|
isChatActive: false,
|
|
1145
|
-
question: "",
|
|
1146
|
-
setQuestion: () => {},
|
|
1147
|
-
loading: false,
|
|
1148
|
-
error: null,
|
|
1149
|
-
answer: [],
|
|
1150
|
-
setAnswer: () => {},
|
|
1151
|
-
ask: () => {},
|
|
1152
|
-
askAssistant: () => {},
|
|
1153
|
-
closeChat: () => null,
|
|
1154
|
-
registerSearchClose: () => () => {},
|
|
1155
|
-
resetChat: () => {},
|
|
1156
|
-
chatInputRef: { current: null },
|
|
1157
267
|
});
|
|
1158
268
|
|
|
1159
269
|
interface ChatContextProviderProps {
|
|
@@ -1161,361 +271,20 @@ interface ChatContextProviderProps {
|
|
|
1161
271
|
isChatActive: boolean;
|
|
1162
272
|
}
|
|
1163
273
|
|
|
1164
|
-
const INITIAL_GREETING = "Hey there, how can I assist you?";
|
|
1165
|
-
|
|
1166
274
|
const ChtProvider = ({ children, isChatActive }: ChatContextProviderProps) => {
|
|
1167
|
-
const
|
|
1168
|
-
const [question, setQuestion] = useState("");
|
|
1169
|
-
const [loading, setLoading] = useState(false);
|
|
1170
|
-
const [error, setError] = useState<string | null>(null);
|
|
1171
|
-
const [answer, setAnswer] = useState<Answer[]>([]);
|
|
1172
|
-
const abortRef = useRef<AbortController | null>(null);
|
|
1173
|
-
const chatInputRef = useRef<HTMLInputElement | null>(null);
|
|
1174
|
-
const focusTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
1175
|
-
const restoreFocusTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(
|
|
1176
|
-
null,
|
|
1177
|
-
);
|
|
1178
|
-
const pendingRestoreFocusRef = useRef<HTMLElement | null>(null);
|
|
1179
|
-
const openerRef = useRef<HTMLElement | null>(null);
|
|
1180
|
-
const closeSearchRef = useRef<
|
|
1181
|
-
((restoreFocus?: boolean) => HTMLElement | null) | null
|
|
1182
|
-
>(null);
|
|
1183
|
-
const isOpenRef = useRef(isOpen);
|
|
1184
|
-
|
|
1185
|
-
useEffect(() => {
|
|
1186
|
-
isOpenRef.current = isOpen;
|
|
1187
|
-
}, [isOpen]);
|
|
1188
|
-
|
|
1189
|
-
const showChat = useCallback((returnFocusTo?: HTMLElement | null) => {
|
|
1190
|
-
const previousOverlayOpener = closeSearchRef.current?.(false) ?? null;
|
|
1191
|
-
const pendingChatOpener = pendingRestoreFocusRef.current;
|
|
1192
|
-
if (restoreFocusTimeoutRef.current) {
|
|
1193
|
-
clearTimeout(restoreFocusTimeoutRef.current);
|
|
1194
|
-
restoreFocusTimeoutRef.current = null;
|
|
1195
|
-
}
|
|
1196
|
-
pendingRestoreFocusRef.current = null;
|
|
1197
|
-
if (!isOpenRef.current) {
|
|
1198
|
-
const activeElement = document.activeElement;
|
|
1199
|
-
openerRef.current =
|
|
1200
|
-
returnFocusTo?.isConnected === true
|
|
1201
|
-
? returnFocusTo
|
|
1202
|
-
: previousOverlayOpener?.isConnected === true
|
|
1203
|
-
? previousOverlayOpener
|
|
1204
|
-
: pendingChatOpener?.isConnected === true
|
|
1205
|
-
? pendingChatOpener
|
|
1206
|
-
: activeElement instanceof HTMLElement &&
|
|
1207
|
-
activeElement !== document.body
|
|
1208
|
-
? activeElement
|
|
1209
|
-
: null;
|
|
1210
|
-
}
|
|
1211
|
-
isOpenRef.current = true;
|
|
1212
|
-
setIsOpen(true);
|
|
1213
|
-
if (focusTimeoutRef.current) clearTimeout(focusTimeoutRef.current);
|
|
1214
|
-
focusTimeoutRef.current = setTimeout(() => {
|
|
1215
|
-
focusTimeoutRef.current = null;
|
|
1216
|
-
chatInputRef.current?.focus();
|
|
1217
|
-
}, 0);
|
|
1218
|
-
}, []);
|
|
1219
|
-
|
|
1220
|
-
// Open the assistant and seed its greeting on the first visit. The opener is
|
|
1221
|
-
// captured before the mobile dialog makes the rest of the page inert.
|
|
1222
|
-
const openChat = useCallback(() => {
|
|
1223
|
-
showChat();
|
|
1224
|
-
setAnswer((prev) =>
|
|
1225
|
-
prev.length === 0 ? [{ text: INITIAL_GREETING, answer: true }] : prev,
|
|
1226
|
-
);
|
|
1227
|
-
}, [showChat]);
|
|
1228
|
-
|
|
1229
|
-
const closeChat = useCallback((restoreFocus = true) => {
|
|
1230
|
-
const pendingOpener = pendingRestoreFocusRef.current;
|
|
1231
|
-
if (!restoreFocus && restoreFocusTimeoutRef.current) {
|
|
1232
|
-
clearTimeout(restoreFocusTimeoutRef.current);
|
|
1233
|
-
restoreFocusTimeoutRef.current = null;
|
|
1234
|
-
pendingRestoreFocusRef.current = null;
|
|
1235
|
-
}
|
|
1236
|
-
if (!isOpenRef.current) return pendingOpener;
|
|
1237
|
-
if (focusTimeoutRef.current) {
|
|
1238
|
-
clearTimeout(focusTimeoutRef.current);
|
|
1239
|
-
focusTimeoutRef.current = null;
|
|
1240
|
-
}
|
|
1241
|
-
const activeElement = document.activeElement;
|
|
1242
|
-
if (
|
|
1243
|
-
activeElement instanceof HTMLElement &&
|
|
1244
|
-
activeElement.closest("[data-chat-surface]")
|
|
1245
|
-
) {
|
|
1246
|
-
activeElement.blur();
|
|
1247
|
-
}
|
|
1248
|
-
const opener = openerRef.current;
|
|
1249
|
-
openerRef.current = null;
|
|
1250
|
-
isOpenRef.current = false;
|
|
1251
|
-
setIsOpen(false);
|
|
1252
|
-
if (restoreFocusTimeoutRef.current) {
|
|
1253
|
-
clearTimeout(restoreFocusTimeoutRef.current);
|
|
1254
|
-
}
|
|
1255
|
-
if (restoreFocus) {
|
|
1256
|
-
pendingRestoreFocusRef.current = opener;
|
|
1257
|
-
restoreFocusTimeoutRef.current = setTimeout(() => {
|
|
1258
|
-
const focusTarget = pendingRestoreFocusRef.current;
|
|
1259
|
-
restoreFocusTimeoutRef.current = null;
|
|
1260
|
-
pendingRestoreFocusRef.current = null;
|
|
1261
|
-
if (focusTarget?.isConnected) focusTarget.focus();
|
|
1262
|
-
}, 0);
|
|
1263
|
-
} else {
|
|
1264
|
-
pendingRestoreFocusRef.current = null;
|
|
1265
|
-
}
|
|
1266
|
-
return opener;
|
|
1267
|
-
}, []);
|
|
1268
|
-
|
|
1269
|
-
const registerSearchClose = useCallback(
|
|
1270
|
-
(closeSearch: (restoreFocus?: boolean) => HTMLElement | null) => {
|
|
1271
|
-
closeSearchRef.current = closeSearch;
|
|
1272
|
-
return () => {
|
|
1273
|
-
if (closeSearchRef.current === closeSearch) {
|
|
1274
|
-
closeSearchRef.current = null;
|
|
1275
|
-
}
|
|
1276
|
-
};
|
|
1277
|
-
},
|
|
1278
|
-
[],
|
|
1279
|
-
);
|
|
1280
|
-
|
|
1281
|
-
const toggleChat = useCallback(() => {
|
|
1282
|
-
if (isOpenRef.current) {
|
|
1283
|
-
closeChat();
|
|
1284
|
-
} else {
|
|
1285
|
-
openChat();
|
|
1286
|
-
}
|
|
1287
|
-
}, [closeChat, openChat]);
|
|
1288
|
-
|
|
1289
|
-
useEffect(() => {
|
|
1290
|
-
return () => {
|
|
1291
|
-
if (focusTimeoutRef.current) clearTimeout(focusTimeoutRef.current);
|
|
1292
|
-
if (restoreFocusTimeoutRef.current) {
|
|
1293
|
-
clearTimeout(restoreFocusTimeoutRef.current);
|
|
1294
|
-
}
|
|
1295
|
-
};
|
|
1296
|
-
}, []);
|
|
1297
|
-
|
|
1298
|
-
// Global Cmd/Ctrl+I toggles the assistant, but only when chat is enabled.
|
|
1299
|
-
useEffect(() => {
|
|
1300
|
-
if (!isChatActive) return;
|
|
1301
|
-
function handleKeyDown(e: KeyboardEvent) {
|
|
1302
|
-
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "i") {
|
|
1303
|
-
e.preventDefault();
|
|
1304
|
-
toggleChat();
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
document.addEventListener("keydown", handleKeyDown);
|
|
1308
|
-
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
1309
|
-
}, [isChatActive, toggleChat]);
|
|
1310
|
-
|
|
1311
|
-
async function runQuestion(
|
|
1312
|
-
currentQuestion: string,
|
|
1313
|
-
returnFocusTo?: HTMLElement | null,
|
|
1314
|
-
) {
|
|
1315
|
-
setQuestion("");
|
|
1316
|
-
showChat(returnFocusTo);
|
|
1317
|
-
setLoading(true);
|
|
1318
|
-
setError(null);
|
|
1319
|
-
|
|
1320
|
-
const base =
|
|
1321
|
-
answer.length > 0 ? answer : [{ text: INITIAL_GREETING, answer: true }];
|
|
1322
|
-
const mergedQuestions = [...base, { text: currentQuestion, answer: false }];
|
|
1323
|
-
|
|
1324
|
-
setAnswer(mergedQuestions);
|
|
1325
|
-
|
|
1326
|
-
const controller = new AbortController();
|
|
1327
|
-
abortRef.current = controller;
|
|
1328
|
-
|
|
1329
|
-
try {
|
|
1330
|
-
const history = answer
|
|
1331
|
-
.filter((a) => a.text.trim() !== "")
|
|
1332
|
-
.slice(-20)
|
|
1333
|
-
.map((a) => ({
|
|
1334
|
-
role: a.answer ? ("assistant" as const) : ("user" as const),
|
|
1335
|
-
content: a.text.slice(0, 4000),
|
|
1336
|
-
}));
|
|
1337
|
-
|
|
1338
|
-
const res = await fetch("/api/rag", {
|
|
1339
|
-
method: "POST",
|
|
1340
|
-
headers: { "Content-Type": "application/json" },
|
|
1341
|
-
body: JSON.stringify({ question: currentQuestion, history }),
|
|
1342
|
-
signal: controller.signal,
|
|
1343
|
-
});
|
|
1344
|
-
|
|
1345
|
-
if (!res.ok) {
|
|
1346
|
-
// The body may be a proxy/gateway HTML error page (e.g. Cloudflare
|
|
1347
|
-
// 524), not our JSON - never blindly JSON.parse it.
|
|
1348
|
-
const contentType = res.headers.get("content-type") || "";
|
|
1349
|
-
let message = "Request failed";
|
|
1350
|
-
if (contentType.includes("application/json")) {
|
|
1351
|
-
try {
|
|
1352
|
-
const errorData = await res.json();
|
|
1353
|
-
message = errorData.error || message;
|
|
1354
|
-
} catch {
|
|
1355
|
-
// Non-JSON despite the header - fall through to the default.
|
|
1356
|
-
}
|
|
1357
|
-
} else if ([502, 503, 504, 524].includes(res.status)) {
|
|
1358
|
-
message = "The assistant took too long to respond. Please try again.";
|
|
1359
|
-
}
|
|
1360
|
-
throw new Error(message);
|
|
1361
|
-
}
|
|
1362
|
-
|
|
1363
|
-
const reader = res.body?.getReader();
|
|
1364
|
-
const decoder = new TextDecoder();
|
|
1365
|
-
const contentParts: string[] = [];
|
|
1366
|
-
let sources: Source[] = [];
|
|
1367
|
-
if (!reader) {
|
|
1368
|
-
throw new Error("Failed to get response reader");
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
// Don't insert the answer bubble yet - an empty bubble renders a blank
|
|
1372
|
-
// gap above the "Answering..." loader. The bubble is created on the first
|
|
1373
|
-
// streamed token (or the done event) instead, so the loader stays put
|
|
1374
|
-
// until real text appears.
|
|
1375
|
-
const streamingAnswerIndex = mergedQuestions.length;
|
|
1376
|
-
|
|
1377
|
-
let buffer = "";
|
|
1378
|
-
while (true) {
|
|
1379
|
-
const { done, value } = await reader.read();
|
|
1380
|
-
if (done) break;
|
|
1381
|
-
|
|
1382
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1383
|
-
const parts = buffer.split("\\n");
|
|
1384
|
-
buffer = parts.pop() ?? "";
|
|
1385
|
-
|
|
1386
|
-
for (const line of parts) {
|
|
1387
|
-
if (!line.startsWith("data: ")) continue;
|
|
1388
|
-
|
|
1389
|
-
// Guard only the parse: a malformed frame is skipped, while the
|
|
1390
|
-
// server's error event below must throw past this loop to the
|
|
1391
|
-
// outer catch so it renders in the chat's error callout instead
|
|
1392
|
-
// of dying in a parse-error log.
|
|
1393
|
-
let data;
|
|
1394
|
-
try {
|
|
1395
|
-
data = JSON.parse(line.slice(6));
|
|
1396
|
-
} catch {
|
|
1397
|
-
console.error("Failed to parse SSE data:", line);
|
|
1398
|
-
continue;
|
|
1399
|
-
}
|
|
1400
|
-
|
|
1401
|
-
if (data.type === "metadata") {
|
|
1402
|
-
const allSources: Source[] = data.data?.sources ?? [];
|
|
1403
|
-
const seen = new Set<string>();
|
|
1404
|
-
sources = allSources.filter((s: Source) => {
|
|
1405
|
-
if (s.score < 0.4 || seen.has(s.uri)) return false;
|
|
1406
|
-
seen.add(s.uri);
|
|
1407
|
-
return true;
|
|
1408
|
-
});
|
|
1409
|
-
} else if (data.type === "content") {
|
|
1410
|
-
contentParts.push(data.data);
|
|
1411
|
-
const streamedContent = contentParts.join("");
|
|
1412
|
-
|
|
1413
|
-
setAnswer((prev) => {
|
|
1414
|
-
const newAnswers = [...prev];
|
|
1415
|
-
newAnswers[streamingAnswerIndex] = {
|
|
1416
|
-
text: streamedContent,
|
|
1417
|
-
answer: true,
|
|
1418
|
-
sources,
|
|
1419
|
-
};
|
|
1420
|
-
return newAnswers;
|
|
1421
|
-
});
|
|
1422
|
-
} else if (data.type === "error") {
|
|
1423
|
-
throw new Error(data.data);
|
|
1424
|
-
} else if (data.type === "done") {
|
|
1425
|
-
const streamedContent = contentParts.join("");
|
|
1426
|
-
let mdxSource: MDXRemoteSerializeResult | null = null;
|
|
1427
|
-
try {
|
|
1428
|
-
mdxSource = await serialize(streamedContent, {
|
|
1429
|
-
parseFrontmatter: false,
|
|
1430
|
-
mdxOptions: {
|
|
1431
|
-
remarkPlugins: [remarkGfm],
|
|
1432
|
-
rehypePlugins: [rehypeHighlight],
|
|
1433
|
-
format: "md",
|
|
1434
|
-
development: false,
|
|
1435
|
-
},
|
|
1436
|
-
});
|
|
1437
|
-
} catch (mdxError: unknown) {
|
|
1438
|
-
console.error("MDX serialization error:", mdxError);
|
|
1439
|
-
}
|
|
1440
|
-
|
|
1441
|
-
setAnswer((prev) => {
|
|
1442
|
-
const newAnswers = [...prev];
|
|
1443
|
-
newAnswers[streamingAnswerIndex] = {
|
|
1444
|
-
text: streamedContent,
|
|
1445
|
-
answer: true,
|
|
1446
|
-
mdx: mdxSource || undefined,
|
|
1447
|
-
sources,
|
|
1448
|
-
};
|
|
1449
|
-
return newAnswers;
|
|
1450
|
-
});
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
} catch (err: unknown) {
|
|
1455
|
-
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
1456
|
-
setError(err instanceof Error ? err.message : "Unknown error");
|
|
1457
|
-
} finally {
|
|
1458
|
-
abortRef.current = null;
|
|
1459
|
-
setLoading(false);
|
|
1460
|
-
}
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
async function ask(e: React.FormEvent) {
|
|
1464
|
-
e.preventDefault();
|
|
1465
|
-
if (loading || question.trim() === "") return;
|
|
1466
|
-
await runQuestion(question);
|
|
1467
|
-
}
|
|
1468
|
-
|
|
1469
|
-
// Bridge from the search modal: hand the typed query to the assistant. If a
|
|
1470
|
-
// response is already streaming (loading), we can't submit yet - open the
|
|
1471
|
-
// panel and pre-fill the input so the user can send it the moment the
|
|
1472
|
-
// current answer finishes. Otherwise submit it right away.
|
|
1473
|
-
function askAssistant(
|
|
1474
|
-
rawQuestion: string,
|
|
1475
|
-
returnFocusTo?: HTMLElement | null,
|
|
1476
|
-
) {
|
|
1477
|
-
const trimmed = rawQuestion.trim();
|
|
1478
|
-
if (trimmed === "") return;
|
|
1479
|
-
if (loading) {
|
|
1480
|
-
showChat(returnFocusTo);
|
|
1481
|
-
setQuestion(trimmed);
|
|
1482
|
-
} else {
|
|
1483
|
-
void runQuestion(trimmed, returnFocusTo);
|
|
1484
|
-
}
|
|
1485
|
-
}
|
|
1486
|
-
|
|
1487
|
-
function resetChat() {
|
|
1488
|
-
abortRef.current?.abort();
|
|
1489
|
-
setLoading(false);
|
|
1490
|
-
setError(null);
|
|
1491
|
-
setAnswer([{ text: INITIAL_GREETING, answer: true }]);
|
|
1492
|
-
}
|
|
275
|
+
const value = useMemo(() => ({ isChatActive }), [isChatActive]);
|
|
1493
276
|
|
|
1494
277
|
return (
|
|
1495
|
-
<ChatContext.Provider
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
setQuestion,
|
|
1503
|
-
loading,
|
|
1504
|
-
error,
|
|
1505
|
-
answer,
|
|
1506
|
-
setAnswer,
|
|
1507
|
-
ask,
|
|
1508
|
-
askAssistant,
|
|
1509
|
-
closeChat,
|
|
1510
|
-
registerSearchClose,
|
|
1511
|
-
resetChat,
|
|
1512
|
-
chatInputRef,
|
|
1513
|
-
}}
|
|
1514
|
-
>
|
|
1515
|
-
{children}
|
|
278
|
+
<ChatContext.Provider value={value}>
|
|
279
|
+
<ChatProvider
|
|
280
|
+
onSend={sendToAssistant}
|
|
281
|
+
shortcut={isChatActive ? "i" : null}
|
|
282
|
+
>
|
|
283
|
+
{children}
|
|
284
|
+
</ChatProvider>
|
|
1516
285
|
</ChatContext.Provider>
|
|
1517
286
|
);
|
|
1518
287
|
};
|
|
1519
288
|
|
|
1520
|
-
export { Chat, ChtProvider, ChatContext
|
|
289
|
+
export { Chat, ChtProvider, ChatContext };
|
|
1521
290
|
`;
|