Merge pull request #650 from LucaStngn/main

Refactor Python quickstart examples to use asyncio
This commit is contained in:
Sarina Li
2025-12-12 23:00:54 -05:00
committed by GitHub

View File

@@ -147,6 +147,7 @@ Connect to your Cua computer and perform basic interactions, such as taking scre
```python
import os
from computer import Computer
import asyncio
os.environ["CUA_API_KEY"] = "sk_cua-api01_..."
@@ -155,42 +156,117 @@ Connect to your Cua computer and perform basic interactions, such as taking scre
provider_type="cloud",
name="your-sandbox-name" # from CLI or website
)
await computer.run() # Connect to the sandbox
async def main():
await computer.run() # Connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
</Tab>
<Tab value="Linux on Docker">
```python
from computer import Computer
import asyncio
computer = Computer(
os_type="linux",
provider_type="docker",
image="trycua/cua-xfce:latest" # or "trycua/cua-ubuntu:latest"
)
await computer.run() # Launch & connect to the sandbox
async def main():
await computer.run() # Launch & connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
</Tab>
<Tab value="macOS Sandbox">
```python
from computer import Computer
import asyncio
computer = Computer(
os_type="macos",
provider_type="lume",
name="macos-sequoia-cua:latest"
)
await computer.run() # Launch & connect to the sandbox
async def main():
await computer.run() # Launch & connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
</Tab>
<Tab value="Windows Sandbox">
```python
from computer import Computer
import asyncio
computer = Computer(
os_type="windows",
provider_type="windows_sandbox"
)
await computer.run() # Launch & connect to the sandbox
async def main():
await computer.run() # Launch & connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
</Tab>
<Tab value="Your host desktop">
@@ -203,26 +279,32 @@ Connect to your Cua computer and perform basic interactions, such as taking scre
Then, use the `Computer` object to connect:
```python
from computer import Computer
import asyncio
computer = Computer(use_host_computer_server=True)
await computer.run() # Connect to the host desktop
async def main():
await computer.run() # Connect to the host desktop
# Alternative: If your computer server is not running, use start() instead:
# await computer.start() # Start and connect to the host desktop
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.disconnect()
# Alternative: If you want to fully stop everything, use stop() instead:
# await computer.stop() # Fully stop and disconnect
asyncio.run(main())
```
</Tab>
</Tabs>
Once connected, you can perform interactions:
```python
try:
# Take a screenshot of the computer's current display
screenshot = await computer.interface.screenshot()
# Simulate a left-click at coordinates (100, 100)
await computer.interface.left_click(100, 100)
# Type "Hello!" into the active application
await computer.interface.type_text("Hello!")
finally:
await computer.close()
```
</Tab>
<Tab value="TypeScript">
<Callout type="warn" title="TypeScript SDK Deprecated">
@@ -316,7 +398,7 @@ Connect to your Cua computer and perform basic interactions, such as taking scre
// Type "Hello!" into the active application
await computer.interface.typeText("Hello!");
} finally {
await computer.close();
await computer.disconnect();
}
```
@@ -349,22 +431,42 @@ Choose how you want to access vision-language models for your agent:
**Use the agent with CUA models:**
```python
import os
import asyncio
from computer import Computer
from agent import ComputerAgent
os.environ["CUA_API_KEY"] = "sk_cua-api01_..."
agent = ComputerAgent(
model="cua/anthropic/claude-sonnet-4.5", # CUA-routed model
tools=[computer],
max_trajectory_budget=5.0
computer = Computer(
os_type="linux", # or "windows" or "macos"
provider_type="cloud",
name="your-sandbox-name" # from CLI or website
)
messages = [{"role": "user", "content": "Take a screenshot and tell me what you see"}]
async def main():
await computer.run() # Connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
async for result in agent.run(messages):
for item in result["output"]:
if item["type"] == "message":
print(item["content"][0]["text"])
try:
agent = ComputerAgent(
model="cua/anthropic/claude-sonnet-4.5", # CUA-routed model
tools=[computer],
max_trajectory_budget=5.0
)
messages = [{"role": "user", "content": "Take a screenshot and tell me what you see"}]
async for result in agent.run(messages):
for item in result["output"]:
if item["type"] == "message":
print(item["content"][0]["text"])
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
**Available CUA models:**
@@ -386,6 +488,8 @@ Choose how you want to access vision-language models for your agent:
**Use the agent with your provider:**
```python
import os
import asyncio
from computer import Computer
from agent import ComputerAgent
# Set your provider API key
@@ -393,18 +497,36 @@ Choose how you want to access vision-language models for your agent:
# OR
os.environ["OPENAI_API_KEY"] = "sk-..." # For OpenAI
agent = ComputerAgent(
model="anthropic/claude-sonnet-4-5-20250929", # Direct provider model
tools=[computer],
max_trajectory_budget=5.0
computer = Computer(
os_type="linux", # or "windows" or "macos"
provider_type="cloud",
name="your-sandbox-name" # from CLI or website
)
messages = [{"role": "user", "content": "Take a screenshot and tell me what you see"}]
async def main():
await computer.run() # Launch & connect to the sandbox
# Alternative: If your VM is not running, use start() instead:
# await computer.start() # Start and connect to the sandbox
async for result in agent.run(messages):
for item in result["output"]:
if item["type"] == "message":
print(item["content"][0]["text"])
try:
agent = ComputerAgent(
model="anthropic/claude-sonnet-4-5-20250929", # Direct provider model
tools=[computer],
max_trajectory_budget=5.0
)
messages = [{"role": "user", "content": "Take a screenshot and tell me what you see"}]
async for result in agent.run(messages):
for item in result["output"]:
if item["type"] == "message":
print(item["content"][0]["text"])
finally:
await computer.disconnect()
# Alternative: If you want to fully stop the VM, use stop() instead:
# await computer.stop() # Fully stop VM and disconnect
asyncio.run(main())
```
**Supported providers:**