5 Commits

Author SHA1 Message Date
Grail Finder
b67ae1be98 Enha: filter out thinking blocks from chat history, removed {role}: 2026-02-17 13:42:49 +03:00
Grail Finder
372e49199b Feat: collapse/expand thinking blocks with alt+t 2026-02-17 13:15:09 +03:00
Grail Finder
d6d4f09f8d Merge branch 'feat/filepicker-search' 2026-02-17 11:17:17 +03:00
Grail Finder
fa846225ee Enha: remove updatequeue, since it waits for another main action 2026-02-17 10:29:28 +03:00
Grail Finder
7b2fa04391 Fix (img prompt): botname: after <__media__> for /completion 2026-02-17 08:23:08 +03:00
7 changed files with 71 additions and 54 deletions

10
bot.go
View File

@@ -1087,7 +1087,15 @@ func chatToTextSlice(messages []models.RoleMsg, showSys bool) []string {
func chatToText(messages []models.RoleMsg, showSys bool) string {
s := chatToTextSlice(messages, showSys)
return strings.Join(s, "\n")
text := strings.Join(s, "\n")
// Collapse thinking blocks if enabled
if thinkingCollapsed {
placeholder := "[yellow::i][thinking... (press Alt+T to expand)][-:-:-]"
text = thinkRE.ReplaceAllString(text, placeholder)
}
return text
}
func removeThinking(chatBody *models.ChatBody) {

View File

@@ -48,3 +48,4 @@ EnableMouse = false # Enable mouse support in the UI
CharSpecificContextEnabled = true
CharSpecificContextTag = "@"
AutoTurn = true
StripThinkingFromAPI = true # Strip <think> blocks from messages before sending to LLM (keeps them in chat history)

View File

@@ -19,6 +19,7 @@ type Config struct {
ToolRole string `toml:"ToolRole"`
ToolUse bool `toml:"ToolUse"`
ThinkUse bool `toml:"ThinkUse"`
StripThinkingFromAPI bool `toml:"StripThinkingFromAPI"`
AssistantRole string `toml:"AssistantRole"`
SysDir string `toml:"SysDir"`
ChunkLimit uint32 `toml:"ChunkLimit"`

View File

@@ -23,6 +23,25 @@ func isASCII(s string) bool {
return true
}
// stripThinkingFromMsg removes thinking blocks from assistant messages.
// Skips user, tool, and system messages as they may contain thinking examples.
func stripThinkingFromMsg(msg models.RoleMsg) *models.RoleMsg {
if !cfg.StripThinkingFromAPI {
return &msg
}
// Skip user, tool, and system messages - they might contain thinking examples
if msg.Role == cfg.UserRole || msg.Role == cfg.ToolRole || msg.Role == "system" {
return &msg
}
// Strip thinking from assistant messages
if thinkRE.MatchString(msg.Content) {
msg.Content = thinkRE.ReplaceAllString(msg.Content, "")
// Clean up any double newlines that might result
msg.Content = strings.TrimSpace(msg.Content)
}
return &msg
}
// refreshChatDisplay updates the chat display based on current character view
// It filters messages for the character the user is currently "writing as"
// and updates the textView with the filtered conversation

78
llm.go
View File

@@ -13,28 +13,6 @@ var imageAttachmentPath string // Global variable to track image attachment for
var lastImg string // for ctrl+j
var RAGMsg = "Retrieved context for user's query:\n"
// addPersonaSuffixToLastUserMessage adds the persona suffix to the last user message
// to indicate to the assistant who it should reply as
func addPersonaSuffixToLastUserMessage(messages []models.RoleMsg, persona string) []models.RoleMsg {
if len(messages) == 0 {
return messages
}
// // Find the last user message to modify
// for i := len(messages) - 1; i >= 0; i-- {
// if messages[i].Role == cfg.UserRole || messages[i].Role == "user" {
// // Create a copy of the message to avoid modifying the original
// modifiedMsg := messages[i]
// modifiedMsg.Content = modifiedMsg.Content + "\n" + persona + ":"
// messages[i] = modifiedMsg
// break
// }
// }
modifiedMsg := messages[len(messages)-1]
modifiedMsg.Content = modifiedMsg.Content + "\n" + persona + ":\n"
messages[len(messages)-1] = modifiedMsg
return messages
}
// containsToolSysMsg checks if the toolSysMsg already exists in the chat body
func containsToolSysMsg() bool {
for _, msg := range chatBody.Messages {
@@ -187,17 +165,9 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages {
messages[i] = m.ToPrompt()
messages[i] = stripThinkingFromMsg(m).ToPrompt()
}
prompt := strings.Join(messages, "\n")
// strings builder?
if !resume {
botMsgStart := "\n" + botPersona + ":\n"
prompt += botMsgStart
}
if cfg.ThinkUse && !cfg.ToolUse {
prompt += "<think>"
}
// Add multimodal media markers to the prompt text when multimodal data is present
// This is required by llama.cpp multimodal models so they know where to insert media
if len(multimodalData) > 0 {
@@ -209,6 +179,14 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
}
prompt = sb.String()
}
// needs to be after <__media__> if there are images
if !resume {
botMsgStart := "\n" + botPersona + ":\n"
prompt += botMsgStart
}
if cfg.ThinkUse && !cfg.ToolUse {
prompt += "<think>"
}
logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse,
"msg", msg, "resume", resume, "prompt", prompt, "multimodal_data_count", len(multimodalData))
payload := models.NewLCPReq(prompt, chatBody.Model, multimodalData,
@@ -341,23 +319,21 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("LCPChat: RAG message added to chat body", "role", ragMsg.Role,
"rag_content_len", len(ragMsg.Content), "message_count_after_rag", len(chatBody.Messages))
}
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
// 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
if cfg.AutoTurn && !resume {
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
}
bodyCopy := &models.ChatBody{
Messages: make([]models.RoleMsg, len(filteredMessages)),
Model: chatBody.Model,
Stream: chatBody.Stream,
}
for i, msg := range filteredMessages {
if msg.Role == cfg.UserRole {
bodyCopy.Messages[i] = msg
strippedMsg := *stripThinkingFromMsg(msg)
if strippedMsg.Role == cfg.UserRole {
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user"
} else {
bodyCopy.Messages[i] = msg
bodyCopy.Messages[i] = strippedMsg
}
}
// Clean null/empty messages to prevent API issues
@@ -437,7 +413,7 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages {
messages[i] = m.ToPrompt()
messages[i] = stripThinkingFromMsg(m).ToPrompt()
}
prompt := strings.Join(messages, "\n")
// strings builder?
@@ -519,22 +495,20 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
}
// Create copy of chat body with standardized user role
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
// Add persona suffix to the last user message to indicate who the assistant should reply as
if cfg.AutoTurn && !resume {
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
}
bodyCopy := &models.ChatBody{
Messages: make([]models.RoleMsg, len(filteredMessages)),
Model: chatBody.Model,
Stream: chatBody.Stream,
}
for i, msg := range filteredMessages {
if msg.Role == cfg.UserRole || i == 1 {
bodyCopy.Messages[i] = msg
strippedMsg := *stripThinkingFromMsg(msg)
if strippedMsg.Role == cfg.UserRole || i == 1 {
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user"
} else {
bodyCopy.Messages[i] = msg
bodyCopy.Messages[i] = strippedMsg
}
}
// Clean null/empty messages to prevent API issues
@@ -605,7 +579,7 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages {
messages[i] = m.ToPrompt()
messages[i] = stripThinkingFromMsg(m).ToPrompt()
}
prompt := strings.Join(messages, "\n")
// strings builder?
@@ -718,21 +692,19 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
}
// Create copy of chat body with standardized user role
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
// Add persona suffix to the last user message to indicate who the assistant should reply as
if cfg.AutoTurn && !resume {
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
}
bodyCopy := &models.ChatBody{
Messages: make([]models.RoleMsg, len(filteredMessages)),
Model: chatBody.Model,
Stream: chatBody.Stream,
}
for i, msg := range filteredMessages {
bodyCopy.Messages[i] = msg
strippedMsg := *stripThinkingFromMsg(msg)
bodyCopy.Messages[i] = strippedMsg
// Standardize role if it's a user role
if bodyCopy.Messages[i].Role == cfg.UserRole {
bodyCopy.Messages[i] = msg
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user"
}
}

View File

@@ -12,6 +12,7 @@ var (
injectRole = true
selectedIndex = int(-1)
shellMode = false
thinkingCollapsed = false
indexLineCompletion = "F12 to show keys help | llm turn: [%s:-:b]%v[-:-:-] (F6) | chat: [orange:-:b]%s[-:-:-] (F1) | toolUseAdviced: [%s:-:b]%v[-:-:-] (ctrl+k) | model: [orange:-:b]%s[-:-:-] (ctrl+l) | skip LLM resp: [%s:-:b]%v[-:-:-] (F10)\nAPI: [orange:-:b]%s[-:-:-] (ctrl+v) | recording: [%s:-:b]%v[-:-:-] (ctrl+r) | writing as: [orange:-:b]%s[-:-:-] (ctrl+q) | bot will write as [orange:-:b]%s[-:-:-] (ctrl+x) | role injection (alt+7) [%s:-:b]%v[-:-:-]"
focusSwitcher = map[tview.Primitive]tview.Primitive{}
)

15
tui.go
View File

@@ -96,6 +96,7 @@ var (
[yellow]Alt+7[white]: toggle role injection (inject role in messages)
[yellow]Alt+8[white]: show char img or last picked img
[yellow]Alt+9[white]: warm up (load) selected llama.cpp model
[yellow]Alt+t[white]: toggle thinking blocks visibility (collapse/expand <think> blocks)
=== scrolling chat window (some keys similar to vim) ===
[yellow]arrows up/down and j/k[white]: scroll up and down
@@ -831,6 +832,20 @@ func init() {
injectRole = !injectRole
updateStatusLine()
}
// Handle Alt+T to toggle thinking block visibility
if event.Key() == tcell.KeyRune && event.Rune() == 't' && event.Modifiers()&tcell.ModAlt != 0 {
thinkingCollapsed = !thinkingCollapsed
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
status := "expanded"
if thinkingCollapsed {
status = "collapsed"
}
if err := notifyUser("thinking", fmt.Sprintf("Thinking blocks %s", status)); err != nil {
logger.Error("failed to send notification", "error", err)
}
return nil
}
if event.Key() == tcell.KeyF1 {
// chatList, err := loadHistoryChats()
chatList, err := store.GetChatByChar(cfg.AssistantRole)