now_tool.rs

 1use std::sync::Arc;
 2
 3use agent_client_protocol as acp;
 4use anyhow::Result;
 5use chrono::{Local, Utc};
 6use gpui::{App, SharedString, Task};
 7use schemars::JsonSchema;
 8use serde::{Deserialize, Serialize};
 9
10use crate::{AgentTool, ToolCallEventStream};
11
12#[derive(Debug, Serialize, Deserialize, JsonSchema)]
13#[serde(rename_all = "snake_case")]
14#[schemars(inline)]
15pub enum Timezone {
16    /// Use UTC for the datetime.
17    Utc,
18    /// Use local time for the datetime.
19    Local,
20}
21
22/// Returns the current datetime in RFC 3339 format.
23/// Only use this tool when the user specifically asks for it or the current task would benefit from knowing the current datetime.
24#[derive(Debug, Serialize, Deserialize, JsonSchema)]
25pub struct NowToolInput {
26    /// The timezone to use for the datetime.
27    timezone: Timezone,
28}
29
30pub struct NowTool;
31
32impl AgentTool for NowTool {
33    type Input = NowToolInput;
34    type Output = String;
35
36    fn name() -> &'static str {
37        "now"
38    }
39
40    fn kind() -> acp::ToolKind {
41        acp::ToolKind::Other
42    }
43
44    fn initial_title(
45        &self,
46        _input: Result<Self::Input, serde_json::Value>,
47        _cx: &mut App,
48    ) -> SharedString {
49        "Get current time".into()
50    }
51
52    fn run(
53        self: Arc<Self>,
54        input: Self::Input,
55        _event_stream: ToolCallEventStream,
56        _cx: &mut App,
57    ) -> Task<Result<String>> {
58        let now = match input.timezone {
59            Timezone::Utc => Utc::now().to_rfc3339(),
60            Timezone::Local => Local::now().to_rfc3339(),
61        };
62        Task::ready(Ok(format!("The current datetime is {now}.")))
63    }
64}