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, _: &Entity<Project>, _: &App) -> bool {
28        false
29    }
30
31    fn may_perform_edits(&self) -> bool {
32        false
33    }
34
35    fn description(&self) -> String {
36        include_str!("./thinking_tool/description.md").to_string()
37    }
38
39    fn icon(&self) -> IconName {
40        IconName::ToolThink
41    }
42
43    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
44        json_schema_for::<ThinkingToolInput>(format)
45    }
46
47    fn ui_text(&self, _input: &serde_json::Value) -> String {
48        "Thinking".to_string()
49    }
50
51    fn run(
52        self: Arc<Self>,
53        input: serde_json::Value,
54        _request: Arc<LanguageModelRequest>,
55        _project: Entity<Project>,
56        _action_log: Entity<ActionLog>,
57        _model: Arc<dyn LanguageModel>,
58        _window: Option<AnyWindowHandle>,
59        _cx: &mut App,
60    ) -> ToolResult {
61        // This tool just "thinks out loud" and doesn't perform any actions.
62        Task::ready(match serde_json::from_value::<ThinkingToolInput>(input) {
63            Ok(_input) => Ok("Finished thinking.".to_string().into()),
64            Err(err) => Err(anyhow!(err)),
65        })
66        .into()
67    }
68}