π€ refactor: extract agent resolution + stream context from AIService.streamMessage() #2242
+762
β408
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Extract three cohesive blocks (~363 lines) from
streamMessage()into two new standalone modules, reducingaiService.tsfrom 1,708 to 1,367 lines (β341).Background
Part of Phase 1b: decomposing
AIService.streamMessage(). Prior extractions:This PR targets the next two highest-impact blocks identified in the analysis β the agent resolution block (largest cohesive section, 164 lines) and the plan+system-prompt blocks (combined 160+ lines). Both were selected because they have minimal or zero
this.*dependencies, allowing clean functional extraction.Implementation
Commit 1:
agentResolution.ts(290 lines)New
resolveAgentForStream(opts)handles:Service dependencies (
emitError,initStateManager) injected via options object. ReturnsResult<AgentResolutionResult, SendMessageError>for clean error propagation.Impact: aiService.ts 1,708 β 1,574 (β134)
Commit 2:
streamContextBuilder.ts(405 lines)Two exported functions + one moved helper:
buildPlanInstructions(opts)β pure function, zerothis.*:buildStreamSystemContext(opts)β pure function, zerothis.*:discoverAvailableSubagentsForToolContext()β moved fromaiService.ts(re-exported for test compatibility).Impact: aiService.ts 1,574 β 1,367 (β207). 15 imports removed.
Cumulative Progress
agentResolution.tsstreamContextBuilder.tsValidation
make typecheck: β passesmake static-check: β all checks passmake test: β 3,446 passed, 0 failedRisks
Low risk β purely mechanical extractions with no behavioral changes. All code paths preserved 1:1. The
Resultreturn type inresolveAgentForStreamcorrectly propagates the disabled-agentErrcase. The plan/system-prompt functions are completely pure (no service dependencies).Generated with
muxβ’ Model:anthropic:claude-opus-4-6β’ Thinking:xhighβ’ Cost:$234.49