@@ -6,13 +6,19 @@ import * as vscode from 'vscode'
66import { ConfigurationEntry , GetRecommendationsResponse , vsCodeState } from '../models/model'
77import { isCloud9 } from '../../shared/extensionUtilities'
88import { isInlineCompletionEnabled } from '../util/commonUtil'
9- import { CodewhispererAutomatedTriggerType , CodewhispererTriggerType } from '../../shared/telemetry/telemetry'
9+ import {
10+ CodewhispererAutomatedTriggerType ,
11+ CodewhispererTriggerType ,
12+ telemetry ,
13+ } from '../../shared/telemetry/telemetry'
1014import { AuthUtil } from '../util/authUtil'
1115import { isIamConnection } from '../../auth/connection'
1216import { RecommendationHandler } from '../service/recommendationHandler'
1317import { InlineCompletionService } from '../service/inlineCompletionService'
1418import { ClassifierTrigger } from './classifierTrigger'
1519import { DefaultCodeWhispererClient } from '../client/codewhisperer'
20+ import { randomUUID } from '../../shared/crypto'
21+ import { TelemetryHelper } from '../util/telemetryHelper'
1622
1723export interface SuggestionActionEvent {
1824 readonly editor : vscode . TextEditor | undefined
@@ -68,99 +74,107 @@ export class RecommendationService {
6874 return
6975 }
7076
71- if ( isCloud9 ( 'any' ) ) {
72- // C9 manual trigger key alt/option + C is ALWAYS enabled because the VSC version C9 is on doesn't support setContextKey which is used for CODEWHISPERER_ENABLED
73- // therefore we need a connection check if there is ANY connection(regardless of the connection's state) connected to CodeWhisperer on C9
74- if ( triggerType === 'OnDemand' && ! AuthUtil . instance . isConnected ( ) ) {
75- return
76- }
77+ /**
78+ * Use an existing trace ID if invoked through a command (e.g., manual invocation),
79+ * otherwise generate a new trace ID
80+ */
81+ const traceId = telemetry . attributes ?. traceId ?? randomUUID ( )
82+ TelemetryHelper . instance . setTraceId ( traceId )
83+ await telemetry . withTraceId ( async ( ) => {
84+ if ( isCloud9 ( 'any' ) ) {
85+ // C9 manual trigger key alt/option + C is ALWAYS enabled because the VSC version C9 is on doesn't support setContextKey which is used for CODEWHISPERER_ENABLED
86+ // therefore we need a connection check if there is ANY connection(regardless of the connection's state) connected to CodeWhisperer on C9
87+ if ( triggerType === 'OnDemand' && ! AuthUtil . instance . isConnected ( ) ) {
88+ return
89+ }
7790
78- RecommendationHandler . instance . checkAndResetCancellationTokens ( )
79- vsCodeState . isIntelliSenseActive = false
80- this . _isRunning = true
81- let response : GetRecommendationsResponse = {
82- result : 'Failed' ,
83- errorMessage : undefined ,
84- recommendationCount : 0 ,
85- }
91+ RecommendationHandler . instance . checkAndResetCancellationTokens ( )
92+ vsCodeState . isIntelliSenseActive = false
93+ this . _isRunning = true
94+ let response : GetRecommendationsResponse = {
95+ result : 'Failed' ,
96+ errorMessage : undefined ,
97+ recommendationCount : 0 ,
98+ }
8699
87- try {
88- this . _onSuggestionActionEvent . fire ( {
89- editor : editor ,
90- isRunning : true ,
91- triggerType : triggerType ,
92- response : undefined ,
93- } )
100+ try {
101+ this . _onSuggestionActionEvent . fire ( {
102+ editor : editor ,
103+ isRunning : true ,
104+ triggerType : triggerType ,
105+ response : undefined ,
106+ } )
94107
95- if ( isCloud9 ( 'classic' ) || isIamConnection ( AuthUtil . instance . conn ) ) {
96- response = await RecommendationHandler . instance . getRecommendations (
97- client ,
98- editor ,
99- triggerType ,
100- config ,
101- autoTriggerType ,
102- false
103- )
104- } else {
105- if ( AuthUtil . instance . isConnectionExpired ( ) ) {
106- await AuthUtil . instance . showReauthenticatePrompt ( )
108+ if ( isCloud9 ( 'classic' ) || isIamConnection ( AuthUtil . instance . conn ) ) {
109+ response = await RecommendationHandler . instance . getRecommendations (
110+ client ,
111+ editor ,
112+ triggerType ,
113+ config ,
114+ autoTriggerType ,
115+ false
116+ )
117+ } else {
118+ if ( AuthUtil . instance . isConnectionExpired ( ) ) {
119+ await AuthUtil . instance . showReauthenticatePrompt ( )
120+ }
121+ response = await RecommendationHandler . instance . getRecommendations (
122+ client ,
123+ editor ,
124+ triggerType ,
125+ config ,
126+ autoTriggerType ,
127+ true
128+ )
129+ }
130+ if ( RecommendationHandler . instance . canShowRecommendationInIntelliSense ( editor , true , response ) ) {
131+ await vscode . commands . executeCommand ( 'editor.action.triggerSuggest' ) . then ( ( ) => {
132+ vsCodeState . isIntelliSenseActive = true
133+ } )
107134 }
108- response = await RecommendationHandler . instance . getRecommendations (
135+ } finally {
136+ this . _isRunning = false
137+ this . _onSuggestionActionEvent . fire ( {
138+ editor : editor ,
139+ isRunning : false ,
140+ triggerType : triggerType ,
141+ response : response ,
142+ } )
143+ }
144+ } else if ( isInlineCompletionEnabled ( ) ) {
145+ if ( triggerType === 'OnDemand' ) {
146+ ClassifierTrigger . instance . recordClassifierResultForManualTrigger ( editor )
147+ }
148+
149+ this . _isRunning = true
150+ let response : GetRecommendationsResponse | undefined = undefined
151+
152+ try {
153+ this . _onSuggestionActionEvent . fire ( {
154+ editor : editor ,
155+ isRunning : true ,
156+ triggerType : triggerType ,
157+ response : undefined ,
158+ } )
159+
160+ response = await InlineCompletionService . instance . getPaginatedRecommendation (
109161 client ,
110162 editor ,
111163 triggerType ,
112164 config ,
113165 autoTriggerType ,
114- true
166+ event
115167 )
116- }
117- if ( RecommendationHandler . instance . canShowRecommendationInIntelliSense ( editor , true , response ) ) {
118- await vscode . commands . executeCommand ( 'editor.action.triggerSuggest' ) . then ( ( ) => {
119- vsCodeState . isIntelliSenseActive = true
168+ } finally {
169+ this . _isRunning = false
170+ this . _onSuggestionActionEvent . fire ( {
171+ editor : editor ,
172+ isRunning : false ,
173+ triggerType : triggerType ,
174+ response : response ,
120175 } )
121176 }
122- } finally {
123- this . _isRunning = false
124- this . _onSuggestionActionEvent . fire ( {
125- editor : editor ,
126- isRunning : false ,
127- triggerType : triggerType ,
128- response : response ,
129- } )
130- }
131- } else if ( isInlineCompletionEnabled ( ) ) {
132- if ( triggerType === 'OnDemand' ) {
133- ClassifierTrigger . instance . recordClassifierResultForManualTrigger ( editor )
134177 }
135-
136- this . _isRunning = true
137- let response : GetRecommendationsResponse | undefined = undefined
138-
139- try {
140- this . _onSuggestionActionEvent . fire ( {
141- editor : editor ,
142- isRunning : true ,
143- triggerType : triggerType ,
144- response : undefined ,
145- } )
146-
147- response = await InlineCompletionService . instance . getPaginatedRecommendation (
148- client ,
149- editor ,
150- triggerType ,
151- config ,
152- autoTriggerType ,
153- event
154- )
155- } finally {
156- this . _isRunning = false
157- this . _onSuggestionActionEvent . fire ( {
158- editor : editor ,
159- isRunning : false ,
160- triggerType : triggerType ,
161- response : response ,
162- } )
163- }
164- }
178+ } , traceId )
165179 }
166180}
0 commit comments