Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/backend/v3/callbacks/response_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import asyncio
import logging
import time

import re
from semantic_kernel.contents import ChatMessageContent, StreamingChatMessageContent
from v3.config.settings import connection_config
from v3.models.messages import (
Expand All @@ -18,6 +18,24 @@
)


def clean_citations(text: str) -> str:
"""Remove citation markers from agent responses while preserving formatting."""
if not text:
return text

# Remove citation patterns like [9:0|source], [9:1|source], etc.
text = re.sub(r'\[\d+:\d+\|source\]', '', text)

# Remove other common citation pattern
text = re.sub(r'\[\s*source\s*\]', '', text, flags=re.IGNORECASE)
text = re.sub(r'\[\d+\]', '', text)
text = re.sub(r'【[^】]*】', '', text) # Unicode brackets
text = re.sub(r'\(source:[^)]*\)', '', text, flags=re.IGNORECASE)
text = re.sub(r'\[source:[^\]]*\]', '', text, flags=re.IGNORECASE)

return text


def agent_response_callback(message: ChatMessageContent, user_id: str = None) -> None:
"""Observer function to print detailed information about streaming messages."""
# import sys
Expand Down Expand Up @@ -55,7 +73,7 @@ def agent_response_callback(message: ChatMessageContent, user_id: str = None) ->
final_message = AgentMessage(
agent_name=agent_name,
timestamp=time.time() or "",
content=message.content or "",
content=clean_citations(message.content) or "",
)

asyncio.create_task(
Expand All @@ -80,7 +98,7 @@ async def streaming_agent_response_callback(
try:
message = AgentMessageStreaming(
agent_name=streaming_message.name or "Unknown Agent",
content=streaming_message.content,
content=clean_citations(streaming_message.content),
is_final=is_final,
)
await connection_config.send_status_update_async(
Expand Down
Loading