From 910c959829fe58d4a6ce022705f09c884f0384c2 Mon Sep 17 00:00:00 2001 From: caoxiaozhu Date: Thu, 14 May 2026 15:42:39 +0000 Subject: [PATCH] =?UTF-8?q?feat(server):=20=E6=96=B0=E5=A2=9E=E7=BC=96?= =?UTF-8?q?=E6=8E=92=E5=99=A8=E5=92=8C=E6=8A=A5=E9=94=80=E5=8D=95API?= =?UTF-8?q?=E7=AB=AF=E7=82=B9=EF=BC=8C=E6=89=A9=E5=B1=95=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E4=BD=93=E5=AF=B9=E8=AF=9D=E6=9C=8D=E5=8A=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/app/services/agent_conversations.py | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/server/src/app/services/agent_conversations.py b/server/src/app/services/agent_conversations.py index 9a70fcc..45843ed 100644 --- a/server/src/app/services/agent_conversations.py +++ b/server/src/app/services/agent_conversations.py @@ -125,6 +125,7 @@ class AgentConversationService: user_id: str | None, source: str | None = "user_message", session_type: str | None = None, + prefer_recoverable: bool = False, ) -> AgentConversation | None: self.prune_expired_conversations() @@ -141,11 +142,36 @@ class AgentConversationService: if not normalized_session_type: return conversations[0] if conversations else None + fallback_conversation = None for conversation in conversations: current_session_type = str((conversation.state_json or {}).get("session_type") or "").strip() or "expense" - if current_session_type == normalized_session_type: + if current_session_type != normalized_session_type: + continue + if fallback_conversation is None: + fallback_conversation = conversation + if not prefer_recoverable or self._is_recoverable_conversation(conversation): return conversation - return None + return fallback_conversation + + @staticmethod + def _is_recoverable_conversation(conversation: AgentConversation) -> bool: + if str(conversation.draft_claim_id or "").strip(): + return True + + state_json = dict(conversation.state_json or {}) + documents = state_json.get("ocr_documents") + if not isinstance(documents, list): + return False + + for item in documents: + if not isinstance(item, dict): + continue + preview_url = str(item.get("preview_url") or "").strip() + preview_data_url = str(item.get("preview_data_url") or "").strip() + preview_kind = str(item.get("preview_kind") or "").strip() + if (preview_url or preview_data_url) and preview_kind in {"image", "pdf"}: + return True + return False def hydrate_context_json( self,