@sveda-ai/solid 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +557 -0
  2. package/README.md +18 -0
  3. package/dist/appearance.d.ts +9 -0
  4. package/dist/appearance.d.ts.map +1 -0
  5. package/dist/components/SvedaChat.d.ts +16 -0
  6. package/dist/components/SvedaChat.d.ts.map +1 -0
  7. package/dist/components/SvedaComposer.d.ts +17 -0
  8. package/dist/components/SvedaComposer.d.ts.map +1 -0
  9. package/dist/components/SvedaMessageList.d.ts +11 -0
  10. package/dist/components/SvedaMessageList.d.ts.map +1 -0
  11. package/dist/components/SvedaMinimizedTrigger.d.ts +10 -0
  12. package/dist/components/SvedaMinimizedTrigger.d.ts.map +1 -0
  13. package/dist/components/SvedaQuickPrompts.d.ts +14 -0
  14. package/dist/components/SvedaQuickPrompts.d.ts.map +1 -0
  15. package/dist/components/SvedaTabs.d.ts +14 -0
  16. package/dist/components/SvedaTabs.d.ts.map +1 -0
  17. package/dist/components/SvedaToolbar.d.ts +21 -0
  18. package/dist/components/SvedaToolbar.d.ts.map +1 -0
  19. package/dist/components/SvedaWelcome.d.ts +20 -0
  20. package/dist/components/SvedaWelcome.d.ts.map +1 -0
  21. package/dist/components/shell/ChatHistoryList.d.ts +11 -0
  22. package/dist/components/shell/ChatHistoryList.d.ts.map +1 -0
  23. package/dist/components/shell/SvedaFrameTicks.d.ts +2 -0
  24. package/dist/components/shell/SvedaFrameTicks.d.ts.map +1 -0
  25. package/dist/components/shell/SvedaResizeHandles.d.ts +8 -0
  26. package/dist/components/shell/SvedaResizeHandles.d.ts.map +1 -0
  27. package/dist/hooks/useSvedaChat.d.ts +33 -0
  28. package/dist/hooks/useSvedaChat.d.ts.map +1 -0
  29. package/dist/hooks/useSvedaChatPage.d.ts +99 -0
  30. package/dist/hooks/useSvedaChatPage.d.ts.map +1 -0
  31. package/dist/hooks/useSvedaStreaming.d.ts +44 -0
  32. package/dist/hooks/useSvedaStreaming.d.ts.map +1 -0
  33. package/dist/i18n/index.d.ts +16 -0
  34. package/dist/i18n/index.d.ts.map +1 -0
  35. package/dist/index.d.ts +26 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +1557 -0
  38. package/dist/launcherIcons.d.ts +7 -0
  39. package/dist/launcherIcons.d.ts.map +1 -0
  40. package/dist/lib/utils.d.ts +3 -0
  41. package/dist/lib/utils.d.ts.map +1 -0
  42. package/dist/provider.d.ts +65 -0
  43. package/dist/provider.d.ts.map +1 -0
  44. package/dist/styles.css +38 -0
  45. package/package.json +71 -0
  46. package/src/appearance.ts +75 -0
  47. package/src/chat.css +35 -0
  48. package/src/components/SvedaChat.tsx +381 -0
  49. package/src/components/SvedaComposer.tsx +116 -0
  50. package/src/components/SvedaMessageList.tsx +89 -0
  51. package/src/components/SvedaMinimizedTrigger.tsx +40 -0
  52. package/src/components/SvedaQuickPrompts.tsx +36 -0
  53. package/src/components/SvedaTabs.tsx +109 -0
  54. package/src/components/SvedaToolbar.tsx +140 -0
  55. package/src/components/SvedaWelcome.tsx +50 -0
  56. package/src/components/shell/ChatHistoryList.tsx +158 -0
  57. package/src/components/shell/SvedaFrameTicks.tsx +22 -0
  58. package/src/components/shell/SvedaResizeHandles.tsx +49 -0
  59. package/src/hooks/useSvedaChat.ts +97 -0
  60. package/src/hooks/useSvedaChatPage.ts +644 -0
  61. package/src/hooks/useSvedaStreaming.ts +102 -0
  62. package/src/i18n/index.ts +61 -0
  63. package/src/index.ts +113 -0
  64. package/src/launcherIcons.ts +41 -0
  65. package/src/lib/utils.ts +6 -0
  66. package/src/markdown.css +96 -0
  67. package/src/provider.tsx +196 -0
  68. package/src/styles.css +6 -0
  69. package/src/theme.css +137 -0
@@ -0,0 +1,644 @@
1
+ import {
2
+ applyFixedLeftResize,
3
+ applyFloatingResize,
4
+ hasPendingToolConfirmation,
5
+ pointerScreenDelta,
6
+ SVEDA_CHAT_LAYOUT_MAX_HEIGHT,
7
+ SVEDA_CHAT_LAYOUT_MAX_WIDTH,
8
+ SVEDA_CHAT_LAYOUT_MIN_HEIGHT,
9
+ SVEDA_CHAT_LAYOUT_MIN_WIDTH,
10
+ userMessageHasVisibleContent,
11
+ type SvedaUserMessageLike,
12
+ } from '@sveda-ai/chat';
13
+ import type { SvedaSendOptions } from '@sveda-ai/core';
14
+ import {
15
+ createEffect,
16
+ createMemo,
17
+ createSignal,
18
+ onMount,
19
+ type Accessor,
20
+ } from 'solid-js';
21
+ import { useSvedaChrome, useSvedaLauncher } from '../appearance';
22
+ import { useSvedaT } from '../i18n/index';
23
+ import { useSvedaConfig, useSvedaContext, type SvedaModelOption, type SvedaQuickPrompt } from '../provider';
24
+ import { useSvedaChat } from './useSvedaChat';
25
+ import { useSvedaStreaming } from './useSvedaStreaming';
26
+
27
+ const SVEDA_CHAT_MODEL_STORAGE_KEY = 'sveda.chat-model';
28
+ const SVEDA_CHAT_THINKING_STORAGE_KEY = 'sveda.chat-thinking';
29
+ const PAGE_SESSION_BOOT_KEY = '__SVEDA_CHAT_PAGE_BOOTED__';
30
+ const VIEW_MODE_STORAGE_KEY = 'sveda.chat-view-mode';
31
+ const FIXED_WIDTH_STORAGE_KEY = 'sveda.chat-fixed-width';
32
+ const CHAT_STORAGE_KEY = 'sveda.chat-dimensions';
33
+ const FIXED_MODE_BODY_CLASS = 'sveda-chat-fixed-mode';
34
+ const IMMERSIVE_BODY_CLASS = 'sveda-chat-immersive-mode';
35
+ const CHAT_WIDTH_CSS_VAR = '--sveda-chat-width';
36
+ const FIXED_RESIZE_BODY_CLASS = 'sveda-chat-fixed-resizing';
37
+ const DEFAULT_WIDTH = 384;
38
+ const DEFAULT_HEIGHT = 600;
39
+ const DEFAULT_FIXED_WIDTH = 400;
40
+ const MAIN_CONTENT_MIN_WIDTH = 768;
41
+
42
+ type SvedaChatViewMode = 'floating' | 'fixed' | 'immersive';
43
+
44
+ const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
45
+
46
+ const readChatDimensions = (): { width: number; height: number } => {
47
+ if (typeof localStorage === 'undefined') {
48
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
49
+ }
50
+ try {
51
+ const saved = localStorage.getItem(CHAT_STORAGE_KEY);
52
+ if (!saved) {
53
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
54
+ }
55
+ const parsed = JSON.parse(saved) as { width?: number; height?: number };
56
+ return {
57
+ width: clamp(Number(parsed.width) || DEFAULT_WIDTH, SVEDA_CHAT_LAYOUT_MIN_WIDTH, SVEDA_CHAT_LAYOUT_MAX_WIDTH),
58
+ height: clamp(Number(parsed.height) || DEFAULT_HEIGHT, SVEDA_CHAT_LAYOUT_MIN_HEIGHT, SVEDA_CHAT_LAYOUT_MAX_HEIGHT),
59
+ };
60
+ } catch {
61
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
62
+ }
63
+ };
64
+
65
+ const readViewMode = (hostEmbed: boolean): SvedaChatViewMode => {
66
+ if (typeof localStorage === 'undefined') {
67
+ return hostEmbed ? 'floating' : 'fixed';
68
+ }
69
+ const stored = localStorage.getItem(VIEW_MODE_STORAGE_KEY);
70
+ if (stored === 'floating' || stored === 'fixed' || stored === 'immersive') {
71
+ return stored;
72
+ }
73
+ return hostEmbed ? 'floating' : 'fixed';
74
+ };
75
+
76
+ const readNumber = (key: string, fallback: number): number => {
77
+ if (typeof localStorage === 'undefined') {
78
+ return fallback;
79
+ }
80
+ const parsed = Number(localStorage.getItem(key));
81
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
82
+ };
83
+
84
+ export interface SvedaAgentTasksPayload {
85
+ phase?: string;
86
+ tasks?: Array<{ id?: string; label?: string; status?: string }>;
87
+ }
88
+
89
+ export interface SvedaChatPageOptions {
90
+ models?: Accessor<SvedaModelOption[]>;
91
+ quickPrompts?: Accessor<SvedaQuickPrompt[]>;
92
+ brandName?: Accessor<string | undefined>;
93
+ brandLogo?: Accessor<string | undefined>;
94
+ pageUrl?: Accessor<string | undefined>;
95
+ onNavigate?: (url: string) => void;
96
+ notify?: (kind: 'error', message: string) => void;
97
+ agentTasksSubscribe?: (
98
+ onPayload: (payload: SvedaAgentTasksPayload) => void,
99
+ ) => void | (() => void);
100
+ }
101
+
102
+ const svedaErrorMessage = (error: unknown, fallback: string): string => {
103
+ if (error instanceof Error && error.message.trim()) {
104
+ return error.message;
105
+ }
106
+ if (error && typeof error === 'object' && 'message' in error) {
107
+ const message = (error as { message?: unknown }).message;
108
+ if (typeof message === 'string' && message.trim()) {
109
+ return message;
110
+ }
111
+ }
112
+ return fallback;
113
+ };
114
+
115
+ export function useSvedaChatPage(options: SvedaChatPageOptions = {}) {
116
+ const t = useSvedaT();
117
+ const config = useSvedaConfig();
118
+ const ctx = useSvedaContext();
119
+ const launcher = useSvedaLauncher();
120
+ const chrome = useSvedaChrome();
121
+
122
+ const chatStore = useSvedaChat();
123
+ const {
124
+ currentChat,
125
+ sortedHistories,
126
+ openChatTabs,
127
+ isMinimized,
128
+ isLoading,
129
+ isLoadingChatHistory,
130
+ hasCurrentChat,
131
+ loadHistories,
132
+ createNewChat,
133
+ setCurrentChat,
134
+ setChatTitle,
135
+ refreshChatTitleFromServer,
136
+ setChatMessages,
137
+ incrementChatTokens,
138
+ setChatContextWindowTokens,
139
+ deleteChat,
140
+ renameChat,
141
+ closeChatTab,
142
+ openChat,
143
+ maximizeChat,
144
+ minimizeChat,
145
+ setLoading,
146
+ } = chatStore;
147
+
148
+ const [inputMessage, setInputMessage] = createSignal('');
149
+ const [isMobile, setIsMobile] = createSignal(
150
+ typeof window !== 'undefined' ? window.matchMedia('(max-width: 768px)').matches : false,
151
+ );
152
+ const initialSize = readChatDimensions();
153
+ const [chatWidth, setChatWidth] = createSignal(initialSize.width);
154
+ const [chatHeight, setChatHeight] = createSignal(initialSize.height);
155
+ const [fixedWidth, setFixedWidth] = createSignal(readNumber(FIXED_WIDTH_STORAGE_KEY, DEFAULT_FIXED_WIDTH));
156
+ const [viewMode, setViewMode] = createSignal<SvedaChatViewMode>(readViewMode(ctx.hostEmbed));
157
+ const [lastNonImmersiveViewMode, setLastNonImmersiveViewMode] = createSignal<'floating' | 'fixed'>(
158
+ ctx.hostEmbed ? 'floating' : 'fixed',
159
+ );
160
+ const [showHistorySidebar, setShowHistorySidebar] = createSignal(false);
161
+
162
+ const models = createMemo(() => options.models?.() ?? config.models);
163
+ const quickPrompts = createMemo(() => options.quickPrompts?.() ?? config.quickPrompts);
164
+
165
+ const readStoredModel = (): string => {
166
+ if (typeof window === 'undefined') {
167
+ return models()[0]?.id ?? '';
168
+ }
169
+ const saved = localStorage.getItem(SVEDA_CHAT_MODEL_STORAGE_KEY);
170
+ if (saved && models().some(option => option.id === saved)) {
171
+ return saved;
172
+ }
173
+ return models()[0]?.id ?? '';
174
+ };
175
+
176
+ const [selectedChatModel, setSelectedChatModel] = createSignal(readStoredModel());
177
+ const [thinkingEnabled, setThinkingEnabled] = createSignal(
178
+ typeof window === 'undefined'
179
+ ? true
180
+ : localStorage.getItem(SVEDA_CHAT_THINKING_STORAGE_KEY) !== '0',
181
+ );
182
+
183
+ createEffect(() => {
184
+ const list = models();
185
+ if (list.length > 0 && !list.some(option => option.id === selectedChatModel())) {
186
+ setSelectedChatModel(list[0].id);
187
+ }
188
+ });
189
+
190
+ createEffect(() => {
191
+ const value = selectedChatModel();
192
+ if (typeof window !== 'undefined' && value) {
193
+ localStorage.setItem(SVEDA_CHAT_MODEL_STORAGE_KEY, value);
194
+ }
195
+ });
196
+
197
+ createEffect(() => {
198
+ if (typeof window !== 'undefined') {
199
+ localStorage.setItem(SVEDA_CHAT_THINKING_STORAGE_KEY, thinkingEnabled() ? '1' : '0');
200
+ }
201
+ });
202
+
203
+ onMount(() => {
204
+ const mq = window.matchMedia('(max-width: 768px)');
205
+ const onChange = () => setIsMobile(mq.matches && !ctx.hostEmbed);
206
+ onChange();
207
+ mq.addEventListener('change', onChange);
208
+ return () => mq.removeEventListener('change', onChange);
209
+ });
210
+
211
+ createEffect(() => {
212
+ if (typeof document === 'undefined') {
213
+ return;
214
+ }
215
+ const body = document.body;
216
+ body.classList.remove(FIXED_MODE_BODY_CLASS, IMMERSIVE_BODY_CLASS);
217
+ document.documentElement.style.removeProperty(CHAT_WIDTH_CSS_VAR);
218
+ body.style.removeProperty(CHAT_WIDTH_CSS_VAR);
219
+ if (!isMinimized() && !isMobile() && viewMode() === 'fixed') {
220
+ body.classList.add(FIXED_MODE_BODY_CLASS);
221
+ body.style.setProperty(CHAT_WIDTH_CSS_VAR, `${fixedWidth()}px`);
222
+ }
223
+ if (!isMinimized() && !isMobile() && viewMode() === 'immersive') {
224
+ body.classList.add(IMMERSIVE_BODY_CLASS);
225
+ }
226
+ });
227
+
228
+ const saveViewMode = (mode: SvedaChatViewMode) => {
229
+ localStorage.setItem(VIEW_MODE_STORAGE_KEY, mode);
230
+ };
231
+
232
+ const toggleViewMode = () => {
233
+ if (viewMode() === 'immersive') {
234
+ return;
235
+ }
236
+ const next: SvedaChatViewMode = viewMode() === 'floating' ? 'fixed' : 'floating';
237
+ setViewMode(next);
238
+ setLastNonImmersiveViewMode(next);
239
+ saveViewMode(next);
240
+ };
241
+
242
+ const enterImmersiveMode = () => {
243
+ if (isMobile() || viewMode() === 'immersive') {
244
+ return;
245
+ }
246
+ setLastNonImmersiveViewMode(viewMode() === 'floating' ? 'floating' : 'fixed');
247
+ setViewMode('immersive');
248
+ saveViewMode('immersive');
249
+ };
250
+
251
+ const exitImmersiveMode = () => {
252
+ if (viewMode() !== 'immersive') {
253
+ return;
254
+ }
255
+ const next = lastNonImmersiveViewMode();
256
+ setViewMode(next);
257
+ saveViewMode(next);
258
+ };
259
+
260
+ const startResize = (direction: string, event: PointerEvent) => {
261
+ event.preventDefault();
262
+ event.stopPropagation();
263
+ const handle = event.currentTarget;
264
+ if (!(handle instanceof HTMLElement)) {
265
+ return;
266
+ }
267
+ const pointerId = event.pointerId;
268
+ try {
269
+ handle.setPointerCapture(pointerId);
270
+ } catch {
271
+ // Capture is best-effort.
272
+ }
273
+
274
+ const startX = event.screenX;
275
+ const startY = event.screenY;
276
+ const mode = viewMode();
277
+ const startWidth = mode === 'fixed' ? fixedWidth() : chatWidth();
278
+ const startHeight = chatHeight();
279
+ let latestWidth = startWidth;
280
+ let latestHeight = startHeight;
281
+ let finished = false;
282
+
283
+ const stop = () => {
284
+ window.removeEventListener('pointermove', onMove);
285
+ window.removeEventListener('pointerup', onUp);
286
+ window.removeEventListener('pointercancel', onUp);
287
+ try {
288
+ handle.releasePointerCapture(pointerId);
289
+ } catch {
290
+ // Already released.
291
+ }
292
+ };
293
+
294
+ const onUp = () => {
295
+ if (finished) {
296
+ return;
297
+ }
298
+ finished = true;
299
+ document.body.classList.remove(FIXED_RESIZE_BODY_CLASS);
300
+ if (mode === 'floating') {
301
+ localStorage.setItem(CHAT_STORAGE_KEY, JSON.stringify({ width: latestWidth, height: latestHeight }));
302
+ }
303
+ if (mode === 'fixed') {
304
+ localStorage.setItem(FIXED_WIDTH_STORAGE_KEY, String(latestWidth));
305
+ }
306
+ stop();
307
+ };
308
+
309
+ const onMove = (moveEvent: PointerEvent) => {
310
+ if (finished || moveEvent.pointerId !== pointerId) {
311
+ return;
312
+ }
313
+ const { deltaX, deltaY } = pointerScreenDelta(moveEvent, startX, startY);
314
+ if (mode === 'fixed' && direction === 'fixed-left') {
315
+ document.body.classList.add(FIXED_RESIZE_BODY_CLASS);
316
+ const maxChat = Math.max(SVEDA_CHAT_LAYOUT_MIN_WIDTH, window.innerWidth - MAIN_CONTENT_MIN_WIDTH);
317
+ const result = applyFixedLeftResize(startWidth, deltaX, maxChat);
318
+ latestWidth = result.width;
319
+ setFixedWidth(result.width);
320
+ if (result.enterImmersive) {
321
+ setLastNonImmersiveViewMode('fixed');
322
+ setViewMode('immersive');
323
+ localStorage.setItem(VIEW_MODE_STORAGE_KEY, 'immersive');
324
+ localStorage.setItem(FIXED_WIDTH_STORAGE_KEY, String(result.width));
325
+ finished = true;
326
+ document.body.classList.remove(FIXED_RESIZE_BODY_CLASS);
327
+ stop();
328
+ }
329
+ return;
330
+ }
331
+ if (mode === 'floating') {
332
+ const next = applyFloatingResize(direction, startWidth, startHeight, deltaX, deltaY);
333
+ latestWidth = next.width;
334
+ latestHeight = next.height;
335
+ setChatWidth(next.width);
336
+ setChatHeight(next.height);
337
+ }
338
+ };
339
+
340
+ window.addEventListener('pointermove', onMove);
341
+ window.addEventListener('pointerup', onUp);
342
+ window.addEventListener('pointercancel', onUp);
343
+ };
344
+
345
+ const isImmersiveDesktop = createMemo(
346
+ () => !isMobile() && !isMinimized() && viewMode() === 'immersive',
347
+ );
348
+
349
+ const scrollToBottom = (_opts?: { behavior?: string; onlyIfNearBottom?: boolean }) => {
350
+ const el = document.querySelector('.sveda-chat [data-sveda-messages]');
351
+ if (el instanceof HTMLElement) {
352
+ el.scrollTop = el.scrollHeight;
353
+ }
354
+ };
355
+
356
+ const resolveStreamingSendOptions = (): Pick<SvedaSendOptions, 'model' | 'options'> => {
357
+ const supportsThinking =
358
+ models().find(option => option.id === selectedChatModel())?.supportsThinking ?? false;
359
+ return {
360
+ model: selectedChatModel() || undefined,
361
+ options: {
362
+ thinking: Boolean(chrome.thinking && supportsThinking && thinkingEnabled()),
363
+ },
364
+ };
365
+ };
366
+
367
+ const streaming = useSvedaStreaming(
368
+ {
369
+ currentChat,
370
+ setChatMessages,
371
+ setChatTitle,
372
+ refreshChatTitleFromServer,
373
+ incrementChatTokens,
374
+ setChatContextWindowTokens,
375
+ },
376
+ scrollToBottom,
377
+ {
378
+ newChatLabel: () => t('newChat'),
379
+ resolveSendOptions: resolveStreamingSendOptions,
380
+ onError: (_chatId, error) => {
381
+ options.notify?.('error', svedaErrorMessage(error, t('errorSendingMessage')));
382
+ },
383
+ },
384
+ );
385
+
386
+ const messages = createMemo(() => currentChat()?.messages ?? []);
387
+ const hasUserMessages = createMemo(() =>
388
+ (messages() as SvedaUserMessageLike[]).some(userMessageHasVisibleContent),
389
+ );
390
+
391
+ const quickPromptButtons = createMemo(() => {
392
+ const list = quickPrompts();
393
+ if (!Array.isArray(list)) return [];
394
+ return list
395
+ .map(item => {
396
+ const label = String(item?.label ?? '').trim();
397
+ const prompt = String(item?.prompt ?? '').trim();
398
+ return label && prompt ? { label, prompt } : null;
399
+ })
400
+ .filter((item): item is SvedaQuickPrompt => !!item);
401
+ });
402
+
403
+ const showQuickPromptButtons = createMemo(() => {
404
+ if (!currentChat() || quickPromptButtons().length === 0) return false;
405
+ return !hasUserMessages();
406
+ });
407
+
408
+ const confirmationPending = createMemo(() =>
409
+ hasPendingToolConfirmation(currentChat()?.messages),
410
+ );
411
+
412
+ const brandDisplayName = createMemo(
413
+ () => options.brandName?.() ?? config.brand.name ?? 'Sveda',
414
+ );
415
+ const brandLogo = createMemo(() => options.brandLogo?.() ?? config.brand.logoUrl ?? null);
416
+ const headerTitle = createMemo(() => currentChat()?.title || t('chatTitle'));
417
+ const launcherLabel = createMemo(() => {
418
+ const custom = launcher.label.trim();
419
+ return custom !== '' ? custom : brandDisplayName();
420
+ });
421
+ const launcherIcon = createMemo(() => launcher.icon);
422
+ const launcherImage = createMemo(() => {
423
+ const uploaded = launcher.image.trim();
424
+ if (uploaded !== '') return uploaded;
425
+ return brandLogo() || '';
426
+ });
427
+
428
+ const fillHost = ctx.fillHost;
429
+ const hideLauncher = ctx.hideLauncher;
430
+
431
+ const chatShellStyle = createMemo(() => {
432
+ if (fillHost && !isMinimized()) {
433
+ return { width: '100%', height: '100%' };
434
+ }
435
+ if (!isMobile() && !isMinimized() && viewMode() === 'immersive') {
436
+ return { width: '100vw', height: '100dvh' };
437
+ }
438
+ if (!isMobile() && !isMinimized() && viewMode() === 'fixed') {
439
+ return { width: 'var(--sveda-chat-width)', height: '100dvh' };
440
+ }
441
+ return undefined;
442
+ });
443
+
444
+ const chatCardStyle = createMemo(() => {
445
+ if (isMobile() || fillHost) {
446
+ return { width: '100%', height: '100%' };
447
+ }
448
+ if (viewMode() === 'fixed') {
449
+ return { width: 'var(--sveda-chat-width)', height: '100%' };
450
+ }
451
+ if (viewMode() === 'floating') {
452
+ return { width: `${chatWidth()}px`, height: `${chatHeight()}px` };
453
+ }
454
+ return { width: '100%', height: '100%' };
455
+ });
456
+
457
+ const floatShellStyle = createMemo(() => {
458
+ if (fillHost) {
459
+ return { height: '100%', minHeight: 0 };
460
+ }
461
+ if (isMobile() || viewMode() !== 'floating') {
462
+ return undefined;
463
+ }
464
+ return { height: `${chatHeight()}px`, minHeight: 0 };
465
+ });
466
+
467
+ const sendMessage = async () => {
468
+ if (isLoading() || confirmationPending() || !currentChat()) return;
469
+ const text = inputMessage().trim();
470
+ if (!text) return;
471
+
472
+ try {
473
+ setLoading(true);
474
+ await ctx.beforeSend?.();
475
+ } catch {
476
+ return;
477
+ } finally {
478
+ setLoading(false);
479
+ }
480
+
481
+ maximizeChat();
482
+ setInputMessage('');
483
+ await streaming.sendMessage(
484
+ { displayText: text, promptText: text },
485
+ {},
486
+ );
487
+ scrollToBottom({ behavior: 'smooth', onlyIfNearBottom: false });
488
+ };
489
+
490
+ const sendQuickPrompt = async (prompt: string) => {
491
+ if (!prompt || isLoading() || confirmationPending() || !currentChat()) return;
492
+ setInputMessage(prompt);
493
+ await sendMessage();
494
+ };
495
+
496
+ const handleNewChat = () => {
497
+ createNewChat();
498
+ };
499
+
500
+ const handleSelectChat = async (chatId: string) => {
501
+ streaming.clearChatUnread(chatId);
502
+ await setCurrentChat(chatId);
503
+ scrollToBottom({ behavior: 'auto', onlyIfNearBottom: false });
504
+ };
505
+
506
+ onMount(async () => {
507
+ await loadHistories();
508
+ const globalScope = window as unknown as Record<string, unknown>;
509
+ const isPageBooted = Boolean(globalScope[PAGE_SESSION_BOOT_KEY]);
510
+ if (!isPageBooted) {
511
+ globalScope[PAGE_SESSION_BOOT_KEY] = true;
512
+ createNewChat();
513
+ } else if (!currentChat()) {
514
+ if (sortedHistories().length > 0) {
515
+ await setCurrentChat(sortedHistories()[0].id);
516
+ } else {
517
+ createNewChat();
518
+ }
519
+ }
520
+ scrollToBottom();
521
+ });
522
+
523
+ const shellClass = createMemo(() => {
524
+ const classes = ['sveda-chat', 'flex', 'flex-col'];
525
+ if (fillHost) {
526
+ classes.push('relative', 'h-full', 'min-h-0', 'w-full', 'overflow-hidden', 'items-stretch');
527
+ } else if (isMinimized()) {
528
+ classes.push(
529
+ 'fixed',
530
+ 'bottom-0',
531
+ 'right-0',
532
+ 'z-50',
533
+ 'min-[1872px]:bottom-4',
534
+ 'min-[1872px]:right-4',
535
+ 'items-end',
536
+ 'gap-2',
537
+ );
538
+ } else if (isMobile()) {
539
+ classes.push(
540
+ 'sveda-chat-surface',
541
+ 'fixed',
542
+ 'inset-0',
543
+ 'z-50',
544
+ 'pb-[env(safe-area-inset-bottom)]',
545
+ 'pt-[env(safe-area-inset-top)]',
546
+ 'items-stretch',
547
+ );
548
+ } else if (viewMode() === 'immersive') {
549
+ classes.push(
550
+ 'sveda-chat-surface',
551
+ 'fixed',
552
+ 'right-0',
553
+ 'top-0',
554
+ 'z-50',
555
+ 'h-[100dvh]',
556
+ 'pb-[env(safe-area-inset-bottom)]',
557
+ 'pt-[env(safe-area-inset-top)]',
558
+ 'items-stretch',
559
+ );
560
+ } else if (viewMode() === 'fixed') {
561
+ classes.push(
562
+ 'sveda-chat-surface',
563
+ 'fixed',
564
+ 'right-0',
565
+ 'top-0',
566
+ 'z-50',
567
+ 'h-screen',
568
+ 'pt-[env(safe-area-inset-top)]',
569
+ 'items-stretch',
570
+ );
571
+ } else {
572
+ classes.push('fixed', 'bottom-4', 'right-4', 'z-50', 'items-end', 'gap-2');
573
+ }
574
+ return classes.join(' ');
575
+ });
576
+
577
+ return {
578
+ t,
579
+ currentChat,
580
+ sortedHistories,
581
+ openChatTabs,
582
+ isMinimized,
583
+ isLoading,
584
+ isLoadingChatHistory,
585
+ hasCurrentChat,
586
+ isMobile,
587
+ viewMode,
588
+ setViewMode,
589
+ showHistorySidebar,
590
+ setShowHistorySidebar,
591
+ isImmersiveDesktop,
592
+ fixedWidth,
593
+ toggleViewMode,
594
+ enterImmersiveMode,
595
+ exitImmersiveMode,
596
+ startResize,
597
+ fillHost,
598
+ hideLauncher,
599
+ inputMessage,
600
+ setInputMessage,
601
+ selectedChatModel,
602
+ setSelectedChatModel,
603
+ thinkingEnabled,
604
+ setThinkingEnabled,
605
+ chatModels: models,
606
+ messages,
607
+ hasUserMessages,
608
+ quickPromptButtons,
609
+ showQuickPromptButtons,
610
+ confirmationPending,
611
+ brandDisplayName,
612
+ brandLogo,
613
+ headerTitle,
614
+ launcherLabel,
615
+ launcherIcon,
616
+ launcherImage,
617
+ chatShellStyle,
618
+ chatCardStyle,
619
+ floatShellStyle,
620
+ shellClass,
621
+ chatWidth,
622
+ chatHeight,
623
+ thinkingTooltipText: createMemo(() => t('thinkingTooltip')),
624
+ sendMessage,
625
+ sendQuickPrompt,
626
+ handleNewChat,
627
+ handleSelectChat,
628
+ deleteChat,
629
+ renameChat,
630
+ closeChatTab,
631
+ openChat,
632
+ minimizeChat,
633
+ maximizeChat,
634
+ isStreaming: streaming.isStreaming,
635
+ isThinking: streaming.isThinking,
636
+ thinkingMessage: streaming.thinkingMessage,
637
+ stopStreaming: streaming.stopStreaming,
638
+ streamingChatIds: streaming.streamingChatIds,
639
+ unreadChatIds: streaming.unreadChatIds,
640
+ resolveToolConfirmation: streaming.resolveToolConfirmation,
641
+ pageUrl: createMemo(() => options.pageUrl?.() ?? ''),
642
+ onNavigate: options.onNavigate,
643
+ };
644
+ }