8
8
9
9
logger = logging .getLogger (__name__ )
10
10
11
- from typing import Any , AsyncIterator , Sequence , Union , cast
11
+ from typing import Any , AsyncIterator , Union , cast
12
12
13
13
from agents import (
14
14
AgentOutputSchema ,
@@ -54,7 +54,7 @@ def __init__(
54
54
async def get_response (
55
55
self ,
56
56
system_instructions : Optional [str ],
57
- input : Union [str , list [TResponseInputItem ], dict [ str , str ] ],
57
+ input : Union [str , list [TResponseInputItem ]],
58
58
model_settings : ModelSettings ,
59
59
tools : list [Tool ],
60
60
output_schema : Optional [AgentOutputSchemaBase ],
@@ -64,28 +64,6 @@ async def get_response(
64
64
previous_response_id : Optional [str ],
65
65
prompt : Optional [ResponsePromptParam ],
66
66
) -> ModelResponse :
67
- def get_summary (
68
- input : Union [str , list [TResponseInputItem ], dict [str , str ]],
69
- ) -> str :
70
- ### Activity summary shown in the UI
71
- try :
72
- max_size = 100
73
- if isinstance (input , str ):
74
- return input [:max_size ]
75
- elif isinstance (input , list ):
76
- seq_input = cast (Sequence [Any ], input )
77
- last_item = seq_input [- 1 ]
78
- if isinstance (last_item , dict ):
79
- return last_item .get ("content" , "" )[:max_size ]
80
- elif hasattr (last_item , "content" ):
81
- return str (getattr (last_item , "content" ))[:max_size ]
82
- return str (last_item )[:max_size ]
83
- elif isinstance (input , dict ):
84
- return input .get ("content" , "" )[:max_size ]
85
- except Exception as e :
86
- logger .error (f"Error getting summary: { e } " )
87
- return ""
88
-
89
67
def make_tool_info (tool : Tool ) -> ToolInput :
90
68
if isinstance (tool , (FileSearchTool , WebSearchTool )):
91
69
return tool
@@ -150,7 +128,7 @@ def make_tool_info(tool: Tool) -> ToolInput:
150
128
return await workflow .execute_activity_method (
151
129
ModelActivity .invoke_model_activity ,
152
130
activity_input ,
153
- summary = self .model_params .summary_override or get_summary (input ),
131
+ summary = self .model_params .summary_override or _extract_summary (input ),
154
132
task_queue = self .model_params .task_queue ,
155
133
schedule_to_close_timeout = self .model_params .schedule_to_close_timeout ,
156
134
schedule_to_start_timeout = self .model_params .schedule_to_start_timeout ,
@@ -176,3 +154,34 @@ def stream_response(
176
154
prompt : ResponsePromptParam | None ,
177
155
) -> AsyncIterator [TResponseStreamEvent ]:
178
156
raise NotImplementedError ("Temporal model doesn't support streams yet" )
157
+
158
+
159
+ def _extract_summary (input : Union [str , list [TResponseInputItem ]]) -> str :
160
+ ### Activity summary shown in the UI
161
+ try :
162
+ max_size = 100
163
+ if isinstance (input , str ):
164
+ return input [:max_size ]
165
+ elif isinstance (input , list ):
166
+ # Find all message inputs, which are reasonably summarizable
167
+ messages : list [TResponseInputItem ] = [
168
+ item for item in input if item .get ("type" , "message" ) == "message"
169
+ ]
170
+ if not messages :
171
+ return ""
172
+
173
+ content : Any = messages [- 1 ].get ("content" , "" )
174
+
175
+ # In the case of multiple contents, take the last one
176
+ if isinstance (content , list ):
177
+ if not content :
178
+ return ""
179
+ content = content [- 1 ]
180
+
181
+ # Take the text field from the content if present
182
+ if isinstance (content , dict ) and content .get ("text" ) is not None :
183
+ content = content .get ("text" )
184
+ return str (content )[:max_size ]
185
+ except Exception as e :
186
+ logger .error (f"Error getting summary: { e } " )
187
+ return ""
0 commit comments