Skip to content

Conversation

daisuke-awaji
Copy link
Contributor

Refactor useAgentChat Hook into Modular Components

概要

1058行の巨大なuseAgentChatフックを7つの専門化されたフックに分割してリファクタリングしました。

問題

  • 単一責任の原則違反: 1つのフックが複数の責任を担っていた
  • 可読性の低下: 1000行を超えるファイルは理解・保守が困難
  • テストの困難さ: 複雑すぎてユニットテストが難しい
  • 再利用性の低下: 機能が密結合で部分的な再利用が困難

解決策

分割された新しいフック構造

1. useMessages (105行)

  • メッセージ状態管理
  • メッセージの永続化
  • ユーザー/アシスタントメッセージの追加

2. useRequestControl (53行)

  • AbortControllerの管理
  • 通信の中断・制御
  • リクエストライフサイクル管理

3. useChatUIState (40行)

  • ローディング状態
  • 推論状態
  • ツール実行状態

4. useSessionManager (131行)

  • セッション管理
  • タイトル生成
  • セッション切り替え

5. useToolExecution (205行)

  • ツール実行ロジック
  • ガードレール適用
  • エラーハンドリング

6. useStreamChat (457行)

  • ストリーミング通信処理
  • コンテンツブロック処理
  • メタデータ処理

7. useAgentChatRefactored (311行)

  • 全フックの統合
  • メイン API の提供
  • 元の useAgentChat と同じインターフェース

メリット

単一責任の原則: 各フックが明確な責任を持つ
テスト性の向上: 小さな単位でのテストが可能
再利用性の向上: 個別フックの他コンテキストでの利用
保守性の向上: 特定機能の理解・修正が容易
複雑性の削減: 500行を超えるファイルがない

統計

  • 元のファイル: 1058行
  • リファクタリング後: 1391行(型定義・エクスポート含む)
  • 最大ファイルサイズ: 457行(useStreamChat)
  • 平均ファイルサイズ: 約200行

使用方法

// 従来の useAgentChat と同じインターフェース
import { useAgentChatRefactored } from './hooks/refactored'

const chatHook = useAgentChatRefactored(modelId, systemPrompt, agentId, sessionId, options)

今後の作業

  • 元の useAgentChat を useAgentChatRefactored に置き換え
  • 個別フックの単体テスト作成
  • 統合テストの実行・検証

リファクタリングにより、コードの保守性と理解しやすさが大幅に向上しました。

remote-swe-app[bot] added 8 commits June 15, 2025 14:53
- Split 1058-line useAgentChat into 7 focused hooks:
  - useMessages: Message state management and persistence (105 lines)
  - useRequestControl: AbortController and request lifecycle (53 lines)
  - useChatUIState: UI state management (loading, reasoning, tools) (40 lines)
  - useSessionManager: Session management and title generation (131 lines)
  - useToolExecution: Tool execution and guardrail logic (205 lines)
  - useStreamChat: Streaming communication and content processing (457 lines)
  - useAgentChatRefactored: Main hook integrating all components (311 lines)

Benefits:
- Single Responsibility Principle: Each hook has one clear purpose
- Improved testability: Smaller, focused units can be tested independently
- Better reusability: Individual hooks can be used in different contexts
- Enhanced maintainability: Easier to understand and modify specific functionality
- Reduced complexity: No single file exceeds 500 lines

Total lines: 1058 → 1391 (includes type definitions and exports)
Net complexity reduction through better organization and separation of concerns.
- Fix import syntax error in useAgentChatRefactored.ts
- Apply automatic formatting with prettier to all refactored files
- Resolve all ESLint warnings related to code formatting
- Fix incorrect import paths in index.ts
- Replace AttachedImage import with local type definition
- Fix variable naming conflicts (messages -> messagesHook)
- Add explicit return statement for TypeScript compliance
- Make checkAndGenerateTitle async to match expected signature
- Resolve type mismatches in function parameters
- Add missing closing brace for useCallback function body
- Fix syntax error that was preventing compilation
- Fix persistMessage function calls to include sessionId parameter
- Update generateTitleForCurrentSession signature to accept messages parameter
- Wrap persistMessage calls with proper async/await and null checks
- Fix return type inconsistencies across hooks
- Add sessionId parameter to persistMessage in useStreamChat
- Update useToolExecution to accept 3-parameter streamChat function
- Add AbortSignal parameter to streamChat calls in useToolExecution
- Fix persistMessage wrapper in useAgentChatRefactored to handle optional sessionId
- Add sessionId parameter to addUserMessage function signature
- Resolve remaining TypeScript errors in useAgentChatRefactored calls
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