diff --git a/PoshCodex/Docs/Write-Completion.md b/PoshCodex/Docs/Enter-CompletionKeybind.md similarity index 60% rename from PoshCodex/Docs/Write-Completion.md rename to PoshCodex/Docs/Enter-CompletionKeybind.md index 58b0bb8..3e17efd 100644 --- a/PoshCodex/Docs/Write-Completion.md +++ b/PoshCodex/Docs/Enter-CompletionKeybind.md @@ -1,59 +1,66 @@ ---- -external help file: PoshCodex-help.xml -Module Name: PoshCodex -online version: -schema: 2.0.0 ---- - -# Write-Completion - -## SYNOPSIS - -This function is called when the keybind is hit, and should only be called by hitting the keybind, NOT manually because that would defeat the purpose of completion (since this function reads the current buffer and tries to generate the completion). - -## SYNTAX - -``` -Write-Completion [-ProgressAction ] [] -``` - -## DESCRIPTION - -Check help for the module to learn more. - -PS> Get-Help PoshCodex - -## EXAMPLES - -## PARAMETERS - -### -ProgressAction - -{{ Fill ProgressAction Description }} - -```yaml -Type: ActionPreference -Parameter Sets: (All) -Aliases: proga - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### CommonParameters -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). - -## INPUTS - -### None (reads current buffer) - -## OUTPUTS - -### None (writes to console) - -## NOTES - -## RELATED LINKS +--- +external help file: PoshCodex-help.xml +Module Name: PoshCodex +online version: +schema: 2.0.0 +--- + +# Enter-CompletionKeybind + +## SYNOPSIS + +{{ Change the completion keybind for PoshCodex by reading user input. }} + +## SYNTAX + +``` +Enter-CompletionKeybind [-ProgressAction ] [] +``` + +## DESCRIPTION + +{{ }} + +## EXAMPLES + +### Example 1 + +```powershell +PS C:\> {{ Add example code here }} +``` + +{{ Add example description here }} + +## PARAMETERS + +### -ProgressAction + +{{ Fill ProgressAction Description }} + +```yaml +Type: ActionPreference +Parameter Sets: (All) +Aliases: proga + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters + +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +### None + +## OUTPUTS + +### System.Object + +## NOTES + +## RELATED LINKS diff --git a/PoshCodex/Docs/Set-CompletionKeybind.md b/PoshCodex/Docs/Set-CompletionKeybind.md deleted file mode 100644 index 3585402..0000000 --- a/PoshCodex/Docs/Set-CompletionKeybind.md +++ /dev/null @@ -1,111 +0,0 @@ ---- -external help file: PoshCodex-help.xml -Module Name: PoshCodex -online version: -schema: 2.0.0 ---- - -# Set-CompletionKeybind - -## SYNOPSIS - -This function is used to change the keybind that calls the Write-Completion function. - -## SYNTAX - -``` -Set-CompletionKeybind [[-old_keybind] ] [[-new_keybind] ] [-ProgressAction ] - [] -``` - -## DESCRIPTION - -Check help for the module to learn more. - -PS> Get-Help PoshCodex - -## EXAMPLES - -### Example 1 - -```powershell -PS C:\> Set-CompletionKeybind 'Shift+y' -``` - -### Example 2 - -```powershell -PS C:\> Set-CompletionKeybind 'Tab' -``` - -### Example 3 - -```powershell -PS C:\> Set-CompletionKeybind 'Ctrl+K,Ctrl+E' -``` - -## PARAMETERS - -### -new_keybind - -{{ Fill new_keybind Description }} - -```yaml -Type: Object -Parameter Sets: (All) -Aliases: - -Required: False -Position: 1 -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -old_keybind - -{{ Fill old_keybind Description }} - -```yaml -Type: Object -Parameter Sets: (All) -Aliases: - -Required: False -Position: 0 -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -ProgressAction - -{{ Fill ProgressAction Description }} - -```yaml -Type: ActionPreference -Parameter Sets: (All) -Aliases: proga - -Required: False -Position: Named -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### CommonParameters - -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). - -## INPUTS - -### String (keybind to set) - -## OUTPUTS - -### None - -## NOTES - -## RELATED LINKS diff --git a/PoshCodex/Source/Private/Invoke-Ollama-Api.ps1 b/PoshCodex/Source/Private/Invoke-Ollama-Api.ps1 index bc19469..b297681 100644 --- a/PoshCodex/Source/Private/Invoke-Ollama-Api.ps1 +++ b/PoshCodex/Source/Private/Invoke-Ollama-Api.ps1 @@ -4,17 +4,17 @@ function Invoke-Ollama-Api { $BUFFER ) - # [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line) - # [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() + $ollama_model = [Environment]::GetEnvironmentVariable('OLLAMA_MODEL', 'User') + $ollama_host = [Environment]::GetEnvironmentVariable('OLLAMA_HOST', 'User') $data = @{ - model = "$env:OLLAMA_MODEL" + model = "$ollama_model" prompt = $BUFFER stream = $false } $json_output = Invoke-RestMethod -Method POST ` - -Uri "$env:OLLAMA_HOST/api/generate" ` + -Uri "$ollama_host/api/generate" ` -Body ($data | ConvertTo-Json) ` -ContentType 'application/json; charset=utf-8'; diff --git a/PoshCodex/Source/Private/Set-CompletionKeybind.ps1 b/PoshCodex/Source/Private/Set-CompletionKeybind.ps1 index 22f7fad..7b2a183 100644 --- a/PoshCodex/Source/Private/Set-CompletionKeybind.ps1 +++ b/PoshCodex/Source/Private/Set-CompletionKeybind.ps1 @@ -18,4 +18,7 @@ function Set-CompletionKeybind { -BriefDescription Write-Completion ` -LongDescription 'Autocomplete the command' ` -ScriptBlock { Write-Completion } + + # Update env var with new keybind + [Environment]::SetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', $new_keybind, [System.EnvironmentVariableTarget]::User) } \ No newline at end of file diff --git a/PoshCodex/Source/Public/Enter-CompletionKeybind.ps1 b/PoshCodex/Source/Public/Enter-CompletionKeybind.ps1 index da0e1e1..95eed00 100644 --- a/PoshCodex/Source/Public/Enter-CompletionKeybind.ps1 +++ b/PoshCodex/Source/Public/Enter-CompletionKeybind.ps1 @@ -18,9 +18,5 @@ function Enter-CompletionKeybind { # get old keybind, call set keybind for new $old_autocomplete_keybind = [Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User') Set-CompletionKeybind $old_autocomplete_keybind $new_autocomplete_keybind - Write-Host "New keybind set: $new_autocomplete_keybind" - - # Update env var with new keybind - [Environment]::SetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', $new_autocomplete_keybind, [System.EnvironmentVariableTarget]::User) - # Write-Host "Value of newly set env var: $([Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User'))" + Write-Host "New keybind set: $([Environment]::GetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', 'User'))" } \ No newline at end of file diff --git a/PoshCodex/Source/Scripts/Initialize-Module-On-Import.ps1 b/PoshCodex/Source/Scripts/Initialize-Module-On-Import.ps1 index 06b90d0..4d30c84 100644 --- a/PoshCodex/Source/Scripts/Initialize-Module-On-Import.ps1 +++ b/PoshCodex/Source/Scripts/Initialize-Module-On-Import.ps1 @@ -1,11 +1,10 @@ ## Set necessary environment variables: -[System.Environment]::SetEnvironmentVariable('OLLAMA_HOST', 'http://localhost:11434', [System.EnvironmentVariableTarget]::User) -[System.Environment]::SetEnvironmentVariable('OLLAMA_MODEL', 'rishi255/posh_codex_model', [System.EnvironmentVariableTarget]::User) +[Environment]::SetEnvironmentVariable('OLLAMA_HOST', 'http://localhost:11434', [System.EnvironmentVariableTarget]::User) +[Environment]::SetEnvironmentVariable('OLLAMA_MODEL', 'rishi255/posh_codex_model', [System.EnvironmentVariableTarget]::User) ## Set default keybind: $default_keybind = 'Ctrl+Shift+O' -Set-CompletionKeybind $null $default_keybind; -[Environment]::SetEnvironmentVariable('AUTOCOMPLETE_KEYBIND', $default_keybind, [System.EnvironmentVariableTarget]::User) \ No newline at end of file +Set-CompletionKeybind $null $default_keybind; \ No newline at end of file diff --git a/PoshCodex/build.ps1 b/PoshCodex/build.ps1 index 46a9438..caf5fca 100644 --- a/PoshCodex/build.ps1 +++ b/PoshCodex/build.ps1 @@ -225,10 +225,10 @@ task Build { } else { # Write-Verbose -Message 'Removing old Help files, to generate new files.' - # Remove-Item -Path ".\Docs\*.*" -Exclude "about_*" + # Remove-Item -Path '.\Docs\*.*' -Exclude 'about_*' if (Get-Module -Name $($ModuleName)) { # Write-Verbose -Message "Module: $($ModuleName) is imported into session, updating Help Files" - # New-MarkdownHelp -Module $ModuleName -OutputFolder ".\Docs" + # New-MarkdownHelp -Module $ModuleName -OutputFolder '.\Docs' Update-MarkdownHelp '.\Docs' New-ExternalHelp '.\Docs' -OutputPath ".\Output\$($ModuleName)\$($ModuleVersion)\en-US\" } diff --git a/README.md b/README.md index 5b75861..980b0b4 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,10 @@
+ src="assets/PoshCodex_Demo.gif" + style="width: 80%" + alt="PoshCodex Demo" + />
You just need to write a comment or variable name and the AI will write the corresponding code. @@ -74,7 +76,7 @@ Forked from the impressive [zsh version of this extension by Tom Doerr](https:// ## How to Install -### 1. Through PowerShellGallery (recommended, cross-platform) +### 1. Using PowerShellGallery (recommended, cross-platform) ```powershell # to install or update to the latest version @@ -83,13 +85,13 @@ Install-Module -Name PoshCodex -Force Import-Module PoshCodex -Force # to check if it's installed properly: -Get-Module -Name PoshCodex # should display the Write-Completion command +Get-Module -Name PoshCodex # should display the Enter-CompletionKeybind command # Auto-import the module on every powershell session, so you can directly use the keybind for completion: echo "`nImport-Module PoshCodex" >> $PROFILE ``` -### 2. Through Scoop (Windows only) +### 2. Using Scoop (Windows only) Scoop is an easy-to-use command-line installer for Windows apps. You can get Scoop from [here](https://scoop.sh/). @@ -97,7 +99,9 @@ Scoop is an easy-to-use command-line installer for Windows apps. You can get Sco scoop bucket add poshcodex_bucket https://github.com/rishi255/posh_codex scoop install PoshCodex # not case sensitive -# to update the module later: +Import-Module PoshCodex -Force + +# to update the module later, you can use: scoop update PoshCodex # Auto-import the module on every powershell session, so you can directly use the keybind for completion: @@ -119,7 +123,8 @@ Invoke-Build -File build.ps1 Import-Module ./Output/PoshCodex//PoshCodex.psd1 # Now the module can be used in the current powershell session. -# See above step for auto-import on every powershell session. +# Auto-import the module on every powershell session, so you can directly use the keybind for completion: +echo "`nImport-Module ./Output/PoshCodex//PoshCodex.psd1" >> $PROFILE ``` ## Configuration of the Ollama Model @@ -133,7 +138,7 @@ scoop install versions/innounp-unicode scoop install ollama # pull the base model -ollama pull rishi255/posh_codex_model +ollama pull rishi255/posh_codex_model:latest # or, create a new model tailored for your needs using Modelfile.txt # (refer https://github.com/ollama/ollama/blob/main/docs/modelfile.md) @@ -148,17 +153,29 @@ Just type a comment or partial code snippet, and hit the keybind! See the GIF above for a demonstration. ```powershell -# type some comment that describes what you want to do, eg: -"# install the 'scoop' module" # with/without any of the quotes - -# Just hit Ctrl+Alt+x (or your own keybind if changed) and the AI will write the corresponding code for you. +# print hello world to the console ``` +`Hit Ctrl+Shift+O after typing the above comment` + ## Changing the keybind -When you import the module for the first time, you can enter your own keybind. Just type `Enter-CompletionKeybind` in the terminal and record the keyboard shortcut you want to use. +**The default keybind is `Ctrl+Shift+O`.** + +After you import the module, you can enter your own keybind. +Just type `Enter-CompletionKeybind` in the terminal and record the keyboard shortcut you want to use. + +## Configuration + +The following environment variables are available for configuration: + +| Environment Variable | Default Value | Description | +| -------------------- | --------------------------- | ----------------------------------------------- | +| `OLLAMA_MODEL` | `rishi255/posh_codex_model` | The Ollama model name to use for AI completion. | +| `OLLAMA_HOST` | `http://localhost:11434` | The base URL of your Ollama API. | +| `AUTOCOMPLETE_KEY` | `Ctrl+Shift+O` | The keybind to use for AI completion. | -## TODO checklist +## The Journey So Far (there's still a lot TODO) - [x] Test basic PS plugin working with hardcoded completions - [x] Test plugin by comparing the generated output from [my text-to-PowerShell OpenAI playground](https://platform.openai.com/playground/chat?models=gpt-3.5-turbo-0125&preset=4FqkeG4WQuIPfOUS6cvXQfQR) @@ -167,14 +184,19 @@ When you import the module for the first time, you can enter your own keybind. J - [x] Integrate with GitHub Actions to auto-publish new versions - [x] Make required modules auto-install when this module is installed - [x] Publish plugin for installation through Scoop -- [x] Add a way to change the hotkey for completion - default is `Ctrl+Alt+x` -- [x] Add a way to change the hotkey for completion by reading key input, instead of user having to call function` +- [x] Add a way to change the hotkey for completion by reading key input: `Enter-CompletionKeybind` +- [ ] Simplify installation - 3 remote-executable setup scripts - one each for install thru PSGallery, Scoop and self build. + - [ ] For PSGallery and Scoop, make only one common script - add it to scoop manifest's pre-install section - [ ] Stream the output, instead of waiting for entire thing to be generated - [ ] OR Show a progress/loading indicator when inference is running +- [ ] Need support for inline completion, currently we are inserting the response on a new line + - [ ] Need to fine tune / prompt engineer the model better for this as well - currently it isn't very good at it - [ ] Switch to chat API instead of generate - to provide context of previous messages? - [ ] Switch from environment variables based configuration to a config file (`poshcodex.ini`) -- [ ] Add proper documentation in `PoshCodex/Docs/about_PoshCodex.md` and `PoshCodex/Docs/Write-Completion.md` -- [ ] Add GIF of working demo in terminal + - [ ] Ensure that getting and setting config values are only done through the config file + - [ ] After this change, `Initialize-Module-On-Import` needs to call `Set-CompletionKeybind` internally after reading latest value from config. - [ ] Make completed text a lighter colour to show that it is only a potential solution + - For changing text colour of prediction, look at `Set-PSReadLineOption` or in that direction +- [ ] Add proper documentation for all the functions and `Docs/about_PoshCodex.md` - [ ] Cycle through suggestions using some modifiable keybind (e.g. `Alt+C`) -- [ ] Make a website playground that lets users try this out live using my API key +- [ ] Make a website playground that lets users try this out live? Need to check feasibility.