@@ -550,6 +550,45 @@ export function registerCommands(
550550 }
551551 } ;
552552
553+ /**
554+ * Metadata passed from chat/agent sessions containing repository information.
555+ * This is provided by VS Code when commands are invoked from chat session toolbars.
556+ */
557+ interface SessionMetadata {
558+ /** GitHub repository owner/organization name */
559+ owner ?: string ;
560+ /** GitHub repository name */
561+ name ?: string ;
562+ [ key : string ] : unknown ;
563+ }
564+
565+ /**
566+ * Get the folder manager and GitHub repository for a repository based on metadata.
567+ * Falls back to the first folder manager if metadata is not provided or repository not found.
568+ * @param metadata Session metadata containing owner and repo information
569+ * @returns Object with folderManager and githubRepo, or undefined if no folder managers exist
570+ */
571+ function getFolderManagerFromMetadata ( metadata : SessionMetadata | undefined ) : { folderManager : FolderRepositoryManager ; githubRepo : GitHubRepository } | undefined {
572+ if ( metadata ?. owner && metadata ?. name ) {
573+ const folderManager = reposManager . getManagerForRepository ( metadata . owner , metadata . name ) ?? reposManager . folderManagers [ 0 ] ;
574+ if ( ! folderManager || folderManager . gitHubRepositories . length === 0 ) {
575+ return undefined ;
576+ }
577+ const githubRepo = folderManager . gitHubRepositories . find (
578+ repo => repo . remote . owner === metadata . owner && repo . remote . repositoryName === metadata . name
579+ ) ?? folderManager . gitHubRepositories [ 0 ] ;
580+ return { folderManager, githubRepo } ;
581+ }
582+ if ( reposManager . folderManagers . length === 0 ) {
583+ return undefined ;
584+ }
585+ const folderManager = reposManager . folderManagers [ 0 ] ;
586+ if ( folderManager . gitHubRepositories . length === 0 ) {
587+ return undefined ;
588+ }
589+ return { folderManager, githubRepo : folderManager . gitHubRepositories [ 0 ] } ;
590+ }
591+
553592 function contextHasPath ( ctx : OverviewContext | { path : string } | undefined ) : ctx is { path : string } {
554593 const contextAsPath : Partial < { path : string } > = ( ctx as { path : string } ) ;
555594 return ! ! contextAsPath . path ;
@@ -567,7 +606,7 @@ export function registerCommands(
567606 }
568607 }
569608
570- context . subscriptions . push ( vscode . commands . registerCommand ( 'pr.checkoutFromDescription' , async ( ctx : OverviewContext | { path : string } | undefined ) => {
609+ context . subscriptions . push ( vscode . commands . registerCommand ( 'pr.checkoutFromDescription' , async ( ctx : OverviewContext | { path : string } | undefined , metadata ?: SessionMetadata ) => {
571610 if ( ! ctx ) {
572611 return vscode . window . showErrorMessage ( vscode . l10n . t ( 'No pull request context provided for checkout.' ) ) ;
573612 }
@@ -578,8 +617,13 @@ export function registerCommands(
578617 if ( ! prNumber ) {
579618 return vscode . window . showErrorMessage ( vscode . l10n . t ( 'No pull request number found in context path.' ) ) ;
580619 }
581- const folderManager = reposManager . folderManagers [ 0 ] ;
582- const pullRequest = await folderManager . fetchById ( folderManager . gitHubRepositories [ 0 ] , Number ( prNumber ) ) ;
620+ // Use metadata to find the correct repository if available
621+ const result = getFolderManagerFromMetadata ( metadata ) ;
622+ if ( ! result ) {
623+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to find repository manager.' ) ) ;
624+ }
625+ const { folderManager, githubRepo } = result ;
626+ const pullRequest = await folderManager . fetchById ( githubRepo , Number ( prNumber ) ) ;
583627 if ( ! pullRequest ) {
584628 return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to find pull request #{0}' , prNumber . toString ( ) ) ) ;
585629 }
@@ -595,7 +639,7 @@ export function registerCommands(
595639
596640 } ) ) ;
597641
598- context . subscriptions . push ( vscode . commands . registerCommand ( 'pr.applyChangesFromDescription' , async ( ctx : OverviewContext | { path : string } | undefined ) => {
642+ context . subscriptions . push ( vscode . commands . registerCommand ( 'pr.applyChangesFromDescription' , async ( ctx : OverviewContext | { path : string } | undefined , metadata ?: SessionMetadata ) => {
599643 if ( ! ctx ) {
600644 return vscode . window . showErrorMessage ( vscode . l10n . t ( 'No pull request context provided for applying changes.' ) ) ;
601645 }
@@ -616,8 +660,13 @@ export function registerCommands(
616660 async ( task ) => {
617661 task . report ( { increment : 30 } ) ;
618662
619- const folderManager = reposManager . folderManagers [ 0 ] ;
620- const pullRequest = await folderManager . fetchById ( folderManager . gitHubRepositories [ 0 ] , Number ( prNumber ) ) ;
663+ // Use metadata to find the correct repository if available
664+ const result = getFolderManagerFromMetadata ( metadata ) ;
665+ if ( ! result ) {
666+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to find repository manager.' ) ) ;
667+ }
668+ const { folderManager, githubRepo } = result ;
669+ const pullRequest = await folderManager . fetchById ( githubRepo , Number ( prNumber ) ) ;
621670 if ( ! pullRequest ) {
622671 return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to find pull request #{0}' , prNumber . toString ( ) ) ) ;
623672 }
0 commit comments