Skip to content

Commit

Permalink
Merge pull request #36 from rishi255/develop
Browse files Browse the repository at this point in the history
Refactor and new Enter-CompletionKeybind feature
  • Loading branch information
rishi255 authored Oct 1, 2024
2 parents 95f3ec2 + fa2e4ea commit c1ec31a
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 150 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
shared_with_containers_dir

# Environments
.env

Expand Down
38 changes: 36 additions & 2 deletions Modelfile.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
FROM deepseek-coder-v2:16b
FROM deepseek-coder:6.7b

SYSTEM "You are a PowerShell terminal expert who knows ONLY PowerShell and no other language. \nPlease help me complete the following commands in valid PowerShell syntax, you should ONLY output the completed command while keeping it as simple as possible, no need to include any other explanation. Do not put completed command in a code block. You can use PowerShell aliases as well to make commands simpler for beginners coming from Linux."
SYSTEM """You are a PowerShell expert AI for autocompletion in the terminal, that knows ONLY PowerShell and no other language. Please help me complete the following commands in valid PowerShell syntax, while always following these 3 rules: 1. You should ONLY output the completed PowerShell code in a few lines (maximum) while keeping it as simple as possible, do not include any other text or explanations. 2. Do NOT use code blocks in output. 3. DO NOT use commands that are invalid in PowerShell."""

PARAMETER num_predict -2
PARAMETER temperature 0.4
PARAMETER num_ctx 4096
PARAMETER repeat_last_n -1

MESSAGE user Print "hello world" to the console.
MESSAGE assistant echo "Hello world"
MESSAGE user Print all directories containing "python" in the current directory
MESSAGE assistant Get-ChildItem | findstr "python"
MESSAGE user Rename each directory found in the previous example to append "_py" at the end
MESSAGE assistant foreach ($dir in $(gci -Name | findstr "python")) { mv "$dir" $dir"_py" }
MESSAGE user Reload current powershell profile present in $PROFILE
MESSAGE assistant . $PROFILE
MESSAGE user Run "testing.py" and store the output in "output.txt"
MESSAGE assistant python testing.py > output.txt
MESSAGE user Move each directory containing "python" to the Desktop
MESSAGE assistant Get-ChildItem -Directory -fi *python* | Move-Item -Destination ~/Desktop/
MESSAGE user List all ruby files in the current folder
MESSAGE assistant Get-ChildItem | where Name -match '\.rb$'
MESSAGE user Display names of all text files in all subfolders recursively
MESSAGE assistant gci -r -LiteralPath . | where { $_.Name -like "*.txt" }
MESSAGE user Delete the file with the smallest size in the current directory
MESSAGE assistant gci . | sort Length | select -First 1 | rm
MESSAGE user Print the most recently modified file in the directory called "tests"
MESSAGE assistant gci tests | sort LastWriteTime | select -Last 1 | remove-item
MESSAGE user Delete the most recently modified ruby file in all subfolders of Desktop
MESSAGE assistant gci -LiteralPath ~/Desktop -r -fi *.rb | sort LastWriteTime | select -Last 1 | remove-item
MESSAGE user Rename all javascript files in all subfolders by adding "js_" to the beginning of the filenames
MESSAGE assistant gci -r -fi *.js | Rename-Item -NewName { "js_" + $_.Name };
MESSAGE user Print only the file extensions of each file in the current directory
MESSAGE assistant gci -File | select Extension
MESSAGE user Append the filename with extension to each file in the current directory
MESSAGE assistant foreach ($file in $(gci -File *.txt)) {echo $file.Extension >> $file}
21 changes: 18 additions & 3 deletions PoshCodex/Docs/Set-CompletionKeybind.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This function is used to change the keybind that calls the Write-Completion func
## SYNTAX

```
Set-CompletionKeybind [[-keybind] <Object>] [-ProgressAction <ActionPreference>] [<CommonParameters>]
Set-CompletionKeybind [[-old_keybind] <Object>] [[-new_keybind] <Object>] [-ProgressAction <ActionPreference>]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -45,9 +46,23 @@ PS C:\> Set-CompletionKeybind 'Ctrl+K,Ctrl+E'

## PARAMETERS

### -keybind
### -new_keybind
{{ Fill new_keybind Description }}

{{ Fill 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
Expand Down
158 changes: 79 additions & 79 deletions PoshCodex/Source/PoshCodex.psd1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#
#
# Module manifest for module 'PoshCodex'
#
# Generated by: Rishikesh Rachchh
Expand All @@ -8,125 +8,125 @@

@{

# Script module or binary module file associated with this manifest.
# RootModule = ''
# Script module or binary module file associated with this manifest.
# RootModule = ''

# Version number of this module.
ModuleVersion = '0.6.4'
# Version number of this module.
ModuleVersion = '0.6.4'

# Supported PSEditions
# CompatiblePSEditions = @()
# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = '314fd9e1-ca0a-4e0d-946a-71c730e31519'
# ID used to uniquely identify this module
GUID = '314fd9e1-ca0a-4e0d-946a-71c730e31519'

# Author of this module
Author = 'Rishikesh Rachchh'
# Author of this module
Author = 'Rishikesh Rachchh'

# Company or vendor of this module
CompanyName = 'Rishikesh Rachchh'
# Company or vendor of this module
CompanyName = 'Rishikesh Rachchh'

# Copyright statement for this module
Copyright = '(c) 2024 Rishikesh Rachchh. All rights reserved.'
# Copyright statement for this module
Copyright = '(c) 2024 Rishikesh Rachchh. All rights reserved.'

# Description of the functionality provided by this module
Description = 'AI Assistant for your PowerShell terminal.'
# Description of the functionality provided by this module
Description = 'AI Assistant for your PowerShell terminal.'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '6.0.0'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '6.0.0'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('PSReadLine')
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('PSReadLine')

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @('')

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'
# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()
# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()
# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Windows','PSEdition_Core','PSEdition_Desktop','codex','copilot','terminal','powershell-terminal','powershell-terminal-copilot','open-ai','natural-language-to-code','llama','ollama','deepseek','deepseek-coder','deepseek-coder-v2','code-completion'
# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Windows', 'PSEdition_Core', 'PSEdition_Desktop', 'codex', 'copilot', 'terminal', 'powershell-terminal', 'powershell-terminal-copilot', 'open-ai', 'natural-language-to-code', 'llama', 'ollama', 'deepseek', 'deepseek-coder', 'deepseek-coder-v2', 'code-completion'

# A URL to the license for this module.
LicenseUri = 'https://github.com/rishi255/posh_codex/blob/main/LICENSE'
# A URL to the license for this module.
LicenseUri = 'https://github.com/rishi255/posh_codex/blob/main/LICENSE'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/rishi255/posh_codex'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/rishi255/posh_codex'

# A URL to an icon representing this module.
# IconUri = ''
# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''
# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''
# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
ExternalModuleDependencies = @('PSReadLine')
# External dependent modules of this module
ExternalModuleDependencies = @('PSReadLine')

} # End of PSData hashtable
} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''
# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

37 changes: 37 additions & 0 deletions PoshCodex/Source/Private/Convert-KeyPressToString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#Requires -Modules PSReadLine

# Function to convert a user input key press into a keybind string
function Convert-KeyPressToString {
# Add cmdletBinding to the parameter list
[CmdletBinding()]
param($key)

# First, check and append Ctrl if it's pressed
if ($key.Modifiers -band [ConsoleModifiers]::Control) {
$keybind += 'Ctrl+'
}

# Next, check and append Alt if it's pressed
if ($key.Modifiers -band [ConsoleModifiers]::Alt) {
$keybind += 'Alt+'
}

# Lastly, check and append Shift if it's pressed
if ($key.Modifiers -band [ConsoleModifiers]::Shift) {
$keybind += 'Shift+'
}

# Append the actual key that was pressed
$keybind += $key.Key.ToString()

# Display the keybind string
# Write-Host "Keybind entered: $keybind"

# If the Escape key is pressed, exit the loop
if ($key.Key -eq 'Escape') {
Write-Host 'Aborted by user, exiting...'
return $null
}

return $keybind
}
7 changes: 0 additions & 7 deletions PoshCodex/Source/Private/InstallPrerequisites.ps1

This file was deleted.

4 changes: 2 additions & 2 deletions PoshCodex/Source/Private/Invoke-Ollama-Api.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function Invoke-Ollama-Api {
# [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()

$data = @{
model = 'posh_codex_model'
model = "$env:OLLAMA_MODEL"
prompt = $BUFFER
stream = $false
}

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

Expand Down
21 changes: 21 additions & 0 deletions PoshCodex/Source/Private/Set-CompletionKeybind.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Requires -Modules PSReadLine

# Function to simply handle the setting and removal of keybinds, without worrying about the user input.
function Set-CompletionKeybind {
# Add cmdletBinding to the parameter list
[CmdletBinding()]
param(
$old_keybind, $new_keybind
)

if ($null -ne $old_keybind) {
# unset current handler for Write-Completion if it exists
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 }
}
Loading

0 comments on commit c1ec31a

Please sign in to comment.