bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
156 lines
4.5 KiB
Plaintext
156 lines
4.5 KiB
Plaintext
---
|
|
title: Python Quickstart
|
|
icon: /images/python-logo.svg
|
|
description: Create a Mirage Python workspace, mount RAM as a virtual filesystem, and run shell commands that read, write, search, and transform files.
|
|
keywords: ["Mirage Python quickstart", "mirage-ai", "virtual filesystem", "AI agents", "shell commands"]
|
|
---
|
|
|
|
## Installation
|
|
|
|
Install Mirage:
|
|
|
|
```bash
|
|
uv add mirage-ai
|
|
```
|
|
|
|
If you are not using uv:
|
|
|
|
```bash
|
|
pip install mirage-ai
|
|
```
|
|
|
|
For resources with extra dependencies, install the matching extra:
|
|
|
|
```bash
|
|
uv add "mirage-ai[s3]"
|
|
```
|
|
|
|
## Create a Workspace
|
|
|
|
Start with the RAM resource so you can try Mirage without credentials.
|
|
|
|
```python
|
|
import asyncio
|
|
|
|
from mirage import MountMode, Workspace
|
|
from mirage.resource.ram import RAMResource
|
|
|
|
|
|
async def main() -> None:
|
|
ws = Workspace({"/data": RAMResource()}, mode=MountMode.WRITE)
|
|
|
|
await ws.execute('echo "hello mirage" | tee /data/hello.txt')
|
|
result = await ws.execute("cat /data/hello.txt")
|
|
print(await result.stdout_str())
|
|
|
|
await ws.close()
|
|
|
|
|
|
asyncio.run(main())
|
|
```
|
|
|
|
## Run Commands
|
|
|
|
Once a resource is mounted, you can use Mirage like a shell over your
|
|
virtual filesystem:
|
|
|
|
```python
|
|
import asyncio
|
|
|
|
from mirage import MountMode, Workspace
|
|
from mirage.resource.ram import RAMResource
|
|
|
|
|
|
async def main() -> None:
|
|
ws = Workspace({"/data": RAMResource()}, mode=MountMode.WRITE)
|
|
|
|
await ws.execute('echo \'{"name": "alice"}\' | tee /data/user.json')
|
|
|
|
result = await ws.execute("ls /data/")
|
|
print(await result.stdout_str())
|
|
|
|
result = await ws.execute("cat /data/hello.txt")
|
|
print(await result.stdout_str())
|
|
|
|
result = await ws.execute('jq ".name" /data/user.json')
|
|
print(await result.stdout_str())
|
|
|
|
result = await ws.execute("grep hello /data/hello.txt")
|
|
print(await result.stdout_str())
|
|
|
|
await ws.close()
|
|
|
|
|
|
asyncio.run(main())
|
|
```
|
|
|
|
## Estimate Before You Run
|
|
|
|
`execute(..., provision=True)` returns a `ProvisionResult` instead of
|
|
running the command: network/cache bytes, read ops, and a `precision`
|
|
telling you how much to trust the numbers (`exact`, `range`,
|
|
`unknown` -- totals under `unknown` are floors). Pipelines, `&&`/`||`,
|
|
`if`/`case`, loops, and subshells aggregate automatically.
|
|
|
|
```python
|
|
plan = await ws.execute("cat /data/user.json | wc -l", provision=True)
|
|
print(plan.network_read, plan.read_ops, plan.precision)
|
|
```
|
|
|
|
Read commands are estimated out of the box on every backend. When you
|
|
register your own command, pass `provision=` to the `@command`
|
|
decorator (reuse a helper like `make_file_read_provision(my_stat)` or
|
|
`default_provision(name, my_stat)` from
|
|
`mirage.commands.builtin.generic_bind`), or omit it and the planner
|
|
reports `unknown`. Full semantics live in the
|
|
[CLI provision docs](/home/cli#5-dry-run-with-provision).
|
|
|
|
## Output Safeguards
|
|
|
|
To keep huge reads from flooding an agent, `cat`, `grep`, `rg`, `head`,
|
|
and `tail` cap their **final** output at 2000 lines by default. When a
|
|
cap fires, the agent sees the truncated bytes plus a stderr notice
|
|
(`output truncated at safeguard limit (2000 lines); ...`); exit code
|
|
stays 0.
|
|
|
|
Caps fire only on the **terminal** command of a pipeline, so
|
|
`cat big.txt | head -n 30` still shows 30 lines.
|
|
|
|
### Configure per mount
|
|
|
|
Limits are per-command and per-mount. Attach them when you mount a
|
|
resource by passing a `(resource, mode, {command: CommandSafeguard})`
|
|
tuple. Each guard sets `max_lines` / `max_bytes` (output cap) and/or
|
|
`timeout_seconds` (deadline); `on_exceed` is `TRUNCATE` (default, exit 0
|
|
plus notice) or `ERROR` (exit 1 plus notice):
|
|
|
|
```python
|
|
from mirage import MountMode, Workspace
|
|
from mirage.resource.ram import RAMResource
|
|
from mirage.types import CommandSafeguard, OnExceed
|
|
|
|
ws = Workspace(
|
|
{
|
|
"/data": (
|
|
RAMResource(),
|
|
MountMode.WRITE,
|
|
{
|
|
"head": CommandSafeguard(max_lines=100), # cap, keep going
|
|
"grep": CommandSafeguard(max_lines=50, on_exceed=OnExceed.ERROR),
|
|
"rg": CommandSafeguard(timeout_seconds=30), # deadline
|
|
},
|
|
),
|
|
},
|
|
mode=MountMode.WRITE,
|
|
)
|
|
```
|
|
|
|
The same limits are available to the CLI as a `command_safeguards`
|
|
block in the workspace YAML.
|
|
|
|
## Next Steps
|
|
|
|
- See [Python Installation](/python/install) for resource extras and the `uv` workflow.
|
|
- Browse [Python Agents](/python/agents/index) to wire Mirage into the OpenAI Agents SDK, LangChain, Pydantic AI, CAMEL, and OpenHands.
|
|
- Pick a real backend from [Resource Docs](/python/resource/index), such as [S3](/python/resource/s3), [Slack](/python/resource/slack), or [GitHub](/python/resource/github).
|