Decouple bundled UI from runtime API and add remote instance tooling (#1228)
Add a packaged-client runtime boundary so the shared UI can talk to local, desktop, remote, and VS Code runtimes through the right transport instead of assuming one same-origin web server. Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and runtime URL helpers, while keeping official OpenCode traffic on the SDK path. Support runtime switching, remote host selection, desktop client credentials, and headless connection links for pairing packaged clients with remote OpenChamber servers. Harden the new auth model by moving long-lived client tokens out of browser URLs, introducing short-lived scoped URL tokens for browser-owned transports, restricting URL-token access to explicit readable/realtime routes, and making client-token management session-scoped or self-scoped as appropriate. Update browser-owned assets and preview proxy flows to work with the split runtime model, including authenticated project icons, preview token propagation, CSP-safe preview bridge injection, and preview proxy auth that survives short-lived URL-token expiry. Tighten Electron security boundaries for packaged clients by gating privileged preload state to trusted origins and requiring explicit confirmation before connect deep-links import or switch remote runtimes. Also refresh agent guidance and project skills so future runtime/API, auth, preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new architecture.
This commit is contained in:
committed by
GitHub
parent
a4314c189b
commit
2031e3b4a8
@@ -1,6 +1,66 @@
|
||||
import type { NotificationPayload, NotificationsAPI } from '@openchamber/ui/lib/api/types';
|
||||
|
||||
const SW_READY_TIMEOUT_MS = 1500;
|
||||
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;
|
||||
};
|
||||
|
||||
const getNotificationRegistration = async (): Promise<ServiceWorkerRegistration | null> => {
|
||||
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) {
|
||||
@@ -54,7 +114,24 @@ const notifyWithServiceWorker = async (payload?: NotificationPayload): Promise<b
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
const notifyWithWebAPI = async (payload?: NotificationPayload): Promise<boolean> => {
|
||||
if (payload?.requireHidden && typeof document !== 'undefined' && document.hasFocus()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof Notification === 'undefined') {
|
||||
console.info('Notifications not supported in this environment', payload);
|
||||
return false;
|
||||
@@ -73,6 +150,17 @@ const notifyWithWebAPI = async (payload?: NotificationPayload): Promise<boolean>
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
try {
|
||||
// Some installed PWAs expose Notification.permission but only allow
|
||||
// notifications through an active service worker registration.
|
||||
@@ -91,7 +179,7 @@ const notifyWithWebAPI = async (payload?: NotificationPayload): Promise<boolean>
|
||||
}
|
||||
};
|
||||
|
||||
const notifyWithTauri = async (payload?: NotificationPayload): Promise<boolean> => {
|
||||
const notifyWithDesktop = async (payload?: NotificationPayload): Promise<boolean> => {
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
@@ -107,18 +195,22 @@ const notifyWithTauri = async (payload?: NotificationPayload): Promise<boolean>
|
||||
title: payload?.title,
|
||||
body: payload?.body,
|
||||
tag: payload?.tag,
|
||||
kind: payload?.kind,
|
||||
sessionId: payload?.sessionId,
|
||||
directory: payload?.directory,
|
||||
requireHidden: payload?.requireHidden,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn('Failed to send native notification (tauri)', error);
|
||||
console.warn('Failed to send native notification (desktop)', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const createWebNotificationsAPI = (): NotificationsAPI => ({
|
||||
async notifyAgentCompletion(payload?: NotificationPayload): Promise<boolean> {
|
||||
return (await notifyWithTauri(payload)) || (await notifyWithWebAPI(payload));
|
||||
return (await notifyWithDesktop(payload)) || (await notifyWithWebAPI(payload));
|
||||
},
|
||||
canNotify: () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
|
||||
Reference in New Issue
Block a user