summaryrefslogtreecommitdiff
path: root/models/models.go
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2026-02-25 16:57:55 +0300
committerGrail Finder <wohilas@gmail.com>2026-02-25 16:57:55 +0300
commit9f51bd385336e7b314316c372bc31de2a3c374f4 (patch)
treebaefc4ba8e5bec89d5df89c05be8d9df9d820025 /models/models.go
parentb386c1181f0424418999fe143f1285d395b0db0f (diff)
Fix: text manipulation for multimodal messages
Diffstat (limited to 'models/models.go')
-rw-r--r--models/models.go60
1 files changed, 60 insertions, 0 deletions
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 {