From 61a0ddfdfd266fa533abf6d3bb8f000f1214a2cd Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 20 Feb 2026 20:11:52 +0300 Subject: [PATCH] Fix: stop making http request per each keypress --- helpfuncs.go | 62 ++++++++++++++++++++++++++++++++++++++-------------- tui.go | 5 +++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/helpfuncs.go b/helpfuncs.go index 55df7c1..d8c28af 100644 --- a/helpfuncs.go +++ b/helpfuncs.go @@ -12,6 +12,7 @@ import ( "path/filepath" "slices" "strings" + "time" "unicode" "math/rand/v2" @@ -19,6 +20,46 @@ import ( "github.com/rivo/tview" ) +// Cached model color - updated by background goroutine +var cachedModelColor string = "orange" + +// startModelColorUpdater starts a background goroutine that periodically updates +// the cached model color. Only runs HTTP requests for local llama.cpp APIs. +func startModelColorUpdater() { + go func() { + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + + // Initial check + updateCachedModelColor() + + for range ticker.C { + updateCachedModelColor() + } + }() +} + +// updateCachedModelColor updates the global cachedModelColor variable +func updateCachedModelColor() { + if !isLocalLlamacpp() { + cachedModelColor = "orange" + return + } + + // Check if model is loaded + loaded, err := isModelLoaded(chatBody.Model) + if err != nil { + // On error, assume not loaded (red) + cachedModelColor = "red" + return + } + if loaded { + cachedModelColor = "green" + } else { + cachedModelColor = "red" + } +} + func isASCII(s string) bool { for i := 0; i < len(s); i++ { if s[i] > unicode.MaxASCII { @@ -132,8 +173,8 @@ func colorText() { } func updateStatusLine() { - statusLineWidget.SetText(makeStatusLine()) - helpView.SetText(fmt.Sprintf(helpText, makeStatusLine())) + status := makeStatusLine() + statusLineWidget.SetText(status) } func initSysCards() ([]string, error) { @@ -275,22 +316,11 @@ func isLocalLlamacpp() bool { return host == "localhost" || host == "127.0.0.1" || host == "::1" } -// getModelColor returns the color tag for the model name based on its load status. +// getModelColor returns the cached color tag for the model name. +// The cached value is updated by a background goroutine every 5 seconds. // For non-local models, returns orange. For local llama.cpp models, returns green if loaded, red if not. func getModelColor() string { - if !isLocalLlamacpp() { - return "orange" - } - // Check if model is loaded - loaded, err := isModelLoaded(chatBody.Model) - if err != nil { - // On error, assume not loaded (red) - return "red" - } - if loaded { - return "green" - } - return "red" + return cachedModelColor } func makeStatusLine() string { diff --git a/tui.go b/tui.go index 2c4fe67..b45f3e8 100644 --- a/tui.go +++ b/tui.go @@ -177,6 +177,9 @@ Press or 'x' to return ) func init() { + // Start background goroutine to update model color cache + startModelColorUpdater() + tview.Styles = colorschemes["default"] app = tview.NewApplication() pages = tview.NewPages() @@ -749,6 +752,8 @@ func init() { } if event.Key() == tcell.KeyF12 { // help window cheatsheet + // Update help text with current status before showing + helpView.SetText(fmt.Sprintf(helpText, makeStatusLine())) pages.AddPage(helpPage, helpView, true, true) return nil }