refactor: extract testable per-chunk parsers from each provider's streamChat loop#1071
Merged
Merged
Conversation
…dd 27 parser tests
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
… doc idGenerator, default-case + message.content tests)
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
Closes the gap left from #1070's review. Each AI provider's stream-parsing logic was buried inside the for-await loop of
streamChat, with mutable state (toolUseIdsByIndex,toolCallIndexToId, token counters) co-mingled withcontinuation.yield. Hard to test without mocking URLSession.This PR extracts a per-provider
parseChunk(_:state:)static function plus a state struct. The for-await loop becomes a thin shell that decodes lines and forwards events. 27 new tests cover the parsers directly.Refactor shape (same pattern per provider)
The for-await loop in
streamChat:Same shape for OpenAI/Compatible (with a
(events, shouldBreak)return for Ollama'sdone: true) and Gemini (with an injectedidGenerator: () -> Stringso tests can pin the synthetic UUID Gemini doesn't provide).What's covered now
AnthropicProviderParserTests(12 tests):.textDelta.toolUseStart.toolUseEndand clears the mappingstreamingFailedOpenAICompatibleProviderParserTests(8 tests):.textDelta.toolUseStart, subsequent chunks emit.toolUseDeltaonly.toolUseEndfor all tracked callsGeminiProviderParserTests(7 tests):.textDelta{}on invalid input, round-trips valid objectsWhat was demoted
Three private helpers became internal-static (or were extracted from instance methods to static):
AnthropicProvider.decodeStreamLine,parseChunkOpenAICompatibleProvider.decodeStreamLine,parseChunk,handleToolCallDeltas,handleOllamaToolCallsGeminiProvider.decodeStreamLine,parseChunk,encodeArgsToJSONStringThe instance method
GeminiProvider.encodeArgsToJSONStringwas removed — the static replaces it.Risk
Low. The for-await loop is a thin wrapper around the same logic that was inline before. Behavior should be identical. Tests lock the wire-shape contracts that were previously fragile.
CHANGELOG
No entry. This is a pure refactor with no user-visible behavior change.
Lint
swiftlint lint --strictclean across all 819 files.Test plan
xcodebuild test -only-testing:TableProTests/AnthropicProviderParserTestsxcodebuild test -only-testing:TableProTests/OpenAICompatibleProviderParserTestsxcodebuild test -only-testing:TableProTests/GeminiProviderParserTests