From 0fb3f5eb93ee2e78da0ddad47282044c0200b881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=E2=82=82=E2=82=82H=E2=82=82=E2=82=85NO=E2=82=86?= <96930391+Sisyphbaous-DT-Project@users.noreply.github.com> Date: Tue, 9 Jun 2026 10:11:20 +0800 Subject: [PATCH] feat: display current weekday information (#8669) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在现实世界时间感知提示中追加 Weekday 字段,让模型能直接获取今天是星期几。\n\n使用固定英文星期表避免受系统 locale 影响,并补充单测覆盖输出格式。 Co-authored-by: C₂₂H₂₅NO₆ --- astrbot/core/astr_main_agent.py | 22 +++++++++++++------ tests/unit/test_astr_main_agent.py | 34 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index ee007140b..cee6e9e27 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -115,6 +115,15 @@ from astrbot.core.utils.quoted_message_parser import ( from astrbot.core.utils.string_utils import normalize_and_dedupe_strings LLM_ERROR_MESSAGE_EXTRA_KEY = "_llm_error_message" +WEEKDAY_NAMES = ( + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", +) WEB_SEARCH_CITATION_TOOL_NAMES = frozenset( { "web_search_baidu", @@ -874,18 +883,17 @@ def _append_system_reminders( system_parts.append(f"Group name: {group_name}") if cfg.get("datetime_system_prompt"): - current_time = None + now = None if timezone: try: now = datetime.datetime.now(zoneinfo.ZoneInfo(timezone)) - current_time = now.strftime("%Y-%m-%d %H:%M (%Z)") except Exception as exc: # noqa: BLE001 logger.error("时区设置错误: %s, 使用本地时区", exc) - if not current_time: - current_time = ( - datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M (%Z)") - ) - system_parts.append(f"Current datetime: {current_time}") + if now is None: + now = datetime.datetime.now().astimezone() + current_time = now.strftime("%Y-%m-%d %H:%M (%Z)") + weekday = WEEKDAY_NAMES[now.weekday()] + system_parts.append(f"Current datetime: {current_time}, Weekday: {weekday}") if system_parts: system_content = ( diff --git a/tests/unit/test_astr_main_agent.py b/tests/unit/test_astr_main_agent.py index 84f354894..31c80e09e 100644 --- a/tests/unit/test_astr_main_agent.py +++ b/tests/unit/test_astr_main_agent.py @@ -1,5 +1,6 @@ """Tests for astr_main_agent module.""" +import datetime import os from unittest.mock import AsyncMock, MagicMock, patch @@ -134,6 +135,39 @@ def _setup_conversation_for_build(conv_mgr, cid: str = "conv-id") -> MagicMock: return conversation +def test_append_system_reminders_includes_weekday(mock_event): + """Test datetime reminder includes weekday information.""" + req = ProviderRequest(prompt="Hello") + fixed_now = datetime.datetime( + 2026, + 6, + 8, + 12, + 34, + tzinfo=datetime.timezone.utc, + ) + + class FixedDateTime(datetime.datetime): + @classmethod + def now(cls, tz=None): + if tz: + return fixed_now.astimezone(tz) + return fixed_now + + with patch("astrbot.core.astr_main_agent.datetime.datetime", FixedDateTime): + ama._append_system_reminders( + mock_event, + req, + {"datetime_system_prompt": True}, + "UTC", + ) + + assert [part.text for part in req.extra_user_content_parts] == [ + "Current datetime: " + "2026-06-08 12:34 (UTC), Weekday: Monday" + ] + + class TestMainAgentBuildConfig: """Tests for MainAgentBuildConfig dataclass."""