@@ -34,16 +34,20 @@ func main() {
3434 fmt .Println ("\n 3. File Operations Example:" )
3535 fileOperationsExample (apiKey )
3636
37- // Example 4: Web search and scraping
38- fmt .Println ("\n 4. Web Tools Example:" )
37+ // Example 4: File sandbox
38+ fmt .Println ("\n 4. File Sandbox Example:" )
39+ fileSandboxExample (apiKey )
40+
41+ // Example 5: Web search and scraping
42+ fmt .Println ("\n 5. Web Tools Example:" )
3943 webToolsExample (apiKey )
4044
41- // Example 5 : Multiple tools
42- fmt .Println ("\n 5 . Multiple Tools Example:" )
45+ // Example 6 : Multiple tools
46+ fmt .Println ("\n 6 . Multiple Tools Example:" )
4347 multipleToolsExample (apiKey )
4448
45- // Example 6 : Custom tool
46- fmt .Println ("\n 6 . Custom Tool Example:" )
49+ // Example 7 : Custom tool
50+ fmt .Println ("\n 7 . 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
121174func 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