|
| 1 | +package center |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + "github.com/andyrewlee/amux/internal/data" |
| 11 | + appPty "github.com/andyrewlee/amux/internal/pty" |
| 12 | + "github.com/andyrewlee/amux/internal/tmux" |
| 13 | + "github.com/andyrewlee/amux/internal/vterm" |
| 14 | +) |
| 15 | + |
| 16 | +func (m *Model) addDetachedTab(ws *data.Workspace, info data.TabInfo) { |
| 17 | + tm := m.terminalMetrics() |
| 18 | + termWidth := tm.Width |
| 19 | + termHeight := tm.Height |
| 20 | + if termWidth < 1 { |
| 21 | + termWidth = 80 |
| 22 | + } |
| 23 | + if termHeight < 1 { |
| 24 | + termHeight = 24 |
| 25 | + } |
| 26 | + displayName := strings.TrimSpace(info.Name) |
| 27 | + if displayName == "" { |
| 28 | + displayName = strings.TrimSpace(info.Assistant) |
| 29 | + } |
| 30 | + if displayName == "" { |
| 31 | + displayName = "Terminal" |
| 32 | + } |
| 33 | + term := vterm.New(termWidth, termHeight) |
| 34 | + term.AllowAltScreenScrollback = true |
| 35 | + ca := info.CreatedAt |
| 36 | + if ca == 0 { |
| 37 | + ca = time.Now().Unix() |
| 38 | + } |
| 39 | + tab := &Tab{ |
| 40 | + ID: generateTabID(), |
| 41 | + Name: displayName, |
| 42 | + Assistant: info.Assistant, |
| 43 | + Workspace: ws, |
| 44 | + SessionName: info.SessionName, |
| 45 | + Detached: true, |
| 46 | + Running: false, |
| 47 | + Terminal: term, |
| 48 | + createdAt: ca, |
| 49 | + } |
| 50 | + wsID := string(ws.ID()) |
| 51 | + m.tabsByWorkspace[wsID] = append(m.tabsByWorkspace[wsID], tab) |
| 52 | +} |
| 53 | + |
| 54 | +// addPlaceholderTab synchronously creates a placeholder tab in the correct slice |
| 55 | +// position. The tab starts detached and non-running; an async reattach upgrades |
| 56 | +// it in-place (by TabID) without changing slice order. |
| 57 | +func (m *Model) addPlaceholderTab(ws *data.Workspace, info data.TabInfo) (TabID, string) { |
| 58 | + tm := m.terminalMetrics() |
| 59 | + termWidth := tm.Width |
| 60 | + termHeight := tm.Height |
| 61 | + if termWidth < 1 { |
| 62 | + termWidth = 80 |
| 63 | + } |
| 64 | + if termHeight < 1 { |
| 65 | + termHeight = 24 |
| 66 | + } |
| 67 | + displayName := strings.TrimSpace(info.Name) |
| 68 | + if displayName == "" { |
| 69 | + displayName = strings.TrimSpace(info.Assistant) |
| 70 | + } |
| 71 | + if displayName == "" { |
| 72 | + displayName = "Terminal" |
| 73 | + } |
| 74 | + term := vterm.New(termWidth, termHeight) |
| 75 | + term.AllowAltScreenScrollback = true |
| 76 | + tabID := generateTabID() |
| 77 | + sessionName := strings.TrimSpace(info.SessionName) |
| 78 | + if sessionName == "" { |
| 79 | + sessionName = tmux.SessionName("amux", string(ws.ID()), string(tabID)) |
| 80 | + } |
| 81 | + ca := info.CreatedAt |
| 82 | + if ca == 0 { |
| 83 | + ca = time.Now().Unix() |
| 84 | + } |
| 85 | + tab := &Tab{ |
| 86 | + ID: tabID, |
| 87 | + Name: displayName, |
| 88 | + Assistant: info.Assistant, |
| 89 | + Workspace: ws, |
| 90 | + SessionName: sessionName, |
| 91 | + Detached: true, |
| 92 | + Running: false, |
| 93 | + Terminal: term, |
| 94 | + createdAt: ca, |
| 95 | + } |
| 96 | + wsID := string(ws.ID()) |
| 97 | + m.tabsByWorkspace[wsID] = append(m.tabsByWorkspace[wsID], tab) |
| 98 | + return tabID, sessionName |
| 99 | +} |
| 100 | + |
| 101 | +// reattachToSession returns a tea.Cmd that asynchronously connects a placeholder |
| 102 | +// tab to its tmux session. On success it produces ptyTabReattachResult which |
| 103 | +// updates the tab in-place (by TabID). On failure it produces ptyTabReattachFailed. |
| 104 | +func (m *Model) reattachToSession(ws *data.Workspace, tabID TabID, assistant string, sessionName string) tea.Cmd { |
| 105 | + tm := m.terminalMetrics() |
| 106 | + termWidth := tm.Width |
| 107 | + termHeight := tm.Height |
| 108 | + opts := m.getTmuxOptions() |
| 109 | + return func() tea.Msg { |
| 110 | + state, err := tmux.SessionStateFor(sessionName, opts) |
| 111 | + if err != nil { |
| 112 | + return ptyTabReattachFailed{ |
| 113 | + WorkspaceID: string(ws.ID()), |
| 114 | + TabID: tabID, |
| 115 | + Err: err, |
| 116 | + Action: "reattach", |
| 117 | + } |
| 118 | + } |
| 119 | + if !state.Exists || !state.HasLivePane { |
| 120 | + return ptyTabReattachFailed{ |
| 121 | + WorkspaceID: string(ws.ID()), |
| 122 | + TabID: tabID, |
| 123 | + Err: fmt.Errorf("tmux session ended"), |
| 124 | + Stopped: true, |
| 125 | + Action: "reattach", |
| 126 | + } |
| 127 | + } |
| 128 | + tags := tmux.SessionTags{ |
| 129 | + WorkspaceID: string(ws.ID()), |
| 130 | + TabID: string(tabID), |
| 131 | + Type: "agent", |
| 132 | + Assistant: assistant, |
| 133 | + } |
| 134 | + agent, err := m.agentManager.CreateAgentWithTags(ws, appPty.AgentType(assistant), sessionName, uint16(termHeight), uint16(termWidth), tags) |
| 135 | + if err != nil { |
| 136 | + return ptyTabReattachFailed{ |
| 137 | + WorkspaceID: string(ws.ID()), |
| 138 | + TabID: tabID, |
| 139 | + Err: err, |
| 140 | + Action: "reattach", |
| 141 | + } |
| 142 | + } |
| 143 | + scrollback, _ := tmux.CapturePane(sessionName, opts) |
| 144 | + return ptyTabReattachResult{ |
| 145 | + WorkspaceID: string(ws.ID()), |
| 146 | + TabID: tabID, |
| 147 | + Agent: agent, |
| 148 | + Rows: termHeight, |
| 149 | + Cols: termWidth, |
| 150 | + ScrollbackCapture: scrollback, |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// safeBatch wraps commands in a batch, handling nil commands gracefully. |
| 156 | +func safeBatch(cmds ...tea.Cmd) tea.Cmd { |
| 157 | + var valid []tea.Cmd |
| 158 | + for _, cmd := range cmds { |
| 159 | + if cmd != nil { |
| 160 | + valid = append(valid, cmd) |
| 161 | + } |
| 162 | + } |
| 163 | + if len(valid) == 0 { |
| 164 | + return nil |
| 165 | + } |
| 166 | + if len(valid) == 1 { |
| 167 | + return valid[0] |
| 168 | + } |
| 169 | + return tea.Batch(valid...) |
| 170 | +} |
0 commit comments