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 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 };

View file

@ -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;

View file

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