Add support for replied messages
This commit is contained in:
parent
6264567ec9
commit
791883f580
5 changed files with 46 additions and 8 deletions
|
|
@ -28,7 +28,12 @@ const registerModuleAI = (
|
|||
});
|
||||
};
|
||||
|
||||
const onAI = async (text: string, roomId: string, sender: string) => {
|
||||
const onAI = async (
|
||||
text: string,
|
||||
roomId: string,
|
||||
sender: string,
|
||||
repliedMessage?: string,
|
||||
) => {
|
||||
const user = getUserById(sender);
|
||||
|
||||
if (text.trim().length < 5) {
|
||||
|
|
@ -66,9 +71,13 @@ const onAI = async (text: string, roomId: string, sender: string) => {
|
|||
} as IAIInstructions;
|
||||
|
||||
const username = getUserName(user);
|
||||
textMod = `${username}:\n${textMod}`;
|
||||
textMod = `${username}: ${textMod}`;
|
||||
|
||||
const responseAI = await getTextGemini(instructions, textMod);
|
||||
const responseAI = await getTextGemini(
|
||||
instructions,
|
||||
textMod,
|
||||
repliedMessage,
|
||||
);
|
||||
|
||||
user.aiCost += responseAI.tokens * prices.text;
|
||||
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ const checkMessageCallback = (
|
|||
const registerModules = (client: MatrixClient) => {
|
||||
const startupTime = Date.now();
|
||||
|
||||
client.on(RoomEvent.Timeline, (event: MatrixEvent) => {
|
||||
client.on(RoomEvent.Timeline, async (event: MatrixEvent) => {
|
||||
const ts = event.getTs();
|
||||
if (ts < startupTime) {
|
||||
return;
|
||||
|
|
@ -83,6 +83,23 @@ const registerModules = (client: MatrixClient) => {
|
|||
return;
|
||||
}
|
||||
|
||||
const relatesTo = event.getContent()["m.relates_to"];
|
||||
const replyToId = relatesTo?.["m.in_reply_to"]?.event_id;
|
||||
let repliedMessage: string | undefined;
|
||||
|
||||
if (replyToId) {
|
||||
const repliedEvent = await client.fetchRoomEvent(roomId, replyToId);
|
||||
const repliedContent = repliedEvent.content;
|
||||
|
||||
if (repliedContent) {
|
||||
if (repliedContent.body) {
|
||||
repliedMessage = repliedContent.body.toString();
|
||||
} else if (repliedContent.formatted_body) {
|
||||
repliedMessage = repliedContent.formatted_body.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Message from ${sender} in ${roomId}: ${body}`);
|
||||
|
||||
onAnyMessage(client, body.toString(), roomId, sender);
|
||||
|
|
@ -97,7 +114,12 @@ const registerModules = (client: MatrixClient) => {
|
|||
sender,
|
||||
)
|
||||
) {
|
||||
callback.callbackFunc(body.toString(), roomId, sender);
|
||||
callback.callbackFunc(
|
||||
body.toString(),
|
||||
roomId,
|
||||
sender,
|
||||
repliedMessage,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,7 +9,12 @@ interface ICallback {
|
|||
includesConditions?: string[];
|
||||
allowedRoles?: TRole[];
|
||||
allowedRooms?: string;
|
||||
callbackFunc: (text: string, roomId: string, sender: string) => void;
|
||||
callbackFunc: (
|
||||
text: string,
|
||||
roomId: string,
|
||||
sender: string,
|
||||
repliedMessage?: string,
|
||||
) => void;
|
||||
}
|
||||
|
||||
export { type ICallbackStore, type ICallback };
|
||||
|
|
|
|||
|
|
@ -10,10 +10,12 @@ const googleAI = new GoogleGenAI({
|
|||
const getTextGemini = async (
|
||||
instructions: IAIInstructions,
|
||||
input: string,
|
||||
oldInput?: string,
|
||||
): Promise<AIResponseText> => {
|
||||
console.log([oldInput, input]);
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-3-flash-preview",
|
||||
contents: input,
|
||||
contents: oldInput ? [oldInput, input] : input,
|
||||
config: {
|
||||
systemInstruction: JSON.stringify(instructions),
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue