On the demo account's first upload ever (room.uploads from INGEST 3 is 1), tellJoel posts an inquiry-shaped message to the mailer Worker through the MAILER service binding (the Worker's direct connection to the mailer Worker). upload() then answers 200 with the file's name, page count, skipped pages, chunk count and characters, and with headers the site's Pages Function reads for its demo_upload event.
demo/src/app.ts32 / 32 lines on this page explainedlines 385-393,439-461 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
// INGEST 7 · tell Joel about a demo's first upload, and reply// Once per demo, at its first upload ever: a prospect uploading again after their documents were// cleared, or after removing every file, is not a new demo.
Once per account Never runs
WhatThe stage marker, and a comment stating the rule: the email goes at the demo account's first upload ever, and an account whose documents were cleared, or whose files were all removed, does not send it again.
Whyuploads goes up only at INGEST 3 and down only when INGEST 5 undoes a failed upload; neither the idle purge nor a removal decrements it. So the email goes with the stored upload that took the count to 1. If two uploads run at once and the one that got 1 then fails at INGEST 5, no email is ever sent for the account. The email can be missed, but it is never sent twice.
WhatWhen room.uploads, returned by INGEST 3's UPDATE, is 1 and the MAILER binding exists, upload() awaits tellJoel with the account's row, the file name and its page count.
Whyuploads is incremented and read in one statement, so two first uploads arriving together get 1 and 2, never both 1. Awaiting tellJoel delays the reply by one mailer call, and only for the first upload.
// The reply tells the page what was indexed; the X-Demo-* headers are for the site's own event record.returnjson(200, { name, pages: pageCount, skipped, chunks: chunks.length, chars }, about(demo))}
The reply Ingestion
What200 with { name, pages: pageCount, skipped, chunks: chunks.length, chars }. about(demo) adds X-Demo-Id and, when the contact has an address in angle brackets, X-Demo-Email.
WhyThe page draws the file's row from name, pages and skipped, the same shape the session returns for every file. The site's Pages Function reads name, pages and chars and the two headers for its demo_upload event, then deletes the headers before the response reaches the browser.
/** * Emails Joel, through the mailer, that a new demo has started, sent as an inquiry from the demo's * contact. A failure is logged and never fails the upload. */
What it sends Never runs
WhatThe doc comment states what tellJoel sends and that a failure is logged and never fails the upload.
WhyThe mailer is a separate Worker (service joelstevick-com-mailer in demo/wrangler.toml) outside this repository; the demo Worker knows only its request shape.
asyncfunctiontellJoel(mailer: Fetcher, demo: Demo, file: string, pages: number): Promise<void> {// The contact is "Name <address>"; without one, the name is a placeholder and the address is Joel's own.const [, name = demo.contact ?? 'A demo visitor', email = '[email protected]'] = /^(.*) <(.*)>$/.exec(demo.contact ?? '') ?? []
Who it is from Ingestion
WhattellJoel takes the MAILER binding, the account's row (demo), the file name and the page count, and returns nothing. demo.contact is matched against "Name <address>". With no match, the name is the whole contact, or "A demo visitor" when there is none, and the address is Joel's own.
WhyThe mailer expects the contact form's fields. Defaulting the address to Joel's own keeps the request valid for a demo account created without a contact.
try {// The body has the contact form's shape; budget 'rag' files it under the RAG pipeline and eval harness.await mailer.fetch('https://mailer/', {method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ name, email, company: demo.company ?? '', budget: 'rag',message: `DEMO STARTED (demo ${demo.id})\nCompany: ${demo.company ?? '(not given)'}\nFirst upload: ${file}\nFiles: 1, pages: ${pages}`, }), })
POST to the mailer Ingestion
WhatA JSON POST through the MAILER service binding with the contact form's fields: name, email, company ('' when there is none), budget 'rag', and a message headed DEMO STARTED that gives the account's id, the company or "(not given)", the first file's name, "Files: 1" and its page count.
WhyWith the contact form's shape, the mailer files the new demo account like any other inquiry, under the RAG pipeline and eval harness ('rag'). The URL's host is a placeholder, which a service binding ignores.
} catch (error) {console.error('demo upload: the mailer could not be reached', error) }}
Never fail the upload Ingestion
WhatA thrown error, such as an unreachable binding, is caught and logged, and tellJoel returns normally.
WhyThe visitor's upload has already succeeded, so a notification problem must not turn it into an error. fetch does not throw on an HTTP error status, and the response is not checked, so a mailer that answers 500 is not logged either.