now_tool.rs

 1use std::sync::Arc;
 2
 3use anyhow::{anyhow, Result};
 4use assistant_tool::Tool;
 5use chrono::{Local, Utc};
 6use gpui::{Task, WeakView, WindowContext};
 7use schemars::JsonSchema;
 8use serde::{Deserialize, Serialize};
 9
10#[derive(Debug, Serialize, Deserialize, JsonSchema)]
11#[serde(rename_all = "snake_case")]
12pub enum Timezone {
13    /// Use UTC for the datetime.
14    Utc,
15    /// Use local time for the datetime.
16    Local,
17}
18
19#[derive(Debug, Serialize, Deserialize, JsonSchema)]
20pub struct FileToolInput {
21    /// The timezone to use for the datetime.
22    timezone: Timezone,
23}
24
25pub struct NowTool;
26
27impl Tool for NowTool {
28    fn name(&self) -> String {
29        "now".into()
30    }
31
32    fn description(&self) -> String {
33        "Returns the current datetime in RFC 3339 format.".into()
34    }
35
36    fn input_schema(&self) -> serde_json::Value {
37        let schema = schemars::schema_for!(FileToolInput);
38        serde_json::to_value(&schema).unwrap()
39    }
40
41    fn run(
42        self: Arc<Self>,
43        input: serde_json::Value,
44        _workspace: WeakView<workspace::Workspace>,
45        _cx: &mut WindowContext,
46    ) -> Task<Result<String>> {
47        let input: FileToolInput = match serde_json::from_value(input) {
48            Ok(input) => input,
49            Err(err) => return Task::ready(Err(anyhow!(err))),
50        };
51
52        let now = match input.timezone {
53            Timezone::Utc => Utc::now().to_rfc3339(),
54            Timezone::Local => Local::now().to_rfc3339(),
55        };
56        let text = format!("The current datetime is {now}.");
57
58        Task::ready(Ok(text))
59    }
60}