ai-codegen-cli-vrk 2.0.9 → 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 +28 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-codegen-cli-vrk",
3
- "version": "2.0.9",
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
@@ -8,39 +8,38 @@ export function setApiKey(apiKey) {
8
8
  }
9
9
 
10
10
  /**
11
- * Robust model discovery. Tries newer Flash models first.
11
+ * Ultimate discovery: Asks Google for a list of working models for your key.
12
12
  */
13
13
  async function getWorkingModel() {
14
14
  if (activeModel) return activeModel;
15
15
 
16
- // Added 2.0 Flash to the list
17
- const modelNames = ["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-flash-latest", "gemini-pro"];
18
- const apiVersions = ["v1beta", "v1"];
19
-
20
- let lastError = null;
21
-
22
- for (const ver of apiVersions) {
23
- for (const name of modelNames) {
24
- try {
25
- const m = genAI.getGenerativeModel({ model: name }, { apiVersion: ver });
26
- // Verification call
27
- await m.generateContent({
28
- contents: [{ role: "user", parts: [{ text: "hi" }] }],
29
- generationConfig: { maxOutputTokens: 1 }
30
- });
31
- activeModel = m;
32
- return m;
33
- } catch (err) {
34
- lastError = err;
35
- if (err.message.includes("404") || err.message.includes("not found")) continue;
36
- if (err.message.includes("API key not valid") || err.message.includes("401")) {
37
- throw new Error("Invalid API Key. Please check it at aistudio.google.com");
38
- }
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;
39
34
  }
40
35
  }
36
+ } catch (e) {
37
+ // If listing fails, fall back to a safe guess
41
38
  }
42
-
43
- throw new Error(`Connection Failed. Google's server rejected the model request. Error: ${lastError?.message}`);
39
+
40
+ // Final fallback to the most common working model
41
+ activeModel = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }, { apiVersion: "v1beta" });
42
+ return activeModel;
44
43
  }
45
44
 
46
45
  export async function generateFullProject(task, tests, retryCount = 0) {
@@ -76,9 +75,9 @@ ${tests}
76
75
 
77
76
  } catch (error) {
78
77
  // Handle Overloaded (503) or Rate Limit (429) - Critical for Free Tier
79
- if ((error.message.includes("503") || error.message.includes("429")) && retryCount < 5) {
80
- console.log(`..... (Quota hit or Server busy, waiting to retry ${retryCount + 1}/5)`);
81
- // Wait 15 seconds to safely clear the Free Tier 15-RPM limit
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)`);
82
81
  await new Promise(r => setTimeout(r, 15000));
83
82
  return generateFullProject(task, tests, retryCount + 1);
84
83
  }