mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-15 18:25:18 +02:00
Add create chat mutation
This commit is contained in:
parent
666b797772
commit
1cf36c6e6c
3 changed files with 57 additions and 14 deletions
|
|
@ -1,7 +1,11 @@
|
||||||
import { atomWithMutation } from "jotai-tanstack-query";
|
import { atomWithMutation } from "jotai-tanstack-query";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import type { Chat } from "@/app/dashboard/[search_space_id]/chats/chats-client";
|
import type { Chat } from "@/app/dashboard/[search_space_id]/chats/chats-client";
|
||||||
import type { DeleteChatRequest } from "@/contracts/types/chat.types";
|
import type {
|
||||||
|
CreateChatRequest,
|
||||||
|
DeleteChatRequest,
|
||||||
|
UpdateChatRequest,
|
||||||
|
} from "@/contracts/types/chat.types";
|
||||||
import { chatsApiService } from "@/lib/apis/chats-api.service";
|
import { chatsApiService } from "@/lib/apis/chats-api.service";
|
||||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||||
import { queryClient } from "@/lib/query-client/client";
|
import { queryClient } from "@/lib/query-client/client";
|
||||||
|
|
@ -31,3 +35,43 @@ export const deleteChatMutationAtom = atomWithMutation((get) => {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const createChatMutationAtom = atomWithMutation((get) => {
|
||||||
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||||
|
const authToken = localStorage.getItem("surfsense_bearer_token");
|
||||||
|
const chatsQueryParams = get(globalChatsQueryParamsAtom);
|
||||||
|
|
||||||
|
return {
|
||||||
|
mutationKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
||||||
|
enabled: !!searchSpaceId && !!authToken,
|
||||||
|
mutationFn: async (request: CreateChatRequest) => {
|
||||||
|
return chatsApiService.createChat(request);
|
||||||
|
},
|
||||||
|
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export const updateChatMutationAtom = atomWithMutation((get) => {
|
||||||
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||||
|
const authToken = localStorage.getItem("surfsense_bearer_token");
|
||||||
|
const chatsQueryParams = get(globalChatsQueryParamsAtom);
|
||||||
|
|
||||||
|
return {
|
||||||
|
mutationKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
||||||
|
enabled: !!searchSpaceId && !!authToken,
|
||||||
|
mutationFn: async (request: UpdateChatRequest) => {
|
||||||
|
return chatsApiService.updateChat(request);
|
||||||
|
},
|
||||||
|
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,17 @@ export const deleteChatResponse = z.object({
|
||||||
|
|
||||||
export const deleteChatRequest = chatSummary.pick({ id: true });
|
export const deleteChatRequest = chatSummary.pick({ id: true });
|
||||||
|
|
||||||
export const createChatRequest = chatDetails.omit({
|
export const createChatRequest = z.object({
|
||||||
created_at: true,
|
type: chatTypeEnum,
|
||||||
id: true,
|
title: z.string().optional().default("Untitled Chat"),
|
||||||
state_version: true,
|
initial_connectors: z.array(z.string()),
|
||||||
|
messages: z.array(
|
||||||
|
z.object({
|
||||||
|
role: z.enum(["user", "assistant"]),
|
||||||
|
content: z.string(),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
search_space_id: z.number(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const updateChatRequest = chatDetails.omit({
|
export const updateChatRequest = chatDetails.omit({
|
||||||
|
|
|
||||||
|
|
@ -86,20 +86,12 @@ class ChatApiService {
|
||||||
throw new ValidationError(`Invalid request: ${errorMessage}`);
|
throw new ValidationError(`Invalid request: ${errorMessage}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { type, title, initial_connectors, messages, search_space_id } = parsedRequest.data;
|
|
||||||
|
|
||||||
return baseApiService.post(
|
return baseApiService.post(
|
||||||
`/api/v1/chats`,
|
`/api/v1/chats`,
|
||||||
|
|
||||||
chatSummary,
|
chatSummary,
|
||||||
{
|
{
|
||||||
body: {
|
body: parsedRequest.data,
|
||||||
type,
|
|
||||||
title,
|
|
||||||
initial_connectors,
|
|
||||||
messages,
|
|
||||||
search_space_id,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue