diff --git a/python/api/notification_create.py b/python/api/notification_create.py index d76352fea..6699fd258 100644 --- a/python/api/notification_create.py +++ b/python/api/notification_create.py @@ -1,6 +1,6 @@ from python.helpers.api import ApiHandler from flask import Request, Response -from python.helpers.notification import AgentNotification, NotificationType +from python.helpers.notification import NotificationManager, NotificationPriority, NotificationType class NotificationCreate(ApiHandler): @@ -10,7 +10,8 @@ class NotificationCreate(ApiHandler): async def process(self, input: dict, request: Request) -> dict | Response: # Extract notification data - notification_type = input.get("type", "info") + notification_type = input.get("type", NotificationType.INFO.value) + priority = input.get("priority", NotificationPriority.NORMAL.value) message = input.get("message", "") title = input.get("title", "") detail = input.get("detail", "") @@ -34,31 +35,31 @@ class NotificationCreate(ApiHandler): if isinstance(notification_type, str): notification_type = NotificationType(notification_type.lower()) except ValueError: - return {"success": False, "error": f"Invalid notification type: {notification_type}"} + return { + "success": False, + "error": f"Invalid notification type: {notification_type}", + } # Create notification using the appropriate helper method try: - if notification_type == NotificationType.INFO: - notification = AgentNotification.info(message, title, detail, display_time, group) - elif notification_type == NotificationType.SUCCESS: - notification = AgentNotification.success(message, title, detail, display_time, group) - elif notification_type == NotificationType.WARNING: - notification = AgentNotification.warning(message, title, detail, display_time, group) - elif notification_type == NotificationType.ERROR: - notification = AgentNotification.error(message, title, detail, display_time, group) - elif notification_type == NotificationType.PROGRESS: - notification = AgentNotification.progress(message, title, detail, display_time, group) - else: - notification = AgentNotification.info(message, title, detail, display_time, group) + notification = NotificationManager.send_notification( + notification_type, + priority, + message, + title, + detail, + display_time, + group, + ) return { "success": True, "notification_id": notification.id, - "message": "Notification created successfully" + "message": "Notification created successfully", } except Exception as e: return { "success": False, - "error": f"Failed to create notification: {str(e)}" + "error": f"Failed to create notification: {str(e)}", } diff --git a/python/api/notifications_clear.py b/python/api/notifications_clear.py new file mode 100644 index 000000000..f8e6a0520 --- /dev/null +++ b/python/api/notifications_clear.py @@ -0,0 +1,18 @@ +from python.helpers.api import ApiHandler +from flask import Request, Response +from agent import AgentContext + + +class NotificationsClear(ApiHandler): + @classmethod + def requires_auth(cls) -> bool: + return True + + async def process(self, input: dict, request: Request) -> dict | Response: + # Get the global notification manager + notification_manager = AgentContext.get_notification_manager() + + # Clear all notifications + notification_manager.clear_all() + + return {"success": True, "message": "All notifications cleared"} diff --git a/python/helpers/notification.py b/python/helpers/notification.py index 528d55f3a..1aefff7b4 100644 --- a/python/helpers/notification.py +++ b/python/helpers/notification.py @@ -11,12 +11,17 @@ class NotificationType(Enum): ERROR = "error" PROGRESS = "progress" +class NotificationPriority(Enum): + NORMAL = 10 + HIGH = 20 + @dataclass class NotificationItem: manager: "NotificationManager" no: int type: NotificationType + priority: NotificationPriority title: str message: str detail: str # HTML content for expandable details @@ -41,7 +46,8 @@ class NotificationItem: return { "no": self.no, "id": self.id, - "type": self.type.value, + "type": self.type.value if isinstance(self.type, NotificationType) else self.type, + "priority": self.priority.value if isinstance(self.priority, NotificationPriority) else self.priority, "title": self.title, "message": self.message, "detail": self.detail, @@ -59,9 +65,25 @@ class NotificationManager: self.notifications: list[NotificationItem] = [] self.max_notifications = max_notifications + @staticmethod + def send_notification( + type: NotificationType, + priority: NotificationPriority, + message: str, + title: str = "", + detail: str = "", + display_time: int = 3, + group: str = "", + ) -> NotificationItem: + from agent import AgentContext + return AgentContext.get_notification_manager().add_notification( + type, priority, message, title, detail, display_time, group + ) + def add_notification( self, type: NotificationType, + priority: NotificationPriority, message: str, title: str = "", detail: str = "", @@ -72,7 +94,8 @@ class NotificationManager: item = NotificationItem( manager=self, no=len(self.notifications), - type=type, + type=NotificationType(type), + priority=NotificationPriority(priority), title=title, message=message, detail=detail, @@ -138,41 +161,4 @@ class NotificationManager: self.guid = str(uuid.uuid4()) def get_notifications_by_type(self, type: NotificationType) -> list[NotificationItem]: - return [n for n in self.notifications if n.type == type] - - -class AgentNotification: - @staticmethod - def info(message: str, title: str = "", detail: str = "", display_time: int = 3, group: str = "") -> NotificationItem: - from agent import AgentContext - return AgentContext.get_notification_manager().add_notification( - NotificationType.INFO, message, title, detail, display_time, group - ) - - @staticmethod - def success(message: str, title: str = "", detail: str = "", display_time: int = 3, group: str = "") -> NotificationItem: - from agent import AgentContext - return AgentContext.get_notification_manager().add_notification( - NotificationType.SUCCESS, message, title, detail, display_time, group - ) - - @staticmethod - def warning(message: str, title: str = "", detail: str = "", display_time: int = 3, group: str = "") -> NotificationItem: - from agent import AgentContext - return AgentContext.get_notification_manager().add_notification( - NotificationType.WARNING, message, title, detail, display_time, group - ) - - @staticmethod - def error(message: str, title: str = "", detail: str = "", display_time: int = 3, group: str = "") -> NotificationItem: - from agent import AgentContext - return AgentContext.get_notification_manager().add_notification( - NotificationType.ERROR, message, title, detail, display_time, group - ) - - @staticmethod - def progress(message: str, title: str = "", detail: str = "", display_time: int = 3, group: str = "") -> NotificationItem: - from agent import AgentContext - return AgentContext.get_notification_manager().add_notification( - NotificationType.PROGRESS, message, title, detail, display_time, group - ) + return [n for n in self.notifications if n.type == type] \ No newline at end of file diff --git a/python/tools/notify_user.py b/python/tools/notify_user.py index 487c501ca..b436cffde 100644 --- a/python/tools/notify_user.py +++ b/python/tools/notify_user.py @@ -1,6 +1,6 @@ from python.helpers.tool import Tool, Response from agent import AgentContext - +from python.helpers.notification import NotificationPriority, NotificationType class NotifyUserTool(Tool): @@ -9,11 +9,20 @@ class NotifyUserTool(Tool): message = self.args.get("message", "") title = self.args.get("title", "") detail = self.args.get("detail", "") - notification_type = self.args.get("type", "info") + notification_type = self.args.get("type", NotificationType.INFO) + priority = self.args.get("priority", NotificationPriority.HIGH) # by default, agents should notify with high priority + timeout = int(self.args.get("timeout", 30)) # agent's notifications should have longer timeouts - if notification_type not in ["info", "success", "warning", "error", "progress"]: + try: + notification_type = NotificationType(notification_type) + except ValueError: return Response(message=f"Invalid notification type: {notification_type}", break_loop=False) + try: + priority = NotificationPriority(priority) + except ValueError: + return Response(message=f"Invalid notification priority: {priority}", break_loop=False) + if not message: return Response(message="Message is required", break_loop=False) @@ -22,5 +31,7 @@ class NotifyUserTool(Tool): title=title, detail=detail, type=notification_type, + priority=priority, + display_time=timeout, ) return Response(message=self.agent.read_prompt("fw.notify_user.notification_sent.md"), break_loop=False) diff --git a/webui/components/notifications/notification-icons.html b/webui/components/notifications/notification-icons.html index 918cd885f..2beb22e5f 100644 --- a/webui/components/notifications/notification-icons.html +++ b/webui/components/notifications/notification-icons.html @@ -10,7 +10,7 @@