AI Error handling; Fix logging

This commit is contained in:
Aslan 2026-01-08 18:19:46 -05:00
parent 8f01db9643
commit 28f28eb0ce
4 changed files with 66 additions and 43 deletions

View file

@ -129,7 +129,7 @@ const changePersonality = (
const log = (logMessage: string) => {
appendFileSync(
config.logPath,
`[${new Date().toLocaleString()}] ${logMessage}`,
`[${new Date().toLocaleString()}] ${logMessage}\n`,
);
};

View file

@ -11,6 +11,7 @@ import { toolFunctions, tools } from "./tools.js";
import type { FunctionResponse } from "@google/genai";
import type { Content } from "@google/genai";
import { log } from "../../helpers.js";
import type { GenerateContentResponse } from "@google/genai";
const googleAI = new GoogleGenAI({
apiKey: config.app.ai.api.key,
@ -61,19 +62,27 @@ const getTextGemini = async (
? [oldInputContent, inputContent]
: [inputContent];
const response = await googleAI.models.generateContent({
model: "gemini-3-flash-preview",
contents: contents,
config: {
systemInstruction: JSON.stringify(instructions),
toolConfig: {
functionCallingConfig: {
mode: FunctionCallingConfigMode.AUTO,
let response: GenerateContentResponse;
try {
response = await googleAI.models.generateContent({
model: "gemini-3-flash-preview",
contents: contents,
config: {
systemInstruction: JSON.stringify(instructions),
toolConfig: {
functionCallingConfig: {
mode: FunctionCallingConfigMode.AUTO,
},
},
tools: [{ functionDeclarations: tools }],
},
tools: [{ functionDeclarations: tools }],
},
});
});
} catch (e: unknown) {
return {
text: "AI Error",
tokens: 0,
};
}
let text = response.text ?? "AI Error";
let token = response.usageMetadata?.totalTokenCount ?? 0;
@ -111,30 +120,38 @@ const getTextGemini = async (
},
};
const responseTool = await googleAI.models.generateContent({
model: "gemini-3-flash-preview",
contents: [
...contents,
content,
{
role: "tool",
parts: [
{
functionResponse: functionResponse,
},
],
},
],
config: {
systemInstruction: JSON.stringify(instructions),
toolConfig: {
functionCallingConfig: {
mode: FunctionCallingConfigMode.AUTO,
let responseTool: GenerateContentResponse;
try {
responseTool = await googleAI.models.generateContent({
model: "gemini-3-flash-preview",
contents: [
...contents,
content,
{
role: "tool",
parts: [
{
functionResponse: functionResponse,
},
],
},
],
config: {
systemInstruction: JSON.stringify(instructions),
toolConfig: {
functionCallingConfig: {
mode: FunctionCallingConfigMode.AUTO,
},
},
tools: [{ functionDeclarations: tools }],
},
tools: [{ functionDeclarations: tools }],
},
});
});
} catch (e: unknown) {
return {
text: "AI Error",
tokens: 0,
};
}
return {
text: responseTool.text ?? "AI Error",
@ -143,10 +160,18 @@ const getTextGemini = async (
};
const getImageGemini = async (input: string): Promise<AIResponseImage> => {
const response = await googleAI.models.generateContent({
model: "gemini-2.5-flash-image",
contents: input,
});
let response: GenerateContentResponse;
try {
response = await googleAI.models.generateContent({
model: "gemini-2.5-flash-image",
contents: input,
});
} catch (e: unknown) {
return {
image: undefined,
tokens: 0,
};
}
const firstCandidate = (response.candidates ?? [])[0];
const parts = firstCandidate?.content?.parts ?? [];