|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { AuthUtil } from 'aws-core-vscode/codewhisperer' |
| 7 | +import { AuthStatus } from '../../../core/dist/src/shared/telemetry/telemetry' |
| 8 | +import { AwsConnection } from 'aws-core-vscode/auth' |
| 9 | +import { activateExtension, getLogger } from 'aws-core-vscode/shared' |
| 10 | +import { VSCODE_EXTENSION_ID } from 'aws-core-vscode/utils' |
| 11 | + |
| 12 | +/** Provides the status of the Auth connection for Amazon Q, specifically for telemetry purposes. */ |
| 13 | +export async function getAuthStatus(): Promise<AuthStatus> { |
| 14 | + // Get auth state from the Amazon Q extension |
| 15 | + const authState = (await AuthUtil.instance.getChatAuthState()).codewhispererChat |
| 16 | + let authStatus: AuthStatus = authState === 'connected' || authState === 'expired' ? authState : 'notConnected' |
| 17 | + |
| 18 | + // If the Q extension does not have its own connection, it will fallback and check |
| 19 | + // if the Toolkit extension can provide a connection that works with Q |
| 20 | + if (authStatus === 'notConnected') { |
| 21 | + let autoConnectConn: AwsConnection | undefined = undefined |
| 22 | + try { |
| 23 | + autoConnectConn = await getAutoConnectableConnection() |
| 24 | + } catch (e) { |
| 25 | + getLogger().error(`Failed ${getAutoConnectableConnection.name}:\n\n%s`, JSON.stringify(e)) |
| 26 | + } |
| 27 | + |
| 28 | + // Determine the status of the Toolkit connection we will autoconnect to |
| 29 | + if (autoConnectConn) { |
| 30 | + authStatus = autoConnectConn.state === 'valid' ? 'connected' : 'expired' |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return authStatus |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Returns a connection from the standalone Toolkit extension that |
| 39 | + * the Amazon Q extension can use. Otherwise it returns undefined. |
| 40 | + * |
| 41 | + * HACK: Our frontend Login ui for Amazon Q will auto connect if required/possible, |
| 42 | + * but we cannot detect this at the end of Amazon Q extension activation. |
| 43 | + * So we reuse the same {@link findUsableQConnection}() |
| 44 | + * and assume that the frontend will have the same result and auto connect. |
| 45 | + */ |
| 46 | +async function getAutoConnectableConnection(): Promise<AwsConnection | undefined> { |
| 47 | + const extension = await activateExtension<any>(VSCODE_EXTENSION_ID.awstoolkit) |
| 48 | + if (!extension) { |
| 49 | + return undefined |
| 50 | + } |
| 51 | + const importedApis = extension.exports.getApi(VSCODE_EXTENSION_ID.awstoolkit) |
| 52 | + |
| 53 | + const listConnections: () => Promise<AwsConnection[]> = importedApis?.listConnections |
| 54 | + if (!listConnections) { |
| 55 | + // Either the user has an older toolkit version w/o the API, or the API has changed |
| 56 | + // and this needs to be updated. |
| 57 | + return undefined |
| 58 | + } |
| 59 | + |
| 60 | + return AuthUtil.instance.findUsableQConnection(await listConnections()) |
| 61 | +} |
0 commit comments