mux-sesh 1.9.3 → 1.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +27 -9
  2. package/package.json +4 -2
  3. package/src/agents/index.ts +1 -0
  4. package/src/agents/session-name.ts +24 -0
  5. package/src/app/commands.ts +20 -16
  6. package/src/app/controller.ts +30 -18
  7. package/src/app/controls.ts +11 -9
  8. package/src/app/core-state.ts +8 -4
  9. package/src/app/data.ts +42 -7
  10. package/src/app/derived.ts +11 -11
  11. package/src/app/effects.ts +141 -23
  12. package/src/app/handlers.ts +48 -24
  13. package/src/app/keyboard.ts +12 -12
  14. package/src/app/modals.ts +8 -8
  15. package/src/app/runtime.ts +4 -1
  16. package/src/app/screen.tsx +30 -15
  17. package/src/app/state.ts +24 -4
  18. package/src/app/view.ts +24 -13
  19. package/src/config/index.ts +10 -0
  20. package/src/handlers/action-helpers.ts +22 -7
  21. package/src/handlers/actions.ts +51 -42
  22. package/src/handlers/keyboard/agents.ts +134 -0
  23. package/src/handlers/keyboard/index.ts +1 -1
  24. package/src/handlers/keyboard/new-session.ts +1 -1
  25. package/src/handlers/keyboard/normal.ts +12 -8
  26. package/src/handlers/keyboard/search.ts +10 -6
  27. package/src/handlers/keyboard/shared.ts +6 -6
  28. package/src/herdr/backend.ts +357 -0
  29. package/src/herdr/protocol.ts +268 -0
  30. package/src/items/order.ts +7 -11
  31. package/src/multiplexer/backend.ts +43 -0
  32. package/src/multiplexer/command.ts +48 -0
  33. package/src/multiplexer/index.ts +21 -0
  34. package/src/multiplexer/items.ts +66 -0
  35. package/src/multiplexer/resolve.ts +106 -0
  36. package/src/multiplexer/tmux-backend.ts +87 -0
  37. package/src/multiplexer/types.ts +93 -0
  38. package/src/opencode/session-name.ts +5 -4
  39. package/src/preview/project.ts +13 -11
  40. package/src/projects/session-links.ts +22 -4
  41. package/src/settings.ts +19 -0
  42. package/src/tmux/workflows.ts +5 -1
  43. package/src/types/index.ts +14 -2
  44. package/src/ui/{OpencodeSessionGroup.tsx → AgentSessionGroup.tsx} +27 -12
  45. package/src/ui/CommandsModal.tsx +18 -10
  46. package/src/ui/ItemList.tsx +3 -2
  47. package/src/ui/SessionDetailsPanel.tsx +68 -34
  48. package/src/ui/SessionList.tsx +23 -7
  49. package/src/ui/item-icon.ts +24 -12
  50. package/src/ui/session-meta.ts +10 -0
  51. package/src/handlers/keyboard/opencode.ts +0 -105
@@ -0,0 +1,134 @@
1
+ import { isOpencodeSessionItem } from '../../opencode/session-name'
2
+ import { isHerdrAgentItem } from '../../multiplexer/items'
3
+ import type { Item } from '../../types'
4
+ import { AppMode, type KeybindMode } from '../../types'
5
+ import { clampCursorIndex } from '../../ui/list-window'
6
+ import {
7
+ activatePrefix,
8
+ clearPrefix,
9
+ getKeyName,
10
+ matchesPrefixKey,
11
+ type KeyboardHandlerContext,
12
+ type KeyboardInput,
13
+ } from './shared'
14
+
15
+ function loadStatsIfOpencode(ctx: KeyboardHandlerContext, item: Item) {
16
+ if (isOpencodeSessionItem(item)) {
17
+ void ctx.loadOpencodeStatsForSession(item.title)
18
+ }
19
+ }
20
+
21
+ export function handleAgentsManageMode(
22
+ key: KeyboardInput,
23
+ ctx: KeyboardHandlerContext,
24
+ keybindMode: KeybindMode = 'vim'
25
+ ) {
26
+ const keyName = getKeyName(key)
27
+ const isStandard = keybindMode === 'standard'
28
+
29
+ if (key.ctrl && keyName === 'p') {
30
+ ctx.clearPendingKill()
31
+ ctx.openCommandsModal()
32
+ return
33
+ }
34
+
35
+ if (ctx.prefixActive) {
36
+ clearPrefix(ctx)
37
+
38
+ switch (keyName) {
39
+ case 'o':
40
+ ctx.clearPendingKill()
41
+ ctx.setAppMode(AppMode.Normal)
42
+ return
43
+ case 'd':
44
+ if (
45
+ ctx.agentSessions[ctx.agentCursor] &&
46
+ !isHerdrAgentItem(ctx.agentSessions[ctx.agentCursor])
47
+ ) {
48
+ ctx.requestKillSession(ctx.agentSessions[ctx.agentCursor])
49
+ }
50
+ return
51
+ case 'r':
52
+ if (
53
+ ctx.agentSessions[ctx.agentCursor] &&
54
+ !isHerdrAgentItem(ctx.agentSessions[ctx.agentCursor])
55
+ ) {
56
+ ctx.clearPendingKill()
57
+ ctx.openRenameModal(ctx.agentSessions[ctx.agentCursor])
58
+ }
59
+ return
60
+ }
61
+
62
+ return
63
+ }
64
+
65
+ if (ctx.prefixKey && matchesPrefixKey(key, ctx.prefixKey)) {
66
+ activatePrefix(ctx)
67
+ return
68
+ }
69
+
70
+ if (keyName === 'escape' || (!ctx.prefixKey && !isStandard && keyName === 'o')) {
71
+ ctx.clearPendingKill()
72
+ ctx.setAppMode(AppMode.Normal)
73
+ return
74
+ }
75
+
76
+ if (keyName === 'return' || keyName === 'enter') {
77
+ const selectedAgent = ctx.agentSessions[ctx.agentCursor]
78
+ if (selectedAgent) {
79
+ void ctx.handleSelect(selectedAgent)
80
+ }
81
+ return
82
+ }
83
+
84
+ if (keyName === 'down' || (!isStandard && keyName === 'j')) {
85
+ ctx.clearPendingKill()
86
+ ctx.setAgentCursor(cursor => {
87
+ const next = clampCursorIndex(ctx.agentSessions.length, cursor + 1)
88
+ if (next !== cursor && ctx.agentSessions[next]) {
89
+ loadStatsIfOpencode(ctx, ctx.agentSessions[next])
90
+ }
91
+ return next
92
+ })
93
+ return
94
+ }
95
+
96
+ if (keyName === 'up' || (!isStandard && keyName === 'k')) {
97
+ ctx.clearPendingKill()
98
+ ctx.setAgentCursor(cursor => {
99
+ const next = clampCursorIndex(ctx.agentSessions.length, cursor - 1)
100
+ if (next !== cursor && ctx.agentSessions[next]) {
101
+ loadStatsIfOpencode(ctx, ctx.agentSessions[next])
102
+ }
103
+ return next
104
+ })
105
+ return
106
+ }
107
+
108
+ if (!isStandard && keyName === 'd') {
109
+ if (
110
+ ctx.agentSessions[ctx.agentCursor] &&
111
+ !isHerdrAgentItem(ctx.agentSessions[ctx.agentCursor])
112
+ ) {
113
+ ctx.requestKillSession(ctx.agentSessions[ctx.agentCursor])
114
+ }
115
+ return
116
+ }
117
+
118
+ if (!ctx.prefixKey && !isStandard && keyName === 'r') {
119
+ if (
120
+ ctx.agentSessions[ctx.agentCursor] &&
121
+ !isHerdrAgentItem(ctx.agentSessions[ctx.agentCursor])
122
+ ) {
123
+ ctx.clearPendingKill()
124
+ ctx.openRenameModal(ctx.agentSessions[ctx.agentCursor])
125
+ }
126
+ return
127
+ }
128
+
129
+ if (!ctx.prefixKey && !isStandard && keyName === ',') {
130
+ ctx.clearPendingKill()
131
+ ctx.openSettingsModal()
132
+ return
133
+ }
134
+ }
@@ -1,5 +1,5 @@
1
1
  export type { KeyboardHandlerContext, KeyboardInput } from './shared'
2
2
  export { handleNormalMode } from './normal'
3
- export { handleOpencodeManageMode } from './opencode'
3
+ export { handleAgentsManageMode } from './agents'
4
4
  export { handleSearchMode } from './search'
5
5
  export { handleNewSessionMode } from './new-session'
@@ -22,7 +22,7 @@ export function handleNewSessionMode(
22
22
  }
23
23
 
24
24
  if (keyName === 'd' && ctx.items[ctx.cursor]?.isSession) {
25
- ctx.requestKillSession(ctx.items[ctx.cursor].title)
25
+ ctx.requestKillSession(ctx.items[ctx.cursor])
26
26
  return
27
27
  }
28
28
  }
@@ -1,3 +1,4 @@
1
+ import { isOpencodeSessionItem } from '../../opencode/session-name'
1
2
  import { AppMode, ViewMode, type KeybindMode } from '../../types'
2
3
  import { clampCursorIndex } from '../../ui/list-window'
3
4
  import { requestShutdown } from '../../util/shutdown'
@@ -47,13 +48,13 @@ export function handleNormalMode(
47
48
  return
48
49
  case 'd':
49
50
  if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
50
- ctx.requestKillSession(ctx.regularSessions[ctx.cursor].title)
51
+ ctx.requestKillSession(ctx.regularSessions[ctx.cursor])
51
52
  }
52
53
  return
53
54
  case 'r':
54
55
  if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
55
56
  ctx.clearPendingKill()
56
- ctx.openRenameModal(ctx.regularSessions[ctx.cursor].title)
57
+ ctx.openRenameModal(ctx.regularSessions[ctx.cursor])
57
58
  }
58
59
  return
59
60
  case 's':
@@ -98,10 +99,13 @@ export function handleNormalMode(
98
99
 
99
100
  if (!isStandard && keyName === 'o') {
100
101
  ctx.clearPendingKill()
101
- if (ctx.viewMode === ViewMode.Sessions && ctx.opencodeSessions.length > 0) {
102
- ctx.setAppMode(AppMode.OpencodeManage)
103
- ctx.setOpencodeCursor(0)
104
- void ctx.loadOpencodeStatsForSession(ctx.opencodeSessions[0].title)
102
+ if (ctx.viewMode === ViewMode.Sessions && ctx.agentSessions.length > 0) {
103
+ ctx.setAppMode(AppMode.AgentsManage)
104
+ ctx.setAgentCursor(0)
105
+ const firstAgent = ctx.agentSessions[0]
106
+ if (isOpencodeSessionItem(firstAgent)) {
107
+ void ctx.loadOpencodeStatsForSession(firstAgent.title)
108
+ }
105
109
  }
106
110
  return
107
111
  }
@@ -139,7 +143,7 @@ export function handleNormalMode(
139
143
 
140
144
  if (!isStandard && keyName === 'd') {
141
145
  if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
142
- ctx.requestKillSession(ctx.regularSessions[ctx.cursor].title)
146
+ ctx.requestKillSession(ctx.regularSessions[ctx.cursor])
143
147
  }
144
148
  return
145
149
  }
@@ -147,7 +151,7 @@ export function handleNormalMode(
147
151
  if (!ctx.prefixKey && !isStandard && keyName === 'r') {
148
152
  if (ctx.viewMode === ViewMode.Sessions && ctx.regularSessions[ctx.cursor]?.isSession) {
149
153
  ctx.clearPendingKill()
150
- ctx.openRenameModal(ctx.regularSessions[ctx.cursor].title)
154
+ ctx.openRenameModal(ctx.regularSessions[ctx.cursor])
151
155
  }
152
156
  return
153
157
  }
@@ -1,3 +1,4 @@
1
+ import { isOpencodeSessionItem } from '../../opencode/session-name'
1
2
  import { AppMode, ViewMode, type KeybindMode } from '../../types'
2
3
  import { clampCursorIndex } from '../../ui/list-window'
3
4
  import { requestShutdown } from '../../util/shutdown'
@@ -27,7 +28,7 @@ export function handleSearchMode(
27
28
  switch (keyName) {
28
29
  case 'd':
29
30
  if (ctx.viewMode === ViewMode.Sessions && ctx.items[ctx.cursor]?.isSession) {
30
- ctx.requestKillSession(ctx.items[ctx.cursor].title)
31
+ ctx.requestKillSession(ctx.items[ctx.cursor])
31
32
  }
32
33
  return
33
34
  case 'n':
@@ -35,10 +36,13 @@ export function handleSearchMode(
35
36
  return
36
37
  case 'o':
37
38
  ctx.clearPendingKill()
38
- if (ctx.viewMode === ViewMode.Sessions && ctx.opencodeSessions.length > 0) {
39
- ctx.setAppMode(AppMode.OpencodeManage)
40
- ctx.setOpencodeCursor(0)
41
- void ctx.loadOpencodeStatsForSession(ctx.opencodeSessions[0].title)
39
+ if (ctx.viewMode === ViewMode.Sessions && ctx.agentSessions.length > 0) {
40
+ ctx.setAppMode(AppMode.AgentsManage)
41
+ ctx.setAgentCursor(0)
42
+ const firstAgent = ctx.agentSessions[0]
43
+ if (isOpencodeSessionItem(firstAgent)) {
44
+ void ctx.loadOpencodeStatsForSession(firstAgent.title)
45
+ }
42
46
  }
43
47
  return
44
48
  case 's':
@@ -62,7 +66,7 @@ export function handleSearchMode(
62
66
 
63
67
  if (ctx.viewMode === ViewMode.Sessions && ctx.items[ctx.cursor]?.isSession) {
64
68
  ctx.clearPendingKill()
65
- ctx.openRenameModal(ctx.items[ctx.cursor].title)
69
+ ctx.openRenameModal(ctx.items[ctx.cursor])
66
70
  }
67
71
  return
68
72
  default:
@@ -12,9 +12,9 @@ export interface KeyboardHandlerContext {
12
12
  viewMode: ViewMode
13
13
  items: Item[]
14
14
  regularSessions: Item[]
15
- opencodeSessions: Item[]
15
+ agentSessions: Item[]
16
16
  cursor: number
17
- opencodeCursor: number
17
+ agentCursor: number
18
18
  searchQuery: string
19
19
  prefixKey?: string
20
20
  projectItems: Item[]
@@ -24,21 +24,21 @@ export interface KeyboardHandlerContext {
24
24
  setAppMode: (mode: AppMode) => void
25
25
  setViewMode: (mode: ViewMode) => void
26
26
  setCursor: Dispatch<SetStateAction<number>>
27
- setOpencodeCursor: Dispatch<SetStateAction<number>>
27
+ setAgentCursor: Dispatch<SetStateAction<number>>
28
28
  setSearchQuery: (query: string) => void
29
29
  setAllItems: Dispatch<SetStateAction<Item[]>>
30
30
  setItems: Dispatch<SetStateAction<Item[]>>
31
31
  setPrefixActive: (active: boolean) => void
32
32
  refreshItems: (forceViewMode?: ViewMode, nextConfig?: Config) => Promise<void>
33
- requestKillSession: (sessionName: string) => void
33
+ requestKillSession: (item: Item) => void
34
34
  togglePinnedSession: (sessionName: string) => Promise<void>
35
35
  clearPendingKill: () => void
36
36
  handleSelect: (item: Item) => Promise<void>
37
- handleKillSession: (sessionName: string) => Promise<void>
37
+ handleKillSession: (item: Item) => Promise<void>
38
38
  handleLastSession: () => Promise<void>
39
39
  handleRootSession: (item?: Item) => Promise<void>
40
40
  handleEditTarget: (item?: Item) => Promise<void>
41
- openRenameModal: (sessionName: string) => void
41
+ openRenameModal: (item: Item) => void
42
42
  openCommandsModal: () => void
43
43
  openSettingsModal: () => void
44
44
  loadOpencodeStatsForSession: (sessionName: string) => Promise<OpencodeSessionStats | null>
@@ -0,0 +1,357 @@
1
+ import { resolve } from 'path'
2
+ import {
3
+ MultiplexerError,
4
+ type LiveWorkspace,
5
+ type MultiplexerBackend,
6
+ type OpenWorkspaceInput,
7
+ type WorkspaceDetails,
8
+ type WorkspaceRef,
9
+ } from '../multiplexer'
10
+ import type { CommandOptions, CommandRunner } from '../multiplexer/command'
11
+ import {
12
+ parseHerdrCreatedWorkspace,
13
+ parseHerdrCreatedTab,
14
+ parseHerdrResult,
15
+ parseHerdrSnapshot,
16
+ type HerdrSnapshot,
17
+ type HerdrWorkspace,
18
+ } from './protocol'
19
+
20
+ export type { CommandOptions, CommandRunner } from '../multiplexer/command'
21
+
22
+ export interface HerdrBackendOptions {
23
+ runner: CommandRunner
24
+ insideHerdr: boolean
25
+ }
26
+
27
+ const AGENTS_WORKSPACE_LABEL = 'Agents'
28
+
29
+ function isAgentTarget(workspace: WorkspaceRef): workspace is Extract<
30
+ WorkspaceRef,
31
+ { backend: 'herdr' }
32
+ > & {
33
+ target: { kind: 'agent'; tabId: string; paneId: string }
34
+ } {
35
+ return workspace.backend === 'herdr' && workspace.target?.kind === 'agent'
36
+ }
37
+
38
+ function workspacePaths(snapshot: HerdrSnapshot, workspace: HerdrWorkspace): string[] {
39
+ const panes = snapshot.panes.filter(pane => pane.workspaceId === workspace.id)
40
+ const activePane =
41
+ panes.find(
42
+ pane =>
43
+ pane.id ===
44
+ snapshot.layouts.find(layout => layout.tabId === workspace.activeTabId)?.focusedPaneId
45
+ ) ??
46
+ panes.find(pane => pane.tabId === workspace.activeTabId) ??
47
+ panes[0]
48
+ return [activePane?.cwd, workspace.worktreePath].filter(
49
+ (path): path is string => typeof path === 'string' && path.length > 0
50
+ )
51
+ }
52
+
53
+ function workspacePath(snapshot: HerdrSnapshot, workspace: HerdrWorkspace): string {
54
+ return workspacePaths(snapshot, workspace)[0] ?? ''
55
+ }
56
+
57
+ function focusedPaneForTab(snapshot: HerdrSnapshot, tabId: string) {
58
+ const focusedPaneId = snapshot.layouts.find(layout => layout.tabId === tabId)?.focusedPaneId
59
+ return (
60
+ snapshot.panes.find(pane => pane.id === focusedPaneId) ??
61
+ snapshot.panes.find(pane => pane.tabId === tabId)
62
+ )
63
+ }
64
+
65
+ export function createHerdrBackend(options: HerdrBackendOptions): MultiplexerBackend {
66
+ let latestSnapshot: HerdrSnapshot | undefined
67
+
68
+ async function snapshot(): Promise<HerdrSnapshot> {
69
+ const result = await options.runner.run(['herdr', 'api', 'snapshot'])
70
+ if (result.exitCode !== 0) {
71
+ const reason = result.stderr.trim()
72
+ throw new MultiplexerError(
73
+ 'command-failed',
74
+ reason
75
+ ? `Failed to load Herdr workspaces: ${reason}`
76
+ : 'Failed to load Herdr workspaces. Run herdr status --json to verify the server.'
77
+ )
78
+ }
79
+ latestSnapshot = parseHerdrSnapshot(result.stdout)
80
+ return latestSnapshot
81
+ }
82
+
83
+ async function command(argv: readonly string[], expectedType: string): Promise<string> {
84
+ const result = await options.runner.run(argv)
85
+ if (result.exitCode !== 0) {
86
+ const reason = result.stderr.trim()
87
+ throw new MultiplexerError(
88
+ 'command-failed',
89
+ reason
90
+ ? `Herdr command failed: ${reason}`
91
+ : `Herdr command failed: ${argv.slice(1).join(' ')}`
92
+ )
93
+ }
94
+ parseHerdrResult(result.stdout, expectedType)
95
+ return result.stdout
96
+ }
97
+
98
+ async function commandWithoutOutput(argv: readonly string[]): Promise<void> {
99
+ const result = await options.runner.run(argv)
100
+ if (result.exitCode !== 0) {
101
+ const reason = result.stderr.trim()
102
+ throw new MultiplexerError(
103
+ 'command-failed',
104
+ reason
105
+ ? `Herdr command failed: ${reason}`
106
+ : `Herdr command failed: ${argv.slice(1).join(' ')}`
107
+ )
108
+ }
109
+ }
110
+
111
+ async function focus(workspaceId: string): Promise<void> {
112
+ await command(['herdr', 'workspace', 'focus', workspaceId], 'workspace_info')
113
+ latestSnapshot = undefined
114
+ }
115
+
116
+ async function attachIfNeeded(): Promise<void> {
117
+ if (options.insideHerdr) {
118
+ return
119
+ }
120
+ const result = await options.runner.run(['herdr'], { inheritStdio: true })
121
+ if (result.exitCode !== 0) {
122
+ throw new MultiplexerError(
123
+ 'command-failed',
124
+ 'Failed to attach the Herdr client. The selected workspace remains focused.'
125
+ )
126
+ }
127
+ }
128
+
129
+ return {
130
+ kind: 'herdr',
131
+ capabilities: { previousWorkspace: false },
132
+ async list(): Promise<LiveWorkspace[]> {
133
+ const state = await snapshot()
134
+ return state.workspaces.flatMap(workspace => {
135
+ if (workspace.label !== AGENTS_WORKSPACE_LABEL) {
136
+ return [
137
+ {
138
+ backend: 'herdr' as const,
139
+ id: workspace.id,
140
+ title: workspace.label,
141
+ path: workspacePath(state, workspace),
142
+ isActive: workspace.focused,
143
+ unitCount: workspace.tabCount,
144
+ agentStatus: workspace.agentStatus,
145
+ },
146
+ ]
147
+ }
148
+
149
+ return state.agents
150
+ .filter(agent => agent.workspaceId === workspace.id)
151
+ .map(agent => {
152
+ const pane = state.panes.find(entry => entry.id === agent.paneId)
153
+ const tab = state.tabs.find(entry => entry.id === agent.tabId)
154
+ return {
155
+ backend: 'herdr' as const,
156
+ id: workspace.id,
157
+ title: agent.name ?? agent.displayAgent ?? agent.agent ?? tab?.label ?? 'agent',
158
+ workspaceTitle: workspace.label,
159
+ path:
160
+ agent.foregroundCwd ??
161
+ agent.cwd ??
162
+ pane?.foregroundCwd ??
163
+ pane?.cwd ??
164
+ workspace.worktreePath ??
165
+ '',
166
+ isActive:
167
+ workspace.focused &&
168
+ (state.focusedPaneId === agent.paneId || state.focusedTabId === agent.tabId),
169
+ unitCount: 1,
170
+ agentStatus: agent.status,
171
+ target: { kind: 'agent' as const, tabId: agent.tabId, paneId: agent.paneId },
172
+ }
173
+ })
174
+ })
175
+ },
176
+ async open(workspace: WorkspaceRef): Promise<void> {
177
+ if (isAgentTarget(workspace)) {
178
+ await command(['herdr', 'tab', 'focus', workspace.target.tabId], 'tab_info')
179
+ latestSnapshot = undefined
180
+ } else {
181
+ await focus(workspace.id)
182
+ }
183
+ await attachIfNeeded()
184
+ },
185
+ async openOrCreate(input: OpenWorkspaceInput): Promise<void> {
186
+ const state = await snapshot()
187
+ const normalizedPath = resolve(input.path)
188
+ const existing = state.workspaces.find(
189
+ workspace =>
190
+ workspace.label === input.title &&
191
+ workspacePaths(state, workspace).some(path => resolve(path) === normalizedPath)
192
+ )
193
+ if (existing) {
194
+ await focus(existing.id)
195
+ await attachIfNeeded()
196
+ return
197
+ }
198
+
199
+ const output = await command(
200
+ ['herdr', 'workspace', 'create', '--cwd', input.path, '--label', input.title, '--focus'],
201
+ 'workspace_created'
202
+ )
203
+ const created = parseHerdrCreatedWorkspace(output)
204
+ if (input.startupCommand) {
205
+ await commandWithoutOutput([
206
+ 'herdr',
207
+ 'pane',
208
+ 'run',
209
+ created.rootPaneId,
210
+ input.startupCommand,
211
+ ])
212
+ }
213
+ latestSnapshot = undefined
214
+ await attachIfNeeded()
215
+ },
216
+ async rename(workspace: WorkspaceRef, title: string): Promise<void> {
217
+ if (isAgentTarget(workspace)) {
218
+ throw new MultiplexerError(
219
+ 'unsupported',
220
+ 'Cannot rename an agent tab from mux-sesh. Rename it in Herdr instead.'
221
+ )
222
+ }
223
+ await command(['herdr', 'workspace', 'rename', workspace.id, title], 'workspace_info')
224
+ latestSnapshot = undefined
225
+ },
226
+ async close(workspace: WorkspaceRef): Promise<void> {
227
+ if (isAgentTarget(workspace)) {
228
+ throw new MultiplexerError(
229
+ 'unsupported',
230
+ 'Cannot close an agent tab from mux-sesh. Close it in Herdr instead.'
231
+ )
232
+ }
233
+ await command(['herdr', 'workspace', 'close', workspace.id], 'ok')
234
+ latestSnapshot = undefined
235
+ },
236
+ async current(): Promise<WorkspaceRef | undefined> {
237
+ const state = latestSnapshot ?? (await snapshot())
238
+ const workspace = state.workspaces.find(entry => entry.id === state.focusedWorkspaceId)
239
+ return workspace ? { backend: 'herdr', id: workspace.id, title: workspace.label } : undefined
240
+ },
241
+ async directory(workspace: WorkspaceRef): Promise<string | undefined> {
242
+ const state = latestSnapshot ?? (await snapshot())
243
+ if (isAgentTarget(workspace)) {
244
+ const pane = state.panes.find(entry => entry.id === workspace.target.paneId)
245
+ return pane?.foregroundCwd ?? pane?.cwd
246
+ }
247
+ const target = state.workspaces.find(entry => entry.id === workspace.id)
248
+ return target ? workspacePath(state, target) || undefined : undefined
249
+ },
250
+ async details(workspace: WorkspaceRef): Promise<WorkspaceDetails> {
251
+ const state = latestSnapshot ?? (await snapshot())
252
+ const target = state.workspaces.find(entry => entry.id === workspace.id)
253
+ if (!target) {
254
+ throw new MultiplexerError(
255
+ 'command-failed',
256
+ `Herdr workspace '${workspace.title}' (${workspace.id}) no longer exists.`
257
+ )
258
+ }
259
+
260
+ const workspaceTabs = state.tabs.filter(
261
+ tab =>
262
+ tab.workspaceId === workspace.id &&
263
+ (!isAgentTarget(workspace) || tab.id === workspace.target.tabId)
264
+ )
265
+ const workspacePanes = state.panes.filter(pane => pane.workspaceId === workspace.id)
266
+ const previewPane = isAgentTarget(workspace)
267
+ ? state.panes.find(pane => pane.id === workspace.target.paneId)
268
+ : (focusedPaneForTab(state, target.activeTabId) ?? workspacePanes[0])
269
+ let previewLines: string[] | undefined
270
+ if (previewPane) {
271
+ const preview = await options.runner.run([
272
+ 'herdr',
273
+ 'pane',
274
+ 'read',
275
+ previewPane.id,
276
+ '--source',
277
+ 'recent-unwrapped',
278
+ '--lines',
279
+ '16',
280
+ ])
281
+ if (preview.exitCode === 0) {
282
+ const lines = preview.stdout
283
+ .split('\n')
284
+ .map(line => line.trimEnd())
285
+ .filter(line => line.length > 0)
286
+ previewLines = lines.length > 0 ? lines : undefined
287
+ }
288
+ }
289
+
290
+ return {
291
+ workspace: isAgentTarget(workspace)
292
+ ? workspace
293
+ : { backend: 'herdr', id: target.id, title: target.label },
294
+ isActive: isAgentTarget(workspace)
295
+ ? target.focused &&
296
+ (state.focusedPaneId === workspace.target.paneId ||
297
+ state.focusedTabId === workspace.target.tabId)
298
+ : target.focused,
299
+ unitLabel: 'Tabs',
300
+ units: workspaceTabs.map(tab => {
301
+ const pane = focusedPaneForTab(state, tab.id)
302
+ return {
303
+ id: tab.id,
304
+ name: tab.label,
305
+ currentPath: pane?.foregroundCwd ?? pane?.cwd ?? '',
306
+ currentCommand: pane?.displayAgent ?? pane?.label ?? pane?.title ?? '',
307
+ }
308
+ }),
309
+ previewLines,
310
+ agents: state.agents
311
+ .filter(
312
+ agent =>
313
+ agent.workspaceId === workspace.id &&
314
+ (!isAgentTarget(workspace) || agent.paneId === workspace.target.paneId)
315
+ )
316
+ .map(agent => ({
317
+ paneId: agent.paneId,
318
+ name: agent.name ?? agent.displayAgent ?? agent.agent ?? 'agent',
319
+ status: agent.status,
320
+ cwd: agent.foregroundCwd ?? agent.cwd,
321
+ })),
322
+ }
323
+ },
324
+ async openEditor(path: string, editorCommand: string): Promise<boolean> {
325
+ if (!options.insideHerdr) {
326
+ return false
327
+ }
328
+ const state = latestSnapshot ?? (await snapshot())
329
+ const workspaceId = state.focusedWorkspaceId
330
+ if (!workspaceId) {
331
+ throw new MultiplexerError(
332
+ 'command-failed',
333
+ 'Herdr has no focused workspace for the editor tab.'
334
+ )
335
+ }
336
+ const output = await command(
337
+ [
338
+ 'herdr',
339
+ 'tab',
340
+ 'create',
341
+ '--workspace',
342
+ workspaceId,
343
+ '--cwd',
344
+ path,
345
+ '--label',
346
+ 'editor',
347
+ '--focus',
348
+ ],
349
+ 'tab_created'
350
+ )
351
+ const created = parseHerdrCreatedTab(output)
352
+ await commandWithoutOutput(['herdr', 'pane', 'run', created.rootPaneId, editorCommand])
353
+ latestSnapshot = undefined
354
+ return true
355
+ },
356
+ }
357
+ }