fabro/apps/fabro-web/app/hooks/use-run-toasts.ts
Bryan Helmkamp bc31b8d23a
Serve the run stream as the only run event API
Step 4 of the legacy executor deletion, third commit: the legacy event
API and every reader of it go, so that the next commits can delete the
event log, its reducer and the types beneath them.

The API:
- `GET /runs/{id}/events` pages the run stream only
  (`PaginatedRunStreamList` by `after`); the legacy `since_seq`,
  `before_seq` and `order` cursors, the `oneOf` envelope, the legacy
  `EventEnvelope`, `PaginatedEventList`, `RunEvent`, `EventSeq`,
  `AppendEventResponse` and `RunEventDetailResponse` schemas,
  `POST /runs/{id}/events`, `GET /runs/{id}/events/{seq}` and
  `GET /runs/{id}/stages/{stageId}/events` are deleted. `GET
  /runs/{id}/attach` and `GET /attach` frame `RunStreamItem`s only.
- The Rust and TypeScript clients regenerate; the removed models leave
  the TypeScript package.

The readers:
- `fabro-client` drops the legacy run event listing, tail and attach
  methods and `RunEventStream`; `list_run_stream_until` bounds a stream
  read.
- `fabro-tool`'s `fabro_run_events` lists, searches and details the run
  stream: `after` is the exclusive `stream_seq` cursor, `event_id` the
  item's id, filters match the item's name and `recorded_at`.
- `fabro-dump` writes the stream to `events.jsonl`; `fabro dump` reads
  it.
- The CLI's progress renderer keeps only what the run stream drives:
  the legacy event conversion, the sandbox and setup displays and their
  styles go. `fabro system events` prints stream items.
- The server's demo mode folds its agent fixture straight into the
  session projection and answers the attach stub with a stream item;
  the demo stage events endpoint is gone.
- The web app: every run is a Petri run. The legacy event hooks,
  renderer props, stage popover summary, run phases derivation and
  live-event payload handling are deleted or ported to `RunStreamItem`;
  toasts and board refreshes read the stream's platform records.
- Tests: the legacy API round trips and pagination tests are deleted;
  the CLI's MCP, attach and system event mocks serve stream pages; the
  CLI test helpers read stream items.

Still failing until the later commits: the CLI tests seeded through
`POST /runs/{id}/events`, the server tests over the legacy store, and
the legacy type tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 13:36:00 -04:00

76 lines
2.6 KiB
TypeScript

import { useEffect, useRef } from "react";
import type { RunStreamItem } from "@qltysh/fabro-api-client";
import { useToast } from "../components/toast";
import { eventDedupeKey } from "../lib/cross-tab-sse";
import {
petriBody,
petriEventName,
platformRecordKind,
} from "../lib/petri-stream";
import { subscribeToRunEvents } from "../lib/run-events";
import type { MutateFn } from "../lib/sse";
import { getBool, getObject, getString } from "../lib/unknown";
const NOOP_MUTATE = (() => undefined) as MutateFn;
const DEDUPE_WINDOW = 256;
/**
* Synchronizes toast notifications with a run-scoped SSE stream. Changing
* `runId` resubscribes, and the active subscription is closed on unmount.
*/
export function useRunToasts(runId: string | undefined) {
const { push } = useToast();
const seenItemKeysRef = useRef(new Set<string>());
useEffect(() => {
if (!runId) return;
const seen = new Set<string>();
seenItemKeysRef.current = seen;
return subscribeToRunEvents(runId, NOOP_MUTATE, undefined, {
onItem: (item) => {
const dedupeKey = eventDedupeKey(item);
if (dedupeKey) {
if (seen.has(dedupeKey)) return;
seen.add(dedupeKey);
if (seen.size > DEDUPE_WINDOW) {
// Set iteration order is insertion order; drop the oldest.
const oldest = seen.values().next().value;
if (oldest !== undefined) seen.delete(oldest);
}
}
const message = steeringToastMessage(item);
if (message) {
push({ message });
}
},
});
}, [push, runId]);
}
/**
* The toast a steering item earns: a `control.requested` that delivers a
* `$steer` (queued until an agent stage runs when Petri says it is not
* deliverable) or cancels the firing (an interrupt), and the `run.notice`
* the worker records when it refuses a steer.
*/
export function steeringToastMessage(item: RunStreamItem): string | null {
if (platformRecordKind(item) === "run.notice") {
const record = getObject(item.item, "record");
if (getString(record, "code") !== "steer_refused") return null;
return getString(record, "message") ?? "Steer refused: no agent stage is running.";
}
if (petriEventName(item) !== "control.requested") return null;
const ctl = getObject(petriBody(item), "ctl");
if (!ctl) return null;
if (getObject(ctl, "deliver")?.$steer !== undefined) {
const deliverable = getBool(getObject(item.item, "derived"), "deliverable");
return deliverable === false
? "Steer queued — will apply when an agent stage runs."
: "Steer delivered.";
}
if (ctl.cancel !== undefined) return "Agent interrupted.";
return null;
}