-
Notifications
You must be signed in to change notification settings - Fork 892
Add system notifications + sounds #1131
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
Closed
ThallesP
wants to merge
3
commits into
pingdotgg:main
from
ThallesP:feature/web/agent-completion-notifications
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,352 @@ | ||
| import type { OrchestrationReadModel, ThreadId } from "@t3tools/contracts"; | ||
| import type { AppSettings } from "./appSettings"; | ||
| import { resolveCustomNotificationSoundSrc } from "./notificationSoundStorage"; | ||
|
|
||
| export type NotificationPermissionState = "unavailable" | "default" | "granted" | "denied"; | ||
| export type TurnCompletionOutcome = "success" | "error" | "interrupted"; | ||
|
|
||
| export interface SettledTurnNotificationCandidate { | ||
| threadId: ThreadId; | ||
| threadTitle: string; | ||
| projectTitle: string | null; | ||
| outcome: TurnCompletionOutcome; | ||
| turnId: string; | ||
| completedAt: string; | ||
| } | ||
|
|
||
| export interface NotificationDedupeState { | ||
| initialized: boolean; | ||
| settlementKeyByThreadId: Map<string, string | null>; | ||
| } | ||
|
|
||
| interface DispatchTurnCompletionEffectsInput { | ||
| settledTurn: SettledTurnNotificationCandidate; | ||
| settings: Pick< | ||
| AppSettings, | ||
| | "enableSystemNotifications" | ||
| | "enableCompletionSound" | ||
| | "notificationSoundSelection" | ||
| | "notificationCustomSoundId" | ||
| >; | ||
| backgrounded: boolean; | ||
| onOpenThread: (threadId: ThreadId) => void; | ||
| } | ||
|
|
||
| interface PlayCompletionSoundOptions { | ||
| src?: string | null; | ||
| swallowErrors?: boolean; | ||
| } | ||
|
|
||
| interface ShowTurnCompletionNotificationInput { | ||
| settledTurn: SettledTurnNotificationCandidate; | ||
| onOpenThread: (threadId: ThreadId) => void; | ||
| } | ||
|
|
||
| let completionSound: HTMLAudioElement | null = null; | ||
| let completionSoundSrc: string | null = null; | ||
|
|
||
| function getSettledTurnOutcome( | ||
| thread: OrchestrationReadModel["threads"][number], | ||
| ): TurnCompletionOutcome | null { | ||
| if (!thread.latestTurn?.completedAt) { | ||
| return null; | ||
| } | ||
|
|
||
| switch (thread.latestTurn.state) { | ||
| case "completed": | ||
| return "success"; | ||
| case "error": | ||
| return "error"; | ||
| case "interrupted": | ||
| return "interrupted"; | ||
| case "running": | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function getSettledTurnKey(thread: OrchestrationReadModel["threads"][number]): string | null { | ||
| const outcome = getSettledTurnOutcome(thread); | ||
| const completedAt = thread.latestTurn?.completedAt; | ||
| if (!outcome || !completedAt) { | ||
| return null; | ||
| } | ||
|
|
||
| return `${thread.latestTurn.turnId}:${completedAt}:${thread.latestTurn.state}`; | ||
| } | ||
|
|
||
| function getNotificationCopy(settledTurn: SettledTurnNotificationCandidate): { | ||
| title: string; | ||
| body: string; | ||
| } { | ||
| const body = settledTurn.projectTitle | ||
| ? `${settledTurn.projectTitle} - ${settledTurn.threadTitle}` | ||
| : settledTurn.threadTitle; | ||
|
|
||
| switch (settledTurn.outcome) { | ||
| case "success": | ||
| return { title: "Agent finished", body }; | ||
| case "error": | ||
| return { title: "Agent finished with an error", body }; | ||
| case "interrupted": | ||
| return { title: "Agent was interrupted", body }; | ||
| } | ||
| } | ||
|
|
||
| function buildSettledTurnCandidate( | ||
| thread: OrchestrationReadModel["threads"][number], | ||
| projectTitle: string | null, | ||
| ): SettledTurnNotificationCandidate | null { | ||
| const outcome = getSettledTurnOutcome(thread); | ||
| const completedAt = thread.latestTurn?.completedAt; | ||
| if (!outcome || !completedAt) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| threadId: thread.id, | ||
| threadTitle: thread.title, | ||
| projectTitle, | ||
| outcome, | ||
| turnId: thread.latestTurn.turnId, | ||
| completedAt, | ||
| }; | ||
| } | ||
|
|
||
| function replaceSettlementKeys( | ||
| dedupeState: NotificationDedupeState, | ||
| nextSettlementKeyByThreadId: Map<string, string | null>, | ||
| ): void { | ||
| dedupeState.settlementKeyByThreadId.clear(); | ||
| for (const [threadId, key] of nextSettlementKeyByThreadId) { | ||
| dedupeState.settlementKeyByThreadId.set(threadId, key); | ||
| } | ||
| } | ||
|
|
||
| function createCompletionSound(src: string): HTMLAudioElement { | ||
| if (typeof Audio === "undefined") { | ||
| throw new Error("Audio playback is unavailable in this environment."); | ||
| } | ||
|
|
||
| const audio = new Audio(src); | ||
| audio.preload = "auto"; | ||
| return audio; | ||
| } | ||
|
|
||
| export async function resolveCompletionSoundSrc( | ||
| settings: Pick< | ||
| AppSettings, | ||
| "enableCompletionSound" | "notificationSoundSelection" | "notificationCustomSoundId" | ||
| >, | ||
| ): Promise<string | null> { | ||
| if (!settings.enableCompletionSound) { | ||
| return null; | ||
| } | ||
|
|
||
| if ( | ||
| settings.notificationSoundSelection === "custom" && | ||
| settings.notificationCustomSoundId.length > 0 | ||
| ) { | ||
| const customSrc = await resolveCustomNotificationSoundSrc(settings.notificationCustomSoundId); | ||
| return customSrc ?? "/sounds/agent-finished.mp3"; | ||
| } | ||
|
|
||
| return "/sounds/agent-finished.mp3"; | ||
| } | ||
|
|
||
| export function createNotificationDedupeState(): NotificationDedupeState { | ||
| return { | ||
| initialized: false, | ||
| settlementKeyByThreadId: new Map(), | ||
| }; | ||
| } | ||
|
|
||
| export function seedNotificationDedupeStateFromSnapshot( | ||
| snapshot: OrchestrationReadModel, | ||
| dedupeState: NotificationDedupeState, | ||
| ): void { | ||
| const nextSettlementKeyByThreadId = new Map<string, string | null>(); | ||
| for (const thread of snapshot.threads) { | ||
| nextSettlementKeyByThreadId.set(thread.id, getSettledTurnKey(thread)); | ||
| } | ||
|
|
||
| dedupeState.initialized = true; | ||
| replaceSettlementKeys(dedupeState, nextSettlementKeyByThreadId); | ||
| } | ||
|
|
||
| export function getNotificationPermissionState(): NotificationPermissionState { | ||
| if (typeof Notification === "undefined") { | ||
| return "unavailable"; | ||
| } | ||
|
|
||
| switch (Notification.permission) { | ||
| case "granted": | ||
| return "granted"; | ||
| case "denied": | ||
| return "denied"; | ||
| case "default": | ||
| return "default"; | ||
| default: | ||
| return "unavailable"; | ||
| } | ||
| } | ||
|
|
||
| export async function requestNotificationPermission(): Promise<NotificationPermissionState> { | ||
| if (typeof Notification === "undefined") { | ||
| return "unavailable"; | ||
| } | ||
|
|
||
| try { | ||
| const permission = await Notification.requestPermission(); | ||
| switch (permission) { | ||
| case "granted": | ||
| return "granted"; | ||
| case "denied": | ||
| return "denied"; | ||
| case "default": | ||
| default: | ||
| return "default"; | ||
| } | ||
| } catch { | ||
| return getNotificationPermissionState(); | ||
| } | ||
| } | ||
|
|
||
| export function isAppBackgrounded(): boolean { | ||
| if (typeof document === "undefined") { | ||
| return false; | ||
| } | ||
|
|
||
| return document.visibilityState !== "visible" || !document.hasFocus(); | ||
| } | ||
|
|
||
| export function collectNewlySettledTurns( | ||
| previousSnapshot: OrchestrationReadModel | null, | ||
| nextSnapshot: OrchestrationReadModel, | ||
| dedupeState: NotificationDedupeState, | ||
| ): SettledTurnNotificationCandidate[] { | ||
| const previousSettlementKeyByThreadId = new Map<string, string | null>(); | ||
| if (previousSnapshot) { | ||
| for (const thread of previousSnapshot.threads) { | ||
| previousSettlementKeyByThreadId.set(thread.id, getSettledTurnKey(thread)); | ||
| } | ||
| } | ||
|
|
||
| const projectTitleById = new Map( | ||
| nextSnapshot.projects.map((project) => [project.id, project.title]), | ||
| ); | ||
| const nextSettlementKeyByThreadId = new Map<string, string | null>(); | ||
| const candidates: SettledTurnNotificationCandidate[] = []; | ||
|
|
||
| for (const thread of nextSnapshot.threads) { | ||
| const nextKey = getSettledTurnKey(thread); | ||
| nextSettlementKeyByThreadId.set(thread.id, nextKey); | ||
|
|
||
| if (!dedupeState.initialized || !nextKey) { | ||
| continue; | ||
| } | ||
|
|
||
| const previousKey = | ||
| dedupeState.settlementKeyByThreadId.get(thread.id) ?? | ||
| previousSettlementKeyByThreadId.get(thread.id) ?? | ||
| null; | ||
| if (previousKey === nextKey) { | ||
| continue; | ||
| } | ||
|
|
||
| const candidate = buildSettledTurnCandidate( | ||
| thread, | ||
| projectTitleById.get(thread.projectId) ?? null, | ||
| ); | ||
| if (candidate) { | ||
| candidates.push(candidate); | ||
| } | ||
| } | ||
|
|
||
| dedupeState.initialized = true; | ||
| replaceSettlementKeys(dedupeState, nextSettlementKeyByThreadId); | ||
|
|
||
| return candidates; | ||
| } | ||
|
|
||
| export async function playCompletionSound(options: PlayCompletionSoundOptions = {}): Promise<void> { | ||
| try { | ||
| if (options.src === null) { | ||
| return; | ||
| } | ||
| const src = options.src ?? "/sounds/agent-finished.mp3"; | ||
| if (!completionSound || completionSoundSrc !== src) { | ||
| completionSound = createCompletionSound(src); | ||
| completionSoundSrc = src; | ||
| } | ||
| const audio = completionSound; | ||
| audio.currentTime = 0; | ||
| await audio.play(); | ||
| } catch (error) { | ||
| if (options.swallowErrors) { | ||
| return; | ||
| } | ||
|
|
||
| throw error instanceof Error ? error : new Error(String(error)); | ||
| } | ||
| } | ||
|
|
||
| export async function playConfiguredCompletionSound( | ||
| settings: Pick< | ||
| AppSettings, | ||
| "enableCompletionSound" | "notificationSoundSelection" | "notificationCustomSoundId" | ||
| >, | ||
| options: Omit<PlayCompletionSoundOptions, "src"> = {}, | ||
| ): Promise<void> { | ||
| const src = await resolveCompletionSoundSrc(settings); | ||
| await playCompletionSound({ | ||
| ...options, | ||
| src, | ||
| }); | ||
| } | ||
|
|
||
| export function showTurnCompletionNotification( | ||
| input: ShowTurnCompletionNotificationInput, | ||
| ): Notification | null { | ||
| if (getNotificationPermissionState() !== "granted" || typeof Notification === "undefined") { | ||
| return null; | ||
| } | ||
|
|
||
| const copy = getNotificationCopy(input.settledTurn); | ||
| try { | ||
| const notification = new Notification(copy.title, { | ||
| body: copy.body, | ||
| tag: `agent-completion:${input.settledTurn.threadId}:${input.settledTurn.turnId}`, | ||
| }); | ||
| notification.addEventListener("click", () => { | ||
| if (typeof window !== "undefined") { | ||
| window.focus(); | ||
| } | ||
| input.onOpenThread(input.settledTurn.threadId); | ||
| notification.close(); | ||
| }); | ||
| return notification; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export async function dispatchTurnCompletionEffects( | ||
| input: DispatchTurnCompletionEffectsInput, | ||
| ): Promise<void> { | ||
| if (!input.backgrounded) { | ||
| return; | ||
| } | ||
|
|
||
| if (input.settings.enableSystemNotifications) { | ||
| showTurnCompletionNotification({ | ||
| settledTurn: input.settledTurn, | ||
| onOpenThread: input.onOpenThread, | ||
| }); | ||
| } | ||
|
|
||
| if (input.settings.enableCompletionSound) { | ||
| await playConfiguredCompletionSound(input.settings, { | ||
| swallowErrors: true, | ||
| }); | ||
| } | ||
| } | ||
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.
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.