react-instantsearch 7.32.2 → 7.33.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.
@@ -1,4 +1,4 @@
1
- /*! React InstantSearch 7.32.2 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
1
+ /*! React InstantSearch 7.33.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
4
4
  typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
@@ -24,7 +24,7 @@
24
24
 
25
25
  var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
26
26
 
27
- var version$2 = '7.32.2';
27
+ var version$2 = '7.33.0';
28
28
 
29
29
  function _define_property(obj, key, value) {
30
30
  if (key in obj) {
@@ -10821,7 +10821,7 @@
10821
10821
  };
10822
10822
  }
10823
10823
 
10824
- var version = '4.96.2';
10824
+ var version = '4.98.0';
10825
10825
 
10826
10826
  var withUsage$q = createDocumentationMessageGenerator({
10827
10827
  name: 'instantsearch'
@@ -12536,6 +12536,12 @@
12536
12536
  }
12537
12537
  });
12538
12538
  /**
12539
+ * Regenerate the chat id. Use this to start a fresh conversation on the
12540
+ * server while keeping the same Chat instance and its registered listeners.
12541
+ */ _define_property(this, "regenerateId", function() {
12542
+ _this.id = _this.generateId();
12543
+ });
12544
+ /**
12539
12545
  * Add a tool result for a tool call.
12540
12546
  */ _define_property(this, "addToolResult", function(param) {
12541
12547
  var tool = param.tool, toolCallId = param.toolCallId, output = param.output;
@@ -13412,6 +13418,41 @@
13412
13418
  return Chat;
13413
13419
  }(AbstractChat);
13414
13420
 
13421
+ // Centralizes the "open the chat from an entry point" behavior shared by the
13422
+ // SearchBox AI button, the Autocomplete AI button, prompt suggestions, and any
13423
+ // future entry point. The chat is always opened; the message is only sent when
13424
+ // it is non-empty and the chat is not already processing a message.
13425
+ // Returns true when a message was submitted, so callers can clear their input.
13426
+ function openChat(chatRenderState) {
13427
+ var _ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, message = _ref.message, referer = _ref.referer;
13428
+ var _ref1;
13429
+ var _chatRenderState_setOpen;
13430
+ if (!chatRenderState) {
13431
+ return false;
13432
+ }
13433
+ (_chatRenderState_setOpen = chatRenderState.setOpen) === null || _chatRenderState_setOpen === void 0 ? void 0 : _chatRenderState_setOpen.call(chatRenderState, true);
13434
+ var trimmed = (_ref1 = message === null || message === void 0 ? void 0 : message.trim()) !== null && _ref1 !== void 0 ? _ref1 : '';
13435
+ if (!trimmed) {
13436
+ var _chatRenderState_focusInput;
13437
+ (_chatRenderState_focusInput = chatRenderState.focusInput) === null || _chatRenderState_focusInput === void 0 ? void 0 : _chatRenderState_focusInput.call(chatRenderState);
13438
+ return false;
13439
+ }
13440
+ if (isChatBusy(chatRenderState) || !chatRenderState.sendMessage) {
13441
+ return false;
13442
+ }
13443
+ chatRenderState.sendMessage({
13444
+ text: trimmed
13445
+ }, referer ? {
13446
+ headers: {
13447
+ 'x-algolia-referer': referer
13448
+ }
13449
+ } : undefined);
13450
+ return true;
13451
+ }
13452
+ function isChatBusy(chatRenderState) {
13453
+ return (chatRenderState === null || chatRenderState === void 0 ? void 0 : chatRenderState.status) === 'submitted' || (chatRenderState === null || chatRenderState === void 0 ? void 0 : chatRenderState.status) === 'streaming';
13454
+ }
13455
+
13415
13456
  var SearchIndexToolType$1 = 'algolia_search_index';
13416
13457
  var RecommendToolType = 'algolia_recommend';
13417
13458
  var MemorizeToolType = 'algolia_memorize';
@@ -13650,12 +13691,13 @@
13650
13691
  var unmountFn = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : noop;
13651
13692
  checkRendering(renderFn, withUsage$n());
13652
13693
  return function(widgetParams) {
13653
- var _ref = widgetParams || {}, _ref_resume = _ref.resume, resume = _ref_resume === void 0 ? false : _ref_resume, _ref_tools = _ref.tools, tools = _ref_tools === void 0 ? {} : _ref_tools, _ref_type = _ref.type, type = _ref_type === void 0 ? 'chat' : _ref_type, context = _ref.context, initialUserMessage = _ref.initialUserMessage, options = _object_without_properties(_ref, [
13694
+ var _ref = widgetParams || {}, _ref_resume = _ref.resume, resume = _ref_resume === void 0 ? false : _ref_resume, _ref_tools = _ref.tools, tools = _ref_tools === void 0 ? {} : _ref_tools, _ref_type = _ref.type, type = _ref_type === void 0 ? 'chat' : _ref_type, context = _ref.context, initialUserMessage = _ref.initialUserMessage, initialMessages = _ref.initialMessages, options = _object_without_properties(_ref, [
13654
13695
  "resume",
13655
13696
  "tools",
13656
13697
  "type",
13657
13698
  "context",
13658
- "initialUserMessage"
13699
+ "initialUserMessage",
13700
+ "initialMessages"
13659
13701
  ]);
13660
13702
  var _chatInstance;
13661
13703
  var input = '';
@@ -13706,12 +13748,14 @@
13706
13748
  var onClearTransitionEnd = function onClearTransitionEnd() {
13707
13749
  setMessages([]);
13708
13750
  _chatInstance.clearError();
13751
+ _chatInstance.regenerateId();
13709
13752
  feedbackState = {};
13710
13753
  setIsClearing(false);
13711
13754
  };
13712
13755
  var makeChatInstance = function makeChatInstance(instantSearchInstance) {
13713
13756
  var transport;
13714
- var _getAppIdAndApiKey = _sliced_to_array(getAppIdAndApiKey(instantSearchInstance.client), 2), appId = _getAppIdAndApiKey[0], apiKey = _getAppIdAndApiKey[1];
13757
+ var client = instantSearchInstance.client;
13758
+ var _getAppIdAndApiKey = _sliced_to_array(getAppIdAndApiKey(client), 2), appId = _getAppIdAndApiKey[0], apiKey = _getAppIdAndApiKey[1];
13715
13759
  // Filter out custom data parts (like data-suggestions) that the backend doesn't accept
13716
13760
  var filterDataParts = function filterDataParts(messages) {
13717
13761
  return messages.map(function(message) {
@@ -13728,9 +13772,16 @@
13728
13772
  transport = new DefaultChatTransport(_object_spread_props(_object_spread({}, options.transport), {
13729
13773
  prepareSendMessagesRequest: function prepareSendMessagesRequest(params) {
13730
13774
  // Call the original prepareSendMessagesRequest if it exists,
13731
- // otherwise construct the default body
13775
+ // otherwise construct a minimal default body containing only the
13776
+ // request payload — without leaking transport metadata such as
13777
+ // resolved headers, api URL, credentials, or `requestMetadata`.
13732
13778
  var preparedOrPromise = originalPrepare ? originalPrepare(params) : {
13733
- body: _object_spread({}, params)
13779
+ body: _object_spread({
13780
+ id: params.id,
13781
+ messageId: params.messageId,
13782
+ trigger: params.trigger,
13783
+ messages: params.messages
13784
+ }, params.body)
13734
13785
  };
13735
13786
  // Then filter out data-* parts
13736
13787
  var applyFilter = function applyFilter(prepared) {
@@ -13757,20 +13808,19 @@
13757
13808
  api: baseApi,
13758
13809
  headers: {
13759
13810
  'x-algolia-application-id': appId,
13760
- 'x-algolia-api-Key': apiKey,
13761
- 'x-algolia-agent': getAlgoliaAgent(instantSearchInstance.client)
13811
+ 'x-algolia-api-key': apiKey,
13812
+ 'x-algolia-agent': "".concat(getAlgoliaAgent(client), "; chat")
13762
13813
  },
13763
- prepareSendMessagesRequest: function prepareSendMessagesRequest(_0) {
13764
- var messages = _0.messages, trigger = _0.trigger, rest = _object_without_properties(_0, [
13765
- "messages",
13766
- "trigger"
13767
- ]);
13814
+ prepareSendMessagesRequest: function prepareSendMessagesRequest(param) {
13815
+ var id = param.id, messages = param.messages, trigger = param.trigger, messageId = param.messageId;
13768
13816
  return {
13769
13817
  // Bypass cache when regenerating to ensure fresh responses
13770
13818
  api: trigger === 'regenerate-message' ? "".concat(baseApi, "&cache=false") : baseApi,
13771
- body: _object_spread_props(_object_spread({}, rest), {
13819
+ body: {
13820
+ id: id,
13821
+ messageId: messageId,
13772
13822
  messages: filterDataParts(messages)
13773
- })
13823
+ }
13774
13824
  };
13775
13825
  }
13776
13826
  });
@@ -13876,13 +13926,19 @@
13876
13926
  });
13877
13927
  };
13878
13928
  }
13929
+ var hasExistingMessages = _chatInstance.messages.length > 0;
13930
+ // Set initialMessages before registering callbacks to avoid
13931
+ // triggering re-renders during init
13932
+ if ((initialMessages === null || initialMessages === void 0 ? void 0 : initialMessages.length) && !resume && !hasExistingMessages) {
13933
+ _chatInstance.messages = initialMessages;
13934
+ }
13879
13935
  _chatInstance['~registerErrorCallback'](render);
13880
13936
  _chatInstance['~registerMessagesCallback'](render);
13881
13937
  _chatInstance['~registerStatusCallback'](render);
13882
13938
  if (resume) {
13883
13939
  _chatInstance.resumeStream();
13884
13940
  }
13885
- if (initialUserMessage && !resume && _chatInstance.messages.length === 0) {
13941
+ if (initialUserMessage && !resume && !hasExistingMessages) {
13886
13942
  _chatInstance.sendMessage({
13887
13943
  text: initialUserMessage
13888
13944
  });
@@ -17558,7 +17614,9 @@
17558
17614
  name: 'filter-suggestions',
17559
17615
  connector: true
17560
17616
  });
17561
- var connectFilterSuggestions = function connectFilterSuggestions(renderFn) {
17617
+ /**
17618
+ * @deprecated Filter suggestions are deprecated and will be removed in a future major version.
17619
+ */ var connectFilterSuggestions = function connectFilterSuggestions(renderFn) {
17562
17620
  var unmountFn = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : noop;
17563
17621
  checkRendering(renderFn, withUsage());
17564
17622
  return function(widgetParams) {
@@ -17770,7 +17828,9 @@
17770
17828
  };
17771
17829
  };
17772
17830
 
17773
- function useFilterSuggestions(props, additionalWidgetProperties) {
17831
+ /**
17832
+ * @deprecated Filter suggestions are deprecated and will be removed in a future major version.
17833
+ */ function useFilterSuggestions(props, additionalWidgetProperties) {
17774
17834
  return useConnector(connectFilterSuggestions, props, additionalWidgetProperties);
17775
17835
  }
17776
17836
 
@@ -18797,7 +18857,7 @@
18797
18857
  function createAutocompleteSearchComponent(param) {
18798
18858
  var createElement = param.createElement;
18799
18859
  return function AutocompleteSearch(userProps) {
18800
- var inputProps = userProps.inputProps, onClear = userProps.onClear, query = userProps.query, isSearchStalled = userProps.isSearchStalled, onCancel = userProps.onCancel, isDetached = userProps.isDetached, submitTitle = userProps.submitTitle, onAiModeClick = userProps.onAiModeClick, _userProps_classNames = userProps.classNames, classNames = _userProps_classNames === void 0 ? {} : _userProps_classNames;
18860
+ var inputProps = userProps.inputProps, onClear = userProps.onClear, query = userProps.query, isSearchStalled = userProps.isSearchStalled, onCancel = userProps.onCancel, isDetached = userProps.isDetached, submitTitle = userProps.submitTitle, onAiModeClick = userProps.onAiModeClick, _userProps_aiModeButtonDisabled = userProps.aiModeButtonDisabled, aiModeButtonDisabled = _userProps_aiModeButtonDisabled === void 0 ? false : _userProps_aiModeButtonDisabled, _userProps_classNames = userProps.classNames, classNames = _userProps_classNames === void 0 ? {} : _userProps_classNames;
18801
18861
  var isBackButton = Boolean(isDetached && onCancel);
18802
18862
  var resolvedCancelTitle = submitTitle !== null && submitTitle !== void 0 ? submitTitle : 'Close';
18803
18863
  var inputRef = inputProps.ref;
@@ -18874,6 +18934,7 @@
18874
18934
  className: cx('ais-AiModeButton', classNames.aiModeButton),
18875
18935
  type: "button",
18876
18936
  title: "AI Mode",
18937
+ disabled: aiModeButtonDisabled,
18877
18938
  onClick: function onClick(e) {
18878
18939
  e.preventDefault();
18879
18940
  onAiModeClick();
@@ -21689,7 +21750,7 @@
21689
21750
  Fragment: React.Fragment
21690
21751
  });
21691
21752
  function AutocompleteSearch(param) {
21692
- var inputProps = param.inputProps, clearQuery = param.clearQuery, onQueryChange = param.onQueryChange, query = param.query, isSearchStalled = param.isSearchStalled, onCancel = param.onCancel, isDetached = param.isDetached, submitTitle = param.submitTitle, onAiModeClick = param.onAiModeClick, classNames = param.classNames;
21753
+ var inputProps = param.inputProps, clearQuery = param.clearQuery, onQueryChange = param.onQueryChange, query = param.query, isSearchStalled = param.isSearchStalled, onCancel = param.onCancel, isDetached = param.isDetached, submitTitle = param.submitTitle, onAiModeClick = param.onAiModeClick, aiModeButtonDisabled = param.aiModeButtonDisabled, classNames = param.classNames;
21693
21754
  return /*#__PURE__*/ React.createElement(AutocompleteSearchComponent, {
21694
21755
  inputProps: _object_spread_props(_object_spread({}, inputProps), {
21695
21756
  onChange: function onChange(event) {
@@ -21704,6 +21765,7 @@
21704
21765
  isDetached: isDetached,
21705
21766
  submitTitle: submitTitle,
21706
21767
  onAiModeClick: onAiModeClick,
21768
+ aiModeButtonDisabled: aiModeButtonDisabled,
21707
21769
  classNames: classNames
21708
21770
  });
21709
21771
  }
@@ -22200,14 +22262,13 @@
22200
22262
  onSelect: userOnSelect !== null && userOnSelect !== void 0 ? userOnSelect : function(param) {
22201
22263
  var item = param.item, query = param.query, setQuery = param.setQuery, url = param.url;
22202
22264
  if (isPromptSuggestion(item)) {
22203
- var chatRenderStateWithFocus = chatRenderState;
22204
- if (chatRenderStateWithFocus) {
22205
- var _chatRenderStateWithFocus_setOpen, _chatRenderStateWithFocus_focusInput, _chatRenderStateWithFocus_sendMessage;
22206
- (_chatRenderStateWithFocus_setOpen = chatRenderStateWithFocus.setOpen) === null || _chatRenderStateWithFocus_setOpen === void 0 ? void 0 : _chatRenderStateWithFocus_setOpen.call(chatRenderStateWithFocus, true);
22207
- (_chatRenderStateWithFocus_focusInput = chatRenderStateWithFocus.focusInput) === null || _chatRenderStateWithFocus_focusInput === void 0 ? void 0 : _chatRenderStateWithFocus_focusInput.call(chatRenderStateWithFocus);
22208
- (_chatRenderStateWithFocus_sendMessage = chatRenderStateWithFocus.sendMessage) === null || _chatRenderStateWithFocus_sendMessage === void 0 ? void 0 : _chatRenderStateWithFocus_sendMessage.call(chatRenderStateWithFocus, {
22209
- text: item.prompt
22210
- });
22265
+ if (chatRenderState) {
22266
+ if (openChat(chatRenderState, {
22267
+ message: item.prompt,
22268
+ referer: 'prompt-suggestions'
22269
+ })) {
22270
+ setQuery('');
22271
+ }
22211
22272
  return;
22212
22273
  }
22213
22274
  if ((showPromptSuggestions === null || showPromptSuggestions === void 0 ? void 0 : showPromptSuggestions.indexName) && !hasWarnedMissingPromptSuggestionsChatRef.current) {
@@ -22347,17 +22408,15 @@
22347
22408
  if (isDetached) {
22348
22409
  setIsModalOpen(false);
22349
22410
  }
22350
- if (chatRenderState) {
22351
- var _chatRenderState_setOpen;
22352
- (_chatRenderState_setOpen = chatRenderState.setOpen) === null || _chatRenderState_setOpen === void 0 ? void 0 : _chatRenderState_setOpen.call(chatRenderState, true);
22353
- if (resolvedQuery.trim()) {
22354
- var _chatRenderState_sendMessage;
22355
- (_chatRenderState_sendMessage = chatRenderState.sendMessage) === null || _chatRenderState_sendMessage === void 0 ? void 0 : _chatRenderState_sendMessage.call(chatRenderState, {
22356
- text: resolvedQuery
22357
- });
22358
- }
22411
+ if (openChat(chatRenderState, {
22412
+ message: resolvedQuery,
22413
+ referer: 'ai-mode'
22414
+ })) {
22415
+ refineSearchBox('');
22416
+ refineAutocomplete('');
22359
22417
  }
22360
22418
  } : undefined,
22419
+ aiModeButtonDisabled: aiMode ? isChatBusy(chatRenderState) : undefined,
22361
22420
  classNames: classNames
22362
22421
  });
22363
22422
  var panelContent = /*#__PURE__*/ React.createElement(AutocompletePanel, _object_spread_props(_object_spread({}, getPanelProps()), {
@@ -24006,7 +24065,7 @@
24006
24065
  }));
24007
24066
  }
24008
24067
  function SearchBox$1(_0) {
24009
- var formRef = _0.formRef, inputRef = _0.inputRef, inputProps = _0.inputProps, isSearchStalled = _0.isSearchStalled, onChange = _0.onChange, onReset = _0.onReset, onSubmit = _0.onSubmit, _0_placeholder = _0.placeholder, placeholder = _0_placeholder === void 0 ? '' : _0_placeholder, value = _0.value, autoFocus = _0.autoFocus, tmp = _0.resetIconComponent, ResetIcon = tmp === void 0 ? DefaultResetIcon : tmp, tmp1 = _0.submitIconComponent, SubmitIcon = tmp1 === void 0 ? DefaultSubmitIcon : tmp1, tmp2 = _0.loadingIconComponent, LoadingIcon = tmp2 === void 0 ? DefaultLoadingIcon : tmp2, tmp3 = _0.aiModeIconComponent, AiModeIcon = tmp3 === void 0 ? DefaultAiModeIcon : tmp3, onAiModeClick = _0.onAiModeClick, _0_classNames = _0.classNames, classNames = _0_classNames === void 0 ? {} : _0_classNames, translations = _0.translations, props = _object_without_properties(_0, [
24068
+ var formRef = _0.formRef, inputRef = _0.inputRef, inputProps = _0.inputProps, isSearchStalled = _0.isSearchStalled, onChange = _0.onChange, onReset = _0.onReset, onSubmit = _0.onSubmit, _0_placeholder = _0.placeholder, placeholder = _0_placeholder === void 0 ? '' : _0_placeholder, value = _0.value, autoFocus = _0.autoFocus, tmp = _0.resetIconComponent, ResetIcon = tmp === void 0 ? DefaultResetIcon : tmp, tmp1 = _0.submitIconComponent, SubmitIcon = tmp1 === void 0 ? DefaultSubmitIcon : tmp1, tmp2 = _0.loadingIconComponent, LoadingIcon = tmp2 === void 0 ? DefaultLoadingIcon : tmp2, tmp3 = _0.aiModeIconComponent, AiModeIcon = tmp3 === void 0 ? DefaultAiModeIcon : tmp3, onAiModeClick = _0.onAiModeClick, _0_aiModeButtonDisabled = _0.aiModeButtonDisabled, aiModeButtonDisabled = _0_aiModeButtonDisabled === void 0 ? false : _0_aiModeButtonDisabled, _0_classNames = _0.classNames, classNames = _0_classNames === void 0 ? {} : _0_classNames, translations = _0.translations, props = _object_without_properties(_0, [
24010
24069
  "formRef",
24011
24070
  "inputRef",
24012
24071
  "inputProps",
@@ -24022,6 +24081,7 @@
24022
24081
  "loadingIconComponent",
24023
24082
  "aiModeIconComponent",
24024
24083
  "onAiModeClick",
24084
+ "aiModeButtonDisabled",
24025
24085
  "classNames",
24026
24086
  "translations"
24027
24087
  ]);
@@ -24085,6 +24145,7 @@
24085
24145
  className: cx('ais-AiModeButton', classNames.aiModeButton),
24086
24146
  type: "button",
24087
24147
  title: translations.aiModeButtonTitle || 'AI Mode',
24148
+ disabled: aiModeButtonDisabled,
24088
24149
  onClick: function onClick(e) {
24089
24150
  e.preventDefault();
24090
24151
  onAiModeClick();
@@ -24303,6 +24364,7 @@
24303
24364
  if (query !== inputValue && document.activeElement !== inputRef.current) {
24304
24365
  setInputValue(query);
24305
24366
  }
24367
+ var chatRenderState = indexRenderState.chat;
24306
24368
  var uiProps = {
24307
24369
  inputRef: inputRef,
24308
24370
  isSearchStalled: isSearchStalled,
@@ -24310,18 +24372,14 @@
24310
24372
  onReset: onReset,
24311
24373
  onSubmit: onSubmit,
24312
24374
  onAiModeClick: aiMode ? function() {
24313
- var chatRenderState = indexRenderState.chat;
24314
- if (chatRenderState) {
24315
- var _chatRenderState_setOpen;
24316
- (_chatRenderState_setOpen = chatRenderState.setOpen) === null || _chatRenderState_setOpen === void 0 ? void 0 : _chatRenderState_setOpen.call(chatRenderState, true);
24317
- if (inputValue.trim()) {
24318
- var _chatRenderState_sendMessage;
24319
- (_chatRenderState_sendMessage = chatRenderState.sendMessage) === null || _chatRenderState_sendMessage === void 0 ? void 0 : _chatRenderState_sendMessage.call(chatRenderState, {
24320
- text: inputValue
24321
- });
24322
- }
24375
+ if (openChat(chatRenderState, {
24376
+ message: inputValue,
24377
+ referer: 'ai-mode'
24378
+ })) {
24379
+ onReset();
24323
24380
  }
24324
24381
  } : undefined,
24382
+ aiModeButtonDisabled: aiMode ? isChatBusy(chatRenderState) : undefined,
24325
24383
  value: inputValue,
24326
24384
  translations: _object_spread({
24327
24385
  submitButtonTitle: 'Submit the search query',
@@ -24639,7 +24697,9 @@
24639
24697
  createElement: React.createElement,
24640
24698
  Fragment: React.Fragment
24641
24699
  });
24642
- function FilterSuggestions(_0) {
24700
+ /**
24701
+ * @deprecated Filter suggestions are deprecated and will be removed in a future major version.
24702
+ */ function FilterSuggestions(_0) {
24643
24703
  var agentId = _0.agentId, attributes = _0.attributes, maxSuggestions = _0.maxSuggestions, debounceMs = _0.debounceMs, hitsToSample = _0.hitsToSample, transformItems = _0.transformItems, itemComponent = _0.itemComponent, headerComponent = _0.headerComponent, emptyComponent = _0.emptyComponent, transport = _0.transport, props = _object_without_properties(_0, [
24644
24704
  "agentId",
24645
24705
  "attributes",