Skip to content

Commit 6d6dfbf

Browse files
committed
refac
1 parent 3425826 commit 6d6dfbf

1 file changed

Lines changed: 74 additions & 64 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
messageInput?.setText(data, async () => {
247247
if (!($settings?.insertSuggestionPrompt ?? false)) {
248248
await tick();
249-
submitPrompt(prompt);
249+
submitHandler(prompt);
250250
}
251251
});
252252
}
@@ -602,7 +602,7 @@
602602
603603
if (prompt !== '') {
604604
await tick();
605-
submitPrompt(prompt);
605+
submitHandler(prompt);
606606
}
607607
}
608608
@@ -623,7 +623,7 @@
623623
if (event.data.text !== '') {
624624
if (isSameOrigin) {
625625
await tick();
626-
submitPrompt(event.data.text);
626+
submitHandler(event.data.text);
627627
} else {
628628
// Cross-origin: ask user to confirm before submitting
629629
eventConfirmationInput = false;
@@ -632,7 +632,7 @@
632632
eventCallback = async (confirmed: boolean) => {
633633
if (confirmed) {
634634
await tick();
635-
submitPrompt(event.data.text);
635+
submitHandler(event.data.text);
636636
}
637637
};
638638
showEventConfirmation = true;
@@ -1232,7 +1232,7 @@
12321232
if (q) {
12331233
if (($page.url.searchParams.get('submit') ?? 'true') === 'true') {
12341234
await tick();
1235-
submitPrompt(q);
1235+
submitHandler(q);
12361236
}
12371237
}
12381238
}
@@ -1296,7 +1296,12 @@
12961296
12971297
if (history.currentId) {
12981298
for (const message of Object.values(history.messages)) {
1299-
if (message && message.role === 'assistant' && message.id !== history.currentId && message.done !== false) {
1299+
if (
1300+
message &&
1301+
message.role === 'assistant' &&
1302+
message.id !== history.currentId &&
1303+
message.done !== false
1304+
) {
13001305
message.done = true;
13011306
}
13021307
}
@@ -1352,14 +1357,16 @@
13521357
return rest;
13531358
});
13541359
1355-
files = combinedFiles;
1356-
await tick();
1357-
await submitPrompt(combinedPrompt);
1360+
await submitPrompt(combinedPrompt, combinedFiles);
13581361
};
13591362
13601363
const chatCompletedHandler = async (_chatId, modelId, responseMessageId, messages) => {
13611364
if (!responseMessageId) {
1362-
console.error('chatCompleted: missing message id', { chatId: _chatId, modelId, messageCount: messages?.length ?? 0 });
1365+
console.error('chatCompleted: missing message id', {
1366+
chatId: _chatId,
1367+
modelId,
1368+
messageCount: messages?.length ?? 0
1369+
});
13631370
return;
13641371
}
13651372
@@ -1787,8 +1794,56 @@
17871794
// Chat functions
17881795
//////////////////////////
17891796
1790-
const submitPrompt = async (userPrompt, { _raw = false } = {}) => {
1791-
console.log('submitPrompt', userPrompt, $chatId);
1797+
const submitPrompt = async (inputContent, inputFiles) => {
1798+
const _files = structuredClone(inputFiles);
1799+
1800+
chatFiles.push(
1801+
..._files.filter(
1802+
(item) =>
1803+
['doc', 'text', 'note', 'chat', 'folder', 'collection'].includes(item.type) ||
1804+
(item.type === 'file' && !(item?.content_type ?? '').startsWith('image/'))
1805+
)
1806+
);
1807+
chatFiles = chatFiles.filter(
1808+
// Remove duplicates
1809+
(item, index, array) =>
1810+
array.findIndex((i) => JSON.stringify(i) === JSON.stringify(item)) === index
1811+
);
1812+
1813+
// Create user message
1814+
let userMessageId = uuidv4();
1815+
let userMessage = {
1816+
id: userMessageId,
1817+
parentId: history.currentId ?? null,
1818+
childrenIds: [],
1819+
role: 'user',
1820+
content: inputContent,
1821+
files: _files.length > 0 ? _files : undefined,
1822+
timestamp: Math.floor(Date.now() / 1000), // Unix epoch
1823+
models: selectedModels
1824+
};
1825+
1826+
// Add message to history and Set currentId to messageId
1827+
history.messages[userMessageId] = userMessage;
1828+
1829+
// Append messageId to childrenIds of parent message
1830+
if (history.currentId !== null) {
1831+
history.messages[history.currentId].childrenIds.push(userMessageId);
1832+
}
1833+
1834+
history.currentId = userMessageId;
1835+
1836+
// focus on chat input
1837+
const chatInput = document.getElementById('chat-input');
1838+
chatInput?.focus();
1839+
1840+
saveSessionSelectedModels();
1841+
1842+
await sendMessage(history, userMessageId, { newChat: true });
1843+
};
1844+
1845+
const submitHandler = async (userPrompt, { _raw = false } = {}) => {
1846+
console.log('submitHandler', userPrompt, $chatId);
17921847
17931848
const _selectedModels = selectedModels.map((modelId) =>
17941849
$models.map((m) => m.id).includes(modelId) ? modelId : ''
@@ -1868,57 +1923,14 @@
18681923
}
18691924
}
18701925
1926+
// Clear input and submit
18711927
messageInput?.setText('');
18721928
prompt = '';
1873-
1874-
const messages = createMessagesList(history, history.currentId);
18751929
const _files = structuredClone(files);
1876-
1877-
chatFiles.push(
1878-
..._files.filter(
1879-
(item) =>
1880-
['doc', 'text', 'note', 'chat', 'folder', 'collection'].includes(item.type) ||
1881-
(item.type === 'file' && !(item?.content_type ?? '').startsWith('image/'))
1882-
)
1883-
);
1884-
chatFiles = chatFiles.filter(
1885-
// Remove duplicates
1886-
(item, index, array) =>
1887-
array.findIndex((i) => JSON.stringify(i) === JSON.stringify(item)) === index
1888-
);
1889-
18901930
files = [];
18911931
messageInput?.setText('');
18921932
1893-
// Create user message
1894-
let userMessageId = uuidv4();
1895-
let userMessage = {
1896-
id: userMessageId,
1897-
parentId: messages.length !== 0 ? messages.at(-1).id : null,
1898-
childrenIds: [],
1899-
role: 'user',
1900-
content: userPrompt,
1901-
files: _files.length > 0 ? _files : undefined,
1902-
timestamp: Math.floor(Date.now() / 1000), // Unix epoch
1903-
models: selectedModels
1904-
};
1905-
1906-
// Add message to history and Set currentId to messageId
1907-
history.messages[userMessageId] = userMessage;
1908-
history.currentId = userMessageId;
1909-
1910-
// Append messageId to childrenIds of parent message
1911-
if (messages.length !== 0) {
1912-
history.messages[messages.at(-1).id].childrenIds.push(userMessageId);
1913-
}
1914-
1915-
// focus on chat input
1916-
const chatInput = document.getElementById('chat-input');
1917-
chatInput?.focus();
1918-
1919-
saveSessionSelectedModels();
1920-
1921-
await sendMessage(history, userMessageId, { newChat: true });
1933+
await submitPrompt(userPrompt, _files);
19221934
};
19231935
19241936
const sendMessage = async (
@@ -2904,10 +2916,8 @@
29042916
// Stop current generation first
29052917
await stopResponse();
29062918
await tick();
2907-
// Set files and submit
2908-
files = item.files;
2909-
await tick();
2910-
await submitPrompt(item.prompt);
2919+
// Submit queued message directly without clearing input
2920+
await submitPrompt(item.prompt, item.files);
29112921
}
29122922
}}
29132923
onQueueEdit={(id) => {
@@ -2941,7 +2951,7 @@
29412951
if (e.detail || files.length > 0) {
29422952
await tick();
29432953

2944-
submitPrompt(e.detail.replaceAll('\n\n', '\n'));
2954+
submitHandler(e.detail.replaceAll('\n\n', '\n'));
29452955
}
29462956
}}
29472957
/>
@@ -2984,7 +2994,7 @@
29842994
clearDraft();
29852995
if (e.detail || files.length > 0) {
29862996
await tick();
2987-
submitPrompt(e.detail.replaceAll('\n\n', '\n'));
2997+
submitHandler(e.detail.replaceAll('\n\n', '\n'));
29882998
}
29892999
}}
29903000
/>
@@ -3009,7 +3019,7 @@
30093019
}
30103020
return a;
30113021
}, [])}
3012-
{submitPrompt}
3022+
submitPrompt={submitHandler}
30133023
{stopResponse}
30143024
{showMessage}
30153025
{eventTarget}

0 commit comments

Comments
 (0)