-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathcopilotRemoteAgent.ts
More file actions
642 lines (562 loc) · 22.9 KB
/
copilotRemoteAgent.ts
File metadata and controls
642 lines (562 loc) · 22.9 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import vscode from 'vscode';
import { Repository } from '../api/api';
import { AuthProvider } from '../common/authentication';
import { COPILOT_LOGINS } from '../common/copilot';
import { commands } from '../common/executeCommands';
import { Disposable } from '../common/lifecycle';
import Logger from '../common/logger';
import { GitHubRemote } from '../common/remote';
import { CODING_AGENT, CODING_AGENT_AUTO_COMMIT_AND_PUSH, CODING_AGENT_ENABLED } from '../common/settingKeys';
import { toOpenPullRequestWebviewUri } from '../common/uri';
import { OctokitCommon } from './common';
import { CopilotApi, RemoteAgentJobPayload, SessionInfo } from './copilotApi';
import { CopilotPRWatcher, CopilotStateModel } from './copilotPrWatcher';
import { CredentialStore } from './credentials';
import { FolderRepositoryManager } from './folderRepositoryManager';
import { GitHubRepository } from './githubRepository';
import { PullRequestModel } from './pullRequestModel';
import { RepositoriesManager } from './repositoriesManager';
type RemoteAgentSuccessResult = { link: string; state: 'success'; number: number; webviewUri: vscode.Uri; llmDetails: string };
type RemoteAgentErrorResult = { error: string; state: 'error' };
type RemoteAgentResult = RemoteAgentSuccessResult | RemoteAgentErrorResult;
export interface IAPISessionLogs {
readonly info: SessionInfo;
readonly logs: string;
}
export interface ICopilotRemoteAgentCommandArgs {
userPrompt: string;
summary?: string;
source?: string;
followup?: string;
}
const LEARN_MORE = vscode.l10n.t('Learn about coding agent');
// Without Pending Changes
const CONTINUE = vscode.l10n.t('Continue');
// With Pending Changes
const PUSH_CHANGES = vscode.l10n.t('Include changes');
const CONTINUE_WITHOUT_PUSHING = vscode.l10n.t('Ignore changes');
const FOLLOW_UP_REGEX = /open-pull-request-webview.*((%7B.*?%7D)|(\{.*?\}))/;
const COPILOT = '@copilot';
export class CopilotRemoteAgentManager extends Disposable {
public static ID = 'CopilotRemoteAgentManager';
private readonly _stateModel: CopilotStateModel;
private readonly _onDidChangeStates = this._register(new vscode.EventEmitter<void>());
readonly onDidChangeStates = this._onDidChangeStates.event;
private readonly _onDidChangeNotifications = this._register(new vscode.EventEmitter<void>());
readonly onDidChangeNotifications = this._onDidChangeNotifications.event;
private readonly _onDidCreatePullRequest = this._register(new vscode.EventEmitter<number>());
readonly onDidCreatePullRequest = this._onDidCreatePullRequest.event;
constructor(private credentialStore: CredentialStore, public repositoriesManager: RepositoriesManager) {
super();
this._register(this.credentialStore.onDidChangeSessions((e: vscode.AuthenticationSessionsChangeEvent) => {
if (e.provider.id === 'github') {
this._copilotApiPromise = undefined; // Invalidate cached session
}
}));
this._stateModel = new CopilotStateModel();
this._register(new CopilotPRWatcher(this.repositoriesManager, this._stateModel));
this._register(this._stateModel.onDidChangeStates(() => this._onDidChangeStates.fire()));
this._register(this._stateModel.onDidChangeNotifications(() => this._onDidChangeNotifications.fire()));
this._register(this.repositoriesManager.onDidChangeFolderRepositories((event) => {
if (event.added) {
this._register(event.added.onDidChangeAssignableUsers(() => {
this.updateAssignabilityContext();
}));
}
this.updateAssignabilityContext();
}));
this.repositoriesManager.folderManagers.forEach(manager => {
this._register(manager.onDidChangeAssignableUsers(() => {
this.updateAssignabilityContext();
}));
});
this._register(vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(CODING_AGENT)) {
this.updateAssignabilityContext();
}
}));
// Set initial context
this.updateAssignabilityContext();
}
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> {
const gh = await this.credentialStore.getHubOrLogin(AuthProvider.github);
const { token } = await gh?.octokit.api.auth() as { token: string };
if (!token || !gh?.octokit) {
return;
}
return new CopilotApi(gh.octokit, token);
}
enabled(): boolean {
return vscode.workspace
.getConfiguration(CODING_AGENT).get(CODING_AGENT_ENABLED, false);
}
async isAssignable(): Promise<boolean> {
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return false;
}
const { fm } = repoInfo;
try {
// Ensure assignable users are loaded
await fm.getAssignableUsers();
const allAssignableUsers = fm.getAllAssignableUsers();
if (!allAssignableUsers) {
return false;
}
// Check if any of the copilot logins are in the assignable users
return allAssignableUsers.some(user => COPILOT_LOGINS.includes(user.login));
} catch (error) {
// If there's an error fetching assignable users, assume not assignable
return false;
}
}
async isAvailable(): Promise<boolean> {
// Check if the manager is enabled, copilot API is available, and it's assignable
if (!this.enabled()) {
return false;
}
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return false;
}
const copilotApi = await this.copilotApi;
if (!copilotApi) {
return false;
}
return await this.isAssignable();
}
private async updateAssignabilityContext(): Promise<void> {
try {
const available = await this.isAvailable();
commands.setContext('copilotCodingAgentAssignable', available);
} catch (error) {
// Presume false
commands.setContext('copilotCodingAgentAssignable', false);
}
}
autoCommitAndPushEnabled(): boolean {
return vscode.workspace
.getConfiguration(CODING_AGENT).get(CODING_AGENT_AUTO_COMMIT_AND_PUSH, false);
}
async repoInfo(): Promise<{ owner: string; repo: string; baseRef: string; remote: GitHubRemote; repository: Repository; ghRepository: GitHubRepository; fm: FolderRepositoryManager } | undefined> {
if (!this.repositoriesManager.folderManagers.length) {
return;
}
const fm = this.repositoriesManager.folderManagers[0];
const repository = fm?.repository;
const ghRepository = fm?.gitHubRepositories.find(repo => repo.remote instanceof GitHubRemote) as GitHubRepository | undefined;
if (!repository || !ghRepository) {
return;
}
const baseRef = repository.state.HEAD?.name; // TODO: Consider edge cases
const ghRemotes = await fm.getGitHubRemotes();
if (!ghRemotes || ghRemotes.length === 0) {
return;
}
const remote =
ghRemotes.find(remote => remote.remoteName === 'origin')
|| ghRemotes[0]; // Fallback to the first remote
// 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 };
}
private parseFollowup(followup: string | undefined, repoInfo: { owner: string; repo: string }): number | undefined {
if (!followup) {
return;
}
const match = followup.match(FOLLOW_UP_REGEX);
if (!match || match.length < 2) {
Logger.error(`Ignoring. Invalid followup format: ${followup}`, CopilotRemoteAgentManager.ID);
return;
}
try {
const followUpData = JSON.parse(decodeURIComponent(match[1]));
if (!followUpData || !followUpData.owner || !followUpData.repo || !followUpData.pullRequestNumber) {
Logger.error(`Ignoring. Invalid followup data: ${followUpData}`, CopilotRemoteAgentManager.ID);
return;
}
if (repoInfo.owner !== followUpData.owner || repoInfo.repo !== followUpData.repo) {
Logger.error(`Ignoring. Follow up data does not match current repository: ${JSON.stringify(followUpData)}`, CopilotRemoteAgentManager.ID);
return;
}
return followUpData.pullRequestNumber;
} catch (error) {
Logger.error(`Ignoring. Error while parsing follow up data: ${followup}`, CopilotRemoteAgentManager.ID);
}
}
async addFollowUpToExistingPR(pullRequestNumber: number, userPrompt: string, summary?: string): Promise<string | undefined> {
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return;
}
try {
const ghRepo = repoInfo.ghRepository;
const pr = await ghRepo.getPullRequest(pullRequestNumber);
if (!pr) {
Logger.error(`Could not find pull request #${pullRequestNumber}`, CopilotRemoteAgentManager.ID);
return;
}
// Add a comment tagging @copilot with the user's prompt
const commentBody = `${COPILOT} ${userPrompt} \n\n --- \n\n ${summary ?? ''}`;
const commentResult = await pr.createIssueComment(commentBody);
if (!commentResult) {
Logger.error(`Failed to add comment to PR #${pullRequestNumber}`, CopilotRemoteAgentManager.ID);
return;
}
Logger.appendLine(`Added comment ${commentResult.htmlUrl}`, CopilotRemoteAgentManager.ID);
// allow-any-unicode-next-line
return vscode.l10n.t('🚀 Follow-up comment added to [#{0}]({1})', pullRequestNumber, commentResult.htmlUrl);
} catch (err) {
Logger.error(`Failed to add follow-up comment to PR #${pullRequestNumber}: ${err}`, CopilotRemoteAgentManager.ID);
return;
}
}
async commandImpl(args?: ICopilotRemoteAgentCommandArgs): Promise<string | undefined> {
if (!args) {
return;
}
const { userPrompt, summary, source, followup } = args;
if (!userPrompt || userPrompt.trim().length === 0) {
return;
}
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return;
}
const { repository, owner, repo } = repoInfo;
// If this is a followup, parse out the necessary data
// Group 2 is this, url-encoded:
// {"owner":"monalisa","repo":"app","pullRequestNumber":18}
let followUpPR: number | undefined = this.parseFollowup(followup, repoInfo);
// Check if the currently active PR is a coding agent PR
if (!followUpPR) {
const activePR = repoInfo.fm.activePullRequest;
if (activePR && this._stateModel.get(owner, repo, activePR.number)) {
followUpPR = activePR.number;
}
}
if (followUpPR) {
return this.addFollowUpToExistingPR(followUpPR, userPrompt, summary);
}
const repoName = `${owner}/${repo}`;
const hasChanges = repository.state.workingTreeChanges.length > 0 || repository.state.indexChanges.length > 0;
const learnMoreCb = async () => {
vscode.env.openExternal(vscode.Uri.parse('https://docs.github.com/copilot/using-github-copilot/coding-agent'));
};
let autoPushAndCommit = false;
const message = vscode.l10n.t('Copilot coding agent will continue your work in \'{0}\'', repoName);
if (source !== 'prompt' && hasChanges && this.autoCommitAndPushEnabled()) {
const modalResult = await vscode.window.showInformationMessage(
message,
{
modal: true,
detail: vscode.l10n.t('Local changes detected'),
},
PUSH_CHANGES,
CONTINUE_WITHOUT_PUSHING,
LEARN_MORE,
);
if (!modalResult) {
return;
}
if (modalResult === LEARN_MORE) {
learnMoreCb();
return;
}
if (modalResult === PUSH_CHANGES) {
autoPushAndCommit = true;
}
} else {
const modalResult = await vscode.window.showInformationMessage(
(source !== 'prompt' ? message : vscode.l10n.t('Copilot coding agent will implement the specification outlined in this prompt file')),
{
modal: true,
},
CONTINUE,
LEARN_MORE,
);
if (!modalResult) {
return;
}
if (modalResult === LEARN_MORE) {
learnMoreCb();
return;
}
}
const result = await this.invokeRemoteAgent(
userPrompt,
summary || userPrompt,
autoPushAndCommit,
);
if (result.state !== 'success') {
vscode.window.showErrorMessage(result.error);
return;
}
const { webviewUri, link, number } = result;
if (source === 'prompt') {
const VIEW = vscode.l10n.t('View');
const finished = vscode.l10n.t('Coding agent has begun work on your prompt in #{0}', number);
vscode.window.showInformationMessage(finished, VIEW).then((value) => {
if (value === VIEW) {
vscode.commands.executeCommand('vscode.open', webviewUri);
}
});
}
// allow-any-unicode-next-line
return vscode.l10n.t('🚀 Coding agent will continue work in [#{0}]({1}). Track progress [here]({2}).', number, link, webviewUri.toString());
}
/**
* Opens a terminal and waits for user to successfully commit
* This is a fallback for when the commit cannot be done automatically (eg: GPG signing password needed)
*/
private async handleInteractiveCommit(repository: Repository, commitMessage: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
const startingCommit = repository.state.HEAD?.commit;
// Create terminal with git commit command
const terminal = vscode.window.createTerminal({
name: 'GitHub Coding Agent',
cwd: repository.rootUri.fsPath,
message: vscode.l10n.t('Commit your changes to continue coding agent session')
});
// Show terminal and send commit command
terminal.show();
terminal.sendText(`# Complete this commit to continue with your coding agent session. Ctrl+C to cancel.`);
terminal.sendText(`git commit -m "${commitMessage}"`);
let disposed = false;
let timeoutId: NodeJS.Timeout;
let stateListener: vscode.Disposable | undefined;
let disposalListener: vscode.Disposable | undefined;
const cleanup = () => {
if (disposed) return;
disposed = true;
clearTimeout(timeoutId);
stateListener?.dispose();
disposalListener?.dispose();
terminal.dispose();
};
// Listen for repository state changes
stateListener = repository.state.onDidChange(() => {
// Check if commit was successful (HEAD changed and no more staged changes)
if (repository.state.HEAD?.commit !== startingCommit &&
repository.state.indexChanges.length === 0) {
cleanup();
resolve(true);
}
});
// Set a timeout to avoid waiting forever
timeoutId = setTimeout(() => {
cleanup();
vscode.window.showWarningMessage(
vscode.l10n.t('Commit timeout. Please try the operation again after committing your changes.')
);
resolve(false);
}, 5 * 60 * 1000); // 5 minutes timeout
// Listen for terminal disposal (user closed it)
disposalListener = vscode.window.onDidCloseTerminal((closedTerminal) => {
if (closedTerminal === terminal) {
// Give a brief moment for potential state changes to propagate
setTimeout(() => {
if (!disposed) {
cleanup();
// Check one more time if commit happened just before terminal was closed
resolve(repository.state.HEAD?.commit !== startingCommit &&
repository.state.indexChanges.length === 0);
}
}, 1000);
}
});
});
}
async invokeRemoteAgent(prompt: string, problemContext: string, autoPushAndCommit = true): Promise<RemoteAgentResult> {
const capiClient = await this.copilotApi;
if (!capiClient) {
return { error: vscode.l10n.t('Failed to initialize Copilot API'), state: 'error' };
}
const repoInfo = await this.repoInfo();
if (!repoInfo) {
return { error: vscode.l10n.t('No repository information found. Please open a workspace with a GitHub repository.'), state: 'error' };
}
const { owner, repo, remote, repository, ghRepository, baseRef } = repoInfo;
// NOTE: This is as unobtrusive as possible with the current high-level APIs.
// We only create a new branch and commit if there are staged or working changes.
// This could be improved if we add lower-level APIs to our git extension (e.g. in-memory temp git index).
let ref = baseRef;
const hasChanges = autoPushAndCommit && (repository.state.workingTreeChanges.length > 0 || repository.state.indexChanges.length > 0);
if (hasChanges) {
if (!this.autoCommitAndPushEnabled()) {
return { error: vscode.l10n.t('Uncommitted changes detected. Please commit or stash your changes before starting the remote agent. Enable \'{0}\' to push your changes automatically.', CODING_AGENT_AUTO_COMMIT_AND_PUSH), state: 'error' };
}
const asyncBranch = `copilot/vscode${Date.now()}`;
try {
await repository.createBranch(asyncBranch, true);
const commitMessage = 'Checkpoint from VS Code for coding agent session';
try {
await repository.commit(commitMessage, { all: true });
if (repository.state.HEAD?.name !== asyncBranch || repository.state.workingTreeChanges.length > 0 || repository.state.indexChanges.length > 0) {
throw new Error(vscode.l10n.t('Uncommitted changes still detected.'));
}
} catch (e) {
// Instead of immediately failing, open terminal for interactive commit
const commitSuccessful = await vscode.window.withProgress({
title: vscode.l10n.t('Waiting for commit to complete in the integrated terminal...'),
cancellable: true,
location: vscode.ProgressLocation.Notification
}, async (progress, token) => {
const commitPromise = this.handleInteractiveCommit(repository, commitMessage);
if (token) {
token.onCancellationRequested(() => {
return false;
});
}
return await commitPromise;
});
if (!commitSuccessful) {
return { error: vscode.l10n.t('Commit was unsuccessful. Manually commit or stash your changes and try again.'), state: 'error' };
}
}
await repository.push(remote.remoteName, asyncBranch, true);
ref = asyncBranch;
} catch (e) {
return { error: vscode.l10n.t('Could not auto-push pending changes. Manually commit or stash your changes and try again. ({0})', e.message), state: 'error' };
} finally {
// Swap back to the original branch without your pending changes
// TODO: Better if we show a confirmation dialog in chat
if (repository.state.HEAD?.name !== baseRef) {
// show notification asking the user if they want to switch back to the original branch
const SWAP_BACK_TO_ORIGINAL_BRANCH = vscode.l10n.t(`Swap back to '{0}'`, baseRef);
vscode.window.showInformationMessage(
vscode.l10n.t(`Pending changes pushed to remote branch '{0}'.`, ref),
SWAP_BACK_TO_ORIGINAL_BRANCH,
).then(async (selection) => {
if (selection === SWAP_BACK_TO_ORIGINAL_BRANCH) {
await repository.checkout(baseRef);
}
});
}
}
}
const base_ref = hasChanges ? baseRef : ref;
try {
if (!(await ghRepository.hasBranch(base_ref))) {
if (!this.autoCommitAndPushEnabled()) {
// We won't auto-push a branch if the user has disabled the setting
return { error: vscode.l10n.t('The branch \'{0}\' does not exist on the remote repository \'{1}/{2}\'. Please create the remote branch first.', base_ref, owner, repo), state: 'error' };
}
// Push the branch
Logger.appendLine(`Base ref needs to exist on remote. Auto pushing base_ref '${base_ref}' to remote repository '${owner}/${repo}'`, CopilotRemoteAgentManager.ID);
await repository.push(remote.remoteName, base_ref, true);
}
} catch (error) {
return { error: vscode.l10n.t('Failed to configure base branch \'{0}\' does not exist on the remote repository \'{1}/{2}\'. Please create the remote branch first.', base_ref, owner, repo), state: 'error' };
}
let title = prompt;
const titleMatch = problemContext.match(/TITLE: \s*(.*)/i);
if (titleMatch && titleMatch[1]) {
title = titleMatch[1].trim();
}
const problemStatement: string = `${prompt} ${problemContext ? `: ${problemContext}` : ''}`;
const payload: RemoteAgentJobPayload = {
problem_statement: problemStatement,
pull_request: {
title,
body_placeholder: problemContext,
base_ref,
...(hasChanges && { head_ref: ref })
}
};
try {
const { pull_request } = await capiClient.postRemoteAgentJob(owner, repo, payload);
this._onDidCreatePullRequest.fire(pull_request.number);
const webviewUri = await toOpenPullRequestWebviewUri({ owner, repo, pullRequestNumber: pull_request.number });
const prLlmString = `The remote agent has begun work. The user can track progress on GitHub.com by visiting ${pull_request.html_url} and within VS Code by visiting ${webviewUri.toString()}. Format all links as markdown (eg: [link text](url)).`;
return {
state: 'success',
number: pull_request.number,
link: pull_request.html_url,
webviewUri,
llmDetails: hasChanges ? `The pending changes have been pushed to branch '${ref}'. ${prLlmString}` : prLlmString
};
} catch (error) {
return { error: error.message, state: 'error' };
}
}
async getSessionLogsFromAction(pullRequest: PullRequestModel) {
const capi = await this.copilotApi;
if (!capi) {
return [];
}
const lastRun = await this.getLatestCodingAgentFromAction(pullRequest);
if (!lastRun) {
return [];
}
return await capi.getLogsFromZipUrl(lastRun.logs_url);
}
async getLatestCodingAgentFromAction(pullRequest: PullRequestModel, sessionIndex = 0, completedOnly = true): Promise<OctokitCommon.WorkflowRun | undefined> {
const capi = await this.copilotApi;
if (!capi) {
return;
}
const runs = await pullRequest.githubRepository.getWorkflowRunsFromAction(pullRequest.createdAt);
const padawanRuns = runs
.filter(run => run.path && run.path.startsWith('dynamic/copilot-swe-agent'))
.filter(run => run.pull_requests?.some(pr => pr.id === pullRequest.id));
const session = padawanRuns.filter(s => !completedOnly || s.status === 'completed').at(sessionIndex);
if (!session) {
return;
}
return this.getLatestRun(padawanRuns);
}
async getSessionLogFromPullRequest(pullRequestId: number, sessionIndex = 0, completedOnly = true): Promise<IAPISessionLogs | undefined> {
const capi = await this.copilotApi;
if (!capi) {
return undefined;
}
const sessions = await capi.getAllSessions(pullRequestId);
const session = sessions.filter(s => !completedOnly || s.state === 'completed').at(sessionIndex);
if (!session) {
return undefined;
}
const logs = await capi.getLogsFromSession(session.id);
return { info: session, logs };
}
async getSessionUrlFromPullRequest(pullRequest: PullRequestModel): Promise<string | undefined> {
const capi = await this.copilotApi;
if (!capi) {
return;
}
const sessions = await this.getLatestCodingAgentFromAction(pullRequest);
if (!sessions) {
return;
}
return sessions.html_url;
}
private getLatestRun<T extends { last_updated_at?: string; updated_at?: string }>(runs: T[]): T {
return runs
.slice()
.sort((a, b) => {
const dateA = new Date(a.last_updated_at ?? a.updated_at ?? 0).getTime();
const dateB = new Date(b.last_updated_at ?? b.updated_at ?? 0).getTime();
return dateB - dateA;
})[0];
}
clearNotifications() {
this._stateModel.clearNotifications();
}
get notifications(): ReadonlySet<string> {
return this._stateModel.notifications;
}
}