(
&self,
state: &mut State<'gc>,
tool_calls: &Option<Vec<ToolCall>>,
)
| 200 | } |
| 201 | |
| 202 | fn handle_tool_call( |
| 203 | &self, |
| 204 | state: &mut State<'gc>, |
| 205 | tool_calls: &Option<Vec<ToolCall>>, |
| 206 | ) -> Result<Response<'gc>, String> { |
| 207 | let mut response = Response::default(); |
| 208 | for tool_call in tool_calls.as_ref().unwrap() { |
| 209 | let name = tool_call.function.name.as_ref().unwrap(); |
| 210 | if let Some(tool_def) = self.tools.get(name) { |
| 211 | let arguments = serde_json::from_str::<serde_json::Value>( |
| 212 | tool_call.function.arguments.as_ref().unwrap(), |
| 213 | ) |
| 214 | .unwrap(); |
| 215 | // Pass params as positional arguments |
| 216 | let params = tool_def |
| 217 | .params |
| 218 | .keys() |
| 219 | .filter_map(|key| { |
| 220 | arguments |
| 221 | .get(key) |
| 222 | .map(|v| Value::String(state.intern(v.as_str().unwrap().as_bytes()))) |
| 223 | }) |
| 224 | .collect::<Vec<_>>(); |
| 225 | let result = state |
| 226 | .eval_function_with_id(tool_def.chunk_id, ¶ms) |
| 227 | .unwrap(); |
| 228 | // println!( |
| 229 | // "call tool {name} params: {:?}, result: {}", |
| 230 | // params.iter().map(|i| format!("{i}")).collect::<Vec<_>>(), |
| 231 | // result |
| 232 | // ); |
| 233 | let content = if let Value::Agent(agent) = result { |
| 234 | let agent_name = agent.name; |
| 235 | response.agent = state.get_global(agent_name).map(|v| v.as_agent().unwrap()); |
| 236 | format!("{{\"assistant\": {}}}", agent_name) |
| 237 | } else { |
| 238 | result.to_string() |
| 239 | }; |
| 240 | response.messages.push(ChatCompletionMessage { |
| 241 | role: MessageRole::tool, |
| 242 | content: Content::Text(content), |
| 243 | name: tool_call.function.name.clone(), |
| 244 | tool_calls: None, |
| 245 | tool_call_id: Some(tool_call.id.clone()), |
| 246 | }); |
| 247 | } else { |
| 248 | return Err(format!("Warning: unknow tool function: {name}")); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | Ok(response) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | #[cfg(feature = "ai_test")] |
no test coverage detected