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.
This commit is contained in:
yuez committed 2026-10-27 19:08:47 +08:00
1 parent c4d0057b0b
commit 92f844d694
3 files changed
+82 -1

No files matched your search

+62
View File
@@ -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<string, unknown>, 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<string, unknown>, 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<string, unknown>,
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<string, unknown>, 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),
};
}
+2 -1
View File
@@ -55,7 +55,7 @@ export const useSessionsStore = create<SessionsStore>((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<SessionsStore>((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,
};