6a66a5d481
Under gateway.multiplex_profiles a secondary's api_server and webhook are never built as
adapters (run_adapters skips SHARED_LISTENER_MIRROR_PLATFORMS: the default's listener answers
/p/<profile>/...). The multiplexer record therefore has no `<profile>:api_server` entry,
profile_platforms_from_multiplexer() returned {} for them and both /api/messaging/platforms
and /api/status?profile= fell through to `pending_restart`: the Desktop Messaging card and
Command Center said "Restart needed" forever for a platform that was answering.
- gateway.status.shared_listener_mirror_platforms projects the default's LIVE api_server /
webhook entry onto every served secondary with `ingress_url` = `<listener>/p/<profile>/v1`
(`.../webhooks/<route>`); a dead default listener is not mirrored. The api_server / webhook
adapters stamp the listener they actually bound (`listener_base`) on connect so the URL is
the real one, not a config guess. `hermes status` lists those URLs beside the other
shared-ingress platforms.
- /api/status?profile= reports `gateway_shared_with` (every profile the multiplexer carries)
when the served rung answered; null for a standalone gateway.
- Desktop: the messaging card shows the URL line; "Restart gateway" from a served profile
(statusbar menu, Cmd+K, messaging/webhooks banners, Command Center) confirms "Restart the
shared gateway? All bots on this device reconnect: default, alpha, beta" (Restart all /
Cancel) and toasts "Shared gateway restarted (3 bots)". Standalone keeps the silent path.
- Dashboard: same confirm + toast on the System page and the sidebar restart; the 409 from
start/stop on a served profile renders as an inline notice instead of a raw error toast.
146 lines
4.2 KiB
TypeScript
146 lines
4.2 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
|
import { api } from "@/lib/api";
|
|
import type { ActionStatusResponse } from "@/lib/api";
|
|
import { Toast } from "@nous-research/ui/ui/components/toast";
|
|
import { sharedGatewayProfiles, sharedGatewayRestartedMessage } from "@/lib/shared-gateway";
|
|
import { useI18n } from "@/i18n";
|
|
import {
|
|
SystemActionsContext,
|
|
type SystemAction,
|
|
} from "./system-actions-context";
|
|
|
|
const ACTION_NAMES: Record<SystemAction, string> = {
|
|
restart: "gateway-restart",
|
|
update: "hermes-update",
|
|
};
|
|
|
|
export function SystemActionsProvider({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const [pendingAction, setPendingAction] = useState<SystemAction | null>(null);
|
|
const [activeAction, setActiveAction] = useState<SystemAction | null>(null);
|
|
const [actionStatus, setActionStatus] = useState<ActionStatusResponse | null>(
|
|
null,
|
|
);
|
|
const [toast, setToast] = useState<ToastState | null>(null);
|
|
const { t } = useI18n();
|
|
|
|
useEffect(() => {
|
|
if (!toast) return;
|
|
const timer = setTimeout(() => setToast(null), 4000);
|
|
return () => clearTimeout(timer);
|
|
}, [toast]);
|
|
|
|
useEffect(() => {
|
|
if (!activeAction) return;
|
|
const name = ACTION_NAMES[activeAction];
|
|
let cancelled = false;
|
|
|
|
const poll = async () => {
|
|
try {
|
|
const resp = await api.getActionStatus(name);
|
|
if (cancelled) return;
|
|
setActionStatus(resp);
|
|
if (!resp.running) {
|
|
const ok = resp.exit_code === 0;
|
|
// A restart of the shared multiplexer reconnected every bot on the device: name the count.
|
|
const shared =
|
|
ok && activeAction === "restart"
|
|
? sharedGatewayProfiles(await api.getStatus().catch(() => null))
|
|
: null;
|
|
if (cancelled) return;
|
|
setToast({
|
|
type: ok ? "success" : "error",
|
|
message: ok
|
|
? shared
|
|
? sharedGatewayRestartedMessage(shared.length)
|
|
: t.status.actionFinished
|
|
: `${t.status.actionFailed} (exit ${resp.exit_code ?? "?"})`,
|
|
});
|
|
return;
|
|
}
|
|
} catch {
|
|
// transient fetch error; keep polling
|
|
}
|
|
if (!cancelled) setTimeout(poll, 1500);
|
|
};
|
|
|
|
poll();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [activeAction, t.status.actionFinished, t.status.actionFailed]);
|
|
|
|
const runAction = useCallback(
|
|
async (action: SystemAction) => {
|
|
setPendingAction(action);
|
|
setActionStatus(null);
|
|
try {
|
|
if (action === "restart") {
|
|
await api.restartGateway();
|
|
setActiveAction(action);
|
|
} else {
|
|
const resp = await api.updateHermes();
|
|
// Some installs cannot apply updates from inside the dashboard. The
|
|
// endpoint returns a structured {ok:false, message, update_command}
|
|
// envelope instead of spawning the action; surface that guidance
|
|
// rather than polling a synthetic failed action.
|
|
if (!resp.ok) {
|
|
const cmd = resp.update_command ? ` ${resp.update_command}` : "";
|
|
setToast({
|
|
type: "success",
|
|
message:
|
|
(resp.message ??
|
|
"Updates don't apply from this dashboard.") +
|
|
cmd,
|
|
});
|
|
return;
|
|
}
|
|
setActiveAction(action);
|
|
}
|
|
} catch (err) {
|
|
const detail = err instanceof Error ? err.message : String(err);
|
|
setToast({
|
|
type: "error",
|
|
message: `${t.status.actionFailed}: ${detail}`,
|
|
});
|
|
} finally {
|
|
setPendingAction(null);
|
|
}
|
|
},
|
|
[t.status.actionFailed],
|
|
);
|
|
|
|
const dismissLog = useCallback(() => {
|
|
setActiveAction(null);
|
|
setActionStatus(null);
|
|
}, []);
|
|
|
|
const isRunning = activeAction !== null && actionStatus?.running !== false;
|
|
const isBusy = pendingAction !== null || isRunning;
|
|
|
|
return (
|
|
<SystemActionsContext.Provider
|
|
value={{
|
|
actionStatus,
|
|
activeAction,
|
|
dismissLog,
|
|
isBusy,
|
|
isRunning,
|
|
pendingAction,
|
|
runAction,
|
|
}}
|
|
>
|
|
{children}
|
|
<Toast toast={toast} />
|
|
</SystemActionsContext.Provider>
|
|
);
|
|
}
|
|
|
|
interface ToastState {
|
|
message: string;
|
|
type: "success" | "error";
|
|
}
|