Repository navigation
refactor(coderd): store all message types as SDK parts, remove fantasy envelope for new writes - #22821
Closed
kylecarbs wants to merge 2 commits into
Closed
refactor(coderd): store all message types as SDK parts, remove fantasy envelope for new writes#22821kylecarbs wants to merge 2 commits into
kylecarbs wants to merge 2 commits into
Conversation
kylecarbs
force-pushed
the
fix/file-reference-user-messages
branch
4 times, most recently
from
March 9, 2026 02:50
3890484 to
b288d05
Compare
User messages are now stored as []codersdk.ChatMessagePart JSON
directly in the DB content column, instead of the fantasy
{"type": ..., "data": {...}} envelope format. This means:
- File-reference parts keep their structured metadata (file_name,
start_line, end_line, content) in storage, so the API returns
them as proper file-reference ChatMessagePart types that the
frontend renders as styled chips.
- File parts store file_id and media_type directly on the part
instead of using injectFileID/ExtractFileID to hack them into
the fantasy envelope.
- MarshalUserContent is now just json.Marshal(parts).
- The chatd option structs carry []codersdk.ChatMessagePart
directly, with no separate fileIDs/mediaTypes maps.
The read path detects the storage format: new SDK format has
fields at the top level, legacy fantasy format has a "data"
object wrapper. Both are handled transparently.
Assistant and tool messages continue using the fantasy envelope
format since they originate from the LLM via fantasy.Content.
kylecarbs
force-pushed
the
fix/file-reference-user-messages
branch
from
March 9, 2026 02:59
b288d05 to
be75517
Compare
…ing titles All message roles (user, assistant, tool) now persist as []codersdk.ChatMessagePart JSON in the database instead of the fantasy envelope format. Fantasy types are only used at LLM dispatch time. Key changes: - MarshalParts replaces MarshalContent/MarshalUserContent/marshalContentBlock - ParseContent returns []codersdk.ChatMessagePart (was []fantasy.Content) - chatloop accumulates []codersdk.ChatMessagePart (was []fantasy.Content) - db2sdk delegates to chatprompt.ParseContent for all roles - ProviderMetadata stored as opaque JSON on ChatMessagePart for round-trip - Reasoning title extraction removed (reasoningSummaryTitle, etc.) - Legacy fantasy envelope format still readable for backward compat Removes ~500 lines of fantasy envelope marshaling, title injection, file ID injection, and duplicate content-to-part conversion code.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
File-reference parts in user messages rendered as plain text instead of styled
chips in the Agents chat UI. The root cause was the backend storage path: user
messages were converted into fantasy
Contenttypes before persistence, andsince fantasy has no
file-referencecontent type, these parts were serializedas
TextContentwith a formatted string.More broadly, using the fantasy envelope format (
{"type":"...","data":{...}})for all storage created complexity: dual-format detection, file ID
injection/extraction, reasoning title injection, and duplicate content↔part
conversion code across chatprompt and db2sdk.
Solution
Store all message roles (user, assistant, tool) as
[]codersdk.ChatMessagePartJSON directly in the database. Fantasy types areonly used at LLM dispatch time when building
[]fantasy.Messagefor theprovider.
What changed
Storage format — All new messages use the SDK part format:
[{"type":"text","text":"..."},{"type":"tool-call","tool_call_id":"...","tool_name":"...","args":{...}}]Legacy fantasy envelope rows are still readable via
IsFantasyEnvelopeFormatdetection in
ParseContent.codersdk/chats.go— AddedProviderMetadata json.RawMessageandProviderExecuted boolfields toChatMessagePartfor round-trip fidelity.Provider metadata is stripped before returning to API clients.
coderd/chatd/chatprompt/chatprompt.go(core changes):MarshalPartsreplacesMarshalContent,MarshalUserContent,marshalContentBlock,injectFileIDParseContentnow returns[]codersdk.ChatMessagePart(was[]fantasy.Content), handling SDK format, fantasy envelope, and plainstrings
ConvertMessagesWithFilesrewritten: parses to SDK parts first, collectsfile IDs inline, converts to
fantasy.MessagePartviapartsToMessagePartsToMessageParts,patchFileContent,extractFileIDsreasoningSummaryTitle,compactReasoningSummaryTitle,reasoningSummaryHeadline,reasoningTitleFromContent,ReasoningTitleFromFirstLinecoderd/chatd/chatloop/chatloop.go:stepResult.contentchanged from[]fantasy.Contentto[]codersdk.ChatMessageParttoResponseMessagesrewritten for SDK partspersistInterruptedStepupdated for SDK partscoderd/chatd/chatd.go:persistStepsplits onChatMessagePartTypeToolResultinstead offantasy.AsContentTypechatprompt.MarshalPartscoderd/database/db2sdk/db2sdk.go:chatMessagePartsdelegates tochatprompt.ParseContentfor all roles,with legacy
toolResultRowfallback for old tool messagesparseContentBlocks,contentBlockToPart,reasoningStoredTitle,toolResultOutputToRawJSON,toolResultOutputIsError,tryParseSDKPartsBackward compatibility
IsFantasyEnvelopeFormatchecks whether the first JSON array element has a"data"field starting with{.ParseContenttries SDK format first, fallsback to fantasy
UnmarshalContentfor legacy rows. Tool messages also fallback to the
[{"tool_call_id":...}]format.Why this is safe
User messages only contain
text,file,file-reference— none usefantasy-specific metadata. Assistant/tool messages now store
provider_metadataas an opaque JSON blob on each part, preserving round-tripfidelity for LLM replay (e.g., Anthropic cache hints).