feat: extract lightweight agent directory (Phase 1)

This commit is contained in:
Mohamed Boudra
2025-12-02 09:28:52 +00:00
parent 34e0e21657
commit b278b43fac
4 changed files with 126 additions and 22 deletions
+77 -3
View File
@@ -1,5 +1,6 @@
import { useSyncExternalStore } from "react";
import type { SessionContextValue } from "@/contexts/session-context";
import type { AgentDirectoryEntry } from "@/types/agent-directory";
import { isPerfLoggingEnabled, measurePayload, perfLog } from "@/utils/perf";
// SessionData mirrors SessionContextValue so consumers can subscribe to a single source of truth.
@@ -7,6 +8,7 @@ export type SessionData = SessionContextValue;
interface SessionStoreState {
sessions: Record<string, SessionData>;
agentDirectory: Record<string, AgentDirectoryEntry[]>;
}
interface SessionStore extends SessionStoreState {
@@ -14,11 +16,14 @@ interface SessionStore extends SessionStoreState {
updateSession: (serverId: string, partial: Partial<SessionData>) => void;
clearSession: (serverId: string) => void;
getSession: (serverId: string) => SessionData | undefined;
setAgentDirectory: (serverId: string, agents: AgentDirectoryEntry[]) => void;
clearAgentDirectory: (serverId: string) => void;
getAgentDirectory: (serverId: string) => AgentDirectoryEntry[] | undefined;
}
type SessionListener = () => void;
let storeState: SessionStoreState = { sessions: {} };
let storeState: SessionStoreState = { sessions: {}, agentDirectory: {} };
const listeners = new Set<SessionListener>();
const SESSION_STORE_LOG_TAG = "[SessionStore]";
let sessionStoreUpdateCount = 0;
@@ -30,7 +35,7 @@ const emit = () => {
};
const logSessionStoreUpdate = (
type: "setSession" | "updateSession" | "clearSession",
type: "setSession" | "updateSession" | "clearSession" | "setAgentDirectory" | "clearAgentDirectory",
serverId: string,
payload?: unknown
) => {
@@ -65,6 +70,7 @@ const setSession: SessionStore["setSession"] = (serverId, data) => {
}
logSessionStoreUpdate("setSession", serverId, data);
return {
...prev,
sessions: {
...prev.sessions,
[serverId]: data,
@@ -92,6 +98,7 @@ const updateSession: SessionStore["updateSession"] = (serverId, partial) => {
logSessionStoreUpdate("updateSession", serverId, partial);
return {
...prev,
sessions: {
...prev.sessions,
[serverId]: next,
@@ -108,7 +115,7 @@ const clearSession: SessionStore["clearSession"] = (serverId) => {
logSessionStoreUpdate("clearSession", serverId);
const nextSessions = { ...prev.sessions };
delete nextSessions[serverId];
return { sessions: nextSessions };
return { ...prev, sessions: nextSessions };
});
};
@@ -116,6 +123,39 @@ const getSession: SessionStore["getSession"] = (serverId) => {
return storeState.sessions[serverId];
};
const setAgentDirectory: SessionStore["setAgentDirectory"] = (serverId, agents) => {
updateStoreState((prev) => {
const existing = prev.agentDirectory[serverId];
if (existing && areAgentDirectoriesEqual(existing, agents)) {
return prev;
}
logSessionStoreUpdate("setAgentDirectory", serverId, { agentCount: agents.length });
return {
...prev,
agentDirectory: {
...prev.agentDirectory,
[serverId]: agents,
},
};
});
};
const clearAgentDirectory: SessionStore["clearAgentDirectory"] = (serverId) => {
updateStoreState((prev) => {
if (!(serverId in prev.agentDirectory)) {
return prev;
}
logSessionStoreUpdate("clearAgentDirectory", serverId);
const nextDirectory = { ...prev.agentDirectory };
delete nextDirectory[serverId];
return { ...prev, agentDirectory: nextDirectory };
});
};
const getAgentDirectory: SessionStore["getAgentDirectory"] = (serverId) => {
return storeState.agentDirectory[serverId];
};
const subscribe = (listener: SessionListener) => {
listeners.add(listener);
return () => {
@@ -125,10 +165,14 @@ const subscribe = (listener: SessionListener) => {
const buildSnapshot = (): SessionStore => ({
sessions: storeState.sessions,
agentDirectory: storeState.agentDirectory,
setSession,
updateSession,
clearSession,
getSession,
setAgentDirectory,
clearAgentDirectory,
getAgentDirectory,
});
const shallowEqual = (left: SessionData, right: SessionData): boolean => {
@@ -155,3 +199,33 @@ export function useSessionStore<T>(selector: (state: SessionStore) => T): T {
() => selector(buildSnapshot())
);
}
function areAgentDirectoriesEqual(
left: AgentDirectoryEntry[] | undefined,
right: AgentDirectoryEntry[]
): boolean {
if (!left) {
return false;
}
if (left.length !== right.length) {
return false;
}
const leftById = new Map(left.map((entry) => [entry.id, entry]));
for (const entry of right) {
const previous = leftById.get(entry.id);
if (!previous) {
return false;
}
if (
previous.serverId !== entry.serverId ||
previous.title !== entry.title ||
previous.status !== entry.status ||
previous.provider !== entry.provider ||
previous.cwd !== entry.cwd ||
previous.lastActivityAt.getTime() !== entry.lastActivityAt.getTime()
) {
return false;
}
}
return true;
}