@syntrologie/adapt-chatbot 2.8.0-canary.549 → 2.8.0-canary.550

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/runtime.js CHANGED
@@ -2031,7 +2031,7 @@ async function pollOnce(cb, isActive) {
2031
2031
  const qs = new URLSearchParams({
2032
2032
  conversationId: id.conversationId,
2033
2033
  locationId: id.locationId,
2034
- lastMessageId: id.lastMessageId ?? "",
2034
+ lastMessageId: "",
2035
2035
  pageLimit: String(LC_PAGE_LIMIT)
2036
2036
  });
2037
2037
  try {
@@ -2051,14 +2051,15 @@ async function pollOnce(cb, isActive) {
2051
2051
  const j = await r.json();
2052
2052
  if (!isActive()) return;
2053
2053
  const messages = j.messages ?? [];
2054
- for (const m of messages) {
2054
+ const seenIdx = id.lastSeenMessageId ? messages.findIndex((m) => m.id === id.lastSeenMessageId) : -1;
2055
+ const fresh = seenIdx === -1 ? messages : messages.slice(0, seenIdx);
2056
+ for (const m of [...fresh].reverse()) {
2055
2057
  if (m.direction === "outbound" && typeof m.body === "string") cb(m.body);
2056
2058
  }
2057
- const lastMessage = messages[messages.length - 1];
2058
- const nextLastMessageId = typeof j.lastMessageId === "string" && j.lastMessageId ? j.lastMessageId : typeof lastMessage?.id === "string" && lastMessage.id ? lastMessage.id : void 0;
2059
- if (nextLastMessageId) {
2059
+ const newestId = messages[0]?.id;
2060
+ if (typeof newestId === "string" && newestId) {
2060
2061
  const current = load();
2061
- save({ ...id, ...current, lastMessageId: nextLastMessageId });
2062
+ save({ ...id, ...current, lastSeenMessageId: newestId });
2062
2063
  }
2063
2064
  pollWarned = false;
2064
2065
  } catch (err) {