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,137 +0,0 @@
1
- import {
2
- __async,
3
- __spreadValues,
4
- decodeAIStreamChunk
5
- } from "./chunk-WXH4YPZV.mjs";
6
-
7
- // vue/use-completion.ts
8
- import swrv from "swrv";
9
- import { ref } from "vue";
10
- var uniqueId = 0;
11
- var useSWRV = swrv.default || swrv;
12
- var store = {};
13
- function useCompletion({
14
- api = "/api/completion",
15
- id,
16
- initialCompletion = "",
17
- initialInput = "",
18
- headers,
19
- body,
20
- onResponse,
21
- onFinish,
22
- onError
23
- } = {}) {
24
- const completionId = id || `completion-${uniqueId++}`;
25
- const key = `${api}|${completionId}`;
26
- const { data, mutate: originalMutate } = useSWRV(
27
- key,
28
- () => store[key] || initialCompletion
29
- );
30
- data.value || (data.value = initialCompletion);
31
- const mutate = (data2) => {
32
- store[key] = data2;
33
- return originalMutate();
34
- };
35
- const completion = data;
36
- const error = ref(void 0);
37
- const isLoading = ref(false);
38
- let abortController = null;
39
- function triggerRequest(prompt) {
40
- return __async(this, null, function* () {
41
- try {
42
- isLoading.value = true;
43
- abortController = new AbortController();
44
- mutate("");
45
- const res = yield fetch(api, {
46
- method: "POST",
47
- body: JSON.stringify(__spreadValues({
48
- prompt
49
- }, body)),
50
- headers: headers || {},
51
- signal: abortController.signal
52
- }).catch((err) => {
53
- throw err;
54
- });
55
- if (onResponse) {
56
- try {
57
- yield onResponse(res);
58
- } catch (err) {
59
- throw err;
60
- }
61
- }
62
- if (!res.ok) {
63
- throw new Error(
64
- (yield res.text()) || "Failed to fetch the chat response."
65
- );
66
- }
67
- if (!res.body) {
68
- throw new Error("The response body is empty.");
69
- }
70
- let result = "";
71
- const reader = res.body.getReader();
72
- while (true) {
73
- const { done, value } = yield reader.read();
74
- if (done) {
75
- break;
76
- }
77
- result += decodeAIStreamChunk(value);
78
- mutate(result);
79
- if (abortController === null) {
80
- reader.cancel();
81
- break;
82
- }
83
- }
84
- if (onFinish) {
85
- onFinish(prompt, result);
86
- }
87
- abortController = null;
88
- return result;
89
- } catch (err) {
90
- if (err.name === "AbortError") {
91
- abortController = null;
92
- return null;
93
- }
94
- if (onError && error instanceof Error) {
95
- onError(error);
96
- }
97
- error.value = err;
98
- } finally {
99
- isLoading.value = false;
100
- }
101
- });
102
- }
103
- const complete = (prompt) => __async(this, null, function* () {
104
- return triggerRequest(prompt);
105
- });
106
- const stop = () => {
107
- if (abortController) {
108
- abortController.abort();
109
- abortController = null;
110
- }
111
- };
112
- const setCompletion = (completion2) => {
113
- mutate(completion2);
114
- };
115
- const input = ref(initialInput);
116
- const handleSubmit = (e) => {
117
- e.preventDefault();
118
- const inputValue = input.value;
119
- if (!inputValue)
120
- return;
121
- return complete(inputValue);
122
- };
123
- return {
124
- completion,
125
- complete,
126
- error,
127
- stop,
128
- setCompletion,
129
- input,
130
- handleSubmit,
131
- isLoading
132
- };
133
- }
134
-
135
- export {
136
- useCompletion
137
- };
@@ -1,178 +0,0 @@
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,54 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __async = (__this, __arguments, generator) => {
18
- return new Promise((resolve, reject) => {
19
- var fulfilled = (value) => {
20
- try {
21
- step(generator.next(value));
22
- } catch (e) {
23
- reject(e);
24
- }
25
- };
26
- var rejected = (value) => {
27
- try {
28
- step(generator.throw(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
- step((generator = generator.apply(__this, __arguments)).next());
35
- });
36
- };
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
-
49
- export {
50
- __spreadValues,
51
- __async,
52
- nanoid,
53
- decodeAIStreamChunk
54
- };
@@ -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,39 +0,0 @@
1
- import { Ref } from 'vue';
2
- import { M as Message, C as CreateMessage, U as UseChatOptions } from './types-f862f74a.js';
3
-
4
- type UseChatHelpers = {
5
- /** Current messages in the chat */
6
- messages: Ref<Message[]>;
7
- /** The error object of the API request */
8
- error: Ref<undefined | Error>;
9
- /**
10
- * Append a user message to the chat list. This triggers the API call to fetch
11
- * the assistant's response.
12
- */
13
- append: (message: Message | CreateMessage) => Promise<string | null | undefined>;
14
- /**
15
- * Reload the last AI chat response for the given chat history. If the last
16
- * message isn't from the assistant, it will request the API to generate a
17
- * new response.
18
- */
19
- reload: () => Promise<string | null | undefined>;
20
- /**
21
- * Abort the current request immediately, keep the generated tokens if any.
22
- */
23
- stop: () => void;
24
- /**
25
- * Update the `messages` state locally. This is useful when you want to
26
- * edit the messages on the client, and then trigger the `reload` method
27
- * manually to regenerate the AI response.
28
- */
29
- setMessages: (messages: Message[]) => void;
30
- /** The current value of the input */
31
- input: Ref<string>;
32
- /** Form submission handler to automattically reset input and append a user message */
33
- handleSubmit: (e: any) => void;
34
- /** Whether the API request is in progress */
35
- isLoading: Ref<boolean>;
36
- };
37
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, headers, body }?: UseChatOptions): UseChatHelpers;
38
-
39
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions, useChat };