seconohe.comfy_notification

 1# Copyright (c) 2025 Salvador E. Tropea
 2# Copyright (c) 2025 Instituto Nacional de TecnologĂ­a Industrial
 3# License: GPLv3
 4# Project: SeCoNoHe
 5#
 6# ComfyUI Toast API messages
 7# Original code from Gemini 2.5 Pro, which was really outdated
 8# Took ideas from Easy Use nodes and looking at ComfyUI code
 9import logging
10from typing import Optional, Literal
11# ComfyUI imports
12try:
13    from server import PromptServer
14    with_comfy = True
15except Exception:
16    with_comfy = False
17
18_EVENT_NAME = "seconohe-toast"
19ToastSeverity = Literal['success', 'info', 'warn', 'error', 'secondary', 'contrast']
20
21
22def send_toast_notification(logger: logging.Logger, message: str, summary: str = "Warning", severity: ToastSeverity = "warn",
23                            sid: Optional[str] = None) -> None:
24    """
25    Sends a toast notification event to the ComfyUI web client via WebSocket.
26
27    This function communicates with the frontend JavaScript listener for the
28    ``seconohe-toast`` event, triggering a UI notification. It will fail silently
29    if not run within a ComfyUI server environment.
30
31    :param logger: The logger instance to use for reporting errors if the send fails.
32    :type logger: logging.Logger
33    :param message: The main detail message to display in the toast.
34    :type message: str
35    :param summary: A short, bolded summary or title for the toast. Defaults to 'Warning'.
36    :type summary: str
37    :param severity: The style/color of the toast. Must be one of 'success',
38                     'info', 'warn', 'error', 'secondary', or 'contrast'.
39                     Defaults to 'warn'.
40    :type severity: Literal['success', 'info', 'warn', 'error', 'secondary', 'contrast']
41    :param sid: The session ID of a specific client to send the message to.
42                If ``None``, the message is broadcast to all connected clients.
43                Defaults to ``None``.
44    :type sid: Optional[str]
45    """
46    # Check if we are in a ComfyUI environment and the server instance is available
47    if not with_comfy or not hasattr(PromptServer, 'instance') or PromptServer.instance is None:
48        logger.debug("Toast notification skipped: Not running in an active ComfyUI server environment.")
49        return
50    try:
51        # Use the PromptServer instance to send a custom event to the frontend
52        PromptServer.instance.send_sync(
53            _EVENT_NAME,  # The custom event name the JS is listening for
54            {
55                'message': message,
56                'summary': summary,
57                'severity': severity
58            },
59            sid
60        )
61    except Exception as e:
62        logger.error(f"Failed to send toast notification via ComfyUI PromptServer: {e}")
ToastSeverity = typing.Literal['success', 'info', 'warn', 'error', 'secondary', 'contrast']
def send_toast_notification( logger: logging.Logger, message: str, summary: str = 'Warning', severity: Literal['success', 'info', 'warn', 'error', 'secondary', 'contrast'] = 'warn', sid: Optional[str] = None) -> None:
23def send_toast_notification(logger: logging.Logger, message: str, summary: str = "Warning", severity: ToastSeverity = "warn",
24                            sid: Optional[str] = None) -> None:
25    """
26    Sends a toast notification event to the ComfyUI web client via WebSocket.
27
28    This function communicates with the frontend JavaScript listener for the
29    ``seconohe-toast`` event, triggering a UI notification. It will fail silently
30    if not run within a ComfyUI server environment.
31
32    :param logger: The logger instance to use for reporting errors if the send fails.
33    :type logger: logging.Logger
34    :param message: The main detail message to display in the toast.
35    :type message: str
36    :param summary: A short, bolded summary or title for the toast. Defaults to 'Warning'.
37    :type summary: str
38    :param severity: The style/color of the toast. Must be one of 'success',
39                     'info', 'warn', 'error', 'secondary', or 'contrast'.
40                     Defaults to 'warn'.
41    :type severity: Literal['success', 'info', 'warn', 'error', 'secondary', 'contrast']
42    :param sid: The session ID of a specific client to send the message to.
43                If ``None``, the message is broadcast to all connected clients.
44                Defaults to ``None``.
45    :type sid: Optional[str]
46    """
47    # Check if we are in a ComfyUI environment and the server instance is available
48    if not with_comfy or not hasattr(PromptServer, 'instance') or PromptServer.instance is None:
49        logger.debug("Toast notification skipped: Not running in an active ComfyUI server environment.")
50        return
51    try:
52        # Use the PromptServer instance to send a custom event to the frontend
53        PromptServer.instance.send_sync(
54            _EVENT_NAME,  # The custom event name the JS is listening for
55            {
56                'message': message,
57                'summary': summary,
58                'severity': severity
59            },
60            sid
61        )
62    except Exception as e:
63        logger.error(f"Failed to send toast notification via ComfyUI PromptServer: {e}")

Sends a toast notification event to the ComfyUI web client via WebSocket.

This function communicates with the frontend JavaScript listener for the seconohe-toast event, triggering a UI notification. It will fail silently if not run within a ComfyUI server environment.

Parameters
  • logger: The logger instance to use for reporting errors if the send fails.
  • message: The main detail message to display in the toast.
  • summary: A short, bolded summary or title for the toast. Defaults to 'Warning'.
  • severity: The style/color of the toast. Must be one of 'success', 'info', 'warn', 'error', 'secondary', or 'contrast'. Defaults to 'warn'.
  • sid: The session ID of a specific client to send the message to. If None, the message is broadcast to all connected clients. Defaults to None.