feat(ai): support stdin for Gemini CLI and verify with integration test

This commit is contained in:
2026-02-25 14:23:20 -05:00
parent 3ce4fa0c07
commit d187a6c8d9
4 changed files with 298 additions and 12 deletions

118
gui_2.py
View File

@@ -113,8 +113,8 @@ class App:
self.config = load_config()
ai_cfg = self.config.get("ai", {})
self.current_provider: str = ai_cfg.get("provider", "gemini")
self.current_model: str = ai_cfg.get("model", "gemini-2.5-flash-lite")
self._current_provider: str = ai_cfg.get("provider", "gemini")
self._current_model: str = ai_cfg.get("model", "gemini-2.5-flash-lite")
self.available_models: list[str] = []
self.temperature: float = ai_cfg.get("temperature", 0.0)
self.max_tokens: int = ai_cfg.get("max_tokens", 8192)
@@ -193,6 +193,12 @@ class App:
self._pending_dialog_lock = threading.Lock()
self._pending_actions: dict[str, ConfirmDialog] = {}
# Ask-related state (for tool approvals from CLI)
self._pending_ask_dialog = False
self._ask_dialog_open = False
self._ask_request_id = None
self._ask_tool_data = None
self._tool_log: list[tuple[str, str]] = []
self._comms_log: list[dict] = []
@@ -258,7 +264,45 @@ class App:
self._last_autosave = time.time()
session_logger.open_session()
self._init_ai_and_hooks()
@property
def current_provider(self):
return self._current_provider
@current_provider.setter
def current_provider(self, value):
if value != self._current_provider:
self._current_provider = value
ai_client.reset_session()
ai_client.set_provider(value, self.current_model)
if value == "gemini_cli":
# Ensure the adapter is initialized with the current path
if not ai_client._gemini_cli_adapter:
ai_client._gemini_cli_adapter = ai_client.GeminiCliAdapter(binary_path=self.ui_gemini_cli_path)
else:
ai_client._gemini_cli_adapter.binary_path = self.ui_gemini_cli_path
self.available_models = []
self._fetch_models(value)
@property
def current_model(self):
return self._current_model
@current_model.setter
def current_model(self, value):
if value != self._current_model:
self._current_model = value
ai_client.reset_session()
ai_client.set_provider(self.current_provider, value)
def _init_ai_and_hooks(self):
ai_client.set_provider(self.current_provider, self.current_model)
if self.current_provider == "gemini_cli":
if not ai_client._gemini_cli_adapter:
ai_client._gemini_cli_adapter = ai_client.GeminiCliAdapter(binary_path=self.ui_gemini_cli_path)
else:
ai_client._gemini_cli_adapter.binary_path = self.ui_gemini_cli_path
ai_client.confirm_and_run_callback = self._confirm_and_run
ai_client.comms_log_callback = self._on_comms_entry
ai_client.tool_log_callback = self._on_tool_log
@@ -277,6 +321,7 @@ class App:
'auto_add_history': 'ui_auto_add_history',
'disc_new_name_input': 'ui_disc_new_name_input',
'project_main_context': 'ui_project_main_context',
'gcli_path': 'ui_gemini_cli_path',
'output_dir': 'ui_output_dir',
'files_base_dir': 'ui_files_base_dir',
'ai_status': 'ai_status',
@@ -749,6 +794,11 @@ class App:
if item == "disc_listbox":
self._switch_discussion(value)
elif task.get("type") == "ask":
self._pending_ask_dialog = True
self._ask_request_id = task.get("request_id")
self._ask_tool_data = task.get("data", {})
elif action == "custom_callback":
cb = task.get("callback")
args = task.get("args", [])
@@ -789,6 +839,34 @@ class App:
else:
print("[DEBUG] No pending dialog to reject")
def _handle_approve_ask(self):
"""Responds with approval for a pending /api/ask request."""
if not self._ask_request_id: return
try:
requests.post(
"http://127.0.0.1:8999/api/ask/respond",
json={"request_id": self._ask_request_id, "response": {"approved": True}},
timeout=2
)
except Exception as e: print(f"Error responding to ask: {e}")
self._pending_ask_dialog = False
self._ask_request_id = None
self._ask_tool_data = None
def _handle_reject_ask(self):
"""Responds with rejection for a pending /api/ask request."""
if not self._ask_request_id: return
try:
requests.post(
"http://127.0.0.1:8999/api/ask/respond",
json={"request_id": self._ask_request_id, "response": {"approved": False}},
timeout=2
)
except Exception as e: print(f"Error responding to ask: {e}")
self._pending_ask_dialog = False
self._ask_request_id = None
self._ask_tool_data = None
def _handle_reset_session(self):
"""Logic for resetting the AI session."""
ai_client.reset_session()
@@ -1398,6 +1476,36 @@ class App:
imgui.close_current_popup()
imgui.end_popup()
if self._pending_ask_dialog:
if not self._ask_dialog_open:
imgui.open_popup("Approve Tool Execution")
self._ask_dialog_open = True
else:
self._ask_dialog_open = False
if imgui.begin_popup_modal("Approve Tool Execution", None, imgui.WindowFlags_.always_auto_resize)[0]:
if not self._pending_ask_dialog:
imgui.close_current_popup()
else:
tool_name = self._ask_tool_data.get("tool", "unknown")
tool_args = self._ask_tool_data.get("args", {})
imgui.text("The AI wants to execute a tool:")
imgui.text_colored(vec4(200, 200, 100), f"Tool: {tool_name}")
imgui.separator()
imgui.text("Arguments:")
imgui.begin_child("ask_args_child", imgui.ImVec2(400, 200), True)
imgui.text_unformatted(json.dumps(tool_args, indent=2))
imgui.end_child()
imgui.separator()
if imgui.button("Approve", imgui.ImVec2(120, 0)):
self._handle_approve_ask()
imgui.close_current_popup()
imgui.same_line()
if imgui.button("Deny", imgui.ImVec2(120, 0)):
self._handle_reject_ask()
imgui.close_current_popup()
imgui.end_popup()
if self.show_script_output:
if self._trigger_script_blink:
self._trigger_script_blink = False
@@ -1845,10 +1953,6 @@ class App:
for p in PROVIDERS:
if imgui.selectable(p, p == self.current_provider)[0]:
self.current_provider = p
ai_client.reset_session()
ai_client.set_provider(p, self.current_model)
self.available_models = []
self._fetch_models(p)
imgui.end_combo()
imgui.separator()
imgui.text("Model")
@@ -1860,8 +1964,6 @@ class App:
for m in self.available_models:
if imgui.selectable(m, m == self.current_model)[0]:
self.current_model = m
ai_client.reset_session()
ai_client.set_provider(self.current_provider, m)
imgui.end_list_box()
imgui.separator()
imgui.text("Parameters")