Skip to content

refactor: Split useChatHandler into focused modules (#173) - #188

Merged
will-lamerton merged 3 commits into
mainfrom
refactor/chat-handler-split
Dec 21, 2025
Merged

will-lamerton merged 3 commits into
mainfrom
refactor/chat-handler-split

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

Summary

Refactored the 815-line useChatHandler.tsx into a focused module structure following the established pattern in the codebase (similar to source/utils/logging/, source/types/, etc.).

Part 1 of 5 for issue #173 - This PR tackles the highest priority file (useChatHandler.tsx).

Changes

New Structure

source/hooks/chat-handler/
├── conversation/
│   ├── conversation-loop.tsx (~450 lines) - Main conversation orchestration
│   ├── conversation-loop.spec.ts
│   ├── tool-executor.tsx (~110 lines) - Direct tool execution
│   └── tool-executor.spec.ts
├── utils/
│   ├── tool-filters.ts (~65 lines) - Tool validation and deduplication
│   ├── tool-filters.spec.ts
│   ├── context-checker.tsx (~70 lines) - Context usage monitoring
│   ├── context-checker.spec.ts
│   ├── message-helpers.tsx (~40 lines) - Error display utilities
│   └── message-helpers.spec.ts
├── state/
│   ├── streaming-state.ts (~20 lines) - State reset helper
│   └── streaming-state.spec.ts
├── useChatHandler.tsx (~200 lines) - Simplified main hook
├── useChatHandler.spec.ts
├── types.ts - Shared type definitions
└── index.ts - Clean re-exports

What Changed

  • Before: Single 815-line file with 534-line processAssistantResponse function
  • After: 10 focused modules, each <500 lines with single responsibility
  • All new modules include comprehensive test coverage (15 new test files)
  • Import path updated: @/hooks/useChatHandler → @/hooks/chat-handler

Test Coverage

  • ✅ All existing tests pass (2528 tests)
  • ✅ 15 new test files with unit tests for each module
  • ✅ Build succeeds
  • ✅ TypeScript strict mode passes
  • ✅ Biome linting passes

Test Plan

  • Run pnpm run test:all - All tests pass
  • Run pnpm run build - Build succeeds
  • Verify no circular dependencies
  • Check that application runs without errors

Related to #173

Refactored the 815-line useChatHandler.tsx into a focused module structure:

- conversation/conversation-loop.tsx - Main conversation orchestration
- conversation/tool-executor.tsx - Direct tool execution logic
- utils/tool-filters.ts - Tool call validation and deduplication
- utils/context-checker.tsx - Context usage monitoring
- utils/message-helpers.tsx - Error display utilities
- state/streaming-state.ts - Streaming state management
- useChatHandler.tsx - Simplified main hook (~200 lines)
- types.ts - Shared type definitions
- index.ts - Clean re-exports

All new modules include comprehensive test coverage.
- Use ink-testing-library to properly test React hooks
- Test hook in actual React rendering context
- Validate return interface and streaming state
- Test different development modes and configurations
- All 12 tests passing with proper React component rendering
@Avtrkrb

Avtrkrb commented Dec 21, 2025

Copy link
Copy Markdown
Member

@will-lamerton Changes look good !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants