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