Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix: grep for binding-client (#474)

Maojia Sheng50323dfa6cdc
diff --git a/openviking/pyagfs/binding_client.py b/openviking/pyagfs/binding_client.py
index 3ba5254db..8a6b70cc9 100644
--- a/openviking/pyagfs/binding_client.py
+++ b/openviking/pyagfs/binding_client.py
@@ -188,6 +188,16 @@ class BindingLib:
self.lib.AGFS_GetHandleInfo.argtypes = [ctypes.c_int64]
self.lib.AGFS_GetHandleInfo.restype = ctypes.c_char_p
+ self.lib.AGFS_Grep.argtypes = [
+ ctypes.c_int64, # clientID
+ ctypes.c_char_p, # path
+ ctypes.c_char_p, # pattern
+ ctypes.c_int, # recursive
+ ctypes.c_int, # caseInsensitive
+ ctypes.c_int, # stream
+ ctypes.c_int, # nodeLimit
+ ]
+ self.lib.AGFS_Grep.restype = ctypes.c_char_p
class AGFSBindingClient:
"""Client for interacting with AGFS using Python binding (no HTTP server required).
@@ -387,9 +397,34 @@ class AGFSBindingClient:
recursive: bool = False,
case_insensitive: bool = False,
stream: bool = False,
+ node_limit: Optional[int] = None,
):
- """Search for a pattern in files."""
- raise AGFSNotSupportedError("Grep not supported in binding mode")
+ """Search for a pattern in files.
+
+ Args:
+ path: Path to file or directory to search
+ pattern: Regular expression pattern to search for
+ recursive: Whether to search recursively in directories (default: False)
+ case_insensitive: Whether to perform case-insensitive matching (default: False)
+ stream: Whether to stream results (not supported in binding mode, default: False)
+ node_limit: Maximum number of results to return (default: None)
+
+ Returns:
+ Dict with 'matches' (list of match objects) and 'count'
+ """
+ if stream:
+ raise AGFSNotSupportedError("Streaming not supported in binding mode")
+
+ result = self._lib.lib.AGFS_Grep(
+ self._client_id,
+ path.encode("utf-8"),
+ pattern.encode("utf-8"),
+ 1 if recursive else 0,
+ 1 if case_insensitive else 0,
+ 0, # stream not supported
+ node_limit if node_limit is not None else 0,
+ )
+ return self._parse_response(result)
def digest(self, path: str, algorithm: str = "xxh3") -> Dict[str, Any]:
"""Calculate the digest of a file."""
diff --git a/third_party/agfs/agfs-server/cmd/pybinding/main.go b/third_party/agfs/agfs-server/cmd/pybinding/main.go
index 0f95cfa67..ded736e7a 100644
--- a/third_party/agfs/agfs-server/cmd/pybinding/main.go
+++ b/third_party/agfs/agfs-server/cmd/pybinding/main.go
@@ -8,8 +8,12 @@ package main
import "C"
import (
+ "bufio"
+ "bytes"
"encoding/json"
"fmt"
+ "path"
+ "regexp"
"sync"
"time"
"unsafe"
@@ -32,6 +36,7 @@ import (
"github.com/c4pt0r/agfs/agfs-server/pkg/plugins/sqlfs"
"github.com/c4pt0r/agfs/agfs-server/pkg/plugins/streamfs"
"github.com/c4pt0r/agfs/agfs-server/pkg/plugins/streamrotatefs"
+ log "github.com/sirupsen/logrus"
)
var (
@@ -654,6 +659,137 @@ func AGFS_GetPluginLoader() unsafe.Pointer {
return unsafe.Pointer(l)
}
+// GrepMatch represents a single match result
+type GrepMatch struct {
+ File string `json:"file"`
+ Line int `json:"line"`
+ Content string `json:"content"`
+}
+
+// GrepResponse represents the grep search results
+type GrepResponse struct {
+ Matches []GrepMatch `json:"matches"`
+ Count int `json:"count"`
+}
+
+func grepFile(fs *mountablefs.MountableFS, path string, re *regexp.Regexp, nodeLimit int) ([]GrepMatch, error) {
+ data, err := fs.Read(path, 0, -1)
+ if err != nil && err.Error() != "EOF" {
+ return nil, err
+ }
+
+ var matches []GrepMatch
+ scanner := bufio.NewScanner(bytes.NewReader(data))
+ lineNum := 1
+
+ for scanner.Scan() {
+ if nodeLimit > 0 && len(matches) >= nodeLimit {
+ break
+ }
+ line := scanner.Text()
+ if re.MatchString(line) {
+ matches = append(matches, GrepMatch{
+ File: path,
+ Line: lineNum,
+ Content: line,
+ })
+ }
+ lineNum++
+ }
+
+ if err := scanner.Err(); err != nil {
+ return nil, err
+ }
+
+ return matches, nil
+}
+
+func grepDirectory(fs *mountablefs.MountableFS, dirPath string, re *regexp.Regexp, nodeLimit int) ([]GrepMatch, error) {
+ var allMatches []GrepMatch
+
+ entries, err := fs.ReadDir(dirPath)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, entry := range entries {
+ if nodeLimit > 0 && len(allMatches) >= nodeLimit {
+ break
+ }
+ fullPath := path.Join(dirPath, entry.Name)
+
+ if entry.IsDir {
+ subMatches, err := grepDirectory(fs, fullPath, re, nodeLimit-len(allMatches))
+ if err != nil {
+ log.Warnf("failed to search directory %s: %v", fullPath, err)
+ continue
+ }
+ allMatches = append(allMatches, subMatches...)
+ } else {
+ matches, err := grepFile(fs, fullPath, re, nodeLimit-len(allMatches))
+ if err != nil {
+ log.Warnf("failed to search file %s: %v", fullPath, err)
+ continue
+ }
+ allMatches = append(allMatches, matches...)
+ }
+ }
+
+ return allMatches, nil
+}
+
+//export AGFS_Grep
+func AGFS_Grep(clientID int64, path *C.char, pattern *C.char, recursive C.int, caseInsensitive C.int, stream C.int, nodeLimit C.int) *C.char {
+ p := C.GoString(path)
+ pat := C.GoString(pattern)
+ nodeLim := int(nodeLimit)
+
+ globalFSMu.RLock()
+ defer globalFSMu.RUnlock()
+ fs := globalFS
+
+ info, err := fs.Stat(p)
+ if err != nil {
+ errorID := storeError(err)
+ return C.CString(fmt.Sprintf(`{"error_id": %d}`, errorID))
+ }
+
+ var re *regexp.Regexp
+ if caseInsensitive != 0 {
+ re, err = regexp.Compile("(?i)" + pat)
+ } else {
+ re, err = regexp.Compile(pat)
+ }
+ if err != nil {
+ errorID := storeError(err)
+ return C.CString(fmt.Sprintf(`{"error_id": %d}`, errorID))
+ }
+
+ var matches []GrepMatch
+ if info.IsDir {
+ if recursive == 0 {
+ errorID := storeError(fmt.Errorf("path is a directory, use recursive=true to search"))
+ return C.CString(fmt.Sprintf(`{"error_id": %d}`, errorID))
+ }
+ matches, err = grepDirectory(fs, p, re, nodeLim)
+ } else {
+ matches, err = grepFile(fs, p, re, nodeLim)
+ }
+
+ if err != nil {
+ errorID := storeError(err)
+ return C.CString(fmt.Sprintf(`{"error_id": %d}`, errorID))
+ }
+
+ response := GrepResponse{
+ Matches: matches,
+ Count: len(matches),
+ }
+
+ data, _ := json.Marshal(response)
+ return C.CString(string(data))
+}
+
func GetMountableFS() *mountablefs.MountableFS {
globalFSMu.RLock()
defer globalFSMu.RUnlock()