@@ -2,9 +2,11 @@ import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods';
22import { generateObject } from 'ai' ;
33import { Context } from 'probot' ;
44import { toneDetails } from '~common/consts' ;
5+ import { Severity } from '~common/enums' ;
56import { Handler } from '~common/interfaces' ;
67import { Config , FileContent , SummaryObject } from '~common/types' ;
78import { aiConfig , appConfig } from '~configs' ;
9+ import { SystemError } from '~errors' ;
810import { summaryObjectSchema } from '~schemas' ;
911import { AiService , CacheService , GitHubService , QueueService , TemplateService } from '~services' ;
1012import { summaryCommentTemplate , summaryPromptTemplate , summarySystemTemplate } from '~templates' ;
@@ -25,7 +27,7 @@ export class SummaryHandler implements Handler<'pull_request'> {
2527 }
2628
2729 const fileContents = await this . queueService . schedule ( ( ) => GitHubService . getPullRequestFilesContent ( context ) ) ;
28- const summaryObject = await this . generateFileContexts ( apiKey , config , fileContents ) ;
30+ const summaryObject = await this . generateSummaryObject ( apiKey , config , fileContents ) ;
2931 const summary = this . generateMarkdown ( summaryObject . data ) ;
3032 const existingComment = await this . findExistingComment ( context ) ;
3133
@@ -75,30 +77,34 @@ export class SummaryHandler implements Handler<'pull_request'> {
7577 . join ( '\n' ) ;
7678 }
7779
78- private async generateFileContexts (
80+ private async generateSummaryObject (
7981 apiKey : string ,
8082 config : Config ,
8183 fileContents : FileContent [ ]
8284 ) : Promise < SummaryObject > {
83- const filesContext = fileContents . map ( ( file ) => file . path + ': ' + file . content ) . join ( '\n' ) ;
85+ const filesContext = JSON . stringify ( fileContents ) ;
8486
85- const result = await this . queueService . schedule ( ( ) =>
86- generateObject < SummaryObject > ( {
87- topP : aiConfig . topP . summary ,
88- model : this . aiService . getModel ( aiConfig . model . summary , apiKey , true ) ,
89- system : TemplateService . render ( summarySystemTemplate , {
90- tone : toneDetails [ config . summary . tone ] ,
91- language : config . language
92- } ) ,
93- prompt : TemplateService . render ( summaryPromptTemplate , {
94- tone : toneDetails [ config . summary . tone ] ,
95- language : config . language ,
96- filesContext
97- } ) ,
98- schema : summaryObjectSchema
99- } )
100- ) ;
87+ try {
88+ const result = await this . queueService . schedule ( ( ) =>
89+ generateObject < SummaryObject > ( {
90+ topP : aiConfig . topP . summary ,
91+ model : this . aiService . getModel ( aiConfig . model . summary , apiKey , true ) ,
92+ system : TemplateService . render ( summarySystemTemplate , {
93+ tone : toneDetails [ config . summary . tone ] ,
94+ language : config . language
95+ } ) ,
96+ prompt : TemplateService . render ( summaryPromptTemplate , {
97+ tone : toneDetails [ config . summary . tone ] ,
98+ language : config . language ,
99+ filesContext
100+ } ) ,
101+ schema : summaryObjectSchema
102+ } )
103+ ) ;
101104
102- return result . object ;
105+ return result . object ;
106+ } catch ( error ) {
107+ throw new SystemError ( 'Failed to generate summary object' , Severity . ERROR , error ) ;
108+ }
103109 }
104110}
0 commit comments