import { useQuery } from "@tanstack/react-query"; import { listAllMemoryStoreScopes, listMemoryStoreScopes, } from "@/services/memory"; import { queryKeys } from "@/lib/query-keys"; import { useRuntimeContext } from "@/providers/runtime-context-provider"; import type { MemoryScope } from "@/types/memory"; /** * Single-page hook: returns one nextToken-paginated page. Useful when the * caller wants to drive its own pagination UI. */ export function useMemoryStoreScopes( memoryStoreName: string, scope: MemoryScope | null, options?: { limit?: number; nextToken?: string }, ) { const { context, ready } = useRuntimeContext(); return useQuery({ queryKey: queryKeys.memoryStoreScopes( context.dataScopeKey, memoryStoreName, scope, options?.nextToken, options?.limit, ), queryFn: () => listMemoryStoreScopes(memoryStoreName, scope!, options), enabled: ready && !!memoryStoreName && !!scope?.appId, staleTime: 30 * 1000, }); } /** * All-pages variant that walks every nextToken page client-side and dedups * scope tuples. * * ListMemoryStoreScopes accepts wildcard `*` for any dimension, so when the * caller hasn't picked a concrete `appId` (e.g. the user just opened the * store and nothing is filled yet) we still want to surface every scope * that exists under the store. The hook fills the wildcard so the request * always carries a complete `scope` envelope and the engine returns the * unfiltered set. * * Powers the ScopeBar dropdown so the picker reflects real ListScopes * output instead of guessing from the first memory unit's scope. */ export function useAllMemoryStoreScopes( memoryStoreName: string, scope: MemoryScope | null, options: { enabled?: boolean } = {}, ) { const { context, ready } = useRuntimeContext(); const effective: MemoryScope = { appId: scope?.appId || "*", tenantId: scope?.tenantId || "*", agentId: scope?.agentId || "*", runId: scope?.runId || "*", }; return useQuery({ queryKey: [ ...queryKeys.memoryStoreScopes( context.dataScopeKey, memoryStoreName, effective, ), "all", ], queryFn: () => listAllMemoryStoreScopes(memoryStoreName, effective), enabled: options.enabled !== false && ready && !!memoryStoreName, staleTime: 30 * 1000, }); }