|
246 | 246 | messageInput?.setText(data, async () => { |
247 | 247 | if (!($settings?.insertSuggestionPrompt ?? false)) { |
248 | 248 | await tick(); |
249 | | - submitPrompt(prompt); |
| 249 | + submitHandler(prompt); |
250 | 250 | } |
251 | 251 | }); |
252 | 252 | } |
|
602 | 602 |
|
603 | 603 | if (prompt !== '') { |
604 | 604 | await tick(); |
605 | | - submitPrompt(prompt); |
| 605 | + submitHandler(prompt); |
606 | 606 | } |
607 | 607 | } |
608 | 608 |
|
|
623 | 623 | if (event.data.text !== '') { |
624 | 624 | if (isSameOrigin) { |
625 | 625 | await tick(); |
626 | | - submitPrompt(event.data.text); |
| 626 | + submitHandler(event.data.text); |
627 | 627 | } else { |
628 | 628 | // Cross-origin: ask user to confirm before submitting |
629 | 629 | eventConfirmationInput = false; |
|
632 | 632 | eventCallback = async (confirmed: boolean) => { |
633 | 633 | if (confirmed) { |
634 | 634 | await tick(); |
635 | | - submitPrompt(event.data.text); |
| 635 | + submitHandler(event.data.text); |
636 | 636 | } |
637 | 637 | }; |
638 | 638 | showEventConfirmation = true; |
|
1232 | 1232 | if (q) { |
1233 | 1233 | if (($page.url.searchParams.get('submit') ?? 'true') === 'true') { |
1234 | 1234 | await tick(); |
1235 | | - submitPrompt(q); |
| 1235 | + submitHandler(q); |
1236 | 1236 | } |
1237 | 1237 | } |
1238 | 1238 | } |
|
1296 | 1296 |
|
1297 | 1297 | if (history.currentId) { |
1298 | 1298 | 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 | + ) { |
1300 | 1305 | message.done = true; |
1301 | 1306 | } |
1302 | 1307 | } |
|
1352 | 1357 | return rest; |
1353 | 1358 | }); |
1354 | 1359 |
|
1355 | | - files = combinedFiles; |
1356 | | - await tick(); |
1357 | | - await submitPrompt(combinedPrompt); |
| 1360 | + await submitPrompt(combinedPrompt, combinedFiles); |
1358 | 1361 | }; |
1359 | 1362 |
|
1360 | 1363 | const chatCompletedHandler = async (_chatId, modelId, responseMessageId, messages) => { |
1361 | 1364 | 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 | + }); |
1363 | 1370 | return; |
1364 | 1371 | } |
1365 | 1372 |
|
|
1787 | 1794 | // Chat functions |
1788 | 1795 | ////////////////////////// |
1789 | 1796 |
|
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); |
1792 | 1847 |
|
1793 | 1848 | const _selectedModels = selectedModels.map((modelId) => |
1794 | 1849 | $models.map((m) => m.id).includes(modelId) ? modelId : '' |
|
1868 | 1923 | } |
1869 | 1924 | } |
1870 | 1925 |
|
| 1926 | + // Clear input and submit |
1871 | 1927 | messageInput?.setText(''); |
1872 | 1928 | prompt = ''; |
1873 | | -
|
1874 | | - const messages = createMessagesList(history, history.currentId); |
1875 | 1929 | 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 | | -
|
1890 | 1930 | files = []; |
1891 | 1931 | messageInput?.setText(''); |
1892 | 1932 |
|
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); |
1922 | 1934 | }; |
1923 | 1935 |
|
1924 | 1936 | const sendMessage = async ( |
|
2904 | 2916 | // Stop current generation first |
2905 | 2917 | await stopResponse(); |
2906 | 2918 | 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); |
2911 | 2921 | } |
2912 | 2922 | }} |
2913 | 2923 | onQueueEdit={(id) => { |
|
2941 | 2951 | if (e.detail || files.length > 0) { |
2942 | 2952 | await tick(); |
2943 | 2953 |
|
2944 | | - submitPrompt(e.detail.replaceAll('\n\n', '\n')); |
| 2954 | + submitHandler(e.detail.replaceAll('\n\n', '\n')); |
2945 | 2955 | } |
2946 | 2956 | }} |
2947 | 2957 | /> |
|
2984 | 2994 | clearDraft(); |
2985 | 2995 | if (e.detail || files.length > 0) { |
2986 | 2996 | await tick(); |
2987 | | - submitPrompt(e.detail.replaceAll('\n\n', '\n')); |
| 2997 | + submitHandler(e.detail.replaceAll('\n\n', '\n')); |
2988 | 2998 | } |
2989 | 2999 | }} |
2990 | 3000 | /> |
|
3009 | 3019 | } |
3010 | 3020 | return a; |
3011 | 3021 | }, [])} |
3012 | | - {submitPrompt} |
| 3022 | + submitPrompt={submitHandler} |
3013 | 3023 | {stopResponse} |
3014 | 3024 | {showMessage} |
3015 | 3025 | {eventTarget} |
|
0 commit comments