|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import type * as messages from '../../webviews/sessionLogView/messages'; |
| 8 | +import { AuthProvider } from '../common/authentication'; |
| 9 | +import { Disposable } from '../common/lifecycle'; |
| 10 | +import { CopilotApi } from '../github/copilotApi'; |
| 11 | +import { CredentialStore } from '../github/credentials'; |
| 12 | +import { PullRequestModel } from '../github/pullRequestModel'; |
| 13 | +import { hasEnterpriseUri } from '../github/utils'; |
| 14 | + |
| 15 | +export class SessionLogViewManager extends Disposable { |
| 16 | + static instance: SessionLogViewManager | undefined; |
| 17 | + |
| 18 | + constructor( |
| 19 | + private readonly credentialStore: CredentialStore, |
| 20 | + private readonly context: vscode.ExtensionContext, |
| 21 | + ) { |
| 22 | + super(); |
| 23 | + |
| 24 | + SessionLogViewManager.instance = this; |
| 25 | + |
| 26 | + this._register(vscode.commands.registerCommand('padawan.openSessionLog', async () => { |
| 27 | + const copilotApi = await getCopilotApi(credentialStore); |
| 28 | + if (!copilotApi) { |
| 29 | + vscode.window.showErrorMessage(vscode.l10n.t('You must be authenticated to view sessions.')); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const allSessions = await copilotApi.getAllSessions(undefined); |
| 34 | + if (!allSessions.length) { |
| 35 | + vscode.window.showErrorMessage(vscode.l10n.t('No sessions found.')); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const sessionItems = allSessions.map(session => ({ |
| 40 | + label: session.name || session.id, |
| 41 | + description: session.created_at ? new Date(session.created_at).toLocaleString() : undefined, |
| 42 | + detail: session.id, |
| 43 | + sessionId: session.id |
| 44 | + })); |
| 45 | + |
| 46 | + const picked = await vscode.window.showQuickPick(sessionItems, { |
| 47 | + placeHolder: vscode.l10n.t('Select a session log to view') |
| 48 | + }); |
| 49 | + |
| 50 | + if (!picked) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + return this.open(picked.sessionId); |
| 55 | + })); |
| 56 | + } |
| 57 | + |
| 58 | + async openForPull(pullRequest: PullRequestModel): Promise<void> { |
| 59 | + const copilotApi = await getCopilotApi(this.credentialStore); |
| 60 | + if (!copilotApi) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const sessionId = (await copilotApi.getAllSessions(pullRequest))[0].id; |
| 65 | + if (!sessionId) { |
| 66 | + vscode.window.showErrorMessage(vscode.l10n.t('No sessions found for this pull request.')); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + return this.open(sessionId); |
| 71 | + } |
| 72 | + |
| 73 | + async open(sessionId: string): Promise<void> { |
| 74 | + const copilotApi = await getCopilotApi(this.credentialStore); |
| 75 | + if (!copilotApi) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const webviewPanel = vscode.window.createWebviewPanel('padawanSessionView', vscode.l10n.t('Session Logs'), vscode.ViewColumn.Active); |
| 80 | + |
| 81 | + const distDir = vscode.Uri.joinPath(this.context.extensionUri, 'dist'); |
| 82 | + |
| 83 | + webviewPanel.webview.options = { |
| 84 | + enableScripts: true, |
| 85 | + localResourceRoots: [ |
| 86 | + distDir |
| 87 | + ] |
| 88 | + }; |
| 89 | + webviewPanel.webview.html = `<!DOCTYPE html> |
| 90 | + <html lang="en"> |
| 91 | + <head> |
| 92 | + <meta charset="UTF-8"> |
| 93 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 94 | + <title>Session Log</title> |
| 95 | + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline' ${webviewPanel.webview.cspSource}; script-src ${webviewPanel.webview.cspSource};"> |
| 96 | + </head> |
| 97 | + <body> |
| 98 | + <div id="app"></div> |
| 99 | +
|
| 100 | + <script type="module" src="${webviewPanel.webview.asWebviewUri(vscode.Uri.joinPath(distDir, 'webview-session-log-view.js'))}"></script> |
| 101 | + </body> |
| 102 | + </html>`; |
| 103 | + |
| 104 | + const [info, logs] = await Promise.all([ |
| 105 | + copilotApi.getSessionInfo(sessionId), |
| 106 | + copilotApi.getLogsFromSession(sessionId) |
| 107 | + ]); |
| 108 | + |
| 109 | + webviewPanel.webview.postMessage({ |
| 110 | + type: 'init', |
| 111 | + info, |
| 112 | + logs, |
| 113 | + } as messages.InitMessage); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +async function getCopilotApi(credentialStore: CredentialStore): Promise<CopilotApi | undefined> { |
| 118 | + let authProvider: AuthProvider | undefined; |
| 119 | + if (credentialStore.isAuthenticated(AuthProvider.githubEnterprise) && hasEnterpriseUri()) { |
| 120 | + authProvider = AuthProvider.githubEnterprise; |
| 121 | + } else if (credentialStore.isAuthenticated(AuthProvider.github)) { |
| 122 | + authProvider = AuthProvider.github; |
| 123 | + } else { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + const github = credentialStore.getHub(authProvider); |
| 128 | + if (!github || !github.octokit) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const { token } = await github.octokit.api.auth() as { token: string }; |
| 133 | + return new CopilotApi(github.octokit, token); |
| 134 | +} |
0 commit comments