ai 2.2.26 → 2.2.28
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/dist/index.d.ts +15 -6
- package/dist/index.js +106 -71
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +106 -71
- package/dist/index.mjs.map +1 -0
- package/package.json +22 -12
- package/prompts/dist/index.d.ts +5 -1
- package/prompts/dist/index.js +1 -0
- package/prompts/dist/index.js.map +1 -0
- package/prompts/dist/index.mjs +1 -0
- package/prompts/dist/index.mjs.map +1 -0
- package/react/dist/index.d.ts +12 -4
- package/react/dist/index.js +273 -196
- package/react/dist/index.js.map +1 -0
- package/react/dist/index.mjs +273 -196
- package/react/dist/index.mjs.map +1 -0
- package/react/dist/index.server.js +1 -0
- package/react/dist/index.server.js.map +1 -0
- package/react/dist/index.server.mjs +1 -0
- package/react/dist/index.server.mjs.map +1 -0
- package/solid/dist/index.d.ts +4 -1
- package/solid/dist/index.js +248 -152
- package/solid/dist/index.js.map +1 -0
- package/solid/dist/index.mjs +248 -152
- package/solid/dist/index.mjs.map +1 -0
- package/svelte/dist/index.d.ts +4 -1
- package/svelte/dist/index.js +244 -151
- package/svelte/dist/index.js.map +1 -0
- package/svelte/dist/index.mjs +244 -151
- package/svelte/dist/index.mjs.map +1 -0
- package/vue/dist/index.d.ts +4 -1
- package/vue/dist/index.js +246 -147
- package/vue/dist/index.js.map +1 -0
- package/vue/dist/index.mjs +246 -147
- package/vue/dist/index.mjs.map +1 -0
package/react/dist/index.d.ts
CHANGED
@@ -42,7 +42,7 @@ interface Message {
|
|
42
42
|
createdAt?: Date;
|
43
43
|
content: string;
|
44
44
|
ui?: string | JSX.Element | JSX.Element[] | null | undefined;
|
45
|
-
role: 'system' | 'user' | 'assistant' | 'function';
|
45
|
+
role: 'system' | 'user' | 'assistant' | 'function' | 'data';
|
46
46
|
/**
|
47
47
|
* If the message has a role of `function`, the `name` field is the name of the function.
|
48
48
|
* Otherwise, the name field should not be set.
|
@@ -54,6 +54,7 @@ interface Message {
|
|
54
54
|
* not be set.
|
55
55
|
*/
|
56
56
|
function_call?: string | FunctionCall;
|
57
|
+
data?: JSONValue;
|
57
58
|
}
|
58
59
|
type CreateMessage = Omit<Message, 'id'> & {
|
59
60
|
id?: Message['id'];
|
@@ -298,6 +299,7 @@ type StreamingReactResponseAction = (payload: {
|
|
298
299
|
}) => Promise<experimental_StreamingReactResponse>;
|
299
300
|
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
|
300
301
|
api?: string | StreamingReactResponseAction;
|
302
|
+
key?: string;
|
301
303
|
}): UseChatHelpers;
|
302
304
|
|
303
305
|
type UseCompletionHelpers = {
|
@@ -341,6 +343,8 @@ type UseCompletionHelpers = {
|
|
341
343
|
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
|
342
344
|
/** Whether the API request is in progress */
|
343
345
|
isLoading: boolean;
|
346
|
+
/** Additional data added on the server via StreamData */
|
347
|
+
data?: JSONValue[] | undefined;
|
344
348
|
};
|
345
349
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
346
350
|
|
@@ -363,12 +367,16 @@ type UseAssistantHelpers = {
|
|
363
367
|
/** Current error, if any */
|
364
368
|
error: undefined | unknown;
|
365
369
|
};
|
366
|
-
|
370
|
+
type UseAssistantOptions = {
|
367
371
|
api: string;
|
368
372
|
threadId?: string | undefined;
|
369
|
-
|
373
|
+
credentials?: RequestCredentials;
|
374
|
+
headers?: Record<string, string> | Headers;
|
375
|
+
body?: object;
|
376
|
+
};
|
377
|
+
declare function experimental_useAssistant({ api, threadId: threadIdParam, credentials, headers, body, }: UseAssistantOptions): UseAssistantHelpers;
|
370
378
|
|
371
|
-
export { AssistantStatus, CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useAssistant, useChat, useCompletion };
|
379
|
+
export { AssistantStatus, CreateMessage, Message, UseAssistantHelpers, UseAssistantOptions, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useAssistant, useChat, useCompletion };
|
372
380
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
373
381
|
|
374
382
|
type Props = {
|
package/react/dist/index.js
CHANGED
@@ -41,9 +41,6 @@ module.exports = __toCommonJS(react_exports);
|
|
41
41
|
var import_react = require("react");
|
42
42
|
var import_swr = __toESM(require("swr"));
|
43
43
|
|
44
|
-
// shared/utils.ts
|
45
|
-
var import_non_secure = require("nanoid/non-secure");
|
46
|
-
|
47
44
|
// shared/stream-parts.ts
|
48
45
|
var textStreamPart = {
|
49
46
|
code: "0",
|
@@ -90,7 +87,7 @@ var errorStreamPart = {
|
|
90
87
|
return { type: "error", value };
|
91
88
|
}
|
92
89
|
};
|
93
|
-
var
|
90
|
+
var assistantMessageStreamPart = {
|
94
91
|
code: "4",
|
95
92
|
name: "assistant_message",
|
96
93
|
parse: (value) => {
|
@@ -107,7 +104,7 @@ var assistantMessage = {
|
|
107
104
|
};
|
108
105
|
}
|
109
106
|
};
|
110
|
-
var
|
107
|
+
var assistantControlDataStreamPart = {
|
111
108
|
code: "5",
|
112
109
|
name: "assistant_control_data",
|
113
110
|
parse: (value) => {
|
@@ -125,29 +122,47 @@ var assistantControlData = {
|
|
125
122
|
};
|
126
123
|
}
|
127
124
|
};
|
125
|
+
var dataMessageStreamPart = {
|
126
|
+
code: "6",
|
127
|
+
name: "data_message",
|
128
|
+
parse: (value) => {
|
129
|
+
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
130
|
+
throw new Error(
|
131
|
+
'"data_message" parts expect an object with a "role" and "data" property.'
|
132
|
+
);
|
133
|
+
}
|
134
|
+
return {
|
135
|
+
type: "data_message",
|
136
|
+
value
|
137
|
+
};
|
138
|
+
}
|
139
|
+
};
|
128
140
|
var streamParts = [
|
129
141
|
textStreamPart,
|
130
142
|
functionCallStreamPart,
|
131
143
|
dataStreamPart,
|
132
144
|
errorStreamPart,
|
133
|
-
|
134
|
-
|
145
|
+
assistantMessageStreamPart,
|
146
|
+
assistantControlDataStreamPart,
|
147
|
+
dataMessageStreamPart
|
135
148
|
];
|
136
149
|
var streamPartsByCode = {
|
137
150
|
[textStreamPart.code]: textStreamPart,
|
138
151
|
[functionCallStreamPart.code]: functionCallStreamPart,
|
139
152
|
[dataStreamPart.code]: dataStreamPart,
|
140
153
|
[errorStreamPart.code]: errorStreamPart,
|
141
|
-
[
|
142
|
-
[
|
154
|
+
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
155
|
+
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
156
|
+
[dataMessageStreamPart.code]: dataMessageStreamPart
|
143
157
|
};
|
144
158
|
var StreamStringPrefixes = {
|
145
159
|
[textStreamPart.name]: textStreamPart.code,
|
146
160
|
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
147
161
|
[dataStreamPart.name]: dataStreamPart.code,
|
148
162
|
[errorStreamPart.name]: errorStreamPart.code,
|
149
|
-
[
|
150
|
-
[
|
163
|
+
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
164
|
+
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
165
|
+
[dataMessageStreamPart.name]: dataMessageStreamPart.code
|
151
166
|
};
|
152
167
|
var validCodes = streamParts.map((part) => part.code);
|
153
168
|
var parseStreamPart = (line) => {
|
@@ -165,7 +180,51 @@ var parseStreamPart = (line) => {
|
|
165
180
|
return streamPartsByCode[code].parse(jsonValue);
|
166
181
|
};
|
167
182
|
|
183
|
+
// shared/read-data-stream.ts
|
184
|
+
var NEWLINE = "\n".charCodeAt(0);
|
185
|
+
function concatChunks(chunks, totalLength) {
|
186
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
187
|
+
let offset = 0;
|
188
|
+
for (const chunk of chunks) {
|
189
|
+
concatenatedChunks.set(chunk, offset);
|
190
|
+
offset += chunk.length;
|
191
|
+
}
|
192
|
+
chunks.length = 0;
|
193
|
+
return concatenatedChunks;
|
194
|
+
}
|
195
|
+
async function* readDataStream(reader, {
|
196
|
+
isAborted
|
197
|
+
} = {}) {
|
198
|
+
const decoder = new TextDecoder();
|
199
|
+
const chunks = [];
|
200
|
+
let totalLength = 0;
|
201
|
+
while (true) {
|
202
|
+
const { value } = await reader.read();
|
203
|
+
if (value) {
|
204
|
+
chunks.push(value);
|
205
|
+
totalLength += value.length;
|
206
|
+
if (value[value.length - 1] !== NEWLINE) {
|
207
|
+
continue;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
if (chunks.length === 0) {
|
211
|
+
break;
|
212
|
+
}
|
213
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
214
|
+
totalLength = 0;
|
215
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
216
|
+
for (const streamPart of streamParts2) {
|
217
|
+
yield streamPart;
|
218
|
+
}
|
219
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
220
|
+
reader.cancel();
|
221
|
+
break;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
168
226
|
// shared/utils.ts
|
227
|
+
var import_non_secure = require("nanoid/non-secure");
|
169
228
|
var nanoid = (0, import_non_secure.customAlphabet)(
|
170
229
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
171
230
|
7
|
@@ -196,80 +255,47 @@ async function parseComplexResponse({
|
|
196
255
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
197
256
|
}) {
|
198
257
|
const createdAt = getCurrentDate();
|
199
|
-
const decode = createChunkDecoder(true);
|
200
258
|
const prefixMap = {
|
201
259
|
data: []
|
202
260
|
};
|
203
|
-
const
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
}
|
215
|
-
if (chunks.length === 0) {
|
216
|
-
break;
|
217
|
-
}
|
218
|
-
let concatenatedChunks = new Uint8Array(totalLength);
|
219
|
-
let offset = 0;
|
220
|
-
for (const chunk of chunks) {
|
221
|
-
concatenatedChunks.set(chunk, offset);
|
222
|
-
offset += chunk.length;
|
223
|
-
}
|
224
|
-
chunks.length = 0;
|
225
|
-
totalLength = 0;
|
226
|
-
const lines = decode(concatenatedChunks);
|
227
|
-
if (typeof lines === "string") {
|
228
|
-
throw new Error(
|
229
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
230
|
-
);
|
231
|
-
}
|
232
|
-
for (const { type, value: value2 } of lines) {
|
233
|
-
if (type === "text") {
|
234
|
-
if (prefixMap["text"]) {
|
235
|
-
prefixMap["text"] = {
|
236
|
-
...prefixMap["text"],
|
237
|
-
content: (prefixMap["text"].content || "") + value2
|
238
|
-
};
|
239
|
-
} else {
|
240
|
-
prefixMap["text"] = {
|
241
|
-
id: generateId(),
|
242
|
-
role: "assistant",
|
243
|
-
content: value2,
|
244
|
-
createdAt
|
245
|
-
};
|
246
|
-
}
|
247
|
-
}
|
248
|
-
let functionCallMessage = null;
|
249
|
-
if (type === "function_call") {
|
250
|
-
prefixMap["function_call"] = {
|
261
|
+
for await (const { type, value } of readDataStream(reader, {
|
262
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
263
|
+
})) {
|
264
|
+
if (type === "text") {
|
265
|
+
if (prefixMap["text"]) {
|
266
|
+
prefixMap["text"] = {
|
267
|
+
...prefixMap["text"],
|
268
|
+
content: (prefixMap["text"].content || "") + value
|
269
|
+
};
|
270
|
+
} else {
|
271
|
+
prefixMap["text"] = {
|
251
272
|
id: generateId(),
|
252
273
|
role: "assistant",
|
253
|
-
content:
|
254
|
-
function_call: value2.function_call,
|
255
|
-
name: value2.function_call.name,
|
274
|
+
content: value,
|
256
275
|
createdAt
|
257
276
|
};
|
258
|
-
functionCallMessage = prefixMap["function_call"];
|
259
|
-
}
|
260
|
-
if (type === "data") {
|
261
|
-
prefixMap["data"].push(...value2);
|
262
|
-
}
|
263
|
-
const responseMessage = prefixMap["text"];
|
264
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
265
|
-
Boolean
|
266
|
-
);
|
267
|
-
update(merged, [...prefixMap["data"]]);
|
268
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
269
|
-
reader.cancel();
|
270
|
-
break;
|
271
277
|
}
|
272
278
|
}
|
279
|
+
let functionCallMessage = null;
|
280
|
+
if (type === "function_call") {
|
281
|
+
prefixMap["function_call"] = {
|
282
|
+
id: generateId(),
|
283
|
+
role: "assistant",
|
284
|
+
content: "",
|
285
|
+
function_call: value.function_call,
|
286
|
+
name: value.function_call.name,
|
287
|
+
createdAt
|
288
|
+
};
|
289
|
+
functionCallMessage = prefixMap["function_call"];
|
290
|
+
}
|
291
|
+
if (type === "data") {
|
292
|
+
prefixMap["data"].push(...value);
|
293
|
+
}
|
294
|
+
const responseMessage = prefixMap["text"];
|
295
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
296
|
+
Boolean
|
297
|
+
);
|
298
|
+
update(merged, [...prefixMap["data"]]);
|
273
299
|
}
|
274
300
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
275
301
|
return {
|
@@ -280,8 +306,8 @@ async function parseComplexResponse({
|
|
280
306
|
};
|
281
307
|
}
|
282
308
|
|
283
|
-
// shared/call-api.ts
|
284
|
-
async function
|
309
|
+
// shared/call-chat-api.ts
|
310
|
+
async function callChatApi({
|
285
311
|
api,
|
286
312
|
messages,
|
287
313
|
body,
|
@@ -476,7 +502,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
476
502
|
}
|
477
503
|
return responseMessage;
|
478
504
|
}
|
479
|
-
return await
|
505
|
+
return await callChatApi({
|
480
506
|
api,
|
481
507
|
messages: constructedMessagesPayload,
|
482
508
|
body: {
|
@@ -527,16 +553,20 @@ function useChat({
|
|
527
553
|
generateId = nanoid
|
528
554
|
} = {}) {
|
529
555
|
const hookId = (0, import_react.useId)();
|
530
|
-
const
|
556
|
+
const idKey = id != null ? id : hookId;
|
557
|
+
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
531
558
|
const [initialMessagesFallback] = (0, import_react.useState)([]);
|
532
|
-
const { data: messages, mutate } = (0, import_swr.default)(
|
533
|
-
|
534
|
-
|
559
|
+
const { data: messages, mutate } = (0, import_swr.default)(
|
560
|
+
[chatKey, "messages"],
|
561
|
+
null,
|
562
|
+
{ fallbackData: initialMessages != null ? initialMessages : initialMessagesFallback }
|
563
|
+
);
|
535
564
|
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
536
|
-
[
|
565
|
+
[chatKey, "loading"],
|
537
566
|
null
|
538
567
|
);
|
539
|
-
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([
|
568
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
569
|
+
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
540
570
|
const messagesRef = (0, import_react.useRef)(messages || []);
|
541
571
|
(0, import_react.useEffect)(() => {
|
542
572
|
messagesRef.current = messages || [];
|
@@ -554,7 +584,6 @@ function useChat({
|
|
554
584
|
body
|
555
585
|
};
|
556
586
|
}, [credentials, headers, body]);
|
557
|
-
const [error, setError] = (0, import_react.useState)();
|
558
587
|
const triggerRequest = (0, import_react.useCallback)(
|
559
588
|
async (chatRequest) => {
|
560
589
|
try {
|
@@ -610,8 +639,8 @@ function useChat({
|
|
610
639
|
streamData,
|
611
640
|
sendExtraMessageFields,
|
612
641
|
experimental_onFunctionCall,
|
613
|
-
messagesRef
|
614
|
-
abortControllerRef
|
642
|
+
messagesRef,
|
643
|
+
abortControllerRef,
|
615
644
|
generateId
|
616
645
|
]
|
617
646
|
);
|
@@ -715,24 +744,110 @@ function useChat({
|
|
715
744
|
var import_react2 = require("react");
|
716
745
|
var import_swr2 = __toESM(require("swr"));
|
717
746
|
|
718
|
-
// shared/
|
719
|
-
async function
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
747
|
+
// shared/call-completion-api.ts
|
748
|
+
async function callCompletionApi({
|
749
|
+
api,
|
750
|
+
prompt,
|
751
|
+
credentials,
|
752
|
+
headers,
|
753
|
+
body,
|
754
|
+
setCompletion,
|
755
|
+
setLoading,
|
756
|
+
setError,
|
757
|
+
setAbortController,
|
758
|
+
onResponse,
|
759
|
+
onFinish,
|
760
|
+
onError,
|
761
|
+
onData
|
762
|
+
}) {
|
763
|
+
try {
|
764
|
+
setLoading(true);
|
765
|
+
setError(void 0);
|
766
|
+
const abortController = new AbortController();
|
767
|
+
setAbortController(abortController);
|
768
|
+
setCompletion("");
|
769
|
+
const res = await fetch(api, {
|
770
|
+
method: "POST",
|
771
|
+
body: JSON.stringify({
|
772
|
+
prompt,
|
773
|
+
...body
|
774
|
+
}),
|
775
|
+
credentials,
|
776
|
+
headers: {
|
777
|
+
"Content-Type": "application/json",
|
778
|
+
...headers
|
779
|
+
},
|
780
|
+
signal: abortController.signal
|
781
|
+
}).catch((err) => {
|
782
|
+
throw err;
|
783
|
+
});
|
784
|
+
if (onResponse) {
|
785
|
+
try {
|
786
|
+
await onResponse(res);
|
787
|
+
} catch (err) {
|
788
|
+
throw err;
|
727
789
|
}
|
728
|
-
break;
|
729
790
|
}
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
791
|
+
if (!res.ok) {
|
792
|
+
throw new Error(
|
793
|
+
await res.text() || "Failed to fetch the chat response."
|
794
|
+
);
|
795
|
+
}
|
796
|
+
if (!res.body) {
|
797
|
+
throw new Error("The response body is empty.");
|
735
798
|
}
|
799
|
+
let result = "";
|
800
|
+
const reader = res.body.getReader();
|
801
|
+
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
802
|
+
if (isComplexMode) {
|
803
|
+
for await (const { type, value } of readDataStream(reader, {
|
804
|
+
isAborted: () => abortController === null
|
805
|
+
})) {
|
806
|
+
switch (type) {
|
807
|
+
case "text": {
|
808
|
+
result += value;
|
809
|
+
setCompletion(result);
|
810
|
+
break;
|
811
|
+
}
|
812
|
+
case "data": {
|
813
|
+
onData == null ? void 0 : onData(value);
|
814
|
+
break;
|
815
|
+
}
|
816
|
+
}
|
817
|
+
}
|
818
|
+
} else {
|
819
|
+
const decoder = createChunkDecoder();
|
820
|
+
while (true) {
|
821
|
+
const { done, value } = await reader.read();
|
822
|
+
if (done) {
|
823
|
+
break;
|
824
|
+
}
|
825
|
+
result += decoder(value);
|
826
|
+
setCompletion(result);
|
827
|
+
if (abortController === null) {
|
828
|
+
reader.cancel();
|
829
|
+
break;
|
830
|
+
}
|
831
|
+
}
|
832
|
+
}
|
833
|
+
if (onFinish) {
|
834
|
+
onFinish(prompt, result);
|
835
|
+
}
|
836
|
+
setAbortController(null);
|
837
|
+
return result;
|
838
|
+
} catch (err) {
|
839
|
+
if (err.name === "AbortError") {
|
840
|
+
setAbortController(null);
|
841
|
+
return null;
|
842
|
+
}
|
843
|
+
if (err instanceof Error) {
|
844
|
+
if (onError) {
|
845
|
+
onError(err);
|
846
|
+
}
|
847
|
+
}
|
848
|
+
setError(err);
|
849
|
+
} finally {
|
850
|
+
setLoading(false);
|
736
851
|
}
|
737
852
|
}
|
738
853
|
|
@@ -758,6 +873,7 @@ function useCompletion({
|
|
758
873
|
[completionId, "loading"],
|
759
874
|
null
|
760
875
|
);
|
876
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
761
877
|
const [error, setError] = (0, import_react2.useState)(void 0);
|
762
878
|
const completion = data;
|
763
879
|
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
@@ -774,90 +890,26 @@ function useCompletion({
|
|
774
890
|
};
|
775
891
|
}, [credentials, headers, body]);
|
776
892
|
const triggerRequest = (0, import_react2.useCallback)(
|
777
|
-
async (prompt, options) => {
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
},
|
796
|
-
signal: abortController2.signal
|
797
|
-
}).catch((err) => {
|
798
|
-
throw err;
|
799
|
-
});
|
800
|
-
if (onResponse) {
|
801
|
-
try {
|
802
|
-
await onResponse(res);
|
803
|
-
} catch (err) {
|
804
|
-
throw err;
|
805
|
-
}
|
806
|
-
}
|
807
|
-
if (!res.ok) {
|
808
|
-
throw new Error(
|
809
|
-
await res.text() || "Failed to fetch the chat response."
|
810
|
-
);
|
811
|
-
}
|
812
|
-
if (!res.body) {
|
813
|
-
throw new Error("The response body is empty.");
|
814
|
-
}
|
815
|
-
let result = "";
|
816
|
-
const reader = res.body.getReader();
|
817
|
-
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
818
|
-
if (isComplexMode) {
|
819
|
-
await processMessageStream(reader, (message) => {
|
820
|
-
const { type, value } = parseStreamPart(message);
|
821
|
-
if (type === "text") {
|
822
|
-
result += value;
|
823
|
-
mutate(result, false);
|
824
|
-
}
|
825
|
-
});
|
826
|
-
} else {
|
827
|
-
const decoder = createChunkDecoder();
|
828
|
-
while (true) {
|
829
|
-
const { done, value } = await reader.read();
|
830
|
-
if (done) {
|
831
|
-
break;
|
832
|
-
}
|
833
|
-
result += decoder(value);
|
834
|
-
mutate(result, false);
|
835
|
-
if (abortController2 === null) {
|
836
|
-
reader.cancel();
|
837
|
-
break;
|
838
|
-
}
|
839
|
-
}
|
840
|
-
}
|
841
|
-
if (onFinish) {
|
842
|
-
onFinish(prompt, result);
|
843
|
-
}
|
844
|
-
setAbortController(null);
|
845
|
-
return result;
|
846
|
-
} catch (err) {
|
847
|
-
if (err.name === "AbortError") {
|
848
|
-
setAbortController(null);
|
849
|
-
return null;
|
850
|
-
}
|
851
|
-
if (err instanceof Error) {
|
852
|
-
if (onError) {
|
853
|
-
onError(err);
|
854
|
-
}
|
855
|
-
}
|
856
|
-
setError(err);
|
857
|
-
} finally {
|
858
|
-
mutateLoading(false);
|
893
|
+
async (prompt, options) => callCompletionApi({
|
894
|
+
api,
|
895
|
+
prompt,
|
896
|
+
credentials: extraMetadataRef.current.credentials,
|
897
|
+
headers: { ...extraMetadataRef.current.headers, ...options == null ? void 0 : options.headers },
|
898
|
+
body: {
|
899
|
+
...extraMetadataRef.current.body,
|
900
|
+
...options == null ? void 0 : options.body
|
901
|
+
},
|
902
|
+
setCompletion: (completion2) => mutate(completion2, false),
|
903
|
+
setLoading: mutateLoading,
|
904
|
+
setError,
|
905
|
+
setAbortController,
|
906
|
+
onResponse,
|
907
|
+
onFinish,
|
908
|
+
onError,
|
909
|
+
onData: (data2) => {
|
910
|
+
mutateStreamData([...streamData || [], ...data2 || []], false);
|
859
911
|
}
|
860
|
-
},
|
912
|
+
}),
|
861
913
|
[
|
862
914
|
mutate,
|
863
915
|
mutateLoading,
|
@@ -867,7 +919,9 @@ function useCompletion({
|
|
867
919
|
onResponse,
|
868
920
|
onFinish,
|
869
921
|
onError,
|
870
|
-
setError
|
922
|
+
setError,
|
923
|
+
streamData,
|
924
|
+
mutateStreamData
|
871
925
|
]
|
872
926
|
);
|
873
927
|
const stop = (0, import_react2.useCallback)(() => {
|
@@ -911,7 +965,8 @@ function useCompletion({
|
|
911
965
|
setInput,
|
912
966
|
handleInputChange,
|
913
967
|
handleSubmit,
|
914
|
-
isLoading
|
968
|
+
isLoading,
|
969
|
+
data: streamData
|
915
970
|
};
|
916
971
|
}
|
917
972
|
|
@@ -919,7 +974,10 @@ function useCompletion({
|
|
919
974
|
var import_react3 = require("react");
|
920
975
|
function experimental_useAssistant({
|
921
976
|
api,
|
922
|
-
threadId: threadIdParam
|
977
|
+
threadId: threadIdParam,
|
978
|
+
credentials,
|
979
|
+
headers,
|
980
|
+
body
|
923
981
|
}) {
|
924
982
|
const [messages, setMessages] = (0, import_react3.useState)([]);
|
925
983
|
const [input, setInput] = (0, import_react3.useState)("");
|
@@ -943,8 +1001,10 @@ function experimental_useAssistant({
|
|
943
1001
|
setInput("");
|
944
1002
|
const result = await fetch(api, {
|
945
1003
|
method: "POST",
|
946
|
-
|
1004
|
+
credentials,
|
1005
|
+
headers: { "Content-Type": "application/json", ...headers },
|
947
1006
|
body: JSON.stringify({
|
1007
|
+
...body,
|
948
1008
|
// always use user-provided threadId when available:
|
949
1009
|
threadId: (_b = threadIdParam != null ? threadIdParam : threadId) != null ? _b : null,
|
950
1010
|
message: input,
|
@@ -955,9 +1015,10 @@ function experimental_useAssistant({
|
|
955
1015
|
if (result.body == null) {
|
956
1016
|
throw new Error("The response body is empty.");
|
957
1017
|
}
|
958
|
-
|
959
|
-
|
960
|
-
|
1018
|
+
try {
|
1019
|
+
for await (const { type, value } of readDataStream(
|
1020
|
+
result.body.getReader()
|
1021
|
+
)) {
|
961
1022
|
switch (type) {
|
962
1023
|
case "assistant_message": {
|
963
1024
|
setMessages((messages2) => [
|
@@ -970,6 +1031,21 @@ function experimental_useAssistant({
|
|
970
1031
|
]);
|
971
1032
|
break;
|
972
1033
|
}
|
1034
|
+
case "data_message": {
|
1035
|
+
setMessages((messages2) => {
|
1036
|
+
var _a2;
|
1037
|
+
return [
|
1038
|
+
...messages2,
|
1039
|
+
{
|
1040
|
+
id: (_a2 = value.id) != null ? _a2 : "",
|
1041
|
+
role: "data",
|
1042
|
+
content: "",
|
1043
|
+
data: value.data
|
1044
|
+
}
|
1045
|
+
];
|
1046
|
+
});
|
1047
|
+
break;
|
1048
|
+
}
|
973
1049
|
case "assistant_control_data": {
|
974
1050
|
setThreadId(value.threadId);
|
975
1051
|
setMessages((messages2) => {
|
@@ -984,10 +1060,10 @@ function experimental_useAssistant({
|
|
984
1060
|
break;
|
985
1061
|
}
|
986
1062
|
}
|
987
|
-
} catch (error2) {
|
988
|
-
setError(error2);
|
989
1063
|
}
|
990
|
-
})
|
1064
|
+
} catch (error2) {
|
1065
|
+
setError(error2);
|
1066
|
+
}
|
991
1067
|
setStatus("awaiting_message");
|
992
1068
|
};
|
993
1069
|
return {
|
@@ -1006,3 +1082,4 @@ function experimental_useAssistant({
|
|
1006
1082
|
useChat,
|
1007
1083
|
useCompletion
|
1008
1084
|
});
|
1085
|
+
//# sourceMappingURL=index.js.map
|