File size: 1,968 Bytes
cc7440f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Return the current date and time for orchestrator context."""

from __future__ import annotations

from datetime import datetime, timezone as dt_timezone
from typing import Any, Dict

try:
    from zoneinfo import ZoneInfo
except ImportError:  # pragma: no cover
    ZoneInfo = None  # type: ignore

TOOL_DEFINITION = {
    "type": "function",
    "function": {
        "name": "get_current_datetime",
        "description": (
            "Get the current date and time in UTC and in a configured local timezone. "
            "Use when the user asks about today, deadlines, timelines, schedules, "
            "or when accurate temporal context improves security guidance."
        ),
        "parameters": {
            "type": "object",
            "properties": {
                "timezone": {
                    "type": "string",
                    "description": (
                        "IANA timezone name (e.g. America/Los_Angeles). "
                        "Optional; defaults to the app-configured timezone."
                    ),
                },
            },
            "required": [],
        },
    },
}


async def execute(name: str = "", timezone: str | None = None, **_: Any) -> Dict[str, Any]:
    from app.config import get_settings

    cfg = get_settings().tools.get_tool_config("current_datetime")
    tz_name = timezone or cfg.get("default_timezone") or "UTC"
    tz = dt_timezone.utc
    if ZoneInfo is not None:
        try:
            tz = ZoneInfo(tz_name)
        except Exception:
            tz_name = "UTC"
            tz = dt_timezone.utc

    now_utc = datetime.now(dt_timezone.utc)
    now_local = now_utc.astimezone(tz)

    return {
        "utc_iso": now_utc.isoformat(),
        "local_iso": now_local.isoformat(),
        "local_timezone": tz_name,
        "local_weekday": now_local.strftime("%A"),
        "local_date": now_local.strftime("%Y-%m-%d"),
        "local_time": now_local.strftime("%H:%M:%S"),
    }