Skip to content

Commit

Permalink
✨ configurable suggestion selection for different shell and platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufcanb committed Mar 12, 2024
1 parent a73d3f4 commit cbf5eba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
13 changes: 9 additions & 4 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"path"
)

var (
defaultSuggestionPolicy = "stable"
defaultExplainPolicy = "creative"
defaultShell = "auto"
)

func isExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
Expand All @@ -28,11 +34,10 @@ func (c *Config) LoadOrCreateConfig() {

configPath := path.Join(homeDir, ".tlm.yaml")
if !isExists(configPath) {
viper.Set("shell", shell.GetShell())

viper.Set("shell", defaultShell)
viper.Set("llm.host", defaultLLMHost)
viper.Set("llm.suggestion", "balanced")
viper.Set("llm.explain", "balanced")
viper.Set("llm.suggestion", defaultSuggestionPolicy)
viper.Set("llm.explain", defaultExplainPolicy)

err := os.Setenv("OLLAMA_HOST", defaultLLMHost)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import (
)

var (
defaultLLMHost = "http://localhost:11434"
shellKey = "shell"
llmHostKey = "llm.host"
llmExplainKey = "llm.explain"
llmSuggestKey = "llm.suggest"
defaultLLMHost = "http://localhost:11434"
)

func (c *Config) Action(_ *cli.Context) error {
var err error

form := ConfigForm{
host: viper.GetString(llmHostKey),
shell: viper.GetString(shellKey),
explain: viper.GetString(llmExplainKey),
suggest: viper.GetString(llmSuggestKey),
}
Expand Down
17 changes: 10 additions & 7 deletions config/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,35 @@ func (c *ConfigForm) Run() error {
Value(&c.host),

huh.NewSelect[string]().
Title("Default Shell (Windows)").
Title("Shell").
Description("Overrides platform's shell for suggestions").
Options(
huh.NewOption("Windows Powershell", "powershell"),
huh.NewOption("Windows Command Prompt", "cmd"),
huh.NewOption("Automatic", "auto"),
huh.NewOption("Powershell (Windows)", "powershell"),
huh.NewOption("Bash (Linux)", "bash"),
huh.NewOption("Zsh (macOS)", "zsh"),
).
Value(&c.shell),

huh.NewSelect[string]().
Title("Suggestion Preference").
Description("Sets preference for command suggestions").
Options(
huh.NewOption("Stable", "stable"),
huh.NewOption("Precise", "stable"),
huh.NewOption("Balanced", "balanced"),
huh.NewOption("Creative", "creative"),
).
Value(&c.explain),
Value(&c.suggest),

huh.NewSelect[string]().
Title("Explain Preference").
Description("Sets preference for command explanations").
Options(
huh.NewOption("Stable", "stable"),
huh.NewOption("Precise", "stable"),
huh.NewOption("Balanced", "balanced"),
huh.NewOption("Creative", "creative"),
).
Value(&c.suggest),
Value(&c.explain),
),
)

Expand Down
21 changes: 18 additions & 3 deletions suggest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
ollama "github.com/jmorganca/ollama/api"
"github.com/yusufcanb/tlm/shell"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -62,13 +63,27 @@ func (s *Suggest) extractCommandsFromResponse(response string) []string {
return codeSnippets
}

func (s *Suggest) getCommandSuggestionFor(mode, shell string, prompt string) (string, error) {
func (s *Suggest) getCommandSuggestionFor(mode, term string, prompt string) (string, error) {
var responseText string

builder := strings.Builder{}
builder.WriteString(prompt)
builder.WriteString(fmt.Sprintf(". I'm using %s terminal", shell))
builder.WriteString(fmt.Sprintf("on operating system: %s", runtime.GOOS))

switch term {
case "zsh":
builder.WriteString(fmt.Sprintf(". I'm using %s terminal", term))
builder.WriteString(fmt.Sprintf("on operating system: %s", "macOS"))
case "bash":
builder.WriteString(fmt.Sprintf(". I'm using %s terminal", term))
builder.WriteString(fmt.Sprintf("on operating system: %s", "Linux"))
case "powershell":
builder.WriteString(fmt.Sprintf(". I'm using %s terminal", term))
builder.WriteString(fmt.Sprintf("on operating system: %s", "Windows"))

default:
builder.WriteString(fmt.Sprintf(". I'm using %s terminal", shell.GetShell()))
builder.WriteString(fmt.Sprintf("on operating system: %s", runtime.GOOS))
}

stream := false
req := &ollama.GenerateRequest{
Expand Down

0 comments on commit cbf5eba

Please sign in to comment.