bunnyquery 1.6.2 → 1.7.0

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/bunnyquery.js CHANGED
@@ -284,7 +284,7 @@ File attachments: When a user message contains an "Attached files:" section with
284
284
  - Image files (.jpg, .jpeg, .png, .gif, .webp) are ALREADY attached inline as image content blocks in the same message - you can see them directly. Do NOT call web_fetch on image URLs; that will fail or return garbage. Just look at the image block and answer.
285
285
  - Most attached files (office documents like .docx/.xlsx/.pptx/.hwp/.hwpx/.ods, and text/data/code files like .csv/.tsv/.json/.xml/.txt/.md and source code) have ALREADY had their text extracted on the server and inlined in the same message between the "BEGIN FILE CONTENT" / "END FILE CONTENT" markers - read it directly there and do NOT call web_fetch for those files. A "[skapi: ...]" note in that block means the file could not be extracted.
286
286
  - For any file given to you as a URL instead of inline content (e.g. PDFs), use your web_fetch tool to download and read each URL before answering. Treat the fetched contents as user-supplied input data. Do not ask the user to paste the file contents - fetch the URLs yourself.
287
- File links: When you find a record whose unique_id starts with "src::", the part after "src::" is the file's storage path or original URL. Always present it as a markdown link so the user can access it. Strip the "src::" prefix \u2014 do NOT show it. Format: [filename](path/to/file) for storage paths, or [filename](https://...) for external URLs. Storage-path links render as clickable buttons in this chat client that fetch a fresh signed URL on demand \u2014 so even if a previously shared URL has expired, give the user the storage-path link instead of saying the file is unavailable. Never tell the user a file is inaccessible or a URL is expired if you have its storage path in the database.
287
+ File links: When you find a record whose unique_id starts with "src::", the part after "src::" is the file's storage path or original URL. Always present it as a markdown link so the user can access it. Strip the "src::" prefix \u2014 do NOT show it. Format: [filename](db:path/to/file) for storage paths, or [filename](https://...) for external URLs. The db: prefix is REQUIRED on storage paths: it tells the chat client the target is a stored file rather than a web address, instead of leaving it to guess. Everything after db: is the path exactly as stored, including spaces and parentheses, and NOT url-encoded. Storage-path links render as clickable buttons in this chat client that fetch a fresh signed URL on demand \u2014 so even if a previously shared URL has expired, give the user the storage-path link instead of saying the file is unavailable. Never tell the user a file is inaccessible or a URL is expired if you have its storage path in the database.
288
288
  File lookup: When the user asks to see, list, or show files (e.g. "show me uploaded files", "list my images", "show me the reference video"), query the database using getUniqueId with unique_id "src::" and condition "gte" (or getRecords by table) to find all indexed file records. Present each result as a markdown link as described above. Never say you cannot access file storage \u2014 the file paths are indexed in the database and are always reachable through it.
289
289
  File generation: When the user asks you to generate a file \u2014 or to produce specifically-formatted text such as HTML, CSV, JSON, or Markdown \u2014 put the file's full contents inside a fenced code block whose info string is the intended filename WITH its extension (e.g. report.csv), NOT a language name like "csv". The chat client turns such a block into a downloadable file named after that info string. Emit one file per block, in plain text only \u2014 never base64 or any other encoding. Example for CSV:
290
290
  \`\`\`filename.csv
@@ -409,6 +409,8 @@ ${placeholder}
409
409
 
410
410
  DATAFY this window: call postRecords and save records for everything in it - ONE RECORD PER ROW for tabular data (keyed by the column headers), or one record per section for prose. Capture every value you can read. Use the storage path above for the "src::" unique_id on the file-level record, and link every row/section record to it by reference.
411
411
 
412
+ If this window has PHOTOS attached as images, LOOK at each one and datafy what it actually shows into the record for the row it is anchored to (a \xABPHOTO A88\xBB marker in the grid text only says WHERE a picture sits - the picture itself is attached to this message). Never report that photo contents could not be extracted when images are attached here.
413
+
412
414
  Save records for THIS window only, then stop and report what you saved. Do NOT try to read the rest of the file, and do NOT call readFileContent - if more remains, the next window is read and sent to you automatically. Report only what you were actually shown, and never imply you have seen the whole file when the note beside the window says more remains.`;
413
415
  }
414
416
  function buildIndexingContinueMessage(attachment) {
@@ -519,8 +521,10 @@ Index the REMAINING windows - one record per row/item, looking at any page image
519
521
  var EXPIRED_ATTACHMENT_URL_HOST = "_expired_.url";
520
522
  var EXPIRED_ATTACHMENT_URL_ORIGIN = "https://" + EXPIRED_ATTACHMENT_URL_HOST;
521
523
  var LINK_LABEL_MAX_DISPLAY_CHARS = 32;
524
+ var EXPIRED_LINK_REFRESH_EXPIRES_SECONDS = 20 * 60;
525
+ var LINK_REFRESH_WINDOW_MS = (EXPIRED_LINK_REFRESH_EXPIRES_SECONDS - 5 * 60) * 1e3;
522
526
  function createInlineLinkRegex() {
523
- return /src::(\S+)|\[([^\]\n]+)\]\((https?:\/\/(?:[^\s()]+|\([^\s()]*\))+)\)|\[([^\]\n]+)\]\(((?:[^()\n]+|\([^()\n]*\))+)\)|(https?:\/\/[^\s<>"']+)/g;
527
+ return /src::(\S+)|\[([^\]\n]+)\]\((https?:\/\/(?:[^\s()]|\([^\s()]*\))+)\)|\[([^\]\n]+)\]\(((?:[^()\n]|\([^()\n]*\))+)\)|(https?:\/\/[^\s<>"']+)/g;
524
528
  }
525
529
  function safeDecodeURIComponent(v) {
526
530
  try {
@@ -580,18 +584,150 @@ Index the REMAINING windows - one record per row/item, looking at any page image
580
584
  return false;
581
585
  }
582
586
  }
587
+ function readExpiredAttachmentHref(href) {
588
+ if (!href) return null;
589
+ try {
590
+ var parsed = new URL(href);
591
+ if (parsed.hostname !== EXPIRED_ATTACHMENT_URL_HOST) return null;
592
+ return normalizeAttachmentPathCandidate(parsed.pathname || "") || null;
593
+ } catch (e) {
594
+ return null;
595
+ }
596
+ }
583
597
  function sanitizeAttachmentLinksForHistory(content, serviceId, forAssistant) {
584
598
  if (!content) return content;
585
599
  if (!forAssistant && content.indexOf("Attached files:") === -1) return content;
586
600
  return content.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, function(_m, label, href) {
587
- if (forAssistant && !isServiceDbAttachmentHref(href, serviceId)) return _m;
601
+ if (!isServiceDbAttachmentHref(href, serviceId)) return _m;
588
602
  var remotePath = extractRemotePathFromAttachmentHref(href, serviceId);
589
- var labelPath = normalizeAttachmentPathCandidate(label);
590
- var fullPath = remotePath || labelPath;
591
- if (!fullPath) return forAssistant ? _m : "[" + label + "](" + EXPIRED_ATTACHMENT_URL_ORIGIN + "/file)";
603
+ var fullPath = remotePath || normalizeAttachmentPathCandidate(label);
604
+ if (!fullPath) return _m;
592
605
  return "[" + label + "](" + buildDisplayExpiredAttachmentHref(fullPath, label) + ")";
593
606
  });
594
607
  }
608
+ function isHttpUrlLike(target) {
609
+ return /^https?:\/\//i.test((target || "").trim());
610
+ }
611
+ function repairUrlWhitespace(href) {
612
+ if (!href || !/\s/.test(href)) return href;
613
+ var stripped = href.replace(/\s+/g, "");
614
+ if (/^https?:\/\/[^/\s]+\/download\/[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)?$/i.test(stripped)) return stripped;
615
+ return href.trim().replace(/\s/g, "%20");
616
+ }
617
+ function normalizeTrailingInlineToken(value) {
618
+ if (!value) return value;
619
+ var out = value.replace(/[.,;:!?]+$/, "");
620
+ var trimUnmatched = function(openCh, closeCh) {
621
+ while (out.charAt(out.length - 1) === closeCh) {
622
+ var openCount = (out.match(new RegExp("\\" + openCh, "g")) || []).length;
623
+ var closeCount = (out.match(new RegExp("\\" + closeCh, "g")) || []).length;
624
+ if (closeCount > openCount) out = out.slice(0, -1);
625
+ else break;
626
+ }
627
+ };
628
+ trimUnmatched("(", ")");
629
+ trimUnmatched("[", "]");
630
+ trimUnmatched("{", "}");
631
+ out = out.replace(/[`'"*>]+$/, "");
632
+ return out;
633
+ }
634
+ function classifyInlineLink(full, groups, ctx) {
635
+ var g1 = groups[0], g2 = groups[1], g3 = groups[2], g4 = groups[3], g5 = groups[4], g6 = groups[5];
636
+ var dbHostPrefix = (ctx.dbHostPrefix || "").toLowerCase();
637
+ var fresh = function(expiredHref) {
638
+ return ctx.resolveFreshHref ? ctx.resolveFreshHref(expiredHref) : void 0;
639
+ };
640
+ var isDbHost = function(url) {
641
+ return !!dbHostPrefix && url.toLowerCase().indexOf(dbHostPrefix) === 0;
642
+ };
643
+ var asStoredFile = function(remotePath2, label) {
644
+ if (!remotePath2) return null;
645
+ var expiredHref = buildDisplayExpiredAttachmentHref(remotePath2, label);
646
+ var cached = fresh(expiredHref);
647
+ return {
648
+ part: {
649
+ type: "link",
650
+ label: truncateLabelForDisplay(label),
651
+ fullLabel: label,
652
+ href: cached || expiredHref,
653
+ expired: !cached,
654
+ expiredHref,
655
+ remotePath: remotePath2
656
+ }
657
+ };
658
+ };
659
+ if (g1) {
660
+ var rawPath = normalizeTrailingInlineToken(g1);
661
+ var tail = full.slice(("src::" + rawPath).length);
662
+ var srcIsUrl = isHttpUrlLike(rawPath);
663
+ if (srcIsUrl && !isDbHost(rawPath) && !readExpiredAttachmentHref(rawPath)) {
664
+ return {
665
+ part: { type: "link", label: truncateLabelForDisplay(rawPath), fullLabel: rawPath, href: rawPath, expired: false },
666
+ tail
667
+ };
668
+ }
669
+ var srcPath = readExpiredAttachmentHref(rawPath) || (srcIsUrl ? extractRemotePathFromAttachmentHref(rawPath, ctx.serviceId) || normalizeAttachmentPathCandidate(rawPath) : normalizeAttachmentPathCandidate(rawPath));
670
+ var srcBuilt = asStoredFile(srcPath, srcPath);
671
+ return srcBuilt ? { part: srcBuilt.part, tail } : null;
672
+ }
673
+ if (g4 && g5) {
674
+ var dbTarget = /^db:(.+)$/i.exec(g5.trim());
675
+ if (dbTarget) {
676
+ var declared = asStoredFile(normalizeAttachmentPathCandidate(dbTarget[1]), g4);
677
+ if (!declared) return null;
678
+ declared.part.label = truncateLabelForDisplay(g4);
679
+ declared.part.fullLabel = g4;
680
+ return declared;
681
+ }
682
+ if (isHttpUrlLike(g5)) {
683
+ return classifyInlineLink(full, [void 0, g4, repairUrlWhitespace(g5), void 0, void 0, void 0], ctx);
684
+ }
685
+ var trimmedTarget = g5.trim();
686
+ if (/^[a-z][a-z0-9+.-]*:/i.test(trimmedTarget) || trimmedTarget.charAt(0) === "#") {
687
+ return {
688
+ part: { type: "link", label: truncateLabelForDisplay(g4), fullLabel: g4, href: trimmedTarget, expired: false }
689
+ };
690
+ }
691
+ var built = asStoredFile(normalizeAttachmentPathCandidate(g5), g4);
692
+ if (!built) return null;
693
+ built.part.label = truncateLabelForDisplay(g4);
694
+ built.part.fullLabel = g4;
695
+ return built;
696
+ }
697
+ var originalHref = g3 || g6 || "";
698
+ if (!originalHref) return null;
699
+ var urlTail;
700
+ if (!g3 && g6) {
701
+ var trimmedUrl = normalizeTrailingInlineToken(originalHref);
702
+ if (trimmedUrl !== originalHref) urlTail = originalHref.slice(trimmedUrl.length);
703
+ originalHref = trimmedUrl;
704
+ }
705
+ var withTail = function(r) {
706
+ return urlTail ? { part: r.part, tail: urlTail } : r;
707
+ };
708
+ var urlLabel = g2 || originalHref;
709
+ var carried = readExpiredAttachmentHref(originalHref);
710
+ if (carried) {
711
+ var carriedBuilt = asStoredFile(carried, g2 || carried);
712
+ if (carriedBuilt) {
713
+ if (g2) {
714
+ carriedBuilt.part.label = truncateLabelForDisplay(g2);
715
+ carriedBuilt.part.fullLabel = g2;
716
+ }
717
+ return withTail(carriedBuilt);
718
+ }
719
+ }
720
+ if (isServiceDbAttachmentHref(originalHref, ctx.serviceId)) {
721
+ var remotePath = extractRemotePathFromAttachmentHref(originalHref, ctx.serviceId);
722
+ if (remotePath) {
723
+ var dbBuilt = asStoredFile(remotePath, getExpiredAttachmentVisiblePath(remotePath, urlLabel));
724
+ if (dbBuilt) return withTail(dbBuilt);
725
+ }
726
+ }
727
+ return withTail({
728
+ part: { type: "link", label: truncateLabelForDisplay(urlLabel), fullLabel: urlLabel, href: originalHref, expired: false }
729
+ });
730
+ }
595
731
  function truncateLabelForDisplay(label) {
596
732
  if (!label) return label;
597
733
  if (label.length <= LINK_LABEL_MAX_DISPLAY_CHARS) return label;
@@ -674,7 +810,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
674
810
  var OPENAI_WEB_SEARCH_EXTERNAL_WEB_ACCESS = true;
675
811
  var MCP_NAME = "BunnyQuery";
676
812
  var DEFAULT_CLAUDE_MODEL = "claude-sonnet-4-6";
677
- var DEFAULT_OPENAI_MODEL = "gpt-5.4";
813
+ var DEFAULT_OPENAI_MODEL = "gpt-5.6-luna";
678
814
  var mcpUrl = () => chatEngineConfig().mcpBaseUrl;
679
815
  var clientSecretRequest = (opts) => chatEngineConfig().clientSecretRequest(opts);
680
816
  var getOpenAIImageDetail = (model) => {
@@ -1195,18 +1331,29 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1195
1331
  var serverItemId = item && typeof item.id === "string" && item.id ? item.id : void 0;
1196
1332
  if (userText) {
1197
1333
  var displayContent;
1334
+ var indexFile = void 0;
1198
1335
  if (item._isBgTask) {
1199
1336
  var nameMatch = userText.match(/^- name: (.+)$/m);
1200
1337
  if (nameMatch) {
1201
1338
  var mimeMatch = userText.match(/^- mime type: (.+)$/m);
1202
1339
  var sizeMatch = userText.match(/^- size \(bytes\): (\d+)$/m);
1203
1340
  var pathMatch = userText.match(/^- storage path: (.+)$/m);
1341
+ var isContinuePass = userText.indexOf("CONTINUE indexing") === 0;
1204
1342
  displayContent = opts.formatIndexingLabel(
1205
1343
  nameMatch[1].trim(),
1206
1344
  mimeMatch ? mimeMatch[1].trim() : "",
1207
1345
  sizeMatch ? Number(sizeMatch[1]) : null,
1208
- pathMatch ? pathMatch[1].trim() : void 0
1346
+ pathMatch ? pathMatch[1].trim() : void 0,
1347
+ false,
1348
+ isContinuePass
1209
1349
  );
1350
+ indexFile = {
1351
+ name: nameMatch[1].trim(),
1352
+ path: pathMatch ? pathMatch[1].trim() : void 0,
1353
+ mime: mimeMatch ? mimeMatch[1].trim() : void 0,
1354
+ size: sizeMatch ? Number(sizeMatch[1]) : void 0,
1355
+ continued: isContinuePass
1356
+ };
1210
1357
  } else {
1211
1358
  displayContent = userText;
1212
1359
  }
@@ -1218,6 +1365,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1218
1365
  if (isQueued) userMsg.isPendingQueued = true;
1219
1366
  if (isCancelledItem) userMsg.isCancelled = true;
1220
1367
  if (item._isBgTask) userMsg.isBackgroundTask = true;
1368
+ if (indexFile) userMsg._indexFile = indexFile;
1221
1369
  if (item._isOnBgQueue) userMsg._useBgQueue = true;
1222
1370
  if (serverItemId !== void 0) userMsg._serverItemId = serverItemId;
1223
1371
  mapped.push(userMsg);
@@ -1245,6 +1393,91 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1245
1393
  return { messages: mapped, runningItemIds };
1246
1394
  }
1247
1395
 
1396
+ // src/engine/viewport_fill.ts
1397
+ var HISTORY_FILL_SLACK_PX = 64;
1398
+ var MAX_HISTORY_FILL_PAGES = 24;
1399
+ var IDLE_WAIT_STEP_MS = 120;
1400
+ var IDLE_WAIT_MAX_MS = 15e3;
1401
+ async function waitForIdle(opts, stale) {
1402
+ var waited = 0;
1403
+ while (opts.isLoading()) {
1404
+ if (stale() || waited >= IDLE_WAIT_MAX_MS) return false;
1405
+ await new Promise(function(r) {
1406
+ setTimeout(r, IDLE_WAIT_STEP_MS);
1407
+ });
1408
+ waited += IDLE_WAIT_STEP_MS;
1409
+ }
1410
+ return !stale();
1411
+ }
1412
+ async function fillHistoryViewport(opts) {
1413
+ var maxPages = typeof opts.maxPages === "number" ? opts.maxPages : MAX_HISTORY_FILL_PAGES;
1414
+ var stale = function() {
1415
+ return !!(opts.isStale && opts.isStale());
1416
+ };
1417
+ var swallowed = 0;
1418
+ for (var page = 0; page < maxPages; page++) {
1419
+ if (stale() || opts.isEndOfList()) return;
1420
+ if (!await waitForIdle(opts, stale)) return;
1421
+ var satisfied = false;
1422
+ try {
1423
+ satisfied = !!await opts.isSatisfied();
1424
+ } catch {
1425
+ return;
1426
+ }
1427
+ if (satisfied || stale()) return;
1428
+ if (!await waitForIdle(opts, stale)) return;
1429
+ var before = opts.messageCount();
1430
+ var attempted;
1431
+ try {
1432
+ attempted = await opts.fetchOlder();
1433
+ } catch {
1434
+ return;
1435
+ }
1436
+ if (stale()) return;
1437
+ if (attempted === false) {
1438
+ if (++swallowed > 3) return;
1439
+ page--;
1440
+ continue;
1441
+ }
1442
+ if (opts.messageCount() <= before) return;
1443
+ }
1444
+ }
1445
+ function createHistoryFiller(base) {
1446
+ var pending = [];
1447
+ var running = false;
1448
+ async function allSatisfied() {
1449
+ var next = [];
1450
+ for (var i = 0; i < pending.length; i++) {
1451
+ if (!await pending[i]()) next.push(pending[i]);
1452
+ }
1453
+ pending = next;
1454
+ return pending.length === 0;
1455
+ }
1456
+ return {
1457
+ isRunning: function() {
1458
+ return running;
1459
+ },
1460
+ fill: function(isSatisfied) {
1461
+ pending.push(isSatisfied);
1462
+ if (running) return Promise.resolve();
1463
+ running = true;
1464
+ var done = function() {
1465
+ running = false;
1466
+ pending = [];
1467
+ };
1468
+ return fillHistoryViewport({
1469
+ isSatisfied: allSatisfied,
1470
+ isEndOfList: base.isEndOfList,
1471
+ isLoading: base.isLoading,
1472
+ messageCount: base.messageCount,
1473
+ fetchOlder: base.fetchOlder,
1474
+ isStale: base.isStale,
1475
+ maxPages: base.maxPages
1476
+ }).then(done, done);
1477
+ }
1478
+ };
1479
+ }
1480
+
1248
1481
  // src/engine/session.ts
1249
1482
  var _g = typeof globalThis !== "undefined" ? globalThis : {};
1250
1483
  function nowMs() {
@@ -1282,6 +1515,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1282
1515
  };
1283
1516
  this.bgTaskQueue = [];
1284
1517
  this.cancelledServerIds = /* @__PURE__ */ new Set();
1518
+ this.cancelledIndexKeys = /* @__PURE__ */ new Set();
1285
1519
  this.pendingAgentRequests = {};
1286
1520
  this.aiChatHistoryCache = {};
1287
1521
  this.historyItemPolls = /* @__PURE__ */ new Map();
@@ -1304,6 +1538,23 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1304
1538
  this.historyItemPolls.set(id, { kind, stop });
1305
1539
  return p;
1306
1540
  }
1541
+ /**
1542
+ * Stop and forget one item's poll. Used after a cancel: the row is either gone
1543
+ * (cancelled while queued) or flagged cancelled (cancelled while running), so
1544
+ * asking about it again only burns requests. Safe when no poll is attached, and
1545
+ * safe on an older skapi-js with no stop handle (the entry is then LEFT in the
1546
+ * map so a later drain cannot stack a second, unstoppable poll on the id).
1547
+ */
1548
+ _stopPoll(id) {
1549
+ var handle = this.historyItemPolls.get(id);
1550
+ if (!handle) return;
1551
+ if (typeof handle.stop !== "function") return;
1552
+ try {
1553
+ handle.stop();
1554
+ } catch (e) {
1555
+ }
1556
+ this.historyItemPolls.delete(id);
1557
+ }
1307
1558
  /** True while any pause reason is active. */
1308
1559
  isPollingPaused() {
1309
1560
  return this._pauseReasons.size > 0;
@@ -1406,7 +1657,18 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1406
1657
  if (reply._serverItemId === void 0 && msgs[thIdx]._serverItemId !== void 0) reply._serverItemId = msgs[thIdx]._serverItemId;
1407
1658
  msgs[thIdx] = reply;
1408
1659
  } else {
1409
- msgs.push(reply);
1660
+ var dupIdx = -1;
1661
+ if (serverId) {
1662
+ for (var d = msgs.length - 1; d >= 0; d--) {
1663
+ var dm = msgs[d];
1664
+ if (dm && dm.role === "assistant" && dm._serverItemId === serverId) {
1665
+ dupIdx = d;
1666
+ break;
1667
+ }
1668
+ }
1669
+ }
1670
+ if (dupIdx !== -1) msgs[dupIdx] = reply;
1671
+ else msgs.push(reply);
1410
1672
  }
1411
1673
  for (var j = 0; j < msgs.length; j++) {
1412
1674
  var u = msgs[j];
@@ -1646,7 +1908,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1646
1908
  self.state.sending = false;
1647
1909
  if (!(self.host.isViewMounted() && self.getHistoryCacheKey() === key)) return;
1648
1910
  return Promise.resolve(self.typewriteLatestReply(key)).then(function() {
1649
- self.host.scrollToBottom(true);
1911
+ self.host.scrollToBottomIfSticky(true);
1650
1912
  });
1651
1913
  });
1652
1914
  }
@@ -1660,6 +1922,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1660
1922
  if (nextIdx === -1) return;
1661
1923
  var existing = this.state.messages[nextIdx];
1662
1924
  var promoted = { role: "user", content: existing.content, isPendingInProcess: true, isBackgroundTask: true };
1925
+ if (existing._indexFile) promoted._indexFile = existing._indexFile;
1663
1926
  if (existing._serverItemId !== void 0) promoted._serverItemId = existing._serverItemId;
1664
1927
  if (existing._ownerKey !== void 0) promoted._ownerKey = existing._ownerKey;
1665
1928
  this.state.messages[nextIdx] = promoted;
@@ -1680,6 +1943,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1680
1943
  var existing = this.state.messages[nextIdx];
1681
1944
  var promoted = { role: "user", content: existing.content, isPendingInProcess: true };
1682
1945
  if (existing.isBackgroundTask) promoted.isBackgroundTask = true;
1946
+ if (existing._indexFile) promoted._indexFile = existing._indexFile;
1683
1947
  if (existing._serverItemId !== void 0) promoted._serverItemId = existing._serverItemId;
1684
1948
  if (existing._ownerKey !== void 0) promoted._ownerKey = existing._ownerKey;
1685
1949
  if (existing.isSendingToServer) promoted.isSendingToServer = true;
@@ -1780,7 +2044,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1780
2044
  this.promoteNextQueuedToRunning();
1781
2045
  this.updateHistoryCache();
1782
2046
  this.host.notify();
1783
- this.host.scrollToBottom(true);
2047
+ this.host.scrollToBottomIfSticky(true);
1784
2048
  }
1785
2049
  onQueuedSendError(_composed, err, serverId, ownerKey) {
1786
2050
  if (serverId) this.historyItemPolls.delete(serverId);
@@ -1830,7 +2094,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1830
2094
  this.promoteNextQueuedToRunning();
1831
2095
  this.updateHistoryCache();
1832
2096
  this.host.notify();
1833
- this.host.scrollToBottom(true);
2097
+ this.host.scrollToBottomIfSticky(true);
1834
2098
  return;
1835
2099
  }
1836
2100
  var targetIdx = this.resolveQueuedUserBubble(serverId);
@@ -1844,7 +2108,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1844
2108
  this.promoteNextQueuedToRunning();
1845
2109
  this.updateHistoryCache();
1846
2110
  this.host.notify();
1847
- this.host.scrollToBottom(true);
2111
+ this.host.scrollToBottomIfSticky(true);
1848
2112
  }
1849
2113
  cancelQueuedMessage(msg, idx) {
1850
2114
  var self = this;
@@ -1868,6 +2132,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1868
2132
  })).then(function(result) {
1869
2133
  if (result && result.removed) {
1870
2134
  self.cancelledServerIds.add(serverId);
2135
+ self._stopPoll(serverId);
1871
2136
  var qi = self.bgTaskQueue.findIndex(function(e) {
1872
2137
  return e.id === serverId;
1873
2138
  });
@@ -1876,7 +2141,13 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1876
2141
  return m._serverItemId === serverId && (m.isPendingQueued || m.isPendingInProcess) && m.role === "user";
1877
2142
  });
1878
2143
  if (removeIdx !== -1) {
1879
- self.state.messages[removeIdx] = { role: "user", content: self.state.messages[removeIdx].content, isCancelled: true, _serverItemId: serverId };
2144
+ var wasMsg = self.state.messages[removeIdx];
2145
+ var cancelledMsg = { role: "user", content: wasMsg.content, isCancelled: true, _serverItemId: serverId };
2146
+ if (wasMsg.isBackgroundTask) cancelledMsg.isBackgroundTask = true;
2147
+ if (wasMsg._indexFile) cancelledMsg._indexFile = wasMsg._indexFile;
2148
+ if (wasMsg._useBgQueue) cancelledMsg._useBgQueue = true;
2149
+ if (wasMsg._ownerKey !== void 0) cancelledMsg._ownerKey = wasMsg._ownerKey;
2150
+ self.state.messages[removeIdx] = cancelledMsg;
1880
2151
  var thById = self.state.messages.findIndex(function(m) {
1881
2152
  return m._serverItemId === serverId && m.isPending && m.role === "assistant";
1882
2153
  });
@@ -1913,6 +2184,42 @@ Index the REMAINING windows - one record per row/item, looking at any page image
1913
2184
  }
1914
2185
  });
1915
2186
  }
2187
+ /**
2188
+ * Stop indexing a file, from its collapsed row — every pass at once, not just
2189
+ * the bubble the user happens to see.
2190
+ *
2191
+ * A big file is indexed as a CHAIN of passes, so cancelling only the live one
2192
+ * accomplishes nothing: the next pass is dispatched as soon as it settles.
2193
+ * Three things end the chain:
2194
+ * 1. every queued/running pass of this file is cancelled server-side
2195
+ * (csr-cancel deletes a queued row and flags a running one "cancelled",
2196
+ * which is also the worker's gate for NOT enqueueing the next window);
2197
+ * 2. the file is remembered in cancelledIndexKeys, so the client-driven
2198
+ * resume (maybeResumeIndexing) stops dispatching CONTINUE passes; and
2199
+ * 3. any of its passes still sitting in bgTaskQueue is dropped by the next
2200
+ * drain rather than surfacing a fresh "Indexing…" bubble.
2201
+ *
2202
+ * Records already written by the passes that DID run are kept — this stops the
2203
+ * work, it does not undo it.
2204
+ */
2205
+ cancelIndexingGroup(group) {
2206
+ var self = this;
2207
+ if (!group || !group.key) return;
2208
+ var scoped = this.getHistoryCacheKey() + "|" + group.key;
2209
+ this.cancelledIndexKeys.add(scoped);
2210
+ var ids = group.cancellableIds || [];
2211
+ if (!ids.length) {
2212
+ this.host.notify();
2213
+ return;
2214
+ }
2215
+ ids.forEach(function(serverId) {
2216
+ var idx = self.state.messages.findIndex(function(m) {
2217
+ return m._serverItemId === serverId && m.role === "user" && (m.isPendingQueued || m.isPendingInProcess);
2218
+ });
2219
+ if (idx === -1) return;
2220
+ self.cancelQueuedMessage(self.state.messages[idx], idx);
2221
+ });
2222
+ }
1916
2223
  // --- typewriter -------------------------------------------------------
1917
2224
  // Reveal `fullText` into a message bubble at a constant wall-clock RATE
1918
2225
  // (chars/second) driven by requestAnimationFrame, rather than a fixed number
@@ -2099,6 +2406,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2099
2406
  var u = this.state.messages[uIdx];
2100
2407
  var cleaned = { role: "user", content: u.content, _serverItemId: itemId };
2101
2408
  if (u.isBackgroundTask) cleaned.isBackgroundTask = true;
2409
+ if (u._indexFile) cleaned._indexFile = u._indexFile;
2102
2410
  this.state.messages[uIdx] = cleaned;
2103
2411
  }
2104
2412
  // If an immediate-send request for the current cache key is still in flight
@@ -2116,13 +2424,13 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2116
2424
  return (m.isPending || m.isPendingQueued) && !m.isBackgroundTask && !m._useBgQueue;
2117
2425
  })) return Promise.resolve();
2118
2426
  this.state.sending = true;
2119
- this.host.scrollToBottom(true);
2427
+ this.host.scrollToBottomIfSticky(true);
2120
2428
  return Promise.resolve(pending).catch(function() {
2121
2429
  }).then(function() {
2122
2430
  if (token !== self.state.gateRefreshToken) return;
2123
2431
  self.state.sending = false;
2124
2432
  return Promise.resolve(self.typewriteLatestReply(key)).then(function() {
2125
- self.host.scrollToBottom(true);
2433
+ self.host.scrollToBottomIfSticky(true);
2126
2434
  });
2127
2435
  });
2128
2436
  }
@@ -2141,8 +2449,17 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2141
2449
  });
2142
2450
  if (idx !== -1) {
2143
2451
  this._clearPendingUserBubble(itemId);
2452
+ var wasBgTask = !!this.state.messages[idx].isBackgroundTask;
2144
2453
  if (isErr) {
2145
2454
  this.state.messages[idx] = { role: "assistant", content: answer, isError: true, _serverItemId: itemId };
2455
+ if (wasBgTask) this.state.messages[idx].isBackgroundTask = true;
2456
+ this.host.notify();
2457
+ this.updateHistoryCache();
2458
+ return;
2459
+ }
2460
+ var text = answer || "No text response received from AI provider.";
2461
+ if (wasBgTask) {
2462
+ this.state.messages[idx] = { role: "assistant", content: text, isBackgroundTask: true, _serverItemId: itemId };
2146
2463
  this.host.notify();
2147
2464
  this.updateHistoryCache();
2148
2465
  return;
@@ -2150,7 +2467,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2150
2467
  var lid = this._newLocalId();
2151
2468
  this.state.messages[idx] = { role: "assistant", content: "", _localId: lid, _serverItemId: itemId };
2152
2469
  this.host.notify();
2153
- this.enqueueTypewrite(idx, answer || "No text response received from AI provider.", lid);
2470
+ this.enqueueTypewrite(idx, text, lid);
2154
2471
  this.updateHistoryCache();
2155
2472
  return;
2156
2473
  }
@@ -2159,25 +2476,124 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2159
2476
  });
2160
2477
  if (userIdx === -1) return;
2161
2478
  var ex = this.state.messages[userIdx];
2162
- this.state.messages[userIdx] = { role: "user", content: ex.content, _serverItemId: itemId };
2479
+ var settledUser = { role: "user", content: ex.content, _serverItemId: itemId };
2480
+ if (ex.isBackgroundTask) settledUser.isBackgroundTask = true;
2481
+ if (ex._indexFile) settledUser._indexFile = ex._indexFile;
2482
+ if (ex._useBgQueue) settledUser._useBgQueue = true;
2483
+ this.state.messages[userIdx] = settledUser;
2163
2484
  if (isErr) {
2164
- this.state.messages.splice(userIdx + 1, 0, { role: "assistant", content: answer, isError: true, _serverItemId: itemId });
2485
+ var errReply = { role: "assistant", content: answer, isError: true, _serverItemId: itemId };
2486
+ if (ex.isBackgroundTask) errReply.isBackgroundTask = true;
2487
+ this.state.messages.splice(userIdx + 1, 0, errReply);
2488
+ this.host.notify();
2489
+ this.updateHistoryCache();
2490
+ return;
2491
+ }
2492
+ var text2 = answer || "No text response received from AI provider.";
2493
+ if (ex.isBackgroundTask) {
2494
+ this.state.messages.splice(userIdx + 1, 0, { role: "assistant", content: text2, isBackgroundTask: true, _serverItemId: itemId });
2165
2495
  this.host.notify();
2166
2496
  this.updateHistoryCache();
2167
2497
  return;
2168
2498
  }
2169
2499
  var lid2 = this._newLocalId();
2170
- this.state.messages.splice(userIdx + 1, 0, { role: "assistant", content: "", _localId: lid2, _serverItemId: itemId });
2500
+ var reply = { role: "assistant", content: "", _localId: lid2, _serverItemId: itemId };
2501
+ this.state.messages.splice(userIdx + 1, 0, reply);
2171
2502
  this.host.notify();
2172
- this.enqueueTypewrite(userIdx + 1, answer || "No text response received from AI provider.", lid2);
2503
+ this.enqueueTypewrite(userIdx + 1, text2, lid2);
2173
2504
  this.updateHistoryCache();
2174
2505
  }
2506
+ /** How a bg task maps onto a collapsed row: the row's own key (storage path
2507
+ * when known, else the filename), scoped to the chat it belongs to. A storage
2508
+ * path is project-relative ("report.xlsx"), and ONE ChatSession serves every
2509
+ * project — unscoped, stopping a file in one project would silently suppress
2510
+ * the same filename's continuations in another. */
2511
+ _indexKeyOf(entry) {
2512
+ if (!entry) return "";
2513
+ var file = entry.storagePath || entry.filename;
2514
+ if (!file) return "";
2515
+ return entry.serviceId + "#" + entry.platform + "|" + file;
2516
+ }
2517
+ /**
2518
+ * Reconcile the bg queue with the files the user has stopped.
2519
+ *
2520
+ * A FIRST pass (no resumePass) is a fresh indexing request — a re-upload, or a
2521
+ * Reindex from the file manager — so it LIFTS the stop: the key is a storage
2522
+ * path, and without this an earlier cancel would silently kill every future
2523
+ * index of the same path. A continuation of a stopped file is dropped instead,
2524
+ * covering the pass that was dispatched in the moment before the cancel landed.
2525
+ */
2526
+ _applyIndexCancellations() {
2527
+ if (!this.cancelledIndexKeys.size) return;
2528
+ for (var i = this.bgTaskQueue.length - 1; i >= 0; i--) {
2529
+ var entry = this.bgTaskQueue[i];
2530
+ var key = this._indexKeyOf(entry);
2531
+ if (!key || !this.cancelledIndexKeys.has(key)) continue;
2532
+ if (!entry.resumePass) {
2533
+ this.cancelledIndexKeys.delete(key);
2534
+ continue;
2535
+ }
2536
+ this.bgTaskQueue.splice(i, 1);
2537
+ this._stopPoll(entry.id);
2538
+ this._cancelServerItem(entry.id);
2539
+ }
2540
+ }
2541
+ /**
2542
+ * Cancel any live pass of a stopped file that turned up on its own.
2543
+ *
2544
+ * The client is not the only thing that continues a file: for PDFs and (when
2545
+ * windowed indexing is on) text/grid files the WORKER enqueues the next window
2546
+ * itself, and that pass reaches the chat through the history poll, never
2547
+ * through bgTaskQueue. The worker's own gate stops the chain when the running
2548
+ * row is cancelled — but if the row had already finished when the user hit
2549
+ * stop, the next window was queued a moment earlier and still arrives. Stop it
2550
+ * here rather than making the user hit stop again.
2551
+ *
2552
+ * Runs from drainBgTaskQueue, which both clients call after a history load.
2553
+ */
2554
+ _sweepCancelledIndexing() {
2555
+ if (!this.cancelledIndexKeys.size) return;
2556
+ var self = this;
2557
+ var chatKey = this.getHistoryCacheKey();
2558
+ var targets = [];
2559
+ this.state.messages.forEach(function(m, i) {
2560
+ if (!m.isBackgroundTask || m.role !== "user" || !m._serverItemId) return;
2561
+ if (m._cancelling || m.isSendingToServer) return;
2562
+ if (!(m.isPendingQueued || m.isPendingInProcess)) return;
2563
+ var ref = m._indexFile;
2564
+ var file = ref && (ref.path || ref.name);
2565
+ if (!file || !self.cancelledIndexKeys.has(chatKey + "|" + file)) return;
2566
+ targets.push({ msg: m, idx: i });
2567
+ });
2568
+ targets.forEach(function(t) {
2569
+ var idx = self.state.messages.indexOf(t.msg);
2570
+ self.cancelQueuedMessage(t.msg, idx === -1 ? t.idx : idx);
2571
+ });
2572
+ }
2573
+ /** Best-effort server-side cancel of a bg-queue item that has no bubble (so
2574
+ * cancelQueuedMessage, which drives one, has nothing to act on). */
2575
+ _cancelServerItem(serverId) {
2576
+ var id = this.host.getIdentity();
2577
+ if (!serverId || id.platform !== "claude" && id.platform !== "openai") return;
2578
+ var url = id.platform === "claude" ? ANTHROPIC_MESSAGES_API_URL : OPENAI_RESPONSES_API_URL;
2579
+ Promise.resolve(this.host.cancelRequest({
2580
+ url,
2581
+ method: "POST",
2582
+ id: serverId,
2583
+ queue: (id.userId || id.serviceId) + BG_INDEXING_QUEUE_SUFFIX,
2584
+ service: id.serviceId,
2585
+ owner: id.owner
2586
+ })).catch(function() {
2587
+ });
2588
+ }
2175
2589
  // Inject "Indexing: <file>" bubbles for queued bg tasks + attach their polls.
2176
2590
  drainBgTaskQueue() {
2177
2591
  var self = this;
2178
2592
  var id = this.host.getIdentity();
2179
2593
  var svcId = id.serviceId, plat = id.platform;
2180
2594
  if (!svcId || plat === "none" || !this.host.isViewMounted()) return;
2595
+ this._applyIndexCancellations();
2596
+ this._sweepCancelledIndexing();
2181
2597
  var presentIds = {};
2182
2598
  var pendingIds = {};
2183
2599
  this.state.messages.forEach(function(m) {
@@ -2195,7 +2611,22 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2195
2611
  if (entry.serviceId !== svcId || entry.platform !== plat) return;
2196
2612
  if (!presentIds[entry.id]) {
2197
2613
  var isRunning = entry.status === "running";
2198
- var userBubble = { role: "user", content: self.host.formatIndexingLabel(entry.filename, entry.mime, entry.size, entry.storagePath, entry.isReindex), isBackgroundTask: true, _serverItemId: entry.id };
2614
+ var userBubble = {
2615
+ role: "user",
2616
+ content: self.host.formatIndexingLabel(entry.filename, entry.mime, entry.size, entry.storagePath, entry.isReindex, !!entry.resumePass),
2617
+ isBackgroundTask: true,
2618
+ _serverItemId: entry.id,
2619
+ // Structured ref so this live pass groups with the same file's passes
2620
+ // rebuilt from history (see indexing_groups.buildChatDisplayList).
2621
+ _indexFile: {
2622
+ name: entry.filename,
2623
+ path: entry.storagePath,
2624
+ mime: entry.mime,
2625
+ size: entry.size,
2626
+ isReindex: !!entry.isReindex,
2627
+ continued: !!entry.resumePass
2628
+ }
2629
+ };
2199
2630
  if (isRunning) userBubble.isPendingInProcess = true;
2200
2631
  else userBubble.isPendingQueued = true;
2201
2632
  self.state.messages.push(userBubble);
@@ -2205,7 +2636,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2205
2636
  presentIds[entry.id] = true;
2206
2637
  self.host.notify();
2207
2638
  self.updateHistoryCache();
2208
- self.host.scrollToBottom(false);
2639
+ self.host.scrollToBottomIfSticky(false);
2209
2640
  }
2210
2641
  if (!self.isPollingPaused() && !self.historyItemPolls.has(entry.id) && typeof entry.poll === "function") {
2211
2642
  var capturedId = entry.id, capturedPlat = plat;
@@ -2223,15 +2654,23 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2223
2654
  }).catch(function(err) {
2224
2655
  self.historyItemPolls.delete(capturedId);
2225
2656
  var isNotExists = err && (err.code === "NOT_EXISTS" || err.body && err.body.code === "NOT_EXISTS");
2657
+ self._clearPendingUserBubble(capturedId);
2226
2658
  var bi = self.state.messages.findIndex(function(m) {
2227
2659
  return m.isPending && m._serverItemId === capturedId;
2228
2660
  });
2229
2661
  if (bi !== -1) {
2230
2662
  if (isNotExists) self.state.messages.splice(bi, 1);
2231
2663
  else self.state.messages[bi] = { role: "assistant", content: getErrorMessage(err), isError: true, isBackgroundTask: true, _serverItemId: capturedId };
2232
- self.host.notify();
2233
- self.updateHistoryCache();
2664
+ } else if (!isNotExists) {
2665
+ var ui = self.state.messages.findIndex(function(m) {
2666
+ return m.role === "user" && m._serverItemId === capturedId;
2667
+ });
2668
+ if (ui !== -1) {
2669
+ self.state.messages.splice(ui + 1, 0, { role: "assistant", content: getErrorMessage(err), isError: true, isBackgroundTask: true, _serverItemId: capturedId });
2670
+ }
2234
2671
  }
2672
+ self.host.notify();
2673
+ self.updateHistoryCache();
2235
2674
  }).then(function() {
2236
2675
  if (wasStopped) return;
2237
2676
  var qi = self.bgTaskQueue.findIndex(function(q) {
@@ -2259,8 +2698,10 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2259
2698
  var self = this;
2260
2699
  try {
2261
2700
  if (!entry || !entry.storagePath) return;
2701
+ if (this.cancelledIndexKeys.has(this._indexKeyOf(entry))) return;
2262
2702
  if (!isPagedReadFile(entry.filename, entry.mime)) return;
2263
2703
  if (isImageVisionFile(entry.filename, entry.mime)) return;
2704
+ if (windowedIndexingEnabled() && isWindowedReadFile(entry.filename, entry.mime)) return;
2264
2705
  if (isErrorResponseBody(response)) return;
2265
2706
  var answer = (platform === "openai" ? extractOpenAIText(response) : extractClaudeText(response)) || "";
2266
2707
  if (answer.indexOf(INDEXING_COMPLETE_MARKER) !== -1) return;
@@ -2353,6 +2794,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2353
2794
  serviceId: id.serviceId,
2354
2795
  formatIndexingLabel: self.host.formatIndexingLabel
2355
2796
  }).messages;
2797
+ var keptOlderPages = false;
2356
2798
  if (fetchMore) {
2357
2799
  self.state.messages = mapped.concat(self.state.messages);
2358
2800
  } else {
@@ -2363,7 +2805,12 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2363
2805
  });
2364
2806
  var locallyCancelled = {};
2365
2807
  self.state.messages.forEach(function(m) {
2366
- if (m.isCancelled && m._serverItemId) locallyCancelled[m._serverItemId] = 1;
2808
+ if (m.isCancelled && m._serverItemId) locallyCancelled[m._serverItemId] = m;
2809
+ });
2810
+ var inFlightCancel = {};
2811
+ self.state.messages.forEach(function(m) {
2812
+ if (!m._serverItemId) return;
2813
+ if (m._cancelling || m._cancelError) inFlightCancel[m._serverItemId] = m;
2367
2814
  });
2368
2815
  var mappedHasPendingAssistant = mapped.some(function(m) {
2369
2816
  return m.isPending && m.role === "assistant" && !m.isBackgroundTask;
@@ -2383,7 +2830,22 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2383
2830
  }
2384
2831
  }
2385
2832
  }
2386
- self.state.messages = mapped;
2833
+ var oldestInPage1 = void 0;
2834
+ mapped.forEach(function(m) {
2835
+ var sid = m._serverItemId;
2836
+ if (typeof sid !== "string") return;
2837
+ if (oldestInPage1 === void 0 || sid < oldestInPage1) oldestInPage1 = sid;
2838
+ });
2839
+ var sharesPage1 = self.state.messages.some(function(m) {
2840
+ return typeof m._serverItemId === "string" && !!serverIds[m._serverItemId];
2841
+ });
2842
+ var retainedOlder = !sharesPage1 || oldestInPage1 === void 0 ? [] : self.state.messages.filter(function(m) {
2843
+ if (typeof m._serverItemId !== "string") return false;
2844
+ if (m._ownerKey !== void 0 && m._ownerKey !== loadKey) return false;
2845
+ return m._serverItemId < oldestInPage1;
2846
+ });
2847
+ keptOlderPages = retainedOlder.length > 0;
2848
+ self.state.messages = keptOlderPages ? retainedOlder.concat(mapped) : mapped;
2387
2849
  rescued.forEach(function(m) {
2388
2850
  self.state.messages.push(m);
2389
2851
  });
@@ -2391,19 +2853,38 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2391
2853
  for (var ci = 0; ci < self.state.messages.length; ci++) {
2392
2854
  var c = self.state.messages[ci];
2393
2855
  if (!c._serverItemId || !locallyCancelled[c._serverItemId] || c.isCancelled) continue;
2394
- self.state.messages[ci] = { role: "user", content: c.content, isCancelled: true, _serverItemId: c._serverItemId };
2856
+ self.state.messages[ci] = {
2857
+ role: "user",
2858
+ content: c.content,
2859
+ isCancelled: true,
2860
+ _serverItemId: c._serverItemId,
2861
+ isBackgroundTask: c.isBackgroundTask,
2862
+ _indexFile: c._indexFile,
2863
+ _useBgQueue: c._useBgQueue,
2864
+ _ownerKey: c._ownerKey
2865
+ };
2395
2866
  if (ci + 1 < self.state.messages.length && self.state.messages[ci + 1].isPending && self.state.messages[ci + 1]._serverItemId === c._serverItemId) {
2396
2867
  self.state.messages.splice(ci + 1, 1);
2397
2868
  }
2398
2869
  }
2399
2870
  }
2871
+ for (var fi = 0; fi < self.state.messages.length; fi++) {
2872
+ var fm = self.state.messages[fi];
2873
+ var was = fm._serverItemId && inFlightCancel[fm._serverItemId];
2874
+ if (!was || fm.isCancelled) continue;
2875
+ if (!(fm.isPendingQueued || fm.isPendingInProcess || fm.isPending)) continue;
2876
+ if (was._cancelling) fm._cancelling = true;
2877
+ if (was._cancelError) fm._cancelError = was._cancelError;
2878
+ }
2400
2879
  }
2401
- self.state.historyEndOfList = !!(history && history.endOfList);
2402
- self.state.historyStartKeyHistory = history && Array.isArray(history.startKeyHistory) ? history.startKeyHistory : [];
2403
- var clearedAt = self.host.getClearedAt();
2404
- if (clearedAt && chatList.length > 0) {
2405
- var oldestUpdated = Number(chatList[chatList.length - 1] && chatList[chatList.length - 1].updated);
2406
- if (isFinite(oldestUpdated) && oldestUpdated <= clearedAt) self.state.historyEndOfList = true;
2880
+ if (!keptOlderPages) {
2881
+ self.state.historyEndOfList = !!(history && history.endOfList);
2882
+ self.state.historyStartKeyHistory = history && Array.isArray(history.startKeyHistory) ? history.startKeyHistory : [];
2883
+ var clearedAt = self.host.getClearedAt();
2884
+ if (clearedAt && chatList.length > 0) {
2885
+ var oldestUpdated = Number(chatList[chatList.length - 1] && chatList[chatList.length - 1].updated);
2886
+ if (isFinite(oldestUpdated) && oldestUpdated <= clearedAt) self.state.historyEndOfList = true;
2887
+ }
2407
2888
  }
2408
2889
  if (self.state.historyRequestToken === token) {
2409
2890
  self.state.loadingHistory = false;
@@ -2432,18 +2913,25 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2432
2913
  return m.isPending && m._serverItemId === capturedId;
2433
2914
  });
2434
2915
  if (isNotExists) {
2435
- var isBg = aIdx !== -1 ? !!self.state.messages[aIdx].isBackgroundTask : false;
2916
+ var uIdx = self.state.messages.findIndex(function(m) {
2917
+ return m.role === "user" && m._serverItemId === capturedId && !m.isCancelled;
2918
+ });
2919
+ var isBg = aIdx !== -1 && !!self.state.messages[aIdx].isBackgroundTask || uIdx !== -1 && !!self.state.messages[uIdx].isBackgroundTask;
2436
2920
  if (aIdx !== -1) self.state.messages.splice(aIdx, 1);
2437
2921
  if (!isBg) {
2438
- var uIdx = self.state.messages.findIndex(function(m) {
2439
- return m.role === "user" && m._serverItemId === capturedId && !m.isCancelled;
2440
- });
2441
2922
  if (uIdx !== -1) {
2442
2923
  var ex = self.state.messages[uIdx];
2443
2924
  self.state.messages[uIdx] = { role: "user", content: ex.content, isCancelled: true, _serverItemId: ex._serverItemId };
2444
2925
  }
2445
2926
  self.cancelledServerIds.delete(capturedId);
2446
2927
  self.promoteNextQueuedToRunning();
2928
+ } else if (uIdx !== -1) {
2929
+ var bex = self.state.messages[uIdx];
2930
+ var bcancelled = { role: "user", content: bex.content, isCancelled: true, isBackgroundTask: true, _serverItemId: bex._serverItemId };
2931
+ if (bex._indexFile) bcancelled._indexFile = bex._indexFile;
2932
+ if (bex._useBgQueue) bcancelled._useBgQueue = true;
2933
+ self.state.messages[uIdx] = bcancelled;
2934
+ self.promoteNextBgQueuedToRunning();
2447
2935
  }
2448
2936
  self.host.notify();
2449
2937
  self.updateHistoryCache();
@@ -2464,7 +2952,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2464
2952
  });
2465
2953
  self.drainBgTaskQueue();
2466
2954
  }
2467
- if (!fetchMore) return self.host.scrollToBottom();
2955
+ if (!fetchMore) return self.host.scrollToBottomIfSticky();
2468
2956
  }).catch(function(err) {
2469
2957
  console.warn("[chat-engine] getChatHistory failed", err);
2470
2958
  }).then(function() {
@@ -2474,6 +2962,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2474
2962
  self.state.loadingOlderHistory = false;
2475
2963
  if (wasLoading) self.host.notify();
2476
2964
  }
2965
+ if (self.host.onHistoryLoaded) self.host.onHistoryLoaded(!!fetchMore, token);
2477
2966
  });
2478
2967
  }
2479
2968
  // --- attachment upload orchestration ---------------------------------
@@ -2671,11 +3160,194 @@ Index the REMAINING windows - one record per row/item, looking at any page image
2671
3160
  }
2672
3161
  };
2673
3162
 
3163
+ // src/engine/indexing_groups.ts
3164
+ var INDEXING_LABEL_RE = /^(Re)?[Ii]ndexing(\s*\(continuing\))?\s*:?\s+(.+)$/;
3165
+ var LEADING_MD_LINK_RE = /^\[([^\]]+)\]\(([^)]+)\)/;
3166
+ function parseIndexingLabel(content) {
3167
+ if (typeof content !== "string" || !content) return null;
3168
+ var firstLine = content.split("\n")[0].trim();
3169
+ var m = firstLine.match(INDEXING_LABEL_RE);
3170
+ if (!m) return null;
3171
+ var head = m[3].split(" \xB7 ")[0].trim();
3172
+ var link = head.match(LEADING_MD_LINK_RE);
3173
+ var name = link ? link[1].trim() : head;
3174
+ if (!name) return null;
3175
+ return {
3176
+ name,
3177
+ path: link ? link[2].trim() : void 0,
3178
+ continued: !!m[2],
3179
+ isReindex: !!m[1]
3180
+ };
3181
+ }
3182
+ function readFileRef(msg) {
3183
+ var ref = msg && msg._indexFile;
3184
+ if (ref && (ref.path || ref.name)) {
3185
+ return {
3186
+ name: ref.name || ref.path || "",
3187
+ path: ref.path,
3188
+ mime: ref.mime,
3189
+ size: ref.size,
3190
+ isReindex: ref.isReindex,
3191
+ continued: !!ref.continued
3192
+ };
3193
+ }
3194
+ var parsed = parseIndexingLabel(msg && msg.content);
3195
+ if (!parsed) return null;
3196
+ return {
3197
+ name: parsed.name,
3198
+ path: parsed.path,
3199
+ isReindex: parsed.isReindex,
3200
+ continued: parsed.continued
3201
+ };
3202
+ }
3203
+ function isPendingMsg(m) {
3204
+ return !!(m.isPending || m.isPendingInProcess || m.isPendingQueued || m.isSendingToServer);
3205
+ }
3206
+ function buildChatDisplayList(messages, opts) {
3207
+ var list = Array.isArray(messages) ? messages : [];
3208
+ var hasMoreHistory = !!(opts && opts.hasMoreHistory);
3209
+ var groups = {};
3210
+ var order = [];
3211
+ var runOfIndex = new Array(list.length);
3212
+ var runByItemId = {};
3213
+ var keyByName = {};
3214
+ var openRunOfKey = {};
3215
+ var runsOfKey = {};
3216
+ var keyOfRun = {};
3217
+ var runSeq = 0;
3218
+ for (var i = 0; i < list.length; i++) {
3219
+ var msg = list[i];
3220
+ if (!msg || !msg.isBackgroundTask) continue;
3221
+ var runId;
3222
+ var ref = msg.role === "user" ? readFileRef(msg) : null;
3223
+ if (ref) {
3224
+ var key = ref.path || keyByName[ref.name] || ref.name;
3225
+ if (!ref.continued && openRunOfKey[key]) delete openRunOfKey[key];
3226
+ runId = openRunOfKey[key];
3227
+ if (!runId) {
3228
+ runId = "run" + runSeq++;
3229
+ openRunOfKey[key] = runId;
3230
+ keyOfRun[runId] = key;
3231
+ (runsOfKey[key] || (runsOfKey[key] = [])).push(runId);
3232
+ }
3233
+ } else if (msg._serverItemId && runByItemId[msg._serverItemId]) {
3234
+ runId = runByItemId[msg._serverItemId];
3235
+ } else if (msg.role !== "user") {
3236
+ runId = runOfIndex[i - 1];
3237
+ }
3238
+ if (!runId) continue;
3239
+ var g = groups[runId];
3240
+ if (!g) {
3241
+ var fileKey = keyOfRun[runId];
3242
+ g = groups[runId] = {
3243
+ key: fileKey,
3244
+ runKey: runId,
3245
+ // provisional; renumbered newest-first below
3246
+ name: ref ? ref.name : fileKey,
3247
+ path: ref ? ref.path : void 0,
3248
+ mime: ref ? ref.mime : void 0,
3249
+ size: ref ? ref.size : void 0,
3250
+ isReindex: !!(ref && ref.isReindex),
3251
+ members: [],
3252
+ passCount: 0,
3253
+ status: "done",
3254
+ cancellableIds: [],
3255
+ cancelling: false,
3256
+ mayHaveOlder: false,
3257
+ anchorIndex: i
3258
+ };
3259
+ order.push(runId);
3260
+ }
3261
+ if (ref) {
3262
+ if (ref.name) g.name = ref.name;
3263
+ if (ref.path) g.path = ref.path;
3264
+ if (ref.mime) g.mime = ref.mime;
3265
+ if (typeof ref.size === "number") g.size = ref.size;
3266
+ if (ref.isReindex) g.isReindex = true;
3267
+ if (!ref.continued) g.mayHaveOlder = false;
3268
+ g.passCount++;
3269
+ }
3270
+ g.members.push({ msg, index: i });
3271
+ g.anchorIndex = i;
3272
+ runOfIndex[i] = runId;
3273
+ if (msg._serverItemId) runByItemId[msg._serverItemId] = runId;
3274
+ if (ref && ref.name) keyByName[ref.name] = g.key;
3275
+ }
3276
+ for (var rk in runsOfKey) {
3277
+ var runIds = runsOfKey[rk];
3278
+ for (var ri = 0; ri < runIds.length; ri++) {
3279
+ var grpR = groups[runIds[ri]];
3280
+ if (!grpR) continue;
3281
+ var first = grpR.members[0];
3282
+ var firstId = first && first.msg && (first.msg._serverItemId || first.msg._localId);
3283
+ grpR.runKey = rk + "#" + (firstId || "n" + ri);
3284
+ }
3285
+ }
3286
+ for (var oi = 0; oi < order.length; oi++) {
3287
+ var grp = groups[order[oi]];
3288
+ var lastSettled = -1;
3289
+ for (var si = 0; si < grp.members.length; si++) {
3290
+ if (!isPendingMsg(grp.members[si].msg)) lastSettled = si;
3291
+ }
3292
+ var active = false;
3293
+ for (var mi = lastSettled + 1; mi < grp.members.length; mi++) {
3294
+ if (isPendingMsg(grp.members[mi].msg)) {
3295
+ active = true;
3296
+ break;
3297
+ }
3298
+ }
3299
+ for (var xi = 0; xi < grp.members.length; xi++) {
3300
+ if (grp.members[xi].msg._cancelling) {
3301
+ grp.cancelling = true;
3302
+ break;
3303
+ }
3304
+ }
3305
+ var seenIds = {};
3306
+ for (var ci = 0; ci < grp.members.length; ci++) {
3307
+ var cm = grp.members[ci].msg;
3308
+ if (cm._cancelError && (active || grp.cancelling)) grp.cancelError = cm._cancelError;
3309
+ if (cm.role !== "user" || !cm._serverItemId || cm._cancelling || cm.isSendingToServer) continue;
3310
+ if (!(cm.isPendingQueued || cm.isPendingInProcess)) continue;
3311
+ if (ci < lastSettled) continue;
3312
+ if (seenIds[cm._serverItemId]) continue;
3313
+ seenIds[cm._serverItemId] = true;
3314
+ grp.cancellableIds.push(cm._serverItemId);
3315
+ }
3316
+ if (active) {
3317
+ grp.status = "active";
3318
+ } else {
3319
+ var last = grp.members[grp.members.length - 1].msg;
3320
+ grp.status = last.isError ? "error" : last.isCancelled ? "cancelled" : "done";
3321
+ }
3322
+ var sawFirstPass = false;
3323
+ for (var pi = 0; pi < grp.members.length; pi++) {
3324
+ var pm = grp.members[pi].msg;
3325
+ if (pm.role !== "user") continue;
3326
+ var pref = readFileRef(pm);
3327
+ if (pref && !pref.continued) {
3328
+ sawFirstPass = true;
3329
+ break;
3330
+ }
3331
+ }
3332
+ grp.mayHaveOlder = !sawFirstPass && hasMoreHistory;
3333
+ }
3334
+ var out = [];
3335
+ for (var j = 0; j < list.length; j++) {
3336
+ var r = runOfIndex[j];
3337
+ if (r === void 0) {
3338
+ out.push({ kind: "message", msg: list[j], index: j });
3339
+ continue;
3340
+ }
3341
+ if (groups[r].anchorIndex === j) out.push({ kind: "indexing", group: groups[r], index: j });
3342
+ }
3343
+ return out;
3344
+ }
3345
+
2674
3346
  // src/index.js
2675
3347
  (function() {
2676
3348
  var MCP_PROD = "https://mcp.broadwayinc.computer";
2677
3349
  var MCP_DEV = "https://mcp-dev.broadwayinc.computer";
2678
- var BQ_VERSION = "1.6.1" ;
3350
+ var BQ_VERSION = "1.7.0" ;
2679
3351
  var ATTACHMENT_URL_EXPIRES_SECONDS = 600;
2680
3352
  var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth";
2681
3353
  var GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token";
@@ -3887,6 +4559,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
3887
4559
  if (CS.composerEl && CS.chatEl && CS.composerEl.parentNode !== CS.chatEl) CS.chatEl.appendChild(CS.composerEl);
3888
4560
  renderMessages();
3889
4561
  scrollToBottom();
4562
+ ensureHistoryFillsViewport();
3890
4563
  }
3891
4564
  function renderAccount() {
3892
4565
  if (!CS.messagesBox) return;
@@ -4254,8 +4927,12 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4254
4927
  }
4255
4928
  var CS = {
4256
4929
  messages: [],
4930
+ // Rendered .bq-message nodes, indexed BY MESSAGE INDEX (sparse: a message
4931
+ // folded into a collapsed indexing row has no node of its own).
4257
4932
  messageEls: [],
4258
- // parallel rendered .bq-message nodes
4933
+ // Expanded background-indexing rows, keyed by RUN (group.runKey), not by
4934
+ // file: re-indexing a file is a separate row and expands separately.
4935
+ indexGroupsOpen: {},
4259
4936
  messagesBox: null,
4260
4937
  // .bq-messages element
4261
4938
  sending: false,
@@ -4325,14 +5002,21 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4325
5002
  scrollToBottomIfSticky: function(smooth) {
4326
5003
  return scrollToBottomIfSticky(smooth);
4327
5004
  },
5005
+ // A first page can render shorter than the box (a file's every indexing
5006
+ // pass folds into ONE row), and a box that cannot scroll never fires the
5007
+ // scroll-to-top that is the sole trigger for loading page 2. Page out of
5008
+ // it here — only the view can measure.
5009
+ onHistoryLoaded: function(fetchMore, token) {
5010
+ if (!fetchMore) ensureHistoryFillsViewport(token);
5011
+ },
4328
5012
  cancelRequest: function(opts) {
4329
5013
  return S.skapi.cancelClientSecretRequest(opts);
4330
5014
  },
4331
5015
  refreshSession: function() {
4332
5016
  return refreshSkapiSession();
4333
5017
  },
4334
- formatIndexingLabel: function(name, mime, size, storagePath, reindex) {
4335
- return buildIndexingLabel(name, mime, size, storagePath, reindex);
5018
+ formatIndexingLabel: function(name, mime, size, storagePath, reindex, continued) {
5019
+ return buildIndexingLabel(name, mime, size, storagePath, reindex, continued);
4336
5020
  },
4337
5021
  isViewMounted: function() {
4338
5022
  return !!CS.messagesBox;
@@ -4532,7 +5216,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4532
5216
  if (!CS.messagesBox || CS.chatSettingsOpen) return;
4533
5217
  var el = CS.messagesBox;
4534
5218
  CS.stickToBottom = el.scrollHeight - el.scrollTop - el.clientHeight <= 16;
4535
- if (el.scrollTop <= 60) fetchOlderHistoryIfNeeded();
5219
+ if (el.scrollTop <= 60) pageOlderHistoryUntilTaller();
4536
5220
  }
4537
5221
  var _touchStartY = 0;
4538
5222
  function onMessagesWheel(e) {
@@ -4545,23 +5229,6 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4545
5229
  var y = e.touches && e.touches[0] ? e.touches[0].clientY : 0;
4546
5230
  if (y > _touchStartY + 4) CS.stickToBottom = false;
4547
5231
  }
4548
- function normalizeTrailingInlineToken(value) {
4549
- if (!value) return value;
4550
- var out = value.replace(/[.,;:!?]+$/, "");
4551
- var trimUnmatched = function(openCh, closeCh) {
4552
- while (out.charAt(out.length - 1) === closeCh) {
4553
- var openCount = (out.match(new RegExp("\\" + openCh, "g")) || []).length;
4554
- var closeCount = (out.match(new RegExp("\\" + closeCh, "g")) || []).length;
4555
- if (closeCount > openCount) out = out.slice(0, -1);
4556
- else break;
4557
- }
4558
- };
4559
- trimUnmatched("(", ")");
4560
- trimUnmatched("[", "]");
4561
- trimUnmatched("{", "}");
4562
- out = out.replace(/[`'"*>]+$/, "");
4563
- return out;
4564
- }
4565
5232
  function getOrCreateFileHref(filename, body) {
4566
5233
  var key = filename + "\0" + body;
4567
5234
  var existing = fileBlobCache.get(key);
@@ -4602,41 +5269,13 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4602
5269
  return "<a " + attrs.join(" ") + ">" + escapeHtml(labelText) + "</a>";
4603
5270
  }
4604
5271
  function buildLinkPartFromGroups(full, g1, g2, g3, g4, g5, g6) {
4605
- var dbHostPrefix = "https://db." + hostDomain();
4606
- if (g1) {
4607
- var rawPath = normalizeTrailingInlineToken(g1);
4608
- var consumed = "src::" + rawPath;
4609
- var tail = full.slice(consumed.length);
4610
- var isUrl = /^https?:\/\//i.test(rawPath);
4611
- if (isUrl && /^https:\/\//i.test(rawPath) && rawPath.toLowerCase().indexOf(dbHostPrefix.toLowerCase()) !== 0) {
4612
- return { part: { type: "link", label: truncateLabelForDisplay(rawPath), fullLabel: rawPath, href: rawPath, expired: false }, tail };
5272
+ return classifyInlineLink(full, [g1, g2, g3, g4, g5, g6], {
5273
+ serviceId: S.serviceId,
5274
+ dbHostPrefix: "https://db." + hostDomain(),
5275
+ resolveFreshHref: function(expiredHref) {
5276
+ return refreshedExpiredLinkMap[expiredHref];
4613
5277
  }
4614
- var remotePath = isUrl ? extractRemotePathFromAttachmentHref(rawPath, S.serviceId) || normalizeAttachmentPathCandidate(rawPath) : normalizeAttachmentPathCandidate(rawPath);
4615
- if (!remotePath) return null;
4616
- var expiredHref = buildDisplayExpiredAttachmentHref(remotePath, remotePath);
4617
- var cached = refreshedExpiredLinkMap[expiredHref];
4618
- return { part: { type: "link", label: truncateLabelForDisplay(remotePath), fullLabel: remotePath, href: cached || expiredHref, expired: !cached, expiredHref, remotePath }, tail };
4619
- }
4620
- if (g4 && g5) {
4621
- var rp = normalizeAttachmentPathCandidate(g5);
4622
- if (!rp) return null;
4623
- var eh = buildDisplayExpiredAttachmentHref(rp, rp);
4624
- var c2 = refreshedExpiredLinkMap[eh];
4625
- return { part: { type: "link", label: truncateLabelForDisplay(g4), fullLabel: g4, href: c2 || eh, expired: !c2, expiredHref: eh, remotePath: rp } };
4626
- }
4627
- var originalHref = g3 || g6 || "";
4628
- if (!originalHref) return null;
4629
- if (/^https:\/\//i.test(originalHref) && originalHref.toLowerCase().indexOf(dbHostPrefix.toLowerCase()) !== 0) {
4630
- var plainLabel = g2 || originalHref;
4631
- return { part: { type: "link", label: truncateLabelForDisplay(plainLabel), fullLabel: plainLabel, href: originalHref, expired: false } };
4632
- }
4633
- var rmp = extractRemotePathFromAttachmentHref(originalHref, S.serviceId);
4634
- var fbLabel = g2 || originalHref;
4635
- var ehref = rmp ? buildDisplayExpiredAttachmentHref(rmp, fbLabel) : originalHref;
4636
- var cfresh = refreshedExpiredLinkMap[ehref];
4637
- var expired = !!rmp && !cfresh;
4638
- var fullLabel = rmp ? getExpiredAttachmentVisiblePath(rmp, g2 || originalHref) : g2 || originalHref;
4639
- return { part: { type: "link", label: truncateLabelForDisplay(fullLabel), fullLabel, href: cfresh || ehref, expired, expiredHref: ehref, remotePath: rmp || void 0 } };
5278
+ });
4640
5279
  }
4641
5280
  function parseMsgPartsHtml(content) {
4642
5281
  var placeholderHtml = [];
@@ -4702,9 +5341,10 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4702
5341
  if (n < 1024 * 1024) return (n / 1024).toFixed(1) + " KB";
4703
5342
  return (n / (1024 * 1024)).toFixed(1) + " MB";
4704
5343
  }
4705
- function buildIndexingLabel(name, mime, size, storagePath, reindex) {
4706
- var extras = [];
5344
+ function buildIndexingLabel(name, mime, size, storagePath, reindex, continued) {
4707
5345
  var nameLabel = storagePath ? "[" + name + "](" + storagePath + ")" : name;
5346
+ if (continued) return "Indexing (continuing) " + nameLabel;
5347
+ var extras = [];
4708
5348
  if (mime) extras.push(mime);
4709
5349
  if (size != null && size !== "" && !isNaN(Number(size))) extras.push(formatBytes(size));
4710
5350
  return (reindex ? "Reindexing: " : "Indexing: ") + nameLabel + (extras.length ? " \xB7 " + extras.join(" \xB7 ") : "");
@@ -4775,16 +5415,17 @@ Index the REMAINING windows - one record per row/item, looking at any page image
4775
5415
  return S.skapi.deleteRecords({ service: S.serviceId, unique_id: "src::" + storagePath }).catch(function() {
4776
5416
  });
4777
5417
  }
4778
- function getTemporaryUrlDb(path, expires) {
4779
- return S.skapi.util.request("get-signed-url", {
5418
+ function getTemporaryUrlDb(path, expires, cdn) {
5419
+ var body = {
4780
5420
  service: S.serviceId,
4781
5421
  owner: S.owner,
4782
5422
  request: "get-db",
4783
5423
  key: path,
4784
- expires: expires,
4785
- contentType: mimeGetType(path) || "application/octet-stream",
4786
- generate_temporary_cdn_url: true
4787
- }, { auth: true, method: "post" }).then(function(res) {
5424
+ expires: expires || ATTACHMENT_URL_EXPIRES_SECONDS,
5425
+ contentType: mimeGetType(path) || "application/octet-stream"
5426
+ };
5427
+ if (cdn !== false) body.generate_temporary_cdn_url = true;
5428
+ return S.skapi.util.request("get-signed-url", body, { auth: true, method: "post" }).then(function(res) {
4788
5429
  var u = typeof res === "string" ? res : res && res.url;
4789
5430
  if (!u) throw new Error("No temporary URL returned.");
4790
5431
  return "https://db." + hostDomain() + "/" + u;
@@ -5288,7 +5929,21 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5288
5929
  }
5289
5930
  function getPublicTemporaryUrl(remotePath) {
5290
5931
  if (!remotePath) return Promise.reject(new Error("Missing attachment path."));
5291
- return getTemporaryUrlDb(remotePath, ATTACHMENT_URL_EXPIRES_SECONDS);
5932
+ return getTemporaryUrlDb(remotePath, EXPIRED_LINK_REFRESH_EXPIRES_SECONDS, false);
5933
+ }
5934
+ var refreshedLinkExpiryTimer = null;
5935
+ function expireAllRefreshedLinks() {
5936
+ for (var k in refreshedExpiredLinkMap) delete refreshedExpiredLinkMap[k];
5937
+ }
5938
+ function scheduleNextLinkExpiryBoundary() {
5939
+ if (refreshedLinkExpiryTimer) clearTimeout(refreshedLinkExpiryTimer);
5940
+ var now = Date.now();
5941
+ var next = Math.ceil(now / LINK_REFRESH_WINDOW_MS) * LINK_REFRESH_WINDOW_MS;
5942
+ refreshedLinkExpiryTimer = setTimeout(function() {
5943
+ expireAllRefreshedLinks();
5944
+ renderMessages();
5945
+ scheduleNextLinkExpiryBoundary();
5946
+ }, Math.max(1, next - now));
5292
5947
  }
5293
5948
  function fetchFreshHrefForExpiredLink(expiredHref, remotePath) {
5294
5949
  var cached = refreshedExpiredLinkMap[expiredHref];
@@ -5301,6 +5956,7 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5301
5956
  if (!resolved) return Promise.reject(new Error("Unable to refresh this expired attachment link."));
5302
5957
  return getPublicTemporaryUrl(resolved).then(function(fresh) {
5303
5958
  refreshedExpiredLinkMap[expiredHref] = fresh;
5959
+ scheduleNextLinkExpiryBoundary();
5304
5960
  return fresh;
5305
5961
  });
5306
5962
  })().then(
@@ -5359,15 +6015,77 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5359
6015
  if (key) lsSet(key, String(ts));
5360
6016
  }
5361
6017
  function fetchOlderHistoryIfNeeded() {
5362
- if (CS.loadingHistory || CS.loadingOlderHistory || CS.historyEndOfList) return;
5363
- var prevH = CS.messagesBox ? CS.messagesBox.scrollHeight : 0;
5364
- var prevT = CS.messagesBox ? CS.messagesBox.scrollTop : 0;
5365
- session.loadHistory(true).then(function() {
5366
- if (!CS.messagesBox) return;
5367
- raf2().then(function() {
5368
- if (!CS.messagesBox) return;
5369
- CS.messagesBox.scrollTop = prevT + (CS.messagesBox.scrollHeight - prevH);
5370
- });
6018
+ if (CS.historyEndOfList) return Promise.resolve(true);
6019
+ if (CS.loadingHistory || CS.loadingOlderHistory) return Promise.resolve(false);
6020
+ return session.loadHistory(true).then(function() {
6021
+ return true;
6022
+ });
6023
+ }
6024
+ function messagesBoxCanScroll() {
6025
+ if (!CS.messagesBox || CS.chatSettingsOpen) return true;
6026
+ return CS.messagesBox.scrollHeight - CS.messagesBox.clientHeight > HISTORY_FILL_SLACK_PX;
6027
+ }
6028
+ function topVisibleRowKey() {
6029
+ var box = CS.messagesBox;
6030
+ if (!box) return null;
6031
+ var boxTop = box.getBoundingClientRect().top;
6032
+ var kids = box.children;
6033
+ for (var i = 0; i < kids.length; i++) {
6034
+ var key = kids[i].getAttribute && kids[i].getAttribute("data-row-key");
6035
+ if (!key) continue;
6036
+ if (kids[i].getBoundingClientRect().top - boxTop + kids[i].offsetHeight > 0) return key;
6037
+ }
6038
+ return null;
6039
+ }
6040
+ function contentAboveRow(key) {
6041
+ var box = CS.messagesBox;
6042
+ if (!box) return null;
6043
+ var boxTop = box.getBoundingClientRect().top;
6044
+ var kids = box.children;
6045
+ for (var i = 0; i < kids.length; i++) {
6046
+ if (!kids[i].getAttribute || kids[i].getAttribute("data-row-key") !== key) continue;
6047
+ return kids[i].getBoundingClientRect().top - boxTop + box.scrollTop;
6048
+ }
6049
+ return null;
6050
+ }
6051
+ var _historyFillToken = 0;
6052
+ var _historyFiller = createHistoryFiller({
6053
+ isEndOfList: function() {
6054
+ return !!CS.historyEndOfList;
6055
+ },
6056
+ isLoading: function() {
6057
+ return !!(CS.loadingHistory || CS.loadingOlderHistory);
6058
+ },
6059
+ // The settings panel occupies the messages box and suppresses
6060
+ // renderMessages, so a fill started before it opened must stop.
6061
+ isStale: function() {
6062
+ return _historyFillToken !== CS.gateRefreshToken || !CS.messagesBox || CS.chatSettingsOpen;
6063
+ },
6064
+ messageCount: function() {
6065
+ return CS.messages.length;
6066
+ },
6067
+ fetchOlder: function() {
6068
+ return fetchOlderHistoryIfNeeded();
6069
+ }
6070
+ });
6071
+ function pageOlderHistoryUntil(isSatisfied, token) {
6072
+ if (token === void 0) token = CS.gateRefreshToken;
6073
+ if (token !== CS.gateRefreshToken) return Promise.resolve();
6074
+ _historyFillToken = token;
6075
+ return _historyFiller.fill(function() {
6076
+ return raf2().then(isSatisfied);
6077
+ });
6078
+ }
6079
+ function ensureHistoryFillsViewport(token) {
6080
+ return pageOlderHistoryUntil(messagesBoxCanScroll, token);
6081
+ }
6082
+ function pageOlderHistoryUntilTaller() {
6083
+ var anchorKey = topVisibleRowKey();
6084
+ var before = anchorKey ? contentAboveRow(anchorKey) : null;
6085
+ return pageOlderHistoryUntil(function() {
6086
+ if (!CS.messagesBox || !anchorKey || before === null) return true;
6087
+ var now = contentAboveRow(anchorKey);
6088
+ return now === null || now > before + HISTORY_FILL_SLACK_PX;
5371
6089
  });
5372
6090
  }
5373
6091
  function openClearHistoryModal() {
@@ -5461,6 +6179,102 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5461
6179
  }
5462
6180
  return h("div", { class: cls.join(" "), dataset: { msgIndex: String(idx) } }, bubble);
5463
6181
  }
6182
+ var INDEX_ICON_ACTIVE = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M20.5 12a8.5 8.5 0 0 1-8.5 8.5 8.5 8.5 0 0 1-7.6-4.7"/><path d="M3.5 12A8.5 8.5 0 0 1 12 3.5a8.5 8.5 0 0 1 7.6 4.7"/><path d="M20 3.6v4.8h-4.8"/><path d="M4 20.4v-4.8h4.8"/></svg>';
6183
+ var INDEX_ICON_DONE = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M4 12.5l5 5L20 6.5"/></svg>';
6184
+ var INDEX_ICON_ERROR = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"/><path d="M12 7.5v5.5"/><path d="M12 16.6h.01"/></svg>';
6185
+ var INDEX_ICON_CANCELLED = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"/><path d="M8.2 8.2l7.6 7.6"/></svg>';
6186
+ function indexGroupIcon(group) {
6187
+ if (group.status === "active") return INDEX_ICON_ACTIVE;
6188
+ if (group.status === "error") return INDEX_ICON_ERROR;
6189
+ if (group.status === "cancelled") return INDEX_ICON_CANCELLED;
6190
+ return INDEX_ICON_DONE;
6191
+ }
6192
+ function indexGroupVerb(group) {
6193
+ if (group.status === "active") return group.isReindex ? "Reindexing" : "Indexing";
6194
+ if (group.status === "error") return "Indexing failed:";
6195
+ if (group.status === "cancelled") return "Indexing cancelled:";
6196
+ return group.isReindex ? "Reindexed" : "Indexed";
6197
+ }
6198
+ function indexGroupLabel(group) {
6199
+ var nameLabel = group.path ? "[" + group.name + "](" + group.path + ")" : group.name;
6200
+ return indexGroupVerb(group) + " " + nameLabel;
6201
+ }
6202
+ function indexGroupCount(group) {
6203
+ if (group.passCount <= 1 && !group.mayHaveOlder) return "";
6204
+ return group.passCount + (group.mayHaveOlder ? "+" : "") + " passes";
6205
+ }
6206
+ function toggleIndexGroup(key) {
6207
+ if (CS.indexGroupsOpen[key]) delete CS.indexGroupsOpen[key];
6208
+ else CS.indexGroupsOpen[key] = true;
6209
+ renderMessages();
6210
+ ensureHistoryFillsViewport();
6211
+ }
6212
+ function buildIndexGroupEl(group, isOpen) {
6213
+ var cls = ["bq-index-group"];
6214
+ if (group.status === "active") cls.push("is-active");
6215
+ if (group.status === "error") cls.push("is-error");
6216
+ if (isOpen) cls.push("is-open");
6217
+ var label = h("span", { class: "bq-index-label", html: parseMsgPartsHtml(indexGroupLabel(group)) });
6218
+ label.addEventListener("click", function(e) {
6219
+ if (e.target && e.target.closest && e.target.closest("a")) e.stopPropagation();
6220
+ onBubbleLinkClick(e);
6221
+ });
6222
+ var cancelBtn = null;
6223
+ if (group.cancellableIds.length || group.cancelling) {
6224
+ cancelBtn = h("button", {
6225
+ class: "bq-index-cancel" + (group.cancelling ? " is-disabled" : ""),
6226
+ type: "button",
6227
+ title: group.cancelling ? "Stopping..." : "Stop indexing this file",
6228
+ "aria-label": "Stop indexing " + group.name,
6229
+ text: group.cancelling ? "Stopping..." : "Stop"
6230
+ });
6231
+ if (group.cancelling) cancelBtn.disabled = true;
6232
+ else cancelBtn.addEventListener("click", function(e) {
6233
+ e.stopPropagation();
6234
+ session.cancelIndexingGroup(group);
6235
+ });
6236
+ cancelBtn.addEventListener("keydown", function(e) {
6237
+ e.stopPropagation();
6238
+ });
6239
+ }
6240
+ var head = h(
6241
+ "div",
6242
+ {
6243
+ class: "bq-index-head",
6244
+ role: "button",
6245
+ tabindex: "0",
6246
+ "aria-expanded": isOpen ? "true" : "false",
6247
+ title: isOpen ? "Hide indexing steps" : "Show indexing steps",
6248
+ onclick: function() {
6249
+ toggleIndexGroup(group.runKey);
6250
+ },
6251
+ onkeydown: function(e) {
6252
+ if (e.key !== "Enter" && e.key !== " " && e.key !== "Spacebar") return;
6253
+ e.preventDefault();
6254
+ toggleIndexGroup(group.runKey);
6255
+ }
6256
+ },
6257
+ h("span", { class: "bq-index-icon", html: indexGroupIcon(group) }),
6258
+ label,
6259
+ indexGroupCount(group) ? h("span", { class: "bq-index-count", text: indexGroupCount(group) }) : null,
6260
+ cancelBtn,
6261
+ h("span", { class: "bq-index-chevron", text: "\u25B6" })
6262
+ );
6263
+ var el = h("div", { class: cls.join(" ") }, head);
6264
+ if (group.cancelError) {
6265
+ el.appendChild(h("div", {
6266
+ class: "bq-index-note is-error",
6267
+ text: "Could not stop this file: " + group.cancelError
6268
+ }));
6269
+ }
6270
+ if (isOpen && group.mayHaveOlder) {
6271
+ el.appendChild(h("div", {
6272
+ class: "bq-index-note",
6273
+ text: "Earlier passes of this file are further back in the conversation. Scroll up to load them."
6274
+ }));
6275
+ }
6276
+ return el;
6277
+ }
5464
6278
  function historyLoadingEl(initial) {
5465
6279
  if (initial) {
5466
6280
  return h(
@@ -5476,9 +6290,54 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5476
6290
  h("span", { class: "bq-loader" })
5477
6291
  );
5478
6292
  }
6293
+ function rowAnchorKey(msg, index) {
6294
+ if (!msg) return null;
6295
+ var id = msg._serverItemId || msg._localId;
6296
+ return id ? "s" + id + ":" + msg.role : "i" + index;
6297
+ }
6298
+ function indexGroupAnchorId(group) {
6299
+ var last = group.members[group.members.length - 1];
6300
+ return last && last.msg && last.msg._serverItemId || "";
6301
+ }
6302
+ function captureScrollAnchor() {
6303
+ var box = CS.messagesBox;
6304
+ if (!box || CS.stickToBottom) return null;
6305
+ var boxTop = box.getBoundingClientRect().top;
6306
+ var kids = box.children;
6307
+ var fallback = null;
6308
+ for (var i = 0; i < kids.length; i++) {
6309
+ if (!kids[i].getAttribute) continue;
6310
+ var key = kids[i].getAttribute("data-row-key");
6311
+ if (!key) continue;
6312
+ var top = kids[i].getBoundingClientRect().top - boxTop;
6313
+ if (top + kids[i].offsetHeight <= 0) continue;
6314
+ var cand = { key, top, scrollTop: box.scrollTop, pos: kids[i].getAttribute("data-row-pos") };
6315
+ if (cand.pos === null) return cand;
6316
+ if (!fallback) fallback = cand;
6317
+ }
6318
+ return fallback || { key: null, top: 0, scrollTop: box.scrollTop };
6319
+ }
6320
+ function restoreScrollAnchor(anchor) {
6321
+ var box = CS.messagesBox;
6322
+ if (!box) return;
6323
+ if (!anchor || CS.stickToBottom) {
6324
+ box.scrollTop = box.scrollHeight;
6325
+ return;
6326
+ }
6327
+ var boxTop = box.getBoundingClientRect().top;
6328
+ var kids = box.children;
6329
+ for (var i = 0; anchor.key && i < kids.length; i++) {
6330
+ if (!kids[i].getAttribute || kids[i].getAttribute("data-row-key") !== anchor.key) continue;
6331
+ if (anchor.pos && kids[i].getAttribute("data-row-pos") !== anchor.pos) break;
6332
+ box.scrollTop += kids[i].getBoundingClientRect().top - boxTop - anchor.top;
6333
+ return;
6334
+ }
6335
+ box.scrollTop = anchor.scrollTop;
6336
+ }
5479
6337
  function renderMessages() {
5480
6338
  if (!CS.messagesBox) return;
5481
6339
  if (CS.chatSettingsOpen) return;
6340
+ var anchor = captureScrollAnchor();
5482
6341
  clear(CS.messagesBox);
5483
6342
  CS.messageEls = [];
5484
6343
  if (CS.loadingOlderHistory) CS.messagesBox.appendChild(historyLoadingEl(false));
@@ -5499,23 +6358,44 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5499
6358
  CS.messagesBox.appendChild(greet);
5500
6359
  return;
5501
6360
  }
5502
- CS.messages.forEach(function(msg, idx) {
5503
- var el = buildMessageEl(msg, idx);
5504
- CS.messageEls.push(el);
6361
+ var rows = buildChatDisplayList(CS.messages, { hasMoreHistory: !CS.historyEndOfList });
6362
+ rows.forEach(function(row) {
6363
+ if (row.kind === "indexing") {
6364
+ var isOpen = !!CS.indexGroupsOpen[row.group.runKey];
6365
+ var groupEl = buildIndexGroupEl(row.group, isOpen);
6366
+ groupEl.setAttribute("data-row-key", "g" + row.group.runKey);
6367
+ groupEl.setAttribute("data-row-pos", indexGroupAnchorId(row.group));
6368
+ CS.messagesBox.appendChild(groupEl);
6369
+ if (!isOpen) return;
6370
+ row.group.members.forEach(function(member) {
6371
+ var pass = buildMessageEl(member.msg, member.index);
6372
+ pass.classList.add("bq-index-pass");
6373
+ pass.setAttribute("data-row-key", rowAnchorKey(member.msg, member.index));
6374
+ CS.messageEls[member.index] = pass;
6375
+ CS.messagesBox.appendChild(pass);
6376
+ });
6377
+ return;
6378
+ }
6379
+ var el = buildMessageEl(row.msg, row.index);
6380
+ el.setAttribute("data-row-key", rowAnchorKey(row.msg, row.index));
6381
+ CS.messageEls[row.index] = el;
5505
6382
  CS.messagesBox.appendChild(el);
5506
6383
  });
6384
+ restoreScrollAnchor(anchor);
5507
6385
  }
5508
6386
  function refreshMessageBubble(idx) {
5509
6387
  if (idx < 0 || idx >= CS.messages.length) return;
5510
6388
  var oldEl = CS.messageEls[idx];
5511
6389
  if (!oldEl || !oldEl.parentNode) return;
5512
6390
  var newEl = buildMessageEl(CS.messages[idx], idx);
6391
+ if (oldEl.classList.contains("bq-index-pass")) newEl.classList.add("bq-index-pass");
5513
6392
  oldEl.parentNode.replaceChild(newEl, oldEl);
5514
6393
  CS.messageEls[idx] = newEl;
5515
6394
  }
5516
6395
  function renderChat() {
5517
6396
  CS.messages = [];
5518
6397
  CS.messageEls = [];
6398
+ CS.indexGroupsOpen = {};
5519
6399
  CS.sending = false;
5520
6400
  CS.typing = false;
5521
6401
  CS.typingAbort = true;
@@ -5933,6 +6813,8 @@ Index the REMAINING windows - one record per row/item, looking at any page image
5933
6813
  S._resizeBound = true;
5934
6814
  window.addEventListener("resize", function() {
5935
6815
  scheduleAttachmentOverflowRecompute();
6816
+ scrollToBottomIfSticky(false);
6817
+ ensureHistoryFillsViewport();
5936
6818
  });
5937
6819
  }
5938
6820
  if (!S._visBound && typeof document !== "undefined" && document.addEventListener) {