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.
- package/package.json +4 -1
- 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
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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;
|