react-instantsearch-core 7.18.0 → 7.20.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/dist/cjs/connectors/useChat.js +5 -198
- package/dist/cjs/version.js +1 -1
- package/dist/es/connectors/useChat.d.ts +5 -79
- package/dist/es/connectors/useChat.js +4 -198
- package/dist/es/version.d.ts +1 -1
- package/dist/es/version.js +1 -1
- package/dist/umd/ReactInstantSearchCore.js +39 -21
- package/dist/umd/ReactInstantSearchCore.js.map +1 -1
- package/dist/umd/ReactInstantSearchCore.min.js +1 -1
- package/dist/umd/ReactInstantSearchCore.min.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,205 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
6
|
exports.useChat = useChat;
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var _excluded = ["resume", "initialInput", "defaultOpen", "tools"];
|
|
14
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
17
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
18
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
19
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
20
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
21
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
22
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
23
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
24
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
25
|
-
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
26
|
-
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
27
|
-
function useChat() {
|
|
28
|
-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
29
|
-
_ref$resume = _ref.resume,
|
|
30
|
-
resume = _ref$resume === void 0 ? false : _ref$resume,
|
|
31
|
-
_ref$initialInput = _ref.initialInput,
|
|
32
|
-
initialInput = _ref$initialInput === void 0 ? '' : _ref$initialInput,
|
|
33
|
-
_ref$defaultOpen = _ref.defaultOpen,
|
|
34
|
-
defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
|
|
35
|
-
_ref$tools = _ref.tools,
|
|
36
|
-
tools = _ref$tools === void 0 ? {} : _ref$tools,
|
|
37
|
-
options = _objectWithoutProperties(_ref, _excluded);
|
|
38
|
-
process.env.NODE_ENV === 'development' ? (0, _warn.warn)(false, 'Chat is not yet stable and will change in the future.') : void 0;
|
|
39
|
-
process.env.NODE_ENV === 'development' ? (0, _warn.warn)(!('agentId' in options && 'transport' in options), "`useChat` with `agentId` and `transport` can't be used together. The `transport` option will be used.") : void 0;
|
|
40
|
-
var _useAppIdAndApiKey = (0, _useAppIdAndApiKey3.useAppIdAndApiKey)(),
|
|
41
|
-
_useAppIdAndApiKey2 = _slicedToArray(_useAppIdAndApiKey, 2),
|
|
42
|
-
appId = _useAppIdAndApiKey2[0],
|
|
43
|
-
apiKey = _useAppIdAndApiKey2[1];
|
|
44
|
-
var _useState = (0, _react.useState)(initialInput),
|
|
45
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
46
|
-
input = _useState2[0],
|
|
47
|
-
setInput = _useState2[1];
|
|
48
|
-
var _useState3 = (0, _react.useState)(defaultOpen),
|
|
49
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
50
|
-
open = _useState4[0],
|
|
51
|
-
setOpen = _useState4[1];
|
|
52
|
-
var _useState5 = (0, _react.useState)(false),
|
|
53
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
54
|
-
isClearing = _useState6[0],
|
|
55
|
-
setIsClearing = _useState6[1];
|
|
56
|
-
var transport = (0, _react.useMemo)(function () {
|
|
57
|
-
if ('transport' in options && options.transport) {
|
|
58
|
-
return new _ai.DefaultChatTransport(options.transport);
|
|
59
|
-
}
|
|
60
|
-
if ('agentId' in options && options.agentId) {
|
|
61
|
-
var agentId = options.agentId;
|
|
62
|
-
if (!appId || !apiKey) {
|
|
63
|
-
throw new Error('The `useChat` hook requires an `appId` and `apiKey` to be set on the `InstantSearch` component when using the `agentId` option.');
|
|
64
|
-
}
|
|
65
|
-
return new _ai.DefaultChatTransport({
|
|
66
|
-
api: "https://".concat(appId, ".algolia.net/agent-studio/1/agents/").concat(agentId, "/completions?compatibilityMode=ai-sdk-5"),
|
|
67
|
-
headers: {
|
|
68
|
-
'x-algolia-application-id': appId,
|
|
69
|
-
'x-algolia-api-Key': apiKey
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
throw new Error('You need to provide either an `agentId` or a `transport`.');
|
|
74
|
-
}, [apiKey, appId, options]);
|
|
75
|
-
var optionsWithTransport = (0, _react.useMemo)(function () {
|
|
76
|
-
if ('chat' in options) {
|
|
77
|
-
return options;
|
|
78
|
-
}
|
|
79
|
-
return _objectSpread(_objectSpread({}, options), {}, {
|
|
80
|
-
transport: transport
|
|
81
|
-
});
|
|
82
|
-
}, [options, transport]);
|
|
83
|
-
var createChatInstance = (0, _react.useCallback)(function () {
|
|
84
|
-
if ('chat' in optionsWithTransport) {
|
|
85
|
-
return optionsWithTransport.chat;
|
|
86
|
-
}
|
|
87
|
-
var chatInstanceRef = {
|
|
88
|
-
current: null
|
|
89
|
-
};
|
|
90
|
-
var onToolCall = function onToolCall(_ref2) {
|
|
91
|
-
var toolCall = _ref2.toolCall;
|
|
92
|
-
var tool = tools[toolCall.toolName];
|
|
93
|
-
var chatInstance = chatInstanceRef.current;
|
|
94
|
-
if (!tool) {
|
|
95
|
-
if (process.env.NODE_ENV === 'development') {
|
|
96
|
-
throw new Error("No tool implementation found for \"".concat(toolCall.toolName, "\". Please provide a tool implementation in the `tools` prop."));
|
|
97
|
-
}
|
|
98
|
-
chatInstance.addToolResult({
|
|
99
|
-
output: "No tool implemented for \"".concat(toolCall.toolName, "\"."),
|
|
100
|
-
tool: toolCall.toolName,
|
|
101
|
-
toolCallId: toolCall.toolCallId
|
|
102
|
-
});
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (tool.onToolCall) {
|
|
106
|
-
var scopedAddToolResult = function scopedAddToolResult(_ref3) {
|
|
107
|
-
var output = _ref3.output;
|
|
108
|
-
return chatInstance.addToolResult({
|
|
109
|
-
output: output,
|
|
110
|
-
tool: toolCall.toolName,
|
|
111
|
-
toolCallId: toolCall.toolCallId
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
tool.onToolCall(_objectSpread(_objectSpread({}, toolCall), {}, {
|
|
115
|
-
addToolResult: scopedAddToolResult
|
|
116
|
-
}));
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
var chatInstance = new _chat.Chat(_objectSpread(_objectSpread({}, optionsWithTransport), {}, {
|
|
120
|
-
onToolCall: onToolCall
|
|
121
|
-
}));
|
|
122
|
-
chatInstanceRef.current = chatInstance;
|
|
123
|
-
return chatInstance;
|
|
124
|
-
}, [optionsWithTransport, tools]);
|
|
125
|
-
var chatRef = (0, _react.useRef)(createChatInstance());
|
|
126
|
-
var shouldRecreateChat = 'chat' in optionsWithTransport && optionsWithTransport.chat !== chatRef.current || 'id' in optionsWithTransport && chatRef.current.id !== optionsWithTransport.id;
|
|
127
|
-
if (shouldRecreateChat) {
|
|
128
|
-
chatRef.current = createChatInstance();
|
|
129
|
-
}
|
|
130
|
-
var optionsId = 'id' in optionsWithTransport ? optionsWithTransport.id : null;
|
|
131
|
-
var subscribeToMessages = (0, _react.useCallback)(function (update) {
|
|
132
|
-
return chatRef.current['~registerMessagesCallback'](update);
|
|
133
|
-
},
|
|
134
|
-
// optionsId is required to trigger re-subscription when the chat ID changes
|
|
135
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
136
|
-
[optionsId]);
|
|
137
|
-
var messages = (0, _react.useSyncExternalStore)(subscribeToMessages, function () {
|
|
138
|
-
return chatRef.current.messages;
|
|
139
|
-
}, function () {
|
|
140
|
-
return chatRef.current.messages;
|
|
141
|
-
});
|
|
142
|
-
var status = (0, _react.useSyncExternalStore)(chatRef.current['~registerStatusCallback'], function () {
|
|
143
|
-
return chatRef.current.status;
|
|
144
|
-
}, function () {
|
|
145
|
-
return chatRef.current.status;
|
|
146
|
-
});
|
|
147
|
-
var error = (0, _react.useSyncExternalStore)(chatRef.current['~registerErrorCallback'], function () {
|
|
148
|
-
return chatRef.current.error;
|
|
149
|
-
}, function () {
|
|
150
|
-
return chatRef.current.error;
|
|
151
|
-
});
|
|
152
|
-
var setMessages = (0, _react.useCallback)(function (messagesParam) {
|
|
153
|
-
if (typeof messagesParam === 'function') {
|
|
154
|
-
messagesParam = messagesParam(chatRef.current.messages);
|
|
155
|
-
}
|
|
156
|
-
chatRef.current.messages = messagesParam;
|
|
157
|
-
}, [chatRef]);
|
|
158
|
-
var clearMessages = (0, _react.useCallback)(function () {
|
|
159
|
-
if (!messages || messages.length === 0) return;
|
|
160
|
-
setIsClearing(true);
|
|
161
|
-
}, [messages]);
|
|
162
|
-
var onClearTransitionEnd = (0, _react.useCallback)(function () {
|
|
163
|
-
setMessages([]);
|
|
164
|
-
chatRef.current.clearError();
|
|
165
|
-
setIsClearing(false);
|
|
166
|
-
}, [setMessages, chatRef]);
|
|
167
|
-
var toolsWithAddToolResult = (0, _react.useMemo)(function () {
|
|
168
|
-
var result = {};
|
|
169
|
-
Object.entries(tools).forEach(function (_ref4) {
|
|
170
|
-
var _ref5 = _slicedToArray(_ref4, 2),
|
|
171
|
-
key = _ref5[0],
|
|
172
|
-
tool = _ref5[1];
|
|
173
|
-
result[key] = _objectSpread(_objectSpread({}, tool), {}, {
|
|
174
|
-
addToolResult: chatRef.current.addToolResult
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
return result;
|
|
178
|
-
}, [tools]);
|
|
179
|
-
(0, _react.useEffect)(function () {
|
|
180
|
-
if (resume) {
|
|
181
|
-
chatRef.current.resumeStream();
|
|
182
|
-
}
|
|
183
|
-
}, [resume, chatRef]);
|
|
184
|
-
return {
|
|
185
|
-
id: chatRef.current.id,
|
|
186
|
-
messages: messages,
|
|
187
|
-
setMessages: setMessages,
|
|
188
|
-
input: input,
|
|
189
|
-
setInput: setInput,
|
|
190
|
-
open: open,
|
|
191
|
-
setOpen: setOpen,
|
|
192
|
-
isClearing: isClearing,
|
|
193
|
-
clearMessages: clearMessages,
|
|
194
|
-
onClearTransitionEnd: onClearTransitionEnd,
|
|
195
|
-
tools: toolsWithAddToolResult,
|
|
196
|
-
sendMessage: chatRef.current.sendMessage,
|
|
197
|
-
regenerate: chatRef.current.regenerate,
|
|
198
|
-
clearError: chatRef.current.clearError,
|
|
199
|
-
stop: chatRef.current.stop,
|
|
200
|
-
error: error,
|
|
201
|
-
resumeStream: chatRef.current.resumeStream,
|
|
202
|
-
status: status,
|
|
203
|
-
addToolResult: chatRef.current.addToolResult
|
|
204
|
-
};
|
|
7
|
+
var _connectChat = _interopRequireDefault(require("instantsearch.js/cjs/connectors/chat/connectChat"));
|
|
8
|
+
var _useConnector = require("../hooks/useConnector");
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
function useChat(props, additionalWidgetProperties) {
|
|
11
|
+
return (0, _useConnector.useConnector)(_connectChat.default, props, additionalWidgetProperties);
|
|
205
12
|
}
|
package/dist/cjs/version.js
CHANGED
|
@@ -1,79 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
/**
|
|
7
|
-
* The id of the chat.
|
|
8
|
-
*/
|
|
9
|
-
readonly id: string;
|
|
10
|
-
/**
|
|
11
|
-
* Update the `messages` state locally. This is useful when you want to
|
|
12
|
-
* edit the messages on the client, and then trigger the `reload` method
|
|
13
|
-
* manually to regenerate the AI response.
|
|
14
|
-
*/
|
|
15
|
-
setMessages: (messages: TUiMessage[] | ((m: TUiMessage[]) => TUiMessage[])) => void;
|
|
16
|
-
/**
|
|
17
|
-
* The current input value.
|
|
18
|
-
*/
|
|
19
|
-
input: string;
|
|
20
|
-
/**
|
|
21
|
-
* Update the input value.
|
|
22
|
-
*/
|
|
23
|
-
setInput: (input: string) => void;
|
|
24
|
-
/**
|
|
25
|
-
* Whether the chat is currently open.
|
|
26
|
-
*/
|
|
27
|
-
open: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Update the open state.
|
|
30
|
-
*/
|
|
31
|
-
setOpen: (open: boolean) => void;
|
|
32
|
-
/**
|
|
33
|
-
* Whether the chat is in the process of clearing messages.
|
|
34
|
-
*/
|
|
35
|
-
isClearing: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Clear all messages.
|
|
38
|
-
*/
|
|
39
|
-
clearMessages: () => void;
|
|
40
|
-
/**
|
|
41
|
-
* Callback to be called when the clear transition ends.
|
|
42
|
-
*/
|
|
43
|
-
onClearTransitionEnd: () => void;
|
|
44
|
-
/**
|
|
45
|
-
* Tools with addToolResult injected, ready to be used by the UI.
|
|
46
|
-
*/
|
|
47
|
-
tools: ClientSideTools;
|
|
48
|
-
/**
|
|
49
|
-
* The current error.
|
|
50
|
-
*/
|
|
51
|
-
error: Error | undefined;
|
|
52
|
-
} & Pick<AbstractChat<TUiMessage>, 'sendMessage' | 'regenerate' | 'stop' | 'resumeStream' | 'addToolResult' | 'status' | 'messages' | 'clearError'>;
|
|
53
|
-
export type ChatInitWithoutTransport<TUiMessage extends UIMessage> = Omit<ChatInitAi<TUiMessage>, 'transport'>;
|
|
54
|
-
export type ChatTransport = {
|
|
55
|
-
agentId?: string;
|
|
56
|
-
transport?: ConstructorParameters<typeof DefaultChatTransport>[0];
|
|
57
|
-
};
|
|
58
|
-
export type ChatInit<TUiMessage extends UIMessage> = ChatInitWithoutTransport<TUiMessage> & ChatTransport;
|
|
59
|
-
export type UseChatOptions<TUiMessage extends UIMessage> = ({
|
|
60
|
-
chat: Chat<TUiMessage>;
|
|
61
|
-
} | ChatInit<TUiMessage>) & {
|
|
62
|
-
/**
|
|
63
|
-
* Whether to resume an ongoing chat generation stream.
|
|
64
|
-
*/
|
|
65
|
-
resume?: boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Initial value for the input field.
|
|
68
|
-
*/
|
|
69
|
-
initialInput?: string;
|
|
70
|
-
/**
|
|
71
|
-
* Whether the chat should be open by default.
|
|
72
|
-
*/
|
|
73
|
-
defaultOpen?: boolean;
|
|
74
|
-
/**
|
|
75
|
-
* Custom tools to be used in the chat.
|
|
76
|
-
*/
|
|
77
|
-
tools?: UserClientSideTools;
|
|
78
|
-
};
|
|
79
|
-
export declare function useChat<TUiMessage extends UIMessage = UIMessage>({ resume, initialInput, defaultOpen, tools, ...options }?: UseChatOptions<TUiMessage>): UseChatHelpers<TUiMessage>;
|
|
1
|
+
import type { AdditionalWidgetProperties } from '../hooks/useConnector';
|
|
2
|
+
import type { ChatConnectorParams } from 'instantsearch.js/es/connectors/chat/connectChat';
|
|
3
|
+
import type { UIMessage } from 'instantsearch.js/es/lib/chat';
|
|
4
|
+
export type UseChatProps<TUiMessage extends UIMessage = UIMessage> = ChatConnectorParams<TUiMessage>;
|
|
5
|
+
export declare function useChat<TUiMessage extends UIMessage = UIMessage>(props: UseChatProps<TUiMessage>, additionalWidgetProperties?: AdditionalWidgetProperties): import("instantsearch.js/es/connectors/chat/connectChat").ChatRenderState<TUiMessage>;
|
|
@@ -1,199 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
6
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
7
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
8
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
9
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
11
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
12
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
13
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
14
|
-
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
15
|
-
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
16
|
-
import { DefaultChatTransport } from 'ai';
|
|
17
|
-
import { Chat } from "instantsearch.js/es/lib/chat/index.js";
|
|
18
|
-
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
|
|
19
|
-
import { useAppIdAndApiKey } from "../lib/useAppIdAndApiKey.js";
|
|
20
|
-
import { warn } from "../lib/warn.js";
|
|
21
|
-
export function useChat() {
|
|
22
|
-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
23
|
-
_ref$resume = _ref.resume,
|
|
24
|
-
resume = _ref$resume === void 0 ? false : _ref$resume,
|
|
25
|
-
_ref$initialInput = _ref.initialInput,
|
|
26
|
-
initialInput = _ref$initialInput === void 0 ? '' : _ref$initialInput,
|
|
27
|
-
_ref$defaultOpen = _ref.defaultOpen,
|
|
28
|
-
defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
|
|
29
|
-
_ref$tools = _ref.tools,
|
|
30
|
-
tools = _ref$tools === void 0 ? {} : _ref$tools,
|
|
31
|
-
options = _objectWithoutProperties(_ref, _excluded);
|
|
32
|
-
process.env.NODE_ENV === 'development' ? warn(false, 'Chat is not yet stable and will change in the future.') : void 0;
|
|
33
|
-
process.env.NODE_ENV === 'development' ? warn(!('agentId' in options && 'transport' in options), "`useChat` with `agentId` and `transport` can't be used together. The `transport` option will be used.") : void 0;
|
|
34
|
-
var _useAppIdAndApiKey = useAppIdAndApiKey(),
|
|
35
|
-
_useAppIdAndApiKey2 = _slicedToArray(_useAppIdAndApiKey, 2),
|
|
36
|
-
appId = _useAppIdAndApiKey2[0],
|
|
37
|
-
apiKey = _useAppIdAndApiKey2[1];
|
|
38
|
-
var _useState = useState(initialInput),
|
|
39
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
40
|
-
input = _useState2[0],
|
|
41
|
-
setInput = _useState2[1];
|
|
42
|
-
var _useState3 = useState(defaultOpen),
|
|
43
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
44
|
-
open = _useState4[0],
|
|
45
|
-
setOpen = _useState4[1];
|
|
46
|
-
var _useState5 = useState(false),
|
|
47
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
48
|
-
isClearing = _useState6[0],
|
|
49
|
-
setIsClearing = _useState6[1];
|
|
50
|
-
var transport = useMemo(function () {
|
|
51
|
-
if ('transport' in options && options.transport) {
|
|
52
|
-
return new DefaultChatTransport(options.transport);
|
|
53
|
-
}
|
|
54
|
-
if ('agentId' in options && options.agentId) {
|
|
55
|
-
var agentId = options.agentId;
|
|
56
|
-
if (!appId || !apiKey) {
|
|
57
|
-
throw new Error('The `useChat` hook requires an `appId` and `apiKey` to be set on the `InstantSearch` component when using the `agentId` option.');
|
|
58
|
-
}
|
|
59
|
-
return new DefaultChatTransport({
|
|
60
|
-
api: "https://".concat(appId, ".algolia.net/agent-studio/1/agents/").concat(agentId, "/completions?compatibilityMode=ai-sdk-5"),
|
|
61
|
-
headers: {
|
|
62
|
-
'x-algolia-application-id': appId,
|
|
63
|
-
'x-algolia-api-Key': apiKey
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
throw new Error('You need to provide either an `agentId` or a `transport`.');
|
|
68
|
-
}, [apiKey, appId, options]);
|
|
69
|
-
var optionsWithTransport = useMemo(function () {
|
|
70
|
-
if ('chat' in options) {
|
|
71
|
-
return options;
|
|
72
|
-
}
|
|
73
|
-
return _objectSpread(_objectSpread({}, options), {}, {
|
|
74
|
-
transport: transport
|
|
75
|
-
});
|
|
76
|
-
}, [options, transport]);
|
|
77
|
-
var createChatInstance = useCallback(function () {
|
|
78
|
-
if ('chat' in optionsWithTransport) {
|
|
79
|
-
return optionsWithTransport.chat;
|
|
80
|
-
}
|
|
81
|
-
var chatInstanceRef = {
|
|
82
|
-
current: null
|
|
83
|
-
};
|
|
84
|
-
var onToolCall = function onToolCall(_ref2) {
|
|
85
|
-
var toolCall = _ref2.toolCall;
|
|
86
|
-
var tool = tools[toolCall.toolName];
|
|
87
|
-
var chatInstance = chatInstanceRef.current;
|
|
88
|
-
if (!tool) {
|
|
89
|
-
if (process.env.NODE_ENV === 'development') {
|
|
90
|
-
throw new Error("No tool implementation found for \"".concat(toolCall.toolName, "\". Please provide a tool implementation in the `tools` prop."));
|
|
91
|
-
}
|
|
92
|
-
chatInstance.addToolResult({
|
|
93
|
-
output: "No tool implemented for \"".concat(toolCall.toolName, "\"."),
|
|
94
|
-
tool: toolCall.toolName,
|
|
95
|
-
toolCallId: toolCall.toolCallId
|
|
96
|
-
});
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (tool.onToolCall) {
|
|
100
|
-
var scopedAddToolResult = function scopedAddToolResult(_ref3) {
|
|
101
|
-
var output = _ref3.output;
|
|
102
|
-
return chatInstance.addToolResult({
|
|
103
|
-
output: output,
|
|
104
|
-
tool: toolCall.toolName,
|
|
105
|
-
toolCallId: toolCall.toolCallId
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
tool.onToolCall(_objectSpread(_objectSpread({}, toolCall), {}, {
|
|
109
|
-
addToolResult: scopedAddToolResult
|
|
110
|
-
}));
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
var chatInstance = new Chat(_objectSpread(_objectSpread({}, optionsWithTransport), {}, {
|
|
114
|
-
onToolCall: onToolCall
|
|
115
|
-
}));
|
|
116
|
-
chatInstanceRef.current = chatInstance;
|
|
117
|
-
return chatInstance;
|
|
118
|
-
}, [optionsWithTransport, tools]);
|
|
119
|
-
var chatRef = useRef(createChatInstance());
|
|
120
|
-
var shouldRecreateChat = 'chat' in optionsWithTransport && optionsWithTransport.chat !== chatRef.current || 'id' in optionsWithTransport && chatRef.current.id !== optionsWithTransport.id;
|
|
121
|
-
if (shouldRecreateChat) {
|
|
122
|
-
chatRef.current = createChatInstance();
|
|
123
|
-
}
|
|
124
|
-
var optionsId = 'id' in optionsWithTransport ? optionsWithTransport.id : null;
|
|
125
|
-
var subscribeToMessages = useCallback(function (update) {
|
|
126
|
-
return chatRef.current['~registerMessagesCallback'](update);
|
|
127
|
-
},
|
|
128
|
-
// optionsId is required to trigger re-subscription when the chat ID changes
|
|
129
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
130
|
-
[optionsId]);
|
|
131
|
-
var messages = useSyncExternalStore(subscribeToMessages, function () {
|
|
132
|
-
return chatRef.current.messages;
|
|
133
|
-
}, function () {
|
|
134
|
-
return chatRef.current.messages;
|
|
135
|
-
});
|
|
136
|
-
var status = useSyncExternalStore(chatRef.current['~registerStatusCallback'], function () {
|
|
137
|
-
return chatRef.current.status;
|
|
138
|
-
}, function () {
|
|
139
|
-
return chatRef.current.status;
|
|
140
|
-
});
|
|
141
|
-
var error = useSyncExternalStore(chatRef.current['~registerErrorCallback'], function () {
|
|
142
|
-
return chatRef.current.error;
|
|
143
|
-
}, function () {
|
|
144
|
-
return chatRef.current.error;
|
|
145
|
-
});
|
|
146
|
-
var setMessages = useCallback(function (messagesParam) {
|
|
147
|
-
if (typeof messagesParam === 'function') {
|
|
148
|
-
messagesParam = messagesParam(chatRef.current.messages);
|
|
149
|
-
}
|
|
150
|
-
chatRef.current.messages = messagesParam;
|
|
151
|
-
}, [chatRef]);
|
|
152
|
-
var clearMessages = useCallback(function () {
|
|
153
|
-
if (!messages || messages.length === 0) return;
|
|
154
|
-
setIsClearing(true);
|
|
155
|
-
}, [messages]);
|
|
156
|
-
var onClearTransitionEnd = useCallback(function () {
|
|
157
|
-
setMessages([]);
|
|
158
|
-
chatRef.current.clearError();
|
|
159
|
-
setIsClearing(false);
|
|
160
|
-
}, [setMessages, chatRef]);
|
|
161
|
-
var toolsWithAddToolResult = useMemo(function () {
|
|
162
|
-
var result = {};
|
|
163
|
-
Object.entries(tools).forEach(function (_ref4) {
|
|
164
|
-
var _ref5 = _slicedToArray(_ref4, 2),
|
|
165
|
-
key = _ref5[0],
|
|
166
|
-
tool = _ref5[1];
|
|
167
|
-
result[key] = _objectSpread(_objectSpread({}, tool), {}, {
|
|
168
|
-
addToolResult: chatRef.current.addToolResult
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
return result;
|
|
172
|
-
}, [tools]);
|
|
173
|
-
useEffect(function () {
|
|
174
|
-
if (resume) {
|
|
175
|
-
chatRef.current.resumeStream();
|
|
176
|
-
}
|
|
177
|
-
}, [resume, chatRef]);
|
|
178
|
-
return {
|
|
179
|
-
id: chatRef.current.id,
|
|
180
|
-
messages: messages,
|
|
181
|
-
setMessages: setMessages,
|
|
182
|
-
input: input,
|
|
183
|
-
setInput: setInput,
|
|
184
|
-
open: open,
|
|
185
|
-
setOpen: setOpen,
|
|
186
|
-
isClearing: isClearing,
|
|
187
|
-
clearMessages: clearMessages,
|
|
188
|
-
onClearTransitionEnd: onClearTransitionEnd,
|
|
189
|
-
tools: toolsWithAddToolResult,
|
|
190
|
-
sendMessage: chatRef.current.sendMessage,
|
|
191
|
-
regenerate: chatRef.current.regenerate,
|
|
192
|
-
clearError: chatRef.current.clearError,
|
|
193
|
-
stop: chatRef.current.stop,
|
|
194
|
-
error: error,
|
|
195
|
-
resumeStream: chatRef.current.resumeStream,
|
|
196
|
-
status: status,
|
|
197
|
-
addToolResult: chatRef.current.addToolResult
|
|
198
|
-
};
|
|
1
|
+
import connectChat from "instantsearch.js/es/connectors/chat/connectChat.js";
|
|
2
|
+
import { useConnector } from "../hooks/useConnector.js";
|
|
3
|
+
export function useChat(props, additionalWidgetProperties) {
|
|
4
|
+
return useConnector(connectChat, props, additionalWidgetProperties);
|
|
199
5
|
}
|
package/dist/es/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "7.
|
|
1
|
+
declare const _default: "7.20.0";
|
|
2
2
|
export default _default;
|
package/dist/es/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '7.
|
|
1
|
+
export default '7.20.0';
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
var React__default = 'default' in React ? React['default'] : React;
|
|
9
9
|
|
|
10
|
-
var version = '7.
|
|
10
|
+
var version = '7.20.0';
|
|
11
11
|
|
|
12
12
|
function _arrayLikeToArray(r, a) {
|
|
13
13
|
(null == a || a > r.length) && (a = r.length);
|
|
@@ -4481,8 +4481,8 @@
|
|
|
4481
4481
|
|
|
4482
4482
|
return recSort(
|
|
4483
4483
|
function (data, facetName) {
|
|
4484
|
+
var facetOrdering = getFacetOrdering(results, facetName);
|
|
4484
4485
|
if (options.facetOrdering) {
|
|
4485
|
-
var facetOrdering = getFacetOrdering(results, facetName);
|
|
4486
4486
|
if (facetOrdering) {
|
|
4487
4487
|
return sortViaFacetOrdering(data, facetOrdering);
|
|
4488
4488
|
}
|
|
@@ -4490,7 +4490,24 @@
|
|
|
4490
4490
|
|
|
4491
4491
|
if (Array.isArray(options.sortBy)) {
|
|
4492
4492
|
var order = formatSort(options.sortBy, SearchResults.DEFAULT_SORT);
|
|
4493
|
-
|
|
4493
|
+
var items = orderBy_1(data, order[0], order[1]);
|
|
4494
|
+
|
|
4495
|
+
var hide =
|
|
4496
|
+
facetOrdering && facetOrdering.hide ? facetOrdering.hide : [];
|
|
4497
|
+
if (hide.length > 0) {
|
|
4498
|
+
var visible = [];
|
|
4499
|
+
items.forEach(function (item) {
|
|
4500
|
+
// hierarchical facets get sorted using their raw name
|
|
4501
|
+
var name = item.path || item.name;
|
|
4502
|
+
if (hide.indexOf(name) === -1) {
|
|
4503
|
+
visible.push(item);
|
|
4504
|
+
}
|
|
4505
|
+
});
|
|
4506
|
+
|
|
4507
|
+
return visible;
|
|
4508
|
+
}
|
|
4509
|
+
|
|
4510
|
+
return items;
|
|
4494
4511
|
} else if (typeof options.sortBy === 'function') {
|
|
4495
4512
|
return vanillaSortFn(options.sortBy, data);
|
|
4496
4513
|
}
|
|
@@ -4755,7 +4772,7 @@
|
|
|
4755
4772
|
|
|
4756
4773
|
var sortAndMergeRecommendations_1 = sortAndMergeRecommendations;
|
|
4757
4774
|
|
|
4758
|
-
var version$1 = '3.26.
|
|
4775
|
+
var version$1 = '3.26.1';
|
|
4759
4776
|
|
|
4760
4777
|
var escapeFacetValue$3 = escapeFacetValue_1.escapeFacetValue;
|
|
4761
4778
|
|
|
@@ -5208,16 +5225,19 @@
|
|
|
5208
5225
|
|
|
5209
5226
|
content = Array.isArray(content) ? content[0] : content;
|
|
5210
5227
|
|
|
5211
|
-
content.facetHits.
|
|
5228
|
+
content.facetHits = content.facetHits.reduce(function (acc, f) {
|
|
5212
5229
|
if (hide.indexOf(f.value) > -1) {
|
|
5213
|
-
|
|
5214
|
-
return;
|
|
5230
|
+
return acc;
|
|
5215
5231
|
}
|
|
5232
|
+
|
|
5216
5233
|
f.escapedValue = escapeFacetValue$3(f.value);
|
|
5217
5234
|
f.isRefined = isDisjunctive
|
|
5218
5235
|
? state.isDisjunctiveFacetRefined(facet, f.escapedValue)
|
|
5219
5236
|
: state.isFacetRefined(facet, f.escapedValue);
|
|
5220
|
-
|
|
5237
|
+
|
|
5238
|
+
acc.push(f);
|
|
5239
|
+
return acc;
|
|
5240
|
+
}, []);
|
|
5221
5241
|
|
|
5222
5242
|
return content;
|
|
5223
5243
|
},
|
|
@@ -10043,6 +10063,12 @@
|
|
|
10043
10063
|
helper: helper
|
|
10044
10064
|
})));
|
|
10045
10065
|
},
|
|
10066
|
+
scheduleLocalSearch: defer(function () {
|
|
10067
|
+
if (isolated) {
|
|
10068
|
+
var _helper2;
|
|
10069
|
+
(_helper2 = helper) === null || _helper2 === void 0 ? void 0 : _helper2.search();
|
|
10070
|
+
}
|
|
10071
|
+
}),
|
|
10046
10072
|
getWidgets: function getWidgets() {
|
|
10047
10073
|
return localWidgets;
|
|
10048
10074
|
},
|
|
@@ -10108,8 +10134,7 @@
|
|
|
10108
10134
|
}
|
|
10109
10135
|
});
|
|
10110
10136
|
if (isolated) {
|
|
10111
|
-
|
|
10112
|
-
(_helper2 = helper) === null || _helper2 === void 0 ? void 0 : _helper2.search();
|
|
10137
|
+
this.scheduleLocalSearch();
|
|
10113
10138
|
} else {
|
|
10114
10139
|
localInstantSearchInstance.scheduleSearch();
|
|
10115
10140
|
}
|
|
@@ -10188,8 +10213,7 @@
|
|
|
10188
10213
|
helper.recommendState = cleanedRecommendState;
|
|
10189
10214
|
if (localWidgets.length) {
|
|
10190
10215
|
if (isolated) {
|
|
10191
|
-
|
|
10192
|
-
(_helper3 = helper) === null || _helper3 === void 0 ? void 0 : _helper3.search();
|
|
10216
|
+
this.scheduleLocalSearch();
|
|
10193
10217
|
} else {
|
|
10194
10218
|
localInstantSearchInstance.scheduleSearch();
|
|
10195
10219
|
}
|
|
@@ -10261,12 +10285,6 @@
|
|
|
10261
10285
|
helper.searchWithoutTriggeringOnStateChange = function () {
|
|
10262
10286
|
return mainHelper.search();
|
|
10263
10287
|
};
|
|
10264
|
-
|
|
10265
|
-
// We use the same pattern for the `searchForFacetValues`.
|
|
10266
|
-
helper.searchForFacetValues = function (facetName, facetValue, maxFacetHits, userState) {
|
|
10267
|
-
var state = helper.state.setQueryParameters(userState);
|
|
10268
|
-
return mainHelper.searchForFacetValues(facetName, facetValue, maxFacetHits, state);
|
|
10269
|
-
};
|
|
10270
10288
|
var isolatedHelper = indexName ? helper : algoliasearchHelper_1({}, '__empty_index__', {});
|
|
10271
10289
|
var derivingHelper = isolated ? isolatedHelper : nearestIsolatedHelper(parent, mainHelper);
|
|
10272
10290
|
derivedHelper = derivingHelper.derive(function () {
|
|
@@ -10439,7 +10457,7 @@
|
|
|
10439
10457
|
},
|
|
10440
10458
|
dispose: function dispose() {
|
|
10441
10459
|
var _this5 = this,
|
|
10442
|
-
|
|
10460
|
+
_helper3,
|
|
10443
10461
|
_derivedHelper3;
|
|
10444
10462
|
localWidgets.forEach(function (widget) {
|
|
10445
10463
|
if (widget.dispose && helper) {
|
|
@@ -10459,7 +10477,7 @@
|
|
|
10459
10477
|
});
|
|
10460
10478
|
localInstantSearchInstance = null;
|
|
10461
10479
|
localParent = null;
|
|
10462
|
-
(
|
|
10480
|
+
(_helper3 = helper) === null || _helper3 === void 0 ? void 0 : _helper3.removeAllListeners();
|
|
10463
10481
|
helper = null;
|
|
10464
10482
|
(_derivedHelper3 = derivedHelper) === null || _derivedHelper3 === void 0 ? void 0 : _derivedHelper3.detach();
|
|
10465
10483
|
derivedHelper = null;
|
|
@@ -12811,7 +12829,7 @@
|
|
|
12811
12829
|
};
|
|
12812
12830
|
}
|
|
12813
12831
|
|
|
12814
|
-
var version$2 = '4.
|
|
12832
|
+
var version$2 = '4.84.0';
|
|
12815
12833
|
|
|
12816
12834
|
function _typeof$l(o) {
|
|
12817
12835
|
"@babel/helpers - typeof";
|