@@ -21,54 +21,70 @@ export class ChatSessionContentBuilder {
2121 constructor (
2222 private loggerId : string ,
2323 private readonly handler : string ,
24- private getChangeModels : ( ) => Promise < ( RemoteFileChangeModel | InMemFileChangeModel ) [ ] >
24+ private getChangeModels : Promise < ( RemoteFileChangeModel | InMemFileChangeModel ) [ ] >
2525 ) { }
2626
2727 public async buildSessionHistory (
2828 sessions : SessionInfo [ ] ,
2929 pullRequest : PullRequestModel ,
30- capi : CopilotApi
30+ capi : CopilotApi ,
31+ timelineEventsPromise : Promise < TimelineEvent [ ] >
3132 ) : Promise < Array < vscode . ChatRequestTurn | vscode . ChatResponseTurn2 > > {
3233 const sortedSessions = sessions . slice ( ) . sort ( ( a , b ) =>
3334 new Date ( a . created_at ) . getTime ( ) - new Date ( b . created_at ) . getTime ( )
3435 ) ;
3536
36- const history : Array < vscode . ChatRequestTurn | vscode . ChatResponseTurn2 > = [ ] ;
37- const timelineEvents = await pullRequest . getTimelineEvents ( ) ;
37+ // Process all sessions concurrently while maintaining order
38+ const sessionResults = await Promise . all (
39+ sortedSessions . map ( async ( session , sessionIndex ) => {
40+ const firstHistoryEntry = async ( ) => {
41+ const sessionPrompt = await this . determineSessionPrompt ( session , sessionIndex , pullRequest , timelineEventsPromise , capi ) ;
42+
43+ // Create request turn for this session
44+ const sessionRequest = new vscode . ChatRequestTurn2 (
45+ sessionPrompt ,
46+ undefined , // command
47+ [ ] , // references
48+ COPILOT_SWE_AGENT ,
49+ [ ] , // toolReferences
50+ [ ]
51+ ) ;
52+ return sessionRequest ;
53+ } ;
54+ const secondHistoryEntry = async ( ) => {
55+ const logs = await capi . getLogsFromSession ( session . id ) ;
56+ // Create response turn
57+ const responseHistory = await this . createResponseTurn ( pullRequest , logs , session ) ;
58+ return responseHistory ;
59+ } ;
60+ const [ first , second ] = await Promise . all ( [
61+ firstHistoryEntry ( ) ,
62+ secondHistoryEntry ( ) ,
63+ ] ) ;
64+
65+ return { first, second, sessionIndex } ;
66+ } )
67+ ) ;
3868
39- Logger . appendLine ( `Found ${ timelineEvents . length } timeline events` , this . loggerId ) ;
69+ const history : Array < vscode . ChatRequestTurn | vscode . ChatResponseTurn2 > = [ ] ;
4070
41- for ( const [ sessionIndex , session ] of sortedSessions . entries ( ) ) {
42- const logs = await capi . getLogsFromSession ( session . id ) ;
43- const sessionPrompt = await this . determineSessionPrompt ( session , sessionIndex , pullRequest , timelineEvents , capi ) ;
44-
45- // Create request turn for this session
46- const sessionRequest = new vscode . ChatRequestTurn2 (
47- sessionPrompt ,
48- undefined , // command
49- [ ] , // references
50- COPILOT_SWE_AGENT ,
51- [ ] , // toolReferences
52- [ ]
53- ) ;
54- history . push ( sessionRequest ) ;
71+ // Build history array in the correct order
72+ for ( const { first, second, sessionIndex } of sessionResults ) {
73+ history . push ( first ) ;
5574
56- // Create response turn
57- const responseHistory = await this . createResponseTurn ( pullRequest , logs , session ) ;
58- if ( responseHistory ) {
75+ if ( second ) {
5976 // if this is the first response, then also add the PR card
60- if ( history . length === 1 ) {
77+ if ( sessionIndex === 0 ) {
6178 const uri = await toOpenPullRequestWebviewUri ( { owner : pullRequest . remote . owner , repo : pullRequest . remote . repositoryName , pullRequestNumber : pullRequest . number } ) ;
6279 const plaintextBody = marked . parse ( pullRequest . body , { renderer : new PlainTextRenderer ( ) , } ) . trim ( ) ;
6380
6481 const card = new vscode . ChatResponsePullRequestPart ( uri , pullRequest . title , plaintextBody , pullRequest . author . specialDisplayName ?? pullRequest . author . login , `#${ pullRequest . number } ` ) ;
6582 const cardTurn = new vscode . ChatResponseTurn2 ( [ card ] , { } , COPILOT_SWE_AGENT ) ;
6683 history . push ( cardTurn ) ;
6784 }
68- history . push ( responseHistory ) ;
85+ history . push ( second ) ;
6986 }
7087 }
71-
7288 return history ;
7389 }
7490
@@ -92,15 +108,15 @@ export class ChatSessionContentBuilder {
92108 session : SessionInfo ,
93109 sessionIndex : number ,
94110 pullRequest : PullRequestModel ,
95- timelineEvents : readonly TimelineEvent [ ] ,
111+ timelineEventsPromise : Promise < TimelineEvent [ ] > ,
96112 capi : CopilotApi
97113 ) : Promise < string > {
98114 let sessionPrompt = session . name || `Session ${ sessionIndex + 1 } (ID: ${ session . id } )` ;
99115
100116 if ( sessionIndex === 0 ) {
101117 sessionPrompt = await this . getInitialSessionPrompt ( session , pullRequest , capi , sessionPrompt ) ;
102118 } else {
103- sessionPrompt = await this . getFollowUpSessionPrompt ( sessionIndex , timelineEvents , sessionPrompt ) ;
119+ sessionPrompt = await this . getFollowUpSessionPrompt ( sessionIndex , timelineEventsPromise , sessionPrompt ) ;
104120 }
105121
106122 // TODO: @rebornix , remove @copilot prefix from session prompt for now
@@ -110,9 +126,11 @@ export class ChatSessionContentBuilder {
110126
111127 private async getFollowUpSessionPrompt (
112128 sessionIndex : number ,
113- timelineEvents : readonly TimelineEvent [ ] ,
129+ timelineEventsPromise : Promise < TimelineEvent [ ] > ,
114130 defaultPrompt : string
115131 ) : Promise < string > {
132+ const timelineEvents = await timelineEventsPromise ;
133+ Logger . appendLine ( `Found ${ timelineEvents . length } timeline events` , this . loggerId ) ;
116134 const copilotStartedEvents = timelineEvents
117135 . filter ( ( event ) : event is CopilotStartedEvent => event . event === EventType . CopilotStarted )
118136 . sort ( ( a , b ) => new Date ( a . createdAt ) . getTime ( ) - new Date ( b . createdAt ) . getTime ( ) ) ;
@@ -395,7 +413,7 @@ export class ChatSessionContentBuilder {
395413
396414 private async getFileChangesMultiDiffPart ( pullRequest : PullRequestModel ) : Promise < vscode . ChatResponseMultiDiffPart | undefined > {
397415 try {
398- const changeModels = await this . getChangeModels ( ) ;
416+ const changeModels = await this . getChangeModels ;
399417
400418 if ( changeModels . length === 0 ) {
401419 return undefined ;
0 commit comments