assistant_tool.rs

 1mod action_log;
 2mod tool_registry;
 3mod tool_working_set;
 4
 5use std::fmt;
 6use std::fmt::Debug;
 7use std::fmt::Formatter;
 8use std::sync::Arc;
 9
10use anyhow::Result;
11use gpui::{App, Entity, SharedString, Task};
12use icons::IconName;
13use language_model::LanguageModelRequestMessage;
14use language_model::LanguageModelToolSchemaFormat;
15use project::Project;
16
17pub use crate::action_log::*;
18pub use crate::tool_registry::*;
19pub use crate::tool_working_set::*;
20
21pub fn init(cx: &mut App) {
22    ToolRegistry::default_global(cx);
23}
24
25#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
26pub enum ToolSource {
27    /// A native tool built-in to Zed.
28    Native,
29    /// A tool provided by a context server.
30    ContextServer { id: SharedString },
31}
32
33/// A tool that can be used by a language model.
34pub trait Tool: 'static + Send + Sync {
35    /// Returns the name of the tool.
36    fn name(&self) -> String;
37
38    /// Returns the description of the tool.
39    fn description(&self) -> String;
40
41    /// Returns the icon for the tool.
42    fn icon(&self) -> IconName;
43
44    /// Returns the source of the tool.
45    fn source(&self) -> ToolSource {
46        ToolSource::Native
47    }
48
49    /// Returns true iff the tool needs the users's confirmation
50    /// before having permission to run.
51    fn needs_confirmation(&self, input: &serde_json::Value, cx: &App) -> bool;
52
53    /// Returns the JSON schema that describes the tool's input.
54    fn input_schema(&self, _: LanguageModelToolSchemaFormat) -> serde_json::Value {
55        serde_json::Value::Object(serde_json::Map::default())
56    }
57
58    /// Returns markdown to be displayed in the UI for this tool.
59    fn ui_text(&self, input: &serde_json::Value) -> String;
60
61    /// Runs the tool with the provided input.
62    fn run(
63        self: Arc<Self>,
64        input: serde_json::Value,
65        messages: &[LanguageModelRequestMessage],
66        project: Entity<Project>,
67        action_log: Entity<ActionLog>,
68        cx: &mut App,
69    ) -> Task<Result<String>>;
70}
71
72impl Debug for dyn Tool {
73    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
74        f.debug_struct("Tool").field("name", &self.name()).finish()
75    }
76}