Skip to content

Consolidate chat handlers with base class pattern#33

Merged
gricha merged 1 commit intomainfrom
refactor/consolidate-chat-handlers
Jan 7, 2026
Merged

Consolidate chat handlers with base class pattern#33
gricha merged 1 commit intomainfrom
refactor/consolidate-chat-handlers

Conversation

@gricha
Copy link
Copy Markdown
Owner

@gricha gricha commented Jan 7, 2026

Summary

  • Refactor to eliminate ~95% code duplication between container and host chat handlers
  • Create abstract base classes BaseClaudeSession and BaseOpencodeSession with shared logic
  • Each handler now only implements spawn configuration and context-specific messages
  • Reduces code from ~850 lines to ~600 lines total (with better separation of concerns)

Changes

  • Create src/chat/types.ts with shared interfaces
  • Create src/chat/base-claude-session.ts for Claude Code handlers
  • Create src/chat/base-opencode-session.ts for OpenCode handlers
  • Update all 4 handlers to extend base classes
  • Add DEFAULT_CLAUDE_MODEL constant

Test plan

  • All existing tests pass (101/101)
  • Build succeeds
  • Lint/typecheck pass

🤖 Generated with Claude Code

Refactor to eliminate ~95% code duplication between container and host
chat handlers by extracting shared logic into abstract base classes:

- Create src/chat/types.ts with shared interfaces (ChatMessage,
  ClaudeStreamMessage, OpencodeStreamEvent, SpawnConfig, etc.)
- Create src/chat/base-claude-session.ts with shared Claude Code logic
  (buffer processing, stream message handling, interrupt, setModel)
- Create src/chat/base-opencode-session.ts with shared OpenCode logic
  (buffer processing, stream event handling, interrupt, setModel)
- Refactor ChatSession and HostChatSession to extend BaseClaudeSession
- Refactor OpencodeSession and HostOpencodeSession to extend
  BaseOpencodeSession
- Add DEFAULT_CLAUDE_MODEL constant to src/shared/constants.ts

Each handler now only implements:
- getSpawnConfig(): command and spawn options specific to container/host
- getLogPrefix(): logging identifier
- getNoOutputErrorMessage(): context-specific error message
- loadHistory() (OpenCode only): history loading strategy

This reduces code from ~850 lines across 4 files to ~250 lines in
handlers plus ~350 lines in shared base classes.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gricha gricha merged commit d3e20a1 into main Jan 7, 2026
6 checks passed
@gricha gricha deleted the refactor/consolidate-chat-handlers branch January 7, 2026 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant