From b6a77a0f122296faad08ec894aa302c0f2b372b1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 7 Nov 2026 21:23:21 +0900 Subject: [PATCH] refac --- src/lib/components/NotificationToast.svelte | 20 +-- .../chat/FileNav/FilePreview.svelte | 3 +- .../chat/FileNav/MarkdownDocument.svelte | 45 +++--- .../chat/FileNav/MarkdownFileEditor.svelte | 139 ++++++++++++++++++ .../chat/Settings/Notifications.svelte | 60 +++++++- src/lib/i18n/locales/en-US/translation.json | 9 ++ src/lib/stores/index.ts | 4 +- src/lib/utils/audio.ts | 99 +++++++++++++ src/routes/+layout.svelte | 29 ++-- 9 files changed, 357 insertions(+), 51 deletions(-) create mode 100644 src/lib/components/chat/FileNav/MarkdownFileEditor.svelte diff --git a/src/lib/components/NotificationToast.svelte b/src/lib/components/NotificationToast.svelte index 844c502955..6cf4066dca 100644 --- a/src/lib/components/NotificationToast.svelte +++ b/src/lib/components/NotificationToast.svelte @@ -1,6 +1,7 @@ diff --git a/src/lib/components/chat/FileNav/FilePreview.svelte b/src/lib/components/chat/FileNav/FilePreview.svelte index 244408d938..164e1a16f3 100644 --- a/src/lib/components/chat/FileNav/FilePreview.svelte +++ b/src/lib/components/chat/FileNav/FilePreview.svelte @@ -311,6 +311,7 @@
-
diff --git a/src/lib/components/chat/FileNav/MarkdownDocument.svelte b/src/lib/components/chat/FileNav/MarkdownDocument.svelte index b6f0b273d6..824673a337 100644 --- a/src/lib/components/chat/FileNav/MarkdownDocument.svelte +++ b/src/lib/components/chat/FileNav/MarkdownDocument.svelte @@ -13,6 +13,29 @@ } ] }); + /** Local document resources never become arbitrary network requests. */ + export function resolveDocumentPath(documentPath: string, reference: string): string | null { + if (!reference || reference.startsWith('#') || reference.startsWith('//')) return null; + if (/^[a-z][a-z\d+.-]*:/i.test(reference) && !/^[a-z]:[\\/]/i.test(reference)) return null; + let path: string; + try { + path = decodeURIComponent(reference.split(/[?#]/)[0]).replace(/\\/g, '/'); + } catch { + return null; + } + if (/[\x00-\x1f]/.test(path)) return null; + const base = documentPath.replace(/\\/g, '/'); + if (!path.startsWith('/') && !/^[a-z]:\//i.test(path)) + path = base.slice(0, base.lastIndexOf('/') + 1) + path; + const prefix = /^[a-z]:\//i.test(path) ? path.slice(0, 3) : '/'; + const parts: string[] = []; + for (const part of path.slice(prefix.length).split('/')) { + if (part === '..') parts.pop(); + else if (part && part !== '.') parts.push(part); + } + return prefix + parts.join('/'); + } + + + diff --git a/src/lib/components/chat/Settings/Notifications.svelte b/src/lib/components/chat/Settings/Notifications.svelte index 73c51bc40b..a8928169ea 100644 --- a/src/lib/components/chat/Settings/Notifications.svelte +++ b/src/lib/components/chat/Settings/Notifications.svelte @@ -2,13 +2,22 @@ import UserSettingRow from './UserSettingRow.svelte'; import { canUseNotificationTargets } from '$lib/utils/settings-access'; import { getContext, onMount } from 'svelte'; + import { + playNotificationSound, + setNotificationSoundEnabled, + notificationSoundStyles, + unlockNotificationAudio, + type NotificationSoundStyle + } from '$lib/utils/audio'; import type { Writable } from 'svelte/store'; import { toast } from 'svelte-sonner'; import { config, settings, user } from '$lib/stores'; import Modal from '$lib/components/common/Modal.svelte'; import Switch from '$lib/components/common/Switch.svelte'; + import SettingsSelect from '$lib/components/common/SettingsSelect.svelte'; import Plus from '$lib/components/icons/Plus.svelte'; + import Play from '$lib/components/icons/Play.svelte'; import { createNotificationTarget, deleteNotificationTarget, @@ -26,6 +35,15 @@ let notificationEnabled = false; let notificationSound = true; + let notificationSoundType: NotificationSoundStyle = 'chime'; + const previewSound = async () => { + if (!notificationSound) return; + setNotificationSoundEnabled(true); + if (!(await playNotificationSound(notificationSoundType))) { + toast.error($i18n.t('Unable to play notification sound.')); + return; + } + }; let targets: NotificationTarget[] = []; // Keep dynamically translated event keys visible to i18next-parser. // $i18n.t('Chat finished'); $i18n.t('A chat run finished successfully.'); @@ -188,6 +206,8 @@ onMount(async () => { notificationEnabled = $settings.notificationEnabled ?? false; notificationSound = $settings?.notificationSound ?? true; + const savedSound = $settings?.notificationSoundType ?? 'chime'; + notificationSoundType = notificationSoundStyles.includes(savedSound) ? savedSound : 'chime'; }); @@ -213,7 +233,10 @@ {$i18n.t('Allow browser notifications for completed responses.')}

- + {$i18n.t('settings.personal.notifications.notificationSound.label')} @@ -222,11 +245,46 @@ bind:state={notificationSound} ariaLabel={$i18n.t('settings.personal.notifications.notificationSound.label')} on:change={() => { + setNotificationSoundEnabled(notificationSound); + if (notificationSound) void unlockNotificationAudio(); saveSettings({ notificationSound }); }} /> + {#if notificationSound} + +
+ { + saveSettings({ notificationSoundType }); + }} + > + + + + + + +
+
+ {/if} + {#if canUseWebhooks}
diff --git a/src/lib/i18n/locales/en-US/translation.json b/src/lib/i18n/locales/en-US/translation.json index add07cfb2e..b4ad5683fa 100644 --- a/src/lib/i18n/locales/en-US/translation.json +++ b/src/lib/i18n/locales/en-US/translation.json @@ -406,6 +406,7 @@ "Bearer": "", "before": "", "Being lazy": "", + "Bell": "Bell", "Binary file — text comparison is unavailable.": "", "Binary file changed": "", "Binary file. Download to open.": "", @@ -500,6 +501,7 @@ "Check what is running in this chat.": "", "Checking for updates...": "", "Checkout": "", + "Chime": "Chime", "Choose a date range to compare": "", "Choose a model before saving...": "", "Choose a parent page. Subpages move with this page.": "", @@ -2292,6 +2294,7 @@ "Preview behavior": "", "Preview failed": "", "Preview gesture": "", + "Preview notification sound": "Preview notification sound", "Preview not available": "", "Previous": "", "Previous 30 days": "", @@ -3817,8 +3820,11 @@ "settings.personal.interface.widescreenMode.label": "Widescreen Mode", "settings.personal.notifications.addNotificationTarget.label": "Add Notification Target", "settings.personal.notifications.browserNotifications.label": "Browser Notifications", + "settings.personal.notifications.notificationSound.description": "Play a sound when a response is ready.", "settings.personal.notifications.notificationSound.label": "Notification Sound", "settings.personal.notifications.notificationTargets.label": "Notification Targets", + "settings.personal.notifications.soundStyle.description": "Choose a sound and use the play button to preview it.", + "settings.personal.notifications.soundStyle.label": "Notification sound style", "settings.personal.notifications.title": "Notifications", "settings.personal.personalization.clearMemory.label": "Clear memory", "settings.personal.personalization.manageMemories.label": "Manage", @@ -3970,6 +3976,7 @@ "Slider": "", "Slim requires external services for embeddings, vector storage, and document extraction. Basic text files can be read locally.": "", "Slim requires external speech providers. Local speech models and audio conversion are unavailable.": "", + "Soft": "Soft", "Something went wrong :/": "", "Sonar": "", "Sonar Deep Research": "", @@ -4068,6 +4075,7 @@ "Tags": "", "Tail free sampling is used to reduce the impact of less probable tokens from the output. A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this setting.": "", "Talk to Model": "", + "Tap": "Tap", "Tap to interrupt": "", "Target ID": "", "Target ID for notify": "", @@ -4254,6 +4262,7 @@ "Unable to load session.": "Unable to load session.", "Unable to load sessions.": "Unable to load sessions.", "Unable to load tools": "", + "Unable to play notification sound.": "Unable to play notification sound.", "Unarchive": "", "Unarchive Chat": "", "Unassigned": "", diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts index 7ad229a7df..cbbe562002 100644 --- a/src/lib/stores/index.ts +++ b/src/lib/stores/index.ts @@ -10,7 +10,7 @@ import type { CommentTarget } from '$lib/apis/comments'; import type { ModelConfig } from '$lib/apis'; import type { Banner } from '$lib/types'; import type { Socket } from 'socket.io-client'; -import type { AudioQueue } from '$lib/utils/audio'; +import type { AudioQueue, NotificationSoundStyle } from '$lib/utils/audio'; import type { I18nOverrides } from '$lib/utils/translationDictionary'; import emojiShortCodes from '$lib/emoji-shortcodes.json'; @@ -210,7 +210,6 @@ export type DesktopEvent = export const desktopEvent: Writable = writable(null); export const isLastActiveTab = writable(true); -export const playingNotificationSound = writable(false); export type Model = OpenAIModel | OllamaModel | AgentModel; @@ -282,6 +281,7 @@ type Settings = { collapseCodeBlocks?: boolean; expandDetails?: boolean; notificationSound?: boolean; + notificationSoundType?: NotificationSoundStyle; notificationSoundAlways?: boolean; stylizedPdfExport?: boolean; notifications?: any; diff --git a/src/lib/utils/audio.ts b/src/lib/utils/audio.ts index 33328c895d..fc1d01f007 100644 --- a/src/lib/utils/audio.ts +++ b/src/lib/utils/audio.ts @@ -127,3 +127,102 @@ export class AudioQueue { this.current = null; } } + +export const notificationSoundStyles = ['chime', 'soft', 'bell', 'tap'] as const; +export type NotificationSoundStyle = (typeof notificationSoundStyles)[number]; + +type NotificationTone = { + frequency: number; + duration: number; + volume: number; + delay?: number; + endFrequency?: number; +}; + +const notificationTones: Record = { + chime: [ + { frequency: 659.25, duration: 0.36, volume: 0.08 }, + { frequency: 880, duration: 0.48, volume: 0.06, delay: 0.09 } + ], + soft: [ + { frequency: 440, duration: 0.3, volume: 0.07 }, + { frequency: 660, duration: 0.24, volume: 0.025 } + ], + bell: [ + { frequency: 1046.5, duration: 0.6, volume: 0.06 }, + { frequency: 2093, duration: 0.24, volume: 0.025 }, + { frequency: 2900, duration: 0.13, volume: 0.012 } + ], + tap: [{ frequency: 200, endFrequency: 90, duration: 0.07, volume: 0.16 }] +}; + +let notificationContext: AudioContext | null = null; +let notificationSoundEnabled = true; +const notificationVoices = new Set<{ oscillator: OscillatorNode; gain: GainNode }>(); + +function stopNotificationVoices() { + if (!notificationContext) return; + const now = notificationContext.currentTime; + for (const { oscillator, gain } of notificationVoices) { + gain.gain.cancelScheduledValues(now); + gain.gain.setValueAtTime(gain.gain.value, now); + gain.gain.linearRampToValueAtTime(0, now + 0.02); + oscillator.stop(now + 0.02); + } + notificationVoices.clear(); +} + +export function setNotificationSoundEnabled(enabled: boolean) { + notificationSoundEnabled = enabled; + if (!enabled) stopNotificationVoices(); +} + +// Unlock inside a user gesture so later background completions can make sound. +export async function unlockNotificationAudio(): Promise { + if ( + !notificationSoundEnabled || + typeof window === 'undefined' || + navigator.userActivation?.hasBeenActive === false || + !window.AudioContext + ) + return false; + try { + notificationContext ??= new AudioContext(); + if (notificationContext.state === 'suspended') await notificationContext.resume(); + return notificationContext.state === 'running'; + } catch { + return false; + } +} + +export async function playNotificationSound( + style: NotificationSoundStyle = 'chime' +): Promise { + if (!(await unlockNotificationAudio()) || !notificationSoundEnabled || !notificationContext) + return false; + stopNotificationVoices(); + const selected = notificationSoundStyles.includes(style) ? style : 'chime'; + for (const tone of notificationTones[selected]) { + const oscillator = notificationContext.createOscillator(); + const gain = notificationContext.createGain(); + const start = notificationContext.currentTime + (tone.delay ?? 0); + oscillator.frequency.setValueAtTime(tone.frequency, start); + if (tone.endFrequency) { + oscillator.frequency.exponentialRampToValueAtTime(tone.endFrequency, start + tone.duration); + } + gain.gain.setValueAtTime(0, start); + gain.gain.linearRampToValueAtTime(tone.volume, start + (selected === 'soft' ? 0.03 : 0.005)); + gain.gain.exponentialRampToValueAtTime(0.0001, start + tone.duration); + oscillator.connect(gain).connect(notificationContext.destination); + const voice = { oscillator, gain }; + notificationVoices.add(voice); + oscillator.onended = () => { + oscillator.disconnect(); + gain.disconnect(); + notificationVoices.delete(voice); + }; + oscillator.start(start); + oscillator.stop(start + tone.duration + 0.02); + } + return true; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 4092eac111..138feff65c 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,5 +1,10 @@ + +