2026-03-20 01:01:03 +02:00
|
|
|
import type { Session } from '@opencode-ai/sdk/v2';
|
|
|
|
|
|
2026-06-26 19:27:53 +03:00
|
|
|
const RECENT_SESSION_MAX_AGE_MS = 48 * 60 * 60 * 1000;
|
2026-03-20 01:01:03 +02:00
|
|
|
|
|
|
|
|
const isSubtaskSession = (session: Session): boolean => {
|
|
|
|
|
return Boolean((session as Session & { parentID?: string | null }).parentID);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isArchivedSession = (session: Session): boolean => {
|
|
|
|
|
return Boolean(session.time?.archived);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getSessionUpdatedAt = (session: Session): number => {
|
|
|
|
|
const updated = session.time?.updated;
|
|
|
|
|
const created = session.time?.created;
|
|
|
|
|
if (typeof updated === 'number' && Number.isFinite(updated)) {
|
|
|
|
|
return updated;
|
|
|
|
|
}
|
|
|
|
|
if (typeof created === 'number' && Number.isFinite(created)) {
|
|
|
|
|
return created;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-23 16:46:20 +03:00
|
|
|
// Recent contains non-archived root sessions that are active now or were
|
|
|
|
|
// updated within the retention window. The caller applies shared lifecycle
|
2026-07-28 12:04:39 +03:00
|
|
|
// ordering after this membership filter; batching ("Show more") handles long
|
|
|
|
|
// windows in the UI.
|
2026-06-12 14:34:33 +03:00
|
|
|
export const deriveRecentSessions = (
|
2026-04-16 17:02:23 +03:00
|
|
|
sessions: Session[],
|
2026-07-23 16:46:20 +03:00
|
|
|
activeSessionIds: ReadonlySet<string>,
|
2026-06-12 14:34:33 +03:00
|
|
|
now = Date.now(),
|
2026-04-16 17:02:23 +03:00
|
|
|
): Session[] => {
|
2026-06-12 14:34:33 +03:00
|
|
|
const minUpdatedAt = now - RECENT_SESSION_MAX_AGE_MS;
|
2026-07-23 16:46:20 +03:00
|
|
|
return sessions.filter((session) => {
|
2026-04-16 17:02:23 +03:00
|
|
|
if (isArchivedSession(session) || isSubtaskSession(session)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-23 16:46:20 +03:00
|
|
|
return activeSessionIds.has(session.id) || getSessionUpdatedAt(session) >= minUpdatedAt;
|
2026-04-16 17:02:23 +03:00
|
|
|
});
|
|
|
|
|
};
|