Skip to content

Commit 0446ccb

Browse files
committed
add more try-catch for error handling
1 parent 7d25d3f commit 0446ccb

File tree

3 files changed

+82
-68
lines changed

3 files changed

+82
-68
lines changed

packages/core/src/codewhisperer/nextEditPrediction/PredictionTracker.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ export class PredictionTracker {
5454
return
5555
}
5656

57-
// Get existing snapshots for this file
58-
const fileSnapshots = this.snapshots.get(filePath) || []
59-
const timestamp = globals.clock.Date.now()
57+
try {
58+
// Get existing snapshots for this file
59+
const fileSnapshots = this.snapshots.get(filePath) || []
60+
const timestamp = globals.clock.Date.now()
6061

61-
// Anti-throttling, only add snap shot after the debounce is cleared
62-
const shouldAddSnapshot =
63-
fileSnapshots.length === 0 ||
64-
timestamp - fileSnapshots[fileSnapshots.length - 1].timestamp > this.config.debounceIntervalMs
62+
// Anti-throttling, only add snap shot after the debounce is cleared
63+
const shouldAddSnapshot =
64+
fileSnapshots.length === 0 ||
65+
timestamp - fileSnapshots[fileSnapshots.length - 1].timestamp > this.config.debounceIntervalMs
6566

66-
if (!shouldAddSnapshot) {
67-
return
68-
}
67+
if (!shouldAddSnapshot) {
68+
return
69+
}
6970

70-
try {
7171
const content = previousContent
7272
const size = Buffer.byteLength(content, 'utf8')
7373
const snapshot: FileSnapshot = {
@@ -201,32 +201,38 @@ export class PredictionTracker {
201201
* @returns Array of SupplementalContext objects containing diffs between snapshots and current content
202202
*/
203203
public async generatePredictionSupplementalContext(): Promise<codewhispererClient.SupplementalContext[]> {
204-
const activeEditor = vscode.window.activeTextEditor
205-
if (activeEditor === undefined) {
206-
return []
207-
}
208-
const filePath = activeEditor.document.uri.fsPath
209-
const currentContent = activeEditor.document.getText()
210-
const snapshots = this.getFileSnapshots(filePath)
204+
try {
205+
const activeEditor = vscode.window.activeTextEditor
206+
if (activeEditor === undefined) {
207+
return []
208+
}
209+
const filePath = activeEditor.document.uri.fsPath
210+
const currentContent = activeEditor.document.getText()
211+
const snapshots = this.getFileSnapshots(filePath)
211212

212-
if (snapshots.length === 0) {
213+
if (snapshots.length === 0) {
214+
return []
215+
}
216+
217+
// Create SnapshotContent array from snapshots
218+
const snapshotContents: diffGenerator.SnapshotContent[] = snapshots.map((snapshot) => ({
219+
filePath: snapshot.filePath,
220+
content: snapshot.content,
221+
timestamp: snapshot.timestamp,
222+
}))
223+
224+
// Use the diffGenerator module to generate supplemental contexts
225+
return diffGenerator.generateDiffContexts(
226+
filePath,
227+
currentContent,
228+
snapshotContents,
229+
this.config.maxSupplementalContext
230+
)
231+
} catch (err) {
232+
// this ensures we are not breaking inline requests
233+
this.logger.error(`Failed to generate prediction supplemental context: ${err}`)
213234
return []
214235
}
215-
216-
// Create SnapshotContent array from snapshots
217-
const snapshotContents: diffGenerator.SnapshotContent[] = snapshots.map((snapshot) => ({
218-
filePath: snapshot.filePath,
219-
content: snapshot.content,
220-
timestamp: snapshot.timestamp,
221-
}))
222-
223-
// Use the diffGenerator module to generate supplemental contexts
224-
return diffGenerator.generateDiffContexts(
225-
filePath,
226-
currentContent,
227-
snapshotContents,
228-
this.config.maxSupplementalContext
229-
)
230236
}
231237

232238
public getTotalSize() {

packages/core/src/codewhisperer/nextEditPrediction/activation.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ export let predictionTracker: PredictionTracker | undefined
1313
let keyStrokeHandler: PredictionKeyStrokeHandler | undefined
1414

1515
export function activateEditTracking(context: ExtContext): void {
16-
predictionTracker = new PredictionTracker(context.extensionContext)
16+
try {
17+
predictionTracker = new PredictionTracker(context.extensionContext)
1718

18-
keyStrokeHandler = new PredictionKeyStrokeHandler(predictionTracker)
19-
context.extensionContext.subscriptions.push(
20-
vscode.Disposable.from({
21-
dispose: () => {
22-
keyStrokeHandler?.dispose()
23-
},
24-
})
25-
)
19+
keyStrokeHandler = new PredictionKeyStrokeHandler(predictionTracker)
20+
context.extensionContext.subscriptions.push(
21+
vscode.Disposable.from({
22+
dispose: () => {
23+
keyStrokeHandler?.dispose()
24+
},
25+
})
26+
)
2627

27-
getLogger('nextEditPrediction').debug('Next Edit Prediction activated')
28+
getLogger('nextEditPrediction').debug('Next Edit Prediction activated')
29+
} catch (error) {
30+
getLogger('nextEditPrediction').error(`Error in activateEditTracking: ${error}`)
31+
}
2832
}

packages/core/src/codewhisperer/util/editorContext.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,42 +122,23 @@ export async function buildListRecommendationRequest(
122122

123123
// Get predictionSupplementalContext from PredictionTracker
124124
let predictionSupplementalContext: codewhispererClient.SupplementalContext[] = []
125-
try {
126-
if (predictionTracker) {
127-
predictionSupplementalContext = await predictionTracker.generatePredictionSupplementalContext()
128-
}
129-
} catch (error) {
130-
getLogger().error(`Error getting prediction supplemental context: ${error}`)
125+
if (predictionTracker) {
126+
predictionSupplementalContext = await predictionTracker.generatePredictionSupplementalContext()
131127
}
132128

133129
const selectedCustomization = getSelectedCustomization()
134-
const inlineSupplementalContext: codewhispererClient.SupplementalContext[] = supplementalContexts
130+
const completionSupplementalContext: codewhispererClient.SupplementalContext[] = supplementalContexts
135131
? supplementalContexts.supplementalContextItems.map((v) => {
136132
return selectFrom(v, 'content', 'filePath')
137133
})
138134
: []
139135

140136
const profile = AuthUtil.instance.regionProfileManager.activeRegionProfile
141137

142-
// Dynamically create editorState from current editor context
143-
const editorState = {
144-
document: {
145-
programmingLanguage: {
146-
languageName: fileContext.programmingLanguage.languageName,
147-
},
148-
relativeFilePath: fileContext.filename,
149-
text: editor.document.getText(),
150-
},
151-
cursorState: {
152-
position: {
153-
line: editor.selection.active.line,
154-
character: editor.selection.active.character,
155-
},
156-
},
157-
}
138+
const editorState = getEditorState(editor, fileContext)
158139

159140
// Combine inline and prediction supplemental contexts
160-
const finalSupplementalContext = inlineSupplementalContext.concat(predictionSupplementalContext)
141+
const finalSupplementalContext = completionSupplementalContext.concat(predictionSupplementalContext)
161142
return {
162143
request: {
163144
fileContext: fileContext,
@@ -233,6 +214,29 @@ export function getTabSize(): number {
233214
return tabSize
234215
}
235216

217+
export function getEditorState(editor: vscode.TextEditor, fileContext: codewhispererClient.FileContext): any {
218+
try {
219+
return {
220+
document: {
221+
programmingLanguage: {
222+
languageName: fileContext.programmingLanguage.languageName,
223+
},
224+
relativeFilePath: fileContext.filename,
225+
text: editor.document.getText(),
226+
},
227+
cursorState: {
228+
position: {
229+
line: editor.selection.active.line,
230+
character: editor.selection.active.character,
231+
},
232+
},
233+
}
234+
} catch (error) {
235+
getLogger().error(`Error generating editor state: ${error}`)
236+
return undefined
237+
}
238+
}
239+
236240
export function getLeftContext(editor: vscode.TextEditor, line: number): string {
237241
let lineText = ''
238242
try {

0 commit comments

Comments
 (0)