Compare commits
2 Commits
master
...
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 {
|
||||||
|
|||||||
@@ -1,218 +0,0 @@
|
|||||||
//go:build extra
|
|
||||||
// +build extra
|
|
||||||
|
|
||||||
package extra
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"gf-lt/models"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
google_translate_tts "github.com/GrailFinder/google-translate-tts"
|
|
||||||
"github.com/neurosnap/sentences/english"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GoogleTranslateOrator struct {
|
|
||||||
logger *slog.Logger
|
|
||||||
mu sync.Mutex
|
|
||||||
speech *google_translate_tts.Speech
|
|
||||||
// fields for playback control
|
|
||||||
cmd *exec.Cmd
|
|
||||||
cmdMu sync.Mutex
|
|
||||||
stopCh chan struct{}
|
|
||||||
// text buffer and interrupt flag
|
|
||||||
textBuffer strings.Builder
|
|
||||||
interrupt bool
|
|
||||||
Speed float32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *GoogleTranslateOrator) stoproutine() {
|
|
||||||
for {
|
|
||||||
<-TTSDoneChan
|
|
||||||
o.logger.Debug("orator got done signal")
|
|
||||||
o.Stop()
|
|
||||||
for len(TTSTextChan) > 0 {
|
|
||||||
<-TTSTextChan
|
|
||||||
}
|
|
||||||
o.mu.Lock()
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.interrupt = true
|
|
||||||
o.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *GoogleTranslateOrator) readroutine() {
|
|
||||||
tokenizer, _ := english.NewSentenceTokenizer(nil)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case chunk := <-TTSTextChan:
|
|
||||||
o.mu.Lock()
|
|
||||||
o.interrupt = false
|
|
||||||
_, err := o.textBuffer.WriteString(chunk)
|
|
||||||
if err != nil {
|
|
||||||
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
|
||||||
o.mu.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
text := o.textBuffer.String()
|
|
||||||
sentences := tokenizer.Tokenize(text)
|
|
||||||
o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences))
|
|
||||||
if len(sentences) <= 1 {
|
|
||||||
o.mu.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
completeSentences := sentences[:len(sentences)-1]
|
|
||||||
remaining := sentences[len(sentences)-1].Text
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.textBuffer.WriteString(remaining)
|
|
||||||
o.mu.Unlock()
|
|
||||||
for _, sentence := range completeSentences {
|
|
||||||
o.mu.Lock()
|
|
||||||
interrupted := o.interrupt
|
|
||||||
o.mu.Unlock()
|
|
||||||
if interrupted {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cleanedText := models.CleanText(sentence.Text)
|
|
||||||
if cleanedText == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
o.logger.Debug("calling Speak with sentence", "sent", cleanedText)
|
|
||||||
if err := o.Speak(cleanedText); err != nil {
|
|
||||||
o.logger.Error("tts failed", "sentence", cleanedText, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case <-TTSFlushChan:
|
|
||||||
o.logger.Debug("got flushchan signal start")
|
|
||||||
// lln is done get the whole message out
|
|
||||||
if len(TTSTextChan) > 0 { // otherwise might get stuck
|
|
||||||
for chunk := range TTSTextChan {
|
|
||||||
o.mu.Lock()
|
|
||||||
_, err := o.textBuffer.WriteString(chunk)
|
|
||||||
o.mu.Unlock()
|
|
||||||
if err != nil {
|
|
||||||
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if len(TTSTextChan) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
o.mu.Lock()
|
|
||||||
remaining := o.textBuffer.String()
|
|
||||||
remaining = models.CleanText(remaining)
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.mu.Unlock()
|
|
||||||
if remaining == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
o.logger.Debug("calling Speak with remainder", "rem", remaining)
|
|
||||||
sentencesRem := tokenizer.Tokenize(remaining)
|
|
||||||
for _, rs := range sentencesRem { // to avoid dumping large volume of text
|
|
||||||
o.mu.Lock()
|
|
||||||
interrupt := o.interrupt
|
|
||||||
o.mu.Unlock()
|
|
||||||
if interrupt {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err := o.Speak(rs.Text); err != nil {
|
|
||||||
o.logger.Error("tts failed", "sentence", rs.Text, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *GoogleTranslateOrator) GetLogger() *slog.Logger {
|
|
||||||
return o.logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *GoogleTranslateOrator) Speak(text string) error {
|
|
||||||
o.logger.Debug("fn: Speak is called", "text-len", len(text))
|
|
||||||
// Generate MP3 data directly as an io.Reader
|
|
||||||
reader, err := o.speech.GenerateSpeech(text)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("generate speech failed: %w", err)
|
|
||||||
}
|
|
||||||
// Wrap in io.NopCloser since GenerateSpeech returns io.Reader (no close needed)
|
|
||||||
body := io.NopCloser(reader)
|
|
||||||
defer body.Close()
|
|
||||||
// Build ffplay command with optional speed filter
|
|
||||||
args := []string{"-nodisp", "-autoexit"}
|
|
||||||
if o.Speed > 0.1 && o.Speed != 1.0 {
|
|
||||||
// atempo range is 0.5 to 2.0; you might clamp it here
|
|
||||||
args = append(args, "-af", fmt.Sprintf("atempo=%.2f", o.Speed))
|
|
||||||
}
|
|
||||||
args = append(args, "-i", "pipe:0")
|
|
||||||
cmd := exec.Command("ffplay", args...)
|
|
||||||
stdin, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get stdin pipe: %w", err)
|
|
||||||
}
|
|
||||||
o.cmdMu.Lock()
|
|
||||||
o.cmd = cmd
|
|
||||||
o.stopCh = make(chan struct{})
|
|
||||||
o.cmdMu.Unlock()
|
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
return fmt.Errorf("failed to start ffplay: %w", err)
|
|
||||||
}
|
|
||||||
copyErr := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
_, err := io.Copy(stdin, body)
|
|
||||||
stdin.Close()
|
|
||||||
copyErr <- err
|
|
||||||
}()
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
done <- cmd.Wait()
|
|
||||||
}()
|
|
||||||
select {
|
|
||||||
case <-o.stopCh:
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
return nil
|
|
||||||
case copyErrVal := <-copyErr:
|
|
||||||
if copyErrVal != nil {
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
return copyErrVal
|
|
||||||
}
|
|
||||||
return <-done
|
|
||||||
case err := <-done:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *GoogleTranslateOrator) Stop() {
|
|
||||||
o.cmdMu.Lock()
|
|
||||||
defer o.cmdMu.Unlock()
|
|
||||||
// Signal any running Speak to stop
|
|
||||||
if o.stopCh != nil {
|
|
||||||
select {
|
|
||||||
case <-o.stopCh: // already closed
|
|
||||||
default:
|
|
||||||
close(o.stopCh)
|
|
||||||
}
|
|
||||||
o.stopCh = nil
|
|
||||||
}
|
|
||||||
// Kill the external player process if it's still running
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
o.cmd.Wait() // clean up zombie process
|
|
||||||
o.cmd = nil
|
|
||||||
}
|
|
||||||
// Also reset text buffer and interrupt flag (with o.mu)
|
|
||||||
o.mu.Lock()
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.interrupt = true
|
|
||||||
o.mu.Unlock()
|
|
||||||
}
|
|
||||||
259
extra/kokoro.go
259
extra/kokoro.go
@@ -1,259 +0,0 @@
|
|||||||
//go:build extra
|
|
||||||
// +build extra
|
|
||||||
|
|
||||||
package extra
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"gf-lt/models"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/neurosnap/sentences/english"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KokoroOrator struct {
|
|
||||||
logger *slog.Logger
|
|
||||||
mu sync.Mutex
|
|
||||||
URL string
|
|
||||||
Format models.AudioFormat
|
|
||||||
Stream bool
|
|
||||||
Speed float32
|
|
||||||
Language string
|
|
||||||
Voice string
|
|
||||||
// fields for playback control
|
|
||||||
cmd *exec.Cmd
|
|
||||||
cmdMu sync.Mutex
|
|
||||||
stopCh chan struct{}
|
|
||||||
// textBuffer, interrupt etc. remain the same
|
|
||||||
textBuffer strings.Builder
|
|
||||||
interrupt bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *KokoroOrator) GetLogger() *slog.Logger {
|
|
||||||
return o.logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *KokoroOrator) Speak(text string) error {
|
|
||||||
o.logger.Debug("fn: Speak is called", "text-len", len(text))
|
|
||||||
body, err := o.requestSound(text)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("request failed: %w", err)
|
|
||||||
}
|
|
||||||
defer body.Close()
|
|
||||||
cmd := exec.Command("ffplay", "-nodisp", "-autoexit", "-i", "pipe:0")
|
|
||||||
stdin, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get stdin pipe: %w", err)
|
|
||||||
}
|
|
||||||
o.cmdMu.Lock()
|
|
||||||
o.cmd = cmd
|
|
||||||
o.stopCh = make(chan struct{})
|
|
||||||
o.cmdMu.Unlock()
|
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
return fmt.Errorf("failed to start ffplay: %w", err)
|
|
||||||
}
|
|
||||||
// Copy audio in background
|
|
||||||
copyErr := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
_, err := io.Copy(stdin, body)
|
|
||||||
stdin.Close()
|
|
||||||
copyErr <- err
|
|
||||||
}()
|
|
||||||
// Wait for player in background
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
done <- cmd.Wait()
|
|
||||||
}()
|
|
||||||
// Wait for BOTH copy and player, but ensure we block until done
|
|
||||||
select {
|
|
||||||
case <-o.stopCh:
|
|
||||||
// Stop requested: kill player and wait for it to exit
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
}
|
|
||||||
<-done // Wait for process to actually exit
|
|
||||||
return nil
|
|
||||||
case copyErrVal := <-copyErr:
|
|
||||||
if copyErrVal != nil {
|
|
||||||
// Copy failed: kill player and wait
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
return copyErrVal
|
|
||||||
}
|
|
||||||
// Copy succeeded, now wait for playback to complete
|
|
||||||
return <-done
|
|
||||||
case err := <-done:
|
|
||||||
// Playback finished normally (copy must have succeeded or player would have exited early)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func (o *KokoroOrator) requestSound(text string) (io.ReadCloser, error) {
|
|
||||||
if o.URL == "" {
|
|
||||||
return nil, fmt.Errorf("TTS URL is empty")
|
|
||||||
}
|
|
||||||
payload := map[string]interface{}{
|
|
||||||
"input": text,
|
|
||||||
"voice": o.Voice,
|
|
||||||
"response_format": o.Format,
|
|
||||||
"download_format": o.Format,
|
|
||||||
"stream": o.Stream,
|
|
||||||
"speed": o.Speed,
|
|
||||||
// "return_download_link": true,
|
|
||||||
"lang_code": o.Language,
|
|
||||||
}
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to marshal payload: %w", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", o.URL, bytes.NewBuffer(payloadBytes)) //nolint:noctx
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("accept", "application/json")
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
defer resp.Body.Close()
|
|
||||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
return resp.Body, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *KokoroOrator) stoproutine() {
|
|
||||||
for {
|
|
||||||
<-TTSDoneChan
|
|
||||||
o.logger.Debug("orator got done signal")
|
|
||||||
// 1. Stop any ongoing playback (kills external player, closes stopCh)
|
|
||||||
o.Stop()
|
|
||||||
// 2. Drain any pending text chunks
|
|
||||||
for len(TTSTextChan) > 0 {
|
|
||||||
<-TTSTextChan
|
|
||||||
}
|
|
||||||
// 3. Reset internal state
|
|
||||||
o.mu.Lock()
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.interrupt = true
|
|
||||||
o.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *KokoroOrator) Stop() {
|
|
||||||
o.cmdMu.Lock()
|
|
||||||
defer o.cmdMu.Unlock()
|
|
||||||
// Signal any running Speak to stop
|
|
||||||
if o.stopCh != nil {
|
|
||||||
select {
|
|
||||||
case <-o.stopCh: // already closed
|
|
||||||
default:
|
|
||||||
close(o.stopCh)
|
|
||||||
}
|
|
||||||
o.stopCh = nil
|
|
||||||
}
|
|
||||||
// Kill the external player process if it's still running
|
|
||||||
if o.cmd != nil && o.cmd.Process != nil {
|
|
||||||
o.cmd.Process.Kill()
|
|
||||||
o.cmd.Wait() // clean up zombie process
|
|
||||||
o.cmd = nil
|
|
||||||
}
|
|
||||||
// Also reset text buffer and interrupt flag (with o.mu)
|
|
||||||
o.mu.Lock()
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.interrupt = true
|
|
||||||
o.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *KokoroOrator) readroutine() {
|
|
||||||
tokenizer, _ := english.NewSentenceTokenizer(nil)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case chunk := <-TTSTextChan:
|
|
||||||
o.mu.Lock()
|
|
||||||
o.interrupt = false
|
|
||||||
_, err := o.textBuffer.WriteString(chunk)
|
|
||||||
if err != nil {
|
|
||||||
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
|
||||||
o.mu.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
text := o.textBuffer.String()
|
|
||||||
sentences := tokenizer.Tokenize(text)
|
|
||||||
o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences))
|
|
||||||
if len(sentences) <= 1 {
|
|
||||||
o.mu.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
completeSentences := sentences[:len(sentences)-1]
|
|
||||||
remaining := sentences[len(sentences)-1].Text
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.textBuffer.WriteString(remaining)
|
|
||||||
o.mu.Unlock()
|
|
||||||
for _, sentence := range completeSentences {
|
|
||||||
o.mu.Lock()
|
|
||||||
interrupted := o.interrupt
|
|
||||||
o.mu.Unlock()
|
|
||||||
if interrupted {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cleanedText := models.CleanText(sentence.Text)
|
|
||||||
if cleanedText == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
o.logger.Debug("calling Speak with sentence", "sent", cleanedText)
|
|
||||||
if err := o.Speak(cleanedText); err != nil {
|
|
||||||
o.logger.Error("tts failed", "sentence", cleanedText, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case <-TTSFlushChan:
|
|
||||||
o.logger.Debug("got flushchan signal start")
|
|
||||||
// lln is done get the whole message out
|
|
||||||
if len(TTSTextChan) > 0 { // otherwise might get stuck
|
|
||||||
for chunk := range TTSTextChan {
|
|
||||||
o.mu.Lock()
|
|
||||||
_, err := o.textBuffer.WriteString(chunk)
|
|
||||||
o.mu.Unlock()
|
|
||||||
if err != nil {
|
|
||||||
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if len(TTSTextChan) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// flush remaining text
|
|
||||||
o.mu.Lock()
|
|
||||||
remaining := o.textBuffer.String()
|
|
||||||
remaining = models.CleanText(remaining)
|
|
||||||
o.textBuffer.Reset()
|
|
||||||
o.mu.Unlock()
|
|
||||||
if remaining == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
o.logger.Debug("calling Speak with remainder", "rem", remaining)
|
|
||||||
sentencesRem := tokenizer.Tokenize(remaining)
|
|
||||||
for _, rs := range sentencesRem { // to avoid dumping large volume of text
|
|
||||||
o.mu.Lock()
|
|
||||||
interrupt := o.interrupt
|
|
||||||
o.mu.Unlock()
|
|
||||||
if interrupt {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err := o.Speak(rs.Text); err != nil {
|
|
||||||
o.logger.Error("tts failed", "sentence", rs, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
406
extra/tts.go
406
extra/tts.go
@@ -4,13 +4,25 @@
|
|||||||
package extra
|
package extra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"gf-lt/config"
|
"gf-lt/config"
|
||||||
"gf-lt/models"
|
"gf-lt/models"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
google_translate_tts "github.com/GrailFinder/google-translate-tts"
|
google_translate_tts "github.com/GrailFinder/google-translate-tts"
|
||||||
|
"github.com/GrailFinder/google-translate-tts/handlers"
|
||||||
|
"github.com/gopxl/beep/v2"
|
||||||
|
"github.com/gopxl/beep/v2/mp3"
|
||||||
|
"github.com/gopxl/beep/v2/speaker"
|
||||||
|
"github.com/neurosnap/sentences/english"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -27,6 +39,142 @@ type Orator interface {
|
|||||||
GetLogger() *slog.Logger
|
GetLogger() *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// impl https://github.com/remsky/Kokoro-FastAPI
|
||||||
|
type KokoroOrator struct {
|
||||||
|
logger *slog.Logger
|
||||||
|
mu sync.Mutex
|
||||||
|
URL string
|
||||||
|
Format models.AudioFormat
|
||||||
|
Stream bool
|
||||||
|
Speed float32
|
||||||
|
Language string
|
||||||
|
Voice string
|
||||||
|
currentStream *beep.Ctrl // Added for playback control
|
||||||
|
currentDone chan bool
|
||||||
|
textBuffer strings.Builder
|
||||||
|
interrupt bool
|
||||||
|
// textBuffer bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Google Translate TTS implementation
|
||||||
|
type GoogleTranslateOrator struct {
|
||||||
|
logger *slog.Logger
|
||||||
|
mu sync.Mutex
|
||||||
|
speech *google_translate_tts.Speech
|
||||||
|
currentStream *beep.Ctrl
|
||||||
|
currentDone chan bool
|
||||||
|
textBuffer strings.Builder
|
||||||
|
interrupt bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) stoproutine() {
|
||||||
|
for {
|
||||||
|
<-TTSDoneChan
|
||||||
|
o.logger.Debug("orator got done signal")
|
||||||
|
o.Stop()
|
||||||
|
// drain the channel
|
||||||
|
for len(TTSTextChan) > 0 {
|
||||||
|
<-TTSTextChan
|
||||||
|
}
|
||||||
|
o.mu.Lock()
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
if o.currentDone != nil {
|
||||||
|
select {
|
||||||
|
case o.currentDone <- true:
|
||||||
|
default:
|
||||||
|
// Channel might be closed, ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
o.interrupt = true
|
||||||
|
o.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) readroutine() {
|
||||||
|
tokenizer, _ := english.NewSentenceTokenizer(nil)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case chunk := <-TTSTextChan:
|
||||||
|
o.mu.Lock()
|
||||||
|
o.interrupt = false
|
||||||
|
_, err := o.textBuffer.WriteString(chunk)
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
||||||
|
o.mu.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
text := o.textBuffer.String()
|
||||||
|
sentences := tokenizer.Tokenize(text)
|
||||||
|
o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences))
|
||||||
|
if len(sentences) <= 1 {
|
||||||
|
o.mu.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
completeSentences := sentences[:len(sentences)-1]
|
||||||
|
remaining := sentences[len(sentences)-1].Text
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
o.textBuffer.WriteString(remaining)
|
||||||
|
o.mu.Unlock()
|
||||||
|
|
||||||
|
for _, sentence := range completeSentences {
|
||||||
|
o.mu.Lock()
|
||||||
|
interrupted := o.interrupt
|
||||||
|
o.mu.Unlock()
|
||||||
|
if interrupted {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cleanedText := models.CleanText(sentence.Text)
|
||||||
|
if cleanedText == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
o.logger.Debug("calling Speak with sentence", "sent", cleanedText)
|
||||||
|
if err := o.Speak(cleanedText); err != nil {
|
||||||
|
o.logger.Error("tts failed", "sentence", cleanedText, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-TTSFlushChan:
|
||||||
|
o.logger.Debug("got flushchan signal start")
|
||||||
|
// lln is done get the whole message out
|
||||||
|
if len(TTSTextChan) > 0 { // otherwise might get stuck
|
||||||
|
for chunk := range TTSTextChan {
|
||||||
|
o.mu.Lock()
|
||||||
|
_, err := o.textBuffer.WriteString(chunk)
|
||||||
|
o.mu.Unlock()
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(TTSTextChan) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// flush remaining text
|
||||||
|
o.mu.Lock()
|
||||||
|
remaining := o.textBuffer.String()
|
||||||
|
remaining = models.CleanText(remaining)
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
o.mu.Unlock()
|
||||||
|
if remaining == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
o.logger.Debug("calling Speak with remainder", "rem", remaining)
|
||||||
|
sentencesRem := tokenizer.Tokenize(remaining)
|
||||||
|
for _, rs := range sentencesRem { // to avoid dumping large volume of text
|
||||||
|
o.mu.Lock()
|
||||||
|
interrupt := o.interrupt
|
||||||
|
o.mu.Unlock()
|
||||||
|
if interrupt {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := o.Speak(rs.Text); err != nil {
|
||||||
|
o.logger.Error("tts failed", "sentence", rs, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewOrator(log *slog.Logger, cfg *config.Config) Orator {
|
func NewOrator(log *slog.Logger, cfg *config.Config) Orator {
|
||||||
provider := cfg.TTS_PROVIDER
|
provider := cfg.TTS_PROVIDER
|
||||||
if provider == "" {
|
if provider == "" {
|
||||||
@@ -56,14 +204,270 @@ func NewOrator(log *slog.Logger, cfg *config.Config) Orator {
|
|||||||
Language: language,
|
Language: language,
|
||||||
Proxy: "", // Proxy not supported
|
Proxy: "", // Proxy not supported
|
||||||
Speed: cfg.TTS_SPEED,
|
Speed: cfg.TTS_SPEED,
|
||||||
|
Handler: &handlers.Beep{},
|
||||||
}
|
}
|
||||||
orator := &GoogleTranslateOrator{
|
orator := &GoogleTranslateOrator{
|
||||||
logger: log,
|
logger: log,
|
||||||
speech: speech,
|
speech: speech,
|
||||||
Speed: cfg.TTS_SPEED,
|
|
||||||
}
|
}
|
||||||
go orator.readroutine()
|
go orator.readroutine()
|
||||||
go orator.stoproutine()
|
go orator.stoproutine()
|
||||||
return orator
|
return orator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) GetLogger() *slog.Logger {
|
||||||
|
return o.logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) requestSound(text string) (io.ReadCloser, error) {
|
||||||
|
if o.URL == "" {
|
||||||
|
return nil, fmt.Errorf("TTS URL is empty")
|
||||||
|
}
|
||||||
|
payload := map[string]interface{}{
|
||||||
|
"input": text,
|
||||||
|
"voice": o.Voice,
|
||||||
|
"response_format": o.Format,
|
||||||
|
"download_format": o.Format,
|
||||||
|
"stream": o.Stream,
|
||||||
|
"speed": o.Speed,
|
||||||
|
// "return_download_link": true,
|
||||||
|
"lang_code": o.Language,
|
||||||
|
}
|
||||||
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal payload: %w", err)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", o.URL, bytes.NewBuffer(payloadBytes)) //nolint:noctx
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("accept", "application/json")
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed: %w", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return resp.Body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) Speak(text string) error {
|
||||||
|
o.logger.Debug("fn: Speak is called", "text-len", len(text))
|
||||||
|
body, err := o.requestSound(text)
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Error("request failed", "error", err)
|
||||||
|
return fmt.Errorf("request failed: %w", err)
|
||||||
|
}
|
||||||
|
defer body.Close()
|
||||||
|
// Decode the mp3 audio from response body
|
||||||
|
streamer, format, err := mp3.Decode(body)
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Error("mp3 decode failed", "error", err)
|
||||||
|
return fmt.Errorf("mp3 decode failed: %w", err)
|
||||||
|
}
|
||||||
|
defer streamer.Close()
|
||||||
|
// here it spams with errors that speaker cannot be initialized more than once, but how would we deal with many audio records then?
|
||||||
|
if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)); err != nil {
|
||||||
|
o.logger.Debug("failed to init speaker", "error", err)
|
||||||
|
}
|
||||||
|
done := make(chan bool)
|
||||||
|
o.mu.Lock()
|
||||||
|
o.currentDone = done
|
||||||
|
o.currentStream = &beep.Ctrl{Streamer: beep.Seq(streamer, beep.Callback(func() {
|
||||||
|
o.mu.Lock()
|
||||||
|
close(done)
|
||||||
|
o.currentStream = nil
|
||||||
|
o.currentDone = nil
|
||||||
|
o.mu.Unlock()
|
||||||
|
})), Paused: false}
|
||||||
|
o.mu.Unlock()
|
||||||
|
speaker.Play(o.currentStream)
|
||||||
|
<-done
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *KokoroOrator) Stop() {
|
||||||
|
// speaker.Clear()
|
||||||
|
o.logger.Debug("attempted to stop orator", "orator", o)
|
||||||
|
speaker.Lock()
|
||||||
|
defer speaker.Unlock()
|
||||||
|
o.mu.Lock()
|
||||||
|
defer o.mu.Unlock()
|
||||||
|
if o.currentStream != nil {
|
||||||
|
// o.currentStream.Paused = true
|
||||||
|
o.currentStream.Streamer = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *GoogleTranslateOrator) stoproutine() {
|
||||||
|
for {
|
||||||
|
<-TTSDoneChan
|
||||||
|
o.logger.Debug("orator got done signal")
|
||||||
|
o.Stop()
|
||||||
|
// drain the channel
|
||||||
|
for len(TTSTextChan) > 0 {
|
||||||
|
<-TTSTextChan
|
||||||
|
}
|
||||||
|
o.mu.Lock()
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
if o.currentDone != nil {
|
||||||
|
select {
|
||||||
|
case o.currentDone <- true:
|
||||||
|
default:
|
||||||
|
// Channel might be closed, ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
o.interrupt = true
|
||||||
|
o.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *GoogleTranslateOrator) readroutine() {
|
||||||
|
tokenizer, _ := english.NewSentenceTokenizer(nil)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case chunk := <-TTSTextChan:
|
||||||
|
o.mu.Lock()
|
||||||
|
o.interrupt = false
|
||||||
|
_, err := o.textBuffer.WriteString(chunk)
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
||||||
|
o.mu.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
text := o.textBuffer.String()
|
||||||
|
sentences := tokenizer.Tokenize(text)
|
||||||
|
o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences))
|
||||||
|
if len(sentences) <= 1 {
|
||||||
|
o.mu.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
completeSentences := sentences[:len(sentences)-1]
|
||||||
|
remaining := sentences[len(sentences)-1].Text
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
o.textBuffer.WriteString(remaining)
|
||||||
|
o.mu.Unlock()
|
||||||
|
|
||||||
|
for _, sentence := range completeSentences {
|
||||||
|
o.mu.Lock()
|
||||||
|
interrupted := o.interrupt
|
||||||
|
o.mu.Unlock()
|
||||||
|
if interrupted {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cleanedText := models.CleanText(sentence.Text)
|
||||||
|
if cleanedText == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
o.logger.Debug("calling Speak with sentence", "sent", cleanedText)
|
||||||
|
if err := o.Speak(cleanedText); err != nil {
|
||||||
|
o.logger.Error("tts failed", "sentence", cleanedText, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-TTSFlushChan:
|
||||||
|
o.logger.Debug("got flushchan signal start")
|
||||||
|
// lln is done get the whole message out
|
||||||
|
if len(TTSTextChan) > 0 { // otherwise might get stuck
|
||||||
|
for chunk := range TTSTextChan {
|
||||||
|
o.mu.Lock()
|
||||||
|
_, err := o.textBuffer.WriteString(chunk)
|
||||||
|
o.mu.Unlock()
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Warn("failed to write to stringbuilder", "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(TTSTextChan) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
o.mu.Lock()
|
||||||
|
remaining := o.textBuffer.String()
|
||||||
|
remaining = models.CleanText(remaining)
|
||||||
|
o.textBuffer.Reset()
|
||||||
|
o.mu.Unlock()
|
||||||
|
if remaining == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
o.logger.Debug("calling Speak with remainder", "rem", remaining)
|
||||||
|
sentencesRem := tokenizer.Tokenize(remaining)
|
||||||
|
for _, rs := range sentencesRem { // to avoid dumping large volume of text
|
||||||
|
o.mu.Lock()
|
||||||
|
interrupt := o.interrupt
|
||||||
|
o.mu.Unlock()
|
||||||
|
if interrupt {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := o.Speak(rs.Text); err != nil {
|
||||||
|
o.logger.Error("tts failed", "sentence", rs.Text, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *GoogleTranslateOrator) GetLogger() *slog.Logger {
|
||||||
|
return o.logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *GoogleTranslateOrator) Speak(text string) error {
|
||||||
|
o.logger.Debug("fn: Speak is called", "text-len", len(text))
|
||||||
|
// Generate MP3 data using google-translate-tts
|
||||||
|
reader, err := o.speech.GenerateSpeech(text)
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Error("generate speech failed", "error", err)
|
||||||
|
return fmt.Errorf("generate speech failed: %w", err)
|
||||||
|
}
|
||||||
|
// Decode the mp3 audio from reader (wrap with NopCloser for io.ReadCloser)
|
||||||
|
streamer, format, err := mp3.Decode(io.NopCloser(reader))
|
||||||
|
if err != nil {
|
||||||
|
o.logger.Error("mp3 decode failed", "error", err)
|
||||||
|
return fmt.Errorf("mp3 decode failed: %w", err)
|
||||||
|
}
|
||||||
|
defer streamer.Close()
|
||||||
|
playbackStreamer := beep.Streamer(streamer)
|
||||||
|
speed := o.speech.Speed
|
||||||
|
if speed <= 0 {
|
||||||
|
speed = 1.0
|
||||||
|
}
|
||||||
|
if speed != 1.0 {
|
||||||
|
playbackStreamer = beep.ResampleRatio(3, float64(speed), streamer)
|
||||||
|
}
|
||||||
|
// Initialize speaker with the format's sample rate
|
||||||
|
if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)); err != nil {
|
||||||
|
o.logger.Debug("failed to init speaker", "error", err)
|
||||||
|
}
|
||||||
|
done := make(chan bool)
|
||||||
|
o.mu.Lock()
|
||||||
|
o.currentDone = done
|
||||||
|
o.currentStream = &beep.Ctrl{Streamer: beep.Seq(playbackStreamer, beep.Callback(func() {
|
||||||
|
o.mu.Lock()
|
||||||
|
close(done)
|
||||||
|
o.currentStream = nil
|
||||||
|
o.currentDone = nil
|
||||||
|
o.mu.Unlock()
|
||||||
|
})), Paused: false}
|
||||||
|
o.mu.Unlock()
|
||||||
|
speaker.Play(o.currentStream)
|
||||||
|
<-done // wait for playback to complete
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *GoogleTranslateOrator) Stop() {
|
||||||
|
o.logger.Debug("attempted to stop google translate orator")
|
||||||
|
speaker.Lock()
|
||||||
|
defer speaker.Unlock()
|
||||||
|
o.mu.Lock()
|
||||||
|
defer o.mu.Unlock()
|
||||||
|
if o.currentStream != nil {
|
||||||
|
o.currentStream.Streamer = nil
|
||||||
|
}
|
||||||
|
// Also stop the speech handler if possible
|
||||||
|
if o.speech != nil {
|
||||||
|
_ = o.speech.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
7
go.mod
7
go.mod
@@ -4,11 +4,12 @@ go 1.25.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.5.0
|
github.com/BurntSushi/toml v1.5.0
|
||||||
github.com/GrailFinder/google-translate-tts v0.1.4
|
github.com/GrailFinder/google-translate-tts v0.1.3
|
||||||
github.com/GrailFinder/searchagent v0.2.0
|
github.com/GrailFinder/searchagent v0.2.0
|
||||||
github.com/PuerkitoBio/goquery v1.11.0
|
github.com/PuerkitoBio/goquery v1.11.0
|
||||||
github.com/gdamore/tcell/v2 v2.13.2
|
github.com/gdamore/tcell/v2 v2.13.2
|
||||||
github.com/glebarez/go-sqlite v1.22.0
|
github.com/glebarez/go-sqlite v1.22.0
|
||||||
|
github.com/gopxl/beep/v2 v2.1.1
|
||||||
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b
|
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b
|
||||||
github.com/jmoiron/sqlx v1.4.0
|
github.com/jmoiron/sqlx v1.4.0
|
||||||
github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728
|
github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728
|
||||||
@@ -24,17 +25,21 @@ require (
|
|||||||
github.com/andybalholm/cascadia v1.3.3 // indirect
|
github.com/andybalholm/cascadia v1.3.3 // indirect
|
||||||
github.com/deckarep/golang-set/v2 v2.8.0 // indirect
|
github.com/deckarep/golang-set/v2 v2.8.0 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/ebitengine/oto/v3 v3.4.0 // indirect
|
||||||
|
github.com/ebitengine/purego v0.9.1 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/gdamore/encoding v1.0.1 // indirect
|
github.com/gdamore/encoding v1.0.1 // indirect
|
||||||
github.com/go-jose/go-jose/v3 v3.0.4 // indirect
|
github.com/go-jose/go-jose/v3 v3.0.4 // indirect
|
||||||
github.com/go-stack/stack v1.8.1 // indirect
|
github.com/go-stack/stack v1.8.1 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.4 // indirect
|
github.com/hajimehoshi/go-mp3 v0.3.4 // indirect
|
||||||
|
github.com/hajimehoshi/oto/v2 v2.3.1 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
|
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/schollz/progressbar/v2 v2.15.0 // indirect
|
github.com/schollz/progressbar/v2 v2.15.0 // indirect
|
||||||
|
|||||||
13
go.sum
13
go.sum
@@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
|||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
github.com/GrailFinder/google-translate-tts v0.1.4 h1:NJoPZUGfBrmouQMN19MUcNPNUx4tmf4a8OZRME4E4Mg=
|
github.com/GrailFinder/google-translate-tts v0.1.3 h1:Mww9tNzTWjjSh+OCbTPl/+21oMPKcUecXZfU7nTB/lA=
|
||||||
github.com/GrailFinder/google-translate-tts v0.1.4/go.mod h1:YIOLKR7sObazdUCrSex3u9OVBovU55eYgWa25vsQJ18=
|
github.com/GrailFinder/google-translate-tts v0.1.3/go.mod h1:YIOLKR7sObazdUCrSex3u9OVBovU55eYgWa25vsQJ18=
|
||||||
github.com/GrailFinder/searchagent v0.2.0 h1:U2GVjLh/9xZt0xX9OcYk9Q2fMkyzyTiADPUmUisRdtQ=
|
github.com/GrailFinder/searchagent v0.2.0 h1:U2GVjLh/9xZt0xX9OcYk9Q2fMkyzyTiADPUmUisRdtQ=
|
||||||
github.com/GrailFinder/searchagent v0.2.0/go.mod h1:d66tn5+22LI8IGJREUsRBT60P0sFdgQgvQRqyvgItrs=
|
github.com/GrailFinder/searchagent v0.2.0/go.mod h1:d66tn5+22LI8IGJREUsRBT60P0sFdgQgvQRqyvgItrs=
|
||||||
github.com/PuerkitoBio/goquery v1.11.0 h1:jZ7pwMQXIITcUXNH83LLk+txlaEy6NVOfTuP43xxfqw=
|
github.com/PuerkitoBio/goquery v1.11.0 h1:jZ7pwMQXIITcUXNH83LLk+txlaEy6NVOfTuP43xxfqw=
|
||||||
@@ -17,6 +17,10 @@ github.com/deckarep/golang-set/v2 v2.8.0 h1:swm0rlPCmdWn9mESxKOjWk8hXSqoxOp+Zlfu
|
|||||||
github.com/deckarep/golang-set/v2 v2.8.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
github.com/deckarep/golang-set/v2 v2.8.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/ebitengine/oto/v3 v3.4.0 h1:br0PgASsEWaoWn38b2Goe7m1GKFYfNgnsjSd5Gg+/bQ=
|
||||||
|
github.com/ebitengine/oto/v3 v3.4.0/go.mod h1:IOleLVD0m+CMak3mRVwsYY8vTctQgOM0iiL6S7Ar7eI=
|
||||||
|
github.com/ebitengine/purego v0.9.1 h1:a/k2f2HQU3Pi399RPW1MOaZyhKJL9w/xFpKAg4q1s0A=
|
||||||
|
github.com/ebitengine/purego v0.9.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||||
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
|
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
|
||||||
@@ -37,10 +41,13 @@ github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17k
|
|||||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/gopxl/beep/v2 v2.1.1 h1:6FYIYMm2qPAdWkjX+7xwKrViS1x0Po5kDMdRkq8NVbU=
|
||||||
|
github.com/gopxl/beep/v2 v2.1.1/go.mod h1:ZAm9TGQ9lvpoiFLd4zf5B1IuyxZhgRACMId1XJbaW0E=
|
||||||
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b h1:WEuQWBxelOGHA6z9lABqaMLMrfwVyMdN3UgRLT+YUPo=
|
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b h1:WEuQWBxelOGHA6z9lABqaMLMrfwVyMdN3UgRLT+YUPo=
|
||||||
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b/go.mod h1:esZFQEUwqC+l76f2R8bIWSwXMaPbp79PppwZ1eJhFco=
|
github.com/gordonklaus/portaudio v0.0.0-20250206071425-98a94950218b/go.mod h1:esZFQEUwqC+l76f2R8bIWSwXMaPbp79PppwZ1eJhFco=
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
|
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
|
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
|
||||||
|
github.com/hajimehoshi/oto/v2 v2.3.1 h1:qrLKpNus2UfD674oxckKjNJmesp9hMh7u7QCrStB3Rc=
|
||||||
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
|
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
@@ -64,6 +71,8 @@ github.com/neurosnap/sentences v1.1.2 h1:iphYOzx/XckXeBiLIUBkPu2EKMJ+6jDbz/sLJZ7
|
|||||||
github.com/neurosnap/sentences v1.1.2/go.mod h1:/pwU4E9XNL21ygMIkOIllv/SMy2ujHwpf8GQPu1YPbQ=
|
github.com/neurosnap/sentences v1.1.2/go.mod h1:/pwU4E9XNL21ygMIkOIllv/SMy2ujHwpf8GQPu1YPbQ=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/playwright-community/playwright-go v0.5700.1 h1:PNFb1byWqrTT720rEO0JL88C6Ju0EmUnR5deFLvtP/U=
|
github.com/playwright-community/playwright-go v0.5700.1 h1:PNFb1byWqrTT720rEO0JL88C6Ju0EmUnR5deFLvtP/U=
|
||||||
github.com/playwright-community/playwright-go v0.5700.1/go.mod h1:MlSn1dZrx8rszbCxY6x3qK89ZesJUYVx21B2JnkoNF0=
|
github.com/playwright-community/playwright-go v0.5700.1/go.mod h1:MlSn1dZrx8rszbCxY6x3qK89ZesJUYVx21B2JnkoNF0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
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