Files
paseo/packages/app/src/subagents/select.ts
T
Mohamed Boudra 3eda7dd15b fix(app): subagents track display + archive flicker
- Bucket finished subagents as `done` instead of `attention`; drop
  "needs attention" segment from the section header — subagents are
  transient and don't carry an unread state worth surfacing.
- Gate the subagents selector on the existing pending-archive query so
  optimistic archives survive authoritative agent snapshots that wipe
  the local `archivedAt`. Last subagent archived now cleanly hides the
  whole track via the existing empty-rows guard.
2026-05-09 16:28:09 +07:00

73 lines
1.9 KiB
TypeScript

import { usePendingArchiveAgentIds } from "@/hooks/use-archive-agent";
import equal from "fast-deep-equal";
import { useStoreWithEqualityFn } from "zustand/traditional";
import { useSessionStore, type Agent } from "@/stores/session-store";
export interface SubagentRow {
id: Agent["id"];
provider: Agent["provider"];
title: Agent["title"];
status: Agent["status"];
requiresAttention: Agent["requiresAttention"];
createdAt: Agent["createdAt"];
}
type SessionStoreSnapshot = ReturnType<typeof useSessionStore.getState>;
interface SelectSubagentsParams {
serverId: string;
parentAgentId: string;
}
const EMPTY_SUBAGENT_ROWS: SubagentRow[] = [];
function toSubagentRow(agent: Agent): SubagentRow {
return {
id: agent.id,
provider: agent.provider,
title: agent.title,
status: agent.status,
requiresAttention: agent.requiresAttention,
createdAt: agent.createdAt,
};
}
export function selectSubagentsForParent(
state: SessionStoreSnapshot,
params: SelectSubagentsParams,
pendingArchiveIds: ReadonlySet<string>,
): SubagentRow[] {
const agents = state.sessions[params.serverId]?.agents;
if (!agents || agents.size === 0) {
return EMPTY_SUBAGENT_ROWS;
}
const rows: SubagentRow[] = [];
for (const agent of agents.values()) {
if (
agent.archivedAt ||
pendingArchiveIds.has(agent.id) ||
agent.parentAgentId !== params.parentAgentId
) {
continue;
}
rows.push(toSubagentRow(agent));
}
if (rows.length === 0) {
return EMPTY_SUBAGENT_ROWS;
}
rows.sort((left, right) => left.createdAt.getTime() - right.createdAt.getTime());
return rows;
}
export function useSubagentsForParent(params: SelectSubagentsParams): SubagentRow[] {
const pendingArchiveIds = usePendingArchiveAgentIds(params.serverId);
return useStoreWithEqualityFn(
useSessionStore,
(state) => selectSubagentsForParent(state, params, pendingArchiveIds),
equal,
);
}