Chore: linter complaints
This commit is contained in:
@@ -57,15 +57,12 @@ 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 {
|
||||
@@ -74,19 +71,17 @@ func (a *PWAgent) setToolCallOnLastMessage(resp []byte, toolCallID string) {
|
||||
if fn, ok := tc["function"].(map[string]interface{}); ok {
|
||||
name, _ = fn["name"].(string)
|
||||
argsStr, _ := fn["arguments"].(string)
|
||||
json.Unmarshal([]byte(argsStr), &args)
|
||||
_ = 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
|
||||
@@ -110,14 +105,12 @@ func extractToolNameFromText(text string) string {
|
||||
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 ""
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gf-lt/models"
|
||||
@@ -252,7 +253,6 @@ func findToolCall(resp []byte) (func() []byte, string, bool) {
|
||||
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 {
|
||||
@@ -268,11 +268,9 @@ func findToolCall(resp []byte) (func() []byte, string, bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if content, ok := genericResp["content"].(string); ok {
|
||||
return findToolCallFromText(content)
|
||||
}
|
||||
|
||||
return findToolCallFromText(string(resp))
|
||||
}
|
||||
|
||||
@@ -280,20 +278,17 @@ 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 {
|
||||
@@ -308,12 +303,10 @@ func findToolCallFromText(text string) (func() []byte, string, bool) {
|
||||
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 {
|
||||
@@ -321,20 +314,16 @@ func findToolCallFromText(text string) (func() []byte, string, bool) {
|
||||
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 {
|
||||
@@ -345,5 +334,5 @@ func findToolCallFromText(text string) (func() []byte, string, bool) {
|
||||
}
|
||||
|
||||
func generateToolCallID() string {
|
||||
return fmt.Sprintf("%d", len(pwToolMap)%10000)
|
||||
return strconv.Itoa(len(pwToolMap) % 10000)
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ func (d dummyStore) ChatGetMaxID() (uint32, error) { ret
|
||||
func (d dummyStore) Memorise(m *models.Memory) (*models.Memory, error) { return m, nil }
|
||||
func (d dummyStore) Recall(agent, topic string) (string, error) { return "", nil }
|
||||
func (d dummyStore) RecallTopics(agent string) ([]string, error) { return nil, nil }
|
||||
func (d dummyStore) Forget(agent, topic string) error { return nil }
|
||||
|
||||
// VectorRepo methods (not used but required by interface)
|
||||
func (d dummyStore) WriteVector(row *models.VectorRow) error { return nil }
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -143,7 +145,7 @@ func ExecChain(command string) string {
|
||||
func execSingle(command, stdin string) (string, error) {
|
||||
parts := tokenize(command)
|
||||
if len(parts) == 0 {
|
||||
return "", fmt.Errorf("empty command")
|
||||
return "", errors.New("empty command")
|
||||
}
|
||||
name := parts[0]
|
||||
args := parts[1:]
|
||||
@@ -243,10 +245,10 @@ func execBuiltin(name string, args []string, stdin string) string {
|
||||
return fmt.Sprintf("[error] cd: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Sprintf("[error] cd: not a directory: %s", dir)
|
||||
return "[error] cd: not a directory: " + dir
|
||||
}
|
||||
cfg.FilePickerDir = abs
|
||||
return fmt.Sprintf("Changed directory to: %s", cfg.FilePickerDir)
|
||||
return "Changed directory to: " + cfg.FilePickerDir
|
||||
case "mkdir":
|
||||
if len(args) == 0 {
|
||||
return "[error] usage: mkdir [-p] <dir>"
|
||||
@@ -278,9 +280,9 @@ func execBuiltin(name string, args []string, stdin string) string {
|
||||
return fmt.Sprintf("[error] mkdir: %v", err)
|
||||
}
|
||||
if createParents {
|
||||
return fmt.Sprintf("Created %s (with parents)", dirPath)
|
||||
return "Created " + dirPath + " (with parents)"
|
||||
}
|
||||
return fmt.Sprintf("Created %s", dirPath)
|
||||
return "Created " + dirPath
|
||||
case "ls":
|
||||
dir := "."
|
||||
for _, a := range args {
|
||||
@@ -300,16 +302,17 @@ func execBuiltin(name string, args []string, stdin string) string {
|
||||
var out strings.Builder
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
if e.IsDir() {
|
||||
switch {
|
||||
case e.IsDir():
|
||||
fmt.Fprintf(&out, "d %-8s %s/\n", "-", e.Name())
|
||||
} else if info != nil {
|
||||
case info != nil:
|
||||
size := info.Size()
|
||||
sizeStr := fmt.Sprintf("%d", size)
|
||||
sizeStr := strconv.FormatInt(size, 10)
|
||||
if size > 1024 {
|
||||
sizeStr = fmt.Sprintf("%.1fKB", float64(size)/1024)
|
||||
}
|
||||
fmt.Fprintf(&out, "f %-8s %s\n", sizeStr, e.Name())
|
||||
} else {
|
||||
default:
|
||||
fmt.Fprintf(&out, "f %-8s %s\n", "?", e.Name())
|
||||
}
|
||||
}
|
||||
|
||||
78
tools/fs.go
78
tools/fs.go
@@ -3,6 +3,7 @@ package tools
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gf-lt/models"
|
||||
"os"
|
||||
@@ -30,6 +31,9 @@ func SetMemoryStore(store MemoryStore, role string) {
|
||||
}
|
||||
|
||||
func SetFSRoot(dir string) {
|
||||
if cfg == nil {
|
||||
return
|
||||
}
|
||||
cfg.FilePickerDir = dir
|
||||
}
|
||||
|
||||
@@ -55,7 +59,7 @@ func SetFSCwd(dir string) error {
|
||||
|
||||
func resolvePath(rel string) (string, error) {
|
||||
if cfg.FilePickerDir == "" {
|
||||
return "", fmt.Errorf("fs root not set")
|
||||
return "", errors.New("fs root not set")
|
||||
}
|
||||
if filepath.IsAbs(rel) {
|
||||
abs := filepath.Clean(rel)
|
||||
@@ -104,11 +108,12 @@ func FsLs(args []string, stdin string) string {
|
||||
var out strings.Builder
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
if e.IsDir() {
|
||||
switch {
|
||||
case e.IsDir():
|
||||
fmt.Fprintf(&out, "d %-8s %s/\n", "-", e.Name())
|
||||
} else if info != nil {
|
||||
case info != nil:
|
||||
fmt.Fprintf(&out, "f %-8s %s\n", humanSize(info.Size()), e.Name())
|
||||
} else {
|
||||
default:
|
||||
fmt.Fprintf(&out, "f %-8s %s\n", "?", e.Name())
|
||||
}
|
||||
}
|
||||
@@ -198,12 +203,15 @@ func FsWrite(args []string, stdin string) string {
|
||||
var path string
|
||||
var contentParts []string
|
||||
for _, a := range args {
|
||||
if a == "-b" || a == "--base64" {
|
||||
switch a {
|
||||
case "-b", "--base64":
|
||||
b64 = true
|
||||
} else if path == "" {
|
||||
path = a
|
||||
} else {
|
||||
contentParts = append(contentParts, a)
|
||||
default:
|
||||
if path == "" {
|
||||
path = a
|
||||
} else {
|
||||
contentParts = append(contentParts, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
if path == "" {
|
||||
@@ -296,7 +304,7 @@ func FsRm(args []string, stdin string) string {
|
||||
if err := os.RemoveAll(abs); err != nil {
|
||||
return fmt.Sprintf("[error] rm: %v", err)
|
||||
}
|
||||
return fmt.Sprintf("Removed %s", args[0])
|
||||
return "Removed " + args[0]
|
||||
}
|
||||
|
||||
func FsCp(args []string, stdin string) string {
|
||||
@@ -375,9 +383,9 @@ func FsMkdir(args []string, stdin string) string {
|
||||
return fmt.Sprintf("[error] mkdir: %v", err)
|
||||
}
|
||||
if createParents {
|
||||
return fmt.Sprintf("Created %s (with parents)", dirPath)
|
||||
return "Created " + dirPath + " (with parents)"
|
||||
}
|
||||
return fmt.Sprintf("Created %s", dirPath)
|
||||
return "Created " + dirPath
|
||||
}
|
||||
|
||||
// Text processing commands
|
||||
@@ -435,7 +443,7 @@ func FsGrep(args []string, stdin string) string {
|
||||
}
|
||||
}
|
||||
if countOnly {
|
||||
return fmt.Sprintf("%d", len(matched))
|
||||
return strconv.Itoa(len(matched))
|
||||
}
|
||||
return strings.Join(matched, "\n")
|
||||
}
|
||||
@@ -487,11 +495,11 @@ func FsWc(args []string, stdin string) string {
|
||||
if len(args) > 0 {
|
||||
switch args[0] {
|
||||
case "-l":
|
||||
return fmt.Sprintf("%d", lines)
|
||||
return strconv.Itoa(lines)
|
||||
case "-w":
|
||||
return fmt.Sprintf("%d", words)
|
||||
return strconv.Itoa(words)
|
||||
case "-c":
|
||||
return fmt.Sprintf("%d", chars)
|
||||
return strconv.Itoa(chars)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%d lines, %d words, %d chars", lines, words, chars)
|
||||
@@ -502,9 +510,10 @@ func FsSort(args []string, stdin string) string {
|
||||
reverse := false
|
||||
numeric := false
|
||||
for _, a := range args {
|
||||
if a == "-r" {
|
||||
switch a {
|
||||
case "-r":
|
||||
reverse = true
|
||||
} else if a == "-n" {
|
||||
case "-n":
|
||||
numeric = true
|
||||
}
|
||||
}
|
||||
@@ -615,10 +624,10 @@ func FsCd(args []string, stdin string) string {
|
||||
return fmt.Sprintf("[error] cd: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Sprintf("[error] cd: not a directory: %s", dir)
|
||||
return "[error] cd: not a directory: " + dir
|
||||
}
|
||||
cfg.FilePickerDir = abs
|
||||
return fmt.Sprintf("Changed directory to: %s", cfg.FilePickerDir)
|
||||
return "Changed directory to: " + cfg.FilePickerDir
|
||||
}
|
||||
|
||||
func FsSed(args []string, stdin string) string {
|
||||
@@ -629,13 +638,15 @@ func FsSed(args []string, stdin string) string {
|
||||
var filePath string
|
||||
var pattern string
|
||||
for _, a := range args {
|
||||
if a == "-i" || a == "--in-place" {
|
||||
switch a {
|
||||
case "-i", "--in-place":
|
||||
inPlace = true
|
||||
} else if strings.HasPrefix(a, "s") && len(a) > 1 {
|
||||
// This looks like a sed pattern
|
||||
pattern = a
|
||||
} else if filePath == "" && !strings.HasPrefix(a, "-") {
|
||||
filePath = a
|
||||
default:
|
||||
if strings.HasPrefix(a, "s") && len(a) > 1 {
|
||||
pattern = a
|
||||
} else if filePath == "" && !strings.HasPrefix(a, "-") {
|
||||
filePath = a
|
||||
}
|
||||
}
|
||||
}
|
||||
if pattern == "" {
|
||||
@@ -650,8 +661,8 @@ func FsSed(args []string, stdin string) string {
|
||||
newStr := parts[1]
|
||||
global := len(parts) >= 3 && strings.Contains(parts[2], "g")
|
||||
var content string
|
||||
if filePath != "" && stdin == "" {
|
||||
// Read from file
|
||||
switch {
|
||||
case filePath != "" && stdin == "":
|
||||
abs, err := resolvePath(filePath)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("[error] sed: %v", err)
|
||||
@@ -661,10 +672,9 @@ func FsSed(args []string, stdin string) string {
|
||||
return fmt.Sprintf("[error] sed: %v", err)
|
||||
}
|
||||
content = string(data)
|
||||
} else if stdin != "" {
|
||||
// Use stdin
|
||||
case stdin != "":
|
||||
content = stdin
|
||||
} else {
|
||||
default:
|
||||
return "[error] sed: no input (use file path or pipe from stdin)"
|
||||
}
|
||||
// Apply sed replacement
|
||||
@@ -681,7 +691,7 @@ func FsSed(args []string, stdin string) string {
|
||||
if err := os.WriteFile(abs, []byte(content), 0644); err != nil {
|
||||
return fmt.Sprintf("[error] sed: %v", err)
|
||||
}
|
||||
return fmt.Sprintf("Modified %s", filePath)
|
||||
return "Modified " + filePath
|
||||
}
|
||||
return content
|
||||
}
|
||||
@@ -709,7 +719,7 @@ func FsMemory(args []string, stdin string) string {
|
||||
if err != nil {
|
||||
return fmt.Sprintf("[error] failed to store: %v", err)
|
||||
}
|
||||
return fmt.Sprintf("Stored under topic: %s", topic)
|
||||
return "Stored under topic: " + topic
|
||||
case "get":
|
||||
if len(args) < 2 {
|
||||
return "[error] usage: memory get <topic>"
|
||||
@@ -738,7 +748,7 @@ func FsMemory(args []string, stdin string) string {
|
||||
if err != nil {
|
||||
return fmt.Sprintf("[error] failed to forget: %v", err)
|
||||
}
|
||||
return fmt.Sprintf("Deleted topic: %s", topic)
|
||||
return "Deleted topic: " + topic
|
||||
default:
|
||||
return fmt.Sprintf("[error] unknown subcommand: %s. Use: store, get, list, topics, forget, delete", args[0])
|
||||
}
|
||||
|
||||
@@ -386,8 +386,6 @@ func pwDragBySelector(args map[string]string) []byte {
|
||||
if !browserStarted || page == nil {
|
||||
return []byte(`{"error": "Browser not started. Call pw_start first."}`)
|
||||
}
|
||||
|
||||
// Get center coordinates of both elements using JavaScript
|
||||
fromJS := fmt.Sprintf(`
|
||||
function getCenter(selector) {
|
||||
const el = document.querySelector(selector);
|
||||
@@ -406,7 +404,6 @@ func pwDragBySelector(args map[string]string) []byte {
|
||||
}
|
||||
getCenter(%q)
|
||||
`, toSelector)
|
||||
|
||||
fromResult, err := page.Evaluate(fromJS)
|
||||
if err != nil {
|
||||
return []byte(fmt.Sprintf(`{"error": "failed to get from element: %s"}`, err.Error()))
|
||||
@@ -417,7 +414,6 @@ func pwDragBySelector(args map[string]string) []byte {
|
||||
}
|
||||
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()))
|
||||
@@ -428,8 +424,6 @@ func pwDragBySelector(args map[string]string) []byte {
|
||||
}
|
||||
toX := toMap["x"].(float64)
|
||||
toY := toMap["y"].(float64)
|
||||
|
||||
// Perform the drag using coordinates
|
||||
mouse := page.Mouse()
|
||||
err = mouse.Move(fromX, fromY)
|
||||
if err != nil {
|
||||
|
||||
@@ -135,8 +135,8 @@ func (t *Tools) initAgentsB() {
|
||||
}
|
||||
|
||||
func InitTools(cfg *config.Config, logger *slog.Logger, store storage.FullRepo) *Tools {
|
||||
logger = logger
|
||||
cfg = cfg
|
||||
_ = logger
|
||||
_ = cfg
|
||||
if cfg.PlaywrightEnabled {
|
||||
if err := CheckPlaywright(); err != nil {
|
||||
// slow, need a faster check if playwright install
|
||||
@@ -392,8 +392,7 @@ func runCmd(args map[string]string) []byte {
|
||||
// memory store <topic> <data> | memory get <topic> | memory list | memory forget <topic>
|
||||
return []byte(FsMemory(append([]string{"store"}, rest...), ""))
|
||||
case "todo":
|
||||
// todo create|read|update|delete - route to existing todo handlers
|
||||
return []byte(handleTodoSubcommand(rest, args))
|
||||
return handleTodoSubcommand(rest, args)
|
||||
case "window", "windows":
|
||||
// window list - list all windows
|
||||
return listWindows(args)
|
||||
@@ -545,7 +544,7 @@ Actions:
|
||||
"toSelector": rest[1],
|
||||
})
|
||||
default:
|
||||
return []byte(fmt.Sprintf("unknown browser action: %s", action))
|
||||
return []byte("unknown browser action: " + action)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -816,7 +815,7 @@ func handleTodoSubcommand(args []string, originalArgs map[string]string) []byte
|
||||
}
|
||||
return todoDelete(map[string]string{"id": args[1]})
|
||||
default:
|
||||
return []byte(fmt.Sprintf("unknown todo subcommand: %s", subcmd))
|
||||
return []byte("unknown todo subcommand: " + subcmd)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1296,7 +1295,7 @@ func summarizeChat(args map[string]string) []byte {
|
||||
if err != nil {
|
||||
return []byte("error: failed to marshal arguments")
|
||||
}
|
||||
return []byte(data)
|
||||
return data
|
||||
}
|
||||
|
||||
// func removePlaywrightToolsFromBaseTools() {
|
||||
|
||||
Reference in New Issue
Block a user