@crestapps/ai-chat-ui 2.0.0-preview.225 → 2.0.0-preview.243

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/ai-chat.js CHANGED
@@ -498,6 +498,7 @@
498
498
  isNavigatingAway: false,
499
499
  autoScroll: true,
500
500
  stream: null,
501
+ pageTools: null,
501
502
  messages: [],
502
503
  notifications: [],
503
504
  prompt: "",
@@ -1073,6 +1074,20 @@
1073
1074
  } catch (err) {
1074
1075
  console.error("SignalR Connection Error: ", err);
1075
1076
  }
1077
+ this.ensurePageTools();
1078
+ },
1079
+ ensurePageTools() {
1080
+ if (!this.pageTools && this.connection && window.crestAppsWebMcp && typeof window.crestAppsWebMcp.connectChat === "function") this.pageTools = window.crestAppsWebMcp.connectChat(this.connection);
1081
+ return this.pageTools;
1082
+ },
1083
+ async syncPageTools() {
1084
+ var pageTools = this.ensurePageTools();
1085
+ if (!pageTools) return;
1086
+ try {
1087
+ await pageTools.sync();
1088
+ } catch (err) {
1089
+ console.warn("The page tools could not be sent to the chat.", err);
1090
+ }
1076
1091
  },
1077
1092
  async ensureConnectionStarted() {
1078
1093
  if (!this.connection) return false;
@@ -1328,62 +1343,65 @@
1328
1343
  var lastResponseId = null;
1329
1344
  var messageIndex = this.messages.length;
1330
1345
  var currentSessionId = this.getSessionId();
1331
- this.stream = this.connection.stream("SendMessage", profileId, trimmedPrompt, currentSessionId, sessionProfileId).subscribe({
1332
- next: (chunk) => {
1333
- let message = this.messages[messageIndex];
1334
- if (!message) {
1335
- if (chunk.sessionId && !currentSessionId) this.initializeSession(chunk.sessionId);
1346
+ var openStream = () => {
1347
+ this.stream = this.connection.stream("SendMessage", profileId, trimmedPrompt, currentSessionId, sessionProfileId).subscribe({
1348
+ next: (chunk) => {
1349
+ let message = this.messages[messageIndex];
1350
+ if (!message) {
1351
+ if (chunk.sessionId && !currentSessionId) this.initializeSession(chunk.sessionId);
1352
+ this.hideTypingIndicator();
1353
+ messageIndex = this.messages.length;
1354
+ let newMessage = {
1355
+ id: chunk.messageId,
1356
+ role: "assistant",
1357
+ title: chunk.title,
1358
+ content: "",
1359
+ htmlContent: "",
1360
+ isStreaming: true,
1361
+ userRating: null
1362
+ };
1363
+ this.messages.push(newMessage);
1364
+ message = newMessage;
1365
+ }
1366
+ if (chunk.title && (!message.title || message.title !== chunk.title)) message.title = chunk.title;
1367
+ if (chunk.references && typeof chunk.references === "object" && Object.keys(chunk.references).length) for (const [key, value] of Object.entries(chunk.references)) references[key] = normalizeReference(value) ?? {};
1368
+ if (chunk.content) {
1369
+ if (chunk.responseId && lastResponseId && chunk.responseId !== lastResponseId) content += "\n\n";
1370
+ if (chunk.responseId) lastResponseId = chunk.responseId;
1371
+ content += chunk.content;
1372
+ }
1373
+ message.rawContent = content;
1374
+ updateMessagePresentation(message, references);
1375
+ this.messages[messageIndex] = message;
1376
+ this.$nextTick(() => {
1377
+ renderMessageMedia(message);
1378
+ this.scrollToBottom();
1379
+ });
1380
+ },
1381
+ complete: () => {
1382
+ this.processReferences(references, messageIndex);
1383
+ this.streamingFinished();
1384
+ let msg = this.messages[messageIndex];
1385
+ if (msg) msg.isStreaming = false;
1386
+ if (!msg || !msg.content) this.hideTypingIndicator();
1387
+ if (this.isConversationMode && this.textToSpeechEnabled && msg && msg.content) this.synthesizeSpeech(msg.content);
1388
+ this.stream?.dispose();
1389
+ this.stream = null;
1390
+ },
1391
+ error: (err) => {
1392
+ this.processReferences(references, messageIndex);
1393
+ this.streamingFinished();
1394
+ let msg = this.messages[messageIndex];
1395
+ if (msg) msg.isStreaming = false;
1336
1396
  this.hideTypingIndicator();
1337
- messageIndex = this.messages.length;
1338
- let newMessage = {
1339
- id: chunk.messageId,
1340
- role: "assistant",
1341
- title: chunk.title,
1342
- content: "",
1343
- htmlContent: "",
1344
- isStreaming: true,
1345
- userRating: null
1346
- };
1347
- this.messages.push(newMessage);
1348
- message = newMessage;
1397
+ if (!this.isNavigatingAway) this.addMessage(this.getServiceDownMessage());
1398
+ this.stream?.dispose();
1399
+ this.stream = null;
1400
+ console.error("Stream error:", err);
1349
1401
  }
1350
- if (chunk.title && (!message.title || message.title !== chunk.title)) message.title = chunk.title;
1351
- if (chunk.references && typeof chunk.references === "object" && Object.keys(chunk.references).length) for (const [key, value] of Object.entries(chunk.references)) references[key] = normalizeReference(value) ?? {};
1352
- if (chunk.content) {
1353
- if (chunk.responseId && lastResponseId && chunk.responseId !== lastResponseId) content += "\n\n";
1354
- if (chunk.responseId) lastResponseId = chunk.responseId;
1355
- content += chunk.content;
1356
- }
1357
- message.rawContent = content;
1358
- updateMessagePresentation(message, references);
1359
- this.messages[messageIndex] = message;
1360
- this.$nextTick(() => {
1361
- renderMessageMedia(message);
1362
- this.scrollToBottom();
1363
- });
1364
- },
1365
- complete: () => {
1366
- this.processReferences(references, messageIndex);
1367
- this.streamingFinished();
1368
- let msg = this.messages[messageIndex];
1369
- if (msg) msg.isStreaming = false;
1370
- if (!msg || !msg.content) this.hideTypingIndicator();
1371
- if (this.isConversationMode && this.textToSpeechEnabled && msg && msg.content) this.synthesizeSpeech(msg.content);
1372
- this.stream?.dispose();
1373
- this.stream = null;
1374
- },
1375
- error: (err) => {
1376
- this.processReferences(references, messageIndex);
1377
- this.streamingFinished();
1378
- let msg = this.messages[messageIndex];
1379
- if (msg) msg.isStreaming = false;
1380
- this.hideTypingIndicator();
1381
- if (!this.isNavigatingAway) this.addMessage(this.getServiceDownMessage());
1382
- this.stream?.dispose();
1383
- this.stream = null;
1384
- console.error("Stream error:", err);
1385
- }
1386
- });
1402
+ });
1403
+ };
1404
+ this.syncPageTools().then(openStream, openStream);
1387
1405
  },
1388
1406
  getServiceDownMessage() {
1389
1407
  return {