Skip to content

Commit cd42ede

Browse files
committed
Remove duplicate code
1 parent db06902 commit cd42ede

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

packages/amazonq/src/inlineChat/provider/inlineChatProvider.ts

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
ChatTriggerType,
1717
EditorContextExtractor,
1818
PromptMessage,
19-
TriggerEvent,
2019
TriggerEventsStorage,
2120
TriggerPayload,
2221
triggerPayloadToChatRequest,
@@ -70,33 +69,8 @@ export class InlineChatProvider {
7069
}
7170
}
7271

73-
private getTriggerInteractionFromTriggerEvent(triggerEvent: TriggerEvent | undefined): CwsprChatTriggerInteraction {
74-
switch (triggerEvent?.type) {
75-
case 'editor_context_command':
76-
return triggerEvent.command?.triggerType === 'keybinding' ? 'hotkeys' : 'contextMenu'
77-
case 'follow_up':
78-
case 'chat_message':
79-
default:
80-
return 'click'
81-
}
82-
}
83-
8472
public async processPromptMessageLSP(message: PromptMessage): Promise<InlineChatResult> {
85-
const triggerInteraction = this.getTriggerInteractionFromTriggerEvent(
86-
this.triggerEventsStorage.getLastTriggerEventByTabID(message.tabID)
87-
)
88-
if (!AuthUtil.instance.isSsoSession()) {
89-
telemetry.amazonq_messageResponseError.emit({
90-
result: 'Failed',
91-
cwsprChatConversationType: 'Chat',
92-
cwsprChatRequestLength: message.message?.length ?? 0,
93-
cwsprChatResponseCode: 401,
94-
cwsprChatTriggerInteraction: triggerInteraction,
95-
reason: 'AuthenticationError',
96-
reasonDesc: 'Inline chat requires SSO authentication, but current session is not',
97-
})
98-
throw new ToolkitError('Inline chat is only available with SSO authentication')
99-
}
73+
this.throwOnIamSession(message)
10074

10175
// TODO: handle partial responses.
10276
getLogger().info('Making inline chat request with message %O', message)
@@ -112,22 +86,7 @@ export class InlineChatProvider {
11286

11387
// TODO: remove in favor of LSP implementation.
11488
public async processPromptMessage(message: PromptMessage) {
115-
const triggerInteraction = this.getTriggerInteractionFromTriggerEvent(
116-
this.triggerEventsStorage.getLastTriggerEventByTabID(message.tabID)
117-
)
118-
if (!AuthUtil.instance.isSsoSession()) {
119-
telemetry.amazonq_messageResponseError.emit({
120-
result: 'Failed',
121-
cwsprChatConversationType: 'Chat',
122-
cwsprChatRequestLength: message.message?.length ?? 0,
123-
cwsprChatResponseCode: 401,
124-
cwsprChatTriggerInteraction: triggerInteraction,
125-
reason: 'AuthenticationError',
126-
reasonDesc: 'Inline chat requires SSO authentication, but current session is not',
127-
credentialStartUrl: AuthUtil.instance.connection?.startUrl,
128-
})
129-
throw new ToolkitError('Inline chat is only available with SSO authentication')
130-
}
89+
this.throwOnIamSession(message)
13190

13291
return this.editorContextExtractor
13392
.extractContextForTrigger('ChatMessage')
@@ -271,6 +230,34 @@ export class InlineChatProvider {
271230
})
272231
}
273232

233+
private throwOnIamSession(message: PromptMessage) {
234+
const triggerEvent = this.triggerEventsStorage.getLastTriggerEventByTabID(message.tabID)
235+
let triggerInteraction: CwsprChatTriggerInteraction
236+
switch (triggerEvent?.type) {
237+
case 'editor_context_command':
238+
triggerInteraction = triggerEvent.command?.triggerType === 'keybinding' ? 'hotkeys' : 'contextMenu'
239+
break
240+
case 'follow_up':
241+
case 'chat_message':
242+
default:
243+
triggerInteraction = 'click'
244+
break
245+
}
246+
247+
if (!AuthUtil.instance.isSsoSession()) {
248+
telemetry.amazonq_messageResponseError.emit({
249+
result: 'Failed',
250+
cwsprChatConversationType: 'Chat',
251+
cwsprChatRequestLength: message.message?.length ?? 0,
252+
cwsprChatResponseCode: 401,
253+
cwsprChatTriggerInteraction: triggerInteraction,
254+
reason: 'AuthenticationError',
255+
reasonDesc: 'Inline chat requires SSO authentication, but current session is not',
256+
})
257+
throw new ToolkitError('Inline chat is only available with SSO authentication')
258+
}
259+
}
260+
274261
public sendTelemetryEvent(inlineChatEvent: InlineChatEvent, currentTask?: InlineTask) {
275262
codeWhispererClient
276263
.sendTelemetryEvent({

0 commit comments

Comments
 (0)