mirror of
https://github.com/stablyai/orca.git
synced 2026-10-02 00:02:05 +00:00
* perf: reuse AI-vault path matchers and query and sort keys * test(ai-vault): add cross-platform path and sort-key differentials --------- Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local> Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import {
|
|
createNormalizedPathInsideOrEqualMatcher,
|
|
normalizeRuntimePathForComparison
|
|
} from './cross-platform-path'
|
|
import type { AiVaultListArgs, AiVaultListResult } from './ai-vault-types'
|
|
|
|
export const DEFAULT_AI_VAULT_SCAN_LIMIT = 1000
|
|
|
|
export type AiVaultSessionDepth = number | 'unlimited'
|
|
|
|
export function requestedAiVaultSessionDepth(
|
|
args?: Pick<AiVaultListArgs, 'limit' | 'unlimited'>
|
|
): AiVaultSessionDepth {
|
|
if (args?.unlimited === true) {
|
|
return 'unlimited'
|
|
}
|
|
return args?.limit && Number.isFinite(args.limit) && args.limit > 0
|
|
? Math.floor(args.limit)
|
|
: DEFAULT_AI_VAULT_SCAN_LIMIT
|
|
}
|
|
|
|
// Scanners bound with slice/comparisons, so 'unlimited' resolves to no bound.
|
|
// Single owner of the normalization every scan limit goes through.
|
|
export function aiVaultScanLimit(args?: Pick<AiVaultListArgs, 'limit' | 'unlimited'>): number {
|
|
const depth = requestedAiVaultSessionDepth(args)
|
|
return depth === 'unlimited' ? Number.POSITIVE_INFINITY : depth
|
|
}
|
|
|
|
export function aiVaultSessionDepthCovers(
|
|
cached: AiVaultSessionDepth,
|
|
requested: AiVaultSessionDepth
|
|
): boolean {
|
|
return cached === 'unlimited' || (requested !== 'unlimited' && cached >= requested)
|
|
}
|
|
|
|
export function truncateAiVaultListResult(
|
|
result: AiVaultListResult,
|
|
depth: AiVaultSessionDepth,
|
|
scopePaths: readonly string[] = []
|
|
): AiVaultListResult {
|
|
if (depth === 'unlimited') {
|
|
return result
|
|
}
|
|
const selectedIds = new Set(result.sessions.slice(0, depth).map((session) => session.id))
|
|
if (scopePaths.length > 0) {
|
|
const scopeMatchers = scopePaths.map(createNormalizedPathInsideOrEqualMatcher)
|
|
let scopedCount = 0
|
|
for (const session of result.sessions) {
|
|
const cwd = session.cwd
|
|
const normalizedCwd = cwd ? normalizeRuntimePathForComparison(cwd) : null
|
|
if (normalizedCwd !== null && scopeMatchers.some((matches) => matches(normalizedCwd))) {
|
|
selectedIds.add(session.id)
|
|
if (++scopedCount >= depth) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { ...result, sessions: result.sessions.filter((session) => selectedIds.has(session.id)) }
|
|
}
|