Skip to content

Commit 6b7a928

Browse files
committed
Remove duplicate code
1 parent db06902 commit 6b7a928

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,8 @@ export class InlineChatProvider {
7070
}
7171
}
7272

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-
8473
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-
}
74+
this.throwOnIamSession(message)
10075

10176
// TODO: handle partial responses.
10277
getLogger().info('Making inline chat request with message %O', message)
@@ -112,22 +87,7 @@ export class InlineChatProvider {
11287

11388
// TODO: remove in favor of LSP implementation.
11489
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-
}
90+
this.throwOnIamSession(message)
13191

13292
return this.editorContextExtractor
13393
.extractContextForTrigger('ChatMessage')
@@ -271,6 +231,34 @@ export class InlineChatProvider {
271231
})
272232
}
273233

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

0 commit comments

Comments
 (0)