Chore: return blank lines between funcs
This commit is contained in:
44
tools.go
44
tools.go
@@ -17,6 +17,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gf-lt/rag"
|
"gf-lt/rag"
|
||||||
|
|
||||||
"github.com/GrailFinder/searchagent/searcher"
|
"github.com/GrailFinder/searchagent/searcher"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -176,6 +177,7 @@ func init() {
|
|||||||
logger.Warn("failed to init rag; rag_search tool will not be available", "error", err)
|
logger.Warn("failed to init rag; rag_search tool will not be available", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getWebAgentClient returns a singleton AgentClient for web agents.
|
// getWebAgentClient returns a singleton AgentClient for web agents.
|
||||||
func getWebAgentClient() *agent.AgentClient {
|
func getWebAgentClient() *agent.AgentClient {
|
||||||
webAgentClientOnce.Do(func() {
|
webAgentClientOnce.Do(func() {
|
||||||
@@ -195,6 +197,7 @@ func getWebAgentClient() *agent.AgentClient {
|
|||||||
})
|
})
|
||||||
return webAgentClient
|
return webAgentClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// registerWebAgents registers WebAgentB instances for websearch and read_url tools.
|
// registerWebAgents registers WebAgentB instances for websearch and read_url tools.
|
||||||
func registerWebAgents() {
|
func registerWebAgents() {
|
||||||
webAgentsOnce.Do(func() {
|
webAgentsOnce.Do(func() {
|
||||||
@@ -209,6 +212,7 @@ func registerWebAgents() {
|
|||||||
agent.Register("summarize_chat", agent.NewWebAgentB(client, summarySysPrompt))
|
agent.Register("summarize_chat", agent.NewWebAgentB(client, summarySysPrompt))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// web search (depends on extra server)
|
// web search (depends on extra server)
|
||||||
func websearch(args map[string]string) []byte {
|
func websearch(args map[string]string) []byte {
|
||||||
// make http request return bytes
|
// make http request return bytes
|
||||||
@@ -242,6 +246,7 @@ func websearch(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// rag search (searches local document database)
|
// rag search (searches local document database)
|
||||||
func ragsearch(args map[string]string) []byte {
|
func ragsearch(args map[string]string) []byte {
|
||||||
query, ok := args["query"]
|
query, ok := args["query"]
|
||||||
@@ -280,6 +285,7 @@ func ragsearch(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// web search raw (returns raw data without processing)
|
// web search raw (returns raw data without processing)
|
||||||
func websearchRaw(args map[string]string) []byte {
|
func websearchRaw(args map[string]string) []byte {
|
||||||
// make http request return bytes
|
// make http request return bytes
|
||||||
@@ -308,6 +314,7 @@ func websearchRaw(args map[string]string) []byte {
|
|||||||
// Return raw response without any processing
|
// Return raw response without any processing
|
||||||
return []byte(fmt.Sprintf("%+v", resp))
|
return []byte(fmt.Sprintf("%+v", resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieves url content (text)
|
// retrieves url content (text)
|
||||||
func readURL(args map[string]string) []byte {
|
func readURL(args map[string]string) []byte {
|
||||||
// make http request return bytes
|
// make http request return bytes
|
||||||
@@ -331,6 +338,7 @@ func readURL(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieves url content raw (returns raw content without processing)
|
// retrieves url content raw (returns raw content without processing)
|
||||||
func readURLRaw(args map[string]string) []byte {
|
func readURLRaw(args map[string]string) []byte {
|
||||||
// make http request return bytes
|
// make http request return bytes
|
||||||
@@ -349,6 +357,7 @@ func readURLRaw(args map[string]string) []byte {
|
|||||||
// Return raw response without any processing
|
// Return raw response without any processing
|
||||||
return []byte(fmt.Sprintf("%+v", resp))
|
return []byte(fmt.Sprintf("%+v", resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
consider cases:
|
consider cases:
|
||||||
- append mode (treat it like a journal appendix)
|
- append mode (treat it like a journal appendix)
|
||||||
@@ -378,6 +387,7 @@ func memorise(args map[string]string) []byte {
|
|||||||
msg := "info saved under the topic:" + args["topic"]
|
msg := "info saved under the topic:" + args["topic"]
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func recall(args map[string]string) []byte {
|
func recall(args map[string]string) []byte {
|
||||||
agent := cfg.AssistantRole
|
agent := cfg.AssistantRole
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
@@ -393,6 +403,7 @@ func recall(args map[string]string) []byte {
|
|||||||
answer := fmt.Sprintf("under the topic: %s is stored:\n%s", args["topic"], mind)
|
answer := fmt.Sprintf("under the topic: %s is stored:\n%s", args["topic"], mind)
|
||||||
return []byte(answer)
|
return []byte(answer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func recallTopics(args map[string]string) []byte {
|
func recallTopics(args map[string]string) []byte {
|
||||||
agent := cfg.AssistantRole
|
agent := cfg.AssistantRole
|
||||||
topics, err := store.RecallTopics(agent)
|
topics, err := store.RecallTopics(agent)
|
||||||
@@ -403,6 +414,7 @@ func recallTopics(args map[string]string) []byte {
|
|||||||
joinedS := strings.Join(topics, ";")
|
joinedS := strings.Join(topics, ";")
|
||||||
return []byte(joinedS)
|
return []byte(joinedS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// File Manipulation Tools
|
// File Manipulation Tools
|
||||||
func fileCreate(args map[string]string) []byte {
|
func fileCreate(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
@@ -424,6 +436,7 @@ func fileCreate(args map[string]string) []byte {
|
|||||||
msg := "file created successfully at " + path
|
msg := "file created successfully at " + path
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileRead(args map[string]string) []byte {
|
func fileRead(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
if !ok || path == "" {
|
if !ok || path == "" {
|
||||||
@@ -450,6 +463,7 @@ func fileRead(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileWrite(args map[string]string) []byte {
|
func fileWrite(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
if !ok || path == "" {
|
if !ok || path == "" {
|
||||||
@@ -470,6 +484,7 @@ func fileWrite(args map[string]string) []byte {
|
|||||||
msg := "file written successfully at " + path
|
msg := "file written successfully at " + path
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileWriteAppend(args map[string]string) []byte {
|
func fileWriteAppend(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
if !ok || path == "" {
|
if !ok || path == "" {
|
||||||
@@ -490,6 +505,7 @@ func fileWriteAppend(args map[string]string) []byte {
|
|||||||
msg := "file written successfully at " + path
|
msg := "file written successfully at " + path
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileDelete(args map[string]string) []byte {
|
func fileDelete(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
if !ok || path == "" {
|
if !ok || path == "" {
|
||||||
@@ -506,6 +522,7 @@ func fileDelete(args map[string]string) []byte {
|
|||||||
msg := "file deleted successfully at " + path
|
msg := "file deleted successfully at " + path
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileMove(args map[string]string) []byte {
|
func fileMove(args map[string]string) []byte {
|
||||||
src, ok := args["src"]
|
src, ok := args["src"]
|
||||||
if !ok || src == "" {
|
if !ok || src == "" {
|
||||||
@@ -529,6 +546,7 @@ func fileMove(args map[string]string) []byte {
|
|||||||
msg := fmt.Sprintf("file moved successfully from %s to %s", src, dst)
|
msg := fmt.Sprintf("file moved successfully from %s to %s", src, dst)
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileCopy(args map[string]string) []byte {
|
func fileCopy(args map[string]string) []byte {
|
||||||
src, ok := args["src"]
|
src, ok := args["src"]
|
||||||
if !ok || src == "" {
|
if !ok || src == "" {
|
||||||
@@ -552,6 +570,7 @@ func fileCopy(args map[string]string) []byte {
|
|||||||
msg := fmt.Sprintf("file copied successfully from %s to %s", src, dst)
|
msg := fmt.Sprintf("file copied successfully from %s to %s", src, dst)
|
||||||
return []byte(msg)
|
return []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileList(args map[string]string) []byte {
|
func fileList(args map[string]string) []byte {
|
||||||
path, ok := args["path"]
|
path, ok := args["path"]
|
||||||
if !ok || path == "" {
|
if !ok || path == "" {
|
||||||
@@ -576,6 +595,7 @@ func fileList(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper functions for file operations
|
// Helper functions for file operations
|
||||||
func resolvePath(p string) string {
|
func resolvePath(p string) string {
|
||||||
if filepath.IsAbs(p) {
|
if filepath.IsAbs(p) {
|
||||||
@@ -583,6 +603,7 @@ func resolvePath(p string) string {
|
|||||||
}
|
}
|
||||||
return filepath.Join(cfg.FilePickerDir, p)
|
return filepath.Join(cfg.FilePickerDir, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStringFromFile(filename string) (string, error) {
|
func readStringFromFile(filename string) (string, error) {
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -590,9 +611,11 @@ func readStringFromFile(filename string) (string, error) {
|
|||||||
}
|
}
|
||||||
return string(data), nil
|
return string(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeStringToFile(filename string, data string) error {
|
func writeStringToFile(filename string, data string) error {
|
||||||
return os.WriteFile(filename, []byte(data), 0644)
|
return os.WriteFile(filename, []byte(data), 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendStringToFile(filename string, data string) error {
|
func appendStringToFile(filename string, data string) error {
|
||||||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -602,9 +625,11 @@ func appendStringToFile(filename string, data string) error {
|
|||||||
_, err = file.WriteString(data)
|
_, err = file.WriteString(data)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeFile(filename string) error {
|
func removeFile(filename string) error {
|
||||||
return os.Remove(filename)
|
return os.Remove(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
func moveFile(src, dst string) error {
|
func moveFile(src, dst string) error {
|
||||||
// First try with os.Rename (works within same filesystem)
|
// First try with os.Rename (works within same filesystem)
|
||||||
if err := os.Rename(src, dst); err == nil {
|
if err := os.Rename(src, dst); err == nil {
|
||||||
@@ -613,6 +638,7 @@ func moveFile(src, dst string) error {
|
|||||||
// If that fails (e.g., cross-filesystem), copy and delete
|
// If that fails (e.g., cross-filesystem), copy and delete
|
||||||
return copyAndRemove(src, dst)
|
return copyAndRemove(src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyFile(src, dst string) error {
|
func copyFile(src, dst string) error {
|
||||||
srcFile, err := os.Open(src)
|
srcFile, err := os.Open(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -627,6 +653,7 @@ func copyFile(src, dst string) error {
|
|||||||
_, err = io.Copy(dstFile, srcFile)
|
_, err = io.Copy(dstFile, srcFile)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyAndRemove(src, dst string) error {
|
func copyAndRemove(src, dst string) error {
|
||||||
// Copy the file
|
// Copy the file
|
||||||
if err := copyFile(src, dst); err != nil {
|
if err := copyFile(src, dst); err != nil {
|
||||||
@@ -635,6 +662,7 @@ func copyAndRemove(src, dst string) error {
|
|||||||
// Remove the source file
|
// Remove the source file
|
||||||
return os.Remove(src)
|
return os.Remove(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listDirectory(path string) ([]string, error) {
|
func listDirectory(path string) ([]string, error) {
|
||||||
entries, err := os.ReadDir(path)
|
entries, err := os.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -650,6 +678,7 @@ func listDirectory(path string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command Execution Tool
|
// Command Execution Tool
|
||||||
func executeCommand(args map[string]string) []byte {
|
func executeCommand(args map[string]string) []byte {
|
||||||
command, ok := args["command"]
|
command, ok := args["command"]
|
||||||
@@ -698,6 +727,7 @@ func executeCommand(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper functions for command execution
|
// Helper functions for command execution
|
||||||
// Todo structure
|
// Todo structure
|
||||||
type TodoItem struct {
|
type TodoItem struct {
|
||||||
@@ -708,10 +738,12 @@ type TodoItem struct {
|
|||||||
type TodoList struct {
|
type TodoList struct {
|
||||||
Items []TodoItem `json:"items"`
|
Items []TodoItem `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global todo list storage
|
// Global todo list storage
|
||||||
var globalTodoList = TodoList{
|
var globalTodoList = TodoList{
|
||||||
Items: []TodoItem{},
|
Items: []TodoItem{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo Management Tools
|
// Todo Management Tools
|
||||||
func todoCreate(args map[string]string) []byte {
|
func todoCreate(args map[string]string) []byte {
|
||||||
task, ok := args["task"]
|
task, ok := args["task"]
|
||||||
@@ -742,6 +774,7 @@ func todoCreate(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func todoRead(args map[string]string) []byte {
|
func todoRead(args map[string]string) []byte {
|
||||||
id, ok := args["id"]
|
id, ok := args["id"]
|
||||||
if ok && id != "" {
|
if ok && id != "" {
|
||||||
@@ -784,6 +817,7 @@ func todoRead(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func todoUpdate(args map[string]string) []byte {
|
func todoUpdate(args map[string]string) []byte {
|
||||||
id, ok := args["id"]
|
id, ok := args["id"]
|
||||||
if !ok || id == "" {
|
if !ok || id == "" {
|
||||||
@@ -846,6 +880,7 @@ func todoUpdate(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func todoDelete(args map[string]string) []byte {
|
func todoDelete(args map[string]string) []byte {
|
||||||
id, ok := args["id"]
|
id, ok := args["id"]
|
||||||
if !ok || id == "" {
|
if !ok || id == "" {
|
||||||
@@ -883,6 +918,7 @@ func todoDelete(args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return jsonResult
|
return jsonResult
|
||||||
}
|
}
|
||||||
|
|
||||||
var gitReadSubcommands = map[string]bool{
|
var gitReadSubcommands = map[string]bool{
|
||||||
"status": true,
|
"status": true,
|
||||||
"log": true,
|
"log": true,
|
||||||
@@ -894,6 +930,7 @@ var gitReadSubcommands = map[string]bool{
|
|||||||
"shortlog": true,
|
"shortlog": true,
|
||||||
"describe": true,
|
"describe": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
func isCommandAllowed(command string, args ...string) bool {
|
func isCommandAllowed(command string, args ...string) bool {
|
||||||
allowedCommands := map[string]bool{
|
allowedCommands := map[string]bool{
|
||||||
"grep": true,
|
"grep": true,
|
||||||
@@ -934,6 +971,7 @@ func isCommandAllowed(command string, args ...string) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func summarizeChat(args map[string]string) []byte {
|
func summarizeChat(args map[string]string) []byte {
|
||||||
if len(chatBody.Messages) == 0 {
|
if len(chatBody.Messages) == 0 {
|
||||||
return []byte("No chat history to summarize.")
|
return []byte("No chat history to summarize.")
|
||||||
@@ -942,7 +980,9 @@ func summarizeChat(args map[string]string) []byte {
|
|||||||
chatText := chatToText(chatBody.Messages, true) // include system and tool messages
|
chatText := chatToText(chatBody.Messages, true) // include system and tool messages
|
||||||
return []byte(chatText)
|
return []byte(chatText)
|
||||||
}
|
}
|
||||||
|
|
||||||
type fnSig func(map[string]string) []byte
|
type fnSig func(map[string]string) []byte
|
||||||
|
|
||||||
var fnMap = map[string]fnSig{
|
var fnMap = map[string]fnSig{
|
||||||
"recall": recall,
|
"recall": recall,
|
||||||
"recall_topics": recallTopics,
|
"recall_topics": recallTopics,
|
||||||
@@ -967,6 +1007,7 @@ var fnMap = map[string]fnSig{
|
|||||||
"todo_delete": todoDelete,
|
"todo_delete": todoDelete,
|
||||||
"summarize_chat": summarizeChat,
|
"summarize_chat": summarizeChat,
|
||||||
}
|
}
|
||||||
|
|
||||||
// callToolWithAgent calls the tool and applies any registered agent.
|
// callToolWithAgent calls the tool and applies any registered agent.
|
||||||
func callToolWithAgent(name string, args map[string]string) []byte {
|
func callToolWithAgent(name string, args map[string]string) []byte {
|
||||||
registerWebAgents()
|
registerWebAgents()
|
||||||
@@ -980,6 +1021,7 @@ func callToolWithAgent(name string, args map[string]string) []byte {
|
|||||||
}
|
}
|
||||||
return raw
|
return raw
|
||||||
}
|
}
|
||||||
|
|
||||||
// openai style def
|
// openai style def
|
||||||
var baseTools = []models.Tool{
|
var baseTools = []models.Tool{
|
||||||
// rag_search
|
// rag_search
|
||||||
@@ -1403,4 +1445,4 @@ var baseTools = []models.Tool{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user