private async resolveContextCommandPayload()

in packages/core/src/codewhispererChat/controllers/chat/controller.ts [1008:1086]


    private async resolveContextCommandPayload(triggerPayload: TriggerPayload, session: ChatSession) {
        const contextCommands: ContextCommandItem[] = []

        // Check for workspace rules to add to context
        const workspaceRules = await this.collectWorkspaceRules()
        if (workspaceRules.length > 0) {
            contextCommands.push(
                ...workspaceRules.map((rule) => {
                    const workspaceFolderPath =
                        vscode.workspace.getWorkspaceFolder(vscode.Uri.parse(rule))?.uri?.path || ''
                    return {
                        workspaceFolder: workspaceFolderPath,
                        type: 'file' as ContextCommandItemType,
                        relativePath: path.relative(workspaceFolderPath, rule),
                    }
                })
            )
        }
        triggerPayload.workspaceRulesCount = workspaceRules.length

        for (const context of triggerPayload.context) {
            if (typeof context !== 'string' && context.route && context.route.length === 2) {
                contextCommands.push({
                    workspaceFolder: context.route[0] || '',
                    type: (context.label || '') as ContextCommandItemType,
                    relativePath: context.route[1] || '',
                    id: context.id,
                })
            }
        }

        if (contextCommands.length === 0) {
            return []
        }
        const workspaceFolders = (vscode.workspace.workspaceFolders ?? []).map((folder) => folder.uri.fsPath)
        if (!workspaceFolders) {
            return []
        }
        workspaceFolders.sort()
        const workspaceFolder = workspaceFolders[0]
        for (const contextCommand of contextCommands) {
            session.relativePathToWorkspaceRoot.set(contextCommand.workspaceFolder, contextCommand.workspaceFolder)
        }
        let prompts: AdditionalContextPrompt[] = []
        try {
            prompts = await LspClient.instance.getContextCommandPrompt(contextCommands)
        } catch (e) {
            // todo: handle @workspace used before indexing is ready
            getLogger().verbose(`Could not get context command prompts: ${e}`)
        }

        triggerPayload.contextLengths.additionalContextLengths = this.telemetryHelper.getContextLengths(prompts)
        for (const prompt of prompts.slice(0, 20)) {
            // Add system prompt for user prompts and workspace rules
            const contextType = this.telemetryHelper.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

            // Handle user prompts outside the workspace
            const relativePath = prompt.filePath.startsWith(getUserPromptsDirectory())
                ? path.basename(prompt.filePath)
                : path.relative(workspaceFolder, prompt.filePath)

            const entry = {
                name: prompt.name.substring(0, aditionalContentNameLimit),
                description: description.substring(0, aditionalContentNameLimit),
                innerContext: prompt.content.substring(0, additionalContentInnerContextLimit),
                type: contextType,
                relativePath: relativePath,
                startLine: prompt.startLine,
                endLine: prompt.endLine,
            }

            triggerPayload.additionalContents.push(entry)
        }
        getLogger().info(`Retrieved chunks of additional context count: ${triggerPayload.additionalContents.length} `)
    }