diff --git a/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts b/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts index 400bc19ad..9b484e0de 100644 --- a/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts +++ b/surfsense_web/atoms/llm-config/llm-config-mutation.atoms.ts @@ -1,7 +1,12 @@ import { atomWithMutation } from "jotai-tanstack-query"; import { toast } from "sonner"; import { activeSearchSpaceIdAtom } from "@/atoms/seach-spaces/seach-space-queries.atom"; -import type { CreateLLMConfigRequest, UpdateLLMConfigRequest } from "@/contracts/types/llm-config.types"; +import type { + CreateLLMConfigRequest, + UpdateLLMConfigRequest, + DeleteLLMConfigRequest, + GetLLMConfigsResponse, +} 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"; @@ -52,3 +57,37 @@ export const updateLLMConfigMutationAtom = atomWithMutation((get) => { }, }; }); + +export const deleteLLMConfigMutationAtom = atomWithMutation((get) => { + const searchSpaceId = get(activeSearchSpaceIdAtom); + const authToken = localStorage.getItem("surfsense_bearer_token"); + + return { + mutationKey: cacheKeys.llmConfigs.all(searchSpaceId!), + enabled: !!searchSpaceId && !!authToken, + mutationFn: async (request: DeleteLLMConfigRequest) => { + return llmConfigApiService.deleteLLMConfig(request); + }, + + onSuccess: (_, request: DeleteLLMConfigRequest) => { + toast.success("LLM configuration deleted successfully"); + queryClient.setQueryData( + cacheKeys.llmConfigs.all(searchSpaceId!), + (oldData: GetLLMConfigsResponse | undefined) => { + if (!oldData) return oldData; + return { + ...oldData, + items: oldData.items.filter((config) => config.id !== request.id), + total: oldData.total - 1, + }; + } + ); + queryClient.invalidateQueries({ + queryKey: cacheKeys.llmConfigs.byId(String(request.id)), + }); + queryClient.invalidateQueries({ + queryKey: cacheKeys.llmConfigs.global(), + }); + }, + }; +}); diff --git a/surfsense_web/contracts/types/llm-config.types.ts b/surfsense_web/contracts/types/llm-config.types.ts index f24b23af6..1cfba6e4c 100644 --- a/surfsense_web/contracts/types/llm-config.types.ts +++ b/surfsense_web/contracts/types/llm-config.types.ts @@ -98,7 +98,10 @@ export const getLLMConfigsRequest = z.object({ .nullish(), }); -export const getLLMConfigsResponse = z.array(llmConfig); +export const getLLMConfigsResponse = z.object({ + items: z.array(llmConfig), + total: z.number(), +}); /** * Get LLM config by ID