1use serde::{Deserialize, Serialize};
2use serde_json::json;
3
4use crate::completion::ToolDefinition;
5use crate::tool::Tool;
6
7#[derive(Deserialize)]
9pub struct ThinkArgs {
10 pub thought: String,
12}
13
14#[derive(Debug, thiserror::Error)]
16#[error("Think tool error: {0}")]
17pub struct ThinkError(String);
18
19#[derive(Deserialize, Serialize)]
29pub struct ThinkTool;
30
31impl Tool for ThinkTool {
32 const NAME: &'static str = "think";
33
34 type Error = ThinkError;
35 type Args = ThinkArgs;
36 type Output = String;
37
38 async fn definition(&self, _prompt: String) -> ToolDefinition {
39 ToolDefinition {
40 name: "think".to_string(),
41 description: "Use the tool to think about something. It will not obtain new information
42 or change the database, but just append the thought to the log. Use it when complex
43 reasoning or some cache memory is needed."
44 .to_string(),
45 parameters: json!({
46 "type": "object",
47 "properties": {
48 "thought": {
49 "type": "string",
50 "description": "A thought to think about."
51 }
52 },
53 "required": ["thought"]
54 }),
55 }
56 }
57
58 async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
59 Ok(args.thought)
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[tokio::test]
70 async fn test_think_tool_definition() {
71 let tool = ThinkTool;
72 let definition = tool.definition("".to_string()).await;
73
74 assert_eq!(definition.name, "think");
75 assert!(
76 definition
77 .description
78 .contains("Use the tool to think about something")
79 );
80 }
81
82 #[tokio::test]
83 async fn test_think_tool_call() {
84 let tool = ThinkTool;
85 let args = ThinkArgs {
86 thought: "I need to verify the user's identity before proceeding".to_string(),
87 };
88
89 let result = tool.call(args).await.unwrap();
90 assert_eq!(
91 result,
92 "I need to verify the user's identity before proceeding"
93 );
94 }
95}