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,38 +0,0 @@
1
- import { Readable, Writable } from 'svelte/store';
2
- import { a as UseCompletionOptions } from './types-f862f74a.js';
3
-
4
- type UseCompletionHelpers = {
5
- /** The current completion result */
6
- completion: Readable<string>;
7
- /** The error object of the API request */
8
- error: Readable<undefined | Error>;
9
- /**
10
- * Send a new prompt to the API endpoint and update the completion state.
11
- */
12
- complete: (prompt: string) => Promise<string | null | undefined>;
13
- /**
14
- * Abort the current API request but keep the generated tokens.
15
- */
16
- stop: () => void;
17
- /**
18
- * Update the `completion` state locally.
19
- */
20
- setCompletion: (completion: string) => void;
21
- /** The current value of the input */
22
- input: Writable<string>;
23
- /**
24
- * Form submission handler to automattically reset input and append a user message
25
- * @example
26
- * ```jsx
27
- * <form onSubmit={handleSubmit}>
28
- * <input onChange={handleInputChange} value={input} />
29
- * </form>
30
- * ```
31
- */
32
- handleSubmit: (e: any) => void;
33
- /** Whether the API request is in progress */
34
- isLoading: Writable<boolean>;
35
- };
36
- declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
37
-
38
- export { UseCompletionHelpers, useCompletion };
@@ -1,640 +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-completion.ts
55
- var use_completion_exports = {};
56
- __export(use_completion_exports, {
57
- useCompletion: () => useCompletion
58
- });
59
- module.exports = __toCommonJS(use_completion_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-completion.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-completion.ts
514
- var uniqueId = 0;
515
- var store = {};
516
- function useCompletion({
517
- api = "/api/completion",
518
- id,
519
- initialCompletion = "",
520
- initialInput = "",
521
- headers,
522
- body,
523
- onResponse,
524
- onFinish,
525
- onError
526
- } = {}) {
527
- const completionId = id || `completion-${uniqueId++}`;
528
- const key = `${api}|${completionId}`;
529
- const { data, mutate: originalMutate } = $(key, {
530
- fetcher: () => store[key] || initialCompletion,
531
- initialData: initialCompletion
532
- });
533
- data.set(initialCompletion);
534
- const mutate = (data2) => {
535
- store[key] = data2;
536
- return originalMutate(data2);
537
- };
538
- const completion = data;
539
- const error = (0, import_store.writable)(void 0);
540
- const isLoading = (0, import_store.writable)(false);
541
- let abortController = null;
542
- function triggerRequest(prompt) {
543
- return __async(this, null, function* () {
544
- try {
545
- isLoading.set(true);
546
- abortController = new AbortController();
547
- mutate("");
548
- const res = yield fetch(api, {
549
- method: "POST",
550
- body: JSON.stringify(__spreadValues({
551
- prompt
552
- }, body)),
553
- headers: headers || {},
554
- signal: abortController.signal
555
- }).catch((err) => {
556
- throw err;
557
- });
558
- if (onResponse) {
559
- try {
560
- yield onResponse(res);
561
- } catch (err) {
562
- throw err;
563
- }
564
- }
565
- if (!res.ok) {
566
- throw new Error(
567
- (yield res.text()) || "Failed to fetch the chat response."
568
- );
569
- }
570
- if (!res.body) {
571
- throw new Error("The response body is empty.");
572
- }
573
- let result = "";
574
- const reader = res.body.getReader();
575
- while (true) {
576
- const { done, value } = yield reader.read();
577
- if (done) {
578
- break;
579
- }
580
- result += decodeAIStreamChunk(value);
581
- mutate(result);
582
- if (abortController === null) {
583
- reader.cancel();
584
- break;
585
- }
586
- }
587
- if (onFinish) {
588
- onFinish(prompt, result);
589
- }
590
- abortController = null;
591
- return result;
592
- } catch (err) {
593
- if (err.name === "AbortError") {
594
- abortController = null;
595
- return null;
596
- }
597
- if (onError && error instanceof Error) {
598
- onError(error);
599
- }
600
- error.set(err);
601
- } finally {
602
- isLoading.set(false);
603
- }
604
- });
605
- }
606
- const complete = (prompt) => __async(this, null, function* () {
607
- return triggerRequest(prompt);
608
- });
609
- const stop = () => {
610
- if (abortController) {
611
- abortController.abort();
612
- abortController = null;
613
- }
614
- };
615
- const setCompletion = (completion2) => {
616
- mutate(completion2);
617
- };
618
- const input = (0, import_store.writable)(initialInput);
619
- const handleSubmit = (e) => {
620
- e.preventDefault();
621
- const inputValue = (0, import_store.get)(input);
622
- if (!inputValue)
623
- return;
624
- return complete(inputValue);
625
- };
626
- return {
627
- completion,
628
- complete,
629
- error,
630
- stop,
631
- setCompletion,
632
- input,
633
- handleSubmit,
634
- isLoading
635
- };
636
- }
637
- // Annotate the CommonJS export names for ESM import in node:
638
- 0 && (module.exports = {
639
- useCompletion
640
- });
@@ -1,7 +0,0 @@
1
- import {
2
- useCompletion
3
- } from "./chunk-BQ64GHZ3.mjs";
4
- import "./chunk-CENOSGDG.mjs";
5
- export {
6
- useCompletion
7
- };