ai 3.0.22 → 3.0.24
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.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/react/dist/index.d.mts +6 -2
- package/react/dist/index.d.ts +6 -2
- package/react/dist/index.js +104 -21
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +104 -21
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/index.d.ts +35 -3
- package/rsc/dist/rsc-server.d.mts +35 -3
- package/rsc/dist/rsc-server.mjs +2 -2
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +6 -2
- package/solid/dist/index.d.ts +6 -2
- package/solid/dist/index.js +102 -20
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +102 -20
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +6 -2
- package/svelte/dist/index.d.ts +6 -2
- package/svelte/dist/index.js +104 -21
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +104 -21
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +6 -2
- package/vue/dist/index.d.ts +6 -2
- package/vue/dist/index.js +102 -20
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +102 -20
- package/vue/dist/index.mjs.map +1 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ai",
|
3
|
-
"version": "3.0.
|
3
|
+
"version": "3.0.24",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"sideEffects": false,
|
6
6
|
"main": "./dist/index.js",
|
@@ -58,7 +58,7 @@
|
|
58
58
|
},
|
59
59
|
"dependencies": {
|
60
60
|
"@ai-sdk/provider": "0.0.0",
|
61
|
-
"@ai-sdk/provider-utils": "0.0.
|
61
|
+
"@ai-sdk/provider-utils": "0.0.1",
|
62
62
|
"secure-json-parse": "2.7.0",
|
63
63
|
"eventsource-parser": "1.1.2",
|
64
64
|
"jsondiffpatch": "0.6.0",
|
package/react/dist/index.d.mts
CHANGED
@@ -202,6 +202,8 @@ type UseChatOptions = {
|
|
202
202
|
* handle the extra fields before forwarding the request to the AI service.
|
203
203
|
*/
|
204
204
|
sendExtraMessageFields?: boolean;
|
205
|
+
/** Stream mode (default to "stream-data") */
|
206
|
+
streamMode?: 'stream-data' | 'text';
|
205
207
|
};
|
206
208
|
type UseCompletionOptions = {
|
207
209
|
/**
|
@@ -258,6 +260,8 @@ type UseCompletionOptions = {
|
|
258
260
|
* ```
|
259
261
|
*/
|
260
262
|
body?: object;
|
263
|
+
/** Stream mode (default to "stream-data") */
|
264
|
+
streamMode?: 'stream-data' | 'text';
|
261
265
|
};
|
262
266
|
type JSONValue = null | string | number | boolean | {
|
263
267
|
[x: string]: JSONValue;
|
@@ -352,7 +356,7 @@ type StreamingReactResponseAction = (payload: {
|
|
352
356
|
messages: Message[];
|
353
357
|
data?: Record<string, string>;
|
354
358
|
}) => Promise<experimental_StreamingReactResponse>;
|
355
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
|
359
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
|
356
360
|
api?: string | StreamingReactResponseAction;
|
357
361
|
key?: string;
|
358
362
|
}): UseChatHelpers;
|
@@ -401,7 +405,7 @@ type UseCompletionHelpers = {
|
|
401
405
|
/** Additional data added on the server via StreamData */
|
402
406
|
data?: JSONValue[];
|
403
407
|
};
|
404
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
408
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
405
409
|
|
406
410
|
type AssistantStatus = 'in_progress' | 'awaiting_message';
|
407
411
|
type UseAssistantHelpers = {
|
package/react/dist/index.d.ts
CHANGED
@@ -202,6 +202,8 @@ type UseChatOptions = {
|
|
202
202
|
* handle the extra fields before forwarding the request to the AI service.
|
203
203
|
*/
|
204
204
|
sendExtraMessageFields?: boolean;
|
205
|
+
/** Stream mode (default to "stream-data") */
|
206
|
+
streamMode?: 'stream-data' | 'text';
|
205
207
|
};
|
206
208
|
type UseCompletionOptions = {
|
207
209
|
/**
|
@@ -258,6 +260,8 @@ type UseCompletionOptions = {
|
|
258
260
|
* ```
|
259
261
|
*/
|
260
262
|
body?: object;
|
263
|
+
/** Stream mode (default to "stream-data") */
|
264
|
+
streamMode?: 'stream-data' | 'text';
|
261
265
|
};
|
262
266
|
type JSONValue = null | string | number | boolean | {
|
263
267
|
[x: string]: JSONValue;
|
@@ -352,7 +356,7 @@ type StreamingReactResponseAction = (payload: {
|
|
352
356
|
messages: Message[];
|
353
357
|
data?: Record<string, string>;
|
354
358
|
}) => Promise<experimental_StreamingReactResponse>;
|
355
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
|
359
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
|
356
360
|
api?: string | StreamingReactResponseAction;
|
357
361
|
key?: string;
|
358
362
|
}): UseChatHelpers;
|
@@ -401,7 +405,7 @@ type UseCompletionHelpers = {
|
|
401
405
|
/** Additional data added on the server via StreamData */
|
402
406
|
data?: JSONValue[];
|
403
407
|
};
|
404
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
408
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
405
409
|
|
406
410
|
type AssistantStatus = 'in_progress' | 'awaiting_message';
|
407
411
|
type UseAssistantHelpers = {
|
package/react/dist/index.js
CHANGED
@@ -375,11 +375,28 @@ async function parseComplexResponse({
|
|
375
375
|
};
|
376
376
|
}
|
377
377
|
|
378
|
+
// shared/utils.ts
|
379
|
+
function createChunkDecoder(complex) {
|
380
|
+
const decoder = new TextDecoder();
|
381
|
+
if (!complex) {
|
382
|
+
return function(chunk) {
|
383
|
+
if (!chunk)
|
384
|
+
return "";
|
385
|
+
return decoder.decode(chunk, { stream: true });
|
386
|
+
};
|
387
|
+
}
|
388
|
+
return function(chunk) {
|
389
|
+
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
390
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
391
|
+
};
|
392
|
+
}
|
393
|
+
|
378
394
|
// shared/call-chat-api.ts
|
379
395
|
async function callChatApi({
|
380
396
|
api,
|
381
397
|
messages,
|
382
398
|
body,
|
399
|
+
streamMode = "stream-data",
|
383
400
|
credentials,
|
384
401
|
headers,
|
385
402
|
abortController,
|
@@ -423,17 +440,52 @@ async function callChatApi({
|
|
423
440
|
throw new Error("The response body is empty.");
|
424
441
|
}
|
425
442
|
const reader = response.body.getReader();
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
443
|
+
switch (streamMode) {
|
444
|
+
case "text": {
|
445
|
+
const decoder = createChunkDecoder();
|
446
|
+
const resultMessage = {
|
447
|
+
id: generateId2(),
|
448
|
+
createdAt: /* @__PURE__ */ new Date(),
|
449
|
+
role: "assistant",
|
450
|
+
content: ""
|
451
|
+
};
|
452
|
+
while (true) {
|
453
|
+
const { done, value } = await reader.read();
|
454
|
+
if (done) {
|
455
|
+
break;
|
456
|
+
}
|
457
|
+
resultMessage.content += decoder(value);
|
458
|
+
resultMessage.id = generateId2();
|
459
|
+
onUpdate([{ ...resultMessage }], []);
|
460
|
+
if ((abortController == null ? void 0 : abortController()) === null) {
|
461
|
+
reader.cancel();
|
462
|
+
break;
|
463
|
+
}
|
433
464
|
}
|
434
|
-
|
435
|
-
|
436
|
-
|
465
|
+
onFinish == null ? void 0 : onFinish(resultMessage);
|
466
|
+
return {
|
467
|
+
messages: [resultMessage],
|
468
|
+
data: []
|
469
|
+
};
|
470
|
+
}
|
471
|
+
case "stream-data": {
|
472
|
+
return await parseComplexResponse({
|
473
|
+
reader,
|
474
|
+
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
475
|
+
update: onUpdate,
|
476
|
+
onFinish(prefixMap) {
|
477
|
+
if (onFinish && prefixMap.text != null) {
|
478
|
+
onFinish(prefixMap.text);
|
479
|
+
}
|
480
|
+
},
|
481
|
+
generateId: generateId2
|
482
|
+
});
|
483
|
+
}
|
484
|
+
default: {
|
485
|
+
const exhaustiveCheck = streamMode;
|
486
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
487
|
+
}
|
488
|
+
}
|
437
489
|
}
|
438
490
|
|
439
491
|
// shared/process-chat-stream.ts
|
@@ -553,7 +605,7 @@ async function processChatStream({
|
|
553
605
|
}
|
554
606
|
|
555
607
|
// react/use-chat.ts
|
556
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, onFinish, onResponse, sendExtraMessageFields) => {
|
608
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
|
557
609
|
var _a, _b;
|
558
610
|
const previousMessages = messagesRef.current;
|
559
611
|
mutate(chatRequest.messages, false);
|
@@ -624,6 +676,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
624
676
|
tool_choice: chatRequest.tool_choice
|
625
677
|
}
|
626
678
|
},
|
679
|
+
streamMode,
|
627
680
|
credentials: extraMetadataRef.current.credentials,
|
628
681
|
headers: {
|
629
682
|
...extraMetadataRef.current.headers,
|
@@ -650,6 +703,7 @@ function useChat({
|
|
650
703
|
sendExtraMessageFields,
|
651
704
|
experimental_onFunctionCall,
|
652
705
|
experimental_onToolCall,
|
706
|
+
streamMode,
|
653
707
|
onResponse,
|
654
708
|
onFinish,
|
655
709
|
onError,
|
@@ -708,6 +762,7 @@ function useChat({
|
|
708
762
|
messagesRef,
|
709
763
|
abortControllerRef,
|
710
764
|
generateId2,
|
765
|
+
streamMode,
|
711
766
|
onFinish,
|
712
767
|
onResponse,
|
713
768
|
sendExtraMessageFields
|
@@ -878,6 +933,7 @@ async function callCompletionApi({
|
|
878
933
|
credentials,
|
879
934
|
headers,
|
880
935
|
body,
|
936
|
+
streamMode = "stream-data",
|
881
937
|
setCompletion,
|
882
938
|
setLoading,
|
883
939
|
setError,
|
@@ -925,19 +981,44 @@ async function callCompletionApi({
|
|
925
981
|
}
|
926
982
|
let result = "";
|
927
983
|
const reader = res.body.getReader();
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
984
|
+
switch (streamMode) {
|
985
|
+
case "text": {
|
986
|
+
const decoder = createChunkDecoder();
|
987
|
+
while (true) {
|
988
|
+
const { done, value } = await reader.read();
|
989
|
+
if (done) {
|
990
|
+
break;
|
991
|
+
}
|
992
|
+
result += decoder(value);
|
934
993
|
setCompletion(result);
|
935
|
-
|
994
|
+
if (abortController === null) {
|
995
|
+
reader.cancel();
|
996
|
+
break;
|
997
|
+
}
|
936
998
|
}
|
937
|
-
|
938
|
-
|
939
|
-
|
999
|
+
break;
|
1000
|
+
}
|
1001
|
+
case "stream-data": {
|
1002
|
+
for await (const { type, value } of readDataStream(reader, {
|
1003
|
+
isAborted: () => abortController === null
|
1004
|
+
})) {
|
1005
|
+
switch (type) {
|
1006
|
+
case "text": {
|
1007
|
+
result += value;
|
1008
|
+
setCompletion(result);
|
1009
|
+
break;
|
1010
|
+
}
|
1011
|
+
case "data": {
|
1012
|
+
onData == null ? void 0 : onData(value);
|
1013
|
+
break;
|
1014
|
+
}
|
1015
|
+
}
|
940
1016
|
}
|
1017
|
+
break;
|
1018
|
+
}
|
1019
|
+
default: {
|
1020
|
+
const exhaustiveCheck = streamMode;
|
1021
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
941
1022
|
}
|
942
1023
|
}
|
943
1024
|
if (onFinish) {
|
@@ -970,6 +1051,7 @@ function useCompletion({
|
|
970
1051
|
credentials,
|
971
1052
|
headers,
|
972
1053
|
body,
|
1054
|
+
streamMode,
|
973
1055
|
onResponse,
|
974
1056
|
onFinish,
|
975
1057
|
onError
|
@@ -1009,6 +1091,7 @@ function useCompletion({
|
|
1009
1091
|
...extraMetadataRef.current.body,
|
1010
1092
|
...options == null ? void 0 : options.body
|
1011
1093
|
},
|
1094
|
+
streamMode,
|
1012
1095
|
setCompletion: (completion2) => mutate(completion2, false),
|
1013
1096
|
setLoading: mutateLoading,
|
1014
1097
|
setError,
|