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    const NAME: &'static str = "now";
37
38    fn kind() -> acp::ToolKind {
39        acp::ToolKind::Other
40    }
41
42    fn initial_title(
43        &self,
44        _input: Result<Self::Input, serde_json::Value>,
45        _cx: &mut App,
46    ) -> SharedString {
47        "Get current time".into()
48    }
49
50    fn run(
51        self: Arc<Self>,
52        input: Self::Input,
53        _event_stream: ToolCallEventStream,
54        _cx: &mut App,
55    ) -> Task<Result<String>> {
56        let now = match input.timezone {
57            Timezone::Utc => Utc::now().to_rfc3339(),
58            Timezone::Local => Local::now().to_rfc3339(),
59        };
60        Task::ready(Ok(format!("The current datetime is {now}.")))
61    }
62}