diff --git a/specs/epics/README.md b/specs/epics/README.md index 29155e3..3d43cc9 100644 --- a/specs/epics/README.md +++ b/specs/epics/README.md @@ -7,5 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace. | Epic | Cluster | Status | What it is | |------|---------|--------|------------| +| [`ai-slackbot/`](./ai-slackbot/) | **SB** | In progress (SB0) | Vercel's AI SDK Slackbot — an AI chatbot for Slack whose every tool call an Open Policy Agent policy gates — as one Cloudflare Worker answering Slack's Events API with Workers AI, and an Orun baseline that instantiates it in one landing (SB2) | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/ai-slackbot/IMPLEMENTATION-STATUS.md b/specs/epics/ai-slackbot/IMPLEMENTATION-STATUS.md new file mode 100644 index 0000000..ce4cdd6 --- /dev/null +++ b/specs/epics/ai-slackbot/IMPLEMENTATION-STATUS.md @@ -0,0 +1,11 @@ +# ai-slackbot — implementation status + +| Milestone | State | Landed | +|---|---|---| +| SB0 — the spec | ✅ | SB-1 | +| SB1 — the Slack bot on Workers | planned | SB-2 | +| SB2 — a baseline | planned | SB-3 | + +## Departures from the upstream template + +Recorded as they land, from SB1 on. diff --git a/specs/epics/ai-slackbot/README.md b/specs/epics/ai-slackbot/README.md new file mode 100644 index 0000000..e9e1acf --- /dev/null +++ b/specs/epics/ai-slackbot/README.md @@ -0,0 +1,35 @@ +# ai-slackbot — Vercel's AI SDK Slackbot on Cloudflare Workers + +**The template is a Slack bot in one Vercel function — Slack's Events API at +`api/events.ts`, answered by an AI SDK agent whose every tool call an Open +Policy Agent policy gates — so the port is a Hono route at the same path on +one plain Cloudflare Worker: Slack's signature verified first, the event +acknowledged at once and answered in `waitUntil`, Workers AI as the model so +it needs no key, deployed by Orun to stage and prod on every merge — and the +baseline that makes a copy of it somebody else's bot in one landing.** + +| | | +|---|---| +| Status | In progress — SB0 on main | +| Upstream | [vercel-labs/ai-sdk-slackbot](https://github.com/vercel-labs/ai-sdk-slackbot) (MIT, as its README declares; no LICENSE file), at `7d84809` | +| Reference | [hono-api](https://github.com/sourceplane/hono-api) and hono-mcp — the same plain Worker, scaffolding, tests and bootstrap | +| Key | `SB` | + +## Read in this order + +1. [design.md](design.md) — the template, how each Vercel piece maps to + Cloudflare, the repository and the build, and the alternatives rejected. +2. [implementation-plan.md](implementation-plan.md) — the milestones and + what each one lands. +3. [risks-and-open-questions.md](risks-and-open-questions.md) — what is not + proven, and what was accepted. +4. [IMPLEMENTATION-STATUS.md](IMPLEMENTATION-STATUS.md) — what shipped, and + every departure from the upstream template. + +## Milestones + +| Milestone | What it lands | Task | +|---|---|---| +| SB0 — the spec | the repository, its intent, CI, tooling and spec index; this doc set | SB-1 | +| SB1 — the Slack bot on Workers | the template in `apps/api` as one plain Worker, its policy, its tests, the README | SB-2 | +| SB2 — a baseline | the console card, the build document, the context pack and the baseline's tests | SB-3 | diff --git a/specs/epics/ai-slackbot/design.md b/specs/epics/ai-slackbot/design.md new file mode 100644 index 0000000..39e6c78 --- /dev/null +++ b/specs/epics/ai-slackbot/design.md @@ -0,0 +1,102 @@ +# ai-slackbot — design + +## 1. The template + +Vercel's AI SDK Slackbot (`vercel-labs/ai-sdk-slackbot`, at `7d84809`) is one +Vercel function, `api/events.ts`, that Slack's Events API POSTs to. It +answers the URL verification, then hands `app_mention`, +`assistant_thread_started` and `message.im` events to handlers under `lib/` +inside Vercel's `waitUntil`. The handlers post a status, read the thread +(`conversations.replies`), run an AI SDK agent (`generateText`, up to ten +steps) and post or update the answer. The agent's tools are `getWeather` +(Open-Meteo), `searchWeb` (Exa), `throwDice` and bash-tool's `bash`, +`readFile` and `writeFile` over just-bash's in-memory workspace; every tool +call is gated by `@ai-sdk/policy-opa` against an OPA policy +(`policies/decision.rego`), compiled to WASM and read from disk. The model is +OpenAI's `gpt-4o`, or a local Ollama. + +## 2. Where it runs + +One plain Cloudflare Worker (`cloudflare-worker-turbo`): a Hono app whose +default export is the module handler, the route at upstream's path. Nothing +outlives a request — Slack's thread is the conversation, re-read each turn — +so there is no Durable Object, no database and no queue. + +## 3. Slack's door + +Every request is verified FIRST: the `X-Slack-Signature` header against an +HMAC-SHA256 of `v0::` with the app's +signing secret (WebCrypto; a constant-time compare), refused if the timestamp +is more than five minutes from now. Upstream answered the URL verification +before verifying and dropped the check's result (`verifyRequest` returned a +400 nobody returned), so a forged event was handled as a real one; here a +failure is a `400` and nothing else happens. Then: the challenge is answered; +a retry (`X-Slack-Retry-Num`) is acknowledged with `X-Slack-No-Retry: 1` and +not answered again, because the first delivery was acknowledged at once and +is already being answered; an `event_callback` is acknowledged at once and +everything — the bot's own id (`auth.test`, which upstream looked up before +acknowledging), the thread, the model, the reply — runs in the Worker's +`waitUntil`. Before Slack is connected (either key missing) the door answers +`503` with what to do, and does nothing. + +**Rejected:** answering the challenge unsigned, as upstream (a forged +challenge is harmless, but one rule — verify first — is simpler to hold); +deduplicating retries by event id in KV (every event is acknowledged in +milliseconds, so a retry means Slack lost the acknowledgement, not that the +answer is missing); a Queue for the answer (not an Orun composition yet — +the 30-second `waitUntil` window is risk SB-C). + +## 4. The model and the tools + +Workers AI through the Worker's own `AI` binding, `@cf/moonshotai/kimi-k2.6` +— a model that calls tools, the one ai-chatbot uses — via +`workers-ai-provider`, so a fresh bot answers with no key. Upstream's OpenAI +is used, directly with `@ai-sdk/openai`, once `OPENAI_API_KEY` is stored +(`OPENAI_MODEL` picks the model, as upstream). Never an AI gateway. Ollama is +not carried: it serves on `localhost`, which a deployed Worker cannot reach. +`searchWeb` needs Exa's key, so without `EXA_API_KEY` the model is not +offered it and the system prompt does not name it — a tool that can only +fail is not a tool. + +**Rejected:** OpenAI as the default (a key nobody brokers); AI SDK 7's +string model ids (they route through Vercel's AI Gateway). + +## 5. The policy and the sandbox on Workers + +A Worker cannot compile WebAssembly bytes at runtime. So `policy.wasm` is an +import, which wrangler compiles at upload into a `WebAssembly.Module`, and +opa-wasm's `loadPolicy` — which `@ai-sdk/policy-opa`'s `wasmPolicyClient` +calls with whatever it is given — takes a module as readily as bytes. The +committed `policy.wasm` is what deploys; `policy:build` rebuilds it with the +`opa` CLI, which CI does not carry. + +just-bash ships a Node build and a browser build. The Node build's `python3` +and `js` commands need `node:worker_threads`, and its decompressors load +native addons; neither runs in a Worker. The browser build is the same +in-memory bash without them, but it lacks two classes bash-tool imports +from the package root — `BashTransformPipeline` and `TeePlugin`, used only +by bash-tool's opt-in `experimentalTeeTransform`, which the template never +turns on. So wrangler aliases `just-bash` to `src/vendor/just-bash.js`: the +browser build re-exported, and those two as stubs that throw if anyone turns +the transform on. bash-tool itself is upstream's. Under Node (the tests) the +package's own Node build is used. + +**Rejected:** writing the three tools again over just-bash directly (more +code, and the template's tool descriptions and output limits would drift); +dropping the sandbox (it is the template's showcase of policy-gated +actions). + +## 6. Keys and the repository + +The bot's keys — `SLACK_SIGNING_SECRET`, `SLACK_BOT_TOKEN`, and optionally +`OPENAI_API_KEY`, `OPENAI_MODEL`, `EXA_API_KEY` — are the owner's Orun +secrets per environment, declared in `optionalSecretEnv` and pushed to the +Worker as Worker secrets after each deploy by the composition's +`secrets-live` step (`runtimeSecrets`). None reaches a build. The bot is +therefore born not connected and says so; the README walks through creating +the Slack app. + +The repository is hono-mcp's (hono-api's shape): `apps/api`, `tests/api`, +the shared tooling, two phases. `/health` reports what the bot is connected +to by name, and the deploy's smoke checks it and that the events door +refuses an unsigned event — the one check that needs no Slack app. diff --git a/specs/epics/ai-slackbot/epic.yaml b/specs/epics/ai-slackbot/epic.yaml new file mode 100644 index 0000000..bcfe00d --- /dev/null +++ b/specs/epics/ai-slackbot/epic.yaml @@ -0,0 +1,30 @@ +# The epic's declaration. metadata.name equals the directory name; metadata.key +# is the task-key prefix this epic reserves (SB → SB-1, SB-2, …). Owner, target +# date and health are the platform's — never written here. +apiVersion: orun.io/v1 +kind: Epic +metadata: + name: ai-slackbot + key: SB +spec: + title: "Vercel's AI SDK Slackbot on Cloudflare Workers" + summary: "Vercel's AI SDK Slackbot — an AI chatbot for Slack that answers app mentions and direct messages with an LLM and tools, every tool call gated by an Open Policy Agent policy compiled to WebAssembly — runs as one plain Cloudflare Worker: Slack's Events API at /api/events, verified with WebCrypto, acknowledged at once and answered in waitUntil, with Workers AI as its keyless model; inside the Cirrus baseline's repository conventions, it becomes an Orun baseline that takes a new Slack bot from nothing to live on stage and prod in one landing." + state: started + milestones: + - name: "SB0 — the spec" + exitCriteria: + - "the repository is born with its intent, CI, tooling and spec index, and this doc set and the task contracts are on main" + tasks: [SB-1] + - name: "SB1 — the Slack bot on Workers" + exitCriteria: + - "apps/api is upstream's bot as a Hono Worker: api/events.ts a route at the same path, its agent, tools and OPA policy unchanged in behaviour, Workers AI its default model" + - "every request is verified first with WebCrypto, acknowledged at once and answered in waitUntil; retries are not answered twice; before Slack is connected the door says so" + - "`pnpm typecheck`, `pnpm lint`, `pnpm test` and `pnpm build` are green, the tests driving Slack's events, the signature check and every policy rule in-process" + - "the README says how to connect Slack, run, test and deploy it, and how every Vercel piece maps to Cloudflare" + tasks: [SB-2] + - name: "SB2 — a baseline" + exitCriteria: + - "blueprint.yaml (the console card) and repo-blueprint.yaml (the build) take a new Slack bot from nothing to live on stage and prod in one landing, then record it" + - "a bot built from it carries the bot only, renamed throughout to its own name, repository and Worker, with the template's licence and policy intact" + - "Baseline CI is green: contract tests, generated component docs, the blueprint read by the pinned orun, the leak gate, the offline bootstrap and the end-to-end tier — the stage lane's steps in a staged job tree, the Worker smoke-tested as born and connected and Slack's events driven against stand-ins" + tasks: [SB-3] diff --git a/specs/epics/ai-slackbot/implementation-plan.md b/specs/epics/ai-slackbot/implementation-plan.md new file mode 100644 index 0000000..6a3780c --- /dev/null +++ b/specs/epics/ai-slackbot/implementation-plan.md @@ -0,0 +1,50 @@ +# ai-slackbot — implementation plan + +Three milestones, one task each, each landed as one pull request. + +## SB0 — the spec (SB-1) + +The repository is born with hono-api's frame — `intent.yaml` (dev +verify-only, stage, prod after stage; the placeholder workspace +`ws_CHANGE_ME`, so CI plans nothing until it is linked), `ci.yml` byte for +byte turso-starter's (as hono-api carries it), the shared TypeScript and +ESLint configuration, the wrangler renderer, the pnpm workspace and turbo +pipeline, and the spec index — and this doc set and the three task +contracts land on it. + +## SB1 — the Slack bot on Workers (SB-2) + +- `apps/api/src/index.ts`: upstream's `api/events.ts` as a Hono route at the + same path — Slack's signature verified first with WebCrypto, the URL + verification, retries acknowledged and not answered, the event handled in + `waitUntil` — plus `/` and `/health`; `src/identity.ts` holds `API_NAME`. +- `src/lib/`: upstream's handlers, agent, reply builder and policy loader, + taking a per-request context (the Slack client, the model, the optional + search) instead of `process.env` at import; Workers AI as the default model + (`model.ts`); Exa only with its key. +- `policies/`: upstream's Rego, its tests and its compiled `policy.wasm`, + imported as a WebAssembly module; `src/vendor/just-bash.js`, just-bash's + browser build for bash-tool. +- `wrangler.jsonc` (the `AI` binding in every environment, the just-bash + alias) and `component.yaml` (`cloudflare-worker-turbo`, the five keys as + optional secrets pushed by `runtimeSecrets`, a smoke over `/health` and the + events door). `LICENSE.md`. +- `tests/api`: vitest — the routes and every Slack event in-process against + stand-ins for Slack's Web API and the AI binding, the signature check + against Node's HMAC, every rule of the shipped `policy.wasm`, the reply + format, the model choice and the name seam. +- The README: connect Slack, run, test, deploy; how each Vercel piece maps. + +Done when typecheck, lint, test and build are green, and `wrangler dev` +answers Slack's signed events against the stand-ins. + +## SB2 — a baseline (SB-3) + +hono-mcp's baseline machinery, adapted: the console card (`blueprint.yaml`) +and the build document (`repo-blueprint.yaml`) — two phases, `01-product` in +one landing and `02-docs` recording what went live — the rename engine with +its one seam (`API_NAME`) and `.wasm` excluded, the context pack, the +deployment-record hook probing `/health` and the events door, and the +baseline's tests: contract, leak (the policy and the vendor shim must ship), +placement, and the end-to-end tier serving the staged Worker as born and +connected, driving every Slack event through the stand-ins. diff --git a/specs/epics/ai-slackbot/risks-and-open-questions.md b/specs/epics/ai-slackbot/risks-and-open-questions.md new file mode 100644 index 0000000..c6b7e89 --- /dev/null +++ b/specs/epics/ai-slackbot/risks-and-open-questions.md @@ -0,0 +1,14 @@ +# ai-slackbot — risks and open questions + +| ID | Risk | State | Notes | +|---|---|---|---| +| SB-A | `baseline-v1` is not yet proven live: the repository created and linked, the deploy token minted, a real deploy, the keys pushed by `secrets-live`, the smoke and the probes | OPEN | Proven offline in Baseline CI: the offline bootstrap with the blueprint's own hooks, the stage lane's own steps in a staged job tree, the Worker served as born and connected | +| SB-B | No real Slack workspace and no real Workers AI answer | OPEN | The e2e's stand-ins (`testing/lib/fake-slack-api.py`, `fake-workers-ai.js`) check the token, the payloads and the call order; whether `kimi-k2.6` uses the tools as well as `gpt-4o` is unmeasured | +| SB-C | `waitUntil` keeps background work alive for about 30 s after the response; upstream's function had 60 s | WATCH | A long multi-step answer (ten steps, web search) can be cut off. A Queue would lift it, and is not an Orun composition yet | +| SB-D | `workers-ai-provider` is deprecated (its successor is `agents/models/ai-sdk`); 4.0.1 is its AI SDK 7 release | WATCH | The same package ai-chatbot and nuxt-ai-chatbot run | +| SB-E | The policy's `allowed_dice_channel` is upstream's channel id | ACCEPTED | `throwDice` is denied everywhere until the owner sets theirs (README) | +| SB-F | Anyone can POST to `/api/events` | ACCEPTED | Only a request signed with the app's secret, within five minutes, is handled; the rest are a 400 before any work | +| SB-G | just-bash's `python3` and `js` commands are absent in the Worker (its browser build) | ACCEPTED | The policy's allow-list names neither | +| SB-H | A deploy pushes the keys that exist and deletes none | ACCEPTED | `wrangler secret delete` removes one (README) | +| SB-I | Upstream ships no LICENSE file; its README declares MIT, its package.json ISC | ACCEPTED | `apps/api/LICENSE.md` states this and carries the MIT terms | +| SB-J | `bash-tool` 1.3.17's peer range names `ai` ^6; the bot runs `ai` 7 | ACCEPTED | Upstream runs the same pair (on `ai` 7 canaries); 1.3.19 widens the range | diff --git a/tasks/SB-1.TaskContract.yaml b/tasks/SB-1.TaskContract.yaml new file mode 100644 index 0000000..5b4902b --- /dev/null +++ b/tasks/SB-1.TaskContract.yaml @@ -0,0 +1,15 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: SB-1 + title: "The spec" +spec: + goal: The ai-slackbot repository is born with its intent, CI, tooling and spec index, and the ai-slackbot epic — declaration, doc set and task contracts — is on main + affects: + - specs/epics/ai-slackbot + - specs/epics/README.md + - tasks + doneWhen: + - epic.yaml, README, design, implementation plan, risks and status are on main + - the three task contracts exist + gates: [] diff --git a/tasks/SB-2.TaskContract.yaml b/tasks/SB-2.TaskContract.yaml new file mode 100644 index 0000000..ec2ea77 --- /dev/null +++ b/tasks/SB-2.TaskContract.yaml @@ -0,0 +1,17 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: SB-2 + title: "The Slack bot on Workers" +spec: + goal: Vercel's AI SDK Slackbot runs as one Cloudflare Worker — Slack's events at /api/events, verified, acknowledged and answered in waitUntil with Workers AI and the policy-gated tools — with its tests, and its README saying how it maps to Vercel + affects: + - apps/api + - tests/api + - pnpm-lock.yaml + - pnpm-workspace.yaml + - README.md + doneWhen: + - pnpm typecheck, lint, test and build are green + - wrangler dev answers Slack's signed events — a mention answered in its thread through the policy, a refusal relayed, a DM from its thread — and refuses unsigned, forged and stale ones; /health names the bot, the environment and what it is connected to + gates: [] diff --git a/tasks/SB-3.TaskContract.yaml b/tasks/SB-3.TaskContract.yaml new file mode 100644 index 0000000..c8b9952 --- /dev/null +++ b/tasks/SB-3.TaskContract.yaml @@ -0,0 +1,29 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: SB-3 + title: "ai-slackbot becomes an Orun baseline" +spec: + goal: blueprint.yaml and repo-blueprint.yaml take a new Slack bot from nothing to live on stage and prod in one landing, held by Baseline CI + affects: + - blueprint.yaml + - repo-blueprint.yaml + - BOOTSTRAP.md + - .baseline + - .github/workflows/baseline.yml + - .github/workflows/tag.yml + - ai/context + - docs/phases + - hooks + - testing + - tooling/rebrand + - tooling/bootstrap + - tooling/catalog + - apps/api/docs + - tests/api/docs + - specs/epics/ai-slackbot + - README.md + doneWhen: + - every Baseline CI tier is green + - a bot placed from it carries the bot only, branded, with the template's licence and policy intact + gates: []