ai-codegen-cli-vrk 2.1.0 → 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 +1 -1
- package/src/aiClient.js +35 -28
package/package.json
CHANGED
package/src/aiClient.js
CHANGED
|
@@ -1,51 +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
|
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
18
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${genAI.apiKey}`);
|
|
20
19
|
const data = await response.json();
|
|
21
20
|
|
|
22
21
|
if (data.models) {
|
|
23
|
-
//
|
|
24
|
-
const
|
|
25
|
-
m.supportedGenerationMethods.includes("generateContent")
|
|
26
|
-
(m
|
|
27
|
-
|
|
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());
|
|
26
|
+
|
|
27
|
+
const preferred = ["gemini-1.5-flash", "gemini-1.5-flash-latest", "gemini-2.0-flash-exp", "gemini-pro"];
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
activeModel = genAI.getGenerativeModel({ model: modelName }, { apiVersion: "v1beta" });
|
|
33
|
-
return activeModel;
|
|
34
|
-
}
|
|
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); });
|
|
35
32
|
}
|
|
36
33
|
} catch (e) {
|
|
37
|
-
|
|
34
|
+
modelQueue = ["gemini-1.5-flash", "gemini-pro"];
|
|
38
35
|
}
|
|
39
|
-
|
|
40
|
-
// Final fallback to the most common working model
|
|
41
|
-
activeModel = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }, { apiVersion: "v1beta" });
|
|
42
|
-
return activeModel;
|
|
43
36
|
}
|
|
44
37
|
|
|
45
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
|
+
|
|
46
44
|
try {
|
|
47
|
-
const model = await getWorkingModel();
|
|
48
|
-
|
|
49
45
|
const prompt = `
|
|
50
46
|
Generate the ENTIRE project in a SINGLE response.
|
|
51
47
|
Strictly pass all test cases.
|
|
@@ -74,11 +70,22 @@ ${tests}
|
|
|
74
70
|
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
75
71
|
|
|
76
72
|
} catch (error) {
|
|
77
|
-
// Handle Overloaded (503) or Rate Limit (429) - Critical for Free Tier
|
|
78
73
|
const msg = error.message || "";
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
|
82
89
|
return generateFullProject(task, tests, retryCount + 1);
|
|
83
90
|
}
|
|
84
91
|
throw error;
|