mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
feat: display current weekday information (#8669)
在现实世界时间感知提示中追加 Weekday 字段,让模型能直接获取今天是星期几。\n\n使用固定英文星期表避免受系统 locale 影响,并补充单测覆盖输出格式。 Co-authored-by: C₂₂H₂₅NO₆ <Sisyphbaous-DT-Project@users.noreply.github.com>
This commit is contained in:
@@ -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 = (
|
||||
|
||||
@@ -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] == [
|
||||
"<system_reminder>Current datetime: "
|
||||
"2026-06-08 12:34 (UTC), Weekday: Monday</system_reminder>"
|
||||
]
|
||||
|
||||
|
||||
class TestMainAgentBuildConfig:
|
||||
"""Tests for MainAgentBuildConfig dataclass."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user