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