fix: restore vscode native notification behavior

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.
This commit is contained in:
Bohdan Triapitsyn
2026-05-19 02:06:32 +03:00
parent d928185640
commit a57b02a308
8 changed files with 484 additions and 45 deletions
+37 -35
View File
@@ -26,6 +26,19 @@ const clearGlobalEventWatcherRetry = (): void => {
globalEventWatcherRetryTimer = null;
};
const unwrapGlobalEventPayload = (eventData: unknown): Record<string, unknown> | null => {
if (!eventData || typeof eventData !== 'object') {
return null;
}
const record = eventData as { payload?: unknown };
if (record.payload && typeof record.payload === 'object') {
return record.payload as Record<string, unknown>;
}
return eventData as Record<string, unknown>;
};
const reconcileSessionActivityFromStatus = async (manager: OpenCodeManager): Promise<void> => {
const baseUrl = manager.getApiUrl();
if (!baseUrl) {
@@ -61,7 +74,6 @@ const reconcileSessionActivityFromStatus = async (manager: OpenCodeManager): Pro
const setSessionActivityPhase = (sessionId: string, phase: ActivityPhase): void => {
if (!sessionId) return;
// Cancel existing cooldown timer
const existingTimer = sessionActivityCooldowns.get(sessionId);
if (existingTimer) {
clearTimeout(existingTimer);
@@ -69,36 +81,30 @@ const setSessionActivityPhase = (sessionId: string, phase: ActivityPhase): void
}
const current = sessionActivityPhases.get(sessionId);
if (current?.phase === phase) return; // No change
if (current?.phase === phase) return;
sessionActivityPhases.set(sessionId, { phase, updatedAt: Date.now() });
// Notify webview if available
if (chatViewProvider) {
chatViewProvider.postMessage({
type: 'openchamber:session-activity',
properties: {
sessionId,
phase,
},
});
}
chatViewProvider?.postMessage({
type: 'openchamber:session-activity',
properties: {
sessionId,
phase,
},
});
// Schedule transition from cooldown to idle
if (phase === 'cooldown') {
const timer = setTimeout(() => {
const now = sessionActivityPhases.get(sessionId);
if (now?.phase === 'cooldown') {
sessionActivityPhases.set(sessionId, { phase: 'idle', updatedAt: Date.now() });
if (chatViewProvider) {
chatViewProvider.postMessage({
type: 'openchamber:session-activity',
properties: {
sessionId,
phase: 'idle',
},
});
}
chatViewProvider?.postMessage({
type: 'openchamber:session-activity',
properties: {
sessionId,
phase: 'idle',
},
});
}
sessionActivityCooldowns.delete(sessionId);
}, SESSION_COOLDOWN_DURATION_MS);
@@ -230,22 +236,19 @@ export const startGlobalEventWatcher = async (
const result = await client.global.event({
signal,
sseMaxRetryAttempts: 0,
onSseEvent: (event) => {
const payload = event.data;
if (!payload || typeof payload !== 'object') {
return;
}
const activity = deriveSessionActivity(payload as Record<string, unknown>);
if (activity) {
setSessionActivityPhase(activity.sessionId, activity.phase);
}
},
});
console.log('[VSCode:Activity] connected');
for await (const _ of result.stream) {
void _;
for await (const event of result.stream) {
const payload = unwrapGlobalEventPayload((event as { payload?: unknown }).payload ?? event);
if (payload) {
const activity = deriveSessionActivity(payload);
if (activity) {
setSessionActivityPhase(activity.sessionId, activity.phase);
}
}
if (signal.aborted) {
break;
}
@@ -279,7 +282,6 @@ export const stopGlobalEventWatcher = (): void => {
globalEventWatcherAbortController = null;
chatViewProvider = null;
// Clear all cooldown timers
for (const timer of sessionActivityCooldowns.values()) {
clearTimeout(timer);
}