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.
@@ -2,21 +2,7 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
6
  var __export = (target, all) => {
21
7
  for (var name in all)
22
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -30,26 +16,6 @@ var __copyProps = (to, from, except, desc) => {
30
16
  return to;
31
17
  };
32
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
- var __async = (__this, __arguments, generator) => {
34
- return new Promise((resolve, reject) => {
35
- var fulfilled = (value) => {
36
- try {
37
- step(generator.next(value));
38
- } catch (e) {
39
- reject(e);
40
- }
41
- };
42
- var rejected = (value) => {
43
- try {
44
- step(generator.throw(value));
45
- } catch (e) {
46
- reject(e);
47
- }
48
- };
49
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
50
- step((generator = generator.apply(__this, __arguments)).next());
51
- });
52
- };
53
19
 
54
20
  // solid/index.ts
55
21
  var solid_exports = {};
@@ -83,10 +49,10 @@ function createChunkDecoder() {
83
49
  var uniqueId = 0;
84
50
  var store = {};
85
51
  var chatApiStore = (0, import_swr_store.createSWRStore)({
86
- get: (key) => __async(void 0, null, function* () {
52
+ get: async (key) => {
87
53
  var _a;
88
54
  return (_a = store[key]) != null ? _a : [];
89
- })
55
+ }
90
56
  });
91
57
  function useChat({
92
58
  api = "/api/chat",
@@ -117,100 +83,103 @@ function useChat({
117
83
  const [error, setError] = (0, import_solid_js.createSignal)(void 0);
118
84
  const [isLoading, setIsLoading] = (0, import_solid_js.createSignal)(false);
119
85
  let abortController = null;
120
- function triggerRequest(messagesSnapshot, options) {
121
- return __async(this, null, function* () {
122
- try {
123
- setIsLoading(true);
124
- abortController = new AbortController();
125
- const previousMessages = chatApiStore.get([key], {
126
- shouldRevalidate: false
127
- });
128
- mutate(messagesSnapshot);
129
- const res = yield fetch(api, {
130
- method: "POST",
131
- body: JSON.stringify(__spreadValues(__spreadValues({
132
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
133
- role,
134
- content
135
- }))
136
- }, body), options == null ? void 0 : options.body)),
137
- headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
138
- signal: abortController.signal,
139
- credentials
140
- }).catch((err) => {
141
- if (previousMessages.status === "success") {
142
- mutate(previousMessages.data);
143
- }
144
- throw err;
145
- });
146
- if (onResponse) {
147
- try {
148
- yield onResponse(res);
149
- } catch (err) {
150
- throw err;
151
- }
86
+ async function triggerRequest(messagesSnapshot, options) {
87
+ try {
88
+ setIsLoading(true);
89
+ abortController = new AbortController();
90
+ const previousMessages = chatApiStore.get([key], {
91
+ shouldRevalidate: false
92
+ });
93
+ mutate(messagesSnapshot);
94
+ const res = await fetch(api, {
95
+ method: "POST",
96
+ body: JSON.stringify({
97
+ messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
98
+ role,
99
+ content
100
+ })),
101
+ ...body,
102
+ ...options == null ? void 0 : options.body
103
+ }),
104
+ headers: {
105
+ ...headers,
106
+ ...options == null ? void 0 : options.headers
107
+ },
108
+ signal: abortController.signal,
109
+ credentials
110
+ }).catch((err) => {
111
+ if (previousMessages.status === "success") {
112
+ mutate(previousMessages.data);
152
113
  }
153
- if (!res.ok) {
154
- if (previousMessages.status === "success") {
155
- mutate(previousMessages.data);
156
- }
157
- throw new Error(
158
- (yield res.text()) || "Failed to fetch the chat response."
159
- );
114
+ throw err;
115
+ });
116
+ if (onResponse) {
117
+ try {
118
+ await onResponse(res);
119
+ } catch (err) {
120
+ throw err;
160
121
  }
161
- if (!res.body) {
162
- throw new Error("The response body is empty.");
122
+ }
123
+ if (!res.ok) {
124
+ if (previousMessages.status === "success") {
125
+ mutate(previousMessages.data);
163
126
  }
164
- let result = "";
165
- const createdAt = /* @__PURE__ */ new Date();
166
- const replyId = nanoid();
167
- const reader = res.body.getReader();
168
- const decoder = createChunkDecoder();
169
- while (true) {
170
- const { done, value } = yield reader.read();
171
- if (done) {
172
- break;
173
- }
174
- result += decoder(value);
175
- mutate([
176
- ...messagesSnapshot,
177
- {
178
- id: replyId,
179
- createdAt,
180
- content: result,
181
- role: "assistant"
182
- }
183
- ]);
184
- if (abortController === null) {
185
- reader.cancel();
186
- break;
187
- }
127
+ throw new Error(
128
+ await res.text() || "Failed to fetch the chat response."
129
+ );
130
+ }
131
+ if (!res.body) {
132
+ throw new Error("The response body is empty.");
133
+ }
134
+ let result = "";
135
+ const createdAt = /* @__PURE__ */ new Date();
136
+ const replyId = nanoid();
137
+ const reader = res.body.getReader();
138
+ const decoder = createChunkDecoder();
139
+ while (true) {
140
+ const { done, value } = await reader.read();
141
+ if (done) {
142
+ break;
188
143
  }
189
- if (onFinish) {
190
- onFinish({
144
+ result += decoder(value);
145
+ mutate([
146
+ ...messagesSnapshot,
147
+ {
191
148
  id: replyId,
192
149
  createdAt,
193
150
  content: result,
194
151
  role: "assistant"
195
- });
152
+ }
153
+ ]);
154
+ if (abortController === null) {
155
+ reader.cancel();
156
+ break;
196
157
  }
158
+ }
159
+ if (onFinish) {
160
+ onFinish({
161
+ id: replyId,
162
+ createdAt,
163
+ content: result,
164
+ role: "assistant"
165
+ });
166
+ }
167
+ abortController = null;
168
+ return result;
169
+ } catch (err) {
170
+ if (err.name === "AbortError") {
197
171
  abortController = null;
198
- return result;
199
- } catch (err) {
200
- if (err.name === "AbortError") {
201
- abortController = null;
202
- return null;
203
- }
204
- if (onError && err instanceof Error) {
205
- onError(err);
206
- }
207
- setError(err);
208
- } finally {
209
- setIsLoading(false);
172
+ return null;
210
173
  }
211
- });
174
+ if (onError && err instanceof Error) {
175
+ onError(err);
176
+ }
177
+ setError(err);
178
+ } finally {
179
+ setIsLoading(false);
180
+ }
212
181
  }
213
- const append = (message, options) => __async(this, null, function* () {
182
+ const append = async (message, options) => {
214
183
  var _a;
215
184
  if (!message.id) {
216
185
  message.id = nanoid();
@@ -219,8 +188,8 @@ function useChat({
219
188
  ((_a = messages()) != null ? _a : []).concat(message),
220
189
  options
221
190
  );
222
- });
223
- const reload = (options) => __async(this, null, function* () {
191
+ };
192
+ const reload = async (options) => {
224
193
  const messagesSnapshot = messages();
225
194
  if (!messagesSnapshot || messagesSnapshot.length === 0)
226
195
  return null;
@@ -229,7 +198,7 @@ function useChat({
229
198
  return triggerRequest(messagesSnapshot.slice(0, -1), options);
230
199
  }
231
200
  return triggerRequest(messagesSnapshot, options);
232
- });
201
+ };
233
202
  const stop = () => {
234
203
  if (abortController) {
235
204
  abortController.abort();
@@ -273,10 +242,10 @@ var import_solid_swr_store2 = require("solid-swr-store");
273
242
  var uniqueId2 = 0;
274
243
  var store2 = {};
275
244
  var completionApiStore = (0, import_swr_store2.createSWRStore)({
276
- get: (key) => __async(void 0, null, function* () {
245
+ get: async (key) => {
277
246
  var _a;
278
247
  return (_a = store2[key]) != null ? _a : [];
279
- })
248
+ }
280
249
  });
281
250
  function useCompletion({
282
251
  api = "/api/completion",
@@ -306,75 +275,78 @@ function useCompletion({
306
275
  const [error, setError] = (0, import_solid_js2.createSignal)(void 0);
307
276
  const [isLoading, setIsLoading] = (0, import_solid_js2.createSignal)(false);
308
277
  let abortController = null;
309
- function triggerRequest(prompt, options) {
310
- return __async(this, null, function* () {
311
- try {
312
- setIsLoading(true);
313
- abortController = new AbortController();
314
- mutate("");
315
- const res = yield fetch(api, {
316
- method: "POST",
317
- body: JSON.stringify(__spreadValues(__spreadValues({
318
- prompt
319
- }, body), options == null ? void 0 : options.body)),
320
- headers: __spreadValues(__spreadValues({}, headers), options == null ? void 0 : options.headers),
321
- signal: abortController.signal,
322
- credentials
323
- }).catch((err) => {
278
+ async function triggerRequest(prompt, options) {
279
+ try {
280
+ setIsLoading(true);
281
+ abortController = new AbortController();
282
+ mutate("");
283
+ const res = await fetch(api, {
284
+ method: "POST",
285
+ body: JSON.stringify({
286
+ prompt,
287
+ ...body,
288
+ ...options == null ? void 0 : options.body
289
+ }),
290
+ headers: {
291
+ ...headers,
292
+ ...options == null ? void 0 : options.headers
293
+ },
294
+ signal: abortController.signal,
295
+ credentials
296
+ }).catch((err) => {
297
+ throw err;
298
+ });
299
+ if (onResponse) {
300
+ try {
301
+ await onResponse(res);
302
+ } catch (err) {
324
303
  throw err;
325
- });
326
- if (onResponse) {
327
- try {
328
- yield onResponse(res);
329
- } catch (err) {
330
- throw err;
331
- }
332
- }
333
- if (!res.ok) {
334
- throw new Error(
335
- (yield res.text()) || "Failed to fetch the chat response."
336
- );
337
304
  }
338
- if (!res.body) {
339
- throw new Error("The response body is empty.");
340
- }
341
- let result = "";
342
- const reader = res.body.getReader();
343
- const decoder = createChunkDecoder();
344
- while (true) {
345
- const { done, value } = yield reader.read();
346
- if (done) {
347
- break;
348
- }
349
- result += decoder(value);
350
- mutate(result);
351
- if (abortController === null) {
352
- reader.cancel();
353
- break;
354
- }
305
+ }
306
+ if (!res.ok) {
307
+ throw new Error(
308
+ await res.text() || "Failed to fetch the chat response."
309
+ );
310
+ }
311
+ if (!res.body) {
312
+ throw new Error("The response body is empty.");
313
+ }
314
+ let result = "";
315
+ const reader = res.body.getReader();
316
+ const decoder = createChunkDecoder();
317
+ while (true) {
318
+ const { done, value } = await reader.read();
319
+ if (done) {
320
+ break;
355
321
  }
356
- if (onFinish) {
357
- onFinish(prompt, result);
322
+ result += decoder(value);
323
+ mutate(result);
324
+ if (abortController === null) {
325
+ reader.cancel();
326
+ break;
358
327
  }
328
+ }
329
+ if (onFinish) {
330
+ onFinish(prompt, result);
331
+ }
332
+ abortController = null;
333
+ return result;
334
+ } catch (err) {
335
+ if (err.name === "AbortError") {
359
336
  abortController = null;
360
- return result;
361
- } catch (err) {
362
- if (err.name === "AbortError") {
363
- abortController = null;
364
- return null;
365
- }
366
- if (onError && error instanceof Error) {
367
- onError(error);
368
- }
369
- setError(err);
370
- } finally {
371
- setIsLoading(false);
337
+ return null;
372
338
  }
373
- });
339
+ if (onError && error instanceof Error) {
340
+ onError(error);
341
+ }
342
+ setError(err);
343
+ } finally {
344
+ setIsLoading(false);
345
+ }
374
346
  }
375
- const complete = (prompt, options) => __async(this, null, function* () {
347
+ const complete = async (prompt, options) => {
376
348
  return triggerRequest(prompt, options);
377
- });
349
+ };
378
350
  const stop = () => {
379
351
  if (abortController) {
380
352
  abortController.abort();