PoshCodex.psm1

### --- PUBLIC FUNCTIONS --- ###
#Region - Set-CompletionKeybind.ps1
#Requires -Modules PSReadLine

$defaultKeybind = 'Ctrl+Alt+x';

function Set-CompletionKeybind {
    # Add cmdletBinding to the parameter list
    [CmdletBinding()]
    param(
        $keybind
    )

    # unset current handler for Write-Completion if it exists
    Remove-PSReadLineKeyHandler -Chord $global:previousKeybind
    Write-Host "Previous keybind removed: $global:previousKeybind"

    Set-PSReadLineKeyHandler -Chord $keybind `
        -BriefDescription Write-Completion `
        -LongDescription 'Autocomplete the stuff' `
        -ScriptBlock { Write-Completion }
    
    Write-Host "New keybind set: $keybind"
    $global:previousKeybind = $keybind
}

$global:previousKeybind = $defaultKeybind;
Set-CompletionKeybind $defaultKeybind;
Export-ModuleMember -Function Set-CompletionKeybind
#EndRegion - Set-CompletionKeybind.ps1
#Region - Write-Completion.ps1
#Requires -Modules PSReadLine

function Write-Completion {
    # Add cmdletBinding to the parameter list
    [CmdletBinding()]
    param()

    #? If $env:OPENAI_API_KEY is not set, then print message and exit.
    if ($null -eq $env:OPENAI_API_KEY) {
        # Write the error output to console without using Write-Color module
        $error_message = "`n`$env:OPENAI_API_KEY is not set! Please set it using the following command:`n`$env:OPENAI_API_KEY = `"YOUR_API_KEY`"`nThen, restart the shell and try again.`n"
        Write-Host "$error_message" -ForegroundColor Red
        return
    }

    $BUFFER = $null
    $cursor = $null

    # read text from current buffer
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$BUFFER, [ref]$cursor)
    
    # If the buffer text itself contains double quotes, then we need to escape them.
    $BUFFER = $BUFFER.Replace('"', '""')

    $json_output = Invoke-OpenAI-Api $BUFFER

    # check if json_output is not equal to null
    if ($null -ne $json_output) {
        $completion = $json_output.choices[0].text

        # Insert the completion on the next line. This will NOT cause the command to be executed.
        [Microsoft.PowerShell.PSConsoleReadLine]::InsertLineBelow();
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert($completion)
    }
    else {
        Write-Host 'Response returned by OpenAI API is null! It could be an internal error or an issue with your API key. Please check your API key and try again.' -ForegroundColor Red
    }
}
Export-ModuleMember -Function Write-Completion
#EndRegion - Write-Completion.ps1
### --- PRIVATE FUNCTIONS --- ###
#Region - Invoke-OpenAI-Api.ps1
function Invoke-OpenAI-Api {
    [CmdletBinding()]
    param (
        $BUFFER
    )

    # [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
    # [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()

    $data = @{
        prompt            = "#!/usr/bin/env powershell\n\n" + $BUFFER
        max_tokens        = 64
        temperature       = 0.3
        frequency_penalty = 0.0
        presence_penalty  = 0.0
    }

    $json_output = Invoke-RestMethod "https://api.openai.com/v1/engines/davinci-instruct-beta-v3/completions" `
        -Body (ConvertTo-Json $data) `
        -ContentType "application/json" `
        -Authentication Bearer `
        -Token  ( ConvertTo-SecureString -String $env:OPENAI_API_KEY -AsPlainText) `
        -Method POST

    return $json_output
}
#EndRegion - Invoke-OpenAI-Api.ps1