Skip to content

Commit 294347a

Browse files
committed
refactor fetchSupplementalContext to reduce duplicate code snippet and fine-tune supcontext strategyId sent out
1 parent 016e478 commit 294347a

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

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

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -73,94 +73,94 @@ export async function fetchSupplementalContextForSrc(
7373
return undefined
7474
}
7575

76-
// opentabs context will use bm25 and users' open tabs to fetch supplemental context
77-
if (supplementalContextConfig === 'opentabs') {
78-
return {
79-
supplementalContextItems: (await fetchOpentabsContext(editor, cancellationToken)) ?? [],
80-
strategy: 'opentabs',
81-
}
82-
}
76+
// fallback to opentabs if projectContext timeout for 'default' | 'bm25'
77+
const opentabsContextPromise = waitUntil(
78+
async function () {
79+
return await fetchOpentabsContext(editor, cancellationToken)
80+
},
81+
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
82+
)
8383

84-
// codemap will use opentabs context plus repomap if it's present
85-
if (supplementalContextConfig === 'codemap') {
86-
const opentabsContextAndCodemap = await waitUntil(
87-
async function () {
88-
const result: CodeWhispererSupplementalContextItem[] = []
89-
const opentabsContext = await fetchOpentabsContext(editor, cancellationToken)
90-
const codemap = await fetchProjectContext(editor, 'codemap')
84+
let supContextPromise: Promise<CodeWhispererSupplementalContextItem[] | undefined> | undefined = new Promise(
85+
(resolve) => resolve(undefined)
86+
)
9187

92-
if (codemap && codemap.length > 0) {
93-
result.push(...codemap)
94-
}
88+
let target: 'default' | 'codemap' | 'bm25' | undefined = undefined
89+
switch (supplementalContextConfig) {
90+
case 'codemap':
91+
target = 'codemap'
92+
break
93+
case 'bm25':
94+
target = 'bm25'
95+
break
96+
case 'default':
97+
target = 'default'
98+
break
99+
default:
100+
break
101+
}
95102

96-
if (opentabsContext && opentabsContext.length > 0) {
97-
result.push(...opentabsContext)
103+
if (target) {
104+
supContextPromise = waitUntil(
105+
async function () {
106+
if (target) {
107+
return await fetchProjectContext(editor, target)
98108
}
99109

100-
return result
110+
return undefined
101111
},
102112
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
103113
)
114+
}
115+
116+
const [nullableOpentabsContext, nullableSupContext] = await Promise.all([opentabsContextPromise, supContextPromise])
117+
const opentabsContext = nullableOpentabsContext ?? []
118+
const supContext = nullableSupContext ?? []
104119

120+
// opentabs context will use bm25 and users' open tabs to fetch supplemental context
121+
if (supplementalContextConfig === 'opentabs') {
105122
return {
106-
supplementalContextItems: opentabsContextAndCodemap ?? [],
107-
strategy: 'codemap',
123+
supplementalContextItems: opentabsContext,
124+
strategy: opentabsContext.length === 0 ? 'Empty' : 'opentabs',
108125
}
109126
}
110127

111-
// fallback to opentabs if projectContext timeout for 'default' | 'bm25'
112-
const opentabsContextPromise = waitUntil(
113-
async function () {
114-
return await fetchOpentabsContext(editor, cancellationToken)
115-
},
116-
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
117-
)
128+
// repomap + opentabs
129+
if (supplementalContextConfig === 'codemap') {
130+
if (supContext.length > 0) {
131+
// TODO: truncation
132+
const res = [...supContext, ...opentabsContext]
133+
return {
134+
supplementalContextItems: res,
135+
strategy: 'codemap',
136+
}
137+
}
138+
}
118139

119140
// global bm25 without repomap
120141
if (supplementalContextConfig === 'bm25') {
121-
const projectBM25Promise = waitUntil(
122-
async function () {
123-
return await fetchProjectContext(editor, 'bm25')
124-
},
125-
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
126-
)
127-
128-
const [projectContext, opentabsContext] = await Promise.all([projectBM25Promise, opentabsContextPromise])
129-
if (projectContext && projectContext.length > 0) {
142+
if (supContext.length > 0) {
130143
return {
131-
supplementalContextItems: projectContext,
144+
supplementalContextItems: supContext,
132145
strategy: 'bm25',
133146
}
134147
}
135-
136-
return {
137-
supplementalContextItems: opentabsContext ?? [],
138-
strategy: 'opentabs',
139-
}
140148
}
141149

142-
// global bm25 with repomap
143-
const projectContextAndCodemapPromise = waitUntil(
144-
async function () {
145-
return await fetchProjectContext(editor, 'default')
146-
},
147-
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
148-
)
149-
150-
const [projectContext, opentabsContext] = await Promise.all([
151-
projectContextAndCodemapPromise,
152-
opentabsContextPromise,
153-
])
154-
if (projectContext && projectContext.length > 0) {
155-
return {
156-
supplementalContextItems: projectContext,
157-
strategy: 'default',
150+
// repomap + global bm25
151+
if (supplementalContextConfig === 'default') {
152+
if (supContext.length > 0) {
153+
return {
154+
supplementalContextItems: supContext,
155+
strategy: 'default',
156+
}
158157
}
159158
}
160159

160+
// fallback to use opentabs if other srategy return empty or timeout
161161
return {
162-
supplementalContextItems: opentabsContext ?? [],
163-
strategy: 'opentabs',
162+
supplementalContextItems: opentabsContext,
163+
strategy: opentabsContext.length === 0 ? 'Empty' : 'opentabs',
164164
}
165165
}
166166

0 commit comments

Comments
 (0)