-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Open
Labels
Needs-TriageFor issues raised to be triaged and prioritized by internal Microsoft teamsFor issues raised to be triaged and prioritized by internal Microsoft teams
Description
Description of the new feature / enhancement
I wrote a help tool very similar to what I see y'all doing in terminal with "powertoys" command and with the ai-powered copy/paste. Just wanted to suggest it as a feature. It requires an OpenAI key, and basically it just takes the context of your failed commands and suggests a new one. It's a very short powershell script I'll just leave it below. I find it very helpful.
Usage 1: when a command fails, just type "help" and the script will suggest a fix.
Usage 2: when you don't know the command you need type "help 'make a new guid'" and it will suggest a one liner for you
param(
[Alias('nc')]
[switch]$NoCopy = $false,
[string]$Description
)
# Requires: Set your OpenAI API key in $env:OPENAI_API_KEY
if ($env:OPENAI_API_KEY -eq $null) {
Write-Host "Error: OPENAI_API_KEY environment variable is not set." -ForegroundColor Red
Write-Host "Get one from https://platform.openai.com/account/api-keys and set it in your environment."
Exit 1
}
if ($Description) {
$prompt = (
"Given this description, reply with a single PowerShell command that best accomplishes it.`n" +
"Do NOT include any explanation, label, or extra text.`n" +
"Description: $Description"
)
} else {
$history = Get-History | Select-Object -Last 1
$lastCommand = $history.CommandLine
$lastError = $Error | Select-Object -First 1 | Out-String
$prompt = (
"Given the PowerShell command and error below, reply with a single improved command to try next.`n" +
"Do NOT include any explanation, label, or extra text.`n" +
"Command: $lastCommand`n" +
"Error: $lastError"
)
}
$body = @{
model = "gpt-3.5-turbo"
messages = @(@{role="user"; content=$prompt})
}
$response = Invoke-RestMethod -Uri "https://api.openai.com/v1/chat/completions" `
-Headers @{ "Authorization" = "Bearer $env:OPENAI_API_KEY" } `
-ContentType "application/json" `
-Method Post `
-Body ($body | ConvertTo-Json -Depth 4)
$suggestion = $response.choices[0].message.content.Trim()
# it usually will respond with "Command: <command>" so strip that if present
if ($suggestion -match ": ") { $suggestion = ($suggestion -split ":",2)[1].Trim() }
Write-Host "Suggestion: " -NoNewLine
Write-Host "$suggestion" -ForegroundColor Green -NoNewLine
if (-not $NoCopy -and -not $n) {
$suggestion | Set-Clipboard
Write-Host " (copied to clipboard)"
}
else {
Write-Host
}
Scenario when this would be used?
Reduce context switching and speed up terminal navigation
Supporting information
No response
Metadata
Metadata
Assignees
Labels
Needs-TriageFor issues raised to be triaged and prioritized by internal Microsoft teamsFor issues raised to be triaged and prioritized by internal Microsoft teams