use crate::error::Result;
use crate::llm::tools::{FunctionDescriptor, LlmTool, ToolDescriptor};
use async_trait::async_trait;
use serde_json::{json, Value};
use std::collections::HashMap;
#[derive(Clone)]
pub struct CurrentDatetimeTool;
impl CurrentDatetimeTool {
pub fn new() -> Self {
Self
}
}
impl Default for CurrentDatetimeTool {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl LlmTool for CurrentDatetimeTool {
async fn run(
&self,
args: &HashMap<String, Value>,
_ctx: &crate::llm::tools::ToolRunCtx,
) -> Result<Value> {
let format_string = args
.get("format_string")
.and_then(|v| v.as_str())
.unwrap_or("%Y-%m-%d %H:%M:%S");
let now = super::datetime::now();
let formatted_time = super::datetime::format(&now, format_string)?;
let timestamp = now.timestamp().as_second();
let timezone = super::datetime::format(&now, "%Z")?;
Ok(json!({
"current_datetime": formatted_time,
"timestamp": timestamp,
"timezone": timezone
}))
}
fn descriptor(&self) -> ToolDescriptor {
ToolDescriptor {
r#type: "function".to_string(),
function: FunctionDescriptor {
name: "get_current_datetime".to_string(),
description: "Get the current date and time. Useful when you need to know the current time or date.".to_string(),
parameters: json!({
"type": "object",
"properties": {
"format_string": {
"type": "string",
"description": "Format string for the datetime (e.g., '%Y-%m-%d %H:%M:%S', '%A, %B %d, %Y'). Default is ISO format."
}
},
"required": []
}),
},
}
}
fn clone_box(&self) -> Box<dyn LlmTool> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_descriptor() {
let tool = CurrentDatetimeTool::new();
let descriptor = tool.descriptor();
assert_eq!(descriptor.r#type, "function");
assert_eq!(descriptor.function.name, "get_current_datetime");
assert!(descriptor.function.description.contains("current date and time"));
}
#[test]
fn test_tool_matches() {
let tool = CurrentDatetimeTool::new();
assert!(tool.matches("get_current_datetime"));
assert!(!tool.matches("other_tool"));
}
}