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 #4959

Merged
merged 19 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
private val codeReferences = mutableListOf<CodeReference>()
private var requestId: String = ""
private var statusCode: Int = 0
private var codeBlockLanguage: String = "plaintext"

companion object {
private val CODE_BLOCK_PATTERN = Regex("<pre>\\s*<code")
Expand Down Expand Up @@ -143,6 +144,24 @@
}
}

private fun extractCodeBlockLanguage(message: String): String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved extractCodeBlockLanguage to shared/TextUtils file and added test cases.

// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
val codeBlockStart = message.indexOf("```")
if (codeBlockStart == -1) {
return "plaintext"
laileni-aws marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

val language = message.substring(languageStart, languageEnd).trim()
return if (language.isNotEmpty()) language else "plaintext"

Check notice on line 162 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/messenger/ChatPromptHandler.kt

View workflow job for this annotation

GitHub Actions / qodana

'if' condition can be replaced with lambda call

Replace with 'ifEmpty {...}'
}

private fun processChatEvent(
tabId: String,
triggerId: String,
Expand Down Expand Up @@ -209,6 +228,10 @@
} else {
responseText.toString()
}
if (codeBlockLanguage == "plaintext") {
// To get the language of generated code in Q chat.
codeBlockLanguage = extractCodeBlockLanguage(message)
}
ChatMessage(
tabId = tabId,
triggerId = triggerId,
Expand All @@ -217,6 +240,7 @@
message = message,
codeReference = codeReferences,
userIntent = data.userIntent,
codeBlockLanguage = codeBlockLanguage,
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
credentialStartUrl = getStartUrl(context.project),
cwsprChatCodeBlockIndex = message.codeBlockIndex?.toLong(),
cwsprChatTotalCodeBlocks = message.totalCodeBlocks?.toLong(),
cwsprChatHasProjectContext = getMessageHasProjectContext(message.messageId)
cwsprChatHasProjectContext = getMessageHasProjectContext(message.messageId),
cwsprChatProgrammingLanguage = message.codeBlockLanguage,
)
ChatInteractWithMessageEvent.builder().apply {
conversationId(getConversationId(message.tabId).orEmpty())
Expand All @@ -244,7 +245,8 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
credentialStartUrl = getStartUrl(context.project),
cwsprChatCodeBlockIndex = message.codeBlockIndex?.toLong(),
cwsprChatTotalCodeBlocks = message.totalCodeBlocks?.toLong(),
cwsprChatHasProjectContext = getMessageHasProjectContext(message.messageId)
cwsprChatHasProjectContext = getMessageHasProjectContext(message.messageId),
cwsprChatProgrammingLanguage = message.codeBlockLanguage,
)
ChatInteractWithMessageEvent.builder().apply {
conversationId(getConversationId(message.tabId).orEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ sealed interface IncomingCwcMessage : CwcMessage {
val eventId: String?,
val codeBlockIndex: Int?,
val totalCodeBlocks: Int?,
val codeBlockLanguage: String?,
) : IncomingCwcMessage

data class InsertCodeAtCursorPosition(
Expand All @@ -84,6 +85,7 @@ sealed interface IncomingCwcMessage : CwcMessage {
val eventId: String?,
val codeBlockIndex: Int?,
val totalCodeBlocks: Int?,
val codeBlockLanguage: String?,
) : IncomingCwcMessage

data class TriggerTabIdReceived(
Expand Down Expand Up @@ -214,6 +216,7 @@ data class ChatMessage(
val relatedSuggestions: List<Suggestion>? = null,
val codeReference: List<CodeReference>? = null,
val userIntent: UserIntent? = null,
val codeBlockLanguage: String? = "plaintext",
) : UiMessage(
tabId = tabId,
type = "chatMessage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ class TelemetryHelperTest {
"insertionTargetType",
"eventId",
codeBlockIndex,
totalCodeBlocks
totalCodeBlocks,
lang
)
)

Expand Down Expand Up @@ -484,7 +485,8 @@ class TelemetryHelperTest {
emptyList(),
eventId,
codeBlockIndex,
totalCodeBlocks
totalCodeBlocks,
lang
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class Connector {
codeBlockIndex?: number,
totalCodeBlocks?: number,
userIntent?: string,
codeBlockLanguage?: string,
): void => {
this.sendMessageToExtension({
tabID: tabID,
Expand All @@ -113,7 +114,8 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
userIntent,
codeBlockLanguage
})
}

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

Expand Down Expand Up @@ -271,6 +275,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 @@ -304,6 +309,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
15 changes: 10 additions & 5 deletions plugins/amazonq/mynah-ui/src/mynah-ui/ui/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export interface ChatPayload {
}

export interface CWCChatItem extends ChatItem {
userIntent?: string
userIntent?: string,
codeBlockLanguage?: string,
}

export interface ConnectorProps {
Expand Down Expand Up @@ -235,7 +236,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 @@ -248,7 +250,8 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
userIntent,
codeBlockLanguage
)
break
case 'featuredev':
Expand All @@ -266,7 +269,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 @@ -279,7 +283,8 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
userIntent,
codeBlockLanguage
)
break
case 'featuredev':
Expand Down
12 changes: 7 additions & 5 deletions plugins/amazonq/mynah-ui/src/mynah-ui/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
let mynahUI: MynahUI
// eslint-disable-next-line prefer-const
let connector: Connector
const messageUserIntentMap = new Map<string, string>()
const responseMetadata = new Map<string, string[]>()

const tabsStorage = new TabsStorage({
onTabTimeout: tabID => {
Expand Down Expand Up @@ -254,8 +254,8 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
? { 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])
}
return
}
Expand Down Expand Up @@ -466,7 +466,8 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
responseMetadata.get(messageId)?.[0] ?? undefined,
responseMetadata.get(messageId)?.[1] ?? undefined
)
break
case 'copy':
Expand All @@ -479,7 +480,8 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
responseMetadata.get(messageId)?.[0] ?? undefined,
responseMetadata.get(messageId)?.[1] ?? undefined
)
mynahUI.notify({
type: NotificationType.SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,10 @@
{
"type": "cwsprChatHasProjectContext",
"required": false
},
laileni-aws marked this conversation as resolved.
Show resolved Hide resolved
{
"type": "cwsprChatProgrammingLanguage",
"required": false
}
]
},
Expand Down
Loading