test_tools.rs

  1use super::*;
  2use anyhow::Result;
  3use gpui::{App, SharedString, Task};
  4use std::future;
  5
  6/// A tool that echoes its input
  7#[derive(JsonSchema, Serialize, Deserialize)]
  8pub struct EchoToolInput {
  9    /// The text to echo.
 10    text: String,
 11}
 12
 13pub struct EchoTool;
 14
 15impl AgentTool for EchoTool {
 16    type Input = EchoToolInput;
 17
 18    fn name(&self) -> SharedString {
 19        "echo".into()
 20    }
 21
 22    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
 23        false
 24    }
 25
 26    fn run(self: Arc<Self>, input: Self::Input, _cx: &mut App) -> Task<Result<String>> {
 27        Task::ready(Ok(input.text))
 28    }
 29}
 30
 31/// A tool that waits for a specified delay
 32#[derive(JsonSchema, Serialize, Deserialize)]
 33pub struct DelayToolInput {
 34    /// The delay in milliseconds.
 35    ms: u64,
 36}
 37
 38pub struct DelayTool;
 39
 40impl AgentTool for DelayTool {
 41    type Input = DelayToolInput;
 42
 43    fn name(&self) -> SharedString {
 44        "delay".into()
 45    }
 46
 47    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
 48        false
 49    }
 50
 51    fn run(self: Arc<Self>, input: Self::Input, cx: &mut App) -> Task<Result<String>>
 52    where
 53        Self: Sized,
 54    {
 55        cx.foreground_executor().spawn(async move {
 56            smol::Timer::after(Duration::from_millis(input.ms)).await;
 57            Ok("Ding".to_string())
 58        })
 59    }
 60}
 61
 62#[derive(JsonSchema, Serialize, Deserialize)]
 63pub struct ToolRequiringPermissionInput {}
 64
 65pub struct ToolRequiringPermission;
 66
 67impl AgentTool for ToolRequiringPermission {
 68    type Input = ToolRequiringPermissionInput;
 69
 70    fn name(&self) -> SharedString {
 71        "tool_requiring_permission".into()
 72    }
 73
 74    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
 75        true
 76    }
 77
 78    fn run(self: Arc<Self>, _input: Self::Input, cx: &mut App) -> Task<Result<String>>
 79    where
 80        Self: Sized,
 81    {
 82        cx.foreground_executor()
 83            .spawn(async move { Ok("Allowed".to_string()) })
 84    }
 85}
 86
 87#[derive(JsonSchema, Serialize, Deserialize)]
 88pub struct InfiniteToolInput {}
 89
 90pub struct InfiniteTool;
 91
 92impl AgentTool for InfiniteTool {
 93    type Input = InfiniteToolInput;
 94
 95    fn name(&self) -> SharedString {
 96        "infinite".into()
 97    }
 98
 99    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
100        false
101    }
102
103    fn run(self: Arc<Self>, _input: Self::Input, cx: &mut App) -> Task<Result<String>> {
104        cx.foreground_executor().spawn(async move {
105            future::pending::<()>().await;
106            unreachable!()
107        })
108    }
109}
110
111/// A tool that takes an object with map from letters to random words starting with that letter.
112/// All fiealds are required! Pass a word for every letter!
113#[derive(JsonSchema, Serialize, Deserialize)]
114pub struct WordListInput {
115    /// Provide a random word that starts with A.
116    a: Option<String>,
117    /// Provide a random word that starts with B.
118    b: Option<String>,
119    /// Provide a random word that starts with C.
120    c: Option<String>,
121    /// Provide a random word that starts with D.
122    d: Option<String>,
123    /// Provide a random word that starts with E.
124    e: Option<String>,
125    /// Provide a random word that starts with F.
126    f: Option<String>,
127    /// Provide a random word that starts with G.
128    g: Option<String>,
129}
130
131pub struct WordListTool;
132
133impl AgentTool for WordListTool {
134    type Input = WordListInput;
135
136    fn name(&self) -> SharedString {
137        "word_list".into()
138    }
139
140    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
141        false
142    }
143
144    fn run(self: Arc<Self>, _input: Self::Input, _cx: &mut App) -> Task<Result<String>> {
145        Task::ready(Ok("ok".to_string()))
146    }
147}