Skip to content

Commit e2c13f7

Browse files
committed
Revert "chore(amazonq): nep cherrypick codewhispererService.ts (aws#1689)"
This reverts commit bf596bf.
1 parent 4b9761f commit e2c13f7

File tree

1 file changed

+13
-51
lines changed

1 file changed

+13
-51
lines changed

server/aws-lsp-codewhisperer/src/shared/codeWhispererService.ts

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ import {
2222
createCodeWhispererTokenClient,
2323
RequestExtras,
2424
} from '../client/token/codewhisperer'
25-
import CodeWhispererSigv4Client = require('../client/sigv4/codewhisperersigv4client')
26-
import CodeWhispererTokenClient = require('../client/token/codewhispererbearertokenclient')
27-
import { getErrorId } from './utils'
28-
import { GenerateCompletionsResponse } from '../client/token/codewhispererbearertokenclient'
2925

26+
// Define our own Suggestion interface to wrap the differences between Token and IAM Client
3027
export interface Suggestion extends CodeWhispererTokenClient.Completion, CodeWhispererSigv4Client.Recommendation {
3128
itemId: string
3229
}
@@ -45,17 +42,16 @@ export interface ResponseContext {
4542
nextToken?: string
4643
}
4744

48-
export enum SuggestionType {
49-
EDIT = 'EDIT',
50-
COMPLETION = 'COMPLETION',
51-
}
52-
5345
export interface GenerateSuggestionsResponse {
5446
suggestions: Suggestion[]
55-
suggestionType?: SuggestionType
5647
responseContext: ResponseContext
5748
}
5849

50+
import CodeWhispererSigv4Client = require('../client/sigv4/codewhisperersigv4client')
51+
import CodeWhispererTokenClient = require('../client/token/codewhispererbearertokenclient')
52+
import { getErrorId } from './utils'
53+
54+
// Right now the only difference between the token client and the IAM client for codewhsiperer is the difference in function name
5955
// This abstract class can grow in the future to account for any additional changes across the clients
6056
export abstract class CodeWhispererServiceBase {
6157
protected readonly codeWhispererRegion
@@ -138,6 +134,7 @@ export class CodeWhispererServiceIAM extends CodeWhispererServiceBase {
138134
// add cancellation check
139135
// add error check
140136
if (this.customizationArn) request = { ...request, customizationArn: this.customizationArn }
137+
141138
const response = await this.client.generateRecommendations(request).promise()
142139
const responseContext = {
143140
requestId: response?.$response?.requestId,
@@ -151,7 +148,6 @@ export class CodeWhispererServiceIAM extends CodeWhispererServiceBase {
151148

152149
return {
153150
suggestions: response.recommendations as Suggestion[],
154-
suggestionType: SuggestionType.COMPLETION,
155151
responseContext,
156152
}
157153
}
@@ -176,7 +172,6 @@ export class CodeWhispererServiceToken extends CodeWhispererServiceBase {
176172
sdkInitializator: SDKInitializator
177173
) {
178174
super(codeWhispererRegion, codeWhispererEndpoint)
179-
180175
const options: CodeWhispererTokenClientConfigurationOptions = {
181176
region: this.codeWhispererRegion,
182177
endpoint: this.codeWhispererEndpoint,
@@ -198,23 +193,7 @@ export class CodeWhispererServiceToken extends CodeWhispererServiceBase {
198193
throw err
199194
}
200195
})
201-
req.on('complete', response => {
202-
const requestStartTime = req.startTime?.getTime() || 0
203-
const requestEndTime = new Date().getTime()
204-
const latency = requestStartTime > 0 ? requestEndTime - requestStartTime : 0
205-
206-
const requestBody = req.httpRequest.body ? JSON.parse(String(req.httpRequest.body)) : {}
207-
this.completeRequest(req)
208-
})
209-
req.on('error', async (error, response) => {
210-
const requestStartTime = req.startTime?.getTime() || 0
211-
const requestEndTime = new Date().getTime()
212-
const latency = requestStartTime > 0 ? requestEndTime - requestStartTime : 0
213-
214-
const requestBody = req.httpRequest.body ? JSON.parse(String(req.httpRequest.body)) : {}
215-
this.completeRequest(req)
216-
})
217-
req.on('error', () => {
196+
req.on('complete', () => {
218197
this.completeRequest(req)
219198
})
220199
req.on('error', () => {
@@ -240,40 +219,23 @@ export class CodeWhispererServiceToken extends CodeWhispererServiceBase {
240219
// add cancellation check
241220
// add error check
242221
if (this.customizationArn) request.customizationArn = this.customizationArn
222+
243223
const response = await this.client.generateCompletions(this.withProfileArn(request)).promise()
244224
const responseContext = {
245225
requestId: response?.$response?.requestId,
246226
codewhispererSessionId: response?.$response?.httpResponse?.headers['x-amzn-sessionid'],
247227
nextToken: response.nextToken,
248228
}
249-
return this.mapCodeWhispererApiResponseToSuggestion(response, responseContext)
250-
}
251229

252-
private mapCodeWhispererApiResponseToSuggestion(
253-
apiResponse: GenerateCompletionsResponse,
254-
responseContext: ResponseContext
255-
): GenerateSuggestionsResponse {
256-
if (apiResponse?.predictions && apiResponse.predictions.length > 0) {
257-
const suggestionType = apiResponse.predictions[0].edit ? SuggestionType.EDIT : SuggestionType.COMPLETION
258-
const predictionType = suggestionType === SuggestionType.COMPLETION ? 'completion' : 'edit'
259-
260-
return {
261-
suggestions: apiResponse.predictions.map(prediction => ({
262-
content: prediction[predictionType]?.content ?? '',
263-
references: prediction[predictionType]?.references ?? [],
264-
itemId: this.generateItemId(),
265-
})),
266-
suggestionType,
267-
responseContext,
268-
}
230+
for (const recommendation of response?.completions ?? []) {
231+
Object.assign(recommendation, { itemId: this.generateItemId() })
269232
}
233+
270234
return {
271-
suggestions: apiResponse.completions as Suggestion[],
272-
suggestionType: SuggestionType.COMPLETION,
235+
suggestions: response.completions as Suggestion[],
273236
responseContext,
274237
}
275238
}
276-
277239
public async codeModernizerCreateUploadUrl(
278240
request: CodeWhispererTokenClient.CreateUploadUrlRequest
279241
): Promise<CodeWhispererTokenClient.CreateUploadUrlResponse> {

0 commit comments

Comments
 (0)