ai-codegen-cli-vrk 2.0.4 → 2.0.7
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 +67 -45
- package/src/runner.js +5 -4
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.7",
|
|
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,56 +1,78 @@
|
|
|
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
|
+
// Auto-trim to remove any accidental spaces from copy-pasting
|
|
8
|
+
const cleanKey = apiKey.trim();
|
|
9
|
+
genAI = new GoogleGenerativeAI(cleanKey);
|
|
6
10
|
}
|
|
7
11
|
|
|
8
|
-
async function
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
m.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
async function getWorkingModel() {
|
|
13
|
+
if (activeModel) return activeModel;
|
|
14
|
+
|
|
15
|
+
const candidates = ["gemini-1.5-flash", "gemini-1.5-flash-latest", "gemini-pro"];
|
|
16
|
+
|
|
17
|
+
for (const name of candidates) {
|
|
18
|
+
try {
|
|
19
|
+
// Force the stable 'v1' API version to avoid Beta key issues
|
|
20
|
+
const m = genAI.getGenerativeModel({ model: name }, { apiVersion: "v1" });
|
|
21
|
+
|
|
22
|
+
// Verification call
|
|
23
|
+
await m.generateContent({ contents: [{ role: "user", parts: [{ text: "hi" }] }], generationConfig: { maxOutputTokens: 1 } });
|
|
24
|
+
activeModel = m;
|
|
25
|
+
return m;
|
|
26
|
+
} catch (err) {
|
|
27
|
+
if (err.message.includes("404") || err.message.includes("not found")) continue;
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
24
30
|
}
|
|
31
|
+
throw new Error("No compatible Gemini models found.");
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
export async function generateFullProject(task, tests, retryCount = 0) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
TASK: ${task}
|
|
35
|
-
TESTS: ${tests}`;
|
|
36
|
-
|
|
37
|
-
const response = await fetch(url, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
headers: { "Content-Type": "application/json" },
|
|
40
|
-
body: JSON.stringify({ contents: [{ parts: [{ text: prompt }] }] })
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const data = await response.json();
|
|
44
|
-
|
|
45
|
-
// Handle Overloaded / Service Unavailable (503) or Rate Limit (429)
|
|
46
|
-
if ((response.status === 503 || response.status === 429) && retryCount < 3) {
|
|
47
|
-
console.log(`..... (retrying due to server load)`);
|
|
48
|
-
await new Promise(r => setTimeout(r, 5000)); // Wait 5 seconds
|
|
49
|
-
return generateFullProject(task, tests, retryCount + 1);
|
|
50
|
-
}
|
|
35
|
+
try {
|
|
36
|
+
const model = await getWorkingModel();
|
|
37
|
+
|
|
38
|
+
const prompt = `
|
|
39
|
+
Generate the ENTIRE project in a SINGLE response.
|
|
40
|
+
Strictly pass all test cases.
|
|
51
41
|
|
|
52
|
-
|
|
42
|
+
### FORMATTING:
|
|
43
|
+
File headers must be exactly:
|
|
44
|
+
// ==========================================
|
|
45
|
+
// FILE: path/to/filename.js
|
|
46
|
+
// ==========================================
|
|
53
47
|
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
TASK:
|
|
49
|
+
${task}
|
|
50
|
+
|
|
51
|
+
TESTS:
|
|
52
|
+
${tests}
|
|
53
|
+
|
|
54
|
+
### RULES:
|
|
55
|
+
- Minimalist logic.
|
|
56
|
+
- Bare minimum code to pass.
|
|
57
|
+
- No talk or explanations.
|
|
58
|
+
`;
|
|
59
|
+
|
|
60
|
+
const result = await model.generateContent(prompt);
|
|
61
|
+
const response = await result.response;
|
|
62
|
+
const text = response.text();
|
|
63
|
+
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
64
|
+
|
|
65
|
+
} catch (error) {
|
|
66
|
+
// If key is truly invalid, give a clear message
|
|
67
|
+
if (error.message.includes("API key not valid")) {
|
|
68
|
+
throw new Error("The API key you entered is invalid. Please double-check it at aistudio.google.com");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if ((error.message.includes("503") || error.message.includes("429")) && retryCount < 5) {
|
|
72
|
+
console.log(`..... (Server busy, retrying ${retryCount + 1}/5)`);
|
|
73
|
+
await new Promise(r => setTimeout(r, 10000));
|
|
74
|
+
return generateFullProject(task, tests, retryCount + 1);
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
56
78
|
}
|
package/src/runner.js
CHANGED
|
@@ -4,10 +4,11 @@ import { setApiKey, generateFullProject } from "./aiClient.js";
|
|
|
4
4
|
import { writeSingleFile } from "./fileWriter.js";
|
|
5
5
|
|
|
6
6
|
async function main() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const apiKeyInput = readlineSync.question("--- ", { hideEchoBack: false });
|
|
8
|
+
if (!apiKeyInput || apiKeyInput.trim().length === 0) process.exit(1);
|
|
9
|
+
|
|
10
|
+
// Trimming again here just to be safe
|
|
11
|
+
setApiKey(apiKeyInput.trim());
|
|
11
12
|
|
|
12
13
|
const task = readlineSync.question("- ");
|
|
13
14
|
if (!task || task.trim().length === 0) process.exit(1);
|