ai-computer-client 0.3.2__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)}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ai-computer-client
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Python client for interacting with the AI Computer service
5
5
  Project-URL: Homepage, https://github.com/ColeMurray/ai-computer-client-python
6
6
  Project-URL: Documentation, https://github.com/ColeMurray/ai-computer-client-python#readme
@@ -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,,
@@ -1,6 +0,0 @@
1
- ai_computer/__init__.py,sha256=GlAloQ10Yg8fIBKNj9HXovNHWAq9PsazLtYCIS1jOig,197
2
- ai_computer/client.py,sha256=fo0OxmbZPYsSCWeYWkBzEGGDQ7bvx0qqvijDkr8zO8M,28733
3
- ai_computer_client-0.3.2.dist-info/METADATA,sha256=aFEKdik1aa7F2hBhhWImmdewJgekuXnqDU5QJoPKzpU,5658
4
- ai_computer_client-0.3.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- ai_computer_client-0.3.2.dist-info/licenses/LICENSE,sha256=N_0S5G1Wik2LWVDViJMAM0Z-6vTBX1bvDjb8vouBA-c,1068
6
- ai_computer_client-0.3.2.dist-info/RECORD,,