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 run(self: Arc<Self>, input: Self::Input, _cx: &mut App) -> Task<Result<String>> {
 23        Task::ready(Ok(input.text))
 24    }
 25}
 26
 27/// A tool that waits for a specified delay
 28#[derive(JsonSchema, Serialize, Deserialize)]
 29pub struct DelayToolInput {
 30    /// The delay in milliseconds.
 31    ms: u64,
 32}
 33
 34pub struct DelayTool;
 35
 36impl AgentTool for DelayTool {
 37    type Input = DelayToolInput;
 38
 39    fn name(&self) -> SharedString {
 40        "delay".into()
 41    }
 42
 43    fn run(self: Arc<Self>, input: Self::Input, cx: &mut App) -> Task<Result<String>>
 44    where
 45        Self: Sized,
 46    {
 47        cx.foreground_executor().spawn(async move {
 48            smol::Timer::after(Duration::from_millis(input.ms)).await;
 49            Ok("Ding".to_string())
 50        })
 51    }
 52}
 53
 54#[derive(JsonSchema, Serialize, Deserialize)]
 55pub struct InfiniteToolInput {}
 56
 57pub struct InfiniteTool;
 58
 59impl AgentTool for InfiniteTool {
 60    type Input = InfiniteToolInput;
 61
 62    fn name(&self) -> SharedString {
 63        "infinite".into()
 64    }
 65
 66    fn run(self: Arc<Self>, _input: Self::Input, cx: &mut App) -> Task<Result<String>> {
 67        cx.foreground_executor().spawn(async move {
 68            future::pending::<()>().await;
 69            unreachable!()
 70        })
 71    }
 72}
 73
 74/// A tool that takes an object with map from letters to random words starting with that letter.
 75/// All fiealds are required! Pass a word for every letter!
 76#[derive(JsonSchema, Serialize, Deserialize)]
 77pub struct WordListInput {
 78    /// Provide a random word that starts with A.
 79    a: Option<String>,
 80    /// Provide a random word that starts with B.
 81    b: Option<String>,
 82    /// Provide a random word that starts with C.
 83    c: Option<String>,
 84    /// Provide a random word that starts with D.
 85    d: Option<String>,
 86    /// Provide a random word that starts with E.
 87    e: Option<String>,
 88    /// Provide a random word that starts with F.
 89    f: Option<String>,
 90    /// Provide a random word that starts with G.
 91    g: Option<String>,
 92}
 93
 94pub struct WordListTool;
 95
 96impl AgentTool for WordListTool {
 97    type Input = WordListInput;
 98
 99    fn name(&self) -> SharedString {
100        "word_list".into()
101    }
102
103    fn run(self: Arc<Self>, _input: Self::Input, _cx: &mut App) -> Task<Result<String>> {
104        Task::ready(Ok("ok".to_string()))
105    }
106}