From 791883f58081ef25202d2b8f38923db506d2eefb782bfb52923eddac9d2410f5 Mon Sep 17 00:00:00 2001 From: aslan Date: Mon, 29 Dec 2025 00:33:52 +0100 Subject: [PATCH] Add support for replied messages --- package.json | 2 +- src/modules/ai/ai.ts | 15 ++++++++++++--- src/modules/module.ts | 26 ++++++++++++++++++++++++-- src/modules/types.ts | 7 ++++++- src/services/ai/ai.ts | 4 +++- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 586ebc4..9a51ef9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aslobot-matrix", - "version": "0.8.0", + "version": "0.8.1", "description": "", "license": "ISC", "author": "", diff --git a/src/modules/ai/ai.ts b/src/modules/ai/ai.ts index 2013bc0..42095eb 100644 --- a/src/modules/ai/ai.ts +++ b/src/modules/ai/ai.ts @@ -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; diff --git a/src/modules/module.ts b/src/modules/module.ts index 00cc52b..91734da 100644 --- a/src/modules/module.ts +++ b/src/modules/module.ts @@ -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, + ); } }); }); diff --git a/src/modules/types.ts b/src/modules/types.ts index 48a169d..bd2be07 100644 --- a/src/modules/types.ts +++ b/src/modules/types.ts @@ -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 }; diff --git a/src/services/ai/ai.ts b/src/services/ai/ai.ts index 49ba489..bcf29dc 100644 --- a/src/services/ai/ai.ts +++ b/src/services/ai/ai.ts @@ -10,10 +10,12 @@ const googleAI = new GoogleGenAI({ const getTextGemini = async ( instructions: IAIInstructions, input: string, + oldInput?: string, ): Promise => { + 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), },