Skip to content

Commit 3b27d75

Browse files
committed
feat(deploy/cf): expose env on photon instances + auto-bind Workers AI
Plumbs the Worker's env onto the photon instance as this.env so a photon can reach CF bindings (Workers AI, KV, R2, queues) directly. Photons that want to stay portable can branch on (this as any).env?.AI and fall back to a non-CF path on the local daemon (where env is undefined). Auto-includes the [ai] binding in every generated wrangler.toml so photons can call env.AI.run for embeddings, classification, or small LLMs without extra setup. Free tier covers thousands of calls/day; photons that do not use it pay nothing for the binding being declared.
1 parent c409ec8 commit 3b27d75

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

templates/cloudflare/worker.ts.template

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ function withCfCapabilities(
354354
configurable: false,
355355
});
356356

357+
// Worker env exposed to the photon for direct binding access — Workers AI
358+
// (`env.AI.run('@cf/...', ...)`), KV, R2, queues, secrets. Photons that
359+
// want to stay CF-portable should branch on `(this as any).env?.AI` and
360+
// fall back to a non-CF path on the local daemon (where `env` is
361+
// undefined). Non-enumerable so it doesn't leak into JSON output.
362+
Object.defineProperty(instance, 'env', {
363+
value: env,
364+
writable: false,
365+
enumerable: false,
366+
configurable: false,
367+
});
368+
357369
// Human/LLM-in-the-loop primitives. Each one wraps an MCP server-initiated
358370
// request (sampling/createMessage, elicitation/create), pushed over the
359371
// active tool call's SSE response stream and awaited on a Promise keyed by

templates/cloudflare/wrangler.toml.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ main = "src/worker.ts"
77
compatibility_date = "2024-09-23"
88
compatibility_flags = ["nodejs_compat"]
99
__OBSERVABILITY__
10+
# Workers AI binding — gives every photon access to CF's edge inference
11+
# (embeddings, classification, small LLMs) via `(this as any).env.AI.run(...)`.
12+
# Free tier covers thousands of calls/day; charges only kick in at scale.
13+
# Photons that don't use it pay nothing for having the binding declared.
14+
[ai]
15+
binding = "AI"
16+
1017
# Durable Objects backing every photon class bundled into this Worker. The
1118
# host photon is always bound to `PHOTON`; each `@photons` sibling gets
1219
# its own `PHOTON_<NAME>` binding so `this.call('sibling.method', args)`

0 commit comments

Comments
 (0)