2025-12-07 19:32:53 +02:00
|
|
|
import type { NotificationPayload, NotificationsAPI } from '@openchamber/ui/lib/api/types';
|
|
|
|
|
|
2026-04-26 00:48:48 +07:00
|
|
|
const SW_READY_TIMEOUT_MS = 1500;
|
2026-06-02 00:43:05 +03:00
|
|
|
const NOTIFICATION_DEDUPE_TTL_MS = 5000;
|
|
|
|
|
const NOTIFICATION_DEDUPE_STORAGE_PREFIX = 'openchamber-notification-claim:';
|
|
|
|
|
|
|
|
|
|
const notificationClaims = new Map<string, number>();
|
|
|
|
|
|
|
|
|
|
const isClientFocused = (): boolean => {
|
|
|
|
|
if (typeof document === 'undefined') return true;
|
|
|
|
|
return document.visibilityState === 'visible' && document.hasFocus();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getNotificationClaimKey = (payload?: NotificationPayload): string => {
|
|
|
|
|
const tag = typeof payload?.tag === 'string' ? payload.tag.trim() : '';
|
|
|
|
|
if (tag) return tag;
|
|
|
|
|
|
|
|
|
|
return [payload?.sessionId, payload?.kind, payload?.title, payload?.body]
|
|
|
|
|
.filter((value): value is string => typeof value === 'string' && value.trim().length > 0)
|
|
|
|
|
.map((value) => value.trim())
|
|
|
|
|
.join('|');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const pruneNotificationClaims = (now: number): void => {
|
|
|
|
|
for (const [key, claimedAt] of notificationClaims) {
|
|
|
|
|
if (now - claimedAt > NOTIFICATION_DEDUPE_TTL_MS) {
|
|
|
|
|
notificationClaims.delete(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const claimNotificationPayload = (payload?: NotificationPayload): boolean => {
|
|
|
|
|
const key = getNotificationClaimKey(payload);
|
|
|
|
|
if (!key) return true;
|
|
|
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
pruneNotificationClaims(now);
|
|
|
|
|
|
|
|
|
|
const claimedAt = notificationClaims.get(key) ?? 0;
|
|
|
|
|
if (now - claimedAt < NOTIFICATION_DEDUPE_TTL_MS) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (typeof window !== 'undefined' && window.localStorage) {
|
|
|
|
|
const storageKey = `${NOTIFICATION_DEDUPE_STORAGE_PREFIX}${key}`;
|
|
|
|
|
const stored = Number(window.localStorage.getItem(storageKey) ?? '0');
|
|
|
|
|
if (Number.isFinite(stored) && now - stored < NOTIFICATION_DEDUPE_TTL_MS) {
|
|
|
|
|
notificationClaims.set(key, stored);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (Number.isFinite(stored) && stored > 0) {
|
|
|
|
|
window.localStorage.removeItem(storageKey);
|
|
|
|
|
}
|
|
|
|
|
window.localStorage.setItem(storageKey, String(now));
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Storage is best-effort; in-memory dedupe still covers duplicate streams in this tab.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notificationClaims.set(key, now);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2026-04-26 00:48:48 +07:00
|
|
|
|
|
|
|
|
const getNotificationRegistration = async (): Promise<ServiceWorkerRegistration | null> => {
|
|
|
|
|
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let existing: ServiceWorkerRegistration | null = null;
|
|
|
|
|
try {
|
|
|
|
|
existing = (await navigator.serviceWorker.getRegistration()) ?? null;
|
|
|
|
|
} catch {
|
|
|
|
|
existing = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (existing?.active) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ready = await Promise.race<ServiceWorkerRegistration | null>([
|
|
|
|
|
navigator.serviceWorker.ready,
|
|
|
|
|
new Promise<null>((resolve) => {
|
|
|
|
|
setTimeout(() => resolve(null), SW_READY_TIMEOUT_MS);
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return ready ?? existing;
|
|
|
|
|
} catch {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const notifyWithServiceWorker = async (payload?: NotificationPayload): Promise<boolean> => {
|
|
|
|
|
const registration = await getNotificationRegistration();
|
|
|
|
|
if (!registration || typeof registration.showNotification !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await registration.showNotification(payload?.title ?? 'OpenChamber', {
|
|
|
|
|
body: payload?.body,
|
|
|
|
|
tag: payload?.tag,
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to send notification via service worker', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
const hasActivePushSubscription = async (): Promise<boolean> => {
|
|
|
|
|
const registration = await getNotificationRegistration();
|
|
|
|
|
if (!registration || !('pushManager' in registration) || !registration.pushManager) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return Boolean(await registration.pushManager.getSubscription());
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
const notifyWithWebAPI = async (payload?: NotificationPayload): Promise<boolean> => {
|
2026-06-02 00:43:05 +03:00
|
|
|
if (payload?.requireHidden && typeof document !== 'undefined' && document.hasFocus()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
if (typeof Notification === 'undefined') {
|
|
|
|
|
console.info('Notifications not supported in this environment', payload);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 01:19:24 +02:00
|
|
|
if (Notification.permission === 'default') {
|
|
|
|
|
const permission = await Notification.requestPermission();
|
|
|
|
|
if (permission !== 'granted') {
|
|
|
|
|
console.warn('Notification permission not granted');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Notification.permission !== 'granted') {
|
2025-12-07 19:32:53 +02:00
|
|
|
console.warn('Notification permission not granted');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
// Background push is the delivery channel when the web/PWA client is not
|
|
|
|
|
// focused. Keep the main notification toggle and templates enabled, but avoid
|
|
|
|
|
// also showing the same foreground notification from a hidden page.
|
|
|
|
|
if (!isClientFocused() && await hasActivePushSubscription()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!claimNotificationPayload(payload)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
try {
|
2026-04-26 00:48:48 +07:00
|
|
|
// Some installed PWAs expose Notification.permission but only allow
|
|
|
|
|
// notifications through an active service worker registration.
|
|
|
|
|
if (await notifyWithServiceWorker(payload)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
new Notification(payload?.title ?? 'OpenChamber', {
|
|
|
|
|
body: payload?.body,
|
|
|
|
|
tag: payload?.tag,
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to send notification', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
const notifyWithDesktop = async (payload?: NotificationPayload): Promise<boolean> => {
|
2026-02-05 01:59:49 +02:00
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 02:42:00 +03:00
|
|
|
const desktop = (window as unknown as { __OPENCHAMBER_DESKTOP__?: DesktopBridgeGlobal }).__OPENCHAMBER_DESKTOP__;
|
|
|
|
|
if (!desktop?.invoke) {
|
2026-02-05 01:59:49 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-06-03 02:42:00 +03:00
|
|
|
await desktop.invoke('desktop_notify', {
|
2026-02-05 01:59:49 +02:00
|
|
|
payload: {
|
|
|
|
|
title: payload?.title,
|
|
|
|
|
body: payload?.body,
|
|
|
|
|
tag: payload?.tag,
|
2026-06-02 00:43:05 +03:00
|
|
|
kind: payload?.kind,
|
|
|
|
|
sessionId: payload?.sessionId,
|
|
|
|
|
directory: payload?.directory,
|
|
|
|
|
requireHidden: payload?.requireHidden,
|
2026-02-05 01:59:49 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
2026-06-02 00:43:05 +03:00
|
|
|
console.warn('Failed to send native notification (desktop)', error);
|
2026-02-05 01:59:49 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export const createWebNotificationsAPI = (): NotificationsAPI => ({
|
|
|
|
|
async notifyAgentCompletion(payload?: NotificationPayload): Promise<boolean> {
|
2026-06-02 00:43:05 +03:00
|
|
|
return (await notifyWithDesktop(payload)) || (await notifyWithWebAPI(payload));
|
2026-02-05 01:59:49 +02:00
|
|
|
},
|
|
|
|
|
canNotify: () => {
|
|
|
|
|
if (typeof window !== 'undefined') {
|
2026-06-03 02:42:00 +03:00
|
|
|
const desktop = (window as unknown as { __OPENCHAMBER_DESKTOP__?: DesktopBridgeGlobal }).__OPENCHAMBER_DESKTOP__;
|
|
|
|
|
if (desktop?.invoke) {
|
2026-02-05 01:59:49 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return typeof Notification !== 'undefined' ? Notification.permission === 'granted' : false;
|
2025-12-07 19:32:53 +02:00
|
|
|
},
|
|
|
|
|
});
|
2026-06-03 02:42:00 +03:00
|
|
|
type DesktopBridgeGlobal = {
|
|
|
|
|
invoke?: (cmd: string, args?: Record<string, unknown>) => Promise<unknown>;
|
2026-02-05 01:59:49 +02:00
|
|
|
};
|