@@ -19,7 +19,7 @@ import { CODING_AGENT, CODING_AGENT_AUTO_COMMIT_AND_PUSH } from '../common/setti
1919import { ITelemetry } from '../common/telemetry' ;
2020import { toOpenPullRequestWebviewUri } from '../common/uri' ;
2121import { copilotPRStatusToSessionStatus , IAPISessionLogs , ICopilotRemoteAgentCommandArgs , ICopilotRemoteAgentCommandResponse , OctokitCommon , RemoteAgentResult , RepoInfo } from './common' ;
22- import { ChatSessionWithPR , CopilotApi , getCopilotApi , RemoteAgentJobPayload , SessionInfo , SessionSetupStep } from './copilotApi' ;
22+ import { ChatSessionWithPR , CopilotApi , getCopilotApi , JobInfo , RemoteAgentJobPayload , SessionInfo , SessionSetupStep } from './copilotApi' ;
2323import { CodingAgentPRAndStatus , CopilotPRWatcher , CopilotStateModel } from './copilotPrWatcher' ;
2424import { ChatSessionContentBuilder } from './copilotRemoteAgent/chatSessionContentBuilder' ;
2525import { GitOperationsManager } from './copilotRemoteAgent/gitOperationsManager' ;
@@ -94,7 +94,6 @@ export class CopilotRemoteAgentManager extends Disposable {
9494 historySummaryLength : historySummary ?. length . toString ( ) || '0' ,
9595 source,
9696 } ) ;
97- stream . progress ( vscode . l10n . t ( 'Delegating to coding agent' ) ) ;
9897 const result = await this . invokeRemoteAgent (
9998 promptSummary || prompt ,
10099 [
@@ -103,6 +102,7 @@ export class CopilotRemoteAgentManager extends Disposable {
103102 ] . join ( '\n\n' ) . trim ( ) ,
104103 token ,
105104 false ,
105+ stream ,
106106 ) ;
107107 if ( result . state !== 'success' ) {
108108 Logger . error ( `Failed to provide new chat session item: ${ result . error } ` , CopilotRemoteAgentManager . ID ) ;
@@ -194,7 +194,7 @@ export class CopilotRemoteAgentManager extends Disposable {
194194 stream . markdown ( result ) ;
195195 stream . markdown ( '\n\n' ) ;
196196
197- stream . progress ( vscode . l10n . t ( 'Waiting for coding agent to respond ' ) ) ;
197+ stream . progress ( vscode . l10n . t ( 'Attaching to session ' ) ) ;
198198
199199 // Wait for new session and stream its progress
200200 const newSession = await this . waitForNewSession ( pullRequest , stream , token , true ) ;
@@ -705,7 +705,7 @@ export class CopilotRemoteAgentManager extends Disposable {
705705 return vscode . l10n . t ( '🚀 Coding agent will continue work in [#{0}]({1}). Track progress [here]({2}).' , number , link , webviewUri . toString ( ) ) ;
706706 }
707707
708- async invokeRemoteAgent ( prompt : string , problemContext ?: string , token ?: vscode . CancellationToken , autoPushAndCommit = true ) : Promise < RemoteAgentResult > {
708+ async invokeRemoteAgent ( prompt : string , problemContext ?: string , token ?: vscode . CancellationToken , autoPushAndCommit = true , chatStream ?: vscode . ChatResponseStream ) : Promise < RemoteAgentResult > {
709709 const capiClient = await this . copilotApi ;
710710 if ( ! capiClient ) {
711711 return { error : vscode . l10n . t ( 'Failed to initialize Copilot API' ) , state : 'error' } ;
@@ -731,6 +731,7 @@ export class CopilotRemoteAgentManager extends Disposable {
731731 return { error : vscode . l10n . t ( 'Uncommitted changes detected. Please commit or stash your changes before starting the remote agent. Enable \'{0}\' to push your changes automatically.' , CODING_AGENT_AUTO_COMMIT_AND_PUSH ) , state : 'error' } ;
732732 }
733733 try {
734+ chatStream ?. progress ( vscode . l10n . t ( 'Waiting for local changes' ) ) ;
734735 head_ref = await this . gitOperationsManager . commitAndPushChanges ( repoInfo ) ;
735736 } catch ( error ) {
736737 return { error : error . message , state : 'error' } ;
@@ -755,6 +756,7 @@ export class CopilotRemoteAgentManager extends Disposable {
755756 const { problemStatement, isTruncated } = truncatePrompt ( prompt , problemContext ) ;
756757
757758 if ( isTruncated ) {
759+ chatStream ?. progress ( vscode . l10n . t ( 'Truncating context' ) ) ;
758760 const truncationResult = await vscode . window . showWarningMessage (
759761 vscode . l10n . t ( 'Prompt size exceeded' ) , { modal : true , detail : vscode . l10n . t ( 'Your prompt will be truncated to fit within coding agent\'s context window. This may affect the quality of the response.' ) } , CONTINUE_TRUNCATION ) ;
760762 const userCancelled = token ?. isCancellationRequested || ! truncationResult || truncationResult !== CONTINUE_TRUNCATION ;
@@ -784,19 +786,36 @@ export class CopilotRemoteAgentManager extends Disposable {
784786 } ;
785787
786788 try {
787- const { pull_request, session_id } = await capiClient . postRemoteAgentJob ( owner , repo , payload , isTruncated ) ;
788- this . _onDidCreatePullRequest . fire ( pull_request . number ) ;
789- const webviewUri = await toOpenPullRequestWebviewUri ( { owner, repo, pullRequestNumber : pull_request . number } ) ;
789+ chatStream ?. progress ( vscode . l10n . t ( 'Delegating to coding agent' ) ) ;
790+ const response = await capiClient . postRemoteAgentJob ( owner , repo , payload , isTruncated ) ;
791+
792+ // For v1 API, we need to fetch the job details to get the PR info
793+ // Since the PR might not be created immediately, we need to poll for it
794+ chatStream ?. progress ( vscode . l10n . t ( 'Creating pull request' ) ) ;
795+ const jobInfo = await this . waitForJobWithPullRequest ( capiClient , owner , repo , response . job_id , token ) ;
796+ if ( ! jobInfo || ! jobInfo . pull_request ) {
797+ return { error : vscode . l10n . t ( 'Failed to retrieve pull request information from job' ) , state : 'error' } ;
798+ }
799+
800+ const { number } = jobInfo . pull_request ;
801+ this . _onDidCreatePullRequest . fire ( number ) ;
802+
803+ // Find the actual PR to get the HTML URL
804+ const pullRequest = await this . findPullRequestById ( number , true ) ;
805+ const htmlUrl = pullRequest ?. html_url || `https://github.com/${ owner } /${ repo } /pull/${ number } ` ;
806+
807+ const webviewUri = await toOpenPullRequestWebviewUri ( { owner, repo, pullRequestNumber : number } ) ;
790808 const prLlmString = `The remote agent has begun work and has created a pull request. Details about the pull request are being shown to the user. If the user wants to track progress or iterate on the agent's work, they should use the pull request.` ;
791809
792- await this . waitForQueuedToInProgress ( session_id , token ) ;
810+ chatStream ?. progress ( vscode . l10n . t ( 'Attaching to session' ) ) ;
811+ await this . waitForQueuedToInProgress ( response . session_id , token ) ;
793812 return {
794813 state : 'success' ,
795- number : pull_request . number ,
796- link : pull_request . html_url ,
814+ number,
815+ link : htmlUrl ,
797816 webviewUri,
798817 llmDetails : head_ref ? `Local pending changes have been pushed to branch '${ head_ref } '. ${ prLlmString } ` : prLlmString ,
799- sessionId : session_id
818+ sessionId : response . session_id
800819 } ;
801820 } catch ( error ) {
802821 return { error : error . message , state : 'error' } ;
@@ -1585,6 +1604,32 @@ export class CopilotRemoteAgentManager extends Disposable {
15851604 this . _stateModel . clear ( ) ;
15861605 }
15871606
1607+ private async waitForJobWithPullRequest (
1608+ capiClient : CopilotApi ,
1609+ owner : string ,
1610+ repo : string ,
1611+ jobId : string ,
1612+ token ?: vscode . CancellationToken
1613+ ) : Promise < JobInfo | undefined > {
1614+ const maxWaitTime = 30 * 1000 ; // 30 seconds
1615+ const pollInterval = 2000 ; // 2 seconds
1616+ const startTime = Date . now ( ) ;
1617+
1618+ Logger . appendLine ( `Waiting for job ${ jobId } to have pull request information...` , CopilotRemoteAgentManager . ID ) ;
1619+
1620+ while ( Date . now ( ) - startTime < maxWaitTime && ( ! token || ! token . isCancellationRequested ) ) {
1621+ const jobInfo = await capiClient . getJobByJobId ( owner , repo , jobId ) ;
1622+ if ( jobInfo && jobInfo . pull_request && jobInfo . pull_request . number ) {
1623+ Logger . appendLine ( `Job ${ jobId } now has pull request #${ jobInfo . pull_request . number } ` , CopilotRemoteAgentManager . ID ) ;
1624+ return jobInfo ;
1625+ }
1626+ await new Promise ( resolve => setTimeout ( resolve , pollInterval ) ) ;
1627+ }
1628+
1629+ Logger . warn ( `Timed out waiting for job ${ jobId } to have pull request information` , CopilotRemoteAgentManager . ID ) ;
1630+ return undefined ;
1631+ }
1632+
15881633 public async cancelMostRecentChatSession ( pullRequest : PullRequestModel ) : Promise < void > {
15891634 const capi = await this . copilotApi ;
15901635 if ( ! capi ) {
0 commit comments