librechat-data-provider 0.1.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/.github/workflows/npm-publish.yml +33 -0
- package/babel.config.js +4 -0
- package/dist/index.es.js +765 -0
- package/dist/index.js +822 -0
- package/jest.config.js +18 -0
- package/package.json +49 -0
- package/rollup.config.js +31 -0
- package/src/api-endpoints.ts +95 -0
- package/src/createPayload.ts +28 -0
- package/src/data-service.ts +119 -0
- package/src/headers-helpers.ts +9 -0
- package/src/index.ts +7 -0
- package/src/react-query-service.ts +362 -0
- package/src/request.ts +55 -0
- package/src/sse.mjs +219 -0
- package/src/types.ts +275 -0
- package/tsconfig.json +27 -0
- package/types/api-endpoints.d.ts +24 -0
- package/types/createPayload.d.ts +20 -0
- package/types/data-service.d.ts +27 -0
- package/types/headers-helpers.d.ts +2 -0
- package/types/index.d.ts +7 -0
- package/types/react-query-service.d.ts +45 -0
- package/types/request.d.ts +18 -0
- package/types/types.d.ts +239 -0
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
|
|
4
|
+
/******************************************************************************
|
|
5
|
+
Copyright (c) Microsoft Corporation.
|
|
6
|
+
|
|
7
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
8
|
+
purpose with or without fee is hereby granted.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
11
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
12
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
13
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
14
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
15
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
16
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
17
|
+
***************************************************************************** */
|
|
18
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
var __assign = function() {
|
|
22
|
+
__assign = Object.assign || function __assign(t) {
|
|
23
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
24
|
+
s = arguments[i];
|
|
25
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
26
|
+
}
|
|
27
|
+
return t;
|
|
28
|
+
};
|
|
29
|
+
return __assign.apply(this, arguments);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function __generator(thisArg, body) {
|
|
43
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
44
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
45
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
46
|
+
function step(op) {
|
|
47
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
48
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
49
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
50
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
51
|
+
switch (op[0]) {
|
|
52
|
+
case 0: case 1: t = op; break;
|
|
53
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
54
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
55
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
56
|
+
default:
|
|
57
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
58
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
59
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
60
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
61
|
+
if (t[2]) _.ops.pop();
|
|
62
|
+
_.trys.pop(); continue;
|
|
63
|
+
}
|
|
64
|
+
op = body.call(thisArg, _);
|
|
65
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
66
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
71
|
+
var e = new Error(message);
|
|
72
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function _get(url, options) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
77
|
+
var response;
|
|
78
|
+
return __generator(this, function (_a) {
|
|
79
|
+
switch (_a.label) {
|
|
80
|
+
case 0: return [4 /*yield*/, axios.get(url, __assign({}, options))];
|
|
81
|
+
case 1:
|
|
82
|
+
response = _a.sent();
|
|
83
|
+
return [2 /*return*/, response.data];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function _post(url, data) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
+
var response;
|
|
91
|
+
return __generator(this, function (_a) {
|
|
92
|
+
switch (_a.label) {
|
|
93
|
+
case 0: return [4 /*yield*/, axios.post(url, JSON.stringify(data), {
|
|
94
|
+
headers: { 'Content-Type': 'application/json' },
|
|
95
|
+
})];
|
|
96
|
+
case 1:
|
|
97
|
+
response = _a.sent();
|
|
98
|
+
return [2 /*return*/, response.data];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function _postMultiPart(url, formData, options) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
105
|
+
var response;
|
|
106
|
+
return __generator(this, function (_a) {
|
|
107
|
+
switch (_a.label) {
|
|
108
|
+
case 0: return [4 /*yield*/, axios.post(url, formData, __assign(__assign({}, options), { headers: { 'Content-Type': 'multipart/form-data' } }))];
|
|
109
|
+
case 1:
|
|
110
|
+
response = _a.sent();
|
|
111
|
+
return [2 /*return*/, response.data];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function _put(url, data) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
118
|
+
var response;
|
|
119
|
+
return __generator(this, function (_a) {
|
|
120
|
+
switch (_a.label) {
|
|
121
|
+
case 0: return [4 /*yield*/, axios.put(url, JSON.stringify(data), {
|
|
122
|
+
headers: { 'Content-Type': 'application/json' },
|
|
123
|
+
})];
|
|
124
|
+
case 1:
|
|
125
|
+
response = _a.sent();
|
|
126
|
+
return [2 /*return*/, response.data];
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function _delete(url) {
|
|
132
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
133
|
+
var response;
|
|
134
|
+
return __generator(this, function (_a) {
|
|
135
|
+
switch (_a.label) {
|
|
136
|
+
case 0: return [4 /*yield*/, axios.delete(url)];
|
|
137
|
+
case 1:
|
|
138
|
+
response = _a.sent();
|
|
139
|
+
return [2 /*return*/, response.data];
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function _deleteWithOptions(url, options) {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
146
|
+
var response;
|
|
147
|
+
return __generator(this, function (_a) {
|
|
148
|
+
switch (_a.label) {
|
|
149
|
+
case 0: return [4 /*yield*/, axios.delete(url, __assign({}, options))];
|
|
150
|
+
case 1:
|
|
151
|
+
response = _a.sent();
|
|
152
|
+
return [2 /*return*/, response.data];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function _patch(url, data) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
var response;
|
|
160
|
+
return __generator(this, function (_a) {
|
|
161
|
+
switch (_a.label) {
|
|
162
|
+
case 0: return [4 /*yield*/, axios.patch(url, JSON.stringify(data), {
|
|
163
|
+
headers: { 'Content-Type': 'application/json' },
|
|
164
|
+
})];
|
|
165
|
+
case 1:
|
|
166
|
+
response = _a.sent();
|
|
167
|
+
return [2 /*return*/, response.data];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
var request = {
|
|
173
|
+
get: _get,
|
|
174
|
+
post: _post,
|
|
175
|
+
postMultiPart: _postMultiPart,
|
|
176
|
+
put: _put,
|
|
177
|
+
delete: _delete,
|
|
178
|
+
deleteWithOptions: _deleteWithOptions,
|
|
179
|
+
patch: _patch,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
var user = function () {
|
|
183
|
+
return '/api/user';
|
|
184
|
+
};
|
|
185
|
+
var userPlugins = function () {
|
|
186
|
+
return '/api/user/plugins';
|
|
187
|
+
};
|
|
188
|
+
var messages = function (id) {
|
|
189
|
+
return "/api/messages/".concat(id);
|
|
190
|
+
};
|
|
191
|
+
var abortRequest = function (endpoint) {
|
|
192
|
+
return "/api/ask/".concat(endpoint, "/abort");
|
|
193
|
+
};
|
|
194
|
+
var conversations = function (pageNumber) {
|
|
195
|
+
return "/api/convos?pageNumber=".concat(pageNumber);
|
|
196
|
+
};
|
|
197
|
+
var conversationById = function (id) {
|
|
198
|
+
return "/api/convos/".concat(id);
|
|
199
|
+
};
|
|
200
|
+
var updateConversation$1 = function () {
|
|
201
|
+
return '/api/convos/update';
|
|
202
|
+
};
|
|
203
|
+
var deleteConversation$1 = function () {
|
|
204
|
+
return '/api/convos/clear';
|
|
205
|
+
};
|
|
206
|
+
var search = function (q, pageNumber) {
|
|
207
|
+
return "/api/search?q=".concat(q, "&pageNumber=").concat(pageNumber);
|
|
208
|
+
};
|
|
209
|
+
var searchEnabled = function () {
|
|
210
|
+
return '/api/search/enable';
|
|
211
|
+
};
|
|
212
|
+
var presets = function () {
|
|
213
|
+
return '/api/presets';
|
|
214
|
+
};
|
|
215
|
+
var deletePreset$1 = function () {
|
|
216
|
+
return '/api/presets/delete';
|
|
217
|
+
};
|
|
218
|
+
var aiEndpoints = function () {
|
|
219
|
+
return '/api/endpoints';
|
|
220
|
+
};
|
|
221
|
+
var tokenizer = function () {
|
|
222
|
+
return '/api/tokenizer';
|
|
223
|
+
};
|
|
224
|
+
var login$1 = function () {
|
|
225
|
+
return '/api/auth/login';
|
|
226
|
+
};
|
|
227
|
+
var logout$1 = function () {
|
|
228
|
+
return '/api/auth/logout';
|
|
229
|
+
};
|
|
230
|
+
var register$1 = function () {
|
|
231
|
+
return '/api/auth/register';
|
|
232
|
+
};
|
|
233
|
+
var loginGoogle = function () {
|
|
234
|
+
return '/api/auth/google';
|
|
235
|
+
};
|
|
236
|
+
var refreshToken$1 = function () {
|
|
237
|
+
return '/api/auth/refresh';
|
|
238
|
+
};
|
|
239
|
+
var requestPasswordReset$1 = function () {
|
|
240
|
+
return '/api/auth/requestPasswordReset';
|
|
241
|
+
};
|
|
242
|
+
var resetPassword$1 = function () {
|
|
243
|
+
return '/api/auth/resetPassword';
|
|
244
|
+
};
|
|
245
|
+
var plugins = function () {
|
|
246
|
+
return '/api/plugins';
|
|
247
|
+
};
|
|
248
|
+
var config = function () {
|
|
249
|
+
return '/api/config';
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
function getConversations(pageNumber) {
|
|
253
|
+
return request.get(conversations(pageNumber));
|
|
254
|
+
}
|
|
255
|
+
function abortRequestWithMessage(endpoint, abortKey, message) {
|
|
256
|
+
return request.post(abortRequest(endpoint), { arg: { abortKey: abortKey, message: message } });
|
|
257
|
+
}
|
|
258
|
+
function deleteConversation(payload) {
|
|
259
|
+
//todo: this should be a DELETE request
|
|
260
|
+
return request.post(deleteConversation$1(), { arg: payload });
|
|
261
|
+
}
|
|
262
|
+
function clearAllConversations() {
|
|
263
|
+
return request.post(deleteConversation$1(), { arg: {} });
|
|
264
|
+
}
|
|
265
|
+
function getMessagesByConvoId(id) {
|
|
266
|
+
return request.get(messages(id));
|
|
267
|
+
}
|
|
268
|
+
function getConversationById(id) {
|
|
269
|
+
return request.get(conversationById(id));
|
|
270
|
+
}
|
|
271
|
+
function updateConversation(payload) {
|
|
272
|
+
return request.post(updateConversation$1(), { arg: payload });
|
|
273
|
+
}
|
|
274
|
+
function getPresets() {
|
|
275
|
+
return request.get(presets());
|
|
276
|
+
}
|
|
277
|
+
function createPreset(payload) {
|
|
278
|
+
return request.post(presets(), payload);
|
|
279
|
+
}
|
|
280
|
+
function updatePreset(payload) {
|
|
281
|
+
return request.post(presets(), payload);
|
|
282
|
+
}
|
|
283
|
+
function deletePreset(arg) {
|
|
284
|
+
return request.post(deletePreset$1(), arg);
|
|
285
|
+
}
|
|
286
|
+
function getSearchEnabled() {
|
|
287
|
+
return request.get(searchEnabled());
|
|
288
|
+
}
|
|
289
|
+
function getUser() {
|
|
290
|
+
return request.get(user());
|
|
291
|
+
}
|
|
292
|
+
var searchConversations = function (q, pageNumber) { return __awaiter(void 0, void 0, void 0, function () {
|
|
293
|
+
return __generator(this, function (_a) {
|
|
294
|
+
return [2 /*return*/, request.get(search(q, pageNumber))];
|
|
295
|
+
});
|
|
296
|
+
}); };
|
|
297
|
+
var getAIEndpoints = function () {
|
|
298
|
+
return request.get(aiEndpoints());
|
|
299
|
+
};
|
|
300
|
+
var updateTokenCount = function (text) {
|
|
301
|
+
return request.post(tokenizer(), { arg: text });
|
|
302
|
+
};
|
|
303
|
+
var login = function (payload) {
|
|
304
|
+
return request.post(login$1(), payload);
|
|
305
|
+
};
|
|
306
|
+
var logout = function () {
|
|
307
|
+
return request.post(logout$1());
|
|
308
|
+
};
|
|
309
|
+
var register = function (payload) {
|
|
310
|
+
return request.post(register$1(), payload);
|
|
311
|
+
};
|
|
312
|
+
var refreshToken = function () {
|
|
313
|
+
return request.post(refreshToken$1());
|
|
314
|
+
};
|
|
315
|
+
var getLoginGoogle = function () {
|
|
316
|
+
return request.get(loginGoogle());
|
|
317
|
+
};
|
|
318
|
+
var requestPasswordReset = function (payload) {
|
|
319
|
+
return request.post(requestPasswordReset$1(), payload);
|
|
320
|
+
};
|
|
321
|
+
var resetPassword = function (payload) {
|
|
322
|
+
return request.post(resetPassword$1(), payload);
|
|
323
|
+
};
|
|
324
|
+
var getAvailablePlugins = function () {
|
|
325
|
+
return request.get(plugins());
|
|
326
|
+
};
|
|
327
|
+
var updateUserPlugins = function (payload) {
|
|
328
|
+
return request.post(userPlugins(), payload);
|
|
329
|
+
};
|
|
330
|
+
var getStartupConfig = function () {
|
|
331
|
+
return request.get(config());
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
var EModelEndpoint;
|
|
335
|
+
(function (EModelEndpoint) {
|
|
336
|
+
EModelEndpoint["azureOpenAI"] = "azureOpenAI";
|
|
337
|
+
EModelEndpoint["openAI"] = "openAI";
|
|
338
|
+
EModelEndpoint["bingAI"] = "bingAI";
|
|
339
|
+
EModelEndpoint["chatGPT"] = "chatGPT";
|
|
340
|
+
EModelEndpoint["chatGPTBrowser"] = "chatGPTBrowser";
|
|
341
|
+
EModelEndpoint["google"] = "google";
|
|
342
|
+
EModelEndpoint["gptPlugins"] = "gptPlugins";
|
|
343
|
+
EModelEndpoint["anthropic"] = "anthropic";
|
|
344
|
+
})(EModelEndpoint || (EModelEndpoint = {}));
|
|
345
|
+
|
|
346
|
+
var QueryKeys;
|
|
347
|
+
(function (QueryKeys) {
|
|
348
|
+
QueryKeys["messages"] = "messsages";
|
|
349
|
+
QueryKeys["allConversations"] = "allConversations";
|
|
350
|
+
QueryKeys["conversation"] = "conversation";
|
|
351
|
+
QueryKeys["searchEnabled"] = "searchEnabled";
|
|
352
|
+
QueryKeys["user"] = "user";
|
|
353
|
+
QueryKeys["endpoints"] = "endpoints";
|
|
354
|
+
QueryKeys["presets"] = "presets";
|
|
355
|
+
QueryKeys["searchResults"] = "searchResults";
|
|
356
|
+
QueryKeys["tokenCount"] = "tokenCount";
|
|
357
|
+
QueryKeys["availablePlugins"] = "availablePlugins";
|
|
358
|
+
QueryKeys["startupConfig"] = "startupConfig";
|
|
359
|
+
})(QueryKeys || (QueryKeys = {}));
|
|
360
|
+
var useAbortRequestWithMessage = function () {
|
|
361
|
+
return useMutation(function (_a) {
|
|
362
|
+
var endpoint = _a.endpoint, abortKey = _a.abortKey, message = _a.message;
|
|
363
|
+
return abortRequestWithMessage(endpoint, abortKey, message);
|
|
364
|
+
});
|
|
365
|
+
};
|
|
366
|
+
var useGetUserQuery = function (config) {
|
|
367
|
+
return useQuery([QueryKeys.user], function () { return getUser(); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false, retry: false }, config));
|
|
368
|
+
};
|
|
369
|
+
var useGetMessagesByConvoId = function (id, config) {
|
|
370
|
+
return useQuery([QueryKeys.messages, id], function () { return getMessagesByConvoId(id); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false }, config));
|
|
371
|
+
};
|
|
372
|
+
var useGetConversationByIdQuery = function (id, config) {
|
|
373
|
+
return useQuery([QueryKeys.conversation, id], function () { return getConversationById(id); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false }, config));
|
|
374
|
+
};
|
|
375
|
+
//This isn't ideal because its just a query and we're using mutation, but it was the only way
|
|
376
|
+
//to make it work with how the Chat component is structured
|
|
377
|
+
var useGetConversationByIdMutation = function (id) {
|
|
378
|
+
var queryClient = useQueryClient();
|
|
379
|
+
return useMutation(function () { return getConversationById(id); }, {
|
|
380
|
+
// onSuccess: (res: t.TConversation) => {
|
|
381
|
+
onSuccess: function () {
|
|
382
|
+
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
};
|
|
386
|
+
var useUpdateConversationMutation = function (id) {
|
|
387
|
+
var queryClient = useQueryClient();
|
|
388
|
+
return useMutation(function (payload) { return updateConversation(payload); }, {
|
|
389
|
+
onSuccess: function () {
|
|
390
|
+
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
|
391
|
+
queryClient.invalidateQueries([QueryKeys.allConversations]);
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
var useDeleteConversationMutation = function (id) {
|
|
396
|
+
var queryClient = useQueryClient();
|
|
397
|
+
return useMutation(function (payload) { return deleteConversation(payload); }, {
|
|
398
|
+
onSuccess: function () {
|
|
399
|
+
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
|
400
|
+
queryClient.invalidateQueries([QueryKeys.allConversations]);
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
var useClearConversationsMutation = function () {
|
|
405
|
+
var queryClient = useQueryClient();
|
|
406
|
+
return useMutation(function () { return clearAllConversations(); }, {
|
|
407
|
+
onSuccess: function () {
|
|
408
|
+
queryClient.invalidateQueries([QueryKeys.allConversations]);
|
|
409
|
+
},
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
var useGetConversationsQuery = function (pageNumber, config) {
|
|
413
|
+
return useQuery([QueryKeys.allConversations, pageNumber], function () { return getConversations(pageNumber); }, __assign({ refetchOnReconnect: false, refetchOnMount: false, retry: 1 }, config));
|
|
414
|
+
};
|
|
415
|
+
var useGetSearchEnabledQuery = function (config) {
|
|
416
|
+
return useQuery([QueryKeys.searchEnabled], function () { return getSearchEnabled(); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false }, config));
|
|
417
|
+
};
|
|
418
|
+
var useGetEndpointsQuery = function () {
|
|
419
|
+
return useQuery([QueryKeys.endpoints], function () { return getAIEndpoints(); }, {
|
|
420
|
+
refetchOnWindowFocus: false,
|
|
421
|
+
refetchOnReconnect: false,
|
|
422
|
+
refetchOnMount: false,
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
var useCreatePresetMutation = function () {
|
|
426
|
+
var queryClient = useQueryClient();
|
|
427
|
+
return useMutation(function (payload) { return createPreset(payload); }, {
|
|
428
|
+
onSuccess: function () {
|
|
429
|
+
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
};
|
|
433
|
+
var useUpdatePresetMutation = function () {
|
|
434
|
+
var queryClient = useQueryClient();
|
|
435
|
+
return useMutation(function (payload) { return updatePreset(payload); }, {
|
|
436
|
+
onSuccess: function () {
|
|
437
|
+
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
var useGetPresetsQuery = function (config) {
|
|
442
|
+
return useQuery([QueryKeys.presets], function () { return getPresets(); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false }, config));
|
|
443
|
+
};
|
|
444
|
+
var useDeletePresetMutation = function () {
|
|
445
|
+
var queryClient = useQueryClient();
|
|
446
|
+
return useMutation(function (payload) { return deletePreset(payload); }, {
|
|
447
|
+
onSuccess: function () {
|
|
448
|
+
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
};
|
|
452
|
+
var useSearchQuery = function (searchQuery, pageNumber, config) {
|
|
453
|
+
return useQuery([QueryKeys.searchResults, pageNumber, searchQuery], function () { return searchConversations(searchQuery, pageNumber); }, __assign({ refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false }, config));
|
|
454
|
+
};
|
|
455
|
+
var useUpdateTokenCountMutation = function () {
|
|
456
|
+
var queryClient = useQueryClient();
|
|
457
|
+
return useMutation(function (text) { return updateTokenCount(text); }, {
|
|
458
|
+
onSuccess: function () {
|
|
459
|
+
queryClient.invalidateQueries([QueryKeys.tokenCount]);
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
var useLoginUserMutation = function () {
|
|
464
|
+
var queryClient = useQueryClient();
|
|
465
|
+
return useMutation(function (payload) { return login(payload); }, {
|
|
466
|
+
onSuccess: function () {
|
|
467
|
+
queryClient.invalidateQueries([QueryKeys.user]);
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
var useRegisterUserMutation = function () {
|
|
472
|
+
var queryClient = useQueryClient();
|
|
473
|
+
return useMutation(function (payload) { return register(payload); }, {
|
|
474
|
+
onSuccess: function () {
|
|
475
|
+
queryClient.invalidateQueries([QueryKeys.user]);
|
|
476
|
+
},
|
|
477
|
+
});
|
|
478
|
+
};
|
|
479
|
+
var useLogoutUserMutation = function () {
|
|
480
|
+
var queryClient = useQueryClient();
|
|
481
|
+
return useMutation(function () { return logout(); }, {
|
|
482
|
+
onSuccess: function () {
|
|
483
|
+
queryClient.invalidateQueries([QueryKeys.user]);
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
};
|
|
487
|
+
var useRefreshTokenMutation = function () {
|
|
488
|
+
return useMutation(function () { return refreshToken(); }, {});
|
|
489
|
+
};
|
|
490
|
+
var useRequestPasswordResetMutation = function () {
|
|
491
|
+
return useMutation(function (payload) {
|
|
492
|
+
return requestPasswordReset(payload);
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
var useResetPasswordMutation = function () {
|
|
496
|
+
return useMutation(function (payload) { return resetPassword(payload); });
|
|
497
|
+
};
|
|
498
|
+
var useAvailablePluginsQuery = function () {
|
|
499
|
+
return useQuery([QueryKeys.availablePlugins], function () { return getAvailablePlugins(); }, {
|
|
500
|
+
refetchOnWindowFocus: false,
|
|
501
|
+
refetchOnReconnect: false,
|
|
502
|
+
refetchOnMount: false,
|
|
503
|
+
});
|
|
504
|
+
};
|
|
505
|
+
var useUpdateUserPluginsMutation = function () {
|
|
506
|
+
var queryClient = useQueryClient();
|
|
507
|
+
return useMutation(function (payload) { return updateUserPlugins(payload); }, {
|
|
508
|
+
onSuccess: function () {
|
|
509
|
+
queryClient.invalidateQueries([QueryKeys.user]);
|
|
510
|
+
},
|
|
511
|
+
});
|
|
512
|
+
};
|
|
513
|
+
var useGetStartupConfig = function () {
|
|
514
|
+
return useQuery([QueryKeys.startupConfig], function () { return getStartupConfig(); }, {
|
|
515
|
+
refetchOnWindowFocus: false,
|
|
516
|
+
refetchOnReconnect: false,
|
|
517
|
+
refetchOnMount: false,
|
|
518
|
+
});
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
function setAcceptLanguageHeader(value) {
|
|
522
|
+
axios.defaults.headers.common['Accept-Language'] = value;
|
|
523
|
+
}
|
|
524
|
+
function setTokenHeader(token) {
|
|
525
|
+
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Copyright (C) 2016 Maxime Petazzoni <maxime.petazzoni@bulix.org>.
|
|
530
|
+
* All rights reserved.
|
|
531
|
+
*/
|
|
532
|
+
|
|
533
|
+
var SSE = function (url, options) {
|
|
534
|
+
if (!(this instanceof SSE)) {
|
|
535
|
+
return new SSE(url, options);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
this.INITIALIZING = -1;
|
|
539
|
+
this.CONNECTING = 0;
|
|
540
|
+
this.OPEN = 1;
|
|
541
|
+
this.CLOSED = 2;
|
|
542
|
+
|
|
543
|
+
this.url = url;
|
|
544
|
+
|
|
545
|
+
options = options || {};
|
|
546
|
+
this.headers = options.headers || {};
|
|
547
|
+
this.payload = options.payload !== undefined ? options.payload : '';
|
|
548
|
+
this.method = options.method || (this.payload && 'POST' || 'GET');
|
|
549
|
+
this.withCredentials = !!options.withCredentials;
|
|
550
|
+
|
|
551
|
+
this.FIELD_SEPARATOR = ':';
|
|
552
|
+
this.listeners = {};
|
|
553
|
+
|
|
554
|
+
this.xhr = null;
|
|
555
|
+
this.readyState = this.INITIALIZING;
|
|
556
|
+
this.progress = 0;
|
|
557
|
+
this.chunk = '';
|
|
558
|
+
|
|
559
|
+
this.addEventListener = function(type, listener) {
|
|
560
|
+
if (this.listeners[type] === undefined) {
|
|
561
|
+
this.listeners[type] = [];
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (this.listeners[type].indexOf(listener) === -1) {
|
|
565
|
+
this.listeners[type].push(listener);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
this.removeEventListener = function(type, listener) {
|
|
570
|
+
if (this.listeners[type] === undefined) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
var filtered = [];
|
|
575
|
+
this.listeners[type].forEach(function(element) {
|
|
576
|
+
if (element !== listener) {
|
|
577
|
+
filtered.push(element);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
if (filtered.length === 0) {
|
|
581
|
+
delete this.listeners[type];
|
|
582
|
+
} else {
|
|
583
|
+
this.listeners[type] = filtered;
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
this.dispatchEvent = function(e) {
|
|
588
|
+
if (!e) {
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
e.source = this;
|
|
593
|
+
|
|
594
|
+
var onHandler = 'on' + e.type;
|
|
595
|
+
if (this.hasOwnProperty(onHandler)) {
|
|
596
|
+
this[onHandler].call(this, e);
|
|
597
|
+
if (e.defaultPrevented) {
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (this.listeners[e.type]) {
|
|
603
|
+
return this.listeners[e.type].every(function(callback) {
|
|
604
|
+
callback(e);
|
|
605
|
+
return !e.defaultPrevented;
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return true;
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
this._setReadyState = function(state) {
|
|
613
|
+
var event = new CustomEvent('readystatechange');
|
|
614
|
+
event.readyState = state;
|
|
615
|
+
this.readyState = state;
|
|
616
|
+
this.dispatchEvent(event);
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
this._onStreamFailure = function(e) {
|
|
620
|
+
var event = new CustomEvent('error');
|
|
621
|
+
event.data = e.currentTarget.response;
|
|
622
|
+
this.dispatchEvent(event);
|
|
623
|
+
this.close();
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
this._onStreamAbort = function(e) {
|
|
627
|
+
this.dispatchEvent(new CustomEvent('abort'));
|
|
628
|
+
this.close();
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
this._onStreamProgress = function(e) {
|
|
632
|
+
if (!this.xhr) {
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
if (this.xhr.status !== 200) {
|
|
637
|
+
this._onStreamFailure(e);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
if (this.readyState == this.CONNECTING) {
|
|
642
|
+
this.dispatchEvent(new CustomEvent('open'));
|
|
643
|
+
this._setReadyState(this.OPEN);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
var data = this.xhr.responseText.substring(this.progress);
|
|
647
|
+
this.progress += data.length;
|
|
648
|
+
data.split(/(\r\n|\r|\n){2}/g).forEach(function(part) {
|
|
649
|
+
if (part.trim().length === 0) {
|
|
650
|
+
this.dispatchEvent(this._parseEventChunk(this.chunk.trim()));
|
|
651
|
+
this.chunk = '';
|
|
652
|
+
} else {
|
|
653
|
+
this.chunk += part;
|
|
654
|
+
}
|
|
655
|
+
}.bind(this));
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
this._onStreamLoaded = function(e) {
|
|
659
|
+
this._onStreamProgress(e);
|
|
660
|
+
|
|
661
|
+
// Parse the last chunk.
|
|
662
|
+
this.dispatchEvent(this._parseEventChunk(this.chunk));
|
|
663
|
+
this.chunk = '';
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Parse a received SSE event chunk into a constructed event object.
|
|
668
|
+
*/
|
|
669
|
+
this._parseEventChunk = function(chunk) {
|
|
670
|
+
if (!chunk || chunk.length === 0) {
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
var e = {'id': null, 'retry': null, 'data': '', 'event': 'message'};
|
|
675
|
+
chunk.split(/\n|\r\n|\r/).forEach(function(line) {
|
|
676
|
+
line = line.trimRight();
|
|
677
|
+
var index = line.indexOf(this.FIELD_SEPARATOR);
|
|
678
|
+
if (index <= 0) {
|
|
679
|
+
// Line was either empty, or started with a separator and is a comment.
|
|
680
|
+
// Either way, ignore.
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
var field = line.substring(0, index);
|
|
685
|
+
if (!(field in e)) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
var value = line.substring(index + 1).trimLeft();
|
|
690
|
+
if (field === 'data') {
|
|
691
|
+
e[field] += value;
|
|
692
|
+
} else {
|
|
693
|
+
e[field] = value;
|
|
694
|
+
}
|
|
695
|
+
}.bind(this));
|
|
696
|
+
|
|
697
|
+
var event = new CustomEvent(e.event);
|
|
698
|
+
event.data = e.data;
|
|
699
|
+
event.id = e.id;
|
|
700
|
+
return event;
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
this._checkStreamClosed = function() {
|
|
704
|
+
if (!this.xhr) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (this.xhr.readyState === XMLHttpRequest.DONE) {
|
|
709
|
+
this._setReadyState(this.CLOSED);
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
this.stream = function() {
|
|
714
|
+
this._setReadyState(this.CONNECTING);
|
|
715
|
+
|
|
716
|
+
this.xhr = new XMLHttpRequest();
|
|
717
|
+
this.xhr.addEventListener('progress', this._onStreamProgress.bind(this));
|
|
718
|
+
this.xhr.addEventListener('load', this._onStreamLoaded.bind(this));
|
|
719
|
+
this.xhr.addEventListener('readystatechange', this._checkStreamClosed.bind(this));
|
|
720
|
+
this.xhr.addEventListener('error', this._onStreamFailure.bind(this));
|
|
721
|
+
this.xhr.addEventListener('abort', this._onStreamAbort.bind(this));
|
|
722
|
+
this.xhr.open(this.method, this.url);
|
|
723
|
+
for (var header in this.headers) {
|
|
724
|
+
this.xhr.setRequestHeader(header, this.headers[header]);
|
|
725
|
+
}
|
|
726
|
+
this.xhr.withCredentials = this.withCredentials;
|
|
727
|
+
this.xhr.send(this.payload);
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
this.close = function() {
|
|
731
|
+
if (this.readyState === this.CLOSED) {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
this.xhr.abort();
|
|
736
|
+
this.xhr = null;
|
|
737
|
+
this._setReadyState(this.CLOSED);
|
|
738
|
+
};
|
|
739
|
+
};
|
|
740
|
+
// Export our SSE module for npm.js
|
|
741
|
+
// if (typeof exports !== 'undefined') {
|
|
742
|
+
// // exports.SSE = SSE;
|
|
743
|
+
// module.exports = { SSE };
|
|
744
|
+
// }
|
|
745
|
+
|
|
746
|
+
function createPayload(submission) {
|
|
747
|
+
var conversation = submission.conversation, message = submission.message, endpointOption = submission.endpointOption;
|
|
748
|
+
var conversationId = conversation.conversationId;
|
|
749
|
+
var endpoint = endpointOption.endpoint;
|
|
750
|
+
var endpointUrlMap = {
|
|
751
|
+
azureOpenAI: '/api/ask/azureOpenAI',
|
|
752
|
+
openAI: '/api/ask/openAI',
|
|
753
|
+
google: '/api/ask/google',
|
|
754
|
+
bingAI: '/api/ask/bingAI',
|
|
755
|
+
chatGPT: '/api/ask/chatGPT',
|
|
756
|
+
chatGPTBrowser: '/api/ask/chatGPTBrowser',
|
|
757
|
+
gptPlugins: '/api/ask/gptPlugins',
|
|
758
|
+
anthropic: '/api/ask/anthropic',
|
|
759
|
+
};
|
|
760
|
+
var server = endpointUrlMap[endpoint];
|
|
761
|
+
var payload = __assign(__assign(__assign({}, message), endpointOption), { conversationId: conversationId });
|
|
762
|
+
return { server: server, payload: payload };
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
export { EModelEndpoint, QueryKeys, SSE, abortRequestWithMessage, clearAllConversations, createPayload, createPreset, deleteConversation, deletePreset, getAIEndpoints, getAvailablePlugins, getConversationById, getConversations, getLoginGoogle, getMessagesByConvoId, getPresets, getSearchEnabled, getStartupConfig, getUser, login, logout, refreshToken, register, requestPasswordReset, resetPassword, searchConversations, setAcceptLanguageHeader, setTokenHeader, updateConversation, updatePreset, updateTokenCount, updateUserPlugins, useAbortRequestWithMessage, useAvailablePluginsQuery, useClearConversationsMutation, useCreatePresetMutation, useDeleteConversationMutation, useDeletePresetMutation, useGetConversationByIdMutation, useGetConversationByIdQuery, useGetConversationsQuery, useGetEndpointsQuery, useGetMessagesByConvoId, useGetPresetsQuery, useGetSearchEnabledQuery, useGetStartupConfig, useGetUserQuery, useLoginUserMutation, useLogoutUserMutation, useRefreshTokenMutation, useRegisterUserMutation, useRequestPasswordResetMutation, useResetPasswordMutation, useSearchQuery, useUpdateConversationMutation, useUpdatePresetMutation, useUpdateTokenCountMutation, useUpdateUserPluginsMutation };
|