ai-computer-client 0.2.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 CHANGED
@@ -1,4 +1,4 @@
1
- from .client import SandboxClient, SandboxResponse, StreamEvent
1
+ from .client import SandboxClient, SandboxResponse, StreamEvent, FileOperationResponse
2
2
 
3
- __version__ = "0.1.0"
4
- __all__ = ["SandboxClient", "SandboxResponse", "StreamEvent"]
3
+ __version__ = "0.3.1"
4
+ __all__ = ["SandboxClient", "SandboxResponse", "StreamEvent", "FileOperationResponse"]
ai_computer/client.py CHANGED
@@ -1,8 +1,11 @@
1
1
  import aiohttp
2
2
  import json
3
3
  import asyncio
4
- from typing import Optional, Dict, AsyncGenerator, Union, List
4
+ from typing import Optional, Dict, AsyncGenerator, Union, List, BinaryIO
5
5
  from dataclasses import dataclass
6
+ import os
7
+ import mimetypes
8
+ from pathlib import Path
6
9
 
7
10
  @dataclass
8
11
  class SandboxResponse:
@@ -28,6 +31,25 @@ class StreamEvent:
28
31
  type: str
29
32
  data: str
30
33
 
34
+ @dataclass
35
+ class FileOperationResponse:
36
+ """Response from file operations.
37
+
38
+ Attributes:
39
+ success: Whether the operation was successful
40
+ filename: Name of the file
41
+ size: Size of the file in bytes
42
+ path: Path where the file was saved
43
+ message: Optional status message
44
+ error: Optional error message if operation failed
45
+ """
46
+ success: bool
47
+ filename: Optional[str] = None
48
+ size: Optional[int] = None
49
+ path: Optional[str] = None
50
+ message: Optional[str] = None
51
+ error: Optional[str] = None
52
+
31
53
  class SandboxClient:
32
54
  """Client for interacting with the AI Sandbox service.
33
55
 
@@ -315,4 +337,425 @@ class SandboxClient:
315
337
  return SandboxResponse(success=True, data=data)
316
338
  else:
317
339
  text = await response.text()
318
- return SandboxResponse(success=False, error=text)
340
+ return SandboxResponse(success=False, error=text)
341
+
342
+ async def upload_file(
343
+ self,
344
+ file_path: Union[str, Path],
345
+ destination: str = "/workspace",
346
+ chunk_size: int = 1024 * 1024, # 1MB chunks
347
+ timeout: int = 300 # 5 minutes
348
+ ) -> FileOperationResponse:
349
+ """Upload a file to the sandbox environment.
350
+
351
+ Args:
352
+ file_path: Path to the file to upload
353
+ destination: Destination path in the sandbox (absolute path starting with /)
354
+ chunk_size: Size of chunks for reading large files
355
+ timeout: Maximum upload time in seconds
356
+
357
+ Returns:
358
+ FileOperationResponse containing upload results
359
+ """
360
+ if not self.token or not self.sandbox_id:
361
+ return FileOperationResponse(
362
+ success=False,
363
+ error="Client not properly initialized. Call setup() first"
364
+ )
365
+
366
+ # Ensure sandbox is ready
367
+ ready = await self.wait_for_ready()
368
+ if not ready.success:
369
+ return FileOperationResponse(
370
+ success=False,
371
+ error=ready.error or "Sandbox not ready"
372
+ )
373
+
374
+ # Convert to Path object and validate file
375
+ file_path = Path(file_path)
376
+ if not file_path.exists():
377
+ return FileOperationResponse(
378
+ success=False,
379
+ error=f"File not found: {file_path}"
380
+ )
381
+
382
+ if not file_path.is_file():
383
+ return FileOperationResponse(
384
+ success=False,
385
+ error=f"Not a file: {file_path}"
386
+ )
387
+
388
+ # Get file size and validate
389
+ file_size = file_path.stat().st_size
390
+ if file_size > 100 * 1024 * 1024: # 100MB limit
391
+ return FileOperationResponse(
392
+ success=False,
393
+ error="File too large. Maximum size is 100MB"
394
+ )
395
+
396
+ try:
397
+ # Prepare the upload
398
+ headers = {
399
+ "Authorization": f"Bearer {self.token}"
400
+ }
401
+
402
+ # Guess content type
403
+ content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
404
+
405
+ # Prepare multipart form data
406
+ data = aiohttp.FormData()
407
+ data.add_field('file',
408
+ open(file_path, 'rb').read(),
409
+ filename=file_path.name,
410
+ content_type=content_type)
411
+ data.add_field('path', destination)
412
+
413
+ timeout_settings = aiohttp.ClientTimeout(
414
+ total=timeout,
415
+ connect=30,
416
+ sock_connect=30,
417
+ sock_read=timeout
418
+ )
419
+
420
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
421
+ async with session.post(
422
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/upload",
423
+ headers=headers,
424
+ data=data
425
+ ) as response:
426
+ if response.status != 200:
427
+ error_text = await response.text()
428
+ return FileOperationResponse(
429
+ success=False,
430
+ error=f"Upload failed: {error_text}"
431
+ )
432
+
433
+ result = await response.json()
434
+ return FileOperationResponse(
435
+ success=True,
436
+ filename=result.get("filename"),
437
+ size=result.get("size"),
438
+ path=result.get("path"),
439
+ message=result.get("message")
440
+ )
441
+
442
+ except asyncio.TimeoutError:
443
+ return FileOperationResponse(
444
+ success=False,
445
+ error=f"Upload timed out after {timeout} seconds"
446
+ )
447
+ except Exception as e:
448
+ return FileOperationResponse(
449
+ success=False,
450
+ error=f"Upload failed: {str(e)}"
451
+ )
452
+
453
+ async def download_file(
454
+ self,
455
+ sandbox_path: str,
456
+ local_path: Optional[Union[str, Path]] = None,
457
+ chunk_size: int = 8192, # 8KB chunks for download
458
+ timeout: int = 300 # 5 minutes
459
+ ) -> FileOperationResponse:
460
+ """Download a file from the sandbox environment.
461
+
462
+ Args:
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.
465
+ local_path: Local path to save the file (default: current directory with original filename)
466
+ chunk_size: Size of chunks for downloading large files
467
+ timeout: Maximum download time in seconds
468
+
469
+ Returns:
470
+ FileOperationResponse containing download results
471
+ """
472
+ if not self.token or not self.sandbox_id:
473
+ return FileOperationResponse(
474
+ success=False,
475
+ error="Client not properly initialized. Call setup() first"
476
+ )
477
+
478
+ # Ensure sandbox is ready
479
+ ready = await self.wait_for_ready()
480
+ if not ready.success:
481
+ return FileOperationResponse(
482
+ success=False,
483
+ error=ready.error or "Sandbox not ready"
484
+ )
485
+
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}"
491
+
492
+ # Determine local path
493
+ if local_path is None:
494
+ local_path = Path(os.path.basename(sandbox_path))
495
+ else:
496
+ local_path = Path(local_path)
497
+
498
+ # Create parent directories if they don't exist
499
+ local_path.parent.mkdir(parents=True, exist_ok=True)
500
+
501
+ try:
502
+ timeout_settings = aiohttp.ClientTimeout(
503
+ total=timeout,
504
+ connect=30,
505
+ sock_connect=30,
506
+ sock_read=timeout
507
+ )
508
+
509
+ headers = {
510
+ "Authorization": f"Bearer {self.token}"
511
+ }
512
+
513
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
514
+ async with session.get(
515
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download{clean_path}",
516
+ headers=headers
517
+ ) as response:
518
+ if response.status != 200:
519
+ error_text = await response.text()
520
+ return FileOperationResponse(
521
+ success=False,
522
+ error=f"Download failed: {error_text}"
523
+ )
524
+
525
+ # Get content length if available
526
+ total_size = int(response.headers.get('content-length', 0))
527
+
528
+ # Download the file in chunks
529
+ downloaded_size = 0
530
+ try:
531
+ with open(local_path, 'wb') as f:
532
+ async for chunk in response.content.iter_chunked(chunk_size):
533
+ f.write(chunk)
534
+ downloaded_size += len(chunk)
535
+
536
+ return FileOperationResponse(
537
+ success=True,
538
+ filename=local_path.name,
539
+ size=downloaded_size or total_size,
540
+ path=str(local_path.absolute()),
541
+ message="File downloaded successfully"
542
+ )
543
+ except Exception as e:
544
+ # Clean up partial download
545
+ if local_path.exists():
546
+ local_path.unlink()
547
+ raise e
548
+
549
+ except asyncio.TimeoutError:
550
+ # Clean up partial download
551
+ if local_path.exists():
552
+ local_path.unlink()
553
+ return FileOperationResponse(
554
+ success=False,
555
+ error=f"Download timed out after {timeout} seconds"
556
+ )
557
+ except Exception as e:
558
+ # Clean up partial download
559
+ if local_path.exists():
560
+ local_path.unlink()
561
+ return FileOperationResponse(
562
+ success=False,
563
+ error=f"Download failed: {str(e)}"
564
+ )
565
+
566
+ async def upload_bytes(
567
+ self,
568
+ content: Union[bytes, BinaryIO],
569
+ filename: str,
570
+ destination: str = "/workspace",
571
+ content_type: Optional[str] = None,
572
+ timeout: int = 300 # 5 minutes
573
+ ) -> FileOperationResponse:
574
+ """Upload bytes or a file-like object to the sandbox environment.
575
+
576
+ Args:
577
+ content: Bytes or file-like object to upload
578
+ filename: Name to give the file in the sandbox
579
+ destination: Destination path in the sandbox (absolute path starting with /)
580
+ content_type: Optional MIME type (will be guessed from filename if not provided)
581
+ timeout: Maximum upload time in seconds
582
+
583
+ Returns:
584
+ FileOperationResponse containing upload results
585
+ """
586
+ if not self.token or not self.sandbox_id:
587
+ return FileOperationResponse(
588
+ success=False,
589
+ error="Client not properly initialized. Call setup() first"
590
+ )
591
+
592
+ # Ensure sandbox is ready
593
+ ready = await self.wait_for_ready()
594
+ if not ready.success:
595
+ return FileOperationResponse(
596
+ success=False,
597
+ error=ready.error or "Sandbox not ready"
598
+ )
599
+
600
+ try:
601
+ # Handle both bytes and file-like objects
602
+ if isinstance(content, bytes):
603
+ file_obj = content
604
+ content_length = len(content)
605
+ else:
606
+ # Ensure we're at the start of the file
607
+ if hasattr(content, 'seek'):
608
+ content.seek(0)
609
+ file_obj = content
610
+ # Try to get content length if possible
611
+ content_length = None
612
+ if hasattr(content, 'seek') and hasattr(content, 'tell'):
613
+ try:
614
+ current_pos = content.tell()
615
+ content.seek(0, os.SEEK_END)
616
+ content_length = content.tell()
617
+ content.seek(current_pos)
618
+ except (OSError, IOError):
619
+ pass
620
+
621
+ # Validate size if we can determine it
622
+ if content_length and content_length > 100 * 1024 * 1024: # 100MB limit
623
+ return FileOperationResponse(
624
+ success=False,
625
+ error="Content too large. Maximum size is 100MB"
626
+ )
627
+
628
+ # Prepare the upload
629
+ headers = {
630
+ "Authorization": f"Bearer {self.token}"
631
+ }
632
+
633
+ # Guess content type if not provided
634
+ if not content_type:
635
+ content_type = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
636
+
637
+ # Prepare multipart form data
638
+ data = aiohttp.FormData()
639
+ data.add_field('file',
640
+ file_obj,
641
+ filename=filename,
642
+ content_type=content_type)
643
+ data.add_field('path', destination)
644
+
645
+ timeout_settings = aiohttp.ClientTimeout(
646
+ total=timeout,
647
+ connect=30,
648
+ sock_connect=30,
649
+ sock_read=timeout
650
+ )
651
+
652
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
653
+ async with session.post(
654
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/upload",
655
+ headers=headers,
656
+ data=data
657
+ ) as response:
658
+ if response.status != 200:
659
+ error_text = await response.text()
660
+ return FileOperationResponse(
661
+ success=False,
662
+ error=f"Upload failed: {error_text}"
663
+ )
664
+
665
+ result = await response.json()
666
+ return FileOperationResponse(
667
+ success=True,
668
+ filename=result.get("filename"),
669
+ size=result.get("size"),
670
+ path=result.get("path"),
671
+ message=result.get("message")
672
+ )
673
+
674
+ except asyncio.TimeoutError:
675
+ return FileOperationResponse(
676
+ success=False,
677
+ error=f"Upload timed out after {timeout} seconds"
678
+ )
679
+ except Exception as e:
680
+ return FileOperationResponse(
681
+ success=False,
682
+ error=f"Upload failed: {str(e)}"
683
+ )
684
+
685
+ async def download_bytes(
686
+ self,
687
+ sandbox_path: str,
688
+ chunk_size: int = 8192, # 8KB chunks for download
689
+ timeout: int = 300 # 5 minutes
690
+ ) -> Union[bytes, FileOperationResponse]:
691
+ """Download a file from the sandbox environment into memory.
692
+
693
+ Args:
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.
696
+ chunk_size: Size of chunks for downloading large files
697
+ timeout: Maximum download time in seconds
698
+
699
+ Returns:
700
+ On success: The file contents as bytes
701
+ On failure: FileOperationResponse with error details
702
+ """
703
+ if not self.token or not self.sandbox_id:
704
+ return FileOperationResponse(
705
+ success=False,
706
+ error="Client not properly initialized. Call setup() first"
707
+ )
708
+
709
+ # Ensure sandbox is ready
710
+ ready = await self.wait_for_ready()
711
+ if not ready.success:
712
+ return FileOperationResponse(
713
+ success=False,
714
+ error=ready.error or "Sandbox not ready"
715
+ )
716
+
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}"
722
+
723
+ try:
724
+ timeout_settings = aiohttp.ClientTimeout(
725
+ total=timeout,
726
+ connect=30,
727
+ sock_connect=30,
728
+ sock_read=timeout
729
+ )
730
+
731
+ headers = {
732
+ "Authorization": f"Bearer {self.token}"
733
+ }
734
+
735
+ async with aiohttp.ClientSession(timeout=timeout_settings) as session:
736
+ async with session.get(
737
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/files/download{clean_path}",
738
+ headers=headers
739
+ ) as response:
740
+ if response.status != 200:
741
+ error_text = await response.text()
742
+ return FileOperationResponse(
743
+ success=False,
744
+ error=f"Download failed: {error_text}"
745
+ )
746
+
747
+ # Read the entire response into memory
748
+ content = await response.read()
749
+
750
+ return content
751
+
752
+ except asyncio.TimeoutError:
753
+ return FileOperationResponse(
754
+ success=False,
755
+ error=f"Download timed out after {timeout} seconds"
756
+ )
757
+ except Exception as e:
758
+ return FileOperationResponse(
759
+ success=False,
760
+ error=f"Download failed: {str(e)}"
761
+ )
@@ -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.2.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=g2yDiDT6i24MV3Y2JNfk2ZFkYvxvrN6NXywjZPslXDY,149
2
- ai_computer/client.py,sha256=8LgOSO0AD3R8lljblzktIwPLwoAdmX4n-JG58ebd5xY,12119
3
- ai_computer_client-0.2.0.dist-info/METADATA,sha256=3XRPWBjdJTIiErS_Kqnoc7iIOOO8LC2QIzc4k3AVHAc,5654
4
- ai_computer_client-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- ai_computer_client-0.2.0.dist-info/licenses/LICENSE,sha256=N_0S5G1Wik2LWVDViJMAM0Z-6vTBX1bvDjb8vouBA-c,1068
6
- ai_computer_client-0.2.0.dist-info/RECORD,,