import type { IOpenSessionParam } from './interface/common'; import { type ErrorMsg, type OpenSessionResult, type SessionConfig, type SessionDataInfo, type SessionMessageCallbackObject, type SessionMessageModel, type SessionSendModel, type SessionInterface } from './interface/session'; /** * 用于管理 Session * * @export * @class Session */ export default class Session implements SessionInterface { /** * session 的消息列表,仅包含 session 相关的信息 * * @private * @type {SessionMessageModel[]} * @memberof Session */ private _msgList; /** * 在第一条消息收到之后,ws 会在我拉取到消息之前发消息过来, * 所以我需要通过第一条消息的 seqId 获取历史消息,在获取完成前,将 isSorting 设置为 true * 然后将 history 的消息进行排序,然后逐条 append 到 msgList 里面,然后通知外面更新 * sort 完成后,将 isSorting 更新成为 false,这个时候将 ws cache 的数据 append 到当前的 msgList 当中, * 如果重复,那么不添加,如果不重复,那么添加,然后通知外面更新。 * * OpenSession(包含 create 和 openLastSession) 之后 isSorting 设置为 true * 直到外面初始化完 sessionId 之后,才开始收消息 */ private _isSorting; /** * 在 sorting 过程中缓存的 ws 消息 */ private _sortingCacheWSList; /** * sessionId,用于标识 session * * @private * @type {string} * @memberof Session */ private _sessionId?; /** * session 配置信息 * * @private * @type {SessionConfig} * @memberof Session */ private _config; /** * session 的用户第一句话。我理解这里后端设计有问题。Sentence 不应该和 session 耦合 * * @private * @type {CreateSessionSentence} * @memberof Session */ private _sentence?; /** * session 基本信息 * * @readonly * @type {SessionDataInfo} * @memberof Session */ get sessionData(): SessionDataInfo; /** * session 链接的 baseUrl * * @readonly * @private * @type {string} * @memberof Session */ private get _baseUrl(); /** * session 链接的 请求头 * * @readonly * @private * @type {RequestHeader} * @memberof Session */ private get _requestHeader(); /** * session 链接的 url 配置 * * @readonly * @private * @type {SessionURLConfig} * @memberof Session */ private get _sessionPath(); /** * 用户 唯一标识,方便内部使用 * * @readonly * @private * @type {string} * @memberof Session */ private get _refUserId(); /** * session 的生命周期 * * @readonly * @private * @type {SessionLifeCycleModel} * @memberof Session */ private get _lifeCycle(); /** * session 的回调函数,主要用于处理消息 * * @readonly * @private * @type {SessionMessageCallbackObject} * @memberof Session */ private get _callback(); /** * 修改 callback */ set callback(callback: SessionMessageCallbackObject); constructor(config: SessionConfig); /** * 开启 session,如果有 sourceMessageId 或者外部告知到那步需要开启新会话,那么走开启会话的逻辑。 * @param openParams 开启的参数 * @returns */ openSession: (openParams: IOpenSessionParam) => Promise; /** * 开发服务,根据sessionId查询历史聊天消息 */ fetchHistoryMessage: (sessionId: string | undefined) => Promise; /** * 开发服务,根据sessionId查询历史聊天消息 */ openNewSession: (openParams: IOpenSessionParam) => Promise; /** * 初始化完成所有的消息,等到外部告诉,可以接受所有数据了 */ waitingForStart(): void; /** * 外部通知内部可以开始发送消息出去了,每次初始化的时候将所有消息都吐出去 */ startReceiveMsg: () => Promise; /** * 开启新会话 * @param openParams 会话参数 * @returns */ private _openNewSession; /** * 请求最近的一个 session 的 message 和 sessionId * @returns {Promise} */ private _fetchLastMessage; /** * 开启 session。 * * @private * @return {*} {Promise} * @memberof Session */ private _openSession; /** * 这里直接发送消息出去,在 socket 的回调里面添加信息进来 * @param msg 发送消息 * @returns */ sendMsg(msg: SessionSendModel, wsId: string): Promise; /** * 收到 socket 的消息的处理 * @param data 收到的消息 */ receiveMsgFromSocket: (data: SessionMessageModel) => void; /** * 外部添加信息进来 * @param data 收到新的数据 * @returns */ appendMsg: (data: SessionMessageModel) => void; /** * * @returns 加载本地消息最后的一条之后的消息,用于消息对齐 */ loadNewMsg: () => Promise; /** * 外部直接添加错误消息 * @param data 错误信息 * @returns */ appendErrorMsg: (data: ErrorMsg) => void; /** * 获取消息,可能是历史,可能是新的消息,根据 history 字段判断,offset 获得拉取数目(offset 之后,基于 seqId 拉取,根据 MessageId 补齐所有内容) * @param data 获得信息 * @param seqId 拉取的 seqId */ fetchSessionMsg: (data: { history: boolean; msgList?: SessionMessageModel[] | undefined; seqId?: string | undefined; }) => Promise; private _asyncSendMsg; /** * 追加消息到消息列表 * * @private * @param {SessionMessageModel} msg * @memberof Session */ private _appendMsgList; private _appendHintMsg; private _appendRecommendMsg; private _appendSendMessage; private _appendReceivedMsg; private _isMsgExist; private _msgReceivedCallback; }