src/_Get-CciModuleCompletionSource.ps1

function _Get-CciModuleCompletionSource {
<#
.SYNOPSIS
    Returns the modules available to install, for tab completion and for the
    next-step hints Connect-CciGet prints.
.DESCRIPTION
    Completion must never be slow, never prompt, and never throw - so this only
    reads state that is already established. The distribution store index is
    cached in module state by Connect-CciGet; when connected to a feed the
    repository is queried once and the result cached for the session.
 
    Emits objects with Name (what a user types), Full (the tenant-prefixed
    name) and Version.
#>

    [CmdletBinding()]
    param()

    if ($Script:CciGetModuleCache) { return $Script:CciGetModuleCache }

    $result = @()
    try {
        $tenant = (Get-CciGetConfig).defaultFeed
        $feed = (_Resolve-CciGetFeed -Tenant $tenant)[0]
        $prefixProp = $feed.PSObject.Properties['prefix']
        $prefix = if ($prefixProp) { $prefixProp.Value }

        if ($Script:CciGetSource -eq 'store') {
            $ctx = _Connect-CciBlobStore -Feed $feed
            if ($ctx) {
                $index = _Get-CciBlobModuleIndex -Feed $feed -Context $ctx
                if ($index) {
                    $result = foreach ($p in $index.modules.PSObject.Properties) {
                        $short = if ($prefix -and $p.Name -like "$prefix.*") { $p.Name.Substring($prefix.Length + 1) } else { $p.Name }
                        [pscustomobject]@{ Name = $short; Full = $p.Name; Version = $p.Value.latest }
                    }
                }
            }
        }
        elseif ($Script:CciGetSource -eq 'feed') {
            $repo = _Get-CciGetRepositoryName -FeedName $feed.name
            if (Get-PSResourceRepository -Name $repo -ErrorAction SilentlyContinue) {
                $found = Find-PSResource -Name '*' -Repository $repo -ErrorAction SilentlyContinue
                $result = foreach ($m in ($found | Sort-Object Name -Unique)) {
                    $short = if ($prefix -and $m.Name -like "$prefix.*") { $m.Name.Substring($prefix.Length + 1) } else { $m.Name }
                    [pscustomobject]@{ Name = $short; Full = $m.Name; Version = "$($m.Version)" }
                }
            }
        }
    }
    catch {
        Write-Verbose "cciget: module completion unavailable ($_)."
        return @()
    }

    $result = @($result | Sort-Object Name)
    if ($result.Count) { $Script:CciGetModuleCache = $result }
    $result
}