parseUserRole uses a structural heuristic to distinguish legacy fantasy envelope from new SDK parts.
(raw pqtype.NullRawMessage)
| 447 | // parseUserRole uses a structural heuristic to distinguish legacy |
| 448 | // fantasy envelope from new SDK parts. |
| 449 | func parseUserRole(raw pqtype.NullRawMessage) ([]codersdk.ChatMessagePart, error) { |
| 450 | // Legacy: plain JSON string (very old format). |
| 451 | var text string |
| 452 | if err := json.Unmarshal(raw.RawMessage, &text); err == nil { |
| 453 | if strings.TrimSpace(text) == "" { |
| 454 | return nil, nil |
| 455 | } |
| 456 | return []codersdk.ChatMessagePart{codersdk.ChatMessageText(text)}, nil |
| 457 | } |
| 458 | |
| 459 | if isFantasyEnvelopeFormat(raw.RawMessage) { |
| 460 | return parseLegacyUserBlocks(raw) |
| 461 | } |
| 462 | |
| 463 | // New SDK format. |
| 464 | var parts []codersdk.ChatMessagePart |
| 465 | if err := json.Unmarshal(raw.RawMessage, &parts); err != nil { |
| 466 | return nil, xerrors.Errorf("parse user content: %w", err) |
| 467 | } |
| 468 | if !hasNonEmptyType(parts) { |
| 469 | return nil, nil |
| 470 | } |
| 471 | return parts, nil |
| 472 | } |
| 473 | |
| 474 | // parseLegacyUserBlocks decodes a user message stored in fantasy |
| 475 | // envelope format, extracting file_id references from the raw |
no test coverage detected