PoshCodex.psm1

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

$defaultKeybind = 'Ctrl+Shift+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:AutocompleteKeybind
    Write-Host "Previous keybind removed: $global:AutocompleteKeybind"

    Set-PSReadLineKeyHandler -Chord $keybind `
        -BriefDescription Write-Completion `
        -LongDescription 'Autocomplete the stuff' `
        -ScriptBlock { Write-Completion }

    Write-Host "New keybind set: $keybind"
    $global:AutocompleteKeybind = $keybind
}

$global:AutocompleteKeybind = $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()

    $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-Ollama-Api $BUFFER

    # check if json_output is not equal to null
    if ($null -ne $json_output) {
        $completion = $json_output.response

        # 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-Output 'Response returned by API is null! It could be an internal error or the model is not installed properly hrough Ollama. Please fix and try again.' -ForegroundColor Red
    }
}
Export-ModuleMember -Function Write-Completion
#EndRegion - Write-Completion.ps1
### --- PRIVATE FUNCTIONS --- ###
#Region - InstallPrerequisites.ps1
# install scoop

# ask user for their choice of Ollama model to use

# run ollama pull with it

# TODO LATER: ask user for keybind (once keybind func is changed to directly read user input)
#EndRegion - InstallPrerequisites.ps1
#Region - Invoke-Ollama-Api.ps1
function Invoke-Ollama-Api {
    [CmdletBinding()]
    param (
        $BUFFER
    )

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

    $data = @{
        model  = 'posh_codex_model'
        prompt = $BUFFER
        stream = $false
    }

    $json_output = Invoke-RestMethod -Method POST `
        -Uri 'http://localhost:11434/api/generate' `
        -Body ($data | ConvertTo-Json) `
        -ContentType 'application/json; charset=utf-8';

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