Fix (race): mutex chatbody

This commit is contained in:
Grail Finder
2026-03-07 10:46:18 +03:00
parent 014e297ae3
commit a842b00e96
9 changed files with 422 additions and 142 deletions

View File

@@ -43,7 +43,7 @@ func updateCachedModelColor() {
return
}
// Check if model is loaded
loaded, err := isModelLoaded(chatBody.Model)
loaded, err := isModelLoaded(chatBody.GetModel())
if err != nil {
// On error, assume not loaded (red)
cachedModelColor = "red"
@@ -103,7 +103,7 @@ func refreshChatDisplay() {
viewingAs = cfg.WriteNextMsgAs
}
// Filter messages for this character
filteredMessages := filterMessagesForCharacter(chatBody.Messages, viewingAs)
filteredMessages := filterMessagesForCharacter(chatBody.GetMessages(), viewingAs)
displayText := chatToText(filteredMessages, cfg.ShowSys)
textView.SetText(displayText)
colorText()
@@ -217,8 +217,8 @@ func startNewChat(keepSysP bool) {
logger.Warn("no such sys msg", "name", cfg.AssistantRole)
}
// set chat body
chatBody.Messages = chatBody.Messages[:2]
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
chatBody.TruncateMessages(2)
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
newChat := &models.Chat{
ID: id + 1,
Name: fmt.Sprintf("%d_%s", id+1, cfg.AssistantRole),
@@ -370,7 +370,7 @@ func makeStatusLine() string {
// Get model color based on load status for local llama.cpp models
modelColor := getModelColor()
statusLine := fmt.Sprintf(statusLineTempl, activeChatName,
boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp],
boolColors[cfg.ToolUse], modelColor, chatBody.GetModel(), boolColors[cfg.SkipLLMResp],
cfg.CurrentAPI, persona, botPersona)
if cfg.STT_ENABLED {
recordingS := fmt.Sprintf(" | [%s:-:b]voice recording[-:-:-] (ctrl+r)",
@@ -396,11 +396,11 @@ func makeStatusLine() string {
}
func getContextTokens() int {
if chatBody == nil || chatBody.Messages == nil {
if chatBody == nil {
return 0
}
total := 0
messages := chatBody.Messages
messages := chatBody.GetMessages()
for i := range messages {
msg := &messages[i]
if msg.Stats != nil && msg.Stats.Tokens > 0 {
@@ -415,10 +415,10 @@ func getContextTokens() int {
const deepseekContext = 128000
func getMaxContextTokens() int {
if chatBody == nil || chatBody.Model == "" {
if chatBody == nil || chatBody.GetModel() == "" {
return 0
}
modelName := chatBody.Model
modelName := chatBody.GetModel()
switch {
case strings.Contains(cfg.CurrentAPI, "openrouter"):
if orModelsData != nil {
@@ -490,7 +490,7 @@ func listChatRoles() []string {
func deepseekModelValidator() error {
if cfg.CurrentAPI == cfg.DeepSeekChatAPI || cfg.CurrentAPI == cfg.DeepSeekCompletionAPI {
if chatBody.Model != "deepseek-chat" && chatBody.Model != "deepseek-reasoner" {
if chatBody.GetModel() != "deepseek-chat" && chatBody.GetModel() != "deepseek-reasoner" {
showToast("bad request", "wrong deepseek model name")
return nil
}
@@ -567,13 +567,13 @@ func executeCommandAndDisplay(cmdText string) {
outputContent := workingDir
// Add the command being executed to the chat
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), cfg.ToolRole, cmdText)
chatBody.GetMessageCount(), cfg.ToolRole, cmdText)
fmt.Fprintf(textView, "%s\n", outputContent)
combinedMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: "$ " + cmdText + "\n\n" + outputContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
chatBody.AppendMessage(combinedMsg)
if scrollToEndEnabled {
textView.ScrollToEnd()
}
@@ -582,13 +582,13 @@ func executeCommandAndDisplay(cmdText string) {
} else {
outputContent := "cd: " + newDir + ": No such file or directory"
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), cfg.ToolRole, cmdText)
chatBody.GetMessageCount(), cfg.ToolRole, cmdText)
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputContent)
combinedMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: "$ " + cmdText + "\n\n" + outputContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
chatBody.AppendMessage(combinedMsg)
if scrollToEndEnabled {
textView.ScrollToEnd()
}
@@ -604,7 +604,7 @@ func executeCommandAndDisplay(cmdText string) {
output, err := cmd.CombinedOutput()
// Add the command being executed to the chat
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), cfg.ToolRole, cmdText)
chatBody.GetMessageCount(), cfg.ToolRole, cmdText)
var outputContent string
if err != nil {
// Include both output and error
@@ -635,7 +635,7 @@ func executeCommandAndDisplay(cmdText string) {
Role: cfg.ToolRole,
Content: combinedContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
chatBody.AppendMessage(combinedMsg)
// Scroll to end and update colors
if scrollToEndEnabled {
textView.ScrollToEnd()
@@ -665,7 +665,7 @@ func performSearch(term string) {
searchResultLengths = nil
originalTextForSearch = ""
// Re-render text without highlights
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
colorText()
return
}