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(&self, _input: Result<Self::Input, serde_json::Value>) -> SharedString {
45        "Get current time".into()
46    }
47
48    fn run(
49        self: Arc<Self>,
50        input: Self::Input,
51        _event_stream: ToolCallEventStream,
52        _cx: &mut App,
53    ) -> Task<Result<String>> {
54        let now = match input.timezone {
55            Timezone::Utc => Utc::now().to_rfc3339(),
56            Timezone::Local => Local::now().to_rfc3339(),
57        };
58        Task::ready(Ok(format!("The current datetime is {now}.")))
59    }
60}