import type { AssignBotPayload, BulkTransferResult, Chat, ChatActionPayload, ChatActionResult, ChatList, ChatSession, CtwaReferral, DeleteMessagePayload, FetchMessagePayload, ListChatsPayload, Message, MessageDeleteResult, MessageStatusUpdate, RetryDownloadPayload, SendMessagePayload, UpdateChatPayload, UpdateChatResult } from '../types/inbox'; /** What a rejected event sends back. The HTTP error body, field for field. */ export interface InboxError { /** The event that failed. */ event: string; code: string; message: string; status: number; details?: unknown; requestId: string; } /** The acknowledgement a request-style event resolves with. */ export type InboxAck = { ok: true; data: T; } | { ok: false; error: InboxError; }; /** * Every event the client sends, with its payload and what it answers. * * The server replies two ways, so both generations of client work: through the * acknowledgement callback when one is passed, and by emitting the same event * name back, which is what the original inbox listens for. */ export interface InboxRequests { ping: { payload: unknown; result: unknown; }; 'contact.list': { payload: ListChatsPayload; result: ChatList; }; 'contact.fetch': { payload: { id: string; }; result: Chat; }; 'contact.update': { payload: UpdateChatPayload; result: UpdateChatResult; }; 'contact.done': { payload: { id: string; isEnded?: boolean; }; result: ChatActionResult; }; 'contact.bot_assign': { payload: AssignBotPayload; result: { success: boolean; id: string; }; }; 'chat.action': { payload: ChatActionPayload; result: ChatActionResult | BulkTransferResult; }; 'message.list': { payload: { id: string; limit?: number; last_message_id?: string; }; result: Message[]; }; 'message.fetch': { payload: FetchMessagePayload; result: Message; }; 'message.send': { payload: SendMessagePayload; result: Message; }; 'message.delete': { payload: DeleteMessagePayload; result: MessageDeleteResult; }; 'message.retry_download': { payload: RetryDownloadPayload; result: Message; }; 'ctwa.list': { payload: { id: string; limit?: number; }; result: CtwaReferral[]; }; 'chat_session.list': { payload: { id: string; channel?: string; }; result: ChatSession[]; }; } export type InboxRequestName = keyof InboxRequests; /** * Events the server pushes without being asked. * * `chat.action` and the `message.*` names appear here as well as in the request * map on purpose: an archive performed by another agent arrives under the same * name and shape as one you asked for, so a client needs only one handler per * kind of change rather than one per cause. */ export interface InboxPushes { /** * A message arrived, or one that was sent now exists as a stored row. * * The second case is how an optimistic bubble is replaced: `message.send` * answered with a queue id, and this carries the same message with the * provider's real `message_id`. */ 'message.received': Message; /** * The same row as `message.received`, in the shape it is stored in. * * Kept because the inbox this replaced listened for one name, `message.send`, * for both its own reply and the row that followed. New clients should use * `message.received`, which is mapped; this one carries `_id` and `user` as the * database holds them. */ 'message.send': Record; /** A delivery state moved: sent, delivered, read, failed. */ 'message.status': MessageStatusUpdate; /** A chat changed: unread count, assignment, automation, tags. */ 'chat.action': ChatActionResult; /** * A contact wrote in for the first time, so a chat row appeared. * * The stored contact document, not a mapped `Chat` -- it comes from the service * that created it. */ 'contact.received': Record; 'message.delete': MessageDeleteResult; 'message.retry_download': Message; 'contact.done': ChatActionResult; /** An event was refused. Carries the event name that failed. */ error: InboxError; } export type InboxPushName = keyof InboxPushes;