Skip to content

Commit 1d5dc79

Browse files
feat(adk): add execute backend&tool
1 parent 9e6963f commit 1d5dc79

14 files changed

+1558
-169
lines changed

adk/filesystem/backend.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}
File renamed without changes.
File renamed without changes.

adk/middlewares/filesystem/backend.go

Lines changed: 11 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,115 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Package filesystem provides middlewares and types for file system operations.
17+
// Package filesystem provides middlewares.
1818
package filesystem
1919

2020
import (
2121
"context"
22-
)
23-
24-
// FileInfo represents basic file metadata information.
25-
type FileInfo struct {
26-
// Path is the absolute path of the file or directory.
27-
Path string
28-
}
29-
30-
// GrepMatch represents a single pattern match result.
31-
type GrepMatch struct {
32-
// Path is the absolute path of the file where the match occurred.
33-
Path string
34-
// Line is the 1-based line number of the match.
35-
Line int
36-
// Content is the full text content of the line containing the match.
37-
Content string
38-
}
39-
40-
// LsInfoRequest contains parameters for listing file information.
41-
type LsInfoRequest struct {
42-
// Path specifies the absolute directory path to list.
43-
// It must be an absolute path starting with '/'.
44-
// An empty string is treated as the root directory ("/").
45-
Path string
46-
}
47-
48-
// ReadRequest contains parameters for reading file content.
49-
type ReadRequest struct {
50-
// FilePath is the absolute path to the file to be read. Must start with '/'.
51-
FilePath string
52-
53-
// Offset is the 0-based line number to start reading from.
54-
// If negative, it is treated as 0. Defaults to 0.
55-
Offset int
56-
57-
// Limit specifies the maximum number of lines to read.
58-
// If non-positive (<= 0), a default limit is used (typically 200).
59-
Limit int
60-
}
61-
62-
// GrepRequest contains parameters for searching file content.
63-
type GrepRequest struct {
64-
// Pattern is the literal string to search for. This is not a regular expression.
65-
// The search performs an exact substring match within the file's content.
66-
// For example, "TODO" will match any line containing "TODO".
67-
Pattern string
68-
69-
// Path is an optional directory path to limit the search scope.
70-
// If empty, the search is performed from the working directory.
71-
Path string
7222

73-
// Glob is an optional pattern to filter the files to be searched.
74-
// It filters by file path, not content. If empty, no files are filtered.
75-
// Supports standard glob wildcards:
76-
// - `*` matches any characters except path separators.
77-
// - `**` matches any directories recursively.
78-
// - `?` matches a single character.
79-
// - `[abc]` matches one character from the set.
80-
Glob string
81-
}
82-
83-
// GlobInfoRequest contains parameters for glob pattern matching.
84-
type GlobInfoRequest struct {
85-
// Pattern is the glob expression used to match file paths.
86-
// It supports standard glob syntax:
87-
// - `*` matches any characters except path separators.
88-
// - `**` matches any directories recursively.
89-
// - `?` matches a single character.
90-
// - `[abc]` matches one character from the set.
91-
Pattern string
92-
93-
// Path is the base directory from which to start the search.
94-
// The glob pattern is applied relative to this path. Defaults to the root directory ("/").
95-
Path string
96-
}
97-
98-
// WriteRequest contains parameters for writing file content.
99-
type WriteRequest struct {
100-
// FilePath is the absolute path of the file to write. Must start with '/'.
101-
// The file will be created if it does not exist, or error if file exists.
102-
FilePath string
103-
104-
// Content is the data to be written to the file.
105-
Content string
106-
}
107-
108-
// EditRequest contains parameters for editing file content.
109-
type EditRequest struct {
110-
// FilePath is the absolute path of the file to edit. Must start with '/'.
111-
FilePath string
112-
113-
// OldString is the exact string to be replaced. It must be non-empty and will be matched literally, including whitespace.
114-
OldString string
115-
116-
// NewString is the string that will replace OldString.
117-
// It must be different from OldString.
118-
// An empty string can be used to effectively delete OldString.
119-
NewString string
23+
"github.com/cloudwego/eino/adk/filesystem"
24+
)
12025

121-
// ReplaceAll controls the replacement behavior.
122-
// If true, all occurrences of OldString are replaced.
123-
// If false, the operation fails unless OldString appears exactly once in the file.
124-
ReplaceAll bool
125-
}
26+
type FileInfo = filesystem.FileInfo
27+
type GrepMatch = filesystem.GrepMatch
28+
type LsInfoRequest = filesystem.LsInfoRequest
29+
type ReadRequest = filesystem.ReadRequest
30+
type GrepRequest = filesystem.GrepRequest
31+
type GlobInfoRequest = filesystem.GlobInfoRequest
32+
type WriteRequest = filesystem.WriteRequest
33+
type EditRequest = filesystem.EditRequest
12634

12735
// Backend is a pluggable, unified file backend protocol interface.
12836
//
@@ -169,7 +77,3 @@ type Backend interface {
16977
// - error: Error if file does not exist, OldString is empty, or OldString is not found
17078
Edit(ctx context.Context, req *EditRequest) error
17179
}
172-
173-
//type SandboxFileSystem interface {
174-
// Execute(ctx context.Context, command string) (output string, exitCode *int, truncated bool, err error)
175-
//}

0 commit comments

Comments
 (0)