From 5ed470a093ffb7d28c88f9687429f238959935da Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 7 Sep 2025 11:49:38 +0300 Subject: test: add seams for RunTUI and client; expand hexaiaction tests; cover lsp initialized and testutil fixtures --- docs/coverage.html | 410 +++++++++++++++++++++++++++-------------------------- 1 file changed, 207 insertions(+), 203 deletions(-) (limited to 'docs/coverage.html') diff --git a/docs/coverage.html b/docs/coverage.html index 8f6eb62..90eae60 100644 --- a/docs/coverage.html +++ b/docs/coverage.html @@ -63,13 +63,13 @@ - + - + @@ -109,7 +109,7 @@ - + @@ -117,7 +117,7 @@ - + @@ -321,7 +321,7 @@ type App struct { } // Constructor: defaults for App (kept first among functions) -func newDefaultConfig() App { +func newDefaultConfig() App { // Coding-friendly default temperature across providers // Users can override per provider in config.toml (including 0.0). t := 0.2 @@ -372,17 +372,17 @@ func newDefaultConfig() App { // Load reads configuration from a file and merges with defaults. // It respects the XDG Base Directory Specification. -func Load(logger *log.Logger) App { +func Load(logger *log.Logger) App { cfg := newDefaultConfig() if logger == nil { return cfg // Return defaults if no logger is provided (e.g. in tests) } - configPath, err := getConfigPath() + configPath, err := getConfigPath() if err != nil { logger.Printf("%v", err) // Even if config path cannot be resolved, still allow env overrides below. - } else { + } else { if fileCfg, err := loadFromFile(configPath, logger); err == nil && fileCfg != nil { cfg.mergeWith(fileCfg) } @@ -391,10 +391,10 @@ func Load(logger *log.Logger) App { } // Environment overrides (take precedence over file) - if envCfg := loadFromEnv(logger); envCfg != nil { + if envCfg := loadFromEnv(logger); envCfg != nil { cfg.mergeWith(envCfg) } - return cfg + return cfg } // Private helpers @@ -665,16 +665,16 @@ func (fc *fileConfig) toApp() App { return out } -func loadFromFile(path string, logger *log.Logger) (*App, error) { +func loadFromFile(path string, logger *log.Logger) (*App, error) { b, err := os.ReadFile(path) - if err != nil { + if err != nil { if !os.IsNotExist(err) && logger != nil { logger.Printf("cannot open TOML config file %s: %v", path, err) } - return nil, err + return nil, err } - var tables fileConfig + var tables fileConfig errTables := toml.NewDecoder(strings.NewReader(string(b))).Decode(&tables) // Raw map for validation/presence checks var raw map[string]any @@ -747,50 +747,50 @@ func (a *App) mergeWith(other *App) { } // mergeBasics merges general (non-provider) fields. -func (a *App) mergeBasics(other *App) { +func (a *App) mergeBasics(other *App) { if other.MaxTokens > 0 { a.MaxTokens = other.MaxTokens } - if s := strings.TrimSpace(other.ContextMode); s != "" { + if s := strings.TrimSpace(other.ContextMode); s != "" { a.ContextMode = s } - if other.ContextWindowLines > 0 { + if other.ContextWindowLines > 0 { a.ContextWindowLines = other.ContextWindowLines } - if other.MaxContextTokens > 0 { + if other.MaxContextTokens > 0 { a.MaxContextTokens = other.MaxContextTokens } - if other.LogPreviewLimit >= 0 { + if other.LogPreviewLimit >= 0 { a.LogPreviewLimit = other.LogPreviewLimit } - if other.CodingTemperature != nil { // allow explicit 0.0 + if other.CodingTemperature != nil { // allow explicit 0.0 a.CodingTemperature = other.CodingTemperature } - if other.ManualInvokeMinPrefix >= 0 { + if other.ManualInvokeMinPrefix >= 0 { a.ManualInvokeMinPrefix = other.ManualInvokeMinPrefix } - if other.CompletionDebounceMs > 0 { + if other.CompletionDebounceMs > 0 { a.CompletionDebounceMs = other.CompletionDebounceMs } - if other.CompletionThrottleMs > 0 { + if other.CompletionThrottleMs > 0 { a.CompletionThrottleMs = other.CompletionThrottleMs } - if len(other.TriggerCharacters) > 0 { + if len(other.TriggerCharacters) > 0 { a.TriggerCharacters = slices.Clone(other.TriggerCharacters) } - if s := strings.TrimSpace(other.InlineOpen); s != "" { + if s := strings.TrimSpace(other.InlineOpen); s != "" { a.InlineOpen = s } - if s := strings.TrimSpace(other.InlineClose); s != "" { + if s := strings.TrimSpace(other.InlineClose); s != "" { a.InlineClose = s } - if s := strings.TrimSpace(other.ChatSuffix); s != "" { + if s := strings.TrimSpace(other.ChatSuffix); s != "" { a.ChatSuffix = s } - if len(other.ChatPrefixes) > 0 { + if len(other.ChatPrefixes) > 0 { a.ChatPrefixes = slices.Clone(other.ChatPrefixes) } - if s := strings.TrimSpace(other.Provider); s != "" { + if s := strings.TrimSpace(other.Provider); s != "" { a.Provider = s } } @@ -889,33 +889,33 @@ func (a *App) mergeProviderFields(other *App) { } } -func getConfigPath() (string, error) { +func getConfigPath() (string, error) { var configPath string if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" { configPath = filepath.Join(xdgConfigHome, "hexai", "config.toml") - } else { + } else { home, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("cannot find user home directory: %v", err) } - configPath = filepath.Join(home, ".config", "hexai", "config.toml") + configPath = filepath.Join(home, ".config", "hexai", "config.toml") } - return configPath, nil + return configPath, nil } // --- Environment overrides --- // loadFromEnv constructs an App containing only fields set via HEXAI_* env vars. // These values should take precedence over file config when merged. -func loadFromEnv(logger *log.Logger) *App { +func loadFromEnv(logger *log.Logger) *App { var out App var any bool // helpers - getenv := func(k string) string { return strings.TrimSpace(os.Getenv(k)) } - parseInt := func(k string) (int, bool) { + getenv := func(k string) string { return strings.TrimSpace(os.Getenv(k)) } + parseInt := func(k string) (int, bool) { v := getenv(k) - if v == "" { + if v == "" { return 0, false } n, err := strconv.Atoi(v) @@ -927,9 +927,9 @@ func loadFromEnv(logger *log.Logger) *App { } return n, true } - parseFloatPtr := func(k string) (*float64, bool) { + parseFloatPtr := func(k string) (*float64, bool) { v := getenv(k) - if v == "" { + if v == "" { return nil, false } f, err := strconv.ParseFloat(v, 64) @@ -942,43 +942,43 @@ func loadFromEnv(logger *log.Logger) *App { return &f, true } - if n, ok := parseInt("HEXAI_MAX_TOKENS"); ok { + if n, ok := parseInt("HEXAI_MAX_TOKENS"); ok { out.MaxTokens = n any = true } - if s := getenv("HEXAI_CONTEXT_MODE"); s != "" { + if s := getenv("HEXAI_CONTEXT_MODE"); s != "" { out.ContextMode = s any = true } - if n, ok := parseInt("HEXAI_CONTEXT_WINDOW_LINES"); ok { + if n, ok := parseInt("HEXAI_CONTEXT_WINDOW_LINES"); ok { out.ContextWindowLines = n any = true } - if n, ok := parseInt("HEXAI_MAX_CONTEXT_TOKENS"); ok { + if n, ok := parseInt("HEXAI_MAX_CONTEXT_TOKENS"); ok { out.MaxContextTokens = n any = true } - if n, ok := parseInt("HEXAI_LOG_PREVIEW_LIMIT"); ok { + if n, ok := parseInt("HEXAI_LOG_PREVIEW_LIMIT"); ok { out.LogPreviewLimit = n any = true } - if n, ok := parseInt("HEXAI_MANUAL_INVOKE_MIN_PREFIX"); ok { + if n, ok := parseInt("HEXAI_MANUAL_INVOKE_MIN_PREFIX"); ok { out.ManualInvokeMinPrefix = n any = true } - if n, ok := parseInt("HEXAI_COMPLETION_DEBOUNCE_MS"); ok { + if n, ok := parseInt("HEXAI_COMPLETION_DEBOUNCE_MS"); ok { out.CompletionDebounceMs = n any = true } - if n, ok := parseInt("HEXAI_COMPLETION_THROTTLE_MS"); ok { + if n, ok := parseInt("HEXAI_COMPLETION_THROTTLE_MS"); ok { out.CompletionThrottleMs = n any = true } - if f, ok := parseFloatPtr("HEXAI_CODING_TEMPERATURE"); ok { + if f, ok := parseFloatPtr("HEXAI_CODING_TEMPERATURE"); ok { out.CodingTemperature = f any = true } - if s := getenv("HEXAI_TRIGGER_CHARACTERS"); s != "" { + if s := getenv("HEXAI_TRIGGER_CHARACTERS"); s != "" { parts := strings.Split(s, ",") out.TriggerCharacters = nil for _, p := range parts { @@ -988,19 +988,19 @@ func loadFromEnv(logger *log.Logger) *App { } any = true } - if s := getenv("HEXAI_INLINE_OPEN"); s != "" { + if s := getenv("HEXAI_INLINE_OPEN"); s != "" { out.InlineOpen = s any = true } - if s := getenv("HEXAI_INLINE_CLOSE"); s != "" { + if s := getenv("HEXAI_INLINE_CLOSE"); s != "" { out.InlineClose = s any = true } - if s := getenv("HEXAI_CHAT_SUFFIX"); s != "" { + if s := getenv("HEXAI_CHAT_SUFFIX"); s != "" { out.ChatSuffix = s any = true } - if s := getenv("HEXAI_CHAT_PREFIXES"); s != "" { + if s := getenv("HEXAI_CHAT_PREFIXES"); s != "" { parts := strings.Split(s, ",") out.ChatPrefixes = nil for _, p := range parts { @@ -1010,52 +1010,52 @@ func loadFromEnv(logger *log.Logger) *App { } any = true } - if s := getenv("HEXAI_PROVIDER"); s != "" { + if s := getenv("HEXAI_PROVIDER"); s != "" { out.Provider = s any = true } // Provider-specific - if s := getenv("HEXAI_OPENAI_BASE_URL"); s != "" { + if s := getenv("HEXAI_OPENAI_BASE_URL"); s != "" { out.OpenAIBaseURL = s any = true } - if s := getenv("HEXAI_OPENAI_MODEL"); s != "" { + if s := getenv("HEXAI_OPENAI_MODEL"); s != "" { out.OpenAIModel = s any = true } - if f, ok := parseFloatPtr("HEXAI_OPENAI_TEMPERATURE"); ok { + if f, ok := parseFloatPtr("HEXAI_OPENAI_TEMPERATURE"); ok { out.OpenAITemperature = f any = true } - if s := getenv("HEXAI_OLLAMA_BASE_URL"); s != "" { + if s := getenv("HEXAI_OLLAMA_BASE_URL"); s != "" { out.OllamaBaseURL = s any = true } - if s := getenv("HEXAI_OLLAMA_MODEL"); s != "" { + if s := getenv("HEXAI_OLLAMA_MODEL"); s != "" { out.OllamaModel = s any = true } - if f, ok := parseFloatPtr("HEXAI_OLLAMA_TEMPERATURE"); ok { + if f, ok := parseFloatPtr("HEXAI_OLLAMA_TEMPERATURE"); ok { out.OllamaTemperature = f any = true } - if s := getenv("HEXAI_COPILOT_BASE_URL"); s != "" { + if s := getenv("HEXAI_COPILOT_BASE_URL"); s != "" { out.CopilotBaseURL = s any = true } - if s := getenv("HEXAI_COPILOT_MODEL"); s != "" { + if s := getenv("HEXAI_COPILOT_MODEL"); s != "" { out.CopilotModel = s any = true } - if f, ok := parseFloatPtr("HEXAI_COPILOT_TEMPERATURE"); ok { + if f, ok := parseFloatPtr("HEXAI_COPILOT_TEMPERATURE"); ok { out.CopilotTemperature = f any = true } - if !any { + if !any { return nil } return &out @@ -1090,22 +1090,22 @@ type Options struct { // RunCommand is the CLI orchestrator used by cmd/hexai-action. It decides whether // to run inline, in a tmux split pane, or in child mode; then delegates to Run. -func RunCommand(ctx context.Context, opts Options, stdin io.Reader, stdout, stderr io.Writer) error { - if opts.UIChild { +func RunCommand(ctx context.Context, opts Options, stdin io.Reader, stdout, stderr io.Writer) error { + if opts.UIChild { return runChild(ctx, opts.Infile, opts.Outfile, stdout, stderr) } - if shouldRunInTmux(opts.ForceTmux, opts.NoTmux) { + if shouldRunInTmux(opts.ForceTmux, opts.NoTmux) { return runInTmuxParent(stdin, stdout, opts.TmuxTarget, opts.TmuxSplit, opts.TmuxPercent) } // Inline path: only if we have a TTY for UI; otherwise echo input - if isTTYFn(os.Stdout.Fd()) && isTTYFn(os.Stdin.Fd()) { + if isTTYFn(os.Stdout.Fd()) && isTTYFn(os.Stdin.Fd()) { in, out, closeIn, closeOut, err := openIO(opts.Infile, opts.Outfile) if err != nil { return err } defer closeIn(); defer closeOut() return Run(ctx, in, out, stderr) } // Fallback: echo - return echoThrough(opts.Infile, opts.Outfile, stdin, stdout) + return echoThrough(opts.Infile, opts.Outfile, stdin, stdout) } // seams for unit tests @@ -1115,36 +1115,36 @@ var splitRunFn = tmux.SplitRun var osExecutableFn = os.Executable var runFn = Run -func shouldRunInTmux(forceTmux, noTmux bool) bool { - if noTmux { return false } - if forceTmux { return true } - if !(isTTYFn(os.Stdin.Fd()) && isTTYFn(os.Stdout.Fd())) && tmuxAvailableFn() { return true } +func shouldRunInTmux(forceTmux, noTmux bool) bool { + if noTmux { return false } + if forceTmux { return true } + if !(isTTYFn(os.Stdin.Fd()) && isTTYFn(os.Stdout.Fd())) && tmuxAvailableFn() { return true } return false } // openIO returns readers/writers for infile/outfile flags with deferred closers. -func openIO(infile, outfile string) (io.Reader, io.Writer, func(), func(), error) { +func openIO(infile, outfile string) (io.Reader, io.Writer, func(), func(), error) { in := io.Reader(os.Stdin) out := io.Writer(os.Stdout) closeIn := func() {} - closeOut := func() {} - if path := infile; path != "" { + closeOut := func() {} + if path := infile; path != "" { f, err := os.Open(path) if err != nil { return nil, nil, func(){}, func(){}, fmt.Errorf("hexai-action: cannot open infile: %w", err) } - in = f - closeIn = func() { _ = f.Close() } + in = f + closeIn = func() { _ = f.Close() } } - if path := outfile; path != "" { + if path := outfile; path != "" { f, err := os.Create(path) if err != nil { return nil, nil, func(){}, func(){}, fmt.Errorf("hexai-action: cannot open outfile: %w", err) } - out = f - closeOut = func() { _ = f.Close() } + out = f + closeOut = func() { _ = f.Close() } } - return in, out, closeIn, closeOut, nil + return in, out, closeIn, closeOut, nil } // runChild runs the interactive flow and writes the final output atomically when outfile is set. -func runChild(ctx context.Context, infile, outfile string, stdout, stderr io.Writer) error { +func runChild(ctx context.Context, infile, outfile string, stdout, stderr io.Writer) error { if outfile == "" { // No atomic handoff needed; just run normally to provided stdout var in io.Reader = os.Stdin @@ -1156,63 +1156,63 @@ func runChild(ctx context.Context, infile, outfile string, stdout, stderr io.Wri } return runFn(ctx, in, stdout, stderr) } - tmp := outfile + ".tmp" + tmp := outfile + ".tmp" in, out, closeIn, closeOut, err := openIO(infile, tmp) if err != nil { return err } - defer closeIn() + defer closeIn() if err := runFn(ctx, in, out, stderr); err != nil { closeOut() if copyErr := echoThrough(infile, tmp, os.Stdin, stdout); copyErr != nil { return fmt.Errorf("hexai-action child: %v; echo failed: %v", err, copyErr) } - } else { + } else { closeOut() } - return os.Rename(tmp, outfile) + return os.Rename(tmp, outfile) } -func runInTmuxParent(stdin io.Reader, stdout io.Writer, target, split string, percent int) error { +func runInTmuxParent(stdin io.Reader, stdout io.Writer, target, split string, percent int) error { dir, err := os.MkdirTemp("", "hexai-action-") if err != nil { return err } - defer func() { _ = os.RemoveAll(dir) }() - inPath := filepath.Join(dir, "input.txt") + defer func() { _ = os.RemoveAll(dir) }() + inPath := filepath.Join(dir, "input.txt") outPath := filepath.Join(dir, "reply.txt") if err := persistStdin(inPath, stdin); err != nil { return err } - exe, err := osExecutableFn() + exe, err := osExecutableFn() if err != nil { return err } - argv := []string{exe, "-ui-child", "-infile", inPath, "-outfile", outPath} + argv := []string{exe, "-ui-child", "-infile", inPath, "-outfile", outPath} opts := tmux.SplitOpts{Target: target, Vertical: split != "h", Percent: percent} if err := splitRunFn(opts, argv); err != nil { return err } - if err := waitForFile(outPath, 60*time.Second); err != nil { return err } - return catFileTo(stdout, outPath) + if err := waitForFile(outPath, 60*time.Second); err != nil { return err } + return catFileTo(stdout, outPath) } -func persistStdin(path string, stdin io.Reader) error { +func persistStdin(path string, stdin io.Reader) error { f, err := os.Create(path) if err != nil { return err } - defer func() { _ = f.Close() }() - if _, err := io.Copy(f, stdin); err != nil { return err } - return f.Sync() + defer func() { _ = f.Close() }() + if _, err := io.Copy(f, stdin); err != nil { return err } + return f.Sync() } -func waitForFile(path string, timeout time.Duration) error { +func waitForFile(path string, timeout time.Duration) error { deadline := time.Now().Add(timeout) - for { - if _, err := os.Stat(path); err == nil { return nil } + for { + if _, err := os.Stat(path); err == nil { return nil } if time.Now().After(deadline) { return fmt.Errorf("hexai-action: timeout waiting for reply file") } time.Sleep(200 * time.Millisecond) } } -func catFileTo(w io.Writer, path string) error { +func catFileTo(w io.Writer, path string) error { f, err := os.Open(path) if err != nil { return err } - defer func() { _ = f.Close() }() - _, err = io.Copy(w, f) + defer func() { _ = f.Close() }() + _, err = io.Copy(w, f) return err } -func echoThrough(infile, outfile string, stdin io.Reader, stdout io.Writer) error { +func echoThrough(infile, outfile string, stdin io.Reader, stdout io.Writer) error { var in io.Reader = stdin var out io.Writer = stdout if infile != "" { @@ -1221,13 +1221,13 @@ func echoThrough(infile, outfile string, stdin io.Reader, stdout io.Writer) erro defer func() { _ = f.Close() }() in = f } - if outfile != "" { + if outfile != "" { f, err := os.Create(outfile) if err != nil { return err } defer func() { _ = f.Close() }() out = f } - _, err := io.Copy(out, in) + _, err := io.Copy(out, in) return err } @@ -1251,32 +1251,32 @@ import ( // <rest is selection/code> // // If the header is absent, the entire input is treated as selection. -func ParseInput(r io.Reader) (InputParts, error) { +func ParseInput(r io.Reader) (InputParts, error) { b, err := io.ReadAll(bufio.NewReader(r)) if err != nil { return InputParts{}, err } - raw := strings.TrimSpace(string(b)) + raw := strings.TrimSpace(string(b)) if raw == "" { return InputParts{Selection: ""}, nil } - lines := strings.Split(raw, "\n") + lines := strings.Split(raw, "\n") // find a case-insensitive line equal to "diagnostics:" diagsIdx := -1 - for i, ln := range lines { + for i, ln := range lines { t := strings.TrimSpace(strings.ToLower(ln)) if t == "diagnostics:" { diagsIdx = i break } } - if diagsIdx < 0 { + if diagsIdx < 0 { return InputParts{Selection: raw}, nil } // collect diagnostics until a blank line or EOF diags := []string{} i := diagsIdx + 1 - for ; i < len(lines); i++ { + for ; i < len(lines); i++ { t := strings.TrimSpace(lines[i]) if t == "" { i++ @@ -1291,7 +1291,7 @@ func ParseInput(r io.Reader) (InputParts, error) { // ExtractInstruction mirrors the LSP instructionFromSelection behavior (subset), // scanning the first line for an instruction marker and removing it from the selection. -func ExtractInstruction(sel string) (string, string) { return textutil.InstructionFromSelection(sel) } +func ExtractInstruction(sel string) (string, string) { return textutil.InstructionFromSelection(sel) } // findFirstInstructionInLine follows the same precedence as LSP: // - ;text; (strict) @@ -1316,16 +1316,16 @@ import ( ) // Render performs simple {{var}} replacement like LSP. -func Render(t string, vars map[string]string) string { return textutil.RenderTemplate(t, vars) } +func Render(t string, vars map[string]string) string { return textutil.RenderTemplate(t, vars) } // StripFences removes surrounding markdown code fences. -func StripFences(s string) string { return textutil.StripCodeFences(s) } +func StripFences(s string) string { return textutil.StripCodeFences(s) } type chatDoer interface { Chat(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error) } -func runRewrite(ctx context.Context, cfg appconfig.App, client chatDoer, instruction, selection string) (string, error) { +func runRewrite(ctx context.Context, cfg appconfig.App, client chatDoer, instruction, selection string) (string, error) { sys := cfg.PromptCodeActionRewriteSystem user := Render(cfg.PromptCodeActionRewriteUser, map[string]string{"instruction": instruction, "selection": selection}) return runOnceWithOpts(ctx, client, sys, user, reqOptsFrom(cfg)) @@ -1368,26 +1368,26 @@ func runOnce(ctx context.Context, client chatDoer, sys, user string) (string, er return strings.TrimSpace(StripFences(txt)), nil } -func runOnceWithOpts(ctx context.Context, client chatDoer, sys, user string, opts []llm.RequestOption) (string, error) { +func runOnceWithOpts(ctx context.Context, client chatDoer, sys, user string, opts []llm.RequestOption) (string, error) { msgs := []llm.Message{{Role: "system", Content: sys}, {Role: "user", Content: user}} txt, err := client.Chat(ctx, msgs, opts...) if err != nil { return "", err } - return strings.TrimSpace(StripFences(txt)), nil + return strings.TrimSpace(StripFences(txt)), nil } // reqOptsFrom builds LLM request options similar to LSP behavior. -func reqOptsFrom(cfg appconfig.App) []llm.RequestOption { +func reqOptsFrom(cfg appconfig.App) []llm.RequestOption { opts := []llm.RequestOption{llm.WithMaxTokens(cfg.MaxTokens)} - if cfg.CodingTemperature != nil { + if cfg.CodingTemperature != nil { opts = append(opts, llm.WithTemperature(*cfg.CodingTemperature)) } - return opts + return opts } // Timeout helpers to mirror LSP behavior. -func timeout10s(parent context.Context) (context.Context, context.CancelFunc) { +func timeout10s(parent context.Context) (context.Context, context.CancelFunc) { return context.WithTimeout(parent, 10*time.Second) } @@ -1411,45 +1411,49 @@ import ( ) // Run executes the hexai-action command flow. -func Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error { - logger := log.New(stderr, "hexai-action ", log.LstdFlags|log.Lmsgprefix) - cfg := appconfig.Load(logger) - client, err := llmutils.NewClientFromApp(cfg) - if err != nil { - fmt.Fprintf(stderr, logging.AnsiBase+"hexai-action: LLM disabled: %v"+logging.AnsiReset+"\n", err) - return err - } - parts, err := ParseInput(stdin) +// seams for testability +var chooseActionFn = RunTUI +var newClientFromApp = llmutils.NewClientFromApp + +func Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error { + logger := log.New(stderr, "hexai-action ", log.LstdFlags|log.Lmsgprefix) + cfg := appconfig.Load(logger) + client, err := newClientFromApp(cfg) + if err != nil { + fmt.Fprintf(stderr, logging.AnsiBase+"hexai-action: LLM disabled: %v"+logging.AnsiReset+"\n", err) + return err + } + parts, err := ParseInput(stdin) if err != nil { fmt.Fprintln(stderr, logging.AnsiBase+"hexai-action: failed to read input"+logging.AnsiReset) return err } - if strings.TrimSpace(parts.Selection) == "" { + if strings.TrimSpace(parts.Selection) == "" { return fmt.Errorf("hexai-action: no input provided on stdin") } - kind, err := RunTUI() - if err != nil { - return err - } - out, err := executeAction(ctx, kind, parts, cfg, client, stderr) + 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) + 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) { +func executeAction(ctx context.Context, kind ActionKind, parts InputParts, cfg appconfig.App, client chatDoer, stderr io.Writer) (string, error) { switch kind { - case ActionSkip: + case ActionSkip: return parts.Selection, nil - case ActionRewrite: + case ActionRewrite: instr, cleaned := ExtractInstruction(parts.Selection) if strings.TrimSpace(instr) == "" { fmt.Fprintln(stderr, logging.AnsiBase+"hexai-action: no inline instruction found; echoing input"+logging.AnsiReset) return parts.Selection, nil } - cctx, cancel := timeout10s(ctx) + cctx, cancel := timeout10s(ctx) defer cancel() return runRewrite(cctx, cfg, client, instr, cleaned) case ActionDiagnostics: @@ -2901,8 +2905,8 @@ type Options struct { type RequestOption func(*Options) func WithModel(model string) RequestOption { return func(o *Options) { o.Model = model } } -func WithTemperature(t float64) RequestOption { return func(o *Options) { o.Temperature = t } } -func WithMaxTokens(n int) RequestOption { return func(o *Options) { o.MaxTokens = n } } +func WithTemperature(t float64) RequestOption { return func(o *Options) { o.Temperature = t } } +func WithMaxTokens(n int) RequestOption { return func(o *Options) { o.MaxTokens = n } } func WithStop(stop ...string) RequestOption { return func(o *Options) { o.Stop = append([]string{}, stop...) } } @@ -3069,11 +3073,11 @@ var std *log.Logger func Bind(l *log.Logger) { std = l } // Logf prints a formatted message with a module prefix and base ANSI style. -func Logf(prefix, format string, args ...any) { +func Logf(prefix, format string, args ...any) { if std == nil { return } - msg := fmt.Sprintf(format, args...) + msg := fmt.Sprintf(format, args...) std.Print(AnsiBase + prefix + msg + AnsiReset) } @@ -5092,7 +5096,7 @@ func (s *Server) handleInitialize(req Request) { s.reply(req.ID, res, nil) } -func (s *Server) handleInitialized() { +func (s *Server) handleInitialized() { logging.Logf("lsp ", "client initialized") } @@ -5694,70 +5698,70 @@ type ServerOptions struct { PromptGoTestUser string } -func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) *Server { +func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) *Server { s := &Server{in: bufio.NewReader(r), out: w, logger: logger, docs: make(map[string]*document), logContext: opts.LogContext} maxTokens := opts.MaxTokens - if maxTokens <= 0 { + if maxTokens <= 0 { maxTokens = 500 } - s.maxTokens = maxTokens + s.maxTokens = maxTokens contextMode := opts.ContextMode - if contextMode == "" { + if contextMode == "" { contextMode = "file-on-new-func" } - windowLines := opts.WindowLines - if windowLines <= 0 { + windowLines := opts.WindowLines + if windowLines <= 0 { windowLines = 120 } - maxContextTokens := opts.MaxContextTokens - if maxContextTokens <= 0 { + maxContextTokens := opts.MaxContextTokens + if maxContextTokens <= 0 { maxContextTokens = 2000 } - s.contextMode = contextMode + s.contextMode = contextMode s.windowLines = windowLines s.maxContextTokens = maxContextTokens s.startTime = time.Now() s.llmClient = opts.Client - if len(opts.TriggerCharacters) == 0 { + if len(opts.TriggerCharacters) == 0 { // Defaults (no space to avoid auto-trigger after whitespace) s.triggerChars = []string{".", ":", "/", "_", ")", "{"} } else { s.triggerChars = append([]string{}, opts.TriggerCharacters...) } - s.codingTemperature = opts.CodingTemperature + s.codingTemperature = opts.CodingTemperature s.compCache = make(map[string]string) s.manualInvokeMinPrefix = opts.ManualInvokeMinPrefix if opts.CompletionDebounceMs > 0 { s.completionDebounce = time.Duration(opts.CompletionDebounceMs) * time.Millisecond } - if opts.CompletionThrottleMs > 0 { + if opts.CompletionThrottleMs > 0 { s.throttleInterval = time.Duration(opts.CompletionThrottleMs) * time.Millisecond } // Trigger character config (with sane defaults if missing) - if strings.TrimSpace(opts.InlineOpen) == "" { + if strings.TrimSpace(opts.InlineOpen) == "" { s.inlineOpen = ">" } else { s.inlineOpen = opts.InlineOpen } - if strings.TrimSpace(opts.InlineClose) == "" { + if strings.TrimSpace(opts.InlineClose) == "" { s.inlineClose = ">" } else { s.inlineClose = opts.InlineClose } - if strings.TrimSpace(opts.ChatSuffix) == "" { + if strings.TrimSpace(opts.ChatSuffix) == "" { s.chatSuffix = ">" } else { s.chatSuffix = opts.ChatSuffix } - if len(opts.ChatPrefixes) == 0 { + if len(opts.ChatPrefixes) == 0 { s.chatPrefixes = []string{"?", "!", ":", ";"} } else { s.chatPrefixes = append([]string{}, opts.ChatPrefixes...) } // Prompts - s.promptCompSysGeneral = opts.PromptCompSysGeneral + s.promptCompSysGeneral = opts.PromptCompSysGeneral s.promptCompSysParams = opts.PromptCompSysParams s.promptCompSysInline = opts.PromptCompSysInline s.promptCompUserGeneral = opts.PromptCompUserGeneral @@ -5775,20 +5779,20 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) s.promptGoTestUser = opts.PromptGoTestUser // Assign package-level inline trigger chars for free helper functions - if s.inlineOpen != "" { + if s.inlineOpen != "" { inlineOpenChar = s.inlineOpen[0] } - if s.inlineClose != "" { + if s.inlineClose != "" { inlineCloseChar = s.inlineClose[0] } - if s.chatSuffix != "" { + if s.chatSuffix != "" { chatSuffixChar = s.chatSuffix[0] } - if len(s.chatPrefixes) > 0 { + if len(s.chatPrefixes) > 0 { chatPrefixSingles = append([]string{}, s.chatPrefixes...) } // Initialize dispatch table - s.handlers = map[string]func(Request){ + s.handlers = map[string]func(Request){ "initialize": s.handleInitialize, "initialized": func(_ Request) { s.handleInitialized() }, "shutdown": s.handleShutdown, @@ -5801,7 +5805,7 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) "codeAction/resolve": s.handleCodeActionResolve, "workspace/executeCommand": s.handleExecuteCommand, } - return s + return s } func (s *Server) Run() error { @@ -5916,12 +5920,12 @@ func MultilineFunctionSuggestion() string { } // MarkdownCodeFence returns a fenced markdown snippet used in post-processing tests. -func MarkdownCodeFence() string { +func MarkdownCodeFence() string { return "```go\nname := value\n```" } // MalformedJSON returns a deliberately malformed JSON string. -func MalformedJSON() string { +func MalformedJSON() string { return "{\"choices\":[{\"delta\":{\"content\":\"oops\"}}]" } @@ -5931,51 +5935,51 @@ func MalformedJSON() string { import "strings" // RenderTemplate performs simple {{var}} replacement in a template string. -func RenderTemplate(t string, vars map[string]string) string { +func RenderTemplate(t string, vars map[string]string) string { if t == "" || len(vars) == 0 { return t } - out := t - for k, v := range vars { + out := t + for k, v := range vars { out = strings.ReplaceAll(out, "{{"+k+"}}", v) } - return out + return out } // StripCodeFences removes surrounding Markdown triple-backtick fences. -func StripCodeFences(s string) string { +func StripCodeFences(s string) string { t := strings.TrimSpace(s) if t == "" { return t } - lines := strings.Split(t, "\n") + lines := strings.Split(t, "\n") start := 0 for start < len(lines) && strings.TrimSpace(lines[start]) == "" { start++ } - end := len(lines) - 1 + end := len(lines) - 1 for end >= 0 && strings.TrimSpace(lines[end]) == "" { end-- } - if start >= len(lines) || end < 0 || start > end { + if start >= len(lines) || end < 0 || start > end { return t } - first := strings.TrimSpace(lines[start]) + first := strings.TrimSpace(lines[start]) last := strings.TrimSpace(lines[end]) if strings.HasPrefix(first, "```") && last == "```" && end > start { inner := strings.Join(lines[start+1:end], "\n") return inner } - return t + return t } // InstructionFromSelection extracts the first inline instruction and returns // (instruction, cleanedSelection). It detects markers on the earliest position // per line in precedence: strict ;text;, /* */, <!-- -->, //, #, --. -func InstructionFromSelection(sel string) (string, string) { +func InstructionFromSelection(sel string) (string, string) { lines := strings.Split(sel, "\n") - for idx, line := range lines { - if instr, cleaned, ok := FindFirstInstructionInLine(line); ok && strings.TrimSpace(instr) != "" { + for idx, line := range lines { + if instr, cleaned, ok := FindFirstInstructionInLine(line); ok && strings.TrimSpace(instr) != "" { lines[idx] = cleaned return instr, strings.Join(lines, "\n") } @@ -5984,13 +5988,13 @@ func InstructionFromSelection(sel string) (string, string) { +func FindFirstInstructionInLine(line string) (instr, cleaned string, ok bool) { type cand struct{ start, end int; text string } cands := []cand{} - if t, l, r, ok := FindStrictInlineTag(line); ok { + if t, l, r, ok := FindStrictInlineTag(line); ok { cands = append(cands, cand{start: l, end: r, text: t}) } - if i := strings.Index(line, "/*"); i >= 0 { + if i := strings.Index(line, "/*"); i >= 0 { if j := strings.Index(line[i+2:], "*/"); j >= 0 { start := i end := i + 2 + j + 2 @@ -5998,7 +6002,7 @@ func FindFirstInstructionInLine(line string) (instr, cleaned string, ok bool) } - if i := strings.Index(line, "<!--"); i >= 0 { + if i := strings.Index(line, "<!--"); i >= 0 { if j := strings.Index(line[i+4:], "-->"); j >= 0 { start := i end := i + 4 + j + 3 @@ -6006,34 +6010,34 @@ func FindFirstInstructionInLine(line string) (instr, cleaned string, ok bool) } - if i := strings.Index(line, "//"); i >= 0 { + if i := strings.Index(line, "//"); i >= 0 { cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+2:])}) } - if i := strings.Index(line, "#"); i >= 0 { + if i := strings.Index(line, "#"); i >= 0 { cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+1:])}) } - if i := strings.Index(line, "--"); i >= 0 { + if i := strings.Index(line, "--"); i >= 0 { cands = append(cands, cand{start: i, end: len(line), text: strings.TrimSpace(line[i+2:])}) } - if len(cands) == 0 { return "", line, false } - best := cands[0] + if len(cands) == 0 { return "", line, false } + best := cands[0] for _, c := range cands[1:] { if c.start >= 0 && (best.start < 0 || c.start < best.start) { best = c } } - cleaned = strings.TrimRight(line[:best.start]+line[best.end:], " \t") + cleaned = strings.TrimRight(line[:best.start]+line[best.end:], " \t") return best.text, cleaned, true } // FindStrictInlineTag finds ;text; with no spaces after/before semicolons. -func FindStrictInlineTag(line string) (text string, left, right int, ok bool) { - for i := 0; i < len(line); i++ { +func FindStrictInlineTag(line string) (text string, left, right int, ok bool) { + for i := 0; i < len(line); i++ { if line[i] != ';' { continue } - if i+1 < len(line) && line[i+1] == ' ' { continue } - for j := i + 1; j < len(line); j++ { - if line[j] == ';' { + if i+1 < len(line) && line[i+1] == ' ' { continue } + for j := i + 1; j < len(line); j++ { + if line[j] == ';' { if j-1 >= 0 && line[j-1] == ' ' { continue } - inner := strings.TrimSpace(line[i+1 : j]) - if inner != "" { return inner, i, j + 1, true } + inner := strings.TrimSpace(line[i+1 : j]) + if inner != "" { return inner, i, j + 1, true } } } } -- cgit v1.2.3