date format fix

This commit is contained in:
Roberto
2026-04-26 21:06:38 +02:00
parent 2c7cac9e03
commit cb8f56d909
5 changed files with 85 additions and 9 deletions

View File

@@ -55,6 +55,42 @@ def _language_instruction(context: dict[str, Any]) -> str:
f"All your output text must be written in {lang}."
)
def _datetime_context_injection(context: dict[str, Any]) -> str:
"""Build a system-prompt paragraph with current timestamp, user timezone, and format prefs."""
fp = context.get("format_prefs")
if not fp or not isinstance(fp, dict):
return ""
try:
from zoneinfo import ZoneInfo
from datetime import datetime as _dt, timezone as _utc
tz_name: str = str(fp.get("timezone") or "UTC")
now_iso: str = str(fp.get("now_iso") or "")
date_fmt: str = str(fp.get("date_format") or "dd/MM/yyyy")
time_fmt: str = str(fp.get("time_format") or "24h")
if now_iso:
now_utc = _dt.fromisoformat(now_iso.replace("Z", "+00:00"))
else:
now_utc = _dt.now(_utc.utc)
tz = ZoneInfo(tz_name)
now_local = now_utc.astimezone(tz)
today_local = now_local.strftime("%Y-%m-%d")
weekday_local = now_local.strftime("%A")
return (
f"\n\nCurrent instant: {now_utc.isoformat()}. "
f"User local date: {today_local} ({weekday_local}). "
f"Timezone: {tz_name}. "
f"Display preference: dateFormat={date_fmt}, timeFormat={time_fmt}. "
f"When calling tools with date fields, always pass integer Unix milliseconds (ms since epoch, UTC). "
f"When calling list_tasks_due_today or list_timelines_today, always pass user_timezone=\"{tz_name}\". "
f"When presenting dates to the user in chat, format using the display preference above."
)
except Exception:
return ""
def _proactive_hints_injection(context: dict[str, Any]) -> str:
"""Return a system-prompt paragraph listing proactive behavioral hints.
@@ -938,6 +974,7 @@ async def run_home(user_id: str, message: str, context: dict[str, Any]) -> str:
)
system_prompt += _relational_memory_injection(context)
system_prompt += _proactive_hints_injection(context)
system_prompt += _datetime_context_injection(context)
system_prompt += _language_instruction(context)
response = await _run_single_agent(
user_id=user_id,
@@ -958,6 +995,7 @@ async def run_floating(user_id: str, message: str, context: dict[str, Any]) -> t
)
system_prompt += _relational_memory_injection(context)
system_prompt += _proactive_hints_injection(context)
system_prompt += _datetime_context_injection(context)
system_prompt += _language_instruction(context)
response = await _run_single_agent(
user_id=user_id,
@@ -984,6 +1022,7 @@ async def run_home_stream(
)
system_prompt += _relational_memory_injection(context)
system_prompt += _proactive_hints_injection(context)
system_prompt += _datetime_context_injection(context)
system_prompt += _language_instruction(context)
text_chunks: list[str] = []
async for event in _run_single_agent_stream(
@@ -1019,6 +1058,7 @@ async def run_floating_stream(
)
system_prompt += _relational_memory_injection(context)
system_prompt += _proactive_hints_injection(context)
system_prompt += _datetime_context_injection(context)
system_prompt += _language_instruction(context)
sanitizer = _FloatingStreamSanitizer()
emitted_sanitized = False