import type { MemoryScope } from "@/types/memory"; export const MAX_RECENT_MEMORY_SCOPES = 10; const STORAGE_KEY_PREFIX = "tsagent.memoryScopes.v1"; type ScopeStorage = Pick; type OptionalScopeField = "tenantId" | "agentId" | "runId"; const OPTIONAL_SCOPE_FIELDS: OptionalScopeField[] = [ "tenantId", "agentId", "runId", ]; export function getRecentMemoryScopeStorageKey( dataScopeKey: string, memoryStoreName: string, ): string { return `${STORAGE_KEY_PREFIX}:${JSON.stringify([dataScopeKey, memoryStoreName])}`; } export function isAllWildcardMemoryScope( value: Partial | null | undefined, ): boolean { if (!value || typeof value.appId !== "string" || value.appId.trim() !== "*") { return false; } return OPTIONAL_SCOPE_FIELDS.every((field) => { const fieldValue = value[field]; if (fieldValue === undefined) return true; if (typeof fieldValue !== "string") return false; const normalized = fieldValue.trim(); return !normalized || normalized === "*"; }); } export function normalizeRecentMemoryScope( value: Partial | null | undefined, ): MemoryScope | null { if (!value || typeof value.appId !== "string" || !value.appId.trim()) { return null; } const scope: MemoryScope = { appId: value.appId.trim() }; for (const field of OPTIONAL_SCOPE_FIELDS) { const fieldValue = value[field]; if (typeof fieldValue === "string" && fieldValue.trim()) { scope[field] = fieldValue.trim(); } } return isAllWildcardMemoryScope(scope) ? null : scope; } function scopeIdentity(scope: MemoryScope): string { return JSON.stringify([ scope.appId, scope.tenantId ?? "", scope.agentId ?? "", scope.runId ?? "", ]); } export function readRecentMemoryScopes( storage: Pick, dataScopeKey: string, memoryStoreName: string, ): MemoryScope[] { try { const raw = storage.getItem( getRecentMemoryScopeStorageKey(dataScopeKey, memoryStoreName), ); if (!raw) return []; const parsed = JSON.parse(raw) as unknown; if (!Array.isArray(parsed)) return []; const identities = new Set(); const scopes: MemoryScope[] = []; for (const item of parsed) { const value = item && typeof item === "object" ? (item as Partial) : null; if (isAllWildcardMemoryScope(value)) continue; const scope = normalizeRecentMemoryScope(value); if (!scope) continue; const identity = scopeIdentity(scope); if (identities.has(identity)) continue; identities.add(identity); scopes.push(scope); if (scopes.length === MAX_RECENT_MEMORY_SCOPES) break; } return scopes; } catch { return []; } } export function rememberRecentMemoryScope( storage: Pick, dataScopeKey: string, memoryStoreName: string, value: Partial, ): MemoryScope[] { const current = readRecentMemoryScopes( storage, dataScopeKey, memoryStoreName, ); if (isAllWildcardMemoryScope(value)) return current; const scope = normalizeRecentMemoryScope(value); if (!scope) return current; const identity = scopeIdentity(scope); const next = [ scope, ...current.filter((item) => scopeIdentity(item) !== identity), ].slice(0, MAX_RECENT_MEMORY_SCOPES); try { storage.setItem( getRecentMemoryScopeStorageKey(dataScopeKey, memoryStoreName), JSON.stringify(next), ); } catch { // Scope history must never block memory navigation. } return next; } export function clearRecentMemoryScopes( storage: Pick, dataScopeKey: string, memoryStoreName: string, ): void { try { storage.removeItem( getRecentMemoryScopeStorageKey(dataScopeKey, memoryStoreName), ); } catch { // Scope history must never block memory navigation. } } export function excludeRecentMemoryScopes( knownScopes: readonly MemoryScope[], recentScopes: readonly MemoryScope[], ): MemoryScope[] { const recentIdentities = new Set( recentScopes .map(normalizeRecentMemoryScope) .filter((scope): scope is MemoryScope => scope !== null) .map(scopeIdentity), ); return knownScopes.filter((scope) => { const normalized = normalizeRecentMemoryScope(scope); return !normalized || !recentIdentities.has(scopeIdentity(normalized)); }); }