Skip to content

Commit 4b2e52c

Browse files
plyghtadamdotdevin
andauthored
feat(tui): paste minimizing (#784)
Co-authored-by: adamdotdevin <[email protected]>
1 parent 6867658 commit 4b2e52c

4 files changed

Lines changed: 122 additions & 14 deletions

File tree

‎packages/tui/internal/app/prompt.go‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,32 @@ func (p Prompt) ToMessage(
2525
Created: float64(time.Now().UnixMilli()),
2626
},
2727
}
28+
29+
text := p.Text
30+
textAttachments := []*attachment.Attachment{}
31+
for _, attachment := range p.Attachments {
32+
if attachment.Type == "text" {
33+
textAttachments = append(textAttachments, attachment)
34+
}
35+
}
36+
for i := 0; i < len(textAttachments)-1; i++ {
37+
for j := i + 1; j < len(textAttachments); j++ {
38+
if textAttachments[i].StartIndex < textAttachments[j].StartIndex {
39+
textAttachments[i], textAttachments[j] = textAttachments[j], textAttachments[i]
40+
}
41+
}
42+
}
43+
for _, att := range textAttachments {
44+
source, _ := att.GetTextSource()
45+
text = text[:att.StartIndex] + source.Value + text[att.EndIndex:]
46+
}
47+
2848
parts := []opencode.PartUnion{opencode.TextPart{
2949
ID: id.Ascending(id.Part),
3050
MessageID: messageID,
3151
SessionID: sessionID,
3252
Type: opencode.TextPartTypeText,
33-
Text: p.Text,
53+
Text: text,
3454
}}
3555
for _, attachment := range p.Attachments {
3656
text := opencode.FilePartSourceText{
@@ -40,6 +60,8 @@ func (p Prompt) ToMessage(
4060
}
4161
var source *opencode.FilePartSource
4262
switch attachment.Type {
63+
case "text":
64+
continue
4365
case "file":
4466
fileSource, _ := attachment.GetFileSource()
4567
source = &opencode.FilePartSource{

‎packages/tui/internal/attachment/attachment.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"github.com/google/uuid"
55
)
66

7+
type TextSource struct {
8+
Value string `toml:"value"`
9+
}
10+
711
type FileSource struct {
812
Path string `toml:"path"`
913
Mime string `toml:"mime"`
@@ -46,6 +50,14 @@ func NewAttachment() *Attachment {
4650
}
4751
}
4852

53+
func (a *Attachment) GetTextSource() (*TextSource, bool) {
54+
if a.Type != "text" {
55+
return nil, false
56+
}
57+
ts, ok := a.Source.(*TextSource)
58+
return ts, ok
59+
}
60+
4961
// GetFileSource returns the source as FileSource if the attachment is a file type
5062
func (a *Attachment) GetFileSource() (*FileSource, bool) {
5163
if a.Type != "file" {

‎packages/tui/internal/components/chat/editor.go‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type editorComponent struct {
5656
exitKeyInDebounce bool
5757
historyIndex int // -1 means current (not in history)
5858
currentText string // Store current text when navigating history
59+
pasteCounter int
5960
}
6061

6162
func (m *editorComponent) Init() tea.Cmd {
@@ -129,28 +130,47 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
129130
text, err := strconv.Unquote(`"` + text + `"`)
130131
if err != nil {
131132
slog.Error("Failed to unquote text", "error", err)
132-
m.textarea.InsertRunesFromUserInput([]rune(msg))
133+
text := string(msg)
134+
if m.shouldSummarizePastedText(text) {
135+
m.handleLongPaste(text)
136+
} else {
137+
m.textarea.InsertRunesFromUserInput([]rune(msg))
138+
}
133139
return m, nil
134140
}
135141
if _, err := os.Stat(text); err != nil {
136142
slog.Error("Failed to paste file", "error", err)
137-
m.textarea.InsertRunesFromUserInput([]rune(msg))
143+
text := string(msg)
144+
if m.shouldSummarizePastedText(text) {
145+
m.handleLongPaste(text)
146+
} else {
147+
m.textarea.InsertRunesFromUserInput([]rune(msg))
148+
}
138149
return m, nil
139150
}
140151

141152
filePath := text
142153

143154
attachment := m.createAttachmentFromFile(filePath)
144155
if attachment == nil {
145-
m.textarea.InsertRunesFromUserInput([]rune(msg))
156+
if m.shouldSummarizePastedText(text) {
157+
m.handleLongPaste(text)
158+
} else {
159+
m.textarea.InsertRunesFromUserInput([]rune(msg))
160+
}
146161
return m, nil
147162
}
148163

149164
m.textarea.InsertAttachment(attachment)
150165
m.textarea.InsertString(" ")
151166
case tea.ClipboardMsg:
152167
text := string(msg)
153-
m.textarea.InsertRunesFromUserInput([]rune(text))
168+
// Check if the pasted text is long and should be summarized
169+
if m.shouldSummarizePastedText(text) {
170+
m.handleLongPaste(text)
171+
} else {
172+
m.textarea.InsertRunesFromUserInput([]rune(text))
173+
}
154174
case dialog.ThemeSelectedMsg:
155175
m.textarea = updateTextareaStyles(m.textarea)
156176
m.spinner = createSpinner()
@@ -392,6 +412,7 @@ func (m *editorComponent) Clear() (tea.Model, tea.Cmd) {
392412
m.textarea.Reset()
393413
m.historyIndex = -1
394414
m.currentText = ""
415+
m.pasteCounter = 0
395416
return m, nil
396417
}
397418

@@ -421,7 +442,13 @@ func (m *editorComponent) Paste() (tea.Model, tea.Cmd) {
421442

422443
textBytes := clipboard.Read(clipboard.FmtText)
423444
if textBytes != nil {
424-
m.textarea.InsertRunesFromUserInput([]rune(string(textBytes)))
445+
text := string(textBytes)
446+
// Check if the pasted text is long and should be summarized
447+
if m.shouldSummarizePastedText(text) {
448+
m.handleLongPaste(text)
449+
} else {
450+
m.textarea.InsertRunesFromUserInput([]rune(text))
451+
}
425452
return m, nil
426453
}
427454

@@ -490,6 +517,48 @@ func (m *editorComponent) getExitKeyText() string {
490517
return m.app.Commands[commands.AppExitCommand].Keys()[0]
491518
}
492519

520+
// shouldSummarizePastedText determines if pasted text should be summarized
521+
func (m *editorComponent) shouldSummarizePastedText(text string) bool {
522+
lines := strings.Split(text, "\n")
523+
lineCount := len(lines)
524+
charCount := len(text)
525+
526+
// Consider text long if it has more than 3 lines or more than 150 characters
527+
return lineCount > 3 || charCount > 150
528+
}
529+
530+
// handleLongPaste handles long pasted text by creating a summary attachment
531+
func (m *editorComponent) handleLongPaste(text string) {
532+
lines := strings.Split(text, "\n")
533+
lineCount := len(lines)
534+
535+
// Increment paste counter
536+
m.pasteCounter++
537+
538+
// Create attachment with full text as base64 encoded data
539+
fileBytes := []byte(text)
540+
base64EncodedText := base64.StdEncoding.EncodeToString(fileBytes)
541+
url := fmt.Sprintf("data:text/plain;base64,%s", base64EncodedText)
542+
543+
fileName := fmt.Sprintf("pasted-text-%d.txt", m.pasteCounter)
544+
displayText := fmt.Sprintf("[pasted #%d %d+ lines]", m.pasteCounter, lineCount)
545+
546+
attachment := &attachment.Attachment{
547+
ID: uuid.NewString(),
548+
Type: "text",
549+
MediaType: "text/plain",
550+
Display: displayText,
551+
URL: url,
552+
Filename: fileName,
553+
Source: &attachment.TextSource{
554+
Value: text,
555+
},
556+
}
557+
558+
m.textarea.InsertAttachment(attachment)
559+
m.textarea.InsertString(" ")
560+
}
561+
493562
func updateTextareaStyles(ta textarea.Model) textarea.Model {
494563
t := theme.CurrentTheme()
495564
bgColor := t.BackgroundElement()
@@ -551,6 +620,7 @@ func NewEditorComponent(app *app.App) EditorComponent {
551620
spinner: s,
552621
interruptKeyInDebounce: false,
553622
historyIndex: -1,
623+
pasteCounter: 0,
554624
}
555625

556626
return m

‎packages/tui/internal/components/chat/message.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,20 @@ func renderText(
196196
case opencode.UserMessage:
197197
ts = time.UnixMilli(int64(casted.Time.Created))
198198
base := styles.NewStyle().Foreground(t.Text()).Background(backgroundColor)
199-
words := strings.Fields(text)
200-
for i, word := range words {
201-
if strings.HasPrefix(word, "@") {
202-
words[i] = base.Foreground(t.Secondary()).Render(word + " ")
203-
} else {
204-
words[i] = base.Render(word + " ")
199+
text = ansi.WordwrapWc(text, width-6, " -")
200+
lines := strings.Split(text, "\n")
201+
for i, line := range lines {
202+
words := strings.Fields(line)
203+
for i, word := range words {
204+
if strings.HasPrefix(word, "@") {
205+
words[i] = base.Foreground(t.Secondary()).Render(word + " ")
206+
} else {
207+
words[i] = base.Render(word + " ")
208+
}
205209
}
210+
lines[i] = strings.Join(words, "")
206211
}
207-
text = strings.Join(words, "")
208-
text = ansi.WordwrapWc(text, width-6, " -")
212+
text = strings.Join(lines, "\n")
209213
content = base.Width(width - 6).Render(text)
210214
}
211215

0 commit comments

Comments
 (0)