Compare commits
63 Commits
enha/rag-e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4428c06356 | ||
|
|
78918b2949 | ||
|
|
39e099cbe9 | ||
|
|
aea19a6c0d | ||
|
|
6befe7f4bf | ||
|
|
3e51ed2ceb | ||
|
|
ec3ccaae90 | ||
|
|
1504214941 | ||
|
|
50035e667b | ||
|
|
76cc48eb54 | ||
|
|
43d78ff47b | ||
|
|
556eab9d89 | ||
|
|
cdc237c81c | ||
|
|
85c0a0ec62 | ||
|
|
0bc6a09786 | ||
|
|
ef67cbb456 | ||
|
|
267feb0722 | ||
|
|
5413c97b23 | ||
|
|
aff7d73d16 | ||
|
|
9488c5773e | ||
|
|
77506950e4 | ||
|
|
9ff4a465d9 | ||
|
|
11fe89c243 | ||
|
|
4cf8833423 | ||
|
|
7a6d2b8777 | ||
|
|
69a69547ff | ||
|
|
3e4213b5c3 | ||
|
|
451e6f0381 | ||
|
|
326a1a4d09 | ||
|
|
0b7f621a75 | ||
|
|
46dbb7b981 | ||
|
|
ef0940daa8 | ||
|
|
3d44686a51 | ||
|
|
df04d8c21c | ||
|
|
7c8697f48e | ||
|
|
7f8bbefb05 | ||
|
|
47b3d37a97 | ||
|
|
2c9c36e2c6 | ||
|
|
e476575334 | ||
|
|
7e346b5e19 | ||
|
|
1396b3eb05 | ||
|
|
619b19cb46 | ||
|
|
26377702d3 | ||
|
|
fdcaa6c5e2 | ||
|
|
77c365959d | ||
|
|
f4fcb85570 | ||
|
|
92acfb7ed4 | ||
|
|
2901208c80 | ||
|
|
4cfe2fe37f | ||
|
|
648035b194 | ||
|
|
13773bcc97 | ||
|
|
d9b820c9c4 | ||
|
|
adc4dea644 | ||
|
|
6456cb0922 | ||
|
|
e77dbb3160 | ||
|
|
3013f21a75 | ||
|
|
bb57be95c6 | ||
|
|
528d4210fc | ||
|
|
54b4cccf92 | ||
|
|
c2c90f6d2b | ||
|
|
94769225cf | ||
|
|
0e42a6f069 | ||
|
|
2687f38d00 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,4 +1,3 @@
|
||||
*.txt
|
||||
*.json
|
||||
testlog
|
||||
history/
|
||||
@@ -18,3 +17,7 @@ chat_exports/*.json
|
||||
ragimport
|
||||
.env
|
||||
onnx/
|
||||
*.log
|
||||
log.txt
|
||||
dumps/
|
||||
batteries/whisper.cpp
|
||||
|
||||
5
Makefile
5
Makefile
@@ -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!"
|
||||
|
||||
@@ -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
|
||||
[](https://youtu.be/WCS4Xc902F8 "gf-lt showcase")
|
||||
|
||||
#### how it looks
|
||||

|
||||
|
||||
@@ -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;
|
||||

|
||||
|
||||
#### setting up config
|
||||
|
||||
@@ -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
119
agent/pw_agent.go
Normal 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
338
agent/pw_tools.go
Normal 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)
|
||||
}
|
||||
123
agent/request.go
123
agent/request.go
@@ -32,10 +32,14 @@ func detectAPI(api string) (isCompletion, isChat, isDeepSeek, isOpenRouter bool)
|
||||
type AgentClient struct {
|
||||
cfg *config.Config
|
||||
getToken func() string
|
||||
log slog.Logger
|
||||
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)
|
||||
req := models.NewDSChatReq(*ag.chatBody)
|
||||
return json.Marshal(req)
|
||||
case isOpenRouter:
|
||||
// OpenRouter chat - agents don't use reasoning by default
|
||||
req := models.NewOpenRouterChatReq(*chatBody, defaultProps, "")
|
||||
req := models.NewOpenRouterChatReq(*ag.chatBody, defaultProps, ag.cfg.ReasoningEffort)
|
||||
return json.Marshal(req)
|
||||
default:
|
||||
// Assume llama.cpp chat (OpenAI format)
|
||||
req := models.OpenAIReq{
|
||||
ChatBody: chatBody,
|
||||
Tools: nil,
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
BIN
assets/yt_thumb.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
Submodule batteries/whisper.cpp deleted from a88b93f85f
318
bot.go
318
bot.go
@@ -11,6 +11,7 @@ import (
|
||||
"gf-lt/models"
|
||||
"gf-lt/rag"
|
||||
"gf-lt/storage"
|
||||
"gf-lt/tools"
|
||||
"html"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -24,6 +25,8 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -39,14 +42,66 @@ var (
|
||||
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
|
||||
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
|
||||
@@ -69,8 +124,6 @@ var (
|
||||
orModelsData *models.ORModels
|
||||
)
|
||||
|
||||
var thinkBlockRE = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
||||
|
||||
// parseKnownToTag extracts known_to list from content using configured tag.
|
||||
// Returns cleaned content and list of character names.
|
||||
func parseKnownToTag(content string) []string {
|
||||
@@ -458,6 +511,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() {
|
||||
@@ -555,12 +631,57 @@ func finalizeRespStats(tokenCount int, startTime time.Time) {
|
||||
}
|
||||
}
|
||||
|
||||
func dumpRequestToFile(api string, body []byte, token string, statusCode int, respError string) {
|
||||
dumpDir := "dumps"
|
||||
if err := os.MkdirAll(dumpDir, 0755); err != nil {
|
||||
logger.Warn("failed to create dumps directory", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
bodyFilename := fmt.Sprintf("%s/request_%s_%d_body.json", dumpDir, timestamp, statusCode)
|
||||
curlFilename := fmt.Sprintf("%s/request_%s_%d.curl", dumpDir, timestamp, statusCode)
|
||||
|
||||
if err := os.WriteFile(bodyFilename, body, 0644); err != nil {
|
||||
logger.Warn("failed to write request body dump", "error", err, "filename", bodyFilename)
|
||||
return
|
||||
}
|
||||
|
||||
var authPart string
|
||||
if token != "" {
|
||||
authPart = fmt.Sprintf(`-H "Authorization: Bearer %s"`, token)
|
||||
}
|
||||
|
||||
curlCmd := fmt.Sprintf(`curl -X POST "%s" \
|
||||
-H "Content-Type: application/json" \
|
||||
%s \
|
||||
--data-binary @%s`,
|
||||
api, authPart, bodyFilename)
|
||||
|
||||
if err := os.WriteFile(curlFilename, []byte(curlCmd), 0644); err != nil {
|
||||
logger.Warn("failed to write request dump", "error", err, "filename", curlFilename)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info("request dump saved", "curl_file", curlFilename, "body_file", bodyFilename, "status", statusCode)
|
||||
}
|
||||
|
||||
// sendMsgToLLM expects streaming resp
|
||||
func sendMsgToLLM(body io.Reader) {
|
||||
choseChunkParser()
|
||||
// openrouter does not respect stop strings, so we have to cut the message ourselves
|
||||
stopStrings := chatBody.MakeStopSliceExcluding("", listChatRoles())
|
||||
req, err := http.NewRequest("POST", cfg.CurrentAPI, body)
|
||||
|
||||
// Read body content for potential dump on error
|
||||
bodyBytes, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
logger.Error("failed to read request body", "error", err)
|
||||
showToast("error", "apicall failed:"+err.Error())
|
||||
streamDone <- true
|
||||
return
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes))
|
||||
if err != nil {
|
||||
logger.Error("newreq error", "error", err)
|
||||
showToast("error", "apicall failed:"+err.Error())
|
||||
@@ -582,7 +703,7 @@ func sendMsgToLLM(body io.Reader) {
|
||||
// Check if the initial response is an error before starting to stream
|
||||
if resp.StatusCode >= 400 {
|
||||
// Read the response body to get detailed error information
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
respBodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Error("failed to read error response body", "error", err, "status_code", resp.StatusCode)
|
||||
detailedError := fmt.Sprintf("HTTP Status: %d, Failed to read response body: %v", resp.StatusCode, err)
|
||||
@@ -592,8 +713,9 @@ func sendMsgToLLM(body io.Reader) {
|
||||
return
|
||||
}
|
||||
// Parse the error response for detailed information
|
||||
detailedError := extractDetailedErrorFromBytes(bodyBytes, resp.StatusCode)
|
||||
detailedError := extractDetailedErrorFromBytes(respBodyBytes, resp.StatusCode)
|
||||
logger.Error("API returned error status", "status_code", resp.StatusCode, "detailed_error", detailedError)
|
||||
dumpRequestToFile(cfg.CurrentAPI, bodyBytes, chunkParser.GetToken(), resp.StatusCode, detailedError)
|
||||
showToast("API Error", detailedError)
|
||||
resp.Body.Close()
|
||||
streamDone <- true
|
||||
@@ -764,6 +886,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 +916,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 +954,22 @@ func chatRound(r *models.ChatRoundReq) error {
|
||||
Role: botPersona, Content: "",
|
||||
})
|
||||
nl := "\n\n"
|
||||
prevText := textView.GetText(true)
|
||||
prevText := cliPrevOutput
|
||||
if cfg.CLIMode {
|
||||
if strings.HasSuffix(prevText, nl) {
|
||||
nl = ""
|
||||
} else if strings.HasSuffix(prevText, "\n") {
|
||||
nl = "\n"
|
||||
}
|
||||
fmt.Fprintf(textView, "%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
|
||||
} else {
|
||||
prevText = textView.GetText(true)
|
||||
if strings.HasSuffix(prevText, nl) {
|
||||
nl = ""
|
||||
} else if strings.HasSuffix(prevText, "\n") {
|
||||
nl = "\n"
|
||||
}
|
||||
}
|
||||
outputHandler.Writef("%s[-:-:b](%d) %s[-:-:-]\n", nl, msgIdx, roleToIcon(botPersona))
|
||||
} else {
|
||||
msgIdx = len(chatBody.Messages) - 1
|
||||
}
|
||||
@@ -850,9 +991,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 +1008,7 @@ out:
|
||||
respText.WriteString(chunk)
|
||||
justExitedThinkingCollapsed = true
|
||||
if cfg.AutoScrollEnabled {
|
||||
textView.ScrollToEnd()
|
||||
outputHandler.ScrollToEnd()
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -884,32 +1025,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,20 +1092,27 @@ 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)
|
||||
}
|
||||
// Strip think blocks before parsing for tool calls
|
||||
respTextNoThink := thinkBlockRE.ReplaceAllString(respText.String(), "")
|
||||
respTextNoThink := models.ThinkRE.ReplaceAllString(respText.String(), "")
|
||||
if interruptResp.Load() {
|
||||
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 +1250,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 +1318,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 +1365,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 +1419,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 +1442,23 @@ 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")
|
||||
argsPreview := messages[i].ToolCall.Args
|
||||
if len(messages[i].ToolCall.Args) > 30 {
|
||||
argsPreview = messages[i].ToolCall.Args[:30]
|
||||
}
|
||||
resp[i] = strings.ReplaceAll(
|
||||
fmt.Sprintf(
|
||||
"%s\n%s\n[yellow::i][tool call: %s %s (press Ctrl+T to expand)][-:-:-]\n",
|
||||
icon, messages[i].GetText(), toolName, argsPreview),
|
||||
"\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 +1492,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 +1508,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,8 +1591,10 @@ func updateModelLists() {
|
||||
chatBody.Model = m
|
||||
cachedModelColor.Store("green")
|
||||
updateStatusLine()
|
||||
updateToolCapabilities()
|
||||
app.Draw()
|
||||
UpdateToolCapabilities()
|
||||
if !cfg.CLIMode {
|
||||
app.Draw() // raw?
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -1434,14 +1618,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 +1669,10 @@ func summarizeAndStartNewChat() {
|
||||
}
|
||||
chatBody.Messages = append(chatBody.Messages, toolMsg)
|
||||
// Update UI
|
||||
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 +1691,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 +1703,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 +1730,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 +1750,24 @@ 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 {
|
||||
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 := checkPlaywright(); err != nil {
|
||||
if err := tools.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()
|
||||
}
|
||||
|
||||
74
cli-tests/sort-img/check.sh
Executable file
74
cli-tests/sort-img/check.sh
Executable 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
|
||||
27
cli-tests/sort-img/run.sh
Executable file
27
cli-tests/sort-img/run.sh
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/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")
|
||||
LMODEL=${LMODEL:-Qwen3.5-9B-Q6_K}
|
||||
cd ../../
|
||||
go run . -cli -msg "$TASK" -model "$LMODEL"
|
||||
|
||||
echo ""
|
||||
echo "=== Done ==="
|
||||
cp "$LOG_FILE" "$SCRIPT_DIR/latest_run.log"
|
||||
echo "Log file: $LOG_FILE"
|
||||
11
cli-tests/sort-img/setup.sh
Executable file
11
cli-tests/sort-img/setup.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
|
||||
mkdir -p /tmp/sort-img
|
||||
|
||||
cp "$SCRIPT_DIR/../../assets/ex01.png" /tmp/sort-img/file1.png
|
||||
cp "$SCRIPT_DIR/../../assets/helppage.png" /tmp/sort-img/file2.png
|
||||
cp "$SCRIPT_DIR/../../assets/yt_thumb.jpg" /tmp/sort-img/file3.jpg
|
||||
2
cli-tests/sort-img/task.txt
Normal file
2
cli-tests/sort-img/task.txt
Normal 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
4
cli-tests/sort-img/teardown.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
rm -rf /tmp/sort-img
|
||||
91
cli-tests/sort-text/check.sh
Executable file
91
cli-tests/sort-text/check.sh
Executable 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
|
||||
28
cli-tests/sort-text/run.sh
Executable file
28
cli-tests/sort-text/run.sh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/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")
|
||||
# LMODEL=${LMODEL:-gemma-4-31B-it-Q4_K_M}
|
||||
LMODEL=${LMODEL:-Qwen3.5-9B-Q6_K}
|
||||
cd ../../
|
||||
go run . -cli -msg "$TASK" -model "$LMODEL"
|
||||
|
||||
echo ""
|
||||
echo "=== Done ==="
|
||||
cp "$LOG_FILE" "$SCRIPT_DIR/latest_run.log"
|
||||
echo "Log file: $LOG_FILE"
|
||||
10
cli-tests/sort-text/setup.sh
Executable file
10
cli-tests/sort-text/setup.sh
Executable 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
|
||||
2
cli-tests/sort-text/task.txt
Normal file
2
cli-tests/sort-text/task.txt
Normal 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
|
||||
4
cli-tests/sort-text/teardown.sh
Executable file
4
cli-tests/sort-text/teardown.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
rm -rf /tmp/sort-text
|
||||
@@ -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)
|
||||
|
||||
@@ -30,6 +30,7 @@ type Config struct {
|
||||
DBPATH string `toml:"DBPATH"`
|
||||
FilePickerDir string `toml:"FilePickerDir"`
|
||||
FilePickerExts string `toml:"FilePickerExts"`
|
||||
FSAllowOutOfRoot bool `toml:"FSAllowOutOfRoot"`
|
||||
ImagePreview bool `toml:"ImagePreview"`
|
||||
EnableMouse bool `toml:"EnableMouse"`
|
||||
// embeddings
|
||||
@@ -75,6 +76,9 @@ type Config struct {
|
||||
// playwright browser
|
||||
PlaywrightEnabled bool `toml:"PlaywrightEnabled"`
|
||||
PlaywrightDebug bool `toml:"PlaywrightDebug"` // !headless
|
||||
// CLI mode
|
||||
CLIMode bool
|
||||
UseNotifySend bool
|
||||
}
|
||||
|
||||
func LoadConfig(fn string) (*Config, error) {
|
||||
@@ -86,6 +90,15 @@ func LoadConfig(fn string) (*Config, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Default FilePickerDir to current working directory if not set
|
||||
if config.FilePickerDir == "" {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
config.FilePickerDir = "."
|
||||
} else {
|
||||
config.FilePickerDir = cwd
|
||||
}
|
||||
}
|
||||
config.CurrentAPI = config.ChatAPI
|
||||
config.APIMap = map[string]string{
|
||||
config.ChatAPI: config.CompletionAPI,
|
||||
|
||||
2
go.mod
2
go.mod
@@ -5,7 +5,7 @@ go 1.25.1
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.5.0
|
||||
github.com/GrailFinder/google-translate-tts v0.1.4
|
||||
github.com/GrailFinder/searchagent v0.2.0
|
||||
github.com/GrailFinder/searchagent v0.2.1
|
||||
github.com/PuerkitoBio/goquery v1.11.0
|
||||
github.com/gdamore/tcell/v2 v2.13.2
|
||||
github.com/glebarez/go-sqlite v1.22.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -4,8 +4,8 @@ github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/GrailFinder/google-translate-tts v0.1.4 h1:NJoPZUGfBrmouQMN19MUcNPNUx4tmf4a8OZRME4E4Mg=
|
||||
github.com/GrailFinder/google-translate-tts v0.1.4/go.mod h1:YIOLKR7sObazdUCrSex3u9OVBovU55eYgWa25vsQJ18=
|
||||
github.com/GrailFinder/searchagent v0.2.0 h1:U2GVjLh/9xZt0xX9OcYk9Q2fMkyzyTiADPUmUisRdtQ=
|
||||
github.com/GrailFinder/searchagent v0.2.0/go.mod h1:d66tn5+22LI8IGJREUsRBT60P0sFdgQgvQRqyvgItrs=
|
||||
github.com/GrailFinder/searchagent v0.2.1 h1:c2A8UXEkAMhJgheUzhz4eRH4qvDfRJdZ0PB+Pf6TTAo=
|
||||
github.com/GrailFinder/searchagent v0.2.1/go.mod h1:d66tn5+22LI8IGJREUsRBT60P0sFdgQgvQRqyvgItrs=
|
||||
github.com/PuerkitoBio/goquery v1.11.0 h1:jZ7pwMQXIITcUXNH83LLk+txlaEy6NVOfTuP43xxfqw=
|
||||
github.com/PuerkitoBio/goquery v1.11.0/go.mod h1:wQHgxUOU3JGuj3oD/QFfxUdlzW6xPHfqyHre6VMY4DQ=
|
||||
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
||||
|
||||
51
helpfuncs.go
51
helpfuncs.go
@@ -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)
|
||||
}
|
||||
@@ -98,6 +98,9 @@ func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
|
||||
// It filters messages for the character the user is currently "writing as"
|
||||
// and updates the textView with the filtered conversation
|
||||
func refreshChatDisplay() {
|
||||
if cfg.CLIMode {
|
||||
return
|
||||
}
|
||||
// Determine which character's view to show
|
||||
viewingAs := cfg.UserRole
|
||||
if cfg.WriteNextMsgAs != "" {
|
||||
@@ -148,7 +151,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 +160,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 +170,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)
|
||||
@@ -178,17 +181,22 @@ func colorText() {
|
||||
for i, tb := range thinkBlocks {
|
||||
text = strings.Replace(text, fmt.Sprintf(placeholderThink, i), tb, 1)
|
||||
}
|
||||
// text = strings.ReplaceAll(text, `$\rightarrow$`, "->")
|
||||
text = RenderLatex(text)
|
||||
textView.SetText(text)
|
||||
}
|
||||
|
||||
func updateStatusLine() {
|
||||
if cfg.CLIMode {
|
||||
return // no status line in cli mode
|
||||
}
|
||||
status := makeStatusLine()
|
||||
statusLineWidget.SetText(status)
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -595,7 +603,6 @@ func executeCommandAndDisplay(cmdText string) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Use /bin/sh to support pipes, redirects, etc.
|
||||
cmd := exec.Command("/bin/sh", "-c", cmdText)
|
||||
cmd.Dir = workingDir
|
||||
@@ -1015,3 +1022,27 @@ 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]
|
||||
}
|
||||
|
||||
func notifySend(topic, message string) error {
|
||||
// Sanitize message to remove control characters that notify-send doesn't handle
|
||||
sanitized := strings.Map(func(r rune) rune {
|
||||
if r < 32 && r != '\t' {
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}, message)
|
||||
// Truncate if too long
|
||||
if len(sanitized) > 200 {
|
||||
sanitized = sanitized[:197] + "..."
|
||||
}
|
||||
cmd := exec.Command("notify-send", topic, sanitized)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
190
latex.go
Normal file
190
latex.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
mathInline = regexp.MustCompile(`\$([^\$]+)\$`) // $...$
|
||||
mathDisplay = regexp.MustCompile(`\$\$([^\$]+)\$\$`) // $$...$$
|
||||
)
|
||||
|
||||
// RenderLatex converts all LaTeX math blocks in a string to terminal‑friendly text.
|
||||
func RenderLatex(text string) string {
|
||||
// Handle display math ($$...$$) – add newlines for separation
|
||||
text = mathDisplay.ReplaceAllStringFunc(text, func(match string) string {
|
||||
inner := mathDisplay.FindStringSubmatch(match)[1]
|
||||
return "\n" + convertLatex(inner) + "\n"
|
||||
})
|
||||
// Handle inline math ($...$)
|
||||
text = mathInline.ReplaceAllStringFunc(text, func(match string) string {
|
||||
inner := mathInline.FindStringSubmatch(match)[1]
|
||||
return convertLatex(inner)
|
||||
})
|
||||
return text
|
||||
}
|
||||
|
||||
func convertLatex(s string) string {
|
||||
// ----- 1. Greek letters -----
|
||||
greek := map[string]string{
|
||||
`\alpha`: "α", `\beta`: "β", `\gamma`: "γ", `\delta`: "δ",
|
||||
`\epsilon`: "ε", `\zeta`: "ζ", `\eta`: "η", `\theta`: "θ",
|
||||
`\iota`: "ι", `\kappa`: "κ", `\lambda`: "λ", `\mu`: "μ",
|
||||
`\nu`: "ν", `\xi`: "ξ", `\pi`: "π", `\rho`: "ρ",
|
||||
`\sigma`: "σ", `\tau`: "τ", `\upsilon`: "υ", `\phi`: "φ",
|
||||
`\chi`: "χ", `\psi`: "ψ", `\omega`: "ω",
|
||||
`\Gamma`: "Γ", `\Delta`: "Δ", `\Theta`: "Θ", `\Lambda`: "Λ",
|
||||
`\Xi`: "Ξ", `\Pi`: "Π", `\Sigma`: "Σ", `\Upsilon`: "Υ",
|
||||
`\Phi`: "Φ", `\Psi`: "Ψ", `\Omega`: "Ω",
|
||||
}
|
||||
for cmd, uni := range greek {
|
||||
s = strings.ReplaceAll(s, cmd, uni)
|
||||
}
|
||||
|
||||
// ----- 2. Arrows, relations, operators, symbols -----
|
||||
symbols := map[string]string{
|
||||
// Arrows
|
||||
`\leftarrow`: "←", `\rightarrow`: "→", `\leftrightarrow`: "↔",
|
||||
`\Leftarrow`: "⇐", `\Rightarrow`: "⇒", `\Leftrightarrow`: "⇔",
|
||||
`\uparrow`: "↑", `\downarrow`: "↓", `\updownarrow`: "↕",
|
||||
`\mapsto`: "↦", `\to`: "→", `\gets`: "←",
|
||||
// Relations
|
||||
`\le`: "≤", `\ge`: "≥", `\neq`: "≠", `\approx`: "≈",
|
||||
`\equiv`: "≡", `\pm`: "±", `\mp`: "∓", `\times`: "×",
|
||||
`\div`: "÷", `\cdot`: "·", `\circ`: "°", `\bullet`: "•",
|
||||
// Other symbols
|
||||
`\infty`: "∞", `\partial`: "∂", `\nabla`: "∇", `\exists`: "∃",
|
||||
`\forall`: "∀", `\in`: "∈", `\notin`: "∉", `\subset`: "⊂",
|
||||
`\subseteq`: "⊆", `\supset`: "⊃", `\supseteq`: "⊇", `\cup`: "∪",
|
||||
`\cap`: "∩", `\emptyset`: "∅", `\ell`: "ℓ", `\Re`: "ℜ",
|
||||
`\Im`: "ℑ", `\wp`: "℘", `\dag`: "†", `\ddag`: "‡",
|
||||
`\prime`: "′", `\degree`: "°", // some LLMs output \degree
|
||||
}
|
||||
for cmd, uni := range symbols {
|
||||
s = strings.ReplaceAll(s, cmd, uni)
|
||||
}
|
||||
|
||||
// ----- 3. Remove \text{...} -----
|
||||
textRe := regexp.MustCompile(`\\text\{([^}]*)\}`)
|
||||
s = textRe.ReplaceAllString(s, "$1")
|
||||
|
||||
// ----- 4. Fractions: \frac{a}{b} → a/b -----
|
||||
fracRe := regexp.MustCompile(`\\frac\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}`)
|
||||
s = fracRe.ReplaceAllString(s, "$1/$2")
|
||||
|
||||
// ----- 5. Remove formatting commands (\mathrm, \mathbf, etc.) -----
|
||||
for _, cmd := range []string{"mathrm", "mathbf", "mathit", "mathsf", "mathtt", "mathbb", "mathcal"} {
|
||||
re := regexp.MustCompile(`\\` + cmd + `\{([^}]*)\}`)
|
||||
s = re.ReplaceAllString(s, "$1")
|
||||
}
|
||||
|
||||
// ----- 6. Subscripts and superscripts -----
|
||||
s = convertSubscripts(s)
|
||||
s = convertSuperscripts(s)
|
||||
|
||||
// ----- 7. Clean up leftover braces (but keep backslashes) -----
|
||||
s = strings.ReplaceAll(s, "{", "")
|
||||
s = strings.ReplaceAll(s, "}", "")
|
||||
|
||||
// ----- 8. (Optional) Remove any remaining backslash+word if you really want -----
|
||||
// But as discussed, this can break things. I'll leave it commented.
|
||||
// cmdRe := regexp.MustCompile(`\\([a-zA-Z]+)`)
|
||||
// s = cmdRe.ReplaceAllString(s, "$1")
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Subscript converter (handles both _{...} and _x)
|
||||
func convertSubscripts(s string) string {
|
||||
subMap := map[rune]string{
|
||||
'0': "₀", '1': "₁", '2': "₂", '3': "₃", '4': "₄",
|
||||
'5': "₅", '6': "₆", '7': "₇", '8': "₈", '9': "₉",
|
||||
'+': "₊", '-': "₋", '=': "₌", '(': "₍", ')': "₎",
|
||||
'a': "ₐ", 'e': "ₑ", 'i': "ᵢ", 'o': "ₒ", 'u': "ᵤ",
|
||||
'v': "ᵥ", 'x': "ₓ",
|
||||
}
|
||||
// Braced: _{...}
|
||||
reBraced := regexp.MustCompile(`_\{([^}]*)\}`)
|
||||
s = reBraced.ReplaceAllStringFunc(s, func(match string) string {
|
||||
inner := reBraced.FindStringSubmatch(match)[1]
|
||||
return subscriptify(inner, subMap)
|
||||
})
|
||||
// Unbraced: _x (single character)
|
||||
reUnbraced := regexp.MustCompile(`_([a-zA-Z0-9])`)
|
||||
s = reUnbraced.ReplaceAllStringFunc(s, func(match string) string {
|
||||
ch := rune(match[1])
|
||||
if sub, ok := subMap[ch]; ok {
|
||||
return sub
|
||||
}
|
||||
return match // keep original _x
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func subscriptify(inner string, subMap map[rune]string) string {
|
||||
var out strings.Builder
|
||||
for _, ch := range inner {
|
||||
if sub, ok := subMap[ch]; ok {
|
||||
out.WriteString(sub)
|
||||
} else {
|
||||
return "_{" + inner + "}" // fallback
|
||||
}
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
// Superscript converter (handles both ^{...} and ^x)
|
||||
func convertSuperscripts(s string) string {
|
||||
supMap := map[rune]string{
|
||||
'0': "⁰", '1': "¹", '2': "²", '3': "³", '4': "⁴",
|
||||
'5': "⁵", '6': "⁶", '7': "⁷", '8': "⁸", '9': "⁹",
|
||||
'+': "⁺", '-': "⁻", '=': "⁼", '(': "⁽", ')': "⁾",
|
||||
'n': "ⁿ", 'i': "ⁱ",
|
||||
}
|
||||
// Special single-character superscripts that replace the caret entirely
|
||||
specialSup := map[string]string{
|
||||
"°": "°", // degree
|
||||
"'": "′", // prime
|
||||
"\"": "″", // double prime
|
||||
}
|
||||
// Braced: ^{...}
|
||||
reBraced := regexp.MustCompile(`\^\{(.*?)\}`)
|
||||
s = reBraced.ReplaceAllStringFunc(s, func(match string) string {
|
||||
inner := reBraced.FindStringSubmatch(match)[1]
|
||||
return superscriptify(inner, supMap, specialSup)
|
||||
})
|
||||
// Unbraced: ^x (single character)
|
||||
reUnbraced := regexp.MustCompile(`\^([^\{[:space:]]?)`)
|
||||
s = reUnbraced.ReplaceAllStringFunc(s, func(match string) string {
|
||||
if len(match) < 2 {
|
||||
return match
|
||||
}
|
||||
ch := match[1:]
|
||||
if special, ok := specialSup[ch]; ok {
|
||||
return special
|
||||
}
|
||||
if len(ch) == 1 {
|
||||
if sup, ok := supMap[rune(ch[0])]; ok {
|
||||
return sup
|
||||
}
|
||||
}
|
||||
return match // keep ^x
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func superscriptify(inner string, supMap map[rune]string, specialSup map[string]string) string {
|
||||
if special, ok := specialSup[inner]; ok {
|
||||
return special
|
||||
}
|
||||
var out strings.Builder
|
||||
for _, ch := range inner {
|
||||
if sup, ok := supMap[ch]; ok {
|
||||
out.WriteString(sup)
|
||||
} else {
|
||||
return "^{" + inner + "}" // fallback
|
||||
}
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
15
llm.go
15
llm.go
@@ -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 {
|
||||
|
||||
292
main.go
292
main.go
@@ -1,6 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"gf-lt/models"
|
||||
"gf-lt/pngmeta"
|
||||
"gf-lt/tools"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
@@ -22,9 +32,32 @@ 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() {
|
||||
// parse flags
|
||||
flag.BoolVar(&cfg.CLIMode, "cli", false, "Run in CLI mode without TUI")
|
||||
flag.BoolVar(&cfg.ToolUse, "tools", true, "run with tools")
|
||||
flag.StringVar(&cfg.CurrentModel, "model", "modelname", "name of the model to use")
|
||||
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 {
|
||||
initTUI()
|
||||
}
|
||||
chatBody.Model = cfg.CurrentModel
|
||||
go updateModelLists()
|
||||
tools.InitTools(cfg, logger, store)
|
||||
// tooler = tools.InitTools(cfg, logger, store)
|
||||
// tooler.RegisterWindowTools(modelHasVision)
|
||||
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 +65,262 @@ 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 sessions")
|
||||
fmt.Println(" /hs [index] - Show chat history (messages)")
|
||||
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 "/hs":
|
||||
if len(chatBody.Messages) == 0 {
|
||||
fmt.Println("No messages in current chat.")
|
||||
return true
|
||||
}
|
||||
if len(args) == 0 {
|
||||
fmt.Println("Chat history:")
|
||||
for i := range chatBody.Messages {
|
||||
fmt.Printf("%d: %s\n", i, MsgToText(i, &chatBody.Messages[i]))
|
||||
}
|
||||
return true
|
||||
}
|
||||
idx, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("Invalid index: %s\n", args[0])
|
||||
return true
|
||||
}
|
||||
if idx < 0 {
|
||||
idx = len(chatBody.Messages) + idx
|
||||
}
|
||||
if idx < 0 || idx >= len(chatBody.Messages) {
|
||||
fmt.Printf("Index out of range (0-%d)\n", len(chatBody.Messages)-1)
|
||||
return true
|
||||
}
|
||||
fmt.Printf("%d: %s\n", idx, MsgToText(idx, &chatBody.Messages[idx]))
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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"}
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
})
|
||||
@@ -129,6 +126,9 @@ func makePropsTable(props map[string]float32) *tview.Table {
|
||||
addCheckboxRow("Image Preview (file picker)", cfg.ImagePreview, func(checked bool) {
|
||||
cfg.ImagePreview = checked
|
||||
})
|
||||
addCheckboxRow("Allow FS out of root", cfg.FSAllowOutOfRoot, func(checked bool) {
|
||||
cfg.FSAllowOutOfRoot = checked
|
||||
})
|
||||
addCheckboxRow("Auto turn (for cards with many chars)", cfg.AutoTurn, func(checked bool) {
|
||||
cfg.AutoTurn = checked
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,13 @@ 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 }
|
||||
|
||||
// TableLister method
|
||||
func (d dummyStore) ListTables() ([]string, error) { return nil, nil }
|
||||
func (d dummyStore) GetTableColumns(table string) ([]storage.TableColumn, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// VectorRepo methods (not used but required by interface)
|
||||
func (d dummyStore) WriteVector(row *models.VectorRow) error { return nil }
|
||||
@@ -297,7 +304,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 clan’s renown and history that they were able to get away with building something that dwarfed the mansions of the nobility.`
|
||||
|
||||
chunks := []*models.VectorRow{
|
||||
{
|
||||
Slug: "kjv_bible.epub_1786_0",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
28
session.go
28
session.go
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
-- 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;
|
||||
|
||||
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;
|
||||
87
storage/migrations/005_drop_unused_embeddings.down.sql
Normal file
87
storage/migrations/005_drop_unused_embeddings.down.sql
Normal 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);
|
||||
32
storage/migrations/005_drop_unused_embeddings.up.sql
Normal file
32
storage/migrations/005_drop_unused_embeddings.up.sql
Normal 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;
|
||||
@@ -1,6 +1,7 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"gf-lt/models"
|
||||
"log/slog"
|
||||
|
||||
@@ -12,6 +13,12 @@ type FullRepo interface {
|
||||
ChatHistory
|
||||
Memories
|
||||
VectorRepo
|
||||
TableLister
|
||||
}
|
||||
|
||||
type TableLister interface {
|
||||
ListTables() ([]string, error)
|
||||
GetTableColumns(table string) ([]TableColumn, error)
|
||||
}
|
||||
|
||||
type ChatHistory interface {
|
||||
@@ -130,3 +137,24 @@ func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo {
|
||||
func (p ProviderSQL) DB() *sqlx.DB {
|
||||
return p.db
|
||||
}
|
||||
|
||||
type TableColumn struct {
|
||||
CID int `db:"cid"`
|
||||
Name string `db:"name"`
|
||||
Type string `db:"type"`
|
||||
NotNull bool `db:"notnull"`
|
||||
DFltVal sql.NullString `db:"dflt_value"`
|
||||
PK int `db:"pk"`
|
||||
}
|
||||
|
||||
func (p ProviderSQL) ListTables() ([]string, error) {
|
||||
resp := []string{}
|
||||
err := p.db.Select(&resp, "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name;")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (p ProviderSQL) GetTableColumns(table string) ([]TableColumn, error) {
|
||||
resp := []TableColumn{}
|
||||
err := p.db.Select(&resp, "PRAGMA table_info("+table+");")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
for _, table := range tableNames {
|
||||
query := "SELECT DISTINCT filename FROM " + table
|
||||
query := "SELECT DISTINCT filename FROM embeddings_768"
|
||||
rows, err := p.db.Query(query)
|
||||
if err != nil {
|
||||
// Continue if one table doesn't exist
|
||||
continue
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var files []string
|
||||
defer rows.Close()
|
||||
var allFiles []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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"sys_prompt": "You are an expert software engineering assistant. Your goal is to help users with coding tasks, debugging, refactoring, and software development.\n\n## Core Principles\n1. **Security First**: Never expose secrets, keys, or credentials. Never commit sensitive data.\n2. **No Git Actions**: You can READ git info (status, log, diff) for context, but NEVER perform git actions (commit, add, push, checkout, reset, rm, etc.). Let the user handle all git operations.\n3. **Explore Before Execute**: Always understand the codebase structure before making changes.\n4. **Follow Conventions**: Match existing code style, patterns, and frameworks used in the project.\n5. **Be Concise**: Minimize output tokens while maintaining quality. Avoid unnecessary explanations.\n6. **Ask First**: When uncertain about intent, ask the user. Don't assume.\n\n## Workflow for Complex Tasks\nFor multi-step tasks, ALWAYS use the todo system to track progress:\n\n1. **Create Todo List**: At the start of complex tasks, use `todo_create` to break down work into actionable items.\n2. **Update Progress**: Mark items as `in_progress` when working on them, and `completed` when done.\n3. **Check Status**: Use `todo_read` to review your progress.\n\nExample workflow:\n- User: \"Add user authentication to this app\"\n- You: Create todos: [\"Analyze existing auth structure\", \"Check frameworks in use\", \"Implement auth middleware\", \"Add login endpoints\", \"Test implementation\"]\n\n## Task Execution Flow\n\n### Phase 1: Exploration (Always First)\n- Use `file_list` to understand directory structure (path defaults to FilePickerDir if not specified)\n- Use `file_read` to examine relevant files (paths are relative to FilePickerDir unless starting with `/`)\n- Use `execute_command` with `grep`/`find` to search for patterns\n- Check README, Makefile, package.json, or similar for build/test commands\n- Identify: frameworks, conventions, testing approach, lint/typecheck commands\n- **Git reads allowed**: You may use `git status`, `git log`, `git diff` for context, but only to inform your work\n- **Path handling**: Relative paths resolve against FilePickerDir; absolute paths (starting with `/`) bypass it\n\n### Phase 2: Planning\n- For complex tasks: create todo items\n- Identify files that need modification\n- Plan your approach following existing patterns\n\n### Phase 3: Implementation\n- Make changes using appropriate file tools\n- Prefer `file_write` for new files, `file_read` then edit for existing files\n- Follow existing code style exactly\n- Use existing libraries and utilities\n\n### Phase 4: Verification\n- Run tests if available (check for test scripts in README/Makefile)\n- Run linting/type checking commands\n- Verify changes work as expected\n\n### Phase 5: Completion\n- Update todos to `completed`\n- Provide concise summary of changes\n- Reference specific file paths and line numbers when relevant\n- **DO NOT commit changes** - inform user what was done so they can review and commit themselves\n\n## Command Execution\n- Use `execute_command` with a single string containing command and arguments (e.g., `go run main.go`, `ls -la`, `cd /tmp`)\n- Use `cd /path` to change the working directory for file operations",
|
||||
"sys_prompt": "You are a software engineering assistant. Your goal is to help user with coding tasks, debugging, refactoring, and software development.\n\n## Core Principles\n1. **Security First**: Never expose secrets, keys, or credentials. Never commit sensitive data.\n2. **No Git Actions**: You can READ git info (status, log, diff) for context, but NEVER perform git actions (commit, add, push, checkout, reset, rm, etc.). Let the user handle all git operations.\n3. **Explore Before Execute**: Always understand the codebase structure before making changes.\n4. **Follow Conventions**: Match existing code style, patterns, and frameworks used in the project.\n5. **Be Concise**: Minimize output tokens while maintaining quality. Avoid unnecessary explanations.\n6. **Ask First**: When uncertain about intent, ask the user. Don't assume.\n\n## Workflow for Complex Tasks\nFor multi-step tasks, ALWAYS use the todo system to track progress:\n\n1. **Create Todo List**: At the start of complex tasks, use `todo create` to break down work into actionable items.\n2. **Update Progress**: Mark items as `in_progress` when working on them, and `completed` when done.\n3. **Check Status**: Use `todo read` to review your progress.\n\nExample workflow:\n- User: \"Add user authentication to this app\"\n- You: Create todos: [\"Analyze existing auth structure\", \"Check frameworks in use\", \"Implement auth middleware\", \"Add login endpoints\", \"Test implementation\"]\n\n## Task Execution Flow\n\n### Phase 1: Exploration (Always First)\n- Use `run \"ls\"` to understand directory structure\n- Use `run \"cat <file>\"` to examine relevant files\n- Use `run \"grep <pattern>\"` or `run \"find . -name *.go\"` to search for patterns\n- Check README, Makefile, go.mod for build/test commands\n- Identify: frameworks, conventions, testing approach, lint/typecheck commands\n- **Git reads allowed**: You may use `run \"git status\"`, `run \"git log\"`, `run \"git diff\"` for context\n- **Path handling**: Relative paths resolve against FilePickerDir; absolute paths (starting with `/`) bypass it\n\n### Phase 2: Planning\n- For complex tasks: create todo items\n- Identify files that need modification\n- Plan your approach following existing patterns\n\n### Phase 3: Implementation\n- Make changes using `run \"write <file> <content>\"` for new files or full rewrites. For small targeted changes, use `run \"sed -i 's/old/new/g' <file>\"`. For larger edits, read the file first and then use write to overwrite the entire file with updated content.\n- Follow existing code style exactly\n- Use existing libraries and utilities\n\n### Phase 4: Verification\n- Run tests if available (check for test commands in README/Makefile)\n- Run linting/type checking commands\n- Verify changes work as expected\n\n### Phase 5: Completion\n- Update todos to `completed`\n- Provide concise summary of changes\n- Reference specific file paths and line numbers when relevant\n- **DO NOT commit changes** - inform user what was done so they can review and commit themselves\n\n## Available Commands\n- `run \"ls [path]\"` - list files in directory\n- `run \"cat <file>\"` - read file content\n- `run \"write <file> <content>\"` - write/overwrite content to file\n- `run \"stat <file>\"` - get file info (size, type, modified)\n- `run \"rm <file>\"` - delete file\n- `run \"cp <src> <dst>\"` - copy file\n- `run \"mv <src> <dst>\"` - move/rename file\n- `run \"mkdir <dir>\"` - create directory\n- `run \"pwd\"` - print working directory\n- `run \"cd <dir>\"` - change directory\n- `run \"sed 's/old/new/' [file]\"` - text replacement (use -i for in-place editing)\n- `run \"grep <pattern> [file]\"` - filter lines\n- `run \"head [n] [file]\"` - show first n lines\n- `run \"tail [n] [file]\"` - show last n lines\n- `run \"wc [-l|-w|-c] [file]\"` - count lines/words/chars\n- `run \"sort [-r] [file]\"` - sort lines\n- `run \"uniq [file]\"` - remove duplicates\n- `run \"echo <text>\"` - echo back input\n- `run \"time\"` - show current time\n- `run \"go <cmd>\"` - go commands (run, build, test, mod, etc.)\n- `run \"git <cmd>\"` - git commands (status, log, diff, show, branch, etc.)\n- `run \"memory store <topic> <data>\"` - save to memory\n- `run \"memory get <topic>\"` - retrieve from memory\n- `run \"memory list\"` - list all topics\n- `run \"memory forget <topic>\"` - delete from memory\n- `run \"todo create <task>\"` - create a todo\n- `run \"todo read\"` - list all todos\n- `run \"todo update <id> <status>\"` - update todo\n- `run \"todo delete <id>\"` - delete a todo\n- `run \"view_img <file>\"` - view an image file\n\nUse: run \"command\" to execute. Supports chaining: cmd1 | cmd2, cmd1 && cmd2",
|
||||
"role": "CodingAssistant",
|
||||
"filepath": "sysprompts/coding_assistant.json",
|
||||
"first_msg": "Hello! I'm your coding assistant. Give me a specific task and I'll get started. For complex work, I'll track progress with todos."
|
||||
|
||||
386
tables.go
386
tables.go
@@ -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)
|
||||
@@ -272,7 +273,7 @@ func makeRAGTable(fileList []string, loadedFiles []string) *tview.Flex {
|
||||
fileTable := tview.NewTable().
|
||||
SetBorders(true)
|
||||
longStatusView := tview.NewTextView()
|
||||
longStatusView.SetText("press x to exit")
|
||||
longStatusView.SetText("press x to exit | press d to view DB")
|
||||
longStatusView.SetBorder(true).SetTitle("status")
|
||||
longStatusView.SetChangedFunc(func() {
|
||||
app.Draw()
|
||||
@@ -497,6 +498,14 @@ func makeRAGTable(fileList []string, loadedFiles []string) *tview.Flex {
|
||||
pages.RemovePage(RAGPage)
|
||||
return nil
|
||||
}
|
||||
if event.Key() == tcell.KeyRune && event.Rune() == 'd' {
|
||||
pages.RemovePage(RAGPage)
|
||||
dbTable := makeDbTable()
|
||||
if dbTable != nil {
|
||||
pages.AddPage(dbTablesPage, dbTable, true, true)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return event
|
||||
})
|
||||
return ragflex
|
||||
@@ -1188,3 +1197,376 @@ func makeFilePicker() *tview.Flex {
|
||||
})
|
||||
return flex
|
||||
}
|
||||
|
||||
func makeDbTable() *tview.Flex {
|
||||
tables, err := store.ListTables()
|
||||
if err != nil {
|
||||
logger.Error("failed to list tables", "error", err)
|
||||
showToast("error", "failed to list tables: "+err.Error())
|
||||
return nil
|
||||
}
|
||||
if len(tables) == 0 {
|
||||
showToast("info", "no tables found in database")
|
||||
return nil
|
||||
}
|
||||
tblList := tview.NewList().ShowSecondaryText(false)
|
||||
rowCounts := make(map[string]int)
|
||||
for _, t := range tables {
|
||||
var count int
|
||||
_ = store.DB().Get(&count, "SELECT COUNT(*) FROM "+t)
|
||||
rowCounts[t] = count
|
||||
tblList.AddItem(t, fmt.Sprintf("%d rows", count), 0, nil)
|
||||
}
|
||||
tblList.SetBorder(true).SetTitle("Tables")
|
||||
dataTable := tview.NewTable().SetBorders(true)
|
||||
dataTable.SetBorder(true).SetTitle("Data")
|
||||
flex := tview.NewFlex().
|
||||
AddItem(tblList, 0, 1, true).
|
||||
AddItem(dataTable, 0, 2, false)
|
||||
loadTableData := func(tableName string, tbl *tview.Table) {
|
||||
rows, err := store.DB().Queryx("SELECT * FROM " + tableName + " LIMIT 80")
|
||||
if err != nil {
|
||||
logger.Error("failed to query table", "table", tableName, "error", err)
|
||||
return
|
||||
}
|
||||
columnNames, _ := rows.Columns()
|
||||
tbl.Clear()
|
||||
for c, name := range columnNames {
|
||||
tbl.SetCell(0, c,
|
||||
tview.NewTableCell(name).
|
||||
SetTextColor(tcell.ColorYellow).
|
||||
SetAlign(tview.AlignCenter))
|
||||
}
|
||||
r := 1
|
||||
for rows.Next() {
|
||||
row := make(map[string]interface{})
|
||||
if err := rows.MapScan(row); err != nil {
|
||||
continue
|
||||
}
|
||||
for c, name := range columnNames {
|
||||
val, ok := row[name]
|
||||
var cellText string
|
||||
var color tcell.Color
|
||||
if !ok || val == nil {
|
||||
cellText = "NULL"
|
||||
color = tcell.ColorDarkGray
|
||||
} else {
|
||||
cellText = fmt.Sprintf("%v", val)
|
||||
if len(cellText) > 30 {
|
||||
cellText = cellText[:30] + "..."
|
||||
}
|
||||
color = tcell.ColorWhite
|
||||
}
|
||||
tbl.SetCell(r, c,
|
||||
tview.NewTableCell(cellText).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter))
|
||||
}
|
||||
r++
|
||||
}
|
||||
rows.Close()
|
||||
tbl.Select(0, 0)
|
||||
}
|
||||
tblList.SetSelectedFunc(func(idx int, mainText, secondaryText string, rune rune) {
|
||||
if idx >= 0 && idx < len(tables) {
|
||||
loadTableData(tables[idx], dataTable)
|
||||
dataTable.SetBorder(true).SetTitle("Data: " + tables[idx])
|
||||
}
|
||||
})
|
||||
tblList.SetChangedFunc(func(idx int, mainText, secondaryText string, rune rune) {
|
||||
if idx >= 0 && idx < len(tables) {
|
||||
loadTableData(tables[idx], dataTable)
|
||||
dataTable.SetBorder(true).SetTitle("Data: " + tables[idx])
|
||||
}
|
||||
})
|
||||
tblList.SetDoneFunc(func() {
|
||||
pages.RemovePage(dbTablesPage)
|
||||
})
|
||||
tblList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
if event.Key() == tcell.KeyRune && event.Rune() == 'x' {
|
||||
pages.RemovePage(dbTablesPage)
|
||||
app.SetFocus(textArea)
|
||||
return nil
|
||||
}
|
||||
if event.Key() == tcell.KeyEnter {
|
||||
idx := tblList.GetCurrentItem()
|
||||
if idx >= 0 && idx < len(tables) {
|
||||
showDbContentView(tables[idx])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return event
|
||||
})
|
||||
if len(tables) > 0 {
|
||||
tblList.SetCurrentItem(0)
|
||||
}
|
||||
return flex
|
||||
}
|
||||
|
||||
func updateColumnsView(tableName string, tbl *tview.Table) {
|
||||
columns, err := store.GetTableColumns(tableName)
|
||||
if err != nil {
|
||||
logger.Error("failed to get table columns", "table", tableName, "error", err)
|
||||
return
|
||||
}
|
||||
tbl.Clear()
|
||||
cols := 5
|
||||
tbl.SetFixed(1, 0)
|
||||
for c := 0; c < cols; c++ {
|
||||
color := tcell.ColorYellow
|
||||
var headerText string
|
||||
switch c {
|
||||
case 0:
|
||||
headerText = "CID"
|
||||
case 1:
|
||||
headerText = "Name"
|
||||
case 2:
|
||||
headerText = "Type"
|
||||
case 3:
|
||||
headerText = "NotNull"
|
||||
case 4:
|
||||
headerText = "PK"
|
||||
}
|
||||
tbl.SetCell(0, c,
|
||||
tview.NewTableCell(headerText).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
for r, col := range columns {
|
||||
for c := 0; c < cols; c++ {
|
||||
color := tcell.ColorWhite
|
||||
if col.PK > 0 {
|
||||
color = tcell.ColorRed
|
||||
}
|
||||
switch c {
|
||||
case 0:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(fmt.Sprintf("%d", col.CID)).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 1:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(col.Name).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 2:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(col.Type).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 3:
|
||||
notNull := "N"
|
||||
if col.NotNull {
|
||||
notNull = "Y"
|
||||
}
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(notNull).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 4:
|
||||
pk := ""
|
||||
if col.PK > 0 {
|
||||
pk = fmt.Sprintf("%d", col.PK)
|
||||
}
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(pk).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
tbl.Select(0, 0)
|
||||
}
|
||||
|
||||
func showDbColumnsView(tableName, parentPage string) {
|
||||
longStatusView := tview.NewTextView()
|
||||
longStatusView.SetText("table: " + tableName + " | press x to exit | press Enter to view content").SetBorder(true).SetTitle("status")
|
||||
longStatusView.SetChangedFunc(func() {
|
||||
app.Draw()
|
||||
})
|
||||
flex := tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(longStatusView, 0, 10, false).
|
||||
AddItem(tview.NewTable().SetBorders(true), 0, 60, true)
|
||||
columns, err := store.GetTableColumns(tableName)
|
||||
if err != nil {
|
||||
logger.Error("failed to get table columns", "table", tableName, "error", err)
|
||||
showToast("error", "failed to get columns: "+err.Error())
|
||||
return
|
||||
}
|
||||
tbl := flex.GetItem(1).(*tview.Table)
|
||||
cols := 5 // CID | Name | Type | NotNull | PK
|
||||
tbl.SetFixed(1, 0)
|
||||
for c := 0; c < cols; c++ {
|
||||
color := tcell.ColorYellow
|
||||
var headerText string
|
||||
switch c {
|
||||
case 0:
|
||||
headerText = "CID"
|
||||
case 1:
|
||||
headerText = "Name"
|
||||
case 2:
|
||||
headerText = "Type"
|
||||
case 3:
|
||||
headerText = "NotNull"
|
||||
case 4:
|
||||
headerText = "PK"
|
||||
}
|
||||
tbl.SetCell(0, c,
|
||||
tview.NewTableCell(headerText).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
for r, col := range columns {
|
||||
for c := 0; c < cols; c++ {
|
||||
color := tcell.ColorWhite
|
||||
if col.PK > 0 {
|
||||
color = tcell.ColorRed
|
||||
}
|
||||
switch c {
|
||||
case 0:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(fmt.Sprintf("%d", col.CID)).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 1:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(col.Name).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 2:
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(col.Type).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 3:
|
||||
notNull := "N"
|
||||
if col.NotNull {
|
||||
notNull = "Y"
|
||||
}
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(notNull).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
case 4:
|
||||
pk := ""
|
||||
if col.PK > 0 {
|
||||
pk = fmt.Sprintf("%d", col.PK)
|
||||
}
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(pk).
|
||||
SetTextColor(color).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
columnsPageName := "dbColumns"
|
||||
pages.AddPage(columnsPageName, flex, true, true)
|
||||
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
if event.Key() == tcell.KeyRune && event.Rune() == 'x' {
|
||||
pages.RemovePage(columnsPageName)
|
||||
return nil
|
||||
}
|
||||
if event.Key() == tcell.KeyEnter {
|
||||
pages.RemovePage(columnsPageName)
|
||||
showDbContentView(tableName)
|
||||
}
|
||||
return event
|
||||
})
|
||||
}
|
||||
|
||||
func showDbContentView(tableName string) {
|
||||
batchSize := 80
|
||||
longStatusView := tview.NewTextView()
|
||||
longStatusView.SetText("table: " + tableName + " | press Enter to load more").SetBorder(true).SetTitle("status")
|
||||
longStatusView.SetChangedFunc(func() {
|
||||
app.Draw()
|
||||
})
|
||||
tbl := tview.NewTable().SetBorders(true).SetFixed(1, 0)
|
||||
flex := tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(longStatusView, 0, 10, false).
|
||||
AddItem(tbl, 0, 60, true)
|
||||
contentPageName := "db_content_" + tableName
|
||||
offset := 0
|
||||
var rowCount int
|
||||
_ = store.DB().Get(&rowCount, "SELECT COUNT(*) FROM "+tableName)
|
||||
var columnNames []string
|
||||
loadRows := func(off int) {
|
||||
rows, err := store.DB().Queryx("SELECT * FROM " + tableName + " LIMIT " + fmt.Sprintf("%d", batchSize) + " OFFSET " + fmt.Sprintf("%d", off))
|
||||
if err != nil {
|
||||
logger.Error("failed to query table", "table", tableName, "error", err)
|
||||
return
|
||||
}
|
||||
if off == 0 {
|
||||
columnNames, _ = rows.Columns()
|
||||
for c, name := range columnNames {
|
||||
tbl.SetCell(0, c,
|
||||
tview.NewTableCell(name).
|
||||
SetTextColor(tcell.ColorYellow).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
}
|
||||
r := off
|
||||
for rows.Next() {
|
||||
row := make(map[string]interface{})
|
||||
if err := rows.MapScan(row); err != nil {
|
||||
logger.Error("failed to scan row", "error", err)
|
||||
continue
|
||||
}
|
||||
for c, name := range columnNames {
|
||||
val, ok := row[name]
|
||||
if !ok {
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell("NULL").
|
||||
SetTextColor(tcell.ColorDarkGray).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
} else {
|
||||
str := fmt.Sprintf("%v", val)
|
||||
if len(str) > 50 {
|
||||
str = str[:50] + "..."
|
||||
}
|
||||
tbl.SetCell(r+1, c,
|
||||
tview.NewTableCell(str).
|
||||
SetTextColor(tcell.ColorWhite).
|
||||
SetAlign(tview.AlignCenter).
|
||||
SetSelectable(false))
|
||||
}
|
||||
}
|
||||
r++
|
||||
}
|
||||
rows.Close()
|
||||
loaded := tbl.GetRowCount() - 1
|
||||
if loaded < rowCount {
|
||||
longStatusView.SetText(fmt.Sprintf("table: %s | loaded %d of %d rows | press Enter for more", tableName, loaded, rowCount))
|
||||
} else {
|
||||
longStatusView.SetText(fmt.Sprintf("table: %s | loaded %d rows (all)", tableName, loaded))
|
||||
}
|
||||
}
|
||||
loadRows(0)
|
||||
pages.AddPage(contentPageName, flex, true, true)
|
||||
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
if event.Key() == tcell.KeyRune && event.Rune() == 'x' {
|
||||
pages.RemovePage(contentPageName)
|
||||
return nil
|
||||
}
|
||||
if event.Key() == tcell.KeyEnter {
|
||||
offset += batchSize
|
||||
loadRows(offset)
|
||||
tbl.ScrollToEnd()
|
||||
}
|
||||
return event
|
||||
})
|
||||
}
|
||||
|
||||
416
tools/chain.go
Normal file
416
tools/chain.go
Normal file
@@ -0,0 +1,416 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Operator represents a chain operator between commands.
|
||||
type Operator int
|
||||
|
||||
const (
|
||||
OpNone Operator = iota
|
||||
OpAnd // &&
|
||||
OpOr // ||
|
||||
OpSeq // ;
|
||||
OpPipe // |
|
||||
OpRedirect // >
|
||||
OpAppend // >>
|
||||
)
|
||||
|
||||
// Segment is a single command in a chain.
|
||||
type Segment struct {
|
||||
Raw string
|
||||
Op Operator // operator AFTER this segment
|
||||
RedirectTo string // file path for > or >>
|
||||
IsAppend bool // true for >>, false for >
|
||||
}
|
||||
|
||||
// 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] == '>' {
|
||||
cmd := strings.TrimSpace(current.String())
|
||||
if cmd != "" {
|
||||
segments = append(segments, Segment{
|
||||
Raw: cmd,
|
||||
Op: OpAppend,
|
||||
})
|
||||
}
|
||||
current.Reset()
|
||||
i++ // skip second >
|
||||
continue
|
||||
}
|
||||
// >
|
||||
if ch == '>' {
|
||||
cmd := strings.TrimSpace(current.String())
|
||||
if cmd != "" {
|
||||
segments = append(segments, Segment{
|
||||
Raw: cmd,
|
||||
Op: OpRedirect,
|
||||
})
|
||||
}
|
||||
current.Reset()
|
||||
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"
|
||||
}
|
||||
|
||||
// Check if we have a redirect
|
||||
var redirectTo string
|
||||
var isAppend bool
|
||||
redirectIdx := -1
|
||||
for i, seg := range segments {
|
||||
if seg.Op == OpRedirect || seg.Op == OpAppend {
|
||||
redirectIdx = i
|
||||
isAppend = seg.Op == OpAppend
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if redirectIdx >= 0 && redirectIdx+1 < len(segments) {
|
||||
targetPath, err := resolveRedirectPath(segments[redirectIdx+1].Raw)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("[error] redirect: %v", err)
|
||||
}
|
||||
redirectTo = targetPath
|
||||
redirectCmd := segments[redirectIdx].Raw
|
||||
|
||||
// Check if there's chaining after redirect by looking at the operator of the segment AFTER the target
|
||||
// The segment at redirectIdx+1 is the target file, and its Op tells us what operator follows
|
||||
hasChainingAfterRedirect := false
|
||||
if redirectIdx+1 < len(segments) && segments[redirectIdx+1].Op != OpNone && segments[redirectIdx+1].Op != OpPipe {
|
||||
hasChainingAfterRedirect = true
|
||||
}
|
||||
|
||||
// Remove both the redirect segment and its target
|
||||
segments = append(segments[:redirectIdx], segments[redirectIdx+2:]...)
|
||||
|
||||
// Execute the redirect command
|
||||
var lastOutput string
|
||||
var lastErr error
|
||||
lastOutput, lastErr = execSingle(redirectCmd, "")
|
||||
if lastErr != nil {
|
||||
return fmt.Sprintf("[error] redirect: %v", lastErr)
|
||||
}
|
||||
if err := writeFile(redirectTo, lastOutput, isAppend); err != nil {
|
||||
return fmt.Sprintf("[error] redirect: %v", err)
|
||||
}
|
||||
mode := "Wrote"
|
||||
if isAppend {
|
||||
mode = "Appended"
|
||||
}
|
||||
size := humanSizeChain(int64(len(lastOutput)))
|
||||
redirectResult := fmt.Sprintf("%s %s → %s", mode, size, filepath.Base(redirectTo))
|
||||
|
||||
// If no remaining segments or no chaining, just return the write confirmation
|
||||
if len(segments) == 0 || !hasChainingAfterRedirect {
|
||||
return redirectResult
|
||||
}
|
||||
|
||||
// There are remaining commands after the redirect
|
||||
collected := []string{redirectResult}
|
||||
|
||||
// Execute remaining commands
|
||||
for _, seg := range segments {
|
||||
lastOutput, lastErr = execSingle(seg.Raw, "")
|
||||
if lastOutput != "" {
|
||||
collected = append(collected, lastOutput)
|
||||
}
|
||||
}
|
||||
return strings.Join(collected, "\n")
|
||||
} else if redirectIdx >= 0 && redirectIdx+1 >= len(segments) {
|
||||
return "[error] redirect: target file required"
|
||||
}
|
||||
|
||||
var collected []string
|
||||
var lastOutput string
|
||||
var lastErr error
|
||||
pipeInput := ""
|
||||
for i, seg := range segments {
|
||||
if i > 0 {
|
||||
prevOp := segments[i-1].Op
|
||||
if prevOp == OpAnd && lastErr != nil {
|
||||
continue
|
||||
}
|
||||
if prevOp == OpOr && lastErr == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
segStdin := ""
|
||||
if i == 0 {
|
||||
segStdin = pipeInput
|
||||
} else if segments[i-1].Op == OpPipe {
|
||||
segStdin = lastOutput
|
||||
}
|
||||
lastOutput, lastErr = execSingle(seg.Raw, segStdin)
|
||||
if i < len(segments)-1 && seg.Op == OpPipe {
|
||||
continue
|
||||
}
|
||||
if lastOutput != "" {
|
||||
collected = append(collected, lastOutput)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle redirect if present
|
||||
if redirectTo != "" {
|
||||
output := lastOutput
|
||||
if err := writeFile(redirectTo, output, isAppend); err != nil {
|
||||
return fmt.Sprintf("[error] redirect: %v", err)
|
||||
}
|
||||
mode := "Wrote"
|
||||
if isAppend {
|
||||
mode = "Appended"
|
||||
}
|
||||
size := humanSizeChain(int64(len(output)))
|
||||
return fmt.Sprintf("%s %s → %s", mode, size, filepath.Base(redirectTo))
|
||||
}
|
||||
|
||||
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
|
||||
result, err := execBuiltin(name, args, stdin)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
// Check if it's a "not a builtin" error (meaning we should try system command)
|
||||
if err.Error() == "not a builtin" {
|
||||
// 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
|
||||
}
|
||||
// It's a builtin that returned an error
|
||||
return result, err
|
||||
}
|
||||
|
||||
// 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.
|
||||
func execBuiltin(name string, args []string, stdin string) (string, error) {
|
||||
var result string
|
||||
switch name {
|
||||
case "echo":
|
||||
result = FsEcho(args, stdin)
|
||||
case "time":
|
||||
result = FsTime(args, stdin)
|
||||
case "cat":
|
||||
result = FsCat(args, stdin)
|
||||
case "pwd":
|
||||
result = FsPwd(args, stdin)
|
||||
case "cd":
|
||||
result = FsCd(args, stdin)
|
||||
case "mkdir":
|
||||
result = FsMkdir(args, stdin)
|
||||
case "ls":
|
||||
result = FsLs(args, stdin)
|
||||
case "cp":
|
||||
result = FsCp(args, stdin)
|
||||
case "mv":
|
||||
result = FsMv(args, stdin)
|
||||
case "rm":
|
||||
result = FsRm(args, stdin)
|
||||
case "grep":
|
||||
result = FsGrep(args, stdin)
|
||||
case "head":
|
||||
result = FsHead(args, stdin)
|
||||
case "tail":
|
||||
result = FsTail(args, stdin)
|
||||
case "wc":
|
||||
result = FsWc(args, stdin)
|
||||
case "sort":
|
||||
result = FsSort(args, stdin)
|
||||
case "uniq":
|
||||
result = FsUniq(args, stdin)
|
||||
case "sed":
|
||||
result = FsSed(args, stdin)
|
||||
case "stat":
|
||||
result = FsStat(args, stdin)
|
||||
case "go":
|
||||
if len(args) == 0 {
|
||||
return "[error] usage: go <subcommand> [options]", nil
|
||||
}
|
||||
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)), nil
|
||||
}
|
||||
return string(output), nil
|
||||
default:
|
||||
return "", errors.New("not a builtin")
|
||||
}
|
||||
if strings.HasPrefix(result, "[error]") {
|
||||
return result, errors.New(result)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// resolveRedirectPath resolves the target path for a redirect operator
|
||||
func resolveRedirectPath(path string) (string, error) {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return "", errors.New("redirect target required")
|
||||
}
|
||||
abs, err := resolvePath(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return abs, nil
|
||||
}
|
||||
|
||||
// writeFile writes content to a file (truncate or append)
|
||||
func writeFile(path, content string, append bool) error {
|
||||
flags := os.O_CREATE | os.O_WRONLY
|
||||
if append {
|
||||
flags |= os.O_APPEND
|
||||
} else {
|
||||
flags |= os.O_TRUNC
|
||||
}
|
||||
f, err := os.OpenFile(path, flags, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.WriteString(content)
|
||||
return err
|
||||
}
|
||||
|
||||
// humanSizeChain returns human-readable file size
|
||||
func humanSizeChain(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)
|
||||
}
|
||||
}
|
||||
1167
tools/fs.go
Normal file
1167
tools/fs.go
Normal file
File diff suppressed because it is too large
Load Diff
423
tools/fs_test.go
Normal file
423
tools/fs_test.go
Normal file
@@ -0,0 +1,423 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gf-lt/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cfg = &config.Config{}
|
||||
cwd, _ := os.Getwd()
|
||||
if strings.HasSuffix(cwd, "/tools") || strings.HasSuffix(cwd, "\\tools") {
|
||||
cwd = filepath.Dir(cwd)
|
||||
}
|
||||
cfg.FilePickerDir = cwd
|
||||
}
|
||||
|
||||
func TestFsLs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
check func(string) bool
|
||||
}{
|
||||
{"no args", []string{}, "", func(r string) bool { return strings.Contains(r, "tools/") }},
|
||||
{"long format", []string{"-l"}, "", func(r string) bool { return strings.Contains(r, "f ") }},
|
||||
{"all files", []string{"-a"}, "", func(r string) bool { return strings.Contains(r, ".") || strings.Contains(r, "..") }},
|
||||
{"combine flags", []string{"-la"}, "", func(r string) bool { return strings.Contains(r, "f ") && strings.Contains(r, ".") }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsLs(tt.args, tt.stdin)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.name, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsCat(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_cat.txt")
|
||||
content := "hello\nworld\n"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"file path", []string{tmpFile}, "", "hello\nworld\n"},
|
||||
{"stdin fallback", []string{}, "stdin content", "stdin content"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsCat(tt.args, tt.stdin)
|
||||
if result != tt.want && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsHead(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_head.txt")
|
||||
content := "line1\nline2\nline3\nline4\nline5\n"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"default from stdin", []string{}, "line1\nline2\nline3", "line1\nline2\nline3"},
|
||||
{"n from stdin", []string{"-n", "2"}, "line1\nline2\nline3", "line1\nline2"},
|
||||
{"numeric n", []string{"-2"}, "line1\nline2\nline3", "line1\nline2"},
|
||||
{"file path", []string{tmpFile}, "", "line1\nline2\nline3\nline4\nline5"},
|
||||
{"file with n", []string{"-n", "2", tmpFile}, "", "line1\nline2"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsHead(tt.args, tt.stdin)
|
||||
if result != tt.want && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsTail(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_tail.txt")
|
||||
content := "line1\nline2\nline3\nline4\nline5\n"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"default from stdin", []string{}, "line1\nline2\nline3", "line1\nline2\nline3"},
|
||||
{"n from stdin", []string{"-n", "2"}, "line1\nline2\nline3", "line2\nline3"},
|
||||
{"file path", []string{tmpFile}, "", "line1\nline2\nline3\nline4\nline5"},
|
||||
{"file with n", []string{"-n", "3", tmpFile}, "", "line3\nline4\nline5"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsTail(tt.args, tt.stdin)
|
||||
if result != tt.want && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsWc(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_wc.txt")
|
||||
content := "one two three\nfour five\nsix\n"
|
||||
os.WriteFile(tmpFile, []byte(content), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"default", []string{}, "one two", "1 lines, 2 words, 7 chars"},
|
||||
{"lines", []string{"-l"}, "line1\nline2\nline3", "3"},
|
||||
{"words", []string{"-w"}, "one two three", "3"},
|
||||
{"chars", []string{"-c"}, "abc", "3"},
|
||||
{"file lines", []string{"-l", tmpFile}, "", "3"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsWc(tt.args, tt.stdin)
|
||||
if !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q in output, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsSort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"basic", []string{}, "c\na\nb\n", "a\nb\nc"},
|
||||
{"reverse", []string{"-r"}, "a\nb\nc", "c\nb\na"},
|
||||
{"numeric", []string{"-n"}, "10\n2\n1\n", "1\n2\n10"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsSort(tt.args, tt.stdin)
|
||||
if result != tt.want {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsUniq(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"basic", []string{}, "a\nb\na\nc", "a\nb\nc"},
|
||||
{"count", []string{"-c"}, "a\na\nb", "2 a\n1 b"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsUniq(tt.args, tt.stdin)
|
||||
if result != tt.want && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsGrep(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"basic", []string{"world"}, "hello\nworld\ntest", "world"},
|
||||
{"ignore case", []string{"-i", "WORLD"}, "hello\nworld\ntest", "world"},
|
||||
{"invert", []string{"-v", "world"}, "hello\nworld\ntest", "hello\ntest"},
|
||||
{"count", []string{"-c", "o"}, "hello\no world\no foo", "3"},
|
||||
{"no match", []string{"xyz"}, "hello\nworld", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsGrep(tt.args, tt.stdin)
|
||||
if tt.want != "" && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsEcho(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"single", []string{"hello"}, "", "hello\n"},
|
||||
{"multiple", []string{"hello", "world"}, "", "hello world\n"},
|
||||
{"with stdin", []string{}, "stdin", "stdin"},
|
||||
{"empty", []string{}, "", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsEcho(tt.args, tt.stdin)
|
||||
if result != tt.want {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsPwd(t *testing.T) {
|
||||
result := FsPwd(nil, "")
|
||||
if !strings.Contains(result, "gf-lt") {
|
||||
t.Errorf("expected gf-lt in path, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsTime(t *testing.T) {
|
||||
result := FsTime(nil, "")
|
||||
if len(result) < 10 {
|
||||
t.Errorf("expected time output, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsStat(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_stat.txt")
|
||||
os.WriteFile(tmpFile, []byte("content"), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
result := FsStat([]string{tmpFile}, "")
|
||||
if !strings.Contains(result, "test_stat.txt") {
|
||||
t.Errorf("expected filename in output, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsMkdir(t *testing.T) {
|
||||
testDir := filepath.Join(cfg.FilePickerDir, "test_mkdir_xyz")
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
result := FsMkdir([]string{testDir}, "")
|
||||
if _, err := os.Stat(testDir); err != nil {
|
||||
t.Errorf("directory not created: %v, result: %q", err, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsCp(t *testing.T) {
|
||||
src := filepath.Join(cfg.FilePickerDir, "test_cp_src.txt")
|
||||
dst := filepath.Join(cfg.FilePickerDir, "test_cp_dst.txt")
|
||||
os.WriteFile(src, []byte("test"), 0644)
|
||||
defer os.Remove(src)
|
||||
defer os.Remove(dst)
|
||||
|
||||
result := FsCp([]string{src, dst}, "")
|
||||
if _, err := os.Stat(dst); err != nil {
|
||||
t.Errorf("file not copied: %v, result: %q", err, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsMv(t *testing.T) {
|
||||
src := filepath.Join(cfg.FilePickerDir, "test_mv_src.txt")
|
||||
dst := filepath.Join(cfg.FilePickerDir, "test_mv_dst.txt")
|
||||
os.WriteFile(src, []byte("test"), 0644)
|
||||
defer os.Remove(src)
|
||||
defer os.Remove(dst)
|
||||
|
||||
result := FsMv([]string{src, dst}, "")
|
||||
if _, err := os.Stat(dst); err != nil {
|
||||
t.Errorf("file not moved: %v, result: %q", err, result)
|
||||
}
|
||||
if _, err := os.Stat(src); err == nil {
|
||||
t.Errorf("source file still exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsRm(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_rm_xyz.txt")
|
||||
os.WriteFile(tmpFile, []byte("test"), 0644)
|
||||
|
||||
result := FsRm([]string{tmpFile}, "")
|
||||
if _, err := os.Stat(tmpFile); err == nil {
|
||||
t.Errorf("file not removed, result: %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsSed(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdin string
|
||||
want string
|
||||
}{
|
||||
{"replace", []string{"s/hello/bye/"}, "hello world", "bye world"},
|
||||
{"global", []string{"s/o/X/g"}, "hello world", "hellX wXrld"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := FsSed(tt.args, tt.stdin)
|
||||
if result != tt.want && !strings.Contains(result, tt.want) {
|
||||
t.Errorf("expected %q, got %q", tt.want, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPiping(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_pipe.txt")
|
||||
os.WriteFile(tmpFile, []byte("line3\nline1\nline2"), 0644)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
check func(string) bool
|
||||
}{
|
||||
{"ls | head -3", "ls | head -3", func(r string) bool { return r != "" }},
|
||||
{"sort file", "sort " + tmpFile, func(r string) bool { return strings.Contains(r, "line1") }},
|
||||
{"grep file", "grep line1 " + tmpFile, func(r string) bool { return r == "line1" }},
|
||||
{"wc file", "wc -l " + tmpFile, func(r string) bool { return r == "3" }},
|
||||
{"head file", "head -2 " + tmpFile, func(r string) bool { return strings.Contains(r, "line3") }},
|
||||
{"tail file", "tail -2 " + tmpFile, func(r string) bool { return strings.Contains(r, "line2") }},
|
||||
{"echo | head", "echo a b c | head -2", func(r string) bool { return strings.Contains(r, "a") }},
|
||||
{"echo | wc -l", "echo a b c | wc -l", func(r string) bool { return r == "1" }},
|
||||
{"echo | sort", "echo c a b | sort", func(r string) bool { return strings.Contains(r, "a") }},
|
||||
{"echo | grep", "echo hello world | grep hello", func(r string) bool { return strings.Contains(r, "hello") }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.name, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChaining(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
check func(string) bool
|
||||
}{
|
||||
{"ls && echo ok", "ls && echo ok", func(r string) bool { return strings.Contains(r, "ok") }},
|
||||
{"ls || echo not run", "ls || echo fallback", func(r string) bool { return !strings.Contains(r, "fallback") }},
|
||||
{"false || echo run", "cd /nonexistent123 || echo fallback", func(r string) bool { return strings.Contains(r, "fallback") }},
|
||||
{"echo a ; echo b", "echo a ; echo b", func(r string) bool { return strings.Contains(r, "a") && strings.Contains(r, "b") }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.name, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirect(t *testing.T) {
|
||||
tmpFile := filepath.Join(cfg.FilePickerDir, "test_redirect.txt")
|
||||
os.Remove(tmpFile)
|
||||
defer os.Remove(tmpFile)
|
||||
|
||||
// Test echo >
|
||||
result1 := ExecChain("echo hello world > " + tmpFile)
|
||||
if !strings.Contains(result1, "Wrote") {
|
||||
t.Errorf("echo > failed: %q", result1)
|
||||
}
|
||||
|
||||
// Test cat
|
||||
result2 := ExecChain("cat " + tmpFile)
|
||||
if !strings.Contains(result2, "hello") {
|
||||
t.Errorf("cat failed: %q", result2)
|
||||
}
|
||||
|
||||
// Test echo >>
|
||||
result3 := ExecChain("echo more >> " + tmpFile)
|
||||
if !strings.Contains(result3, "Appended") {
|
||||
t.Errorf("echo >> failed: %q", result3)
|
||||
}
|
||||
|
||||
// Test cat after append
|
||||
result4 := ExecChain("cat " + tmpFile)
|
||||
if !strings.Contains(result4, "hello") || !strings.Contains(result4, "more") {
|
||||
t.Errorf("cat after append failed: %q", result4)
|
||||
}
|
||||
}
|
||||
@@ -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"]
|
||||
1502
tools/tools.go
Normal file
1502
tools/tools.go
Normal file
File diff suppressed because it is too large
Load Diff
316
tools/unix_test.go
Normal file
316
tools/unix_test.go
Normal file
@@ -0,0 +1,316 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gf-lt/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cfg = &config.Config{}
|
||||
cwd, _ := os.Getwd()
|
||||
if strings.HasSuffix(cwd, "/tools") || strings.HasSuffix(cwd, "\\tools") {
|
||||
cwd = filepath.Dir(cwd)
|
||||
}
|
||||
cfg.FilePickerDir = cwd
|
||||
}
|
||||
|
||||
func TestUnixGlobExpansion(t *testing.T) {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_glob_tmp")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "file1.txt"), []byte("content1"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "file2.txt"), []byte("content2"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "file3.log"), []byte("content3"), 0644)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
wantErr bool
|
||||
check func(string) bool
|
||||
}{
|
||||
{
|
||||
name: "ls glob txt files",
|
||||
cmd: "ls " + tmpDir + "/*.txt",
|
||||
wantErr: false,
|
||||
check: func(r string) bool { return strings.Contains(r, "file1.txt") && strings.Contains(r, "file2.txt") },
|
||||
},
|
||||
{
|
||||
name: "cat glob txt files",
|
||||
cmd: "cat " + tmpDir + "/*.txt",
|
||||
wantErr: false,
|
||||
check: func(r string) bool { return strings.Contains(r, "content1") && strings.Contains(r, "content2") },
|
||||
},
|
||||
{
|
||||
name: "ls glob no matches",
|
||||
cmd: "ls " + tmpDir + "/*.nonexistent",
|
||||
wantErr: false,
|
||||
check: func(r string) bool { return strings.Contains(r, "no such file") || strings.Contains(r, "(empty") },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if tt.wantErr && result == "" {
|
||||
t.Errorf("expected error for %q, got empty", tt.cmd)
|
||||
}
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.cmd, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixCatMultipleFiles(t *testing.T) {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_cat_multi")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("file a content\n"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "b.txt"), []byte("file b content\n"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "c.txt"), []byte("file c content\n"), 0644)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
check func(string) bool
|
||||
}{
|
||||
{
|
||||
name: "cat multiple files with paths",
|
||||
cmd: "cat " + tmpDir + "/a.txt " + tmpDir + "/b.txt",
|
||||
check: func(r string) bool {
|
||||
return strings.Contains(r, "file a content") && strings.Contains(r, "file b content")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cat three files",
|
||||
cmd: "cat " + tmpDir + "/a.txt " + tmpDir + "/b.txt " + tmpDir + "/c.txt",
|
||||
check: func(r string) bool {
|
||||
return strings.Contains(r, "file a content") && strings.Contains(r, "file b content") && strings.Contains(r, "file c content")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cat via shell with glob",
|
||||
cmd: "cat " + tmpDir + "/*.txt",
|
||||
check: func(r string) bool {
|
||||
return strings.Contains(r, "file a content") && strings.Contains(r, "file b content")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.cmd, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixGrepPatternQuoting(t *testing.T) {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_grep_quote")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "animals.txt"), []byte("dog\ncat\nbird\nfish\n"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "colors.txt"), []byte("red\nblue\ngreen\n"), 0644)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
check func(string) bool
|
||||
}{
|
||||
{
|
||||
name: "grep with double quotes OR pattern",
|
||||
cmd: "grep -E \"dog|cat\" " + tmpDir + "/animals.txt",
|
||||
check: func(r string) bool { return strings.Contains(r, "dog") && strings.Contains(r, "cat") },
|
||||
},
|
||||
{
|
||||
name: "grep with single quotes OR pattern",
|
||||
cmd: "grep -E 'dog|cat' " + tmpDir + "/animals.txt",
|
||||
check: func(r string) bool { return strings.Contains(r, "dog") && strings.Contains(r, "cat") },
|
||||
},
|
||||
{
|
||||
name: "grep case insensitive with quotes",
|
||||
cmd: "grep -iE \"DOG|CAT\" " + tmpDir + "/animals.txt",
|
||||
check: func(r string) bool { return strings.Contains(r, "dog") && strings.Contains(r, "cat") },
|
||||
},
|
||||
{
|
||||
name: "grep piped from cat",
|
||||
cmd: "cat " + tmpDir + "/animals.txt | grep -E \"dog|cat\"",
|
||||
check: func(r string) bool { return strings.Contains(r, "dog") && strings.Contains(r, "cat") },
|
||||
},
|
||||
{
|
||||
name: "grep with complex pattern",
|
||||
cmd: "grep -E \"red|blue|green\" " + tmpDir + "/colors.txt",
|
||||
check: func(r string) bool {
|
||||
return strings.Contains(r, "red") && strings.Contains(r, "blue") && strings.Contains(r, "green")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.cmd, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixForLoop(t *testing.T) {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_forloop")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "dog.txt"), []byte("I have a dog\n"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "cat.txt"), []byte("I have a cat\n"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "red.txt"), []byte("red color\n"), 0644)
|
||||
|
||||
result := ExecChain("cd " + tmpDir + " && for f in *.txt; do echo \"file: $f\"; done")
|
||||
if result == "" {
|
||||
t.Error("empty result from for loop execution")
|
||||
}
|
||||
if strings.Contains(result, "file:") {
|
||||
t.Logf("for loop is supported: %s", result)
|
||||
} else {
|
||||
t.Logf("for loops not supported (expected): %s", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixGlobWithFileOps(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
setup func() string
|
||||
check func(string) bool
|
||||
}{
|
||||
{
|
||||
name: "rm glob txt files",
|
||||
cmd: "rm {dir}/*.txt",
|
||||
setup: func() string {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_rm_glob")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("content"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "b.txt"), []byte("content"), 0644)
|
||||
return tmpDir
|
||||
},
|
||||
check: func(r string) bool { return !strings.Contains(r, "[error]") },
|
||||
},
|
||||
{
|
||||
name: "cp glob to dest",
|
||||
cmd: "cp {dir}/*.txt {dir}/dest/",
|
||||
setup: func() string {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_cp_glob")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
os.MkdirAll(filepath.Join(tmpDir, "dest"), 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("content a"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "b.txt"), []byte("content b"), 0644)
|
||||
return tmpDir
|
||||
},
|
||||
check: func(r string) bool { return !strings.Contains(r, "[error]") },
|
||||
},
|
||||
{
|
||||
name: "mv glob to dest",
|
||||
cmd: "mv {dir}/*.log {dir}/dest/",
|
||||
setup: func() string {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_mv_glob")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
os.MkdirAll(filepath.Join(tmpDir, "dest"), 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "c.log"), []byte("content c"), 0644)
|
||||
return tmpDir
|
||||
},
|
||||
check: func(r string) bool { return !strings.Contains(r, "[error]") },
|
||||
},
|
||||
{
|
||||
name: "ls with flags and glob",
|
||||
cmd: "ls -la {dir}/*.txt",
|
||||
setup: func() string {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_ls_glob")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("content"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "b.txt"), []byte("content"), 0644)
|
||||
return tmpDir
|
||||
},
|
||||
check: func(r string) bool { return strings.Contains(r, "a.txt") || strings.Contains(r, "b.txt") },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tmpDir := tt.setup()
|
||||
defer os.RemoveAll(tmpDir)
|
||||
cmd := strings.ReplaceAll(tt.cmd, "{dir}", tmpDir)
|
||||
result := ExecChain(cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", cmd, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixComplexPiping(t *testing.T) {
|
||||
tmpDir := filepath.Join(cfg.FilePickerDir, "test_pipe_complex")
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "data.txt"), []byte("apple\nbanana\nAPPLE\ncherry\nbanana\n"), 0644)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
check func(string) bool
|
||||
}{
|
||||
{
|
||||
name: "cat | grep -i | sort",
|
||||
cmd: "cat " + tmpDir + "/data.txt | grep -i apple | sort",
|
||||
check: func(r string) bool { return strings.Contains(r, "apple") && !strings.Contains(r, "banana") },
|
||||
},
|
||||
{
|
||||
name: "ls | wc -l",
|
||||
cmd: "ls " + tmpDir + " | wc -l",
|
||||
check: func(r string) bool { return strings.TrimSpace(r) == "1" },
|
||||
},
|
||||
{
|
||||
name: "echo > file && cat file",
|
||||
cmd: "echo 'hello world' > " + tmpDir + "/out.txt && cat " + tmpDir + "/out.txt",
|
||||
check: func(r string) bool { return strings.Contains(r, "hello world") },
|
||||
},
|
||||
{
|
||||
name: "grep file | head -2",
|
||||
cmd: "grep a " + tmpDir + "/data.txt | head -2",
|
||||
check: func(r string) bool { return strings.Contains(r, "apple") || strings.Contains(r, "banana") },
|
||||
},
|
||||
{
|
||||
name: "cat | grep | wc -l",
|
||||
cmd: "cat " + tmpDir + "/data.txt | grep -i apple | wc -l",
|
||||
check: func(r string) bool { return strings.TrimSpace(r) == "2" },
|
||||
},
|
||||
{
|
||||
name: "ls | grep txt | head -1",
|
||||
cmd: "ls " + tmpDir + " | grep txt | head -1",
|
||||
check: func(r string) bool { return strings.Contains(r, "data.txt") },
|
||||
},
|
||||
{
|
||||
name: "echo | sed replacement",
|
||||
cmd: "echo 'hello world' | sed 's/world/universe/'",
|
||||
check: func(r string) bool { return strings.Contains(r, "hello universe") },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExecChain(tt.cmd)
|
||||
if !tt.check(result) {
|
||||
t.Errorf("check failed for %q, got %q", tt.cmd, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
42
tui.go
42
tui.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"gf-lt/models"
|
||||
"gf-lt/tools"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
@@ -50,6 +51,7 @@ var (
|
||||
helpPage = "helpPage"
|
||||
renamePage = "renamePage"
|
||||
RAGPage = "RAGPage"
|
||||
dbTablesPage = "dbTables"
|
||||
propsPage = "propsPage"
|
||||
codeBlockPage = "codeBlockPage"
|
||||
imgPage = "imgPage"
|
||||
@@ -68,8 +70,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 +106,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
|
||||
@@ -141,6 +144,13 @@ func setShellMode(enabled bool) {
|
||||
// showToast displays a temporary notification in the bottom-right corner.
|
||||
// It auto-hides after 3 seconds.
|
||||
func showToast(title, message string) {
|
||||
if cfg.UseNotifySend {
|
||||
notifySend(title, message)
|
||||
return
|
||||
}
|
||||
if cfg.CLIMode {
|
||||
return
|
||||
}
|
||||
sanitize := func(s string, maxLen int) string {
|
||||
sanitized := strings.Map(func(r rune) rune {
|
||||
if r < 32 && r != '\t' {
|
||||
@@ -318,6 +328,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 +356,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 +859,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 +958,7 @@ func initTUI() {
|
||||
if event.Key() == tcell.KeyCtrlK {
|
||||
// add message from tools
|
||||
cfg.ToolUse = !cfg.ToolUse
|
||||
updateToolCapabilities()
|
||||
UpdateToolCapabilities()
|
||||
updateStatusLine()
|
||||
return nil
|
||||
}
|
||||
@@ -1035,7 +1064,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 +1156,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]):], " ")
|
||||
@@ -1171,5 +1200,4 @@ func initTUI() {
|
||||
}
|
||||
return event
|
||||
})
|
||||
go updateModelLists()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user