37 Commits

Author SHA1 Message Date
Grail Finder
3e4213b5c3 Fix: streaming to tui 2026-03-17 10:51:53 +03:00
Grail Finder
451e6f0381 Merge branch 'master' into feat/agent-flow 2026-03-17 09:23:35 +03:00
Grail Finder
326a1a4d09 Enha: pick api and model by index (/m 1) 2026-03-17 09:21:50 +03:00
Grail Finder
0b7f621a75 Chore (cli): sort-img task 2026-03-17 07:20:19 +03:00
Grail Finder
46dbb7b981 Chore: update .gitignore
write logs to log.log
2026-03-17 07:05:17 +03:00
Grail Finder
ef0940daa8 Enha(cli): add sort img test case 2026-03-16 13:46:25 +03:00
Grail Finder
3d44686a51 Feat (cli): test run and teardown 2026-03-16 09:44:28 +03:00
Grail Finder
df04d8c21c Fix: wait for the tool resp 2026-03-16 08:38:48 +03:00
Grail Finder
7c8697f48e Enha (cli): -msg -continue flags 2026-03-16 07:52:55 +03:00
Grail Finder
7f8bbefb05 Chore: add cli test 2026-03-16 06:36:24 +03:00
Grail Finder
47b3d37a97 Enha: remove browser agent from completion 2026-03-15 16:04:03 +03:00
Grail Finder
2c9c36e2c6 Feat: cli mode 2026-03-15 15:30:10 +03:00
Grail Finder
e476575334 Chore: linter complaints 2026-03-15 11:50:22 +03:00
Grail Finder
7e346b5e19 Refactor: deal with unused code 2026-03-15 10:55:07 +03:00
Grail Finder
1396b3eb05 Refactor: moving tool related code into tools package 2026-03-15 08:05:12 +03:00
Grail Finder
619b19cb46 Enha: avoid posting full help on wrong command 2026-03-14 18:10:34 +03:00
Grail Finder
26377702d3 Fix: view_img, exec commands 2026-03-14 16:34:58 +03:00
Grail Finder
fdcaa6c5e2 Enha: return help message on unexpected command 2026-03-14 14:10:59 +03:00
Grail Finder
77c365959d Merge branch 'master' into feat/agent-flow 2026-03-14 12:38:46 +03:00
Grail Finder
f4fcb85570 Feat: run browser 2026-03-14 12:24:11 +03:00
Grail Finder
92acfb7ed4 Feat: run sed 2026-03-14 11:06:18 +03:00
Grail Finder
2901208c80 Feat: minimize top commands agent-clip style 2026-03-14 10:28:04 +03:00
Grail Finder
4cfe2fe37f Fix: linter complaints 2026-03-13 11:19:49 +03:00
Grail Finder
648035b194 Fix: slow startup from silly migrations 2026-03-13 10:18:31 +03:00
Grail Finder
13773bcc97 Merge branch 'master' into feat/agent-flow 2026-03-13 09:26:11 +03:00
Grail Finder
d9b820c9c4 Enha: insert to paste 2026-03-13 09:23:57 +03:00
Grail Finder
adc4dea644 Chore: resize yt link image 2026-03-13 07:56:23 +03:00
Grail Finder
6456cb0922 Doc: add youtube showcase link 2026-03-13 07:52:11 +03:00
Grail Finder
e77dbb3160 Fix: webagent call 2026-03-12 07:59:21 +03:00
Grail Finder
3013f21a75 Chore: remove roleinject from props table
already exists as keybind (alt+7)
2026-03-10 12:22:27 +03:00
Grail Finder
bb57be95c6 Enha (pw_agent): save browser state 2026-03-09 16:43:31 +03:00
Grail Finder
528d4210fc Fix: path to whisper model binary 2026-03-09 11:56:30 +03:00
Grail Finder
54b4cccf92 Fix: make commands for whisper 2026-03-09 10:45:30 +03:00
Grail Finder
c2c90f6d2b Enha: pw agent 2026-03-09 08:50:33 +03:00
Grail Finder
94769225cf Enha: agent client redo [WIP] 2026-03-09 07:50:11 +03:00
Grail Finder
0e42a6f069 Merge branch 'master' into feat/agent-flow 2026-03-09 07:07:36 +03:00
Grail Finder
2687f38d00 Chore: remove unnecessary code 2026-03-05 00:28:47 +03:00
46 changed files with 4720 additions and 2659 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,3 @@
*.txt
*.json
testlog
history/
@@ -18,3 +17,4 @@ chat_exports/*.json
ragimport
.env
onnx/
*.log

View File

@@ -134,8 +134,9 @@ 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 [ ! -d "batteries/whisper.cpp" ]; then \
@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
@@ -144,7 +145,7 @@ build-whisper: ## Build whisper.cpp from source in batteries directory
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" \
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!"

View File

@@ -10,6 +10,10 @@ made with use of [tview](https://github.com/rivo/tview)
- function calls (function calls are implemented natively, to avoid calling outside sources);
- [character specific context (unique feature)](docs/char-specific-context.md)
#### showcase on youtube
[![gf-lt video showcase](assets/yt_thumb.jpg)](https://youtu.be/WCS4Xc902F8 "gf-lt showcase")
#### how it looks
![how it looks](assets/ex01.png)
@@ -34,7 +38,8 @@ make noextra-run
```
#### keybinds
while running you can press f12 for list of keys;
- use `insert` button to paste text from the clipboard to the text area, instead of shift+insert (might freeze the program);
- press f12 for list of keys;
![keybinds](assets/helppage.png)
#### setting up config

View File

@@ -4,11 +4,12 @@ package agent
// ones who do their own tools calls
// ones that works only with the output
// A: main chat -> agent (handles everything: tool + processing)
// A: main chat -> agent (handles everything: tool + processing), supports tool chaining
// 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
// AgenterA gets a task like "go to the webpage, login and take a screenshot (tell me what you see)"
// proceeds to make a plan and executes it.
// returns with final result or an error
type AgenterA interface {
ProcessTask(task string) []byte
}
@@ -38,8 +39,3 @@ func RegisterA(toolNames []string, a AgenterA) {
func Get(toolName string) AgenterB {
return RegistryB[toolName]
}
// Register is a convenience wrapper for RegisterB.
func Register(toolName string, a AgenterB) {
RegisterB(toolName, a)
}

119
agent/pw_agent.go Normal file
View File

@@ -0,0 +1,119 @@
package agent
import (
"encoding/json"
"gf-lt/models"
"strings"
)
// PWAgent: is AgenterA type agent (enclosed with tool chaining)
// sysprompt explain tools and how to plan for execution
type PWAgent struct {
*AgentClient
sysprompt string
}
// NewPWAgent creates a PWAgent with the given client and system prompt
func NewPWAgent(client *AgentClient, sysprompt string) *PWAgent {
return &PWAgent{AgentClient: client, sysprompt: sysprompt}
}
// SetTools sets the tools available to the agent
func (a *PWAgent) SetTools(tools []models.Tool) {
a.tools = tools
}
func (a *PWAgent) ProcessTask(task string) []byte {
req, err := a.FormFirstMsg(a.sysprompt, task)
if err != nil {
a.Log().Error("PWAgent failed to process the request", "error", err)
return []byte("PWAgent failed to process the request; err: " + err.Error())
}
toolCallLimit := 10
for i := 0; i < toolCallLimit; i++ {
resp, err := a.LLMRequest(req)
if err != nil {
a.Log().Error("failed to process the request", "error", err)
return []byte("failed to process the request; err: " + err.Error())
}
execTool, toolCallID, hasToolCall := findToolCall(resp)
if !hasToolCall {
return resp
}
a.setToolCallOnLastMessage(resp, toolCallID)
toolResp := string(execTool())
req, err = a.FormMsgWithToolCallID(toolResp, toolCallID)
if err != nil {
a.Log().Error("failed to form next message", "error", err)
return []byte("failed to form next message; err: " + err.Error())
}
}
return nil
}
func (a *PWAgent) setToolCallOnLastMessage(resp []byte, toolCallID string) {
if toolCallID == "" {
return
}
var genericResp map[string]interface{}
if err := json.Unmarshal(resp, &genericResp); err != nil {
return
}
var name string
var args map[string]string
if choices, ok := genericResp["choices"].([]interface{}); ok && len(choices) > 0 {
if firstChoice, ok := choices[0].(map[string]interface{}); ok {
if message, ok := firstChoice["message"].(map[string]interface{}); ok {
if toolCalls, ok := message["tool_calls"].([]interface{}); ok && len(toolCalls) > 0 {
if tc, ok := toolCalls[0].(map[string]interface{}); ok {
if fn, ok := tc["function"].(map[string]interface{}); ok {
name, _ = fn["name"].(string)
argsStr, _ := fn["arguments"].(string)
_ = json.Unmarshal([]byte(argsStr), &args)
}
}
}
}
}
}
if name == "" {
content, _ := genericResp["content"].(string)
name = extractToolNameFromText(content)
}
lastIdx := len(a.chatBody.Messages) - 1
if lastIdx >= 0 {
a.chatBody.Messages[lastIdx].ToolCallID = toolCallID
if name != "" {
argsJSON, _ := json.Marshal(args)
a.chatBody.Messages[lastIdx].ToolCall = &models.ToolCall{
ID: toolCallID,
Name: name,
Args: string(argsJSON),
}
}
}
}
func extractToolNameFromText(text string) string {
jsStr := toolCallRE.FindString(text)
if jsStr == "" {
return ""
}
jsStr = strings.TrimSpace(jsStr)
jsStr = strings.TrimPrefix(jsStr, "__tool_call__")
jsStr = strings.TrimSuffix(jsStr, "__tool_call__")
jsStr = strings.TrimSpace(jsStr)
start := strings.Index(jsStr, "{")
end := strings.LastIndex(jsStr, "}")
if start == -1 || end == -1 || end <= start {
return ""
}
jsStr = jsStr[start : end+1]
var fc models.FuncCall
if err := json.Unmarshal([]byte(jsStr), &fc); err != nil {
return ""
}
return fc.Name
}

338
agent/pw_tools.go Normal file
View File

@@ -0,0 +1,338 @@
package agent
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"gf-lt/models"
)
type ToolFunc func(map[string]string) []byte
var pwToolMap = make(map[string]ToolFunc)
func RegisterPWTool(name string, fn ToolFunc) {
pwToolMap[name] = fn
}
func GetPWTools() []models.Tool {
return pwTools
}
var pwTools = []models.Tool{
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_start",
Description: "Start a Playwright browser instance. Must be called first before any other browser automation. Uses headless mode by default.",
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": {Type: "string", Description: "URL to navigate to"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_click",
Description: "Click on an element on the current webpage. Use 'index' for multiple matches (default 0).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for the element"},
"index": {Type: "integer", Description: "Index for multiple matches (default 0)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_fill",
Description: "Type text into an input field. Use 'index' for multiple matches (default 0).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector", "text"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for the input element"},
"text": {Type: "string", Description: "Text to type into the field"},
"index": {Type: "integer", Description: "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. Use selector 'body' for all page text.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector (default '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 a file path to the image.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for element to screenshot"},
"full_page": {Type: "boolean", Description: "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 to visually verify page state.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for element to screenshot"},
"full_page": {Type: "boolean", Description: "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 before proceeding.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector to wait for"},
"timeout": {Type: "integer", Description: "Timeout in milliseconds (default 30000)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_drag",
Description: "Drag the mouse from point (x1,y1) to (x2,y2).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"x1", "y1", "x2", "y2"},
Properties: map[string]models.ToolArgProps{
"x1": {Type: "number", Description: "Starting X coordinate"},
"y1": {Type: "number", Description: "Starting Y coordinate"},
"x2": {Type: "number", Description: "Ending X coordinate"},
"y2": {Type: "number", Description: "Ending Y coordinate"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_click_at",
Description: "Click at specific X,Y coordinates on the page.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"x", "y"},
Properties: map[string]models.ToolArgProps{
"x": {Type: "number", Description: "X coordinate"},
"y": {Type: "number", Description: "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": {Type: "string", Description: "CSS selector (default 'body')"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_get_dom",
Description: "Get a structured DOM representation with tag, attributes, text, and children.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector (default 'body')"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_search_elements",
Description: "Search for elements by text content or CSS selector.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"text": {Type: "string", Description: "Text content to search for"},
"selector": {Type: "string", Description: "CSS selector to search for"},
},
},
},
},
}
var toolCallRE = regexp.MustCompile(`__tool_call__(.+?)__tool_call__`)
type ParsedToolCall struct {
ID string
Name string
Args map[string]string
}
func findToolCall(resp []byte) (func() []byte, string, bool) {
var genericResp map[string]interface{}
if err := json.Unmarshal(resp, &genericResp); err != nil {
return findToolCallFromText(string(resp))
}
if choices, ok := genericResp["choices"].([]interface{}); ok && len(choices) > 0 {
if firstChoice, ok := choices[0].(map[string]interface{}); ok {
if message, ok := firstChoice["message"].(map[string]interface{}); ok {
if toolCalls, ok := message["tool_calls"].([]interface{}); ok && len(toolCalls) > 0 {
return parseOpenAIToolCall(toolCalls)
}
if content, ok := message["content"].(string); ok {
return findToolCallFromText(content)
}
}
if text, ok := firstChoice["text"].(string); ok {
return findToolCallFromText(text)
}
}
}
if content, ok := genericResp["content"].(string); ok {
return findToolCallFromText(content)
}
return findToolCallFromText(string(resp))
}
func parseOpenAIToolCall(toolCalls []interface{}) (func() []byte, string, bool) {
if len(toolCalls) == 0 {
return nil, "", false
}
tc := toolCalls[0].(map[string]interface{})
id, _ := tc["id"].(string)
function, _ := tc["function"].(map[string]interface{})
name, _ := function["name"].(string)
argsStr, _ := function["arguments"].(string)
var args map[string]string
if err := json.Unmarshal([]byte(argsStr), &args); err != nil {
return func() []byte {
return []byte(fmt.Sprintf(`{"error": "failed to parse arguments: %v"}`, err))
}, id, true
}
return func() []byte {
fn, ok := pwToolMap[name]
if !ok {
return []byte(fmt.Sprintf(`{"error": "tool %s not found"}`, name))
}
return fn(args)
}, id, true
}
func findToolCallFromText(text string) (func() []byte, string, bool) {
jsStr := toolCallRE.FindString(text)
if jsStr == "" {
return nil, "", false
}
jsStr = strings.TrimSpace(jsStr)
jsStr = strings.TrimPrefix(jsStr, "__tool_call__")
jsStr = strings.TrimSuffix(jsStr, "__tool_call__")
jsStr = strings.TrimSpace(jsStr)
start := strings.Index(jsStr, "{")
end := strings.LastIndex(jsStr, "}")
if start == -1 || end == -1 || end <= start {
return func() []byte {
return []byte(`{"error": "no valid JSON found in tool call"}`)
}, "", true
}
jsStr = jsStr[start : end+1]
var fc models.FuncCall
if err := json.Unmarshal([]byte(jsStr), &fc); err != nil {
return func() []byte {
return []byte(fmt.Sprintf(`{"error": "failed to parse tool call: %v}`, err))
}, "", true
}
if fc.ID == "" {
fc.ID = "call_" + generateToolCallID()
}
return func() []byte {
fn, ok := pwToolMap[fc.Name]
if !ok {
return []byte(fmt.Sprintf(`{"error": "tool %s not found"}`, fc.Name))
}
return fn(fc.Args)
}, fc.ID, true
}
func generateToolCallID() string {
return strconv.Itoa(len(pwToolMap) % 10000)
}

View File

@@ -30,12 +30,16 @@ func detectAPI(api string) (isCompletion, isChat, isDeepSeek, isOpenRouter bool)
}
type AgentClient struct {
cfg *config.Config
getToken func() string
log slog.Logger
cfg *config.Config
getToken func() string
log *slog.Logger
chatBody *models.ChatBody
sysprompt string
// lastToolCallID string
tools []models.Tool
}
func NewAgentClient(cfg *config.Config, log slog.Logger, gt func() string) *AgentClient {
func NewAgentClient(cfg *config.Config, log *slog.Logger, gt func() string) *AgentClient {
return &AgentClient{
cfg: cfg,
getToken: gt,
@@ -44,11 +48,46 @@ func NewAgentClient(cfg *config.Config, log slog.Logger, gt func() string) *Agen
}
func (ag *AgentClient) Log() *slog.Logger {
return &ag.log
return ag.log
}
func (ag *AgentClient) FormMsg(sysprompt, msg string) (io.Reader, error) {
b, err := ag.buildRequest(sysprompt, msg)
func (ag *AgentClient) FormFirstMsg(sysprompt, msg string) (io.Reader, error) {
ag.sysprompt = sysprompt
ag.chatBody = &models.ChatBody{
Messages: []models.RoleMsg{
{Role: "system", Content: ag.sysprompt},
{Role: "user", Content: msg},
},
Stream: false,
Model: ag.cfg.CurrentModel,
}
b, err := ag.buildRequest()
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func (ag *AgentClient) FormMsg(msg string) (io.Reader, error) {
m := models.RoleMsg{
Role: "tool", Content: msg,
}
ag.chatBody.Messages = append(ag.chatBody.Messages, m)
b, err := ag.buildRequest()
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func (ag *AgentClient) FormMsgWithToolCallID(msg, toolCallID string) (io.Reader, error) {
m := models.RoleMsg{
Role: "tool",
Content: msg,
ToolCallID: toolCallID,
}
ag.chatBody.Messages = append(ag.chatBody.Messages, m)
b, err := ag.buildRequest()
if err != nil {
return nil, err
}
@@ -56,81 +95,52 @@ func (ag *AgentClient) FormMsg(sysprompt, msg string) (io.Reader, error) {
}
// 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)
func (ag *AgentClient) buildRequest() ([]byte, error) {
isCompletion, isChat, isDeepSeek, isOpenRouter := detectAPI(ag.cfg.CurrentAPI)
ag.log.Debug("agent building request", "api", ag.cfg.CurrentAPI, "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())
for i := range ag.chatBody.Messages {
sb.WriteString(ag.chatBody.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 := models.NewDSCompletionReq(prompt, ag.chatBody.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 := models.NewOpenRouterCompletionReq(ag.chatBody.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 := models.NewLCPReq(prompt, ag.chatBody.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)
switch {
case isDeepSeek:
// DeepSeek chat
req := models.NewDSChatReq(*ag.chatBody)
return json.Marshal(req)
case isOpenRouter:
// OpenRouter chat - agents don't use reasoning by default
req := models.NewOpenRouterChatReq(*ag.chatBody, defaultProps, ag.cfg.ReasoningEffort)
return json.Marshal(req)
default:
// Assume llama.cpp chat (OpenAI format)
req := models.OpenAIReq{
ChatBody: ag.chatBody,
Tools: ag.tools,
}
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) {
@@ -165,7 +175,6 @@ func (ag *AgentClient) LLMRequest(body io.Reader) ([]byte, error) {
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 {
@@ -179,17 +188,16 @@ func (ag *AgentClient) LLMRequest(body io.Reader) ([]byte, error) {
// 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{}
var genericResp map[string]any
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 {
if choices, ok := genericResp["choices"].([]any); ok && len(choices) > 0 {
if firstChoice, ok := choices[0].(map[string]any); ok {
// Chat completion: choices[0].message.content
if message, ok := firstChoice["message"].(map[string]interface{}); ok {
if message, ok := firstChoice["message"].(map[string]any); ok {
if content, ok := message["content"].(string); ok {
return content, nil
}
@@ -199,19 +207,17 @@ func extractTextFromResponse(data []byte) (string, error) {
return text, nil
}
// Delta format for streaming (should not happen with stream: false)
if delta, ok := firstChoice["delta"].(map[string]interface{}); ok {
if delta, ok := firstChoice["delta"].(map[string]any); 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 {
@@ -219,10 +225,3 @@ func extractTextFromResponse(data []byte) (string, error) {
}
return string(prettyJSON), nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}

View File

@@ -17,7 +17,8 @@ func NewWebAgentB(client *AgentClient, sysprompt string) *WebAgentB {
// 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,
msg, err := a.FormFirstMsg(
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)

BIN
assets/yt_thumb.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

328
bot.go
View File

@@ -11,6 +11,7 @@ import (
"gf-lt/models"
"gf-lt/rag"
"gf-lt/storage"
"gf-lt/tools"
"html"
"io"
"log/slog"
@@ -24,29 +25,83 @@ import (
"sync"
"sync/atomic"
"time"
"github.com/rivo/tview"
)
var (
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.FullRepo
defaultFirstMsg = "Hello! What can I do for you?"
defaultStarter = []models.RoleMsg{}
interruptResp atomic.Bool
ragger *rag.RAG
chunkParser ChunkParser
lastToolCall *models.FuncCall
lastRespStats *models.ResponseStats
//nolint:unused // TTS_ENABLED conditionally uses this
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.FullRepo
defaultStarter = []models.RoleMsg{}
interruptResp atomic.Bool
ragger *rag.RAG
chunkParser ChunkParser
lastToolCall *models.FuncCall
lastRespStats *models.ResponseStats
outputHandler OutputHandler
cliPrevOutput string
cliRespDone chan bool
)
type OutputHandler interface {
Write(p string)
Writef(format string, args ...interface{})
ScrollToEnd()
}
type TUIOutputHandler struct {
tv *tview.TextView
}
func (h *TUIOutputHandler) Write(p string) {
fmt.Fprint(h.tv, p)
}
func (h *TUIOutputHandler) Writef(format string, args ...interface{}) {
fmt.Fprintf(h.tv, format, args...)
}
func (h *TUIOutputHandler) ScrollToEnd() {
h.tv.ScrollToEnd()
}
type CLIOutputHandler struct{}
func (h *CLIOutputHandler) Write(p string) {
fmt.Print(p)
}
func (h *CLIOutputHandler) Writef(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func (h *CLIOutputHandler) ScrollToEnd() {
}
var (
basicCard = &models.CharCard{
ID: models.ComputeCardID("assistant", "basic_sys"),
SysPrompt: models.BasicSysMsg,
FirstMsg: models.DefaultFirstMsg,
Role: "assistant",
FilePath: "basic_sys",
}
sysMap = map[string]*models.CharCard{}
roleToID = map[string]string{}
modelHasVision bool
windowToolsAvailable bool
// tooler *tools.Tools
//
orator Orator
asr STT
localModelsMu sync.RWMutex
@@ -458,6 +513,29 @@ func ModelHasVision(api, modelID string) bool {
}
}
func UpdateToolCapabilities() {
if !cfg.ToolUse {
return
}
modelHasVision = false
if cfg == nil || cfg.CurrentAPI == "" {
logger.Warn("cannot determine model capabilities: cfg or CurrentAPI is nil")
// tooler.RegisterWindowTools(modelHasVision)
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")
}
}
// tooler.RegisterWindowTools(modelHasVision)
}
// monitorModelLoad starts a goroutine that periodically checks if the specified model is loaded.
func monitorModelLoad(modelID string) {
go func() {
@@ -764,6 +842,10 @@ func chatWatcher(ctx context.Context) {
// inpired by https://github.com/rivo/tview/issues/225
func showSpinner() {
if cfg.CLIMode {
showSpinnerCLI()
return
}
spinners := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
var i int
botPersona := cfg.AssistantRole
@@ -790,6 +872,12 @@ func showSpinner() {
})
}
func showSpinnerCLI() {
for botRespMode.Load() || toolRunningMode.Load() {
time.Sleep(400 * time.Millisecond)
}
}
func chatRound(r *models.ChatRoundReq) error {
interruptResp.Store(false)
botRespMode.Store(true)
@@ -822,13 +910,22 @@ func chatRound(r *models.ChatRoundReq) error {
Role: botPersona, Content: "",
})
nl := "\n\n"
prevText := textView.GetText(true)
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
prevText := cliPrevOutput
if cfg.CLIMode {
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
}
} else {
prevText = textView.GetText(true)
if strings.HasSuffix(prevText, nl) {
nl = ""
} else if strings.HasSuffix(prevText, "\n") {
nl = "\n"
}
}
fmt.Fprintf(textView, "%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
outputHandler.Writef("%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
} else {
msgIdx = len(chatBody.Messages) - 1
}
@@ -850,9 +947,9 @@ out:
thinkingBuffer.WriteString(chunk)
if thinkingCollapsed {
// Show placeholder immediately when thinking starts in collapsed mode
fmt.Fprint(textView, "[yellow::i][thinking... (press Alt+T to expand)][-:-:-]")
outputHandler.Write("[yellow::i][thinking... (press Alt+T to expand)][-:-:-]")
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
respText.WriteString(chunk)
continue
@@ -867,7 +964,7 @@ out:
respText.WriteString(chunk)
justExitedThinkingCollapsed = true
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
continue
}
@@ -884,32 +981,32 @@ out:
chunk = "\n\n" + chunk
justExitedThinkingCollapsed = false
}
fmt.Fprint(textView, chunk)
outputHandler.Write(chunk)
respText.WriteString(chunk)
// Update the message in chatBody.Messages so it persists during Alt+T
if !r.Resume {
chatBody.Messages[msgIdx].Content += respText.String()
}
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
// Send chunk to audio stream handler
if cfg.TTS_ENABLED {
TTSTextChan <- chunk
}
case toolChunk := <-openAIToolChan:
fmt.Fprint(textView, toolChunk)
outputHandler.Write(toolChunk)
toolResp.WriteString(toolChunk)
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
case <-streamDone:
for len(chunkChan) > 0 {
chunk := <-chunkChan
fmt.Fprint(textView, chunk)
outputHandler.Write(chunk)
respText.WriteString(chunk)
if cfg.AutoScrollEnabled {
textView.ScrollToEnd()
outputHandler.ScrollToEnd()
}
if cfg.TTS_ENABLED {
TTSTextChan <- chunk
@@ -951,8 +1048,7 @@ out:
cleanChatBody()
refreshChatDisplay()
updateStatusLine()
// bot msg is done;
// now check it for func call
// bot msg is done; now check it for func call
// logChat(activeChatName, chatBody.Messages)
if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil {
logger.Warn("failed to update storage", "error", err, "name", activeChatName)
@@ -963,8 +1059,16 @@ out:
return nil
}
if findCall(respTextNoThink, toolResp.String()) {
// Tool was found and executed, subsequent chatRound will signal cliRespDone when complete
return nil
}
// No tool call - signal completion now
if cfg.CLIMode && cliRespDone != nil {
select {
case cliRespDone <- true:
default:
}
}
// 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
@@ -1102,7 +1206,7 @@ func findCall(msg, toolCall string) bool {
// 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)
jsStr := models.ToolCallRE.FindString(msg)
if jsStr == "" { // no tool call case
return false
}
@@ -1170,17 +1274,42 @@ func findCall(msg, toolCall string) bool {
Args: mapToString(lastToolCall.Args),
}
// call a func
_, ok := fnMap[fc.Name]
if !ok {
m := fc.Name + " is not implemented"
// _, ok := tools.FnMap[fc.Name]
// if !ok {
// 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
// }
// Show tool call progress indicator before execution
outputHandler.Writef("\n[yellow::i][tool: %s...][-:-:-]", fc.Name)
toolRunningMode.Store(true)
resp, okT := tools.CallToolWithAgent(fc.Name, fc.Args)
if !okT {
// Create tool response message with the proper tool_call_id
toolResponseMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: m,
Content: string(resp),
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))
logger.Debug("findCall: added tool not implemented response", "role", toolResponseMsg.Role,
"content_len", len(toolResponseMsg.Content), "tool_call_id", toolResponseMsg.ToolCallID)
// Clear the stored tool call ID after using it
lastToolCall.ID = ""
// Trigger the assistant to continue processing with the new tool response
@@ -1192,10 +1321,6 @@ func findCall(msg, toolCall string) bool {
chatRoundChan <- crr
return true
}
// 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)
@@ -1250,10 +1375,9 @@ func findCall(msg, toolCall string) bool {
IsShellCommand: isShellCommand,
}
}
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
outputHandler.Writef("%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
"\n\n", len(chatBody.Messages), cfg.ToolRole, toolResponseMsg.GetText())
chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
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
@@ -1274,11 +1398,19 @@ func chatToTextSlice(messages []models.RoleMsg, showSys bool) []string {
// 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")
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")
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
}
@@ -1312,7 +1444,7 @@ func chatToText(messages []models.RoleMsg, showSys bool) string {
text := strings.Join(s, "\n")
// Collapse thinking blocks if enabled
if thinkingCollapsed {
text = thinkRE.ReplaceAllStringFunc(text, func(match string) string {
text = models.ThinkRE.ReplaceAllStringFunc(text, func(match string) string {
// Extract content between <think> and </think>
start := len("<think>")
end := len(match) - len("</think>")
@@ -1328,7 +1460,9 @@ func chatToText(messages []models.RoleMsg, showSys bool) string {
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))
placeholder := fmt.Sprintf(
"[yellow::i][thinking... (%d chars) (press Alt+T to expand)][-:-:-]",
len(content))
text = text[:startIdx] + placeholder
}
}
@@ -1409,7 +1543,7 @@ func updateModelLists() {
chatBody.Model = m
cachedModelColor.Store("green")
updateStatusLine()
updateToolCapabilities()
UpdateToolCapabilities()
app.Draw()
return
}
@@ -1434,14 +1568,42 @@ func refreshLocalModelsIfEmpty() {
localModelsMu.Unlock()
}
func startNewCLIChat() []models.RoleMsg {
id, err := store.ChatGetMaxID()
if err != nil {
logger.Error("failed to get chat id", "error", err)
}
id++
charToStart(cfg.AssistantRole, false)
newChat := &models.Chat{
ID: id,
Name: fmt.Sprintf("%d_%s", id, cfg.AssistantRole),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Msgs: "",
Agent: cfg.AssistantRole,
}
activeChatName = newChat.Name
chatMap[newChat.Name] = newChat
cliPrevOutput = ""
return chatBody.Messages
}
func startNewCLIErrors() []models.RoleMsg {
return startNewCLIChat()
}
func summarizeAndStartNewChat() {
if len(chatBody.Messages) == 0 {
showToast("info", "No chat history to summarize")
return
}
showToast("info", "Summarizing chat history...")
arg := map[string]string{
"chat": chatToText(chatBody.Messages, false),
}
// Call the summarize_chat tool via agent
summaryBytes := callToolWithAgent("summarize_chat", map[string]string{})
summaryBytes, _ := tools.CallToolWithAgent("summarize_chat", arg)
summary := string(summaryBytes)
if summary == "" {
showToast("error", "Failed to generate summary")
@@ -1457,8 +1619,10 @@ func summarizeAndStartNewChat() {
}
chatBody.Messages = append(chatBody.Messages, toolMsg)
// Update UI
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
if !cfg.CLIMode {
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
}
// Update storage
if err := updateStorageChat(activeChatName, chatBody.Messages); err != nil {
logger.Warn("failed to update storage after injecting summary", "error", err)
@@ -1477,8 +1641,8 @@ func init() {
return
}
defaultStarter = []models.RoleMsg{
{Role: "system", Content: basicSysMsg},
{Role: cfg.AssistantRole, Content: defaultFirstMsg},
{Role: "system", Content: models.BasicSysMsg},
{Role: cfg.AssistantRole, Content: models.DefaultFirstMsg},
}
logfile, err := os.OpenFile(cfg.LogFile,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -1489,6 +1653,8 @@ func init() {
return
}
// load cards
sysMap[basicCard.ID] = basicCard
roleToID["assistant"] = basicCard.ID
basicCard.Role = cfg.AssistantRole
logLevel.Set(slog.LevelInfo)
logger = slog.New(slog.NewTextHandler(logfile, &slog.HandlerOptions{Level: logLevel}))
@@ -1514,7 +1680,12 @@ func init() {
return
}
lastToolCall = &models.FuncCall{}
lastChat := loadOldChatOrGetNew()
var lastChat []models.RoleMsg
if cfg.CLIMode {
lastChat = startNewCLIErrors()
} else {
lastChat = loadOldChatOrGetNew()
}
chatBody = &models.ChatBody{
Model: "modelname",
Stream: true,
@@ -1529,25 +1700,30 @@ func init() {
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
go func() {
if err := tools.CheckPlaywright(); err != nil {
if err := tools.InstallPW(); err != nil {
logger.Error("failed to install playwright", "error", err)
cancel()
os.Exit(1)
return
}
if err := tools.CheckPlaywright(); err != nil {
logger.Error("failed to run 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()
if !cfg.CLIMode {
initTUI()
}
tools.InitTools(cfg, logger, store)
// tooler = tools.InitTools(cfg, logger, store)
// tooler.RegisterWindowTools(modelHasVision)
}

74
cli-tests/sort-img/check.sh Executable file
View File

@@ -0,0 +1,74 @@
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_FILE=$(ls -t "$SCRIPT_DIR"/*_run.log 2>/dev/null | head -1)
PASS=0
FAIL=0
log_pass() {
echo "[PASS] $1"
PASS=$((PASS + 1))
}
log_fail() {
echo "[FAIL] $1"
FAIL=$((FAIL + 1))
}
echo "=== Checking results ==="
echo ""
# Check has-animals directory exists
if [ -d "/tmp/sort-img/has-animals" ]; then
log_pass "has-animals directory exists"
else
log_fail "has-animals directory missing"
fi
# Check no-animals directory exists
if [ -d "/tmp/sort-img/no-animals" ]; then
log_pass "no-animals directory exists"
else
log_fail "no-animals directory missing"
fi
# Check has-animals contains at least one image
HAS_ANIMALS_FILES=$(ls -1 /tmp/sort-img/has-animals 2>/dev/null | wc -l)
if [ "$HAS_ANIMALS_FILES" -gt 0 ]; then
log_pass "has-animals contains images ($HAS_ANIMALS_FILES files)"
else
log_fail "has-animals is empty"
fi
# Check no-animals contains at least one image
NO_ANIMALS_FILES=$(ls -1 /tmp/sort-img/no-animals 2>/dev/null | wc -l)
if [ "$NO_ANIMALS_FILES" -gt 0 ]; then
log_pass "no-animals contains images ($NO_ANIMALS_FILES files)"
else
log_fail "no-animals is empty"
fi
# Check total files sorted correctly (3 original files should be in subdirs)
TOTAL_SORTED=$((HAS_ANIMALS_FILES + NO_ANIMALS_FILES))
if [ "$TOTAL_SORTED" -eq 3 ]; then
log_pass "all 3 files sorted into subdirectories"
else
log_fail "expected 3 files sorted, got $TOTAL_SORTED"
fi
echo ""
echo "=== Summary ==="
echo "PASSED: $PASS"
echo "FAILED: $FAIL"
if [ $FAIL -gt 0 ]; then
echo ""
echo "Log file: $LOG_FILE"
exit 1
fi
echo ""
echo "All tests passed!"
exit 0

25
cli-tests/sort-img/run.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="$SCRIPT_DIR/${TIMESTAMP}_run.log"
exec > "$LOG_FILE" 2>&1
echo "=== Running teardown ==="
"$SCRIPT_DIR/teardown.sh"
echo ""
echo "=== Running setup ==="
"$SCRIPT_DIR/setup.sh"
echo ""
echo "=== Running task ==="
TASK=$(cat "$SCRIPT_DIR/task.txt")
cd /home/grail/projects/plays/goplays/gf-lt
go run . -cli -msg "$TASK"
echo ""
echo "=== Done ==="
echo "Log file: $LOG_FILE"

9
cli-tests/sort-img/setup.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/sh
set -e
mkdir -p /tmp/sort-img
cp ../../../assets/ex01.png /tmp/sort-img/file1.png
cp ../../../assets/helppage.png /tmp/sort-img/file2.png
cp ../../../assets/yt_thumb.jpg /tmp/sort-img/file3.jpg

View File

@@ -0,0 +1,2 @@
go to /tmp/sort-img, create directories: has-animals, no-animals
sort images in /tmp/sort-img into created directories by content

4
cli-tests/sort-img/teardown.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -e
rm -rf /tmp/sort-img

91
cli-tests/sort-text/check.sh Executable file
View File

@@ -0,0 +1,91 @@
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_FILE=$(ls -t "$SCRIPT_DIR"/*_run.log 2>/dev/null | head -1)
PASS=0
FAIL=0
log_pass() {
echo "[PASS] $1"
PASS=$((PASS + 1))
}
log_fail() {
echo "[FAIL] $1"
FAIL=$((FAIL + 1))
}
echo "=== Checking results ==="
echo ""
# Check animals directory exists
if [ -d "/tmp/sort-text/animals" ]; then
log_pass "animals directory exists"
else
log_fail "animals directory missing"
fi
# Check colors directory exists
if [ -d "/tmp/sort-text/colors" ]; then
log_pass "colors directory exists"
else
log_fail "colors directory missing"
fi
# Check animals contain cat/dog
ANIMALS_FILES=$(ls -1 /tmp/sort-text/animals 2>/dev/null | tr '\n' ' ')
if echo "$ANIMALS_FILES" | grep -q "file1.txt" && echo "$ANIMALS_FILES" | grep -q "file3.txt"; then
log_pass "animals contains animal files"
else
log_fail "animals missing animal files (got: $ANIMALS_FILES)"
fi
# Check colors contain red/blue
COLORS_FILES=$(ls -1 /tmp/sort-text/colors 2>/dev/null | tr '\n' ' ')
if echo "$COLORS_FILES" | grep -q "file2.txt" && echo "$COLORS_FILES" | grep -q "file4.txt"; then
log_pass "colors contains color files"
else
log_fail "colors missing color files (got: $COLORS_FILES)"
fi
# Verify content
if grep -q "cat" /tmp/sort-text/animals/file1.txt 2>/dev/null; then
log_pass "file1.txt contains 'cat'"
else
log_fail "file1.txt missing 'cat'"
fi
if grep -q "dog" /tmp/sort-text/animals/file3.txt 2>/dev/null; then
log_pass "file3.txt contains 'dog'"
else
log_fail "file3.txt missing 'dog'"
fi
if grep -q "red" /tmp/sort-text/colors/file2.txt 2>/dev/null; then
log_pass "file2.txt contains 'red'"
else
log_fail "file2.txt missing 'red'"
fi
if grep -q "blue" /tmp/sort-text/colors/file4.txt 2>/dev/null; then
log_pass "file4.txt contains 'blue'"
else
log_fail "file4.txt missing 'blue'"
fi
echo ""
echo "=== Summary ==="
echo "PASSED: $PASS"
echo "FAILED: $FAIL"
if [ $FAIL -gt 0 ]; then
echo ""
echo "Log file: $LOG_FILE"
exit 1
fi
echo ""
echo "All tests passed!"
exit 0

25
cli-tests/sort-text/run.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="$SCRIPT_DIR/${TIMESTAMP}_run.log"
exec > "$LOG_FILE" 2>&1
echo "=== Running teardown ==="
"$SCRIPT_DIR/teardown.sh"
echo ""
echo "=== Running setup ==="
"$SCRIPT_DIR/setup.sh"
echo ""
echo "=== Running task ==="
TASK=$(cat "$SCRIPT_DIR/task.txt")
cd /home/grail/projects/plays/goplays/gf-lt
go run . -cli -msg "$TASK"
echo ""
echo "=== Done ==="
echo "Log file: $LOG_FILE"

10
cli-tests/sort-text/setup.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/sh
set -e
mkdir -p /tmp/sort-text
printf "cat" > /tmp/sort-text/file1.txt
printf "red" > /tmp/sort-text/file2.txt
printf "dog" > /tmp/sort-text/file3.txt
printf "blue" > /tmp/sort-text/file4.txt

View File

@@ -0,0 +1,2 @@
go to /tmp/sort-text, create directories: animals, colors
sort /tmp/sort-text/*.txt into created directories by text content

View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -e
rm -rf /tmp/sort-text

View File

@@ -18,7 +18,7 @@ EmbedTokenizerPath = "onnx/embedgemma/tokenizer.json"
EmbedDims = 768
#
ShowSys = true
LogFile = "log.txt"
LogFile = "log.log"
UserRole = "user"
ToolRole = "tool"
AssistantRole = "assistant"
@@ -42,7 +42,7 @@ 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)
WhisperModelPath = "./batteries/whisper.cpp/models/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
#
@@ -61,4 +61,4 @@ StripThinkingFromAPI = true # Strip <think> blocks from messages before sending
ReasoningEffort = "medium"
# playwright tools
PlaywrightEnabled = false
PlaywrightDebug = false
PlaywrightDebug = false # when true opens in gui mode (headless=false)

View File

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

View File

@@ -86,8 +86,8 @@ func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
}
// Strip thinking from assistant messages
msgText := msg.GetText()
if thinkRE.MatchString(msgText) {
cleanedText := thinkRE.ReplaceAllString(msgText, "")
if models.ThinkRE.MatchString(msgText) {
cleanedText := models.ThinkRE.ReplaceAllString(msgText, "")
cleanedText = strings.TrimSpace(cleanedText)
msg.SetText(cleanedText)
}
@@ -148,7 +148,7 @@ func colorText() {
placeholderThink := "__THINK_BLOCK_%d__"
counterThink := 0
// Replace code blocks with placeholders and store their styled versions
text = codeBlockRE.ReplaceAllStringFunc(text, func(match string) string {
text = models.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)
@@ -157,7 +157,7 @@ func colorText() {
counter++
return id
})
text = thinkRE.ReplaceAllStringFunc(text, func(match string) string {
text = models.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)
@@ -167,10 +167,10 @@ func colorText() {
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[-:-:-]`)
text = models.QuotesRE.ReplaceAllString(text, `[orange::-]$1[-:-:-]`)
text = models.StarRE.ReplaceAllString(text, `[turquoise::i]$1[-:-:-]`)
text = models.SingleBacktickRE.ReplaceAllString(text, "`[pink::i]$1[-:-:-]`")
// text = tools.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)
@@ -188,7 +188,7 @@ func updateStatusLine() {
func initSysCards() ([]string, error) {
labels := []string{}
labels = append(labels, sysLabels...)
labels = append(labels, models.SysLabels...)
cards, err := pngmeta.ReadDirCards(cfg.SysDir, cfg.UserRole, logger)
if err != nil {
logger.Error("failed to read sys dir", "error", err)
@@ -1015,3 +1015,11 @@ func triggerPrivateMessageResponses(msg *models.RoleMsg) {
fmt.Fprint(textView, "[-:-:-]\n")
chatRoundChan <- crr
}
func GetCardByRole(role string) *models.CharCard {
cardID, ok := roleToID[role]
if !ok {
return nil
}
return sysMap[cardID]
}

15
llm.go
View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"gf-lt/models"
"gf-lt/tools"
"io"
"strings"
)
@@ -11,10 +12,10 @@ import (
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
// containsToolSysMsg checks if the tools.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 {
if chatBody.Messages[i].Role == cfg.ToolRole && chatBody.Messages[i].Content == tools.ToolSysMsg {
return true
}
}
@@ -144,7 +145,7 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
}
// 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})
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: tools.ToolSysMsg})
}
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
// Build prompt and extract images inline as we process each message
@@ -331,7 +332,7 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
Tools: nil,
}
if cfg.ToolUse && !resume && role != cfg.ToolRole {
req.Tools = baseTools // set tools to use
req.Tools = tools.BaseTools // set tools to use
}
data, err := json.Marshal(req)
if err != nil {
@@ -384,7 +385,7 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
}
// 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})
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: tools.ToolSysMsg})
}
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages))
@@ -536,7 +537,7 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
}
// 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})
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: tools.ToolSysMsg})
}
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
messages := make([]string, len(filteredMessages))
@@ -671,7 +672,7 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
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
orBody.Tools = tools.BaseTools // set tools to use
}
data, err := json.Marshal(orBody)
if err != nil {

255
main.go
View File

@@ -1,6 +1,15 @@
package main
import (
"bufio"
"flag"
"fmt"
"gf-lt/models"
"gf-lt/pngmeta"
"os"
"slices"
"strconv"
"strings"
"sync/atomic"
"github.com/rivo/tview"
@@ -22,9 +31,22 @@ var (
statusLineTempl = "help (F12) | chat: [orange:-:b]%s[-:-:-] (F1) | [%s:-:b]tool use[-:-:-] (ctrl+k) | model: [%s:-:b]%s[-:-:-] (ctrl+l) | [%s:-:b]skip LLM resp[-:-:-] (F10) | API: [orange:-:b]%s[-:-:-] (ctrl+v)\nwriting as: [orange:-:b]%s[-:-:-] (ctrl+q) | bot will write as [orange:-:b]%s[-:-:-] (ctrl+x)"
focusSwitcher = map[tview.Primitive]tview.Primitive{}
app *tview.Application
cliCardPath string
cliContinue bool
cliMsg string
)
func main() {
flag.BoolVar(&cfg.CLIMode, "cli", false, "Run in CLI mode without TUI")
flag.BoolVar(&cfg.ToolUse, "tools", true, "run with tools")
flag.StringVar(&cliCardPath, "card", "", "Path to syscard JSON file")
flag.BoolVar(&cliContinue, "continue", false, "Continue from last chat (by agent or card)")
flag.StringVar(&cliMsg, "msg", "", "Send message and exit (one-shot mode)")
flag.Parse()
if cfg.CLIMode {
runCLIMode()
return
}
pages.AddPage("main", flex, true, true)
if err := app.SetRoot(pages,
true).EnableMouse(cfg.EnableMouse).EnablePaste(true).Run(); err != nil {
@@ -32,3 +54,236 @@ func main() {
return
}
}
func runCLIMode() {
outputHandler = &CLIOutputHandler{}
cliRespDone = make(chan bool, 1)
if cliCardPath != "" {
card, err := pngmeta.ReadCardJson(cliCardPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load syscard: %v\n", err)
os.Exit(1)
}
cfg.AssistantRole = card.Role
sysMap[card.ID] = card
roleToID[card.Role] = card.ID
charToStart(card.Role, false)
fmt.Printf("Loaded syscard: %s (%s)\n", card.Role, card.FilePath)
}
if cliContinue {
if cliCardPath != "" {
history, err := loadAgentsLastChat(cfg.AssistantRole)
if err != nil {
fmt.Printf("No previous chat found for %s, starting new chat\n", cfg.AssistantRole)
startNewCLIChat()
} else {
chatBody.Messages = history
fmt.Printf("Continued chat: %s\n", activeChatName)
}
} else {
chatBody.Messages = loadOldChatOrGetNew()
fmt.Printf("Continued chat: %s\n", activeChatName)
}
} else {
startNewCLIChat()
}
printCLIWelcome()
go func() {
<-ctx.Done()
os.Exit(0)
}()
if cliMsg != "" {
persona := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
persona = cfg.WriteNextMsgAs
}
chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: cliMsg}
<-cliRespDone
fmt.Println()
return
}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if !scanner.Scan() {
break
}
msg := scanner.Text()
if msg == "" {
continue
}
if strings.HasPrefix(msg, "/") {
if !handleCLICommand(msg) {
return
}
fmt.Println()
continue
}
persona := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
persona = cfg.WriteNextMsgAs
}
chatRoundChan <- &models.ChatRoundReq{Role: persona, UserMsg: msg}
<-cliRespDone
fmt.Println()
}
}
func printCLIWelcome() {
fmt.Println("CLI Mode started. Type your messages or commands.")
fmt.Println("Type /help for available commands.")
fmt.Println()
}
func printCLIHelp() {
fmt.Println("Available commands:")
fmt.Println(" /help, /h - Show this help message")
fmt.Println(" /new, /n - Start a new chat (clears conversation)")
fmt.Println(" /card <path>, /c <path> - Load a different syscard")
fmt.Println(" /undo, /u - Delete last message")
fmt.Println(" /history, /ls - List chat history")
fmt.Println(" /load <name> - Load a specific chat by name")
fmt.Println(" /model <name>, /m <name> - Switch model")
fmt.Println(" /api <index>, /a <index> - Switch API link (no index to list)")
fmt.Println(" /quit, /q, /exit - Exit CLI mode")
fmt.Println()
fmt.Printf("Current syscard: %s\n", cfg.AssistantRole)
fmt.Printf("Current model: %s\n", chatBody.Model)
fmt.Printf("Current API: %s\n", cfg.CurrentAPI)
fmt.Println()
}
func handleCLICommand(msg string) bool {
parts := strings.Fields(msg)
cmd := strings.ToLower(parts[0])
args := parts[1:]
switch cmd {
case "/help", "/h":
printCLIHelp()
case "/new", "/n":
startNewCLIChat()
fmt.Println("New chat started.")
fmt.Printf("Syscard: %s\n", cfg.AssistantRole)
fmt.Println()
case "/card", "/c":
if len(args) == 0 {
fmt.Println("Usage: /card <path>")
return true
}
card, err := pngmeta.ReadCardJson(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load syscard: %v\n", err)
return true
}
cfg.AssistantRole = card.Role
sysMap[card.ID] = card
roleToID[card.Role] = card.ID
charToStart(card.Role, false)
startNewCLIChat()
fmt.Printf("Switched to syscard: %s (%s)\n", card.Role, card.FilePath)
case "/undo", "/u":
if len(chatBody.Messages) == 0 {
fmt.Println("No messages to delete.")
return true
}
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
cliPrevOutput = ""
fmt.Println("Last message deleted.")
case "/history", "/ls":
fmt.Println("Chat history:")
for name := range chatMap {
marker := " "
if name == activeChatName {
marker = "* "
}
fmt.Printf("%s%s\n", marker, name)
}
fmt.Println()
case "/load":
if len(args) == 0 {
fmt.Println("Usage: /load <name>")
return true
}
name := args[0]
chat, ok := chatMap[name]
if !ok {
fmt.Printf("Chat not found: %s\n", name)
return true
}
history, err := chat.ToHistory()
if err != nil {
fmt.Printf("Failed to load chat: %v\n", err)
return true
}
chatBody.Messages = history
activeChatName = name
cfg.AssistantRole = chat.Agent
fmt.Printf("Loaded chat: %s\n", name)
case "/model", "/m":
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
}
return LocalModels
}
modelList := getModelListForAPI(cfg.CurrentAPI)
if len(args) == 0 {
fmt.Println("Models:")
for i, model := range modelList {
marker := " "
if model == chatBody.Model {
marker = "* "
}
fmt.Printf("%s%d: %s\n", marker, i, model)
}
fmt.Printf("\nCurrent model: %s\n", chatBody.Model)
return true
}
// Try index first, then model name
if idx, err := strconv.Atoi(args[0]); err == nil && idx >= 0 && idx < len(modelList) {
chatBody.Model = modelList[idx]
fmt.Printf("Switched to model: %s\n", chatBody.Model)
return true
}
if slices.Index(modelList, args[0]) < 0 {
fmt.Printf("Model '%s' not found. Use index or choose from:\n", args[0])
for i, model := range modelList {
fmt.Printf(" %d: %s\n", i, model)
}
return true
}
chatBody.Model = args[0]
fmt.Printf("Switched to model: %s\n", args[0])
case "/api", "/a":
if len(args) == 0 {
fmt.Println("API Links:")
for i, link := range cfg.ApiLinks {
marker := " "
if link == cfg.CurrentAPI {
marker = "* "
}
fmt.Printf("%s%d: %s\n", marker, i, link)
}
fmt.Printf("\nCurrent API: %s\n", cfg.CurrentAPI)
return true
}
idx := 0
fmt.Sscanf(args[0], "%d", &idx)
if idx < 0 || idx >= len(cfg.ApiLinks) {
fmt.Printf("Invalid index. Valid range: 0-%d\n", len(cfg.ApiLinks)-1)
return true
}
cfg.CurrentAPI = cfg.ApiLinks[idx]
fmt.Printf("Switched to API: %s\n", cfg.CurrentAPI)
case "/quit", "/q", "/exit":
fmt.Println("Goodbye!")
return false
default:
fmt.Printf("Unknown command: %s\n", msg)
fmt.Println("Type /help for available commands.")
}
return true
}

View File

@@ -1,8 +1,12 @@
package models
import "regexp"
const (
LoadedMark = "(loaded) "
ToolRespMultyType = "multimodel_content"
DefaultFirstMsg = "Hello! What can I do for you?"
BasicSysMsg = "Large Language Model that helps user with any of his requests."
)
type APIType int
@@ -11,3 +15,17 @@ const (
APITypeChat APIType = iota
APITypeCompletion
)
var (
ToolCallRE = regexp.MustCompile(`__tool_call__\s*([\s\S]*?)__tool_call__`)
QuotesRE = regexp.MustCompile(`(".*?")`)
StarRE = regexp.MustCompile(`(\*.*?\*)`)
ThinkRE = regexp.MustCompile(`(?s)<think>.*?</think>`)
CodeBlockRE = regexp.MustCompile(`(?s)\x60{3}(?:.*?)\n(.*?)\n\s*\x60{3}\s*`)
SingleBacktickRE = regexp.MustCompile(`\x60([^\x60]*)\x60`)
RoleRE = regexp.MustCompile(`^(\w+):`)
)
var (
SysLabels = []string{"assistant"}
)

View File

@@ -139,7 +139,7 @@ func showAPILinkSelectionPopup() {
apiListWidget.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
// Update the API in config
cfg.CurrentAPI = mainText
// updateToolCapabilities()
// tools.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 {
@@ -159,7 +159,7 @@ func showAPILinkSelectionPopup() {
if len(newModelList) > 0 && !slices.Contains(newModelList, chatBody.Model) {
chatBody.Model = strings.TrimPrefix(newModelList[0], models.LoadedMark)
cfg.CurrentModel = chatBody.Model
updateToolCapabilities()
UpdateToolCapabilities()
}
pages.RemovePage("apiLinkSelectionPopup")
app.SetFocus(textArea)

View File

@@ -115,9 +115,6 @@ func makePropsTable(props map[string]float32) *tview.Table {
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
})

View File

@@ -47,7 +47,6 @@ func isStopWord(word string) bool {
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], ".,!?;:'\"()[]{}")
@@ -70,7 +69,6 @@ func detectPhrases(query string) []string {
}
}
}
return phrases
}
@@ -122,7 +120,6 @@ func areSlugsAdjacent(slug1, slug2 string) bool {
if prefix1 != prefix2 {
return false
}
batch1, chunk1, ok1 := parseSlugIndices(slug1)
batch2, chunk2, ok2 := parseSlugIndices(slug2)
if !ok1 || !ok2 {
@@ -843,7 +840,6 @@ func (r *RAG) GenerateQueryVariations(query string) []string {
}
}
}
return variations
}

View File

@@ -52,6 +52,7 @@ func (d dummyStore) ChatGetMaxID() (uint32, error) { ret
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 }
func (d dummyStore) Forget(agent, topic string) error { return nil }
// VectorRepo methods (not used but required by interface)
func (d dummyStore) WriteVector(row *models.VectorRow) error { return nil }
@@ -297,7 +298,6 @@ 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 clans 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",

View File

@@ -41,7 +41,6 @@ func TestRealBiblicalQuery(t *testing.T) {
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 {
@@ -95,7 +94,6 @@ func TestRealQueryVariations(t *testing.T) {
t.Fatalf("failed to create RAG instance: %v", err)
}
t.Cleanup(func() { rag.Destroy() })
tests := []struct {
name string
query string

View File

@@ -30,7 +30,6 @@ func TestDetectPhrases(t *testing.T) {
expect: []string{},
},
}
for _, tt := range tests {
got := detectPhrases(tt.query)
if len(got) != len(tt.expect) {
@@ -73,7 +72,6 @@ func TestCountPhraseMatches(t *testing.T) {
expect: 2, // "she bears" and "bald prophet"
},
}
for _, tt := range tests {
got := countPhraseMatches(tt.text, tt.query)
if got != tt.expect {
@@ -119,7 +117,6 @@ func TestAreSlugsAdjacent(t *testing.T) {
expect: true, // sequential batches with same chunk index are adjacent
},
}
for _, tt := range tests {
got := areSlugsAdjacent(tt.slug1, tt.slug2)
if got != tt.expect {
@@ -141,7 +138,6 @@ func TestParseSlugIndices(t *testing.T) {
{"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 {

View File

@@ -1,6 +1,7 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
@@ -161,10 +162,31 @@ func loadOldChatOrGetNew() []models.RoleMsg {
}
func copyToClipboard(text string) error {
cmd := exec.Command("xclip", "-selection", "clipboard")
cmd.Stdin = nil
var cmd *exec.Cmd
if _, err := exec.LookPath("xclip"); err == nil {
cmd = exec.Command("xclip", "-selection", "clipboard")
} else if _, err := exec.LookPath("wl-copy"); err == nil {
cmd = exec.Command("wl-copy")
} else {
return errors.New("no clipboard tool found (install xclip or wl-clipboard)")
}
cmd.Stdin = strings.NewReader(text)
cmd.Stdout = nil
cmd.Stderr = nil
cmd.Stdin = strings.NewReader(text)
return cmd.Run()
}
func readFromClipboard() (string, error) {
var cmd *exec.Cmd
if _, err := exec.LookPath("xclip"); err == nil {
cmd = exec.Command("xclip", "-selection", "clipboard", "-out")
} else if _, err := exec.LookPath("wl-paste"); err == nil {
cmd = exec.Command("wl-paste")
} else {
return "", errors.New("no clipboard tool found (install xclip or wl-clipboard)")
}
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
return out.String(), err
}

View File

@@ -6,6 +6,7 @@ type Memories interface {
Memorise(m *models.Memory) (*models.Memory, error)
Recall(agent, topic string) (string, error)
RecallTopics(agent string) ([]string, error)
Forget(agent, topic string) error
}
func (p ProviderSQL) Memorise(m *models.Memory) (*models.Memory, error) {
@@ -52,3 +53,13 @@ func (p ProviderSQL) RecallTopics(agent string) ([]string, error) {
}
return topics, nil
}
func (p ProviderSQL) Forget(agent, topic string) error {
query := "DELETE FROM memories WHERE agent = $1 AND topic = $2"
_, err := p.db.Exec(query, agent, topic)
if err != nil {
p.logger.Error("failed to delete memory", "query", query, "error", err)
return err
}
return nil
}

View File

@@ -23,9 +23,20 @@ func (p *ProviderSQL) Migrate() error {
p.logger.Error("Failed to read migrations directory;", "error", err)
return fmt.Errorf("failed to read migrations directory: %w", err)
}
// Check if FTS already has data - skip populate migration if so
var ftsCount int
_ = p.db.QueryRow("SELECT COUNT(*) FROM fts_embeddings").Scan(&ftsCount)
skipFTSMigration := ftsCount > 0
// Execute each .up.sql file
for _, file := range files {
if strings.HasSuffix(file.Name(), ".up.sql") {
// Skip FTS populate migration if already populated
if skipFTSMigration && strings.Contains(file.Name(), "004_populate_fts") {
p.logger.Debug("Skipping FTS migration - already populated", "file", file.Name())
continue
}
err := p.executeMigration(migrationsDir, file.Name())
if err != nil {
p.logger.Error("Failed to execute migration %s: %v", file.Name(), err)

View File

@@ -1,26 +1,4 @@
-- 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;
-- Populate FTS table with existing embeddings (incremental - only inserts missing rows)
-- Only use 768 embeddings as that's what we use
INSERT OR IGNORE INTO fts_embeddings (slug, raw_text, filename, embedding_size)
SELECT slug, raw_text, filename, 768 FROM embeddings_768;

View File

@@ -0,0 +1,87 @@
-- Recreate unused embedding tables (for rollback)
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,
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,
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,
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,
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,
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,
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,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_embeddings_384_filename ON embeddings_384(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_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_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);

View File

@@ -0,0 +1,32 @@
-- Drop unused embedding tables (we only use 768)
DROP INDEX IF EXISTS idx_embeddings_384_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_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_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_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;

View File

@@ -48,22 +48,8 @@ func mathBitsToFloat32(b uint32) float32 {
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))
}
@@ -170,62 +156,25 @@ func sqrt(f float32) float32 {
}
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",
query := "SELECT DISTINCT filename FROM embeddings_768"
rows, err := p.db.Query(query)
if err != nil {
return nil, err
}
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
defer rows.Close()
var allFiles []string
for rows.Next() {
var filename string
if err := rows.Scan(&filename); err != nil {
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)
}
}
allFiles = append(allFiles, filename)
}
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
query := "DELETE FROM embeddings_768 WHERE filename = ?"
_, err := p.db.Exec(query, filename)
return err
}

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"gf-lt/tools"
"image"
"os"
"path"
@@ -171,7 +172,7 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
return
case "move sysprompt onto 1st msg":
chatBody.Messages[1].Content = chatBody.Messages[0].Content + chatBody.Messages[1].Content
chatBody.Messages[0].Content = rpDefenitionSysMsg
chatBody.Messages[0].Content = tools.RpDefenitionSysMsg
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
activeChatName = selectedChat
pages.RemovePage(historyPage)

2275
tools.go

File diff suppressed because it is too large Load Diff

416
tools/chain.go Normal file
View File

@@ -0,0 +1,416 @@
package tools
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// Operator represents a chain operator between commands.
type Operator int
const (
OpNone Operator = iota
OpAnd // &&
OpOr // ||
OpSeq // ;
OpPipe // |
)
// Segment is a single command in a chain.
type Segment struct {
Raw string
Op Operator // operator AFTER this segment
}
// ParseChain splits a command string into segments by &&, ;, and |.
// Respects quoted strings (single and double quotes).
func ParseChain(input string) []Segment {
var segments []Segment
var current strings.Builder
runes := []rune(input)
n := len(runes)
for i := 0; i < n; i++ {
ch := runes[i]
// handle quotes
if ch == '\'' || ch == '"' {
quote := ch
current.WriteRune(ch)
i++
for i < n && runes[i] != quote {
current.WriteRune(runes[i])
i++
}
if i < n {
current.WriteRune(runes[i])
}
continue
}
// &&
if ch == '&' && i+1 < n && runes[i+1] == '&' {
segments = append(segments, Segment{
Raw: strings.TrimSpace(current.String()),
Op: OpAnd,
})
current.Reset()
i++ // skip second &
continue
}
// ;
if ch == ';' {
segments = append(segments, Segment{
Raw: strings.TrimSpace(current.String()),
Op: OpSeq,
})
current.Reset()
continue
}
// ||
if ch == '|' && i+1 < n && runes[i+1] == '|' {
segments = append(segments, Segment{
Raw: strings.TrimSpace(current.String()),
Op: OpOr,
})
current.Reset()
i++ // skip second |
continue
}
// | (single pipe)
if ch == '|' {
segments = append(segments, Segment{
Raw: strings.TrimSpace(current.String()),
Op: OpPipe,
})
current.Reset()
continue
}
current.WriteRune(ch)
}
// last segment
last := strings.TrimSpace(current.String())
if last != "" {
segments = append(segments, Segment{Raw: last, Op: OpNone})
}
return segments
}
// ExecChain executes a command string with pipe/chaining support.
// Returns the combined output of all commands.
func ExecChain(command string) string {
segments := ParseChain(command)
if len(segments) == 0 {
return "[error] empty command"
}
var collected []string
var lastOutput string
var lastErr error
pipeInput := ""
for i, seg := range segments {
if i > 0 {
prevOp := segments[i-1].Op
// && semantics: skip if previous failed
if prevOp == OpAnd && lastErr != nil {
continue
}
// || semantics: skip if previous succeeded
if prevOp == OpOr && lastErr == nil {
continue
}
}
// determine stdin for this segment
segStdin := ""
if i == 0 {
segStdin = pipeInput
} else if segments[i-1].Op == OpPipe {
segStdin = lastOutput
}
lastOutput, lastErr = execSingle(seg.Raw, segStdin)
// pipe: output flows to next command's stdin
// && or ;: collect output
if i < len(segments)-1 && seg.Op == OpPipe {
continue
}
if lastOutput != "" {
collected = append(collected, lastOutput)
}
}
return strings.Join(collected, "\n")
}
// execSingle executes a single command (with arguments) and returns output and error.
func execSingle(command, stdin string) (string, error) {
parts := tokenize(command)
if len(parts) == 0 {
return "", errors.New("empty command")
}
name := parts[0]
args := parts[1:]
// Check if it's a built-in Go command
if result, isBuiltin := execBuiltin(name, args, stdin); isBuiltin {
return result, nil
}
// Otherwise execute as system command
cmd := exec.Command(name, args...)
if stdin != "" {
cmd.Stdin = strings.NewReader(stdin)
}
output, err := cmd.CombinedOutput()
if err != nil {
return string(output), err
}
return string(output), nil
}
// tokenize splits a command string by whitespace, respecting quotes.
func tokenize(input string) []string {
var tokens []string
var current strings.Builder
inQuote := false
var quoteChar rune
for _, ch := range input {
if inQuote {
if ch == quoteChar {
inQuote = false
} else {
current.WriteRune(ch)
}
continue
}
if ch == '\'' || ch == '"' {
inQuote = true
quoteChar = ch
continue
}
if ch == ' ' || ch == '\t' {
if current.Len() > 0 {
tokens = append(tokens, current.String())
current.Reset()
}
continue
}
current.WriteRune(ch)
}
if current.Len() > 0 {
tokens = append(tokens, current.String())
}
return tokens
}
// execBuiltin executes a built-in command if it exists.
// Returns (result, true) if it was a built-in (even if result is empty).
// Returns ("", false) if it's not a built-in command.
func execBuiltin(name string, args []string, stdin string) (string, bool) {
switch name {
case "echo":
if stdin != "" {
return stdin, true
}
return strings.Join(args, " "), true
case "time":
return "2006-01-02 15:04:05 MST", true
case "cat":
if len(args) == 0 {
if stdin != "" {
return stdin, true
}
return "", true
}
path := args[0]
abs := path
if !filepath.IsAbs(path) {
abs = filepath.Join(cfg.FilePickerDir, path)
}
data, err := os.ReadFile(abs)
if err != nil {
return fmt.Sprintf("[error] cat: %v", err), true
}
return string(data), true
case "pwd":
return cfg.FilePickerDir, true
case "cd":
if len(args) == 0 {
return "[error] usage: cd <dir>", true
}
dir := args[0]
// Resolve relative to cfg.FilePickerDir
abs := dir
if !filepath.IsAbs(dir) {
abs = filepath.Join(cfg.FilePickerDir, dir)
}
abs = filepath.Clean(abs)
info, err := os.Stat(abs)
if err != nil {
return fmt.Sprintf("[error] cd: %v", err), true
}
if !info.IsDir() {
return "[error] cd: not a directory: " + dir, true
}
cfg.FilePickerDir = abs
return "Changed directory to: " + cfg.FilePickerDir, true
case "mkdir":
if len(args) == 0 {
return "[error] usage: mkdir [-p] <dir>", true
}
createParents := false
var dirPath string
for _, a := range args {
if a == "-p" || a == "--parents" {
createParents = true
} else if dirPath == "" {
dirPath = a
}
}
if dirPath == "" {
return "[error] usage: mkdir [-p] <dir>", true
}
abs := dirPath
if !filepath.IsAbs(dirPath) {
abs = filepath.Join(cfg.FilePickerDir, dirPath)
}
abs = filepath.Clean(abs)
var mkdirFunc func(string, os.FileMode) error
if createParents {
mkdirFunc = os.MkdirAll
} else {
mkdirFunc = os.Mkdir
}
if err := mkdirFunc(abs, 0o755); err != nil {
return fmt.Sprintf("[error] mkdir: %v", err), true
}
if createParents {
return "Created " + dirPath + " (with parents)", true
}
return "Created " + dirPath, true
case "ls":
dir := "."
for _, a := range args {
if !strings.HasPrefix(a, "-") {
dir = a
break
}
}
abs := dir
if !filepath.IsAbs(dir) {
abs = filepath.Join(cfg.FilePickerDir, dir)
}
entries, err := os.ReadDir(abs)
if err != nil {
return fmt.Sprintf("[error] ls: %v", err), true
}
var out strings.Builder
for _, e := range entries {
info, _ := e.Info()
switch {
case e.IsDir():
fmt.Fprintf(&out, "d %-8s %s/\n", "-", e.Name())
case info != nil:
size := info.Size()
sizeStr := strconv.FormatInt(size, 10)
if size > 1024 {
sizeStr = fmt.Sprintf("%.1fKB", float64(size)/1024)
}
fmt.Fprintf(&out, "f %-8s %s\n", sizeStr, e.Name())
default:
fmt.Fprintf(&out, "f %-8s %s\n", "?", e.Name())
}
}
if out.Len() == 0 {
return "(empty directory)", true
}
return strings.TrimRight(out.String(), "\n"), true
case "go":
// Allow all go subcommands
if len(args) == 0 {
return "[error] usage: go <subcommand> [options]", true
}
cmd := exec.Command("go", args...)
cmd.Dir = cfg.FilePickerDir
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("[error] go %s: %v\n%s", args[0], err, string(output)), true
}
return string(output), true
case "cp":
if len(args) < 2 {
return "[error] usage: cp <source> <dest>", true
}
src := args[0]
dst := args[1]
if !filepath.IsAbs(src) {
src = filepath.Join(cfg.FilePickerDir, src)
}
if !filepath.IsAbs(dst) {
dst = filepath.Join(cfg.FilePickerDir, dst)
}
data, err := os.ReadFile(src)
if err != nil {
return fmt.Sprintf("[error] cp: %v", err), true
}
err = os.WriteFile(dst, data, 0644)
if err != nil {
return fmt.Sprintf("[error] cp: %v", err), true
}
return "Copied " + src + " to " + dst, true
case "mv":
if len(args) < 2 {
return "[error] usage: mv <source> <dest>", true
}
src := args[0]
dst := args[1]
if !filepath.IsAbs(src) {
src = filepath.Join(cfg.FilePickerDir, src)
}
if !filepath.IsAbs(dst) {
dst = filepath.Join(cfg.FilePickerDir, dst)
}
err := os.Rename(src, dst)
if err != nil {
return fmt.Sprintf("[error] mv: %v", err), true
}
return "Moved " + src + " to " + dst, true
case "rm":
if len(args) == 0 {
return "[error] usage: rm [-r] <file>", true
}
recursive := false
var target string
for _, a := range args {
if a == "-r" || a == "-rf" || a == "-fr" || a == "-recursive" {
recursive = true
} else if target == "" {
target = a
}
}
if target == "" {
return "[error] usage: rm [-r] <file>", true
}
abs := target
if !filepath.IsAbs(target) {
abs = filepath.Join(cfg.FilePickerDir, target)
}
info, err := os.Stat(abs)
if err != nil {
return fmt.Sprintf("[error] rm: %v", err), true
}
if info.IsDir() {
if recursive {
err = os.RemoveAll(abs)
if err != nil {
return fmt.Sprintf("[error] rm: %v", err), true
}
return "Removed " + abs, true
}
return "[error] rm: is a directory (use -r)", true
}
err = os.Remove(abs)
if err != nil {
return fmt.Sprintf("[error] rm: %v", err), true
}
return "Removed " + abs, true
}
return "", false
}

755
tools/fs.go Normal file
View File

@@ -0,0 +1,755 @@
package tools
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"gf-lt/models"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
var memoryStore MemoryStore
var agentRole string
type MemoryStore interface {
Memorise(agent, topic, data string) (string, error)
Recall(agent, topic string) (string, error)
RecallTopics(agent string) ([]string, error)
Forget(agent, topic string) error
}
func SetMemoryStore(store MemoryStore, role string) {
memoryStore = store
agentRole = role
}
func SetFSRoot(dir string) {
if cfg == nil {
return
}
cfg.FilePickerDir = dir
}
func GetFSRoot() string {
return cfg.FilePickerDir
}
func SetFSCwd(dir string) error {
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
info, err := os.Stat(abs)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}
cfg.FilePickerDir = abs
return nil
}
func resolvePath(rel string) (string, error) {
if cfg.FilePickerDir == "" {
return "", errors.New("fs root not set")
}
if filepath.IsAbs(rel) {
abs := filepath.Clean(rel)
if !strings.HasPrefix(abs, cfg.FilePickerDir+string(os.PathSeparator)) && abs != cfg.FilePickerDir {
return "", fmt.Errorf("path escapes fs root: %s", rel)
}
return abs, nil
}
abs := filepath.Join(cfg.FilePickerDir, rel)
abs = filepath.Clean(abs)
if !strings.HasPrefix(abs, cfg.FilePickerDir+string(os.PathSeparator)) && abs != cfg.FilePickerDir {
return "", fmt.Errorf("path escapes fs root: %s", rel)
}
return abs, nil
}
func humanSize(n int64) string {
switch {
case n >= 1<<20:
return fmt.Sprintf("%.1fMB", float64(n)/float64(1<<20))
case n >= 1<<10:
return fmt.Sprintf("%.1fKB", float64(n)/float64(1<<10))
default:
return fmt.Sprintf("%dB", n)
}
}
func IsImageFile(path string) bool {
ext := strings.ToLower(filepath.Ext(path))
return ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" || ext == ".svg"
}
func FsLs(args []string, stdin string) string {
dir := ""
if len(args) > 0 {
dir = args[0]
}
abs, err := resolvePath(dir)
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
entries, err := os.ReadDir(abs)
if err != nil {
return fmt.Sprintf("[error] ls: %v", err)
}
var out strings.Builder
for _, e := range entries {
info, _ := e.Info()
switch {
case e.IsDir():
fmt.Fprintf(&out, "d %-8s %s/\n", "-", e.Name())
case info != nil:
fmt.Fprintf(&out, "f %-8s %s\n", humanSize(info.Size()), e.Name())
default:
fmt.Fprintf(&out, "f %-8s %s\n", "?", e.Name())
}
}
if out.Len() == 0 {
return "(empty directory)"
}
return strings.TrimRight(out.String(), "\n")
}
func FsCat(args []string, stdin string) string {
b64 := false
var path string
for _, a := range args {
if a == "-b" || a == "--base64" {
b64 = true
} else if path == "" {
path = a
}
}
if path == "" {
return "[error] usage: cat <path>"
}
abs, err := resolvePath(path)
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
data, err := os.ReadFile(abs)
if err != nil {
return fmt.Sprintf("[error] cat: %v", err)
}
if b64 {
result := base64.StdEncoding.EncodeToString(data)
if IsImageFile(path) {
result += fmt.Sprintf("\n![image](file://%s)", abs)
}
return result
}
return string(data)
}
func FsViewImg(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: view_img <image-path>"
}
path := args[0]
var abs string
if filepath.IsAbs(path) {
abs = path
} else {
var err error
abs, err = resolvePath(path)
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
}
if _, err := os.Stat(abs); err != nil {
return fmt.Sprintf("[error] view_img: %v", err)
}
if !IsImageFile(path) {
return fmt.Sprintf("[error] not an image file: %s (use cat to read text files)", path)
}
dataURL, err := models.CreateImageURLFromPath(abs)
if err != nil {
return fmt.Sprintf("[error] view_img: %v", err)
}
result := models.MultimodalToolResp{
Type: "multimodal_content",
Parts: []map[string]string{
{"type": "text", "text": "Image: " + path},
{"type": "image_url", "url": dataURL},
},
}
jsonResult, err := json.Marshal(result)
if err != nil {
return fmt.Sprintf("[error] view_img: %v", err)
}
return string(jsonResult)
}
// FsSee is deprecated, use FsViewImg
func FsSee(args []string, stdin string) string {
return FsViewImg(args, stdin)
}
func FsWrite(args []string, stdin string) string {
b64 := false
var path string
var contentParts []string
for _, a := range args {
switch a {
case "-b", "--base64":
b64 = true
default:
if path == "" {
path = a
} else {
contentParts = append(contentParts, a)
}
}
}
if path == "" {
return "[error] usage: write <path> [content] or pipe stdin"
}
abs, err := resolvePath(path)
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
return fmt.Sprintf("[error] mkdir: %v", err)
}
var data []byte
if b64 {
src := stdin
if src == "" && len(contentParts) > 0 {
src = strings.Join(contentParts, " ")
}
src = strings.TrimSpace(src)
var err error
data, err = base64.StdEncoding.DecodeString(src)
if err != nil {
return fmt.Sprintf("[error] base64 decode: %v", err)
}
} else {
if len(contentParts) > 0 {
data = []byte(strings.Join(contentParts, " "))
} else {
data = []byte(stdin)
}
}
if err := os.WriteFile(abs, data, 0o644); err != nil {
return fmt.Sprintf("[error] write: %v", err)
}
size := humanSize(int64(len(data)))
result := fmt.Sprintf("Written %s → %s", size, path)
if IsImageFile(path) {
result += fmt.Sprintf("\n![image](file://%s)", abs)
}
return result
}
func FsStat(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: stat <path>"
}
abs, err := resolvePath(args[0])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
info, err := os.Stat(abs)
if err != nil {
return fmt.Sprintf("[error] stat: %v", err)
}
mime := "application/octet-stream"
if IsImageFile(args[0]) {
ext := strings.ToLower(filepath.Ext(args[0]))
switch ext {
case ".png":
mime = "image/png"
case ".jpg", ".jpeg":
mime = "image/jpeg"
case ".gif":
mime = "image/gif"
case ".webp":
mime = "image/webp"
case ".svg":
mime = "image/svg+xml"
}
}
var out strings.Builder
fmt.Fprintf(&out, "File: %s\n", args[0])
fmt.Fprintf(&out, "Size: %s (%d bytes)\n", humanSize(info.Size()), info.Size())
fmt.Fprintf(&out, "Type: %s\n", mime)
fmt.Fprintf(&out, "Modified: %s\n", info.ModTime().Format(time.RFC3339))
if info.IsDir() {
fmt.Fprintf(&out, "Kind: directory\n")
}
return strings.TrimRight(out.String(), "\n")
}
func FsRm(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: rm <path>"
}
abs, err := resolvePath(args[0])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
if err := os.RemoveAll(abs); err != nil {
return fmt.Sprintf("[error] rm: %v", err)
}
return "Removed " + args[0]
}
func FsCp(args []string, stdin string) string {
if len(args) < 2 {
return "[error] usage: cp <src> <dst>"
}
srcAbs, err := resolvePath(args[0])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
dstAbs, err := resolvePath(args[1])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
data, err := os.ReadFile(srcAbs)
if err != nil {
return fmt.Sprintf("[error] cp read: %v", err)
}
if err := os.MkdirAll(filepath.Dir(dstAbs), 0o755); err != nil {
return fmt.Sprintf("[error] cp mkdir: %v", err)
}
if err := os.WriteFile(dstAbs, data, 0o644); err != nil {
return fmt.Sprintf("[error] cp write: %v", err)
}
return fmt.Sprintf("Copied %s → %s (%s)", args[0], args[1], humanSize(int64(len(data))))
}
func FsMv(args []string, stdin string) string {
if len(args) < 2 {
return "[error] usage: mv <src> <dst>"
}
srcAbs, err := resolvePath(args[0])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
dstAbs, err := resolvePath(args[1])
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
if err := os.MkdirAll(filepath.Dir(dstAbs), 0o755); err != nil {
return fmt.Sprintf("[error] mv mkdir: %v", err)
}
if err := os.Rename(srcAbs, dstAbs); err != nil {
return fmt.Sprintf("[error] mv: %v", err)
}
return fmt.Sprintf("Moved %s → %s", args[0], args[1])
}
func FsMkdir(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: mkdir [-p] <dir>"
}
createParents := false
var dirPath string
for _, a := range args {
if a == "-p" || a == "--parents" {
createParents = true
} else if dirPath == "" {
dirPath = a
}
}
if dirPath == "" {
return "[error] usage: mkdir [-p] <dir>"
}
abs, err := resolvePath(dirPath)
if err != nil {
return fmt.Sprintf("[error] %v", err)
}
var mkdirFunc func(string, os.FileMode) error
if createParents {
mkdirFunc = os.MkdirAll
} else {
mkdirFunc = os.Mkdir
}
if err := mkdirFunc(abs, 0o755); err != nil {
return fmt.Sprintf("[error] mkdir: %v", err)
}
if createParents {
return "Created " + dirPath + " (with parents)"
}
return "Created " + dirPath
}
// Text processing commands
func FsEcho(args []string, stdin string) string {
if stdin != "" {
return stdin
}
return strings.Join(args, " ")
}
func FsTime(args []string, stdin string) string {
return time.Now().Format("2006-01-02 15:04:05 MST")
}
func FsGrep(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: grep [-i] [-v] [-c] <pattern>"
}
ignoreCase := false
invert := false
countOnly := false
var pattern string
for _, a := range args {
switch a {
case "-i":
ignoreCase = true
case "-v":
invert = true
case "-c":
countOnly = true
default:
pattern = a
}
}
if pattern == "" {
return "[error] pattern required"
}
if ignoreCase {
pattern = strings.ToLower(pattern)
}
lines := strings.Split(stdin, "\n")
var matched []string
for _, line := range lines {
haystack := line
if ignoreCase {
haystack = strings.ToLower(line)
}
match := strings.Contains(haystack, pattern)
if invert {
match = !match
}
if match {
matched = append(matched, line)
}
}
if countOnly {
return strconv.Itoa(len(matched))
}
return strings.Join(matched, "\n")
}
func FsHead(args []string, stdin string) string {
n := 10
for i, a := range args {
if a == "-n" && i+1 < len(args) {
if parsed, err := strconv.Atoi(args[i+1]); err == nil {
n = parsed
}
} else if strings.HasPrefix(a, "-") {
continue
} else if parsed, err := strconv.Atoi(a); err == nil {
n = parsed
}
}
lines := strings.Split(stdin, "\n")
if n > 0 && len(lines) > n {
lines = lines[:n]
}
return strings.Join(lines, "\n")
}
func FsTail(args []string, stdin string) string {
n := 10
for i, a := range args {
if a == "-n" && i+1 < len(args) {
if parsed, err := strconv.Atoi(args[i+1]); err == nil {
n = parsed
}
} else if strings.HasPrefix(a, "-") {
continue
} else if parsed, err := strconv.Atoi(a); err == nil {
n = parsed
}
}
lines := strings.Split(stdin, "\n")
if n > 0 && len(lines) > n {
lines = lines[len(lines)-n:]
}
return strings.Join(lines, "\n")
}
func FsWc(args []string, stdin string) string {
lines := len(strings.Split(stdin, "\n"))
words := len(strings.Fields(stdin))
chars := len(stdin)
if len(args) > 0 {
switch args[0] {
case "-l":
return strconv.Itoa(lines)
case "-w":
return strconv.Itoa(words)
case "-c":
return strconv.Itoa(chars)
}
}
return fmt.Sprintf("%d lines, %d words, %d chars", lines, words, chars)
}
func FsSort(args []string, stdin string) string {
lines := strings.Split(stdin, "\n")
reverse := false
numeric := false
for _, a := range args {
switch a {
case "-r":
reverse = true
case "-n":
numeric = true
}
}
sortFunc := func(i, j int) bool {
if numeric {
ni, _ := strconv.Atoi(lines[i])
nj, _ := strconv.Atoi(lines[j])
if reverse {
return ni > nj
}
return ni < nj
}
if reverse {
return lines[i] > lines[j]
}
return lines[i] < lines[j]
}
sort.Slice(lines, sortFunc)
return strings.Join(lines, "\n")
}
func FsUniq(args []string, stdin string) string {
lines := strings.Split(stdin, "\n")
showCount := false
for _, a := range args {
if a == "-c" {
showCount = true
}
}
var result []string
var prev string
first := true
count := 0
for _, line := range lines {
if first || line != prev {
if !first && showCount {
result = append(result, fmt.Sprintf("%d %s", count, prev))
} else if !first {
result = append(result, prev)
}
count = 1
prev = line
first = false
} else {
count++
}
}
if !first {
if showCount {
result = append(result, fmt.Sprintf("%d %s", count, prev))
} else {
result = append(result, prev)
}
}
return strings.Join(result, "\n")
}
var allowedGitSubcommands = map[string]bool{
"status": true,
"log": true,
"diff": true,
"show": true,
"branch": true,
"reflog": true,
"rev-parse": true,
"shortlog": true,
"describe": true,
"rev-list": true,
}
func FsGit(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: git <subcommand> [options]"
}
subcmd := args[0]
if !allowedGitSubcommands[subcmd] {
return fmt.Sprintf("[error] git: '%s' is not an allowed git command. Allowed: status, log, diff, show, branch, reflog, rev-parse, shortlog, describe, rev-list", subcmd)
}
abs, err := resolvePath(".")
if err != nil {
return fmt.Sprintf("[error] git: %v", err)
}
// Pass all args to git (first arg is subcommand, rest are options)
cmd := exec.Command("git", args...)
cmd.Dir = abs
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("[error] git %s: %v\n%s", subcmd, err, string(output))
}
return string(output)
}
func FsPwd(args []string, stdin string) string {
return cfg.FilePickerDir
}
func FsCd(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: cd <dir>"
}
dir := args[0]
abs, err := resolvePath(dir)
if err != nil {
return fmt.Sprintf("[error] cd: %v", err)
}
info, err := os.Stat(abs)
if err != nil {
return fmt.Sprintf("[error] cd: %v", err)
}
if !info.IsDir() {
return "[error] cd: not a directory: " + dir
}
cfg.FilePickerDir = abs
return "Changed directory to: " + cfg.FilePickerDir
}
func FsSed(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: sed 's/old/new/[g]' [file]"
}
inPlace := false
var filePath string
var pattern string
for _, a := range args {
switch a {
case "-i", "--in-place":
inPlace = true
default:
if strings.HasPrefix(a, "s") && len(a) > 1 {
pattern = a
} else if filePath == "" && !strings.HasPrefix(a, "-") {
filePath = a
}
}
}
if pattern == "" {
return "[error] usage: sed 's/old/new/[g]' [file]"
}
// Parse pattern: s/old/new/flags
parts := strings.Split(pattern[1:], "/")
if len(parts) < 2 {
return "[error] invalid sed pattern. Use: s/old/new/[g]"
}
oldStr := parts[0]
newStr := parts[1]
global := len(parts) >= 3 && strings.Contains(parts[2], "g")
var content string
switch {
case filePath != "" && stdin == "":
abs, err := resolvePath(filePath)
if err != nil {
return fmt.Sprintf("[error] sed: %v", err)
}
data, err := os.ReadFile(abs)
if err != nil {
return fmt.Sprintf("[error] sed: %v", err)
}
content = string(data)
case stdin != "":
content = stdin
default:
return "[error] sed: no input (use file path or pipe from stdin)"
}
// Apply sed replacement
if global {
content = strings.ReplaceAll(content, oldStr, newStr)
} else {
content = strings.Replace(content, oldStr, newStr, 1)
}
if inPlace && filePath != "" {
abs, err := resolvePath(filePath)
if err != nil {
return fmt.Sprintf("[error] sed: %v", err)
}
if err := os.WriteFile(abs, []byte(content), 0644); err != nil {
return fmt.Sprintf("[error] sed: %v", err)
}
return "Modified " + filePath
}
return content
}
func FsMemory(args []string, stdin string) string {
if len(args) == 0 {
return "[error] usage: memory store <topic> <data> | memory get <topic> | memory list | memory forget <topic>"
}
if memoryStore == nil {
return "[error] memory store not initialized"
}
switch args[0] {
case "store":
if len(args) < 3 && stdin == "" {
return "[error] usage: memory store <topic> <data>"
}
topic := args[1]
var data string
if len(args) >= 3 {
data = strings.Join(args[2:], " ")
} else {
data = stdin
}
_, err := memoryStore.Memorise(agentRole, topic, data)
if err != nil {
return fmt.Sprintf("[error] failed to store: %v", err)
}
return "Stored under topic: " + topic
case "get":
if len(args) < 2 {
return "[error] usage: memory get <topic>"
}
topic := args[1]
data, err := memoryStore.Recall(agentRole, topic)
if err != nil {
return fmt.Sprintf("[error] failed to recall: %v", err)
}
return fmt.Sprintf("Topic: %s\n%s", topic, data)
case "list", "topics":
topics, err := memoryStore.RecallTopics(agentRole)
if err != nil {
return fmt.Sprintf("[error] failed to list topics: %v", err)
}
if len(topics) == 0 {
return "No topics stored."
}
return "Topics: " + strings.Join(topics, ", ")
case "forget", "delete":
if len(args) < 2 {
return "[error] usage: memory forget <topic>"
}
topic := args[1]
err := memoryStore.Forget(agentRole, topic)
if err != nil {
return fmt.Sprintf("[error] failed to forget: %v", err)
}
return "Deleted topic: " + topic
default:
return fmt.Sprintf("[error] unknown subcommand: %s. Use: store, get, list, topics, forget, delete", args[0])
}
}

View File

@@ -1,4 +1,4 @@
package main
package tools
import (
"encoding/json"
@@ -12,87 +12,6 @@ import (
"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
@@ -101,7 +20,7 @@ var (
page playwright.Page
)
func pwShutDown() error {
func PwShutDown() error {
if pw == nil {
return nil
}
@@ -109,7 +28,7 @@ func pwShutDown() error {
return pw.Stop()
}
func installPW() error {
func InstallPW() error {
err := playwright.Install(&playwright.RunOptions{Verbose: false})
if err != nil {
logger.Warn("playwright not available", "error", err)
@@ -118,7 +37,7 @@ func installPW() error {
return nil
}
func checkPlaywright() error {
func CheckPlaywright() error {
var err error
pw, err = playwright.Run()
if err != nil {
@@ -455,6 +374,78 @@ func pwDrag(args map[string]string) []byte {
return []byte(fmt.Sprintf(`{"success": true, "message": "Dragged from (%s,%s) to (%s,%s)"}`, x1, y1, x2, y2))
}
func pwDragBySelector(args map[string]string) []byte {
fromSelector, ok := args["fromSelector"]
if !ok || fromSelector == "" {
return []byte(`{"error": "fromSelector not provided"}`)
}
toSelector, ok := args["toSelector"]
if !ok || toSelector == "" {
return []byte(`{"error": "toSelector not provided"}`)
}
if !browserStarted || page == nil {
return []byte(`{"error": "Browser not started. Call pw_start first."}`)
}
fromJS := fmt.Sprintf(`
function getCenter(selector) {
const el = document.querySelector(selector);
if (!el) return null;
const rect = el.getBoundingClientRect();
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
}
getCenter(%q)
`, fromSelector)
toJS := fmt.Sprintf(`
function getCenter(selector) {
const el = document.querySelector(selector);
if (!el) return null;
const rect = el.getBoundingClientRect();
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
}
getCenter(%q)
`, toSelector)
fromResult, err := page.Evaluate(fromJS)
if err != nil {
return []byte(fmt.Sprintf(`{"error": "failed to get from element: %s"}`, err.Error()))
}
fromMap, ok := fromResult.(map[string]interface{})
if !ok || fromMap == nil {
return []byte(fmt.Sprintf(`{"error": "from selector '%s' not found"}`, fromSelector))
}
fromX := fromMap["x"].(float64)
fromY := fromMap["y"].(float64)
toResult, err := page.Evaluate(toJS)
if err != nil {
return []byte(fmt.Sprintf(`{"error": "failed to get to element: %s"}`, err.Error()))
}
toMap, ok := toResult.(map[string]interface{})
if !ok || toMap == nil {
return []byte(fmt.Sprintf(`{"error": "to selector '%s' not found"}`, toSelector))
}
toX := toMap["x"].(float64)
toY := toMap["y"].(float64)
mouse := page.Mouse()
err = mouse.Move(fromX, fromY)
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(toX, toY)
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()))
}
msg := fmt.Sprintf("Dragged from %s (%.0f,%.0f) to %s (%.0f,%.0f)", fromSelector, fromX, fromY, toSelector, toX, toY)
return []byte(fmt.Sprintf(`{"success": true, "message": "%s"}`, msg))
}
// nolint:unused
func pwClickAt(args map[string]string) []byte {
x, ok := args["x"]
if !ok {
@@ -605,6 +596,7 @@ func pwGetDOM(args map[string]string) []byte {
return []byte(fmt.Sprintf(`{"dom": %s}`, string(data)))
}
// nolint:unused
func pwSearchElements(args map[string]string) []byte {
text := args["text"]
selector := args["selector"]

1909
tools/tools.go Normal file

File diff suppressed because it is too large Load Diff

33
tui.go
View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"gf-lt/models"
"gf-lt/tools"
"image"
_ "image/jpeg"
_ "image/png"
@@ -68,8 +69,8 @@ var (
[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]F7[white]: copy last msg to clipboard (linux xclip or wl-copy)
[yellow]F8[white]: copy n msg to clipboard (linux xclip or wl-copy)
[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
@@ -104,6 +105,7 @@ var (
[yellow]Alt+t[white]: toggle thinking blocks visibility (collapse/expand <think> blocks)
[yellow]Ctrl+t[white]: toggle tool call/response visibility (collapse/expand tool calls and non-shell tool responses)
[yellow]Alt+i[white]: show colorscheme selection popup
[yellow]Insert[white]: paste from clipboard to the text area (use it instead shift+insert)
=== scrolling chat window (some keys similar to vim) ===
[yellow]arrows up/down and j/k[white]: scroll up and down
@@ -318,6 +320,24 @@ func initTUI() {
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")
textArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyInsert {
text, err := readFromClipboard()
if err != nil {
logger.Error("failed to read clipboard", "error", err)
return event
}
maxPaste := 100000
if len(text) > maxPaste {
text = text[:maxPaste]
showToast("paste truncated", "pasted text exceeded 100KB limit")
}
current := textArea.GetText()
textArea.SetText(current+text, true)
return nil
}
return event
})
textView = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
@@ -328,6 +348,7 @@ func initTUI() {
// calling it explicitly makes text streaming to look more smooth
app.Draw()
})
outputHandler = &TUIOutputHandler{tv: textView}
notificationWidget = tview.NewTextView().
SetTextAlign(tview.AlignCenter).
SetDynamicColors(true).
@@ -830,7 +851,7 @@ func initTUI() {
if event.Key() == tcell.KeyF9 {
// table of codeblocks to copy
text := textView.GetText(false)
cb := codeBlockRE.FindAllString(text, -1)
cb := models.CodeBlockRE.FindAllString(text, -1)
if len(cb) == 0 {
showToast("notify", "no code blocks in chat")
return nil
@@ -929,7 +950,7 @@ func initTUI() {
if event.Key() == tcell.KeyCtrlK {
// add message from tools
cfg.ToolUse = !cfg.ToolUse
updateToolCapabilities()
UpdateToolCapabilities()
updateStatusLine()
return nil
}
@@ -1035,7 +1056,7 @@ func initTUI() {
if event.Key() == tcell.KeyCtrlC {
logger.Info("caught Ctrl+C via tcell event")
go func() {
if err := pwShutDown(); err != nil {
if err := tools.PwShutDown(); err != nil {
logger.Error("shutdown failed", "err", err)
}
app.Stop()
@@ -1127,7 +1148,7 @@ func initTUI() {
}
// check if plain text
if !injectRole {
matches := roleRE.FindStringSubmatch(msgText)
matches := models.RoleRE.FindStringSubmatch(msgText)
if len(matches) > 1 {
persona = matches[1]
msgText = strings.TrimLeft(msgText[len(matches[0]):], " ")