@ai-matrx/messaging 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -766,6 +766,15 @@ interface MessagingStore {
766
766
  ingestMany(messages: readonly Message[]): void;
767
767
  removeMessage(conversationId: ConversationId, messageId: MessageId): void;
768
768
  markConversationRead(id: ConversationId): void;
769
+ /**
770
+ * Move a conversation's preview + sort position to reflect a message, with NO
771
+ * refetch. Sending a message must not cost a conversation-list RPC — at a
772
+ * busy org that is one full page read per keystroke-burst, and the row's new
773
+ * state is entirely derivable from the message we already hold.
774
+ */
775
+ applyLastMessage(message: Message, args?: {
776
+ incrementUnread?: boolean;
777
+ }): void;
769
778
  setUnreadCount(id: ConversationId, count: number): void;
770
779
  }
771
780
  declare function createMessagingStore(): MessagingStore;
package/dist/index.d.ts CHANGED
@@ -766,6 +766,15 @@ interface MessagingStore {
766
766
  ingestMany(messages: readonly Message[]): void;
767
767
  removeMessage(conversationId: ConversationId, messageId: MessageId): void;
768
768
  markConversationRead(id: ConversationId): void;
769
+ /**
770
+ * Move a conversation's preview + sort position to reflect a message, with NO
771
+ * refetch. Sending a message must not cost a conversation-list RPC — at a
772
+ * busy org that is one full page read per keystroke-burst, and the row's new
773
+ * state is entirely derivable from the message we already hold.
774
+ */
775
+ applyLastMessage(message: Message, args?: {
776
+ incrementUnread?: boolean;
777
+ }): void;
769
778
  setUnreadCount(id: ConversationId, count: number): void;
770
779
  }
771
780
  declare function createMessagingStore(): MessagingStore;
package/dist/index.js CHANGED
@@ -1046,6 +1046,29 @@ function createMessagingStore() {
1046
1046
  });
1047
1047
  emit();
1048
1048
  },
1049
+ applyLastMessage(message, args = {}) {
1050
+ const existing = conversations.find(
1051
+ (item) => item.conversation.id === message.conversationId
1052
+ );
1053
+ if (existing === void 0) return;
1054
+ if (message.deliveryState === "sending" || message.deletedAt !== null) return;
1055
+ if (existing.lastMessageAt !== null && message.createdAt < existing.lastMessageAt) return;
1056
+ const isActive = activeConversationId === message.conversationId;
1057
+ const shouldCount = args.incrementUnread === true && !isActive;
1058
+ conversations = normalizeConversations(
1059
+ conversations.map(
1060
+ (item) => item.conversation.id === message.conversationId ? {
1061
+ ...item,
1062
+ lastMessageContent: message.content,
1063
+ lastMessageSenderId: message.senderId,
1064
+ lastMessageAt: message.createdAt,
1065
+ sortAt: message.createdAt,
1066
+ unreadCount: shouldCount ? item.unreadCount + 1 : item.unreadCount
1067
+ } : item
1068
+ )
1069
+ );
1070
+ emit();
1071
+ },
1049
1072
  markConversationRead(id) {
1050
1073
  conversations = conversations.map(
1051
1074
  (item) => item.conversation.id === id ? { ...item, unreadCount: 0 } : item
@@ -1116,7 +1139,7 @@ function createMessagingEngine(options) {
1116
1139
  onSent: (entry, message) => {
1117
1140
  store.ingest(message);
1118
1141
  broadcastMessage(message);
1119
- void refreshConversationRow(message.conversationId);
1142
+ store.applyLastMessage(message);
1120
1143
  void entry;
1121
1144
  },
1122
1145
  onChange: (entries) => {
@@ -1142,15 +1165,6 @@ function createMessagingEngine(options) {
1142
1165
  function broadcastMessage(message) {
1143
1166
  openChannels.get(message.conversationId)?.send(MESSAGING_EVENTS.message, message);
1144
1167
  }
1145
- async function refreshConversationRow(id) {
1146
- try {
1147
- const page = await repository.listConversations({ limit: conversationPageSize });
1148
- const row = page.items.find((item) => item.conversation.id === id);
1149
- if (row !== void 0) store.upsertConversation(row);
1150
- } catch (error) {
1151
- reportError(error, "refreshConversationRow");
1152
- }
1153
- }
1154
1168
  async function reloadInbox() {
1155
1169
  const page = await repository.listConversations({ limit: conversationPageSize });
1156
1170
  conversationCursor = page.nextCursor;
@@ -1201,10 +1215,9 @@ function createMessagingEngine(options) {
1201
1215
  return;
1202
1216
  }
1203
1217
  store.ingest(message);
1204
- if (message.senderId !== identity.userId) {
1205
- options.onIncoming?.(message);
1206
- }
1207
- void refreshConversationRow(message.conversationId);
1218
+ const isMine = message.senderId === identity.userId;
1219
+ store.applyLastMessage(message, { incrementUnread: !isMine });
1220
+ if (!isMine) options.onIncoming?.(message);
1208
1221
  }
1209
1222
  },
1210
1223
  {