|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * This class is responsible for listening to and processing events |
| 6 | + * from the webview and translating them into events to be handled by the extension, |
| 7 | + * and events from the extension and translating them into events to be handled by the webview. |
| 8 | + */ |
| 9 | + |
| 10 | +import { ChatItem, ChatItemType } from '@aws/mynah-ui' |
| 11 | +import { ExtensionMessage } from '../commands' |
| 12 | +import { TabOpenType, TabsStorage } from '../storages/tabsStorage' |
| 13 | +import { GumbyMessageType } from '../../../../amazonqGumby/chat/views/connector/connector' //TODO |
| 14 | +import { ChatPayload } from '../connector' |
| 15 | + |
| 16 | +export interface ConnectorProps { |
| 17 | + sendMessageToExtension: (message: ExtensionMessage) => void |
| 18 | + onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void |
| 19 | + onAsyncEventProgress: (tabID: string, inProgress: boolean, message: string, messageId: string) => void |
| 20 | + onChatAnswerReceived?: (tabID: string, message: ChatItem) => void |
| 21 | + onChatAnswerUpdated?: (tabID: string, message: ChatItem) => void |
| 22 | + onQuickHandlerCommand: (tabID: string, command: string, eventId?: string) => void |
| 23 | + onError: (tabID: string, message: string, title: string) => void |
| 24 | + onWarning: (tabID: string, message: string, title: string) => void |
| 25 | + onUpdateAuthentication: (gumbyEnabled: boolean, authenticatingTabIDs: string[]) => void |
| 26 | + onChatInputEnabled: (tabID: string, enabled: boolean) => void |
| 27 | + onUpdatePlaceholder: (tabID: string, newPlaceholder: string) => void |
| 28 | + tabsStorage: TabsStorage |
| 29 | +} |
| 30 | + |
| 31 | +export interface MessageData { |
| 32 | + tabID: string |
| 33 | + type: GumbyMessageType |
| 34 | +} |
| 35 | + |
| 36 | +export class Connector { |
| 37 | + private readonly onAuthenticationUpdate |
| 38 | + private readonly sendMessageToExtension |
| 39 | + private readonly onError |
| 40 | + private readonly onChatAnswerReceived |
| 41 | + private readonly onChatAnswerUpdated |
| 42 | + private readonly chatInputEnabled |
| 43 | + private readonly onAsyncEventProgress |
| 44 | + private readonly onQuickHandlerCommand |
| 45 | + private readonly updatePlaceholder |
| 46 | + private readonly tabStorage |
| 47 | + |
| 48 | + constructor(props: ConnectorProps) { |
| 49 | + this.sendMessageToExtension = props.sendMessageToExtension |
| 50 | + this.onChatAnswerReceived = props.onChatAnswerReceived |
| 51 | + this.onChatAnswerUpdated = props.onChatAnswerUpdated |
| 52 | + this.onError = props.onError |
| 53 | + this.chatInputEnabled = props.onChatInputEnabled |
| 54 | + this.onAsyncEventProgress = props.onAsyncEventProgress |
| 55 | + this.updatePlaceholder = props.onUpdatePlaceholder |
| 56 | + this.onQuickHandlerCommand = props.onQuickHandlerCommand |
| 57 | + this.onAuthenticationUpdate = props.onUpdateAuthentication |
| 58 | + this.tabStorage = props.tabsStorage |
| 59 | + } |
| 60 | + |
| 61 | + onTabAdd = (tabID: string, tabOpenInteractionType?: TabOpenType): void => { |
| 62 | + this.sendMessageToExtension({ |
| 63 | + tabID: tabID, |
| 64 | + command: 'new-tab-was-created', |
| 65 | + tabType: 'scan', |
| 66 | + tabOpenInteractionType, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + onTabRemove(tabID: string) { |
| 71 | + this.sendMessageToExtension({ |
| 72 | + tabID: tabID, |
| 73 | + command: 'tab-was-removed', |
| 74 | + tabType: 'scan', |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + private processChatPrompt = async (messageData: any, tabID: string): Promise<void> => { |
| 79 | + if (this.onChatAnswerReceived === undefined) { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + const answer: ChatItem = { |
| 84 | + type: ChatItemType.AI_PROMPT, |
| 85 | + body: messageData.message, |
| 86 | + formItems: messageData.formItems, |
| 87 | + buttons: messageData.formButtons, |
| 88 | + followUp: undefined, |
| 89 | + status: 'info', |
| 90 | + canBeVoted: false, |
| 91 | + } |
| 92 | + |
| 93 | + this.onChatAnswerReceived(tabID, answer) |
| 94 | + |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + private processChatMessage = async (messageData: any): Promise<void> => { |
| 99 | + if (this.onChatAnswerReceived === undefined || this.onChatAnswerUpdated === undefined) { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + if (messageData.message !== undefined) { |
| 104 | + const answer: ChatItem = { |
| 105 | + type: messageData.messageType, |
| 106 | + messageId: messageData.messageId ?? messageData.triggerID, |
| 107 | + body: messageData.message, |
| 108 | + buttons: messageData.buttons ?? [], |
| 109 | + canBeVoted: false, |
| 110 | + } |
| 111 | + |
| 112 | + if (messageData.messageId !== undefined) { |
| 113 | + this.onChatAnswerUpdated(messageData.tabID, answer) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + this.onChatAnswerReceived(messageData.tabID, answer) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + transform = (tabID: string): void => { |
| 122 | + this.sendMessageToExtension({ |
| 123 | + tabID: tabID, |
| 124 | + command: 'scan', |
| 125 | + chatMessage: 'transform', |
| 126 | + tabType: 'scan', //TODO |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + requestAnswer = (tabID: string, payload: ChatPayload) => { |
| 131 | + this.tabStorage.updateTabStatus(tabID, 'busy') |
| 132 | + this.sendMessageToExtension({ |
| 133 | + tabID: tabID, |
| 134 | + command: 'chat-prompt', |
| 135 | + chatMessage: payload.chatMessage, |
| 136 | + chatCommand: payload.chatCommand, |
| 137 | + tabType: 'scan', |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + private processAuthNeededException = async (messageData: any): Promise<void> => { |
| 142 | + if (this.onChatAnswerReceived === undefined) { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + this.onChatAnswerReceived(messageData.tabID, { |
| 147 | + type: ChatItemType.SYSTEM_PROMPT, |
| 148 | + body: messageData.message, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + onCustomFormAction( |
| 153 | + tabId: string, |
| 154 | + action: { |
| 155 | + id: string |
| 156 | + text?: string | undefined |
| 157 | + formItemValues?: Record<string, string> | undefined |
| 158 | + } |
| 159 | + ) { |
| 160 | + if (action === undefined) { |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + this.sendMessageToExtension({ |
| 165 | + command: 'form-action-click', |
| 166 | + action: action.id, |
| 167 | + formSelectedValues: action.formItemValues, |
| 168 | + tabType: 'scan', |
| 169 | + tabID: tabId, |
| 170 | + }) |
| 171 | + } |
| 172 | + |
| 173 | + onResponseBodyLinkClick = (tabID: string, messageId: string, link: string): void => { |
| 174 | + this.sendMessageToExtension({ |
| 175 | + command: 'response-body-link-click', |
| 176 | + tabID, |
| 177 | + messageId, |
| 178 | + link, |
| 179 | + tabType: 'scan', |
| 180 | + }) |
| 181 | + } |
| 182 | + |
| 183 | + private processExecuteCommand = async (messageData: any): Promise<void> => { |
| 184 | + this.onQuickHandlerCommand(messageData.tabID, messageData.command, messageData.eventId) |
| 185 | + } |
| 186 | + |
| 187 | + // This handles messages received from the extension, to be forwarded to the webview |
| 188 | + handleMessageReceive = async (messageData: { type: GumbyMessageType } & Record<string, any>) => { |
| 189 | + switch (messageData.type) { |
| 190 | + case 'asyncEventProgressMessage': |
| 191 | + this.onAsyncEventProgress( |
| 192 | + messageData.tabID, |
| 193 | + messageData.inProgress, |
| 194 | + messageData.message, |
| 195 | + messageData.messageId |
| 196 | + ) |
| 197 | + break |
| 198 | + case 'authNeededException': |
| 199 | + await this.processAuthNeededException(messageData) |
| 200 | + break |
| 201 | + case 'authenticationUpdateMessage': |
| 202 | + this.onAuthenticationUpdate(messageData.gumbyEnabled, messageData.authenticatingTabIDs) |
| 203 | + break |
| 204 | + case 'chatInputEnabledMessage': |
| 205 | + this.chatInputEnabled(messageData.tabID, messageData.enabled) |
| 206 | + break |
| 207 | + case 'chatMessage': |
| 208 | + await this.processChatMessage(messageData) |
| 209 | + break |
| 210 | + case 'chatPrompt': |
| 211 | + await this.processChatPrompt(messageData, messageData.tabID) |
| 212 | + break |
| 213 | + case 'errorMessage': |
| 214 | + this.onError(messageData.tabID, messageData.message, messageData.title) |
| 215 | + break |
| 216 | + case 'sendCommandMessage': |
| 217 | + await this.processExecuteCommand(messageData) |
| 218 | + break |
| 219 | + case 'updatePlaceholderMessage': |
| 220 | + this.updatePlaceholder(messageData.tabID, messageData.newPlaceholder) |
| 221 | + break |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments