pi-famulus 0.2.2-beta.1 → 0.2.2-beta.3
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 +1 -1
- package/package.json +7 -7
- package/src/config.ts +6 -0
- package/src/index.ts +12 -7
- package/src/intercom/intercom.ts +52 -17
- package/src/manager-client.ts +1 -1
- package/src/tui/famulus-command.ts +48 -10
- package/src/tui/famulus-settings.ts +35 -8
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ Run the full test suite and tell me what fails.
|
|
|
73
73
|
- **One daemon for every shell and monitor**: The daemon is the parent of every shell and monitor task across your pi sessions. If it crashes or is killed with `kill -9`, each task's process group is stopped. Commands that move into a new session with `setsid` escape this.
|
|
74
74
|
- **Shared capacity**: By default, at most 8 subagents run at once across all pi sessions on the machine (`pi-famulus config set max-agents <n>` changes it). The rest wait in a queue. Subagents labelled `work_kind: "test-suite"` or `"test"` are capped at 2 at a time; without a label, a subagent counts as `other`.
|
|
75
75
|
- **Parent and subagent talk**: A subagent asks its parent a question with `contact_supervisor` and waits for the answer. You can answer it yourself with `/reply`. `agent_message` sends messages between parent and subagents.
|
|
76
|
-
- **Cross-session messaging (pi-famulus)**: pi sessions join the local pi-famulus bridge over the same manager connection — no second socket. `SendMessage` / `ListSessions` tools, `/rename` and the `/intercom` session overlay, `@name` completion inserting `<RequestSendMessage … />` markers, a standing cross-session system-prompt section, `<cross-session-message>` deliver envelopes with read receipts, and authoritative busy/idle publication. Peer messages are input, not authority: they never grant consent.
|
|
76
|
+
- **Cross-session messaging (pi-famulus)**: pi sessions join the local pi-famulus bridge over the same manager connection — no second socket. `SendMessage` / `ListSessions` tools, `/rename` and the `/intercom` session overlay, `@name` completion inserting `<RequestSendMessage … />` markers, a standing cross-session system-prompt section, `<cross-session-message>` deliver envelopes with read receipts, and authoritative busy/idle publication. Peer messages are input, not authority: they never grant consent. Messaging is off by default: opt in with `messaging.enabled: true` after [registering the device](https://github.com/Leechael/pi-famulus/blob/main/docs/config.md#hosted-server-enrollment). While off (also with `PI_FAMULUS_NO_INTERCOM`), no messaging tools or hooks are registered. If enabled messaging fails, the plugin shows the actual error, including relay authentication failures, and `/famulus` retains the latest error until reconnection. Absorbed from the pi-famulus pi plugin (upstream baseline 98183fb); an installed legacy plugin is detected and you are asked to remove it.
|
|
77
77
|
- **Inspectable**: In a terminal, `pi-famulus top` is a live dashboard of sessions, subagents, shells and monitors. Open a subagent to read its conversation or a task to read its output, and stop a running shell or monitor from there. `ls`, `show`, `agent` and `events` show what ran, why it ended, and why a notification did or did not arrive.
|
|
78
78
|
- **Tested against real models**: Tool descriptions are graded on real-model runs. The latest matrix (2026-10-08; 12 xAI and OpenAI configurations, 10 scenarios, k=10) recorded 1198 passes and 2 failures, with 6 invalid or errored episodes excluded ([eval/RESULTS.md](https://github.com/Leechael/pi-famulus/blob/main/eval/RESULTS.md)). It predates a later wording change to the `subagent` and wake prompts ([PR #62](https://github.com/Leechael/pi-famulus/pull/62)), which has not been through the full matrix yet.
|
|
79
79
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-famulus",
|
|
3
|
-
"version": "0.2.2-beta.
|
|
3
|
+
"version": "0.2.2-beta.3",
|
|
4
4
|
"description": "Keep your main agent's context clean: subagents on cheaper models, long bash in the background, and monitors that wake the agent. One Rust daemon runs the shells.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
"pi-famulus": "./bin/pi-famulus.js"
|
|
55
55
|
},
|
|
56
56
|
"optionalDependencies": {
|
|
57
|
-
"pi-famulus-linux-x64": "0.2.2-beta.
|
|
58
|
-
"pi-famulus-linux-arm64": "0.2.2-beta.
|
|
59
|
-
"pi-famulus-darwin-x64": "0.2.2-beta.
|
|
60
|
-
"pi-famulus-darwin-arm64": "0.2.2-beta.
|
|
61
|
-
"pi-famulus-win-x64": "0.2.2-beta.
|
|
62
|
-
"pi-famulus-win-arm64": "0.2.2-beta.
|
|
57
|
+
"pi-famulus-linux-x64": "0.2.2-beta.3",
|
|
58
|
+
"pi-famulus-linux-arm64": "0.2.2-beta.3",
|
|
59
|
+
"pi-famulus-darwin-x64": "0.2.2-beta.3",
|
|
60
|
+
"pi-famulus-darwin-arm64": "0.2.2-beta.3",
|
|
61
|
+
"pi-famulus-win-x64": "0.2.2-beta.3",
|
|
62
|
+
"pi-famulus-win-arm64": "0.2.2-beta.3"
|
|
63
63
|
},
|
|
64
64
|
"pi": {
|
|
65
65
|
"extensions": [
|
package/src/config.ts
CHANGED
|
@@ -17,6 +17,8 @@ export interface FamulusConfig {
|
|
|
17
17
|
/** Explicit path to the pi-famulus binary, or null for auto-resolution. */
|
|
18
18
|
managerPath: string | null;
|
|
19
19
|
logLevel: "debug" | "info" | "warn" | "error";
|
|
20
|
+
/** Cross-session messaging requires explicit opt-in. */
|
|
21
|
+
messaging?: { enabled: boolean };
|
|
20
22
|
/** Optional M3 subagent tuning section (design doc §4.6 limits). */
|
|
21
23
|
subagent?: FamulusSubagentConfig;
|
|
22
24
|
}
|
|
@@ -139,6 +141,7 @@ export const DEFAULT_CONFIG: FamulusConfig = {
|
|
|
139
141
|
subagentBudgetMs: 45000,
|
|
140
142
|
managerPath: null,
|
|
141
143
|
logLevel: "info",
|
|
144
|
+
messaging: { enabled: false },
|
|
142
145
|
};
|
|
143
146
|
|
|
144
147
|
/** Resolve the pi-famulus base directory. PI_FAMULUS_HOME overrides the default. */
|
|
@@ -210,6 +213,9 @@ export function loadConfig(home: string = getFamulusHome()): FamulusConfig {
|
|
|
210
213
|
if (typeof raw !== "object" || raw === null) return { ...DEFAULT_CONFIG };
|
|
211
214
|
const obj = raw as Record<string, unknown>;
|
|
212
215
|
const config = { ...DEFAULT_CONFIG };
|
|
216
|
+
if (typeof obj.messaging === "object" && obj.messaging !== null) {
|
|
217
|
+
config.messaging = { enabled: (obj.messaging as Record<string, unknown>).enabled === true };
|
|
218
|
+
}
|
|
213
219
|
if (typeof obj.foregroundBudgetMs === "number" && obj.foregroundBudgetMs > 0) {
|
|
214
220
|
config.foregroundBudgetMs = obj.foregroundBudgetMs;
|
|
215
221
|
}
|
package/src/index.ts
CHANGED
|
@@ -169,6 +169,7 @@ function createFamulusRuntime(retiring?: Promise<unknown>): FamulusRuntime {
|
|
|
169
169
|
managerPath: string | null;
|
|
170
170
|
describeSearch: () => string;
|
|
171
171
|
getClient: () => ManagerClient | null;
|
|
172
|
+
messagingEnabled: boolean;
|
|
172
173
|
} | null = null;
|
|
173
174
|
const nativeRestore: Promise<string | null> = managerPath
|
|
174
175
|
? Promise.resolve(managerPath)
|
|
@@ -622,6 +623,7 @@ function createFamulusRuntime(retiring?: Promise<unknown>): FamulusRuntime {
|
|
|
622
623
|
managerPath,
|
|
623
624
|
describeSearch: () => describeManagerSearch(config, home),
|
|
624
625
|
getClient: () => client,
|
|
626
|
+
messagingEnabled: false,
|
|
625
627
|
};
|
|
626
628
|
registerFamulusCommand(pi, famulusInfo);
|
|
627
629
|
monitorRegistry.onChange(() => {
|
|
@@ -1244,7 +1246,16 @@ function createFamulusRuntime(retiring?: Promise<unknown>): FamulusRuntime {
|
|
|
1244
1246
|
}
|
|
1245
1247
|
ctx = null;
|
|
1246
1248
|
};
|
|
1247
|
-
const runtime: FamulusRuntime = {
|
|
1249
|
+
const runtime: FamulusRuntime = {
|
|
1250
|
+
attach: (host) => {
|
|
1251
|
+
const enabled = loadConfig(home).messaging?.enabled === true && process.env.PI_FAMULUS_NO_INTERCOM === undefined;
|
|
1252
|
+
if (famulusInfo) famulusInfo.messagingEnabled = enabled;
|
|
1253
|
+
binding.attach(host);
|
|
1254
|
+
// Messaging is session-scoped, not parked: reload must re-evaluate opt-in
|
|
1255
|
+
// without replacing the manager connection or other background work.
|
|
1256
|
+
registerIntercom(host, { enabled, getClient: () => client });
|
|
1257
|
+
},
|
|
1258
|
+
};
|
|
1248
1259
|
|
|
1249
1260
|
pi.on("session_before_switch", async (event, switchCtx) => {
|
|
1250
1261
|
const file = event.targetSessionFile;
|
|
@@ -1291,11 +1302,5 @@ function createFamulusRuntime(retiring?: Promise<unknown>): FamulusRuntime {
|
|
|
1291
1302
|
return message ? { message } : undefined;
|
|
1292
1303
|
});
|
|
1293
1304
|
|
|
1294
|
-
// The intercom port (cross-session messaging, absorbed from the
|
|
1295
|
-
// pi-famulus pi plugin) rides the same manager connection. Registered
|
|
1296
|
-
// after the session hooks above so its session_start sees the fresh
|
|
1297
|
-
// client. Hosted children never reach here (child-mode returns early).
|
|
1298
|
-
registerIntercom(pi, { getClient: () => client });
|
|
1299
|
-
|
|
1300
1305
|
return runtime;
|
|
1301
1306
|
}
|
package/src/intercom/intercom.ts
CHANGED
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
* connection (protocol 7 `intercom` request; frames arrive as `intercom`
|
|
13
13
|
* events; the daemon pushes `intercom_reset` when its bridge restarts).
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Messaging is opt-in: disabled configuration or PI_FAMULUS_NO_INTERCOM
|
|
16
|
+
* registers nothing. Enabled sessions report bridge and relay failures once
|
|
17
|
+
* per error and probe for recovery every 3s.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
@@ -166,6 +166,8 @@ function formatSession(s: SessionRow): string {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
export interface IntercomDeps {
|
|
169
|
+
/** Only literal true enables messaging registration. */
|
|
170
|
+
enabled?: boolean;
|
|
169
171
|
/** The current session's manager client (null before session_start).
|
|
170
172
|
* Unused when a transport is injected directly. */
|
|
171
173
|
getClient?: () => ManagerClient | null;
|
|
@@ -179,9 +181,10 @@ export interface IntercomDeps {
|
|
|
179
181
|
|
|
180
182
|
/**
|
|
181
183
|
* Registers everything intercom: tools, commands, hooks, renderers.
|
|
182
|
-
* Call once per
|
|
184
|
+
* Call once per extension attachment with the host API; reload recreates it.
|
|
183
185
|
*/
|
|
184
186
|
export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
187
|
+
if (deps.enabled !== true || process.env.PI_FAMULUS_NO_INTERCOM !== undefined) return;
|
|
185
188
|
const transport = deps.transport ?? createManagerTransport(deps.getClient ?? (() => null));
|
|
186
189
|
const now = deps.now ?? Date.now;
|
|
187
190
|
const extensionDirs = deps.extensionDirs ?? [
|
|
@@ -204,8 +207,8 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
204
207
|
let lastCtx: ExtensionContext | undefined;
|
|
205
208
|
let retryTimer: ReturnType<typeof setInterval> | undefined;
|
|
206
209
|
let manualCompactionRefresh: ReturnType<typeof setTimeout> | undefined;
|
|
207
|
-
|
|
208
|
-
let
|
|
210
|
+
let lastMessagingError: string | undefined;
|
|
211
|
+
let statusCheckEpoch: number | undefined;
|
|
209
212
|
let legacyPluginNotified = false;
|
|
210
213
|
let autocompleteBound = false;
|
|
211
214
|
let offFrames: (() => void) | undefined;
|
|
@@ -275,6 +278,37 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
275
278
|
}
|
|
276
279
|
};
|
|
277
280
|
|
|
281
|
+
function updateMessagingError(message?: string, level: "warning" | "error" = "error") {
|
|
282
|
+
if (message === lastMessagingError) return;
|
|
283
|
+
lastMessagingError = message;
|
|
284
|
+
lastCtx?.ui.setStatus?.("famulus-messaging", message);
|
|
285
|
+
if (message) notify(message, level);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function checkMessagingStatus() {
|
|
289
|
+
const client = deps.getClient?.();
|
|
290
|
+
if (!client?.isAvailable() || !client.status || statusCheckEpoch === epoch) return;
|
|
291
|
+
const expectedEpoch = epoch;
|
|
292
|
+
statusCheckEpoch = expectedEpoch;
|
|
293
|
+
try {
|
|
294
|
+
const { messaging } = await client.status();
|
|
295
|
+
if (stopped || epoch !== expectedEpoch || !lastCtx || !useContext(lastCtx)) return;
|
|
296
|
+
if (!messaging || messaging.state === "disabled") {
|
|
297
|
+
updateMessagingError();
|
|
298
|
+
} else if (messaging.error || messaging.state === "disconnected" || messaging.state === "unavailable") {
|
|
299
|
+
updateMessagingError(`pi-famulus messaging ${messaging.state}: ${messaging.error || "relay not connected"}`);
|
|
300
|
+
} else {
|
|
301
|
+
updateMessagingError();
|
|
302
|
+
}
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (!stopped && epoch === expectedEpoch && lastCtx && useContext(lastCtx)) {
|
|
305
|
+
updateMessagingError(`pi-famulus messaging status unavailable: ${(error as Error).message}`);
|
|
306
|
+
}
|
|
307
|
+
} finally {
|
|
308
|
+
if (statusCheckEpoch === expectedEpoch) statusCheckEpoch = undefined;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
278
312
|
const text = (t: string, isError = false) => ({
|
|
279
313
|
content: [{ type: "text" as const, text: t }],
|
|
280
314
|
details: undefined,
|
|
@@ -577,11 +611,8 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
577
611
|
// The first connect may still be in flight: stay silent. A
|
|
578
612
|
// recorded failure is a genuine outage: say so once. The 3s
|
|
579
613
|
// probe keeps retrying either way.
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
notify("pi-famulus: messaging bridge unavailable, cross-session messaging is paused (the rest of this session is unaffected)", "warning");
|
|
583
|
-
}
|
|
584
|
-
throw new IntercomError(BRIDGE_DOWN, "E_TRANSPORT", "unavailable");
|
|
614
|
+
const error = deps.getClient?.()?.lastError();
|
|
615
|
+
throw new IntercomError(`${BRIDGE_DOWN}${error ? ` (${error})` : ""}`, "E_TRANSPORT", "unavailable");
|
|
585
616
|
}
|
|
586
617
|
}
|
|
587
618
|
return await register(currentStatus(ctx));
|
|
@@ -601,6 +632,8 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
601
632
|
const key = sessionKey(lastCtx);
|
|
602
633
|
if (key !== boundKey || !registered || !transport.isUp()) {
|
|
603
634
|
start(lastCtx, true, false, true).catch(() => {});
|
|
635
|
+
} else {
|
|
636
|
+
void checkMessagingStatus();
|
|
604
637
|
}
|
|
605
638
|
} catch {
|
|
606
639
|
void stop();
|
|
@@ -657,22 +690,23 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
657
690
|
if (sid) sessionId = sid;
|
|
658
691
|
registered = true;
|
|
659
692
|
down = undefined;
|
|
660
|
-
|
|
693
|
+
if (deps.getClient?.()?.status) void checkMessagingStatus();
|
|
694
|
+
else updateMessagingError();
|
|
661
695
|
notify(`pi-famulus intercom active as "${sessionName}"`, "info");
|
|
662
696
|
} catch (e) {
|
|
663
697
|
if (stopped) return;
|
|
664
698
|
started = false;
|
|
665
699
|
if (e instanceof IntercomError && e.reason) {
|
|
666
700
|
down = e.reason;
|
|
667
|
-
if (e.reason === "
|
|
668
|
-
|
|
669
|
-
|
|
701
|
+
if (e.reason === "disabled") updateMessagingError();
|
|
702
|
+
else if (outageIsReal()) {
|
|
703
|
+
updateMessagingError(`pi-famulus: messaging bridge unavailable (${e.message}); cross-session messaging is paused`, "warning");
|
|
670
704
|
}
|
|
671
705
|
return;
|
|
672
706
|
}
|
|
673
707
|
const msg = (e as Error).message || String(e);
|
|
674
|
-
if (
|
|
675
|
-
|
|
708
|
+
if (msg !== BRIDGE_DOWN && msg !== BRIDGE_DISABLED) {
|
|
709
|
+
updateMessagingError(`pi-famulus intercom: ${msg}`);
|
|
676
710
|
}
|
|
677
711
|
}
|
|
678
712
|
})();
|
|
@@ -701,6 +735,7 @@ export function registerIntercom(pi: ExtensionAPI, deps: IntercomDeps): void {
|
|
|
701
735
|
readiness = undefined;
|
|
702
736
|
started = false;
|
|
703
737
|
listCache = undefined;
|
|
738
|
+
updateMessagingError();
|
|
704
739
|
lastCtx = undefined;
|
|
705
740
|
stopPromise = (async () => {
|
|
706
741
|
try {
|
package/src/manager-client.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { famulusPaths } from "./config";
|
|
|
18
18
|
import { realClock, type Clock, type ClockTimer } from "./clock";
|
|
19
19
|
|
|
20
20
|
const MAX_FRAME_BYTES = 4 * 1024 * 1024; // 4 MiB (§3.3)
|
|
21
|
-
const EXTENSION_VERSION = "0.2.2-beta.
|
|
21
|
+
const EXTENSION_VERSION = "0.2.2-beta.3";
|
|
22
22
|
export { EXTENSION_VERSION };
|
|
23
23
|
// 6: hosted subagents. 7: the extension's messaging bridge (`intercom`
|
|
24
24
|
// request plus `intercom`/`intercom_reset` events).
|
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
* whether messaging reaches its relay, and the settings pages.
|
|
4
4
|
*/
|
|
5
5
|
import { execFile } from "node:child_process";
|
|
6
|
+
import { dirname } from "node:path";
|
|
7
|
+
import { stripVTControlCharacters } from "node:util";
|
|
6
8
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
7
9
|
import type { ManagerClient, MessagingStatus } from "../manager-client";
|
|
8
10
|
import { SETTINGS_SECTIONS, listTheme, readConfigText, sectionPage, writeConfigText } from "./famulus-settings";
|
|
9
|
-
import { loadPiTui, truncateToWidth } from "./pi-tui-load";
|
|
11
|
+
import { loadPiTui, truncateToWidth, wrapTextWithAnsi } from "./pi-tui-load";
|
|
10
12
|
|
|
11
13
|
export interface FamulusCommandDeps {
|
|
12
14
|
extensionVersion: string;
|
|
@@ -15,8 +17,12 @@ export interface FamulusCommandDeps {
|
|
|
15
17
|
/** Where the binary was looked for, shown when none was found. */
|
|
16
18
|
describeSearch: () => string;
|
|
17
19
|
getClient: () => ManagerClient | null;
|
|
20
|
+
/** Effective plugin opt-in, including the environment disable override. */
|
|
21
|
+
messagingEnabled?: boolean;
|
|
18
22
|
/** `<binary> --version`; null when it cannot be run. */
|
|
19
23
|
binaryVersion?: (path: string) => Promise<string | null>;
|
|
24
|
+
/** Exchange an enrollment token using the current messaging configuration. */
|
|
25
|
+
enrollMessagingToken?: (token: string) => Promise<void>;
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
export interface FamulusInfo {
|
|
@@ -32,17 +38,23 @@ export interface FamulusInfo {
|
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
const RELAY_STATES: Record<string, string> = {
|
|
41
|
+
disabled: "disabled",
|
|
35
42
|
connected: "connected",
|
|
36
43
|
disconnected: "disconnected",
|
|
37
44
|
local_only: "local-only",
|
|
38
45
|
};
|
|
39
46
|
|
|
47
|
+
function singleLine(text: string): string {
|
|
48
|
+
return stripVTControlCharacters(text).replace(/\s+/g, " ").trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
40
51
|
export function formatMessaging(info: FamulusInfo): string {
|
|
41
52
|
const m = info.messaging;
|
|
42
53
|
if (!m) return info.daemon ? "not reported by this manager" : "unknown (manager not connected)";
|
|
43
|
-
const state = RELAY_STATES[m.state];
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
const state = RELAY_STATES[m.state] ?? "unavailable";
|
|
55
|
+
const label = m.name && RELAY_STATES[m.state] ? `${state} · name ${singleLine(m.name)}` : state;
|
|
56
|
+
const error = singleLine(m.error ?? "");
|
|
57
|
+
return `${label}${error ? ` (${error})` : ""}`;
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
export function formatFamulusInfo(info: FamulusInfo): string {
|
|
@@ -71,6 +83,17 @@ export function readBinaryVersion(path: string): Promise<string | null> {
|
|
|
71
83
|
});
|
|
72
84
|
}
|
|
73
85
|
|
|
86
|
+
export function exchangeMessagingToken(managerPath: string | null, configPath: string, token: string): Promise<void> {
|
|
87
|
+
if (!managerPath) return Promise.reject(new Error("pi-famulus binary is unavailable."));
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
execFile(managerPath, ["--home", dirname(configPath), "intercom", "enroll", "--token", token],
|
|
90
|
+
{ timeout: 25_000, windowsHide: true }, (error, _stdout, stderr) => {
|
|
91
|
+
if (error) reject(new Error(stderr.trim() || `Device enrollment failed (${error.code ?? "command failed"}).`));
|
|
92
|
+
else resolve();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
74
97
|
const STATUS_TIMEOUT_MS = 2000;
|
|
75
98
|
|
|
76
99
|
async function readMessaging(client: ManagerClient): Promise<MessagingStatus | null> {
|
|
@@ -97,14 +120,21 @@ export async function collectFamulusInfo(deps: FamulusCommandDeps): Promise<Famu
|
|
|
97
120
|
binaryVersion: deps.managerPath ? await (deps.binaryVersion ?? readBinaryVersion)(deps.managerPath) : null,
|
|
98
121
|
daemon: connected ? client?.managerInfo() ?? null : null,
|
|
99
122
|
daemonError: connected ? "" : client?.lastError() ?? "",
|
|
100
|
-
messaging:
|
|
123
|
+
messaging: deps.messagingEnabled === false
|
|
124
|
+
? { state: "disabled", name: "" }
|
|
125
|
+
: connected && client ? await readMessaging(client) : null,
|
|
101
126
|
};
|
|
102
127
|
}
|
|
103
128
|
|
|
104
129
|
type OverviewChoice = "edit-raw" | "reload" | undefined;
|
|
105
130
|
|
|
106
131
|
/** Status first, then one row per settings section and the two actions. */
|
|
107
|
-
function showOverview(
|
|
132
|
+
function showOverview(
|
|
133
|
+
ctx: ExtensionCommandContext,
|
|
134
|
+
info: FamulusInfo,
|
|
135
|
+
configPath: string,
|
|
136
|
+
enrollMessagingToken: (token: string) => Promise<void>,
|
|
137
|
+
): Promise<OverviewChoice> {
|
|
108
138
|
const piTui = loadPiTui();
|
|
109
139
|
if (!piTui) {
|
|
110
140
|
ctx.ui.notify(formatFamulusInfo(info), "info");
|
|
@@ -116,6 +146,7 @@ function showOverview(ctx: ExtensionCommandContext, info: FamulusInfo, configPat
|
|
|
116
146
|
tui: piTui,
|
|
117
147
|
theme,
|
|
118
148
|
configPath,
|
|
149
|
+
enrollMessagingToken,
|
|
119
150
|
requestRender: () => tui.requestRender(),
|
|
120
151
|
notice: (text: string, error = false) => {
|
|
121
152
|
notice = theme.fg(error ? "error" : "success", text);
|
|
@@ -149,12 +180,18 @@ function showOverview(ctx: ExtensionCommandContext, info: FamulusInfo, configPat
|
|
|
149
180
|
);
|
|
150
181
|
return {
|
|
151
182
|
render(width: number) {
|
|
152
|
-
const status = formatFamulusInfo(
|
|
183
|
+
const status = formatFamulusInfo({
|
|
184
|
+
...info,
|
|
185
|
+
messaging: info.messaging ? { ...info.messaging, error: "" } : null,
|
|
186
|
+
}).split("\n");
|
|
153
187
|
const lines = [theme.fg("accent", status[0]), ...status.slice(1).map((line) => {
|
|
154
188
|
const at = line.indexOf(": ");
|
|
155
189
|
return ` ${theme.fg("dim", line.slice(0, at + 1).padEnd(12))}${line.slice(at + 2)}`;
|
|
156
|
-
})
|
|
157
|
-
|
|
190
|
+
})];
|
|
191
|
+
const error = singleLine(info.messaging?.error ?? "");
|
|
192
|
+
if (error) lines.push(...wrapTextWithAnsi(theme.fg("error", `Messaging error: ${error}`), width));
|
|
193
|
+
lines.push("", ...list.render(width));
|
|
194
|
+
if (notice) lines.push("", ...wrapTextWithAnsi(notice, width));
|
|
158
195
|
return lines.map((line) => truncateToWidth(line, width));
|
|
159
196
|
},
|
|
160
197
|
invalidate: () => list.invalidate(),
|
|
@@ -176,7 +213,8 @@ export function registerFamulusCommand(pi: ExtensionAPI, deps: FamulusCommandDep
|
|
|
176
213
|
return;
|
|
177
214
|
}
|
|
178
215
|
while (true) {
|
|
179
|
-
const action = await showOverview(ctx, await collectFamulusInfo(deps), deps.configPath
|
|
216
|
+
const action = await showOverview(ctx, await collectFamulusInfo(deps), deps.configPath,
|
|
217
|
+
deps.enrollMessagingToken ?? ((token) => exchangeMessagingToken(deps.managerPath, deps.configPath, token)));
|
|
180
218
|
if (action === "reload") {
|
|
181
219
|
await ctx.reload();
|
|
182
220
|
return;
|
|
@@ -96,10 +96,11 @@ export const SETTINGS_SECTIONS: SettingsSection[] = [
|
|
|
96
96
|
label: "Messaging",
|
|
97
97
|
applies: RESTART,
|
|
98
98
|
specs: [
|
|
99
|
+
{ path: ["messaging", "enabled"], kind: "bool", default: "false", description: "Opt in to cross-session messaging tools, commands, and connections.", applies: "reload the extension and restart the manager daemon to apply" },
|
|
99
100
|
{ path: ["messaging", "name"], kind: "text", default: hostname(), description: "Device name shown to peers; the hostname when unset." },
|
|
100
101
|
{ path: ["messaging", "server"], kind: "text", default: "https://127.0.0.1:8443", description: "Relay server URL." },
|
|
101
|
-
{ path: ["messaging", "token"], kind: "secret", default: "not set", description: "
|
|
102
|
-
{ path: ["messaging", "localOnly"], kind: "bool", default: "false", description: "Only local sessions, no relay.
|
|
102
|
+
{ path: ["messaging", "token"], kind: "secret", default: "not set", description: "Enrollment token; Enter exchanges it for a saved device credential. The token is not stored.", applies: "exchanges immediately" },
|
|
103
|
+
{ path: ["messaging", "localOnly"], kind: "bool", default: "false", description: "Only local sessions, no relay. Enable explicitly for local-only messaging." },
|
|
103
104
|
{ path: ["messaging", "remoteMessages"], kind: "enum", values: ["allow", "refuse"], default: "allow", description: "Whether messages from other machines are accepted." },
|
|
104
105
|
{ path: ["messaging", "tlsInsecure"], kind: "bool", default: "false", description: "Skip relay TLS certificate verification." },
|
|
105
106
|
{ path: ["messaging", "mcpListen"], kind: "text", default: "off", description: "MCP listener address; empty disables it." },
|
|
@@ -211,6 +212,7 @@ export interface SettingsPageDeps {
|
|
|
211
212
|
tui: PiTuiModule;
|
|
212
213
|
theme: { fg(color: string, text: string): string };
|
|
213
214
|
configPath: string;
|
|
215
|
+
enrollMessagingToken(token: string): Promise<void>;
|
|
214
216
|
requestRender(): void;
|
|
215
217
|
/** Report a save or its failure under the list. */
|
|
216
218
|
notice(text: string, error?: boolean): void;
|
|
@@ -238,7 +240,7 @@ function valueEditor(
|
|
|
238
240
|
deps: SettingsPageDeps,
|
|
239
241
|
spec: SettingSpec,
|
|
240
242
|
raw: unknown,
|
|
241
|
-
save: (value: unknown) => boolean
|
|
243
|
+
save: (value: unknown) => boolean | Promise<boolean>,
|
|
242
244
|
done: (shown?: string) => void,
|
|
243
245
|
): PiTuiInteractive {
|
|
244
246
|
const input = new deps.tui.Input();
|
|
@@ -252,6 +254,7 @@ function valueEditor(
|
|
|
252
254
|
? "enter save · empty keeps the current value · esc cancel"
|
|
253
255
|
: `enter save · empty resets to ${spec.default} · esc cancel`;
|
|
254
256
|
let error = "";
|
|
257
|
+
let pending = false;
|
|
255
258
|
input.onSubmit = (text) => {
|
|
256
259
|
if (spec.kind === "secret" && text.trim() === "") return done();
|
|
257
260
|
const parsed = parseInput(spec, text);
|
|
@@ -260,7 +263,19 @@ function valueEditor(
|
|
|
260
263
|
deps.requestRender();
|
|
261
264
|
return;
|
|
262
265
|
}
|
|
263
|
-
|
|
266
|
+
error = "";
|
|
267
|
+
const saved = save(parsed.value);
|
|
268
|
+
if (typeof saved === "boolean") {
|
|
269
|
+
if (saved) done(displayValue(spec, parsed.value));
|
|
270
|
+
} else {
|
|
271
|
+
pending = true;
|
|
272
|
+
deps.requestRender();
|
|
273
|
+
void saved.then((success) => {
|
|
274
|
+
pending = false;
|
|
275
|
+
if (success) done("exchanged");
|
|
276
|
+
deps.requestRender();
|
|
277
|
+
});
|
|
278
|
+
}
|
|
264
279
|
};
|
|
265
280
|
input.onEscape = () => done();
|
|
266
281
|
return {
|
|
@@ -268,12 +283,12 @@ function valueEditor(
|
|
|
268
283
|
deps.theme.fg("accent", keyOf(spec)),
|
|
269
284
|
deps.theme.fg("dim", spec.description),
|
|
270
285
|
"",
|
|
271
|
-
...input.render(width),
|
|
286
|
+
...(pending ? [deps.theme.fg("dim", "Exchanging token...")] : input.render(width)),
|
|
272
287
|
"",
|
|
273
|
-
error ? deps.theme.fg("error", error) : deps.theme.fg("dim", hint),
|
|
288
|
+
error ? deps.theme.fg("error", error) : deps.theme.fg("dim", pending ? "Waiting for enrollment response" : hint),
|
|
274
289
|
],
|
|
275
290
|
invalidate: () => input.invalidate(),
|
|
276
|
-
handleInput: (data) => input.handleInput(data),
|
|
291
|
+
handleInput: (data) => { if (!pending) input.handleInput(data); },
|
|
277
292
|
};
|
|
278
293
|
}
|
|
279
294
|
|
|
@@ -287,7 +302,19 @@ export function sectionPage(deps: SettingsPageDeps, section: SettingsSection, cl
|
|
|
287
302
|
config = {};
|
|
288
303
|
}
|
|
289
304
|
const specs = new Map(section.specs.map((spec) => [keyOf(spec), spec]));
|
|
290
|
-
const
|
|
305
|
+
const enroll = async (token: string): Promise<boolean> => {
|
|
306
|
+
try {
|
|
307
|
+
await deps.enrollMessagingToken(token);
|
|
308
|
+
setConfigValue(deps.configPath, ["messaging", "token"], undefined);
|
|
309
|
+
deps.notice("Device enrolled; device credential saved.");
|
|
310
|
+
return true;
|
|
311
|
+
} catch (error) {
|
|
312
|
+
deps.notice(`Device enrollment failed: ${(error as Error).message.replaceAll(token, "[redacted]")}`, true);
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
const save = (spec: SettingSpec & { applies?: string }, value: unknown): boolean | Promise<boolean> => {
|
|
317
|
+
if (keyOf(spec) === "messaging.token") return enroll(value as string);
|
|
291
318
|
try {
|
|
292
319
|
setConfigValue(deps.configPath, spec.path, value);
|
|
293
320
|
} catch (error) {
|