(&mut self, user_message: impl Into<String>)
| 133 | } |
| 134 | |
| 135 | pub async fn execute(&mut self, user_message: impl Into<String>) -> Result<String, AgentError> { |
| 136 | self.conversation.add_user_message(user_message); |
| 137 | |
| 138 | // Plugin hook: session.start |
| 139 | opencode_plugin::trigger( |
| 140 | HookContext::new(HookEvent::SessionStart) |
| 141 | .with_data("agent", serde_json::json!(&self.agent.name)) |
| 142 | .with_data("max_steps", serde_json::json!(self.max_steps)), |
| 143 | ) |
| 144 | .await; |
| 145 | |
| 146 | let mut steps = 0; |
| 147 | let mut final_response = String::new(); |
| 148 | |
| 149 | while steps < self.max_steps { |
| 150 | steps += 1; |
| 151 | |
| 152 | let provider = self.get_provider()?; |
| 153 | let model_id = self.get_model_id(&provider); |
| 154 | |
| 155 | // Plugin hook: chat.system.transform — let plugins modify system prompt per step |
| 156 | opencode_plugin::trigger( |
| 157 | HookContext::new(HookEvent::ChatSystemTransform) |
| 158 | .with_data("agent", serde_json::json!(&self.agent.name)) |
| 159 | .with_data("model_id", serde_json::json!(&model_id)) |
| 160 | .with_data("step", serde_json::json!(steps)), |
| 161 | ) |
| 162 | .await; |
| 163 | |
| 164 | let request = ChatRequest::new(model_id, self.conversation.to_provider_messages()); |
| 165 | |
| 166 | let stream = provider |
| 167 | .chat_stream(request) |
| 168 | .await |
| 169 | .map_err(|e| AgentError::ProviderError(e.to_string()))?; |
| 170 | |
| 171 | let (response, tool_calls) = self.process_stream(stream).await?; |
| 172 | |
| 173 | if tool_calls.is_empty() { |
| 174 | final_response = response; |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | self.conversation.add_assistant_message(&response); |
| 179 | |
| 180 | for tool_call in tool_calls { |
| 181 | let result = self.execute_tool(&tool_call).await; |
| 182 | |
| 183 | let (content, is_error) = match result { |
| 184 | Ok(output) => (output, false), |
| 185 | Err(e) => (e.to_string(), true), |
| 186 | }; |
| 187 | |
| 188 | self.conversation.add_tool_result( |
| 189 | &tool_call.id, |
| 190 | &tool_call.name, |
| 191 | content, |
| 192 | is_error, |
no test coverage detected