Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
feat: add browser notification for task completion
Generdf20c710fcfd
diff --git a/src/shared/src/types.ts b/src/shared/src/types.tsindex 6cc363eec..ba80a0318 100644--- a/src/shared/src/types.ts+++ b/src/shared/src/types.ts@@ -198,7 +198,7 @@ export type WsMessage =| { type: "runtime.registered"; daemonId: string; hostname: string; workspaceId: string }| { type: "runtime.status"; daemonId: string; workspaceId: string; status: string }| { type: "runtime.deleted"; daemonId: string }- | { type: "task.updated"; taskId: string; status: string }+ | { type: "task.updated"; taskId: string; agentId: string; status: string }| { type: "task.messages"; taskId: string; messages: TaskMessage[] }| { type: "email.received"; agentId: string }| { type: "artifact.uploaded"; conversationId: string; artifact: Artifact }diff --git a/src/web/src/app/(app)/w/[slug]/settings/notification-tab.tsx b/src/web/src/app/(app)/w/[slug]/settings/notification-tab.tsxnew file mode 100644index 000000000..0dd3f5c5a--- /dev/null+++ b/src/web/src/app/(app)/w/[slug]/settings/notification-tab.tsx@@ -0,0 +1,109 @@+"use client";++import { useEffect, useState } from "react";+import { toast } from "sonner";+import { Checkbox } from "@/components/ui/checkbox";+import { Switch } from "@/components/ui/switch";+import {+ isNotificationSupported,+ getNotificationEnabled,+ setNotificationEnabled,+ getNotificationEvents,+ setNotificationEvents,+ requestNotificationPermission,+ NOTIFICATION_EVENTS,+ NOTIFICATION_EVENT_LABELS,+ type NotificationEvent,+} from "@/lib/browser-notification";++export function NotificationTab() {+ const [notifEnabled, setNotifEnabled] = useState(false);+ const [notifEvents, setNotifEvents] = useState<NotificationEvent[]>([...NOTIFICATION_EVENTS]);+ const [notifSupported, setNotifSupported] = useState(true);+ const [notifDenied, setNotifDenied] = useState(false);++ useEffect(() => {+ setNotifSupported(isNotificationSupported());+ setNotifEnabled(getNotificationEnabled());+ setNotifEvents(getNotificationEvents());+ if (isNotificationSupported()) {+ setNotifDenied(Notification.permission === "denied");+ }+ }, []);++ const handleToggleNotification = async (checked: boolean) => {+ if (!checked) {+ setNotifEnabled(false);+ setNotificationEnabled(false);+ return;+ }+ const granted = await requestNotificationPermission();+ if (granted) {+ setNotifEnabled(true);+ setNotificationEnabled(true);+ } else {+ setNotifDenied(true);+ toast.error("Notification permission denied. Please enable it in browser settings.");+ }+ };++ const handleToggleEvent = (event: NotificationEvent) => {+ setNotifEvents((prev) => {+ const next = prev.includes(event)+ ? prev.filter((e) => e !== event)+ : [...prev, event];+ setNotificationEvents(next);+ return next;+ });+ };++ if (!notifSupported) {+ return (+ <p className="text-sm text-muted-foreground">+ Your browser does not support notifications.+ </p>+ );+ }++ return (+ <div className="space-y-6">+ <section className="space-y-4">+ <h2 className="text-sm font-medium">Browser Notifications</h2>+ <div className="space-y-3">+ <div className="flex items-center justify-between">+ <div className="space-y-0.5">+ <p className="text-sm">Enable notifications</p>+ <p className="text-xs text-muted-foreground">+ Get notified when tasks finish while the tab is in the background+ </p>+ </div>+ <Switch+ checked={notifEnabled}+ onCheckedChange={handleToggleNotification}+ disabled={notifDenied && !notifEnabled}+ />+ </div>+ {notifDenied && !notifEnabled && (+ <p className="text-xs text-destructive">+ Notification permission was denied. Please allow it in your browser settings.+ </p>+ )}+ {notifEnabled && (+ <div className="space-y-2 pl-0.5">+ <p className="text-xs text-muted-foreground">Notify me when:</p>+ {NOTIFICATION_EVENTS.map((event) => (+ <label key={event} className="flex items-center gap-2 text-sm cursor-pointer select-none">+ <Checkbox+ checked={notifEvents.includes(event)}+ onCheckedChange={() => handleToggleEvent(event)}+ />+ {NOTIFICATION_EVENT_LABELS[event]}+ </label>+ ))}+ </div>+ )}+ </div>+ </section>+ </div>+ );+}diff --git a/src/web/src/app/(app)/w/[slug]/settings/page.tsx b/src/web/src/app/(app)/w/[slug]/settings/page.tsxindex 234a5f55c..120a297aa 100644--- a/src/web/src/app/(app)/w/[slug]/settings/page.tsx+++ b/src/web/src/app/(app)/w/[slug]/settings/page.tsx@@ -7,10 +7,12 @@ import { cn } from "@/lib/utils";import { GeneralTab } from "./general-tab";import { InstructionTab } from "./instruction-tab";import { MembersTab } from "./members-tab";+import { NotificationTab } from "./notification-tab";const TABS = [{ id: "general", label: "General" },{ id: "instruction", label: "Instruction" },+ { id: "notifications", label: "Notifications" },{ id: "members", label: "Members" },] as const;@@ -68,6 +70,7 @@ export default function SettingsPage() {) : (<div className="mx-auto max-w-md">{activeTab === "general" && <GeneralTab />}+ {activeTab === "notifications" && <NotificationTab />}{activeTab === "members" && <MembersTab />}</div>)}diff --git a/src/web/src/app/api/conversations/[id]/active-task/route.ts b/src/web/src/app/api/conversations/[id]/active-task/route.tsindex 6a3714510..ddaeb3e0a 100644--- a/src/web/src/app/api/conversations/[id]/active-task/route.ts+++ b/src/web/src/app/api/conversations/[id]/active-task/route.ts@@ -59,6 +59,7 @@ export const DELETE = withAuth(async (req, ctx) => {broadcastToUser(ctx.userId, {type: "task.updated",taskId: cancelled.id,+ agentId: cancelled.agentId,status: "cancelled",}).catch(() => {});diff --git a/src/web/src/app/api/conversations/[id]/messages/route.ts b/src/web/src/app/api/conversations/[id]/messages/route.tsindex 346410940..eecd6c734 100644--- a/src/web/src/app/api/conversations/[id]/messages/route.ts+++ b/src/web/src/app/api/conversations/[id]/messages/route.ts@@ -162,7 +162,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {context: artifactIds.length > 0 ? { attachment_ids: artifactIds } : undefined,},);- broadcastToUser(ctx.userId, { type: "task.updated", taskId: task.id, status: "queued" }).catch(() => {});+ broadcastToUser(ctx.userId, { type: "task.updated", taskId: task.id, agentId: task.agentId, status: "queued" }).catch(() => {});return writeJSON({ message: messageToResponse(message), task: taskToResponse(task) },201diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts b/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.tsindex 05728c9bd..6592be8f3 100644--- a/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts+++ b/src/web/src/app/api/daemon/tasks/[taskId]/complete/route.ts@@ -35,7 +35,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {result,sessionId);- broadcastToUser(ctx.userId, { type: "task.updated", taskId, status: "completed" }).catch(() => {});+ broadcastToUser(ctx.userId, { type: "task.updated", taskId, agentId: task.agentId, status: "completed" }).catch(() => {});return writeJSON(taskToResponse(task));} catch (e: unknown) {return writeError(e instanceof Error ? e.message : "Unknown error", 400);diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts b/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.tsindex a5e6bad56..641c66359 100644--- a/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts+++ b/src/web/src/app/api/daemon/tasks/[taskId]/fail/route.ts@@ -27,7 +27,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {const taskService = new TaskService(db);try {const task = await taskService.failTask(taskId, ctx.workspaceId, body.error);- broadcastToUser(ctx.userId, { type: "task.updated", taskId, status: "failed" }).catch(() => {});+ broadcastToUser(ctx.userId, { type: "task.updated", taskId, agentId: task.agentId, status: "failed" }).catch(() => {});return writeJSON(taskToResponse(task));} catch (e: unknown) {return writeError(e instanceof Error ? e.message : "Unknown error", 400);diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/supersede/route.ts b/src/web/src/app/api/daemon/tasks/[taskId]/supersede/route.tsindex a5e09cc48..97b9b1efb 100644--- a/src/web/src/app/api/daemon/tasks/[taskId]/supersede/route.ts+++ b/src/web/src/app/api/daemon/tasks/[taskId]/supersede/route.ts@@ -23,7 +23,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {const taskService = new TaskService(db);try {const task = await taskService.supersedeTask(taskId, ctx.workspaceId);- broadcastToUser(ctx.userId, { type: "task.updated", taskId, status: "superseded" }).catch(() => {});+ broadcastToUser(ctx.userId, { type: "task.updated", taskId, agentId: task.agentId, status: "superseded" }).catch(() => {});return writeJSON(taskToResponse(task));} catch (e: unknown) {return writeError(e instanceof Error ? e.message : "Unknown error", 400);diff --git a/src/web/src/components/ui/checkbox.tsx b/src/web/src/components/ui/checkbox.tsxnew file mode 100644index 000000000..4fcd84785--- /dev/null+++ b/src/web/src/components/ui/checkbox.tsx@@ -0,0 +1,29 @@+"use client"++import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox"++import { cn } from "@/lib/utils"+import { CheckIcon } from "lucide-react"++function Checkbox({ className, ...props }: CheckboxPrimitive.Root.Props) {+ return (+ <CheckboxPrimitive.Root+ data-slot="checkbox"+ className={cn(+ "peer relative flex size-4 shrink-0 items-center justify-center rounded-[4px] border border-input transition-colors outline-none group-has-disabled/field:opacity-50 after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary",+ className+ )}+ {...props}+ >+ <CheckboxPrimitive.Indicator+ data-slot="checkbox-indicator"+ className="grid place-content-center text-current transition-none [&>svg]:size-3.5"+ >+ <CheckIcon+ />+ </CheckboxPrimitive.Indicator>+ </CheckboxPrimitive.Root>+ )+}++export { Checkbox }diff --git a/src/web/src/components/ui/switch.tsx b/src/web/src/components/ui/switch.tsxnew file mode 100644index 000000000..9b8b44b1d--- /dev/null+++ b/src/web/src/components/ui/switch.tsx@@ -0,0 +1,32 @@+"use client"++import { Switch as SwitchPrimitive } from "@base-ui/react/switch"++import { cn } from "@/lib/utils"++function Switch({+ className,+ size = "default",+ ...props+}: SwitchPrimitive.Root.Props & {+ size?: "sm" | "default"+}) {+ return (+ <SwitchPrimitive.Root+ data-slot="switch"+ data-size={size}+ className={cn(+ "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:bg-primary data-unchecked:bg-input dark:data-unchecked:bg-input/80 data-disabled:cursor-not-allowed data-disabled:opacity-50",+ className+ )}+ {...props}+ >+ <SwitchPrimitive.Thumb+ data-slot="switch-thumb"+ className="pointer-events-none block rounded-full bg-background ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] dark:data-checked:bg-primary-foreground group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 dark:data-unchecked:bg-foreground"+ />+ </SwitchPrimitive.Root>+ )+}++export { Switch }diff --git a/src/web/src/contexts/agent-context.tsx b/src/web/src/contexts/agent-context.tsxindex 76758072b..0cd184e22 100644--- a/src/web/src/contexts/agent-context.tsx+++ b/src/web/src/contexts/agent-context.tsx@@ -32,6 +32,7 @@ import type {WsMessage,} from "@alook/shared";import { useUserWs } from "@/lib/use-user-ws";+import { sendTaskNotification } from "@/lib/browser-notification";type WsSubscriber = (msg: WsMessage) => void;@@ -164,10 +165,14 @@ export function AgentProvider({break;case "task.updated":fetchTaskCounts();+ if (msg.status === "completed" || msg.status === "failed") {+ const agent = agents.find((a) => a.id === msg.agentId);+ sendTaskNotification(msg.status, agent?.name);+ }break;}},- [reload, debouncedReload, fetchTaskCounts, workspaceId]+ [reload, debouncedReload, fetchTaskCounts, workspaceId, agents]);useUserWs(handleWsMessage);diff --git a/src/web/src/lib/browser-notification.ts b/src/web/src/lib/browser-notification.tsnew file mode 100644index 000000000..cd7313b70--- /dev/null+++ b/src/web/src/lib/browser-notification.ts@@ -0,0 +1,65 @@+const STORAGE_KEY = "browser-notification-enabled";+const EVENTS_KEY = "browser-notification-events";++export const NOTIFICATION_EVENTS = ["completed", "failed"] as const;+export type NotificationEvent = (typeof NOTIFICATION_EVENTS)[number];++export const NOTIFICATION_EVENT_LABELS: Record<NotificationEvent, string> = {+ completed: "Task Completed",+ failed: "Task Failed",+};++export function isNotificationSupported(): boolean {+ return typeof window !== "undefined" && "Notification" in window;+}++export function getNotificationEnabled(): boolean {+ if (typeof window === "undefined") return false;+ return localStorage.getItem(STORAGE_KEY) === "true";+}++export function setNotificationEnabled(enabled: boolean): void {+ localStorage.setItem(STORAGE_KEY, String(enabled));+}++export function getNotificationEvents(): NotificationEvent[] {+ if (typeof window === "undefined") return [...NOTIFICATION_EVENTS];+ try {+ const raw = localStorage.getItem(EVENTS_KEY);+ if (raw) return JSON.parse(raw) as NotificationEvent[];+ } catch {}+ return [...NOTIFICATION_EVENTS];+}++export function setNotificationEvents(events: NotificationEvent[]): void {+ localStorage.setItem(EVENTS_KEY, JSON.stringify(events));+}++export async function requestNotificationPermission(): Promise<boolean> {+ if (!isNotificationSupported()) return false;+ if (Notification.permission === "granted") return true;+ if (Notification.permission === "denied") return false;+ const result = await Notification.requestPermission();+ return result === "granted";+}++export function sendTaskNotification(+ status: string,+ agentName?: string,+ body?: string,+): void {+ if (typeof window === "undefined") return;+ if (!isNotificationSupported()) return;+ if (Notification.permission !== "granted") return;+ if (!getNotificationEnabled()) return;++ const events = getNotificationEvents();+ if (!events.includes(status as NotificationEvent)) return;++ const label = NOTIFICATION_EVENT_LABELS[status as NotificationEvent] ?? `Task ${status}`;+ const title = agentName ? `${agentName} — ${label}` : label;+ new Notification(title, {+ body: body ?? "",+ icon: "/alook.svg",+ });+}