ai-error-formatter
Version:
AI tool to explain JavaScript errors with structured output and fun modes like roast, sarcasm, emojis, and child-like explanations.
104 lines (94 loc) • 2.51 kB
JavaScript
// src/index.ts
import dotenv2 from "dotenv";
// src/functions.ts
import { GoogleGenAI } from "@google/genai";
import dotenv from "dotenv";
// src/languageMap.ts
var languageMap = {
en: "English",
hi: "Hindi",
fr: "French",
es: "Spanish",
de: "German",
zh: "Chinese",
ja: "Japanese",
ru: "Russian",
ar: "Arabic",
pt: "Portuguese",
bn: "Bengali",
ta: "Tamil",
te: "Telugu",
ml: "Malayalam",
ur: "Urdu",
ko: "Korean",
it: "Italian",
tr: "Turkish",
vi: "Vietnamese",
id: "Indonesian",
pl: "Polish",
nl: "Dutch",
gu: "Gujarati",
mr: "Marathi"
};
var languageMap_default = languageMap;
// src/buildPrompt.ts
function buildPrompt(error, options) {
const langCode = typeof options.language === "string" ? options.language.toLowerCase() : "en";
const lang = languageMap_default[langCode] || "English";
let prompt = `
You are an expert AI. Return your answer wrapped between these tags:
"::::: AI ERROR EXPLAINER START :::::" and "::::: AI ERROR EXPLAINER END :::::"
Inside that block, use exactly this structure:
::cause::
<short cause of the error>
::suggestion::
<clear fix or advice>
::explanation::
<simple explanation of what went wrong>
Here is the error:
- Error Message: ${error.message}
- Stack Trace: ${error.stack}
`;
if (options.childLikeMode) {
prompt += `
Make the explanation gentle and playful like you're talking to a small child.`;
} else if (options.breakupLetterMode) {
prompt += `
Write this like a dramatic breakup letter between the developer and the error.`;
} else if (options.roastMode) {
prompt += `
Roast the developer playfully for this mistake. Add some sass.`;
} else if (options.sarcastic) {
prompt += `
Use heavy sarcasm, like a grumpy senior dev.`;
}
if (options.emojiMode) {
prompt += `
Sprinkle relevant emojis to lighten the tone.`;
}
prompt += `
Respond in ${lang}.
Only respond within the structure and border described.
`;
return prompt;
}
// src/functions.ts
dotenv.config();
function sayHello({ firstName, lastName }) {
console.log("FirstName is :", firstName, "Lastname is :", lastName);
}
async function errorFormatter(error, options = {}) {
const prompt = buildPrompt(error, options);
const ai = new GoogleGenAI({ apiKey: process.env.AET_GEMINI_API });
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: prompt
});
return response.text;
}
// src/index.ts
dotenv2.config();
export {
errorFormatter,
sayHello
};