Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -58,7 +58,14 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
.onStart {
// The first thing we always send back is an AnswerStream message to indicate the beginning of a streaming answer
val response =
ChatMessage(tabId = tabId, triggerId = triggerId, messageId = requestId, messageType = ChatMessageType.AnswerStream, message = "")
ChatMessage(
tabId = tabId,
triggerId = triggerId,
messageId = requestId,
messageType = ChatMessageType.AnswerStream,
message = "",
userIntent = data.userIntent,
)

telemetryHelper.setResponseStreamStartTime(tabId)
emit(response)
Expand All @@ -81,13 +88,20 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
messageType = ChatMessageType.AnswerPart,
message = responseText.toString(),
relatedSuggestions = relatedSuggestions,
userIntent = data.userIntent,
)
emit(suggestionMessage)
}

// Send the Answer message to indicate the end of the response stream
val response =
ChatMessage(tabId = tabId, triggerId = triggerId, messageId = requestId, messageType = ChatMessageType.Answer, followUps = followUps)
val response = ChatMessage(
tabId = tabId,
triggerId = triggerId,
messageId = requestId,
messageType = ChatMessageType.Answer,
followUps = followUps,
userIntent = data.userIntent,
)

telemetryHelper.setResponseStreamTotalTime(tabId)
telemetryHelper.setResponseHasProjectContext(
Expand Down Expand Up @@ -119,11 +133,23 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
}
}
.collect { responseEvent ->
processChatEvent(tabId, triggerId, responseEvent, shouldAddIndexInProgressMessage)?.let { emit(it) }
processChatEvent(
tabId,
triggerId,
data,
responseEvent,
shouldAddIndexInProgressMessage
)?.let { emit(it) }
}
}

private fun processChatEvent(tabId: String, triggerId: String, event: ChatResponseEvent, shouldAddIndexInProgressMessage: Boolean): ChatMessage? {
private fun processChatEvent(
tabId: String,
triggerId: String,
data: ChatRequestData,
event: ChatResponseEvent,
shouldAddIndexInProgressMessage: Boolean,
): ChatMessage? {
requestId = event.requestId
statusCode = event.statusCode

Expand Down Expand Up @@ -190,6 +216,7 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
messageType = ChatMessageType.AnswerPart,
message = message,
codeReference = codeReferences,
userIntent = data.userIntent,
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ data class ChatMessage(
val followUpsHeader: String? = null,
val relatedSuggestions: List<Suggestion>? = null,
val codeReference: List<CodeReference>? = null,
val userIntent: UserIntent? = null,
) : UiMessage(
tabId = tabId,
type = "chatMessage",
Expand Down
21 changes: 14 additions & 7 deletions plugins/amazonq/mynah-ui/src/mynah-ui/ui/apps/cwChatConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { ChatItem, ChatItemAction, ChatItemType, FeedbackPayload } from '@aws/mynah-ui-chat'
import { ChatItemAction, ChatItemType, FeedbackPayload } from '@aws/mynah-ui-chat'
import { ExtensionMessage } from '../commands'
import { CodeReference } from './amazonqCommonsConnector'
import { TabOpenType, TabsStorage } from '../storages/tabsStorage'
import { FollowUpGenerator } from '../followUps/generator'
import { CWCChatItem } from "../connector";

interface ChatPayload {
chatMessage: string
Expand All @@ -17,8 +18,8 @@ interface ChatPayload {
export interface ConnectorProps {
sendMessageToExtension: (message: ExtensionMessage) => void
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
onChatAnswerReceived?: (tabID: string, message: ChatItem) => void
onCWCContextCommandMessage: (message: ChatItem, command?: string) => string | undefined
onChatAnswerReceived?: (tabID: string, message: CWCChatItem) => void
onCWCContextCommandMessage: (message: CWCChatItem, command?: string) => string | undefined
onError: (tabID: string, message: string, title: string) => void
onWarning: (tabID: string, message: string, title: string) => void
onOpenSettingsMessage: (tabID: string) => void
Expand Down Expand Up @@ -98,7 +99,8 @@ export class Connector {
codeReference?: CodeReference[],
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number
totalCodeBlocks?: number,
userIntent?: string,
): void => {
this.sendMessageToExtension({
tabID: tabID,
Expand All @@ -111,6 +113,7 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
})
}

Expand All @@ -122,7 +125,8 @@ export class Connector {
codeReference?: CodeReference[],
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number
totalCodeBlocks?: number,
userIntent?: string,
): void => {
this.sendMessageToExtension({
tabID: tabID,
Expand All @@ -135,6 +139,7 @@ export class Connector {
eventId,
codeBlockIndex,
totalCodeBlocks,
userIntent
})
}

Expand Down Expand Up @@ -258,13 +263,14 @@ export class Connector {
}
: undefined

const answer: ChatItem = {
const answer: CWCChatItem = {
type: messageData.messageType,
messageId: messageData.messageId ?? messageData.triggerID,
body: messageData.message,
followUp: followUps,
canBeVoted: true,
codeReference: messageData.codeReference,
userIntent: messageData.userIntent,
}

// If it is not there we will not set it
Expand All @@ -291,12 +297,13 @@ export class Connector {
return
}
if (messageData.messageType === ChatItemType.ANSWER) {
const answer: ChatItem = {
const answer: CWCChatItem = {
type: messageData.messageType,
body: undefined,
relatedContent: undefined,
messageId: messageData.messageId,
codeReference: messageData.codeReference,
userIntent: messageData.userIntent,
followUp:
messageData.followUps !== undefined && messageData.followUps.length > 0
? {
Expand Down
16 changes: 12 additions & 4 deletions plugins/amazonq/mynah-ui/src/mynah-ui/ui/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface ChatPayload {
chatCommand?: string
}

export interface CWCChatItem extends ChatItem {
userIntent?: string
}

export interface ConnectorProps {
sendMessageToExtension: (message: ExtensionMessage) => void
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
Expand Down Expand Up @@ -230,7 +234,8 @@ export class Connector {
codeReference?: CodeReference[],
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number
totalCodeBlocks?: number,
userIntent?: string
): void => {
switch (this.tabsStorage.getTab(tabID)?.type) {
case 'cwc':
Expand All @@ -242,7 +247,8 @@ export class Connector {
codeReference,
eventId,
codeBlockIndex,
totalCodeBlocks
totalCodeBlocks,
userIntent
)
break
case 'featuredev':
Expand All @@ -259,7 +265,8 @@ export class Connector {
codeReference?: CodeReference[],
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number
totalCodeBlocks?: number,
userIntent?: string
): void => {
switch (this.tabsStorage.getTab(tabID)?.type) {
case 'cwc':
Expand All @@ -271,7 +278,8 @@ export class Connector {
codeReference,
eventId,
codeBlockIndex,
totalCodeBlocks
totalCodeBlocks,
userIntent
)
break
case 'featuredev':
Expand Down
81 changes: 54 additions & 27 deletions plugins/amazonq/mynah-ui/src/mynah-ui/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { Connector } from './connector'
import { ChatItem, ChatItemType, MynahIcons, MynahUI, MynahUIDataModel, NotificationType } from '@aws/mynah-ui-chat'
import { Connector, CWCChatItem } from './connector'
import {ChatItem, ChatItemType, MynahIcons, MynahUI, MynahUIDataModel, NotificationType, ReferenceTrackerInformation} from '@aws/mynah-ui-chat'
import './styles/dark.scss'
import { TabsStorage, TabType } from './storages/tabsStorage'
import { WelcomeFollowupType } from './apps/amazonqCommonsConnector'
Expand All @@ -17,12 +17,15 @@ import { MessageController } from './messages/controller'
import { getActions, getDetails } from './diffTree/actions'
import { DiffTreeFileInfo } from './diffTree/types'
import './styles.css'
import {CodeSelectionType} from "@aws/mynah-ui-chat/dist/static";

export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeTransformInitEnabled: boolean) => {
// eslint-disable-next-line prefer-const
let mynahUI: MynahUI
// eslint-disable-next-line prefer-const
let connector: Connector
const messageUserIntentMap = new Map<string, string>()

const tabsStorage = new TabsStorage({
onTabTimeout: tabID => {
mynahUI.addChatItem(tabID, {
Expand Down Expand Up @@ -239,7 +242,7 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
sendMessageToExtension: message => {
ideApi.postMessage(message)
},
onChatAnswerReceived: (tabID: string, item: ChatItem) => {
onChatAnswerReceived: (tabID: string, item: CWCChatItem) => {
if (item.type === ChatItemType.ANSWER_PART || item.type === ChatItemType.CODE_RESULT) {
mynahUI.updateLastChatAnswer(tabID, {
...(item.messageId !== undefined ? { messageId: item.messageId } : {}),
Expand All @@ -251,6 +254,9 @@ 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)
}
return
}

Expand Down Expand Up @@ -437,31 +443,52 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
content: 'Thanks for your feedback.',
})
},
onCodeInsertToCursorPosition: connector.onCodeInsertToCursorPosition,
onCopyCodeToClipboard: (
tabId,
messageId,
code,
type,
referenceTrackerInfo,
eventId,
codeBlockIndex,
totalCodeBlocks
onCodeBlockActionClicked: (
tabId: string,
messageId: string,
actionId: string,
data?: string,
code?: string,
type?: CodeSelectionType,
referenceTrackerInformation?: ReferenceTrackerInformation[],
eventId?: string,
codeBlockIndex?: number,
totalCodeBlocks?: number
) => {
connector.onCopyCodeToClipboard(
tabId,
messageId,
code,
type,
referenceTrackerInfo,
eventId,
codeBlockIndex,
totalCodeBlocks
)
mynahUI.notify({
type: NotificationType.SUCCESS,
content: 'Selected code is copied to clipboard',
})
switch (actionId) {
case 'insert-to-cursor':
connector.onCodeInsertToCursorPosition(
tabId,
messageId,
code,
type,
referenceTrackerInformation,
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
)
break
case 'copy':
connector.onCopyCodeToClipboard(
tabId,
messageId,
code,
type,
referenceTrackerInformation,
eventId,
codeBlockIndex,
totalCodeBlocks,
messageUserIntentMap.get(messageId) ?? undefined
)
mynahUI.notify({
type: NotificationType.SUCCESS,
content: 'Selected code is copied to clipboard',
})
break
default:
break
}
},
onChatItemEngagement: connector.triggerSuggestionEngagement,
onSourceLinkClick: (tabId, messageId, link, mouseEvent) => {
Expand Down
Loading