extractMessageContent extracts text content from a message content field. Handles both string content and array content (multimodal messages). For array content, extracts text from all text-type elements.
(content gjson.Result)
| 774 | // Handles both string content and array content (multimodal messages). |
| 775 | // For array content, extracts text from all text-type elements. |
| 776 | func extractMessageContent(content gjson.Result) string { |
| 777 | // String content: "Hello world" |
| 778 | if content.Type == gjson.String { |
| 779 | return content.String() |
| 780 | } |
| 781 | |
| 782 | // Array content: [{"type":"text","text":"Hello"},{"type":"image",...}] |
| 783 | if content.IsArray() { |
| 784 | var texts []string |
| 785 | content.ForEach(func(_, part gjson.Result) bool { |
| 786 | // Handle Claude format: {"type":"text","text":"content"} |
| 787 | if part.Get("type").String() == "text" { |
| 788 | if text := part.Get("text").String(); text != "" { |
| 789 | texts = append(texts, text) |
| 790 | } |
| 791 | } |
| 792 | // Handle OpenAI format: {"type":"text","text":"content"} |
| 793 | // Same structure as Claude, already handled above |
| 794 | return true |
| 795 | }) |
| 796 | if len(texts) > 0 { |
| 797 | return strings.Join(texts, " ") |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return "" |
| 802 | } |
| 803 | |
| 804 | func extractResponsesAPIContent(content gjson.Result) string { |
| 805 | if !content.IsArray() { |
no test coverage detected