Move VS Code/Cursor desktop notifications onto the webview Notification API instead of the extension-host watcher path, which could not reliably produce native OS notifications. Route OpenCode runtime events from the shared sync pipeline into the VS Code webview so completion, error, question, and permission notifications use the same live event stream as the UI. Respect the OpenChamber notification settings in VS Code, including template rendering, completion cooldowns, permission auto-accept suppression, and the notify-while-focused mode. Use VS Code's window focus signal from the extension host instead of document.hasFocus() inside the webview, so hidden-only notifications are suppressed while Cursor or VS Code is focused across platforms.
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import type { NotificationPayload, NotificationsAPI } from '@openchamber/ui/lib/api/types';
|
|
|
|
const showWebviewNotification = async (payload?: NotificationPayload): Promise<boolean> => {
|
|
if (typeof Notification === 'undefined') {
|
|
return false;
|
|
}
|
|
|
|
if (Notification.permission === 'default') {
|
|
const permission = await Notification.requestPermission();
|
|
if (permission !== 'granted') {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (Notification.permission !== 'granted') {
|
|
return false;
|
|
}
|
|
|
|
const title = typeof payload?.title === 'string' && payload.title.trim().length > 0
|
|
? payload.title.trim()
|
|
: 'OpenChamber';
|
|
const body = typeof payload?.body === 'string' ? payload.body : '';
|
|
|
|
new Notification(title, { body });
|
|
return true;
|
|
};
|
|
|
|
export const createVSCodeNotificationsAPI = (): NotificationsAPI => ({
|
|
async notifyAgentCompletion(payload?: NotificationPayload): Promise<boolean> {
|
|
try {
|
|
return await showWebviewNotification(payload);
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
|
|
async canNotify(): Promise<boolean> {
|
|
return typeof Notification !== 'undefined' && Notification.permission !== 'denied';
|
|
},
|
|
});
|