diff --git a/src/app_controller.py b/src/app_controller.py index 1062b81e..d618a464 100644 --- a/src/app_controller.py +++ b/src/app_controller.py @@ -275,45 +275,37 @@ def _api_generate(controller: 'AppController', req: GenerateRequest) -> dict[str "ts": project_manager.now_ts() }) - try: - with controller._disc_entries_lock: - has_ai_response = any(e.get("role") == "AI" for e in controller.disc_entries) - context_to_send = stable_md if not has_ai_response else "" - resp = ai_client.send(context_to_send, user_msg, base_dir, controller.last_file_items, disc_text, rag_engine=None) - - if req.auto_add_history: - with controller._pending_history_adds_lock: - controller._pending_history_adds.append({ - "role": "AI", - "content": resp, - "collapsed": True, - "ts": project_manager.now_ts() - }) - - controller._recalculate_session_usage() - duration = time.time() - start_time - return { - "text": resp, - "metadata": { - "provider": controller.current_provider, - "model": controller.current_model, - "duration_sec": round(duration, 3), - "timestamp": project_manager.now_ts() - }, - "usage": controller.session_usage - } - except ai_client.ProviderError as e: - raise HTTPException(status_code=500, detail=e.ui_message()) - except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) + result = ai_client.send_result(context_to_send, user_msg, base_dir, controller.last_file_items, disc_text, rag_engine=None) + if not result.ok: + err = result.errors[0] + raise HTTPException(status_code=502, detail=err.ui_message()) + resp = result.data + + if req.auto_add_history: + with controller._pending_history_adds_lock: + controller._pending_history_adds.append({ + "role": "AI", + "content": resp, + "collapsed": True, + "ts": project_manager.now_ts() + }) + + controller._recalculate_session_usage() + duration = time.time() - start_time + return { + "text": resp, + "metadata": { + "provider": controller.current_provider, + "model": controller.current_model, + "duration_sec": round(duration, 3), + "timestamp": project_manager.now_ts() + }, + "usage": controller.session_usage + } except Exception as e: import traceback traceback.print_exc() raise HTTPException(status_code=500, detail=str(e)) - except ai_client.ProviderError as e: - raise HTTPException(status_code=502, detail=f"AI Provider Error: {e.ui_message()}") - except Exception as e: - raise HTTPException(status_code=500, detail=f"In-flight AI request failure: {e}") async def _api_stream(controller: 'AppController', req: GenerateRequest) -> Any: """ @@ -3673,6 +3665,8 @@ class AppController: ai_client.set_model_params(self.temperature, self.max_tokens, self.history_trunc_limit, self.top_p) ai_client.set_agent_tools(self.ui_agent_tools) # Force update adapter path right before send to bypass potential duplication issues self._update_gcli_adapter(self.ui_gemini_cli_path) + # FR2 / Bug #1: per conductor/code_styleguides/error_handling.md section 3.1 (AND over OR), + # we check result.ok instead of catching a ProviderError exception. result = ai_client.send_result( event.stable_md, user_msg,