77import * as vscode from 'vscode' ;
88import { openPullRequestOnGitHub } from '../commands' ;
99import { IComment } from '../common/comment' ;
10+ import { copilotEventToStatus , CopilotPRStatus , mostRecentCopilotEvent } from '../common/copilot' ;
1011import { commands , contexts } from '../common/executeCommands' ;
1112import { disposeAll } from '../common/lifecycle' ;
1213import Logger from '../common/logger' ;
1314import { DEFAULT_MERGE_METHOD , PR_SETTINGS_NAMESPACE } from '../common/settingKeys' ;
1415import { ITelemetry } from '../common/telemetry' ;
15- import { ReviewEvent , SessionPullInfo } from '../common/timelineEvent' ;
16+ import { EventType , ReviewEvent , SessionPullInfo , TimelineEvent } from '../common/timelineEvent' ;
1617import { asPromise , formatError } from '../common/utils' ;
1718import { IRequestMessage , PULL_REQUEST_OVERVIEW_VIEW_TYPE } from '../common/webview' ;
1819import { SessionLogViewManager } from '../view/sessionLogView' ;
20+ import { getCopilotApi } from './copilotApi' ;
1921import { FolderRepositoryManager } from './folderRepositoryManager' ;
2022import {
2123 GithubItemStateEnum ,
@@ -34,7 +36,7 @@ import { PullRequestModel } from './pullRequestModel';
3436import { PullRequestView } from './pullRequestOverviewCommon' ;
3537import { pickEmail , reviewersQuickPick } from './quickPicks' ;
3638import { parseReviewers } from './utils' ;
37- import { MergeArguments , MergeResult , PullRequest , ReviewType , SubmitReviewReply } from './views' ;
39+ import { CancelCodingAgentReply , MergeArguments , MergeResult , PullRequest , ReviewType , SubmitReviewReply } from './views' ;
3840
3941export class PullRequestOverviewPanel extends IssueOverviewPanel < PullRequestModel > {
4042 public static override ID : string = 'PullRequestOverviewPanel' ;
@@ -359,6 +361,8 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
359361 return this . revert ( message ) ;
360362 case 'pr.open-session-log' :
361363 return this . openSessionLog ( message . args . link ) ;
364+ case 'pr.cancel-coding-agent' :
365+ return this . cancelCodingAgent ( message ) ;
362366 }
363367 }
364368
@@ -462,6 +466,41 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
462466 }
463467 }
464468
469+ private async cancelCodingAgent ( message : IRequestMessage < TimelineEvent > ) : Promise < void > {
470+ try {
471+ let result = false ;
472+ if ( message . args . event !== EventType . CopilotStarted ) {
473+ return this . _replyMessage ( message , { success : false , error : 'Invalid event type' } ) ;
474+ } else {
475+ const copilotApi = await getCopilotApi ( this . _folderRepositoryManager . credentialStore , this . _item . remote . authProviderId ) ;
476+ if ( copilotApi ) {
477+ const session = ( await copilotApi . getAllSessions ( this . _item ) ) [ 0 ] ;
478+ if ( session . state !== 'completed' ) {
479+ result = await this . _item . githubRepository . cancelWorkflow ( session . workflow_run_id ) ;
480+ }
481+ }
482+ }
483+ // need to wait until we get the updated timeline events
484+ let events : TimelineEvent [ ] = [ ] ;
485+ if ( result ) {
486+ do {
487+ events = await this . _item . getTimelineEvents ( ) ;
488+ } while ( copilotEventToStatus ( mostRecentCopilotEvent ( events ) ) !== CopilotPRStatus . Completed && await new Promise < boolean > ( c => setTimeout ( ( ) => c ( true ) , 2000 ) ) ) ;
489+ }
490+ const reply : CancelCodingAgentReply = {
491+ events
492+ } ;
493+ this . _replyMessage ( message , reply ) ;
494+ } catch ( e ) {
495+ Logger . error ( `Cancelling coding agent failed: ${ formatError ( e ) } ` , PullRequestOverviewPanel . ID ) ;
496+ vscode . window . showErrorMessage ( vscode . l10n . t ( 'Cannot cancel coding agent' ) ) ;
497+ const reply : CancelCodingAgentReply = {
498+ events : [ ] ,
499+ } ;
500+ this . _replyMessage ( message , reply ) ;
501+ }
502+ }
503+
465504 private async openChanges ( ) : Promise < void > {
466505 return PullRequestModel . openChanges ( this . _folderRepositoryManager , this . _item ) ;
467506 }
0 commit comments