From 9f51bd385336e7b314316c372bc31de2a3c374f4 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Wed, 25 Feb 2026 16:57:55 +0300 Subject: Fix: text manipulation for multimodal messages --- models/models.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'models') diff --git a/models/models.go b/models/models.go index ee13928..21d71d8 100644 --- a/models/models.go +++ b/models/models.go @@ -329,6 +329,66 @@ func (m *RoleMsg) Copy() RoleMsg { } } +// GetText returns the text content of the message, handling both +// simple Content and multimodal ContentParts formats. +func (m *RoleMsg) GetText() string { + if !m.hasContentParts { + return m.Content + } + var textParts []string + for _, part := range m.ContentParts { + switch p := part.(type) { + case TextContentPart: + if p.Type == "text" { + textParts = append(textParts, p.Text) + } + case map[string]any: + if partType, exists := p["type"]; exists { + if partType == "text" { + if textVal, textExists := p["text"]; textExists { + if textStr, isStr := textVal.(string); isStr { + textParts = append(textParts, textStr) + } + } + } + } + } + } + return strings.Join(textParts, " ") +} + +// SetText updates the text content of the message. If the message has +// ContentParts (multimodal), it updates the text parts while preserving +// images. If not, it sets the simple Content field. +func (m *RoleMsg) SetText(text string) { + if !m.hasContentParts { + m.Content = text + return + } + var newParts []any + for _, part := range m.ContentParts { + switch p := part.(type) { + case TextContentPart: + if p.Type == "text" { + p.Text = text + newParts = append(newParts, p) + } else { + newParts = append(newParts, p) + } + case map[string]any: + if partType, exists := p["type"]; exists && partType == "text" { + p["text"] = text + newParts = append(newParts, p) + } else { + newParts = append(newParts, p) + } + default: + newParts = append(newParts, part) + } + } + m.ContentParts = newParts +} + // AddTextPart adds a text content part to the message func (m *RoleMsg) AddTextPart(text string) { if !m.hasContentParts { -- cgit v1.2.3