Ignore case; AI refactor

This commit is contained in:
Aslan 2025-12-26 11:26:07 +01:00
parent 3187885ac3
commit eb3f3fda0d
3 changed files with 14 additions and 29 deletions

View file

@ -1,7 +1,7 @@
import { MatrixClient, MsgType } from "matrix-js-sdk"; import { MatrixClient, MsgType } from "matrix-js-sdk";
import type { ICallbackStore } from "../types.js"; import type { ICallbackStore } from "../types.js";
import { config } from "../../config.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 type { IAnimal } from "./types.js";
import { animals } from "./animals.js"; import { animals } from "./animals.js";
@ -18,25 +18,15 @@ const registerModuleAI = (
callbackFunc: onAI, callbackFunc: onAI,
}); });
callbackStore.messageCallbacks.push({ callbackStore.messageCallbacks.push({
startConditions: animals startConditions: [
.map((animal) => `${animal.name} when`) ...animals.map((animal) => `${animal.name} when`),
.concat(animals.map((animal) => `${animal.name} ked`)), ...animals.map((animal) => `${animal.name} ked`),
`!img`,
],
callbackFunc: onImageGen, 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) => { const onAI = async (text: string, roomId: string) => {
if (text.trim().length < 5) { if (text.trim().length < 5) {
return; return;
@ -47,22 +37,17 @@ const onAI = async (text: string, roomId: string) => {
}; };
const onImageGen = async (text: string, roomId: string) => { const onImageGen = async (text: string, roomId: string) => {
const firstAnimal = getAnimal(text.trim().split(/\s+/)[0] ?? ""); let textMod = text.replace("!img", "").trim().toLowerCase();
if (!firstAnimal) {
return;
}
let textMod = text;
animals.forEach((animal) => { animals.forEach((animal) => {
textMod = textMod.replaceAll(animal.name, animal.animal); textMod = textMod.replaceAll(animal.name, animal.animal);
}); });
const buffer = await getImageNanoBanana(textMod.trim()); const buffer = await getImageGemini(textMod);
if (!buffer || buffer.length < 10) { if (!buffer || buffer.length < 10) {
return; return;
} }
const imageName = `photo-${firstAnimal.name}.png`; const imageName = `photo-img-gen.png`;
const uploadResult = await client.uploadContent(buffer, { const uploadResult = await client.uploadContent(buffer, {
type: "image/png", type: "image/png",
@ -80,4 +65,4 @@ const onImageGen = async (text: string, roomId: string) => {
}); });
}; };
export { getAnimal, registerModuleAI, onImageGen }; export { registerModuleAI, onImageGen };

View file

@ -31,7 +31,7 @@ const checkMessageCallback = (
if ( if (
callback.startConditions && callback.startConditions &&
!callback.startConditions.some((condition) => !callback.startConditions.some((condition) =>
text.startsWith(condition), text.toLowerCase().startsWith(condition),
) )
) { ) {
return false; return false;
@ -40,7 +40,7 @@ const checkMessageCallback = (
if ( if (
callback.includesConditions && callback.includesConditions &&
!callback.includesConditions.some((condition) => !callback.includesConditions.some((condition) =>
text.includes(condition), text.toLowerCase().includes(condition),
) )
) { ) {
return false; return false;

View file

@ -14,7 +14,7 @@ const getTextGemini = async (input: string): Promise<string> => {
return response.text ?? "AI Error"; return response.text ?? "AI Error";
}; };
const getImageNanoBanana = async ( const getImageGemini = async (
input: string, input: string,
): Promise<Buffer<ArrayBuffer> | undefined> => { ): Promise<Buffer<ArrayBuffer> | undefined> => {
const response = await googleAI.models.generateContent({ const response = await googleAI.models.generateContent({
@ -41,4 +41,4 @@ const getImageNanoBanana = async (
return buffer; return buffer;
}; };
export { getTextGemini, getImageNanoBanana }; export { getTextGemini, getImageGemini };