@ai-sdk/react 0.0.2 → 0.0.4
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/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-clean.log +1 -1
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +83 -44
- package/dist/index.d.ts +83 -44
- package/dist/index.js +247 -182
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +240 -173
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -4
- package/src/index.ts +2 -1
- package/src/use-assistant.ui.test.tsx +1 -1
- package/src/use-object.ts +123 -0
- package/src/use-object.ui.test.tsx +79 -0
package/dist/index.js
CHANGED
|
@@ -31,15 +31,174 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
experimental_useAssistant: () => experimental_useAssistant,
|
|
34
|
+
experimental_useObject: () => experimental_useObject,
|
|
34
35
|
useAssistant: () => useAssistant,
|
|
35
36
|
useChat: () => useChat,
|
|
36
37
|
useCompletion: () => useCompletion
|
|
37
38
|
});
|
|
38
39
|
module.exports = __toCommonJS(src_exports);
|
|
39
40
|
|
|
40
|
-
// src/use-
|
|
41
|
+
// src/use-assistant.ts
|
|
42
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
41
43
|
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
42
44
|
var import_react = require("react");
|
|
45
|
+
function useAssistant({
|
|
46
|
+
api,
|
|
47
|
+
threadId: threadIdParam,
|
|
48
|
+
credentials,
|
|
49
|
+
headers,
|
|
50
|
+
body,
|
|
51
|
+
onError
|
|
52
|
+
}) {
|
|
53
|
+
const [messages, setMessages] = (0, import_react.useState)([]);
|
|
54
|
+
const [input, setInput] = (0, import_react.useState)("");
|
|
55
|
+
const [threadId, setThreadId] = (0, import_react.useState)(void 0);
|
|
56
|
+
const [status, setStatus] = (0, import_react.useState)("awaiting_message");
|
|
57
|
+
const [error, setError] = (0, import_react.useState)(void 0);
|
|
58
|
+
const handleInputChange = (event) => {
|
|
59
|
+
setInput(event.target.value);
|
|
60
|
+
};
|
|
61
|
+
const abortControllerRef = (0, import_react.useRef)(null);
|
|
62
|
+
const stop = (0, import_react.useCallback)(() => {
|
|
63
|
+
if (abortControllerRef.current) {
|
|
64
|
+
abortControllerRef.current.abort();
|
|
65
|
+
abortControllerRef.current = null;
|
|
66
|
+
}
|
|
67
|
+
}, []);
|
|
68
|
+
const append = async (message, requestOptions) => {
|
|
69
|
+
var _a;
|
|
70
|
+
setStatus("in_progress");
|
|
71
|
+
setMessages((messages2) => {
|
|
72
|
+
var _a2;
|
|
73
|
+
return [
|
|
74
|
+
...messages2,
|
|
75
|
+
{
|
|
76
|
+
...message,
|
|
77
|
+
id: (_a2 = message.id) != null ? _a2 : (0, import_ui_utils.generateId)()
|
|
78
|
+
}
|
|
79
|
+
];
|
|
80
|
+
});
|
|
81
|
+
setInput("");
|
|
82
|
+
const abortController = new AbortController();
|
|
83
|
+
try {
|
|
84
|
+
abortControllerRef.current = abortController;
|
|
85
|
+
const result = await fetch(api, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
credentials,
|
|
88
|
+
signal: abortController.signal,
|
|
89
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
90
|
+
body: JSON.stringify({
|
|
91
|
+
...body,
|
|
92
|
+
// always use user-provided threadId when available:
|
|
93
|
+
threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
|
|
94
|
+
message: message.content,
|
|
95
|
+
// optional request data:
|
|
96
|
+
data: requestOptions == null ? void 0 : requestOptions.data
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
if (result.body == null) {
|
|
100
|
+
throw new Error("The response body is empty.");
|
|
101
|
+
}
|
|
102
|
+
for await (const { type, value } of (0, import_ui_utils.readDataStream)(
|
|
103
|
+
result.body.getReader()
|
|
104
|
+
)) {
|
|
105
|
+
switch (type) {
|
|
106
|
+
case "assistant_message": {
|
|
107
|
+
setMessages((messages2) => [
|
|
108
|
+
...messages2,
|
|
109
|
+
{
|
|
110
|
+
id: value.id,
|
|
111
|
+
role: value.role,
|
|
112
|
+
content: value.content[0].text.value
|
|
113
|
+
}
|
|
114
|
+
]);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "text": {
|
|
118
|
+
setMessages((messages2) => {
|
|
119
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
120
|
+
return [
|
|
121
|
+
...messages2.slice(0, messages2.length - 1),
|
|
122
|
+
{
|
|
123
|
+
id: lastMessage.id,
|
|
124
|
+
role: lastMessage.role,
|
|
125
|
+
content: lastMessage.content + value
|
|
126
|
+
}
|
|
127
|
+
];
|
|
128
|
+
});
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case "data_message": {
|
|
132
|
+
setMessages((messages2) => {
|
|
133
|
+
var _a2;
|
|
134
|
+
return [
|
|
135
|
+
...messages2,
|
|
136
|
+
{
|
|
137
|
+
id: (_a2 = value.id) != null ? _a2 : (0, import_ui_utils.generateId)(),
|
|
138
|
+
role: "data",
|
|
139
|
+
content: "",
|
|
140
|
+
data: value.data
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case "assistant_control_data": {
|
|
147
|
+
setThreadId(value.threadId);
|
|
148
|
+
setMessages((messages2) => {
|
|
149
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
150
|
+
lastMessage.id = value.messageId;
|
|
151
|
+
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
152
|
+
});
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case "error": {
|
|
156
|
+
setError(new Error(value));
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} catch (error2) {
|
|
162
|
+
if ((0, import_provider_utils.isAbortError)(error2) && abortController.signal.aborted) {
|
|
163
|
+
abortControllerRef.current = null;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (onError && error2 instanceof Error) {
|
|
167
|
+
onError(error2);
|
|
168
|
+
}
|
|
169
|
+
setError(error2);
|
|
170
|
+
} finally {
|
|
171
|
+
abortControllerRef.current = null;
|
|
172
|
+
setStatus("awaiting_message");
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const submitMessage = async (event, requestOptions) => {
|
|
176
|
+
var _a;
|
|
177
|
+
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
178
|
+
if (input === "") {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
append({ role: "user", content: input }, requestOptions);
|
|
182
|
+
};
|
|
183
|
+
return {
|
|
184
|
+
append,
|
|
185
|
+
messages,
|
|
186
|
+
setMessages,
|
|
187
|
+
threadId,
|
|
188
|
+
input,
|
|
189
|
+
setInput,
|
|
190
|
+
handleInputChange,
|
|
191
|
+
submitMessage,
|
|
192
|
+
status,
|
|
193
|
+
error,
|
|
194
|
+
stop
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
var experimental_useAssistant = useAssistant;
|
|
198
|
+
|
|
199
|
+
// src/use-chat.ts
|
|
200
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
201
|
+
var import_react2 = require("react");
|
|
43
202
|
var import_swr = __toESM(require("swr"));
|
|
44
203
|
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, onToolCall, sendExtraMessageFields) => {
|
|
45
204
|
var _a, _b;
|
|
@@ -69,7 +228,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
|
69
228
|
...tool_calls !== void 0 && { tool_calls }
|
|
70
229
|
})
|
|
71
230
|
);
|
|
72
|
-
return await (0,
|
|
231
|
+
return await (0, import_ui_utils2.callChatApi)({
|
|
73
232
|
api,
|
|
74
233
|
messages: constructedMessagesPayload,
|
|
75
234
|
body: {
|
|
@@ -128,12 +287,12 @@ function useChat({
|
|
|
128
287
|
credentials,
|
|
129
288
|
headers,
|
|
130
289
|
body,
|
|
131
|
-
generateId: generateId2 =
|
|
290
|
+
generateId: generateId2 = import_ui_utils2.generateId
|
|
132
291
|
} = {}) {
|
|
133
|
-
const hookId = (0,
|
|
292
|
+
const hookId = (0, import_react2.useId)();
|
|
134
293
|
const idKey = id != null ? id : hookId;
|
|
135
294
|
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
|
136
|
-
const [initialMessagesFallback] = (0,
|
|
295
|
+
const [initialMessagesFallback] = (0, import_react2.useState)([]);
|
|
137
296
|
const { data: messages, mutate } = (0, import_swr.default)(
|
|
138
297
|
[chatKey, "messages"],
|
|
139
298
|
null,
|
|
@@ -145,31 +304,31 @@ function useChat({
|
|
|
145
304
|
);
|
|
146
305
|
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
|
147
306
|
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
|
148
|
-
const messagesRef = (0,
|
|
149
|
-
(0,
|
|
307
|
+
const messagesRef = (0, import_react2.useRef)(messages || []);
|
|
308
|
+
(0, import_react2.useEffect)(() => {
|
|
150
309
|
messagesRef.current = messages || [];
|
|
151
310
|
}, [messages]);
|
|
152
|
-
const abortControllerRef = (0,
|
|
153
|
-
const extraMetadataRef = (0,
|
|
311
|
+
const abortControllerRef = (0, import_react2.useRef)(null);
|
|
312
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
154
313
|
credentials,
|
|
155
314
|
headers,
|
|
156
315
|
body
|
|
157
316
|
});
|
|
158
|
-
(0,
|
|
317
|
+
(0, import_react2.useEffect)(() => {
|
|
159
318
|
extraMetadataRef.current = {
|
|
160
319
|
credentials,
|
|
161
320
|
headers,
|
|
162
321
|
body
|
|
163
322
|
};
|
|
164
323
|
}, [credentials, headers, body]);
|
|
165
|
-
const triggerRequest = (0,
|
|
324
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
166
325
|
async (chatRequest) => {
|
|
167
326
|
try {
|
|
168
327
|
mutateLoading(true);
|
|
169
328
|
setError(void 0);
|
|
170
329
|
const abortController = new AbortController();
|
|
171
330
|
abortControllerRef.current = abortController;
|
|
172
|
-
await (0,
|
|
331
|
+
await (0, import_ui_utils2.processChatStream)({
|
|
173
332
|
getStreamedResponse: () => getStreamedResponse(
|
|
174
333
|
api,
|
|
175
334
|
chatRequest,
|
|
@@ -240,7 +399,7 @@ function useChat({
|
|
|
240
399
|
generateId2
|
|
241
400
|
]
|
|
242
401
|
);
|
|
243
|
-
const append = (0,
|
|
402
|
+
const append = (0, import_react2.useCallback)(
|
|
244
403
|
async (message, {
|
|
245
404
|
options,
|
|
246
405
|
functions,
|
|
@@ -265,7 +424,7 @@ function useChat({
|
|
|
265
424
|
},
|
|
266
425
|
[triggerRequest, generateId2]
|
|
267
426
|
);
|
|
268
|
-
const reload = (0,
|
|
427
|
+
const reload = (0, import_react2.useCallback)(
|
|
269
428
|
async ({
|
|
270
429
|
options,
|
|
271
430
|
functions,
|
|
@@ -299,21 +458,21 @@ function useChat({
|
|
|
299
458
|
},
|
|
300
459
|
[triggerRequest]
|
|
301
460
|
);
|
|
302
|
-
const stop = (0,
|
|
461
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
303
462
|
if (abortControllerRef.current) {
|
|
304
463
|
abortControllerRef.current.abort();
|
|
305
464
|
abortControllerRef.current = null;
|
|
306
465
|
}
|
|
307
466
|
}, []);
|
|
308
|
-
const setMessages = (0,
|
|
467
|
+
const setMessages = (0, import_react2.useCallback)(
|
|
309
468
|
(messages2) => {
|
|
310
469
|
mutate(messages2, false);
|
|
311
470
|
messagesRef.current = messages2;
|
|
312
471
|
},
|
|
313
472
|
[mutate]
|
|
314
473
|
);
|
|
315
|
-
const [input, setInput] = (0,
|
|
316
|
-
const handleSubmit = (0,
|
|
474
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
475
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
317
476
|
(e, options = {}, metadata) => {
|
|
318
477
|
if (metadata) {
|
|
319
478
|
extraMetadataRef.current = {
|
|
@@ -393,8 +552,8 @@ function countTrailingAssistantMessages(messages) {
|
|
|
393
552
|
}
|
|
394
553
|
|
|
395
554
|
// src/use-completion.ts
|
|
396
|
-
var
|
|
397
|
-
var
|
|
555
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
556
|
+
var import_react3 = require("react");
|
|
398
557
|
var import_swr2 = __toESM(require("swr"));
|
|
399
558
|
function useCompletion({
|
|
400
559
|
api = "/api/completion",
|
|
@@ -409,7 +568,7 @@ function useCompletion({
|
|
|
409
568
|
onFinish,
|
|
410
569
|
onError
|
|
411
570
|
} = {}) {
|
|
412
|
-
const hookId = (0,
|
|
571
|
+
const hookId = (0, import_react3.useId)();
|
|
413
572
|
const completionId = id || hookId;
|
|
414
573
|
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
415
574
|
fallbackData: initialCompletion
|
|
@@ -419,23 +578,23 @@ function useCompletion({
|
|
|
419
578
|
null
|
|
420
579
|
);
|
|
421
580
|
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
|
422
|
-
const [error, setError] = (0,
|
|
581
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
423
582
|
const completion = data;
|
|
424
|
-
const [abortController, setAbortController] = (0,
|
|
425
|
-
const extraMetadataRef = (0,
|
|
583
|
+
const [abortController, setAbortController] = (0, import_react3.useState)(null);
|
|
584
|
+
const extraMetadataRef = (0, import_react3.useRef)({
|
|
426
585
|
credentials,
|
|
427
586
|
headers,
|
|
428
587
|
body
|
|
429
588
|
});
|
|
430
|
-
(0,
|
|
589
|
+
(0, import_react3.useEffect)(() => {
|
|
431
590
|
extraMetadataRef.current = {
|
|
432
591
|
credentials,
|
|
433
592
|
headers,
|
|
434
593
|
body
|
|
435
594
|
};
|
|
436
595
|
}, [credentials, headers, body]);
|
|
437
|
-
const triggerRequest = (0,
|
|
438
|
-
async (prompt, options) => (0,
|
|
596
|
+
const triggerRequest = (0, import_react3.useCallback)(
|
|
597
|
+
async (prompt, options) => (0, import_ui_utils3.callCompletionApi)({
|
|
439
598
|
api,
|
|
440
599
|
prompt,
|
|
441
600
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -471,26 +630,26 @@ function useCompletion({
|
|
|
471
630
|
mutateStreamData
|
|
472
631
|
]
|
|
473
632
|
);
|
|
474
|
-
const stop = (0,
|
|
633
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
475
634
|
if (abortController) {
|
|
476
635
|
abortController.abort();
|
|
477
636
|
setAbortController(null);
|
|
478
637
|
}
|
|
479
638
|
}, [abortController]);
|
|
480
|
-
const setCompletion = (0,
|
|
639
|
+
const setCompletion = (0, import_react3.useCallback)(
|
|
481
640
|
(completion2) => {
|
|
482
641
|
mutate(completion2, false);
|
|
483
642
|
},
|
|
484
643
|
[mutate]
|
|
485
644
|
);
|
|
486
|
-
const complete = (0,
|
|
645
|
+
const complete = (0, import_react3.useCallback)(
|
|
487
646
|
async (prompt, options) => {
|
|
488
647
|
return triggerRequest(prompt, options);
|
|
489
648
|
},
|
|
490
649
|
[triggerRequest]
|
|
491
650
|
);
|
|
492
|
-
const [input, setInput] = (0,
|
|
493
|
-
const handleSubmit = (0,
|
|
651
|
+
const [input, setInput] = (0, import_react3.useState)(initialInput);
|
|
652
|
+
const handleSubmit = (0, import_react3.useCallback)(
|
|
494
653
|
(e) => {
|
|
495
654
|
e.preventDefault();
|
|
496
655
|
if (!input)
|
|
@@ -517,166 +676,72 @@ function useCompletion({
|
|
|
517
676
|
};
|
|
518
677
|
}
|
|
519
678
|
|
|
520
|
-
// src/use-
|
|
521
|
-
var
|
|
522
|
-
var
|
|
523
|
-
var
|
|
524
|
-
function
|
|
679
|
+
// src/use-object.ts
|
|
680
|
+
var import_ui_utils4 = require("@ai-sdk/ui-utils");
|
|
681
|
+
var import_react4 = require("react");
|
|
682
|
+
var import_swr3 = __toESM(require("swr"));
|
|
683
|
+
function useObject({
|
|
525
684
|
api,
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
onError
|
|
685
|
+
id,
|
|
686
|
+
schema,
|
|
687
|
+
// required, in the future we will use it for validation
|
|
688
|
+
initialValue
|
|
531
689
|
}) {
|
|
532
|
-
const
|
|
533
|
-
const
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
...messages2,
|
|
554
|
-
{
|
|
555
|
-
...message,
|
|
556
|
-
id: (_a2 = message.id) != null ? _a2 : (0, import_ui_utils3.generateId)()
|
|
690
|
+
const hookId = (0, import_react4.useId)();
|
|
691
|
+
const completionId = id != null ? id : hookId;
|
|
692
|
+
const { data, mutate } = (0, import_swr3.default)(
|
|
693
|
+
[api, completionId],
|
|
694
|
+
null,
|
|
695
|
+
{ fallbackData: initialValue }
|
|
696
|
+
);
|
|
697
|
+
const [error, setError] = (0, import_react4.useState)(void 0);
|
|
698
|
+
return {
|
|
699
|
+
async setInput(input) {
|
|
700
|
+
var _a;
|
|
701
|
+
try {
|
|
702
|
+
const response = await fetch(api, {
|
|
703
|
+
method: "POST",
|
|
704
|
+
headers: { "Content-Type": "application/json" },
|
|
705
|
+
body: JSON.stringify(input)
|
|
706
|
+
});
|
|
707
|
+
if (!response.ok) {
|
|
708
|
+
throw new Error(
|
|
709
|
+
(_a = await response.text()) != null ? _a : "Failed to fetch the response."
|
|
710
|
+
);
|
|
557
711
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
setInput("");
|
|
561
|
-
const abortController = new AbortController();
|
|
562
|
-
try {
|
|
563
|
-
abortControllerRef.current = abortController;
|
|
564
|
-
const result = await fetch(api, {
|
|
565
|
-
method: "POST",
|
|
566
|
-
credentials,
|
|
567
|
-
signal: abortController.signal,
|
|
568
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
569
|
-
body: JSON.stringify({
|
|
570
|
-
...body,
|
|
571
|
-
// always use user-provided threadId when available:
|
|
572
|
-
threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
|
|
573
|
-
message: message.content,
|
|
574
|
-
// optional request data:
|
|
575
|
-
data: requestOptions == null ? void 0 : requestOptions.data
|
|
576
|
-
})
|
|
577
|
-
});
|
|
578
|
-
if (result.body == null) {
|
|
579
|
-
throw new Error("The response body is empty.");
|
|
580
|
-
}
|
|
581
|
-
for await (const { type, value } of (0, import_ui_utils3.readDataStream)(
|
|
582
|
-
result.body.getReader()
|
|
583
|
-
)) {
|
|
584
|
-
switch (type) {
|
|
585
|
-
case "assistant_message": {
|
|
586
|
-
setMessages((messages2) => [
|
|
587
|
-
...messages2,
|
|
588
|
-
{
|
|
589
|
-
id: value.id,
|
|
590
|
-
role: value.role,
|
|
591
|
-
content: value.content[0].text.value
|
|
592
|
-
}
|
|
593
|
-
]);
|
|
594
|
-
break;
|
|
595
|
-
}
|
|
596
|
-
case "text": {
|
|
597
|
-
setMessages((messages2) => {
|
|
598
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
599
|
-
return [
|
|
600
|
-
...messages2.slice(0, messages2.length - 1),
|
|
601
|
-
{
|
|
602
|
-
id: lastMessage.id,
|
|
603
|
-
role: lastMessage.role,
|
|
604
|
-
content: lastMessage.content + value
|
|
605
|
-
}
|
|
606
|
-
];
|
|
607
|
-
});
|
|
608
|
-
break;
|
|
609
|
-
}
|
|
610
|
-
case "data_message": {
|
|
611
|
-
setMessages((messages2) => {
|
|
612
|
-
var _a2;
|
|
613
|
-
return [
|
|
614
|
-
...messages2,
|
|
615
|
-
{
|
|
616
|
-
id: (_a2 = value.id) != null ? _a2 : (0, import_ui_utils3.generateId)(),
|
|
617
|
-
role: "data",
|
|
618
|
-
content: "",
|
|
619
|
-
data: value.data
|
|
620
|
-
}
|
|
621
|
-
];
|
|
622
|
-
});
|
|
623
|
-
break;
|
|
624
|
-
}
|
|
625
|
-
case "assistant_control_data": {
|
|
626
|
-
setThreadId(value.threadId);
|
|
627
|
-
setMessages((messages2) => {
|
|
628
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
629
|
-
lastMessage.id = value.messageId;
|
|
630
|
-
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
631
|
-
});
|
|
632
|
-
break;
|
|
633
|
-
}
|
|
634
|
-
case "error": {
|
|
635
|
-
setError(new Error(value));
|
|
636
|
-
break;
|
|
637
|
-
}
|
|
712
|
+
if (response.body == null) {
|
|
713
|
+
throw new Error("The response body is empty.");
|
|
638
714
|
}
|
|
715
|
+
let accumulatedText = "";
|
|
716
|
+
let latestObject = void 0;
|
|
717
|
+
response.body.pipeThrough(new TextDecoderStream()).pipeTo(
|
|
718
|
+
new WritableStream({
|
|
719
|
+
write(chunk) {
|
|
720
|
+
accumulatedText += chunk;
|
|
721
|
+
const currentObject = (0, import_ui_utils4.parsePartialJson)(
|
|
722
|
+
accumulatedText
|
|
723
|
+
);
|
|
724
|
+
if (!(0, import_ui_utils4.isDeepEqualData)(latestObject, currentObject)) {
|
|
725
|
+
latestObject = currentObject;
|
|
726
|
+
mutate(currentObject);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
})
|
|
730
|
+
);
|
|
731
|
+
setError(void 0);
|
|
732
|
+
} catch (error2) {
|
|
733
|
+
setError(error2);
|
|
639
734
|
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
if (onError && error2 instanceof Error) {
|
|
646
|
-
onError(error2);
|
|
647
|
-
}
|
|
648
|
-
setError(error2);
|
|
649
|
-
} finally {
|
|
650
|
-
abortControllerRef.current = null;
|
|
651
|
-
setStatus("awaiting_message");
|
|
652
|
-
}
|
|
653
|
-
};
|
|
654
|
-
const submitMessage = async (event, requestOptions) => {
|
|
655
|
-
var _a;
|
|
656
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
657
|
-
if (input === "") {
|
|
658
|
-
return;
|
|
659
|
-
}
|
|
660
|
-
append({ role: "user", content: input }, requestOptions);
|
|
661
|
-
};
|
|
662
|
-
return {
|
|
663
|
-
append,
|
|
664
|
-
messages,
|
|
665
|
-
setMessages,
|
|
666
|
-
threadId,
|
|
667
|
-
input,
|
|
668
|
-
setInput,
|
|
669
|
-
handleInputChange,
|
|
670
|
-
submitMessage,
|
|
671
|
-
status,
|
|
672
|
-
error,
|
|
673
|
-
stop
|
|
735
|
+
},
|
|
736
|
+
object: data,
|
|
737
|
+
error
|
|
674
738
|
};
|
|
675
739
|
}
|
|
676
|
-
var
|
|
740
|
+
var experimental_useObject = useObject;
|
|
677
741
|
// Annotate the CommonJS export names for ESM import in node:
|
|
678
742
|
0 && (module.exports = {
|
|
679
743
|
experimental_useAssistant,
|
|
744
|
+
experimental_useObject,
|
|
680
745
|
useAssistant,
|
|
681
746
|
useChat,
|
|
682
747
|
useCompletion
|