forked from TrafficGuard/typedai
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommit.ts
More file actions
66 lines (50 loc) · 1.8 KB
/
commit.ts
File metadata and controls
66 lines (50 loc) · 1.8 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
import '#fastify/trace-init/trace-init';
import { initApplicationContext } from '#app/applicationContext';
import { shutdownTrace } from '#fastify/trace-init/trace-init';
import { Git } from '#functions/scm/git';
import { FileSystemRead } from '#functions/storage/fileSystemRead';
import { defaultLLMs } from '#llm/services/defaultLlms';
async function main() {
await initApplicationContext();
console.log('Commit command starting...');
const git = new Git();
const stagedFiles = await git.getStagedFiles();
if (stagedFiles.length === 0) {
console.log('No staged files to commit.');
await shutdownTrace();
return;
}
let gitDiff = await git.getStagedDiff();
if (gitDiff.length > 10000) {
gitDiff = gitDiff.substring(0, 10000);
}
const fsRead = new FileSystemRead();
const fileContentsArray = await Promise.all(stagedFiles.map((file) => fsRead.readFile(file)));
const fileContents = stagedFiles.map((path, index) => `<file path="${path}">${fileContentsArray[index]}</file>`).join('\n');
const { medium } = defaultLLMs();
const prompt = `
Based on the following file contents and git diff, generate a conventional commit message.
<file_contents>
${fileContents}
</file_contents>
<git_diff>
${gitDiff}
</git_diff>
Return the result as a JSON object with "title" and "description" keys, wrapped in <json> tags.
`;
const result = await medium.generateText(prompt);
const jsonResult = result.match(/<json>([\s\S]*?)<\/json>/);
if (jsonResult?.[1]) {
const commitMessage = JSON.parse(jsonResult[1]);
console.log('Title:', commitMessage.title);
console.log('Description:', commitMessage.description);
} else {
console.log('Could not parse commit message from LLM response:');
console.log(result);
}
await shutdownTrace();
}
main().then(
() => console.log('done'),
(e) => console.error(e),
);