@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 +7 -0
- package/package.json +6 -2
- package/.eslintrc.js +0 -4
- package/.turbo/turbo-build.log +0 -21
- package/.turbo/turbo-clean.log +0 -4
- package/src/index.ts +0 -4
- package/src/use-assistant.ts +0 -301
- package/src/use-assistant.ui.test.tsx +0 -337
- package/src/use-chat.ts +0 -716
- package/src/use-chat.ui.test.tsx +0 -1542
- package/src/use-completion.ts +0 -213
- package/src/use-completion.ui.test.tsx +0 -166
- package/src/use-object.ts +0 -229
- package/src/use-object.ui.test.tsx +0 -218
- package/tsconfig.json +0 -9
- package/tsup.config.ts +0 -13
- package/turbo.json +0 -8
- package/vitest.config.js +0 -12
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/react",
|
|
3
|
-
"version": "0.0.
|
|
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.
|
|
23
|
+
"@ai-sdk/ui-utils": "0.0.40",
|
|
20
24
|
"swr": "2.2.5"
|
|
21
25
|
},
|
|
22
26
|
"devDependencies": {
|
package/.eslintrc.js
DELETED
package/.turbo/turbo-build.log
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @ai-sdk/react@0.0.52 build /home/runner/work/ai/ai/packages/react
|
|
3
|
-
> tsup
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v7.2.0
|
|
8
|
-
[34mCLI[39m Using tsup config: /home/runner/work/ai/ai/packages/react/tsup.config.ts
|
|
9
|
-
[34mCLI[39m Target: es2018
|
|
10
|
-
[34mCJS[39m Build start
|
|
11
|
-
[34mESM[39m Build start
|
|
12
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m25.83 KB[39m
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs.map [22m[32m56.29 KB[39m
|
|
14
|
-
[32mESM[39m ⚡️ Build success in 44ms
|
|
15
|
-
[32mCJS[39m [1mdist/index.js [22m[32m28.32 KB[39m
|
|
16
|
-
[32mCJS[39m [1mdist/index.js.map [22m[32m56.33 KB[39m
|
|
17
|
-
[32mCJS[39m ⚡️ Build success in 44ms
|
|
18
|
-
[34mDTS[39m Build start
|
|
19
|
-
[32mDTS[39m ⚡️ Build success in 5665ms
|
|
20
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m11.05 KB[39m
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m11.05 KB[39m
|
package/.turbo/turbo-clean.log
DELETED
package/src/index.ts
DELETED
package/src/use-assistant.ts
DELETED
|
@@ -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;
|