1- import { generateText } from 'ai' ;
1+ import { generateObject } from 'ai' ;
22import { Context } from 'probot' ;
33import { profileDetails , toneDetails } from '~common/consts' ;
44import { Severity } from '~common/enums' ;
55import { Handler } from '~common/interfaces' ;
6- import { CombinedLineGroup , Config , DiffLine , DiffPosition } from '~common/types' ;
6+ import { CombinedLineGroup , Config , DiffLine , DiffPosition , ReviewObject } from '~common/types' ;
77import { aiConfig } from '~configs' ;
88import { SystemError } from '~errors' ;
99import { cleanDiffString } from '~helpers' ;
10+ import { reviewObjectSchema } from '~schemas' ;
1011import { AiService , CacheService , GitHubService , QueueService , TemplateService } from '~services' ;
11- import { reviewPromptTemplate , reviewSystemTemplate } from '~templates' ;
12+ import { reviewCommentTemplate , reviewPromptTemplate , reviewSystemTemplate } from '~templates' ;
1213import { ReviewSkipConditions } from '~utils' ;
1314
1415export class ReviewHandler implements Handler < 'pull_request' > {
@@ -30,18 +31,30 @@ export class ReviewHandler implements Handler<'pull_request'> {
3031
3132 await Promise . all (
3233 fileContents . flatMap ( ( file ) => {
33- const combinedDiff = diffPositions . find ( ( diffFile ) => diffFile ? .path === file . name ) ?. positions ;
34+ const combinedDiff = diffPositions . find ( ( diffFile ) => diffFile . path === file . name ) ?. positions ;
3435
3536 if ( combinedDiff ) {
36- return Promise . all (
37- combinedDiff . map ( async ( diff ) => {
38- const result = await this . generateReport ( apiKey , config , file . content , diff . content ) ;
39-
40- return this . queueService . schedule ( ( ) =>
41- GitHubService . createInlineReviewComment ( context , result , file . name , diff . endDiffPosition )
42- ) ;
43- } )
44- ) ;
37+ return combinedDiff . map ( async ( diff ) => {
38+ const { commitableSuggestion, review, shouldReview } = await this . generateReport (
39+ apiKey ,
40+ config ,
41+ file . content ,
42+ diff . content
43+ ) ;
44+
45+ if ( ! shouldReview ) {
46+ return null ;
47+ }
48+
49+ const comment = TemplateService . render ( reviewCommentTemplate , {
50+ review,
51+ commitableSuggestion
52+ } ) ;
53+
54+ return this . queueService . schedule ( ( ) =>
55+ GitHubService . createInlineReviewComment ( context , comment , file . name , diff . endDiffPosition )
56+ ) ;
57+ } ) ;
4558 }
4659
4760 return [ ] ;
@@ -141,7 +154,7 @@ export class ReviewHandler implements Handler<'pull_request'> {
141154 return result ;
142155 }
143156
144- private async generateReport ( apiKey : string , config : Config , content : string , diff : string ) : Promise < string > {
157+ private async generateReport ( apiKey : string , config : Config , content : string , diff : string ) : Promise < ReviewObject > {
145158 const prompt = TemplateService . render ( reviewPromptTemplate , {
146159 content,
147160 diff : cleanDiffString ( diff )
@@ -154,14 +167,15 @@ export class ReviewHandler implements Handler<'pull_request'> {
154167
155168 return this . queueService . schedule ( async ( ) => {
156169 try {
157- const result = await generateText ( {
170+ const result = await generateObject < ReviewObject > ( {
158171 model : this . aiService . getModel ( aiConfig . model . incrementalReview , apiKey ) ,
159172 topP : aiConfig . topP . review ,
160173 system,
161- prompt
174+ prompt,
175+ schema : reviewObjectSchema
162176 } ) ;
163177
164- return result . text ;
178+ return result . object ;
165179 } catch ( error ) {
166180 throw new SystemError ( 'Failed to generate review' , Severity . ERROR , error ) ;
167181 }
0 commit comments