feat: add citation sources for fetch_url tool results (#21669)

feat: add citation sources for fetch_url tool results

URL fetches now produce clickable citation sources in the UI, matching
the existing behavior of search_web and knowledge file tools. When a
model calls fetch_url during native tool calling, the fetched URL
appears as a citable source with a content preview, giving users full
transparency into what pages the model referenced.
This commit is contained in:
Classic298
2026-02-21 21:49:19 +01:00
committed by GitHub
parent 8c713a171d
commit d247adb60c

View File

@@ -171,7 +171,10 @@ def get_citation_source_from_tool_result(
Returns a list of sources (usually one, but query_knowledge_files may return multiple).
"""
try:
tool_result = json.loads(tool_result)
try:
tool_result = json.loads(tool_result)
except (json.JSONDecodeError, TypeError):
pass # keep tool_result as-is (e.g. fetch_url returns plain text)
if isinstance(tool_result, dict) and "error" in tool_result:
return []
@@ -232,6 +235,25 @@ def get_citation_source_from_tool_result(
}
]
elif tool_name == "fetch_url":
url = tool_params.get("url", "")
content = tool_result if isinstance(tool_result, str) else str(tool_result)
snippet = content[:500] + ("..." if len(content) > 500 else "")
return [
{
"source": {"name": url or "fetch_url", "id": url or "fetch_url"},
"document": [snippet],
"metadata": [
{
"source": url,
"name": url,
"url": url,
}
],
}
]
elif tool_name == "query_knowledge_files":
chunks = tool_result
@@ -4102,6 +4124,7 @@ async def streaming_chat_response_handler(response, ctx):
tool_function_name
in [
"search_web",
"fetch_url",
"view_knowledge_file",
"query_knowledge_files",
]