From 92f844d6948efed31d24f4fd6f6dddc650e89884 Mon Sep 17 00:00:00 2001 From: yuez Date: Tue, 27 Oct 2026 19:08:47 +0800 Subject: [PATCH] feat(protocol): carry tab, tabRank, createdAt and lastActiveAt on SessionState List metadata the web console needs to order the chat session list like pi2.nvim's :PiSessions: the tab handle, its 1-based visual tabline position, and the creation/last-activity timestamps. Never emitted by the reducer; the web normalizer and the sessions store preserve them across snapshot and live event updates. --- src/protocol/types.ts | 18 ++++++++ web/src/features/chat/session-state.ts | 62 ++++++++++++++++++++++++++ web/src/stores/sessions.ts | 3 +- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/protocol/types.ts b/src/protocol/types.ts index 1708110..31198e0 100644 --- a/src/protocol/types.ts +++ b/src/protocol/types.ts @@ -206,6 +206,24 @@ export interface SessionState { * `first_message` fallback). Never emitted by the reducer. */ firstMessage?: string | null; + /** + * List metadata only: the live projection of `Session.attached_tab`; positive + * finite number marks an nvim tab handle. Never emitted by the reducer. + */ + tab?: number | null; + /** + * List metadata only: the 1-based visual tabline position of `tab` in + * `nvim_list_tabpages()`. Absent on older plugins. Never emitted by the reducer. + */ + tabRank?: number | null; + /** + * List metadata only: creation timestamp in epoch ms. Never emitted by the reducer. + */ + createdAt?: number | null; + /** + * List metadata only: last active timestamp in epoch ms. Never emitted by the reducer. + */ + lastActiveAt?: number | null; } /* ------------------------------------------------------------------ */ diff --git a/web/src/features/chat/session-state.ts b/web/src/features/chat/session-state.ts index ee56067..9cd14ee 100644 --- a/web/src/features/chat/session-state.ts +++ b/web/src/features/chat/session-state.ts @@ -35,6 +35,10 @@ export interface EmptySessionInput { parentId?: string | null; visible?: boolean; task?: string; + tab?: number | null; + tabRank?: number | null; + createdAt?: number | null; + lastActiveAt?: number | null; } export function emptySessionState(input: EmptySessionInput): SessionState { @@ -62,6 +66,10 @@ export function emptySessionState(input: EmptySessionInput): SessionState { ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), ...(input.visible !== undefined ? { visible: input.visible } : {}), ...(input.task !== undefined ? { task: input.task } : {}), + ...(input.tab !== undefined ? { tab: input.tab } : {}), + ...(input.tabRank !== undefined ? { tabRank: input.tabRank } : {}), + ...(input.createdAt !== undefined ? { createdAt: input.createdAt } : {}), + ...(input.lastActiveAt !== undefined ? { lastActiveAt: input.lastActiveAt } : {}), }; } @@ -235,6 +243,48 @@ export function applySessionEvent(state: SessionState, event: SessionEvent): Ses return next; } +function positiveNum(value: unknown): number | undefined { + return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined; +} + +function epochMs(value: unknown): number | undefined { + if (typeof value === "number" && Number.isFinite(value)) return value; + if (typeof value === "string") { + const parsed = Date.parse(value); + if (Number.isFinite(parsed)) return parsed; + } + return undefined; +} + +function summaryTab(record: Record, base?: SessionState): number | null { + if (!("tab" in record) && !("attached_tab" in record)) return base?.tab ?? null; + const raw = record.tab !== undefined ? record.tab : record.attached_tab; + if (raw === undefined) return base?.tab ?? null; + if (raw === null) return null; + return positiveNum(raw) ?? null; +} + +function summaryTabRank(record: Record, base?: SessionState): number | null { + if (!("tabRank" in record) && !("tab_rank" in record)) return base?.tabRank ?? null; + const raw = record.tabRank !== undefined ? record.tabRank : record.tab_rank; + if (raw === undefined) return base?.tabRank ?? null; + if (raw === null) return null; + return positiveNum(raw) ?? null; +} + +function summaryEpoch( + record: Record, + camelKey: string, + snakeKey: string, + fallback?: number | null, +): number | null { + if (!(camelKey in record) && !(snakeKey in record)) return fallback ?? null; + const raw = record[camelKey] !== undefined ? record[camelKey] : record[snakeKey]; + if (raw === undefined) return fallback ?? null; + if (raw === null) return null; + return epochMs(raw) ?? null; +} + /** `parentId` as carried by a list row: string → value, explicit null → `null`. */ function summaryParentId(record: Record, base: SessionState): string | null { if (!("parentId" in record) && !("parent_id" in record)) return base.parentId ?? null; @@ -260,6 +310,10 @@ export function normalizeSnapshotState( const parentId = str(candidate.parentId ?? candidate.parent_id) ?? existing?.parentId ?? null; const visible = typeof candidate.visible === "boolean" ? candidate.visible : (existing?.visible ?? true); const task = str(candidate.task) ?? existing?.task; + const tab = summaryTab(candidate, existing); + const tabRank = summaryTabRank(candidate, existing); + const createdAt = summaryEpoch(candidate, "createdAt", "created_at", existing?.createdAt ?? null); + const lastActiveAt = summaryEpoch(candidate, "lastActiveAt", "last_active_at", existing?.lastActiveAt ?? null); const state: SessionState = { ...base, ...candidate, @@ -268,6 +322,10 @@ export function normalizeSnapshotState( parentId, visible, ...(task !== undefined ? { task } : {}), + tab, + tabRank, + createdAt, + lastActiveAt, flags: { attention: candidate.flags ? rec(candidate.flags)?.attention === true : false, done: rec(candidate.flags)?.done === true, @@ -351,6 +409,10 @@ export function normalizeSessionSummary(raw: unknown, workspaceId: string, exist visible: typeof record.visible === "boolean" ? record.visible : base.visible, ...(task !== undefined ? { task } : {}), ...(firstMessage !== undefined ? { firstMessage } : {}), + tab: summaryTab(record, base), + tabRank: summaryTabRank(record, base), + createdAt: summaryEpoch(record, "createdAt", "created_at", base.createdAt), + lastActiveAt: summaryEpoch(record, "lastActiveAt", "last_active_at", base.lastActiveAt), }; } diff --git a/web/src/stores/sessions.ts b/web/src/stores/sessions.ts index 3e476cc..9c62abf 100644 --- a/web/src/stores/sessions.ts +++ b/web/src/stores/sessions.ts @@ -55,7 +55,7 @@ export const useSessionsStore = create((set, get) => ({ } // Tree metadata is list metadata: a live update (`pi2:session`, // reducer events, a rename) carries no `parentId`/`visible`/`task`/ - // `tab`/`createdAt`/`lastActiveAt` and must not orphan the row. + // `tab`/`tabRank`/`createdAt`/`lastActiveAt` and must not orphan the row. // Explicit `undefined` is the only "not carried" marker — an explicit // `parentId: null` means root and wins, which `??` would invert. const previous = list[index] as SessionState; @@ -65,6 +65,7 @@ export const useSessionsStore = create((set, get) => ({ visible: session.visible !== undefined ? session.visible : previous.visible, task: session.task !== undefined ? session.task : previous.task, tab: session.tab !== undefined ? session.tab : previous.tab, + tabRank: session.tabRank !== undefined ? session.tabRank : previous.tabRank, createdAt: session.createdAt !== undefined ? session.createdAt : previous.createdAt, lastActiveAt: session.lastActiveAt !== undefined ? session.lastActiveAt : previous.lastActiveAt, };