MarshalContent encodes message content blocks in legacy fantasy envelope format. Retained for backward-compatible test fixtures that create legacy-format DB rows. Production write paths use MarshalParts instead.
(blocks []fantasy.Content, fileIDs map[int]uuid.UUID)
| 653 | // that create legacy-format DB rows. Production write paths use |
| 654 | // MarshalParts instead. |
| 655 | func MarshalContent(blocks []fantasy.Content, fileIDs map[int]uuid.UUID) (pqtype.NullRawMessage, error) { |
| 656 | if len(blocks) == 0 { |
| 657 | return pqtype.NullRawMessage{}, nil |
| 658 | } |
| 659 | |
| 660 | encodedBlocks := make([]json.RawMessage, 0, len(blocks)) |
| 661 | for i, block := range blocks { |
| 662 | encoded, err := json.Marshal(block) |
| 663 | if err != nil { |
| 664 | return pqtype.NullRawMessage{}, xerrors.Errorf( |
| 665 | "encode content block %d: %w", |
| 666 | i, |
| 667 | err, |
| 668 | ) |
| 669 | } |
| 670 | if fid, ok := fileIDs[i]; ok { |
| 671 | // Inline file_id injection into the fantasy envelope's |
| 672 | // data sub-object, stripping inline data. |
| 673 | var envelope struct { |
| 674 | Type string `json:"type"` |
| 675 | Data struct { |
| 676 | MediaType string `json:"media_type"` |
| 677 | Data json.RawMessage `json:"data,omitempty"` |
| 678 | FileID string `json:"file_id,omitempty"` |
| 679 | ProviderMetadata *json.RawMessage `json:"provider_metadata,omitempty"` |
| 680 | } `json:"data"` |
| 681 | } |
| 682 | if err := json.Unmarshal(encoded, &envelope); err == nil { |
| 683 | envelope.Data.FileID = fid.String() |
| 684 | envelope.Data.Data = nil |
| 685 | if patched, err := json.Marshal(envelope); err == nil { |
| 686 | encoded = patched |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | encodedBlocks = append(encodedBlocks, encoded) |
| 691 | } |
| 692 | |
| 693 | data, err := json.Marshal(encodedBlocks) |
| 694 | if err != nil { |
| 695 | return pqtype.NullRawMessage{}, xerrors.Errorf("encode content blocks: %w", err) |
| 696 | } |
| 697 | return pqtype.NullRawMessage{RawMessage: data, Valid: true}, nil |
| 698 | } |
| 699 | |
| 700 | // MarshalToolResult encodes a single tool result in the legacy |
| 701 | // tool-row format. Retained for test fixtures that create |