ai-computer-client 0.3.0__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_computer/__init__.py +1 -1
- ai_computer/client.py +19 -11
- ai_computer_client-0.3.1.dist-info/METADATA +147 -0
- ai_computer_client-0.3.1.dist-info/RECORD +6 -0
- ai_computer_client-0.3.0.dist-info/METADATA +0 -222
- ai_computer_client-0.3.0.dist-info/RECORD +0 -6
- {ai_computer_client-0.3.0.dist-info → ai_computer_client-0.3.1.dist-info}/WHEEL +0 -0
- {ai_computer_client-0.3.0.dist-info → ai_computer_client-0.3.1.dist-info}/licenses/LICENSE +0 -0
ai_computer/__init__.py
CHANGED
ai_computer/client.py
CHANGED
@@ -350,7 +350,7 @@ class SandboxClient:
|
|
350
350
|
|
351
351
|
Args:
|
352
352
|
file_path: Path to the file to upload
|
353
|
-
destination: Destination path in the sandbox (
|
353
|
+
destination: Destination path in the sandbox (absolute path starting with /)
|
354
354
|
chunk_size: Size of chunks for reading large files
|
355
355
|
timeout: Maximum upload time in seconds
|
356
356
|
|
@@ -405,7 +405,7 @@ class SandboxClient:
|
|
405
405
|
# Prepare multipart form data
|
406
406
|
data = aiohttp.FormData()
|
407
407
|
data.add_field('file',
|
408
|
-
open(file_path, 'rb'),
|
408
|
+
open(file_path, 'rb').read(),
|
409
409
|
filename=file_path.name,
|
410
410
|
content_type=content_type)
|
411
411
|
data.add_field('path', destination)
|
@@ -460,7 +460,8 @@ class SandboxClient:
|
|
460
460
|
"""Download a file from the sandbox environment.
|
461
461
|
|
462
462
|
Args:
|
463
|
-
sandbox_path: Path to the file in the sandbox
|
463
|
+
sandbox_path: Path to the file in the sandbox (must be an absolute path starting with /).
|
464
|
+
Any double slashes in the path will be normalized.
|
464
465
|
local_path: Local path to save the file (default: current directory with original filename)
|
465
466
|
chunk_size: Size of chunks for downloading large files
|
466
467
|
timeout: Maximum download time in seconds
|
@@ -482,8 +483,11 @@ class SandboxClient:
|
|
482
483
|
error=ready.error or "Sandbox not ready"
|
483
484
|
)
|
484
485
|
|
485
|
-
#
|
486
|
-
|
486
|
+
# Ensure path is absolute and normalize any double slashes
|
487
|
+
if not sandbox_path.startswith('/'):
|
488
|
+
sandbox_path = f"/{sandbox_path}"
|
489
|
+
clean_path = '/'.join(part for part in sandbox_path.split('/') if part)
|
490
|
+
clean_path = f"/{clean_path}"
|
487
491
|
|
488
492
|
# Determine local path
|
489
493
|
if local_path is None:
|
@@ -508,7 +512,7 @@ class SandboxClient:
|
|
508
512
|
|
509
513
|
async with aiohttp.ClientSession(timeout=timeout_settings) as session:
|
510
514
|
async with session.get(
|
511
|
-
f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download
|
515
|
+
f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download{clean_path}",
|
512
516
|
headers=headers
|
513
517
|
) as response:
|
514
518
|
if response.status != 200:
|
@@ -572,7 +576,7 @@ class SandboxClient:
|
|
572
576
|
Args:
|
573
577
|
content: Bytes or file-like object to upload
|
574
578
|
filename: Name to give the file in the sandbox
|
575
|
-
destination: Destination path in the sandbox (
|
579
|
+
destination: Destination path in the sandbox (absolute path starting with /)
|
576
580
|
content_type: Optional MIME type (will be guessed from filename if not provided)
|
577
581
|
timeout: Maximum upload time in seconds
|
578
582
|
|
@@ -687,7 +691,8 @@ class SandboxClient:
|
|
687
691
|
"""Download a file from the sandbox environment into memory.
|
688
692
|
|
689
693
|
Args:
|
690
|
-
sandbox_path: Path to the file in the sandbox
|
694
|
+
sandbox_path: Path to the file in the sandbox (must be an absolute path starting with /).
|
695
|
+
Any double slashes in the path will be normalized.
|
691
696
|
chunk_size: Size of chunks for downloading large files
|
692
697
|
timeout: Maximum download time in seconds
|
693
698
|
|
@@ -709,8 +714,11 @@ class SandboxClient:
|
|
709
714
|
error=ready.error or "Sandbox not ready"
|
710
715
|
)
|
711
716
|
|
712
|
-
#
|
713
|
-
|
717
|
+
# Ensure path is absolute and normalize any double slashes
|
718
|
+
if not sandbox_path.startswith('/'):
|
719
|
+
sandbox_path = f"/{sandbox_path}"
|
720
|
+
clean_path = '/'.join(part for part in sandbox_path.split('/') if part)
|
721
|
+
clean_path = f"/{clean_path}"
|
714
722
|
|
715
723
|
try:
|
716
724
|
timeout_settings = aiohttp.ClientTimeout(
|
@@ -726,7 +734,7 @@ class SandboxClient:
|
|
726
734
|
|
727
735
|
async with aiohttp.ClientSession(timeout=timeout_settings) as session:
|
728
736
|
async with session.get(
|
729
|
-
f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download
|
737
|
+
f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download{clean_path}",
|
730
738
|
headers=headers
|
731
739
|
) as response:
|
732
740
|
if response.status != 200:
|
@@ -0,0 +1,147 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: ai-computer-client
|
3
|
+
Version: 0.3.1
|
4
|
+
Summary: Python client for interacting with the AI Computer service
|
5
|
+
Project-URL: Homepage, https://github.com/ColeMurray/ai-computer-client-python
|
6
|
+
Project-URL: Documentation, https://github.com/ColeMurray/ai-computer-client-python#readme
|
7
|
+
Author: AI Computer
|
8
|
+
License: MIT
|
9
|
+
License-File: LICENSE
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
11
|
+
Classifier: Intended Audience :: Developers
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
14
|
+
Classifier: Programming Language :: Python :: 3.7
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
19
|
+
Requires-Python: >=3.7
|
20
|
+
Requires-Dist: aiohttp>=3.8.0
|
21
|
+
Requires-Dist: typing-extensions>=4.0.0
|
22
|
+
Provides-Extra: dev
|
23
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
|
24
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
25
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
26
|
+
Description-Content-Type: text/markdown
|
27
|
+
|
28
|
+
# AI Computer Python Client
|
29
|
+
|
30
|
+
Python client library for interacting with the AI Computer Sandbox service.
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
```bash
|
35
|
+
pip install ai-computer-client
|
36
|
+
```
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
### Basic Usage
|
41
|
+
|
42
|
+
```python
|
43
|
+
from ai_computer import SandboxClient
|
44
|
+
|
45
|
+
async def main():
|
46
|
+
# Initialize client
|
47
|
+
client = SandboxClient()
|
48
|
+
|
49
|
+
# Setup sandbox environment
|
50
|
+
await client.setup()
|
51
|
+
|
52
|
+
try:
|
53
|
+
# Execute code
|
54
|
+
result = await client.execute_code("print('Hello, World!')")
|
55
|
+
print(result.data["output"])
|
56
|
+
|
57
|
+
# Upload a file
|
58
|
+
response = await client.upload_file("local/path/to/file.txt")
|
59
|
+
if response.success:
|
60
|
+
print(f"File uploaded to {response.path}")
|
61
|
+
|
62
|
+
# Download a file
|
63
|
+
response = await client.download_file(
|
64
|
+
"/workspace/file.txt",
|
65
|
+
"local/download/path.txt"
|
66
|
+
)
|
67
|
+
if response.success:
|
68
|
+
print(f"File downloaded to {response.path}")
|
69
|
+
|
70
|
+
# Work with bytes directly
|
71
|
+
content = b"Hello, World!"
|
72
|
+
response = await client.upload_bytes(
|
73
|
+
content=content,
|
74
|
+
filename="hello.txt"
|
75
|
+
)
|
76
|
+
|
77
|
+
# Download as bytes
|
78
|
+
content = await client.download_bytes("/workspace/hello.txt")
|
79
|
+
if isinstance(content, bytes):
|
80
|
+
print(content.decode())
|
81
|
+
|
82
|
+
finally:
|
83
|
+
# Cleanup
|
84
|
+
await client.cleanup()
|
85
|
+
|
86
|
+
# Run with asyncio
|
87
|
+
import asyncio
|
88
|
+
asyncio.run(main())
|
89
|
+
```
|
90
|
+
|
91
|
+
### Advanced Usage
|
92
|
+
|
93
|
+
```python
|
94
|
+
# Stream code execution
|
95
|
+
async for event in client.execute_code_stream("print('Hello')\nprint('World')"):
|
96
|
+
if event.type == "stdout":
|
97
|
+
print(f"Output: {event.data}")
|
98
|
+
elif event.type == "stderr":
|
99
|
+
print(f"Error: {event.data}")
|
100
|
+
elif event.type == "completed":
|
101
|
+
print("Execution completed")
|
102
|
+
|
103
|
+
# Upload with custom settings
|
104
|
+
response = await client.upload_file(
|
105
|
+
"file.txt",
|
106
|
+
destination="/workspace/custom/path",
|
107
|
+
chunk_size=2*1024*1024, # 2MB chunks
|
108
|
+
timeout=600 # 10 minutes
|
109
|
+
)
|
110
|
+
|
111
|
+
# Work with file-like objects
|
112
|
+
from io import BytesIO
|
113
|
+
|
114
|
+
buffer = BytesIO(b"Hello from buffer!")
|
115
|
+
response = await client.upload_bytes(
|
116
|
+
content=buffer,
|
117
|
+
filename="buffer.txt",
|
118
|
+
content_type="text/plain"
|
119
|
+
)
|
120
|
+
```
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
### Setup
|
125
|
+
|
126
|
+
```bash
|
127
|
+
# Clone the repository
|
128
|
+
git clone https://github.com/ai-computer/ai-computer-client-python
|
129
|
+
cd ai-computer-client-python
|
130
|
+
|
131
|
+
# Create and activate virtual environment
|
132
|
+
python -m venv venv
|
133
|
+
source venv/bin/activate # or `venv\Scripts\activate` on Windows
|
134
|
+
|
135
|
+
# Install development dependencies
|
136
|
+
pip install -e ".[dev]"
|
137
|
+
```
|
138
|
+
|
139
|
+
### Running Tests
|
140
|
+
|
141
|
+
```bash
|
142
|
+
pytest tests/
|
143
|
+
```
|
144
|
+
|
145
|
+
## License
|
146
|
+
|
147
|
+
MIT License
|
@@ -0,0 +1,6 @@
|
|
1
|
+
ai_computer/__init__.py,sha256=9slbwvU5DHW6aOeLy7qIFe7TQkXF3-kOMI2x82VqiAk,197
|
2
|
+
ai_computer/client.py,sha256=TwVsAmNjaj5MDytKZtoHefFJAwYyL2OndtVbY2YxJlM,28729
|
3
|
+
ai_computer_client-0.3.1.dist-info/METADATA,sha256=YDRTkYudV6X0SRLW0OECdduJgCc3dYITjTr99eat0ls,3754
|
4
|
+
ai_computer_client-0.3.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
ai_computer_client-0.3.1.dist-info/licenses/LICENSE,sha256=N_0S5G1Wik2LWVDViJMAM0Z-6vTBX1bvDjb8vouBA-c,1068
|
6
|
+
ai_computer_client-0.3.1.dist-info/RECORD,,
|
@@ -1,222 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: ai-computer-client
|
3
|
-
Version: 0.3.0
|
4
|
-
Summary: Python client for interacting with the AI Computer service
|
5
|
-
Project-URL: Homepage, https://github.com/ColeMurray/ai-computer-client-python
|
6
|
-
Project-URL: Documentation, https://github.com/ColeMurray/ai-computer-client-python#readme
|
7
|
-
Author: AI Computer
|
8
|
-
License: MIT
|
9
|
-
License-File: LICENSE
|
10
|
-
Classifier: Development Status :: 4 - Beta
|
11
|
-
Classifier: Intended Audience :: Developers
|
12
|
-
Classifier: License :: OSI Approved :: MIT License
|
13
|
-
Classifier: Programming Language :: Python :: 3
|
14
|
-
Classifier: Programming Language :: Python :: 3.7
|
15
|
-
Classifier: Programming Language :: Python :: 3.8
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
17
|
-
Classifier: Programming Language :: Python :: 3.10
|
18
|
-
Classifier: Programming Language :: Python :: 3.11
|
19
|
-
Requires-Python: >=3.7
|
20
|
-
Requires-Dist: aiohttp>=3.8.0
|
21
|
-
Requires-Dist: typing-extensions>=4.0.0
|
22
|
-
Provides-Extra: dev
|
23
|
-
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
|
24
|
-
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
25
|
-
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
26
|
-
Description-Content-Type: text/markdown
|
27
|
-
|
28
|
-
# AI Computer Python Client
|
29
|
-
|
30
|
-
A Python client for interacting with the AI Computer service. This client provides a simple interface for executing Python code in an isolated sandbox environment.
|
31
|
-
|
32
|
-
## Installation
|
33
|
-
|
34
|
-
```bash
|
35
|
-
pip install ai-computer-client
|
36
|
-
```
|
37
|
-
|
38
|
-
## Quick Start
|
39
|
-
|
40
|
-
```python
|
41
|
-
import asyncio
|
42
|
-
from ai_computer import SandboxClient
|
43
|
-
|
44
|
-
async def main():
|
45
|
-
# Initialize the client
|
46
|
-
client = SandboxClient()
|
47
|
-
|
48
|
-
# Setup the client (gets token and creates sandbox)
|
49
|
-
setup_response = await client.setup()
|
50
|
-
if not setup_response.success:
|
51
|
-
print(f"Setup failed: {setup_response.error}")
|
52
|
-
return
|
53
|
-
|
54
|
-
try:
|
55
|
-
# Example 1: Simple code execution
|
56
|
-
code = """x = 10
|
57
|
-
y = 20
|
58
|
-
result = x + y
|
59
|
-
print(f"The sum is: {result}")"""
|
60
|
-
|
61
|
-
print("\nExample 1: Simple execution")
|
62
|
-
print("-" * 50)
|
63
|
-
response = await client.execute_code(code)
|
64
|
-
if response.success:
|
65
|
-
print("Execution result:", response.data)
|
66
|
-
else:
|
67
|
-
print("Execution failed:", response.error)
|
68
|
-
|
69
|
-
# Example 2: Streaming execution
|
70
|
-
code = """import time
|
71
|
-
|
72
|
-
for i in range(5):
|
73
|
-
print(f"Processing step {i + 1}")
|
74
|
-
time.sleep(1) # Simulate work
|
75
|
-
|
76
|
-
result = "Calculation complete!"
|
77
|
-
print(result)"""
|
78
|
-
|
79
|
-
print("\nExample 2: Streaming execution")
|
80
|
-
print("-" * 50)
|
81
|
-
async for event in client.execute_code_stream(code):
|
82
|
-
if event.type == 'stdout':
|
83
|
-
print(f"Output: {event.data}")
|
84
|
-
elif event.type == 'stderr':
|
85
|
-
print(f"Error: {event.data}")
|
86
|
-
elif event.type == 'error':
|
87
|
-
print(f"Execution error: {event.data}")
|
88
|
-
break
|
89
|
-
elif event.type == 'completed':
|
90
|
-
print("Execution completed")
|
91
|
-
break
|
92
|
-
|
93
|
-
finally:
|
94
|
-
# Clean up
|
95
|
-
await client.cleanup()
|
96
|
-
|
97
|
-
if __name__ == "__main__":
|
98
|
-
asyncio.run(main())
|
99
|
-
```
|
100
|
-
|
101
|
-
Example output:
|
102
|
-
```
|
103
|
-
Example 1: Simple execution
|
104
|
-
--------------------------------------------------
|
105
|
-
Execution result: {'output': 'The sum is: 30\n', 'sandbox_id': '06a30496-b535-47b0-9fe7-34f7ec483cd7'}
|
106
|
-
|
107
|
-
Example 2: Streaming execution
|
108
|
-
--------------------------------------------------
|
109
|
-
Output: Processing step 1
|
110
|
-
Output: Processing step 2
|
111
|
-
Output: Processing step 3
|
112
|
-
Output: Processing step 4
|
113
|
-
Output: Processing step 5
|
114
|
-
Output: Calculation complete!
|
115
|
-
Execution completed
|
116
|
-
```
|
117
|
-
|
118
|
-
## Features
|
119
|
-
|
120
|
-
- Asynchronous API for efficient execution
|
121
|
-
- Real-time streaming of code output
|
122
|
-
- Automatic sandbox management
|
123
|
-
- Error handling and timeouts
|
124
|
-
- Type hints for better IDE support
|
125
|
-
|
126
|
-
## API Reference
|
127
|
-
|
128
|
-
### SandboxClient
|
129
|
-
|
130
|
-
The main client class for interacting with the AI Computer service.
|
131
|
-
|
132
|
-
```python
|
133
|
-
client = SandboxClient(base_url="http://aicomputer.dev")
|
134
|
-
```
|
135
|
-
|
136
|
-
#### Methods
|
137
|
-
|
138
|
-
##### `async setup() -> SandboxResponse`
|
139
|
-
Initialize the client and create a sandbox. This must be called before executing any code.
|
140
|
-
|
141
|
-
```python
|
142
|
-
response = await client.setup()
|
143
|
-
if response.success:
|
144
|
-
print("Sandbox ready")
|
145
|
-
```
|
146
|
-
|
147
|
-
##### `async execute_code(code: str, timeout: int = 30) -> SandboxResponse`
|
148
|
-
Execute Python code and return the combined output.
|
149
|
-
|
150
|
-
```python
|
151
|
-
code = """
|
152
|
-
x = 10
|
153
|
-
y = 20
|
154
|
-
result = x + y
|
155
|
-
print(f"The sum is: {result}")
|
156
|
-
"""
|
157
|
-
|
158
|
-
response = await client.execute_code(code)
|
159
|
-
if response.success:
|
160
|
-
print("Output:", response.data['output'])
|
161
|
-
```
|
162
|
-
|
163
|
-
##### `async execute_code_stream(code: str, timeout: int = 30) -> AsyncGenerator[StreamEvent, None]`
|
164
|
-
Execute Python code and stream the output in real-time.
|
165
|
-
|
166
|
-
```python
|
167
|
-
async for event in client.execute_code_stream(code):
|
168
|
-
if event.type == 'stdout':
|
169
|
-
print("Output:", event.data)
|
170
|
-
elif event.type == 'stderr':
|
171
|
-
print("Error:", event.data)
|
172
|
-
```
|
173
|
-
|
174
|
-
##### `async cleanup() -> SandboxResponse`
|
175
|
-
Delete the sandbox and clean up resources.
|
176
|
-
|
177
|
-
```python
|
178
|
-
await client.cleanup()
|
179
|
-
```
|
180
|
-
|
181
|
-
### Response Types
|
182
|
-
|
183
|
-
#### SandboxResponse
|
184
|
-
```python
|
185
|
-
@dataclass
|
186
|
-
class SandboxResponse:
|
187
|
-
success: bool
|
188
|
-
data: Optional[Dict] = None
|
189
|
-
error: Optional[str] = None
|
190
|
-
```
|
191
|
-
|
192
|
-
#### StreamEvent
|
193
|
-
```python
|
194
|
-
@dataclass
|
195
|
-
class StreamEvent:
|
196
|
-
type: str # 'stdout', 'stderr', 'error', 'completed'
|
197
|
-
data: str
|
198
|
-
```
|
199
|
-
|
200
|
-
## Development
|
201
|
-
|
202
|
-
### Running Tests
|
203
|
-
|
204
|
-
```bash
|
205
|
-
# Install development dependencies
|
206
|
-
pip install -e ".[dev]"
|
207
|
-
|
208
|
-
# Run tests
|
209
|
-
pytest
|
210
|
-
```
|
211
|
-
|
212
|
-
### Contributing
|
213
|
-
|
214
|
-
1. Fork the repository
|
215
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
216
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
217
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
218
|
-
5. Open a Pull Request
|
219
|
-
|
220
|
-
## License
|
221
|
-
|
222
|
-
MIT License
|
@@ -1,6 +0,0 @@
|
|
1
|
-
ai_computer/__init__.py,sha256=41ibex5hg-OZ28VsmhU2RqxzzV6YEUD6vLbuNSddRsk,197
|
2
|
-
ai_computer/client.py,sha256=--gmDUzABePoV7XWvo-zMH69Jg3JQR1vbotwEoAHtSg,28080
|
3
|
-
ai_computer_client-0.3.0.dist-info/METADATA,sha256=rhUG0es12ZwHZApMsT8CBna_BB_2VJSrpMVr9Kc3wcs,5654
|
4
|
-
ai_computer_client-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
ai_computer_client-0.3.0.dist-info/licenses/LICENSE,sha256=N_0S5G1Wik2LWVDViJMAM0Z-6vTBX1bvDjb8vouBA-c,1068
|
6
|
-
ai_computer_client-0.3.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|