docs(conductor): Synchronize docs for track 'GUI Performance Profiling & Optimization'

This commit is contained in:
2026-03-07 13:20:12 -05:00
parent 7198c8717a
commit d71d82bafb
7 changed files with 184 additions and 100 deletions

View File

@@ -12,12 +12,14 @@ For Gemini: injects the initial context directly into system_instruction
during chat creation to avoid massive history bloat.
"""
# ai_client.py
# ai_client.py
import tomllib
import asyncio
import json
import sys
import time
import datetime
from src import performance_monitor
import hashlib
import difflib
import threading
@@ -568,6 +570,8 @@ async def _execute_tool_calls_concurrently(
Executes multiple tool calls concurrently using asyncio.gather.
Returns a list of (tool_name, call_id, output, original_name).
"""
monitor = performance_monitor.get_monitor()
if monitor.enabled: monitor.start_component("ai_client._execute_tool_calls_concurrently")
tasks = []
for fc in calls:
if provider == "gemini":
@@ -596,6 +600,7 @@ async def _execute_tool_calls_concurrently(
tasks.append(_execute_single_tool_call_async(name, args, call_id, base_dir, pre_tool_callback, qa_callback, r_idx, patch_callback))
results = await asyncio.gather(*tasks)
if monitor.enabled: monitor.end_component("ai_client._execute_tool_calls_concurrently")
return results
async def _execute_single_tool_call_async(
@@ -807,6 +812,8 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str,
stream_callback: Optional[Callable[[str], None]] = None,
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
global _gemini_chat, _gemini_cache, _gemini_cache_md_hash, _gemini_cache_created_at, _gemini_cached_file_paths
monitor = performance_monitor.get_monitor()
if monitor.enabled: monitor.start_component("ai_client._send_gemini")
try:
_ensure_gemini_client(); mcp_client.configure(file_items or [], [base_dir])
sys_instr = f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"
@@ -1018,8 +1025,12 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str,
_append_comms("OUT", "request", {"message": f"[TOOL OUTPUT BUDGET EXCEEDED: {_cumulative_tool_bytes} bytes]"})
_append_comms("OUT", "tool_result_send", {"results": log})
payload = f_resps
return "\n\n".join(all_text) if all_text else "(No text returned)"
except Exception as e: raise _classify_gemini_error(e) from e
res = "\n\n".join(all_text) if all_text else "(No text returned)"
if monitor.enabled: monitor.end_component("ai_client._send_gemini")
return res
except Exception as e:
if monitor.enabled: monitor.end_component("ai_client._send_gemini")
raise _classify_gemini_error(e) from e
def _send_gemini_cli(md_content: str, user_message: str, base_dir: str,
file_items: list[dict[str, Any]] | None = None,
@@ -1294,7 +1305,9 @@ def _repair_anthropic_history(history: list[dict[str, Any]]) -> None:
],
})
def _send_anthropic(md_content: str, user_message: str, base_dir: str, file_items: list[dict[str, Any]] | None = None, discussion_history: str = "", pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None, qa_callback: Optional[Callable[[str], str]] = None, stream_callback: Optional[Callable[[str], None]] = None) -> str:
def _send_anthropic(md_content: str, user_message: str, base_dir: str, file_items: list[dict[str, Any]] | None = None, discussion_history: str = "", pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None, qa_callback: Optional[Callable[[str], str]] = None, stream_callback: Optional[Callable[[str], None]] = None, patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
monitor = performance_monitor.get_monitor()
if monitor.enabled: monitor.start_component("ai_client._send_anthropic")
try:
_ensure_anthropic_client()
mcp_client.configure(file_items or [], [base_dir])
@@ -1411,7 +1424,7 @@ def _send_anthropic(md_content: str, user_message: str, base_dir: str, file_item
try:
loop = asyncio.get_running_loop()
results = asyncio.run_coroutine_threadsafe(
_execute_tool_calls_concurrently(response.content, base_dir, pre_tool_callback, qa_callback, round_idx, "anthropic"),
_execute_tool_calls_concurrently(response.content, base_dir, pre_tool_callback, qa_callback, round_idx, "anthropic", patch_callback),
loop
).result()
except RuntimeError:
@@ -1463,10 +1476,14 @@ def _send_anthropic(md_content: str, user_message: str, base_dir: str, file_item
],
})
final_text = "\n\n".join(all_text_parts)
return final_text if final_text.strip() else "(No text returned by the model)"
res = final_text if final_text.strip() else "(No text returned by the model)"
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
return res
except ProviderError:
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
raise
except Exception as exc:
if monitor.enabled: monitor.end_component("ai_client._send_anthropic")
raise _classify_anthropic_error(exc) from exc
def _ensure_deepseek_client() -> None:
@@ -1491,12 +1508,16 @@ def _send_deepseek(md_content: str, user_message: str, base_dir: str,
stream: bool = False,
pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None,
qa_callback: Optional[Callable[[str], str]] = None,
stream_callback: Optional[Callable[[str], None]] = None) -> str:
stream_callback: Optional[Callable[[str], None]] = None,
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
monitor = performance_monitor.get_monitor()
if monitor.enabled: monitor.start_component("ai_client._send_deepseek")
try:
mcp_client.configure(file_items or [], [base_dir])
creds = _load_credentials()
api_key = creds.get("deepseek", {}).get("api_key")
if not api_key:
if monitor.enabled: monitor.end_component("ai_client._send_deepseek")
raise ValueError("DeepSeek API key not found in credentials.toml")
api_url = "https://api.deepseek.com/chat/completions"
headers = {
@@ -1578,6 +1599,7 @@ def _send_deepseek(md_content: str, user_message: str, base_dir: str,
response = requests.post(api_url, headers=headers, json=request_payload, timeout=120, stream=stream)
response.raise_for_status()
except requests.exceptions.RequestException as e:
if monitor.enabled: monitor.end_component("ai_client._send_deepseek")
raise _classify_deepseek_error(e) from e
assistant_text = ""
@@ -1724,8 +1746,11 @@ def _send_deepseek(md_content: str, user_message: str, base_dir: str,
for tr in tool_results_for_history:
_deepseek_history.append(tr)
return "\n\n".join(all_text_parts) if all_text_parts else "(No text returned)"
res = "\n\n".join(all_text_parts) if all_text_parts else "(No text returned)"
if monitor.enabled: monitor.end_component("ai_client._send_deepseek")
return res
except Exception as e:
if monitor.enabled: monitor.end_component("ai_client._send_deepseek")
raise _classify_deepseek_error(e) from e
def _send_minimax(md_content: str, user_message: str, base_dir: str,
@@ -2062,34 +2087,39 @@ def send(
stream_callback: Optional[Callable[[str], None]] = None,
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None,
) -> str:
monitor = performance_monitor.get_monitor()
if monitor.enabled: monitor.start_component("ai_client.send")
with _send_lock:
if _provider == "gemini":
return _send_gemini(
res = _send_gemini(
md_content, user_message, base_dir, file_items, discussion_history,
pre_tool_callback, qa_callback, enable_tools, stream_callback, patch_callback
)
elif _provider == "gemini_cli":
return _send_gemini_cli(
res = _send_gemini_cli(
md_content, user_message, base_dir, file_items, discussion_history,
pre_tool_callback, qa_callback, stream_callback, patch_callback
)
elif _provider == "anthropic":
return _send_anthropic(
res = _send_anthropic(
md_content, user_message, base_dir, file_items, discussion_history,
pre_tool_callback, qa_callback, stream_callback=stream_callback, patch_callback=patch_callback
)
elif _provider == "deepseek":
return _send_deepseek(
res = _send_deepseek(
md_content, user_message, base_dir, file_items, discussion_history,
stream, pre_tool_callback, qa_callback, stream_callback, patch_callback
)
elif _provider == "minimax":
return _send_minimax(
res = _send_minimax(
md_content, user_message, base_dir, file_items, discussion_history,
stream, pre_tool_callback, qa_callback, stream_callback, patch_callback
)
else:
if monitor.enabled: monitor.end_component("ai_client.send")
raise ValueError(f"Unknown provider: {_provider}")
if monitor.enabled: monitor.end_component("ai_client.send")
return res
def _add_bleed_derived(d: dict[str, Any], sys_tok: int = 0, tool_tok: int = 0) -> dict[str, Any]:
cur = d.get("current", 0)