Skip to content

Refactor: Extract bootstrap and command handlers into separate modules#26

Merged
chinmaymk merged 1 commit intomainfrom
claude/refactor-index-ts-THuE5
Mar 11, 2026
Merged

Refactor: Extract bootstrap and command handlers into separate modules#26
chinmaymk merged 1 commit intomainfrom
claude/refactor-index-ts-THuE5

Conversation

@chinmaymk
Copy link
Copy Markdown
Owner

Summary

This refactor extracts the monolithic src/index.ts into focused, single-responsibility modules to improve maintainability and testability. The main entry point now orchestrates three distinct phases: early exits (help, version, exec), bootstrap (config + subsystem initialization), and interface launching.

Key Changes

  • New src/bootstrap.ts: Centralizes all subsystem initialization (provider, tools, middleware, skills, memory, MCP, observability) into a single bootstrap() function that returns an AppContext object. This eliminates ~200 lines of setup code from the main entry point and makes the initialization sequence explicit and reusable.

  • New src/interfaces/commands.ts: Extracts standalone command handlers (runExecScript, runSkillCommand, showContext, runMemoryCommand) that don't require a full interface loop. These were previously inline in main().

  • New src/interfaces/help.ts: Moves the HELP text constant to a dedicated module for better organization and to reduce clutter in the main entry point.

  • Refactored src/index.ts: Restructured into three logical phases:

    1. Early exits (handleEarlyExits): Version, help, exec, skill commands — no config/bootstrap needed
    2. Standalone commands (handleStandaloneCommands): Show context, memory operations — need bootstrap but no interface
    3. Interface launchers: Separate functions for each interface type (CLI, REPL, HTTP, MCP) that consume the AppContext
  • Signal handling: Extracted onSignals() helper to centralize SIGINT/SIGTERM registration and removal, making cleanup more consistent across interfaces.

Implementation Details

  • The AppContext type encapsulates all state needed by interface layers, eliminating the need to pass dozens of individual parameters.
  • Bootstrap is now a pure function with no side effects on the main process (no signal handlers, no process.exit calls).
  • Interface launchers are now independent functions that can be tested or reused without coupling to the main entry point.
  • The refactor maintains 100% behavioral compatibility — no changes to CLI flags, config loading, or runtime behavior.

https://claude.ai/code/session_01RVH28SjMU97CoJVbkfWK8T

…used modules

Split the 570-line monolithic index.ts into clean, single-responsibility modules:
- src/bootstrap.ts: wires up provider, tools, middleware, skills, memory, MCP, observability
- src/interfaces/commands.ts: skill management, memory CLI, context display, exec script
- src/interfaces/help.ts: help text constant
- src/index.ts: thin orchestrator with named launcher functions per interface

Also improved signal handler cleanup (use process.off instead of removeAllListeners)
and deduplicated the sidecar MCP startup pattern.

https://claude.ai/code/session_01RVH28SjMU97CoJVbkfWK8T
@chinmaymk chinmaymk merged commit f3b68e2 into main Mar 11, 2026
1 check passed
@chinmaymk chinmaymk deleted the claude/refactor-index-ts-THuE5 branch March 11, 2026 22:49
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.

2 participants