@sveda-ai/react 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 (65) hide show
  1. package/LICENSE +557 -0
  2. package/README.md +18 -0
  3. package/dist/appearance.d.ts +12 -0
  4. package/dist/appearance.d.ts.map +1 -0
  5. package/dist/components/SvedaChat.d.ts +14 -0
  6. package/dist/components/SvedaChat.d.ts.map +1 -0
  7. package/dist/components/shell/ChatHistoryList.d.ts +10 -0
  8. package/dist/components/shell/ChatHistoryList.d.ts.map +1 -0
  9. package/dist/components/shell/ChatMessageList.d.ts +7 -0
  10. package/dist/components/shell/ChatMessageList.d.ts.map +1 -0
  11. package/dist/components/shell/SvedaComposer.d.ts +11 -0
  12. package/dist/components/shell/SvedaComposer.d.ts.map +1 -0
  13. package/dist/components/shell/SvedaFrameTicks.d.ts +2 -0
  14. package/dist/components/shell/SvedaFrameTicks.d.ts.map +1 -0
  15. package/dist/components/shell/SvedaMinimizedTrigger.d.ts +9 -0
  16. package/dist/components/shell/SvedaMinimizedTrigger.d.ts.map +1 -0
  17. package/dist/components/shell/SvedaResizeHandles.d.ts +9 -0
  18. package/dist/components/shell/SvedaResizeHandles.d.ts.map +1 -0
  19. package/dist/components/shell/SvedaTabs.d.ts +13 -0
  20. package/dist/components/shell/SvedaTabs.d.ts.map +1 -0
  21. package/dist/components/shell/SvedaToolbar.d.ts +21 -0
  22. package/dist/components/shell/SvedaToolbar.d.ts.map +1 -0
  23. package/dist/hooks/useSvedaChat.d.ts +33 -0
  24. package/dist/hooks/useSvedaChat.d.ts.map +1 -0
  25. package/dist/hooks/useSvedaChatLayout.d.ts +25 -0
  26. package/dist/hooks/useSvedaChatLayout.d.ts.map +1 -0
  27. package/dist/hooks/useSvedaStreaming.d.ts +43 -0
  28. package/dist/hooks/useSvedaStreaming.d.ts.map +1 -0
  29. package/dist/i18n.d.ts +14 -0
  30. package/dist/i18n.d.ts.map +1 -0
  31. package/dist/index.d.ts +25 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +1238 -0
  34. package/dist/launcherIcons.d.ts +5 -0
  35. package/dist/launcherIcons.d.ts.map +1 -0
  36. package/dist/lib/utils.d.ts +3 -0
  37. package/dist/lib/utils.d.ts.map +1 -0
  38. package/dist/provider.d.ts +15 -0
  39. package/dist/provider.d.ts.map +1 -0
  40. package/dist/styles.css +3 -0
  41. package/dist/types.d.ts +58 -0
  42. package/dist/types.d.ts.map +1 -0
  43. package/package.json +74 -0
  44. package/src/appearance.ts +81 -0
  45. package/src/components/SvedaChat.tsx +428 -0
  46. package/src/components/shell/ChatHistoryList.tsx +151 -0
  47. package/src/components/shell/ChatMessageList.tsx +52 -0
  48. package/src/components/shell/SvedaComposer.tsx +100 -0
  49. package/src/components/shell/SvedaFrameTicks.tsx +22 -0
  50. package/src/components/shell/SvedaMinimizedTrigger.tsx +41 -0
  51. package/src/components/shell/SvedaResizeHandles.tsx +51 -0
  52. package/src/components/shell/SvedaTabs.tsx +106 -0
  53. package/src/components/shell/SvedaToolbar.tsx +132 -0
  54. package/src/hooks/useSvedaChat.ts +91 -0
  55. package/src/hooks/useSvedaChatLayout.ts +281 -0
  56. package/src/hooks/useSvedaStreaming.ts +139 -0
  57. package/src/i18n.ts +51 -0
  58. package/src/index.ts +116 -0
  59. package/src/launcherIcons.ts +43 -0
  60. package/src/lib/utils.ts +6 -0
  61. package/src/markdown.css +96 -0
  62. package/src/provider.tsx +179 -0
  63. package/src/styles.css +6 -0
  64. package/src/theme.css +137 -0
  65. package/src/types.ts +70 -0
@@ -0,0 +1,281 @@
1
+ import {
2
+ applyFixedLeftResize,
3
+ applyFloatingResize,
4
+ pointerScreenDelta,
5
+ SVEDA_CHAT_LAYOUT_MAX_HEIGHT,
6
+ SVEDA_CHAT_LAYOUT_MAX_WIDTH,
7
+ SVEDA_CHAT_LAYOUT_MIN_HEIGHT,
8
+ SVEDA_CHAT_LAYOUT_MIN_WIDTH,
9
+ } from '@sveda-ai/chat';
10
+ import { useCallback, useEffect, useRef, useState, type PointerEvent as ReactPointerEvent } from 'react';
11
+
12
+ export type SvedaChatViewMode = 'floating' | 'fixed' | 'immersive';
13
+
14
+ const VIEW_MODE_STORAGE_KEY = 'sveda.chat-view-mode';
15
+ const FIXED_WIDTH_STORAGE_KEY = 'sveda.chat-fixed-width';
16
+ const CHAT_STORAGE_KEY = 'sveda.chat-dimensions';
17
+ const FIXED_MODE_BODY_CLASS = 'sveda-chat-fixed-mode';
18
+ const IMMERSIVE_BODY_CLASS = 'sveda-chat-immersive-mode';
19
+ const CHAT_WIDTH_CSS_VAR = '--sveda-chat-width';
20
+ const FIXED_RESIZE_BODY_CLASS = 'sveda-chat-fixed-resizing';
21
+ const DEFAULT_WIDTH = 384;
22
+ const DEFAULT_HEIGHT = 600;
23
+ const DEFAULT_FIXED_WIDTH = 400;
24
+ const MAIN_CONTENT_MIN_WIDTH = 768;
25
+
26
+ const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
27
+
28
+ const readChatDimensions = (): { width: number; height: number } => {
29
+ if (typeof localStorage === 'undefined') {
30
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
31
+ }
32
+ try {
33
+ const saved = localStorage.getItem(CHAT_STORAGE_KEY);
34
+ if (!saved) {
35
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
36
+ }
37
+ const parsed = JSON.parse(saved) as { width?: number; height?: number };
38
+ return {
39
+ width: clamp(Number(parsed.width) || DEFAULT_WIDTH, SVEDA_CHAT_LAYOUT_MIN_WIDTH, SVEDA_CHAT_LAYOUT_MAX_WIDTH),
40
+ height: clamp(Number(parsed.height) || DEFAULT_HEIGHT, SVEDA_CHAT_LAYOUT_MIN_HEIGHT, SVEDA_CHAT_LAYOUT_MAX_HEIGHT),
41
+ };
42
+ } catch {
43
+ return { width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT };
44
+ }
45
+ };
46
+
47
+ const readViewMode = (hostEmbed: boolean): SvedaChatViewMode => {
48
+ if (typeof localStorage === 'undefined') {
49
+ return hostEmbed ? 'floating' : 'fixed';
50
+ }
51
+ const stored = localStorage.getItem(VIEW_MODE_STORAGE_KEY);
52
+ if (stored === 'floating' || stored === 'fixed' || stored === 'immersive') {
53
+ return stored;
54
+ }
55
+ return hostEmbed ? 'floating' : 'fixed';
56
+ };
57
+
58
+ const readNumber = (key: string, fallback: number): number => {
59
+ if (typeof localStorage === 'undefined') {
60
+ return fallback;
61
+ }
62
+ const parsed = Number(localStorage.getItem(key));
63
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
64
+ };
65
+
66
+ export function useSvedaChatLayout(isMinimized: boolean, hostEmbed: boolean, fillHost: boolean) {
67
+ const [viewMode, setViewMode] = useState<SvedaChatViewMode>(() => readViewMode(hostEmbed));
68
+ const [lastNonImmersiveViewMode, setLastNonImmersiveViewMode] = useState<SvedaChatViewMode>(() =>
69
+ hostEmbed ? 'floating' : 'fixed'
70
+ );
71
+ const [isMobile, setIsMobile] = useState(false);
72
+ const [chatWidth, setChatWidth] = useState(() => readChatDimensions().width);
73
+ const [chatHeight, setChatHeight] = useState(() => readChatDimensions().height);
74
+ const [fixedWidth, setFixedWidth] = useState(() => readNumber(FIXED_WIDTH_STORAGE_KEY, DEFAULT_FIXED_WIDTH));
75
+ const sizeRef = useRef({ chatWidth, chatHeight, fixedWidth, viewMode });
76
+ sizeRef.current = { chatWidth, chatHeight, fixedWidth, viewMode };
77
+
78
+ useEffect(() => {
79
+ if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
80
+ return;
81
+ }
82
+ const media = window.matchMedia('(max-width: 768px)');
83
+ const apply = () => setIsMobile(hostEmbed ? false : media.matches);
84
+ apply();
85
+ media.addEventListener('change', apply);
86
+ return () => media.removeEventListener('change', apply);
87
+ }, [hostEmbed]);
88
+
89
+ useEffect(() => {
90
+ if (typeof document === 'undefined') {
91
+ return;
92
+ }
93
+ const body = document.body;
94
+ body.classList.remove(FIXED_MODE_BODY_CLASS, IMMERSIVE_BODY_CLASS);
95
+ document.documentElement.style.removeProperty(CHAT_WIDTH_CSS_VAR);
96
+ body.style.removeProperty(CHAT_WIDTH_CSS_VAR);
97
+
98
+ if (!isMinimized && !isMobile && viewMode === 'fixed') {
99
+ body.classList.add(FIXED_MODE_BODY_CLASS);
100
+ body.style.setProperty(CHAT_WIDTH_CSS_VAR, `${fixedWidth}px`);
101
+ }
102
+ if (!isMinimized && !isMobile && viewMode === 'immersive') {
103
+ body.classList.add(IMMERSIVE_BODY_CLASS);
104
+ }
105
+
106
+ return () => {
107
+ body.classList.remove(FIXED_MODE_BODY_CLASS, IMMERSIVE_BODY_CLASS);
108
+ body.style.removeProperty(CHAT_WIDTH_CSS_VAR);
109
+ };
110
+ }, [fixedWidth, isMinimized, isMobile, viewMode]);
111
+
112
+ const saveViewMode = (mode: SvedaChatViewMode) => {
113
+ localStorage.setItem(VIEW_MODE_STORAGE_KEY, mode);
114
+ };
115
+
116
+ const toggleViewMode = () => {
117
+ if (viewMode === 'immersive') {
118
+ return;
119
+ }
120
+ const next: SvedaChatViewMode = viewMode === 'floating' ? 'fixed' : 'floating';
121
+ setViewMode(next);
122
+ setLastNonImmersiveViewMode(next);
123
+ saveViewMode(next);
124
+ };
125
+
126
+ const enterImmersiveMode = () => {
127
+ if (isMobile || viewMode === 'immersive') {
128
+ return;
129
+ }
130
+ setLastNonImmersiveViewMode(viewMode);
131
+ setViewMode('immersive');
132
+ saveViewMode('immersive');
133
+ };
134
+
135
+ const exitImmersiveMode = () => {
136
+ if (viewMode !== 'immersive') {
137
+ return;
138
+ }
139
+ const next = lastNonImmersiveViewMode === 'immersive' ? 'fixed' : lastNonImmersiveViewMode;
140
+ setViewMode(next);
141
+ saveViewMode(next);
142
+ };
143
+
144
+ const startResize = useCallback((direction: string, event: ReactPointerEvent<HTMLElement>) => {
145
+ event.preventDefault();
146
+ event.stopPropagation();
147
+ const handle = event.currentTarget;
148
+ const pointerId = event.pointerId;
149
+ try {
150
+ handle.setPointerCapture(pointerId);
151
+ } catch {
152
+ // Capture is best-effort.
153
+ }
154
+
155
+ const startX = event.screenX;
156
+ const startY = event.screenY;
157
+ const mode = sizeRef.current.viewMode;
158
+ const startWidth = mode === 'fixed' ? sizeRef.current.fixedWidth : sizeRef.current.chatWidth;
159
+ const startHeight = sizeRef.current.chatHeight;
160
+ let latestWidth = startWidth;
161
+ let latestHeight = startHeight;
162
+ let finished = false;
163
+
164
+ const stop = () => {
165
+ window.removeEventListener('pointermove', onMove);
166
+ window.removeEventListener('pointerup', onUp);
167
+ window.removeEventListener('pointercancel', onUp);
168
+ try {
169
+ handle.releasePointerCapture(pointerId);
170
+ } catch {
171
+ // Already released.
172
+ }
173
+ };
174
+
175
+ const onUp = () => {
176
+ if (finished) {
177
+ return;
178
+ }
179
+ finished = true;
180
+ document.body.classList.remove(FIXED_RESIZE_BODY_CLASS);
181
+ if (mode === 'floating') {
182
+ localStorage.setItem(CHAT_STORAGE_KEY, JSON.stringify({ width: latestWidth, height: latestHeight }));
183
+ }
184
+ if (mode === 'fixed') {
185
+ localStorage.setItem(FIXED_WIDTH_STORAGE_KEY, String(latestWidth));
186
+ }
187
+ stop();
188
+ };
189
+
190
+ const onMove = (moveEvent: PointerEvent) => {
191
+ if (finished || moveEvent.pointerId !== pointerId) {
192
+ return;
193
+ }
194
+ const { deltaX, deltaY } = pointerScreenDelta(moveEvent, startX, startY);
195
+ if (mode === 'fixed' && direction === 'fixed-left') {
196
+ document.body.classList.add(FIXED_RESIZE_BODY_CLASS);
197
+ const maxChat = Math.max(SVEDA_CHAT_LAYOUT_MIN_WIDTH, window.innerWidth - MAIN_CONTENT_MIN_WIDTH);
198
+ const result = applyFixedLeftResize(startWidth, deltaX, maxChat);
199
+ latestWidth = result.width;
200
+ setFixedWidth(result.width);
201
+ if (result.enterImmersive) {
202
+ setLastNonImmersiveViewMode('fixed');
203
+ setViewMode('immersive');
204
+ localStorage.setItem(VIEW_MODE_STORAGE_KEY, 'immersive');
205
+ localStorage.setItem(FIXED_WIDTH_STORAGE_KEY, String(result.width));
206
+ finished = true;
207
+ document.body.classList.remove(FIXED_RESIZE_BODY_CLASS);
208
+ stop();
209
+ }
210
+ return;
211
+ }
212
+ if (mode === 'floating') {
213
+ const next = applyFloatingResize(direction, startWidth, startHeight, deltaX, deltaY);
214
+ latestWidth = next.width;
215
+ latestHeight = next.height;
216
+ setChatWidth(next.width);
217
+ setChatHeight(next.height);
218
+ }
219
+ };
220
+
221
+ window.addEventListener('pointermove', onMove);
222
+ window.addEventListener('pointerup', onUp);
223
+ window.addEventListener('pointercancel', onUp);
224
+ }, []);
225
+
226
+ const isImmersiveDesktop = !isMobile && !isMinimized && viewMode === 'immersive';
227
+
228
+ const chatShellClass = [
229
+ 'sveda-chat flex flex-col',
230
+ fillHost && !isMinimized ? 'relative h-full min-h-0 w-full overflow-hidden' : '',
231
+ !fillHost && isMinimized ? 'fixed bottom-0 right-0 z-50 min-[1872px]:bottom-4 min-[1872px]:right-4' : '',
232
+ !fillHost && !isMinimized && isMobile
233
+ ? 'sveda-chat-surface fixed inset-0 z-50 pb-[env(safe-area-inset-bottom)] pt-[env(safe-area-inset-top)]'
234
+ : '',
235
+ !fillHost && !isMinimized && !isMobile && viewMode === 'immersive'
236
+ ? 'sveda-chat-surface fixed right-0 top-0 z-50 h-[100dvh] pb-[env(safe-area-inset-bottom)] pt-[env(safe-area-inset-top)]'
237
+ : '',
238
+ !fillHost && !isMinimized && !isMobile && viewMode === 'fixed'
239
+ ? 'sveda-chat-surface fixed right-0 top-0 z-50 h-screen pt-[env(safe-area-inset-top)]'
240
+ : '',
241
+ !fillHost && !isMinimized && !isMobile && viewMode === 'floating' ? 'fixed bottom-4 right-4 z-50' : '',
242
+ fillHost || isMobile || viewMode === 'fixed' || viewMode === 'immersive' ? 'items-stretch' : 'items-end gap-2',
243
+ ]
244
+ .filter(Boolean)
245
+ .join(' ');
246
+
247
+ const chatShellStyle =
248
+ fillHost && !isMinimized
249
+ ? { width: '100%', height: '100%' }
250
+ : !isMobile && !isMinimized && viewMode === 'immersive'
251
+ ? { width: '100vw', height: '100dvh' }
252
+ : !isMobile && !isMinimized && viewMode === 'fixed'
253
+ ? { width: 'var(--sveda-chat-width)', height: '100dvh' }
254
+ : undefined;
255
+
256
+ const chatCardStyle =
257
+ isMobile || fillHost
258
+ ? { width: '100%', height: '100%' }
259
+ : viewMode === 'fixed'
260
+ ? { width: 'var(--sveda-chat-width)', height: '100%' }
261
+ : viewMode === 'floating'
262
+ ? { width: `${chatWidth}px`, height: `${chatHeight}px` }
263
+ : { width: '100%', height: '100%' };
264
+
265
+ return {
266
+ isMobile,
267
+ viewMode,
268
+ fixedWidth,
269
+ setFixedWidth,
270
+ isImmersiveDesktop,
271
+ chatShellClass,
272
+ chatShellStyle,
273
+ chatCardStyle,
274
+ chatWidth,
275
+ chatHeight,
276
+ startResize,
277
+ toggleViewMode,
278
+ enterImmersiveMode,
279
+ exitImmersiveMode,
280
+ };
281
+ }
@@ -0,0 +1,139 @@
1
+ import {
2
+ createSvedaStreaming,
3
+ type SvedaSendPayload,
4
+ type SvedaStreamingOptions as HeadlessStreamingOptions,
5
+ type SvedaStreamingState,
6
+ type SvedaStreamingStore as HeadlessStreamingStore,
7
+ } from '@sveda-ai/chat';
8
+ import type { SvedaDisplayMessage, SvedaSendOptions } from '@sveda-ai/core';
9
+ import type { SvedaContextUsageEvent, SvedaToolProgressEvent } from '@sveda-ai/protocol';
10
+ import { useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
11
+ import { useSvedaContext, useSvedaT } from '../provider';
12
+
13
+ export interface SvedaStreamingStore {
14
+ currentChat: {
15
+ id: string;
16
+ title?: string;
17
+ messages?: SvedaDisplayMessage[];
18
+ } | null;
19
+ setChatMessages: (chatId: string, messages: SvedaDisplayMessage[]) => void;
20
+ setChatTitle: (chatId: string, title: string) => void;
21
+ refreshChatTitleFromServer: (chatId: string) => Promise<string | null>;
22
+ incrementChatTokens: (chatId: string, tokens: number) => void;
23
+ setChatContextWindowTokens: (chatId: string, tokens: number) => void;
24
+ }
25
+
26
+ export interface SvedaStreamingOptions {
27
+ newChatLabel?: () => string;
28
+ resolveSendOptions?: () => Pick<SvedaSendOptions, 'model' | 'provider' | 'options'>;
29
+ onMaxStepsReached?: (chatId: string, data: { maxSteps: number }) => void;
30
+ onToolProgress?: (chatId: string, event: SvedaToolProgressEvent) => void;
31
+ onContextUsage?: (chatId: string, event: SvedaContextUsageEvent) => void;
32
+ onError?: (chatId: string, error: unknown) => void;
33
+ onDone?: (chatId: string) => void;
34
+ }
35
+
36
+ export type { SvedaSendPayload };
37
+
38
+ export function useSvedaStreaming(
39
+ store: SvedaStreamingStore,
40
+ scrollToBottom: (options?: { behavior?: string; onlyIfNearBottom?: boolean }) => void,
41
+ options: SvedaStreamingOptions = {}
42
+ ) {
43
+ const t = useSvedaT();
44
+ const { client, beforeSend } = useSvedaContext();
45
+ const storeRef = useRef(store);
46
+ storeRef.current = store;
47
+ const scrollRef = useRef(scrollToBottom);
48
+ scrollRef.current = scrollToBottom;
49
+ const optionsRef = useRef(options);
50
+ optionsRef.current = options;
51
+ const translateRef = useRef(t);
52
+ translateRef.current = t;
53
+
54
+ const controller = useMemo(() => {
55
+ const headlessStore: HeadlessStreamingStore = {
56
+ getCurrentChat: () => storeRef.current.currentChat,
57
+ setChatMessages: (chatId, messages) => storeRef.current.setChatMessages(chatId, messages),
58
+ setChatTitle: (chatId, title) => storeRef.current.setChatTitle(chatId, title),
59
+ refreshChatTitleFromServer: (chatId) => storeRef.current.refreshChatTitleFromServer(chatId),
60
+ incrementChatTokens: (chatId, tokens) => storeRef.current.incrementChatTokens(chatId, tokens),
61
+ setChatContextWindowTokens: (chatId, tokens) =>
62
+ storeRef.current.setChatContextWindowTokens(chatId, tokens),
63
+ };
64
+
65
+ const headlessOptions: HeadlessStreamingOptions = {
66
+ get newChatLabel() {
67
+ return optionsRef.current.newChatLabel;
68
+ },
69
+ get resolveSendOptions() {
70
+ return optionsRef.current.resolveSendOptions;
71
+ },
72
+ get onMaxStepsReached() {
73
+ return optionsRef.current.onMaxStepsReached;
74
+ },
75
+ get onToolProgress() {
76
+ return optionsRef.current.onToolProgress;
77
+ },
78
+ get onContextUsage() {
79
+ return optionsRef.current.onContextUsage;
80
+ },
81
+ get onError() {
82
+ return optionsRef.current.onError;
83
+ },
84
+ get onDone() {
85
+ return optionsRef.current.onDone;
86
+ },
87
+ translate: (key: string) => translateRef.current(key),
88
+ beforeSend,
89
+ };
90
+
91
+ return createSvedaStreaming(
92
+ client,
93
+ headlessStore,
94
+ (opts) => scrollRef.current(opts),
95
+ headlessOptions
96
+ );
97
+ }, [client, beforeSend]);
98
+
99
+ const cachedState = useRef<SvedaStreamingState>(controller.getState());
100
+
101
+ const streamingState = useSyncExternalStore(
102
+ (listener) =>
103
+ controller.subscribe(() => {
104
+ cachedState.current = controller.getState();
105
+ listener();
106
+ }),
107
+ () => cachedState.current,
108
+ () => cachedState.current
109
+ );
110
+
111
+ const pollRef = useRef<() => void>(() => {});
112
+
113
+ useEffect(() => {
114
+ cachedState.current = controller.getState();
115
+ pollRef.current = controller.watchCurrentChatId(() => storeRef.current.currentChat?.id);
116
+ return () => {
117
+ controller.dispose();
118
+ };
119
+ }, [controller]);
120
+
121
+ useEffect(() => {
122
+ pollRef.current();
123
+ }, [store.currentChat?.id]);
124
+
125
+ const isStreaming = controller.isStreamingFor(store.currentChat?.id);
126
+
127
+ return {
128
+ sendMessage: controller.sendMessage.bind(controller),
129
+ continueAfterMaxSteps: controller.continueAfterMaxSteps.bind(controller),
130
+ resolveToolConfirmation: controller.resolveToolConfirmation.bind(controller),
131
+ isStreaming,
132
+ isThinking: streamingState.isThinking,
133
+ thinkingMessage: streamingState.thinkingMessage,
134
+ stopStreaming: controller.stopStreaming.bind(controller),
135
+ streamingChatIds: streamingState.streamingChatIds,
136
+ unreadChatIds: streamingState.unreadChatIds,
137
+ clearChatUnread: controller.clearChatUnread.bind(controller),
138
+ };
139
+ }
package/src/i18n.ts ADDED
@@ -0,0 +1,51 @@
1
+ export type SvedaMessages = Record<string, string>;
2
+
3
+ export interface SvedaI18n {
4
+ readonly locale: string;
5
+ t(key: string, params?: Record<string, unknown>): string;
6
+ setLocale(locale: string): void;
7
+ extend(locale: string, messages: SvedaMessages): void;
8
+ }
9
+
10
+ export interface SvedaI18nOptions {
11
+ locale?: string;
12
+ fallbackLocale?: string;
13
+ messages?: Record<string, SvedaMessages>;
14
+ }
15
+
16
+ export function createSvedaI18n(options: SvedaI18nOptions = {}): SvedaI18n {
17
+ let locale = options.locale ?? 'en';
18
+ const fallbackLocale = options.fallbackLocale ?? 'en';
19
+ const messages: Record<string, SvedaMessages> = { ...(options.messages ?? {}) };
20
+
21
+ const interpolate = (template: string, params?: Record<string, unknown>): string => {
22
+ if (!params) {
23
+ return template;
24
+ }
25
+
26
+ return template.replace(/\{(\w+)\}/g, (match, name: string) =>
27
+ params[name] === undefined || params[name] === null ? match : String(params[name])
28
+ );
29
+ };
30
+
31
+ return {
32
+ get locale() {
33
+ return locale;
34
+ },
35
+ t(key: string, params?: Record<string, unknown>): string {
36
+ const message = messages[locale]?.[key] ?? messages[fallbackLocale]?.[key];
37
+
38
+ if (message === undefined) {
39
+ return key;
40
+ }
41
+
42
+ return interpolate(message, params);
43
+ },
44
+ setLocale(next: string): void {
45
+ locale = next;
46
+ },
47
+ extend(nextLocale: string, localeMessages: SvedaMessages): void {
48
+ messages[nextLocale] = { ...(messages[nextLocale] ?? {}), ...localeMessages };
49
+ },
50
+ };
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1,116 @@
1
+ export * from '@sveda-ai/core';
2
+
3
+ export {
4
+ SvedaProvider,
5
+ useSvedaClient,
6
+ useSvedaConfig,
7
+ useSvedaContext,
8
+ useSvedaT,
9
+ createSvedaContextValue,
10
+ SvedaContext,
11
+ } from './provider';
12
+ export type { SvedaProviderProps } from './provider';
13
+
14
+ export type {
15
+ SvedaProviderOptions,
16
+ SvedaConfig,
17
+ SvedaBrand,
18
+ SvedaModelOption,
19
+ SvedaQuickPrompt,
20
+ SvedaBeforeSend,
21
+ SvedaContextValue,
22
+ } from './types';
23
+ export { SVEDA_EMBED_AUTH_EVENT, DEFAULT_CONFIG } from './types';
24
+
25
+ export {
26
+ applySvedaAppearance,
27
+ buildAppearanceCss,
28
+ formatRadiusPx,
29
+ hexToHsl,
30
+ hslToHex,
31
+ isSvedaAppearanceProvided,
32
+ isSvedaHsl,
33
+ mergeSvedaAppearance,
34
+ parseRadiusPx,
35
+ resolveSvedaAppearance,
36
+ sanitizeSvedaChrome,
37
+ sanitizeSvedaLauncher,
38
+ sanitizeSvedaLauncherImage,
39
+ sanitizeSvedaRadius,
40
+ sanitizeSvedaTheme,
41
+ useSvedaChrome,
42
+ useSvedaLauncher,
43
+ getSvedaLauncherSnapshot,
44
+ getSvedaChromeSnapshot,
45
+ subscribeSvedaAppearance,
46
+ svedaChrome,
47
+ svedaLauncher,
48
+ SVEDA_APPEARANCE_PRESET_IDS,
49
+ SVEDA_APPEARANCE_PRESETS,
50
+ SVEDA_APPEARANCE_STYLE_ID,
51
+ SVEDA_DEFAULT_LAUNCHER_ICON,
52
+ SVEDA_LAUNCHER_ICON_IDS,
53
+ SVEDA_LAUNCHER_IMAGE_MAX_BYTES,
54
+ SVEDA_LAUNCHER_IMAGE_MAX_CHARS,
55
+ SVEDA_TOKEN_KEYS,
56
+ } from './appearance';
57
+ export type {
58
+ SvedaAppearance,
59
+ SvedaAppearanceChrome,
60
+ SvedaAppearanceLauncher,
61
+ SvedaAppearancePresetId,
62
+ SvedaAppearanceTheme,
63
+ SvedaAppearanceTokens,
64
+ SvedaLauncherIconId,
65
+ SvedaTokenKey,
66
+ } from './appearance';
67
+
68
+ export { svedaLauncherIconComponent, svedaLauncherIconMap } from './launcherIcons';
69
+
70
+ export { createSvedaI18n } from './i18n';
71
+ export type { SvedaI18n, SvedaI18nOptions, SvedaMessages } from './i18n';
72
+
73
+ export { SvedaChat } from './components/SvedaChat';
74
+ export type { SvedaChatProps } from './components/SvedaChat';
75
+ export { SvedaToolbar } from './components/shell/SvedaToolbar';
76
+ export { SvedaTabs } from './components/shell/SvedaTabs';
77
+ export { SvedaComposer } from './components/shell/SvedaComposer';
78
+ export { SvedaMinimizedTrigger } from './components/shell/SvedaMinimizedTrigger';
79
+ export { ChatMessageList } from './components/shell/ChatMessageList';
80
+
81
+ export { useSvedaChat } from './hooks/useSvedaChat';
82
+ export type { SvedaChatHistory, SvedaChatState } from './hooks/useSvedaChat';
83
+ export { useSvedaStreaming } from './hooks/useSvedaStreaming';
84
+ export type {
85
+ SvedaStreamingStore,
86
+ SvedaStreamingOptions,
87
+ SvedaSendPayload,
88
+ } from './hooks/useSvedaStreaming';
89
+
90
+ export {
91
+ enMessages,
92
+ ruMessages,
93
+ chatComponentI18nKeys,
94
+ isNewChatPlaceholder,
95
+ buildChatTabTitleMap,
96
+ isChatTitlePlaceholder,
97
+ deriveProvisionalChatTitle,
98
+ SVEDA_CHAT_MINIMIZED_STORAGE_KEY,
99
+ readPersistedMinimized,
100
+ readPersistedMinimizedPreference,
101
+ isPersistedSessionChatOpen,
102
+ writePersistedMinimized,
103
+ humanizeModelId,
104
+ getSvedaModelDisplayName,
105
+ finalizeMessagesForDisplay,
106
+ getUserMessageText,
107
+ getUserMessageAttachmentNames,
108
+ userMessageHasVisibleContent,
109
+ } from '@sveda-ai/chat';
110
+ export type {
111
+ SvedaTabChat,
112
+ SvedaFinalizableMessage,
113
+ SvedaUserMessageLike,
114
+ } from '@sveda-ai/chat';
115
+
116
+ export const SVEDA_REACT_VERSION = '0.3.1';
@@ -0,0 +1,43 @@
1
+ import {
2
+ Bot,
3
+ BotMessageSquare,
4
+ Brain,
5
+ CircleHelp,
6
+ Gem,
7
+ Heart,
8
+ MessageCircle,
9
+ MessageSquare,
10
+ Rocket,
11
+ Sparkles,
12
+ Star,
13
+ Zap,
14
+ type LucideIcon,
15
+ } from 'lucide-react';
16
+ import {
17
+ SVEDA_DEFAULT_LAUNCHER_ICON,
18
+ SVEDA_LAUNCHER_ICON_IDS,
19
+ type SvedaLauncherIconId,
20
+ } from './appearance';
21
+
22
+ export const svedaLauncherIconMap: Record<SvedaLauncherIconId, LucideIcon> = {
23
+ sparkles: Sparkles,
24
+ 'message-circle': MessageCircle,
25
+ 'message-square': MessageSquare,
26
+ bot: Bot,
27
+ 'bot-message-square': BotMessageSquare,
28
+ brain: Brain,
29
+ zap: Zap,
30
+ star: Star,
31
+ heart: Heart,
32
+ 'circle-help': CircleHelp,
33
+ rocket: Rocket,
34
+ gem: Gem,
35
+ };
36
+
37
+ export const svedaLauncherIconComponent = (icon: string): LucideIcon => {
38
+ if ((SVEDA_LAUNCHER_ICON_IDS as readonly string[]).includes(icon)) {
39
+ return svedaLauncherIconMap[icon as SvedaLauncherIconId];
40
+ }
41
+
42
+ return svedaLauncherIconMap[SVEDA_DEFAULT_LAUNCHER_ICON];
43
+ };
@@ -0,0 +1,6 @@
1
+ import { type ClassValue, clsx } from 'clsx';
2
+ import { twMerge } from 'tailwind-merge';
3
+
4
+ export function cn(...inputs: ClassValue[]) {
5
+ return twMerge(clsx(inputs));
6
+ }