Complete the provider-agnostic refactoring to enable clean OpenCode integration alongside Claude without code duplication.
- ✅ 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)
- claude.lua: Reduce from 1008 lines to ~200-250 lines
- claude/diff.lua: Reduce from 375 lines to ~50-75 lines
- claude/tools.lua: Reduce from 287 lines to ~50-75 lines
- Total protocol code: ~300-400 lines (currently 1670)
-
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
- Create
-
Simplify Tool Module
- Keep only delegation functions (lines 87-155)
- Load schemas dynamically from files
- Remove all descriptive text and schema definitions
-
Create MessageService
- New file:
services/messages.lua - Move all message processing logic from claude.lua
- Handle initialize, tools/list, tool calls, completions
- New file:
-
Extract Connection Management
- Move WebSocket message handling to ConnectionService
- Move client management logic
- Keep only routing in claude.lua
-
Remove Redundant Code
- Delete commented legacy code blocks
- Remove AppleScript functions (lines 820-844)
- Consolidate duplicate validation logic
-
Remove File Operations
- Delete file reading logic (lines 87-113)
- Use ToolService for all file operations
- Keep only parameter translation
-
Extract Buffer Management
- Move remaining buffer operations to BufferService
- Remove direct vim.api calls
- Delegate all UI operations
-
Simplify Event Handlers
- Reduce event handlers to single-line delegations
- Move response formatting to MessageService
- Remove all scheduling logic
-
Remove All Business Logic from Protocol
- Ensure protocol files only:
- Receive messages
- Translate parameters
- Delegate to services
- Return responses
- Ensure protocol files only:
-
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
-
Verify Provider Agnosticism
- No protocol-specific conditionals in services
- All provider differences in configuration
- Services work identically for any provider
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 ✅
- Start with Phase 1 (schemas) - Easiest, immediate impact
- Then Phase 3 (diff) - Quick wins, clear extraction targets
- Then Phase 2 (messages) - Most complex but highest impact
- Finally Phase 4 (cleanup) - Polish and verification
- 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
- Total: 2.5 - 3 hours of focused work
- Can be done incrementally
- Each phase independently valuable
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.