|
| 1 | +/* |
| 2 | + * Copyright 2025 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Package filesystem provides file system operations. |
| 18 | +package filesystem |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + |
| 23 | + "github.com/cloudwego/eino/schema" |
| 24 | +) |
| 25 | + |
| 26 | +// FileInfo represents basic file metadata information. |
| 27 | +type FileInfo struct { |
| 28 | + // Path is the absolute path of the file or directory. |
| 29 | + Path string |
| 30 | +} |
| 31 | + |
| 32 | +// GrepMatch represents a single pattern match result. |
| 33 | +type GrepMatch struct { |
| 34 | + // Path is the absolute path of the file where the match occurred. |
| 35 | + Path string |
| 36 | + // Line is the 1-based line number of the match. |
| 37 | + Line int |
| 38 | + // Content is the full text content of the line containing the match. |
| 39 | + Content string |
| 40 | +} |
| 41 | + |
| 42 | +// LsInfoRequest contains parameters for listing file information. |
| 43 | +type LsInfoRequest struct { |
| 44 | + // Path specifies the absolute directory path to list. |
| 45 | + // It must be an absolute path starting with '/'. |
| 46 | + // An empty string is treated as the root directory ("/"). |
| 47 | + Path string |
| 48 | +} |
| 49 | + |
| 50 | +// ReadRequest contains parameters for reading file content. |
| 51 | +type ReadRequest struct { |
| 52 | + // FilePath is the absolute path to the file to be read. Must start with '/'. |
| 53 | + FilePath string |
| 54 | + |
| 55 | + // Offset is the 0-based line number to start reading from. |
| 56 | + // If negative, it is treated as 0. Defaults to 0. |
| 57 | + Offset int |
| 58 | + |
| 59 | + // Limit specifies the maximum number of lines to read. |
| 60 | + // If non-positive (<= 0), a default limit is used (typically 200). |
| 61 | + Limit int |
| 62 | +} |
| 63 | + |
| 64 | +// GrepRequest contains parameters for searching file content. |
| 65 | +type GrepRequest struct { |
| 66 | + // Pattern is the literal string to search for. This is not a regular expression. |
| 67 | + // The search performs an exact substring match within the file's content. |
| 68 | + // For example, "TODO" will match any line containing "TODO". |
| 69 | + Pattern string |
| 70 | + |
| 71 | + // Path is an optional directory path to limit the search scope. |
| 72 | + // If empty, the search is performed from the working directory. |
| 73 | + Path string |
| 74 | + |
| 75 | + // Glob is an optional pattern to filter the files to be searched. |
| 76 | + // It filters by file path, not content. If empty, no files are filtered. |
| 77 | + // Supports standard glob wildcards: |
| 78 | + // - `*` matches any characters except path separators. |
| 79 | + // - `**` matches any directories recursively. |
| 80 | + // - `?` matches a single character. |
| 81 | + // - `[abc]` matches one character from the set. |
| 82 | + Glob string |
| 83 | +} |
| 84 | + |
| 85 | +// GlobInfoRequest contains parameters for glob pattern matching. |
| 86 | +type GlobInfoRequest struct { |
| 87 | + // Pattern is the glob expression used to match file paths. |
| 88 | + // It supports standard glob syntax: |
| 89 | + // - `*` matches any characters except path separators. |
| 90 | + // - `**` matches any directories recursively. |
| 91 | + // - `?` matches a single character. |
| 92 | + // - `[abc]` matches one character from the set. |
| 93 | + Pattern string |
| 94 | + |
| 95 | + // Path is the base directory from which to start the search. |
| 96 | + // The glob pattern is applied relative to this path. Defaults to the root directory ("/"). |
| 97 | + Path string |
| 98 | +} |
| 99 | + |
| 100 | +// WriteRequest contains parameters for writing file content. |
| 101 | +type WriteRequest struct { |
| 102 | + // FilePath is the absolute path of the file to write. Must start with '/'. |
| 103 | + // The file will be created if it does not exist, or error if file exists. |
| 104 | + FilePath string |
| 105 | + |
| 106 | + // Content is the data to be written to the file. |
| 107 | + Content string |
| 108 | +} |
| 109 | + |
| 110 | +// EditRequest contains parameters for editing file content. |
| 111 | +type EditRequest struct { |
| 112 | + // FilePath is the absolute path of the file to edit. Must start with '/'. |
| 113 | + FilePath string |
| 114 | + |
| 115 | + // OldString is the exact string to be replaced. It must be non-empty and will be matched literally, including whitespace. |
| 116 | + OldString string |
| 117 | + |
| 118 | + // NewString is the string that will replace OldString. |
| 119 | + // It must be different from OldString. |
| 120 | + // An empty string can be used to effectively delete OldString. |
| 121 | + NewString string |
| 122 | + |
| 123 | + // ReplaceAll controls the replacement behavior. |
| 124 | + // If true, all occurrences of OldString are replaced. |
| 125 | + // If false, the operation fails unless OldString appears exactly once in the file. |
| 126 | + ReplaceAll bool |
| 127 | +} |
| 128 | + |
| 129 | +// Backend is a pluggable, unified file backend protocol interface. |
| 130 | +// |
| 131 | +// All methods use struct-based parameters to allow future extensibility |
| 132 | +// without breaking backward compatibility. |
| 133 | +type Backend interface { |
| 134 | + // LsInfo lists file information under the given path. |
| 135 | + // |
| 136 | + // Returns: |
| 137 | + // - []FileInfo: List of matching file information |
| 138 | + // - error: Error if the operation fails |
| 139 | + LsInfo(ctx context.Context, req *LsInfoRequest) ([]FileInfo, error) |
| 140 | + |
| 141 | + // Read reads file content with support for line-based offset and limit. |
| 142 | + // |
| 143 | + // Returns: |
| 144 | + // - string: The file content read |
| 145 | + // - error: Error if file does not exist or read fails |
| 146 | + Read(ctx context.Context, req *ReadRequest) (string, error) |
| 147 | + |
| 148 | + // GrepRaw searches for content matching the specified pattern in files. |
| 149 | + // |
| 150 | + // Returns: |
| 151 | + // - []GrepMatch: List of all matching results |
| 152 | + // - error: Error if the search fails |
| 153 | + GrepRaw(ctx context.Context, req *GrepRequest) ([]GrepMatch, error) |
| 154 | + |
| 155 | + // GlobInfo returns file information matching the glob pattern. |
| 156 | + // |
| 157 | + // Returns: |
| 158 | + // - []FileInfo: List of matching file information |
| 159 | + // - error: Error if the pattern is invalid or operation fails |
| 160 | + GlobInfo(ctx context.Context, req *GlobInfoRequest) ([]FileInfo, error) |
| 161 | + |
| 162 | + // Write creates or updates file content. |
| 163 | + // |
| 164 | + // Returns: |
| 165 | + // - error: Error if the write operation fails |
| 166 | + Write(ctx context.Context, req *WriteRequest) error |
| 167 | + |
| 168 | + // Edit replaces string occurrences in a file. |
| 169 | + // |
| 170 | + // Returns: |
| 171 | + // - error: Error if file does not exist, OldString is empty, or OldString is not found |
| 172 | + Edit(ctx context.Context, req *EditRequest) error |
| 173 | +} |
| 174 | + |
| 175 | +type ExecuteRequest struct { |
| 176 | + Command string |
| 177 | +} |
| 178 | + |
| 179 | +type ExecuteResponse struct { |
| 180 | + Output string |
| 181 | + ExitCode *int |
| 182 | + Truncated bool |
| 183 | +} |
| 184 | + |
| 185 | +type ShellBackend interface { |
| 186 | + Backend |
| 187 | + Execute(ctx context.Context, input *ExecuteRequest) (result *ExecuteResponse, err error) |
| 188 | +} |
| 189 | + |
| 190 | +type StreamingShellBackend interface { |
| 191 | + Backend |
| 192 | + ExecuteStreaming(ctx context.Context, input *ExecuteRequest) (result *schema.StreamReader[*ExecuteResponse], err error) |
| 193 | +} |
0 commit comments