Skip to content

refactor(tui): extract attachments from document parts in session#3484

Open
Piyush0049 wants to merge 2 commits into
docker:mainfrom
Piyush0049:refactor/extract-attachments
Open

refactor(tui): extract attachments from document parts in session#3484
Piyush0049 wants to merge 2 commits into
docker:mainfrom
Piyush0049:refactor/extract-attachments

Conversation

@Piyush0049

Copy link
Copy Markdown
Contributor

Description

This PR resolves a technical debt TODO in pkg/tui/page/chat/chat.go regarding how attachments are retrieved from session history when editing a past message in the TUI.

Previously, extractAttachmentsFromSession relied exclusively on a text-matching fallback ("Contents of <filename>: <dataURL>"). As the codebase has evolved to use MessagePartTypeDocument for structured file attachments, this old extraction logic would fail to properly restore modern attachments to the message editor.

Changes:

  • Added proper parsing for chat.MessagePartTypeDocument to safely extract InlineText and InlineData.
  • Implemented automatic conversion of binary InlineData payloads into standard data: URIs (base64) so that image attachments render properly in the UI.
  • Retained the original "Contents of " string matching strictly as a fallback (legacyPrefix) to ensure backward compatibility with older saved sessions.
  • Removed the outdated TODO comment.

Testing

  • go build ./pkg/tui/... succeeds.
  • go test ./pkg/tui/page/chat/... passes.

@Piyush0049 Piyush0049 requested a review from a team as a code owner July 6, 2026 09:15
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI kind/refactor PR refactors code without behavior change labels Jul 6, 2026
This commit updates extractAttachmentsFromSession to properly read modern MessagePartTypeDocument attachments instead of relying solely on the legacy 'Contents of' string matching fallback, addressing the TODO in chat.go.
@Piyush0049 Piyush0049 force-pushed the refactor/extract-attachments branch from 1104c82 to 85616ba Compare July 6, 2026 10:10
@Piyush0049

Copy link
Copy Markdown
Contributor Author

I just force-pushed to fix a trailing whitespace error that was failing the linter. The CI should pass now.

@dgageot

dgageot commented Jul 6, 2026

Copy link
Copy Markdown
Member

Blocking issue: binary attachments round-trip as raw base64 text in the prompt.

On the resend path, the extracted attachment has FilePath == "" and Content != "", so App.RunprocessInlineAttachment (pkg/app/app.go:638-642) embeds the data:<mime>;base64,... URI as literal prompt text inside <attached_file> tags. Nothing downstream parses it back into an image/document part (parseDataURI is only reached via MessagePartTypeImageURL parts). A 3 MB image becomes ~4 MB of base64 text (~1M tokens) — context overflow or a very expensive garbage request; arguably worse than the previous silent drop. It also violates Attachment.Content's documented "raw text content" contract (pkg/tui/messages/session.go:18-21).

Suggested fixes:

  • Extend messages.Attachment with MimeType/Data []byte (its doc comment anticipates this) and rebuild a Document part in App.Run, e.g. via chat.ProcessAttachmentWithMetadata; or
  • As a safe stopgap, extract only InlineText documents and keep skipping InlineData, with a short comment explaining why.

@dgageot dgageot left a comment

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.

See comment above

@Piyush0049

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I am working on the suggested fixes.

This fixes a context overflow issue where binary attachments (like images) were being serialized as massive base64 text strings when a message was edited and resent. We now extend messages.Attachment to properly hold MimeType and Data, and rebuild the Document part natively in App.Run.
@Piyush0049

Copy link
Copy Markdown
Contributor Author

@dgageot I've implemented the primary suggestion to address the context overflow on the resend path.

Specifically, I:

  1. Extended messages.Attachment with MimeType and Data []byte to natively support binary payloads (as the doc comment anticipated).
  2. Updated the TUI extraction logic in pkg/tui/page/chat/chat.go to stop base64-encoding InlineData.
  3. Added a new branch in App.Run to intercept binary attachments (len(att.Data) > 0) and route them through chat.ProcessAttachmentWithMetadata, safely rebuilding the Document part natively.

Please let me know if this looks good to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tui For features/issues/fixes related to the TUI kind/refactor PR refactors code without behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants