ai 3.1.0-canary.3 → 3.1.0
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/README.md +1 -1
- package/dist/index.d.mts +982 -24
- package/dist/index.d.ts +982 -24
- package/dist/index.js +1748 -175
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1723 -174
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -31
- package/prompts/dist/index.d.mts +13 -1
- package/prompts/dist/index.d.ts +13 -1
- package/prompts/dist/index.js +13 -0
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs +12 -0
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +27 -6
- package/react/dist/index.d.ts +31 -8
- package/react/dist/index.js +155 -141
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +154 -141
- package/react/dist/index.mjs.map +1 -1
- package/react/dist/index.server.d.mts +4 -2
- package/react/dist/index.server.d.ts +4 -2
- package/react/dist/index.server.js.map +1 -1
- package/react/dist/index.server.mjs.map +1 -1
- package/rsc/dist/index.d.ts +385 -20
- package/rsc/dist/rsc-client.d.mts +1 -1
- package/rsc/dist/rsc-client.mjs +2 -0
- package/rsc/dist/rsc-client.mjs.map +1 -1
- package/rsc/dist/rsc-server.d.mts +367 -20
- package/rsc/dist/rsc-server.mjs +676 -35
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.d.mts +24 -9
- package/rsc/dist/rsc-shared.mjs +98 -4
- package/rsc/dist/rsc-shared.mjs.map +1 -1
- package/solid/dist/index.d.mts +7 -3
- package/solid/dist/index.d.ts +7 -3
- package/solid/dist/index.js +106 -107
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +106 -107
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +7 -3
- package/svelte/dist/index.d.ts +7 -3
- package/svelte/dist/index.js +109 -109
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +109 -109
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +7 -3
- package/vue/dist/index.d.ts +7 -3
- package/vue/dist/index.js +106 -107
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +106 -107
- package/vue/dist/index.mjs.map +1 -1
- package/ai-model-specification/dist/index.d.mts +0 -606
- package/ai-model-specification/dist/index.d.ts +0 -606
- package/ai-model-specification/dist/index.js +0 -617
- package/ai-model-specification/dist/index.js.map +0 -1
- package/ai-model-specification/dist/index.mjs +0 -560
- package/ai-model-specification/dist/index.mjs.map +0 -1
- package/core/dist/index.d.mts +0 -590
- package/core/dist/index.d.ts +0 -590
- package/core/dist/index.js +0 -1528
- package/core/dist/index.js.map +0 -1
- package/core/dist/index.mjs +0 -1481
- package/core/dist/index.mjs.map +0 -1
- package/provider/dist/index.d.mts +0 -429
- package/provider/dist/index.d.ts +0 -429
- package/provider/dist/index.js +0 -1194
- package/provider/dist/index.js.map +0 -1
- package/provider/dist/index.mjs +0 -1158
- package/provider/dist/index.mjs.map +0 -1
package/solid/dist/index.js
CHANGED
@@ -130,9 +130,9 @@ var toolCallStreamPart = {
|
|
130
130
|
code: "7",
|
131
131
|
name: "tool_calls",
|
132
132
|
parse: (value) => {
|
133
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
134
|
-
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
135
|
-
|
133
|
+
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
134
|
+
(tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
135
|
+
)) {
|
136
136
|
throw new Error(
|
137
137
|
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
138
138
|
);
|
@@ -245,27 +245,12 @@ async function* readDataStream(reader, {
|
|
245
245
|
}
|
246
246
|
}
|
247
247
|
|
248
|
-
// shared/
|
248
|
+
// shared/generate-id.ts
|
249
249
|
var import_non_secure = require("nanoid/non-secure");
|
250
|
-
var
|
250
|
+
var generateId = (0, import_non_secure.customAlphabet)(
|
251
251
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
252
252
|
7
|
253
253
|
);
|
254
|
-
function createChunkDecoder(complex) {
|
255
|
-
const decoder = new TextDecoder();
|
256
|
-
if (!complex) {
|
257
|
-
return function(chunk) {
|
258
|
-
if (!chunk)
|
259
|
-
return "";
|
260
|
-
return decoder.decode(chunk, { stream: true });
|
261
|
-
};
|
262
|
-
}
|
263
|
-
return function(chunk) {
|
264
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
265
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
266
|
-
};
|
267
|
-
}
|
268
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
269
254
|
|
270
255
|
// shared/parse-complex-response.ts
|
271
256
|
function assignAnnotationsToMessage(message, annotations) {
|
@@ -278,7 +263,7 @@ async function parseComplexResponse({
|
|
278
263
|
abortControllerRef,
|
279
264
|
update,
|
280
265
|
onFinish,
|
281
|
-
generateId =
|
266
|
+
generateId: generateId2 = generateId,
|
282
267
|
getCurrentDate = () => /* @__PURE__ */ new Date()
|
283
268
|
}) {
|
284
269
|
const createdAt = getCurrentDate();
|
@@ -297,7 +282,7 @@ async function parseComplexResponse({
|
|
297
282
|
};
|
298
283
|
} else {
|
299
284
|
prefixMap["text"] = {
|
300
|
-
id:
|
285
|
+
id: generateId2(),
|
301
286
|
role: "assistant",
|
302
287
|
content: value,
|
303
288
|
createdAt
|
@@ -307,7 +292,7 @@ async function parseComplexResponse({
|
|
307
292
|
let functionCallMessage = null;
|
308
293
|
if (type === "function_call") {
|
309
294
|
prefixMap["function_call"] = {
|
310
|
-
id:
|
295
|
+
id: generateId2(),
|
311
296
|
role: "assistant",
|
312
297
|
content: "",
|
313
298
|
function_call: value.function_call,
|
@@ -319,7 +304,7 @@ async function parseComplexResponse({
|
|
319
304
|
let toolCallMessage = null;
|
320
305
|
if (type === "tool_calls") {
|
321
306
|
prefixMap["tool_calls"] = {
|
322
|
-
id:
|
307
|
+
id: generateId2(),
|
323
308
|
role: "assistant",
|
324
309
|
content: "",
|
325
310
|
tool_calls: value.tool_calls,
|
@@ -378,20 +363,36 @@ async function parseComplexResponse({
|
|
378
363
|
};
|
379
364
|
}
|
380
365
|
|
366
|
+
// shared/utils.ts
|
367
|
+
function createChunkDecoder(complex) {
|
368
|
+
const decoder = new TextDecoder();
|
369
|
+
if (!complex) {
|
370
|
+
return function(chunk) {
|
371
|
+
if (!chunk)
|
372
|
+
return "";
|
373
|
+
return decoder.decode(chunk, { stream: true });
|
374
|
+
};
|
375
|
+
}
|
376
|
+
return function(chunk) {
|
377
|
+
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
378
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
379
|
+
};
|
380
|
+
}
|
381
|
+
|
381
382
|
// shared/call-chat-api.ts
|
382
383
|
async function callChatApi({
|
383
384
|
api,
|
384
385
|
messages,
|
385
386
|
body,
|
387
|
+
streamMode = "stream-data",
|
386
388
|
credentials,
|
387
389
|
headers,
|
388
390
|
abortController,
|
389
|
-
appendMessage,
|
390
391
|
restoreMessagesOnFailure,
|
391
392
|
onResponse,
|
392
393
|
onUpdate,
|
393
394
|
onFinish,
|
394
|
-
generateId
|
395
|
+
generateId: generateId2
|
395
396
|
}) {
|
396
397
|
var _a;
|
397
398
|
const response = await fetch(api, {
|
@@ -427,63 +428,51 @@ async function callChatApi({
|
|
427
428
|
throw new Error("The response body is empty.");
|
428
429
|
}
|
429
430
|
const reader = response.body.getReader();
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
431
|
+
switch (streamMode) {
|
432
|
+
case "text": {
|
433
|
+
const decoder = createChunkDecoder();
|
434
|
+
const resultMessage = {
|
435
|
+
id: generateId2(),
|
436
|
+
createdAt: /* @__PURE__ */ new Date(),
|
437
|
+
role: "assistant",
|
438
|
+
content: ""
|
439
|
+
};
|
440
|
+
while (true) {
|
441
|
+
const { done, value } = await reader.read();
|
442
|
+
if (done) {
|
443
|
+
break;
|
444
|
+
}
|
445
|
+
resultMessage.content += decoder(value);
|
446
|
+
resultMessage.id = generateId2();
|
447
|
+
onUpdate([{ ...resultMessage }], []);
|
448
|
+
if ((abortController == null ? void 0 : abortController()) === null) {
|
449
|
+
reader.cancel();
|
450
|
+
break;
|
439
451
|
}
|
440
|
-
},
|
441
|
-
generateId
|
442
|
-
});
|
443
|
-
} else {
|
444
|
-
const createdAt = /* @__PURE__ */ new Date();
|
445
|
-
const decode = createChunkDecoder(false);
|
446
|
-
let streamedResponse = "";
|
447
|
-
const replyId = generateId();
|
448
|
-
let responseMessage = {
|
449
|
-
id: replyId,
|
450
|
-
createdAt,
|
451
|
-
content: "",
|
452
|
-
role: "assistant"
|
453
|
-
};
|
454
|
-
while (true) {
|
455
|
-
const { done, value } = await reader.read();
|
456
|
-
if (done) {
|
457
|
-
break;
|
458
|
-
}
|
459
|
-
streamedResponse += decode(value);
|
460
|
-
if (streamedResponse.startsWith('{"function_call":')) {
|
461
|
-
responseMessage["function_call"] = streamedResponse;
|
462
|
-
} else if (streamedResponse.startsWith('{"tool_calls":')) {
|
463
|
-
responseMessage["tool_calls"] = streamedResponse;
|
464
|
-
} else {
|
465
|
-
responseMessage["content"] = streamedResponse;
|
466
|
-
}
|
467
|
-
appendMessage({ ...responseMessage });
|
468
|
-
if ((abortController == null ? void 0 : abortController()) === null) {
|
469
|
-
reader.cancel();
|
470
|
-
break;
|
471
452
|
}
|
453
|
+
onFinish == null ? void 0 : onFinish(resultMessage);
|
454
|
+
return {
|
455
|
+
messages: [resultMessage],
|
456
|
+
data: []
|
457
|
+
};
|
472
458
|
}
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
459
|
+
case "stream-data": {
|
460
|
+
return await parseComplexResponse({
|
461
|
+
reader,
|
462
|
+
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
463
|
+
update: onUpdate,
|
464
|
+
onFinish(prefixMap) {
|
465
|
+
if (onFinish && prefixMap.text != null) {
|
466
|
+
onFinish(prefixMap.text);
|
467
|
+
}
|
468
|
+
},
|
469
|
+
generateId: generateId2
|
470
|
+
});
|
482
471
|
}
|
483
|
-
|
484
|
-
|
472
|
+
default: {
|
473
|
+
const exhaustiveCheck = streamMode;
|
474
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
485
475
|
}
|
486
|
-
return responseMessage;
|
487
476
|
}
|
488
477
|
}
|
489
478
|
|
@@ -625,7 +614,8 @@ function useChat({
|
|
625
614
|
credentials,
|
626
615
|
headers,
|
627
616
|
body,
|
628
|
-
|
617
|
+
streamMode,
|
618
|
+
generateId: generateId2 = generateId
|
629
619
|
} = {}) {
|
630
620
|
const chatId = id || `chat-${uniqueId++}`;
|
631
621
|
const key = `${api}|${chatId}`;
|
@@ -681,6 +671,7 @@ function useChat({
|
|
681
671
|
...body,
|
682
672
|
...options == null ? void 0 : options.body
|
683
673
|
},
|
674
|
+
streamMode,
|
684
675
|
headers: {
|
685
676
|
...headers,
|
686
677
|
...options == null ? void 0 : options.headers
|
@@ -693,15 +684,12 @@ function useChat({
|
|
693
684
|
setStreamData([...existingData, ...data2 != null ? data2 : []]);
|
694
685
|
},
|
695
686
|
onFinish,
|
696
|
-
appendMessage(message) {
|
697
|
-
mutate([...chatRequest.messages, message]);
|
698
|
-
},
|
699
687
|
restoreMessagesOnFailure() {
|
700
688
|
if (previousMessages.status === "success") {
|
701
689
|
mutate(previousMessages.data);
|
702
690
|
}
|
703
691
|
},
|
704
|
-
generateId
|
692
|
+
generateId: generateId2
|
705
693
|
});
|
706
694
|
},
|
707
695
|
experimental_onFunctionCall,
|
@@ -727,7 +715,7 @@ function useChat({
|
|
727
715
|
const append = async (message, options) => {
|
728
716
|
var _a;
|
729
717
|
if (!message.id) {
|
730
|
-
message.id =
|
718
|
+
message.id = generateId2();
|
731
719
|
}
|
732
720
|
return triggerRequest(
|
733
721
|
((_a = messages()) != null ? _a : []).concat(message),
|
@@ -796,6 +784,7 @@ async function callCompletionApi({
|
|
796
784
|
credentials,
|
797
785
|
headers,
|
798
786
|
body,
|
787
|
+
streamMode = "stream-data",
|
799
788
|
setCompletion,
|
800
789
|
setLoading,
|
801
790
|
setError,
|
@@ -843,36 +832,44 @@ async function callCompletionApi({
|
|
843
832
|
}
|
844
833
|
let result = "";
|
845
834
|
const reader = res.body.getReader();
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
case "text": {
|
853
|
-
result += value;
|
854
|
-
setCompletion(result);
|
835
|
+
switch (streamMode) {
|
836
|
+
case "text": {
|
837
|
+
const decoder = createChunkDecoder();
|
838
|
+
while (true) {
|
839
|
+
const { done, value } = await reader.read();
|
840
|
+
if (done) {
|
855
841
|
break;
|
856
842
|
}
|
857
|
-
|
858
|
-
|
843
|
+
result += decoder(value);
|
844
|
+
setCompletion(result);
|
845
|
+
if (abortController === null) {
|
846
|
+
reader.cancel();
|
859
847
|
break;
|
860
848
|
}
|
861
849
|
}
|
850
|
+
break;
|
862
851
|
}
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
852
|
+
case "stream-data": {
|
853
|
+
for await (const { type, value } of readDataStream(reader, {
|
854
|
+
isAborted: () => abortController === null
|
855
|
+
})) {
|
856
|
+
switch (type) {
|
857
|
+
case "text": {
|
858
|
+
result += value;
|
859
|
+
setCompletion(result);
|
860
|
+
break;
|
861
|
+
}
|
862
|
+
case "data": {
|
863
|
+
onData == null ? void 0 : onData(value);
|
864
|
+
break;
|
865
|
+
}
|
866
|
+
}
|
875
867
|
}
|
868
|
+
break;
|
869
|
+
}
|
870
|
+
default: {
|
871
|
+
const exhaustiveCheck = streamMode;
|
872
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
876
873
|
}
|
877
874
|
}
|
878
875
|
if (onFinish) {
|
@@ -913,6 +910,7 @@ function useCompletion({
|
|
913
910
|
credentials,
|
914
911
|
headers,
|
915
912
|
body,
|
913
|
+
streamMode,
|
916
914
|
onResponse,
|
917
915
|
onFinish,
|
918
916
|
onError
|
@@ -951,6 +949,7 @@ function useCompletion({
|
|
951
949
|
...body,
|
952
950
|
...options == null ? void 0 : options.body
|
953
951
|
},
|
952
|
+
streamMode,
|
954
953
|
setCompletion: mutate,
|
955
954
|
setLoading: setIsLoading,
|
956
955
|
setError,
|