-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathagent.py
More file actions
318 lines (265 loc) · 10.5 KB
/
agent.py
File metadata and controls
318 lines (265 loc) · 10.5 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from typing import Union, AsyncIterable, Optional, Any, TypeAlias
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from agent_squad.types import ConversationMessage
from agent_squad.utils import Logger
from uuid import UUID
# Type aliases for complex types
AgentParamsType: TypeAlias = dict[str, Any]
AgentOutputType: TypeAlias = Union[str, "AgentStreamResponse", Any] # Forward reference
@dataclass
class AgentProcessingResult:
"""
Contains metadata about the result of an agent's processing.
Attributes:
user_input: The original input from the user
agent_id: Unique identifier for the agent
agent_name: Display name of the agent
user_id: Identifier for the user
session_id: Identifier for the current session
additional_params: Optional additional parameters for the agent
"""
user_input: str
agent_id: str
agent_name: str
user_id: str
session_id: str
additional_params: AgentParamsType = field(default_factory=dict)
@dataclass
class AgentStreamResponse:
"""
Represents a streaming response from an agent.
Attributes:
text: The current text in the stream
final_message: The complete message when streaming is complete
"""
text: str = ""
thinking: Optional[str] = ""
final_message: Optional[ConversationMessage] = None
final_thinking: Optional[str] = None
@dataclass
class AgentResponse:
"""
Complete response from an agent, including metadata and output.
Attributes:
metadata: Processing metadata
output: The actual output from the agent
streaming: Whether this response is streaming
"""
metadata: AgentProcessingResult
output: AgentOutputType
streaming: bool
class AgentCallbacks:
async def on_agent_start(
self,
agent_name,
payload_input: Any,
messages: list[Any],
run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> dict:
"""
Callback method that runs when an agent starts processing.
This method is called at the beginning of an agent's execution, providing information
about the agent session and its context.
Parameters:
self: The instance of the callback handler class.
agent_name: Name of the agent that is starting.
payload_input: Dictionary containing the agent's input.
messages: List of message dictionaries representing the conversation history.
run_id: Unique identifier for this specific agent run.
tags: Optional list of string tags associated with this agent run.
metadata: Optional dictionary containing additional metadata about the run.
**kwargs: Additional keyword arguments that might be passed to the callback.
Returns:
dict: The agent tracking information, this is made available to all other
callbacks using the agent_tracking_info key in kwargs.
"""
return {}
async def on_agent_end(
self,
agent_name,
response: Any,
messages: list[Any],
run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""
Callback method that runs when an agent completes its processing.
This method is called at the end of an agent's execution, providing information
about the completed agent session and its response.
Parameters:
self: The instance of the callback handler class.
agent_name: Name of the agent that is completing.
response: Dictionary containing the agent's response or output.
run_id: Unique identifier for this specific agent run.
tags: Optional list of string tags associated with this agent run.
metadata: Optional dictionary containing additional metadata about the run.
**kwargs: Additional keyword arguments that might be passed to the callback.
Returns:
Any: The return value is implementation-dependent.
"""
pass
async def on_llm_start(
self,
name: str,
payload_input: Any,
run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""
Callback method that runs when an llm starts processing.
This method is called at the beginning of an llm's execution, providing information
about the llm session and its context.
Parameters:
self: The instance of the callback handler class.
name: Name of the llm that is starting.
payload_input: Dictionary containing the agent's input.
run_id: Unique identifier for this specific agent run.
tags: Optional list of string tags associated with this agent run.
metadata: Optional dictionary containing additional metadata about the run.
**kwargs: Additional keyword arguments that might be passed to the callback.
Returns:
Any: The return value is implementation-dependent.
"""
pass
"""
Defines callbacks that can be triggered during agent processing.
Provides default implementations that can be overridden by subclasses.
"""
async def on_llm_new_token(self,
token: str,
**kwargs: Any) -> None:
"""
Called when a new token is generated by the LLM.
Args:
self: The instance of the callback handler class.
token: The new token generated (text)
**kwargs: Additional keyword arguments that might be passed to the callback.
"""
pass # Default implementation does nothing
async def on_llm_end(
self,
name: str,
output: Any,
run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""
Callback method that runs when an llm stops.
This method is called at the end of an llm's execution, providing information
about the llm session and its context.
Parameters:
self: The instance of the callback handler class.
name: Name of the LLM that is starting.
output: Dictionary containing the agent's input.
run_id: Unique identifier for this specific agent run.
tags: Optional list of string tags associated with this agent run.
metadata: Optional dictionary containing additional metadata about the run.
**kwargs: Additional keyword arguments that might be passed to the callback.
Returns:
Any: The return value is implementation-dependent.
"""
pass
@dataclass
class AgentOptions:
"""
Configuration options for an agent.
Attributes:
name: The display name of the agent
description: A description of the agent's purpose and capabilities
save_chat: Whether to save the chat history
callbacks: Optional callbacks for agent events
LOG_AGENT_DEBUG_TRACE: Whether to enable debug tracing for this agent
"""
name: str
description: str
save_chat: bool = True
callbacks: Optional[AgentCallbacks] = None
# Optional: Flag to enable/disable agent debug trace logging
# If true, the agent will log additional debug information
LOG_AGENT_DEBUG_TRACE: Optional[bool] = False
class Agent(ABC):
"""
Abstract base class for all agents in the system.
Implements common functionality and defines the required interface
for concrete agent implementations.
"""
def __init__(self, options: AgentOptions):
"""
Initialize a new agent with the given options.
Args:
options: Configuration options for this agent
"""
self.name = options.name
self.id = self.generate_key_from_name(options.name)
self.description = options.description
self.save_chat = options.save_chat
self.callbacks = (
options.callbacks if options.callbacks is not None else AgentCallbacks()
)
self.log_debug_trace = options.LOG_AGENT_DEBUG_TRACE
def is_streaming_enabled(self) -> bool:
"""
Whether this agent supports streaming responses.
Returns:
True if streaming is enabled, False otherwise
"""
return False
@staticmethod
def generate_key_from_name(name: str) -> str:
"""
Generate a standardized key from an agent name.
Args:
name: The display name to convert
Returns:
A lowercase, hyphenated key with special characters removed
"""
# Remove special characters and replace spaces with hyphens
key = re.sub(r"[^a-zA-Z0-9\s-]", "", name)
key = re.sub(r"\s+", "-", key)
return key.lower()
@abstractmethod
async def process_request(
self,
input_text: str,
user_id: str,
session_id: str,
chat_history: list[ConversationMessage],
additional_params: Optional[AgentParamsType] = None,
) -> Union[ConversationMessage, AsyncIterable[AgentOutputType]]:
"""
Process a user request and generate a response.
Args:
input_text: The user's input text
user_id: Identifier for the user
session_id: Identifier for the current session
chat_history: List of previous messages in the conversation
additional_params: Optional additional parameters
Returns:
Either a complete message or an async iterable for streaming responses
"""
pass
def log_debug(self, class_name: str, message: str, data: Any = None) -> None:
"""
Log a debug message if debug tracing is enabled.
Args:
class_name: Name of the class logging the message
message: The message to log
data: Optional data to include in the log
"""
if self.log_debug_trace:
prefix = f"> {class_name} \n> {self.name} \n>"
if data:
Logger.info(f"{prefix} {message} \n> {data}")
else:
Logger.info(f"{prefix} {message} \n>")