gitflarePublicPartly agent-ready50
cf-workflows-pipeline
Durable content pipeline on Cloudflare Workflows: ingest, extract, summarise with Workers AI via AI Gateway, human approval, publish.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
629b2faOct 20, 2026
Fix pipeline runs failing at ingest (steps.attempt NOT NULL)
629b2faimport type { Env, RunParams } from "./env";import { parsePlan } from "./lib/inject";import { validateUrl } from "./lib/validate";import { buildTimeline, type StepRow } from "./lib/timeline";export { ContentPipeline } from "./workflow"; const json = (d: unknown, status = 200) => Response.json(d, { status, headers: { "cache-control": "no-store" } });const CODES: Record<number, string> = { 400: "bad_request", 404: "not_found", 405: "method_not_allowed", 409: "conflict", 500: "internal_error" };const fail = (status: number, message: string) => json({ error: { code: CODES[status] ?? "error", message } }, status);const SEC: Record<string, string> = { "x-content-type-options": "nosniff", "referrer-policy": "no-referrer", "x-frame-options": "DENY", "content-security-policy": "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'",};const esc = (s: string) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`); async function route(req: Request, env: Env): Promise<Response> { const url = new URL(req.url); const path = url.pathname; if (path === "/health") return json({ ok: true, service: "cf-workflows-pipeline" }); if (path === "/api/runs" && req.method === "POST") { const b = (await req.json().catch(() => null)) as Record<string, unknown> | null; if (!b) return fail(400, "invalid json"); const text = typeof b.text === "string" ? b.text.trim() : ""; let src = ""; if (!text) { const v = validateUrl(String(b.url ?? "")); if (!v.ok) return fail(400, v.error); src = v.url; } const length = (["short", "medium", "long"].includes(String(b.length)) ? b.length : "medium") as RunParams["length"]; const failures = parsePlan(b.failures); const runId = crypto.randomUUID().slice(0, 8); const params: RunParams = { runId, length, failures, ...(text ? { text: text.slice(0, 200000) } : { url: src }) }; const now = Date.now(); await env.DB.prepare("INSERT INTO runs (id,status,source,length,params,created_at,updated_at) VALUES (?1,'queued',?2,?3,?4,?5,?5)") .bind(runId, src || "pasted text", length, JSON.stringify(params), now).run(); await env.PIPELINE.create({ id: runId, params }); return json({ id: runId }, 201); } if (path === "/api/runs" && req.method === "GET") { const r = await env.DB.prepare("SELECT id,status,source,length,created_at,updated_at FROM runs ORDER BY created_at DESC LIMIT 50").all(); return json({ runs: r.results }); } const m = /^\/api\/runs\/([\w-]+)(?:\/(approve|reject))?$/.exec(path); if (m) { const [, id, action] = m; const run = await env.DB.prepare("SELECT id,status,source,length,note,created_at,updated_at FROM runs WHERE id=?1").bind(id).first(); if (!run) return fail(404, "not found"); if (!action && req.method === "GET") { const rows = await env.DB.prepare("SELECT name,state,attempt,started_at,ended_at,output_excerpt,error FROM steps WHERE run_id=?1").bind(id).all<StepRow>(); let instance: unknown = null; try { instance = await (await env.PIPELINE.get(id)).status(); } catch { /* optional */ } return json({ run, steps: buildTimeline(rows.results, Date.now()), instance }); } if (action && req.method === "POST") { if (run.status !== "awaiting_approval") return fail(409, "run is not awaiting approval"); const b = (await req.json().catch(() => ({}))) as { note?: string }; const approved = action === "approve"; await (await env.PIPELINE.get(id)).sendEvent({ type: "approval", payload: { approved, note: String(b.note ?? "").slice(0, 500) } }); await env.DB.prepare("UPDATE runs SET note=?1, updated_at=?2 WHERE id=?3").bind(String(b.note ?? ""), Date.now(), id).run(); return json({ ok: true }); } return fail(405, "method not allowed"); } const p = /^\/p\/([\w-]+)$/.exec(path); if (p) { const row = await env.DB.prepare("SELECT title,summary,source_url FROM published WHERE id=?1").bind(p[1]).first<{ title: string; summary: string; source_url: string | null }>(); if (!row) return fail(404, "not found"); const src = row.source_url && /^https?:/.test(row.source_url) ? `<p><a href="${esc(row.source_url)}" rel="noopener">Source</a></p>` : ""; return new Response(`<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${esc(row.title)}</title><link rel="stylesheet" href="/style.css"><main class="pub"><h1>${esc(row.title)}</h1><p>${esc(row.summary)}</p>${src}</main>`, { headers: { "content-type": "text/html; charset=utf-8" } }); } if (path === "/runs" || /^\/runs\/[\w-]+$/.test(path)) return env.ASSETS.fetch(new Request(new URL("/", req.url), req)); return env.ASSETS.fetch(req);} export async function handle(req: Request, env: Env): Promise<Response> { const t0 = Date.now(); let res: Response; try { res = await route(req, env); } catch (e) { console.log(JSON.stringify({ level: "error", msg: "unhandled", path: new URL(req.url).pathname, error: String(e) })); res = fail(500, "internal error"); } const out = new Response(res.body, res); for (const [k, v] of Object.entries(SEC)) out.headers.set(k, v); console.log(JSON.stringify({ level: "info", method: req.method, path: new URL(req.url).pathname, status: out.status, ms: Date.now() - t0 })); return out;} export default { fetch: (req: Request, env: Env) => handle(req, env) } satisfies ExportedHandler<Env>;