jan/web/app/_hooks/useSendChatMessage.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

36 lines
1.2 KiB
TypeScript

import { currentPromptAtom } from "@/_helpers/JotaiWrapper";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { DataService, EventName, events } from "@janhq/core";
import { RawMessage, toChatMessage } from "@/_models/ChatMessage";
import { executeSerial } from "@/_services/pluginService";
import { addNewMessageAtom } from "@/_helpers/atoms/ChatMessage.atom";
import { currentConversationAtom } from "@/_helpers/atoms/Conversation.atom";
export default function useSendChatMessage() {
const currentConvo = useAtomValue(currentConversationAtom);
const addNewMessage = useSetAtom(addNewMessageAtom);
const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom);
const sendChatMessage = async () => {
setCurrentPrompt("");
const prompt = currentPrompt.trim();
const newMessage: RawMessage = {
conversationId: currentConvo?._id,
message: prompt,
user: "user",
createdAt: new Date().toISOString(),
};
const id = await executeSerial(DataService.CreateMessage, newMessage);
newMessage._id = id;
const newChatMessage = toChatMessage(newMessage);
addNewMessage(newChatMessage);
events.emit(EventName.OnNewMessageRequest, newMessage);
};
return {
sendChatMessage,
};
}