import type { MemoryScope } from "@/types/memory"; export const SCOPE_LEVELS = ["app", "tenant", "agent", "run"] as const; export type ScopeLevel = (typeof SCOPE_LEVELS)[number]; export type ScopeGranularity = "all" | ScopeLevel; export interface ScopeChild { level: ScopeLevel; value: string; } export interface ScopeBreadcrumbItem extends ScopeChild { concrete: boolean; } export const SCOPE_FIELD_BY_LEVEL = { app: "appId", tenant: "tenantId", agent: "agentId", run: "runId", } as const satisfies Record; const ALL_SCOPE: MemoryScope = { appId: "*", tenantId: "*", agentId: "*", runId: "*", }; const LEVEL_BY_FIELD: Record = { appId: "app", tenantId: "tenant", agentId: "agent", runId: "run", }; export function normalizeHierarchicalScope( scope: MemoryScope, ): MemoryScope | null { const appId = scope.appId?.trim(); if (!appId) return null; const values = [ appId, scope.tenantId?.trim() || "*", scope.agentId?.trim() || "*", scope.runId?.trim() || "*", ]; let wildcardSeen = false; const canonical = values.map((value) => { if (wildcardSeen || value === "*") { wildcardSeen = true; return "*"; } return value; }); return { appId: canonical[0], tenantId: canonical[1], agentId: canonical[2], runId: canonical[3], }; } export function scopeGranularity(scope: MemoryScope): ScopeGranularity { const canonical = normalizeHierarchicalScope(scope) ?? ALL_SCOPE; if (canonical.appId === "*") return "all"; if (canonical.tenantId === "*") return "app"; if (canonical.agentId === "*") return "tenant"; if (canonical.runId === "*") return "agent"; return "run"; } export function scopeAtLevel( scope: MemoryScope, level: ScopeGranularity, ): MemoryScope { if (level === "all") return { ...ALL_SCOPE }; const canonical = normalizeHierarchicalScope(scope) ?? ALL_SCOPE; const selectedIndex = SCOPE_LEVELS.indexOf(level); const result = { ...ALL_SCOPE }; for (let index = 0; index <= selectedIndex; index += 1) { const field = SCOPE_FIELD_BY_LEVEL[SCOPE_LEVELS[index]]; const value = canonical[field]; if (!value || value === "*") break; result[field] = value; } return result; } export function scopeWithSelection( scope: MemoryScope, field: keyof MemoryScope, value: string, ): MemoryScope { const canonical = normalizeHierarchicalScope(scope) ?? { ...ALL_SCOPE }; const level = LEVEL_BY_FIELD[field]; const levelIndex = SCOPE_LEVELS.indexOf(level); canonical[field] = value.trim() || "*"; for (let index = levelIndex + 1; index < SCOPE_LEVELS.length; index += 1) { canonical[SCOPE_FIELD_BY_LEVEL[SCOPE_LEVELS[index]]] = "*"; } return normalizeHierarchicalScope(canonical) ?? { ...ALL_SCOPE }; } export function scopeChildren( scopes: readonly MemoryScope[], parent: MemoryScope, ): ScopeChild[] { const canonicalParent = normalizeHierarchicalScope(parent) ?? ALL_SCOPE; const parentGranularity = scopeGranularity(canonicalParent); const nextIndex = parentGranularity === "all" ? 0 : SCOPE_LEVELS.indexOf(parentGranularity) + 1; if (nextIndex >= SCOPE_LEVELS.length) return []; const nextLevel = SCOPE_LEVELS[nextIndex]; const nextField = SCOPE_FIELD_BY_LEVEL[nextLevel]; const values = new Set(); for (const scope of scopes) { const candidate = normalizeHierarchicalScope(scope); if (!candidate) continue; const matchesParent = SCOPE_LEVELS.slice(0, nextIndex).every((level) => { const field = SCOPE_FIELD_BY_LEVEL[level]; return candidate[field] === canonicalParent[field]; }); const value = candidate[nextField]; if (matchesParent && value && value !== "*") values.add(value); } return [...values] .sort((left, right) => left.localeCompare(right)) .map((value) => ({ level: nextLevel, value })); } export function scopeBreadcrumb(scope: MemoryScope): ScopeBreadcrumbItem[] { const canonical = normalizeHierarchicalScope(scope) ?? ALL_SCOPE; return SCOPE_LEVELS.map((level) => { const value = canonical[SCOPE_FIELD_BY_LEVEL[level]] ?? "*"; return { level, value, concrete: value !== "*" }; }); } export function filterScopeTuples( scopes: readonly MemoryScope[], query: string, ): MemoryScope[] { const terms = query.toLocaleLowerCase().split(/\s+/).filter(Boolean); if (terms.length === 0) return [...scopes]; return scopes.filter((scope) => { const canonical = normalizeHierarchicalScope(scope); if (!canonical) return false; const text = SCOPE_LEVELS.map( (level) => canonical[SCOPE_FIELD_BY_LEVEL[level]], ) .join(" ") .toLocaleLowerCase(); return terms.every((term) => text.includes(term)); }); }