π€ refactor: decompose AIService.streamMessage() β Phase 1b Part 2 #2243
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
Continues the decomposition of the monolithic
AIService.streamMessage()method, extracting self-contained concerns into focused modules and simplifying the orchestration code that remains.Background
AIServicewas 1,367 lines withstreamMessage()accounting for ~900 of those. Previous PRs (#2242) extracted agent resolution and stream context building. This PR continues with simulation, tool assembly, model resolution, and structural cleanup.Implementation
Commit 1: Extract simulation + tool assembly
streamSimulation.ts(185 lines):simulateContextLimitErrorandsimulateToolPolicyNoopβ synthetic stream event sequences for OpenAI SDK testing features (forceContextLimitError,simulateToolPolicyNoop).toolAssembly.ts(235 lines):applyToolPolicyAndExperiments(tool policy + PTC lazy-loading) andcaptureMcpToolTelemetry(MCP stats + telemetry emission). Moves the PTC singleton (getPTCModules) entirely out ofaiService.ts.Commit 2: Model resolution + MCP telemetry + DRY cleanup
providerModelFactory.ts: AddedresolveAndCreateModel()combining xAI Grok variant mapping, gateway resolution, and model creation into a single call.for-ofloop.deleteAbortedPlaceholderhelper.@paramblock, removed backward-compat re-export.Commit 3: Options bag + safeClone + inline cleanup
StreamMessageOptions: ConvertedstreamMessage()from 19 positional parameters to a typed options bag. Call sites are now self-documenting with named fields instead ofundefinedplaceholders.safeClone<T>(): Extracted DRY utility for the duplicatedstructuredClone-with-JSON-fallback pattern (used in 2 debug snapshot capture sites).providerForMessagesalias, no-op OpenAI debug block, unusedworkspacevariable, stale comments. MovedassistantMessageIdcreation before PTC callback for clearer closure capture.Metrics
aiService.tslinesValidation
make test)make static-check)Generated with
muxβ’ Model:anthropic:claude-opus-4-6β’ Thinking:xhighβ’ Cost:$251.22