Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telemetry(amazonq): Adding cwsprChatProgrammingLanguage parameter to insertAtCursor and CopyAtClipboard events #5768

Merged
merged 10 commits into from
Oct 15, 2024
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present"
},
"devDependencies": {
"@aws-toolkits/telemetry": "^1.0.267",
"@aws-toolkits/telemetry": "^1.0.272",
"@playwright/browser-chromium": "^1.43.1",
"@types/vscode": "^1.68.0",
"@types/vscode-webview": "^1.57.1",
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/amazonq/webview/ui/apps/cwChatConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export class Connector {
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number,
userIntent?: string
userIntent?: string,
codeBlockLanguage?: string
): void => {
this.sendMessageToExtension({
tabID: tabID,
Expand All @@ -125,6 +126,7 @@ export class Connector {
codeBlockIndex,
totalCodeBlocks,
userIntent,
codeBlockLanguage,
})
}

Expand All @@ -137,7 +139,8 @@ export class Connector {
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number,
userIntent?: string
userIntent?: string,
codeBlockLanguage?: string
): void => {
this.sendMessageToExtension({
tabID: tabID,
Expand All @@ -151,6 +154,7 @@ export class Connector {
codeBlockIndex,
totalCodeBlocks,
userIntent,
codeBlockLanguage,
})
}

Expand Down Expand Up @@ -295,6 +299,7 @@ export class Connector {
canBeVoted: true,
codeReference: messageData.codeReference,
userIntent: messageData.userIntent,
codeBlockLanguage: messageData.codeBlockLanguage,
}

// If it is not there we will not set it
Expand Down Expand Up @@ -328,6 +333,7 @@ export class Connector {
messageId: messageData.messageID,
codeReference: messageData.codeReference,
userIntent: messageData.userIntent,
codeBlockLanguage: messageData.codeBlockLanguage,
followUp:
messageData.followUps !== undefined && messageData.followUps.length > 0
? {
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/amazonq/webview/ui/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface ChatPayload {
export interface CWCChatItem extends ChatItem {
traceId?: string
userIntent?: UserIntent
codeBlockLanguage?: string
}

export interface ConnectorProps {
Expand Down Expand Up @@ -242,7 +243,8 @@ export class Connector {
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number,
userIntent?: string
userIntent?: string,
codeBlockLanguage?: string
): void => {
switch (this.tabsStorage.getTab(tabID)?.type) {
case 'cwc':
Expand All @@ -255,7 +257,8 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
userIntent,
codeBlockLanguage
)
break
case 'featuredev':
Expand Down Expand Up @@ -331,7 +334,8 @@ export class Connector {
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number,
userIntent?: string
userIntent?: string,
codeBlockLanguage?: string
): void => {
switch (this.tabsStorage.getTab(tabID)?.type) {
case 'cwc':
Expand All @@ -344,7 +348,8 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
userIntent,
codeBlockLanguage
)
break
case 'featuredev':
Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/amazonq/webview/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const createMynahUI = (
// eslint-disable-next-line prefer-const
let connector: Connector
//Store the mapping between messageId and messageUserIntent for amazonq_interactWithMessage telemetry
const messageUserIntentMap = new Map<string, string>()
const responseMetadata = new Map<string, string[]>()

window.addEventListener('error', (e) => {
const { error, message } = e
Expand Down Expand Up @@ -252,8 +252,12 @@ export const createMynahUI = (
? { type: ChatItemType.CODE_RESULT, fileList: item.fileList }
: {}),
})
if (item.messageId !== undefined && item.userIntent !== undefined) {
messageUserIntentMap.set(item.messageId, item.userIntent)
if (
item.messageId !== undefined &&
item.userIntent !== undefined &&
item.codeBlockLanguage !== undefined
) {
responseMetadata.set(item.messageId, [item.userIntent, item.codeBlockLanguage])
}
ideApi.postMessage({
command: 'update-chat-message-telemetry',
Expand Down Expand Up @@ -515,7 +519,8 @@ export const createMynahUI = (
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
responseMetadata.get(messageId)?.[0] ?? undefined,
responseMetadata.get(messageId)?.[1] ?? undefined
)
},
onCodeBlockActionClicked: (
Expand Down Expand Up @@ -582,7 +587,8 @@ export const createMynahUI = (
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
responseMetadata.get(messageId)?.[0] ?? undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add these to ViewDiff and AcceptDiff as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that can be in different PR as these two are unavailable for Generate Tests(Beta).
@ivikash Correct me on this!

responseMetadata.get(messageId)?.[1] ?? undefined
)
mynahUI.notify({
type: NotificationType.SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { CodeScanIssue } from '../../../../codewhisperer/models/model'
import { marked } from 'marked'
import { JSDOM } from 'jsdom'
import { LspController } from '../../../../amazonq/lsp/lspController'
import { extractCodeBlockLanguage } from '../../../../shared/markdown'

export type StaticTextResponseType = 'quick-action-help' | 'onboarding-help' | 'transform' | 'help'

Expand Down Expand Up @@ -87,6 +88,7 @@ export class Messenger {
triggerID,
messageID: '',
userIntent: undefined,
codeBlockLanguage: undefined,
},
tabID
)
Expand Down Expand Up @@ -131,6 +133,7 @@ export class Messenger {
let codeReference: CodeReference[] = []
let followUps: FollowUp[] = []
let relatedSuggestions: Suggestion[] = []
let codeBlockLanguage: string = 'plaintext'

if (response.generateAssistantResponseResponse === undefined) {
throw new ToolkitError(
Expand Down Expand Up @@ -182,7 +185,9 @@ export class Messenger {
chatEvent.assistantResponseEvent.content.length > 0
) {
message += chatEvent.assistantResponseEvent.content

if (codeBlockLanguage === 'plaintext') {
codeBlockLanguage = extractCodeBlockLanguage(message)
}
this.dispatcher.sendChatMessage(
new ChatMessage(
{
Expand All @@ -195,6 +200,7 @@ export class Messenger {
triggerID,
messageID,
userIntent: triggerPayload.userIntent,
codeBlockLanguage: codeBlockLanguage,
},
tabID
)
Expand Down Expand Up @@ -272,6 +278,7 @@ export class Messenger {
triggerID,
messageID,
userIntent: triggerPayload.userIntent,
codeBlockLanguage: codeBlockLanguage,
},
tabID
)
Expand All @@ -290,6 +297,7 @@ export class Messenger {
triggerID,
messageID,
userIntent: triggerPayload.userIntent,
codeBlockLanguage: undefined,
},
tabID
)
Expand All @@ -307,6 +315,7 @@ export class Messenger {
triggerID,
messageID,
userIntent: triggerPayload.userIntent,
codeBlockLanguage: undefined,
},
tabID
)
Expand Down Expand Up @@ -431,6 +440,7 @@ export class Messenger {
triggerID,
messageID: 'static_message_' + triggerID,
userIntent: undefined,
codeBlockLanguage: undefined,
},
tabID
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface InsertCodeAtCursorPosition {
eventId: string
codeBlockIndex: number
totalCodeBlocks: number
codeBlockLanguage: string
}

export interface CopyCodeToClipboard {
Expand All @@ -59,6 +60,7 @@ export interface CopyCodeToClipboard {
eventId: string
codeBlockIndex: number
totalCodeBlocks: number
codeBlockLanguage: string
}

export interface AcceptDiff {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class CWCTelemetryHelper {
cwsprChatCodeBlockIndex: message.codeBlockIndex,
cwsprChatTotalCodeBlocks: message.totalCodeBlocks,
cwsprChatHasProjectContext: this.responseWithProjectContext.get(message.messageId),
cwsprChatProgrammingLanguage: message.codeBlockLanguage,
}
break
case 'code_was_copied_to_clipboard':
Expand All @@ -220,6 +221,7 @@ export class CWCTelemetryHelper {
cwsprChatCodeBlockIndex: message.codeBlockIndex,
cwsprChatTotalCodeBlocks: message.totalCodeBlocks,
cwsprChatHasProjectContext: this.responseWithProjectContext.get(message.messageId),
cwsprChatProgrammingLanguage: message.codeBlockLanguage,
}
break
case 'accept_diff':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface ChatMessageProps {
readonly triggerID: string
readonly messageID: string
readonly userIntent: string | undefined
readonly codeBlockLanguage: string | undefined
}

export class ChatMessage extends UiMessage {
Expand All @@ -155,6 +156,7 @@ export class ChatMessage extends UiMessage {
readonly triggerID: string
readonly messageID: string | undefined
readonly userIntent: string | undefined
readonly codeBlockLanguage: string | undefined
override type = 'chatMessage'

constructor(props: ChatMessageProps, tabID: string) {
Expand All @@ -168,6 +170,7 @@ export class ChatMessage extends UiMessage {
this.triggerID = props.triggerID
this.messageID = props.messageID
this.userIntent = props.userIntent
this.codeBlockLanguage = props.codeBlockLanguage
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class UIMessageListener {
eventId: msg.eventId,
codeBlockIndex: msg.codeBlockIndex,
totalCodeBlocks: msg.totalCodeBlocks,
codeBlockLanguage: msg.codeBlockLanguage,
})
}

Expand Down Expand Up @@ -196,6 +197,7 @@ export class UIMessageListener {
eventId: msg.eventId,
codeBlockIndex: msg.codeBlockIndex,
totalCodeBlocks: msg.totalCodeBlocks,
codeBlockLanguage: msg.codeBlockLanguage,
})
}

Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/shared/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

export const extractCodeBlockLanguage = (message: string) => {
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
const codeBlockStart = message.indexOf('```')
if (codeBlockStart === -1) {
return 'plaintext'
}

const languageStart = codeBlockStart + 3
const languageEnd = message.indexOf('\n', languageStart)

if (languageEnd === -1) {
return 'plaintext'
}

const language = message.substring(languageStart, languageEnd).trim()
return language !== '' ? language : 'plaintext'
}
Loading
Loading