Skip to content

Latest commit

 

History

History
135 lines (110 loc) · 4.66 KB

File metadata and controls

135 lines (110 loc) · 4.66 KB

Final Refactoring Plan for Provider-Agnostic Architecture

Objective

Complete the provider-agnostic refactoring to enable clean OpenCode integration alongside Claude without code duplication.

Current Status

  • ✅ Service layer created (9 services, 838 lines total)
  • ✅ Provider behavior configuration implemented
  • ✅ Deferred responses migrated to DiffManager
  • ✅ Performance optimizations completed
  • ❌ Protocol files still 4-5x larger than target (1670 lines vs 300-400 target)

Critical Success Metrics

  1. claude.lua: Reduce from 1008 lines to ~200-250 lines
  2. claude/diff.lua: Reduce from 375 lines to ~50-75 lines
  3. claude/tools.lua: Reduce from 287 lines to ~50-75 lines
  4. Total protocol code: ~300-400 lines (currently 1670)

Phase 1: Extract Tool Schemas (30 minutes)

Goal: Reduce claude/tools.lua from 287 to ~50 lines

  1. Create Schema Storage

    • Create lua/diffusion/schemas/claude/ directory
    • Move tool schemas from lines 178-284 to separate JSON/Lua files
    • One file per tool or single combined schema file
  2. Simplify Tool Module

    • Keep only delegation functions (lines 87-155)
    • Load schemas dynamically from files
    • Remove all descriptive text and schema definitions

Phase 2: Extract Message Handling (1 hour)

Goal: Reduce claude.lua from 1008 to ~200 lines

  1. Create MessageService

    • New file: services/messages.lua
    • Move all message processing logic from claude.lua
    • Handle initialize, tools/list, tool calls, completions
  2. Extract Connection Management

    • Move WebSocket message handling to ConnectionService
    • Move client management logic
    • Keep only routing in claude.lua
  3. Remove Redundant Code

    • Delete commented legacy code blocks
    • Remove AppleScript functions (lines 820-844)
    • Consolidate duplicate validation logic

Phase 3: Slim Diff Module (30 minutes)

Goal: Reduce claude/diff.lua from 375 to ~50 lines

  1. Remove File Operations

    • Delete file reading logic (lines 87-113)
    • Use ToolService for all file operations
    • Keep only parameter translation
  2. Extract Buffer Management

    • Move remaining buffer operations to BufferService
    • Remove direct vim.api calls
    • Delegate all UI operations
  3. Simplify Event Handlers

    • Reduce event handlers to single-line delegations
    • Move response formatting to MessageService
    • Remove all scheduling logic

Phase 4: Final Cleanup (30 minutes)

  1. Remove All Business Logic from Protocol

    • Ensure protocol files only:
      • Receive messages
      • Translate parameters
      • Delegate to services
      • Return responses
  2. Consolidate Protocol Files

    • Consider merging claude/diff.lua and claude/tools.lua into claude.lua
    • If kept separate, ensure clear boundaries
    • Remove all cross-file dependencies
  3. Verify Provider Agnosticism

    • No protocol-specific conditionals in services
    • All provider differences in configuration
    • Services work identically for any provider

Expected Final Structure

protocol/
├── claude.lua (~200 lines)
│   ├── Connection setup (50 lines)
│   ├── Message routing (100 lines)
│   └── Service delegation (50 lines)
├── claude/
│   ├── diff.lua (~50 lines) - Optional, could merge
│   └── tools.lua (~50 lines) - Optional, could merge
└── opencode.lua (~200-250 lines) - Future, same pattern

services/ (No changes needed - already complete)
├── buffer.lua ✅
├── connection.lua ✅
├── diagnostics.lua ✅
├── discovery.lua ✅
├── events.lua ✅
├── messages.lua (NEW - to be created)
├── tmux.lua ✅
├── tools.lua ✅
├── ui.lua ✅
└── workspace.lua ✅

Implementation Order

  1. Start with Phase 1 (schemas) - Easiest, immediate impact
  2. Then Phase 3 (diff) - Quick wins, clear extraction targets
  3. Then Phase 2 (messages) - Most complex but highest impact
  4. Finally Phase 4 (cleanup) - Polish and verification

Success Validation

  • Protocol files total < 400 lines
  • No business logic in protocol layer
  • All services provider-agnostic
  • OpenCode can be added by copying claude.lua and changing:
    • Connection details
    • Message format translation
    • Tool name mappings
  • Zero code duplication between providers

Time Estimate

  • Total: 2.5 - 3 hours of focused work
  • Can be done incrementally
  • Each phase independently valuable

Note for Codex

Focus on aggressive extraction. When in doubt, move code to services. Protocol files should be "dumb pipes" that only translate between wire format and service calls. Think of protocol handlers as thin adapters, not implementations.