From 1e94df499c2a5199702c7d6dc4b0c50b6f7e342e14a08eb7de9ec54f13f7f727 Mon Sep 17 00:00:00 2001 From: aslan Date: Mon, 29 Dec 2025 21:21:45 +0100 Subject: [PATCH] Fixes; Logging --- package.json | 2 +- src/config.preset.json | 2 +- src/helpers.ts | 17 ++++++++++++++--- src/modules/ai/ai.ts | 31 ++++++++++++++----------------- src/modules/global.ts | 4 +++- src/modules/module.ts | 3 ++- src/services/ai/ai.ts | 21 +++++++++++++-------- src/services/ai/tools.ts | 32 +++++++++++++++++++++++++++++++- 8 files changed, 79 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 64c45d8..9ab785f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aslobot-matrix", - "version": "1.2.0", + "version": "1.2.1", "description": "", "license": "ISC", "author": "", diff --git a/src/config.preset.json b/src/config.preset.json index d408241..5eea8f5 100644 --- a/src/config.preset.json +++ b/src/config.preset.json @@ -2,8 +2,8 @@ "baseUrl": "https://", "server": "", "userId": "@", - "authPath": "auth.json", "storePath": "store.json", + "logPath": "log.log", "auth": { "accessToken": "", "deviceId": "" diff --git a/src/helpers.ts b/src/helpers.ts index 73b6733..aebadcd 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -3,6 +3,7 @@ import { config } from "./config.js"; import { state } from "./store/store.js"; import { type IUser, type TRole } from "./store/types.js"; import type { ILevel } from "./types.js"; +import { appendFileSync } from "fs"; const fixUserData = (user: IUser) => { if (!user.experience) { @@ -25,6 +26,7 @@ const fixUserData = (user: IUser) => { const getUserById = (userId: string): IUser => { const user = state.users.find((user) => user.id === userId); if (!user) { + log(`Tried to get a non existing user ${userId}}`); return { id: ":", role: "NONE", @@ -110,17 +112,25 @@ const changePersonality = ( return false; } + const name = + personalityName.charAt(0).toUpperCase() + personalityName.slice(1); + state.personality = { index: personalityIndex, startTime: Date.now(), }; - client.setDisplayName( - `AsloBot (${config.app.ai.personalities[state.personality.index]?.personalityName})`, - ); + client.setDisplayName(name); return true; }; +const log = (logMessage: string) => { + appendFileSync( + config.logPath, + `[${new Date().toLocaleString()}] ${logMessage}`, + ); +}; + export { fixUserData, getUserById, @@ -129,4 +139,5 @@ export { getUserName, getUserFromMention, changePersonality, + log, }; diff --git a/src/modules/ai/ai.ts b/src/modules/ai/ai.ts index 0b7a132..167977f 100644 --- a/src/modules/ai/ai.ts +++ b/src/modules/ai/ai.ts @@ -4,7 +4,7 @@ import { config } from "../../config.js"; import { getTextGemini, getImageGemini } from "../../services/ai/ai.js"; import { alts } from "./alts.js"; import type { IAIInstructions } from "./types.js"; -import { getUserById, getUserName } from "../../helpers.js"; +import { changePersonality, getUserById, getUserName } from "../../helpers.js"; import { prices } from "./prices.js"; import { state } from "../../store/store.js"; @@ -43,24 +43,8 @@ const onAI = async ( const user = getUserById(sender); - const date = Date.now(); let personality = config.app.ai.personalities[state.personality.index]; - if (date > state.personality.startTime + (personality?.timeout ?? 0)) { - const randomInt = Math.floor( - Math.random() * config.app.ai.personalities.length, - ); - state.personality = { - index: randomInt, - startTime: date, - }; - client.setDisplayName( - `AsloBot (${config.app.ai.personalities[state.personality.index]?.personalityName})`, - ); - } - - personality = config.app.ai.personalities[state.personality.index]; - let textMod = text.replace("!ai", "").trim(); let instructions = { prefferedLanguages: ["english", "slovak"], @@ -96,6 +80,19 @@ const onAI = async ( user.aiCost += responseAI.tokens * prices.text; client.sendTextMessage(roomId, responseAI.text); + + const date = Date.now(); + if (date > state.personality.startTime + (personality?.timeout ?? 0)) { + const randomInt = Math.floor( + Math.random() * config.app.ai.personalities.length, + ); + + const newPersonalityName = + config.app.ai.personalities[randomInt]?.personalityName; + if (newPersonalityName) { + changePersonality(client, newPersonalityName); + } + } }; const onImageGen = async (text: string, roomId: string, sender: string) => { diff --git a/src/modules/global.ts b/src/modules/global.ts index c73b2eb..525267b 100644 --- a/src/modules/global.ts +++ b/src/modules/global.ts @@ -1,6 +1,6 @@ import type { MatrixClient } from "matrix-js-sdk"; import type { TRole } from "../store/types.js"; -import { getLevel, getUserById } from "../helpers.js"; +import { getLevel, getUserById, log } from "../helpers.js"; import { config } from "../config.js"; import { state } from "../store/store.js"; @@ -35,11 +35,13 @@ const onAnyMessage = ( ) { user.experience += config.app.experience.gain; user.lastExperienceGainTimestamp = date; + log(`${user.id} gained ${config.app.experience.gain} experience`); } if (date > user.lastMoneyGainTimestamp + config.app.money.timeout) { user.money += config.app.money.gain; user.lastMoneyGainTimestamp = date; + log(`${user.id} gained ${config.app.money.gain} money`); } user.lastMessageTimestamp = date; diff --git a/src/modules/module.ts b/src/modules/module.ts index 2417b2d..70c0751 100644 --- a/src/modules/module.ts +++ b/src/modules/module.ts @@ -10,7 +10,7 @@ import { registerModuleBase } from "./base/base.js"; import { registerModuleAdmin } from "./admin/admin.js"; import { registerModuleUser } from "./user/user.js"; import { registerModuleAI } from "./ai/ai.js"; -import { checkRoles, getUserById } from "../helpers.js"; +import { checkRoles, getUserById, log } from "../helpers.js"; import { onAnyMessage, onMissingRole } from "./global.js"; import { config } from "../config.js"; @@ -129,6 +129,7 @@ const registerModules = (client: MatrixClient) => { } console.log(`Message from ${sender} in ${roomId}: ${body}`); + log(`Message from ${sender} in ${roomId}: ${body}`); onAnyMessage(client, body.toString(), roomId, sender); diff --git a/src/services/ai/ai.ts b/src/services/ai/ai.ts index 5fc0019..ba2537e 100644 --- a/src/services/ai/ai.ts +++ b/src/services/ai/ai.ts @@ -10,6 +10,7 @@ import { FunctionCallingConfigMode } from "@google/genai"; import { toolFunctions, tools } from "./tools.js"; import type { FunctionResponse } from "@google/genai"; import type { Content } from "@google/genai"; +import { log } from "../../helpers.js"; const googleAI = new GoogleGenAI({ apiKey: config.app.ai.api.key, @@ -22,6 +23,16 @@ const getTextGemini = async ( oldInput?: string, inputImage?: Buffer, ): Promise => { + log(`AI Text Request: ${input}`); + + const oldInputContent: Content = { + role: "user", + parts: [ + { + text: oldInput ?? "", + }, + ], + }; const inputContent: Content = inputImage ? { role: "user", @@ -45,14 +56,6 @@ const getTextGemini = async ( }, ], }; - const oldInputContent: Content = { - role: "user", - parts: [ - { - text: oldInput ?? "", - }, - ], - }; const contents: Content[] = oldInput ? [oldInputContent, inputContent] @@ -91,12 +94,14 @@ const getTextGemini = async ( (func) => func.name === functionCall.name, )?.function; if (!func) { + log(`Failed to tool: ${functionCall.name}`); return { text: text, tokens: token, }; } + log(`Calling tool: ${functionCall.name}...`); const output = func(matrixData, functionCall.args); const functionResponse: FunctionResponse = { id: functionCall.id ?? "", diff --git a/src/services/ai/tools.ts b/src/services/ai/tools.ts index 636b021..d772816 100644 --- a/src/services/ai/tools.ts +++ b/src/services/ai/tools.ts @@ -4,6 +4,7 @@ import type { AIToolFunction } from "./types.js"; import { save, state } from "../../store/store.js"; import { EventType, RelationType } from "matrix-js-sdk"; import { changePersonality } from "../../helpers.js"; +import { readFileSync } from "fs"; const tools: FunctionDeclaration[] = [ { @@ -110,6 +111,16 @@ const tools: FunctionDeclaration[] = [ required: [], }, }, + { + name: "getBotLog", + description: + "Gets the recent log about the activity of this bot from a log file", + parametersJsonSchema: { + type: "object", + properties: {}, + required: [], + }, + }, ]; const toolFunctions: AIToolFunction[] = [ @@ -202,7 +213,12 @@ const toolFunctions: AIToolFunction[] = [ }; } - state.aiMemory.splice(index, 1); + state.aiMemory = state.aiMemory.filter( + (memory) => + memory !== args.knowledge && + memory !== null && + memory !== undefined, + ); return { message: "success", @@ -246,6 +262,20 @@ const toolFunctions: AIToolFunction[] = [ }; }, }, + { + name: "getBotLog", + function: (_matrix, _args) => { + const delimeter = "\n"; + const logFile = readFileSync(config.logPath); + + let lines = logFile.toString().trim(); + lines = lines.split(delimeter).slice(-20).join(delimeter); + + return { + log: lines, + }; + }, + }, ]; export { tools, toolFunctions };