Overview diagram

Untitled

Untitled

Q: Does it use embeddings to find similar code snippets?

The short answer is, not in this particular context. Instead, it uses Jaccard similarity.

How it works in detail:

Starting point: https://github.com/sourcegraph/cody/blob/main/vscode/src/completions/get-inline-completions.ts#L354

// Fetch context and apply remaining debounce time
const [contextResult] = await Promise.all([
    wrapInActiveSpan('autocomplete.retrieve', () =>
        contextMixer.getContext({
            document,
            position,
            docContext,
            abortSignal,
            maxChars: providerConfig.contextSizeHints.totalChars,
            lastCandidate,
        })
    ),
    remainingInterval > 0
        ? wrapInActiveSpan('autocomplete.debounce.remaining', () => sleep(remainingInterval))
        : null,
])

^Note that it calls contextMixer.getContext().

This function is defined here: https://github.com/sourcegraph/cody/blob/800e4b7f22070307463ec46fdacd38b3d549a188/vscode/src/completions/context/context-mixer.ts#L69

For that, we get retrievers here: https://github.com/sourcegraph/cody/blob/800e4b7f22070307463ec46fdacd38b3d549a188/vscode/src/completions/context/context-mixer.ts#L72

    const { name: strategy, retrievers } = this.strategyFactory.getStrategy(options.document)

JaccardSimilarityRetriever is a retriever that seems to be used for this, and it comes from this line: https://github.com/sourcegraph/cody/blob/800e4b7f22070307463ec46fdacd38b3d549a188/vscode/src/completions/context/context-strategy.ts#L4

The key function is here: https://github.com/sourcegraph/cody/blob/800e4b7f22070307463ec46fdacd38b3d549a188/vscode/src/completions/context/retrievers/jaccard-similarity/jaccard-similarity-retriever.ts#L41

And finally the algorithm is defined here: https://github.com/sourcegraph/cody/blob/800e4b7f22070307463ec46fdacd38b3d549a188/vscode/src/completions/context/retrievers/jaccard-similarity/bestJaccardMatch.ts#L22