assistant_tool.rs

 1mod action_log;
 2mod tool_registry;
 3mod tool_working_set;
 4
 5use anyhow::Result;
 6use gpui::{App, Entity, SharedString, Task};
 7use language_model::LanguageModelRequestMessage;
 8use project::Project;
 9use std::sync::Arc;
10
11pub use crate::action_log::*;
12pub use crate::tool_registry::*;
13pub use crate::tool_working_set::*;
14
15pub fn init(cx: &mut App) {
16    ToolRegistry::default_global(cx);
17}
18
19#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
20pub enum ToolSource {
21    /// A native tool built-in to Zed.
22    Native,
23    /// A tool provided by a context server.
24    ContextServer { id: SharedString },
25}
26
27/// A tool that can be used by a language model.
28pub trait Tool: 'static + Send + Sync {
29    /// Returns the name of the tool.
30    fn name(&self) -> String;
31
32    /// Returns the description of the tool.
33    fn description(&self) -> String;
34
35    /// Returns the source of the tool.
36    fn source(&self) -> ToolSource {
37        ToolSource::Native
38    }
39
40    /// Returns the JSON schema that describes the tool's input.
41    fn input_schema(&self) -> serde_json::Value {
42        serde_json::Value::Object(serde_json::Map::default())
43    }
44
45    /// Runs the tool with the provided input.
46    fn run(
47        self: Arc<Self>,
48        input: serde_json::Value,
49        messages: &[LanguageModelRequestMessage],
50        project: Entity<Project>,
51        action_log: Entity<ActionLog>,
52        cx: &mut App,
53    ) -> Task<Result<String>>;
54}