diff --git a/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts b/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts new file mode 100644 index 000000000..35a80d84d --- /dev/null +++ b/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts @@ -0,0 +1,29 @@ +import { atomWithMutation } from "jotai-tanstack-query"; +import { toast } from "sonner"; +import { activeSearchSpaceIdAtom } from "@/atoms/seach-spaces/seach-space-queries.atom"; +import type { CreateLLMConfigRequest } from "@/contracts/types/llm-config.types"; +import { llmConfigApiService } from "@/lib/apis/llm-config-api.service"; +import { cacheKeys } from "@/lib/query-client/cache-keys"; +import { queryClient } from "@/lib/query-client/client"; + +export const createLLMConfigMutationAtom = atomWithMutation((get) => { + const searchSpaceId = get(activeSearchSpaceIdAtom); + + return { + mutationKey: cacheKeys.llmConfigs.all(searchSpaceId!), + enabled: !!searchSpaceId, + mutationFn: async (request: CreateLLMConfigRequest) => { + return llmConfigApiService.createLLMConfig(request); + }, + + onSuccess: () => { + toast.success("LLM configuration created successfully"); + queryClient.invalidateQueries({ + queryKey: cacheKeys.llmConfigs.all(searchSpaceId!), + }); + queryClient.invalidateQueries({ + queryKey: cacheKeys.llmConfigs.global(), + }); + }, + }; +}); diff --git a/surfsense_web/lib/apis/llm-config-api.service.ts b/surfsense_web/lib/apis/llm-config-api.service.ts index 4c831d18c..69f9eac9a 100644 --- a/surfsense_web/lib/apis/llm-config-api.service.ts +++ b/surfsense_web/lib/apis/llm-config-api.service.ts @@ -5,7 +5,6 @@ import { type DeleteLLMConfigRequest, deleteLLMConfigRequest, deleteLLMConfigResponse, - type GetGlobalLLMConfigsResponse, type GetLLMConfigRequest, type GetLLMConfigsRequest, type GetLLMPreferencesRequest, diff --git a/surfsense_web/lib/query-client/cache-keys.ts b/surfsense_web/lib/query-client/cache-keys.ts index 033f7563b..d514fd6f1 100644 --- a/surfsense_web/lib/query-client/cache-keys.ts +++ b/surfsense_web/lib/query-client/cache-keys.ts @@ -1,6 +1,7 @@ import type { GetChatsRequest } from "@/contracts/types/chat.types"; import type { GetDocumentsRequest } from "@/contracts/types/document.types"; import type { GetPodcastsRequest } from "@/contracts/types/podcast.types"; +import type { GetLLMConfigsRequest } from "@/contracts/types/llm-config.types"; export const cacheKeys = { chats: { @@ -21,6 +22,14 @@ export const cacheKeys = { typeCounts: (searchSpaceId?: string) => ["documents", "type-counts", searchSpaceId] as const, byChunk: (chunkId: string) => ["documents", "by-chunk", chunkId] as const, }, + llmConfigs: { + global: () => ["llm-configs", "global"] as const, + all: (searchSpaceId: string) => ["llm-configs", searchSpaceId] as const, + withQueryParams: (queries: GetLLMConfigsRequest["queryParams"]) => + ["llm-configs", ...(queries ? Object.values(queries) : [])] as const, + byId: (llmConfigId: string) => ["llm-config", llmConfigId] as const, + preferences: (searchSpaceId: string) => ["llm-preferences", searchSpaceId] as const, + }, auth: { user: ["auth", "user"] as const, },