Files
VERSE dd1fb156fc feat(history): atuin history support (#134)
closes #91

3 mode config atuin history
```toml
[core]
# 0 = off, 1 = atuin history only, 2 = atuin history + default history
atuin-history = 0
```
Default is Mode 0 (turn off), who uses atuin history has to config it
manually

Mode 1 = atuin only (as you can see only 11 commands):
<img width="1285" height="466" alt="image"
src="https://github.com/user-attachments/assets/ca520ec7-fb45-4403-8087-a4951ba8dba0"
/>

Mode 2 = combine shell history + atuin history (prefer atuin):
<img width="1261" height="469" alt="image"
src="https://github.com/user-attachments/assets/1558ad3e-38f3-4d6e-bc8e-cfc00ea9cb4b"
/>

Mode 0 = turn off atuin

---
Custom atuin db path if anyone's using their own path (leave empty to
use default from atuin)
```toml
atuin-db-path = ""
```

---

I also handled the search menu overlay of Atuin, IRIS won't show in this
mode, but you have to change the keybind between 2 tool cuz they're
using same keybind Ctrl + R, but I think using IRIS make it unnecessary
to use Atuin search mode (82b180c9610f75d41008a54dfde474c7f2cce015)


<img width="70%" alt="image"
src="https://github.com/user-attachments/assets/e0c62050-7e6e-4e37-a1cc-2bfb63e1e984"
/>

<i><b>Before (Menu layout completely broken)</b></i>

<img width="50%" alt="image"
src="https://github.com/user-attachments/assets/1e6d8de9-cde8-473d-8a42-4748c480c812"
/>

<i ><b>After handling (not shows anymore)</b></i>
2026-08-09 21:08:11 +07:00

172 lines
4.1 KiB
Go

package root
import (
"context"
"sort"
"strings"
"sync"
"time"
"github.com/versenilvis/iris/integration"
"github.com/versenilvis/iris/internal/ai"
"github.com/versenilvis/iris/internal/config"
"github.com/versenilvis/iris/internal/logger"
"github.com/versenilvis/iris/internal/scoring"
"github.com/versenilvis/iris/spec"
)
// MergeResults collects and dedupes suggestions for a query and mode
func MergeResults(query string, mode string) []spec.Suggestion {
maxSugg := config.Get().UI.MaxSuggestions
seen := make(map[string]bool)
deduped := []spec.Suggestion{}
normalizedQuery := strings.TrimSpace(query)
// add suggestion helper to deduplicate
addSuggestion := func(s spec.Suggestion) {
normalizedCmd := strings.TrimSpace(s.Cmd)
if normalizedCmd == "" {
return
}
if s.Source != "alias" && normalizedCmd == normalizedQuery {
return
}
if s.Source == "" {
s.Source = "spec"
if s.Confidence == 0 {
s.Confidence = 50
}
}
if !seen[s.Cmd] {
seen[s.Cmd] = true
deduped = append(deduped, s)
}
}
// always call lookup to scan aliases and get spec suggestions
logger.Debugf("Merge Calling Lookup for '%s'", query)
cmdResults := spec.Lookup(query)
if mode == "history" {
aliases := spec.GetAliasesCopy()
histResults, _ := integration.SearchHistory(query, aliases)
// scale confidence based on recency (index in histResults) so the most recent commands stay on top
baseConf := 75
for i, h := range histResults {
conf := max(baseConf-(i*2), 60)
icon := "history"
if h.Source == "atuin" {
icon = "atuin"
}
addSuggestion(spec.Suggestion{
Cmd: h.Cmd,
Desc: h.Source,
Icon: icon,
Source: h.Source,
Confidence: conf,
})
}
}
for _, s := range cmdResults {
addSuggestion(s)
}
if mode == "history" && normalizedQuery == "" {
if len(deduped) > maxSugg {
return deduped[:maxSugg]
}
return deduped
}
injectAISuggestion(&deduped, seen, normalizedQuery)
var finalResults []spec.Suggestion
if mode == "history" {
sort.SliceStable(deduped, func(i, j int) bool {
return deduped[i].Confidence > deduped[j].Confidence
})
finalResults = deduped
} else {
cwd := spec.GetCWD()
tokens := spec.Tokenize(query)
rootCmd := ""
if len(tokens) > 0 {
rootCmd = tokens[0]
}
ctxTimeout, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
store, _ := scoring.GetFrecencyStore()
signals := scoring.CollectSignals(ctxTimeout, cwd, query, rootCmd, store, getPrevSkeleton())
scored := scoring.Score(deduped, signals)
finalResults = make([]spec.Suggestion, 0, len(scored))
for _, sc := range scored {
finalResults = append(finalResults, sc.Suggestion)
}
}
if len(finalResults) > maxSugg {
return finalResults[:maxSugg]
}
return finalResults
}
func injectAISuggestion(deduped *[]spec.Suggestion, seen map[string]bool, normalizedQuery string) {
if aiSugg := GetCurrentAISuggestion(); aiSugg != nil {
normalizedCmd := strings.TrimSpace(aiSugg.Cmd)
if normalizedCmd != "" && normalizedCmd != normalizedQuery && strings.HasPrefix(strings.ToLower(normalizedCmd), strings.ToLower(normalizedQuery)) {
if !seen[aiSugg.Cmd] {
seen[aiSugg.Cmd] = true
*deduped = append(*deduped, *aiSugg)
} else {
for i, item := range *deduped {
if item.Cmd == aiSugg.Cmd && aiSugg.Confidence > item.Confidence {
(*deduped)[i].Confidence = aiSugg.Confidence
if (*deduped)[i].Source == "" || (*deduped)[i].Source == "spec" || (*deduped)[i].Source == "history" {
(*deduped)[i].Source = "ai"
}
break
}
}
}
}
}
}
var (
aiEngine *ai.AIEngine
aiEngineOnce sync.Once
)
func GetAIEngine() *ai.AIEngine {
aiEngineOnce.Do(func() {
aiEngine = ai.NewAIEngine(nil)
for _, p := range ai.DefaultProviders {
aiEngine.RegisterProvider(p)
}
})
return aiEngine
}
var (
currentAISugg *spec.Suggestion
aiSuggMu sync.RWMutex
)
func SetCurrentAISuggestion(sugg *spec.Suggestion) {
aiSuggMu.Lock()
defer aiSuggMu.Unlock()
currentAISugg = sugg
}
func GetCurrentAISuggestion() *spec.Suggestion {
aiSuggMu.RLock()
defer aiSuggMu.RUnlock()
return currentAISugg
}