Remove primary host plumbing

This commit is contained in:
Mohamed Boudra
2025-11-26 18:30:04 +01:00
parent 5807f62f1a
commit db87f331fb
17 changed files with 455 additions and 387 deletions
@@ -9,6 +9,8 @@ export interface AggregatedAgentGroup {
agents: Agent[];
}
const EMPTY_AGENT_MAP: Map<string, Agent> = new Map();
const sortAgents = (agents: Agent[]): Agent[] => {
return [...agents].sort((left, right) => {
const leftRunning = left.status === "running";
@@ -25,9 +27,10 @@ const sortAgents = (agents: Agent[]): Agent[] => {
});
};
export function useAggregatedAgents(fallbackAgents: Map<string, Agent>): AggregatedAgentGroup[] {
export function useAggregatedAgents(fallbackAgents?: Map<string, Agent>): AggregatedAgentGroup[] {
const { connectionStates } = useDaemonConnections();
const sessionDirectory = useSessionDirectory();
const fallback = fallbackAgents ?? EMPTY_AGENT_MAP;
return useMemo(() => {
const groups = new Map<string, { serverLabel: string; agents: Agent[] }>();
@@ -55,10 +58,10 @@ export function useAggregatedAgents(fallbackAgents: Map<string, Agent>): Aggrega
});
}
if (groups.size === 0) {
if (groups.size === 0 && fallback.size > 0) {
const fallbackServerId = connectionStates.keys().next().value ?? "default";
const label = connectionStates.get(fallbackServerId)?.daemon.label ?? fallbackServerId;
const fallbackList = Array.from(fallbackAgents.values());
const fallbackList = Array.from(fallback.values());
if (fallbackList.length > 0) {
groups.set(fallbackServerId, { serverLabel: label, agents: fallbackList });
}
@@ -86,5 +89,5 @@ export function useAggregatedAgents(fallbackAgents: Map<string, Agent>): Aggrega
});
return aggregatedGroups;
}, [sessionDirectory, fallbackAgents, connectionStates]);
}, [sessionDirectory, fallback, connectionStates]);
}
+13 -18
View File
@@ -1,7 +1,6 @@
import { useRef } from "react";
import { Alert } from "react-native";
import type { SessionContextValue } from "@/contexts/session-context";
import { useSession } from "@/contexts/session-context";
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
import { useSessionDirectory } from "./use-session-directory";
@@ -32,45 +31,41 @@ type UseDaemonSessionOptions = {
};
export function useDaemonSession(
serverId?: string,
serverId?: string | null,
options?: UseDaemonSessionOptions & { allowUnavailable?: false }
): SessionContextValue;
): SessionContextValue | null;
export function useDaemonSession(
serverId: string | undefined,
serverId: string | null | undefined,
options: UseDaemonSessionOptions & { allowUnavailable: true }
): SessionContextValue | null;
export function useDaemonSession(serverId?: string, options?: UseDaemonSessionOptions) {
const activeSession = useSession();
export function useDaemonSession(serverId?: string | null, options?: UseDaemonSessionOptions) {
const sessionDirectory = useSessionDirectory();
const { connectionStates } = useDaemonConnections();
const alertedDaemonsRef = useRef<Set<string>>(new Set());
const loggedDaemonsRef = useRef<Set<string>>(new Set());
const { suppressUnavailableAlert = false, allowUnavailable = false } = options ?? {};
const targetServerId = serverId ?? activeSession.serverId;
const isActiveSession = targetServerId === activeSession.serverId;
if (isActiveSession || !targetServerId) {
return activeSession;
if (!serverId) {
return null;
}
try {
return getSessionForServer(targetServerId, sessionDirectory);
return getSessionForServer(serverId, sessionDirectory);
} catch (error) {
if (error instanceof DaemonSessionUnavailableError) {
const connection = connectionStates.get(targetServerId);
const label = connection?.daemon.label ?? targetServerId;
const connection = connectionStates.get(serverId);
const label = connection?.daemon.label ?? serverId;
const status = connection?.status ?? "unknown";
const lastError = connection?.lastError ? `\n${connection.lastError}` : "";
const message = `${label} isn't connected yet (${status}). Switch to it or enable auto-connect so Paseo can reach it.${lastError}`;
if (!suppressUnavailableAlert && !alertedDaemonsRef.current.has(targetServerId)) {
alertedDaemonsRef.current.add(targetServerId);
if (!suppressUnavailableAlert && !alertedDaemonsRef.current.has(serverId)) {
alertedDaemonsRef.current.add(serverId);
Alert.alert("Host unavailable", message.trim());
}
if (!loggedDaemonsRef.current.has(targetServerId)) {
loggedDaemonsRef.current.add(targetServerId);
if (!loggedDaemonsRef.current.has(serverId)) {
loggedDaemonsRef.current.add(serverId);
console.warn(`[useDaemonSession] Session unavailable for daemon "${label}" (${status}).`);
}