ai 2.0.1 → 2.1.2

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.
Files changed (57) hide show
  1. package/dist/index.d.ts +48 -8
  2. package/dist/index.mjs +234 -22
  3. package/package.json +20 -8
  4. package/react/dist/index.d.ts +206 -3
  5. package/react/dist/index.mjs +384 -7
  6. package/svelte/dist/index.d.ts +194 -4
  7. package/svelte/dist/index.mjs +779 -7
  8. package/{react/dist/types-f862f74a.d.ts → vue/dist/index.d.ts} +72 -1
  9. package/vue/dist/index.js +384 -0
  10. package/vue/dist/index.mjs +349 -0
  11. package/dist/ai-stream.d.ts +0 -18
  12. package/dist/ai-stream.js +0 -132
  13. package/dist/ai-stream.mjs +0 -13
  14. package/dist/anthropic-stream.d.ts +0 -5
  15. package/dist/anthropic-stream.js +0 -133
  16. package/dist/anthropic-stream.mjs +0 -8
  17. package/dist/chunk-265FSSO4.mjs +0 -91
  18. package/dist/chunk-2L3ZO4UM.mjs +0 -45
  19. package/dist/chunk-GT4HKF2X.mjs +0 -33
  20. package/dist/chunk-JGDC3BXD.mjs +0 -22
  21. package/dist/chunk-NK2CVBLI.mjs +0 -38
  22. package/dist/chunk-PEYAHBDF.mjs +0 -43
  23. package/dist/chunk-TJMME6CL.mjs +0 -24
  24. package/dist/huggingface-stream.d.ts +0 -5
  25. package/dist/huggingface-stream.js +0 -121
  26. package/dist/huggingface-stream.mjs +0 -8
  27. package/dist/index.test.d.ts +0 -2
  28. package/dist/index.test.js +0 -12
  29. package/dist/index.test.mjs +0 -10
  30. package/dist/langchain-stream.d.ts +0 -12
  31. package/dist/langchain-stream.js +0 -102
  32. package/dist/langchain-stream.mjs +0 -8
  33. package/dist/openai-stream.d.ts +0 -5
  34. package/dist/openai-stream.js +0 -144
  35. package/dist/openai-stream.mjs +0 -8
  36. package/dist/streaming-text-response.d.ts +0 -17
  37. package/dist/streaming-text-response.js +0 -75
  38. package/dist/streaming-text-response.mjs +0 -9
  39. package/react/dist/chunk-5PP6W52J.mjs +0 -202
  40. package/react/dist/chunk-6EH3SWMP.mjs +0 -55
  41. package/react/dist/chunk-PW6HSU2N.mjs +0 -154
  42. package/react/dist/use-chat.d.ts +0 -42
  43. package/react/dist/use-chat.js +0 -276
  44. package/react/dist/use-chat.mjs +0 -8
  45. package/react/dist/use-completion.d.ts +0 -47
  46. package/react/dist/use-completion.js +0 -229
  47. package/react/dist/use-completion.mjs +0 -8
  48. package/svelte/dist/chunk-6USBQIV6.mjs +0 -177
  49. package/svelte/dist/chunk-BQ64GHZ3.mjs +0 -136
  50. package/svelte/dist/chunk-CENOSGDG.mjs +0 -493
  51. package/svelte/dist/types-f862f74a.d.ts +0 -123
  52. package/svelte/dist/use-chat.d.ts +0 -39
  53. package/svelte/dist/use-chat.js +0 -680
  54. package/svelte/dist/use-chat.mjs +0 -7
  55. package/svelte/dist/use-completion.d.ts +0 -38
  56. package/svelte/dist/use-completion.js +0 -640
  57. package/svelte/dist/use-completion.mjs +0 -7
@@ -1,680 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
- 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
- var __export = (target, all) => {
21
- for (var name in all)
22
- __defProp(target, name, { get: all[name], enumerable: true });
23
- };
24
- var __copyProps = (to, from, except, desc) => {
25
- if (from && typeof from === "object" || typeof from === "function") {
26
- for (let key of __getOwnPropNames(from))
27
- if (!__hasOwnProp.call(to, key) && key !== except)
28
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
- }
30
- return to;
31
- };
32
- 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 = (x2) => x2.done ? resolve(x2.value) : Promise.resolve(x2.value).then(fulfilled, rejected);
50
- step((generator = generator.apply(__this, __arguments)).next());
51
- });
52
- };
53
-
54
- // svelte/use-chat.ts
55
- var use_chat_exports = {};
56
- __export(use_chat_exports, {
57
- useChat: () => useChat
58
- });
59
- module.exports = __toCommonJS(use_chat_exports);
60
-
61
- // ../../node_modules/.pnpm/swrev@3.0.0/node_modules/swrev/dist/swrev.es.js
62
- var __defProp2 = Object.defineProperty;
63
- var __defProps = Object.defineProperties;
64
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
65
- var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
66
- var __hasOwnProp2 = Object.prototype.hasOwnProperty;
67
- var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
68
- var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
69
- var __spreadValues2 = (a, b) => {
70
- for (var prop in b || (b = {}))
71
- if (__hasOwnProp2.call(b, prop))
72
- __defNormalProp2(a, prop, b[prop]);
73
- if (__getOwnPropSymbols2)
74
- for (var prop of __getOwnPropSymbols2(b)) {
75
- if (__propIsEnum2.call(b, prop))
76
- __defNormalProp2(a, prop, b[prop]);
77
- }
78
- return a;
79
- };
80
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
81
- var __publicField = (obj, key, value) => {
82
- __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
83
- return value;
84
- };
85
- var DefaultSWREventManager = class {
86
- constructor() {
87
- __publicField(this, "listeners", /* @__PURE__ */ new Map());
88
- }
89
- subscribe(key, listener) {
90
- if (!this.listeners.has(key))
91
- this.listeners.set(key, []);
92
- if (this.listeners.get(key).includes(listener))
93
- return;
94
- this.listeners.get(key).push(listener);
95
- }
96
- unsubscribe(key, listener) {
97
- if (!this.listeners.has(key))
98
- return;
99
- if (!this.listeners.get(key).includes(listener))
100
- return;
101
- this.listeners.get(key).splice(this.listeners.get(key).indexOf(listener), 1);
102
- if (this.listeners.get(key).length === 0)
103
- this.listeners.delete(key);
104
- }
105
- emit(key, payload) {
106
- if (!this.listeners.has(key))
107
- return;
108
- this.listeners.get(key).forEach((listener) => listener(payload));
109
- }
110
- };
111
- var defaultCacheRemoveOptions = {
112
- broadcast: false
113
- };
114
- var defaultCacheClearOptions = {
115
- broadcast: false
116
- };
117
- var CacheItem = class {
118
- constructor({ data, expiresAt = null }) {
119
- __publicField(this, "data");
120
- __publicField(this, "expiresAt");
121
- this.data = data;
122
- this.expiresAt = expiresAt;
123
- }
124
- isResolving() {
125
- return this.data instanceof Promise;
126
- }
127
- hasExpired() {
128
- return this.expiresAt === null || this.expiresAt < /* @__PURE__ */ new Date();
129
- }
130
- expiresIn(milliseconds) {
131
- this.expiresAt = /* @__PURE__ */ new Date();
132
- this.expiresAt.setMilliseconds(this.expiresAt.getMilliseconds() + milliseconds);
133
- return this;
134
- }
135
- };
136
- var DefaultCache = class {
137
- constructor() {
138
- __publicField(this, "elements", /* @__PURE__ */ new Map());
139
- __publicField(this, "event", new DefaultSWREventManager());
140
- }
141
- resolve(key, value) {
142
- Promise.resolve(value.data).then((detail) => {
143
- if (detail === void 0 || detail === null) {
144
- return this.remove(key);
145
- }
146
- value.data = detail;
147
- this.broadcast(key, detail);
148
- });
149
- }
150
- get(key) {
151
- return this.elements.get(key);
152
- }
153
- set(key, value) {
154
- this.elements.set(key, value);
155
- this.resolve(key, value);
156
- }
157
- remove(key, options) {
158
- const { broadcast } = __spreadValues2(__spreadValues2({}, defaultCacheRemoveOptions), options);
159
- if (broadcast)
160
- this.broadcast(key, void 0);
161
- this.elements.delete(key);
162
- }
163
- clear(options) {
164
- const { broadcast } = __spreadValues2(__spreadValues2({}, defaultCacheClearOptions), options);
165
- if (broadcast)
166
- for (const key of this.elements.keys())
167
- this.broadcast(key, void 0);
168
- this.elements.clear();
169
- }
170
- has(key) {
171
- return this.elements.has(key);
172
- }
173
- subscribe(key, listener) {
174
- this.event.subscribe(key, listener);
175
- }
176
- unsubscribe(key, listener) {
177
- this.event.unsubscribe(key, listener);
178
- }
179
- broadcast(key, data) {
180
- this.event.emit(key, data);
181
- }
182
- };
183
- var defaultOptions = {
184
- cache: new DefaultCache(),
185
- errors: new DefaultSWREventManager(),
186
- fetcher: (url) => __async(void 0, null, function* () {
187
- const response = yield fetch(url);
188
- if (!response.ok)
189
- throw Error("Not a 2XX response.");
190
- return response.json();
191
- }),
192
- initialData: void 0,
193
- loadInitialCache: true,
194
- revalidateOnStart: true,
195
- dedupingInterval: 2e3,
196
- revalidateOnFocus: true,
197
- focusThrottleInterval: 5e3,
198
- revalidateOnReconnect: true,
199
- reconnectWhen: (notify, { enabled }) => {
200
- if (enabled && typeof window !== "undefined") {
201
- window.addEventListener("online", notify);
202
- return () => window.removeEventListener("online", notify);
203
- }
204
- return () => {
205
- };
206
- },
207
- focusWhen: (notify, { enabled, throttleInterval }) => {
208
- if (enabled && typeof window !== "undefined") {
209
- let lastFocus = null;
210
- const rawHandler = () => {
211
- const now = Date.now();
212
- if (lastFocus === null || now - lastFocus > throttleInterval) {
213
- lastFocus = now;
214
- notify();
215
- }
216
- };
217
- window.addEventListener("focus", rawHandler);
218
- return () => window.removeEventListener("focus", rawHandler);
219
- }
220
- return () => {
221
- };
222
- }
223
- };
224
- var defaultRevalidateOptions = __spreadProps(__spreadValues2({}, defaultOptions), {
225
- force: false
226
- });
227
- var defaultMutateOptions = {
228
- revalidate: true,
229
- revalidateOptions: __spreadValues2({}, defaultRevalidateOptions)
230
- };
231
- var defaultClearOptions = {
232
- broadcast: false
233
- };
234
- var SWR = class {
235
- constructor(options) {
236
- __publicField(this, "options");
237
- this.options = __spreadValues2(__spreadValues2({}, defaultOptions), options);
238
- }
239
- get cache() {
240
- return this.options.cache;
241
- }
242
- get errors() {
243
- return this.options.errors;
244
- }
245
- requestData(key, fetcher) {
246
- return Promise.resolve(fetcher(key)).catch((data) => {
247
- this.errors.emit(key, data);
248
- return void 0;
249
- });
250
- }
251
- resolveKey(key) {
252
- if (typeof key === "function") {
253
- try {
254
- return key();
255
- } catch (e) {
256
- return void 0;
257
- }
258
- }
259
- return key;
260
- }
261
- clear(keys, options) {
262
- const ops = __spreadValues2(__spreadValues2({}, defaultClearOptions), options);
263
- if (keys === void 0 || keys === null)
264
- return this.cache.clear(ops);
265
- if (!Array.isArray(keys))
266
- return this.cache.remove(keys, ops);
267
- for (const key of keys)
268
- this.cache.remove(key, ops);
269
- }
270
- revalidate(key, options) {
271
- if (!key)
272
- return;
273
- const { fetcher: defaultFetcher, dedupingInterval: defaultDedupingInterval } = this.options;
274
- const { force, fetcher, dedupingInterval } = __spreadValues2(__spreadValues2(__spreadValues2({}, defaultRevalidateOptions), { fetcher: defaultFetcher, dedupingInterval: defaultDedupingInterval }), options);
275
- let data = void 0;
276
- if (force || !this.cache.has(key) || this.cache.has(key) && this.cache.get(key).hasExpired()) {
277
- data = this.requestData(key, fetcher);
278
- }
279
- if (data !== void 0) {
280
- this.mutate(key, new CacheItem({ data }).expiresIn(dedupingInterval), {
281
- revalidate: false
282
- });
283
- }
284
- }
285
- mutate(key, value, options) {
286
- if (!key)
287
- return;
288
- const { revalidate: revalidateAfterMutation, revalidateOptions } = __spreadValues2(__spreadValues2({}, defaultMutateOptions), options);
289
- let data;
290
- if (typeof value === "function") {
291
- let state = null;
292
- if (this.cache.has(key)) {
293
- const item = this.cache.get(key);
294
- if (!item.isResolving())
295
- state = item.data;
296
- }
297
- data = value(state);
298
- } else {
299
- data = value;
300
- }
301
- this.cache.set(key, data instanceof CacheItem ? data : new CacheItem({ data }));
302
- if (revalidateAfterMutation)
303
- this.revalidate(key, revalidateOptions);
304
- }
305
- subscribeData(key, onData) {
306
- if (key) {
307
- const handler = (payload) => onData(payload);
308
- this.cache.subscribe(key, handler);
309
- return () => this.cache.unsubscribe(key, handler);
310
- }
311
- return () => {
312
- };
313
- }
314
- subscribeErrors(key, onError) {
315
- if (key) {
316
- const handler = (payload) => onError(payload);
317
- this.errors.subscribe(key, handler);
318
- return () => this.errors.unsubscribe(key, handler);
319
- }
320
- return () => {
321
- };
322
- }
323
- get(key) {
324
- if (key && this.cache.has(key)) {
325
- const item = this.cache.get(key);
326
- if (!item.isResolving())
327
- return item.data;
328
- }
329
- return void 0;
330
- }
331
- getWait(key) {
332
- return new Promise((resolve, reject) => {
333
- const unsubscribe = this.subscribeData(key, (data) => {
334
- unsubscribe();
335
- return resolve(data);
336
- });
337
- const unsubscribeErrors = this.subscribeErrors(key, (error) => {
338
- unsubscribeErrors();
339
- return reject(error);
340
- });
341
- const current = this.get(key);
342
- if (current)
343
- return resolve(current);
344
- });
345
- }
346
- subscribe(key, onData, onError, options) {
347
- const {
348
- fetcher,
349
- initialData,
350
- loadInitialCache,
351
- revalidateOnStart,
352
- dedupingInterval,
353
- revalidateOnFocus,
354
- focusThrottleInterval,
355
- revalidateOnReconnect,
356
- reconnectWhen,
357
- focusWhen
358
- } = __spreadValues2(__spreadValues2({}, this.options), options);
359
- const mutateCurrent = (value, options2) => {
360
- return this.mutate(this.resolveKey(key), value, options2);
361
- };
362
- const revalidateCurrent = (options2) => {
363
- return this.revalidate(this.resolveKey(key), options2);
364
- };
365
- const revalidateCurrentWithOptions = () => {
366
- return revalidateCurrent({ fetcher, dedupingInterval });
367
- };
368
- if (revalidateOnStart)
369
- revalidateCurrentWithOptions();
370
- const unsubscribeData = this.subscribeData(this.resolveKey(key), onData);
371
- const unsubscribeErrors = this.subscribeErrors(this.resolveKey(key), onError);
372
- const unsubscribeVisibility = focusWhen(revalidateCurrentWithOptions, {
373
- throttleInterval: focusThrottleInterval,
374
- enabled: revalidateOnFocus
375
- });
376
- const unsubscribeNetwork = reconnectWhen(revalidateCurrentWithOptions, {
377
- enabled: revalidateOnReconnect
378
- });
379
- const unsubscribe = () => {
380
- unsubscribeData();
381
- unsubscribeErrors();
382
- unsubscribeVisibility == null ? void 0 : unsubscribeVisibility();
383
- unsubscribeNetwork == null ? void 0 : unsubscribeNetwork();
384
- };
385
- if (initialData) {
386
- mutateCurrent(initialData, { revalidate: false });
387
- }
388
- if (loadInitialCache) {
389
- const cachedData = this.get(this.resolveKey(key));
390
- if (cachedData)
391
- onData(cachedData);
392
- }
393
- return { unsubscribe };
394
- }
395
- };
396
-
397
- // ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.59.1/node_modules/sswr/dist/sswr.mjs
398
- var import_svelte = require("svelte");
399
- function h() {
400
- }
401
- function E(t) {
402
- return t();
403
- }
404
- function q(t) {
405
- t.forEach(E);
406
- }
407
- function w(t) {
408
- return typeof t == "function";
409
- }
410
- function K(t, e) {
411
- return t != t ? e == e : t !== e || t && typeof t == "object" || typeof t == "function";
412
- }
413
- function x(t, ...e) {
414
- if (t == null)
415
- return h;
416
- const n = t.subscribe(...e);
417
- return n.unsubscribe ? () => n.unsubscribe() : n;
418
- }
419
- Promise.resolve();
420
- var v = [];
421
- function z(t, e) {
422
- return {
423
- subscribe: m(t, e).subscribe
424
- };
425
- }
426
- function m(t, e = h) {
427
- let n;
428
- const r = /* @__PURE__ */ new Set();
429
- function c(i) {
430
- if (K(t, i) && (t = i, n)) {
431
- const f = !v.length;
432
- for (const s of r)
433
- s[1](), v.push(s, t);
434
- if (f) {
435
- for (let s = 0; s < v.length; s += 2)
436
- v[s][0](v[s + 1]);
437
- v.length = 0;
438
- }
439
- }
440
- }
441
- function l(i) {
442
- c(i(t));
443
- }
444
- function d(i, f = h) {
445
- const s = [i, f];
446
- return r.add(s), r.size === 1 && (n = e(c) || h), i(t), () => {
447
- r.delete(s), r.size === 0 && (n(), n = null);
448
- };
449
- }
450
- return { set: c, update: l, subscribe: d };
451
- }
452
- function S(t, e, n) {
453
- const r = !Array.isArray(t), c = r ? [t] : t, l = e.length < 2;
454
- return z(n, (d) => {
455
- let i = false;
456
- const f = [];
457
- let s = 0, p = h;
458
- const o = () => {
459
- if (s)
460
- return;
461
- p();
462
- const a = e(r ? f[0] : f, d);
463
- l ? d(a) : p = w(a) ? a : h;
464
- }, b = c.map((a, g) => x(a, (y) => {
465
- f[g] = y, s &= ~(1 << g), i && o();
466
- }, () => {
467
- s |= 1 << g;
468
- }));
469
- return i = true, o(), function() {
470
- q(b), p();
471
- };
472
- });
473
- }
474
- var A = class extends SWR {
475
- /**
476
- * Svelte specific use of SWR.
477
- */
478
- useSWR(e, n) {
479
- let r;
480
- const c = m(void 0, () => () => r == null ? void 0 : r()), l = m(void 0, () => () => r == null ? void 0 : r());
481
- (0, import_svelte.beforeUpdate)(() => {
482
- const o = (a) => {
483
- l.set(void 0), c.set(a);
484
- }, b = (a) => l.set(a);
485
- r || (r = this.subscribe(e, o, b, __spreadValues({
486
- loadInitialCache: true
487
- }, n)).unsubscribe);
488
- }), (0, import_svelte.onDestroy)(() => r == null ? void 0 : r());
489
- const d = (o, b) => this.mutate(this.resolveKey(e), o, __spreadValues({
490
- revalidateOptions: n
491
- }, b)), i = (o) => this.revalidate(this.resolveKey(e), __spreadValues(__spreadValues({}, n), o)), f = (o) => this.clear(this.resolveKey(e), o), s = S([c, l], ([o, b]) => o === void 0 && b === void 0), p = S([c, l], ([o, b]) => o !== void 0 && b === void 0);
492
- return { data: c, error: l, mutate: d, revalidate: i, clear: f, isLoading: s, isValid: p };
493
- }
494
- };
495
- var W = (t) => new A(t);
496
- var u = W();
497
- var $ = (t, e) => u.useSWR(t, e);
498
-
499
- // svelte/use-chat.ts
500
- var import_store = require("svelte/store");
501
-
502
- // shared/utils.ts
503
- var import_nanoid = require("nanoid");
504
- var nanoid = (0, import_nanoid.customAlphabet)(
505
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
506
- 7
507
- );
508
- var decoder = new TextDecoder();
509
- function decodeAIStreamChunk(chunk) {
510
- return decoder.decode(chunk);
511
- }
512
-
513
- // svelte/use-chat.ts
514
- var uniqueId = 0;
515
- var store = {};
516
- function useChat({
517
- api = "/api/chat",
518
- id,
519
- initialMessages = [],
520
- initialInput = "",
521
- sendExtraMessageFields,
522
- onResponse,
523
- onFinish,
524
- onError,
525
- headers,
526
- body
527
- } = {}) {
528
- const chatId = id || `chat-${uniqueId++}`;
529
- const key = `${api}|${chatId}`;
530
- const { data, mutate: originalMutate } = $(key, {
531
- fetcher: () => store[key] || initialMessages,
532
- initialData: initialMessages
533
- });
534
- data.set(initialMessages);
535
- const mutate = (data2) => {
536
- store[key] = data2;
537
- return originalMutate(data2);
538
- };
539
- const messages = data;
540
- const error = (0, import_store.writable)(void 0);
541
- const isLoading = (0, import_store.writable)(false);
542
- let abortController = null;
543
- function triggerRequest(messagesSnapshot) {
544
- return __async(this, null, function* () {
545
- try {
546
- isLoading.set(true);
547
- abortController = new AbortController();
548
- const previousMessages = (0, import_store.get)(messages);
549
- mutate(messagesSnapshot);
550
- const res = yield fetch(api, {
551
- method: "POST",
552
- body: JSON.stringify(__spreadValues({
553
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
554
- role,
555
- content
556
- }))
557
- }, body)),
558
- headers: headers || {},
559
- signal: abortController.signal
560
- }).catch((err) => {
561
- mutate(previousMessages);
562
- throw err;
563
- });
564
- if (onResponse) {
565
- try {
566
- yield onResponse(res);
567
- } catch (err) {
568
- throw err;
569
- }
570
- }
571
- if (!res.ok) {
572
- mutate(previousMessages);
573
- throw new Error(
574
- (yield res.text()) || "Failed to fetch the chat response."
575
- );
576
- }
577
- if (!res.body) {
578
- throw new Error("The response body is empty.");
579
- }
580
- let result = "";
581
- const createdAt = /* @__PURE__ */ new Date();
582
- const replyId = nanoid();
583
- const reader = res.body.getReader();
584
- while (true) {
585
- const { done, value } = yield reader.read();
586
- if (done) {
587
- break;
588
- }
589
- result += decodeAIStreamChunk(value);
590
- mutate([
591
- ...messagesSnapshot,
592
- {
593
- id: replyId,
594
- createdAt,
595
- content: result,
596
- role: "assistant"
597
- }
598
- ]);
599
- if (abortController === null) {
600
- reader.cancel();
601
- break;
602
- }
603
- }
604
- if (onFinish) {
605
- onFinish({
606
- id: replyId,
607
- createdAt,
608
- content: result,
609
- role: "assistant"
610
- });
611
- }
612
- abortController = null;
613
- return result;
614
- } catch (err) {
615
- if (err.name === "AbortError") {
616
- abortController = null;
617
- return null;
618
- }
619
- if (onError && err instanceof Error) {
620
- onError(err);
621
- }
622
- error.set(err);
623
- } finally {
624
- isLoading.set(false);
625
- }
626
- });
627
- }
628
- const append = (message) => __async(this, null, function* () {
629
- if (!message.id) {
630
- message.id = nanoid();
631
- }
632
- return triggerRequest((0, import_store.get)(messages).concat(message));
633
- });
634
- const reload = () => __async(this, null, function* () {
635
- const messagesSnapshot = (0, import_store.get)(messages);
636
- if (messagesSnapshot.length === 0)
637
- return null;
638
- const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
639
- if (lastMessage.role === "assistant") {
640
- return triggerRequest(messagesSnapshot.slice(0, -1));
641
- }
642
- return triggerRequest(messagesSnapshot);
643
- });
644
- const stop = () => {
645
- if (abortController) {
646
- abortController.abort();
647
- abortController = null;
648
- }
649
- };
650
- const setMessages = (messages2) => {
651
- mutate(messages2);
652
- };
653
- const input = (0, import_store.writable)(initialInput);
654
- const handleSubmit = (e) => {
655
- e.preventDefault();
656
- const inputValue = (0, import_store.get)(input);
657
- if (!inputValue)
658
- return;
659
- append({
660
- content: inputValue,
661
- role: "user"
662
- });
663
- input.set("");
664
- };
665
- return {
666
- messages,
667
- append,
668
- error,
669
- reload,
670
- stop,
671
- setMessages,
672
- input,
673
- handleSubmit,
674
- isLoading
675
- };
676
- }
677
- // Annotate the CommonJS export names for ESM import in node:
678
- 0 && (module.exports = {
679
- useChat
680
- });
@@ -1,7 +0,0 @@
1
- import {
2
- useChat
3
- } from "./chunk-6USBQIV6.mjs";
4
- import "./chunk-CENOSGDG.mjs";
5
- export {
6
- useChat
7
- };