feat: implement modular provider architecture with Codex CLI support

Implements a flexible provider pattern that supports both Claude Agent SDK
and OpenAI Codex CLI, enabling future expansion to other AI providers
(Cursor, OpenCode, etc.) with minimal changes.

## Architecture Changes

### New Provider System
- Created provider abstraction layer with BaseProvider interface
- Model-based routing: model prefix determines provider
  - `gpt-*`, `o*` → CodexProvider (subprocess CLI)
  - `claude-*`, `opus/sonnet/haiku` → ClaudeProvider (SDK)
- Providers implement common ExecuteOptions interface

### New Files Created
- `providers/types.ts` - Shared interfaces (ExecuteOptions, ProviderMessage, etc.)
- `providers/base-provider.ts` - Abstract base class
- `providers/claude-provider.ts` - Claude Agent SDK wrapper
- `providers/codex-provider.ts` - Codex CLI subprocess executor
- `providers/codex-cli-detector.ts` - Installation & auth detection
- `providers/codex-config-manager.ts` - TOML config management
- `providers/provider-factory.ts` - Model-based provider routing
- `lib/subprocess-manager.ts` - Reusable subprocess utilities

## Features Implemented

### Codex CLI Integration
- Spawns Codex CLI as subprocess with JSONL output
- Converts Codex events to Claude SDK-compatible format
- Supports both `codex login` and OPENAI_API_KEY auth methods
- Handles: reasoning, messages, commands, todos, file changes
- Extracts text from content blocks for non-vision CLI

### Conversation History
- Added conversationHistory support to ExecuteOptions
- ClaudeProvider: yields previous messages to SDK
- CodexProvider: prepends history as text context
- Follow-up prompts maintain full conversation context

### Image Upload Support
- Images embedded as base64 for vision models
- Image paths appended to prompt text for Read tool access
- Auto-mode: copies images to feature folder
- Follow-up: combines original + new images
- Updates feature.json with image metadata

### Session Model Persistence
- Added `model` field to Session and SessionMetadata
- Sessions remember model preference across interactions
- API endpoints accept model parameter
- Auto-mode respects feature's model setting

## Modified Files

### Services
- `agent-service.ts`:
  - Added conversation history building
  - Uses ProviderFactory instead of direct SDK calls
  - Appends image paths to prompts
  - Added model parameter and persistence

- `auto-mode-service.ts`:
  - Removed OpenAI model block restriction
  - Uses ProviderFactory for all models
  - Added image support in buildFeaturePrompt
  - Follow-up: loads context, copies images, updates feature.json
  - Returns to waiting_approval after follow-up

### Routes
- `agent.ts`: Added model parameter to /send endpoint
- `sessions.ts`: Added model field to create/update
- `models.ts`: Added Codex models (gpt-5.2, gpt-5.1-codex*)

### Configuration
- `.env.example`: Added OPENAI_API_KEY and CODEX_CLI_PATH
- `.gitignore`: Added provider-specific ignores

## Bug Fixes
- Fixed image path resolution (relative → absolute)
- Fixed Codex empty prompt when images attached
- Fixed follow-up status management (in_progress → waiting_approval)
- Fixed follow-up images not appearing in prompt text
- Removed OpenAI model restrictions in auto-mode

## Testing Notes
- Codex CLI authentication verified with both methods
- Image uploads work for both Claude (vision) and Codex (Read tool)
- Follow-up prompts maintain full context
- Conversation history persists across turns
- Model switching works per-session

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Kacper
2025-12-13 03:45:41 +01:00
parent 55603cb5c7
commit a65b16cbae
15 changed files with 2404 additions and 89 deletions

View File

@@ -0,0 +1,96 @@
/**
* Abstract base class for AI model providers
*/
import type {
ProviderConfig,
ExecuteOptions,
ProviderMessage,
InstallationStatus,
ValidationResult,
ModelDefinition,
} from "./types.js";
/**
* Base provider class that all provider implementations must extend
*/
export abstract class BaseProvider {
protected config: ProviderConfig;
protected name: string;
constructor(config: ProviderConfig = {}) {
this.config = config;
this.name = this.getName();
}
/**
* Get the provider name (e.g., "claude", "codex", "cursor")
*/
abstract getName(): string;
/**
* Execute a query and stream responses
* @param options Execution options
* @returns AsyncGenerator yielding provider messages
*/
abstract executeQuery(
options: ExecuteOptions
): AsyncGenerator<ProviderMessage>;
/**
* Detect if the provider is installed and configured
* @returns Installation status
*/
abstract detectInstallation(): Promise<InstallationStatus>;
/**
* Get available models for this provider
* @returns Array of model definitions
*/
abstract getAvailableModels(): ModelDefinition[];
/**
* Validate the provider configuration
* @returns Validation result
*/
validateConfig(): ValidationResult {
const errors: string[] = [];
const warnings: string[] = [];
// Base validation (can be overridden)
if (!this.config) {
errors.push("Provider config is missing");
}
return {
valid: errors.length === 0,
errors,
warnings,
};
}
/**
* Check if the provider supports a specific feature
* @param feature Feature name (e.g., "vision", "tools", "mcp")
* @returns Whether the feature is supported
*/
supportsFeature(feature: string): boolean {
// Default implementation - override in subclasses
const commonFeatures = ["tools", "text"];
return commonFeatures.includes(feature);
}
/**
* Get provider configuration
*/
getConfig(): ProviderConfig {
return this.config;
}
/**
* Update provider configuration
*/
setConfig(config: Partial<ProviderConfig>): void {
this.config = { ...this.config, ...config };
}
}