2626 called_tools_history_template = Template (f .read ())
2727
2828
29+ def build_called_tool_record (tool : dict [str , Any ], result : CallToolResult ) -> dict [str , Any ]:
30+ """Build a called-tool history record matching called_tools_history.jinja."""
31+ return {
32+ 'name' : tool ['name' ],
33+ 'arguments' : tool ['arguments' ],
34+ 'isError' : result .isError ,
35+ 'result' : result .content [0 ].text ,
36+ }
37+
38+
2939def stream_llm (prompt : str ) -> Generator [str , None ]:
3040 """Stream LLM response.
3141
@@ -80,9 +90,7 @@ async def decide(
8090 return self .call_llm (question )
8191 tool_prompt = await get_mcp_tool_prompt (self .mcp_url )
8292 if called_tools :
83- called_tools_prompt = called_tools_history_template .render (
84- called_tools = called_tools
85- )
93+ called_tools_prompt = called_tools_history_template .render (called_tools = called_tools )
8694 else :
8795 called_tools_prompt = ''
8896
@@ -112,10 +120,7 @@ async def call_tool(self, tools: list[dict]) -> list[CallToolResult]:
112120 tools (list[dict]): The tools to call.
113121 """
114122 return await asyncio .gather (
115- * [
116- call_mcp_tool (self .mcp_url , tool ['name' ], tool ['arguments' ])
117- for tool in tools
118- ]
123+ * [call_mcp_tool (self .mcp_url , tool ['name' ], tool ['arguments' ]) for tool in tools ]
119124 )
120125
121126 async def stream (self , question : str ) -> AsyncGenerator [dict [str , Any ]]:
@@ -128,48 +133,29 @@ async def stream(self, question: str) -> AsyncGenerator[dict[str, Any]]:
128133 dict: Streaming output, including intermediate steps and final result.
129134 """
130135 called_tools = []
131- for i in range (10 ):
132- yield {
133- 'is_task_complete' : False ,
134- 'require_user_input' : False ,
135- 'content' : f'Step { i } ' ,
136- }
137-
138- response = ''
136+ last_response = ''
137+ for _ in range (10 ):
138+ last_response = ''
139139 for chunk in await self .decide (question , called_tools ):
140- response += chunk
140+ last_response += chunk
141141 yield {
142142 'is_task_complete' : False ,
143143 'require_user_input' : False ,
144144 'content' : chunk ,
145145 }
146- tools = self .extract_tools (response )
146+ tools = self .extract_tools (last_response )
147147 if not tools :
148148 break
149149 results = await self .call_tool (tools )
150-
151150 called_tools += [
152- {
153- 'tool' : tool ['name' ],
154- 'arguments' : tool ['arguments' ],
155- 'isError' : result .isError ,
156- 'result' : result .content [0 ].text ,
157- }
151+ build_called_tool_record (tool , result )
158152 for tool , result in zip (tools , results , strict = True )
159153 ]
160- called_tools_history = called_tools_history_template .render (
161- called_tools = called_tools , question = question
162- )
163- yield {
164- 'is_task_complete' : False ,
165- 'require_user_input' : False ,
166- 'content' : called_tools_history ,
167- }
168154
169155 yield {
170156 'is_task_complete' : True ,
171157 'require_user_input' : False ,
172- 'content' : 'Task completed' ,
158+ 'content' : last_response ,
173159 }
174160
175161
0 commit comments