@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,102 @@
1
+ import {
2
+ createSvedaStreaming,
3
+ type SvedaSendPayload,
4
+ type SvedaStreamingOptions as HeadlessStreamingOptions,
5
+ type SvedaStreamingStore as HeadlessStreamingStore,
6
+ } from '@sveda-ai/chat';
7
+ import type { SvedaDisplayMessage, SvedaSendOptions } from '@sveda-ai/core';
8
+ import type { SvedaContextUsageEvent, SvedaToolProgressEvent } from '@sveda-ai/protocol';
9
+ import { createMemo, createSignal, onCleanup, type Accessor } from 'solid-js';
10
+ import { useSvedaT } from '../i18n/index';
11
+ import { useSvedaContext } from '../provider';
12
+
13
+ export interface SvedaStreamingStore {
14
+ currentChat: Accessor<{
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 ctx = useSvedaContext();
45
+ const beforeSend = ctx.beforeSend;
46
+
47
+ const [isThinking, setIsThinking] = createSignal(false);
48
+ const [thinkingMessage, setThinkingMessage] = createSignal('');
49
+ const [streamingChatIds, setStreamingChatIds] = createSignal(new Set<string>());
50
+ const [unreadChatIds, setUnreadChatIds] = createSignal(new Set<string>());
51
+
52
+ const headlessStore: HeadlessStreamingStore = {
53
+ getCurrentChat: () => store.currentChat(),
54
+ setChatMessages: store.setChatMessages,
55
+ setChatTitle: store.setChatTitle,
56
+ refreshChatTitleFromServer: store.refreshChatTitleFromServer,
57
+ incrementChatTokens: store.incrementChatTokens,
58
+ setChatContextWindowTokens: store.setChatContextWindowTokens,
59
+ };
60
+
61
+ const headlessOptions: HeadlessStreamingOptions = {
62
+ ...options,
63
+ translate: t,
64
+ beforeSend,
65
+ };
66
+
67
+ const controller = createSvedaStreaming(ctx.client, headlessStore, scrollToBottom, headlessOptions);
68
+
69
+ const sync = () => {
70
+ const state = controller.getState();
71
+ setIsThinking(state.isThinking);
72
+ setThinkingMessage(state.thinkingMessage);
73
+ setStreamingChatIds(state.streamingChatIds);
74
+ setUnreadChatIds(state.unreadChatIds);
75
+ };
76
+
77
+ const unsubscribe = controller.subscribe(sync);
78
+ sync();
79
+
80
+ const stopWatch = controller.watchCurrentChatId(() => store.currentChat()?.id);
81
+
82
+ const isStreaming = createMemo(() => controller.isStreamingFor(store.currentChat()?.id));
83
+
84
+ onCleanup(() => {
85
+ stopWatch();
86
+ unsubscribe();
87
+ controller.dispose();
88
+ });
89
+
90
+ return {
91
+ sendMessage: controller.sendMessage,
92
+ continueAfterMaxSteps: controller.continueAfterMaxSteps,
93
+ resolveToolConfirmation: controller.resolveToolConfirmation,
94
+ isStreaming,
95
+ isThinking,
96
+ thinkingMessage,
97
+ stopStreaming: controller.stopStreaming,
98
+ streamingChatIds,
99
+ unreadChatIds,
100
+ clearChatUnread: controller.clearChatUnread,
101
+ };
102
+ }
@@ -0,0 +1,61 @@
1
+ import { createContext, useContext } from 'solid-js';
2
+
3
+ export type SvedaMessages = Record<string, string>;
4
+
5
+ export interface SvedaI18n {
6
+ readonly locale: string;
7
+ t(key: string, params?: Record<string, unknown>): string;
8
+ setLocale(locale: string): void;
9
+ extend(locale: string, messages: SvedaMessages): void;
10
+ }
11
+
12
+ export interface SvedaI18nOptions {
13
+ locale?: string;
14
+ fallbackLocale?: string;
15
+ messages?: Record<string, SvedaMessages>;
16
+ }
17
+
18
+ export const SvedaI18nContext = createContext<SvedaI18n>();
19
+
20
+ export function createSvedaI18n(options: SvedaI18nOptions = {}): SvedaI18n {
21
+ let locale = options.locale ?? 'en';
22
+ const fallbackLocale = options.fallbackLocale ?? 'en';
23
+ const messages: Record<string, SvedaMessages> = { ...(options.messages ?? {}) };
24
+
25
+ const interpolate = (template: string, params?: Record<string, unknown>): string => {
26
+ if (!params) {
27
+ return template;
28
+ }
29
+
30
+ return template.replace(/\{(\w+)\}/g, (match, name: string) =>
31
+ params[name] === undefined || params[name] === null ? match : String(params[name]),
32
+ );
33
+ };
34
+
35
+ return {
36
+ get locale() {
37
+ return locale;
38
+ },
39
+ t(key: string, params?: Record<string, unknown>): string {
40
+ const message = messages[locale]?.[key] ?? messages[fallbackLocale]?.[key];
41
+ if (message === undefined) {
42
+ return key;
43
+ }
44
+ return interpolate(message, params);
45
+ },
46
+ setLocale(next: string): void {
47
+ locale = next;
48
+ },
49
+ extend(loc: string, localeMessages: SvedaMessages): void {
50
+ messages[loc] = { ...(messages[loc] ?? {}), ...localeMessages };
51
+ },
52
+ };
53
+ }
54
+
55
+ export function useSvedaT(): (key: string, params?: Record<string, unknown>) => string {
56
+ const i18n = useContext(SvedaI18nContext);
57
+ if (!i18n) {
58
+ return (key: string) => key;
59
+ }
60
+ return (key, params) => i18n.t(key, params);
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,113 @@
1
+ export * from '@sveda-ai/core';
2
+
3
+ export {
4
+ SvedaProvider,
5
+ createSveda,
6
+ useSvedaClient,
7
+ useSvedaConfig,
8
+ useSvedaContext,
9
+ SvedaContext,
10
+ SVEDA_EMBED_AUTH_EVENT,
11
+ } from './provider';
12
+ export type {
13
+ SvedaProviderOptions,
14
+ SvedaConfig,
15
+ SvedaBrand,
16
+ SvedaModelOption,
17
+ SvedaQuickPrompt,
18
+ SvedaBeforeSend,
19
+ SvedaContextValue,
20
+ } from './provider';
21
+
22
+ export {
23
+ applySvedaAppearance,
24
+ buildAppearanceCss,
25
+ formatRadiusPx,
26
+ hexToHsl,
27
+ hslToHex,
28
+ isSvedaAppearanceProvided,
29
+ isSvedaHsl,
30
+ mergeSvedaAppearance,
31
+ parseRadiusPx,
32
+ resolveSvedaAppearance,
33
+ sanitizeSvedaChrome,
34
+ sanitizeSvedaLauncher,
35
+ sanitizeSvedaLauncherImage,
36
+ sanitizeSvedaRadius,
37
+ sanitizeSvedaTheme,
38
+ useSvedaChrome,
39
+ useSvedaLauncher,
40
+ svedaChrome,
41
+ svedaLauncher,
42
+ SVEDA_APPEARANCE_PRESET_IDS,
43
+ SVEDA_APPEARANCE_PRESETS,
44
+ SVEDA_APPEARANCE_STYLE_ID,
45
+ SVEDA_DEFAULT_LAUNCHER_ICON,
46
+ SVEDA_LAUNCHER_ICON_IDS,
47
+ SVEDA_LAUNCHER_IMAGE_MAX_BYTES,
48
+ SVEDA_LAUNCHER_IMAGE_MAX_CHARS,
49
+ SVEDA_TOKEN_KEYS,
50
+ } from './appearance';
51
+ export type {
52
+ SvedaAppearance,
53
+ SvedaAppearanceChrome,
54
+ SvedaAppearanceLauncher,
55
+ SvedaAppearancePresetId,
56
+ SvedaAppearanceTheme,
57
+ SvedaAppearanceTokens,
58
+ SvedaLauncherIconId,
59
+ SvedaTokenKey,
60
+ } from './appearance';
61
+
62
+ export { svedaLauncherIconComponent, svedaLauncherIconMap } from './launcherIcons';
63
+
64
+ export { createSvedaI18n, useSvedaT, SvedaI18nContext } from './i18n/index';
65
+ export type { SvedaI18n, SvedaI18nOptions, SvedaMessages } from './i18n/index';
66
+
67
+ export { SvedaChat } from './components/SvedaChat';
68
+ export { SvedaMinimizedTrigger } from './components/SvedaMinimizedTrigger';
69
+ export { SvedaTabs } from './components/SvedaTabs';
70
+ export { SvedaToolbar } from './components/SvedaToolbar';
71
+ export { SvedaComposer } from './components/SvedaComposer';
72
+ export { SvedaWelcome } from './components/SvedaWelcome';
73
+ export { SvedaMessageList } from './components/SvedaMessageList';
74
+ export { SvedaQuickPrompts } from './components/SvedaQuickPrompts';
75
+
76
+ export { useSvedaChat } from './hooks/useSvedaChat';
77
+ export type { SvedaChatHistory, SvedaChatState } from './hooks/useSvedaChat';
78
+ export { useSvedaStreaming } from './hooks/useSvedaStreaming';
79
+ export type {
80
+ SvedaStreamingStore,
81
+ SvedaStreamingOptions,
82
+ SvedaSendPayload,
83
+ } from './hooks/useSvedaStreaming';
84
+ export { useSvedaChatPage } from './hooks/useSvedaChatPage';
85
+ export type {
86
+ SvedaChatPageOptions,
87
+ SvedaAgentTasksPayload,
88
+ } from './hooks/useSvedaChatPage';
89
+
90
+ export {
91
+ isNewChatPlaceholder,
92
+ buildChatTabTitleMap,
93
+ isChatTitlePlaceholder,
94
+ deriveProvisionalChatTitle,
95
+ SVEDA_CHAT_MINIMIZED_STORAGE_KEY,
96
+ readPersistedMinimized,
97
+ readPersistedMinimizedPreference,
98
+ isPersistedSessionChatOpen,
99
+ writePersistedMinimized,
100
+ humanizeModelId,
101
+ getSvedaModelDisplayName,
102
+ finalizeMessagesForDisplay,
103
+ getUserMessageText,
104
+ getUserMessageAttachmentNames,
105
+ userMessageHasVisibleContent,
106
+ } from '@sveda-ai/chat';
107
+ export type {
108
+ SvedaTabChat,
109
+ SvedaFinalizableMessage,
110
+ SvedaUserMessageLike,
111
+ } from '@sveda-ai/chat';
112
+
113
+ export const SVEDA_SOLID_VERSION = '0.3.1';
@@ -0,0 +1,41 @@
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
+ } from 'lucide-solid';
15
+ import type { Component, JSX } from 'solid-js';
16
+ import { SVEDA_DEFAULT_LAUNCHER_ICON, SVEDA_LAUNCHER_ICON_IDS, type SvedaLauncherIconId } from './appearance';
17
+
18
+ type LucideIcon = Component<JSX.SvgSVGAttributes<SVGSVGElement>>;
19
+
20
+ export const svedaLauncherIconMap: Record<SvedaLauncherIconId, LucideIcon> = {
21
+ sparkles: Sparkles,
22
+ 'message-circle': MessageCircle,
23
+ 'message-square': MessageSquare,
24
+ bot: Bot,
25
+ 'bot-message-square': BotMessageSquare,
26
+ brain: Brain,
27
+ zap: Zap,
28
+ star: Star,
29
+ heart: Heart,
30
+ 'circle-help': CircleHelp,
31
+ rocket: Rocket,
32
+ gem: Gem,
33
+ };
34
+
35
+ export const svedaLauncherIconComponent = (icon: string): LucideIcon => {
36
+ if ((SVEDA_LAUNCHER_ICON_IDS as readonly string[]).includes(icon)) {
37
+ return svedaLauncherIconMap[icon as SvedaLauncherIconId];
38
+ }
39
+
40
+ return svedaLauncherIconMap[SVEDA_DEFAULT_LAUNCHER_ICON];
41
+ };
@@ -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
+ }
@@ -0,0 +1,96 @@
1
+ .sveda-chat .prose {
2
+ line-height: 1.55;
3
+ }
4
+
5
+ .sveda-chat .prose h1,
6
+ .sveda-chat .prose h2,
7
+ .sveda-chat .prose h3,
8
+ .sveda-chat .prose h4 {
9
+ color: inherit;
10
+ line-height: 1.3;
11
+ letter-spacing: -0.01em;
12
+ }
13
+
14
+ .sveda-chat .prose h1 {
15
+ font-size: 1.2rem;
16
+ font-weight: 700;
17
+ margin: 0.9em 0 0.35em;
18
+ }
19
+
20
+ .sveda-chat .prose h2 {
21
+ font-size: 1.08rem;
22
+ font-weight: 650;
23
+ margin: 0.85em 0 0.3em;
24
+ }
25
+
26
+ .sveda-chat .prose h3,
27
+ .sveda-chat .prose h4 {
28
+ font-size: 1rem;
29
+ font-weight: 600;
30
+ margin: 0.75em 0 0.25em;
31
+ }
32
+
33
+ .sveda-chat .prose h1:first-child,
34
+ .sveda-chat .prose h2:first-child,
35
+ .sveda-chat .prose h3:first-child,
36
+ .sveda-chat .prose h4:first-child {
37
+ margin-top: 0;
38
+ }
39
+
40
+ .sveda-chat .prose p {
41
+ margin: 0.45em 0;
42
+ }
43
+
44
+ .sveda-chat .prose p:first-child {
45
+ margin-top: 0;
46
+ }
47
+
48
+ .sveda-chat .prose p:last-child {
49
+ margin-bottom: 0;
50
+ }
51
+
52
+ .sveda-chat .prose ul,
53
+ .sveda-chat .prose ol {
54
+ margin: 0.4em 0;
55
+ padding-left: 1.25rem;
56
+ }
57
+
58
+ .sveda-chat .prose ul {
59
+ list-style-type: disc;
60
+ }
61
+
62
+ .sveda-chat .prose ol {
63
+ list-style-type: decimal;
64
+ }
65
+
66
+ .sveda-chat .prose li {
67
+ display: list-item;
68
+ margin: 0.2em 0;
69
+ }
70
+
71
+ .sveda-chat .prose li > p {
72
+ margin: 0.15em 0;
73
+ }
74
+
75
+ .sveda-chat .prose hr {
76
+ border: 0;
77
+ border-top: 1px solid hsl(var(--sveda-border));
78
+ margin: 0.9em 0;
79
+ }
80
+
81
+ .sveda-chat .prose a {
82
+ color: hsl(var(--sveda-brand));
83
+ text-decoration: underline;
84
+ text-underline-offset: 2px;
85
+ }
86
+
87
+ .sveda-chat .prose blockquote {
88
+ margin: 0.5em 0;
89
+ padding-left: 0.75rem;
90
+ border-left: 2px solid hsl(var(--sveda-border));
91
+ color: hsl(var(--sveda-muted-foreground));
92
+ }
93
+
94
+ .sveda-chat .prose strong {
95
+ font-weight: 650;
96
+ }
@@ -0,0 +1,196 @@
1
+ import { SvedaClient } from '@sveda-ai/core';
2
+ import { chatComponentI18nKeys, enMessages as en, ruMessages as ru } from '@sveda-ai/chat';
3
+ import { type JSX, createContext, useContext, type ParentProps } from 'solid-js';
4
+ import {
5
+ applySvedaAppearance,
6
+ mergeSvedaAppearance,
7
+ type SvedaAppearance,
8
+ } from './appearance';
9
+ import { createSvedaI18n, SvedaI18nContext, type SvedaI18n, type SvedaMessages } from './i18n/index';
10
+
11
+ export interface SvedaModelOption {
12
+ id: string;
13
+ label: string;
14
+ supportsThinking?: boolean;
15
+ }
16
+
17
+ export interface SvedaQuickPrompt {
18
+ label: string;
19
+ prompt: string;
20
+ }
21
+
22
+ export interface SvedaBrand {
23
+ name?: string;
24
+ logoUrl?: string;
25
+ }
26
+
27
+ export type SvedaBeforeSend = () => void | Promise<void>;
28
+
29
+ export interface SvedaProviderOptions {
30
+ endpoints: {
31
+ stream: string;
32
+ message?: string;
33
+ histories?: string;
34
+ documentsExtract?: string;
35
+ };
36
+ protocolMode?: 'sveda' | 'vercel';
37
+ headers?: Record<string, string> | (() => Record<string, string>);
38
+ credentials?: RequestCredentials;
39
+ locale?: string;
40
+ messages?: Record<string, Record<string, string>>;
41
+ brand?: SvedaBrand;
42
+ models?: SvedaModelOption[];
43
+ quickPrompts?: SvedaQuickPrompt[];
44
+ appearance?: SvedaAppearance | null;
45
+ theme?: 'light' | 'dark';
46
+ hostEmbed?: boolean;
47
+ fillHost?: boolean;
48
+ hideLauncher?: boolean;
49
+ beforeSend?: SvedaBeforeSend;
50
+ }
51
+
52
+ export interface SvedaConfig {
53
+ brand: {
54
+ name: string;
55
+ logoUrl: string | null;
56
+ };
57
+ models: SvedaModelOption[];
58
+ quickPrompts: SvedaQuickPrompt[];
59
+ }
60
+
61
+ export interface SvedaContextValue {
62
+ client: SvedaClient;
63
+ i18n: SvedaI18n;
64
+ config: SvedaConfig;
65
+ hostEmbed: boolean;
66
+ fillHost: boolean;
67
+ hideLauncher: boolean;
68
+ beforeSend: SvedaBeforeSend | null;
69
+ }
70
+
71
+ export const SVEDA_EMBED_AUTH_EVENT = 'sveda-embed-auth';
72
+
73
+ const DEFAULT_CONFIG: SvedaConfig = {
74
+ brand: { name: 'Sveda', logoUrl: null },
75
+ models: [],
76
+ quickPrompts: [],
77
+ };
78
+
79
+ export const SvedaContext = createContext<SvedaContextValue>();
80
+
81
+ const scheduleEmbedAppearance = (stream: string): void => {
82
+ if (typeof fetch === 'undefined') {
83
+ return;
84
+ }
85
+
86
+ let origin = '';
87
+ try {
88
+ const url = new URL(stream);
89
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
90
+ return;
91
+ }
92
+ origin = url.origin;
93
+ } catch {
94
+ return;
95
+ }
96
+
97
+ void fetch(`${origin}/sveda/embed/config`)
98
+ .then(response => (response.ok ? response.json() : null))
99
+ .then(config => {
100
+ if (!config || typeof config !== 'object') {
101
+ return;
102
+ }
103
+
104
+ applySvedaAppearance(
105
+ mergeSvedaAppearance((config as { appearance?: SvedaAppearance | null }).appearance, undefined),
106
+ );
107
+ })
108
+ .catch(() => {});
109
+ };
110
+
111
+ export function createSveda(options: SvedaProviderOptions): SvedaContextValue {
112
+ const client = new SvedaClient({
113
+ endpoints: options.endpoints,
114
+ protocolMode: options.protocolMode,
115
+ headers: options.headers,
116
+ credentials: options.credentials,
117
+ });
118
+
119
+ const messages: Record<string, SvedaMessages> = {};
120
+ const mergeMessages = (locale: string, localeMessages: SvedaMessages) => {
121
+ messages[locale] = { ...(messages[locale] ?? {}), ...localeMessages };
122
+ };
123
+
124
+ for (const [locale, localeMessages] of Object.entries({ en, ru })) {
125
+ mergeMessages(locale, localeMessages);
126
+ }
127
+ for (const [locale, localeMessages] of Object.entries(chatComponentI18nKeys)) {
128
+ mergeMessages(locale, localeMessages);
129
+ }
130
+ for (const [locale, localeMessages] of Object.entries(options.messages ?? {})) {
131
+ mergeMessages(locale, localeMessages);
132
+ }
133
+
134
+ const i18n = createSvedaI18n({
135
+ locale: options.locale ?? 'en',
136
+ messages,
137
+ });
138
+
139
+ const config: SvedaConfig = {
140
+ brand: {
141
+ name: options.brand?.name ?? 'Sveda',
142
+ logoUrl: options.brand?.logoUrl ?? null,
143
+ },
144
+ models: options.models ?? [],
145
+ quickPrompts: options.quickPrompts ?? [],
146
+ };
147
+
148
+ if (options.appearance !== undefined || options.theme !== undefined) {
149
+ if (options.appearance === null && options.theme === undefined) {
150
+ applySvedaAppearance(null);
151
+ } else {
152
+ applySvedaAppearance({
153
+ ...(options.appearance && typeof options.appearance === 'object' ? options.appearance : {}),
154
+ ...(options.theme ? { theme: options.theme } : {}),
155
+ });
156
+ }
157
+ } else {
158
+ scheduleEmbedAppearance(options.endpoints.stream);
159
+ }
160
+
161
+ return {
162
+ client,
163
+ i18n,
164
+ config,
165
+ hostEmbed: Boolean(options.hostEmbed),
166
+ fillHost: Boolean(options.fillHost ?? options.hostEmbed),
167
+ hideLauncher: Boolean(options.hideLauncher),
168
+ beforeSend: options.beforeSend ?? null,
169
+ };
170
+ }
171
+
172
+ export function SvedaProvider(props: ParentProps<SvedaProviderOptions>): JSX.Element {
173
+ const value = createSveda(props);
174
+
175
+ return (
176
+ <SvedaContext.Provider value={value}>
177
+ <SvedaI18nContext.Provider value={value.i18n}>{props.children}</SvedaI18nContext.Provider>
178
+ </SvedaContext.Provider>
179
+ );
180
+ }
181
+
182
+ export function useSvedaContext(): SvedaContextValue {
183
+ const ctx = useContext(SvedaContext);
184
+ if (!ctx) {
185
+ throw new Error('[sveda] SvedaProvider is missing. Wrap your tree in <SvedaProvider>.');
186
+ }
187
+ return ctx;
188
+ }
189
+
190
+ export function useSvedaClient(): SvedaClient {
191
+ return useSvedaContext().client;
192
+ }
193
+
194
+ export function useSvedaConfig(): SvedaConfig {
195
+ return useContext(SvedaContext)?.config ?? DEFAULT_CONFIG;
196
+ }
package/src/styles.css ADDED
@@ -0,0 +1,6 @@
1
+ @import './theme.css';
2
+ @import './markdown.css';
3
+
4
+ @tailwind base;
5
+ @tailwind components;
6
+ @tailwind utilities;