ai 2.1.26 → 2.1.27

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.
@@ -4,22 +4,8 @@ var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
7
  var __getProtoOf = Object.getPrototypeOf;
9
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
9
  var __export = (target, all) => {
24
10
  for (var name in all)
25
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -41,26 +27,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
41
27
  mod
42
28
  ));
43
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
44
- var __async = (__this, __arguments, generator) => {
45
- return new Promise((resolve, reject) => {
46
- var fulfilled = (value) => {
47
- try {
48
- step(generator.next(value));
49
- } catch (e) {
50
- reject(e);
51
- }
52
- };
53
- var rejected = (value) => {
54
- try {
55
- step(generator.throw(value));
56
- } catch (e) {
57
- reject(e);
58
- }
59
- };
60
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
61
- step((generator = generator.apply(__this, __arguments)).next());
62
- });
63
- };
64
30
 
65
31
  // react/index.ts
66
32
  var react_exports = {};
@@ -72,7 +38,6 @@ module.exports = __toCommonJS(react_exports);
72
38
 
73
39
  // react/use-chat.ts
74
40
  var import_react = require("react");
75
- var import_mutation = __toESM(require("swr/mutation"));
76
41
  var import_swr = __toESM(require("swr"));
77
42
 
78
43
  // shared/utils.ts
@@ -91,44 +56,54 @@ function createChunkDecoder() {
91
56
  }
92
57
 
93
58
  // react/use-chat.ts
94
- var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => __async(void 0, null, function* () {
59
+ var getStreamedResponse = async (api, chatRequest, mutate, extraMetadataRef, messagesRef, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
95
60
  var _a, _b;
96
61
  const previousMessages = messagesRef.current;
97
62
  mutate(chatRequest.messages, false);
98
- const res = yield fetch(api, __spreadValues({
63
+ const res = await fetch(api, {
99
64
  method: "POST",
100
- body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
65
+ body: JSON.stringify({
101
66
  messages: sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
102
- ({ role, content, name, function_call }) => __spreadValues(__spreadValues({
67
+ ({ role, content, name, function_call }) => ({
103
68
  role,
104
- content
105
- }, name !== void 0 && { name }), function_call !== void 0 && {
106
- function_call
69
+ content,
70
+ ...name !== void 0 && { name },
71
+ ...function_call !== void 0 && {
72
+ function_call
73
+ }
107
74
  })
108
- )
109
- }, extraMetadataRef.current.body), (_a = chatRequest.options) == null ? void 0 : _a.body), chatRequest.functions !== void 0 && {
110
- functions: chatRequest.functions
111
- }), chatRequest.function_call !== void 0 && {
112
- function_call: chatRequest.function_call
113
- })),
75
+ ),
76
+ ...extraMetadataRef.current.body,
77
+ ...(_a = chatRequest.options) == null ? void 0 : _a.body,
78
+ ...chatRequest.functions !== void 0 && {
79
+ functions: chatRequest.functions
80
+ },
81
+ ...chatRequest.function_call !== void 0 && {
82
+ function_call: chatRequest.function_call
83
+ }
84
+ }),
114
85
  credentials: extraMetadataRef.current.credentials,
115
- headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), (_b = chatRequest.options) == null ? void 0 : _b.headers)
116
- }, abortControllerRef.current !== null && {
117
- signal: abortControllerRef.current.signal
118
- })).catch((err) => {
86
+ headers: {
87
+ ...extraMetadataRef.current.headers,
88
+ ...(_b = chatRequest.options) == null ? void 0 : _b.headers
89
+ },
90
+ ...abortControllerRef.current !== null && {
91
+ signal: abortControllerRef.current.signal
92
+ }
93
+ }).catch((err) => {
119
94
  mutate(previousMessages, false);
120
95
  throw err;
121
96
  });
122
97
  if (onResponse) {
123
98
  try {
124
- yield onResponse(res);
99
+ await onResponse(res);
125
100
  } catch (err) {
126
101
  throw err;
127
102
  }
128
103
  }
129
104
  if (!res.ok) {
130
105
  mutate(previousMessages, false);
131
- throw new Error((yield res.text()) || "Failed to fetch the chat response.");
106
+ throw new Error(await res.text() || "Failed to fetch the chat response.");
132
107
  }
133
108
  if (!res.body) {
134
109
  throw new Error("The response body is empty.");
@@ -145,7 +120,7 @@ var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesR
145
120
  role: "assistant"
146
121
  };
147
122
  while (true) {
148
- const { done, value } = yield reader.read();
123
+ const { done, value } = await reader.read();
149
124
  if (done) {
150
125
  break;
151
126
  }
@@ -155,7 +130,7 @@ var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesR
155
130
  } else {
156
131
  responseMessage["content"] = streamedResponse;
157
132
  }
158
- mutate([...chatRequest.messages, __spreadValues({}, responseMessage)], false);
133
+ mutate([...chatRequest.messages, { ...responseMessage }], false);
159
134
  if (abortControllerRef.current === null) {
160
135
  reader.cancel();
161
136
  break;
@@ -164,13 +139,13 @@ var getStreamedResponse = (api, chatRequest, mutate, extraMetadataRef, messagesR
164
139
  if (streamedResponse.startsWith('{"function_call":')) {
165
140
  const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
166
141
  responseMessage["function_call"] = parsedFunctionCall;
167
- mutate([...chatRequest.messages, __spreadValues({}, responseMessage)]);
142
+ mutate([...chatRequest.messages, { ...responseMessage }]);
168
143
  }
169
144
  if (onFinish) {
170
145
  onFinish(responseMessage);
171
146
  }
172
147
  return responseMessage;
173
- });
148
+ };
174
149
  function useChat({
175
150
  api = "/api/chat",
176
151
  id,
@@ -190,6 +165,10 @@ function useChat({
190
165
  const { data, mutate } = (0, import_swr.default)([api, chatId], null, {
191
166
  fallbackData: initialMessages
192
167
  });
168
+ const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
169
+ [api, chatId, "loading"],
170
+ null
171
+ );
193
172
  const messages = data;
194
173
  const messagesRef = (0, import_react.useRef)(messages);
195
174
  (0, import_react.useEffect)(() => {
@@ -208,91 +187,88 @@ function useChat({
208
187
  body
209
188
  };
210
189
  }, [credentials, headers, body]);
211
- const { error, trigger, isMutating } = (0, import_mutation.default)(
212
- [api, chatId],
213
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg: initialChatRequest }) {
214
- try {
215
- const abortController = new AbortController();
216
- abortControllerRef.current = abortController;
217
- let chatRequest = initialChatRequest;
218
- while (true) {
219
- const streamedResponseMessage = yield getStreamedResponse(
220
- api,
221
- chatRequest,
222
- mutate,
223
- extraMetadataRef,
224
- messagesRef,
225
- abortControllerRef,
226
- onFinish,
227
- onResponse,
228
- sendExtraMessageFields
229
- );
230
- if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
190
+ const [error, setError] = (0, import_react.useState)();
191
+ async function triggerRequest(chatRequest) {
192
+ try {
193
+ mutateLoading(true);
194
+ const abortController = new AbortController();
195
+ abortControllerRef.current = abortController;
196
+ while (true) {
197
+ const streamedResponseMessage = await getStreamedResponse(
198
+ api,
199
+ chatRequest,
200
+ mutate,
201
+ extraMetadataRef,
202
+ messagesRef,
203
+ abortControllerRef,
204
+ onFinish,
205
+ onResponse,
206
+ sendExtraMessageFields
207
+ );
208
+ if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
209
+ break;
210
+ }
211
+ if (experimental_onFunctionCall) {
212
+ const functionCall = streamedResponseMessage.function_call;
213
+ const functionCallResponse = await experimental_onFunctionCall(messagesRef.current, functionCall);
214
+ if (functionCallResponse === void 0)
231
215
  break;
232
- }
233
- if (experimental_onFunctionCall) {
234
- const functionCall = streamedResponseMessage.function_call;
235
- const functionCallResponse = yield experimental_onFunctionCall(
236
- messagesRef.current,
237
- functionCall
238
- );
239
- if (functionCallResponse === void 0)
240
- break;
241
- chatRequest = functionCallResponse;
242
- }
216
+ chatRequest = functionCallResponse;
243
217
  }
218
+ }
219
+ abortControllerRef.current = null;
220
+ return null;
221
+ } catch (err) {
222
+ if (err.name === "AbortError") {
244
223
  abortControllerRef.current = null;
245
224
  return null;
246
- } catch (err) {
247
- if (err.name === "AbortError") {
248
- abortControllerRef.current = null;
249
- return null;
250
- }
251
- if (onError && err instanceof Error) {
252
- onError(err);
253
- }
254
- throw err;
255
225
  }
256
- }),
257
- {
258
- populateCache: false,
259
- revalidate: false,
260
- // @ts-expect-error - SWR tries to be clever with the throwOnError type
261
- throwOnError: Boolean(!onError)
226
+ if (onError && err instanceof Error) {
227
+ onError(err);
228
+ }
229
+ setError(err);
230
+ } finally {
231
+ mutateLoading(false);
262
232
  }
263
- );
233
+ }
264
234
  const append = (0, import_react.useCallback)(
265
- (_0, ..._1) => __async(this, [_0, ..._1], function* (message, { options, functions, function_call } = {}) {
235
+ async (message, { options, functions, function_call } = {}) => {
266
236
  if (!message.id) {
267
237
  message.id = nanoid();
268
238
  }
269
- const chatRequest = __spreadValues(__spreadValues({
239
+ const chatRequest = {
270
240
  messages: messagesRef.current.concat(message),
271
- options
272
- }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
273
- return trigger(chatRequest);
274
- }),
275
- [trigger]
241
+ options,
242
+ ...functions !== void 0 && { functions },
243
+ ...function_call !== void 0 && { function_call }
244
+ };
245
+ return triggerRequest(chatRequest);
246
+ },
247
+ [triggerRequest]
276
248
  );
277
249
  const reload = (0, import_react.useCallback)(
278
- (..._0) => __async(this, [..._0], function* ({ options, functions, function_call } = {}) {
250
+ async ({ options, functions, function_call } = {}) => {
279
251
  if (messagesRef.current.length === 0)
280
252
  return null;
281
253
  const lastMessage = messagesRef.current[messagesRef.current.length - 1];
282
254
  if (lastMessage.role === "assistant") {
283
- const chatRequest2 = __spreadValues(__spreadValues({
255
+ const chatRequest2 = {
284
256
  messages: messagesRef.current.slice(0, -1),
285
- options
286
- }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
287
- return trigger(chatRequest2);
257
+ options,
258
+ ...functions !== void 0 && { functions },
259
+ ...function_call !== void 0 && { function_call }
260
+ };
261
+ return triggerRequest(chatRequest2);
288
262
  }
289
- const chatRequest = __spreadValues(__spreadValues({
263
+ const chatRequest = {
290
264
  messages: messagesRef.current,
291
- options
292
- }, functions !== void 0 && { functions }), function_call !== void 0 && { function_call });
293
- return trigger(chatRequest);
294
- }),
295
- [trigger]
265
+ options,
266
+ ...functions !== void 0 && { functions },
267
+ ...function_call !== void 0 && { function_call }
268
+ };
269
+ return triggerRequest(chatRequest);
270
+ },
271
+ [triggerRequest]
296
272
  );
297
273
  const stop = (0, import_react.useCallback)(() => {
298
274
  if (abortControllerRef.current) {
@@ -311,7 +287,10 @@ function useChat({
311
287
  const handleSubmit = (0, import_react.useCallback)(
312
288
  (e, { options, functions, function_call } = {}, metadata) => {
313
289
  if (metadata) {
314
- extraMetadataRef.current = __spreadValues(__spreadValues({}, extraMetadataRef.current), metadata);
290
+ extraMetadataRef.current = {
291
+ ...extraMetadataRef.current,
292
+ ...metadata
293
+ };
315
294
  }
316
295
  e.preventDefault();
317
296
  if (!input)
@@ -342,13 +321,12 @@ function useChat({
342
321
  setInput,
343
322
  handleInputChange,
344
323
  handleSubmit,
345
- isLoading: isMutating
324
+ isLoading
346
325
  };
347
326
  }
348
327
 
349
328
  // react/use-completion.ts
350
329
  var import_react2 = require("react");
351
- var import_mutation2 = __toESM(require("swr/mutation"));
352
330
  var import_swr2 = __toESM(require("swr"));
353
331
  function useCompletion({
354
332
  api = "/api/completion",
@@ -367,6 +345,11 @@ function useCompletion({
367
345
  const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
368
346
  fallbackData: initialCompletion
369
347
  });
348
+ const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr2.default)(
349
+ [api, completionId, "loading"],
350
+ null
351
+ );
352
+ const [error, setError] = (0, import_react2.useState)(void 0);
370
353
  const completion = data;
371
354
  const [abortController, setAbortController] = (0, import_react2.useState)(null);
372
355
  const extraMetadataRef = (0, import_react2.useRef)({
@@ -381,78 +364,78 @@ function useCompletion({
381
364
  body
382
365
  };
383
366
  }, [credentials, headers, body]);
384
- const { error, trigger, isMutating } = (0, import_mutation2.default)(
385
- [api, completionId],
386
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg }) {
387
- try {
388
- const { prompt, options } = arg;
389
- const abortController2 = new AbortController();
390
- setAbortController(abortController2);
391
- mutate("", false);
392
- const res = yield fetch(api, {
393
- method: "POST",
394
- body: JSON.stringify(__spreadValues(__spreadValues({
395
- prompt
396
- }, extraMetadataRef.current.body), options == null ? void 0 : options.body)),
397
- credentials: extraMetadataRef.current.credentials,
398
- headers: __spreadValues(__spreadValues({}, extraMetadataRef.current.headers), options == null ? void 0 : options.headers),
399
- signal: abortController2.signal
400
- }).catch((err) => {
367
+ async function triggerRequest(prompt, options) {
368
+ try {
369
+ mutateLoading(true);
370
+ const abortController2 = new AbortController();
371
+ setAbortController(abortController2);
372
+ mutate("", false);
373
+ const res = await fetch(api, {
374
+ method: "POST",
375
+ body: JSON.stringify({
376
+ prompt,
377
+ ...extraMetadataRef.current.body,
378
+ ...options == null ? void 0 : options.body
379
+ }),
380
+ credentials: extraMetadataRef.current.credentials,
381
+ headers: {
382
+ ...extraMetadataRef.current.headers,
383
+ ...options == null ? void 0 : options.headers
384
+ },
385
+ signal: abortController2.signal
386
+ }).catch((err) => {
387
+ throw err;
388
+ });
389
+ if (onResponse) {
390
+ try {
391
+ await onResponse(res);
392
+ } catch (err) {
401
393
  throw err;
402
- });
403
- if (onResponse) {
404
- try {
405
- yield onResponse(res);
406
- } catch (err) {
407
- throw err;
408
- }
409
- }
410
- if (!res.ok) {
411
- throw new Error(
412
- (yield res.text()) || "Failed to fetch the chat response."
413
- );
414
394
  }
415
- if (!res.body) {
416
- throw new Error("The response body is empty.");
417
- }
418
- let result = "";
419
- const reader = res.body.getReader();
420
- const decoder = createChunkDecoder();
421
- while (true) {
422
- const { done, value } = yield reader.read();
423
- if (done) {
424
- break;
425
- }
426
- result += decoder(value);
427
- mutate(result, false);
428
- if (abortController2 === null) {
429
- reader.cancel();
430
- break;
431
- }
395
+ }
396
+ if (!res.ok) {
397
+ throw new Error(
398
+ await res.text() || "Failed to fetch the chat response."
399
+ );
400
+ }
401
+ if (!res.body) {
402
+ throw new Error("The response body is empty.");
403
+ }
404
+ let result = "";
405
+ const reader = res.body.getReader();
406
+ const decoder = createChunkDecoder();
407
+ while (true) {
408
+ const { done, value } = await reader.read();
409
+ if (done) {
410
+ break;
432
411
  }
433
- if (onFinish) {
434
- onFinish(prompt, result);
412
+ result += decoder(value);
413
+ mutate(result, false);
414
+ if (abortController2 === null) {
415
+ reader.cancel();
416
+ break;
435
417
  }
418
+ }
419
+ if (onFinish) {
420
+ onFinish(prompt, result);
421
+ }
422
+ setAbortController(null);
423
+ return result;
424
+ } catch (err) {
425
+ if (err.name === "AbortError") {
436
426
  setAbortController(null);
437
- return result;
438
- } catch (err) {
439
- if (err.name === "AbortError") {
440
- setAbortController(null);
441
- return null;
442
- }
443
- if (onError && err instanceof Error) {
427
+ return null;
428
+ }
429
+ if (err instanceof Error) {
430
+ if (onError) {
444
431
  onError(err);
445
432
  }
446
- throw err;
447
433
  }
448
- }),
449
- {
450
- populateCache: false,
451
- revalidate: false,
452
- // @ts-expect-error - SWR tries to be clever with the throwOnError type
453
- throwOnError: Boolean(onError)
434
+ setError(err);
435
+ } finally {
436
+ mutateLoading(false);
454
437
  }
455
- );
438
+ }
456
439
  const stop = (0, import_react2.useCallback)(() => {
457
440
  if (abortController) {
458
441
  abortController.abort();
@@ -466,13 +449,10 @@ function useCompletion({
466
449
  [mutate]
467
450
  );
468
451
  const complete = (0, import_react2.useCallback)(
469
- (prompt, options) => __async(this, null, function* () {
470
- return trigger({
471
- prompt,
472
- options
473
- });
474
- }),
475
- [trigger]
452
+ async (prompt, options) => {
453
+ return triggerRequest(prompt, options);
454
+ },
455
+ [triggerRequest]
476
456
  );
477
457
  const [input, setInput] = (0, import_react2.useState)(initialInput);
478
458
  const handleSubmit = (0, import_react2.useCallback)(
@@ -497,7 +477,7 @@ function useCompletion({
497
477
  setInput,
498
478
  handleInputChange,
499
479
  handleSubmit,
500
- isLoading: isMutating
480
+ isLoading
501
481
  };
502
482
  }
503
483
  // Annotate the CommonJS export names for ESM import in node: