mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 18:36:23 +02:00
add update chat mutation atom
This commit is contained in:
parent
684589fc19
commit
373bd668f7
2 changed files with 23 additions and 22 deletions
|
|
@ -4,7 +4,7 @@ import { type CreateMessage, type Message, useChat } from "@ai-sdk/react";
|
||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import { useEffect, useMemo, useRef } from "react";
|
import { useEffect, useMemo, useRef } from "react";
|
||||||
import { createChatMutationAtom } from "@/atoms/chats/chat-mutation.atoms";
|
import { createChatMutationAtom, updateChatMutationAtom } from "@/atoms/chats/chat-mutation.atoms";
|
||||||
import { activeChatAtom } from "@/atoms/chats/chat-query.atoms";
|
import { activeChatAtom } from "@/atoms/chats/chat-query.atoms";
|
||||||
import { activeChatIdAtom } from "@/atoms/chats/ui.atoms";
|
import { activeChatIdAtom } from "@/atoms/chats/ui.atoms";
|
||||||
import ChatInterface from "@/components/chat/ChatInterface";
|
import ChatInterface from "@/components/chat/ChatInterface";
|
||||||
|
|
@ -19,11 +19,8 @@ export default function ResearcherPage() {
|
||||||
const hasSetInitialConnectors = useRef(false);
|
const hasSetInitialConnectors = useRef(false);
|
||||||
const activeChatId = useAtomValue(activeChatIdAtom);
|
const activeChatId = useAtomValue(activeChatIdAtom);
|
||||||
const { data: activeChatState, isFetching: isChatLoading } = useAtomValue(activeChatAtom);
|
const { data: activeChatState, isFetching: isChatLoading } = useAtomValue(activeChatAtom);
|
||||||
const {
|
const { mutateAsync: createChat } = useAtomValue(createChatMutationAtom);
|
||||||
mutateAsync: createChat,
|
const { mutateAsync: updateChat } = useAtomValue(updateChatMutationAtom);
|
||||||
isPending: isCreatingChat,
|
|
||||||
error: createChatError,
|
|
||||||
} = useAtomValue(createChatMutationAtom);
|
|
||||||
const isNewChat = !activeChatId;
|
const isNewChat = !activeChatId;
|
||||||
|
|
||||||
// Reset the flag when chat ID changes
|
// Reset the flag when chat ID changes
|
||||||
|
|
@ -47,10 +44,10 @@ export default function ResearcherPage() {
|
||||||
chat_id: activeChatId ?? undefined,
|
chat_id: activeChatId ?? undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { updateChat } = useChatAPI({
|
// const { updateChat } = useChatAPI({
|
||||||
token,
|
// token,
|
||||||
search_space_id: search_space_id as string,
|
// search_space_id: search_space_id as string,
|
||||||
});
|
// });
|
||||||
|
|
||||||
// Fetch all available sources (document types + live search connectors)
|
// Fetch all available sources (document types + live search connectors)
|
||||||
const { documentTypes } = useDocumentTypes(Number(search_space_id));
|
const { documentTypes } = useDocumentTypes(Number(search_space_id));
|
||||||
|
|
@ -245,7 +242,18 @@ export default function ResearcherPage() {
|
||||||
handler.messages.length > 0 &&
|
handler.messages.length > 0 &&
|
||||||
handler.messages[handler.messages.length - 1]?.role === "assistant"
|
handler.messages[handler.messages.length - 1]?.role === "assistant"
|
||||||
) {
|
) {
|
||||||
updateChat(activeChatId, handler.messages, researchMode, selectedConnectors);
|
const userMessages = handler.messages.filter((msg) => msg.role === "user");
|
||||||
|
if (userMessages.length === 0) return;
|
||||||
|
const title = userMessages[0].content;
|
||||||
|
|
||||||
|
updateChat({
|
||||||
|
type: researchMode,
|
||||||
|
title: title,
|
||||||
|
initial_connectors: selectedConnectors,
|
||||||
|
messages: handler.messages,
|
||||||
|
search_space_id: Number(search_space_id),
|
||||||
|
id: Number(activeChatId),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, [handler.messages, handler.status, activeChatId, isNewChat, isChatLoading]);
|
}, [handler.messages, handler.status, activeChatId, isNewChat, isChatLoading]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,10 @@ export const deleteChatResponse = z.object({
|
||||||
|
|
||||||
export const deleteChatRequest = chatSummary.pick({ id: true });
|
export const deleteChatRequest = chatSummary.pick({ id: true });
|
||||||
|
|
||||||
export const createChatRequest = z.object({
|
export const createChatRequest = chatDetails.omit({
|
||||||
type: chatTypeEnum,
|
created_at: true,
|
||||||
title: z.string(),
|
id: true,
|
||||||
initial_connectors: z.array(z.string()),
|
state_version: true,
|
||||||
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({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue