MCPcopy Create free account
hub / github.com/coder/coder / parseUserRole

Function parseUserRole

coderd/x/chatd/chatprompt/chatprompt.go:449–472  ·  view source on GitHub ↗

parseUserRole uses a structural heuristic to distinguish legacy fantasy envelope from new SDK parts.

(raw pqtype.NullRawMessage)

Source from the content-addressed store, hash-verified

447// parseUserRole uses a structural heuristic to distinguish legacy
448// fantasy envelope from new SDK parts.
449func 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

Callers 1

parseLegacyContentFunction · 0.85

Calls 6

ChatMessageTextFunction · 0.92
isFantasyEnvelopeFormatFunction · 0.85
parseLegacyUserBlocksFunction · 0.85
hasNonEmptyTypeFunction · 0.85
UnmarshalMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected