Files
archived-orca/src/preload/api/notifications-bridge.ts
T
Kelvin AmoabaandPr1p 2e3c8fb530 fix(notifications): clear acknowledged desktop alerts (#37974)
Explicit worktree reads and session acknowledgements share the existing desktop dismissal authority. Desktop and phone copies preserve transport, physical workspace and session/cursor identity; Linux retains dismissible notification handles after banners expire.

Mailbox consumption keeps its existing semantics.

Fixes #37328
STA-13409

Co-authored-by: Pr1p <zwq19980411@gmail.com>
2026-11-04 04:03:39 +00:00

117 lines
4.5 KiB
TypeScript

import { ipcRenderer } from 'electron'
import type {
NotificationAttentionAcknowledgement,
NotificationDispatchRequest,
NotificationDeliveryProbeResult,
NotificationDismissResult,
NotificationDispatchResult,
NotificationPermissionStatusResult,
NotificationSoundDataResult,
NotificationSoundPathResult,
NotificationSoundResult,
StructuredNotificationRead
} from '../../shared/notification-settings-types'
import type { AgentSessionExecutionLocation } from '../../shared/agent-session/lifecycle/agent-session-record'
import type { PreloadApi } from '../api-types'
// Reuse sound data to avoid reading large files for every notification.
let cachedNotificationSound: {
path: string
blobUrl: string
audio: HTMLAudioElement
} | null = null
function disposeCachedNotificationSound(): void {
if (cachedNotificationSound) {
cachedNotificationSound.audio.pause()
cachedNotificationSound.audio.src = ''
URL.revokeObjectURL(cachedNotificationSound.blobUrl)
cachedNotificationSound = null
}
}
export const notificationsApi = {
getDesktopAwayState: (): Promise<boolean | undefined> =>
ipcRenderer.invoke('notifications:getDesktopAwayState'),
dispatch: (args: Record<string, unknown>): Promise<NotificationDispatchResult> =>
ipcRenderer.invoke('notifications:dispatch', args),
acknowledgeAttention: (
acknowledgement: NotificationAttentionAcknowledgement,
executionHost?: NotificationDispatchRequest['executionHost']
): Promise<NotificationDismissResult> =>
ipcRenderer.invoke('notifications:acknowledgeAttention', acknowledgement, executionHost),
dismiss: (
ids: string[],
paneKeys?: string[],
reads?: StructuredNotificationRead[]
): Promise<NotificationDismissResult> =>
reads === undefined
? ipcRenderer.invoke('notifications:dismiss', ids, paneKeys)
: ipcRenderer.invoke('notifications:dismiss', ids, paneKeys, reads),
settleStructuredPrompts: (
scope: AgentSessionExecutionLocation,
sessionId: string,
executionHost?: NotificationDispatchRequest['executionHost']
): Promise<void> =>
ipcRenderer.invoke('notifications:settleStructuredPrompts', scope, sessionId, executionHost),
openSystemSettings: (): Promise<void> => ipcRenderer.invoke('notifications:openSystemSettings'),
getPermissionStatus: (): Promise<NotificationPermissionStatusResult> =>
ipcRenderer.invoke('notifications:getPermissionStatus'),
probeDelivery: (args?: { force?: boolean }): Promise<NotificationDeliveryProbeResult> =>
ipcRenderer.invoke('notifications:probeDelivery', args),
playSound: async (options?: {
force?: boolean
volume?: number
}): Promise<NotificationSoundResult> => {
try {
const resolved = (await ipcRenderer.invoke(
'notifications:resolveSoundPath'
)) as NotificationSoundPathResult
if (!resolved.ok) {
if (cachedNotificationSound) {
disposeCachedNotificationSound()
}
return { played: false, reason: resolved.reason }
}
let entry = cachedNotificationSound
if (!entry || entry.path !== resolved.path) {
const sound = (await ipcRenderer.invoke(
'notifications:loadSound'
)) as NotificationSoundDataResult
if (!sound.ok) {
disposeCachedNotificationSound()
return { played: false, reason: sound.reason }
}
// Another playSound may have populated the cache during this load.
const latestEntry = cachedNotificationSound
if (latestEntry?.path === sound.path) {
entry = latestEntry
} else {
const arrayBuffer = new ArrayBuffer(sound.data.byteLength)
new Uint8Array(arrayBuffer).set(sound.data)
const blob = new Blob([arrayBuffer], { type: sound.mimeType })
disposeCachedNotificationSound()
const blobUrl = URL.createObjectURL(blob)
entry = { path: sound.path, blobUrl, audio: new Audio(blobUrl) }
cachedNotificationSound = entry
}
}
const audio = entry.audio
// Replay from the start so bursts do not stack copies.
audio.currentTime = 0
if (typeof options?.volume === 'number' && Number.isFinite(options.volume)) {
audio.volume = Math.min(1, Math.max(0, options.volume / 100))
}
try {
await audio.play()
} catch {
return { played: false, reason: 'playback-failed' }
}
return { played: true }
} catch {
return { played: false, reason: 'playback-failed' }
}
}
} satisfies PreloadApi['notifications']