Add support for replied messages

This commit is contained in:
Aslan 2025-12-29 00:33:52 +01:00
parent 6264567ec9
commit 791883f580
5 changed files with 46 additions and 8 deletions

View file

@ -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;

View file

@ -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,
);
}
});
});

View file

@ -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 };