File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2171,14 +2171,24 @@ async def convert_url_images_to_base64(form_data):
21712171
21722172async def load_messages_from_db (chat_id : str , message_id : str ) -> Optional [list [dict ]]:
21732173 """
2174- Load the message chain from DB up to message_id,
2175- keeping only LLM-relevant fields (role, content, output) .
2174+ Load the message chain, keeping only LLM-relevant fields.
2175+ Falls back to embedded history when the chain is incomplete .
21762176 """
21772177 messages_map = await Chats .get_messages_map_by_chat_id (chat_id )
21782178 if not messages_map :
21792179 return None
21802180
21812181 db_messages = get_message_list (messages_map , message_id )
2182+
2183+ # Target unreachable or chain truncated before root.
2184+ if not db_messages or db_messages [0 ].get ('parentId' ) is not None :
2185+ chat = await Chats .get_chat_by_id (chat_id )
2186+ if chat :
2187+ history = (chat .chat or {}).get ('history' , {}).get ('messages' , {}) or {}
2188+ history_messages = get_message_list (history , message_id )
2189+ if history_messages :
2190+ db_messages = history_messages
2191+
21822192 if not db_messages :
21832193 return None
21842194
You can’t perform that action at this time.
0 commit comments