bunnyquery 1.4.3 → 1.4.5

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
@@ -1122,11 +1122,17 @@ var ChatSession = class {
1122
1122
  }
1123
1123
  dispatchAgentRequest(params) {
1124
1124
  var self = this;
1125
+ var dispatchItemId;
1125
1126
  var sendAndPoll = function() {
1126
1127
  return Promise.resolve(
1127
1128
  self._callProviderFor(params.aiPlatform, params.text, params.boundedMessages, params.systemPrompt, params.aiModel, params.userId, params.extractContent)
1128
1129
  ).then(function(initial) {
1129
1130
  if (initial && initial.poll && (initial.status === "pending" || initial.status === "running")) {
1131
+ if (initial.id) {
1132
+ if (dispatchItemId && dispatchItemId !== initial.id) self.historyItemPolls.delete(dispatchItemId);
1133
+ dispatchItemId = initial.id;
1134
+ self.historyItemPolls.set(initial.id, true);
1135
+ }
1130
1136
  return initial.poll({ latency: POLL_INTERVAL });
1131
1137
  }
1132
1138
  return initial;
@@ -1148,13 +1154,39 @@ var ChatSession = class {
1148
1154
  }).catch(function(err) {
1149
1155
  return { content: getErrorMessage(err), isError: true };
1150
1156
  }).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
1157
  delete self.pendingAgentRequests[params.key];
1158
+ if (dispatchItemId) self.historyItemPolls.delete(dispatchItemId);
1159
+ var existing = self.aiChatHistoryCache[params.key] || { messages: [], endOfList: false, startKeyHistory: [] };
1160
+ var reply = { role: "assistant", content: result.content, isError: result.isError };
1161
+ var onThisVisibleChat = self.host.isViewMounted() && self.getHistoryCacheKey() === params.key;
1162
+ if (onThisVisibleChat) {
1163
+ self.aiChatHistoryCache[params.key] = {
1164
+ messages: existing.messages.concat([reply]),
1165
+ endOfList: existing.endOfList,
1166
+ startKeyHistory: existing.startKeyHistory
1167
+ };
1168
+ } else {
1169
+ var msgs = existing.messages.slice();
1170
+ var idx = -1;
1171
+ for (var i = msgs.length - 1; i >= 0; i--) {
1172
+ var m = msgs[i];
1173
+ if (m && m.isPending && m.role === "assistant" && !m.isBackgroundTask) {
1174
+ idx = i;
1175
+ break;
1176
+ }
1177
+ }
1178
+ if (idx !== -1) {
1179
+ reply._serverItemId = msgs[idx]._serverItemId;
1180
+ msgs[idx] = reply;
1181
+ } else {
1182
+ msgs.push(reply);
1183
+ }
1184
+ self.aiChatHistoryCache[params.key] = {
1185
+ messages: msgs,
1186
+ endOfList: existing.endOfList,
1187
+ startKeyHistory: existing.startKeyHistory
1188
+ };
1189
+ }
1158
1190
  return result;
1159
1191
  });
1160
1192
  this.pendingAgentRequests[params.key] = run;
@@ -1207,6 +1239,7 @@ var ChatSession = class {
1207
1239
  self.host.notify();
1208
1240
  }
1209
1241
  if (result && result.poll && (result.status === "pending" || result.status === "running")) {
1242
+ if (serverId) self.historyItemPolls.set(serverId, true);
1210
1243
  return result.poll({ latency: POLL_INTERVAL }).then(function(res) {
1211
1244
  return self.onQueuedSendResponse(capturedComposed, res, capturedPlatform, serverId);
1212
1245
  }).catch(function(err) {
@@ -1244,7 +1277,6 @@ var ChatSession = class {
1244
1277
  serviceId: id.serviceId,
1245
1278
  history: historyForLlm
1246
1279
  });
1247
- var requestToken = this.state.gateRefreshToken;
1248
1280
  var run = this.dispatchAgentRequest({
1249
1281
  key,
1250
1282
  serviceId: id.serviceId,
@@ -1259,7 +1291,7 @@ var ChatSession = class {
1259
1291
  });
1260
1292
  Promise.resolve(run).catch(function() {
1261
1293
  }).then(function() {
1262
- if (requestToken !== self.state.gateRefreshToken || self.getHistoryCacheKey() !== key) return;
1294
+ if (!(self.host.isViewMounted() && self.getHistoryCacheKey() === key)) return;
1263
1295
  self.state.sending = false;
1264
1296
  return Promise.resolve(self.typewriteLatestReply(key)).then(function() {
1265
1297
  self.host.scrollToBottom(true);
@@ -1347,6 +1379,7 @@ var ChatSession = class {
1347
1379
  else this.state.messages.push(msg);
1348
1380
  }
1349
1381
  onQueuedSendResponse(_composed, response, platform, serverId) {
1382
+ if (serverId) this.historyItemPolls.delete(serverId);
1350
1383
  var targetIdx = this.resolveQueuedUserBubble(serverId);
1351
1384
  if (targetIdx === void 0) {
1352
1385
  this.host.notify();
@@ -1381,6 +1414,7 @@ var ChatSession = class {
1381
1414
  this.host.scrollToBottom(true);
1382
1415
  }
1383
1416
  onQueuedSendError(_composed, err, serverId) {
1417
+ if (serverId) this.historyItemPolls.delete(serverId);
1384
1418
  var isNotExists = err && (err.code === "NOT_EXISTS" || err.body && err.body.code === "NOT_EXISTS");
1385
1419
  if (isNotExists) {
1386
1420
  var userIdx = serverId ? this.state.messages.findIndex(function(m) {
@@ -1873,7 +1907,7 @@ var ChatSession = class {
1873
1907
  if (item.status !== "running" && item.status !== "pending") return;
1874
1908
  if (!item.poll || !item.id) return;
1875
1909
  if (self.historyItemPolls.has(item.id)) return;
1876
- if (item.status === "running" && self.pendingAgentRequests[self.getHistoryCacheKey()]) return;
1910
+ if ((item.status === "running" || item.status === "pending") && self.pendingAgentRequests[self.getHistoryCacheKey()]) return;
1877
1911
  self.historyItemPolls.set(item.id, true);
1878
1912
  var capturedId = item.id;
1879
1913
  var pp = item.poll({