ws_monitor.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/env python3
  2. """
  3. ws_monitor.py — Real-time ComfyUI WebSocket monitor.
  4. Connects to /ws and pretty-prints execution events: node start/finish, sampling
  5. progress, cached nodes, errors. Optionally writes preview frames to disk.
  6. Useful for:
  7. - Watching a long-running job in real time without parsing JSON yourself
  8. - Saving in-progress preview frames for video / animation workflows
  9. - Debugging "why is this hanging?" — see exactly which node is stuck
  10. Usage:
  11. # Local — watch all jobs from this client_id
  12. python3 ws_monitor.py
  13. # Cloud — watch a specific prompt_id
  14. python3 ws_monitor.py --host https://cloud.comfy.org \
  15. --prompt-id abc-123-def
  16. # Save preview frames to ./previews/
  17. python3 ws_monitor.py --previews ./previews
  18. Requires: websocket-client (`pip install websocket-client`).
  19. Falls back to a clear error message when not installed.
  20. """
  21. from __future__ import annotations
  22. import argparse
  23. import json
  24. import struct
  25. import sys
  26. from pathlib import Path
  27. from urllib.parse import urlparse
  28. sys.path.insert(0, str(Path(__file__).resolve().parent))
  29. from _common import (
  30. DEFAULT_LOCAL_HOST, ENV_API_KEY, log, new_client_id, resolve_api_key, is_cloud_host,
  31. )
  32. # Binary frame types from ComfyUI WebSocket protocol
  33. BINARY_PREVIEW_IMAGE = 1
  34. BINARY_TEXT = 3
  35. BINARY_PREVIEW_IMAGE_WITH_METADATA = 4
  36. # Image type codes inside PREVIEW_IMAGE
  37. IMAGE_TYPE_JPEG = 1
  38. IMAGE_TYPE_PNG = 2
  39. # ANSI escape codes (works on most modern terminals)
  40. RESET = "\033[0m"
  41. DIM = "\033[2m"
  42. BOLD = "\033[1m"
  43. GREEN = "\033[32m"
  44. YELLOW = "\033[33m"
  45. RED = "\033[31m"
  46. CYAN = "\033[36m"
  47. def fmt_color(s: str, color: str, *, color_on: bool = True) -> str:
  48. return f"{color}{s}{RESET}" if color_on else s
  49. def parse_binary_frame(data: bytes) -> dict | None:
  50. if len(data) < 8:
  51. return None
  52. type_code = struct.unpack(">I", data[0:4])[0]
  53. if type_code == BINARY_PREVIEW_IMAGE:
  54. image_type = struct.unpack(">I", data[4:8])[0]
  55. ext = "jpg" if image_type == IMAGE_TYPE_JPEG else "png" if image_type == IMAGE_TYPE_PNG else "bin"
  56. return {
  57. "kind": "preview",
  58. "image_type": image_type,
  59. "ext": ext,
  60. "image_bytes": data[8:],
  61. }
  62. if type_code == BINARY_PREVIEW_IMAGE_WITH_METADATA:
  63. if len(data) < 12:
  64. return None
  65. meta_len = struct.unpack(">I", data[4:8])[0]
  66. meta_end = 8 + meta_len
  67. if len(data) < meta_end:
  68. return None
  69. try:
  70. meta = json.loads(data[8:meta_end].decode("utf-8"))
  71. except Exception:
  72. meta = {"raw": data[8:meta_end][:200].decode("utf-8", "replace")}
  73. return {
  74. "kind": "preview_with_metadata",
  75. "metadata": meta,
  76. "image_bytes": data[meta_end:],
  77. "ext": "png",
  78. }
  79. if type_code == BINARY_TEXT:
  80. if len(data) < 8:
  81. return None
  82. nid_len = struct.unpack(">I", data[4:8])[0]
  83. nid_end = 8 + nid_len
  84. if len(data) < nid_end:
  85. return None
  86. return {
  87. "kind": "text",
  88. "node_id": data[8:nid_end].decode("utf-8", "replace"),
  89. "text": data[nid_end:].decode("utf-8", "replace"),
  90. }
  91. return {"kind": "unknown", "type_code": type_code, "size": len(data)}
  92. def main(argv: list[str] | None = None) -> int:
  93. p = argparse.ArgumentParser(description="Real-time ComfyUI WebSocket monitor")
  94. p.add_argument("--host", default=DEFAULT_LOCAL_HOST, help="ComfyUI server URL")
  95. p.add_argument("--api-key", help=f"API key for cloud (or set ${ENV_API_KEY} env var)")
  96. p.add_argument("--client-id", default=None, help="Client ID (default: random UUID)")
  97. p.add_argument("--prompt-id", default=None,
  98. help="Filter to a specific prompt_id (default: all jobs)")
  99. p.add_argument("--previews", default=None,
  100. help="Directory to save in-progress preview frames")
  101. p.add_argument("--no-color", action="store_true", help="Disable ANSI colour")
  102. p.add_argument("--timeout", type=float, default=600.0,
  103. help="Hard cap on monitor duration (default 600s)")
  104. args = p.parse_args(argv)
  105. try:
  106. import websocket # type: ignore[import-not-found]
  107. except ImportError:
  108. print(json.dumps({
  109. "error": "websocket-client not installed",
  110. "install": "pip install websocket-client",
  111. }))
  112. return 1
  113. api_key = resolve_api_key(args.api_key)
  114. cloud = is_cloud_host(args.host)
  115. client_id = args.client_id or new_client_id()
  116. # Build WS URL preserving any base-path component (e.g. behind reverse proxy).
  117. parsed = urlparse(args.host if "://" in args.host else f"http://{args.host}")
  118. scheme = "wss" if parsed.scheme == "https" else "ws"
  119. netloc = parsed.netloc
  120. base_path = parsed.path.rstrip("/")
  121. ws_url = f"{scheme}://{netloc}{base_path}/ws?clientId={client_id}"
  122. if cloud and api_key:
  123. ws_url += f"&token={api_key}"
  124. color_on = not args.no_color and sys.stdout.isatty()
  125. preview_dir = Path(args.previews).expanduser() if args.previews else None
  126. if preview_dir:
  127. preview_dir.mkdir(parents=True, exist_ok=True)
  128. log(f"Saving previews to {preview_dir}")
  129. log(f"Connecting to {ws_url} (client_id={client_id})")
  130. if args.prompt_id:
  131. log(f"Filtering messages to prompt_id={args.prompt_id}")
  132. ws = websocket.create_connection(ws_url, timeout=args.timeout)
  133. ws.settimeout(args.timeout)
  134. preview_counter = 0
  135. try:
  136. while True:
  137. try:
  138. msg = ws.recv()
  139. except websocket.WebSocketTimeoutException:
  140. log(f"Idle for {args.timeout}s — exiting")
  141. return 0
  142. if isinstance(msg, bytes):
  143. parsed = parse_binary_frame(msg)
  144. if parsed is None:
  145. continue
  146. if parsed["kind"] in {"preview", "preview_with_metadata"} and preview_dir:
  147. img_bytes = parsed.get("image_bytes", b"")
  148. if img_bytes:
  149. ext = parsed.get("ext", "png")
  150. out = preview_dir / f"preview_{preview_counter:05d}.{ext}"
  151. out.write_bytes(img_bytes)
  152. preview_counter += 1
  153. log(f" [preview] saved {out.name} ({len(img_bytes)} bytes)")
  154. continue
  155. try:
  156. payload = json.loads(msg)
  157. except Exception:
  158. continue
  159. mtype = payload.get("type", "")
  160. mdata = payload.get("data", {}) or {}
  161. pid = mdata.get("prompt_id")
  162. if args.prompt_id and pid and pid != args.prompt_id:
  163. continue
  164. if mtype == "status":
  165. qr = mdata.get("status", {}).get("exec_info", {}).get("queue_remaining", "?")
  166. print(fmt_color(f"[status] queue_remaining={qr}", DIM, color_on=color_on))
  167. elif mtype == "execution_start":
  168. print(fmt_color(f"[start] prompt_id={pid}", BOLD, color_on=color_on))
  169. elif mtype == "executing":
  170. node = mdata.get("node")
  171. if node:
  172. print(fmt_color(f" [executing] node={node}", CYAN, color_on=color_on))
  173. else:
  174. print(fmt_color(f" [executing] (workflow done) prompt_id={pid}", DIM, color_on=color_on))
  175. elif mtype == "progress":
  176. v, m = mdata.get("value", 0), mdata.get("max", 0)
  177. pct = (v / m * 100) if m else 0
  178. print(f" [progress] {v}/{m} ({pct:5.1f}%) node={mdata.get('node')}")
  179. elif mtype == "progress_state":
  180. # Newer extended progress message
  181. nodes = mdata.get("nodes") or {}
  182. running = [k for k, v in nodes.items() if v.get("running")]
  183. if running:
  184. print(fmt_color(f" [progress_state] running={running}", DIM, color_on=color_on))
  185. elif mtype == "executed":
  186. node = mdata.get("node")
  187. out = mdata.get("output") or {}
  188. summary_parts = []
  189. for key in ("images", "video", "videos", "gifs", "audio", "files"):
  190. if out.get(key):
  191. summary_parts.append(f"{key}={len(out[key])}")
  192. summary = ", ".join(summary_parts) if summary_parts else "(no files)"
  193. print(fmt_color(f" [executed] node={node} {summary}", GREEN, color_on=color_on))
  194. elif mtype == "execution_cached":
  195. cached = mdata.get("nodes") or []
  196. if cached:
  197. print(fmt_color(f" [cached] {len(cached)} nodes skipped", DIM, color_on=color_on))
  198. elif mtype == "execution_success":
  199. print(fmt_color(f"[success] prompt_id={pid}", GREEN + BOLD, color_on=color_on))
  200. if args.prompt_id:
  201. return 0
  202. elif mtype == "execution_error":
  203. exc_type = mdata.get("exception_type", "?")
  204. exc_msg = mdata.get("exception_message", "?")
  205. print(fmt_color(f"[error] {exc_type}: {exc_msg}", RED + BOLD, color_on=color_on))
  206. tb = mdata.get("traceback")
  207. if tb:
  208. if isinstance(tb, list):
  209. for line in tb:
  210. print(fmt_color(f" {line}", RED, color_on=color_on))
  211. else:
  212. print(fmt_color(f" {tb}", RED, color_on=color_on))
  213. if args.prompt_id:
  214. return 1
  215. elif mtype == "execution_interrupted":
  216. print(fmt_color(f"[interrupted] prompt_id={pid}", YELLOW, color_on=color_on))
  217. if args.prompt_id:
  218. return 1
  219. elif mtype == "notification":
  220. v = mdata.get("value", "")
  221. print(fmt_color(f"[notification] {v}", DIM, color_on=color_on))
  222. else:
  223. # Unknown / lightly-used types: print compactly
  224. print(fmt_color(f"[{mtype}] {json.dumps(mdata, default=str)[:200]}", DIM, color_on=color_on))
  225. except KeyboardInterrupt:
  226. log("Interrupted")
  227. return 130
  228. finally:
  229. try:
  230. ws.close()
  231. except Exception:
  232. pass
  233. if __name__ == "__main__":
  234. sys.exit(main())