ai-codegen-cli-vrk 2.0.8 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +4 -1
  2. package/src/aiClient.js +32 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-codegen-cli-vrk",
3
- "version": "2.0.8",
3
+ "version": "2.1.0",
4
4
  "description": "Minimalist Terminal-based AI code generator",
5
5
  "type": "module",
6
6
  "bin": {
@@ -14,5 +14,8 @@
14
14
  "@google/generative-ai": "^0.21.0",
15
15
  "fs-extra": "^11.2.0",
16
16
  "readline-sync": "^1.4.10"
17
+ },
18
+ "engines": {
19
+ "node": ">=18.0.0"
17
20
  }
18
21
  }
package/src/aiClient.js CHANGED
@@ -7,38 +7,39 @@ export function setApiKey(apiKey) {
7
7
  genAI = new GoogleGenerativeAI(apiKey.trim());
8
8
  }
9
9
 
10
+ /**
11
+ * Ultimate discovery: Asks Google for a list of working models for your key.
12
+ */
10
13
  async function getWorkingModel() {
11
14
  if (activeModel) return activeModel;
12
15
 
13
- const modelNames = ["gemini-1.5-flash", "gemini-pro", "gemini-1.5-flash-latest"];
14
- const apiVersions = ["v1beta", "v1"];
15
-
16
- let lastError = null;
17
-
18
- for (const ver of apiVersions) {
19
- for (const name of modelNames) {
20
- try {
21
- const m = genAI.getGenerativeModel({ model: name }, { apiVersion: ver });
22
- // Minimal verification call
23
- await m.generateContent({
24
- contents: [{ role: "user", parts: [{ text: "hi" }] }],
25
- generationConfig: { maxOutputTokens: 1 }
26
- });
27
- activeModel = m;
28
- return m;
29
- } catch (err) {
30
- lastError = err;
31
- // If it's a 404, this combination is invalid; try next.
32
- if (err.message.includes("404") || err.message.includes("not found")) continue;
33
- // If it's an Auth error, stop and throw it.
34
- if (err.message.includes("API key not valid") || err.message.includes("401")) {
35
- throw new Error("Invalid API Key. Please check it at aistudio.google.com");
36
- }
16
+ try {
17
+ // Attempt to list models to see what this specific key is allowed to use
18
+ // We use v1beta as it has the most complete model list
19
+ const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${genAI.apiKey}`);
20
+ const data = await response.json();
21
+
22
+ if (data.models) {
23
+ // Look for 1.5-flash or 2.0-flash in the permitted list
24
+ const best = data.models.find(m =>
25
+ m.supportedGenerationMethods.includes("generateContent") &&
26
+ (m.name.includes("1.5-flash") || m.name.includes("2.0-flash"))
27
+ );
28
+
29
+ if (best) {
30
+ // Strip "models/" prefix if present for the SDK
31
+ const modelName = best.name.split('/').pop();
32
+ activeModel = genAI.getGenerativeModel({ model: modelName }, { apiVersion: "v1beta" });
33
+ return activeModel;
37
34
  }
38
35
  }
36
+ } catch (e) {
37
+ // If listing fails, fall back to a safe guess
39
38
  }
40
-
41
- throw new Error(`Connection Failed. Last error: ${lastError?.message || "Unknown"}`);
39
+
40
+ // Final fallback to the most common working model
41
+ activeModel = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }, { apiVersion: "v1beta" });
42
+ return activeModel;
42
43
  }
43
44
 
44
45
  export async function generateFullProject(task, tests, retryCount = 0) {
@@ -46,7 +47,6 @@ export async function generateFullProject(task, tests, retryCount = 0) {
46
47
  const model = await getWorkingModel();
47
48
 
48
49
  const prompt = `
49
- You are an expert automated coding exam solver.
50
50
  Generate the ENTIRE project in a SINGLE response.
51
51
  Strictly pass all test cases.
52
52
 
@@ -74,10 +74,11 @@ ${tests}
74
74
  return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
75
75
 
76
76
  } catch (error) {
77
- // Handle Overloaded (503) or Rate Limit (429)
78
- if ((error.message.includes("503") || error.message.includes("429")) && retryCount < 5) {
79
- console.log(`..... (Server busy, retrying ${retryCount + 1}/5)`);
80
- await new Promise(r => setTimeout(r, 10000));
77
+ // Handle Overloaded (503) or Rate Limit (429) - Critical for Free Tier
78
+ const msg = error.message || "";
79
+ if ((msg.includes("503") || msg.includes("429") || msg.includes("overloaded")) && retryCount < 5) {
80
+ console.log(`..... (Server busy, waiting 15s to retry ${retryCount + 1}/5)`);
81
+ await new Promise(r => setTimeout(r, 15000));
81
82
  return generateFullProject(task, tests, retryCount + 1);
82
83
  }
83
84
  throw error;