scai 0.1.3 ā 0.1.5
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.
|
@@ -1,4 +1,50 @@
|
|
|
1
1
|
import { execSync } from 'child_process';
|
|
2
|
+
import readline from 'readline';
|
|
3
|
+
function askUserToChoose(suggestions) {
|
|
4
|
+
return new Promise((resolve) => {
|
|
5
|
+
console.log('\nš” AI-suggested commit messages:\n');
|
|
6
|
+
suggestions.forEach((msg, i) => {
|
|
7
|
+
console.log(`${i + 1}) ${msg}`);
|
|
8
|
+
});
|
|
9
|
+
console.log(`${suggestions.length + 1}) š Regenerate suggestions`);
|
|
10
|
+
const rl = readline.createInterface({
|
|
11
|
+
input: process.stdin,
|
|
12
|
+
output: process.stdout,
|
|
13
|
+
});
|
|
14
|
+
rl.question(`\nš Choose a commit message [1-${suggestions.length + 1}]: `, (answer) => {
|
|
15
|
+
rl.close();
|
|
16
|
+
const choice = parseInt(answer, 10);
|
|
17
|
+
if (isNaN(choice) || choice < 1 || choice > suggestions.length + 1) {
|
|
18
|
+
console.log('ā ļø Invalid selection. Using the first suggestion by default.');
|
|
19
|
+
resolve(0);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
resolve(choice - 1); // Return 0-based index (0 to 3)
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async function generateSuggestions(prompt) {
|
|
28
|
+
const res = await fetch("http://localhost:11434/api/generate", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: { "Content-Type": "application/json" },
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
model: "llama3",
|
|
33
|
+
prompt,
|
|
34
|
+
stream: false,
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
const { response } = await res.json();
|
|
38
|
+
if (!response || typeof response !== 'string') {
|
|
39
|
+
throw new Error('Invalid LLM response');
|
|
40
|
+
}
|
|
41
|
+
const lines = response.trim().split('\n').filter(line => /^\d+\.\s+/.test(line));
|
|
42
|
+
const messages = lines.map(line => line.replace(/^\d+\.\s+/, '').replace(/^"(.*)"$/, '$1').trim());
|
|
43
|
+
if (messages.length === 0) {
|
|
44
|
+
throw new Error('No valid commit messages found in LLM response.');
|
|
45
|
+
}
|
|
46
|
+
return messages;
|
|
47
|
+
}
|
|
2
48
|
export async function suggestCommitMessage(options) {
|
|
3
49
|
try {
|
|
4
50
|
let diff = execSync("git diff", { encoding: "utf-8" }).trim();
|
|
@@ -9,36 +55,35 @@ export async function suggestCommitMessage(options) {
|
|
|
9
55
|
console.log('ā ļø No staged changes to suggest a message for.');
|
|
10
56
|
return;
|
|
11
57
|
}
|
|
12
|
-
const prompt = `Suggest
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
58
|
+
const prompt = `Suggest 3 concise, conventional Git commit message options for this diff. Return ONLY the commit messages, numbered 1 to 3, like so:
|
|
59
|
+
1. ...
|
|
60
|
+
2. ...
|
|
61
|
+
3. ...
|
|
62
|
+
|
|
63
|
+
Here is the diff:
|
|
64
|
+
${diff}`;
|
|
65
|
+
let message = null;
|
|
66
|
+
while (message === null) {
|
|
67
|
+
const suggestions = await generateSuggestions(prompt);
|
|
68
|
+
const choiceIndex = await askUserToChoose(suggestions);
|
|
69
|
+
if (choiceIndex === suggestions.length) {
|
|
70
|
+
// User chose "Regenerate"
|
|
71
|
+
console.log('\nš Regenerating suggestions...\n');
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
message = suggestions[choiceIndex];
|
|
25
75
|
}
|
|
26
|
-
|
|
27
|
-
console.log(`${message}`);
|
|
28
|
-
// Remove double quotes from the message
|
|
29
|
-
message = message.replace(/^"(.*)"$/, '$1');
|
|
30
|
-
// 3) Optionally commit
|
|
76
|
+
console.log(`\nā
Selected commit message:\n${message}\n`);
|
|
31
77
|
if (options.commit) {
|
|
32
|
-
// If code not already staged
|
|
33
78
|
const commitDiff = execSync("git diff", { encoding: "utf-8" }).trim();
|
|
34
79
|
if (commitDiff) {
|
|
35
|
-
execSync("git add .", { encoding: "utf-8" })
|
|
80
|
+
execSync("git add .", { encoding: "utf-8" });
|
|
36
81
|
}
|
|
37
82
|
execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { stdio: 'inherit' });
|
|
38
83
|
console.log('ā
Committed with AI-suggested message.');
|
|
39
84
|
}
|
|
40
85
|
}
|
|
41
86
|
catch (err) {
|
|
42
|
-
console.error('ā Error in
|
|
87
|
+
console.error('ā Error in commit message suggestion:', err.message);
|
|
43
88
|
}
|
|
44
89
|
}
|
package/package.json
CHANGED