Compare commits
2 Commits
enha/save-
...
fix/datara
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c4d01ab3b | ||
|
|
a842b00e96 |
160
bot.go
160
bot.go
@@ -22,7 +22,7 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ var (
|
|||||||
chunkChan = make(chan string, 10)
|
chunkChan = make(chan string, 10)
|
||||||
openAIToolChan = make(chan string, 10)
|
openAIToolChan = make(chan string, 10)
|
||||||
streamDone = make(chan bool, 1)
|
streamDone = make(chan bool, 1)
|
||||||
chatBody *models.ChatBody
|
chatBody *models.SafeChatBody
|
||||||
store storage.FullRepo
|
store storage.FullRepo
|
||||||
defaultFirstMsg = "Hello! What can I do for you?"
|
defaultFirstMsg = "Hello! What can I do for you?"
|
||||||
defaultStarter = []models.RoleMsg{}
|
defaultStarter = []models.RoleMsg{}
|
||||||
@@ -49,7 +49,6 @@ var (
|
|||||||
//nolint:unused // TTS_ENABLED conditionally uses this
|
//nolint:unused // TTS_ENABLED conditionally uses this
|
||||||
orator Orator
|
orator Orator
|
||||||
asr STT
|
asr STT
|
||||||
localModelsMu sync.RWMutex
|
|
||||||
defaultLCPProps = map[string]float32{
|
defaultLCPProps = map[string]float32{
|
||||||
"temperature": 0.8,
|
"temperature": 0.8,
|
||||||
"dry_multiplier": 0.0,
|
"dry_multiplier": 0.0,
|
||||||
@@ -64,11 +63,17 @@ var (
|
|||||||
"google/gemma-3-27b-it:free",
|
"google/gemma-3-27b-it:free",
|
||||||
"meta-llama/llama-3.3-70b-instruct:free",
|
"meta-llama/llama-3.3-70b-instruct:free",
|
||||||
}
|
}
|
||||||
LocalModels = []string{}
|
LocalModels atomic.Value // stores []string
|
||||||
localModelsData *models.LCPModels
|
localModelsData atomic.Value // stores *models.LCPModels
|
||||||
orModelsData *models.ORModels
|
orModelsData atomic.Value // stores *models.ORModels
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
LocalModels.Store([]string{})
|
||||||
|
localModelsData.Store((*models.LCPModels)(nil))
|
||||||
|
orModelsData.Store((*models.ORModels)(nil))
|
||||||
|
}
|
||||||
|
|
||||||
var thinkBlockRE = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
var thinkBlockRE = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
||||||
|
|
||||||
// parseKnownToTag extracts known_to list from content using configured tag.
|
// parseKnownToTag extracts known_to list from content using configured tag.
|
||||||
@@ -262,13 +267,13 @@ func warmUpModel() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Check if model is already loaded
|
// Check if model is already loaded
|
||||||
loaded, err := isModelLoaded(chatBody.Model)
|
loaded, err := isModelLoaded(chatBody.GetModel())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug("failed to check model status", "model", chatBody.Model, "error", err)
|
logger.Debug("failed to check model status", "model", chatBody.GetModel(), "error", err)
|
||||||
// Continue with warmup attempt anyway
|
// Continue with warmup attempt anyway
|
||||||
}
|
}
|
||||||
if loaded {
|
if loaded {
|
||||||
showToast("model already loaded", "Model "+chatBody.Model+" is already loaded.")
|
showToast("model already loaded", "Model "+chatBody.GetModel()+" is already loaded.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
@@ -277,7 +282,7 @@ func warmUpModel() {
|
|||||||
switch {
|
switch {
|
||||||
case strings.HasSuffix(cfg.CurrentAPI, "/completion"):
|
case strings.HasSuffix(cfg.CurrentAPI, "/completion"):
|
||||||
// Old completion endpoint
|
// Old completion endpoint
|
||||||
req := models.NewLCPReq(".", chatBody.Model, nil, map[string]float32{
|
req := models.NewLCPReq(".", chatBody.GetModel(), nil, map[string]float32{
|
||||||
"temperature": 0.8,
|
"temperature": 0.8,
|
||||||
"dry_multiplier": 0.0,
|
"dry_multiplier": 0.0,
|
||||||
"min_p": 0.05,
|
"min_p": 0.05,
|
||||||
@@ -289,7 +294,7 @@ func warmUpModel() {
|
|||||||
// OpenAI-compatible chat endpoint
|
// OpenAI-compatible chat endpoint
|
||||||
req := models.OpenAIReq{
|
req := models.OpenAIReq{
|
||||||
ChatBody: &models.ChatBody{
|
ChatBody: &models.ChatBody{
|
||||||
Model: chatBody.Model,
|
Model: chatBody.GetModel(),
|
||||||
Messages: []models.RoleMsg{
|
Messages: []models.RoleMsg{
|
||||||
{Role: "system", Content: "."},
|
{Role: "system", Content: "."},
|
||||||
},
|
},
|
||||||
@@ -313,7 +318,7 @@ func warmUpModel() {
|
|||||||
}
|
}
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
// Start monitoring for model load completion
|
// Start monitoring for model load completion
|
||||||
monitorModelLoad(chatBody.Model)
|
monitorModelLoad(chatBody.GetModel())
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,7 +361,7 @@ func fetchORModels(free bool) ([]string, error) {
|
|||||||
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
orModelsData = data
|
orModelsData.Store(data)
|
||||||
freeModels := data.ListModels(free)
|
freeModels := data.ListModels(free)
|
||||||
return freeModels, nil
|
return freeModels, nil
|
||||||
}
|
}
|
||||||
@@ -418,7 +423,7 @@ func fetchLCPModelsWithStatus() (*models.LCPModels, error) {
|
|||||||
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
localModelsData = data
|
localModelsData.Store(data)
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -821,10 +826,10 @@ func chatRound(r *models.ChatRoundReq) error {
|
|||||||
}
|
}
|
||||||
go sendMsgToLLM(reader)
|
go sendMsgToLLM(reader)
|
||||||
logger.Debug("looking at vars in chatRound", "msg", r.UserMsg, "regen", r.Regen, "resume", r.Resume)
|
logger.Debug("looking at vars in chatRound", "msg", r.UserMsg, "regen", r.Regen, "resume", r.Resume)
|
||||||
msgIdx := len(chatBody.Messages)
|
msgIdx := chatBody.GetMessageCount()
|
||||||
if !r.Resume {
|
if !r.Resume {
|
||||||
// Add empty message to chatBody immediately so it persists during Alt+T toggle
|
// Add empty message to chatBody immediately so it persists during Alt+T toggle
|
||||||
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{
|
chatBody.AppendMessage(models.RoleMsg{
|
||||||
Role: botPersona, Content: "",
|
Role: botPersona, Content: "",
|
||||||
})
|
})
|
||||||
nl := "\n\n"
|
nl := "\n\n"
|
||||||
@@ -836,7 +841,7 @@ func chatRound(r *models.ChatRoundReq) error {
|
|||||||
}
|
}
|
||||||
fmt.Fprintf(textView, "%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
|
fmt.Fprintf(textView, "%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
|
||||||
} else {
|
} else {
|
||||||
msgIdx = len(chatBody.Messages) - 1
|
msgIdx = chatBody.GetMessageCount() - 1
|
||||||
}
|
}
|
||||||
respText := strings.Builder{}
|
respText := strings.Builder{}
|
||||||
toolResp := strings.Builder{}
|
toolResp := strings.Builder{}
|
||||||
@@ -893,7 +898,10 @@ out:
|
|||||||
fmt.Fprint(textView, chunk)
|
fmt.Fprint(textView, chunk)
|
||||||
respText.WriteString(chunk)
|
respText.WriteString(chunk)
|
||||||
// Update the message in chatBody.Messages so it persists during Alt+T
|
// Update the message in chatBody.Messages so it persists during Alt+T
|
||||||
chatBody.Messages[msgIdx].Content = respText.String()
|
chatBody.UpdateMessageFunc(msgIdx, func(msg models.RoleMsg) models.RoleMsg {
|
||||||
|
msg.Content = respText.String()
|
||||||
|
return msg
|
||||||
|
})
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
}
|
}
|
||||||
@@ -936,29 +944,32 @@ out:
|
|||||||
}
|
}
|
||||||
botRespMode = false
|
botRespMode = false
|
||||||
if r.Resume {
|
if r.Resume {
|
||||||
chatBody.Messages[len(chatBody.Messages)-1].Content += respText.String()
|
chatBody.UpdateMessageFunc(chatBody.GetMessageCount()-1, func(msg models.RoleMsg) models.RoleMsg {
|
||||||
updatedMsg := chatBody.Messages[len(chatBody.Messages)-1]
|
msg.Content += respText.String()
|
||||||
processedMsg := processMessageTag(&updatedMsg)
|
processedMsg := processMessageTag(&msg)
|
||||||
chatBody.Messages[len(chatBody.Messages)-1] = *processedMsg
|
if msgStats != nil && processedMsg.Role != cfg.ToolRole {
|
||||||
if msgStats != nil && chatBody.Messages[len(chatBody.Messages)-1].Role != cfg.ToolRole {
|
processedMsg.Stats = msgStats
|
||||||
chatBody.Messages[len(chatBody.Messages)-1].Stats = msgStats
|
}
|
||||||
}
|
return *processedMsg
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
chatBody.Messages[msgIdx].Content = respText.String()
|
chatBody.UpdateMessageFunc(msgIdx, func(msg models.RoleMsg) models.RoleMsg {
|
||||||
processedMsg := processMessageTag(&chatBody.Messages[msgIdx])
|
msg.Content = respText.String()
|
||||||
chatBody.Messages[msgIdx] = *processedMsg
|
processedMsg := processMessageTag(&msg)
|
||||||
if msgStats != nil && chatBody.Messages[msgIdx].Role != cfg.ToolRole {
|
if msgStats != nil && processedMsg.Role != cfg.ToolRole {
|
||||||
chatBody.Messages[msgIdx].Stats = msgStats
|
processedMsg.Stats = msgStats
|
||||||
}
|
}
|
||||||
stopTTSIfNotForUser(&chatBody.Messages[msgIdx])
|
return *processedMsg
|
||||||
|
})
|
||||||
|
stopTTSIfNotForUser(&chatBody.GetMessages()[msgIdx])
|
||||||
}
|
}
|
||||||
cleanChatBody()
|
cleanChatBody()
|
||||||
refreshChatDisplay()
|
refreshChatDisplay()
|
||||||
updateStatusLine()
|
updateStatusLine()
|
||||||
// bot msg is done;
|
// bot msg is done;
|
||||||
// now check it for func call
|
// now check it for func call
|
||||||
// logChat(activeChatName, chatBody.Messages)
|
// logChat(activeChatName, chatBody.GetMessages())
|
||||||
if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil {
|
if err := updateStorageChat(activeChatName, chatBody.GetMessages()); err != nil {
|
||||||
logger.Warn("failed to update storage", "error", err, "name", activeChatName)
|
logger.Warn("failed to update storage", "error", err, "name", activeChatName)
|
||||||
}
|
}
|
||||||
// Strip think blocks before parsing for tool calls
|
// Strip think blocks before parsing for tool calls
|
||||||
@@ -973,8 +984,8 @@ out:
|
|||||||
// If so, trigger those characters to respond if that char is not controlled by user
|
// If so, trigger those characters to respond if that char is not controlled by user
|
||||||
// perhaps we should have narrator role to determine which char is next to act
|
// perhaps we should have narrator role to determine which char is next to act
|
||||||
if cfg.AutoTurn {
|
if cfg.AutoTurn {
|
||||||
lastMsg := chatBody.Messages[len(chatBody.Messages)-1]
|
lastMsg, ok := chatBody.GetLastMessage()
|
||||||
if len(lastMsg.KnownTo) > 0 {
|
if ok && len(lastMsg.KnownTo) > 0 {
|
||||||
triggerPrivateMessageResponses(&lastMsg)
|
triggerPrivateMessageResponses(&lastMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -983,13 +994,15 @@ out:
|
|||||||
|
|
||||||
// cleanChatBody removes messages with null or empty content to prevent API issues
|
// cleanChatBody removes messages with null or empty content to prevent API issues
|
||||||
func cleanChatBody() {
|
func cleanChatBody() {
|
||||||
if chatBody == nil || chatBody.Messages == nil {
|
if chatBody == nil || chatBody.GetMessageCount() == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Tool request cleaning is now configurable via AutoCleanToolCallsFromCtx (default false)
|
// Tool request cleaning is now configurable via AutoCleanToolCallsFromCtx (default false)
|
||||||
// /completion msg where part meant for user and other part tool call
|
// /completion msg where part meant for user and other part tool call
|
||||||
// chatBody.Messages = cleanToolCalls(chatBody.Messages)
|
// chatBody.Messages = cleanToolCalls(chatBody.Messages)
|
||||||
chatBody.Messages = consolidateAssistantMessages(chatBody.Messages)
|
chatBody.WithLock(func(cb *models.ChatBody) {
|
||||||
|
cb.Messages = consolidateAssistantMessages(cb.Messages)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertJSONToMapStringString unmarshals JSON into map[string]interface{} and converts all values to strings.
|
// convertJSONToMapStringString unmarshals JSON into map[string]interface{} and converts all values to strings.
|
||||||
@@ -1089,7 +1102,7 @@ func findCall(msg, toolCall string) bool {
|
|||||||
Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err),
|
Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err),
|
||||||
ToolCallID: lastToolCall.ID, // Use the stored tool call ID
|
ToolCallID: lastToolCall.ID, // Use the stored tool call ID
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
|
chatBody.AppendMessage(toolResponseMsg)
|
||||||
// Clear the stored tool call ID after using it (no longer needed)
|
// Clear the stored tool call ID after using it (no longer needed)
|
||||||
// Trigger the assistant to continue processing with the error message
|
// Trigger the assistant to continue processing with the error message
|
||||||
crr := &models.ChatRoundReq{
|
crr := &models.ChatRoundReq{
|
||||||
@@ -1126,7 +1139,7 @@ func findCall(msg, toolCall string) bool {
|
|||||||
Role: cfg.ToolRole,
|
Role: cfg.ToolRole,
|
||||||
Content: "Error processing tool call: no valid JSON found. Please check the JSON format.",
|
Content: "Error processing tool call: no valid JSON found. Please check the JSON format.",
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
|
chatBody.AppendMessage(toolResponseMsg)
|
||||||
crr := &models.ChatRoundReq{
|
crr := &models.ChatRoundReq{
|
||||||
Role: cfg.AssistantRole,
|
Role: cfg.AssistantRole,
|
||||||
}
|
}
|
||||||
@@ -1143,8 +1156,8 @@ func findCall(msg, toolCall string) bool {
|
|||||||
Role: cfg.ToolRole,
|
Role: cfg.ToolRole,
|
||||||
Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err),
|
Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err),
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
|
chatBody.AppendMessage(toolResponseMsg)
|
||||||
logger.Debug("findCall: added tool error response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "message_count_after_add", len(chatBody.Messages))
|
logger.Debug("findCall: added tool error response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "message_count_after_add", chatBody.GetMessageCount())
|
||||||
// Trigger the assistant to continue processing with the error message
|
// Trigger the assistant to continue processing with the error message
|
||||||
// chatRound("", cfg.AssistantRole, tv, false, false)
|
// chatRound("", cfg.AssistantRole, tv, false, false)
|
||||||
crr := &models.ChatRoundReq{
|
crr := &models.ChatRoundReq{
|
||||||
@@ -1162,17 +1175,23 @@ func findCall(msg, toolCall string) bool {
|
|||||||
// we got here => last msg recognized as a tool call (correct or not)
|
// we got here => last msg recognized as a tool call (correct or not)
|
||||||
// Use the tool call ID from streaming response (lastToolCall.ID)
|
// Use the tool call ID from streaming response (lastToolCall.ID)
|
||||||
// Don't generate random ID - the ID should match between assistant message and tool response
|
// Don't generate random ID - the ID should match between assistant message and tool response
|
||||||
lastMsgIdx := len(chatBody.Messages) - 1
|
lastMsgIdx := chatBody.GetMessageCount() - 1
|
||||||
if lastToolCall.ID != "" {
|
if lastToolCall.ID != "" {
|
||||||
chatBody.Messages[lastMsgIdx].ToolCallID = lastToolCall.ID
|
chatBody.UpdateMessageFunc(lastMsgIdx, func(msg models.RoleMsg) models.RoleMsg {
|
||||||
|
msg.ToolCallID = lastToolCall.ID
|
||||||
|
return msg
|
||||||
|
})
|
||||||
}
|
}
|
||||||
// Store tool call info in the assistant message
|
// Store tool call info in the assistant message
|
||||||
// Convert Args map to JSON string for storage
|
// Convert Args map to JSON string for storage
|
||||||
chatBody.Messages[lastMsgIdx].ToolCall = &models.ToolCall{
|
chatBody.UpdateMessageFunc(lastMsgIdx, func(msg models.RoleMsg) models.RoleMsg {
|
||||||
ID: lastToolCall.ID,
|
msg.ToolCall = &models.ToolCall{
|
||||||
Name: lastToolCall.Name,
|
ID: lastToolCall.ID,
|
||||||
Args: mapToString(lastToolCall.Args),
|
Name: lastToolCall.Name,
|
||||||
}
|
Args: mapToString(lastToolCall.Args),
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
})
|
||||||
// call a func
|
// call a func
|
||||||
_, ok := fnMap[fc.Name]
|
_, ok := fnMap[fc.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -1183,8 +1202,8 @@ func findCall(msg, toolCall string) bool {
|
|||||||
Content: m,
|
Content: m,
|
||||||
ToolCallID: lastToolCall.ID, // Use the stored tool call ID
|
ToolCallID: lastToolCall.ID, // Use the stored tool call ID
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
|
chatBody.AppendMessage(toolResponseMsg)
|
||||||
logger.Debug("findCall: added tool not implemented response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID, "message_count_after_add", len(chatBody.Messages))
|
logger.Debug("findCall: added tool not implemented response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID, "message_count_after_add", chatBody.GetMessageCount())
|
||||||
// Clear the stored tool call ID after using it
|
// Clear the stored tool call ID after using it
|
||||||
lastToolCall.ID = ""
|
lastToolCall.ID = ""
|
||||||
// Trigger the assistant to continue processing with the new tool response
|
// Trigger the assistant to continue processing with the new tool response
|
||||||
@@ -1255,9 +1274,9 @@ func findCall(msg, toolCall string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
|
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
|
||||||
"\n\n", len(chatBody.Messages), cfg.ToolRole, toolResponseMsg.GetText())
|
"\n\n", chatBody.GetMessageCount(), cfg.ToolRole, toolResponseMsg.GetText())
|
||||||
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
|
chatBody.AppendMessage(toolResponseMsg)
|
||||||
logger.Debug("findCall: added actual tool response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID, "message_count_after_add", len(chatBody.Messages))
|
logger.Debug("findCall: added actual tool response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID, "message_count_after_add", chatBody.GetMessageCount())
|
||||||
// Clear the stored tool call ID after using it
|
// Clear the stored tool call ID after using it
|
||||||
lastToolCall.ID = ""
|
lastToolCall.ID = ""
|
||||||
// Trigger the assistant to continue processing with the new tool response
|
// Trigger the assistant to continue processing with the new tool response
|
||||||
@@ -1387,7 +1406,7 @@ func charToStart(agentName string, keepSysP bool) bool {
|
|||||||
func updateModelLists() {
|
func updateModelLists() {
|
||||||
var err error
|
var err error
|
||||||
if cfg.OpenRouterToken != "" {
|
if cfg.OpenRouterToken != "" {
|
||||||
ORFreeModels, err = fetchORModels(true)
|
_, err := fetchORModels(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("failed to fetch or models", "error", err)
|
logger.Warn("failed to fetch or models", "error", err)
|
||||||
}
|
}
|
||||||
@@ -1397,22 +1416,19 @@ func updateModelLists() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("failed to fetch llama.cpp models", "error", err)
|
logger.Warn("failed to fetch llama.cpp models", "error", err)
|
||||||
}
|
}
|
||||||
localModelsMu.Lock()
|
LocalModels.Store(ml)
|
||||||
LocalModels = ml
|
|
||||||
localModelsMu.Unlock()
|
|
||||||
for statusLineWidget == nil {
|
for statusLineWidget == nil {
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
}
|
}
|
||||||
// set already loaded model in llama.cpp
|
// set already loaded model in llama.cpp
|
||||||
if strings.Contains(cfg.CurrentAPI, "localhost") || strings.Contains(cfg.CurrentAPI, "127.0.0.1") {
|
if strings.Contains(cfg.CurrentAPI, "localhost") || strings.Contains(cfg.CurrentAPI, "127.0.0.1") {
|
||||||
localModelsMu.Lock()
|
modelList := LocalModels.Load().([]string)
|
||||||
defer localModelsMu.Unlock()
|
for i := range modelList {
|
||||||
for i := range LocalModels {
|
if strings.Contains(modelList[i], models.LoadedMark) {
|
||||||
if strings.Contains(LocalModels[i], models.LoadedMark) {
|
m := strings.TrimPrefix(modelList[i], models.LoadedMark)
|
||||||
m := strings.TrimPrefix(LocalModels[i], models.LoadedMark)
|
|
||||||
cfg.CurrentModel = m
|
cfg.CurrentModel = m
|
||||||
chatBody.Model = m
|
chatBody.Model = m
|
||||||
cachedModelColor = "green"
|
cachedModelColor.Store("green")
|
||||||
updateStatusLine()
|
updateStatusLine()
|
||||||
updateToolCapabilities()
|
updateToolCapabilities()
|
||||||
app.Draw()
|
app.Draw()
|
||||||
@@ -1423,21 +1439,17 @@ func updateModelLists() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func refreshLocalModelsIfEmpty() {
|
func refreshLocalModelsIfEmpty() {
|
||||||
localModelsMu.RLock()
|
models := LocalModels.Load().([]string)
|
||||||
if len(LocalModels) > 0 {
|
if len(models) > 0 {
|
||||||
localModelsMu.RUnlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
localModelsMu.RUnlock()
|
|
||||||
// try to fetch
|
// try to fetch
|
||||||
models, err := fetchLCPModels()
|
models, err := fetchLCPModels()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("failed to fetch llama.cpp models", "error", err)
|
logger.Warn("failed to fetch llama.cpp models", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
localModelsMu.Lock()
|
LocalModels.Store(models)
|
||||||
LocalModels = models
|
|
||||||
localModelsMu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func summarizeAndStartNewChat() {
|
func summarizeAndStartNewChat() {
|
||||||
@@ -1497,7 +1509,7 @@ func init() {
|
|||||||
// load cards
|
// load cards
|
||||||
basicCard.Role = cfg.AssistantRole
|
basicCard.Role = cfg.AssistantRole
|
||||||
logLevel.Set(slog.LevelInfo)
|
logLevel.Set(slog.LevelInfo)
|
||||||
logger = slog.New(slog.NewTextHandler(logfile, &slog.HandlerOptions{Level: logLevel}))
|
logger = slog.New(slog.NewTextHandler(logfile, &slog.HandlerOptions{Level: logLevel, AddSource: true}))
|
||||||
store = storage.NewProviderSQL(cfg.DBPATH, logger)
|
store = storage.NewProviderSQL(cfg.DBPATH, logger)
|
||||||
if store == nil {
|
if store == nil {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -1521,11 +1533,11 @@ func init() {
|
|||||||
}
|
}
|
||||||
lastToolCall = &models.FuncCall{}
|
lastToolCall = &models.FuncCall{}
|
||||||
lastChat := loadOldChatOrGetNew()
|
lastChat := loadOldChatOrGetNew()
|
||||||
chatBody = &models.ChatBody{
|
chatBody = models.NewSafeChatBody(&models.ChatBody{
|
||||||
Model: "modelname",
|
Model: "modelname",
|
||||||
Stream: true,
|
Stream: true,
|
||||||
Messages: lastChat,
|
Messages: lastChat,
|
||||||
}
|
})
|
||||||
choseChunkParser()
|
choseChunkParser()
|
||||||
httpClient = createClient(time.Second * 90)
|
httpClient = createClient(time.Second * 90)
|
||||||
if cfg.TTS_ENABLED {
|
if cfg.TTS_ENABLED {
|
||||||
|
|||||||
112
helpfuncs.go
112
helpfuncs.go
@@ -16,11 +16,17 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cached model color - updated by background goroutine
|
// Cached model color - updated by background goroutine
|
||||||
var cachedModelColor string = "orange"
|
var cachedModelColor atomic.Value // stores string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cachedModelColor.Store("orange")
|
||||||
|
}
|
||||||
|
|
||||||
// startModelColorUpdater starts a background goroutine that periodically updates
|
// startModelColorUpdater starts a background goroutine that periodically updates
|
||||||
// the cached model color. Only runs HTTP requests for local llama.cpp APIs.
|
// the cached model color. Only runs HTTP requests for local llama.cpp APIs.
|
||||||
@@ -39,20 +45,20 @@ func startModelColorUpdater() {
|
|||||||
// updateCachedModelColor updates the global cachedModelColor variable
|
// updateCachedModelColor updates the global cachedModelColor variable
|
||||||
func updateCachedModelColor() {
|
func updateCachedModelColor() {
|
||||||
if !isLocalLlamacpp() {
|
if !isLocalLlamacpp() {
|
||||||
cachedModelColor = "orange"
|
cachedModelColor.Store("orange")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Check if model is loaded
|
// Check if model is loaded
|
||||||
loaded, err := isModelLoaded(chatBody.Model)
|
loaded, err := isModelLoaded(chatBody.GetModel())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// On error, assume not loaded (red)
|
// On error, assume not loaded (red)
|
||||||
cachedModelColor = "red"
|
cachedModelColor.Store("red")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if loaded {
|
if loaded {
|
||||||
cachedModelColor = "green"
|
cachedModelColor.Store("green")
|
||||||
} else {
|
} else {
|
||||||
cachedModelColor = "red"
|
cachedModelColor.Store("red")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +109,7 @@ func refreshChatDisplay() {
|
|||||||
viewingAs = cfg.WriteNextMsgAs
|
viewingAs = cfg.WriteNextMsgAs
|
||||||
}
|
}
|
||||||
// Filter messages for this character
|
// Filter messages for this character
|
||||||
filteredMessages := filterMessagesForCharacter(chatBody.Messages, viewingAs)
|
filteredMessages := filterMessagesForCharacter(chatBody.GetMessages(), viewingAs)
|
||||||
displayText := chatToText(filteredMessages, cfg.ShowSys)
|
displayText := chatToText(filteredMessages, cfg.ShowSys)
|
||||||
textView.SetText(displayText)
|
textView.SetText(displayText)
|
||||||
colorText()
|
colorText()
|
||||||
@@ -217,8 +223,8 @@ func startNewChat(keepSysP bool) {
|
|||||||
logger.Warn("no such sys msg", "name", cfg.AssistantRole)
|
logger.Warn("no such sys msg", "name", cfg.AssistantRole)
|
||||||
}
|
}
|
||||||
// set chat body
|
// set chat body
|
||||||
chatBody.Messages = chatBody.Messages[:2]
|
chatBody.TruncateMessages(2)
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
newChat := &models.Chat{
|
newChat := &models.Chat{
|
||||||
ID: id + 1,
|
ID: id + 1,
|
||||||
Name: fmt.Sprintf("%d_%s", id+1, cfg.AssistantRole),
|
Name: fmt.Sprintf("%d_%s", id+1, cfg.AssistantRole),
|
||||||
@@ -335,7 +341,7 @@ func isLocalLlamacpp() bool {
|
|||||||
// The cached value is updated by a background goroutine every 5 seconds.
|
// 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 {
|
||||||
return cachedModelColor
|
return cachedModelColor.Load().(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeStatusLine() string {
|
func makeStatusLine() string {
|
||||||
@@ -370,7 +376,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, activeChatName,
|
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)
|
cfg.CurrentAPI, persona, botPersona)
|
||||||
if cfg.STT_ENABLED {
|
if cfg.STT_ENABLED {
|
||||||
recordingS := fmt.Sprintf(" | [%s:-:b]voice recording[-:-:-] (ctrl+r)",
|
recordingS := fmt.Sprintf(" | [%s:-:b]voice recording[-:-:-] (ctrl+r)",
|
||||||
@@ -396,11 +402,11 @@ func makeStatusLine() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getContextTokens() int {
|
func getContextTokens() int {
|
||||||
if chatBody == nil || chatBody.Messages == nil {
|
if chatBody == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
total := 0
|
total := 0
|
||||||
messages := chatBody.Messages
|
messages := chatBody.GetMessages()
|
||||||
for i := range messages {
|
for i := range messages {
|
||||||
msg := &messages[i]
|
msg := &messages[i]
|
||||||
if msg.Stats != nil && msg.Stats.Tokens > 0 {
|
if msg.Stats != nil && msg.Stats.Tokens > 0 {
|
||||||
@@ -415,46 +421,54 @@ func getContextTokens() int {
|
|||||||
const deepseekContext = 128000
|
const deepseekContext = 128000
|
||||||
|
|
||||||
func getMaxContextTokens() int {
|
func getMaxContextTokens() int {
|
||||||
if chatBody == nil || chatBody.Model == "" {
|
if chatBody == nil || chatBody.GetModel() == "" {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
modelName := chatBody.Model
|
modelName := chatBody.GetModel()
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(cfg.CurrentAPI, "openrouter"):
|
case strings.Contains(cfg.CurrentAPI, "openrouter"):
|
||||||
if orModelsData != nil {
|
ord := orModelsData.Load()
|
||||||
for i := range orModelsData.Data {
|
if ord != nil {
|
||||||
m := &orModelsData.Data[i]
|
data := ord.(*models.ORModels)
|
||||||
if m.ID == modelName {
|
if data != nil {
|
||||||
return m.ContextLength
|
for i := range data.Data {
|
||||||
|
m := &data.Data[i]
|
||||||
|
if m.ID == modelName {
|
||||||
|
return m.ContextLength
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case strings.Contains(cfg.CurrentAPI, "deepseek"):
|
case strings.Contains(cfg.CurrentAPI, "deepseek"):
|
||||||
return deepseekContext
|
return deepseekContext
|
||||||
default:
|
default:
|
||||||
if localModelsData != nil {
|
lmd := localModelsData.Load()
|
||||||
for i := range localModelsData.Data {
|
if lmd != nil {
|
||||||
m := &localModelsData.Data[i]
|
data := lmd.(*models.LCPModels)
|
||||||
if m.ID == modelName {
|
if data != nil {
|
||||||
for _, arg := range m.Status.Args {
|
for i := range data.Data {
|
||||||
if strings.HasPrefix(arg, "--ctx-size") {
|
m := &data.Data[i]
|
||||||
if strings.Contains(arg, "=") {
|
if m.ID == modelName {
|
||||||
val := strings.Split(arg, "=")[1]
|
for _, arg := range m.Status.Args {
|
||||||
if n, err := strconv.Atoi(val); err == nil {
|
if strings.HasPrefix(arg, "--ctx-size") {
|
||||||
return n
|
if strings.Contains(arg, "=") {
|
||||||
}
|
val := strings.Split(arg, "=")[1]
|
||||||
} else {
|
if n, err := strconv.Atoi(val); err == nil {
|
||||||
idx := -1
|
|
||||||
for j, a := range m.Status.Args {
|
|
||||||
if a == "--ctx-size" && j+1 < len(m.Status.Args) {
|
|
||||||
idx = j + 1
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if idx != -1 {
|
|
||||||
if n, err := strconv.Atoi(m.Status.Args[idx]); err == nil {
|
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
idx := -1
|
||||||
|
for j, a := range m.Status.Args {
|
||||||
|
if a == "--ctx-size" && j+1 < len(m.Status.Args) {
|
||||||
|
idx = j + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idx != -1 {
|
||||||
|
if n, err := strconv.Atoi(m.Status.Args[idx]); err == nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -490,7 +504,7 @@ func listChatRoles() []string {
|
|||||||
|
|
||||||
func deepseekModelValidator() error {
|
func deepseekModelValidator() error {
|
||||||
if cfg.CurrentAPI == cfg.DeepSeekChatAPI || cfg.CurrentAPI == cfg.DeepSeekCompletionAPI {
|
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")
|
showToast("bad request", "wrong deepseek model name")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -567,13 +581,13 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
outputContent := workingDir
|
outputContent := workingDir
|
||||||
// Add the command being executed to the chat
|
// Add the command being executed to the chat
|
||||||
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
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)
|
fmt.Fprintf(textView, "%s\n", outputContent)
|
||||||
combinedMsg := models.RoleMsg{
|
combinedMsg := models.RoleMsg{
|
||||||
Role: cfg.ToolRole,
|
Role: cfg.ToolRole,
|
||||||
Content: "$ " + cmdText + "\n\n" + outputContent,
|
Content: "$ " + cmdText + "\n\n" + outputContent,
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, combinedMsg)
|
chatBody.AppendMessage(combinedMsg)
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
}
|
}
|
||||||
@@ -582,13 +596,13 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
} else {
|
} else {
|
||||||
outputContent := "cd: " + newDir + ": No such file or directory"
|
outputContent := "cd: " + newDir + ": No such file or directory"
|
||||||
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
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)
|
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputContent)
|
||||||
combinedMsg := models.RoleMsg{
|
combinedMsg := models.RoleMsg{
|
||||||
Role: cfg.ToolRole,
|
Role: cfg.ToolRole,
|
||||||
Content: "$ " + cmdText + "\n\n" + outputContent,
|
Content: "$ " + cmdText + "\n\n" + outputContent,
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, combinedMsg)
|
chatBody.AppendMessage(combinedMsg)
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
}
|
}
|
||||||
@@ -604,7 +618,7 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
// Add the command being executed to the chat
|
// Add the command being executed to the chat
|
||||||
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
|
||||||
len(chatBody.Messages), cfg.ToolRole, cmdText)
|
chatBody.GetMessageCount(), cfg.ToolRole, cmdText)
|
||||||
var outputContent string
|
var outputContent string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Include both output and error
|
// Include both output and error
|
||||||
@@ -635,7 +649,7 @@ func executeCommandAndDisplay(cmdText string) {
|
|||||||
Role: cfg.ToolRole,
|
Role: cfg.ToolRole,
|
||||||
Content: combinedContent,
|
Content: combinedContent,
|
||||||
}
|
}
|
||||||
chatBody.Messages = append(chatBody.Messages, combinedMsg)
|
chatBody.AppendMessage(combinedMsg)
|
||||||
// Scroll to end and update colors
|
// Scroll to end and update colors
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
@@ -665,7 +679,7 @@ func performSearch(term string) {
|
|||||||
searchResultLengths = nil
|
searchResultLengths = nil
|
||||||
originalTextForSearch = ""
|
originalTextForSearch = ""
|
||||||
// Re-render text without highlights
|
// Re-render text without highlights
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
55
llm.go
55
llm.go
@@ -13,8 +13,9 @@ var lastImg string // for ctrl+j
|
|||||||
|
|
||||||
// containsToolSysMsg checks if the toolSysMsg already exists in the chat body
|
// containsToolSysMsg checks if the toolSysMsg already exists in the chat body
|
||||||
func containsToolSysMsg() bool {
|
func containsToolSysMsg() bool {
|
||||||
for i := range chatBody.Messages {
|
messages := chatBody.GetMessages()
|
||||||
if chatBody.Messages[i].Role == cfg.ToolRole && chatBody.Messages[i].Content == toolSysMsg {
|
for i := range messages {
|
||||||
|
if messages[i].Role == cfg.ToolRole && messages[i].Content == toolSysMsg {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,13 +136,13 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
newMsg = models.RoleMsg{Role: role, Content: msg}
|
newMsg = models.RoleMsg{Role: role, Content: msg}
|
||||||
}
|
}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
}
|
}
|
||||||
// sending description of the tools and how to use them
|
// sending description of the tools and how to use them
|
||||||
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
||||||
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
chatBody.AppendMessage(models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
||||||
}
|
}
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
// Build prompt and extract images inline as we process each message
|
// Build prompt and extract images inline as we process each message
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
@@ -183,7 +184,7 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
}
|
}
|
||||||
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
||||||
"msg", msg, "resume", resume, "prompt", prompt, "multimodal_data_count", len(multimodalData))
|
"msg", msg, "resume", resume, "prompt", prompt, "multimodal_data_count", len(multimodalData))
|
||||||
payload := models.NewLCPReq(prompt, chatBody.Model, multimodalData,
|
payload := models.NewLCPReq(prompt, chatBody.GetModel(), multimodalData,
|
||||||
defaultLCPProps, chatBody.MakeStopSliceExcluding("", listChatRoles()))
|
defaultLCPProps, chatBody.MakeStopSliceExcluding("", listChatRoles()))
|
||||||
data, err := json.Marshal(payload)
|
data, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -289,17 +290,17 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
|
|||||||
newMsg = models.NewRoleMsg(role, msg)
|
newMsg = models.NewRoleMsg(role, msg)
|
||||||
}
|
}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
logger.Debug("LCPChat FormMsg: added message to chatBody", "role", newMsg.Role,
|
logger.Debug("LCPChat FormMsg: added message to chatBody", "role", newMsg.Role,
|
||||||
"content_len", len(newMsg.Content), "message_count_after_add", len(chatBody.Messages))
|
"content_len", len(newMsg.Content), "message_count_after_add", chatBody.GetMessageCount())
|
||||||
}
|
}
|
||||||
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
// openai /v1/chat does not support custom roles; needs to be user, assistant, system
|
// openai /v1/chat does not support custom roles; needs to be user, assistant, system
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.GetModel(),
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.GetStream(),
|
||||||
}
|
}
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
||||||
@@ -375,13 +376,13 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
if msg != "" { // otherwise let the bot to continue
|
if msg != "" { // otherwise let the bot to continue
|
||||||
newMsg := models.RoleMsg{Role: role, Content: msg}
|
newMsg := models.RoleMsg{Role: role, Content: msg}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
}
|
}
|
||||||
// sending description of the tools and how to use them
|
// sending description of the tools and how to use them
|
||||||
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
||||||
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
chatBody.AppendMessage(models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
||||||
}
|
}
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
|
messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
|
||||||
@@ -394,7 +395,7 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
}
|
}
|
||||||
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
||||||
"msg", msg, "resume", resume, "prompt", prompt)
|
"msg", msg, "resume", resume, "prompt", prompt)
|
||||||
payload := models.NewDSCompletionReq(prompt, chatBody.Model,
|
payload := models.NewDSCompletionReq(prompt, chatBody.GetModel(),
|
||||||
defaultLCPProps["temp"],
|
defaultLCPProps["temp"],
|
||||||
chatBody.MakeStopSliceExcluding("", listChatRoles()))
|
chatBody.MakeStopSliceExcluding("", listChatRoles()))
|
||||||
data, err := json.Marshal(payload)
|
data, err := json.Marshal(payload)
|
||||||
@@ -448,15 +449,15 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
if msg != "" { // otherwise let the bot continue
|
if msg != "" { // otherwise let the bot continue
|
||||||
newMsg := models.RoleMsg{Role: role, Content: msg}
|
newMsg := models.RoleMsg{Role: role, Content: msg}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
}
|
}
|
||||||
// Create copy of chat body with standardized user role
|
// Create copy of chat body with standardized user role
|
||||||
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.GetModel(),
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.GetStream(),
|
||||||
}
|
}
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
||||||
@@ -527,13 +528,13 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
if msg != "" { // otherwise let the bot to continue
|
if msg != "" { // otherwise let the bot to continue
|
||||||
newMsg := models.RoleMsg{Role: role, Content: msg}
|
newMsg := models.RoleMsg{Role: role, Content: msg}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
}
|
}
|
||||||
// sending description of the tools and how to use them
|
// sending description of the tools and how to use them
|
||||||
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() {
|
||||||
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
chatBody.AppendMessage(models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
|
||||||
}
|
}
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
|
messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
|
||||||
@@ -547,7 +548,7 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
stopSlice := chatBody.MakeStopSliceExcluding("", listChatRoles())
|
stopSlice := chatBody.MakeStopSliceExcluding("", listChatRoles())
|
||||||
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
|
||||||
"msg", msg, "resume", resume, "prompt", prompt, "stop_strings", stopSlice)
|
"msg", msg, "resume", resume, "prompt", prompt, "stop_strings", stopSlice)
|
||||||
payload := models.NewOpenRouterCompletionReq(chatBody.Model, prompt,
|
payload := models.NewOpenRouterCompletionReq(chatBody.GetModel(), prompt,
|
||||||
defaultLCPProps, stopSlice)
|
defaultLCPProps, stopSlice)
|
||||||
data, err := json.Marshal(payload)
|
data, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -633,15 +634,15 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
newMsg = models.NewRoleMsg(role, msg)
|
newMsg = models.NewRoleMsg(role, msg)
|
||||||
}
|
}
|
||||||
newMsg = *processMessageTag(&newMsg)
|
newMsg = *processMessageTag(&newMsg)
|
||||||
chatBody.Messages = append(chatBody.Messages, newMsg)
|
chatBody.AppendMessage(newMsg)
|
||||||
}
|
}
|
||||||
// Create copy of chat body with standardized user role
|
// Create copy of chat body with standardized user role
|
||||||
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.GetMessages())
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.GetModel(),
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.GetStream(),
|
||||||
}
|
}
|
||||||
for i := range filteredMessages {
|
for i := range filteredMessages {
|
||||||
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
|
||||||
|
|||||||
251
models/models.go
251
models/models.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FuncCall struct {
|
type FuncCall struct {
|
||||||
@@ -639,3 +640,253 @@ type MultimodalToolResp struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Parts []map[string]string `json:"parts"`
|
Parts []map[string]string `json:"parts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SafeChatBody is a thread-safe wrapper around ChatBody using RWMutex.
|
||||||
|
// This allows safe concurrent access to chat state from multiple goroutines.
|
||||||
|
type SafeChatBody struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
ChatBody
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSafeChatBody creates a new SafeChatBody from an existing ChatBody.
|
||||||
|
// If cb is nil, creates an empty ChatBody.
|
||||||
|
func NewSafeChatBody(cb *ChatBody) *SafeChatBody {
|
||||||
|
if cb == nil {
|
||||||
|
return &SafeChatBody{
|
||||||
|
ChatBody: ChatBody{
|
||||||
|
Messages: []RoleMsg{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &SafeChatBody{
|
||||||
|
ChatBody: *cb,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModel returns the model name (thread-safe read).
|
||||||
|
func (s *SafeChatBody) GetModel() string {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetModel sets the model name (thread-safe write).
|
||||||
|
func (s *SafeChatBody) SetModel(model string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.Model = model
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStream returns the stream flag (thread-safe read).
|
||||||
|
func (s *SafeChatBody) GetStream() bool {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.Stream
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStream sets the stream flag (thread-safe write).
|
||||||
|
func (s *SafeChatBody) SetStream(stream bool) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.Stream = stream
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMessages returns a copy of all messages (thread-safe read).
|
||||||
|
// Returns a copy to prevent race conditions after the lock is released.
|
||||||
|
func (s *SafeChatBody) GetMessages() []RoleMsg {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
// Return a copy to prevent external modification
|
||||||
|
messagesCopy := make([]RoleMsg, len(s.Messages))
|
||||||
|
copy(messagesCopy, s.Messages)
|
||||||
|
return messagesCopy
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMessages replaces all messages (thread-safe write).
|
||||||
|
func (s *SafeChatBody) SetMessages(messages []RoleMsg) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.Messages = messages
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendMessage adds a message to the end (thread-safe write).
|
||||||
|
func (s *SafeChatBody) AppendMessage(msg RoleMsg) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.Messages = append(s.Messages, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMessageAt returns a message at a specific index (thread-safe read).
|
||||||
|
// Returns the message and a boolean indicating if the index was valid.
|
||||||
|
func (s *SafeChatBody) GetMessageAt(index int) (RoleMsg, bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
if index < 0 || index >= len(s.Messages) {
|
||||||
|
return RoleMsg{}, false
|
||||||
|
}
|
||||||
|
return s.Messages[index], true
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMessageAt updates a message at a specific index (thread-safe write).
|
||||||
|
// Returns false if index is out of bounds.
|
||||||
|
func (s *SafeChatBody) SetMessageAt(index int, msg RoleMsg) bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if index < 0 || index >= len(s.Messages) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s.Messages[index] = msg
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastMessage returns the last message (thread-safe read).
|
||||||
|
// Returns the message and a boolean indicating if the chat has messages.
|
||||||
|
func (s *SafeChatBody) GetLastMessage() (RoleMsg, bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
if len(s.Messages) == 0 {
|
||||||
|
return RoleMsg{}, false
|
||||||
|
}
|
||||||
|
return s.Messages[len(s.Messages)-1], true
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMessageCount returns the number of messages (thread-safe read).
|
||||||
|
func (s *SafeChatBody) GetMessageCount() int {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return len(s.Messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveLastMessage removes the last message (thread-safe write).
|
||||||
|
// Returns false if there are no messages.
|
||||||
|
func (s *SafeChatBody) RemoveLastMessage() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if len(s.Messages) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s.Messages = s.Messages[:len(s.Messages)-1]
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// TruncateMessages keeps only the first n messages (thread-safe write).
|
||||||
|
func (s *SafeChatBody) TruncateMessages(n int) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if n < len(s.Messages) {
|
||||||
|
s.Messages = s.Messages[:n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearMessages removes all messages (thread-safe write).
|
||||||
|
func (s *SafeChatBody) ClearMessages() {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.Messages = []RoleMsg{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rename renames all occurrences of oldname to newname in messages (thread-safe read-modify-write).
|
||||||
|
func (s *SafeChatBody) Rename(oldname, newname string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
for i := range s.Messages {
|
||||||
|
s.Messages[i].Content = strings.ReplaceAll(s.Messages[i].Content, oldname, newname)
|
||||||
|
s.Messages[i].Role = strings.ReplaceAll(s.Messages[i].Role, oldname, newname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListRoles returns all unique roles in messages (thread-safe read).
|
||||||
|
func (s *SafeChatBody) ListRoles() []string {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
namesMap := make(map[string]struct{})
|
||||||
|
for i := range s.Messages {
|
||||||
|
namesMap[s.Messages[i].Role] = struct{}{}
|
||||||
|
}
|
||||||
|
resp := make([]string, len(namesMap))
|
||||||
|
i := 0
|
||||||
|
for k := range namesMap {
|
||||||
|
resp[i] = k
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeStopSlice returns stop strings for all roles (thread-safe read).
|
||||||
|
func (s *SafeChatBody) MakeStopSlice() []string {
|
||||||
|
return s.MakeStopSliceExcluding("", s.ListRoles())
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeStopSliceExcluding returns stop strings excluding a specific role (thread-safe read).
|
||||||
|
func (s *SafeChatBody) MakeStopSliceExcluding(excludeRole string, roleList []string) []string {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
ss := []string{}
|
||||||
|
for _, role := range roleList {
|
||||||
|
if role == excludeRole {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ss = append(ss,
|
||||||
|
role+":\n",
|
||||||
|
role+":",
|
||||||
|
role+": ",
|
||||||
|
role+": ",
|
||||||
|
role+": \n",
|
||||||
|
role+": ",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return ss
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateMessageFunc updates a message at index using a provided function.
|
||||||
|
// The function receives the current message and returns the updated message.
|
||||||
|
// This is atomic and thread-safe (read-modify-write under single lock).
|
||||||
|
// Returns false if index is out of bounds.
|
||||||
|
func (s *SafeChatBody) UpdateMessageFunc(index int, updater func(RoleMsg) RoleMsg) bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if index < 0 || index >= len(s.Messages) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s.Messages[index] = updater(s.Messages[index])
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendMessageFunc appends a new message created by a provided function.
|
||||||
|
// The function receives the current message count and returns the new message.
|
||||||
|
// This is atomic and thread-safe.
|
||||||
|
func (s *SafeChatBody) AppendMessageFunc(creator func(count int) RoleMsg) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
msg := creator(len(s.Messages))
|
||||||
|
s.Messages = append(s.Messages, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMessagesForLLM returns a filtered copy of messages for sending to LLM.
|
||||||
|
// This is thread-safe and returns a copy safe for external modification.
|
||||||
|
func (s *SafeChatBody) GetMessagesForLLM(filterFunc func([]RoleMsg) []RoleMsg) []RoleMsg {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
if filterFunc == nil {
|
||||||
|
messagesCopy := make([]RoleMsg, len(s.Messages))
|
||||||
|
copy(messagesCopy, s.Messages)
|
||||||
|
return messagesCopy
|
||||||
|
}
|
||||||
|
return filterFunc(s.Messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLock executes a function while holding the write lock.
|
||||||
|
// Use this for complex operations that need to be atomic.
|
||||||
|
func (s *SafeChatBody) WithLock(fn func(*ChatBody)) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
fn(&s.ChatBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRLock executes a function while holding the read lock.
|
||||||
|
// Use this for complex read-only operations.
|
||||||
|
func (s *SafeChatBody) WithRLock(fn func(*ChatBody)) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
fn(&s.ChatBody)
|
||||||
|
}
|
||||||
|
|||||||
23
popups.go
23
popups.go
@@ -22,7 +22,7 @@ func showModelSelectionPopup() {
|
|||||||
models, err := fetchLCPModelsWithLoadStatus()
|
models, err := fetchLCPModelsWithLoadStatus()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("failed to fetch models with load status", "error", err)
|
logger.Error("failed to fetch models with load status", "error", err)
|
||||||
return LocalModels
|
return LocalModels.Load().([]string)
|
||||||
}
|
}
|
||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,8 @@ func showModelSelectionPopup() {
|
|||||||
modelList := getModelListForAPI(cfg.CurrentAPI)
|
modelList := getModelListForAPI(cfg.CurrentAPI)
|
||||||
// Check for empty options list
|
// Check for empty options list
|
||||||
if len(modelList) == 0 {
|
if len(modelList) == 0 {
|
||||||
logger.Warn("empty model list for", "api", cfg.CurrentAPI, "localModelsLen", len(LocalModels), "orModelsLen", len(ORFreeModels))
|
localModels := LocalModels.Load().([]string)
|
||||||
|
logger.Warn("empty model list for", "api", cfg.CurrentAPI, "localModelsLen", len(localModels), "orModelsLen", len(ORFreeModels))
|
||||||
var message string
|
var message string
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(cfg.CurrentAPI, "openrouter.ai"):
|
case strings.Contains(cfg.CurrentAPI, "openrouter.ai"):
|
||||||
@@ -50,7 +51,7 @@ func showModelSelectionPopup() {
|
|||||||
// Find the current model index to set as selected
|
// Find the current model index to set as selected
|
||||||
currentModelIndex := -1
|
currentModelIndex := -1
|
||||||
for i, model := range modelList {
|
for i, model := range modelList {
|
||||||
if strings.TrimPrefix(model, models.LoadedMark) == chatBody.Model {
|
if strings.TrimPrefix(model, models.LoadedMark) == chatBody.GetModel() {
|
||||||
currentModelIndex = i
|
currentModelIndex = i
|
||||||
}
|
}
|
||||||
modelListWidget.AddItem(model, "", 0, nil)
|
modelListWidget.AddItem(model, "", 0, nil)
|
||||||
@@ -61,8 +62,8 @@ func showModelSelectionPopup() {
|
|||||||
}
|
}
|
||||||
modelListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
|
modelListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
|
||||||
modelName := strings.TrimPrefix(mainText, models.LoadedMark)
|
modelName := strings.TrimPrefix(mainText, models.LoadedMark)
|
||||||
chatBody.Model = modelName
|
chatBody.SetModel(modelName)
|
||||||
cfg.CurrentModel = chatBody.Model
|
cfg.CurrentModel = chatBody.GetModel()
|
||||||
pages.RemovePage("modelSelectionPopup")
|
pages.RemovePage("modelSelectionPopup")
|
||||||
app.SetFocus(textArea)
|
app.SetFocus(textArea)
|
||||||
updateCachedModelColor()
|
updateCachedModelColor()
|
||||||
@@ -150,15 +151,13 @@ func showAPILinkSelectionPopup() {
|
|||||||
}
|
}
|
||||||
// Assume local llama.cpp
|
// Assume local llama.cpp
|
||||||
refreshLocalModelsIfEmpty()
|
refreshLocalModelsIfEmpty()
|
||||||
localModelsMu.RLock()
|
return LocalModels.Load().([]string)
|
||||||
defer localModelsMu.RUnlock()
|
|
||||||
return LocalModels
|
|
||||||
}
|
}
|
||||||
newModelList := getModelListForAPI(cfg.CurrentAPI)
|
newModelList := getModelListForAPI(cfg.CurrentAPI)
|
||||||
// Ensure chatBody.Model is in the new list; if not, set to first available model
|
// Ensure chatBody.Model is in the new list; if not, set to first available model
|
||||||
if len(newModelList) > 0 && !slices.Contains(newModelList, chatBody.Model) {
|
if len(newModelList) > 0 && !slices.Contains(newModelList, chatBody.GetModel()) {
|
||||||
chatBody.Model = strings.TrimPrefix(newModelList[0], models.LoadedMark)
|
chatBody.SetModel(strings.TrimPrefix(newModelList[0], models.LoadedMark))
|
||||||
cfg.CurrentModel = chatBody.Model
|
cfg.CurrentModel = chatBody.GetModel()
|
||||||
updateToolCapabilities()
|
updateToolCapabilities()
|
||||||
}
|
}
|
||||||
pages.RemovePage("apiLinkSelectionPopup")
|
pages.RemovePage("apiLinkSelectionPopup")
|
||||||
@@ -229,7 +228,7 @@ func showUserRoleSelectionPopup() {
|
|||||||
// Update the user role in config
|
// Update the user role in config
|
||||||
cfg.WriteNextMsgAs = mainText
|
cfg.WriteNextMsgAs = mainText
|
||||||
// role got switch, update textview with character specific context for user
|
// role got switch, update textview with character specific context for user
|
||||||
filtered := filterMessagesForCharacter(chatBody.Messages, mainText)
|
filtered := filterMessagesForCharacter(chatBody.GetMessages(), mainText)
|
||||||
textView.SetText(chatToText(filtered, cfg.ShowSys))
|
textView.SetText(chatToText(filtered, cfg.ShowSys))
|
||||||
// Remove the popup page
|
// Remove the popup page
|
||||||
pages.RemovePage("userRoleSelectionPopup")
|
pages.RemovePage("userRoleSelectionPopup")
|
||||||
|
|||||||
@@ -4,14 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = sync.RWMutex{}
|
|
||||||
|
|
||||||
// Define constants for cell types
|
// Define constants for cell types
|
||||||
const (
|
const (
|
||||||
CellTypeCheckbox = "checkbox"
|
CellTypeCheckbox = "checkbox"
|
||||||
@@ -157,9 +154,7 @@ func makePropsTable(props map[string]float32) *tview.Table {
|
|||||||
}
|
}
|
||||||
// Assume local llama.cpp
|
// Assume local llama.cpp
|
||||||
refreshLocalModelsIfEmpty()
|
refreshLocalModelsIfEmpty()
|
||||||
localModelsMu.RLock()
|
return LocalModels.Load().([]string)
|
||||||
defer localModelsMu.RUnlock()
|
|
||||||
return LocalModels
|
|
||||||
}
|
}
|
||||||
// Add input fields
|
// Add input fields
|
||||||
addInputRow("New char to write msg as", "", func(text string) {
|
addInputRow("New char to write msg as", "", func(text string) {
|
||||||
@@ -262,7 +257,8 @@ func makePropsTable(props map[string]float32) *tview.Table {
|
|||||||
|
|
||||||
// Check for empty options list
|
// Check for empty options list
|
||||||
if len(data.Options) == 0 {
|
if len(data.Options) == 0 {
|
||||||
logger.Warn("empty options list for", "label", label, "api", cfg.CurrentAPI, "localModelsLen", len(LocalModels), "orModelsLen", len(ORFreeModels))
|
localModels := LocalModels.Load().([]string)
|
||||||
|
logger.Warn("empty options list for", "label", label, "api", cfg.CurrentAPI, "localModelsLen", len(localModels), "orModelsLen", len(ORFreeModels))
|
||||||
message := "No options available for " + label
|
message := "No options available for " + label
|
||||||
if label == "Select a model" {
|
if label == "Select a model" {
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func historyToSJSON(msgs []models.RoleMsg) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func exportChat() error {
|
func exportChat() error {
|
||||||
data, err := json.MarshalIndent(chatBody.Messages, "", " ")
|
data, err := json.MarshalIndent(chatBody.GetMessages(), "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ func importChat(filename string) error {
|
|||||||
if _, ok := chatMap[activeChatName]; !ok {
|
if _, ok := chatMap[activeChatName]; !ok {
|
||||||
addNewChat(activeChatName)
|
addNewChat(activeChatName)
|
||||||
}
|
}
|
||||||
chatBody.Messages = messages
|
chatBody.SetMessages(messages)
|
||||||
cfg.AssistantRole = messages[1].Role
|
cfg.AssistantRole = messages[1].Role
|
||||||
if cfg.AssistantRole == cfg.UserRole {
|
if cfg.AssistantRole == cfg.UserRole {
|
||||||
cfg.AssistantRole = messages[2].Role
|
cfg.AssistantRole = messages[2].Role
|
||||||
|
|||||||
30
tables.go
30
tables.go
@@ -128,8 +128,8 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
|
|||||||
pages.RemovePage(historyPage)
|
pages.RemovePage(historyPage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
chatBody.Messages = history
|
chatBody.SetMessages(history)
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
activeChatName = selectedChat
|
activeChatName = selectedChat
|
||||||
pages.RemovePage(historyPage)
|
pages.RemovePage(historyPage)
|
||||||
return
|
return
|
||||||
@@ -149,8 +149,8 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
|
|||||||
}
|
}
|
||||||
showToast("chat deleted", selectedChat+" was deleted")
|
showToast("chat deleted", selectedChat+" was deleted")
|
||||||
// load last chat
|
// load last chat
|
||||||
chatBody.Messages = loadOldChatOrGetNew()
|
chatBody.SetMessages(loadOldChatOrGetNew())
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
pages.RemovePage(historyPage)
|
pages.RemovePage(historyPage)
|
||||||
return
|
return
|
||||||
case "update card":
|
case "update card":
|
||||||
@@ -163,16 +163,24 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
|
|||||||
showToast("error", "no such card: "+agentName)
|
showToast("error", "no such card: "+agentName)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cc.SysPrompt = chatBody.Messages[0].Content
|
if msg0, ok := chatBody.GetMessageAt(0); ok {
|
||||||
cc.FirstMsg = chatBody.Messages[1].Content
|
cc.SysPrompt = msg0.Content
|
||||||
|
}
|
||||||
|
if msg1, ok := chatBody.GetMessageAt(1); ok {
|
||||||
|
cc.FirstMsg = msg1.Content
|
||||||
|
}
|
||||||
if err := pngmeta.WriteToPng(cc.ToSpec(cfg.UserRole), cc.FilePath, cc.FilePath); err != nil {
|
if err := pngmeta.WriteToPng(cc.ToSpec(cfg.UserRole), cc.FilePath, cc.FilePath); err != nil {
|
||||||
logger.Error("failed to write charcard", "error", err)
|
logger.Error("failed to write charcard", "error", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
case "move sysprompt onto 1st msg":
|
case "move sysprompt onto 1st msg":
|
||||||
chatBody.Messages[1].Content = chatBody.Messages[0].Content + chatBody.Messages[1].Content
|
chatBody.WithLock(func(cb *models.ChatBody) {
|
||||||
chatBody.Messages[0].Content = rpDefenitionSysMsg
|
if len(cb.Messages) >= 2 {
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
cb.Messages[1].Content = cb.Messages[0].Content + cb.Messages[1].Content
|
||||||
|
cb.Messages[0].Content = rpDefenitionSysMsg
|
||||||
|
}
|
||||||
|
})
|
||||||
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
activeChatName = selectedChat
|
activeChatName = selectedChat
|
||||||
pages.RemovePage(historyPage)
|
pages.RemovePage(historyPage)
|
||||||
return
|
return
|
||||||
@@ -563,7 +571,7 @@ func makeAgentTable(agentList []string) *tview.Table {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// replace textview
|
// replace textview
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
updateStatusLine()
|
updateStatusLine()
|
||||||
// sysModal.ClearButtons()
|
// sysModal.ClearButtons()
|
||||||
@@ -732,7 +740,7 @@ func makeImportChatTable(filenames []string) *tview.Table {
|
|||||||
colorText()
|
colorText()
|
||||||
updateStatusLine()
|
updateStatusLine()
|
||||||
// redraw the text in text area
|
// redraw the text in text area
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
pages.RemovePage(historyPage)
|
pages.RemovePage(historyPage)
|
||||||
app.SetFocus(textArea)
|
app.SetFocus(textArea)
|
||||||
return
|
return
|
||||||
|
|||||||
4
tools.go
4
tools.go
@@ -1215,11 +1215,11 @@ func isCommandAllowed(command string, args ...string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func summarizeChat(args map[string]string) []byte {
|
func summarizeChat(args map[string]string) []byte {
|
||||||
if len(chatBody.Messages) == 0 {
|
if chatBody.GetMessageCount() == 0 {
|
||||||
return []byte("No chat history to summarize.")
|
return []byte("No chat history to summarize.")
|
||||||
}
|
}
|
||||||
// Format chat history for the agent
|
// Format chat history for the agent
|
||||||
chatText := chatToText(chatBody.Messages, true) // include system and tool messages
|
chatText := chatToText(chatBody.GetMessages(), true) // include system and tool messages
|
||||||
return []byte(chatText)
|
return []byte(chatText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
56
tui.go
56
tui.go
@@ -355,7 +355,7 @@ func init() {
|
|||||||
searchResults = nil // Clear search results
|
searchResults = nil // Clear search results
|
||||||
searchResultLengths = nil // Clear search result lengths
|
searchResultLengths = nil // Clear search result lengths
|
||||||
originalTextForSearch = ""
|
originalTextForSearch = ""
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) // Reset text without search regions
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys)) // Reset text without search regions
|
||||||
colorText() // Apply normal chat coloring
|
colorText() // Apply normal chat coloring
|
||||||
} else {
|
} else {
|
||||||
// Original logic if no search is active
|
// Original logic if no search is active
|
||||||
@@ -436,9 +436,11 @@ func init() {
|
|||||||
pages.RemovePage(editMsgPage)
|
pages.RemovePage(editMsgPage)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
chatBody.Messages[selectedIndex].SetText(editedMsg)
|
chatBody.WithLock(func(cb *models.ChatBody) {
|
||||||
|
cb.Messages[selectedIndex].SetText(editedMsg)
|
||||||
|
})
|
||||||
// change textarea
|
// change textarea
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
pages.RemovePage(editMsgPage)
|
pages.RemovePage(editMsgPage)
|
||||||
editMode = false
|
editMode = false
|
||||||
return nil
|
return nil
|
||||||
@@ -466,9 +468,11 @@ func init() {
|
|||||||
pages.RemovePage(roleEditPage)
|
pages.RemovePage(roleEditPage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if selectedIndex >= 0 && selectedIndex < len(chatBody.Messages) {
|
if selectedIndex >= 0 && selectedIndex < chatBody.GetMessageCount() {
|
||||||
chatBody.Messages[selectedIndex].Role = newRole
|
chatBody.WithLock(func(cb *models.ChatBody) {
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
cb.Messages[selectedIndex].Role = newRole
|
||||||
|
})
|
||||||
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
pages.RemovePage(roleEditPage)
|
pages.RemovePage(roleEditPage)
|
||||||
}
|
}
|
||||||
@@ -497,7 +501,7 @@ func init() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
selectedIndex = siInt
|
selectedIndex = siInt
|
||||||
if len(chatBody.Messages)-1 < selectedIndex || selectedIndex < 0 {
|
if chatBody.GetMessageCount()-1 < selectedIndex || selectedIndex < 0 {
|
||||||
msg := "chosen index is out of bounds, will copy user input"
|
msg := "chosen index is out of bounds, will copy user input"
|
||||||
logger.Warn(msg, "index", selectedIndex)
|
logger.Warn(msg, "index", selectedIndex)
|
||||||
showToast("error", msg)
|
showToast("error", msg)
|
||||||
@@ -507,7 +511,7 @@ func init() {
|
|||||||
hideIndexBar() // Hide overlay instead of removing page directly
|
hideIndexBar() // Hide overlay instead of removing page directly
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
m := chatBody.Messages[selectedIndex]
|
m := chatBody.GetMessages()[selectedIndex]
|
||||||
switch {
|
switch {
|
||||||
case roleEditMode:
|
case roleEditMode:
|
||||||
hideIndexBar() // Hide overlay first
|
hideIndexBar() // Hide overlay first
|
||||||
@@ -574,7 +578,7 @@ func init() {
|
|||||||
searchResults = nil
|
searchResults = nil
|
||||||
searchResultLengths = nil
|
searchResultLengths = nil
|
||||||
originalTextForSearch = ""
|
originalTextForSearch = ""
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
@@ -632,7 +636,7 @@ func init() {
|
|||||||
//
|
//
|
||||||
textArea.SetMovedFunc(updateStatusLine)
|
textArea.SetMovedFunc(updateStatusLine)
|
||||||
updateStatusLine()
|
updateStatusLine()
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
@@ -646,7 +650,7 @@ func init() {
|
|||||||
if event.Key() == tcell.KeyRune && event.Rune() == '5' && event.Modifiers()&tcell.ModAlt != 0 {
|
if event.Key() == tcell.KeyRune && event.Rune() == '5' && event.Modifiers()&tcell.ModAlt != 0 {
|
||||||
// switch cfg.ShowSys
|
// switch cfg.ShowSys
|
||||||
cfg.ShowSys = !cfg.ShowSys
|
cfg.ShowSys = !cfg.ShowSys
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
}
|
}
|
||||||
if event.Key() == tcell.KeyRune && event.Rune() == '3' && event.Modifiers()&tcell.ModAlt != 0 {
|
if event.Key() == tcell.KeyRune && event.Rune() == '3' && event.Modifiers()&tcell.ModAlt != 0 {
|
||||||
@@ -679,7 +683,7 @@ func init() {
|
|||||||
// Handle Alt+T to toggle thinking block visibility
|
// Handle Alt+T to toggle thinking block visibility
|
||||||
if event.Key() == tcell.KeyRune && event.Rune() == 't' && event.Modifiers()&tcell.ModAlt != 0 {
|
if event.Key() == tcell.KeyRune && event.Rune() == 't' && event.Modifiers()&tcell.ModAlt != 0 {
|
||||||
thinkingCollapsed = !thinkingCollapsed
|
thinkingCollapsed = !thinkingCollapsed
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
status := "expanded"
|
status := "expanded"
|
||||||
if thinkingCollapsed {
|
if thinkingCollapsed {
|
||||||
@@ -691,7 +695,7 @@ func init() {
|
|||||||
// Handle Ctrl+T to toggle tool call/response visibility
|
// Handle Ctrl+T to toggle tool call/response visibility
|
||||||
if event.Key() == tcell.KeyCtrlT {
|
if event.Key() == tcell.KeyCtrlT {
|
||||||
toolCollapsed = !toolCollapsed
|
toolCollapsed = !toolCollapsed
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
colorText()
|
colorText()
|
||||||
status := "expanded"
|
status := "expanded"
|
||||||
if toolCollapsed {
|
if toolCollapsed {
|
||||||
@@ -734,14 +738,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
if event.Key() == tcell.KeyF2 && !botRespMode {
|
if event.Key() == tcell.KeyF2 && !botRespMode {
|
||||||
// regen last msg
|
// regen last msg
|
||||||
if len(chatBody.Messages) == 0 {
|
if chatBody.GetMessageCount() == 0 {
|
||||||
showToast("info", "no messages to regenerate")
|
showToast("info", "no messages to regenerate")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
|
chatBody.TruncateMessages(chatBody.GetMessageCount() - 1)
|
||||||
// there is no case where user msg is regenerated
|
// there is no case where user msg is regenerated
|
||||||
// lastRole := chatBody.Messages[len(chatBody.Messages)-1].Role
|
// lastRole := chatBody.GetMessages()[chatBody.GetMessageCount()-1].Role
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
// go chatRound("", cfg.UserRole, textView, true, false)
|
// go chatRound("", cfg.UserRole, textView, true, false)
|
||||||
if cfg.TTS_ENABLED {
|
if cfg.TTS_ENABLED {
|
||||||
TTSDoneChan <- true
|
TTSDoneChan <- true
|
||||||
@@ -760,12 +764,12 @@ func init() {
|
|||||||
colorText()
|
colorText()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len(chatBody.Messages) == 0 {
|
if chatBody.GetMessageCount() == 0 {
|
||||||
showToast("info", "no messages to delete")
|
showToast("info", "no messages to delete")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
|
chatBody.TruncateMessages(chatBody.GetMessageCount() - 1)
|
||||||
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
|
textView.SetText(chatToText(chatBody.GetMessages(), cfg.ShowSys))
|
||||||
if cfg.TTS_ENABLED {
|
if cfg.TTS_ENABLED {
|
||||||
TTSDoneChan <- true
|
TTSDoneChan <- true
|
||||||
}
|
}
|
||||||
@@ -813,7 +817,7 @@ func init() {
|
|||||||
if event.Key() == tcell.KeyF7 {
|
if event.Key() == tcell.KeyF7 {
|
||||||
// copy msg to clipboard
|
// copy msg to clipboard
|
||||||
editMode = false
|
editMode = false
|
||||||
m := chatBody.Messages[len(chatBody.Messages)-1]
|
m := chatBody.GetMessages()[chatBody.GetMessageCount()-1]
|
||||||
msgText := m.GetText()
|
msgText := m.GetText()
|
||||||
if err := copyToClipboard(msgText); err != nil {
|
if err := copyToClipboard(msgText); err != nil {
|
||||||
logger.Error("failed to copy to clipboard", "error", err)
|
logger.Error("failed to copy to clipboard", "error", err)
|
||||||
@@ -997,10 +1001,10 @@ func init() {
|
|||||||
TTSDoneChan <- true
|
TTSDoneChan <- true
|
||||||
}
|
}
|
||||||
if event.Key() == tcell.KeyRune && event.Rune() == '0' && event.Modifiers()&tcell.ModAlt != 0 && cfg.TTS_ENABLED {
|
if event.Key() == tcell.KeyRune && event.Rune() == '0' && event.Modifiers()&tcell.ModAlt != 0 && cfg.TTS_ENABLED {
|
||||||
if len(chatBody.Messages) > 0 {
|
if chatBody.GetMessageCount() > 0 {
|
||||||
// Stop any currently playing TTS first
|
// Stop any currently playing TTS first
|
||||||
TTSDoneChan <- true
|
TTSDoneChan <- true
|
||||||
lastMsg := chatBody.Messages[len(chatBody.Messages)-1]
|
lastMsg := chatBody.GetMessages()[chatBody.GetMessageCount()-1]
|
||||||
cleanedText := models.CleanText(lastMsg.GetText())
|
cleanedText := models.CleanText(lastMsg.GetText())
|
||||||
if cleanedText != "" {
|
if cleanedText != "" {
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
@@ -1012,7 +1016,7 @@ func init() {
|
|||||||
if event.Key() == tcell.KeyCtrlW {
|
if event.Key() == tcell.KeyCtrlW {
|
||||||
// INFO: continue bot/text message
|
// INFO: continue bot/text message
|
||||||
// without new role
|
// without new role
|
||||||
lastRole := chatBody.Messages[len(chatBody.Messages)-1].Role
|
lastRole := chatBody.GetMessages()[chatBody.GetMessageCount()-1].Role
|
||||||
// go chatRound("", lastRole, textView, false, true)
|
// go chatRound("", lastRole, textView, false, true)
|
||||||
chatRoundChan <- &models.ChatRoundReq{Role: lastRole, Resume: true}
|
chatRoundChan <- &models.ChatRoundReq{Role: lastRole, Resume: true}
|
||||||
return nil
|
return nil
|
||||||
@@ -1098,7 +1102,7 @@ func init() {
|
|||||||
if event.Key() == tcell.KeyRune && event.Modifiers() == tcell.ModAlt && event.Rune() == '9' {
|
if event.Key() == tcell.KeyRune && event.Modifiers() == tcell.ModAlt && event.Rune() == '9' {
|
||||||
// Warm up (load) the currently selected model
|
// Warm up (load) the currently selected model
|
||||||
go warmUpModel()
|
go warmUpModel()
|
||||||
showToast("model warmup", "loading model: "+chatBody.Model)
|
showToast("model warmup", "loading model: "+chatBody.GetModel())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// cannot send msg in editMode or botRespMode
|
// cannot send msg in editMode or botRespMode
|
||||||
@@ -1137,7 +1141,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
// add user icon before user msg
|
// add user icon before user msg
|
||||||
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
|
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
|
||||||
nl, len(chatBody.Messages), persona, msgText)
|
nl, chatBody.GetMessageCount(), persona, msgText)
|
||||||
textArea.SetText("", true)
|
textArea.SetText("", true)
|
||||||
if scrollToEndEnabled {
|
if scrollToEndEnabled {
|
||||||
textView.ScrollToEnd()
|
textView.ScrollToEnd()
|
||||||
|
|||||||
Reference in New Issue
Block a user