1use crate::{
2 agent::Agent,
3 completion::{CompletionModel, Prompt, PromptError, ToolDefinition},
4 tool::Tool,
5};
6use schemars::{JsonSchema, schema_for};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10pub struct AgentToolArgs {
11 prompt: String,
13}
14
15impl<M: CompletionModel> Tool for Agent<M> {
16 const NAME: &'static str = "agent_tool";
17
18 type Error = PromptError;
19 type Args = AgentToolArgs;
20 type Output = String;
21
22 async fn definition(&self, _prompt: String) -> ToolDefinition {
23 ToolDefinition {
24 name: <Self as Tool>::name(self),
25 description: format!(
26 "A tool that allows the agent to call another agent by prompting it. The preamble
27 of that agent follows:
28 ---
29 {}",
30 self.preamble.clone().unwrap_or_default()
31 ),
32 parameters: serde_json::to_value(schema_for!(AgentToolArgs))
33 .expect("converting JSON schema to JSON value should never fail"),
34 }
35 }
36
37 async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
38 self.prompt(args.prompt).await
39 }
40
41 fn name(&self) -> String {
42 self.name.clone().unwrap_or_else(|| Self::NAME.to_string())
43 }
44}