Repository navigation
refactor: add chat_message_role enum and content_version column - #23042
Conversation
670f04b to
d1a9408
Compare
johnstcn
left a comment
There was a problem hiding this comment.
Nothing blocking, thanks for the refactor!
| // ContentVersionV0 is the legacy content format. Parsing uses | ||
| // role-aware heuristics to distinguish fantasy envelope format | ||
| // from SDK parts. | ||
| ContentVersionV0 int16 = 0 |
There was a problem hiding this comment.
Any particular reason to use this data type?
There was a problem hiding this comment.
Only that it matches the DB type (smallint). We could use a more regular type but that takes up a little more space in the DB.
| if err := json.Unmarshal(raw.RawMessage, &parts); err != nil { | ||
| return nil, xerrors.Errorf("parse %s content: %w", role, err) | ||
| } | ||
| if !hasNonEmptyType(parts) { |
There was a problem hiding this comment.
Don't use no double negatives!
There was a problem hiding this comment.
Good catch. Removed the !hasNonEmptyType guard from parseContentV1 entirely. V1 content is always written by MarshalParts so parts will always have types. The guard was carried over from the legacy parsing functions where it made sense as a heuristic fallback.
There was a problem hiding this comment.
Double check the migration number before merge; Mikes pr has that number I think
There was a problem hiding this comment.
Will check before merge, thanks.
bd42887 to
3805d95
Compare
…sion column Adds a Postgres enum for chat_messages.role (previously text) and a content_version column that replaces the structural heuristic used to distinguish legacy fantasy envelope format from SDK parts. Migration 000434 creates the chat_message_role enum, converts the role column, rebuilds the partial index, and adds content_version (smallint, default 0). Version 0 rows use the existing role-aware heuristic detection. Version 1 rows (all new inserts) store content as []ChatMessagePart JSON uniformly across all roles, including system messages. ParseContent now takes database.ChatMessage directly and dispatches on content version internally. Callers never see or pass version numbers. Unknown versions error instead of falling through. All string(codersdk.ChatMessageRole*) casts at DB write sites are replaced with database.ChatMessageRole* constants generated by sqlc. System messages are now written as []ChatMessagePart via MarshalParts instead of bare JSON strings, eliminating the V1 special case.
3805d95 to
b17402c
Compare
Follow-up to #22958. Adds a Postgres enum for
chat_messages.roleand acontent_versioncolumn that replaces the structural heuristic for distinguishing content formats.Migration 000434 creates
chat_message_roleenum, converts the role column from text, rebuilds the partial index, and addscontent_version smallint NOT NULL. The column is added withDEFAULT 0to backfill existing rows, then the default is dropped so future inserts must specify the version explicitly.Content versioning: version 0 rows (all existing data) use the role-aware heuristic detection from #22958. Version 1 rows (all new inserts) store content as
[]ChatMessagePartJSON uniformly, including system messages.ParseContenttakesdatabase.ChatMessagedirectly and dispatches on version internally, callers never see version numbers. Unknown versions error instead of falling through.Role enum: eliminates
string(codersdk.ChatMessageRole*)casts at all 9 DB write sites andcodersdk.ChatMessageRole(m.Role)unvalidated casts at read sites. Invalid roles are now caught at the DB layer.System messages: now written as
[]ChatMessagePartviaMarshalPartsin V1, removing the special case that kept them as bare JSON strings. V0 system messages still parse throughparseSystemRoleas before.Refs #22958