feat(web): parse and apply pushed workspace_update frames

The workspace list only changes when a query refetches — a new workspace, a name
change or a session count from another browser waits for a poll, and the socket
carries the same information unused.

`parseWorkspaceUpdate` narrows a `workspace_update` frame defensively (hostile
or half-filled payloads return null rather than throwing), and
`applyWorkspaceUpdate` merges one row into the `["workspaces"]` and
`["chat-workspaces"]` caches, upserts the zustand store and invalidates
`["health"]` so the status page re-reads instance state.

Not wired yet: `src/instances/connection.ts` still drops `workspace_update` on
the floor, so the daemon never relays it to a browser and the module has no
caller. This lands the validated reducer and its tests so the daemon-side relay
has something to plug into; the queries.ts fallback (poll only while the socket
is down) covers the gap meanwhile.
This commit is contained in:
yuez committed 2026-10-28 08:14:44 +08:00
1 parent c08e0c7e94
commit 7f9ce543d2
2 files changed
+193

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import type { QueryClient } from "@tanstack/react-query";
import type { WorkspaceInfo } from "@/lib/protocol";
import type { WsFrame } from "@/lib/ws";
import { useWorkspacesStore } from "@/stores/workspaces";
/**
* Validate and narrow a pushed `workspace_update` WS frame.
* Never throws on hostile input (null, malformed envelopes, wrong types).
*/
export function parseWorkspaceUpdate(frame: WsFrame): WorkspaceInfo | null {
if (!frame || typeof frame !== "object") return null;
try {
const payload = frame.payload;
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null;
const record = payload as Record<string, unknown>;
if (typeof record.workspaceId !== "string" || record.workspaceId.trim() === "") {
return null;
}
if (typeof record.cwd !== "string") {
return null;
}
return payload as unknown as WorkspaceInfo;
} catch {
return null;
}
}
function mergeRows(list: WorkspaceInfo[], info: WorkspaceInfo): WorkspaceInfo[] {
const index = list.findIndex((w) => w.workspaceId === info.workspaceId);
if (index === -1) return [...list, info];
const next = list.slice();
next[index] = info;
return next;
}
/**
* Apply a live workspace update:
* - replaces or appends in the ['workspaces'] cache
* - replaces or appends in ['chat-workspaces'] cache only if already cached
* - upserts in the zustand useWorkspacesStore
* - invalidates ['health'] so the status page reflects instance state
*/
export function applyWorkspaceUpdate(queryClient: QueryClient, info: WorkspaceInfo): void {
queryClient.setQueryData<WorkspaceInfo[]>(["workspaces"], (old) =>
Array.isArray(old) ? mergeRows(old, info) : [info],
);
queryClient.setQueryData<WorkspaceInfo[]>(["chat-workspaces"], (old) =>
Array.isArray(old) ? mergeRows(old, info) : old,
);
useWorkspacesStore.getState().upsert(info);
void queryClient.invalidateQueries({ queryKey: ["health"] });
}