YET ANOTEHR BOTCHED TRACK.

This commit is contained in:
2026-03-05 16:14:58 -05:00
parent d04574aa8f
commit 3d5773fa63
5 changed files with 73 additions and 28 deletions

View File

@@ -679,28 +679,56 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str,
for r_idx in range(MAX_TOOL_ROUNDS + 2):
events.emit("request_start", payload={"provider": "gemini", "model": _model, "round": r_idx})
if stream_callback:
resp = _gemini_chat.send_message_stream(payload)
# In 1.0.0, we use send_message with stream=True
config = types.GenerateContentConfig(
tools=[types.Tool(function_declarations=[types.FunctionDeclaration(**s) for s in mcp_client.get_tool_schemas()])] if enable_tools else [],
temperature=_temperature,
max_output_tokens=_max_tokens,
)
resp = _gemini_chat.send_message(payload, config=config, stream=True)
txt_chunks: list[str] = []
calls = []
usage = {}
reason = "STOP"
final_resp = None
for chunk in resp:
c_txt = chunk.text
if c_txt:
txt_chunks.append(c_txt)
stream_callback(c_txt)
if chunk.text:
txt_chunks.append(chunk.text)
stream_callback(chunk.text)
if chunk.candidates:
c = chunk.candidates[0]
if c.content and c.content.parts:
calls.extend([p.function_call for p in c.content.parts if p.function_call])
if hasattr(c, "finish_reason") and c.finish_reason:
reason = c.finish_reason.name
if chunk.usage_metadata:
usage = {
"input_tokens": chunk.usage_metadata.prompt_token_count,
"output_tokens": chunk.usage_metadata.candidates_token_count,
"total_tokens": chunk.usage_metadata.total_token_count,
"cache_read_input_tokens": getattr(chunk.usage_metadata, "cached_content_token_count", 0)
}
final_resp = chunk
txt = "".join(txt_chunks)
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "function_call") and p.function_call]
usage = {"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0), "output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0)}
cached_tokens = getattr(resp.usage_metadata, "cached_content_token_count", None)
if cached_tokens: usage["cache_read_input_tokens"] = cached_tokens
# Final validation of response object for subsequent code
resp = final_resp
else:
resp = _gemini_chat.send_message(payload)
txt = "\n".join(p.text for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "text") and p.text)
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if hasattr(p, "function_call") and p.function_call]
usage = {"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0), "output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0)}
cached_tokens = getattr(resp.usage_metadata, "cached_content_token_count", None)
if cached_tokens: usage["cache_read_input_tokens"] = cached_tokens
if txt: all_text.append(txt)
events.emit("response_received", payload={"provider": "gemini", "model": _model, "usage": usage, "round": r_idx})
reason = resp.candidates[0].finish_reason.name if resp.candidates and hasattr(resp.candidates[0], "finish_reason") else "STOP"
config = types.GenerateContentConfig(
tools=[types.Tool(function_declarations=[types.FunctionDeclaration(**s) for s in mcp_client.get_tool_schemas()])] if enable_tools else [],
temperature=_temperature,
max_output_tokens=_max_tokens,
)
resp = _gemini_chat.send_message(payload, config=config)
txt = resp.text or ""
calls = [p.function_call for c in resp.candidates if getattr(c, "content", None) for p in c.content.parts if p.function_call]
usage = {
"input_tokens": getattr(resp.usage_metadata, "prompt_token_count", 0),
"output_tokens": getattr(resp.usage_metadata, "candidates_token_count", 0),
"total_tokens": getattr(resp.usage_metadata, "total_token_count", 0),
"cache_read_input_tokens": getattr(resp.usage_metadata, "cached_content_token_count", 0)
}
reason = resp.candidates[0].finish_reason.name if (resp.candidates and hasattr(resp.candidates[0], "finish_reason")) else "STOP"
_append_comms("IN", "response", {"round": r_idx, "stop_reason": reason, "text": txt, "tool_calls": [{"name": c.name, "args": dict(c.args)} for c in calls], "usage": usage})
total_in = usage.get("input_tokens", 0)
if total_in > _GEMINI_MAX_INPUT_TOKENS * 0.4 and _gemini_chat and _get_gemini_history_list(_gemini_chat):

View File

@@ -732,12 +732,12 @@ class AppController:
self._set_status("fetching models...")
def do_fetch() -> None:
try:
models = ai_client.list_models(provider)
self.available_models = models
if self.current_model not in models and models:
self.current_model = models[0]
models_list = ai_client.list_models(provider)
self.available_models = models_list
if self.current_model not in models_list and models_list:
self.current_model = models_list[0]
ai_client.set_provider(self._current_provider, self.current_model)
self._set_status(f"models loaded: {len(models)}")
self._set_status(f"models loaded: {len(models_list)}")
except Exception as e:
self._set_status(f"model fetch error: {e}")
self.models_thread = threading.Thread(target=do_fetch, daemon=True)
@@ -843,6 +843,12 @@ class AppController:
with self._pending_gui_tasks_lock:
# These payloads already contain the 'action' field
self._pending_gui_tasks.append(payload)
elif event_name == "response":
with self._pending_gui_tasks_lock:
self._pending_gui_tasks.append({
"action": "handle_ai_response",
"payload": payload
})
def _handle_request_event(self, event: events.UserRequestEvent) -> None:
"""Processes a UserRequestEvent by calling the AI client."""