mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
Split the host into transport-independent execution logic and a VM driver, so the VM can run in a worker thread (CodemodeSandbox, unchanged), on the calling thread (InlineCodemodeSandbox), or in another isolate over RPC (RemoteCodemodeSandbox). Add the /portable entry without Node imports and the /cloudflare entry with CodemodeSandboxDurableObject, meant to be deployed as its own Worker so scripts never block the caller's thread. Scripts that compute without awaiting a tool are bounded by an interrupt budget, now also polled while draining promise jobs. examples/cloudflare shows the setup.
31 lines
1 KiB
Markdown
31 lines
1 KiB
Markdown
# pi-codemode on Cloudflare
|
|
|
|
This example has two Workers:
|
|
|
|
- **`sandbox/`** deploys `CodemodeSandboxDurableObject` as its own Worker, `pi-codemode-sandbox`.
|
|
Scripts run there, in a separate isolate.
|
|
- **`agent/`** has a `Session` Durable Object that owns the tools. It runs scripts through
|
|
`RemoteCodemodeSandbox`, which keeps the tools and results in `Session` while the sandbox Worker
|
|
runs the VM.
|
|
|
|
The [README of the package](../../README.md#usage-on-cloudflare-workers) explains why the
|
|
sandbox must be a separate Worker, and which limits apply.
|
|
|
|
## Try it locally
|
|
|
|
Install `@earendil-works/pi-codemode` and `wrangler` in each directory, then start both Workers in
|
|
one process:
|
|
|
|
```sh
|
|
npx wrangler dev -c agent/wrangler.jsonc -c sandbox/wrangler.jsonc
|
|
curl -X POST http://localhost:8787 --data 'const n = await tools.count({ add: 2 }); text("n=" + n); return n'
|
|
```
|
|
|
|
## Deploy
|
|
|
|
Deploy the sandbox first, because the agent's binding refers to it by name:
|
|
|
|
```sh
|
|
(cd sandbox && npx wrangler deploy)
|
|
(cd agent && npx wrangler deploy)
|
|
```
|