"use client"; import { useEffect, useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import type { MessageKey, TranslateFn } from "@/i18n/i18n-context"; import { scopeLabel } from "@/lib/memory"; import { filterScopeTuples, normalizeHierarchicalScope, scopeAtLevel, scopeBreadcrumb, scopeChildren, scopeGranularity, scopeWithSelection, SCOPE_LEVELS, SCOPE_FIELD_BY_LEVEL, type ScopeGranularity, type ScopeLevel, } from "@/lib/scope-hierarchy"; import type { MemoryScope } from "@/types/memory"; import { cn } from "@/lib/utils"; export interface ScopeHierarchyPickerProps { scopes: readonly MemoryScope[]; currentScope: MemoryScope; search: string; onSearchChange: (value: string) => void; onApply: (scope: MemoryScope) => void; loading?: boolean; error?: boolean; truncated?: boolean; onRetry?: () => void; allowPartial?: boolean; optionLimit?: number; maxLevel?: ScopeLevel; t: TranslateFn; } const ALL_SCOPE: MemoryScope = { appId: "*", tenantId: "*", agentId: "*", runId: "*", }; const CHOOSE_KEY: Record = { app: "memory.scopeHierarchy.chooseApp", tenant: "memory.scopeHierarchy.chooseTenant", agent: "memory.scopeHierarchy.chooseAgent", run: "memory.scopeHierarchy.chooseRun", }; const ALL_CHILDREN_KEY: Record = { app: "memory.scopeHierarchy.allApps", tenant: "memory.scopeHierarchy.allTenants", agent: "memory.scopeHierarchy.allAgents", run: "memory.scopeHierarchy.allRuns", }; const RANGE_KEY: Record = { all: "memory.scopeHierarchy.allRange", app: "memory.scopeHierarchy.appRange", tenant: "memory.scopeHierarchy.tenantRange", agent: "memory.scopeHierarchy.agentRange", run: "memory.scopeHierarchy.runRange", }; function identity(scope: MemoryScope): string { return JSON.stringify([ scope.appId, scope.tenantId, scope.agentId, scope.runId, ]); } export function ScopeHierarchyPicker({ scopes, currentScope, search, onApply, loading = false, error = false, truncated = false, onRetry, allowPartial = false, optionLimit, maxLevel = "run", t, }: ScopeHierarchyPickerProps) { const normalizedCurrent = normalizeHierarchicalScope(currentScope) ?? ALL_SCOPE; const [draft, setDraft] = useState(normalizedCurrent); useEffect(() => { setDraft(normalizeHierarchicalScope(currentScope) ?? ALL_SCOPE); }, [currentScope]); const granularity = scopeGranularity(draft); const breadcrumb = scopeBreadcrumb(draft); const children = useMemo(() => { if ( granularity !== "all" && SCOPE_LEVELS.indexOf(granularity) >= SCOPE_LEVELS.indexOf(maxLevel) ) { return []; } const items = scopeChildren(scopes, draft); return optionLimit === undefined ? items : items.slice(0, optionLimit); }, [draft, granularity, maxLevel, optionLimit, scopes]); const searchResults = useMemo(() => { const seen = new Set(); const matches = filterScopeTuples(scopes, search).filter((scope) => { const key = identity(scope); if (seen.has(key)) return false; seen.add(key); return true; }); const limited = optionLimit === undefined ? matches : matches.slice(0, optionLimit); return limited.map((scope) => scopeAtLevel(scope, maxLevel)); }, [maxLevel, optionLimit, scopes, search]); const isSearching = search.trim().length > 0; function selectLevel(level: ScopeLevel) { setDraft(scopeAtLevel(draft, level)); } function selectChild(level: ScopeLevel, value: string) { const next = scopeWithSelection(draft, SCOPE_FIELD_BY_LEVEL[level], value); setDraft(next); if (level === maxLevel) onApply(next); } return (
{loading ? (

{t("common.loading")}

) : error ? (
{t("memory.scopeCombobox.loadFailed")} {onRetry ? ( ) : null}
) : isSearching ? (
{searchResults.length > 0 ? ( searchResults.map((scope) => { const normalized = normalizeHierarchicalScope(scope); if (!normalized) return null; const label = scopeLabel(normalized); return ( ); }) ) : (

{t("memory.common.noMatchingScope")}

)}
) : (
{children.length > 0 ? ( children.map((child) => ( )) ) : granularity === maxLevel ? (

{t("memory.scopeHierarchy.exactRunSelected")}

) : (

{t("memory.scopeCombobox.empty")}

)}
)} {allowPartial && !isSearching ? ( ) : null} {truncated ? (

{t("memory.scopeHierarchy.inventoryIncomplete", { count: scopes.length, })}

) : null}
); }