import type { QueryClient, QueryKey } from "@tanstack/react-query"; import type { FileMemoryScope } from "@/types/file-memory"; import type { WikiInteractionRef, WikiScope } from "@/types/wiki"; export const RUNTIME_CONTEXT_QUERY_KEY = ["runtime-context"] as const; export const PENDING_DATA_SCOPE_KEY = "pending"; export const fileMemoryQueryKeys = { store: (dataScopeKey: string, memoryStoreName: string) => ["memory", dataScopeKey, "stores", memoryStoreName, "file-memory"] as const, root: ( dataScopeKey: string, memoryStoreName: string, scope: FileMemoryScope, ) => [ ...fileMemoryQueryKeys.store(dataScopeKey, memoryStoreName), scope, ] as const, list: ( dataScopeKey: string, memoryStoreName: string, scope: FileMemoryScope, pathPrefix?: string, ) => [ ...fileMemoryQueryKeys.root(dataScopeKey, memoryStoreName, scope), "list", pathPrefix, ] as const, item: ( dataScopeKey: string, memoryStoreName: string, scope: FileMemoryScope, address: string, ) => [ ...fileMemoryQueryKeys.root(dataScopeKey, memoryStoreName, scope), "item", address, ] as const, versions: ( dataScopeKey: string, memoryStoreName: string, scope: FileMemoryScope, itemId: string, ) => [ ...fileMemoryQueryKeys.root(dataScopeKey, memoryStoreName, scope), "versions", itemId, ] as const, version: ( dataScopeKey: string, memoryStoreName: string, scope: FileMemoryScope, versionId: string, ) => [ ...fileMemoryQueryKeys.root(dataScopeKey, memoryStoreName, scope), "version", versionId, ] as const, }; export const wikiQueryKeys = { root: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope | undefined, ) => [ "wiki", dataScopeKey, memoryStoreName, scope?.appId ?? "", scope?.tenantId ?? "", scope?.agentId ?? "", ] as const, detail: (dataScopeKey: string, memoryStoreName: string, scope: WikiScope) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "detail", ] as const, sources: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, limit?: number, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "sources", limit ?? null, ] as const, artifacts: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, kind: "skill" | "profile", limit?: number, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "artifacts", kind, limit ?? null, ] as const, source: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, path: string, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "source", path, ] as const, pages: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, limit?: number, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "pages", limit ?? null, ] as const, page: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, path: string, includeReflectionInfo?: boolean, interaction?: WikiInteractionRef, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "page", path, includeReflectionInfo ?? null, interaction?.sessionId ?? null, interaction?.turnId ?? null, ] as const, search: ( dataScopeKey: string, memoryStoreName: string, scope: WikiScope, query: string, limit?: number, includeReflectionInfo?: boolean, interaction?: WikiInteractionRef, ) => [ ...wikiQueryKeys.root(dataScopeKey, memoryStoreName, scope), "search", query, limit ?? null, includeReflectionInfo ?? null, interaction?.sessionId ?? null, interaction?.turnId ?? null, ] as const, }; export const queryKeys = { runtimeContext: RUNTIME_CONTEXT_QUERY_KEY, instances: (scope: string) => ["instances", scope] as const, instanceConnectivity: (scope: string) => ["instance-connectivity", scope] as const, instance: (scope: string, name: string | null | undefined) => ["instance", scope, name] as const, knowledgeBases: (scope: string) => ["knowledge-bases", scope] as const, knowledgeBase: (scope: string, name: string | null | undefined) => ["knowledge-bases", scope, name] as const, documents: ( scope: string, knowledgeBaseName: string, nextToken?: string, maxResults?: number, subspace?: string, ) => [ "knowledge-bases", scope, knowledgeBaseName, "documents", nextToken, maxResults, subspace, ] as const, document: ( scope: string, knowledgeBaseName: string, docId: string | null | undefined, subspace?: string, ) => [ "knowledge-bases", scope, knowledgeBaseName, "document", docId, subspace, ] as const, chunks: ( scope: string, knowledgeBaseName: string, docId: string | null, subspace?: string, ) => ["chunks", scope, knowledgeBaseName, docId, subspace] as const, memoryStores: (scope: string) => ["memory", scope, "stores"] as const, memoryStore: (scope: string, memoryStoreName: string | undefined) => ["memory", scope, "stores", memoryStoreName] as const, memoryUnits: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, filter?: unknown, ) => [ "memory", scopeKey, "stores", memoryStoreName, "units", scope, nextToken, limit, filter, ] as const, memoryUnit: ( scopeKey: string, memoryStoreName: string, memoryId: string | undefined, scope: unknown, ) => [ "memory", scopeKey, "stores", memoryStoreName, "units", "detail", memoryId, scope, ] as const, memoryRequests: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, operation?: string, ) => [ "memory", scopeKey, "stores", memoryStoreName, "requests", scope, nextToken, limit, operation, ] as const, memoryMessages: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, filters?: { minTimestamp?: string; maxTimestamp?: string }, ) => [ "memory", scopeKey, "stores", memoryStoreName, "messages", scope, nextToken, limit, filters?.minTimestamp, filters?.maxTimestamp, ] as const, memoryStoreScopes: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, ) => [ "memory", scopeKey, "stores", memoryStoreName, "scopes", scope, nextToken, limit, ] as const, memoryScopeDirectory: ( scopeKey: string, memoryStoreName: string, requestKey: string, ) => [ "memory", scopeKey, "stores", memoryStoreName, "scope-directory", requestKey, ] as const, memoryTasks: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, filters?: { status?: string; minTimestamp?: string; maxTimestamp?: string }, ) => [ "memory", scopeKey, "stores", memoryStoreName, "tasks", scope, nextToken, limit, filters?.status, filters?.minTimestamp, filters?.maxTimestamp, ] as const, memoryTask: ( scopeKey: string, memoryStoreName: string, requestId: string | undefined, scope: unknown, ) => [ "memory", scopeKey, "stores", memoryStoreName, "tasks", "detail", requestId, scope, ] as const, dreamTasks: ( scopeKey: string, memoryStoreName: string, scope: unknown, nextToken?: string, limit?: number, filters?: { status?: string; minTimestamp?: string; maxTimestamp?: string }, ) => [ "memory", scopeKey, "stores", memoryStoreName, "dream-tasks", scope, nextToken, limit, filters?.status, filters?.minTimestamp, filters?.maxTimestamp, ] as const, dreamTask: ( scopeKey: string, memoryStoreName: string, dreamId: string | undefined, ) => [ "memory", scopeKey, "stores", memoryStoreName, "dream-tasks", "detail", dreamId, ] as const, dreamActions: ( scopeKey: string, memoryStoreName: string, dreamId: string | undefined, filter: unknown, nextToken?: string, limit?: number, ) => [ "memory", scopeKey, "stores", memoryStoreName, "dream-tasks", dreamId, "actions", filter, nextToken, limit, ] as const, llmProviders: (scope: string) => ["llm-providers", scope] as const, llmProvider: (scope: string, id: string | undefined) => ["llm-providers", scope, id] as const, llmProviderModels: (scope: string, id: string | undefined) => ["llm-provider-models", scope, id] as const, apiKeys: (scope: string) => ["api-keys", scope] as const, apiKeyPolicies: (scope: string, id: string) => ["api-key-policies", scope, id] as const, customPolicy: (scope: string, id: string) => ["custom-policy", scope, id] as const, effectivePermissions: (scope: string, id: string) => ["effective-permissions", scope, id] as const, }; const INSTANCE_SCOPED_QUERY_ROOTS = new Set([ "instances", "instance", "knowledge-bases", "chunks", "memory", "wiki", "llm-providers", "llm-provider-models", "api-keys", "api-key-policies", "custom-policy", "effective-permissions", "auth-status", ]); export function isInstanceScopedQueryKey(queryKey: QueryKey): boolean { const root = queryKey[0]; return typeof root === "string" && INSTANCE_SCOPED_QUERY_ROOTS.has(root); } function belongsToDataScope(queryKey: QueryKey, dataScopeKey: string): boolean { return queryKey[1] === dataScopeKey; } export function resetInstanceScopedQueries( queryClient: QueryClient, dataScopeKey?: string, ): void { queryClient.removeQueries({ predicate: (query) => isInstanceScopedQueryKey(query.queryKey) && (!dataScopeKey || belongsToDataScope(query.queryKey, dataScopeKey)), }); } export function invalidateInstanceScopedQueries( queryClient: QueryClient, ): void { queryClient.invalidateQueries({ predicate: (query) => isInstanceScopedQueryKey(query.queryKey), }); } export function invalidateKnowledgeBaseQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["knowledge-bases"] }); } export function invalidateMemoryQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["memory"] }); } export function invalidateLLMProviderQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["llm-providers"] }); } export function invalidateApiKeyQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["api-keys"] }); } export function invalidateApiKeyPolicyQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["api-key-policies"] }); } export function invalidateCustomPolicyQueries(queryClient: QueryClient): void { queryClient.invalidateQueries({ queryKey: ["custom-policy"] }); } export function invalidateEffectivePermissionsQueries( queryClient: QueryClient, ): void { queryClient.invalidateQueries({ queryKey: ["effective-permissions"] }); }