ai-computer-client 0.1.0__tar.gz → 0.2.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ai-computer-client
3
- Version: 0.1.0
3
+ Version: 0.2.0
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
@@ -50,16 +50,34 @@ async def main():
50
50
  if not setup_response.success:
51
51
  print(f"Setup failed: {setup_response.error}")
52
52
  return
53
-
53
+
54
54
  try:
55
- # Execute some code
56
- code = """
57
- print('Hello from AI Computer!')
58
- result = 42
59
- print(f'The answer is {result}')
60
- """
55
+ # Example 1: Simple code execution
56
+ code = """x = 10
57
+ y = 20
58
+ result = x + y
59
+ print(f"The sum is: {result}")"""
61
60
 
62
- # Stream execution
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)
63
81
  async for event in client.execute_code_stream(code):
64
82
  if event.type == 'stdout':
65
83
  print(f"Output: {event.data}")
@@ -80,10 +98,27 @@ if __name__ == "__main__":
80
98
  asyncio.run(main())
81
99
  ```
82
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
+
83
118
  ## Features
84
119
 
85
- - Asynchronous API
86
- - Streaming execution output
120
+ - Asynchronous API for efficient execution
121
+ - Real-time streaming of code output
87
122
  - Automatic sandbox management
88
123
  - Error handling and timeouts
89
124
  - Type hints for better IDE support
@@ -94,20 +129,83 @@ if __name__ == "__main__":
94
129
 
95
130
  The main client class for interacting with the AI Computer service.
96
131
 
132
+ ```python
133
+ client = SandboxClient(base_url="http://aicomputer.dev")
134
+ ```
135
+
97
136
  #### Methods
98
137
 
99
- - `setup()`: Initialize the client and create a sandbox
100
- - `execute_code(code: str, timeout: int = 30)`: Execute code and return combined output
101
- - `execute_code_stream(code: str, timeout: int = 30)`: Execute code and stream output
102
- - `cleanup()`: Delete the sandbox
103
- - `wait_for_ready()`: Wait for sandbox to be ready
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
+ ```
104
199
 
105
200
  ## Development
106
201
 
107
202
  ### Running Tests
108
203
 
109
204
  ```bash
205
+ # Install development dependencies
110
206
  pip install -e ".[dev]"
207
+
208
+ # Run tests
111
209
  pytest
112
210
  ```
113
211
 
@@ -0,0 +1,195 @@
1
+ # AI Computer Python Client
2
+
3
+ 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.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install ai-computer-client
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ import asyncio
15
+ from ai_computer import SandboxClient
16
+
17
+ async def main():
18
+ # Initialize the client
19
+ client = SandboxClient()
20
+
21
+ # Setup the client (gets token and creates sandbox)
22
+ setup_response = await client.setup()
23
+ if not setup_response.success:
24
+ print(f"Setup failed: {setup_response.error}")
25
+ return
26
+
27
+ try:
28
+ # Example 1: Simple code execution
29
+ code = """x = 10
30
+ y = 20
31
+ result = x + y
32
+ print(f"The sum is: {result}")"""
33
+
34
+ print("\nExample 1: Simple execution")
35
+ print("-" * 50)
36
+ response = await client.execute_code(code)
37
+ if response.success:
38
+ print("Execution result:", response.data)
39
+ else:
40
+ print("Execution failed:", response.error)
41
+
42
+ # Example 2: Streaming execution
43
+ code = """import time
44
+
45
+ for i in range(5):
46
+ print(f"Processing step {i + 1}")
47
+ time.sleep(1) # Simulate work
48
+
49
+ result = "Calculation complete!"
50
+ print(result)"""
51
+
52
+ print("\nExample 2: Streaming execution")
53
+ print("-" * 50)
54
+ async for event in client.execute_code_stream(code):
55
+ if event.type == 'stdout':
56
+ print(f"Output: {event.data}")
57
+ elif event.type == 'stderr':
58
+ print(f"Error: {event.data}")
59
+ elif event.type == 'error':
60
+ print(f"Execution error: {event.data}")
61
+ break
62
+ elif event.type == 'completed':
63
+ print("Execution completed")
64
+ break
65
+
66
+ finally:
67
+ # Clean up
68
+ await client.cleanup()
69
+
70
+ if __name__ == "__main__":
71
+ asyncio.run(main())
72
+ ```
73
+
74
+ Example output:
75
+ ```
76
+ Example 1: Simple execution
77
+ --------------------------------------------------
78
+ Execution result: {'output': 'The sum is: 30\n', 'sandbox_id': '06a30496-b535-47b0-9fe7-34f7ec483cd7'}
79
+
80
+ Example 2: Streaming execution
81
+ --------------------------------------------------
82
+ Output: Processing step 1
83
+ Output: Processing step 2
84
+ Output: Processing step 3
85
+ Output: Processing step 4
86
+ Output: Processing step 5
87
+ Output: Calculation complete!
88
+ Execution completed
89
+ ```
90
+
91
+ ## Features
92
+
93
+ - Asynchronous API for efficient execution
94
+ - Real-time streaming of code output
95
+ - Automatic sandbox management
96
+ - Error handling and timeouts
97
+ - Type hints for better IDE support
98
+
99
+ ## API Reference
100
+
101
+ ### SandboxClient
102
+
103
+ The main client class for interacting with the AI Computer service.
104
+
105
+ ```python
106
+ client = SandboxClient(base_url="http://aicomputer.dev")
107
+ ```
108
+
109
+ #### Methods
110
+
111
+ ##### `async setup() -> SandboxResponse`
112
+ Initialize the client and create a sandbox. This must be called before executing any code.
113
+
114
+ ```python
115
+ response = await client.setup()
116
+ if response.success:
117
+ print("Sandbox ready")
118
+ ```
119
+
120
+ ##### `async execute_code(code: str, timeout: int = 30) -> SandboxResponse`
121
+ Execute Python code and return the combined output.
122
+
123
+ ```python
124
+ code = """
125
+ x = 10
126
+ y = 20
127
+ result = x + y
128
+ print(f"The sum is: {result}")
129
+ """
130
+
131
+ response = await client.execute_code(code)
132
+ if response.success:
133
+ print("Output:", response.data['output'])
134
+ ```
135
+
136
+ ##### `async execute_code_stream(code: str, timeout: int = 30) -> AsyncGenerator[StreamEvent, None]`
137
+ Execute Python code and stream the output in real-time.
138
+
139
+ ```python
140
+ async for event in client.execute_code_stream(code):
141
+ if event.type == 'stdout':
142
+ print("Output:", event.data)
143
+ elif event.type == 'stderr':
144
+ print("Error:", event.data)
145
+ ```
146
+
147
+ ##### `async cleanup() -> SandboxResponse`
148
+ Delete the sandbox and clean up resources.
149
+
150
+ ```python
151
+ await client.cleanup()
152
+ ```
153
+
154
+ ### Response Types
155
+
156
+ #### SandboxResponse
157
+ ```python
158
+ @dataclass
159
+ class SandboxResponse:
160
+ success: bool
161
+ data: Optional[Dict] = None
162
+ error: Optional[str] = None
163
+ ```
164
+
165
+ #### StreamEvent
166
+ ```python
167
+ @dataclass
168
+ class StreamEvent:
169
+ type: str # 'stdout', 'stderr', 'error', 'completed'
170
+ data: str
171
+ ```
172
+
173
+ ## Development
174
+
175
+ ### Running Tests
176
+
177
+ ```bash
178
+ # Install development dependencies
179
+ pip install -e ".[dev]"
180
+
181
+ # Run tests
182
+ pytest
183
+ ```
184
+
185
+ ### Contributing
186
+
187
+ 1. Fork the repository
188
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
189
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
190
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
191
+ 5. Open a Pull Request
192
+
193
+ ## License
194
+
195
+ MIT License
@@ -1,7 +1,7 @@
1
1
  import aiohttp
2
2
  import json
3
3
  import asyncio
4
- from typing import Optional, Dict, AsyncGenerator, Union
4
+ from typing import Optional, Dict, AsyncGenerator, Union, List
5
5
  from dataclasses import dataclass
6
6
 
7
7
  @dataclass
@@ -241,6 +241,59 @@ class SandboxClient:
241
241
  except Exception as e:
242
242
  yield StreamEvent(type="error", data=f"Connection error: {str(e)}")
243
243
 
244
+ async def execute_shell(
245
+ self,
246
+ command: str,
247
+ args: Optional[List[str]] = None,
248
+ timeout: int = 30
249
+ ) -> SandboxResponse:
250
+ """Execute a shell command in the sandbox.
251
+
252
+ Args:
253
+ command: The shell command to execute
254
+ args: Optional list of arguments for the command
255
+ timeout: Maximum execution time in seconds
256
+
257
+ Returns:
258
+ SandboxResponse containing execution results
259
+ """
260
+ if not self.token or not self.sandbox_id:
261
+ return SandboxResponse(success=False, error="Client not properly initialized. Call setup() first")
262
+
263
+ # Ensure sandbox is ready
264
+ ready = await self.wait_for_ready()
265
+ if not ready.success:
266
+ return ready
267
+
268
+ headers = {
269
+ "Authorization": f"Bearer {self.token}",
270
+ "Content-Type": "application/json"
271
+ }
272
+
273
+ data = {
274
+ "command": command,
275
+ "args": args or [],
276
+ "timeout": timeout
277
+ }
278
+
279
+ try:
280
+ async with aiohttp.ClientSession() as session:
281
+ async with session.post(
282
+ f"{self.base_url}/api/v1/sandbox/{self.sandbox_id}/execute/shell",
283
+ headers=headers,
284
+ json=data
285
+ ) as response:
286
+ if response.status != 200:
287
+ error_text = await response.text()
288
+ return SandboxResponse(success=False, error=error_text)
289
+
290
+ # Parse the response
291
+ result = await response.json()
292
+ return SandboxResponse(success=True, data=result)
293
+
294
+ except Exception as e:
295
+ return SandboxResponse(success=False, error=f"Connection error: {str(e)}")
296
+
244
297
  async def cleanup(self) -> SandboxResponse:
245
298
  """Delete the sandbox.
246
299
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "ai-computer-client"
7
- version = "0.1.0"
7
+ version = "0.2.0"
8
8
  description = "Python client for interacting with the AI Computer service"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.7"
@@ -1,97 +0,0 @@
1
- # AI Computer Python Client
2
-
3
- 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.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pip install ai-computer-client
9
- ```
10
-
11
- ## Quick Start
12
-
13
- ```python
14
- import asyncio
15
- from ai_computer import SandboxClient
16
-
17
- async def main():
18
- # Initialize the client
19
- client = SandboxClient()
20
-
21
- # Setup the client (gets token and creates sandbox)
22
- setup_response = await client.setup()
23
- if not setup_response.success:
24
- print(f"Setup failed: {setup_response.error}")
25
- return
26
-
27
- try:
28
- # Execute some code
29
- code = """
30
- print('Hello from AI Computer!')
31
- result = 42
32
- print(f'The answer is {result}')
33
- """
34
-
35
- # Stream execution
36
- async for event in client.execute_code_stream(code):
37
- if event.type == 'stdout':
38
- print(f"Output: {event.data}")
39
- elif event.type == 'stderr':
40
- print(f"Error: {event.data}")
41
- elif event.type == 'error':
42
- print(f"Execution error: {event.data}")
43
- break
44
- elif event.type == 'completed':
45
- print("Execution completed")
46
- break
47
-
48
- finally:
49
- # Clean up
50
- await client.cleanup()
51
-
52
- if __name__ == "__main__":
53
- asyncio.run(main())
54
- ```
55
-
56
- ## Features
57
-
58
- - Asynchronous API
59
- - Streaming execution output
60
- - Automatic sandbox management
61
- - Error handling and timeouts
62
- - Type hints for better IDE support
63
-
64
- ## API Reference
65
-
66
- ### SandboxClient
67
-
68
- The main client class for interacting with the AI Computer service.
69
-
70
- #### Methods
71
-
72
- - `setup()`: Initialize the client and create a sandbox
73
- - `execute_code(code: str, timeout: int = 30)`: Execute code and return combined output
74
- - `execute_code_stream(code: str, timeout: int = 30)`: Execute code and stream output
75
- - `cleanup()`: Delete the sandbox
76
- - `wait_for_ready()`: Wait for sandbox to be ready
77
-
78
- ## Development
79
-
80
- ### Running Tests
81
-
82
- ```bash
83
- pip install -e ".[dev]"
84
- pytest
85
- ```
86
-
87
- ### Contributing
88
-
89
- 1. Fork the repository
90
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
91
- 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
92
- 4. Push to the branch (`git push origin feature/amazing-feature`)
93
- 5. Open a Pull Request
94
-
95
- ## License
96
-
97
- MIT License