jan/web/app/_hooks/useCreateConversation.ts
Louis b46042654c
chore: update core services and module export (#376)
* chore: update core services and module export

* Correct version of plugins (#374)

Co-authored-by: Hien To <tominhhien97@gmail.com>

* janhq/jan: Update tag build 1.0.2 for data-plugin

* janhq/jan: Update tag build 1.0.2 for inference-plugin

* janhq/jan: Update tag build 1.0.2 for model-management-plugin

* janhq/jan: Update tag build 1.0.2 for monitoring-plugin

* janhq/jan: Update tag build 1.0.2 for openai-plugin

* chore: update web to use @janhq/core module

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
Co-authored-by: Hien To <tominhhien97@gmail.com>
Co-authored-by: Service Account <service@jan.ai>
2023-10-17 19:48:43 +07:00

67 lines
2.0 KiB
TypeScript

import { useAtom, useSetAtom } from "jotai";
import { Conversation } from "@/_models/Conversation";
import { executeSerial } from "@/_services/pluginService";
import { DataService } from "@janhq/core";
import {
userConversationsAtom,
setActiveConvoIdAtom,
addNewConversationStateAtom,
updateConversationWaitingForResponseAtom,
updateConversationErrorAtom,
} from "@/_helpers/atoms/Conversation.atom";
import useInitModel from "./useInitModel";
import { AssistantModel } from "@/_models/AssistantModel";
const useCreateConversation = () => {
const { initModel } = useInitModel();
const [userConversations, setUserConversations] = useAtom(
userConversationsAtom
);
const setActiveConvoId = useSetAtom(setActiveConvoIdAtom);
const addNewConvoState = useSetAtom(addNewConversationStateAtom);
const updateConvWaiting = useSetAtom(
updateConversationWaitingForResponseAtom
);
const updateConvError = useSetAtom(updateConversationErrorAtom);
const requestCreateConvo = async (model: AssistantModel) => {
const conversationName = model.name;
const conv: Conversation = {
modelId: model._id,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
name: conversationName,
};
const id = await executeSerial(DataService.CreateConversation, conv);
if (id) updateConvWaiting(id, true);
initModel(model).then((res: any) => {
if (id) updateConvWaiting(id, false);
if (res?.error) {
updateConvError(id, res.error);
}
});
const mappedConvo: Conversation = {
_id: id,
modelId: model._id,
name: conversationName,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
addNewConvoState(id ?? "", {
hasMore: true,
waitingForResponse: false,
});
setUserConversations([mappedConvo, ...userConversations]);
setActiveConvoId(id);
};
return {
requestCreateConvo,
};
};
export default useCreateConversation;