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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion crates/q_cli/src/cli/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3071,7 +3071,14 @@ fn create_stream(model_responses: serde_json::Value) -> StreamingClient {

/// Returns all tools supported by Q chat.
pub fn load_tools() -> Result<HashMap<String, ToolSpec>> {
Ok(serde_json::from_str(include_str!("tools/tool_index.json"))?)
let mut tools: HashMap<String, ToolSpec> = serde_json::from_str(include_str!("tools/tool_index.json"))?;

// Only include the think tool if the feature is enabled
if !tools::think::Think::should_include_in_tools() {
tools.remove("think");
}

Ok(tools)
}

#[cfg(test)]
Expand Down
10 changes: 9 additions & 1 deletion crates/q_cli/src/cli/chat/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,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 @@ -24,6 +24,7 @@ use fs_read::FsRead;
use fs_write::FsWrite;
use gh_issue::GhIssue;
use serde::Deserialize;
use think::Think;
use use_aws::UseAws;

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

impl Tool {
Expand All @@ -52,6 +54,7 @@ impl Tool {
Tool::ExecuteBash(_) => "execute_bash",
Tool::UseAws(_) => "use_aws",
Tool::GhIssue(_) => "gh_issue",
Tool::Think(_) => "model_think_tool",
}
}

Expand All @@ -63,6 +66,7 @@ impl Tool {
Tool::ExecuteBash(execute_bash) => execute_bash.requires_acceptance(),
Tool::UseAws(use_aws) => use_aws.requires_acceptance(),
Tool::GhIssue(_) => false,
Tool::Think(_) => false,
}
}

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

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

Expand All @@ -96,6 +102,7 @@ impl Tool {
Tool::ExecuteBash(execute_bash) => execute_bash.validate(ctx).await,
Tool::UseAws(use_aws) => use_aws.validate(ctx).await,
Tool::GhIssue(gh_issue) => gh_issue.validate(ctx).await,
Tool::Think(think) => think.validate(ctx).await,
}
}
}
Expand All @@ -118,6 +125,7 @@ impl TryFrom<AssistantToolUse> for Tool {
"execute_bash" => Self::ExecuteBash(serde_json::from_value::<ExecuteBash>(value.args).map_err(map_err)?),
"use_aws" => Self::UseAws(serde_json::from_value::<UseAws>(value.args).map_err(map_err)?),
"report_issue" => Self::GhIssue(serde_json::from_value::<GhIssue>(value.args).map_err(map_err)?),
"think" => Self::Think(serde_json::from_value::<Think>(value.args).map_err(map_err)?),
unknown => {
return Err(ToolUseResult {
tool_use_id: value.id,
Expand Down
86 changes: 86 additions & 0 deletions crates/q_cli/src/cli/chat/tools/think.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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 enable_thinking true`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enable_thinking should be chat.enableThinking

#[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
fn is_enabled() -> bool {
// Default to enabled if setting doesn't exist or can't be read
settings::get_value("chat.enableThinking")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use settings::get_bool_or("chat.enableThinking", true) instead

.map(|val| val.and_then(|v| v.as_bool()).unwrap_or(true))
.unwrap_or(true)
}

/// Checks if the think tool should be included in the tool list
/// This allows us to completely exclude the tool when the feature is disabled
pub fn should_include_in_tools() -> bool {
Self::is_enabled()
}

/// 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 the feature is enabled and there's actual thought content
if Self::is_enabled() && !think.thought.trim().is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't include is_enabled here or in invoke, since it's really just checking whether or not to include it in the tool list. Why not just have is_enabled and call that instead when loading the tools?

// 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> {
// Only process non-empty thoughts if the feature is enabled
if Self::is_enabled() && !self.thought.trim().is_empty() {
// We've already shown the thought in queue_description
// Just return an empty output to avoid duplication
return Ok(InvokeOutput {
output: OutputKind::Text(String::new()),
});
}

// If disabled or empty thought, return empty output
Ok(InvokeOutput {
output: OutputKind::Text(String::new()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we return the same response in both cases?

})
}

/// Validates the thought - accepts empty thoughts
/// Also checks if the thinking feature is enabled in settings
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_cli/src/cli/chat/tools/tool_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,19 @@
},
"required": ["title"]
}
},
"think":{
"name": "think",
"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