-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathcopilotRemoteAgent.ts
More file actions
171 lines (143 loc) · 5.55 KB
/
copilotRemoteAgent.ts
File metadata and controls
171 lines (143 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { RepoInfo } from './common';
import { CopilotApi, getCopilotApi } from './copilotApi';
import { CopilotPRWatcher } from './copilotPrWatcher';
import { CredentialStore } from './credentials';
import { FolderRepositoryManager } from './folderRepositoryManager';
import { GitHubRepository } from './githubRepository';
import { RepositoriesManager } from './repositoriesManager';
import { CopilotRemoteAgentConfig } from '../common/config';
import { COPILOT_CLOUD_AGENT, COPILOT_LOGINS } from '../common/copilot';
import { Disposable } from '../common/lifecycle';
import Logger from '../common/logger';
import { GitHubRemote } from '../common/remote';
import { ITelemetry } from '../common/telemetry';
import { PrsTreeModel } from '../view/prsTreeModel';
const PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY = 'PREFERRED_GITHUB_CODING_AGENT_REMOTE';
export namespace SessionIdForPr {
const prefix = 'pull-session-by-index';
export function getResource(prNumber: number, sessionIndex: number): vscode.Uri {
return vscode.Uri.from({
scheme: COPILOT_CLOUD_AGENT, path: `/${prefix}-${prNumber}-${sessionIndex}`,
});
}
export function parse(resource: vscode.Uri): { prNumber: number; sessionIndex: number } | undefined {
const match = resource.path.match(new RegExp(`^/${prefix}-(\\d+)-(\\d+)$`));
if (match) {
return {
prNumber: parseInt(match[1], 10),
sessionIndex: parseInt(match[2], 10)
};
}
return undefined;
}
}
export class CopilotRemoteAgentManager extends Disposable {
public static ID = 'CopilotRemoteAgentManager';
private _isAssignable: boolean | undefined;
constructor(
private credentialStore: CredentialStore,
public repositoriesManager: RepositoriesManager,
private telemetry: ITelemetry,
private context: vscode.ExtensionContext,
private readonly prsTreeModel: PrsTreeModel,
) {
super();
this._register(new CopilotPRWatcher(this.repositoriesManager, this.prsTreeModel));
}
private _copilotApiPromise: Promise<CopilotApi | undefined> | undefined;
private get copilotApi(): Promise<CopilotApi | undefined> {
if (!this._copilotApiPromise) {
this._copilotApiPromise = this.initializeCopilotApi();
}
return this._copilotApiPromise;
}
private async initializeCopilotApi(): Promise<CopilotApi | undefined> {
return await getCopilotApi(this.credentialStore, this.telemetry);
}
async isAssignable(): Promise<boolean> {
const setCachedResult = (b: boolean) => {
this._isAssignable = b;
return b;
};
if (this._isAssignable !== undefined) {
return this._isAssignable;
}
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return setCachedResult(false);
}
const { fm } = repoInfo;
try {
// Ensure assignable users are loaded
await fm.getAssignableUsers();
const allAssignableUsers = fm.getAllAssignableUsers();
if (!allAssignableUsers) {
return setCachedResult(false);
}
return setCachedResult(allAssignableUsers.some(user => COPILOT_LOGINS.includes(user.login)));
} catch (error) {
// If there's an error fetching assignable users, assume not assignable
return setCachedResult(false);
}
}
async isAvailable(): Promise<boolean> {
// Check if the manager is enabled, copilot API is available, and it's assignable
if (!CopilotRemoteAgentConfig.getEnabled()) {
return false;
}
if (!this.credentialStore.isAnyAuthenticated()) {
// If not signed in, then we optimistically say it's available.
return true;
}
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return false;
}
const copilotApi = await this.copilotApi;
if (!copilotApi) {
return false;
}
return await this.isAssignable();
}
private firstFolderManager(): FolderRepositoryManager | undefined {
if (!this.repositoriesManager.folderManagers.length) {
return;
}
return this.repositoriesManager.folderManagers[0];
}
async repoInfo(fm?: FolderRepositoryManager): Promise<RepoInfo | undefined> {
fm = fm || this.firstFolderManager();
const repository = fm?.repository;
const ghRepository = fm?.gitHubRepositories.find(repo => repo.remote instanceof GitHubRemote) as GitHubRepository | undefined;
if (!fm || !repository || !ghRepository) {
return;
}
const baseRef = repository.state.HEAD?.name; // TODO: Consider edge cases
const preferredRemoteName = this.context.workspaceState.get(PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY);
const ghRemotes = await fm.getGitHubRemotes();
if (!ghRemotes || ghRemotes.length === 0) {
return;
}
const remote =
preferredRemoteName
? ghRemotes.find(remote => remote.remoteName === preferredRemoteName) // Cached preferred value
: (ghRemotes.find(remote => remote.remoteName === 'origin') || ghRemotes[0]); // Fallback to the first remote
if (!remote) {
Logger.error(`no valid remotes for coding agent`, CopilotRemoteAgentManager.ID);
// Clear preference, something is wrong
this.context.workspaceState.update(PREFERRED_GITHUB_CODING_AGENT_REMOTE_WORKSPACE_KEY, undefined);
return;
}
// Extract repo data from target remote
const { owner, repositoryName: repo } = remote;
if (!owner || !repo || !baseRef || !repository) {
return;
}
return { owner, repo, baseRef, remote, repository, ghRepository, fm };
}
}