11"""Messages from the backend to the frontend via WebSocket."""
22
3+ from enum import Enum
34import uuid
45from dataclasses import asdict , dataclass , field
56from typing import Any , Dict , List , Literal , Optional
@@ -135,4 +136,36 @@ class ApprovalRequest(KernelBaseModel):
135136 session_id : str
136137 user_id : str
137138 action : str
138- agent_name : str
139+ agent_name : str
140+
141+
142+
143+ class WebsocketMessageType (str , Enum ):
144+ AGENT_MESSAGE = "agent_message"
145+ AGENT_STREAM_START = "agent_stream_start"
146+ AGENT_STREAM_END = "agent_stream_end"
147+ AGENT_MESSAGE_STREAMING = "agent_message_streaming"
148+ AGENT_TOOL_MESSAGE = "agent_tool_message"
149+ PLAN_APPROVAL_REQUEST = "plan_approval_request"
150+ PLAN_APPROVAL_RESPONSE = "plan_approval_response"
151+ REPLAN_APPROVAL_REQUEST = "replan_approval_request"
152+ REPLAN_APPROVAL_RESPONSE = "replan_approval_response"
153+ USER_CLARIFICATION_REQUEST = "user_clarification_request"
154+ USER_CLARIFICATION_RESPONSE = "user_clarification_response"
155+ FINAL_RESULT_MESSAGE = "final_result_message"
156+
157+
158+ @dataclass (slots = True )
159+ class WebsocketMessage :
160+ """Generic WebSocket message wrapper."""
161+ type : WebsocketMessageType
162+ message : Any
163+ data : Any
164+
165+ def to_dict (self ) -> Dict [str , Any ]:
166+ """Convert the WebsocketMessage to a dictionary for JSON serialization."""
167+ return {
168+ "type" : self .type ,
169+ "data" : self .data .to_dict () if hasattr (self .data , 'to_dict' ) else self .data ,
170+ "message" : self .message .to_dict () if hasattr (self .message , 'to_dict' ) else self .message
171+ }
0 commit comments