const AC = "https://aesthetic.computer"; const escapeHtml = (value) => String(value) .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); async function timedFetch(fetcher, url, options = {}) { const started = Date.now(); try { const response = await fetcher(url, { redirect: options.redirect || "follow", signal: AbortSignal.timeout(options.timeout || 8000), headers: options.headers, }); return { response, ms: Date.now() - started }; } catch (error) { return { error: error?.message || String(error), ms: Date.now() - started }; } } export async function collectStatus(fetcher = fetch, handle = null) { const checks = []; const check = async (id, label, url, validate) => { const result = await timedFetch(fetcher, url); let detail = result.error || `HTTP ${result.response?.status || 0}`; let data; if (result.response) { try { data = await result.response.clone().json(); } catch {} } const ok = Boolean(result.response?.ok && (!validate || validate(data, result.response))); if (ok) detail = `HTTP ${result.response.status}`; checks.push({ id, label, url, ok, ms: result.ms, detail, data }); return { ...result, data, ok }; }; await check("front-door", "Aesthetic Computer", AC); await check( "api-docs", "API abstraction", `${AC}/api`, ); await check( "database-read", "Database read path", `${AC}/api/chat-messages?instance=system&limit=1`, (data) => Array.isArray(data?.messages), ); await check( "asset-delivery", "DigitalOcean asset delivery", "https://assets.aesthetic.computer/aesthetic-inc/pals.png", (_, response) => (response.headers.get("content-type") || "").startsWith("image/"), ); await check( "upload-presign", "DigitalOcean upload signing", `${AC}/presigned-upload-url/png`, (data) => Boolean(data?.uploadURL && data?.slug), ); if (handle) { const encodedHandle = encodeURIComponent(handle); await check( "profile", `${handle} profile`, `${AC}/api/profile/${encodedHandle}`, (data) => Boolean(data?.sub), ); const collection = await check( "portfolio", `${handle} painting index`, `${AC}/media-collection?for=${encodedHandle}/painting`, (data) => Array.isArray(data?.files), ); const latestUrl = collection.data?.files?.at(-1); if (latestUrl) { await check( "latest-painting", `${handle} latest painting`, latestUrl, (_, response) => (response.headers.get("content-type") || "").startsWith("image/"), ); } } return { ok: checks.every((item) => item.ok), checkedAt: new Date().toISOString(), ...(handle ? { handle } : {}), checks: checks.map(({ data, ...item }) => item), }; } function render(status) { const cards = status.checks.map((item) => `
${escapeHtml(item.label)} ${escapeHtml(item.detail)} · ${item.ms}ms
`).join(""); return ` prompt.ac status

status.prompt.ac

${status.ok ? "All watched paths are operational." : "One or more watched paths need attention."}

${cards}
`; } async function saveSnapshot(env, status) { if (!env?.STATUS_HISTORY) return; const key = `snapshot:${status.checkedAt}`; await env.STATUS_HISTORY.put(key, JSON.stringify(status), { expirationTtl: 60 * 60 * 24 * 30 }); await env.STATUS_HISTORY.put("latest", JSON.stringify(status)); } export default { async fetch(request, env) { const url = new URL(request.url); const status = await collectStatus(fetch, url.searchParams.get("handle")); await saveSnapshot(env, status); if (url.pathname === "/api/status") { return Response.json(status, { status: status.ok ? 200 : 503, headers: { "cache-control": "public, max-age=30" }, }); } return new Response(render(status), { status: 200, headers: { "content-type": "text/html; charset=utf-8", "cache-control": "public, max-age=30" }, }); }, async scheduled(_controller, env) { const status = await collectStatus(); await saveSnapshot(env, status); }, };