Skip to content

Commit 93c25dc

Browse files
committed
file sandbox support
1 parent e230794 commit 93c25dc

4 files changed

Lines changed: 210 additions & 25 deletions

File tree

README.zh-CN.md renamed to README-CN.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ func main() {
8181
}
8282
```
8383

84+
### 文件工具沙箱
85+
86+
```go
87+
// 无限制访问
88+
tools.FileReader()
89+
90+
// 仅当前目录
91+
sandbox := tools.DefaultSandbox()
92+
tools.FileReaderWithSandbox(sandbox)
93+
94+
// 自定义允许路径
95+
sandbox := &tools.FileSandbox{
96+
AllowedPaths: []string{"./data", "./uploads"},
97+
AllowCurrentDir: false,
98+
}
99+
tools.FileWriterWithSandbox(sandbox)
100+
```
101+
84102
### 团队协作
85103

86104
```go
@@ -135,9 +153,9 @@ MAS 开箱即用地提供有用的工具:
135153
- **域名信息** - WHOIS、DNS、SSL信息
136154

137155
### 文件操作
138-
- **文件读写器** - 读写文件
139-
- **目录列表器** - 浏览文件系统
140-
- **文件信息** - 获取文件元数据
156+
- **文件读写器** - 读写文件,支持沙箱限制
157+
- **目录列表器** - 浏览文件系统,支持路径限制
158+
- **文件信息** - 获取文件元数据,支持访问控制
141159

142160
### 数据处理
143161
- **JSON解析器** - 解析和操作JSON

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ func main() {
8181
}
8282
```
8383

84+
### File Tools with Sandbox
85+
86+
```go
87+
// Unrestricted access
88+
tools.FileReader()
89+
90+
// Current directory only
91+
sandbox := tools.DefaultSandbox()
92+
tools.FileReaderWithSandbox(sandbox)
93+
94+
// Custom allowed paths
95+
sandbox := &tools.FileSandbox{
96+
AllowedPaths: []string{"./data", "./uploads"},
97+
AllowCurrentDir: false,
98+
}
99+
tools.FileWriterWithSandbox(sandbox)
100+
```
101+
84102
### Team Collaboration
85103

86104
```go
@@ -135,9 +153,9 @@ MAS comes with useful tools out of the box:
135153
- **Domain Info** - WHOIS, DNS, SSL information
136154

137155
### File Operations
138-
- **File Reader/Writer** - Read and write files
139-
- **Directory Lister** - Browse filesystem
140-
- **File Info** - Get file metadata
156+
- **File Reader/Writer** - Read and write files with sandbox support
157+
- **Directory Lister** - Browse filesystem with path restrictions
158+
- **File Info** - Get file metadata with access control
141159

142160
### Data Processing
143161
- **JSON Parser** - Parse and manipulate JSON

examples/tools/main.go

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ func main() {
3434
fmt.Println("\n3. File Operations Example:")
3535
fileOperationsExample(apiKey)
3636

37-
// Example 4: Web search and scraping
38-
fmt.Println("\n4. Web Tools Example:")
37+
// Example 4: File sandbox
38+
fmt.Println("\n4. File Sandbox Example:")
39+
fileSandboxExample(apiKey)
40+
41+
// Example 5: Web search and scraping
42+
fmt.Println("\n5. Web Tools Example:")
3943
webToolsExample(apiKey)
4044

41-
// Example 5: Multiple tools
42-
fmt.Println("\n5. Multiple Tools Example:")
45+
// Example 6: Multiple tools
46+
fmt.Println("\n6. Multiple Tools Example:")
4347
multipleToolsExample(apiKey)
4448

45-
// Example 6: Custom tool
46-
fmt.Println("\n6. Custom Tool Example:")
49+
// Example 7: Custom tool
50+
fmt.Println("\n7. Custom Tool Example:")
4751
customToolExample(apiKey)
4852
}
4953

@@ -68,7 +72,7 @@ func httpRequestExample(apiKey string) {
6872
WithTools(tools.HTTPRequest(), tools.JSONParser()).
6973
WithSystemPrompt("You are a web API assistant. Use HTTP tools to fetch and analyze data.")
7074

71-
response, err := agent.Chat(context.Background(),
75+
response, err := agent.Chat(context.Background(),
7276
"Make a GET request to https://httpbin.org/json and tell me about the response")
7377
if err != nil {
7478
log.Printf("Error: %v", err)
@@ -92,7 +96,7 @@ func fileOperationsExample(apiKey string) {
9296
ctx := context.Background()
9397

9498
// Create a test file
95-
response1, err := agent.Chat(ctx,
99+
response1, err := agent.Chat(ctx,
96100
"Create a file called 'test.txt' with the content 'Hello, MAS Framework!'")
97101
if err != nil {
98102
log.Printf("Error: %v", err)
@@ -117,6 +121,55 @@ func fileOperationsExample(apiKey string) {
117121
fmt.Printf("Agent: %s\n", response3)
118122
}
119123

124+
// fileSandboxExample demonstrates file tools with sandbox restrictions
125+
func fileSandboxExample(apiKey string) {
126+
ctx := context.Background()
127+
128+
// Example 1: No sandbox (unrestricted)
129+
fmt.Println(" Unrestricted access:")
130+
agent1 := mas.NewAgent("gpt-4", apiKey).
131+
WithTools(tools.FileReader()).
132+
WithSystemPrompt("You are a file assistant.")
133+
134+
response1, err := agent1.Chat(ctx, "List current directory")
135+
if err != nil {
136+
log.Printf("Error: %v", err)
137+
} else {
138+
fmt.Printf(" Agent: %s\n", response1)
139+
}
140+
141+
// Example 2: Current directory only
142+
fmt.Println(" Current directory only:")
143+
sandbox := tools.DefaultSandbox()
144+
agent2 := mas.NewAgent("gpt-4", apiKey).
145+
WithTools(tools.DirectoryListerWithSandbox(sandbox)).
146+
WithSystemPrompt("You are restricted to current directory.")
147+
148+
response2, err := agent2.Chat(ctx, "Try to list parent directory '../'")
149+
if err != nil {
150+
fmt.Printf(" Expected restriction: %v\n", err)
151+
} else {
152+
fmt.Printf(" Agent: %s\n", response2)
153+
}
154+
155+
// Example 3: Custom allowed paths
156+
fmt.Println(" Custom allowed paths:")
157+
customSandbox := &tools.FileSandbox{
158+
AllowedPaths: []string{"./examples"},
159+
AllowCurrentDir: false,
160+
}
161+
agent3 := mas.NewAgent("gpt-4", apiKey).
162+
WithTools(tools.DirectoryListerWithSandbox(customSandbox)).
163+
WithSystemPrompt("You can only access ./examples directory.")
164+
165+
response3, err := agent3.Chat(ctx, "List ./examples directory")
166+
if err != nil {
167+
log.Printf("Error: %v", err)
168+
} else {
169+
fmt.Printf(" Agent: %s\n", response3)
170+
}
171+
}
172+
120173
// webToolsExample demonstrates web search and scraping tools
121174
func webToolsExample(apiKey string) {
122175
agent := mas.NewAgent("gpt-4", apiKey).
@@ -127,7 +180,7 @@ func webToolsExample(apiKey string) {
127180
).
128181
WithSystemPrompt("You are a web research assistant. Help users find and analyze web information.")
129182

130-
response, err := agent.Chat(context.Background(),
183+
response, err := agent.Chat(context.Background(),
131184
"Search for information about 'artificial intelligence' and tell me what you find")
132185
if err != nil {
133186
log.Printf("Error: %v", err)
@@ -152,7 +205,7 @@ func multipleToolsExample(apiKey string) {
152205
ctx := context.Background()
153206

154207
// Complex task requiring multiple tools
155-
response, err := agent.Chat(ctx,
208+
response, err := agent.Chat(ctx,
156209
"Calculate 15% of 250, then search for information about percentage calculations, and save the result to a file called 'calculation_result.txt'")
157210
if err != nil {
158211
log.Printf("Error: %v", err)
@@ -171,7 +224,7 @@ func customToolExample(apiKey string) {
171224
WithTools(randomTool).
172225
WithSystemPrompt("You are an assistant with a random number generator tool.")
173226

174-
response, err := agent.Chat(context.Background(),
227+
response, err := agent.Chat(context.Background(),
175228
"Generate a random number between 1 and 100")
176229
if err != nil {
177230
log.Printf("Error: %v", err)
@@ -211,11 +264,11 @@ func createRandomNumberTool() mas.Tool {
211264
// Simple random number generation (not cryptographically secure)
212265
range_ := max - min + 1
213266
// Using a simple hash-based approach for demonstration
214-
hash := int64(min*31 + max*17) % 1000
267+
hash := int64(min*31+max*17) % 1000
215268
if hash < 0 {
216269
hash = -hash
217270
}
218-
271+
219272
result := min + float64(hash%int64(range_))
220273

221274
return map[string]interface{}{
@@ -251,7 +304,7 @@ func toolRegistryExample(apiKey string) {
251304
WithTools(registry.List()...).
252305
WithSystemPrompt("You have access to multiple tools. Use them as needed.")
253306

254-
response, err := agent.Chat(context.Background(),
307+
response, err := agent.Chat(context.Background(),
255308
"What tools do you have available?")
256309
if err != nil {
257310
log.Printf("Error: %v", err)
@@ -277,4 +330,4 @@ func toolErrorHandlingExample(apiKey string) {
277330
}
278331

279332
fmt.Printf("Agent: %s\n", response)
280-
}
333+
}

0 commit comments

Comments
 (0)