@@ -24,11 +24,29 @@ import { getSettledValue } from '../../system/promise';
2424import type { ServerConnection } from '../gk/serverConnection' ;
2525import type { AIActionType , AIModel , AIModelDescriptor } from './models/model' ;
2626import type { PromptTemplateContext } from './models/promptTemplates' ;
27- import type { AIProvider } from './models/provider' ;
27+ import type { AIProvider , AIRequestResult } from './models/provider' ;
2828
2929export interface AIResult {
30- readonly summary : string ;
31- readonly body : string ;
30+ readonly id ?: string ;
31+ readonly content : string ;
32+ readonly usage ?: {
33+ readonly promptTokens ?: number ;
34+ readonly completionTokens ?: number ;
35+ readonly totalTokens ?: number ;
36+
37+ readonly limits ?: {
38+ readonly used : number ;
39+ readonly limit : number ;
40+ readonly resetsOn : Date ;
41+ } ;
42+ } ;
43+ }
44+
45+ export interface AISummarizeResult extends AIResult {
46+ readonly parsed : {
47+ readonly summary : string ;
48+ readonly body : string ;
49+ } ;
3250}
3351
3452export interface AIGenerateChangelogChange {
@@ -270,7 +288,7 @@ export class AIProviderService implements Disposable {
270288 commitOrRevision : GitRevisionReference | GitCommit ,
271289 sourceContext : Source & { type : TelemetryEvents [ 'ai/explain' ] [ 'changeType' ] } ,
272290 options ?: { cancellation ?: CancellationToken ; progress ?: ProgressOptions } ,
273- ) : Promise < AIResult | undefined > {
291+ ) : Promise < AISummarizeResult | undefined > {
274292 const diff = await this . container . git . diff ( commitOrRevision . repoPath ) . getDiff ?.( commitOrRevision . ref ) ;
275293 if ( ! diff ?. contents ) throw new Error ( 'No changes found to explain.' ) ;
276294
@@ -308,7 +326,7 @@ export class AIProviderService implements Disposable {
308326 } ) ,
309327 options ,
310328 ) ;
311- return result != null ? parseResult ( result ) : undefined ;
329+ return result != null ? { ... result , parsed : parseSummarizeResult ( result . content ) } : undefined ;
312330 }
313331
314332 async generateCommitMessage (
@@ -320,7 +338,7 @@ export class AIProviderService implements Disposable {
320338 generating ?: Deferred < AIModel > ;
321339 progress ?: ProgressOptions ;
322340 } ,
323- ) : Promise < AIResult | undefined > {
341+ ) : Promise < AISummarizeResult | undefined > {
324342 const changes : string | undefined = await this . getChanges ( changesOrRepo ) ;
325343 if ( changes == null ) return undefined ;
326344
@@ -345,7 +363,7 @@ export class AIProviderService implements Disposable {
345363 } ) ,
346364 options ,
347365 ) ;
348- return result != null ? parseResult ( result ) : undefined ;
366+ return result != null ? { ... result , parsed : parseSummarizeResult ( result . content ) } : undefined ;
349367 }
350368
351369 async generateDraftMessage (
@@ -358,7 +376,7 @@ export class AIProviderService implements Disposable {
358376 progress ?: ProgressOptions ;
359377 codeSuggestion ?: boolean ;
360378 } ,
361- ) : Promise < AIResult | undefined > {
379+ ) : Promise < AISummarizeResult | undefined > {
362380 const changes : string | undefined = await this . getChanges ( changesOrRepo ) ;
363381 if ( changes == null ) return undefined ;
364382
@@ -392,7 +410,7 @@ export class AIProviderService implements Disposable {
392410 } ) ,
393411 options ,
394412 ) ;
395- return result != null ? parseResult ( result ) : undefined ;
413+ return result != null ? { ... result , parsed : parseSummarizeResult ( result . content ) } : undefined ;
396414 }
397415
398416 async generateStashMessage (
@@ -404,7 +422,7 @@ export class AIProviderService implements Disposable {
404422 generating ?: Deferred < AIModel > ;
405423 progress ?: ProgressOptions ;
406424 } ,
407- ) : Promise < AIResult | undefined > {
425+ ) : Promise < AISummarizeResult | undefined > {
408426 const changes : string | undefined = await this . getChanges ( changesOrRepo ) ;
409427 if ( changes == null ) {
410428 options ?. generating ?. cancel ( ) ;
@@ -432,14 +450,14 @@ export class AIProviderService implements Disposable {
432450 } ) ,
433451 options ,
434452 ) ;
435- return result != null ? parseResult ( result ) : undefined ;
453+ return result != null ? { ... result , parsed : parseSummarizeResult ( result . content ) } : undefined ;
436454 }
437455
438456 async generateChangelog (
439457 changes : Lazy < Promise < AIGenerateChangelogChange [ ] > > ,
440458 source : Source ,
441459 options ?: { cancellation ?: CancellationToken ; progress ?: ProgressOptions } ,
442- ) : Promise < string | undefined > {
460+ ) : Promise < AIResult | undefined > {
443461 const result = await this . sendRequest (
444462 'generate-changelog' ,
445463 async ( ) => ( {
@@ -460,7 +478,7 @@ export class AIProviderService implements Disposable {
460478 } ) ,
461479 options ,
462480 ) ;
463- return result ;
481+ return result != null ? { ... result } : undefined ;
464482 }
465483
466484 private async sendRequest < T extends AIActionType > (
@@ -477,7 +495,7 @@ export class AIProviderService implements Disposable {
477495 generating ?: Deferred < AIModel > ;
478496 progress ?: ProgressOptions ;
479497 } ,
480- ) : Promise < string | undefined > {
498+ ) : Promise < AIRequestResult | undefined > {
481499 const { confirmed, model } = await getModelAndConfirmAIProviderToS (
482500 'diff' ,
483501 source ,
@@ -525,7 +543,7 @@ export class AIProviderService implements Disposable {
525543 ? window . withProgress ( { ...options . progress , title : getProgressTitle ( model ) } , ( ) => promise )
526544 : promise ) ;
527545
528- telementry . data [ 'output.length' ] = result ?. length ;
546+ telementry . data [ 'output.length' ] = result ?. content ?. length ;
529547 this . container . telemetry . sendEvent (
530548 telementry . key ,
531549 { ...telementry . data , duration : Date . now ( ) - start } ,
@@ -693,7 +711,7 @@ async function getModelAndConfirmAIProviderToS(
693711 }
694712}
695713
696- function parseResult ( result : string ) : AIResult {
714+ function parseSummarizeResult ( result : string ) : NonNullable < AISummarizeResult [ 'parsed' ] > {
697715 result = result . trim ( ) ;
698716 let summary = result . match ( / < s u m m a r y > \s ? ( [ \s \S ] * ?) \s ? ( < \/ s u m m a r y > | $ ) / ) ?. [ 1 ] ?. trim ( ) ?? '' ;
699717 let body = result . match ( / < b o d y > \s ? ( [ \s \S ] * ?) \s ? ( < \/ b o d y > | $ ) / ) ?. [ 1 ] ?. trim ( ) ?? '' ;
@@ -720,7 +738,7 @@ function parseResult(result: string): AIResult {
720738 return { summary : summary , body : body } ;
721739}
722740
723- function splitMessageIntoSummaryAndBody ( message : string ) : AIResult {
741+ function splitMessageIntoSummaryAndBody ( message : string ) : NonNullable < AISummarizeResult [ 'parsed' ] > {
724742 const index = message . indexOf ( '\n' ) ;
725743 if ( index === - 1 ) return { summary : message , body : '' } ;
726744
0 commit comments