ai 2.2.26 → 2.2.27
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.js +72 -62
- package/dist/index.mjs +72 -62
- package/package.json +10 -10
- package/react/dist/index.d.ts +9 -3
- package/react/dist/index.js +106 -100
- package/react/dist/index.mjs +106 -100
- package/solid/dist/index.d.ts +2 -0
- package/solid/dist/index.js +113 -80
- package/solid/dist/index.mjs +113 -80
- package/svelte/dist/index.d.ts +2 -0
- package/svelte/dist/index.js +109 -79
- package/svelte/dist/index.mjs +109 -79
- package/vue/dist/index.d.ts +2 -0
- package/vue/dist/index.js +111 -79
- package/vue/dist/index.mjs +111 -79
package/dist/index.js
CHANGED
@@ -863,21 +863,22 @@ async function ReplicateStream(res, cb, options) {
|
|
863
863
|
);
|
864
864
|
}
|
865
865
|
|
866
|
-
// shared/
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
}
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
866
|
+
// shared/read-data-stream.ts
|
867
|
+
var NEWLINE = "\n".charCodeAt(0);
|
868
|
+
function concatChunks(chunks, totalLength) {
|
869
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
870
|
+
let offset = 0;
|
871
|
+
for (const chunk of chunks) {
|
872
|
+
concatenatedChunks.set(chunk, offset);
|
873
|
+
offset += chunk.length;
|
874
|
+
}
|
875
|
+
chunks.length = 0;
|
876
|
+
return concatenatedChunks;
|
877
|
+
}
|
878
|
+
async function* readDataStream(reader, {
|
879
|
+
isAborted
|
880
|
+
} = {}) {
|
881
|
+
const decoder = new TextDecoder();
|
881
882
|
const chunks = [];
|
882
883
|
let totalLength = 0;
|
883
884
|
while (true) {
|
@@ -892,61 +893,70 @@ async function parseComplexResponse({
|
|
892
893
|
if (chunks.length === 0) {
|
893
894
|
break;
|
894
895
|
}
|
895
|
-
|
896
|
-
let offset = 0;
|
897
|
-
for (const chunk of chunks) {
|
898
|
-
concatenatedChunks.set(chunk, offset);
|
899
|
-
offset += chunk.length;
|
900
|
-
}
|
901
|
-
chunks.length = 0;
|
896
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
902
897
|
totalLength = 0;
|
903
|
-
const
|
904
|
-
|
905
|
-
|
906
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
907
|
-
);
|
898
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
899
|
+
for (const streamPart of streamParts2) {
|
900
|
+
yield streamPart;
|
908
901
|
}
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
902
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
903
|
+
reader.cancel();
|
904
|
+
break;
|
905
|
+
}
|
906
|
+
}
|
907
|
+
}
|
908
|
+
|
909
|
+
// shared/parse-complex-response.ts
|
910
|
+
async function parseComplexResponse({
|
911
|
+
reader,
|
912
|
+
abortControllerRef,
|
913
|
+
update,
|
914
|
+
onFinish,
|
915
|
+
generateId = nanoid,
|
916
|
+
getCurrentDate = () => /* @__PURE__ */ new Date()
|
917
|
+
}) {
|
918
|
+
const createdAt = getCurrentDate();
|
919
|
+
const prefixMap = {
|
920
|
+
data: []
|
921
|
+
};
|
922
|
+
for await (const { type, value } of readDataStream(reader, {
|
923
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
924
|
+
})) {
|
925
|
+
if (type === "text") {
|
926
|
+
if (prefixMap["text"]) {
|
927
|
+
prefixMap["text"] = {
|
928
|
+
...prefixMap["text"],
|
929
|
+
content: (prefixMap["text"].content || "") + value
|
930
|
+
};
|
931
|
+
} else {
|
932
|
+
prefixMap["text"] = {
|
928
933
|
id: generateId(),
|
929
934
|
role: "assistant",
|
930
|
-
content:
|
931
|
-
function_call: value2.function_call,
|
932
|
-
name: value2.function_call.name,
|
935
|
+
content: value,
|
933
936
|
createdAt
|
934
937
|
};
|
935
|
-
functionCallMessage = prefixMap["function_call"];
|
936
|
-
}
|
937
|
-
if (type === "data") {
|
938
|
-
prefixMap["data"].push(...value2);
|
939
|
-
}
|
940
|
-
const responseMessage = prefixMap["text"];
|
941
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
942
|
-
Boolean
|
943
|
-
);
|
944
|
-
update(merged, [...prefixMap["data"]]);
|
945
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
946
|
-
reader.cancel();
|
947
|
-
break;
|
948
938
|
}
|
949
939
|
}
|
940
|
+
let functionCallMessage = null;
|
941
|
+
if (type === "function_call") {
|
942
|
+
prefixMap["function_call"] = {
|
943
|
+
id: generateId(),
|
944
|
+
role: "assistant",
|
945
|
+
content: "",
|
946
|
+
function_call: value.function_call,
|
947
|
+
name: value.function_call.name,
|
948
|
+
createdAt
|
949
|
+
};
|
950
|
+
functionCallMessage = prefixMap["function_call"];
|
951
|
+
}
|
952
|
+
if (type === "data") {
|
953
|
+
prefixMap["data"].push(...value);
|
954
|
+
}
|
955
|
+
const responseMessage = prefixMap["text"];
|
956
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
957
|
+
Boolean
|
958
|
+
);
|
959
|
+
update(merged, [...prefixMap["data"]]);
|
950
960
|
}
|
951
961
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
952
962
|
return {
|
package/dist/index.mjs
CHANGED
@@ -815,21 +815,22 @@ async function ReplicateStream(res, cb, options) {
|
|
815
815
|
);
|
816
816
|
}
|
817
817
|
|
818
|
-
// shared/
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
}
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
818
|
+
// shared/read-data-stream.ts
|
819
|
+
var NEWLINE = "\n".charCodeAt(0);
|
820
|
+
function concatChunks(chunks, totalLength) {
|
821
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
822
|
+
let offset = 0;
|
823
|
+
for (const chunk of chunks) {
|
824
|
+
concatenatedChunks.set(chunk, offset);
|
825
|
+
offset += chunk.length;
|
826
|
+
}
|
827
|
+
chunks.length = 0;
|
828
|
+
return concatenatedChunks;
|
829
|
+
}
|
830
|
+
async function* readDataStream(reader, {
|
831
|
+
isAborted
|
832
|
+
} = {}) {
|
833
|
+
const decoder = new TextDecoder();
|
833
834
|
const chunks = [];
|
834
835
|
let totalLength = 0;
|
835
836
|
while (true) {
|
@@ -844,61 +845,70 @@ async function parseComplexResponse({
|
|
844
845
|
if (chunks.length === 0) {
|
845
846
|
break;
|
846
847
|
}
|
847
|
-
|
848
|
-
let offset = 0;
|
849
|
-
for (const chunk of chunks) {
|
850
|
-
concatenatedChunks.set(chunk, offset);
|
851
|
-
offset += chunk.length;
|
852
|
-
}
|
853
|
-
chunks.length = 0;
|
848
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
854
849
|
totalLength = 0;
|
855
|
-
const
|
856
|
-
|
857
|
-
|
858
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
859
|
-
);
|
850
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
851
|
+
for (const streamPart of streamParts2) {
|
852
|
+
yield streamPart;
|
860
853
|
}
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
854
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
855
|
+
reader.cancel();
|
856
|
+
break;
|
857
|
+
}
|
858
|
+
}
|
859
|
+
}
|
860
|
+
|
861
|
+
// shared/parse-complex-response.ts
|
862
|
+
async function parseComplexResponse({
|
863
|
+
reader,
|
864
|
+
abortControllerRef,
|
865
|
+
update,
|
866
|
+
onFinish,
|
867
|
+
generateId = nanoid,
|
868
|
+
getCurrentDate = () => /* @__PURE__ */ new Date()
|
869
|
+
}) {
|
870
|
+
const createdAt = getCurrentDate();
|
871
|
+
const prefixMap = {
|
872
|
+
data: []
|
873
|
+
};
|
874
|
+
for await (const { type, value } of readDataStream(reader, {
|
875
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
876
|
+
})) {
|
877
|
+
if (type === "text") {
|
878
|
+
if (prefixMap["text"]) {
|
879
|
+
prefixMap["text"] = {
|
880
|
+
...prefixMap["text"],
|
881
|
+
content: (prefixMap["text"].content || "") + value
|
882
|
+
};
|
883
|
+
} else {
|
884
|
+
prefixMap["text"] = {
|
880
885
|
id: generateId(),
|
881
886
|
role: "assistant",
|
882
|
-
content:
|
883
|
-
function_call: value2.function_call,
|
884
|
-
name: value2.function_call.name,
|
887
|
+
content: value,
|
885
888
|
createdAt
|
886
889
|
};
|
887
|
-
functionCallMessage = prefixMap["function_call"];
|
888
|
-
}
|
889
|
-
if (type === "data") {
|
890
|
-
prefixMap["data"].push(...value2);
|
891
|
-
}
|
892
|
-
const responseMessage = prefixMap["text"];
|
893
|
-
const merged = [functionCallMessage, responseMessage].filter(
|
894
|
-
Boolean
|
895
|
-
);
|
896
|
-
update(merged, [...prefixMap["data"]]);
|
897
|
-
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
898
|
-
reader.cancel();
|
899
|
-
break;
|
900
890
|
}
|
901
891
|
}
|
892
|
+
let functionCallMessage = null;
|
893
|
+
if (type === "function_call") {
|
894
|
+
prefixMap["function_call"] = {
|
895
|
+
id: generateId(),
|
896
|
+
role: "assistant",
|
897
|
+
content: "",
|
898
|
+
function_call: value.function_call,
|
899
|
+
name: value.function_call.name,
|
900
|
+
createdAt
|
901
|
+
};
|
902
|
+
functionCallMessage = prefixMap["function_call"];
|
903
|
+
}
|
904
|
+
if (type === "data") {
|
905
|
+
prefixMap["data"].push(...value);
|
906
|
+
}
|
907
|
+
const responseMessage = prefixMap["text"];
|
908
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
909
|
+
Boolean
|
910
|
+
);
|
911
|
+
update(merged, [...prefixMap["data"]]);
|
902
912
|
}
|
903
913
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
904
914
|
return {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ai",
|
3
|
-
"version": "2.2.
|
3
|
+
"version": "2.2.27",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"sideEffects": false,
|
6
6
|
"main": "./dist/index.js",
|
@@ -76,14 +76,14 @@
|
|
76
76
|
"@types/node": "^17.0.12",
|
77
77
|
"@types/react": "^18.2.8",
|
78
78
|
"@types/react-dom": "^18.2.0",
|
79
|
-
"@
|
79
|
+
"@vitejs/plugin-react": "4.2.0",
|
80
|
+
"@vitejs/plugin-vue": "4.5.0",
|
80
81
|
"eslint": "^7.32.0",
|
81
|
-
"
|
82
|
-
"
|
83
|
-
"
|
84
|
-
"langchain": "0.0.172",
|
82
|
+
"jsdom": "^23.0.0",
|
83
|
+
"langchain": "0.0.196",
|
84
|
+
"msw": "2.0.9",
|
85
85
|
"openai": "4.16.1",
|
86
|
-
"
|
86
|
+
"react-dom": "^18.2.0",
|
87
87
|
"tsup": "^6.7.0",
|
88
88
|
"typescript": "5.1.3",
|
89
89
|
"@vercel/ai-tsconfig": "0.0.0",
|
@@ -138,8 +138,8 @@
|
|
138
138
|
"type-check": "tsc --noEmit",
|
139
139
|
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
140
140
|
"test": "pnpm test:node && pnpm test:edge && pnpm test:ui",
|
141
|
-
"test:edge": "
|
142
|
-
"test:node": "
|
143
|
-
"test:ui": "
|
141
|
+
"test:edge": "vitest --config vitest.edge.config.js --run",
|
142
|
+
"test:node": "vitest --config vitest.node.config.js --run",
|
143
|
+
"test:ui": "vitest --config vitest.ui.config.js --run"
|
144
144
|
}
|
145
145
|
}
|
package/react/dist/index.d.ts
CHANGED
@@ -341,6 +341,8 @@ type UseCompletionHelpers = {
|
|
341
341
|
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
|
342
342
|
/** Whether the API request is in progress */
|
343
343
|
isLoading: boolean;
|
344
|
+
/** Additional data added on the server via StreamData */
|
345
|
+
data?: JSONValue[] | undefined;
|
344
346
|
};
|
345
347
|
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
346
348
|
|
@@ -363,12 +365,16 @@ type UseAssistantHelpers = {
|
|
363
365
|
/** Current error, if any */
|
364
366
|
error: undefined | unknown;
|
365
367
|
};
|
366
|
-
|
368
|
+
type UseAssistantOptions = {
|
367
369
|
api: string;
|
368
370
|
threadId?: string | undefined;
|
369
|
-
|
371
|
+
credentials?: RequestCredentials;
|
372
|
+
headers?: Record<string, string> | Headers;
|
373
|
+
body?: object;
|
374
|
+
};
|
375
|
+
declare function experimental_useAssistant({ api, threadId: threadIdParam, credentials, headers, body, }: UseAssistantOptions): UseAssistantHelpers;
|
370
376
|
|
371
|
-
export { AssistantStatus, CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useAssistant, useChat, useCompletion };
|
377
|
+
export { AssistantStatus, CreateMessage, Message, UseAssistantHelpers, UseAssistantOptions, UseChatHelpers, UseChatOptions, UseCompletionHelpers, experimental_useAssistant, useChat, useCompletion };
|
372
378
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
373
379
|
|
374
380
|
type Props = {
|
package/react/dist/index.js
CHANGED
@@ -186,21 +186,22 @@ function createChunkDecoder(complex) {
|
|
186
186
|
}
|
187
187
|
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
188
188
|
|
189
|
-
// shared/
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
189
|
+
// shared/read-data-stream.ts
|
190
|
+
var NEWLINE = "\n".charCodeAt(0);
|
191
|
+
function concatChunks(chunks, totalLength) {
|
192
|
+
const concatenatedChunks = new Uint8Array(totalLength);
|
193
|
+
let offset = 0;
|
194
|
+
for (const chunk of chunks) {
|
195
|
+
concatenatedChunks.set(chunk, offset);
|
196
|
+
offset += chunk.length;
|
197
|
+
}
|
198
|
+
chunks.length = 0;
|
199
|
+
return concatenatedChunks;
|
200
|
+
}
|
201
|
+
async function* readDataStream(reader, {
|
202
|
+
isAborted
|
203
|
+
} = {}) {
|
204
|
+
const decoder = new TextDecoder();
|
204
205
|
const chunks = [];
|
205
206
|
let totalLength = 0;
|
206
207
|
while (true) {
|
@@ -215,61 +216,70 @@ async function parseComplexResponse({
|
|
215
216
|
if (chunks.length === 0) {
|
216
217
|
break;
|
217
218
|
}
|
218
|
-
|
219
|
-
let offset = 0;
|
220
|
-
for (const chunk of chunks) {
|
221
|
-
concatenatedChunks.set(chunk, offset);
|
222
|
-
offset += chunk.length;
|
223
|
-
}
|
224
|
-
chunks.length = 0;
|
219
|
+
const concatenatedChunks = concatChunks(chunks, totalLength);
|
225
220
|
totalLength = 0;
|
226
|
-
const
|
227
|
-
|
228
|
-
|
229
|
-
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
230
|
-
);
|
221
|
+
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
222
|
+
for (const streamPart of streamParts2) {
|
223
|
+
yield streamPart;
|
231
224
|
}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
225
|
+
if (isAborted == null ? void 0 : isAborted()) {
|
226
|
+
reader.cancel();
|
227
|
+
break;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
// shared/parse-complex-response.ts
|
233
|
+
async function parseComplexResponse({
|
234
|
+
reader,
|
235
|
+
abortControllerRef,
|
236
|
+
update,
|
237
|
+
onFinish,
|
238
|
+
generateId = nanoid,
|
239
|
+
getCurrentDate = () => /* @__PURE__ */ new Date()
|
240
|
+
}) {
|
241
|
+
const createdAt = getCurrentDate();
|
242
|
+
const prefixMap = {
|
243
|
+
data: []
|
244
|
+
};
|
245
|
+
for await (const { type, value } of readDataStream(reader, {
|
246
|
+
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
247
|
+
})) {
|
248
|
+
if (type === "text") {
|
249
|
+
if (prefixMap["text"]) {
|
250
|
+
prefixMap["text"] = {
|
251
|
+
...prefixMap["text"],
|
252
|
+
content: (prefixMap["text"].content || "") + value
|
253
|
+
};
|
254
|
+
} else {
|
255
|
+
prefixMap["text"] = {
|
251
256
|
id: generateId(),
|
252
257
|
role: "assistant",
|
253
|
-
content:
|
254
|
-
function_call: value2.function_call,
|
255
|
-
name: value2.function_call.name,
|
258
|
+
content: value,
|
256
259
|
createdAt
|
257
260
|
};
|
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
261
|
}
|
272
262
|
}
|
263
|
+
let functionCallMessage = null;
|
264
|
+
if (type === "function_call") {
|
265
|
+
prefixMap["function_call"] = {
|
266
|
+
id: generateId(),
|
267
|
+
role: "assistant",
|
268
|
+
content: "",
|
269
|
+
function_call: value.function_call,
|
270
|
+
name: value.function_call.name,
|
271
|
+
createdAt
|
272
|
+
};
|
273
|
+
functionCallMessage = prefixMap["function_call"];
|
274
|
+
}
|
275
|
+
if (type === "data") {
|
276
|
+
prefixMap["data"].push(...value);
|
277
|
+
}
|
278
|
+
const responseMessage = prefixMap["text"];
|
279
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
280
|
+
Boolean
|
281
|
+
);
|
282
|
+
update(merged, [...prefixMap["data"]]);
|
273
283
|
}
|
274
284
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
275
285
|
return {
|
@@ -714,29 +724,6 @@ function useChat({
|
|
714
724
|
// react/use-completion.ts
|
715
725
|
var import_react2 = require("react");
|
716
726
|
var import_swr2 = __toESM(require("swr"));
|
717
|
-
|
718
|
-
// shared/process-message-stream.ts
|
719
|
-
async function processMessageStream(reader, processMessage) {
|
720
|
-
const decoder = new TextDecoder();
|
721
|
-
let buffer = "";
|
722
|
-
while (true) {
|
723
|
-
const { done, value } = await reader.read();
|
724
|
-
if (done) {
|
725
|
-
if (buffer.length > 0) {
|
726
|
-
processMessage(buffer);
|
727
|
-
}
|
728
|
-
break;
|
729
|
-
}
|
730
|
-
buffer += decoder.decode(value, { stream: true });
|
731
|
-
let endIndex;
|
732
|
-
while ((endIndex = buffer.indexOf("\n")) !== -1) {
|
733
|
-
processMessage(buffer.substring(0, endIndex).trim());
|
734
|
-
buffer = buffer.substring(endIndex + 1);
|
735
|
-
}
|
736
|
-
}
|
737
|
-
}
|
738
|
-
|
739
|
-
// react/use-completion.ts
|
740
727
|
function useCompletion({
|
741
728
|
api = "/api/completion",
|
742
729
|
id,
|
@@ -758,6 +745,7 @@ function useCompletion({
|
|
758
745
|
[completionId, "loading"],
|
759
746
|
null
|
760
747
|
);
|
748
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
761
749
|
const [error, setError] = (0, import_react2.useState)(void 0);
|
762
750
|
const completion = data;
|
763
751
|
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
@@ -816,13 +804,24 @@ function useCompletion({
|
|
816
804
|
const reader = res.body.getReader();
|
817
805
|
const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
|
818
806
|
if (isComplexMode) {
|
819
|
-
await
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
807
|
+
for await (const { type, value } of readDataStream(reader, {
|
808
|
+
isAborted: () => abortController2 === null
|
809
|
+
})) {
|
810
|
+
switch (type) {
|
811
|
+
case "text": {
|
812
|
+
result += value;
|
813
|
+
mutate(result, false);
|
814
|
+
break;
|
815
|
+
}
|
816
|
+
case "data": {
|
817
|
+
mutateStreamData(
|
818
|
+
[...streamData || [], ...value || []],
|
819
|
+
false
|
820
|
+
);
|
821
|
+
break;
|
822
|
+
}
|
824
823
|
}
|
825
|
-
}
|
824
|
+
}
|
826
825
|
} else {
|
827
826
|
const decoder = createChunkDecoder();
|
828
827
|
while (true) {
|
@@ -911,7 +910,8 @@ function useCompletion({
|
|
911
910
|
setInput,
|
912
911
|
handleInputChange,
|
913
912
|
handleSubmit,
|
914
|
-
isLoading
|
913
|
+
isLoading,
|
914
|
+
data: streamData
|
915
915
|
};
|
916
916
|
}
|
917
917
|
|
@@ -919,7 +919,10 @@ function useCompletion({
|
|
919
919
|
var import_react3 = require("react");
|
920
920
|
function experimental_useAssistant({
|
921
921
|
api,
|
922
|
-
threadId: threadIdParam
|
922
|
+
threadId: threadIdParam,
|
923
|
+
credentials,
|
924
|
+
headers,
|
925
|
+
body
|
923
926
|
}) {
|
924
927
|
const [messages, setMessages] = (0, import_react3.useState)([]);
|
925
928
|
const [input, setInput] = (0, import_react3.useState)("");
|
@@ -943,8 +946,10 @@ function experimental_useAssistant({
|
|
943
946
|
setInput("");
|
944
947
|
const result = await fetch(api, {
|
945
948
|
method: "POST",
|
946
|
-
|
949
|
+
credentials,
|
950
|
+
headers: { "Content-Type": "application/json", ...headers },
|
947
951
|
body: JSON.stringify({
|
952
|
+
...body,
|
948
953
|
// always use user-provided threadId when available:
|
949
954
|
threadId: (_b = threadIdParam != null ? threadIdParam : threadId) != null ? _b : null,
|
950
955
|
message: input,
|
@@ -955,9 +960,10 @@ function experimental_useAssistant({
|
|
955
960
|
if (result.body == null) {
|
956
961
|
throw new Error("The response body is empty.");
|
957
962
|
}
|
958
|
-
|
959
|
-
|
960
|
-
|
963
|
+
try {
|
964
|
+
for await (const { type, value } of readDataStream(
|
965
|
+
result.body.getReader()
|
966
|
+
)) {
|
961
967
|
switch (type) {
|
962
968
|
case "assistant_message": {
|
963
969
|
setMessages((messages2) => [
|
@@ -984,10 +990,10 @@ function experimental_useAssistant({
|
|
984
990
|
break;
|
985
991
|
}
|
986
992
|
}
|
987
|
-
} catch (error2) {
|
988
|
-
setError(error2);
|
989
993
|
}
|
990
|
-
})
|
994
|
+
} catch (error2) {
|
995
|
+
setError(error2);
|
996
|
+
}
|
991
997
|
setStatus("awaiting_message");
|
992
998
|
};
|
993
999
|
return {
|