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