@@ -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' ;
@@ -784,19 +784,33 @@ export class CopilotRemoteAgentManager extends Disposable {
784784 } ;
785785
786786 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 } ) ;
787+ const response = await capiClient . postRemoteAgentJob ( owner , repo , payload , isTruncated ) ;
788+
789+ // For v1 API, we need to fetch the job details to get the PR info
790+ // Since the PR might not be created immediately, we need to poll for it
791+ const jobInfo = await this . waitForJobWithPullRequest ( capiClient , owner , repo , response . job_id , token ) ;
792+ if ( ! jobInfo || ! jobInfo . pull_request ) {
793+ return { error : vscode . l10n . t ( 'Failed to retrieve pull request information from job' ) , state : 'error' } ;
794+ }
795+
796+ const { number } = jobInfo . pull_request ;
797+ this . _onDidCreatePullRequest . fire ( number ) ;
798+
799+ // Find the actual PR to get the HTML URL
800+ const pullRequest = await this . findPullRequestById ( number , true ) ;
801+ const htmlUrl = pullRequest ?. html_url || `https://github.com/${ owner } /${ repo } /pull/${ number } ` ;
802+
803+ const webviewUri = await toOpenPullRequestWebviewUri ( { owner, repo, pullRequestNumber : number } ) ;
790804 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.` ;
791805
792- await this . waitForQueuedToInProgress ( session_id , token ) ;
806+ await this . waitForQueuedToInProgress ( response . session_id , token ) ;
793807 return {
794808 state : 'success' ,
795- number : pull_request . number ,
796- link : pull_request . html_url ,
809+ number,
810+ link : htmlUrl ,
797811 webviewUri,
798812 llmDetails : head_ref ? `Local pending changes have been pushed to branch '${ head_ref } '. ${ prLlmString } ` : prLlmString ,
799- sessionId : session_id
813+ sessionId : response . session_id
800814 } ;
801815 } catch ( error ) {
802816 return { error : error . message , state : 'error' } ;
@@ -1585,6 +1599,32 @@ export class CopilotRemoteAgentManager extends Disposable {
15851599 this . _stateModel . clear ( ) ;
15861600 }
15871601
1602+ private async waitForJobWithPullRequest (
1603+ capiClient : CopilotApi ,
1604+ owner : string ,
1605+ repo : string ,
1606+ jobId : string ,
1607+ token ?: vscode . CancellationToken
1608+ ) : Promise < JobInfo | undefined > {
1609+ const maxWaitTime = 30 * 1000 ; // 30 seconds
1610+ const pollInterval = 2000 ; // 2 seconds
1611+ const startTime = Date . now ( ) ;
1612+
1613+ Logger . appendLine ( `Waiting for job ${ jobId } to have pull request information...` , CopilotRemoteAgentManager . ID ) ;
1614+
1615+ while ( Date . now ( ) - startTime < maxWaitTime && ( ! token || ! token . isCancellationRequested ) ) {
1616+ const jobInfo = await capiClient . getJobByJobId ( owner , repo , jobId ) ;
1617+ if ( jobInfo && jobInfo . pull_request && jobInfo . pull_request . number ) {
1618+ Logger . appendLine ( `Job ${ jobId } now has pull request #${ jobInfo . pull_request . number } ` , CopilotRemoteAgentManager . ID ) ;
1619+ return jobInfo ;
1620+ }
1621+ await new Promise ( resolve => setTimeout ( resolve , pollInterval ) ) ;
1622+ }
1623+
1624+ Logger . warn ( `Timed out waiting for job ${ jobId } to have pull request information` , CopilotRemoteAgentManager . ID ) ;
1625+ return undefined ;
1626+ }
1627+
15881628 public async cancelMostRecentChatSession ( pullRequest : PullRequestModel ) : Promise < void > {
15891629 const capi = await this . copilotApi ;
15901630 if ( ! capi ) {
0 commit comments