Add ai functionality
This commit is contained in:
parent
da66cf9001
commit
dd4da06753
19 changed files with 1118 additions and 52 deletions
44
src/services/ai/ai.ts
Normal file
44
src/services/ai/ai.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { GoogleGenAI } from "@google/genai";
|
||||
import { config } from "../../config.js";
|
||||
|
||||
const googleAI = new GoogleGenAI({
|
||||
apiKey: config.app.ai.api.key,
|
||||
});
|
||||
|
||||
const getTextGemini = async (input: string): Promise<string> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-3-flash-preview",
|
||||
contents: input,
|
||||
});
|
||||
|
||||
return response.text ?? "AI Error";
|
||||
};
|
||||
|
||||
const getImageNanoBanana = async (
|
||||
input: string,
|
||||
): Promise<Buffer<ArrayBuffer> | undefined> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-2.5-flash-image",
|
||||
contents: input,
|
||||
});
|
||||
|
||||
const firstCandidate = (response.candidates ?? [])[0];
|
||||
const parts = firstCandidate?.content?.parts ?? [];
|
||||
|
||||
let buffer: Buffer<ArrayBuffer> | undefined = undefined;
|
||||
|
||||
parts.forEach((part) => {
|
||||
if (part.inlineData) {
|
||||
const imageData = part.inlineData.data;
|
||||
if (!imageData) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = Buffer.from(imageData, "base64");
|
||||
}
|
||||
});
|
||||
|
||||
return buffer;
|
||||
};
|
||||
|
||||
export { getTextGemini, getImageNanoBanana };
|
||||
1
src/services/ai/index.ts
Normal file
1
src/services/ai/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./ai.js";
|
||||
33
src/services/auth/auth.ts
Normal file
33
src/services/auth/auth.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
import crypto from "crypto";
|
||||
import type { BotAuth, BotAuthJson } from "./types.js";
|
||||
|
||||
const newAuth = (): BotAuth => {
|
||||
return {
|
||||
secretKey: new Uint8Array(crypto.randomBytes(32)),
|
||||
};
|
||||
};
|
||||
|
||||
const saveAuth = (authPath: string, auth: BotAuth) => {
|
||||
const authJson: BotAuthJson = {
|
||||
secretKey: Buffer.from(auth.secretKey).toString("base64"),
|
||||
};
|
||||
const json = JSON.stringify(authJson);
|
||||
|
||||
writeFileSync(authPath, json);
|
||||
};
|
||||
|
||||
const loadAuth = (authPath: string): BotAuth => {
|
||||
if (!existsSync(authPath)) {
|
||||
saveAuth(authPath, newAuth());
|
||||
}
|
||||
|
||||
const json = readFileSync(authPath).toString();
|
||||
const authJson = JSON.parse(json) as BotAuthJson;
|
||||
|
||||
return {
|
||||
secretKey: new Uint8Array(Buffer.from(authJson.secretKey, "base64")),
|
||||
};
|
||||
};
|
||||
|
||||
export { newAuth, saveAuth, loadAuth };
|
||||
2
src/services/auth/index.ts
Normal file
2
src/services/auth/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./auth.js";
|
||||
export * from "./types.js";
|
||||
9
src/services/auth/types.ts
Normal file
9
src/services/auth/types.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
interface BotAuth {
|
||||
secretKey: Uint8Array<ArrayBuffer>;
|
||||
}
|
||||
|
||||
interface BotAuthJson {
|
||||
secretKey: string;
|
||||
}
|
||||
|
||||
export { type BotAuth, type BotAuthJson };
|
||||
Loading…
Add table
Add a link
Reference in a new issue