A desktop coding agent with free access to frontier models.
TypeScript
feat: notification click opens chat
Samrat Talukdarb827663ae13d
diff --git a/apps/app/src/react-app/domains/session/sync/session-sync.ts b/apps/app/src/react-app/domains/session/sync/session-sync.tsindex 014c04c0..157a3d23 100644--- a/apps/app/src/react-app/domains/session/sync/session-sync.ts+++ b/apps/app/src/react-app/domains/session/sync/session-sync.ts@@ -961,7 +961,7 @@ function applyEvent(entry: SyncEntry, workspaceId: string, event: OpencodeEvent)});trackTaskFailed(sessionId, Date.now() - runStartedAt);}- notifyDesktopEvent({ type: "task.failed", sessionId, errorText });+ notifyDesktopEvent({ type: "task.failed", sessionId, workspaceId, errorText });useSessionActivityStore.getState().setError(workspaceId, sessionId, errorText);stopTrackingLiveSession(entry, sessionId);if (isTrackedSession(entry, sessionId)) {@@ -1035,6 +1035,7 @@ function applyEvent(entry: SyncEntry, workspaceId: string, event: OpencodeEvent)notifyDesktopEvent({type: "permission.asked",sessionId: permission.sessionID,+ workspaceId,detail: permissionNotificationDetail(permission),});useSessionActivityStore.getState().setWaitingRequest(workspaceId, permission.sessionID, "permission", permission.id, true);@@ -1057,6 +1058,7 @@ function applyEvent(entry: SyncEntry, workspaceId: string, event: OpencodeEvent)notifyDesktopEvent({type: "permission.asked",sessionId: permission.sessionID,+ workspaceId,detail: permissionNotificationDetail(permission),});useSessionActivityStore.getState().setWaitingRequest(workspaceId, permission.sessionID, "permission", permission.id, true);@@ -1086,6 +1088,7 @@ function applyEvent(entry: SyncEntry, workspaceId: string, event: OpencodeEvent)notifyDesktopEvent({type: "question.asked",sessionId: question.sessionID,+ workspaceId,question: questionNotificationText(question),});useSessionActivityStore.getState().setWaitingRequest(workspaceId, question.sessionID, "question", question.id, true);@@ -1449,7 +1452,7 @@ function applySessionRunStatus(duration_ms: Date.now() - runStartedAt,});trackTaskCompleted(sessionId, Date.now() - runStartedAt);- notifyDesktopEvent({ type: "task.completed", sessionId });+ notifyDesktopEvent({ type: "task.completed", sessionId, workspaceId });entry.titleRecovery?.observe(sessionId);}const shouldRecordTerminal = wasLive || runStartedAt !== null;diff --git a/apps/app/src/react-app/domains/settings/pages/preferences-view.tsx b/apps/app/src/react-app/domains/settings/pages/preferences-view.tsxindex 50c4a7d0..0e680737 100644--- a/apps/app/src/react-app/domains/settings/pages/preferences-view.tsx+++ b/apps/app/src/react-app/domains/settings/pages/preferences-view.tsx@@ -133,8 +133,11 @@ export function PreferencesView(props: PreferencesViewProps) {<SelectContent><SelectGroup>{DESKTOP_NOTIFICATION_PREFERENCE_VALUES.map((value) => (- <SelectItem key={value} value={value}>- {desktopNotificationPreferenceLabel(value)}+ <SelectItem key={value} value={value} data-testid={`desktop-notifications-${value}`}>+ <span className="flex flex-col">+ <span>{desktopNotificationPreferenceLabel(value)}</span>+ <span className="text-xs text-muted-foreground">{t(`settings.desktop_notifications.${value}_desc`)}</span>+ </span></SelectItem>))}</SelectGroup>diff --git a/apps/app/src/react-app/kernel/desktop-notification-preferences.ts b/apps/app/src/react-app/kernel/desktop-notification-preferences.tsindex ea0c53d5..5a93c094 100644--- a/apps/app/src/react-app/kernel/desktop-notification-preferences.ts+++ b/apps/app/src/react-app/kernel/desktop-notification-preferences.ts@@ -3,7 +3,8 @@ export const DESKTOP_NOTIFICATION_PREFERENCE_VALUES = ["off", "important", "all"export type DesktopNotificationPreference =(typeof DESKTOP_NOTIFICATION_PREFERENCE_VALUES)[number];-export const DEFAULT_DESKTOP_NOTIFICATION_PREFERENCE: DesktopNotificationPreference = "off";+/** Finished tasks and approvals or questions, while the window is not focused. */+export const DEFAULT_DESKTOP_NOTIFICATION_PREFERENCE: DesktopNotificationPreference = "all";export function isDesktopNotificationPreference(value: unknown,diff --git a/apps/app/src/react-app/shell/app-menu.tsx b/apps/app/src/react-app/shell/app-menu.tsxindex 6f30cb76..762d9247 100644--- a/apps/app/src/react-app/shell/app-menu.tsx+++ b/apps/app/src/react-app/shell/app-menu.tsx@@ -3,6 +3,7 @@ import { useEffect, type ReactNode } from "react";import { useLocation, useNavigate } from "react-router";import { useUpdateCheckRequestStore } from "../domains/settings/state/update-check-request";+import { NOTIFICATION_OPEN_SESSION_EVENT, notificationSessionRoute } from "./desktop-notifications";import { useUiStateStore } from "./ui-state-store";import { settingsNavigationFromPathname } from "./workspace-routes";@@ -26,13 +27,26 @@ export function AppMenuProvider({ children }: { children: ReactNode }) {openSettingsTab("updates");};+ // A clicked desktop notification: the shell focused the window, open the chat it was about.+ const openNotifiedSession = (event: Event) => {+ const detail = event instanceof CustomEvent ? (event.detail as unknown) : null;+ if (!detail || typeof detail !== "object") return;+ const sessionId = Reflect.get(detail, "sessionId");+ const workspaceId = Reflect.get(detail, "workspaceId");+ if (typeof sessionId !== "string" || !sessionId.trim()) return;+ const route = notificationSessionRoute({ sessionId, workspaceId: typeof workspaceId === "string" ? workspaceId : undefined });+ if (route !== pathname) navigate(route);+ };+window.addEventListener(NATIVE_MENU_OPEN_SETTINGS_EVENT, openSettings);window.addEventListener(NATIVE_MENU_TOGGLE_SIDEBAR_EVENT, toggleSidebar);window.addEventListener(NATIVE_MENU_CHECK_UPDATES_EVENT, checkUpdates);+ window.addEventListener(NOTIFICATION_OPEN_SESSION_EVENT, openNotifiedSession);return () => {window.removeEventListener(NATIVE_MENU_OPEN_SETTINGS_EVENT, openSettings);window.removeEventListener(NATIVE_MENU_TOGGLE_SIDEBAR_EVENT, toggleSidebar);window.removeEventListener(NATIVE_MENU_CHECK_UPDATES_EVENT, checkUpdates);+ window.removeEventListener(NOTIFICATION_OPEN_SESSION_EVENT, openNotifiedSession);};}, [navigate, pathname, toggleSidebar]);diff --git a/apps/app/src/react-app/shell/desktop-notifications.ts b/apps/app/src/react-app/shell/desktop-notifications.tsindex a9a73430..fdca7930 100644--- a/apps/app/src/react-app/shell/desktop-notifications.ts+++ b/apps/app/src/react-app/shell/desktop-notifications.ts@@ -6,15 +6,29 @@ import {type DesktopNotificationPreference,} from "@/react-app/kernel/desktop-notification-preferences";import { LOCAL_PREFERENCES_KEY } from "@/react-app/kernel/local-preferences-storage";+import { legacySessionRoute, workspaceSessionRoute } from "@/react-app/shell/workspace-routes";type DesktopNotificationImportance = "important" | "routine";type WebNotificationHandler = (title: string, description?: string, href?: string) => Promise<void>;+/** The workspace the session belongs to, so a click on the notification can open it. */+type NotificationTarget = { sessionId: string; workspaceId?: string };+export type DesktopNotificationEvent =- | { type: "task.completed"; sessionId: string }- | { type: "task.failed"; sessionId: string; errorText?: string }- | { type: "permission.asked"; sessionId: string; detail?: string }- | { type: "question.asked"; sessionId: string; question?: string };+ | ({ type: "task.completed" } & NotificationTarget)+ | ({ type: "task.failed"; errorText?: string } & NotificationTarget)+ | ({ type: "permission.asked"; detail?: string } & NotificationTarget)+ | ({ type: "question.asked"; question?: string } & NotificationTarget);++/** Sent by the desktop shell when a notification is clicked: open this chat. */+export const NOTIFICATION_OPEN_SESSION_EVENT = "omnirush:notification:open-session";++/** The route of the chat a notification is about. */+export function notificationSessionRoute(target: NotificationTarget): string {+ return target.workspaceId?.trim()+ ? workspaceSessionRoute(target.workspaceId, target.sessionId)+ : legacySessionRoute(target.sessionId);+}type NotificationCopy = {title: string;@@ -94,12 +108,14 @@ export function notifyDesktopEvent(event: DesktopNotificationEvent): void {if (isAppInView()) return;if (!isDesktopRuntime()) {- void webNotificationHandler?.(copy.title, copy.body).catch(() => undefined);+ void webNotificationHandler?.(copy.title, copy.body, notificationSessionRoute(event)).catch(() => undefined);return;}void desktopNotificationShow({title: copy.title,body: copy.body,+ sessionId: event.sessionId,+ ...(event.workspaceId ? { workspaceId: event.workspaceId } : {}),}).catch(() => undefined);}diff --git a/apps/app/tests/desktop-notifications.test.ts b/apps/app/tests/desktop-notifications.test.tsindex d7668678..afad9907 100644--- a/apps/app/tests/desktop-notifications.test.ts+++ b/apps/app/tests/desktop-notifications.test.ts@@ -1,7 +1,8 @@import { beforeEach, describe, expect, test } from "bun:test";import { LOCAL_PREFERENCES_KEY } from "../src/react-app/kernel/local-preferences-storage";-import { notifyDesktopEvent } from "../src/react-app/shell/desktop-notifications";+import { DEFAULT_DESKTOP_NOTIFICATION_PREFERENCE } from "../src/react-app/kernel/desktop-notification-preferences";+import { notificationSessionRoute, notifyDesktopEvent } from "../src/react-app/shell/desktop-notifications";type DesktopCall = { command: string; args: unknown[] };@@ -92,6 +93,27 @@ describe("desktop notifications", () => {});});+ test("by default finished tasks and approvals notify, naming the chat to open on click", async () => {+ expect(DEFAULT_DESKTOP_NOTIFICATION_PREFERENCE).toBe("all");++ notifyDesktopEvent({ type: "task.completed", sessionId: "session-a", workspaceId: "ws_1" });+ notifyDesktopEvent({ type: "permission.asked", sessionId: "session-b", workspaceId: "ws_1", detail: "git push" });+ notifyDesktopEvent({ type: "question.asked", sessionId: "session-c" });+ await Promise.resolve();++ expect(calls.map((call) => call.args[0])).toEqual([+ { title: "Task completed", body: "The session finished running.", sessionId: "session-a", workspaceId: "ws_1" },+ { title: "Permission needed", body: "git push", sessionId: "session-b", workspaceId: "ws_1" },+ { title: "Question needs your answer", body: "A session is waiting for your answer.", sessionId: "session-c" },+ ]);+ });++ test("a clicked notification opens its chat's route", () => {+ expect(notificationSessionRoute({ sessionId: "ses 1", workspaceId: "ws_1" })).toBe("/workspace/ws_1/session/ses%201");+ expect(notificationSessionRoute({ sessionId: "ses_2" })).toBe("/session/ses_2");+ expect(notificationSessionRoute({ sessionId: "ses_3", workspaceId: " " })).toBe("/session/ses_3");+ });+test("focused app suppresses native popups", () => {setPreference("all");installRuntime({ focused: true });diff --git a/apps/desktop/electron/main.mjs b/apps/desktop/electron/main.mjsindex f3094a18..90a4ca7e 100644--- a/apps/desktop/electron/main.mjs+++ b/apps/desktop/electron/main.mjs@@ -710,12 +710,30 @@ async function applyDefaultAppIconImage(expectedSequence = null) {});}-async function focusMainWindowFromNotification() {+/** Sent to the renderer when a notification about a chat is clicked (desktop-notifications.ts). */+const NOTIFICATION_OPEN_SESSION_EVENT = "omnirush:notification:open-session";++/**+ * The chat a notification is about, from its input; null when it names none.+ * @param {Record<string, unknown>} record+ * @returns {import("@omnirush/types/desktop-ipc").DesktopNotificationOpenSession | null}+ */+function notificationSessionTarget(record) {+ const sessionId = typeof record.sessionId === "string" ? record.sessionId.trim() : "";+ if (!sessionId) return null;+ const workspaceId = typeof record.workspaceId === "string" ? record.workspaceId.trim() : "";+ return workspaceId ? { sessionId, workspaceId } : { sessionId };+}++/** @param {import("@omnirush/types/desktop-ipc").DesktopNotificationOpenSession | null} [target] */+async function focusMainWindowFromNotification(target = null) {const win = await createMainWindow();if (win.isDestroyed()) return;if (win.isMinimized()) win.restore();win.show();win.focus();+ // The renderer opens the chat the notification was about.+ if (target && !win.webContents.isDestroyed()) win.webContents.send(NOTIFICATION_OPEN_SESSION_EVENT, target);}/**@@ -734,6 +752,7 @@ function showDesktopNotification(input) {}const body = String(Reflect.get(record, "body") ?? "").trim();+ const target = notificationSessionTarget(record);const icon = resolveBrandIconImage() ?? APP_ICON_IMAGE;const options = {title,@@ -745,7 +764,7 @@ function showDesktopNotification(input) {try {const notification = new ElectronNotification(options);notification.on("click", () => {- runDetachedTask("focus window from notification", focusMainWindowFromNotification);+ runDetachedTask("focus window from notification", () => focusMainWindowFromNotification(target));});notification.show();return { ok: true };diff --git a/apps/desktop/electron/preload.mjs b/apps/desktop/electron/preload.mjsindex d50b5f5c..500dae09 100644--- a/apps/desktop/electron/preload.mjs+++ b/apps/desktop/electron/preload.mjs@@ -369,6 +369,12 @@ ipcRenderer.on("omnirush:feedback:event", (_event, detail) => {window.dispatchEvent(new CustomEvent("omnirush:feedback:event", { detail }));});+// A clicked notification about a chat (main.mjs showDesktopNotification): open that chat.+ipcRenderer.on("omnirush:notification:open-session", (_event, detail) => {+ if (typeof window === "undefined") return;+ window.dispatchEvent(new CustomEvent("omnirush:notification:open-session", { detail }));+});+ipcRenderer.on(NATIVE_MENU_ZOOM_EVENT, (_event, action) => {if (typeof window === "undefined") return;window.dispatchEvent(new CustomEvent(NATIVE_MENU_ZOOM_EVENT, { detail: action }));diff --git a/packages/types/src/desktop-ipc.ts b/packages/types/src/desktop-ipc.tsindex 94c630cb..eb4bc58d 100644--- a/packages/types/src/desktop-ipc.ts+++ b/packages/types/src/desktop-ipc.ts@@ -56,8 +56,14 @@ export type DesktopNotificationInput = {body?: string;href?: string;silent?: boolean;+ /** The chat the notification is about: a click focuses the window and opens it. */+ sessionId?: string;+ workspaceId?: string;};+/** Sent to the renderer when a notification about a chat is clicked. */+export type DesktopNotificationOpenSession = { sessionId: string; workspaceId?: string };+export type DesktopNotificationResult =| { ok: true }| { ok: false; reason: string };