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:
1 parent
c4d0057b0b
commit
92f844d694
3 files changed
+82
-1
No files matched your search
@@ -206,6 +206,24 @@ export interface SessionState {
|
|||||||
* `first_message` fallback). Never emitted by the reducer.
|
* `first_message` fallback). Never emitted by the reducer.
|
||||||
*/
|
*/
|
||||||
firstMessage?: string | null;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ export interface EmptySessionInput {
|
|||||||
parentId?: string | null;
|
parentId?: string | null;
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
task?: string;
|
task?: string;
|
||||||
|
tab?: number | null;
|
||||||
|
tabRank?: number | null;
|
||||||
|
createdAt?: number | null;
|
||||||
|
lastActiveAt?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function emptySessionState(input: EmptySessionInput): SessionState {
|
export function emptySessionState(input: EmptySessionInput): SessionState {
|
||||||
@@ -62,6 +66,10 @@ export function emptySessionState(input: EmptySessionInput): SessionState {
|
|||||||
...(input.parentId !== undefined ? { parentId: input.parentId } : {}),
|
...(input.parentId !== undefined ? { parentId: input.parentId } : {}),
|
||||||
...(input.visible !== undefined ? { visible: input.visible } : {}),
|
...(input.visible !== undefined ? { visible: input.visible } : {}),
|
||||||
...(input.task !== undefined ? { task: input.task } : {}),
|
...(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;
|
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`. */
|
/** `parentId` as carried by a list row: string → value, explicit null → `null`. */
|
||||||
function summaryParentId(record: Record<string, unknown>, base: SessionState): string | null {
|
function summaryParentId(record: Record<string, unknown>, base: SessionState): string | null {
|
||||||
if (!("parentId" in record) && !("parent_id" in record)) return base.parentId ?? 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 parentId = str(candidate.parentId ?? candidate.parent_id) ?? existing?.parentId ?? null;
|
||||||
const visible = typeof candidate.visible === "boolean" ? candidate.visible : (existing?.visible ?? true);
|
const visible = typeof candidate.visible === "boolean" ? candidate.visible : (existing?.visible ?? true);
|
||||||
const task = str(candidate.task) ?? existing?.task;
|
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 = {
|
const state: SessionState = {
|
||||||
...base,
|
...base,
|
||||||
...candidate,
|
...candidate,
|
||||||
@@ -268,6 +322,10 @@ export function normalizeSnapshotState(
|
|||||||
parentId,
|
parentId,
|
||||||
visible,
|
visible,
|
||||||
...(task !== undefined ? { task } : {}),
|
...(task !== undefined ? { task } : {}),
|
||||||
|
tab,
|
||||||
|
tabRank,
|
||||||
|
createdAt,
|
||||||
|
lastActiveAt,
|
||||||
flags: {
|
flags: {
|
||||||
attention: candidate.flags ? rec(candidate.flags)?.attention === true : false,
|
attention: candidate.flags ? rec(candidate.flags)?.attention === true : false,
|
||||||
done: rec(candidate.flags)?.done === true,
|
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,
|
visible: typeof record.visible === "boolean" ? record.visible : base.visible,
|
||||||
...(task !== undefined ? { task } : {}),
|
...(task !== undefined ? { task } : {}),
|
||||||
...(firstMessage !== undefined ? { firstMessage } : {}),
|
...(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),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export const useSessionsStore = create<SessionsStore>((set, get) => ({
|
|||||||
}
|
}
|
||||||
// Tree metadata is list metadata: a live update (`pi2:session`,
|
// Tree metadata is list metadata: a live update (`pi2:session`,
|
||||||
// reducer events, a rename) carries no `parentId`/`visible`/`task`/
|
// 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
|
// Explicit `undefined` is the only "not carried" marker — an explicit
|
||||||
// `parentId: null` means root and wins, which `??` would invert.
|
// `parentId: null` means root and wins, which `??` would invert.
|
||||||
const previous = list[index] as SessionState;
|
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,
|
visible: session.visible !== undefined ? session.visible : previous.visible,
|
||||||
task: session.task !== undefined ? session.task : previous.task,
|
task: session.task !== undefined ? session.task : previous.task,
|
||||||
tab: session.tab !== undefined ? session.tab : previous.tab,
|
tab: session.tab !== undefined ? session.tab : previous.tab,
|
||||||
|
tabRank: session.tabRank !== undefined ? session.tabRank : previous.tabRank,
|
||||||
createdAt: session.createdAt !== undefined ? session.createdAt : previous.createdAt,
|
createdAt: session.createdAt !== undefined ? session.createdAt : previous.createdAt,
|
||||||
lastActiveAt: session.lastActiveAt !== undefined ? session.lastActiveAt : previous.lastActiveAt,
|
lastActiveAt: session.lastActiveAt !== undefined ? session.lastActiveAt : previous.lastActiveAt,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user