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
3 changes: 2 additions & 1 deletion context/verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ variable for the real build). Like `pnpm --filter docs typecheck`, it rewrites `

`pnpm --filter docs worker:bundle-check` builds the chat Worker and runs `wrangler deploy --dry-run` on it
(output under the gitignored `docs/worker/dist/dry-run/`): it proves the Worker still bundles for Cloudflare and needs **no credentials**,
because a dry run builds the upload and sends nothing. It does not run the Worker; that is `wrangler dev --local` (plan #257 §8).
because a dry run builds the upload and sends nothing. It does not run the Worker; that is `pnpm --filter docs worker:dev` (plan #257 §8 named `wrangler dev --local`, which
disables the Worker's remote bindings; #367 dropped the flag).

## Audit

Expand Down
2 changes: 2 additions & 0 deletions docs/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ GEMINI_API_KEY=
# URL of the deployed Cloudflare Worker (set for production builds).
# For local dev this defaults to http://localhost:8787 — no need to set it here.
PUBLIC_GEMINI_WORKER_URL=https://worktree-gemini-proxy.northguild.workers.dev
# Cloudflare credentials: `worker:dev` (and `dev:local*`) opens remote sessions for the Worker's AI and KV bindings,
# and wrangler reads these from this file. Alternatively, run `wrangler login`.
CLOUDFLARE_API_TOKEN=
CLOUDFLARE_ACCOUNT_ID=

8 changes: 6 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ Both commands build the worker, write `.dev.vars`, start the Cloudflare Worker o

`PUBLIC_GEMINI_WORKER_URL` is the variable that controls which worker the docs app talks to (Astro only exposes `PUBLIC_` variables to client code; unset, the chat falls back to `http://localhost:8787`). The `dev:local*` scripts set it to `http://localhost:8787` so Astro hits your local `wrangler dev` instance. In production, the GitHub Actions workflow bakes in (from its `GEMINI_WORKER_URL` repository variable) the deployed Cloudflare Worker URL at build time.

**Prerequisite:** `docs/.env.local` must exist with your Gemini API key.
**Prerequisite:** `docs/.env.local` must exist with your Gemini API key and Cloudflare credentials.
Copy `.env.local.example` and fill it in:

```bash
cp docs/.env.local.example docs/.env.local
# then edit docs/.env.local and set GEMINI_API_KEY=...
# then edit docs/.env.local and set GEMINI_API_KEY=..., CLOUDFLARE_API_TOKEN=... and CLOUDFLARE_ACCOUNT_ID=...
```

The Worker's `AI` and `DOX_USAGE` bindings are remote in dev, so `wrangler dev` needs access to the Cloudflare account that
owns them: the two `CLOUDFLARE_*` values (wrangler reads `docs/.env.local`), or `wrangler login`. To run without that access,
see *Local development* in [`worker/README.md`](worker/README.md#local-development).

---

## How the AI chat works
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dev": "astro dev",
"dev:local": "pnpm run worker:build && pnpm run worker:setup-dev-vars && concurrently -k -n worker,docs \"pnpm run worker:dev\" \"PUBLIC_GEMINI_WORKER_URL=http://localhost:8787 astro dev\"",
"dev:local:watch": "pnpm run worker:build && pnpm run worker:setup-dev-vars && concurrently -k -n watch,worker,docs \"pnpm run worker:build:watch\" \"pnpm run worker:dev\" \"PUBLIC_GEMINI_WORKER_URL=http://localhost:8787 astro dev\"",
"worker:dev": "wrangler dev worker/dist/worker.js --local --config worker/wrangler.toml --show-interactive-dev-session=false",
"worker:dev": "wrangler dev worker/dist/worker.js --config worker/wrangler.toml --show-interactive-dev-session=false",
Comment thread
baldurpan marked this conversation as resolved.
Resolved
"worker:build:watch": "cd worker && tsup worker.ts --watch",
"worker:setup-dev-vars": "node worker/setup-dev-vars.mjs",
"worker:build-context": "node worker/build-context.mjs",
Expand Down
18 changes: 16 additions & 2 deletions docs/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,18 @@ so pointed at this TOML file it prints the snippet and writes nothing. Each comm
is derived from the title; ignore that name. Put the first id in `id` and the second in `preview_id`, under
`binding = "DOX_USAGE"`. A made-up id makes `wrangler deploy` fail.

`remote = true` is stated, as it is for `[ai]`, because `worker:dev` runs `wrangler dev --local`, and a local `wrangler dev`
defaults to a simulated local KV that ignores both ids (Cloudflare's KV docs: "Wrangler defaults to using a local version of KV
`remote = true` is stated, as it is for `[ai]`, because `wrangler dev` defaults to a simulated local KV that ignores both
ids (Cloudflare's KV docs: "Wrangler defaults to using a local version of KV
... you can set `"remote": true` in the KV binding configuration"; wrangler 4.152.0's `config-schema.json` describes
`kv_namespaces[].remote` as "Whether the KV namespace should be remote or not in local development"). With it, `wrangler dev`
reads and writes the preview namespace on Cloudflare, so a local question spends real KV writes (R2). Remove the line to
develop against the simulated local KV instead.

`worker:dev` does not pass `--local`: in wrangler 4 that flag means "run locally with remote bindings disabled", so it would
ignore `remote = true` on both bindings. Workers AI has no local simulator, and under `--local` the bindings table prints
`env.AI AI not supported` and every answer fails with `upstream_unavailable`. Read the bindings table at start-up: `env.AI`
and `env.DOX_USAGE` should say `remote`.

### Errors inside the stream

Provider error text never reaches the reader. `error-mapping.ts` reads the HTTP status inside the failure and replaces the event. Gemini's adapter puts Google's JSON body in the `RUN_ERROR`'s `message` and sets no `code`; the Workers AI adapter's message is the HTTP status, a space and Cloudflare's text, and its `code` is Cloudflare's own number:
Expand Down Expand Up @@ -234,6 +239,15 @@ All commands are run from the repo root with `pnpm --filter docs run <command>`.
Copy `docs/.env.local.example` → `docs/.env.local` and fill it in. The first two brains use the `AI` binding, which is remote (see [Brains](#brains)): a question asked against `wrangler dev`
spends the account's real daily allocation. Leave the binding out of `wrangler.toml` to run on Gemini alone.

**Prerequisite: Cloudflare credentials.** `worker:dev`, and so `dev:local` and `dev:local:watch`, opens a remote session for
the `AI` binding and the `DOX_USAGE` KV namespace, so it needs access to the account that owns them: `CLOUDFLARE_API_TOKEN` and
`CLOUDFLARE_ACCOUNT_ID` in `docs/.env.local` (wrangler reads that file), or `wrangler login`. Without them, wrangler asks you
to log in; non-interactively, wrangler 4.152.0 stops with "Failed to start the remote proxy session ... it's necessary to set a
CLOUDFLARE_API_TOKEN environment variable". Under `concurrently`, that line is easy to miss among the other output.
Without access, edit `wrangler.toml` locally and do not commit the edit. Delete the `[ai]` block to run on Gemini alone, as
above; there is no local Workers AI. Then delete `remote = true` from the `[[kv_namespaces]]` block
(`binding = "DOX_USAGE"`), so the ledger uses a simulated local KV.

Two commands cover the common cases:

| Command | What it does |
Expand Down
4 changes: 2 additions & 2 deletions docs/worker/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ remote = true
# sharing it would merge both sites' counts and visitor caps. The ids are not secrets (gmt commits its own); `docs/worker/README.md`, "The binding",
# has the commands that made them.
#
# `remote = true` for the same reason as `[ai]`: `worker:dev` runs `wrangler dev --local`, which would otherwise use a
# simulated local KV. With it, a local question reads and writes the preview namespace on Cloudflare.
# `remote = true` because `wrangler dev` otherwise uses a simulated local KV. (Do not add `--local` to `worker:dev`:
# it disables remote bindings, so this line and `[ai]` would both be ignored.) With it, a local question reads and writes the preview namespace on Cloudflare.
[[kv_namespaces]]
binding = "DOX_USAGE"
id = "6622db5e987c41e6a7102b24bb5d5713"
Expand Down