import { useInfiniteQuery, useMutation, useQuery, useQueryClient, } from "@tanstack/react-query"; import { fileMemoryQueryKeys } from "@/lib/query-keys"; import { useDataScopeKey } from "@/providers/runtime-context-provider"; import { addItem, deleteItem, deleteItemVersions, getItem, getItemVersion, listItems, listItemVersions, redactItemVersion, updateItem, } from "@/services/file-memory"; import type { AddItemRequest, DeleteItemRequest, DeleteItemVersionsRequest, FileMemoryScope, GetItemRequest, ListItemsRequest, ListItemVersionsRequest, RedactItemVersionRequest, UpdateItemRequest, } from "@/types/file-memory"; export { fileMemoryQueryKeys }; function isExactScope( scope: FileMemoryScope | undefined, ): scope is FileMemoryScope { return Boolean( scope && scope.appId && scope.tenantId && scope.agentId && scope.runId && !Object.values(scope).includes("*"), ); } export function useItems( memoryStoreName: string | undefined, scope: FileMemoryScope | undefined, options: Pick & { enabled?: boolean; } = {}, ) { const dataScopeKey = useDataScopeKey(); return useInfiniteQuery({ queryKey: fileMemoryQueryKeys.list( dataScopeKey, memoryStoreName ?? "", scope!, options.pathPrefix, ), queryFn: ({ pageParam }) => listItems(memoryStoreName!, { scope: scope!, ...(options.pathPrefix ? { pathPrefix: options.pathPrefix } : {}), ...(options.depth !== undefined ? { depth: options.depth } : {}), ...(options.view ? { view: options.view } : {}), ...(options.limit ? { limit: options.limit } : {}), ...(typeof pageParam === "string" ? { nextToken: pageParam } : {}), }), getNextPageParam: (last) => last.nextToken ?? undefined, initialPageParam: undefined as string | undefined, enabled: options.enabled !== false && !!memoryStoreName && isExactScope(scope), }); } type ItemAddress = Pick; export function useItem( memoryStoreName: string | undefined, scope: FileMemoryScope | undefined, address: ItemAddress | null, ) { const dataScopeKey = useDataScopeKey(); const addressKey = address?.itemId ?? address?.path ?? ""; return useQuery({ queryKey: fileMemoryQueryKeys.item( dataScopeKey, memoryStoreName ?? "", scope!, addressKey, ), queryFn: () => getItem(memoryStoreName!, { scope: scope!, ...address!, view: "full", }), enabled: !!memoryStoreName && isExactScope(scope) && !!addressKey, }); } export function useItemVersions( memoryStoreName: string | undefined, scope: FileMemoryScope | undefined, itemId: string | undefined, options: Pick< ListItemVersionsRequest, | "sessionId" | "apiKeyId" | "createdAtGte" | "createdAtLte" | "operation" | "view" | "limit" > = {}, ) { const dataScopeKey = useDataScopeKey(); return useInfiniteQuery({ queryKey: fileMemoryQueryKeys.versions( dataScopeKey, memoryStoreName ?? "", scope!, itemId ?? "", ), queryFn: ({ pageParam }) => listItemVersions(memoryStoreName!, { scope: scope!, itemId: itemId!, ...options, ...(typeof pageParam === "string" ? { nextToken: pageParam } : {}), }), getNextPageParam: (last) => last.nextToken ?? undefined, initialPageParam: undefined as string | undefined, enabled: !!memoryStoreName && isExactScope(scope) && !!itemId, }); } export function useItemVersion( memoryStoreName: string | undefined, scope: FileMemoryScope | undefined, versionId: string | undefined, ) { const dataScopeKey = useDataScopeKey(); return useQuery({ queryKey: fileMemoryQueryKeys.version( dataScopeKey, memoryStoreName ?? "", scope!, versionId ?? "", ), queryFn: () => getItemVersion(memoryStoreName!, { scope: scope!, versionId: versionId!, view: "full", }), enabled: !!memoryStoreName && isExactScope(scope) && !!versionId, }); } type MutationVariables = TRequest & { memoryStoreName: string }; function useItemMutation( mutate: (memoryStoreName: string, request: TRequest) => Promise, ) { const dataScopeKey = useDataScopeKey(); const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ memoryStoreName, ...request }: MutationVariables) => mutate(memoryStoreName, request as unknown as TRequest), onSuccess: (_data, variables) => { const wildcard = Object.values(variables.scope).includes("*"); return queryClient.invalidateQueries({ queryKey: wildcard ? fileMemoryQueryKeys.store(dataScopeKey, variables.memoryStoreName) : fileMemoryQueryKeys.root( dataScopeKey, variables.memoryStoreName, variables.scope, ), }); }, }); } export function useAddItem() { return useItemMutation(addItem); } export function useUpdateItem() { return useItemMutation(updateItem); } export function useDeleteItem() { return useItemMutation(deleteItem); } export function useDeleteItemVersions() { return useItemMutation(deleteItemVersions); } export function useRedactItemVersion() { return useItemMutation(redactItemVersion); } export type AddItemVariables = MutationVariables; export type UpdateItemVariables = MutationVariables; export type DeleteItemVariables = MutationVariables; export type DeleteItemVersionsVariables = MutationVariables; export type RedactItemVersionVariables = MutationVariables;