Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
96 lines
3.4 KiB
TypeScript
96 lines
3.4 KiB
TypeScript
import type { AISession } from '../../infrastructure/ai/types';
|
|
import { getSessionScopeMatchRank } from './sessionScopeMatch';
|
|
|
|
type HistoryCacheKey = string;
|
|
const MAX_HISTORY_CACHE_ENTRIES_PER_SESSION_LIST = 64;
|
|
const historyCache = new WeakMap<AISession[], Map<HistoryCacheKey, AISession[]>>();
|
|
|
|
function buildHistoryCacheKey(
|
|
scopeType: 'terminal' | 'workspace',
|
|
scopeTargetId: string | undefined,
|
|
scopeHostIds: string[] | undefined,
|
|
activeTerminalSessionIds: Set<string>,
|
|
workspaceMemberTerminalIds: Set<string> | undefined,
|
|
workspaceMemberActiveSessionIds: Set<string>,
|
|
): HistoryCacheKey {
|
|
const hostKey = scopeHostIds ? [...scopeHostIds].sort().join(',') : '';
|
|
const terminalKey = scopeType === 'terminal'
|
|
? [...activeTerminalSessionIds].sort().join(',')
|
|
: '';
|
|
const memberKey = scopeType === 'workspace' && workspaceMemberTerminalIds
|
|
? [...workspaceMemberTerminalIds].sort().join(',')
|
|
: '';
|
|
const memberActiveKey = [...workspaceMemberActiveSessionIds].sort().join(',');
|
|
return `${scopeType}:${scopeTargetId ?? ''}:${hostKey}:${terminalKey}:${memberKey}:${memberActiveKey}`;
|
|
}
|
|
|
|
export function getScopedHistorySessions(
|
|
sessions: AISession[],
|
|
scopeType: 'terminal' | 'workspace',
|
|
scopeTargetId: string | undefined,
|
|
scopeHostIds: string[] | undefined,
|
|
activeTerminalSessionIds: Set<string>,
|
|
workspaceMemberTerminalIds?: Set<string>,
|
|
activeSessionIdMap?: Readonly<Record<string, string | null | undefined>>,
|
|
): AISession[] {
|
|
// A member can be continuing history created on an older terminal. Its
|
|
// selected chat belongs in workspace history even though scope.targetId
|
|
// still identifies that older terminal. Do not include nonmember selections.
|
|
const workspaceMemberActiveSessionIds = new Set<string>();
|
|
if (scopeType === 'workspace' && workspaceMemberTerminalIds && activeSessionIdMap) {
|
|
for (const terminalId of workspaceMemberTerminalIds) {
|
|
const sessionId = activeSessionIdMap[`terminal:${terminalId}`];
|
|
if (sessionId) workspaceMemberActiveSessionIds.add(sessionId);
|
|
}
|
|
}
|
|
let scopeCache = historyCache.get(sessions);
|
|
if (!scopeCache) {
|
|
scopeCache = new Map();
|
|
historyCache.set(sessions, scopeCache);
|
|
}
|
|
|
|
const cacheKey = buildHistoryCacheKey(
|
|
scopeType,
|
|
scopeTargetId,
|
|
scopeHostIds,
|
|
activeTerminalSessionIds,
|
|
workspaceMemberTerminalIds,
|
|
workspaceMemberActiveSessionIds,
|
|
);
|
|
const cached = scopeCache.get(cacheKey);
|
|
if (cached) {
|
|
scopeCache.delete(cacheKey);
|
|
scopeCache.set(cacheKey, cached);
|
|
return cached;
|
|
}
|
|
|
|
const result = sessions
|
|
.map((session) => ({
|
|
session,
|
|
matchRank: getSessionScopeMatchRank(
|
|
session,
|
|
scopeType,
|
|
scopeTargetId,
|
|
scopeHostIds,
|
|
activeTerminalSessionIds,
|
|
workspaceMemberTerminalIds,
|
|
workspaceMemberActiveSessionIds,
|
|
),
|
|
}))
|
|
.filter(({ matchRank }) => matchRank > 0)
|
|
.sort((a, b) => b.matchRank - a.matchRank || b.session.updatedAt - a.session.updatedAt)
|
|
.map(({ session }) => session);
|
|
|
|
scopeCache.set(cacheKey, result);
|
|
while (scopeCache.size > MAX_HISTORY_CACHE_ENTRIES_PER_SESSION_LIST) {
|
|
const oldestKey = scopeCache.keys().next().value;
|
|
if (oldestKey == null) break;
|
|
scopeCache.delete(oldestKey);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function _getScopedHistoryCacheSizeForTests(sessions: AISession[]): number {
|
|
return historyCache.get(sessions)?.size ?? 0;
|
|
}
|