ai 2.1.0 → 2.1.3

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 (71) hide show
  1. package/README.md +89 -0
  2. package/dist/index.d.ts +48 -8
  3. package/dist/index.mjs +234 -26
  4. package/package.json +4 -4
  5. package/react/dist/index.d.ts +206 -3
  6. package/react/dist/index.js +2 -1
  7. package/react/dist/index.mjs +385 -7
  8. package/svelte/dist/index.d.ts +194 -4
  9. package/svelte/dist/index.js +2 -1
  10. package/svelte/dist/index.mjs +780 -7
  11. package/vue/dist/index.d.ts +194 -4
  12. package/vue/dist/index.mjs +345 -7
  13. package/dist/ai-stream.d.ts +0 -18
  14. package/dist/ai-stream.js +0 -132
  15. package/dist/ai-stream.mjs +0 -15
  16. package/dist/anthropic-stream.d.ts +0 -5
  17. package/dist/anthropic-stream.js +0 -133
  18. package/dist/anthropic-stream.mjs +0 -10
  19. package/dist/chunk-2JQWCLY2.mjs +0 -70
  20. package/dist/chunk-7KLTYB74.mjs +0 -70
  21. package/dist/chunk-BJMBMGA3.mjs +0 -34
  22. package/dist/chunk-KKQRUR3E.mjs +0 -51
  23. package/dist/chunk-RBP6ONSV.mjs +0 -45
  24. package/dist/chunk-TWW2ODJW.mjs +0 -32
  25. package/dist/chunk-U2OQ6HW6.mjs +0 -41
  26. package/dist/chunk-UJV6VDVU.mjs +0 -97
  27. package/dist/huggingface-stream.d.ts +0 -5
  28. package/dist/huggingface-stream.js +0 -121
  29. package/dist/huggingface-stream.mjs +0 -10
  30. package/dist/index.test.d.ts +0 -2
  31. package/dist/index.test.js +0 -568
  32. package/dist/index.test.mjs +0 -286
  33. package/dist/langchain-stream.d.ts +0 -12
  34. package/dist/langchain-stream.js +0 -102
  35. package/dist/langchain-stream.mjs +0 -10
  36. package/dist/openai-stream.d.ts +0 -5
  37. package/dist/openai-stream.js +0 -144
  38. package/dist/openai-stream.mjs +0 -10
  39. package/dist/streaming-text-response.d.ts +0 -17
  40. package/dist/streaming-text-response.js +0 -75
  41. package/dist/streaming-text-response.mjs +0 -11
  42. package/react/dist/chunk-5PP6W52J.mjs +0 -202
  43. package/react/dist/chunk-6EH3SWMP.mjs +0 -55
  44. package/react/dist/chunk-PW6HSU2N.mjs +0 -154
  45. package/react/dist/types-f862f74a.d.ts +0 -123
  46. package/react/dist/use-chat.d.ts +0 -42
  47. package/react/dist/use-chat.js +0 -276
  48. package/react/dist/use-chat.mjs +0 -8
  49. package/react/dist/use-completion.d.ts +0 -47
  50. package/react/dist/use-completion.js +0 -229
  51. package/react/dist/use-completion.mjs +0 -8
  52. package/svelte/dist/chunk-6USBQIV6.mjs +0 -177
  53. package/svelte/dist/chunk-BQ64GHZ3.mjs +0 -136
  54. package/svelte/dist/chunk-CENOSGDG.mjs +0 -493
  55. package/svelte/dist/types-f862f74a.d.ts +0 -123
  56. package/svelte/dist/use-chat.d.ts +0 -39
  57. package/svelte/dist/use-chat.js +0 -680
  58. package/svelte/dist/use-chat.mjs +0 -7
  59. package/svelte/dist/use-completion.d.ts +0 -38
  60. package/svelte/dist/use-completion.js +0 -640
  61. package/svelte/dist/use-completion.mjs +0 -7
  62. package/vue/dist/chunk-FT26CHLO.mjs +0 -137
  63. package/vue/dist/chunk-OYI6GFBM.mjs +0 -178
  64. package/vue/dist/chunk-WXH4YPZV.mjs +0 -54
  65. package/vue/dist/types-f862f74a.d.ts +0 -123
  66. package/vue/dist/use-chat.d.ts +0 -39
  67. package/vue/dist/use-chat.js +0 -252
  68. package/vue/dist/use-chat.mjs +0 -7
  69. package/vue/dist/use-completion.d.ts +0 -38
  70. package/vue/dist/use-completion.js +0 -212
  71. package/vue/dist/use-completion.mjs +0 -7
@@ -1,276 +0,0 @@
1
- 'use client'
2
- "use strict";
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __export = (target, all) => {
24
- for (var name in all)
25
- __defProp(target, name, { get: all[name], enumerable: true });
26
- };
27
- var __copyProps = (to, from, except, desc) => {
28
- if (from && typeof from === "object" || typeof from === "function") {
29
- for (let key of __getOwnPropNames(from))
30
- if (!__hasOwnProp.call(to, key) && key !== except)
31
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
- }
33
- return to;
34
- };
35
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
36
- // If the importer is in node compatibility mode or this is not an ESM
37
- // file that has been converted to a CommonJS file using a Babel-
38
- // compatible transform (i.e. "__esModule" has not been set), then set
39
- // "default" to the CommonJS "module.exports" for node compatibility.
40
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
41
- mod
42
- ));
43
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
44
- var __async = (__this, __arguments, generator) => {
45
- return new Promise((resolve, reject) => {
46
- var fulfilled = (value) => {
47
- try {
48
- step(generator.next(value));
49
- } catch (e) {
50
- reject(e);
51
- }
52
- };
53
- var rejected = (value) => {
54
- try {
55
- step(generator.throw(value));
56
- } catch (e) {
57
- reject(e);
58
- }
59
- };
60
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
61
- step((generator = generator.apply(__this, __arguments)).next());
62
- });
63
- };
64
-
65
- // react/use-chat.ts
66
- var use_chat_exports = {};
67
- __export(use_chat_exports, {
68
- useChat: () => useChat
69
- });
70
- module.exports = __toCommonJS(use_chat_exports);
71
- var import_react = require("react");
72
- var import_mutation = __toESM(require("swr/mutation"));
73
- var import_swr = __toESM(require("swr"));
74
-
75
- // shared/utils.ts
76
- var import_nanoid = require("nanoid");
77
- var nanoid = (0, import_nanoid.customAlphabet)(
78
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
79
- 7
80
- );
81
- var decoder = new TextDecoder();
82
- function decodeAIStreamChunk(chunk) {
83
- return decoder.decode(chunk);
84
- }
85
-
86
- // react/use-chat.ts
87
- function useChat({
88
- api = "/api/chat",
89
- id,
90
- initialMessages = [],
91
- initialInput = "",
92
- sendExtraMessageFields,
93
- onResponse,
94
- onFinish,
95
- onError,
96
- headers,
97
- body
98
- } = {}) {
99
- const hookId = (0, import_react.useId)();
100
- const chatId = id || hookId;
101
- const { data, mutate } = (0, import_swr.default)([api, chatId], null, {
102
- fallbackData: initialMessages
103
- });
104
- const messages = data;
105
- const messagesRef = (0, import_react.useRef)(messages);
106
- (0, import_react.useEffect)(() => {
107
- messagesRef.current = messages;
108
- }, [messages]);
109
- const abortControllerRef = (0, import_react.useRef)(null);
110
- const extraMetadataRef = (0, import_react.useRef)({
111
- headers,
112
- body
113
- });
114
- (0, import_react.useEffect)(() => {
115
- extraMetadataRef.current = {
116
- headers,
117
- body
118
- };
119
- }, [headers, body]);
120
- const { error, trigger, isMutating } = (0, import_mutation.default)(
121
- [api, chatId],
122
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg: messagesSnapshot }) {
123
- try {
124
- const abortController = new AbortController();
125
- abortControllerRef.current = abortController;
126
- const previousMessages = messagesRef.current;
127
- mutate(messagesSnapshot, false);
128
- const res = yield fetch(api, {
129
- method: "POST",
130
- body: JSON.stringify(__spreadValues({
131
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
132
- role,
133
- content
134
- }))
135
- }, extraMetadataRef.current.body)),
136
- headers: extraMetadataRef.current.headers || {},
137
- signal: abortController.signal
138
- }).catch((err) => {
139
- mutate(previousMessages, false);
140
- throw err;
141
- });
142
- if (onResponse) {
143
- try {
144
- yield onResponse(res);
145
- } catch (err) {
146
- throw err;
147
- }
148
- }
149
- if (!res.ok) {
150
- mutate(previousMessages, false);
151
- throw new Error(
152
- (yield res.text()) || "Failed to fetch the chat response."
153
- );
154
- }
155
- if (!res.body) {
156
- throw new Error("The response body is empty.");
157
- }
158
- let result = "";
159
- const createdAt = /* @__PURE__ */ new Date();
160
- const replyId = nanoid();
161
- const reader = res.body.getReader();
162
- while (true) {
163
- const { done, value } = yield reader.read();
164
- if (done) {
165
- break;
166
- }
167
- result += decodeAIStreamChunk(value);
168
- mutate(
169
- [
170
- ...messagesSnapshot,
171
- {
172
- id: replyId,
173
- createdAt,
174
- content: result,
175
- role: "assistant"
176
- }
177
- ],
178
- false
179
- );
180
- if (abortControllerRef.current === null) {
181
- reader.cancel();
182
- break;
183
- }
184
- }
185
- if (onFinish) {
186
- onFinish({
187
- id: replyId,
188
- createdAt,
189
- content: result,
190
- role: "assistant"
191
- });
192
- }
193
- abortControllerRef.current = null;
194
- return result;
195
- } catch (err) {
196
- if (err.name === "AbortError") {
197
- abortControllerRef.current = null;
198
- return null;
199
- }
200
- if (onError && err instanceof Error) {
201
- onError(err);
202
- }
203
- throw err;
204
- }
205
- }),
206
- {
207
- populateCache: false,
208
- revalidate: false
209
- }
210
- );
211
- const append = (0, import_react.useCallback)(
212
- (message) => __async(this, null, function* () {
213
- if (!message.id) {
214
- message.id = nanoid();
215
- }
216
- return trigger(messagesRef.current.concat(message));
217
- }),
218
- [trigger]
219
- );
220
- const reload = (0, import_react.useCallback)(() => __async(this, null, function* () {
221
- if (messagesRef.current.length === 0)
222
- return null;
223
- const lastMessage = messagesRef.current[messagesRef.current.length - 1];
224
- if (lastMessage.role === "assistant") {
225
- return trigger(messagesRef.current.slice(0, -1));
226
- }
227
- return trigger(messagesRef.current);
228
- }), [trigger]);
229
- const stop = (0, import_react.useCallback)(() => {
230
- if (abortControllerRef.current) {
231
- abortControllerRef.current.abort();
232
- abortControllerRef.current = null;
233
- }
234
- }, []);
235
- const setMessages = (0, import_react.useCallback)(
236
- (messages2) => {
237
- mutate(messages2, false);
238
- messagesRef.current = messages2;
239
- },
240
- [mutate]
241
- );
242
- const [input, setInput] = (0, import_react.useState)(initialInput);
243
- const handleSubmit = (0, import_react.useCallback)(
244
- (e) => {
245
- e.preventDefault();
246
- if (!input)
247
- return;
248
- append({
249
- content: input,
250
- role: "user"
251
- });
252
- setInput("");
253
- },
254
- [input, append]
255
- );
256
- const handleInputChange = (e) => {
257
- setInput(e.target.value);
258
- };
259
- return {
260
- messages,
261
- error,
262
- append,
263
- reload,
264
- stop,
265
- setMessages,
266
- input,
267
- setInput,
268
- handleInputChange,
269
- handleSubmit,
270
- isLoading: isMutating
271
- };
272
- }
273
- // Annotate the CommonJS export names for ESM import in node:
274
- 0 && (module.exports = {
275
- useChat
276
- });
@@ -1,8 +0,0 @@
1
- 'use client'
2
- import {
3
- useChat
4
- } from "./chunk-5PP6W52J.mjs";
5
- import "./chunk-6EH3SWMP.mjs";
6
- export {
7
- useChat
8
- };
@@ -1,47 +0,0 @@
1
- import { a as UseCompletionOptions } from './types-f862f74a.js';
2
-
3
- type UseCompletionHelpers = {
4
- /** The current completion result */
5
- completion: string;
6
- /**
7
- * Send a new prompt to the API endpoint and update the completion state.
8
- */
9
- complete: (prompt: string) => Promise<string | null | undefined>;
10
- /** The error object of the API request */
11
- error: undefined | Error;
12
- /**
13
- * Abort the current API request but keep the generated tokens.
14
- */
15
- stop: () => void;
16
- /**
17
- * Update the `completion` state locally.
18
- */
19
- setCompletion: (completion: string) => void;
20
- /** The current value of the input */
21
- input: string;
22
- /** setState-powered method to update the input value */
23
- setInput: React.Dispatch<React.SetStateAction<string>>;
24
- /**
25
- * An input/textarea-ready onChange handler to control the value of the input
26
- * @example
27
- * ```jsx
28
- * <input onChange={handleInputChange} value={input} />
29
- * ```
30
- */
31
- handleInputChange: (e: any) => void;
32
- /**
33
- * Form submission handler to automattically reset input and append a user message
34
- * @example
35
- * ```jsx
36
- * <form onSubmit={handleSubmit}>
37
- * <input onChange={handleInputChange} value={input} />
38
- * </form>
39
- * ```
40
- */
41
- handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
42
- /** Whether the API request is in progress */
43
- isLoading: boolean;
44
- };
45
- declare function useCompletion({ api, id, initialCompletion, initialInput, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
46
-
47
- export { UseCompletionHelpers, useCompletion };
@@ -1,229 +0,0 @@
1
- 'use client'
2
- "use strict";
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __export = (target, all) => {
24
- for (var name in all)
25
- __defProp(target, name, { get: all[name], enumerable: true });
26
- };
27
- var __copyProps = (to, from, except, desc) => {
28
- if (from && typeof from === "object" || typeof from === "function") {
29
- for (let key of __getOwnPropNames(from))
30
- if (!__hasOwnProp.call(to, key) && key !== except)
31
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
- }
33
- return to;
34
- };
35
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
36
- // If the importer is in node compatibility mode or this is not an ESM
37
- // file that has been converted to a CommonJS file using a Babel-
38
- // compatible transform (i.e. "__esModule" has not been set), then set
39
- // "default" to the CommonJS "module.exports" for node compatibility.
40
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
41
- mod
42
- ));
43
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
44
- var __async = (__this, __arguments, generator) => {
45
- return new Promise((resolve, reject) => {
46
- var fulfilled = (value) => {
47
- try {
48
- step(generator.next(value));
49
- } catch (e) {
50
- reject(e);
51
- }
52
- };
53
- var rejected = (value) => {
54
- try {
55
- step(generator.throw(value));
56
- } catch (e) {
57
- reject(e);
58
- }
59
- };
60
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
61
- step((generator = generator.apply(__this, __arguments)).next());
62
- });
63
- };
64
-
65
- // react/use-completion.ts
66
- var use_completion_exports = {};
67
- __export(use_completion_exports, {
68
- useCompletion: () => useCompletion
69
- });
70
- module.exports = __toCommonJS(use_completion_exports);
71
- var import_react = require("react");
72
- var import_mutation = __toESM(require("swr/mutation"));
73
- var import_swr = __toESM(require("swr"));
74
-
75
- // shared/utils.ts
76
- var import_nanoid = require("nanoid");
77
- var nanoid = (0, import_nanoid.customAlphabet)(
78
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
79
- 7
80
- );
81
- var decoder = new TextDecoder();
82
- function decodeAIStreamChunk(chunk) {
83
- return decoder.decode(chunk);
84
- }
85
-
86
- // react/use-completion.ts
87
- function useCompletion({
88
- api = "/api/completion",
89
- id,
90
- initialCompletion = "",
91
- initialInput = "",
92
- headers,
93
- body,
94
- onResponse,
95
- onFinish,
96
- onError
97
- } = {}) {
98
- const hookId = (0, import_react.useId)();
99
- const completionId = id || hookId;
100
- const { data, mutate } = (0, import_swr.default)([api, completionId], null, {
101
- fallbackData: initialCompletion
102
- });
103
- const completion = data;
104
- const [abortController, setAbortController] = (0, import_react.useState)(null);
105
- const extraMetadataRef = (0, import_react.useRef)({
106
- headers,
107
- body
108
- });
109
- (0, import_react.useEffect)(() => {
110
- extraMetadataRef.current = {
111
- headers,
112
- body
113
- };
114
- }, [headers, body]);
115
- const { error, trigger, isMutating } = (0, import_mutation.default)(
116
- [api, completionId],
117
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg: prompt }) {
118
- try {
119
- const abortController2 = new AbortController();
120
- setAbortController(abortController2);
121
- mutate("", false);
122
- const res = yield fetch(api, {
123
- method: "POST",
124
- body: JSON.stringify(__spreadValues({
125
- prompt
126
- }, extraMetadataRef.current.body)),
127
- headers: extraMetadataRef.current.headers || {},
128
- signal: abortController2.signal
129
- }).catch((err) => {
130
- throw err;
131
- });
132
- if (onResponse) {
133
- try {
134
- yield onResponse(res);
135
- } catch (err) {
136
- throw err;
137
- }
138
- }
139
- if (!res.ok) {
140
- throw new Error(
141
- (yield res.text()) || "Failed to fetch the chat response."
142
- );
143
- }
144
- if (!res.body) {
145
- throw new Error("The response body is empty.");
146
- }
147
- let result = "";
148
- const reader = res.body.getReader();
149
- while (true) {
150
- const { done, value } = yield reader.read();
151
- if (done) {
152
- break;
153
- }
154
- result += decodeAIStreamChunk(value);
155
- mutate(result, false);
156
- if (abortController2 === null) {
157
- reader.cancel();
158
- break;
159
- }
160
- }
161
- if (onFinish) {
162
- onFinish(prompt, result);
163
- }
164
- setAbortController(null);
165
- return result;
166
- } catch (err) {
167
- if (err.name === "AbortError") {
168
- setAbortController(null);
169
- return null;
170
- }
171
- if (onError && err instanceof Error) {
172
- onError(err);
173
- }
174
- throw err;
175
- }
176
- }),
177
- {
178
- populateCache: false,
179
- revalidate: false
180
- }
181
- );
182
- const stop = (0, import_react.useCallback)(() => {
183
- if (abortController) {
184
- abortController.abort();
185
- setAbortController(null);
186
- }
187
- }, [abortController]);
188
- const setCompletion = (0, import_react.useCallback)(
189
- (completion2) => {
190
- mutate(completion2, false);
191
- },
192
- [mutate]
193
- );
194
- const [input, setInput] = (0, import_react.useState)(initialInput);
195
- const handleSubmit = (0, import_react.useCallback)(
196
- (e) => {
197
- e.preventDefault();
198
- if (!input)
199
- return;
200
- return trigger(input);
201
- },
202
- [input, trigger]
203
- );
204
- const handleInputChange = (e) => {
205
- setInput(e.target.value);
206
- };
207
- const complete = (0, import_react.useCallback)(
208
- (prompt) => __async(this, null, function* () {
209
- return trigger(prompt);
210
- }),
211
- [trigger]
212
- );
213
- return {
214
- completion,
215
- complete,
216
- error,
217
- setCompletion,
218
- stop,
219
- input,
220
- setInput,
221
- handleInputChange,
222
- handleSubmit,
223
- isLoading: isMutating
224
- };
225
- }
226
- // Annotate the CommonJS export names for ESM import in node:
227
- 0 && (module.exports = {
228
- useCompletion
229
- });
@@ -1,8 +0,0 @@
1
- 'use client'
2
- import {
3
- useCompletion
4
- } from "./chunk-PW6HSU2N.mjs";
5
- import "./chunk-6EH3SWMP.mjs";
6
- export {
7
- useCompletion
8
- };