Joel Stevick

The ask · ASK 1 · stage 1 of 7

Validate the question

ask() first checks that the question in the request body is a string of 1 to 500 characters after trimming, and the question goes on to ASK 2.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/app.ts6 / 6 lines on this page explainedlines 54-55,548-551 of 1124; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo/src/app.ts The demo Worker.

MAX_QUESTION

constant · lines 54–55View on GitHub

The longest question accepted.

/** The longest question accepted, in characters. */const MAX_QUESTION = 500

The longest question Module load

WhatThe limit is counted as JavaScript string length, in UTF-16 code units, so an emoji can count as two characters.

WhyThe question goes into the embedding call, the BM25 query, the reranker's prompt and the answer prompt, so its length bounds the cost of every model call it reaches. The page's question input has the same maxlength (demo.html line 80), which also counts UTF-16 code units, so the two limits agree.

MAX_QUESTION = 500

ask(): ASK 1

segment · lines 548–551View on GitHub

Checks the question's type and length.

Calls json() Shared helpers

  // ASK 1 · validate the question  const question = typeof body.question === 'string' ? body.question.trim() : ''

A string, trimmed Query time

Whatbody.question is typed unknown because the body was parsed from untrusted JSON, and a body that is not JSON became {}. A string is trimmed. Any other type (a number, an object, a missing field) becomes the empty string. One body fails differently. The JSON literal null parses without error, so body is null and reading body.question throws a TypeError. createApp's catch-all answers that with a 500, still before anything is reserved.

WhyMapping every wrong type to '' lets the one length check refuse them all with the same message. Trimming before the length check makes a question of whitespace alone count as empty, and the question that is stored and searched carries no padding.

  if (question === '' || question.length > MAX_QUESTION) return json(400, { error: `Please ask a question of up to ${MAX_QUESTION} characters.` })

Refuse before reserving Query time

WhatAn empty question, or one over 500 characters, gets a 400 with a JSON error message.

WhyThis check runs before ASK 2's UPDATE, so a refused question never uses one of the demo account's ten questions. The response is JSON even when the page asked for a stream, because nothing has been streamed yet, and the page tells a refusal from a stream by its Content-Type.

status 400