Closed vroby65 closed 5 months ago
yes_support_patch.txt this is the code: in main.go ... var temperature string var top_p string var max_length string var preprompt string var url *string var yes = false .... isShell := flag.Bool("s", false, "Generate and Execute shell commands.") flag.BoolVar(isShell, "shell", false, "Generate and Execute shell commands.")
isYes := flag.Bool("y", false, "")
flag.BoolVar(isYes, "yes", false, "Deactivate confirm request.")
.....
case isShell:
if len(prompt) > 1 {
go loading(&stopSpin)
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Fprintln(os.Stderr, "You need to provide some text")
fmt.Fprintln(os.Stderr, Example: tgpt -s "How to update system"
)
os.Exit(1)
}
if isYes {
yes=true
}
shellCommand(*preprompt + trimmedPrompt + contextText + pipedInput, yes)
.....
in helper.go
....
func shellCommand(input string, yes bool) { // Get OS operatingSystem := "" if runtime.GOOS == "windows" { operatingSystem = "Windows" } else if runtime.GOOS == "darwin" { operatingSystem = "MacOS" } else if runtime.GOOS == "linux" { result, err := exec.Command("lsb_release", "-si").Output() distro := strings.TrimSpace(string(result)) if err != nil { distro = "" } operatingSystem = "Linux" + "/" + distro } else { operatingSystem = runtime.GOOS }
// Get Shell
shellName := "/bin/sh"
if runtime.GOOS == "windows" {
shellName = "cmd.exe"
if len(os.Getenv("PSModulePath")) > 0 {
shellName = "powershell.exe"
}
} else {
shellEnv := os.Getenv("SHELL")
if len(shellEnv) > 0 {
shellName = shellEnv
}
}
shellPrompt := fmt.Sprintf(
"Your role: Provide only plain text without Markdown formatting. Do not show any warnings or information regarding your capabilities. Do not provide any description. If you need to store any data, assume it will be stored in the chat. Provide only %s command for %s without any description. If there is a lack of details, provide most logical solution. Ensure the output is a valid shell command. If multiple steps required try to combine them together. Prompt: %s\n\nCommand:", shellName, operatingSystem, input)
getCommand(shellPrompt)
}
// Get a command in response func getCommand(shellPrompt string) { checkInputLength(shellPrompt)
resp, err := providers.NewRequest(shellPrompt, structs.Params{ApiKey: *apiKey, ApiModel: *apiModel, Provider: *provider, Max_length: *max_length, Temperature: *temperature, Top_p: *top_p, Preprompt: *preprompt, Url: *url}, structs.ExtraOptions{})
if err != nil {
stopSpin = true
printConnectionErrorMsg(err)
}
defer resp.Body.Close()
stopSpin = true
code := resp.StatusCode
if code >= 400 {
handleStatus400(resp)
}
fmt.Print("\r \r")
scanner := bufio.NewScanner(resp.Body)
// Variables
fullLine := ""
previousText := ""
// Handling each part
for scanner.Scan() {
newText := providers.GetMainText(scanner.Text(), *provider, shellPrompt)
if len(newText) < 1 {
continue
}
mainText := strings.Replace(newText, previousText, "", -1)
previousText = newText
fullLine += mainText
bold.Print(mainText)
}
lineCount := strings.Count(fullLine, "\n") + 1
if lineCount == 1 {
var userInput string
if yes {
userInput ="Y"
bold.Print("\n");
} else {
bold.Print("\n\nExecute shell command? [Y/n]: ")
reader := bufio.NewReader(os.Stdin)
userInput, _ = reader.ReadString('\n')
userInput = strings.TrimSpace(userInput)
}
if userInput != "n" {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
shellName := "cmd"
if len(os.Getenv("PSModulePath")) > 0 {
shellName = "powershell"
}
if shellName == "cmd" {
cmd = exec.Command("cmd", "/C", fullLine)
} else {
cmd = exec.Command("powershell", fullLine)
}
} else {
cmd = exec.Command("bash", "-c", fullLine)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Some error has occurred. Error:", err)
os.Exit(1)
}
}
}
.......
I think that's good idea to add -y parameter to automatize shell request