now_tool.rs

 1use std::sync::Arc;
 2
 3use anyhow::{anyhow, Result};
 4use assistant_tool::Tool;
 5use chrono::{Local, Utc};
 6use gpui::{App, Task, WeakEntity, Window};
 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 NowToolInput {
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. Only use this tool when the user specifically asks for it or the current task would benefit from knowing the current datetime.".into()
34    }
35
36    fn input_schema(&self) -> serde_json::Value {
37        let schema = schemars::schema_for!(NowToolInput);
38        serde_json::to_value(&schema).unwrap()
39    }
40
41    fn run(
42        self: Arc<Self>,
43        input: serde_json::Value,
44        _workspace: WeakEntity<workspace::Workspace>,
45        _window: &mut Window,
46        _cx: &mut App,
47    ) -> Task<Result<String>> {
48        let input: NowToolInput = match serde_json::from_value(input) {
49            Ok(input) => input,
50            Err(err) => return Task::ready(Err(anyhow!(err))),
51        };
52
53        let now = match input.timezone {
54            Timezone::Utc => Utc::now().to_rfc3339(),
55            Timezone::Local => Local::now().to_rfc3339(),
56        };
57        let text = format!("The current datetime is {now}.");
58
59        Task::ready(Ok(text))
60    }
61}