(
&mut self,
user_message: impl Into<String>,
)
| 210 | } |
| 211 | |
| 212 | async fn execute_subsession( |
| 213 | &mut self, |
| 214 | user_message: impl Into<String>, |
| 215 | ) -> Result<String, AgentError> { |
| 216 | self.conversation.add_user_message(user_message); |
| 217 | |
| 218 | let mut steps = 0; |
| 219 | let mut final_response = String::new(); |
| 220 | |
| 221 | while steps < self.max_steps { |
| 222 | steps += 1; |
| 223 | |
| 224 | let provider = self.get_provider()?; |
| 225 | let model_id = self.get_model_id(&provider); |
| 226 | let request = ChatRequest::new(model_id, self.conversation.to_provider_messages()); |
| 227 | |
| 228 | let stream = provider |
| 229 | .chat_stream(request) |
| 230 | .await |
| 231 | .map_err(|e| AgentError::ProviderError(e.to_string()))?; |
| 232 | |
| 233 | let (response, tool_calls) = self.process_stream(stream).await?; |
| 234 | |
| 235 | if tool_calls.is_empty() { |
| 236 | final_response = response; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | self.conversation.add_assistant_message(&response); |
| 241 | |
| 242 | for tool_call in tool_calls { |
| 243 | let result = self.execute_tool_without_subsessions(&tool_call).await; |
| 244 | |
| 245 | let (content, is_error) = match result { |
| 246 | Ok(output) => (output, false), |
| 247 | Err(e) => (e.to_string(), true), |
| 248 | }; |
| 249 | |
| 250 | self.conversation.add_tool_result( |
| 251 | &tool_call.id, |
| 252 | &tool_call.name, |
| 253 | content, |
| 254 | is_error, |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if steps >= self.max_steps { |
| 260 | return Err(AgentError::MaxStepsExceeded); |
| 261 | } |
| 262 | |
| 263 | Ok(final_response) |
| 264 | } |
| 265 | |
| 266 | pub async fn execute_streaming( |
| 267 | &mut self, |
no test coverage detected