@@ -890,21 +890,24 @@ def handle_responses_streaming_event(
890890 return current_output , None
891891
892892
893- def get_source_context (sources : list , include_content : bool = True ) -> str :
893+ def get_source_context (
894+ sources : list , source_id_map : dict = None , include_content : bool = True
895+ ) -> str :
894896 """
895897 Build <source> tag context string from citation sources.
896898 """
897899 context_string = ""
898- citation_idx = {}
900+ if source_id_map is None :
901+ source_id_map = {}
899902 for source in sources :
900903 for doc , meta in zip (source .get ("document" , []), source .get ("metadata" , [])):
901904 src_id = meta .get ("source" ) or source .get ("source" , {}).get ("id" ) or "N/A"
902- if src_id not in citation_idx :
903- citation_idx [src_id ] = len (citation_idx ) + 1
905+ if src_id not in source_id_map :
906+ source_id_map [src_id ] = len (source_id_map ) + 1
904907 src_name = source .get ("source" , {}).get ("name" )
905908 body = doc if include_content else ""
906909 context_string += (
907- f'<source id="{ citation_idx [src_id ]} "'
910+ f'<source id="{ source_id_map [src_id ]} "'
908911 + (f' name="{ src_name } "' if src_name else "" )
909912 + f">{ body } </source>\n "
910913 )
@@ -929,7 +932,7 @@ def apply_source_context_to_messages(
929932 if not sources or not user_message :
930933 return messages
931934
932- context = get_source_context (sources , include_content )
935+ context = get_source_context (sources , include_content = include_content )
933936
934937 context = context .strip ()
935938 if not context :
@@ -4491,10 +4494,13 @@ async def flush_pending_delta_data(threshold: int = 0):
44914494
44924495 # Build context: file sources with content,
44934496 # tool sources as citation markers only.
4497+ source_id_map = {}
44944498 source_context = get_source_context (
4495- metadata .get ("sources" , [])
4499+ metadata .get ("sources" , []), source_id_map
44964500 ) + get_source_context (
4497- all_tool_call_sources , include_content = False
4501+ all_tool_call_sources ,
4502+ source_id_map ,
4503+ include_content = False ,
44984504 )
44994505 source_context = source_context .strip ()
45004506 if source_context :
0 commit comments