Skip to content
Closed
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
21 changes: 21 additions & 0 deletions gateway/platforms/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,7 @@ def _create_agent(
tool_complete_callback=None,
gateway_session_key: Optional[str] = None,
route: Optional[Dict[str, Any]] = None,
interim_assistant_callback=None,
) -> Any:
"""
Create an AIAgent instance using the gateway's runtime config.
Expand Down Expand Up @@ -1869,6 +1870,7 @@ def _create_agent(
tool_progress_callback=tool_progress_callback,
tool_start_callback=tool_start_callback,
tool_complete_callback=tool_complete_callback,
interim_assistant_callback=interim_assistant_callback,
session_db=self._ensure_session_db(),
fallback_model=fallback_model,
reasoning_config=reasoning_config,
Expand Down Expand Up @@ -2568,11 +2570,27 @@ def _tool_progress(event_type: str, tool_name: str = None, preview: str = None,
event_name = event_type.replace("tool.", "tool.")
_enqueue(event_name, {"message_id": message_id, "tool_name": tool_name, "preview": preview, "args": args})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honor already_streamed here. Core invokes this callback with already_streamed=True when the same visible text was already sent through the delta stream (run_agent.py:4893-4900); emitting it again as assistant.commentary duplicates client-visible narration.

def _commentary(text: str, *, already_streamed: bool = False) -> None:
if text and text.strip():
_enqueue("assistant.commentary", {
"message_id": message_id,
"text": text,
"already_streamed": already_streamed,
})

async def _run_and_signal() -> None:
try:
await queue.put(_event_payload("run.started", {"user_message": {"role": "user", "content": user_message}}))
await queue.put(_event_payload("message.started", {"message": {"id": message_id, "role": "assistant"}}))
history = await self._conversation_history_for_session(session_id)

# Respect display.show_commentary config gate
from gateway.run import _load_gateway_config
user_config = _load_gateway_config()
display_section = user_config.get("display", {}) if isinstance(user_config, dict) else {}
show_commentary = display_section.get("show_commentary", True) if isinstance(display_section, dict) else True
commentary_cb = _commentary if show_commentary else None

result, usage = await self._run_agent(
user_message=user_message,
conversation_history=history,
Expand All @@ -2581,6 +2599,7 @@ async def _run_and_signal() -> None:
stream_delta_callback=_delta,
tool_progress_callback=_tool_progress,
gateway_session_key=gateway_session_key,
interim_assistant_callback=commentary_cb,
)
final_response = _resolve_media_to_data_urls(result.get("final_response", "") if isinstance(result, dict) else "")
effective_session_id = result.get("session_id", session_id) if isinstance(result, dict) else session_id
Expand Down Expand Up @@ -4632,6 +4651,7 @@ async def _run_agent(
agent_ref: Optional[list] = None,
gateway_session_key: Optional[str] = None,
route: Optional[Dict[str, Any]] = None,
interim_assistant_callback=None,
) -> tuple:
"""
Create an agent and run a conversation in a thread executor.
Expand Down Expand Up @@ -4673,6 +4693,7 @@ def _run():
tool_complete_callback=tool_complete_callback,
gateway_session_key=gateway_session_key,
route=route,
interim_assistant_callback=interim_assistant_callback,
)
if agent_ref is not None:
agent_ref[0] = agent
Expand Down
Loading