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,177 +0,0 @@
1
- import {
2
- $,
3
- __async,
4
- __spreadValues,
5
- decodeAIStreamChunk,
6
- nanoid
7
- } from "./chunk-CENOSGDG.mjs";
8
-
9
- // svelte/use-chat.ts
10
- import { get, writable } from "svelte/store";
11
- var uniqueId = 0;
12
- var store = {};
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 chatId = id || `chat-${uniqueId++}`;
26
- const key = `${api}|${chatId}`;
27
- const { data, mutate: originalMutate } = $(key, {
28
- fetcher: () => store[key] || initialMessages,
29
- initialData: initialMessages
30
- });
31
- data.set(initialMessages);
32
- const mutate = (data2) => {
33
- store[key] = data2;
34
- return originalMutate(data2);
35
- };
36
- const messages = data;
37
- const error = writable(void 0);
38
- const isLoading = writable(false);
39
- let abortController = null;
40
- function triggerRequest(messagesSnapshot) {
41
- return __async(this, null, function* () {
42
- try {
43
- isLoading.set(true);
44
- abortController = new AbortController();
45
- const previousMessages = get(messages);
46
- mutate(messagesSnapshot);
47
- const res = yield fetch(api, {
48
- method: "POST",
49
- body: JSON.stringify(__spreadValues({
50
- messages: sendExtraMessageFields ? messagesSnapshot : messagesSnapshot.map(({ role, content }) => ({
51
- role,
52
- content
53
- }))
54
- }, body)),
55
- headers: headers || {},
56
- signal: abortController.signal
57
- }).catch((err) => {
58
- mutate(previousMessages);
59
- throw err;
60
- });
61
- if (onResponse) {
62
- try {
63
- yield onResponse(res);
64
- } catch (err) {
65
- throw err;
66
- }
67
- }
68
- if (!res.ok) {
69
- mutate(previousMessages);
70
- throw new Error(
71
- (yield res.text()) || "Failed to fetch the chat response."
72
- );
73
- }
74
- if (!res.body) {
75
- throw new Error("The response body is empty.");
76
- }
77
- let result = "";
78
- const createdAt = /* @__PURE__ */ new Date();
79
- const replyId = nanoid();
80
- const reader = res.body.getReader();
81
- while (true) {
82
- const { done, value } = yield reader.read();
83
- if (done) {
84
- break;
85
- }
86
- result += decodeAIStreamChunk(value);
87
- mutate([
88
- ...messagesSnapshot,
89
- {
90
- id: replyId,
91
- createdAt,
92
- content: result,
93
- role: "assistant"
94
- }
95
- ]);
96
- if (abortController === null) {
97
- reader.cancel();
98
- break;
99
- }
100
- }
101
- if (onFinish) {
102
- onFinish({
103
- id: replyId,
104
- createdAt,
105
- content: result,
106
- role: "assistant"
107
- });
108
- }
109
- abortController = null;
110
- return result;
111
- } catch (err) {
112
- if (err.name === "AbortError") {
113
- abortController = null;
114
- return null;
115
- }
116
- if (onError && err instanceof Error) {
117
- onError(err);
118
- }
119
- error.set(err);
120
- } finally {
121
- isLoading.set(false);
122
- }
123
- });
124
- }
125
- const append = (message) => __async(this, null, function* () {
126
- if (!message.id) {
127
- message.id = nanoid();
128
- }
129
- return triggerRequest(get(messages).concat(message));
130
- });
131
- const reload = () => __async(this, null, function* () {
132
- const messagesSnapshot = get(messages);
133
- if (messagesSnapshot.length === 0)
134
- return null;
135
- const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];
136
- if (lastMessage.role === "assistant") {
137
- return triggerRequest(messagesSnapshot.slice(0, -1));
138
- }
139
- return triggerRequest(messagesSnapshot);
140
- });
141
- const stop = () => {
142
- if (abortController) {
143
- abortController.abort();
144
- abortController = null;
145
- }
146
- };
147
- const setMessages = (messages2) => {
148
- mutate(messages2);
149
- };
150
- const input = writable(initialInput);
151
- const handleSubmit = (e) => {
152
- e.preventDefault();
153
- const inputValue = get(input);
154
- if (!inputValue)
155
- return;
156
- append({
157
- content: inputValue,
158
- role: "user"
159
- });
160
- input.set("");
161
- };
162
- return {
163
- messages,
164
- append,
165
- error,
166
- reload,
167
- stop,
168
- setMessages,
169
- input,
170
- handleSubmit,
171
- isLoading
172
- };
173
- }
174
-
175
- export {
176
- useChat
177
- };
@@ -1,136 +0,0 @@
1
- import {
2
- $,
3
- __async,
4
- __spreadValues,
5
- decodeAIStreamChunk
6
- } from "./chunk-CENOSGDG.mjs";
7
-
8
- // svelte/use-completion.ts
9
- import { get, writable } from "svelte/store";
10
- var uniqueId = 0;
11
- var store = {};
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 completionId = id || `completion-${uniqueId++}`;
24
- const key = `${api}|${completionId}`;
25
- const { data, mutate: originalMutate } = $(key, {
26
- fetcher: () => store[key] || initialCompletion,
27
- initialData: initialCompletion
28
- });
29
- data.set(initialCompletion);
30
- const mutate = (data2) => {
31
- store[key] = data2;
32
- return originalMutate(data2);
33
- };
34
- const completion = data;
35
- const error = writable(void 0);
36
- const isLoading = writable(false);
37
- let abortController = null;
38
- function triggerRequest(prompt) {
39
- return __async(this, null, function* () {
40
- try {
41
- isLoading.set(true);
42
- abortController = new AbortController();
43
- mutate("");
44
- const res = yield fetch(api, {
45
- method: "POST",
46
- body: JSON.stringify(__spreadValues({
47
- prompt
48
- }, body)),
49
- headers: headers || {},
50
- signal: abortController.signal
51
- }).catch((err) => {
52
- throw err;
53
- });
54
- if (onResponse) {
55
- try {
56
- yield onResponse(res);
57
- } catch (err) {
58
- throw err;
59
- }
60
- }
61
- if (!res.ok) {
62
- throw new Error(
63
- (yield res.text()) || "Failed to fetch the chat response."
64
- );
65
- }
66
- if (!res.body) {
67
- throw new Error("The response body is empty.");
68
- }
69
- let result = "";
70
- const reader = res.body.getReader();
71
- while (true) {
72
- const { done, value } = yield reader.read();
73
- if (done) {
74
- break;
75
- }
76
- result += decodeAIStreamChunk(value);
77
- mutate(result);
78
- if (abortController === null) {
79
- reader.cancel();
80
- break;
81
- }
82
- }
83
- if (onFinish) {
84
- onFinish(prompt, result);
85
- }
86
- abortController = null;
87
- return result;
88
- } catch (err) {
89
- if (err.name === "AbortError") {
90
- abortController = null;
91
- return null;
92
- }
93
- if (onError && error instanceof Error) {
94
- onError(error);
95
- }
96
- error.set(err);
97
- } finally {
98
- isLoading.set(false);
99
- }
100
- });
101
- }
102
- const complete = (prompt) => __async(this, null, function* () {
103
- return triggerRequest(prompt);
104
- });
105
- const stop = () => {
106
- if (abortController) {
107
- abortController.abort();
108
- abortController = null;
109
- }
110
- };
111
- const setCompletion = (completion2) => {
112
- mutate(completion2);
113
- };
114
- const input = writable(initialInput);
115
- const handleSubmit = (e) => {
116
- e.preventDefault();
117
- const inputValue = get(input);
118
- if (!inputValue)
119
- return;
120
- return complete(inputValue);
121
- };
122
- return {
123
- completion,
124
- complete,
125
- error,
126
- stop,
127
- setCompletion,
128
- input,
129
- handleSubmit,
130
- isLoading
131
- };
132
- }
133
-
134
- export {
135
- useCompletion
136
- };