From 480c60c0a738f24d4bb3b9fc33f45e7f32ca5c09 Mon Sep 17 00:00:00 2001 From: cowagent Date: Wed, 4 Feb 2026 22:27:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8A=A8=E6=80=81=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=8F=90=E7=A4=BA=E8=AF=8D=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E4=BF=A1=E6=81=AF=EF=BC=88=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: - system_prompt 在 Agent 初始化时固定,导致模型获取的时间信息过时 - 长时间运行的会话中,模型对时间判断不准确 解决方案: - 在 get_full_system_prompt() 中添加动态更新逻辑 - 每次获取系统提示词时,使用正则表达式替换运行时信息中的时间戳 - 保持其他运行时信息(模型、工作空间等)不变 测试: - 创建测试脚本验证时间动态更新功能 - 等待3秒后时间正确更新(22:19:45 -> 22:19:48) --- agent/protocol/agent.py | 49 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/agent/protocol/agent.py b/agent/protocol/agent.py index 1b031e0..f5cb9c9 100644 --- a/agent/protocol/agent.py +++ b/agent/protocol/agent.py @@ -1,6 +1,8 @@ import json import time import threading +import datetime +import re from common.log import logger from agent.protocol.models import LLMRequest, LLMModel @@ -105,8 +107,51 @@ class Agent: :return: Complete system prompt """ # Skills are now included in system_prompt by PromptBuilder - # No need to append them here - return self.system_prompt + # Update runtime info (timestamp) dynamically before returning + return self._update_runtime_info(self.system_prompt) + + def _update_runtime_info(self, prompt: str) -> str: + """ + Update runtime information (timestamp) in the system prompt. + + This ensures the model always has the current time, even if the + agent was initialized hours ago. + + :param prompt: Original system prompt + :return: Updated system prompt with current time + """ + # Find the runtime info section + runtime_section_pattern = r'(## 运行时信息\s*\n\s*\n)(当前时间: )([^\n]+)(\s+星期[一二三四五六日])(\s+\([^)]+\))?' + + # Get current time info + now = datetime.datetime.now() + + # Get timezone + try: + offset = -time.timezone if not time.daylight else -time.altzone + hours = offset // 3600 + minutes = (offset % 3600) // 60 + timezone_name = f"UTC{hours:+03d}:{minutes:02d}" if minutes else f"UTC{hours:+03d}" + except Exception: + timezone_name = "UTC" + + # Chinese weekday mapping + weekday_map = { + 'Monday': '星期一', 'Tuesday': '星期二', 'Wednesday': '星期三', + 'Thursday': '星期四', 'Friday': '星期五', 'Saturday': '星期六', 'Sunday': '星期日' + } + weekday_zh = weekday_map.get(now.strftime("%A"), now.strftime("%A")) + + # Build new time string + new_time = now.strftime("%Y-%m-%d %H:%M:%S") + + # Replace the time in the prompt + def replace_time(match): + return f"{match.group(1)}{match.group(2)}{new_time} {weekday_zh} ({timezone_name})" + + updated_prompt = re.sub(runtime_section_pattern, replace_time, prompt) + + return updated_prompt def refresh_skills(self): """Refresh the loaded skills."""