| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- package chat
- import (
- "encoding/base64"
- "fmt"
- "log/slog"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "github.com/charmbracelet/bubbles/v2/spinner"
- tea "github.com/charmbracelet/bubbletea/v2"
- "github.com/charmbracelet/lipgloss/v2"
- "github.com/google/uuid"
- "github.com/sst/opencode-sdk-go"
- "github.com/sst/opencode/internal/app"
- "github.com/sst/opencode/internal/clipboard"
- "github.com/sst/opencode/internal/commands"
- "github.com/sst/opencode/internal/components/dialog"
- "github.com/sst/opencode/internal/components/textarea"
- "github.com/sst/opencode/internal/styles"
- "github.com/sst/opencode/internal/theme"
- "github.com/sst/opencode/internal/util"
- )
- type EditorComponent interface {
- tea.Model
- View(width int) string
- Content(width int) string
- Lines() int
- Value() string
- Length() int
- Focused() bool
- Focus() (tea.Model, tea.Cmd)
- Blur()
- Submit() (tea.Model, tea.Cmd)
- Clear() (tea.Model, tea.Cmd)
- Paste() (tea.Model, tea.Cmd)
- Newline() (tea.Model, tea.Cmd)
- SetValue(value string)
- SetInterruptKeyInDebounce(inDebounce bool)
- SetExitKeyInDebounce(inDebounce bool)
- }
- type editorComponent struct {
- app *app.App
- textarea textarea.Model
- spinner spinner.Model
- interruptKeyInDebounce bool
- exitKeyInDebounce bool
- }
- func (m *editorComponent) Init() tea.Cmd {
- return tea.Batch(m.textarea.Focus(), m.spinner.Tick, tea.EnableReportFocus)
- }
- func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
- var cmds []tea.Cmd
- var cmd tea.Cmd
- switch msg := msg.(type) {
- case spinner.TickMsg:
- m.spinner, cmd = m.spinner.Update(msg)
- return m, cmd
- case tea.KeyPressMsg:
- // Maximize editor responsiveness for printable characters
- if msg.Text != "" {
- m.textarea, cmd = m.textarea.Update(msg)
- cmds = append(cmds, cmd)
- return m, tea.Batch(cmds...)
- }
- case tea.PasteMsg:
- text := string(msg)
- text = strings.ReplaceAll(text, "\\", "")
- text, err := strconv.Unquote(`"` + text + `"`)
- if err != nil {
- slog.Error("Failed to unquote text", "error", err)
- m.textarea.InsertRunesFromUserInput([]rune(msg))
- return m, nil
- }
- if _, err := os.Stat(text); err != nil {
- slog.Error("Failed to paste file", "error", err)
- m.textarea.InsertRunesFromUserInput([]rune(msg))
- return m, nil
- }
- filePath := text
- ext := strings.ToLower(filepath.Ext(filePath))
- mediaType := ""
- switch ext {
- case ".jpg":
- mediaType = "image/jpeg"
- case ".png", ".jpeg", ".gif", ".webp":
- mediaType = "image/" + ext[1:]
- case ".pdf":
- mediaType = "application/pdf"
- default:
- attachment := &textarea.Attachment{
- ID: uuid.NewString(),
- Display: "@" + filePath,
- URL: fmt.Sprintf("file://./%s", filePath),
- Filename: filePath,
- MediaType: "text/plain",
- }
- m.textarea.InsertAttachment(attachment)
- m.textarea.InsertString(" ")
- return m, nil
- }
- fileBytes, err := os.ReadFile(filePath)
- if err != nil {
- slog.Error("Failed to read file", "error", err)
- m.textarea.InsertRunesFromUserInput([]rune(msg))
- return m, nil
- }
- base64EncodedFile := base64.StdEncoding.EncodeToString(fileBytes)
- url := fmt.Sprintf("data:%s;base64,%s", mediaType, base64EncodedFile)
- attachmentCount := len(m.textarea.GetAttachments())
- attachmentIndex := attachmentCount + 1
- label := "File"
- if strings.HasPrefix(mediaType, "image/") {
- label = "Image"
- }
- attachment := &textarea.Attachment{
- ID: uuid.NewString(),
- MediaType: mediaType,
- Display: fmt.Sprintf("[%s #%d]", label, attachmentIndex),
- URL: url,
- Filename: filePath,
- }
- m.textarea.InsertAttachment(attachment)
- m.textarea.InsertString(" ")
- case tea.ClipboardMsg:
- text := string(msg)
- m.textarea.InsertRunesFromUserInput([]rune(text))
- case dialog.ThemeSelectedMsg:
- m.textarea = m.resetTextareaStyles()
- m.spinner = createSpinner()
- return m, tea.Batch(m.spinner.Tick, m.textarea.Focus())
- case dialog.CompletionSelectedMsg:
- switch msg.Item.GetProviderID() {
- case "commands":
- commandName := strings.TrimPrefix(msg.Item.GetValue(), "/")
- updated, cmd := m.Clear()
- m = updated.(*editorComponent)
- cmds = append(cmds, cmd)
- cmds = append(cmds, util.CmdHandler(commands.ExecuteCommandMsg(m.app.Commands[commands.CommandName(commandName)])))
- return m, tea.Batch(cmds...)
- case "files":
- atIndex := m.textarea.LastRuneIndex('@')
- if atIndex == -1 {
- // Should not happen, but as a fallback, just insert.
- m.textarea.InsertString(msg.Item.GetValue() + " ")
- return m, nil
- }
- // The range to replace is from the '@' up to the current cursor position.
- // Replace the search term (e.g., "@search") with an empty string first.
- cursorCol := m.textarea.CursorColumn()
- m.textarea.ReplaceRange(atIndex, cursorCol, "")
- // Now, insert the attachment at the position where the '@' was.
- // The cursor is now at `atIndex` after the replacement.
- filePath := msg.Item.GetValue()
- extension := filepath.Ext(filePath)
- mediaType := ""
- switch extension {
- case ".jpg":
- mediaType = "image/jpeg"
- case ".png", ".jpeg", ".gif", ".webp":
- mediaType = "image/" + extension[1:]
- case ".pdf":
- mediaType = "application/pdf"
- default:
- mediaType = "text/plain"
- }
- attachment := &textarea.Attachment{
- ID: uuid.NewString(),
- Display: "@" + filePath,
- URL: fmt.Sprintf("file://./%s", filePath),
- Filename: filePath,
- MediaType: mediaType,
- }
- m.textarea.InsertAttachment(attachment)
- m.textarea.InsertString(" ")
- return m, nil
- case "symbols":
- atIndex := m.textarea.LastRuneIndex('@')
- if atIndex == -1 {
- // Should not happen, but as a fallback, just insert.
- m.textarea.InsertString(msg.Item.GetValue() + " ")
- return m, nil
- }
- cursorCol := m.textarea.CursorColumn()
- m.textarea.ReplaceRange(atIndex, cursorCol, "")
- symbol := msg.Item.GetRaw().(opencode.Symbol)
- parts := strings.Split(symbol.Name, ".")
- lastPart := parts[len(parts)-1]
- attachment := &textarea.Attachment{
- ID: uuid.NewString(),
- Display: "@" + lastPart,
- URL: msg.Item.GetValue(),
- Filename: lastPart,
- MediaType: "text/plain",
- }
- m.textarea.InsertAttachment(attachment)
- m.textarea.InsertString(" ")
- return m, nil
- default:
- slog.Debug("Unknown provider", "provider", msg.Item.GetProviderID())
- return m, nil
- }
- }
- m.spinner, cmd = m.spinner.Update(msg)
- cmds = append(cmds, cmd)
- m.textarea, cmd = m.textarea.Update(msg)
- cmds = append(cmds, cmd)
- return m, tea.Batch(cmds...)
- }
- func (m *editorComponent) Content(width int) string {
- t := theme.CurrentTheme()
- base := styles.NewStyle().Foreground(t.Text()).Background(t.Background()).Render
- muted := styles.NewStyle().Foreground(t.TextMuted()).Background(t.Background()).Render
- promptStyle := styles.NewStyle().Foreground(t.Primary()).
- Padding(0, 0, 0, 1).
- Bold(true)
- prompt := promptStyle.Render(">")
- m.textarea.SetWidth(width - 6)
- textarea := lipgloss.JoinHorizontal(
- lipgloss.Top,
- prompt,
- m.textarea.View(),
- )
- textarea = styles.NewStyle().
- Background(t.BackgroundElement()).
- Width(width).
- PaddingTop(1).
- PaddingBottom(1).
- BorderStyle(lipgloss.ThickBorder()).
- BorderForeground(t.Border()).
- BorderBackground(t.Background()).
- BorderLeft(true).
- BorderRight(true).
- Render(textarea)
- hint := base(m.getSubmitKeyText()) + muted(" send ")
- if m.exitKeyInDebounce {
- keyText := m.getExitKeyText()
- hint = base(keyText+" again") + muted(" to exit")
- } else if m.app.IsBusy() {
- keyText := m.getInterruptKeyText()
- if m.interruptKeyInDebounce {
- hint = muted(
- "working",
- ) + m.spinner.View() + muted(
- " ",
- ) + base(
- keyText+" again",
- ) + muted(
- " interrupt",
- )
- } else {
- hint = muted("working") + m.spinner.View() + muted(" ") + base(keyText) + muted(" interrupt")
- }
- }
- model := ""
- if m.app.Model != nil {
- model = muted(m.app.Provider.Name) + base(" "+m.app.Model.Name)
- }
- space := width - 2 - lipgloss.Width(model) - lipgloss.Width(hint)
- spacer := styles.NewStyle().Background(t.Background()).Width(space).Render("")
- info := hint + spacer + model
- info = styles.NewStyle().Background(t.Background()).Padding(0, 1).Render(info)
- content := strings.Join([]string{"", textarea, info}, "\n")
- return content
- }
- func (m *editorComponent) View(width int) string {
- if m.Lines() > 1 {
- return lipgloss.Place(
- width,
- 5,
- lipgloss.Center,
- lipgloss.Center,
- "",
- styles.WhitespaceStyle(theme.CurrentTheme().Background()),
- )
- }
- return m.Content(width)
- }
- func (m *editorComponent) Focused() bool {
- return m.textarea.Focused()
- }
- func (m *editorComponent) Focus() (tea.Model, tea.Cmd) {
- return m, m.textarea.Focus()
- }
- func (m *editorComponent) Blur() {
- m.textarea.Blur()
- }
- func (m *editorComponent) Lines() int {
- return m.textarea.LineCount()
- }
- func (m *editorComponent) Value() string {
- return m.textarea.Value()
- }
- func (m *editorComponent) Length() int {
- return m.textarea.Length()
- }
- func (m *editorComponent) Submit() (tea.Model, tea.Cmd) {
- value := strings.TrimSpace(m.Value())
- if value == "" {
- return m, nil
- }
- if len(value) > 0 && value[len(value)-1] == '\\' {
- // If the last character is a backslash, remove it and add a newline
- m.textarea.ReplaceRange(len(value)-1, len(value), "")
- m.textarea.InsertString("\n")
- return m, nil
- }
- var cmds []tea.Cmd
- attachments := m.textarea.GetAttachments()
- fileParts := make([]opencode.FilePartParam, 0)
- for _, attachment := range attachments {
- fileParts = append(fileParts, opencode.FilePartParam{
- Type: opencode.F(opencode.FilePartTypeFile),
- Mime: opencode.F(attachment.MediaType),
- URL: opencode.F(attachment.URL),
- Filename: opencode.F(attachment.Filename),
- })
- }
- updated, cmd := m.Clear()
- m = updated.(*editorComponent)
- cmds = append(cmds, cmd)
- cmds = append(cmds, util.CmdHandler(app.SendMsg{Text: value, Attachments: fileParts}))
- return m, tea.Batch(cmds...)
- }
- func (m *editorComponent) Clear() (tea.Model, tea.Cmd) {
- m.textarea.Reset()
- return m, nil
- }
- func (m *editorComponent) Paste() (tea.Model, tea.Cmd) {
- imageBytes := clipboard.Read(clipboard.FmtImage)
- if imageBytes != nil {
- attachmentCount := len(m.textarea.GetAttachments())
- attachmentIndex := attachmentCount + 1
- base64EncodedFile := base64.StdEncoding.EncodeToString(imageBytes)
- attachment := &textarea.Attachment{
- ID: uuid.NewString(),
- MediaType: "image/png",
- Display: fmt.Sprintf("[Image #%d]", attachmentIndex),
- Filename: fmt.Sprintf("image-%d.png", attachmentIndex),
- URL: fmt.Sprintf("data:image/png;base64,%s", base64EncodedFile),
- }
- m.textarea.InsertAttachment(attachment)
- m.textarea.InsertString(" ")
- return m, nil
- }
- textBytes := clipboard.Read(clipboard.FmtText)
- if textBytes != nil {
- m.textarea.InsertRunesFromUserInput([]rune(string(textBytes)))
- return m, nil
- }
- // fallback to reading the clipboard using OSC52
- return m, tea.ReadClipboard
- }
- func (m *editorComponent) Newline() (tea.Model, tea.Cmd) {
- m.textarea.Newline()
- return m, nil
- }
- func (m *editorComponent) SetInterruptKeyInDebounce(inDebounce bool) {
- m.interruptKeyInDebounce = inDebounce
- }
- func (m *editorComponent) SetValue(value string) {
- m.textarea.SetValue(value)
- }
- func (m *editorComponent) SetExitKeyInDebounce(inDebounce bool) {
- m.exitKeyInDebounce = inDebounce
- }
- func (m *editorComponent) getInterruptKeyText() string {
- return m.app.Commands[commands.SessionInterruptCommand].Keys()[0]
- }
- func (m *editorComponent) getSubmitKeyText() string {
- return m.app.Commands[commands.InputSubmitCommand].Keys()[0]
- }
- func (m *editorComponent) getExitKeyText() string {
- return m.app.Commands[commands.AppExitCommand].Keys()[0]
- }
- func (m *editorComponent) resetTextareaStyles() textarea.Model {
- t := theme.CurrentTheme()
- bgColor := t.BackgroundElement()
- textColor := t.Text()
- textMutedColor := t.TextMuted()
- ta := m.textarea
- ta.Styles.Blurred.Base = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss()
- ta.Styles.Blurred.CursorLine = styles.NewStyle().Background(bgColor).Lipgloss()
- ta.Styles.Blurred.Placeholder = styles.NewStyle().
- Foreground(textMutedColor).
- Background(bgColor).
- Lipgloss()
- ta.Styles.Blurred.Text = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss()
- ta.Styles.Focused.Base = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss()
- ta.Styles.Focused.CursorLine = styles.NewStyle().Background(bgColor).Lipgloss()
- ta.Styles.Focused.Placeholder = styles.NewStyle().
- Foreground(textMutedColor).
- Background(bgColor).
- Lipgloss()
- ta.Styles.Focused.Text = styles.NewStyle().Foreground(textColor).Background(bgColor).Lipgloss()
- ta.Styles.Attachment = styles.NewStyle().
- Foreground(t.Secondary()).
- Background(bgColor).
- Lipgloss()
- ta.Styles.SelectedAttachment = styles.NewStyle().
- Foreground(t.Text()).
- Background(t.Secondary()).
- Lipgloss()
- ta.Styles.Cursor.Color = t.Primary()
- return ta
- }
- func createSpinner() spinner.Model {
- t := theme.CurrentTheme()
- return spinner.New(
- spinner.WithSpinner(spinner.Ellipsis),
- spinner.WithStyle(
- styles.NewStyle().
- Background(t.Background()).
- Foreground(t.TextMuted()).
- Width(3).
- Lipgloss(),
- ),
- )
- }
- func NewEditorComponent(app *app.App) EditorComponent {
- s := createSpinner()
- ta := textarea.New()
- ta.Prompt = " "
- ta.ShowLineNumbers = false
- ta.CharLimit = -1
- m := &editorComponent{
- app: app,
- textarea: ta,
- spinner: s,
- interruptKeyInDebounce: false,
- }
- m.resetTextareaStyles()
- return m
- }
|