refactor(ai_client): _send_grok_result() returns Result[str]
This commit is contained in:
+40
-37
@@ -2247,50 +2247,53 @@ def _ensure_grok_client() -> Any:
|
||||
_grok_client = openai.OpenAI(api_key=api_key, base_url="https://api.x.ai/v1")
|
||||
return _grok_client
|
||||
|
||||
def _send_grok(md_content: str, user_message: str, base_dir: str,
|
||||
def _send_grok_result(md_content: str, user_message: str, base_dir: str,
|
||||
file_items: list[dict[str, Any]] | None = None,
|
||||
discussion_history: 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,
|
||||
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
|
||||
from src.openai_compatible import OpenAICompatibleRequest
|
||||
client = _ensure_grok_client()
|
||||
tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None
|
||||
caps = get_capabilities("grok", _model)
|
||||
with _grok_history_lock:
|
||||
user_content = user_message
|
||||
if file_items:
|
||||
for fi in file_items:
|
||||
if fi.get("is_image") and fi.get("base64_data"):
|
||||
user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}"
|
||||
if discussion_history and not _grok_history:
|
||||
_grok_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
|
||||
else:
|
||||
_grok_history.append({"role": "user", "content": user_content})
|
||||
def _build_grok_request(_round_idx: int) -> OpenAICompatibleRequest:
|
||||
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]:
|
||||
from src.openai_compatible import OpenAICompatibleRequest, _classify_openai_compatible_error
|
||||
try:
|
||||
client = _ensure_grok_client()
|
||||
tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None
|
||||
caps = get_capabilities("grok", _model)
|
||||
with _grok_history_lock:
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
||||
messages.extend(_grok_history)
|
||||
extra_body: dict[str, Any] = {}
|
||||
if caps.web_search:
|
||||
extra_body["search_parameters"] = {"mode": "auto"}
|
||||
if caps.x_search:
|
||||
extra_body.setdefault("search_parameters", {})
|
||||
extra_body["search_parameters"]["sources"] = [{"type": "x"}]
|
||||
return OpenAICompatibleRequest(
|
||||
messages=messages, model=_model, temperature=_temperature, top_p=_top_p,
|
||||
max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback,
|
||||
tools=tools, tool_choice="auto" if tools else "auto",
|
||||
extra_body=extra_body or None,
|
||||
)
|
||||
return run_with_tool_loop(
|
||||
client, _build_grok_request, capabilities=caps,
|
||||
pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback,
|
||||
patch_callback=patch_callback, base_dir=base_dir, vendor_name="grok",
|
||||
history_lock=_grok_history_lock, history=_grok_history,
|
||||
)
|
||||
user_content = user_message
|
||||
if file_items:
|
||||
for fi in file_items:
|
||||
if fi.get("is_image") and fi.get("base64_data"):
|
||||
user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}"
|
||||
if discussion_history and not _grok_history:
|
||||
_grok_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
|
||||
else:
|
||||
_grok_history.append({"role": "user", "content": user_content})
|
||||
def _build_grok_request(_round_idx: int) -> OpenAICompatibleRequest:
|
||||
with _grok_history_lock:
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
||||
messages.extend(_grok_history)
|
||||
extra_body: dict[str, Any] = {}
|
||||
if caps.web_search:
|
||||
extra_body["search_parameters"] = {"mode": "auto"}
|
||||
if caps.x_search:
|
||||
extra_body.setdefault("search_parameters", {})
|
||||
extra_body["search_parameters"]["sources"] = [{"type": "x"}]
|
||||
return OpenAICompatibleRequest(
|
||||
messages=messages, model=_model, temperature=_temperature, top_p=_top_p,
|
||||
max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback,
|
||||
tools=tools, tool_choice="auto" if tools else "auto",
|
||||
extra_body=extra_body or None,
|
||||
)
|
||||
return Result(data=run_with_tool_loop(
|
||||
client, _build_grok_request, capabilities=caps,
|
||||
pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback,
|
||||
patch_callback=patch_callback, base_dir=base_dir, vendor_name="grok",
|
||||
history_lock=_grok_history_lock, history=_grok_history,
|
||||
))
|
||||
except Exception as exc:
|
||||
return Result(data="", errors=[_classify_openai_compatible_error(exc, source="ai_client.grok")])
|
||||
|
||||
def _list_grok_models() -> list[str]:
|
||||
from src.vendor_capabilities import list_models_for_vendor
|
||||
|
||||
Reference in New Issue
Block a user