@sveda-ai/svelte 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.
package/src/index.ts ADDED
@@ -0,0 +1,106 @@
1
+ export * from '@sveda-ai/core';
2
+
3
+ export {
4
+ createSveda,
5
+ SVEDA_CONTEXT_KEY,
6
+ SVEDA_EMBED_AUTH_EVENT,
7
+ getDefaultSvedaConfig,
8
+ } from './provider.js';
9
+ export type {
10
+ SvedaPluginOptions,
11
+ SvedaContext,
12
+ SvedaConfig,
13
+ SvedaBrand,
14
+ SvedaModelOption,
15
+ SvedaQuickPrompt,
16
+ SvedaBeforeSend,
17
+ } from './provider.js';
18
+
19
+ export {
20
+ setSvedaContext,
21
+ createSvedaContext,
22
+ useSvedaContext,
23
+ useSvedaClient,
24
+ useSvedaConfig,
25
+ useSvedaT,
26
+ } from './context.js';
27
+
28
+ export {
29
+ applySvedaAppearance,
30
+ buildAppearanceCss,
31
+ formatRadiusPx,
32
+ hexToHsl,
33
+ hslToHex,
34
+ isSvedaAppearanceProvided,
35
+ isSvedaHsl,
36
+ mergeSvedaAppearance,
37
+ parseRadiusPx,
38
+ resolveSvedaAppearance,
39
+ sanitizeSvedaChrome,
40
+ sanitizeSvedaLauncher,
41
+ sanitizeSvedaLauncherImage,
42
+ sanitizeSvedaRadius,
43
+ sanitizeSvedaTheme,
44
+ useSvedaChrome,
45
+ useSvedaLauncher,
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.js';
57
+ export type {
58
+ SvedaAppearance,
59
+ SvedaAppearanceChrome,
60
+ SvedaAppearanceLauncher,
61
+ SvedaAppearancePresetId,
62
+ SvedaAppearanceTheme,
63
+ SvedaAppearanceTokens,
64
+ SvedaLauncherIconId,
65
+ SvedaTokenKey,
66
+ } from './appearance.js';
67
+
68
+ export { createSvedaI18n } from './i18n.js';
69
+ export type { SvedaI18n, SvedaI18nOptions, SvedaMessages } from './i18n.js';
70
+
71
+ export { default as SvedaProvider } from './components/SvedaProvider.svelte';
72
+ export { default as SvedaChat } from './components/SvedaChat.svelte';
73
+
74
+ export { useSvedaChat } from './hooks/useSvedaChat.svelte.js';
75
+ export type { SvedaChatHistory, SvedaChatState } from './hooks/useSvedaChat.svelte.js';
76
+ export { useSvedaStreaming } from './hooks/useSvedaStreaming.svelte.js';
77
+ export type {
78
+ SvedaStreamingStore,
79
+ SvedaStreamingOptions,
80
+ SvedaSendPayload,
81
+ } from './hooks/useSvedaStreaming.svelte.js';
82
+
83
+ export {
84
+ isNewChatPlaceholder,
85
+ buildChatTabTitleMap,
86
+ isChatTitlePlaceholder,
87
+ deriveProvisionalChatTitle,
88
+ SVEDA_CHAT_MINIMIZED_STORAGE_KEY,
89
+ readPersistedMinimized,
90
+ readPersistedMinimizedPreference,
91
+ isPersistedSessionChatOpen,
92
+ writePersistedMinimized,
93
+ humanizeModelId,
94
+ getSvedaModelDisplayName,
95
+ finalizeMessagesForDisplay,
96
+ getUserMessageText,
97
+ getUserMessageAttachmentNames,
98
+ userMessageHasVisibleContent,
99
+ } from '@sveda-ai/chat';
100
+ export type {
101
+ SvedaTabChat,
102
+ SvedaFinalizableMessage,
103
+ SvedaUserMessageLike,
104
+ } from '@sveda-ai/chat';
105
+
106
+ export const SVEDA_SVELTE_VERSION = '0.3.1';
@@ -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,173 @@
1
+ import { SvedaClient } from '@sveda-ai/core';
2
+ import {
3
+ applySvedaAppearance,
4
+ mergeSvedaAppearance,
5
+ type SvedaAppearance,
6
+ } from './appearance.js';
7
+ import { createSvedaI18n, type SvedaI18n, type SvedaMessages } from './i18n.js';
8
+ import { chatComponentI18nKeys, enMessages as en, ruMessages as ru } from '@sveda-ai/chat';
9
+
10
+ export interface SvedaModelOption {
11
+ id: string;
12
+ label: string;
13
+ supportsThinking?: boolean;
14
+ }
15
+
16
+ export interface SvedaQuickPrompt {
17
+ label: string;
18
+ prompt: string;
19
+ }
20
+
21
+ export interface SvedaBrand {
22
+ name?: string;
23
+ logoUrl?: string;
24
+ }
25
+
26
+ export type SvedaBeforeSend = () => void | Promise<void>;
27
+
28
+ export interface SvedaPluginOptions {
29
+ endpoints: {
30
+ stream: string;
31
+ message?: string;
32
+ histories?: string;
33
+ documentsExtract?: string;
34
+ };
35
+ protocolMode?: 'sveda' | 'vercel';
36
+ headers?: Record<string, string> | (() => Record<string, string>);
37
+ credentials?: RequestCredentials;
38
+ locale?: string;
39
+ messages?: Record<string, Record<string, string>>;
40
+ brand?: SvedaBrand;
41
+ models?: SvedaModelOption[];
42
+ quickPrompts?: SvedaQuickPrompt[];
43
+ appearance?: SvedaAppearance | null;
44
+ theme?: 'light' | 'dark';
45
+ hostEmbed?: boolean;
46
+ fillHost?: boolean;
47
+ hideLauncher?: boolean;
48
+ beforeSend?: SvedaBeforeSend;
49
+ }
50
+
51
+ export interface SvedaConfig {
52
+ brand: {
53
+ name: string;
54
+ logoUrl: string | null;
55
+ };
56
+ models: SvedaModelOption[];
57
+ quickPrompts: SvedaQuickPrompt[];
58
+ }
59
+
60
+ export interface SvedaContext {
61
+ client: SvedaClient;
62
+ i18n: SvedaI18n;
63
+ config: SvedaConfig;
64
+ hostEmbed: boolean;
65
+ fillHost: boolean;
66
+ hideLauncher: boolean;
67
+ beforeSend: SvedaBeforeSend | null;
68
+ }
69
+
70
+ export const SVEDA_CONTEXT_KEY = 'sveda';
71
+
72
+ export const SVEDA_EMBED_AUTH_EVENT = 'sveda-embed-auth';
73
+
74
+ const DEFAULT_CONFIG: SvedaConfig = {
75
+ brand: { name: 'Sveda', logoUrl: null },
76
+ models: [],
77
+ quickPrompts: [],
78
+ };
79
+
80
+ const scheduleEmbedAppearance = (stream: string): void => {
81
+ if (typeof fetch === 'undefined') {
82
+ return;
83
+ }
84
+
85
+ let origin = '';
86
+ try {
87
+ const url = new URL(stream);
88
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
89
+ return;
90
+ }
91
+ origin = url.origin;
92
+ } catch {
93
+ return;
94
+ }
95
+
96
+ void fetch(`${origin}/sveda/embed/config`)
97
+ .then((response) => (response.ok ? response.json() : null))
98
+ .then((config) => {
99
+ if (!config || typeof config !== 'object') {
100
+ return;
101
+ }
102
+
103
+ applySvedaAppearance(
104
+ mergeSvedaAppearance((config as { appearance?: SvedaAppearance | null }).appearance, undefined),
105
+ );
106
+ })
107
+ .catch(() => {});
108
+ };
109
+
110
+ export function createSveda(options: SvedaPluginOptions): SvedaContext {
111
+ const client = new SvedaClient({
112
+ endpoints: options.endpoints,
113
+ protocolMode: options.protocolMode,
114
+ headers: options.headers,
115
+ credentials: options.credentials,
116
+ });
117
+
118
+ const messages: Record<string, SvedaMessages> = {};
119
+ const mergeMessages = (locale: string, localeMessages: SvedaMessages) => {
120
+ messages[locale] = { ...(messages[locale] ?? {}), ...localeMessages };
121
+ };
122
+
123
+ for (const [locale, localeMessages] of Object.entries({ en, ru })) {
124
+ mergeMessages(locale, localeMessages);
125
+ }
126
+ for (const [locale, localeMessages] of Object.entries(chatComponentI18nKeys)) {
127
+ mergeMessages(locale, localeMessages);
128
+ }
129
+ for (const [locale, localeMessages] of Object.entries(options.messages ?? {})) {
130
+ mergeMessages(locale, localeMessages);
131
+ }
132
+
133
+ const i18n = createSvedaI18n({
134
+ locale: options.locale ?? 'en',
135
+ messages,
136
+ });
137
+
138
+ const config: SvedaConfig = {
139
+ brand: {
140
+ name: options.brand?.name ?? 'Sveda',
141
+ logoUrl: options.brand?.logoUrl ?? null,
142
+ },
143
+ models: options.models ?? [],
144
+ quickPrompts: options.quickPrompts ?? [],
145
+ };
146
+
147
+ if (options.appearance !== undefined || options.theme !== undefined) {
148
+ if (options.appearance === null && options.theme === undefined) {
149
+ applySvedaAppearance(null);
150
+ } else {
151
+ applySvedaAppearance({
152
+ ...(options.appearance && typeof options.appearance === 'object' ? options.appearance : {}),
153
+ ...(options.theme ? { theme: options.theme } : {}),
154
+ });
155
+ }
156
+ } else {
157
+ scheduleEmbedAppearance(options.endpoints.stream);
158
+ }
159
+
160
+ return {
161
+ client,
162
+ i18n,
163
+ config,
164
+ hostEmbed: Boolean(options.hostEmbed),
165
+ fillHost: Boolean(options.fillHost ?? options.hostEmbed),
166
+ hideLauncher: Boolean(options.hideLauncher),
167
+ beforeSend: options.beforeSend ?? null,
168
+ };
169
+ }
170
+
171
+ export function getDefaultSvedaConfig(): SvedaConfig {
172
+ return DEFAULT_CONFIG;
173
+ }
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;
package/src/theme.css ADDED
@@ -0,0 +1,137 @@
1
+ :where(.sveda-chat, sveda-chat) {
2
+ --sveda-background: 0 0% 100%;
3
+ --sveda-foreground: 0 0% 4%;
4
+ --sveda-card: 0 0% 100%;
5
+ --sveda-card-foreground: 0 0% 4%;
6
+ --sveda-popover: 0 0% 100%;
7
+ --sveda-popover-foreground: 0 0% 4%;
8
+ --sveda-primary: 0 0% 4%;
9
+ --sveda-primary-foreground: 0 0% 100%;
10
+ --sveda-secondary: 40 20% 90%;
11
+ --sveda-secondary-foreground: 0 0% 4%;
12
+ --sveda-muted: 40 22% 90%;
13
+ --sveda-muted-foreground: 40 6% 41%;
14
+ --sveda-accent: 40 22% 90%;
15
+ --sveda-accent-foreground: 0 0% 4%;
16
+ --sveda-destructive: 0 84.2% 60.2%;
17
+ --sveda-destructive-foreground: 0 0% 98%;
18
+ --sveda-border: 42 18% 82%;
19
+ --sveda-input: 40 22% 92%;
20
+ --sveda-ring: 0 0% 4%;
21
+ --sveda-radius: 0;
22
+ --sveda-brand: 0 0% 4%;
23
+ --sveda-brand-foreground: 0 0% 100%;
24
+ --sveda-surface: hsl(var(--sveda-background));
25
+ --font-sans: Geist, ui-sans-serif, system-ui, sans-serif;
26
+ --font-serif: Newsreader, ui-serif, Georgia, serif;
27
+ --font-mono: 'Geist Mono', ui-monospace, monospace;
28
+ }
29
+
30
+ .sveda-chat,
31
+ sveda-chat {
32
+ --color-background: hsl(var(--sveda-background));
33
+ --color-foreground: hsl(var(--sveda-foreground));
34
+ --color-card: hsl(var(--sveda-card));
35
+ --color-card-foreground: hsl(var(--sveda-card-foreground));
36
+ --color-popover: hsl(var(--sveda-popover));
37
+ --color-popover-foreground: hsl(var(--sveda-popover-foreground));
38
+ --color-primary: hsl(var(--sveda-primary));
39
+ --color-primary-foreground: hsl(var(--sveda-primary-foreground));
40
+ --color-secondary: hsl(var(--sveda-secondary));
41
+ --color-secondary-foreground: hsl(var(--sveda-secondary-foreground));
42
+ --color-muted: hsl(var(--sveda-muted));
43
+ --color-muted-foreground: hsl(var(--sveda-muted-foreground));
44
+ --color-accent: hsl(var(--sveda-accent));
45
+ --color-accent-foreground: hsl(var(--sveda-accent-foreground));
46
+ --color-destructive: hsl(var(--sveda-destructive));
47
+ --color-destructive-foreground: hsl(var(--sveda-destructive-foreground));
48
+ --color-border: hsl(var(--sveda-border));
49
+ --color-input: hsl(var(--sveda-input));
50
+ --color-ring: hsl(var(--sveda-ring));
51
+ --color-brand-primary-purple: hsl(var(--sveda-brand));
52
+ --color-brand-primary-purple-foreground: hsl(var(--sveda-brand-foreground));
53
+ --radius: var(--sveda-radius);
54
+ --radius-xs: var(--sveda-radius);
55
+ --radius-sm: var(--sveda-radius);
56
+ --radius-md: var(--sveda-radius);
57
+ --radius-lg: var(--sveda-radius);
58
+ --radius-xl: var(--sveda-radius);
59
+ --radius-2xl: var(--sveda-radius);
60
+ --radius-3xl: var(--sveda-radius);
61
+ --radius-4xl: var(--sveda-radius);
62
+ font-family: var(--font-sans, Geist, ui-sans-serif, system-ui, sans-serif);
63
+ }
64
+
65
+ :where(.sveda-chat.dark, sveda-chat.dark, .dark .sveda-chat, .dark sveda-chat) {
66
+ --sveda-background: 30 12% 9%;
67
+ --sveda-foreground: 40 24% 94%;
68
+ --sveda-card: 30 10% 12%;
69
+ --sveda-card-foreground: 40 24% 94%;
70
+ --sveda-popover: 30 10% 12%;
71
+ --sveda-popover-foreground: 40 24% 94%;
72
+ --sveda-primary: 40 24% 94%;
73
+ --sveda-primary-foreground: 30 12% 9%;
74
+ --sveda-secondary: 30 8% 16%;
75
+ --sveda-secondary-foreground: 40 24% 94%;
76
+ --sveda-muted: 30 8% 16%;
77
+ --sveda-muted-foreground: 36 10% 62%;
78
+ --sveda-accent: 30 8% 16%;
79
+ --sveda-accent-foreground: 40 24% 94%;
80
+ --sveda-destructive: 0 62.8% 30.6%;
81
+ --sveda-destructive-foreground: 40 24% 94%;
82
+ --sveda-border: 30 8% 22%;
83
+ --sveda-input: 30 8% 16%;
84
+ --sveda-ring: 40 24% 94%;
85
+ --sveda-brand: 40 24% 94%;
86
+ --sveda-brand-foreground: 30 12% 9%;
87
+ }
88
+
89
+ .sveda-chat-surface {
90
+ background-color: var(--sveda-surface, hsl(var(--sveda-background)));
91
+ }
92
+
93
+ /* Host pages often style input/textarea unlayered, which beats Tailwind utilities. */
94
+ .sveda-chat input[type='text'],
95
+ .sveda-chat input[type='search'],
96
+ .sveda-chat input:not([type]),
97
+ .sveda-chat textarea {
98
+ box-sizing: border-box;
99
+ width: auto;
100
+ max-width: 100%;
101
+ margin: 0;
102
+ border: 0;
103
+ border-radius: var(--sveda-radius, 0);
104
+ background: transparent;
105
+ padding: 0;
106
+ min-height: 0;
107
+ box-shadow: none;
108
+ font: inherit;
109
+ color: inherit;
110
+ letter-spacing: inherit;
111
+ resize: none;
112
+ appearance: none;
113
+ }
114
+
115
+ .sveda-chat .sveda-chat-input-textarea {
116
+ width: 100%;
117
+ min-height: 60px;
118
+ max-height: 250px;
119
+ padding: 0.75rem;
120
+ }
121
+
122
+ .sveda-chat-frame,
123
+ .sveda-chat .sveda-chat-frame,
124
+ sveda-chat .sveda-chat-frame {
125
+ border: 1px dashed hsl(var(--sveda-foreground));
126
+ }
127
+
128
+ .sveda-chat ::selection,
129
+ sveda-chat ::selection {
130
+ background: hsl(var(--sveda-foreground));
131
+ color: hsl(var(--sveda-background));
132
+ }
133
+
134
+ body.sveda-chat-immersive-mode {
135
+ overflow: hidden;
136
+ overscroll-behavior: none;
137
+ }