# Implementation Plan: OpenAI Provider Integration

## Phase 1: Core Client Implementation
- [ ] Task: Define OpenAI state and initialize client in `src/ai_client.py`.
    - [ ] Import `openai` (ensure it's added to `requirements.txt` if missing).
    - [ ] Add `_openai_client` and `_openai_history` module-level variables.
    - [ ] Implement `_classify_openai_error(exc)` for structured error mapping.
- [ ] Task: Implement `_send_openai` tool-call loop.
    - [ ] Implement function/tool definition conversion to OpenAI format.
    - [ ] Implement the core Chat Completions API call with streaming support.
    - [ ] Implement tool result packaging and the recursion loop (up to `MAX_TOOL_ROUNDS`).
    - [ ] Integrate `_reread_file_items` for context refresh after tool rounds.
- [ ] Task: Implement Multi-modal (Vision) support.
    - [ ] Add logic to `_send_openai` to process `screenshot` inputs by encoding them as base64 data URIs.
- [ ] Task: Implement model discovery.
    - [ ] Implement `_list_openai_models()` using the `client.models.list()` API.
- [ ] Task: Update `ai_client.py` utility functions.
    - [ ] Update `send()` dispatcher to route to `_send_openai`.
    - [ ] Update `reset_session()` to clear `_openai_history`.
- [ ] Task: Conductor - User Manual Verification 'Phase 1: Core Client Implementation' (Protocol in workflow.md)

## Phase 2: Configuration & Credentials
- [ ] Task: Update credential loading in `src/ai_client.py`.
    - [ ] Update `_load_credentials()` to include `openai` in the error message and loading logic.
- [ ] Task: Add cost tracking for OpenAI models in `src/cost_tracker.py`.
    - [ ] Add regex patterns and rates for `gpt-4o`, `gpt-4o-mini`, `o1`, and `o3-mini` to `MODEL_PRICING`.
- [ ] Task: Verify that `credentials.toml` works with the new provider section.
- [ ] Task: Conductor - User Manual Verification 'Phase 2: Configuration & Credentials' (Protocol in workflow.md)

## Phase 3: GUI & Controller Integration
- [ ] Task: Register `openai` as a provider.
    - [ ] Add `openai` to `PROVIDERS` in `src/gui_2.py`.
    - [ ] Add `openai` to `PROVIDERS` in `src/app_controller.py`.
- [ ] Task: Ensure model fetching works in the GUI.
    - [ ] Verify that clicking "Fetch Models" in the AI Settings panel correctly populates OpenAI models when the provider is selected.
- [ ] Task: Conductor - User Manual Verification 'Phase 3: GUI & Controller Integration' (Protocol in workflow.md)

## Phase 4: MMA Integration & Final Verification
- [ ] Task: Verify MMA compatibility.
    - [ ] Test a simple multi-agent workflow where a Tier 3 worker is configured to use `gpt-4o-mini`.
    - [ ] Verify that tool calls, history, and context injection work as expected within the tiered architecture.
- [ ] Task: Run full regression suite.
    - [ ] Ensure adding OpenAI hasn't introduced side effects for Gemini or Anthropic providers.
- [ ] Task: Conductor - User Manual Verification 'Phase 4: MMA Integration & Final Verification' (Protocol in workflow.md)
