ai-codegen-cli-vrk 2.0.5 → 2.0.8
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/bin/index.js +1 -1
- package/package.json +17 -19
- package/src/aiClient.js +54 -41
- package/src/runner.js +3 -6
package/bin/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
import "../src/runner.js";
|
package/package.json
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ai-codegen-cli-vrk",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "Minimalist Terminal-based AI code generator",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"ai-codegen": "bin/index.js"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"bin",
|
|
11
|
-
"src"
|
|
12
|
-
],
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"node": ">=18.0.0"
|
|
19
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-codegen-cli-vrk",
|
|
3
|
+
"version": "2.0.8",
|
|
4
|
+
"description": "Minimalist Terminal-based AI code generator",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ai-codegen": "bin/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@google/generative-ai": "^0.21.0",
|
|
15
|
+
"fs-extra": "^11.2.0",
|
|
16
|
+
"readline-sync": "^1.4.10"
|
|
17
|
+
}
|
|
20
18
|
}
|
package/src/aiClient.js
CHANGED
|
@@ -1,34 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
2
|
+
|
|
3
|
+
let genAI = null;
|
|
4
|
+
let activeModel = null;
|
|
3
5
|
|
|
4
6
|
export function setApiKey(apiKey) {
|
|
5
|
-
|
|
7
|
+
genAI = new GoogleGenerativeAI(apiKey.trim());
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
async function
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
async function getWorkingModel() {
|
|
11
|
+
if (activeModel) return activeModel;
|
|
12
|
+
|
|
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
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
24
39
|
}
|
|
40
|
+
|
|
41
|
+
throw new Error(`Connection Failed. Last error: ${lastError?.message || "Unknown"}`);
|
|
25
42
|
}
|
|
26
43
|
|
|
27
44
|
export async function generateFullProject(task, tests, retryCount = 0) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
45
|
+
try {
|
|
46
|
+
const model = await getWorkingModel();
|
|
47
|
+
|
|
48
|
+
const prompt = `
|
|
49
|
+
You are an expert automated coding exam solver.
|
|
32
50
|
Generate the ENTIRE project in a SINGLE response.
|
|
33
51
|
Strictly pass all test cases.
|
|
34
52
|
|
|
@@ -45,28 +63,23 @@ TESTS:
|
|
|
45
63
|
${tests}
|
|
46
64
|
|
|
47
65
|
### RULES:
|
|
48
|
-
- Minimalist logic
|
|
66
|
+
- Minimalist logic.
|
|
49
67
|
- Bare minimum code to pass.
|
|
50
68
|
- No talk or explanations.
|
|
51
69
|
`;
|
|
52
70
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const data = await response.json();
|
|
71
|
+
const result = await model.generateContent(prompt);
|
|
72
|
+
const response = await result.response;
|
|
73
|
+
const text = response.text();
|
|
74
|
+
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
60
75
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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));
|
|
81
|
+
return generateFullProject(task, tests, retryCount + 1);
|
|
82
|
+
}
|
|
83
|
+
throw error;
|
|
66
84
|
}
|
|
67
|
-
|
|
68
|
-
if (!response.ok) throw new Error(data.error?.message || "Generation failed");
|
|
69
|
-
|
|
70
|
-
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || "";
|
|
71
|
-
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
72
85
|
}
|
package/src/runner.js
CHANGED
|
@@ -4,16 +4,13 @@ import { setApiKey, generateFullProject } from "./aiClient.js";
|
|
|
4
4
|
import { writeSingleFile } from "./fileWriter.js";
|
|
5
5
|
|
|
6
6
|
async function main() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
setApiKey(apiKey.trim());
|
|
7
|
+
const apiKeyInput = readlineSync.question("--- ", { hideEchoBack: false });
|
|
8
|
+
if (!apiKeyInput || apiKeyInput.trim().length === 0) process.exit(1);
|
|
9
|
+
setApiKey(apiKeyInput.trim());
|
|
11
10
|
|
|
12
|
-
// Input Task
|
|
13
11
|
const task = readlineSync.question("- ");
|
|
14
12
|
if (!task || task.trim().length === 0) process.exit(1);
|
|
15
13
|
|
|
16
|
-
// Input Tests
|
|
17
14
|
const testsInput = readlineSync.question("-- ");
|
|
18
15
|
let tests = "";
|
|
19
16
|
try {
|