-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (144 loc) · 4.38 KB
/
Copy pathindex.js
File metadata and controls
155 lines (144 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { exec } from "child_process";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import ollama from "ollama";
import prompts from "prompts";
import { systemPrompt } from "./systemPrompt.js";
import { commands } from "./commands.js";
const args = yargs(hideBin(process.argv)).option("debug", {
alias: "d",
type: "boolean",
description: "Run with debug output",
}).argv;
/**
* Executes a shell command and returns the result as a Promise.
* @param {string} command - The command to run.
* @param {string[]} [extraArgs=[]] - Additional arguments for the command.
* @returns {Promise<string>} - The result of the command.
*/
function runCommand(command, extraArgs = []) {
return new Promise((resolve, reject) => {
const shellCommand = Array.isArray(command) ? command : command.split(" ");
const fullCommand = [...shellCommand, ...extraArgs].join(" ");
if (args.debug) {
console.log("-".repeat(50));
console.log(`⚡️ Running command: ${fullCommand}`);
console.log("-".repeat(50));
}
exec(fullCommand, { encoding: "utf-8" }, (error, stdout, stderr) => {
if (error) {
console.error(`❌ Error running command: ${stderr}`);
reject(error);
} else {
resolve(stdout);
}
});
});
}
/**
* Generates a commit message using the local AI model.
* @param {string} stagedChanges - The staged changes to generate the commit message for.
* @returns {Promise<string>} - The generated commit message.
*/
async function generateCommitMessageLocalModel(stagedChanges) {
const modelName = "llama3.2:3b";
if (args.debug) {
console.log(`>>> Using Ollama with ${modelName}`);
}
try {
const stream = await ollama.chat({
model: modelName,
messages: [
{ role: "system", content: systemPrompt },
{
role: "user",
content: `Here are the staged changes '''${stagedChanges}'''`,
},
],
stream: true,
});
console.log("✨ Generating commit message...");
console.log("-".repeat(50) + "\n");
let commitMessage = "";
for await (const part of stream) {
process.stdout.write(part.message.content);
commitMessage += part.message.content;
}
console.log();
return commitMessage;
} catch (error) {
console.error(`❌ Error generating commit message: ${error.message}`);
throw error;
}
}
/**
* Handles the user interaction loop for committing changes.
* @param {string} stagedChanges - The staged changes to generate the commit message for.
*/
async function interactionLoop(stagedChanges) {
while (true) {
try {
const commitMessage = await generateCommitMessageLocalModel(
stagedChanges
);
const response = await prompts({
type: "text",
name: "action",
message: "\n\nProceed to commit? [y(yes) | n(no) | r(regenerate)]",
});
switch (response.action) {
case "r":
case "regenerate":
const clearScreen = await runCommand(commands["clearScreen"]);
console.log(`${clearScreen}`);
continue;
case "y":
case "yes":
console.log("committing...");
const res = await runCommand(`${commands.commit} "${commitMessage}"`);
console.log(`\n${res}\n✨ Committed!`);
break;
case "n":
case "no":
console.log("\n❌ Discarding AI commit message.");
break;
default:
console.log("\n🤖 Invalid action");
break;
}
break;
} catch (error) {
console.error(`❌ Error during interaction loop: ${error.message}`);
break;
}
}
}
/**
* Main function to run the AI commit generator.
*/
async function run() {
try {
const isGitRepo = await runCommand(commands.isGitRepo);
if (!isGitRepo) {
console.log("\n\n🐙 Not a git repo.");
return;
}
const stagedChanges = await runCommand(commands.getStashedChanges);
if (args.debug) {
console.log(stagedChanges);
console.log("-".repeat(50));
}
if (!stagedChanges) {
console.log("\n🗂️ No staged changes");
return;
}
await interactionLoop(stagedChanges);
} catch (error) {
if (error instanceof Error && error.message.includes("SIGINT")) {
console.log("\n\n❌ AI commit exited.");
} else {
console.error(`❌ Unexpected error: ${error.message}`);
}
}
}
run();