jan/web/app/_hooks/useSendChatMessage.ts
Louis 27258433d1
#357 plugin & app can subscribe and emit events (#358)
* feature: event based plugin

* chore: update README.md

* Update yarn script for build plugins (#363)

* Update yarn script for build plugins

* Plugin-core install from npmjs instead of from local

---------

Co-authored-by: Hien To <>

* #360 plugin preferences (#361)

* feature: #360 plugin preferences

* chore: update core-plugin README.md

* chore: create collections on start

* chore: bumb core version

* chore: update README

* chore: notify preferences update

* fix: preference update

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
2023-10-16 10:23:38 +00:00

36 lines
1.2 KiB
TypeScript

import { currentPromptAtom } from "@/_helpers/JotaiWrapper";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { DataService, EventName, events } from "@janhq/plugin-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,
};
}