@sveda-ai/vue 0.3.0 → 0.3.2
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 +4 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3047 -4428
- package/dist/plugin.d.ts +1 -0
- 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 +20 -28
- package/src/appearance.ts +44 -652
- package/src/components/chat/AnimatedStreamBlock.vue +12 -10
- package/src/components/chat/ChatMessageActivityBlock.vue +20 -16
- 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 +54 -553
- 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 +15 -11
- package/src/plugin.ts +3 -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
|
@@ -1,460 +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
|
-
import { SvedaClientKey } from '../plugin';
|
|
21
|
-
|
|
22
|
-
export interface SvedaChatHistory {
|
|
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
|
-
});
|
|
8
|
+
import { SvedaClientKey, SVEDA_EMBED_AUTH_EVENT } from '../plugin';
|
|
51
9
|
|
|
52
|
-
|
|
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 fetchHistoriesFromServer = async (): Promise<SvedaChatHistory[]> => {
|
|
139
|
-
if (!activeClient || !historiesEndpointConfigured()) {
|
|
140
|
-
return [];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
try {
|
|
144
|
-
const histories = await activeClient.listHistories();
|
|
145
|
-
return histories.map(history => mapServerHistory(history, false));
|
|
146
|
-
} catch (err) {
|
|
147
|
-
console.error('Error loading chat histories from server:', err);
|
|
148
|
-
return [];
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const fetchChatHistoryFromServer = async (chatId: string): Promise<SvedaChatHistory | null> => {
|
|
153
|
-
if (!activeClient || !historiesEndpointConfigured()) {
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
const detail = await activeClient.getHistory(chatId);
|
|
159
|
-
return mapServerHistory(detail, true);
|
|
160
|
-
} catch (err) {
|
|
161
|
-
console.error('Error loading chat history from server:', err);
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const deleteChatOnServer = async (chatId: string) => {
|
|
167
|
-
if (!activeClient || !historiesEndpointConfigured()) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
await activeClient.deleteHistory(chatId);
|
|
173
|
-
} catch (err) {
|
|
174
|
-
console.error('Error deleting chat history from server:', err);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const renameChatOnServer = async (chatId: string, title: string) => {
|
|
179
|
-
if (!activeClient || !historiesEndpointConfigured()) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
try {
|
|
184
|
-
await activeClient.renameHistory(chatId, title);
|
|
185
|
-
} catch (err) {
|
|
186
|
-
console.error('Error renaming chat history on server:', err);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
const buildWelcomeMessage = (text: string): SvedaDisplayMessage => ({
|
|
191
|
-
id: createMessageId(),
|
|
192
|
-
role: 'assistant',
|
|
193
|
-
parts: [{ type: 'text', text }],
|
|
194
|
-
createdAt: new Date().toISOString(),
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const loadHistories = async () => {
|
|
198
|
-
const serverHistories = await fetchHistoriesFromServer();
|
|
199
|
-
|
|
200
|
-
chatHistories.value = mergeServerChatHistories(serverHistories, currentChat.value);
|
|
201
|
-
currentChat.value = resolveMergedCurrentChat(chatHistories.value, currentChat.value);
|
|
202
|
-
if (currentChat.value) {
|
|
203
|
-
chatState.currentChatId = currentChat.value.id;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (chatHistories.value.length > 0 && !currentChat.value) {
|
|
207
|
-
const latest = [...chatHistories.value].sort((a, b) => b.updatedAt - a.updatedAt)[0];
|
|
208
|
-
await setCurrentChat(latest.id);
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
const createNewChat = (translate?: (key: string) => string): string => {
|
|
213
|
-
const chatId = generateChatId();
|
|
214
|
-
const welcomeText = translate ? translate('welcomeMessage') : '';
|
|
215
|
-
const newChatTitle = translate ? translate('newChat') : 'New Chat';
|
|
216
|
-
|
|
217
|
-
const newChat: SvedaChatHistory = {
|
|
218
|
-
id: chatId,
|
|
219
|
-
title: newChatTitle,
|
|
220
|
-
preview: '',
|
|
221
|
-
messages: welcomeText ? [buildWelcomeMessage(welcomeText)] : [],
|
|
222
|
-
tokensUsed: 0,
|
|
223
|
-
contextWindowTokens: 0,
|
|
224
|
-
createdAt: Date.now(),
|
|
225
|
-
updatedAt: Date.now(),
|
|
226
|
-
messagesLoaded: true,
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
chatHistories.value.unshift(newChat);
|
|
230
|
-
ensureChatTab(chatId);
|
|
231
|
-
void setCurrentChat(chatId, translate);
|
|
232
|
-
|
|
233
|
-
return chatId;
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
const loadChatHistoryIfNeeded = async (chatId: string): Promise<boolean> => {
|
|
237
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
238
|
-
if (chatIndex === -1) {
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const chat = chatHistories.value[chatIndex];
|
|
243
|
-
if (chat.messagesLoaded) {
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const loadedHistory = await fetchChatHistoryFromServer(chatId);
|
|
248
|
-
if (!loadedHistory) {
|
|
249
|
-
return false;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
chatHistories.value[chatIndex] = {
|
|
253
|
-
...loadedHistory,
|
|
254
|
-
preview: chat.preview || loadedHistory.preview,
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
return true;
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
const setCurrentChat = async (chatId: string, translate?: (key: string) => string) => {
|
|
261
|
-
chatState.isLoadingChatHistory = true;
|
|
262
|
-
try {
|
|
263
|
-
await loadChatHistoryIfNeeded(chatId);
|
|
264
|
-
|
|
265
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
266
|
-
const chat = chatIndex === -1 ? undefined : chatHistories.value[chatIndex];
|
|
267
|
-
if (chat) {
|
|
268
|
-
chatHistories.value[chatIndex] = {
|
|
269
|
-
...chat,
|
|
270
|
-
messages: finalizeForDisplay(chat.messages),
|
|
271
|
-
};
|
|
272
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
273
|
-
chatState.currentChatId = chatId;
|
|
274
|
-
ensureChatTab(chatId);
|
|
275
|
-
if (currentChat.value.messages.length === 0 && translate) {
|
|
276
|
-
const welcomeText = translate('welcomeMessage');
|
|
277
|
-
if (welcomeText) {
|
|
278
|
-
currentChat.value.messages.push(buildWelcomeMessage(welcomeText));
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
} finally {
|
|
283
|
-
chatState.isLoadingChatHistory = false;
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
const addMessage = (chatId: string, message: SvedaDisplayMessage): SvedaDisplayMessage | null => {
|
|
288
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
289
|
-
if (chatIndex !== -1) {
|
|
290
|
-
const chat = chatHistories.value[chatIndex];
|
|
291
|
-
if (!message.createdAt) {
|
|
292
|
-
message.createdAt = new Date().toISOString();
|
|
293
|
-
}
|
|
294
|
-
if (!message.id) {
|
|
295
|
-
message.id = createMessageId();
|
|
296
|
-
}
|
|
297
|
-
chat.messages = [...chat.messages, message];
|
|
298
|
-
chat.messagesLoaded = true;
|
|
299
|
-
chat.updatedAt = Date.now();
|
|
300
|
-
|
|
301
|
-
chatHistories.value = [...chatHistories.value];
|
|
302
|
-
|
|
303
|
-
if (currentChat.value?.id === chatId) {
|
|
304
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
305
|
-
}
|
|
306
|
-
return message;
|
|
307
|
-
}
|
|
308
|
-
return null;
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
const setChatTitle = (chatId: string, title: string) => {
|
|
312
|
-
const normalizedTitle = title.trim();
|
|
313
|
-
if (!normalizedTitle) {
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
318
|
-
if (chatIndex === -1) {
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const chat = chatHistories.value[chatIndex];
|
|
323
|
-
chat.title = normalizedTitle;
|
|
324
|
-
chat.updatedAt = Date.now();
|
|
325
|
-
|
|
326
|
-
chatHistories.value = [...chatHistories.value];
|
|
327
|
-
|
|
328
|
-
if (currentChat.value?.id === chatId) {
|
|
329
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
const incrementChatTokens = (chatId: string, tokens: number) => {
|
|
334
|
-
const normalizedTokens = Number(tokens);
|
|
335
|
-
if (!Number.isFinite(normalizedTokens) || normalizedTokens <= 0) {
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
340
|
-
if (chatIndex !== -1) {
|
|
341
|
-
const chat = chatHistories.value[chatIndex];
|
|
342
|
-
chat.tokensUsed = Math.max(0, Number(chat.tokensUsed || 0) + Math.floor(normalizedTokens));
|
|
343
|
-
chat.updatedAt = Date.now();
|
|
344
|
-
|
|
345
|
-
chatHistories.value = [...chatHistories.value];
|
|
346
|
-
|
|
347
|
-
if (currentChat.value?.id === chatId) {
|
|
348
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
const setChatContextWindowTokens = (chatId: string, tokens: number) => {
|
|
354
|
-
const normalizedTokens = Number(tokens);
|
|
355
|
-
if (!Number.isFinite(normalizedTokens) || normalizedTokens < 0) {
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
360
|
-
if (chatIndex !== -1) {
|
|
361
|
-
const chat = chatHistories.value[chatIndex];
|
|
362
|
-
chat.contextWindowTokens = Math.max(0, Math.floor(normalizedTokens));
|
|
363
|
-
|
|
364
|
-
chatHistories.value = [...chatHistories.value];
|
|
365
|
-
|
|
366
|
-
if (currentChat.value?.id === chatId) {
|
|
367
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
const updateMessage = (
|
|
373
|
-
chatId: string,
|
|
374
|
-
messageId: string,
|
|
375
|
-
updates: Partial<SvedaDisplayMessage>
|
|
376
|
-
) => {
|
|
377
|
-
const chatIndex = chatHistories.value.findIndex(c => c.id === chatId);
|
|
378
|
-
if (chatIndex !== -1) {
|
|
379
|
-
const chat = chatHistories.value[chatIndex];
|
|
380
|
-
const messageIndex = chat.messages.findIndex(m => m.id === messageId);
|
|
381
|
-
if (messageIndex !== -1) {
|
|
382
|
-
const updatedMessage = {
|
|
383
|
-
...chat.messages[messageIndex],
|
|
384
|
-
...updates,
|
|
385
|
-
};
|
|
386
|
-
chat.messages.splice(messageIndex, 1, updatedMessage);
|
|
387
|
-
chat.updatedAt = Date.now();
|
|
388
|
-
|
|
389
|
-
chatHistories.value = [...chatHistories.value];
|
|
390
|
-
|
|
391
|
-
if (currentChat.value?.id === chatId) {
|
|
392
|
-
currentChat.value = chatHistories.value[chatIndex];
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
return updatedMessage;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
return null;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
const setChatMessages = (chatId: string, messages: SvedaDisplayMessage[]) => {
|
|
402
|
-
const result = upsertChatHistoryMessages(
|
|
403
|
-
chatHistories.value,
|
|
404
|
-
currentChat.value,
|
|
405
|
-
chatId,
|
|
406
|
-
finalizeForDisplay(messages)
|
|
407
|
-
);
|
|
408
|
-
|
|
409
|
-
chatHistories.value = result.histories;
|
|
410
|
-
currentChat.value = result.current;
|
|
411
|
-
if (result.current?.id === chatId) {
|
|
412
|
-
chatState.currentChatId = chatId;
|
|
413
|
-
ensureChatTab(chatId);
|
|
414
|
-
}
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
const deleteChat = (chatId: string, translate?: (key: string) => string) => {
|
|
418
|
-
const index = chatHistories.value.findIndex(c => c.id === chatId);
|
|
419
|
-
if (index !== -1) {
|
|
420
|
-
chatHistories.value.splice(index, 1);
|
|
421
|
-
removeChatTab(chatId);
|
|
422
|
-
void deleteChatOnServer(chatId);
|
|
423
|
-
|
|
424
|
-
if (chatState.currentChatId === chatId) {
|
|
425
|
-
if (chatHistories.value.length > 0) {
|
|
426
|
-
const nextChat = chatHistories.value[0];
|
|
427
|
-
void setCurrentChat(nextChat.id, translate);
|
|
428
|
-
} else {
|
|
429
|
-
createNewChat(translate);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
const renameChat = (chatId: string, title: string) => {
|
|
436
|
-
const normalizedTitle = title.trim();
|
|
437
|
-
if (!normalizedTitle) {
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
setChatTitle(chatId, normalizedTitle);
|
|
442
|
-
void renameChatOnServer(chatId, normalizedTitle);
|
|
443
|
-
};
|
|
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
|
+
}
|
|
444
34
|
|
|
445
35
|
export const useSvedaChat = () => {
|
|
446
36
|
const client = inject(SvedaClientKey, null);
|
|
447
37
|
const t = useSvedaT();
|
|
448
38
|
|
|
449
39
|
if (client) {
|
|
450
|
-
|
|
40
|
+
store.setClient(client);
|
|
451
41
|
}
|
|
42
|
+
store.setTranslate(t);
|
|
452
43
|
|
|
453
44
|
if (!initialized) {
|
|
454
45
|
initialized = true;
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
}
|
|
46
|
+
store.initAuthListener();
|
|
47
|
+
void store.loadHistories();
|
|
458
48
|
}
|
|
459
49
|
|
|
460
50
|
const isOpen = computed(() => chatState.isOpen);
|
|
@@ -463,103 +53,14 @@ export const useSvedaChat = () => {
|
|
|
463
53
|
const isLoadingChatHistory = computed(() => chatState.isLoadingChatHistory);
|
|
464
54
|
const hasCurrentChat = computed(() => !!currentChat.value);
|
|
465
55
|
const sortedHistories = computed(() => {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
56
|
+
void storeRevision.value;
|
|
57
|
+
return store.sortedHistories();
|
|
469
58
|
});
|
|
470
59
|
const openChatTabs = computed(() => {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
ids.push(currentChat.value.id);
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return ids
|
|
477
|
-
.map(id => chatHistories.value.find(chat => chat.id === id))
|
|
478
|
-
.filter((chat): chat is SvedaChatHistory => !!chat);
|
|
60
|
+
void storeRevision.value;
|
|
61
|
+
return store.openChatTabs();
|
|
479
62
|
});
|
|
480
63
|
|
|
481
|
-
const createNewChatWithI18n = (): string => {
|
|
482
|
-
return createNewChat(t);
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
const setCurrentChatWithI18n = async (chatId: string) => {
|
|
486
|
-
await setCurrentChat(chatId, t);
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
const closeChatTab = async (chatId: string) => {
|
|
490
|
-
const currentIndex = openChatTabIds.value.findIndex(id => id === chatId);
|
|
491
|
-
removeChatTab(chatId);
|
|
492
|
-
|
|
493
|
-
if (currentChat.value?.id !== chatId) {
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const nextChatId =
|
|
498
|
-
openChatTabIds.value[currentIndex] ||
|
|
499
|
-
openChatTabIds.value[currentIndex - 1] ||
|
|
500
|
-
sortedHistories.value.find(chat => chat.id !== chatId)?.id;
|
|
501
|
-
|
|
502
|
-
if (nextChatId) {
|
|
503
|
-
await setCurrentChatWithI18n(nextChatId);
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
createNewChatWithI18n();
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
const openChat = async () => {
|
|
511
|
-
chatState.isOpen = true;
|
|
512
|
-
chatState.isMinimized = false;
|
|
513
|
-
writePersistedMinimized(false);
|
|
514
|
-
if (!currentChat.value && chatHistories.value.length === 0) {
|
|
515
|
-
createNewChatWithI18n();
|
|
516
|
-
} else if (!currentChat.value && chatHistories.value.length > 0) {
|
|
517
|
-
await setCurrentChatWithI18n(chatHistories.value[0].id);
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
const closeChat = () => {
|
|
522
|
-
chatState.isOpen = false;
|
|
523
|
-
chatState.isMinimized = true;
|
|
524
|
-
writePersistedMinimized(true);
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
const minimizeChat = () => {
|
|
528
|
-
chatState.isMinimized = true;
|
|
529
|
-
writePersistedMinimized(true);
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
const maximizeChat = () => {
|
|
533
|
-
chatState.isMinimized = false;
|
|
534
|
-
chatState.isOpen = true;
|
|
535
|
-
writePersistedMinimized(false);
|
|
536
|
-
};
|
|
537
|
-
|
|
538
|
-
const setLoading = (loading: boolean) => {
|
|
539
|
-
chatState.isLoading = loading;
|
|
540
|
-
};
|
|
541
|
-
|
|
542
|
-
const refreshChatTitleFromServer = async (chatId: string): Promise<string | null> => {
|
|
543
|
-
const loaded = await fetchChatHistoryFromServer(chatId);
|
|
544
|
-
if (!loaded) {
|
|
545
|
-
return null;
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
if (loaded.messages.length > 0) {
|
|
549
|
-
const current =
|
|
550
|
-
currentChat.value?.id === chatId
|
|
551
|
-
? currentChat.value
|
|
552
|
-
: chatHistories.value.find(chat => chat.id === chatId);
|
|
553
|
-
const localCount = current?.messages.length ?? 0;
|
|
554
|
-
if (loaded.messages.length >= localCount) {
|
|
555
|
-
setChatMessages(chatId, loaded.messages);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
const title = loaded.title?.trim();
|
|
560
|
-
return title || null;
|
|
561
|
-
};
|
|
562
|
-
|
|
563
64
|
return {
|
|
564
65
|
chatState: readonly(chatState),
|
|
565
66
|
currentChat,
|
|
@@ -571,23 +72,23 @@ export const useSvedaChat = () => {
|
|
|
571
72
|
isLoading,
|
|
572
73
|
isLoadingChatHistory,
|
|
573
74
|
hasCurrentChat,
|
|
574
|
-
loadHistories,
|
|
575
|
-
createNewChat:
|
|
576
|
-
setCurrentChat:
|
|
577
|
-
addMessage,
|
|
578
|
-
setChatTitle,
|
|
579
|
-
refreshChatTitleFromServer,
|
|
580
|
-
updateMessage,
|
|
581
|
-
setChatMessages,
|
|
582
|
-
incrementChatTokens,
|
|
583
|
-
setChatContextWindowTokens,
|
|
584
|
-
deleteChat: (chatId: string) => deleteChat(chatId, t),
|
|
585
|
-
renameChat: (chatId: string, title: string) => renameChat(chatId, title),
|
|
586
|
-
closeChatTab,
|
|
587
|
-
openChat,
|
|
588
|
-
closeChat,
|
|
589
|
-
minimizeChat,
|
|
590
|
-
maximizeChat,
|
|
591
|
-
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),
|
|
592
93
|
};
|
|
593
94
|
};
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
SVEDA_CHAT_LAYOUT_MIN_HEIGHT,
|
|
11
11
|
SVEDA_CHAT_LAYOUT_MIN_WIDTH,
|
|
12
12
|
SVEDA_HISTORY_SIDEBAR_WIDTH,
|
|
13
|
-
} from '
|
|
13
|
+
} from '@sveda-ai/chat';
|
|
14
14
|
import { SvedaFillHostKey, SvedaHostEmbedKey } from '../plugin';
|
|
15
15
|
|
|
16
16
|
const CHAT_STORAGE_KEY = 'sveda.chat-dimensions';
|
|
@@ -13,7 +13,7 @@ import { useSvedaPrompts, type SvedaQuickPromptItem } from './useSvedaPrompts';
|
|
|
13
13
|
import { useSvedaScroll, type SvedaScrollContainer } from './useSvedaScroll';
|
|
14
14
|
import { useSvedaShell, type SvedaBrandInfo } from './useSvedaShell';
|
|
15
15
|
import { useSvedaStreaming } from './useSvedaStreaming';
|
|
16
|
-
import { hasPendingToolConfirmation } from '
|
|
16
|
+
import { hasPendingToolConfirmation } from '@sveda-ai/chat';
|
|
17
17
|
|
|
18
18
|
export const svedaErrorMessage = (error: unknown, fallback: string): string => {
|
|
19
19
|
if (error instanceof Error && error.message.trim()) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { computed, type ComputedRef, type Ref } from 'vue';
|
|
2
|
-
import { userMessageHasVisibleContent, type SvedaUserMessageLike } from '
|
|
2
|
+
import { userMessageHasVisibleContent, type SvedaUserMessageLike } from '@sveda-ai/chat';
|
|
3
3
|
|
|
4
4
|
export interface SvedaQuickPromptItem {
|
|
5
5
|
label: string;
|