ai 3.1.6 → 3.1.8
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 +170 -34
- package/dist/index.d.ts +170 -34
- package/dist/index.js +298 -88
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +275 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/prompts/dist/index.d.mts +53 -0
- package/prompts/dist/index.d.ts +53 -0
- package/react/dist/index.d.mts +59 -1
- package/react/dist/index.d.ts +59 -1
- package/react/dist/index.js +119 -20
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +119 -20
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +102 -66
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +53 -0
- package/solid/dist/index.d.ts +53 -0
- package/solid/dist/index.js +81 -11
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +81 -11
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +53 -0
- package/svelte/dist/index.d.ts +53 -0
- package/svelte/dist/index.js +81 -11
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +81 -11
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +53 -0
- package/vue/dist/index.d.ts +53 -0
- package/vue/dist/index.js +81 -11
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +81 -11
- package/vue/dist/index.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -4,6 +4,96 @@ var __export = (target, all) => {
|
|
4
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
5
5
|
};
|
6
6
|
|
7
|
+
// core/util/retry-with-exponential-backoff.ts
|
8
|
+
import { APICallError, RetryError } from "@ai-sdk/provider";
|
9
|
+
import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
10
|
+
|
11
|
+
// core/util/delay.ts
|
12
|
+
async function delay(delayInMs) {
|
13
|
+
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
14
|
+
}
|
15
|
+
|
16
|
+
// core/util/retry-with-exponential-backoff.ts
|
17
|
+
var retryWithExponentialBackoff = ({
|
18
|
+
maxRetries = 2,
|
19
|
+
initialDelayInMs = 2e3,
|
20
|
+
backoffFactor = 2
|
21
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
22
|
+
maxRetries,
|
23
|
+
delayInMs: initialDelayInMs,
|
24
|
+
backoffFactor
|
25
|
+
});
|
26
|
+
async function _retryWithExponentialBackoff(f, {
|
27
|
+
maxRetries,
|
28
|
+
delayInMs,
|
29
|
+
backoffFactor
|
30
|
+
}, errors = []) {
|
31
|
+
try {
|
32
|
+
return await f();
|
33
|
+
} catch (error) {
|
34
|
+
if (isAbortError(error)) {
|
35
|
+
throw error;
|
36
|
+
}
|
37
|
+
if (maxRetries === 0) {
|
38
|
+
throw error;
|
39
|
+
}
|
40
|
+
const errorMessage = getErrorMessage(error);
|
41
|
+
const newErrors = [...errors, error];
|
42
|
+
const tryNumber = newErrors.length;
|
43
|
+
if (tryNumber > maxRetries) {
|
44
|
+
throw new RetryError({
|
45
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
46
|
+
reason: "maxRetriesExceeded",
|
47
|
+
errors: newErrors
|
48
|
+
});
|
49
|
+
}
|
50
|
+
if (error instanceof Error && APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
51
|
+
await delay(delayInMs);
|
52
|
+
return _retryWithExponentialBackoff(
|
53
|
+
f,
|
54
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
55
|
+
newErrors
|
56
|
+
);
|
57
|
+
}
|
58
|
+
if (tryNumber === 1) {
|
59
|
+
throw error;
|
60
|
+
}
|
61
|
+
throw new RetryError({
|
62
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
63
|
+
reason: "errorNotRetryable",
|
64
|
+
errors: newErrors
|
65
|
+
});
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
// core/embed/embed.ts
|
70
|
+
async function embed({
|
71
|
+
model,
|
72
|
+
value,
|
73
|
+
maxRetries,
|
74
|
+
abortSignal
|
75
|
+
}) {
|
76
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
77
|
+
const modelResponse = await retry(
|
78
|
+
() => model.doEmbed({
|
79
|
+
values: [value],
|
80
|
+
abortSignal
|
81
|
+
})
|
82
|
+
);
|
83
|
+
return new EmbedResult({
|
84
|
+
value,
|
85
|
+
embedding: modelResponse.embeddings[0],
|
86
|
+
rawResponse: modelResponse.rawResponse
|
87
|
+
});
|
88
|
+
}
|
89
|
+
var EmbedResult = class {
|
90
|
+
constructor(options) {
|
91
|
+
this.value = options.value;
|
92
|
+
this.embedding = options.embedding;
|
93
|
+
this.rawResponse = options.rawResponse;
|
94
|
+
}
|
95
|
+
};
|
96
|
+
|
7
97
|
// core/generate-object/generate-object.ts
|
8
98
|
import { NoObjectGeneratedError } from "@ai-sdk/provider";
|
9
99
|
import { safeParseJSON } from "@ai-sdk/provider-utils";
|
@@ -287,68 +377,6 @@ function convertZodToJSONSchema(zodSchema) {
|
|
287
377
|
return zodToJsonSchema(zodSchema);
|
288
378
|
}
|
289
379
|
|
290
|
-
// core/util/retry-with-exponential-backoff.ts
|
291
|
-
import { APICallError, RetryError } from "@ai-sdk/provider";
|
292
|
-
import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
293
|
-
|
294
|
-
// core/util/delay.ts
|
295
|
-
async function delay(delayInMs) {
|
296
|
-
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
297
|
-
}
|
298
|
-
|
299
|
-
// core/util/retry-with-exponential-backoff.ts
|
300
|
-
var retryWithExponentialBackoff = ({
|
301
|
-
maxRetries = 2,
|
302
|
-
initialDelayInMs = 2e3,
|
303
|
-
backoffFactor = 2
|
304
|
-
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
305
|
-
maxRetries,
|
306
|
-
delayInMs: initialDelayInMs,
|
307
|
-
backoffFactor
|
308
|
-
});
|
309
|
-
async function _retryWithExponentialBackoff(f, {
|
310
|
-
maxRetries,
|
311
|
-
delayInMs,
|
312
|
-
backoffFactor
|
313
|
-
}, errors = []) {
|
314
|
-
try {
|
315
|
-
return await f();
|
316
|
-
} catch (error) {
|
317
|
-
if (isAbortError(error)) {
|
318
|
-
throw error;
|
319
|
-
}
|
320
|
-
if (maxRetries === 0) {
|
321
|
-
throw error;
|
322
|
-
}
|
323
|
-
const errorMessage = getErrorMessage(error);
|
324
|
-
const newErrors = [...errors, error];
|
325
|
-
const tryNumber = newErrors.length;
|
326
|
-
if (tryNumber > maxRetries) {
|
327
|
-
throw new RetryError({
|
328
|
-
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
329
|
-
reason: "maxRetriesExceeded",
|
330
|
-
errors: newErrors
|
331
|
-
});
|
332
|
-
}
|
333
|
-
if (error instanceof Error && APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
334
|
-
await delay(delayInMs);
|
335
|
-
return _retryWithExponentialBackoff(
|
336
|
-
f,
|
337
|
-
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
338
|
-
newErrors
|
339
|
-
);
|
340
|
-
}
|
341
|
-
if (tryNumber === 1) {
|
342
|
-
throw error;
|
343
|
-
}
|
344
|
-
throw new RetryError({
|
345
|
-
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
346
|
-
reason: "errorNotRetryable",
|
347
|
-
errors: newErrors
|
348
|
-
});
|
349
|
-
}
|
350
|
-
}
|
351
|
-
|
352
380
|
// core/generate-object/inject-json-schema-into-system.ts
|
353
381
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
354
382
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
@@ -1491,8 +1519,65 @@ var StreamTextResult = class {
|
|
1491
1519
|
|
1492
1520
|
@returns an `AIStream` object.
|
1493
1521
|
*/
|
1494
|
-
toAIStream(callbacks) {
|
1495
|
-
|
1522
|
+
toAIStream(callbacks = {}) {
|
1523
|
+
let aggregatedResponse = "";
|
1524
|
+
const callbackTransformer = new TransformStream({
|
1525
|
+
async start() {
|
1526
|
+
if (callbacks.onStart)
|
1527
|
+
await callbacks.onStart();
|
1528
|
+
},
|
1529
|
+
async transform(chunk, controller) {
|
1530
|
+
controller.enqueue(chunk);
|
1531
|
+
if (chunk.type === "text-delta") {
|
1532
|
+
const textDelta = chunk.textDelta;
|
1533
|
+
aggregatedResponse += textDelta;
|
1534
|
+
if (callbacks.onToken)
|
1535
|
+
await callbacks.onToken(textDelta);
|
1536
|
+
if (callbacks.onText)
|
1537
|
+
await callbacks.onText(textDelta);
|
1538
|
+
}
|
1539
|
+
},
|
1540
|
+
async flush() {
|
1541
|
+
if (callbacks.onCompletion)
|
1542
|
+
await callbacks.onCompletion(aggregatedResponse);
|
1543
|
+
if (callbacks.onFinal)
|
1544
|
+
await callbacks.onFinal(aggregatedResponse);
|
1545
|
+
}
|
1546
|
+
});
|
1547
|
+
const streamDataTransformer = new TransformStream({
|
1548
|
+
transform: async (chunk, controller) => {
|
1549
|
+
switch (chunk.type) {
|
1550
|
+
case "text-delta":
|
1551
|
+
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1552
|
+
break;
|
1553
|
+
case "tool-call":
|
1554
|
+
controller.enqueue(
|
1555
|
+
formatStreamPart("tool_call", {
|
1556
|
+
toolCallId: chunk.toolCallId,
|
1557
|
+
toolName: chunk.toolName,
|
1558
|
+
args: chunk.args
|
1559
|
+
})
|
1560
|
+
);
|
1561
|
+
break;
|
1562
|
+
case "tool-result":
|
1563
|
+
controller.enqueue(
|
1564
|
+
formatStreamPart("tool_result", {
|
1565
|
+
toolCallId: chunk.toolCallId,
|
1566
|
+
toolName: chunk.toolName,
|
1567
|
+
args: chunk.args,
|
1568
|
+
result: chunk.result
|
1569
|
+
})
|
1570
|
+
);
|
1571
|
+
break;
|
1572
|
+
case "error":
|
1573
|
+
controller.enqueue(
|
1574
|
+
formatStreamPart("error", JSON.stringify(chunk.error))
|
1575
|
+
);
|
1576
|
+
break;
|
1577
|
+
}
|
1578
|
+
}
|
1579
|
+
});
|
1580
|
+
return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
|
1496
1581
|
}
|
1497
1582
|
/**
|
1498
1583
|
Writes stream data output to a Node.js response-like object.
|
@@ -1598,6 +1683,55 @@ var StreamTextResult = class {
|
|
1598
1683
|
};
|
1599
1684
|
var experimental_streamText = streamText;
|
1600
1685
|
|
1686
|
+
// core/prompt/convert-to-core-messages.ts
|
1687
|
+
function convertToCoreMessages(messages) {
|
1688
|
+
const coreMessages = [];
|
1689
|
+
for (const { role, content, toolInvocations } of messages) {
|
1690
|
+
switch (role) {
|
1691
|
+
case "user": {
|
1692
|
+
coreMessages.push({ role: "user", content });
|
1693
|
+
break;
|
1694
|
+
}
|
1695
|
+
case "assistant": {
|
1696
|
+
if (toolInvocations == null) {
|
1697
|
+
coreMessages.push({ role: "assistant", content });
|
1698
|
+
break;
|
1699
|
+
}
|
1700
|
+
coreMessages.push({
|
1701
|
+
role: "assistant",
|
1702
|
+
content: [
|
1703
|
+
{ type: "text", text: content },
|
1704
|
+
...toolInvocations.map(({ toolCallId, toolName, args }) => ({
|
1705
|
+
type: "tool-call",
|
1706
|
+
toolCallId,
|
1707
|
+
toolName,
|
1708
|
+
args
|
1709
|
+
}))
|
1710
|
+
]
|
1711
|
+
});
|
1712
|
+
coreMessages.push({
|
1713
|
+
role: "tool",
|
1714
|
+
content: toolInvocations.map(
|
1715
|
+
({ toolCallId, toolName, args, result }) => ({
|
1716
|
+
type: "tool-result",
|
1717
|
+
toolCallId,
|
1718
|
+
toolName,
|
1719
|
+
args,
|
1720
|
+
result
|
1721
|
+
})
|
1722
|
+
)
|
1723
|
+
});
|
1724
|
+
break;
|
1725
|
+
}
|
1726
|
+
default: {
|
1727
|
+
const _exhaustiveCheck = role;
|
1728
|
+
throw new Error(`Unhandled role: ${_exhaustiveCheck}`);
|
1729
|
+
}
|
1730
|
+
}
|
1731
|
+
}
|
1732
|
+
return coreMessages;
|
1733
|
+
}
|
1734
|
+
|
1601
1735
|
// core/tool/tool.ts
|
1602
1736
|
function tool(tool2) {
|
1603
1737
|
return tool2;
|
@@ -1719,7 +1853,7 @@ var dataMessageStreamPart = {
|
|
1719
1853
|
};
|
1720
1854
|
}
|
1721
1855
|
};
|
1722
|
-
var
|
1856
|
+
var toolCallsStreamPart = {
|
1723
1857
|
code: "7",
|
1724
1858
|
name: "tool_calls",
|
1725
1859
|
parse: (value) => {
|
@@ -1746,6 +1880,36 @@ var messageAnnotationsStreamPart = {
|
|
1746
1880
|
return { type: "message_annotations", value };
|
1747
1881
|
}
|
1748
1882
|
};
|
1883
|
+
var toolCallStreamPart = {
|
1884
|
+
code: "9",
|
1885
|
+
name: "tool_call",
|
1886
|
+
parse: (value) => {
|
1887
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
1888
|
+
throw new Error(
|
1889
|
+
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
1890
|
+
);
|
1891
|
+
}
|
1892
|
+
return {
|
1893
|
+
type: "tool_call",
|
1894
|
+
value
|
1895
|
+
};
|
1896
|
+
}
|
1897
|
+
};
|
1898
|
+
var toolResultStreamPart = {
|
1899
|
+
code: "a",
|
1900
|
+
name: "tool_result",
|
1901
|
+
parse: (value) => {
|
1902
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
|
1903
|
+
throw new Error(
|
1904
|
+
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
1905
|
+
);
|
1906
|
+
}
|
1907
|
+
return {
|
1908
|
+
type: "tool_result",
|
1909
|
+
value
|
1910
|
+
};
|
1911
|
+
}
|
1912
|
+
};
|
1749
1913
|
var streamParts = [
|
1750
1914
|
textStreamPart,
|
1751
1915
|
functionCallStreamPart,
|
@@ -1754,8 +1918,10 @@ var streamParts = [
|
|
1754
1918
|
assistantMessageStreamPart,
|
1755
1919
|
assistantControlDataStreamPart,
|
1756
1920
|
dataMessageStreamPart,
|
1921
|
+
toolCallsStreamPart,
|
1922
|
+
messageAnnotationsStreamPart,
|
1757
1923
|
toolCallStreamPart,
|
1758
|
-
|
1924
|
+
toolResultStreamPart
|
1759
1925
|
];
|
1760
1926
|
var streamPartsByCode = {
|
1761
1927
|
[textStreamPart.code]: textStreamPart,
|
@@ -1765,8 +1931,10 @@ var streamPartsByCode = {
|
|
1765
1931
|
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
1766
1932
|
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
1767
1933
|
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
1934
|
+
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
1935
|
+
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
1768
1936
|
[toolCallStreamPart.code]: toolCallStreamPart,
|
1769
|
-
[
|
1937
|
+
[toolResultStreamPart.code]: toolResultStreamPart
|
1770
1938
|
};
|
1771
1939
|
var StreamStringPrefixes = {
|
1772
1940
|
[textStreamPart.name]: textStreamPart.code,
|
@@ -1776,8 +1944,10 @@ var StreamStringPrefixes = {
|
|
1776
1944
|
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
1777
1945
|
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
1778
1946
|
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
1947
|
+
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
1948
|
+
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
1779
1949
|
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
1780
|
-
[
|
1950
|
+
[toolResultStreamPart.name]: toolResultStreamPart.code
|
1781
1951
|
};
|
1782
1952
|
var validCodes = streamParts.map((part) => part.code);
|
1783
1953
|
var parseStreamPart = (line) => {
|
@@ -2882,6 +3052,40 @@ async function parseComplexResponse({
|
|
2882
3052
|
};
|
2883
3053
|
}
|
2884
3054
|
}
|
3055
|
+
if (type === "tool_call") {
|
3056
|
+
if (prefixMap.text == null) {
|
3057
|
+
prefixMap.text = {
|
3058
|
+
id: generateId2(),
|
3059
|
+
role: "assistant",
|
3060
|
+
content: "",
|
3061
|
+
createdAt
|
3062
|
+
};
|
3063
|
+
}
|
3064
|
+
if (prefixMap.text.toolInvocations == null) {
|
3065
|
+
prefixMap.text.toolInvocations = [];
|
3066
|
+
}
|
3067
|
+
prefixMap.text.toolInvocations.push(value);
|
3068
|
+
} else if (type === "tool_result") {
|
3069
|
+
if (prefixMap.text == null) {
|
3070
|
+
prefixMap.text = {
|
3071
|
+
id: generateId2(),
|
3072
|
+
role: "assistant",
|
3073
|
+
content: "",
|
3074
|
+
createdAt
|
3075
|
+
};
|
3076
|
+
}
|
3077
|
+
if (prefixMap.text.toolInvocations == null) {
|
3078
|
+
prefixMap.text.toolInvocations = [];
|
3079
|
+
}
|
3080
|
+
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
3081
|
+
(invocation) => invocation.toolCallId === value.toolCallId
|
3082
|
+
);
|
3083
|
+
if (toolInvocationIndex !== -1) {
|
3084
|
+
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
3085
|
+
} else {
|
3086
|
+
prefixMap.text.toolInvocations.push(value);
|
3087
|
+
}
|
3088
|
+
}
|
2885
3089
|
let functionCallMessage = null;
|
2886
3090
|
if (type === "function_call") {
|
2887
3091
|
prefixMap["function_call"] = {
|
@@ -3044,6 +3248,7 @@ export {
|
|
3044
3248
|
AnthropicStream,
|
3045
3249
|
AssistantResponse,
|
3046
3250
|
CohereStream,
|
3251
|
+
EmbedResult,
|
3047
3252
|
EmptyResponseBodyError,
|
3048
3253
|
GenerateObjectResult,
|
3049
3254
|
GenerateTextResult,
|
@@ -3075,10 +3280,12 @@ export {
|
|
3075
3280
|
UnsupportedJSONSchemaError,
|
3076
3281
|
convertDataContentToBase64String,
|
3077
3282
|
convertDataContentToUint8Array,
|
3283
|
+
convertToCoreMessages,
|
3078
3284
|
createCallbacksTransformer,
|
3079
3285
|
createChunkDecoder,
|
3080
3286
|
createEventStreamTransformer,
|
3081
3287
|
createStreamDataTransformer,
|
3288
|
+
embed,
|
3082
3289
|
experimental_AssistantResponse,
|
3083
3290
|
experimental_StreamData,
|
3084
3291
|
experimental_StreamingReactResponse,
|