@smart-cloud/ai-kit-ui 1.5.13 → 1.5.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LocaleDirectionProvider.d.ts +5 -0
- package/dist/index.cjs +9 -9
- package/dist/index.js +9 -9
- package/dist/locale.d.ts +13 -0
- package/package.json +35 -19
- package/eslint.config.js +0 -18
- package/src/ShadowBoundary.tsx +0 -229
- package/src/ai-chatbot/AiChatbot.tsx +0 -2307
- package/src/ai-chatbot/attachmentStorage.ts +0 -238
- package/src/ai-chatbot/index.tsx +0 -1
- package/src/ai-feature/AiFeature.tsx +0 -2334
- package/src/ai-feature/AiFeatureBorder.tsx +0 -22
- package/src/ai-feature/ProofreadDiff.tsx +0 -118
- package/src/ai-feature/index.tsx +0 -2
- package/src/ai-feature/utils.tsx +0 -20
- package/src/doc-search/DocSearch.tsx +0 -1313
- package/src/doc-search/index.tsx +0 -1
- package/src/doc-search/initialFilters.ts +0 -10
- package/src/errorHandling.ts +0 -300
- package/src/i18n/ar.ts +0 -252
- package/src/i18n/de.ts +0 -255
- package/src/i18n/en.ts +0 -267
- package/src/i18n/es.ts +0 -255
- package/src/i18n/fr.ts +0 -256
- package/src/i18n/he.ts +0 -248
- package/src/i18n/hi.ts +0 -254
- package/src/i18n/hu.ts +0 -267
- package/src/i18n/id.ts +0 -254
- package/src/i18n/index.ts +0 -47
- package/src/i18n/it.ts +0 -257
- package/src/i18n/ja.ts +0 -254
- package/src/i18n/ko.ts +0 -252
- package/src/i18n/nb.ts +0 -252
- package/src/i18n/nl.ts +0 -253
- package/src/i18n/pl.ts +0 -256
- package/src/i18n/pt.ts +0 -252
- package/src/i18n/ru.ts +0 -252
- package/src/i18n/sv.ts +0 -251
- package/src/i18n/th.ts +0 -250
- package/src/i18n/tr.ts +0 -254
- package/src/i18n/ua.ts +0 -255
- package/src/i18n/zh.ts +0 -245
- package/src/index.tsx +0 -7
- package/src/poweredBy.tsx +0 -39
- package/src/styles/ai-kit-ui.css +0 -1006
- package/src/useAiRun.ts +0 -182
- package/src/withAiKitShell.tsx +0 -363
- package/tests/ai-disclosures.test.ts +0 -97
- package/tests/doc-search-initial-filters.test.ts +0 -20
- package/tests/error-handling.test.ts +0 -123
- package/tsconfig.json +0 -32
- package/tsconfig.node.json +0 -13
- package/tsconfig.types.json +0 -13
- package/tsup.config.ts +0 -22
|
@@ -1,1313 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Alert,
|
|
3
|
-
Anchor,
|
|
4
|
-
Button,
|
|
5
|
-
Checkbox,
|
|
6
|
-
Collapse,
|
|
7
|
-
Divider,
|
|
8
|
-
Group,
|
|
9
|
-
Loader,
|
|
10
|
-
Modal,
|
|
11
|
-
MultiSelect,
|
|
12
|
-
Paper,
|
|
13
|
-
Progress,
|
|
14
|
-
Stack,
|
|
15
|
-
Text,
|
|
16
|
-
TextInput,
|
|
17
|
-
Title,
|
|
18
|
-
} from "@mantine/core";
|
|
19
|
-
import {
|
|
20
|
-
AiKitDocSearchIcon,
|
|
21
|
-
CapabilityDecision,
|
|
22
|
-
DEFAULT_DOC_SEARCH_AI_DISCLOSURE,
|
|
23
|
-
type ContextKind,
|
|
24
|
-
dispatchBackend,
|
|
25
|
-
resolveBackend,
|
|
26
|
-
sendSearchMessage,
|
|
27
|
-
type AiKitStatusEvent,
|
|
28
|
-
type DocSearchProps,
|
|
29
|
-
type SearchResult,
|
|
30
|
-
} from "@smart-cloud/ai-kit-core";
|
|
31
|
-
import { I18n } from "aws-amplify/utils";
|
|
32
|
-
import { FC, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
33
|
-
import ReactMarkdown from "react-markdown";
|
|
34
|
-
import rehypeRaw from "rehype-raw";
|
|
35
|
-
import remarkGfm from "remark-gfm";
|
|
36
|
-
|
|
37
|
-
import {
|
|
38
|
-
IconChevronDown,
|
|
39
|
-
IconChevronRight,
|
|
40
|
-
IconMicrophone,
|
|
41
|
-
IconMicrophoneOff,
|
|
42
|
-
IconSearch,
|
|
43
|
-
IconX,
|
|
44
|
-
} from "@tabler/icons-react";
|
|
45
|
-
|
|
46
|
-
import { AiFeatureBorder } from "../ai-feature/AiFeatureBorder";
|
|
47
|
-
import { translations } from "../i18n";
|
|
48
|
-
import { PoweredBy } from "../poweredBy";
|
|
49
|
-
import { useAiRun } from "../useAiRun";
|
|
50
|
-
import { AiKitShellInjectedProps, withAiKitShell } from "../withAiKitShell";
|
|
51
|
-
import { normalizeInitialFilterValues } from "./initialFilters";
|
|
52
|
-
|
|
53
|
-
I18n.putVocabularies(translations);
|
|
54
|
-
|
|
55
|
-
type Props = DocSearchProps & AiKitShellInjectedProps;
|
|
56
|
-
|
|
57
|
-
type MetadataOptions = {
|
|
58
|
-
allowedCategories: Record<string, string[]>;
|
|
59
|
-
allowedTags: string[];
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const metadataOptionsCache = new Map<string, MetadataOptions>();
|
|
63
|
-
const metadataOptionsRequestCache = new Map<string, Promise<MetadataOptions>>();
|
|
64
|
-
|
|
65
|
-
const USE_AUDIO = false; // Set to true to enable audio recording feature (requires backend support for audio input)
|
|
66
|
-
|
|
67
|
-
async function loadMetadataOptionsFromBackend(
|
|
68
|
-
context: ContextKind,
|
|
69
|
-
): Promise<MetadataOptions> {
|
|
70
|
-
const cached = metadataOptionsCache.get(context);
|
|
71
|
-
if (cached) {
|
|
72
|
-
return cached;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const inFlight = metadataOptionsRequestCache.get(context);
|
|
76
|
-
if (inFlight) {
|
|
77
|
-
return inFlight;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const request = (async () => {
|
|
81
|
-
const backend = await resolveBackend();
|
|
82
|
-
|
|
83
|
-
if (!backend.available) {
|
|
84
|
-
throw new Error("Backend not available for metadata options");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const decision: CapabilityDecision = {
|
|
88
|
-
feature: "prompt",
|
|
89
|
-
source: "backend",
|
|
90
|
-
mode: "backend-only",
|
|
91
|
-
onDeviceAvailable: false,
|
|
92
|
-
backendAvailable: backend.available,
|
|
93
|
-
backendTransport: backend.transport,
|
|
94
|
-
backendApiName: backend.apiName,
|
|
95
|
-
backendBaseUrl: backend.baseUrl,
|
|
96
|
-
reason: backend.reason ?? "",
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const data = (await dispatchBackend(
|
|
100
|
-
decision,
|
|
101
|
-
context,
|
|
102
|
-
"/kb/metadata-options",
|
|
103
|
-
"GET",
|
|
104
|
-
null,
|
|
105
|
-
{},
|
|
106
|
-
)) as MetadataOptions;
|
|
107
|
-
|
|
108
|
-
const normalized: MetadataOptions = {
|
|
109
|
-
allowedCategories: data.allowedCategories || {},
|
|
110
|
-
allowedTags: data.allowedTags || [],
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
metadataOptionsCache.set(context, normalized);
|
|
114
|
-
return normalized;
|
|
115
|
-
})();
|
|
116
|
-
|
|
117
|
-
metadataOptionsRequestCache.set(context, request);
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
return await request;
|
|
121
|
-
} finally {
|
|
122
|
-
metadataOptionsRequestCache.delete(context);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function groupChunksByDoc(result: SearchResult | null) {
|
|
127
|
-
const docs = result?.citations?.docs ?? [];
|
|
128
|
-
const chunks = result?.citations?.chunks ?? [];
|
|
129
|
-
const byDoc = new Map<
|
|
130
|
-
string,
|
|
131
|
-
{ doc: (typeof docs)[number]; chunks: typeof chunks }
|
|
132
|
-
>();
|
|
133
|
-
|
|
134
|
-
for (const doc of docs) {
|
|
135
|
-
byDoc.set(doc.docId, { doc, chunks: [] });
|
|
136
|
-
}
|
|
137
|
-
for (const ch of chunks) {
|
|
138
|
-
const cur = byDoc.get(ch.docId);
|
|
139
|
-
if (cur) {
|
|
140
|
-
cur.chunks.push(ch);
|
|
141
|
-
} else {
|
|
142
|
-
// fallback if doc list is incomplete
|
|
143
|
-
byDoc.set(ch.docId, { doc: { docId: ch.docId }, chunks: [ch] });
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return Array.from(byDoc.values());
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function isWordChar(ch: string) {
|
|
150
|
-
// Unicode letter/number + underscore; good for Cyrillic too
|
|
151
|
-
return Boolean(ch) && /[\p{L}\p{N}_]/u.test(ch);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Backend sometimes returns anchor `end` that lands inside a word.
|
|
156
|
-
* Snap the insertion point to a word boundary (end of current word).
|
|
157
|
-
*/
|
|
158
|
-
function snapEndToBoundary(text: string, end: number) {
|
|
159
|
-
const len = text.length;
|
|
160
|
-
if (end <= 0) return 0;
|
|
161
|
-
if (end >= len) return len;
|
|
162
|
-
|
|
163
|
-
const prev = text[end - 1] ?? "";
|
|
164
|
-
const cur = text[end] ?? "";
|
|
165
|
-
|
|
166
|
-
// If we're inside a word, walk forward until word ends.
|
|
167
|
-
if (isWordChar(prev) && isWordChar(cur)) {
|
|
168
|
-
let i = end;
|
|
169
|
-
while (i < len && isWordChar(text[i] ?? "")) i++;
|
|
170
|
-
return i;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return end;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function escapeCssId(id: string) {
|
|
177
|
-
// Prefer native CSS.escape, fallback to a minimal safe escape.
|
|
178
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
179
|
-
const esc = (globalThis as any)?.CSS?.escape;
|
|
180
|
-
if (typeof esc === "function") return esc(id);
|
|
181
|
-
return id.replace(/[^a-zA-Z0-9_-]/g, "\\$&");
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const DocSearchBase: FC<Props> = (props) => {
|
|
185
|
-
const {
|
|
186
|
-
autoRun = true,
|
|
187
|
-
aiDisclosure,
|
|
188
|
-
context,
|
|
189
|
-
title,
|
|
190
|
-
showOpenButton = false,
|
|
191
|
-
openButtonTitle,
|
|
192
|
-
showOpenButtonTitle = true,
|
|
193
|
-
openButtonIcon,
|
|
194
|
-
showOpenButtonIcon = true,
|
|
195
|
-
searchButtonIcon,
|
|
196
|
-
showSearchButtonTitle = true,
|
|
197
|
-
showSearchButtonIcon = true,
|
|
198
|
-
showSources = true,
|
|
199
|
-
topK = 10,
|
|
200
|
-
getSearchText,
|
|
201
|
-
enableUserFilters = false,
|
|
202
|
-
availableCategories,
|
|
203
|
-
availableTags,
|
|
204
|
-
initialSelectedCategories,
|
|
205
|
-
initialSelectedSubcategories,
|
|
206
|
-
initialSelectedTags,
|
|
207
|
-
|
|
208
|
-
variation,
|
|
209
|
-
rootElement,
|
|
210
|
-
modalRootElement,
|
|
211
|
-
colorMode,
|
|
212
|
-
language,
|
|
213
|
-
onClose,
|
|
214
|
-
onClickDoc,
|
|
215
|
-
} = props;
|
|
216
|
-
|
|
217
|
-
const [featureOpen, setFeatureOpen] = useState<boolean>(!showOpenButton);
|
|
218
|
-
const resolvedAiDisclosure = I18n.get(
|
|
219
|
-
aiDisclosure?.trim() || DEFAULT_DOC_SEARCH_AI_DISCLOSURE,
|
|
220
|
-
);
|
|
221
|
-
const [query, setQuery] = useState<string>("");
|
|
222
|
-
const [recording, setRecording] = useState<boolean>(false);
|
|
223
|
-
const [audioBlob, setAudioBlob] = useState<Blob | null>(null);
|
|
224
|
-
const [audioLevel, setAudioLevel] = useState<number>(0);
|
|
225
|
-
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
|
226
|
-
const audioChunksRef = useRef<Blob[]>([]);
|
|
227
|
-
const audioContextRef = useRef<AudioContext | null>(null);
|
|
228
|
-
const analyserRef = useRef<AnalyserNode | null>(null);
|
|
229
|
-
|
|
230
|
-
// User filter states
|
|
231
|
-
const [filtersOpen, setFiltersOpen] = useState(
|
|
232
|
-
() =>
|
|
233
|
-
normalizeInitialFilterValues(initialSelectedCategories).length > 0 ||
|
|
234
|
-
normalizeInitialFilterValues(initialSelectedSubcategories).length > 0 ||
|
|
235
|
-
normalizeInitialFilterValues(initialSelectedTags).length > 0,
|
|
236
|
-
);
|
|
237
|
-
const [selectedCategories, setSelectedCategories] = useState<string[]>(() =>
|
|
238
|
-
normalizeInitialFilterValues(initialSelectedCategories),
|
|
239
|
-
);
|
|
240
|
-
const [selectedSubcategories, setSelectedSubcategories] = useState<string[]>(
|
|
241
|
-
() => normalizeInitialFilterValues(initialSelectedSubcategories),
|
|
242
|
-
);
|
|
243
|
-
const [selectedTags, setSelectedTags] = useState<string[]>(() =>
|
|
244
|
-
normalizeInitialFilterValues(initialSelectedTags),
|
|
245
|
-
);
|
|
246
|
-
const [tagSearchValue, setTagSearchValue] = useState<string>("");
|
|
247
|
-
const [metadataOptions, setMetadataOptions] =
|
|
248
|
-
useState<MetadataOptions | null>(null);
|
|
249
|
-
const [loadingMetadata, setLoadingMetadata] = useState(false);
|
|
250
|
-
const animationFrameRef = useRef<number | null>(null);
|
|
251
|
-
|
|
252
|
-
// Audio cache: store uploaded audio to avoid re-uploading within session
|
|
253
|
-
const audioCacheRef = useRef<{
|
|
254
|
-
blob: Blob;
|
|
255
|
-
uploadTimestamp: number;
|
|
256
|
-
} | null>(null);
|
|
257
|
-
const AUDIO_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
|
|
258
|
-
|
|
259
|
-
const { busy, error, errorDetails, statusEvent, result, run, cancel, reset } =
|
|
260
|
-
useAiRun<SearchResult>();
|
|
261
|
-
|
|
262
|
-
const autoRunOnceRef = useRef(false);
|
|
263
|
-
const prevSelectedCategoriesRef = useRef<string[]>([]);
|
|
264
|
-
|
|
265
|
-
const sessionId = result?.sessionId;
|
|
266
|
-
const citationDocs = result?.citations?.docs ?? [];
|
|
267
|
-
const citationChunks = result?.citations?.chunks ?? [];
|
|
268
|
-
const citationAnchors = result?.citations?.anchors ?? [];
|
|
269
|
-
const summaryText = result?.result ?? "";
|
|
270
|
-
|
|
271
|
-
const buttonLeftIcon = useMemo(() => {
|
|
272
|
-
if (!showSearchButtonIcon) return undefined;
|
|
273
|
-
|
|
274
|
-
if (searchButtonIcon?.trim()) {
|
|
275
|
-
return (
|
|
276
|
-
<img
|
|
277
|
-
src={searchButtonIcon}
|
|
278
|
-
alt=""
|
|
279
|
-
style={{ width: 18, height: 18, objectFit: "contain" }}
|
|
280
|
-
/>
|
|
281
|
-
);
|
|
282
|
-
}
|
|
283
|
-
return <IconSearch size={18} />;
|
|
284
|
-
}, [searchButtonIcon, showSearchButtonIcon]);
|
|
285
|
-
|
|
286
|
-
const defaultTitle = useMemo(() => {
|
|
287
|
-
if (language) {
|
|
288
|
-
I18n.setLanguage(language || "en");
|
|
289
|
-
}
|
|
290
|
-
return I18n.get(title || "Search with AI-Kit");
|
|
291
|
-
}, [language]);
|
|
292
|
-
|
|
293
|
-
const openButtonAccessibleLabel = I18n.get(openButtonTitle || defaultTitle);
|
|
294
|
-
const searchButtonAccessibleLabel = I18n.get("Search");
|
|
295
|
-
|
|
296
|
-
const statusText = useMemo(() => {
|
|
297
|
-
const e: AiKitStatusEvent | null = statusEvent;
|
|
298
|
-
if (!e) return null;
|
|
299
|
-
// Keep this generic and short (backend / on-device messages differ).
|
|
300
|
-
return I18n.get("Searching…");
|
|
301
|
-
}, [language, statusEvent]);
|
|
302
|
-
|
|
303
|
-
const inputText = useMemo(() => {
|
|
304
|
-
return query || getSearchText;
|
|
305
|
-
}, [query, getSearchText]);
|
|
306
|
-
|
|
307
|
-
const canSearch = useMemo(() => {
|
|
308
|
-
if (busy) return false;
|
|
309
|
-
if (!featureOpen) return false;
|
|
310
|
-
// Can search if we have text OR audio
|
|
311
|
-
const text = typeof inputText === "function" ? inputText() : inputText;
|
|
312
|
-
return Boolean((text && text.trim().length > 0) || audioBlob);
|
|
313
|
-
}, [inputText, busy, audioBlob, featureOpen]);
|
|
314
|
-
|
|
315
|
-
const hasValidFilterOptions = useMemo(() => {
|
|
316
|
-
if (!metadataOptions) return false;
|
|
317
|
-
const hasCategories =
|
|
318
|
-
Object.keys(metadataOptions.allowedCategories).length > 0;
|
|
319
|
-
const hasTags = metadataOptions.allowedTags.length > 0;
|
|
320
|
-
return hasCategories || hasTags;
|
|
321
|
-
}, [metadataOptions]);
|
|
322
|
-
|
|
323
|
-
const subcategories = useMemo(() => {
|
|
324
|
-
if (selectedCategories.length === 0 || !metadataOptions) {
|
|
325
|
-
return [];
|
|
326
|
-
}
|
|
327
|
-
return selectedCategories
|
|
328
|
-
.flatMap((cat) => metadataOptions.allowedCategories[cat] || [])
|
|
329
|
-
.filter((subcat, index, self) => self.indexOf(subcat) === index);
|
|
330
|
-
}, [selectedCategories, metadataOptions]);
|
|
331
|
-
|
|
332
|
-
const startRecording = useCallback(async () => {
|
|
333
|
-
try {
|
|
334
|
-
// Clear query input when starting audio recording
|
|
335
|
-
setQuery("");
|
|
336
|
-
|
|
337
|
-
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
338
|
-
const mediaRecorder = new MediaRecorder(stream, {
|
|
339
|
-
mimeType: "audio/webm",
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
// Setup audio analysis for visual feedback
|
|
343
|
-
const audioContext = new AudioContext();
|
|
344
|
-
const source = audioContext.createMediaStreamSource(stream);
|
|
345
|
-
const analyser = audioContext.createAnalyser();
|
|
346
|
-
analyser.fftSize = 2048; // Higher for smoother results
|
|
347
|
-
analyser.smoothingTimeConstant = 0.8; // Smoother transitions
|
|
348
|
-
source.connect(analyser);
|
|
349
|
-
|
|
350
|
-
audioContextRef.current = audioContext;
|
|
351
|
-
analyserRef.current = analyser;
|
|
352
|
-
|
|
353
|
-
// Monitor audio level using time domain data (more reliable)
|
|
354
|
-
const dataArray = new Uint8Array(analyser.fftSize);
|
|
355
|
-
const updateLevel = () => {
|
|
356
|
-
if (!analyserRef.current) return;
|
|
357
|
-
analyserRef.current.getByteTimeDomainData(dataArray);
|
|
358
|
-
|
|
359
|
-
// Calculate RMS (Root Mean Square) for accurate volume
|
|
360
|
-
let sum = 0;
|
|
361
|
-
for (let i = 0; i < dataArray.length; i++) {
|
|
362
|
-
const normalized = (dataArray[i]! - 128) / 128; // Normalize to -1 to 1
|
|
363
|
-
sum += normalized * normalized;
|
|
364
|
-
}
|
|
365
|
-
const rms = Math.sqrt(sum / dataArray.length);
|
|
366
|
-
|
|
367
|
-
// Convert to percentage (0-100) with scaling for better visibility
|
|
368
|
-
const level = Math.min(100, rms * 200);
|
|
369
|
-
setAudioLevel(level);
|
|
370
|
-
|
|
371
|
-
animationFrameRef.current = requestAnimationFrame(updateLevel);
|
|
372
|
-
};
|
|
373
|
-
updateLevel();
|
|
374
|
-
|
|
375
|
-
audioChunksRef.current = [];
|
|
376
|
-
|
|
377
|
-
mediaRecorder.ondataavailable = (event) => {
|
|
378
|
-
if (event.data.size > 0) {
|
|
379
|
-
audioChunksRef.current.push(event.data);
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
mediaRecorder.onstop = () => {
|
|
384
|
-
const audioBlob = new Blob(audioChunksRef.current, {
|
|
385
|
-
type: "audio/webm",
|
|
386
|
-
});
|
|
387
|
-
setAudioBlob(audioBlob);
|
|
388
|
-
stream.getTracks().forEach((track) => track.stop());
|
|
389
|
-
|
|
390
|
-
// Cleanup audio analysis
|
|
391
|
-
if (animationFrameRef.current) {
|
|
392
|
-
cancelAnimationFrame(animationFrameRef.current);
|
|
393
|
-
animationFrameRef.current = null;
|
|
394
|
-
}
|
|
395
|
-
if (audioContextRef.current) {
|
|
396
|
-
audioContextRef.current.close();
|
|
397
|
-
audioContextRef.current = null;
|
|
398
|
-
}
|
|
399
|
-
analyserRef.current = null;
|
|
400
|
-
setAudioLevel(0);
|
|
401
|
-
};
|
|
402
|
-
|
|
403
|
-
mediaRecorderRef.current = mediaRecorder;
|
|
404
|
-
mediaRecorder.start();
|
|
405
|
-
setRecording(true);
|
|
406
|
-
} catch (error) {
|
|
407
|
-
console.error("Failed to start recording:", error);
|
|
408
|
-
}
|
|
409
|
-
}, []);
|
|
410
|
-
|
|
411
|
-
const stopRecording = useCallback(() => {
|
|
412
|
-
if (mediaRecorderRef.current && recording) {
|
|
413
|
-
mediaRecorderRef.current.stop();
|
|
414
|
-
setRecording(false);
|
|
415
|
-
}
|
|
416
|
-
}, [recording]);
|
|
417
|
-
|
|
418
|
-
const clearAudio = useCallback(() => {
|
|
419
|
-
setAudioBlob(null);
|
|
420
|
-
audioChunksRef.current = [];
|
|
421
|
-
setAudioLevel(0);
|
|
422
|
-
// Clear audio cache when user manually clears audio
|
|
423
|
-
audioCacheRef.current = null;
|
|
424
|
-
}, []);
|
|
425
|
-
|
|
426
|
-
// Cleanup on unmount
|
|
427
|
-
useEffect(() => {
|
|
428
|
-
return () => {
|
|
429
|
-
if (animationFrameRef.current) {
|
|
430
|
-
cancelAnimationFrame(animationFrameRef.current);
|
|
431
|
-
}
|
|
432
|
-
if (audioContextRef.current) {
|
|
433
|
-
audioContextRef.current.close();
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
}, []);
|
|
437
|
-
|
|
438
|
-
// Load metadata options when user filters are enabled
|
|
439
|
-
useEffect(() => {
|
|
440
|
-
if (!enableUserFilters) return;
|
|
441
|
-
|
|
442
|
-
if (showOpenButton && !featureOpen) return;
|
|
443
|
-
|
|
444
|
-
// Use provided options if available
|
|
445
|
-
if (availableCategories || availableTags) {
|
|
446
|
-
setMetadataOptions({
|
|
447
|
-
allowedCategories: availableCategories || {},
|
|
448
|
-
allowedTags: availableTags || [],
|
|
449
|
-
});
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
// Otherwise fetch from backend
|
|
454
|
-
const loadMetadata = async () => {
|
|
455
|
-
const effectiveContext: ContextKind =
|
|
456
|
-
context === "admin" ? "admin" : "frontend";
|
|
457
|
-
|
|
458
|
-
setLoadingMetadata(true);
|
|
459
|
-
try {
|
|
460
|
-
const data = await loadMetadataOptionsFromBackend(effectiveContext);
|
|
461
|
-
|
|
462
|
-
setMetadataOptions(data);
|
|
463
|
-
} catch {
|
|
464
|
-
// Search remains available when optional filter metadata is unavailable.
|
|
465
|
-
} finally {
|
|
466
|
-
setLoadingMetadata(false);
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
void loadMetadata();
|
|
471
|
-
}, [
|
|
472
|
-
enableUserFilters,
|
|
473
|
-
availableCategories,
|
|
474
|
-
availableTags,
|
|
475
|
-
context,
|
|
476
|
-
featureOpen,
|
|
477
|
-
showOpenButton,
|
|
478
|
-
]);
|
|
479
|
-
|
|
480
|
-
const onSearch = useCallback(async () => {
|
|
481
|
-
let q: string | undefined;
|
|
482
|
-
|
|
483
|
-
// Get text query if available (not recording audio)
|
|
484
|
-
if (!audioBlob) {
|
|
485
|
-
q =
|
|
486
|
-
typeof inputText === "function"
|
|
487
|
-
? (inputText as () => string)()
|
|
488
|
-
: inputText;
|
|
489
|
-
if (!q) return;
|
|
490
|
-
setQuery(q);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
// Check if we can reuse cached audio (same blob within TTL)
|
|
494
|
-
const now = Date.now();
|
|
495
|
-
const isSameAudio =
|
|
496
|
-
audioBlob &&
|
|
497
|
-
audioCacheRef.current?.blob === audioBlob &&
|
|
498
|
-
now - audioCacheRef.current.uploadTimestamp < AUDIO_CACHE_TTL;
|
|
499
|
-
|
|
500
|
-
if (audioBlob && !isSameAudio) {
|
|
501
|
-
// Update cache for new audio blob
|
|
502
|
-
audioCacheRef.current = {
|
|
503
|
-
blob: audioBlob,
|
|
504
|
-
uploadTimestamp: now,
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
reset();
|
|
509
|
-
try {
|
|
510
|
-
await run(async ({ signal, onStatus }) => {
|
|
511
|
-
return await sendSearchMessage(
|
|
512
|
-
{
|
|
513
|
-
sessionId,
|
|
514
|
-
...(q && { query: q }),
|
|
515
|
-
...(audioBlob && { audio: audioBlob }), // Pass Blob directly
|
|
516
|
-
topK,
|
|
517
|
-
// Include user-selected filters if enabled
|
|
518
|
-
// Always send userSelectedCategories array when enableUserFilters is true (even if empty)
|
|
519
|
-
// to prevent backend from applying its own kb-filter
|
|
520
|
-
...(enableUserFilters && {
|
|
521
|
-
userSelectedCategories: selectedCategories,
|
|
522
|
-
}),
|
|
523
|
-
...(enableUserFilters &&
|
|
524
|
-
selectedSubcategories.length > 0 && {
|
|
525
|
-
userSelectedSubcategories: selectedSubcategories,
|
|
526
|
-
}),
|
|
527
|
-
...(enableUserFilters &&
|
|
528
|
-
selectedTags.length > 0 && { userSelectedTags: selectedTags }),
|
|
529
|
-
},
|
|
530
|
-
{ signal, onStatus, context },
|
|
531
|
-
);
|
|
532
|
-
});
|
|
533
|
-
} catch {
|
|
534
|
-
// useAiRun stores a normalized, localized error for the accessible UI.
|
|
535
|
-
}
|
|
536
|
-
}, [
|
|
537
|
-
context,
|
|
538
|
-
inputText,
|
|
539
|
-
audioBlob,
|
|
540
|
-
run,
|
|
541
|
-
reset,
|
|
542
|
-
topK,
|
|
543
|
-
sessionId,
|
|
544
|
-
enableUserFilters,
|
|
545
|
-
selectedCategories,
|
|
546
|
-
selectedSubcategories,
|
|
547
|
-
selectedTags,
|
|
548
|
-
]);
|
|
549
|
-
|
|
550
|
-
const close = useCallback(async () => {
|
|
551
|
-
setFeatureOpen(false);
|
|
552
|
-
reset();
|
|
553
|
-
//autoRunOnceRef.current = false;
|
|
554
|
-
if (!showOpenButton) {
|
|
555
|
-
onClose();
|
|
556
|
-
}
|
|
557
|
-
}, [onClose, reset, autoRunOnceRef, showOpenButton]);
|
|
558
|
-
|
|
559
|
-
useEffect(() => {
|
|
560
|
-
if (!autoRun || !canSearch || busy || autoRunOnceRef.current) {
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
|
-
autoRunOnceRef.current = true;
|
|
564
|
-
queueMicrotask(() => {
|
|
565
|
-
void onSearch();
|
|
566
|
-
});
|
|
567
|
-
}, [busy, autoRunOnceRef, canSearch, autoRun, onSearch]);
|
|
568
|
-
|
|
569
|
-
useEffect(() => {
|
|
570
|
-
if (!canSearch) {
|
|
571
|
-
autoRunOnceRef.current = true;
|
|
572
|
-
}
|
|
573
|
-
}, [canSearch]);
|
|
574
|
-
|
|
575
|
-
// Reset session when main categories change
|
|
576
|
-
useEffect(() => {
|
|
577
|
-
const prev = prevSelectedCategoriesRef.current;
|
|
578
|
-
const current = selectedCategories;
|
|
579
|
-
|
|
580
|
-
// Check if categories changed (different length or different items)
|
|
581
|
-
const categoriesChanged =
|
|
582
|
-
prev.length !== current.length ||
|
|
583
|
-
!current.every((cat) => prev.includes(cat));
|
|
584
|
-
|
|
585
|
-
if (categoriesChanged && prev.length > 0) {
|
|
586
|
-
// Reset session only if we had categories before (not on initial mount)
|
|
587
|
-
reset();
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
prevSelectedCategoriesRef.current = [...current];
|
|
591
|
-
}, [selectedCategories, reset]);
|
|
592
|
-
|
|
593
|
-
const grouped = useMemo(() => groupChunksByDoc(result), [result]);
|
|
594
|
-
|
|
595
|
-
const docNumberMap = useMemo(() => {
|
|
596
|
-
const map = new Map<string, number>();
|
|
597
|
-
citationDocs.forEach((doc, index) => {
|
|
598
|
-
if (doc?.docId) {
|
|
599
|
-
map.set(doc.docId, index + 1);
|
|
600
|
-
}
|
|
601
|
-
});
|
|
602
|
-
return map;
|
|
603
|
-
}, [citationDocs]);
|
|
604
|
-
|
|
605
|
-
const chunkDocMap = useMemo(() => {
|
|
606
|
-
const map = new Map<string, string>();
|
|
607
|
-
citationChunks.forEach((chunk) => {
|
|
608
|
-
if (chunk?.chunkId && chunk?.docId) {
|
|
609
|
-
map.set(chunk.chunkId, chunk.docId);
|
|
610
|
-
}
|
|
611
|
-
});
|
|
612
|
-
return map;
|
|
613
|
-
}, [citationChunks]);
|
|
614
|
-
|
|
615
|
-
const annotatedSummary = useMemo(() => {
|
|
616
|
-
if (!summaryText) return "";
|
|
617
|
-
if (!citationAnchors.length || docNumberMap.size === 0) {
|
|
618
|
-
return summaryText;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
const sortedAnchors = [...citationAnchors]
|
|
622
|
-
.filter(
|
|
623
|
-
(anchor) =>
|
|
624
|
-
Array.isArray(anchor?.chunkIds) && anchor?.span?.end !== undefined,
|
|
625
|
-
)
|
|
626
|
-
.sort((a, b) => {
|
|
627
|
-
const aEnd = a.span?.end ?? 0;
|
|
628
|
-
const bEnd = b.span?.end ?? 0;
|
|
629
|
-
return aEnd - bEnd;
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
let cursor = 0;
|
|
633
|
-
const segments: string[] = [];
|
|
634
|
-
|
|
635
|
-
for (const anchor of sortedAnchors) {
|
|
636
|
-
const end = anchor.span?.end;
|
|
637
|
-
if (typeof end !== "number" || end < cursor) {
|
|
638
|
-
continue;
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
const refs = Array.from(
|
|
642
|
-
new Set(
|
|
643
|
-
(anchor.chunkIds ?? [])
|
|
644
|
-
.map((chunkId) => (chunkId ? chunkDocMap.get(chunkId) : undefined))
|
|
645
|
-
.map((docId) => (docId ? docNumberMap.get(docId) : undefined))
|
|
646
|
-
.filter((num): num is number => typeof num === "number"),
|
|
647
|
-
),
|
|
648
|
-
);
|
|
649
|
-
|
|
650
|
-
if (!refs.length) {
|
|
651
|
-
continue;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
const safeEnd = Math.min(end, summaryText.length);
|
|
655
|
-
const snappedEnd = snapEndToBoundary(summaryText, safeEnd);
|
|
656
|
-
|
|
657
|
-
segments.push(summaryText.slice(cursor, snappedEnd));
|
|
658
|
-
|
|
659
|
-
const supHtml = refs
|
|
660
|
-
.map(
|
|
661
|
-
(n) =>
|
|
662
|
-
`<a href="#docsearch-source-${n}" data-docsearch-cite="${n}" style="color: inherit; text-decoration: none;">${n}</a>`,
|
|
663
|
-
)
|
|
664
|
-
.join(",");
|
|
665
|
-
|
|
666
|
-
segments.push(`<sup>${supHtml}</sup>`);
|
|
667
|
-
cursor = snappedEnd;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
segments.push(summaryText.slice(cursor));
|
|
671
|
-
return segments.join("");
|
|
672
|
-
}, [citationAnchors, summaryText, docNumberMap, chunkDocMap]);
|
|
673
|
-
|
|
674
|
-
const RootComponent: typeof Modal.Root | typeof Group =
|
|
675
|
-
variation === "modal" ? Modal.Root : Group;
|
|
676
|
-
const ContentComponent: typeof Modal.Content | typeof Group =
|
|
677
|
-
variation === "modal" ? Modal.Content : Group;
|
|
678
|
-
const BodyComponent: typeof Modal.Body | typeof Group =
|
|
679
|
-
variation === "modal" ? Modal.Body : Group;
|
|
680
|
-
|
|
681
|
-
useEffect(() => {
|
|
682
|
-
if (variation !== "modal" || !featureOpen) {
|
|
683
|
-
return;
|
|
684
|
-
}
|
|
685
|
-
document.body.style.overflow = "hidden";
|
|
686
|
-
document.body.onkeydown = (e: KeyboardEvent) => {
|
|
687
|
-
if (e.key === "Escape") {
|
|
688
|
-
e.preventDefault();
|
|
689
|
-
close();
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
return () => {
|
|
693
|
-
// remove overflow: hidden; from body element
|
|
694
|
-
document.body.style.overflow = "";
|
|
695
|
-
document.body.onkeydown = null;
|
|
696
|
-
};
|
|
697
|
-
}, [close, variation, featureOpen]);
|
|
698
|
-
|
|
699
|
-
return (
|
|
700
|
-
<>
|
|
701
|
-
{showOpenButton && (
|
|
702
|
-
<Button
|
|
703
|
-
leftSection={
|
|
704
|
-
showOpenButtonIcon &&
|
|
705
|
-
(openButtonIcon ? (
|
|
706
|
-
<span dangerouslySetInnerHTML={{ __html: openButtonIcon }} />
|
|
707
|
-
) : (
|
|
708
|
-
<IconSearch size={18} />
|
|
709
|
-
))
|
|
710
|
-
}
|
|
711
|
-
className={
|
|
712
|
-
showOpenButtonTitle
|
|
713
|
-
? "ai-feature-open-button"
|
|
714
|
-
: "ai-feature-open-button-no-title"
|
|
715
|
-
}
|
|
716
|
-
variant={"filled"}
|
|
717
|
-
disabled={featureOpen}
|
|
718
|
-
onClick={() => setFeatureOpen(true)}
|
|
719
|
-
aria-label={openButtonAccessibleLabel}
|
|
720
|
-
data-ai-kit-open-button
|
|
721
|
-
>
|
|
722
|
-
{showOpenButtonTitle && I18n.get(openButtonTitle || defaultTitle)}
|
|
723
|
-
</Button>
|
|
724
|
-
)}
|
|
725
|
-
|
|
726
|
-
{featureOpen && (
|
|
727
|
-
<RootComponent
|
|
728
|
-
opened={true}
|
|
729
|
-
className="doc-search-root"
|
|
730
|
-
onClose={close}
|
|
731
|
-
padding="md"
|
|
732
|
-
gap="md"
|
|
733
|
-
size="xl"
|
|
734
|
-
portalProps={
|
|
735
|
-
variation === "modal"
|
|
736
|
-
? {
|
|
737
|
-
target: modalRootElement ?? rootElement,
|
|
738
|
-
reuseTargetNode: true,
|
|
739
|
-
}
|
|
740
|
-
: undefined
|
|
741
|
-
}
|
|
742
|
-
data-ai-kit-theme={colorMode}
|
|
743
|
-
data-ai-kit-variation={variation}
|
|
744
|
-
>
|
|
745
|
-
{variation === "modal" && <Modal.Overlay />}
|
|
746
|
-
<ContentComponent
|
|
747
|
-
w="100%"
|
|
748
|
-
style={{
|
|
749
|
-
left: 0,
|
|
750
|
-
...(variation === "modal" &&
|
|
751
|
-
!result?.result && { overflow: "visible" }),
|
|
752
|
-
}}
|
|
753
|
-
>
|
|
754
|
-
{variation === "modal" && (
|
|
755
|
-
<Modal.Header style={{ zIndex: 1000 }}>
|
|
756
|
-
<AiKitDocSearchIcon className="doc-search-title-icon" />
|
|
757
|
-
<Modal.Title>{I18n.get(defaultTitle)}</Modal.Title>
|
|
758
|
-
<Modal.CloseButton />
|
|
759
|
-
</Modal.Header>
|
|
760
|
-
)}
|
|
761
|
-
<BodyComponent
|
|
762
|
-
w="100%"
|
|
763
|
-
style={{
|
|
764
|
-
zIndex: 1001,
|
|
765
|
-
...(variation === "modal" &&
|
|
766
|
-
!result?.result && { overflow: "visible" }),
|
|
767
|
-
}}
|
|
768
|
-
>
|
|
769
|
-
<AiFeatureBorder
|
|
770
|
-
enabled={variation !== "modal"}
|
|
771
|
-
working={busy}
|
|
772
|
-
variation={variation}
|
|
773
|
-
>
|
|
774
|
-
<Paper shadow="sm" radius="md" p="md">
|
|
775
|
-
<Stack gap="sm">
|
|
776
|
-
{variation !== "modal" && (
|
|
777
|
-
<Group justify="space-between">
|
|
778
|
-
<Title order={4} style={{ margin: 0 }}>
|
|
779
|
-
{I18n.get(defaultTitle)}
|
|
780
|
-
</Title>
|
|
781
|
-
{showOpenButton && (
|
|
782
|
-
<Button
|
|
783
|
-
variant="subtle"
|
|
784
|
-
color="gray"
|
|
785
|
-
size="xs"
|
|
786
|
-
onClick={close}
|
|
787
|
-
style={{
|
|
788
|
-
minWidth: 32,
|
|
789
|
-
padding: 0,
|
|
790
|
-
display: "flex",
|
|
791
|
-
alignItems: "center",
|
|
792
|
-
justifyContent: "center",
|
|
793
|
-
}}
|
|
794
|
-
aria-label={I18n.get("Close")}
|
|
795
|
-
>
|
|
796
|
-
<IconX
|
|
797
|
-
size={18}
|
|
798
|
-
style={{
|
|
799
|
-
color:
|
|
800
|
-
colorMode === "dark"
|
|
801
|
-
? "var(--mantine-color-dark-1)"
|
|
802
|
-
: "var(--mantine-color-gray-7)",
|
|
803
|
-
}}
|
|
804
|
-
/>
|
|
805
|
-
</Button>
|
|
806
|
-
)}
|
|
807
|
-
</Group>
|
|
808
|
-
)}
|
|
809
|
-
|
|
810
|
-
<Group gap="sm" align="flex-end" wrap="nowrap">
|
|
811
|
-
<TextInput
|
|
812
|
-
style={{ flex: 1 }}
|
|
813
|
-
value={query}
|
|
814
|
-
onChange={(e) => {
|
|
815
|
-
setQuery(e.currentTarget.value);
|
|
816
|
-
// Clear audio when typing
|
|
817
|
-
if (audioBlob) {
|
|
818
|
-
clearAudio();
|
|
819
|
-
}
|
|
820
|
-
}}
|
|
821
|
-
placeholder={
|
|
822
|
-
audioBlob
|
|
823
|
-
? I18n.get("Audio recorded")
|
|
824
|
-
: I18n.get("Search the documentation…")
|
|
825
|
-
}
|
|
826
|
-
disabled={busy || recording || !!audioBlob}
|
|
827
|
-
onKeyDown={(e) => {
|
|
828
|
-
if (e.key === "Enter" && canSearch) {
|
|
829
|
-
e.preventDefault();
|
|
830
|
-
void onSearch();
|
|
831
|
-
}
|
|
832
|
-
}}
|
|
833
|
-
/>
|
|
834
|
-
|
|
835
|
-
{/* Microphone button */}
|
|
836
|
-
{USE_AUDIO && (
|
|
837
|
-
<>
|
|
838
|
-
{audioBlob ? (
|
|
839
|
-
<Button
|
|
840
|
-
variant="outline"
|
|
841
|
-
size="sm"
|
|
842
|
-
color="red"
|
|
843
|
-
onClick={clearAudio}
|
|
844
|
-
disabled={busy}
|
|
845
|
-
title={I18n.get("Clear audio")}
|
|
846
|
-
>
|
|
847
|
-
<IconMicrophoneOff size={18} />
|
|
848
|
-
</Button>
|
|
849
|
-
) : (
|
|
850
|
-
<Button
|
|
851
|
-
variant={recording ? "filled" : "outline"}
|
|
852
|
-
size="sm"
|
|
853
|
-
color={recording ? "red" : "gray"}
|
|
854
|
-
onClick={
|
|
855
|
-
recording ? stopRecording : startRecording
|
|
856
|
-
}
|
|
857
|
-
disabled={busy}
|
|
858
|
-
title={
|
|
859
|
-
recording
|
|
860
|
-
? I18n.get("Stop recording")
|
|
861
|
-
: I18n.get("Record audio")
|
|
862
|
-
}
|
|
863
|
-
style={
|
|
864
|
-
recording
|
|
865
|
-
? {
|
|
866
|
-
transform: `scale(${1 + audioLevel / 300})`,
|
|
867
|
-
transition: "transform 0.1s ease-out",
|
|
868
|
-
}
|
|
869
|
-
: undefined
|
|
870
|
-
}
|
|
871
|
-
>
|
|
872
|
-
<IconMicrophone size={18} />
|
|
873
|
-
</Button>
|
|
874
|
-
)}
|
|
875
|
-
</>
|
|
876
|
-
)}
|
|
877
|
-
|
|
878
|
-
<Button
|
|
879
|
-
variant="filled"
|
|
880
|
-
size="sm"
|
|
881
|
-
leftSection={buttonLeftIcon}
|
|
882
|
-
onClick={() => void onSearch()}
|
|
883
|
-
disabled={!canSearch}
|
|
884
|
-
aria-label={searchButtonAccessibleLabel}
|
|
885
|
-
className={
|
|
886
|
-
showSearchButtonTitle
|
|
887
|
-
? "doc-search-button"
|
|
888
|
-
: "doc-search-button-no-title"
|
|
889
|
-
}
|
|
890
|
-
>
|
|
891
|
-
{showSearchButtonTitle ? I18n.get("Search") : null}
|
|
892
|
-
</Button>
|
|
893
|
-
|
|
894
|
-
{busy ? (
|
|
895
|
-
<Button variant="outline" size="sm" onClick={cancel}>
|
|
896
|
-
{I18n.get("Stop")}
|
|
897
|
-
</Button>
|
|
898
|
-
) : null}
|
|
899
|
-
</Group>
|
|
900
|
-
|
|
901
|
-
{/* Empty state */}
|
|
902
|
-
{!busy && !error && !result?.result ? (
|
|
903
|
-
<Text size="sm" c="dimmed" data-doc-search-no-results>
|
|
904
|
-
{I18n.get("Enter a search query to start.")}
|
|
905
|
-
</Text>
|
|
906
|
-
) : null}
|
|
907
|
-
|
|
908
|
-
{/* User filter collapse */}
|
|
909
|
-
{enableUserFilters &&
|
|
910
|
-
metadataOptions &&
|
|
911
|
-
hasValidFilterOptions && (
|
|
912
|
-
<Stack gap="xs">
|
|
913
|
-
<Button
|
|
914
|
-
variant="subtle"
|
|
915
|
-
size="xs"
|
|
916
|
-
onClick={() => setFiltersOpen(!filtersOpen)}
|
|
917
|
-
leftSection={
|
|
918
|
-
filtersOpen ? (
|
|
919
|
-
<IconChevronDown size={14} />
|
|
920
|
-
) : (
|
|
921
|
-
<IconChevronRight size={14} />
|
|
922
|
-
)
|
|
923
|
-
}
|
|
924
|
-
style={{ alignSelf: "flex-start" }}
|
|
925
|
-
>
|
|
926
|
-
{I18n.get("Filters")}
|
|
927
|
-
</Button>
|
|
928
|
-
|
|
929
|
-
<Collapse in={filtersOpen}>
|
|
930
|
-
<Stack gap="md">
|
|
931
|
-
{/* Main categories as checkboxes */}
|
|
932
|
-
{Object.keys(metadataOptions.allowedCategories)
|
|
933
|
-
.length > 0 && (
|
|
934
|
-
<div>
|
|
935
|
-
<Text size="sm" fw={500} mb="xs">
|
|
936
|
-
{I18n.get("Categories")}
|
|
937
|
-
</Text>
|
|
938
|
-
<Group gap="md">
|
|
939
|
-
{Object.keys(
|
|
940
|
-
metadataOptions.allowedCategories,
|
|
941
|
-
).map((category) => (
|
|
942
|
-
<Checkbox
|
|
943
|
-
key={category}
|
|
944
|
-
label={I18n.get(category)}
|
|
945
|
-
checked={selectedCategories.includes(
|
|
946
|
-
category,
|
|
947
|
-
)}
|
|
948
|
-
onChange={(e) => {
|
|
949
|
-
if (e.currentTarget.checked) {
|
|
950
|
-
setSelectedCategories([
|
|
951
|
-
...selectedCategories,
|
|
952
|
-
category,
|
|
953
|
-
]);
|
|
954
|
-
} else {
|
|
955
|
-
setSelectedCategories(
|
|
956
|
-
selectedCategories.filter(
|
|
957
|
-
(c) => c !== category,
|
|
958
|
-
),
|
|
959
|
-
);
|
|
960
|
-
// Remove subcategories of unchecked category
|
|
961
|
-
const subcatsToRemove =
|
|
962
|
-
metadataOptions.allowedCategories[
|
|
963
|
-
category
|
|
964
|
-
] || [];
|
|
965
|
-
setSelectedSubcategories(
|
|
966
|
-
selectedSubcategories.filter(
|
|
967
|
-
(sc) =>
|
|
968
|
-
!subcatsToRemove.includes(sc),
|
|
969
|
-
),
|
|
970
|
-
);
|
|
971
|
-
}
|
|
972
|
-
}}
|
|
973
|
-
disabled={busy || loadingMetadata}
|
|
974
|
-
/>
|
|
975
|
-
))}
|
|
976
|
-
</Group>
|
|
977
|
-
</div>
|
|
978
|
-
)}
|
|
979
|
-
|
|
980
|
-
{/* Subcategories for selected categories */}
|
|
981
|
-
{subcategories.length > 0 && (
|
|
982
|
-
<div>
|
|
983
|
-
<Text size="sm" fw={500} mb="xs">
|
|
984
|
-
{I18n.get("Subcategories")}
|
|
985
|
-
</Text>
|
|
986
|
-
<Group gap="md">
|
|
987
|
-
{subcategories.map((subcat) => (
|
|
988
|
-
<Checkbox
|
|
989
|
-
key={subcat}
|
|
990
|
-
label={I18n.get(subcat)}
|
|
991
|
-
checked={selectedSubcategories.includes(
|
|
992
|
-
subcat,
|
|
993
|
-
)}
|
|
994
|
-
onChange={(e) => {
|
|
995
|
-
if (e.currentTarget.checked) {
|
|
996
|
-
setSelectedSubcategories([
|
|
997
|
-
...selectedSubcategories,
|
|
998
|
-
subcat,
|
|
999
|
-
]);
|
|
1000
|
-
} else {
|
|
1001
|
-
setSelectedSubcategories(
|
|
1002
|
-
selectedSubcategories.filter(
|
|
1003
|
-
(sc) => sc !== subcat,
|
|
1004
|
-
),
|
|
1005
|
-
);
|
|
1006
|
-
}
|
|
1007
|
-
}}
|
|
1008
|
-
disabled={busy || loadingMetadata}
|
|
1009
|
-
/>
|
|
1010
|
-
))}
|
|
1011
|
-
</Group>
|
|
1012
|
-
</div>
|
|
1013
|
-
)}
|
|
1014
|
-
|
|
1015
|
-
{/* Tags input */}
|
|
1016
|
-
{metadataOptions.allowedTags.length > 0 && (
|
|
1017
|
-
<MultiSelect
|
|
1018
|
-
label={I18n.get("Tags")}
|
|
1019
|
-
placeholder={I18n.get(
|
|
1020
|
-
"Select or type tags...",
|
|
1021
|
-
)}
|
|
1022
|
-
data={metadataOptions.allowedTags.map(
|
|
1023
|
-
(tag) => ({
|
|
1024
|
-
value: tag,
|
|
1025
|
-
label: I18n.get(tag),
|
|
1026
|
-
}),
|
|
1027
|
-
)}
|
|
1028
|
-
value={selectedTags}
|
|
1029
|
-
onChange={setSelectedTags}
|
|
1030
|
-
searchValue={tagSearchValue}
|
|
1031
|
-
onSearchChange={setTagSearchValue}
|
|
1032
|
-
disabled={busy || loadingMetadata}
|
|
1033
|
-
searchable
|
|
1034
|
-
clearable
|
|
1035
|
-
maxDropdownHeight={200}
|
|
1036
|
-
limit={20}
|
|
1037
|
-
/>
|
|
1038
|
-
)}
|
|
1039
|
-
</Stack>
|
|
1040
|
-
</Collapse>
|
|
1041
|
-
</Stack>
|
|
1042
|
-
)}
|
|
1043
|
-
|
|
1044
|
-
{/* Audio level indicator when recording */}
|
|
1045
|
-
{USE_AUDIO && (
|
|
1046
|
-
<>
|
|
1047
|
-
{recording && (
|
|
1048
|
-
<Stack gap="xs">
|
|
1049
|
-
<Text size="xs" c="dimmed">
|
|
1050
|
-
{I18n.get("Recording...")} 🎤
|
|
1051
|
-
</Text>
|
|
1052
|
-
<Progress
|
|
1053
|
-
value={audioLevel}
|
|
1054
|
-
size="sm"
|
|
1055
|
-
color="red"
|
|
1056
|
-
animated
|
|
1057
|
-
striped
|
|
1058
|
-
/>
|
|
1059
|
-
</Stack>
|
|
1060
|
-
)}
|
|
1061
|
-
|
|
1062
|
-
{/* Audio playback when recorded */}
|
|
1063
|
-
{audioBlob && !recording && (
|
|
1064
|
-
<Stack gap="xs">
|
|
1065
|
-
<Text size="xs" c="dimmed">
|
|
1066
|
-
{I18n.get("Recorded audio:")}
|
|
1067
|
-
</Text>
|
|
1068
|
-
<audio
|
|
1069
|
-
controls
|
|
1070
|
-
src={URL.createObjectURL(audioBlob)}
|
|
1071
|
-
className="ai-kit-audio-player"
|
|
1072
|
-
/>
|
|
1073
|
-
</Stack>
|
|
1074
|
-
)}
|
|
1075
|
-
</>
|
|
1076
|
-
)}
|
|
1077
|
-
|
|
1078
|
-
{error ? (
|
|
1079
|
-
<Alert
|
|
1080
|
-
color="red"
|
|
1081
|
-
title={I18n.get("Error")}
|
|
1082
|
-
role="alert"
|
|
1083
|
-
aria-live="assertive"
|
|
1084
|
-
data-ai-kit-error
|
|
1085
|
-
>
|
|
1086
|
-
<Stack gap={2}>
|
|
1087
|
-
<Text size="sm">{error}</Text>
|
|
1088
|
-
{errorDetails?.requestId ? (
|
|
1089
|
-
<Text size="xs" c="dimmed">
|
|
1090
|
-
{I18n.get("Request ID")}: {errorDetails.requestId}
|
|
1091
|
-
</Text>
|
|
1092
|
-
) : null}
|
|
1093
|
-
</Stack>
|
|
1094
|
-
</Alert>
|
|
1095
|
-
) : null}
|
|
1096
|
-
|
|
1097
|
-
{busy && statusText && (
|
|
1098
|
-
<AiFeatureBorder
|
|
1099
|
-
enabled={variation === "modal"}
|
|
1100
|
-
working={true}
|
|
1101
|
-
variation={variation}
|
|
1102
|
-
>
|
|
1103
|
-
<Group
|
|
1104
|
-
justify="center"
|
|
1105
|
-
align="center"
|
|
1106
|
-
gap="sm"
|
|
1107
|
-
m="sm"
|
|
1108
|
-
pr="lg"
|
|
1109
|
-
>
|
|
1110
|
-
<Loader size="sm" />
|
|
1111
|
-
<Text size="sm" c="dimmed">
|
|
1112
|
-
{statusText}
|
|
1113
|
-
</Text>
|
|
1114
|
-
</Group>
|
|
1115
|
-
</AiFeatureBorder>
|
|
1116
|
-
)}
|
|
1117
|
-
|
|
1118
|
-
{result?.result ? (
|
|
1119
|
-
<>
|
|
1120
|
-
<Divider />
|
|
1121
|
-
<Stack gap="xs" data-doc-search-result>
|
|
1122
|
-
<Text
|
|
1123
|
-
size="sm"
|
|
1124
|
-
c="dimmed"
|
|
1125
|
-
data-doc-search-result-title
|
|
1126
|
-
>
|
|
1127
|
-
{I18n.get("AI Summary")}
|
|
1128
|
-
</Text>
|
|
1129
|
-
<ReactMarkdown
|
|
1130
|
-
remarkPlugins={[remarkGfm]}
|
|
1131
|
-
rehypePlugins={[rehypeRaw]}
|
|
1132
|
-
components={{
|
|
1133
|
-
a: ({ href, children, ...rest }) => {
|
|
1134
|
-
const isCite =
|
|
1135
|
-
typeof href === "string" &&
|
|
1136
|
-
href.startsWith("#docsearch-source-");
|
|
1137
|
-
|
|
1138
|
-
return (
|
|
1139
|
-
<a
|
|
1140
|
-
href={href}
|
|
1141
|
-
{...rest}
|
|
1142
|
-
onClick={(e) => {
|
|
1143
|
-
if (!isCite) return;
|
|
1144
|
-
e.preventDefault();
|
|
1145
|
-
|
|
1146
|
-
const id = href!.slice(1);
|
|
1147
|
-
const selector = `#${escapeCssId(id)}`;
|
|
1148
|
-
|
|
1149
|
-
const scope: ParentNode =
|
|
1150
|
-
(rootElement as unknown as ParentNode) ??
|
|
1151
|
-
document;
|
|
1152
|
-
|
|
1153
|
-
const el = (
|
|
1154
|
-
scope as Document | Element
|
|
1155
|
-
).querySelector?.(
|
|
1156
|
-
selector,
|
|
1157
|
-
) as HTMLElement | null;
|
|
1158
|
-
|
|
1159
|
-
el?.scrollIntoView({
|
|
1160
|
-
block: "start",
|
|
1161
|
-
behavior: "smooth",
|
|
1162
|
-
});
|
|
1163
|
-
}}
|
|
1164
|
-
>
|
|
1165
|
-
{children}
|
|
1166
|
-
</a>
|
|
1167
|
-
);
|
|
1168
|
-
},
|
|
1169
|
-
}}
|
|
1170
|
-
data-doc-search-result-content
|
|
1171
|
-
>
|
|
1172
|
-
{annotatedSummary || summaryText}
|
|
1173
|
-
</ReactMarkdown>
|
|
1174
|
-
<Text
|
|
1175
|
-
className="ai-generated-content-disclosure"
|
|
1176
|
-
size="xs"
|
|
1177
|
-
c="dimmed"
|
|
1178
|
-
role="note"
|
|
1179
|
-
data-ai-kit-ai-disclosure
|
|
1180
|
-
>
|
|
1181
|
-
{resolvedAiDisclosure}
|
|
1182
|
-
</Text>
|
|
1183
|
-
</Stack>
|
|
1184
|
-
</>
|
|
1185
|
-
) : null}
|
|
1186
|
-
|
|
1187
|
-
{showSources &&
|
|
1188
|
-
(result?.citations?.docs?.length ||
|
|
1189
|
-
result?.citations?.chunks?.length) ? (
|
|
1190
|
-
<>
|
|
1191
|
-
<Divider />
|
|
1192
|
-
<Stack gap="sm" data-doc-search-sources>
|
|
1193
|
-
<Text
|
|
1194
|
-
size="sm"
|
|
1195
|
-
c="dimmed"
|
|
1196
|
-
data-doc-search-sources-title
|
|
1197
|
-
>
|
|
1198
|
-
{I18n.get("Sources")}
|
|
1199
|
-
</Text>
|
|
1200
|
-
|
|
1201
|
-
{grouped.map(({ doc }) => {
|
|
1202
|
-
const href = doc.sourceUrl?.trim() || undefined;
|
|
1203
|
-
const docNumber = doc.docId
|
|
1204
|
-
? docNumberMap.get(doc.docId)
|
|
1205
|
-
: undefined;
|
|
1206
|
-
const titleText = doc.title?.trim() || doc.docId;
|
|
1207
|
-
const titleNode = (
|
|
1208
|
-
<Text fw={600} style={{ display: "inline" }}>
|
|
1209
|
-
{docNumber ? `${docNumber}. ` : ""}
|
|
1210
|
-
{titleText}
|
|
1211
|
-
</Text>
|
|
1212
|
-
);
|
|
1213
|
-
|
|
1214
|
-
return (
|
|
1215
|
-
<Paper
|
|
1216
|
-
key={doc.docId}
|
|
1217
|
-
withBorder
|
|
1218
|
-
radius="md"
|
|
1219
|
-
p="sm"
|
|
1220
|
-
id={
|
|
1221
|
-
docNumber
|
|
1222
|
-
? `docsearch-source-${docNumber}`
|
|
1223
|
-
: undefined
|
|
1224
|
-
}
|
|
1225
|
-
style={{ wordBreak: "break-word" }}
|
|
1226
|
-
>
|
|
1227
|
-
<Stack gap="xs">
|
|
1228
|
-
<Group
|
|
1229
|
-
justify="space-between"
|
|
1230
|
-
align="flex-start"
|
|
1231
|
-
>
|
|
1232
|
-
<Stack
|
|
1233
|
-
gap={2}
|
|
1234
|
-
style={{ flex: 1 }}
|
|
1235
|
-
data-doc-search-source
|
|
1236
|
-
>
|
|
1237
|
-
{href ? (
|
|
1238
|
-
<Anchor
|
|
1239
|
-
href={href}
|
|
1240
|
-
target="_blank"
|
|
1241
|
-
rel="noreferrer"
|
|
1242
|
-
style={{ textDecoration: "none" }}
|
|
1243
|
-
onClick={(e) => {
|
|
1244
|
-
if (!onClickDoc) return;
|
|
1245
|
-
e.preventDefault();
|
|
1246
|
-
onClickDoc?.(doc);
|
|
1247
|
-
}}
|
|
1248
|
-
data-doc-search-source-title
|
|
1249
|
-
>
|
|
1250
|
-
{titleNode}
|
|
1251
|
-
</Anchor>
|
|
1252
|
-
) : (
|
|
1253
|
-
titleNode
|
|
1254
|
-
)}
|
|
1255
|
-
|
|
1256
|
-
<Anchor
|
|
1257
|
-
href={href}
|
|
1258
|
-
target="_blank"
|
|
1259
|
-
rel="noreferrer"
|
|
1260
|
-
style={{ textDecoration: "none" }}
|
|
1261
|
-
onClick={(e) => {
|
|
1262
|
-
if (!onClickDoc) return;
|
|
1263
|
-
e.preventDefault();
|
|
1264
|
-
onClickDoc?.(doc);
|
|
1265
|
-
}}
|
|
1266
|
-
data-doc-search-source-url
|
|
1267
|
-
>
|
|
1268
|
-
{doc.sourceUrl}
|
|
1269
|
-
</Anchor>
|
|
1270
|
-
|
|
1271
|
-
{doc.author ? (
|
|
1272
|
-
<Text
|
|
1273
|
-
size="xs"
|
|
1274
|
-
c="dimmed"
|
|
1275
|
-
data-doc-search-source-author
|
|
1276
|
-
>
|
|
1277
|
-
{doc.author}
|
|
1278
|
-
</Text>
|
|
1279
|
-
) : null}
|
|
1280
|
-
|
|
1281
|
-
{doc.description ? (
|
|
1282
|
-
<Text
|
|
1283
|
-
size="sm"
|
|
1284
|
-
c="dimmed"
|
|
1285
|
-
fs="italic"
|
|
1286
|
-
data-doc-search-source-description
|
|
1287
|
-
>
|
|
1288
|
-
{doc.description}
|
|
1289
|
-
</Text>
|
|
1290
|
-
) : null}
|
|
1291
|
-
</Stack>
|
|
1292
|
-
</Group>
|
|
1293
|
-
</Stack>
|
|
1294
|
-
</Paper>
|
|
1295
|
-
);
|
|
1296
|
-
})}
|
|
1297
|
-
</Stack>
|
|
1298
|
-
</>
|
|
1299
|
-
) : null}
|
|
1300
|
-
|
|
1301
|
-
<PoweredBy variation={variation} />
|
|
1302
|
-
</Stack>
|
|
1303
|
-
</Paper>
|
|
1304
|
-
</AiFeatureBorder>
|
|
1305
|
-
</BodyComponent>
|
|
1306
|
-
</ContentComponent>
|
|
1307
|
-
</RootComponent>
|
|
1308
|
-
)}
|
|
1309
|
-
</>
|
|
1310
|
-
);
|
|
1311
|
-
};
|
|
1312
|
-
|
|
1313
|
-
export const DocSearch = withAiKitShell(DocSearchBase);
|