ws_smoke.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import argparse
  2. import asyncio
  3. import json
  4. from typing import Any
  5. DEFAULT_URL = "ws://127.0.0.1:8000/ws/debug"
  6. DEFAULT_MESSAGE = "Debug this agent handoff path."
  7. DEFAULT_MODEL = ""
  8. def build_payload(
  9. message: str,
  10. model: str,
  11. handoff_note_enabled: bool,
  12. ) -> dict[str, Any]:
  13. return {
  14. "user_message": message,
  15. "system_prompts": [
  16. "You are a chat agent running inside Agent Lab. "
  17. "Stream a concise answer and call handoff_note when an event handoff helps."
  18. ],
  19. "pre_messages": [],
  20. "chat_agent": {
  21. "model": model,
  22. "temperature": 0.2,
  23. "max_tokens": 1024,
  24. },
  25. "event_agent": {
  26. "enabled_tools": ["handoff_note"] if handoff_note_enabled else [],
  27. "max_event_loops": 3,
  28. },
  29. }
  30. def build_parser() -> argparse.ArgumentParser:
  31. parser = argparse.ArgumentParser(
  32. description="Send one Agent Lab WebSocket debug request and print streamed JSON."
  33. )
  34. parser.add_argument("--url", default=DEFAULT_URL, help=f"WebSocket URL. Default: {DEFAULT_URL}")
  35. parser.add_argument("--message", default=DEFAULT_MESSAGE, help="User message to send.")
  36. parser.add_argument(
  37. "--model",
  38. default=DEFAULT_MODEL,
  39. help="Chat model name. Defaults to the backend configured model.",
  40. )
  41. tool_group = parser.add_mutually_exclusive_group()
  42. tool_group.add_argument(
  43. "--enable-handoff-note",
  44. dest="handoff_note_enabled",
  45. action="store_true",
  46. default=True,
  47. help="Enable the handoff_note tool. This is the default.",
  48. )
  49. tool_group.add_argument(
  50. "--disable-handoff-note",
  51. dest="handoff_note_enabled",
  52. action="store_false",
  53. help="Disable the handoff_note tool.",
  54. )
  55. return parser
  56. async def run_smoke(url: str, payload: dict[str, Any]) -> None:
  57. import websockets
  58. async with websockets.connect(url) as websocket:
  59. await websocket.send(_compact_json(payload))
  60. async for raw_message in websocket:
  61. try:
  62. message = json.loads(raw_message)
  63. except json.JSONDecodeError:
  64. print(raw_message, flush=True)
  65. continue
  66. print(_compact_json(message), flush=True)
  67. if message.get("type") in {"done", "error"}:
  68. break
  69. def _compact_json(value: Any) -> str:
  70. return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
  71. def main() -> int:
  72. args = build_parser().parse_args()
  73. payload = build_payload(
  74. message=args.message,
  75. model=args.model,
  76. handoff_note_enabled=args.handoff_note_enabled,
  77. )
  78. asyncio.run(run_smoke(args.url, payload))
  79. return 0
  80. if __name__ == "__main__":
  81. raise SystemExit(main())