@adhdev/daemon-core 0.9.63 → 0.9.64

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.mjs CHANGED
@@ -12162,9 +12162,34 @@ function findLastMessageIndexBySignature(messages, signature) {
12162
12162
  }
12163
12163
  return -1;
12164
12164
  }
12165
+ function isReadChatConversationAnchorMessage(message) {
12166
+ if (!message) return false;
12167
+ const role = String(message.role || "").trim().toLowerCase();
12168
+ if (role !== "user" && role !== "assistant") return false;
12169
+ const kind = String(message.kind || "standard").trim().toLowerCase();
12170
+ return !kind || kind === "standard";
12171
+ }
12172
+ function buildVisibleReadChatTailMessages(messages, tailLimit) {
12173
+ const totalMessages = messages.length;
12174
+ if (tailLimit <= 0 || totalMessages <= tailLimit) return messages;
12175
+ const tailMessages = messages.slice(-tailLimit);
12176
+ if (tailMessages.some(isReadChatConversationAnchorMessage)) return tailMessages;
12177
+ const hiddenMessages = messages.slice(0, totalMessages - tailLimit);
12178
+ const anchors = [];
12179
+ const seenRoles = /* @__PURE__ */ new Set();
12180
+ for (let index = hiddenMessages.length - 1; index >= 0 && anchors.length < 2; index -= 1) {
12181
+ const message = hiddenMessages[index];
12182
+ if (!isReadChatConversationAnchorMessage(message)) continue;
12183
+ const role = String(message.role || "").trim().toLowerCase();
12184
+ if (seenRoles.has(role)) continue;
12185
+ seenRoles.add(role);
12186
+ anchors.unshift(message);
12187
+ }
12188
+ return anchors.length > 0 ? [...anchors, ...tailMessages] : tailMessages;
12189
+ }
12165
12190
  function buildBoundedTailSync(messages, cursor) {
12166
12191
  const totalMessages = messages.length;
12167
- const tailMessages = cursor.tailLimit > 0 && totalMessages > cursor.tailLimit ? messages.slice(-cursor.tailLimit) : messages;
12192
+ const tailMessages = buildVisibleReadChatTailMessages(messages, cursor.tailLimit);
12168
12193
  return {
12169
12194
  syncMode: "full",
12170
12195
  replaceFrom: 0,
@@ -12286,8 +12311,8 @@ function buildReadChatCommandResult(payload, args) {
12286
12311
  const messages = collapseReplayDuplicatesFromReadChat(normalizeReadChatMessages(validatedPayload));
12287
12312
  const cursor = normalizeReadChatCursor(args);
12288
12313
  if (!cursor.knownMessageCount && !cursor.lastMessageSignature && cursor.tailLimit > 0 && messages.length > cursor.tailLimit) {
12289
- const tailMessages = messages.slice(-cursor.tailLimit);
12290
- const lastMessageSignature = getChatMessageSignature(tailMessages[tailMessages.length - 1]);
12314
+ const tailMessages = buildVisibleReadChatTailMessages(messages, cursor.tailLimit);
12315
+ const lastMessageSignature = getChatMessageSignature(messages[messages.length - 1]);
12291
12316
  return {
12292
12317
  success: true,
12293
12318
  ...validatedPayload,