MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / handle_tool_call

Method handle_tool_call

aiscript-vm/src/ai/agent.rs:202–253  ·  view source on GitHub ↗
(
        &self,
        state: &mut State<'gc>,
        tool_calls: &Option<Vec<ToolCall>>,
    )

Source from the content-addressed store, hash-verified

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, &params)
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")]

Callers 1

_run_agentFunction · 0.80

Calls 10

as_bytesMethod · 0.80
eval_function_with_idMethod · 0.80
as_agentMethod · 0.80
pushMethod · 0.80
as_refMethod · 0.45
getMethod · 0.45
internMethod · 0.45
as_strMethod · 0.45
get_globalMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected