ai 2.0.0 → 2.1.0

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 (34) hide show
  1. package/dist/ai-stream.mjs +4 -2
  2. package/dist/anthropic-stream.mjs +6 -4
  3. package/dist/chunk-2JQWCLY2.mjs +70 -0
  4. package/dist/chunk-7KLTYB74.mjs +70 -0
  5. package/dist/{chunk-TJMME6CL.mjs → chunk-BJMBMGA3.mjs} +12 -2
  6. package/dist/{chunk-PEYAHBDF.mjs → chunk-KKQRUR3E.mjs} +12 -4
  7. package/dist/{chunk-NK2CVBLI.mjs → chunk-RBP6ONSV.mjs} +19 -12
  8. package/dist/{chunk-JGDC3BXD.mjs → chunk-TWW2ODJW.mjs} +13 -3
  9. package/dist/{chunk-EZJ7FC5E.mjs → chunk-U2OQ6HW6.mjs} +14 -6
  10. package/dist/{chunk-265FSSO4.mjs → chunk-UJV6VDVU.mjs} +9 -3
  11. package/dist/huggingface-stream.mjs +6 -4
  12. package/dist/index.js +1 -1
  13. package/dist/index.mjs +11 -7
  14. package/dist/index.test.js +562 -6
  15. package/dist/index.test.mjs +283 -7
  16. package/dist/langchain-stream.d.ts +1 -1
  17. package/dist/langchain-stream.js +1 -1
  18. package/dist/langchain-stream.mjs +6 -4
  19. package/dist/openai-stream.mjs +6 -4
  20. package/dist/streaming-text-response.mjs +4 -2
  21. package/package.json +18 -6
  22. package/vue/dist/chunk-FT26CHLO.mjs +137 -0
  23. package/vue/dist/chunk-OYI6GFBM.mjs +178 -0
  24. package/{dist/chunk-2L3ZO4UM.mjs → vue/dist/chunk-WXH4YPZV.mjs} +14 -5
  25. package/vue/dist/index.d.ts +4 -0
  26. package/vue/dist/index.js +384 -0
  27. package/vue/dist/index.mjs +11 -0
  28. package/vue/dist/types-f862f74a.d.ts +123 -0
  29. package/vue/dist/use-chat.d.ts +39 -0
  30. package/vue/dist/use-chat.js +252 -0
  31. package/vue/dist/use-chat.mjs +7 -0
  32. package/vue/dist/use-completion.d.ts +38 -0
  33. package/vue/dist/use-completion.js +212 -0
  34. package/vue/dist/use-completion.mjs +7 -0
@@ -0,0 +1,178 @@
1
+ import {
2
+ __async,
3
+ __spreadValues,
4
+ decodeAIStreamChunk,
5
+ nanoid
6
+ } from "./chunk-WXH4YPZV.mjs";
7
+
8
+ // vue/use-chat.ts
9
+ import swrv from "swrv";
10
+ import { ref } from "vue";
11
+ var uniqueId = 0;
12
+ var useSWRV = swrv.default || swrv;
13
+ var store = {};
14
+ function useChat({
15
+ api = "/api/chat",
16
+ id,
17
+ initialMessages = [],
18
+ initialInput = "",
19
+ sendExtraMessageFields,
20
+ onResponse,
21
+ onFinish,
22
+ onError,
23
+ headers,
24
+ body
25
+ } = {}) {
26
+ const chatId = id || `chat-${uniqueId++}`;
27
+ const key = `${api}|${chatId}`;
28
+ const { data, mutate: originalMutate } = useSWRV(
29
+ key,
30
+ () => store[key] || initialMessages
31
+ );
32
+ data.value || (data.value = initialMessages);
33
+ const mutate = (data2) => {
34
+ store[key] = data2;
35
+ return originalMutate();
36
+ };
37
+ const messages = data;
38
+ const error = ref(void 0);
39
+ const isLoading = ref(false);
40
+ let abortController = null;
41
+ function triggerRequest(messagesSnapshot) {
42
+ return __async(this, null, function* () {
43
+ try {
44
+ isLoading.value = true;
45
+ abortController = new AbortController();
46
+ const previousMessages = messages.value;
47
+ mutate(messagesSnapshot);
48
+ const res = yield fetch(api, {
49
+ method: "POST",
50
+ body: JSON.stringify(__spreadValues({
51
+ messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
52
+ role,
53
+ content
54
+ }))
55
+ }, body)),
56
+ headers: headers || {},
57
+ signal: abortController.signal
58
+ }).catch((err) => {
59
+ mutate(previousMessages);
60
+ throw err;
61
+ });
62
+ if (onResponse) {
63
+ try {
64
+ yield onResponse(res);
65
+ } catch (err) {
66
+ throw err;
67
+ }
68
+ }
69
+ if (!res.ok) {
70
+ mutate(previousMessages);
71
+ throw new Error(
72
+ (yield res.text()) || "Failed to fetch the chat response."
73
+ );
74
+ }
75
+ if (!res.body) {
76
+ throw new Error("The response body is empty.");
77
+ }
78
+ let result = "";
79
+ const createdAt = /* @__PURE__ */ new Date();
80
+ const replyId = nanoid();
81
+ const reader = res.body.getReader();
82
+ while (true) {
83
+ const { done, value } = yield reader.read();
84
+ if (done) {
85
+ break;
86
+ }
87
+ result += decodeAIStreamChunk(value);
88
+ mutate([
89
+ ...messagesSnapshot,
90
+ {
91
+ id: replyId,
92
+ createdAt,
93
+ content: result,
94
+ role: "assistant"
95
+ }
96
+ ]);
97
+ if (abortController === null) {
98
+ reader.cancel();
99
+ break;
100
+ }
101
+ }
102
+ if (onFinish) {
103
+ onFinish({
104
+ id: replyId,
105
+ createdAt,
106
+ content: result,
107
+ role: "assistant"
108
+ });
109
+ }
110
+ abortController = null;
111
+ return result;
112
+ } catch (err) {
113
+ if (err.name === "AbortError") {
114
+ abortController = null;
115
+ return null;
116
+ }
117
+ if (onError && err instanceof Error) {
118
+ onError(err);
119
+ }
120
+ error.value = err;
121
+ } finally {
122
+ isLoading.value = false;
123
+ }
124
+ });
125
+ }
126
+ const append = (message) => __async(this, null, function* () {
127
+ if (!message.id) {
128
+ message.id = nanoid();
129
+ }
130
+ return triggerRequest(messages.value.concat(message));
131
+ });
132
+ const reload = () => __async(this, null, function* () {
133
+ const messagesSnapshot = messages.value;
134
+ if (messagesSnapshot.length === 0)
135
+ return null;
136
+ const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
137
+ if (lastMessage.role === "assistant") {
138
+ return triggerRequest(messagesSnapshot.slice(0, -1));
139
+ }
140
+ return triggerRequest(messagesSnapshot);
141
+ });
142
+ const stop = () => {
143
+ if (abortController) {
144
+ abortController.abort();
145
+ abortController = null;
146
+ }
147
+ };
148
+ const setMessages = (messages2) => {
149
+ mutate(messages2);
150
+ };
151
+ const input = ref(initialInput);
152
+ const handleSubmit = (e) => {
153
+ e.preventDefault();
154
+ const inputValue = input.value;
155
+ if (!inputValue)
156
+ return;
157
+ append({
158
+ content: inputValue,
159
+ role: "user"
160
+ });
161
+ input.value = "";
162
+ };
163
+ return {
164
+ messages,
165
+ append,
166
+ error,
167
+ reload,
168
+ stop,
169
+ setMessages,
170
+ input,
171
+ handleSubmit,
172
+ isLoading
173
+ };
174
+ }
175
+
176
+ export {
177
+ useChat
178
+ };
@@ -1,6 +1,4 @@
1
1
  var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
2
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
3
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
4
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -16,7 +14,6 @@ var __spreadValues = (a, b) => {
16
14
  }
17
15
  return a;
18
16
  };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
17
  var __async = (__this, __arguments, generator) => {
21
18
  return new Promise((resolve, reject) => {
22
19
  var fulfilled = (value) => {
@@ -38,8 +35,20 @@ var __async = (__this, __arguments, generator) => {
38
35
  });
39
36
  };
40
37
 
38
+ // shared/utils.ts
39
+ import { customAlphabet } from "nanoid";
40
+ var nanoid = customAlphabet(
41
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
42
+ 7
43
+ );
44
+ var decoder = new TextDecoder();
45
+ function decodeAIStreamChunk(chunk) {
46
+ return decoder.decode(chunk);
47
+ }
48
+
41
49
  export {
42
50
  __spreadValues,
43
- __spreadProps,
44
- __async
51
+ __async,
52
+ nanoid,
53
+ decodeAIStreamChunk
45
54
  };
@@ -0,0 +1,4 @@
1
+ export { UseChatHelpers, useChat } from './use-chat.js';
2
+ export { UseCompletionHelpers, useCompletion } from './use-completion.js';
3
+ export { C as CreateMessage, M as Message, U as UseChatOptions } from './types-f862f74a.js';
4
+ import 'vue';
@@ -0,0 +1,384 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __export = (target, all) => {
23
+ for (var name in all)
24
+ __defProp(target, name, { get: all[name], enumerable: true });
25
+ };
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
31
+ }
32
+ return to;
33
+ };
34
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
35
+ // If the importer is in node compatibility mode or this is not an ESM
36
+ // file that has been converted to a CommonJS file using a Babel-
37
+ // compatible transform (i.e. "__esModule" has not been set), then set
38
+ // "default" to the CommonJS "module.exports" for node compatibility.
39
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
40
+ mod
41
+ ));
42
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
43
+ var __async = (__this, __arguments, generator) => {
44
+ return new Promise((resolve, reject) => {
45
+ var fulfilled = (value) => {
46
+ try {
47
+ step(generator.next(value));
48
+ } catch (e) {
49
+ reject(e);
50
+ }
51
+ };
52
+ var rejected = (value) => {
53
+ try {
54
+ step(generator.throw(value));
55
+ } catch (e) {
56
+ reject(e);
57
+ }
58
+ };
59
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
60
+ step((generator = generator.apply(__this, __arguments)).next());
61
+ });
62
+ };
63
+
64
+ // vue/index.ts
65
+ var vue_exports = {};
66
+ __export(vue_exports, {
67
+ useChat: () => useChat,
68
+ useCompletion: () => useCompletion
69
+ });
70
+ module.exports = __toCommonJS(vue_exports);
71
+
72
+ // vue/use-chat.ts
73
+ var import_swrv = __toESM(require("swrv"));
74
+ var import_vue = require("vue");
75
+
76
+ // shared/utils.ts
77
+ var import_nanoid = require("nanoid");
78
+ var nanoid = (0, import_nanoid.customAlphabet)(
79
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
80
+ 7
81
+ );
82
+ var decoder = new TextDecoder();
83
+ function decodeAIStreamChunk(chunk) {
84
+ return decoder.decode(chunk);
85
+ }
86
+
87
+ // vue/use-chat.ts
88
+ var uniqueId = 0;
89
+ var useSWRV = import_swrv.default.default || import_swrv.default;
90
+ var store = {};
91
+ function useChat({
92
+ api = "/api/chat",
93
+ id,
94
+ initialMessages = [],
95
+ initialInput = "",
96
+ sendExtraMessageFields,
97
+ onResponse,
98
+ onFinish,
99
+ onError,
100
+ headers,
101
+ body
102
+ } = {}) {
103
+ const chatId = id || `chat-${uniqueId++}`;
104
+ const key = `${api}|${chatId}`;
105
+ const { data, mutate: originalMutate } = useSWRV(
106
+ key,
107
+ () => store[key] || initialMessages
108
+ );
109
+ data.value || (data.value = initialMessages);
110
+ const mutate = (data2) => {
111
+ store[key] = data2;
112
+ return originalMutate();
113
+ };
114
+ const messages = data;
115
+ const error = (0, import_vue.ref)(void 0);
116
+ const isLoading = (0, import_vue.ref)(false);
117
+ let abortController = null;
118
+ function triggerRequest(messagesSnapshot) {
119
+ return __async(this, null, function* () {
120
+ try {
121
+ isLoading.value = true;
122
+ abortController = new AbortController();
123
+ const previousMessages = messages.value;
124
+ mutate(messagesSnapshot);
125
+ const res = yield fetch(api, {
126
+ method: "POST",
127
+ body: JSON.stringify(__spreadValues({
128
+ messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
129
+ role,
130
+ content
131
+ }))
132
+ }, body)),
133
+ headers: headers || {},
134
+ signal: abortController.signal
135
+ }).catch((err) => {
136
+ mutate(previousMessages);
137
+ throw err;
138
+ });
139
+ if (onResponse) {
140
+ try {
141
+ yield onResponse(res);
142
+ } catch (err) {
143
+ throw err;
144
+ }
145
+ }
146
+ if (!res.ok) {
147
+ mutate(previousMessages);
148
+ throw new Error(
149
+ (yield res.text()) || "Failed to fetch the chat response."
150
+ );
151
+ }
152
+ if (!res.body) {
153
+ throw new Error("The response body is empty.");
154
+ }
155
+ let result = "";
156
+ const createdAt = /* @__PURE__ */ new Date();
157
+ const replyId = nanoid();
158
+ const reader = res.body.getReader();
159
+ while (true) {
160
+ const { done, value } = yield reader.read();
161
+ if (done) {
162
+ break;
163
+ }
164
+ result += decodeAIStreamChunk(value);
165
+ mutate([
166
+ ...messagesSnapshot,
167
+ {
168
+ id: replyId,
169
+ createdAt,
170
+ content: result,
171
+ role: "assistant"
172
+ }
173
+ ]);
174
+ if (abortController === null) {
175
+ reader.cancel();
176
+ break;
177
+ }
178
+ }
179
+ if (onFinish) {
180
+ onFinish({
181
+ id: replyId,
182
+ createdAt,
183
+ content: result,
184
+ role: "assistant"
185
+ });
186
+ }
187
+ abortController = null;
188
+ return result;
189
+ } catch (err) {
190
+ if (err.name === "AbortError") {
191
+ abortController = null;
192
+ return null;
193
+ }
194
+ if (onError && err instanceof Error) {
195
+ onError(err);
196
+ }
197
+ error.value = err;
198
+ } finally {
199
+ isLoading.value = false;
200
+ }
201
+ });
202
+ }
203
+ const append = (message) => __async(this, null, function* () {
204
+ if (!message.id) {
205
+ message.id = nanoid();
206
+ }
207
+ return triggerRequest(messages.value.concat(message));
208
+ });
209
+ const reload = () => __async(this, null, function* () {
210
+ const messagesSnapshot = messages.value;
211
+ if (messagesSnapshot.length === 0)
212
+ return null;
213
+ const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
214
+ if (lastMessage.role === "assistant") {
215
+ return triggerRequest(messagesSnapshot.slice(0, -1));
216
+ }
217
+ return triggerRequest(messagesSnapshot);
218
+ });
219
+ const stop = () => {
220
+ if (abortController) {
221
+ abortController.abort();
222
+ abortController = null;
223
+ }
224
+ };
225
+ const setMessages = (messages2) => {
226
+ mutate(messages2);
227
+ };
228
+ const input = (0, import_vue.ref)(initialInput);
229
+ const handleSubmit = (e) => {
230
+ e.preventDefault();
231
+ const inputValue = input.value;
232
+ if (!inputValue)
233
+ return;
234
+ append({
235
+ content: inputValue,
236
+ role: "user"
237
+ });
238
+ input.value = "";
239
+ };
240
+ return {
241
+ messages,
242
+ append,
243
+ error,
244
+ reload,
245
+ stop,
246
+ setMessages,
247
+ input,
248
+ handleSubmit,
249
+ isLoading
250
+ };
251
+ }
252
+
253
+ // vue/use-completion.ts
254
+ var import_swrv2 = __toESM(require("swrv"));
255
+ var import_vue2 = require("vue");
256
+ var uniqueId2 = 0;
257
+ var useSWRV2 = import_swrv2.default.default || import_swrv2.default;
258
+ var store2 = {};
259
+ function useCompletion({
260
+ api = "/api/completion",
261
+ id,
262
+ initialCompletion = "",
263
+ initialInput = "",
264
+ headers,
265
+ body,
266
+ onResponse,
267
+ onFinish,
268
+ onError
269
+ } = {}) {
270
+ const completionId = id || `completion-${uniqueId2++}`;
271
+ const key = `${api}|${completionId}`;
272
+ const { data, mutate: originalMutate } = useSWRV2(
273
+ key,
274
+ () => store2[key] || initialCompletion
275
+ );
276
+ data.value || (data.value = initialCompletion);
277
+ const mutate = (data2) => {
278
+ store2[key] = data2;
279
+ return originalMutate();
280
+ };
281
+ const completion = data;
282
+ const error = (0, import_vue2.ref)(void 0);
283
+ const isLoading = (0, import_vue2.ref)(false);
284
+ let abortController = null;
285
+ function triggerRequest(prompt) {
286
+ return __async(this, null, function* () {
287
+ try {
288
+ isLoading.value = true;
289
+ abortController = new AbortController();
290
+ mutate("");
291
+ const res = yield fetch(api, {
292
+ method: "POST",
293
+ body: JSON.stringify(__spreadValues({
294
+ prompt
295
+ }, body)),
296
+ headers: headers || {},
297
+ signal: abortController.signal
298
+ }).catch((err) => {
299
+ throw err;
300
+ });
301
+ if (onResponse) {
302
+ try {
303
+ yield onResponse(res);
304
+ } catch (err) {
305
+ throw err;
306
+ }
307
+ }
308
+ if (!res.ok) {
309
+ throw new Error(
310
+ (yield res.text()) || "Failed to fetch the chat response."
311
+ );
312
+ }
313
+ if (!res.body) {
314
+ throw new Error("The response body is empty.");
315
+ }
316
+ let result = "";
317
+ const reader = res.body.getReader();
318
+ while (true) {
319
+ const { done, value } = yield reader.read();
320
+ if (done) {
321
+ break;
322
+ }
323
+ result += decodeAIStreamChunk(value);
324
+ mutate(result);
325
+ if (abortController === null) {
326
+ reader.cancel();
327
+ break;
328
+ }
329
+ }
330
+ if (onFinish) {
331
+ onFinish(prompt, result);
332
+ }
333
+ abortController = null;
334
+ return result;
335
+ } catch (err) {
336
+ if (err.name === "AbortError") {
337
+ abortController = null;
338
+ return null;
339
+ }
340
+ if (onError && error instanceof Error) {
341
+ onError(error);
342
+ }
343
+ error.value = err;
344
+ } finally {
345
+ isLoading.value = false;
346
+ }
347
+ });
348
+ }
349
+ const complete = (prompt) => __async(this, null, function* () {
350
+ return triggerRequest(prompt);
351
+ });
352
+ const stop = () => {
353
+ if (abortController) {
354
+ abortController.abort();
355
+ abortController = null;
356
+ }
357
+ };
358
+ const setCompletion = (completion2) => {
359
+ mutate(completion2);
360
+ };
361
+ const input = (0, import_vue2.ref)(initialInput);
362
+ const handleSubmit = (e) => {
363
+ e.preventDefault();
364
+ const inputValue = input.value;
365
+ if (!inputValue)
366
+ return;
367
+ return complete(inputValue);
368
+ };
369
+ return {
370
+ completion,
371
+ complete,
372
+ error,
373
+ stop,
374
+ setCompletion,
375
+ input,
376
+ handleSubmit,
377
+ isLoading
378
+ };
379
+ }
380
+ // Annotate the CommonJS export names for ESM import in node:
381
+ 0 && (module.exports = {
382
+ useChat,
383
+ useCompletion
384
+ });
@@ -0,0 +1,11 @@
1
+ import {
2
+ useChat
3
+ } from "./chunk-OYI6GFBM.mjs";
4
+ import {
5
+ useCompletion
6
+ } from "./chunk-FT26CHLO.mjs";
7
+ import "./chunk-WXH4YPZV.mjs";
8
+ export {
9
+ useChat,
10
+ useCompletion
11
+ };