Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.6 KB
Raw Blame
import type {
  DocumentChannel,
  GatewayMuxClient,
  GatewayMuxStatus,
  MuxDocCapacity,
  MuxDocServerMessage,
} from '@lobechat/agent-gateway-client';
import * as Y from 'yjs';

type Listener = (...args: any[]) => void;
type UserState = Record<string, unknown>;

export type PageCollabAccess = 'edit' | 'view';

export type PageCollabStatus = 'connecting' | 'error' | 'offline' | 'synced';

export type PageCollabResetReason = 'bootstrap_conflict' | 'epoch';

/** The room could not be joined: it answered with an error, or never synced in time. */
export interface PageCollabFailure {
  code: string;
  /** Time since the subscription was sent. */
  elapsedMs: number;
  message?: string;
}

/** How long a subscription may wait for the room's first sync before it counts as failed. */
export const PAGE_ROOM_SYNC_TIMEOUT_MS = 10_000;

// The room keeps awareness only in memory and loses it whenever it is evicted while idle,
// so every client re-sends its own state and drops peers that stopped renewing (y-protocols timing).
export const AWARENESS_RENEW_INTERVAL_MS = 15_000;
export const AWARENESS_TIMEOUT_MS = 30_000;

/** The server refused to store the room's page for the plan's per-page character limit. */
export interface PageCollabCharLimitRefusal {
  actual: number;
  limit: number;
  plan?: string;
}

/** `doc_error` code the room sends when the server refuses its page for its length. */
export const PAGE_ROOM_CHAR_LIMIT_CODE = 'char_limit_exceeded';

export type PageCollabBlockedReason = 'deleted' | 'too_large';

const BLOCKED_REASON: Record<string, PageCollabBlockedReason> = {
  doc_deleted: 'deleted',
  doc_too_large: 'too_large',
};

export interface PageCollabProviderEvents {
  onAccess?: (access: PageCollabAccess) => void;
  onBlocked?: (reason: PageCollabBlockedReason) => void;
  onBootstrap?: () => void;
  onCapacity?: (capacity: MuxDocCapacity) => void;
  /**
   * The server stored the room's page again after refusing it. A page already
   * over the limit is stored when it does not grow, so only the server knows.
   */
  onCharLimitCleared?: () => void;
  /**
   * The page grew past the plan's per-page limit: the room keeps the edits, but
   * the stored page stays at its last accepted version until it is shortened.
   */
  onCharLimitExceeded?: (refusal: PageCollabCharLimitRefusal) => void;
  onFailure?: (failure: PageCollabFailure) => void;
  /** This member changed the page, as opposed to the editor syncing or bootstrapping it. */
  onLocalEdit?: () => void;
  onPendingChange?: (pending: number) => void;
  onReset?: (reason: PageCollabResetReason) => void;
  onStatus?: (status: PageCollabStatus) => void;
}

const toBase64 = (bytes: Uint8Array) => {
  let binary = '';
  for (let i = 0; i < bytes.length; i += 0x8000) {
    binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
  }
  return btoa(binary);
};

const fromBase64 = (value: string) => Uint8Array.from(atob(value), (char) => char.charCodeAt(0));

class Emitter {
  private listeners = new Map<string, Set<Listener>>();

  emit(type: string, ...args: unknown[]) {
    this.listeners.get(type)?.forEach((cb) => cb(...args));
  }

  off(type: string, cb: Listener) {
    this.listeners.get(type)?.delete(cb);
  }

  on(type: string, cb: Listener) {
    if (!this.listeners.has(type)) this.listeners.set(type, new Set());
    this.listeners.get(type)!.add(cb);
  }
}

class PageCollabAwareness extends Emitter {
  private localState: UserState | null = null;
  private states = new Map<number, UserState>();
  private lastSeen = new Map<number, number>();

  constructor(
    private readonly clientID: number,
    private readonly publish: (state: UserState | null) => void,
  ) {
    super();
  }

  getLocalState() {
    return this.localState;
  }

  getStates() {
    const states = new Map(this.states);
    if (this.localState) states.set(this.clientID, this.localState);
    return states;
  }

  setLocalState(state: UserState | null) {
    this.localState = state;
    this.publish(state);
    this.emit('update');
  }

  setLocalStateField(field: string, value: unknown) {
    this.setLocalState({ ...this.localState, [field]: value });
  }

  setRemoteState(clientID: number, state: UserState | null) {
    if (clientID === this.clientID) return;
    if (state) {
      this.states.set(clientID, state);
      this.lastSeen.set(clientID, Date.now());
    } else {
      this.states.delete(clientID);
      this.lastSeen.delete(clientID);
    }
    this.emit('update');
  }

  clearRemote() {
    this.states.clear();
    this.lastSeen.clear();
    this.emit('update');
  }

  renewLocal() {
    if (this.localState) this.publish(this.localState);
  }

  expireRemote(timeoutMs: number) {
    const cutoff = Date.now() - timeoutMs;
    let expired = false;
    for (const [clientID, seenAt] of this.lastSeen) {
      if (seenAt > cutoff) continue;
      this.states.delete(clientID);
      this.lastSeen.delete(clientID);
      expired = true;
    }
    if (expired) this.emit('update');
  }
}

export class PageCollabProvider extends Emitter {
  readonly awareness: PageCollabAwareness;

  private channel: DocumentChannel | null = null;
  private epoch: string | undefined;
  private seq = 0;
  private readonly pending = new Set<number>();
  private waitingForBootstrap = false;
  private bootstrapPending = false;
  private bootstrapping = false;
  private conflicted = false;
  private synced = false;
  private acceptsEdits = false;
  // The hub refuses anything for the document until its subscribe settles, and a
  // refusal before the first sync fails the join.
  private subscribed = false;
  private failed = false;
  private subscribedAt = 0;
  private syncTimer: ReturnType<typeof setTimeout> | undefined;
  private awarenessTimer: ReturnType<typeof setInterval> | undefined;

  constructor(
    private readonly documentId: string,
    private readonly doc: Y.Doc,
    private readonly mux: GatewayMuxClient,
    private readonly events: PageCollabProviderEvents = {},
  ) {
    super();
    this.awareness = new PageCollabAwareness(doc.clientID, (state) => {
      if (!this.subscribed) return;
      this.channel?.send({ clientID: doc.clientID, documentId, state, type: 'doc_awareness' });
    });
    doc.on('update', this.handleLocalUpdate);
  }

  get pendingCount() {
    return this.pending.size;
  }

  // The binding may subscribe after the room already answered (the editor
  // mounts later than the provider connects), so late `sync` listeners replay it.
  on(type: string, cb: Listener) {
    super.on(type, cb);
    if (type === 'sync' && this.synced) queueMicrotask(() => cb(true));
  }

  connect() {
    if (this.channel) return;
    this.emit('status', { status: 'connecting' });
    this.events.onStatus?.('connecting');
    this.channel = this.mux.openDocument(this.documentId, {
      onMessage: this.handleMessage,
      onReady: this.subscribe,
      onStatus: this.handleStatus,
    });
    this.awarenessTimer = setInterval(() => {
      if (!this.subscribed) return;
      this.awareness.renewLocal();
      this.awareness.expireRemote(AWARENESS_TIMEOUT_MS);
    }, AWARENESS_RENEW_INTERVAL_MS);
  }

  disconnect() {
    if (!this.channel) return;
    clearInterval(this.awarenessTimer);
    this.awareness.setLocalState(null);
    this.channel.close();
    this.channel = null;
    this.awareness.clearRemote();
    this.emit('status', { status: 'disconnected' });
  }

  destroy() {
    clearTimeout(this.syncTimer);
    this.disconnect();
    this.doc.off('update', this.handleLocalUpdate);
  }

  /** Joins the room again after a failure. */
  retry() {
    this.failed = false;
    this.events.onStatus?.('connecting');
    if (this.channel) this.subscribe();
    else this.connect();
  }

  private fail(code: string, message?: string) {
    if (this.failed) return;
    this.failed = true;
    clearTimeout(this.syncTimer);
    this.events.onStatus?.('error');
    this.events.onFailure?.({ code, elapsedMs: Date.now() - this.subscribedAt, message });
  }

  private subscribe = () => {
    this.synced = false;
    this.subscribed = false;
    this.subscribedAt = Date.now();
    clearTimeout(this.syncTimer);
    this.syncTimer = setTimeout(() => {
      if (!this.synced) this.fail('sync_timeout');
    }, PAGE_ROOM_SYNC_TIMEOUT_MS);
    this.channel?.send({
      documentId: this.documentId,
      ...(this.epoch ? { epoch: this.epoch } : {}),
      stateVector: toBase64(Y.encodeStateVector(this.doc)),
      type: 'doc_subscribe',
    });
  };

  private handleStatus = (status: GatewayMuxStatus) => {
    if (status !== 'connected') {
      this.synced = false;
      this.subscribed = false;
      this.emit('status', { status: status === 'connecting' ? 'reconnecting' : status });
      this.events.onStatus?.('offline');
    }
  };

  private handleLocalUpdate = (update: Uint8Array, origin: unknown) => {
    if (origin === this) return;
    if (this.acceptsEdits && !this.bootstrapping) this.events.onLocalEdit?.();
    const seq = ++this.seq;
    this.pending.add(seq);
    this.events.onPendingChange?.(this.pending.size);
    this.channel?.send({
      ...(this.bootstrapping ? { bootstrap: true as const } : {}),
      documentId: this.documentId,
      seq,
      type: 'doc_update',
      update: toBase64(update),
    });
  };

  private markSynced() {
    if (this.synced) return;
    clearTimeout(this.syncTimer);
    this.failed = false;
    this.synced = true;
    this.waitingForBootstrap = false;
    this.emit('status', { status: 'connected' });
    this.emit('sync', true);
    this.acceptsEdits = true;
    this.events.onStatus?.('synced');
  }

  // Someone else (e.g. the server agent) may fill the room while we hold the
  // claim; their content arriving first means our bootstrap would duplicate it.
  private scheduleBootstrap() {
    this.bootstrapPending = true;
    queueMicrotask(() => {
      if (!this.bootstrapPending) return;
      this.bootstrapPending = false;
      this.events.onBootstrap?.();
    });
  }

  private resendPending() {
    if (this.pending.size === 0) return;
    this.pending.clear();
    const update = Y.encodeStateAsUpdate(this.doc);
    this.handleLocalUpdate(update, null);
  }

  private reportCapacity(capacity: MuxDocCapacity) {
    this.events.onCapacity?.(capacity);
    if (capacity === 'full') this.events.onBlocked?.('too_large');
  }

  private handleMessage = (message: MuxDocServerMessage) => {
    switch (message.type) {
      case 'doc_synced': {
        if (message.reset && this.epoch && this.epoch !== message.epoch) {
          this.events.onReset?.('epoch');
          return;
        }
        this.subscribed = true;
        this.bootstrapping = false;
        this.epoch = message.epoch;
        this.events.onAccess?.(message.access);
        Y.applyUpdate(this.doc, fromBase64(message.update), this);
        for (const entry of message.awareness ?? []) {
          this.awareness.setRemoteState(entry.clientID, entry.state);
        }
        if (this.awareness.getLocalState()) {
          this.awareness.setLocalState(this.awareness.getLocalState());
        }
        this.resendPending();
        if (message.capacity) this.reportCapacity(message.capacity);
        if (message.bootstrap || message.bootstrapped) {
          // The binding seeds an empty root as soon as it hears `sync`, before
          // the bootstrap below runs; that write is part of our bootstrap too.
          this.bootstrapping = message.bootstrap;
          this.markSynced();
          if (message.bootstrap) this.scheduleBootstrap();
        } else {
          this.waitingForBootstrap = true;
        }
        return;
      }

      case 'doc_update': {
        this.bootstrapPending = false;
        Y.applyUpdate(this.doc, fromBase64(message.update), this);
        if (this.waitingForBootstrap) this.markSynced();
        return;
      }

      case 'doc_ack': {
        this.bootstrapping = false;
        if (this.pending.delete(message.seq)) this.events.onPendingChange?.(this.pending.size);
        return;
      }

      case 'doc_awareness': {
        this.awareness.setRemoteState(message.clientID, message.state);
        return;
      }

      case 'doc_char_limit_cleared': {
        this.events.onCharLimitCleared?.();
        return;
      }

      case 'doc_capacity': {
        this.reportCapacity(message.capacity);
        return;
      }

      case 'doc_error': {
        // Our doc now holds a second copy of the page; the room refuses it until we rejoin with a fresh one.
        if (message.code === 'bootstrap_conflict') {
          if (!this.conflicted) {
            this.conflicted = true;
            this.events.onReset?.('bootstrap_conflict');
          }
          return;
        }
        // Not a join failure: the room still syncs, only storing the page is refused.
        if (message.code === PAGE_ROOM_CHAR_LIMIT_CODE) {
          if (typeof message.limit === 'number') {
            this.events.onCharLimitExceeded?.({
              actual: message.actual ?? 0,
              limit: message.limit,
              plan: message.plan,
            });
          } else {
            console.warn('[PageCollabProvider] char limit refusal without a limit', message);
          }
          return;
        }
        // Not a sync failure: retrying cannot help, the page stays read-only.
        const blocked = BLOCKED_REASON[message.code];
        if (blocked) {
          this.events.onBlocked?.(blocked);
          return;
        }
        // A view-only member is still in the room; only their writes are refused.
        if (message.code === 'read_only' || message.code === 'forbidden') {
          this.events.onAccess?.('view');
          return;
        }
        console.warn('[PageCollabProvider]', message.code, message.message);
        if (!this.synced) this.fail(message.code, message.message);
      }
    }
  };
}