Skip to content

refactor(coderd): store all message types as SDK parts, remove fantasy envelope for new writes - #22821

Closed
kylecarbs wants to merge 2 commits into
mainfrom
fix/file-reference-user-messages
Closed

kylecarbs wants to merge 2 commits into
mainfrom
fix/file-reference-user-messages

Conversation

@kylecarbs

@kylecarbs kylecarbs commented Mar 9, 2026 •

Copy link
Copy Markdown
Member

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 Content types before persistence, and
since fantasy has no file-reference content type, these parts were serialized
as TextContent with 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.ChatMessagePart JSON directly in the database. Fantasy types are
only used at LLM dispatch time when building []fantasy.Message for the
provider.

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 IsFantasyEnvelopeFormat
detection in ParseContent.

codersdk/chats.go — Added ProviderMetadata json.RawMessage and
ProviderExecuted bool fields to ChatMessagePart for round-trip fidelity.
Provider metadata is stripped before returning to API clients.

coderd/chatd/chatprompt/chatprompt.go (core changes):

  • MarshalParts replaces MarshalContent, MarshalUserContent,
    marshalContentBlock, injectFileID
  • ParseContent now returns []codersdk.ChatMessagePart (was
    []fantasy.Content), handling SDK format, fantasy envelope, and plain
    strings
  • ConvertMessagesWithFiles rewritten: parses to SDK parts first, collects
    file IDs inline, converts to fantasy.MessagePart via partsToMessageParts
  • Removed ToMessageParts, patchFileContent, extractFileIDs
  • Removed all reasoning title code: reasoningSummaryTitle,
    compactReasoningSummaryTitle, reasoningSummaryHeadline,
    reasoningTitleFromContent, ReasoningTitleFromFirstLine

coderd/chatd/chatloop/chatloop.go:

  • stepResult.content changed from []fantasy.Content to
    []codersdk.ChatMessagePart
  • All streaming accumulation points build SDK parts directly
  • Reasoning title derivation and publishing removed
  • toResponseMessages rewritten for SDK parts
  • persistInterruptedStep updated for SDK parts

coderd/chatd/chatd.go:

  • persistStep splits on ChatMessagePartTypeToolResult instead of
    fantasy.AsContentType
  • All marshal calls use chatprompt.MarshalParts

coderd/database/db2sdk/db2sdk.go:

  • chatMessageParts delegates to chatprompt.ParseContent for all roles,
    with legacy toolResultRow fallback for old tool messages
  • Removed parseContentBlocks, contentBlockToPart, reasoningStoredTitle,
    toolResultOutputToRawJSON, toolResultOutputIsError, tryParseSDKParts

Backward compatibility

IsFantasyEnvelopeFormat checks whether the first JSON array element has a
"data" field starting with {. ParseContent tries SDK format first, falls
back to fantasy UnmarshalContent for legacy rows. Tool messages also fall
back to the [{"tool_call_id":...}] format.

Why this is safe

User messages only contain text, file, file-reference — none use
fantasy-specific metadata. Assistant/tool messages now store
provider_metadata as an opaque JSON blob on each part, preserving round-trip
fidelity for LLM replay (e.g., Anthropic cache hints).

@kylecarbs
kylecarbs force-pushed the fix/file-reference-user-messages branch 4 times, most recently from 3890484 to b288d05 Compare March 9, 2026 02:50
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
kylecarbs force-pushed the fix/file-reference-user-messages branch from b288d05 to be75517 Compare March 9, 2026 02:59
@kylecarbs kylecarbs changed the title fix(site): render file-reference parts as chips in user messages fix(coderd): store user message parts in SDK format to preserve file-reference structure Mar 9, 2026
…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.
@kylecarbs kylecarbs changed the title fix(coderd): store user message parts in SDK format to preserve file-reference structure refactor(coderd): store all message types as SDK parts, remove fantasy envelope for new writes Mar 10, 2026
@kylecarbs kylecarbs closed this Mar 11, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 11, 2026
@github-actions
github-actions Bot deleted the fix/file-reference-user-messages branch September 11, 2026 00:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant