In this video, you added a Stop button to the club’s page and watched what it stopped. Aborting the page’s fetch() stopped the reply on the page, but the server kept reading the model’s reply to the end and paid for it. Then you made the route abort its own call to the model when the page’s connection closes.
Key Points
- A member may want to stop a reply. Mei sent her first message, then spotted a mistake in it (primero should be primera) and wanted to stop the reply and fix her message.
- The page aborts its
fetch()with anAbortController. Send gets anid, and a Stop button in the form hastype="button", so pressing it doesn’t send the form. The script finds both buttons and keeps a controller, so Stop works before any message. Each message turns Send off, so a second message can’t replace the controller Stop aborts. Each message also gets a new controller, since an aborted one stays aborted. The controller’s signal goes tofetch(), and a click on Stop aborts it. - The page catches the abort. An abort makes
fetch()or thefor awaitloop throw, so the request and the loop sit in atryblock. Thecatchkeeps the words already shown and adds[Stopped], or[Connection lost]for any other error, such as a dropped connection. Afinallyblock turns Send back on. - The server logs what each call costs. After the turn count, the route logs the call’s cost with
costInCents()from the starting project, and skips the line when a call reports no usage. - Stopping only the page doesn’t stop the model. In the video’s run, Mei pressed Stop after one second and the page showed
[Stopped]. Almost four seconds after Stop, the server logged the session at two turns and a cost of 0.051 cents, for 81 output tokens. The model wrote the whole reply, and the club paid for it. - There are two requests. Stop aborted the page’s request to the club’s server and closed that connection. The server’s call to the provider is a second request, and nothing aborted it, so the route’s loop kept reading the reply to the end. Each write into the closed connection fails silently, so nothing stopped the loop.
- The cost adds up. The club pays a fraction of a cent for every stopped reply. And the reply Mei never saw stays in the history, so every later request pays to send it again.
- The route aborts its own call when the page leaves. Before it calls
streamAnswer(), it makes anAbortControllerof its own. Its response to the page,res, has acloseevent, andres.on()listens for it, like.addEventListener()on the page. It fires when that connection closes, after a Stop, a closed tab or a lost network. It also fires after a normal finish, so the handler aborts only when the response hasn’t finished (res.writableFinishedisfalse). streamAnswer()gets the controller’s signal. It already passes that signal to the provider’s request, so aborting ends that request.- The stopped call ends at once. In the rerun, the server logged the aborted call and one turn right after Stop. The route’s
catchlogs it as a failure. That’s expected here, but a real feature would log a stop as a stop. - No cost line doesn’t mean free. There was no cost line, because the call ended before reporting any usage. The provider can still bill Mei’s message and anything written before the abort. But closing the request tells the provider to stop generating the rest.
- The stopped reply stays out of the history. The history kept Mei’s message alone, at its estimate. Then Mei sent her fixed message, and in this run, the reply streamed in.
A Stop button has to end two requests. The page aborts its fetch(), and the server notices the closed connection and aborts its call to the model.