Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 145 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,147 @@
# ai-artifacts

[Fragments](https://github.com/e2b-dev/fragments) — E2B's open-source AI app
generator: describe an app or an analysis, a model writes it, and E2B runs it
in a cloud sandbox — on **one Cloudflare Worker** built by
[`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), its default
model **Workers AI** with no key, its rate limits and share links in a
**Workers KV** namespace provisioned by Terraform through
[Orun](https://github.com/sourceplane/orun), and packaged, once it is built,
as an **Orun baseline**.

Being built in reviewed steps; the plan and its status are in
[`specs/epics/ai-artifacts`](specs/epics/ai-artifacts/README.md).

Fragments is © FoundryLabs, Inc. (E2B), under the Apache License 2.0.
E2B's [Fragments](https://github.com/e2b-dev/fragments) — an open-source
take on Claude's Artifacts, v0 and GPT Engineer: describe an app or an
analysis, a model writes it as a *fragment* (its code, the stack it runs on
and its dependencies) streamed into the page, and E2B runs it in a cloud
sandbox — a Python cell's output and charts, or a live Next.js, Vue,
Streamlit or Gradio preview — running on **one Cloudflare Worker + Static
Assets** built by [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare).
Its default model is **Workers AI**, called through the Worker's own binding
with no key; its rate limits and the published previews' short links are in
a **Workers KV** namespace provisioned by Terraform through
[Orun](https://github.com/sourceplane/orun).

Both of Fragments' halves are HTTP — the AI SDK's `streamObject` and the E2B
SDK's REST and streaming calls — so the whole app is one Worker (a spike
proved the E2B SDK bundles for workerd and reaches E2B's API from it). A
fresh deployment writes fragments with nothing configured; running them
needs an E2B API key, and every vendor model needs its own key, each an
optional secret. See [`specs/epics/ai-artifacts`](specs/epics/ai-artifacts/README.md)
for the design, the milestones and the as-built status.

## What is here

| Surface | Where |
|---|---|
| The page, the chat, the picker, the settings, the preview | `apps/web/app/page.tsx`, `apps/web/components` |
| The model call — a fragment streamed as a structured object | `apps/web/app/api/chat/route.ts`, `app/api/morph-chat/route.ts` (Morph's apply-edit mode) |
| The stacks (personas) and the fragment's schema | `apps/web/lib/templates.ts`, `apps/web/lib/schema.ts` |
| The models: Workers AI first, vendor models behind their keys | `apps/web/lib/models.ts`, `apps/web/lib/models.json` |
| Running a fragment in E2B | `apps/web/app/api/sandbox/route.ts`, `apps/web/lib/e2b.ts` |
| The rate limit and the share links, in KV | `apps/web/lib/ratelimit.ts`, `lib/links.ts`, `app/actions/publish.ts`, `app/s/[id]/route.ts` |
| The product's name and source link | `apps/web/lib/site.ts` |
| What the deploy's smoke reads | `apps/web/app/api/health/route.ts` |
| Workers build, bindings and deploy contract | `apps/web/next.config.mjs`, `open-next.config.ts`, `wrangler.template.jsonc` + `wiring.fixture.json` (→ the committed `wrangler.jsonc`), `component.yaml` |
| The KV namespace (stage, prod) | `infra/terraform/cloudflare-kv` |
| The wrangler renderer, the Worker-secrets helper | `tooling/wire` |
| Tests | `tests/web` (the models, the routes, the rate limit, the links — against in-memory bindings, no network) |

## Getting started

```bash
pnpm install
pnpm run wire:fixture # render apps/web/wrangler.jsonc from the offline fixture
pnpm typecheck
pnpm lint
pnpm test
pnpm build # next build + opennextjs-cloudflare build → apps/web/.open-next
```

```bash
cp apps/web/.dev.vars.example apps/web/.dev.vars # optional keys, all may stay empty
pnpm --filter @app/web dev # http://localhost:3000 — the bindings through wrangler's platform proxy
pnpm --filter @app/web preview # wrangler dev over the built Worker
```

Workers AI has no local mode: `next dev`, `preview` and `wrangler dev` reach
it on your account, so run `pnpm exec wrangler login` first (a build needs no
credential — only the dev server opens the platform proxy). The KV namespace
is local under both.

## Connecting E2B

Fragments are written without E2B; running one needs an
[E2B API key](https://e2b.dev/dashboard?tab=keys). Store it as the product's
Orun secret `E2B_API_KEY` for an environment and redeploy it: the build hands
it to the Worker as a Worker secret (`tooling/wire/worker-secrets.mjs`, the
value over stdin, never in a bundle). Until then the sandbox route answers
"E2B is not connected", the page shows that beside the code, and the code is
ready to copy. The five stacks are E2B's public sandbox templates
(`code-interpreter-v1`, `nextjs-developer`, `vue-developer`,
`streamlit-developer`, `gradio-developer`), hosted by E2B. Locally, put the
key in `apps/web/.dev.vars`.

Removing an Orun secret does not remove the Worker's copy: delete it with
`wrangler secret delete E2B_API_KEY --name <worker>` too.

## Connecting a model provider

The picker lists every model upstream lists, Workers AI's first. A Workers
AI model answers with the Worker's own account; a vendor model is sent with
the key the visitor types in the settings (as upstream allows) or, without
one, with the server's — an optional Orun secret per provider, pushed to the
Worker the same way:

| Provider | Secret |
|---|---|
| OpenAI | `OPENAI_API_KEY` |
| Anthropic | `ANTHROPIC_API_KEY` |
| Google AI | `GOOGLE_GENERATIVE_AI_API_KEY` |
| Mistral | `MISTRAL_API_KEY` |
| Groq | `GROQ_API_KEY` |
| Together AI | `TOGETHER_API_KEY` |
| Fireworks | `FIREWORKS_API_KEY` |
| xAI | `XAI_API_KEY` |
| DeepSeek | `DEEPSEEK_API_KEY` |
| Morph (the apply-edit mode) | `MORPH_API_KEY` |

A vendor model with neither key is refused with the secret to add, before
any request leaves the Worker. Requests sent with the server's keys — and
every Workers AI request — are limited per client IP, 10 a day unless
`RATE_LIMIT_MAX_REQUESTS` and `RATE_LIMIT_WINDOW` (upstream's variables, set
as `vars` in `wrangler.template.jsonc`) say otherwise.

## How it maps to Vercel

| Vercel-side | Here |
|---|---|
| Next.js 14 hosting, `maxDuration` routes | One Cloudflare Worker (`.open-next/worker.js`) + Static Assets, Next.js 16; the routes stream |
| AI SDK 3 (`streamObject`, `experimental_useObject`) | AI SDK 7 and `@ai-sdk/react` 4 — the same two calls |
| A vendor key for every model | **Workers AI** through the `AI` binding (`workers-ai-provider`, the schema sent as JSON mode), the default, no key; vendor models behind the visitor's key or an optional server key |
| `E2B_API_KEY` environment variable | An optional Orun secret → Worker secret; without it fragments are written, not run |
| `@upstash/ratelimit` on Vercel KV (`KV_REST_API_URL`) | A fixed window per client IP in the `KV` binding (`lib/ratelimit.ts`) |
| Vercel KV share links + `/s/:id` middleware | `fragment:<id>` keys in the `KV` binding, `/s/[id]` a route handler; the link is on the visitor's own origin, not `NEXT_PUBLIC_SITE_URL` |
| Supabase sign-in and teams, PostHog, Vercel Analytics, Google Tag Manager | Left out; the account menu keeps its links |
| Vertex AI, Ollama | Left out: a service-account file and a local server do not fit a Worker |
| Deploy on push | Orun applies the namespace and deploys `<repo>-web-{stage,prod}` on merge to `main`, smoke-tested on `/api/health` |
| "Deploy with Vercel" (clone the template) | An Orun baseline — once it is one |

## How the namespace reaches the Worker

```
infra/terraform/cloudflare-kv ──apply──► orun secret WIRING_CLOUDFLARE_KV (stage, prod)
▼
apps/web dependsOn cloudflare-kv (enforced on main) · secretEnv WIRING_CLOUDFLARE_KV_<ENV>
build: tooling/wire/live.mjs → wrangler.jsonc with the environment's namespace id
(dev, verify-only, renders the fixture) → next build → opennextjs-cloudflare
→ each stored key to the Worker as a secret (worker-secrets.mjs)
smoke: / renders to </html>; /api/health reads the namespace and finds the AI binding
```

`wrangler.jsonc` is committed as the fixture render of
`wrangler.template.jsonc`, because the deploy lane's `verify-app-structure`
step checks the config exists before it builds.

## CI

`ci.yml` is the Orun pipeline: `orun plan --changed`, then one job per lane.
A pull request runs the verify lanes (the tests, the KV root planned, the
Worker built and dry-deployed per environment); a merge applies the
namespace, then deploys stage → prod. It plans nothing while `intent.yaml`
names the placeholder workspace `ws_CHANGE_ME`, as this repository does. A
manual run (Orunbase's Deploy button, or Run workflow on `main`) plans the
environment and components it names under the push-to-main trigger.

Fragments is © FoundryLabs, Inc. (E2B), under the Apache License 2.0
(`apps/web/LICENSE`).
2 changes: 1 addition & 1 deletion specs/epics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace.

| Epic | Cluster | Status | What it is |
|------|---------|--------|------------|
| [`ai-artifacts/`](./ai-artifacts/) | **AA** | In progress | E2B's Fragments — an AI app generator whose code runs in E2B sandboxes — equivalent in code and function on ONE Cloudflare Worker, the code written by Workers AI with no key, the rate limits and share links in Workers KV — and an Orun baseline that instantiates it in one landing |
| [`ai-artifacts/`](./ai-artifacts/) | **AA** | AA0–AA3 shipped; AA4 in progress | E2B's Fragments — an AI app generator whose code runs in E2B sandboxes — equivalent in code and function on ONE Cloudflare Worker, the code written by Workers AI with no key, the rate limits and share links in Workers KV — and an Orun baseline that instantiates it in one landing |

As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`.
32 changes: 28 additions & 4 deletions specs/epics/ai-artifacts/IMPLEMENTATION-STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@ As built, kept apart from the plan.

| Milestone | Status | PR |
|---|---|---|
| AA0 — the spec | in progress | |
| AA1 — the app on Workers | not started | |
| AA2 — the KV namespace | not started | |
| AA3 — ready to instantiate | not started | |
| AA0 — the spec | ✅ Shipped | [#1](https://github.com/sourceplane/ai-artifacts/pull/1) |
| AA1 — the app on Workers | ✅ Shipped | [#2](https://github.com/sourceplane/ai-artifacts/pull/2) |
| AA2 — the KV namespace | ✅ Shipped | [#3](https://github.com/sourceplane/ai-artifacts/pull/3) |
| AA3 — ready to instantiate | ✅ Shipped | [#4](https://github.com/sourceplane/ai-artifacts/pull/4) |
| AA4 — a baseline | not started | |

## Departures from upstream (e2b-dev/fragments at `3b11c7f`)

| Upstream | As built | Why |
|---|---|---|
| Next.js 14, React 18, AI SDK 3 | Next.js 16.3.8, React 19, AI SDK 7 + `@ai-sdk/react` 4 | The reference stack; `streamObject` and `experimental_useObject` carry over unchanged. `maxTokens` is passed as `maxOutputTokens` |
| Vercel hosting | One Worker + Static Assets through `@opennextjs/cloudflare`, the page pre-rendered and served from the static-assets incremental cache (`scripts/populate-static-assets-cache.mjs`, because `populateCache local` opens the platform proxy and the AI binding has no local mode) | Workers |
| A vendor key for every model; Claude the default | Workers AI the default (`@cf/meta/llama-3.3-70b-instruct-fp8-fast`, a JSON-mode model, plus Llama 4 Scout), through the `AI` binding | A fresh deployment writes fragments with no key |
| Vertex AI, Ollama | Left out | A service-account file and a local server do not fit a Worker |
| Server keys read from the environment; a missing key failed at the provider | The same keys as optional Worker secrets; a model with no key is refused with the secret to add | Say what to connect before any request leaves |
| `createOpenAI` for Groq, Together, xAI, DeepSeek, Morph | `createOpenAI(...).chat(...)` | AI SDK 7's OpenAI provider defaults to the Responses API, which those endpoints do not serve |
| `E2B_API_KEY` required | Optional; without it the sandbox route answers "E2B is not connected" and the page shows that beside the code | Deploy before an E2B account exists |
| `@upstash/ratelimit` sliding window on Vercel KV | A fixed window per client IP in the `KV` binding; `CF-Connecting-IP` before `X-Forwarded-For` | Workers KV; eventually consistent, so approximate across locations (risk AA-D) |
| Vercel KV share links, `/s/:id` middleware, `NEXT_PUBLIC_SITE_URL` | `fragment:<id>` in the `KV` binding (60 s expiry floor), `/s/[id]` a route handler that redirects only to `*.e2b.app`, the link on the visitor's origin | Workers KV; each environment links to itself |
| Supabase sign-in, teams and their headers to E2B; ZeroBounce e-mail check | Left out | Upstream runs without them (a demo session); none is needed to write or run a fragment |
| PostHog, Vercel Analytics, Google Tag Manager | Left out | No analytics ship with the product |
| The account menu (avatar, e-mail, sign out) | A menu with upstream's links (About E2B, the product's source, E2B's Discord and X) | No account |
| "Fragments by E2B" heading | `{SITE_NAME} on E2B` (`lib/site.ts`) | The product names itself; E2B is credited |
| `fragment.code.forEach(async …)` | `Promise.all` | The writes are awaited before the code runs |
| `next lint` with `next/core-web-vitals` | ESLint 9 with eslint-config-next 16; the rules it added after the template are off by name | The template is kept as written |
| node:test with `tsx` | vitest in `tests/web`, with in-memory KV and AI bindings and no network | The reference test shape |

Not proven offline: a real Workers AI model following the schema (AA-B), a
real sandbox run (AA-A, beyond the spike), and the namespace's apply.
2 changes: 1 addition & 1 deletion specs/epics/ai-artifacts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The port keeps the chat, the five stacks (Python data analyst, Next.js, Vue, Str

| Field | Value |
|-------|-------|
| Status | AA0 in progress |
| Status | AA0–AA3 shipped (#1–#4); AA4 in progress |
| Cluster | **AA** (AA0–AA4) |
| Owner(s) | `apps/web` (the page, the chat, sandbox and publish routes, `lib/`) · `tests/web` · `infra/terraform/cloudflare-kv` |
| Builds on | `ai-chatbot` (Workers AI through the `AI` binding with `workers-ai-provider`, vendor models behind their keys, the AI stand-in in the e2e), `kv-redis-starter` (the `cloudflare-kv` root and the wiring render), `ai-emoji-generator` (optional Orun secrets pushed as Worker secrets) |
Expand Down
Loading