import type { MutableAtomToken } from "atom.io" import type { Json } from "atom.io/foundations/json" /** The wire protocol version implemented by this release of Mosaic. */ export const MOSAIC_PROTOCOL_VERSION = 1 as const /** Stable transport event names shared by every Mosaic atom. */ export const MOSAIC_EVENTS = { join: `atom.io:mosaic:join`, operation: `atom.io:mosaic:operation`, presence: `atom.io:mosaic:presence`, rejection: `atom.io:mosaic:rejection`, snapshot: `atom.io:mosaic:snapshot`, } as const export type MosaicProtocolVersion = typeof MOSAIC_PROTOCOL_VERSION export type MosaicModelIdentifier = { /** Exact behavior-affecting configuration for this model variant. */ readonly configuration?: Json.Serializable readonly key: string readonly version: number } /** The ordinary mutable atom token identity carried over the wire. */ export type MosaicAtomAddress = { readonly family?: { readonly key: string readonly subKey: string } readonly key: string readonly type: `mutable_atom` } /** Copy the serializable address from a mutable atom or family-member token. */ export function mosaicAtomAddress( token: MutableAtomToken, ): MosaicAtomAddress { return { ...(token.family === undefined ? {} : { family: token.family }), key: token.key, type: `mutable_atom`, } } /** Stable map/storage key shared by clients, servers, and adapters. */ export function mosaicAtomAddressKey(address: MosaicAtomAddress): string { return JSON.stringify([ address.type, address.key, address.family?.key ?? null, address.family?.subKey ?? null, ]) } /** Metadata generated by one logical client session. */ export type MosaicProposalMetadata = { /** * Immediate operation IDs at the proposal's causal frontier. Model-owned IDs * may have been created by transitive ancestors rather than these operations. */ readonly dependencies: readonly string[] readonly group: string | null readonly id: string readonly session: string } /** Untrusted operation proposed by a client. Authorship is server-stamped. */ export type MosaicOperationProposal< Operation extends Json.Serializable = Json.Serializable, > = MosaicProposalMetadata & { readonly model: MosaicModelIdentifier readonly operation: Operation readonly protocolVersion: MosaicProtocolVersion readonly atom: MosaicAtomAddress } /** An operation whose actor has been authenticated by the server. */ export type MosaicOperationEnvelope< Operation extends Json.Serializable = Json.Serializable, > = MosaicOperationProposal & { readonly actor: string } export type MosaicOperationMetadata = MosaicProposalMetadata & { readonly actor: string } /** A durable checkpoint used for hydration and revision-gap recovery. */ export type MosaicSnapshotEnvelope< Snapshot extends Json.Serializable = Json.Serializable, > = { /** The subset of the joining client's pending IDs already accepted. */ readonly acceptedPendingOperationIds: readonly string[] readonly atom: MosaicAtomAddress /** The current causal frontier from which new local operations depend. */ readonly headOperationIds: readonly string[] readonly model: MosaicModelIdentifier readonly protocolVersion: MosaicProtocolVersion readonly revision: number /** Correlates the snapshot with the currently active client incarnation. */ readonly session: string readonly snapshot: Snapshot } /** Request to join or resynchronize one mutable-atom stream. */ export type MosaicJoinEnvelope = { readonly atom: MosaicAtomAddress readonly knownRevision: number | null readonly model: MosaicModelIdentifier readonly pendingOperationIds: readonly string[] readonly protocolVersion: MosaicProtocolVersion readonly session: string } /** An accepted operation paired with its server-assigned stream revision. */ export type MosaicAcceptedOperationEnvelope< Operation extends Json.Serializable = Json.Serializable, > = { readonly operation: MosaicOperationEnvelope readonly revision: number } export type MosaicRejectionCode = | `capacity-exceeded` | `incompatible-version` | `invalid-model-operation` | `invalid-payload` | `missing-dependency` | `operation-id-collision` | `atom-unavailable` | `stale-history` | `unauthorized` export type MosaicRecovery = | `discard-operation` | `none` | `resnapshot` | `retry` | `upgrade` /** A safe, structured rejection of an operation or atom request. */ export type MosaicRejectionEnvelope = { readonly code: MosaicRejectionCode readonly atom: MosaicAtomAddress readonly operationId: string | null readonly protocolVersion: MosaicProtocolVersion readonly reason: string readonly recovery: MosaicRecovery /** Correlates join- and operation-level failures with one client incarnation. */ readonly session: string } /** Untrusted ephemeral presence proposed by a bound client session. */ export type MosaicPresenceProposal< Presence extends Json.Serializable = Json.Serializable, > = { readonly atom: MosaicAtomAddress readonly presence: Presence readonly protocolVersion: MosaicProtocolVersion readonly session: string } /** Ephemeral presence stamped with the authenticated actor. */ export type MosaicPresenceEnvelope< Presence extends Json.Serializable = Json.Serializable, > = MosaicPresenceProposal & { readonly actor: string }