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