-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_handler.py
More file actions
173 lines (147 loc) · 6.86 KB
/
Copy pathcallback_handler.py
File metadata and controls
173 lines (147 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""WebSocket callback handler for LangChain agent events."""
import json
from typing import Any, Dict, List, Optional
from datetime import datetime
from fastapi import WebSocket, WebSocketDisconnect
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import AgentAction, AgentFinish, LLMResult
from logger import LoggerMixin
from models import WebSocketMessage, MessageType
from exceptions import ConnectionError
class WebSocketCallbackHandler(BaseCallbackHandler, LoggerMixin):
"""Enhanced WebSocket callback handler with error handling and logging."""
def __init__(self, websocket: WebSocket, session_id: Optional[str] = None):
super().__init__()
self.websocket = websocket
self.session_id = session_id or f"session_{datetime.now().timestamp()}"
self.message_count = 0
self.start_time = datetime.now()
async def _send_message(self, message_type: MessageType, content: str,
metadata: Optional[Dict[str, Any]] = None):
"""Send a message through the WebSocket with error handling."""
try:
message = WebSocketMessage(
type=message_type,
content=content,
metadata=metadata or {}
)
await self.websocket.send_text(message.model_dump_json())
self.message_count += 1
except WebSocketDisconnect:
self.logger.warning("WebSocket disconnected during message send",
session_id=self.session_id)
raise ConnectionError("WebSocket connection lost")
except Exception as e:
self.logger.error("Failed to send WebSocket message",
session_id=self.session_id,
error=str(e),
message_type=message_type)
raise ConnectionError(f"Failed to send message: {str(e)}")
async def on_llm_start(self, serialized: Dict[str, Any], prompts: List[str], **kwargs) -> None:
"""Called when LLM starts running."""
self.logger.info("LLM started", session_id=self.session_id)
await self._send_message(
MessageType.LOG,
"🤖 AI is thinking about your PC build request...",
{"event": "llm_start"}
)
async def on_llm_new_token(self, token: str, **kwargs) -> None:
"""Called when a new token is generated."""
await self._send_message(MessageType.TOKEN, token)
async def on_llm_end(self, response: LLMResult, **kwargs) -> None:
"""Called when LLM finishes running."""
self.logger.info("LLM finished",
session_id=self.session_id,
token_count=response.llm_output.get('token_usage', {}).get('total_tokens'))
async def on_llm_error(self, error: BaseException, **kwargs) -> None:
"""Called when LLM encounters an error."""
self.logger.error("LLM error",
session_id=self.session_id,
error=str(error))
await self._send_message(
MessageType.ERROR,
f"AI processing error: {str(error)}",
{"event": "llm_error"}
)
async def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwargs) -> None:
"""Called when a tool starts running."""
tool_name = serialized.get('name', 'Unknown Tool')
self.logger.info("Tool started",
session_id=self.session_id,
tool=tool_name,
input=input_str)
await self._send_message(
MessageType.LOG,
f"🔍 Searching for: {input_str}",
{"event": "tool_start", "tool": tool_name}
)
async def on_tool_end(self, output: str, **kwargs) -> None:
"""Called when a tool finishes running."""
self.logger.info("Tool finished", session_id=self.session_id)
await self._send_message(
MessageType.LOG,
"✅ Search completed, analyzing results...",
{"event": "tool_end"}
)
async def on_tool_error(self, error: BaseException, **kwargs) -> None:
"""Called when a tool encounters an error."""
self.logger.error("Tool error",
session_id=self.session_id,
error=str(error))
await self._send_message(
MessageType.ERROR,
f"Search error: {str(error)}",
{"event": "tool_error"}
)
async def on_agent_action(self, action: AgentAction, **kwargs) -> None:
"""Called when the agent takes an action."""
self.logger.info("Agent action",
session_id=self.session_id,
action=action.tool,
input=action.tool_input)
await self._send_message(
MessageType.LOG,
f"🎯 Action: {action.tool}",
{"event": "agent_action", "tool": action.tool, "input": action.tool_input}
)
async def on_agent_finish(self, finish: AgentFinish, **kwargs) -> None:
"""Called when the agent finishes."""
processing_time = (datetime.now() - self.start_time).total_seconds()
self.logger.info("Agent finished",
session_id=self.session_id,
processing_time=processing_time,
messages_sent=self.message_count)
await self._send_message(
MessageType.FINAL_OUTPUT,
finish.return_values.get("output", "No output generated"),
{
"event": "agent_finish",
"processing_time": processing_time,
"messages_sent": self.message_count
}
)
async def on_chain_start(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs) -> None:
"""Called when a chain starts."""
self.logger.info("Chain started",
session_id=self.session_id,
inputs=list(inputs.keys()))
async def on_chain_end(self, outputs: Dict[str, Any], **kwargs) -> None:
"""Called when a chain ends."""
self.logger.info("Chain ended", session_id=self.session_id)
async def on_chain_error(self, error: BaseException, **kwargs) -> None:
"""Called when a chain encounters an error."""
self.logger.error("Chain error",
session_id=self.session_id,
error=str(error))
await self._send_message(
MessageType.ERROR,
f"Processing error: {str(error)}",
{"event": "chain_error"}
)
async def send_heartbeat(self):
"""Send a heartbeat message to keep the connection alive."""
await self._send_message(
MessageType.HEARTBEAT,
"ping",
{"timestamp": datetime.now().isoformat()}
)