| 1 | # PiHarness |
| 2 | |
| 3 | This example runs the experimental `PiHarness` from Cloudflare Agents pull |
| 4 | request 2197 in a celld Durable Object. The harness needs a model, and the |
| 5 | example gets one from any OpenAI-compatible chat completions endpoint. It |
| 6 | uses OpenRouter and Llama 3.2 1B by default. |
| 7 | |
| 8 | The pi provider expects the `ai.run()` shape of a Workers AI binding, and celld |
| 9 | serves no such binding, so the example builds a small adapter that posts each |
| 10 | request to `PI_ENDPOINT` with `OPENROUTER_API_KEY` as the bearer token. The |
| 11 | example stops at startup when a variable is absent, because a stand-in model |
| 12 | answers every prompt and therefore hides a broken configuration. Set |
| 13 | `PI_ENDPOINT` and `PI_MODEL` in `wrangler.jsonc` to use a different provider, |
| 14 | such as a local server. |
| 15 | |
| 16 | The `agents` package on npm does not contain `PiHarness`, therefore this example |
| 17 | depends on a preview build of pull request 2197. The preview build can change or |
| 18 | expire, so an install can fail until that pull request is released. |
| 19 | |
| 20 | Install the dependencies, write the key to a `.dev.vars` file in this |
| 21 | directory, then start the example. The `agents` package declares many peer |
| 22 | dependencies that the example does not use, so leave them out: |
| 23 | |
| 24 | ```sh |
| 25 | npm install --legacy-peer-deps |
| 26 | |
| 27 | echo 'OPENROUTER_API_KEY=sk-or-...' > .dev.vars |
| 28 | |
| 29 | celld dev . |
| 30 | ``` |
| 31 | |
| 32 | Run a prompt and inspect the persisted transcript: |
| 33 | |
| 34 | ```sh |
| 35 | curl http://127.0.0.1:9876/ |
| 36 | curl http://127.0.0.1:9876/messages |
| 37 | ``` |
| 38 | |
| 39 | The prompt route answers 200 for a completed run, and 502 with the reported |
| 40 | errors for a failed one. |
| 41 | |
| 42 | The Durable Object storage holds the transcript, so the state survives a |
| 43 | restart. A model change does not migrate that state, therefore start again with |
| 44 | `celld dev --clean .` after you edit `PI_MODEL`. |