@@ -787,7 +787,6 @@ def get_async_explainer():
787787 & (db .useinfo .course_id == course_name )
788788 ).select (orderby = db .useinfo .id )
789789
790- # Deduplicate sendmessage events — keep last message per student
791790 seen = {}
792791 for row in messages :
793792 try :
@@ -796,15 +795,14 @@ def get_async_explainer():
796795 msg = row .act
797796 seen [row .sid ] = msg
798797
799- # Fetch LLM conversation turns for this question
800798 llm_turns = db (
801799 (db .useinfo .event == "pi_llm_turn" )
802800 & (db .useinfo .div_id == div_id )
803801 & (db .useinfo .course_id == course_name )
804802 ).select (orderby = db .useinfo .id )
805803
806- # Group turns by sid, keeping only the most recent pi_attempt_id per student
807804 llm_by_sid = {}
805+ sid_order = []
808806 for row in llm_turns :
809807 try :
810808 turn = json .loads (row .act )
@@ -814,15 +812,19 @@ def get_async_explainer():
814812 content = turn .get ("content" , "" )
815813 if row .sid not in llm_by_sid :
816814 llm_by_sid [row .sid ] = {}
815+ sid_order .append (row .sid )
817816 if attempt_id not in llm_by_sid [row .sid ]:
818817 llm_by_sid [row .sid ][attempt_id ] = []
819818 llm_by_sid [row .sid ][attempt_id ].append ((turn_index , role , content ))
820819 except Exception :
821820 pass
822821
822+ for sid in seen :
823+ if sid not in llm_by_sid :
824+ sid_order .append (sid )
825+
823826 parts = []
824- all_sids = set (list (seen .keys ()) + list (llm_by_sid .keys ()))
825- for sid in all_sids :
827+ for sid in sid_order :
826828 if sid in seen :
827829 parts .append (f"<li><strong>{ sid } </strong> said: { seen [sid ]} </li>" )
828830 if sid in llm_by_sid :
@@ -832,8 +834,8 @@ def get_async_explainer():
832834 )
833835 turns = sorted (llm_by_sid [sid ][latest_attempt ], key = lambda t : t [0 ])
834836 for _ , role , content in turns :
835- label = sid if role == "user" else "LLM Peer"
836- parts .append (f"<li><strong>{ label } </strong> said: { content } </li>" )
837+ if role == "assistant" :
838+ parts .append (f"<li><strong>LLM Peer </strong> said: { content } </li>" )
837839
838840 if not parts :
839841 mess = "Sorry there are no explanations yet."
0 commit comments