Skip to content

Commit ec122a3

Browse files
committed
Refactoring code
1 parent 1d54c6e commit ec122a3

File tree

10 files changed

+62
-48
lines changed

10 files changed

+62
-48
lines changed

packages/core/src/amazonq/webview/ui/apps/cwChatConnector.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ConnectorProps {
1818
sendMessageToExtension: (message: ExtensionMessage) => void
1919
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
2020
onChatAnswerReceived?: (tabID: string, message: ChatItem) => void
21+
onChatAnswerStreamEnded?: (tabID: string, messageId: string) => void
2122
onCWCContextCommandMessage: (message: ChatItem, command?: string) => string | undefined
2223
onError: (tabID: string, message: string, title: string) => void
2324
onWarning: (tabID: string, message: string, title: string) => void
@@ -29,12 +30,14 @@ export class Connector {
2930
private readonly onError
3031
private readonly onWarning
3132
private readonly onChatAnswerReceived
33+
private readonly onChatAnswerStreamEnded
3234
private readonly onCWCContextCommandMessage
3335
private readonly followUpGenerator: FollowUpGenerator
3436

3537
constructor(props: ConnectorProps) {
3638
this.sendMessageToExtension = props.sendMessageToExtension
3739
this.onChatAnswerReceived = props.onChatAnswerReceived
40+
this.onChatAnswerStreamEnded = props.onChatAnswerStreamEnded
3841
this.onWarning = props.onWarning
3942
this.onError = props.onError
4043
this.onCWCContextCommandMessage = props.onCWCContextCommandMessage
@@ -123,10 +126,10 @@ export class Connector {
123126
})
124127
}
125128

126-
handleMessageofStreamedData = (tabID: string, totalCodeBlocks: number, messageId: string) => {
129+
onTotalCodeBlocksReceived = (tabID: string, totalCodeBlocks: number, messageId: string) => {
127130
this.sendMessageToExtension({
128131
tabID: tabID,
129-
command: 'message-of-streamed-data',
132+
command: 'chat-stream-ended',
130133
tabType: 'cwc',
131134
messageId: messageId,
132135
totalCodeBlocks,
@@ -306,6 +309,14 @@ export class Connector {
306309
}
307310
}
308311

312+
private processChatStreamEnded = async (messageData: any): Promise<void> => {
313+
if (this.onChatAnswerStreamEnded === undefined) {
314+
return
315+
}
316+
317+
this.onChatAnswerStreamEnded(messageData.tabID, messageData.messageID)
318+
}
319+
309320
private processAuthNeededException = async (messageData: any): Promise<void> => {
310321
if (this.onChatAnswerReceived === undefined) {
311322
return
@@ -337,6 +348,11 @@ export class Connector {
337348
return
338349
}
339350

351+
if (messageData.type === 'chatStreamEnded') {
352+
await this.processChatStreamEnded(messageData)
353+
return
354+
}
355+
340356
if (messageData.type === 'editorContextCommandMessage') {
341357
await this.processEditorContextCommandMessage(messageData)
342358
return

packages/core/src/amazonq/webview/ui/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ type MessageCommand =
3030
| 'footer-info-link-click'
3131
| 'file-click'
3232
| 'form-action-click'
33-
| 'message-of-streamed-data'
33+
| 'chat-stream-ended'
3434

3535
export type ExtensionMessage = Record<string, any> & { command: MessageCommand }

packages/core/src/amazonq/webview/ui/connector.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface ConnectorProps {
4848
onNewTab: (tabType: TabType) => void
4949
onFileActionClick: (tabID: string, messageId: string, filePath: string, actionName: string) => void
5050
tabsStorage: TabsStorage
51+
onChatAnswerStreamEnded?: (tabID: string, messageId: string) => void
5152
}
5253

5354
export class Connector {
@@ -196,10 +197,10 @@ export class Connector {
196197
}
197198
}
198199

199-
handleMessageofStreamedData = (tabID: string, totalCodeBlocks: number, messageId: string): void => {
200+
onTotalCodeBlocksReceived = (tabID: string, totalCodeBlocks: number, messageId: string): void => {
200201
switch (this.tabsStorage.getTab(tabID)?.type) {
201202
case 'cwc':
202-
this.cwChatConnector.handleMessageofStreamedData(tabID, totalCodeBlocks, messageId)
203+
this.cwChatConnector.onTotalCodeBlocksReceived(tabID, totalCodeBlocks, messageId)
203204
break
204205
}
205206
}

packages/core/src/amazonq/webview/ui/main.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,19 @@ export const createMynahUI = (ideApi: any, amazonQEnabled: boolean) => {
223223
}
224224

225225
if (item.type === ChatItemType.ANSWER) {
226-
if (item.messageId !== undefined) {
227-
const count = mynahUI.endMessageStream(tabID, item.messageId).totalNumberOfCodeBlocks
228-
connector.handleMessageofStreamedData(tabID, count === undefined ? 0 : count, item.messageId)
229-
}
230226
mynahUI.updateStore(tabID, {
231227
loadingChat: false,
232228
promptInputDisabledState: tabsStorage.isTabDead(tabID),
233229
})
234230
tabsStorage.updateTabStatus(tabID, 'free')
235231
}
236232
},
233+
onChatAnswerStreamEnded: (tabID: string, messageId: string) => {
234+
if (messageId !== undefined) {
235+
const count = mynahUI.endMessageStream(tabID, messageId).totalNumberOfCodeBlocks
236+
connector.onTotalCodeBlocksReceived(tabID, count === undefined ? 0 : count, messageId)
237+
}
238+
},
237239
onMessageReceived: (tabID: string, messageData: MynahUIDataModel) => {
238240
mynahUI.updateStore(tabID, messageData)
239241
},

packages/core/src/codewhispererChat/app.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function init(appContext: AmazonQAppInitContext) {
4747
processSourceLinkClick: new EventEmitter<SourceLinkClickMessage>(),
4848
processResponseBodyLinkClick: new EventEmitter<ResponseBodyLinkClickMessage>(),
4949
processFooterInfoLinkClick: new EventEmitter<FooterInfoLinkClick>(),
50-
processMessageofStreamedData: new EventEmitter<MessageofStreamedData>(),
50+
processTotalCodeBlocksReceived: new EventEmitter<MessageofStreamedData>(),
5151
}
5252

5353
const cwChatControllerMessageListeners = {
@@ -97,8 +97,8 @@ export function init(appContext: AmazonQAppInitContext) {
9797
processFooterInfoLinkClick: new MessageListener<FooterInfoLinkClick>(
9898
cwChatControllerEventEmitters.processFooterInfoLinkClick
9999
),
100-
processMessageofStreamedData: new MessageListener<MessageofStreamedData>(
101-
cwChatControllerEventEmitters.processMessageofStreamedData
100+
processTotalCodeBlocksReceived: new MessageListener<MessageofStreamedData>(
101+
cwChatControllerEventEmitters.processTotalCodeBlocksReceived
102102
),
103103
}
104104

@@ -151,8 +151,8 @@ export function init(appContext: AmazonQAppInitContext) {
151151
processFooterInfoLinkClick: new MessagePublisher<FooterInfoLinkClick>(
152152
cwChatControllerEventEmitters.processFooterInfoLinkClick
153153
),
154-
processMessageofStreamedData: new MessagePublisher<MessageofStreamedData>(
155-
cwChatControllerEventEmitters.processMessageofStreamedData
154+
processTotalCodeBlocksReceived: new MessagePublisher<MessageofStreamedData>(
155+
cwChatControllerEventEmitters.processTotalCodeBlocksReceived
156156
),
157157
}
158158

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface ChatControllerMessagePublishers {
6565
readonly processSourceLinkClick: MessagePublisher<SourceLinkClickMessage>
6666
readonly processResponseBodyLinkClick: MessagePublisher<ResponseBodyLinkClickMessage>
6767
readonly processFooterInfoLinkClick: MessagePublisher<FooterInfoLinkClick>
68-
readonly processMessageofStreamedData: MessagePublisher<MessageofStreamedData>
68+
readonly processTotalCodeBlocksReceived: MessagePublisher<MessageofStreamedData>
6969
}
7070

7171
export interface ChatControllerMessageListeners {
@@ -85,7 +85,7 @@ export interface ChatControllerMessageListeners {
8585
readonly processSourceLinkClick: MessageListener<SourceLinkClickMessage>
8686
readonly processResponseBodyLinkClick: MessageListener<ResponseBodyLinkClickMessage>
8787
readonly processFooterInfoLinkClick: MessageListener<FooterInfoLinkClick>
88-
readonly processMessageofStreamedData: MessageListener<MessageofStreamedData>
88+
readonly processTotalCodeBlocksReceived: MessageListener<MessageofStreamedData>
8989
}
9090

9191
export class ChatController {
@@ -147,8 +147,8 @@ export class ChatController {
147147
return this.processCopyCodeToClipboard(data)
148148
})
149149

150-
this.chatControllerMessageListeners.processMessageofStreamedData.onMessage(data => {
151-
return this.processMessageofStreamedData(data)
150+
this.chatControllerMessageListeners.processTotalCodeBlocksReceived.onMessage(data => {
151+
return this.processTotalCodeBlocksReceived(data)
152152
})
153153

154154
this.chatControllerMessageListeners.processContextMenuCommand.onMessage(data => {
@@ -317,7 +317,7 @@ export class ChatController {
317317
this.telemetryHelper.recordInteractWithMessage(message)
318318
}
319319

320-
private async processMessageofStreamedData(message: MessageofStreamedData) {
320+
private async processTotalCodeBlocksReceived(message: MessageofStreamedData) {
321321
// Check the telemtry and trigger from here
322322
this.telemetryHelper.recordInteractWithMessage(message)
323323
}

packages/core/src/codewhispererChat/controllers/chat/messenger/messenger.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,6 @@ export class Messenger {
232232
)
233233
)
234234
}
235-
this.dispatcher.sendMessageResponseEnded(
236-
new StopResponseMessage(
237-
{
238-
messageID: messageID,
239-
stopResponse: true,
240-
},
241-
tabID
242-
)
243-
)
244235

245236
this.dispatcher.sendChatMessage(
246237
new ChatMessage(
@@ -257,6 +248,14 @@ export class Messenger {
257248
)
258249
)
259250

251+
this.dispatcher.sendChatStreamEnded(
252+
new StopResponseMessage(
253+
{
254+
messageID: messageID,
255+
},
256+
tabID
257+
)
258+
)
260259
getLogger().info(
261260
`All events received. requestId=%s counts=%s`,
262261
response.$metadata.requestId,

packages/core/src/codewhispererChat/controllers/chat/telemetryHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@ export class CWCTelemetryHelper {
230230
cwsprChatInteractionTarget: message.link,
231231
}
232232
break
233-
case 'message-of-streamed-data':
233+
case 'chat-stream-ended':
234234
message = message as MessageofStreamedData
235235
event = {
236236
result: 'Succeeded',
237237
cwsprChatTotalCodeBlocks: message.totalCodeBlocks,
238238
cwsprChatMessageId: message.messageId,
239239
cwsprChatConversationId: conversationId ?? '',
240240
credentialStartUrl: AuthUtil.instance.startUrl,
241-
cwsprChatInteractionType: 'upvote',
241+
cwsprChatInteractionType: 'upvote', // Need to change to 'chat' in user-service.json and in vscodeTelemetry.json
242242
}
243243
break
244244
}

packages/core/src/codewhispererChat/view/connector/connector.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,14 @@ export class ChatMessage extends UiMessage {
167167

168168
export interface StopResponseMessageProps {
169169
readonly messageID: string
170-
readonly stopResponse: boolean
171170
}
172171

173172
export class StopResponseMessage extends UiMessage {
174173
readonly messageID: string | undefined
175-
readonly stopResponse: boolean
176-
174+
override type = 'chatStreamEnded'
177175
constructor(props: StopResponseMessageProps, tabID: string) {
178176
super(tabID)
179177
this.messageID = props.messageID
180-
this.stopResponse = props.stopResponse
181178
}
182179
}
183180

@@ -271,7 +268,7 @@ export class AppToWebViewMessageDispatcher {
271268
this.appsToWebViewMessagePublisher.publish(message)
272269
}
273270

274-
public sendMessageResponseEnded(message: StopResponseMessage) {
271+
public sendChatStreamEnded(message: StopResponseMessage) {
275272
this.appsToWebViewMessagePublisher.publish(message)
276273
}
277274
}

packages/core/src/codewhispererChat/view/messages/messageListener.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export class UIMessageListener {
9898
case 'footer-info-link-click':
9999
this.processFooterInfoLinkClick(msg)
100100
break
101-
case 'message-of-streamed-data':
102-
this.processMessageofStreamedData(msg)
101+
case 'chat-stream-ended':
102+
this.processTotalCodeBlocksReceived(msg)
103103
}
104104
}
105105

@@ -173,17 +173,6 @@ export class UIMessageListener {
173173
})
174174
}
175175

176-
// gets the total number of code blocks in a streamed data
177-
private processMessageofStreamedData(msg: any) {
178-
this.chatControllerMessagePublishers.processMessageofStreamedData.publish({
179-
command: msg.command,
180-
tabID: msg.tabID,
181-
messageId: msg.messageId,
182-
totalCodeBlocks: msg.totalCodeBlocks,
183-
tabType: msg.tabType,
184-
})
185-
}
186-
187176
private processTabWasRemoved(msg: any) {
188177
this.chatControllerMessagePublishers.processTabClosedMessage.publish({
189178
tabID: msg.tabID,
@@ -214,6 +203,16 @@ export class UIMessageListener {
214203
})
215204
}
216205

206+
private processTotalCodeBlocksReceived(msg: any) {
207+
this.chatControllerMessagePublishers.processTotalCodeBlocksReceived.publish({
208+
command: msg.command,
209+
tabID: msg.tabID,
210+
messageId: msg.messageId,
211+
totalCodeBlocks: msg.totalCodeBlocks,
212+
tabType: msg.tabType,
213+
})
214+
}
215+
217216
private stopResponse(msg: any) {
218217
this.chatControllerMessagePublishers.processStopResponseMessage.publish({
219218
tabID: msg.tabID,

0 commit comments

Comments
 (0)