@@ -7,24 +7,72 @@ dotenv.config();
77
88const openai = new OpenAI ( process . env . OPENAI_API_KEY ) ;
99
10- async function getGitDiff ( ) {
10+ // remove any staged changes in git
11+ async function initGit ( ) {
1112 try {
12- const { stdout : gitDiff } = await execa ( 'git' , [ 'diff' ] ) ;
13-
14- const completion = await openai . chat . completions . create ( {
15- messages : [
16- {
17- role : "system" ,
18- content : config . prompt ,
19- } ,
20- { role : "user" , content : gitDiff } ,
21- ] ,
22- model : "gpt-3.5-turbo-0125"
23- } ) ;
24- console . log ( completion . choices [ 0 ] . message . content ) ;
13+ await execa ( 'git' , [ 'restore' , '--staged' , '.' ] ) ;
2514 } catch ( error ) {
26- console . error ( error ) ;
15+ console . error ( error ) ;
2716 }
2817}
2918
30- export default getGitDiff ;
19+ // git status to see if there are any changes
20+ // if there's any changes add the first file in the list of changes
21+ async function gitStatus ( ) {
22+ try {
23+ const { stdout : status } = await execa ( 'git' , [ 'status' , '--porcelain' ] ) ;
24+ if ( status ) {
25+ // get the first file path in the list of changes
26+ const lines = status . split ( '\n' ) ;
27+ const filePaths = lines
28+ . map ( line => line . split ( ' ' ) . slice ( 2 ) . join ( ' ' ) . trim ( ) )
29+ . filter ( filePath => filePath !== '' )
30+ . concat ( lines
31+ . filter ( line => line . startsWith ( '??' ) )
32+ . map ( line => line . split ( ' ' ) . slice ( 1 ) . join ( ' ' ) . trim ( ) )
33+ ) ;
34+ // git add the first file in the list of changes
35+ await execa ( 'git' , [ 'add' , filePaths [ 0 ] ] ) ;
36+ console . log ( `${ filePaths [ 0 ] } has been added to the staging area.` ) ;
37+ }
38+ } catch ( error ) {
39+ console . error ( error ) ;
40+ }
41+ }
42+
43+ // get the diff of the staged changes
44+ async function gitDiff ( ) {
45+ try {
46+ const { stdout : gitDiff } = await execa ( 'git' , [ 'diff' , '--staged' ] ) ;
47+ return gitDiff ;
48+ } catch ( error ) {
49+ console . error ( error ) ;
50+ }
51+ }
52+
53+ async function generatePrompt ( ) {
54+ // get the staged changes
55+ await initGit ( ) ;
56+ await gitStatus ( ) ;
57+ const gitDiffContent = await gitDiff ( ) ;
58+
59+ // use the prompt from the config file emoji and send to openai
60+ const category = await openai . chat . completions . create ( {
61+ messages : [
62+ { role : "system" , content : config . emoji } ,
63+ { role : "user" , content : gitDiffContent } ,
64+ ] ,
65+ model : "gpt-3.5-turbo"
66+ } ) ;
67+ // use the prmopt from the config file message and send to openai
68+ const message = await openai . chat . completions . create ( {
69+ messages : [
70+ { role : "system" , content : config . message } ,
71+ { role : "user" , content : gitDiffContent } ,
72+ ] ,
73+ model : "gpt-3.5-turbo"
74+ } ) ;
75+ return `${ category . choices [ 0 ] . message . content } : ${ message . choices [ 0 ] . message . content } ` ;
76+ }
77+
78+ export default generatePrompt ;
0 commit comments