-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleet-picker.mjs
More file actions
116 lines (102 loc) · 3.89 KB
/
Copy pathfleet-picker.mjs
File metadata and controls
116 lines (102 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { pathToFileURL } from 'node:url';
import {
attachMode,
chiefAgentName,
fleetProjectDir,
listBrokerAgents,
relayAgentLabel,
} from './fleet.mjs';
import { requestHerdr } from './herdr-socket.mjs';
export const FLEET_WORKSPACE_LABEL = 'Relay fleet';
export const FLEET_AGENT_ENTRYPOINT = 'fleet-agent';
function createdWorkspace(response) {
const workspace = response?.result?.workspace;
const rootPane = response?.result?.root_pane;
if (typeof workspace?.workspace_id !== 'string' || typeof rootPane?.pane_id !== 'string') {
throw new Error('Herdr did not return the created fleet workspace');
}
return { workspaceId: workspace.workspace_id, rootPaneId: rootPane.pane_id };
}
function openedPane(response) {
const pane = response?.result?.plugin_pane?.pane;
if (typeof pane?.pane_id !== 'string') throw new Error('Herdr did not return the opened fleet pane');
return pane;
}
export async function runFleetPicker({
environment = process.env,
listAgents = listBrokerAgents,
findChief = chiefAgentName,
request = requestHerdr,
logger = console,
} = {}) {
const socketPath = environment.HERDR_SOCKET_PATH;
if (!socketPath) throw new Error('Herdr did not provide HERDR_SOCKET_PATH');
const pluginId = environment.HERDR_PLUGIN_ID;
if (!pluginId) throw new Error('Herdr did not provide HERDR_PLUGIN_ID');
const projectDir = fleetProjectDir(environment);
const mode = attachMode(environment.HERDR_RELAY_ATTACH_MODE);
const agents = await listAgents(projectDir);
if (!agents.length) {
throw new Error(`No live Agent Relay broker agents were found for ${projectDir}`);
}
const residentChief = await findChief(projectDir);
const created = createdWorkspace(
await request(socketPath, 'workspace.create', {
cwd: projectDir,
label: FLEET_WORKSPACE_LABEL,
focus: false,
})
);
const opened = [];
try {
for (const agent of agents) {
const label = relayAgentLabel(agent);
const response = await request(socketPath, 'plugin.pane.open', {
plugin_id: pluginId,
entrypoint: FLEET_AGENT_ENTRYPOINT,
placement: 'tab',
workspace_id: created.workspaceId,
cwd: projectDir,
focus: false,
env: {
HERDR_RELAY_AGENT_NAME: agent.name,
HERDR_RELAY_AGENT_LABEL: label,
HERDR_RELAY_ATTACH_MODE: mode,
HERDR_RELAY_RESIDENT_CHIEF: agent.name === residentChief ? '1' : '0',
},
});
const pane = openedPane(response);
opened.push({ agent: agent.name, paneId: pane.pane_id });
await request(socketPath, 'pane.rename', { pane_id: pane.pane_id, label: agent.name });
}
await request(socketPath, 'pane.close', { pane_id: created.rootPaneId });
await request(socketPath, 'workspace.focus', { workspace_id: created.workspaceId });
} catch (error) {
await request(socketPath, 'workspace.close', { workspace_id: created.workspaceId }).catch(() => undefined);
throw error;
}
logger.log(
`Opened ${opened.length} live Agent Relay agent pane(s) in ${FLEET_WORKSPACE_LABEL} from ${projectDir}.`
);
return { projectDir, workspaceId: created.workspaceId, panes: opened };
}
export function isDirectEntrypoint(moduleUrl, argv1) {
return Boolean(argv1) && moduleUrl === pathToFileURL(argv1).href;
}
export function waitForDismiss(input = process.stdin, output = process.stdout) {
if (!input.isTTY) return Promise.resolve();
output.write('\nPress Enter to close this pane.\n');
input.setEncoding('utf8');
input.resume();
return new Promise((resolve) => input.once('data', resolve));
}
export async function main({ dismiss = waitForDismiss, pickerOptions } = {}) {
try {
await runFleetPicker(pickerOptions);
} catch (error) {
console.error(`Agent Relay fleet picker failed: ${error.message}`);
process.exitCode = 1;
await dismiss();
}
}
if (isDirectEntrypoint(import.meta.url, process.argv[1])) await main();