refactor: replace AIProvider with ChatTransport content-block protocol#1057
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This was referenced May 7, 2026
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.
First PR against #1047 (AI Chat full rewrite). This is the foundation: replaces the flat text-only
AIProviderwith a content-block transport protocol that can carry text, tool-use, tool-result, and attachment blocks. No UI changes; no behavior change for end users.What changed
New abstractions in
TablePro/Core/AI/Chat/ChatTurn— replacesAIChatMessage. Carries[ChatContentBlock]instead of a flatcontent: String. Includes per-turnmodelIdandproviderIdslots for the model picker UI in the next phase. Backwards-compatible decoder reads either the newblocksfield or the legacycontentfield, so persisted conversations decode without a migration step.ChatContentBlock— discriminated enum:.text,.toolUse(ToolUseBlock),.toolResult(ToolResultBlock),.attachment(ContextItem). ManualCodablewith akinddiscriminator (Swift's auto-synthesized Codable can't handle enums with named-case associated values).ChatTransport— replacesAIProvider.streamChat(turns:options:)returnsAsyncThrowingStream<ChatStreamEvent, Error>.ChatStreamEvent— replacesAIStreamEvent. Cases:.textDelta,.toolUseStart,.toolUseDelta,.toolUseEnd,.usage. Tool events are wired into the type but no transport emits them yet (Phase 4 territory).JSONValue— strict Swift discriminated-union for arbitrary JSON. Used byToolUseBlock.inputandChatToolSpec.inputSchema. Round-trips losslessly across providers and throughCodablestorage. Tools decode to their typedInput: CodableviaJSONValue.decoded(as:).ContextItem— placeholder for@-mention attachments (Phase 3):.schema,.table,.currentQuery,.queryResult,.savedQuery,.file. Defined now soChatContentBlock.attachment(ContextItem)is closed; UI does not emit any yet.Migrated to
ChatTransportAnthropicProviderOpenAICompatibleProvider(covers OpenAI, OpenRouter, Ollama, Custom)GeminiProviderCopilotChatProviderAIChatInlineSource(consumer)All providers map their request format from
[ChatTurn](text-only for now viaturn.plainText) and yield.textDelta/.usageevents. Tool blocks emitted by future versions are accepted by the type system and ignored by current providers.Deleted
AIProviderprotocolAIChatMessagestructAIChatRoleenumAIStreamEventenumAIProviderErrorstays (it's the user-facing error type used across providers and UI).collectErrorBody(from:)moved to aChatTransportextension.AITokenUsagestays — it's the user-facing token count display type, no need to rename.Touched
AIProviderFactory—ResolvedProvider.provideris nowChatTransport.AIProviderDescriptor.makeProvider— returnsChatTransport.AIChatViewModel—messages: [ChatTurn]..content +=mutations replaced withappendText(_:)helper onChatTurn. Stream switch handles new tool events with explicitbreak.AIChatMessageView— readsmessage.plainTextinstead ofmessage.content.AIChatPanelView— same.AIConversation—messages: [ChatTurn].Why the design is what it is
JSONValueover[String: Any]shim or rawStringJSON: tools need to round-trip arbitrary structured input across the wire and throughCodablestorage.[String: Any]requires anAnyCodableshim. RawStringJSON loses structural validation across the type system.JSONValueis a closed enum that mirrors what JSONSerialization produces, isCodable/Sendable/Equatable/Hashablenatively, and exposes typed decode helpers for tool handlers (value.decoded(as: MyToolInput.self)).Manual
CodableforChatContentBlock: Swift's auto-synthesizedCodableflattens enum cases with associated values into a single key, which breaks round-trip across providers and across versions. Manualinit(from:)/encode(to:)with akinddiscriminator gives explicit, stable wire format that we can extend without breaking persisted conversations.Backwards-compatible decoder on
ChatTurn: persisted conversations from before this rewrite havecontent: String. The newinit(from: Decoder)reads eitherblocks: [...](new) or wrapscontentinto[.text(content)](legacy). Writes always use the new shape. No separate migration script needed.What's deferred
This PR ships the protocol foundation. The remaining umbrella items become separate PRs:
ChatTurn.modelId/.providerIdare wired but no UI consumes them yet.@-mention attachments + slash commands —ContextItemexists but no picker UI emits one.ChatContentBlock.attachment(ContextItem)is reachable through Codable but never produced.ChatToolSpecexists;ChatTransportOptions.tools: [ChatToolSpec]is wired through; no transport actually sends tools yet, noChatToolprotocol exists yet, no MCP bridging.CHANGELOG
No entry. Internal architectural refactor with no user-visible behavior change. Persisted conversations decode under the new types via the backwards-compatible decoder.
Test plan