-
Notifications
You must be signed in to change notification settings - Fork 87
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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` | ||
#[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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
.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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't include |
||
// 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()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enable_thinking
should bechat.enableThinking