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)
This commit is contained in:
Menghuan1918
2026-09-27 22:18:35 +08:00
parent 2a39a0c8b2
commit a454cfc9fe
21 changed files with 1198 additions and 338 deletions
+3 -3
View File
@@ -34,6 +34,7 @@ import {
} from '@deepseek-ai/dsh-client-ui-primitives'
import type {
Context,
SidebarChildLiveView,
SidebarJobView,
SidebarSessionList,
SidebarSubagentAddress,
@@ -45,7 +46,6 @@ import {
} from './subagent-detect.ts'
import { subagentCatalogs } from './subagent-catalog.ts'
import { treeSessionIds } from './subagent-lineage.ts'
import { type LastActivity } from '../subagent-activity.ts'
import { collectTreeJobs, orderJobs } from './subagent-jobs.ts'
import { api, type TeamsViewResult } from './api.ts'
import { usePolling } from './use-polling.ts'
@@ -93,8 +93,8 @@ interface CatalogRefreshFace {
function useSubagentLive(
rootId: string | undefined,
active: boolean,
): Readonly<Record<string, LastActivity>> {
const [live, setLive] = useState<Record<string, LastActivity>>({})
): Readonly<Record<string, SidebarChildLiveView>> {
const [live, setLive] = useState<Record<string, SidebarChildLiveView>>({})
// A new tree must never inherit another root's live previews.
useEffect(() => { setLive({}) }, [rootId])
+4 -3
View File
@@ -32,6 +32,7 @@ import type { TasksAgentNode, TasksWorkflowNode } from './tasks-model.ts'
import {
AgentGlyph, WorkflowGlyph, flatten, modeLabel, nodeDotState, taskDotState, taskStatusKey, workflowStatusKey,
} from './tasks-shared.tsx'
import { liveActivityLabel } from './process-labels.ts'
import { t, type CopyKey } from './locales.ts'
import css from './tasks-graph.module.css'
@@ -81,9 +82,9 @@ export function AgentNodePopover(props: {
/** The row's mode word; `unknown` claims no mode and this row is omitted. */
const nodeMode = modeLabel(node.mode)
const liveText = node.live?.text !== undefined ? flatten(node.live.text) : undefined
const liveTool = node.live?.tool !== undefined
? `${node.live.tool.name}${node.live.tool.args === '' ? '' : ` ${node.live.tool.args}`}`
: undefined
// The merged activity the main conversation would show for this node's
// newest range (category wording + the running call's detail).
const liveTool = liveActivityLabel(node.live?.summary)
return (
<div className={css.popCard}>
<div className={css.popHead}>
+5 -4
View File
@@ -7,10 +7,10 @@
* request). Failures surface as {@link SidebarApiError} with the wire code.
*/
import { encodeHtmlUrl } from '../html-route.ts'
import type { LastActivity } from '../subagent-activity.ts'
import type { SidechatLiveEvent, SidechatLogEvent, SidechatThreadInfo } from '../sidechat-core.ts'
import type {
SidebarCreateTeamTaskRequest,
SidebarChildLiveView,
SidebarJobView,
SidebarSessionEvent,
SidebarTeamMemberView,
@@ -119,8 +119,8 @@ export interface JobOutputResult {
read: boolean
}
/** The `subagents.live` response: running child id → latest activity. */
export type SubagentLiveResult = { live: Record<string, LastActivity> }
/** The `subagents.live` response: one row per tree child (and the root). */
export type SubagentLiveResult = { live: Record<string, SidebarChildLiveView> }
/** The `workflows.list` response: the tree's folded workflow runs. */
export type WorkflowsListResult = { runs: WorkflowRunView[] }
@@ -375,7 +375,8 @@ export const api = {
/**
* One batch live-preview fetch for the whole Subagent tree. The payload is
* the already-resolved topology ROOT (not a session scope); the host
* enumerates descendants once and folds running children's activity.
* enumerates descendants once and folds every child's newest process range
* into the main agent's merged-activity summary.
*/
subagentsLive: (rootSessionId: string, signal?: AbortSignal) =>
call<SubagentLiveResult>('subagents.live', { rootSessionId }, signal),
+52 -2
View File
@@ -976,7 +976,17 @@ export const LOCALE_NS = 'betterSidebar'
// path most sessions never enter.
/** The DSH locale service attached by the client apply (absent → browser detection). */
let localeService: { getSnapshot(): { active: string } } | undefined
let localeService: {
getSnapshot(): { active: string }
/**
* Bind one registered namespace to a translate function (DSH's
* `LocaleRuntime.bind`). Optional because a consumer may attach only the
* snapshot face. The untyped overload is what makes cross-package key reads
* possible: an UNREGISTERED namespace yields a function that returns the key
* itself, which is exactly how {@link chatT} detects "not available".
*/
bind?(ns: string): (key: string, params?: Record<string, string | number>) => string
} | undefined
/**
* The better-locale override store attached by the client apply
@@ -1003,10 +1013,50 @@ let betterLocaleStore: {
* `t()` function, and the Sidebar root's locale subscription re-renders the
* whole tree on switches.
*/
export function attachLocale(service: { getSnapshot(): { active: string } } | undefined): void {
export function attachLocale(service: {
getSnapshot(): { active: string }
/** Optional namespace binding ({@link chatT} needs it; a bare snapshot face is still valid). */
bind?(ns: string): (key: string, params?: Record<string, string | number>) => string
} | undefined): void {
localeService = service
}
/**
* Translate a key in the HOST's own `chat` namespace — the wording the main
* conversation's merged "process" header uses (`message.stepProcess.*`,
* registered by `@deepseek-ai/dsh-client-ui-chat`). The Tasks page reads it so
* a running node reads exactly like the main agent's own activity line instead
* of inventing a second vocabulary (and without adding ~40 keys to 20 plugin
* dictionaries).
*
* Deliberately untyped: these keys are NOT a documented host contract. A host
* that renames one, or a deployment where the chat target never mounted,
* returns the key unchanged — the caller then falls back to its own wording
* ({@link chatT} answers undefined) rather than printing `message.…`.
*
* @param key - full `chat`-namespace key.
* @param params - `{name}` placeholders to interpolate (the host does this).
* @returns the translated text, or undefined when unavailable.
*/
export function chatT(key: string, params?: Record<string, string | number>): string | undefined {
const service = localeService
if (service === undefined) return undefined
try {
// MUST stay a METHOD call: DSH's `bind` reads `this.bound`, so a detached
// reference (`const bind = service.bind; bind('chat')`) throws
// "Cannot read properties of undefined (reading 'bound')" — caught by the
// real-host mount lane, invisible to closure fakes in unit tests.
const translate = service.bind?.('chat')
if (typeof translate !== 'function') return undefined
const text = translate(key, params)
return typeof text === 'string' && text !== '' && text !== key ? text : undefined
} catch {
// The namespace seam is undocumented: a host that reshapes it must degrade
// to the caller's own wording, never break a render.
return undefined
}
}
/**
* Attach (or detach, with undefined) the better-locale override store.
* When attached with an active override, `t()` consults the store's
+115
View File
@@ -0,0 +1,115 @@
/**
* The two labels the Tasks page borrows from the main conversation's merged
* activity display: the LIVE line ("正在运行命令 · npm run build") and the
* DONE title for a settled range ("已读取文件、执行了命令").
*
* Both are ports of the host's own composition
* (`dsh-client-ui-chat`'s `ProcessGroupHeader` + `step-process.js`
* `processTitle`), reading the host's `chat` dictionary through
* {@link chatT} so the wording — and every language the host ships — matches
* what the main chat shows for the same work.
*
* The translator is injectable for tests; the default is the host bridge. When
* a key does not resolve (a host that renamed it, or a deployment where the
* chat target never mounted) the call answers undefined and the card falls
* back to the tool detail it already has, never to a raw `message.…` key.
*/
import type { ProcessActivitySummary } from '../process-activity.ts'
import { chatT } from './locales.ts'
/** A host-namespace translator; undefined means "this key is not available". */
export type ChatTranslate = (key: string, params?: Record<string, string | number>) => string | undefined
/** The host's `' · '` line separator (`message.turnProcess.separator`). */
function separator(t: ChatTranslate): string {
return t('message.turnProcess.separator') ?? ' · '
}
/**
* The live line of one stage: the running category's `…ing` wording plus the
* running call's detail, or the "thinking" default while nothing is dispatched
* (exactly the host's `ProcessGroupHeader` label rule).
*
* @param summary - the newest stage's summary (absent while the child has none).
* @param t - host translator (defaults to the `chat` namespace bridge).
* @returns the line, or undefined when there is nothing to say.
*/
export function liveActivityLabel(
summary: ProcessActivitySummary | undefined,
t: ChatTranslate = chatT,
): string | undefined {
const activity = summary?.running ?? 'thinking'
const preparing = summary?.preparing === true
// The host maps its `thinking` fallback onto `prepare.tools` (there is no
// `prepare.thinking` key).
const label = preparing
? t(`message.stepProcess.prepare.${activity === 'thinking' ? 'tools' : activity}`)
: t(`message.stepProcess.${activity}`)
if (label === undefined) {
// No host wording for this category: the detail is the honest fallback.
return summary?.runningDetail === undefined || summary.runningDetail === ''
? undefined
: summary.runningDetail
}
const detail = preparing ? '' : summary?.runningDetail ?? ''
return detail === '' ? label : `${label}${separator(t)}${detail}`
}
/**
* The last-resort activity text: the ranked categories spelled out as
* `kind ×count`. It keeps a card informative when the host's `chat` wording is
* unavailable (renamed keys, or a deployment where the chat target never
* mounted) without inventing a second vocabulary for the normal case — the
* category names are the host's own, and they are language-neutral.
*
* @param summary - a stage's summary.
* @returns the counts text, or undefined when the stage called nothing.
*/
export function activityCountsText(
summary: ProcessActivitySummary | undefined,
): string | undefined {
const counts = summary?.counts ?? []
if (counts.length === 0) return undefined
return counts.map(entry => `${entry.kind} ×${entry.count}`).join(' · ')
}
/**
* The settled range's title: the top three categories' `done.*` wording joined
* the way the host joins it (two labels share the "已" prefix; more than three
* append the "等" suffix). Counts are never printed.
*
* @param summary - a stage's summary.
* @param t - host translator (defaults to the `chat` namespace bridge).
* @returns the title, or undefined when the host wording is unavailable.
*/
export function doneActivityTitle(
summary: ProcessActivitySummary | undefined,
t: ChatTranslate = chatT,
): string | undefined {
const counts = summary?.counts ?? []
if (counts.length === 0) return t('message.stepProcess.done.thinking')
const labels: string[] = []
for (const { kind } of counts.slice(0, 3)) {
const label = t(`message.stepProcess.done.${kind}`)
if (label === undefined) return undefined
labels.push(label)
}
const first = labels[0]
if (first === undefined) return undefined
const second = labels[1]
if (second === undefined) return first
// The host lowercases every label after the first for latin languages; a
// Chinese label is unaffected by that.
const continuation = (label: string): string => label.charAt(0).toLowerCase() + label.slice(1)
if (labels.length === 2) {
const prefix = t('message.stepProcess.sharedPrefix') ?? ''
const stripped = prefix !== '' && first.startsWith(prefix) && second.startsWith(prefix)
? second.slice(prefix.length)
: second
return t('message.stepProcess.joinTwo', { first, second: continuation(stripped) })
?? `${first}${separator(t)}${continuation(stripped)}`
}
const joined = [first, ...labels.slice(1).map(continuation)].join(t('message.stepProcess.comma') ?? ', ')
if (counts.length > 3) return t('message.stepProcess.more', { title: joined }) ?? joined
return joined
}
+12 -9
View File
@@ -32,7 +32,7 @@ import type {
SidebarSessionList,
SidebarSubagentCatalogEntry,
} from '../context-types.ts'
import type { LastActivity } from '../subagent-activity.ts'
import type { SidebarChildLiveView } from '../context-types.ts'
/** One parent's direct-child catalog, as the topology consumes it. */
export interface SubagentCatalogView {
@@ -89,16 +89,19 @@ export function isKnownLeaf(
}
/**
* The child's live state. The `subagents.live` route folds only RUNNING
* children (a Side Chat thread and an idle child are absent), so absence is
* the inactive answer rather than missing data.
* One child's live view, or undefined when the batch has not reported it yet
* (the first poll of a freshly rooted tree).
*
* @param live - the batch live map (`child id → latest activity`).
* 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 childActivity(
live: Readonly<Record<string, LastActivity | undefined>>,
export function childLive(
live: Readonly<Record<string, SidebarChildLiveView | undefined>>,
childSessionId: string,
): 'running' | 'inactive' {
return live[childSessionId] === undefined ? 'inactive' : 'running'
): SidebarChildLiveView | undefined {
return live[childSessionId]
}
+8 -1
View File
@@ -10,6 +10,7 @@
* the core bundle; <100-node trees need no virtualisation).
*/
import type { TasksNode } from './tasks-model.ts'
import type { SidebarChildLiveView } from '../context-types.ts'
import { tasksEdges } from './tasks-model.ts'
/** Geometry constants of the canvas (px, pre-scale). The design target is the
@@ -115,12 +116,18 @@ export interface LayoutOptions {
* The tallest shape therefore is base + live + live-tail + task, which stays
* inside {@link GRAPH_ROW_STRIDE} (rows can never overlap).
*/
/** Whether a node's live view carries a merged-activity row to render. */
function hasMergedActivity(live: SidebarChildLiveView | undefined): boolean {
if (live === undefined) return false
return (live.summary?.counts.length ?? 0) > 0 || live.summary?.running !== undefined
}
function nodeHeight(node: TasksNode): number {
if (node.kind !== 'agent') return GRAPH_NODE_H
let height = GRAPH_NODE_H
if (node.state === 'running') {
height += GRAPH_LIVE_H
if (node.live?.tool !== undefined && node.live.text !== undefined) height += GRAPH_LIVE_TAIL_H
if (hasMergedActivity(node.live) && node.live?.text !== undefined) height += GRAPH_LIVE_TAIL_H
}
if ((node.tasks?.length ?? 0) > 0) height += GRAPH_TASK_H
return height
+10 -9
View File
@@ -26,10 +26,10 @@ import type {
SidebarTeamMemberView,
SidebarTeamTaskView,
} from '../context-types.ts'
import type { LastActivity } from '../subagent-activity.ts'
import type { SidebarChildLiveView } from '../context-types.ts'
import type { WorkflowRunView } from '../workflow-runs.ts'
import { isSideThreadSummary } from './subagent-detect.ts'
import { childActivity, isKnownLeaf, type SubagentCatalogView } from './subagent-catalog.ts'
import { childLive, isKnownLeaf, type SubagentCatalogView } from './subagent-catalog.ts'
/** Display state of one agent node (drives the dot + fold candidacy). */
export type TasksNodeState = 'running' | 'idle' | 'done' | 'error'
@@ -59,8 +59,8 @@ export interface TasksAgentNode {
activity: 'running' | 'inactive'
/** The on-screen session (the "you are here" marker). */
current: boolean
/** Live tail of a running child (the icon + tool + args line). */
live?: LastActivity
/** Live tail of a running child (the merged activity + newest text). */
live?: SidebarChildLiveView
/** Team enrichment (membership view matched by session id). */
team?: {
role: 'lead' | 'teammate'
@@ -108,7 +108,7 @@ export interface TasksModelInput {
catalogs: Readonly<Record<string, SubagentCatalogView | undefined>>
rootId: string
currentSessionId: string
live: Readonly<Record<string, LastActivity | undefined>>
live: Readonly<Record<string, SidebarChildLiveView | undefined>>
runs: readonly WorkflowRunView[]
teamMembers: readonly SidebarTeamMemberView[]
/** The team's shared tasks; each lands on its OWNER's node. */
@@ -215,9 +215,10 @@ export function buildTasksModel(input: TasksModelInput): TasksNode[] {
if (entry.label?.startsWith('Side: ') ?? false) continue
const team = teamOf(entry.id)
// DSH 0.1.7's catalog row carries the identity only: the live channel
// folds RUNNING children (absence = not running) and a row keeps its
// disclosure unless its own catalog is known-empty.
const activity = childActivity(live, entry.id)
// reports EVERY child with an explicit `running` flag, and a row keeps
// its disclosure unless its own catalog is known-empty.
const view = childLive(live, entry.id)
const activity: 'running' | 'inactive' = view?.running === true ? 'running' : 'inactive'
const state: TasksNodeState = activity === 'running'
? 'running'
: team !== undefined ? teamState(team.status) : 'done'
@@ -232,7 +233,7 @@ export function buildTasksModel(input: TasksModelInput): TasksNode[] {
state,
activity,
current: entry.id === currentSessionId,
...(live[entry.id] !== undefined ? { live: live[entry.id] } : {}),
...(view !== undefined ? { live: view } : {}),
...(team !== undefined ? { team } : {}),
...(tasksByNode.get(entry.id) !== undefined ? { tasks: tasksByNode.get(entry.id) } : {}),
hasChildren: !isKnownLeaf(catalogs, entry.id),
+12 -10
View File
@@ -16,7 +16,8 @@ import {
IconAgentPresetOutlineRegular, IconBranchOutlineRegular, IconChecklistOutlineRegular, IconUserOutlineRegular,
type StateDotState,
} from '@deepseek-ai/dsh-client-ui-primitives'
import type { LastActivity } from '../subagent-activity.ts'
import type { SidebarChildLiveView } from '../context-types.ts'
import { liveActivityLabel } from './process-labels.ts'
import type { TasksAgentNode, TasksNodeState, TasksNodeTask, TasksWorkflowNode } from './tasks-model.ts'
import { toolGlyph } from './tool-icons.tsx'
import { t, type CopyKey } from './locales.ts'
@@ -189,25 +190,26 @@ export function TaskLine(props: {
}
/**
* The live activity row of a RUNNING agent: the tool's own icon + tool name +
* args (the approved format), plus the flattened last text line underneath.
* The live activity row of a RUNNING agent: the merged process summary the
* main conversation would show for the child's newest range (category wording
* + the running call's detail), plus the flattened last text line underneath.
* A running node with neither reads as thinking.
*/
export function LiveLine(props: { live: LastActivity | undefined }): ReactNode {
export function LiveLine(props: { live: SidebarChildLiveView | undefined }): ReactNode {
const { live } = props
if (live?.text === undefined && live?.tool === undefined) {
const activity = liveActivityLabel(live?.summary)
if (activity === undefined && live?.text === undefined) {
return <span className={css.nodeMeta}>{t('subagentThinking')}</span>
}
return (
<>
{live.tool !== undefined && (
{activity !== undefined && (
<span className={css.live}>
<span className={css.liveGlyph} aria-hidden="true">{toolGlyph(live.tool.name)(9)}</span>
<span className={css.liveTool}>{live.tool.name}</span>
{live.tool.args !== '' && <span className={css.liveArgs}>{preview(live.tool.args)}</span>}
<span className={css.liveGlyph} aria-hidden="true">{toolGlyph('bash')(9)}</span>
<span className={css.liveTool} title={activity}>{activity}</span>
</span>
)}
{live.text !== undefined && (
{live?.text !== undefined && (
<span className={css.liveText}>{preview(flatten(live.text), TEXT_PREVIEW)}</span>
)}
</>
+26
View File
@@ -30,6 +30,7 @@
*/
import type { Context as CordisContext } from '@deepseek-ai/cordis'
import type { BetterSidebarService } from './client/service.ts'
import type { ProcessActivitySummary } from './process-activity.ts'
/** The request face route handlers see (structural subset of node's
* IncomingMessage: the URL/method/header reads and the async body
@@ -173,6 +174,31 @@ export interface SidebarHistoryEntry {
/** Lifecycle status set of one background job (closed wire union). */
export type SidebarJobStatus = 'running' | 'stopping' | 'completed' | 'killed' | 'failed'
/**
* One tree child's live view as the `subagents.live` route reports it: the
* catalog's activity flag plus the fold of the child's newest process range
* (see ./process-activity.ts).
*
* `running` is the catalog's flag and is therefore always present on a CHILD
* row; the fold's fields are optional. The route used to report running
* children only, so "absent from the map" meant "not running" — a reading that
* cannot survive a route which also reports settled children's summaries.
*/
export interface SidebarChildLiveView {
/**
* The catalog's activity flag. Present on every CHILD row; ABSENT on the
* topology root, which the host cannot classify (the caller already knows
* its session's running state from the session list).
*/
running?: boolean
/** The child's newest assistant text (the card's detail line). */
text?: string
/** The newest range's merged activity; absent when that range called nothing. */
summary?: ProcessActivitySummary
/** Epoch ms of the newest event observed. */
lastEventTime?: number
}
/**
* One background job as the client mirror sees it (wire `JobView` shape:
* id/kind/label/status/detail?/startedAt/finishedAt?).
+394
View File
@@ -0,0 +1,394 @@
/**
* The plugin's port of the MAIN AGENT's merged "process" display: the compact
* activity summary DSH's chat shows for a range of tool calls.
*
* The host computes that summary over materialized Chat nodes inside
* `@deepseek-ai/dsh-client-ui-chat` (`conversation-nodes/process-activity.js`
* → `activity()` / `liveToolDetail()` / `processActivity()`, rendered by
* `ProcessGroupHeader`). None of it is exported — the client bundle's only
* runtime exports are `apply`/`inject`/`isRunningTool`/`isSettledTool` — so a
* surface that has only a SESSION EVENT LOG (a subagent's, which the chat view
* never materializes) must reproduce the algorithm. This module is that
* reproduction, kept framework-free so the host route and the node test
* environment share one implementation.
*
* Faithful points, all deliberate:
*
* - the tool NAME → category map is copied verbatim (including the
* `terminal_` prefix rule and the `*_inspect` suffix rule);
* - `counts` ranks by distinct-call count descending, ties broken by first
* appearance (the host relies on `Array.sort` stability + Map insertion
* order);
* - the running tool is the LAST unsettled call, and a "preparing" call (one
* the model announced but never dispatched) counts and can be the running
* one, exactly like the host's `PreparingToolCall` branch;
* - `runningDetail` walks the host's key list in order and truncates the same
* way;
* - the range is cut where the host's process GROUPS are cut: at a
* `user/message` and at every assistant message that carries text
* (`TurnGroups.rebuild`'s `INDEPENDENT` set plus its `reply(node)` flush).
*
* Deliberate deviations, all forced by the input:
*
* - the host's `runningDetail` falls back to the last REASONING paragraph of a
* running assistant step; a durable log carries no partial step, so the
* fallback is omitted (the label then reads as the "thinking" default);
* - the host reads `arguments` only for a dispatched call; a durable
* `assistant/message` already carries the announced block's `arguments`, but
* the preparing detail still follows the host (no detail at all) so both
* sides read the same;
* - an unsettled call at the tail of a SETTLED session is reported as not
* running ({@link foldProcess}'s `live: false`), mirroring the host's
* `closed` groups (`summary = { counts, running: undefined, runningDetail: '' }`);
* - when the NEWEST range holds no call at all (the child just replied), the
* fold reports the previous range instead — a card whose bar read "已完成分析"
* because the last thing that happened was a sentence would tell the reader
* nothing. The fallback range is never reported as running.
*
* This module deliberately does NOT build a per-STAGE list: the Tasks page
* marks workflow phases with a badge + colour instead (see tasks-model.ts), so
* only the current range's summary is derived here.
*/
import type { SidebarSessionEvent } from './context-types.ts'
/** The host's `ProcessActivity` union, verbatim. */
export type ProcessActivity =
| 'read' | 'readImage' | 'search' | 'write' | 'edit' | 'commands' | 'code'
| 'webSearch' | 'webFetch' | 'subagents' | 'plan' | 'questions' | 'tools'
/**
* The host's `ProcessActivitySummary` shape: the ranked work of one range plus
* the live task detail. `preparing` exists only while the selected live
* activity has not reached its `tool/call` event.
*/
export interface ProcessActivitySummary {
counts: { kind: ProcessActivity; count: number }[]
running?: ProcessActivity
runningDetail: string
preparing?: true
}
/** One fold result: the newest range's summary plus the newest text line. */
export interface ProcessFold {
/** What a node card's status bar shows. */
current: ProcessActivitySummary
/** The newest assembled assistant text in the window (the card's detail line). */
text?: string
/** Epoch ms of the newest event in the window. */
lastEventTime?: number
}
/** How many events one fold may scan backwards (bounds the cost per child). */
export const PROCESS_WINDOW_EVENTS = 400
/** Detail cap, mirroring the host's `LIVE_TOOL_DETAIL_MAX_CHARS`. */
export const PROCESS_DETAIL_MAX_CHARS = 160
/** Cap of the detail text line sent to the client. */
export const PROCESS_TEXT_MAX_CHARS = 400
/**
* The host's category map, verbatim (the arms are mutually exclusive, so the
* order only mirrors the host's for review).
* @param name - durable tool-call name.
* @returns the category the main agent's process group counts it under.
*/
export function activityOf(name: string): ProcessActivity {
if (name === 'read') return 'read'
if (name === 'read_image') return 'readImage'
if (name === 'grep' || name === 'glob' || name.endsWith('_inspect')) return 'search'
if (name === 'write') return 'write'
if (name === 'edit' || name === 'apply_patch') return 'edit'
if (['bash', 'pwsh', 'exec_command', 'write_stdin'].includes(name) || name.startsWith('terminal_')) {
return 'commands'
}
if (name === 'run_code') return 'code'
if (name === 'web_search') return 'webSearch'
if (name === 'web_fetch') return 'webFetch'
if (name === 'subagent' || name.startsWith('subagent_')) return 'subagents'
if (['todo_write', 'create_goal', 'update_goal', 'get_goal'].includes(name)) return 'plan'
if (name === 'ask_user_question' || name === 'request_user_input') return 'questions'
return 'tools'
}
/** The host's live-detail key priority, verbatim. */
const LIVE_TOOL_DETAIL_KEYS = [
'title', 'description', 'objective', 'task', 'task_name', 'name', 'question',
'questions', 'prompt', 'message', 'command', 'cmd', 'queries', 'query',
'pattern', 'url', 'uri', 'file_path', 'path', 'target', 'action', 'status',
] as const
/** Grapheme segmenter when the runtime has one (the host relies on `Intl.Segmenter`). */
const SEGMENTER = typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function'
? new Intl.Segmenter(undefined, { granularity: 'grapheme' })
: undefined
/** Split into user-perceived characters, falling back to code points. */
function graphemes(text: string): string[] {
if (SEGMENTER === undefined) return Array.from(text)
return Array.from(SEGMENTER.segment(text), part => part.segment)
}
/** Collapse whitespace and truncate on a grapheme boundary (the host's normalize). */
function normalizeDetail(value: unknown): string {
const flat = (typeof value === 'string'
? value
: Array.isArray(value) && value.every(item => typeof item === 'string')
? value.join(', ')
: '').replace(/\s+/g, ' ').trim()
const chars = graphemes(flat)
if (chars.length <= PROCESS_DETAIL_MAX_CHARS) return flat
return `${chars.slice(0, PROCESS_DETAIL_MAX_CHARS - 1).join('').trimEnd()}…`
}
/** The first `question` string of an `ask_user_question` argument list. */
function questionDetail(value: unknown): string {
if (!Array.isArray(value)) return ''
for (const item of value) {
if (item === null || typeof item !== 'object') continue
const detail = normalizeDetail((item as { question?: unknown }).question)
if (detail !== '') return detail
}
return ''
}
/**
* The host's `liveToolDetail`: parse the raw arguments and take the first
* present key of {@link LIVE_TOOL_DETAIL_KEYS}, falling back to the tool name.
* @param name - tool-call name.
* @param argsRaw - the call's raw `arguments` JSON string.
*/
export function liveToolDetail(name: string, argsRaw: string): string {
let args: unknown
try {
args = JSON.parse(argsRaw)
} catch {
return normalizeDetail(name)
}
if (args === null || typeof args !== 'object') return normalizeDetail(name)
const record = args as Record<string, unknown>
for (const key of LIVE_TOOL_DETAIL_KEYS) {
if (!(key in record)) continue
const value = record[key]
const detail = key === 'questions' ? questionDetail(value) : normalizeDetail(value)
if (detail !== '') return detail
}
return normalizeDetail(name)
}
/**
* Extract the concatenated plain text of a content-block list (the durable
* `ContentBlock[]` shape, structurally: blocks with `type: 'text'` carry
* `text`; anything else — a tool call, an image, … — contributes nothing).
* @param content - the raw `content` field of a message event.
* @returns the joined text, or undefined when the message carries no text.
*/
export function contentText(content: unknown): string | undefined {
if (!Array.isArray(content)) return undefined
const parts: string[] = []
for (const block of content) {
if (block === null || typeof block !== 'object') continue
const candidate = block as { type?: unknown; text?: unknown }
if (candidate.type === 'text' && typeof candidate.text === 'string') {
parts.push(candidate.text)
}
}
return parts.length > 0 ? parts.join('\n') : undefined
}
/** One announced or dispatched call inside the current range. */
interface CallRecord {
callId: string
name: string
args: string
seq: number
/** A paired `tool/result` was observed in the scanned window. */
settled: boolean
/** Announced by the model but never dispatched (the host's `PreparingToolCall`). */
preparing: boolean
}
/** A tool-call content block under BOTH durable vocabularies (0.1.7 `tool-call`; older logs `tool_use`). */
function announcedCalls(content: unknown): { callId: string; name: string; args: string }[] {
if (!Array.isArray(content)) return []
const found: { callId: string; name: string; args: string }[] = []
for (const block of content) {
if (block === null || typeof block !== 'object') continue
const candidate = block as { type?: unknown; id?: unknown; callId?: unknown; name?: unknown; arguments?: unknown }
if (candidate.type !== 'tool-call' && candidate.type !== 'tool_use') continue
const rawId = candidate.id ?? candidate.callId
if (typeof rawId !== 'string' || typeof candidate.name !== 'string') continue
found.push({
callId: rawId,
name: candidate.name,
args: typeof candidate.arguments === 'string' ? candidate.arguments : '',
})
}
return found
}
/** The callId of one `tool/result` event under both logged shapes. */
function resultCallId(data: Record<string, unknown>): string | undefined {
const message = data.message as { source?: { callId?: unknown }; toolCallId?: unknown } | undefined
for (const candidate of [data.callId, message?.source?.callId, message?.toolCallId, data.toolCallId]) {
if (typeof candidate === 'string' && candidate !== '') return candidate
}
return undefined
}
/** Rank one range's calls into the host's summary shape. */
function summarize(calls: readonly CallRecord[], live: boolean): ProcessActivitySummary {
const counts = new Map<ProcessActivity, number>()
let running: ProcessActivity | undefined
let preparing: true | undefined
let runningDetail = ''
let runningSeq = -Infinity
// The host counts in FORWARD member order, so a count tie ranks by first
// appearance; this fold collects backwards and must restore that order.
const ordered = calls.slice().sort((left, right) => left.seq - right.seq)
for (const call of ordered) {
const kind = activityOf(call.name)
counts.set(kind, (counts.get(kind) ?? 0) + 1)
if (call.settled || !live) continue
// The host selects on `>=` in event order: the newest unsettled call wins.
if (call.seq < runningSeq) continue
runningSeq = call.seq
running = kind
preparing = call.preparing ? true : undefined
runningDetail = call.preparing ? (kind === 'tools' ? call.name : '') : liveToolDetail(call.name, call.args)
}
const ranked = [...counts].map(([kind, count]) => ({ kind, count }))
ranked.sort((left, right) => right.count - left.count)
return {
counts: ranked,
...(running === undefined ? {} : { running }),
runningDetail,
...(preparing === true ? { preparing: true as const } : {}),
}
}
/** Cap one detail line without cutting a surrogate pair in half. */
function truncateText(text: string): string {
const chars = graphemes(text)
if (chars.length <= PROCESS_TEXT_MAX_CHARS) return text
return `${chars.slice(0, PROCESS_TEXT_MAX_CHARS - 1).join('').trimEnd()}…`
}
/** The calls of the range currently being collected. */
interface Range {
calls: CallRecord[]
ids: Set<string>
}
/** Start an empty range. */
function openRange(): Range {
return { calls: [], ids: new Set() }
}
/** Append one call to a range (deduped by callId). */
function addCall(range: Range, call: CallRecord): void {
if (range.ids.has(call.callId)) return
range.calls.push(call)
range.ids.add(call.callId)
}
/**
* Fold a session event log into the host's merged activity view: the newest
* range's summary and the newest text line. Scans BACKWARD from the end and
* stops at the range it reports, so a long history costs only its recent tail.
*
* @param events - the session's append-only event log (oldest → newest).
* @param options - `live` (whether the session is executing; a settled one
* reports no running tool, like the host's closed groups) and `maxEvents`.
* @returns the current summary plus the optional detail fields.
*/
export function foldProcess(
events: readonly SidebarSessionEvent[],
options: { live: boolean; maxEvents?: number },
): ProcessFold {
const live = options.live
const maxEvents = Math.max(1, options.maxEvents ?? PROCESS_WINDOW_EVENTS)
let range = openRange()
/** The newest NON-EMPTY range (the fallback when the newest one is empty). */
let answer: CallRecord[] | undefined
/** Whether {@link answer} is the newest range (only then can it be running). */
let answerLive = false
const settled = new Set<string>()
let latestText: string | undefined
let lastEventTime: number | undefined
let crossed = 0
for (let index = events.length - 1, scanned = 0; index >= 0; index -= 1) {
if (scanned >= maxEvents) break
const event = events[index]
if (event === undefined) continue
scanned += 1
lastEventTime ??= event.time
const { type, data } = event
if (type === 'tool/result') {
const callId = resultCallId(data)
if (callId !== undefined) settled.add(callId)
continue
}
if (type === 'tool/call') {
const callId = typeof data.callId === 'string' && data.callId !== '' ? data.callId : `anon-${event.seq}`
addCall(range, {
callId,
name: typeof data.name === 'string' ? data.name : 'tool',
args: typeof data.arguments === 'string' ? data.arguments : '',
seq: event.seq,
settled: settled.has(callId),
preparing: false,
})
continue
}
if (type !== 'assistant/message' && type !== 'user/message') continue
const message = type === 'assistant/message'
? (data.message as { content?: unknown } | undefined)
: undefined
if (message !== undefined) {
// Announced calls belong to the range that FOLLOWS their assistant
// message — the range being collected right now.
for (const announced of announcedCalls(message.content)) {
const isSettled = settled.has(announced.callId)
addCall(range, {
callId: announced.callId,
name: announced.name,
args: announced.args,
seq: event.seq,
settled: isSettled,
preparing: !isSettled,
})
}
}
const text = message === undefined ? undefined : contentText(message.content)
if (text !== undefined) latestText ??= text
// A text-bearing assistant message and a user message both END the range
// to their left (the host's flush points).
if (type !== 'user/message' && text === undefined) continue
if (range.calls.length > 0) {
// The range just closed is the newest one that did anything.
answer = range.calls
answerLive = crossed === 0
break
}
// Nothing happened in this range: step past it into the previous one, once.
crossed += 1
if (crossed > 1) break
range = openRange()
}
// `answer` is set when a boundary closed a range that did something; the
// range still in hand covers two ends of the log: the whole log (no boundary
// at all — that range IS the newest) and the one the scan ran out of events
// in (a fallback range, so it is never live).
const calls = answer ?? range.calls
const newest = answer === undefined ? crossed === 0 : answerLive
return {
current: summarize(calls, live && newest),
...(latestText === undefined ? {} : { text: truncateText(latestText) }),
...(lastEventTime === undefined ? {} : { lastEventTime }),
}
}
-93
View File
@@ -1,93 +0,0 @@
/**
* Pure derivation of the compact LIVE line shown on a running subagent card:
* the last text output and the last tool call of the child's session events.
* The host batch route feeds raw session events into this parser; the client
* only receives the already-folded `LastActivity` map. Renders nothing
* itself — the SubagentView component turns this into the card's status
* lines. Kept framework-free so the parser is unit-testable in the node
* environment.
*/
import type { SidebarSessionEvent } from './context-types.ts'
/**
* Extract the concatenated plain text of a content-block list (the durable
* `ContentBlock[]` shape, structurally: blocks with `type: 'text'` carry
* `text`; anything else — tool_use, image, … — contributes nothing).
* @param content - the raw `content` field of a message event.
* @returns the joined text, or undefined when the message carries no text.
*/
export function contentText(content: unknown): string | undefined {
if (!Array.isArray(content)) return undefined
const parts: string[] = []
for (const block of content) {
if (block === null || typeof block !== 'object') continue
const candidate = block as { type?: unknown; text?: unknown }
if (candidate.type === 'text' && typeof candidate.text === 'string') {
parts.push(candidate.text)
}
}
return parts.length > 0 ? parts.join('\n') : undefined
}
/** The live status of one subagent card (both fields optional). */
export interface LastActivity {
/** The latest assembled assistant text output in the tail. */
text?: string
/** The latest tool call in the tail. */
tool?: { name: string; args: string }
}
/**
* Fold a session event log into the last text output + last tool call (each
* is the LAST occurrence in event order). Lifecycle events and raw stream
* rows are ignored — the card shows what the subagent is doing right now,
* not its plumbing. (DSH 0.1.5 publishes the raw deltas as process-local
* frames instead of log events, so only the assembled `assistant/message`
* reaches this scan.) The scan runs BACKWARD from the newest
* event and stops once both fields are found, so a long history costs only
* the recent tail in the common case.
* @param events - the session's append-only event log (oldest → newest).
* @param maxMessages - optional message-boundary window: only the tail's
* last `maxMessages` surface messages (`user/message`, `assistant/message`)
* and the events between them are considered, mirroring the old
* `subagents.history({ maxMessages })` window. Stale activity older than
* the window is never surfaced, and a long log is never scanned in full.
* @returns the last text and/or tool call; an empty object when the log has neither.
*/
export function lastActivity(
events: readonly SidebarSessionEvent[],
maxMessages = Infinity,
): LastActivity {
let text: string | undefined
let tool: { name: string; args: string } | undefined
let messagesSeen = 0
for (let index = events.length - 1; index >= 0; index -= 1) {
if (text !== undefined && tool !== undefined) break
const event = events[index]
if (event === undefined) continue
const { type, data } = event
if (type === 'user/message' || type === 'assistant/message') {
messagesSeen += 1
if (messagesSeen > maxMessages) break
} else if (messagesSeen >= maxMessages) {
// The window already holds its `maxMessages` messages: anything older
// than the oldest in-window message sits outside the recent window.
continue
}
if (text === undefined && type === 'assistant/message') {
const message = data.message as { content?: unknown } | undefined
const extracted = contentText(message?.content)
if (extracted !== undefined) text = extracted
} else if (tool === undefined && type === 'tool/call') {
tool = {
name: typeof data.name === 'string' ? data.name : 'tool',
args: typeof data.arguments === 'string' ? data.arguments : '',
}
}
}
if (text === undefined && tool === undefined) return {}
return {
...(text === undefined ? {} : { text }),
...(tool === undefined ? {} : { tool }),
}
}
+51 -36
View File
@@ -4,42 +4,44 @@
*
* The route takes the already-resolved topology root (`rootSessionId`),
* enumerates the whole descendant tree ONCE through the host subagent
* runtime (`ctx.get('subagents')` / `listDescendants`), keeps only rows the
* catalog reports running (`activity: 'running'` — the same gate the client
* renders cards on), and folds the newest text/tool activity from each
* child's attached session event log. It never touches DSH source and never
* reads the model's `job_output` cursor.
* runtime (`ctx.get('subagents')` / `listDescendants`), and folds EVERY child
* session's newest process range with {@link foldProcess} — the plugin's port
* of the main agent's merged process summary (category counts + the one
* running call + its detail). It never touches DSH source and never reads the
* model's `job_output` cursor.
*
* Folding a settled child is cheap by construction: the default fold stops at
* the first range boundary it can report, so a child that is idle between
* turns costs a handful of event reads (`snapshotEvents()` itself returns the
* session's cached frozen snapshot, not a per-call copy).
*
* Degradation contract:
* - `ctx.get('subagents')` missing or `listDescendants` failure → 503 (the
* Subagent page has no topology to show in such deployments anyway).
* - One child's events missing/corrupt → that child is skipped, the rest of
* the batch still returns.
* - One child's events missing/corrupt → that child is still REPORTED (its
* `running` flag is the catalog's), just without a summary; the rest of the
* batch is unaffected.
*/
import type { Context, SidebarSubagentsService } from './context-types.ts'
import type {
Context,
SidebarChildLiveView,
SidebarSubagentsService,
} from './context-types.ts'
import { SIDE_LABEL_PREFIX } from './sidechat-core.ts'
import { lastActivity, type LastActivity } from './subagent-activity.ts'
import { foldProcess } from './process-activity.ts'
import { requireString, SidebarError } from './wire.ts'
/** The live-preview routes of the /sidebar JSON API. */
export interface SidebarSubagentLiveRoutes {
/**
* Fold one tree's running subagent histories into a compact live map.
* Fold one tree's subagent activity into a compact live map.
* @param payload - `{ rootSessionId }`.
* @returns `{ live: Record<childSessionId, LastActivity> }`; children with
* no text/tool yet are omitted.
* @returns `{ live: Record<sessionId, SidebarChildLiveView> }` over the
* whole descendant catalog plus the topology root.
*/
live(payload: unknown): Promise<{ live: Record<string, LastActivity> }>
live(payload: unknown): Promise<{ live: Record<string, SidebarChildLiveView> }>
}
/**
* The recent-message window of the live preview: only the last 12 surface
* messages of a child's log are folded, matching the old per-card
* `subagents.history({ maxMessages: 12 })` window. Keeps stale tool calls
* out of the preview and bounds the backward scan per child.
*/
export const LIVE_WINDOW_MESSAGES = 12
/**
* Build the live-preview routes bound to the plugin context.
* @param ctx - host plugin context.
@@ -67,25 +69,38 @@ export function buildSubagentLiveApi(ctx: Context): SidebarSubagentLiveRoutes {
)
}
const live: Record<string, LastActivity> = {}
const live: Record<string, SidebarChildLiveView> = {}
/**
* Fold one session into the map. `running` is the caller-visible activity
* flag: the catalog's for a child, undefined for the topology ROOT (the
* host cannot classify it, and the page already knows the answer from the
* session list). The fold's own `live` gate is that flag when it exists,
* and the LOG's evidence for the root.
*/
const foldInto = (sessionId: string, running: boolean | undefined): void => {
const view: SidebarChildLiveView = running === undefined ? {} : { running }
try {
const events = ctx.sessions.get(sessionId)?.snapshotEvents() ?? []
const fold = foldProcess(events, { live: running !== false })
if (fold.current.counts.length > 0 || fold.current.running !== undefined) {
view.summary = fold.current
}
if (fold.text !== undefined) view.text = fold.text
if (fold.lastEventTime !== undefined) view.lastEventTime = fold.lastEventTime
} catch {
// One session's event log is not readable: keep the row (a child's
// running flag is the catalog's) and skip only its details.
}
live[sessionId] = view
}
foldInto(rootSessionId, undefined)
for (const entry of descendants) {
// Same gate the client renders cards on: only catalog-running
// children get live lines (spec: "仅对 running 且非 Side Chat").
if (entry.kind !== 'child' || entry.activity !== 'running') continue
if (entry.kind !== 'child') continue
// Side Chat threads ride the subagent origin but are sidebar tabs,
// never topology — keep them out of the live map too.
if (entry.label?.startsWith(SIDE_LABEL_PREFIX) ?? false) continue
try {
const activity = lastActivity(
ctx.sessions.get(entry.id)?.snapshotEvents() ?? [],
LIVE_WINDOW_MESSAGES,
)
if (activity.text !== undefined || activity.tool !== undefined) {
live[entry.id] = activity
}
} catch {
// One child's event log is not readable: skip only that child.
}
foldInto(entry.id, entry.activity === 'running')
}
return { live }
},