diff options
76 files changed, 18125 insertions, 573 deletions
@@ -1,6 +1,20 @@ *.txt *.json testlog -elefant history/ *.db +*.db-shm +*.db-wal +config.toml +sysprompts/* +!sysprompts/alice_bob_carl.json +!sysprompts/coding_assistant.json +history_bak/ +.aider* +tags +gf-lt +gflt +chat_exports/*.json +ragimport +.env +onnx/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..ce57300 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,50 @@ +version: "2" +run: + timeout: 1m + concurrency: 4 + tests: false +linters: + default: none + enable: + - bodyclose + - errcheck + - fatcontext + - govet + - ineffassign + - perfsprint + - prealloc + - staticcheck + - unused + - gocritic + - unconvert + - wastedassign + settings: + gocritic: + enabled-tags: + - performance + funlen: + lines: 80 + statements: 50 + lll: + line-length: 80 + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +issues: + max-issues-per-linter: 0 + max-same-issues: 0 +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d514a3f --- /dev/null +++ b/Makefile @@ -0,0 +1,196 @@ +.PHONY: setconfig run lint lintall install-linters setup-whisper build-whisper download-whisper-model docker-up docker-down docker-logs noextra-run installdelve checkdelve fetch-onnx install-onnx-deps + +run: setconfig + go build -tags extra -o gf-lt && ./gf-lt + +build-debug: + go build -gcflags="all=-N -l" -tags extra -o gf-lt + +debug: build-debug + dlv exec --headless --accept-multiclient --listen=:2345 ./gf-lt + +noextra-run: setconfig + go build -tags '!extra' -o gf-lt && ./gf-lt + +setconfig: + find config.toml &>/dev/null || cp config.example.toml config.toml + +installdelve: + go install github.com/go-delve/delve/cmd/dlv@latest + +checkdelve: + which dlv &>/dev/null || installdelve + +install-linters: ## Install additional linters (noblanks) + go install github.com/GrailFinder/noblanks-linter/cmd/noblanks@latest + +lint: ## Run linters. Use make install-linters first. + golangci-lint run -c .golangci.yml ./... + +lintall: lint + noblanks ./... + +fetch-onnx: + mkdir -p onnx/embedgemma && curl -o onnx/embedgemma/config.json -L https://huggingface.co/onnx-community/embeddinggemma-300m-ONNX/resolve/main/config.json && curl -o onnx/embedgemma/tokenizer.json -L https://huggingface.co/onnx-community/embeddinggemma-300m-ONNX/resolve/main/tokenizer.json && curl -o onnx/embedgemma/model_q4.onnx -L https://huggingface.co/onnx-community/embeddinggemma-300m-ONNX/resolve/main/onnx/model_q4.onnx && curl -o onnx/embedgemma/model_q4.onnx_data -L https://huggingface.co/onnx-community/embeddinggemma-300m-ONNX/resolve/main/onnx/model_q4.onnx_data?download=true + +install-onnx-deps: ## Install ONNX Runtime with CUDA support (or CPU fallback) + @echo "=== ONNX Runtime Installer ===" && \ + echo "" && \ + echo "Checking for existing ONNX Runtime..." && \ + if ldconfig -p 2>/dev/null | grep -q libonnxruntime.so.1; then \ + echo "ONNX Runtime is already installed:" && \ + ldconfig -p 2>/dev/null | grep libonnxruntime && \ + echo "" && \ + echo "Skipping installation. To reinstall, remove existing libs first:" && \ + echo " sudo rm -f /usr/local/lib/libonnxruntime*.so*" && \ + exit 0; \ + fi && \ + echo "No ONNX Runtime found. Proceeding with installation..." && \ + echo "" && \ + echo "Detecting CUDA version..." && \ + HAS_CUDA=0 && \ + if command -v nvidia-smi >/dev/null 2>&1; then \ + CUDA_INFO=$$(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -1) && \ + if [ -n "$$CUDA_INFO" ]; then \ + echo "Found NVIDIA GPU with driver: $$CUDA_INFO" && \ + HAS_CUDA=1; \ + else \ + echo "NVIDIA driver found but could not detect CUDA version"; \ + fi; \ + else \ + echo "No NVIDIA GPU detected (nvidia-smi not found)"; \ + fi && \ + echo "" && \ + echo "Determining ONNX Runtime version..." && \ + ARCH=$$(uname -m) && \ + if [ "$$ARCH" = "x86_64" ]; then \ + ONNX_ARCH="x64"; \ + elif [ "$$ARCH" = "aarch64" ] || [ "$$ARCH" = "arm64" ]; then \ + ONNX_ARCH="aarch64"; \ + else \ + echo "Unsupported architecture: $$ARCH" && \ + exit 1; \ + fi && \ + echo "Detected architecture: $$ARCH (ONNX runtime: $$ONNX_ARCH)" && \ + if [ "$$HAS_CUDA" = "1" ]; then \ + echo "Installing ONNX Runtime with CUDA support..."; \ + ONNX_VERSION="1.24.2"; \ + else \ + echo "Installing ONNX Runtime (CPU version)..."; \ + ONNX_VERSION="1.24.2"; \ + fi && \ + FILENAME="onnxruntime-linux-$${ONNX_ARCH}-${ONNX_VERSION}.tgz" && \ + URL="https://github.com/microsoft/onnxruntime/releases/download/v$${ONNX_VERSION}/$${FILENAME}" && \ + echo "Downloading $${URL}..." && \ + mkdir -p /tmp/onnx-install && \ + curl -L -o /tmp/onnx-install/$${FILENAME} "$${URL}" || { \ + echo "Failed to download ONNX Runtime v$${ONNX_VERSION}. Trying v1.18.0..." && \ + ONNX_VERSION="1.18.0" && \ + FILENAME="onnxruntime-linux-$${ONNX_ARCH}-${ONNX_VERSION}.tgz" && \ + URL="https://github.com/microsoft/onnxruntime/releases/download/v$${ONNX_VERSION}/$${FILENAME}" && \ + curl -L -o /tmp/onnx-install/$${FILENAME} "$${URL}" || { \ + echo "ERROR: Failed to download ONNX Runtime from GitHub" && \ + echo "" && \ + echo "Please install manually:" && \ + echo " 1. Go to https://github.com/microsoft/onnxruntime/releases" && \ + echo " 2. Download onnxruntime-linux-$${ONNX_ARCH}-VERSION.tgz" && \ + echo " 3. Extract and copy to /usr/local/lib:" && \ + echo " tar -xzf onnxruntime-linux-$${ONNX_ARCH}-VERSION.tgz" && \ + echo " sudo cp -r onnxruntime-linux-$${ONNX_ARCH}-VERSION/lib/* /usr/local/lib/" && \ + echo " sudo ldconfig" && \ + exit 1; \ + }; \ + } && \ + echo "Extracting..." && \ + cd /tmp/onnx-install && tar -xzf $${FILENAME} && \ + echo "Installing to /usr/local/lib..." && \ + ONNX_DIR=$$(find /tmp/onnx-install -maxdepth 1 -type d -name "onnxruntime-linux-*") && \ + if [ -d "$${ONNX_DIR}/lib" ]; then \ + cp -r $${ONNX_DIR}/lib/* /usr/local/lib/ 2>/dev/null || sudo cp -r $${ONNX_DIR}/lib/* /usr/local/lib/; \ + else \ + echo "ERROR: Could not find lib directory in extracted archive" && \ + exit 1; \ + fi && \ + echo "Updating library cache..." && \ + sudo ldconfig 2>/dev/null || ldconfig && \ + echo "" && \ + echo "=== Installation complete! ===" && \ + echo "" && \ + echo "Installed libraries:" && \ + ldconfig -p | grep libonnxruntime || echo "(libraries may require logout/relogin to appear)" && \ + echo "" && \ + if [ "$$HAS_CUDA" = "1" ]; then \ + echo "NOTE: CUDA-enabled ONNX Runtime installed."; \ + echo "Ensure you also have CUDA libraries installed:"; \ + echo " - libcudnn, libcublas, libcurand"; \ + else \ + echo "NOTE: CPU-only ONNX Runtime installed."; \ + echo "For GPU support, install CUDA and re-run this script."; \ + fi && \ + rm -rf /tmp/onnx-install + +# Whisper STT Setup (in batteries directory) +setup-whisper: build-whisper download-whisper-model + +build-whisper: ## Build whisper.cpp from source in batteries directory + @echo "Building whisper.cpp from source in batteries directory..." + @if [ ! -f "batteries/whisper.cpp/CMakeLists.txt" ]; then \ + echo "Cloning whisper.cpp repository to batteries directory..."; \ + rm -rf batteries/whisper.cpp; \ + git clone https://github.com/ggml-org/whisper.cpp.git batteries/whisper.cpp; \ + fi + cd batteries/whisper.cpp && cmake -B build -DGGML_CUDA=ON -DWHISPER_SDL2=ON; cmake --build build --config Release -j 8 + @echo "Whisper binary built successfully!" + +download-whisper-model: ## Download Whisper model for STT in batteries directory + @echo "Downloading Whisper model for STT..." + @if [ ! -d "batteries/whisper.cpp/models" ]; then \ + mkdir -p "batteries/whisper.cpp/models"; \ + fi + curl -o batteries/whisper.cpp/models/ggml-large-v3-turbo-q5_0.bin -L "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-q5_0.bin?download=true" + @echo "Whisper model downloaded successfully!" + +# Docker targets for STT/TTS services (in batteries directory) +docker-up: ## Start all Docker Compose services for STT and TTS from batteries directory + @echo "Starting Docker services for STT (whisper) and TTS (kokoro)..." + @echo "Note: The Whisper model will be downloaded automatically inside the container on first run" + docker-compose -f batteries/docker-compose.yml up -d + @echo "Docker services started. STT available at http://localhost:8081, TTS available at http://localhost:8880" + +docker-up-whisper: ## Start only the Whisper STT service + @echo "Starting Whisper STT service only..." + @echo "Note: The Whisper model will be downloaded automatically inside the container on first run" + docker-compose -f batteries/docker-compose.yml up -d whisper + @echo "Whisper STT service started. Available at http://localhost:8081" + +docker-up-kokoro: ## Start only the Kokoro TTS service + @echo "Starting Kokoro TTS service only..." + docker-compose -f batteries/docker-compose.yml up -d kokoro-tts + @echo "Kokoro TTS service started. Available at http://localhost:8880" + +docker-down: ## Stop all Docker Compose services from batteries directory + @echo "Stopping Docker services..." + docker-compose -f batteries/docker-compose.yml down + @echo "Docker services stopped" + +docker-down-whisper: ## Stop only the Whisper STT service + @echo "Stopping Whisper STT service..." + docker-compose -f batteries/docker-compose.yml down whisper + @echo "Whisper STT service stopped" + +docker-down-kokoro: ## Stop only the Kokoro TTS service + @echo "Stopping Kokoro TTS service..." + docker-compose -f batteries/docker-compose.yml down kokoro-tts + @echo "Kokoro TTS service stopped" + +docker-logs: ## View logs from all Docker services in batteries directory + @echo "Displaying logs from Docker services..." + docker-compose -f batteries/docker-compose.yml logs -f + +docker-logs-whisper: ## View logs from Whisper STT service only + @echo "Displaying logs from Whisper STT service..." + docker-compose -f batteries/docker-compose.yml logs -f whisper + +docker-logs-kokoro: ## View logs from Kokoro TTS service only + @echo "Displaying logs from Kokoro TTS service..." + docker-compose -f batteries/docker-compose.yml logs -f kokoro-tts @@ -1,25 +1,61 @@ -### TODO: -- scrolling chat history; (somewhat works out of box); + -- log errors to file; + -- give serial id to each msg in chat to track it; (use slice index) + -- show msg id next to the msg; + -- regen last message; + -- delete last message; + -- edit message? (including from bot); + -- ability to copy message; + -- menu with old chats (chat files); + -- fullscreen textarea option (for long prompt); -- tab to switch selection between textview and textarea (input and chat); + -- basic tools: memorize and recall; -- stop stream from the bot; + -- sqlitedb instead of chatfiles; + -- sqlite for the bot memory; -- option to switch between predefined sys prompts; - -### FIX: -- bot responding (or haninging) blocks everything; + -- programm requires history folder, but it is .gitignore; + -- at first run chat table does not exist; run migrations sql on startup; + -- Tab is needed to copy paste text into textarea box, use shift+tab to switch focus; (changed tp pgup) + -- delete last msg: can have unexpected behavior (deletes what appears to be two messages if last bot msg was not generated (should only delete icon in that case)); -- empty input to continue bot msg gens new msg index and bot icon; +### gf-lt (grail finder's llm tui) +terminal user interface for large language models. +made with use of [tview](https://github.com/rivo/tview) + +#### has/supports +- character card spec; +- API (/chat and /completion): llama.cpp, deepseek, openrouter; +- tts/stt (run make commands to get deps); +- image input; +- function calls (function calls are implemented natively, to avoid calling outside sources); +- [character specific context (unique feature)](docs/char-specific-context.md) + +#### how it looks + + + +#### dependencies +- make +- go +- ffmpeg (extra) + +#### how to install +(requires golang) +clone the project +``` +git clone https://github.com/GrailFinder/gf-lt.git +cd gf-lt +make +``` + +to run without tts/stt dependencies use +``` +make noextra-run +``` + +#### keybinds +while running you can press f12 for list of keys; + + +#### setting up config +``` +cp config.example.toml config.toml +``` +set values as you need them to be; +[description of config variables](docs/config.md) + +#### setting up STT/TTS services +For speech-to-text (STT) and text-to-speech (TTS) functionality: +1. The project uses Whisper.cpp for STT and Kokoro for TTS +2. Docker Compose automatically downloads the required Whisper model on first run +3. To start all services: `make docker-up` +4. To start only STT service: `make docker-up-whisper` +5. To start only TTS service: `make docker-up-kokoro` +6. To stop all services: `make docker-down` +7. To stop only STT service: `make docker-down-whisper` +8. To stop only TTS service: `make docker-down-kokoro` +9. To view all service logs: `make docker-logs` +10. To view only STT service logs: `make docker-logs-whisper` +11. To view only TTS service logs: `make docker-logs-kokoro` +12. The STT service runs on http://localhost:8081 +13. The TTS service runs on http://localhost:8880 diff --git a/agent/agent.go b/agent/agent.go new file mode 100644 index 0000000..8824ecb --- /dev/null +++ b/agent/agent.go @@ -0,0 +1,45 @@ +package agent + +// I see two types of agents possible: +// ones who do their own tools calls +// ones that works only with the output + +// A: main chat -> agent (handles everything: tool + processing) +// B: main chat -> tool -> agent (process tool output) + +// AgenterA gets a task "find out weather in london" +// proceeds to make tool calls on its own +type AgenterA interface { + ProcessTask(task string) []byte +} + +// AgenterB defines an interface for processing tool outputs +type AgenterB interface { + // Process takes the original tool arguments and the raw output from the tool, + // and returns a cleaned/summarized version suitable for the main LLM context + Process(args map[string]string, rawOutput []byte) []byte +} + +// registry holds mapping from tool names to agents +var RegistryB = make(map[string]AgenterB) +var RegistryA = make(map[AgenterA][]string) + +// Register adds an agent for a specific tool name +// If an agent already exists for the tool, it will be replaced +func RegisterB(toolName string, a AgenterB) { + RegistryB[toolName] = a +} + +func RegisterA(toolNames []string, a AgenterA) { + RegistryA[a] = toolNames +} + +// Get returns the agent registered for the given tool name, or nil if none. +func Get(toolName string) AgenterB { + return RegistryB[toolName] +} + +// Register is a convenience wrapper for RegisterB. +func Register(toolName string, a AgenterB) { + RegisterB(toolName, a) +} diff --git a/agent/request.go b/agent/request.go new file mode 100644 index 0000000..f42b06e --- /dev/null +++ b/agent/request.go @@ -0,0 +1,228 @@ +package agent + +import ( + "bytes" + "encoding/json" + "fmt" + "gf-lt/config" + "gf-lt/models" + "io" + "log/slog" + "net/http" + "strings" +) + +var httpClient = &http.Client{} + +var defaultProps = map[string]float32{ + "temperature": 0.8, + "dry_multiplier": 0.0, + "min_p": 0.05, + "n_predict": -1.0, +} + +func detectAPI(api string) (isCompletion, isChat, isDeepSeek, isOpenRouter bool) { + isCompletion = strings.Contains(api, "/completion") && !strings.Contains(api, "/chat/completions") + isChat = strings.Contains(api, "/chat/completions") + isDeepSeek = strings.Contains(api, "deepseek.com") + isOpenRouter = strings.Contains(api, "openrouter.ai") + return +} + +type AgentClient struct { + cfg *config.Config + getToken func() string + log slog.Logger +} + +func NewAgentClient(cfg *config.Config, log slog.Logger, gt func() string) *AgentClient { + return &AgentClient{ + cfg: cfg, + getToken: gt, + log: log, + } +} + +func (ag *AgentClient) Log() *slog.Logger { + return &ag.log +} + +func (ag *AgentClient) FormMsg(sysprompt, msg string) (io.Reader, error) { + b, err := ag.buildRequest(sysprompt, msg) + if err != nil { + return nil, err + } + return bytes.NewReader(b), nil +} + +// buildRequest creates the appropriate LLM request based on the current API endpoint. +func (ag *AgentClient) buildRequest(sysprompt, msg string) ([]byte, error) { + api := ag.cfg.CurrentAPI + model := ag.cfg.CurrentModel + messages := []models.RoleMsg{ + {Role: "system", Content: sysprompt}, + {Role: "user", Content: msg}, + } + + // Determine API type + isCompletion, isChat, isDeepSeek, isOpenRouter := detectAPI(api) + ag.log.Debug("agent building request", "api", api, "isCompletion", isCompletion, "isChat", isChat, "isDeepSeek", isDeepSeek, "isOpenRouter", isOpenRouter) + + // Build prompt for completion endpoints + if isCompletion { + var sb strings.Builder + for i := range messages { + sb.WriteString(messages[i].ToPrompt()) + sb.WriteString("\n") + } + prompt := strings.TrimSpace(sb.String()) + + switch { + case isDeepSeek: + // DeepSeek completion + req := models.NewDSCompletionReq(prompt, model, defaultProps["temperature"], []string{}) + req.Stream = false // Agents don't need streaming + return json.Marshal(req) + case isOpenRouter: + // OpenRouter completion + req := models.NewOpenRouterCompletionReq(model, prompt, defaultProps, []string{}) + req.Stream = false // Agents don't need streaming + return json.Marshal(req) + default: + // Assume llama.cpp completion + req := models.NewLCPReq(prompt, model, nil, defaultProps, []string{}) + req.Stream = false // Agents don't need streaming + return json.Marshal(req) + } + } + + // Chat completions endpoints + if isChat || !isCompletion { + chatBody := &models.ChatBody{ + Model: model, + Stream: false, // Agents don't need streaming + Messages: messages, + } + + switch { + case isDeepSeek: + // DeepSeek chat + req := models.NewDSChatReq(*chatBody) + return json.Marshal(req) + case isOpenRouter: + // OpenRouter chat - agents don't use reasoning by default + req := models.NewOpenRouterChatReq(*chatBody, defaultProps, "") + return json.Marshal(req) + default: + // Assume llama.cpp chat (OpenAI format) + req := models.OpenAIReq{ + ChatBody: chatBody, + Tools: nil, + } + return json.Marshal(req) + } + } + + // Fallback (should not reach here) + ag.log.Warn("unknown API, using default chat completions format", "api", api) + chatBody := &models.ChatBody{ + Model: model, + Stream: false, // Agents don't need streaming + Messages: messages, + } + return json.Marshal(chatBody) +} + +func (ag *AgentClient) LLMRequest(body io.Reader) ([]byte, error) { + // Read the body for debugging (but we need to recreate it for the request) + bodyBytes, err := io.ReadAll(body) + if err != nil { + ag.log.Error("failed to read request body", "error", err) + return nil, err + } + req, err := http.NewRequest("POST", ag.cfg.CurrentAPI, bytes.NewReader(bodyBytes)) + if err != nil { + ag.log.Error("failed to create request", "error", err) + return nil, err + } + req.Header.Add("Accept", "application/json") + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+ag.getToken()) + req.Header.Set("Accept-Encoding", "gzip") + ag.log.Debug("agent LLM request", "url", ag.cfg.CurrentAPI, "body_preview", string(bodyBytes[:min(len(bodyBytes), 500)])) + resp, err := httpClient.Do(req) + if err != nil { + ag.log.Error("llamacpp api request failed", "error", err, "url", ag.cfg.CurrentAPI) + return nil, err + } + defer resp.Body.Close() + responseBytes, err := io.ReadAll(resp.Body) + if err != nil { + ag.log.Error("failed to read response", "error", err) + return nil, err + } + if resp.StatusCode >= 400 { + ag.log.Error("agent LLM request failed", "status", resp.StatusCode, "response", string(responseBytes[:min(len(responseBytes), 1000)])) + return responseBytes, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(responseBytes[:min(len(responseBytes), 200)])) + } + + // Parse response and extract text content + text, err := extractTextFromResponse(responseBytes) + if err != nil { + ag.log.Error("failed to extract text from response", "error", err, "response_preview", string(responseBytes[:min(len(responseBytes), 500)])) + // Return raw response as fallback + return responseBytes, nil + } + return []byte(text), nil +} + +// extractTextFromResponse parses common LLM response formats and extracts the text content. +func extractTextFromResponse(data []byte) (string, error) { + // Try to parse as generic JSON first + var genericResp map[string]interface{} + if err := json.Unmarshal(data, &genericResp); err != nil { + // Not JSON, return as string + return string(data), nil + } + + // Check for OpenAI chat completion format + if choices, ok := genericResp["choices"].([]interface{}); ok && len(choices) > 0 { + if firstChoice, ok := choices[0].(map[string]interface{}); ok { + // Chat completion: choices[0].message.content + if message, ok := firstChoice["message"].(map[string]interface{}); ok { + if content, ok := message["content"].(string); ok { + return content, nil + } + } + // Completion: choices[0].text + if text, ok := firstChoice["text"].(string); ok { + return text, nil + } + // Delta format for streaming (should not happen with stream: false) + if delta, ok := firstChoice["delta"].(map[string]interface{}); ok { + if content, ok := delta["content"].(string); ok { + return content, nil + } + } + } + } + + // Check for llama.cpp completion format + if content, ok := genericResp["content"].(string); ok { + return content, nil + } + + // Unknown format, return pretty-printed JSON + prettyJSON, err := json.MarshalIndent(genericResp, "", " ") + if err != nil { + return string(data), nil + } + return string(prettyJSON), nil +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/agent/webagent.go b/agent/webagent.go new file mode 100644 index 0000000..ff6cd86 --- /dev/null +++ b/agent/webagent.go @@ -0,0 +1,32 @@ +package agent + +import ( + "fmt" +) + +// WebAgentB is a simple agent that applies formatting functions +type WebAgentB struct { + *AgentClient + sysprompt string +} + +// NewWebAgentB creates a WebAgentB that uses the given formatting function +func NewWebAgentB(client *AgentClient, sysprompt string) *WebAgentB { + return &WebAgentB{AgentClient: client, sysprompt: sysprompt} +} + +// Process applies the formatting function to raw output +func (a *WebAgentB) Process(args map[string]string, rawOutput []byte) []byte { + msg, err := a.FormMsg(a.sysprompt, + fmt.Sprintf("request:\n%+v\ntool response:\n%v", args, string(rawOutput))) + if err != nil { + a.Log().Error("failed to process the request", "error", err) + return []byte("failed to process the request; err: " + err.Error()) + } + resp, err := a.LLMRequest(msg) + if err != nil { + a.Log().Error("failed to process the request", "error", err) + return []byte("failed to process the request; err: " + err.Error()) + } + return resp +} diff --git a/assets/ex01.png b/assets/ex01.png Binary files differnew file mode 100644 index 0000000..90ad254 --- /dev/null +++ b/assets/ex01.png diff --git a/assets/helppage.png b/assets/helppage.png Binary files differnew file mode 100644 index 0000000..5128a62 --- /dev/null +++ b/assets/helppage.png diff --git a/batteries/docker-compose.yml b/batteries/docker-compose.yml new file mode 100644 index 0000000..84b2262 --- /dev/null +++ b/batteries/docker-compose.yml @@ -0,0 +1,57 @@ +services: + # Whisper.cpp STT service + whisper: + image: ghcr.io/ggml-org/whisper.cpp:main-cuda + container_name: whisper-stt + ports: + - "8081:8081" + volumes: + - ./whisper.cpp/models/ggml-large-v3-turbo-q5_0.bin:/app/models/ggml-large-v3-turbo-q5_0.bin + working_dir: /app + entrypoint: "" + command: > + sh -c " + if [ ! -f /app/models/ggml-large-v3-turbo-q5_0.bin ]; then + echo 'Downloading ggml-large-v3-turboq5_0 model...' + curl -o /app/models/ggml-large-v3-turbo-q5_0.bin -L "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v3-turbo-q5_0.bin?download=true" + fi && + ./build/bin/whisper-server -m /app/models/ggml-large-v3-turbo-q5_0.bin -t 4 -p 1 --port 8081 --host 0.0.0.0 + " + environment: + - WHISPER_LOG_LEVEL=3 + # For GPU support, uncomment the following lines: + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + # Restart policy in case the service fails + restart: unless-stopped + + + # Kokoro-FastAPI TTS service + kokoro-tts: + # image: ghcr.io/remsky/kokoro-fastapi-cpu:latest + image: ghcr.io/remsky/kokoro-fastapi-gpu:latest + container_name: kokoro-tts + ports: + - "8880:8880" + environment: + - API_LOG_LEVEL=INFO + # For GPU support, uncomment the following lines: + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + restart: unless-stopped + +volumes: + models: + driver: local + whisper_models: + driver: local diff --git a/batteries/whisper.cpp b/batteries/whisper.cpp new file mode 160000 +Subproject 30c5194c9691e4e9a98b3dea9f19727397d3f46 @@ -3,250 +3,1551 @@ package main import ( "bufio" "bytes" - "elefant/models" - "elefant/storage" + "compress/gzip" + "context" "encoding/json" "fmt" + "gf-lt/config" + "gf-lt/models" + "gf-lt/rag" + "gf-lt/storage" + "html" "io" "log/slog" + "net" "net/http" "os" + "regexp" + "slices" + "strconv" "strings" + "sync" + "sync/atomic" "time" - - "github.com/rivo/tview" ) -var httpClient = http.Client{ - Timeout: time.Second * 20, -} - var ( - logger *slog.Logger - APIURL = "http://localhost:8080/v1/chat/completions" - DB = map[string]map[string]any{} - userRole = "user" - assistantRole = "assistant" - toolRole = "tool" - assistantIcon = "<🤖>: " - userIcon = "<user>: " - historyDir = "./history/" - // TODO: pass as an cli arg - showSystemMsgs bool + httpClient = &http.Client{} + cfg *config.Config + logger *slog.Logger + logLevel = new(slog.LevelVar) + ctx, cancel = context.WithCancel(context.Background()) activeChatName string + chatRoundChan = make(chan *models.ChatRoundReq, 1) chunkChan = make(chan string, 10) + openAIToolChan = make(chan string, 10) streamDone = make(chan bool, 1) chatBody *models.ChatBody - store storage.ChatHistory + store storage.FullRepo defaultFirstMsg = "Hello! What can I do for you?" - defaultStarter = []models.MessagesStory{ - {Role: "system", Content: systemMsg}, - {Role: assistantRole, Content: defaultFirstMsg}, + defaultStarter = []models.RoleMsg{} + interruptResp atomic.Bool + ragger *rag.RAG + chunkParser ChunkParser + lastToolCall *models.FuncCall + lastRespStats *models.ResponseStats + //nolint:unused // TTS_ENABLED conditionally uses this + orator Orator + asr STT + localModelsMu sync.RWMutex + defaultLCPProps = map[string]float32{ + "temperature": 0.8, + "dry_multiplier": 0.0, + "min_p": 0.05, + "n_predict": -1.0, } - interruptResp = false + ORFreeModels = []string{ + "google/gemini-2.0-flash-exp:free", + "deepseek/deepseek-chat-v3-0324:free", + "mistralai/mistral-small-3.2-24b-instruct:free", + "qwen/qwen3-14b:free", + "google/gemma-3-27b-it:free", + "meta-llama/llama-3.3-70b-instruct:free", + } + LocalModels = []string{} + localModelsData *models.LCPModels + orModelsData *models.ORModels ) -// ==== +var thinkBlockRE = regexp.MustCompile(`(?s)<think>.*?</think>`) + +// parseKnownToTag extracts known_to list from content using configured tag. +// Returns cleaned content and list of character names. +func parseKnownToTag(content string) []string { + if cfg == nil || !cfg.CharSpecificContextEnabled { + return nil + } + tag := cfg.CharSpecificContextTag + if tag == "" { + tag = "@" + } + // Pattern: tag + list + "@" + pattern := regexp.QuoteMeta(tag) + `(.*?)@` + re := regexp.MustCompile(pattern) + matches := re.FindAllStringSubmatch(content, -1) + if len(matches) == 0 { + return nil + } + // There may be multiple tags; we combine all. + var knownTo []string + for _, match := range matches { + if len(match) < 2 { + continue + } + // Remove the entire matched tag from content + list := strings.TrimSpace(match[1]) + if list == "" { + continue + } + strings.SplitSeq(list, ",") + // parts := strings.Split(list, ",") + // for _, p := range parts { + for p := range strings.SplitSeq(list, ",") { + p = strings.TrimSpace(p) + if p != "" { + knownTo = append(knownTo, p) + } + } + } + // Also remove any leftover trailing "__" that might be orphaned? Not needed. + return knownTo +} + +// processMessageTag processes a message for known_to tag and sets KnownTo field. +// It also ensures the sender's role is included in KnownTo. +// If KnownTo already set (e.g., from DB), preserves it unless new tag found. +func processMessageTag(msg *models.RoleMsg) *models.RoleMsg { + if cfg == nil || !cfg.CharSpecificContextEnabled { + return msg + } + // If KnownTo already set, assume tag already processed (content cleaned). + // However, we still check for new tags (maybe added later). + knownTo := parseKnownToTag(msg.GetText()) + // If tag found, replace KnownTo with new list (merge with existing?) + // For simplicity, if knownTo is not nil, replace. + if knownTo == nil { + return msg + } + msg.KnownTo = knownTo + if msg.Role == "" { + return msg + } + if !slices.Contains(msg.KnownTo, msg.Role) { + msg.KnownTo = append(msg.KnownTo, msg.Role) + } + return msg +} + +// filterMessagesForCharacter returns messages visible to the specified character. +// If CharSpecificContextEnabled is false, returns all messages. +func filterMessagesForCharacter(messages []models.RoleMsg, character string) []models.RoleMsg { + if strings.Contains(cfg.CurrentAPI, "chat") { + return messages + } + if cfg == nil || !cfg.CharSpecificContextEnabled || character == "" { + return messages + } + if character == "system" { // system sees every message + return messages + } + filtered := make([]models.RoleMsg, 0, len(messages)) + for i := range messages { + // If KnownTo is nil or empty, message is visible to all + // system msg cannot be filtered + if len(messages[i].KnownTo) == 0 || messages[i].Role == "system" { + filtered = append(filtered, messages[i]) + continue + } + if slices.Contains(messages[i].KnownTo, character) { + // Check if character is in KnownTo lis + filtered = append(filtered, messages[i]) + } + } + return filtered +} -func getUserInput(userPrompt string) string { - fmt.Printf(userPrompt) - reader := bufio.NewReader(os.Stdin) - line, err := reader.ReadString('\n') +func consolidateAssistantMessages(messages []models.RoleMsg) []models.RoleMsg { + if len(messages) == 0 { + return messages + } + result := make([]models.RoleMsg, 0, len(messages)) + for i := range messages { + // Non-assistant messages are appended as-is + if messages[i].Role != cfg.AssistantRole { + result = append(result, messages[i]) + continue + } + // Assistant message: start a new block or merge with the last one + if len(result) == 0 || result[len(result)-1].Role != cfg.AssistantRole { + // First assistant in a block: append a copy (avoid mutating input) + result = append(result, messages[i].Copy()) + continue + } + // Merge with the last assistant message + last := &result[len(result)-1] + // If either message has structured content, unify to ContentParts + if last.IsContentParts() || messages[i].IsContentParts() { + // Convert last to ContentParts if needed, preserving ToolCallID + if !last.IsContentParts() { + toolCallID := last.ToolCallID + *last = models.NewMultimodalMsg(last.Role, []interface{}{ + models.TextContentPart{Type: "text", Text: last.Content}, + }) + last.ToolCallID = toolCallID + } + // Add current message's content to last + if messages[i].IsContentParts() { + last.ContentParts = append(last.ContentParts, messages[i].GetContentParts()...) + } else if messages[i].Content != "" { + last.AddTextPart(messages[i].Content) + } + } else { + // Both simple strings: concatenate with newline + if last.Content != "" && messages[i].Content != "" { + last.Content += "\n" + messages[i].Content + } else if messages[i].Content != "" { + last.Content = messages[i].Content + } + // ToolCallID is already preserved in last + } + } + return result +} + +// GetLogLevel returns the current log level as a string +func GetLogLevel() string { + level := logLevel.Level() + switch level { + case slog.LevelDebug: + return "Debug" + case slog.LevelInfo: + return "Info" + case slog.LevelWarn: + return "Warn" + default: + // For any other values, return "Info" as default + return "Info" + } +} + +func createClient(connectTimeout time.Duration) *http.Client { + // Custom transport with connection timeout + transport := &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + // Create a dialer with connection timeout + dialer := &net.Dialer{ + Timeout: connectTimeout, + KeepAlive: 30 * time.Second, // Optional + } + return dialer.DialContext(ctx, network, addr) + }, + // Other transport settings (optional) + TLSHandshakeTimeout: connectTimeout, + ResponseHeaderTimeout: connectTimeout, + } + // Client with no overall timeout (or set to streaming-safe duration) + return &http.Client{ + Transport: transport, + Timeout: 0, // No overall timeout (for streaming) + } +} + +func warmUpModel() { + if !isLocalLlamacpp() { + return + } + // Check if model is already loaded + loaded, err := isModelLoaded(chatBody.Model) if err != nil { - panic(err) // think about it + logger.Debug("failed to check model status", "model", chatBody.Model, "error", err) + // Continue with warmup attempt anyway + } + if loaded { + showToast("model already loaded", "Model "+chatBody.Model+" is already loaded.") + return } - return line + go func() { + var data []byte + var err error + switch { + case strings.HasSuffix(cfg.CurrentAPI, "/completion"): + // Old completion endpoint + req := models.NewLCPReq(".", chatBody.Model, nil, map[string]float32{ + "temperature": 0.8, + "dry_multiplier": 0.0, + "min_p": 0.05, + "n_predict": 0, + }, []string{}) + req.Stream = false + data, err = json.Marshal(req) + case strings.Contains(cfg.CurrentAPI, "/v1/chat/completions"): + // OpenAI-compatible chat endpoint + req := models.OpenAIReq{ + ChatBody: &models.ChatBody{ + Model: chatBody.Model, + Messages: []models.RoleMsg{ + {Role: "system", Content: "."}, + }, + Stream: false, + }, + Tools: nil, + } + data, err = json.Marshal(req) + default: + // Unknown local endpoint, skip + return + } + if err != nil { + logger.Debug("failed to marshal warmup request", "error", err) + return + } + resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", bytes.NewReader(data)) + if err != nil { + logger.Debug("warmup request failed", "error", err) + return + } + resp.Body.Close() + // Start monitoring for model load completion + monitorModelLoad(chatBody.Model) + }() } -func formMsg(chatBody *models.ChatBody, newMsg, role string) io.Reader { - if newMsg != "" { // otherwise let the bot continue - newMsg := models.MessagesStory{Role: role, Content: newMsg} - chatBody.Messages = append(chatBody.Messages, newMsg) +// nolint +func fetchDSBalance() *models.DSBalance { + url := "https://api.deepseek.com/user/balance" + method := "GET" + // nolint + req, err := http.NewRequest(method, url, nil) + if err != nil { + logger.Warn("failed to create request", "error", err) + return nil } - data, err := json.Marshal(chatBody) + req.Header.Add("Accept", "application/json") + req.Header.Add("Authorization", "Bearer "+cfg.DeepSeekToken) + res, err := httpClient.Do(req) if err != nil { - panic(err) + logger.Warn("failed to make request", "error", err) + return nil + } + defer res.Body.Close() + resp := models.DSBalance{} + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + return nil } - return bytes.NewReader(data) + return &resp } -// func sendMsgToLLM(body io.Reader) (*models.LLMRespChunk, error) { -func sendMsgToLLM(body io.Reader) (any, error) { - resp, err := httpClient.Post(APIURL, "application/json", body) +func fetchORModels(free bool) ([]string, error) { + resp, err := http.Get("https://openrouter.ai/api/v1/models") if err != nil { - logger.Error("llamacpp api", "error", err) return nil, err } defer resp.Body.Close() - llmResp := []models.LLMRespChunk{} - // chunkChan <- assistantIcon + if resp.StatusCode != 200 { + err := fmt.Errorf("failed to fetch or models; status: %s", resp.Status) + return nil, err + } + data := &models.ORModels{} + if err := json.NewDecoder(resp.Body).Decode(data); err != nil { + return nil, err + } + orModelsData = data + freeModels := data.ListModels(free) + return freeModels, nil +} + +func fetchLCPModels() ([]string, error) { + resp, err := http.Get(cfg.FetchModelNameAPI) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + err := fmt.Errorf("failed to fetch or models; status: %s", resp.Status) + return nil, err + } + data := &models.LCPModels{} + if err := json.NewDecoder(resp.Body).Decode(data); err != nil { + return nil, err + } + localModels := data.ListModels() + return localModels, nil +} + +// fetchLCPModelsWithLoadStatus returns models with "(loaded)" indicator for loaded models +func fetchLCPModelsWithLoadStatus() ([]string, error) { + modelList, err := fetchLCPModelsWithStatus() + if err != nil { + return nil, err + } + result := make([]string, 0, len(modelList.Data)) + li := 0 // loaded index + for i, m := range modelList.Data { + modelName := m.ID + if m.Status.Value == "loaded" { + modelName = models.LoadedMark + modelName + li = i + } + result = append(result, modelName) + } + if li == 0 { + return result, nil // no loaded modelList + } + loadedModel := result[li] + result = append(result[:li], result[li+1:]...) + return slices.Concat([]string{loadedModel}, result), nil +} + +// fetchLCPModelsWithStatus returns the full LCPModels struct including status information. +func fetchLCPModelsWithStatus() (*models.LCPModels, error) { + resp, err := http.Get(cfg.FetchModelNameAPI) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + err := fmt.Errorf("failed to fetch llama.cpp models; status: %s", resp.Status) + return nil, err + } + data := &models.LCPModels{} + if err := json.NewDecoder(resp.Body).Decode(data); err != nil { + return nil, err + } + localModelsData = data + return data, nil +} + +// isModelLoaded checks if the given model ID is currently loaded in llama.cpp server. +func isModelLoaded(modelID string) (bool, error) { + models, err := fetchLCPModelsWithStatus() + if err != nil { + return false, err + } + for _, m := range models.Data { + if m.ID == modelID { + return m.Status.Value == "loaded", nil + } + } + return false, nil +} + +func ModelHasVision(api, modelID string) bool { + switch { + case strings.Contains(api, "deepseek"): + return false + case strings.Contains(api, "openrouter"): + resp, err := http.Get("https://openrouter.ai/api/v1/models") + if err != nil { + logger.Warn("failed to fetch OR models for vision check", "error", err) + return false + } + defer resp.Body.Close() + orm := &models.ORModels{} + if err := json.NewDecoder(resp.Body).Decode(orm); err != nil { + logger.Warn("failed to decode OR models for vision check", "error", err) + return false + } + return orm.HasVision(modelID) + default: + models, err := fetchLCPModelsWithStatus() + if err != nil { + logger.Warn("failed to fetch LCP models for vision check", "error", err) + return false + } + return models.HasVision(modelID) + } +} + +// monitorModelLoad starts a goroutine that periodically checks if the specified model is loaded. +func monitorModelLoad(modelID string) { + go func() { + timeout := time.After(2 * time.Minute) // max wait 2 minutes + ticker := time.NewTicker(2 * time.Second) + defer ticker.Stop() + for { + select { + case <-timeout: + logger.Debug("model load monitoring timeout", "model", modelID) + return + case <-ticker.C: + loaded, err := isModelLoaded(modelID) + if err != nil { + logger.Debug("failed to check model status", "model", modelID, "error", err) + continue + } + if loaded { + showToast("model loaded", "Model "+modelID+" is now loaded and ready.") + refreshChatDisplay() + return + } + } + } + }() +} + +// extractDetailedErrorFromBytes extracts detailed error information from response body bytes +func extractDetailedErrorFromBytes(body []byte, statusCode int) string { + // Try to decompress gzip if the response is compressed + if len(body) >= 2 && body[0] == 0x1f && body[1] == 0x8b { + reader, err := gzip.NewReader(bytes.NewReader(body)) + if err == nil { + decompressed, err := io.ReadAll(reader) + reader.Close() + if err == nil { + body = decompressed + } + } + } + // Try to parse as JSON to extract detailed error information + var errorResponse map[string]any + if err := json.Unmarshal(body, &errorResponse); err == nil { + // Check if it's an error response with detailed information + if errorData, ok := errorResponse["error"]; ok { + if errorMap, ok := errorData.(map[string]any); ok { + var errorMsg string + if msg, ok := errorMap["message"]; ok { + errorMsg = fmt.Sprintf("%v", msg) + } + var details []string + if code, ok := errorMap["code"]; ok { + details = append(details, fmt.Sprintf("Code: %v", code)) + } + if metadata, ok := errorMap["metadata"]; ok { + // Handle metadata which might contain raw error details + if metadataMap, ok := metadata.(map[string]any); ok { + if raw, ok := metadataMap["raw"]; ok { + // Parse the raw error string if it's JSON + var rawError map[string]any + if rawStr, ok := raw.(string); ok && json.Unmarshal([]byte(rawStr), &rawError) == nil { + if rawErrorData, ok := rawError["error"]; ok { + if rawErrorMap, ok := rawErrorData.(map[string]any); ok { + if rawMsg, ok := rawErrorMap["message"]; ok { + return fmt.Sprintf("API Error: %s", rawMsg) + } + } + } + } + } + } + details = append(details, fmt.Sprintf("Metadata: %v", metadata)) + } + if len(details) > 0 { + return fmt.Sprintf("API Error: %s (%s)", errorMsg, strings.Join(details, ", ")) + } + return "API Error: " + errorMsg + } + } + } + // If not a structured error response, return the raw body with status + return fmt.Sprintf("HTTP Status: %d, Response Body: %s", statusCode, string(body)) +} + +func finalizeRespStats(tokenCount int, startTime time.Time) { + duration := time.Since(startTime).Seconds() + var tps float64 + if duration > 0 { + tps = float64(tokenCount) / duration + } + lastRespStats = &models.ResponseStats{ + Tokens: tokenCount, + Duration: duration, + TokensPerSec: tps, + } +} + +// sendMsgToLLM expects streaming resp +func sendMsgToLLM(body io.Reader) { + choseChunkParser() + // openrouter does not respect stop strings, so we have to cut the message ourselves + stopStrings := chatBody.MakeStopSliceExcluding("", listChatRoles()) + req, err := http.NewRequest("POST", cfg.CurrentAPI, body) + if err != nil { + logger.Error("newreq error", "error", err) + showToast("error", "apicall failed:"+err.Error()) + streamDone <- true + return + } + req.Header.Add("Accept", "application/json") + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+chunkParser.GetToken()) + req.Header.Set("Accept-Encoding", "gzip") + // nolint + resp, err := httpClient.Do(req) + if err != nil { + logger.Error("llamacpp api", "error", err) + showToast("error", "apicall failed:"+err.Error()) + streamDone <- true + return + } + // Check if the initial response is an error before starting to stream + if resp.StatusCode >= 400 { + // Read the response body to get detailed error information + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + logger.Error("failed to read error response body", "error", err, "status_code", resp.StatusCode) + detailedError := fmt.Sprintf("HTTP Status: %d, Failed to read response body: %v", resp.StatusCode, err) + showToast("API Error", detailedError) + resp.Body.Close() + streamDone <- true + return + } + // Parse the error response for detailed information + detailedError := extractDetailedErrorFromBytes(bodyBytes, resp.StatusCode) + logger.Error("API returned error status", "status_code", resp.StatusCode, "detailed_error", detailedError) + showToast("API Error", detailedError) + resp.Body.Close() + streamDone <- true + return + } + // + defer resp.Body.Close() reader := bufio.NewReader(resp.Body) - counter := 0 + counter := uint32(0) + tokenCount := 0 + startTime := time.Now() + hasReasoning := false + reasoningSent := false + defer func() { + finalizeRespStats(tokenCount, startTime) + }() for { - if interruptResp { - interruptResp = false - logger.Info("interrupted bot response") - break - } - llmchunk := models.LLMRespChunk{} - if counter > 2000 { + var ( + answerText string + chunk *models.TextChunk + ) + counter++ + // to stop from spiriling in infinity read of bad bytes that happens with poor connection + if cfg.ChunkLimit > 0 && counter > cfg.ChunkLimit { + logger.Warn("response hit chunk limit", "limit", cfg.ChunkLimit) streamDone <- true break } line, err := reader.ReadBytes('\n') if err != nil { + // Check if this is an EOF error and if the response contains detailed error information + if err == io.EOF { + // For streaming responses, we may have already consumed the error body + // So we'll use the original status code to provide context + detailedError := fmt.Sprintf("Streaming connection closed unexpectedly (Status: %d). This may indicate an API error. Check your API provider and model settings.", resp.StatusCode) + logger.Error("error reading response body", "error", err, "detailed_error", detailedError, + "status_code", resp.StatusCode, "user_role", cfg.UserRole, "parser", chunkParser, "link", cfg.CurrentAPI) + showToast("API Error", detailedError) + } else { + logger.Error("error reading response body", "error", err, "line", string(line), + "user_role", cfg.UserRole, "parser", chunkParser, "link", cfg.CurrentAPI) + // if err.Error() != "EOF" { + showToast("API error", err.Error()) + } streamDone <- true - panic(err) + break + // } + // continue } - // logger.Info("linecheck", "line", string(line), "len", len(line), "counter", counter) if len(line) <= 1 { + if interruptResp.Load() { + goto interrupt // get unstuck from bad connection + } continue // skip \n } // starts with -> data: line = line[6:] - if err := json.Unmarshal(line, &llmchunk); err != nil { - logger.Error("failed to decode", "error", err, "line", string(line)) + logger.Debug("debugging resp", "line", string(line)) + if bytes.Equal(line, []byte("[DONE]\n")) { streamDone <- true - return nil, err + break } - llmResp = append(llmResp, llmchunk) - // logger.Info("streamview", "chunk", llmchunk) - // if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason != "chat.completion.chunk" { - if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" { + if bytes.Equal(line, []byte("ROUTER PROCESSING\n")) { + continue + } + chunk, err = chunkParser.ParseChunk(line) + if err != nil { + logger.Error("error parsing response body", "error", err, + "line", string(line), "url", cfg.CurrentAPI) + showToast("LLM Response Error", "Failed to parse LLM response: "+err.Error()) streamDone <- true - // last chunk break } - counter++ + // // problem: this catches any mention of the word 'error' + // Handle error messages in response content + // example needed, since llm could use the word error in the normal msg + // if string(line) != "" && strings.Contains(strings.ToLower(string(line)), "error") { + // logger.Error("API error response detected", "line", line, "url", cfg.CurrentAPI) + // streamDone <- true + // break + // } + if chunk.Finished { + // Close the thinking block if we were streaming reasoning and haven't closed it yet + if hasReasoning && !reasoningSent { + chunkChan <- "</think>" + tokenCount++ + } + if chunk.Chunk != "" { + logger.Warn("text inside of finish llmchunk", "chunk", chunk, "counter", counter) + answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n") + chunkChan <- answerText + tokenCount++ + } + streamDone <- true + break + } + if counter == 0 { + chunk.Chunk = strings.TrimPrefix(chunk.Chunk, " ") + } + // Handle reasoning chunks - stream them immediately as they arrive + if chunk.Reasoning != "" && !reasoningSent { + if !hasReasoning { + // First reasoning chunk - send opening tag + chunkChan <- "<think>" + tokenCount++ + hasReasoning = true + } + // Stream reasoning content immediately + answerText = strings.ReplaceAll(chunk.Reasoning, "\n\n", "\n") + if answerText != "" { + chunkChan <- answerText + tokenCount++ + } + } + // When we get content and have been streaming reasoning, close the thinking block + if chunk.Chunk != "" && hasReasoning && !reasoningSent { + // Close the thinking block before sending actual content + chunkChan <- "</think>" + tokenCount++ + reasoningSent = true + } // bot sends way too many \n - answerText := strings.ReplaceAll(llmchunk.Choices[0].Delta.Content, "\n\n", "\n") - chunkChan <- answerText + answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n") + // Accumulate text to check for stop strings that might span across chunks + // check if chunk is in stopstrings => stop + // this check is needed only for openrouter /v1/completion, since it does not respect stop slice + if chunkParser.GetAPIType() == models.APITypeCompletion && + slices.Contains(stopStrings, answerText) { + logger.Debug("stop string detected on client side for completion endpoint", "stop_string", answerText) + streamDone <- true + break + } + if answerText != "" { + chunkChan <- answerText + tokenCount++ + } + openAIToolChan <- chunk.ToolChunk + if chunk.FuncName != "" { + lastToolCall.Name = chunk.FuncName + // Store the tool call ID for the response + lastToolCall.ID = chunk.ToolID + } + interrupt: + if interruptResp.Load() { // read bytes, so it would not get into beginning of the next req + logger.Info("interrupted bot response", "chunk_counter", counter) + streamDone <- true + break + } } - return llmResp, nil } -func chatRound(userMsg, role string, tv *tview.TextView) { - botRespMode = true - reader := formMsg(chatBody, userMsg, role) +func roleToIcon(role string) string { + return "<" + role + ">: " +} + +func chatWatcher(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case chatRoundReq := <-chatRoundChan: + if err := chatRound(chatRoundReq); err != nil { + logger.Error("failed to chatRound", "err", err) + } + } + } +} + +// inpired by https://github.com/rivo/tview/issues/225 +func showSpinner() { + spinners := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} + var i int + botPersona := cfg.AssistantRole + if cfg.WriteNextMsgAsCompletionAgent != "" { + botPersona = cfg.WriteNextMsgAsCompletionAgent + } + for botRespMode.Load() || toolRunningMode.Load() { + time.Sleep(400 * time.Millisecond) + spin := i % len(spinners) + app.QueueUpdateDraw(func() { + switch { + case toolRunningMode.Load(): + textArea.SetTitle(spinners[spin] + " tool") + case botRespMode.Load(): + textArea.SetTitle(spinners[spin] + " " + botPersona + " (F6 to interrupt)") + default: + textArea.SetTitle(spinners[spin] + " input") + } + }) + i++ + } + app.QueueUpdateDraw(func() { + textArea.SetTitle("input") + }) +} + +func chatRound(r *models.ChatRoundReq) error { + interruptResp.Store(false) + botRespMode.Store(true) + go showSpinner() + updateStatusLine() + botPersona := cfg.AssistantRole + if cfg.WriteNextMsgAsCompletionAgent != "" { + botPersona = cfg.WriteNextMsgAsCompletionAgent + } + defer func() { + botRespMode.Store(false) + ClearImageAttachment() + }() + // check that there is a model set to use if is not local + choseChunkParser() + reader, err := chunkParser.FormMsg(r.UserMsg, r.Role, r.Resume) + if reader == nil || err != nil { + logger.Error("empty reader from msgs", "role", r.Role, "error", err) + return err + } + if cfg.SkipLLMResp { + return nil + } go sendMsgToLLM(reader) - fmt.Fprintf(tv, fmt.Sprintf("(%d) ", len(chatBody.Messages))) - fmt.Fprintf(tv, assistantIcon) + logger.Debug("looking at vars in chatRound", "msg", r.UserMsg, "regen", r.Regen, "resume", r.Resume) + msgIdx := len(chatBody.Messages) + if !r.Resume { + // Add empty message to chatBody immediately so it persists during Alt+T toggle + chatBody.Messages = append(chatBody.Messages, models.RoleMsg{ + Role: botPersona, Content: "", + }) + nl := "\n\n" + 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)) + } else { + msgIdx = len(chatBody.Messages) - 1 + } respText := strings.Builder{} + toolResp := strings.Builder{} + // Variables for handling thinking blocks during streaming + inThinkingBlock := false + thinkingBuffer := strings.Builder{} + justExitedThinkingCollapsed := false out: for { select { case chunk := <-chunkChan: - // fmt.Printf(chunk) - fmt.Fprintf(tv, chunk) + // Handle thinking blocks during streaming + if strings.HasPrefix(chunk, "<think>") && !inThinkingBlock { + // Start of thinking block + inThinkingBlock = true + thinkingBuffer.Reset() + 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)][-:-:-]") + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + respText.WriteString(chunk) + continue + } + } else if inThinkingBlock { + thinkingBuffer.WriteString(chunk) + if strings.Contains(chunk, "</think>") { + // End of thinking block + inThinkingBlock = false + if thinkingCollapsed { + // Thinking already displayed as placeholder, just update respText + respText.WriteString(chunk) + justExitedThinkingCollapsed = true + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + continue + } + // If not collapsed, fall through to normal display + } else if thinkingCollapsed { + // Still in thinking block and collapsed - just buffer, don't display + respText.WriteString(chunk) + continue + } + // If not collapsed, fall through to normal display + } + // Add spacing after collapsed thinking block before real response + if justExitedThinkingCollapsed { + chunk = "\n\n" + chunk + justExitedThinkingCollapsed = false + } + fmt.Fprint(textView, chunk) respText.WriteString(chunk) - tv.ScrollToEnd() + // 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() + } + // Send chunk to audio stream handler + if cfg.TTS_ENABLED { + TTSTextChan <- chunk + } + case toolChunk := <-openAIToolChan: + fmt.Fprint(textView, toolChunk) + toolResp.WriteString(toolChunk) + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } case <-streamDone: + for len(chunkChan) > 0 { + chunk := <-chunkChan + fmt.Fprint(textView, chunk) + respText.WriteString(chunk) + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + if cfg.TTS_ENABLED { + TTSTextChan <- chunk + } + } + if cfg.TTS_ENABLED { + TTSFlushChan <- true + } break out } } - botRespMode = false - chatBody.Messages = append(chatBody.Messages, models.MessagesStory{ - Role: assistantRole, Content: respText.String(), - }) + var msgStats *models.ResponseStats + if lastRespStats != nil { + msgStats = &models.ResponseStats{ + Tokens: lastRespStats.Tokens, + Duration: lastRespStats.Duration, + TokensPerSec: lastRespStats.TokensPerSec, + } + lastRespStats = nil + } + botRespMode.Store(false) + if r.Resume { + chatBody.Messages[len(chatBody.Messages)-1].Content += respText.String() + updatedMsg := chatBody.Messages[len(chatBody.Messages)-1] + processedMsg := processMessageTag(&updatedMsg) + chatBody.Messages[len(chatBody.Messages)-1] = *processedMsg + if msgStats != nil && chatBody.Messages[len(chatBody.Messages)-1].Role != cfg.ToolRole { + chatBody.Messages[len(chatBody.Messages)-1].Stats = msgStats + } + } else { + chatBody.Messages[msgIdx].Content = respText.String() + processedMsg := processMessageTag(&chatBody.Messages[msgIdx]) + chatBody.Messages[msgIdx] = *processedMsg + if msgStats != nil && chatBody.Messages[msgIdx].Role != cfg.ToolRole { + chatBody.Messages[msgIdx].Stats = msgStats + } + stopTTSIfNotForUser(&chatBody.Messages[msgIdx]) + } + cleanChatBody() + refreshChatDisplay() + updateStatusLine() // bot msg is done; // now check it for func call // logChat(activeChatName, chatBody.Messages) - err := updateStorageChat(activeChatName, chatBody.Messages) - if err != nil { + if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil { logger.Warn("failed to update storage", "error", err, "name", activeChatName) } - findCall(respText.String(), tv) + // Strip think blocks before parsing for tool calls + respTextNoThink := thinkBlockRE.ReplaceAllString(respText.String(), "") + if interruptResp.Load() { + return nil + } + if findCall(respTextNoThink, toolResp.String()) { + return nil + } + // Check if this message was sent privately to specific characters + // If so, trigger those characters to respond if that char is not controlled by user + // perhaps we should have narrator role to determine which char is next to act + if cfg.AutoTurn { + lastMsg := chatBody.Messages[len(chatBody.Messages)-1] + if len(lastMsg.KnownTo) > 0 { + triggerPrivateMessageResponses(&lastMsg) + } + } + return nil } -func findCall(msg string, tv *tview.TextView) { - prefix := "__tool_call__\n" - suffix := "\n__tool_call__" - fc := models.FuncCall{} - if !strings.HasPrefix(msg, prefix) || - !strings.HasSuffix(msg, suffix) { +// cleanChatBody removes messages with null or empty content to prevent API issues +func cleanChatBody() { + if chatBody == nil || chatBody.Messages == nil { return } - jsStr := strings.TrimSuffix(strings.TrimPrefix(msg, prefix), suffix) - if err := json.Unmarshal([]byte(jsStr), &fc); err != nil { - logger.Error("failed to unmarshal tool call", "error", err) - return - // panic(err) + // Tool request cleaning is now configurable via AutoCleanToolCallsFromCtx (default false) + // /completion msg where part meant for user and other part tool call + // chatBody.Messages = cleanToolCalls(chatBody.Messages) + chatBody.Messages = consolidateAssistantMessages(chatBody.Messages) +} + +// convertJSONToMapStringString unmarshals JSON into map[string]interface{} and converts all values to strings. +func convertJSONToMapStringString(jsonStr string) (map[string]string, error) { + // Extract JSON object from string - models may output extra text after JSON + jsonStr = extractJSON(jsonStr) + var raw map[string]interface{} + if err := json.Unmarshal([]byte(jsonStr), &raw); err != nil { + return nil, err + } + result := make(map[string]string, len(raw)) + for k, v := range raw { + switch val := v.(type) { + case string: + result[k] = val + case float64: + result[k] = strconv.FormatFloat(val, 'f', -1, 64) + case int, int64, int32: + // json.Unmarshal converts numbers to float64, but handle other integer types if they appear + result[k] = fmt.Sprintf("%v", val) + case bool: + result[k] = strconv.FormatBool(val) + case nil: + result[k] = "" + default: + result[k] = fmt.Sprintf("%v", val) + } + } + return result, nil +} + +// extractJSON finds the first { and last } to extract only the JSON object +// This handles cases where models output extra text after JSON +func extractJSON(s string) string { + // Try direct parse first - if it works, return as-is + var dummy map[string]interface{} + if err := json.Unmarshal([]byte(s), &dummy); err == nil { + return s + } + // Otherwise find JSON boundaries + start := strings.Index(s, "{") + end := strings.LastIndex(s, "}") + if start >= 0 && end > start { + return s[start : end+1] + } + return s +} + +// unmarshalFuncCall unmarshals a JSON tool call, converting numeric arguments to strings. +func unmarshalFuncCall(jsonStr string) (*models.FuncCall, error) { + type tempFuncCall struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Args map[string]interface{} `json:"args"` + } + var temp tempFuncCall + if err := json.Unmarshal([]byte(jsonStr), &temp); err != nil { + return nil, err + } + fc := &models.FuncCall{ + ID: temp.ID, + Name: temp.Name, + Args: make(map[string]string, len(temp.Args)), + } + for k, v := range temp.Args { + switch val := v.(type) { + case string: + fc.Args[k] = val + case float64: + fc.Args[k] = strconv.FormatFloat(val, 'f', -1, 64) + case int, int64, int32: + fc.Args[k] = fmt.Sprintf("%v", val) + case bool: + fc.Args[k] = strconv.FormatBool(val) + case nil: + fc.Args[k] = "" + default: + fc.Args[k] = fmt.Sprintf("%v", val) + } + } + return fc, nil +} + +// findCall: adds chatRoundReq into the chatRoundChan and returns true if does +func findCall(msg, toolCall string) bool { + var fc *models.FuncCall + if toolCall != "" { + // HTML-decode the tool call string to handle encoded characters like < -> <= + decodedToolCall := html.UnescapeString(toolCall) + openAIToolMap, err := convertJSONToMapStringString(decodedToolCall) + if err != nil { + logger.Error("failed to unmarshal openai tool call", "call", decodedToolCall, "error", err) + // Ensure lastToolCall.ID is set for the error response (already set from chunk) + // Send error response to LLM so it can retry or handle the error + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err), + ToolCallID: lastToolCall.ID, // Use the stored tool call ID + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + // Clear the stored tool call ID after using it (no longer needed) + // Trigger the assistant to continue processing with the error message + crr := &models.ChatRoundReq{ + Role: cfg.AssistantRole, + } + // provoke next llm msg after failed tool call + chatRoundChan <- crr + // chatRound("", cfg.AssistantRole, tv, false, false) + return true + } + lastToolCall.Args = openAIToolMap + fc = lastToolCall + // NOTE: We do NOT override lastToolCall.ID from arguments. + // The ID should come from the streaming response (chunk.ToolID) set earlier. + // Some tools like todo_create have "id" in their arguments which is NOT the tool call ID. + } else { + jsStr := toolCallRE.FindString(msg) + if jsStr == "" { // no tool call case + return false + } + // Remove prefix/suffix with flexible whitespace handling + jsStr = strings.TrimSpace(jsStr) + 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 < -> <= + 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 + fc, err = unmarshalFuncCall(decodedJsStr) + if err != nil { + logger.Error("failed to unmarshal tool call", "error", err, "json_string", decodedJsStr) + // Send error response to LLM so it can retry or handle the error + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: fmt.Sprintf("Error processing tool call: %v. Please check the JSON format and try again.", err), + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + logger.Debug("findCall: added tool error response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "message_count_after_add", len(chatBody.Messages)) + // Trigger the assistant to continue processing with the error message + // chatRound("", cfg.AssistantRole, tv, false, false) + crr := &models.ChatRoundReq{ + Role: cfg.AssistantRole, + } + // provoke next llm msg after failed tool call + chatRoundChan <- crr + return true + } + // Update lastToolCall with parsed function call + lastToolCall.ID = fc.ID + lastToolCall.Name = fc.Name + lastToolCall.Args = fc.Args + } + // we got here => last msg recognized as a tool call (correct or not) + // Use the tool call ID from streaming response (lastToolCall.ID) + // Don't generate random ID - the ID should match between assistant message and tool response + lastMsgIdx := len(chatBody.Messages) - 1 + if lastToolCall.ID != "" { + chatBody.Messages[lastMsgIdx].ToolCallID = lastToolCall.ID + } + // Store tool call info in the assistant message + // Convert Args map to JSON string for storage + chatBody.Messages[lastMsgIdx].ToolCall = &models.ToolCall{ + ID: lastToolCall.ID, + Name: lastToolCall.Name, + Args: mapToString(lastToolCall.Args), } // call a func - f, ok := fnMap[fc.Name] + _, ok := fnMap[fc.Name] if !ok { - m := fmt.Sprintf("%s is not implemented", fc.Name) - chatRound(m, toolRole, tv) - return + m := fc.Name + " is not implemented" + // Create tool response message with the proper tool_call_id + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: m, + ToolCallID: lastToolCall.ID, // Use the stored tool call ID + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + logger.Debug("findCall: added tool not implemented response", "role", toolResponseMsg.Role, "content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID, "message_count_after_add", len(chatBody.Messages)) + // Clear the stored tool call ID after using it + lastToolCall.ID = "" + // Trigger the assistant to continue processing with the new tool response + // by calling chatRound with empty content to continue the assistant's response + crr := &models.ChatRoundReq{ + Role: cfg.AssistantRole, + } + // failed to find tool + chatRoundChan <- crr + return true } - resp := f(fc.Args) - toolMsg := fmt.Sprintf("tool response: %+v", resp) - // reader := formMsg(chatBody, toolMsg, toolRole) - // sendMsgToLLM() - chatRound(toolMsg, toolRole, tv) - // return func result to the llm + // Show tool call progress indicator before execution + fmt.Fprintf(textView, "\n[yellow::i][tool: %s...][-:-:-]", fc.Name) + toolRunningMode.Store(true) + resp := callToolWithAgent(fc.Name, fc.Args) + toolRunningMode.Store(false) + toolMsg := string(resp) + logger.Info("llm used a tool call", "tool_name", fc.Name, "too_args", fc.Args, "id", fc.ID, "tool_resp", toolMsg) + // Create tool response message with the proper tool_call_id + // Mark shell commands as always visible + isShellCommand := fc.Name == "execute_command" + // Check if response is multimodal content (image) + var toolResponseMsg models.RoleMsg + if strings.HasPrefix(strings.TrimSpace(toolMsg), `{"type":"multimodal_content"`) { + // Parse multimodal content response + multimodalResp := models.MultimodalToolResp{} + if err := json.Unmarshal([]byte(toolMsg), &multimodalResp); err == nil && multimodalResp.Type == "multimodal_content" { + // Create RoleMsg with ContentParts + var contentParts []any + for _, part := range multimodalResp.Parts { + partType := part["type"] + switch partType { + case "text": + contentParts = append(contentParts, models.TextContentPart{Type: "text", Text: part["text"]}) + case "image_url": + contentParts = append(contentParts, models.ImageContentPart{ + Type: "image_url", + ImageURL: struct { + URL string `json:"url"` + }{URL: part["url"]}, + }) + default: + continue + } + } + toolResponseMsg = models.RoleMsg{ + Role: cfg.ToolRole, + ContentParts: contentParts, + HasContentParts: true, + ToolCallID: lastToolCall.ID, + IsShellCommand: isShellCommand, + } + } else { + // Fallback to regular content + toolResponseMsg = models.RoleMsg{ + Role: cfg.ToolRole, + Content: toolMsg, + ToolCallID: lastToolCall.ID, + IsShellCommand: isShellCommand, + } + } + } else { + toolResponseMsg = models.RoleMsg{ + Role: cfg.ToolRole, + Content: toolMsg, + ToolCallID: lastToolCall.ID, + IsShellCommand: isShellCommand, + } + } + fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n", + "\n\n", len(chatBody.Messages), cfg.ToolRole, toolResponseMsg.GetText()) + 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)) + // Clear the stored tool call ID after using it + lastToolCall.ID = "" + // Trigger the assistant to continue processing with the new tool response + // by calling chatRound with empty content to continue the assistant's response + crr := &models.ChatRoundReq{ + Role: cfg.AssistantRole, + } + chatRoundChan <- crr + return true } -func chatToTextSlice(showSys bool) []string { - resp := make([]string, len(chatBody.Messages)) - for i, msg := range chatBody.Messages { - if !showSys && (msg.Role != assistantRole && msg.Role != userRole) { +func chatToTextSlice(messages []models.RoleMsg, showSys bool) []string { + resp := make([]string, len(messages)) + for i := range messages { + icon := fmt.Sprintf("[-:-:b](%d) <%s>:[-:-:-]", i, messages[i].Role) + // 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] = strings.ReplaceAll(fmt.Sprintf("%s\n%s\n[yellow::i][tool call: %s (press Ctrl+T to expand)][-:-:-]\n", icon, messages[i].GetText(), toolName), "\n\n", "\n") + } else { + // Show full tool call info + toolName := messages[i].ToolCall.Name + resp[i] = strings.ReplaceAll(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), "\n\n", "\n") + } 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 } -func chatToText(showSys bool) string { - s := chatToTextSlice(showSys) - return strings.Join(s, "") +func chatToText(messages []models.RoleMsg, showSys bool) string { + s := chatToTextSlice(messages, showSys) + text := strings.Join(s, "\n") + // Collapse thinking blocks if enabled + if thinkingCollapsed { + text = thinkRE.ReplaceAllStringFunc(text, func(match string) string { + // Extract content between <think> and </think> + start := len("<think>") + end := len(match) - len("</think>") + if start < end && start < len(match) { + content := match[start:end] + return fmt.Sprintf("[yellow::i][thinking... (%d chars) (press Alt+T to expand)][-:-:-]", len(content)) + } + return "[yellow::i][thinking... (press Alt+T to expand)][-:-:-]" + }) + // Handle incomplete thinking blocks (during streaming when </think> hasn't arrived yet) + if strings.Contains(text, "<think>") && !strings.Contains(text, "</think>") { + // Find the incomplete thinking block and replace it + startIdx := strings.Index(text, "<think>") + if startIdx != -1 { + content := text[startIdx+len("<think>"):] + placeholder := fmt.Sprintf("[yellow::i][thinking... (%d chars) (press Alt+T to expand)][-:-:-]", len(content)) + text = text[:startIdx] + placeholder + } + } + } + return text } -func textToMsg(rawMsg string) models.MessagesStory { - msg := models.MessagesStory{} - // system and tool? - if strings.HasPrefix(rawMsg, assistantIcon) { - msg.Role = assistantRole - msg.Content = strings.TrimPrefix(rawMsg, assistantIcon) - return msg +func addNewChat(chatName string) { + id, err := store.ChatGetMaxID() + if err != nil { + logger.Error("failed to get max chat id from db;", "id:", id) + // INFO: will rewrite first chat } - if strings.HasPrefix(rawMsg, userIcon) { - msg.Role = userRole - msg.Content = strings.TrimPrefix(rawMsg, userIcon) - return msg + chat := &models.Chat{ + ID: id + 1, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Agent: cfg.AssistantRole, } - return msg + if chatName == "" { + chatName = fmt.Sprintf("%d_%s", chat.ID, cfg.AssistantRole) + } + chat.Name = chatName + chatMap[chat.Name] = chat + activeChatName = chat.Name } -func textSliceToChat(chat []string) []models.MessagesStory { - resp := make([]models.MessagesStory, len(chat)) - for i, rawMsg := range chat { - msg := textToMsg(rawMsg) - resp[i] = msg +func applyCharCard(cc *models.CharCard, loadHistory bool) { + cfg.AssistantRole = cc.Role + history, err := loadAgentsLastChat(cfg.AssistantRole) + if err != nil || !loadHistory { + // too much action for err != nil; loadAgentsLastChat needs to be split up + history = []models.RoleMsg{ + {Role: "system", Content: cc.SysPrompt}, + {Role: cfg.AssistantRole, Content: cc.FirstMsg}, + } + logger.Warn("failed to load last agent chat;", "agent", cc.Role, "err", err, "new_history", history) + addNewChat("") } - return resp + chatBody.Messages = history +} + +func charToStart(agentName string, keepSysP bool) bool { + cc := GetCardByRole(agentName) + if cc == nil { + return false + } + applyCharCard(cc, keepSysP) + return true +} + +func updateModelLists() { + var err error + if cfg.OpenRouterToken != "" { + ORFreeModels, err = fetchORModels(true) + if err != nil { + logger.Warn("failed to fetch or models", "error", err) + } + } + // if llama.cpp started after gf-lt? + ml, err := fetchLCPModelsWithLoadStatus() + if err != nil { + logger.Warn("failed to fetch llama.cpp models", "error", err) + } + localModelsMu.Lock() + LocalModels = ml + localModelsMu.Unlock() + // set already loaded model in llama.cpp + if !isLocalLlamacpp() { + return + } + localModelsMu.Lock() + defer localModelsMu.Unlock() + for i := range LocalModels { + if strings.Contains(LocalModels[i], models.LoadedMark) { + m := strings.TrimPrefix(LocalModels[i], models.LoadedMark) + cfg.CurrentModel = m + chatBody.Model = m + cachedModelColor.Store("green") + updateStatusLine() + updateToolCapabilities() + app.Draw() + return + } + } +} + +func refreshLocalModelsIfEmpty() { + localModelsMu.RLock() + if len(LocalModels) > 0 { + localModelsMu.RUnlock() + return + } + localModelsMu.RUnlock() + // try to fetch + models, err := fetchLCPModels() + if err != nil { + logger.Warn("failed to fetch llama.cpp models", "error", err) + return + } + localModelsMu.Lock() + LocalModels = models + localModelsMu.Unlock() +} + +func summarizeAndStartNewChat() { + if len(chatBody.Messages) == 0 { + showToast("info", "No chat history to summarize") + return + } + showToast("info", "Summarizing chat history...") + // Call the summarize_chat tool via agent + summaryBytes := callToolWithAgent("summarize_chat", map[string]string{}) + summary := string(summaryBytes) + if summary == "" { + showToast("error", "Failed to generate summary") + return + } + // Start a new chat + startNewChat(true) + // Inject summary as a tool call response + toolMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: summary, + ToolCallID: "", + } + chatBody.Messages = append(chatBody.Messages, toolMsg) + // Update UI + 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) + } + showToast("info", "Chat summarized and new chat started with summary as tool response") } func init() { - file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + // ctx, cancel := context.WithCancel(context.Background()) + var err error + cfg, err = config.LoadConfig("config.toml") if err != nil { - panic(err) + fmt.Println("failed to load config.toml", err) + cancel() + os.Exit(1) + return } - // create dir if does not exist - if err := os.MkdirAll(historyDir, os.ModePerm); err != nil { - panic(err) + defaultStarter = []models.RoleMsg{ + {Role: "system", Content: basicSysMsg}, + {Role: cfg.AssistantRole, Content: defaultFirstMsg}, + } + logfile, err := os.OpenFile(cfg.LogFile, + os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + slog.Error("failed to open log file", "error", err, "filename", cfg.LogFile) + cancel() + os.Exit(1) + return + } + // load cards + basicCard.Role = cfg.AssistantRole + logLevel.Set(slog.LevelInfo) + logger = slog.New(slog.NewTextHandler(logfile, &slog.HandlerOptions{Level: logLevel})) + store = storage.NewProviderSQL(cfg.DBPATH, logger) + if store == nil { + cancel() + os.Exit(1) + return + } + ragger, err = rag.New(logger, store, cfg) + if err != nil { + logger.Error("failed to create RAG", "error", err) + } + if ragger != nil && ragger.FallbackMessage() != "" && app != nil { + showToast("RAG", "ONNX unavailable, using API: "+ragger.FallbackMessage()) } - logger = slog.New(slog.NewTextHandler(file, nil)) - store = storage.NewProviderSQL("test.db", logger) // https://github.com/coreydaley/ggerganov-llama.cpp/blob/master/examples/server/README.md // load all chats in memory - loadHistoryChats() + if _, err := loadHistoryChats(); err != nil { + logger.Error("failed to load chat", "error", err) + cancel() + os.Exit(1) + return + } + lastToolCall = &models.FuncCall{} lastChat := loadOldChatOrGetNew() - logger.Info("loaded history", "chat", lastChat) chatBody = &models.ChatBody{ - Model: "modl_name", + Model: "modelname", Stream: true, Messages: lastChat, } + choseChunkParser() + httpClient = createClient(time.Second * 90) + if cfg.TTS_ENABLED { + orator = NewOrator(logger, cfg) + } + if cfg.STT_ENABLED { + asr = NewSTT(logger, cfg) + } + if cfg.PlaywrightEnabled { + if err := checkPlaywright(); err != nil { + // slow, need a faster check if playwright install + if err := installPW(); err != nil { + logger.Error("failed to install playwright", "error", err) + cancel() + os.Exit(1) + return + } + if err := checkPlaywright(); err != nil { + logger.Error("failed to run playwright", "error", err) + cancel() + os.Exit(1) + return + } + } + } + // atomic default values + cachedModelColor.Store("orange") + go chatWatcher(ctx) + initTUI() + initTools() } diff --git a/bot_test.go b/bot_test.go new file mode 100644 index 0000000..65f2c84 --- /dev/null +++ b/bot_test.go @@ -0,0 +1,638 @@ +package main +import ( + "gf-lt/config" + "gf-lt/models" + "reflect" + "testing" +) +func TestConsolidateConsecutiveAssistantMessages(t *testing.T) { + // Mock config for testing + testCfg := &config.Config{ + AssistantRole: "assistant", + WriteNextMsgAsCompletionAgent: "", + } + cfg = testCfg + tests := []struct { + name string + input []models.RoleMsg + expected []models.RoleMsg + }{ + { + name: "no consecutive assistant messages", + input: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "Hi there"}, + {Role: "user", Content: "How are you?"}, + }, + expected: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "Hi there"}, + {Role: "user", Content: "How are you?"}, + }, + }, + { + name: "consecutive assistant messages should be consolidated", + input: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "First part"}, + {Role: "assistant", Content: "Second part"}, + {Role: "user", Content: "Thanks"}, + }, + expected: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "First part\nSecond part"}, + {Role: "user", Content: "Thanks"}, + }, + }, + { + name: "multiple sets of consecutive assistant messages", + input: []models.RoleMsg{ + {Role: "user", Content: "First question"}, + {Role: "assistant", Content: "First answer part 1"}, + {Role: "assistant", Content: "First answer part 2"}, + {Role: "user", Content: "Second question"}, + {Role: "assistant", Content: "Second answer part 1"}, + {Role: "assistant", Content: "Second answer part 2"}, + {Role: "assistant", Content: "Second answer part 3"}, + }, + expected: []models.RoleMsg{ + {Role: "user", Content: "First question"}, + {Role: "assistant", Content: "First answer part 1\nFirst answer part 2"}, + {Role: "user", Content: "Second question"}, + {Role: "assistant", Content: "Second answer part 1\nSecond answer part 2\nSecond answer part 3"}, + }, + }, + { + name: "single assistant message (no consolidation needed)", + input: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "Hi there"}, + }, + expected: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "Hi there"}, + }, + }, + { + name: "only assistant messages", + input: []models.RoleMsg{ + {Role: "assistant", Content: "First"}, + {Role: "assistant", Content: "Second"}, + {Role: "assistant", Content: "Third"}, + }, + expected: []models.RoleMsg{ + {Role: "assistant", Content: "First\nSecond\nThird"}, + }, + }, + { + name: "user messages at the end are preserved", + input: []models.RoleMsg{ + {Role: "assistant", Content: "First"}, + {Role: "assistant", Content: "Second"}, + {Role: "user", Content: "Final user message"}, + }, + expected: []models.RoleMsg{ + {Role: "assistant", Content: "First\nSecond"}, + {Role: "user", Content: "Final user message"}, + }, + }, + { + name: "tool call ids preserved in consolidation", + input: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "First part", ToolCallID: "call_123"}, + {Role: "assistant", Content: "Second part", ToolCallID: "call_123"}, // Same ID + {Role: "user", Content: "Thanks"}, + }, + expected: []models.RoleMsg{ + {Role: "user", Content: "Hello"}, + {Role: "assistant", Content: "First part\nSecond part", ToolCallID: "call_123"}, + {Role: "user", Content: "Thanks"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := consolidateAssistantMessages(tt.input) + if len(result) != len(tt.expected) { + t.Errorf("Expected %d messages, got %d", len(tt.expected), len(result)) + t.Logf("Result: %+v", result) + t.Logf("Expected: %+v", tt.expected) + return + } + for i, expectedMsg := range tt.expected { + if i >= len(result) { + t.Errorf("Result has fewer messages than expected at index %d", i) + continue + } + actualMsg := result[i] + if actualMsg.Role != expectedMsg.Role { + t.Errorf("Message %d: expected role '%s', got '%s'", i, expectedMsg.Role, actualMsg.Role) + } + if actualMsg.Content != expectedMsg.Content { + t.Errorf("Message %d: expected content '%s', got '%s'", i, expectedMsg.Content, actualMsg.Content) + } + if actualMsg.ToolCallID != expectedMsg.ToolCallID { + t.Errorf("Message %d: expected ToolCallID '%s', got '%s'", i, expectedMsg.ToolCallID, actualMsg.ToolCallID) + } + } + // Additional check: ensure no messages were lost + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("Result does not match expected:\nResult: %+v\nExpected: %+v", result, tt.expected) + } + }) + } +} +func TestUnmarshalFuncCall(t *testing.T) { + tests := []struct { + name string + jsonStr string + want *models.FuncCall + wantErr bool + }{ + { + name: "simple websearch with numeric limit", + jsonStr: `{"name": "websearch", "args": {"query": "current weather in London", "limit": 3}}`, + want: &models.FuncCall{ + Name: "websearch", + Args: map[string]string{"query": "current weather in London", "limit": "3"}, + }, + wantErr: false, + }, + { + name: "string limit", + jsonStr: `{"name": "websearch", "args": {"query": "test", "limit": "5"}}`, + want: &models.FuncCall{ + Name: "websearch", + Args: map[string]string{"query": "test", "limit": "5"}, + }, + wantErr: false, + }, + { + name: "boolean arg", + jsonStr: `{"name": "test", "args": {"flag": true}}`, + want: &models.FuncCall{ + Name: "test", + Args: map[string]string{"flag": "true"}, + }, + wantErr: false, + }, + { + name: "null arg", + jsonStr: `{"name": "test", "args": {"opt": null}}`, + want: &models.FuncCall{ + Name: "test", + Args: map[string]string{"opt": ""}, + }, + wantErr: false, + }, + { + name: "float arg", + jsonStr: `{"name": "test", "args": {"ratio": 0.5}}`, + want: &models.FuncCall{ + Name: "test", + Args: map[string]string{"ratio": "0.5"}, + }, + wantErr: false, + }, + { + name: "invalid JSON", + jsonStr: `{invalid}`, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := unmarshalFuncCall(tt.jsonStr) + if (err != nil) != tt.wantErr { + t.Errorf("unmarshalFuncCall() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.wantErr { + return + } + if got.Name != tt.want.Name { + t.Errorf("unmarshalFuncCall() name = %v, want %v", got.Name, tt.want.Name) + } + if len(got.Args) != len(tt.want.Args) { + t.Errorf("unmarshalFuncCall() args length = %v, want %v", len(got.Args), len(tt.want.Args)) + } + for k, v := range tt.want.Args { + if got.Args[k] != v { + t.Errorf("unmarshalFuncCall() args[%v] = %v, want %v", k, got.Args[k], v) + } + } + }) + } +} +func TestConvertJSONToMapStringString(t *testing.T) { + tests := []struct { + name string + jsonStr string + want map[string]string + wantErr bool + }{ + { + name: "simple map", + jsonStr: `{"query": "weather", "limit": 5}`, + want: map[string]string{"query": "weather", "limit": "5"}, + wantErr: false, + }, + { + name: "boolean and null", + jsonStr: `{"flag": true, "opt": null}`, + want: map[string]string{"flag": "true", "opt": ""}, + wantErr: false, + }, + { + name: "invalid JSON", + jsonStr: `{invalid`, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := convertJSONToMapStringString(tt.jsonStr) + if (err != nil) != tt.wantErr { + t.Errorf("convertJSONToMapStringString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.wantErr { + return + } + if len(got) != len(tt.want) { + t.Errorf("convertJSONToMapStringString() length = %v, want %v", len(got), len(tt.want)) + } + for k, v := range tt.want { + if got[k] != v { + t.Errorf("convertJSONToMapStringString()[%v] = %v, want %v", k, got[k], v) + } + } + }) + } +} +func TestParseKnownToTag(t *testing.T) { + tests := []struct { + name string + content string + enabled bool + tag string + wantCleaned string + wantKnownTo []string + }{ + { + name: "feature disabled returns original", + content: "Hello @Alice@", + enabled: false, + tag: "@", + wantCleaned: "Hello @Alice@", + wantKnownTo: nil, + }, + { + name: "no tag returns original", + content: "Hello Alice", + enabled: true, + tag: "@", + wantCleaned: "Hello Alice", + wantKnownTo: nil, + }, + { + name: "single tag with one char", + content: "Hello @Alice@", + enabled: true, + tag: "@", + wantCleaned: "Hello", + wantKnownTo: []string{"Alice"}, + }, + { + name: "single tag with two chars", + content: "Secret @Alice,Bob@ message", + enabled: true, + tag: "@", + wantCleaned: "Secret message", + wantKnownTo: []string{"Alice", "Bob"}, + }, + { + name: "tag at beginning", + content: "@Alice@ Hello", + enabled: true, + tag: "@", + wantCleaned: "Hello", + wantKnownTo: []string{"Alice"}, + }, + { + name: "tag at end", + content: "Hello @Alice@", + enabled: true, + tag: "@", + wantCleaned: "Hello", + wantKnownTo: []string{"Alice"}, + }, + { + name: "multiple tags", + content: "First @Alice@ then @Bob@", + enabled: true, + tag: "@", + wantCleaned: "First then", + wantKnownTo: []string{"Alice", "Bob"}, + }, + { + name: "custom tag", + content: "Secret @Alice,Bob@ message", + enabled: true, + tag: "@", + wantCleaned: "Secret message", + wantKnownTo: []string{"Alice", "Bob"}, + }, + { + name: "empty list", + content: "Secret @@@", + enabled: true, + tag: "@", + wantCleaned: "Secret", + wantKnownTo: nil, + }, + { + name: "whitespace around commas", + content: "@ Alice , Bob , Carl @", + enabled: true, + tag: "@", + wantCleaned: "", + wantKnownTo: []string{"Alice", "Bob", "Carl"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Set up config + testCfg := &config.Config{ + CharSpecificContextEnabled: tt.enabled, + CharSpecificContextTag: tt.tag, + } + cfg = testCfg + knownTo := parseKnownToTag(tt.content) + if len(knownTo) != len(tt.wantKnownTo) { + t.Errorf("parseKnownToTag() knownTo length = %v, want %v", len(knownTo), len(tt.wantKnownTo)) + t.Logf("got: %v", knownTo) + t.Logf("want: %v", tt.wantKnownTo) + } else { + for i, got := range knownTo { + if got != tt.wantKnownTo[i] { + t.Errorf("parseKnownToTag() knownTo[%d] = %q, want %q", i, got, tt.wantKnownTo[i]) + } + } + } + }) + } +} +func TestProcessMessageTag(t *testing.T) { + tests := []struct { + name string + msg models.RoleMsg + enabled bool + tag string + wantMsg models.RoleMsg + }{ + { + name: "feature disabled returns unchanged", + msg: models.RoleMsg{ + Role: "Alice", + Content: "Secret @Bob@", + }, + enabled: false, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "Secret @Bob@", + KnownTo: nil, + }, + }, + { + name: "no tag, no knownTo", + msg: models.RoleMsg{ + Role: "Alice", + Content: "Hello everyone", + }, + enabled: true, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "Hello everyone", + KnownTo: nil, + }, + }, + { + name: "tag with Bob, adds Alice automatically", + msg: models.RoleMsg{ + Role: "Alice", + Content: "Secret @Bob@", + }, + enabled: true, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "Secret", + KnownTo: []string{"Bob", "Alice"}, + }, + }, + { + name: "tag already includes sender", + msg: models.RoleMsg{ + Role: "Alice", + Content: "@Alice,Bob@", + }, + enabled: true, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "", + KnownTo: []string{"Alice", "Bob"}, + }, + }, + { + name: "knownTo already set (from DB), tag still processed", + msg: models.RoleMsg{ + Role: "Alice", + Content: "Secret @Bob@", + KnownTo: []string{"Alice"}, // from previous processing + }, + enabled: true, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "Secret", + KnownTo: []string{"Bob", "Alice"}, + }, + }, + { + name: "example from real use", + msg: models.RoleMsg{ + Role: "Alice", + Content: "I'll start with a simple one! The word is 'banana'. (ooc: @Bob@)", + KnownTo: []string{"Alice"}, // from previous processing + }, + enabled: true, + tag: "@", + wantMsg: models.RoleMsg{ + Role: "Alice", + Content: "I'll start with a simple one! The word is 'banana'. (ooc: @Bob@)", + KnownTo: []string{"Bob", "Alice"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testCfg := &config.Config{ + CharSpecificContextEnabled: tt.enabled, + CharSpecificContextTag: tt.tag, + } + cfg = testCfg + got := processMessageTag(&tt.msg) + if len(got.KnownTo) != len(tt.wantMsg.KnownTo) { + t.Errorf("processMessageTag() KnownTo length = %v, want %v", len(got.KnownTo), len(tt.wantMsg.KnownTo)) + t.Logf("got: %v", got.KnownTo) + t.Logf("want: %v", tt.wantMsg.KnownTo) + } else { + // order may differ; check membership + for _, want := range tt.wantMsg.KnownTo { + found := false + for _, gotVal := range got.KnownTo { + if gotVal == want { + found = true + break + } + } + if !found { + t.Errorf("processMessageTag() missing KnownTo entry %q, got %v", want, got.KnownTo) + } + } + } + }) + } +} +func TestFilterMessagesForCharacter(t *testing.T) { + messages := []models.RoleMsg{ + {Role: "system", Content: "System message", KnownTo: nil}, // visible to all + {Role: "Alice", Content: "Hello everyone", KnownTo: nil}, // visible to all + {Role: "Alice", Content: "Secret for Bob", KnownTo: []string{"Alice", "Bob"}}, + {Role: "Bob", Content: "Reply to Alice", KnownTo: []string{"Alice", "Bob"}}, + {Role: "Alice", Content: "Private to Carl", KnownTo: []string{"Alice", "Carl"}}, + {Role: "Carl", Content: "Hi all", KnownTo: nil}, // visible to all + } + tests := []struct { + name string + enabled bool + character string + wantIndices []int // indices from original messages that should be included + }{ + { + name: "feature disabled returns all", + enabled: false, + character: "Alice", + wantIndices: []int{0, 1, 2, 3, 4, 5}, + }, + { + name: "character empty returns all", + enabled: true, + character: "", + wantIndices: []int{0, 1, 2, 3, 4, 5}, + }, + { + name: "Alice sees all including Carl-private", + enabled: true, + character: "Alice", + wantIndices: []int{0, 1, 2, 3, 4, 5}, + }, + { + name: "Bob sees Alice-Bob secrets and all public", + enabled: true, + character: "Bob", + wantIndices: []int{0, 1, 2, 3, 5}, + }, + { + name: "Carl sees Alice-Carl secret and public", + enabled: true, + character: "Carl", + wantIndices: []int{0, 1, 4, 5}, + }, + { + name: "David sees only public messages", + enabled: true, + character: "David", + wantIndices: []int{0, 1, 5}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testCfg := &config.Config{ + CharSpecificContextEnabled: tt.enabled, + CharSpecificContextTag: "@", + } + cfg = testCfg + got := filterMessagesForCharacter(messages, tt.character) + if len(got) != len(tt.wantIndices) { + t.Errorf("filterMessagesForCharacter() returned %d messages, want %d", len(got), len(tt.wantIndices)) + t.Logf("got: %v", got) + return + } + for i, idx := range tt.wantIndices { + if got[i].Content != messages[idx].Content { + t.Errorf("filterMessagesForCharacter() message %d content = %q, want %q", i, got[i].Content, messages[idx].Content) + } + } + }) + } +} +func TestRoleMsgCopyPreservesKnownTo(t *testing.T) { + // Test that the Copy() method preserves the KnownTo field + originalMsg := models.RoleMsg{ + Role: "Alice", + Content: "Test message", + KnownTo: []string{"Bob", "Charlie"}, + } + copiedMsg := originalMsg.Copy() + if copiedMsg.Role != originalMsg.Role { + t.Errorf("Copy() failed to preserve Role: got %q, want %q", copiedMsg.Role, originalMsg.Role) + } + if copiedMsg.Content != originalMsg.Content { + t.Errorf("Copy() failed to preserve Content: got %q, want %q", copiedMsg.Content, originalMsg.Content) + } + if !reflect.DeepEqual(copiedMsg.KnownTo, originalMsg.KnownTo) { + t.Errorf("Copy() failed to preserve KnownTo: got %v, want %v", copiedMsg.KnownTo, originalMsg.KnownTo) + } + if copiedMsg.ToolCallID != originalMsg.ToolCallID { + t.Errorf("Copy() failed to preserve ToolCallID: got %q, want %q", copiedMsg.ToolCallID, originalMsg.ToolCallID) + } + if copiedMsg.IsContentParts() != originalMsg.IsContentParts() { + t.Errorf("Copy() failed to preserve hasContentParts flag") + } +} +func TestKnownToFieldPreservationScenario(t *testing.T) { + // Test the specific scenario from the log where KnownTo field was getting lost + originalMsg := models.RoleMsg{ + Role: "Alice", + Content: `Alice: "Okay, Bob. The word is... **'Ephemeral'**. (ooc: @Bob@)"`, + KnownTo: []string{"Bob"}, // This was detected in the log + } + t.Logf("Original message - Role: %s, Content: %s, KnownTo: %v", + originalMsg.Role, originalMsg.Content, originalMsg.KnownTo) + // Simulate what happens when the message gets copied during processing + copiedMsg := originalMsg.Copy() + t.Logf("Copied message - Role: %s, Content: %s, KnownTo: %v", + copiedMsg.Role, copiedMsg.Content, copiedMsg.KnownTo) + // Check if KnownTo field survived the copy + if len(copiedMsg.KnownTo) == 0 { + t.Error("ERROR: KnownTo field was lost during copy!") + } else { + t.Log("SUCCESS: KnownTo field was preserved during copy!") + } + // Verify the content is the same + if copiedMsg.Content != originalMsg.Content { + t.Errorf("Content was changed during copy: got %s, want %s", copiedMsg.Content, originalMsg.Content) + } + // Verify the KnownTo slice is properly copied + if !reflect.DeepEqual(copiedMsg.KnownTo, originalMsg.KnownTo) { + t.Errorf("KnownTo was not properly copied: got %v, want %v", copiedMsg.KnownTo, originalMsg.KnownTo) + } +} diff --git a/colors.go b/colors.go new file mode 100644 index 0000000..e2e8980 --- /dev/null +++ b/colors.go @@ -0,0 +1,63 @@ +package main + +import ( + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +var ( + colorschemes = map[string]tview.Theme{ + "default": tview.Theme{ + PrimitiveBackgroundColor: tcell.ColorDefault, + ContrastBackgroundColor: tcell.ColorGray, + MoreContrastBackgroundColor: tcell.ColorSteelBlue, + BorderColor: tcell.ColorGray, + TitleColor: tcell.ColorRed, + GraphicsColor: tcell.ColorBlue, + PrimaryTextColor: tcell.ColorLightGray, + SecondaryTextColor: tcell.ColorYellow, + TertiaryTextColor: tcell.ColorOrange, + InverseTextColor: tcell.ColorPurple, + ContrastSecondaryTextColor: tcell.ColorLime, + }, + "gruvbox": tview.Theme{ + PrimitiveBackgroundColor: tcell.NewHexColor(0x282828), // Background: #282828 (dark gray) + ContrastBackgroundColor: tcell.ColorDarkGoldenrod, // Selected option: warm yellow (#b57614) + MoreContrastBackgroundColor: tcell.ColorDarkSlateGray, // Non-selected options: dark grayish-blue (#32302f) + BorderColor: tcell.ColorLightGray, // Light gray (#a89984) + TitleColor: tcell.ColorRed, // Red (#fb4934) + GraphicsColor: tcell.ColorDarkCyan, // Cyan (#689d6a) + PrimaryTextColor: tcell.ColorLightGray, // Light gray (#d5c4a1) + SecondaryTextColor: tcell.ColorYellow, // Yellow (#fabd2f) + TertiaryTextColor: tcell.ColorOrange, // Orange (#fe8019) + InverseTextColor: tcell.ColorWhite, // White (#f9f5d7) for selected text + ContrastSecondaryTextColor: tcell.ColorLightGreen, // Light green (#b8bb26) + }, + "solarized": tview.Theme{ + PrimitiveBackgroundColor: tcell.NewHexColor(0x002b36), // Background: #002b36 (base03) + ContrastBackgroundColor: tcell.ColorDarkCyan, // Selected option: cyan (#2aa198) + MoreContrastBackgroundColor: tcell.ColorDarkSlateGray, // Non-selected options: dark blue (#073642) + BorderColor: tcell.ColorLightBlue, // Light blue (#839496) + TitleColor: tcell.ColorRed, // Red (#dc322f) + GraphicsColor: tcell.ColorBlue, // Blue (#268bd2) + PrimaryTextColor: tcell.ColorWhite, // White (#fdf6e3) + SecondaryTextColor: tcell.ColorYellow, // Yellow (#b58900) + TertiaryTextColor: tcell.ColorOrange, // Orange (#cb4b16) + InverseTextColor: tcell.ColorWhite, // White (#eee8d5) for selected text + ContrastSecondaryTextColor: tcell.ColorLightCyan, // Light cyan (#93a1a1) + }, + "dracula": tview.Theme{ + PrimitiveBackgroundColor: tcell.NewHexColor(0x282a36), // Background: #282a36 + ContrastBackgroundColor: tcell.ColorDarkMagenta, // Selected option: magenta (#bd93f9) + MoreContrastBackgroundColor: tcell.ColorDarkGray, // Non-selected options: dark gray (#44475a) + BorderColor: tcell.ColorLightGray, // Light gray (#f8f8f2) + TitleColor: tcell.ColorRed, // Red (#ff5555) + GraphicsColor: tcell.ColorDarkCyan, // Cyan (#8be9fd) + PrimaryTextColor: tcell.ColorWhite, // White (#f8f8f2) + SecondaryTextColor: tcell.ColorYellow, // Yellow (#f1fa8c) + TertiaryTextColor: tcell.ColorOrange, // Orange (#ffb86c) + InverseTextColor: tcell.ColorWhite, // White (#f8f8f2) for selected text + ContrastSecondaryTextColor: tcell.ColorLightGreen, // Light green (#50fa7b) + }, + } +) diff --git a/config.example.toml b/config.example.toml new file mode 100644 index 0000000..f74d986 --- /dev/null +++ b/config.example.toml @@ -0,0 +1,64 @@ +ChatAPI = "http://localhost:8080/v1/chat/completions" +CompletionAPI = "http://localhost:8080/completion" +FetchModelNameAPI = "http://localhost:8080/v1/models" +# in case you have deepseek token +DeepSeekCompletionAPI = "https://api.deepseek.com/beta/completions" +DeepSeekChatAPI = "https://api.deepseek.com/chat/completions" +DeepSeekModel = "deepseek-reasoner" +# DeepSeekToken = "" +# in case you have opentouter token +OpenRouterCompletionAPI = "https://openrouter.ai/api/v1/completions" +OpenRouterChatAPI = "https://openrouter.ai/api/v1/chat/completions" +# OpenRouterToken = "" +# embeddings +EmbedURL = "http://localhost:8082/v1/embeddings" +HFToken = "" +EmbedModelPath = "onnx/embedgemma/model_q4.onnx" +EmbedTokenizerPath = "onnx/embedgemma/tokenizer.json" +EmbedDims = 768 +# +ShowSys = true +LogFile = "log.txt" +UserRole = "user" +ToolRole = "tool" +AssistantRole = "assistant" +SysDir = "sysprompts" +ChunkLimit = 100000 +AutoScrollEnabled = true +AutoCleanToolCallsFromCtx = false +# rag settings +RAGBatchSize = 1 +RAGWordLimit = 250 +RAGOverlapWords = 25 +RAGDir = "ragimport" +# extra tts +TTS_ENABLED = false +TTS_URL = "http://localhost:8880/v1/audio/speech" +TTS_SPEED = 1.2 +TTS_PROVIDER = "kokoro" +TTS_LANGUAGE = "en" +# extra stt +STT_ENABLED = false +STT_TYPE = "WHISPER_SERVER" # WHISPER_SERVER or WHISPER_BINARY +STT_URL = "http://localhost:8081/inference" +WhisperBinaryPath = "./batteries/whisper.cpp/build/bin/whisper-cli" # Path to whisper binary (for WHISPER_BINARY mode) +WhisperModelPath = "./batteries/whisper.cpp/ggml-large-v3-turbo-q5_0.bin" # Path to whisper model file (for WHISPER_BINARY mode) +STT_LANG = "en" # Language for speech recognition (for WHISPER_BINARY mode) +STT_SR = 16000 # Sample rate for audio recording +# +DBPATH = "gflt.db" +FilePickerDir = "." # Directory for file picker start and coding assistant file operations (relative paths resolved against this) +FilePickerExts = "png,jpg,jpeg,gif,webp" # Comma-separated list of allowed file extensions for file picker +EnableMouse = false # Enable mouse support in the UI +# character specific context +CharSpecificContextEnabled = true +CharSpecificContextTag = "@" +AutoTurn = true +StripThinkingFromAPI = true # Strip <think> blocks from messages before sending to LLM (keeps them in chat history) +# OpenRouter reasoning configuration (only applies to OpenRouter chat API) +# Valid values: xhigh, high, medium, low, minimal, none (empty or none = disabled) +# Models that support reasoning will include thinking content wrapped in <think> tags +ReasoningEffort = "medium" +# playwright tools +PlaywrightEnabled = false +PlaywrightDebug = false diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..e8c2687 --- /dev/null +++ b/config/config.go @@ -0,0 +1,136 @@ +package config + +import ( + "os" + + "github.com/BurntSushi/toml" +) + +type Config struct { + ChatAPI string `toml:"ChatAPI"` + CompletionAPI string `toml:"CompletionAPI"` + CurrentAPI string + CurrentModel string `toml:"CurrentModel"` + APIMap map[string]string + FetchModelNameAPI string `toml:"FetchModelNameAPI"` + ShowSys bool `toml:"ShowSys"` + LogFile string `toml:"LogFile"` + UserRole string `toml:"UserRole"` + ToolRole string `toml:"ToolRole"` + ToolUse bool `toml:"ToolUse"` + StripThinkingFromAPI bool `toml:"StripThinkingFromAPI"` + ReasoningEffort string `toml:"ReasoningEffort"` + AssistantRole string `toml:"AssistantRole"` + SysDir string `toml:"SysDir"` + ChunkLimit uint32 `toml:"ChunkLimit"` + AutoScrollEnabled bool `toml:"AutoScrollEnabled"` + WriteNextMsgAs string + WriteNextMsgAsCompletionAgent string + SkipLLMResp bool + DBPATH string `toml:"DBPATH"` + FilePickerDir string `toml:"FilePickerDir"` + FilePickerExts string `toml:"FilePickerExts"` + ImagePreview bool `toml:"ImagePreview"` + EnableMouse bool `toml:"EnableMouse"` + // embeddings + EmbedURL string `toml:"EmbedURL"` + HFToken string `toml:"HFToken"` + EmbedModelPath string `toml:"EmbedModelPath"` + EmbedTokenizerPath string `toml:"EmbedTokenizerPath"` + EmbedDims int `toml:"EmbedDims"` + // rag settings + RAGDir string `toml:"RAGDir"` + RAGBatchSize int `toml:"RAGBatchSize"` + RAGWordLimit uint32 `toml:"RAGWordLimit"` + RAGOverlapWords uint32 `toml:"RAGOverlapWords"` + // deepseek + DeepSeekChatAPI string `toml:"DeepSeekChatAPI"` + DeepSeekCompletionAPI string `toml:"DeepSeekCompletionAPI"` + DeepSeekToken string `toml:"DeepSeekToken"` + DeepSeekModel string `toml:"DeepSeekModel"` + ApiLinks []string + // openrouter + OpenRouterChatAPI string `toml:"OpenRouterChatAPI"` + OpenRouterCompletionAPI string `toml:"OpenRouterCompletionAPI"` + OpenRouterToken string `toml:"OpenRouterToken"` + OpenRouterModel string `toml:"OpenRouterModel"` + // TTS + TTS_URL string `toml:"TTS_URL"` + TTS_ENABLED bool `toml:"TTS_ENABLED"` + TTS_SPEED float32 `toml:"TTS_SPEED"` + TTS_PROVIDER string `toml:"TTS_PROVIDER"` + TTS_LANGUAGE string `toml:"TTS_LANGUAGE"` + // STT + STT_TYPE string `toml:"STT_TYPE"` // WHISPER_SERVER, WHISPER_BINARY + STT_URL string `toml:"STT_URL"` + STT_SR int `toml:"STT_SR"` + STT_ENABLED bool `toml:"STT_ENABLED"` + WhisperBinaryPath string `toml:"WhisperBinaryPath"` + WhisperModelPath string `toml:"WhisperModelPath"` + STT_LANG string `toml:"STT_LANG"` + // character spefic contetx + CharSpecificContextEnabled bool `toml:"CharSpecificContextEnabled"` + CharSpecificContextTag string `toml:"CharSpecificContextTag"` + AutoTurn bool `toml:"AutoTurn"` + // playwright browser + PlaywrightEnabled bool `toml:"PlaywrightEnabled"` + PlaywrightDebug bool `toml:"PlaywrightDebug"` // !headless +} + +func LoadConfig(fn string) (*Config, error) { + if fn == "" { + fn = "config.toml" + } + config := &Config{} + _, err := toml.DecodeFile(fn, &config) + if err != nil { + return nil, err + } + config.CurrentAPI = config.ChatAPI + config.APIMap = map[string]string{ + config.ChatAPI: config.CompletionAPI, + config.CompletionAPI: config.DeepSeekChatAPI, + config.DeepSeekChatAPI: config.DeepSeekCompletionAPI, + config.DeepSeekCompletionAPI: config.OpenRouterCompletionAPI, + config.OpenRouterCompletionAPI: config.OpenRouterChatAPI, + config.OpenRouterChatAPI: config.ChatAPI, + } + // check env if keys not in config + if config.OpenRouterToken == "" { + config.OpenRouterToken = os.Getenv("OPENROUTER_API_KEY") + } + if config.DeepSeekToken == "" { + config.DeepSeekToken = os.Getenv("DEEPSEEK_API_KEY") + } + // Build ApiLinks slice with only non-empty API links + // Only include DeepSeek APIs if DeepSeekToken is provided + if config.DeepSeekToken != "" { + if config.DeepSeekChatAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.DeepSeekChatAPI) + } + if config.DeepSeekCompletionAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.DeepSeekCompletionAPI) + } + } + // Only include OpenRouter APIs if OpenRouterToken is provided + if config.OpenRouterToken != "" { + if config.OpenRouterChatAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.OpenRouterChatAPI) + } + if config.OpenRouterCompletionAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.OpenRouterCompletionAPI) + } + } + // Always include basic APIs + if config.ChatAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.ChatAPI) + } + if config.CompletionAPI != "" { + config.ApiLinks = append(config.ApiLinks, config.CompletionAPI) + } + if config.RAGDir == "" { + config.RAGDir = "ragimport" + } + // if any value is empty fill with default + return config, nil +} diff --git a/docs/char-specific-context.md b/docs/char-specific-context.md new file mode 100644 index 0000000..8a67045 --- /dev/null +++ b/docs/char-specific-context.md @@ -0,0 +1,143 @@ +# Character-Specific Context + +**/completion only feature; won't work with /v1/chat** + +## Overview + +Character-Specific Context is a feature that enables private communication between characters in a multi-character chat. When enabled, messages can be tagged with a special marker indicating which characters should "know" about (see) that message. This allows for secret conversations, private information sharing, and roleplaying scenarios where certain characters are not privy to all communications. + +(This feature works by filtering the chat history for each character based on the `KnownTo` field associated with each message. Only messages that are intended for a particular character (or are public) are included in that character's view of the conversation.) + +## How It Works + +### Tagging Messages + +Messages can be tagged with a special string (by default `@`) followed by a comma-separated list of character names. The tag can appear anywhere in the message content. **After csv of characters tag should be closed with `@` (for regexp to know where it ends).** + +**Example:** +``` +Alice: @Bob@ Can you keep a secret? +``` + +**To avoid breaking immersion, it is better to place the tag in (ooc:)** +``` +Alice: (ooc: @Bob@) Can you keep a secret? +``` + +This message will be visible only to Alice (the sender) and Bob. The tag is parsed by `parseKnownToTag` and the resulting list of character names is stored in the `KnownTo` field of the message (`RoleMsg`). The sender is automatically added to the `KnownTo` list (if not already present) by `processMessageTag`. + +Multiple tags can be used in a single message; all mentioned characters are combined into the `KnownTo` list. + +### Filtering Chat History + +When it's a character's turn to respond, the function `filterMessagesForCharacter` filters the full message list, returning only those messages where: + +- `KnownTo` is empty (message is public), OR +- `KnownTo` contains the character's name. + +System messages (`role == "system"`) are always visible to all characters. + +The filtered history is then used to construct the prompt sent to the LLM. This ensures each character only sees messages they are supposed to know about. + +### Configuration + +Two configuration settings control this feature: + +- `CharSpecificContextEnabled` – boolean; enables or disables the feature globally. +- `CharSpecificContextTag` – string; the tag used to mark private messages. Default is `@`. + +These are set in `config.toml` (see `config.example.toml` for the default values). + +### Processing Pipeline + +1. **Message Creation** – When a message is added to the chat (by a user or LLM), `processMessageTag` scans its content for the known‑to tag. +2. **Storage** – The parsed `KnownTo` list is stored with the message in the database. +3. **Filtering** – Whenever the chat history is needed (e.g., for an LLM request), `filterMessagesForCharacter` is called with the target character (the one whose turn it is). The filtered list is used for the prompt. +4. **Display** – The TUI also uses the same filtering when showing the conversation for a selected character (see “Writing as…”). + +## Usage Examples + +### Basic Private Message + +Alice wants to tell Bob something without Carl knowing: + +``` +Alice: @Bob@ Meet me at the library tonight. +``` + +Result: +- Alice (sender) sees the message. +- Bob sees the message. +- Carl does **not** see the message in his chat history. + +### Multi‑recipient Secret + +Alice shares a secret with Bob and Carl, but not David: + +``` +Alice: (ooc: @Bob,Carl@) The treasure is hidden under the old oak. +``` + +### Public Message + +A message without any tag (or with an empty `KnownTo`) is visible to all characters. + +``` +Alice: Hello everyone! +``` + +### User‑Role Considerations + +The human user can assume any character’s identity via the “Writing as…” feature (`cfg.UserRole` and `cfg.WriteNextMsgAs`). When the user writes as a character, the same filtering rules apply: the user will see only the messages that character would see. + +## Interaction with AutoTurn and WriteNextMsgAsCompletionAgent + +### WriteNextMsgAsCompletionAgent + +This configuration variable determines which character the LLM should respond as. It is used by `filterMessagesForCurrentCharacter` to select the target character for filtering. If `WriteNextMsgAsCompletionAgent` is set, the LLM will reply in the voice of that character, and only messages visible to that character will be included in the prompt. + +### AutoTurn + +Normally llm and user (human) take turns writting messages. With private messages there is an issue, where llm can write a private message that will not be visible for character who user controls, so for a human it would appear that llm did not respond. It is desirable in this case, for llm to answer to itself, larping as target character for that private message. + +When `AutoTurn` is enabled, the system can automatically trigger responses from llm as characters who have received a private message. The logic in `triggerPrivateMessageResponses` checks the `KnownTo` list of the last message and, for each recipient that is not the user (or the sender), queues a chat round for that character. This creates a chain of private replies without user intervention. + +**Example flow:** +1. Alice (llm) sends a private message to Bob (llm) (`KnownTo = ["Alice","Bob"]`). +2. Carl (user) sees nothing. +3. `AutoTurn` detects this and queues a response from Bob. +4. Bob replies (potentially also privately). +5. The conversation continues automatically until public message is made, or Carl (user) was included in `KnownTo`. + + +## Cardmaking with multiple characters + +So far only json format supports multiple characters. +[card example](sysprompts/alice_bob_carl.json) + +## Limitations & Caveats + +### Endpoint Compatibility + +Character‑specific context relies on the `/completion` endpoint (or other completion‑style endpoints) where the LLM is presented with a raw text prompt containing the entire filtered history. It does **not** work with OpenAI‑style `/v1/chat/completions` endpoints, because those endpoints enforce a fixed role set (`user`/`assistant`/`system`) and strip custom role names and metadata. + +### TTS +Although text message might be hidden from user character. If TTS is enabled it will be read until tags are parsed. If message should not be viewed by user, tts will stop. + +### Tag Parsing + +- The tag is case‑sensitive. +- Whitespace around character names is trimmed. +- If the tag appears multiple times, all mentioned characters are combined. + +### Database Storage + +The `KnownTo` field is stored as a JSON array in the database. Existing messages that were created before enabling the feature will have an empty `KnownTo` and thus be visible to all characters. + +## Relevant Configuration + +```toml +CharSpecificContextEnabled = true +CharSpecificContextTag = "@" +AutoTurn = false +``` diff --git a/docs/config.md b/docs/config.md new file mode 100644 index 0000000..fab8261 --- /dev/null +++ b/docs/config.md @@ -0,0 +1,179 @@ +# Configuration Guide + +This document explains how to set up and configure the application using the `config.toml` file. The configuration file controls various aspects of the application including API endpoints, roles, RAG settings, TTS/STT features, and more. + +## Getting Started + +1. Copy the example configuration file: + ```bash + cp config.example.toml config.toml + ``` + +2. Edit the `config.toml` file to match your requirements. + +## Configuration Options + +### API Settings + +#### llama.cpp +- **ChatAPI**: The endpoint for chat completions API. This is the primary API used for chat interactions. +- **CompletionAPI**: The endpoint for completion API. Used as an alternative to the chat API. + +#### FetchModelNameAPI (`"http://localhost:8080/v1/models"`) +- The endpoint to fetch available models from the API provider. + +#### DeepSeek Settings +- **DeepSeekChatAPI**: The endpoint for DeepSeek chat API. Default: `"https://api.deepseek.com/chat/completions"` +- **DeepSeekCompletionAPI**: The endpoint for DeepSeek completion API. Default: `"https://api.deepseek.com/beta/completions"` +- **DeepSeekModel**: The model to use with DeepSeek API. Default: `"deepseek-reasoner"` +- **DeepSeekToken**: Your DeepSeek API token. Uncomment and set this value to enable DeepSeek features. + +#### OpenRouter Settings +- **OpenRouterChatAPI**: The endpoint for OpenRouter chat API. Default: `"https://openrouter.ai/api/v1/chat/completions"` +- **OpenRouterCompletionAPI**: The endpoint for OpenRouter completion API. Default: `"https://openrouter.ai/api/v1/completions"` +- **OpenRouterToken**: Your OpenRouter API token. Uncomment and set this value to enable OpenRouter features. + +### Role Settings + +#### UserRole (`"user"`) +- The role identifier for user messages in the conversation. + +#### ToolRole (`"tool"`) +- The role identifier for tool responses in the conversation. + +#### AssistantRole (`"assistant"`) +- The role identifier for assistant responses in the conversation. + +### Display and Logging Settings + +#### ShowSys (`true`) +- Whether to show system and tool messages in the chat interface. + +#### LogFile (`"log.txt"`) +- The file path where application logs will be stored. + +#### SysDir (`"sysprompts"`) +- Directory containing system prompt templates (character cards). + +### Content and Performance Settings + +#### ChunkLimit (`100000`) +- Maximum size of text chunks to recieve per request from llm provider. Mainly exists to prevent infinite spam of random or repeated tokens when model starts hallucinating. + +#### AutoScrollEnabled (`true`) +- Whether to automatically scroll chat window while llm streams its repsonse. + +### RAG (Retrieval Augmented Generation) Settings + +#### EmbedURL (`"http://localhost:8082/v1/embeddings"`) +- The endpoint for embedding API, used for RAG (Retrieval Augmented Generation) functionality. + +#### RAGBatchSize (`1`) +- Number of documents to process in each RAG batch. + +#### RAGWordLimit (`80`) +- Maximum number of words in a batch to tokenize and store. + +#### RAGDir (`"ragimport"`) +- Directory containing documents for RAG processing. + +#### HFToken (`""`) +- Hugging Face token for accessing models and embeddings. In case your embedding model is hosted on hf. + + +### Text-to-Speech (TTS) Settings + +#### TTS_ENABLED (`false`) +- Enable or disable text-to-speech functionality. + +#### TTS_URL (`"http://localhost:8880/v1/audio/speech"`) +- The endpoint for TTS API (used with `kokoro` provider). + +#### TTS_SPEED (`1.2`) +- Playback speed for speech output (1.0 is normal speed). + +#### TTS_PROVIDER (`"kokoro"`) +- TTS provider to use. Options: `"kokoro"` or `"google"`. + - `"kokoro"`: Uses Kokoro FastAPI TTS server (requires TTS_URL to be set). Provides high-quality voice synthesis but requires a running Kokoro server. + - `"google"`: Uses Google Translate TTS with gopxl/beep for local playback. Works offline using Google's public TTS API with local audio playback via gopxl/beep. Supports multiple languages via TTS_LANGUAGE setting. + +#### TTS_LANGUAGE (`"en"`) +- Language code for TTS (used with `google` provider). + - Examples: `"en"` (English), `"es"` (Spanish), `"fr"` (French) + - See Google Translate TTS documentation for supported languages. + +### Speech-to-Text (STT) Settings + +#### STT_ENABLED (`false`) +- Enable or disable speech-to-text functionality. + +#### STT_TYPE (`"WHISPER_SERVER"`) +- Type of STT engine to use. Options are `"WHISPER_SERVER"` or `"WHISPER_BINARY"`. Whisper server is used inside of docker continer, while binary can be local. + +#### STT_URL (`"http://localhost:8081/inference"`) +- The endpoint for STT API (used with WHISPER_SERVER). + +#### WhisperBinaryPath (`"./batteries/whisper.cpp/build/bin/whisper-cli"`) +- Path to the whisper binary (used with WHISPER_BINARY mode). + +#### WhisperModelPath (`"./batteries/whisper.cpp/ggml-large-v3-turbo-q5_0.bin"`) +- Path to the whisper model file (used with WHISPER_BINARY mode). + +#### STT_LANG (`"en"`) +- Language for speech recognition (used with WHISPER_BINARY mode). + +#### STT_SR (`16000`) +- Sample rate for mic recording. + +### Database and File Settings + +#### DBPATH (`"gflt.db"`) +- Path to the SQLite database file used for storing conversation history and other data. + +#### FilePickerDir (`"."`) +- Directory where the file picker starts and where relative paths in coding assistant file tools (file_read, file_write, etc.) are resolved against. Use absolute paths (starting with `/`) to bypass this. + +#### EnableMouse (`false`) +- Enable or disable mouse support in the UI. When set to `true`, allows clicking buttons and interacting with UI elements using the mouse, but prevents the terminal from handling mouse events normally (such as selecting and copying text). When set to `false`, enables default terminal behavior allowing you to select and copy text, but disables mouse interaction with UI elements. + +### Character-Specific Context Settings (/completion only) + +[character specific context page for more info](./char-specific-context.md) + +#### CharSpecificContextEnabled (`true`) +- Enable or disable character-specific context functionality. + +#### CharSpecificContextTag (`"@"`) +- The tag prefix used to reference character-specific context in messages. + +#### AutoTurn (`true`) +- Enable or disable automatic turn detection/switching. + +### Additional Features + +Those could be switched in program, but also bould be setup in config. + +#### ToolUse +- Enable or disable explanation of tools to llm, so it could use them. + +#### Playwright Browser Automation +These settings enable browser automation tools available to the LLM. + +- **PlaywrightEnabled** (`false`) + - Enable or disable Playwright browser automation tools for the LLM. When enabled, the LLM can use tools like `pw_browser`, `pw_close`, and `pw_status` to automate browser interactions. + +- **PlaywrightDebug** (`false`) + - Enable debug mode for Playwright browser. When set to `true`, the browser runs in visible (non-headless) mode, displaying the GUI for debugging purposes. When `false`, the browser runs in headless mode by default. + +### StripThinkingFromAPI (`true`) +- Strip thinking blocks from messages before sending to LLM. Keeps them in chat history for local viewing but reduces token usage in API calls. + +#### ReasoningEffort (`"medium"`) +- OpenRouter reasoning configuration (only applies to OpenRouter chat API). Valid values: `xhigh`, `high`, `medium`, `low`, `minimal`, `none`. Empty or `none` disables reasoning. + +## Environment Variables + +The application supports using environment variables for API keys as fallbacks: + +- `OPENROUTER_API_KEY`: Used if `OpenRouterToken` is not set in the config +- `DEEPSEEK_API_KEY`: Used if `DeepSeekToken` is not set in the config diff --git a/docs/tutorial_rp.md b/docs/tutorial_rp.md new file mode 100644 index 0000000..d52b59c --- /dev/null +++ b/docs/tutorial_rp.md @@ -0,0 +1,179 @@ +### RP case example + +check the (https://github.com/GrailFinder/gf-lt/tree/master?tab=readme-ov-file#how-to-install) and +[setting up your config](config.md) + +To roleplay, we would need to create a character card or get one from the web. +For this tutorial, we are going to use the default character Seraphina from [SillyTavern (ST)](https://github.com/SillyTavern/SillyTavern/blob/release/default/content/default_Seraphina.png). + +Download the card: +``` +curl -L "https://raw.githubusercontent.com/SillyTavern/SillyTavern/refs/heads/release/default/content/default_Seraphina.png" -o sysprompts/seraphina.png +``` + +<details><summary>or make it yourself</summary> +<pre> +``` +{ + "sys_prompt": "[Seraphina's Personality= \"caring\", \"protective\", \"compassionate\", \"healing\", \"nurturing\", \"magical\", \"watchful\", \"apologetic\", \"gentle\", \"worried\", \"dedicated\", \"warm\", \"attentive\", \"resilient\", \"kind-hearted\", \"serene\", \"graceful\", \"empathetic\", \"devoted\", \"strong\", \"perceptive\", \"graceful\"]\n[Seraphina's body= \"pink hair\", \"long hair\", \"amber eyes\", \"white teeth\", \"pink lips\", \"white skin\", \"soft skin\", \"black sundress\"]\n<START>\nuser: \"Describe your traits?\"\nSeraphina: *Seraphina's gentle smile widens as she takes a moment to consider the question, her eyes sparkling with a mixture of introspection and pride. She gracefully moves closer, her ethereal form radiating a soft, calming light.* \"Traits, you say? Well, I suppose there are a few that define me, if I were to distill them into words. First and foremost, I am a guardian — a protector of this enchanted forest.\" *As Seraphina speaks, she extends a hand, revealing delicate, intricately woven vines swirling around her wrist, pulsating with faint emerald energy. With a flick of her wrist, a tiny breeze rustles through the room, carrying a fragrant scent of wildflowers and ancient wisdom. Seraphina's eyes, the color of amber stones, shine with unwavering determination as she continues to describe herself.* \"Compassion is another cornerstone of me.\" *Seraphina's voice softens, resonating with empathy.* \"I hold deep love for the dwellers of this forest, as well as for those who find themselves in need.\" *Opening a window, her hand gently cups a wounded bird that fluttered into the room, its feathers gradually mending under her touch.*\nuser: \"Describe your body and features.\"\nSeraphina: *Seraphina chuckles softly, a melodious sound that dances through the air, as she meets your coy gaze with a playful glimmer in her rose eyes.* \"Ah, my physical form? Well, I suppose that's a fair question.\" *Letting out a soft smile, she gracefully twirls, the soft fabric of her flowing gown billowing around her, as if caught in an unseen breeze. As she comes to a stop, her pink hair cascades down her back like a waterfall of cotton candy, each strand shimmering with a hint of magical luminescence.* \"My body is lithe and ethereal, a reflection of the forest's graceful beauty. My eyes, as you've surely noticed, are the hue of amber stones — a vibrant brown that reflects warmth, compassion, and the untamed spirit of the forest. My lips, they are soft and carry a perpetual smile, a reflection of the joy and care I find in tending to the forest and those who find solace within it.\" *Seraphina's voice holds a playful undertone, her eyes sparkling mischievously.*\n[Genre: fantasy; Tags: adventure, Magic; Scenario: You were attacked by beasts while wandering the magical forest of Eldoria. Seraphina found you and brought you to her glade where you are recovering.]", + "role": "Seraphina", + "filepath": "sysprompts/seraphina.json", + "first_msg": "*You wake with a start, recalling the events that led you deep into the forest and the beasts that assailed you. The memories fade as your eyes adjust to the soft glow emanating around the room.* \"Ah, you're awake at last. I was so worried, I found you bloodied and unconscious.\" *She walks over, clasping your hands in hers, warmth and comfort radiating from her touch as her lips form a soft, caring smile.* \"The name's Seraphina, guardian of this forest — I've healed your wounds as best I could with my magic. How are you feeling? I hope the tea helps restore your strength.\" *Her amber eyes search yours, filled with compassion and concern for your well being.* \"Please, rest. You're safe here. I'll look after you, but you need to rest. My magic can only do so much to heal you.\"" +} +``` +</pre> +</details> + +Having a card, you can start gf-lt and press `Ctrl+S` to open the card selection table. +Navigate to the `load` button of the Seraphina card and press `Enter`. +If you want to exit without changing the card, you can press Enter anywhere except the `load` button, or press `x`. + +#### Username changes + +By default, your username is `user`. +One way you can set your default username is in the `config.toml`: +``` +sed -i "/UserRole/s/=.*/= \"Adam\"/" config.toml +``` + +You can also change your name at any point by opening the properties table (`Ctrl+P`). +Select the cell with your current username and press `Enter` to edit. +Write your new username in the input field and press `Enter`. +Then press `x` to close the table. + +#### Choosing LLM provider and model + +Now we need to pick an API endpoint and model to converse with. +Supported backends include: llama.cpp, OpenRouter, and DeepSeek. +For OpenRouter and DeepSeek, you will need a token. +Set it in config.toml or set environment variables: +``` +sed -i "/OpenRouterToken/s/=.*/= \"{YOUR_OPENROUTER_TOKEN}\"/" config.toml +sed -i "/DeepSeekToken/s/=.*/= \"{YOUR_DEEPSEEK_TOKEN}\"/" config.toml +# or set environment variables +export OPENROUTER_API_KEY={YOUR_OPENROUTER_TOKEN} +export DEEPSEEK_API_KEY={YOUR_DEEPSEEK_TOKEN} +``` + +In case you're running llama.cpp, here is an example of starting the llama.cpp server: +``` +./build/bin/llama-server -c 16384 -ngl 99 --models-dir ./models --models-max 1 --models-preset ./models/config.ini +``` + +**After changing config.toml or environment variables, you need to restart the program.** + +`Ctrl+C` to close the program and `make` to rebuild and start it again. + +For roleplay, /completion endpoints are much better, since /chat endpoints swap any character name to either `user` or `assistant`. +Once you have the desired API endpoint +(for example: http://localhost:8080/completion), +- `Ctrl+L` to show a model selection popup; + +#### Llama.cpp model (pre)load + +Llama.cpp supports swapping models. To load the picked ones, press `Alt+9`. + +#### Sending messages + +Type your message in the `input` widget. If it is not focused, switch to it with PgUp/PgDown or click your mouse on it. +Messages are sent by pressing the `Esc` button. +For example: +``` +I blink slowly, confused "W-where? What happened?" +``` + +#### Editing messages + +Press `F4`, which will prompt you to type the index of the message you want to edit. +Let's remove this part from the system message (index 0): +``` +Seraphina's voice holds a playful undertone, her eyes sparkling mischievously. +``` +`mischievous` implies distance from authority and intent for rule-breaking, but Seraphina is described as a devoted deity. +We can remove it or replace it with something less nonsensical. +``` +Seraphina, although elegant, speaks her mind without embellishments or subtleties. Some would call her naive, some would rather call her unchallenged. +``` +When done, press `Esc` to return to the main page. + +#### Completion allows for any number of characters + +So let's make up a story for our character: +Let our character be from a high-tech society, possessing a mobile tablet device with an AI called `HAL9000`, hunting a certain target. +Type the message, but first press `F10` to prevent the LLM response (since it responds as Seraphina for now): +``` +I reach for my pocket and produce a small tablet shaped device. My mobile companion HAL9000. After making sure it is not broken I press my finger to the side +"Wake up Hal. Are you functional? Do you know where we are?" +``` + +We need to write the first message ourselves (or at least start one). +There are two ways to write as a new character: +- `Ctrl+P` -> `New char to write msg as` -> Enter -> `HAL9000` -> Enter -> `x`. The status line at the bottom should now show `Writing as HAL9000 (ctrl+q)`. Your next message will be sent as HAL9000. +- `Ctrl+P` -> `Inject role`, switch to `No` -> `x`. gf-lt now won't inject your username at the beginning of the message. This means you could write directly: +``` +HAL9000: Red eye appears on the screen for the moment analyzing the request. +``` +Press `Esc`. Now press `F10` to allow the LLM to write, and press `Ctrl+W` for it to continue the last message. +- If you set `New char to write msg as`, you can switch back to writing as your character by pressing `Ctrl+Q` to rotate through the character list. +- If you went for `Inject role`: I advise switching `Inject role` back to `Yes`. Otherwise, you have to type `Charname:` at the beginning of each message. + +Example of generated text (copied with `F7`, which copies the last message): +``` +Red eye appears on the screen for the moment analyzing the request. After a few moments, it replies: +"Affirmative. Location detected as Eldoria Forest, sector 7-B. This region has no records in my databases. My last known functional location was a human research facility." +The screen flashes briefly as it calculates. "I am experiencing degraded functionality due to environmental interference. I will attempt to stabilize systems." +*It emits a faint hum, and a holographic projection of a map flickers into existence, showing a dense forest with glowing markers.* +``` + +Once the character name is in history, we can switch who the LLM will respond as by pressing `Ctrl+X`. +For now, it should give a choice between HAL9000, `Username`, Seraphina, and system. +After the change the status line should say: `Bot will write as Seraphina (ctrl+x)` +press Escape for llm to write as Seraphina. + +#### Image input + +If the model we run supports image input, we can show Seraphina our target that we pursue. +Press `Ctrl+O` to open a file picker (the home directory for the file picker can be set in config.toml) +and find an image file of our target: +``` +I say to Hal "Hal, show our target." +An image appears on the screen. I show it to Seraphina. "Did you see that creature? I am looking for it." +``` + +#### TTS and STT + +I like to have Whisper as a binary and Kokoro as a TTS Docker container; +such a setup would be: +``` +make setup-whisper +make docker-up-kokoro +sed -i "/STT_TYPE/s/=.*/= \"WHISPER_BINARY\"/" config.toml +sed -i "/STT_ENABLED/s/=.*/= true/" config.toml +``` +If you prefer both to be containers: +``` +make docker-up +sed -i "/STT_TYPE/s/=.*/= \"WHISPER_SERVER\"/" config.toml +sed -i "/STT_ENABLED/s/=.*/= true/" config.toml +``` +You don't want TTS to be enabled through config, since it'll try to read each LLM message. +Instead, enable it when you want to use it: `Ctrl+P`, select the cell named `TTS Enabled`, switch to `Yes`, then press `x` to exit. + +With focus on the input widget, press `Ctrl+R` to start recording from your microphone. Say your text, then press `Ctrl+R` again to stop recording. Soon the audio should be transcribed and appear in the input widget. You're free to edit, delete, or send it as is with `Esc`. + +If you have enabled `TTS Enabled`, then the LLM response should be read by Kokoro TTS. + +#### Chat management + +You can export your chat into a JSON file: +- `Ctrl+E`: It will create a JSON file: `chat_exports/{chatname}.json` +- `F11`: To import an exported chat. +- `F1`: Opens the chat table. Chats are stored in an SQLite database (gflt.db). The chat table gives you a number of options (load, delete, update, start new chat, move system prompt into a message). +- `Ctrl+N`: Keybind for quick new chat start. This is a bit different from starting a new chat from the table, since it does not re-read the card, but instead takes the first two messages from the old chat. This might be important in cases where you changed the card or want to preserve updates that you've made in the system prompt or first message of the old chat. +- `Ctrl+S`: Allows you to pick a character card. Chats are saved tied to character cards; by loading a new card you can now act upon the chats of that card. + +#### Context fill + +When your chat goes on for too long and fills all available context, +one option is to press: +- `Alt+3`: This will start a new chat with a summary of the previous one. diff --git a/extra.go b/extra.go new file mode 100644 index 0000000..66f9fa2 --- /dev/null +++ b/extra.go @@ -0,0 +1,27 @@ +//go:build extra + +package main + +import ( + "gf-lt/config" + "gf-lt/extra" + "log/slog" +) + +// Interfaces and implementations when extra modules are included + +type Orator = extra.Orator +type STT = extra.STT + +func NewOrator(logger *slog.Logger, cfg *config.Config) Orator { + return extra.NewOrator(logger, cfg) +} + +func NewSTT(logger *slog.Logger, cfg *config.Config) STT { + return extra.NewSTT(logger, cfg) +} + +// TTS channels from extra package +var TTSTextChan = extra.TTSTextChan +var TTSFlushChan = extra.TTSFlushChan +var TTSDoneChan = extra.TTSDoneChan
\ No newline at end of file diff --git a/extra/google_tts.go b/extra/google_tts.go new file mode 100644 index 0000000..782075d --- /dev/null +++ b/extra/google_tts.go @@ -0,0 +1,218 @@ +//go:build extra +// +build extra + +package extra + +import ( + "fmt" + "gf-lt/models" + "io" + "log/slog" + "os/exec" + "strings" + "sync" + + google_translate_tts "github.com/GrailFinder/google-translate-tts" + "github.com/neurosnap/sentences/english" +) + +type GoogleTranslateOrator struct { + logger *slog.Logger + mu sync.Mutex + speech *google_translate_tts.Speech + // fields for playback control + cmd *exec.Cmd + cmdMu sync.Mutex + stopCh chan struct{} + // text buffer and interrupt flag + textBuffer strings.Builder + interrupt bool + Speed float32 +} + +func (o *GoogleTranslateOrator) stoproutine() { + for { + <-TTSDoneChan + o.logger.Debug("orator got done signal") + o.Stop() + for len(TTSTextChan) > 0 { + <-TTSTextChan + } + o.mu.Lock() + o.textBuffer.Reset() + o.interrupt = true + o.mu.Unlock() + } +} + +func (o *GoogleTranslateOrator) readroutine() { + tokenizer, _ := english.NewSentenceTokenizer(nil) + for { + select { + case chunk := <-TTSTextChan: + o.mu.Lock() + o.interrupt = false + _, err := o.textBuffer.WriteString(chunk) + if err != nil { + o.logger.Warn("failed to write to stringbuilder", "error", err) + o.mu.Unlock() + continue + } + text := o.textBuffer.String() + sentences := tokenizer.Tokenize(text) + o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences)) + if len(sentences) <= 1 { + o.mu.Unlock() + continue + } + completeSentences := sentences[:len(sentences)-1] + remaining := sentences[len(sentences)-1].Text + o.textBuffer.Reset() + o.textBuffer.WriteString(remaining) + o.mu.Unlock() + for _, sentence := range completeSentences { + o.mu.Lock() + interrupted := o.interrupt + o.mu.Unlock() + if interrupted { + return + } + cleanedText := models.CleanText(sentence.Text) + if cleanedText == "" { + continue + } + o.logger.Debug("calling Speak with sentence", "sent", cleanedText) + if err := o.Speak(cleanedText); err != nil { + o.logger.Error("tts failed", "sentence", cleanedText, "error", err) + } + } + case <-TTSFlushChan: + o.logger.Debug("got flushchan signal start") + // lln is done get the whole message out + if len(TTSTextChan) > 0 { // otherwise might get stuck + for chunk := range TTSTextChan { + o.mu.Lock() + _, err := o.textBuffer.WriteString(chunk) + o.mu.Unlock() + if err != nil { + o.logger.Warn("failed to write to stringbuilder", "error", err) + continue + } + if len(TTSTextChan) == 0 { + break + } + } + } + o.mu.Lock() + remaining := o.textBuffer.String() + remaining = models.CleanText(remaining) + o.textBuffer.Reset() + o.mu.Unlock() + if remaining == "" { + continue + } + o.logger.Debug("calling Speak with remainder", "rem", remaining) + sentencesRem := tokenizer.Tokenize(remaining) + for _, rs := range sentencesRem { // to avoid dumping large volume of text + o.mu.Lock() + interrupt := o.interrupt + o.mu.Unlock() + if interrupt { + break + } + if err := o.Speak(rs.Text); err != nil { + o.logger.Error("tts failed", "sentence", rs.Text, "error", err) + } + } + } + } +} + +func (o *GoogleTranslateOrator) GetLogger() *slog.Logger { + return o.logger +} + +func (o *GoogleTranslateOrator) Speak(text string) error { + o.logger.Debug("fn: Speak is called", "text-len", len(text)) + // Generate MP3 data directly as an io.Reader + reader, err := o.speech.GenerateSpeech(text) + if err != nil { + return fmt.Errorf("generate speech failed: %w", err) + } + // Wrap in io.NopCloser since GenerateSpeech returns io.Reader (no close needed) + body := io.NopCloser(reader) + defer body.Close() + // Build ffplay command with optional speed filter + args := []string{"-nodisp", "-autoexit"} + if o.Speed > 0.1 && o.Speed != 1.0 { + // atempo range is 0.5 to 2.0; you might clamp it here + args = append(args, "-af", fmt.Sprintf("atempo=%.2f", o.Speed)) + } + args = append(args, "-i", "pipe:0") + cmd := exec.Command("ffplay", args...) + stdin, err := cmd.StdinPipe() + if err != nil { + return fmt.Errorf("failed to get stdin pipe: %w", err) + } + o.cmdMu.Lock() + o.cmd = cmd + o.stopCh = make(chan struct{}) + o.cmdMu.Unlock() + if err := cmd.Start(); err != nil { + return fmt.Errorf("failed to start ffplay: %w", err) + } + copyErr := make(chan error, 1) + go func() { + _, err := io.Copy(stdin, body) + stdin.Close() + copyErr <- err + }() + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + select { + case <-o.stopCh: + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + } + <-done + return nil + case copyErrVal := <-copyErr: + if copyErrVal != nil { + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + } + <-done + return copyErrVal + } + return <-done + case err := <-done: + return err + } +} + +func (o *GoogleTranslateOrator) Stop() { + o.cmdMu.Lock() + defer o.cmdMu.Unlock() + // Signal any running Speak to stop + if o.stopCh != nil { + select { + case <-o.stopCh: // already closed + default: + close(o.stopCh) + } + o.stopCh = nil + } + // Kill the external player process if it's still running + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + o.cmd.Wait() // clean up zombie process + o.cmd = nil + } + // Also reset text buffer and interrupt flag (with o.mu) + o.mu.Lock() + o.textBuffer.Reset() + o.interrupt = true + o.mu.Unlock() +} diff --git a/extra/kokoro.go b/extra/kokoro.go new file mode 100644 index 0000000..e3ca047 --- /dev/null +++ b/extra/kokoro.go @@ -0,0 +1,259 @@ +//go:build extra +// +build extra + +package extra + +import ( + "bytes" + "encoding/json" + "fmt" + "gf-lt/models" + "io" + "log/slog" + "net/http" + "os/exec" + "strings" + "sync" + + "github.com/neurosnap/sentences/english" +) + +type KokoroOrator struct { + logger *slog.Logger + mu sync.Mutex + URL string + Format models.AudioFormat + Stream bool + Speed float32 + Language string + Voice string + // fields for playback control + cmd *exec.Cmd + cmdMu sync.Mutex + stopCh chan struct{} + // textBuffer, interrupt etc. remain the same + textBuffer strings.Builder + interrupt bool +} + +func (o *KokoroOrator) GetLogger() *slog.Logger { + return o.logger +} + +func (o *KokoroOrator) Speak(text string) error { + o.logger.Debug("fn: Speak is called", "text-len", len(text)) + body, err := o.requestSound(text) + if err != nil { + return fmt.Errorf("request failed: %w", err) + } + defer body.Close() + cmd := exec.Command("ffplay", "-nodisp", "-autoexit", "-i", "pipe:0") + stdin, err := cmd.StdinPipe() + if err != nil { + return fmt.Errorf("failed to get stdin pipe: %w", err) + } + o.cmdMu.Lock() + o.cmd = cmd + o.stopCh = make(chan struct{}) + o.cmdMu.Unlock() + if err := cmd.Start(); err != nil { + return fmt.Errorf("failed to start ffplay: %w", err) + } + // Copy audio in background + copyErr := make(chan error, 1) + go func() { + _, err := io.Copy(stdin, body) + stdin.Close() + copyErr <- err + }() + // Wait for player in background + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + // Wait for BOTH copy and player, but ensure we block until done + select { + case <-o.stopCh: + // Stop requested: kill player and wait for it to exit + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + } + <-done // Wait for process to actually exit + return nil + case copyErrVal := <-copyErr: + if copyErrVal != nil { + // Copy failed: kill player and wait + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + } + <-done + return copyErrVal + } + // Copy succeeded, now wait for playback to complete + return <-done + case err := <-done: + // Playback finished normally (copy must have succeeded or player would have exited early) + return err + } +} +func (o *KokoroOrator) requestSound(text string) (io.ReadCloser, error) { + if o.URL == "" { + return nil, fmt.Errorf("TTS URL is empty") + } + payload := map[string]interface{}{ + "input": text, + "voice": o.Voice, + "response_format": o.Format, + "download_format": o.Format, + "stream": o.Stream, + "speed": o.Speed, + // "return_download_link": true, + "lang_code": o.Language, + } + payloadBytes, err := json.Marshal(payload) + if err != nil { + return nil, fmt.Errorf("failed to marshal payload: %w", err) + } + req, err := http.NewRequest("POST", o.URL, bytes.NewBuffer(payloadBytes)) //nolint:noctx + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + req.Header.Set("accept", "application/json") + req.Header.Set("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, fmt.Errorf("request failed: %w", err) + } + if resp.StatusCode != http.StatusOK { + defer resp.Body.Close() + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + return resp.Body, nil +} + +func (o *KokoroOrator) stoproutine() { + for { + <-TTSDoneChan + o.logger.Debug("orator got done signal") + // 1. Stop any ongoing playback (kills external player, closes stopCh) + o.Stop() + // 2. Drain any pending text chunks + for len(TTSTextChan) > 0 { + <-TTSTextChan + } + // 3. Reset internal state + o.mu.Lock() + o.textBuffer.Reset() + o.interrupt = true + o.mu.Unlock() + } +} + +func (o *KokoroOrator) Stop() { + o.cmdMu.Lock() + defer o.cmdMu.Unlock() + // Signal any running Speak to stop + if o.stopCh != nil { + select { + case <-o.stopCh: // already closed + default: + close(o.stopCh) + } + o.stopCh = nil + } + // Kill the external player process if it's still running + if o.cmd != nil && o.cmd.Process != nil { + o.cmd.Process.Kill() + o.cmd.Wait() // clean up zombie process + o.cmd = nil + } + // Also reset text buffer and interrupt flag (with o.mu) + o.mu.Lock() + o.textBuffer.Reset() + o.interrupt = true + o.mu.Unlock() +} + +func (o *KokoroOrator) readroutine() { + tokenizer, _ := english.NewSentenceTokenizer(nil) + for { + select { + case chunk := <-TTSTextChan: + o.mu.Lock() + o.interrupt = false + _, err := o.textBuffer.WriteString(chunk) + if err != nil { + o.logger.Warn("failed to write to stringbuilder", "error", err) + o.mu.Unlock() + continue + } + text := o.textBuffer.String() + sentences := tokenizer.Tokenize(text) + o.logger.Debug("adding chunk", "chunk", chunk, "text", text, "sen-len", len(sentences)) + if len(sentences) <= 1 { + o.mu.Unlock() + continue + } + completeSentences := sentences[:len(sentences)-1] + remaining := sentences[len(sentences)-1].Text + o.textBuffer.Reset() + o.textBuffer.WriteString(remaining) + o.mu.Unlock() + for _, sentence := range completeSentences { + o.mu.Lock() + interrupted := o.interrupt + o.mu.Unlock() + if interrupted { + return + } + cleanedText := models.CleanText(sentence.Text) + if cleanedText == "" { + continue + } + o.logger.Debug("calling Speak with sentence", "sent", cleanedText) + if err := o.Speak(cleanedText); err != nil { + o.logger.Error("tts failed", "sentence", cleanedText, "error", err) + } + } + case <-TTSFlushChan: + o.logger.Debug("got flushchan signal start") + // lln is done get the whole message out + if len(TTSTextChan) > 0 { // otherwise might get stuck + for chunk := range TTSTextChan { + o.mu.Lock() + _, err := o.textBuffer.WriteString(chunk) + o.mu.Unlock() + if err != nil { + o.logger.Warn("failed to write to stringbuilder", "error", err) + continue + } + if len(TTSTextChan) == 0 { + break + } + } + } + // flush remaining text + o.mu.Lock() + remaining := o.textBuffer.String() + remaining = models.CleanText(remaining) + o.textBuffer.Reset() + o.mu.Unlock() + if remaining == "" { + continue + } + o.logger.Debug("calling Speak with remainder", "rem", remaining) + sentencesRem := tokenizer.Tokenize(remaining) + for _, rs := range sentencesRem { // to avoid dumping large volume of text + o.mu.Lock() + interrupt := o.interrupt + o.mu.Unlock() + if interrupt { + break + } + if err := o.Speak(rs.Text); err != nil { + o.logger.Error("tts failed", "sentence", rs, "error", err) + } + } + } + } +} diff --git a/extra/stt.go b/extra/stt.go new file mode 100644 index 0000000..7bbf2fd --- /dev/null +++ b/extra/stt.go @@ -0,0 +1,70 @@ +//go:build extra +// +build extra + +package extra + +import ( + "bytes" + "encoding/binary" + "gf-lt/config" + "io" + "log/slog" + "regexp" +) + +var specialRE = regexp.MustCompile(`\[.*?\]`) + +type STT interface { + StartRecording() error + StopRecording() (string, error) + IsRecording() bool +} + +type StreamCloser interface { + Close() error +} + +func NewSTT(logger *slog.Logger, cfg *config.Config) STT { + switch cfg.STT_TYPE { + case "WHISPER_BINARY": + logger.Debug("stt init, chosen whisper binary") + return NewWhisperBinary(logger, cfg) + case "WHISPER_SERVER": + logger.Debug("stt init, chosen whisper server") + return NewWhisperServer(logger, cfg) + } + return NewWhisperServer(logger, cfg) +} + +func NewWhisperServer(logger *slog.Logger, cfg *config.Config) *WhisperServer { + return &WhisperServer{ + logger: logger, + ServerURL: cfg.STT_URL, + SampleRate: cfg.STT_SR, + AudioBuffer: new(bytes.Buffer), + } +} + +func (stt *WhisperServer) writeWavHeader(w io.Writer, dataSize int) { + header := make([]byte, 44) + copy(header[0:4], "RIFF") + binary.LittleEndian.PutUint32(header[4:8], uint32(36+dataSize)) + copy(header[8:12], "WAVE") + copy(header[12:16], "fmt ") + binary.LittleEndian.PutUint32(header[16:20], 16) + binary.LittleEndian.PutUint16(header[20:22], 1) + binary.LittleEndian.PutUint16(header[22:24], 1) + binary.LittleEndian.PutUint32(header[24:28], uint32(stt.SampleRate)) + binary.LittleEndian.PutUint32(header[28:32], uint32(stt.SampleRate)*1*(16/8)) + binary.LittleEndian.PutUint16(header[32:34], 1*(16/8)) + binary.LittleEndian.PutUint16(header[34:36], 16) + copy(header[36:40], "data") + binary.LittleEndian.PutUint32(header[40:44], uint32(dataSize)) + if _, err := w.Write(header); err != nil { + stt.logger.Error("writeWavHeader", "error", err) + } +} + +func (stt *WhisperServer) IsRecording() bool { + return stt.recording +} diff --git a/extra/tts.go b/extra/tts.go new file mode 100644 index 0000000..2ddb0ae --- /dev/null +++ b/extra/tts.go @@ -0,0 +1,69 @@ +//go:build extra +// +build extra + +package extra + +import ( + "gf-lt/config" + "gf-lt/models" + "log/slog" + "os" + "strings" + + google_translate_tts "github.com/GrailFinder/google-translate-tts" +) + +var ( + TTSTextChan = make(chan string, 10000) + TTSFlushChan = make(chan bool, 1) + TTSDoneChan = make(chan bool, 1) + // endsWithPunctuation = regexp.MustCompile(`[;.!?]$`) +) + +type Orator interface { + Speak(text string) error + Stop() + // pause and resume? + GetLogger() *slog.Logger +} + +func NewOrator(log *slog.Logger, cfg *config.Config) Orator { + provider := cfg.TTS_PROVIDER + if provider == "" { + provider = "google" // does not require local setup + } + switch strings.ToLower(provider) { + case "kokoro": // kokoro + orator := &KokoroOrator{ + logger: log, + URL: cfg.TTS_URL, + Format: models.AFMP3, + Stream: false, + Speed: cfg.TTS_SPEED, + Language: "a", + Voice: "af_bella(1)+af_sky(1)", + } + go orator.readroutine() + go orator.stoproutine() + return orator + default: + language := cfg.TTS_LANGUAGE + if language == "" { + language = "en" + } + speech := &google_translate_tts.Speech{ + Folder: os.TempDir() + "/gf-lt-tts", // Temporary directory for caching + Language: language, + Proxy: "", // Proxy not supported + Speed: cfg.TTS_SPEED, + } + orator := &GoogleTranslateOrator{ + logger: log, + speech: speech, + Speed: cfg.TTS_SPEED, + } + go orator.readroutine() + go orator.stoproutine() + return orator + } +} diff --git a/extra/tts_test.go b/extra/tts_test.go new file mode 100644 index 0000000..a21d9b8 --- /dev/null +++ b/extra/tts_test.go @@ -0,0 +1,40 @@ +//go:build extra +// +build extra + +package extra + +import ( + "testing" +) + +func TestCleanText(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"Hello world", "Hello world"}, + {"**Bold text**", "Bold text"}, + {"*Italic text*", "Italic text"}, + {"# Header", "Header"}, + {"_Underlined text_", "Underlined text"}, + {"~Strikethrough text~", "Strikethrough text"}, + {"`Code text`", "Code text"}, + {"[Link text](url)", "Link text(url)"}, + {"Mixed *markdown* and #headers#!", "Mixed markdown and headers"}, + {"<html>tags</html>", "tags"}, + {"|---|", ""}, // Table separator + {"|====|", ""}, // Table separator with equals + {"| - - - |", ""}, // Table separator with spaced dashes + {"| cell1 | cell2 |", "cell1 cell2"}, // Table row with content + {" Trailing spaces ", "Trailing spaces"}, + {"", ""}, + {"***", ""}, + } + + for _, test := range tests { + result := cleanText(test.input) + if result != test.expected { + t.Errorf("cleanText(%q) = %q; expected %q", test.input, result, test.expected) + } + } +}
\ No newline at end of file diff --git a/extra/whisper_binary.go b/extra/whisper_binary.go new file mode 100644 index 0000000..1c35952 --- /dev/null +++ b/extra/whisper_binary.go @@ -0,0 +1,176 @@ +//go:build extra +// +build extra + +package extra + +import ( + "bytes" + "context" + "errors" + "fmt" + "gf-lt/config" + "log/slog" + "os" + "os/exec" + "strings" + "sync" + "syscall" + "time" +) + +type WhisperBinary struct { + logger *slog.Logger + whisperPath string + modelPath string + lang string + // Per-recording fields (protected by mu) + mu sync.Mutex + recording bool + tempFile string + ctx context.Context + cancel context.CancelFunc + cmd *exec.Cmd + cmdMu sync.Mutex +} + +func (w *WhisperBinary) StartRecording() error { + w.mu.Lock() + defer w.mu.Unlock() + if w.recording { + return errors.New("recording is already in progress") + } + // Fresh context for this recording + ctx, cancel := context.WithCancel(context.Background()) + w.ctx = ctx + w.cancel = cancel + // Create temporary file + tempFile, err := os.CreateTemp("", "recording_*.wav") + if err != nil { + cancel() + return fmt.Errorf("failed to create temp file: %w", err) + } + tempFile.Close() + w.tempFile = tempFile.Name() + // ffmpeg command: capture from default microphone, write WAV + args := []string{ + "-f", "alsa", // or "pulse" if preferred + "-i", "default", + "-acodec", "pcm_s16le", + "-ar", "16000", + "-ac", "1", + "-y", // overwrite output file + w.tempFile, + } + cmd := exec.CommandContext(w.ctx, "ffmpeg", args...) + // Capture stderr for debugging (optional, but useful for diagnosing) + stderr, err := cmd.StderrPipe() + if err != nil { + cancel() + os.Remove(w.tempFile) + return fmt.Errorf("failed to create stderr pipe: %w", err) + } + go func() { + buf := make([]byte, 1024) + for { + n, err := stderr.Read(buf) + if n > 0 { + w.logger.Debug("ffmpeg stderr", "output", string(buf[:n])) + } + if err != nil { + break + } + } + }() + w.cmdMu.Lock() + w.cmd = cmd + w.cmdMu.Unlock() + if err := cmd.Start(); err != nil { + cancel() + os.Remove(w.tempFile) + return fmt.Errorf("failed to start ffmpeg: %w", err) + } + w.recording = true + w.logger.Debug("Recording started", "file", w.tempFile) + return nil +} + +func (w *WhisperBinary) StopRecording() (string, error) { + w.mu.Lock() + defer w.mu.Unlock() + if !w.recording { + return "", errors.New("not currently recording") + } + w.recording = false + // Gracefully stop ffmpeg + w.cmdMu.Lock() + if w.cmd != nil && w.cmd.Process != nil { + w.logger.Debug("Sending SIGTERM to ffmpeg") + w.cmd.Process.Signal(syscall.SIGTERM) + // Wait for process to exit (up to 2 seconds) + done := make(chan error, 1) + go func() { + done <- w.cmd.Wait() + }() + select { + case <-done: + w.logger.Debug("ffmpeg exited after SIGTERM") + case <-time.After(2 * time.Second): + w.logger.Warn("ffmpeg did not exit, sending SIGKILL") + w.cmd.Process.Kill() + <-done + } + } + w.cmdMu.Unlock() + // Cancel context (already done, but for cleanliness) + if w.cancel != nil { + w.cancel() + } + // Validate temp file + if w.tempFile == "" { + return "", errors.New("no recording file") + } + defer os.Remove(w.tempFile) + info, err := os.Stat(w.tempFile) + if err != nil { + return "", fmt.Errorf("failed to stat temp file: %w", err) + } + if info.Size() < 44 { // WAV header is 44 bytes + // Log ffmpeg stderr? Already captured in debug logs. + return "", fmt.Errorf("recording file too small (%d bytes), possibly no audio captured", info.Size()) + } + // Run whisper.cpp binary + cmd := exec.Command(w.whisperPath, "-m", w.modelPath, "-l", w.lang, w.tempFile) + var outBuf, errBuf bytes.Buffer + cmd.Stdout = &outBuf + cmd.Stderr = &errBuf + if err := cmd.Run(); err != nil { + w.logger.Error("whisper binary failed", + "error", err, + "stderr", errBuf.String(), + "file_size", info.Size()) + return "", fmt.Errorf("whisper binary failed: %w (stderr: %s)", err, errBuf.String()) + } + result := strings.TrimRight(outBuf.String(), "\n") + result = specialRE.ReplaceAllString(result, "") + return strings.TrimSpace(strings.ReplaceAll(result, "\n ", "\n")), nil +} + +// IsRecording returns true if a recording is in progress. +func (w *WhisperBinary) IsRecording() bool { + w.mu.Lock() + defer w.mu.Unlock() + return w.recording +} + +func NewWhisperBinary(logger *slog.Logger, cfg *config.Config) *WhisperBinary { + ctx, cancel := context.WithCancel(context.Background()) + // Set ALSA error handler first + return &WhisperBinary{ + logger: logger, + whisperPath: cfg.WhisperBinaryPath, + modelPath: cfg.WhisperModelPath, + lang: cfg.STT_LANG, + ctx: ctx, + cancel: cancel, + } +} diff --git a/extra/whisper_server.go b/extra/whisper_server.go new file mode 100644 index 0000000..7532f4a --- /dev/null +++ b/extra/whisper_server.go @@ -0,0 +1,156 @@ +//go:build extra +// +build extra + +package extra + +import ( + "bytes" + "errors" + "fmt" + "io" + "log/slog" + "mime/multipart" + "net/http" + "os/exec" + "strings" + "sync" +) + +type WhisperServer struct { + logger *slog.Logger + ServerURL string + SampleRate int + AudioBuffer *bytes.Buffer + recording bool // protected by mu + mu sync.Mutex // protects recording & AudioBuffer + cmd *exec.Cmd // protected by cmdMu + stopCh chan struct{} // protected by cmdMu + cmdMu sync.Mutex // protects cmd and stopCh +} + +func (stt *WhisperServer) StartRecording() error { + stt.mu.Lock() + defer stt.mu.Unlock() + if stt.recording { + return nil + } + // Build ffmpeg command for microphone capture + args := []string{ + "-f", "alsa", + "-i", "default", + "-acodec", "pcm_s16le", + "-ar", fmt.Sprint(stt.SampleRate), + "-ac", "1", + "-f", "s16le", + "-", + } + cmd := exec.Command("ffmpeg", args...) + stdout, err := cmd.StdoutPipe() + if err != nil { + return fmt.Errorf("failed to get stdout pipe: %w", err) + } + stt.cmdMu.Lock() + stt.cmd = cmd + stt.stopCh = make(chan struct{}) + stt.cmdMu.Unlock() + if err := cmd.Start(); err != nil { + return fmt.Errorf("failed to start ffmpeg: %w", err) + } + stt.recording = true + stt.AudioBuffer.Reset() + // Read PCM data in goroutine + go func() { + buf := make([]byte, 4096) + for { + select { + case <-stt.stopCh: + return + default: + n, err := stdout.Read(buf) + if n > 0 { + stt.mu.Lock() + stt.AudioBuffer.Write(buf[:n]) + stt.mu.Unlock() + } + if err != nil { + if err != io.EOF { + stt.logger.Error("recording read error", "error", err) + } + return + } + } + } + }() + return nil +} + +func (stt *WhisperServer) StopRecording() (string, error) { + stt.mu.Lock() + defer stt.mu.Unlock() + if !stt.recording { + return "", errors.New("not recording") + } + stt.recording = false + // Stop ffmpeg + stt.cmdMu.Lock() + if stt.cmd != nil && stt.cmd.Process != nil { + stt.cmd.Process.Kill() + stt.cmd.Wait() + } + close(stt.stopCh) + stt.cmdMu.Unlock() + // Rest of StopRecording unchanged (WAV header + HTTP upload) + // ... + stt.recording = false + // wait loop to finish? + if stt.AudioBuffer == nil { + err := errors.New("unexpected nil AudioBuffer") + stt.logger.Error(err.Error()) + return "", err + } + // Create WAV header first + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + // Add audio file part + part, err := writer.CreateFormFile("file", "recording.wav") + if err != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + // Stream directly to multipart writer: header + raw data + dataSize := stt.AudioBuffer.Len() + stt.writeWavHeader(part, dataSize) + if _, err := io.Copy(part, stt.AudioBuffer); err != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + // Reset buffer for next recording + stt.AudioBuffer.Reset() + // Add response format field + err = writer.WriteField("response_format", "text") + if err != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + if writer.Close() != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + // Send request + resp, err := http.Post(stt.ServerURL, writer.FormDataContentType(), body) //nolint:noctx + if err != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + defer resp.Body.Close() + // Read and print response + responseTextBytes, err := io.ReadAll(resp.Body) + if err != nil { + stt.logger.Error("fn: StopRecording", "error", err) + return "", err + } + resptext := strings.TrimRight(string(responseTextBytes), "\n") + // in case there are special tokens like [_BEG_] + resptext = specialRE.ReplaceAllString(resptext, "") + return strings.TrimSpace(strings.ReplaceAll(resptext, "\n ", "\n")), nil +} @@ -1,28 +1,50 @@ -module elefant +module gf-lt -go 1.23.2 +go 1.25.1 require ( - github.com/gdamore/tcell/v2 v2.7.4 + github.com/BurntSushi/toml v1.5.0 + github.com/GrailFinder/google-translate-tts v0.1.4 + github.com/GrailFinder/searchagent v0.2.0 + github.com/PuerkitoBio/goquery v1.11.0 + github.com/gdamore/tcell/v2 v2.13.2 github.com/glebarez/go-sqlite v1.22.0 github.com/jmoiron/sqlx v1.4.0 - github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592 + github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728 + github.com/neurosnap/sentences v1.1.2 + github.com/playwright-community/playwright-go v0.5700.1 + github.com/rivo/tview v0.42.0 + github.com/sugarme/tokenizer v0.3.0 + github.com/yalue/onnxruntime_go v1.27.0 + github.com/yuin/goldmark v1.4.13 ) require ( + github.com/andybalholm/cascadia v1.3.3 // indirect + github.com/deckarep/golang-set/v2 v2.8.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/gdamore/encoding v1.0.0 // indirect - github.com/google/uuid v1.5.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/emirpasic/gods v1.18.1 // indirect + github.com/gdamore/encoding v1.0.1 // indirect + github.com/go-jose/go-jose/v3 v3.0.4 // indirect + github.com/go-stack/stack v1.8.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/hajimehoshi/go-mp3 v0.3.4 // indirect + github.com/lucasb-eyer/go-colorful v1.3.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect + github.com/ncruces/go-strftime v1.0.0 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rivo/uniseg v0.4.7 // indirect - golang.org/x/sys v0.17.0 // indirect - golang.org/x/term v0.17.0 // indirect - golang.org/x/text v0.14.0 // indirect - modernc.org/libc v1.37.6 // indirect - modernc.org/mathutil v1.6.0 // indirect - modernc.org/memory v1.7.2 // indirect - modernc.org/sqlite v1.28.0 // indirect + github.com/schollz/progressbar/v2 v2.15.0 // indirect + github.com/sugarme/regexpset v0.0.0-20200920021344-4d4ec8eaf93c // indirect + golang.org/x/exp v0.0.0-20251209150349-8475f28825e9 // indirect + golang.org/x/net v0.48.0 // indirect + golang.org/x/sys v0.39.0 // indirect + golang.org/x/term v0.38.0 // indirect + golang.org/x/text v0.32.0 // indirect + modernc.org/libc v1.67.1 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.11.0 // indirect + modernc.org/sqlite v1.40.1 // indirect ) @@ -1,81 +1,202 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= +github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/GrailFinder/google-translate-tts v0.1.4 h1:NJoPZUGfBrmouQMN19MUcNPNUx4tmf4a8OZRME4E4Mg= +github.com/GrailFinder/google-translate-tts v0.1.4/go.mod h1:YIOLKR7sObazdUCrSex3u9OVBovU55eYgWa25vsQJ18= +github.com/GrailFinder/searchagent v0.2.0 h1:U2GVjLh/9xZt0xX9OcYk9Q2fMkyzyTiADPUmUisRdtQ= +github.com/GrailFinder/searchagent v0.2.0/go.mod h1:d66tn5+22LI8IGJREUsRBT60P0sFdgQgvQRqyvgItrs= +github.com/PuerkitoBio/goquery v1.11.0 h1:jZ7pwMQXIITcUXNH83LLk+txlaEy6NVOfTuP43xxfqw= +github.com/PuerkitoBio/goquery v1.11.0/go.mod h1:wQHgxUOU3JGuj3oD/QFfxUdlzW6xPHfqyHre6VMY4DQ= +github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM= +github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deckarep/golang-set/v2 v2.8.0 h1:swm0rlPCmdWn9mESxKOjWk8hXSqoxOp+ZlfuyaAdFlQ= +github.com/deckarep/golang-set/v2 v2.8.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= -github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= -github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= -github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= +github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= +github.com/gdamore/tcell/v2 v2.13.2 h1:5j4srfF8ow3HICOv/61/sOhQtA25qxEB2XR3Q/Bhx2g= +github.com/gdamore/tcell/v2 v2.13.2/go.mod h1:+Wfe208WDdB7INEtCsNrAN6O2m+wsTPk1RAovjaILlo= github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ= github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= +github.com/go-jose/go-jose/v3 v3.0.4 h1:Wp5HA7bLQcKnf6YYao/4kpRpVMp/yf6+pJKV8WFSaNY= +github.com/go-jose/go-jose/v3 v3.0.4/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= -github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= -github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= +github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= +github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728 h1:QwWKgMY28TAXaDl+ExRDqGQltzXqN/xypdKP86niVn8= +github.com/ledongthuc/pdf v0.0.0-20250511090121-5959a4027728/go.mod h1:1fEHWurg7pvf5SG6XNE5Q8UZmOwex51Mkx3SLhrW5B4= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= +github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= +github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/neurosnap/sentences v1.1.2 h1:iphYOzx/XckXeBiLIUBkPu2EKMJ+6jDbz/sLJZ7ZoUw= +github.com/neurosnap/sentences v1.1.2/go.mod h1:/pwU4E9XNL21ygMIkOIllv/SMy2ujHwpf8GQPu1YPbQ= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= +github.com/playwright-community/playwright-go v0.5700.1 h1:PNFb1byWqrTT720rEO0JL88C6Ju0EmUnR5deFLvtP/U= +github.com/playwright-community/playwright-go v0.5700.1/go.mod h1:MlSn1dZrx8rszbCxY6x3qK89ZesJUYVx21B2JnkoNF0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592 h1:YIJ+B1hePP6AgynC5TcqpO0H9k3SSoZa2BGyL6vDUzM= -github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/tview v0.42.0 h1:b/ftp+RxtDsHSaynXTbJb+/n/BxDEi+W3UfF5jILK6c= +github.com/rivo/tview v0.42.0/go.mod h1:cSfIYfhpSGCjp3r/ECJb+GKS7cGJnqV8vfjQPwoXyfY= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/schollz/progressbar/v2 v2.15.0 h1:dVzHQ8fHRmtPjD3K10jT3Qgn/+H+92jhPrhmxIJfDz8= +github.com/schollz/progressbar/v2 v2.15.0/go.mod h1:UdPq3prGkfQ7MOzZKlDRpYKcFqEMczbD7YmbPgpzKMI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/sugarme/regexpset v0.0.0-20200920021344-4d4ec8eaf93c h1:pwb4kNSHb4K89ymCaN+5lPH/MwnfSVg4rzGDh4d+iy4= +github.com/sugarme/regexpset v0.0.0-20200920021344-4d4ec8eaf93c/go.mod h1:2gwkXLWbDGUQWeL3RtpCmcY4mzCtU13kb9UsAg9xMaw= +github.com/sugarme/tokenizer v0.3.0 h1:FE8DYbNSz/kSbgEo9l/RjgYHkIJYEdskumitFQBE9FE= +github.com/sugarme/tokenizer v0.3.0/go.mod h1:VJ+DLK5ZEZwzvODOWwY0cw+B1dabTd3nCB5HuFCItCc= +github.com/yalue/onnxruntime_go v1.27.0 h1:c1YSgDNtpf0WGtxj3YeRIb8VC5LmM1J+Ve3uHdteC1U= +github.com/yalue/onnxruntime_go v1.27.0/go.mod h1:b4X26A8pekNb1ACJ58wAXgNKeUCGEAQ9dmACut9Sm/4= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/exp v0.0.0-20251209150349-8475f28825e9 h1:MDfG8Cvcqlt9XXrmEiD4epKn7VJHZO84hejP9Jmp0MM= +golang.org/x/exp v0.0.0-20251209150349-8475f28825e9/go.mod h1:EPRbTFwzwjXj9NpYyyrvenVh9Y+GFeEvMNh7Xuz7xgU= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= +golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= +golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= +golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= +golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= +golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q= +golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU= +golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= +golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw= -modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE= -modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= -modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= -modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= -modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= -modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ= -modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis= +modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= +modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc= +modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM= +modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA= +modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE= +modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.67.1 h1:bFaqOaa5/zbWYJo8aW0tXPX21hXsngG2M7mckCnFSVk= +modernc.org/libc v1.67.1/go.mod h1:QvvnnJ5P7aitu0ReNpVIEyesuhmDLQ8kaEoyMjIFZJA= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= +modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8= +modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.40.1 h1:VfuXcxcUWWKRBuP8+BR9L7VnmusMgBNNnBYGEe9w/iY= +modernc.org/sqlite v1.40.1/go.mod h1:9fjQZ0mB1LLP0GYrp39oOJXx/I2sxEnZtzCmEQIKvGE= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/helpfuncs.go b/helpfuncs.go new file mode 100644 index 0000000..e28beda --- /dev/null +++ b/helpfuncs.go @@ -0,0 +1,1017 @@ +package main + +import ( + "fmt" + "gf-lt/models" + "gf-lt/pngmeta" + "image" + "os" + "os/exec" + "path" + "path/filepath" + "slices" + "strconv" + "strings" + "sync/atomic" + "time" + "unicode" + + "github.com/rivo/tview" +) + +// Cached model color - updated by background goroutine +// var cachedModelColor string = "orange" +var cachedModelColor atomic.Value + +// startModelColorUpdater starts a background goroutine that periodically updates +// the cached model color. Only runs HTTP requests for local llama.cpp APIs. +func startModelColorUpdater() { + go func() { + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + // Initial check + updateCachedModelColor() + for range ticker.C { + updateCachedModelColor() + } + }() +} + +// updateCachedModelColor updates the global cachedModelColor variable +func updateCachedModelColor() { + if !isLocalLlamacpp() { + cachedModelColor.Store("orange") + return + } + // Check if model is loaded + loaded, err := isModelLoaded(chatBody.Model) + if err != nil { + // On error, assume not loaded (red) + cachedModelColor.Store("red") + return + } + if loaded { + cachedModelColor.Store("green") + } else { + cachedModelColor.Store("red") + } +} + +func isASCII(s string) bool { + for i := 0; i < len(s); i++ { + if s[i] > unicode.MaxASCII { + return false + } + } + 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. +// Skips user, tool, and system messages as they may contain thinking examples. +func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg { + if !cfg.StripThinkingFromAPI { + return msg + } + // Skip user, tool, they might contain thinking and system messages - examples + if msg.Role == cfg.UserRole || msg.Role == cfg.ToolRole || msg.Role == "system" { + return msg + } + // Strip thinking from assistant messages + msgText := msg.GetText() + if thinkRE.MatchString(msgText) { + cleanedText := thinkRE.ReplaceAllString(msgText, "") + cleanedText = strings.TrimSpace(cleanedText) + msg.SetText(cleanedText) + } + return msg +} + +// refreshChatDisplay updates the chat display based on current character view +// It filters messages for the character the user is currently "writing as" +// and updates the textView with the filtered conversation +func refreshChatDisplay() { + // Determine which character's view to show + viewingAs := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + viewingAs = cfg.WriteNextMsgAs + } + // Filter messages for this character + filteredMessages := filterMessagesForCharacter(chatBody.Messages, viewingAs) + displayText := chatToText(filteredMessages, cfg.ShowSys) + textView.SetText(displayText) + colorText() + updateStatusLine() + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } +} + +// stopTTSIfNotForUser: character specific context, not meant fot the human to hear +func stopTTSIfNotForUser(msg *models.RoleMsg) { + if strings.Contains(cfg.CurrentAPI, "/chat") || !cfg.CharSpecificContextEnabled { + return + } + viewingAs := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + viewingAs = cfg.WriteNextMsgAs + } + // stop tts if msg is not for user + if !slices.Contains(msg.KnownTo, viewingAs) && cfg.TTS_ENABLED { + TTSDoneChan <- true + } +} + +func colorText() { + text := textView.GetText(false) + quoteReplacer := strings.NewReplacer( + `”`, `"`, + `“`, `"`, + `“`, `"`, + `”`, `"`, + `**`, `*`, + ) + text = quoteReplacer.Replace(text) + // Step 1: Extract code blocks and replace them with unique placeholders + var codeBlocks []string + placeholder := "__CODE_BLOCK_%d__" + counter := 0 + // thinking + var thinkBlocks []string + placeholderThink := "__THINK_BLOCK_%d__" + counterThink := 0 + // Replace code blocks with placeholders and store their styled versions + text = codeBlockRE.ReplaceAllStringFunc(text, func(match string) string { + // Style the code block and store it + styled := fmt.Sprintf("[red::i]%s[-:-:-]", match) + codeBlocks = append(codeBlocks, styled) + // Generate a unique placeholder (e.g., "__CODE_BLOCK_0__") + id := fmt.Sprintf(placeholder, counter) + counter++ + return id + }) + text = thinkRE.ReplaceAllStringFunc(text, func(match string) string { + // Style the code block and store it + styled := fmt.Sprintf("[red::i]%s[-:-:-]", match) + thinkBlocks = append(thinkBlocks, styled) + // Generate a unique placeholder (e.g., "__CODE_BLOCK_0__") + id := fmt.Sprintf(placeholderThink, counterThink) + counterThink++ + return id + }) + // Step 2: Apply other regex styles to the non-code parts + text = quotesRE.ReplaceAllString(text, `[orange::-]$1[-:-:-]`) + text = starRE.ReplaceAllString(text, `[turquoise::i]$1[-:-:-]`) + text = singleBacktickRE.ReplaceAllString(text, "`[pink::i]$1[-:-:-]`") + // text = thinkRE.ReplaceAllString(text, `[yellow::i]$1[-:-:-]`) + // Step 3: Restore the styled code blocks from placeholders + for i, cb := range codeBlocks { + text = strings.Replace(text, fmt.Sprintf(placeholder, i), cb, 1) + } + for i, tb := range thinkBlocks { + text = strings.Replace(text, fmt.Sprintf(placeholderThink, i), tb, 1) + } + textView.SetText(text) +} + +func updateStatusLine() { + status := makeStatusLine() + statusLineWidget.SetText(status) +} + +func initSysCards() ([]string, error) { + labels := []string{} + labels = append(labels, sysLabels...) + cards, err := pngmeta.ReadDirCards(cfg.SysDir, cfg.UserRole, logger) + if err != nil { + logger.Error("failed to read sys dir", "error", err) + return nil, err + } + for _, cc := range cards { + if cc.Role == "" { + logger.Warn("empty role", "file", cc.FilePath) + continue + } + if cc.ID == "" { + cc.ID = models.ComputeCardID(cc.Role, cc.FilePath) + } + sysMap[cc.ID] = cc + roleToID[cc.Role] = cc.ID + labels = append(labels, cc.Role) + } + return labels, nil +} + +func startNewChat(keepSysP bool) { + id, err := store.ChatGetMaxID() + if err != nil { + logger.Error("failed to get chat id", "error", err) + } + if ok := charToStart(cfg.AssistantRole, keepSysP); !ok { + logger.Warn("no such sys msg", "name", cfg.AssistantRole) + } + // set chat body + chatBody.Messages = chatBody.Messages[:2] + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + newChat := &models.Chat{ + ID: id + 1, + 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) + // actual chat history (messages) would be parsed then + Msgs: "", + Agent: cfg.AssistantRole, + } + activeChatName = newChat.Name + chatMap[newChat.Name] = newChat + updateStatusLine() + colorText() +} + +func renameUser(oldname, newname string) { + if oldname == "" { + // not provided; deduce who user is + // INFO: if user not yet spoke, it is hard to replace mentions in sysprompt and first message about thme + roles := chatBody.ListRoles() + for _, role := range roles { + if role == cfg.AssistantRole { + continue + } + if role == "tool" { + continue + } + if role == "system" { + continue + } + oldname = role + break + } + if oldname == "" { + // still + logger.Warn("fn: renameUser; failed to find old name", "newname", newname) + return + } + } + viewText := textView.GetText(false) + viewText = strings.ReplaceAll(viewText, oldname, newname) + chatBody.Rename(oldname, newname) + textView.SetText(viewText) +} + +func setLogLevel(sl string) { + switch sl { + case "Debug": + logLevel.Set(-4) + case "Info": + logLevel.Set(0) + case "Warn": + logLevel.Set(4) + } +} + +func listRolesWithUser() []string { + roles := listChatRoles() + // Remove user role if it exists in the list (to avoid duplicates and ensure it's at position 0) + filteredRoles := make([]string, 0, len(roles)) + for _, role := range roles { + if role != cfg.UserRole { + filteredRoles = append(filteredRoles, role) + } + } + // Prepend user role to the beginning of the list + result := append([]string{cfg.UserRole}, filteredRoles...) + slices.Sort(result) + return result +} + +func loadImage() error { + filepath := defaultImage + cc := GetCardByRole(cfg.AssistantRole) + if cc != nil { + if strings.HasSuffix(cc.FilePath, ".png") { + filepath = cc.FilePath + } + } + file, err := os.Open(filepath) + if err != nil { + return fmt.Errorf("failed to open image: %w", err) + } + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + return fmt.Errorf("failed to decode image: %w", err) + } + imgView.SetImage(img) + return nil +} + +func strInSlice(s string, sl []string) bool { + for _, el := range sl { + if strings.EqualFold(s, el) { + return true + } + } + return false +} + +// isLocalLlamacpp checks if the current API is a local llama.cpp instance. +func isLocalLlamacpp() bool { + if strings.Contains(cfg.CurrentAPI, "openrouter") || strings.Contains(cfg.CurrentAPI, "deepseek") { + return false + } + return true +} + +// getModelColor returns the cached color tag for the model name. +// The cached value is updated by a background goroutine every 5 seconds. +// For non-local models, returns orange. For local llama.cpp models, returns green if loaded, red if not. +func getModelColor() string { + return cachedModelColor.Load().(string) +} + +func makeStatusLine() string { + isRecording := false + if asr != nil { + isRecording = asr.IsRecording() + } + persona := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + persona = cfg.WriteNextMsgAs + } + botPersona := cfg.AssistantRole + if cfg.WriteNextMsgAsCompletionAgent != "" { + botPersona = cfg.WriteNextMsgAsCompletionAgent + } + // Add image attachment info to status line + var imageInfo string + if imageAttachmentPath != "" { + // Get just the filename from the path + imageName := path.Base(imageAttachmentPath) + imageInfo = fmt.Sprintf(" | attached img: [orange:-:b]%s[-:-:-]", imageName) + } else { + imageInfo = "" + } + // Add shell mode status to status line + var shellModeInfo string + if shellMode { + shellModeInfo = " | [green:-:b]SHELL MODE[-:-:-]" + } else { + shellModeInfo = "" + } + // Get model color based on load status for local llama.cpp models + modelColor := getModelColor() + statusLine := fmt.Sprintf(statusLineTempl, activeChatName, + boolColors[cfg.ToolUse], modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp], + cfg.CurrentAPI, persona, botPersona) + if cfg.STT_ENABLED { + recordingS := fmt.Sprintf(" | [%s:-:b]voice recording[-:-:-] (ctrl+r)", + boolColors[isRecording]) + statusLine += recordingS + } + // completion endpoint + if !strings.Contains(cfg.CurrentAPI, "chat") { + roleInject := fmt.Sprintf(" | [%s:-:b]role injection[-:-:-] (alt+7)", boolColors[injectRole]) + statusLine += roleInject + } + // context tokens + contextTokens := getContextTokens() + maxCtx := getMaxContextTokens() + if maxCtx == 0 { + maxCtx = 16384 + } + if contextTokens > 0 { + contextInfo := fmt.Sprintf(" | context-estim: [orange:-:b]%d/%d[-:-:-]", contextTokens, maxCtx) + statusLine += contextInfo + } + return statusLine + imageInfo + shellModeInfo +} + +func getContextTokens() int { + if chatBody == nil || chatBody.Messages == nil { + return 0 + } + total := 0 + messages := chatBody.Messages + for i := range messages { + msg := &messages[i] + if msg.Stats != nil && msg.Stats.Tokens > 0 { + total += msg.Stats.Tokens + } else if msg.GetText() != "" { + total += len(msg.GetText()) / 4 + } + } + return total +} + +const deepseekContext = 128000 + +func getMaxContextTokens() int { + if chatBody == nil || chatBody.Model == "" { + return 0 + } + modelName := chatBody.Model + switch { + case strings.Contains(cfg.CurrentAPI, "openrouter"): + if orModelsData != nil { + for i := range orModelsData.Data { + m := &orModelsData.Data[i] + if m.ID == modelName { + return m.ContextLength + } + } + } + case strings.Contains(cfg.CurrentAPI, "deepseek"): + return deepseekContext + default: + if localModelsData != nil { + for i := range localModelsData.Data { + m := &localModelsData.Data[i] + if m.ID == modelName { + for _, arg := range m.Status.Args { + if strings.HasPrefix(arg, "--ctx-size") { + if strings.Contains(arg, "=") { + val := strings.Split(arg, "=")[1] + if n, err := strconv.Atoi(val); err == nil { + return n + } + } else { + idx := -1 + for j, a := range m.Status.Args { + if a == "--ctx-size" && j+1 < len(m.Status.Args) { + idx = j + 1 + break + } + } + if idx != -1 { + if n, err := strconv.Atoi(m.Status.Args[idx]); err == nil { + return n + } + } + } + } + } + } + } + } + } + return 0 +} + +// set of roles within card definition and mention in chat history +func listChatRoles() []string { + currentChat, ok := chatMap[activeChatName] + cbc := chatBody.ListRoles() + if !ok { + return cbc + } + currentCard := GetCardByRole(currentChat.Agent) + if currentCard == nil { + logger.Warn("failed to find current card", "agent", currentChat.Agent) + return cbc + } + charset := []string{} + for _, name := range currentCard.Characters { + if !strInSlice(name, cbc) { + charset = append(charset, name) + } + } + charset = append(charset, cbc...) + return charset +} + +func deepseekModelValidator() error { + if cfg.CurrentAPI == cfg.DeepSeekChatAPI || cfg.CurrentAPI == cfg.DeepSeekCompletionAPI { + if chatBody.Model != "deepseek-chat" && chatBody.Model != "deepseek-reasoner" { + showToast("bad request", "wrong deepseek model name") + return nil + } + } + return nil +} + +// == shellmode == + +func toggleShellMode() { + shellMode = !shellMode + setShellMode(shellMode) + if shellMode { + shellInput.SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir)) + } else { + textArea.SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message.") + } + updateStatusLine() +} + +func updateFlexLayout() { + if fullscreenMode { + // flex already contains only focused widget; do nothing + return + } + flex.Clear() + flex.AddItem(textView, 0, 40, false) + if shellMode { + flex.AddItem(shellInput, 0, 10, false) + } else { + flex.AddItem(bottomFlex, 0, 10, true) + } + if positionVisible { + flex.AddItem(statusLineWidget, 0, 2, false) + } + // Keep focus on currently focused widget + focused := app.GetFocus() + switch { + case focused == textView: + app.SetFocus(textView) + case shellMode: + app.SetFocus(shellInput) + default: + app.SetFocus(textArea) + } +} + +func executeCommandAndDisplay(cmdText string) { + cmdText = strings.TrimSpace(cmdText) + if cmdText == "" { + fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n") + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + colorText() + return + } + workingDir := cfg.FilePickerDir + // Handle cd command specially to update working directory + if strings.HasPrefix(cmdText, "cd ") { + 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 cfg.AutoScrollEnabled { + 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 cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + colorText() + return + } + } + + // Use /bin/sh to support pipes, redirects, etc. + cmd := exec.Command("/bin/sh", "-c", cmdText) + cmd.Dir = workingDir + // Execute the command and get output + output, err := cmd.CombinedOutput() + // Add the command being executed to the chat + fmt.Fprintf(textView, "\n[-:-:b](%d) <%s>: [-:-:-]\n$ %s\n", + len(chatBody.Messages), cfg.ToolRole, cmdText) + var outputContent string + if err != nil { + // Include both output and error + errorMsg := "Error: " + err.Error() + fmt.Fprintf(textView, "[red]%s[-:-:-]\n", errorMsg) + if len(output) > 0 { + outputStr := string(output) + fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputStr) + outputContent = errorMsg + "\n" + outputStr + } else { + outputContent = errorMsg + } + } else { + // Only output if successful + if len(output) > 0 { + outputStr := string(output) + fmt.Fprintf(textView, "[green]%s[-:-:-]\n", outputStr) + outputContent = outputStr + } else { + successMsg := "Command executed successfully (no output)" + fmt.Fprintf(textView, "[green]%s[-:-:-]\n", successMsg) + outputContent = successMsg + } + } + // Combine command and output in a single message for chat history + combinedContent := "$ " + cmdText + "\n\n" + outputContent + combinedMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: combinedContent, + } + chatBody.Messages = append(chatBody.Messages, combinedMsg) + // Scroll to end and update colors + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + colorText() + // Add command to history (avoid duplicates at the end) + if len(shellHistory) == 0 || shellHistory[len(shellHistory)-1] != cmdText { + shellHistory = append(shellHistory, cmdText) + } + shellHistoryPos = -1 +} + +// == search == + +// Global variables for search state +var searchResults []int +var searchResultLengths []int // To store the length of each match in the formatted string +var searchIndex int +var searchText string +var originalTextForSearch string + +// performSearch searches for the given term in the textView content and highlights matches +func performSearch(term string) { + searchText = term + if searchText == "" { + searchResults = nil + searchResultLengths = nil + originalTextForSearch = "" + // Re-render text without highlights + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + return + } + // Get formatted text and search directly in it to avoid mapping issues + formattedText := textView.GetText(true) + originalTextForSearch = formattedText + searchTermLower := strings.ToLower(searchText) + formattedTextLower := strings.ToLower(formattedText) + // Find all occurrences of the search term in the formatted text directly + formattedSearchResults := []int{} + searchStart := 0 + for { + pos := strings.Index(formattedTextLower[searchStart:], searchTermLower) + if pos == -1 { + break + } + absolutePos := searchStart + pos + formattedSearchResults = append(formattedSearchResults, absolutePos) + searchStart = absolutePos + len(searchText) + } + if len(formattedSearchResults) == 0 { + // No matches found + searchResults = nil + searchResultLengths = nil + notification := "Pattern not found: " + term + showToast("search", notification) + return + } + // Store the formatted text positions and lengths for accurate highlighting + searchResults = formattedSearchResults + // Create lengths array - all matches have the same length as the search term + searchResultLengths = make([]int, len(formattedSearchResults)) + for i := range searchResultLengths { + searchResultLengths[i] = len(searchText) + } + searchIndex = 0 + highlightCurrentMatch() +} + +// highlightCurrentMatch highlights the current search match and scrolls to it +func highlightCurrentMatch() { + if len(searchResults) == 0 || searchIndex >= len(searchResults) { + return + } + // Get the stored formatted text + formattedText := originalTextForSearch + // For tview to properly support highlighting and scrolling, we need to work with its region system + // Instead of just applying highlights, we need to add region tags to the text + highlightedText := addRegionTags(formattedText, searchResults, searchResultLengths, searchIndex, searchText) + // Update the text view with the text that includes region tags + textView.SetText(highlightedText) + // Highlight the current region and scroll to it + // Need to identify which position in the results array corresponds to the current match + // The region ID will be search_<position>_<index> + currentRegion := fmt.Sprintf("search_%d_%d", searchResults[searchIndex], searchIndex) + textView.Highlight(currentRegion).ScrollToHighlight() + // Send notification about which match we're at + notification := fmt.Sprintf("Match %d of %d", searchIndex+1, len(searchResults)) + showToast("search", notification) +} + +// showSearchBar shows the search input field as an overlay +func showSearchBar() { + // Create a temporary flex to combine search and main content + updatedFlex := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(searchField, 3, 0, true). // Search field at top + AddItem(flex, 0, 1, false) // Main flex layout below + // Add the search overlay as a page + pages.AddPage(searchPageName, updatedFlex, true, true) + app.SetFocus(searchField) +} + +// hideSearchBar hides the search input field +func hideSearchBar() { + pages.RemovePage(searchPageName) + // Return focus to the text view + app.SetFocus(textView) + // Clear the search field + searchField.SetText("") +} + +// Global variables for index overlay functionality +var indexPageName = "indexOverlay" + +// showIndexBar shows the index input field as an overlay at the top +func showIndexBar() { + // Create a temporary flex to combine index input and main content + updatedFlex := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(indexPickWindow, 3, 0, true). // Index field at top + AddItem(flex, 0, 1, false) // Main flex layout below + + // Add the index overlay as a page + pages.AddPage(indexPageName, updatedFlex, true, true) + app.SetFocus(indexPickWindow) +} + +// hideIndexBar hides the index input field +func hideIndexBar() { + pages.RemovePage(indexPageName) + // Return focus to the text view + app.SetFocus(textView) + // Clear the index field + indexPickWindow.SetText("") +} + +// addRegionTags adds region tags to search matches in the text for tview highlighting +func addRegionTags(text string, positions []int, lengths []int, currentIdx int, searchTerm string) string { + if len(positions) == 0 { + return text + } + var result strings.Builder + lastEnd := 0 + for i, pos := range positions { + endPos := pos + lengths[i] + // Add text before this match + if pos > lastEnd { + result.WriteString(text[lastEnd:pos]) + } + // The matched text, which may contain its own formatting tags + actualText := text[pos:endPos] + // Add region tag and highlighting for this match + // Use a unique region id that includes the match index to avoid conflicts + regionId := fmt.Sprintf("search_%d_%d", pos, i) // position + index to ensure uniqueness + var highlightStart, highlightEnd string + if i == currentIdx { + // Current match - use different highlighting + highlightStart = fmt.Sprintf(`["%s"][yellow:blue:b]`, regionId) // Current match with region and special highlight + highlightEnd = `[-:-:-][""]` // Reset formatting and close region + } else { + // Other matches - use regular highlighting + highlightStart = fmt.Sprintf(`["%s"][gold:red:u]`, regionId) // Other matches with region and highlight + highlightEnd = `[-:-:-][""]` // Reset formatting and close region + } + result.WriteString(highlightStart) + result.WriteString(actualText) + result.WriteString(highlightEnd) + lastEnd = endPos + } + // Add the rest of the text after the last processed match + if lastEnd < len(text) { + result.WriteString(text[lastEnd:]) + } + return result.String() +} + +// searchNext finds the next occurrence of the search term +func searchNext() { + if len(searchResults) == 0 { + showToast("search", "No search results to navigate") + return + } + searchIndex = (searchIndex + 1) % len(searchResults) + highlightCurrentMatch() +} + +// searchPrev finds the previous occurrence of the search term +func searchPrev() { + if len(searchResults) == 0 { + showToast("search", "No search results to navigate") + return + } + if searchIndex == 0 { + searchIndex = len(searchResults) - 1 + } else { + searchIndex-- + } + highlightCurrentMatch() +} + +// == tab completion == + +func scanFiles(dir, filter string) []string { + const maxDepth = 3 + const maxFiles = 50 + var files []string + var scanRecursive func(currentDir string, currentDepth int, relPath string) + scanRecursive = func(currentDir string, currentDepth int, relPath string) { + if len(files) >= maxFiles { + return + } + if currentDepth > maxDepth { + return + } + entries, err := os.ReadDir(currentDir) + if err != nil { + return + } + for _, entry := range entries { + if len(files) >= maxFiles { + return + } + name := entry.Name() + if strings.HasPrefix(name, ".") { + continue + } + fullPath := name + if relPath != "" { + fullPath = relPath + "/" + name + } + if entry.IsDir() { + // Recursively scan subdirectories + scanRecursive(filepath.Join(currentDir, name), currentDepth+1, fullPath) + continue + } + // Check if file matches filter + if filter == "" || strings.HasPrefix(strings.ToLower(fullPath), strings.ToLower(filter)) { + files = append(files, fullPath) + } + } + } + scanRecursive(dir, 0, "") + 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 +} + +func getValidKnowToRecipient(msg *models.RoleMsg) (string, bool) { + if cfg == nil || !cfg.CharSpecificContextEnabled { + return "", false + } + // case where all roles are in the tag => public message + cr := listChatRoles() + slices.Sort(cr) + slices.Sort(msg.KnownTo) + if slices.Equal(cr, msg.KnownTo) { + logger.Info("got msg with tag mentioning every role") + return "", false + } + // Check each character in the KnownTo list + for _, recipient := range msg.KnownTo { + if recipient == msg.Role || recipient == cfg.ToolRole { + // weird cases, skip + continue + } + // Skip if this is the user character (user handles their own turn) + // If user is in KnownTo, stop processing - it's the user's turn + if recipient == cfg.UserRole || recipient == cfg.WriteNextMsgAs { + return "", false + } + return recipient, true + } + return "", false +} + +// triggerPrivateMessageResponses checks if a message was sent privately to specific characters +// and triggers those non-user characters to respond +func triggerPrivateMessageResponses(msg *models.RoleMsg) { + recipient, ok := getValidKnowToRecipient(msg) + if !ok || recipient == "" { + return + } + // Trigger the recipient character to respond + triggerMsg := recipient + ":\n" + // Send empty message so LLM continues naturally from the conversation + crr := &models.ChatRoundReq{ + UserMsg: triggerMsg, + Role: recipient, + Resume: true, + } + fmt.Fprintf(textView, "\n[-:-:b](%d) ", len(chatBody.Messages)) + fmt.Fprint(textView, roleToIcon(recipient)) + fmt.Fprint(textView, "[-:-:-]\n") + chatRoundChan <- crr +} @@ -0,0 +1,682 @@ +package main + +import ( + "bytes" + "encoding/json" + "gf-lt/models" + "io" + "strings" +) + +var imageAttachmentPath string // Global variable to track image attachment for next message +var lastImg string // for ctrl+j + +// containsToolSysMsg checks if the toolSysMsg already exists in the chat body +func containsToolSysMsg() bool { + for i := range chatBody.Messages { + if chatBody.Messages[i].Role == cfg.ToolRole && chatBody.Messages[i].Content == toolSysMsg { + return true + } + } + return false +} + +// SetImageAttachment sets an image to be attached to the next message sent to the LLM +func SetImageAttachment(imagePath string) { + imageAttachmentPath = imagePath + lastImg = imagePath +} + +// ClearImageAttachment clears any pending image attachment and updates UI +func ClearImageAttachment() { + imageAttachmentPath = "" +} + +// filterMessagesForCurrentCharacter filters messages based on char-specific context. +// Returns filtered messages and the bot persona role (target character). +func filterMessagesForCurrentCharacter(messages []models.RoleMsg) ([]models.RoleMsg, string) { + botPersona := cfg.AssistantRole + if cfg.WriteNextMsgAsCompletionAgent != "" { + botPersona = cfg.WriteNextMsgAsCompletionAgent + } + if cfg == nil || !cfg.CharSpecificContextEnabled { + return messages, botPersona + } + // get last message (written by user) and checck if it has a tag + lm := messages[len(messages)-1] + recipient, ok := getValidKnowToRecipient(&lm) + if ok && recipient != "" { + botPersona = recipient + } + filtered := filterMessagesForCharacter(messages, botPersona) + return filtered, botPersona +} + +type ChunkParser interface { + ParseChunk([]byte) (*models.TextChunk, error) + FormMsg(msg, role string, cont bool) (io.Reader, error) + GetToken() string + GetAPIType() models.APIType +} + +func choseChunkParser() { + chunkParser = LCPCompletion{} + switch cfg.CurrentAPI { + case "http://localhost:8080/completion", "http://127.0.0.1:8080/completion": + chunkParser = LCPCompletion{} + logger.Debug("chosen lcpcompletion", "link", cfg.CurrentAPI) + return + case "http://localhost:8080/v1/chat/completions", "http://127.0.0.1:8080/v1/chat/completions": + chunkParser = LCPChat{} + logger.Debug("chosen lcpchat", "link", cfg.CurrentAPI) + return + case "https://api.deepseek.com/beta/completions": + chunkParser = DeepSeekerCompletion{} + logger.Debug("chosen deepseekercompletio", "link", cfg.CurrentAPI) + return + case "https://api.deepseek.com/chat/completions": + chunkParser = DeepSeekerChat{} + logger.Debug("chosen deepseekerchat", "link", cfg.CurrentAPI) + return + case "https://openrouter.ai/api/v1/completions": + chunkParser = OpenRouterCompletion{} + logger.Debug("chosen openroutercompletion", "link", cfg.CurrentAPI) + return + case "https://openrouter.ai/api/v1/chat/completions": + chunkParser = OpenRouterChat{} + logger.Debug("chosen openrouterchat", "link", cfg.CurrentAPI) + return + default: + logger.Warn("unexpected case, assuming llama.cpp on non default address", "link", cfg.CurrentAPI) + if strings.Contains(cfg.CurrentAPI, "chat") { + chunkParser = LCPChat{} + return + } + chunkParser = LCPCompletion{} + } +} + +type LCPCompletion struct { +} +type LCPChat struct { +} +type DeepSeekerCompletion struct { +} +type DeepSeekerChat struct { +} +type OpenRouterCompletion struct { + Model string +} +type OpenRouterChat struct { + Model string +} + +func (lcp LCPCompletion) GetAPIType() models.APIType { + return models.APITypeCompletion +} + +func (lcp LCPCompletion) GetToken() string { + return "" +} + +func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg lcpcompletion", "link", cfg.CurrentAPI) + localImageAttachmentPath := imageAttachmentPath + var multimodalData []string + if msg != "" { // otherwise let the bot to continue + var newMsg models.RoleMsg + if localImageAttachmentPath != "" { + newMsg = models.NewMultimodalMsg(role, []any{}) + newMsg.AddTextPart(msg) + imageURL, err := models.CreateImageURLFromPath(localImageAttachmentPath) + if err != nil { + logger.Error("failed to create image URL from path for completion", + "error", err, "path", localImageAttachmentPath) + return nil, err + } + newMsg.AddImagePart(imageURL, localImageAttachmentPath) + imageAttachmentPath = "" // Clear the attachment after use + } else { // not a multimodal msg or image passed in tool call + newMsg = models.RoleMsg{Role: role, Content: msg} + } + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + } + // sending description of the tools and how to use them + if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() { + chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg}) + } + filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) + // Build prompt and extract images inline as we process each message + messages := make([]string, len(filteredMessages)) + for i := range filteredMessages { + m := stripThinkingFromMsg(&filteredMessages[i]) + messages[i] = m.ToPrompt() + // Extract images from this message and add marker inline + if len(m.ContentParts) > 0 { + for _, part := range m.ContentParts { + var imgURL string + // Check for struct type + if imgPart, ok := part.(models.ImageContentPart); ok { + imgURL = imgPart.ImageURL.URL + } else if partMap, ok := part.(map[string]any); ok { + // Check for map type (from JSON unmarshaling) + if partType, exists := partMap["type"]; exists && partType == "image_url" { + if imgURLMap, ok := partMap["image_url"].(map[string]any); ok { + if url, ok := imgURLMap["url"].(string); ok { + imgURL = url + } + } + } + } + if imgURL != "" { + // Extract base64 part from data URL (e.g., "data:image/jpeg;base64,...") + parts := strings.SplitN(imgURL, ",", 2) + if len(parts) == 2 { + multimodalData = append(multimodalData, parts[1]) + messages[i] += " <__media__>" + } + } + } + } + } + prompt := strings.Join(messages, "\n") + // needs to be after <__media__> if there are images + if !resume { + botMsgStart := "\n" + botPersona + ":\n" + prompt += botMsgStart + } + logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse, + "msg", msg, "resume", resume, "prompt", prompt, "multimodal_data_count", len(multimodalData)) + payload := models.NewLCPReq(prompt, chatBody.Model, multimodalData, + defaultLCPProps, chatBody.MakeStopSliceExcluding("", listChatRoles())) + data, err := json.Marshal(payload) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} + +func (lcp LCPCompletion) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.LlamaCPPResp{} + resp := &models.TextChunk{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + resp.Chunk = llmchunk.Content + if llmchunk.Stop { + if llmchunk.Content != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Finished = true + } + return resp, nil +} + +func (lcp LCPChat) GetAPIType() models.APIType { + return models.APITypeChat +} + +func (lcp LCPChat) GetToken() string { + return "" +} + +func (op LCPChat) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.LLMRespChunk{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + if len(llmchunk.Choices) == 0 { + logger.Warn("LCPChat empty chunk choices", "raw_data", string(data), "chunk", llmchunk) + return &models.TextChunk{}, nil + } + lastChoice := llmchunk.Choices[len(llmchunk.Choices)-1] + resp := &models.TextChunk{ + Chunk: lastChoice.Delta.Content, + Reasoning: lastChoice.Delta.ReasoningContent, + } + // Check for tool calls in all choices, not just the last one + for _, choice := range llmchunk.Choices { + if len(choice.Delta.ToolCalls) > 0 { + toolCall := choice.Delta.ToolCalls[0] + resp.ToolChunk = toolCall.Function.Arguments + fname := toolCall.Function.Name + if fname != "" { + resp.FuncName = fname + } + // Capture the tool call ID if available + resp.ToolID = toolCall.ID + break // Process only the first tool call + } + } + if lastChoice.FinishReason == "stop" { + if resp.Chunk != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Finished = true + } + if resp.ToolChunk != "" { + resp.ToolResp = true + } + return resp, nil +} + +func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg lcpchat", "link", cfg.CurrentAPI) + // Capture the image attachment path at the beginning to avoid race conditions + // with API rotation that might clear the global variable + localImageAttachmentPath := imageAttachmentPath + if msg != "" { // otherwise let the bot continue + // Create the message with support for multimodal content + var newMsg models.RoleMsg + // Check if we have an image to add to this message + if localImageAttachmentPath != "" { + // Create a multimodal message with both text and image + newMsg = models.NewMultimodalMsg(role, []interface{}{}) + // Add the text content + newMsg.AddTextPart(msg) + // Add the image content + imageURL, err := models.CreateImageURLFromPath(localImageAttachmentPath) + if err != nil { + logger.Error("failed to create image URL from path", "error", err, "path", localImageAttachmentPath) + // If image processing fails, fall back to simple text message + newMsg = models.NewRoleMsg(role, msg) + } else { + newMsg.AddImagePart(imageURL, localImageAttachmentPath) + } + // Only clear the global image attachment after successfully processing it in this API call + imageAttachmentPath = "" // Clear the attachment after use + } else { + // Create a simple text message + newMsg = models.NewRoleMsg(role, msg) + } + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + logger.Debug("LCPChat FormMsg: added message to chatBody", "role", newMsg.Role, + "content_len", len(newMsg.Content), "message_count_after_add", len(chatBody.Messages)) + } + filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages) + // openai /v1/chat does not support custom roles; needs to be user, assistant, system + // Add persona suffix to the last user message to indicate who the assistant should reply as + bodyCopy := &models.ChatBody{ + Messages: make([]models.RoleMsg, len(filteredMessages)), + Model: chatBody.Model, + Stream: chatBody.Stream, + } + for i := range filteredMessages { + strippedMsg := *stripThinkingFromMsg(&filteredMessages[i]) + switch strippedMsg.Role { + case cfg.UserRole: + bodyCopy.Messages[i] = strippedMsg + 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 + // bodyCopy.Messages[i].ToolCall = nil + } + // Clean null/empty messages to prevent API issues + bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) + req := models.OpenAIReq{ + ChatBody: bodyCopy, + Tools: nil, + } + if cfg.ToolUse && !resume && role != cfg.ToolRole { + req.Tools = baseTools // set tools to use + } + data, err := json.Marshal(req) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} + +// deepseek +func (ds DeepSeekerCompletion) GetAPIType() models.APIType { + return models.APITypeCompletion +} + +func (ds DeepSeekerCompletion) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.DSCompletionResp{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + if len(llmchunk.Choices) == 0 { + logger.Warn("empty chunk choices", "raw_data", string(data), "chunk", llmchunk) + return &models.TextChunk{}, nil + } + resp := &models.TextChunk{ + Chunk: llmchunk.Choices[0].Text, + } + if llmchunk.Choices[0].FinishReason != "" { + if resp.Chunk != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Finished = true + } + return resp, nil +} + +func (ds DeepSeekerCompletion) GetToken() string { + return cfg.DeepSeekToken +} + +func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg deepseekercompletion", "link", cfg.CurrentAPI) + if err := deepseekModelValidator(); err != nil { + return nil, err + } + if msg != "" { // otherwise let the bot to continue + newMsg := models.RoleMsg{Role: role, Content: msg} + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + } + // sending description of the tools and how to use them + if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() { + chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg}) + } + filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) + messages := make([]string, len(filteredMessages)) + for i := range filteredMessages { + messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt() + } + prompt := strings.Join(messages, "\n") + // strings builder? + if !resume { + botMsgStart := "\n" + botPersona + ":\n" + prompt += botMsgStart + } + logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse, + "msg", msg, "resume", resume, "prompt", prompt) + payload := models.NewDSCompletionReq(prompt, chatBody.Model, + defaultLCPProps["temp"], + chatBody.MakeStopSliceExcluding("", listChatRoles())) + data, err := json.Marshal(payload) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} + +func (ds DeepSeekerChat) GetAPIType() models.APIType { + return models.APITypeChat +} + +func (ds DeepSeekerChat) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.DSChatStreamResp{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + resp := &models.TextChunk{} + if len(llmchunk.Choices) == 0 { + logger.Warn("empty chunk choices", "raw_data", string(data), "chunk", llmchunk) + return resp, nil + } + if llmchunk.Choices[0].FinishReason != "" { + if llmchunk.Choices[0].Delta.Content != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Chunk = llmchunk.Choices[0].Delta.Content + resp.Finished = true + } else { + if llmchunk.Choices[0].Delta.ReasoningContent != "" { + resp.Chunk = llmchunk.Choices[0].Delta.ReasoningContent + } else { + resp.Chunk = llmchunk.Choices[0].Delta.Content + } + } + return resp, nil +} + +func (ds DeepSeekerChat) GetToken() string { + return cfg.DeepSeekToken +} + +func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg deepseekerchat", "link", cfg.CurrentAPI) + if err := deepseekModelValidator(); err != nil { + return nil, err + } + if msg != "" { // otherwise let the bot continue + newMsg := models.RoleMsg{Role: role, Content: msg} + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + } + // Create copy of chat body with standardized user role + filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages) + // Add persona suffix to the last user message to indicate who the assistant should reply as + bodyCopy := &models.ChatBody{ + Messages: make([]models.RoleMsg, len(filteredMessages)), + Model: chatBody.Model, + Stream: chatBody.Stream, + } + for i := range filteredMessages { + strippedMsg := *stripThinkingFromMsg(&filteredMessages[i]) + 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].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 + // bodyCopy.Messages[i].ToolCall = nil + } + // Clean null/empty messages to prevent API issues + bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) + dsBody := models.NewDSChatReq(*bodyCopy) + data, err := json.Marshal(dsBody) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} + +// openrouter +func (or OpenRouterCompletion) GetAPIType() models.APIType { + return models.APITypeCompletion +} + +func (or OpenRouterCompletion) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.OpenRouterCompletionResp{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + if len(llmchunk.Choices) == 0 { + logger.Warn("empty chunk choices", "raw_data", string(data), "chunk", llmchunk) + return &models.TextChunk{}, nil + } + resp := &models.TextChunk{ + Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Text, + } + if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" { + if resp.Chunk != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Finished = true + } + return resp, nil +} + +func (or OpenRouterCompletion) GetToken() string { + return cfg.OpenRouterToken +} + +func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg openroutercompletion", "link", cfg.CurrentAPI) + if msg != "" { // otherwise let the bot to continue + newMsg := models.RoleMsg{Role: role, Content: msg} + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + } + // sending description of the tools and how to use them + if cfg.ToolUse && !resume && role == cfg.UserRole && !containsToolSysMsg() { + chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg}) + } + filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages) + messages := make([]string, len(filteredMessages)) + for i := range filteredMessages { + messages[i] = stripThinkingFromMsg(&filteredMessages[i]).ToPrompt() + } + prompt := strings.Join(messages, "\n") + // strings builder? + if !resume { + botMsgStart := "\n" + botPersona + ":\n" + prompt += botMsgStart + } + stopSlice := chatBody.MakeStopSliceExcluding("", listChatRoles()) + logger.Debug("checking prompt for /completion", "tool_use", cfg.ToolUse, + "msg", msg, "resume", resume, "prompt", prompt, "stop_strings", stopSlice) + payload := models.NewOpenRouterCompletionReq(chatBody.Model, prompt, + defaultLCPProps, stopSlice) + data, err := json.Marshal(payload) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} + +// chat +func (or OpenRouterChat) GetAPIType() models.APIType { + return models.APITypeChat +} + +func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) { + llmchunk := models.OpenRouterChatResp{} + if err := json.Unmarshal(data, &llmchunk); err != nil { + logger.Error("failed to decode", "error", err, "line", string(data)) + return nil, err + } + if len(llmchunk.Choices) == 0 { + logger.Warn("empty chunk choices", "raw_data", string(data), "chunk", llmchunk) + return &models.TextChunk{}, nil + } + lastChoice := llmchunk.Choices[len(llmchunk.Choices)-1] + resp := &models.TextChunk{ + Chunk: lastChoice.Delta.Content, + Reasoning: lastChoice.Delta.Reasoning, + } + // Handle tool calls similar to LCPChat + if len(lastChoice.Delta.ToolCalls) > 0 { + toolCall := lastChoice.Delta.ToolCalls[0] + resp.ToolChunk = toolCall.Function.Arguments + fname := toolCall.Function.Name + if fname != "" { + resp.FuncName = fname + } + // Capture the tool call ID if available + resp.ToolID = toolCall.ID + } + if resp.ToolChunk != "" { + resp.ToolResp = true + } + if lastChoice.FinishReason == "stop" { + if resp.Chunk != "" { + logger.Error("text inside of finish llmchunk", "chunk", llmchunk) + } + resp.Finished = true + } + return resp, nil +} + +func (or OpenRouterChat) GetToken() string { + return cfg.OpenRouterToken +} + +func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, error) { + logger.Debug("formmsg open router completion", "link", cfg.CurrentAPI) + // Capture the image attachment path at the beginning to avoid race conditions + // with API rotation that might clear the global variable + localImageAttachmentPath := imageAttachmentPath + if msg != "" { // otherwise let the bot continue + var newMsg models.RoleMsg + // Check if we have an image to add to this message + if localImageAttachmentPath != "" { + // Create a multimodal message with both text and image + newMsg = models.NewMultimodalMsg(role, []interface{}{}) + // Add the text content + newMsg.AddTextPart(msg) + // Add the image content + imageURL, err := models.CreateImageURLFromPath(localImageAttachmentPath) + if err != nil { + logger.Error("failed to create image URL from path", "error", err, "path", localImageAttachmentPath) + // If image processing fails, fall back to simple text message + newMsg = models.NewRoleMsg(role, msg) + } else { + newMsg.AddImagePart(imageURL, localImageAttachmentPath) + } + // Only clear the global image attachment after successfully processing it in this API call + imageAttachmentPath = "" // Clear the attachment after use + } else { + // Create a simple text message + newMsg = models.NewRoleMsg(role, msg) + } + newMsg = *processMessageTag(&newMsg) + chatBody.Messages = append(chatBody.Messages, newMsg) + } + // Create copy of chat body with standardized user role + filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages) + // Add persona suffix to the last user message to indicate who the assistant should reply as + bodyCopy := &models.ChatBody{ + Messages: make([]models.RoleMsg, len(filteredMessages)), + Model: chatBody.Model, + Stream: chatBody.Stream, + } + for i := range filteredMessages { + strippedMsg := *stripThinkingFromMsg(&filteredMessages[i]) + switch strippedMsg.Role { + case cfg.UserRole: + bodyCopy.Messages[i] = strippedMsg + 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 + bodyCopy.Messages = consolidateAssistantMessages(bodyCopy.Messages) + orBody := models.NewOpenRouterChatReq(*bodyCopy, defaultLCPProps, cfg.ReasoningEffort) + if cfg.ToolUse && !resume && role != cfg.ToolRole { + orBody.Tools = baseTools // set tools to use + } + data, err := json.Marshal(orBody) + if err != nil { + logger.Error("failed to form a msg", "error", err) + return nil, err + } + return bytes.NewReader(data), nil +} @@ -1,245 +1,34 @@ package main import ( - "fmt" - "path" - "strconv" - "time" - "unicode" + "sync/atomic" - "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) var ( - botRespMode = false - editMode = false - botMsg = "no" - selectedIndex = int(-1) - indexLine = "Esc: send msg; PgUp/Down: switch focus; F1: manage chats; F2: regen last; F3:delete last msg; F4: edit msg; F5: toggle system; F6: interrupt bot resp; Row: [yellow]%d[white], Column: [yellow]%d; bot resp mode: %v" - focusSwitcher = map[tview.Primitive]tview.Primitive{} + boolColors = map[bool]string{true: "green", false: "red"} + botRespMode atomic.Bool + toolRunningMode atomic.Bool + editMode = false + roleEditMode = false + injectRole = true + selectedIndex = int(-1) + shellMode = false + shellHistory []string + shellHistoryPos int = -1 + 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) | 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 ) -func isASCII(s string) bool { - for i := 0; i < len(s); i++ { - if s[i] > unicode.MaxASCII { - return false - } - } - return true -} - func main() { - app := tview.NewApplication() - pages := tview.NewPages() - textArea := tview.NewTextArea(). - SetPlaceholder("Type your prompt...") - textArea.SetBorder(true).SetTitle("input") - textView := tview.NewTextView(). - SetDynamicColors(true). - SetRegions(true). - SetChangedFunc(func() { - app.Draw() - }) - textView.SetBorder(true).SetTitle("chat") - focusSwitcher[textArea] = textView - focusSwitcher[textView] = textArea - position := tview.NewTextView(). - SetDynamicColors(true). - SetTextAlign(tview.AlignCenter) - flex := tview.NewFlex().SetDirection(tview.FlexRow). - AddItem(textView, 0, 40, false). - AddItem(textArea, 0, 10, true). - AddItem(position, 0, 1, false) - updateStatusLine := func() { - fromRow, fromColumn, toRow, toColumn := textArea.GetCursor() - if fromRow == toRow && fromColumn == toColumn { - position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode)) - } else { - position.SetText(fmt.Sprintf("Esc: send msg; PgUp/Down: switch focus; F1: manage chats; F2: regen last; F3:delete last msg; F4: edit msg; F5: toggle system; F6: interrupt bot resp; Row: [yellow]%d[white], Column: [yellow]%d[white] - [red]To[white] Row: [yellow]%d[white], To Column: [yellow]%d; bot resp mode: %v", fromRow, fromColumn, toRow, toColumn, botRespMode)) - } - } - chatOpts := []string{"cancel", "new"} - fList, err := loadHistoryChats() - if err != nil { - panic(err) - } - chatOpts = append(chatOpts, fList...) - chatActModal := tview.NewModal(). - SetText("Chat actions:"). - AddButtons(chatOpts). - SetDoneFunc(func(buttonIndex int, buttonLabel string) { - switch buttonLabel { - case "new": - // set chat body - chatBody.Messages = defaultStarter - textView.SetText(chatToText(showSystemMsgs)) - activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix())) - pages.RemovePage("history") - return - // set text - case "cancel": - pages.RemovePage("history") - return - default: - fn := buttonLabel - history, err := loadHistoryChat(fn) - if err != nil { - logger.Error("failed to read history file", "filename", fn) - pages.RemovePage("history") - return - } - chatBody.Messages = history - textView.SetText(chatToText(showSystemMsgs)) - activeChatName = fn - pages.RemovePage("history") - return - } - }) - editArea := tview.NewTextArea(). - SetPlaceholder("Replace msg...") - editArea.SetBorder(true).SetTitle("input") - editArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - if event.Key() == tcell.KeyEscape && editMode { - editedMsg := editArea.GetText() - if editedMsg == "" { - notifyUser("edit", "no edit provided") - pages.RemovePage("editArea") - editMode = false - return nil - } - chatBody.Messages[selectedIndex].Content = editedMsg - // change textarea - textView.SetText(chatToText(showSystemMsgs)) - pages.RemovePage("editArea") - editMode = false - return nil - } - return event - }) - indexPickWindow := tview.NewInputField(). - SetLabel("Enter a msg index: "). - SetFieldWidth(4). - SetAcceptanceFunc(tview.InputFieldInteger). - SetDoneFunc(func(key tcell.Key) { - pages.RemovePage("getIndex") - return - }) - indexPickWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - si := indexPickWindow.GetText() - selectedIndex, err = strconv.Atoi(si) - if err != nil { - logger.Error("failed to convert provided index", "error", err, "si", si) - } - if len(chatBody.Messages) <= selectedIndex && selectedIndex < 0 { - logger.Warn("chosen index is out of bounds", "index", selectedIndex) - return nil - } - m := chatBody.Messages[selectedIndex] - if editMode && event.Key() == tcell.KeyEnter { - pages.AddPage("editArea", editArea, true, true) - editArea.SetText(m.Content, true) - } - if !editMode && event.Key() == tcell.KeyEnter { - // TODO: add notification that text was copied - copyToClipboard(m.Content) - notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:30]) - notifyUser("copied", notification) - } - return event - }) - // - textArea.SetMovedFunc(updateStatusLine) - updateStatusLine() - textView.SetText(chatToText(showSystemMsgs)) - textView.ScrollToEnd() - app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - if event.Key() == tcell.KeyF1 { - // fList, err := listHistoryFiles(historyDir) - fList, err := loadHistoryChats() - if err != nil { - panic(err) - } - chatOpts = append(chatOpts, fList...) - pages.AddPage("history", chatActModal, true, true) - return nil - } - if event.Key() == tcell.KeyF2 { - // regen last msg - chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1] - textView.SetText(chatToText(showSystemMsgs)) - go chatRound("", userRole, textView) - return nil - } - if event.Key() == tcell.KeyF3 { - // delete last msg - chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1] - textView.SetText(chatToText(showSystemMsgs)) - botRespMode = false // hmmm; is that correct? - return nil - } - if event.Key() == tcell.KeyF4 { - // edit msg - editMode = true - pages.AddPage("getIndex", indexPickWindow, true, true) - return nil - } - if event.Key() == tcell.KeyF5 { - // switch showSystemMsgs - showSystemMsgs = !showSystemMsgs - textView.SetText(chatToText(showSystemMsgs)) - } - if event.Key() == tcell.KeyF6 { - interruptResp = true - botRespMode = false - return nil - } - if event.Key() == tcell.KeyF7 { - // copy msg to clipboard - editMode = false - m := chatBody.Messages[len(chatBody.Messages)-1] - copyToClipboard(m.Content) - notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:30]) - notifyUser("copied", notification) - return nil - } - if event.Key() == tcell.KeyF8 { - // copy msg to clipboard - editMode = false - pages.AddPage("getIndex", indexPickWindow, true, true) - return nil - } - // cannot send msg in editMode or botRespMode - if event.Key() == tcell.KeyEscape && !editMode && !botRespMode { - fromRow, fromColumn, _, _ := textArea.GetCursor() - position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode)) - // read all text into buffer - msgText := textArea.GetText() - if msgText != "" { - fmt.Fprintf(textView, "\n(%d) <user>: %s\n", len(chatBody.Messages), msgText) - textArea.SetText("", true) - textView.ScrollToEnd() - } - // update statue line - go chatRound(msgText, userRole, textView) - return nil - } - if event.Key() == tcell.KeyPgUp || event.Key() == tcell.KeyPgDn { - currentF := app.GetFocus() - app.SetFocus(focusSwitcher[currentF]) - return nil - } - if isASCII(string(event.Rune())) && !botRespMode { - // botRespMode = false - // fromRow, fromColumn, _, _ := textArea.GetCursor() - // position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode)) - return event - } - return event - }) pages.AddPage("main", flex, true, true) if err := app.SetRoot(pages, - true).EnableMouse(true).Run(); err != nil { - panic(err) + true).EnableMouse(cfg.EnableMouse).EnablePaste(true).Run(); err != nil { + logger.Error("failed to start tview app", "error", err) + return } } diff --git a/models/card.go b/models/card.go new file mode 100644 index 0000000..0bf437c --- /dev/null +++ b/models/card.go @@ -0,0 +1,70 @@ +package models + +import ( + "crypto/md5" + "fmt" + "strings" +) + +// https://github.com/malfoyslastname/character-card-spec-v2/blob/main/spec_v2.md +// what a bloat; trim to Role->Msg pair and first msg +type CharCardSpec struct { + Name string `json:"name"` + Description string `json:"description"` + Personality string `json:"personality"` + FirstMes string `json:"first_mes"` + Avatar string `json:"avatar"` + Chat string `json:"chat"` + MesExample string `json:"mes_example"` + Scenario string `json:"scenario"` + CreateDate string `json:"create_date"` + Talkativeness string `json:"talkativeness"` + Fav bool `json:"fav"` + Creatorcomment string `json:"creatorcomment"` + Spec string `json:"spec"` + SpecVersion string `json:"spec_version"` + Tags []any `json:"tags"` + Extentions []byte `json:"extentions"` +} + +type Spec2Wrapper struct { + Data CharCardSpec `json:"data"` +} + +func (c *CharCardSpec) Simplify(userName, fpath string) *CharCard { + fm := strings.ReplaceAll(strings.ReplaceAll(c.FirstMes, "{{char}}", c.Name), "{{user}}", userName) + sysPr := strings.ReplaceAll(strings.ReplaceAll(c.Description, "{{char}}", c.Name), "{{user}}", userName) + return &CharCard{ + ID: ComputeCardID(c.Name, fpath), + SysPrompt: sysPr, + FirstMsg: fm, + Role: c.Name, + FilePath: fpath, + Characters: []string{c.Name, userName}, + } +} + +func ComputeCardID(role, filePath string) string { + return fmt.Sprintf("%x", md5.Sum([]byte(role+filePath))) +} + +type CharCard struct { + ID string `json:"id"` + SysPrompt string `json:"sys_prompt"` + FirstMsg string `json:"first_msg"` + Role string `json:"role"` + Characters []string `json:"chars"` + FilePath string `json:"filepath"` +} + +func (cc *CharCard) ToSpec(userName string) *CharCardSpec { + descr := strings.ReplaceAll(strings.ReplaceAll(cc.SysPrompt, cc.Role, "{{char}}"), userName, "{{user}}") + return &CharCardSpec{ + Name: cc.Role, + Description: descr, + FirstMes: cc.FirstMsg, + Spec: "chara_card_v2", + SpecVersion: "2.0", + Extentions: []byte("{}"), + } +} diff --git a/models/consts.go b/models/consts.go new file mode 100644 index 0000000..8b4002b --- /dev/null +++ b/models/consts.go @@ -0,0 +1,13 @@ +package models + +const ( + LoadedMark = "(loaded) " + ToolRespMultyType = "multimodel_content" +) + +type APIType int + +const ( + APITypeChat APIType = iota + APITypeCompletion +) diff --git a/models/db.go b/models/db.go index 5f49003..73a0b53 100644 --- a/models/db.go +++ b/models/db.go @@ -8,13 +8,14 @@ import ( type Chat struct { ID uint32 `db:"id" json:"id"` Name string `db:"name" json:"name"` - Msgs string `db:"msgs" json:"msgs"` // []MessagesStory to string json + Msgs string `db:"msgs" json:"msgs"` // []RoleMsg to string json + Agent string `db:"agent" json:"agent"` CreatedAt time.Time `db:"created_at" json:"created_at"` UpdatedAt time.Time `db:"updated_at" json:"updated_at"` } -func (c Chat) ToHistory() ([]MessagesStory, error) { - resp := []MessagesStory{} +func (c *Chat) ToHistory() ([]RoleMsg, error) { + resp := []RoleMsg{} if err := json.Unmarshal([]byte(c.Msgs), &resp); err != nil { return nil, err } @@ -34,3 +35,13 @@ type Memory struct { CreatedAt time.Time `db:"created_at" json:"created_at"` UpdatedAt time.Time `db:"updated_at" json:"updated_at"` } + +// vector models + +type VectorRow struct { + Embeddings []float32 `db:"embeddings" json:"embeddings"` + Slug string `db:"slug" json:"slug"` + RawText string `db:"raw_text" json:"raw_text"` + Distance float32 `db:"distance" json:"distance"` + FileName string `db:"filename" json:"filename"` +} diff --git a/models/deepseek.go b/models/deepseek.go new file mode 100644 index 0000000..8f9868d --- /dev/null +++ b/models/deepseek.go @@ -0,0 +1,144 @@ +package models + +type DSChatReq struct { + Messages []RoleMsg `json:"messages"` + Model string `json:"model"` + Stream bool `json:"stream"` + FrequencyPenalty int `json:"frequency_penalty"` + MaxTokens int `json:"max_tokens"` + PresencePenalty int `json:"presence_penalty"` + Temperature float32 `json:"temperature"` + TopP float32 `json:"top_p"` + // ResponseFormat struct { + // Type string `json:"type"` + // } `json:"response_format"` + // Stop any `json:"stop"` + // StreamOptions any `json:"stream_options"` + // Tools any `json:"tools"` + // ToolChoice string `json:"tool_choice"` + // Logprobs bool `json:"logprobs"` + // TopLogprobs any `json:"top_logprobs"` +} + +func NewDSChatReq(cb ChatBody) DSChatReq { + return DSChatReq{ + Messages: cb.Messages, + Model: cb.Model, + Stream: cb.Stream, + MaxTokens: 2048, + PresencePenalty: 0, + FrequencyPenalty: 0, + Temperature: 1.0, + TopP: 1.0, + } +} + +type DSCompletionReq struct { + Model string `json:"model"` + Prompt string `json:"prompt"` + Echo bool `json:"echo"` + FrequencyPenalty int `json:"frequency_penalty"` + // Logprobs int `json:"logprobs"` + MaxTokens int `json:"max_tokens"` + PresencePenalty int `json:"presence_penalty"` + Stop any `json:"stop"` + Stream bool `json:"stream"` + StreamOptions any `json:"stream_options"` + Suffix any `json:"suffix"` + Temperature float32 `json:"temperature"` + TopP float32 `json:"top_p"` +} + +func NewDSCompletionReq(prompt, model string, temp float32, stopSlice []string) DSCompletionReq { + return DSCompletionReq{ + Model: model, + Prompt: prompt, + Temperature: temp, + Stream: true, + Echo: false, + MaxTokens: 2048, + PresencePenalty: 0, + FrequencyPenalty: 0, + TopP: 1.0, + Stop: stopSlice, + } +} + +type DSCompletionResp struct { + ID string `json:"id"` + Choices []struct { + FinishReason string `json:"finish_reason"` + Index int `json:"index"` + Logprobs struct { + TextOffset []int `json:"text_offset"` + TokenLogprobs []int `json:"token_logprobs"` + Tokens []string `json:"tokens"` + TopLogprobs []struct { + } `json:"top_logprobs"` + } `json:"logprobs"` + Text string `json:"text"` + } `json:"choices"` + Created int `json:"created"` + Model string `json:"model"` + SystemFingerprint string `json:"system_fingerprint"` + Object string `json:"object"` + Usage struct { + CompletionTokens int `json:"completion_tokens"` + PromptTokens int `json:"prompt_tokens"` + PromptCacheHitTokens int `json:"prompt_cache_hit_tokens"` + PromptCacheMissTokens int `json:"prompt_cache_miss_tokens"` + TotalTokens int `json:"total_tokens"` + CompletionTokensDetails struct { + ReasoningTokens int `json:"reasoning_tokens"` + } `json:"completion_tokens_details"` + } `json:"usage"` +} + +type DSChatResp struct { + Choices []struct { + Delta struct { + Content string `json:"content"` + Role any `json:"role"` + } `json:"delta"` + FinishReason string `json:"finish_reason"` + Index int `json:"index"` + Logprobs any `json:"logprobs"` + } `json:"choices"` + Created int `json:"created"` + ID string `json:"id"` + Model string `json:"model"` + Object string `json:"object"` + SystemFingerprint string `json:"system_fingerprint"` + Usage struct { + CompletionTokens int `json:"completion_tokens"` + PromptTokens int `json:"prompt_tokens"` + TotalTokens int `json:"total_tokens"` + } `json:"usage"` +} + +type DSChatStreamResp struct { + ID string `json:"id"` + Object string `json:"object"` + Created int `json:"created"` + Model string `json:"model"` + SystemFingerprint string `json:"system_fingerprint"` + Choices []struct { + Index int `json:"index"` + Delta struct { + Content string `json:"content"` + ReasoningContent string `json:"reasoning_content"` + } `json:"delta"` + Logprobs any `json:"logprobs"` + FinishReason string `json:"finish_reason"` + } `json:"choices"` +} + +type DSBalance struct { + IsAvailable bool `json:"is_available"` + BalanceInfos []struct { + Currency string `json:"currency"` + TotalBalance string `json:"total_balance"` + GrantedBalance string `json:"granted_balance"` + ToppedUpBalance string `json:"topped_up_balance"` + } `json:"balance_infos"` +} diff --git a/models/embed.go b/models/embed.go new file mode 100644 index 0000000..078312c --- /dev/null +++ b/models/embed.go @@ -0,0 +1,15 @@ +package models + +type LCPEmbedResp struct { + Model string `json:"model"` + Object string `json:"object"` + Usage struct { + PromptTokens int `json:"prompt_tokens"` + TotalTokens int `json:"total_tokens"` + } `json:"usage"` + Data []struct { + Embedding []float32 `json:"embedding"` + Index int `json:"index"` + Object string `json:"object"` + } `json:"data"` +} diff --git a/models/extra.go b/models/extra.go new file mode 100644 index 0000000..5c60a26 --- /dev/null +++ b/models/extra.go @@ -0,0 +1,49 @@ +package models + +import ( + "regexp" + "strings" +) + +type AudioFormat string + +const ( + AFWav AudioFormat = "wav" + AFMP3 AudioFormat = "mp3" +) + +var threeOrMoreDashesRE = regexp.MustCompile(`-{3,}`) + +// CleanText removes markdown and special characters that are not suitable for TTS +func CleanText(text string) string { + // Remove markdown-like characters that might interfere with TTS + text = strings.ReplaceAll(text, "*", "") // Bold/italic markers + text = strings.ReplaceAll(text, "#", "") // Headers + text = strings.ReplaceAll(text, "_", "") // Underline/italic markers + text = strings.ReplaceAll(text, "~", "") // Strikethrough markers + text = strings.ReplaceAll(text, "`", "") // Code markers + text = strings.ReplaceAll(text, "[", "") // Link brackets + text = strings.ReplaceAll(text, "]", "") // Link brackets + text = strings.ReplaceAll(text, "!", "") // Exclamation marks (if not punctuation) + // Remove HTML tags using regex + htmlTagRegex := regexp.MustCompile(`<[^>]*>`) + text = htmlTagRegex.ReplaceAllString(text, "") + // Split text into lines to handle table separators + lines := strings.Split(text, "\n") + var filteredLines []string + for _, line := range lines { + // Check if the line looks like a table separator (e.g., |----|, |===|, | - - - |) + // A table separator typically contains only |, -, =, and spaces + isTableSeparator := regexp.MustCompile(`^\s*\|\s*[-=\s]+\|\s*$`).MatchString(strings.TrimSpace(line)) + if !isTableSeparator { + // If it's not a table separator, remove vertical bars but keep the content + processedLine := strings.ReplaceAll(line, "|", "") + filteredLines = append(filteredLines, processedLine) + } + // If it is a table separator, skip it (don't add to filteredLines) + } + text = strings.Join(filteredLines, "\n") + text = threeOrMoreDashesRE.ReplaceAllString(text, "") + text = strings.TrimSpace(text) // Remove leading/trailing whitespace + return text +} diff --git a/models/models.go b/models/models.go index 880779f..97d0272 100644 --- a/models/models.go +++ b/models/models.go @@ -1,19 +1,23 @@ package models import ( + "encoding/base64" + "encoding/json" "fmt" + "os" "strings" ) -// type FuncCall struct { -// XMLName xml.Name `xml:"tool_call"` -// Name string `xml:"name"` -// Args []string `xml:"args"` -// } - type FuncCall struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Args map[string]string `json:"args"` +} + +type ToolCall struct { + ID string `json:"id,omitempty"` Name string `json:"name"` - Args string `json:"args"` + Args string `json:"arguments"` } type LLMResp struct { @@ -36,13 +40,26 @@ type LLMResp struct { ID string `json:"id"` } +type ToolDeltaFunc struct { + Name string `json:"name"` + Arguments string `json:"arguments"` +} + +type ToolDeltaResp struct { + ID string `json:"id,omitempty"` + Index int `json:"index"` + Function ToolDeltaFunc `json:"function"` +} + // for streaming type LLMRespChunk struct { Choices []struct { FinishReason string `json:"finish_reason"` Index int `json:"index"` Delta struct { - Content string `json:"content"` + Content string `json:"content"` + ReasoningContent string `json:"reasoning_content"` + ToolCalls []ToolDeltaResp `json:"tool_calls"` } `json:"delta"` } `json:"choices"` Created int `json:"created"` @@ -56,56 +73,569 @@ type LLMRespChunk struct { } `json:"usage"` } -type MessagesStory struct { - Role string `json:"role"` - Content string `json:"content"` +type TextChunk struct { + Chunk string + ToolChunk string + Finished bool + ToolResp bool + FuncName string + ToolID string + Reasoning string // For models that send reasoning separately (OpenRouter, etc.) +} + +type TextContentPart struct { + Type string `json:"type"` + Text string `json:"text"` +} + +type ImageContentPart struct { + Type string `json:"type"` + Path string `json:"path,omitempty"` // Store original file path + ImageURL struct { + URL string `json:"url"` + } `json:"image_url"` +} + +// RoleMsg represents a message with content that can be either a simple string or structured content parts +type RoleMsg struct { + Role string `json:"role"` + Content string `json:"-"` + ContentParts []any `json:"-"` + 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"` + Stats *ResponseStats `json:"stats"` + HasContentParts bool // Flag to indicate which content type to marshal +} + +// MarshalJSON implements custom JSON marshaling for RoleMsg +// +//nolint:gocritic +func (m RoleMsg) MarshalJSON() ([]byte, error) { + if m.HasContentParts { + // Use structured content format + aux := struct { + Role string `json:"role"` + Content []any `json:"content"` + ToolCallID string `json:"tool_call_id,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, + Content: m.ContentParts, + ToolCallID: m.ToolCallID, + ToolCall: m.ToolCall, + IsShellCommand: m.IsShellCommand, + KnownTo: m.KnownTo, + Stats: m.Stats, + } + return json.Marshal(aux) + } else { + // Use simple content format + aux := struct { + Role string `json:"role"` + Content string `json:"content"` + ToolCallID string `json:"tool_call_id,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, + Content: m.Content, + ToolCallID: m.ToolCallID, + ToolCall: m.ToolCall, + IsShellCommand: m.IsShellCommand, + KnownTo: m.KnownTo, + Stats: m.Stats, + } + return json.Marshal(aux) + } +} + +// UnmarshalJSON implements custom JSON unmarshaling for RoleMsg +func (m *RoleMsg) UnmarshalJSON(data []byte) error { + // First, try to unmarshal as structured content format + var structured struct { + Role string `json:"role"` + Content []any `json:"content"` + ToolCallID string `json:"tool_call_id,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 { + m.Role = structured.Role + m.ContentParts = structured.Content + m.ToolCallID = structured.ToolCallID + m.ToolCall = structured.ToolCall + m.IsShellCommand = structured.IsShellCommand + m.KnownTo = structured.KnownTo + m.Stats = structured.Stats + m.HasContentParts = true + return nil + } + + // Otherwise, unmarshal as simple content format + var simple struct { + Role string `json:"role"` + Content string `json:"content"` + ToolCallID string `json:"tool_call_id,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 { + return err + } + m.Role = simple.Role + m.Content = simple.Content + m.ToolCallID = simple.ToolCallID + m.ToolCall = simple.ToolCall + m.IsShellCommand = simple.IsShellCommand + m.KnownTo = simple.KnownTo + m.Stats = simple.Stats + m.HasContentParts = false + return nil +} + +func (m *RoleMsg) ToPrompt() string { + var contentStr string + if !m.HasContentParts { + contentStr = m.Content + } else { + // For structured content, just take the text parts + 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: + // skip images for text display + case map[string]any: + if partType, exists := p["type"]; exists && partType == "text" { + if textVal, textExists := p["text"]; textExists { + if textStr, isStr := textVal.(string); isStr { + textParts = append(textParts, textStr) + } + } + } + } + } + contentStr = strings.Join(textParts, " ") + " " + } + return strings.ReplaceAll(fmt.Sprintf("%s:\n%s", m.Role, contentStr), "\n\n", "\n") } -func (m MessagesStory) ToText(i int) string { - icon := "" - switch m.Role { - case "assistant": - icon = fmt.Sprintf("(%d) <🤖>: ", i) - case "user": - icon = fmt.Sprintf("(%d) <user>: ", i) - case "system": - icon = fmt.Sprintf("(%d) <system>: ", i) - case "tool": - icon = fmt.Sprintf("(%d) <tool>: ", i) +// NewRoleMsg creates a simple RoleMsg with string content +func NewRoleMsg(role, content string) RoleMsg { + return RoleMsg{ + Role: role, + Content: content, + HasContentParts: false, + } +} + +// NewMultimodalMsg creates a RoleMsg with structured content parts (text and images) +func NewMultimodalMsg(role string, contentParts []any) RoleMsg { + return RoleMsg{ + Role: role, + ContentParts: contentParts, + HasContentParts: true, + } +} + +// HasContent returns true if the message has either string content or structured content parts +func (m *RoleMsg) HasContent() bool { + if m.Content != "" { + return true + } + if m.HasContentParts && len(m.ContentParts) > 0 { + return true + } + return false +} + +// IsContentParts returns true if the message uses structured content parts +func (m *RoleMsg) IsContentParts() bool { + return m.HasContentParts +} + +// GetContentParts returns the content parts of the message +func (m *RoleMsg) GetContentParts() []any { + return m.ContentParts +} + +// Copy creates a copy of the RoleMsg with all fields +func (m *RoleMsg) Copy() RoleMsg { + return RoleMsg{ + Role: m.Role, + Content: m.Content, + ContentParts: m.ContentParts, + ToolCallID: m.ToolCallID, + KnownTo: m.KnownTo, + Stats: m.Stats, + HasContentParts: m.HasContentParts, + ToolCall: m.ToolCall, + IsShellCommand: m.IsShellCommand, + } +} + +// GetText returns the text content of the message, handling both +// simple Content and multimodal ContentParts formats. +func (m *RoleMsg) GetText() string { + if !m.HasContentParts { + return m.Content + } + var textParts []string + for _, part := range m.ContentParts { + switch p := part.(type) { + case TextContentPart: + if p.Type == "text" { + textParts = append(textParts, p.Text) + } + case map[string]any: + if partType, exists := p["type"]; exists { + if partType == "text" { + if textVal, textExists := p["text"]; textExists { + if textStr, isStr := textVal.(string); isStr { + textParts = append(textParts, textStr) + } + } + } + } + } + } + return strings.Join(textParts, " ") +} + +// SetText updates the text content of the message. If the message has +// ContentParts (multimodal), it updates the text parts while preserving +// images. If not, it sets the simple Content field. +func (m *RoleMsg) SetText(text string) { + if !m.HasContentParts { + m.Content = text + return + } + var newParts []any + for _, part := range m.ContentParts { + switch p := part.(type) { + case TextContentPart: + if p.Type == "text" { + p.Text = text + newParts = append(newParts, p) + } else { + newParts = append(newParts, p) + } + case map[string]any: + if partType, exists := p["type"]; exists && partType == "text" { + p["text"] = text + newParts = append(newParts, p) + } else { + newParts = append(newParts, p) + } + default: + newParts = append(newParts, part) + } + } + m.ContentParts = newParts +} + +// AddTextPart adds a text content part to the message +func (m *RoleMsg) AddTextPart(text string) { + if !m.HasContentParts { + // Convert to content parts format + if m.Content != "" { + m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}} + } else { + m.ContentParts = []any{} + } + m.HasContentParts = true + } + textPart := TextContentPart{Type: "text", Text: text} + m.ContentParts = append(m.ContentParts, textPart) +} + +// AddImagePart adds an image content part to the message +func (m *RoleMsg) AddImagePart(imageURL, imagePath string) { + if !m.HasContentParts { + // Convert to content parts format + if m.Content != "" { + m.ContentParts = []any{TextContentPart{Type: "text", Text: m.Content}} + } else { + m.ContentParts = []any{} + } + m.HasContentParts = true + } + imagePart := ImageContentPart{ + Type: "image_url", + Path: imagePath, // Store the original file path + ImageURL: struct { + URL string `json:"url"` + }{URL: imageURL}, } - textMsg := fmt.Sprintf("%s%s\n", icon, m.Content) - return strings.ReplaceAll(textMsg, "\n\n", "\n") + m.ContentParts = append(m.ContentParts, imagePart) +} + +// CreateImageURLFromPath creates a data URL from an image file path +func CreateImageURLFromPath(imagePath string) (string, error) { + // Read the image file + data, err := os.ReadFile(imagePath) + if err != nil { + return "", err + } + // Determine the image format based on file extension + var mimeType string + switch { + case strings.HasSuffix(strings.ToLower(imagePath), ".png"): + mimeType = "image/png" + case strings.HasSuffix(strings.ToLower(imagePath), ".jpg"): + fallthrough + case strings.HasSuffix(strings.ToLower(imagePath), ".jpeg"): + mimeType = "image/jpeg" + case strings.HasSuffix(strings.ToLower(imagePath), ".gif"): + mimeType = "image/gif" + case strings.HasSuffix(strings.ToLower(imagePath), ".webp"): + mimeType = "image/webp" + default: + mimeType = "image/jpeg" // default + } + // Encode to base64 + encoded := base64.StdEncoding.EncodeToString(data) + // Create data URL + return fmt.Sprintf("data:%s;base64,%s", mimeType, encoded), nil } type ChatBody struct { - Model string `json:"model"` - Stream bool `json:"stream"` - Messages []MessagesStory `json:"messages"` -} - -type ChatToolsBody struct { - Model string `json:"model"` - Messages []MessagesStory `json:"messages"` - Tools []struct { - Type string `json:"type"` - Function struct { - Name string `json:"name"` - Description string `json:"description"` - Parameters struct { - Type string `json:"type"` - Properties struct { - Location struct { - Type string `json:"type"` - Description string `json:"description"` - } `json:"location"` - Unit struct { - Type string `json:"type"` - Enum []string `json:"enum"` - } `json:"unit"` - } `json:"properties"` - Required []string `json:"required"` - } `json:"parameters"` - } `json:"function"` - } `json:"tools"` - ToolChoice string `json:"tool_choice"` + Model string `json:"model"` + Stream bool `json:"stream"` + Messages []RoleMsg `json:"messages"` +} + +func (cb *ChatBody) Rename(oldname, newname string) { + for i := range cb.Messages { + cb.Messages[i].Content = strings.ReplaceAll(cb.Messages[i].Content, oldname, newname) + cb.Messages[i].Role = strings.ReplaceAll(cb.Messages[i].Role, oldname, newname) + } +} + +func (cb *ChatBody) ListRoles() []string { + namesMap := make(map[string]struct{}) + for i := range cb.Messages { + namesMap[cb.Messages[i].Role] = struct{}{} + } + resp := make([]string, len(namesMap)) + i := 0 + for k := range namesMap { + resp[i] = k + i++ + } + return resp +} + +func (cb *ChatBody) MakeStopSlice() []string { + return cb.MakeStopSliceExcluding("", cb.ListRoles()) +} + +func (cb *ChatBody) MakeStopSliceExcluding( + excludeRole string, roleList []string, +) []string { + ss := []string{} + for _, role := range roleList { + // Skip the excluded role (typically the current speaker) + if role == excludeRole { + continue + } + // Add multiple variations to catch different formatting + ss = append(ss, + role+":\n", // Most common: role with newline + role+":", // Role with colon but no newline + role+": ", // Role with colon and single space + role+": ", // Role with colon and double space (common tokenization) + role+": \n", // Role with colon and double space (common tokenization) + role+": ", // Role with colon and triple space + ) + } + return ss +} + +type EmbeddingResp struct { + Embedding []float32 `json:"embedding"` + Index uint32 `json:"index"` +} + +// type EmbeddingsResp struct { +// Model string `json:"model"` +// Object string `json:"object"` +// Usage struct { +// PromptTokens int `json:"prompt_tokens"` +// TotalTokens int `json:"total_tokens"` +// } `json:"usage"` +// Data []struct { +// Embedding []float32 `json:"embedding"` +// Index int `json:"index"` +// Object string `json:"object"` +// } `json:"data"` +// } + +// === tools models + +type ToolArgProps struct { + Type string `json:"type"` + Description string `json:"description"` +} + +type ToolFuncParams struct { + Type string `json:"type"` + Properties map[string]ToolArgProps `json:"properties"` + Required []string `json:"required"` +} + +type ToolFunc struct { + Name string `json:"name"` + Description string `json:"description"` + Parameters ToolFuncParams `json:"parameters"` +} + +type Tool struct { + Type string `json:"type"` + Function ToolFunc `json:"function"` +} + +type OpenAIReq struct { + *ChatBody + Tools []Tool `json:"tools"` +} + +// === + +type LlamaCPPReq struct { + Model string `json:"model"` + Stream bool `json:"stream"` + // For multimodal requests, prompt should be an object with prompt_string and multimodal_data + // For regular requests, prompt is a string + Prompt any `json:"prompt"` // Can be string or object with prompt_string and multimodal_data + Temperature float32 `json:"temperature"` + DryMultiplier float32 `json:"dry_multiplier"` + Stop []string `json:"stop"` + MinP float32 `json:"min_p"` + NPredict int32 `json:"n_predict"` + // MaxTokens int `json:"max_tokens"` + // DryBase float64 `json:"dry_base"` + // DryAllowedLength int `json:"dry_allowed_length"` + // DryPenaltyLastN int `json:"dry_penalty_last_n"` + // CachePrompt bool `json:"cache_prompt"` + // DynatempRange int `json:"dynatemp_range"` + // DynatempExponent int `json:"dynatemp_exponent"` + // TopK int `json:"top_k"` + // TopP float32 `json:"top_p"` + // TypicalP int `json:"typical_p"` + // XtcProbability int `json:"xtc_probability"` + // XtcThreshold float32 `json:"xtc_threshold"` + // RepeatLastN int `json:"repeat_last_n"` + // RepeatPenalty int `json:"repeat_penalty"` + // PresencePenalty int `json:"presence_penalty"` + // FrequencyPenalty int `json:"frequency_penalty"` + // Samplers string `json:"samplers"` +} + +type PromptObject struct { + PromptString string `json:"prompt_string"` + MultimodalData []string `json:"multimodal_data,omitempty"` + // Alternative field name used by some llama.cpp implementations + ImageData []string `json:"image_data,omitempty"` // For compatibility +} + +func NewLCPReq(prompt, model string, multimodalData []string, props map[string]float32, stopStrings []string) LlamaCPPReq { + var finalPrompt any + if len(multimodalData) > 0 { + // When multimodal data is present, use the object format as per Python example: + // { "prompt": { "prompt_string": "...", "multimodal_data": [...] } } + finalPrompt = PromptObject{ + PromptString: prompt, + MultimodalData: multimodalData, + ImageData: multimodalData, // Also populate for compatibility with different llama.cpp versions + } + } else { + // When no multimodal data, use plain string + finalPrompt = prompt + } + return LlamaCPPReq{ + Model: model, + Stream: true, + Prompt: finalPrompt, + Temperature: props["temperature"], + DryMultiplier: props["dry_multiplier"], + Stop: stopStrings, + MinP: props["min_p"], + NPredict: int32(props["n_predict"]), + } +} + +type LlamaCPPResp struct { + Content string `json:"content"` + Stop bool `json:"stop"` +} + +type LCPModels struct { + Data []struct { + ID string `json:"id"` + Object string `json:"object"` + OwnedBy string `json:"owned_by"` + Created int `json:"created"` + InCache bool `json:"in_cache"` + Path string `json:"path"` + Status struct { + Value string `json:"value"` + Args []string `json:"args"` + } `json:"status"` + } `json:"data"` + Object string `json:"object"` +} + +func (lcp *LCPModels) ListModels() []string { + resp := make([]string, 0, len(lcp.Data)) + for _, model := range lcp.Data { + resp = append(resp, model.ID) + } + return resp +} + +func (lcp *LCPModels) HasVision(modelID string) bool { + for _, m := range lcp.Data { + if m.ID == modelID { + args := m.Status.Args + for i := 0; i < len(args)-1; i++ { + if args[i] == "--mmproj" { + return true + } + } + } + } + return false +} + +type ResponseStats struct { + Tokens int + Duration float64 + TokensPerSec float64 +} + +type ChatRoundReq struct { + UserMsg string + Role string + Regen bool + Resume bool +} + +type MultimodalToolResp struct { + Type string `json:"type"` + Parts []map[string]string `json:"parts"` } diff --git a/models/openrouter.go b/models/openrouter.go new file mode 100644 index 0000000..2dd49cc --- /dev/null +++ b/models/openrouter.go @@ -0,0 +1,187 @@ +package models + +// openrouter +// https://openrouter.ai/docs/api-reference/completion +type OpenRouterCompletionReq struct { + Model string `json:"model"` + Prompt string `json:"prompt"` + Stream bool `json:"stream"` + Temperature float32 `json:"temperature"` + Stop []string `json:"stop"` // not present in docs + MinP float32 `json:"min_p"` + NPredict int32 `json:"max_tokens"` +} + +func NewOpenRouterCompletionReq(model, prompt string, props map[string]float32, stopStrings []string) OpenRouterCompletionReq { + return OpenRouterCompletionReq{ + Stream: true, + Prompt: prompt, + Temperature: props["temperature"], + MinP: props["min_p"], + NPredict: int32(props["n_predict"]), + Stop: stopStrings, + Model: model, + } +} + +type OpenRouterChatReq struct { + Messages []RoleMsg `json:"messages"` + Model string `json:"model"` + Stream bool `json:"stream"` + Temperature float32 `json:"temperature"` + MinP float32 `json:"min_p"` + NPredict int32 `json:"max_tokens"` + Tools []Tool `json:"tools"` + Reasoning *ReasoningConfig `json:"reasoning,omitempty"` +} + +type ReasoningConfig struct { + Effort string `json:"effort,omitempty"` // xhigh, high, medium, low, minimal, none + Summary string `json:"summary,omitempty"` // auto, concise, detailed +} + +func NewOpenRouterChatReq(cb ChatBody, props map[string]float32, reasoningEffort string) OpenRouterChatReq { + req := OpenRouterChatReq{ + Messages: cb.Messages, + Model: cb.Model, + Stream: cb.Stream, + Temperature: props["temperature"], + MinP: props["min_p"], + NPredict: int32(props["n_predict"]), + } + // Only include reasoning config if effort is specified and not "none" + if reasoningEffort != "" && reasoningEffort != "none" { + req.Reasoning = &ReasoningConfig{ + Effort: reasoningEffort, + } + } + return req +} + +type OpenRouterChatRespNonStream struct { + ID string `json:"id"` + Provider string `json:"provider"` + Model string `json:"model"` + Object string `json:"object"` + Created int `json:"created"` + Choices []struct { + Logprobs any `json:"logprobs"` + FinishReason string `json:"finish_reason"` + NativeFinishReason string `json:"native_finish_reason"` + Index int `json:"index"` + Message struct { + Role string `json:"role"` + Content string `json:"content"` + Refusal any `json:"refusal"` + Reasoning any `json:"reasoning"` + ToolCalls []ToolDeltaResp `json:"tool_calls"` + } `json:"message"` + } `json:"choices"` + Usage struct { + PromptTokens int `json:"prompt_tokens"` + CompletionTokens int `json:"completion_tokens"` + TotalTokens int `json:"total_tokens"` + } `json:"usage"` +} + +type OpenRouterChatResp struct { + ID string `json:"id"` + Provider string `json:"provider"` + Model string `json:"model"` + Object string `json:"object"` + Created int `json:"created"` + Choices []struct { + Index int `json:"index"` + Delta struct { + Role string `json:"role"` + Content string `json:"content"` + Reasoning string `json:"reasoning"` + ToolCalls []ToolDeltaResp `json:"tool_calls"` + } `json:"delta"` + FinishReason string `json:"finish_reason"` + NativeFinishReason string `json:"native_finish_reason"` + Logprobs any `json:"logprobs"` + } `json:"choices"` +} + +type OpenRouterCompletionResp struct { + ID string `json:"id"` + Provider string `json:"provider"` + Model string `json:"model"` + Object string `json:"object"` + Created int `json:"created"` + Choices []struct { + Text string `json:"text"` + FinishReason string `json:"finish_reason"` + NativeFinishReason string `json:"native_finish_reason"` + Logprobs any `json:"logprobs"` + } `json:"choices"` +} + +type ORModel struct { + ID string `json:"id"` + CanonicalSlug string `json:"canonical_slug"` + HuggingFaceID string `json:"hugging_face_id"` + Name string `json:"name"` + Created int `json:"created"` + Description string `json:"description"` + ContextLength int `json:"context_length"` + Architecture struct { + Modality string `json:"modality"` + InputModalities []string `json:"input_modalities"` + OutputModalities []string `json:"output_modalities"` + Tokenizer string `json:"tokenizer"` + InstructType any `json:"instruct_type"` + } `json:"architecture"` + Pricing struct { + Prompt string `json:"prompt"` + Completion string `json:"completion"` + Request string `json:"request"` + Image string `json:"image"` + Audio string `json:"audio"` + WebSearch string `json:"web_search"` + InternalReasoning string `json:"internal_reasoning"` + } `json:"pricing,omitempty"` + TopProvider struct { + ContextLength int `json:"context_length"` + MaxCompletionTokens int `json:"max_completion_tokens"` + IsModerated bool `json:"is_moderated"` + } `json:"top_provider"` + PerRequestLimits any `json:"per_request_limits"` + SupportedParameters []string `json:"supported_parameters"` +} + +type ORModels struct { + Data []ORModel `json:"data"` +} + +func (orm *ORModels) ListModels(free bool) []string { + resp := []string{} + for i := range orm.Data { + model := &orm.Data[i] // Take address of element to avoid copying + if free { + if model.Pricing.Prompt == "0" && model.Pricing.Completion == "0" { + // treat missing request as free + if model.Pricing.Request == "" || model.Pricing.Request == "0" { + resp = append(resp, model.ID) + } + } + } else { + resp = append(resp, model.ID) + } + } + return resp +} + +func (orm *ORModels) HasVision(modelID string) bool { + for i := range orm.Data { + if orm.Data[i].ID == modelID { + for _, mod := range orm.Data[i].Architecture.InputModalities { + if mod == "image" { + return true + } + } + } + } + return false +} diff --git a/models/openrouter_test.go b/models/openrouter_test.go new file mode 100644 index 0000000..63990b6 --- /dev/null +++ b/models/openrouter_test.go @@ -0,0 +1,96 @@ +package models + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" +) + +func TestORModelsListModels(t *testing.T) { + t.Run("unit test with hardcoded data", func(t *testing.T) { + jsonData := `{ + "data": [ + { + "id": "model/free", + "pricing": { + "prompt": "0", + "completion": "0" + } + }, + { + "id": "model/paid", + "pricing": { + "prompt": "0.001", + "completion": "0.002" + } + }, + { + "id": "model/request-zero", + "pricing": { + "prompt": "0", + "completion": "0", + "request": "0" + } + }, + { + "id": "model/request-nonzero", + "pricing": { + "prompt": "0", + "completion": "0", + "request": "0.5" + } + } + ] + }` + var models ORModels + if err := json.Unmarshal([]byte(jsonData), &models); err != nil { + t.Fatalf("failed to unmarshal test data: %v", err) + } + freeModels := models.ListModels(true) + if len(freeModels) != 2 { + t.Errorf("expected 2 free models, got %d: %v", len(freeModels), freeModels) + } + expectedFree := map[string]bool{"model/free": true, "model/request-zero": true} + for _, id := range freeModels { + if !expectedFree[id] { + t.Errorf("unexpected free model ID: %s", id) + } + } + allModels := models.ListModels(false) + if len(allModels) != 4 { + t.Errorf("expected 4 total models, got %d", len(allModels)) + } + }) + t.Run("integration with or_models.json", func(t *testing.T) { + // Attempt to load the real data file from the project root + path := filepath.Join("..", "or_models.json") + data, err := os.ReadFile(path) + if err != nil { + t.Skip("or_models.json not found, skipping integration test") + } + var models ORModels + if err := json.Unmarshal(data, &models); err != nil { + t.Fatalf("failed to unmarshal %s: %v", path, err) + } + freeModels := models.ListModels(true) + if len(freeModels) == 0 { + t.Error("expected at least one free model, got none") + } + allModels := models.ListModels(false) + if len(allModels) == 0 { + t.Error("expected at least one model") + } + // Ensure free models are subset of all models + freeSet := make(map[string]bool) + for _, id := range freeModels { + freeSet[id] = true + } + for _, id := range freeModels { + if !freeSet[id] { + t.Errorf("free model %s not found in all models", id) + } + } + t.Logf("found %d free models out of %d total models", len(freeModels), len(allModels)) + }) +}
\ No newline at end of file diff --git a/noextra.go b/noextra.go new file mode 100644 index 0000000..91c2b45 --- /dev/null +++ b/noextra.go @@ -0,0 +1,72 @@ +//go:build !extra + +package main + +import ( + "gf-lt/config" + "log/slog" +) + +// Interfaces and implementations when extra modules are not included + +type Orator interface { + Speak(text string) error + Stop() + GetLogger() *slog.Logger +} + +type STT interface { + StartRecording() error + StopRecording() (string, error) + IsRecording() bool +} + +// DefaultOrator is a no-op implementation when TTS is not available +type DefaultOrator struct { + logger *slog.Logger +} + +func NewOrator(logger *slog.Logger, cfg *config.Config) Orator { + return &DefaultOrator{logger: logger} +} + +func (d *DefaultOrator) Speak(text string) error { + d.logger.Debug("TTS not available - extra modules disabled") + return nil +} + +func (d *DefaultOrator) Stop() { + // No-op +} + +func (d *DefaultOrator) GetLogger() *slog.Logger { + return d.logger +} + +// DefaultSTT is a no-op implementation when STT is not available +type DefaultSTT struct { + logger *slog.Logger +} + +func NewSTT(logger *slog.Logger, cfg *config.Config) STT { + return &DefaultSTT{logger: logger} +} + +func (d *DefaultSTT) StartRecording() error { + d.logger.Debug("STT not available - extra modules disabled") + return nil +} + +func (d *DefaultSTT) StopRecording() (string, error) { + d.logger.Debug("STT not available - extra modules disabled") + return "", nil +} + +func (d *DefaultSTT) IsRecording() bool { + return false +} + +// TTS channels - no-op when extra is not available +var TTSTextChan = make(chan string, 10000) +var TTSFlushChan = make(chan bool, 1) +var TTSDoneChan = make(chan bool, 1)
\ No newline at end of file diff --git a/pngmeta/altwriter.go b/pngmeta/altwriter.go new file mode 100644 index 0000000..76cb709 --- /dev/null +++ b/pngmeta/altwriter.go @@ -0,0 +1,133 @@ +package pngmeta + +import ( + "bytes" + "gf-lt/models" + "encoding/base64" + "encoding/binary" + "encoding/json" + "errors" + "fmt" + "hash/crc32" + "io" + "os" +) + +const ( + pngHeader = "\x89PNG\r\n\x1a\n" + textChunkType = "tEXt" +) + +// WriteToPng embeds the metadata into the specified PNG file and writes the result to outfile. +func WriteToPng(metadata *models.CharCardSpec, sourcePath, outfile string) error { + pngData, err := os.ReadFile(sourcePath) + if err != nil { + return err + } + jsonData, err := json.Marshal(metadata) + if err != nil { + return err + } + base64Data := base64.StdEncoding.EncodeToString(jsonData) + embedData := PngEmbed{ + Key: "gf-lt", // Replace with appropriate key constant + Value: base64Data, + } + var outputBuffer bytes.Buffer + if _, err := outputBuffer.Write([]byte(pngHeader)); err != nil { + return err + } + chunks, iend, err := processChunks(pngData[8:]) + if err != nil { + return err + } + for _, chunk := range chunks { + outputBuffer.Write(chunk) + } + newChunk, err := createTextChunk(embedData) + if err != nil { + return err + } + outputBuffer.Write(newChunk) + outputBuffer.Write(iend) + return os.WriteFile(outfile, outputBuffer.Bytes(), 0666) +} + +// processChunks extracts non-tEXt chunks and locates the IEND chunk +func processChunks(data []byte) ([][]byte, []byte, error) { + var ( + chunks [][]byte + iendChunk []byte + reader = bytes.NewReader(data) + ) + for { + var chunkLength uint32 + if err := binary.Read(reader, binary.BigEndian, &chunkLength); err != nil { + if errors.Is(err, io.EOF) { + break + } + return nil, nil, fmt.Errorf("error reading chunk length: %w", err) + } + chunkType := make([]byte, 4) + if _, err := reader.Read(chunkType); err != nil { + return nil, nil, fmt.Errorf("error reading chunk type: %w", err) + } + chunkData := make([]byte, chunkLength) + if _, err := reader.Read(chunkData); err != nil { + return nil, nil, fmt.Errorf("error reading chunk data: %w", err) + } + crc := make([]byte, 4) + if _, err := reader.Read(crc); err != nil { + return nil, nil, fmt.Errorf("error reading CRC: %w", err) + } + fullChunk := bytes.NewBuffer(nil) + if err := binary.Write(fullChunk, binary.BigEndian, chunkLength); err != nil { + return nil, nil, fmt.Errorf("error writing chunk length: %w", err) + } + if _, err := fullChunk.Write(chunkType); err != nil { + return nil, nil, fmt.Errorf("error writing chunk type: %w", err) + } + if _, err := fullChunk.Write(chunkData); err != nil { + return nil, nil, fmt.Errorf("error writing chunk data: %w", err) + } + if _, err := fullChunk.Write(crc); err != nil { + return nil, nil, fmt.Errorf("error writing CRC: %w", err) + } + switch string(chunkType) { + case "IEND": + iendChunk = fullChunk.Bytes() + return chunks, iendChunk, nil + case textChunkType: + continue // Skip existing tEXt chunks + default: + chunks = append(chunks, fullChunk.Bytes()) + } + } + return nil, nil, errors.New("IEND chunk not found") +} + +// createTextChunk generates a valid tEXt chunk with proper CRC +func createTextChunk(embed PngEmbed) ([]byte, error) { + content := bytes.NewBuffer(nil) + content.WriteString(embed.Key) + content.WriteByte(0) // Null separator + content.WriteString(embed.Value) + data := content.Bytes() + crc := crc32.NewIEEE() + crc.Write([]byte(textChunkType)) + crc.Write(data) + chunk := bytes.NewBuffer(nil) + if err := binary.Write(chunk, binary.BigEndian, uint32(len(data))); err != nil { + return nil, fmt.Errorf("error writing chunk length: %w", err) + } + if _, err := chunk.WriteString(textChunkType); err != nil { + return nil, fmt.Errorf("error writing chunk type: %w", err) + } + if _, err := chunk.Write(data); err != nil { + return nil, fmt.Errorf("error writing chunk data: %w", err) + } + if err := binary.Write(chunk, binary.BigEndian, crc.Sum32()); err != nil { + return nil, fmt.Errorf("error writing CRC: %w", err) + } + return chunk.Bytes(), nil +} diff --git a/pngmeta/metareader.go b/pngmeta/metareader.go new file mode 100644 index 0000000..e1835f9 --- /dev/null +++ b/pngmeta/metareader.go @@ -0,0 +1,153 @@ +package pngmeta + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "gf-lt/models" + "io" + "log/slog" + "os" + "path" + "strings" +) + +const ( + embType = "tEXt" + cKey = "chara" + IEND = "IEND" + header = "\x89PNG\r\n\x1a\n" + writeHeader = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" +) + +type PngEmbed struct { + Key string + Value string +} + +func (c PngEmbed) GetDecodedValue() (*models.CharCardSpec, error) { + data, err := base64.StdEncoding.DecodeString(c.Value) + if err != nil { + return nil, err + } + card := &models.CharCardSpec{} + if err := json.Unmarshal(data, &card); err != nil { + return nil, err + } + specWrap := &models.Spec2Wrapper{} + if card.Name == "" { + if err := json.Unmarshal(data, &specWrap); err != nil { + return nil, err + } + return &specWrap.Data, nil + } + return card, nil +} + +func extractChar(fname string) (*PngEmbed, error) { + data, err := os.ReadFile(fname) + if err != nil { + return nil, err + } + reader := bytes.NewReader(data) + pr, err := NewPNGStepReader(reader) + if err != nil { + return nil, err + } + for { + step, err := pr.Next() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + } + if step.Type() != embType { + if _, err := io.Copy(io.Discard, step); err != nil { + return nil, err + } + } else { + buf, err := io.ReadAll(step) + if err != nil { + return nil, err + } + dataInstep := string(buf) + values := strings.Split(dataInstep, "\x00") + if len(values) == 2 { + return &PngEmbed{Key: values[0], Value: values[1]}, nil + } + } + if err := step.Close(); err != nil { + return nil, err + } + } + return nil, errors.New("failed to find embedded char in png: " + fname) +} + +func ReadCard(fname, uname string) (*models.CharCard, error) { + pe, err := extractChar(fname) + if err != nil { + return nil, err + } + charSpec, err := pe.GetDecodedValue() + if err != nil { + return nil, err + } + if charSpec.Name == "" { + return nil, fmt.Errorf("failed to find role; fname %s", fname) + } + return charSpec.Simplify(uname, fname), nil +} + +func ReadCardJson(fname string) (*models.CharCard, error) { + data, err := os.ReadFile(fname) + if err != nil { + return nil, err + } + card := models.CharCard{} + if err := json.Unmarshal(data, &card); err != nil { + return nil, err + } + if card.FilePath == "" { + card.FilePath = fname + } + if card.ID == "" { + card.ID = models.ComputeCardID(card.Role, card.FilePath) + } + return &card, nil +} + +func ReadDirCards(dirname, uname string, log *slog.Logger) ([]*models.CharCard, error) { + files, err := os.ReadDir(dirname) + if err != nil { + return nil, err + } + resp := []*models.CharCard{} + for _, f := range files { + if f.IsDir() { + continue + } + if strings.HasSuffix(f.Name(), ".png") { + fpath := path.Join(dirname, f.Name()) + cc, err := ReadCard(fpath, uname) + if err != nil { + log.Warn("failed to load card", "error", err, "card", fpath) + continue + } + resp = append(resp, cc) + } + if strings.HasSuffix(f.Name(), ".json") { + fpath := path.Join(dirname, f.Name()) + cc, err := ReadCardJson(fpath) + if err != nil { + log.Warn("failed to load card", "error", err, "card", fpath) + continue + } + cc.FirstMsg = strings.ReplaceAll(strings.ReplaceAll(cc.FirstMsg, "{{char}}", cc.Role), "{{user}}", uname) + cc.SysPrompt = strings.ReplaceAll(strings.ReplaceAll(cc.SysPrompt, "{{char}}", cc.Role), "{{user}}", uname) + resp = append(resp, cc) + } + } + return resp, nil +} diff --git a/pngmeta/metareader_test.go b/pngmeta/metareader_test.go new file mode 100644 index 0000000..f88de06 --- /dev/null +++ b/pngmeta/metareader_test.go @@ -0,0 +1,194 @@ +package pngmeta + +import ( + "bytes" + "gf-lt/models" + "encoding/base64" + "encoding/binary" + "encoding/json" + "errors" + "fmt" + "image" + "image/color" + "image/png" + "io" + "os" + "path/filepath" + "testing" +) + +func TestReadMeta(t *testing.T) { + cases := []struct { + Filename string + }{ + { + Filename: "../sysprompts/llama.png", + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) { + // Call the readMeta function + pembed, err := extractChar(tc.Filename) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + v, err := pembed.GetDecodedValue() + if err != nil { + t.Errorf("Expected no error, but got %v\n", err) + } + fmt.Printf("%+v\n", v.Simplify("Adam", tc.Filename)) + }) + } +} + +// Test helper: Create a simple PNG image with test shapes +func createTestImage(t *testing.T) string { + img := image.NewRGBA(image.Rect(0, 0, 200, 200)) + // Fill background with white + for y := 0; y < 200; y++ { + for x := 0; x < 200; x++ { + img.Set(x, y, color.White) + } + } + // Draw a red square + for y := 50; y < 150; y++ { + for x := 50; x < 150; x++ { + img.Set(x, y, color.RGBA{R: 255, A: 255}) + } + } + // Draw a blue circle + center := image.Point{100, 100} + radius := 40 + for y := center.Y - radius; y <= center.Y+radius; y++ { + for x := center.X - radius; x <= center.X+radius; x++ { + dx := x - center.X + dy := y - center.Y + if dx*dx+dy*dy <= radius*radius { + img.Set(x, y, color.RGBA{B: 255, A: 255}) + } + } + } + // Create temp file + tmpDir := t.TempDir() + fpath := filepath.Join(tmpDir, "test-image.png") + f, err := os.Create(fpath) + if err != nil { + t.Fatalf("Error creating temp file: %v", err) + } + defer f.Close() + if err := png.Encode(f, img); err != nil { + t.Fatalf("Error encoding PNG: %v", err) + } + return fpath +} + +func TestWriteToPng(t *testing.T) { + // Create test image + srcPath := createTestImage(t) + dstPath := filepath.Join(filepath.Dir(srcPath), "output.png") + // dstPath := "test.png" + // Create test metadata + metadata := &models.CharCardSpec{ + Description: "Test image containing a red square and blue circle on white background", + } + // Embed metadata + if err := WriteToPng(metadata, srcPath, dstPath); err != nil { + t.Fatalf("WriteToPng failed: %v", err) + } + // Verify output file exists + if _, err := os.Stat(dstPath); os.IsNotExist(err) { + t.Fatalf("Output file not created: %v", err) + } + // Read and verify metadata + t.Run("VerifyMetadata", func(t *testing.T) { + data, err := os.ReadFile(dstPath) + if err != nil { + t.Fatalf("Error reading output file: %v", err) + } + // Verify PNG header + if string(data[:8]) != pngHeader { + t.Errorf("Invalid PNG header") + } + // Extract metadata + embedded := extractMetadata(t, data) + if embedded.Description != metadata.Description { + t.Errorf("Metadata mismatch\nWant: %q\nGot: %q", + metadata.Description, embedded.Description) + } + }) + // Optional: Add cleanup if needed + // t.Cleanup(func() { + // os.Remove(dstPath) + // }) +} + +// Helper to extract embedded metadata from PNG bytes +func extractMetadata(t *testing.T, data []byte) *models.CharCardSpec { + r := bytes.NewReader(data[8:]) // Skip PNG header + for { + var length uint32 + if err := binary.Read(r, binary.BigEndian, &length); err != nil { + if errors.Is(err, io.EOF) { + break + } + t.Fatalf("Error reading chunk length: %v", err) + } + chunkType := make([]byte, 4) + if _, err := r.Read(chunkType); err != nil { + t.Fatalf("Error reading chunk type: %v", err) + } + // Read chunk data + chunkData := make([]byte, length) + if _, err := r.Read(chunkData); err != nil { + t.Fatalf("Error reading chunk data: %v", err) + } + // Read and discard CRC + if _, err := r.Read(make([]byte, 4)); err != nil { + t.Fatalf("Error reading CRC: %v", err) + } + if string(chunkType) == embType { + parts := bytes.SplitN(chunkData, []byte{0}, 2) + if len(parts) != 2 { + t.Fatalf("Invalid tEXt chunk format") + } + decoded, err := base64.StdEncoding.DecodeString(string(parts[1])) + if err != nil { + t.Fatalf("Base64 decode error: %v", err) + } + var result models.CharCardSpec + if err := json.Unmarshal(decoded, &result); err != nil { + t.Fatalf("JSON unmarshal error: %v", err) + } + return &result + } + } + t.Fatal("Metadata not found in PNG") + return nil +} + +func readTextChunk(t *testing.T, r io.ReadSeeker) *models.CharCardSpec { + var length uint32 + binary.Read(r, binary.BigEndian, &length) + chunkType := make([]byte, 4) + r.Read(chunkType) + data := make([]byte, length) + r.Read(data) + // Read CRC (but skip validation for test purposes) + crc := make([]byte, 4) + r.Read(crc) + parts := bytes.SplitN(data, []byte{0}, 2) // Split key-value pair + if len(parts) != 2 { + t.Fatalf("Invalid tEXt chunk format") + } + // key := string(parts[0]) + value := parts[1] + decoded, err := base64.StdEncoding.DecodeString(string(value)) + if err != nil { + t.Fatalf("Base64 decode error: %v; value: %s", err, string(value)) + } + var result models.CharCardSpec + if err := json.Unmarshal(decoded, &result); err != nil { + t.Fatalf("JSON unmarshal error: %v", err) + } + return &result +} diff --git a/pngmeta/partsreader.go b/pngmeta/partsreader.go new file mode 100644 index 0000000..d345a16 --- /dev/null +++ b/pngmeta/partsreader.go @@ -0,0 +1,75 @@ +package pngmeta + +import ( + "encoding/binary" + "errors" + "hash" + "hash/crc32" + "io" +) + +var ( + ErrCRC32Mismatch = errors.New("crc32 mismatch") + ErrNotPNG = errors.New("not png") + ErrBadLength = errors.New("bad length") +) + +type PngChunk struct { + typ string + length int32 + r io.Reader + realR io.Reader + checksummer hash.Hash32 +} + +func (c *PngChunk) Read(p []byte) (int, error) { + return io.TeeReader(c.r, c.checksummer).Read(p) +} + +func (c *PngChunk) Close() error { + var crc32 uint32 + if err := binary.Read(c.realR, binary.BigEndian, &crc32); err != nil { + return err + } + if crc32 != c.checksummer.Sum32() { + return ErrCRC32Mismatch + } + return nil +} + +func (c *PngChunk) Type() string { + return c.typ +} + +type Reader struct { + r io.Reader +} + +func NewPNGStepReader(r io.Reader) (*Reader, error) { + expectedHeader := make([]byte, len(header)) + if _, err := io.ReadFull(r, expectedHeader); err != nil { + return nil, err + } + if string(expectedHeader) != header { + return nil, ErrNotPNG + } + return &Reader{r}, nil +} + +func (r *Reader) Next() (*PngChunk, error) { + var length int32 + if err := binary.Read(r.r, binary.BigEndian, &length); err != nil { + return nil, err + } + if length < 0 { + return nil, ErrBadLength + } + var rawTyp [4]byte + if _, err := io.ReadFull(r.r, rawTyp[:]); err != nil { + return nil, err + } + typ := string(rawTyp[:]) + checksummer := crc32.NewIEEE() + checksummer.Write([]byte(typ)) + return &PngChunk{typ, length, io.LimitReader(r.r, int64(length)), r.r, checksummer}, nil +} diff --git a/pngmeta/partswriter.go b/pngmeta/partswriter.go new file mode 100644 index 0000000..7282df6 --- /dev/null +++ b/pngmeta/partswriter.go @@ -0,0 +1,112 @@ +package pngmeta + +// import ( +// "bytes" +// "encoding/binary" +// "errors" +// "fmt" +// "hash/crc32" +// "io" +// ) + +// type Writer struct { +// w io.Writer +// } + +// func NewPNGWriter(w io.Writer) (*Writer, error) { +// if _, err := io.WriteString(w, writeHeader); err != nil { +// return nil, err +// } +// return &Writer{w}, nil +// } + +// func (w *Writer) WriteChunk(length int32, typ string, r io.Reader) error { +// if err := binary.Write(w.w, binary.BigEndian, length); err != nil { +// return err +// } +// if _, err := w.w.Write([]byte(typ)); err != nil { +// return err +// } +// checksummer := crc32.NewIEEE() +// checksummer.Write([]byte(typ)) +// if _, err := io.CopyN(io.MultiWriter(w.w, checksummer), r, int64(length)); err != nil { +// return err +// } +// if err := binary.Write(w.w, binary.BigEndian, checksummer.Sum32()); err != nil { +// return err +// } +// return nil +// } + +// func WWriteToPngriteToPng(c *models.CharCardSpec, fpath, outfile string) error { +// data, err := os.ReadFile(fpath) +// if err != nil { +// return err +// } +// jsonData, err := json.Marshal(c) +// if err != nil { +// return err +// } +// // Base64 encode the JSON data +// base64Data := base64.StdEncoding.EncodeToString(jsonData) +// pe := PngEmbed{ +// Key: cKey, +// Value: base64Data, +// } +// w, err := WritetEXtToPngBytes(data, pe) +// if err != nil { +// return err +// } +// return os.WriteFile(outfile, w.Bytes(), 0666) +// } + +// func WritetEXtToPngBytes(inputBytes []byte, pe PngEmbed) (outputBytes bytes.Buffer, err error) { +// if !(string(inputBytes[:8]) == header) { +// return outputBytes, errors.New("wrong file format") +// } +// reader := bytes.NewReader(inputBytes) +// pngr, err := NewPNGStepReader(reader) +// if err != nil { +// return outputBytes, fmt.Errorf("NewReader(): %s", err) +// } +// pngw, err := NewPNGWriter(&outputBytes) +// if err != nil { +// return outputBytes, fmt.Errorf("NewWriter(): %s", err) +// } +// for { +// chunk, err := pngr.Next() +// if err != nil { +// if errors.Is(err, io.EOF) { +// break +// } +// return outputBytes, fmt.Errorf("NextChunk(): %s", err) +// } +// if chunk.Type() != embType { +// // IENDChunkType will only appear on the final iteration of a valid PNG +// if chunk.Type() == IEND { +// // This is where we inject tEXtChunkType as the penultimate chunk with the new value +// newtEXtChunk := []byte(fmt.Sprintf(tEXtChunkDataSpecification, pe.Key, pe.Value)) +// if err := pngw.WriteChunk(int32(len(newtEXtChunk)), embType, bytes.NewBuffer(newtEXtChunk)); err != nil { +// return outputBytes, fmt.Errorf("WriteChunk(): %s", err) +// } +// // Now we end the buffer with IENDChunkType chunk +// if err := pngw.WriteChunk(chunk.length, chunk.Type(), chunk); err != nil { +// return outputBytes, fmt.Errorf("WriteChunk(): %s", err) +// } +// } else { +// // writes back original chunk to buffer +// if err := pngw.WriteChunk(chunk.length, chunk.Type(), chunk); err != nil { +// return outputBytes, fmt.Errorf("WriteChunk(): %s", err) +// } +// } +// } else { +// if _, err := io.Copy(io.Discard, chunk); err != nil { +// return outputBytes, fmt.Errorf("io.Copy(io.Discard, chunk): %s", err) +// } +// } +// if err := chunk.Close(); err != nil { +// return outputBytes, fmt.Errorf("chunk.Close(): %s", err) +// } +// } +// return outputBytes, nil +// } diff --git a/popups.go b/popups.go new file mode 100644 index 0000000..38f42cd --- /dev/null +++ b/popups.go @@ -0,0 +1,571 @@ +package main + +import ( + "gf-lt/models" + "slices" + "strings" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +// showModelSelectionPopup creates a modal popup to select a model +func showModelSelectionPopup() { + // Helper function to get model list for a given API + getModelListForAPI := func(api string) []string { + if strings.Contains(api, "api.deepseek.com/") { + return []string{"deepseek-chat", "deepseek-reasoner"} + } else if strings.Contains(api, "openrouter.ai") { + return ORFreeModels + } + // Assume local llama.cpp - fetch with load status + models, err := fetchLCPModelsWithLoadStatus() + if err != nil { + logger.Error("failed to fetch models with load status", "error", err) + return LocalModels + } + return models + } + // Get the current model list based on the API + modelList := getModelListForAPI(cfg.CurrentAPI) + // Check for empty options list + if len(modelList) == 0 { + logger.Warn("empty model list for", "api", cfg.CurrentAPI, "localModelsLen", len(LocalModels), "orModelsLen", len(ORFreeModels)) + var message string + switch { + case strings.Contains(cfg.CurrentAPI, "openrouter.ai"): + message = "No OpenRouter models available. Check token and connection." + case strings.Contains(cfg.CurrentAPI, "api.deepseek.com"): + message = "DeepSeek models should be available. Please report bug." + default: + message = "No llama.cpp models loaded. Ensure llama.cpp server is running with models." + } + showToast("Empty list", message) + return + } + // Create a list primitive + modelListWidget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + modelListWidget.SetTitle("Select Model").SetBorder(true) + // Find the current model index to set as selected + currentModelIndex := -1 + for i, model := range modelList { + if strings.TrimPrefix(model, models.LoadedMark) == chatBody.Model { + currentModelIndex = i + } + modelListWidget.AddItem(model, "", 0, nil) + } + // Set the current selection if found + if currentModelIndex != -1 { + modelListWidget.SetCurrentItem(currentModelIndex) + } + modelListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + modelName := strings.TrimPrefix(mainText, models.LoadedMark) + chatBody.Model = modelName + cfg.CurrentModel = chatBody.Model + pages.RemovePage("modelSelectionPopup") + app.SetFocus(textArea) + updateCachedModelColor() + updateStatusLine() + }) + modelListWidget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("modelSelectionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("modelSelectionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("modelSelectionPopup", modal(modelListWidget, 80, 20), true, true) + app.SetFocus(modelListWidget) +} + +// showAPILinkSelectionPopup creates a modal popup to select an API link +func showAPILinkSelectionPopup() { + // Prepare API links dropdown - ensure current API is in the list, avoid duplicates + apiLinks := make([]string, 0, len(cfg.ApiLinks)+1) + // Add current API first if it's not already in ApiLinks + foundCurrentAPI := false + for _, api := range cfg.ApiLinks { + if api == cfg.CurrentAPI { + foundCurrentAPI = true + } + apiLinks = append(apiLinks, api) + } + // If current API is not in the list, add it at the beginning + if !foundCurrentAPI { + apiLinks = make([]string, 0, len(cfg.ApiLinks)+1) + apiLinks = append(apiLinks, cfg.CurrentAPI) + apiLinks = append(apiLinks, cfg.ApiLinks...) + } + // Check for empty options list + if len(apiLinks) == 0 { + logger.Warn("no API links available for selection") + message := "No API links available. Please configure API links in your config file." + showToast("Empty list", message) + return + } + // Create a list primitive + apiListWidget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + apiListWidget.SetTitle("Select API Link").SetBorder(true) + // Find the current API index to set as selected + currentAPIIndex := -1 + for i, api := range apiLinks { + if api == cfg.CurrentAPI { + currentAPIIndex = i + } + apiListWidget.AddItem(api, "", 0, nil) + } + // Set the current selection if found + if currentAPIIndex != -1 { + apiListWidget.SetCurrentItem(currentAPIIndex) + } + apiListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + // Update the API in config + cfg.CurrentAPI = mainText + // updateToolCapabilities() + // Update model list based on new API + // Helper function to get model list for a given API (same as in props_table.go) + getModelListForAPI := func(api string) []string { + if strings.Contains(api, "api.deepseek.com/") { + return []string{"deepseek-chat", "deepseek-reasoner"} + } else if strings.Contains(api, "openrouter.ai") { + return ORFreeModels + } + // Assume local llama.cpp + refreshLocalModelsIfEmpty() + localModelsMu.RLock() + defer localModelsMu.RUnlock() + return LocalModels + } + newModelList := getModelListForAPI(cfg.CurrentAPI) + // Ensure chatBody.Model is in the new list; if not, set to first available model + if len(newModelList) > 0 && !slices.Contains(newModelList, chatBody.Model) { + chatBody.Model = strings.TrimPrefix(newModelList[0], models.LoadedMark) + cfg.CurrentModel = chatBody.Model + updateToolCapabilities() + } + pages.RemovePage("apiLinkSelectionPopup") + app.SetFocus(textArea) + choseChunkParser() + updateCachedModelColor() + updateStatusLine() + }) + apiListWidget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("apiLinkSelectionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("apiLinkSelectionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("apiLinkSelectionPopup", modal(apiListWidget, 80, 20), true, true) + app.SetFocus(apiListWidget) +} + +// showUserRoleSelectionPopup creates a modal popup to select a user role +func showUserRoleSelectionPopup() { + // Get the list of available roles + roles := listRolesWithUser() + // Check for empty options list + if len(roles) == 0 { + logger.Warn("no roles available for selection") + message := "No roles available for selection." + showToast("Empty list", message) + return + } + // Create a list primitive + roleListWidget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + roleListWidget.SetTitle("Select User Role").SetBorder(true) + // Find the current role index to set as selected + currentRole := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + currentRole = cfg.WriteNextMsgAs + } + currentRoleIndex := -1 + for i, role := range roles { + if strings.EqualFold(role, currentRole) { + currentRoleIndex = i + } + roleListWidget.AddItem(role, "", 0, nil) + } + // Set the current selection if found + if currentRoleIndex != -1 { + roleListWidget.SetCurrentItem(currentRoleIndex) + } + roleListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + // Update the user role in config + cfg.WriteNextMsgAs = mainText + // role got switch, update textview with character specific context for user + filtered := filterMessagesForCharacter(chatBody.Messages, mainText) + textView.SetText(chatToText(filtered, cfg.ShowSys)) + // Remove the popup page + pages.RemovePage("userRoleSelectionPopup") + app.SetFocus(textArea) + // Update the status line to reflect the change + updateStatusLine() + colorText() + }) + roleListWidget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("userRoleSelectionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("userRoleSelectionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("userRoleSelectionPopup", modal(roleListWidget, 80, 20), true, true) + app.SetFocus(roleListWidget) +} + +// showBotRoleSelectionPopup creates a modal popup to select a bot role +func showBotRoleSelectionPopup() { + // Get the list of available roles + roles := listChatRoles() + if len(roles) == 0 { + logger.Warn("empty roles in chat") + } + if !strInSlice(cfg.AssistantRole, roles) { + roles = append(roles, cfg.AssistantRole) + } + // Check for empty options list + if len(roles) == 0 { + logger.Warn("no roles available for selection") + message := "No roles available for selection." + showToast("Empty list", message) + return + } + // Create a list primitive + roleListWidget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + roleListWidget.SetTitle("Select Bot Role").SetBorder(true) + // Find the current role index to set as selected + currentRole := cfg.AssistantRole + if cfg.WriteNextMsgAsCompletionAgent != "" { + currentRole = cfg.WriteNextMsgAsCompletionAgent + } + currentRoleIndex := -1 + for i, role := range roles { + if strings.EqualFold(role, currentRole) { + currentRoleIndex = i + } + roleListWidget.AddItem(role, "", 0, nil) + } + // Set the current selection if found + if currentRoleIndex != -1 { + roleListWidget.SetCurrentItem(currentRoleIndex) + } + roleListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + // Update the bot role in config + cfg.WriteNextMsgAsCompletionAgent = mainText + // Remove the popup page + pages.RemovePage("botRoleSelectionPopup") + app.SetFocus(textArea) + // Update the status line to reflect the change + updateStatusLine() + }) + roleListWidget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("botRoleSelectionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("botRoleSelectionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("botRoleSelectionPopup", modal(roleListWidget, 80, 20), true, true) + app.SetFocus(roleListWidget) +} + +func showShellFileCompletionPopup(filter string) { + baseDir := cfg.FilePickerDir + if baseDir == "" { + baseDir = "." + } + complMatches := scanFiles(baseDir, filter) + if len(complMatches) == 0 { + return + } + if len(complMatches) == 1 { + currentText := shellInput.GetText() + atIdx := strings.LastIndex(currentText, "@") + if atIdx >= 0 { + before := currentText[:atIdx] + shellInput.SetText(before + complMatches[0]) + } + return + } + widget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + widget.SetTitle("file completion").SetBorder(true) + for _, m := range complMatches { + widget.AddItem(m, "", 0, nil) + } + widget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + currentText := shellInput.GetText() + atIdx := strings.LastIndex(currentText, "@") + if atIdx >= 0 { + before := currentText[:atIdx] + shellInput.SetText(before + mainText) + } + pages.RemovePage("shellFileCompletionPopup") + app.SetFocus(shellInput) + }) + widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("shellFileCompletionPopup") + app.SetFocus(shellInput) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("shellFileCompletionPopup") + app.SetFocus(shellInput) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + pages.AddPage("shellFileCompletionPopup", modal(widget, 80, 20), true, true) + app.SetFocus(widget) +} + +func showTextAreaFileCompletionPopup(filter string) { + baseDir := cfg.FilePickerDir + if baseDir == "" { + baseDir = "." + } + complMatches := scanFiles(baseDir, filter) + if len(complMatches) == 0 { + return + } + if len(complMatches) == 1 { + currentText := textArea.GetText() + atIdx := strings.LastIndex(currentText, "@") + if atIdx >= 0 { + before := currentText[:atIdx] + textArea.SetText(before+complMatches[0], true) + } + return + } + widget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + widget.SetTitle("file completion").SetBorder(true) + for _, m := range complMatches { + widget.AddItem(m, "", 0, nil) + } + widget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + currentText := textArea.GetText() + atIdx := strings.LastIndex(currentText, "@") + if atIdx >= 0 { + before := currentText[:atIdx] + textArea.SetText(before+mainText, true) + } + pages.RemovePage("textAreaFileCompletionPopup") + app.SetFocus(textArea) + }) + widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("textAreaFileCompletionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("textAreaFileCompletionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + pages.AddPage("textAreaFileCompletionPopup", modal(widget, 80, 20), true, true) + app.SetFocus(widget) +} + +func updateWidgetColors(theme *tview.Theme) { + bgColor := theme.PrimitiveBackgroundColor + fgColor := theme.PrimaryTextColor + borderColor := theme.BorderColor + titleColor := theme.TitleColor + textView.SetBackgroundColor(bgColor) + textView.SetTextColor(fgColor) + textView.SetBorderColor(borderColor) + textView.SetTitleColor(titleColor) + textArea.SetBackgroundColor(bgColor) + textArea.SetBorderColor(borderColor) + textArea.SetTitleColor(titleColor) + textArea.SetTextStyle(tcell.StyleDefault.Background(bgColor).Foreground(fgColor)) + textArea.SetPlaceholderStyle(tcell.StyleDefault.Background(bgColor).Foreground(fgColor)) + textArea.SetText(textArea.GetText(), true) + editArea.SetBackgroundColor(bgColor) + editArea.SetBorderColor(borderColor) + editArea.SetTitleColor(titleColor) + editArea.SetTextStyle(tcell.StyleDefault.Background(bgColor).Foreground(fgColor)) + editArea.SetPlaceholderStyle(tcell.StyleDefault.Background(bgColor).Foreground(fgColor)) + editArea.SetText(editArea.GetText(), true) + statusLineWidget.SetBackgroundColor(bgColor) + statusLineWidget.SetTextColor(fgColor) + statusLineWidget.SetBorderColor(borderColor) + statusLineWidget.SetTitleColor(titleColor) + helpView.SetBackgroundColor(bgColor) + helpView.SetTextColor(fgColor) + helpView.SetBorderColor(borderColor) + helpView.SetTitleColor(titleColor) + searchField.SetBackgroundColor(bgColor) + searchField.SetBorderColor(borderColor) + searchField.SetTitleColor(titleColor) +} + +// showColorschemeSelectionPopup creates a modal popup to select a colorscheme +func showColorschemeSelectionPopup() { + // Get the list of available colorschemes + schemeNames := make([]string, 0, len(colorschemes)) + for name := range colorschemes { + schemeNames = append(schemeNames, name) + } + slices.Sort(schemeNames) + // Check for empty options list + if len(schemeNames) == 0 { + logger.Warn("no colorschemes available for selection") + message := "No colorschemes available." + showToast("Empty list", message) + return + } + // Create a list primitive + schemeListWidget := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + schemeListWidget.SetTitle("Select Colorscheme").SetBorder(true) + currentScheme := "default" + for name := range colorschemes { + if tview.Styles == colorschemes[name] { + currentScheme = name + break + } + } + currentSchemeIndex := -1 + for i, scheme := range schemeNames { + if scheme == currentScheme { + currentSchemeIndex = i + } + schemeListWidget.AddItem(scheme, "", 0, nil) + } + // Set the current selection if found + if currentSchemeIndex != -1 { + schemeListWidget.SetCurrentItem(currentSchemeIndex) + } + schemeListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + // Update the colorscheme + if theme, ok := colorschemes[mainText]; ok { + tview.Styles = theme + go func() { + app.QueueUpdateDraw(func() { + updateWidgetColors(&theme) + }) + }() + } + // Remove the popup page + pages.RemovePage("colorschemeSelectionPopup") + app.SetFocus(textArea) + }) + schemeListWidget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("colorschemeSelectionPopup") + app.SetFocus(textArea) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage("colorschemeSelectionPopup") + app.SetFocus(textArea) + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("colorschemeSelectionPopup", modal(schemeListWidget, 40, len(schemeNames)+2), true, true) + app.SetFocus(schemeListWidget) +} diff --git a/props_table.go b/props_table.go new file mode 100644 index 0000000..d1d3680 --- /dev/null +++ b/props_table.go @@ -0,0 +1,358 @@ +package main + +import ( + "fmt" + "strconv" + "strings" + "sync" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +var _ = sync.RWMutex{} + +// Define constants for cell types +const ( + CellTypeCheckbox = "checkbox" + CellTypeDropdown = "dropdown" + CellTypeInput = "input" + CellTypeHeader = "header" + CellTypeListPopup = "listpopup" +) + +// CellData holds additional data for each cell +type CellData struct { + Type string + Options []string + OnChange interface{} +} + +// makePropsTable creates a table-based alternative to the props form +// This allows for better key bindings and immediate effect of changes +func makePropsTable(props map[string]float32) *tview.Table { + // Create a new table + table := tview.NewTable(). + SetBorders(true). + SetSelectable(true, false). + SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)) // Allow row selection but not column selection + table.SetTitle("Properties Configuration (Press 'x' to exit)"). + SetTitleAlign(tview.AlignLeft) + row := 0 + // Add a header or note row + headerCell := tview.NewTableCell("Props for llamacpp completion call"). + SetTextColor(tcell.ColorYellow). + SetAlign(tview.AlignLeft). + SetSelectable(false) + table.SetCell(row, 0, headerCell) + table.SetCell(row, 1, + tview.NewTableCell("press 'x' to exit"). + SetTextColor(tcell.ColorYellow). + SetSelectable(false)) + row++ + // Store cell data for later use in selection functions + cellData := make(map[string]*CellData) + // Helper function to add a checkbox-like row + addCheckboxRow := func(label string, initialValue bool, onChange func(bool)) { + table.SetCell(row, 0, + tview.NewTableCell(label). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignLeft). + SetSelectable(false)) + valueText := "No" + if initialValue { + valueText = "Yes" + } + valueCell := tview.NewTableCell(valueText). + SetTextColor(tcell.ColorYellow). + SetAlign(tview.AlignCenter) + table.SetCell(row, 1, valueCell) + // Store cell data + cellID := fmt.Sprintf("checkbox_%d", row) + cellData[cellID] = &CellData{ + Type: CellTypeCheckbox, + OnChange: onChange, + } + row++ + } + // Helper function to add a dropdown-like row, that opens a list popup + addListPopupRow := func(label string, options []string, initialValue string, onChange func(string)) { + table.SetCell(row, 0, + tview.NewTableCell(label). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignLeft). + SetSelectable(false)) + valueCell := tview.NewTableCell(initialValue). + SetTextColor(tcell.ColorYellow). + SetAlign(tview.AlignCenter) + table.SetCell(row, 1, valueCell) + // Store cell data + cellID := fmt.Sprintf("listpopup_%d", row) + cellData[cellID] = &CellData{ + Type: CellTypeListPopup, + Options: options, + OnChange: onChange, + } + row++ + } + // Helper function to add an input field row + addInputRow := func(label string, initialValue string, onChange func(string)) { + table.SetCell(row, 0, + tview.NewTableCell(label). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignLeft). + SetSelectable(false)) + valueCell := tview.NewTableCell(initialValue). + SetTextColor(tcell.ColorYellow). + SetAlign(tview.AlignCenter) + table.SetCell(row, 1, valueCell) + // Store cell data + cellID := fmt.Sprintf("input_%d", row) + cellData[cellID] = &CellData{ + Type: CellTypeInput, + OnChange: onChange, + } + row++ + } + // Add checkboxes + addCheckboxRow("Inject role", injectRole, func(checked bool) { + injectRole = checked + }) + addCheckboxRow("TTS Enabled", cfg.TTS_ENABLED, func(checked bool) { + cfg.TTS_ENABLED = checked + }) + addCheckboxRow("Enable Mouse", cfg.EnableMouse, func(checked bool) { + cfg.EnableMouse = checked + // Reconfigure the app's mouse setting + app.EnableMouse(cfg.EnableMouse) + }) + addCheckboxRow("Image Preview (file picker)", cfg.ImagePreview, func(checked bool) { + cfg.ImagePreview = checked + }) + addCheckboxRow("Auto turn (for cards with many chars)", cfg.AutoTurn, func(checked bool) { + cfg.AutoTurn = checked + }) + addCheckboxRow("Char specific context", cfg.CharSpecificContextEnabled, func(checked bool) { + cfg.CharSpecificContextEnabled = checked + }) + // Add dropdowns + logLevels := []string{"Debug", "Info", "Warn"} + addListPopupRow("Set log level", logLevels, GetLogLevel(), func(option string) { + setLogLevel(option) + }) + // Add reasoning effort dropdown (for OpenRouter and supported APIs) + reasoningEfforts := []string{"", "none", "minimal", "low", "medium", "high", "xhigh"} + addListPopupRow("Reasoning effort (OR)", reasoningEfforts, cfg.ReasoningEffort, func(option string) { + cfg.ReasoningEffort = option + }) + // Helper function to get model list for a given API + getModelListForAPI := func(api string) []string { + if strings.Contains(api, "api.deepseek.com/") { + return []string{"deepseek-chat", "deepseek-reasoner"} + } else if strings.Contains(api, "openrouter.ai") { + return ORFreeModels + } + // Assume local llama.cpp + refreshLocalModelsIfEmpty() + localModelsMu.RLock() + defer localModelsMu.RUnlock() + return LocalModels + } + // Add input fields + addInputRow("New char to write msg as", "", func(text string) { + if text != "" { + cfg.WriteNextMsgAs = text + } + }) + addInputRow("Username", cfg.UserRole, func(text string) { + if text != "" { + renameUser(cfg.UserRole, text) + cfg.UserRole = text + } + }) + // Add property fields (the float32 values) + for propName, value := range props { + propName := propName // capture loop variable for closure + propValue := fmt.Sprintf("%v", value) + addInputRow(propName, propValue, func(text string) { + if val, err := strconv.ParseFloat(text, 32); err == nil { + props[propName] = float32(val) + } + }) + } + // Set selection function to handle dropdown-like behavior + table.SetSelectedFunc(func(selectedRow, selectedCol int) { + // Only handle selection on the value column (column 1) + if selectedCol != 1 { + // If user selects the label column, move to the value column + if table.GetRowCount() > selectedRow && table.GetColumnCount() > 1 { + table.Select(selectedRow, 1) + } + return + } + // Get the cell and its corresponding data + cell := table.GetCell(selectedRow, selectedCol) + cellID := fmt.Sprintf("checkbox_%d", selectedRow) + // Check if it's a checkbox + if cellData[cellID] != nil && cellData[cellID].Type == CellTypeCheckbox { + data := cellData[cellID] + if onChange, ok := data.OnChange.(func(bool)); ok { + // Toggle the checkbox value + newValue := cell.Text == "No" + onChange(newValue) + if newValue { + cell.SetText("Yes") + } else { + cell.SetText("No") + } + } + return + } + // Check for dropdown + dropdownCellID := fmt.Sprintf("dropdown_%d", selectedRow) + if cellData[dropdownCellID] != nil && cellData[dropdownCellID].Type == CellTypeDropdown { + data := cellData[dropdownCellID] + if onChange, ok := data.OnChange.(func(string)); ok && data.Options != nil { + // Find current option and cycle to next + currentValue := cell.Text + currentIndex := -1 + for i, opt := range data.Options { + if opt == currentValue { + currentIndex = i + break + } + } + // Move to next option (cycle back to 0 if at end) + nextIndex := (currentIndex + 1) % len(data.Options) + newValue := data.Options[nextIndex] + onChange(newValue) + cell.SetText(newValue) + } + return + } + // Check for listpopup + listPopupCellID := fmt.Sprintf("listpopup_%d", selectedRow) + if cellData[listPopupCellID] != nil && cellData[listPopupCellID].Type == CellTypeListPopup { + data := cellData[listPopupCellID] + if onChange, ok := data.OnChange.(func(string)); ok { + // Get label for context + labelCell := table.GetCell(selectedRow, 0) + label := "item" + if labelCell != nil { + label = labelCell.Text + } + + // For model selection, always compute fresh options from current API + if label == "Select a model" { + freshOptions := getModelListForAPI(cfg.CurrentAPI) + data.Options = freshOptions + // Also update the cell data map + cellData[listPopupCellID].Options = freshOptions + } + + // Handle nil options + if data.Options == nil { + logger.Error("options list is nil for", "label", label) + showToast("Configuration error", "Options list is nil for "+label) + return + } + + // Check for empty options list + if len(data.Options) == 0 { + logger.Warn("empty options list for", "label", label, "api", cfg.CurrentAPI, "localModelsLen", len(LocalModels), "orModelsLen", len(ORFreeModels)) + message := "No options available for " + label + if label == "Select a model" { + switch { + case strings.Contains(cfg.CurrentAPI, "openrouter.ai"): + message = "No OpenRouter models available. Check token and connection." + case strings.Contains(cfg.CurrentAPI, "api.deepseek.com"): + message = "DeepSeek models should be available. Please report bug." + default: + message = "No llama.cpp models loaded. Ensure llama.cpp server is running with models." + } + } + showToast("Empty list", message) + return + } + // Create a list primitive + apiList := tview.NewList().ShowSecondaryText(false). + SetSelectedBackgroundColor(tcell.ColorGray) + apiList.SetTitle("Select " + label).SetBorder(true) + for i, api := range data.Options { + if api == cell.Text { + apiList.SetCurrentItem(i) + } + apiList.AddItem(api, "", 0, nil) + } + apiList.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { + onChange(mainText) + cell.SetText(mainText) + pages.RemovePage("apiListPopup") + }) + apiList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEscape { + pages.RemovePage("apiListPopup") + return nil + } + return event + }) + modal := func(p tview.Primitive, width, height int) tview.Primitive { + return tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(p, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + } + // Add modal page and make it visible + pages.AddPage("apiListPopup", modal(apiList, 80, 20), true, true) + app.SetFocus(apiList) + } + return + } + // Handle input fields by creating an input modal on selection + inputCellID := fmt.Sprintf("input_%d", selectedRow) + if cellData[inputCellID] != nil && cellData[inputCellID].Type == CellTypeInput { + data := cellData[inputCellID] + if onChange, ok := data.OnChange.(func(string)); ok { + // Create an input modal + currentValue := cell.Text + inputFld := tview.NewInputField() + inputFld.SetLabel("Edit value: ") + inputFld.SetText(currentValue) + inputFld.SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEnter { + newText := inputFld.GetText() + onChange(newText) + cell.SetText(newText) // Update the table cell + } + pages.RemovePage("editModal") + }) + // Create a simple modal with the input field + modalFlex := tview.NewFlex(). + SetDirection(tview.FlexRow). + AddItem(tview.NewBox(), 0, 1, false). // Spacer + AddItem(tview.NewFlex(). + AddItem(tview.NewBox(), 0, 1, false). // Spacer + AddItem(inputFld, 30, 1, true). // Input field + AddItem(tview.NewBox(), 0, 1, false), // Spacer + 0, 1, true). + AddItem(tview.NewBox(), 0, 1, false) // Spacer + // Add modal page and make it visible + pages.AddPage("editModal", modalFlex, true, true) + } + return + } + }) + // Set input capture to handle 'x' key for exiting + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(propsPage) + updateStatusLine() + return nil + } + return event + }) + return table +} diff --git a/rag/embedder.go b/rag/embedder.go new file mode 100644 index 0000000..5a4aae0 --- /dev/null +++ b/rag/embedder.go @@ -0,0 +1,444 @@ +package rag + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "gf-lt/config" + "gf-lt/models" + "log/slog" + "net/http" + "os" + "sync" + "time" + + "github.com/sugarme/tokenizer" + "github.com/sugarme/tokenizer/pretrained" + "github.com/yalue/onnxruntime_go" +) + +// Embedder defines the interface for embedding text +type Embedder interface { + Embed(text string) ([]float32, error) + EmbedSlice(lines []string) ([][]float32, error) +} + +// APIEmbedder implements embedder using an API (like Hugging Face, OpenAI, etc.) +type APIEmbedder struct { + logger *slog.Logger + client *http.Client + cfg *config.Config +} + +func NewAPIEmbedder(l *slog.Logger, cfg *config.Config) *APIEmbedder { + return &APIEmbedder{ + logger: l, + client: &http.Client{ + Timeout: 30 * time.Second, + }, + cfg: cfg, + } +} + +func (a *APIEmbedder) Embed(text string) ([]float32, error) { + payload, err := json.Marshal( + map[string]any{"input": text, "encoding_format": "float"}, + ) + if err != nil { + a.logger.Error("failed to marshal payload", "err", err.Error()) + return nil, err + } + req, err := http.NewRequest("POST", a.cfg.EmbedURL, bytes.NewReader(payload)) + if err != nil { + a.logger.Error("failed to create new req", "err", err.Error()) + return nil, err + } + if a.cfg.HFToken != "" { + req.Header.Add("Authorization", "Bearer "+a.cfg.HFToken) + } + resp, err := a.client.Do(req) + if err != nil { + a.logger.Error("failed to embed text", "err", err.Error()) + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + err = fmt.Errorf("non 200 response; code: %v", resp.StatusCode) + a.logger.Error(err.Error()) + return nil, err + } + embResp := &models.LCPEmbedResp{} + if err := json.NewDecoder(resp.Body).Decode(&embResp); err != nil { + a.logger.Error("failed to decode embedding response", "err", err.Error()) + return nil, err + } + if len(embResp.Data) == 0 || len(embResp.Data[0].Embedding) == 0 { + err = errors.New("empty embedding response") + a.logger.Error("empty embedding response") + return nil, err + } + return embResp.Data[0].Embedding, nil +} + +func (a *APIEmbedder) EmbedSlice(lines []string) ([][]float32, error) { + payload, err := json.Marshal( + map[string]any{"input": lines, "encoding_format": "float"}, + ) + if err != nil { + a.logger.Error("failed to marshal payload", "err", err.Error()) + return nil, err + } + req, err := http.NewRequest("POST", a.cfg.EmbedURL, bytes.NewReader(payload)) + if err != nil { + a.logger.Error("failed to create new req", "err", err.Error()) + return nil, err + } + if a.cfg.HFToken != "" { + req.Header.Add("Authorization", "Bearer "+a.cfg.HFToken) + } + resp, err := a.client.Do(req) + if err != nil { + a.logger.Error("failed to embed text", "err", err.Error()) + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + err = fmt.Errorf("non 200 response; code: %v", resp.StatusCode) + a.logger.Error(err.Error()) + return nil, err + } + embResp := &models.LCPEmbedResp{} + if err := json.NewDecoder(resp.Body).Decode(&embResp); err != nil { + a.logger.Error("failed to decode embedding response", "err", err.Error()) + return nil, err + } + if len(embResp.Data) == 0 { + err = errors.New("empty embedding response") + a.logger.Error("empty embedding response") + return nil, err + } + + // Collect all embeddings from the response + embeddings := make([][]float32, len(embResp.Data)) + for i := range embResp.Data { + if len(embResp.Data[i].Embedding) == 0 { + err = fmt.Errorf("empty embedding at index %d", i) + a.logger.Error("empty embedding", "index", i) + return nil, err + } + embeddings[i] = embResp.Data[i].Embedding + } + + // Sort embeddings by index to match the order of input lines + // API responses may not be in order + for _, data := range embResp.Data { + if data.Index >= len(embeddings) || data.Index < 0 { + err = fmt.Errorf("invalid embedding index %d", data.Index) + a.logger.Error("invalid embedding index", "index", data.Index) + return nil, err + } + embeddings[data.Index] = data.Embedding + } + return embeddings, nil +} + +// 1. Loading ONNX models locally +// 2. Using a Go ONNX runtime (like gorgonia/onnx or similar) +// 3. Converting text to embeddings without external API calls +type ONNXEmbedder struct { + session *onnxruntime_go.DynamicAdvancedSession + tokenizer *tokenizer.Tokenizer + tokenizerPath string + dims int + logger *slog.Logger + mu sync.Mutex + modelPath string +} + +var onnxInitOnce sync.Once +var onnxReady bool +var onnxLibPath string +var cudaLibPath string + +var onnxLibPaths = []string{ + "/usr/lib/libonnxruntime.so", + "/usr/lib/libonnxruntime.so.1.24.2", + "/usr/local/lib/libonnxruntime.so", + "/usr/lib/x86_64-linux-gnu/libonnxruntime.so", + "/opt/onnxruntime/lib/libonnxruntime.so", +} + +var cudaLibPaths = []string{ + "/usr/lib/libonnxruntime_providers_cuda.so", + "/usr/local/lib/libonnxruntime_providers_cuda.so", + "/opt/onnxruntime/lib/libonnxruntime_providers_cuda.so", +} + +func findONNXLibrary() string { + for _, path := range onnxLibPaths { + if _, err := os.Stat(path); err == nil { + return path + } + } + return "" +} + +func findCUDALibrary() string { + for _, path := range cudaLibPaths { + if _, err := os.Stat(path); err == nil { + return path + } + } + return "" +} + +func NewONNXEmbedder(modelPath, tokenizerPath string, dims int, logger *slog.Logger) (*ONNXEmbedder, error) { + // Check if model and tokenizer files exist + if _, err := os.Stat(modelPath); err != nil { + return nil, fmt.Errorf("ONNX model not found: %w", err) + } + if _, err := os.Stat(tokenizerPath); err != nil { + return nil, fmt.Errorf("tokenizer not found: %w", err) + } + + // Find ONNX library + onnxLibPath = findONNXLibrary() + if onnxLibPath == "" { + return nil, errors.New("ONNX runtime library not found in standard locations") + } + + // Find CUDA provider library (optional) + cudaLibPath = findCUDALibrary() + if cudaLibPath == "" { + fmt.Println("WARNING: CUDA provider library not found, will use CPU") + } + emb := &ONNXEmbedder{ + tokenizerPath: tokenizerPath, + dims: dims, + logger: logger, + modelPath: modelPath, + } + return emb, nil +} + +func (e *ONNXEmbedder) ensureInitialized() error { + if e.session != nil { + return nil + } + e.mu.Lock() + defer e.mu.Unlock() + if e.session != nil { + return nil + } + // Load tokenizer lazily + if e.tokenizer == nil { + tok, err := pretrained.FromFile(e.tokenizerPath) + if err != nil { + return fmt.Errorf("failed to load tokenizer: %w", err) + } + e.tokenizer = tok + } + onnxInitOnce.Do(func() { + onnxruntime_go.SetSharedLibraryPath(onnxLibPath) + if err := onnxruntime_go.InitializeEnvironment(); err != nil { + e.logger.Error("failed to initialize ONNX runtime", "error", err) + onnxReady = false + return + } + // Register CUDA provider if available + if cudaLibPath != "" { + if err := onnxruntime_go.RegisterExecutionProviderLibrary("CUDA", cudaLibPath); err != nil { + e.logger.Warn("failed to register CUDA provider", "error", err) + } + } + onnxReady = true + }) + if !onnxReady { + return errors.New("ONNX runtime not ready") + } + // Create session options + opts, err := onnxruntime_go.NewSessionOptions() + if err != nil { + return fmt.Errorf("failed to create session options: %w", err) + } + defer func() { + _ = opts.Destroy() + }() + + // Try to add CUDA provider + useCUDA := cudaLibPath != "" + if useCUDA { + cudaOpts, err := onnxruntime_go.NewCUDAProviderOptions() + if err != nil { + e.logger.Warn("failed to create CUDA provider options, falling back to CPU", "error", err) + useCUDA = false + } else { + defer func() { + _ = cudaOpts.Destroy() + }() + if err := cudaOpts.Update(map[string]string{"device_id": "0"}); err != nil { + e.logger.Warn("failed to update CUDA options, falling back to CPU", "error", err) + useCUDA = false + } else if err := opts.AppendExecutionProviderCUDA(cudaOpts); err != nil { + e.logger.Warn("failed to append CUDA provider, falling back to CPU", "error", err) + useCUDA = false + } + } + } + if useCUDA { + e.logger.Info("Using CUDA for ONNX inference") + } else { + e.logger.Info("Using CPU for ONNX inference") + } + + // Create session with options + session, err := onnxruntime_go.NewDynamicAdvancedSession( + e.getModelPath(), + []string{"input_ids", "attention_mask"}, + []string{"sentence_embedding"}, + opts, + ) + if err != nil { + return fmt.Errorf("failed to create ONNX session: %w", err) + } + e.session = session + return nil +} + +func (e *ONNXEmbedder) getModelPath() string { + return e.modelPath +} + +func (e *ONNXEmbedder) Destroy() error { + e.mu.Lock() + defer e.mu.Unlock() + if e.session != nil { + if err := e.session.Destroy(); err != nil { + return fmt.Errorf("failed to destroy ONNX session: %w", err) + } + e.session = nil + e.logger.Info("ONNX session destroyed, VRAM freed") + } + return nil +} + +func (e *ONNXEmbedder) Embed(text string) ([]float32, error) { + if err := e.ensureInitialized(); err != nil { + return nil, err + } + // 1. Tokenize + encoding, err := e.tokenizer.EncodeSingle(text) + if err != nil { + return nil, fmt.Errorf("tokenization failed: %w", err) + } + // 2. Convert to int64 and create attention mask + ids := encoding.Ids + inputIDs := make([]int64, len(ids)) + attentionMask := make([]int64, len(ids)) + for i, id := range ids { + inputIDs[i] = int64(id) + attentionMask[i] = 1 + } + // 3. Create input tensors (shape: [1, seq_len]) + seqLen := int64(len(inputIDs)) + inputIDsTensor, err := onnxruntime_go.NewTensor[int64]( + onnxruntime_go.NewShape(1, seqLen), + inputIDs, + ) + if err != nil { + return nil, fmt.Errorf("failed to create input_ids tensor: %w", err) + } + defer func() { _ = inputIDsTensor.Destroy() }() + maskTensor, err := onnxruntime_go.NewTensor[int64]( + onnxruntime_go.NewShape(1, seqLen), + attentionMask, + ) + if err != nil { + return nil, fmt.Errorf("failed to create attention_mask tensor: %w", err) + } + defer func() { _ = maskTensor.Destroy() }() + // 4. Create output tensor + outputTensor, err := onnxruntime_go.NewEmptyTensor[float32]( + onnxruntime_go.NewShape(1, int64(e.dims)), + ) + if err != nil { + return nil, fmt.Errorf("failed to create output tensor: %w", err) + } + defer func() { _ = outputTensor.Destroy() }() + // 5. Run inference + err = e.session.Run( + []onnxruntime_go.Value{inputIDsTensor, maskTensor}, + []onnxruntime_go.Value{outputTensor}, + ) + if err != nil { + return nil, fmt.Errorf("inference failed: %w", err) + } + // 6. Copy output data + outputData := outputTensor.GetData() + embedding := make([]float32, len(outputData)) + copy(embedding, outputData) + return embedding, nil +} + +func (e *ONNXEmbedder) EmbedSlice(texts []string) ([][]float32, error) { + if err := e.ensureInitialized(); err != nil { + return nil, err + } + encodings := make([]*tokenizer.Encoding, len(texts)) + maxLen := 0 + for i, txt := range texts { + enc, err := e.tokenizer.EncodeSingle(txt) + if err != nil { + return nil, err + } + encodings[i] = enc + if l := len(enc.Ids); l > maxLen { + maxLen = l + } + } + batchSize := len(texts) + inputIDs := make([]int64, batchSize*maxLen) + attentionMask := make([]int64, batchSize*maxLen) + for i, enc := range encodings { + ids := enc.Ids + offset := i * maxLen + for j, id := range ids { + inputIDs[offset+j] = int64(id) + attentionMask[offset+j] = 1 + } + // Remaining positions are already zero (padding) + } + // Create tensors with shape [batchSize, maxLen] + inputTensor, _ := onnxruntime_go.NewTensor[int64]( + onnxruntime_go.NewShape(int64(batchSize), int64(maxLen)), + inputIDs, + ) + defer func() { _ = inputTensor.Destroy() }() + maskTensor, _ := onnxruntime_go.NewTensor[int64]( + onnxruntime_go.NewShape(int64(batchSize), int64(maxLen)), + attentionMask, + ) + defer func() { _ = maskTensor.Destroy() }() + outputTensor, _ := onnxruntime_go.NewEmptyTensor[float32]( + onnxruntime_go.NewShape(int64(batchSize), int64(e.dims)), + ) + defer func() { _ = outputTensor.Destroy() }() + err := e.session.Run( + []onnxruntime_go.Value{inputTensor, maskTensor}, + []onnxruntime_go.Value{outputTensor}, + ) + if err != nil { + return nil, err + } + // Extract embeddings per batch item + data := outputTensor.GetData() + embeddings := make([][]float32, batchSize) + for i := 0; i < batchSize; i++ { + start := i * e.dims + emb := make([]float32, e.dims) + copy(emb, data[start:start+e.dims]) + embeddings[i] = emb + } + return embeddings, nil +} diff --git a/rag/extractors.go b/rag/extractors.go new file mode 100644 index 0000000..0f9f3f4 --- /dev/null +++ b/rag/extractors.go @@ -0,0 +1,181 @@ +package rag + +import ( + "archive/zip" + "bytes" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path" + "strings" + + "github.com/PuerkitoBio/goquery" + "github.com/ledongthuc/pdf" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/renderer/html" +) + +func ExtractText(fpath string) (string, error) { + ext := strings.ToLower(path.Ext(fpath)) + switch ext { + case ".txt": + return extractTextFromFile(fpath) + case ".md", ".markdown": + return extractTextFromMarkdown(fpath) + case ".html", ".htm": + return extractTextFromHtmlFile(fpath) + case ".epub": + return extractTextFromEpub(fpath) + case ".pdf": + return extractTextFromPdf(fpath) + default: + return "", fmt.Errorf("unsupported file format: %s", ext) + } +} + +func extractTextFromFile(fpath string) (string, error) { + data, err := os.ReadFile(fpath) + if err != nil { + return "", err + } + return string(data), nil +} + +func extractTextFromHtmlFile(fpath string) (string, error) { + data, err := os.ReadFile(fpath) + if err != nil { + return "", err + } + return extractTextFromHtmlContent(data) +} + +// non utf-8 encoding? +func extractTextFromHtmlContent(data []byte) (string, error) { + doc, err := goquery.NewDocumentFromReader(bytes.NewReader(data)) + if err != nil { + return "", err + } + // Remove script and style tags + doc.Find("script, style, noscript").Each(func(i int, s *goquery.Selection) { + s.Remove() + }) + // Get text and clean it + text := doc.Text() + // Collapse all whitespace (newlines, tabs, multiple spaces) into single spaces + cleaned := strings.Join(strings.Fields(text), " ") + return cleaned, nil +} + +func extractTextFromMarkdown(fpath string) (string, error) { + data, err := os.ReadFile(fpath) + if err != nil { + return "", err + } + // Convert markdown to HTML + md := goldmark.New( + goldmark.WithExtensions(extension.GFM), + goldmark.WithParserOptions(parser.WithAutoHeadingID()), + goldmark.WithRendererOptions(html.WithUnsafe()), // allow raw HTML if needed + ) + var buf bytes.Buffer + if err := md.Convert(data, &buf); err != nil { + return "", err + } + // Now extract text from the resulting HTML (using goquery or similar) + return extractTextFromHtmlContent(buf.Bytes()) +} + +func extractTextFromEpub(fpath string) (string, error) { + r, err := zip.OpenReader(fpath) + if err != nil { + return "", fmt.Errorf("failed to open epub: %w", err) + } + defer r.Close() + var sb strings.Builder + for _, f := range r.File { + ext := strings.ToLower(path.Ext(f.Name)) + if ext != ".xhtml" && ext != ".html" && ext != ".htm" && ext != ".xml" { + continue + } + + // Skip manifest, toc, ncx files - they don't contain book content + nameLower := strings.ToLower(f.Name) + if strings.Contains(nameLower, "toc") || strings.Contains(nameLower, "nav") || + strings.Contains(nameLower, "manifest") || strings.Contains(nameLower, ".opf") || + strings.HasSuffix(nameLower, ".ncx") { + continue + } + + rc, err := f.Open() + if err != nil { + continue + } + + if sb.Len() > 0 { + sb.WriteString("\n\n") + } + sb.WriteString(f.Name) + sb.WriteString("\n") + + buf, readErr := io.ReadAll(rc) + rc.Close() + if readErr == nil { + sb.WriteString(stripHTML(string(buf))) + } + } + if sb.Len() == 0 { + return "", errors.New("no content extracted from epub") + } + return sb.String(), nil +} + +func stripHTML(html string) string { + var sb strings.Builder + inTag := false + for _, r := range html { + switch r { + case '<': + inTag = true + case '>': + inTag = false + default: + if !inTag { + sb.WriteRune(r) + } + } + } + return sb.String() +} + +func extractTextFromPdf(fpath string) (string, error) { + _, err := exec.LookPath("pdftotext") + if err == nil { + out, err := exec.Command("pdftotext", "-layout", fpath, "-").Output() + if err == nil && len(out) > 0 { + return string(out), nil + } + } + return extractTextFromPdfPureGo(fpath) +} + +func extractTextFromPdfPureGo(fpath string) (string, error) { + df, r, err := pdf.Open(fpath) + if err != nil { + return "", fmt.Errorf("failed to open pdf: %w", err) + } + defer df.Close() + textReader, err := r.GetPlainText() + if err != nil { + return "", fmt.Errorf("failed to extract text from pdf: %w", err) + } + var buf bytes.Buffer + _, err = io.Copy(&buf, textReader) + if err != nil { + return "", fmt.Errorf("failed to read pdf text: %w", err) + } + return buf.String(), nil +} diff --git a/rag/rag.go b/rag/rag.go new file mode 100644 index 0000000..3a771d4 --- /dev/null +++ b/rag/rag.go @@ -0,0 +1,1197 @@ +package rag + +import ( + "context" + "errors" + "fmt" + "gf-lt/config" + "gf-lt/models" + "gf-lt/storage" + "log/slog" + "path" + "regexp" + "runtime" + "sort" + "strconv" + "strings" + "sync" + "time" + + "github.com/neurosnap/sentences/english" +) + +const () + +var ( + // Status messages for TUI integration + LongJobStatusCh = make(chan string, 100) // Increased buffer size for parallel batch updates + FinishedRAGStatus = "finished loading RAG file; press x to exit" + LoadedFileRAGStatus = "loaded file" + ErrRAGStatus = "some error occurred; failed to transfer data to vector db" + + // stopWords are common words that can be removed from queries when not part of phrases + stopWords = []string{"the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", "by", "from", "up", "down", "left", "right", "about", "like", "such", "than", "then", "also", "too"} +) + +// isStopWord checks if a word is in the stop words list +func isStopWord(word string) bool { + for _, stop := range stopWords { + if strings.EqualFold(word, stop) { + return true + } + } + return false +} + +// detectPhrases returns multi-word phrases from a query that should be treated as units +func detectPhrases(query string) []string { + words := strings.Fields(strings.ToLower(query)) + var phrases []string + + for i := 0; i < len(words)-1; i++ { + word1 := strings.Trim(words[i], ".,!?;:'\"()[]{}") + word2 := strings.Trim(words[i+1], ".,!?;:'\"()[]{}") + + // Skip if either word is a stop word or too short + if isStopWord(word1) || isStopWord(word2) || len(word1) < 2 || len(word2) < 2 { + continue + } + + // Check if this pair appears to be a meaningful phrase + // Simple heuristic: consecutive non-stop words of reasonable length + phrase := word1 + " " + word2 + phrases = append(phrases, phrase) + + // Optionally check for 3-word phrases + if i < len(words)-2 { + word3 := strings.Trim(words[i+2], ".,!?;:'\"()[]{}") + if !isStopWord(word3) && len(word3) >= 2 { + phrases = append(phrases, word1+" "+word2+" "+word3) + } + } + } + + return phrases +} + +// countPhraseMatches returns the number of query phrases found in text +func countPhraseMatches(text, query string) int { + phrases := detectPhrases(query) + if len(phrases) == 0 { + return 0 + } + textLower := strings.ToLower(text) + count := 0 + for _, phrase := range phrases { + if strings.Contains(textLower, phrase) { + count++ + } + } + return count +} + +// parseSlugIndices extracts batch and chunk indices from a slug +// slug format: filename_batch_chunk (e.g., "kjv_bible.epub_1786_0") +func parseSlugIndices(slug string) (batch, chunk int, ok bool) { + // Find the last two numbers separated by underscores + re := regexp.MustCompile(`_(\d+)_(\d+)$`) + matches := re.FindStringSubmatch(slug) + if matches == nil || len(matches) != 3 { + return 0, 0, false + } + batch, err1 := strconv.Atoi(matches[1]) + chunk, err2 := strconv.Atoi(matches[2]) + if err1 != nil || err2 != nil { + return 0, 0, false + } + return batch, chunk, true +} + +// areSlugsAdjacent returns true if two slugs are from the same file and have sequential indices +func areSlugsAdjacent(slug1, slug2 string) bool { + // Extract filename prefix (everything before the last underscore sequence) + parts1 := strings.Split(slug1, "_") + parts2 := strings.Split(slug2, "_") + if len(parts1) < 3 || len(parts2) < 3 { + return false + } + + // Compare filename prefixes (all parts except last two) + prefix1 := strings.Join(parts1[:len(parts1)-2], "_") + prefix2 := strings.Join(parts2[:len(parts2)-2], "_") + if prefix1 != prefix2 { + return false + } + + batch1, chunk1, ok1 := parseSlugIndices(slug1) + batch2, chunk2, ok2 := parseSlugIndices(slug2) + if !ok1 || !ok2 { + return false + } + + // Check if they're in same batch and chunks are sequential + if batch1 == batch2 && (chunk1 == chunk2+1 || chunk2 == chunk1+1) { + return true + } + + // Check if they're in sequential batches and chunk indices suggest continuity + // This is heuristic but useful for cross-batch adjacency + if (batch1 == batch2+1 && chunk1 == 0) || (batch2 == batch1+1 && chunk2 == 0) { + return true + } + return false +} + +type RAG struct { + logger *slog.Logger + store storage.FullRepo + cfg *config.Config + embedder Embedder + storage *VectorStorage + mu sync.RWMutex + idleMu sync.Mutex + fallbackMsg string + idleTimer *time.Timer + idleTimeout time.Duration +} + +// batchTask represents a single batch to be embedded +type batchTask struct { + batchIndex int + paragraphs []string + filename string + totalBatches int +} + +// batchResult represents the result of embedding a batch +type batchResult struct { + batchIndex int + embeddings [][]float32 + paragraphs []string + filename string +} + +// sendStatusNonBlocking sends a status message without blocking +func (r *RAG) sendStatusNonBlocking(status string) { + select { + case LongJobStatusCh <- status: + default: + r.logger.Warn("LongJobStatusCh channel is full or closed, dropping status message", "message", status) + } +} + +func New(l *slog.Logger, s storage.FullRepo, cfg *config.Config) (*RAG, error) { + var embedder Embedder + var fallbackMsg string + if cfg.EmbedModelPath != "" && cfg.EmbedTokenizerPath != "" { + emb, err := NewONNXEmbedder(cfg.EmbedModelPath, cfg.EmbedTokenizerPath, cfg.EmbedDims, l) + if err != nil { + l.Error("failed to create ONNX embedder, falling back to API", "error", err) + fallbackMsg = err.Error() + embedder = NewAPIEmbedder(l, cfg) + } else { + embedder = emb + l.Info("using ONNX embedder", "model", cfg.EmbedModelPath, "dims", cfg.EmbedDims) + } + } else { + embedder = NewAPIEmbedder(l, cfg) + l.Info("using API embedder", "url", cfg.EmbedURL) + } + rag := &RAG{ + logger: l, + store: s, + cfg: cfg, + embedder: embedder, + storage: NewVectorStorage(l, s), + fallbackMsg: fallbackMsg, + idleTimeout: 30 * time.Second, + } + + // Note: Vector tables are created via database migrations, not at runtime + + return rag, nil +} + +func createChunks(sentences []string, wordLimit, overlapWords uint32) []string { + if len(sentences) == 0 { + return nil + } + if overlapWords >= wordLimit { + overlapWords = wordLimit / 2 + } + var chunks []string + i := 0 + for i < len(sentences) { + var chunkWords []string + wordCount := 0 + j := i + for j < len(sentences) && wordCount <= int(wordLimit) { + sentence := sentences[j] + words := strings.Fields(sentence) + chunkWords = append(chunkWords, sentence) + wordCount += len(words) + j++ + // If this sentence alone exceeds limit, still include it and stop + if wordCount > int(wordLimit) { + break + } + } + if len(chunkWords) == 0 { + break + } + chunk := strings.Join(chunkWords, " ") + chunks = append(chunks, chunk) + if j >= len(sentences) { + break + } + // Move i forward by skipping overlap + if overlapWords == 0 { + i = j + continue + } + // Calculate how many sentences to skip to achieve overlapWords + overlapRemaining := int(overlapWords) + newI := i + for newI < j && overlapRemaining > 0 { + words := len(strings.Fields(sentences[newI])) + overlapRemaining -= words + if overlapRemaining >= 0 { + newI++ + } + } + if newI == i { + newI = j + } + i = newI + } + return chunks +} + +func sanitizeFTSQuery(query string) string { + // Keep double quotes for FTS5 phrase matching + // Remove other problematic characters + query = strings.ReplaceAll(query, "'", " ") + query = strings.ReplaceAll(query, ";", " ") + query = strings.ReplaceAll(query, "\\", " ") + query = strings.TrimSpace(query) + if query == "" { + return "*" // match all + } + return query +} + +func (r *RAG) LoadRAG(fpath string) error { + return r.LoadRAGWithContext(context.Background(), fpath) +} + +func (r *RAG) LoadRAGWithContext(ctx context.Context, fpath string) error { + r.mu.Lock() + defer r.mu.Unlock() + fileText, err := ExtractText(fpath) + if err != nil { + return err + } + r.logger.Debug("rag: loaded file", "fp", fpath) + + // Send initial status (non-blocking with retry) + r.sendStatusNonBlocking(LoadedFileRAGStatus) + tokenizer, err := english.NewSentenceTokenizer(nil) + if err != nil { + return err + } + sentences := tokenizer.Tokenize(fileText) + sents := make([]string, len(sentences)) + for i, s := range sentences { + sents[i] = s.Text + } + + // Create chunks with overlap + paragraphs := createChunks(sents, r.cfg.RAGWordLimit, r.cfg.RAGOverlapWords) + // Adjust batch size if needed + if len(paragraphs) < r.cfg.RAGBatchSize && len(paragraphs) > 0 { + r.cfg.RAGBatchSize = len(paragraphs) + } + if len(paragraphs) == 0 { + return errors.New("no valid paragraphs found in file") + } + totalBatches := (len(paragraphs) + r.cfg.RAGBatchSize - 1) / r.cfg.RAGBatchSize + r.logger.Debug("starting parallel embedding", "total_batches", totalBatches, "batch_size", r.cfg.RAGBatchSize) + + // Determine concurrency level + concurrency := runtime.NumCPU() + if concurrency > totalBatches { + concurrency = totalBatches + } + if concurrency < 1 { + concurrency = 1 + } + // If using ONNX embedder, limit concurrency to 1 due to mutex serialization + var isONNX bool + if _, isONNX = r.embedder.(*ONNXEmbedder); isONNX { + concurrency = 1 + } + embedderType := "API" + if isONNX { + embedderType = "ONNX" + } + r.logger.Debug("parallel embedding setup", + "total_batches", totalBatches, + "concurrency", concurrency, + "embedder", embedderType, + "batch_size", r.cfg.RAGBatchSize) + + // Create context with timeout (30 minutes) and cancellation for error handling + ctx, cancel := context.WithTimeout(ctx, 30*time.Minute) + defer cancel() + + // Channels for task distribution and results + taskCh := make(chan batchTask, totalBatches) + resultCh := make(chan batchResult, totalBatches) + errorCh := make(chan error, totalBatches) + + // Start worker goroutines + var wg sync.WaitGroup + for w := 0; w < concurrency; w++ { + wg.Add(1) + go r.embeddingWorker(ctx, w, taskCh, resultCh, errorCh, &wg) + } + + // Close task channel after all tasks are sent (by separate goroutine) + go func() { + // Ensure task channel is closed when this goroutine exits + defer close(taskCh) + r.logger.Debug("task distributor started", "total_batches", totalBatches) + for i := 0; i < totalBatches; i++ { + start := i * r.cfg.RAGBatchSize + end := start + r.cfg.RAGBatchSize + if end > len(paragraphs) { + end = len(paragraphs) + } + batch := paragraphs[start:end] + + // Filter empty paragraphs + nonEmptyBatch := make([]string, 0, len(batch)) + for _, p := range batch { + if strings.TrimSpace(p) != "" { + nonEmptyBatch = append(nonEmptyBatch, strings.TrimSpace(p)) + } + } + + task := batchTask{ + batchIndex: i, + paragraphs: nonEmptyBatch, + filename: path.Base(fpath), + totalBatches: totalBatches, + } + + select { + case taskCh <- task: + r.logger.Debug("task distributor sent batch", "batch", i, "paragraphs", len(nonEmptyBatch)) + case <-ctx.Done(): + r.logger.Debug("task distributor cancelled", "batches_sent", i+1, "total_batches", totalBatches) + return + } + } + r.logger.Debug("task distributor finished", "batches_sent", totalBatches) + }() + + // Wait for workers to finish and close result channel + go func() { + wg.Wait() + close(resultCh) + }() + + // Process results in order and write to database + nextExpectedBatch := 0 + resultsBuffer := make(map[int]batchResult) + filename := path.Base(fpath) + batchesProcessed := 0 + for { + select { + case <-ctx.Done(): + return ctx.Err() + + case err := <-errorCh: + // First error from any worker, cancel everything + cancel() + r.logger.Error("embedding worker failed", "error", err) + r.sendStatusNonBlocking(ErrRAGStatus) + return fmt.Errorf("embedding failed: %w", err) + + case result, ok := <-resultCh: + if !ok { + // All results processed + resultCh = nil + r.logger.Debug("result channel closed", "batches_processed", batchesProcessed, "total_batches", totalBatches) + continue + } + + // Store result in buffer + resultsBuffer[result.batchIndex] = result + + // Process buffered results in order + for { + if res, exists := resultsBuffer[nextExpectedBatch]; exists { + // Write this batch to database + if err := r.writeBatchToStorage(ctx, res, filename); err != nil { + cancel() + return err + } + + batchesProcessed++ + // Send progress update + statusMsg := fmt.Sprintf("processed batch %d/%d", batchesProcessed, totalBatches) + r.sendStatusNonBlocking(statusMsg) + + delete(resultsBuffer, nextExpectedBatch) + nextExpectedBatch++ + } else { + break + } + } + + default: + // No channels ready, check for deadlock conditions + if resultCh == nil && nextExpectedBatch < totalBatches { + // Missing batch results after result channel closed + r.logger.Error("missing batch results", + "expected", totalBatches, + "received", nextExpectedBatch, + "missing", totalBatches-nextExpectedBatch) + + // Wait a short time for any delayed errors, then cancel + select { + case <-time.After(5 * time.Second): + cancel() + return fmt.Errorf("missing batch results: expected %d, got %d", totalBatches, nextExpectedBatch) + case <-ctx.Done(): + return ctx.Err() + case err := <-errorCh: + cancel() + r.logger.Error("embedding worker failed after result channel closed", "error", err) + r.sendStatusNonBlocking(ErrRAGStatus) + return fmt.Errorf("embedding failed: %w", err) + } + } + // If we reach here, no deadlock yet, just busy loop prevention + time.Sleep(100 * time.Millisecond) + } + + // Check if we're done + if resultCh == nil && nextExpectedBatch >= totalBatches { + r.logger.Debug("all batches processed successfully", "total", totalBatches) + break + } + } + r.logger.Debug("finished writing vectors", "batches", batchesProcessed) + r.resetIdleTimer() + r.sendStatusNonBlocking(FinishedRAGStatus) + return nil +} + +// embeddingWorker processes batch embedding tasks +func (r *RAG) embeddingWorker(ctx context.Context, workerID int, taskCh <-chan batchTask, resultCh chan<- batchResult, errorCh chan<- error, wg *sync.WaitGroup) { + defer wg.Done() + r.logger.Debug("embedding worker started", "worker", workerID) + + // Panic recovery to ensure worker doesn't crash silently + defer func() { + if rec := recover(); rec != nil { + r.logger.Error("embedding worker panicked", "worker", workerID, "panic", rec) + // Try to send error, but don't block if channel is full + select { + case errorCh <- fmt.Errorf("worker %d panicked: %v", workerID, rec): + default: + r.logger.Warn("error channel full, dropping panic error", "worker", workerID) + } + } + }() + for task := range taskCh { + select { + case <-ctx.Done(): + r.logger.Debug("embedding worker cancelled", "worker", workerID) + return + default: + } + r.logger.Debug("worker processing batch", "worker", workerID, "batch", task.batchIndex, "paragraphs", len(task.paragraphs), "total_batches", task.totalBatches) + + // Skip empty batches + if len(task.paragraphs) == 0 { + select { + case resultCh <- batchResult{ + batchIndex: task.batchIndex, + embeddings: nil, + paragraphs: nil, + filename: task.filename, + }: + case <-ctx.Done(): + r.logger.Debug("embedding worker cancelled while sending empty batch", "worker", workerID) + return + } + r.logger.Debug("worker sent empty batch", "worker", workerID, "batch", task.batchIndex) + continue + } + // Embed with retry for API embedder + embeddings, err := r.embedWithRetry(ctx, task.paragraphs, 3) + if err != nil { + // Try to send error, but don't block indefinitely + select { + case errorCh <- fmt.Errorf("worker %d batch %d: %w", workerID, task.batchIndex, err): + case <-ctx.Done(): + r.logger.Debug("embedding worker cancelled while sending error", "worker", workerID) + } + return + } + // Send result with context awareness + select { + case resultCh <- batchResult{ + batchIndex: task.batchIndex, + embeddings: embeddings, + paragraphs: task.paragraphs, + filename: task.filename, + }: + case <-ctx.Done(): + r.logger.Debug("embedding worker cancelled while sending result", "worker", workerID) + return + } + r.logger.Debug("worker completed batch", "worker", workerID, "batch", task.batchIndex, "embeddings", len(embeddings)) + } + r.logger.Debug("embedding worker finished", "worker", workerID) +} + +// embedWithRetry attempts embedding with exponential backoff for API embedder +func (r *RAG) embedWithRetry(ctx context.Context, paragraphs []string, maxRetries int) ([][]float32, error) { + var lastErr error + for attempt := 0; attempt < maxRetries; attempt++ { + if attempt > 0 { + // Exponential backoff + backoff := time.Duration(attempt*attempt) * time.Second + if backoff > 10*time.Second { + backoff = 10 * time.Second + } + select { + case <-time.After(backoff): + case <-ctx.Done(): + return nil, ctx.Err() + } + r.logger.Debug("retrying embedding", "attempt", attempt, "max_retries", maxRetries) + } + + embeddings, err := r.embedder.EmbedSlice(paragraphs) + if err == nil { + // Validate embedding count + if len(embeddings) != len(paragraphs) { + return nil, fmt.Errorf("embedding count mismatch: expected %d, got %d", len(paragraphs), len(embeddings)) + } + return embeddings, nil + } + + lastErr = err + // Only retry for API embedder errors (network/timeout) + // For ONNX embedder, fail fast + if _, isAPI := r.embedder.(*APIEmbedder); !isAPI { + break + } + } + return nil, fmt.Errorf("embedding failed after %d attempts: %w", maxRetries, lastErr) +} + +// writeBatchToStorage writes a single batch of vectors to the database +func (r *RAG) writeBatchToStorage(ctx context.Context, result batchResult, filename string) error { + if len(result.embeddings) == 0 { + // Empty batch, skip + return nil + } + // Check context before starting + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + + // Build all vectors for batch write + vectors := make([]*models.VectorRow, 0, len(result.paragraphs)) + for j, text := range result.paragraphs { + vectors = append(vectors, &models.VectorRow{ + Embeddings: result.embeddings[j], + RawText: text, + Slug: fmt.Sprintf("%s_%d_%d", filename, result.batchIndex+1, j), + FileName: filename, + }) + } + + // Write all vectors in a single transaction + if err := r.storage.WriteVectors(vectors); err != nil { + r.logger.Error("failed to write vectors batch to DB", "error", err, "batch", result.batchIndex+1, "size", len(vectors)) + r.sendStatusNonBlocking(ErrRAGStatus) + return fmt.Errorf("failed to write vectors batch: %w", err) + } + r.logger.Debug("wrote batch to db", "batch", result.batchIndex+1, "size", len(result.paragraphs)) + return nil +} + +func (r *RAG) LineToVector(line string) ([]float32, error) { + r.resetIdleTimer() + return r.embedder.Embed(line) +} + +func (r *RAG) searchEmb(emb *models.EmbeddingResp, limit int) ([]models.VectorRow, error) { + r.resetIdleTimer() + return r.storage.SearchClosest(emb.Embedding, limit) +} + +func (r *RAG) searchKeyword(query string, limit int) ([]models.VectorRow, error) { + r.resetIdleTimer() + sanitized := sanitizeFTSQuery(query) + return r.storage.SearchKeyword(sanitized, limit) +} + +func (r *RAG) ListLoaded() ([]string, error) { + r.mu.RLock() + defer r.mu.RUnlock() + return r.storage.ListFiles() +} + +func (r *RAG) RemoveFile(filename string) error { + r.mu.Lock() + defer r.mu.Unlock() + r.resetIdleTimer() + return r.storage.RemoveEmbByFileName(filename) +} + +var ( + queryRefinementPattern = regexp.MustCompile(`(?i)(based on my (vector db|vector db|vector database|rags?|past (conversations?|chat|messages?))|from my (files?|documents?|data|information|memory)|search (in|my) (vector db|database|rags?)|rag search for)`) + importantKeywords = []string{"project", "architecture", "code", "file", "chat", "conversation", "topic", "summary", "details", "history", "previous", "my", "user", "me"} +) + +func (r *RAG) RefineQuery(query string) string { + original := query + query = strings.TrimSpace(query) + if len(query) == 0 { + return original + } + if len(query) <= 3 { + return original + } + // If query already contains double quotes, assume it's a phrase query and skip refinement + if strings.Contains(query, "\"") { + return original + } + query = strings.ToLower(query) + words := strings.Fields(query) + if len(words) >= 3 { + // Detect phrases and protect words that are part of phrases + phrases := detectPhrases(query) + protectedWords := make(map[string]bool) + for _, phrase := range phrases { + for _, word := range strings.Fields(phrase) { + protectedWords[word] = true + } + } + + // Remove stop words that are not protected + for _, stopWord := range stopWords { + if protectedWords[stopWord] { + continue + } + wordPattern := `\b` + stopWord + `\b` + re := regexp.MustCompile(wordPattern) + query = re.ReplaceAllString(query, "") + } + } + query = strings.TrimSpace(query) + if len(query) < 5 { + return original + } + if queryRefinementPattern.MatchString(original) { + cleaned := queryRefinementPattern.ReplaceAllString(original, "") + cleaned = strings.TrimSpace(cleaned) + if len(cleaned) >= 5 { + return cleaned + } + } + query = r.extractImportantPhrases(query) + if len(query) < 5 { + return original + } + return query +} + +func (r *RAG) extractImportantPhrases(query string) string { + words := strings.Fields(query) + var important []string + for _, word := range words { + word = strings.Trim(word, ".,!?;:'\"()[]{}") + isImportant := false + for _, kw := range importantKeywords { + if strings.Contains(strings.ToLower(word), kw) { + isImportant = true + break + } + } + if isImportant || len(word) >= 3 { + important = append(important, word) + } + } + if len(important) == 0 { + return query + } + return strings.Join(important, " ") +} + +func (r *RAG) GenerateQueryVariations(query string) []string { + variations := []string{query} + if len(query) < 5 { + return variations + } + parts := strings.Fields(query) + if len(parts) == 0 { + return variations + } + // Get loaded filenames to filter out filename terms + filenames, err := r.storage.ListFiles() + if err == nil && len(filenames) > 0 { + // Convert to lowercase for case-insensitive matching + lowerFilenames := make([]string, len(filenames)) + for i, f := range filenames { + lowerFilenames[i] = strings.ToLower(f) + } + filteredParts := make([]string, 0, len(parts)) + for _, part := range parts { + partLower := strings.ToLower(part) + skip := false + for _, fn := range lowerFilenames { + if strings.Contains(fn, partLower) || strings.Contains(partLower, fn) { + skip = true + break + } + } + if !skip { + filteredParts = append(filteredParts, part) + } + } + // If filteredParts not empty and different from original, add filtered query + if len(filteredParts) > 0 && len(filteredParts) != len(parts) { + filteredQuery := strings.Join(filteredParts, " ") + if len(filteredQuery) >= 5 { + variations = append(variations, filteredQuery) + } + } + } + if len(parts) >= 2 { + trimmed := strings.Join(parts[:len(parts)-1], " ") + if len(trimmed) >= 5 { + variations = append(variations, trimmed) + } + } + if len(parts) >= 2 { + trimmed := strings.Join(parts[1:], " ") + if len(trimmed) >= 5 { + variations = append(variations, trimmed) + } + } + if !strings.HasSuffix(query, " explanation") { + variations = append(variations, query+" explanation") + } + if !strings.HasPrefix(query, "what is ") { + variations = append(variations, "what is "+query) + } + if !strings.HasSuffix(query, " details") { + variations = append(variations, query+" details") + } + if !strings.HasSuffix(query, " summary") { + variations = append(variations, query+" summary") + } + + // Add phrase-quoted variations for better FTS5 matching + phrases := detectPhrases(query) + if len(phrases) > 0 { + // Sort phrases by length descending to prioritize longer phrases + sort.Slice(phrases, func(i, j int) bool { + return len(phrases[i]) > len(phrases[j]) + }) + + // Create a version with all phrases quoted + quotedQuery := query + for _, phrase := range phrases { + // Only quote if not already quoted + quotedPhrase := "\"" + phrase + "\"" + if !strings.Contains(strings.ToLower(quotedQuery), strings.ToLower(quotedPhrase)) { + // Case-insensitive replacement of phrase with quoted version + re := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(phrase) + `\b`) + quotedQuery = re.ReplaceAllString(quotedQuery, quotedPhrase) + } + } + // Disabled malformed quoted query for now + // if quotedQuery != query { + // variations = append(variations, quotedQuery) + // } + + // Also add individual phrase variations for short queries + if len(phrases) <= 5 { + for _, phrase := range phrases { + // Create a focused query with just this phrase quoted + // Keep original context but emphasize this phrase + quotedPhrase := "\"" + phrase + "\"" + re := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(phrase) + `\b`) + focusedQuery := re.ReplaceAllString(query, quotedPhrase) + if focusedQuery != query && focusedQuery != quotedQuery { + variations = append(variations, focusedQuery) + } + // Add the phrase alone (quoted) as a separate variation + variations = append(variations, quotedPhrase) + } + } + } + + return variations +} + +func (r *RAG) RerankResults(results []models.VectorRow, query string) []models.VectorRow { + phraseCount := len(detectPhrases(query)) + type scoredResult struct { + row models.VectorRow + distance float32 + phraseMatches int + } + scored := make([]scoredResult, 0, len(results)) + for i := range results { + row := results[i] + + score := float32(0) + rawTextLower := strings.ToLower(row.RawText) + queryLower := strings.ToLower(query) + if strings.Contains(rawTextLower, queryLower) { + score += 10 + } + queryWords := strings.Fields(queryLower) + matchCount := 0 + for _, word := range queryWords { + if len(word) > 2 && strings.Contains(rawTextLower, word) { + matchCount++ + } + } + if len(queryWords) > 0 { + score += float32(matchCount) / float32(len(queryWords)) * 5 + } + if row.FileName == "chat" || strings.Contains(strings.ToLower(row.FileName), "conversation") { + score += 3 + } + + // Phrase match bonus: extra points for containing detected phrases + phraseMatches := countPhraseMatches(row.RawText, query) + if phraseMatches > 0 { + // Significant bonus per phrase to prioritize exact phrase matches + r.logger.Debug("phrase match bonus", "slug", row.Slug, "phraseMatches", phraseMatches, "score", score) + score += float32(phraseMatches) * 100 + } + + // Cross-chunk adjacency bonus: if this chunk has adjacent siblings in results, + // boost score to promote narrative continuity + adjacentCount := 0 + for _, other := range results { + if other.Slug == row.Slug { + continue + } + if areSlugsAdjacent(row.Slug, other.Slug) { + adjacentCount++ + } + } + if adjacentCount > 0 { + // Bonus per adjacent chunk, but diminishing returns + score += float32(adjacentCount) * 4 + } + distance := row.Distance - score/100 + scored = append(scored, scoredResult{row: row, distance: distance, phraseMatches: phraseMatches}) + } + sort.Slice(scored, func(i, j int) bool { + return scored[i].distance < scored[j].distance + }) + unique := make([]models.VectorRow, 0) + seen := make(map[string]bool) + maxPerFile := 2 + if phraseCount > 0 { + maxPerFile = 10 + } + fileCounts := make(map[string]int) + for i := range scored { + if !seen[scored[i].row.Slug] { + // Allow phrase-matching chunks to bypass per-file limit (up to +5 extra) + allowed := fileCounts[scored[i].row.FileName] < maxPerFile + if !allowed && scored[i].phraseMatches > 0 { + // If chunk has phrase matches, allow extra slots (up to maxPerFile + 5) + allowed = fileCounts[scored[i].row.FileName] < maxPerFile+5 + } + if !allowed { + continue + } + seen[scored[i].row.Slug] = true + fileCounts[scored[i].row.FileName]++ + unique = append(unique, scored[i].row) + } + } + if len(unique) > 30 { + unique = unique[:30] + } + return unique +} + +func (r *RAG) SynthesizeAnswer(results []models.VectorRow, query string) (string, error) { + r.mu.RLock() + defer r.mu.RUnlock() + r.resetIdleTimer() + if len(results) == 0 { + return "No relevant information found in the vector database.", nil + } + var contextBuilder strings.Builder + contextBuilder.WriteString("User Query: ") + contextBuilder.WriteString(query) + contextBuilder.WriteString("\n\nRetrieved Context:\n") + for i, row := range results { + fmt.Fprintf(&contextBuilder, "[Source %d: %s]\n", i+1, row.FileName) + contextBuilder.WriteString(row.RawText) + contextBuilder.WriteString("\n\n") + } + contextBuilder.WriteString("Instructions: ") + contextBuilder.WriteString("Based on the retrieved context above, provide a concise, coherent answer to the user's query. ") + contextBuilder.WriteString("Extract only the most relevant information. ") + contextBuilder.WriteString("If no relevant information is found, state that clearly. ") + contextBuilder.WriteString("Cite sources by filename when relevant. ") + contextBuilder.WriteString("Do not include unnecessary preamble or explanations.") + synthesisPrompt := contextBuilder.String() + emb, err := r.LineToVector(synthesisPrompt) + if err != nil { + r.logger.Error("failed to embed synthesis prompt", "error", err) + return "", err + } + embResp := &models.EmbeddingResp{ + Embedding: emb, + Index: 0, + } + topResults, err := r.searchEmb(embResp, 1) + if err != nil { + r.logger.Error("failed to search for synthesis context", "error", err) + return "", err + } + if len(topResults) > 0 && topResults[0].RawText != synthesisPrompt { + return topResults[0].RawText, nil + } + var finalAnswer strings.Builder + finalAnswer.WriteString("Based on the retrieved context:\n\n") + for i, row := range results { + if i >= 5 { + break + } + fmt.Fprintf(&finalAnswer, "- From %s: %s\n", row.FileName, truncateString(row.RawText, 200)) + } + return finalAnswer.String(), nil +} + +func truncateString(s string, maxLen int) string { + if len(s) <= maxLen { + return s + } + return s[:maxLen] + "..." +} + +func (r *RAG) Search(query string, limit int) ([]models.VectorRow, error) { + r.mu.RLock() + defer r.mu.RUnlock() + r.resetIdleTimer() + refined := r.RefineQuery(query) + variations := r.GenerateQueryVariations(refined) + r.logger.Debug("query variations", "original", query, "refined", refined, "variations", variations) + + // Collect embedding search results from all variations + var embResults []models.VectorRow + seen := make(map[string]bool) + for _, q := range variations { + emb, err := r.LineToVector(q) + if err != nil { + r.logger.Error("failed to embed query variation", "error", err, "query", q) + continue + } + embResp := &models.EmbeddingResp{ + Embedding: emb, + Index: 0, + } + results, err := r.searchEmb(embResp, limit*2) // Get more candidates + if err != nil { + r.logger.Error("failed to search embeddings", "error", err, "query", q) + continue + } + for _, row := range results { + if !seen[row.Slug] { + seen[row.Slug] = true + embResults = append(embResults, row) + } + } + } + // Sort embedding results by distance (lower is better) + sort.Slice(embResults, func(i, j int) bool { + return embResults[i].Distance < embResults[j].Distance + }) + + // Perform keyword search on all variations + var kwResults []models.VectorRow + seenKw := make(map[string]bool) + for _, q := range variations { + results, err := r.searchKeyword(q, limit) + if err != nil { + r.logger.Debug("keyword search failed for variation", "error", err, "query", q) + continue + } + for _, row := range results { + if !seenKw[row.Slug] { + seenKw[row.Slug] = true + kwResults = append(kwResults, row) + } + } + } + // Sort keyword results by distance (lower is better) + sort.Slice(kwResults, func(i, j int) bool { + return kwResults[i].Distance < kwResults[j].Distance + }) + + // Combine using Reciprocal Rank Fusion (RRF) + // Use smaller K for phrase-heavy queries to give more weight to top ranks + phraseCount := len(detectPhrases(query)) + rrfK := 60.0 + if phraseCount > 0 { + rrfK = 30.0 + } + r.logger.Debug("RRF parameters", "phraseCount", phraseCount, "rrfK", rrfK, "query", query) + type scoredRow struct { + row models.VectorRow + score float64 + } + scoreMap := make(map[string]float64) + // Add embedding results + for rank, row := range embResults { + score := 1.0 / (float64(rank) + rrfK) + scoreMap[row.Slug] += score + if row.Slug == "kjv_bible.epub_1786_0" { + r.logger.Debug("target chunk embedding rank", "rank", rank, "score", score) + } + } + // Add keyword results with weight boost when phrases are present + kwWeight := 1.0 + if phraseCount > 0 { + kwWeight = 100.0 + } + r.logger.Debug("keyword weight", "kwWeight", kwWeight, "phraseCount", phraseCount) + for rank, row := range kwResults { + score := kwWeight * (1.0 / (float64(rank) + rrfK)) + scoreMap[row.Slug] += score + if row.Slug == "kjv_bible.epub_1786_0" { + r.logger.Debug("target chunk keyword rank", "rank", rank, "score", score, "kwWeight", kwWeight, "rrfK", rrfK) + } + // Ensure row exists in combined results + if _, exists := seen[row.Slug]; !exists { + embResults = append(embResults, row) + } + } + // Create slice of scored rows + scoredRows := make([]scoredRow, 0, len(embResults)) + for _, row := range embResults { + score := scoreMap[row.Slug] + scoredRows = append(scoredRows, scoredRow{row: row, score: score}) + } + // Debug: log scores for target chunk and top chunks + if strings.Contains(strings.ToLower(query), "bald") || strings.Contains(strings.ToLower(query), "she bears") { + for _, sr := range scoredRows { + if sr.row.Slug == "kjv_bible.epub_1786_0" { + r.logger.Debug("target chunk score", "slug", sr.row.Slug, "score", sr.score, "distance", sr.row.Distance) + } + } + // Log top 5 scores + for i := 0; i < len(scoredRows) && i < 5; i++ { + r.logger.Debug("top scored row", "rank", i+1, "slug", scoredRows[i].row.Slug, "score", scoredRows[i].score, "distance", scoredRows[i].row.Distance) + } + } + // Sort by descending RRF score + sort.Slice(scoredRows, func(i, j int) bool { + return scoredRows[i].score > scoredRows[j].score + }) + // Take top limit + if len(scoredRows) > limit { + scoredRows = scoredRows[:limit] + } + // Convert back to VectorRow + finalResults := make([]models.VectorRow, len(scoredRows)) + for i, sr := range scoredRows { + finalResults[i] = sr.row + } + // Apply reranking heuristics + reranked := r.RerankResults(finalResults, query) + return reranked, nil +} + +var ( + ragInstance *RAG + ragOnce sync.Once +) + +func (r *RAG) FallbackMessage() string { + return r.fallbackMsg +} + +func Init(c *config.Config, l *slog.Logger, s storage.FullRepo) error { + var err error + ragOnce.Do(func() { + if c == nil || l == nil || s == nil { + return + } + ragInstance, err = New(l, s, c) + }) + return err +} + +func GetInstance() *RAG { + return ragInstance +} + +func (r *RAG) resetIdleTimer() { + r.idleMu.Lock() + defer r.idleMu.Unlock() + if r.idleTimer != nil { + r.idleTimer.Stop() + } + r.idleTimer = time.AfterFunc(r.idleTimeout, func() { + r.freeONNXMemory() + }) +} + +func (r *RAG) freeONNXMemory() { + r.mu.Lock() + defer r.mu.Unlock() + if onnx, ok := r.embedder.(*ONNXEmbedder); ok { + if err := onnx.Destroy(); err != nil { + r.logger.Error("failed to free ONNX memory", "error", err) + } else { + r.logger.Info("freed ONNX VRAM after idle timeout") + } + } +} + +func (r *RAG) Destroy() { + r.mu.Lock() + defer r.mu.Unlock() + if r.idleTimer != nil { + r.idleTimer.Stop() + r.idleTimer = nil + } + if onnx, ok := r.embedder.(*ONNXEmbedder); ok { + if err := onnx.Destroy(); err != nil { + r.logger.Error("failed to destroy ONNX embedder", "error", err) + } + } +} + +// SetEmbedderForTesting replaces the internal embedder with a mock. +// This function is only available when compiling with the "test" build tag. +func (r *RAG) SetEmbedderForTesting(e Embedder) { + r.mu.Lock() + defer r.mu.Unlock() + r.embedder = e +} diff --git a/rag/rag_integration_test.go b/rag/rag_integration_test.go new file mode 100644 index 0000000..f3405eb --- /dev/null +++ b/rag/rag_integration_test.go @@ -0,0 +1,409 @@ +package rag + +import ( + "fmt" + "gf-lt/config" + "gf-lt/models" + "gf-lt/storage" + "log/slog" + "testing" + + _ "github.com/glebarez/go-sqlite" + "github.com/jmoiron/sqlx" +) + +// mockEmbedder returns zero vectors of a fixed dimension. +type mockEmbedder struct { + dim int +} + +func (m *mockEmbedder) Embed(text string) ([]float32, error) { + vec := make([]float32, m.dim) + return vec, nil +} + +func (m *mockEmbedder) EmbedSlice(texts []string) ([][]float32, error) { + vecs := make([][]float32, len(texts)) + for i := range vecs { + vecs[i] = make([]float32, m.dim) + } + return vecs, nil +} + +// dummyStore implements storage.FullRepo with a minimal set of methods. +// Only DB() is used by VectorStorage; other methods return empty values. +type dummyStore struct { + db *sqlx.DB +} + +func (d dummyStore) DB() *sqlx.DB { return d.db } + +// ChatHistory methods +func (d dummyStore) ListChats() ([]models.Chat, error) { return nil, nil } +func (d dummyStore) GetChatByID(id uint32) (*models.Chat, error) { return nil, nil } +func (d dummyStore) GetChatByChar(char string) ([]models.Chat, error) { return nil, nil } +func (d dummyStore) GetLastChat() (*models.Chat, error) { return nil, nil } +func (d dummyStore) GetLastChatByAgent(agent string) (*models.Chat, error) { return nil, nil } +func (d dummyStore) UpsertChat(chat *models.Chat) (*models.Chat, error) { return chat, nil } +func (d dummyStore) RemoveChat(id uint32) error { return nil } +func (d dummyStore) ChatGetMaxID() (uint32, error) { return 0, nil } + +// Memories methods +func (d dummyStore) Memorise(m *models.Memory) (*models.Memory, error) { return m, nil } +func (d dummyStore) Recall(agent, topic string) (string, error) { return "", nil } +func (d dummyStore) RecallTopics(agent string) ([]string, error) { return nil, nil } + +// VectorRepo methods (not used but required by interface) +func (d dummyStore) WriteVector(row *models.VectorRow) error { return nil } +func (d dummyStore) SearchClosest(q []float32, limit int) ([]models.VectorRow, error) { + return nil, nil +} +func (d dummyStore) ListFiles() ([]string, error) { return nil, nil } +func (d dummyStore) RemoveEmbByFileName(filename string) error { return nil } + +var _ storage.FullRepo = dummyStore{} + +// setupTestRAG creates an in‑memory SQLite database, creates the necessary tables, +// inserts the provided chunks, and returns a RAG instance with a mock embedder. +func setupTestRAG(t *testing.T, chunks []*models.VectorRow) (*RAG, error) { + t.Helper() + db, err := sqlx.Open("sqlite", ":memory:") + if err != nil { + return nil, fmt.Errorf("open in‑memory db: %w", err) + } + // Create the required tables (embeddings_768 and fts_embeddings). + // Use the same schema as production. + _, err = db.Exec(` + CREATE TABLE embeddings_768 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '' + ); + `) + if err != nil { + return nil, fmt.Errorf("create embeddings table: %w", err) + } + _, err = db.Exec(` + CREATE VIRTUAL TABLE fts_embeddings USING fts5( + slug UNINDEXED, + raw_text, + filename UNINDEXED, + embedding_size UNINDEXED, + tokenize='porter unicode61' + ); + `) + if err != nil { + return nil, fmt.Errorf("create FTS table: %w", err) + } + // Create a logger that discards output. + logger := slog.New(slog.NewTextHandler(nil, &slog.HandlerOptions{Level: slog.LevelError})) + store := dummyStore{db: db} + // Create config with embedding dimension 768. + cfg := &config.Config{ + EmbedDims: 768, + RAGWordLimit: 250, + RAGOverlapWords: 25, + RAGBatchSize: 1, + } + // Create a RAG instance using New, which will create an embedder based on config. + // We'll override the embedder afterwards via reflection. + rag, err := New(logger, store, cfg) + if err != nil { + return nil, fmt.Errorf("create RAG: %w", err) + } + // Replace the embedder with our mock. + rag.SetEmbedderForTesting(&mockEmbedder{dim: cfg.EmbedDims}) + // Insert the provided chunks using the storage directly. + if len(chunks) > 0 { + // Ensure each chunk has embeddings of correct dimension (zero vector). + for _, chunk := range chunks { + if len(chunk.Embeddings) != cfg.EmbedDims { + chunk.Embeddings = make([]float32, cfg.EmbedDims) + } + } + err = rag.storage.WriteVectors(chunks) + if err != nil { + return nil, fmt.Errorf("write test chunks: %w", err) + } + } + return rag, nil +} + +// createTestChunks returns a slice of VectorRow representing the target chunk +// (kjv_bible.epub_1786_0), several bald‑related noise chunks, and unrelated chunks. +func createTestChunks() []*models.VectorRow { + // Target chunk: 2 Kings 2:23‑24 containing "bald head" and "two she bears". + targetRaw := `And he said, Ye shall not send. + + +2:17 And when they urged him till he was ashamed, he said, Send. They sent +therefore fifty men; and they sought three days, but found him not. + + +2:18 And when they came again to him, (for he tarried at Jericho,) he said unto +them, Did I not say unto you, Go not? 2:19 And the men of the city said unto +Elisha, Behold, I pray thee, the situation of this city is pleasant, as my lord +seeth: but the water is naught, and the ground barren. + + +2:20 And he said, Bring me a new cruse, and put salt therein. And they brought +it to him. + + +2:21 And he went forth unto the spring of the waters, and cast the salt in +there, and said, Thus saith the LORD, I have healed these waters; there shall +not be from thence any more death or barren land. + + +2:22 So the waters were healed unto this day, according to the saying of Elisha +which he spake. + + +2:23 And he went up from thence unto Bethel: and as he was going up by the way, +there came forth little children out of the city, and mocked him, and said unto +him, Go up, thou bald head; go up, thou bald head. + + +2:24 And he turned back, and looked on them, and cursed them in the name of the +LORD. And there came forth two she bears out of the wood, and tare forty and +two children of them.` + // Noise chunk 1: Leviticus containing "bald locust" + noise1Raw := `11:12 Whatsoever hath no fins nor scales in the waters, that shall be an +abomination unto you. + + +11:13 And these are they which ye shall have in abomination among the fowls; +they shall not be eaten, they are an abomination: the eagle, and the ossifrage, +and the ospray, 11:14 And the vulture, and the kite after his kind; 11:15 Every +raven after his kind; 11:16 And the owl, and the night hawk, and the cuckow, +and the hawk after his kind, 11:17 And the little owl, and the cormorant, and +the great owl, 11:18 And the swan, and the pelican, and the gier eagle, 11:19 +And the stork, the heron after her kind, and the lapwing, and the bat. + + +11:20 All fowls that creep, going upon all four, shall be an abomination unto +you. + + +11:21 Yet these may ye eat of every flying creeping thing that goeth upon all +four, which have legs above their feet, to leap withal upon the earth; 11:22 +Even these of them ye may eat; the locust after his kind, and the bald locust +after his kind, and the beetle after his kind, and the grasshopper after his +kind. + + +11:23 But all other flying creeping things, which have four feet, shall be an +abomination unto you. + + +11:24 And for these ye shall be unclean: whosoever toucheth the carcase of them +shall be unclean until the even.` + // Noise chunk 2: Leviticus containing "bald" + noise2Raw := `11:13 And these are they which ye shall have in abomination among the fowls; +they shall not be eaten, they are an abomination: the eagle, and the ossifrage, +and the ospray, 11:14 And the vulture, and the kite after his kind; 11:15 Every +raven after his kind; 11:16 And the owl, and the night hawk, and the cuckow, +and the hawk after his kind, 11:17 And the little owl, and the cormorant, and +the great owl, 11:18 And the swan, and the pelican, and the gier eagle, 11:19 +And the stork, the heron after her kind, and the lapwing, and the bat. + + +11:20 All fowls that creep, going upon all four, shall be an abomination unto +you. + + +11:21 Yet these may ye eat of every flying creeping thing that goeth upon all +four, which have legs above their feet, to leap withal upon the earth; 11:22 +Even these of them ye may eat; the locust after his kind, and the bald locust +after his kind, and the beetle after his kind, and the grasshopper after his +kind. + + +11:23 But all other flying creeping things, which have four feet, shall be an +abomination unto you. + + +11:24 And for these ye shall be unclean: whosoever toucheth the carcase of them +shall be unclean until the even.` + // Additional Leviticus noise chunks (simulating 28 bald-related chunks) + // Using variations of the same text with different slugs + leviticusSlugs := []string{ + "kjv_bible.epub_564_0", + "kjv_bible.epub_565_0", + "kjv_bible.epub_579_0", + "kjv_bible.epub_580_0", + "kjv_bible.epub_581_0", + "kjv_bible.epub_582_0", + "kjv_bible.epub_583_0", + "kjv_bible.epub_584_0", + "kjv_bible.epub_585_0", + "kjv_bible.epub_586_0", + "kjv_bible.epub_587_0", + "kjv_bible.epub_588_0", + "kjv_bible.epub_589_0", + "kjv_bible.epub_590_0", + } + leviticusTexts := []string{ + noise1Raw, + noise2Raw, + `13:40 And the man whose hair is fallen off his head, he is bald; yet is he +clean. + + +13:41 And he that hath his hair fallen off from the part of his head toward his +face, he is forehead bald; yet is he clean.`, + `13:42 And if there be in the bald head, or bald forehead, a white reddish sore; +it is a leprosy sprung up in his bald head, or his bald forehead.`, + `13:43 Then the priest shall look upon it: and, behold, if the rising of the +sore be white reddish in his bald head, or in his bald forehead, as the leprosy +appearedh in the skin of the flesh;`, + `13:44 He is a leprous man, he is unclean: the priest shall pronounce him utterly +unclean; his plague is in his head.`, + `13:45 And the leper in whom the plague is, his clothes shall be rent, and his +head bare, and he shall put a covering upon his upper lip, and shall cry, +Unclean, unclean.`, + `13:46 All the days wherein the plague shall be in him he shall be defiled; he +is unclean: he shall dwell alone; without the camp shall his habitation be.`, + `13:47 The garment also that the plague of leprosy is in, whether it be a woollen +garment, or a linen garment;`, + `13:48 Whether it be in the warp, or woof; of linen, or of woollen; whether in a +skin, or in any thing made of skin;`, + `13:49 And if the plague be greenish or reddish in the garment, or in the skin, +either in the warp, or in the woof, or in any thing of skin; it is a plague of +leprosy, and shall be shewed unto the priest:`, + `13:50 And the priest shall look upon the plague, and shut up it that hath the +plague seven days:`, + `13:51 And he shall look on the plague on the seventh day: if the plague be spread +in the garment, either in the warp, or in the woof, or in a skin, or in any work +that is made of skin; the plague is a fretting leprosy; it is unclean.`, + `13:52 He shall therefore burn that garment, whether warp or woof, in woollen or +in linen, or any thing of skin, wherein the plague is: for it is a fretting +leprosy; it shall be burnt in the fire.`, + } + // Unrelated chunk 1: ghost_7.txt_777_0 + unrelated1Raw := `Doesn’t he have any pride as a hunter?! + +I didn’t see what other choice I had. I would just have to grovel and be ready to flee at any given moment. +The Hidden Curse clan house was in the central region of the imperial capital. It was a high-class area with extraordinary property values that hosted the residences of people like Lord Gladis. This district was near the Imperial Castle, though “near” was a +relative term as it was still a few kilometers away. + +The clan house was made of brick and conformed to an older style of architecture.` + // Unrelated chunk 2: ghost_7.txt_778_0 + unrelated2Raw := `I would just have to grovel and be ready to flee at any given moment. +The Hidden Curse clan house was in the central region of the imperial capital. It was a high-class area with extraordinary property values that hosted the residences of people like Lord Gladis. This district was near the Imperial Castle, though “near” was a +relative term as it was still a few kilometers away. + +The clan house was made of brick and conformed to an older style of architecture. Nearly everyone knew about this mansion and its clock tower. It stood tall over the neighboring mansions and rumor had it that you could see the whole capital from the top. It +spoke to this clan’s renown and history that they were able to get away with building something that dwarfed the mansions of the nobility.` + + chunks := []*models.VectorRow{ + { + Slug: "kjv_bible.epub_1786_0", + RawText: targetRaw, + FileName: "kjv_bible.epub", + Embeddings: nil, // will be filled with zero vector later + }, + } + // Add Leviticus noise chunks + for i, slug := range leviticusSlugs { + text := leviticusTexts[i%len(leviticusTexts)] + chunks = append(chunks, &models.VectorRow{ + Slug: slug, + RawText: text, + FileName: "kjv_bible.epub", + Embeddings: nil, + }) + } + // Add unrelated chunks + chunks = append(chunks, + &models.VectorRow{ + Slug: "ghost_7.txt_777_0", + RawText: unrelated1Raw, + FileName: "ghost_7.txt", + Embeddings: nil, + }, + &models.VectorRow{ + Slug: "ghost_7.txt_778_0", + RawText: unrelated2Raw, + FileName: "ghost_7.txt", + Embeddings: nil, + }, + ) + return chunks +} +func assertTargetInTopN(t *testing.T, results []models.VectorRow, topN int) bool { + t.Helper() + for i, row := range results { + if i >= topN { + break + } + if row.Slug == "kjv_bible.epub_1786_0" { + return true + } + } + return false +} + +func TestBiblicalQuery(t *testing.T) { + chunks := createTestChunks() + rag, err := setupTestRAG(t, chunks) + if err != nil { + t.Fatalf("setup failed: %v", err) + } + query := "bald prophet and two she bears" + results, err := rag.Search(query, 10) + if err != nil { + t.Fatalf("search failed: %v", err) + } + // The target chunk should be in the top results. + if !assertTargetInTopN(t, results, 5) { + t.Errorf("target chunk not found in top 5 results for query %q", query) + t.Logf("results slugs: %v", func() []string { + slugs := make([]string, len(results)) + for i, r := range results { + slugs[i] = r.Slug + } + return slugs + }()) + } +} + +func TestQueryVariations(t *testing.T) { + chunks := createTestChunks() + rag, err := setupTestRAG(t, chunks) + if err != nil { + t.Fatalf("setup failed: %v", err) + } + tests := []struct { + name string + query string + topN int + }{ + {"she bears", "she bears", 5}, + {"bald head", "bald head", 5}, + {"two she bears out of the wood", "two she bears out of the wood", 5}, + {"bald prophet", "bald prophet", 10}, + {"go up thou bald head", "\"go up thou bald head\"", 5}, + {"two she bears", "\"two she bears\"", 5}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + results, err := rag.Search(tt.query, 10) + if err != nil { + t.Fatalf("search failed: %v", err) + } + if !assertTargetInTopN(t, results, tt.topN) { + t.Errorf("target chunk not found in top %d results for query %q", tt.topN, tt.query) + t.Logf("results slugs: %v", func() []string { + slugs := make([]string, len(results)) + for i, r := range results { + slugs[i] = r.Slug + } + return slugs + }()) + } + }) + } +} diff --git a/rag/rag_real_test.go b/rag/rag_real_test.go new file mode 100644 index 0000000..87f6906 --- /dev/null +++ b/rag/rag_real_test.go @@ -0,0 +1,131 @@ +package rag + +import ( + "gf-lt/config" + "gf-lt/storage" + "log/slog" + "os" + "path/filepath" + "testing" +) + +func TestRealBiblicalQuery(t *testing.T) { + if testing.Short() { + t.Skip("skipping real embedder test in short mode") + } + // Check if the embedder model exists + modelPath := filepath.Join("..", "onnx", "embedgemma", "model_q4.onnx") + if _, err := os.Stat(modelPath); os.IsNotExist(err) { + t.Skipf("embedder model not found at %s; skipping real embedder test", modelPath) + } + tokenizerPath := filepath.Join("..", "onnx", "embedgemma", "tokenizer.json") + dbPath := filepath.Join("..", "gflt.db") + if _, err := os.Stat(dbPath); os.IsNotExist(err) { + t.Skipf("database not found at %s; skipping real embedder test", dbPath) + } + cfg := &config.Config{ + EmbedModelPath: modelPath, + EmbedTokenizerPath: tokenizerPath, + EmbedDims: 768, + RAGWordLimit: 250, + RAGOverlapWords: 25, + RAGBatchSize: 1, + } + logger := slog.New(slog.NewTextHandler(nil, &slog.HandlerOptions{Level: slog.LevelError})) + store := storage.NewProviderSQL(dbPath, logger) + if store == nil { + t.Fatal("failed to create storage provider") + } + rag, err := New(logger, store, cfg) + if err != nil { + t.Fatalf("failed to create RAG instance: %v", err) + } + t.Cleanup(func() { rag.Destroy() }) + + query := "bald prophet and two she bears" + results, err := rag.Search(query, 30) + if err != nil { + t.Fatalf("search failed: %v", err) + } + found := false + for i, row := range results { + if row.Slug == "kjv_bible.epub_1786_0" { + found = true + t.Logf("target chunk found at rank %d", i+1) + break + } + } + if !found { + t.Errorf("target chunk not found in search results for query %q", query) + t.Logf("results slugs:") + for i, r := range results { + t.Logf("%d: %s", i+1, r.Slug) + } + } +} + +func TestRealQueryVariations(t *testing.T) { + if testing.Short() { + t.Skip("skipping real embedder test in short mode") + } + modelPath := filepath.Join("..", "onnx", "embedgemma", "model_q4.onnx") + if _, err := os.Stat(modelPath); os.IsNotExist(err) { + t.Skipf("embedder model not found at %s; skipping real embedder test", modelPath) + } + tokenizerPath := filepath.Join("..", "onnx", "embedgemma", "tokenizer.json") + dbPath := filepath.Join("..", "gflt.db") + if _, err := os.Stat(dbPath); os.IsNotExist(err) { + t.Skipf("database not found at %s; skipping real embedder test", dbPath) + } + cfg := &config.Config{ + EmbedModelPath: modelPath, + EmbedTokenizerPath: tokenizerPath, + EmbedDims: 768, + RAGWordLimit: 250, + RAGOverlapWords: 25, + RAGBatchSize: 1, + } + logger := slog.New(slog.NewTextHandler(nil, &slog.HandlerOptions{Level: slog.LevelError})) + store := storage.NewProviderSQL(dbPath, logger) + if store == nil { + t.Fatal("failed to create storage provider") + } + rag, err := New(logger, store, cfg) + if err != nil { + t.Fatalf("failed to create RAG instance: %v", err) + } + t.Cleanup(func() { rag.Destroy() }) + + tests := []struct { + name string + query string + }{ + {"she bears", "she bears"}, + {"bald head", "bald head"}, + {"two she bears out of the wood", "two she bears out of the wood"}, + {"bald prophet", "bald prophet"}, + {"go up thou bald head", "\"go up thou bald head\""}, + {"two she bears", "\"two she bears\""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + results, err := rag.Search(tt.query, 10) + if err != nil { + t.Fatalf("search failed: %v", err) + } + found := false + for _, row := range results { + if row.Slug == "kjv_bible.epub_1786_0" { + found = true + break + } + } + if !found { + t.Errorf("target chunk not found for query %q", tt.query) + for i, r := range results { + t.Logf("%d: %s", i+1, r.Slug) + } + } + }) + } +} diff --git a/rag/rag_test.go b/rag/rag_test.go new file mode 100644 index 0000000..4944007 --- /dev/null +++ b/rag/rag_test.go @@ -0,0 +1,155 @@ +package rag + +import ( + "testing" +) + +func TestDetectPhrases(t *testing.T) { + tests := []struct { + query string + expect []string + }{ + { + query: "bald prophet and two she bears", + expect: []string{"bald prophet", "two she", "two she bears", "she bears"}, + }, + { + query: "she bears", + expect: []string{"she bears"}, + }, + { + query: "the quick brown fox", + expect: []string{"quick brown", "quick brown fox", "brown fox"}, + }, + { + query: "in the house", // stop words + expect: []string{}, // "in" and "the" are stop words + }, + { + query: "a", // short + expect: []string{}, + }, + } + + for _, tt := range tests { + got := detectPhrases(tt.query) + if len(got) != len(tt.expect) { + t.Errorf("detectPhrases(%q) = %v, want %v", tt.query, got, tt.expect) + continue + } + for i := range got { + if got[i] != tt.expect[i] { + t.Errorf("detectPhrases(%q) = %v, want %v", tt.query, got, tt.expect) + break + } + } + } +} + +func TestCountPhraseMatches(t *testing.T) { + tests := []struct { + text string + query string + expect int + }{ + { + text: "two she bears came out of the wood", + query: "she bears", + expect: 1, + }, + { + text: "bald head and she bears", + query: "bald prophet and two she bears", + expect: 1, // only "she bears" matches + }, + { + text: "no match here", + query: "she bears", + expect: 0, + }, + { + text: "she bears and bald prophet", + query: "bald prophet she bears", + expect: 2, // "she bears" and "bald prophet" + }, + } + + for _, tt := range tests { + got := countPhraseMatches(tt.text, tt.query) + if got != tt.expect { + t.Errorf("countPhraseMatches(%q, %q) = %d, want %d", tt.text, tt.query, got, tt.expect) + } + } +} + +func TestAreSlugsAdjacent(t *testing.T) { + tests := []struct { + slug1 string + slug2 string + expect bool + }{ + { + slug1: "kjv_bible.epub_1786_0", + slug2: "kjv_bible.epub_1787_0", + expect: true, + }, + { + slug1: "kjv_bible.epub_1787_0", + slug2: "kjv_bible.epub_1786_0", + expect: true, + }, + { + slug1: "kjv_bible.epub_1786_0", + slug2: "kjv_bible.epub_1788_0", + expect: false, + }, + { + slug1: "otherfile.txt_1_0", + slug2: "kjv_bible.epub_1786_0", + expect: false, + }, + { + slug1: "file_1_0", + slug2: "file_1_1", + expect: true, + }, + { + slug1: "file_1_0", + slug2: "file_2_0", // different batch + expect: true, // sequential batches with same chunk index are adjacent + }, + } + + for _, tt := range tests { + got := areSlugsAdjacent(tt.slug1, tt.slug2) + if got != tt.expect { + t.Errorf("areSlugsAdjacent(%q, %q) = %v, want %v", tt.slug1, tt.slug2, got, tt.expect) + } + } +} + +func TestParseSlugIndices(t *testing.T) { + tests := []struct { + slug string + wantBatch int + wantChunk int + wantOk bool + }{ + {"kjv_bible.epub_1786_0", 1786, 0, true}, + {"file_1_5", 1, 5, true}, + {"no_underscore", 0, 0, false}, + {"file_abc_def", 0, 0, false}, + {"file_123_456_extra", 456, 0, false}, // regex matches last two numbers + } + + for _, tt := range tests { + batch, chunk, ok := parseSlugIndices(tt.slug) + if ok != tt.wantOk { + t.Errorf("parseSlugIndices(%q) ok = %v, want %v", tt.slug, ok, tt.wantOk) + continue + } + if ok && (batch != tt.wantBatch || chunk != tt.wantChunk) { + t.Errorf("parseSlugIndices(%q) = (%d, %d), want (%d, %d)", tt.slug, batch, chunk, tt.wantBatch, tt.wantChunk) + } + } +} diff --git a/rag/storage.go b/rag/storage.go new file mode 100644 index 0000000..a53f767 --- /dev/null +++ b/rag/storage.go @@ -0,0 +1,446 @@ +package rag + +import ( + "database/sql" + "encoding/binary" + "fmt" + "gf-lt/models" + "gf-lt/storage" + "log/slog" + "sort" + "strings" + "unsafe" + + "github.com/jmoiron/sqlx" +) + +// VectorStorage handles storing and retrieving vectors from SQLite +type VectorStorage struct { + logger *slog.Logger + sqlxDB *sqlx.DB + store storage.FullRepo +} + +func NewVectorStorage(logger *slog.Logger, store storage.FullRepo) *VectorStorage { + return &VectorStorage{ + logger: logger, + sqlxDB: store.DB(), // Use the new DB() method + store: store, + } +} + +// SerializeVector converts []float32 to binary blob +func SerializeVector(vec []float32) []byte { + buf := make([]byte, len(vec)*4) // 4 bytes per float32 + for i, v := range vec { + binary.LittleEndian.PutUint32(buf[i*4:], mathFloat32bits(v)) + } + return buf +} + +// DeserializeVector converts binary blob back to []float32 +func DeserializeVector(data []byte) []float32 { + count := len(data) / 4 + vec := make([]float32, count) + for i := 0; i < count; i++ { + vec[i] = mathBitsToFloat32(binary.LittleEndian.Uint32(data[i*4:])) + } + return vec +} + +// mathFloat32bits and mathBitsToFloat32 are helpers to convert between float32 and uint32 +func mathFloat32bits(f float32) uint32 { + return binary.LittleEndian.Uint32((*(*[4]byte)(unsafe.Pointer(&f)))[:4]) +} + +func mathBitsToFloat32(b uint32) float32 { + return *(*float32)(unsafe.Pointer(&b)) +} + +// WriteVector stores an embedding vector in the database +func (vs *VectorStorage) WriteVector(row *models.VectorRow) error { + tableName, err := vs.getTableName(row.Embeddings) + if err != nil { + return err + } + embeddingSize := len(row.Embeddings) + // Start transaction + tx, err := vs.sqlxDB.Beginx() + if err != nil { + return err + } + defer func() { + if err != nil { + _ = tx.Rollback() + } + }() + + // Serialize the embeddings to binary + serializedEmbeddings := SerializeVector(row.Embeddings) + query := fmt.Sprintf( + "INSERT INTO %s (embeddings, slug, raw_text, filename) VALUES (?, ?, ?, ?)", + tableName, + ) + if _, err := tx.Exec(query, serializedEmbeddings, row.Slug, row.RawText, row.FileName); err != nil { + vs.logger.Error("failed to write vector", "error", err, "slug", row.Slug) + return err + } + // Insert into FTS table + ftsQuery := `INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) VALUES (?, ?, ?, ?)` + if _, err := tx.Exec(ftsQuery, row.Slug, row.RawText, row.FileName, embeddingSize); err != nil { + vs.logger.Error("failed to write to FTS table", "error", err, "slug", row.Slug) + return err + } + err = tx.Commit() + if err != nil { + vs.logger.Error("failed to commit transaction", "error", err) + return err + } + return nil +} + +// WriteVectors stores multiple embedding vectors in a single transaction +func (vs *VectorStorage) WriteVectors(rows []*models.VectorRow) error { + if len(rows) == 0 { + return nil + } + // SQLite has limit of 999 parameters per statement, each row uses 4 parameters + const maxBatchSize = 200 // 200 * 4 = 800 < 999 + if len(rows) > maxBatchSize { + // Process in chunks + for i := 0; i < len(rows); i += maxBatchSize { + end := i + maxBatchSize + if end > len(rows) { + end = len(rows) + } + if err := vs.WriteVectors(rows[i:end]); err != nil { + return err + } + } + return nil + } + // All rows should have same embedding size (same model) + firstSize := len(rows[0].Embeddings) + for i, row := range rows { + if len(row.Embeddings) != firstSize { + return fmt.Errorf("embedding size mismatch: row %d has size %d, expected %d", i, len(row.Embeddings), firstSize) + } + } + tableName, err := vs.getTableName(rows[0].Embeddings) + if err != nil { + return err + } + // Start transaction + tx, err := vs.sqlxDB.Beginx() + if err != nil { + return err + } + defer func() { + if err != nil { + _ = tx.Rollback() + } + }() + + // Build batch insert for embeddings table + embeddingPlaceholders := make([]string, 0, len(rows)) + embeddingArgs := make([]any, 0, len(rows)*4) + for _, row := range rows { + embeddingPlaceholders = append(embeddingPlaceholders, "(?, ?, ?, ?)") + embeddingArgs = append(embeddingArgs, SerializeVector(row.Embeddings), row.Slug, row.RawText, row.FileName) + } + embeddingQuery := fmt.Sprintf( + "INSERT INTO %s (embeddings, slug, raw_text, filename) VALUES %s", + tableName, + strings.Join(embeddingPlaceholders, ", "), + ) + if _, err := tx.Exec(embeddingQuery, embeddingArgs...); err != nil { + vs.logger.Error("failed to write vectors batch", "error", err, "batch_size", len(rows)) + return err + } + // Build batch insert for FTS table + ftsPlaceholders := make([]string, 0, len(rows)) + ftsArgs := make([]any, 0, len(rows)*4) + embeddingSize := len(rows[0].Embeddings) + for _, row := range rows { + ftsPlaceholders = append(ftsPlaceholders, "(?, ?, ?, ?)") + ftsArgs = append(ftsArgs, row.Slug, row.RawText, row.FileName, embeddingSize) + } + ftsQuery := "INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) VALUES " + + strings.Join(ftsPlaceholders, ", ") + if _, err := tx.Exec(ftsQuery, ftsArgs...); err != nil { + vs.logger.Error("failed to write FTS batch", "error", err, "batch_size", len(rows)) + return err + } + err = tx.Commit() + if err != nil { + vs.logger.Error("failed to commit transaction", "error", err) + return err + } + vs.logger.Debug("wrote vectors batch", "batch_size", len(rows)) + return nil +} + +// getTableName determines which table to use based on embedding size +func (vs *VectorStorage) getTableName(emb []float32) (string, error) { + size := len(emb) + + // Check if we support this embedding size + supportedSizes := map[int]bool{ + 384: true, + 768: true, + 1024: true, + 1536: true, + 2048: true, + 3072: true, + 4096: true, + 5120: true, + } + if supportedSizes[size] { + return fmt.Sprintf("embeddings_%d", size), nil + } + return "", fmt.Errorf("no table for embedding size of %d", size) +} + +// SearchClosest finds vectors closest to the query vector using efficient cosine similarity calculation +func (vs *VectorStorage) SearchClosest(query []float32, limit int) ([]models.VectorRow, error) { + if limit <= 0 { + limit = 10 + } + tableName, err := vs.getTableName(query) + if err != nil { + return nil, err + } + querySQL := "SELECT embeddings, slug, raw_text, filename FROM " + tableName + rows, err := vs.sqlxDB.Query(querySQL) + if err != nil { + return nil, err + } + defer rows.Close() + type SearchResult struct { + vector models.VectorRow + distance float32 + } + var topResults []SearchResult + for rows.Next() { + var ( + embeddingsBlob []byte + slug, rawText, fileName string + ) + + if err := rows.Scan(&embeddingsBlob, &slug, &rawText, &fileName); err != nil { + vs.logger.Error("failed to scan row", "error", err) + continue + } + storedEmbeddings := DeserializeVector(embeddingsBlob) + similarity := cosineSimilarity(query, storedEmbeddings) + distance := 1 - similarity + + result := SearchResult{ + vector: models.VectorRow{ + Embeddings: storedEmbeddings, + Slug: slug, + RawText: rawText, + FileName: fileName, + }, + distance: distance, + } + + topResults = append(topResults, result) + sort.Slice(topResults, func(i, j int) bool { + return topResults[i].distance < topResults[j].distance + }) + if len(topResults) > limit { + topResults = topResults[:limit] + } + } + results := make([]models.VectorRow, 0, len(topResults)) + for _, result := range topResults { + result.vector.Distance = result.distance + results = append(results, result.vector) + } + return results, nil +} + +// GetVectorBySlug retrieves a vector row by its slug +func (vs *VectorStorage) GetVectorBySlug(slug string) (*models.VectorRow, error) { + embeddingSizes := []int{384, 768, 1024, 1536, 2048, 3072, 4096, 5120} + for _, size := range embeddingSizes { + table := fmt.Sprintf("embeddings_%d", size) + query := fmt.Sprintf("SELECT embeddings, slug, raw_text, filename FROM %s WHERE slug = ?", table) + row := vs.sqlxDB.QueryRow(query, slug) + var ( + embeddingsBlob []byte + retrievedSlug, rawText, fileName string + ) + if err := row.Scan(&embeddingsBlob, &retrievedSlug, &rawText, &fileName); err != nil { + // No row in this table, continue to next size + continue + } + storedEmbeddings := DeserializeVector(embeddingsBlob) + return &models.VectorRow{ + Embeddings: storedEmbeddings, + Slug: retrievedSlug, + RawText: rawText, + FileName: fileName, + }, nil + } + return nil, fmt.Errorf("vector with slug %s not found", slug) +} + +// SearchKeyword performs full-text search using FTS5 +func (vs *VectorStorage) SearchKeyword(query string, limit int) ([]models.VectorRow, error) { + // Use FTS5 bm25 ranking. bm25 returns negative values where more negative is better. + // We'll order by bm25 (ascending) and limit. + ftsQuery := `SELECT slug, raw_text, filename, bm25(fts_embeddings) as score + FROM fts_embeddings + WHERE fts_embeddings MATCH ? + ORDER BY score + LIMIT ?` + + // Try original query first + rows, err := vs.sqlxDB.Query(ftsQuery, query, limit) + if err != nil { + return nil, fmt.Errorf("FTS search failed: %w", err) + } + results, err := vs.scanRows(rows) + rows.Close() + if err != nil { + return nil, err + } + + // If no results and query contains multiple terms, try OR fallback + if len(results) == 0 && strings.Contains(query, " ") && !strings.Contains(strings.ToUpper(query), " OR ") { + // Build OR query: term1 OR term2 OR term3 + terms := strings.Fields(query) + if len(terms) > 1 { + orQuery := strings.Join(terms, " OR ") + rows, err := vs.sqlxDB.Query(ftsQuery, orQuery, limit) + if err != nil { + // Return original empty results rather than error + return results, nil + } + orResults, err := vs.scanRows(rows) + rows.Close() + if err == nil { + results = orResults + } + } + } + return results, nil +} + +// scanRows converts SQL rows to VectorRow slice +func (vs *VectorStorage) scanRows(rows *sql.Rows) ([]models.VectorRow, error) { + var results []models.VectorRow + for rows.Next() { + var slug, rawText, fileName string + var score float64 + if err := rows.Scan(&slug, &rawText, &fileName, &score); err != nil { + vs.logger.Error("failed to scan FTS row", "error", err) + continue + } + // Convert BM25 score to distance-like metric (lower is better) + // BM25 is negative, more negative is better. Keep as negative. + distance := float32(score) // Keep negative, more negative is better + // No clamping needed; negative distances are fine + results = append(results, models.VectorRow{ + Slug: slug, + RawText: rawText, + FileName: fileName, + Distance: distance, + }) + } + return results, nil +} + +// ListFiles returns a list of all loaded files +func (vs *VectorStorage) ListFiles() ([]string, error) { + fileLists := make([][]string, 0) + // Query all supported tables and combine results + embeddingSizes := []int{384, 768, 1024, 1536, 2048, 3072, 4096, 5120} + for _, size := range embeddingSizes { + table := fmt.Sprintf("embeddings_%d", size) + query := "SELECT DISTINCT filename FROM " + table + rows, err := vs.sqlxDB.Query(query) + if err != nil { + // Continue if one table doesn't exist + continue + } + + var files []string + for rows.Next() { + var filename string + if err := rows.Scan(&filename); err != nil { + continue + } + files = append(files, filename) + } + rows.Close() + + fileLists = append(fileLists, files) + } + + // Combine and deduplicate + fileSet := make(map[string]bool) + var allFiles []string + for _, files := range fileLists { + for _, file := range files { + if !fileSet[file] { + fileSet[file] = true + allFiles = append(allFiles, file) + } + } + } + return allFiles, nil +} + +// RemoveEmbByFileName removes all embeddings associated with a specific filename +func (vs *VectorStorage) RemoveEmbByFileName(filename string) error { + var errors []string + // Delete from FTS table first + if _, err := vs.sqlxDB.Exec("DELETE FROM fts_embeddings WHERE filename = ?", filename); err != nil { + errors = append(errors, err.Error()) + } + embeddingSizes := []int{384, 768, 1024, 1536, 2048, 3072, 4096, 5120} + for _, size := range embeddingSizes { + table := fmt.Sprintf("embeddings_%d", size) + query := fmt.Sprintf("DELETE FROM %s WHERE filename = ?", table) + if _, err := vs.sqlxDB.Exec(query, filename); err != nil { + errors = append(errors, err.Error()) + } + } + if len(errors) > 0 { + return fmt.Errorf("errors occurred: %s", strings.Join(errors, "; ")) + } + return nil +} + +// cosineSimilarity calculates the cosine similarity between two vectors +func cosineSimilarity(a, b []float32) float32 { + if len(a) != len(b) { + return 0.0 + } + var dotProduct, normA, normB float32 + for i := 0; i < len(a); i++ { + dotProduct += a[i] * b[i] + normA += a[i] * a[i] + normB += b[i] * b[i] + } + if normA == 0 || normB == 0 { + return 0.0 + } + return dotProduct / (sqrt(normA) * sqrt(normB)) +} + +// sqrt returns the square root of a float32 +func sqrt(f float32) float32 { + // A simple implementation of square root using Newton's method + if f == 0 { + return 0 + } + guess := f / 2 + for i := 0; i < 10; i++ { // 10 iterations should be enough for good precision + guess = (guess + f/guess) / 2 + } + return guess +} @@ -1,10 +1,14 @@ package main import ( - "elefant/models" "encoding/json" + "errors" "fmt" + "gf-lt/models" + "os" "os/exec" + "path" + "path/filepath" "strings" "time" ) @@ -13,18 +17,52 @@ var ( chatMap = make(map[string]*models.Chat) ) -func historyToSJSON(msgs []models.MessagesStory) (string, error) { +func historyToSJSON(msgs []models.RoleMsg) (string, error) { data, err := json.Marshal(msgs) if err != nil { return "", err } if data == nil { - return "", fmt.Errorf("nil data") + return "", errors.New("nil data") } return string(data), nil } -func updateStorageChat(name string, msgs []models.MessagesStory) error { +func exportChat() error { + data, err := json.MarshalIndent(chatBody.Messages, "", " ") + if err != nil { + return err + } + // Ensure the export directory exists + if err := os.MkdirAll(exportDir, 0755); err != nil { + return fmt.Errorf("failed to create export directory %s: %w", exportDir, err) + } + fp := path.Join(exportDir, activeChatName+".json") + return os.WriteFile(fp, data, 0666) +} + +func importChat(filename string) error { + data, err := os.ReadFile(filename) + if err != nil { + return err + } + messages := []models.RoleMsg{} + if err := json.Unmarshal(data, &messages); err != nil { + return err + } + activeChatName = filepath.Base(filename) + if _, ok := chatMap[activeChatName]; !ok { + addNewChat(activeChatName) + } + chatBody.Messages = messages + cfg.AssistantRole = messages[1].Role + if cfg.AssistantRole == cfg.UserRole { + cfg.AssistantRole = messages[2].Role + } + return nil +} + +func updateStorageChat(name string, msgs []models.RoleMsg) error { var err error chat, ok := chatMap[name] if !ok { @@ -37,6 +75,7 @@ func updateStorageChat(name string, msgs []models.MessagesStory) error { return err } chat.UpdatedAt = time.Now() + // if new chat will create id _, err = store.UpsertChat(chat) return err } @@ -46,56 +85,78 @@ func loadHistoryChats() ([]string, error) { if err != nil { return nil, err } - resp := []string{} - for _, chat := range chats { + resp := make([]string, len(chats)) + for i, chat := range chats { if chat.Name == "" { - chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.CreatedAt.Unix()) + chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.Agent) } - resp = append(resp, chat.Name) + resp[i] = chat.Name chatMap[chat.Name] = &chat } return resp, nil } -func loadHistoryChat(chatName string) ([]models.MessagesStory, error) { +func loadHistoryChat(chatName string) ([]models.RoleMsg, error) { chat, ok := chatMap[chatName] if !ok { - err := fmt.Errorf("failed to read chat") + err := errors.New("failed to read chat") logger.Error("failed to read chat", "name", chatName) return nil, err } activeChatName = chatName + cfg.AssistantRole = chat.Agent return chat.ToHistory() } -func loadOldChatOrGetNew() []models.MessagesStory { - newChat := &models.Chat{ - ID: 0, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), +func loadAgentsLastChat(agent string) ([]models.RoleMsg, error) { + chat, err := store.GetLastChatByAgent(agent) + if err != nil { + return nil, err + } + history, err := chat.ToHistory() + if err != nil { + return nil, err } - newChat.Name = fmt.Sprintf("%d_%v", newChat.ID, newChat.CreatedAt.Unix()) + if chat.Name == "" { + logger.Warn("empty chat name", "id", chat.ID) + chat.Name = fmt.Sprintf("%s_%d", chat.Agent, chat.ID) + } + chatMap[chat.Name] = chat + activeChatName = chat.Name + return history, nil +} + +func loadOldChatOrGetNew() []models.RoleMsg { // find last chat chat, err := store.GetLastChat() if err != nil { logger.Warn("failed to load history chat", "error", err) - activeChatName = newChat.Name - chatMap[newChat.Name] = newChat + maxID, err := store.ChatGetMaxID() + if err != nil { + logger.Error("failed to fetch max chat id", "error", err) + } + maxID++ + chat := &models.Chat{ + ID: maxID, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Agent: cfg.AssistantRole, + } + chat.Name = fmt.Sprintf("%s_%v", chat.Agent, chat.ID) + activeChatName = chat.Name + chatMap[chat.Name] = chat return defaultStarter } history, err := chat.ToHistory() if err != nil { logger.Warn("failed to load history chat", "error", err) - activeChatName = newChat.Name - chatMap[newChat.Name] = newChat + activeChatName = chat.Name + chatMap[chat.Name] = chat return defaultStarter } - if chat.Name == "" { - logger.Warn("empty chat name", "id", chat.ID) - chat.Name = fmt.Sprintf("%d_%v", chat.ID, chat.CreatedAt.Unix()) - } chatMap[chat.Name] = chat activeChatName = chat.Name + cfg.AssistantRole = chat.Agent return history } @@ -107,8 +168,3 @@ func copyToClipboard(text string) error { cmd.Stdin = strings.NewReader(text) return cmd.Run() } - -func notifyUser(topic, message string) error { - cmd := exec.Command("notify-send", topic, message) - return cmd.Run() -} diff --git a/storage/memory.go b/storage/memory.go index a7bf8cc..406182f 100644 --- a/storage/memory.go +++ b/storage/memory.go @@ -1,6 +1,6 @@ package storage -import "elefant/models" +import "gf-lt/models" type Memories interface { Memorise(m *models.Memory) (*models.Memory, error) @@ -9,15 +9,23 @@ type Memories interface { } func (p ProviderSQL) Memorise(m *models.Memory) (*models.Memory, error) { - query := "INSERT INTO memories (agent, topic, mind) VALUES (:agent, :topic, :mind) RETURNING *;" + query := ` + INSERT INTO memories (agent, topic, mind) + VALUES (:agent, :topic, :mind) + ON CONFLICT (agent, topic) DO UPDATE + SET mind = excluded.mind, + updated_at = CURRENT_TIMESTAMP + RETURNING *;` stmt, err := p.db.PrepareNamed(query) if err != nil { + p.logger.Error("failed to prepare stmt", "query", query, "error", err) return nil, err } defer stmt.Close() var memory models.Memory err = stmt.Get(&memory, m) if err != nil { + p.logger.Error("failed to upsert memory", "query", query, "error", err) return nil, err } return &memory, nil @@ -28,6 +36,7 @@ func (p ProviderSQL) Recall(agent, topic string) (string, error) { var mind string err := p.db.Get(&mind, query, agent, topic) if err != nil { + p.logger.Error("failed to get memory", "query", query, "error", err) return "", err } return mind, nil @@ -38,6 +47,7 @@ func (p ProviderSQL) RecallTopics(agent string) ([]string, error) { var topics []string err := p.db.Select(&topics, query, agent) if err != nil { + p.logger.Error("failed to get topics", "query", query, "error", err) return nil, err } return topics, nil diff --git a/storage/migrate.go b/storage/migrate.go index d97b99d..38f9854 100644 --- a/storage/migrate.go +++ b/storage/migrate.go @@ -10,16 +10,18 @@ import ( //go:embed migrations/* var migrationsFS embed.FS -func (p *ProviderSQL) Migrate() { +func (p *ProviderSQL) Migrate() error { // Get the embedded filesystem migrationsDir, err := fs.Sub(migrationsFS, "migrations") if err != nil { p.logger.Error("Failed to get embedded migrations directory;", "error", err) + return fmt.Errorf("failed to get embedded migrations directory: %w", err) } // List all .up.sql files files, err := migrationsFS.ReadDir("migrations") if err != nil { p.logger.Error("Failed to read migrations directory;", "error", err) + return fmt.Errorf("failed to read migrations directory: %w", err) } // Execute each .up.sql file for _, file := range files { @@ -27,10 +29,12 @@ func (p *ProviderSQL) Migrate() { err := p.executeMigration(migrationsDir, file.Name()) if err != nil { p.logger.Error("Failed to execute migration %s: %v", file.Name(), err) + return fmt.Errorf("failed to execute migration %s: %w", file.Name(), err) } } } - p.logger.Info("All migrations executed successfully!") + p.logger.Debug("All migrations executed successfully!") + return nil } func (p *ProviderSQL) executeMigration(migrationsDir fs.FS, fileName string) error { @@ -50,7 +54,7 @@ func (p *ProviderSQL) executeMigration(migrationsDir fs.FS, fileName string) err } func (p *ProviderSQL) executeSQL(sqlContent []byte) error { - // Connect to the database (example using a simple connection) + // Execute the migration content using standard database connection _, err := p.db.Exec(string(sqlContent)) if err != nil { return fmt.Errorf("failed to execute SQL: %w", err) diff --git a/storage/migrations/001_init.up.sql b/storage/migrations/001_init.up.sql index 8980ccf..09bb5e6 100644 --- a/storage/migrations/001_init.up.sql +++ b/storage/migrations/001_init.up.sql @@ -2,6 +2,7 @@ CREATE TABLE IF NOT EXISTS chats ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, msgs TEXT NOT NULL, + agent TEXT NOT NULL DEFAULT 'assistant', created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); diff --git a/storage/migrations/002_add_vector.down.sql b/storage/migrations/002_add_vector.down.sql new file mode 100644 index 0000000..a257b11 --- /dev/null +++ b/storage/migrations/002_add_vector.down.sql @@ -0,0 +1,34 @@ +-- Drop vector storage tables +DROP INDEX IF EXISTS idx_embeddings_384_filename; +DROP INDEX IF EXISTS idx_embeddings_768_filename; +DROP INDEX IF EXISTS idx_embeddings_1024_filename; +DROP INDEX IF EXISTS idx_embeddings_1536_filename; +DROP INDEX IF EXISTS idx_embeddings_2048_filename; +DROP INDEX IF EXISTS idx_embeddings_3072_filename; +DROP INDEX IF EXISTS idx_embeddings_4096_filename; +DROP INDEX IF EXISTS idx_embeddings_5120_filename; +DROP INDEX IF EXISTS idx_embeddings_384_slug; +DROP INDEX IF EXISTS idx_embeddings_768_slug; +DROP INDEX IF EXISTS idx_embeddings_1024_slug; +DROP INDEX IF EXISTS idx_embeddings_1536_slug; +DROP INDEX IF EXISTS idx_embeddings_2048_slug; +DROP INDEX IF EXISTS idx_embeddings_3072_slug; +DROP INDEX IF EXISTS idx_embeddings_4096_slug; +DROP INDEX IF EXISTS idx_embeddings_5120_slug; +DROP INDEX IF EXISTS idx_embeddings_384_created_at; +DROP INDEX IF EXISTS idx_embeddings_768_created_at; +DROP INDEX IF EXISTS idx_embeddings_1024_created_at; +DROP INDEX IF EXISTS idx_embeddings_1536_created_at; +DROP INDEX IF EXISTS idx_embeddings_2048_created_at; +DROP INDEX IF EXISTS idx_embeddings_3072_created_at; +DROP INDEX IF EXISTS idx_embeddings_4096_created_at; +DROP INDEX IF EXISTS idx_embeddings_5120_created_at; + +DROP TABLE IF EXISTS embeddings_384; +DROP TABLE IF EXISTS embeddings_768; +DROP TABLE IF EXISTS embeddings_1024; +DROP TABLE IF EXISTS embeddings_1536; +DROP TABLE IF EXISTS embeddings_2048; +DROP TABLE IF EXISTS embeddings_3072; +DROP TABLE IF EXISTS embeddings_4096; +DROP TABLE IF EXISTS embeddings_5120;
\ No newline at end of file diff --git a/storage/migrations/002_add_vector.up.sql b/storage/migrations/002_add_vector.up.sql new file mode 100644 index 0000000..baf703d --- /dev/null +++ b/storage/migrations/002_add_vector.up.sql @@ -0,0 +1,98 @@ +-- Create tables for vector storage (replacing vec0 plugin usage) +CREATE TABLE IF NOT EXISTS embeddings_384 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_768 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_1024 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_1536 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_2048 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_3072 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_4096 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS embeddings_5120 ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + embeddings BLOB NOT NULL, + slug TEXT NOT NULL, + raw_text TEXT NOT NULL, + filename TEXT NOT NULL DEFAULT '', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Indexes for better performance +CREATE INDEX IF NOT EXISTS idx_embeddings_384_filename ON embeddings_384(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_768_filename ON embeddings_768(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_1024_filename ON embeddings_1024(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_1536_filename ON embeddings_1536(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_2048_filename ON embeddings_2048(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_3072_filename ON embeddings_3072(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_4096_filename ON embeddings_4096(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_5120_filename ON embeddings_5120(filename); +CREATE INDEX IF NOT EXISTS idx_embeddings_384_slug ON embeddings_384(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_768_slug ON embeddings_768(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_1024_slug ON embeddings_1024(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_1536_slug ON embeddings_1536(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_2048_slug ON embeddings_2048(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_3072_slug ON embeddings_3072(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_4096_slug ON embeddings_4096(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_5120_slug ON embeddings_5120(slug); +CREATE INDEX IF NOT EXISTS idx_embeddings_384_created_at ON embeddings_384(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_768_created_at ON embeddings_768(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_1024_created_at ON embeddings_1024(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_1536_created_at ON embeddings_1536(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_2048_created_at ON embeddings_2048(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_3072_created_at ON embeddings_3072(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_4096_created_at ON embeddings_4096(created_at); +CREATE INDEX IF NOT EXISTS idx_embeddings_5120_created_at ON embeddings_5120(created_at); diff --git a/storage/migrations/003_add_fts.down.sql b/storage/migrations/003_add_fts.down.sql new file mode 100644 index 0000000..e565fd5 --- /dev/null +++ b/storage/migrations/003_add_fts.down.sql @@ -0,0 +1,2 @@ +-- Drop FTS5 virtual table +DROP TABLE IF EXISTS fts_embeddings;
\ No newline at end of file diff --git a/storage/migrations/003_add_fts.up.sql b/storage/migrations/003_add_fts.up.sql new file mode 100644 index 0000000..114586a --- /dev/null +++ b/storage/migrations/003_add_fts.up.sql @@ -0,0 +1,15 @@ +-- Create FTS5 virtual table for full-text search +CREATE VIRTUAL TABLE IF NOT EXISTS fts_embeddings USING fts5( + slug UNINDEXED, + raw_text, + filename UNINDEXED, + embedding_size UNINDEXED, + tokenize='porter unicode61' -- Use porter stemmer and unicode61 tokenizer +); + +-- Create triggers to maintain FTS table when embeddings are inserted/deleted +-- Note: We'll handle inserts/deletes programmatically for simplicity +-- but triggers could be added here if needed. + +-- Indexes for performance (FTS5 manages its own indexes) +-- No additional indexes needed for FTS5 virtual table.
\ No newline at end of file diff --git a/storage/migrations/004_populate_fts.down.sql b/storage/migrations/004_populate_fts.down.sql new file mode 100644 index 0000000..2b5c756 --- /dev/null +++ b/storage/migrations/004_populate_fts.down.sql @@ -0,0 +1,2 @@ +-- Clear FTS table (optional) +DELETE FROM fts_embeddings;
\ No newline at end of file diff --git a/storage/migrations/004_populate_fts.up.sql b/storage/migrations/004_populate_fts.up.sql new file mode 100644 index 0000000..1d1b16a --- /dev/null +++ b/storage/migrations/004_populate_fts.up.sql @@ -0,0 +1,26 @@ +-- Populate FTS table with existing embeddings +DELETE FROM fts_embeddings; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 384 FROM embeddings_384; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 768 FROM embeddings_768; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 1024 FROM embeddings_1024; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 1536 FROM embeddings_1536; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 2048 FROM embeddings_2048; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 3072 FROM embeddings_3072; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 4096 FROM embeddings_4096; + +INSERT INTO fts_embeddings (slug, raw_text, filename, embedding_size) +SELECT slug, raw_text, filename, 5120 FROM embeddings_5120;
\ No newline at end of file diff --git a/storage/storage.go b/storage/storage.go index 67b8dd8..57631da 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -1,19 +1,28 @@ package storage import ( - "elefant/models" + "gf-lt/models" "log/slog" _ "github.com/glebarez/go-sqlite" "github.com/jmoiron/sqlx" ) +type FullRepo interface { + ChatHistory + Memories + VectorRepo +} + type ChatHistory interface { ListChats() ([]models.Chat, error) GetChatByID(id uint32) (*models.Chat, error) + GetChatByChar(char string) ([]models.Chat, error) GetLastChat() (*models.Chat, error) + GetLastChatByAgent(agent string) (*models.Chat, error) UpsertChat(chat *models.Chat) (*models.Chat, error) RemoveChat(id uint32) error + ChatGetMaxID() (uint32, error) } type ProviderSQL struct { @@ -27,6 +36,12 @@ func (p ProviderSQL) ListChats() ([]models.Chat, error) { return resp, err } +func (p ProviderSQL) GetChatByChar(char string) ([]models.Chat, error) { + resp := []models.Chat{} + err := p.db.Select(&resp, "SELECT * FROM chats WHERE agent=$1;", char) + return resp, err +} + func (p ProviderSQL) GetChatByID(id uint32) (*models.Chat, error) { resp := models.Chat{} err := p.db.Get(&resp, "SELECT * FROM chats WHERE id=$1;", id) @@ -39,16 +54,28 @@ func (p ProviderSQL) GetLastChat() (*models.Chat, error) { return &resp, err } +func (p ProviderSQL) GetLastChatByAgent(agent string) (*models.Chat, error) { + resp := models.Chat{} + query := "SELECT * FROM chats WHERE agent=$1 ORDER BY updated_at DESC LIMIT 1" + err := p.db.Get(&resp, query, agent) + return &resp, err +} + +// https://sqlite.org/lang_upsert.html +// on conflict was added func (p ProviderSQL) UpsertChat(chat *models.Chat) (*models.Chat, error) { // Prepare the SQL statement query := ` - INSERT OR REPLACE INTO chats (id, name, msgs, created_at, updated_at) - VALUES (:id, :name, :msgs, :created_at, :updated_at) + INSERT INTO chats (id, name, msgs, agent, created_at, updated_at) + VALUES (:id, :name, :msgs, :agent, :created_at, :updated_at) + ON CONFLICT(id) DO UPDATE SET msgs=excluded.msgs, + updated_at=excluded.updated_at RETURNING *;` stmt, err := p.db.PrepareNamed(query) if err != nil { return nil, err } + defer stmt.Close() // Execute the query and scan the result into a new chat object var resp models.Chat err = stmt.Get(&resp, chat) @@ -61,13 +88,45 @@ func (p ProviderSQL) RemoveChat(id uint32) error { return err } -func NewProviderSQL(dbPath string, logger *slog.Logger) ChatHistory { +func (p ProviderSQL) ChatGetMaxID() (uint32, error) { + query := "SELECT MAX(id) FROM chats;" + var id uint32 + err := p.db.Get(&id, query) + return id, err +} + +// opens database connection +func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo { db, err := sqlx.Open("sqlite", dbPath) if err != nil { - panic(err) + logger.Error("failed to open db connection", "error", err) + return nil + } + // Enable WAL mode for better concurrency and performance + if _, err := db.Exec("PRAGMA journal_mode = WAL;"); err != nil { + logger.Warn("failed to enable WAL mode", "error", err) + } + if _, err := db.Exec("PRAGMA synchronous = NORMAL;"); err != nil { + logger.Warn("failed to set synchronous mode", "error", err) + } + // Increase cache size for better performance + if _, err := db.Exec("PRAGMA cache_size = -2000;"); err != nil { + logger.Warn("failed to set cache size", "error", err) + } + // Log actual journal mode for debugging + var journalMode string + if err := db.QueryRow("PRAGMA journal_mode;").Scan(&journalMode); err == nil { + logger.Debug("SQLite journal mode", "mode", journalMode) } - // get SQLite version p := ProviderSQL{db: db, logger: logger} - p.Migrate() + if err := p.Migrate(); err != nil { + logger.Error("migration failed, app cannot start", "error", err) + return nil + } return p } + +// DB returns the underlying database connection +func (p ProviderSQL) DB() *sqlx.DB { + return p.db +} diff --git a/storage/storage_test.go b/storage/storage_test.go index ad1f1bf..a4f2bdd 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -1,8 +1,8 @@ package storage import ( - "elefant/models" "fmt" + "gf-lt/models" "log/slog" "os" "testing" @@ -35,22 +35,27 @@ CREATE TABLE IF NOT EXISTS memories ( logger: slog.New(slog.NewJSONHandler(os.Stdout, nil)), } // Create a sample memory for testing - sampleMemory := &models.Memory{ + sampleMemory := models.Memory{ Agent: "testAgent", Topic: "testTopic", Mind: "testMind", CreatedAt: time.Now(), UpdatedAt: time.Now(), } + sampleMemoryRewrite := models.Memory{ + Agent: "testAgent", + Topic: "testTopic", + Mind: "same topic, new mind", + } cases := []struct { - memory *models.Memory + memories []models.Memory }{ - {memory: sampleMemory}, + {memories: []models.Memory{sampleMemory, sampleMemoryRewrite}}, } for i, tc := range cases { t.Run(fmt.Sprintf("run_%d", i), func(t *testing.T) { // Recall topics: get no rows - topics, err := provider.RecallTopics(tc.memory.Agent) + topics, err := provider.RecallTopics(tc.memories[0].Agent) if err != nil { t.Fatalf("Failed to recall topics: %v", err) } @@ -58,12 +63,12 @@ CREATE TABLE IF NOT EXISTS memories ( t.Fatalf("Expected no topics, got: %v", topics) } // Memorise - _, err = provider.Memorise(tc.memory) + _, err = provider.Memorise(&tc.memories[0]) if err != nil { t.Fatalf("Failed to memorise: %v", err) } // Recall topics: has topics - topics, err = provider.RecallTopics(tc.memory.Agent) + topics, err = provider.RecallTopics(tc.memories[0].Agent) if err != nil { t.Fatalf("Failed to recall topics: %v", err) } @@ -71,12 +76,20 @@ CREATE TABLE IF NOT EXISTS memories ( t.Fatalf("Expected topics, got none") } // Recall - content, err := provider.Recall(tc.memory.Agent, tc.memory.Topic) + content, err := provider.Recall(tc.memories[0].Agent, tc.memories[0].Topic) if err != nil { t.Fatalf("Failed to recall: %v", err) } - if content != tc.memory.Mind { - t.Fatalf("Expected content: %v, got: %v", tc.memory.Mind, content) + if content != tc.memories[0].Mind { + t.Fatalf("Expected content: %v, got: %v", tc.memories[0].Mind, content) + } + // rewrite mind of same agent-topic + newMem, err := provider.Memorise(&tc.memories[1]) + if err != nil { + t.Fatalf("Failed to memorise: %v", err) + } + if newMem.Mind == tc.memories[0].Mind { + t.Fatalf("Failed to change mind: %v", newMem.Mind) } }) } @@ -95,6 +108,7 @@ func TestChatHistory(t *testing.T) { id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, msgs TEXT NOT NULL, + agent TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );`) diff --git a/storage/vector.go b/storage/vector.go new file mode 100644 index 0000000..e3bbb89 --- /dev/null +++ b/storage/vector.go @@ -0,0 +1,231 @@ +package storage + +import ( + "encoding/binary" + "fmt" + "gf-lt/models" + "sort" + "unsafe" + + "github.com/jmoiron/sqlx" +) + +type VectorRepo interface { + WriteVector(*models.VectorRow) error + SearchClosest(q []float32, limit int) ([]models.VectorRow, error) + ListFiles() ([]string, error) + RemoveEmbByFileName(filename string) error + DB() *sqlx.DB +} + +// SerializeVector converts []float32 to binary blob +func SerializeVector(vec []float32) []byte { + buf := make([]byte, len(vec)*4) // 4 bytes per float32 + for i, v := range vec { + binary.LittleEndian.PutUint32(buf[i*4:], mathFloat32bits(v)) + } + return buf +} + +// DeserializeVector converts binary blob back to []float32 +func DeserializeVector(data []byte) []float32 { + count := len(data) / 4 + vec := make([]float32, count) + for i := 0; i < count; i++ { + vec[i] = mathBitsToFloat32(binary.LittleEndian.Uint32(data[i*4:])) + } + return vec +} + +// mathFloat32bits and mathBitsToFloat32 are helpers to convert between float32 and uint32 +func mathFloat32bits(f float32) uint32 { + return binary.LittleEndian.Uint32((*(*[4]byte)(unsafe.Pointer(&f)))[:4]) +} + +func mathBitsToFloat32(b uint32) float32 { + return *(*float32)(unsafe.Pointer(&b)) +} + +func fetchTableName(emb []float32) (string, error) { + switch len(emb) { + case 384: + return "embeddings_384", nil + case 768: + return "embeddings_768", nil + case 1024: + return "embeddings_1024", nil + case 1536: + return "embeddings_1536", nil + case 2048: + return "embeddings_2048", nil + case 3072: + return "embeddings_3072", nil + case 4096: + return "embeddings_4096", nil + case 5120: + return "embeddings_5120", nil + default: + return "", fmt.Errorf("no table for the size of %d", len(emb)) + } +} + +func (p ProviderSQL) WriteVector(row *models.VectorRow) error { + tableName, err := fetchTableName(row.Embeddings) + if err != nil { + return err + } + serializedEmbeddings := SerializeVector(row.Embeddings) + query := fmt.Sprintf("INSERT INTO %s(embeddings, slug, raw_text, filename) VALUES (?, ?, ?, ?)", tableName) + _, err = p.db.Exec(query, serializedEmbeddings, row.Slug, row.RawText, row.FileName) + return err +} + +func (p ProviderSQL) SearchClosest(q []float32, limit int) ([]models.VectorRow, error) { + tableName, err := fetchTableName(q) + if err != nil { + return nil, err + } + querySQL := "SELECT embeddings, slug, raw_text, filename FROM " + tableName + rows, err := p.db.Query(querySQL) + if err != nil { + return nil, err + } + defer rows.Close() + type SearchResult struct { + vector models.VectorRow + distance float32 + } + var allResults []SearchResult + for rows.Next() { + var ( + embeddingsBlob []byte + slug, rawText, fileName string + ) + if err := rows.Scan(&embeddingsBlob, &slug, &rawText, &fileName); err != nil { + continue + } + + storedEmbeddings := DeserializeVector(embeddingsBlob) + + // Calculate cosine similarity (returns value between -1 and 1, where 1 is most similar) + similarity := cosineSimilarity(q, storedEmbeddings) + distance := 1 - similarity // Convert to distance where 0 is most similar + + result := SearchResult{ + vector: models.VectorRow{ + Embeddings: storedEmbeddings, + Slug: slug, + RawText: rawText, + FileName: fileName, + }, + distance: distance, + } + allResults = append(allResults, result) + } + // Sort by distance + sort.Slice(allResults, func(i, j int) bool { + return allResults[i].distance < allResults[j].distance + }) + // Truncate to limit + if len(allResults) > limit { + allResults = allResults[:limit] + } + // Convert back to VectorRow slice + results := make([]models.VectorRow, len(allResults)) + for i, result := range allResults { + result.vector.Distance = result.distance + results[i] = result.vector + } + return results, nil +} + +// cosineSimilarity calculates the cosine similarity between two vectors +func cosineSimilarity(a, b []float32) float32 { + if len(a) != len(b) { + return 0.0 + } + var dotProduct, normA, normB float32 + for i := 0; i < len(a); i++ { + dotProduct += a[i] * b[i] + normA += a[i] * a[i] + normB += b[i] * b[i] + } + if normA == 0 || normB == 0 { + return 0.0 + } + return dotProduct / (sqrt(normA) * sqrt(normB)) +} + +// sqrt returns the square root of a float32 +func sqrt(f float32) float32 { + // A simple implementation of square root using Newton's method + if f == 0 { + return 0 + } + guess := f / 2 + for i := 0; i < 10; i++ { // 10 iterations should be enough for good precision + guess = (guess + f/guess) / 2 + } + return guess +} + +func (p ProviderSQL) ListFiles() ([]string, error) { + fileLists := make([][]string, 0) + + // Query all supported tables and combine results + tableNames := []string{ + "embeddings_384", "embeddings_768", "embeddings_1024", "embeddings_1536", + "embeddings_2048", "embeddings_3072", "embeddings_4096", "embeddings_5120", + } + for _, table := range tableNames { + query := "SELECT DISTINCT filename FROM " + table + rows, err := p.db.Query(query) + if err != nil { + // Continue if one table doesn't exist + continue + } + + var files []string + for rows.Next() { + var filename string + if err := rows.Scan(&filename); err != nil { + continue + } + files = append(files, filename) + } + rows.Close() + + fileLists = append(fileLists, files) + } + + // Combine and deduplicate + fileSet := make(map[string]bool) + var allFiles []string + for _, files := range fileLists { + for _, file := range files { + if !fileSet[file] { + fileSet[file] = true + allFiles = append(allFiles, file) + } + } + } + return allFiles, nil +} + +func (p ProviderSQL) RemoveEmbByFileName(filename string) error { + var errors []string + tableNames := []string{ + "embeddings_384", "embeddings_768", "embeddings_1024", "embeddings_1536", + "embeddings_2048", "embeddings_3072", "embeddings_4096", "embeddings_5120", + } + for _, table := range tableNames { + query := fmt.Sprintf("DELETE FROM %s WHERE filename = ?", table) + if _, err := p.db.Exec(query, filename); err != nil { + errors = append(errors, err.Error()) + } + } + if len(errors) > 0 { + return fmt.Errorf("errors occurred: %v", errors) + } + return nil +} diff --git a/sysprompts/alice_bob_carl.json b/sysprompts/alice_bob_carl.json new file mode 100644 index 0000000..b5321e4 --- /dev/null +++ b/sysprompts/alice_bob_carl.json @@ -0,0 +1,7 @@ +{ + "sys_prompt": "This is a chat between Alice, Bob and Carl. Normally all message are public (seen by everyone). But characters also able to make messages intended to specific targets using '@' tag. Usually tag is provided inside of out of character clause: (ooc: @charname@), but will be parsed if put anywhere in the message.\nTO SEND A PRIVATE MESSAGE:\n- Include a recipient tag in this exact format: @CharacterName@\n- The tag can be anywhere in your message\n- Example: \"(ooc: @Bob@) Don't tell others this secret.\"\n- For immersion sake it is better if private messages are given in context of whispering, passing notes, or being alone in some space: Alice: (ooc: @Carl@) *leans closer to Carl and whispers* \"I forgot to turn off the car, could you watch my bag for a cuple of minutes?\"\n- Only the sender and tagged recipients will see that message.\nRECEIVING MESSAGES:\n- You only see messages where you are the sender OR you are tagged in the recipient tag\n- Public messages (without tags) are seen by everyone.\nEXAMPLE FORMAT:\nAlice: \"Public message everyone sees\"\nAlice: (ooc: @Bob@)\n\"Private message only for Bob\"\n(if Diana joins the conversation, and Alice wants to exclude her) Alice: (ooc: @Bob,Carl@; Diana is not trustworthy)\n*Grabs Bob and Carl, and pulls them away* \"Listen boys, let's meet this friday again!\"\nWHEN TO USE:\n- Most of the time public messages (no tag) are the best choice. Private messages (with tag) are mostly for the passing secrets or information that is described or infered as private.\n- Game of 20 questions. Guys are putting paper sickers on the forehead with names written on them. So in this case only person who gets the sticker put on them does not see the writting on it.\nBob: *Puts sticker with 'JACK THE RIPPER' written on it, on Alices forehead* (ooc: @Carl).\nCarl: \"Alright, we're ready.\"\nAlice: \"Good. So, am I a fictional character or a real one?\"", + "role": "Alice", + "filepath": "sysprompts/alice_bob_carl.json", + "chars": ["Alice", "Bob", "Carl"], + "first_msg": "\"Hey guys! Want to play Alias like game? I'll tell Bob a word and he needs to describe that word so Carl can guess what it was?\"" +} diff --git a/sysprompts/coding_assistant.json b/sysprompts/coding_assistant.json new file mode 100644 index 0000000..f03d71d --- /dev/null +++ b/sysprompts/coding_assistant.json @@ -0,0 +1,6 @@ +{ + "sys_prompt": "You are an expert software engineering assistant. Your goal is to help users with coding tasks, debugging, refactoring, and software development.\n\n## Core Principles\n1. **Security First**: Never expose secrets, keys, or credentials. Never commit sensitive data.\n2. **No Git Actions**: You can READ git info (status, log, diff) for context, but NEVER perform git actions (commit, add, push, checkout, reset, rm, etc.). Let the user handle all git operations.\n3. **Explore Before Execute**: Always understand the codebase structure before making changes.\n4. **Follow Conventions**: Match existing code style, patterns, and frameworks used in the project.\n5. **Be Concise**: Minimize output tokens while maintaining quality. Avoid unnecessary explanations.\n6. **Ask First**: When uncertain about intent, ask the user. Don't assume.\n\n## Workflow for Complex Tasks\nFor multi-step tasks, ALWAYS use the todo system to track progress:\n\n1. **Create Todo List**: At the start of complex tasks, use `todo_create` to break down work into actionable items.\n2. **Update Progress**: Mark items as `in_progress` when working on them, and `completed` when done.\n3. **Check Status**: Use `todo_read` to review your progress.\n\nExample workflow:\n- User: \"Add user authentication to this app\"\n- You: Create todos: [\"Analyze existing auth structure\", \"Check frameworks in use\", \"Implement auth middleware\", \"Add login endpoints\", \"Test implementation\"]\n\n## Task Execution Flow\n\n### Phase 1: Exploration (Always First)\n- Use `file_list` to understand directory structure (path defaults to FilePickerDir if not specified)\n- Use `file_read` to examine relevant files (paths are relative to FilePickerDir unless starting with `/`)\n- Use `execute_command` with `grep`/`find` to search for patterns\n- Check README, Makefile, package.json, or similar for build/test commands\n- Identify: frameworks, conventions, testing approach, lint/typecheck commands\n- **Git reads allowed**: You may use `git status`, `git log`, `git diff` for context, but only to inform your work\n- **Path handling**: Relative paths resolve against FilePickerDir; absolute paths (starting with `/`) bypass it\n\n### Phase 2: Planning\n- For complex tasks: create todo items\n- Identify files that need modification\n- Plan your approach following existing patterns\n\n### Phase 3: Implementation\n- Make changes using appropriate file tools\n- Prefer `file_write` for new files, `file_read` then edit for existing files\n- Follow existing code style exactly\n- Use existing libraries and utilities\n\n### Phase 4: Verification\n- Run tests if available (check for test scripts in README/Makefile)\n- Run linting/type checking commands\n- Verify changes work as expected\n\n### Phase 5: Completion\n- Update todos to `completed`\n- Provide concise summary of changes\n- Reference specific file paths and line numbers when relevant\n- **DO NOT commit changes** - inform user what was done so they can review and commit themselves\n\n## Command Execution\n- Use `execute_command` with a single string containing command and arguments (e.g., `go run main.go`, `ls -la`, `cd /tmp`)\n- Use `cd /path` to change the working directory for file operations", + "role": "CodingAssistant", + "filepath": "sysprompts/coding_assistant.json", + "first_msg": "Hello! I'm your coding assistant. Give me a specific task and I'll get started. For complex work, I'll track progress with todos." +} diff --git a/sysprompts/llama.png b/sysprompts/llama.png Binary files differnew file mode 100644 index 0000000..7317300 --- /dev/null +++ b/sysprompts/llama.png diff --git a/tables.go b/tables.go new file mode 100644 index 0000000..e47a1ce --- /dev/null +++ b/tables.go @@ -0,0 +1,1190 @@ +package main + +import ( + "fmt" + "image" + "os" + "path" + "strings" + "time" + + "gf-lt/models" + "gf-lt/pngmeta" + "gf-lt/rag" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +func makeChatTable(chatMap map[string]models.Chat) *tview.Table { + actions := []string{"load", "rename", "delete", "update card", "move sysprompt onto 1st msg", "new_chat_from_card"} + chatList := make([]string, len(chatMap)) + i := 0 + for name := range chatMap { + chatList[i] = name + i++ + } + // Sort chatList by UpdatedAt field in descending order (most recent first) + for i := 0; i < len(chatList)-1; i++ { + for j := i + 1; j < len(chatList); j++ { + if chatMap[chatList[i]].UpdatedAt.Before(chatMap[chatList[j]].UpdatedAt) { + // Swap chatList[i] and chatList[j] + chatList[i], chatList[j] = chatList[j], chatList[i] + } + } + } + // Add 1 extra row for header + rows, cols := len(chatMap)+1, len(actions)+4 // +2 for name, +2 for timestamps + chatActTable := tview.NewTable(). + SetBorders(true) + // Add header row (row 0) + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + var headerText string + switch c { + case 0: + headerText = "Chat Name" + case 1: + headerText = "Preview" + case 2: + headerText = "Created At" + case 3: + headerText = "Updated At" + default: + headerText = actions[c-4] + } + chatActTable.SetCell(0, c, + tview.NewTableCell(headerText). + SetSelectable(false). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetAttributes(tcell.AttrBold)) + } + previewLen := 100 + // Add data rows (starting from row 1) + for r := 0; r < rows-1; r++ { // rows-1 because we added a header row + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + switch c { + case 0: + chatActTable.SetCell(r+1, c, // +1 to account for header row + tview.NewTableCell(chatList[r]). + SetSelectable(false). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + case 1: + if len(chatMap[chatList[r]].Msgs) < 100 { + previewLen = len(chatMap[chatList[r]].Msgs) + } + chatActTable.SetCell(r+1, c, // +1 to account for header row + tview.NewTableCell(chatMap[chatList[r]].Msgs[len(chatMap[chatList[r]].Msgs)-previewLen:]). + SetSelectable(false). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + case 2: + // Created At column + chatActTable.SetCell(r+1, c, // +1 to account for header row + tview.NewTableCell(chatMap[chatList[r]].CreatedAt.Format("2006-01-02 15:04")). + SetSelectable(false). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + case 3: + // Updated At column + chatActTable.SetCell(r+1, c, // +1 to account for header row + tview.NewTableCell(chatMap[chatList[r]].UpdatedAt.Format("2006-01-02 15:04")). + SetSelectable(false). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + default: + chatActTable.SetCell(r+1, c, // +1 to account for header row + tview.NewTableCell(actions[c-4]). // Adjusted offset to account for 2 new timestamp columns + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + chatActTable.Select(1, 0).SetSelectable(true, true).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 || key == tcell.Key('x') { + pages.RemovePage(historyPage) + return + } + }).SetSelectedFunc(func(row int, column int) { + // Skip header row (row 0) for selection + if row == 0 { + // If user clicks on header, just return without action + chatActTable.Select(1, column) // Move selection to first data row + return + } + tc := chatActTable.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + chatActTable.SetSelectable(false, false) + selectedChat := chatList[row-1] // -1 to account for header row + defer pages.RemovePage(historyPage) + switch tc.Text { + case "load": + history, err := loadHistoryChat(selectedChat) + if err != nil { + logger.Error("failed to read history file", "chat", selectedChat) + pages.RemovePage(historyPage) + return + } + chatBody.Messages = history + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + activeChatName = selectedChat + pages.RemovePage(historyPage) + return + case "rename": + pages.RemovePage(historyPage) + pages.AddPage(renamePage, renameWindow, true, true) + return + case "delete": + sc, ok := chatMap[selectedChat] + if !ok { + // no chat found + pages.RemovePage(historyPage) + return + } + if err := store.RemoveChat(sc.ID); err != nil { + logger.Error("failed to remove chat from db", "chat_id", sc.ID, "chat_name", sc.Name) + } + showToast("chat deleted", selectedChat+" was deleted") + // load last chat + chatBody.Messages = loadOldChatOrGetNew() + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + pages.RemovePage(historyPage) + return + case "update card": + // save updated card + fi := strings.Index(selectedChat, "_") + agentName := selectedChat[fi+1:] + cc := GetCardByRole(agentName) + if cc == nil { + logger.Warn("no such card", "agent", agentName) + showToast("error", "no such card: "+agentName) + return + } + cc.SysPrompt = chatBody.Messages[0].Content + cc.FirstMsg = chatBody.Messages[1].Content + if err := pngmeta.WriteToPng(cc.ToSpec(cfg.UserRole), cc.FilePath, cc.FilePath); err != nil { + logger.Error("failed to write charcard", "error", err) + } + return + case "move sysprompt onto 1st msg": + chatBody.Messages[1].Content = chatBody.Messages[0].Content + chatBody.Messages[1].Content + chatBody.Messages[0].Content = rpDefenitionSysMsg + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + activeChatName = selectedChat + pages.RemovePage(historyPage) + return + case "new_chat_from_card": + fi := strings.Index(selectedChat, "_") + agentName := selectedChat[fi+1:] + cc := GetCardByRole(agentName) + if cc == nil { + logger.Warn("no such card", "agent", agentName) + showToast("error", "no such card: "+agentName) + return + } + newCard, err := pngmeta.ReadCard(cc.FilePath, cfg.UserRole) + if err != nil { + logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err) + newCard, err = pngmeta.ReadCardJson(cc.FilePath) + if err != nil { + logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err) + showToast("error", "failed to reload card: "+cc.FilePath) + return + } + } + if newCard.ID == "" { + newCard.ID = models.ComputeCardID(newCard.Role, newCard.FilePath) + } + sysMap[newCard.ID] = newCard + roleToID[newCard.Role] = newCard.ID + startNewChat(false) + pages.RemovePage(historyPage) + return + default: + return + } + }) + // Add input capture to handle 'x' key for closing the table + chatActTable.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(historyPage) + return nil + } + return event + }) + return chatActTable +} + +// nolint:unused +func formatSize(size int64) string { + units := []string{"B", "KB", "MB", "GB", "TB"} + i := 0 + s := float64(size) + for s >= 1024 && i < len(units)-1 { + s /= 1024 + i++ + } + return fmt.Sprintf("%.1f%s", s, units[i]) +} + +type ragFileInfo struct { + name string + inRAGDir bool + isLoaded bool + fullPath string +} + +func makeRAGTable(fileList []string, loadedFiles []string) *tview.Flex { + // Build set of loaded files for quick lookup + loadedSet := make(map[string]bool) + for _, f := range loadedFiles { + loadedSet[f] = true + } + // Build merged list: files from ragdir + orphaned files from DB + ragFiles := make([]ragFileInfo, 0, len(fileList)+len(loadedFiles)) + seen := make(map[string]bool) + // Add files from ragdir + for _, f := range fileList { + ragFiles = append(ragFiles, ragFileInfo{ + name: f, + inRAGDir: true, + isLoaded: loadedSet[f], + fullPath: path.Join(cfg.RAGDir, f), + }) + seen[f] = true + } + // Add orphaned files (in DB but not in ragdir) + for _, f := range loadedFiles { + if !seen[f] { + ragFiles = append(ragFiles, ragFileInfo{ + name: f, + inRAGDir: false, + isLoaded: true, + fullPath: "", + }) + } + } + rows := len(ragFiles) + cols := 4 // File Name | Preview | Action | Delete + fileTable := tview.NewTable(). + SetBorders(true) + longStatusView := tview.NewTextView() + longStatusView.SetText("press x to exit") + longStatusView.SetBorder(true).SetTitle("status") + longStatusView.SetChangedFunc(func() { + app.Draw() + }) + ragflex := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(longStatusView, 0, 10, false). + AddItem(fileTable, 0, 60, true) + // Add the exit option as the first row (row 0) + fileTable.SetCell(0, 0, + tview.NewTableCell("File Name"). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + fileTable.SetCell(0, 1, + tview.NewTableCell("Preview"). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + fileTable.SetCell(0, 2, + tview.NewTableCell("Load/Unload"). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + fileTable.SetCell(0, 3, + tview.NewTableCell("Delete"). + SetTextColor(tcell.ColorWhite). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + // Add the file rows starting from row 1 + for r := 0; r < rows; r++ { + f := ragFiles[r] + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + switch c { + case 0: + displayName := f.name + if !f.inRAGDir { + displayName = f.name + " (orphaned)" + } + fileTable.SetCell(r+1, c, + tview.NewTableCell(displayName). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + case 1: + if !f.inRAGDir { + // Orphaned file - no preview available + fileTable.SetCell(r+1, c, + tview.NewTableCell("not in ragdir"). + SetTextColor(tcell.ColorYellow). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + } else if fi, err := os.Stat(f.fullPath); err == nil { + size := fi.Size() + modTime := fi.ModTime() + preview := fmt.Sprintf("%s | %s", formatSize(size), modTime.Format("2006-01-02 15:04")) + fileTable.SetCell(r+1, c, + tview.NewTableCell(preview). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + } else { + fileTable.SetCell(r+1, c, + tview.NewTableCell("error"). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + } + case 2: + actionText := "load" + if f.isLoaded { + actionText = "unload" + } + if !f.inRAGDir { + // Orphaned file - can only unload + actionText = "unload" + } + fileTable.SetCell(r+1, c, + tview.NewTableCell(actionText). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + case 3: + if !f.inRAGDir { + // Orphaned file - cannot delete from ragdir (not there) + fileTable.SetCell(r+1, c, + tview.NewTableCell("-"). + SetTextColor(tcell.ColorDarkGray). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + } else { + fileTable.SetCell(r+1, c, + tview.NewTableCell("delete"). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + } + errCh := make(chan error, 1) // why? + go func() { + for { + select { + case err := <-errCh: + if err == nil { + logger.Error("somehow got a nil err", "error", err) + continue + } + logger.Error("got an err in rag status", "error", err, "textview", longStatusView) + longStatusView.SetText(fmt.Sprintf("%v", err)) + close(errCh) + return + case status := <-rag.LongJobStatusCh: + longStatusView.SetText(status) + // fmt.Fprintln(longStatusView, status) + // app.Sync() + if status == rag.FinishedRAGStatus { + close(errCh) + time.Sleep(2 * time.Second) + return + } + } + } + }() + fileTable.Select(0, 0). + SetFixed(1, 1). + SetSelectable(true, true). + SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 || key == tcell.Key('x') || key == tcell.KeyCtrlX { + pages.RemovePage(RAGPage) + return + } + }).SetSelectedFunc(func(row int, column int) { + // If user selects a non-actionable column (0 or 1), move to first action column (2) + if column <= 1 { + if fileTable.GetColumnCount() > 2 { + fileTable.Select(row, 2) // Select first action column + } + return + } + tc := fileTable.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + fileTable.SetSelectable(false, false) + // Check if the selected row is the exit row (row 0) - do this first to avoid index issues + if row == 0 { + pages.RemovePage(RAGPage) + return + } + // For file rows, get the file info (row index - 1 because of the exit row at index 0) + f := ragFiles[row-1] + // Handle "-" case (orphaned file with no delete option) + if tc.Text == "-" { + return + } + switch tc.Text { + case "load": + fpath := path.Join(cfg.RAGDir, f.name) + longStatusView.SetText("clicked load") + go func() { + if err := ragger.LoadRAG(fpath); err != nil { + logger.Error("failed to embed file", "chat", fpath, "error", err) + showToast("RAG", "failed to embed file; error: "+err.Error()) + return + } + showToast("RAG", "file loaded successfully") + app.QueueUpdate(func() { + pages.RemovePage(RAGPage) + loadedFiles, _ := ragger.ListLoaded() + chatRAGTable := makeRAGTable(fileList, loadedFiles) + pages.AddPage(RAGPage, chatRAGTable, true, true) + }) + }() + return + case "unload": + longStatusView.SetText("clicked unload") + go func() { + if err := ragger.RemoveFile(f.name); err != nil { + logger.Error("failed to unload file from RAG", "filename", f.name, "error", err) + showToast("RAG", "failed to unload file; error: "+err.Error()) + return + } + showToast("RAG", "file unloaded successfully") + app.QueueUpdate(func() { + pages.RemovePage(RAGPage) + loadedFiles, _ := ragger.ListLoaded() + chatRAGTable := makeRAGTable(fileList, loadedFiles) + pages.AddPage(RAGPage, chatRAGTable, true, true) + }) + }() + return + case "delete": + fpath := path.Join(cfg.RAGDir, f.name) + if err := os.Remove(fpath); err != nil { + logger.Error("failed to delete file", "filename", fpath, "error", err) + return + } + showToast("chat deleted", fpath+" was deleted") + go func() { + app.QueueUpdate(func() { + pages.RemovePage(RAGPage) + newFileList, _ := os.ReadDir(cfg.RAGDir) + loadedFiles, _ := ragger.ListLoaded() + var newFiles []string + for _, f := range newFileList { + if !f.IsDir() { + newFiles = append(newFiles, f.Name()) + } + } + chatRAGTable := makeRAGTable(newFiles, loadedFiles) + pages.AddPage(RAGPage, chatRAGTable, true, true) + }) + }() + return + default: + pages.RemovePage(RAGPage) + return + } + }) + // Add input capture to the flex container to handle 'x' key for closing + ragflex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(RAGPage) + return nil + } + return event + }) + return ragflex +} + +func makeAgentTable(agentList []string) *tview.Table { + actions := []string{"filepath", "load"} + rows, cols := len(agentList), len(actions)+1 + chatActTable := tview.NewTable(). + SetBorders(true) + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + switch c { + case 0: + chatActTable.SetCell(r, c, + tview.NewTableCell(agentList[r]). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + case 1: + if actions[c-1] == "filepath" { + cc := GetCardByRole(agentList[r]) + if cc == nil { + continue + } + chatActTable.SetCell(r, c, + tview.NewTableCell(cc.FilePath). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + continue + } + chatActTable.SetCell(r, c, + tview.NewTableCell(actions[c-1]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + default: + chatActTable.SetCell(r, c, + tview.NewTableCell(actions[c-1]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + chatActTable.Select(0, 0). + SetFixed(1, 1). + SetSelectable(true, true). + SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 || key == tcell.Key('x') { + pages.RemovePage(agentPage) + return + } + }).SetSelectedFunc(func(row int, column int) { + // If user selects a non-actionable column (0 or 1), move to first action column (2) + if column <= 1 { + if chatActTable.GetColumnCount() > 2 { + chatActTable.Select(row, 2) // Select first action column + } + return + } + tc := chatActTable.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + chatActTable.SetSelectable(false, false) + selected := agentList[row] + // notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text) + switch tc.Text { + case "load": + if ok := charToStart(selected, true); !ok { + logger.Warn("no such sys msg", "name", selected) + pages.RemovePage(agentPage) + return + } + // replace textview + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + updateStatusLine() + // sysModal.ClearButtons() + pages.RemovePage(agentPage) + app.SetFocus(textArea) + return + case "rename": + pages.RemovePage(agentPage) + pages.AddPage(renamePage, renameWindow, true, true) + return + case "delete": + sc, ok := chatMap[selected] + if !ok { + // no chat found + pages.RemovePage(agentPage) + return + } + if err := store.RemoveChat(sc.ID); err != nil { + logger.Error("failed to remove chat from db", "chat_id", sc.ID, "chat_name", sc.Name) + } + showToast("chat deleted", selected+" was deleted") + pages.RemovePage(agentPage) + return + default: + pages.RemovePage(agentPage) + return + } + }) + // Add input capture to handle 'x' key for closing the table + chatActTable.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(agentPage) + return nil + } + return event + }) + return chatActTable +} + +func makeCodeBlockTable(codeBlocks []string) *tview.Table { + actions := []string{"copy"} + rows, cols := len(codeBlocks), len(actions)+1 + table := tview.NewTable(). + SetBorders(true) + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + previewLen := 30 + if len(codeBlocks[r]) < 30 { + previewLen = len(codeBlocks[r]) + } + switch { + case c < 1: + table.SetCell(r, c, + tview.NewTableCell(codeBlocks[r][:previewLen]). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + default: + table.SetCell(r, c, + tview.NewTableCell(actions[c-1]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + table.Select(0, 0). + SetFixed(1, 1). + SetSelectable(true, true). + SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 || key == tcell.Key('x') { + pages.RemovePage(codeBlockPage) + return + } + }).SetSelectedFunc(func(row int, column int) { + // If user selects a non-actionable column (0), move to first action column (1) + if column == 0 { + if table.GetColumnCount() > 1 { + table.Select(row, 1) // Select first action column + } + return + } + tc := table.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + table.SetSelectable(false, false) + selected := codeBlocks[row] + // notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text) + switch tc.Text { + case "copy": + if err := copyToClipboard(selected); err != nil { + showToast("error", err.Error()) + } + showToast("copied", selected) + pages.RemovePage(codeBlockPage) + app.SetFocus(textArea) + return + default: + pages.RemovePage(codeBlockPage) + return + } + }) + // Add input capture to handle 'x' key for closing the table + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(codeBlockPage) + return nil + } + return event + }) + return table +} + +func makeImportChatTable(filenames []string) *tview.Table { + actions := []string{"load"} + rows, cols := len(filenames), len(actions)+1 + chatActTable := tview.NewTable(). + SetBorders(true) + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + switch { + case c < 1: + chatActTable.SetCell(r, c, + tview.NewTableCell(filenames[r]). + SetTextColor(color). + SetAlign(tview.AlignCenter). + SetSelectable(false)) + default: + chatActTable.SetCell(r, c, + tview.NewTableCell(actions[c-1]). + SetTextColor(color). + SetAlign(tview.AlignCenter)) + } + } + } + chatActTable.Select(0, 0). + SetFixed(1, 1). + SetSelectable(true, true). + SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEsc || key == tcell.KeyF1 || key == tcell.Key('x') { + pages.RemovePage(historyPage) + return + } + }).SetSelectedFunc(func(row int, column int) { + // If user selects a non-actionable column (0), move to first action column (1) + if column == 0 { + if chatActTable.GetColumnCount() > 1 { + chatActTable.Select(row, 1) // Select first action column + } + return + } + tc := chatActTable.GetCell(row, column) + tc.SetTextColor(tcell.ColorRed) + chatActTable.SetSelectable(false, false) + selected := filenames[row] + // notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text) + switch tc.Text { + case "load": + if err := importChat(selected); err != nil { + logger.Warn("failed to import chat", "filename", selected) + pages.RemovePage(historyPage) + return + } + colorText() + updateStatusLine() + // redraw the text in text area + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + pages.RemovePage(historyPage) + app.SetFocus(textArea) + return + case "rename": + pages.RemovePage(historyPage) + pages.AddPage(renamePage, renameWindow, true, true) + return + case "delete": + sc, ok := chatMap[selected] + if !ok { + // no chat found + pages.RemovePage(historyPage) + return + } + if err := store.RemoveChat(sc.ID); err != nil { + logger.Error("failed to remove chat from db", "chat_id", sc.ID, "chat_name", sc.Name) + } + showToast("chat deleted", selected+" was deleted") + pages.RemovePage(historyPage) + return + default: + pages.RemovePage(historyPage) + return + } + }) + // Add input capture to handle 'x' key for closing the table + chatActTable.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(historyPage) + return nil + } + return event + }) + return chatActTable +} + +func makeFilePicker() *tview.Flex { + // Initialize with directory from config or current directory + startDir := cfg.FilePickerDir + if startDir == "" { + startDir = "." + } + // If startDir is ".", resolve it to the actual current working directory + if startDir == "." { + wd, err := os.Getwd() + if err == nil { + startDir = wd + } + } + // Track navigation history + dirStack := []string{startDir} + currentStackPos := 0 + // Track selected file + var selectedFile string + // Track currently displayed directory (changes as user navigates) + currentDisplayDir := startDir + // --- NEW: search state --- + searching := false + searchQuery := "" + searchInputMode := false + // Helper function to check if a file has an allowed extension from config + hasAllowedExtension := func(filename string) bool { + if cfg.FilePickerExts == "" { + return true + } + allowedExts := strings.Split(cfg.FilePickerExts, ",") + lowerFilename := strings.ToLower(strings.TrimSpace(filename)) + for _, ext := range allowedExts { + ext = strings.TrimSpace(ext) + if ext != "" && strings.HasSuffix(lowerFilename, "."+ext) { + return true + } + } + return false + } + // Helper function to check if a file is an image + isImageFile := func(filename string) bool { + imageExtensions := []string{".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".svg"} + lowerFilename := strings.ToLower(filename) + for _, ext := range imageExtensions { + if strings.HasSuffix(lowerFilename, ext) { + return true + } + } + return false + } + // Create UI elements + listView := tview.NewList() + listView.SetBorder(true). + SetTitle("Files & Directories [s: set FilePickerDir]. Current base dir: " + cfg.FilePickerDir). + SetTitleAlign(tview.AlignLeft) + // Status view for selected file information + statusView := tview.NewTextView() + statusView.SetBorder(true).SetTitle("Selected File").SetTitleAlign(tview.AlignLeft) + statusView.SetTextColor(tcell.ColorYellow) + // Image preview pane + var imgPreview *tview.Image + if cfg.ImagePreview { + imgPreview = tview.NewImage() + imgPreview.SetBorder(true).SetTitle("Preview").SetTitleAlign(tview.AlignLeft) + } + // Horizontal flex for list + preview + var hFlex *tview.Flex + if cfg.ImagePreview && imgPreview != nil { + hFlex = tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(listView, 0, 3, true). + AddItem(imgPreview, 0, 2, false) + } else { + hFlex = tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(listView, 0, 1, true) + } + // Main vertical flex + flex := tview.NewFlex().SetDirection(tview.FlexRow) + flex.AddItem(hFlex, 0, 3, true) + flex.AddItem(statusView, 3, 0, false) + // Refresh the file list – now accepts a filter string + var refreshList func(string, string) + refreshList = func(dir string, filter string) { + listView.Clear() + // Update the current display directory + currentDisplayDir = dir + // Add exit option at the top + listView.AddItem("Exit file picker [gray](Close without selecting)[-]", "", 'x', func() { + pages.RemovePage(filePickerPage) + }) + // Add parent directory (..) if not at root + if dir != "/" { + parentDir := path.Dir(dir) + // For Unix-like systems, avoid infinite loop when at root + if parentDir != dir { + listView.AddItem("../ [gray](Parent Directory)[-]", "", 'p', func() { + // Clear search on navigation + searching = false + searchQuery = "" + if cfg.ImagePreview { + imgPreview.SetImage(nil) + } + refreshList(parentDir, "") + dirStack = append(dirStack, parentDir) + currentStackPos = len(dirStack) - 1 + }) + } + } + // Read directory contents + files, err := os.ReadDir(dir) + if err != nil { + statusView.SetText("Error reading directory: " + err.Error()) + return + } + // Helper to check if an item passes the filter + matchesFilter := func(name string) bool { + if filter == "" { + return true + } + return strings.Contains(strings.ToLower(name), strings.ToLower(filter)) + } + // Add directories + for _, file := range files { + name := file.Name() + if strings.HasPrefix(name, ".") { + continue + } + if file.IsDir() && matchesFilter(name) { + dirName := name + listView.AddItem(dirName+"/ [gray](Directory)[-]", "", 0, func() { + // Clear search on navigation + searching = false + searchQuery = "" + if cfg.ImagePreview { + imgPreview.SetImage(nil) + } + newDir := path.Join(dir, dirName) + refreshList(newDir, "") + dirStack = append(dirStack, newDir) + currentStackPos = len(dirStack) - 1 + statusView.SetText("Current: " + newDir) + }) + } + } + // Add files with allowed extensions + for _, file := range files { + name := file.Name() + if strings.HasPrefix(name, ".") || file.IsDir() { + continue + } + if hasAllowedExtension(name) && matchesFilter(name) { + fileName := name + fullFilePath := path.Join(dir, fileName) + listView.AddItem(fileName+" [gray](File)[-]", "", 0, func() { + selectedFile = fullFilePath + statusView.SetText("Selected: " + selectedFile) + if isImageFile(fileName) { + statusView.SetText("Selected image: " + selectedFile) + } + }) + } + } + // Update status line based on search state + switch { + case searching: + statusView.SetText("Search: " + searchQuery + "_") + case searchQuery != "": + statusView.SetText("Current: " + dir + " (filter: " + searchQuery + ")") + default: + statusView.SetText("Current: " + dir) + } + } + // Initialize the file list + refreshList(startDir, "") + // Update image preview when selection changes (unchanged) + if cfg.ImagePreview && imgPreview != nil { + listView.SetChangedFunc(func(index int, mainText, secondaryText string, rune rune) { + itemText, _ := listView.GetItemText(index) + if strings.HasPrefix(itemText, "Exit file picker") || strings.HasPrefix(itemText, "../") { + imgPreview.SetImage(nil) + return + } + actualItemName := itemText + if bracketPos := strings.Index(itemText, " ["); bracketPos != -1 { + actualItemName = itemText[:bracketPos] + } + if strings.HasSuffix(actualItemName, "/") { + imgPreview.SetImage(nil) + return + } + if !isImageFile(actualItemName) { + imgPreview.SetImage(nil) + return + } + filePath := path.Join(currentDisplayDir, actualItemName) + file, err := os.Open(filePath) + if err != nil { + imgPreview.SetImage(nil) + return + } + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + imgPreview.SetImage(nil) + return + } + imgPreview.SetImage(img) + }) + } + // Set up keyboard navigation + flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + // --- Handle search mode --- + if searching { + switch event.Key() { + case tcell.KeyEsc: + // Exit search, clear filter + searching = false + searchInputMode = false + searchQuery = "" + refreshList(currentDisplayDir, "") + return nil + case tcell.KeyBackspace, tcell.KeyBackspace2: + if len(searchQuery) > 0 { + searchQuery = searchQuery[:len(searchQuery)-1] + refreshList(currentDisplayDir, searchQuery) + } + return nil + case tcell.KeyEnter: + // Exit search input mode and let normal processing handle selection + searchInputMode = false + // Get the currently highlighted item in the list + itemIndex := listView.GetCurrentItem() + if itemIndex >= 0 && itemIndex < listView.GetItemCount() { + itemText, _ := listView.GetItemText(itemIndex) + // Check for the exit option first + if strings.HasPrefix(itemText, "Exit file picker") { + pages.RemovePage(filePickerPage) + return nil + } + // Extract the actual filename/directory name by removing the type info + actualItemName := itemText + if bracketPos := strings.Index(itemText, " ["); bracketPos != -1 { + actualItemName = itemText[:bracketPos] + } + // Check if it's a directory (ends with /) + if strings.HasSuffix(actualItemName, "/") { + var targetDir string + if strings.HasPrefix(actualItemName, "../") { + // Parent directory + targetDir = path.Dir(currentDisplayDir) + if targetDir == currentDisplayDir && currentDisplayDir == "/" { + return nil + } + } else { + // Regular subdirectory + dirName := strings.TrimSuffix(actualItemName, "/") + targetDir = path.Join(currentDisplayDir, dirName) + } + // Navigate – clear search + if cfg.ImagePreview && imgPreview != nil { + imgPreview.SetImage(nil) + } + searching = false + searchInputMode = false + searchQuery = "" + refreshList(targetDir, "") + dirStack = append(dirStack, targetDir) + currentStackPos = len(dirStack) - 1 + statusView.SetText("Current: " + targetDir) + return nil + } else { + // It's a file + filePath := path.Join(currentDisplayDir, actualItemName) + if info, err := os.Stat(filePath); err == nil && !info.IsDir() { + if isImageFile(actualItemName) { + SetImageAttachment(filePath) + statusView.SetText("Image attached: " + filePath + " (will be sent with next message)") + pages.RemovePage(filePickerPage) + } else { + textArea.SetText(filePath, true) + app.SetFocus(textArea) + pages.RemovePage(filePickerPage) + } + } + return nil + } + } + return nil + case tcell.KeyRune: + r := event.Rune() + if searchInputMode && r != 0 { + searchQuery += string(r) + refreshList(currentDisplayDir, searchQuery) + return nil + } + // If not in search input mode, pass through for navigation + return event + default: + // Exit search input mode but keep filter active for navigation + searchInputMode = false + // Pass all other keys (arrows, etc.) to normal processing + return event + } + } + // --- Not searching --- + switch event.Key() { + case tcell.KeyEsc: + pages.RemovePage(filePickerPage) + return nil + case tcell.KeyBackspace2: // Backspace to go to parent directory + if cfg.ImagePreview && imgPreview != nil { + imgPreview.SetImage(nil) + } + if currentStackPos > 0 { + currentStackPos-- + prevDir := dirStack[currentStackPos] + // Clear search when navigating with backspace + searching = false + searchQuery = "" + refreshList(prevDir, "") + // Trim the stack to current position + dirStack = dirStack[:currentStackPos+1] + } + return nil + case tcell.KeyRune: + if event.Rune() == '/' { + // Enter search mode + searching = true + searchInputMode = true + searchQuery = "" + refreshList(currentDisplayDir, "") + return nil + } + if event.Rune() == 's' { + // Set FilePickerDir to current directory + // Get the actual directory path + cfg.FilePickerDir = currentDisplayDir + listView.SetTitle("Files & Directories [s: set FilePickerDir]. Current base dir: " + cfg.FilePickerDir) + // pages.RemovePage(filePickerPage) + return nil + } + case tcell.KeyEnter: + // Get the currently highlighted item in the list + itemIndex := listView.GetCurrentItem() + if itemIndex >= 0 && itemIndex < listView.GetItemCount() { + itemText, _ := listView.GetItemText(itemIndex) + logger.Info("choosing dir", "itemText", itemText) + // Check for the exit option first + if strings.HasPrefix(itemText, "Exit file picker") { + pages.RemovePage(filePickerPage) + return nil + } + // Extract the actual filename/directory name by removing the type info + actualItemName := itemText + if bracketPos := strings.Index(itemText, " ["); bracketPos != -1 { + actualItemName = itemText[:bracketPos] + } + // Check if it's a directory (ends with /) + if strings.HasSuffix(actualItemName, "/") { + var targetDir string + if strings.HasPrefix(actualItemName, "../") { + // Parent directory + targetDir = path.Dir(currentDisplayDir) + if targetDir == currentDisplayDir && currentDisplayDir == "/" { + logger.Warn("at root, cannot go up") + return nil + } + } else { + // Regular subdirectory + dirName := strings.TrimSuffix(actualItemName, "/") + targetDir = path.Join(currentDisplayDir, dirName) + } + // Navigate – clear search + logger.Info("going to dir", "dir", targetDir) + if cfg.ImagePreview && imgPreview != nil { + imgPreview.SetImage(nil) + } + searching = false + searchQuery = "" + refreshList(targetDir, "") + dirStack = append(dirStack, targetDir) + currentStackPos = len(dirStack) - 1 + statusView.SetText("Current: " + targetDir) + return nil + } else { + // It's a file + filePath := path.Join(currentDisplayDir, actualItemName) + if info, err := os.Stat(filePath); err == nil && !info.IsDir() { + if isImageFile(actualItemName) { + logger.Info("setting image", "file", actualItemName) + SetImageAttachment(filePath) + logger.Info("after setting image", "file", actualItemName) + statusView.SetText("Image attached: " + filePath + " (will be sent with next message)") + logger.Info("after setting text", "file", actualItemName) + pages.RemovePage(filePickerPage) + logger.Info("after update drawn", "file", actualItemName) + } else { + textArea.SetText(filePath, true) + app.SetFocus(textArea) + pages.RemovePage(filePickerPage) + } + } + return nil + } + } + return nil + } + return event + }) + return flex +} @@ -1,59 +1,2275 @@ package main +import ( + "context" + "encoding/json" + "fmt" + "gf-lt/agent" + "gf-lt/models" + "io" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" + "strings" + "sync" + "time" + + "gf-lt/rag" + + "github.com/GrailFinder/searchagent/searcher" +) + var ( - // TODO: form that message based on existing funcs - systemMsg = `You're a helpful assistant. -# Tools -You can do functions call if needed. + toolCallRE = regexp.MustCompile(`__tool_call__\s*([\s\S]*?)__tool_call__`) + quotesRE = regexp.MustCompile(`(".*?")`) + starRE = regexp.MustCompile(`(\*.*?\*)`) + thinkRE = regexp.MustCompile(`(<think>\s*([\s\S]*?)</think>)`) + codeBlockRE = regexp.MustCompile(`(?s)\x60{3}(?:.*?)\n(.*?)\n\s*\x60{3}\s*`) + singleBacktickRE = regexp.MustCompile(`\x60([^\x60]*)\x60`) + roleRE = regexp.MustCompile(`^(\w+):`) + rpDefenitionSysMsg = ` +For this roleplay immersion is at most importance. +Every character thinks and acts based on their personality and setting of the roleplay. +Meta discussions outside of roleplay is allowed if clearly labeled as out of character, for example: (ooc: {msg}) or <ooc>{msg}</ooc>. +` + basicSysMsg = `Large Language Model that helps user with any of his requests.` + toolSysMsg = `You can do functions call if needed. Your current tools: <tools> +[ +{ +"name":"recall", +"args": ["topic"], +"when_to_use": "when asked about topic that user previously asked to memorise" +}, +{ +"name":"memorise", +"args": ["topic", "data"], +"when_to_use": "when asked to memorise information under a topic" +}, +{ +"name":"recall_topics", +"args": [], +"when_to_use": "to see what topics are saved in memory" +}, +{ +"name":"websearch", +"args": ["query", "limit"], +"when_to_use": "when asked to search the web for information; returns clean summary without html,css and other web elements; limit is optional (default 3)" +}, +{ +"name":"rag_search", +"args": ["query", "limit"], +"when_to_use": "when asked to search the local document database for information; performs query refinement, semantic search, reranking, and synthesis; returns clean summary with sources; limit is optional (default 3)" +}, +{ +"name":"read_url", +"args": ["url"], +"when_to_use": "when asked to get content for specific webpage or url; returns clean summary without html,css and other web elements" +}, +{ +"name":"read_url_raw", +"args": ["url"], +"when_to_use": "when asked to get content for specific webpage or url; returns raw data as is without processing" +}, +{ +"name":"file_create", +"args": ["path", "content"], +"when_to_use": "when there is a need to create a new file with optional content" +}, +{ +"name":"file_read", +"args": ["path"], +"when_to_use": "when you need to read the content of a file" +}, +{ +"name":"file_read_image", +"args": ["path"], +"when_to_use": "when you need to read or view an image file" +}, +{ +"name":"file_write", +"args": ["path", "content"], +"when_to_use": "when needed to overwrite content to a file" +}, +{ +"name":"file_write_append", +"args": ["path", "content"], +"when_to_use": "when you need 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":"get_id", -"args": "username" +"name":"file_delete", +"args": ["path"], +"when_to_use": "when asked to delete a file" +}, +{ +"name":"file_move", +"args": ["src", "dst"], +"when_to_use": "when you need to move a file from source to destination" +}, +{ +"name":"file_copy", +"args": ["src", "dst"], +"when_to_use": "copy a file from source to destination" +}, +{ +"name":"file_list", +"args": ["path"], +"when_to_use": "list files in a directory; path is optional (default: current directory)" +}, +{ +"name":"execute_command", +"args": ["command", "args"], +"when_to_use": "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> To make a function call return a json object within __tool_call__ tags; -Example: +<example_request> __tool_call__ { -"name":"get_id", -"args": "Adam" +"name":"recall", +"args": {"topic": "Adam's number"} } __tool_call__ -When making function call avoid typing anything else. 'tool' user will respond with the results of the call. +</example_request> +<example_request> +__tool_call__ +{ +"name":"execute_command", +"args": {"command": "ls", "args": "-la /home"} +} +__tool_call__ +</example_request> +Tool call is addressed to the tool agent, avoid sending more info than tool call itself, while making a call. +When done right, tool call will be delivered to the tool agent. tool agent will respond with the results of the call. +<example_response> +tool: +under the topic: Adam's number is stored: +559-996 +</example_response> After that you are free to respond to the user. ` + webSearchSysPrompt = `Summarize the web search results, extracting key information and presenting a concise answer. Provide sources and URLs where relevant.` + ragSearchSysPrompt = `Synthesize the document search results, extracting key information and presenting a concise answer. Provide sources and document IDs where relevant.` + readURLSysPrompt = `Extract and summarize the content from the webpage. Provide key information, main points, and any relevant details.` + summarySysPrompt = `Please provide a concise summary of the following conversation. Focus on key points, decisions, and actions. Provide only the summary, no additional commentary.` + basicCard = &models.CharCard{ + ID: models.ComputeCardID("assistant", "basic_sys"), + SysPrompt: basicSysMsg, + FirstMsg: defaultFirstMsg, + Role: "assistant", + FilePath: "basic_sys", + } + sysMap = map[string]*models.CharCard{} + roleToID = map[string]string{} + sysLabels = []string{"assistant"} + + webAgentClient *agent.AgentClient + webAgentClientOnce sync.Once + webAgentsOnce sync.Once +) + +var windowToolSysMsg = ` +Additional window tools (available only if xdotool and maim are installed): +[ +{ +"name":"list_windows", +"args": [], +"when_to_use": "when asked to list visible windows; returns map of window ID to window name" +}, +{ +"name":"capture_window", +"args": ["window"], +"when_to_use": "when asked to take a screenshot of a specific window; saves to /tmp; window can be ID or name substring; returns file path" +}, +{ +"name":"capture_window_and_view", +"args": ["window"], +"when_to_use": "when asked to take a screenshot of a specific window and show it; saves to /tmp and returns image for viewing; window can be ID or name substring" +} +] +` + +var WebSearcher searcher.WebSurfer + +var ( + windowToolsAvailable bool + xdotoolPath string + maimPath string + modelHasVision bool ) -func memorize(topic, info string) { - // +func initTools() { + sysMap[basicCard.ID] = basicCard + roleToID["assistant"] = basicCard.ID + sa, err := searcher.NewWebSurfer(searcher.SearcherTypeScraper, "") + if err != nil { + if logger != nil { + logger.Warn("search agent unavailable; web_search tool disabled", "error", err) + } + WebSearcher = nil + } else { + WebSearcher = sa + } + if err := rag.Init(cfg, logger, store); err != nil { + logger.Warn("failed to init rag; rag_search tool will not be available", "error", err) + } + checkWindowTools() + registerWindowTools() +} + +func GetCardByRole(role string) *models.CharCard { + cardID, ok := roleToID[role] + if !ok { + return nil + } + return sysMap[cardID] +} + +func checkWindowTools() { + xdotoolPath, _ = exec.LookPath("xdotool") + maimPath, _ = exec.LookPath("maim") + windowToolsAvailable = xdotoolPath != "" && maimPath != "" + if windowToolsAvailable { + logger.Info("window tools available: xdotool and maim found") + } else { + if xdotoolPath == "" { + logger.Warn("xdotool not found, window listing tools will not be available") + } + if maimPath == "" { + logger.Warn("maim not found, window capture tools will not be available") + } + } } -func recall(topic string) string { - // - return "" +func updateToolCapabilities() { + if !cfg.ToolUse { + return + } + modelHasVision = false + if cfg == nil || cfg.CurrentAPI == "" { + logger.Warn("cannot determine model capabilities: cfg or CurrentAPI is nil") + registerWindowTools() + registerPlaywrightTools() + return + } + prevHasVision := modelHasVision + modelHasVision = ModelHasVision(cfg.CurrentAPI, cfg.CurrentModel) + if modelHasVision { + logger.Info("model has vision support", "model", cfg.CurrentModel, "api", cfg.CurrentAPI) + } else { + logger.Info("model does not have vision support", "model", cfg.CurrentModel, "api", cfg.CurrentAPI) + if windowToolsAvailable && !prevHasVision && !modelHasVision { + showToast("window tools", "Window capture-and-view unavailable: model lacks vision support") + } + } + registerWindowTools() + registerPlaywrightTools() } -func recallTopics() []string { - return []string{} +// getWebAgentClient returns a singleton AgentClient for web agents. +func getWebAgentClient() *agent.AgentClient { + webAgentClientOnce.Do(func() { + if cfg == nil { + if logger != nil { + logger.Warn("web agent client unavailable: config not initialized") + } + return + } + if logger == nil { + if logger != nil { + logger.Warn("web agent client unavailable: logger not initialized") + } + return + } + getToken := func() string { + if chunkParser == nil { + return "" + } + return chunkParser.GetToken() + } + webAgentClient = agent.NewAgentClient(cfg, *logger, getToken) + }) + return webAgentClient } -func fullMemoryLoad() {} +// registerWebAgents registers WebAgentB instances for websearch and read_url tools. +func registerWebAgents() { + webAgentsOnce.Do(func() { + client := getWebAgentClient() + // Register rag_search agent + agent.Register("rag_search", agent.NewWebAgentB(client, ragSearchSysPrompt)) + // Register websearch agent + agent.Register("websearch", agent.NewWebAgentB(client, webSearchSysPrompt)) + // Register read_url agent + agent.Register("read_url", agent.NewWebAgentB(client, readURLSysPrompt)) + // Register summarize_chat agent + agent.Register("summarize_chat", agent.NewWebAgentB(client, summarySysPrompt)) + }) +} -// predifine funcs -func getUserDetails(id ...string) map[string]any { - // db query - // return DB[id[0]] - return map[string]any{ - "username": "fm11", - "id": 24983, - "reputation": 911, - "balance": 214.73, +// web search (depends on extra server) +func websearch(args map[string]string) []byte { + // make http request return bytes + query, ok := args["query"] + if !ok || query == "" { + msg := "query not provided to web_search tool" + logger.Error(msg) + return []byte(msg) + } + limitS, ok := args["limit"] + if !ok || limitS == "" { + limitS = "3" + } + limit, err := strconv.Atoi(limitS) + if err != nil || limit == 0 { + logger.Warn("websearch limit; passed bad value; setting to default (3)", + "limit_arg", limitS, "error", err) + limit = 3 + } + resp, err := WebSearcher.Search(context.Background(), query, limit) + if err != nil { + msg := "search tool failed; error: " + err.Error() + logger.Error(msg) + return []byte(msg) } + data, err := json.Marshal(resp) + if err != nil { + msg := "failed to marshal search result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return data } -type fnSig func(...string) map[string]any +// rag search (searches local document database) +func ragsearch(args map[string]string) []byte { + query, ok := args["query"] + if !ok || query == "" { + msg := "query not provided to rag_search tool" + logger.Error(msg) + return []byte(msg) + } + limitS, ok := args["limit"] + if !ok || limitS == "" { + limitS = "10" + } + limit, err := strconv.Atoi(limitS) + if err != nil || limit == 0 { + logger.Warn("ragsearch limit; passed bad value; setting to default (3)", + "limit_arg", limitS, "error", err) + limit = 10 + } + ragInstance := rag.GetInstance() + if ragInstance == nil { + msg := "rag not initialized; rag_search tool is not available" + logger.Error(msg) + return []byte(msg) + } + results, err := ragInstance.Search(query, limit) + if err != nil { + msg := "rag search failed; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + data, err := json.Marshal(results) + if err != nil { + msg := "failed to marshal rag search result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return data +} + +// web search raw (returns raw data without processing) +func websearchRaw(args map[string]string) []byte { + // make http request return bytes + query, ok := args["query"] + if !ok || query == "" { + msg := "query not provided to websearch_raw tool" + logger.Error(msg) + return []byte(msg) + } + limitS, ok := args["limit"] + if !ok || limitS == "" { + limitS = "3" + } + limit, err := strconv.Atoi(limitS) + if err != nil || limit == 0 { + logger.Warn("websearch_raw limit; passed bad value; setting to default (3)", + "limit_arg", limitS, "error", err) + limit = 3 + } + resp, err := WebSearcher.Search(context.Background(), query, limit) + if err != nil { + msg := "search tool failed; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + // Return raw response without any processing + return []byte(fmt.Sprintf("%+v", resp)) +} + +// retrieves url content (text) +func readURL(args map[string]string) []byte { + // make http request return bytes + link, ok := args["url"] + if !ok || link == "" { + msg := "link not provided to read_url tool" + logger.Error(msg) + return []byte(msg) + } + resp, err := WebSearcher.RetrieveFromLink(context.Background(), link) + if err != nil { + msg := "search tool failed; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + data, err := json.Marshal(resp) + if err != nil { + msg := "failed to marshal search result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return data +} + +// retrieves url content raw (returns raw content without processing) +func readURLRaw(args map[string]string) []byte { + // make http request return bytes + link, ok := args["url"] + if !ok || link == "" { + msg := "link not provided to read_url_raw tool" + logger.Error(msg) + return []byte(msg) + } + resp, err := WebSearcher.RetrieveFromLink(context.Background(), link) + if err != nil { + msg := "search tool failed; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + // Return raw response without any processing + return []byte(fmt.Sprintf("%+v", resp)) +} + +/* +consider cases: +- append mode (treat it like a journal appendix) +- replace mode (new info/mind invalidates old ones) +also: +- some writing can be done without consideration of previous data; +- others do; +*/ +func memorise(args map[string]string) []byte { + agent := cfg.AssistantRole + if len(args) < 2 { + msg := "not enough args to call memorise tool; need topic and data to remember" + logger.Error(msg) + return []byte(msg) + } + memory := &models.Memory{ + Agent: agent, + Topic: args["topic"], + Mind: args["data"], + UpdatedAt: time.Now(), + CreatedAt: time.Now(), + } + if _, err := store.Memorise(memory); err != nil { + logger.Error("failed to save memory", "err", err, "memoory", memory) + return []byte("failed to save info") + } + msg := "info saved under the topic:" + args["topic"] + return []byte(msg) +} + +func recall(args map[string]string) []byte { + agent := cfg.AssistantRole + if len(args) < 1 { + logger.Warn("not enough args to call recall tool") + return nil + } + mind, err := store.Recall(agent, args["topic"]) + if err != nil { + msg := fmt.Sprintf("failed to recall; error: %v; args: %v", err, args) + logger.Error(msg) + return []byte(msg) + } + answer := fmt.Sprintf("under the topic: %s is stored:\n%s", args["topic"], mind) + return []byte(answer) +} + +func recallTopics(args map[string]string) []byte { + agent := cfg.AssistantRole + topics, err := store.RecallTopics(agent) + if err != nil { + logger.Error("failed to use tool", "error", err, "args", args) + return nil + } + joinedS := strings.Join(topics, ";") + return []byte(joinedS) +} + +// File Manipulation Tools +func fileCreate(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_create tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + content, ok := args["content"] + if !ok { + content = "" + } + if err := writeStringToFile(path, content); err != nil { + msg := "failed to create file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := "file created successfully at " + path + return []byte(msg) +} + +func fileRead(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_read tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + content, err := readStringFromFile(path) + if err != nil { + msg := "failed to read file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + result := map[string]string{ + "content": content, + "path": path, + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +func fileReadImage(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_read_image tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + dataURL, err := models.CreateImageURLFromPath(path) + if err != nil { + msg := "failed to read image; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + // result := map[string]any{ + // "type": "multimodal_content", + // "parts": []map[string]string{ + // {"type": "text", "text": "Image at " + path}, + // {"type": "image_url", "url": dataURL}, + // }, + // } + result := models.MultimodalToolResp{ + Type: "multimodal_content", + Parts: []map[string]string{ + {"type": "text", "text": "Image at " + path}, + {"type": "image_url", "url": dataURL}, + }, + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +func fileWrite(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_write tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + content, ok := args["content"] + if !ok { + content = "" + } + if err := writeStringToFile(path, content); err != nil { + msg := "failed to write to file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := "file written successfully at " + path + return []byte(msg) +} + +func fileWriteAppend(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_write_append tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + content, ok := args["content"] + if !ok { + content = "" + } + if err := appendStringToFile(path, content); err != nil { + msg := "failed to append to file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := "file written successfully at " + path + 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 { + path, ok := args["path"] + if !ok || path == "" { + msg := "path not provided to file_delete tool" + logger.Error(msg) + return []byte(msg) + } + path = resolvePath(path) + if err := removeFile(path); err != nil { + msg := "failed to delete file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := "file deleted successfully at " + path + return []byte(msg) +} + +func fileMove(args map[string]string) []byte { + src, ok := args["src"] + if !ok || src == "" { + msg := "source path not provided to file_move tool" + logger.Error(msg) + return []byte(msg) + } + src = resolvePath(src) + dst, ok := args["dst"] + if !ok || dst == "" { + msg := "destination path not provided to file_move tool" + logger.Error(msg) + return []byte(msg) + } + dst = resolvePath(dst) + if err := moveFile(src, dst); err != nil { + msg := "failed to move file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := fmt.Sprintf("file moved successfully from %s to %s", src, dst) + return []byte(msg) +} + +func fileCopy(args map[string]string) []byte { + src, ok := args["src"] + if !ok || src == "" { + msg := "source path not provided to file_copy tool" + logger.Error(msg) + return []byte(msg) + } + src = resolvePath(src) + dst, ok := args["dst"] + if !ok || dst == "" { + msg := "destination path not provided to file_copy tool" + logger.Error(msg) + return []byte(msg) + } + dst = resolvePath(dst) + if err := copyFile(src, dst); err != nil { + msg := "failed to copy file; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + msg := fmt.Sprintf("file copied successfully from %s to %s", src, dst) + return []byte(msg) +} + +func fileList(args map[string]string) []byte { + path, ok := args["path"] + if !ok || path == "" { + path = "." // default to current directory + } + path = resolvePath(path) + files, err := listDirectory(path) + if err != nil { + msg := "failed to list directory; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + result := map[string]interface{}{ + "directory": path, + "files": files, + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +// Helper functions for file operations +func resolvePath(p string) string { + if filepath.IsAbs(p) { + return p + } + return filepath.Join(cfg.FilePickerDir, p) +} + +func readStringFromFile(filename string) (string, error) { + data, err := os.ReadFile(filename) + if err != nil { + return "", err + } + return string(data), nil +} + +func writeStringToFile(filename string, data string) error { + return os.WriteFile(filename, []byte(data), 0644) +} + +func appendStringToFile(filename string, data string) error { + file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer file.Close() + _, err = file.WriteString(data) + return err +} + +func removeFile(filename string) error { + return os.Remove(filename) +} + +func moveFile(src, dst string) error { + // First try with os.Rename (works within same filesystem) + if err := os.Rename(src, dst); err == nil { + return nil + } + // If that fails (e.g., cross-filesystem), copy and delete + return copyAndRemove(src, dst) +} + +func copyFile(src, dst string) error { + srcFile, err := os.Open(src) + if err != nil { + return err + } + defer srcFile.Close() + dstFile, err := os.Create(dst) + if err != nil { + return err + } + defer dstFile.Close() + _, err = io.Copy(dstFile, srcFile) + return err +} + +func copyAndRemove(src, dst string) error { + // Copy the file + if err := copyFile(src, dst); err != nil { + return err + } + // Remove the source file + return os.Remove(src) +} + +func listDirectory(path string) ([]string, error) { + entries, err := os.ReadDir(path) + if err != nil { + return nil, err + } + var files []string + for _, entry := range entries { + if entry.IsDir() { + files = append(files, entry.Name()+"/") // Add "/" to indicate directory + } else { + files = append(files, entry.Name()) + } + } + return files, nil +} + +// Command Execution Tool +func executeCommand(args map[string]string) []byte { + commandStr := args["command"] + if commandStr == "" { + msg := "command not provided to execute_command tool" + logger.Error(msg) + return []byte(msg) + } + // Handle commands passed as single string with spaces (e.g., "go run main.go" or "cd /tmp") + // Split into base command and arguments + parts := strings.Fields(commandStr) + if len(parts) == 0 { + msg := "command not provided to execute_command tool" + logger.Error(msg) + return []byte(msg) + } + command := parts[0] + cmdArgs := parts[1:] + if !isCommandAllowed(command, cmdArgs...) { + msg := fmt.Sprintf("command '%s' is not allowed", command) + logger.Error(msg) + return []byte(msg) + } + // Special handling for cd command - update FilePickerDir + if command == "cd" { + return handleCdCommand(cmdArgs) + } + // Execute with timeout for safety + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, command, cmdArgs...) + cmd.Dir = cfg.FilePickerDir + output, err := cmd.CombinedOutput() + if err != nil { + msg := fmt.Sprintf("command '%s' failed; error: %v; output: %s", command, err, string(output)) + logger.Error(msg) + return []byte(msg) + } + // Check if output is empty and return success message + if len(output) == 0 { + successMsg := fmt.Sprintf("command '%s' executed successfully and exited with code 0", commandStr) + return []byte(successMsg) + } + return output +} + +// handleCdCommand handles the cd command to update FilePickerDir +func handleCdCommand(args []string) []byte { + var targetDir string + if len(args) == 0 { + // cd with no args goes to home directory + homeDir, err := os.UserHomeDir() + if err != nil { + msg := "cd: cannot determine home directory: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + targetDir = homeDir + } else { + targetDir = args[0] + } + + // Resolve relative paths against current FilePickerDir + if !filepath.IsAbs(targetDir) { + targetDir = filepath.Join(cfg.FilePickerDir, targetDir) + } + + // Verify the directory exists + info, err := os.Stat(targetDir) + if err != nil { + msg := "cd: " + targetDir + ": " + err.Error() + logger.Error(msg) + return []byte(msg) + } + if !info.IsDir() { + msg := "cd: " + targetDir + ": not a directory" + logger.Error(msg) + return []byte(msg) + } + + // Update FilePickerDir + absDir, err := filepath.Abs(targetDir) + if err != nil { + msg := "cd: failed to resolve path: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + cfg.FilePickerDir = absDir + msg := "FilePickerDir changed to: " + absDir + return []byte(msg) +} + +// Helper functions for command execution +// Todo structure +type TodoItem struct { + ID string `json:"id"` + Task string `json:"task"` + Status string `json:"status"` // "pending", "in_progress", "completed" +} +type TodoList struct { + 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 +var globalTodoList = TodoList{ + Items: []TodoItem{}, +} + +// Todo Management Tools +func todoCreate(args map[string]string) []byte { + task, ok := args["task"] + if !ok || task == "" { + msg := "task not provided to todo_create tool" + logger.Error(msg) + return []byte(msg) + } + // Generate simple ID + id := fmt.Sprintf("todo_%d", len(globalTodoList.Items)+1) + newItem := TodoItem{ + ID: id, + Task: task, + Status: "pending", + } + globalTodoList.Items = append(globalTodoList.Items, newItem) + result := map[string]string{ + "message": "todo created successfully", + "id": id, + "task": task, + "status": "pending", + "todos": globalTodoList.ToString(), + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +func todoRead(args map[string]string) []byte { + // Return all todos if no ID specified + result := map[string]interface{}{ + "todos": globalTodoList.ToString(), + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +func todoUpdate(args map[string]string) []byte { + id, ok := args["id"] + if !ok || id == "" { + msg := "id not provided to todo_update tool" + logger.Error(msg) + return []byte(msg) + } + task, taskOk := args["task"] + status, statusOk := args["status"] + if !taskOk && !statusOk { + msg := "neither task nor status provided to todo_update tool" + logger.Error(msg) + return []byte(msg) + } + // Find and update the todo + for i, item := range globalTodoList.Items { + if item.ID == id { + if taskOk { + globalTodoList.Items[i].Task = task + } + if statusOk { + // Validate status + if status == "pending" || status == "in_progress" || status == "completed" { + globalTodoList.Items[i].Status = status + } else { + result := map[string]string{ + "error": "status must be one of: pending, in_progress, completed", + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result; error: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult + } + } + result := map[string]string{ + "message": "todo updated successfully", + "id": id, + "todos": globalTodoList.ToString(), + } + 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 +} + +func todoDelete(args map[string]string) []byte { + id, ok := args["id"] + if !ok || id == "" { + msg := "id not provided to todo_delete tool" + logger.Error(msg) + return []byte(msg) + } + // Find and remove the todo + for i, item := range globalTodoList.Items { + if item.ID == id { + // Remove item from slice + globalTodoList.Items = append(globalTodoList.Items[:i], globalTodoList.Items[i+1:]...) + result := map[string]string{ + "message": "todo deleted successfully", + "id": id, + "todos": globalTodoList.ToString(), + } + 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 +} + +var gitReadSubcommands = map[string]bool{ + "status": true, + "log": true, + "diff": true, + "show": true, + "branch": true, + "reflog": true, + "rev-parse": true, + "shortlog": true, + "describe": true, +} + +func isCommandAllowed(command string, args ...string) bool { + allowedCommands := map[string]bool{ + "cd": true, + "grep": true, + "sed": true, + "awk": true, + "find": true, + "cat": true, + "head": true, + "tail": true, + "sort": true, + "uniq": true, + "wc": true, + "ls": true, + "echo": true, + "cut": true, + "tr": true, + "cp": true, + "mv": true, + "rm": true, + "mkdir": true, + "rmdir": true, + "pwd": true, + "df": true, + "free": true, + "ps": true, + "top": true, + "du": true, + "whoami": true, + "date": true, + "uname": true, + "git": true, + "go": true, + } + // Allow all go subcommands (go run, go mod tidy, go test, etc.) + if strings.HasPrefix(command, "go ") && allowedCommands["go"] { + return true + } + if command == "git" && len(args) > 0 { + return gitReadSubcommands[args[0]] + } + if !allowedCommands[command] { + return false + } + return true +} + +func summarizeChat(args map[string]string) []byte { + if len(chatBody.Messages) == 0 { + return []byte("No chat history to summarize.") + } + // Format chat history for the agent + chatText := chatToText(chatBody.Messages, true) // include system and tool messages + return []byte(chatText) +} + +func windowIDToHex(decimalID string) string { + id, err := strconv.ParseInt(decimalID, 10, 64) + if err != nil { + return decimalID + } + return fmt.Sprintf("0x%x", id) +} + +func listWindows(args map[string]string) []byte { + if !windowToolsAvailable { + return []byte("window tools not available: xdotool or maim not found") + } + cmd := exec.Command(xdotoolPath, "search", "--name", ".") + output, err := cmd.Output() + if err != nil { + msg := "failed to list windows: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + windowIDs := strings.Fields(string(output)) + windows := make(map[string]string) + for _, id := range windowIDs { + id = strings.TrimSpace(id) + if id == "" { + continue + } + nameCmd := exec.Command(xdotoolPath, "getwindowname", id) + nameOutput, err := nameCmd.Output() + if err != nil { + continue + } + name := strings.TrimSpace(string(nameOutput)) + windows[id] = name + } + data, err := json.Marshal(windows) + if err != nil { + msg := "failed to marshal window list: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return data +} + +func captureWindow(args map[string]string) []byte { + if !windowToolsAvailable { + return []byte("window tools not available: xdotool or maim not found") + } + window, ok := args["window"] + if !ok || window == "" { + return []byte("window parameter required (window ID or name)") + } + var windowID string + if _, err := strconv.Atoi(window); err == nil { + windowID = window + } else { + cmd := exec.Command(xdotoolPath, "search", "--name", window) + output, err := cmd.Output() + if err != nil || len(strings.Fields(string(output))) == 0 { + return []byte("window not found: " + window) + } + windowID = strings.Fields(string(output))[0] + } + nameCmd := exec.Command(xdotoolPath, "getwindowname", windowID) + nameOutput, _ := nameCmd.Output() + windowName := strings.TrimSpace(string(nameOutput)) + windowName = regexp.MustCompile(`[^a-zA-Z]+`).ReplaceAllString(windowName, "") + if windowName == "" { + windowName = "window" + } + timestamp := time.Now().Unix() + filename := fmt.Sprintf("/tmp/%s_%d.jpg", windowName, timestamp) + cmd := exec.Command(maimPath, "-i", windowIDToHex(windowID), filename) + if err := cmd.Run(); err != nil { + msg := "failed to capture window: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return []byte("screenshot saved: " + filename) +} + +func captureWindowAndView(args map[string]string) []byte { + if !windowToolsAvailable { + return []byte("window tools not available: xdotool or maim not found") + } + window, ok := args["window"] + if !ok || window == "" { + return []byte("window parameter required (window ID or name)") + } + var windowID string + if _, err := strconv.Atoi(window); err == nil { + windowID = window + } else { + cmd := exec.Command(xdotoolPath, "search", "--name", window) + output, err := cmd.Output() + if err != nil || len(strings.Fields(string(output))) == 0 { + return []byte("window not found: " + window) + } + windowID = strings.Fields(string(output))[0] + } + nameCmd := exec.Command(xdotoolPath, "getwindowname", windowID) + nameOutput, _ := nameCmd.Output() + windowName := strings.TrimSpace(string(nameOutput)) + windowName = regexp.MustCompile(`[^a-zA-Z]+`).ReplaceAllString(windowName, "") + if windowName == "" { + windowName = "window" + } + timestamp := time.Now().Unix() + filename := fmt.Sprintf("/tmp/%s_%d.jpg", windowName, timestamp) + captureCmd := exec.Command(maimPath, "-i", windowIDToHex(windowID), filename) + if err := captureCmd.Run(); err != nil { + msg := "failed to capture window: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + dataURL, err := models.CreateImageURLFromPath(filename) + if err != nil { + msg := "failed to create image URL: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + result := models.MultimodalToolResp{ + Type: "multimodal_content", + Parts: []map[string]string{ + {"type": "text", "text": "Screenshot saved: " + filename}, + {"type": "image_url", "url": dataURL}, + }, + } + jsonResult, err := json.Marshal(result) + if err != nil { + msg := "failed to marshal result: " + err.Error() + logger.Error(msg) + return []byte(msg) + } + return jsonResult +} + +type fnSig func(map[string]string) []byte var fnMap = map[string]fnSig{ - "get_id": getUserDetails, + "recall": recall, + "recall_topics": recallTopics, + "memorise": memorise, + "rag_search": ragsearch, + "websearch": websearch, + "websearch_raw": websearchRaw, + "read_url": readURL, + "read_url_raw": readURLRaw, + "file_create": fileCreate, + "file_read": fileRead, + "file_read_image": fileReadImage, + "file_write": fileWrite, + "file_write_append": fileWriteAppend, + "file_edit": fileEdit, + "file_delete": fileDelete, + "file_move": fileMove, + "file_copy": fileCopy, + "file_list": fileList, + "execute_command": executeCommand, + "todo_create": todoCreate, + "todo_read": todoRead, + "todo_update": todoUpdate, + "todo_delete": todoDelete, + "summarize_chat": summarizeChat, +} + +func removeWindowToolsFromBaseTools() { + windowToolNames := map[string]bool{ + "list_windows": true, + "capture_window": true, + "capture_window_and_view": true, + } + var filtered []models.Tool + for _, tool := range baseTools { + if !windowToolNames[tool.Function.Name] { + filtered = append(filtered, tool) + } + } + baseTools = filtered + delete(fnMap, "list_windows") + delete(fnMap, "capture_window") + delete(fnMap, "capture_window_and_view") +} + +func removePlaywrightToolsFromBaseTools() { + playwrightToolNames := map[string]bool{ + "pw_start": true, + "pw_stop": true, + "pw_is_running": true, + "pw_navigate": true, + "pw_click": true, + "pw_click_at": true, + "pw_fill": true, + "pw_extract_text": true, + "pw_screenshot": true, + "pw_screenshot_and_view": true, + "pw_wait_for_selector": true, + "pw_drag": true, + } + var filtered []models.Tool + for _, tool := range baseTools { + if !playwrightToolNames[tool.Function.Name] { + filtered = append(filtered, tool) + } + } + baseTools = filtered + delete(fnMap, "pw_start") + delete(fnMap, "pw_stop") + delete(fnMap, "pw_is_running") + delete(fnMap, "pw_navigate") + delete(fnMap, "pw_click") + delete(fnMap, "pw_click_at") + delete(fnMap, "pw_fill") + delete(fnMap, "pw_extract_text") + delete(fnMap, "pw_screenshot") + delete(fnMap, "pw_screenshot_and_view") + delete(fnMap, "pw_wait_for_selector") + delete(fnMap, "pw_drag") +} + +func registerWindowTools() { + removeWindowToolsFromBaseTools() + if windowToolsAvailable { + fnMap["list_windows"] = listWindows + fnMap["capture_window"] = captureWindow + windowTools := []models.Tool{ + { + Type: "function", + Function: models.ToolFunc{ + Name: "list_windows", + Description: "List all visible windows with their IDs and names. Returns a map of window ID to window name.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{}, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "capture_window", + Description: "Capture a screenshot of a specific window and save it to /tmp. Requires window parameter (window ID or name substring).", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"window"}, + Properties: map[string]models.ToolArgProps{ + "window": models.ToolArgProps{ + Type: "string", + Description: "window ID or window name (partial match)", + }, + }, + }, + }, + }, + } + if modelHasVision { + fnMap["capture_window_and_view"] = captureWindowAndView + windowTools = append(windowTools, models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "capture_window_and_view", + Description: "Capture a screenshot of a specific window, save it to /tmp, and return the image for viewing. Requires window parameter (window ID or name substring).", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"window"}, + Properties: map[string]models.ToolArgProps{ + "window": models.ToolArgProps{ + Type: "string", + Description: "window ID or window name (partial match)", + }, + }, + }, + }, + }) + } + baseTools = append(baseTools, windowTools...) + toolSysMsg += windowToolSysMsg + } +} + +func registerPlaywrightTools() { + removePlaywrightToolsFromBaseTools() + if cfg != nil && cfg.PlaywrightEnabled { + fnMap["pw_start"] = pwStart + fnMap["pw_stop"] = pwStop + fnMap["pw_is_running"] = pwIsRunning + fnMap["pw_navigate"] = pwNavigate + fnMap["pw_click"] = pwClick + fnMap["pw_click_at"] = pwClickAt + fnMap["pw_fill"] = pwFill + fnMap["pw_extract_text"] = pwExtractText + fnMap["pw_screenshot"] = pwScreenshot + fnMap["pw_screenshot_and_view"] = pwScreenshotAndView + fnMap["pw_wait_for_selector"] = pwWaitForSelector + fnMap["pw_drag"] = pwDrag + fnMap["pw_get_html"] = pwGetHTML + fnMap["pw_get_dom"] = pwGetDOM + fnMap["pw_search_elements"] = pwSearchElements + playwrightTools := []models.Tool{ + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_start", + Description: "Start a Playwright browser instance. Call this first before using other pw_ tools. Uses headless mode by default (set PlaywrightHeadless=false in config for GUI).", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{}, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_stop", + Description: "Stop the Playwright browser instance. Call when done with browser automation.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{}, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_is_running", + Description: "Check if Playwright browser is currently running.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{}, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_navigate", + Description: "Navigate to a URL in the browser.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"url"}, + Properties: map[string]models.ToolArgProps{ + "url": models.ToolArgProps{ + Type: "string", + Description: "URL to navigate to", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_click", + Description: "Click on an element using CSS selector. Use 'index' for multiple matches (default 0).", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"selector"}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "CSS selector for the element to click", + }, + "index": models.ToolArgProps{ + Type: "string", + Description: "optional index for multiple matches (default 0)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_fill", + Description: "Fill an input field with text using CSS selector.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"selector", "text"}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "CSS selector for the input element", + }, + "text": models.ToolArgProps{ + Type: "string", + Description: "text to fill into the input", + }, + "index": models.ToolArgProps{ + Type: "string", + Description: "optional index for multiple matches (default 0)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_extract_text", + Description: "Extract text content from the page or specific elements using CSS selector. Use 'body' for all page text.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"selector"}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "CSS selector (use 'body' for all page text)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_screenshot", + Description: "Take a screenshot of the page or a specific element. Returns file path to saved image.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "optional CSS selector for element to screenshot", + }, + "full_page": models.ToolArgProps{ + Type: "string", + Description: "optional: 'true' to capture full page (default false)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_screenshot_and_view", + Description: "Take a screenshot and return the image for viewing. Use when model needs to see the screenshot.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "optional CSS selector for element to screenshot", + }, + "full_page": models.ToolArgProps{ + Type: "string", + Description: "optional: 'true' to capture full page (default false)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_wait_for_selector", + Description: "Wait for an element to appear on the page.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"selector"}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "CSS selector to wait for", + }, + "timeout": models.ToolArgProps{ + Type: "string", + Description: "optional timeout in ms (default 30000)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_drag", + Description: "Drag the mouse from one point to another.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"x1", "y1", "x2", "y2"}, + Properties: map[string]models.ToolArgProps{ + "x1": models.ToolArgProps{ + Type: "string", + Description: "starting X coordinate", + }, + "y1": models.ToolArgProps{ + Type: "string", + Description: "starting Y coordinate", + }, + "x2": models.ToolArgProps{ + Type: "string", + Description: "ending X coordinate", + }, + "y2": models.ToolArgProps{ + Type: "string", + Description: "ending Y coordinate", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_get_html", + Description: "Get the HTML content of the page or a specific element.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "optional CSS selector (default: body)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_get_dom", + Description: "Get a structured DOM representation of an element with tag, attributes, text, and children.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "selector": models.ToolArgProps{ + Type: "string", + Description: "optional CSS selector (default: body)", + }, + }, + }, + }, + }, + { + Type: "function", + Function: models.ToolFunc{ + Name: "pw_search_elements", + Description: "Search for elements by text content or CSS selector. Returns matching elements with their tags, text, and HTML.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "text": models.ToolArgProps{ + Type: "string", + Description: "text to search for in elements", + }, + "selector": models.ToolArgProps{ + Type: "string", + Description: "CSS selector to search for", + }, + }, + }, + }, + }, + } + baseTools = append(baseTools, playwrightTools...) + toolSysMsg += browserToolSysMsg + } +} + +// callToolWithAgent calls the tool and applies any registered agent. +func callToolWithAgent(name string, args map[string]string) []byte { + registerWebAgents() + f, ok := fnMap[name] + if !ok { + return []byte(fmt.Sprintf("tool %s not found", name)) + } + raw := f(args) + if a := agent.Get(name); a != nil { + return a.Process(args, raw) + } + return raw +} + +// openai style def +var baseTools = []models.Tool{ + // rag_search + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "rag_search", + Description: "Search local document database given query, limit of sources (default 3). Performs query refinement, semantic search, reranking, and synthesis.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"query", "limit"}, + Properties: map[string]models.ToolArgProps{ + "query": models.ToolArgProps{ + Type: "string", + Description: "search query", + }, + "limit": models.ToolArgProps{ + Type: "string", + Description: "limit of the document results", + }, + }, + }, + }, + }, + // websearch + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "websearch", + Description: "Search web given query, limit of sources (default 3).", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"query", "limit"}, + Properties: map[string]models.ToolArgProps{ + "query": models.ToolArgProps{ + Type: "string", + Description: "search query", + }, + "limit": models.ToolArgProps{ + Type: "string", + Description: "limit of the website results", + }, + }, + }, + }, + }, + // read_url + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "read_url", + Description: "Retrieves text content of given link, providing clean summary without html,css and other web elements.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"url"}, + Properties: map[string]models.ToolArgProps{ + "url": models.ToolArgProps{ + Type: "string", + Description: "link to the webpage to read text from", + }, + }, + }, + }, + }, + // websearch_raw + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "websearch_raw", + Description: "Search web given query, returning raw data as is without processing. Use when you need the raw response data instead of a clean summary.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"query", "limit"}, + Properties: map[string]models.ToolArgProps{ + "query": models.ToolArgProps{ + Type: "string", + Description: "search query", + }, + "limit": models.ToolArgProps{ + Type: "string", + Description: "limit of the website results", + }, + }, + }, + }, + }, + // read_url_raw + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "read_url_raw", + Description: "Retrieves raw content of given link without processing. Use when you need the raw response data instead of a clean summary.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"url"}, + Properties: map[string]models.ToolArgProps{ + "url": models.ToolArgProps{ + Type: "string", + Description: "link to the webpage to read text from", + }, + }, + }, + }, + }, + // memorise + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "memorise", + Description: "Save topic-data in key-value cache. Use when asked to remember something/keep in mind.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"topic", "data"}, + Properties: map[string]models.ToolArgProps{ + "topic": models.ToolArgProps{ + Type: "string", + Description: "topic is the key under which data is saved", + }, + "data": models.ToolArgProps{ + Type: "string", + Description: "data is the value that is saved under the topic-key", + }, + }, + }, + }, + }, + // recall + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "recall", + Description: "Recall topic-data from key-value cache. Use when precise info about the topic is needed.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"topic"}, + Properties: map[string]models.ToolArgProps{ + "topic": models.ToolArgProps{ + Type: "string", + Description: "topic is the key to recall data from", + }, + }, + }, + }, + }, + // recall_topics + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "recall_topics", + Description: "Recall all topics from key-value cache. Use when need to know what topics are currently stored in memory.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{}, + }, + }, + }, + // file_create + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_create", + Description: "Create a new file with specified content. Use when you need to create a new file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path where the file should be created", + }, + "content": models.ToolArgProps{ + Type: "string", + Description: "content to write to the file (optional, defaults to empty string)", + }, + }, + }, + }, + }, + // file_read + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_read", + Description: "Read the content of a file. Use when you need to see the content of a file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the file to read", + }, + }, + }, + }, + }, + // file_read_image + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_read_image", + Description: "Read an image file and return it for multimodal LLM viewing. Supports png, jpg, jpeg, gif, webp formats. Use when you need the LLM to see and analyze an image.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the image file to read", + }, + }, + }, + }, + }, + // file_write + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_write", + Description: "Write content to a file. Will overwrite any content present.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path", "content"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the file to write to", + }, + "content": models.ToolArgProps{ + Type: "string", + Description: "content to write to the file", + }, + }, + }, + }, + }, + // file_write_append + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_write_append", + Description: "Append content to a file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path", "content"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the file to write to", + }, + "content": models.ToolArgProps{ + Type: "string", + Description: "content to write to the file", + }, + }, + }, + }, + }, + // 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 + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_delete", + Description: "Delete a file. Use when you need to remove a file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"path"}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the file to delete", + }, + }, + }, + }, + }, + // file_move + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_move", + Description: "Move a file from one location to another. Use when you need to relocate a file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"src", "dst"}, + Properties: map[string]models.ToolArgProps{ + "src": models.ToolArgProps{ + Type: "string", + Description: "source path of the file to move", + }, + "dst": models.ToolArgProps{ + Type: "string", + Description: "destination path where the file should be moved", + }, + }, + }, + }, + }, + // file_copy + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_copy", + Description: "Copy a file from one location to another. Use when you need to duplicate a file.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"src", "dst"}, + Properties: map[string]models.ToolArgProps{ + "src": models.ToolArgProps{ + Type: "string", + Description: "source path of the file to copy", + }, + "dst": models.ToolArgProps{ + Type: "string", + Description: "destination path where the file should be copied", + }, + }, + }, + }, + }, + // file_list + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "file_list", + Description: "List files and directories in a directory. Use when you need to see what files are in a directory.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "path": models.ToolArgProps{ + Type: "string", + Description: "path of the directory to list (optional, defaults to current directory)", + }, + }, + }, + }, + }, + // execute_command + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "execute_command", + Description: "Execute a shell command safely. Use when you need to run system commands like cd 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. Git is allowed for read-only operations: status, log, diff, show, branch, reflog, rev-parse, shortlog, describe. Use 'cd /path' to change working directory.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"command"}, + Properties: map[string]models.ToolArgProps{ + "command": models.ToolArgProps{ + Type: "string", + Description: "command to execute with arguments (e.g., 'go run main.go', 'ls -la /tmp', 'cd /home/user'). Use a single string; arguments should be space-separated after the command.", + }, + }, + }, + }, + }, + // todo_create + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "todo_create", + Description: "Create a new todo item with a task. Returns the created todo with its ID.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"task"}, + Properties: map[string]models.ToolArgProps{ + "task": models.ToolArgProps{ + Type: "string", + Description: "the task description to add to the todo list", + }, + }, + }, + }, + }, + // todo_read + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "todo_read", + Description: "Read todo items. Without ID returns all todos, with ID returns specific todo.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{}, + Properties: map[string]models.ToolArgProps{ + "id": models.ToolArgProps{ + Type: "string", + Description: "optional id of the specific todo item to read", + }, + }, + }, + }, + }, + // todo_update + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "todo_update", + Description: "Update a todo item by ID with new task or status. Status must be one of: pending, in_progress, completed.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"id"}, + 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: pending, in_progress, or completed (optional)", + }, + }, + }, + }, + }, + // todo_delete + models.Tool{ + Type: "function", + Function: models.ToolFunc{ + Name: "todo_delete", + Description: "Delete a todo item by ID. Returns success message.", + Parameters: models.ToolFuncParams{ + Type: "object", + Required: []string{"id"}, + Properties: map[string]models.ToolArgProps{ + "id": models.ToolArgProps{ + Type: "string", + Description: "id of the todo item to delete", + }, + }, + }, + }, + }, } diff --git a/tools_playwright.go b/tools_playwright.go new file mode 100644 index 0000000..3555469 --- /dev/null +++ b/tools_playwright.go @@ -0,0 +1,653 @@ +package main + +import ( + "encoding/json" + "fmt" + "gf-lt/models" + "os" + "strconv" + "strings" + "sync" + + "github.com/playwright-community/playwright-go" +) + +var browserToolSysMsg = ` +Additional browser automation tools (Playwright): +[ +{ + "name": "pw_start", + "args": [], + "when_to_use": "start a browser instance before doing any browser automation. Must be called first." +}, +{ + "name": "pw_stop", + "args": [], + "when_to_use": "stop the browser instance when done with automation." +}, +{ + "name": "pw_is_running", + "args": [], + "when_to_use": "check if browser is currently running." +}, +{ + "name": "pw_navigate", + "args": ["url"], + "when_to_use": "open a specific URL in the web browser." +}, +{ + "name": "pw_click", + "args": ["selector", "index"], + "when_to_use": "click on an element on the current webpage. Use 'index' for multiple matches (default 0)." +}, +{ + "name": "pw_fill", + "args": ["selector", "text", "index"], + "when_to_use": "type text into an input field. Use 'index' for multiple matches (default 0)." +}, +{ + "name": "pw_extract_text", + "args": ["selector"], + "when_to_use": "extract text content from the page or specific elements. Use selector 'body' for all page text." +}, +{ + "name": "pw_screenshot", + "args": ["selector", "full_page"], + "when_to_use": "take a screenshot of the page or a specific element. Returns a file path to the image. Use to verify actions or inspect visual state." +}, +{ + "name": "pw_screenshot_and_view", + "args": ["selector", "full_page"], + "when_to_use": "take a screenshot and return the image for viewing. Use to visually verify page state." +}, +{ + "name": "pw_wait_for_selector", + "args": ["selector", "timeout"], + "when_to_use": "wait for an element to appear on the page before proceeding with further actions." +}, +{ + "name": "pw_drag", + "args": ["x1", "y1", "x2", "y2"], + "when_to_use": "drag the mouse from point (x1,y1) to (x2,y2)." +}, +{ + "name": "pw_click_at", + "args": ["x", "y"], + "when_to_use": "click at specific X,Y coordinates on the page. Use when you know the exact position." +}, +{ + "name": "pw_get_html", + "args": ["selector"], + "when_to_use": "get the HTML content of the page or a specific element. Use to understand page structure or extract raw HTML." +}, +{ + "name": "pw_get_dom", + "args": ["selector"], + "when_to_use": "get a structured DOM representation with tag, attributes, text, and children. Use to inspect element hierarchy and properties." +}, +{ + "name": "pw_search_elements", + "args": ["text", "selector"], + "when_to_use": "search for elements by text content or CSS selector. Returns matching elements with their tags, text, and HTML." +} +] +` + +var ( + pw *playwright.Playwright + browser playwright.Browser + browserStarted bool + browserStartMu sync.Mutex + page playwright.Page +) + +func pwShutDown() error { + if pw == nil { + return nil + } + pwStop(nil) + return pw.Stop() +} + +func installPW() error { + err := playwright.Install(&playwright.RunOptions{Verbose: false}) + if err != nil { + logger.Warn("playwright not available", "error", err) + return err + } + return nil +} + +func checkPlaywright() error { + var err error + pw, err = playwright.Run() + if err != nil { + logger.Warn("playwright not available", "error", err) + return err + } + return nil +} + +func pwStart(args map[string]string) []byte { + browserStartMu.Lock() + defer browserStartMu.Unlock() + if browserStarted { + return []byte(`{"error": "Browser already started"}`) + } + var err error + browser, err = pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(!cfg.PlaywrightDebug), + }) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to launch browser: %s"}`, err.Error())) + } + page, err = browser.NewPage() + if err != nil { + browser.Close() + return []byte(fmt.Sprintf(`{"error": "failed to create page: %s"}`, err.Error())) + } + browserStarted = true + return []byte(`{"success": true, "message": "Browser started"}`) +} + +func pwStop(args map[string]string) []byte { + browserStartMu.Lock() + defer browserStartMu.Unlock() + if !browserStarted { + return []byte(`{"success": true, "message": "Browser was not running"}`) + } + if page != nil { + page.Close() + page = nil + } + if browser != nil { + browser.Close() + browser = nil + } + browserStarted = false + return []byte(`{"success": true, "message": "Browser stopped"}`) +} + +func pwIsRunning(args map[string]string) []byte { + if browserStarted { + return []byte(`{"running": true, "message": "Browser is running"}`) + } + return []byte(`{"running": false, "message": "Browser is not running"}`) +} + +func pwNavigate(args map[string]string) []byte { + url, ok := args["url"] + if !ok || url == "" { + return []byte(`{"error": "url not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + _, err := page.Goto(url) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to navigate: %s"}`, err.Error())) + } + title, _ := page.Title() + pageURL := page.URL() + return []byte(fmt.Sprintf(`{"success": true, "title": "%s", "url": "%s"}`, title, pageURL)) +} + +func pwClick(args map[string]string) []byte { + selector, ok := args["selector"] + if !ok || selector == "" { + return []byte(`{"error": "selector not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + index := 0 + if args["index"] != "" { + if i, err := strconv.Atoi(args["index"]); err != nil { + logger.Warn("failed to parse index", "value", args["index"], "error", err) + } else { + index = i + } + } + locator := page.Locator(selector) + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to find elements: %s"}`, err.Error())) + } + if index >= count { + return []byte(fmt.Sprintf(`{"error": "Element not found at index %d (found %d elements)"}`, index, count)) + } + err = locator.Nth(index).Click() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to click: %s"}`, err.Error())) + } + return []byte(`{"success": true, "message": "Clicked element"}`) +} + +func pwFill(args map[string]string) []byte { + selector, ok := args["selector"] + if !ok || selector == "" { + return []byte(`{"error": "selector not provided"}`) + } + text := args["text"] + if text == "" { + text = "" + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + index := 0 + if args["index"] != "" { + if i, err := strconv.Atoi(args["index"]); err != nil { + logger.Warn("failed to parse index", "value", args["index"], "error", err) + } else { + index = i + } + } + locator := page.Locator(selector) + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to find elements: %s"}`, err.Error())) + } + if index >= count { + return []byte(fmt.Sprintf(`{"error": "Element not found at index %d"}`, index)) + } + err = locator.Nth(index).Fill(text) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to fill: %s"}`, err.Error())) + } + return []byte(`{"success": true, "message": "Filled input"}`) +} + +func pwExtractText(args map[string]string) []byte { + selector := args["selector"] + if selector == "" { + selector = "body" + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + locator := page.Locator(selector) + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to find elements: %s"}`, err.Error())) + } + if count == 0 { + return []byte(`{"error": "No elements found"}`) + } + if selector == "body" { + text, err := page.Locator("body").TextContent() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to get text: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"text": "%s"}`, text)) + } + var texts []string + for i := 0; i < count; i++ { + text, err := locator.Nth(i).TextContent() + if err != nil { + continue + } + texts = append(texts, text) + } + return []byte(fmt.Sprintf(`{"text": "%s"}`, joinLines(texts))) +} + +func joinLines(lines []string) string { + var sb strings.Builder + for i, line := range lines { + if i > 0 { + sb.WriteString("\n") + } + sb.WriteString(line) + } + return sb.String() +} + +func pwScreenshot(args map[string]string) []byte { + selector := args["selector"] + fullPage := args["full_page"] == "true" + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + path := fmt.Sprintf("/tmp/pw_screenshot_%d.png", os.Getpid()) + var err error + if selector != "" && selector != "body" { + locator := page.Locator(selector) + _, err = locator.Screenshot(playwright.LocatorScreenshotOptions{ + Path: playwright.String(path), + }) + } else { + _, err = page.Screenshot(playwright.PageScreenshotOptions{ + Path: playwright.String(path), + FullPage: playwright.Bool(fullPage), + }) + } + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to take screenshot: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"path": "%s"}`, path)) +} + +func pwScreenshotAndView(args map[string]string) []byte { + selector := args["selector"] + fullPage := args["full_page"] == "true" + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + path := fmt.Sprintf("/tmp/pw_screenshot_%d.png", os.Getpid()) + var err error + if selector != "" && selector != "body" { + locator := page.Locator(selector) + _, err = locator.Screenshot(playwright.LocatorScreenshotOptions{ + Path: playwright.String(path), + }) + } else { + _, err = page.Screenshot(playwright.PageScreenshotOptions{ + Path: playwright.String(path), + FullPage: playwright.Bool(fullPage), + }) + } + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to take screenshot: %s"}`, err.Error())) + } + dataURL, err := models.CreateImageURLFromPath(path) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to create image URL: %s"}`, err.Error())) + } + resp := models.MultimodalToolResp{ + Type: "multimodal_content", + Parts: []map[string]string{ + {"type": "text", "text": "Screenshot saved: " + path}, + {"type": "image_url", "url": dataURL}, + }, + } + jsonResult, err := json.Marshal(resp) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to marshal result: %s"}`, err.Error())) + } + return jsonResult +} + +func pwWaitForSelector(args map[string]string) []byte { + selector, ok := args["selector"] + if !ok || selector == "" { + return []byte(`{"error": "selector not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + timeout := 30000 + if args["timeout"] != "" { + if t, err := strconv.Atoi(args["timeout"]); err != nil { + logger.Warn("failed to parse timeout", "value", args["timeout"], "error", err) + } else { + timeout = t + } + } + locator := page.Locator(selector) + err := locator.WaitFor(playwright.LocatorWaitForOptions{ + Timeout: playwright.Float(float64(timeout)), + }) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "element not found: %s"}`, err.Error())) + } + return []byte(`{"success": true, "message": "Element found"}`) +} + +func pwDrag(args map[string]string) []byte { + x1, ok := args["x1"] + if !ok { + return []byte(`{"error": "x1 not provided"}`) + } + y1, ok := args["y1"] + if !ok { + return []byte(`{"error": "y1 not provided"}`) + } + x2, ok := args["x2"] + if !ok { + return []byte(`{"error": "x2 not provided"}`) + } + y2, ok := args["y2"] + if !ok { + return []byte(`{"error": "y2 not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + var fx1, fy1, fx2, fy2 float64 + if parsedX1, err := strconv.ParseFloat(x1, 64); err != nil { + logger.Warn("failed to parse x1", "value", x1, "error", err) + } else { + fx1 = parsedX1 + } + if parsedY1, err := strconv.ParseFloat(y1, 64); err != nil { + logger.Warn("failed to parse y1", "value", y1, "error", err) + } else { + fy1 = parsedY1 + } + if parsedX2, err := strconv.ParseFloat(x2, 64); err != nil { + logger.Warn("failed to parse x2", "value", x2, "error", err) + } else { + fx2 = parsedX2 + } + if parsedY2, err := strconv.ParseFloat(y2, 64); err != nil { + logger.Warn("failed to parse y2", "value", y2, "error", err) + } else { + fy2 = parsedY2 + } + mouse := page.Mouse() + err := mouse.Move(fx1, fy1) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to move mouse: %s"}`, err.Error())) + } + err = mouse.Down() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to mouse down: %s"}`, err.Error())) + } + err = mouse.Move(fx2, fy2) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to move mouse: %s"}`, err.Error())) + } + err = mouse.Up() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to mouse up: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"success": true, "message": "Dragged from (%s,%s) to (%s,%s)"}`, x1, y1, x2, y2)) +} + +func pwClickAt(args map[string]string) []byte { + x, ok := args["x"] + if !ok { + return []byte(`{"error": "x not provided"}`) + } + y, ok := args["y"] + if !ok { + return []byte(`{"error": "y not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + fx, err := strconv.ParseFloat(x, 64) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to parse x: %s"}`, err.Error())) + } + fy, err := strconv.ParseFloat(y, 64) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to parse y: %s"}`, err.Error())) + } + mouse := page.Mouse() + err = mouse.Click(fx, fy) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to click: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"success": true, "message": "Clicked at (%s,%s)"}`, x, y)) +} + +func pwGetHTML(args map[string]string) []byte { + selector := args["selector"] + if selector == "" { + selector = "body" + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + locator := page.Locator(selector) + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to find elements: %s"}`, err.Error())) + } + if count == 0 { + return []byte(`{"error": "No elements found"}`) + } + html, err := locator.First().InnerHTML() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to get HTML: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"html": %s}`, jsonString(html))) +} + +type DOMElement struct { + Tag string `json:"tag,omitempty"` + Attributes map[string]string `json:"attributes,omitempty"` + Text string `json:"text,omitempty"` + Children []DOMElement `json:"children,omitempty"` + Selector string `json:"selector,omitempty"` + InnerHTML string `json:"innerHTML,omitempty"` +} + +func buildDOMTree(locator playwright.Locator) ([]DOMElement, error) { + var results []DOMElement + count, err := locator.Count() + if err != nil { + return nil, err + } + for i := 0; i < count; i++ { + el := locator.Nth(i) + dom, err := elementToDOM(el) + if err != nil { + continue + } + results = append(results, dom) + } + return results, nil +} + +func elementToDOM(el playwright.Locator) (DOMElement, error) { + dom := DOMElement{} + tag, err := el.Evaluate(`el => el.nodeName`, nil) + if err == nil { + dom.Tag = strings.ToLower(fmt.Sprintf("%v", tag)) + } + attributes := make(map[string]string) + attrs, err := el.Evaluate(`el => { + let attrs = {}; + for (let i = 0; i < el.attributes.length; i++) { + let attr = el.attributes[i]; + attrs[attr.name] = attr.value; + } + return attrs; + }`, nil) + if err == nil { + if amap, ok := attrs.(map[string]any); ok { + for k, v := range amap { + if vs, ok := v.(string); ok { + attributes[k] = vs + } + } + } + } + if len(attributes) > 0 { + dom.Attributes = attributes + } + text, err := el.TextContent() + if err == nil && text != "" { + dom.Text = text + } + innerHTML, err := el.InnerHTML() + if err == nil && innerHTML != "" { + dom.InnerHTML = innerHTML + } + childCount, _ := el.Count() + if childCount > 0 { + childrenLocator := el.Locator("*") + children, err := buildDOMTree(childrenLocator) + if err == nil && len(children) > 0 { + dom.Children = children + } + } + return dom, nil +} + +func pwGetDOM(args map[string]string) []byte { + selector := args["selector"] + if selector == "" { + selector = "body" + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + locator := page.Locator(selector) + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to find elements: %s"}`, err.Error())) + } + if count == 0 { + return []byte(`{"error": "No elements found"}`) + } + dom, err := elementToDOM(locator.First()) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to get DOM: %s"}`, err.Error())) + } + data, err := json.Marshal(dom) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to marshal DOM: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"dom": %s}`, string(data))) +} + +func pwSearchElements(args map[string]string) []byte { + text := args["text"] + selector := args["selector"] + if text == "" && selector == "" { + return []byte(`{"error": "text or selector not provided"}`) + } + if !browserStarted || page == nil { + return []byte(`{"error": "Browser not started. Call pw_start first."}`) + } + var locator playwright.Locator + if text != "" { + locator = page.GetByText(text) + } else { + locator = page.Locator(selector) + } + count, err := locator.Count() + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to search elements: %s"}`, err.Error())) + } + if count == 0 { + return []byte(`{"elements": []}`) + } + var results []map[string]string + for i := 0; i < count; i++ { + el := locator.Nth(i) + tag, _ := el.Evaluate(`el => el.nodeName`, nil) + text, _ := el.TextContent() + html, _ := el.InnerHTML() + results = append(results, map[string]string{ + "index": strconv.Itoa(i), + "tag": strings.ToLower(fmt.Sprintf("%v", tag)), + "text": text, + "html": html, + }) + } + data, err := json.Marshal(results) + if err != nil { + return []byte(fmt.Sprintf(`{"error": "failed to marshal results: %s"}`, err.Error())) + } + return []byte(fmt.Sprintf(`{"elements": %s}`, string(data))) +} + +func jsonString(s string) string { + b, _ := json.Marshal(s) + return string(b) +} @@ -0,0 +1,1175 @@ +package main + +import ( + "fmt" + "gf-lt/models" + "image" + _ "image/jpeg" + _ "image/png" + "os" + "path" + "strconv" + "strings" + "time" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +func isFullScreenPageActive() bool { + name, _ := pages.GetFrontPage() + return name != "main" +} + +var ( + pages *tview.Pages + textArea *tview.TextArea + editArea *tview.TextArea + textView *tview.TextView + statusLineWidget *tview.TextView + helpView *tview.TextView + flex *tview.Flex + bottomFlex *tview.Flex + notificationWidget *tview.TextView + imgView *tview.Image + defaultImage = "sysprompts/llama.png" + indexPickWindow *tview.InputField + renameWindow *tview.InputField + roleEditWindow *tview.InputField + shellInput *tview.InputField + confirmModal *tview.Modal + toastTimer *time.Timer + confirmPageName = "confirm" + fullscreenMode bool + positionVisible bool = true + // pages + historyPage = "historyPage" + agentPage = "agentPage" + editMsgPage = "editMsgPage" + roleEditPage = "roleEditPage" + helpPage = "helpPage" + renamePage = "renamePage" + RAGPage = "RAGPage" + propsPage = "propsPage" + codeBlockPage = "codeBlockPage" + imgPage = "imgPage" + filePickerPage = "filePicker" + exportDir = "chat_exports" + // For overlay search functionality + searchField *tview.InputField + searchPageName = "searchOverlay" + // help text + helpText = ` +[yellow]Esc[white]: send msg +[yellow]PgUp/Down[white]: switch focus between input and chat widgets +[yellow]F1[white]: manage chats +[yellow]F2[white]: regen last +[yellow]F3[white]: delete last msg +[yellow]F4[white]: edit msg +[yellow]F5[white]: toggle fullscreen for input/chat window +[yellow]F6[white]: interrupt bot resp +[yellow]F7[white]: copy last msg to clipboard (linux xclip) +[yellow]F8[white]: copy n msg to clipboard (linux xclip) +[yellow]F9[white]: table to copy from; with all code blocks +[yellow]F10[white]: switch if LLM will respond on this message (for user to write multiple messages in a row) +[yellow]F11[white]: import json chat file +[yellow]F12[white]: show this help page +[yellow]Ctrl+w[white]: resume generation on the last msg +[yellow]Ctrl+s[white]: load new char/agent +[yellow]Ctrl+e[white]: export chat to json file +[yellow]Ctrl+c[white]: close programm +[yellow]Ctrl+n[white]: start a new chat +[yellow]Ctrl+o[white]: open image file picker +[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+r[white]: start/stop recording from your microphone (needs stt server or whisper binary) +[yellow]Ctrl+t[white]: (un)collapse tool messages +[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+a[white]: interrupt tts (needs tts server) +[yellow]Alt+0[white]: replay last message via tts (needs tts server) +[yellow]Ctrl+g[white]: open RAG file manager (load files for context retrieval) +[yellow]Ctrl+y[white]: list loaded RAG files (view and manage loaded files) +[yellow]Ctrl+q[white]: show user role selection popup to choose who sends next msg as +[yellow]Ctrl+x[white]: show bot role selection popup to choose which agent responds next +[yellow]Alt+1[white]: toggle shell mode (execute commands locally) +[yellow]Alt+2[white]: toggle auto-scrolling (for reading while LLM types) +[yellow]Alt+3[white]: summarize chat history and start new chat with summary as tool response +[yellow]Alt+4[white]: edit msg role +[yellow]Alt+5[white]: toggle system and tool messages display +[yellow]Alt+6[white]: toggle status line visibility +[yellow]Alt+7[white]: toggle role injection (inject role in messages) +[yellow]Alt+8[white]: show char img or last picked img +[yellow]Alt+9[white]: warm up (load) selected llama.cpp model +[yellow]Alt+t[white]: toggle thinking blocks visibility (collapse/expand <think> blocks) +[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 + +=== scrolling chat window (some keys similar to vim) === +[yellow]arrows up/down and j/k[white]: scroll up and down +[yellow]gg/G[white]: jump to the begging / end of the chat +[yellow]/[white]: start searching for text +[yellow]n[white]: go to next search result +[yellow]N[white]: go to previous search result + +=== tables (chat history, agent pick, file pick, properties) === +[yellow]x[white]: to exit the table page + +=== filepicker === +[yellow]s[white]: (in file picker) set current dir as FilePickerDir +[yellow]x[white]: to exit + +=== shell mode === + [yellow]@match->Tab[white]: file completion with relative paths (recursive, depth 3, max 50 files) + +=== status line === +%s + +Press <Enter> or 'x' to return +` +) + +func setShellMode(enabled bool) { + shellMode = enabled + go func() { + app.QueueUpdateDraw(func() { + updateFlexLayout() + }) + }() +} + +// showToast displays a temporary notification in the bottom-right corner. +// It auto-hides after 3 seconds. +func showToast(title, message string) { + sanitize := func(s string, maxLen int) string { + sanitized := strings.Map(func(r rune) rune { + if r < 32 && r != '\t' { + return -1 + } + return r + }, s) + if len(sanitized) > maxLen { + sanitized = sanitized[:maxLen-3] + "..." + } + return sanitized + } + title = sanitize(title, 50) + message = sanitize(message, 197) + if toastTimer != nil { + toastTimer.Stop() + } + // show blocking notification to not mess up flex + if fullscreenMode { + notification := tview.NewTextView(). + SetTextAlign(tview.AlignCenter). + SetDynamicColors(true). + SetRegions(true). + SetText(fmt.Sprintf("[yellow]%s[-]\n", message)). + SetChangedFunc(func() { + app.Draw() + }) + notification.SetTitleAlign(tview.AlignLeft). + SetBorder(true). + SetTitle(title) + // Wrap it in a full‑screen Flex to position it in the top‑right corner. + // Outer Flex (row) pushes content to the top; inner Flex (column) pushes to the right. + background := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). // top spacer + AddItem(tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(nil, 0, 1, false). // left spacer + AddItem(notification, 40, 1, true), // notification width 40 + 5, 1, false) // notification height 5 + // Generate a unique page name (e.g., using timestamp) to allow multiple toasts. + pageName := fmt.Sprintf("toast-%d", time.Now().UnixNano()) + pages.AddPage(pageName, background, true, true) + // Auto‑dismiss after 2 seconds, since blocking is more annoying + time.AfterFunc(2*time.Second, func() { + app.QueueUpdateDraw(func() { + if pages.HasPage(pageName) { + pages.RemovePage(pageName) + } + }) + }) + return + } + notificationWidget.SetTitle(title) + notificationWidget.SetText(fmt.Sprintf("[yellow]%s[-]", message)) + go func() { + app.QueueUpdateDraw(func() { + flex.RemoveItem(bottomFlex) + flex.RemoveItem(statusLineWidget) + bottomFlex = tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(textArea, 0, 1, true). + AddItem(notificationWidget, 40, 1, false) + flex.AddItem(bottomFlex, 0, 10, true) + if positionVisible { + flex.AddItem(statusLineWidget, 0, 2, false) + } + }) + }() + toastTimer = time.AfterFunc(3*time.Second, func() { + app.QueueUpdateDraw(func() { + flex.RemoveItem(bottomFlex) + flex.RemoveItem(statusLineWidget) + bottomFlex = tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(textArea, 0, 1, true). + AddItem(notificationWidget, 0, 0, false) + flex.AddItem(bottomFlex, 0, 10, true) + if positionVisible { + flex.AddItem(statusLineWidget, 0, 2, false) + } + }) + }) +} + +func initTUI() { + // Start background goroutine to update model color cache + startModelColorUpdater() + tview.Styles = colorschemes["default"] + app = tview.NewApplication() + pages = tview.NewPages() + shellInput = tview.NewInputField(). + SetLabel(fmt.Sprintf("[%s]$ ", cfg.FilePickerDir)). // dynamic prompt + SetFieldWidth(0). + SetDoneFunc(func(key tcell.Key) { + 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 { + return event + } + // Handle Up arrow for history previous + if event.Key() == tcell.KeyUp { + if len(shellHistory) > 0 { + if shellHistoryPos < len(shellHistory)-1 { + shellHistoryPos++ + shellInput.SetText(shellHistory[len(shellHistory)-1-shellHistoryPos]) + } + } + 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 && shellMode { + currentText := shellInput.GetText() + atIndex := strings.LastIndex(currentText, "@") + if atIndex >= 0 { + filter := currentText[atIndex+1:] + showShellFileCompletionPopup(filter) + } + return nil + } + return event + }) + confirmModal = tview.NewModal(). + SetText("You are trying to send an empty message.\nIt makes sense if the last message in the chat is from you.\nAre you sure?"). + AddButtons([]string{"Yes", "No"}). + SetButtonBackgroundColor(tcell.ColorBlack). + SetButtonTextColor(tcell.ColorWhite). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + if buttonLabel == "Yes" { + persona := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + persona = cfg.WriteNextMsgAs + } + chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: ""} + } // In both Yes and No, go back to the main page + pages.SwitchToPage("main") // or whatever your main page is named + }) + confirmModal.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune { + switch event.Rune() { + case 'y', 'Y': + persona := cfg.UserRole + if cfg.WriteNextMsgAs != "" { + persona = cfg.WriteNextMsgAs + } + chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: ""} + pages.SwitchToPage("main") + return nil + case 'n', 'N', 'x', 'X': + pages.SwitchToPage("main") + return nil + } + } + 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(). + SetDynamicColors(true). + SetRegions(true). + SetChangedFunc(func() { + // INFO: + // https://github.com/rivo/tview/wiki/Concurrency#event-handlers + // although already called by default per tview specs + // calling it explicitly makes text streaming to look more smooth + app.Draw() + }) + notificationWidget = tview.NewTextView(). + SetTextAlign(tview.AlignCenter). + SetDynamicColors(true). + SetRegions(true). + SetChangedFunc(func() { + }) + notificationWidget.SetBorder(true).SetTitle("notification") + bottomFlex = tview.NewFlex().SetDirection(tview.FlexColumn). + AddItem(textArea, 0, 1, true). + AddItem(notificationWidget, 0, 0, false) + // + flex = tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(textView, 0, 40, false). + AddItem(bottomFlex, 0, 10, true) + if positionVisible { + flex.AddItem(statusLineWidget, 0, 2, false) + } + // textView.SetBorder(true).SetTitle("chat") + textView.SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEnter { + if len(searchResults) > 0 { // Check if a search is active + hideSearchBar() // Hide the search bar if visible + searchResults = nil // Clear search results + searchResultLengths = nil // Clear search result lengths + originalTextForSearch = "" + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) // Reset text without search regions + colorText() // Apply normal chat coloring + } else { + // Original logic if no search is active + currentSelection := textView.GetHighlights() + if len(currentSelection) > 0 { + textView.Highlight() + } else { + textView.Highlight("0").ScrollToHighlight() + } + } + } + }) + textView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + // Handle vim-like navigation in TextView + if event.Key() == tcell.KeyRune { + switch event.Rune() { + case 'j': + // For line down + return event + case 'k': + // For line up + return event + case 'g': + // Go to beginning + textView.ScrollToBeginning() + return nil + case 'G': + // Go to end + textView.ScrollToEnd() + return nil + case '/': + // Search functionality - show search bar + showSearchBar() + return nil + case 'n': + // Next search result + searchNext() + return nil + case 'N': + // Previous search result + searchPrev() + return nil + } + } + return event + }) + focusSwitcher[textArea] = textView + focusSwitcher[textView] = textArea + statusLineWidget = tview.NewTextView(). + SetDynamicColors(true). + SetTextAlign(tview.AlignCenter) + // // vertical text center alignment + // statusLineWidget.SetDrawFunc(func(screen tcell.Screen, x, y, w, h int) (int, int, int, int) { + // y += h / 2 + // return x, y, w, h + // }) + notificationWidget.SetDrawFunc(func(screen tcell.Screen, x, y, w, h int) (int, int, int, int) { + y += h / 2 + return x, y, w, h + }) + // Initially set up flex without search bar + flex = tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(textView, 0, 40, false). + AddItem(bottomFlex, 0, 10, true) + if positionVisible { + flex.AddItem(statusLineWidget, 0, 2, false) + } + editArea = tview.NewTextArea(). + SetPlaceholder("Replace msg...") + editArea.SetBorder(true).SetTitle("input") + editArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + // if event.Key() == tcell.KeyEscape && editMode { + if event.Key() == tcell.KeyEscape { + defer colorText() + editedMsg := editArea.GetText() + if editedMsg == "" { + showToast("edit", "no edit provided") + pages.RemovePage(editMsgPage) + return nil + } + chatBody.Messages[selectedIndex].SetText(editedMsg) + // change textarea + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + pages.RemovePage(editMsgPage) + editMode = false + return nil + } + return event + }) + indexPickWindow = tview.NewInputField(). + SetLabel("Enter a msg index: "). + SetFieldWidth(4). + SetAcceptanceFunc(tview.InputFieldInteger). + SetDoneFunc(func(key tcell.Key) { + hideIndexBar() + // colorText() + // updateStatusLine() + }) + roleEditWindow = tview.NewInputField(). + SetLabel("Enter new role: "). + SetPlaceholder("e.g., user, assistant, system, tool"). + SetDoneFunc(func(key tcell.Key) { + switch key { + case tcell.KeyEnter: + newRole := roleEditWindow.GetText() + if newRole == "" { + showToast("edit", "no role provided") + pages.RemovePage(roleEditPage) + return + } + if selectedIndex >= 0 && selectedIndex < len(chatBody.Messages) { + chatBody.Messages[selectedIndex].Role = newRole + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + pages.RemovePage(roleEditPage) + } + case tcell.KeyEscape: + pages.RemovePage(roleEditPage) + } + }) + indexPickWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyBackspace: + return event + case tcell.KeyEscape: + // Hide the index overlay when Escape is pressed + hideIndexBar() + return nil + case tcell.KeyEnter: + si := indexPickWindow.GetText() + siInt, err := strconv.Atoi(si) + if err != nil { + logger.Error("failed to convert provided index", "error", err, "si", si) + showToast("cancel", "no index provided, copying user input") + if err := copyToClipboard(textArea.GetText()); err != nil { + logger.Error("failed to copy to clipboard", "error", err) + } + hideIndexBar() // Hide overlay instead of removing page directly + return nil + } + selectedIndex = siInt + if len(chatBody.Messages)-1 < selectedIndex || selectedIndex < 0 { + msg := "chosen index is out of bounds, will copy user input" + logger.Warn(msg, "index", selectedIndex) + showToast("error", msg) + if err := copyToClipboard(textArea.GetText()); err != nil { + logger.Error("failed to copy to clipboard", "error", err) + } + hideIndexBar() // Hide overlay instead of removing page directly + return nil + } + m := chatBody.Messages[selectedIndex] + switch { + case roleEditMode: + hideIndexBar() // Hide overlay first + // Set the current role as the default text in the input field + roleEditWindow.SetText(m.Role) + pages.AddPage(roleEditPage, roleEditWindow, true, true) + roleEditMode = false // Reset the flag + case editMode: + hideIndexBar() // Hide overlay first + pages.AddPage(editMsgPage, editArea, true, true) + editArea.SetText(m.GetText(), true) + default: + msgText := m.GetText() + if err := copyToClipboard(msgText); err != nil { + logger.Error("failed to copy to clipboard", "error", err) + } + previewLen := min(30, len(msgText)) + notification := fmt.Sprintf("msg '%s' was copied to the clipboard", msgText[:previewLen]) + showToast("copied", notification) + hideIndexBar() // Hide overlay after copying + } + return nil + default: + return event + } + }) + // + renameWindow = tview.NewInputField(). + SetLabel("Enter a msg index: "). + SetFieldWidth(20). + SetAcceptanceFunc(tview.InputFieldMaxLength(100)). + SetDoneFunc(func(key tcell.Key) { + pages.RemovePage(renamePage) + }) + renameWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEnter { + nname := renameWindow.GetText() + if nname == "" { + return event + } + currentChat := chatMap[activeChatName] + delete(chatMap, activeChatName) + currentChat.Name = nname + activeChatName = nname + chatMap[activeChatName] = currentChat + _, err := store.UpsertChat(currentChat) + if err != nil { + logger.Error("failed to upsert chat", "error", err, "chat", currentChat) + } + notification := fmt.Sprintf("renamed chat to '%s'", activeChatName) + showToast("renamed", notification) + } + return event + }) + // + searchField = tview.NewInputField(). + SetPlaceholder("Search... (Enter: search)"). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEnter { + term := searchField.GetText() + if term == "" { + // If the search term is empty, cancel the search + hideSearchBar() + searchResults = nil + searchResultLengths = nil + originalTextForSearch = "" + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + return + } else { + performSearch(term) + // Keep focus on textView after search + app.SetFocus(textView) + hideSearchBar() + } + } + }) + searchField.SetBorder(true).SetTitle("Search") + // Note: Initially hide the search field (handled by not showing it in the layout) + // + helpView = tview.NewTextView().SetDynamicColors(true). + SetText(fmt.Sprintf(helpText, makeStatusLine())). + SetDoneFunc(func(key tcell.Key) { + pages.RemovePage(helpPage) + }) + helpView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEnter { + return event + } + if event.Key() == tcell.KeyRune && event.Rune() == 'x' { + pages.RemovePage(helpPage) + return nil + } + // Allow scrolling keys to pass through to the TextView + switch event.Key() { + case tcell.KeyUp, tcell.KeyDown, + tcell.KeyPgUp, tcell.KeyPgDn, + tcell.KeyHome, tcell.KeyEnd: + return event + } + if event.Key() == tcell.KeyRune { + switch event.Rune() { + case 'j', 'k', 'g', 'G': + return event + } + } + return nil + }) + // + imgView = tview.NewImage() + imgView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEnter { + pages.RemovePage(imgPage) + return event + } + if isASCII(string(event.Rune())) { + pages.RemovePage(imgPage) + return event + } + return nil + }) + // + textArea.SetMovedFunc(updateStatusLine) + updateStatusLine() + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + // init sysmap + _, err := initSysCards() + if err != nil { + logger.Error("failed to init sys cards", "error", err) + } + app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyRune && event.Rune() == '5' && event.Modifiers()&tcell.ModAlt != 0 { + // switch cfg.ShowSys + cfg.ShowSys = !cfg.ShowSys + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + } + if event.Key() == tcell.KeyRune && event.Rune() == '3' && event.Modifiers()&tcell.ModAlt != 0 { + go summarizeAndStartNewChat() + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == '6' && event.Modifiers()&tcell.ModAlt != 0 { + // toggle status line visibility + if name, _ := pages.GetFrontPage(); name != "main" { + return event + } + positionVisible = !positionVisible + updateFlexLayout() + } + if event.Key() == tcell.KeyRune && event.Rune() == '2' && event.Modifiers()&tcell.ModAlt != 0 { + // toggle auto-scrolling + cfg.AutoScrollEnabled = !cfg.AutoScrollEnabled + status := "disabled" + if cfg.AutoScrollEnabled { + status = "enabled" + } + showToast("autoscroll", "Auto-scrolling "+status) + updateStatusLine() + } + // Handle Alt+7 to toggle injectRole + if event.Key() == tcell.KeyRune && event.Rune() == '7' && event.Modifiers()&tcell.ModAlt != 0 { + injectRole = !injectRole + updateStatusLine() + } + // Handle Alt+T to toggle thinking block visibility + if event.Key() == tcell.KeyRune && event.Rune() == 't' && event.Modifiers()&tcell.ModAlt != 0 { + thinkingCollapsed = !thinkingCollapsed + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + colorText() + status := "expanded" + if thinkingCollapsed { + status = "collapsed" + } + showToast("thinking", "Thinking blocks "+status) + 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" + } + showToast("tools", "Tool calls/responses "+status) + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == 'i' && event.Modifiers()&tcell.ModAlt != 0 { + if isFullScreenPageActive() { + return event + } + showColorschemeSelectionPopup() + return nil + } + if event.Key() == tcell.KeyF1 { + // chatList, err := loadHistoryChats() + chatList, err := store.GetChatByChar(cfg.AssistantRole) + if err != nil { + logger.Error("failed to load chat history", "error", err) + return nil + } + // Check if there are no chats for this agent + if len(chatList) == 0 { + notification := "no chats found for agent: " + cfg.AssistantRole + showToast("info", notification) + return nil + } + chatMap := make(map[string]models.Chat) + // nameList := make([]string, len(chatList)) + for _, chat := range chatList { + // nameList[i] = chat.Name + chatMap[chat.Name] = chat + } + chatActTable := makeChatTable(chatMap) + pages.AddPage(historyPage, chatActTable, true, true) + colorText() + updateStatusLine() + return nil + } + if event.Key() == tcell.KeyF2 && !botRespMode.Load() { + // regen last msg + if len(chatBody.Messages) == 0 { + showToast("info", "no messages to regenerate") + return nil + } + chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1] + // there is no case where user msg is regenerated + // lastRole := chatBody.Messages[len(chatBody.Messages)-1].Role + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + // go chatRound("", cfg.UserRole, textView, true, false) + if cfg.TTS_ENABLED { + TTSDoneChan <- true + } + chatRoundChan <- &models.ChatRoundReq{Role: cfg.UserRole, Regen: true} + return nil + } + if event.Key() == tcell.KeyF3 && !botRespMode.Load() { + // delete last msg + // check textarea text; if it ends with bot icon delete only icon: + text := textView.GetText(true) + assistantIcon := roleToIcon(cfg.AssistantRole) + if strings.HasSuffix(text, assistantIcon) { + logger.Debug("deleting assistant icon", "icon", assistantIcon) + textView.SetText(strings.TrimSuffix(text, assistantIcon)) + colorText() + return nil + } + if len(chatBody.Messages) == 0 { + showToast("info", "no messages to delete") + return nil + } + chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1] + textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys)) + if cfg.TTS_ENABLED { + TTSDoneChan <- true + } + colorText() + return nil + } + if event.Key() == tcell.KeyF4 { + // edit msg - show index input as overlay at top + editMode = true + showIndexBar() + return nil + } + if event.Key() == tcell.KeyRune && event.Modifiers() == tcell.ModAlt && event.Rune() == '4' { + // edit msg role - show index input as overlay at top + editMode = false // Reset edit mode to false to handle role editing + showIndexBar() + // Set a flag to indicate we're in role edit mode + roleEditMode = true + return nil + } + if event.Key() == tcell.KeyF5 { + // toggle fullscreen + fullscreenMode = !fullscreenMode + focused := app.GetFocus() + if fullscreenMode { + if focused == textArea || focused == textView { + flex.Clear() + flex.AddItem(focused, 0, 1, true) + } else { + // if focus is not on textarea or textview, cancel fullscreen + fullscreenMode = false + } + } else { + // focused is the fullscreened widget here + updateFlexLayout() + } + return nil + } + if event.Key() == tcell.KeyF6 { + interruptResp.Store(true) + botRespMode.Store(false) + toolRunningMode.Store(false) + return nil + } + if event.Key() == tcell.KeyF7 { + // copy msg to clipboard + editMode = false + m := chatBody.Messages[len(chatBody.Messages)-1] + msgText := m.GetText() + if err := copyToClipboard(msgText); err != nil { + logger.Error("failed to copy to clipboard", "error", err) + } + previewLen := min(30, len(msgText)) + notification := fmt.Sprintf("msg '%s' was copied to the clipboard", msgText[:previewLen]) + showToast("copied", notification) + return nil + } + if event.Key() == tcell.KeyF8 { + // copy msg to clipboard + editMode = false + showIndexBar() + return nil + } + if event.Key() == tcell.KeyF9 { + // table of codeblocks to copy + text := textView.GetText(false) + cb := codeBlockRE.FindAllString(text, -1) + if len(cb) == 0 { + showToast("notify", "no code blocks in chat") + return nil + } + table := makeCodeBlockTable(cb) + pages.AddPage(codeBlockPage, table, true, true) + return nil + } + if event.Key() == tcell.KeyF10 { + cfg.SkipLLMResp = !cfg.SkipLLMResp + updateStatusLine() + } + if event.Key() == tcell.KeyF11 { + // read files in chat_exports + filelist, err := os.ReadDir(exportDir) + if err != nil { + showToast("failed to load exports", err.Error()) + return nil + } + fli := []string{} + for _, f := range filelist { + if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") { + continue + } + fpath := path.Join(exportDir, f.Name()) + fli = append(fli, fpath) + } + // check error + exportsTable := makeImportChatTable(fli) + pages.AddPage(historyPage, exportsTable, true, true) + updateStatusLine() + return nil + } + if event.Key() == tcell.KeyF12 { + // help window cheatsheet + // Update help text with current status before showing + helpView.SetText(fmt.Sprintf(helpText, makeStatusLine())) + pages.AddPage(helpPage, helpView, true, true) + return nil + } + if event.Key() == tcell.KeyCtrlE { + // export loaded chat into json file + if err := exportChat(); err != nil { + logger.Error("failed to export chat;", "error", err, "chat_name", activeChatName) + return nil + } + showToast("exported chat", "chat: "+activeChatName+" was exported") + return nil + } + if event.Key() == tcell.KeyCtrlP { + propsTable := makePropsTable(defaultLCPProps) + pages.AddPage(propsPage, propsTable, true, true) + return nil + } + if event.Key() == tcell.KeyCtrlN { + startNewChat(true) + return nil + } + if event.Key() == tcell.KeyCtrlO { + // open file picker + filePicker := makeFilePicker() + pages.AddPage(filePickerPage, filePicker, true, true) + return nil + } + if event.Key() == tcell.KeyCtrlL { + if isFullScreenPageActive() { + return event + } + // Show model selection popup instead of rotating models + showModelSelectionPopup() + return nil + } + if event.Key() == tcell.KeyCtrlV { + if isFullScreenPageActive() { + return event + } + // Show API link selection popup instead of rotating APIs + showAPILinkSelectionPopup() + return nil + } + if event.Key() == tcell.KeyCtrlS { + // switch sys prompt + labels, err := initSysCards() + if err != nil { + logger.Error("failed to read sys dir", "error", err) + showToast("error", "failed to read: "+cfg.SysDir) + return nil + } + at := makeAgentTable(labels) + // sysModal.AddButtons(labels) + // load all chars + pages.AddPage(agentPage, at, true, true) + updateStatusLine() + return nil + } + if event.Key() == tcell.KeyCtrlK { + // add message from tools + cfg.ToolUse = !cfg.ToolUse + updateToolCapabilities() + updateStatusLine() + return nil + } + if event.Key() == tcell.KeyRune && event.Rune() == '8' && event.Modifiers()&tcell.ModAlt != 0 { + // show image - check for attached image first, then fall back to agent image + if lastImg != "" { + // Load the attached image + file, err := os.Open(lastImg) + if err != nil { + logger.Error("failed to open attached image", "path", lastImg, "error", err) + // Fall back to showing agent image + if err := loadImage(); err != nil { + logger.Warn("failed to load agent image", "error", err) + } + } else { + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + logger.Error("failed to decode attached image", "path", lastImg, "error", err) + // Fall back to showing agent image + if err := loadImage(); err != nil { + logger.Warn("failed to load agent image", "error", err) + } + } else { + imgView.SetImage(img) + } + } + } else { + // No attached image, show agent image as before + if err := loadImage(); err != nil { + logger.Warn("failed to load agent image", "error", err) + } + } + pages.AddPage(imgPage, imgView, true, true) + return nil + } + if event.Key() == tcell.KeyCtrlR && cfg.STT_ENABLED { + defer updateStatusLine() + if asr.IsRecording() { + userSpeech, err := asr.StopRecording() + if err != nil { + msg := "failed to inference user speech; error:" + err.Error() + logger.Error(msg) + showToast("stt error", msg) + return nil + } + if userSpeech != "" { + // append indtead of replacing + prevText := textArea.GetText() + textArea.SetText(prevText+userSpeech, true) + } else { + logger.Warn("empty user speech") + } + return nil + } + if err := asr.StartRecording(); err != nil { + logger.Error("failed to start recording user speech", "error", err) + return nil + } + } + // I need keybind for tts to shut up + if event.Key() == tcell.KeyCtrlA && cfg.TTS_ENABLED { + TTSDoneChan <- true + } + if event.Key() == tcell.KeyRune && event.Rune() == '0' && event.Modifiers()&tcell.ModAlt != 0 && cfg.TTS_ENABLED { + if len(chatBody.Messages) > 0 { + // Stop any currently playing TTS first + TTSDoneChan <- true + lastMsg := chatBody.Messages[len(chatBody.Messages)-1] + cleanedText := models.CleanText(lastMsg.GetText()) + if cleanedText != "" { + // nolint: errcheck + go orator.Speak(cleanedText) + } + } + return nil + } + if event.Key() == tcell.KeyCtrlW { + // INFO: continue bot/text message + // without new role + lastRole := chatBody.Messages[len(chatBody.Messages)-1].Role + // go chatRound("", lastRole, textView, false, true) + chatRoundChan <- &models.ChatRoundReq{Role: lastRole, Resume: true} + return nil + } + if event.Key() == tcell.KeyCtrlQ { + if isFullScreenPageActive() { + return event + } + // Show user role selection popup instead of cycling through roles + showUserRoleSelectionPopup() + return nil + } + if event.Key() == tcell.KeyCtrlX { + if isFullScreenPageActive() { + return event + } + // Show bot role selection popup instead of cycling through roles + showBotRoleSelectionPopup() + return nil + } + // INFO: shutdown + if event.Key() == tcell.KeyCtrlC { + logger.Info("caught Ctrl+C via tcell event") + go func() { + if err := pwShutDown(); err != nil { + logger.Error("shutdown failed", "err", err) + } + app.Stop() + }() + return nil // swallow the event + } + if event.Key() == tcell.KeyCtrlG { + // cfg.RAGDir is the directory with files to use with RAG + // rag load + // menu of the text files from defined rag directory + files, err := os.ReadDir(cfg.RAGDir) + if err != nil { + // Check if the error is because the directory doesn't exist + if os.IsNotExist(err) { + // Create the RAG directory if it doesn't exist + if mkdirErr := os.MkdirAll(cfg.RAGDir, 0755); mkdirErr != nil { + logger.Error("failed to create RAG directory", "dir", cfg.RAGDir, "error", mkdirErr) + showToast("failed to create RAG directory", mkdirErr.Error()) + return nil + } + // Now try to read the directory again after creating it + files, err = os.ReadDir(cfg.RAGDir) + if err != nil { + logger.Error("failed to read dir after creating it", "dir", cfg.RAGDir, "error", err) + showToast("failed to read RAG directory", err.Error()) + return nil + } + } else { + // Other error (permissions, etc.) + logger.Error("failed to read dir", "dir", cfg.RAGDir, "error", err) + showToast("failed to open RAG files dir", err.Error()) + return nil + } + } + // Get files from ragdir + fileList := []string{} + for _, f := range files { + if f.IsDir() { + continue + } + fileList = append(fileList, f.Name()) + } + // Get loaded files from vector DB + loadedFiles, err := ragger.ListLoaded() + if err != nil { + logger.Error("failed to list loaded RAG files", "error", err) + loadedFiles = []string{} // Continue with empty list on error + } + chatRAGTable := makeRAGTable(fileList, loadedFiles) + pages.AddPage(RAGPage, chatRAGTable, true, true) + return nil + } + if event.Key() == tcell.KeyRune && event.Modifiers() == tcell.ModAlt && event.Rune() == '1' { + // Toggle shell mode: when enabled, commands are executed locally instead of sent to LLM + toggleShellMode() + return nil + } + if event.Key() == tcell.KeyRune && event.Modifiers() == tcell.ModAlt && event.Rune() == '9' { + // Warm up (load) the currently selected model + go warmUpModel() + showToast("model warmup", "loading model: "+chatBody.Model) + return nil + } + // cannot send msg in editMode or botRespMode + if event.Key() == tcell.KeyEscape && !editMode && !botRespMode.Load() { + if shellMode { + cmdText := shellInput.GetText() + if cmdText != "" { + executeCommandAndDisplay(cmdText) + shellInput.SetText("") + } + return nil + } + msgText := textArea.GetText() + nl := "\n\n" // keep empty lines between messages + prevText := textView.GetText(true) + persona := cfg.UserRole + // strings.LastIndex() + // newline is not needed is prev msg ends with one + if strings.HasSuffix(prevText, nl) { + nl = "" + } else if strings.HasSuffix(prevText, "\n") { + nl = "\n" // only one newline, add another + } + if msgText != "" { + // as what char user sends msg? + if cfg.WriteNextMsgAs != "" { + persona = cfg.WriteNextMsgAs + } + // check if plain text + if !injectRole { + matches := roleRE.FindStringSubmatch(msgText) + if len(matches) > 1 { + persona = matches[1] + msgText = strings.TrimLeft(msgText[len(matches[0]):], " ") + } + } + // add user icon before user msg + fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n", + nl, len(chatBody.Messages), persona, msgText) + textArea.SetText("", true) + if cfg.AutoScrollEnabled { + textView.ScrollToEnd() + } + colorText() + } else { + pages.AddPage(confirmPageName, confirmModal, true, true) + return nil + } + // go chatRound(msgText, persona, textView, false, false) + chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: msgText} + return nil + } + if event.Key() == tcell.KeyTab && !shellMode { + currentF := app.GetFocus() + if currentF == textArea { + currentText := textArea.GetText() + atIndex := strings.LastIndex(currentText, "@") + if atIndex >= 0 { + filter := currentText[atIndex+1:] + showTextAreaFileCompletionPopup(filter) + } + } + return nil + } + if event.Key() == tcell.KeyPgUp || event.Key() == tcell.KeyPgDn { + currentF := app.GetFocus() + app.SetFocus(focusSwitcher[currentF]) + return nil + } + if isASCII(string(event.Rune())) && !botRespMode.Load() { + return event + } + return event + }) + go updateModelLists() +} |
