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

@ -1,6 +1,6 @@
{ {
"name": "aslobot-matrix", "name": "aslobot-matrix",
"version": "0.8.0", "version": "0.8.1",
"description": "", "description": "",
"license": "ISC", "license": "ISC",
"author": "", "author": "",

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); const user = getUserById(sender);
if (text.trim().length < 5) { if (text.trim().length < 5) {
@ -66,9 +71,13 @@ const onAI = async (text: string, roomId: string, sender: string) => {
} as IAIInstructions; } as IAIInstructions;
const username = getUserName(user); 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; user.aiCost += responseAI.tokens * prices.text;

View file

@ -57,7 +57,7 @@ const checkMessageCallback = (
const registerModules = (client: MatrixClient) => { const registerModules = (client: MatrixClient) => {
const startupTime = Date.now(); const startupTime = Date.now();
client.on(RoomEvent.Timeline, (event: MatrixEvent) => { client.on(RoomEvent.Timeline, async (event: MatrixEvent) => {
const ts = event.getTs(); const ts = event.getTs();
if (ts < startupTime) { if (ts < startupTime) {
return; return;
@ -83,6 +83,23 @@ const registerModules = (client: MatrixClient) => {
return; 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}`); console.log(`Message from ${sender} in ${roomId}: ${body}`);
onAnyMessage(client, body.toString(), roomId, sender); onAnyMessage(client, body.toString(), roomId, sender);
@ -97,7 +114,12 @@ const registerModules = (client: MatrixClient) => {
sender, sender,
) )
) { ) {
callback.callbackFunc(body.toString(), roomId, sender); callback.callbackFunc(
body.toString(),
roomId,
sender,
repliedMessage,
);
} }
}); });
}); });

View file

@ -9,7 +9,12 @@ interface ICallback {
includesConditions?: string[]; includesConditions?: string[];
allowedRoles?: TRole[]; allowedRoles?: TRole[];
allowedRooms?: string; 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 }; export { type ICallbackStore, type ICallback };

View file

@ -10,10 +10,12 @@ const googleAI = new GoogleGenAI({
const getTextGemini = async ( const getTextGemini = async (
instructions: IAIInstructions, instructions: IAIInstructions,
input: string, input: string,
oldInput?: string,
): Promise<AIResponseText> => { ): Promise<AIResponseText> => {
console.log([oldInput, input]);
const response = await googleAI.models.generateContent({ const response = await googleAI.models.generateContent({
model: "gemini-3-flash-preview", model: "gemini-3-flash-preview",
contents: input, contents: oldInput ? [oldInput, input] : input,
config: { config: {
systemInstruction: JSON.stringify(instructions), systemInstruction: JSON.stringify(instructions),
}, },