ai-codegen-cli-vrk 2.0.9 → 2.1.1
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.
- package/package.json +4 -1
- package/src/aiClient.js +42 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-codegen-cli-vrk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
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
|
@@ -1,52 +1,47 @@
|
|
|
1
1
|
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
2
2
|
|
|
3
3
|
let genAI = null;
|
|
4
|
-
let
|
|
4
|
+
let modelQueue = [];
|
|
5
|
+
let currentModelIndex = 0;
|
|
5
6
|
|
|
6
7
|
export function setApiKey(apiKey) {
|
|
7
8
|
genAI = new GoogleGenerativeAI(apiKey.trim());
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
+
* Lists all usable models and sorts them by stability.
|
|
12
13
|
*/
|
|
13
|
-
async function
|
|
14
|
-
if (
|
|
14
|
+
async function refreshModelQueue() {
|
|
15
|
+
if (modelQueue.length > 0) return;
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
try {
|
|
18
|
+
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${genAI.apiKey}`);
|
|
19
|
+
const data = await response.json();
|
|
20
|
+
|
|
21
|
+
if (data.models) {
|
|
22
|
+
// Prioritize 1.5-flash (most stable) then 2.0-flash (experimental)
|
|
23
|
+
const usable = data.models
|
|
24
|
+
.filter(m => m.supportedGenerationMethods.includes("generateContent"))
|
|
25
|
+
.map(m => m.name.split('/').pop());
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
}
|
|
39
|
-
}
|
|
27
|
+
const preferred = ["gemini-1.5-flash", "gemini-1.5-flash-latest", "gemini-2.0-flash-exp", "gemini-pro"];
|
|
28
|
+
|
|
29
|
+
modelQueue = preferred.filter(p => usable.includes(p));
|
|
30
|
+
// Add any other usable models found as backup
|
|
31
|
+
usable.forEach(u => { if(!modelQueue.includes(u)) modelQueue.push(u); });
|
|
40
32
|
}
|
|
33
|
+
} catch (e) {
|
|
34
|
+
modelQueue = ["gemini-1.5-flash", "gemini-pro"];
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
throw new Error(`Connection Failed. Google's server rejected the model request. Error: ${lastError?.message}`);
|
|
44
36
|
}
|
|
45
37
|
|
|
46
38
|
export async function generateFullProject(task, tests, retryCount = 0) {
|
|
39
|
+
await refreshModelQueue();
|
|
40
|
+
|
|
41
|
+
const modelName = modelQueue[currentModelIndex] || "gemini-1.5-flash";
|
|
42
|
+
const model = genAI.getGenerativeModel({ model: modelName }, { apiVersion: "v1beta" });
|
|
43
|
+
|
|
47
44
|
try {
|
|
48
|
-
const model = await getWorkingModel();
|
|
49
|
-
|
|
50
45
|
const prompt = `
|
|
51
46
|
Generate the ENTIRE project in a SINGLE response.
|
|
52
47
|
Strictly pass all test cases.
|
|
@@ -75,11 +70,22 @@ ${tests}
|
|
|
75
70
|
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
76
71
|
|
|
77
72
|
} catch (error) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
73
|
+
const msg = error.message || "";
|
|
74
|
+
|
|
75
|
+
// Handle Quota Exceeded (429) or Server Busy (503)
|
|
76
|
+
if ((msg.includes("429") || msg.includes("quota") || msg.includes("503") || msg.includes("overloaded")) && retryCount < 5) {
|
|
77
|
+
|
|
78
|
+
// If a specific model is exhausted, try to switch to the next one in the queue
|
|
79
|
+
if (currentModelIndex < modelQueue.length - 1) {
|
|
80
|
+
console.log(`..... (Model ${modelName} exhausted, switching to ${modelQueue[currentModelIndex+1]}...)`);
|
|
81
|
+
currentModelIndex++;
|
|
82
|
+
return generateFullProject(task, tests, retryCount + 1);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// If all models are exhausted, wait a full 60 seconds
|
|
86
|
+
console.log(`..... (All models busy/exhausted, waiting 60s to reset quota...)`);
|
|
87
|
+
await new Promise(r => setTimeout(r, 60000));
|
|
88
|
+
currentModelIndex = 0; // Reset queue and try again
|
|
83
89
|
return generateFullProject(task, tests, retryCount + 1);
|
|
84
90
|
}
|
|
85
91
|
throw error;
|