diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 8894d01e29..a2a912cf66 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -245,6 +245,7 @@ export let onCollapseEmbedded: (() => void) | null = null; // Pads the header and messages when the host draws the composer flush to its frame. export let embeddedInset = false; + export let onRecordedAudio: ((file: File) => unknown) | null = null; export let onNewEmbeddedChat: (() => void | Promise) | null = null; export let onCreateEmbeddedChat: | ((chat: { @@ -6169,7 +6170,7 @@ aria-labelledby={fullWidthTabs ? 'chat-panel-main-chat' : undefined} > {#if loading} - {#if !fullWidthTabs} + {#if !fullWidthTabs && !embeddedCollapsed}
@@ -6410,6 +6411,7 @@ {contextCompactionEnabled} {embedded} autofocus={!embeddedCollapsed} + {onRecordedAudio} compactHandler={handleManualCompact} statusHandler={handleStatusCommand} forkHandler={handleForkChat} @@ -6533,6 +6535,7 @@ {contextCompactionEnabled} {embedded} autofocus={!embeddedCollapsed} + {onRecordedAudio} compactHandler={handleManualCompact} statusHandler={handleStatusCommand} forkHandler={handleForkChat} diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index f21efee371..9faec2fb1f 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -180,6 +180,8 @@ export let contextCompactionEnabled = false; export let embedded = false; export let autofocus = true; + // Recorded audio goes to the host when given (note chats), otherwise it is attached to the message. + export let onRecordedAudio: ((file: File) => unknown) | null = null; export let callActive = false; export let callEnabled = true; @@ -788,6 +790,8 @@ let loaded = false; let recording = false; + let audioRecording = false; + let audioRecordingDisplayMedia = false; let isComposing = false; // Safari has a bug where compositionend is not triggered correctly #16615 @@ -1139,6 +1143,46 @@ return !files.some((item) => item.status === 'pending' && item.file instanceof File); }; + const recordedAudioHandler = async (file: File) => { + if (onRecordedAudio) await onRecordedAudio(file); + else await uploadFileHandler(file); + }; + + const recordActions = { + record: async () => { + try { + let stream = await navigator.mediaDevices.getUserMedia({ audio: true }).catch((err) => { + toast.error( + $i18n.t(`Permission denied when accessing microphone: {{error}}`, { error: err }) + ); + return null; + }); + if (stream) { + stream.getTracks().forEach((track) => track.stop()); + audioRecordingDisplayMedia = false; + audioRecording = true; + } + stream = null; + } catch { + toast.error($i18n.t('Permission denied when accessing microphone')); + } + }, + captureAudio: () => { + audioRecordingDisplayMedia = true; + audioRecording = true; + }, + upload: () => { + const input = document.createElement('input'); + input.type = 'file'; + input.accept = 'audio/*'; + input.onchange = async () => { + const file = input.files?.[0]; + if (file) await recordedAudioHandler(file); + }; + input.click(); + } + }; + const uploadFileHandler = async (file, process = true, itemData = {}) => { if ($_user?.role !== 'admin' && !($_user?.permissions?.chat?.file_upload ?? true)) { toast.error($i18n.t('You do not have permission to upload files.')); @@ -1726,6 +1770,7 @@ 'fork', 'status', 'comments', + 'record', 'side', 'model', 'compare', @@ -1773,6 +1818,7 @@ onCompact: compactHandler, onStatus: statusHandler, onComments: openComments, + onRecord: () => recordActions.record(), onSide: () => { if (!canSide) return; chatTabRequest.set({ parentChatId: chatId, prompt: '' }); @@ -2086,36 +2132,56 @@ }} /> -
- { - recording = false; + +
+
+ { + recording = false; - await tick(); - focus({ preventScroll: true }); - }} - onConfirm={async (data) => { - const { text, filename } = data; + await tick(); + focus({ preventScroll: true }); + }} + onConfirm={async (data) => { + const { text, filename } = data; - recording = false; + recording = false; - await tick(); - await insertTextAtCursor(`${text}`); - await tick(); - focus({ preventScroll: true }); + await tick(); + await insertTextAtCursor(`${text}`); + await tick(); + focus({ preventScroll: true }); - if ($settings?.speechAutoSend ?? false) { - dispatch('submit', prompt); - } - }} - /> + if ($settings?.speechAutoSend ?? false) { + dispatch('submit', prompt); + } + }} + /> +
+ {#if audioRecording} + { + audioRecording = false; + }} + onConfirm={async (data) => { + audioRecording = false; + if (data?.file) await recordedAudioHandler(data.file); + }} + /> + {/if}
{ dispatch('submit', prompt); }} @@ -2616,6 +2682,7 @@ {visionCapableModels} {toolApprovalMode} {onToolApprovalModeChange} + {recordActions} {screenCaptureHandler} {inputFilesHandler} uploadFilesHandler={() => { diff --git a/src/lib/components/chat/MessageInput/CommandSuggestionList.svelte b/src/lib/components/chat/MessageInput/CommandSuggestionList.svelte index d3afd61580..0a063fa6b7 100644 --- a/src/lib/components/chat/MessageInput/CommandSuggestionList.svelte +++ b/src/lib/components/chat/MessageInput/CommandSuggestionList.svelte @@ -25,6 +25,7 @@ export let onCompact: () => void = () => {}; export let onStatus: () => void = () => {}; export let onComments: () => void = () => {}; + export let onRecord: (() => void) | null = null; export let onSide: () => void = () => {}; export let onFork: () => void = () => {}; export let onModel: () => void = () => {}; @@ -127,6 +128,7 @@ compactDisabled={isCompactDisabled} canStatus={statusAvailable} canComments={commentsAvailable} + canRecord={onRecord !== null} canSide={sideAvailable} canFork={forkAvailable} forkDisabled={isForkDisabled} @@ -155,6 +157,9 @@ } else if (type === 'command' && data.id === 'comments') { command({ id: data.id, label: data.id }); onComments(); + } else if (type === 'command' && data.id === 'record') { + command({ id: data.id, label: data.id }); + onRecord?.(); } else if (type === 'command' && data.id === 'side') { command({ id: data.id, label: data.id }); onSide(); diff --git a/src/lib/components/chat/MessageInput/Commands/SlashCommands.svelte b/src/lib/components/chat/MessageInput/Commands/SlashCommands.svelte index 202557b1d0..8490b44b6b 100644 --- a/src/lib/components/chat/MessageInput/Commands/SlashCommands.svelte +++ b/src/lib/components/chat/MessageInput/Commands/SlashCommands.svelte @@ -20,6 +20,7 @@ import Icon from '$lib/components/chat/FileNav/Icon.svelte'; import Plus from '$lib/components/icons/Plus.svelte'; import Sparkles from '$lib/components/icons/Sparkles.svelte'; + import Mic from '$lib/components/icons/Mic.svelte'; import { getChatContext } from '$lib/components/chat/context'; const { chatId } = getChatContext(); @@ -35,6 +36,7 @@ export let compactDisabled = false; export let canStatus = false; export let canComments = false; + export let canRecord = false; export let canSide = false; export let canFork = false; export let forkDisabled = false; @@ -76,6 +78,9 @@ ...(canComments && 'comments'.startsWith(query.toLowerCase()) ? [{ type: 'command', data: { id: 'comments' } }] : []), + ...(canRecord && 'record'.startsWith(query.toLowerCase()) + ? [{ type: 'command', data: { id: 'record' } }] + : []), ...($visibleControls['chat.model_command'] && 'model'.startsWith(query.toLowerCase()) ? [{ type: 'command', data: { id: 'model' } }] : []), @@ -377,10 +382,14 @@ - {:else if item.data.id === 'comments' || item.data.id === 'side'} + {:else if item.data.id === 'comments' || item.data.id === 'side' || item.data.id === 'record'} + {/if} + {#if showAddContext}
+ {:else if tab === 'record' && recordActions} +
+ + + {#each [{ label: 'Record', icon: Mic, action: recordActions.record }, { label: 'Capture Audio', icon: CursorArrowRays, action: recordActions.captureAudio }, { label: 'Upload Audio', icon: CloudArrowUp, action: recordActions.upload }] as item} + + {/each} +
{:else if tab === 'attachments'}
- {loading - ? ' bg-base-200 dark:bg-base-700/50' - : 'bg-indigo-400/20 text-indigo-600 dark:text-indigo-300 '} - - - rounded-full" - aria-label={$i18n.t('Cancel')} - on:click={async () => { - cancelRecording(); - onCancel(); - }} - > - - +
+ + + {loading ? $i18n.t('Processing...') : formatSeconds(durationSeconds)} +
-
+
{#each visualizerData.slice().reverse() as rms} -
+
@@ -527,145 +533,30 @@
-
-
-
+ +
+ {:else} +
-
- -
- {#if loading} -
- -
- {:else} - - {/if} -
-
+ + + + {/if}
- - diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index 969aaf2f6f..0b76b6e40d 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -88,15 +88,12 @@ import RichTextInput from '../common/RichTextInput.svelte'; import { fileUploadsKey } from '../common/RichTextInput/FileAttachment'; import Spinner from '../common/Spinner.svelte'; - import Mic from '../icons/Mic.svelte'; - import VoiceRecording from '../chat/MessageInput/VoiceRecording.svelte'; import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; import AccessButton from '../common/AccessButton.svelte'; import ChatParticipants from '../common/ChatParticipants.svelte'; import FilesOverlay from '../chat/MessageInput/FilesOverlay.svelte'; - import RecordMenu from './RecordMenu.svelte'; import NoteMenu from './Notes/NoteMenu.svelte'; import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte'; import Sparkles from '../icons/Sparkles.svelte'; @@ -348,9 +345,6 @@ let versionIdx = null; let selectedModelId = null; - let recording = false; - let displayMediaRecord = false; - $: commentsEnabled = !!($config?.features?.enable_comments && $user && note?.id); let currentNoteComments: CommentsRequest | null = null; let linkedComment = ''; @@ -381,6 +375,8 @@ }; let noteChatId = null; let noteChatExpanded = false; + // The chat composer records audio; in a note chat it lands in the note instead of the message. + $: noteAudioHandler = note?.write_access ? (file: File) => inputFileHandler(file) : null; // Opening a note lands in its body; the docked chat composer no longer takes focus. let focusedNoteId = ''; const focusOpenedNote = (instance) => { @@ -1260,7 +1256,7 @@ ${content} show={!!currentNoteComments} align="end" maxHeight="min(36rem, calc(100dvh - 6rem))" - contentClass="w-[min(24rem,calc(100vw-2rem))] h-[min(36rem,calc(100dvh-6rem))] rounded-2xl border border-base-100 dark:border-base-800 bg-base-0 dark:bg-base-900 shadow-lg overflow-hidden! z-50" + contentClass="w-[min(24rem,calc(100vw-2rem))] h-[min(36rem,calc(100dvh-6rem))] rounded-3xl border border-base-100/50 dark:border-base-800/50 bg-base-0 dark:bg-base-900 shadow-lg overflow-hidden! z-50" onOpenChange={(open) => { if (!open) currentNoteComments = null; else currentNoteComments ??= { resource_type: 'note', resource_id: note.id }; @@ -1285,66 +1281,6 @@ ${content} {/if} - {#if note?.write_access} - { - displayMediaRecord = false; - - try { - let stream = await navigator.mediaDevices - .getUserMedia({ audio: true }) - .catch(function (err) { - toast.error( - $i18n.t(`Permission denied when accessing microphone: {{error}}`, { - error: err - }) - ); - return null; - }); - - if (stream) { - recording = true; - const tracks = stream.getTracks(); - tracks.forEach((track) => track.stop()); - } - stream = null; - } catch { - toast.error($i18n.t('Permission denied when accessing microphone')); - } - }} - onCaptureAudio={async () => { - displayMediaRecord = true; - - recording = true; - }} - onUpload={async () => { - const input = document.createElement('input'); - input.type = 'file'; - input.accept = 'audio/*'; - input.multiple = false; - input.click(); - - input.onchange = async (e) => { - const files = e.target.files; - - if (files && files.length > 0) { - await inputFileHandler(files[0]); - } - }; - }} - > - - - - - {/if} -
- {#if note && !noteChatLoading && (noteChatId || noteChatDraftKey)} + {#if note}
{#if !noteChatExpanded} @@ -1702,48 +1638,51 @@ ${content} {/if}
- {#key note.id} - (noteChatExpanded = false)} - onBeforeNoteSubmit={async () => { - noteChatExpanded = true; - await flushActivePage(); - }} - onInsertToNote={note?.write_access ? insertHandler : null} - onNewEmbeddedChat={createNoteChat} - onCreateEmbeddedChat={createNoteChatOnFirstMessage} - onSelectEmbeddedChat={(chatId) => { - if (!chatId || chatId === noteChatId) return; - noteChatId = chatId; - noteChatDraftKey = ''; - }} - onDeleteEmbeddedChat={deleteNoteChat} - onEmbeddedChatTitle={(chatId, title) => { - noteChats = noteChats.map((chat) => - chat.id === chatId - ? { - ...chat, - title, - chat: { - ...(chat.chat ?? {}), - title + {#if !noteChatLoading && (noteChatId || noteChatDraftKey)} + {#key note.id} + (noteChatExpanded = false)} + onBeforeNoteSubmit={async () => { + noteChatExpanded = true; + await flushActivePage(); + }} + onInsertToNote={note?.write_access ? insertHandler : null} + onNewEmbeddedChat={createNoteChat} + onCreateEmbeddedChat={createNoteChatOnFirstMessage} + onSelectEmbeddedChat={(chatId) => { + if (!chatId || chatId === noteChatId) return; + noteChatId = chatId; + noteChatDraftKey = ''; + }} + onDeleteEmbeddedChat={deleteNoteChat} + onEmbeddedChatTitle={(chatId, title) => { + noteChats = noteChats.map((chat) => + chat.id === chatId + ? { + ...chat, + title, + chat: { + ...(chat.chat ?? {}), + title + } } - } - : chat - ); - }} - /> - {/key} + : chat + ); + }} + /> + {/key} + {/if}
@@ -1754,31 +1693,5 @@ ${content} {/if} - {#if recording} -
-
- { - recording = false; - displayMediaRecord = false; - }} - onConfirm={(data) => { - if (data?.file) { - inputFileHandler(data?.file); - } - - recording = false; - displayMediaRecord = false; - }} - /> -
-
- {/if} diff --git a/src/lib/components/notes/RecordMenu.svelte b/src/lib/components/notes/RecordMenu.svelte deleted file mode 100644 index 603188f1f2..0000000000 --- a/src/lib/components/notes/RecordMenu.svelte +++ /dev/null @@ -1,74 +0,0 @@ - - - { - dispatch('change', state); - }} -> - - -
- - - - - - - -
-