mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
fix(agent): keep chat reads on native async path
This commit is contained in:
@@ -10,7 +10,10 @@ from app.runtime.settings import RuntimeSettingsCompat
|
||||
|
||||
settings = RuntimeSettingsCompat()
|
||||
from app.application.agentdata import AgentChatPort as AgentChatOper
|
||||
from app.application.messaging.chat import get_configured_agent_chat_persistence
|
||||
from app.application.messaging.chat import (
|
||||
get_configured_agent_chat_persistence,
|
||||
get_configured_agent_chat_service,
|
||||
)
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.agent import ConversationMemory
|
||||
|
||||
@@ -109,19 +112,19 @@ class MemoryManager:
|
||||
async def async_get_agent_messages(
|
||||
self, session_id: str, user_id: str
|
||||
) -> List[BaseMessage]:
|
||||
"""异步恢复 Agent 消息,持久化读取经有界数据库 worker 承接。"""
|
||||
"""异步恢复 Agent 消息,查询与会话应用服务保持同一异步端口。"""
|
||||
memory = self.get_memory(session_id, user_id)
|
||||
if memory:
|
||||
return memory.messages
|
||||
|
||||
try:
|
||||
persistence = get_configured_agent_chat_persistence()
|
||||
chat = await persistence.async_get(
|
||||
service = get_configured_agent_chat_service()
|
||||
chat = await service.get(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
if not chat:
|
||||
chat = await persistence.async_get(session_id=session_id)
|
||||
chat = await service.get(session_id=session_id)
|
||||
except Exception as e:
|
||||
logger.debug(f"读取持久化Agent会话失败: {e}")
|
||||
return []
|
||||
|
||||
@@ -82,6 +82,7 @@ def _get_plugin_tools_revision() -> int:
|
||||
from app.application.agentdata import AgentTaskPort as AgentTaskOper
|
||||
from app.application.agentdata import UserPort as UserOper
|
||||
from app.application.messaging.chat import (
|
||||
get_configured_agent_chat_service,
|
||||
get_configured_agent_chat_persistence,
|
||||
has_custom_agent_chat_title,
|
||||
)
|
||||
@@ -579,7 +580,7 @@ class MoviePilotAgent:
|
||||
return
|
||||
self._tool_context["chat_title_prepared"] = True
|
||||
try:
|
||||
chat = await get_configured_agent_chat_persistence().async_get(
|
||||
chat = await get_configured_agent_chat_service().get(
|
||||
session_id=self.session_id,
|
||||
user_id=self.user_id,
|
||||
)
|
||||
|
||||
@@ -518,12 +518,12 @@ async def _build_web_agent_session_id_async(
|
||||
user: ApiPrincipal,
|
||||
session_id: Optional[str],
|
||||
) -> str:
|
||||
"""异步解析 Web Agent 会话 ID,历史查询经有界数据库 worker 承接。"""
|
||||
"""异步解析 Web Agent 会话 ID,并复用异步会话查询端口。"""
|
||||
seed = str(session_id or "").strip() or uuid.uuid4().hex
|
||||
if seed.startswith(WEB_AGENT_SESSION_PREFIX):
|
||||
return seed
|
||||
try:
|
||||
existing_chat = await get_configured_agent_chat_persistence().async_get(seed)
|
||||
existing_chat = await get_configured_agent_chat_service().get(seed)
|
||||
if existing_chat and AgentChatService.can_access(existing_chat, user):
|
||||
return seed
|
||||
except Exception as e:
|
||||
@@ -663,9 +663,7 @@ async def _save_web_agent_display_snapshot(
|
||||
保存 WebAgent 当前展示消息快照。
|
||||
"""
|
||||
try:
|
||||
existing_chat = await get_configured_agent_chat_persistence().async_get(
|
||||
session_id
|
||||
)
|
||||
existing_chat = await get_configured_agent_chat_service().get(session_id)
|
||||
await get_configured_agent_chat_persistence().async_save_display_messages(
|
||||
session_id=session_id,
|
||||
user_id=(existing_chat.user_id if existing_chat else str(current_user.id)),
|
||||
|
||||
@@ -85,14 +85,6 @@ class AsyncAgentChatRepository(Protocol):
|
||||
class SyncAgentChatRepository(Protocol):
|
||||
"""仅包含 Agent 编排所需同步持久化方法的适配器端口。"""
|
||||
|
||||
def get(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> Optional[Any]:
|
||||
"""读取服务端会话。"""
|
||||
...
|
||||
|
||||
def append_display_messages(
|
||||
self,
|
||||
session_id: str,
|
||||
@@ -165,6 +157,7 @@ class AgentChatRecord:
|
||||
created_at: Any
|
||||
updated_at: Any
|
||||
messages: list[dict]
|
||||
agent_messages: list[dict]
|
||||
|
||||
|
||||
class AsyncUnitOfWork(Protocol):
|
||||
@@ -222,9 +215,16 @@ class AgentChatService:
|
||||
return None
|
||||
return projected
|
||||
|
||||
async def get(self, session_id: str) -> Optional[AgentChatRecord]:
|
||||
async def get(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> Optional[AgentChatRecord]:
|
||||
"""读取不附带授权判断的会话投影。"""
|
||||
record = await self._repository.async_get(session_id=session_id)
|
||||
record = await self._repository.async_get(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
if record is None:
|
||||
return None
|
||||
return self._project(record)
|
||||
@@ -338,11 +338,12 @@ class AgentChatService:
|
||||
created_at=record.created_at,
|
||||
updated_at=record.updated_at,
|
||||
messages=list(record.display_messages or []),
|
||||
agent_messages=list(record.agent_messages or []),
|
||||
)
|
||||
|
||||
|
||||
class AgentChatPersistenceService:
|
||||
"""把 Agent 编排所需的同步短事务委托给有界数据库 worker。"""
|
||||
"""把 Agent 编排所需的同步持久化操作委托给有界数据库 worker。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -354,24 +355,11 @@ class AgentChatPersistenceService:
|
||||
self._async_executor = async_executor
|
||||
|
||||
async def _run(self, operation: Callable[[SyncAgentChatRepository], T]) -> T:
|
||||
"""在线程 worker 中执行一个完整的同步 AgentChat 短事务。"""
|
||||
"""在线程 worker 中执行一个同步 AgentChat 持久化操作。"""
|
||||
return await self._async_executor.run(
|
||||
lambda: operation(self._repository())
|
||||
)
|
||||
|
||||
async def async_get(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> Optional[Any]:
|
||||
"""异步读取会话,实际查询由有界 worker 承接。"""
|
||||
return await self._run(
|
||||
lambda repository: repository.get(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
)
|
||||
|
||||
async def async_append_display_messages(
|
||||
self,
|
||||
*,
|
||||
@@ -383,9 +371,9 @@ class AgentChatPersistenceService:
|
||||
source: Optional[str] = None,
|
||||
original_chat_id: Optional[str] = None,
|
||||
client_session_id: Optional[str] = None,
|
||||
) -> Optional[Any]:
|
||||
) -> None:
|
||||
"""异步追加展示消息,等待同步事务取得确定终态。"""
|
||||
return await self._run(
|
||||
await self._run(
|
||||
lambda repository: repository.append_display_messages(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
@@ -397,6 +385,7 @@ class AgentChatPersistenceService:
|
||||
client_session_id=client_session_id,
|
||||
)
|
||||
)
|
||||
return None
|
||||
|
||||
async def async_save_display_messages(
|
||||
self,
|
||||
@@ -409,9 +398,9 @@ class AgentChatPersistenceService:
|
||||
source: Optional[str] = None,
|
||||
original_chat_id: Optional[str] = None,
|
||||
client_session_id: Optional[str] = None,
|
||||
) -> Optional[Any]:
|
||||
) -> None:
|
||||
"""异步保存展示消息快照,实际写入由有界 worker 承接。"""
|
||||
return await self._run(
|
||||
await self._run(
|
||||
lambda repository: repository.save_display_messages(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
@@ -423,6 +412,7 @@ class AgentChatPersistenceService:
|
||||
client_session_id=client_session_id,
|
||||
)
|
||||
)
|
||||
return None
|
||||
|
||||
async def async_save_agent_messages(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user