Conversation
β¦essage()
Extract two more cohesive blocks from the monolithic streamMessage() method:
1. streamSimulation.ts: Synthetic stream event sequences for testing
- simulateContextLimitError(): context-exceeded error simulation
- simulateToolPolicyNoop(): tool-policy-disabled noop response
- Shared SimulationContext + deduplicated StreamStartEvent construction
2. toolAssembly.ts: Tool policy application + PTC experiment handling
- applyToolPolicyAndExperiments(): merges extra tools, applies policy,
lazy-loads PTC (typescript/prettier/QuickJS WASM) only when needed
- Moves the entire PTC lazy-loading singleton out of aiService.ts
Additional cleanup:
- Simplified 9 one-liner event forwards into a for-of loop
- Removed backwards-compat re-export (test now imports directly)
- Removed 12 PTC/simulation-specific imports from aiService.ts
aiService.ts: 1367 β 1180 lines (β187, β14%)
All 3,446 tests pass.
β¦bort-delete 1. Move model string resolution into ProviderModelFactory.resolveAndCreateModel(): - xAI Grok variant mapping (reasoning vs non-reasoning based on thinking level) - Gateway model string resolution - Model creation Removes normalizeGatewayModel + parseModelString imports from aiService.ts. 2. Extract MCP telemetry capture into captureMcpToolTelemetry() in toolAssembly.ts: - Stats computation (effective MCP stats, tool counts) - Telemetry event emission - Tool configuration logging Removes getRuntimeTypeForTelemetry + roundToBase2 imports from aiService.ts. 3. Trim 18-line @param JSDoc block on streamMessage() β type signatures are self-documenting, the comments just repeated parameter names. 4. DRY the duplicate abort-delete-placeholder pattern: Two identical 7-line blocks consolidated into a local helper closure. aiService.ts: 1180 β 1101 lines (β79, cumulative β266 from 1367 on main)
- Convert streamMessage() from 19 positional params to typed StreamMessageOptions bag. The old call sites (agentSession, replay-history CLI) are now self-documenting β no more `undefined` placeholder args or positional confusion. - Extract safeClone<T>() utility to DRY the structuredClone-with-JSON-fallback pattern (used in both debug snapshot capture sites). - Inline cleanups: - Remove providerForMessages alias (was just canonicalProviderName) - Remove no-op OpenAI debug log block - Simplify workspace existence check (drop unused variable) - Move assistantMessageId creation before PTC callback for clarity - Trim stale comments - Update postCompactionRetry tests: access options bag fields by name instead of positional indices.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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