From bbcd984e6cefd3c2a2ca3695b94becfb3650b365 Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Wed, 30 Apr 2025 07:59:25 -0400 Subject: [PATCH] feat(amazonq): add reference log for chat messages --- packages/amazonq/src/lsp/chat/messages.ts | 14 +++++++++++--- packages/core/src/amazonq/commons/model.ts | 10 ++++++++++ packages/core/src/amazonq/index.ts | 2 +- packages/core/src/amazonqDoc/session/session.ts | 2 +- packages/core/src/amazonqFeatureDev/constants.ts | 8 -------- .../core/src/amazonqFeatureDev/session/session.ts | 4 +++- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/amazonq/src/lsp/chat/messages.ts b/packages/amazonq/src/lsp/chat/messages.ts index 6d2d9947dde..37acb43e7b9 100644 --- a/packages/amazonq/src/lsp/chat/messages.ts +++ b/packages/amazonq/src/lsp/chat/messages.ts @@ -57,13 +57,14 @@ import * as vscode from 'vscode' import { Disposable, LanguageClient, Position, TextDocumentIdentifier } from 'vscode-languageclient' import * as jose from 'jose' import { AmazonQChatViewProvider } from './webviewProvider' -import { AuthUtil } from 'aws-core-vscode/codewhisperer' +import { AuthUtil, ReferenceLogViewProvider } from 'aws-core-vscode/codewhisperer' import { amazonQDiffScheme, AmazonQPromptSettings, messages, openUrl } from 'aws-core-vscode/shared' import { DefaultAmazonQAppInitContext, messageDispatcher, EditorContentController, ViewDiffMessage, + referenceLogText, } from 'aws-core-vscode/amazonq' import { telemetry, TelemetryBase } from 'aws-core-vscode/telemetry' import { isValidResponseError } from './error' @@ -531,13 +532,17 @@ async function handlePartialResult( tabId: tabId, }) } + + for (const ref of decryptedMessage.codeReference ?? []) { + ReferenceLogViewProvider.instance.addReferenceLog(referenceLogText(ref)) + } } /** * Decodes the final chat responses from the language server before sending it to mynah UI. * Once this is called the answer response is finished */ -async function handleCompleteResult( +async function handleCompleteResult( result: string | T, encryptionKey: Buffer | undefined, provider: AmazonQChatViewProvider, @@ -545,12 +550,15 @@ async function handleCompleteResult( disposable: Disposable ) { const decryptedMessage = - typeof result === 'string' && encryptionKey ? await decodeRequest(result, encryptionKey) : result + typeof result === 'string' && encryptionKey ? await decodeRequest(result, encryptionKey) : (result as T) void provider.webview?.postMessage({ command: chatRequestType.method, params: decryptedMessage, tabId: tabId, }) + for (const ref of decryptedMessage.codeReference ?? []) { + ReferenceLogViewProvider.instance.addReferenceLog(referenceLogText(ref)) + } disposable.dispose() } diff --git a/packages/core/src/amazonq/commons/model.ts b/packages/core/src/amazonq/commons/model.ts index 766c5160262..3c5b228d2fc 100644 --- a/packages/core/src/amazonq/commons/model.ts +++ b/packages/core/src/amazonq/commons/model.ts @@ -3,6 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { LicenseUtil } from '../../codewhisperer/util/licenseUtil' +import { CodeReference } from '../../codewhispererChat/view/connector/connector' + // Currently importing ChatItemType in Mynah UI from the vscode side causes an error // TODO remove this once the import stops failing export type ChatItemType = @@ -13,3 +16,10 @@ export type ChatItemType = | 'answer-stream' | 'answer-part' | 'code-result' + +export const referenceLogText = (reference: CodeReference) => + `[${new Date().toLocaleString()}] Accepted recommendation from Amazon Q. Code provided with reference under ${reference.licenseName} license from repository ${reference.repository}.

` diff --git a/packages/core/src/amazonq/index.ts b/packages/core/src/amazonq/index.ts index 168915a8434..14c0e4a59a0 100644 --- a/packages/core/src/amazonq/index.ts +++ b/packages/core/src/amazonq/index.ts @@ -35,7 +35,7 @@ export { computeDiff, } from './commons/diff' export { AuthFollowUpType, AuthMessageDataMap } from './auth/model' -export { ChatItemType } from './commons/model' +export { ChatItemType, referenceLogText } from './commons/model' export { ExtensionMessage } from '../amazonq/webview/ui/commands' export { CodeReference } from '../codewhispererChat/view/connector/connector' export { extractAuthFollowUp } from './util/authUtils' diff --git a/packages/core/src/amazonqDoc/session/session.ts b/packages/core/src/amazonqDoc/session/session.ts index b52474ffdd4..e3eb29d6d32 100644 --- a/packages/core/src/amazonqDoc/session/session.ts +++ b/packages/core/src/amazonqDoc/session/session.ts @@ -15,7 +15,7 @@ import { TelemetryHelper } from '../../amazonq/util/telemetryHelper' import { ConversationNotStartedState } from '../../amazonqFeatureDev/session/sessionState' import { logWithConversationId } from '../../amazonqFeatureDev/userFacingText' import { ConversationIdNotFoundError, IllegalStateError } from '../../amazonqFeatureDev/errors' -import { referenceLogText } from '../../amazonqFeatureDev/constants' +import { referenceLogText } from '../../amazonq/commons/model' import { DocInteractionType, DocV2AcceptanceEvent, diff --git a/packages/core/src/amazonqFeatureDev/constants.ts b/packages/core/src/amazonqFeatureDev/constants.ts index 4bc3ca1fdb2..78cae972cc3 100644 --- a/packages/core/src/amazonqFeatureDev/constants.ts +++ b/packages/core/src/amazonqFeatureDev/constants.ts @@ -26,14 +26,6 @@ export const clientErrorMessages = [ 'The folder you chose did not contain any source files in a supported language. Choose another folder and try again.', ] -// License text that's used in codewhisperer reference log -export const referenceLogText = (reference: CodeReference) => - `[${new Date().toLocaleString()}] Accepted recommendation from Amazon Q. Code provided with reference under ${reference.licenseName} license from repository ${reference.repository}.

` - // License text that's used in the file view export const licenseText = (reference: CodeReference) => `${ diff --git a/packages/core/src/amazonqFeatureDev/session/session.ts b/packages/core/src/amazonqFeatureDev/session/session.ts index d2b174f0f6f..c1fc81a4701 100644 --- a/packages/core/src/amazonqFeatureDev/session/session.ts +++ b/packages/core/src/amazonqFeatureDev/session/session.ts @@ -15,7 +15,7 @@ import { UpdateFilesPathsParams, } from '../../amazonq/commons/types' import { ContentLengthError, ConversationIdNotFoundError, IllegalStateError } from '../errors' -import { featureDevChat, referenceLogText, featureDevScheme } from '../constants' +import { featureDevChat, featureDevScheme } from '../constants' import fs from '../../shared/fs/fs' import { FeatureDevClient } from '../client/featureDev' import { codeGenRetryLimit } from '../limits' @@ -34,6 +34,8 @@ import { FollowUpTypes } from '../../amazonq/commons/types' import { SessionConfig } from '../../amazonq/commons/session/sessionConfigFactory' import { Messenger } from '../../amazonq/commons/connector/baseMessenger' import { ContentLengthError as CommonContentLengthError } from '../../shared/errors' +import { referenceLogText } from '../../amazonq/commons/model' + export class Session { private _state?: SessionState | Omit private task: string = ''