Files
apes-sidebar/src/client/subagent-catalog.ts
T
Menghuan1918 a454cfc9fe refactor(tasks): 移植主 Agent 同款合并活动折叠(数据层)
把上一轮验证过的数据层搬到工作图基座上,图/树两种模式共用:

- 新增 src/process-activity.ts:宿主 ui-chat 的 process group 算法移植
  (activity() 分类表逐条照抄、liveToolDetail 键优先级与 160 字截断、
  counts 计数排序与并列规则、preparing 语义、按 user/reply 边界切范围)。
  按本轮设计裁剪掉逐阶段列表(阶段改由徽标 + 颜色表达)。新增一条
  偏差:最新范围为空(子代理刚回复完)时回退到上一个「做过事」的范围,
  否则已结算卡片的小条只会显示「已完成分析」。
- chatT:从宿主 chat 命名空间读 message.stepProcess.* 措辞(方法调用 +
  try/catch 兜底,key 未命中即回退),4 个回归用例钉住「不许解绑 bind」。
- subagents.live 载荷换新:SidebarChildLiveView { running?, text?, summary?,
  lastEventTime? },报告树内每个子节点(含已结算)与拓扑根;删除
  expand/stages 机制;childActivity → childLive(「不在 map 里 = 未运行」
  的读法随旧协议一起作废)。
- tasks-model 消费新形状;LiveLine 与节点详情浮窗改显示合并活动措辞。
- 删除 src/subagent-activity.ts 与 tests/subagent-activity.spec.ts。

验证:typecheck / lint 通过;pnpm test 1252 passed / 9 skipped。

(cherry picked from commit 5e5284f214846b0c366048acbee459f3a7a79456)
2026-09-27 22:18:35 +08:00

108 lines
4.5 KiB
TypeScript

/**
* The plugin's view of DSH 0.1.7's host-computed session projections — the
* subagent catalog the Tasks page's topology renders, plus the two facts the
* projection row does not carry.
*
* 0.1.6 published a per-parent lazy catalog through the sessions snapshot
* (`subagentsByParent`, entries carrying `activity` / `hasChildren`) and the
* page had to OBSERVE every parent it drew, releasing them when it hid. 0.1.7
* replaced that whole handshake: the client loads every session's projections
* once per connection, `SessionListState.projectionsBySession` publishes the
* finished values, and a row is exactly `{ id, createdAt, mode, label? }`
* (see the subagent catalog projection's `viewSchema`). So this module is a
* pure adapter — no request, no subscription, no per-parent bookkeeping.
*
* The two dropped facts are derived, not invented:
*
* - a row HAS CHILDREN unless the child's own catalog is known-empty
* ({@link isKnownLeaf} — upstream's own disclosure rule, which keeps a
* branch open while its catalog is still loading instead of flapping it
* into a leaf);
* - `activity` comes from the live channel (`subagents.live`, which folds the
* host's RUNNING children): a child the live map does not mention is not
* running ({@link childActivity}).
*
* What the projection genuinely does not carry any more is the 0.1.6
* `diagnostic` row (a child whose descriptor was corrupt / unsupported /
* unavailable): the host's fold publishes healthy identities only, so an
* unreadable child is not a row at all and the page has nothing to show for
* it. See the module report — no local equivalent exists.
*/
import type {
SidebarSessionList,
SidebarSubagentCatalogEntry,
} from '../context-types.ts'
import type { SidebarChildLiveView } from '../context-types.ts'
/** One parent's direct-child catalog, as the topology consumes it. */
export interface SubagentCatalogView {
entries: readonly SidebarSubagentCatalogEntry[]
state: 'loading' | 'ready' | 'error'
error: { code?: string; message?: string } | null
}
/**
* Fold the snapshot's projection values into per-parent catalog views.
*
* An `idle` row has been published but not read yet (the manager seeds rows
* from the persisted checkpoint before the connection's read lands): it is
* `loading` when it carries no value at all and `ready` when a value is
* already there — the same reading upstream's own catalog view uses.
*
* @param projections - `SessionListState.projectionsBySession` (optional: a
* host without the projection store leaves every catalog empty).
* @returns one view per session the snapshot knows about.
*/
export function subagentCatalogs(
projections: SidebarSessionList['projectionsBySession'],
): Readonly<Record<string, SubagentCatalogView>> {
const views: Record<string, SubagentCatalogView> = {}
for (const [sessionId, snapshot] of Object.entries(projections ?? {})) {
const entries = snapshot.values.subagentCatalog
views[sessionId] = {
entries: entries ?? [],
state: snapshot.state === 'idle'
? entries === undefined ? 'loading' : 'ready'
: snapshot.state,
error: snapshot.error ?? null,
}
}
return views
}
/**
* Whether a child is a KNOWN LEAF: its own catalog finished loading and holds
* no children. Everything else (a catalog still loading, one that failed, or
* one this snapshot does not carry yet) keeps its disclosure — the topology
* then draws the child's own level, whose rows hydrate when the projection
* lands.
*
* @param catalogs - the views of one snapshot ({@link subagentCatalogs}).
* @param childSessionId - the child row whose own catalog decides.
*/
export function isKnownLeaf(
catalogs: Readonly<Record<string, SubagentCatalogView | undefined>>,
childSessionId: string,
): boolean {
const catalog = catalogs[childSessionId]
return catalog?.state === 'ready' && catalog.entries.length === 0
}
/**
* One child's live view, or undefined when the batch has not reported it yet
* (the first poll of a freshly rooted tree).
*
* The `subagents.live` route reports EVERY tree child with an explicit
* `running` flag — the previous "absent means not running" reading cannot
* survive a route that also reports settled children's summaries.
*
* @param live - the batch live map (`session id → live view`).
* @param childSessionId - the child row being drawn.
*/
export function childLive(
live: Readonly<Record<string, SidebarChildLiveView | undefined>>,
childSessionId: string,
): SidebarChildLiveView | undefined {
return live[childSessionId]
}