@sveda-ai/vue 0.3.1 → 0.3.3
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/README.md +1 -1
- package/dist/appearance.d.ts +4 -53
- package/dist/appearance.d.ts.map +1 -1
- package/dist/composables/useSvedaChat.d.ts +7 -149
- package/dist/composables/useSvedaChat.d.ts.map +1 -1
- package/dist/composables/useSvedaChatLayout.d.ts +1 -1
- package/dist/composables/useSvedaChatLayout.d.ts.map +1 -1
- package/dist/composables/useSvedaChatPage.d.ts +6 -91
- package/dist/composables/useSvedaChatPage.d.ts.map +1 -1
- package/dist/composables/useSvedaPrompts.d.ts +1 -1
- package/dist/composables/useSvedaPrompts.d.ts.map +1 -1
- package/dist/composables/useSvedaStreaming.d.ts +3 -6
- package/dist/composables/useSvedaStreaming.d.ts.map +1 -1
- package/dist/index.d.ts +3 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3035 -4437
- package/dist/plugin.d.ts.map +1 -1
- package/dist/styles.css +2 -1
- package/dist/ui/sheet/index.d.ts +1 -1
- package/dist/ui/toast/Toast.vue.d.ts +2 -2
- package/dist/vue.css +1 -1
- package/package.json +21 -29
- package/src/appearance.ts +44 -652
- package/src/components/chat/ChatMessageActivityBlock.vue +1 -1
- package/src/components/chat/ChatMessageItem.vue +3 -3
- package/src/components/chat/ChatMessageList.vue +1 -1
- package/src/components/shell/SvedaTabs.vue +1 -1
- package/src/composables/useSvedaChat.ts +53 -573
- package/src/composables/useSvedaChatLayout.ts +1 -1
- package/src/composables/useSvedaChatPage.ts +1 -1
- package/src/composables/useSvedaPrompts.ts +1 -1
- package/src/composables/useSvedaStreaming.ts +41 -368
- package/src/index.ts +14 -11
- package/src/plugin.ts +1 -3
- package/src/theme.test.mjs +5 -4
- package/dist/i18n/locales/en.d.ts +0 -4
- package/dist/i18n/locales/en.d.ts.map +0 -1
- package/dist/i18n/locales/ru.d.ts +0 -4
- package/dist/i18n/locales/ru.d.ts.map +0 -1
- package/dist/lib/assistantMessage.d.ts +0 -90
- package/dist/lib/assistantMessage.d.ts.map +0 -1
- package/dist/lib/chatHistoryMerge.d.ts +0 -20
- package/dist/lib/chatHistoryMerge.d.ts.map +0 -1
- package/dist/lib/chatI18nKeys.d.ts +0 -2
- package/dist/lib/chatI18nKeys.d.ts.map +0 -1
- package/dist/lib/chatMessageVisibility.d.ts +0 -6
- package/dist/lib/chatMessageVisibility.d.ts.map +0 -1
- package/dist/lib/chatResize.d.ts +0 -42
- package/dist/lib/chatResize.d.ts.map +0 -1
- package/dist/lib/chatTabs.d.ts +0 -9
- package/dist/lib/chatTabs.d.ts.map +0 -1
- package/dist/lib/chatTitle.d.ts +0 -3
- package/dist/lib/chatTitle.d.ts.map +0 -1
- package/dist/lib/chatUiStorage.d.ts +0 -6
- package/dist/lib/chatUiStorage.d.ts.map +0 -1
- package/dist/lib/finalizeMessages.d.ts +0 -21
- package/dist/lib/finalizeMessages.d.ts.map +0 -1
- package/dist/lib/markdown.d.ts +0 -8
- package/dist/lib/markdown.d.ts.map +0 -1
- package/dist/lib/modelLabels.d.ts +0 -3
- package/dist/lib/modelLabels.d.ts.map +0 -1
- package/dist/lib/toolConfirmation.d.ts +0 -11
- package/dist/lib/toolConfirmation.d.ts.map +0 -1
- package/dist/lib/userMessage.d.ts +0 -17
- package/dist/lib/userMessage.d.ts.map +0 -1
- package/src/i18n/locales/en.ts +0 -64
- package/src/i18n/locales/ru.ts +0 -65
- package/src/lib/assistantMessage.ts +0 -439
- package/src/lib/chatHistoryMerge.ts +0 -93
- package/src/lib/chatI18nKeys.ts +0 -64
- package/src/lib/chatMessageVisibility.ts +0 -92
- package/src/lib/chatResize.ts +0 -123
- package/src/lib/chatTabs.ts +0 -54
- package/src/lib/chatTitle.ts +0 -30
- package/src/lib/chatUiStorage.ts +0 -69
- package/src/lib/finalizeMessages.ts +0 -81
- package/src/lib/markdown.ts +0 -90
- package/src/lib/modelLabels.ts +0 -24
- package/src/lib/toolConfirmation.ts +0 -31
- package/src/lib/userMessage.ts +0 -55
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
getActivityGroupLabel,
|
|
5
5
|
getToolNameFromPart,
|
|
6
6
|
parseToolResultOutput,
|
|
7
|
-
} from '
|
|
7
|
+
} from '@sveda-ai/chat';
|
|
8
8
|
import { useSvedaT } from '../../i18n/index.js';
|
|
9
9
|
import { CheckCircle, ChevronDown, ChevronRight, CircleStop, ExternalLink, Search, Terminal, XCircle } from 'lucide-vue-next';
|
|
10
10
|
import { computed, ref, watch } from 'vue';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<script setup>
|
|
2
|
-
import { buildAssistantMessageSegments, collectResourceLinks } from '
|
|
3
|
-
import { renderMarkdown } from '
|
|
2
|
+
import { buildAssistantMessageSegments, collectResourceLinks } from '@sveda-ai/chat';
|
|
3
|
+
import { renderMarkdown } from '@sveda-ai/chat';
|
|
4
4
|
import { useAnimatedText } from '../../lib/useAnimatedText.js';
|
|
5
|
-
import { getUserMessageAttachmentNames, getUserMessageText } from '
|
|
5
|
+
import { getUserMessageAttachmentNames, getUserMessageText } from '@sveda-ai/chat';
|
|
6
6
|
import { useSvedaT } from '../../i18n/index.js';
|
|
7
7
|
import { Paperclip } from 'lucide-vue-next';
|
|
8
8
|
import { computed, nextTick, onUnmounted, ref, watch } from 'vue';
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import {
|
|
4
4
|
assistantHasActiveTools,
|
|
5
5
|
shouldShowChatPendingIndicator,
|
|
6
|
-
} from '
|
|
6
|
+
} from '@sveda-ai/chat';
|
|
7
7
|
import { useSvedaT } from '../../i18n/index.js';
|
|
8
8
|
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue';
|
|
9
9
|
import ChatMessageItem from './ChatMessageItem.vue';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
import { Button } from '../../ui/button';
|
|
3
3
|
import { ScrollArea, ScrollBar } from '../../ui/scroll-area';
|
|
4
|
-
import { buildChatTabTitleMap } from '
|
|
4
|
+
import { buildChatTabTitleMap } from '@sveda-ai/chat';
|
|
5
5
|
import { Loader2, Plus, X } from 'lucide-vue-next';
|
|
6
6
|
import { computed, unref } from 'vue';
|
|
7
7
|
import { useSvedaT } from '../../i18n/index';
|
|
@@ -1,481 +1,50 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
type
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
} from '
|
|
7
|
-
import { computed, inject, reactive, readonly, ref } from 'vue';
|
|
2
|
+
getSvedaChatStore,
|
|
3
|
+
type SvedaChatHistory,
|
|
4
|
+
type SvedaChatState,
|
|
5
|
+
} from '@sveda-ai/chat';
|
|
6
|
+
import { computed, inject, reactive, readonly, ref, shallowRef } from 'vue';
|
|
8
7
|
import { useSvedaT } from '../i18n/index';
|
|
9
|
-
import {
|
|
10
|
-
mergeServerChatHistories,
|
|
11
|
-
resolveMergedCurrentChat,
|
|
12
|
-
upsertChatHistoryMessages,
|
|
13
|
-
} from '../lib/chatHistoryMerge';
|
|
14
|
-
import { readPersistedMinimized, writePersistedMinimized } from '../lib/chatUiStorage';
|
|
15
|
-
import {
|
|
16
|
-
finalizeMessagesForDisplay,
|
|
17
|
-
type SvedaFinalizableMessage,
|
|
18
|
-
} from '../lib/finalizeMessages';
|
|
19
|
-
import { getUserMessageAttachmentNames, getUserMessageText } from '../lib/userMessage';
|
|
20
8
|
import { SvedaClientKey, SVEDA_EMBED_AUTH_EVENT } from '../plugin';
|
|
21
9
|
|
|
22
|
-
export
|
|
23
|
-
id: string;
|
|
24
|
-
title: string;
|
|
25
|
-
preview?: string;
|
|
26
|
-
messages: SvedaDisplayMessage[];
|
|
27
|
-
tokensUsed: number;
|
|
28
|
-
contextWindowTokens: number;
|
|
29
|
-
createdAt: number;
|
|
30
|
-
updatedAt: number;
|
|
31
|
-
messagesLoaded: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface SvedaChatState {
|
|
35
|
-
isOpen: boolean;
|
|
36
|
-
isMinimized: boolean;
|
|
37
|
-
currentChatId: string | null;
|
|
38
|
-
isLoading: boolean;
|
|
39
|
-
isLoadingChatHistory: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const initialIsMinimized = readPersistedMinimized();
|
|
43
|
-
|
|
44
|
-
const chatState = reactive<SvedaChatState>({
|
|
45
|
-
isOpen: !initialIsMinimized,
|
|
46
|
-
isMinimized: initialIsMinimized,
|
|
47
|
-
currentChatId: null,
|
|
48
|
-
isLoading: false,
|
|
49
|
-
isLoadingChatHistory: false,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const chatHistories = ref<SvedaChatHistory[]>([]);
|
|
53
|
-
const currentChat = ref<SvedaChatHistory | null>(null);
|
|
54
|
-
const openChatTabIds = ref<string[]>([]);
|
|
10
|
+
export type { SvedaChatHistory, SvedaChatState };
|
|
55
11
|
|
|
56
|
-
let activeClient: SvedaClient | null = null;
|
|
57
12
|
let initialized = false;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const generateChatId = (): string => `chat_${createMessageId()}`;
|
|
82
|
-
|
|
83
|
-
const ensureChatTab = (chatId: string) => {
|
|
84
|
-
if (!openChatTabIds.value.includes(chatId)) {
|
|
85
|
-
openChatTabIds.value = [...openChatTabIds.value, chatId];
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const removeChatTab = (chatId: string) => {
|
|
90
|
-
openChatTabIds.value = openChatTabIds.value.filter(id => id !== chatId);
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const toTimestamp = (value: unknown): number => {
|
|
94
|
-
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
95
|
-
return value;
|
|
96
|
-
}
|
|
97
|
-
if (typeof value === 'string') {
|
|
98
|
-
const parsed = Date.parse(value);
|
|
99
|
-
if (!Number.isNaN(parsed)) {
|
|
100
|
-
return parsed;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return Date.now();
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const finalizeForDisplay = (messages: unknown): SvedaDisplayMessage[] => {
|
|
107
|
-
const list = Array.isArray(messages) ? (messages as SvedaFinalizableMessage[]) : [];
|
|
108
|
-
return finalizeMessagesForDisplay(list) as unknown as SvedaDisplayMessage[];
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const resolveServerHistoryId = (history: SvedaChatHistorySummary): string => {
|
|
112
|
-
const fromChatId = typeof history.chatId === 'string' ? history.chatId.trim() : '';
|
|
113
|
-
if (fromChatId !== '') {
|
|
114
|
-
return fromChatId;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const fromId = typeof history.id === 'string' ? history.id.trim() : '';
|
|
118
|
-
return fromId;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const mapServerHistory = (
|
|
122
|
-
history: SvedaChatHistorySummary & { messages?: unknown },
|
|
123
|
-
messagesLoaded: boolean
|
|
124
|
-
): SvedaChatHistory => ({
|
|
125
|
-
id: resolveServerHistoryId(history),
|
|
126
|
-
title: history.title || '',
|
|
127
|
-
preview: typeof history.preview === 'string' ? history.preview : '',
|
|
128
|
-
messages: messagesLoaded ? finalizeForDisplay(history.messages) : [],
|
|
129
|
-
tokensUsed: Number(history.tokensUsed) || 0,
|
|
130
|
-
contextWindowTokens: Number(history.contextWindowTokens) || 0,
|
|
131
|
-
createdAt: toTimestamp(history.createdAt),
|
|
132
|
-
updatedAt: toTimestamp(history.updatedAt),
|
|
133
|
-
messagesLoaded,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const historiesEndpointConfigured = (): boolean => Boolean(activeClient?.endpoints.histories);
|
|
137
|
-
|
|
138
|
-
const embedAuthReady = (): boolean => {
|
|
139
|
-
if (!activeClient || activeClient.credentials !== 'omit') {
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const headers = activeClient.resolveHeaders();
|
|
144
|
-
return Boolean(
|
|
145
|
-
headers['X-Sveda-Embed-Token'] ||
|
|
146
|
-
headers['x-sveda-embed-token'] ||
|
|
147
|
-
headers.Authorization ||
|
|
148
|
-
headers.authorization,
|
|
149
|
-
);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const fetchHistoriesFromServer = async (): Promise<SvedaChatHistory[]> => {
|
|
153
|
-
if (!activeClient || !historiesEndpointConfigured() || !embedAuthReady()) {
|
|
154
|
-
return [];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
const histories = await activeClient.listHistories();
|
|
159
|
-
return histories.map(history => mapServerHistory(history, false));
|
|
160
|
-
} catch (err) {
|
|
161
|
-
console.error('Error loading chat histories from server:', err);
|
|
162
|
-
return [];
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const fetchChatHistoryFromServer = async (chatId: string): Promise<SvedaChatHistory | null> => {
|
|
167
|
-
if (!activeClient || !historiesEndpointConfigured() || !embedAuthReady()) {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
const detail = await activeClient.getHistory(chatId);
|
|
173
|
-
return mapServerHistory(detail, true);
|
|
174
|
-
} catch (err) {
|
|
175
|
-
console.error('Error loading chat history from server:', err);
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const deleteChatOnServer = async (chatId: string) => {
|
|
181
|
-
if (!activeClient || !historiesEndpointConfigured() || !embedAuthReady()) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
try {
|
|
186
|
-
await activeClient.deleteHistory(chatId);
|
|
187
|
-
} catch (err) {
|
|
188
|
-
console.error('Error deleting chat history from server:', err);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const renameChatOnServer = async (chatId: string, title: string) => {
|
|
193
|
-
if (!activeClient || !historiesEndpointConfigured() || !embedAuthReady()) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
try {
|
|
198
|
-
await activeClient.renameHistory(chatId, title);
|
|
199
|
-
} catch (err) {
|
|
200
|
-
console.error('Error renaming chat history on server:', err);
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const buildWelcomeMessage = (text: string): SvedaDisplayMessage => ({
|
|
205
|
-
id: createMessageId(),
|
|
206
|
-
role: 'assistant',
|
|
207
|
-
parts: [{ type: 'text', text }],
|
|
208
|
-
createdAt: new Date().toISOString(),
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
const loadHistories = async () => {
|
|
212
|
-
const serverHistories = await fetchHistoriesFromServer();
|
|
213
|
-
|
|
214
|
-
chatHistories.value = mergeServerChatHistories(serverHistories, currentChat.value);
|
|
215
|
-
currentChat.value = resolveMergedCurrentChat(chatHistories.value, currentChat.value);
|
|
216
|
-
if (currentChat.value) {
|
|
217
|
-
chatState.currentChatId = currentChat.value.id;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (chatHistories.value.length > 0 && !currentChat.value) {
|
|
221
|
-
const latest = [...chatHistories.value].sort((a, b) => b.updatedAt - a.updatedAt)[0];
|
|
222
|
-
await setCurrentChat(latest.id);
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
const createNewChat = (translate?: (key: string) => string): string => {
|
|
227
|
-
const chatId = generateChatId();
|
|
228
|
-
const welcomeText = translate ? translate('welcomeMessage') : '';
|
|
229
|
-
const newChatTitle = translate ? translate('newChat') : 'New Chat';
|
|
230
|
-
|
|
231
|
-
const newChat: SvedaChatHistory = {
|
|
232
|
-
id: chatId,
|
|
233
|
-
title: newChatTitle,
|
|
234
|
-
preview: '',
|
|
235
|
-
messages: welcomeText ? [buildWelcomeMessage(welcomeText)] : [],
|
|
236
|
-
tokensUsed: 0,
|
|
237
|
-
contextWindowTokens: 0,
|
|
238
|
-
createdAt: Date.now(),
|
|
239
|
-
updatedAt: Date.now(),
|
|
240
|
-
messagesLoaded: true,
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
chatHistories.value.unshift(newChat);
|
|
244
|
-
ensureChatTab(chatId);
|
|
245
|
-
void setCurrentChat(chatId, translate);
|
|
246
|
-
|
|
247
|
-
return chatId;
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
const loadChatHistoryIfNeeded = async (chatId: string): Promise<boolean> => {
|
|
251
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
252
|
-
if (chatIndex === -1) {
|
|
253
|
-
return false;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const chat = chatHistories.value[chatIndex];
|
|
257
|
-
if (chat.messagesLoaded) {
|
|
258
|
-
return true;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const loadedHistory = await fetchChatHistoryFromServer(chatId);
|
|
262
|
-
if (!loadedHistory) {
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
chatHistories.value[chatIndex] = {
|
|
267
|
-
...loadedHistory,
|
|
268
|
-
preview: chat.preview || loadedHistory.preview,
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
return true;
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const setCurrentChat = async (chatId: string, translate?: (key: string) => string) => {
|
|
275
|
-
chatState.isLoadingChatHistory = true;
|
|
276
|
-
try {
|
|
277
|
-
await loadChatHistoryIfNeeded(chatId);
|
|
278
|
-
|
|
279
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
280
|
-
const chat = chatIndex === -1 ? undefined : chatHistories.value[chatIndex];
|
|
281
|
-
if (chat) {
|
|
282
|
-
chatHistories.value[chatIndex] = {
|
|
283
|
-
...chat,
|
|
284
|
-
messages: finalizeForDisplay(chat.messages),
|
|
285
|
-
};
|
|
286
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
287
|
-
chatState.currentChatId = chatId;
|
|
288
|
-
ensureChatTab(chatId);
|
|
289
|
-
if (currentChat.value.messages.length === 0 && translate) {
|
|
290
|
-
const welcomeText = translate('welcomeMessage');
|
|
291
|
-
if (welcomeText) {
|
|
292
|
-
currentChat.value.messages.push(buildWelcomeMessage(welcomeText));
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
} finally {
|
|
297
|
-
chatState.isLoadingChatHistory = false;
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
const addMessage = (chatId: string, message: SvedaDisplayMessage): SvedaDisplayMessage | null => {
|
|
302
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
303
|
-
if (chatIndex !== -1) {
|
|
304
|
-
const chat = chatHistories.value[chatIndex];
|
|
305
|
-
if (!message.createdAt) {
|
|
306
|
-
message.createdAt = new Date().toISOString();
|
|
307
|
-
}
|
|
308
|
-
if (!message.id) {
|
|
309
|
-
message.id = createMessageId();
|
|
310
|
-
}
|
|
311
|
-
chat.messages = [...chat.messages, message];
|
|
312
|
-
chat.messagesLoaded = true;
|
|
313
|
-
chat.updatedAt = Date.now();
|
|
314
|
-
|
|
315
|
-
chatHistories.value = [...chatHistories.value];
|
|
316
|
-
|
|
317
|
-
if (currentChat.value?.id === chatId) {
|
|
318
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
319
|
-
}
|
|
320
|
-
return message;
|
|
321
|
-
}
|
|
322
|
-
return null;
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
const setChatTitle = (chatId: string, title: string) => {
|
|
326
|
-
const normalizedTitle = title.trim();
|
|
327
|
-
if (!normalizedTitle) {
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
332
|
-
if (chatIndex === -1) {
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const chat = chatHistories.value[chatIndex];
|
|
337
|
-
chat.title = normalizedTitle;
|
|
338
|
-
chat.updatedAt = Date.now();
|
|
339
|
-
|
|
340
|
-
chatHistories.value = [...chatHistories.value];
|
|
341
|
-
|
|
342
|
-
if (currentChat.value?.id === chatId) {
|
|
343
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
const incrementChatTokens = (chatId: string, tokens: number) => {
|
|
348
|
-
const normalizedTokens = Number(tokens);
|
|
349
|
-
if (!Number.isFinite(normalizedTokens) || normalizedTokens <= 0) {
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
354
|
-
if (chatIndex !== -1) {
|
|
355
|
-
const chat = chatHistories.value[chatIndex];
|
|
356
|
-
chat.tokensUsed = Math.max(0, Number(chat.tokensUsed || 0) + Math.floor(normalizedTokens));
|
|
357
|
-
chat.updatedAt = Date.now();
|
|
358
|
-
|
|
359
|
-
chatHistories.value = [...chatHistories.value];
|
|
360
|
-
|
|
361
|
-
if (currentChat.value?.id === chatId) {
|
|
362
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
const setChatContextWindowTokens = (chatId: string, tokens: number) => {
|
|
368
|
-
const normalizedTokens = Number(tokens);
|
|
369
|
-
if (!Number.isFinite(normalizedTokens) || normalizedTokens < 0) {
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
374
|
-
if (chatIndex !== -1) {
|
|
375
|
-
const chat = chatHistories.value[chatIndex];
|
|
376
|
-
chat.contextWindowTokens = Math.max(0, Math.floor(normalizedTokens));
|
|
377
|
-
|
|
378
|
-
chatHistories.value = [...chatHistories.value];
|
|
379
|
-
|
|
380
|
-
if (currentChat.value?.id === chatId) {
|
|
381
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
const updateMessage = (
|
|
387
|
-
chatId: string,
|
|
388
|
-
messageId: string,
|
|
389
|
-
updates: Partial<SvedaDisplayMessage>
|
|
390
|
-
) => {
|
|
391
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
392
|
-
if (chatIndex !== -1) {
|
|
393
|
-
const chat = chatHistories.value[chatIndex];
|
|
394
|
-
const messageIndex = chat.messages.findIndex(m => m.id === messageId);
|
|
395
|
-
if (messageIndex !== -1) {
|
|
396
|
-
const updatedMessage = {
|
|
397
|
-
...chat.messages[messageIndex],
|
|
398
|
-
...updates,
|
|
399
|
-
};
|
|
400
|
-
chat.messages.splice(messageIndex, 1, updatedMessage);
|
|
401
|
-
chat.updatedAt = Date.now();
|
|
402
|
-
|
|
403
|
-
chatHistories.value = [...chatHistories.value];
|
|
404
|
-
|
|
405
|
-
if (currentChat.value?.id === chatId) {
|
|
406
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
return updatedMessage;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return null;
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
const setChatMessages = (chatId: string, messages: SvedaDisplayMessage[]) => {
|
|
416
|
-
const result = upsertChatHistoryMessages(
|
|
417
|
-
chatHistories.value,
|
|
418
|
-
currentChat.value,
|
|
419
|
-
chatId,
|
|
420
|
-
finalizeForDisplay(messages)
|
|
421
|
-
);
|
|
422
|
-
|
|
423
|
-
chatHistories.value = result.histories;
|
|
424
|
-
currentChat.value = result.current;
|
|
425
|
-
if (result.current?.id === chatId) {
|
|
426
|
-
chatState.currentChatId = chatId;
|
|
427
|
-
ensureChatTab(chatId);
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
const deleteChat = (chatId: string, translate?: (key: string) => string) => {
|
|
432
|
-
const index = chatHistories.value.findIndex(c => c.id === chatId);
|
|
433
|
-
if (index !== -1) {
|
|
434
|
-
chatHistories.value.splice(index, 1);
|
|
435
|
-
removeChatTab(chatId);
|
|
436
|
-
void deleteChatOnServer(chatId);
|
|
437
|
-
|
|
438
|
-
if (chatState.currentChatId === chatId) {
|
|
439
|
-
if (chatHistories.value.length > 0) {
|
|
440
|
-
const nextChat = chatHistories.value[0];
|
|
441
|
-
void setCurrentChat(nextChat.id, translate);
|
|
442
|
-
} else {
|
|
443
|
-
createNewChat(translate);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
|
|
449
|
-
const renameChat = (chatId: string, title: string) => {
|
|
450
|
-
const normalizedTitle = title.trim();
|
|
451
|
-
if (!normalizedTitle) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
setChatTitle(chatId, normalizedTitle);
|
|
456
|
-
void renameChatOnServer(chatId, normalizedTitle);
|
|
457
|
-
};
|
|
13
|
+
let subscribed = false;
|
|
14
|
+
|
|
15
|
+
const store = getSvedaChatStore({ embedAuthEvent: SVEDA_EMBED_AUTH_EVENT });
|
|
16
|
+
const initial = store.getState();
|
|
17
|
+
const chatState = reactive<SvedaChatState>({ ...initial.chatState });
|
|
18
|
+
const currentChat = shallowRef<SvedaChatHistory | null>(initial.currentChat);
|
|
19
|
+
const chatHistories = shallowRef<SvedaChatHistory[]>(initial.chatHistories);
|
|
20
|
+
const storeRevision = ref(0);
|
|
21
|
+
|
|
22
|
+
const syncFromStore = () => {
|
|
23
|
+
const snapshot = store.getState();
|
|
24
|
+
Object.assign(chatState, snapshot.chatState);
|
|
25
|
+
currentChat.value = snapshot.currentChat;
|
|
26
|
+
chatHistories.value = snapshot.chatHistories;
|
|
27
|
+
storeRevision.value += 1;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
if (!subscribed) {
|
|
31
|
+
subscribed = true;
|
|
32
|
+
store.subscribe(syncFromStore);
|
|
33
|
+
}
|
|
458
34
|
|
|
459
35
|
export const useSvedaChat = () => {
|
|
460
36
|
const client = inject(SvedaClientKey, null);
|
|
461
37
|
const t = useSvedaT();
|
|
462
38
|
|
|
463
39
|
if (client) {
|
|
464
|
-
|
|
40
|
+
store.setClient(client);
|
|
465
41
|
}
|
|
42
|
+
store.setTranslate(t);
|
|
466
43
|
|
|
467
44
|
if (!initialized) {
|
|
468
45
|
initialized = true;
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
if (historiesEndpointConfigured() && embedAuthReady()) {
|
|
472
|
-
void loadHistories();
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
if (historiesEndpointConfigured() && embedAuthReady()) {
|
|
477
|
-
void loadHistories();
|
|
478
|
-
}
|
|
46
|
+
store.initAuthListener();
|
|
47
|
+
void store.loadHistories();
|
|
479
48
|
}
|
|
480
49
|
|
|
481
50
|
const isOpen = computed(() => chatState.isOpen);
|
|
@@ -484,103 +53,14 @@ export const useSvedaChat = () => {
|
|
|
484
53
|
const isLoadingChatHistory = computed(() => chatState.isLoadingChatHistory);
|
|
485
54
|
const hasCurrentChat = computed(() => !!currentChat.value);
|
|
486
55
|
const sortedHistories = computed(() => {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
56
|
+
void storeRevision.value;
|
|
57
|
+
return store.sortedHistories();
|
|
490
58
|
});
|
|
491
59
|
const openChatTabs = computed(() => {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
ids.push(currentChat.value.id);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
return ids
|
|
498
|
-
.map(id => chatHistories.value.find(chat => chat.id === id))
|
|
499
|
-
.filter((chat): chat is SvedaChatHistory => !!chat);
|
|
60
|
+
void storeRevision.value;
|
|
61
|
+
return store.openChatTabs();
|
|
500
62
|
});
|
|
501
63
|
|
|
502
|
-
const createNewChatWithI18n = (): string => {
|
|
503
|
-
return createNewChat(t);
|
|
504
|
-
};
|
|
505
|
-
|
|
506
|
-
const setCurrentChatWithI18n = async (chatId: string) => {
|
|
507
|
-
await setCurrentChat(chatId, t);
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
const closeChatTab = async (chatId: string) => {
|
|
511
|
-
const currentIndex = openChatTabIds.value.findIndex(id => id === chatId);
|
|
512
|
-
removeChatTab(chatId);
|
|
513
|
-
|
|
514
|
-
if (currentChat.value?.id !== chatId) {
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
const nextChatId =
|
|
519
|
-
openChatTabIds.value[currentIndex] ||
|
|
520
|
-
openChatTabIds.value[currentIndex - 1] ||
|
|
521
|
-
sortedHistories.value.find(chat => chat.id !== chatId)?.id;
|
|
522
|
-
|
|
523
|
-
if (nextChatId) {
|
|
524
|
-
await setCurrentChatWithI18n(nextChatId);
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
createNewChatWithI18n();
|
|
529
|
-
};
|
|
530
|
-
|
|
531
|
-
const openChat = async () => {
|
|
532
|
-
chatState.isOpen = true;
|
|
533
|
-
chatState.isMinimized = false;
|
|
534
|
-
writePersistedMinimized(false);
|
|
535
|
-
if (!currentChat.value && chatHistories.value.length === 0) {
|
|
536
|
-
createNewChatWithI18n();
|
|
537
|
-
} else if (!currentChat.value && chatHistories.value.length > 0) {
|
|
538
|
-
await setCurrentChatWithI18n(chatHistories.value[0].id);
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
|
|
542
|
-
const closeChat = () => {
|
|
543
|
-
chatState.isOpen = false;
|
|
544
|
-
chatState.isMinimized = true;
|
|
545
|
-
writePersistedMinimized(true);
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
const minimizeChat = () => {
|
|
549
|
-
chatState.isMinimized = true;
|
|
550
|
-
writePersistedMinimized(true);
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
const maximizeChat = () => {
|
|
554
|
-
chatState.isMinimized = false;
|
|
555
|
-
chatState.isOpen = true;
|
|
556
|
-
writePersistedMinimized(false);
|
|
557
|
-
};
|
|
558
|
-
|
|
559
|
-
const setLoading = (loading: boolean) => {
|
|
560
|
-
chatState.isLoading = loading;
|
|
561
|
-
};
|
|
562
|
-
|
|
563
|
-
const refreshChatTitleFromServer = async (chatId: string): Promise<string | null> => {
|
|
564
|
-
const loaded = await fetchChatHistoryFromServer(chatId);
|
|
565
|
-
if (!loaded) {
|
|
566
|
-
return null;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
if (loaded.messages.length > 0) {
|
|
570
|
-
const current =
|
|
571
|
-
currentChat.value?.id === chatId
|
|
572
|
-
? currentChat.value
|
|
573
|
-
: chatHistories.value.find(chat => chat.id === chatId);
|
|
574
|
-
const localCount = current?.messages.length ?? 0;
|
|
575
|
-
if (loaded.messages.length >= localCount) {
|
|
576
|
-
setChatMessages(chatId, loaded.messages);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
const title = loaded.title?.trim();
|
|
581
|
-
return title || null;
|
|
582
|
-
};
|
|
583
|
-
|
|
584
64
|
return {
|
|
585
65
|
chatState: readonly(chatState),
|
|
586
66
|
currentChat,
|
|
@@ -592,23 +72,23 @@ export const useSvedaChat = () => {
|
|
|
592
72
|
isLoading,
|
|
593
73
|
isLoadingChatHistory,
|
|
594
74
|
hasCurrentChat,
|
|
595
|
-
loadHistories,
|
|
596
|
-
createNewChat:
|
|
597
|
-
setCurrentChat:
|
|
598
|
-
addMessage,
|
|
599
|
-
setChatTitle,
|
|
600
|
-
refreshChatTitleFromServer,
|
|
601
|
-
updateMessage,
|
|
602
|
-
setChatMessages,
|
|
603
|
-
incrementChatTokens,
|
|
604
|
-
setChatContextWindowTokens,
|
|
605
|
-
deleteChat: (chatId: string) => deleteChat(chatId, t),
|
|
606
|
-
renameChat: (chatId: string, title: string) => renameChat(chatId, title),
|
|
607
|
-
closeChatTab,
|
|
608
|
-
openChat,
|
|
609
|
-
closeChat,
|
|
610
|
-
minimizeChat,
|
|
611
|
-
maximizeChat,
|
|
612
|
-
setLoading,
|
|
75
|
+
loadHistories: () => store.loadHistories(),
|
|
76
|
+
createNewChat: () => store.createNewChat(t),
|
|
77
|
+
setCurrentChat: (chatId: string) => store.setCurrentChat(chatId, t),
|
|
78
|
+
addMessage: store.addMessage.bind(store),
|
|
79
|
+
setChatTitle: store.setChatTitle.bind(store),
|
|
80
|
+
refreshChatTitleFromServer: store.refreshChatTitleFromServer.bind(store),
|
|
81
|
+
updateMessage: store.updateMessage.bind(store),
|
|
82
|
+
setChatMessages: store.setChatMessages.bind(store),
|
|
83
|
+
incrementChatTokens: store.incrementChatTokens.bind(store),
|
|
84
|
+
setChatContextWindowTokens: store.setChatContextWindowTokens.bind(store),
|
|
85
|
+
deleteChat: (chatId: string) => store.deleteChat(chatId, t),
|
|
86
|
+
renameChat: (chatId: string, title: string) => store.renameChat(chatId, title),
|
|
87
|
+
closeChatTab: (chatId: string) => store.closeChatTab(chatId, t),
|
|
88
|
+
openChat: () => store.openChat(t),
|
|
89
|
+
closeChat: () => store.closeChat(),
|
|
90
|
+
minimizeChat: () => store.minimizeChat(),
|
|
91
|
+
maximizeChat: () => store.maximizeChat(),
|
|
92
|
+
setLoading: (loading: boolean) => store.setLoading(loading),
|
|
613
93
|
};
|
|
614
94
|
};
|