SurfSense/surfsense_web/lib/query-client/cache-keys.ts

127 lines
5.1 KiB
TypeScript
Raw Normal View History

2025-12-18 22:58:21 +02:00
import type { GetConnectorsRequest } from "@/contracts/types/connector.types";
import type { GetDocumentsRequest } from "@/contracts/types/document.types";
2025-12-26 19:06:09 +02:00
import type { GetLogsRequest } from "@/contracts/types/log.types";
import type { GetWorkspacesRequest } from "@/contracts/types/workspace.types";
2025-11-18 22:12:47 +02:00
/**
* Convert an object to a stable array of [key, value] pairs sorted by key.
* This ensures cache keys are order-independent (avoiding Object.values order-dependency).
* Filters out undefined values.
*/
function stableEntries(obj: Record<string, unknown> | null | undefined): unknown[] {
if (!obj) return [];
return Object.entries(obj)
.filter(([, v]) => v !== undefined)
.sort(([a], [b]) => a.localeCompare(b))
.flat();
}
2025-11-11 04:02:04 +02:00
export const cacheKeys = {
2025-12-21 22:26:33 -08:00
// New chat threads (assistant-ui)
threads: {
detail: (threadId: number) => ["threads", "detail", threadId] as const,
messages: (threadId: number) => ["threads", "messages", threadId] as const,
2025-11-19 08:29:33 +02:00
},
documents: {
globalQueryParams: (queries: GetDocumentsRequest["queryParams"]) =>
["documents", ...stableEntries(queries)] as const,
2025-12-10 14:41:32 -08:00
withQueryParams: (queries: GetDocumentsRequest["queryParams"]) =>
["documents-with-queries", ...stableEntries(queries)] as const,
document: (documentId: string) => ["document", documentId] as const,
},
2025-12-26 17:05:06 +02:00
logs: {
list: (workspaceId?: number | string) => ["logs", "list", workspaceId] as const,
2025-12-26 17:05:06 +02:00
detail: (logId: number | string) => ["logs", "detail", logId] as const,
summary: (workspaceId?: number | string) => ["logs", "summary", workspaceId] as const,
2025-12-26 19:06:09 +02:00
withQueryParams: (queries: GetLogsRequest["queryParams"]) =>
["logs", "with-query-params", ...stableEntries(queries)] as const,
2025-12-26 17:05:06 +02:00
},
modelConnections: {
all: (workspaceId: number) => ["model-connections", workspaceId] as const,
global: () => ["model-connections", "global"] as const,
globalConfigStatus: () => ["model-connections", "global-config-status"] as const,
providers: () => ["model-connections", "providers"] as const,
roles: (workspaceId: number) => ["model-roles", workspaceId] as const,
},
2025-11-15 02:07:20 +02:00
auth: {
user: ["auth", "user"] as const,
},
workspaces: {
all: ["workspaces"] as const,
withQueryParams: (queries: GetWorkspacesRequest["queryParams"]) =>
["workspaces", ...stableEntries(queries)] as const,
detail: (workspaceId: string) => ["workspaces", workspaceId] as const,
2025-12-16 05:37:30 +00:00
},
2025-12-15 12:04:39 +00:00
user: {
current: () => ["user", "me"] as const,
},
roles: {
all: (workspaceId: string) => ["roles", workspaceId] as const,
byId: (workspaceId: string, roleId: string) => ["roles", workspaceId, roleId] as const,
},
permissions: {
all: () => ["permissions"] as const,
},
2025-12-16 14:34:29 +00:00
members: {
all: (workspaceId: string) => ["members", workspaceId] as const,
myAccess: (workspaceId: string) => ["members", "my-access", workspaceId] as const,
2025-12-16 14:34:29 +00:00
},
2025-12-16 15:58:26 +00:00
invites: {
all: (workspaceId: string) => ["invites", workspaceId] as const,
2025-12-16 15:58:26 +00:00
info: (inviteCode: string) => ["invites", "info", inviteCode] as const,
},
agentTools: {
all: () => ["agent-tools"] as const,
},
2025-12-18 22:58:21 +02:00
connectors: {
all: (workspaceId: string) => ["connectors", workspaceId] as const,
2025-12-18 22:58:21 +02:00
withQueryParams: (queries: GetConnectorsRequest["queryParams"]) =>
["connectors", ...stableEntries(queries)] as const,
2025-12-18 22:58:21 +02:00
byId: (connectorId: string) => ["connector", connectorId] as const,
2025-12-18 23:08:36 +02:00
index: () => ["connector", "index"] as const,
googleDrive: {
folders: (connectorId: number, parentId?: string) =>
["connectors", "google-drive", connectorId, "folders", parentId] as const,
},
2025-12-18 22:58:21 +02:00
},
comments: {
byMessage: (messageId: number) => ["comments", "message", messageId] as const,
},
2026-01-26 17:08:26 +02:00
publicChat: {
byToken: (shareToken: string) => ["public-chat", shareToken] as const,
},
github: {
repoStars: (username: string, repo: string) =>
["github", "repo-stars", username, repo] as const,
},
publicChatSnapshots: {
all: ["public-chat-snapshots"] as const,
byWorkspace: (workspaceId: number) =>
["public-chat-snapshots", "workspace", workspaceId] as const,
},
prompts: {
all: () => ["prompts"] as const,
public: () => ["prompts", "public"] as const,
},
notifications: {
search: (workspaceId: number | null, search: string, tab: string) =>
["notifications", "search", workspaceId, search, tab] as const,
sourceTypes: (workspaceId: number | null) =>
["notifications", "source-types", workspaceId] as const,
batchUnreadCounts: (workspaceId: number | null) =>
["notifications", "unread-counts-batch", workspaceId] as const,
},
automations: {
// list endpoint is keyed by pagination too so distinct pages don't collide
list: (workspaceId: number, limit: number, offset: number) =>
["automations", "list", workspaceId, limit, offset] as const,
detail: (automationId: number) => ["automations", "detail", automationId] as const,
runs: (automationId: number, limit: number, offset: number) =>
["automations", "runs", automationId, limit, offset] as const,
run: (automationId: number, runId: number) =>
["automations", "runs", automationId, runId] as const,
modelEligibility: (workspaceId: number) =>
["automations", "model-eligibility", workspaceId] as const,
},
};