Remove agent directory syncing, make it pure derived state
Problem: Agent directory was still being synced from SessionProvider to Zustand store, violating the "SessionProvider is just a message handler" principle and creating unnecessary state duplication. Solution: Make agent directory pure derived state: - Removed agentDirectory from SessionStoreState - Removed setAgentDirectory/clearAgentDirectory actions - Changed getAgentDirectory to compute on-demand from session.agents - Removed buildAgentDirectoryEntries and syncing from SessionProvider - Updated useAggregatedAgents to derive from sessions directly How it works now: - Agent directory is computed on-demand from session.agents Map - lastActivityAt automatically updates when agents update via WebSocket - No syncing, no stale state, no overhead - Single source of truth: session.agents Benefits: - Eliminated redundant state (agentDirectory was duplicate of agents) - No sync overhead or complexity - Always fresh data (derived on read) - Simpler mental model (agents is the only source) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -15,21 +15,27 @@ export interface AggregatedAgentsResult {
|
||||
|
||||
export function useAggregatedAgents(): AggregatedAgentsResult {
|
||||
const { connectionStates } = useDaemonConnections();
|
||||
const agentDirectory = useSessionStore((state) => state.agentDirectory);
|
||||
const sessions = useSessionStore((state) => state.sessions);
|
||||
|
||||
return useMemo(() => {
|
||||
const allAgents: AggregatedAgent[] = [];
|
||||
|
||||
for (const [serverId, agents] of Object.entries(agentDirectory)) {
|
||||
if (!agents || agents.length === 0) {
|
||||
// Derive agent directory from all sessions
|
||||
for (const [serverId, session] of Object.entries(sessions)) {
|
||||
if (!session?.agents || session.agents.size === 0) {
|
||||
continue;
|
||||
}
|
||||
const serverLabel = connectionStates.get(serverId)?.daemon.label ?? serverId;
|
||||
for (const agent of agents) {
|
||||
for (const agent of session.agents.values()) {
|
||||
allAgents.push({
|
||||
...agent,
|
||||
id: agent.id,
|
||||
serverId,
|
||||
serverLabel,
|
||||
title: agent.title ?? null,
|
||||
status: agent.status,
|
||||
lastActivityAt: agent.lastActivityAt,
|
||||
cwd: agent.cwd,
|
||||
provider: agent.provider,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -61,5 +67,5 @@ export function useAggregatedAgents(): AggregatedAgentsResult {
|
||||
});
|
||||
|
||||
return { agents: allAgents, isLoading };
|
||||
}, [agentDirectory, connectionStates]);
|
||||
}, [sessions, connectionStates]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user