refactor: remove daemon connections context, use host runtime directly

This commit is contained in:
Mohamed Boudra
2026-02-21 08:52:35 +07:00
parent 0350f00840
commit 5dae7f854f
13 changed files with 117 additions and 181 deletions
@@ -40,7 +40,7 @@ import {
ExplorerSidebarAnimationProvider,
} from "@/contexts/explorer-sidebar-animation-context";
import { usePanelStore } from "@/stores/panel-store";
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
import { useDaemonRegistry } from "@/contexts/daemon-registry-context";
import { useSessionStore } from "@/stores/session-store";
import {
useHostRuntimeSession,
@@ -112,7 +112,7 @@ export function AgentReadyScreen({
const router = useRouter();
const resolvedAgentId = agentId?.trim() || undefined;
const resolvedServerId = serverId?.trim() || undefined;
const { connectionStates } = useDaemonConnections();
const { daemons } = useDaemonRegistry();
const runtimeServerId = resolvedServerId ?? "";
const {
snapshot: runtimeSnapshot,
@@ -121,12 +121,12 @@ export function AgentReadyScreen({
} = useHostRuntimeSession(runtimeServerId);
const connectionServerId = resolvedServerId ?? null;
const connection = connectionServerId
? connectionStates.get(connectionServerId)
const daemon = connectionServerId
? daemons.find((entry) => entry.serverId === connectionServerId) ?? null
: null;
const serverLabel =
connection?.daemon.label ?? connectionServerId ?? "Selected host";
const isUnknownDaemon = Boolean(connectionServerId && !connection);
daemon?.label ?? connectionServerId ?? "Selected host";
const isUnknownDaemon = Boolean(connectionServerId && !daemon);
const connectionStatus: HostRuntimeConnectionStatus =
runtimeSnapshot?.connectionStatus ??
(isUnknownDaemon ? "offline" : "connecting");
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from 'react'
import { useCallback, useEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from 'react'
import { createNameId } from 'mnemonic-id'
import type { ImageAttachment } from '@/components/message-input'
import { View, Text, Pressable, ScrollView, Keyboard, Platform } from 'react-native'
@@ -26,7 +26,6 @@ import {
checkoutStatusQueryKey,
} from '@/hooks/use-checkout-status-query'
import { useAllAgentsList } from '@/hooks/use-all-agents-list'
import { useDaemonConnections } from '@/contexts/daemon-connections-context'
import { useDaemonRegistry } from '@/contexts/daemon-registry-context'
import { buildBranchComboOptions, normalizeBranchOptionName } from '@/utils/branch-suggestions'
import { shortenPath } from '@/utils/shorten-path'
@@ -35,7 +34,7 @@ import { buildWorkingDirectorySuggestions } from '@/utils/working-directory-sugg
import { useExplorerOpenGesture } from '@/hooks/use-explorer-open-gesture'
import { useSessionStore } from '@/stores/session-store'
import { useCreateFlowStore } from '@/stores/create-flow-store'
import { useHostRuntimeSession } from '@/runtime/host-runtime'
import { getHostRuntimeStore, useHostRuntimeSession } from '@/runtime/host-runtime'
import { ExplorerSidebarAnimationProvider } from '@/contexts/explorer-sidebar-animation-context'
import { usePanelStore, type ExplorerCheckoutContext } from '@/stores/panel-store'
import { MAX_CONTENT_WIDTH } from '@/constants/layout'
@@ -139,8 +138,13 @@ function DraftAgentScreenContent({
const { theme } = useUnistyles()
const router = useRouter()
const insets = useSafeAreaInsets()
const { connectionStates } = useDaemonConnections()
const { daemons } = useDaemonRegistry()
const runtime = getHostRuntimeStore()
const runtimeVersion = useSyncExternalStore(
(onStoreChange) => runtime.subscribeAll(onStoreChange),
() => runtime.getVersion(),
() => runtime.getVersion()
)
const params = useLocalSearchParams<DraftAgentParams>()
const { height: keyboardHeight } = useReanimatedKeyboardAnimation()
@@ -174,11 +178,11 @@ function DraftAgentScreenContent({
if (daemons.length === 0) return []
const out: string[] = []
for (const daemon of daemons) {
const status = connectionStates.get(daemon.serverId)?.status ?? 'idle'
const status = runtime.getSnapshot(daemon.serverId)?.connectionStatus ?? 'connecting'
if (status === 'online') out.push(daemon.serverId)
}
return out
}, [connectionStates, daemons])
}, [daemons, runtime, runtimeVersion])
const initialValues = useMemo((): CreateAgentInitialValues => {
const values: CreateAgentInitialValues = {}
@@ -228,7 +232,6 @@ function DraftAgentScreenContent({
isCreateFlow: true,
onlineServerIds,
})
const hostEntry = selectedServerId ? connectionStates.get(selectedServerId) : undefined
const isMobile = UnistylesRuntime.breakpoint === 'xs' || UnistylesRuntime.breakpoint === 'sm'
const mobileView = usePanelStore((state) => state.mobileView)
const desktopFileExplorerOpen = usePanelStore((state) => state.desktop.fileExplorerOpen)
@@ -5,7 +5,7 @@ import { useRouter } from "expo-router";
import { StyleSheet, useUnistyles } from "react-native-unistyles";
import { BackHeader } from "@/components/headers/back-header";
import { useSessionDirectory } from "@/hooks/use-session-directory";
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
import { useDaemonRegistry } from "@/contexts/daemon-registry-context";
import type { Agent } from "@/contexts/session-context";
import {
buildHostAgentDetailRoute,
@@ -21,7 +21,7 @@ type AgentMatch = {
export function LegacyAgentIdScreen({ agentId }: { agentId: string }) {
const router = useRouter();
const { theme } = useUnistyles();
const { connectionStates } = useDaemonConnections();
const { daemons } = useDaemonRegistry();
const sessionDirectory = useSessionDirectory();
const resolvedAgentId = typeof agentId === "string" ? agentId.trim() : undefined;
@@ -39,12 +39,13 @@ export function LegacyAgentIdScreen({ agentId }: { agentId: string }) {
if (!agent) {
return;
}
const serverLabel = connectionStates.get(serverId)?.daemon.label ?? serverId;
const serverLabel =
daemons.find((daemon) => daemon.serverId === serverId)?.label ?? serverId;
results.push({ serverId, serverLabel, agent });
});
return results;
}, [resolvedAgentId, sessionDirectory, connectionStates]);
}, [daemons, resolvedAgentId, sessionDirectory]);
const hasSessions = sessionDirectory.size > 0;
const isRedirecting = Boolean(resolvedAgentId && matches.length === 1);