# Specification: OpenAI Provider Integration ## Overview This track introduces support for OpenAI as a first-class model provider. It involves implementing a dedicated client in `src/ai_client.py`, updating configuration models, enhancing the GUI for provider selection, and integrating OpenAI into the tiered MMA architecture. ## Functional Requirements ### 1. Core AI Client (`src/ai_client.py`) - **OpenAI Integration:** - Implement `_send_openai()` to handle communication with OpenAI's Chat Completions API. - Implement a tool-call loop similar to `_send_gemini` and `_send_anthropic`. - Support for function calling (using `tools` and `tool_choice`). - Support for multi-modal input (Vision) by encoding screenshots as base64 data URIs in the message payload. - Implement response streaming support compatible with the existing GUI mechanism. - **State Management:** - Define module-level `_openai_client` and `_openai_history`. - Ensure `ai_client.reset_session()` clears OpenAI-specific history. - **Error Handling:** - Implement `_classify_openai_error()` to map OpenAI API exceptions to `ProviderError` types (`quota`, `rate_limit`, `auth`, `balance`, `network`). - **Model Discovery:** - Implement `_list_openai_models()` to fetch available models from the API. ### 2. Configuration & Authentication - **Credentials:** - Update `_load_credentials()` to support an `[openai]` section in `credentials.toml`. - Update the `FileNotFoundError` message in `_load_credentials()` to include the OpenAI example. - **Cost Tracking:** - Update `src/cost_tracker.py` with pricing for `gpt-4o`, `gpt-4o-mini`, `o1`, and `o3-mini`. ### 3. GUI & Controller Integration - **Provider Lists:** - Add `openai` to the `PROVIDERS` list in `src/gui_2.py` and `src/app_controller.py`. - **Model Fetching:** - Ensure `AppController._fetch_models()` correctly dispatches to `ai_client.list_models("openai")`. - **Settings UI:** - The AI Settings panel should automatically handle the new provider and its models once added to the lists. ### 4. MMA Orchestration - **Tier Support:** - Verify that agents in all tiers (1-4) can be configured to use OpenAI models via the `mma_tier_usage` dict in `AppController`. - Ensure `run_worker_lifecycle` in `src/multi_agent_conductor.py` correctly passes OpenAI model names to `ai_client.send()`. ## Non-Functional Requirements - **Consistency:** Follow the established pattern of module-level singleton state in `ai_client.py`. - **Latency:** Ensure the tool-call loop overhead is minimal. - **Security:** Rigorously protect the OpenAI API key; ensure it is never logged or exposed in the GUI. ## Acceptance Criteria - [ ] OpenAI can be selected as a provider in the AI Settings panel. - [ ] Models like `gpt-4o` and `gpt-4o-mini` are listed and selectable. - [ ] Agents can successfully use tools (e.g., `read_file`, `run_powershell`) using OpenAI models. - [ ] Screenshots are correctly processed and described by vision-capable OpenAI models. - [ ] Response streaming is functional in the Discussion panel. - [ ] Estimated costs for OpenAI calls are displayed in the MMA Dashboard. ## Out of Scope - Support for the legacy `gpt-3.5-turbo-instruct` or other non-chat models. - Batch API support. - Fine-tuning management.