44 *--------------------------------------------------------------------------------------------*/
55
66import * as vscode from 'vscode' ;
7- import { RepoInfo } from './common' ;
8- import { CopilotApi , getCopilotApi } from './copilotApi' ;
97import { CopilotPRWatcher } from './copilotPrWatcher' ;
108
119import { CredentialStore } from './credentials' ;
12- import { FolderRepositoryManager } from './folderRepositoryManager' ;
13- import { GitHubRepository } from './githubRepository' ;
1410import { RepositoriesManager } from './repositoriesManager' ;
15- import { CopilotRemoteAgentConfig } from '../common/config' ;
16- import { COPILOT_CLOUD_AGENT , COPILOT_LOGINS } from '../common/copilot' ;
11+ import { COPILOT_CLOUD_AGENT } from '../common/copilot' ;
1712import { Disposable } from '../common/lifecycle' ;
18- import Logger from '../common/logger' ;
19- import { GitHubRemote } from '../common/remote' ;
2013import { ITelemetry } from '../common/telemetry' ;
2114import { PrsTreeModel } from '../view/prsTreeModel' ;
2215
23- const PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY = 'PREFERRED_GITHUB_CODING_AGENT_REMOTE' ;
24-
2516export namespace SessionIdForPr {
2617
2718 const prefix = 'pull-session-by-index' ;
@@ -59,113 +50,4 @@ export class CopilotRemoteAgentManager extends Disposable {
5950
6051 this . _register ( new CopilotPRWatcher ( this . repositoriesManager , this . prsTreeModel ) ) ;
6152 }
62- private _copilotApiPromise : Promise < CopilotApi | undefined > | undefined ;
63- private get copilotApi ( ) : Promise < CopilotApi | undefined > {
64- if ( ! this . _copilotApiPromise ) {
65- this . _copilotApiPromise = this . initializeCopilotApi ( ) ;
66- }
67- return this . _copilotApiPromise ;
68- }
69-
70- private async initializeCopilotApi ( ) : Promise < CopilotApi | undefined > {
71- return await getCopilotApi ( this . credentialStore , this . telemetry ) ;
72- }
73-
74- async isAssignable ( ) : Promise < boolean > {
75- const setCachedResult = ( b : boolean ) => {
76- this . _isAssignable = b ;
77- return b ;
78- } ;
79-
80- if ( this . _isAssignable !== undefined ) {
81- return this . _isAssignable ;
82- }
83-
84- const repoInfo = await this . repoInfo ( ) ;
85- if ( ! repoInfo ) {
86- return setCachedResult ( false ) ;
87- }
88-
89- const { fm } = repoInfo ;
90-
91- try {
92- // Ensure assignable users are loaded
93- await fm . getAssignableUsers ( ) ;
94- const allAssignableUsers = fm . getAllAssignableUsers ( ) ;
95-
96- if ( ! allAssignableUsers ) {
97- return setCachedResult ( false ) ;
98- }
99- return setCachedResult ( allAssignableUsers . some ( user => COPILOT_LOGINS . includes ( user . login ) ) ) ;
100- } catch ( error ) {
101- // If there's an error fetching assignable users, assume not assignable
102- return setCachedResult ( false ) ;
103- }
104- }
105-
106- async isAvailable ( ) : Promise < boolean > {
107- // Check if the manager is enabled, copilot API is available, and it's assignable
108- if ( ! CopilotRemoteAgentConfig . getEnabled ( ) ) {
109- return false ;
110- }
111-
112- if ( ! this . credentialStore . isAnyAuthenticated ( ) ) {
113- // If not signed in, then we optimistically say it's available.
114- return true ;
115- }
116-
117- const repoInfo = await this . repoInfo ( ) ;
118- if ( ! repoInfo ) {
119- return false ;
120- }
121-
122- const copilotApi = await this . copilotApi ;
123- if ( ! copilotApi ) {
124- return false ;
125- }
126-
127- return await this . isAssignable ( ) ;
128- }
129-
130- private firstFolderManager ( ) : FolderRepositoryManager | undefined {
131- if ( ! this . repositoriesManager . folderManagers . length ) {
132- return ;
133- }
134- return this . repositoriesManager . folderManagers [ 0 ] ;
135- }
136-
137- async repoInfo ( fm ?: FolderRepositoryManager ) : Promise < RepoInfo | undefined > {
138- fm = fm || this . firstFolderManager ( ) ;
139- const repository = fm ?. repository ;
140- const ghRepository = fm ?. gitHubRepositories . find ( repo => repo . remote instanceof GitHubRemote ) as GitHubRepository | undefined ;
141- if ( ! fm || ! repository || ! ghRepository ) {
142- return ;
143- }
144- const baseRef = repository . state . HEAD ?. name ; // TODO: Consider edge cases
145- const preferredRemoteName = this . context . workspaceState . get ( PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY ) ;
146- const ghRemotes = await fm . getGitHubRemotes ( ) ;
147- if ( ! ghRemotes || ghRemotes . length === 0 ) {
148- return ;
149- }
150-
151- const remote =
152- preferredRemoteName
153- ? ghRemotes . find ( remote => remote . remoteName === preferredRemoteName ) // Cached preferred value
154- : ( ghRemotes . find ( remote => remote . remoteName === 'origin' ) || ghRemotes [ 0 ] ) ; // Fallback to the first remote
155-
156- if ( ! remote ) {
157- Logger . error ( `no valid remotes for coding agent` , CopilotRemoteAgentManager . ID ) ;
158- // Clear preference, something is wrong
159- this . context . workspaceState . update ( PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY , undefined ) ;
160- return ;
161- }
162-
163- // Extract repo data from target remote
164- const { owner, repositoryName : repo } = remote ;
165- if ( ! owner || ! repo || ! baseRef || ! repository ) {
166- return ;
167- }
168- return { owner, repo, baseRef, remote, repository, ghRepository, fm } ;
169- }
170-
17153}
0 commit comments