diff --git a/cptr/frontend/src/lib/components/FileBrowser.svelte b/cptr/frontend/src/lib/components/FileBrowser.svelte index 0a9426e..2cacaa1 100644 --- a/cptr/frontend/src/lib/components/FileBrowser.svelte +++ b/cptr/frontend/src/lib/components/FileBrowser.svelte @@ -18,6 +18,7 @@ } from '$lib/apis/files'; import { fileIconName } from '$lib/utils/fileIcon'; import Icon from './Icon.svelte'; + import Spinner from './common/Spinner.svelte'; import DropdownMenu from './DropdownMenu.svelte'; import { t } from '$lib/i18n'; diff --git a/cptr/frontend/src/lib/components/NotificationToast.svelte b/cptr/frontend/src/lib/components/NotificationToast.svelte new file mode 100644 index 0000000..79360a8 --- /dev/null +++ b/cptr/frontend/src/lib/components/NotificationToast.svelte @@ -0,0 +1,173 @@ + + + +
(moved = true)} +> + + + + +
+ cptr +
+ + +
+ {#if title} +
{title}
+ {/if} + {#if content} +
{content}
+ {/if} +
+
+ + diff --git a/cptr/frontend/src/lib/components/Settings/General.svelte b/cptr/frontend/src/lib/components/Settings/General.svelte index eeafe17..2de2028 100644 --- a/cptr/frontend/src/lib/components/Settings/General.svelte +++ b/cptr/frontend/src/lib/components/Settings/General.svelte @@ -4,10 +4,55 @@ import { theme, streamingBehavior } from '$lib/stores'; import type { Theme, StreamingBehavior } from '$lib/stores'; import { t, locale, changeLocale, supportedLocales } from '$lib/i18n'; + import { notificationsEnabled, notificationSound } from '$lib/stores/chat'; + import { fetchJSON } from '$lib/apis'; + import { onMount } from 'svelte'; function setTheme(v: Theme) { theme.set(v); } + + // ── Webhook URL ───────────────────────────────────────────── + let webhookUrl = $state(''); + let webhookLoading = $state(false); + + onMount(async () => { + try { + const data = await fetchJSON<{ config: Record }>('/api/admin/config/notifications'); + webhookUrl = data.config?.['notifications.webhook_url'] || ''; + } catch {} + }); + + async function saveWebhookUrl() { + webhookLoading = true; + try { + await fetchJSON('/api/admin/config', { + method: 'PUT', + body: JSON.stringify({ config: { 'notifications.webhook_url': webhookUrl.trim() || null } }) + }); + toast.success('Webhook URL saved'); + } catch { + toast.error('Failed to save webhook URL'); + } finally { + webhookLoading = false; + } + } + + async function toggleNotifications() { + if (!$notificationsEnabled) { + // Enabling: request permission + if ('Notification' in window) { + const permission = await Notification.requestPermission(); + if (permission === 'granted') { + notificationsEnabled.set(true); + } else { + toast.error('Browser notification permission denied'); + } + } + } else { + notificationsEnabled.set(false); + } + }
@@ -40,6 +85,74 @@ {/each} + +

Notifications

+ +
+ + +

+ Show OS-level notifications when a task completes and the tab is not focused. +

+ + + + + +
+ +
+ + +
+

+ Supports Slack, Discord, Teams, and generic JSON webhooks. +

+
+
+

Message queue

{#each [{ value: 'queue' as StreamingBehavior, label: 'Queue' }, { value: 'interrupt' as StreamingBehavior, label: 'Interrupt' }] as opt} diff --git a/cptr/frontend/src/lib/stores/chat.ts b/cptr/frontend/src/lib/stores/chat.ts index 35e2507..8f4a0bb 100644 --- a/cptr/frontend/src/lib/stores/chat.ts +++ b/cptr/frontend/src/lib/stores/chat.ts @@ -2,8 +2,10 @@ * Chat state: tracks whether chat is available (at least one connection configured). */ import { writable, get } from 'svelte/store'; +import { toast } from 'svelte-sonner'; import { fetchJSON } from '$lib/apis'; import { socketStore } from '$lib/stores/socket.svelte'; +import { activeTab, openChatTab } from '$lib/stores'; export const chatEnabled = writable(false); @@ -27,10 +29,27 @@ export function unregisterStreamingChat(chatId: string) { /** * Bind a global socket listener that clears streamingChatTabs when a * chat's "done" event arrives -- even if the ChatPanel is not mounted. + * Also fires browser notifications and in-app toasts. * Called once from the app layout. */ let globalListenerBound = false; +/** Whether browser notifications are enabled (persisted to localStorage). */ +export const notificationsEnabled = writable( + typeof localStorage !== 'undefined' ? localStorage.getItem('notificationsEnabled') === 'true' : false +); +notificationsEnabled.subscribe((v) => { + if (typeof localStorage !== 'undefined') localStorage.setItem('notificationsEnabled', String(v)); +}); + +/** Whether notification sound is enabled (persisted to localStorage). */ +export const notificationSound = writable( + typeof localStorage !== 'undefined' ? localStorage.getItem('notificationSound') !== 'false' : true +); +notificationSound.subscribe((v) => { + if (typeof localStorage !== 'undefined') localStorage.setItem('notificationSound', String(v)); +}); + export function bindGlobalChatListener() { if (globalListenerBound) return; @@ -41,17 +60,72 @@ export function bindGlobalChatListener() { return; } - socket.on('events:chat', (data: { chat_id: string; done?: boolean }) => { - if (!data.done) return; - const tabId = chatToTab.get(data.chat_id); - if (tabId) { - streamingChatTabs.update((s) => { - const next = new Set(s); - next.delete(tabId); - return next; + socket.on( + 'events:chat', + (data: { + chat_id: string; + done?: boolean; + title?: string; + content?: string; + workspace?: string; + }) => { + if (!data.done) return; + + // Clear streaming indicator for the tab + const tabId = chatToTab.get(data.chat_id); + if (tabId) { + streamingChatTabs.update((s) => { + const next = new Set(s); + next.delete(tabId); + return next; + }); + } + + // ── Notifications ────────────────────────────────── + // Skip if user is actively viewing this chat + const currentTab = get(activeTab); + const isViewingThisChat = + !document.hidden && + currentTab?.type === 'chat' && + currentTab?.path === data.chat_id; + + if (isViewingThisChat) return; + + const wsLabel = data.workspace ? `[${data.workspace}] ` : ''; + const title = `${wsLabel}${data.title || 'Chat'}`; + const body = data.content || ''; + + // In-app toast + import('$lib/components/NotificationToast.svelte').then((mod) => { + const chatId = data.chat_id; + const toastId = toast.custom(mod.default, { + componentProps: { + title, + content: body, + onClick: () => { + openChatTab(chatId); + toast.dismiss(toastId); + }, + onclose: () => { + toast.dismiss(toastId); + } + }, + duration: 10000, + unstyled: true + }); }); + + // Browser notification (only when tab is hidden) + if (document.hidden && get(notificationsEnabled)) { + try { + new Notification(`${title} • cptr`, { + body: body.slice(0, 200), + icon: '/favicon.png' + }); + } catch {} + } } - }); + ); globalListenerBound = true; }; diff --git a/cptr/frontend/src/routes/+layout.svelte b/cptr/frontend/src/routes/+layout.svelte index 2fdb10f..914303a 100644 --- a/cptr/frontend/src/routes/+layout.svelte +++ b/cptr/frontend/src/routes/+layout.svelte @@ -283,7 +283,7 @@ {/if}