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.
@@ -1,40 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __async = (__this, __arguments, generator) => {
18
- return new Promise((resolve, reject) => {
19
- var fulfilled = (value) => {
20
- try {
21
- step(generator.next(value));
22
- } catch (e) {
23
- reject(e);
24
- }
25
- };
26
- var rejected = (value) => {
27
- try {
28
- step(generator.throw(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
- step((generator = generator.apply(__this, __arguments)).next());
35
- });
36
- };
37
-
38
1
  // vue/use-chat.ts
39
2
  import swrv from "swrv";
40
3
  import { ref } from "vue";
@@ -77,6 +40,9 @@ function useChat({
77
40
  key,
78
41
  () => store[key] || initialMessages
79
42
  );
43
+ const { data: isLoading, mutate: mutateLoading } = useSWRV(
44
+ `${key}-loading`
45
+ );
80
46
  data.value || (data.value = initialMessages);
81
47
  const mutate = (data2) => {
82
48
  store[key] = data2;
@@ -84,102 +50,104 @@ function useChat({
84
50
  };
85
51
  const messages = data;
86
52
  const error = ref(void 0);
87
- const isLoading = ref(false);
88
53
  let abortController = null;
89
- function triggerRequest(messagesSnapshot, options) {
90
- return __async(this, null, function* () {
91
- try {
92
- isLoading.value = true;
93
- abortController = new AbortController();
94
- const previousMessages = messages.value;
95
- mutate(messagesSnapshot);
96
- const res = yield fetch(api, {
97
- method: "POST",
98
- body: JSON.stringify(__spreadValues(__spreadValues({
99
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
100
- role,
101
- content
102
- }))
103
- }, body), options == null ? void 0 : options.body)),
104
- headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
105
- signal: abortController.signal,
106
- credentials
107
- }).catch((err) => {
108
- mutate(previousMessages);
54
+ async function triggerRequest(messagesSnapshot, options) {
55
+ try {
56
+ mutateLoading(() => true);
57
+ abortController = new AbortController();
58
+ const previousMessages = messages.value;
59
+ mutate(messagesSnapshot);
60
+ const res = await fetch(api, {
61
+ method: "POST",
62
+ body: JSON.stringify({
63
+ messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
64
+ role,
65
+ content
66
+ })),
67
+ ...body,
68
+ ...options == null ? void 0 : options.body
69
+ }),
70
+ headers: {
71
+ ...headers,
72
+ ...options == null ? void 0 : options.headers
73
+ },
74
+ signal: abortController.signal,
75
+ credentials
76
+ }).catch((err) => {
77
+ mutate(previousMessages);
78
+ throw err;
79
+ });
80
+ if (onResponse) {
81
+ try {
82
+ await onResponse(res);
83
+ } catch (err) {
109
84
  throw err;
110
- });
111
- if (onResponse) {
112
- try {
113
- yield onResponse(res);
114
- } catch (err) {
115
- throw err;
116
- }
117
85
  }
118
- if (!res.ok) {
119
- mutate(previousMessages);
120
- throw new Error(
121
- (yield res.text()) || "Failed to fetch the chat response."
122
- );
123
- }
124
- if (!res.body) {
125
- throw new Error("The response body is empty.");
126
- }
127
- let result = "";
128
- const createdAt = /* @__PURE__ */ new Date();
129
- const replyId = nanoid();
130
- const reader = res.body.getReader();
131
- const decoder = createChunkDecoder();
132
- while (true) {
133
- const { done, value } = yield reader.read();
134
- if (done) {
135
- break;
136
- }
137
- result += decoder(value);
138
- mutate([
139
- ...messagesSnapshot,
140
- {
141
- id: replyId,
142
- createdAt,
143
- content: result,
144
- role: "assistant"
145
- }
146
- ]);
147
- if (abortController === null) {
148
- reader.cancel();
149
- break;
150
- }
86
+ }
87
+ if (!res.ok) {
88
+ mutate(previousMessages);
89
+ throw new Error(
90
+ await res.text() || "Failed to fetch the chat response."
91
+ );
92
+ }
93
+ if (!res.body) {
94
+ throw new Error("The response body is empty.");
95
+ }
96
+ let result = "";
97
+ const createdAt = /* @__PURE__ */ new Date();
98
+ const replyId = nanoid();
99
+ const reader = res.body.getReader();
100
+ const decoder = createChunkDecoder();
101
+ while (true) {
102
+ const { done, value } = await reader.read();
103
+ if (done) {
104
+ break;
151
105
  }
152
- if (onFinish) {
153
- onFinish({
106
+ result += decoder(value);
107
+ mutate([
108
+ ...messagesSnapshot,
109
+ {
154
110
  id: replyId,
155
111
  createdAt,
156
112
  content: result,
157
113
  role: "assistant"
158
- });
114
+ }
115
+ ]);
116
+ if (abortController === null) {
117
+ reader.cancel();
118
+ break;
159
119
  }
120
+ }
121
+ if (onFinish) {
122
+ onFinish({
123
+ id: replyId,
124
+ createdAt,
125
+ content: result,
126
+ role: "assistant"
127
+ });
128
+ }
129
+ abortController = null;
130
+ return result;
131
+ } catch (err) {
132
+ if (err.name === "AbortError") {
160
133
  abortController = null;
161
- return result;
162
- } catch (err) {
163
- if (err.name === "AbortError") {
164
- abortController = null;
165
- return null;
166
- }
167
- if (onError && err instanceof Error) {
168
- onError(err);
169
- }
170
- error.value = err;
171
- } finally {
172
- isLoading.value = false;
134
+ return null;
173
135
  }
174
- });
136
+ if (onError && err instanceof Error) {
137
+ onError(err);
138
+ }
139
+ error.value = err;
140
+ } finally {
141
+ mutateLoading(() => false);
142
+ }
175
143
  }
176
- const append = (message, options) => __async(this, null, function* () {
144
+ const append = async (message, options) => {
177
145
  if (!message.id) {
178
146
  message.id = nanoid();
179
147
  }
180
148
  return triggerRequest(messages.value.concat(message), options);
181
- });
182
- const reload = (options) => __async(this, null, function* () {
149
+ };
150
+ const reload = async (options) => {
183
151
  const messagesSnapshot = messages.value;
184
152
  if (messagesSnapshot.length === 0)
185
153
  return null;
@@ -188,7 +156,7 @@ function useChat({
188
156
  return triggerRequest(messagesSnapshot.slice(0, -1), options);
189
157
  }
190
158
  return triggerRequest(messagesSnapshot, options);
191
- });
159
+ };
192
160
  const stop = () => {
193
161
  if (abortController) {
194
162
  abortController.abort();
@@ -247,6 +215,9 @@ function useCompletion({
247
215
  key,
248
216
  () => store2[key] || initialCompletion
249
217
  );
218
+ const { data: isLoading, mutate: mutateLoading } = useSWRV2(
219
+ `${key}-loading`
220
+ );
250
221
  data.value || (data.value = initialCompletion);
251
222
  const mutate = (data2) => {
252
223
  store2[key] = data2;
@@ -254,77 +225,79 @@ function useCompletion({
254
225
  };
255
226
  const completion = data;
256
227
  const error = ref2(void 0);
257
- const isLoading = ref2(false);
258
228
  let abortController = null;
259
- function triggerRequest(prompt, options) {
260
- return __async(this, null, function* () {
261
- try {
262
- isLoading.value = true;
263
- abortController = new AbortController();
264
- mutate("");
265
- const res = yield fetch(api, {
266
- method: "POST",
267
- body: JSON.stringify(__spreadValues(__spreadValues({
268
- prompt
269
- }, body), options == null ? void 0 : options.body)),
270
- headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
271
- signal: abortController.signal,
272
- credentials
273
- }).catch((err) => {
229
+ async function triggerRequest(prompt, options) {
230
+ try {
231
+ mutateLoading(() => true);
232
+ abortController = new AbortController();
233
+ mutate("");
234
+ const res = await fetch(api, {
235
+ method: "POST",
236
+ body: JSON.stringify({
237
+ prompt,
238
+ ...body,
239
+ ...options == null ? void 0 : options.body
240
+ }),
241
+ headers: {
242
+ ...headers,
243
+ ...options == null ? void 0 : options.headers
244
+ },
245
+ signal: abortController.signal,
246
+ credentials
247
+ }).catch((err) => {
248
+ throw err;
249
+ });
250
+ if (onResponse) {
251
+ try {
252
+ await onResponse(res);
253
+ } catch (err) {
274
254
  throw err;
275
- });
276
- if (onResponse) {
277
- try {
278
- yield onResponse(res);
279
- } catch (err) {
280
- throw err;
281
- }
282
- }
283
- if (!res.ok) {
284
- throw new Error(
285
- (yield res.text()) || "Failed to fetch the chat response."
286
- );
287
- }
288
- if (!res.body) {
289
- throw new Error("The response body is empty.");
290
255
  }
291
- let result = "";
292
- const reader = res.body.getReader();
293
- const decoder = createChunkDecoder();
294
- while (true) {
295
- const { done, value } = yield reader.read();
296
- if (done) {
297
- break;
298
- }
299
- result += decoder(value);
300
- mutate(result);
301
- if (abortController === null) {
302
- reader.cancel();
303
- break;
304
- }
256
+ }
257
+ if (!res.ok) {
258
+ throw new Error(
259
+ await res.text() || "Failed to fetch the chat response."
260
+ );
261
+ }
262
+ if (!res.body) {
263
+ throw new Error("The response body is empty.");
264
+ }
265
+ let result = "";
266
+ const reader = res.body.getReader();
267
+ const decoder = createChunkDecoder();
268
+ while (true) {
269
+ const { done, value } = await reader.read();
270
+ if (done) {
271
+ break;
305
272
  }
306
- if (onFinish) {
307
- onFinish(prompt, result);
273
+ result += decoder(value);
274
+ mutate(result);
275
+ if (abortController === null) {
276
+ reader.cancel();
277
+ break;
308
278
  }
279
+ }
280
+ if (onFinish) {
281
+ onFinish(prompt, result);
282
+ }
283
+ abortController = null;
284
+ return result;
285
+ } catch (err) {
286
+ if (err.name === "AbortError") {
309
287
  abortController = null;
310
- return result;
311
- } catch (err) {
312
- if (err.name === "AbortError") {
313
- abortController = null;
314
- return null;
315
- }
316
- if (onError && error instanceof Error) {
317
- onError(error);
318
- }
319
- error.value = err;
320
- } finally {
321
- isLoading.value = false;
288
+ return null;
322
289
  }
323
- });
290
+ if (onError && error instanceof Error) {
291
+ onError(error);
292
+ }
293
+ error.value = err;
294
+ } finally {
295
+ mutateLoading(() => false);
296
+ }
324
297
  }
325
- const complete = (prompt, options) => __async(this, null, function* () {
298
+ const complete = async (prompt, options) => {
326
299
  return triggerRequest(prompt, options);
327
- });
300
+ };
328
301
  const stop = () => {
329
302
  if (abortController) {
330
303
  abortController.abort();