ai-codegen-cli-vrk 2.0.1 → 2.0.3
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 -2
- package/src/aiClient.js +61 -24
- package/src/runner.js +3 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-codegen-cli-vrk",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Minimalist Terminal-based AI code generator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
"src"
|
|
12
12
|
],
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@google/generative-ai": "^0.21.0",
|
|
15
14
|
"fs-extra": "^11.2.0",
|
|
16
15
|
"readline-sync": "^1.4.10"
|
|
17
16
|
},
|
package/src/aiClient.js
CHANGED
|
@@ -1,26 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
let genAI = null;
|
|
4
|
-
let model = null;
|
|
1
|
+
let API_KEY = null;
|
|
2
|
+
let SELECTED_MODEL_PATH = null;
|
|
5
3
|
|
|
6
4
|
export function setApiKey(apiKey) {
|
|
7
|
-
|
|
8
|
-
// Using 1.5-flash for speed and large context window
|
|
9
|
-
model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
|
|
5
|
+
API_KEY = apiKey;
|
|
10
6
|
}
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Automatically finds a model that is active for your account.
|
|
10
|
+
* This prevents the "404 Not Found" error.
|
|
11
|
+
*/
|
|
12
|
+
async function findActiveModel() {
|
|
13
|
+
if (SELECTED_MODEL_PATH) return SELECTED_MODEL_PATH;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
// Use the stable v1 API to list models
|
|
16
|
+
const url = `https://generativelanguage.googleapis.com/v1/models?key=${API_KEY}`;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const response = await fetch(url);
|
|
20
|
+
const data = await response.json();
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if (!response.ok) throw new Error(data.error?.message || "Invalid API Key");
|
|
23
|
+
|
|
24
|
+
// Find the first model that supports content generation
|
|
25
|
+
const models = data.models || [];
|
|
26
|
+
const match = models.find(m =>
|
|
27
|
+
m.supportedGenerationMethods.includes("generateContent") &&
|
|
28
|
+
(m.name.includes("flash") || m.name.includes("pro"))
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
if (match) {
|
|
32
|
+
SELECTED_MODEL_PATH = match.name; // e.g. "models/gemini-1.5-flash"
|
|
33
|
+
return SELECTED_MODEL_PATH;
|
|
34
|
+
}
|
|
35
|
+
} catch (err) {
|
|
36
|
+
// If listing fails, fall back to a standard guess
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
SELECTED_MODEL_PATH = "models/gemini-1.5-flash";
|
|
40
|
+
return SELECTED_MODEL_PATH;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function generateFullProject(task, tests) {
|
|
44
|
+
const modelPath = await findActiveModel();
|
|
45
|
+
|
|
46
|
+
// Use v1 (stable) to avoid v1beta 404 issues
|
|
47
|
+
const url = `https://generativelanguage.googleapis.com/v1/${modelPath}:generateContent?key=${API_KEY}`;
|
|
48
|
+
|
|
49
|
+
const prompt = `
|
|
50
|
+
Generate the ENTIRE coding project in a SINGLE continuous text response.
|
|
51
|
+
Strictly pass all test cases.
|
|
24
52
|
|
|
25
53
|
### FORMATTING (STRICT):
|
|
26
54
|
Every file MUST start with this EXACT header style:
|
|
@@ -43,14 +71,23 @@ ${task}
|
|
|
43
71
|
### TESTS:
|
|
44
72
|
${tests}
|
|
45
73
|
|
|
46
|
-
###
|
|
47
|
-
|
|
74
|
+
### RULES:
|
|
75
|
+
- Minimalist logic. No extra logic.
|
|
76
|
+
- Bare minimum code to pass.
|
|
77
|
+
- No explanations.
|
|
48
78
|
`;
|
|
49
79
|
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
80
|
+
const response = await fetch(url, {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: { "Content-Type": "application/json" },
|
|
83
|
+
body: JSON.stringify({
|
|
84
|
+
contents: [{ parts: [{ text: prompt }] }]
|
|
85
|
+
})
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const data = await response.json();
|
|
89
|
+
if (!response.ok) throw new Error(data.error?.message || "Generation failed");
|
|
90
|
+
|
|
91
|
+
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || "";
|
|
55
92
|
return text.replace(/```[a-z]*\n([\s\S]*?)\n```/gi, "$1").trim();
|
|
56
93
|
}
|
package/src/runner.js
CHANGED
|
@@ -4,8 +4,7 @@ import { setApiKey, generateFullProject } from "./aiClient.js";
|
|
|
4
4
|
import { writeSingleFile } from "./fileWriter.js";
|
|
5
5
|
|
|
6
6
|
async function main() {
|
|
7
|
-
|
|
8
|
-
const apiKey = readlineSync.question("--- ");
|
|
7
|
+
const apiKey = readlineSync.question("--- ", { hideEchoBack: true });
|
|
9
8
|
if (!apiKey || apiKey.trim().length === 0) process.exit(1);
|
|
10
9
|
setApiKey(apiKey.trim());
|
|
11
10
|
|
|
@@ -28,14 +27,13 @@ async function main() {
|
|
|
28
27
|
console.log(".....");
|
|
29
28
|
const projectContent = await generateFullProject(task, tests);
|
|
30
29
|
|
|
31
|
-
if (!projectContent || projectContent.length <
|
|
32
|
-
throw new Error("AI returned
|
|
30
|
+
if (!projectContent || projectContent.length < 50) {
|
|
31
|
+
throw new Error("AI returned empty content. Check your task/tests.");
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
await writeSingleFile(process.cwd(), projectContent);
|
|
36
35
|
process.exit(0);
|
|
37
36
|
} catch (error) {
|
|
38
|
-
// Report errors explicitly instead of exiting silently
|
|
39
37
|
console.error("\n❌ Error:", error.message);
|
|
40
38
|
process.exit(1);
|
|
41
39
|
}
|