From f859d99d91633198687bf73ef73411d2f5527591 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 27 Mar 2026 21:55:09 +0800 Subject: [PATCH] fix current_date --- app/agent/__init__.py | 67 ++++++++++++++++++------------------ app/agent/prompt/__init__.py | 4 ++- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/app/agent/__init__.py b/app/agent/__init__.py index 6c438580..cd92e500 100644 --- a/app/agent/__init__.py +++ b/app/agent/__init__.py @@ -1,7 +1,6 @@ import asyncio import traceback import uuid -from time import strftime from dataclasses import dataclass from typing import Callable, Dict, List, Optional @@ -42,12 +41,12 @@ class MoviePilotAgent: """ def __init__( - self, - session_id: str, - user_id: str = None, - channel: str = None, - source: str = None, - username: str = None, + self, + session_id: str, + user_id: str = None, + channel: str = None, + source: str = None, + username: str = None, ): self.session_id = session_id self.user_id = user_id @@ -92,10 +91,10 @@ class MoviePilotAgent: if block.get("thought"): continue if block.get("type") in ( - "thinking", - "reasoning_content", - "reasoning", - "thought", + "thinking", + "reasoning_content", + "reasoning", + "thought", ): continue if block.get("type") == "text": @@ -126,7 +125,7 @@ class MoviePilotAgent: # 系统提示词 system_prompt = prompt_manager.get_agent_prompt( channel=self.channel - ).format(current_date=strftime("%Y-%m-%d")) + ) # LLM 模型(用于 agent 执行) llm = self._initialize_llm() @@ -201,7 +200,7 @@ class MoviePilotAgent: return error_message async def _stream_agent_tokens( - self, agent, messages: dict, config: dict, on_token: Callable[[str], None] + self, agent, messages: dict, config: dict, on_token: Callable[[str], None] ): """ 流式运行智能体,过滤工具调用token和思考内容,将模型生成的内容通过回调输出。 @@ -211,18 +210,18 @@ class MoviePilotAgent: :param on_token: 收到有效 token 时的回调 """ async for chunk in agent.astream( - messages, - stream_mode="messages", - config=config, - subgraphs=False, - version="v2", + messages, + stream_mode="messages", + config=config, + subgraphs=False, + version="v2", ): if chunk["type"] == "messages": token, metadata = chunk["data"] if ( - token - and hasattr(token, "tool_call_chunks") - and not token.tool_call_chunks + token + and hasattr(token, "tool_call_chunks") + and not token.tool_call_chunks ): # 跳过模型思考/推理内容(如 DeepSeek R1 的 reasoning_content) additional = getattr(token, "additional_kwargs", None) @@ -423,13 +422,13 @@ class AgentManager: self.active_agents.clear() async def process_message( - self, - session_id: str, - user_id: str, - message: str, - channel: str = None, - source: str = None, - username: str = None, + self, + session_id: str, + user_id: str, + message: str, + channel: str = None, + source: str = None, + username: str = None, ) -> str: """ 处理用户消息:将消息放入会话队列,按顺序依次处理。 @@ -453,8 +452,8 @@ class AgentManager: # 如果队列中已有等待的消息,通知用户消息已排队 if queue_size > 0 or ( - session_id in self._session_workers - and not self._session_workers[session_id].done() + session_id in self._session_workers + and not self._session_workers[session_id].done() ): logger.info( f"会话 {session_id} 有任务正在处理,消息已排队等待 " @@ -466,8 +465,8 @@ class AgentManager: # 确保该会话有一个worker在运行 if ( - session_id not in self._session_workers - or self._session_workers[session_id].done() + session_id not in self._session_workers + or self._session_workers[session_id].done() ): self._session_workers[session_id] = asyncio.create_task( self._session_worker(session_id) @@ -508,8 +507,8 @@ class AgentManager: await self._session_workers.pop(session_id, None) # 如果队列为空,清理队列 if ( - session_id in self._session_queues - and self._session_queues[session_id].empty() + session_id in self._session_queues + and self._session_queues[session_id].empty() ): self._session_queues.pop(session_id, None) diff --git a/app/agent/prompt/__init__.py b/app/agent/prompt/__init__.py index baa569d8..dd98aa59 100644 --- a/app/agent/prompt/__init__.py +++ b/app/agent/prompt/__init__.py @@ -1,6 +1,7 @@ """提示词管理器""" from pathlib import Path +from time import strftime from typing import Dict from app.core.config import settings @@ -81,7 +82,8 @@ class PromptManager: # 始终替换占位符,避免后续 .format() 时因残留花括号报 KeyError base_prompt = base_prompt.format( markdown_spec=markdown_spec, - verbose_spec=verbose_spec + verbose_spec=verbose_spec, + current_date=strftime("%Y-%m-%d") ) return base_prompt