Skip to content

API Reference parrot_formdesigner.services.event_dispatcher

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.event_dispatcher

View source

Form lifecycle event dispatcher — FEAT-188.

Implements the central orchestration coroutine dispatch(...) that resolves a FormEventBinding from a FormSchema, looks up the registered handler in the tenant-scoped event_registry, runs it, and returns a normalised EventResolution.

Responsibilities

  1. Resolve the binding for event from form.events (or None if the field does not exist / is not declared).
  2. If no binding → return an empty EventResolution() immediately (no-op, no registry lookup).
  3. Look up the handler via get_form_event(handler_ref, tenant=tenant).
    • required=True + KeyError → RuntimeError (configuration error).
    • required=False + KeyError → log warning + return empty EventResolution().
  4. Build FormEventContext and await handler(ctx).
  5. Normalise None return → empty EventResolution().
  6. Re-raise FormEventAbort intact (the caller converts it to HTTP).
  7. Let all other exceptions propagate; the caller decides whether to dispatch onError and how to respond.

Also exposes apply_schema_overrides(base, overrides) — a pure helper for shallow-merging EventResolution.schema_overrides into a serialised FormSchema dict (top-level keys only, per spec §7).

FEAT-329 adds dispatch_visit(...) — the same pattern for the visit.* namespace (visit / assignment lifecycle). Visit events reuse the FEAT-188 string-keyed tenant-scoped registry: the visit event name itself acts as the handler_ref ("visit.onArrival" vs "<form_id>.onBeforeSubmit" — the namespace disambiguates). No FormSchema / binding / schema_dump is involved in the visit path.

Alias EventResolution

Re-exported from parrot_formdesigner.core.events.EventResolution.

Alias FormEventAbort

Re-exported from parrot_formdesigner.core.events.FormEventAbort.

Alias FormEventContext

Re-exported from parrot_formdesigner.core.events.FormEventContext.

Alias FormEventName

Re-exported from parrot_formdesigner.core.events.FormEventName.

Alias FormSchema

Re-exported from parrot_formdesigner.core.schema.FormSchema.

Alias VisitEventContext

Re-exported from parrot_formdesigner.core.events.VisitEventContext.

Alias VisitEventName

Re-exported from parrot_formdesigner.core.events.VisitEventName.

Function apply_schema_overrides

Shallow-merge overrides onto a copy of base.

Only top-level keys are replaced. Nested structures in base that share a key with overrides are entirely replaced by the value from overrides (no deep merge). This is intentional per spec §7 MVP decision; deep merge is deferred to a follow-up.

Example::

>>> apply_schema_overrides({"a": 1, "b": {"x": 1}}, {"b": {"y": 2}})
{"a": 1, "b": {"y": 2}}  # nested "x" is dropped — shallow only

apply_schema_overrides(base: dict[str, Any], overrides: Mapping[str, Any]) -> dict[str, Any]

Parameter Type Default Description
base dict[str, Any] The serialised FormSchema dict to patch.
overrides Mapping[str, Any] Top-level key/value pairs to merge in.

Returns: dict[str, Any] — A new dict with overrides applied (base is not mutated).

Function dispatch

Resolve and run the handler bound to event for form.

dispatch(event: FormEventName, *, form: FormSchema, request: web.Request, tenant: str | None, auth_context: Any, payload: Mapping[str, Any] | None = None, schema_dump: Mapping[str, Any] | None = None, error: BaseException | None = None, submission_id: str | None = None, llm_validation: Mapping[str, Any] | None = None) -> EventResolution

Parameter Type Default Description
event FormEventName The lifecycle event name to dispatch (e.g. "onBeforeSubmit").
form FormSchema The FormSchema whose events config is inspected.
request web.Request The current aiohttp Request (passed to the context for handler access if needed; the dispatcher itself does not read it).
tenant str | None Tenant slug used for registry lookup with global fallback.
auth_context Any Resolved authentication credentials (typed Any to avoid a circular import; typically an AuthContext instance).
payload Mapping[str, Any] | None None Submission data (present for submit events).
schema_dump Mapping[str, Any] | None None Rendered schema dict (present for open / schema_loaded).
error BaseException | None None The exception that triggered onError (if applicable).
submission_id str | None None Id of the committed submission (onAfterSubmit only).
llm_validation Mapping[str, Any] | None None The persisted context["llm_validation"] plausibility verdict, or None when no check was configured or requested (onAfterSubmit only).

Returns: EventResolution — An EventResolution. If no binding is declared for event on form, returns an empty EventResolution() (no-op).

Raises:

  • FormEventAbort — Re-raised intact when the handler aborts a before* event. The calling handler in FormAPIHandler converts it to an HTTP response.
  • RuntimeError — When the binding has required=True but no handler is registered for handler_ref.
  • Exception — Any other exception raised by the handler propagates unchanged. The caller decides whether to dispatch onError and how to respond.

Function dispatch_visit

Resolve and run the handler bound to a visit lifecycle event.

Mirror of :func:dispatch for the visit.* namespace (FEAT-329). Reuses the FEAT-188 tenant-scoped registry: handlers are registered with @register_form_event("visit.onArrival") (optionally with tenant="<slug>") and the visit event name itself acts as the handler_ref. No FormSchema / binding / schema_dump is involved in the visit path.

Hook semantics (per FEAT-329 spec §2):

  • Pre-hooks (visit.onArrival): FormEventAbort raised by the handler is re-raised intact (the caller converts it to an HTTP status_code + user_message); other exceptions propagate unchanged, mirroring the before* form events.
  • Post-hooks (all other visit events): fire-and-forget — any exception raised by the handler (including FormEventAbort) is logged and swallowed so side-effects never break the visit flow.

dispatch_visit(event: VisitEventName, *, tenant: str | None, auth_context: Any, event_id: str | None = None, shift_id: str | None = None, visit_id: str | None = None, staff_id: str | None = None, payload: Mapping[str, Any] | None = None, extra: dict[str, Any] | None = None, handler_ref: str | None = None) -> EventResolution

Parameter Type Default Description
event VisitEventName The visit lifecycle event name to dispatch (e.g. "visit.onArrival").
tenant str | None Tenant slug used for registry lookup with global fallback.
auth_context Any Resolved authentication credentials (typed Any to avoid a circular import; typically an AuthContext instance).
event_id str | None None Identifier of the parent Event (FEAT-303), if any.
shift_id str | None None Identifier of the Shift the visit belongs to, if any.
visit_id str | None None Identifier of the Visit being processed, if any.
staff_id str | None None Identifier of the staff member performing the visit.
payload Mapping[str, Any] | None None Event-specific data (artifact metadata, GPS fix, ...).
extra dict[str, Any] | None None Free-form bag for correlation IDs, tracing data, etc.
handler_ref str | None None Registry key to look up. Defaults to event itself (the visit.* namespace disambiguates it from form-scoped '<form_id>.<event>' refs in the shared registry).

Returns: EventResolution — An EventResolution. If no handler is registered for handler_ref (tenant-specific or global), returns an empty EventResolution() (no-op).

Raises:

  • FormEventAbort — Re-raised intact when a pre-hook handler aborts (visit.onArrival). Never raised for post-hooks.
  • Exception — Any other exception raised by a pre-hook handler propagates unchanged. Post-hook exceptions are logged and swallowed (fire-and-forget).

Alias get_form_event

Re-exported from parrot_formdesigner.services.event_registry.get_form_event.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally