Feat: cli mode

This commit is contained in:
Grail Finder
2026-03-15 15:30:10 +03:00
parent e476575334
commit 2c9c36e2c6
4 changed files with 320 additions and 22 deletions

161
bot.go
View File

@@ -25,6 +25,8 @@ import (
"sync"
"sync/atomic"
"time"
"github.com/rivo/tview"
)
var (
@@ -46,7 +48,63 @@ var (
chunkParser ChunkParser
lastToolCall *models.FuncCall
lastRespStats *models.ResponseStats
//nolint:unused // TTS_ENABLED conditionally uses this
outputHandler OutputHandler
cliPrevOutput string
cliRespDone chan bool
)
type OutputHandler interface {
Write(p string)
Writef(format string, args ...interface{})
ScrollToEnd()
}
type TUIOutputHandler struct {
tv *tview.TextView
}
func (h *TUIOutputHandler) Write(p string) {
if h.tv != nil {
fmt.Fprint(h.tv, p)
}
if cfg != nil && cfg.CLIMode {
fmt.Print(p)
cliPrevOutput = p
}
}
func (h *TUIOutputHandler) Writef(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
if h.tv != nil {
fmt.Fprint(h.tv, s)
}
if cfg != nil && cfg.CLIMode {
fmt.Print(s)
cliPrevOutput = s
}
}
func (h *TUIOutputHandler) ScrollToEnd() {
if h.tv != nil {
h.tv.ScrollToEnd()
}
}
type CLIOutputHandler struct{}
func (h *CLIOutputHandler) Write(p string) {
fmt.Print(p)
}
func (h *CLIOutputHandler) Writef(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func (h *CLIOutputHandler) ScrollToEnd() {
}
var (
basicCard = &models.CharCard{
ID: models.ComputeCardID("assistant", "basic_sys"),
SysPrompt: models.BasicSysMsg,
@@ -800,6 +858,10 @@ func chatWatcher(ctx context.Context) {
// inpired by https://github.com/rivo/tview/issues/225
func showSpinner() {
if cfg.CLIMode {
showSpinnerCLI()
return
}
spinners := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
var i int
botPersona := cfg.AssistantRole
@@ -826,6 +888,12 @@ func showSpinner() {
})
}
func showSpinnerCLI() {
for botRespMode.Load() || toolRunningMode.Load() {
time.Sleep(400 * time.Millisecond)
}
}
func chatRound(r *models.ChatRoundReq) error {
interruptResp.Store(false)
botRespMode.Store(true)
@@ -858,13 +926,22 @@ func chatRound(r *models.ChatRoundReq) error {
Role: botPersona, Content: "",
})
nl := "\n\n"
prevText := textView.GetText(true)
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
prevText := cliPrevOutput
if cfg.CLIMode {
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
}
} else {
prevText = textView.GetText(true)
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
}
}
fmt.Fprintf(textView, "%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
outputHandler.Writef("%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
} else {
msgIdx = len(chatBody.Messages) - 1
}
@@ -886,9 +963,9 @@ out:
thinkingBuffer.WriteString(chunk)
if thinkingCollapsed {
// Show placeholder immediately when thinking starts in collapsed mode
fmt.Fprint(textView, "[yellow::i][thinking... (press Alt+T to expand)][-:-:-]")
outputHandler.Write("[yellow::i][thinking... (press Alt+T to expand)][-:-:-]")
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
respText.WriteString(chunk)
continue
@@ -903,7 +980,7 @@ out:
respText.WriteString(chunk)
justExitedThinkingCollapsed = true
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
continue
}
@@ -920,32 +997,32 @@ out:
chunk = "\n\n" + chunk
justExitedThinkingCollapsed = false
}
fmt.Fprint(textView, chunk)
outputHandler.Write(chunk)
respText.WriteString(chunk)
// Update the message in chatBody.Messages so it persists during Alt+T
if !r.Resume {
chatBody.Messages[msgIdx].Content += respText.String()
}
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
// Send chunk to audio stream handler
if cfg.TTS_ENABLED {
TTSTextChan <- chunk
}
case toolChunk := <-openAIToolChan:
fmt.Fprint(textView, toolChunk)
outputHandler.Write(toolChunk)
toolResp.WriteString(toolChunk)
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
case <-streamDone:
for len(chunkChan) > 0 {
chunk := <-chunkChan
fmt.Fprint(textView, chunk)
outputHandler.Write(chunk)
respText.WriteString(chunk)
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
if cfg.TTS_ENABLED {
TTSTextChan <- chunk
@@ -987,6 +1064,12 @@ out:
cleanChatBody()
refreshChatDisplay()
updateStatusLine()
if cfg.CLIMode && cliRespDone != nil {
select {
case cliRespDone <- true:
default:
}
}
// bot msg is done;
// now check it for func call
// logChat(activeChatName, chatBody.Messages)
@@ -1229,7 +1312,7 @@ func findCall(msg, toolCall string) bool {
// return true
// }
// Show tool call progress indicator before execution
fmt.Fprintf(textView, "\n[yellow::i][tool: %s...][-:-:-]", fc.Name)
outputHandler.Writef("\n[yellow::i][tool: %s...][-:-:-]", fc.Name)
toolRunningMode.Store(true)
resp, okT := tools.CallToolWithAgent(fc.Name, fc.Args)
if !okT {
@@ -1307,7 +1390,7 @@ func findCall(msg, toolCall string) bool {
IsShellCommand: isShellCommand,
}
}
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
outputHandler.Writef("%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
"\n\n", len(chatBody.Messages), cfg.ToolRole, toolResponseMsg.GetText())
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
// Clear the stored tool call ID after using it
@@ -1500,6 +1583,31 @@ func refreshLocalModelsIfEmpty() {
localModelsMu.Unlock()
}
func startNewCLIChat() []models.RoleMsg {
id, err := store.ChatGetMaxID()
if err != nil {
logger.Error("failed to get chat id", "error", err)
}
id++
charToStart(cfg.AssistantRole, false)
newChat := &models.Chat{
ID: id,
Name: fmt.Sprintf("%d_%s", id, cfg.AssistantRole),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Msgs: "",
Agent: cfg.AssistantRole,
}
activeChatName = newChat.Name
chatMap[newChat.Name] = newChat
cliPrevOutput = ""
return chatBody.Messages
}
func startNewCLIErrors() []models.RoleMsg {
return startNewCLIChat()
}
func summarizeAndStartNewChat() {
if len(chatBody.Messages) == 0 {
showToast("info", "No chat history to summarize")
@@ -1526,8 +1634,10 @@ func summarizeAndStartNewChat() {
}
chatBody.Messages = append(chatBody.Messages, toolMsg)
// Update UI
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
if !cfg.CLIMode {
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
}
// Update storage
if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil {
logger.Warn("failed to update storage after injecting summary", "error", err)
@@ -1585,7 +1695,12 @@ func init() {
return
}
lastToolCall = &models.FuncCall{}
lastChat := loadOldChatOrGetNew()
var lastChat []models.RoleMsg
if cfg.CLIMode {
lastChat = startNewCLIErrors()
} else {
lastChat = loadOldChatOrGetNew()
}
chatBody = &models.ChatBody{
Model: "modelname",
Stream: true,
@@ -1620,7 +1735,9 @@ func init() {
// atomic default values
cachedModelColor.Store("orange")
go chatWatcher(ctx)
initTUI()
if !cfg.CLIMode {
initTUI()
}
tools.InitTools(cfg, logger, store)
// tooler = tools.InitTools(cfg, logger, store)
// tooler.RegisterWindowTools(modelHasVision)

View File

@@ -75,6 +75,8 @@ type Config struct {
// playwright browser
PlaywrightEnabled bool `toml:"PlaywrightEnabled"`
PlaywrightDebug bool `toml:"PlaywrightDebug"` // !headless
// CLI mode
CLIMode bool
}
func LoadConfig(fn string) (*Config, error) {

178
main.go
View File

@@ -1,6 +1,13 @@
package main
import (
"bufio"
"flag"
"fmt"
"gf-lt/models"
"gf-lt/pngmeta"
"os"
"strings"
"sync/atomic"
"github.com/rivo/tview"
@@ -22,9 +29,19 @@ var (
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) | API: [orange:-:b]%s[-:-:-] (ctrl+v)\nwriting as: [orange:-:b]%s[-:-:-] (ctrl+q) | bot will write as [orange:-:b]%s[-:-:-] (ctrl+x)"
focusSwitcher = map[tview.Primitive]tview.Primitive{}
app *tview.Application
cliCardPath string
)
func main() {
flag.BoolVar(&cfg.CLIMode, "cli", false, "Run in CLI mode without TUI")
flag.StringVar(&cliCardPath, "card", "", "Path to syscard JSON file")
flag.Parse()
if cfg.CLIMode {
runCLIMode()
return
}
pages.AddPage("main", flex, true, true)
if err := app.SetRoot(pages,
true).EnableMouse(cfg.EnableMouse).EnablePaste(true).Run(); err != nil {
@@ -32,3 +49,164 @@ func main() {
return
}
}
func runCLIMode() {
outputHandler = &CLIOutputHandler{}
cliRespDone = make(chan bool, 1)
if cliCardPath != "" {
card, err := pngmeta.ReadCardJson(cliCardPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load syscard: %v\n", err)
os.Exit(1)
}
cfg.AssistantRole = card.Role
sysMap[card.ID] = card
roleToID[card.Role] = card.ID
charToStart(card.Role, false)
fmt.Printf("Loaded syscard: %s (%s)\n", card.Role, card.FilePath)
}
startNewCLIChat()
printCLIWelcome()
go func() {
<-ctx.Done()
os.Exit(0)
}()
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if !scanner.Scan() {
break
}
msg := scanner.Text()
if msg == "" {
continue
}
if strings.HasPrefix(msg, "/") {
if !handleCLICommand(msg) {
return
}
fmt.Println()
continue
}
persona := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
persona = cfg.WriteNextMsgAs
}
chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: msg}
<-cliRespDone
fmt.Println()
}
}
func printCLIWelcome() {
fmt.Println("CLI Mode started. Type your messages or commands.")
fmt.Println("Type /help for available commands.")
fmt.Println()
}
func printCLIHelp() {
fmt.Println("Available commands:")
fmt.Println(" /help, /h - Show this help message")
fmt.Println(" /new, /n - Start a new chat (clears conversation)")
fmt.Println(" /card <path>, /c <path> - Load a different syscard")
fmt.Println(" /undo, /u - Delete last message")
fmt.Println(" /history, /ls - List chat history")
fmt.Println(" /load <name> - Load a specific chat by name")
fmt.Println(" /model <name>, /m <name> - Switch model")
fmt.Println(" /quit, /q, /exit - Exit CLI mode")
fmt.Println()
fmt.Printf("Current syscard: %s\n", cfg.AssistantRole)
fmt.Printf("Current model: %s\n", chatBody.Model)
fmt.Println()
}
func handleCLICommand(msg string) bool {
parts := strings.Fields(msg)
cmd := strings.ToLower(parts[0])
args := parts[1:]
switch cmd {
case "/help", "/h":
printCLIHelp()
case "/new", "/n":
startNewCLIChat()
fmt.Println("New chat started.")
fmt.Printf("Syscard: %s\n", cfg.AssistantRole)
fmt.Println()
case "/card", "/c":
if len(args) == 0 {
fmt.Println("Usage: /card <path>")
return true
}
card, err := pngmeta.ReadCardJson(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load syscard: %v\n", err)
return true
}
cfg.AssistantRole = card.Role
sysMap[card.ID] = card
roleToID[card.Role] = card.ID
charToStart(card.Role, false)
startNewCLIChat()
fmt.Printf("Switched to syscard: %s (%s)\n", card.Role, card.FilePath)
case "/undo", "/u":
if len(chatBody.Messages) == 0 {
fmt.Println("No messages to delete.")
return true
}
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
cliPrevOutput = ""
fmt.Println("Last message deleted.")
case "/history", "/ls":
fmt.Println("Chat history:")
for name := range chatMap {
marker := " "
if name == activeChatName {
marker = "* "
}
fmt.Printf("%s%s\n", marker, name)
}
fmt.Println()
case "/load":
if len(args) == 0 {
fmt.Println("Usage: /load <name>")
return true
}
name := args[0]
chat, ok := chatMap[name]
if !ok {
fmt.Printf("Chat not found: %s\n", name)
return true
}
history, err := chat.ToHistory()
if err != nil {
fmt.Printf("Failed to load chat: %v\n", err)
return true
}
chatBody.Messages = history
activeChatName = name
cfg.AssistantRole = chat.Agent
fmt.Printf("Loaded chat: %s\n", name)
case "/model", "/m":
if len(args) == 0 {
fmt.Printf("Current model: %s\n", chatBody.Model)
return true
}
chatBody.Model = args[0]
fmt.Printf("Switched to model: %s\n", args[0])
case "/quit", "/q", "/exit":
fmt.Println("Goodbye!")
return false
default:
fmt.Printf("Unknown command: %s\n", msg)
fmt.Println("Type /help for available commands.")
}
return true
}

1
tui.go
View File

@@ -230,6 +230,7 @@ func initTUI() {
tview.Styles = colorschemes["default"]
app = tview.NewApplication()
pages = tview.NewPages()
outputHandler = &TUIOutputHandler{tv: textView}
shellInput = tview.NewInputField().
SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir)). // dynamic prompt
SetFieldWidth(0).