feat(ai): integrate GeminiCliAdapter into ai_client

This commit is contained in:
2026-02-25 14:02:06 -05:00
parent 211000c926
commit b762a80482
5 changed files with 280 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ import os
import file_cache
import mcp_client
import anthropic
from gemini_cli_adapter import GeminiCliAdapter
from google import genai
from google.genai import types
from events import EventEmitter
@@ -58,6 +59,8 @@ _anthropic_history: list[dict] = []
_anthropic_history_lock = threading.Lock()
_send_lock = threading.Lock()
_gemini_cli_adapter = None
# Injected by gui.py - called when AI wants to run a command.
# Signature: (script: str, base_dir: str) -> str | None
confirm_and_run_callback = None
@@ -253,6 +256,7 @@ def reset_session():
global _gemini_cache_md_hash, _gemini_cache_created_at
global _anthropic_client, _anthropic_history
global _CACHED_ANTHROPIC_TOOLS
global _gemini_cli_adapter
if _gemini_client and _gemini_cache:
try:
_gemini_client.caches.delete(name=_gemini_cache.name)
@@ -263,6 +267,8 @@ def reset_session():
_gemini_cache = None
_gemini_cache_md_hash = None
_gemini_cache_created_at = None
if _gemini_cli_adapter:
_gemini_cli_adapter.session_id = None
_anthropic_client = None
with _anthropic_history_lock:
_anthropic_history = []
@@ -787,8 +793,46 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str,
return "\n\n".join(all_text) if all_text else "(No text returned)"
except Exception as e: raise _classify_gemini_error(e) from e
def _send_gemini_cli(md_content: str, user_message: str, base_dir: str,
file_items: list[dict] | None = None,
discussion_history: str = "") -> str:
global _gemini_cli_adapter
try:
if _gemini_cli_adapter is None:
_gemini_cli_adapter = GeminiCliAdapter(binary_path="gemini")
events.emit("request_start", payload={"provider": "gemini_cli", "model": _model, "round": 0})
# If it's a new session (session_id is None), we should ideally send the context.
# For now, following the simple pattern:
payload = user_message
if _gemini_cli_adapter.session_id is None:
# Prepend context and discussion history to the first message
full_prompt = f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>\n\n"
if discussion_history:
full_prompt += f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n"
full_prompt += user_message
payload = full_prompt
_append_comms("OUT", "request", {"message": f"[CLI] [msg {len(payload)}]"})
result_text = _gemini_cli_adapter.send(payload)
usage = _gemini_cli_adapter.last_usage or {}
events.emit("response_received", payload={"provider": "gemini_cli", "model": _model, "usage": usage, "round": 0})
_append_comms("IN", "response", {
"round": 0,
"stop_reason": "STOP",
"text": result_text,
"tool_calls": [],
"usage": usage
})
return result_text
except Exception as e:
# Basic error classification for CLI
raise ProviderError("unknown", "gemini_cli", e)
# ------------------------------------------------------------------ anthropic history management
@@ -1276,6 +1320,8 @@ def send(
with _send_lock:
if _provider == "gemini":
return _send_gemini(md_content, user_message, base_dir, file_items, discussion_history)
elif _provider == "gemini_cli":
return _send_gemini_cli(md_content, user_message, base_dir, file_items, discussion_history)
elif _provider == "anthropic":
return _send_anthropic(md_content, user_message, base_dir, file_items, discussion_history)
raise ValueError(f"unknown provider: {_provider}")