bunnyquery 1.4.3 → 1.4.4

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/engine.mjs CHANGED
@@ -1148,13 +1148,38 @@ var ChatSession = class {
1148
1148
  }).catch(function(err) {
1149
1149
  return { content: getErrorMessage(err), isError: true };
1150
1150
  }).then(function(result) {
1151
- var existing = self.aiChatHistoryCache[params.key] || { messages: [], endOfList: false, startKeyHistory: [] };
1152
- self.aiChatHistoryCache[params.key] = {
1153
- messages: existing.messages.concat([{ role: "assistant", content: result.content, isError: result.isError }]),
1154
- endOfList: existing.endOfList,
1155
- startKeyHistory: existing.startKeyHistory
1156
- };
1157
1151
  delete self.pendingAgentRequests[params.key];
1152
+ var existing = self.aiChatHistoryCache[params.key] || { messages: [], endOfList: false, startKeyHistory: [] };
1153
+ var reply = { role: "assistant", content: result.content, isError: result.isError };
1154
+ var onThisVisibleChat = self.host.isViewMounted() && self.getHistoryCacheKey() === params.key;
1155
+ if (onThisVisibleChat) {
1156
+ self.aiChatHistoryCache[params.key] = {
1157
+ messages: existing.messages.concat([reply]),
1158
+ endOfList: existing.endOfList,
1159
+ startKeyHistory: existing.startKeyHistory
1160
+ };
1161
+ } else {
1162
+ var msgs = existing.messages.slice();
1163
+ var idx = -1;
1164
+ for (var i = msgs.length - 1; i >= 0; i--) {
1165
+ var m = msgs[i];
1166
+ if (m && m.isPending && m.role === "assistant" && !m.isBackgroundTask) {
1167
+ idx = i;
1168
+ break;
1169
+ }
1170
+ }
1171
+ if (idx !== -1) {
1172
+ reply._serverItemId = msgs[idx]._serverItemId;
1173
+ msgs[idx] = reply;
1174
+ } else {
1175
+ msgs.push(reply);
1176
+ }
1177
+ self.aiChatHistoryCache[params.key] = {
1178
+ messages: msgs,
1179
+ endOfList: existing.endOfList,
1180
+ startKeyHistory: existing.startKeyHistory
1181
+ };
1182
+ }
1158
1183
  return result;
1159
1184
  });
1160
1185
  this.pendingAgentRequests[params.key] = run;
@@ -1244,7 +1269,6 @@ var ChatSession = class {
1244
1269
  serviceId: id.serviceId,
1245
1270
  history: historyForLlm
1246
1271
  });
1247
- var requestToken = this.state.gateRefreshToken;
1248
1272
  var run = this.dispatchAgentRequest({
1249
1273
  key,
1250
1274
  serviceId: id.serviceId,
@@ -1259,7 +1283,7 @@ var ChatSession = class {
1259
1283
  });
1260
1284
  Promise.resolve(run).catch(function() {
1261
1285
  }).then(function() {
1262
- if (requestToken !== self.state.gateRefreshToken || self.getHistoryCacheKey() !== key) return;
1286
+ if (!(self.host.isViewMounted() && self.getHistoryCacheKey() === key)) return;
1263
1287
  self.state.sending = false;
1264
1288
  return Promise.resolve(self.typewriteLatestReply(key)).then(function() {
1265
1289
  self.host.scrollToBottom(true);
@@ -1873,7 +1897,7 @@ var ChatSession = class {
1873
1897
  if (item.status !== "running" && item.status !== "pending") return;
1874
1898
  if (!item.poll || !item.id) return;
1875
1899
  if (self.historyItemPolls.has(item.id)) return;
1876
- if (item.status === "running" && self.pendingAgentRequests[self.getHistoryCacheKey()]) return;
1900
+ if ((item.status === "running" || item.status === "pending") && self.pendingAgentRequests[self.getHistoryCacheKey()]) return;
1877
1901
  self.historyItemPolls.set(item.id, true);
1878
1902
  var capturedId = item.id;
1879
1903
  var pp = item.poll({