@@ -108,6 +108,8 @@ export interface AIModelChangeEvent {
108108 readonly model : AIModel | undefined ;
109109}
110110
111+ export type AIExplainSource = Source & { type : TelemetryEvents [ 'ai/explain' ] [ 'changeType' ] } ;
112+
111113// Order matters for sorting the picker
112114const supportedAIProviders = new Map < AIProviders , AIProviderDescriptorWithType > ( [
113115 [
@@ -490,14 +492,11 @@ export class AIProviderService implements Disposable {
490492
491493 async explainCommit (
492494 commitOrRevision : GitRevisionReference | GitCommit ,
493- sourceContext : Source & { type : TelemetryEvents [ 'ai/explain' ] [ 'changeType' ] } ,
495+ sourceContext : AIExplainSource ,
494496 options ?: { cancellation ?: CancellationToken ; progress ?: ProgressOptions } ,
495497 ) : Promise < AISummarizeResult | undefined > {
496- const { type, ...source } = sourceContext ;
497-
498- const result = await this . sendRequest (
499- 'explain-changes' ,
500- async ( model , reporting , cancellation , maxInputTokens , retries ) => {
498+ return this . explainChanges (
499+ async cancellation => {
501500 const diff = await this . container . git . diff ( commitOrRevision . repoPath ) . getDiff ?.( commitOrRevision . ref ) ;
502501 if ( ! diff ?. contents ) throw new AINoRequestDataError ( 'No changes found to explain.' ) ;
503502 if ( cancellation . isCancellationRequested ) throw new CancellationError ( ) ;
@@ -511,18 +510,45 @@ export class AIProviderService implements Disposable {
511510 if ( ! commit . hasFullDetails ( ) ) {
512511 await commit . ensureFullDetails ( ) ;
513512 assertsCommitHasFullDetails ( commit ) ;
514-
515513 if ( cancellation . isCancellationRequested ) throw new CancellationError ( ) ;
516514 }
517515
516+ return {
517+ diff : diff . contents ,
518+ message : commit . message ,
519+ } ;
520+ } ,
521+ sourceContext ,
522+ options ,
523+ ) ;
524+ }
525+
526+ async explainChanges (
527+ promptContext :
528+ | PromptTemplateContext < 'explain-changes' >
529+ | ( ( cancellationToken : CancellationToken ) => Promise < PromptTemplateContext < 'explain-changes' > > ) ,
530+ sourceContext : AIExplainSource ,
531+ options ?: { cancellation ?: CancellationToken ; progress ?: ProgressOptions } ,
532+ ) : Promise < AISummarizeResult | undefined > {
533+ const { type, ...source } = sourceContext ;
534+
535+ const result = await this . sendRequest (
536+ 'explain-changes' ,
537+ async ( model , reporting , cancellation , maxInputTokens , retries ) => {
538+ if ( typeof promptContext === 'function' ) {
539+ promptContext = await promptContext ( cancellation ) ;
540+ }
541+
542+ promptContext . instructions = `${
543+ promptContext . instructions ? `${ promptContext . instructions } \n` : ''
544+ } ${ configuration . get ( 'ai.explainChanges.customInstructions' ) } `;
545+
546+ if ( cancellation . isCancellationRequested ) throw new CancellationError ( ) ;
547+
518548 const { prompt } = await this . getPrompt (
519549 'explain-changes' ,
520550 model ,
521- {
522- diff : diff . contents ,
523- message : commit . message ,
524- instructions : configuration . get ( 'ai.explainChanges.customInstructions' ) ,
525- } ,
551+ promptContext ,
526552 maxInputTokens ,
527553 retries ,
528554 reporting ,
0 commit comments