Skip to content

gitflarePublicPartly agent-ready50

cf-workflows-pipeline

Durable content pipeline on Cloudflare Workflows: ingest, extract, summarise with Workers AI via AI Gateway, human approval, publish.

Fix pipeline runs failing at ingest (steps.attempt NOT NULL)629b2fa
JavaScript60 lines3.2 KB
export const STEPS = ["ingest", "extract", "summarise", "approval", "publish"];const LABELS = { queued: "Queued", running: "Running", retrying: "Retrying", waiting: "Waiting for approval", done: "Done", failed: "Failed", rejected: "Rejected", published: "Published" };export function normState(s) {  const v = String(s || "queued").toLowerCase();  if (["complete", "completed", "success", "succeeded"].includes(v)) return "done";  if (["waiting", "waiting_approval", "awaiting_approval", "paused"].includes(v)) return "waiting";  if (["error", "errored", "terminated"].includes(v)) return "failed";  return LABELS[v] ? v : "queued";}export function stateLabel(state, attempt = 1, max = 3) {  const s = normState(state);  return s === "retrying" ? `Retrying ${attempt}/${max}` : LABELS[s];}export function formatDuration(ms) {  if (ms == null || !isFinite(ms) || ms < 0) return "–";  if (ms < 1000) return `${Math.round(ms)} ms`;  if (ms < 60000) return `${(ms / 1000).toFixed(1)} s`;  return `${Math.floor(ms / 60000)} m ${Math.round((ms % 60000) / 1000)} s`;}export function stepDuration(step, now = Date.now()) {  const a = step.started_at ?? step.startedAt, b = step.ended_at ?? step.endedAt;  if (a == null) return null;  return (b ?? now) - a;}export function buildTimeline(steps = [], now = Date.now()) {  const by = new Map(steps.map((s) => [s.name, s]));  const names = [...STEPS, ...steps.map((s) => s.name).filter((n) => !STEPS.includes(n))];  return names.map((name) => {    const s = by.get(name) || {};    let state = normState(s.state);    const attempt = Number(s.attempt) || (s.state ? 1 : 0);    if (state === "running" && attempt > 1) state = "retrying";    return { name, state, attempt, label: stateLabel(state, attempt), duration: formatDuration(stepDuration(s, now)),      preview: preview(s.output_excerpt ?? s.output), error: s.error || "", active: state === "running" || state === "retrying" || state === "waiting" };  });}export function preview(t, max = 280) {  const s = String(t ?? "").replace(/\s+/g, " ").trim();  return s.length > max ? s.slice(0, max - 1) + "…" : s;}export const isFinished = (status) => ["done", "failed", "rejected", "published"].includes(normState(status));export function filterRuns(runs, filter) {  if (!filter || filter === "all") return runs;  return runs.filter((r) => normState(r.status) === filter);}export function validateForm(f) {  const errors = {};  const url = (f.url || "").trim(), text = (f.text || "").trim();  if (!url && !text) errors.source = "Enter a URL or paste some text.";  else if (url) { try { const u = new URL(url); if (!/^https?:$/.test(u.protocol)) errors.source = "Only http and https URLs work."; } catch { errors.source = "That doesn't look like a valid URL."; } }  return errors;}export function buildPayload(f) {  const inject = {};  for (const n of ["extract", "summarise", "publish"]) { const c = Math.max(0, Math.min(3, parseInt(f.inject?.[n], 10) || 0)); if (c) inject[n] = c; }  const p = { length: ["short", "medium", "long"].includes(f.length) ? f.length : "medium", inject };  if ((f.url || "").trim()) p.url = f.url.trim(); else p.text = (f.text || "").trim();  return p;}