export async function fetchSupplementalContext()

in server/aws-lsp-codewhisperer/src/shared/supplementalContextUtil/supplementalContextUtil.ts [19:83]


export async function fetchSupplementalContext(
    document: TextDocument,
    position: Position,
    workspace: Workspace,
    logging: Logging,
    cancellationToken: CancellationToken
): Promise<CodeWhispererSupplementalContext | undefined> {
    const timesBeforeFetching = performance.now()

    const isUtg = isTestFile(document.uri, {
        languageId: document.languageId,
        fileContent: document.getText(),
    })

    try {
        let supplementalContextValue:
            | Pick<CodeWhispererSupplementalContext, 'supplementalContextItems' | 'strategy'>
            | undefined

        if (isUtg) {
            return
        } else {
            supplementalContextValue = await fetchSupplementalContextForSrc(
                document,
                position,
                workspace,
                cancellationToken
            )
        }

        if (supplementalContextValue) {
            const resBeforeTruncation = {
                isUtg: isUtg,
                isProcessTimeout: false,
                supplementalContextItems: supplementalContextValue.supplementalContextItems.filter(
                    item => item.content.trim().length !== 0
                ),
                contentsLength: supplementalContextValue.supplementalContextItems.reduce(
                    (acc, curr) => acc + curr.content.length,
                    0
                ),
                latency: performance.now() - timesBeforeFetching,
                strategy: supplementalContextValue.strategy,
            }

            return truncateSupplementalContext(resBeforeTruncation)
        } else {
            return undefined
        }
    } catch (err) {
        if (err instanceof CancellationError) {
            return {
                isUtg: isUtg,
                isProcessTimeout: true,
                supplementalContextItems: [],
                contentsLength: 0,
                latency: performance.now() - timesBeforeFetching,
                strategy: 'Empty',
            }
        } else {
            logging.log(`Fail to fetch supplemental context for target file ${document.uri}: ${err}`)
            return undefined
        }
    }
}