* @janhq/plugin-core module * refactor web to use exported services from module * refactor data-plugin to provide DAL & move model logics to model management plugin * model-management in TS * add ci auto package, increate version, and publish to npm repository * chore: storage operations * chore: hybrid data-plugin esm & cjs module * chore: PouchDB Driver * chore: documentation --------- Co-authored-by: Hien To <hien@jan.ai> Co-authored-by: Service Account <service@jan.ai>
67 lines
2.0 KiB
TypeScript
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/plugin-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;
|