Skip to content

feat(tools): Add think tool with option to disable it. #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/q_chat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,6 @@ mod tests {
"Done",
],
]));

let tool_manager = ToolManager::default();
let tool_config = serde_json::from_str::<HashMap<String, ToolSpec>>(include_str!("tools/tool_index.json"))
.expect("Tools failed to load");
Expand Down
10 changes: 9 additions & 1 deletion crates/q_chat/src/tool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ impl ToolManager {
let tx = self.loading_status_sender.take();
let display_task = self.loading_display_task.take();
let tool_specs = {
let tool_specs = serde_json::from_str::<HashMap<String, ToolSpec>>(include_str!("tools/tool_index.json"))?;
let mut tool_specs =
serde_json::from_str::<HashMap<String, ToolSpec>>(include_str!("tools/tool_index.json"))?;
if !crate::tools::think::Think::is_enabled() {
tool_specs.remove("q_think_tool");
}

Arc::new(Mutex::new(tool_specs))
};
let conversation_id = self.conversation_id.clone();
Expand Down Expand Up @@ -671,6 +676,9 @@ impl ToolManager {
"execute_bash" => Tool::ExecuteBash(serde_json::from_value::<ExecuteBash>(value.args).map_err(map_err)?),
"use_aws" => Tool::UseAws(serde_json::from_value::<UseAws>(value.args).map_err(map_err)?),
"report_issue" => Tool::GhIssue(serde_json::from_value::<GhIssue>(value.args).map_err(map_err)?),
"q_think_tool" => {
Tool::Think(serde_json::from_value::<crate::tools::think::Think>(value.args).map_err(map_err)?)
},
// Note that this name is namespaced with server_name{DELIMITER}tool_name
name => {
let name = self.tn_map.get(name).map_or(name, String::as_str);
Expand Down
10 changes: 9 additions & 1 deletion crates/q_chat/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod execute_bash;
pub mod fs_read;
pub mod fs_write;
pub mod gh_issue;
pub mod think;
pub mod use_aws;

use std::collections::HashMap;
use std::io::Write;
use std::path::{
Expand All @@ -28,6 +28,7 @@ use serde::{
Deserialize,
Serialize,
};
use think::Think;
use use_aws::UseAws;

use super::consts::MAX_TOOL_RESPONSE_SIZE;
Expand All @@ -41,6 +42,7 @@ pub enum Tool {
UseAws(UseAws),
Custom(CustomTool),
GhIssue(GhIssue),
Think(Think),
}

impl Tool {
Expand All @@ -53,6 +55,7 @@ impl Tool {
Tool::UseAws(_) => "use_aws",
Tool::Custom(custom_tool) => &custom_tool.name,
Tool::GhIssue(_) => "gh_issue",
Tool::Think(_) => "q_think_tool (beta)",
}
.to_owned()
}
Expand All @@ -66,6 +69,7 @@ impl Tool {
Tool::UseAws(use_aws) => use_aws.requires_acceptance(),
Tool::Custom(_) => true,
Tool::GhIssue(_) => false,
Tool::Think(_) => true,
}
}

Expand All @@ -78,6 +82,7 @@ impl Tool {
Tool::UseAws(use_aws) => use_aws.invoke(context, updates).await,
Tool::Custom(custom_tool) => custom_tool.invoke(context, updates).await,
Tool::GhIssue(gh_issue) => gh_issue.invoke(updates).await,
Tool::Think(think) => think.invoke(updates).await,
}
}

Expand All @@ -90,6 +95,7 @@ impl Tool {
Tool::UseAws(use_aws) => use_aws.queue_description(updates),
Tool::Custom(custom_tool) => custom_tool.queue_description(updates),
Tool::GhIssue(gh_issue) => gh_issue.queue_description(updates),
Tool::Think(think) => Think::queue_description(think, updates),
}
}

Expand All @@ -102,6 +108,7 @@ impl Tool {
Tool::UseAws(use_aws) => use_aws.validate(ctx).await,
Tool::Custom(custom_tool) => custom_tool.validate(ctx).await,
Tool::GhIssue(gh_issue) => gh_issue.validate(ctx).await,
Tool::Think(think) => think.validate(ctx).await,
}
}
}
Expand Down Expand Up @@ -175,6 +182,7 @@ impl ToolPermissions {
"execute_bash" => "trust read-only commands".dark_grey(),
"use_aws" => "trust read-only commands".dark_grey(),
"report_issue" => "trusted".dark_green().bold(),
"q_think_tool" => "trusted (beta)".dark_green().bold(),
_ => "not trusted".dark_grey(),
};

Expand Down
70 changes: 70 additions & 0 deletions crates/q_chat/src/tools/think.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::io::Write;

use crossterm::queue;
use crossterm::style::{
self,
Color,
};
use eyre::Result;
use fig_settings::settings;
use serde::Deserialize;

use super::{
InvokeOutput,
OutputKind,
};

/// The Think tool allows the model to reason through complex problems during response generation.
/// It provides a dedicated space for the model to process information from tool call results,
/// navigate complex decision trees, and improve the quality of responses in multi-step scenarios.
///
/// This is a beta feature that can be enabled/disabled via settings:
/// `q settings chat.enableThinking true`
#[derive(Debug, Clone, Deserialize)]
pub struct Think {
/// The thought content that the model wants to process
pub thought: String,
}

impl Think {
/// Checks if the thinking feature is enabled in settings
pub fn is_enabled() -> bool {
// Default to enabled if setting doesn't exist or can't be read
settings::get_bool_or("chat.enableThinking", true)
}

/// Queues up a description of the think tool for the user
pub fn queue_description(think: &Think, updates: &mut impl Write) -> Result<()> {
// Only show a description if there's actual thought content
if !think.thought.trim().is_empty() {
// Show a preview of the thought that will be displayed
queue!(
updates,
style::SetForegroundColor(Color::Blue),
style::Print("I'll share my reasoning process: "),
style::SetForegroundColor(Color::Reset),
style::Print(&think.thought),
style::Print("\n")
)?;
}
Ok(())
}

/// Invokes the think tool. This doesn't actually perform any system operations,
/// it's purely for the model's internal reasoning process.
pub async fn invoke(&self, _updates: &mut impl Write) -> Result<InvokeOutput> {
// The think tool always returns an empty output because:
// 1. When enabled with content: We've already shown the thought in queue_description
// 2. When disabled or empty: Nothing should be shown
Ok(InvokeOutput {
output: OutputKind::Text(String::new()),
})
}

/// Validates the thought - accepts empty thoughts
pub async fn validate(&mut self, _ctx: &fig_os_shim::Context) -> Result<()> {
// We accept empty thoughts - they'll just be ignored
// This makes the tool more robust and prevents errors from blocking the model
Ok(())
}
}
14 changes: 14 additions & 0 deletions crates/q_chat/src/tools/tool_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,19 @@
},
"required": ["title"]
}
},
"q_think_tool":{
"name": "q_think_tool",
"description": "The Think Tool is an internal reasoning mechanism enabling the model to systematically approach complex tasks by logically breaking them down before responding or acting; use it specifically for multi-step problems requiring step-by-step dependencies, reasoning through multiple constraints, synthesizing results from previous tool calls, planning intricate sequences of actions, troubleshooting complex errors, or making decisions involving multiple trade-offs. Avoid using it for straightforward tasks, basic information retrieval, summaries, always clearly define the reasoning challenge, structure thoughts explicitly, consider multiple perspectives, and summarize key insights before important decisions or complex tool interactions.",
"input_schema": {
"type": "object",
"properties": {
"thought": {
"type": "string",
"description": "A reflective note or intermediate reasoning step."
}
},
"required": ["thought"]
}
}
}
1 change: 1 addition & 0 deletions crates/q_cli/src/cli/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl SettingsArgs {

Ok(ExitCode::SUCCESS)
},

None => match &self.key {
Some(key) => match (&self.value, self.delete) {
(None, false) => match fig_settings::settings::get_value(key)? {
Expand Down
Loading