Skip to content

Commit d10c2de

Browse files
committed
replace cancellation token with waitUntil in codewhisperer utg code path
1 parent 031b03f commit d10c2de

File tree

2 files changed

+47
-71
lines changed

2 files changed

+47
-71
lines changed

packages/core/src/codewhisperer/util/supplementalContext/supplementalContextUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function fetchSupplementalContext(
2828
>
2929

3030
if (isUtg) {
31-
supplementalContextPromise = fetchSupplementalContextForTest(editor, cancellationToken)
31+
supplementalContextPromise = fetchSupplementalContextForTest(editor)
3232
} else {
3333
supplementalContextPromise = fetchSupplementalContextForSrc(editor)
3434
}

packages/core/src/codewhisperer/util/supplementalContext/utgUtils.ts

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as path from 'path'
7-
import { fs } from '../../../shared'
7+
import { fs, waitUntil } from '../../../shared'
88
import * as vscode from 'vscode'
99
import {
1010
countSubstringMatches,
@@ -14,10 +14,7 @@ import {
1414
utgLanguageConfig,
1515
utgLanguageConfigs,
1616
} from './codeParsingUtil'
17-
import { ToolkitError } from '../../../shared/errors'
18-
import { supplemetalContextFetchingTimeoutMsg } from '../../models/constants'
19-
import { CancellationError } from '../../../shared/utilities/timeoutUtils'
20-
import { utgConfig } from '../../models/constants'
17+
import { supplementalContextTimeoutInMs, utgConfig } from '../../models/constants'
2118
import { getOpenFilesInWindow } from '../../../shared/utilities/editorUtilities'
2219
import { getLogger } from '../../../shared/logger/logger'
2320
import { CodeWhispererSupplementalContext, CodeWhispererSupplementalContextItem, UtgStrategy } from '../../models/model'
@@ -48,8 +45,7 @@ export function shouldFetchUtgContext(languageId: vscode.TextDocument['languageI
4845
* @returns
4946
*/
5047
export async function fetchSupplementalContextForTest(
51-
editor: vscode.TextEditor,
52-
cancellationToken: vscode.CancellationToken
48+
editor: vscode.TextEditor
5349
): Promise<Pick<CodeWhispererSupplementalContext, 'supplementalContextItems' | 'strategy'> | undefined> {
5450
const shouldProceed = shouldFetchUtgContext(editor.document.languageId)
5551

@@ -59,50 +55,53 @@ export async function fetchSupplementalContextForTest(
5955

6056
const languageConfig = utgLanguageConfigs[editor.document.languageId]
6157

62-
// TODO (Metrics): 1. Total number of calls to fetchSupplementalContextForTest
63-
throwIfCancelled(cancellationToken)
64-
65-
let crossSourceFile = await findSourceFileByName(editor, languageConfig, cancellationToken)
66-
if (crossSourceFile) {
67-
// TODO (Metrics): 2. Success count for fetchSourceFileByName (find source file by name)
68-
getLogger().debug(`CodeWhisperer finished fetching utg context by file name`)
69-
return {
70-
supplementalContextItems: await generateSupplementalContextFromFocalFile(
71-
crossSourceFile,
72-
'byName',
73-
cancellationToken
74-
),
75-
strategy: 'byName',
76-
}
77-
}
78-
throwIfCancelled(cancellationToken)
79-
80-
crossSourceFile = await findSourceFileByContent(editor, languageConfig, cancellationToken)
81-
if (crossSourceFile) {
82-
// TODO (Metrics): 3. Success count for fetchSourceFileByContent (find source file by content)
83-
getLogger().debug(`CodeWhisperer finished fetching utg context by file content`)
84-
return {
85-
supplementalContextItems: await generateSupplementalContextFromFocalFile(
86-
crossSourceFile,
87-
'byContent',
88-
cancellationToken
89-
),
90-
strategy: 'byContent',
91-
}
92-
}
58+
const utgContext: Pick<CodeWhispererSupplementalContext, 'supplementalContextItems' | 'strategy'> | undefined =
59+
await waitUntil(
60+
async function () {
61+
let crossSourceFile = await findSourceFileByName(editor)
62+
if (crossSourceFile) {
63+
getLogger().debug(`CodeWhisperer finished fetching utg context by file name`)
64+
return {
65+
supplementalContextItems: await generateSupplementalContextFromFocalFile(
66+
crossSourceFile,
67+
'byName'
68+
),
69+
strategy: 'byName',
70+
}
71+
}
72+
73+
crossSourceFile = await findSourceFileByContent(editor, languageConfig)
74+
if (crossSourceFile) {
75+
getLogger().debug(`CodeWhisperer finished fetching utg context by file content`)
76+
return {
77+
supplementalContextItems: await generateSupplementalContextFromFocalFile(
78+
crossSourceFile,
79+
'byContent'
80+
),
81+
strategy: 'byContent',
82+
}
83+
}
84+
85+
return undefined
86+
},
87+
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
88+
)
9389

94-
// TODO (Metrics): 4. Failure count - when unable to find focal file (supplemental context empty)
95-
getLogger().debug(`CodeWhisperer failed to fetch utg context`)
96-
return {
97-
supplementalContextItems: [],
98-
strategy: 'empty',
90+
if (!utgContext) {
91+
getLogger().debug(`CodeWhisperer failed to fetch utg context`)
9992
}
93+
94+
return (
95+
utgContext ?? {
96+
supplementalContextItems: [],
97+
strategy: 'empty',
98+
}
99+
)
100100
}
101101

102102
async function generateSupplementalContextFromFocalFile(
103103
filePath: string,
104-
strategy: UtgStrategy,
105-
cancellationToken: vscode.CancellationToken
104+
strategy: UtgStrategy
106105
): Promise<CodeWhispererSupplementalContextItem[]> {
107106
const fileContent = await fs.readFileText(vscode.Uri.parse(filePath!).fsPath)
108107

@@ -121,34 +120,23 @@ async function generateSupplementalContextFromFocalFile(
121120

122121
async function findSourceFileByContent(
123122
editor: vscode.TextEditor,
124-
languageConfig: utgLanguageConfig,
125-
cancellationToken: vscode.CancellationToken
123+
languageConfig: utgLanguageConfig
126124
): Promise<string | undefined> {
127125
const testFileContent = await fs.readFileText(editor.document.fileName)
128126
const testElementList = extractFunctions(testFileContent, languageConfig.functionExtractionPattern)
129127

130-
throwIfCancelled(cancellationToken)
131-
132128
testElementList.push(...extractClasses(testFileContent, languageConfig.classExtractionPattern))
133129

134-
throwIfCancelled(cancellationToken)
135-
136130
let sourceFilePath: string | undefined = undefined
137131
let maxMatchCount = 0
138132

139133
if (testElementList.length === 0) {
140-
// TODO: Add metrics here, as unable to parse test file using Regex.
141134
return sourceFilePath
142135
}
143136

144137
const relevantFilePaths = await getRelevantUtgFiles(editor)
145138

146-
throwIfCancelled(cancellationToken)
147-
148-
// TODO (Metrics):Add metrics for relevantFilePaths length
149139
for (const filePath of relevantFilePaths) {
150-
throwIfCancelled(cancellationToken)
151-
152140
const fileContent = await fs.readFileText(filePath)
153141
const elementList = extractFunctions(fileContent, languageConfig.functionExtractionPattern)
154142
elementList.push(...extractClasses(fileContent, languageConfig.classExtractionPattern))
@@ -201,11 +189,7 @@ export function guessSrcFileName(
201189
return undefined
202190
}
203191

204-
async function findSourceFileByName(
205-
editor: vscode.TextEditor,
206-
languageConfig: utgLanguageConfig,
207-
cancellationToken: vscode.CancellationToken
208-
): Promise<string | undefined> {
192+
async function findSourceFileByName(editor: vscode.TextEditor): Promise<string | undefined> {
209193
const testFileName = path.basename(editor.document.fileName)
210194
const assumedSrcFileName = guessSrcFileName(testFileName, editor.document.languageId)
211195
if (!assumedSrcFileName) {
@@ -214,16 +198,8 @@ async function findSourceFileByName(
214198

215199
const sourceFiles = await vscode.workspace.findFiles(`**/${assumedSrcFileName}`)
216200

217-
throwIfCancelled(cancellationToken)
218-
219201
if (sourceFiles.length > 0) {
220202
return sourceFiles[0].toString()
221203
}
222204
return undefined
223205
}
224-
225-
function throwIfCancelled(token: vscode.CancellationToken): void | never {
226-
if (token.isCancellationRequested) {
227-
throw new ToolkitError(supplemetalContextFetchingTimeoutMsg, { cause: new CancellationError('timeout') })
228-
}
229-
}

0 commit comments

Comments
 (0)