Fix: stop making http request per each keypress
This commit is contained in:
62
helpfuncs.go
62
helpfuncs.go
@@ -12,6 +12,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
@@ -19,6 +20,46 @@ import (
|
|||||||
"github.com/rivo/tview"
|
"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 {
|
func isASCII(s string) bool {
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
if s[i] > unicode.MaxASCII {
|
if s[i] > unicode.MaxASCII {
|
||||||
@@ -132,8 +173,8 @@ func colorText() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateStatusLine() {
|
func updateStatusLine() {
|
||||||
statusLineWidget.SetText(makeStatusLine())
|
status := makeStatusLine()
|
||||||
helpView.SetText(fmt.Sprintf(helpText, makeStatusLine()))
|
statusLineWidget.SetText(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initSysCards() ([]string, error) {
|
func initSysCards() ([]string, error) {
|
||||||
@@ -275,22 +316,11 @@ func isLocalLlamacpp() bool {
|
|||||||
return host == "localhost" || host == "127.0.0.1" || host == "::1"
|
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.
|
// For non-local models, returns orange. For local llama.cpp models, returns green if loaded, red if not.
|
||||||
func getModelColor() string {
|
func getModelColor() string {
|
||||||
if !isLocalLlamacpp() {
|
return cachedModelColor
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeStatusLine() string {
|
func makeStatusLine() string {
|
||||||
|
|||||||
5
tui.go
5
tui.go
@@ -177,6 +177,9 @@ Press <Enter> or 'x' to return
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
// Start background goroutine to update model color cache
|
||||||
|
startModelColorUpdater()
|
||||||
|
|
||||||
tview.Styles = colorschemes["default"]
|
tview.Styles = colorschemes["default"]
|
||||||
app = tview.NewApplication()
|
app = tview.NewApplication()
|
||||||
pages = tview.NewPages()
|
pages = tview.NewPages()
|
||||||
@@ -749,6 +752,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
if event.Key() == tcell.KeyF12 {
|
if event.Key() == tcell.KeyF12 {
|
||||||
// help window cheatsheet
|
// help window cheatsheet
|
||||||
|
// Update help text with current status before showing
|
||||||
|
helpView.SetText(fmt.Sprintf(helpText, makeStatusLine()))
|
||||||
pages.AddPage(helpPage, helpView, true, true)
|
pages.AddPage(helpPage, helpView, true, true)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user