thinking_tool.rs

 1use std::sync::Arc;
 2
 3use crate::schema::json_schema_for;
 4use anyhow::{Result, anyhow};
 5use assistant_tool::{ActionLog, Tool, ToolResult};
 6use gpui::{AnyWindowHandle, App, Entity, Task};
 7use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
 8use project::Project;
 9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use ui::IconName;
12
13#[derive(Debug, Serialize, Deserialize, JsonSchema)]
14pub struct ThinkingToolInput {
15    /// Content to think about. This should be a description of what to think about or
16    /// a problem to solve.
17    content: String,
18}
19
20pub struct ThinkingTool;
21
22impl Tool for ThinkingTool {
23    fn name(&self) -> String {
24        "thinking".to_string()
25    }
26
27    fn needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
28        false
29    }
30
31    fn description(&self) -> String {
32        include_str!("./thinking_tool/description.md").to_string()
33    }
34
35    fn icon(&self) -> IconName {
36        IconName::LightBulb
37    }
38
39    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
40        json_schema_for::<ThinkingToolInput>(format)
41    }
42
43    fn ui_text(&self, _input: &serde_json::Value) -> String {
44        "Thinking".to_string()
45    }
46
47    fn run(
48        self: Arc<Self>,
49        input: serde_json::Value,
50        _request: Arc<LanguageModelRequest>,
51        _project: Entity<Project>,
52        _action_log: Entity<ActionLog>,
53        _model: Arc<dyn LanguageModel>,
54        _window: Option<AnyWindowHandle>,
55        _cx: &mut App,
56    ) -> ToolResult {
57        // This tool just "thinks out loud" and doesn't perform any actions.
58        Task::ready(match serde_json::from_value::<ThinkingToolInput>(input) {
59            Ok(_input) => Ok("Finished thinking.".to_string().into()),
60            Err(err) => Err(anyhow!(err)),
61        })
62        .into()
63    }
64}