---
task_id: 1
depth: 1
parent: ../manifest.yaml
siblings: [0:Backend Abstraction Layer: Create src/cleave/tui/backends/ with base.py (abstract Backend class), claude.py (wrap existing SDKSession), and registry in __init__.py. Define Message types, BackendConfig, connection lifecycle, and query streaming interface., 2:TUI Integration: Refactor SDKSession to use Backend interface. Update ControlPanel for backend/model selection. Add connection status indicator. Update pyproject.toml with optional deps.]
tdd: false
---

# Task 1: OpenAI-Compatible Backend + Settings: Create backends/openai_compat.py for Ollama/vLLM/llama.cpp. Extend ~/.cleave/settings.yaml with backend/backends configuration. Add backend auto-detection.

## Mission

Create OpenAI-compatible backend for local/cloud inference and extend settings to support multi-backend configuration.

## Scope

**In:**
- OpenAICompatBackend for Ollama, vLLM, llama.cpp, OpenAI
- Provider endpoint defaults and model lists
- Streaming chat completion
- Conversation history management
- Settings extension: backend, backends configuration
- Backend detection (Claude auth, Ollama socket, OpenAI env)
- Helper functions for backend settings

**Out:**
- TUI widget integration (Task 2)
- Actual async connection testing (requires openai package)

**Depends on:**
- Task 0: Backend, BackendConfig, Message from base.py

**Provides:**
- `from cleave.tui.backends.openai_compat import OpenAICompatBackend`
- `cleave.core.settings.get_active_backend()`, `set_active_backend()`
- `cleave.core.settings.detect_available_backends()`

## Result

**Status:** SUCCESS

**Summary:**
Created OpenAI-compatible backend that works with any OpenAI-compatible API (Ollama, vLLM, llama.cpp, OpenAI). Extended settings.py with backend configuration including per-backend settings (base_url, default_model, api_key_env, models). Added backend detection and helper functions.

**Artifacts:**
- `src/cleave/tui/backends/openai_compat.py` - OpenAICompatBackend class
- `src/cleave/core/settings.py` - Extended with BackendSettings, backend config
- `src/cleave/__init__.py` - New exports for backend settings

**Decisions:**
- Single OpenAICompatBackend handles all OpenAI-compatible APIs (provider param for defaults)
- PROVIDER_ENDPOINTS and PROVIDER_MODELS provide sensible defaults
- Conversation history maintained within backend for multi-turn support
- reset_conversation() method allows clearing context
- detect_available_backends() checks Claude SDK auth, Ollama socket, OpenAI env var

**Verification:**
- Command: `PYTHONPATH=src python3 -c "from cleave.tui.backends.openai_compat import OpenAICompatBackend; print(OpenAICompatBackend('ollama').name)"`
- Output: `OpenAI (ollama)`
- Edge cases: Works without openai package (graceful error on connect)

**Alignment:** SUCCESS - Full OpenAI-compatible backend with settings integration
