forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copilot.ps1
248 lines (199 loc) · 6.62 KB
/
copilot.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
function Get-Runnable {
<#
.SYNOPSIS
Gets the runnable code from the result
.DESCRIPTION
Gets the runnable code from the result
.EXAMPLE
Get-Runnable -result $result
#>
[CmdletBinding()]
param(
$result
)
$runnable = for ($idx = 1; $idx -lt $result.Count; $idx++) {
$line = $result[$idx]
if ([string]::IsNullOrEmpty($line)) {
continue
}
$line = $line.Trim()
if ($line.StartsWith('#')) {
continue
}
$line
}
return ($runnable -join "`n")
}
function copilot {
<#
.SYNOPSIS
Use GPT to help you remember PowerShell commands and other command line tools
.DESCRIPTION
Makes the request to GPT, parses the response and displays it in a box and then prompts the user to run the code or not
.EXAMPLE
# via https://twitter.com/ClemMesserli/status/1616312238209376260?s=20&t=KknO2iPk3yrQ7x42ZayS7g
copilot "using PowerShell regex, just code. split user from domain of email address with match: [email protected]"
.EXAMPLE
copilot 'how to get ImportExcel'
.EXAMPLE
copilot 'processes running with more than 700 handles'
.EXAMPLE
copilot 'processes running with more than 700 handles select first 5, company and name, as json'
.EXAMPLE
copilot 'for each file in the current dir list the name and length'
.EXAMPLE
copilot 'Find all enabled users that have a samaccountname similar to Mazi; List SAMAccountName and DisplayName'
#>
[CmdletBinding()]
[alias("??")]
param(
[Parameter(Mandatory)]
$inputPrompt,
$SystemPrompt = 'using powershell, just code:',
[ValidateRange(0, 2)]
[decimal]$temperature = 0.0,
# The maximum number of tokens to generate. default 256
[ValidateRange(1, 4000)]
$max_tokens = 256,
# Don't show prompt for choice
[Switch]$Raw
)
# $inputPrompt = $args -join ' '
#$shell = 'powershell, just code:'
$promptComments = ', include comments'
if (-not $IncludeComments) {
$promptComments = ''
}
$prompt = "{0} {1}: {2}`n" -f $SystemPrompt, $promptComments, $inputPrompt
$prompt += '```'
$completion = Get-GPT3Completion -prompt $prompt -max_tokens $max_tokens -temperature $temperature -stop '```'
$completion = $completion -split "`n"
if ($completion[0] -ceq 'powershell') {
$completion = $completion[1..($completion.Count - 1)]
}
if ($Raw) {
return $completion
}
else {
$result = @($inputPrompt)
$result += ''
$result += $completion
$runnable = Get-Runnable -result $result
if (Test-AifbScriptAnalyzerAvailable) {
$runnable = Invoke-Formatter -ScriptDefinition $runnable -Verbose:$false
}
Write-Codeblock -Text $runnable -ShowLineNumbers -SyntaxHighlight
do {
$userInput = CustomReadHost
switch ($userInput) {
0 {
(Get-Runnable -result $result) | Invoke-Expression
}
1 {
explain -Value (Get-Runnable -result $result)
write-output "`n"
}
2 {
Get-Runnable -result $result | Set-Clipboard
}
3 {
if (Test-VSCodeInstalled) {
(Get-Runnable $result) | code -
}
else {
"Not running"
}
}
default {
"Not running"
}
}
} while ($userInput -eq 1)
}
}
function git? {
<#
.SYNOPSIS
A brief description of what the cmdlet does.
.DESCRIPTION
A detailed description of what the cmdlet does.
.PARAMETER inputPrompt
Prompt to be sent to GPT
.PARAMETER temperature
The sampling temperature to use when generating text. Default is 0.0.
.PARAMETER max_tokens
The maximum number of tokens to generate. Default is 256.
.PARAMETER Raw
Don't show prompt for choice. Default is false.
.EXAMPLE
git? 'compare this branch to master, just the files'
#>
[CmdletBinding()]
param(
$inputPrompt,
[ValidateRange(0, 2)]
[decimal]$temperature = 0.0,
# The maximum number of tokens to generate. default 256
$max_tokens = 256,
# Don't show prompt for choice
[Switch]$Raw
)
$params = @{
inputPrompt = $inputPrompt
SystemPrompt = 'you are an expert at using git command line, just code: '
temperature = $temperature
max_tokens = $max_tokens
Raw = $Raw
}
copilot @params
}
function gh? {
<#
.SYNOPSIS
A brief description of what the cmdlet does.
.DESCRIPTION
A detailed description of what the cmdlet does.
.PARAMETER inputPrompt
Prompt to be sent to GPT
.PARAMETER temperature
The sampling temperature to use when generating text. Default is 0.0.
.PARAMETER max_tokens
The maximum number of tokens to generate. Default is 256.
.PARAMETER Raw
Don't show prompt for choice. Default is false.
.EXAMPLE
gh? 'list all closed PRs opened by dfinke and find the word fix'
.EXAMPLE
gh? 'list issues on dfinke/importexcel'
#>
[CmdletBinding()]
param(
$inputPrompt,
[ValidateRange(0, 2)]
[decimal]$temperature = 0.0,
# The maximum number of tokens to generate. default 256
$max_tokens = 256,
# Don't show prompt for choice
[Switch]$Raw
)
$params = @{
inputPrompt = $inputPrompt
SystemPrompt = '
1. You are an expert at using GitHub gh cli.
2. You are working with GitHub Repositories.
3. If no owner/repo, default to current dir.
4. Handle owner/repo correctly with --repo.
5. Map the prompt to the correct syntax of the gh cli.
6. Some commands require a flag, like --state
7. Handle pluralization to singular correctly for the gh cli syntax.
8. Handle removing spaces in the command and map to the correct syntax of the gh cli.
9. Do not provide an explanation or usage example.
10. Do not tell me about the command to use.
11. Just output the command:
'
temperature = $temperature
max_tokens = $max_tokens
Raw = $Raw
}
copilot @params
}