Skip to content

Commit

Permalink
Merge pull request #41 from kborowinski/main
Browse files Browse the repository at this point in the history
Code refactoring
  • Loading branch information
rishi255 authored Oct 17, 2024
2 parents 395fff5 + a4eb1d5 commit 3553bfd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
6 changes: 3 additions & 3 deletions PoshCodex/Source/Private/Convert-KeyPressToString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function Convert-KeyPressToString {
# If the Escape key is pressed, exit the loop
if ($key.Key -eq 'Escape') {
Write-Host 'Aborted by user, exiting...'
return $null
return
}

return $keybind
}
$keybind
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Invoke-Ollama-Api {
function Invoke-OllamaApi {
[CmdletBinding()]
param (
$BUFFER
Expand All @@ -13,10 +13,11 @@ function Invoke-Ollama-Api {
stream = $false
}

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

return $json_output
}
$splatRestMethod = @{
Method = 'POST'
Uri = "$ollama_host/api/generate"
Body = ConvertTo-Json -InputObject $data
ContentType = 'application/json; charset=utf-8'
}
Invoke-RestMethod @splatRestMethod
}
19 changes: 11 additions & 8 deletions PoshCodex/Source/Private/Set-CompletionKeybind.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ function Set-CompletionKeybind {
Remove-PSReadLineKeyHandler -Chord $old_keybind
Write-Host "Previous keybind removed: $old_keybind"
}

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


$splatKeyHandler = @{
Chord = $new_keybind
BriefDescription = 'Write-Completion'
Description = 'Autocomplete the command'
ScriptBlock = { Write-Completion }
}
Set-PSReadLineKeyHandler @splatKeyHandler

# Update env var with new keybind
[Environment]::SetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', $new_keybind, [System.EnvironmentVariableTarget]::User)
}
[Environment]::SetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', $new_keybind, [EnvironmentVariableTarget]::User)
}
8 changes: 4 additions & 4 deletions PoshCodex/Source/Private/Write-Completion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function Write-Completion {

# 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
$json_output = Invoke-OllamaApi $BUFFER

# check if json_output is not equal to null
if ($null -ne $json_output) {
Expand All @@ -25,6 +25,6 @@ function Write-Completion {
[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
Write-Host 'Response returned by API is null! It could be an internal error or the model is not installed properly through Ollama. Please fix and try again.' -ForegroundColor Red
}
}
}
11 changes: 6 additions & 5 deletions PoshCodex/Source/Scripts/Initialize-Module-On-Import.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
## Set necessary environment variables:

[Environment]::SetEnvironmentVariable('OLLAMA_HOST', 'http://localhost:11434', [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable('OLLAMA_MODEL', 'rishi255/posh_codex_model', [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable('OLLAMA_HOST', 'http://localhost:11434', [EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable('OLLAMA_MODEL', 'rishi255/posh_codex_model', [EnvironmentVariableTarget]::User)

$current_keybind = [Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User')

if (-not [Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User')) {
$default_keybind = if ([String]::IsNullOrWhiteSpace($current_keybind)) {
## Use default keybind, if none is set
$default_keybind = 'Ctrl+Shift+O'
'Ctrl+Shift+O'
} else {
## Use existing keybind
$default_keybind = [Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User')
$current_keybind
}
Set-CompletionKeybind $null $default_keybind

0 comments on commit 3553bfd

Please sign in to comment.