18 Commits

Author SHA1 Message Date
Grail Finder
1f112259d2 Enha(tools.todo): always provide whole todo list 2026-03-01 07:01:13 +03:00
Grail Finder
a505ffaaa9 Fix (tool): handle subcommands 2026-02-28 16:16:32 +03:00
Grail Finder
32be271aa3 Feat (tools): file_edit 2026-02-28 15:40:52 +03:00
Grail Finder
133ec27938 Feat(shell): cd and pipes support 2026-02-28 13:59:54 +03:00
Grail Finder
d79760a289 Fix: do not delete tool calls or lose them on copy 2026-02-28 10:23:03 +03:00
Grail Finder
2580360f91 Fix: removed code that deletes tool calls 2026-02-28 09:13:05 +03:00
Grail Finder
fe4dd0c982 Enha: add go to allowed commands 2026-02-28 08:39:13 +03:00
Grail Finder
83f99d3577 Enha: first chat name convention 2026-02-28 08:09:56 +03:00
Grail Finder
e521434073 Refactor: move msg totext method to main package
logic requires reference to config
2026-02-28 07:57:49 +03:00
Grail Finder
916c5d3904 Enha: icon for collapsed tools 2026-02-27 21:25:26 +03:00
Grail Finder
5b1cbb46fa Chore: linter complaints 2026-02-27 20:03:47 +03:00
Grail Finder
1fcab8365e Enha: tool filter 2026-02-27 18:45:59 +03:00
Grail Finder
c855c30ae2 Enha: save/load message token stats 2026-02-27 11:23:03 +03:00
Grail Finder
915b029d2c Enha: set work/base dir updates filepicker title 2026-02-27 08:37:13 +03:00
Grail Finder
b599e1ab38 Fix: startnewchat fill created_at 2026-02-27 08:14:41 +03:00
Grail Finder
0d94734090 Enha: tool role index for shellmode 2026-02-27 08:07:55 +03:00
Grail Finder
a0ff384b81 Enha: shellmode within inputfield 2026-02-27 07:58:00 +03:00
Grail Finder
09b5e0d08f Enha: shell mode in filepickerdir 2026-02-26 20:10:00 +03:00
17 changed files with 712 additions and 720 deletions

View File

@@ -1,4 +1,4 @@
.PHONY: setconfig run lint install-linters setup-whisper build-whisper download-whisper-model docker-up docker-down docker-logs noextra-run installdelve checkdelve .PHONY: setconfig run lint lintall install-linters setup-whisper build-whisper download-whisper-model docker-up docker-down docker-logs noextra-run installdelve checkdelve
run: setconfig run: setconfig
go build -tags extra -o gf-lt && ./gf-lt go build -tags extra -o gf-lt && ./gf-lt
@@ -25,7 +25,10 @@ install-linters: ## Install additional linters (noblanks)
go install github.com/GrailFinder/noblanks-linter/cmd/noblanks@latest go install github.com/GrailFinder/noblanks-linter/cmd/noblanks@latest
lint: ## Run linters. Use make install-linters first. lint: ## Run linters. Use make install-linters first.
golangci-lint run -c .golangci.yml ./...; noblanks ./... golangci-lint run -c .golangci.yml ./...
lintall: lint
noblanks ./...
# Whisper STT Setup (in batteries directory) # Whisper STT Setup (in batteries directory)
setup-whisper: build-whisper download-whisper-model setup-whisper: build-whisper download-whisper-model

View File

@@ -71,8 +71,8 @@ func (ag *AgentClient) buildRequest(sysprompt, msg string) ([]byte, error) {
// Build prompt for completion endpoints // Build prompt for completion endpoints
if isCompletion { if isCompletion {
var sb strings.Builder var sb strings.Builder
for _, m := range messages { for i := range messages {
sb.WriteString(m.ToPrompt()) sb.WriteString(messages[i].ToPrompt())
sb.WriteString("\n") sb.WriteString("\n")
} }
prompt := strings.TrimSpace(sb.String()) prompt := strings.TrimSpace(sb.String())

255
bot.go
View File

@@ -66,6 +66,8 @@ var (
LocalModels = []string{} LocalModels = []string{}
) )
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.
// Returns cleaned content and list of character names. // Returns cleaned content and list of character names.
func parseKnownToTag(content string) []string { func parseKnownToTag(content string) []string {
@@ -136,6 +138,9 @@ func processMessageTag(msg *models.RoleMsg) *models.RoleMsg {
// filterMessagesForCharacter returns messages visible to the specified character. // filterMessagesForCharacter returns messages visible to the specified character.
// If CharSpecificContextEnabled is false, returns all messages. // If CharSpecificContextEnabled is false, returns all messages.
func filterMessagesForCharacter(messages []models.RoleMsg, character string) []models.RoleMsg { func filterMessagesForCharacter(messages []models.RoleMsg, character string) []models.RoleMsg {
if strings.Contains(cfg.CurrentAPI, "chat") {
return messages
}
if cfg == nil || !cfg.CharSpecificContextEnabled || character == "" { if cfg == nil || !cfg.CharSpecificContextEnabled || character == "" {
return messages return messages
} }
@@ -143,97 +148,67 @@ func filterMessagesForCharacter(messages []models.RoleMsg, character string) []m
return messages return messages
} }
filtered := make([]models.RoleMsg, 0, len(messages)) filtered := make([]models.RoleMsg, 0, len(messages))
for _, msg := range messages { for i := range messages {
// If KnownTo is nil or empty, message is visible to all // If KnownTo is nil or empty, message is visible to all
// system msg cannot be filtered // system msg cannot be filtered
if len(msg.KnownTo) == 0 || msg.Role == "system" { if len(messages[i].KnownTo) == 0 || messages[i].Role == "system" {
filtered = append(filtered, msg) filtered = append(filtered, messages[i])
continue continue
} }
if slices.Contains(msg.KnownTo, character) { if slices.Contains(messages[i].KnownTo, character) {
// Check if character is in KnownTo lis // Check if character is in KnownTo lis
filtered = append(filtered, msg) filtered = append(filtered, messages[i])
} }
} }
return filtered return filtered
} }
func cleanToolCalls(messages []models.RoleMsg) []models.RoleMsg {
// If AutoCleanToolCallsFromCtx is false, keep tool call messages in context
if cfg != nil && !cfg.AutoCleanToolCallsFromCtx {
return consolidateAssistantMessages(messages)
}
cleaned := make([]models.RoleMsg, 0, len(messages))
for i, msg := range messages {
// recognize the message as the tool call and remove it
// tool call in last msg should stay
if msg.ToolCallID == "" || i == len(messages)-1 {
cleaned = append(cleaned, msg)
}
}
return consolidateAssistantMessages(cleaned)
}
// consolidateAssistantMessages merges consecutive assistant messages into a single message
func consolidateAssistantMessages(messages []models.RoleMsg) []models.RoleMsg { func consolidateAssistantMessages(messages []models.RoleMsg) []models.RoleMsg {
if len(messages) == 0 { if len(messages) == 0 {
return messages return messages
} }
consolidated := make([]models.RoleMsg, 0, len(messages)) result := make([]models.RoleMsg, 0, len(messages))
currentAssistantMsg := models.RoleMsg{} for i := range messages {
isBuildingAssistantMsg := false // Non-assistant messages are appended as-is
for i := 0; i < len(messages); i++ { if messages[i].Role != cfg.AssistantRole {
msg := messages[i] result = append(result, messages[i])
// assistant role only continue
if msg.Role == cfg.AssistantRole { }
// If this is an assistant message, start or continue building // Assistant message: start a new block or merge with the last one
if !isBuildingAssistantMsg { if len(result) == 0 || result[len(result)-1].Role != cfg.AssistantRole {
// Start accumulating assistant message // First assistant in a block: append a copy (avoid mutating input)
currentAssistantMsg = msg.Copy() result = append(result, messages[i].Copy())
isBuildingAssistantMsg = true continue
} else { }
// Continue accumulating - append content to the current assistant message // Merge with the last assistant message
if currentAssistantMsg.IsContentParts() || msg.IsContentParts() { last := &result[len(result)-1]
// Handle structured content // If either message has structured content, unify to ContentParts
if !currentAssistantMsg.IsContentParts() { if last.IsContentParts() || messages[i].IsContentParts() {
// Preserve the original ToolCallID before conversion // Convert last to ContentParts if needed, preserving ToolCallID
originalToolCallID := currentAssistantMsg.ToolCallID if !last.IsContentParts() {
// Convert existing content to content parts toolCallID := last.ToolCallID
currentAssistantMsg = models.NewMultimodalMsg(currentAssistantMsg.Role, []interface{}{models.TextContentPart{Type: "text", Text: currentAssistantMsg.Content}}) *last = models.NewMultimodalMsg(last.Role, []interface{}{
// Restore the original ToolCallID to preserve tool call linking models.TextContentPart{Type: "text", Text: last.Content},
currentAssistantMsg.ToolCallID = originalToolCallID })
} last.ToolCallID = toolCallID
if msg.IsContentParts() { }
currentAssistantMsg.ContentParts = append(currentAssistantMsg.ContentParts, msg.GetContentParts()...) // Add current message's content to last
} else if msg.Content != "" { if messages[i].IsContentParts() {
currentAssistantMsg.AddTextPart(msg.Content) last.ContentParts = append(last.ContentParts, messages[i].GetContentParts()...)
} } else if messages[i].Content != "" {
} else { last.AddTextPart(messages[i].Content)
// Simple string content
if currentAssistantMsg.Content != "" {
currentAssistantMsg.Content += "\n" + msg.Content
} else {
currentAssistantMsg.Content = msg.Content
}
// ToolCallID is already preserved since we're not creating a new message object when just concatenating content
}
} }
} else { } else {
// This is not an assistant message // Both simple strings: concatenate with newline
// If we were building an assistant message, add it to the result if last.Content != "" && messages[i].Content != "" {
if isBuildingAssistantMsg { last.Content += "\n" + messages[i].Content
consolidated = append(consolidated, currentAssistantMsg) } else if messages[i].Content != "" {
isBuildingAssistantMsg = false last.Content = messages[i].Content
} }
// Add the non-assistant message // ToolCallID is already preserved in last
consolidated = append(consolidated, msg)
} }
} }
// Don't forget the last assistant message if we were building one return result
if isBuildingAssistantMsg {
consolidated = append(consolidated, currentAssistantMsg)
}
return consolidated
} }
// GetLogLevel returns the current log level as a string // GetLogLevel returns the current log level as a string
@@ -777,14 +752,14 @@ func showSpinner() {
botPersona = cfg.WriteNextMsgAsCompletionAgent botPersona = cfg.WriteNextMsgAsCompletionAgent
} }
for botRespMode || toolRunningMode { for botRespMode || toolRunningMode {
time.Sleep(100 * time.Millisecond) time.Sleep(400 * time.Millisecond)
spin := i % len(spinners) spin := i % len(spinners)
app.QueueUpdateDraw(func() { app.QueueUpdateDraw(func() {
switch { switch {
case toolRunningMode: case toolRunningMode:
textArea.SetTitle(spinners[spin] + " tool") textArea.SetTitle(spinners[spin] + " tool")
case botRespMode: case botRespMode:
textArea.SetTitle(spinners[spin] + " " + botPersona) textArea.SetTitle(spinners[spin] + " " + botPersona + " (F6 to interrupt)")
default: default:
textArea.SetTitle(spinners[spin] + " input") textArea.SetTitle(spinners[spin] + " input")
} }
@@ -960,7 +935,9 @@ out:
if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil { if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil {
logger.Warn("failed to update storage", "error", err, "name", activeChatName) logger.Warn("failed to update storage", "error", err, "name", activeChatName)
} }
if findCall(respText.String(), toolResp.String()) { // Strip think blocks before parsing for tool calls
respTextNoThink := thinkBlockRE.ReplaceAllString(respText.String(), "")
if findCall(respTextNoThink, toolResp.String()) {
return nil return nil
} }
// Check if this message was sent privately to specific characters // Check if this message was sent privately to specific characters
@@ -982,7 +959,7 @@ func cleanChatBody() {
} }
// 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.Messages = consolidateAssistantMessages(chatBody.Messages)
} }
@@ -1096,22 +1073,38 @@ func findCall(msg, toolCall string) bool {
} }
lastToolCall.Args = openAIToolMap lastToolCall.Args = openAIToolMap
fc = lastToolCall fc = lastToolCall
// Set lastToolCall.ID from parsed tool call ID if available // NOTE: We do NOT override lastToolCall.ID from arguments.
if len(openAIToolMap) > 0 { // The ID should come from the streaming response (chunk.ToolID) set earlier.
if id, exists := openAIToolMap["id"]; exists { // Some tools like todo_create have "id" in their arguments which is NOT the tool call ID.
lastToolCall.ID = id
}
}
} else { } else {
jsStr := toolCallRE.FindString(msg) jsStr := toolCallRE.FindString(msg)
if jsStr == "" { // no tool call case if jsStr == "" { // no tool call case
return false return false
} }
prefix := "__tool_call__\n" // Remove prefix/suffix with flexible whitespace handling
suffix := "\n__tool_call__" jsStr = strings.TrimSpace(jsStr)
jsStr = strings.TrimSuffix(strings.TrimPrefix(jsStr, prefix), suffix) jsStr = strings.TrimPrefix(jsStr, "__tool_call__")
jsStr = strings.TrimSuffix(jsStr, "__tool_call__")
jsStr = strings.TrimSpace(jsStr)
// HTML-decode the JSON string to handle encoded characters like &lt; -> <= // HTML-decode the JSON string to handle encoded characters like &lt; -> <=
decodedJsStr := html.UnescapeString(jsStr) decodedJsStr := html.UnescapeString(jsStr)
// Try to find valid JSON bounds (first { to last })
start := strings.Index(decodedJsStr, "{")
end := strings.LastIndex(decodedJsStr, "}")
if start == -1 || end == -1 || end <= start {
logger.Error("failed to find valid JSON in tool call", "json_string", decodedJsStr)
toolResponseMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: "Error processing tool call: no valid JSON found. Please check the JSON format.",
}
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
crr := &models.ChatRoundReq{
Role: cfg.AssistantRole,
}
chatRoundChan <- crr
return true
}
decodedJsStr = decodedJsStr[start : end+1]
var err error var err error
fc, err = unmarshalFuncCall(decodedJsStr) fc, err = unmarshalFuncCall(decodedJsStr)
if err != nil { if err != nil {
@@ -1138,14 +1131,18 @@ func findCall(msg, toolCall string) bool {
lastToolCall.Args = fc.Args lastToolCall.Args = fc.Args
} }
// 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)
// make sure it has ToolCallID // Use the tool call ID from streaming response (lastToolCall.ID)
if chatBody.Messages[len(chatBody.Messages)-1].ToolCallID == "" { // Don't generate random ID - the ID should match between assistant message and tool response
// Tool call IDs should be alphanumeric strings with length 9! lastMsgIdx := len(chatBody.Messages) - 1
chatBody.Messages[len(chatBody.Messages)-1].ToolCallID = randString(9) if lastToolCall.ID != "" {
chatBody.Messages[lastMsgIdx].ToolCallID = lastToolCall.ID
} }
// Ensure lastToolCall.ID is set, fallback to assistant message's ToolCallID // Store tool call info in the assistant message
if lastToolCall.ID == "" { // Convert Args map to JSON string for storage
lastToolCall.ID = chatBody.Messages[len(chatBody.Messages)-1].ToolCallID chatBody.Messages[lastMsgIdx].ToolCall = &models.ToolCall{
ID: lastToolCall.ID,
Name: lastToolCall.Name,
Args: mapToString(lastToolCall.Args),
} }
// call a func // call a func
_, ok := fnMap[fc.Name] _, ok := fnMap[fc.Name]
@@ -1175,15 +1172,18 @@ func findCall(msg, toolCall string) bool {
toolRunningMode = true toolRunningMode = true
resp := callToolWithAgent(fc.Name, fc.Args) resp := callToolWithAgent(fc.Name, fc.Args)
toolRunningMode = false toolRunningMode = false
toolMsg := string(resp) // Remove the "tool response: " prefix and %+v formatting toolMsg := string(resp)
logger.Info("llm used a tool call", "tool_name", fc.Name, "too_args", fc.Args, "id", fc.ID, "tool_resp", toolMsg) logger.Info("llm used a tool call", "tool_name", fc.Name, "too_args", fc.Args, "id", fc.ID, "tool_resp", toolMsg)
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, toolMsg) "\n\n", len(chatBody.Messages), cfg.ToolRole, toolMsg)
// Create tool response message with the proper tool_call_id // Create tool response message with the proper tool_call_id
// Mark shell commands as always visible
isShellCommand := fc.Name == "execute_command"
toolResponseMsg := models.RoleMsg{ toolResponseMsg := models.RoleMsg{
Role: cfg.ToolRole, Role: cfg.ToolRole,
Content: toolMsg, Content: toolMsg,
ToolCallID: lastToolCall.ID, // Use the stored tool call ID ToolCallID: lastToolCall.ID,
IsShellCommand: isShellCommand,
} }
chatBody.Messages = append(chatBody.Messages, toolResponseMsg) chatBody.Messages = append(chatBody.Messages, 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", len(chatBody.Messages))
@@ -1200,12 +1200,42 @@ func findCall(msg, toolCall string) bool {
func chatToTextSlice(messages []models.RoleMsg, showSys bool) []string { func chatToTextSlice(messages []models.RoleMsg, showSys bool) []string {
resp := make([]string, len(messages)) resp := make([]string, len(messages))
for i, msg := range messages { for i := range messages {
// INFO: skips system msg and tool msg icon := fmt.Sprintf("[-:-:b](%d) <%s>:[-:-:-]", i, messages[i].Role)
if !showSys && (msg.Role == cfg.ToolRole || msg.Role == "system") { // Handle tool call indicators (assistant messages with tool call but empty content)
if messages[i].Role == cfg.AssistantRole && messages[i].ToolCall != nil && messages[i].ToolCall.ID != "" {
// This is a tool call indicator - show collapsed
if toolCollapsed {
toolName := messages[i].ToolCall.Name
resp[i] = fmt.Sprintf("%s\n[yellow::i][tool call: %s (press Ctrl+T to expand)][-:-:-]\n", icon, toolName)
} else {
// Show full tool call info
toolName := messages[i].ToolCall.Name
resp[i] = fmt.Sprintf("%s\n%s\n[yellow::i][tool call: %s][-:-:-]\nargs: %s\nid: %s\n", icon, messages[i].GetText(), toolName, messages[i].ToolCall.Args, messages[i].ToolCall.ID)
}
continue continue
} }
resp[i] = msg.ToText(i) // Handle tool responses
if messages[i].Role == cfg.ToolRole || messages[i].Role == "tool" {
// Always show shell commands
if messages[i].IsShellCommand {
resp[i] = MsgToText(i, &messages[i])
continue
}
// Hide non-shell tool responses when collapsed
if toolCollapsed {
resp[i] = icon + "\n[yellow::i][tool resp (press Ctrl+T to expand)][-:-:-]\n"
continue
}
// When expanded, show tool responses
resp[i] = MsgToText(i, &messages[i])
continue
}
// INFO: skips system msg when showSys is false
if !showSys && messages[i].Role == "system" {
continue
}
resp[i] = MsgToText(i, &messages[i])
} }
return resp return resp
} }
@@ -1239,20 +1269,6 @@ func chatToText(messages []models.RoleMsg, showSys bool) string {
return text return text
} }
func removeThinking(chatBody *models.ChatBody) {
msgs := []models.RoleMsg{}
for _, msg := range chatBody.Messages {
// Filter out tool messages and thinking markers
if msg.Role == cfg.ToolRole {
continue
}
// find thinking and remove it - use SetText to preserve ContentParts
msg.SetText(thinkRE.ReplaceAllString(msg.GetText(), ""))
msgs = append(msgs, msg)
}
chatBody.Messages = msgs
}
func addNewChat(chatName string) { func addNewChat(chatName string) {
id, err := store.ChatGetMaxID() id, err := store.ChatGetMaxID()
if err != nil { if err != nil {
@@ -1374,15 +1390,6 @@ func init() {
os.Exit(1) os.Exit(1)
return return
} }
// Set image base directory for path display
baseDir := cfg.FilePickerDir
if baseDir == "" || baseDir == "." {
// Resolve "." to current working directory
if wd, err := os.Getwd(); err == nil {
baseDir = wd
}
}
models.SetImageBaseDir(baseDir)
defaultStarter = []models.RoleMsg{ defaultStarter = []models.RoleMsg{
{Role: "system", Content: basicSysMsg}, {Role: "system", Content: basicSysMsg},
{Role: cfg.AssistantRole, Content: defaultFirstMsg}, {Role: cfg.AssistantRole, Content: defaultFirstMsg},
@@ -1397,8 +1404,6 @@ func init() {
} }
// load cards // load cards
basicCard.Role = cfg.AssistantRole basicCard.Role = cfg.AssistantRole
// toolCard.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}))
store = storage.NewProviderSQL(cfg.DBPATH, logger) store = storage.NewProviderSQL(cfg.DBPATH, logger)

View File

@@ -27,7 +27,6 @@ AutoCleanToolCallsFromCtx = false
RAGEnabled = false RAGEnabled = false
RAGBatchSize = 1 RAGBatchSize = 1
RAGWordLimit = 80 RAGWordLimit = 80
RAGWorkers = 2
RAGDir = "ragimport" RAGDir = "ragimport"
# extra tts # extra tts
TTS_ENABLED = false TTS_ENABLED = false

View File

@@ -39,7 +39,6 @@ type Config struct {
// rag settings // rag settings
RAGEnabled bool `toml:"RAGEnabled"` RAGEnabled bool `toml:"RAGEnabled"`
RAGDir string `toml:"RAGDir"` RAGDir string `toml:"RAGDir"`
RAGWorkers uint32 `toml:"RAGWorkers"`
RAGBatchSize int `toml:"RAGBatchSize"` RAGBatchSize int `toml:"RAGBatchSize"`
RAGWordLimit uint32 `toml:"RAGWordLimit"` RAGWordLimit uint32 `toml:"RAGWordLimit"`
// deepseek // deepseek

View File

@@ -80,9 +80,6 @@ This document explains how to set up and configure the application using the `co
#### RAGWordLimit (`80`) #### RAGWordLimit (`80`)
- Maximum number of words in a batch to tokenize and store. - Maximum number of words in a batch to tokenize and store.
#### RAGWorkers (`2`)
- Number of concurrent workers for RAG processing.
#### RAGDir (`"ragimport"`) #### RAGDir (`"ragimport"`)
- Directory containing documents for RAG processing. - Directory containing documents for RAG processing.

View File

@@ -15,8 +15,6 @@ import (
"time" "time"
"unicode" "unicode"
"math/rand/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@@ -68,6 +66,14 @@ func isASCII(s string) bool {
return true return true
} }
func mapToString[V any](m map[string]V) string {
rs := strings.Builder{}
for k, v := range m {
fmt.Fprintf(&rs, "%v: %v\n", k, v)
}
return rs.String()
}
// stripThinkingFromMsg removes thinking blocks from assistant messages. // stripThinkingFromMsg removes thinking blocks from assistant messages.
// Skips user, tool, and system messages as they may contain thinking examples. // Skips user, tool, and system messages as they may contain thinking examples.
func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg { func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
@@ -211,8 +217,10 @@ func startNewChat(keepSysP bool) {
chatBody.Messages = chatBody.Messages[:2] chatBody.Messages = chatBody.Messages[:2]
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) textView.SetText(chatToText(chatBody.Messages, 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),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
// chat is written to db when we get first llm response (or any) // chat is written to db when we get first llm response (or any)
// actual chat history (messages) would be parsed then // actual chat history (messages) would be parsed then
Msgs: "", Msgs: "",
@@ -357,7 +365,7 @@ func makeStatusLine() string {
} }
// Get model color based on load status for local llama.cpp models // Get model color based on load status for local llama.cpp models
modelColor := getModelColor() modelColor := getModelColor()
statusLine := fmt.Sprintf(statusLineTempl, boolColors[botRespMode], activeChatName, statusLine := fmt.Sprintf(statusLineTempl, activeChatName,
boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp], boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp],
cfg.CurrentAPI, persona, botPersona) cfg.CurrentAPI, persona, botPersona)
if cfg.STT_ENABLED { if cfg.STT_ENABLED {
@@ -373,16 +381,6 @@ func makeStatusLine() string {
return statusLine + imageInfo + shellModeInfo return statusLine + imageInfo + shellModeInfo
} }
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.IntN(len(letters))]
}
return string(b)
}
// set of roles within card definition and mention in chat history // set of roles within card definition and mention in chat history
func listChatRoles() []string { func listChatRoles() []string {
currentChat, ok := chatMap[activeChatName] currentChat, ok := chatMap[activeChatName]
@@ -426,12 +424,11 @@ func deepseekModelValidator() error {
func toggleShellMode() { func toggleShellMode() {
shellMode = !shellMode shellMode = !shellMode
setShellMode(shellMode)
if shellMode { if shellMode {
// Update input placeholder to indicate shell mode shellInput.SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir))
textArea.SetPlaceholder("SHELL MODE: Enter command and press <Esc> to execute")
} else { } else {
// Reset to normal mode textArea.SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message.")
textArea.SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message. Alt+1 to exit shell mode")
} }
updateStatusLine() updateStatusLine()
} }
@@ -443,23 +440,29 @@ func updateFlexLayout() {
} }
flex.Clear() flex.Clear()
flex.AddItem(textView, 0, 40, false) flex.AddItem(textView, 0, 40, false)
flex.AddItem(textArea, 0, 10, false) if shellMode {
flex.AddItem(shellInput, 0, 10, false)
} else {
flex.AddItem(textArea, 0, 10, false)
}
if positionVisible { if positionVisible {
flex.AddItem(statusLineWidget, 0, 2, false) flex.AddItem(statusLineWidget, 0, 2, false)
} }
// Keep focus on currently focused widget // Keep focus on currently focused widget
focused := app.GetFocus() focused := app.GetFocus()
if focused == textView { switch {
case focused == textView:
app.SetFocus(textView) app.SetFocus(textView)
} else { case shellMode:
app.SetFocus(shellInput)
default:
app.SetFocus(textArea) app.SetFocus(textArea)
} }
} }
func executeCommandAndDisplay(cmdText string) { func executeCommandAndDisplay(cmdText string) {
// Parse the command (split by spaces, but handle quoted arguments) cmdText = strings.TrimSpace(cmdText)
cmdParts := parseCommand(cmdText) if cmdText == "" {
if len(cmdParts) == 0 {
fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n") fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n")
if scrollToEndEnabled { if scrollToEndEnabled {
textView.ScrollToEnd() textView.ScrollToEnd()
@@ -467,17 +470,63 @@ func executeCommandAndDisplay(cmdText string) {
colorText() colorText()
return return
} }
command := cmdParts[0] workingDir := cfg.FilePickerDir
args := []string{} // Handle cd command specially to update working directory
if len(cmdParts) > 1 { if strings.HasPrefix(cmdText, "cd ") {
args = cmdParts[1:] newDir := strings.TrimPrefix(cmdText, "cd ")
newDir = strings.TrimSpace(newDir)
// Handle cd ~ or cdHOME
if strings.HasPrefix(newDir, "~") {
home := os.Getenv("HOME")
newDir = strings.Replace(newDir, "~", home, 1)
}
// Check if directory exists
if _, err := os.Stat(newDir); err == nil {
workingDir = newDir
cfg.FilePickerDir = workingDir
// Update shell input label with new directory
shellInput.SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir))
outputContent := workingDir
// Add the command being executed to the chat
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), cfg.ToolRole, cmdText)
fmt.Fprintf(textView, "%s\n", outputContent)
combinedMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: "$ " + cmdText + "\n\n" + outputContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
if scrollToEndEnabled {
textView.ScrollToEnd()
}
colorText()
return
} else {
outputContent := "cd: " + newDir + ": No such file or directory"
fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), cfg.ToolRole, cmdText)
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputContent)
combinedMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: "$ " + cmdText + "\n\n" + outputContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
if scrollToEndEnabled {
textView.ScrollToEnd()
}
colorText()
return
}
} }
// Create the command execution
cmd := exec.Command(command, args...) // Use /bin/sh to support pipes, redirects, etc.
cmd := exec.Command("/bin/sh", "-c", cmdText)
cmd.Dir = workingDir
// Execute the command and get output // Execute the command and get output
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[yellow]$ %s[-:-:-]\n", cmdText) fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n",
len(chatBody.Messages), 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
@@ -514,42 +563,11 @@ func executeCommandAndDisplay(cmdText string) {
textView.ScrollToEnd() textView.ScrollToEnd()
} }
colorText() colorText()
} // Add command to history (avoid duplicates at the end)
if len(shellHistory) == 0 || shellHistory[len(shellHistory)-1] != cmdText {
// parseCommand splits command string handling quotes properly shellHistory = append(shellHistory, cmdText)
func parseCommand(cmd string) []string {
var args []string
var current string
var inQuotes bool
var quoteChar rune
for _, r := range cmd {
switch r {
case '"', '\'':
if inQuotes {
if r == quoteChar {
inQuotes = false
} else {
current += string(r)
}
} else {
inQuotes = true
quoteChar = r
}
case ' ', '\t':
if inQuotes {
current += string(r)
} else if current != "" {
args = append(args, current)
current = ""
}
default:
current += string(r)
}
} }
if current != "" { shellHistoryPos = -1
args = append(args, current)
}
return args
} }
// == search == // == search ==
@@ -791,3 +809,91 @@ func scanFiles(dir, filter string) []string {
scanRecursive(dir, 0, "") scanRecursive(dir, 0, "")
return files return files
} }
// models logic that is too complex for models package
func MsgToText(i int, m *models.RoleMsg) string {
var contentStr string
var imageIndicators []string
if !m.HasContentParts {
contentStr = m.Content
} else {
var textParts []string
for _, part := range m.ContentParts {
switch p := part.(type) {
case models.TextContentPart:
if p.Type == "text" {
textParts = append(textParts, p.Text)
}
case models.ImageContentPart:
displayPath := p.Path
if displayPath == "" {
displayPath = "image"
} else {
displayPath = extractDisplayPath(displayPath, cfg.FilePickerDir)
}
imageIndicators = append(imageIndicators, fmt.Sprintf("[orange::i][image: %s][-:-:-]", displayPath))
case map[string]any:
if partType, exists := p["type"]; exists {
switch partType {
case "text":
if textVal, textExists := p["text"]; textExists {
if textStr, isStr := textVal.(string); isStr {
textParts = append(textParts, textStr)
}
}
case "image_url":
var displayPath string
if pathVal, pathExists := p["path"]; pathExists {
if pathStr, isStr := pathVal.(string); isStr && pathStr != "" {
displayPath = extractDisplayPath(pathStr, cfg.FilePickerDir)
}
}
if displayPath == "" {
displayPath = "image"
}
imageIndicators = append(imageIndicators, fmt.Sprintf("[orange::i][image: %s][-:-:-]", displayPath))
}
}
}
}
contentStr = strings.Join(textParts, " ") + " "
}
contentStr, _ = strings.CutPrefix(contentStr, m.Role+":")
icon := fmt.Sprintf("(%d) <%s>: ", i, m.Role)
var finalContent strings.Builder
if len(imageIndicators) > 0 {
for _, indicator := range imageIndicators {
finalContent.WriteString(indicator)
finalContent.WriteString("\n")
}
}
finalContent.WriteString(contentStr)
if m.Stats != nil {
fmt.Fprintf(&finalContent, "\n[gray::i][%d tok, %.1fs, %.1f t/s][-:-:-]", m.Stats.Tokens, m.Stats.Duration, m.Stats.TokensPerSec)
}
textMsg := fmt.Sprintf("[-:-:b]%s[-:-:-]\n%s\n", icon, finalContent.String())
return strings.ReplaceAll(textMsg, "\n\n", "\n")
}
// extractDisplayPath returns a path suitable for display, potentially relative to imageBaseDir
func extractDisplayPath(p, bp string) string {
if p == "" {
return ""
}
// If base directory is set, try to make path relative to it
if bp != "" {
if rel, err := filepath.Rel(bp, p); err == nil {
// Check if relative path doesn't start with ".." (meaning it's within base dir)
// If it starts with "..", we might still want to show it as relative
// but for now we show full path if it goes outside base dir
if !strings.HasPrefix(rel, "..") {
p = rel
}
}
}
// Truncate long paths to last 60 characters if needed
if len(p) > 60 {
return "..." + p[len(p)-60:]
}
return p
}

76
llm.go
View File

@@ -14,8 +14,8 @@ 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 _, msg := range chatBody.Messages { for i := range chatBody.Messages {
if msg.Role == cfg.ToolRole && msg.Content == toolSysMsg { if chatBody.Messages[i].Role == cfg.ToolRole && chatBody.Messages[i].Content == toolSysMsg {
return true return true
} }
} }
@@ -147,8 +147,8 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
} }
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages)) messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages { for i := range filteredMessages {
messages[i] = stripThinkingFromMsg(&m).ToPrompt() messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
} }
prompt := strings.Join(messages, "\n") prompt := strings.Join(messages, "\n")
// Add multimodal media markers to the prompt text when multimodal data is present // Add multimodal media markers to the prompt text when multimodal data is present
@@ -289,14 +289,23 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
Model: chatBody.Model, Model: chatBody.Model,
Stream: chatBody.Stream, Stream: chatBody.Stream,
} }
for i, msg := range filteredMessages { for i := range filteredMessages {
strippedMsg := *stripThinkingFromMsg(&msg) strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
if strippedMsg.Role == cfg.UserRole { switch strippedMsg.Role {
case cfg.UserRole:
bodyCopy.Messages[i] = strippedMsg bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user" bodyCopy.Messages[i].Role = "user"
} else { case cfg.AssistantRole:
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "assistant"
case cfg.ToolRole:
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "tool"
default:
bodyCopy.Messages[i] = strippedMsg bodyCopy.Messages[i] = strippedMsg
} }
// Clear ToolCalls - they're stored in chat history for display but not sent to LLM
// bodyCopy.Messages[i].ToolCall = nil
} }
// Clean null/empty messages to prevent API issues // Clean null/empty messages to prevent API issues
bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages)
@@ -358,8 +367,8 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
} }
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages)) messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages { for i := range filteredMessages {
messages[i] = stripThinkingFromMsg(&m).ToPrompt() messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
} }
prompt := strings.Join(messages, "\n") prompt := strings.Join(messages, "\n")
// strings builder? // strings builder?
@@ -429,14 +438,27 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
Model: chatBody.Model, Model: chatBody.Model,
Stream: chatBody.Stream, Stream: chatBody.Stream,
} }
for i, msg := range filteredMessages { for i := range filteredMessages {
strippedMsg := *stripThinkingFromMsg(&msg) strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
if strippedMsg.Role == cfg.UserRole || i == 1 { switch strippedMsg.Role {
case cfg.UserRole:
if i == 1 {
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user"
} else {
bodyCopy.Messages[i] = strippedMsg
}
case cfg.AssistantRole:
bodyCopy.Messages[i] = strippedMsg bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user" bodyCopy.Messages[i].Role = "assistant"
} else { case cfg.ToolRole:
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "tool"
default:
bodyCopy.Messages[i] = strippedMsg bodyCopy.Messages[i] = strippedMsg
} }
// Clear ToolCalls - they're stored in chat history for display but not sent to LLM
// bodyCopy.Messages[i].ToolCall = nil
} }
// Clean null/empty messages to prevent API issues // Clean null/empty messages to prevent API issues
bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages)
@@ -489,8 +511,8 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
} }
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages)) messages := make([]string, len(filteredMessages))
for i, m := range filteredMessages { for i := range filteredMessages {
messages[i] = stripThinkingFromMsg(&m).ToPrompt() messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt()
} }
prompt := strings.Join(messages, "\n") prompt := strings.Join(messages, "\n")
// strings builder? // strings builder?
@@ -593,14 +615,24 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
Model: chatBody.Model, Model: chatBody.Model,
Stream: chatBody.Stream, Stream: chatBody.Stream,
} }
for i, msg := range filteredMessages { for i := range filteredMessages {
strippedMsg := *stripThinkingFromMsg(&msg) strippedMsg := *stripThinkingFromMsg(&filteredMessages[i])
bodyCopy.Messages[i] = strippedMsg switch strippedMsg.Role {
// Standardize role if it's a user role case cfg.UserRole:
if bodyCopy.Messages[i].Role == cfg.UserRole {
bodyCopy.Messages[i] = strippedMsg bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "user" bodyCopy.Messages[i].Role = "user"
case cfg.AssistantRole:
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "assistant"
case cfg.ToolRole:
bodyCopy.Messages[i] = strippedMsg
bodyCopy.Messages[i].Role = "tool"
default:
bodyCopy.Messages[i] = strippedMsg
} }
// Clear ToolCalls - they're stored in chat history for display but not sent to LLM
// literally deletes data that we need
// bodyCopy.Messages[i].ToolCall = nil
} }
// Clean null/empty messages to prevent API issues // Clean null/empty messages to prevent API issues
bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages)

View File

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

View File

@@ -1,42 +0,0 @@
package main
import (
"fmt"
"gf-lt/config"
"gf-lt/models"
"strings"
"testing"
)
func TestRemoveThinking(t *testing.T) {
cases := []struct {
cb *models.ChatBody
toolMsgs uint8
}{
{cb: &models.ChatBody{
Stream: true,
Messages: []models.RoleMsg{
{Role: "tool", Content: "should be ommited"},
{Role: "system", Content: "should stay"},
{Role: "user", Content: "hello, how are you?"},
{Role: "assistant", Content: "Oh, hi. <think>I should thank user and continue the conversation</think> I am geat, thank you! How are you?"},
},
},
toolMsgs: uint8(1),
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("run_%d", i), func(t *testing.T) {
cfg = &config.Config{ToolRole: "tool"} // Initialize cfg.ToolRole for test
mNum := len(tc.cb.Messages)
removeThinking(tc.cb)
if len(tc.cb.Messages) != mNum-int(tc.toolMsgs) {
t.Errorf("failed to delete tools msg %v; expected %d, got %d", tc.cb.Messages, mNum-int(tc.toolMsgs), len(tc.cb.Messages))
}
for _, msg := range tc.cb.Messages {
if strings.Contains(msg.Content, "<think>") {
t.Errorf("msg contains think tag; msg: %s\n", msg.Content)
}
}
}) }
}

View File

@@ -5,28 +5,21 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
) )
var (
// imageBaseDir is the base directory for displaying image paths.
// If set, image paths will be shown relative to this directory.
imageBaseDir = ""
)
// SetImageBaseDir sets the base directory for displaying image paths.
// If dir is empty, full paths will be shown.
func SetImageBaseDir(dir string) {
imageBaseDir = dir
}
type FuncCall struct { type FuncCall struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Args map[string]string `json:"args"` Args map[string]string `json:"args"`
} }
type ToolCall struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Args string `json:"arguments"`
}
type LLMResp struct { type LLMResp struct {
Choices []struct { Choices []struct {
FinishReason string `json:"finish_reason"` FinishReason string `json:"finish_reason"`
@@ -108,40 +101,56 @@ type RoleMsg struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"-"` Content string `json:"-"`
ContentParts []any `json:"-"` ContentParts []any `json:"-"`
ToolCallID string `json:"tool_call_id,omitempty"` // For tool response messages ToolCallID string `json:"tool_call_id,omitempty"` // For tool response messages
ToolCall *ToolCall `json:"tool_call,omitempty"` // For assistant messages with tool calls
IsShellCommand bool `json:"is_shell_command,omitempty"` // True for shell command outputs (always shown)
KnownTo []string `json:"known_to,omitempty"` KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats"` Stats *ResponseStats `json:"stats"`
hasContentParts bool // Flag to indicate which content type to marshal HasContentParts bool // Flag to indicate which content type to marshal
} }
// MarshalJSON implements custom JSON marshaling for RoleMsg // MarshalJSON implements custom JSON marshaling for RoleMsg
func (m *RoleMsg) MarshalJSON() ([]byte, error) { //
if m.hasContentParts { //nolint:gocritic
func (m RoleMsg) MarshalJSON() ([]byte, error) {
if m.HasContentParts {
// Use structured content format // Use structured content format
aux := struct { aux := struct {
Role string `json:"role"` Role string `json:"role"`
Content []any `json:"content"` Content []any `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` ToolCall *ToolCall `json:"tool_call,omitempty"`
IsShellCommand bool `json:"is_shell_command,omitempty"`
KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
}{ }{
Role: m.Role, Role: m.Role,
Content: m.ContentParts, Content: m.ContentParts,
ToolCallID: m.ToolCallID, ToolCallID: m.ToolCallID,
KnownTo: m.KnownTo, ToolCall: m.ToolCall,
IsShellCommand: m.IsShellCommand,
KnownTo: m.KnownTo,
Stats: m.Stats,
} }
return json.Marshal(aux) return json.Marshal(aux)
} else { } else {
// Use simple content format // Use simple content format
aux := struct { aux := struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"content"` Content string `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` ToolCall *ToolCall `json:"tool_call,omitempty"`
IsShellCommand bool `json:"is_shell_command,omitempty"`
KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
}{ }{
Role: m.Role, Role: m.Role,
Content: m.Content, Content: m.Content,
ToolCallID: m.ToolCallID, ToolCallID: m.ToolCallID,
KnownTo: m.KnownTo, ToolCall: m.ToolCall,
IsShellCommand: m.IsShellCommand,
KnownTo: m.KnownTo,
Stats: m.Stats,
} }
return json.Marshal(aux) return json.Marshal(aux)
} }
@@ -151,26 +160,35 @@ func (m *RoleMsg) MarshalJSON() ([]byte, error) {
func (m *RoleMsg) UnmarshalJSON(data []byte) error { func (m *RoleMsg) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as structured content format // First, try to unmarshal as structured content format
var structured struct { var structured struct {
Role string `json:"role"` Role string `json:"role"`
Content []any `json:"content"` Content []any `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` ToolCall *ToolCall `json:"tool_call,omitempty"`
IsShellCommand bool `json:"is_shell_command,omitempty"`
KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
} }
if err := json.Unmarshal(data, &structured); err == nil && len(structured.Content) > 0 { if err := json.Unmarshal(data, &structured); err == nil && len(structured.Content) > 0 {
m.Role = structured.Role m.Role = structured.Role
m.ContentParts = structured.Content m.ContentParts = structured.Content
m.ToolCallID = structured.ToolCallID m.ToolCallID = structured.ToolCallID
m.ToolCall = structured.ToolCall
m.IsShellCommand = structured.IsShellCommand
m.KnownTo = structured.KnownTo m.KnownTo = structured.KnownTo
m.hasContentParts = true m.Stats = structured.Stats
m.HasContentParts = true
return nil return nil
} }
// Otherwise, unmarshal as simple content format // Otherwise, unmarshal as simple content format
var simple struct { var simple struct {
Role string `json:"role"` Role string `json:"role"`
Content string `json:"content"` Content string `json:"content"`
ToolCallID string `json:"tool_call_id,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"`
KnownTo []string `json:"known_to,omitempty"` ToolCall *ToolCall `json:"tool_call,omitempty"`
IsShellCommand bool `json:"is_shell_command,omitempty"`
KnownTo []string `json:"known_to,omitempty"`
Stats *ResponseStats `json:"stats,omitempty"`
} }
if err := json.Unmarshal(data, &simple); err != nil { if err := json.Unmarshal(data, &simple); err != nil {
return err return err
@@ -178,78 +196,17 @@ func (m *RoleMsg) UnmarshalJSON(data []byte) error {
m.Role = simple.Role m.Role = simple.Role
m.Content = simple.Content m.Content = simple.Content
m.ToolCallID = simple.ToolCallID m.ToolCallID = simple.ToolCallID
m.ToolCall = simple.ToolCall
m.IsShellCommand = simple.IsShellCommand
m.KnownTo = simple.KnownTo m.KnownTo = simple.KnownTo
m.hasContentParts = false m.Stats = simple.Stats
m.HasContentParts = false
return nil return nil
} }
func (m *RoleMsg) ToText(i int) string {
var contentStr string
var imageIndicators []string
if !m.hasContentParts {
contentStr = m.Content
} else {
var textParts []string
for _, part := range m.ContentParts {
switch p := part.(type) {
case TextContentPart:
if p.Type == "text" {
textParts = append(textParts, p.Text)
}
case ImageContentPart:
displayPath := p.Path
if displayPath == "" {
displayPath = "image"
} else {
displayPath = extractDisplayPath(displayPath)
}
imageIndicators = append(imageIndicators, fmt.Sprintf("[orange::i][image: %s][-:-:-]", displayPath))
case map[string]any:
if partType, exists := p["type"]; exists {
switch partType {
case "text":
if textVal, textExists := p["text"]; textExists {
if textStr, isStr := textVal.(string); isStr {
textParts = append(textParts, textStr)
}
}
case "image_url":
var displayPath string
if pathVal, pathExists := p["path"]; pathExists {
if pathStr, isStr := pathVal.(string); isStr && pathStr != "" {
displayPath = extractDisplayPath(pathStr)
}
}
if displayPath == "" {
displayPath = "image"
}
imageIndicators = append(imageIndicators, fmt.Sprintf("[orange::i][image: %s][-:-:-]", displayPath))
}
}
}
}
contentStr = strings.Join(textParts, " ") + " "
}
contentStr, _ = strings.CutPrefix(contentStr, m.Role+":")
icon := fmt.Sprintf("(%d) <%s>: ", i, m.Role)
var finalContent strings.Builder
if len(imageIndicators) > 0 {
for _, indicator := range imageIndicators {
finalContent.WriteString(indicator)
finalContent.WriteString("\n")
}
}
finalContent.WriteString(contentStr)
if m.Stats != nil {
fmt.Fprintf(&finalContent, "\n[gray::i][%d tok, %.1fs, %.1f t/s][-:-:-]", m.Stats.Tokens, m.Stats.Duration, m.Stats.TokensPerSec)
}
textMsg := fmt.Sprintf("[-:-:b]%s[-:-:-]\n%s\n", icon, finalContent.String())
return strings.ReplaceAll(textMsg, "\n\n", "\n")
}
func (m *RoleMsg) ToPrompt() string { func (m *RoleMsg) ToPrompt() string {
var contentStr string var contentStr string
if !m.hasContentParts { if !m.HasContentParts {
contentStr = m.Content contentStr = m.Content
} else { } else {
// For structured content, just take the text parts // For structured content, just take the text parts
@@ -282,7 +239,7 @@ func NewRoleMsg(role, content string) RoleMsg {
return RoleMsg{ return RoleMsg{
Role: role, Role: role,
Content: content, Content: content,
hasContentParts: false, HasContentParts: false,
} }
} }
@@ -291,7 +248,7 @@ func NewMultimodalMsg(role string, contentParts []any) RoleMsg {
return RoleMsg{ return RoleMsg{
Role: role, Role: role,
ContentParts: contentParts, ContentParts: contentParts,
hasContentParts: true, HasContentParts: true,
} }
} }
@@ -300,7 +257,7 @@ func (m *RoleMsg) HasContent() bool {
if m.Content != "" { if m.Content != "" {
return true return true
} }
if m.hasContentParts && len(m.ContentParts) > 0 { if m.HasContentParts && len(m.ContentParts) > 0 {
return true return true
} }
return false return false
@@ -308,7 +265,7 @@ func (m *RoleMsg) HasContent() bool {
// IsContentParts returns true if the message uses structured content parts // IsContentParts returns true if the message uses structured content parts
func (m *RoleMsg) IsContentParts() bool { func (m *RoleMsg) IsContentParts() bool {
return m.hasContentParts return m.HasContentParts
} }
// GetContentParts returns the content parts of the message // GetContentParts returns the content parts of the message
@@ -325,14 +282,16 @@ func (m *RoleMsg) Copy() RoleMsg {
ToolCallID: m.ToolCallID, ToolCallID: m.ToolCallID,
KnownTo: m.KnownTo, KnownTo: m.KnownTo,
Stats: m.Stats, Stats: m.Stats,
hasContentParts: m.hasContentParts, HasContentParts: m.HasContentParts,
ToolCall: m.ToolCall,
IsShellCommand: m.IsShellCommand,
} }
} }
// GetText returns the text content of the message, handling both // GetText returns the text content of the message, handling both
// simple Content and multimodal ContentParts formats. // simple Content and multimodal ContentParts formats.
func (m *RoleMsg) GetText() string { func (m *RoleMsg) GetText() string {
if !m.hasContentParts { if !m.HasContentParts {
return m.Content return m.Content
} }
var textParts []string var textParts []string
@@ -361,7 +320,7 @@ func (m *RoleMsg) GetText() string {
// ContentParts (multimodal), it updates the text parts while preserving // ContentParts (multimodal), it updates the text parts while preserving
// images. If not, it sets the simple Content field. // images. If not, it sets the simple Content field.
func (m *RoleMsg) SetText(text string) { func (m *RoleMsg) SetText(text string) {
if !m.hasContentParts { if !m.HasContentParts {
m.Content = text m.Content = text
return return
} }
@@ -391,14 +350,14 @@ func (m *RoleMsg) SetText(text string) {
// AddTextPart adds a text content part to the message // AddTextPart adds a text content part to the message
func (m *RoleMsg) AddTextPart(text string) { func (m *RoleMsg) AddTextPart(text string) {
if !m.hasContentParts { if !m.HasContentParts {
// Convert to content parts format // Convert to content parts format
if m.Content != "" { if m.Content != "" {
m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}} m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}}
} else { } else {
m.ContentParts = []any{} m.ContentParts = []any{}
} }
m.hasContentParts = true m.HasContentParts = true
} }
textPart := TextContentPart{Type: "text", Text: text} textPart := TextContentPart{Type: "text", Text: text}
m.ContentParts = append(m.ContentParts, textPart) m.ContentParts = append(m.ContentParts, textPart)
@@ -406,14 +365,14 @@ func (m *RoleMsg) AddTextPart(text string) {
// AddImagePart adds an image content part to the message // AddImagePart adds an image content part to the message
func (m *RoleMsg) AddImagePart(imageURL, imagePath string) { func (m *RoleMsg) AddImagePart(imageURL, imagePath string) {
if !m.hasContentParts { if !m.HasContentParts {
// Convert to content parts format // Convert to content parts format
if m.Content != "" { if m.Content != "" {
m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}} m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}}
} else { } else {
m.ContentParts = []any{} m.ContentParts = []any{}
} }
m.hasContentParts = true m.HasContentParts = true
} }
imagePart := ImageContentPart{ imagePart := ImageContentPart{
Type: "image_url", Type: "image_url",
@@ -457,31 +416,6 @@ func CreateImageURLFromPath(imagePath string) (string, error) {
return fmt.Sprintf("data:%s;base64,%s", mimeType, encoded), nil return fmt.Sprintf("data:%s;base64,%s", mimeType, encoded), nil
} }
// extractDisplayPath returns a path suitable for display, potentially relative to imageBaseDir
func extractDisplayPath(p string) string {
if p == "" {
return ""
}
// If base directory is set, try to make path relative to it
if imageBaseDir != "" {
if rel, err := filepath.Rel(imageBaseDir, p); err == nil {
// Check if relative path doesn't start with ".." (meaning it's within base dir)
// If it starts with "..", we might still want to show it as relative
// but for now we show full path if it goes outside base dir
if !strings.HasPrefix(rel, "..") {
p = rel
}
}
}
// Truncate long paths to last 60 characters if needed
if len(p) > 60 {
return "..." + p[len(p)-60:]
}
return p
}
type ChatBody struct { type ChatBody struct {
Model string `json:"model"` Model string `json:"model"`
Stream bool `json:"stream"` Stream bool `json:"stream"`
@@ -489,16 +423,16 @@ type ChatBody struct {
} }
func (cb *ChatBody) Rename(oldname, newname string) { func (cb *ChatBody) Rename(oldname, newname string) {
for i, m := range cb.Messages { for i := range cb.Messages {
cb.Messages[i].Content = strings.ReplaceAll(m.Content, oldname, newname) cb.Messages[i].Content = strings.ReplaceAll(cb.Messages[i].Content, oldname, newname)
cb.Messages[i].Role = strings.ReplaceAll(m.Role, oldname, newname) cb.Messages[i].Role = strings.ReplaceAll(cb.Messages[i].Role, oldname, newname)
} }
} }
func (cb *ChatBody) ListRoles() []string { func (cb *ChatBody) ListRoles() []string {
namesMap := make(map[string]struct{}) namesMap := make(map[string]struct{})
for _, m := range cb.Messages { for i := range cb.Messages {
namesMap[m.Role] = struct{}{} namesMap[cb.Messages[i].Role] = struct{}{}
} }
resp := make([]string, len(namesMap)) resp := make([]string, len(namesMap))
i := 0 i := 0

View File

@@ -1,161 +0,0 @@
package models
import (
"strings"
"testing"
)
func TestRoleMsgToTextWithImages(t *testing.T) {
tests := []struct {
name string
msg RoleMsg
index int
expected string // substring to check
}{
{
name: "text and image",
index: 0,
msg: func() RoleMsg {
msg := NewMultimodalMsg("user", []interface{}{})
msg.AddTextPart("Look at this picture")
msg.AddImagePart("data:image/jpeg;base64,abc123", "/home/user/Pictures/cat.jpg")
return msg
}(),
expected: "[orange::i][image: /home/user/Pictures/cat.jpg][-:-:-]",
},
{
name: "image only",
index: 1,
msg: func() RoleMsg {
msg := NewMultimodalMsg("user", []interface{}{})
msg.AddImagePart("data:image/png;base64,xyz789", "/tmp/screenshot_20250217_123456.png")
return msg
}(),
expected: "[orange::i][image: /tmp/screenshot_20250217_123456.png][-:-:-]",
},
{
name: "long filename truncated",
index: 2,
msg: func() RoleMsg {
msg := NewMultimodalMsg("user", []interface{}{})
msg.AddTextPart("Check this")
msg.AddImagePart("data:image/jpeg;base64,foo", "/very/long/path/to/a/really_long_filename_that_exceeds_forty_characters.jpg")
return msg
}(),
expected: "[orange::i][image: .../to/a/really_long_filename_that_exceeds_forty_characters.jpg][-:-:-]",
},
{
name: "multiple images",
index: 3,
msg: func() RoleMsg {
msg := NewMultimodalMsg("user", []interface{}{})
msg.AddTextPart("Multiple images")
msg.AddImagePart("data:image/jpeg;base64,a", "/path/img1.jpg")
msg.AddImagePart("data:image/png;base64,b", "/path/img2.png")
return msg
}(),
expected: "[orange::i][image: /path/img1.jpg][-:-:-]\n[orange::i][image: /path/img2.png][-:-:-]",
},
{
name: "old format without path",
index: 4,
msg: RoleMsg{
Role: "user",
hasContentParts: true,
ContentParts: []interface{}{
map[string]interface{}{
"type": "image_url",
"image_url": map[string]interface{}{
"url": "data:image/jpeg;base64,old",
},
},
},
},
expected: "[orange::i][image: image][-:-:-]",
},
{
name: "old format with path",
index: 5,
msg: RoleMsg{
Role: "user",
hasContentParts: true,
ContentParts: []interface{}{
map[string]interface{}{
"type": "image_url",
"path": "/old/path/photo.jpg",
"image_url": map[string]interface{}{
"url": "data:image/jpeg;base64,old",
},
},
},
},
expected: "[orange::i][image: /old/path/photo.jpg][-:-:-]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.msg.ToText(tt.index)
if !strings.Contains(result, tt.expected) {
t.Errorf("ToText() result does not contain expected indicator\ngot: %s\nwant substring: %s", result, tt.expected)
}
// Ensure the indicator appears before text content
if strings.Contains(tt.expected, "cat.jpg") && strings.Contains(result, "Look at this picture") {
indicatorPos := strings.Index(result, "[orange::i][image: /home/user/Pictures/cat.jpg][-:-:-]")
textPos := strings.Index(result, "Look at this picture")
if indicatorPos == -1 || textPos == -1 || indicatorPos >= textPos {
t.Errorf("image indicator should appear before text")
}
}
})
}
}
func TestExtractDisplayPath(t *testing.T) {
// Save original base dir
originalBaseDir := imageBaseDir
defer func() { imageBaseDir = originalBaseDir }()
tests := []struct {
name string
baseDir string
path string
expected string
}{
{
name: "no base dir shows full path",
baseDir: "",
path: "/home/user/images/cat.jpg",
expected: "/home/user/images/cat.jpg",
},
{
name: "relative path within base dir",
baseDir: "/home/user",
path: "/home/user/images/cat.jpg",
expected: "images/cat.jpg",
},
{
name: "path outside base dir shows full path",
baseDir: "/home/user",
path: "/tmp/test.jpg",
expected: "/tmp/test.jpg",
},
{
name: "same directory",
baseDir: "/home/user/images",
path: "/home/user/images/cat.jpg",
expected: "cat.jpg",
},
{
name: "long path truncated",
baseDir: "",
path: "/very/long/path/to/a/really_long_filename_that_exceeds_sixty_characters_limit_yes_it_is_very_long.jpg",
expected: "..._that_exceeds_sixty_characters_limit_yes_it_is_very_long.jpg",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
imageBaseDir = tt.baseDir
result := extractDisplayPath(tt.path)
if result != tt.expected {
t.Errorf("extractDisplayPath(%q) with baseDir=%q = %q, want %q",
tt.path, tt.baseDir, result, tt.expected)
}
})
}
}

View File

@@ -343,7 +343,7 @@ func showBotRoleSelectionPopup() {
app.SetFocus(roleListWidget) app.SetFocus(roleListWidget)
} }
func showFileCompletionPopup(filter string) { func showShellFileCompletionPopup(filter string) {
baseDir := cfg.FilePickerDir baseDir := cfg.FilePickerDir
if baseDir == "" { if baseDir == "" {
baseDir = "." baseDir = "."
@@ -352,13 +352,12 @@ func showFileCompletionPopup(filter string) {
if len(complMatches) == 0 { if len(complMatches) == 0 {
return return
} }
// If only one match, auto-complete without showing popup
if len(complMatches) == 1 { if len(complMatches) == 1 {
currentText := textArea.GetText() currentText := shellInput.GetText()
atIdx := strings.LastIndex(currentText, "@") atIdx := strings.LastIndex(currentText, "@")
if atIdx >= 0 { if atIdx >= 0 {
before := currentText[:atIdx] before := currentText[:atIdx]
textArea.SetText(before+complMatches[0], true) shellInput.SetText(before + complMatches[0])
} }
return return
} }
@@ -369,24 +368,24 @@ func showFileCompletionPopup(filter string) {
widget.AddItem(m, "", 0, nil) widget.AddItem(m, "", 0, nil)
} }
widget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { widget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
currentText := textArea.GetText() currentText := shellInput.GetText()
atIdx := strings.LastIndex(currentText, "@") atIdx := strings.LastIndex(currentText, "@")
if atIdx >= 0 { if atIdx >= 0 {
before := currentText[:atIdx] before := currentText[:atIdx]
textArea.SetText(before+mainText, true) shellInput.SetText(before + mainText)
} }
pages.RemovePage("fileCompletionPopup") pages.RemovePage("shellFileCompletionPopup")
app.SetFocus(textArea) app.SetFocus(shellInput)
}) })
widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape { if event.Key() == tcell.KeyEscape {
pages.RemovePage("fileCompletionPopup") pages.RemovePage("shellFileCompletionPopup")
app.SetFocus(textArea) app.SetFocus(shellInput)
return nil return nil
} }
if event.Key() == tcell.KeyRune && event.Rune() == 'x' { if event.Key() == tcell.KeyRune && event.Rune() == 'x' {
pages.RemovePage("fileCompletionPopup") pages.RemovePage("shellFileCompletionPopup")
app.SetFocus(textArea) app.SetFocus(shellInput)
return nil return nil
} }
return event return event
@@ -400,8 +399,7 @@ func showFileCompletionPopup(filter string) {
AddItem(nil, 0, 1, false), width, 1, true). AddItem(nil, 0, 1, false), width, 1, true).
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
} }
// Add modal page and make it visible pages.AddPage("shellFileCompletionPopup", modal(widget, 80, 20), true, true)
pages.AddPage("fileCompletionPopup", modal(widget, 80, 20), true, true)
app.SetFocus(widget) app.SetFocus(widget)
} }

View File

@@ -131,13 +131,18 @@ func loadOldChatOrGetNew() []models.RoleMsg {
chat, err := store.GetLastChat() chat, err := store.GetLastChat()
if err != nil { if err != nil {
logger.Warn("failed to load history chat", "error", err) logger.Warn("failed to load history chat", "error", err)
maxID, err := store.ChatGetMaxID()
if err != nil {
logger.Error("failed to fetch max chat id", "error", err)
}
maxID++
chat := &models.Chat{ chat := &models.Chat{
ID: 0, ID: maxID,
CreatedAt: time.Now(), CreatedAt: time.Now(),
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
Agent: cfg.AssistantRole, Agent: cfg.AssistantRole,
} }
chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.CreatedAt.Unix()) chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.ID)
activeChatName = chat.Name activeChatName = chat.Name
chatMap[chat.Name] = chat chatMap[chat.Name] = chat
return defaultStarter return defaultStarter
@@ -149,10 +154,6 @@ func loadOldChatOrGetNew() []models.RoleMsg {
chatMap[chat.Name] = chat chatMap[chat.Name] = chat
return defaultStarter return defaultStarter
} }
// if chat.Name == "" {
// logger.Warn("empty chat name", "id", chat.ID)
// chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.CreatedAt.Unix())
// }
chatMap[chat.Name] = chat chatMap[chat.Name] = chat
activeChatName = chat.Name activeChatName = chat.Name
cfg.AssistantRole = chat.Agent cfg.AssistantRole = chat.Agent

View File

@@ -1130,35 +1130,11 @@ func makeFilePicker() *tview.Flex {
} }
if event.Rune() == 's' { if event.Rune() == 's' {
// Set FilePickerDir to current directory // Set FilePickerDir to current directory
itemIndex := listView.GetCurrentItem() // Get the actual directory path
if itemIndex >= 0 && itemIndex < listView.GetItemCount() { cfg.FilePickerDir = currentDisplayDir
itemText, _ := listView.GetItemText(itemIndex) listView.SetTitle("Files & Directories [s: set FilePickerDir]. Current base dir: " + cfg.FilePickerDir)
// Get the actual directory path // pages.RemovePage(filePickerPage)
var targetDir string return nil
if strings.HasPrefix(itemText, "Exit") || strings.HasPrefix(itemText, "Select this directory") {
targetDir = currentDisplayDir
} else {
actualItemName := itemText
if bracketPos := strings.Index(itemText, " ["); bracketPos != -1 {
actualItemName = itemText[:bracketPos]
}
// nolint: gocritic
if strings.HasPrefix(actualItemName, "../") {
targetDir = path.Dir(currentDisplayDir)
} else if strings.HasSuffix(actualItemName, "/") {
dirName := strings.TrimSuffix(actualItemName, "/")
targetDir = path.Join(currentDisplayDir, dirName)
} else {
targetDir = currentDisplayDir
}
}
cfg.FilePickerDir = targetDir
if err := notifyUser("FilePickerDir", "Set to: "+targetDir); err != nil {
logger.Error("failed to notify user", "error", err)
}
// pages.RemovePage(filePickerPage)
return nil
}
} }
case tcell.KeyEnter: case tcell.KeyEnter:
// Get the currently highlighted item in the list // Get the currently highlighted item in the list

202
tools.go
View File

@@ -95,6 +95,11 @@ Your current tools:
"when_to_use": "when asked to append content to a file; use sed to edit content" "when_to_use": "when asked to append content to a file; use sed to edit content"
}, },
{ {
"name":"file_edit",
"args": ["path", "oldString", "newString", "lineNumber"],
"when_to_use": "when you need to make targeted changes to a specific section of a file without rewriting the entire file; lineNumber is optional - if provided, only edits that specific line; if not provided, replaces all occurrences of oldString"
},
{
"name":"file_delete", "name":"file_delete",
"args": ["path"], "args": ["path"],
"when_to_use": "when asked to delete a file" "when_to_use": "when asked to delete a file"
@@ -117,7 +122,7 @@ Your current tools:
{ {
"name":"execute_command", "name":"execute_command",
"args": ["command", "args"], "args": ["command", "args"],
"when_to_use": "when asked to execute a system command; args is optional; allowed commands: grep, sed, awk, find, cat, head, tail, sort, uniq, wc, ls, echo, cut, tr, cp, mv, rm, mkdir, rmdir, pwd, df, free, ps, top, du, whoami, date, uname" "when_to_use": "when asked to execute a system command; args is optional; allowed commands: grep, sed, awk, find, cat, head, tail, sort, uniq, wc, ls, echo, cut, tr, cp, mv, rm, mkdir, rmdir, pwd, df, free, ps, top, du, whoami, date, uname, go"
} }
] ]
</tools> </tools>
@@ -506,6 +511,85 @@ func fileWriteAppend(args map[string]string) []byte {
return []byte(msg) return []byte(msg)
} }
func fileEdit(args map[string]string) []byte {
path, ok := args["path"]
if !ok || path == "" {
msg := "path not provided to file_edit tool"
logger.Error(msg)
return []byte(msg)
}
path = resolvePath(path)
oldString, ok := args["oldString"]
if !ok || oldString == "" {
msg := "oldString not provided to file_edit tool"
logger.Error(msg)
return []byte(msg)
}
newString, ok := args["newString"]
if !ok {
newString = ""
}
lineNumberStr, hasLineNumber := args["lineNumber"]
// Read file content
content, err := os.ReadFile(path)
if err != nil {
msg := "failed to read file: " + err.Error()
logger.Error(msg)
return []byte(msg)
}
fileContent := string(content)
var replacementCount int
if hasLineNumber && lineNumberStr != "" {
// Line-number based edit
lineNum, err := strconv.Atoi(lineNumberStr)
if err != nil {
msg := "invalid lineNumber: must be a valid integer"
logger.Error(msg)
return []byte(msg)
}
lines := strings.Split(fileContent, "\n")
if lineNum < 1 || lineNum > len(lines) {
msg := fmt.Sprintf("lineNumber %d out of range (file has %d lines)", lineNum, len(lines))
logger.Error(msg)
return []byte(msg)
}
// Find oldString in the specific line
targetLine := lines[lineNum-1]
if !strings.Contains(targetLine, oldString) {
msg := fmt.Sprintf("oldString not found on line %d", lineNum)
logger.Error(msg)
return []byte(msg)
}
lines[lineNum-1] = strings.Replace(targetLine, oldString, newString, 1)
replacementCount = 1
fileContent = strings.Join(lines, "\n")
} else {
// Replace all occurrences
if !strings.Contains(fileContent, oldString) {
msg := "oldString not found in file"
logger.Error(msg)
return []byte(msg)
}
fileContent = strings.ReplaceAll(fileContent, oldString, newString)
replacementCount = strings.Count(fileContent, newString)
}
if err := os.WriteFile(path, []byte(fileContent), 0644); err != nil {
msg := "failed to write file: " + err.Error()
logger.Error(msg)
return []byte(msg)
}
msg := fmt.Sprintf("file edited successfully at %s (%d replacement(s))", path, replacementCount)
return []byte(msg)
}
func fileDelete(args map[string]string) []byte { func fileDelete(args map[string]string) []byte {
path, ok := args["path"] path, ok := args["path"]
if !ok || path == "" { if !ok || path == "" {
@@ -705,6 +789,16 @@ func executeCommand(args map[string]string) []byte {
argNum++ argNum++
} }
} }
// Handle commands passed as single string with spaces (e.g., "go run main.go")
// Split into base command and arguments
if strings.Contains(command, " ") {
parts := strings.Fields(command)
baseCmd := parts[0]
extraArgs := parts[1:]
// Prepend extra args to cmdArgs
cmdArgs = append(extraArgs, cmdArgs...)
command = baseCmd
}
if !isCommandAllowed(command, cmdArgs...) { if !isCommandAllowed(command, cmdArgs...) {
msg := fmt.Sprintf("command '%s' is not allowed", command) msg := fmt.Sprintf("command '%s' is not allowed", command)
logger.Error(msg) logger.Error(msg)
@@ -714,6 +808,7 @@ func executeCommand(args map[string]string) []byte {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, command, cmdArgs...) cmd := exec.CommandContext(ctx, command, cmdArgs...)
cmd.Dir = cfg.FilePickerDir
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
msg := fmt.Sprintf("command '%s' failed; error: %v; output: %s", command, err, string(output)) msg := fmt.Sprintf("command '%s' failed; error: %v; output: %s", command, err, string(output))
@@ -739,6 +834,14 @@ type TodoList struct {
Items []TodoItem `json:"items"` Items []TodoItem `json:"items"`
} }
func (t TodoList) ToString() string {
sb := strings.Builder{}
for i := range t.Items {
fmt.Fprintf(&sb, "\n[%s] %s. %s\n", t.Items[i].Status, t.Items[i].ID, t.Items[i].Task)
}
return sb.String()
}
// Global todo list storage // Global todo list storage
var globalTodoList = TodoList{ var globalTodoList = TodoList{
Items: []TodoItem{}, Items: []TodoItem{},
@@ -765,6 +868,7 @@ func todoCreate(args map[string]string) []byte {
"id": id, "id": id,
"task": task, "task": task,
"status": "pending", "status": "pending",
"todos": globalTodoList.ToString(),
} }
jsonResult, err := json.Marshal(result) jsonResult, err := json.Marshal(result)
if err != nil { if err != nil {
@@ -776,38 +880,9 @@ func todoCreate(args map[string]string) []byte {
} }
func todoRead(args map[string]string) []byte { func todoRead(args map[string]string) []byte {
id, ok := args["id"]
if ok && id != "" {
// Find specific todo by ID
for _, item := range globalTodoList.Items {
if item.ID == id {
result := map[string]interface{}{
"todo": item,
}
jsonResult, err := json.Marshal(result)
if err != nil {
msg := "failed to marshal result; error: " + err.Error()
logger.Error(msg)
return []byte(msg)
}
return jsonResult
}
}
// ID not found
result := map[string]string{
"error": fmt.Sprintf("todo with id %s not found", id),
}
jsonResult, err := json.Marshal(result)
if err != nil {
msg := "failed to marshal result; error: " + err.Error()
logger.Error(msg)
return []byte(msg)
}
return jsonResult
}
// Return all todos if no ID specified // Return all todos if no ID specified
result := map[string]interface{}{ result := map[string]interface{}{
"todos": globalTodoList.Items, "todos": globalTodoList.ToString(),
} }
jsonResult, err := json.Marshal(result) jsonResult, err := json.Marshal(result)
if err != nil { if err != nil {
@@ -858,6 +933,7 @@ func todoUpdate(args map[string]string) []byte {
result := map[string]string{ result := map[string]string{
"message": "todo updated successfully", "message": "todo updated successfully",
"id": id, "id": id,
"todos": globalTodoList.ToString(),
} }
jsonResult, err := json.Marshal(result) jsonResult, err := json.Marshal(result)
if err != nil { if err != nil {
@@ -896,6 +972,7 @@ func todoDelete(args map[string]string) []byte {
result := map[string]string{ result := map[string]string{
"message": "todo deleted successfully", "message": "todo deleted successfully",
"id": id, "id": id,
"todos": globalTodoList.ToString(),
} }
jsonResult, err := json.Marshal(result) jsonResult, err := json.Marshal(result)
if err != nil { if err != nil {
@@ -962,13 +1039,18 @@ func isCommandAllowed(command string, args ...string) bool {
"date": true, "date": true,
"uname": true, "uname": true,
"git": true, "git": true,
"go": true,
} }
if !allowedCommands[command] { // Allow all go subcommands (go run, go mod tidy, go test, etc.)
return false if strings.HasPrefix(command, "go ") && allowedCommands["go"] {
return true
} }
if command == "git" && len(args) > 0 { if command == "git" && len(args) > 0 {
return gitReadSubcommands[args[0]] return gitReadSubcommands[args[0]]
} }
if !allowedCommands[command] {
return false
}
return true return true
} }
@@ -996,6 +1078,7 @@ var fnMap = map[string]fnSig{
"file_read": fileRead, "file_read": fileRead,
"file_write": fileWrite, "file_write": fileWrite,
"file_write_append": fileWriteAppend, "file_write_append": fileWriteAppend,
"file_edit": fileEdit,
"file_delete": fileDelete, "file_delete": fileDelete,
"file_move": fileMove, "file_move": fileMove,
"file_copy": fileCopy, "file_copy": fileCopy,
@@ -1263,6 +1346,36 @@ var baseTools = []models.Tool{
}, },
}, },
}, },
// file_edit
models.Tool{
Type: "function",
Function: models.ToolFunc{
Name: "file_edit",
Description: "Edit a specific section of a file by replacing oldString with newString. Use for targeted changes without rewriting the entire file.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"path", "oldString", "newString"},
Properties: map[string]models.ToolArgProps{
"path": models.ToolArgProps{
Type: "string",
Description: "path of the file to edit",
},
"oldString": models.ToolArgProps{
Type: "string",
Description: "the exact string to find and replace",
},
"newString": models.ToolArgProps{
Type: "string",
Description: "the string to replace oldString with",
},
"lineNumber": models.ToolArgProps{
Type: "string",
Description: "optional line number (1-indexed) to edit - if provided, only that line is edited",
},
},
},
},
},
// file_delete // file_delete
models.Tool{ models.Tool{
Type: "function", Type: "function",
@@ -1348,14 +1461,14 @@ var baseTools = []models.Tool{
Type: "function", Type: "function",
Function: models.ToolFunc{ Function: models.ToolFunc{
Name: "execute_command", Name: "execute_command",
Description: "Execute a shell command safely. Use when you need to run system commands like grep sed awk find cat head tail sort uniq wc ls echo cut tr cp mv rm mkdir rmdir pwd df free ps top du whoami date uname. Git is allowed for read-only operations: status, log, diff, show, branch, reflog, rev-parse, shortlog, describe.", Description: "Execute a shell command safely. Use when you need to run system commands like grep sed awk find cat head tail sort uniq wc ls echo cut tr cp mv rm mkdir rmdir pwd df free ps top du whoami date uname go. Git is allowed for read-only operations: status, log, diff, show, branch, reflog, rev-parse, shortlog, describe.",
Parameters: models.ToolFuncParams{ Parameters: models.ToolFuncParams{
Type: "object", Type: "object",
Required: []string{"command"}, Required: []string{"command"},
Properties: map[string]models.ToolArgProps{ Properties: map[string]models.ToolArgProps{
"command": models.ToolArgProps{ "command": models.ToolArgProps{
Type: "string", Type: "string",
Description: "command to execute (only commands from whitelist are allowed: grep sed awk find cat head tail sort uniq wc ls echo cut tr cp mv rm mkdir rmdir pwd df free ps top du whoami date uname; git allowed for reads: status log diff show branch reflog rev-parse shortlog describe)", Description: "command to execute (only commands from whitelist are allowed: grep sed awk find cat head tail sort uniq wc ls echo cut tr cp mv rm mkdir rmdir pwd df free ps top du whoami date uname go; git allowed for reads: status log diff show branch reflog rev-parse shortlog describe)",
}, },
"args": models.ToolArgProps{ "args": models.ToolArgProps{
Type: "string", Type: "string",
@@ -1408,22 +1521,9 @@ var baseTools = []models.Tool{
Name: "todo_update", Name: "todo_update",
Description: "Update a todo item by ID with new task or status. Status must be one of: pending, in_progress, completed.", Description: "Update a todo item by ID with new task or status. Status must be one of: pending, in_progress, completed.",
Parameters: models.ToolFuncParams{ Parameters: models.ToolFuncParams{
Type: "object", Type: "object",
Required: []string{"id"}, Required: []string{},
Properties: map[string]models.ToolArgProps{ Properties: map[string]models.ToolArgProps{},
"id": models.ToolArgProps{
Type: "string",
Description: "id of the todo item to update",
},
"task": models.ToolArgProps{
Type: "string",
Description: "new task description (optional)",
},
"status": models.ToolArgProps{
Type: "string",
Description: "new status for the todo: pending, in_progress, or completed (optional)",
},
},
}, },
}, },
}, },

126
tui.go
View File

@@ -34,6 +34,7 @@ var (
indexPickWindow *tview.InputField indexPickWindow *tview.InputField
renameWindow *tview.InputField renameWindow *tview.InputField
roleEditWindow *tview.InputField roleEditWindow *tview.InputField
shellInput *tview.InputField
fullscreenMode bool fullscreenMode bool
positionVisible bool = true positionVisible bool = true
scrollToEndEnabled bool = true scrollToEndEnabled bool = true
@@ -79,7 +80,7 @@ var (
[yellow]Ctrl+p[white]: props edit form (min-p, dry, etc.) [yellow]Ctrl+p[white]: props edit form (min-p, dry, etc.)
[yellow]Ctrl+v[white]: show API link selection popup to choose current API [yellow]Ctrl+v[white]: show API link selection popup to choose current API
[yellow]Ctrl+r[white]: start/stop recording from your microphone (needs stt server or whisper binary) [yellow]Ctrl+r[white]: start/stop recording from your microphone (needs stt server or whisper binary)
[yellow]Ctrl+t[white]: remove thinking (<think>) and tool messages from context (delete from chat) [yellow]Ctrl+t[white]: (un)collapse tool messages
[yellow]Ctrl+l[white]: show model selection popup to choose current model [yellow]Ctrl+l[white]: show model selection popup to choose current model
[yellow]Ctrl+k[white]: switch tool use (recommend tool use to llm after user msg) [yellow]Ctrl+k[white]: switch tool use (recommend tool use to llm after user msg)
[yellow]Ctrl+a[white]: interrupt tts (needs tts server) [yellow]Ctrl+a[white]: interrupt tts (needs tts server)
@@ -98,6 +99,7 @@ var (
[yellow]Alt+8[white]: show char img or last picked img [yellow]Alt+8[white]: show char img or last picked img
[yellow]Alt+9[white]: warm up (load) selected llama.cpp model [yellow]Alt+9[white]: warm up (load) selected llama.cpp model
[yellow]Alt+t[white]: toggle thinking blocks visibility (collapse/expand <think> blocks) [yellow]Alt+t[white]: toggle thinking blocks visibility (collapse/expand <think> blocks)
[yellow]Ctrl+t[white]: toggle tool call/response visibility (collapse/expand tool calls and non-shell tool responses)
[yellow]Alt+i[white]: show colorscheme selection popup [yellow]Alt+i[white]: show colorscheme selection popup
=== scrolling chat window (some keys similar to vim) === === scrolling chat window (some keys similar to vim) ===
@@ -124,46 +126,78 @@ Press <Enter> or 'x' to return
` `
) )
func setShellMode(enabled bool) {
shellMode = enabled
go func() {
app.QueueUpdateDraw(func() {
updateFlexLayout()
})
}()
}
func init() { func init() {
// Start background goroutine to update model color cache // Start background goroutine to update model color cache
startModelColorUpdater() startModelColorUpdater()
tview.Styles = colorschemes["default"] tview.Styles = colorschemes["default"]
app = tview.NewApplication() app = tview.NewApplication()
pages = tview.NewPages() pages = tview.NewPages()
textArea = tview.NewTextArea(). shellInput = tview.NewInputField().
SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message.") SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir)). // dynamic prompt
textArea.SetBorder(true).SetTitle("input") SetFieldWidth(0).
// Add input capture for @ completion SetDoneFunc(func(key tcell.Key) {
textArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if key == tcell.KeyEnter {
cmd := shellInput.GetText()
if cmd != "" {
executeCommandAndDisplay(cmd)
}
shellInput.SetText("")
}
})
// Copy your file completion logic to shellInput's InputCapture
shellInput.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if !shellMode { if !shellMode {
return event return event
} }
// Handle Tab key for file completion // Handle Up arrow for history previous
if event.Key() == tcell.KeyTab { if event.Key() == tcell.KeyUp {
currentText := textArea.GetText() if len(shellHistory) > 0 {
row, col, _, _ := textArea.GetCursor() if shellHistoryPos < len(shellHistory)-1 {
// Calculate absolute position from row/col shellHistoryPos++
lines := strings.Split(currentText, "\n") shellInput.SetText(shellHistory[len(shellHistory)-1-shellHistoryPos])
cursorPos := 0
for i := 0; i < row && i < len(lines); i++ {
cursorPos += len(lines[i]) + 1 // +1 for newline
}
cursorPos += col
// Look backwards from cursor to find @
if cursorPos > 0 {
// Find the last @ before cursor
textBeforeCursor := currentText[:cursorPos]
atIndex := strings.LastIndex(textBeforeCursor, "@")
if atIndex >= 0 {
// Extract the partial match text after @
filter := textBeforeCursor[atIndex+1:]
showFileCompletionPopup(filter)
return nil // Consume the Tab event
} }
} }
return nil
}
// Handle Down arrow for history next
if event.Key() == tcell.KeyDown {
if shellHistoryPos > 0 {
shellHistoryPos--
shellInput.SetText(shellHistory[len(shellHistory)-1-shellHistoryPos])
} else if shellHistoryPos == 0 {
shellHistoryPos = -1
shellInput.SetText("")
}
return nil
}
// Reset history position when user types
if event.Key() == tcell.KeyRune {
shellHistoryPos = -1
}
// Handle Tab key for @ file completion
if event.Key() == tcell.KeyTab {
currentText := shellInput.GetText()
atIndex := strings.LastIndex(currentText, "@")
if atIndex >= 0 {
filter := currentText[atIndex+1:]
showShellFileCompletionPopup(filter)
}
return nil
} }
return event return event
}) })
textArea = tview.NewTextArea().
SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message.")
textArea.SetBorder(true).SetTitle("input")
textView = tview.NewTextView(). textView = tview.NewTextView().
SetDynamicColors(true). SetDynamicColors(true).
SetRegions(true). SetRegions(true).
@@ -530,6 +564,20 @@ func init() {
} }
return nil return nil
} }
// Handle Ctrl+T to toggle tool call/response visibility
if event.Key() == tcell.KeyCtrlT {
toolCollapsed = !toolCollapsed
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
status := "expanded"
if toolCollapsed {
status = "collapsed"
}
if err := notifyUser("tools", "Tool calls/responses "+status); err != nil {
logger.Error("failed to send notification", "error", err)
}
return nil
}
if event.Key() == tcell.KeyRune && event.Rune() == 'i' && event.Modifiers()&tcell.ModAlt != 0 { if event.Key() == tcell.KeyRune && event.Rune() == 'i' && event.Modifiers()&tcell.ModAlt != 0 {
if isFullScreenPageActive() { if isFullScreenPageActive() {
return event return event
@@ -748,14 +796,6 @@ func init() {
showModelSelectionPopup() showModelSelectionPopup()
return nil return nil
} }
if event.Key() == tcell.KeyCtrlT {
// clear context
// remove tools and thinking
removeThinking(chatBody)
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
return nil
}
if event.Key() == tcell.KeyCtrlV { if event.Key() == tcell.KeyCtrlV {
if isFullScreenPageActive() { if isFullScreenPageActive() {
return event return event
@@ -948,14 +988,16 @@ func init() {
} }
// cannot send msg in editMode or botRespMode // cannot send msg in editMode or botRespMode
if event.Key() == tcell.KeyEscape && !editMode && !botRespMode { if event.Key() == tcell.KeyEscape && !editMode && !botRespMode {
msgText := textArea.GetText() if shellMode {
if shellMode && msgText != "" { cmdText := shellInput.GetText()
// In shell mode, execute command instead of sending to LLM if cmdText != "" {
executeCommandAndDisplay(msgText) executeCommandAndDisplay(cmdText)
textArea.SetText("", true) // Clear the input area shellInput.SetText("")
}
return nil return nil
} else if !shellMode { }
// Normal mode - send to LLM msgText := textArea.GetText()
if msgText != "" {
nl := "\n\n" // keep empty lines between messages nl := "\n\n" // keep empty lines between messages
prevText := textView.GetText(true) prevText := textView.GetText(true)
persona := cfg.UserRole persona := cfg.UserRole