@ai-sdk/react 2.0.0-canary.21 → 2.0.0-canary.22
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 +12 -0
- package/dist/index.d.mts +1 -15
- package/dist/index.d.ts +1 -15
- package/dist/index.js +146 -277
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -266
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @ai-sdk/react
|
|
2
2
|
|
|
3
|
+
## 2.0.0-canary.22
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 40acf9b: feat (ui): introduce ChatStore and ChatTransport
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [40acf9b]
|
|
12
|
+
- @ai-sdk/provider-utils@3.0.0-canary.18
|
|
13
|
+
- ai@5.0.0-canary.23
|
|
14
|
+
|
|
3
15
|
## 2.0.0-canary.21
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -66,21 +66,7 @@ type UseChatHelpers<MESSAGE_METADATA = unknown> = {
|
|
|
66
66
|
result: any;
|
|
67
67
|
}) => void;
|
|
68
68
|
};
|
|
69
|
-
declare function useChat<MESSAGE_METADATA>({
|
|
70
|
-
/**
|
|
71
|
-
* Experimental (React only). When a function is provided, it will be used
|
|
72
|
-
* to prepare the request body for the chat API. This can be useful for
|
|
73
|
-
* customizing the request body based on the messages and data in the chat.
|
|
74
|
-
*
|
|
75
|
-
* @param id The id of the chat.
|
|
76
|
-
* @param messages The current messages in the chat.
|
|
77
|
-
* @param requestBody The request body object passed in the chat request.
|
|
78
|
-
*/
|
|
79
|
-
experimental_prepareRequestBody?: (options: {
|
|
80
|
-
id: string;
|
|
81
|
-
messages: UIMessage<MESSAGE_METADATA>[];
|
|
82
|
-
requestBody?: object;
|
|
83
|
-
}) => unknown;
|
|
69
|
+
declare function useChat<MESSAGE_METADATA>({ id, initialInput, onToolCall, onFinish, onError, generateId, experimental_throttle: throttleWaitMs, chatStore: chatStoreArg, }?: UseChatOptions<MESSAGE_METADATA> & {
|
|
84
70
|
/**
|
|
85
71
|
Custom throttle wait in ms for the chat messages and data updates.
|
|
86
72
|
Default is undefined, which disables throttling.
|
package/dist/index.d.ts
CHANGED
|
@@ -66,21 +66,7 @@ type UseChatHelpers<MESSAGE_METADATA = unknown> = {
|
|
|
66
66
|
result: any;
|
|
67
67
|
}) => void;
|
|
68
68
|
};
|
|
69
|
-
declare function useChat<MESSAGE_METADATA>({
|
|
70
|
-
/**
|
|
71
|
-
* Experimental (React only). When a function is provided, it will be used
|
|
72
|
-
* to prepare the request body for the chat API. This can be useful for
|
|
73
|
-
* customizing the request body based on the messages and data in the chat.
|
|
74
|
-
*
|
|
75
|
-
* @param id The id of the chat.
|
|
76
|
-
* @param messages The current messages in the chat.
|
|
77
|
-
* @param requestBody The request body object passed in the chat request.
|
|
78
|
-
*/
|
|
79
|
-
experimental_prepareRequestBody?: (options: {
|
|
80
|
-
id: string;
|
|
81
|
-
messages: UIMessage<MESSAGE_METADATA>[];
|
|
82
|
-
requestBody?: object;
|
|
83
|
-
}) => unknown;
|
|
69
|
+
declare function useChat<MESSAGE_METADATA>({ id, initialInput, onToolCall, onFinish, onError, generateId, experimental_throttle: throttleWaitMs, chatStore: chatStoreArg, }?: UseChatOptions<MESSAGE_METADATA> & {
|
|
84
70
|
/**
|
|
85
71
|
Custom throttle wait in ms for the chat messages and data updates.
|
|
86
72
|
Default is undefined, which disables throttling.
|
package/dist/index.js
CHANGED
|
@@ -37,291 +37,152 @@ __export(src_exports, {
|
|
|
37
37
|
module.exports = __toCommonJS(src_exports);
|
|
38
38
|
|
|
39
39
|
// src/use-chat.ts
|
|
40
|
-
var import_ai2 = require("ai");
|
|
41
|
-
var import_react2 = require("react");
|
|
42
|
-
var import_swr = __toESM(require("swr"));
|
|
43
|
-
|
|
44
|
-
// src/throttle.ts
|
|
45
|
-
var import_throttleit = __toESM(require("throttleit"));
|
|
46
|
-
function throttle(fn, waitMs) {
|
|
47
|
-
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/util/use-stable-value.ts
|
|
51
40
|
var import_ai = require("ai");
|
|
52
41
|
var import_react = require("react");
|
|
53
|
-
function useStableValue(latestValue) {
|
|
54
|
-
const [value, setValue] = (0, import_react.useState)(latestValue);
|
|
55
|
-
(0, import_react.useEffect)(() => {
|
|
56
|
-
if (!(0, import_ai.isDeepEqualData)(latestValue, value)) {
|
|
57
|
-
setValue(latestValue);
|
|
58
|
-
}
|
|
59
|
-
}, [latestValue, value]);
|
|
60
|
-
return value;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// src/use-chat.ts
|
|
64
42
|
function useChat({
|
|
65
|
-
api = "/api/chat",
|
|
66
43
|
id,
|
|
67
|
-
initialMessages,
|
|
68
44
|
initialInput = "",
|
|
69
45
|
onToolCall,
|
|
70
|
-
experimental_prepareRequestBody,
|
|
71
|
-
maxSteps = 1,
|
|
72
|
-
streamProtocol = "ui-message",
|
|
73
46
|
onFinish,
|
|
74
47
|
onError,
|
|
75
|
-
|
|
76
|
-
headers,
|
|
77
|
-
body,
|
|
78
|
-
generateId = import_ai2.generateId,
|
|
79
|
-
fetch: fetch2,
|
|
48
|
+
generateId = import_ai.generateId,
|
|
80
49
|
experimental_throttle: throttleWaitMs,
|
|
81
|
-
|
|
50
|
+
chatStore: chatStoreArg
|
|
82
51
|
} = {}) {
|
|
83
|
-
const [hookId] = (0,
|
|
52
|
+
const [hookId] = (0, import_react.useState)(generateId);
|
|
84
53
|
const chatId = id != null ? id : hookId;
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
);
|
|
91
|
-
const { data: messages, mutate } = (0, import_swr.default)(
|
|
92
|
-
[chatKey, "messages"],
|
|
93
|
-
null,
|
|
94
|
-
{ fallbackData: processedInitialMessages }
|
|
54
|
+
const chatStore = (0, import_react.useRef)(
|
|
55
|
+
chatStoreArg != null ? chatStoreArg : (0, import_ai.defaultChatStore)({
|
|
56
|
+
api: "/api/chat",
|
|
57
|
+
generateId
|
|
58
|
+
})
|
|
95
59
|
);
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
credentials,
|
|
111
|
-
headers,
|
|
112
|
-
body
|
|
113
|
-
};
|
|
114
|
-
}, [credentials, headers, body]);
|
|
115
|
-
const triggerRequest = (0, import_react2.useCallback)(
|
|
116
|
-
async (chatRequest, requestType = "generate") => {
|
|
117
|
-
var _a;
|
|
118
|
-
mutateStatus("submitted");
|
|
119
|
-
setError(void 0);
|
|
120
|
-
const chatMessages = chatRequest.messages;
|
|
121
|
-
const messageCount = chatMessages.length;
|
|
122
|
-
const maxStep = (0, import_ai2.extractMaxToolInvocationStep)(
|
|
123
|
-
(0, import_ai2.getToolInvocations)(chatMessages[chatMessages.length - 1])
|
|
124
|
-
);
|
|
125
|
-
try {
|
|
126
|
-
const abortController = new AbortController();
|
|
127
|
-
abortControllerRef.current = abortController;
|
|
128
|
-
const throttledMutate = throttle(mutate, throttleWaitMs);
|
|
129
|
-
throttledMutate(chatMessages, false);
|
|
130
|
-
await (0, import_ai2.callChatApi)({
|
|
131
|
-
api,
|
|
132
|
-
body: (_a = experimental_prepareRequestBody == null ? void 0 : experimental_prepareRequestBody({
|
|
133
|
-
id: chatId,
|
|
134
|
-
messages: chatMessages,
|
|
135
|
-
requestBody: chatRequest.body
|
|
136
|
-
})) != null ? _a : {
|
|
137
|
-
id: chatId,
|
|
138
|
-
messages: chatMessages,
|
|
139
|
-
...extraMetadataRef.current.body,
|
|
140
|
-
...chatRequest.body
|
|
141
|
-
},
|
|
142
|
-
streamProtocol,
|
|
143
|
-
credentials: extraMetadataRef.current.credentials,
|
|
144
|
-
headers: {
|
|
145
|
-
...extraMetadataRef.current.headers,
|
|
146
|
-
...chatRequest.headers
|
|
147
|
-
},
|
|
148
|
-
abortController: () => abortControllerRef.current,
|
|
149
|
-
onUpdate({ message }) {
|
|
150
|
-
mutateStatus("streaming");
|
|
151
|
-
const replaceLastMessage = message.id === chatMessages[chatMessages.length - 1].id;
|
|
152
|
-
throttledMutate(
|
|
153
|
-
[
|
|
154
|
-
...replaceLastMessage ? chatMessages.slice(0, chatMessages.length - 1) : chatMessages,
|
|
155
|
-
message
|
|
156
|
-
],
|
|
157
|
-
false
|
|
158
|
-
);
|
|
159
|
-
},
|
|
160
|
-
onToolCall,
|
|
161
|
-
onFinish,
|
|
162
|
-
generateId,
|
|
163
|
-
fetch: fetch2,
|
|
164
|
-
lastMessage: chatMessages[chatMessages.length - 1],
|
|
165
|
-
requestType,
|
|
166
|
-
messageMetadataSchema
|
|
167
|
-
});
|
|
168
|
-
abortControllerRef.current = null;
|
|
169
|
-
mutateStatus("ready");
|
|
170
|
-
} catch (err) {
|
|
171
|
-
if (err.name === "AbortError") {
|
|
172
|
-
abortControllerRef.current = null;
|
|
173
|
-
mutateStatus("ready");
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
if (onError && err instanceof Error) {
|
|
177
|
-
onError(err);
|
|
60
|
+
if (!chatStore.current.hasChat(chatId)) {
|
|
61
|
+
chatStore.current.addChat(chatId, []);
|
|
62
|
+
}
|
|
63
|
+
const subscribe = (0, import_react.useCallback)(
|
|
64
|
+
({
|
|
65
|
+
onStoreChange,
|
|
66
|
+
eventType
|
|
67
|
+
}) => {
|
|
68
|
+
return chatStore.current.subscribe({
|
|
69
|
+
onChatChanged: (event) => {
|
|
70
|
+
if (event.chatId !== chatId || event.type !== eventType) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
onStoreChange();
|
|
178
74
|
}
|
|
179
|
-
|
|
180
|
-
mutateStatus("error");
|
|
181
|
-
}
|
|
182
|
-
const messages2 = messagesRef.current;
|
|
183
|
-
if ((0, import_ai2.shouldResubmitMessages)({
|
|
184
|
-
originalMaxToolInvocationStep: maxStep,
|
|
185
|
-
originalMessageCount: messageCount,
|
|
186
|
-
maxSteps,
|
|
187
|
-
messages: messages2
|
|
188
|
-
})) {
|
|
189
|
-
await triggerRequest({ messages: messages2 });
|
|
190
|
-
}
|
|
75
|
+
});
|
|
191
76
|
},
|
|
192
|
-
[
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
77
|
+
[chatStore, chatId]
|
|
78
|
+
);
|
|
79
|
+
const addToolResult = (0, import_react.useCallback)(
|
|
80
|
+
(options) => chatStore.current.addToolResult({ chatId, ...options }),
|
|
81
|
+
[chatStore, chatId]
|
|
82
|
+
);
|
|
83
|
+
const stopStream = (0, import_react.useCallback)(() => {
|
|
84
|
+
chatStore.current.stopStream({ chatId });
|
|
85
|
+
}, [chatStore, chatId]);
|
|
86
|
+
const error = (0, import_react.useSyncExternalStore)(
|
|
87
|
+
(callback) => subscribe({
|
|
88
|
+
onStoreChange: callback,
|
|
89
|
+
eventType: "chat-status-changed"
|
|
90
|
+
}),
|
|
91
|
+
() => chatStore.current.getError(chatId),
|
|
92
|
+
() => chatStore.current.getError(chatId)
|
|
93
|
+
);
|
|
94
|
+
const status = (0, import_react.useSyncExternalStore)(
|
|
95
|
+
(callback) => subscribe({
|
|
96
|
+
onStoreChange: callback,
|
|
97
|
+
eventType: "chat-status-changed"
|
|
98
|
+
}),
|
|
99
|
+
() => chatStore.current.getStatus(chatId),
|
|
100
|
+
() => chatStore.current.getStatus(chatId)
|
|
101
|
+
);
|
|
102
|
+
const messages = (0, import_react.useSyncExternalStore)(
|
|
103
|
+
(callback) => {
|
|
104
|
+
return subscribe({
|
|
105
|
+
onStoreChange: callback,
|
|
106
|
+
eventType: "chat-messages-changed"
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
() => chatStore.current.getMessages(chatId),
|
|
110
|
+
() => chatStore.current.getMessages(chatId)
|
|
111
|
+
);
|
|
112
|
+
const append = (0, import_react.useCallback)(
|
|
113
|
+
(message, { headers, body } = {}) => chatStore.current.submitMessage({
|
|
114
|
+
chatId,
|
|
115
|
+
message,
|
|
116
|
+
headers,
|
|
117
|
+
body,
|
|
198
118
|
onError,
|
|
199
|
-
setError,
|
|
200
|
-
streamProtocol,
|
|
201
|
-
experimental_prepareRequestBody,
|
|
202
119
|
onToolCall,
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
120
|
+
onFinish
|
|
121
|
+
}),
|
|
122
|
+
[chatStore, chatId, onError, onToolCall, onFinish]
|
|
123
|
+
);
|
|
124
|
+
const reload = (0, import_react.useCallback)(
|
|
125
|
+
async ({ headers, body } = {}) => chatStore.current.resubmitLastUserMessage({
|
|
209
126
|
chatId,
|
|
210
|
-
|
|
211
|
-
|
|
127
|
+
headers,
|
|
128
|
+
body,
|
|
129
|
+
onError,
|
|
130
|
+
onToolCall,
|
|
131
|
+
onFinish
|
|
132
|
+
}),
|
|
133
|
+
[chatStore, chatId, onError, onToolCall, onFinish]
|
|
212
134
|
);
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
body: body2
|
|
223
|
-
});
|
|
224
|
-
},
|
|
225
|
-
[triggerRequest, generateId]
|
|
135
|
+
const stop = (0, import_react.useCallback)(() => stopStream(), [stopStream]);
|
|
136
|
+
const experimental_resume = (0, import_react.useCallback)(
|
|
137
|
+
async () => chatStore.current.resumeStream({
|
|
138
|
+
chatId,
|
|
139
|
+
onError,
|
|
140
|
+
onToolCall,
|
|
141
|
+
onFinish
|
|
142
|
+
}),
|
|
143
|
+
[chatStore, chatId, onError, onToolCall, onFinish]
|
|
226
144
|
);
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return null;
|
|
145
|
+
const setMessages = (0, import_react.useCallback)(
|
|
146
|
+
(messagesParam) => {
|
|
147
|
+
if (typeof messagesParam === "function") {
|
|
148
|
+
messagesParam = messagesParam(messages);
|
|
232
149
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
messages:
|
|
236
|
-
headers: headers2,
|
|
237
|
-
body: body2
|
|
150
|
+
chatStore.current.setMessages({
|
|
151
|
+
id: chatId,
|
|
152
|
+
messages: messagesParam
|
|
238
153
|
});
|
|
239
154
|
},
|
|
240
|
-
[
|
|
155
|
+
[chatId, messages]
|
|
241
156
|
);
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
abortControllerRef.current = null;
|
|
246
|
-
}
|
|
247
|
-
}, []);
|
|
248
|
-
const experimental_resume = (0, import_react2.useCallback)(async () => {
|
|
249
|
-
const messages2 = messagesRef.current;
|
|
250
|
-
triggerRequest({ messages: messages2 }, "resume");
|
|
251
|
-
}, [triggerRequest]);
|
|
252
|
-
const setMessages = (0, import_react2.useCallback)(
|
|
253
|
-
(messages2) => {
|
|
254
|
-
if (typeof messages2 === "function") {
|
|
255
|
-
messages2 = messages2(messagesRef.current);
|
|
256
|
-
}
|
|
257
|
-
mutate(messages2, false);
|
|
258
|
-
messagesRef.current = messages2;
|
|
259
|
-
},
|
|
260
|
-
[mutate]
|
|
261
|
-
);
|
|
262
|
-
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
263
|
-
const handleSubmit = (0, import_react2.useCallback)(
|
|
264
|
-
async (event, options = {}, metadata) => {
|
|
157
|
+
const [input, setInput] = (0, import_react.useState)(initialInput);
|
|
158
|
+
const handleSubmit = (0, import_react.useCallback)(
|
|
159
|
+
async (event, options = {}) => {
|
|
265
160
|
var _a;
|
|
266
161
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
267
|
-
const fileParts = Array.isArray(options == null ? void 0 : options.files) ? options.files : await (0,
|
|
162
|
+
const fileParts = Array.isArray(options == null ? void 0 : options.files) ? options.files : await (0, import_ai.convertFileListToFileUIParts)(options == null ? void 0 : options.files);
|
|
268
163
|
if (!input && fileParts.length === 0)
|
|
269
164
|
return;
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
...extraMetadataRef.current,
|
|
273
|
-
...metadata
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
triggerRequest({
|
|
277
|
-
messages: messagesRef.current.concat({
|
|
165
|
+
append(
|
|
166
|
+
{
|
|
278
167
|
id: generateId(),
|
|
279
168
|
role: "user",
|
|
280
169
|
metadata: void 0,
|
|
281
170
|
parts: [...fileParts, { type: "text", text: input }]
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
headers: options.headers,
|
|
174
|
+
body: options.body
|
|
175
|
+
}
|
|
176
|
+
);
|
|
286
177
|
setInput("");
|
|
287
178
|
},
|
|
288
|
-
[input, generateId,
|
|
179
|
+
[input, generateId, append, messages]
|
|
289
180
|
);
|
|
290
181
|
const handleInputChange = (e) => {
|
|
291
182
|
setInput(e.target.value);
|
|
292
183
|
};
|
|
293
|
-
const addToolResult = (0, import_react2.useCallback)(
|
|
294
|
-
({ toolCallId, result }) => {
|
|
295
|
-
const currentMessages = messagesRef.current;
|
|
296
|
-
(0, import_ai2.updateToolCallResult)({
|
|
297
|
-
messages: currentMessages,
|
|
298
|
-
toolCallId,
|
|
299
|
-
toolResult: result
|
|
300
|
-
});
|
|
301
|
-
mutate(
|
|
302
|
-
[
|
|
303
|
-
...currentMessages.slice(0, currentMessages.length - 1),
|
|
304
|
-
{
|
|
305
|
-
...currentMessages[currentMessages.length - 1],
|
|
306
|
-
// @ts-ignore
|
|
307
|
-
// update the revisionId to trigger a re-render
|
|
308
|
-
revisionId: generateId()
|
|
309
|
-
}
|
|
310
|
-
],
|
|
311
|
-
false
|
|
312
|
-
);
|
|
313
|
-
if (status === "submitted" || status === "streaming") {
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
317
|
-
if ((0, import_ai2.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
318
|
-
triggerRequest({ messages: currentMessages });
|
|
319
|
-
}
|
|
320
|
-
},
|
|
321
|
-
[mutate, status, triggerRequest, generateId]
|
|
322
|
-
);
|
|
323
184
|
return {
|
|
324
|
-
messages
|
|
185
|
+
messages,
|
|
325
186
|
id: chatId,
|
|
326
187
|
setMessages,
|
|
327
188
|
error,
|
|
@@ -339,9 +200,17 @@ function useChat({
|
|
|
339
200
|
}
|
|
340
201
|
|
|
341
202
|
// src/use-completion.ts
|
|
342
|
-
var
|
|
343
|
-
var
|
|
344
|
-
var
|
|
203
|
+
var import_ai2 = require("ai");
|
|
204
|
+
var import_react2 = require("react");
|
|
205
|
+
var import_swr = __toESM(require("swr"));
|
|
206
|
+
|
|
207
|
+
// src/throttle.ts
|
|
208
|
+
var import_throttleit = __toESM(require("throttleit"));
|
|
209
|
+
function throttle(fn, waitMs) {
|
|
210
|
+
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/use-completion.ts
|
|
345
214
|
function useCompletion({
|
|
346
215
|
api = "/api/completion",
|
|
347
216
|
id,
|
|
@@ -356,32 +225,32 @@ function useCompletion({
|
|
|
356
225
|
onError,
|
|
357
226
|
experimental_throttle: throttleWaitMs
|
|
358
227
|
} = {}) {
|
|
359
|
-
const hookId = (0,
|
|
228
|
+
const hookId = (0, import_react2.useId)();
|
|
360
229
|
const completionId = id || hookId;
|
|
361
|
-
const { data, mutate } = (0,
|
|
230
|
+
const { data, mutate } = (0, import_swr.default)([api, completionId], null, {
|
|
362
231
|
fallbackData: initialCompletion
|
|
363
232
|
});
|
|
364
|
-
const { data: isLoading = false, mutate: mutateLoading } = (0,
|
|
233
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
|
365
234
|
[completionId, "loading"],
|
|
366
235
|
null
|
|
367
236
|
);
|
|
368
|
-
const [error, setError] = (0,
|
|
237
|
+
const [error, setError] = (0, import_react2.useState)(void 0);
|
|
369
238
|
const completion = data;
|
|
370
|
-
const [abortController, setAbortController] = (0,
|
|
371
|
-
const extraMetadataRef = (0,
|
|
239
|
+
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
|
240
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
372
241
|
credentials,
|
|
373
242
|
headers,
|
|
374
243
|
body
|
|
375
244
|
});
|
|
376
|
-
(0,
|
|
245
|
+
(0, import_react2.useEffect)(() => {
|
|
377
246
|
extraMetadataRef.current = {
|
|
378
247
|
credentials,
|
|
379
248
|
headers,
|
|
380
249
|
body
|
|
381
250
|
};
|
|
382
251
|
}, [credentials, headers, body]);
|
|
383
|
-
const triggerRequest = (0,
|
|
384
|
-
async (prompt, options) => (0,
|
|
252
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
253
|
+
async (prompt, options) => (0, import_ai2.callCompletionApi)({
|
|
385
254
|
api,
|
|
386
255
|
prompt,
|
|
387
256
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -417,26 +286,26 @@ function useCompletion({
|
|
|
417
286
|
throttleWaitMs
|
|
418
287
|
]
|
|
419
288
|
);
|
|
420
|
-
const stop = (0,
|
|
289
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
421
290
|
if (abortController) {
|
|
422
291
|
abortController.abort();
|
|
423
292
|
setAbortController(null);
|
|
424
293
|
}
|
|
425
294
|
}, [abortController]);
|
|
426
|
-
const setCompletion = (0,
|
|
295
|
+
const setCompletion = (0, import_react2.useCallback)(
|
|
427
296
|
(completion2) => {
|
|
428
297
|
mutate(completion2, false);
|
|
429
298
|
},
|
|
430
299
|
[mutate]
|
|
431
300
|
);
|
|
432
|
-
const complete = (0,
|
|
301
|
+
const complete = (0, import_react2.useCallback)(
|
|
433
302
|
async (prompt, options) => {
|
|
434
303
|
return triggerRequest(prompt, options);
|
|
435
304
|
},
|
|
436
305
|
[triggerRequest]
|
|
437
306
|
);
|
|
438
|
-
const [input, setInput] = (0,
|
|
439
|
-
const handleSubmit = (0,
|
|
307
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
308
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
440
309
|
(event) => {
|
|
441
310
|
var _a;
|
|
442
311
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -444,7 +313,7 @@ function useCompletion({
|
|
|
444
313
|
},
|
|
445
314
|
[input, complete]
|
|
446
315
|
);
|
|
447
|
-
const handleInputChange = (0,
|
|
316
|
+
const handleInputChange = (0, import_react2.useCallback)(
|
|
448
317
|
(e) => {
|
|
449
318
|
setInput(e.target.value);
|
|
450
319
|
},
|
|
@@ -466,9 +335,9 @@ function useCompletion({
|
|
|
466
335
|
|
|
467
336
|
// src/use-object.ts
|
|
468
337
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
469
|
-
var
|
|
470
|
-
var
|
|
471
|
-
var
|
|
338
|
+
var import_ai3 = require("ai");
|
|
339
|
+
var import_react3 = require("react");
|
|
340
|
+
var import_swr2 = __toESM(require("swr"));
|
|
472
341
|
var getOriginalFetch = () => fetch;
|
|
473
342
|
function useObject({
|
|
474
343
|
api,
|
|
@@ -482,17 +351,17 @@ function useObject({
|
|
|
482
351
|
headers,
|
|
483
352
|
credentials
|
|
484
353
|
}) {
|
|
485
|
-
const hookId = (0,
|
|
354
|
+
const hookId = (0, import_react3.useId)();
|
|
486
355
|
const completionId = id != null ? id : hookId;
|
|
487
|
-
const { data, mutate } = (0,
|
|
356
|
+
const { data, mutate } = (0, import_swr2.default)(
|
|
488
357
|
[api, completionId],
|
|
489
358
|
null,
|
|
490
359
|
{ fallbackData: initialValue }
|
|
491
360
|
);
|
|
492
|
-
const [error, setError] = (0,
|
|
493
|
-
const [isLoading, setIsLoading] = (0,
|
|
494
|
-
const abortControllerRef = (0,
|
|
495
|
-
const stop = (0,
|
|
361
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
362
|
+
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
363
|
+
const abortControllerRef = (0, import_react3.useRef)(null);
|
|
364
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
496
365
|
var _a;
|
|
497
366
|
try {
|
|
498
367
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -535,9 +404,9 @@ function useObject({
|
|
|
535
404
|
new WritableStream({
|
|
536
405
|
async write(chunk) {
|
|
537
406
|
accumulatedText += chunk;
|
|
538
|
-
const { value } = await (0,
|
|
407
|
+
const { value } = await (0, import_ai3.parsePartialJson)(accumulatedText);
|
|
539
408
|
const currentObject = value;
|
|
540
|
-
if (!(0,
|
|
409
|
+
if (!(0, import_ai3.isDeepEqualData)(latestObject, currentObject)) {
|
|
541
410
|
latestObject = currentObject;
|
|
542
411
|
mutate(currentObject);
|
|
543
412
|
}
|
|
@@ -548,7 +417,7 @@ function useObject({
|
|
|
548
417
|
if (onFinish != null) {
|
|
549
418
|
const validationResult = await (0, import_provider_utils.safeValidateTypes)({
|
|
550
419
|
value: latestObject,
|
|
551
|
-
schema: (0,
|
|
420
|
+
schema: (0, import_ai3.asSchema)(schema)
|
|
552
421
|
});
|
|
553
422
|
onFinish(
|
|
554
423
|
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|