in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/addtionalContextProvider.ts [70:171]
async getAdditionalContext(
triggerContext: TriggerContext,
context?: ContextCommand[]
): Promise<AdditionalContentEntryAddition[]> {
if (!triggerContext.contextInfo) {
triggerContext.contextInfo = initialContextInfo
}
const additionalContextCommands: ContextCommandItem[] = []
const workspaceRules = await this.collectWorkspaceRules()
let workspaceFolderPath = triggerContext.workspaceFolder?.uri
? URI.parse(triggerContext.workspaceFolder.uri).fsPath
: workspaceUtils.getWorkspaceFolderPaths(this.lsp)[0]
if (workspaceRules.length > 0) {
additionalContextCommands.push(...workspaceRules)
}
triggerContext.contextInfo.contextCount.ruleContextCount = workspaceRules.length
if (context) {
let fileContextCount = 0
let folderContextCount = 0
let promptContextCount = 0
let codeContextCount = 0
additionalContextCommands.push(...this.mapToContextCommandItems(context, workspaceFolderPath))
for (const c of context) {
if (typeof context !== 'string') {
if (c.id === 'prompt') {
promptContextCount++
} else if (c.label === 'file') {
fileContextCount++
} else if (c.label === 'folder') {
folderContextCount++
} else if (c.label === 'code') {
codeContextCount++
}
}
}
triggerContext.contextInfo!.contextCount = {
...triggerContext.contextInfo!.contextCount,
fileContextCount,
folderContextCount,
promptContextCount,
codeContextCount,
}
}
if (additionalContextCommands.length === 0) {
return []
}
let prompts: AdditionalContextPrompt[] = []
try {
const localProjectContextController = await LocalProjectContextController.getInstance()
prompts = await localProjectContextController.getContextCommandPrompt(additionalContextCommands)
} catch (error) {
// do nothing
}
const contextEntry: AdditionalContentEntryAddition[] = []
let ruleContextLength = 0
let fileContextLength = 0
let promptContextLength = 0
let codeContextLength = 0
for (const prompt of prompts.slice(0, additionalContextMaxLength)) {
const contextType = this.getContextType(prompt)
const description =
contextType === 'rule' || contextType === 'prompt'
? `You must follow the instructions in ${prompt.relativePath}. Below are lines ${prompt.startLine}-${prompt.endLine} of this file:\n`
: prompt.description
const relativePath = prompt.filePath.startsWith(getUserPromptsDirectory())
? path.basename(prompt.filePath)
: path.relative(workspaceFolderPath, prompt.filePath)
const entry = {
name: prompt.name.substring(0, additionalContentNameLimit),
description: description.substring(0, additionalContentNameLimit),
innerContext: prompt.content.substring(0, workspaceChunkMaxSize),
type: contextType,
path: prompt.filePath,
relativePath: relativePath,
startLine: prompt.startLine,
endLine: prompt.endLine,
}
contextEntry.push(entry)
if (contextType === 'rule') {
ruleContextLength += prompt.content.length
} else if (contextType === 'prompt') {
promptContextLength += prompt.content.length
} else if (contextType === 'code') {
codeContextLength += prompt.content.length
} else {
fileContextLength += prompt.content.length
}
}
triggerContext.contextInfo.contextLength = {
ruleContextLength,
fileContextLength,
promptContextLength,
codeContextLength,
}
return contextEntry
}