2026-03-20 01:01:03 +02:00
|
|
|
import type { Session } from '@opencode-ai/sdk/v2';
|
|
|
|
|
|
2026-06-12 14:34:33 +03:00
|
|
|
export 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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const sortSessionsByUpdated = (sessions: Session[]): Session[] => {
|
|
|
|
|
return [...sessions].sort((a, b) => getSessionUpdatedAt(b) - getSessionUpdatedAt(a));
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-12 14:34:33 +03:00
|
|
|
// Recent sessions are simply every non-archived, top-level session updated
|
|
|
|
|
// within the last RECENT_SESSION_MAX_AGE_MS. No persisted history or live-busy
|
|
|
|
|
// tracking — membership is derived directly from session timestamps.
|
|
|
|
|
export const deriveRecentSessions = (
|
2026-04-16 17:02:23 +03:00
|
|
|
sessions: Session[],
|
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;
|
|
|
|
|
const recent = sessions.filter((session) => {
|
2026-04-16 17:02:23 +03:00
|
|
|
if (isArchivedSession(session) || isSubtaskSession(session)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-06-12 14:34:33 +03:00
|
|
|
return getSessionUpdatedAt(session) >= minUpdatedAt;
|
2026-04-16 17:02:23 +03:00
|
|
|
});
|
2026-06-12 14:34:33 +03:00
|
|
|
return sortSessionsByUpdated(recent);
|
2026-04-16 17:02:23 +03:00
|
|
|
};
|
|
|
|
|
|
2026-03-20 01:01:03 +02:00
|
|
|
export const getSessionUpdatedAtMs = getSessionUpdatedAt;
|