| 1 | import { DurableObject } from "cloudflare:workers"; |
| 2 | import { PiHarness } from "agents/harness"; |
| 3 | import { Lifecycle } from "agents/lifecycle"; |
| 4 | import { createWorkersAI } from "agents/providers/pi"; |
| 5 | |
| 6 | // The pi provider speaks OpenAI chat completions, so any endpoint with that |
| 7 | // shape serves it: OpenRouter by default, or a local server through |
| 8 | // PI_ENDPOINT. |
| 9 | function createDirectAi(url, key) { |
| 10 | return { |
| 11 | async run(model, input, options) { |
| 12 | const response = await fetch(url, { |
| 13 | method: "POST", |
| 14 | headers: { |
| 15 | "content-type": "application/json", |
| 16 | authorization: `Bearer ${key}`, |
| 17 | }, |
| 18 | body: JSON.stringify({ model, ...input }), |
| 19 | signal: options?.signal, |
| 20 | }); |
| 21 | if (options?.returnRawResponse) return response; |
| 22 | if (!response.ok) { |
| 23 | const detail = await response.text().catch(() => ""); |
| 24 | throw new Error( |
| 25 | `${url} returned ${response.status}${ |
| 26 | detail ? `: ${detail.slice(0, 512)}` : "" |
| 27 | }`, |
| 28 | ); |
| 29 | } |
| 30 | return response.json(); |
| 31 | }, |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | function required(env, name) { |
| 36 | const value = env[name]; |
| 37 | if (!value) { |
| 38 | throw new Error( |
| 39 | `the pi example needs ${name}; set it in .dev.vars or in the vars of wrangler.jsonc`, |
| 40 | ); |
| 41 | } |
| 42 | return value; |
| 43 | } |
| 44 | |
| 45 | export class PiAgent extends DurableObject { |
| 46 | harness; |
| 47 | lifecycle; |
| 48 | |
| 49 | constructor(ctx, env) { |
| 50 | super(ctx, env); |
| 51 | const ai = createDirectAi( |
| 52 | required(env, "PI_ENDPOINT"), |
| 53 | required(env, "OPENROUTER_API_KEY"), |
| 54 | ); |
| 55 | const runtime = createWorkersAI(ai, { model: required(env, "PI_MODEL") }); |
| 56 | this.harness = new PiHarness({ |
| 57 | models: runtime.models, |
| 58 | model: runtime.model, |
| 59 | compaction: { |
| 60 | enabled: false, |
| 61 | reserveTokens: 0, |
| 62 | keepRecentTokens: 0, |
| 63 | }, |
| 64 | }); |
| 65 | this.lifecycle = Lifecycle.install(this).use(this.harness); |
| 66 | } |
| 67 | |
| 68 | async fetch(request) { |
| 69 | const path = new URL(request.url).pathname; |
| 70 | if (path === "/messages") { |
| 71 | return Response.json(await this.harness.getMessages()); |
| 72 | } |
| 73 | |
| 74 | const result = await this.harness.prompt("ping"); |
| 75 | const errors = result.messages |
| 76 | .filter((message) => message.stopReason === "error") |
| 77 | .map((message) => message.error); |
| 78 | return Response.json({ |
| 79 | status: result.status, |
| 80 | operationId: result.operationId, |
| 81 | errors, |
| 82 | messages: result.messages, |
| 83 | }, { status: result.status === "completed" ? 200 : 502 }); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export default { |
| 88 | fetch(request, env) { |
| 89 | const id = env.PI_AGENT.idFromName("local-pi"); |
| 90 | return env.PI_AGENT.get(id).fetch(request); |
| 91 | }, |
| 92 | }; |