rig/tools/
think.rs

1use serde::{Deserialize, Serialize};
2use serde_json::json;
3
4use crate::completion::ToolDefinition;
5use crate::tool::Tool;
6
7/// Arguments for the Think tool
8#[derive(Deserialize)]
9pub struct ThinkArgs {
10    /// The thought to think about
11    pub thought: String,
12}
13
14/// Error type for the Think tool
15#[derive(Debug, thiserror::Error)]
16#[error("Think tool error: {0}")]
17pub struct ThinkError(String);
18
19/// The Think tool allows agents to stop and think in complex tool use situations.
20///
21/// This tool provides a dedicated space for structured thinking during complex tasks,
22/// particularly when processing external information (e.g., tool call results).
23/// It doesn't actually perform any actions or retrieve any information - it just
24/// provides a space for the model to reason through complex problems.
25///
26/// This tool is original derived from the
27///  [Think tool](https://anthropic.com/engineering/claude-think-tool) blog post from Anthropic.
28#[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        // The think tool doesn't actually do anything except echo back the thought
60        // This is intentional - it's just a space for the model to reason through problems
61        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}