-
Notifications
You must be signed in to change notification settings - Fork 201
/
chatContext.ts
72 lines (67 loc) · 2.43 KB
/
chatContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {
type ContextItem,
FeatureFlag,
type MentionQuery,
featureFlagProvider,
getURLContextItems,
parseMentionQuery,
} from '@sourcegraph/cody-shared'
import * as vscode from 'vscode'
import {
getFileContextFiles,
getOpenTabsContextFile,
getSymbolContextFiles,
} from '../../editor/utils/editor-context'
export async function getChatContextItemsForMention(
query: string,
cancellationToken: vscode.CancellationToken,
telemetryRecorder?: {
empty: () => void
withType: (type: MentionQuery['type']) => void
},
// The number of characters left in current context window.
maxChars?: number
): Promise<ContextItem[]> {
const mentionQuery = parseMentionQuery(query)
// Logging: log when the at-mention starts, and then log when we know the type (after the 1st
// character is typed). Don't log otherwise because we would be logging prefixes of the same
// query repeatedly, which is not needed.
if (mentionQuery.type === 'empty') {
telemetryRecorder?.empty()
} else if (query.length === 1) {
telemetryRecorder?.withType(mentionQuery.type)
}
const MAX_RESULTS = 20
switch (mentionQuery.type) {
case 'empty':
return getOpenTabsContextFile(maxChars)
case 'symbol':
// It would be nice if the VS Code symbols API supports cancellation, but it doesn't
return getSymbolContextFiles(mentionQuery.text, MAX_RESULTS)
case 'file':
return getFileContextFiles(mentionQuery.text, MAX_RESULTS, maxChars)
case 'url':
return (await isURLContextFeatureFlagEnabled())
? getURLContextItems(
mentionQuery.text,
convertCancellationTokenToAbortSignal(cancellationToken)
)
: []
default:
return []
}
}
export async function isURLContextFeatureFlagEnabled(): Promise<boolean> {
return (
vscode.workspace.getConfiguration('cody').get<boolean>('experimental.urlContext') === true ||
(await featureFlagProvider.evaluateFeatureFlag(FeatureFlag.URLContext))
)
}
function convertCancellationTokenToAbortSignal(token: vscode.CancellationToken): AbortSignal {
const controller = new AbortController()
const disposable = token.onCancellationRequested(() => {
controller.abort()
disposable.dispose()
})
return controller.signal
}