Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/app/api/generate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,45 @@
import { SUPPORTED_LANGUAGES } from "@/constants/languages";

export const dynamic = "force-dynamic";
// STEP 1: Generate structure
async function generateStructure(repoData: any) {

Check failure on line 8 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check failure on line 8 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

Unexpected any. Specify a different type
const prompt = `
Create a list of README sections for this repository.
Return ONLY array of section names.

Repo:
${JSON.stringify(repoData)}
`;

const res = await callAI(prompt);
return JSON.parse(res); // ["Introduction", "Features", ...]
}
Comment on lines +7 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

callAI is undefined — code will fail at runtime.

The function callAI is called on line 17 but is never defined or imported in this file. This will throw a ReferenceError when executed.

Additionally, JSON.parse(res) on line 18 lacks error handling. If the AI returns malformed JSON or includes preamble text, this will throw an unhandled exception.

🐛 Proposed fix

Either define callAI or use the existing getGeminiModel() pattern:

+async function callAI(prompt: string): Promise<string> {
+  const model = getGeminiModel();
+  const result = await model.generateContent(prompt);
+  const response = await result.response;
+  return response.text();
+}
+
 // STEP 1: Generate structure
 async function generateStructure(repoData: any) {
   const prompt = `
   Create a list of README sections for this repository.
   Return ONLY array of section names.
 
   Repo:
   ${JSON.stringify(repoData)}
   `;
 
   const res = await callAI(prompt);
-  return JSON.parse(res); // ["Introduction", "Features", ...]
+  try {
+    return JSON.parse(res);
+  } catch {
+    throw new Error("Failed to parse section structure from AI response");
+  }
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/api/generate/route.ts` around lines 7 - 19, The function
generateStructure calls an undefined callAI and blindly JSON.parse's the
response; fix by replacing callAI with the existing getGeminiModel()/appropriate
AI call used elsewhere (or import/define callAI) and wrap the JSON.parse in a
try/catch that sanitizes/preprocesses the model output before parsing (e.g.,
strip non-JSON preamble), validate the parsed value is an array of strings, and
return a safe fallback or throw a clear error; reference generateStructure and
callAI (or use getGeminiModel) when making the change.


// STEP 2: Generate each section
async function generateSection(section: string, repoData: any) {

Check failure on line 22 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check failure on line 22 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

Unexpected any. Specify a different type
const prompt = `
Generate ONLY the "${section}" section of a README.

Repo:
${JSON.stringify(repoData)}
`;

return await callAI(prompt);
}
Comment on lines +21 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Same undefined callAI issue.

This function also calls callAI which doesn't exist. Same fix applies — either define the helper or refactor to use the existing Gemini model pattern.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/api/generate/route.ts` around lines 22 - 32, The generateSection
function calls a non-existent helper callAI; replace that call with the
project's established Gemini invocation or implement a callAI wrapper that
delegates to the existing Gemini-based request flow. Locate the generateSection
function and either (a) refactor the return from "return await callAI(prompt);"
to use the same Gemini call used elsewhere (e.g., the function/method that sends
prompts to the Gemini model), passing the prompt and any required options, or
(b) add a callAI helper that accepts a prompt and internally invokes the Gemini
request function/class used in the codebase, then return its result; ensure you
reference the exact symbol generateSection when making the change so all callers
keep working.


// STEP 3: Combine
export async function generateReadme(repoData: any) {

Check failure on line 34 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check failure on line 34 in src/app/api/generate/route.ts

View workflow job for this annotation

GitHub Actions / build-and-lint

Unexpected any. Specify a different type
const sections = await generateStructure(repoData);

let finalReadme = "";

for (const section of sections) {
const content = await generateSection(section, repoData);
finalReadme += `\n## ${section}\n${content}\n`;
}

return finalReadme;
}
/**
* AI README Generation Endpoint
* Optimized for data accuracy, clean prompt interpolation, and multi-language support.
Expand Down
Loading