22
33import time
44import uuid
5- from typing import Any , Dict , Optional
5+ from typing import Any , Dict , List , Optional
66
77from .. import utils
88from . import enums
@@ -54,10 +54,10 @@ def to_dict(self) -> Dict[str, Any]:
5454 "name" : self .name ,
5555 "id" : str (self .id ),
5656 "type" : self .step_type .value ,
57- "inputs" : self .inputs ,
58- "output" : self .output ,
59- "groundTruth" : self .ground_truth ,
60- "metadata" : self .metadata ,
57+ "inputs" : utils . json_serialize ( self .inputs ) ,
58+ "output" : utils . json_serialize ( self .output ) ,
59+ "groundTruth" : utils . json_serialize ( self .ground_truth ) ,
60+ "metadata" : utils . json_serialize ( self .metadata ) ,
6161 "steps" : [nested_step .to_dict () for nested_step in self .steps ],
6262 "latency" : self .latency ,
6363 "startTime" : self .start_time ,
@@ -119,6 +119,116 @@ def to_dict(self) -> Dict[str, Any]:
119119 return step_dict
120120
121121
122+ class AgentStep (Step ):
123+ """Agent step represents an agent in the trace."""
124+
125+ def __init__ (
126+ self ,
127+ name : str ,
128+ inputs : Optional [Any ] = None ,
129+ output : Optional [Any ] = None ,
130+ metadata : Optional [Dict [str , any ]] = None ,
131+ ) -> None :
132+ super ().__init__ (name = name , inputs = inputs , output = output , metadata = metadata )
133+ self .step_type = enums .StepType .AGENT
134+ self .tool : str = None
135+ self .action : Any = None
136+ self .agent_type : str = None
137+
138+ def to_dict (self ) -> Dict [str , Any ]:
139+ """Dictionary representation of the AgentStep."""
140+ step_dict = super ().to_dict ()
141+ step_dict .update (
142+ {
143+ "tool" : self .tool ,
144+ "action" : self .action ,
145+ "agentType" : self .agent_type ,
146+ }
147+ )
148+ return step_dict
149+
150+
151+ class RetrieverStep (Step ):
152+ """Retriever step represents a retriever in the trace."""
153+
154+ def __init__ (
155+ self ,
156+ name : str ,
157+ inputs : Optional [Any ] = None ,
158+ output : Optional [Any ] = None ,
159+ metadata : Optional [Dict [str , any ]] = None ,
160+ ) -> None :
161+ super ().__init__ (name = name , inputs = inputs , output = output , metadata = metadata )
162+ self .step_type = enums .StepType .RETRIEVER
163+ self .documents : List [Any ] = None
164+
165+ def to_dict (self ) -> Dict [str , Any ]:
166+ """Dictionary representation of the RetrieverStep."""
167+ step_dict = super ().to_dict ()
168+ step_dict .update (
169+ {
170+ "documents" : self .documents ,
171+ }
172+ )
173+ return step_dict
174+
175+
176+ class ToolStep (Step ):
177+ """Tool step represents a tool in the trace."""
178+
179+ def __init__ (
180+ self ,
181+ name : str ,
182+ inputs : Optional [Any ] = None ,
183+ output : Optional [Any ] = None ,
184+ metadata : Optional [Dict [str , any ]] = None ,
185+ ) -> None :
186+ super ().__init__ (name = name , inputs = inputs , output = output , metadata = metadata )
187+ self .step_type = enums .StepType .TOOL
188+ self .function_name : str = None
189+ self .arguments : Any = None
190+
191+ def to_dict (self ) -> Dict [str , Any ]:
192+ """Dictionary representation of the ToolStep."""
193+ step_dict = super ().to_dict ()
194+ step_dict .update (
195+ {
196+ "functionName" : self .function_name ,
197+ "arguments" : self .arguments ,
198+ }
199+ )
200+ return step_dict
201+
202+
203+ class HandoffStep (Step ):
204+ """Handoff step represents a handoff in the trace."""
205+
206+ def __init__ (
207+ self ,
208+ name : str ,
209+ inputs : Optional [Any ] = None ,
210+ output : Optional [Any ] = None ,
211+ metadata : Optional [Dict [str , any ]] = None ,
212+ ) -> None :
213+ super ().__init__ (name = name , inputs = inputs , output = output , metadata = metadata )
214+ self .step_type = enums .StepType .HANDOFF
215+ self .from_component : str = None
216+ self .to_component : str = None
217+ self .handoff_data : Any = None
218+
219+ def to_dict (self ) -> Dict [str , Any ]:
220+ """Dictionary representation of the HandoffStep."""
221+ step_dict = super ().to_dict ()
222+ step_dict .update (
223+ {
224+ "fromComponent" : self .from_component ,
225+ "toComponent" : self .to_component ,
226+ "handoffData" : self .handoff_data ,
227+ }
228+ )
229+ return step_dict
230+
231+
122232# ----------------------------- Factory function ----------------------------- #
123233def step_factory (step_type : enums .StepType , * args , ** kwargs ) -> Step :
124234 """Factory function to create a step based on the step_type."""
@@ -127,5 +237,9 @@ def step_factory(step_type: enums.StepType, *args, **kwargs) -> Step:
127237 step_type_mapping = {
128238 enums .StepType .USER_CALL : UserCallStep ,
129239 enums .StepType .CHAT_COMPLETION : ChatCompletionStep ,
240+ enums .StepType .AGENT : AgentStep ,
241+ enums .StepType .RETRIEVER : RetrieverStep ,
242+ enums .StepType .TOOL : ToolStep ,
243+ enums .StepType .HANDOFF : HandoffStep ,
130244 }
131245 return step_type_mapping [step_type ](* args , ** kwargs )
0 commit comments