@sgrs/client-ts
    Preparing search index...

    Class EventsApi

    Index
    • Invoke handler and route errors to onHandlerError callback or console.error.

      Extracted as a named method so it is directly testable and so the error routing logic is shared between _sub() and subscribeAudit(). Never re-throws inside a void IIFE — that would create an unhandled rejection rather than a visible NATS connection error (HIGH-NEW-9 fix).

      Type Parameters

      Parameters

      Returns Promise<void>

    • Gracefully drain in-flight messages and close the NATS connection. Stops all JetStream consumers, then drains core subscriptions.

      Returns Promise<void>

    • Connect to NATS. No-op (no error) when NATS is not configured. Concurrency-safe: concurrent calls share the same connection attempt.

      Returns Promise<void>

    • Create a durable JetStream audit stream for tenant (idempotent). Requires JetStream enabled on the server: nats-server --jetstream

      Parameters

      • tenant: string
      • OptionalstreamName: string

      Returns Promise<void>

    • Join a queue group — NATS delivers each task to exactly ONE worker. Built-in load balancing across all workers in the same group name.

      Type Parameters

      • T = unknown

      Parameters

      • tenant: string
      • taskType: string
      • handler: TaskHandler<T>
      • groupName: string = "workers"

      Returns Subscription

    • Subscribe to all events for a tenant across scope, model, and agent subjects. Returns a named object so callers can unsubscribe each category individually.

      Parameters

      Returns { agent: Subscription; model: Subscription; scope: Subscription }

    • Subscribe to ALL scope events for a tenant across every scope and every event type — no type filtering applied.

      Use this for SSE relay routes that forward the full event stream to clients. Any new scope event type is automatically included without code changes here.

      Parameters

      Returns Subscription

    • Subscribe to veto activations for a tenant.

      ⚠ CRITICAL PATH — veto must halt ALL swarm agents in <10ms.

      Recommended pattern:

      client.events.onVetoActivated('acme', async (event) => {
      await swarm.halt(event.scopeId, { reason: event.reason });
      });

      Set nats.onHandlerError in ClientConfig to ensure veto failures are surfaced and trigger fallback halt logic.

      Parameters

      Returns Subscription

    • Publish a typed scope event. Tenant ownership is enforced: the derived subject must belong to tenant.

      Parameters

      Returns void

    • Subscribe to the durable audit stream, replaying all events from the beginning.

      Uses the nats.js v2 Consumers API with correct enum deliver policy (H-4/H-5). The consumer messages handle is stored in _consumers and stopped on close() (H-6 fix). Must call enableAuditStream(tenant) first (idempotent, safe to call again).

      Parameters

      • tenant: string
      • handler: EventHandler
      • OptionalconsumerName: string
      • OptionalstreamName: string

      Returns Promise<() => void>

      Cleanup function — call it to stop this specific audit subscription