summaryrefslogtreecommitdiff
path: root/internal/hexaiaction/run.go
blob: d32edbf2ff23445d570fdf64f8c1b55ff7c727c3 (plain)
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
package hexaiaction

import (
	"context"
	"fmt"
	"io"
	"log"
	"strings"

	"codeberg.org/snonux/hexai/internal/appconfig"
	"codeberg.org/snonux/hexai/internal/editor"
	"codeberg.org/snonux/hexai/internal/llmutils"
	"codeberg.org/snonux/hexai/internal/logging"
	"codeberg.org/snonux/hexai/internal/tmux"
)

// Run executes the hexai-tmux-action command flow.
// seams for testability
var (
	chooseActionFn   = RunTUI
	newClientFromApp = llmutils.NewClientFromApp
)

// selectedCustom carries the chosen custom action (if any) from the TUI submenu
// to the executor. Cleared after use.
var selectedCustom *appconfig.CustomAction

func Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error {
	logger := log.New(stderr, "hexai-tmux-action ", log.LstdFlags|log.Lmsgprefix)
	cfg := appconfig.Load(logger)
	if err := cfg.Validate(); err != nil {
		fmt.Fprintf(stderr, logging.AnsiBase+"hexai-tmux-action: %v"+logging.AnsiReset+"\n", err)
		return err
	}
	// Enable custom action submenu with configurable hotkey
	if len(cfg.CustomActions) > 0 {
		chooseActionFn = func() (ActionKind, error) { return RunTUIWithCustom(cfg.CustomActions, cfg.TmuxCustomMenuHotkey) }
	}
	cli, err := newClientFromApp(cfg)
	if err != nil {
		fmt.Fprintf(stderr, logging.AnsiBase+"hexai-tmux-action: LLM disabled: %v"+logging.AnsiReset+"\n", err)
		return err
	}
	_ = tmux.SetStatus(tmux.FormatLLMStartStatus(cli.Name(), cli.DefaultModel()))
	var client chatDoer = cli
	parts, err := ParseInput(stdin)
	if err != nil {
		fmt.Fprintln(stderr, logging.AnsiBase+"hexai-tmux-action: failed to read input"+logging.AnsiReset)
		return err
	}
	if strings.TrimSpace(parts.Selection) == "" {
		return fmt.Errorf("hexai-tmux-action: no input provided on stdin")
	}
	kind, err := chooseActionFn()
	if err != nil {
		return err
	}
	out, err := executeAction(ctx, kind, parts, cfg, client, stderr)
	if err != nil {
		return err
	}
	io.WriteString(stdout, out)
	return nil
}

func executeAction(ctx context.Context, kind ActionKind, parts InputParts, cfg appconfig.App, client chatDoer, stderr io.Writer) (string, error) {
	switch kind {
	case ActionSkip:
		return parts.Selection, nil
	case ActionRewrite:
		instr, cleaned := ExtractInstruction(parts.Selection)
		if strings.TrimSpace(instr) == "" {
			fmt.Fprintln(stderr, logging.AnsiBase+"hexai-tmux-action: no inline instruction found; echoing input"+logging.AnsiReset)
			return parts.Selection, nil
		}
		cctx, cancel := timeout10s(ctx)
		defer cancel()
		return runRewrite(cctx, cfg, client, instr, cleaned)
	case ActionDiagnostics:
		cctx, cancel := timeout10s(ctx)
		defer cancel()
		return runDiagnostics(cctx, cfg, client, parts.Diagnostics, parts.Selection)
	case ActionDocument:
		cctx, cancel := timeout10s(ctx)
		defer cancel()
		return runDocument(cctx, cfg, client, parts.Selection)
	case ActionGoTest:
		cctx, cancel := timeout8s(ctx)
		defer cancel()
		return runGoTest(cctx, cfg, client, parts.Selection)
	case ActionSimplify:
		cctx, cancel := timeout10s(ctx)
		defer cancel()
		return runSimplify(cctx, cfg, client, parts.Selection)
	case ActionCustom:
		cctx, cancel := timeout10s(ctx)
		defer cancel()
		if selectedCustom != nil {
			// Run configured custom action
			out, err := runCustom(cctx, cfg, client, *selectedCustom, parts)
			selectedCustom = nil // clear after use
			return out, err
		}
		// Fallback: open editor for free-form instruction
		prompt, err := editor.OpenTempAndEdit(nil)
		if err != nil || strings.TrimSpace(prompt) == "" {
			fmt.Fprintln(stderr, logging.AnsiBase+"hexai-tmux-action: custom prompt canceled or empty; echoing input"+logging.AnsiReset)
			return parts.Selection, nil
		}
		return runRewrite(cctx, cfg, client, prompt, parts.Selection)
	default:
		return parts.Selection, nil
	}
}

// client construction is shared via internal/llmutils