11import { OpenAI } from 'openai' ;
22
33import {
4+ type AppUtils ,
45 type EnvUtils ,
56 InputTypeEnum ,
67 type InputUtils ,
@@ -14,6 +15,7 @@ export class OpenAIProvider implements Provider {
1415 constructor (
1516 private readonly envUtils : EnvUtils ,
1617 private readonly inputUtils : InputUtils ,
18+ private readonly appUtils : AppUtils ,
1719 ) { }
1820
1921 private get client ( ) {
@@ -32,12 +34,54 @@ export class OpenAIProvider implements Provider {
3234 return this . openai ;
3335 }
3436
37+ public async setup ( ) : Promise < void > {
38+ const env = this . envUtils . get ( ) ;
39+
40+ try {
41+ const apiKey = await this . inputUtils . prompt ( {
42+ message : 'Enter your OpenAI API key:' ,
43+ default : env . OPENAI_API_KEY ,
44+ type : InputTypeEnum . Input ,
45+ } ) ;
46+
47+ const numberOfCommits = await this . inputUtils . prompt ( {
48+ default : env . OPENAI_N_COMMITS ? String ( env . OPENAI_N_COMMITS ) : '2' ,
49+ message : 'Enter the number of commits to generate:' ,
50+ type : InputTypeEnum . Input ,
51+ } ) ;
52+
53+ this . envUtils . update ( {
54+ ...env ,
55+ OPENAI_API_KEY : apiKey ,
56+ } ) ;
57+
58+ this . envUtils . update ( {
59+ ...env ,
60+ OPENAI_N_COMMITS : Number ( numberOfCommits ) ,
61+ PROVIDER : ProviderEnum . OpenAI ,
62+ OPENAI_API_KEY : apiKey ,
63+ } ) ;
64+
65+ // Ensure the OpenAI integration is working
66+ await this . test ( ) ;
67+
68+ console . log ( '\x1b[32m✓\x1b[0m' , 'OpenAI setup successfully completed.' ) ;
69+ } catch ( error ) {
70+ this . envUtils . update ( env ) ;
71+
72+ this . appUtils . logger . error ( 'Failed to set up OpenAI.' , error ) ;
73+ process . exit ( 1 ) ;
74+ }
75+ }
76+
3577 public async generateCommitMessages ( {
3678 diff,
3779 } : {
3880 diff : string ;
3981 } ) : Promise < string [ ] > {
4082 try {
83+ this . checkRequiredEnvVars ( ) ;
84+
4185 const prompt = `Generate a concise and clear commit message using the commitizen format (e.g., feat, chore, refactor, etc.) for the following code changes. The message should be at most 72 characters long:` ;
4286 const n = Number ( this . envUtils . get ( ) . OPENAI_N_COMMITS ) || 2 ;
4387
@@ -61,37 +105,24 @@ export class OpenAIProvider implements Provider {
61105
62106 return commitMessages ;
63107 } catch ( error ) {
64- console . error ( 'Failed to generate commit message' ) ;
108+ this . appUtils . logger . error (
109+ 'Failed to generate commit message.' ,
110+ error ?. error ? `\nOpenAI error:` : '' ,
111+ error ?. error ? error ?. error : '' ,
112+ ) ;
65113
66- throw error ;
114+ process . exit ( 1 ) ;
67115 }
68116 }
69117
70- public async setup ( ) : Promise < void > {
118+ private checkRequiredEnvVars ( ) : void {
71119 const env = this . envUtils . get ( ) ;
72120
73- const apiKey = await this . inputUtils . prompt ( {
74- message : 'Enter your OpenAI API key:' ,
75- default : env . OPENAI_API_KEY ,
76- type : InputTypeEnum . Input ,
77- } ) ;
78-
79- const numberOfCommits = await this . inputUtils . prompt ( {
80- message : 'Enter the number of commits to generate:' ,
81- default : String ( env . OPENAI_N_COMMITS ) || '2' ,
82- type : InputTypeEnum . Input ,
83- } ) ;
84-
85- this . envUtils . update ( {
86- ...env ,
87- OPENAI_N_COMMITS : Number ( numberOfCommits ) ,
88- PROVIDER : ProviderEnum . OpenAI ,
89- OPENAI_API_KEY : apiKey ,
90- } ) ;
91-
92- await this . test ( ) ;
93-
94- console . log ( '\x1b[32m✓\x1b[0m' , 'OpenAI setup successfully completed' ) ;
121+ if ( ! env . OPENAI_API_KEY ) {
122+ this . appUtils . logger . error ( 'OPENAI_API_KEY is required' ) ;
123+ this . appUtils . logger . log ( "Run 'commitfy setup' to set up." ) ;
124+ process . exit ( 0 ) ;
125+ }
95126 }
96127
97128 private async test ( ) : Promise < void > {
0 commit comments