This commit is contained in:
Timothy Jaeryang Baek
2026-02-23 01:37:06 -06:00
parent aa649bec6b
commit 424dba443c
+10 -1
View File
@@ -827,10 +827,15 @@ def apply_source_context_to_messages(
messages: list,
sources: list,
user_message: str,
include_content: bool = True,
) -> list:
"""
Build source context from citation sources and apply to messages.
Uses RAG template to format context for model consumption.
When include_content is False, emit <source> tags with id/name but no
document body — useful when the content is already present elsewhere
(e.g. in a tool result message) and only citation markers are needed.
"""
if not sources or not user_message:
return messages
@@ -844,10 +849,11 @@ def apply_source_context_to_messages(
if src_id not in citation_idx:
citation_idx[src_id] = len(citation_idx) + 1
src_name = source.get("source", {}).get("name")
body = doc if include_content else ""
context_string += (
f'<source id="{citation_idx[src_id]}"'
+ (f' name="{src_name}"' if src_name else "")
+ f">{doc}</source>\n"
+ f">{body}</source>\n"
)
context_string = context_string.strip()
@@ -4227,6 +4233,8 @@ async def streaming_chat_response_handler(response, ctx):
await event_emitter({"type": "source", "data": source})
# Apply source context to messages for model
# Use metadata_only=True to avoid duplicating content
# that is already in the tool result message.
all_tool_call_sources.extend(tool_call_sources)
if all_tool_call_sources and user_message:
# Restore original user message before re-applying to avoid recursive nesting
@@ -4238,6 +4246,7 @@ async def streaming_chat_response_handler(response, ctx):
form_data["messages"],
all_tool_call_sources,
user_message,
include_content=False,
)
tool_call_sources.clear()