import { useInfiniteQuery, useMutation, useQuery, useQueryClient, } from "@tanstack/react-query"; import { wikiQueryKeys } from "@/lib/query-keys"; import { useDataScopeKey } from "@/providers/runtime-context-provider"; import { ApiClientError } from "@/services/api-client"; import { addWikiSources, deleteWikiSource, getWiki, getWikiPage, getWikiSource, ingestWiki, listWikiArtifacts, listWikiPages, listWikiSources, searchWikiPages, updateWiki, updateWikiSource, } from "@/services/wiki"; import type { AddWikiSourcesRequest, DeleteWikiSourceRequest, GetWikiPageRequest, IngestWikiRequest, ListWikiArtifactsRequest, ListWikiArtifactsResponse, ListWikiPagesRequest, ListWikiPagesResponse, ListWikiSourcesRequest, ListWikiSourcesResponse, SearchWikiPagesRequest, UpdateWikiRequest, UpdateWikiSourceRequest, Wiki, WikiPage, WikiRawSource, WikiScope, } from "@/types/wiki"; export { wikiQueryKeys }; function hasValue(value: string | undefined): value is string { return Boolean(value?.trim()); } function isExactScope(scope: WikiScope | undefined): scope is WikiScope { if (!scope) return false; return [scope.appId, scope.tenantId, scope.agentId].every( (segment) => hasValue(segment) && segment.trim() !== "*", ); } function isWikiQueryEnabled( memoryStoreName: string | undefined, scope: WikiScope | undefined, ): boolean { return hasValue(memoryStoreName) && isExactScope(scope); } export function useWiki( memoryStoreName: string | undefined, scope: WikiScope | undefined, ) { const dataScopeKey = useDataScopeKey(); return useQuery({ queryKey: wikiQueryKeys.detail(dataScopeKey, memoryStoreName ?? "", scope!), queryFn: () => getWiki(memoryStoreName!, scope!), enabled: isWikiQueryEnabled(memoryStoreName, scope), }); } export function useWikiSources( memoryStoreName: string | undefined, scope: WikiScope | undefined, options: Pick & { enabled?: boolean } = {}, ) { const dataScopeKey = useDataScopeKey(); return useInfiniteQuery({ queryKey: wikiQueryKeys.sources( dataScopeKey, memoryStoreName ?? "", scope!, options.limit, ), queryFn: ({ pageParam }) => listWikiSources(memoryStoreName!, { scope: scope!, ...(options.limit !== undefined ? { limit: options.limit } : {}), ...(typeof pageParam === "string" ? { nextToken: pageParam } : {}), }), getNextPageParam: (last) => last.nextToken ?? undefined, initialPageParam: undefined as string | undefined, enabled: options.enabled !== false && isWikiQueryEnabled(memoryStoreName, scope), }); } export function useWikiArtifacts( memoryStoreName: string | undefined, scope: WikiScope | undefined, options: Pick & { enabled?: boolean; }, ) { const dataScopeKey = useDataScopeKey(); return useInfiniteQuery({ queryKey: wikiQueryKeys.artifacts( dataScopeKey, memoryStoreName ?? "", scope!, options.kind, options.limit, ), queryFn: ({ pageParam }) => listWikiArtifacts(memoryStoreName!, { scope: scope!, kind: options.kind, ...(options.limit !== undefined ? { limit: options.limit } : {}), ...(typeof pageParam === "string" ? { nextToken: pageParam } : {}), }), getNextPageParam: (last) => last.nextToken ?? undefined, initialPageParam: undefined as string | undefined, enabled: options.enabled !== false && isWikiQueryEnabled(memoryStoreName, scope), }); } export function useWikiSource( memoryStoreName: string | undefined, scope: WikiScope | undefined, path: string | undefined, ) { const dataScopeKey = useDataScopeKey(); return useQuery({ queryKey: wikiQueryKeys.source( dataScopeKey, memoryStoreName ?? "", scope!, path ?? "", ), queryFn: () => getWikiSource(memoryStoreName!, { scope: scope!, path: path! }), enabled: isWikiQueryEnabled(memoryStoreName, scope) && hasValue(path), }); } export function useWikiPages( memoryStoreName: string | undefined, scope: WikiScope | undefined, options: Pick & { enabled?: boolean; } = {}, ) { const dataScopeKey = useDataScopeKey(); const queryClient = useQueryClient(); const queryKey = wikiQueryKeys.pages( dataScopeKey, memoryStoreName ?? "", scope!, options.limit, ); const query = useInfiniteQuery({ queryKey, queryFn: ({ pageParam }) => listWikiPages(memoryStoreName!, { scope: scope!, ...(options.limit !== undefined ? { limit: options.limit } : {}), ...(typeof pageParam === "string" ? { nextToken: pageParam } : {}), }), getNextPageParam: (last) => last.nextToken ?? undefined, initialPageParam: undefined as string | undefined, retry: false, enabled: options.enabled !== false && isWikiQueryEnabled(memoryStoreName, scope), }); return { ...query, resetWikiPages: () => queryClient.resetQueries({ queryKey, exact: true }), }; } export function useWikiPage( memoryStoreName: string | undefined, scope: WikiScope | undefined, request: | Pick | undefined, ) { const dataScopeKey = useDataScopeKey(); return useQuery({ queryKey: wikiQueryKeys.page( dataScopeKey, memoryStoreName ?? "", scope!, request?.path ?? "", request?.includeReflectionInfo, request?.interaction, ), queryFn: () => getWikiPage(memoryStoreName!, { scope: scope!, path: request!.path, ...(request?.interaction ? { interaction: request.interaction } : {}), ...(request?.includeReflectionInfo !== undefined ? { includeReflectionInfo: request.includeReflectionInfo } : {}), }), enabled: isWikiQueryEnabled(memoryStoreName, scope) && hasValue(request?.path), }); } export function useSearchWikiPages( memoryStoreName: string | undefined, scope: WikiScope | undefined, request: Pick< SearchWikiPagesRequest, "query" | "limit" | "interaction" | "includeReflectionInfo" > & { enabled?: boolean; }, ) { const dataScopeKey = useDataScopeKey(); return useQuery<{ pages: WikiPage[] }, ApiClientError>({ queryKey: wikiQueryKeys.search( dataScopeKey, memoryStoreName ?? "", scope!, request.query, request.limit, request.includeReflectionInfo, request.interaction, ), queryFn: () => searchWikiPages(memoryStoreName!, { scope: scope!, query: request.query, ...(request.limit !== undefined ? { limit: request.limit } : {}), ...(request.interaction ? { interaction: request.interaction } : {}), ...(request.includeReflectionInfo !== undefined ? { includeReflectionInfo: request.includeReflectionInfo } : {}), }), enabled: request.enabled !== false && isWikiQueryEnabled(memoryStoreName, scope) && request.query.trim().length > 0, }); } type MutationVariables = TRequest & { memoryStoreName: string; }; function useWikiMutation( 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) => queryClient.invalidateQueries({ queryKey: wikiQueryKeys.root( dataScopeKey, variables.memoryStoreName, variables.scope, ), }), }); } export function useUpdateWiki() { return useWikiMutation(updateWiki); } export function useIngestWiki() { return useWikiMutation(ingestWiki); } export function useAddWikiSources() { return useWikiMutation(addWikiSources); } export function useUpdateWikiSource() { return useWikiMutation(updateWikiSource); } export function useRemoveWikiSource() { return useWikiMutation(deleteWikiSource); } export type UpdateWikiVariables = MutationVariables; export type IngestWikiVariables = MutationVariables; export type AddWikiSourcesVariables = MutationVariables; export type UpdateWikiSourceVariables = MutationVariables; export type DeleteWikiSourceVariables = MutationVariables;