@ai-sdk/react 0.0.52 → 0.0.53

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @ai-sdk/react
2
2
 
3
+ ## 0.0.53
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [aa2dc58]
8
+ - @ai-sdk/ui-utils@0.0.40
9
+
3
10
  ## 0.0.52
4
11
 
5
12
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/react",
3
- "version": "0.0.52",
3
+ "version": "0.0.53",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -14,9 +14,13 @@
14
14
  "require": "./dist/index.js"
15
15
  }
16
16
  },
17
+ "files": [
18
+ "dist/**/*",
19
+ "CHANGELOG.md"
20
+ ],
17
21
  "dependencies": {
18
22
  "@ai-sdk/provider-utils": "1.0.17",
19
- "@ai-sdk/ui-utils": "0.0.39",
23
+ "@ai-sdk/ui-utils": "0.0.40",
20
24
  "swr": "2.2.5"
21
25
  },
22
26
  "devDependencies": {
package/.eslintrc.js DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- extends: ['vercel-ai'],
4
- };
@@ -1,21 +0,0 @@
1
-
2
- > @ai-sdk/react@0.0.52 build /home/runner/work/ai/ai/packages/react
3
- > tsup
4
-
5
- CLI Building entry: src/index.ts
6
- CLI Using tsconfig: tsconfig.json
7
- CLI tsup v7.2.0
8
- CLI Using tsup config: /home/runner/work/ai/ai/packages/react/tsup.config.ts
9
- CLI Target: es2018
10
- CJS Build start
11
- ESM Build start
12
- ESM dist/index.mjs 25.83 KB
13
- ESM dist/index.mjs.map 56.29 KB
14
- ESM ⚡️ Build success in 44ms
15
- CJS dist/index.js 28.32 KB
16
- CJS dist/index.js.map 56.33 KB
17
- CJS ⚡️ Build success in 44ms
18
- DTS Build start
19
- DTS ⚡️ Build success in 5665ms
20
- DTS dist/index.d.ts 11.05 KB
21
- DTS dist/index.d.mts 11.05 KB
@@ -1,4 +0,0 @@
1
-
2
- > @ai-sdk/react@0.0.52 clean /home/runner/work/ai/ai/packages/react
3
- > rm -rf dist
4
-
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './use-assistant';
2
- export * from './use-chat';
3
- export * from './use-completion';
4
- export * from './use-object';
@@ -1,301 +0,0 @@
1
- import { isAbortError } from '@ai-sdk/provider-utils';
2
- import {
3
- AssistantStatus,
4
- CreateMessage,
5
- Message,
6
- UseAssistantOptions,
7
- generateId,
8
- readDataStream,
9
- } from '@ai-sdk/ui-utils';
10
- import { useCallback, useRef, useState } from 'react';
11
-
12
- // use function to allow for mocking in tests:
13
- const getOriginalFetch = () => fetch;
14
-
15
- export type UseAssistantHelpers = {
16
- /**
17
- * The current array of chat messages.
18
- */
19
- messages: Message[];
20
-
21
- /**
22
- * Update the message store with a new array of messages.
23
- */
24
- setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
25
-
26
- /**
27
- * The current thread ID.
28
- */
29
- threadId: string | undefined;
30
-
31
- /**
32
- * Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.
33
- */
34
- setThreadId: (threadId: string | undefined) => void;
35
-
36
- /**
37
- * The current value of the input field.
38
- */
39
- input: string;
40
-
41
- /**
42
- * Append a user message to the chat list. This triggers the API call to fetch
43
- * the assistant's response.
44
- * @param message The message to append
45
- * @param requestOptions Additional options to pass to the API call
46
- */
47
- append: (
48
- message: Message | CreateMessage,
49
- requestOptions?: {
50
- data?: Record<string, string>;
51
- },
52
- ) => Promise<void>;
53
-
54
- /**
55
- Abort the current request immediately, keep the generated tokens if any.
56
- */
57
- stop: () => void;
58
-
59
- /**
60
- * setState-powered method to update the input value.
61
- */
62
- setInput: React.Dispatch<React.SetStateAction<string>>;
63
-
64
- /**
65
- * Handler for the `onChange` event of the input field to control the input's value.
66
- */
67
- handleInputChange: (
68
- event:
69
- | React.ChangeEvent<HTMLInputElement>
70
- | React.ChangeEvent<HTMLTextAreaElement>,
71
- ) => void;
72
-
73
- /**
74
- * Form submission handler that automatically resets the input field and appends a user message.
75
- */
76
- submitMessage: (
77
- event?: React.FormEvent<HTMLFormElement>,
78
- requestOptions?: {
79
- data?: Record<string, string>;
80
- },
81
- ) => Promise<void>;
82
-
83
- /**
84
- * The current status of the assistant. This can be used to show a loading indicator.
85
- */
86
- status: AssistantStatus;
87
-
88
- /**
89
- * The error thrown during the assistant message processing, if any.
90
- */
91
- error: undefined | Error;
92
- };
93
-
94
- export function useAssistant({
95
- api,
96
- threadId: threadIdParam,
97
- credentials,
98
- headers,
99
- body,
100
- onError,
101
- fetch,
102
- }: UseAssistantOptions): UseAssistantHelpers {
103
- const [messages, setMessages] = useState<Message[]>([]);
104
- const [input, setInput] = useState('');
105
- const [currentThreadId, setCurrentThreadId] = useState<string | undefined>(
106
- undefined,
107
- );
108
- const [status, setStatus] = useState<AssistantStatus>('awaiting_message');
109
- const [error, setError] = useState<undefined | Error>(undefined);
110
-
111
- const handleInputChange = (
112
- event:
113
- | React.ChangeEvent<HTMLInputElement>
114
- | React.ChangeEvent<HTMLTextAreaElement>,
115
- ) => {
116
- setInput(event.target.value);
117
- };
118
-
119
- // Abort controller to cancel the current API call.
120
- const abortControllerRef = useRef<AbortController | null>(null);
121
-
122
- const stop = useCallback(() => {
123
- if (abortControllerRef.current) {
124
- abortControllerRef.current.abort();
125
- abortControllerRef.current = null;
126
- }
127
- }, []);
128
-
129
- const append = async (
130
- message: Message | CreateMessage,
131
- requestOptions?: {
132
- data?: Record<string, string>;
133
- },
134
- ) => {
135
- setStatus('in_progress');
136
-
137
- setMessages(messages => [
138
- ...messages,
139
- {
140
- ...message,
141
- id: message.id ?? generateId(),
142
- },
143
- ]);
144
-
145
- setInput('');
146
-
147
- const abortController = new AbortController();
148
-
149
- try {
150
- abortControllerRef.current = abortController;
151
-
152
- const actualFetch = fetch ?? getOriginalFetch();
153
- const response = await actualFetch(api, {
154
- method: 'POST',
155
- credentials,
156
- signal: abortController.signal,
157
- headers: { 'Content-Type': 'application/json', ...headers },
158
- body: JSON.stringify({
159
- ...body,
160
- // always use user-provided threadId when available:
161
- threadId: threadIdParam ?? currentThreadId ?? null,
162
- message: message.content,
163
-
164
- // optional request data:
165
- data: requestOptions?.data,
166
- }),
167
- });
168
-
169
- if (!response.ok) {
170
- throw new Error(
171
- (await response.text()) ?? 'Failed to fetch the assistant response.',
172
- );
173
- }
174
-
175
- if (response.body == null) {
176
- throw new Error('The response body is empty.');
177
- }
178
-
179
- for await (const { type, value } of readDataStream(
180
- response.body.getReader(),
181
- )) {
182
- switch (type) {
183
- case 'assistant_message': {
184
- setMessages(messages => [
185
- ...messages,
186
- {
187
- id: value.id,
188
- role: value.role,
189
- content: value.content[0].text.value,
190
- },
191
- ]);
192
- break;
193
- }
194
-
195
- case 'text': {
196
- // text delta - add to last message:
197
- setMessages(messages => {
198
- const lastMessage = messages[messages.length - 1];
199
- return [
200
- ...messages.slice(0, messages.length - 1),
201
- {
202
- id: lastMessage.id,
203
- role: lastMessage.role,
204
- content: lastMessage.content + value,
205
- },
206
- ];
207
- });
208
-
209
- break;
210
- }
211
-
212
- case 'data_message': {
213
- setMessages(messages => [
214
- ...messages,
215
- {
216
- id: value.id ?? generateId(),
217
- role: 'data',
218
- content: '',
219
- data: value.data,
220
- },
221
- ]);
222
- break;
223
- }
224
-
225
- case 'assistant_control_data': {
226
- setCurrentThreadId(value.threadId);
227
-
228
- // set id of last message:
229
- setMessages(messages => {
230
- const lastMessage = messages[messages.length - 1];
231
- lastMessage.id = value.messageId;
232
- return [...messages.slice(0, messages.length - 1), lastMessage];
233
- });
234
-
235
- break;
236
- }
237
-
238
- case 'error': {
239
- setError(new Error(value));
240
- break;
241
- }
242
- }
243
- }
244
- } catch (error) {
245
- // Ignore abort errors as they are expected when the user cancels the request:
246
- if (isAbortError(error) && abortController.signal.aborted) {
247
- abortControllerRef.current = null;
248
- return;
249
- }
250
-
251
- if (onError && error instanceof Error) {
252
- onError(error);
253
- }
254
-
255
- setError(error as Error);
256
- } finally {
257
- abortControllerRef.current = null;
258
- setStatus('awaiting_message');
259
- }
260
- };
261
-
262
- const submitMessage = async (
263
- event?: React.FormEvent<HTMLFormElement>,
264
- requestOptions?: {
265
- data?: Record<string, string>;
266
- },
267
- ) => {
268
- event?.preventDefault?.();
269
-
270
- if (input === '') {
271
- return;
272
- }
273
-
274
- append({ role: 'user', content: input }, requestOptions);
275
- };
276
-
277
- const setThreadId = (threadId: string | undefined) => {
278
- setCurrentThreadId(threadId);
279
- setMessages([]);
280
- };
281
-
282
- return {
283
- append,
284
- messages,
285
- setMessages,
286
- threadId: currentThreadId,
287
- setThreadId,
288
- input,
289
- setInput,
290
- handleInputChange,
291
- submitMessage,
292
- status,
293
- error,
294
- stop,
295
- };
296
- }
297
-
298
- /**
299
- @deprecated Use `useAssistant` instead.
300
- */
301
- export const experimental_useAssistant = useAssistant;