mux-sesh 1.9.4 → 1.9.6
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.
- package/README.md +27 -9
- package/package.json +12 -10
- package/src/agents/session-name.ts +11 -2
- package/src/app/commands.ts +8 -8
- package/src/app/controller.ts +16 -4
- package/src/app/controls.ts +11 -9
- package/src/app/core-state.ts +5 -1
- package/src/app/data.ts +40 -5
- package/src/app/derived.ts +3 -3
- package/src/app/effects.ts +132 -15
- package/src/app/handlers.ts +42 -18
- package/src/app/keyboard.ts +3 -3
- package/src/app/modals.ts +8 -8
- package/src/app/runtime.ts +4 -1
- package/src/app/screen.tsx +11 -3
- package/src/app/state.ts +24 -4
- package/src/app/view.ts +17 -6
- package/src/config/index.ts +10 -0
- package/src/handlers/action-helpers.ts +22 -7
- package/src/handlers/actions.ts +51 -42
- package/src/handlers/keyboard/agents.ts +36 -14
- package/src/handlers/keyboard/new-session.ts +1 -1
- package/src/handlers/keyboard/normal.ts +8 -8
- package/src/handlers/keyboard/search.ts +6 -6
- package/src/handlers/keyboard/shared.ts +3 -3
- package/src/herdr/backend.ts +357 -0
- package/src/herdr/protocol.ts +268 -0
- package/src/items/order.ts +3 -7
- package/src/multiplexer/backend.ts +43 -0
- package/src/multiplexer/command.ts +48 -0
- package/src/multiplexer/index.ts +21 -0
- package/src/multiplexer/items.ts +66 -0
- package/src/multiplexer/resolve.ts +106 -0
- package/src/multiplexer/tmux-backend.ts +87 -0
- package/src/multiplexer/types.ts +93 -0
- package/src/opencode/session-name.ts +4 -2
- package/src/preview/project.ts +13 -11
- package/src/projects/session-links.ts +22 -4
- package/src/settings.ts +19 -0
- package/src/tmux/workflows.ts +5 -1
- package/src/types/index.ts +13 -1
- package/src/ui/AgentSessionGroup.tsx +25 -10
- package/src/ui/CommandsModal.tsx +15 -7
- package/src/ui/ItemList.tsx +3 -2
- package/src/ui/SessionDetailsPanel.tsx +68 -34
- package/src/ui/SessionList.tsx +23 -7
- package/src/ui/item-icon.ts +20 -7
- package/src/ui/session-meta.ts +10 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { MultiplexerError, type AgentStatus } from '../multiplexer'
|
|
2
|
+
|
|
3
|
+
export interface HerdrWorkspace {
|
|
4
|
+
id: string
|
|
5
|
+
label: string
|
|
6
|
+
focused: boolean
|
|
7
|
+
tabCount: number
|
|
8
|
+
activeTabId: string
|
|
9
|
+
agentStatus: AgentStatus
|
|
10
|
+
worktreePath?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HerdrTab {
|
|
14
|
+
id: string
|
|
15
|
+
workspaceId: string
|
|
16
|
+
number: number
|
|
17
|
+
label: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface HerdrPane {
|
|
21
|
+
id: string
|
|
22
|
+
workspaceId: string
|
|
23
|
+
tabId: string
|
|
24
|
+
focused: boolean
|
|
25
|
+
agentStatus: AgentStatus
|
|
26
|
+
cwd?: string
|
|
27
|
+
foregroundCwd?: string
|
|
28
|
+
displayAgent?: string
|
|
29
|
+
label?: string
|
|
30
|
+
title?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface HerdrAgent {
|
|
34
|
+
paneId: string
|
|
35
|
+
workspaceId: string
|
|
36
|
+
tabId: string
|
|
37
|
+
status: AgentStatus
|
|
38
|
+
name?: string
|
|
39
|
+
agent?: string
|
|
40
|
+
displayAgent?: string
|
|
41
|
+
cwd?: string
|
|
42
|
+
foregroundCwd?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface HerdrLayout {
|
|
46
|
+
workspaceId: string
|
|
47
|
+
tabId: string
|
|
48
|
+
focusedPaneId: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface HerdrSnapshot {
|
|
52
|
+
focusedWorkspaceId?: string
|
|
53
|
+
focusedTabId?: string
|
|
54
|
+
focusedPaneId?: string
|
|
55
|
+
workspaces: HerdrWorkspace[]
|
|
56
|
+
tabs: HerdrTab[]
|
|
57
|
+
panes: HerdrPane[]
|
|
58
|
+
layouts: HerdrLayout[]
|
|
59
|
+
agents: HerdrAgent[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface HerdrCreatedWorkspace {
|
|
63
|
+
workspaceId: string
|
|
64
|
+
workspaceTitle: string
|
|
65
|
+
rootPaneId: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface HerdrCreatedTab {
|
|
69
|
+
tabId: string
|
|
70
|
+
rootPaneId: string
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface HerdrStatus {
|
|
74
|
+
running: boolean
|
|
75
|
+
compatible?: boolean
|
|
76
|
+
protocol?: number
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const SUPPORTED_HERDR_PROTOCOL = 17
|
|
80
|
+
|
|
81
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
82
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function invalid(message: string): never {
|
|
86
|
+
throw new MultiplexerError(
|
|
87
|
+
'invalid-response',
|
|
88
|
+
`Herdr returned an invalid response: ${message}. Run 'herdr status --json' to verify the server and protocol.`
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function record(value: unknown, field: string): Record<string, unknown> {
|
|
93
|
+
return isRecord(value) ? value : invalid(`expected ${field} to be an object`)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function array(value: unknown, field: string): unknown[] {
|
|
97
|
+
return Array.isArray(value) ? value : invalid(`expected ${field} to be an array`)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function string(value: unknown, field: string): string {
|
|
101
|
+
return typeof value === 'string' && value.length > 0
|
|
102
|
+
? value
|
|
103
|
+
: invalid(`expected ${field} to be a non-empty string`)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function optionalString(value: unknown): string | undefined {
|
|
107
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function number(value: unknown, field: string): number {
|
|
111
|
+
return typeof value === 'number' && Number.isFinite(value)
|
|
112
|
+
? value
|
|
113
|
+
: invalid(`expected ${field} to be a number`)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function boolean(value: unknown, field: string): boolean {
|
|
117
|
+
return typeof value === 'boolean' ? value : invalid(`expected ${field} to be a boolean`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function agentStatus(value: unknown): AgentStatus {
|
|
121
|
+
return value === 'idle' ||
|
|
122
|
+
value === 'working' ||
|
|
123
|
+
value === 'blocked' ||
|
|
124
|
+
value === 'done' ||
|
|
125
|
+
value === 'unknown'
|
|
126
|
+
? value
|
|
127
|
+
: 'unknown'
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function parseWorkspace(value: unknown): HerdrWorkspace {
|
|
131
|
+
const raw = record(value, 'workspace')
|
|
132
|
+
const worktree =
|
|
133
|
+
raw.worktree === undefined ? undefined : record(raw.worktree, 'workspace.worktree')
|
|
134
|
+
return {
|
|
135
|
+
id: string(raw.workspace_id, 'workspace.workspace_id'),
|
|
136
|
+
label: string(raw.label, 'workspace.label'),
|
|
137
|
+
focused: boolean(raw.focused, 'workspace.focused'),
|
|
138
|
+
tabCount: number(raw.tab_count, 'workspace.tab_count'),
|
|
139
|
+
activeTabId: string(raw.active_tab_id, 'workspace.active_tab_id'),
|
|
140
|
+
agentStatus: agentStatus(raw.agent_status),
|
|
141
|
+
worktreePath: optionalString(worktree?.checkout_path),
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function parseTab(value: unknown): HerdrTab {
|
|
146
|
+
const raw = record(value, 'tab')
|
|
147
|
+
return {
|
|
148
|
+
id: string(raw.tab_id, 'tab.tab_id'),
|
|
149
|
+
workspaceId: string(raw.workspace_id, 'tab.workspace_id'),
|
|
150
|
+
number: number(raw.number, 'tab.number'),
|
|
151
|
+
label: string(raw.label, 'tab.label'),
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function parsePane(value: unknown): HerdrPane {
|
|
156
|
+
const raw = record(value, 'pane')
|
|
157
|
+
return {
|
|
158
|
+
id: string(raw.pane_id, 'pane.pane_id'),
|
|
159
|
+
workspaceId: string(raw.workspace_id, 'pane.workspace_id'),
|
|
160
|
+
tabId: string(raw.tab_id, 'pane.tab_id'),
|
|
161
|
+
focused: boolean(raw.focused, 'pane.focused'),
|
|
162
|
+
agentStatus: agentStatus(raw.agent_status),
|
|
163
|
+
cwd: optionalString(raw.cwd),
|
|
164
|
+
foregroundCwd: optionalString(raw.foreground_cwd),
|
|
165
|
+
displayAgent: optionalString(raw.display_agent),
|
|
166
|
+
label: optionalString(raw.label),
|
|
167
|
+
title: optionalString(raw.title),
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function parseAgent(value: unknown): HerdrAgent {
|
|
172
|
+
const raw = record(value, 'agent')
|
|
173
|
+
return {
|
|
174
|
+
paneId: string(raw.pane_id, 'agent.pane_id'),
|
|
175
|
+
workspaceId: string(raw.workspace_id, 'agent.workspace_id'),
|
|
176
|
+
tabId: string(raw.tab_id, 'agent.tab_id'),
|
|
177
|
+
status: agentStatus(raw.agent_status),
|
|
178
|
+
name: optionalString(raw.name),
|
|
179
|
+
agent: optionalString(raw.agent),
|
|
180
|
+
displayAgent: optionalString(raw.display_agent),
|
|
181
|
+
cwd: optionalString(raw.cwd),
|
|
182
|
+
foregroundCwd: optionalString(raw.foreground_cwd),
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function parseLayout(value: unknown): HerdrLayout {
|
|
187
|
+
const raw = record(value, 'layout')
|
|
188
|
+
return {
|
|
189
|
+
workspaceId: string(raw.workspace_id, 'layout.workspace_id'),
|
|
190
|
+
tabId: string(raw.tab_id, 'layout.tab_id'),
|
|
191
|
+
focusedPaneId: string(raw.focused_pane_id, 'layout.focused_pane_id'),
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function parseHerdrSnapshot(output: string): HerdrSnapshot {
|
|
196
|
+
const result = parseHerdrResult(output, 'session_snapshot')
|
|
197
|
+
const snapshot = record(result.snapshot, 'result.snapshot')
|
|
198
|
+
return {
|
|
199
|
+
focusedWorkspaceId: optionalString(snapshot.focused_workspace_id),
|
|
200
|
+
focusedTabId: optionalString(snapshot.focused_tab_id),
|
|
201
|
+
focusedPaneId: optionalString(snapshot.focused_pane_id),
|
|
202
|
+
workspaces: array(snapshot.workspaces, 'snapshot.workspaces').map(parseWorkspace),
|
|
203
|
+
tabs: array(snapshot.tabs, 'snapshot.tabs').map(parseTab),
|
|
204
|
+
panes: array(snapshot.panes, 'snapshot.panes').map(parsePane),
|
|
205
|
+
layouts: array(snapshot.layouts, 'snapshot.layouts').map(parseLayout),
|
|
206
|
+
agents: array(snapshot.agents, 'snapshot.agents').map(parseAgent),
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function parseHerdrResult(output: string, expectedType: string): Record<string, unknown> {
|
|
211
|
+
let decoded: unknown
|
|
212
|
+
try {
|
|
213
|
+
decoded = JSON.parse(output)
|
|
214
|
+
} catch {
|
|
215
|
+
invalid('output was not valid JSON')
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const envelope = record(decoded, 'response')
|
|
219
|
+
if (envelope.error !== undefined) {
|
|
220
|
+
const error = record(envelope.error, 'error')
|
|
221
|
+
const message = optionalString(error.message) ?? 'unknown Herdr API error'
|
|
222
|
+
throw new MultiplexerError('command-failed', `Herdr rejected the request: ${message}`)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const result = record(envelope.result, 'result')
|
|
226
|
+
if (result.type !== expectedType) {
|
|
227
|
+
invalid(`expected result.type to be '${expectedType}'`)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return result
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function parseHerdrCreatedWorkspace(output: string): HerdrCreatedWorkspace {
|
|
234
|
+
const result = parseHerdrResult(output, 'workspace_created')
|
|
235
|
+
const workspace = record(result.workspace, 'result.workspace')
|
|
236
|
+
const rootPane = record(result.root_pane, 'result.root_pane')
|
|
237
|
+
return {
|
|
238
|
+
workspaceId: string(workspace.workspace_id, 'workspace.workspace_id'),
|
|
239
|
+
workspaceTitle: string(workspace.label, 'workspace.label'),
|
|
240
|
+
rootPaneId: string(rootPane.pane_id, 'root_pane.pane_id'),
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function parseHerdrCreatedTab(output: string): HerdrCreatedTab {
|
|
245
|
+
const result = parseHerdrResult(output, 'tab_created')
|
|
246
|
+
const tab = record(result.tab, 'result.tab')
|
|
247
|
+
const rootPane = record(result.root_pane, 'result.root_pane')
|
|
248
|
+
return {
|
|
249
|
+
tabId: string(tab.tab_id, 'tab.tab_id'),
|
|
250
|
+
rootPaneId: string(rootPane.pane_id, 'root_pane.pane_id'),
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function parseHerdrStatus(output: string): HerdrStatus {
|
|
255
|
+
let decoded: unknown
|
|
256
|
+
try {
|
|
257
|
+
decoded = JSON.parse(output)
|
|
258
|
+
} catch {
|
|
259
|
+
invalid('status output was not valid JSON')
|
|
260
|
+
}
|
|
261
|
+
const root = record(decoded, 'status')
|
|
262
|
+
const server = record(root.server, 'status.server')
|
|
263
|
+
return {
|
|
264
|
+
running: boolean(server.running, 'status.server.running'),
|
|
265
|
+
compatible: typeof server.compatible === 'boolean' ? server.compatible : undefined,
|
|
266
|
+
protocol: typeof server.protocol === 'number' ? server.protocol : undefined,
|
|
267
|
+
}
|
|
268
|
+
}
|
package/src/items/order.ts
CHANGED
|
@@ -34,13 +34,9 @@ function applyPinnedState(items: Item[], pinnedSessions: readonly string[]): Ite
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
function splitPinnedItems(items: Item[], pinnedSessions: readonly string[]) {
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
return item ? [item] : []
|
|
41
|
-
})
|
|
42
|
-
const pinnedSet = new Set(pinnedItems.map(item => item.title))
|
|
43
|
-
const remainingItems = items.filter(item => !pinnedSet.has(item.title))
|
|
37
|
+
const pinnedItems = pinnedSessions.flatMap(title => items.filter(item => item.title === title))
|
|
38
|
+
const pinnedSet = new Set(pinnedItems)
|
|
39
|
+
const remainingItems = items.filter(item => !pinnedSet.has(item))
|
|
44
40
|
|
|
45
41
|
return { pinnedItems, remainingItems }
|
|
46
42
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createHerdrBackend } from '../herdr/backend'
|
|
2
|
+
import { createCommandRunner, type CommandRunner } from './command'
|
|
3
|
+
import { probeBackendAvailability, probeHerdrAvailability, resolveBackendKind } from './resolve'
|
|
4
|
+
import { createTmuxBackend } from './tmux-backend'
|
|
5
|
+
import { MultiplexerError, type BackendPreference, type MultiplexerBackend } from './types'
|
|
6
|
+
|
|
7
|
+
export interface ResolveMultiplexerOptions {
|
|
8
|
+
preference?: BackendPreference
|
|
9
|
+
env?: Readonly<Record<string, string | undefined>>
|
|
10
|
+
runner?: CommandRunner
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function resolveMultiplexerBackend(
|
|
14
|
+
options: ResolveMultiplexerOptions = {}
|
|
15
|
+
): Promise<MultiplexerBackend> {
|
|
16
|
+
const env = options.env ?? process.env
|
|
17
|
+
const runner = options.runner ?? createCommandRunner()
|
|
18
|
+
if (env.HERDR_ENV === '1') {
|
|
19
|
+
const herdr = await probeHerdrAvailability(runner)
|
|
20
|
+
if (!herdr.running) {
|
|
21
|
+
throw new MultiplexerError(
|
|
22
|
+
'server-stopped',
|
|
23
|
+
'mux-sesh is running inside Herdr, but its server is unavailable.'
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
if (herdr.compatible !== true) {
|
|
27
|
+
throw new MultiplexerError(
|
|
28
|
+
'protocol-mismatch',
|
|
29
|
+
'The active Herdr client and server are not compatible with mux-sesh. Herdr 0.7.5 protocol 17 is required.'
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
return createHerdrBackend({ runner, insideHerdr: true })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const contextKind = env.TMUX ? 'tmux' : undefined
|
|
36
|
+
const kind =
|
|
37
|
+
contextKind ??
|
|
38
|
+
resolveBackendKind(env, options.preference, await probeBackendAvailability(runner))
|
|
39
|
+
|
|
40
|
+
return kind === 'herdr'
|
|
41
|
+
? createHerdrBackend({ runner, insideHerdr: false })
|
|
42
|
+
: createTmuxBackend({ runner, insideTmux: Boolean(env.TMUX) })
|
|
43
|
+
}
|