ai-computer-client 0.3.1__py3-none-any.whl → 0.3.3__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.
@@ -0,0 +1,373 @@
1
+ import aiohttp
2
+ import asyncio
3
+ import os
4
+ import mimetypes
5
+ from pathlib import Path
6
+ from typing import Optional, Union, BinaryIO, Dict, Any
7
+
8
+ from .base import BaseSubmodule
9
+ from ..models import FileOperationResponse, SandboxResponse
10
+
11
+ class FileSystemModule(BaseSubmodule):
12
+ """File system operations for the sandbox environment.
13
+
14
+ This module provides methods for file operations such as uploading,
15
+ downloading, reading, and writing files in the sandbox.
16
+ """
17
+
18
+ async def upload_file(
19
+ self,
20
+ file_path: Union[str, Path],
21
+ destination: str = "/workspace",
22
+ chunk_size: int = 1024 * 1024, # 1MB chunks
23
+ timeout: int = 300 # 5 minutes
24
+ ) -> FileOperationResponse:
25
+ """Upload a file to the sandbox environment.
26
+
27
+ Args:
28
+ file_path: Path to the file to upload
29
+ destination: Destination path in the sandbox (absolute path starting with /)
30
+ chunk_size: Size of chunks for reading large files
31
+ timeout: Maximum upload time in seconds
32
+
33
+ Returns:
34
+ FileOperationResponse containing upload results
35
+ """
36
+ ready = await self._ensure_ready()
37
+ if not ready.success:
38
+ return FileOperationResponse(
39
+ success=False,
40
+ error=ready.error or "Sandbox not ready"
41
+ )
42
+
43
+ # Convert to Path object and validate file
44
+ file_path = Path(file_path)
45
+ if not file_path.exists():
46
+ return FileOperationResponse(
47
+ success=False,
48
+ error=f"File not found: {file_path}"
49
+ )
50
+
51
+ if not file_path.is_file():
52
+ return FileOperationResponse(
53
+ success=False,
54
+ error=f"Not a file: {file_path}"
55
+ )
56
+
57
+ # Get file size and validate
58
+ file_size = file_path.stat().st_size
59
+ if file_size > 100 * 1024 * 1024: # 100MB limit
60
+ return FileOperationResponse(
61
+ success=False,
62
+ error="File too large. Maximum size is 100MB"
63
+ )
64
+
65
+ try:
66
+ # Prepare the upload
67
+ headers = {
68
+ "Authorization": f"Bearer {self.token}"
69
+ }
70
+
71
+ # Guess content type
72
+ content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
73
+
74
+ # Prepare multipart form data
75
+ data = aiohttp.FormData()
76
+ data.add_field('file',
77
+ open(file_path, 'rb'), # Pass file object directly for streaming
78
+ filename=file_path.name,
79
+ content_type=content_type)
80
+ data.add_field('path', destination)
81
+
82
+ timeout_settings = aiohttp.ClientTimeout(
83
+ total=timeout,
84
+ connect=30,
85
+ sock_connect=30,
86
+ sock_read=timeout
87
+ )
88
+
89
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
90
+ async with session.post(
91
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/upload",
92
+ headers=headers,
93
+ data=data
94
+ ) as response:
95
+ if response.status != 200:
96
+ error_text = await response.text()
97
+ return FileOperationResponse(
98
+ success=False,
99
+ error=f"Upload failed: {error_text}"
100
+ )
101
+
102
+ result = await response.json()
103
+ return FileOperationResponse(
104
+ success=True,
105
+ filename=result.get("filename"),
106
+ size=result.get("size"),
107
+ path=result.get("path"),
108
+ message=result.get("message")
109
+ )
110
+
111
+ except asyncio.TimeoutError:
112
+ return FileOperationResponse(
113
+ success=False,
114
+ error=f"Upload timed out after {timeout} seconds"
115
+ )
116
+ except Exception as e:
117
+ return FileOperationResponse(
118
+ success=False,
119
+ error=f"Upload failed: {str(e)}"
120
+ )
121
+
122
+ async def download_file(
123
+ self,
124
+ remote_path: str,
125
+ local_path: Optional[Union[str, Path]] = None,
126
+ timeout: int = 300 # 5 minutes
127
+ ) -> FileOperationResponse:
128
+ """Download a file from the sandbox.
129
+
130
+ Args:
131
+ remote_path: Path to the file in the sandbox
132
+ local_path: Local path to save the file (if None, uses the filename from remote_path)
133
+ timeout: Maximum download time in seconds
134
+
135
+ Returns:
136
+ FileOperationResponse containing download results
137
+ """
138
+ ready = await self._ensure_ready()
139
+ if not ready.success:
140
+ return FileOperationResponse(
141
+ success=False,
142
+ error=ready.error or "Sandbox not ready"
143
+ )
144
+
145
+ # Ensure path is absolute and normalize any double slashes
146
+ if not remote_path.startswith('/'):
147
+ remote_path = f"/{remote_path}"
148
+ clean_path = '/'.join(part for part in remote_path.split('/') if part)
149
+ clean_path = f"/{clean_path}"
150
+
151
+ # Determine local path if not provided
152
+ if local_path is None:
153
+ local_path = os.path.basename(remote_path)
154
+ local_path = Path(local_path)
155
+
156
+ # Create parent directories if they don't exist
157
+ os.makedirs(local_path.parent, exist_ok=True)
158
+
159
+ headers = {
160
+ "Authorization": f"Bearer {self.token}"
161
+ }
162
+
163
+ params = {
164
+ "path": remote_path
165
+ }
166
+
167
+ timeout_settings = aiohttp.ClientTimeout(
168
+ total=timeout,
169
+ connect=30,
170
+ sock_connect=30,
171
+ sock_read=timeout
172
+ )
173
+
174
+ try:
175
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
176
+ async with session.get(
177
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download",
178
+ headers=headers,
179
+ params=params
180
+ ) as response:
181
+ if response.status != 200:
182
+ error_text = await response.text()
183
+ return FileOperationResponse(
184
+ success=False,
185
+ error=f"Download failed: {error_text}"
186
+ )
187
+
188
+ # Get content disposition header to extract filename
189
+ content_disposition = response.headers.get('Content-Disposition', '')
190
+ filename = os.path.basename(remote_path) # Default to basename of remote path
191
+
192
+ # Extract filename from content disposition if available
193
+ if 'filename=' in content_disposition:
194
+ filename = content_disposition.split('filename=')[1].strip('"\'')
195
+
196
+ # Save the file
197
+ with open(local_path, 'wb') as f:
198
+ size = 0
199
+ async for chunk in response.content.iter_chunked(1024 * 1024): # 1MB chunks
200
+ f.write(chunk)
201
+ size += len(chunk)
202
+
203
+ return FileOperationResponse(
204
+ success=True,
205
+ filename=filename,
206
+ size=size,
207
+ path=str(local_path),
208
+ message=f"File downloaded successfully to {local_path}"
209
+ )
210
+
211
+ except asyncio.TimeoutError:
212
+ # Clean up partial download
213
+ if local_path.exists():
214
+ local_path.unlink()
215
+ return FileOperationResponse(
216
+ success=False,
217
+ error=f"Download timed out after {timeout} seconds"
218
+ )
219
+ except Exception as e:
220
+ # Clean up partial download
221
+ if local_path.exists():
222
+ local_path.unlink()
223
+ return FileOperationResponse(
224
+ success=False,
225
+ error=f"Download failed: {str(e)}"
226
+ )
227
+
228
+ async def read_file(self, path: str, encoding: str = 'utf-8') -> SandboxResponse:
229
+ """Read a file from the sandbox and return its contents.
230
+
231
+ Args:
232
+ path: Path to the file in the sandbox
233
+ encoding: Text encoding to use (set to None for binary files)
234
+
235
+ Returns:
236
+ SandboxResponse with the file contents in the data field
237
+ """
238
+ ready = await self._ensure_ready()
239
+ if not ready.success:
240
+ return ready
241
+
242
+ # Ensure path is absolute and normalize any double slashes
243
+ if not path.startswith('/'):
244
+ path = f"/{path}"
245
+ clean_path = '/'.join(part for part in path.split('/') if part)
246
+ clean_path = f"/{clean_path}"
247
+
248
+ headers = {
249
+ "Authorization": f"Bearer {self.token}"
250
+ }
251
+
252
+ params = {
253
+ "path": clean_path
254
+ }
255
+
256
+ try:
257
+ async with aiohttp.ClientSession() as session:
258
+ async with session.get(
259
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download",
260
+ headers=headers,
261
+ params=params
262
+ ) as response:
263
+ if response.status != 200:
264
+ error_text = await response.text()
265
+ return SandboxResponse(
266
+ success=False,
267
+ error=f"Failed to read file: {error_text}"
268
+ )
269
+
270
+ # Read the content
271
+ content = await response.read()
272
+ size = len(content)
273
+
274
+ # Decode if needed
275
+ if encoding is not None:
276
+ try:
277
+ content = content.decode(encoding)
278
+ except UnicodeDecodeError:
279
+ return SandboxResponse(
280
+ success=False,
281
+ error=f"Failed to decode file with encoding {encoding}"
282
+ )
283
+
284
+ return SandboxResponse(
285
+ success=True,
286
+ data={
287
+ 'content': content,
288
+ 'size': size
289
+ }
290
+ )
291
+
292
+ except Exception as e:
293
+ return SandboxResponse(
294
+ success=False,
295
+ error=f"Failed to read file: {str(e)}"
296
+ )
297
+
298
+ async def write_file(
299
+ self,
300
+ path: str,
301
+ content: Union[str, bytes],
302
+ encoding: str = 'utf-8'
303
+ ) -> SandboxResponse:
304
+ """Write content to a file in the sandbox.
305
+
306
+ Args:
307
+ path: Path to the file in the sandbox
308
+ content: Content to write (string or bytes)
309
+ encoding: Text encoding to use (ignored for bytes content)
310
+
311
+ Returns:
312
+ SandboxResponse indicating success or failure
313
+ """
314
+ ready = await self._ensure_ready()
315
+ if not ready.success:
316
+ return ready
317
+
318
+ # Convert string to bytes if needed
319
+ if isinstance(content, str):
320
+ try:
321
+ content = content.encode(encoding)
322
+ except UnicodeEncodeError:
323
+ return SandboxResponse(
324
+ success=False,
325
+ error=f"Failed to encode content with encoding {encoding}"
326
+ )
327
+
328
+ # Ensure path is absolute and normalize any double slashes
329
+ if not path.startswith('/'):
330
+ path = f"/{path}"
331
+ clean_path = '/'.join(part for part in path.split('/') if part)
332
+ clean_path = f"/{clean_path}"
333
+
334
+ # Extract the filename and destination directory
335
+ filename = os.path.basename(path)
336
+ destination = os.path.dirname(path)
337
+
338
+ # Create a temporary file with the content
339
+ import tempfile
340
+ temp_file = tempfile.NamedTemporaryFile(delete=False)
341
+ try:
342
+ temp_file.write(content)
343
+ temp_file.close()
344
+
345
+ # Upload the temporary file
346
+ result = await self.upload_file(
347
+ file_path=temp_file.name,
348
+ destination=destination
349
+ )
350
+
351
+ if result.success:
352
+ return SandboxResponse(
353
+ success=True,
354
+ data={
355
+ 'path': result.path,
356
+ 'size': result.size
357
+ }
358
+ )
359
+ else:
360
+ return SandboxResponse(
361
+ success=False,
362
+ error=f"Failed to write file: {result.error}"
363
+ )
364
+ except Exception as e:
365
+ return SandboxResponse(
366
+ success=False,
367
+ error=f"Failed to write file: {str(e)}"
368
+ )
369
+ finally:
370
+ # Clean up the temporary file
371
+ if os.path.exists(temp_file.name):
372
+ os.unlink(temp_file.name)
373
+
@@ -0,0 +1,52 @@
1
+ import aiohttp
2
+ import asyncio
3
+ from typing import Optional, List, Dict, Any
4
+
5
+ from .base import BaseSubmodule
6
+ from ..models import SandboxResponse, StreamEvent
7
+
8
+ class ShellModule(BaseSubmodule):
9
+ """Shell command execution for the sandbox environment.
10
+
11
+ This module provides methods for executing shell commands in the sandbox.
12
+ """
13
+
14
+ async def execute(
15
+ self,
16
+ command: str,
17
+ args: Optional[List[str]] = None,
18
+ timeout: int = 30
19
+ ) -> SandboxResponse:
20
+ """Execute a shell command in the sandbox.
21
+
22
+ Args:
23
+ command: The shell command to execute
24
+ args: Optional list of arguments for the command
25
+ timeout: Maximum execution time in seconds
26
+
27
+ Returns:
28
+ SandboxResponse containing execution results
29
+ """
30
+ ready = await self._ensure_ready()
31
+ if not ready.success:
32
+ return ready
33
+
34
+ headers = self._get_headers()
35
+
36
+ data = {
37
+ "command": command,
38
+ "args": args or [],
39
+ "timeout": timeout
40
+ }
41
+
42
+ try:
43
+ async with aiohttp.ClientSession() as session:
44
+ async with session.post(
45
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/execute/shell",
46
+ headers=headers,
47
+ json=data
48
+ ) as response:
49
+ return await self._handle_response(response)
50
+
51
+ except Exception as e:
52
+ return SandboxResponse(success=False, error=f"Connection error: {str(e)}")
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.4
2
+ Name: ai-computer-client
3
+ Version: 0.3.3
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://api.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
@@ -0,0 +1,12 @@
1
+ ai_computer/__init__.py,sha256=0L4QSM3q4zWzYqNwisOqF_8ObcMdacmlXHbn55RX9YU,364
2
+ ai_computer/client.py,sha256=ozdVAp_I8l7lGI5zI8cQUN00RfJ_V8XhrkjspwHV5yI,14402
3
+ ai_computer/models.py,sha256=JG3gTKqCVrKlKsr4REAq5tb-WmZttn78LzxlrNiOJsQ,1214
4
+ ai_computer/submodules/__init__.py,sha256=kz4NTuF9r3i_VwTLWsyRHaHKereyq0kFe1HrfKQLtB4,162
5
+ ai_computer/submodules/base.py,sha256=3WoURENRnho26PkdghKM8S9s3-GYr11CwZidXhs-fFM,2530
6
+ ai_computer/submodules/code.py,sha256=U0cLcB7iu90QS3BbFP31oNPJYgpgvYSWzxakhxMa3-A,11216
7
+ ai_computer/submodules/filesystem.py,sha256=79gBefBIMj8qwFwTdVrpktOTqfvAfeZTf_jdTSxtcHs,13610
8
+ ai_computer/submodules/shell.py,sha256=lcy4CpgoJWN6tsGi-IUb6PQpywLSNOD3_lt_yvSU6Y8,1632
9
+ ai_computer_client-0.3.3.dist-info/METADATA,sha256=5kn2XaOh0LeQR7hexOsd1C1pOPrbr-pfWPLhDPUIaWU,5658
10
+ ai_computer_client-0.3.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ ai_computer_client-0.3.3.dist-info/licenses/LICENSE,sha256=N_0S5G1Wik2LWVDViJMAM0Z-6vTBX1bvDjb8vouBA-c,1068
12
+ ai_computer_client-0.3.3.dist-info/RECORD,,