forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnable-AIShortCutKey.ps1
56 lines (48 loc) · 1.8 KB
/
Enable-AIShortCutKey.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function Enable-AIShortCutKey {
<#
.SYNOPSIS
Enable a shortcut key for getting completions
.DESCRIPTION
Enables a shortcut key for getting completions.
The -ShortcutKey can be customized.
If not in Visual Studio Code, the ShortcutKey will default to 'CTRL+G'
In Visual Studio Code, the ShortcutKey will default to 'ALT+G'
.EXAMPLE
# Enables the shortcut key. Outside of VSCode, CTRL+G. Inside of VSCode, ALT+G.
Enable-AIShortCutKey
.EXAMPLE
Enable-AIShortCutKey -ShortcutKey "CTRL+ALT+P"
#>
param(
[string]
$ShortcutKey = $(
# In Visual Studio Code, CTRL+G is "goto",
if ((Get-Process -id $pid).Parent.ProcessName -eq 'code') {
"ALT+G" # so we'll use ALT+G by default.
} else {
"CTRL+G" # If we're not running in code, use CTRL+G by default
}
)
)
Set-PSReadLineKeyHandler -Key $ShortcutKey `
-BriefDescription OpenAICli `
-LongDescription "Calls Open AI on the current buffer" `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
$prompt = "Using PowerShell, just code: $($line)"
$output = Get-GPT3Completion $prompt -max_tokens 256
$output = $output -replace "`r", ""
# check if output is not null
if ($null -ne $output) {
foreach ($str in $output) {
if ($null -ne $str -and $str -ne "") {
[Microsoft.PowerShell.PSConsoleReadLine]::AddLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($str)
}
}
}
}
}