Skip to content

Commit e109b4f

Browse files
authored
refactor: export backend struct types for external access (#697)
1 parent 8f11ae8 commit e109b4f

File tree

7 files changed

+57
-70
lines changed

7 files changed

+57
-70
lines changed

.github/workflows/tests.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ jobs:
3232
# for module in $modules; do
3333
# echo "--------------------------------------------------"
3434
# echo "run unit test for ${module}"
35-
35+
3636
# cd ${module}
3737
# go test -race -coverprofile=./${COVERAGE_FILE} -gcflags="all=-l -N" -coverpkg=./... ./...
3838
# cd -
3939
# coverprofiles="${module}/${COVERAGE_FILE},$coverprofiles"
4040
# done
41-
41+
4242
# # Remove trailing comma from coverprofiles
4343
# coverprofiles=${coverprofiles%,}
44-
44+
4545
# echo "COVERAGE_PROFILES=$coverprofiles" >> $GITHUB_ENV
4646

4747
- name: Go test in go workspace
@@ -70,7 +70,7 @@ jobs:
7070
go test -race -coverprofile=coverage.out -gcflags="all=-l -N" -coverpkg=$coverpkg $list
7171
7272
echo "COVERAGE_PROFILES=$coverprofiles" >> $GITHUB_ENV
73-
73+
7474
- name: Download base.coverage for diff coverage rate
7575
id: download-base-coverage
7676
uses: actions/download-artifact@v4
@@ -86,7 +86,7 @@ jobs:
8686
config: ./.testcoverage.yml
8787
breakdown-file-name: ${{ github.ref_name == 'main' && env.BREAKDOWN_FILE || '' }}
8888
diff-base-breakdown-file-name: ${{ steps.download-base-coverage.outcome == 'success' && env.BREAKDOWN_FILE || '' }}
89-
89+
9090
- name: Upload base.coverage of main branch
9191
uses: actions/upload-artifact@v4
9292
if: github.ref_name == 'main'
@@ -124,7 +124,7 @@ jobs:
124124
- name: Set up Go
125125
uses: actions/setup-go@v5
126126
with:
127-
go-version: stable
127+
go-version: "1.25.6"
128128

129129
- name: Go BenchMark
130130
run: |
@@ -147,7 +147,7 @@ jobs:
147147
- uses: actions/checkout@v4
148148
with:
149149
fetch-depth: 0
150-
150+
151151
- name: Check modified files in submodules
152152
id: check-mods
153153
run: |

adk/backend/agentkit/sandbox.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type Config struct {
104104
ExecutionTimeout int
105105
}
106106

107-
type sandboxToolBackend struct {
107+
type SandboxTool struct {
108108
secretAccessKey string
109109
accessKeyID string
110110
baseURL string
@@ -117,14 +117,14 @@ type sandboxToolBackend struct {
117117
executionTimeout int
118118
}
119119

120-
// NewSandboxToolBackend creates a new sandboxToolBackend instance.
121-
// sandboxToolBackend refers to the sandbox running instance created by the sandbox tool in Volcengine.
120+
// NewSandboxToolBackend creates a new SandboxTool instance.
121+
// SandboxTool refers to the sandbox running instance created by the sandbox tool in Volcengine.
122122
// For creating a sandbox tool environment, please refer to: https://www.volcengine.com/docs/86681/1847934?lang=zh;
123123
// For creating a sandbox tool running instance, please refer to: https://www.volcengine.com/docs/86681/1860266?lang=zh.
124124
// Note: The execution paths within the sandbox environment may be subject to permission restrictions (read, write, execute, etc.).
125125
// Improper path selection can result in operation failures or permission errors.
126126
// It is recommended to perform operations within paths where the sandbox environment has explicit permissions to mitigate permission-related risks.
127-
func NewSandboxToolBackend(config *Config) (filesystem.Backend, error) {
127+
func NewSandboxToolBackend(config *Config) (*SandboxTool, error) {
128128
if config.AccessKeyID == "" {
129129
return nil, fmt.Errorf("AccessKeyID is required")
130130
}
@@ -159,7 +159,7 @@ func NewSandboxToolBackend(config *Config) (filesystem.Backend, error) {
159159
return nil, fmt.Errorf("invalid region: %s", region)
160160
}
161161

162-
return &sandboxToolBackend{
162+
return &SandboxTool{
163163
accessKeyID: config.AccessKeyID,
164164
secretAccessKey: config.SecretAccessKey,
165165
httpClient: httpClient,
@@ -174,7 +174,7 @@ func NewSandboxToolBackend(config *Config) (filesystem.Backend, error) {
174174
}
175175

176176
// LsInfo lists file information under the given path.
177-
func (s *sandboxToolBackend) LsInfo(ctx context.Context, req *filesystem.LsInfoRequest) ([]filesystem.FileInfo, error) {
177+
func (s *SandboxTool) LsInfo(ctx context.Context, req *filesystem.LsInfoRequest) ([]filesystem.FileInfo, error) {
178178
path, err := formatPath(req.Path, "/", true)
179179
if err != nil {
180180
return nil, err
@@ -216,7 +216,7 @@ func (s *sandboxToolBackend) LsInfo(ctx context.Context, req *filesystem.LsInfoR
216216
}
217217

218218
// Read reads file content with support for line-based offset and limit.
219-
func (s *sandboxToolBackend) Read(ctx context.Context, req *filesystem.ReadRequest) (string, error) {
219+
func (s *SandboxTool) Read(ctx context.Context, req *filesystem.ReadRequest) (string, error) {
220220
path, err := formatPath(req.FilePath, "", true)
221221
if err != nil {
222222
return "", err
@@ -251,7 +251,7 @@ func (s *sandboxToolBackend) Read(ctx context.Context, req *filesystem.ReadReque
251251
}
252252

253253
// GrepRaw searches for content matching the specified pattern in files.
254-
func (s *sandboxToolBackend) GrepRaw(ctx context.Context, req *filesystem.GrepRequest) ([]filesystem.GrepMatch, error) {
254+
func (s *SandboxTool) GrepRaw(ctx context.Context, req *filesystem.GrepRequest) ([]filesystem.GrepMatch, error) {
255255
path, _ := formatPath(req.Path, "", false)
256256
params := map[string]any{
257257
"pattern": req.Pattern,
@@ -292,7 +292,7 @@ func (s *sandboxToolBackend) GrepRaw(ctx context.Context, req *filesystem.GrepRe
292292
}
293293

294294
// GlobInfo returns file information matching the glob pattern.
295-
func (s *sandboxToolBackend) GlobInfo(ctx context.Context, req *filesystem.GlobInfoRequest) ([]filesystem.FileInfo, error) {
295+
func (s *SandboxTool) GlobInfo(ctx context.Context, req *filesystem.GlobInfoRequest) ([]filesystem.FileInfo, error) {
296296
path, _ := formatPath(req.Path, "/", false)
297297
params := map[string]any{
298298
"path_b64": base64.StdEncoding.EncodeToString([]byte(path)),
@@ -330,7 +330,7 @@ func (s *sandboxToolBackend) GlobInfo(ctx context.Context, req *filesystem.GlobI
330330
}
331331

332332
// Write creates file content.
333-
func (s *sandboxToolBackend) Write(ctx context.Context, req *filesystem.WriteRequest) error {
333+
func (s *SandboxTool) Write(ctx context.Context, req *filesystem.WriteRequest) error {
334334
path, err := formatPath(req.FilePath, "", true)
335335
if err != nil {
336336
return err
@@ -358,7 +358,7 @@ func (s *sandboxToolBackend) Write(ctx context.Context, req *filesystem.WriteReq
358358
}
359359

360360
// Edit replaces string occurrences in a file.
361-
func (s *sandboxToolBackend) Edit(ctx context.Context, req *filesystem.EditRequest) error {
361+
func (s *SandboxTool) Edit(ctx context.Context, req *filesystem.EditRequest) error {
362362
path, err := formatPath(req.FilePath, "", true)
363363
if err != nil {
364364
return err
@@ -401,7 +401,7 @@ func (s *sandboxToolBackend) Edit(ctx context.Context, req *filesystem.EditReque
401401
}
402402

403403
// execute executes a command in the sandbox.
404-
func (s *sandboxToolBackend) execute(ctx context.Context, command string) (text string, exitCode *int, err error) {
404+
func (s *SandboxTool) execute(ctx context.Context, command string) (text string, exitCode *int, err error) {
405405
var operationPayload string
406406
if s.executionTimeout <= 0 {
407407
operationPayload, err = sonic.MarshalString(map[string]any{
@@ -473,7 +473,7 @@ func (s *sandboxToolBackend) execute(ctx context.Context, command string) (text
473473
return text, exitCode, nil
474474
}
475475

476-
func (s *sandboxToolBackend) invokeTool(ctx context.Context, method string, body []byte) ([]byte, error) {
476+
func (s *SandboxTool) invokeTool(ctx context.Context, method string, body []byte) ([]byte, error) {
477477
queries := make(url.Values)
478478
queries.Set("Action", "InvokeTool")
479479
queries.Set("Version", "2025-10-30")
@@ -506,7 +506,7 @@ func (s *sandboxToolBackend) invokeTool(ctx context.Context, method string, body
506506
return responseBody, nil
507507
}
508508

509-
func (s *sandboxToolBackend) signRequest(request *http.Request, queries url.Values, body []byte) {
509+
func (s *SandboxTool) signRequest(request *http.Request, queries url.Values, body []byte) {
510510
now := time.Now()
511511
date := now.UTC().Format("20060102T150405Z")
512512
authDate := date[:8]
@@ -558,7 +558,7 @@ func (s *sandboxToolBackend) signRequest(request *http.Request, queries url.Valu
558558
request.Header.Set("Authorization", authorization)
559559
}
560560

561-
func (s *sandboxToolBackend) Execute(ctx context.Context, input *filesystem.ExecuteRequest) (result *filesystem.ExecuteResponse, err error) {
561+
func (s *SandboxTool) Execute(ctx context.Context, input *filesystem.ExecuteRequest) (result *filesystem.ExecuteResponse, err error) {
562562
if input.Command == "" {
563563
return nil, fmt.Errorf("command is required")
564564
}

adk/backend/agentkit/sandbox_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestNewArkSandbox(t *testing.T) {
4040
SessionTTL: 3600,
4141
ExecutionTimeout: 60,
4242
}
43-
ss, err := NewSandboxToolBackend(config)
44-
s := ss.(*sandboxToolBackend)
43+
s, err := NewSandboxToolBackend(config)
44+
4545
require.NoError(t, err)
4646
require.NotNil(t, s)
4747
assert.Equal(t, "test-ak", s.accessKeyID)
@@ -61,8 +61,8 @@ func TestNewArkSandbox(t *testing.T) {
6161
ToolID: "test-tool",
6262
UserSessionID: "test-session",
6363
}
64-
ss, err := NewSandboxToolBackend(config)
65-
s := ss.(*sandboxToolBackend)
64+
s, err := NewSandboxToolBackend(config)
65+
6666
require.NoError(t, err)
6767
require.NotNil(t, s)
6868
assert.Equal(t, RegionOfBeijing, s.region)
@@ -115,7 +115,7 @@ func TestNewArkSandbox(t *testing.T) {
115115
var mockAPIHandler http.HandlerFunc
116116

117117
// setupTest creates a mock server and an ArkSandbox client configured to use it.
118-
func setupTest(t *testing.T) (*sandboxToolBackend, *httptest.Server) {
118+
func setupTest(t *testing.T) (*SandboxTool, *httptest.Server) {
119119
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120120
if mockAPIHandler != nil {
121121
mockAPIHandler(w, r)
@@ -131,8 +131,7 @@ func setupTest(t *testing.T) (*sandboxToolBackend, *httptest.Server) {
131131
UserSessionID: "test-session",
132132
HTTPClient: server.Client(),
133133
}
134-
ss, err := NewSandboxToolBackend(config)
135-
sandbox := ss.(*sandboxToolBackend)
134+
sandbox, err := NewSandboxToolBackend(config)
136135
require.NoError(t, err)
137136
sandbox.baseURL = server.URL // Override to point to the mock server
138137

adk/backend/local/examples/middlewares/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func main() {
3535
ctx := context.Background()
3636

3737
fmt.Println("========================================")
38-
fmt.Println("Local Backend Middleware Example")
38+
fmt.Println("Local Local Middleware Example")
3939
fmt.Println("========================================")
4040
fmt.Println()
4141

4242
// ========================================
43-
// Step 1: Initialize Local Backend
43+
// Step 1: Initialize Local Local
4444
// ========================================
45-
fmt.Println("Step 1: Initializing Local Backend...")
45+
fmt.Println("Step 1: Initializing Local Local...")
4646

4747
// The local backend operates directly on the local filesystem
4848
// Optionally provide ValidateCommand for Execute() method security
@@ -53,7 +53,7 @@ func main() {
5353
log.Fatalf("✗ Failed to create LocalBackend: %v", err)
5454
}
5555

56-
fmt.Println("✓ Local Backend initialized")
56+
fmt.Println("✓ Local Local initialized")
5757
fmt.Println()
5858

5959
// ========================================
@@ -69,7 +69,7 @@ func main() {
6969
defer os.RemoveAll(tempDir)
7070

7171
testFilePath := filepath.Join(tempDir, "example_file.txt")
72-
testContent := "Hello from Local Backend!\nThis is a test for file operations.\n"
72+
testContent := "Hello from Local Local!\nThis is a test for file operations.\n"
7373

7474
err = backend.Write(ctx, &filesystem.WriteRequest{
7575
FilePath: testFilePath,

adk/backend/local/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFos
1616
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
1717
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
1818
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
19-
github.com/cloudwego/eino v0.7.19 h1:KoA3KHoZ3adQI0nvC0EBR7tpeu/Ks69n+baRMOQGLqA=
20-
github.com/cloudwego/eino v0.7.19/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
21-
github.com/cloudwego/eino v0.7.26 h1:FD8xnhd8WyV495eJs0Rka8aHnknqj/ljq3ZbwGH/SiQ=
22-
github.com/cloudwego/eino v0.7.26/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
2319
github.com/cloudwego/eino v0.7.27 h1:pHxpvpQjAqez+yPgxxX0V298YmJd5cDQqCBAn8XnJYo=
2420
github.com/cloudwego/eino v0.7.27/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
2521
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

adk/backend/local/local.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ type Config struct {
4040
ValidateCommand func(string) error
4141
}
4242

43-
type backend struct {
43+
type Local struct {
4444
validateCommand func(string) error
4545
}
4646

4747
var defaultValidateCommand = func(string) error {
4848
return nil
4949
}
5050

51-
// NewBackend creates a new local filesystem backend instance.
51+
// NewBackend creates a new local filesystem Local instance.
5252
//
5353
// IMPORTANT - System Compatibility:
5454
// - Supported: Unix/MacOS only
55-
// - NOT Supported: Windows (requires custom implementation of Backend)
55+
// - NOT Supported: Windows (requires custom implementation of filesystem.Backend)
5656
// - Command Execution: Uses /bin/sh by default for Execute method
57-
// - If /bin/sh does not meet your requirements, please implement your own Backend
58-
func NewBackend(_ context.Context, cfg *Config) (filesystem.Backend, error) {
57+
// - If /bin/sh does not meet your requirements, please implement your own filesystem.Backend
58+
func NewBackend(_ context.Context, cfg *Config) (*Local, error) {
5959
if cfg == nil {
6060
return nil, errors.New("config is required")
6161
}
@@ -65,12 +65,12 @@ func NewBackend(_ context.Context, cfg *Config) (filesystem.Backend, error) {
6565
validateCommand = cfg.ValidateCommand
6666
}
6767

68-
return &backend{
68+
return &Local{
6969
validateCommand: validateCommand,
7070
}, nil
7171
}
7272

73-
func (s *backend) LsInfo(ctx context.Context, req *filesystem.LsInfoRequest) ([]filesystem.FileInfo, error) {
73+
func (s *Local) LsInfo(ctx context.Context, req *filesystem.LsInfoRequest) ([]filesystem.FileInfo, error) {
7474
if req.Path == "" {
7575
req.Path = defaultRootPath
7676
}
@@ -105,7 +105,7 @@ func (s *backend) LsInfo(ctx context.Context, req *filesystem.LsInfoRequest) ([]
105105
return files, nil
106106
}
107107

108-
func (s *backend) Read(ctx context.Context, req *filesystem.ReadRequest) (string, error) {
108+
func (s *Local) Read(ctx context.Context, req *filesystem.ReadRequest) (string, error) {
109109
path := filepath.Clean(req.FilePath)
110110
if !filepath.IsAbs(path) {
111111
return "", fmt.Errorf("path must be an absolute path: %s", path)
@@ -160,7 +160,7 @@ func (s *backend) Read(ctx context.Context, req *filesystem.ReadRequest) (string
160160
return result.String(), nil
161161
}
162162

163-
func (s *backend) GrepRaw(ctx context.Context, req *filesystem.GrepRequest) ([]filesystem.GrepMatch, error) {
163+
func (s *Local) GrepRaw(ctx context.Context, req *filesystem.GrepRequest) ([]filesystem.GrepMatch, error) {
164164
path := filepath.Clean(req.Path)
165165

166166
var matches []filesystem.GrepMatch
@@ -232,7 +232,7 @@ func (s *backend) GrepRaw(ctx context.Context, req *filesystem.GrepRequest) ([]f
232232
return matches, nil
233233
}
234234

235-
func (s *backend) GlobInfo(ctx context.Context, req *filesystem.GlobInfoRequest) ([]filesystem.FileInfo, error) {
235+
func (s *Local) GlobInfo(ctx context.Context, req *filesystem.GlobInfoRequest) ([]filesystem.FileInfo, error) {
236236
if req.Path == "" {
237237
req.Path = defaultRootPath
238238
}
@@ -337,7 +337,7 @@ func globToRegex(pattern string) (*regexp.Regexp, error) {
337337
return regexp.Compile(pattern)
338338
}
339339

340-
func (s *backend) Write(ctx context.Context, req *filesystem.WriteRequest) error {
340+
func (s *Local) Write(ctx context.Context, req *filesystem.WriteRequest) error {
341341
if !filepath.IsAbs(req.FilePath) {
342342
return fmt.Errorf("path must be an absolute path: %s", req.FilePath)
343343
}
@@ -364,7 +364,7 @@ func (s *backend) Write(ctx context.Context, req *filesystem.WriteRequest) error
364364
return nil
365365
}
366366

367-
func (s *backend) Edit(ctx context.Context, req *filesystem.EditRequest) error {
367+
func (s *Local) Edit(ctx context.Context, req *filesystem.EditRequest) error {
368368
path := filepath.Clean(req.FilePath)
369369
if !filepath.IsAbs(path) {
370370
return fmt.Errorf("path must be an absolute path: %s", path)
@@ -403,7 +403,7 @@ func (s *backend) Edit(ctx context.Context, req *filesystem.EditRequest) error {
403403
return os.WriteFile(path, []byte(newText), 0644)
404404
}
405405

406-
func (s *backend) ExecuteStreaming(ctx context.Context, input *filesystem.ExecuteRequest) (result *schema.StreamReader[*filesystem.ExecuteResponse], err error) {
406+
func (s *Local) ExecuteStreaming(ctx context.Context, input *filesystem.ExecuteRequest) (result *schema.StreamReader[*filesystem.ExecuteResponse], err error) {
407407
if input.Command == "" {
408408
return nil, fmt.Errorf("command is required")
409409
}

0 commit comments

Comments
 (0)