Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pkg/tui/page/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chat

import (
"context"
"encoding/base64"
"fmt"
"log/slog"
"strings"
Expand Down Expand Up @@ -785,8 +786,8 @@ func (p *chatPage) handleInlineEditCancelled(msg messages.InlineEditCancelledMsg
}

// extractAttachmentsFromSession extracts attachments from a session message at the given position.
// Attachments are stored as text parts in MultiContent with format "Contents of <filename>: <dataURL>".
// TODO(krisetto): meh we can store and retrieve attachments better in the session itself
// Legacy attachments are stored as text parts in MultiContent with format "Contents of <filename>: <dataURL>".
// New attachments are stored as Document parts.
func (p *chatPage) extractAttachmentsFromSession(position int) []msgtypes.Attachment {
sess := p.app.Session()
if sess == nil || position < 0 || position >= len(sess.Messages) {
Expand All @@ -805,20 +806,35 @@ func (p *chatPage) extractAttachmentsFromSession(position int) []msgtypes.Attach
}

var attachments []msgtypes.Attachment
const prefix = "Contents of "
const legacyPrefix = "Contents of "

// Skip the first part (main text content), look for attachment parts
for i := 1; i < len(msg.MultiContent); i++ {
part := msg.MultiContent[i]

if part.Type == chat.MessagePartTypeDocument && part.Document != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No regression test accompanies this change, and this is the exact path that previously shipped the context-overflow bug. A unit test on extractAttachmentsFromSession covering the four cases (Document with InlineText, Document with InlineData, legacy Contents of text, and an empty Document) would lock the behavior. The existing newTestChatPage harness plus an app.New(...) session can drive it.

content := part.Document.Source.InlineText
if content == "" && len(part.Document.Source.InlineData) > 0 {
content = "data:" + part.Document.MimeType + ";base64," + base64.StdEncoding.EncodeToString(part.Document.Source.InlineData)
}
if content != "" {
attachments = append(attachments, msgtypes.Attachment{
Name: part.Document.Name,
Content: content,
})
}
continue
}

if part.Type != chat.MessagePartTypeText {
continue
}
text := part.Text
if !strings.HasPrefix(text, prefix) {
if !strings.HasPrefix(text, legacyPrefix) {
continue
}
// Parse "Contents of <filename>: <dataURL>"
rest := text[len(prefix):]
rest := text[len(legacyPrefix):]
before, after, ok := strings.Cut(rest, ": ")
if !ok {
continue
Expand Down
Loading