Enha: save/load message token stats

This commit is contained in:
Grail Finder
2026-02-27 11:23:03 +03:00
parent 915b029d2c
commit c855c30ae2
4 changed files with 30 additions and 20 deletions

2
bot.go
View File

@@ -784,7 +784,7 @@ func showSpinner() {
case toolRunningMode: case toolRunningMode:
textArea.SetTitle(spinners[spin] + " tool") textArea.SetTitle(spinners[spin] + " tool")
case botRespMode: case botRespMode:
textArea.SetTitle(spinners[spin] + " " + botPersona) textArea.SetTitle(spinners[spin] + " " + botPersona + " (F6 to interrupt)")
default: default:
textArea.SetTitle(spinners[spin] + " input") textArea.SetTitle(spinners[spin] + " input")
} }

View File

@@ -359,7 +359,7 @@ func makeStatusLine() string {
} }
// Get model color based on load status for local llama.cpp models // Get model color based on load status for local llama.cpp models
modelColor := getModelColor() modelColor := getModelColor()
statusLine := fmt.Sprintf(statusLineTempl, boolColors[botRespMode], activeChatName, statusLine := fmt.Sprintf(statusLineTempl, activeChatName,
boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp], boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp],
cfg.CurrentAPI, persona, botPersona) cfg.CurrentAPI, persona, botPersona)
if cfg.STT_ENABLED { if cfg.STT_ENABLED {

View File

@@ -16,7 +16,7 @@ var (
shellHistory []string shellHistory []string
shellHistoryPos int = -1 shellHistoryPos int = -1
thinkingCollapsed = false thinkingCollapsed = false
statusLineTempl = "help (F12) | [%s:-:b]llm writes[-:-:-] (F6 to interrupt) | chat: [orange:-:b]%s[-:-:-] (F1) | [%s:-:b]tool use[-:-:-] (ctrl+k) | model: [%s:-:b]%s[-:-:-] (ctrl+l) | [%s:-:b]skip LLM resp[-:-:-] (F10)\nAPI: [orange:-:b]%s[-:-:-] (ctrl+v) | writing as: [orange:-:b]%s[-:-:-] (ctrl+q) | bot will write as [orange:-:b]%s[-:-:-] (ctrl+x)" statusLineTempl = "help (F12) | chat: [orange:-:b]%s[-:-:-] (F1) | [%s:-:b]tool use[-:-:-] (ctrl+k) | model: [%s:-:b]%s[-:-:-] (ctrl+l) | [%s:-:b]skip LLM resp[-:-:-] (F10)\nAPI: [orange:-:b]%s[-:-:-] (ctrl+v) | writing as: [orange:-:b]%s[-:-:-] (ctrl+q) | bot will write as [orange:-:b]%s[-:-:-] (ctrl+x)"
focusSwitcher = map[tview.Primitive]tview.Primitive{} focusSwitcher = map[tview.Primitive]tview.Primitive{}
) )

View File

@@ -115,33 +115,39 @@ type RoleMsg struct {
} }
// MarshalJSON implements custom JSON marshaling for RoleMsg // MarshalJSON implements custom JSON marshaling for RoleMsg
func (m *RoleMsg) MarshalJSON() ([]byte, error) { //
//nolint:gocritic
func (m RoleMsg) MarshalJSON() ([]byte, error) {
if m.hasContentParts { if m.hasContentParts {
// Use structured content format // Use structured content format
aux := struct { aux := struct {
Role string `json:"role"` Role string `json:"role"`
Content []any `json:"content"` Content []any `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
}{ }{
Role: m.Role, Role: m.Role,
Content: m.ContentParts, Content: m.ContentParts,
ToolCallID: m.ToolCallID, ToolCallID: m.ToolCallID,
KnownTo: m.KnownTo, KnownTo: m.KnownTo,
Stats: m.Stats,
} }
return json.Marshal(aux) return json.Marshal(aux)
} else { } else {
// Use simple content format // Use simple content format
aux := struct { aux := struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"content"` Content string `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
}{ }{
Role: m.Role, Role: m.Role,
Content: m.Content, Content: m.Content,
ToolCallID: m.ToolCallID, ToolCallID: m.ToolCallID,
KnownTo: m.KnownTo, KnownTo: m.KnownTo,
Stats: m.Stats,
} }
return json.Marshal(aux) return json.Marshal(aux)
} }
@@ -151,26 +157,29 @@ func (m *RoleMsg) MarshalJSON() ([]byte, error) {
func (m *RoleMsg) UnmarshalJSON(data []byte) error { func (m *RoleMsg) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as structured content format // First, try to unmarshal as structured content format
var structured struct { var structured struct {
Role string `json:"role"` Role string `json:"role"`
Content []any `json:"content"` Content []any `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
} }
if err := json.Unmarshal(data, &structured); err == nil && len(structured.Content) > 0 { if err := json.Unmarshal(data, &structured); err == nil && len(structured.Content) > 0 {
m.Role = structured.Role m.Role = structured.Role
m.ContentParts = structured.Content m.ContentParts = structured.Content
m.ToolCallID = structured.ToolCallID m.ToolCallID = structured.ToolCallID
m.KnownTo = structured.KnownTo m.KnownTo = structured.KnownTo
m.Stats = structured.Stats
m.hasContentParts = true m.hasContentParts = true
return nil return nil
} }
// Otherwise, unmarshal as simple content format // Otherwise, unmarshal as simple content format
var simple struct { var simple struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"content"` Content string `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
} }
if err := json.Unmarshal(data, &simple); err != nil { if err := json.Unmarshal(data, &simple); err != nil {
return err return err
@@ -179,6 +188,7 @@ func (m *RoleMsg) UnmarshalJSON(data []byte) error {
m.Content = simple.Content m.Content = simple.Content
m.ToolCallID = simple.ToolCallID m.ToolCallID = simple.ToolCallID
m.KnownTo = simple.KnownTo m.KnownTo = simple.KnownTo
m.Stats = simple.Stats
m.hasContentParts = false m.hasContentParts = false
return nil return nil
} }