Repository navigation
feat(web): add opt-in thread notifications and sounds #11481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maria-rcks
merged 4 commits into
pingdotgg:main
from
maria-rcks:feat/thread-notifications
Sep 13, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f992f5c
feat(web): add opt-in thread notifications and sounds
maria-rcks 3b3fe72
fix(web): ignore historical completion alerts
maria-rcks eaf5ad5
fix(web): ignore invalid notification timestamps
maria-rcks 376e41d
chore: merge main into thread notifications
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
111 changes: 111 additions & 0 deletions
111
apps/web/src/components/ThreadNotificationCoordinator.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { useAtomValue } from "@effect/atom-react"; | ||
| import { useNavigate } from "@tanstack/react-router"; | ||
| import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; | ||
| import * as Option from "effect/Option"; | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| import { getClientSettings, useClientSettings } from "../hooks/useSettings"; | ||
| import { useEnvironments } from "../state/environments"; | ||
| import { environmentShell } from "../state/shell"; | ||
| import { | ||
| hasDesktopNotifications, | ||
| hasNotificationSound, | ||
| playNotificationSound, | ||
| unlockNotificationAudio, | ||
| } from "../threadNotifications"; | ||
| import { resolveSidebarThreadStatus } from "./Sidebar.logic"; | ||
|
|
||
| export function ThreadNotificationCoordinator() { | ||
| const { environments } = useEnvironments(); | ||
| const mode = useClientSettings((settings) => settings.notificationMode); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasNotificationSound(mode)) return; | ||
| document.addEventListener("pointerdown", unlockNotificationAudio); | ||
| document.addEventListener("keydown", unlockNotificationAudio); | ||
| return () => { | ||
| document.removeEventListener("pointerdown", unlockNotificationAudio); | ||
| document.removeEventListener("keydown", unlockNotificationAudio); | ||
| }; | ||
| }, [mode]); | ||
|
|
||
| if (mode === "off") return null; | ||
|
|
||
| return environments.map((environment) => ( | ||
| <EnvironmentNotifications | ||
| key={environment.environmentId} | ||
| environmentId={environment.environmentId} | ||
| /> | ||
| )); | ||
| } | ||
|
|
||
| function EnvironmentNotifications({ environmentId }: { environmentId: EnvironmentId }) { | ||
| const shell = useAtomValue(environmentShell.stateValueAtom(environmentId)); | ||
| const mode = useClientSettings((settings) => settings.notificationMode); | ||
| const navigate = useNavigate(); | ||
| const previous = useRef(new Map<ThreadId, { input: string | null; completion: string | null }>()); | ||
|
|
||
| useEffect(() => { | ||
| if (shell.status !== "live" || Option.isNone(shell.snapshot)) { | ||
| previous.current.clear(); | ||
| return; | ||
| } | ||
| const next = new Map<ThreadId, { input: string | null; completion: string | null }>(); | ||
| for (const thread of shell.snapshot.value.threads) { | ||
| const status = resolveSidebarThreadStatus(thread); | ||
| const prior = previous.current.get(thread.id); | ||
| const input = | ||
| status === "input" || status === "approval" | ||
| ? `${thread.latestTurn?.turnId ?? ""}:${status}` | ||
| : null; | ||
| const completion = | ||
| status === "ready" && thread.latestTurn?.state === "completed" | ||
| ? thread.latestTurn.completedAt | ||
| : (prior?.completion ?? null); | ||
| next.set(thread.id, { input, completion }); | ||
| if (!prior || mode === "off" || thread.archivedAt !== null) continue; | ||
| const kind = | ||
| input && input !== prior.input | ||
| ? "input" | ||
| : completion && | ||
| (prior.completion === null || Date.parse(completion) > Date.parse(prior.completion)) | ||
| ? "completion" | ||
| : null; | ||
| if (!kind) continue; | ||
| if (hasNotificationSound(mode)) { | ||
| void playNotificationSound(kind, () => | ||
| hasNotificationSound(getClientSettings().notificationMode), | ||
| ); | ||
| } | ||
| if ( | ||
| !hasDesktopNotifications(mode) || | ||
| typeof Notification === "undefined" || | ||
| Notification.permission !== "granted" | ||
| ) | ||
| continue; | ||
| try { | ||
| const notification = new Notification( | ||
| kind === "completion" | ||
| ? "Thread completed" | ||
| : status === "approval" | ||
| ? "Approval needed" | ||
| : "Input needed", | ||
| { body: thread.title, tag: `${environmentId}:${thread.id}`, silent: true }, | ||
| ); | ||
| notification.addEventListener("click", () => { | ||
| notification.close(); | ||
| window.focus(); | ||
| void navigate({ | ||
| to: "/$environmentId/$threadId", | ||
| params: { environmentId, threadId: thread.id }, | ||
| }); | ||
| }); | ||
| } catch { | ||
| // Some browsers expose Notification but reject desktop presentation. | ||
| } | ||
| } | ||
| previous.current = next; | ||
| }, [environmentId, mode, navigate, shell]); | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| import { | ||
| hasDesktopNotifications, | ||
| hasNotificationSound, | ||
| NOTIFICATION_MODE_LABELS, | ||
| unlockNotificationAudio, | ||
| } from "../../threadNotifications"; | ||
| import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; | ||
| import { SettingsRow } from "./settingsLayout"; | ||
| import { searchableSetting } from "./settingsSearch"; | ||
| import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings"; | ||
|
|
||
| export function NotificationSettings() { | ||
| const mode = useScopedSettings((settings) => settings.notificationMode); | ||
| const updateSettings = useUpdateScopedSettings(); | ||
| const [permissionMessage, setPermissionMessage] = useState<string | null>(null); | ||
| const [requesting, setRequesting] = useState(false); | ||
|
|
||
| return ( | ||
| <SettingsRow | ||
| {...searchableSetting("thread-notifications")} | ||
| description={ | ||
| permissionMessage ?? | ||
| "Alert when a thread finishes or needs input. Applies to this device while T3 Code is open." | ||
| } | ||
| control={ | ||
| <Select | ||
| value={mode} | ||
| disabled={requesting} | ||
| onValueChange={async (value) => { | ||
| if ( | ||
| value !== "off" && | ||
| value !== "notifications" && | ||
| value !== "sound" && | ||
| value !== "notifications-and-sound" | ||
| ) | ||
| return; | ||
| setPermissionMessage(null); | ||
| if (hasNotificationSound(value)) unlockNotificationAudio(); | ||
| if (hasDesktopNotifications(value)) { | ||
| if (typeof Notification === "undefined" || !window.isSecureContext) { | ||
| setPermissionMessage( | ||
| "Notifications need a supported browser over HTTPS, or the desktop app. Sound only is still available.", | ||
| ); | ||
| return; | ||
| } | ||
| setRequesting(true); | ||
| try { | ||
| const permission = await Notification.requestPermission(); | ||
| if (permission !== "granted") { | ||
| setPermissionMessage( | ||
| "Allow notifications in your browser or system settings, then choose this option again. Sound only is still available.", | ||
| ); | ||
| return; | ||
| } | ||
| } catch { | ||
| setPermissionMessage( | ||
| "Notifications are unavailable in this browser. Sound only is still available.", | ||
| ); | ||
| return; | ||
| } finally { | ||
| setRequesting(false); | ||
| } | ||
| } | ||
| updateSettings({ notificationMode: value }); | ||
| }} | ||
| > | ||
| <SelectTrigger size="sm" className="w-full sm:w-56" aria-label="Thread notifications"> | ||
| <SelectValue>{NOTIFICATION_MODE_LABELS[mode]}</SelectValue> | ||
| </SelectTrigger> | ||
| <SelectPopup align="end" alignItemWithTrigger={false}> | ||
| {Object.entries(NOTIFICATION_MODE_LABELS).map(([value, label]) => ( | ||
| <SelectItem key={value} hideIndicator value={value}> | ||
| {label} | ||
| </SelectItem> | ||
| ))} | ||
| </SelectPopup> | ||
| </Select> | ||
| } | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { ClientSettings } from "@t3tools/contracts/settings"; | ||
|
|
||
| import completionUrl from "./assets/notification-completion.mp3"; | ||
| import inputUrl from "./assets/notification-input.mp3"; | ||
|
|
||
| type NotificationMode = ClientSettings["notificationMode"]; | ||
| export const NOTIFICATION_MODE_LABELS = { | ||
| off: "Off", | ||
| notifications: "Notifications only", | ||
| sound: "Sound only", | ||
| "notifications-and-sound": "Notifications with sound", | ||
| } satisfies Record<NotificationMode, string>; | ||
|
|
||
| export function hasNotificationSound(mode: NotificationMode) { | ||
| return mode === "sound" || mode === "notifications-and-sound"; | ||
| } | ||
|
|
||
| export function hasDesktopNotifications(mode: NotificationMode) { | ||
| return mode === "notifications" || mode === "notifications-and-sound"; | ||
| } | ||
|
|
||
| let audioContext: AudioContext | undefined; | ||
| const buffers = new Map<string, Promise<AudioBuffer>>(); | ||
|
|
||
| /** Called from a gesture so browsers allow later background playback. */ | ||
| export function unlockNotificationAudio() { | ||
| audioContext ??= new AudioContext(); | ||
| void audioContext.resume().catch(() => undefined); | ||
| } | ||
|
|
||
| export async function playNotificationSound( | ||
| kind: "completion" | "input", | ||
| shouldPlay: () => boolean, | ||
| ) { | ||
| if (!audioContext || audioContext.state !== "running") return; | ||
| const context = audioContext; | ||
| const url = kind === "completion" ? completionUrl : inputUrl; | ||
| try { | ||
| let buffer = buffers.get(url); | ||
| if (!buffer) { | ||
| buffer = fetch(url) | ||
| .then((response) => response.arrayBuffer()) | ||
| .then((data) => context.decodeAudioData(data)); | ||
| buffers.set(url, buffer); | ||
| } | ||
| const decoded = await buffer; | ||
| if (!shouldPlay() || context.state !== "running") return; | ||
| const source = context.createBufferSource(); | ||
| source.buffer = decoded; | ||
| source.connect(context.destination); | ||
| source.start(); | ||
| } catch { | ||
| buffers.delete(url); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.