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,202 +0,0 @@
1
- 'use client'
2
- import {
3
- __async,
4
- __spreadValues,
5
- decodeAIStreamChunk,
6
- nanoid
7
- } from "./chunk-6EH3SWMP.mjs";
8
-
9
- // react/use-chat.ts
10
- import { useCallback, useId, useRef, useEffect, useState } from "react";
11
- import useSWRMutation from "swr/mutation";
12
- import useSWR from "swr";
13
- function useChat({
14
- api = "/api/chat",
15
- id,
16
- initialMessages = [],
17
- initialInput = "",
18
- sendExtraMessageFields,
19
- onResponse,
20
- onFinish,
21
- onError,
22
- headers,
23
- body
24
- } = {}) {
25
- const hookId = useId();
26
- const chatId = id || hookId;
27
- const { data, mutate } = useSWR([api, chatId], null, {
28
- fallbackData: initialMessages
29
- });
30
- const messages = data;
31
- const messagesRef = useRef(messages);
32
- useEffect(() => {
33
- messagesRef.current = messages;
34
- }, [messages]);
35
- const abortControllerRef = useRef(null);
36
- const extraMetadataRef = useRef({
37
- headers,
38
- body
39
- });
40
- useEffect(() => {
41
- extraMetadataRef.current = {
42
- headers,
43
- body
44
- };
45
- }, [headers, body]);
46
- const { error, trigger, isMutating } = useSWRMutation(
47
- [api, chatId],
48
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg: messagesSnapshot }) {
49
- try {
50
- const abortController = new AbortController();
51
- abortControllerRef.current = abortController;
52
- const previousMessages = messagesRef.current;
53
- mutate(messagesSnapshot, false);
54
- const res = yield fetch(api, {
55
- method: "POST",
56
- body: JSON.stringify(__spreadValues({
57
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
58
- role,
59
- content
60
- }))
61
- }, extraMetadataRef.current.body)),
62
- headers: extraMetadataRef.current.headers || {},
63
- signal: abortController.signal
64
- }).catch((err) => {
65
- mutate(previousMessages, false);
66
- throw err;
67
- });
68
- if (onResponse) {
69
- try {
70
- yield onResponse(res);
71
- } catch (err) {
72
- throw err;
73
- }
74
- }
75
- if (!res.ok) {
76
- mutate(previousMessages, false);
77
- throw new Error(
78
- (yield res.text()) || "Failed to fetch the chat response."
79
- );
80
- }
81
- if (!res.body) {
82
- throw new Error("The response body is empty.");
83
- }
84
- let result = "";
85
- const createdAt = /* @__PURE__ */ new Date();
86
- const replyId = nanoid();
87
- const reader = res.body.getReader();
88
- while (true) {
89
- const { done, value } = yield reader.read();
90
- if (done) {
91
- break;
92
- }
93
- result += decodeAIStreamChunk(value);
94
- mutate(
95
- [
96
- ...messagesSnapshot,
97
- {
98
- id: replyId,
99
- createdAt,
100
- content: result,
101
- role: "assistant"
102
- }
103
- ],
104
- false
105
- );
106
- if (abortControllerRef.current === null) {
107
- reader.cancel();
108
- break;
109
- }
110
- }
111
- if (onFinish) {
112
- onFinish({
113
- id: replyId,
114
- createdAt,
115
- content: result,
116
- role: "assistant"
117
- });
118
- }
119
- abortControllerRef.current = null;
120
- return result;
121
- } catch (err) {
122
- if (err.name === "AbortError") {
123
- abortControllerRef.current = null;
124
- return null;
125
- }
126
- if (onError && err instanceof Error) {
127
- onError(err);
128
- }
129
- throw err;
130
- }
131
- }),
132
- {
133
- populateCache: false,
134
- revalidate: false
135
- }
136
- );
137
- const append = useCallback(
138
- (message) => __async(this, null, function* () {
139
- if (!message.id) {
140
- message.id = nanoid();
141
- }
142
- return trigger(messagesRef.current.concat(message));
143
- }),
144
- [trigger]
145
- );
146
- const reload = useCallback(() => __async(this, null, function* () {
147
- if (messagesRef.current.length === 0)
148
- return null;
149
- const lastMessage = messagesRef.current[messagesRef.current.length - 1];
150
- if (lastMessage.role === "assistant") {
151
- return trigger(messagesRef.current.slice(0, -1));
152
- }
153
- return trigger(messagesRef.current);
154
- }), [trigger]);
155
- const stop = useCallback(() => {
156
- if (abortControllerRef.current) {
157
- abortControllerRef.current.abort();
158
- abortControllerRef.current = null;
159
- }
160
- }, []);
161
- const setMessages = useCallback(
162
- (messages2) => {
163
- mutate(messages2, false);
164
- messagesRef.current = messages2;
165
- },
166
- [mutate]
167
- );
168
- const [input, setInput] = useState(initialInput);
169
- const handleSubmit = useCallback(
170
- (e) => {
171
- e.preventDefault();
172
- if (!input)
173
- return;
174
- append({
175
- content: input,
176
- role: "user"
177
- });
178
- setInput("");
179
- },
180
- [input, append]
181
- );
182
- const handleInputChange = (e) => {
183
- setInput(e.target.value);
184
- };
185
- return {
186
- messages,
187
- error,
188
- append,
189
- reload,
190
- stop,
191
- setMessages,
192
- input,
193
- setInput,
194
- handleInputChange,
195
- handleSubmit,
196
- isLoading: isMutating
197
- };
198
- }
199
-
200
- export {
201
- useChat
202
- };
@@ -1,55 +0,0 @@
1
- 'use client'
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __spreadValues = (a, b) => {
8
- for (var prop in b || (b = {}))
9
- if (__hasOwnProp.call(b, prop))
10
- __defNormalProp(a, prop, b[prop]);
11
- if (__getOwnPropSymbols)
12
- for (var prop of __getOwnPropSymbols(b)) {
13
- if (__propIsEnum.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- }
16
- return a;
17
- };
18
- var __async = (__this, __arguments, generator) => {
19
- return new Promise((resolve, reject) => {
20
- var fulfilled = (value) => {
21
- try {
22
- step(generator.next(value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- };
27
- var rejected = (value) => {
28
- try {
29
- step(generator.throw(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- };
34
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
- step((generator = generator.apply(__this, __arguments)).next());
36
- });
37
- };
38
-
39
- // shared/utils.ts
40
- import { customAlphabet } from "nanoid";
41
- var nanoid = customAlphabet(
42
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
43
- 7
44
- );
45
- var decoder = new TextDecoder();
46
- function decodeAIStreamChunk(chunk) {
47
- return decoder.decode(chunk);
48
- }
49
-
50
- export {
51
- __spreadValues,
52
- __async,
53
- nanoid,
54
- decodeAIStreamChunk
55
- };
@@ -1,154 +0,0 @@
1
- 'use client'
2
- import {
3
- __async,
4
- __spreadValues,
5
- decodeAIStreamChunk
6
- } from "./chunk-6EH3SWMP.mjs";
7
-
8
- // react/use-completion.ts
9
- import { useCallback, useEffect, useId, useRef, useState } from "react";
10
- import useSWRMutation from "swr/mutation";
11
- import useSWR from "swr";
12
- function useCompletion({
13
- api = "/api/completion",
14
- id,
15
- initialCompletion = "",
16
- initialInput = "",
17
- headers,
18
- body,
19
- onResponse,
20
- onFinish,
21
- onError
22
- } = {}) {
23
- const hookId = useId();
24
- const completionId = id || hookId;
25
- const { data, mutate } = useSWR([api, completionId], null, {
26
- fallbackData: initialCompletion
27
- });
28
- const completion = data;
29
- const [abortController, setAbortController] = useState(null);
30
- const extraMetadataRef = useRef({
31
- headers,
32
- body
33
- });
34
- useEffect(() => {
35
- extraMetadataRef.current = {
36
- headers,
37
- body
38
- };
39
- }, [headers, body]);
40
- const { error, trigger, isMutating } = useSWRMutation(
41
- [api, completionId],
42
- (_0, _1) => __async(this, [_0, _1], function* (_, { arg: prompt }) {
43
- try {
44
- const abortController2 = new AbortController();
45
- setAbortController(abortController2);
46
- mutate("", false);
47
- const res = yield fetch(api, {
48
- method: "POST",
49
- body: JSON.stringify(__spreadValues({
50
- prompt
51
- }, extraMetadataRef.current.body)),
52
- headers: extraMetadataRef.current.headers || {},
53
- signal: abortController2.signal
54
- }).catch((err) => {
55
- throw err;
56
- });
57
- if (onResponse) {
58
- try {
59
- yield onResponse(res);
60
- } catch (err) {
61
- throw err;
62
- }
63
- }
64
- if (!res.ok) {
65
- throw new Error(
66
- (yield res.text()) || "Failed to fetch the chat response."
67
- );
68
- }
69
- if (!res.body) {
70
- throw new Error("The response body is empty.");
71
- }
72
- let result = "";
73
- const reader = res.body.getReader();
74
- while (true) {
75
- const { done, value } = yield reader.read();
76
- if (done) {
77
- break;
78
- }
79
- result += decodeAIStreamChunk(value);
80
- mutate(result, false);
81
- if (abortController2 === null) {
82
- reader.cancel();
83
- break;
84
- }
85
- }
86
- if (onFinish) {
87
- onFinish(prompt, result);
88
- }
89
- setAbortController(null);
90
- return result;
91
- } catch (err) {
92
- if (err.name === "AbortError") {
93
- setAbortController(null);
94
- return null;
95
- }
96
- if (onError && err instanceof Error) {
97
- onError(err);
98
- }
99
- throw err;
100
- }
101
- }),
102
- {
103
- populateCache: false,
104
- revalidate: false
105
- }
106
- );
107
- const stop = useCallback(() => {
108
- if (abortController) {
109
- abortController.abort();
110
- setAbortController(null);
111
- }
112
- }, [abortController]);
113
- const setCompletion = useCallback(
114
- (completion2) => {
115
- mutate(completion2, false);
116
- },
117
- [mutate]
118
- );
119
- const [input, setInput] = useState(initialInput);
120
- const handleSubmit = useCallback(
121
- (e) => {
122
- e.preventDefault();
123
- if (!input)
124
- return;
125
- return trigger(input);
126
- },
127
- [input, trigger]
128
- );
129
- const handleInputChange = (e) => {
130
- setInput(e.target.value);
131
- };
132
- const complete = useCallback(
133
- (prompt) => __async(this, null, function* () {
134
- return trigger(prompt);
135
- }),
136
- [trigger]
137
- );
138
- return {
139
- completion,
140
- complete,
141
- error,
142
- setCompletion,
143
- stop,
144
- input,
145
- setInput,
146
- handleInputChange,
147
- handleSubmit,
148
- isLoading: isMutating
149
- };
150
- }
151
-
152
- export {
153
- useCompletion
154
- };
@@ -1,123 +0,0 @@
1
- /**
2
- * Shared types between the API and UI packages.
3
- */
4
- type Message = {
5
- id: string;
6
- createdAt?: Date;
7
- content: string;
8
- role: 'system' | 'user' | 'assistant';
9
- };
10
- type CreateMessage = {
11
- id?: string;
12
- createdAt?: Date;
13
- content: string;
14
- role: 'system' | 'user' | 'assistant';
15
- };
16
- type UseChatOptions = {
17
- /**
18
- * The API endpoint that accepts a `{ messages: Message[] }` object and returns
19
- * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
20
- */
21
- api?: string;
22
- /**
23
- * An unique identifier for the chat. If not provided, a random one will be
24
- * generated. When provided, the `useChat` hook with the same `id` will
25
- * have shared states across components.
26
- */
27
- id?: string;
28
- /**
29
- * Initial messages of the chat. Useful to load an existing chat history.
30
- */
31
- initialMessages?: Message[];
32
- /**
33
- * Initial input of the chat.
34
- */
35
- initialInput?: string;
36
- /**
37
- * Callback function to be called when the API response is received.
38
- */
39
- onResponse?: (response: Response) => void;
40
- /**
41
- * Callback function to be called when the chat is finished streaming.
42
- */
43
- onFinish?: (message: Message) => void;
44
- /**
45
- * Callback function to be called when an error is encountered.
46
- */
47
- onError?: (error: Error) => void;
48
- /**
49
- * HTTP headers to be sent with the API request.
50
- */
51
- headers?: Record<string, string> | Headers;
52
- /**
53
- * Extra body object to be sent with the API request.
54
- * @example
55
- * Send a `sessionId` to the API along with the messages.
56
- * ```js
57
- * useChat({
58
- * body: {
59
- * sessionId: '123',
60
- * }
61
- * })
62
- * ```
63
- */
64
- body?: object;
65
- /**
66
- * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
67
- * Defaults to `false`. When set to `true`, the API endpoint might need to
68
- * handle the extra fields before forwarding the request to the AI service.
69
- */
70
- sendExtraMessageFields?: boolean;
71
- };
72
- type UseCompletionOptions = {
73
- /**
74
- * The API endpoint that accepts a `{ prompt: string }` object and returns
75
- * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
76
- */
77
- api?: string;
78
- /**
79
- * An unique identifier for the chat. If not provided, a random one will be
80
- * generated. When provided, the `useChat` hook with the same `id` will
81
- * have shared states across components.
82
- */
83
- id?: string;
84
- /**
85
- * Initial prompt input of the completion.
86
- */
87
- initialInput?: string;
88
- /**
89
- * Initial completion result. Useful to load an existing history.
90
- */
91
- initialCompletion?: string;
92
- /**
93
- * Callback function to be called when the API response is received.
94
- */
95
- onResponse?: (response: Response) => void;
96
- /**
97
- * Callback function to be called when the completion is finished streaming.
98
- */
99
- onFinish?: (prompt: string, completion: string) => void;
100
- /**
101
- * Callback function to be called when an error is encountered.
102
- */
103
- onError?: (error: Error) => void;
104
- /**
105
- * HTTP headers to be sent with the API request.
106
- */
107
- headers?: Record<string, string> | Headers;
108
- /**
109
- * Extra body object to be sent with the API request.
110
- * @example
111
- * Send a `sessionId` to the API along with the prompt.
112
- * ```js
113
- * useChat({
114
- * body: {
115
- * sessionId: '123',
116
- * }
117
- * })
118
- * ```
119
- */
120
- body?: object;
121
- };
122
-
123
- export { CreateMessage as C, Message as M, UseChatOptions as U, UseCompletionOptions as a };
@@ -1,42 +0,0 @@
1
- import { M as Message, C as CreateMessage, U as UseChatOptions } from './types-f862f74a.js';
2
-
3
- type UseChatHelpers = {
4
- /** Current messages in the chat */
5
- messages: Message[];
6
- /** The error object of the API request */
7
- error: undefined | Error;
8
- /**
9
- * Append a user message to the chat list. This triggers the API call to fetch
10
- * the assistant's response.
11
- */
12
- append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
13
- /**
14
- * Reload the last AI chat response for the given chat history. If the last
15
- * message isn't from the assistant, it will request the API to generate a
16
- * new response.
17
- */
18
- reload: () => Promise<string | null | undefined>;
19
- /**
20
- * Abort the current request immediately, keep the generated tokens if any.
21
- */
22
- stop: () => void;
23
- /**
24
- * Update the `messages` state locally. This is useful when you want to
25
- * edit the messages on the client, and then trigger the `reload` method
26
- * manually to regenerate the AI response.
27
- */
28
- setMessages: (messages: Message[]) => void;
29
- /** The current value of the input */
30
- input: string;
31
- /** setState-powered method to update the input value */
32
- setInput: React.Dispatch<React.SetStateAction<string>>;
33
- /** An input/textarea-ready onChange handler to control the value of the input */
34
- handleInputChange: (e: any) => void;
35
- /** Form submission handler to automattically reset input and append a user message */
36
- handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
37
- /** Whether the API request is in progress */
38
- isLoading: boolean;
39
- };
40
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
41
-
42
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions, useChat };