SpaceMIX v0.1.0

Events

experimentalfor bothsince protocol 1

host.events: emit the events your manifest declares, and with the events permission receive, list, catalog and claim every extension's events.

An event says something happened in an extension: a task was completed, a mail arrived. Any other extension can act on it (an automation, an activity log). The core keeps a capped log of recent events and delivers new ones live to every window and browser.

interface SpacemixHost {
  events: EventsApi; // always present
}

interface EventsApi {
  emit(event: EventInput): Promise<SpacemixEvent>;
  catalog(): Promise<EventCatalogEntry[]>;                 // needs `events`
  list(query?: EventQuery): Promise<SpacemixEvent[]>;      // needs `events`
  subscribe(fn: (event: SpacemixEvent) => void): () => void; // needs `events`
  claim(eventId: string): Promise<boolean>;                // needs `events`
}

interface EventInput {
  type: string;
  payload?: Record<string, unknown>;
  summary?: string;
  target?: string;
}

interface SpacemixEvent {
  id: string;        // sorts by time
  source: string;    // emitting extension's id, set by the host; "api" for an event sent with a SpaceMIX API key
  type: string;
  payload: Record<string, unknown>;
  summary: string | null;
  target: string | null;
  by: string;        // user id of the session that emitted it; "" when the server itself did
  createdAt: number; // ms since the epoch
}

interface EventQuery {
  source?: string;   // an extension id, or "api"
  type?: string;
  after?: string;  // exclusive
  before?: string; // exclusive
  limit?: number;  // default 50, at most 500
}

interface EventCatalogEntry {
  extensionId: string;
  extensionName: string;
  icon: string;
  events: EventDefinition[];
}

Permissions: emitting needs none. Receiving (catalog, list, subscribe, claim) needs events, because an extension with it sees every extension's events, payloads included. The methods are always on host.events; without the permission, catalog, list and claim reject with <Name> doesn't have the "events" permission. and subscribe never calls back (the failure is only logged).

What gets emitted

  • Declared events. The manifest's events (EventDefinition, see manifest.md); you emit them with emit.
  • Collection events. For every collection, the host itself records <collection>_created, <collection>_updated (with changed) and <collection>_deleted on every write through the collection API, whether the view, an action or the assistant made it. You can't emit these yourself. collectionEvents(def) and eventsOf(manifest) (exported from @spacemix/sdk) give their definitions.
  • API events. Events sent by a script or service with an API key through the SpaceMIX API (POST /api/v1/events/emit). They are not any extension's (see below).

API events

An event sent with an API key has source: "api", which is never an extension's id. Its type is free (any snake_case name up to 64 characters, not declared anywhere), and its payload is any object the caller sent, not validated against a schema, with payload.via = { apiKey: <the key's name> } set by the server. by is the key's account. API events show up in list and subscribe like any other, but not in catalog, which lists only installed extensions. A type can be the same as one of yours, so check source as well as type before acting on an event.

host.events.emit

emit(event: EventInput): Promise<SpacemixEvent>;

Records an event and resolves with it as stored. Bridge method: events.emit.

  • type: a declared event's name. A collection event's name rejects with "<type>" is emitted by SpaceMIX itself whenever the collection changes; don't emit it.; any other undeclared name rejects, listing the declared ones.
  • payload: an object. Fields the event's schema doesn't declare are dropped; the rest are validated like collection fields (collections.md), so required fields must be there. An event declared without a schema takes no payload (a non-empty one rejects). At most 16 KB as JSON.
  • summary: one line of plain text for logs and notifications, cleaned like notification text; at most 280 characters after cleaning (longer rejects). Empty means none.
  • target: where in your view it happened, opaque like a notification target; up to 512 characters, no control characters.

The host sets source (your extension id), by (the signed-in person), id and createdAt.

host.events.catalog

catalog(): Promise<EventCatalogEntry[]>;

Every installed extension that can emit anything, with its collection events first and then its declared ones, including titles, descriptions and payload schemas: what to offer the user as triggers. Bridge method: events.catalog.

host.events.list

list(query?: EventQuery): Promise<SpacemixEvent[]>;

Recent events, newest first, filtered by source and type, strictly between after and before (event ids). limit defaults to 50 and is clamped to 1–500. A malformed id rejects with "<id>" isn't an event id. Bridge method: events.list.

Retention. The log keeps the newest 10 000 events and nothing older than 30 days. A list call looks at no more than 5 000 events to find matches.

Catching up. Ids sort by time. Keep the id of the last event you handled and call list({ after: lastId }). If a full page comes back, page further back with before set to the oldest id you got until you reach your cursor, then handle the events oldest first.

host.events.subscribe

subscribe(fn: (event: SpacemixEvent) => void): () => void;

Calls fn for every new event from any extension, window or browser, this one included, as long as the frame runs. SpaceMIX starts the frame of every installed extension with events in the background when someone signs in, so you hear events with your view closed. Events emitted while no window is open are not replayed: use list to catch up. Channel: events.

host.events.claim

claim(eventId: string): Promise<boolean>;

Resolves true for exactly one caller per event and extension, and false for every later one. Every open window and browser runs your frame, so every one of them hears each event; claim before doing anything that must happen once (send a mail, post a message). The core decides atomically. Claims are per extension: two different extensions can each claim the same event once. Bridge method: events.claim.

Rejects with No event with id "<id>"; it may have been pruned. for an event no longer in the log.

While a paid extension is turned off on the server, its emit and claim reject with <Name> is turned off on this server until it's paid for., and it starts no frame to hear events (see Turned off until it's paid for).

Example

import { host, type SpacemixEvent } from "@spacemix/sdk";

export function onTaskCompleted(handle: (e: SpacemixEvent) => Promise<void>): () => void {
  return host.events.subscribe(async (e) => {
    if (e.source !== "ai.spacemix.tasks" || e.type !== "task_completed") return;
    if (!(await host.events.claim(e.id))) return; // another window has it
    await handle(e);
  });
}

Stability

Experimental: built and unit-tested, not yet exercised live with several windows and extensions. The shapes are expected to stay; limits may change.

This page as markdown · All pages as one file