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 a concise, conventional Git commit message for this diff. Return ONLY the commit message:\n\n${diff}`;
13
- const res = await fetch("http://localhost:11434/api/generate", {
14
- method: "POST",
15
- headers: { "Content-Type": "application/json" },
16
- body: JSON.stringify({
17
- model: "llama3",
18
- prompt: prompt,
19
- stream: false
20
- }),
21
- });
22
- const { response } = await res.json();
23
- if (response.error) {
24
- throw new Error(`LLM error: ${response.error}`);
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
- let message = response?.trim();
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" }).trim();
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 msg command:', err.message);
87
+ console.error('āŒ Error in commit message suggestion:', err.message);
43
88
  }
44
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scai",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "scai": "./dist/index.js"
@@ -24,6 +24,7 @@
24
24
  "typescript": "^5.8.3"
25
25
  },
26
26
  "files": [
27
- "dist/"
27
+ "dist/",
28
+ "README.md"
28
29
  ]
29
30
  }