OpenMax
免费试用

订阅

订阅回调 ChatStoreListener、一次变化 ChatStoreChange 和区间变化 ChatStoreDataEvent。

订阅

@openmaxai/chat-store 根入口里关于订阅的导出。状态、在宿主里怎么接和全部导出的索引见概览。

ChatStoreListener

type ChatStoreListener = (change: ChatStoreChange) => void;

订阅回调,见 subscribe。

ChatStoreChange

interface ChatStoreChange {
  conversationId: string;
  events: readonly ChatStoreDataEvent[];
  unloaded?: {
    end: "older" | "newer";
    messageIds: readonly string[];
  };
}

一次 dispatch 给一个会话带来的变化,订阅者每次 dispatch 至多收到一次。

字段 说明
events 已加载区间的变化,见 ChatStoreDataEvent。整窗换入时只有一项 window。可以为空:只有「更早还有 / 更新还有」、游标、水位或 newerPending 变了,已加载的消息没变。
unloaded 内存窗口超过上限时卸掉的那一端和那些消息;那一端离视图最远。

ChatStoreDataEvent

type ChatStoreDataEvent =
  | {
      kind: "window";
    }
  | {
      kind: "before";
      messageIds: readonly string[];
    }
  | {
      kind: "after";
      messageIds: readonly string[];
    }
  | {
      kind: "replace";
      messageIds: readonly string[];
      removedIds: readonly string[];
    };

一个会话的已加载区间的一种变化,只有四种:

kind 说明
window 整窗换入。
before 接在前面;messageIds 是新接上的,按 seq 升序。
after 接在后面;messageIds 是新接上的,按 seq 升序。
replace 原地替换;messageIds 是内容换了的,removedIds 是被删掉的(行从原处消失,其余不动)。本端那条换成服务端那条(对账)也是它:messageIds 里是服务端那条的 id,它和本端那条的 client_msg_id 相同,那一行不重画。

本端的消息(发送中、失败、被拦)不在区间里,它们的变化也按这四种报:新发出的一条是 after,状态变了是 replace 的 messageIds,去掉是 replace 的 removedIds。但本端消息因为窗口没接到最新(hasNewer)而暂时不显示、或重新显示时,不另报事件:它显示与否以快照为准,按事件增量维护列表的话,收到任何变化后要按快照核对本端那几行。