cloudflare / cloudflare/telescope

Telescopetest-io: Use Cloudflare workflows to remove AI content rating timeout and bottleneck in serving 'SAFE' assets

Open
#209 1 comment 0 reactions 0 assignees View on GitHub
telescopetest-io ticket
Dominant language
TypeScript
Stars
305
Forks
36
PR merge metrics
No merged PRs in 30d

Description

## Problem
1) AI content rating in telescopetest-io runs using `ctx.waitUntil()` which has a 30-second timeout. If AI models take longer, the rating fails silently and tests stay stuck as UNKNOWN/IN_PROGRESS.
2) Protecting each asset in R2 (accessed through the endpoint api/tests/{testId}/{filename}) puts a lot of strain on D1 (to fetch the content_rating of one test_id multiple times). This sometimes results in 500 errors (#221), and we need a more permanent solution than #224.

### Problem 1 Solution
Use Cloudflare Workflows to run AI rating without timeout limits.

#### What to Change
1. Create new workflow file `src/workflows/ai-content-rating.ts`
- 4 steps: mark in-progress → fetch R2 files → run AI → update DB
2. Update `wrangler.jsonc`
- Add workflow binding for dev/staging/prod
3. Update `src/pages/api/upload.ts`
- Remove `ctx.waitUntil()` block
- Replace with `env.AI_RATING_WORKFLOW.create({ params: { testId, url } })`
4. Update `worker-configuration.d.ts`
- Add `AI_RATING_WORKFLOW: Workflow` to Env interface
- No changes needed: Database schema, Repository functions, Detail page, Rating API endpoint, Frontend polling logic

#### How It Works
1. User uploads → API saves test → triggers workflow → returns immediately
2. Workflow runs in background (no timeout)
3. Frontend polls `/api/tests/[testId]/rating` until done
4. When workflow finishes, frontend detects rating changed

### Problem 2 Solution
- Ideal implementation we should serve test results directly from a public R2 bucket though CDN rather than using a worker for loading them from R2.
- Which means we do need to implement a private upload bucket where assets go before they are rated (if AI rating is enabled).
- Might be a good task to implement using those workflows.
- Most docs suggest standalone custom domain, but I can imagine we could map a subpath of the main domain to that bucket.
- If we set up separate buckets, we'd be able to do #216 much more easily

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.