Migration Guide
This guide helps you migrate from the old API to the new submodule-based API in the AI Computer Python SDK. The new API organizes functionality into submodules for better code organization and a more intuitive interface.
Overview of Changes
The main changes in the new API include:
- Organization of functionality into submodules (fs, shell, code)
- Removal of deprecated methods like execute_code_stream and list_dir
- Improved file operations with dedicated endpoints
- Consistent response objects for better error handling
Code Execution
Old API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Execute code
7response = await client.execute_code("print('Hello, World!')")
8print(response.data)
9
10# Stream code execution (deprecated)
11async for event in client.execute_code_stream("print('Hello, World!')"):
12 if event.type == 'stdout':
13 print(event.data)
14
15await client.cleanup()
New API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Execute code using the code submodule
7response = await client.code.execute("print('Hello, World!')")
8print(response.data)
9
10# Stream code execution using the code submodule
11async for event in client.code.execute_stream("print('Hello, World!')"):
12 if event.type == 'stdout':
13 print(event.data)
14
15await client.cleanup()
File Operations
Old API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Read file
7response = await client.read_file("/path/to/file.txt")
8print(response.data)
9
10# Write file
11await client.write_file("/path/to/file.txt", "Hello, World!")
12
13# List directory (deprecated)
14response = await client.list_dir("/path")
15print(response.data)
16
17await client.cleanup()
New API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Read file using the fs submodule
7response = await client.fs.read_file("file.txt")
8print(response.data)
9
10# Write file using the fs submodule
11await client.fs.write_file("file.txt", "Hello, World!")
12
13# Upload and download files
14await client.fs.upload_file("local_file.txt", "remote_file.txt")
15await client.fs.download_file("remote_file.txt", "downloaded_file.txt")
16
17# Upload and download binary data
18binary_data = b"\x00\x01\x02\x03"
19await client.fs.upload_bytes(binary_data, "binary_file.bin")
20response = await client.fs.download_bytes("binary_file.bin")
21print(len(response.data))
22
23await client.cleanup()
Shell Commands
Old API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Execute shell command
7response = await client.execute_shell("ls -la")
8print(response.data)
9
10await client.cleanup()
New API
1from ai_computer import SandboxClient
2
3client = SandboxClient()
4await client.setup()
5
6# Execute shell command using the shell submodule
7response = await client.shell.execute("ls -la")
8print(response.data)
9
10await client.cleanup()
Response Objects
The new API uses consistent response objects for better error handling:
- SandboxResponse - For code execution and shell commands
- FileOperationResponse - For file operations
1# Handling SandboxResponse
2response = await client.code.execute("print('Hello, World!')")
3if response.success:
4 print(f"Output: {response.data}")
5else:
6 print(f"Error: {response.error}")
7
8# Handling FileOperationResponse
9response = await client.fs.read_file("file.txt")
10if response.success:
11 print(f"Content: {response.data}")
12else:
13 print(f"Error: {response.error}")
Migration Checklist
1. Update Code Execution
Replace client.execute_code()
with client.code.execute()
2. Update Streaming
Replace client.execute_code_stream()
with client.code.execute_stream()
3. Update File Operations
Replace client.read_file()
with client.fs.read_file()
Replace client.write_file()
with client.fs.write_file()
4. Update Shell Commands
Replace client.execute_shell()
with client.shell.execute()
5. Use Relative Paths
Update file paths to use relative paths instead of absolute paths
6. Update Error Handling
Ensure error handling is consistent with the new response objects