@howaboua/pi-shepherdr 0.2.11 → 0.2.13
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/CHANGELOG.md +19 -0
- package/README.md +40 -10
- package/package.json +6 -6
- package/src/agents-contract.ts +15 -0
- package/src/agents-discovery.ts +6 -0
- package/src/agents-tool.ts +11 -0
- package/src/attachment-notes.ts +181 -0
- package/src/attachment-protocol.ts +155 -0
- package/src/attachment-routing.ts +188 -0
- package/src/attachment-target.ts +321 -0
- package/src/attachment.ts +327 -0
- package/src/board/config.ts +16 -3
- package/src/board/host.ts +28 -26
- package/src/board/identity.ts +14 -0
- package/src/board/membership.ts +187 -0
- package/src/board/protocol.ts +5 -0
- package/src/control-panel-actions.ts +110 -0
- package/src/control-panel-items.ts +91 -0
- package/src/control-panel-view.ts +282 -0
- package/src/control-panel.ts +142 -0
- package/src/controller.ts +79 -87
- package/src/fleet.ts +19 -4
- package/src/machine-catalog.ts +19 -13
- package/src/messages.ts +39 -0
- package/src/remote/shepherdr-checkpoints.mjs +98 -0
- package/src/remote/shepherdr-context.mjs +45 -0
- package/src/remote/shepherdr.mjs +1 -1
- package/src/remote-client.ts +10 -3
- package/src/setup-process.ts +151 -0
- package/src/shared-context.ts +88 -6
- package/src/ssh-setup.ts +144 -0
- package/src/types.ts +2 -0
- package/src/widget.ts +1 -3
- package/src/board/settings.ts +0 -98
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { ConnectedMachine } from "../fleet.js";
|
|
7
|
+
import {
|
|
8
|
+
type BoardBinding,
|
|
9
|
+
binding,
|
|
10
|
+
children,
|
|
11
|
+
members,
|
|
12
|
+
removeMember,
|
|
13
|
+
saveBinding,
|
|
14
|
+
saveChild,
|
|
15
|
+
saveMember,
|
|
16
|
+
} from "./identity.js";
|
|
17
|
+
import type { BoardEnvelope } from "./protocol.js";
|
|
18
|
+
|
|
19
|
+
function sameMembership(left: BoardBinding, right: BoardBinding) {
|
|
20
|
+
return isDeepStrictEqual(
|
|
21
|
+
{ ...left, enabled: false },
|
|
22
|
+
{ ...right, enabled: false },
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Owns board attachment adoption, registration, restoration and removal. */
|
|
27
|
+
export class BoardMembership {
|
|
28
|
+
private readonly pi: ExtensionAPI;
|
|
29
|
+
private readonly toOwner: (
|
|
30
|
+
ctx: ExtensionContext,
|
|
31
|
+
request: BoardEnvelope,
|
|
32
|
+
) => Promise<unknown>;
|
|
33
|
+
private readonly active: Map<string, unknown>;
|
|
34
|
+
constructor(
|
|
35
|
+
pi: ExtensionAPI,
|
|
36
|
+
toOwner: (
|
|
37
|
+
ctx: ExtensionContext,
|
|
38
|
+
request: BoardEnvelope,
|
|
39
|
+
) => Promise<unknown>,
|
|
40
|
+
active: Map<string, unknown>,
|
|
41
|
+
) {
|
|
42
|
+
this.pi = pi;
|
|
43
|
+
this.toOwner = toOwner;
|
|
44
|
+
this.active = active;
|
|
45
|
+
}
|
|
46
|
+
inspectAttachment(ctx: ExtensionContext) {
|
|
47
|
+
const own = binding(ctx);
|
|
48
|
+
if (own.upstream || children(ctx).length || members(ctx).length > 1)
|
|
49
|
+
throw new Error("Target already owns or belongs to a board family");
|
|
50
|
+
return own;
|
|
51
|
+
}
|
|
52
|
+
prepareAttachment(ctx: ExtensionContext, upstream: string, name: string) {
|
|
53
|
+
const own = binding(ctx);
|
|
54
|
+
if (!own.enabled)
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Enable the controller's board before attaching a member",
|
|
57
|
+
);
|
|
58
|
+
if (!ctx.sessionManager.getSessionFile())
|
|
59
|
+
throw new Error("Board attachment requires a saved controller session");
|
|
60
|
+
return { ...own, agentName: `${own.agentName}/${name}`, upstream };
|
|
61
|
+
}
|
|
62
|
+
commitAttachment(
|
|
63
|
+
ctx: ExtensionContext,
|
|
64
|
+
expected: BoardBinding,
|
|
65
|
+
desired: BoardBinding,
|
|
66
|
+
) {
|
|
67
|
+
if (!ctx.isIdle())
|
|
68
|
+
throw new Error("Attach a board only after the target settles");
|
|
69
|
+
const own = binding(ctx);
|
|
70
|
+
const adopted = { ...desired, sessionId: own.sessionId };
|
|
71
|
+
if (isDeepStrictEqual(own, adopted)) return adopted;
|
|
72
|
+
if (!isDeepStrictEqual(this.inspectAttachment(ctx), expected))
|
|
73
|
+
throw new Error(
|
|
74
|
+
"Target board changed during attachment; resolve it again",
|
|
75
|
+
);
|
|
76
|
+
saveBinding(this.pi, adopted);
|
|
77
|
+
return adopted;
|
|
78
|
+
}
|
|
79
|
+
async registerAttachment(
|
|
80
|
+
ctx: ExtensionContext,
|
|
81
|
+
runtime: ConnectedMachine,
|
|
82
|
+
sessionFile: string,
|
|
83
|
+
adopted: BoardBinding,
|
|
84
|
+
expected: BoardBinding,
|
|
85
|
+
) {
|
|
86
|
+
if (!isDeepStrictEqual(binding(ctx), expected))
|
|
87
|
+
throw new Error("Controller board changed during attachment");
|
|
88
|
+
saveBinding(this.pi, expected);
|
|
89
|
+
await this.toOwner(ctx, {
|
|
90
|
+
operation: "board-register",
|
|
91
|
+
caller: expected,
|
|
92
|
+
member: adopted,
|
|
93
|
+
});
|
|
94
|
+
if (!isDeepStrictEqual(binding(ctx), expected))
|
|
95
|
+
throw new Error("Controller board changed during attachment");
|
|
96
|
+
saveChild(this.pi, {
|
|
97
|
+
parentSessionId: expected.sessionId,
|
|
98
|
+
binding: adopted,
|
|
99
|
+
machine: runtime.machine,
|
|
100
|
+
sessionFile,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
detachTarget(
|
|
104
|
+
ctx: ExtensionContext,
|
|
105
|
+
desired: BoardBinding,
|
|
106
|
+
previous: BoardBinding,
|
|
107
|
+
) {
|
|
108
|
+
if (children(ctx).length)
|
|
109
|
+
throw new Error(
|
|
110
|
+
"Detach a board member only when it has no board children",
|
|
111
|
+
);
|
|
112
|
+
if (
|
|
113
|
+
!sameMembership(binding(ctx), {
|
|
114
|
+
...desired,
|
|
115
|
+
sessionId: previous.sessionId,
|
|
116
|
+
})
|
|
117
|
+
)
|
|
118
|
+
throw new Error("Target board changed before detach");
|
|
119
|
+
saveBinding(this.pi, previous);
|
|
120
|
+
this.active.clear();
|
|
121
|
+
}
|
|
122
|
+
async unregisterAttachment(ctx: ExtensionContext, member: BoardBinding) {
|
|
123
|
+
const own = binding(ctx);
|
|
124
|
+
await this.toOwner(ctx, {
|
|
125
|
+
operation: "board-unregister",
|
|
126
|
+
caller: own,
|
|
127
|
+
member,
|
|
128
|
+
});
|
|
129
|
+
if (!sameMembership(binding(ctx), own))
|
|
130
|
+
throw new Error("Controller board changed during detach");
|
|
131
|
+
removeMember(this.pi, member);
|
|
132
|
+
}
|
|
133
|
+
commitDirectory(
|
|
134
|
+
own: BoardBinding,
|
|
135
|
+
directory: BoardBinding[],
|
|
136
|
+
caller: BoardBinding,
|
|
137
|
+
request: Extract<
|
|
138
|
+
BoardEnvelope,
|
|
139
|
+
{ operation: "board-register" | "board-unregister" }
|
|
140
|
+
>,
|
|
141
|
+
) {
|
|
142
|
+
if (request.operation === "board-unregister") {
|
|
143
|
+
const member = request.member;
|
|
144
|
+
if (
|
|
145
|
+
member.boardId !== own.boardId ||
|
|
146
|
+
member.databasePath !== own.databasePath ||
|
|
147
|
+
!member.agentName.startsWith(`${caller.agentName}/`)
|
|
148
|
+
)
|
|
149
|
+
throw new Error("Invalid board member removal");
|
|
150
|
+
if (
|
|
151
|
+
directory.some((entry) =>
|
|
152
|
+
entry.agentName.startsWith(`${member.agentName}/`),
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
throw new Error(
|
|
156
|
+
"Detach a board member only when it has no board children",
|
|
157
|
+
);
|
|
158
|
+
const existing = directory.find(
|
|
159
|
+
(entry) => entry.agentName === member.agentName,
|
|
160
|
+
);
|
|
161
|
+
if (existing && !sameMembership(existing, member))
|
|
162
|
+
throw new Error("Board member changed before detach");
|
|
163
|
+
removeMember(this.pi, member);
|
|
164
|
+
this.active.delete(member.agentName);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
if (request.operation === "board-register") {
|
|
168
|
+
const member = request.member;
|
|
169
|
+
if (
|
|
170
|
+
member.boardId !== own.boardId ||
|
|
171
|
+
member.databasePath !== own.databasePath ||
|
|
172
|
+
member.rootSessionId !== own.rootSessionId ||
|
|
173
|
+
!member.agentName.startsWith(`${caller.agentName}/`)
|
|
174
|
+
)
|
|
175
|
+
throw new Error("Invalid board member registration");
|
|
176
|
+
const existing = directory.find(
|
|
177
|
+
(entry) =>
|
|
178
|
+
entry.agentName === member.agentName ||
|
|
179
|
+
entry.sessionId === member.sessionId,
|
|
180
|
+
);
|
|
181
|
+
if (existing && isDeepStrictEqual(existing, member)) return true;
|
|
182
|
+
if (existing) throw new Error("Board member already bound");
|
|
183
|
+
saveMember(this.pi, member);
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
package/src/board/protocol.ts
CHANGED
|
@@ -26,6 +26,11 @@ const Protocol = Type.Union([
|
|
|
26
26
|
caller: BindingSchema,
|
|
27
27
|
member: BindingSchema,
|
|
28
28
|
}),
|
|
29
|
+
Type.Object({
|
|
30
|
+
operation: Type.Literal("board-unregister"),
|
|
31
|
+
caller: BindingSchema,
|
|
32
|
+
member: BindingSchema,
|
|
33
|
+
}),
|
|
29
34
|
Type.Object({
|
|
30
35
|
operation: Type.Literal("board-enabled"),
|
|
31
36
|
boardId: Type.String(),
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { PanelOwners } from "./control-panel-items.js";
|
|
3
|
+
import { type SshSetupDraft, sshSetupArgs } from "./ssh-setup.js";
|
|
4
|
+
|
|
5
|
+
export interface PanelOptions extends PanelOwners {
|
|
6
|
+
setOrchestration: (enabled: boolean, signal: AbortSignal) => Promise<void>;
|
|
7
|
+
reconnect: (
|
|
8
|
+
machine: string | undefined,
|
|
9
|
+
signal: AbortSignal,
|
|
10
|
+
) => Promise<string>;
|
|
11
|
+
signal: AbortSignal;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Executes panel actions without owning native input or terminal handoff. */
|
|
15
|
+
export class PanelActions {
|
|
16
|
+
busy = false;
|
|
17
|
+
message: string;
|
|
18
|
+
|
|
19
|
+
private readonly ctx: ExtensionContext;
|
|
20
|
+
private readonly options: PanelOptions;
|
|
21
|
+
private readonly signal: AbortSignal;
|
|
22
|
+
private readonly current: () => boolean;
|
|
23
|
+
private readonly check: () => void;
|
|
24
|
+
private readonly rebuild: () => void;
|
|
25
|
+
private readonly requestRender: () => void;
|
|
26
|
+
private readonly finish: (result: "add" | undefined) => void;
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
ctx: ExtensionContext,
|
|
30
|
+
options: PanelOptions,
|
|
31
|
+
signal: AbortSignal,
|
|
32
|
+
message: string,
|
|
33
|
+
current: () => boolean,
|
|
34
|
+
check: () => void,
|
|
35
|
+
rebuild: () => void,
|
|
36
|
+
requestRender: () => void,
|
|
37
|
+
finish: (result: "add" | undefined) => void,
|
|
38
|
+
) {
|
|
39
|
+
this.ctx = ctx;
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.signal = signal;
|
|
42
|
+
this.current = current;
|
|
43
|
+
this.check = check;
|
|
44
|
+
this.rebuild = rebuild;
|
|
45
|
+
this.requestRender = requestRender;
|
|
46
|
+
this.finish = finish;
|
|
47
|
+
this.message = message;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
change(id: string, value: string, draft: SshSetupDraft) {
|
|
51
|
+
if (id === "ssh:add") {
|
|
52
|
+
try {
|
|
53
|
+
sshSetupArgs(draft);
|
|
54
|
+
this.finish("add");
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.message = error instanceof Error ? error.message : String(error);
|
|
57
|
+
this.rebuild();
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.run(async () => {
|
|
62
|
+
if (id === "orchestration") {
|
|
63
|
+
await this.options.setOrchestration(value === "on", this.signal);
|
|
64
|
+
this.message = "Guidance saved for this session.";
|
|
65
|
+
} else if (
|
|
66
|
+
id === "board:session" ||
|
|
67
|
+
id === "board:folder" ||
|
|
68
|
+
id === "board:global"
|
|
69
|
+
) {
|
|
70
|
+
await this.options.board.setSetting(
|
|
71
|
+
this.ctx,
|
|
72
|
+
id === "board:session"
|
|
73
|
+
? "session"
|
|
74
|
+
: id === "board:folder"
|
|
75
|
+
? "folder"
|
|
76
|
+
: "global",
|
|
77
|
+
value === "inherit" ? undefined : value === "on",
|
|
78
|
+
);
|
|
79
|
+
this.check();
|
|
80
|
+
this.message = "Board setting saved.";
|
|
81
|
+
} else if (id === "connect" || id.startsWith("machine:"))
|
|
82
|
+
this.message = await this.options.reconnect(
|
|
83
|
+
id === "connect" ? undefined : id.slice(8),
|
|
84
|
+
this.signal,
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private run(work: () => Promise<void>) {
|
|
90
|
+
if (this.busy || !this.current()) {
|
|
91
|
+
this.rebuild();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.busy = true;
|
|
95
|
+
this.message = "Working...";
|
|
96
|
+
this.requestRender();
|
|
97
|
+
void (async () => {
|
|
98
|
+
try {
|
|
99
|
+
this.check();
|
|
100
|
+
await work();
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (this.current())
|
|
103
|
+
this.message = error instanceof Error ? error.message : String(error);
|
|
104
|
+
} finally {
|
|
105
|
+
this.busy = false;
|
|
106
|
+
this.rebuild();
|
|
107
|
+
}
|
|
108
|
+
})();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { SettingItem } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { AgentBoard } from "./board/host.js";
|
|
4
|
+
import type { AgentFleet } from "./fleet.js";
|
|
5
|
+
import { type SshSetupDraft, sshSetupItems } from "./ssh-setup.js";
|
|
6
|
+
|
|
7
|
+
export const PANEL_TABS = ["Settings", "Status", "Connections"] as const;
|
|
8
|
+
export type PanelTab = (typeof PANEL_TABS)[number];
|
|
9
|
+
export interface PanelOwners {
|
|
10
|
+
fleet: AgentFleet;
|
|
11
|
+
board: AgentBoard;
|
|
12
|
+
orchestration: () => boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function buildPanelItems(
|
|
16
|
+
ctx: ExtensionContext,
|
|
17
|
+
options: PanelOwners,
|
|
18
|
+
tab: PanelTab,
|
|
19
|
+
draft: SshSetupDraft,
|
|
20
|
+
): SettingItem[] {
|
|
21
|
+
if (tab === "Connections") return sshSetupItems(draft);
|
|
22
|
+
if (tab === "Status")
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
id: "connect",
|
|
26
|
+
label: "Refresh and reconnect",
|
|
27
|
+
currentValue: "Enter",
|
|
28
|
+
values: ["Enter"],
|
|
29
|
+
description: "Reload Herdr profiles and retry connections.",
|
|
30
|
+
},
|
|
31
|
+
...options.fleet.statuses().map((machine) => ({
|
|
32
|
+
id: `machine:${machine.id}`,
|
|
33
|
+
label: machine.label ?? (machine.local ? "Local" : machine.id),
|
|
34
|
+
currentValue: machine.attempting ? "connecting" : machine.status,
|
|
35
|
+
values: ["Enter"],
|
|
36
|
+
description: [
|
|
37
|
+
machine.target,
|
|
38
|
+
machine.reason,
|
|
39
|
+
machine.monitoringIssue
|
|
40
|
+
? `Monitoring: ${machine.monitoringIssue.message}`
|
|
41
|
+
: undefined,
|
|
42
|
+
machine.contextRelayError
|
|
43
|
+
? `Sharing: ${machine.contextRelayError}`
|
|
44
|
+
: undefined,
|
|
45
|
+
"Enter to reconnect.",
|
|
46
|
+
]
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
.join("\n"),
|
|
49
|
+
})),
|
|
50
|
+
];
|
|
51
|
+
const rows: SettingItem[] = [
|
|
52
|
+
{
|
|
53
|
+
id: "orchestration",
|
|
54
|
+
label: "Prioritize delegation",
|
|
55
|
+
currentValue: options.orchestration() ? "on" : "off",
|
|
56
|
+
values: ["off", "on"],
|
|
57
|
+
description: "Prefer delegation over direct work in this session.",
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
try {
|
|
61
|
+
const settings = options.board.settings(ctx);
|
|
62
|
+
for (const scope of ["session", "folder", "global"] as const) {
|
|
63
|
+
if (scope === "folder" && !settings.paths.folder) continue;
|
|
64
|
+
rows.push({
|
|
65
|
+
id: `board:${scope}`,
|
|
66
|
+
label: `Board ${scope}`,
|
|
67
|
+
currentValue:
|
|
68
|
+
settings[scope] === undefined
|
|
69
|
+
? "inherit"
|
|
70
|
+
: settings[scope]
|
|
71
|
+
? "on"
|
|
72
|
+
: "off",
|
|
73
|
+
values: scope === "global" ? ["off", "on"] : ["inherit", "on", "off"],
|
|
74
|
+
description:
|
|
75
|
+
scope === "session"
|
|
76
|
+
? "Overrides folder and global defaults. Inherit removes the override."
|
|
77
|
+
: scope === "folder"
|
|
78
|
+
? "Default for this exact launch folder, unless overridden by the session."
|
|
79
|
+
: "Default for sessions without a session or folder override.",
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
rows.push({
|
|
84
|
+
id: "board-owner",
|
|
85
|
+
label: "Board settings",
|
|
86
|
+
currentValue: "unavailable",
|
|
87
|
+
description: error instanceof Error ? error.message : String(error),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return rows;
|
|
91
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ExtensionContext,
|
|
3
|
+
getSettingsListTheme,
|
|
4
|
+
type Theme,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
getKeybindings,
|
|
8
|
+
Input,
|
|
9
|
+
Key,
|
|
10
|
+
matchesKey,
|
|
11
|
+
type SettingItem,
|
|
12
|
+
SettingsList,
|
|
13
|
+
type TUI,
|
|
14
|
+
type TuiMouseEvent,
|
|
15
|
+
truncateToWidth,
|
|
16
|
+
wrapTextWithAnsi,
|
|
17
|
+
} from "@earendil-works/pi-tui";
|
|
18
|
+
import type { PanelActions } from "./control-panel-actions.js";
|
|
19
|
+
import {
|
|
20
|
+
buildPanelItems,
|
|
21
|
+
type PanelOwners,
|
|
22
|
+
type PanelTab,
|
|
23
|
+
PANEL_TABS as TABS,
|
|
24
|
+
} from "./control-panel-items.js";
|
|
25
|
+
import type { SshSetupDraft } from "./ssh-setup.js";
|
|
26
|
+
|
|
27
|
+
const LIST_ROWS = 8;
|
|
28
|
+
|
|
29
|
+
/** Native list, field editor and mouse geometry share one selection owner. */
|
|
30
|
+
export class PanelView {
|
|
31
|
+
private selectedRow: string | undefined;
|
|
32
|
+
private editing: Input | undefined;
|
|
33
|
+
private list!: SettingsList;
|
|
34
|
+
private definitions: SettingItem[] = [];
|
|
35
|
+
private listRowOffset = 0;
|
|
36
|
+
private listHeight = 0;
|
|
37
|
+
private pressedRow: string | undefined;
|
|
38
|
+
|
|
39
|
+
private readonly ctx: ExtensionContext;
|
|
40
|
+
private readonly owners: PanelOwners;
|
|
41
|
+
private readonly tui: TUI;
|
|
42
|
+
private readonly theme: Theme;
|
|
43
|
+
tab: PanelTab;
|
|
44
|
+
private readonly draft: SshSetupDraft;
|
|
45
|
+
private readonly actions: PanelActions;
|
|
46
|
+
private readonly current: () => boolean;
|
|
47
|
+
private readonly close: () => void;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
ctx: ExtensionContext,
|
|
51
|
+
owners: PanelOwners,
|
|
52
|
+
tui: TUI,
|
|
53
|
+
theme: Theme,
|
|
54
|
+
tab: PanelTab,
|
|
55
|
+
draft: SshSetupDraft,
|
|
56
|
+
actions: PanelActions,
|
|
57
|
+
current: () => boolean,
|
|
58
|
+
close: () => void,
|
|
59
|
+
) {
|
|
60
|
+
this.ctx = ctx;
|
|
61
|
+
this.owners = owners;
|
|
62
|
+
this.tui = tui;
|
|
63
|
+
this.theme = theme;
|
|
64
|
+
this.tab = tab;
|
|
65
|
+
this.draft = draft;
|
|
66
|
+
this.actions = actions;
|
|
67
|
+
this.current = current;
|
|
68
|
+
this.close = close;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
rebuild() {
|
|
72
|
+
if (!this.current() || this.editing) return;
|
|
73
|
+
try {
|
|
74
|
+
this.definitions = buildPanelItems(
|
|
75
|
+
this.ctx,
|
|
76
|
+
this.owners,
|
|
77
|
+
this.tab,
|
|
78
|
+
this.draft,
|
|
79
|
+
);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
this.actions.message =
|
|
82
|
+
error instanceof Error ? error.message : String(error);
|
|
83
|
+
this.definitions = [
|
|
84
|
+
{
|
|
85
|
+
id: "error",
|
|
86
|
+
label: "Settings unavailable",
|
|
87
|
+
currentValue: "error",
|
|
88
|
+
description: this.actions.message,
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
if (!this.definitions.some((item) => item.id === this.selectedRow))
|
|
93
|
+
this.selectedRow = this.definitions[0]?.id;
|
|
94
|
+
this.list = new SettingsList(
|
|
95
|
+
this.definitions,
|
|
96
|
+
LIST_ROWS,
|
|
97
|
+
getSettingsListTheme(),
|
|
98
|
+
(id, value) => this.change(id, value),
|
|
99
|
+
this.close,
|
|
100
|
+
);
|
|
101
|
+
this.pressedRow = undefined;
|
|
102
|
+
if (this.selectedRow) this.list.selectItem(this.selectedRow);
|
|
103
|
+
this.tui.requestRender();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private change(id: string, value: string) {
|
|
107
|
+
this.selectedRow = id;
|
|
108
|
+
if (this.actions.busy || !this.current()) {
|
|
109
|
+
this.rebuild();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (id === "ssh:target" || id === "ssh:label" || id === "ssh:session") {
|
|
113
|
+
const field =
|
|
114
|
+
id === "ssh:target"
|
|
115
|
+
? "target"
|
|
116
|
+
: id === "ssh:label"
|
|
117
|
+
? "label"
|
|
118
|
+
: "session";
|
|
119
|
+
const input = new Input({
|
|
120
|
+
prompt: `${this.definitions.find((item) => item.id === id)?.label ?? field}: `,
|
|
121
|
+
});
|
|
122
|
+
input.setValue(this.draft[field]);
|
|
123
|
+
input.focused = true;
|
|
124
|
+
input.onSubmit = (text) => {
|
|
125
|
+
this.draft[field] = text.trim();
|
|
126
|
+
this.editing = undefined;
|
|
127
|
+
this.actions.message = "";
|
|
128
|
+
this.rebuild();
|
|
129
|
+
};
|
|
130
|
+
input.onEscape = () => {
|
|
131
|
+
this.editing = undefined;
|
|
132
|
+
this.rebuild();
|
|
133
|
+
};
|
|
134
|
+
this.editing = input;
|
|
135
|
+
this.tui.requestRender();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
this.actions.change(id, value, this.draft);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
render(width: number) {
|
|
142
|
+
const wrap = (text: string) =>
|
|
143
|
+
text
|
|
144
|
+
.split("\n")
|
|
145
|
+
.flatMap((line) =>
|
|
146
|
+
wrapTextWithAnsi(line, Math.max(1, width - 2)).map(
|
|
147
|
+
(part) => ` ${part}`,
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
const header = [
|
|
151
|
+
this.theme.fg("accent", "─".repeat(Math.max(0, width))),
|
|
152
|
+
truncateToWidth(
|
|
153
|
+
` Shepherdr · ${TABS.map((name) => (name === this.tab ? this.theme.bold(name) : this.theme.fg("dim", name))).join(" / ")}`,
|
|
154
|
+
width,
|
|
155
|
+
"",
|
|
156
|
+
),
|
|
157
|
+
"",
|
|
158
|
+
...wrap(
|
|
159
|
+
this.tab === "Settings"
|
|
160
|
+
? this.owners.board.status(this.ctx, false)
|
|
161
|
+
: this.tab === "Connections"
|
|
162
|
+
? "Remote needs Node on SSH PATH and Pi with Herdr integration. Herdr prepares its server before saving."
|
|
163
|
+
: `${this.owners.fleet.isActive() ? "" : "Run Pi inside Herdr. "}Add SSH targets in Connections; select a machine here to reconnect.`,
|
|
164
|
+
).filter((line) => line.trim()),
|
|
165
|
+
"",
|
|
166
|
+
];
|
|
167
|
+
const listLines = this.editing
|
|
168
|
+
? this.editing.render(width)
|
|
169
|
+
: this.list.render(width).slice(0, -2);
|
|
170
|
+
this.listRowOffset = header.length;
|
|
171
|
+
this.listHeight = listLines.length;
|
|
172
|
+
return [
|
|
173
|
+
...header,
|
|
174
|
+
...listLines,
|
|
175
|
+
...(this.actions.message
|
|
176
|
+
? [
|
|
177
|
+
"",
|
|
178
|
+
...wrap(this.actions.message).map((line) =>
|
|
179
|
+
this.theme.fg(this.actions.busy ? "dim" : "muted", line),
|
|
180
|
+
),
|
|
181
|
+
]
|
|
182
|
+
: []),
|
|
183
|
+
"",
|
|
184
|
+
...wrap(
|
|
185
|
+
this.editing
|
|
186
|
+
? "Enter save · Esc cancel"
|
|
187
|
+
: "Tab switch · ↑/↓ select · Enter change · Esc close",
|
|
188
|
+
).map((line) => this.theme.fg("dim", line)),
|
|
189
|
+
this.theme.fg("accent", "─".repeat(Math.max(0, width))),
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
invalidate() {
|
|
194
|
+
this.list.invalidate();
|
|
195
|
+
this.editing?.invalidate();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
handleMouse(event: TuiMouseEvent) {
|
|
199
|
+
const y = event.y - this.listRowOffset;
|
|
200
|
+
if (!this.current() || y < 0 || y >= this.listHeight) return;
|
|
201
|
+
if (this.editing) return this.editing.handleMouse({ ...event, y });
|
|
202
|
+
if (this.actions.busy) return { handled: true, render: false };
|
|
203
|
+
const index = this.definitions.findIndex(
|
|
204
|
+
(item) => item.id === this.selectedRow,
|
|
205
|
+
);
|
|
206
|
+
// Keep refresh selection aligned with SettingsList's visible window.
|
|
207
|
+
if (event.type === "wheel" && event.wheelDelta) {
|
|
208
|
+
const next = Math.max(
|
|
209
|
+
0,
|
|
210
|
+
Math.min(
|
|
211
|
+
this.definitions.length - 1,
|
|
212
|
+
index + (event.wheelDelta < 0 ? -1 : 1),
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
this.selectedRow = this.definitions[next]?.id;
|
|
216
|
+
} else if (
|
|
217
|
+
event.button === "left" &&
|
|
218
|
+
(event.type === "press" || event.type === "click")
|
|
219
|
+
) {
|
|
220
|
+
const start = Math.max(
|
|
221
|
+
0,
|
|
222
|
+
Math.min(
|
|
223
|
+
index - Math.floor(LIST_ROWS / 2),
|
|
224
|
+
this.definitions.length - LIST_ROWS,
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
const row = start + y;
|
|
228
|
+
if (
|
|
229
|
+
row >= start &&
|
|
230
|
+
row < Math.min(start + LIST_ROWS, this.definitions.length)
|
|
231
|
+
) {
|
|
232
|
+
this.selectedRow =
|
|
233
|
+
event.type === "click"
|
|
234
|
+
? (this.pressedRow ?? this.definitions[row]?.id)
|
|
235
|
+
: this.definitions[row]?.id;
|
|
236
|
+
this.pressedRow = event.type === "press" ? this.selectedRow : undefined;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return this.list.handleMouse({ ...event, y, height: this.listHeight });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
handleInput(data: string) {
|
|
243
|
+
if (this.editing) {
|
|
244
|
+
this.editing.handleInput(data);
|
|
245
|
+
this.tui.requestRender();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (matchesKey(data, Key.escape)) {
|
|
249
|
+
this.close();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.shift(Key.tab))) {
|
|
253
|
+
const direction = matchesKey(data, Key.tab) ? 1 : -1;
|
|
254
|
+
this.tab =
|
|
255
|
+
TABS[
|
|
256
|
+
(TABS.indexOf(this.tab) + direction + TABS.length) % TABS.length
|
|
257
|
+
] ?? "Settings";
|
|
258
|
+
this.selectedRow = undefined;
|
|
259
|
+
this.rebuild();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (!this.actions.busy) {
|
|
263
|
+
const kb = getKeybindings();
|
|
264
|
+
if (
|
|
265
|
+
kb.matches(data, "tui.select.up") ||
|
|
266
|
+
kb.matches(data, "tui.select.down")
|
|
267
|
+
) {
|
|
268
|
+
const direction = kb.matches(data, "tui.select.up") ? -1 : 1;
|
|
269
|
+
const index = this.definitions.findIndex(
|
|
270
|
+
(item) => item.id === this.selectedRow,
|
|
271
|
+
);
|
|
272
|
+
this.selectedRow =
|
|
273
|
+
this.definitions[
|
|
274
|
+
(index + direction + this.definitions.length) %
|
|
275
|
+
this.definitions.length
|
|
276
|
+
]?.id;
|
|
277
|
+
}
|
|
278
|
+
this.list.handleInput(data);
|
|
279
|
+
}
|
|
280
|
+
this.tui.requestRender();
|
|
281
|
+
}
|
|
282
|
+
}
|