From eb3f3fda0db6dfe160a60331c759e6cee0d7c3a8f96f7a483cafd8dc9451716f Mon Sep 17 00:00:00 2001 From: aslan Date: Fri, 26 Dec 2025 11:26:07 +0100 Subject: [PATCH] Ignore case; AI refactor --- src/modules/ai/ai.ts | 35 ++++++++++------------------------- src/modules/module.ts | 4 ++-- src/services/ai/ai.ts | 4 ++-- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/modules/ai/ai.ts b/src/modules/ai/ai.ts index 7d4efa5..7da4930 100644 --- a/src/modules/ai/ai.ts +++ b/src/modules/ai/ai.ts @@ -1,7 +1,7 @@ import { MatrixClient, MsgType } from "matrix-js-sdk"; import type { ICallbackStore } from "../types.js"; import { config } from "../../config.js"; -import { getImageNanoBanana, getTextGemini } from "../../services/ai/ai.js"; +import { getTextGemini, getImageGemini } from "../../services/ai/ai.js"; import type { IAnimal } from "./types.js"; import { animals } from "./animals.js"; @@ -18,25 +18,15 @@ const registerModuleAI = ( callbackFunc: onAI, }); callbackStore.messageCallbacks.push({ - startConditions: animals - .map((animal) => `${animal.name} when`) - .concat(animals.map((animal) => `${animal.name} ked`)), + startConditions: [ + ...animals.map((animal) => `${animal.name} when`), + ...animals.map((animal) => `${animal.name} ked`), + `!img`, + ], callbackFunc: onImageGen, }); }; -const getAnimal = (name: string): IAnimal | undefined => { - const foundAnimals = animals - .map((animal) => { - if (name.includes(animal.name)) { - return animal; - } - }) - .filter((animal) => animal); - - return foundAnimals.at(0); -}; - const onAI = async (text: string, roomId: string) => { if (text.trim().length < 5) { return; @@ -47,22 +37,17 @@ const onAI = async (text: string, roomId: string) => { }; const onImageGen = async (text: string, roomId: string) => { - const firstAnimal = getAnimal(text.trim().split(/\s+/)[0] ?? ""); - if (!firstAnimal) { - return; - } - - let textMod = text; + let textMod = text.replace("!img", "").trim().toLowerCase(); animals.forEach((animal) => { textMod = textMod.replaceAll(animal.name, animal.animal); }); - const buffer = await getImageNanoBanana(textMod.trim()); + const buffer = await getImageGemini(textMod); if (!buffer || buffer.length < 10) { return; } - const imageName = `photo-${firstAnimal.name}.png`; + const imageName = `photo-img-gen.png`; const uploadResult = await client.uploadContent(buffer, { type: "image/png", @@ -80,4 +65,4 @@ const onImageGen = async (text: string, roomId: string) => { }); }; -export { getAnimal, registerModuleAI, onImageGen }; +export { registerModuleAI, onImageGen }; diff --git a/src/modules/module.ts b/src/modules/module.ts index 4bcf896..00cc52b 100644 --- a/src/modules/module.ts +++ b/src/modules/module.ts @@ -31,7 +31,7 @@ const checkMessageCallback = ( if ( callback.startConditions && !callback.startConditions.some((condition) => - text.startsWith(condition), + text.toLowerCase().startsWith(condition), ) ) { return false; @@ -40,7 +40,7 @@ const checkMessageCallback = ( if ( callback.includesConditions && !callback.includesConditions.some((condition) => - text.includes(condition), + text.toLowerCase().includes(condition), ) ) { return false; diff --git a/src/services/ai/ai.ts b/src/services/ai/ai.ts index 3553320..ef5ad34 100644 --- a/src/services/ai/ai.ts +++ b/src/services/ai/ai.ts @@ -14,7 +14,7 @@ const getTextGemini = async (input: string): Promise => { return response.text ?? "AI Error"; }; -const getImageNanoBanana = async ( +const getImageGemini = async ( input: string, ): Promise | undefined> => { const response = await googleAI.models.generateContent({ @@ -41,4 +41,4 @@ const getImageNanoBanana = async ( return buffer; }; -export { getTextGemini, getImageNanoBanana }; +export { getTextGemini, getImageGemini };