ai 7.0.153 → 7.0.155
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/CHANGELOG.md +25 -0
- package/dist/index.d.ts +224 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1071 -893
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +54 -7
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +29 -399
- package/dist/internal/index.js.map +1 -1
- package/docs/02-getting-started/02-nextjs-app-router.mdx +2 -2
- package/docs/02-getting-started/08-angular.mdx +327 -0
- package/docs/02-getting-started/{03-nextjs-pages-router.mdx → 10-nextjs-pages-router.mdx} +12 -20
- package/docs/02-getting-started/index.mdx +2 -0
- package/docs/03-agents/01-overview.mdx +1 -0
- package/docs/03-agents/04-loop-control.mdx +117 -0
- package/docs/03-agents/06-tool-approvals.mdx +1 -0
- package/docs/03-agents/09-chat-platforms.mdx +164 -0
- package/docs/03-agents/index.mdx +6 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +7 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +16 -3
- package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +10 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +86 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +35 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +7 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +25 -10
- package/package.json +11 -11
- package/src/agent/create-agent-ui-stream-response.ts +3 -0
- package/src/agent/create-agent-ui-stream.ts +16 -0
- package/src/agent/pipe-agent-ui-stream-to-response.ts +3 -0
- package/src/agent/tool-loop-agent-settings.ts +37 -1
- package/src/agent/tool-loop-agent.ts +43 -7
- package/src/batch/batch.ts +0 -8
- package/src/decide/validate-decision.ts +39 -64
- package/src/generate-object/validate-object-generation-input.ts +1 -39
- package/src/generate-speech/stream-speech.ts +6 -18
- package/src/generate-text/generate-text-events.ts +37 -0
- package/src/generate-text/generate-text.ts +521 -373
- package/src/generate-text/index.ts +8 -0
- package/src/generate-text/prepare-step.ts +12 -0
- package/src/generate-text/restricted-telemetry-dispatcher.ts +31 -18
- package/src/generate-text/smooth-stream.ts +2 -14
- package/src/generate-text/steering.ts +285 -0
- package/src/generate-text/step-result.ts +18 -0
- package/src/generate-text/stream-text.ts +664 -473
- package/src/generate-text/tool-caller-configuration.ts +2 -26
- package/src/prompt/prepare-language-model-call-options.ts +0 -50
- package/src/prompt/standardize-prompt.ts +9 -58
- package/src/telemetry/create-telemetry-dispatcher.ts +3 -0
- package/src/telemetry/telemetry.ts +15 -1
- package/src/test/mock-sandbox.ts +5 -11
- package/src/util/is-caused-by-abort.ts +20 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.js";
|
|
2
2
|
import { GatewayAuthenticationError, GatewayError, createGateway, gateway, gateway as gateway$1 } from "@ai-sdk/gateway";
|
|
3
3
|
import { DEFAULT_MAX_DOWNLOAD_SIZE, DelayedPromise, DownloadError, DownloadError as DownloadError$1, EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, asArray, asSchema, asSchema as asSchema$1, cancelResponseBody, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertUint8ArrayToBase64, createIdGenerator, createIdGenerator as createIdGenerator$1, delay, detectMediaType, dynamicTool, executeTool, experimental_getToolCaller, experimental_toolCaller, fetchUntrustedUrl, filterNullable, generateId, generateId as generateId$1, getErrorMessage, getRuntimeEnvironmentUserAgent, getWebSocketConstructor, isAbortError, isBuffer, isExecutableTool, isFullMediaType, isNonNullable, isProviderReference, isProviderStreamError, isUrlSupported, jsonSchema, jsonSchema as jsonSchema$1, lazySchema, normalizeHeaders, parseJsonEventStream, parseJsonEventStream as parseJsonEventStream$1, readResponseWithSizeLimit, readWebSocketMessageText, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, toWebSocketUrl, tool, tool as tool$1, validateTypes, waitForWebSocketBufferDrain, withUserAgentSuffix, zodSchema, zodSchema as zodSchema$1 } from "@ai-sdk/provider-utils";
|
|
4
|
-
import { AISDKError, AISDKError as AISDKError$1, APICallError, APICallError as APICallError$1, EmptyResponseBodyError, EmptyResponseBodyError as EmptyResponseBodyError$1, Experimental_DecisionRefusalError, Experimental_DecisionRefusalError as Experimental_DecisionRefusalError$1, Experimental_DecisionUnsupportedQuestionTypeError, Experimental_DecisionUnsupportedQuestionTypeError as Experimental_DecisionUnsupportedQuestionTypeError$1,
|
|
4
|
+
import { AISDKError, AISDKError as AISDKError$1, APICallError, APICallError as APICallError$1, EmptyResponseBodyError, EmptyResponseBodyError as EmptyResponseBodyError$1, Experimental_DecisionRefusalError, Experimental_DecisionRefusalError as Experimental_DecisionRefusalError$1, Experimental_DecisionUnsupportedQuestionTypeError, Experimental_DecisionUnsupportedQuestionTypeError as Experimental_DecisionUnsupportedQuestionTypeError$1, InvalidPromptError, InvalidPromptError as InvalidPromptError$1, InvalidResponseDataError, InvalidResponseDataError as InvalidResponseDataError$1, JSONParseError, JSONParseError as JSONParseError$1, LoadAPIKeyError, LoadSettingError, NoContentGeneratedError, NoSuchModelError, NoSuchModelError as NoSuchModelError$1, NoSuchProviderReferenceError, TooManyEmbeddingValuesForCallError, TypeValidationError, TypeValidationError as TypeValidationError$1, UnsupportedFunctionalityError, UnsupportedFunctionalityError as UnsupportedFunctionalityError$1, getErrorMessage as getErrorMessage$1, isJSONArray, isJSONObject } from "@ai-sdk/provider";
|
|
5
5
|
import { array, boolean, custom, discriminatedUnion, enum as enum$1, instanceof as instanceof$1, lazy, literal, looseObject, never, null as null$1, number, object, record, string, union, unknown } from "zod/v4";
|
|
6
6
|
//#region src/error/invalid-argument-error.ts
|
|
7
7
|
const name$23 = "AI_InvalidArgumentError";
|
|
@@ -1175,7 +1175,7 @@ function cloneValue(value) {
|
|
|
1175
1175
|
}
|
|
1176
1176
|
//#endregion
|
|
1177
1177
|
//#region src/version.ts
|
|
1178
|
-
const VERSION = "7.0.
|
|
1178
|
+
const VERSION = "7.0.155";
|
|
1179
1179
|
//#endregion
|
|
1180
1180
|
//#region src/util/download/download.ts
|
|
1181
1181
|
/**
|
|
@@ -1928,41 +1928,6 @@ function prepareLanguageModelCallOptions({ maxOutputTokens, temperature, topP, t
|
|
|
1928
1928
|
message: "maxOutputTokens must be >= 1"
|
|
1929
1929
|
});
|
|
1930
1930
|
}
|
|
1931
|
-
if (temperature != null) {
|
|
1932
|
-
if (typeof temperature !== "number") throw new InvalidArgumentError({
|
|
1933
|
-
parameter: "temperature",
|
|
1934
|
-
value: temperature,
|
|
1935
|
-
message: "temperature must be a number"
|
|
1936
|
-
});
|
|
1937
|
-
}
|
|
1938
|
-
if (topP != null) {
|
|
1939
|
-
if (typeof topP !== "number") throw new InvalidArgumentError({
|
|
1940
|
-
parameter: "topP",
|
|
1941
|
-
value: topP,
|
|
1942
|
-
message: "topP must be a number"
|
|
1943
|
-
});
|
|
1944
|
-
}
|
|
1945
|
-
if (topK != null) {
|
|
1946
|
-
if (typeof topK !== "number") throw new InvalidArgumentError({
|
|
1947
|
-
parameter: "topK",
|
|
1948
|
-
value: topK,
|
|
1949
|
-
message: "topK must be a number"
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1952
|
-
if (presencePenalty != null) {
|
|
1953
|
-
if (typeof presencePenalty !== "number") throw new InvalidArgumentError({
|
|
1954
|
-
parameter: "presencePenalty",
|
|
1955
|
-
value: presencePenalty,
|
|
1956
|
-
message: "presencePenalty must be a number"
|
|
1957
|
-
});
|
|
1958
|
-
}
|
|
1959
|
-
if (frequencyPenalty != null) {
|
|
1960
|
-
if (typeof frequencyPenalty !== "number") throw new InvalidArgumentError({
|
|
1961
|
-
parameter: "frequencyPenalty",
|
|
1962
|
-
value: frequencyPenalty,
|
|
1963
|
-
message: "frequencyPenalty must be a number"
|
|
1964
|
-
});
|
|
1965
|
-
}
|
|
1966
1931
|
if (seed != null) {
|
|
1967
1932
|
if (!Number.isInteger(seed)) throw new InvalidArgumentError({
|
|
1968
1933
|
parameter: "seed",
|
|
@@ -2105,708 +2070,368 @@ function getToolTimeoutMs(timeout, toolName) {
|
|
|
2105
2070
|
return timeout.tools?.[`${toolName}Ms`] ?? timeout.toolMs;
|
|
2106
2071
|
}
|
|
2107
2072
|
//#endregion
|
|
2108
|
-
//#region src/
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
null:
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
};
|
|
2073
|
+
//#region src/prompt/standardize-prompt.ts
|
|
2074
|
+
/**
|
|
2075
|
+
* Converts a prompt input into a standardized prompt with model messages.
|
|
2076
|
+
*
|
|
2077
|
+
* @param prompt - The prompt definition to standardize.
|
|
2078
|
+
* Set `allowSystemInMessages` to true to allow system messages in the
|
|
2079
|
+
* `prompt` or `messages` fields. System messages in the `instructions`
|
|
2080
|
+
* option are always allowed.
|
|
2081
|
+
* @returns The standardized prompt.
|
|
2082
|
+
* @throws {InvalidPromptError} When the prompt is invalid.
|
|
2083
|
+
*/
|
|
2084
|
+
async function standardizePrompt({ allowSystemInMessages = false, system, instructions = system, prompt, messages }) {
|
|
2085
|
+
messages = prompt == null ? messages : typeof prompt === "string" ? [{
|
|
2086
|
+
role: "user",
|
|
2087
|
+
content: prompt
|
|
2088
|
+
}] : prompt;
|
|
2089
|
+
if (messages.length === 0) throw new InvalidPromptError$1({
|
|
2090
|
+
prompt,
|
|
2091
|
+
message: "messages must not be empty"
|
|
2092
|
+
});
|
|
2093
|
+
if (!allowSystemInMessages && messages.some((message) => message.role === "system")) throw new InvalidPromptError$1({
|
|
2094
|
+
prompt,
|
|
2095
|
+
message: "System messages are not allowed in the prompt or messages fields. Use the instructions option instead."
|
|
2096
|
+
});
|
|
2097
|
+
return {
|
|
2098
|
+
messages,
|
|
2099
|
+
instructions
|
|
2100
|
+
};
|
|
2101
|
+
}
|
|
2128
2102
|
//#endregion
|
|
2129
|
-
//#region src/
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2103
|
+
//#region src/prompt/wrap-gateway-error.ts
|
|
2104
|
+
function wrapGatewayError(error) {
|
|
2105
|
+
if (!GatewayAuthenticationError.isInstance(error)) return error;
|
|
2106
|
+
const isProductionEnv = process?.env.NODE_ENV === "production";
|
|
2107
|
+
const moreInfoURL = "https://ai-sdk.dev/unauthenticated-ai-gateway";
|
|
2108
|
+
if (isProductionEnv) return new AISDKError$1({
|
|
2109
|
+
name: "GatewayError",
|
|
2110
|
+
message: `Unauthenticated. Configure AI_GATEWAY_API_KEY or use a provider module. Learn more: ${moreInfoURL}`
|
|
2111
|
+
});
|
|
2112
|
+
return Object.assign(/* @__PURE__ */ new Error(`\u001b[1m\u001b[31mUnauthenticated request to AI Gateway.\u001b[0m
|
|
2113
|
+
|
|
2114
|
+
To authenticate, set the \u001b[33mAI_GATEWAY_API_KEY\u001b[0m environment variable with your API key.
|
|
2115
|
+
|
|
2116
|
+
Alternatively, you can use a provider module instead of the AI Gateway.
|
|
2117
|
+
|
|
2118
|
+
Learn more: \u001b[34m${moreInfoURL}\u001b[0m
|
|
2119
|
+
|
|
2120
|
+
`), { name: "GatewayAuthenticationError" });
|
|
2121
|
+
}
|
|
2138
2122
|
//#endregion
|
|
2139
|
-
//#region src/types/
|
|
2140
|
-
|
|
2123
|
+
//#region src/types/usage.ts
|
|
2124
|
+
function asLanguageModelUsage(usage) {
|
|
2125
|
+
return {
|
|
2126
|
+
inputTokens: usage.inputTokens.total,
|
|
2127
|
+
inputTokenDetails: {
|
|
2128
|
+
noCacheTokens: usage.inputTokens.noCache,
|
|
2129
|
+
cacheReadTokens: usage.inputTokens.cacheRead,
|
|
2130
|
+
cacheWriteTokens: usage.inputTokens.cacheWrite
|
|
2131
|
+
},
|
|
2132
|
+
outputTokens: usage.outputTokens.total,
|
|
2133
|
+
outputTokenDetails: {
|
|
2134
|
+
textTokens: usage.outputTokens.text,
|
|
2135
|
+
reasoningTokens: usage.outputTokens.reasoning
|
|
2136
|
+
},
|
|
2137
|
+
totalTokens: addTokenCounts(usage.inputTokens.total, usage.outputTokens.total),
|
|
2138
|
+
raw: usage.raw
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
function createNullLanguageModelUsage() {
|
|
2142
|
+
return {
|
|
2143
|
+
inputTokens: void 0,
|
|
2144
|
+
inputTokenDetails: {
|
|
2145
|
+
noCacheTokens: void 0,
|
|
2146
|
+
cacheReadTokens: void 0,
|
|
2147
|
+
cacheWriteTokens: void 0
|
|
2148
|
+
},
|
|
2149
|
+
outputTokens: void 0,
|
|
2150
|
+
outputTokenDetails: {
|
|
2151
|
+
textTokens: void 0,
|
|
2152
|
+
reasoningTokens: void 0
|
|
2153
|
+
},
|
|
2154
|
+
totalTokens: void 0,
|
|
2155
|
+
raw: void 0
|
|
2156
|
+
};
|
|
2157
|
+
}
|
|
2158
|
+
function addLanguageModelUsage(usage1, usage2) {
|
|
2159
|
+
const raw = isNullLanguageModelUsage(usage1) ? usage2.raw : isNullLanguageModelUsage(usage2) ? usage1.raw : void 0;
|
|
2160
|
+
return {
|
|
2161
|
+
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2162
|
+
inputTokenDetails: {
|
|
2163
|
+
noCacheTokens: addTokenCounts(usage1.inputTokenDetails?.noCacheTokens, usage2.inputTokenDetails?.noCacheTokens),
|
|
2164
|
+
cacheReadTokens: addTokenCounts(usage1.inputTokenDetails?.cacheReadTokens, usage2.inputTokenDetails?.cacheReadTokens),
|
|
2165
|
+
cacheWriteTokens: addTokenCounts(usage1.inputTokenDetails?.cacheWriteTokens, usage2.inputTokenDetails?.cacheWriteTokens)
|
|
2166
|
+
},
|
|
2167
|
+
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
|
2168
|
+
outputTokenDetails: {
|
|
2169
|
+
textTokens: addTokenCounts(usage1.outputTokenDetails?.textTokens, usage2.outputTokenDetails?.textTokens),
|
|
2170
|
+
reasoningTokens: addTokenCounts(usage1.outputTokenDetails?.reasoningTokens, usage2.outputTokenDetails?.reasoningTokens)
|
|
2171
|
+
},
|
|
2172
|
+
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens),
|
|
2173
|
+
...raw == null ? {} : { raw }
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
function isNullLanguageModelUsage(usage) {
|
|
2177
|
+
return usage.inputTokens == null && usage.inputTokenDetails?.noCacheTokens == null && usage.inputTokenDetails?.cacheReadTokens == null && usage.inputTokenDetails?.cacheWriteTokens == null && usage.outputTokens == null && usage.outputTokenDetails?.textTokens == null && usage.outputTokenDetails?.reasoningTokens == null && usage.totalTokens == null && usage.raw == null;
|
|
2178
|
+
}
|
|
2179
|
+
function addTokenCounts(tokenCount1, tokenCount2) {
|
|
2180
|
+
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 ?? 0) + (tokenCount2 ?? 0);
|
|
2181
|
+
}
|
|
2182
|
+
function addImageModelUsage(usage1, usage2) {
|
|
2183
|
+
return {
|
|
2184
|
+
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2185
|
+
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
|
2186
|
+
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens)
|
|
2187
|
+
};
|
|
2188
|
+
}
|
|
2141
2189
|
//#endregion
|
|
2142
|
-
//#region src/
|
|
2143
|
-
const fileInlineDataSchema = z.union([
|
|
2144
|
-
z.string(),
|
|
2145
|
-
z.instanceof(Uint8Array),
|
|
2146
|
-
z.instanceof(ArrayBuffer),
|
|
2147
|
-
z.custom(isBuffer, { message: "Must be a Buffer" })
|
|
2148
|
-
]);
|
|
2149
|
-
const providerReferenceSchema$1 = z.record(z.string(), z.string());
|
|
2150
|
-
/**
|
|
2151
|
-
* @internal
|
|
2152
|
-
*/
|
|
2153
|
-
const textPartSchema = z.object({
|
|
2154
|
-
type: z.literal("text"),
|
|
2155
|
-
text: z.string(),
|
|
2156
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2157
|
-
});
|
|
2190
|
+
//#region src/util/get-own.ts
|
|
2158
2191
|
/**
|
|
2159
|
-
*
|
|
2160
|
-
*
|
|
2161
|
-
*
|
|
2192
|
+
* Reads a property by an untrusted key, ignoring inherited prototype members.
|
|
2193
|
+
*
|
|
2194
|
+
* Tool sets, tool contexts, and similar lookup objects are indexed by names
|
|
2195
|
+
* that can come from model output or client-supplied message history. Plain
|
|
2196
|
+
* bracket access (`obj[name]`) resolves names such as `constructor`,
|
|
2197
|
+
* `toString`, or `__proto__` to values on `Object.prototype`, which would slip
|
|
2198
|
+
* past the `== null` / `!value` guards that treat an unknown name as "not
|
|
2199
|
+
* present". This helper returns `undefined` unless `key` is an own property.
|
|
2162
2200
|
*/
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
providerReferenceSchema$1
|
|
2169
|
-
]),
|
|
2170
|
-
mediaType: z.string().optional(),
|
|
2171
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2172
|
-
});
|
|
2173
|
-
const taggedFileDataSchema = z.discriminatedUnion("type", [
|
|
2174
|
-
z.object({
|
|
2175
|
-
type: z.literal("data"),
|
|
2176
|
-
data: fileInlineDataSchema
|
|
2177
|
-
}),
|
|
2178
|
-
z.object({
|
|
2179
|
-
type: z.literal("url"),
|
|
2180
|
-
url: z.instanceof(URL)
|
|
2181
|
-
}),
|
|
2182
|
-
z.object({
|
|
2183
|
-
type: z.literal("reference"),
|
|
2184
|
-
reference: providerReferenceSchema$1
|
|
2185
|
-
}),
|
|
2186
|
-
z.object({
|
|
2187
|
-
type: z.literal("text"),
|
|
2188
|
-
text: z.string()
|
|
2189
|
-
})
|
|
2190
|
-
]);
|
|
2191
|
-
const taggedReasoningFileDataSchema = z.discriminatedUnion("type", [z.object({
|
|
2192
|
-
type: z.literal("data"),
|
|
2193
|
-
data: fileInlineDataSchema
|
|
2194
|
-
}), z.object({
|
|
2195
|
-
type: z.literal("url"),
|
|
2196
|
-
url: z.instanceof(URL)
|
|
2197
|
-
})]);
|
|
2201
|
+
function getOwn(obj, key) {
|
|
2202
|
+
return obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0;
|
|
2203
|
+
}
|
|
2204
|
+
//#endregion
|
|
2205
|
+
//#region src/util/merge-abort-signals.ts
|
|
2198
2206
|
/**
|
|
2199
|
-
*
|
|
2207
|
+
* Merges multiple abort sources into a single `AbortSignal`.
|
|
2208
|
+
* The returned signal will abort when any input signal aborts or when any
|
|
2209
|
+
* numeric timeout elapses, using the reason from the first source to abort.
|
|
2210
|
+
*
|
|
2211
|
+
* @param signals - Abort signals or timeout durations in milliseconds.
|
|
2212
|
+
* `null` and `undefined` values are ignored.
|
|
2213
|
+
* @returns An `AbortSignal` that aborts when any valid source aborts,
|
|
2214
|
+
* or `undefined` if no valid sources are provided.
|
|
2200
2215
|
*/
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
});
|
|
2216
|
+
function mergeAbortSignals(...signals) {
|
|
2217
|
+
const validSignals = filterNullable(...signals).map((signal) => typeof signal === "number" ? AbortSignal.timeout(signal) : signal);
|
|
2218
|
+
return validSignals.length === 0 ? void 0 : validSignals.length === 1 ? validSignals[0] : AbortSignal.any(validSignals);
|
|
2219
|
+
}
|
|
2220
|
+
//#endregion
|
|
2221
|
+
//#region src/util/now.ts
|
|
2222
|
+
function now() {
|
|
2223
|
+
return globalThis?.performance?.now() ?? Date.now();
|
|
2224
|
+
}
|
|
2225
|
+
//#endregion
|
|
2226
|
+
//#region src/util/notify.ts
|
|
2213
2227
|
/**
|
|
2214
|
-
*
|
|
2228
|
+
* Notifies all provided callbacks with the given event in parallel.
|
|
2229
|
+
* Errors in callbacks do not break the generation flow.
|
|
2215
2230
|
*/
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
}
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
* @internal
|
|
2244
|
-
*/
|
|
2245
|
-
const toolCallPartSchema = z.object({
|
|
2246
|
-
type: z.literal("tool-call"),
|
|
2247
|
-
toolCallId: z.string(),
|
|
2248
|
-
toolName: z.string(),
|
|
2249
|
-
input: z.unknown(),
|
|
2250
|
-
providerOptions: providerMetadataSchema.optional(),
|
|
2251
|
-
providerExecuted: z.boolean().optional()
|
|
2252
|
-
});
|
|
2231
|
+
async function notify(options) {
|
|
2232
|
+
await Promise.all(asArray(options.callbacks).map(async (callback) => {
|
|
2233
|
+
try {
|
|
2234
|
+
await callback?.(options.event);
|
|
2235
|
+
} catch {}
|
|
2236
|
+
}));
|
|
2237
|
+
}
|
|
2238
|
+
//#endregion
|
|
2239
|
+
//#region src/util/retry-with-exponential-backoff.ts
|
|
2240
|
+
function getRetryDelayInMs({ error, exponentialBackoffDelay }) {
|
|
2241
|
+
const headers = APICallError$1.isInstance(error) ? error.responseHeaders : APICallError$1.isInstance(error.cause) ? error.cause.responseHeaders : void 0;
|
|
2242
|
+
if (!headers) return exponentialBackoffDelay;
|
|
2243
|
+
let ms;
|
|
2244
|
+
const retryAfterMs = headers["retry-after-ms"];
|
|
2245
|
+
if (retryAfterMs) {
|
|
2246
|
+
const timeoutMs = parseFloat(retryAfterMs);
|
|
2247
|
+
if (!Number.isNaN(timeoutMs)) ms = timeoutMs;
|
|
2248
|
+
}
|
|
2249
|
+
const retryAfter = headers["retry-after"];
|
|
2250
|
+
if (retryAfter && ms === void 0) {
|
|
2251
|
+
const timeoutSeconds = parseFloat(retryAfter);
|
|
2252
|
+
if (!Number.isNaN(timeoutSeconds)) ms = timeoutSeconds * 1e3;
|
|
2253
|
+
else ms = Date.parse(retryAfter) - Date.now();
|
|
2254
|
+
}
|
|
2255
|
+
if (ms != null && !Number.isNaN(ms) && 0 <= ms && (ms < 6e4 || ms < exponentialBackoffDelay)) return ms;
|
|
2256
|
+
return exponentialBackoffDelay;
|
|
2257
|
+
}
|
|
2253
2258
|
/**
|
|
2254
|
-
*
|
|
2259
|
+
* The `retryWithExponentialBackoffRespectingRetryHeaders` strategy retries a failed API call with an exponential backoff,
|
|
2260
|
+
* while respecting rate limit headers (retry-after-ms and retry-after) if they are provided and reasonable (0-60 seconds).
|
|
2261
|
+
* You can configure the maximum number of retries, the initial delay, and the backoff factor.
|
|
2255
2262
|
*/
|
|
2256
|
-
const
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2266
|
-
}),
|
|
2267
|
-
z.object({
|
|
2268
|
-
type: z.literal("execution-denied"),
|
|
2269
|
-
reason: z.string().optional(),
|
|
2270
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2271
|
-
}),
|
|
2272
|
-
z.object({
|
|
2273
|
-
type: z.literal("error-text"),
|
|
2274
|
-
value: z.string(),
|
|
2275
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2276
|
-
}),
|
|
2277
|
-
z.object({
|
|
2278
|
-
type: z.literal("error-json"),
|
|
2279
|
-
value: jsonValueSchema,
|
|
2280
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2263
|
+
const retryWithExponentialBackoffRespectingRetryHeaders = ({ maxRetries = 2, initialDelayInMs = 2e3, backoffFactor = 2, abortSignal, additionalRetryableError } = {}) => retryWithExponentialBackoff({
|
|
2264
|
+
maxRetries,
|
|
2265
|
+
initialDelayInMs,
|
|
2266
|
+
backoffFactor,
|
|
2267
|
+
abortSignal,
|
|
2268
|
+
shouldRetry: async (error) => error instanceof Error && (APICallError$1.isInstance(error) && error.isRetryable === true || GatewayError.isInstance(error) && error.isRetryable === true) || additionalRetryableError != null && await additionalRetryableError(error),
|
|
2269
|
+
getDelayInMs: ({ error, exponentialBackoffDelay }) => getRetryDelayInMs({
|
|
2270
|
+
error,
|
|
2271
|
+
exponentialBackoffDelay
|
|
2281
2272
|
}),
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
type: z.literal("text"),
|
|
2287
|
-
text: z.string(),
|
|
2288
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2289
|
-
}),
|
|
2290
|
-
z.object({
|
|
2291
|
-
type: z.literal("file"),
|
|
2292
|
-
data: taggedFileDataSchema,
|
|
2293
|
-
mediaType: z.string(),
|
|
2294
|
-
filename: z.string().optional(),
|
|
2295
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2296
|
-
}),
|
|
2297
|
-
z.object({
|
|
2298
|
-
type: z.literal("file-data"),
|
|
2299
|
-
data: z.string(),
|
|
2300
|
-
mediaType: z.string(),
|
|
2301
|
-
filename: z.string().optional(),
|
|
2302
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2303
|
-
}),
|
|
2304
|
-
z.object({
|
|
2305
|
-
type: z.literal("file-url"),
|
|
2306
|
-
url: z.string(),
|
|
2307
|
-
mediaType: z.string().optional(),
|
|
2308
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2309
|
-
}),
|
|
2310
|
-
z.object({
|
|
2311
|
-
type: z.literal("file-id"),
|
|
2312
|
-
fileId: z.union([z.string(), z.record(z.string(), z.string())]),
|
|
2313
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2314
|
-
}),
|
|
2315
|
-
z.object({
|
|
2316
|
-
type: z.literal("file-reference"),
|
|
2317
|
-
providerReference: z.record(z.string(), z.string()),
|
|
2318
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2319
|
-
}),
|
|
2320
|
-
z.object({
|
|
2321
|
-
type: z.literal("image-data"),
|
|
2322
|
-
data: z.string(),
|
|
2323
|
-
mediaType: z.string(),
|
|
2324
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2325
|
-
}),
|
|
2326
|
-
z.object({
|
|
2327
|
-
type: z.literal("image-url"),
|
|
2328
|
-
url: z.string(),
|
|
2329
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2330
|
-
}),
|
|
2331
|
-
z.object({
|
|
2332
|
-
type: z.literal("image-file-id"),
|
|
2333
|
-
fileId: z.union([z.string(), z.record(z.string(), z.string())]),
|
|
2334
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2335
|
-
}),
|
|
2336
|
-
z.object({
|
|
2337
|
-
type: z.literal("image-file-reference"),
|
|
2338
|
-
providerReference: z.record(z.string(), z.string()),
|
|
2339
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2340
|
-
}),
|
|
2341
|
-
z.object({
|
|
2342
|
-
type: z.literal("custom"),
|
|
2343
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2344
|
-
})
|
|
2345
|
-
]))
|
|
2273
|
+
createRetryError: ({ message, reason, errors }) => new RetryError({
|
|
2274
|
+
message,
|
|
2275
|
+
reason,
|
|
2276
|
+
errors
|
|
2346
2277
|
})
|
|
2347
|
-
|
|
2278
|
+
});
|
|
2279
|
+
//#endregion
|
|
2280
|
+
//#region src/util/prepare-retries.ts
|
|
2348
2281
|
/**
|
|
2349
|
-
*
|
|
2282
|
+
* Validate and prepare retries.
|
|
2350
2283
|
*/
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
});
|
|
2284
|
+
function prepareRetries({ maxRetries, abortSignal, additionalRetryableError, parameter = "maxRetries", defaultMaxRetries = 2 }) {
|
|
2285
|
+
if (maxRetries != null) {
|
|
2286
|
+
if (!Number.isInteger(maxRetries)) throw new InvalidArgumentError({
|
|
2287
|
+
parameter,
|
|
2288
|
+
value: maxRetries,
|
|
2289
|
+
message: `${parameter} must be an integer`
|
|
2290
|
+
});
|
|
2291
|
+
if (maxRetries < 0) throw new InvalidArgumentError({
|
|
2292
|
+
parameter,
|
|
2293
|
+
value: maxRetries,
|
|
2294
|
+
message: `${parameter} must be >= 0`
|
|
2295
|
+
});
|
|
2296
|
+
}
|
|
2297
|
+
const maxRetriesResult = maxRetries ?? defaultMaxRetries;
|
|
2298
|
+
return {
|
|
2299
|
+
maxRetries: maxRetriesResult,
|
|
2300
|
+
retry: retryWithExponentialBackoffRespectingRetryHeaders({
|
|
2301
|
+
maxRetries: maxRetriesResult,
|
|
2302
|
+
abortSignal,
|
|
2303
|
+
additionalRetryableError
|
|
2304
|
+
})
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
//#endregion
|
|
2308
|
+
//#region src/util/set-abort-timeout.ts
|
|
2358
2309
|
/**
|
|
2359
|
-
*
|
|
2310
|
+
* Schedules a timeout that aborts the given controller with a `TimeoutError`
|
|
2311
|
+
* `DOMException`, matching the reason produced by `AbortSignal.timeout(ms)`.
|
|
2312
|
+
*
|
|
2313
|
+
* @param abortController - The controller to abort when the timeout elapses.
|
|
2314
|
+
* If undefined, no timeout is scheduled.
|
|
2315
|
+
* @param label - Human-readable label included in the error message
|
|
2316
|
+
* (e.g. "Step", "Chunk").
|
|
2317
|
+
* @param timeoutMs - Duration in milliseconds before the controller is aborted.
|
|
2318
|
+
* If undefined, no timeout is scheduled.
|
|
2319
|
+
* @returns The timeout id (suitable for passing to `clearTimeout`), or
|
|
2320
|
+
* `undefined` if no timeout was scheduled.
|
|
2360
2321
|
*/
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
signature: z.string().optional(),
|
|
2368
|
-
inputSchemaInput: z.unknown().optional()
|
|
2369
|
-
});
|
|
2322
|
+
function setAbortTimeout({ abortController, label, timeoutMs }) {
|
|
2323
|
+
if (abortController == null || timeoutMs == null) return;
|
|
2324
|
+
return setTimeout(() => abortController.abort(new DOMException(`${label} timeout of ${timeoutMs}ms exceeded`, "TimeoutError")), timeoutMs);
|
|
2325
|
+
}
|
|
2326
|
+
//#endregion
|
|
2327
|
+
//#region src/generate-text/calculate-tokens-per-second.ts
|
|
2370
2328
|
/**
|
|
2371
|
-
*
|
|
2329
|
+
* Calculates a token rate in tokens per second.
|
|
2330
|
+
*
|
|
2331
|
+
* Returns 0 when the token count is unknown, the duration is unknown or 0, or
|
|
2332
|
+
* the computed rate cannot be represented as a finite JSON-safe number.
|
|
2372
2333
|
*/
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
reason: z.string().optional()
|
|
2378
|
-
});
|
|
2334
|
+
function calculateTokensPerSecond({ tokens, durationMs }) {
|
|
2335
|
+
const tokenRate = 1e3 * (tokens ?? 0) / (durationMs ?? 0);
|
|
2336
|
+
return Number.isFinite(tokenRate) ? tokenRate : 0;
|
|
2337
|
+
}
|
|
2379
2338
|
//#endregion
|
|
2380
|
-
//#region src/
|
|
2381
|
-
const systemModelMessageSchema = z.object({
|
|
2382
|
-
role: z.literal("system"),
|
|
2383
|
-
content: z.string(),
|
|
2384
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2385
|
-
});
|
|
2386
|
-
const userModelMessageSchema = z.object({
|
|
2387
|
-
role: z.literal("user"),
|
|
2388
|
-
content: z.union([z.string(), z.array(z.union([
|
|
2389
|
-
textPartSchema,
|
|
2390
|
-
imagePartSchema,
|
|
2391
|
-
filePartSchema
|
|
2392
|
-
]))]),
|
|
2393
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2394
|
-
});
|
|
2395
|
-
const assistantModelMessageSchema = z.object({
|
|
2396
|
-
role: z.literal("assistant"),
|
|
2397
|
-
content: z.union([z.string(), z.array(z.union([
|
|
2398
|
-
textPartSchema,
|
|
2399
|
-
customPartSchema,
|
|
2400
|
-
filePartSchema,
|
|
2401
|
-
reasoningPartSchema,
|
|
2402
|
-
reasoningFilePartSchema,
|
|
2403
|
-
toolCallPartSchema,
|
|
2404
|
-
toolResultPartSchema,
|
|
2405
|
-
toolApprovalRequestSchema
|
|
2406
|
-
]))]),
|
|
2407
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2408
|
-
});
|
|
2409
|
-
const toolModelMessageSchema = z.object({
|
|
2410
|
-
role: z.literal("tool"),
|
|
2411
|
-
content: z.array(z.union([toolResultPartSchema, toolApprovalResponseSchema])),
|
|
2412
|
-
providerOptions: providerMetadataSchema.optional()
|
|
2413
|
-
});
|
|
2414
|
-
const modelMessageSchema = z.union([
|
|
2415
|
-
systemModelMessageSchema,
|
|
2416
|
-
userModelMessageSchema,
|
|
2417
|
-
assistantModelMessageSchema,
|
|
2418
|
-
toolModelMessageSchema
|
|
2419
|
-
]);
|
|
2420
|
-
//#endregion
|
|
2421
|
-
//#region src/prompt/standardize-prompt.ts
|
|
2339
|
+
//#region src/generate-text/collect-tool-approvals.ts
|
|
2422
2340
|
/**
|
|
2423
|
-
*
|
|
2424
|
-
*
|
|
2425
|
-
*
|
|
2426
|
-
* @param prompt - The prompt definition to standardize.
|
|
2427
|
-
* Set `allowSystemInMessages` to true to allow system messages in the
|
|
2428
|
-
* `prompt` or `messages` fields. System messages in the `instructions`
|
|
2429
|
-
* option are always allowed.
|
|
2430
|
-
* @returns The standardized prompt.
|
|
2431
|
-
* @throws {InvalidPromptError} When the prompt is invalid.
|
|
2341
|
+
* If the last message is a tool message, this function collects all tool approvals
|
|
2342
|
+
* from that message.
|
|
2432
2343
|
*/
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
if (prompt != null && messages != null) throw new InvalidPromptError$1({
|
|
2439
|
-
prompt,
|
|
2440
|
-
message: "prompt and messages cannot be defined at the same time"
|
|
2441
|
-
});
|
|
2442
|
-
if (typeof instructions !== "string" && !asArray(instructions).every((message) => message.role === "system")) throw new InvalidPromptError$1({
|
|
2443
|
-
prompt,
|
|
2444
|
-
message: "instructions must be a string, SystemModelMessage, or array of SystemModelMessage"
|
|
2445
|
-
});
|
|
2446
|
-
if (prompt != null && typeof prompt === "string") messages = [{
|
|
2447
|
-
role: "user",
|
|
2448
|
-
content: prompt
|
|
2449
|
-
}];
|
|
2450
|
-
else if (prompt != null && Array.isArray(prompt)) messages = prompt;
|
|
2451
|
-
else if (messages == null) throw new InvalidPromptError$1({
|
|
2452
|
-
prompt,
|
|
2453
|
-
message: "prompt or messages must be defined"
|
|
2454
|
-
});
|
|
2455
|
-
if (messages.length === 0) throw new InvalidPromptError$1({
|
|
2456
|
-
prompt,
|
|
2457
|
-
message: "messages must not be empty"
|
|
2458
|
-
});
|
|
2459
|
-
if (!allowSystemInMessages && messages.some((message) => message.role === "system")) throw new InvalidPromptError$1({
|
|
2460
|
-
prompt,
|
|
2461
|
-
message: "System messages are not allowed in the prompt or messages fields. Use the instructions option instead."
|
|
2462
|
-
});
|
|
2463
|
-
const validationResult = await safeValidateTypes({
|
|
2464
|
-
value: messages,
|
|
2465
|
-
schema: z.array(modelMessageSchema)
|
|
2466
|
-
});
|
|
2467
|
-
if (!validationResult.success) throw new InvalidPromptError$1({
|
|
2468
|
-
prompt,
|
|
2469
|
-
message: "The messages do not match the ModelMessage[] schema.",
|
|
2470
|
-
cause: validationResult.error
|
|
2471
|
-
});
|
|
2472
|
-
return {
|
|
2473
|
-
messages,
|
|
2474
|
-
instructions
|
|
2475
|
-
};
|
|
2476
|
-
}
|
|
2477
|
-
//#endregion
|
|
2478
|
-
//#region src/prompt/wrap-gateway-error.ts
|
|
2479
|
-
function wrapGatewayError(error) {
|
|
2480
|
-
if (!GatewayAuthenticationError.isInstance(error)) return error;
|
|
2481
|
-
const isProductionEnv = process?.env.NODE_ENV === "production";
|
|
2482
|
-
const moreInfoURL = "https://ai-sdk.dev/unauthenticated-ai-gateway";
|
|
2483
|
-
if (isProductionEnv) return new AISDKError$1({
|
|
2484
|
-
name: "GatewayError",
|
|
2485
|
-
message: `Unauthenticated. Configure AI_GATEWAY_API_KEY or use a provider module. Learn more: ${moreInfoURL}`
|
|
2486
|
-
});
|
|
2487
|
-
return Object.assign(/* @__PURE__ */ new Error(`\u001b[1m\u001b[31mUnauthenticated request to AI Gateway.\u001b[0m
|
|
2488
|
-
|
|
2489
|
-
To authenticate, set the \u001b[33mAI_GATEWAY_API_KEY\u001b[0m environment variable with your API key.
|
|
2490
|
-
|
|
2491
|
-
Alternatively, you can use a provider module instead of the AI Gateway.
|
|
2492
|
-
|
|
2493
|
-
Learn more: \u001b[34m${moreInfoURL}\u001b[0m
|
|
2494
|
-
|
|
2495
|
-
`), { name: "GatewayAuthenticationError" });
|
|
2496
|
-
}
|
|
2497
|
-
//#endregion
|
|
2498
|
-
//#region src/types/usage.ts
|
|
2499
|
-
function asLanguageModelUsage(usage) {
|
|
2500
|
-
return {
|
|
2501
|
-
inputTokens: usage.inputTokens.total,
|
|
2502
|
-
inputTokenDetails: {
|
|
2503
|
-
noCacheTokens: usage.inputTokens.noCache,
|
|
2504
|
-
cacheReadTokens: usage.inputTokens.cacheRead,
|
|
2505
|
-
cacheWriteTokens: usage.inputTokens.cacheWrite
|
|
2506
|
-
},
|
|
2507
|
-
outputTokens: usage.outputTokens.total,
|
|
2508
|
-
outputTokenDetails: {
|
|
2509
|
-
textTokens: usage.outputTokens.text,
|
|
2510
|
-
reasoningTokens: usage.outputTokens.reasoning
|
|
2511
|
-
},
|
|
2512
|
-
totalTokens: addTokenCounts(usage.inputTokens.total, usage.outputTokens.total),
|
|
2513
|
-
raw: usage.raw
|
|
2514
|
-
};
|
|
2515
|
-
}
|
|
2516
|
-
function createNullLanguageModelUsage() {
|
|
2517
|
-
return {
|
|
2518
|
-
inputTokens: void 0,
|
|
2519
|
-
inputTokenDetails: {
|
|
2520
|
-
noCacheTokens: void 0,
|
|
2521
|
-
cacheReadTokens: void 0,
|
|
2522
|
-
cacheWriteTokens: void 0
|
|
2523
|
-
},
|
|
2524
|
-
outputTokens: void 0,
|
|
2525
|
-
outputTokenDetails: {
|
|
2526
|
-
textTokens: void 0,
|
|
2527
|
-
reasoningTokens: void 0
|
|
2528
|
-
},
|
|
2529
|
-
totalTokens: void 0,
|
|
2530
|
-
raw: void 0
|
|
2531
|
-
};
|
|
2532
|
-
}
|
|
2533
|
-
function addLanguageModelUsage(usage1, usage2) {
|
|
2534
|
-
const raw = isNullLanguageModelUsage(usage1) ? usage2.raw : isNullLanguageModelUsage(usage2) ? usage1.raw : void 0;
|
|
2535
|
-
return {
|
|
2536
|
-
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2537
|
-
inputTokenDetails: {
|
|
2538
|
-
noCacheTokens: addTokenCounts(usage1.inputTokenDetails?.noCacheTokens, usage2.inputTokenDetails?.noCacheTokens),
|
|
2539
|
-
cacheReadTokens: addTokenCounts(usage1.inputTokenDetails?.cacheReadTokens, usage2.inputTokenDetails?.cacheReadTokens),
|
|
2540
|
-
cacheWriteTokens: addTokenCounts(usage1.inputTokenDetails?.cacheWriteTokens, usage2.inputTokenDetails?.cacheWriteTokens)
|
|
2541
|
-
},
|
|
2542
|
-
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
|
2543
|
-
outputTokenDetails: {
|
|
2544
|
-
textTokens: addTokenCounts(usage1.outputTokenDetails?.textTokens, usage2.outputTokenDetails?.textTokens),
|
|
2545
|
-
reasoningTokens: addTokenCounts(usage1.outputTokenDetails?.reasoningTokens, usage2.outputTokenDetails?.reasoningTokens)
|
|
2546
|
-
},
|
|
2547
|
-
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens),
|
|
2548
|
-
...raw == null ? {} : { raw }
|
|
2344
|
+
function collectToolApprovals({ messages }) {
|
|
2345
|
+
const lastMessage = messages.at(-1);
|
|
2346
|
+
if (lastMessage?.role != "tool") return {
|
|
2347
|
+
approvedToolApprovals: [],
|
|
2348
|
+
deniedToolApprovals: []
|
|
2549
2349
|
};
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2350
|
+
const toolCallsByToolCallId = Object.create(null);
|
|
2351
|
+
const toolCallOrder = Object.create(null);
|
|
2352
|
+
let nextToolCallIndex = 0;
|
|
2353
|
+
for (const message of messages) if (message.role === "assistant" && typeof message.content !== "string") {
|
|
2354
|
+
const content = message.content;
|
|
2355
|
+
for (const part of content) if (part.type === "tool-call") {
|
|
2356
|
+
toolCallsByToolCallId[part.toolCallId] = part;
|
|
2357
|
+
toolCallOrder[part.toolCallId] = nextToolCallIndex++;
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
const toolApprovalRequestsByApprovalId = Object.create(null);
|
|
2361
|
+
for (const message of messages) if (message.role === "assistant" && typeof message.content !== "string") {
|
|
2362
|
+
const content = message.content;
|
|
2363
|
+
for (const part of content) if (part.type === "tool-approval-request") toolApprovalRequestsByApprovalId[part.approvalId] = part;
|
|
2364
|
+
}
|
|
2365
|
+
const toolResults = Object.create(null);
|
|
2366
|
+
for (const part of lastMessage.content) if (part.type === "tool-result") toolResults[part.toolCallId] = part;
|
|
2367
|
+
const approvedToolApprovals = [];
|
|
2368
|
+
const deniedToolApprovals = [];
|
|
2369
|
+
const collectedToolCallIds = /* @__PURE__ */ new Set();
|
|
2370
|
+
const approvalResponses = lastMessage.content.filter((part) => part.type === "tool-approval-response");
|
|
2371
|
+
for (const approvalResponse of approvalResponses) {
|
|
2372
|
+
const approvalRequest = toolApprovalRequestsByApprovalId[approvalResponse.approvalId];
|
|
2373
|
+
if (approvalRequest == null) throw new InvalidToolApprovalError({ approvalId: approvalResponse.approvalId });
|
|
2374
|
+
const existingToolResult = toolResults[approvalRequest.toolCallId];
|
|
2375
|
+
if (existingToolResult != null && (approvalResponse.approved || existingToolResult.output.type !== "execution-denied")) continue;
|
|
2376
|
+
const toolCall = toolCallsByToolCallId[approvalRequest.toolCallId];
|
|
2377
|
+
if (toolCall == null) throw new ToolCallNotFoundForApprovalError({
|
|
2378
|
+
toolCallId: approvalRequest.toolCallId,
|
|
2379
|
+
approvalId: approvalRequest.approvalId
|
|
2380
|
+
});
|
|
2381
|
+
if (collectedToolCallIds.has(toolCall.toolCallId)) continue;
|
|
2382
|
+
collectedToolCallIds.add(toolCall.toolCallId);
|
|
2383
|
+
const approval = {
|
|
2384
|
+
approvalRequest,
|
|
2385
|
+
approvalResponse,
|
|
2386
|
+
toolCall,
|
|
2387
|
+
...existingToolResult != null ? { existingToolResult } : {}
|
|
2388
|
+
};
|
|
2389
|
+
if (approvalResponse.approved) approvedToolApprovals.push(approval);
|
|
2390
|
+
else deniedToolApprovals.push(approval);
|
|
2391
|
+
}
|
|
2392
|
+
approvedToolApprovals.sort((a, b) => toolCallOrder[a.toolCall.toolCallId] - toolCallOrder[b.toolCall.toolCallId]);
|
|
2558
2393
|
return {
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens)
|
|
2394
|
+
approvedToolApprovals,
|
|
2395
|
+
deniedToolApprovals
|
|
2562
2396
|
};
|
|
2563
2397
|
}
|
|
2564
2398
|
//#endregion
|
|
2565
|
-
//#region src/
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
function
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
//#region src/util/notify.ts
|
|
2602
|
-
/**
|
|
2603
|
-
* Notifies all provided callbacks with the given event in parallel.
|
|
2604
|
-
* Errors in callbacks do not break the generation flow.
|
|
2605
|
-
*/
|
|
2606
|
-
async function notify(options) {
|
|
2607
|
-
await Promise.all(asArray(options.callbacks).map(async (callback) => {
|
|
2608
|
-
try {
|
|
2609
|
-
await callback?.(options.event);
|
|
2610
|
-
} catch {}
|
|
2611
|
-
}));
|
|
2612
|
-
}
|
|
2613
|
-
//#endregion
|
|
2614
|
-
//#region src/util/retry-with-exponential-backoff.ts
|
|
2615
|
-
function getRetryDelayInMs({ error, exponentialBackoffDelay }) {
|
|
2616
|
-
const headers = APICallError$1.isInstance(error) ? error.responseHeaders : APICallError$1.isInstance(error.cause) ? error.cause.responseHeaders : void 0;
|
|
2617
|
-
if (!headers) return exponentialBackoffDelay;
|
|
2618
|
-
let ms;
|
|
2619
|
-
const retryAfterMs = headers["retry-after-ms"];
|
|
2620
|
-
if (retryAfterMs) {
|
|
2621
|
-
const timeoutMs = parseFloat(retryAfterMs);
|
|
2622
|
-
if (!Number.isNaN(timeoutMs)) ms = timeoutMs;
|
|
2623
|
-
}
|
|
2624
|
-
const retryAfter = headers["retry-after"];
|
|
2625
|
-
if (retryAfter && ms === void 0) {
|
|
2626
|
-
const timeoutSeconds = parseFloat(retryAfter);
|
|
2627
|
-
if (!Number.isNaN(timeoutSeconds)) ms = timeoutSeconds * 1e3;
|
|
2628
|
-
else ms = Date.parse(retryAfter) - Date.now();
|
|
2629
|
-
}
|
|
2630
|
-
if (ms != null && !Number.isNaN(ms) && 0 <= ms && (ms < 6e4 || ms < exponentialBackoffDelay)) return ms;
|
|
2631
|
-
return exponentialBackoffDelay;
|
|
2632
|
-
}
|
|
2633
|
-
/**
|
|
2634
|
-
* The `retryWithExponentialBackoffRespectingRetryHeaders` strategy retries a failed API call with an exponential backoff,
|
|
2635
|
-
* while respecting rate limit headers (retry-after-ms and retry-after) if they are provided and reasonable (0-60 seconds).
|
|
2636
|
-
* You can configure the maximum number of retries, the initial delay, and the backoff factor.
|
|
2637
|
-
*/
|
|
2638
|
-
const retryWithExponentialBackoffRespectingRetryHeaders = ({ maxRetries = 2, initialDelayInMs = 2e3, backoffFactor = 2, abortSignal, additionalRetryableError } = {}) => retryWithExponentialBackoff({
|
|
2639
|
-
maxRetries,
|
|
2640
|
-
initialDelayInMs,
|
|
2641
|
-
backoffFactor,
|
|
2642
|
-
abortSignal,
|
|
2643
|
-
shouldRetry: async (error) => error instanceof Error && (APICallError$1.isInstance(error) && error.isRetryable === true || GatewayError.isInstance(error) && error.isRetryable === true) || additionalRetryableError != null && await additionalRetryableError(error),
|
|
2644
|
-
getDelayInMs: ({ error, exponentialBackoffDelay }) => getRetryDelayInMs({
|
|
2645
|
-
error,
|
|
2646
|
-
exponentialBackoffDelay
|
|
2647
|
-
}),
|
|
2648
|
-
createRetryError: ({ message, reason, errors }) => new RetryError({
|
|
2649
|
-
message,
|
|
2650
|
-
reason,
|
|
2651
|
-
errors
|
|
2652
|
-
})
|
|
2653
|
-
});
|
|
2654
|
-
//#endregion
|
|
2655
|
-
//#region src/util/prepare-retries.ts
|
|
2656
|
-
/**
|
|
2657
|
-
* Validate and prepare retries.
|
|
2658
|
-
*/
|
|
2659
|
-
function prepareRetries({ maxRetries, abortSignal, additionalRetryableError, parameter = "maxRetries", defaultMaxRetries = 2 }) {
|
|
2660
|
-
if (maxRetries != null) {
|
|
2661
|
-
if (!Number.isInteger(maxRetries)) throw new InvalidArgumentError({
|
|
2662
|
-
parameter,
|
|
2663
|
-
value: maxRetries,
|
|
2664
|
-
message: `${parameter} must be an integer`
|
|
2665
|
-
});
|
|
2666
|
-
if (maxRetries < 0) throw new InvalidArgumentError({
|
|
2667
|
-
parameter,
|
|
2668
|
-
value: maxRetries,
|
|
2669
|
-
message: `${parameter} must be >= 0`
|
|
2670
|
-
});
|
|
2671
|
-
}
|
|
2672
|
-
const maxRetriesResult = maxRetries ?? defaultMaxRetries;
|
|
2673
|
-
return {
|
|
2674
|
-
maxRetries: maxRetriesResult,
|
|
2675
|
-
retry: retryWithExponentialBackoffRespectingRetryHeaders({
|
|
2676
|
-
maxRetries: maxRetriesResult,
|
|
2677
|
-
abortSignal,
|
|
2678
|
-
additionalRetryableError
|
|
2679
|
-
})
|
|
2680
|
-
};
|
|
2681
|
-
}
|
|
2682
|
-
//#endregion
|
|
2683
|
-
//#region src/util/set-abort-timeout.ts
|
|
2684
|
-
/**
|
|
2685
|
-
* Schedules a timeout that aborts the given controller with a `TimeoutError`
|
|
2686
|
-
* `DOMException`, matching the reason produced by `AbortSignal.timeout(ms)`.
|
|
2687
|
-
*
|
|
2688
|
-
* @param abortController - The controller to abort when the timeout elapses.
|
|
2689
|
-
* If undefined, no timeout is scheduled.
|
|
2690
|
-
* @param label - Human-readable label included in the error message
|
|
2691
|
-
* (e.g. "Step", "Chunk").
|
|
2692
|
-
* @param timeoutMs - Duration in milliseconds before the controller is aborted.
|
|
2693
|
-
* If undefined, no timeout is scheduled.
|
|
2694
|
-
* @returns The timeout id (suitable for passing to `clearTimeout`), or
|
|
2695
|
-
* `undefined` if no timeout was scheduled.
|
|
2696
|
-
*/
|
|
2697
|
-
function setAbortTimeout({ abortController, label, timeoutMs }) {
|
|
2698
|
-
if (abortController == null || timeoutMs == null) return;
|
|
2699
|
-
return setTimeout(() => abortController.abort(new DOMException(`${label} timeout of ${timeoutMs}ms exceeded`, "TimeoutError")), timeoutMs);
|
|
2700
|
-
}
|
|
2701
|
-
//#endregion
|
|
2702
|
-
//#region src/generate-text/calculate-tokens-per-second.ts
|
|
2703
|
-
/**
|
|
2704
|
-
* Calculates a token rate in tokens per second.
|
|
2705
|
-
*
|
|
2706
|
-
* Returns 0 when the token count is unknown, the duration is unknown or 0, or
|
|
2707
|
-
* the computed rate cannot be represented as a finite JSON-safe number.
|
|
2708
|
-
*/
|
|
2709
|
-
function calculateTokensPerSecond({ tokens, durationMs }) {
|
|
2710
|
-
const tokenRate = 1e3 * (tokens ?? 0) / (durationMs ?? 0);
|
|
2711
|
-
return Number.isFinite(tokenRate) ? tokenRate : 0;
|
|
2712
|
-
}
|
|
2713
|
-
//#endregion
|
|
2714
|
-
//#region src/generate-text/collect-tool-approvals.ts
|
|
2715
|
-
/**
|
|
2716
|
-
* If the last message is a tool message, this function collects all tool approvals
|
|
2717
|
-
* from that message.
|
|
2718
|
-
*/
|
|
2719
|
-
function collectToolApprovals({ messages }) {
|
|
2720
|
-
const lastMessage = messages.at(-1);
|
|
2721
|
-
if (lastMessage?.role != "tool") return {
|
|
2722
|
-
approvedToolApprovals: [],
|
|
2723
|
-
deniedToolApprovals: []
|
|
2724
|
-
};
|
|
2725
|
-
const toolCallsByToolCallId = Object.create(null);
|
|
2726
|
-
const toolCallOrder = Object.create(null);
|
|
2727
|
-
let nextToolCallIndex = 0;
|
|
2728
|
-
for (const message of messages) if (message.role === "assistant" && typeof message.content !== "string") {
|
|
2729
|
-
const content = message.content;
|
|
2730
|
-
for (const part of content) if (part.type === "tool-call") {
|
|
2731
|
-
toolCallsByToolCallId[part.toolCallId] = part;
|
|
2732
|
-
toolCallOrder[part.toolCallId] = nextToolCallIndex++;
|
|
2733
|
-
}
|
|
2734
|
-
}
|
|
2735
|
-
const toolApprovalRequestsByApprovalId = Object.create(null);
|
|
2736
|
-
for (const message of messages) if (message.role === "assistant" && typeof message.content !== "string") {
|
|
2737
|
-
const content = message.content;
|
|
2738
|
-
for (const part of content) if (part.type === "tool-approval-request") toolApprovalRequestsByApprovalId[part.approvalId] = part;
|
|
2739
|
-
}
|
|
2740
|
-
const toolResults = Object.create(null);
|
|
2741
|
-
for (const part of lastMessage.content) if (part.type === "tool-result") toolResults[part.toolCallId] = part;
|
|
2742
|
-
const approvedToolApprovals = [];
|
|
2743
|
-
const deniedToolApprovals = [];
|
|
2744
|
-
const collectedToolCallIds = /* @__PURE__ */ new Set();
|
|
2745
|
-
const approvalResponses = lastMessage.content.filter((part) => part.type === "tool-approval-response");
|
|
2746
|
-
for (const approvalResponse of approvalResponses) {
|
|
2747
|
-
const approvalRequest = toolApprovalRequestsByApprovalId[approvalResponse.approvalId];
|
|
2748
|
-
if (approvalRequest == null) throw new InvalidToolApprovalError({ approvalId: approvalResponse.approvalId });
|
|
2749
|
-
const existingToolResult = toolResults[approvalRequest.toolCallId];
|
|
2750
|
-
if (existingToolResult != null && (approvalResponse.approved || existingToolResult.output.type !== "execution-denied")) continue;
|
|
2751
|
-
const toolCall = toolCallsByToolCallId[approvalRequest.toolCallId];
|
|
2752
|
-
if (toolCall == null) throw new ToolCallNotFoundForApprovalError({
|
|
2753
|
-
toolCallId: approvalRequest.toolCallId,
|
|
2754
|
-
approvalId: approvalRequest.approvalId
|
|
2755
|
-
});
|
|
2756
|
-
if (collectedToolCallIds.has(toolCall.toolCallId)) continue;
|
|
2757
|
-
collectedToolCallIds.add(toolCall.toolCallId);
|
|
2758
|
-
const approval = {
|
|
2759
|
-
approvalRequest,
|
|
2760
|
-
approvalResponse,
|
|
2761
|
-
toolCall,
|
|
2762
|
-
...existingToolResult != null ? { existingToolResult } : {}
|
|
2763
|
-
};
|
|
2764
|
-
if (approvalResponse.approved) approvedToolApprovals.push(approval);
|
|
2765
|
-
else deniedToolApprovals.push(approval);
|
|
2766
|
-
}
|
|
2767
|
-
approvedToolApprovals.sort((a, b) => toolCallOrder[a.toolCall.toolCallId] - toolCallOrder[b.toolCall.toolCallId]);
|
|
2768
|
-
return {
|
|
2769
|
-
approvedToolApprovals,
|
|
2770
|
-
deniedToolApprovals
|
|
2771
|
-
};
|
|
2772
|
-
}
|
|
2773
|
-
//#endregion
|
|
2774
|
-
//#region src/generate-text/generated-file.ts
|
|
2775
|
-
var DefaultGeneratedFile = class {
|
|
2776
|
-
constructor({ data, mediaType, providerMetadata }) {
|
|
2777
|
-
const isUint8Array = data instanceof Uint8Array;
|
|
2778
|
-
this.base64Data = isUint8Array ? void 0 : data;
|
|
2779
|
-
this.uint8ArrayData = isUint8Array ? data : void 0;
|
|
2780
|
-
this.mediaType = mediaType;
|
|
2781
|
-
this.providerMetadata = providerMetadata;
|
|
2782
|
-
}
|
|
2783
|
-
get base64() {
|
|
2784
|
-
if (this.base64Data == null) this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
|
|
2785
|
-
return this.base64Data;
|
|
2786
|
-
}
|
|
2787
|
-
get uint8Array() {
|
|
2788
|
-
if (this.uint8ArrayData == null) this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
|
|
2789
|
-
return this.uint8ArrayData;
|
|
2790
|
-
}
|
|
2791
|
-
};
|
|
2792
|
-
var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
|
|
2793
|
-
constructor(..._args) {
|
|
2794
|
-
super(..._args);
|
|
2795
|
-
this.type = "file";
|
|
2796
|
-
}
|
|
2797
|
-
};
|
|
2798
|
-
//#endregion
|
|
2799
|
-
//#region src/generate-text/resolve-generated-file-data.ts
|
|
2800
|
-
async function resolveGeneratedFileData({ data, abortSignal, cache }) {
|
|
2801
|
-
if (data.type === "data") return data.data;
|
|
2802
|
-
const cachedData = cache?.get(data);
|
|
2803
|
-
if (cachedData != null) return cachedData;
|
|
2804
|
-
const downloadedData = (await download({
|
|
2805
|
-
url: data.url,
|
|
2806
|
-
abortSignal
|
|
2807
|
-
})).data;
|
|
2808
|
-
cache?.set(data, downloadedData);
|
|
2809
|
-
return downloadedData;
|
|
2399
|
+
//#region src/generate-text/generated-file.ts
|
|
2400
|
+
var DefaultGeneratedFile = class {
|
|
2401
|
+
constructor({ data, mediaType, providerMetadata }) {
|
|
2402
|
+
const isUint8Array = data instanceof Uint8Array;
|
|
2403
|
+
this.base64Data = isUint8Array ? void 0 : data;
|
|
2404
|
+
this.uint8ArrayData = isUint8Array ? data : void 0;
|
|
2405
|
+
this.mediaType = mediaType;
|
|
2406
|
+
this.providerMetadata = providerMetadata;
|
|
2407
|
+
}
|
|
2408
|
+
get base64() {
|
|
2409
|
+
if (this.base64Data == null) this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
|
|
2410
|
+
return this.base64Data;
|
|
2411
|
+
}
|
|
2412
|
+
get uint8Array() {
|
|
2413
|
+
if (this.uint8ArrayData == null) this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
|
|
2414
|
+
return this.uint8ArrayData;
|
|
2415
|
+
}
|
|
2416
|
+
};
|
|
2417
|
+
var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
|
|
2418
|
+
constructor(..._args) {
|
|
2419
|
+
super(..._args);
|
|
2420
|
+
this.type = "file";
|
|
2421
|
+
}
|
|
2422
|
+
};
|
|
2423
|
+
//#endregion
|
|
2424
|
+
//#region src/generate-text/resolve-generated-file-data.ts
|
|
2425
|
+
async function resolveGeneratedFileData({ data, abortSignal, cache }) {
|
|
2426
|
+
if (data.type === "data") return data.data;
|
|
2427
|
+
const cachedData = cache?.get(data);
|
|
2428
|
+
if (cachedData != null) return cachedData;
|
|
2429
|
+
const downloadedData = (await download({
|
|
2430
|
+
url: data.url,
|
|
2431
|
+
abortSignal
|
|
2432
|
+
})).data;
|
|
2433
|
+
cache?.set(data, downloadedData);
|
|
2434
|
+
return downloadedData;
|
|
2810
2435
|
}
|
|
2811
2436
|
//#endregion
|
|
2812
2437
|
//#region src/generate-text/convert-language-model-content.ts
|
|
@@ -2929,20 +2554,7 @@ function resolveToolCallerConfiguration({ tools, toolCallers }) {
|
|
|
2929
2554
|
value: toolCallers,
|
|
2930
2555
|
message: `unknown tool "${toolName}".`
|
|
2931
2556
|
});
|
|
2932
|
-
if (
|
|
2933
|
-
parameter: "experimental_toolCallers",
|
|
2934
|
-
value: toolCallers,
|
|
2935
|
-
message: `callers for tool "${toolName}" must be an array.`
|
|
2936
|
-
});
|
|
2937
|
-
resolved[toolName] = callers.map((caller) => {
|
|
2938
|
-
if (caller === "AI_SDK_DIRECT_TOOL_CALL") return caller;
|
|
2939
|
-
if (typeof caller !== "string" || !Object.prototype.hasOwnProperty.call(tools, caller) || experimental_getToolCaller(tools[caller]) == null) throw new InvalidArgumentError({
|
|
2940
|
-
parameter: "experimental_toolCallers",
|
|
2941
|
-
value: toolCallers,
|
|
2942
|
-
message: `tool "${toolName}" contains an invalid caller.`
|
|
2943
|
-
});
|
|
2944
|
-
return caller;
|
|
2945
|
-
});
|
|
2557
|
+
if (callers != null) resolved[toolName] = callers;
|
|
2946
2558
|
}
|
|
2947
2559
|
return resolved;
|
|
2948
2560
|
}
|
|
@@ -4488,7 +4100,8 @@ function augmentEvent(event, telemetry, filterContext = false) {
|
|
|
4488
4100
|
const augmentedEvent = Object.assign(Object.create(Object.getPrototypeOf(event)), event, {
|
|
4489
4101
|
recordInputs: telemetry.recordInputs,
|
|
4490
4102
|
recordOutputs: telemetry.recordOutputs,
|
|
4491
|
-
functionId: telemetry.functionId
|
|
4103
|
+
functionId: telemetry.functionId,
|
|
4104
|
+
aiVersion: VERSION
|
|
4492
4105
|
});
|
|
4493
4106
|
if (filterContext && event != null && typeof event === "object" && "runtimeContext" in event) augmentedEvent.runtimeContext = filterIncludedContext({
|
|
4494
4107
|
context: event.runtimeContext,
|
|
@@ -4565,6 +4178,7 @@ function createTelemetryDispatcher({ telemetry }) {
|
|
|
4565
4178
|
onToolExecutionStart: mergeTelemetryCallback("onToolExecutionStart"),
|
|
4566
4179
|
onToolExecutionEnd: mergeTelemetryCallback("onToolExecutionEnd"),
|
|
4567
4180
|
onStepEnd: onStepEnd == null && onStepFinish == null ? void 0 : mergeCallbacks(onStepEnd, onStepFinish),
|
|
4181
|
+
onStepError: mergeTelemetryCallback("onStepError"),
|
|
4568
4182
|
onObjectStepStart: mergeTelemetryCallback("onObjectStepStart"),
|
|
4569
4183
|
onObjectStepEnd: mergeTelemetryCallback("onObjectStepEnd"),
|
|
4570
4184
|
onEmbedStart: mergeTelemetryCallback("onEmbedStart"),
|
|
@@ -4628,7 +4242,7 @@ function asReasoningText(reasoningParts) {
|
|
|
4628
4242
|
//#endregion
|
|
4629
4243
|
//#region src/generate-text/step-result.ts
|
|
4630
4244
|
var DefaultStepResult = class {
|
|
4631
|
-
constructor({ callId, stepNumber, provider, modelId, runtimeContext, toolsContext, content, finishReason, rawFinishReason, usage, performance, warnings, request, response, providerMetadata }) {
|
|
4245
|
+
constructor({ callId, stepNumber, provider, modelId, runtimeContext, toolsContext, content, finishReason, rawFinishReason, usage, performance, warnings, request, response, providerMetadata, steeringMessages }) {
|
|
4632
4246
|
this.callId = callId;
|
|
4633
4247
|
this.stepNumber = stepNumber;
|
|
4634
4248
|
this.model = {
|
|
@@ -4646,6 +4260,7 @@ var DefaultStepResult = class {
|
|
|
4646
4260
|
this.request = request;
|
|
4647
4261
|
this.response = response;
|
|
4648
4262
|
this.providerMetadata = providerMetadata;
|
|
4263
|
+
if (steeringMessages != null && steeringMessages.length > 0) this.experimental_steeringMessages = steeringMessages;
|
|
4649
4264
|
}
|
|
4650
4265
|
get text() {
|
|
4651
4266
|
return this.content.filter((part) => part.type === "text").map((part) => part.text).join("");
|
|
@@ -4709,7 +4324,8 @@ function restrictStepResult({ step, includeRuntimeContext, includeToolsContext }
|
|
|
4709
4324
|
warnings: step.warnings,
|
|
4710
4325
|
request: step.request,
|
|
4711
4326
|
response: step.response,
|
|
4712
|
-
providerMetadata: step.providerMetadata
|
|
4327
|
+
providerMetadata: step.providerMetadata,
|
|
4328
|
+
steeringMessages: step.experimental_steeringMessages
|
|
4713
4329
|
});
|
|
4714
4330
|
}
|
|
4715
4331
|
/**
|
|
@@ -4718,6 +4334,22 @@ function restrictStepResult({ step, includeRuntimeContext, includeToolsContext }
|
|
|
4718
4334
|
*/
|
|
4719
4335
|
function createRestrictedTelemetryDispatcher$3({ telemetry, includeRuntimeContext, includeToolsContext }) {
|
|
4720
4336
|
const telemetryDispatcher = createTelemetryDispatcher({ telemetry });
|
|
4337
|
+
const restrictStepStartEvent = (event) => ({
|
|
4338
|
+
...event,
|
|
4339
|
+
runtimeContext: filterIncludedContext({
|
|
4340
|
+
context: event.runtimeContext,
|
|
4341
|
+
includeContext: includeRuntimeContext
|
|
4342
|
+
}),
|
|
4343
|
+
steps: event.steps.map((step) => restrictStepResult({
|
|
4344
|
+
step,
|
|
4345
|
+
includeRuntimeContext,
|
|
4346
|
+
includeToolsContext
|
|
4347
|
+
})),
|
|
4348
|
+
toolsContext: filterToolsContext({
|
|
4349
|
+
toolsContext: event.toolsContext,
|
|
4350
|
+
includeToolsContext
|
|
4351
|
+
})
|
|
4352
|
+
});
|
|
4721
4353
|
return {
|
|
4722
4354
|
...telemetryDispatcher,
|
|
4723
4355
|
onStart: (event) => telemetryDispatcher.onStart?.({
|
|
@@ -4731,22 +4363,7 @@ function createRestrictedTelemetryDispatcher$3({ telemetry, includeRuntimeContex
|
|
|
4731
4363
|
includeToolsContext
|
|
4732
4364
|
})
|
|
4733
4365
|
}),
|
|
4734
|
-
onStepStart: (event) => telemetryDispatcher.onStepStart?.(
|
|
4735
|
-
...event,
|
|
4736
|
-
runtimeContext: filterIncludedContext({
|
|
4737
|
-
context: event.runtimeContext,
|
|
4738
|
-
includeContext: includeRuntimeContext
|
|
4739
|
-
}),
|
|
4740
|
-
steps: event.steps.map((step) => restrictStepResult({
|
|
4741
|
-
step,
|
|
4742
|
-
includeRuntimeContext,
|
|
4743
|
-
includeToolsContext
|
|
4744
|
-
})),
|
|
4745
|
-
toolsContext: filterToolsContext({
|
|
4746
|
-
toolsContext: event.toolsContext,
|
|
4747
|
-
includeToolsContext
|
|
4748
|
-
})
|
|
4749
|
-
}),
|
|
4366
|
+
onStepStart: (event) => telemetryDispatcher.onStepStart?.(restrictStepStartEvent(event)),
|
|
4750
4367
|
onStepEnd: (event) => telemetryDispatcher.onStepEnd?.(restrictStepResult({
|
|
4751
4368
|
step: event,
|
|
4752
4369
|
includeRuntimeContext,
|
|
@@ -4757,6 +4374,7 @@ function createRestrictedTelemetryDispatcher$3({ telemetry, includeRuntimeContex
|
|
|
4757
4374
|
includeRuntimeContext,
|
|
4758
4375
|
includeToolsContext
|
|
4759
4376
|
})),
|
|
4377
|
+
onStepError: (event) => telemetryDispatcher.onStepError?.(restrictStepStartEvent(event)),
|
|
4760
4378
|
onEnd: (event) => telemetryDispatcher.onEnd?.(((restrictedSteps) => {
|
|
4761
4379
|
return {
|
|
4762
4380
|
...event,
|
|
@@ -4803,6 +4421,134 @@ function createRestrictedTelemetryDispatcher$3({ telemetry, includeRuntimeContex
|
|
|
4803
4421
|
};
|
|
4804
4422
|
}
|
|
4805
4423
|
//#endregion
|
|
4424
|
+
//#region src/generate-text/steering.ts
|
|
4425
|
+
const generateSteeringId = createIdGenerator$1({
|
|
4426
|
+
prefix: "steer",
|
|
4427
|
+
size: 24
|
|
4428
|
+
});
|
|
4429
|
+
const steeringRunStarter = Symbol.for("vercel.ai.steering.start");
|
|
4430
|
+
var DefaultSteering = class {
|
|
4431
|
+
constructor() {
|
|
4432
|
+
this.state = "idle";
|
|
4433
|
+
this.pending = [];
|
|
4434
|
+
this.taken = [];
|
|
4435
|
+
this.outcomes = /* @__PURE__ */ new Map();
|
|
4436
|
+
}
|
|
4437
|
+
steer(message, options) {
|
|
4438
|
+
const id = options?.id ?? generateSteeringId();
|
|
4439
|
+
const existingOutcome = this.outcomes.get(id);
|
|
4440
|
+
if (existingOutcome != null) return existingOutcome;
|
|
4441
|
+
const userMessage = typeof message === "string" ? {
|
|
4442
|
+
role: "user",
|
|
4443
|
+
content: message
|
|
4444
|
+
} : message;
|
|
4445
|
+
const outcome = this.state === "ended" ? Promise.resolve({
|
|
4446
|
+
type: "not-delivered",
|
|
4447
|
+
id,
|
|
4448
|
+
message: userMessage,
|
|
4449
|
+
reason: "finished"
|
|
4450
|
+
}) : new Promise((resolve) => {
|
|
4451
|
+
this.pending.push({
|
|
4452
|
+
id,
|
|
4453
|
+
message: userMessage,
|
|
4454
|
+
resolve
|
|
4455
|
+
});
|
|
4456
|
+
});
|
|
4457
|
+
this.outcomes.set(id, outcome);
|
|
4458
|
+
return outcome;
|
|
4459
|
+
}
|
|
4460
|
+
[steeringRunStarter]() {
|
|
4461
|
+
if (this.state !== "idle") throw new InvalidArgumentError({
|
|
4462
|
+
parameter: "experimental_steering",
|
|
4463
|
+
value: this,
|
|
4464
|
+
message: "The steering object was already used by a generation. Create a new one with experimental_createSteering() for each call."
|
|
4465
|
+
});
|
|
4466
|
+
this.state = "running";
|
|
4467
|
+
return {
|
|
4468
|
+
hasPendingMessages: () => this.state === "running" && this.pending.length > 0,
|
|
4469
|
+
takeMessages: () => {
|
|
4470
|
+
if (this.state !== "running") return [];
|
|
4471
|
+
this.taken = [...this.taken, ...this.pending];
|
|
4472
|
+
this.pending = [];
|
|
4473
|
+
return this.taken.map(({ message }) => message);
|
|
4474
|
+
},
|
|
4475
|
+
resolveTakenMessages: ({ stepNumber, sentMessages }) => {
|
|
4476
|
+
const taken = this.taken;
|
|
4477
|
+
this.taken = [];
|
|
4478
|
+
const delivered = [];
|
|
4479
|
+
for (const { id, message, resolve } of taken) if (isSent({
|
|
4480
|
+
message,
|
|
4481
|
+
sentMessages
|
|
4482
|
+
})) {
|
|
4483
|
+
delivered.push(message);
|
|
4484
|
+
resolve({
|
|
4485
|
+
type: "delivered",
|
|
4486
|
+
id,
|
|
4487
|
+
message,
|
|
4488
|
+
stepNumber
|
|
4489
|
+
});
|
|
4490
|
+
} else resolve({
|
|
4491
|
+
type: "not-delivered",
|
|
4492
|
+
id,
|
|
4493
|
+
message,
|
|
4494
|
+
reason: "removed-by-prepare-step"
|
|
4495
|
+
});
|
|
4496
|
+
return delivered;
|
|
4497
|
+
},
|
|
4498
|
+
end: (reason) => {
|
|
4499
|
+
if (this.state !== "running") return;
|
|
4500
|
+
const notDelivered = [...this.taken, ...this.pending];
|
|
4501
|
+
this.taken = [];
|
|
4502
|
+
this.pending = [];
|
|
4503
|
+
this.state = "ended";
|
|
4504
|
+
for (const { id, message, resolve } of notDelivered) resolve({
|
|
4505
|
+
type: "not-delivered",
|
|
4506
|
+
id,
|
|
4507
|
+
message,
|
|
4508
|
+
reason
|
|
4509
|
+
});
|
|
4510
|
+
}
|
|
4511
|
+
};
|
|
4512
|
+
}
|
|
4513
|
+
};
|
|
4514
|
+
function isSent({ message, sentMessages }) {
|
|
4515
|
+
const content = JSON.stringify(message.content);
|
|
4516
|
+
return sentMessages.some((sentMessage) => sentMessage === message || sentMessage.role === "user" && JSON.stringify(sentMessage.content) === content);
|
|
4517
|
+
}
|
|
4518
|
+
/**
|
|
4519
|
+
* Creates a steering object that sends user messages to a running generation.
|
|
4520
|
+
*
|
|
4521
|
+
* @example
|
|
4522
|
+
* ```ts
|
|
4523
|
+
* const steering = experimental_createSteering();
|
|
4524
|
+
*
|
|
4525
|
+
* const result = streamText({
|
|
4526
|
+
* model,
|
|
4527
|
+
* tools,
|
|
4528
|
+
* stopWhen: isStepCount(20),
|
|
4529
|
+
* experimental_steering: steering,
|
|
4530
|
+
* });
|
|
4531
|
+
*
|
|
4532
|
+
* const outcome = await steering.steer('Use Paris, Texas instead.');
|
|
4533
|
+
* ```
|
|
4534
|
+
*/
|
|
4535
|
+
function createSteering() {
|
|
4536
|
+
return new DefaultSteering();
|
|
4537
|
+
}
|
|
4538
|
+
/**
|
|
4539
|
+
* Starts a run for a steering object that was passed to a generation.
|
|
4540
|
+
*/
|
|
4541
|
+
function startSteeringRun(steering) {
|
|
4542
|
+
if (steering == null) return;
|
|
4543
|
+
const start = steering[steeringRunStarter];
|
|
4544
|
+
if (typeof start !== "function") throw new InvalidArgumentError({
|
|
4545
|
+
parameter: "experimental_steering",
|
|
4546
|
+
value: steering,
|
|
4547
|
+
message: "The steering object must be created with experimental_createSteering()."
|
|
4548
|
+
});
|
|
4549
|
+
return start.call(steering);
|
|
4550
|
+
}
|
|
4551
|
+
//#endregion
|
|
4806
4552
|
//#region src/generate-text/stop-condition.ts
|
|
4807
4553
|
/**
|
|
4808
4554
|
* Creates a stop condition that returns `true` when the number of completed
|
|
@@ -5321,6 +5067,7 @@ const originalGenerateCallId$9 = createIdGenerator$1({
|
|
|
5321
5067
|
* @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
|
|
5322
5068
|
*
|
|
5323
5069
|
* @param experimental_sandbox - The sandbox environment that is passed through to tool execution.
|
|
5070
|
+
* @param experimental_steering - A steering object that adds user messages to the generation at the next step boundary.
|
|
5324
5071
|
* @param runtimeContext - User-defined runtime context that flows through the entire generation lifecycle.
|
|
5325
5072
|
* @param experimental_refineToolInput - Optional mapping of tool names to functions that refine parsed tool inputs before tools are executed and before outputs, callbacks, and telemetry are recorded.
|
|
5326
5073
|
* @param onStart - Callback invoked when generation begins, before any LLM calls.
|
|
@@ -5339,13 +5086,23 @@ const originalGenerateCallId$9 = createIdGenerator$1({
|
|
|
5339
5086
|
* Uses a discriminated union: check `success` to determine if `output` or `error` is present.
|
|
5340
5087
|
* @param experimental_onToolCallFinish - Deprecated alias for `onToolExecutionEnd`.
|
|
5341
5088
|
* @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
|
|
5089
|
+
* @param onStepError - Callback that is called when a step that started fails before it produces a step result, i.e. when the provider call throws or rejects, or when the call is aborted or times out.
|
|
5342
5090
|
* @param onEnd - Callback that is called when all steps are finished and the response is complete.
|
|
5343
5091
|
* @param onFinish - Deprecated alias for `onEnd`.
|
|
5344
5092
|
*
|
|
5345
5093
|
* @returns
|
|
5346
5094
|
* A result object that contains the generated text, the results of the tool calls, and additional information.
|
|
5347
5095
|
*/
|
|
5348
|
-
async function generateText(
|
|
5096
|
+
async function generateText(options) {
|
|
5097
|
+
const steeringRun = startSteeringRun(options.experimental_steering);
|
|
5098
|
+
try {
|
|
5099
|
+
return await generateTextInternal(options, steeringRun);
|
|
5100
|
+
} catch (error) {
|
|
5101
|
+
steeringRun?.end(options.abortSignal?.aborted ? "aborted" : "error");
|
|
5102
|
+
throw error;
|
|
5103
|
+
}
|
|
5104
|
+
}
|
|
5105
|
+
async function generateTextInternal({ model: modelArg, tools, toolChoice, instructions, system, prompt, messages, allowSystemInMessages, maxRetries: maxRetriesArg, abortSignal, timeout, headers, stopWhen = isStepCount(1), experimental_sandbox: sandbox, output, toolApproval, experimental_toolCallers, experimental_toolApprovalSecret, experimental_telemetry, telemetry = experimental_telemetry, providerOptions, activeTools, toolOrder, toolCallConcurrency, prepareStep, experimental_repairToolCall, repairToolCall = experimental_repairToolCall, experimental_refineToolInput: refineToolInput, experimental_download: download, runtimeContext = {}, toolsContext = {}, experimental_include, include = experimental_include, _internal: { generateId = originalGenerateId$4, generateCallId = originalGenerateCallId$9, now: now$4 = now } = {}, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onLanguageModelCallStart, experimental_onLanguageModelCallStart, onLanguageModelCallEnd, experimental_onLanguageModelCallEnd, onToolExecutionStart, onToolExecutionEnd, experimental_onToolCallStart, experimental_onToolCallFinish, onStepEnd, onStepError, onStepFinish, onFinish, onEnd = onFinish, ...settings }, steeringRun) {
|
|
5349
5106
|
include = {
|
|
5350
5107
|
requestBody: include?.requestBody ?? false,
|
|
5351
5108
|
requestMessages: include?.requestMessages ?? false,
|
|
@@ -5442,6 +5199,7 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5442
5199
|
event: generateTextStartEvent,
|
|
5443
5200
|
callbacks: [resolvedOnStart, telemetryDispatcher.onStart]
|
|
5444
5201
|
});
|
|
5202
|
+
let openStep;
|
|
5445
5203
|
try {
|
|
5446
5204
|
const initialMessages = initialPrompt.messages;
|
|
5447
5205
|
const initialResponseMessages = [];
|
|
@@ -5533,6 +5291,29 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5533
5291
|
let instructionsForNextStep = initialPrompt.instructions;
|
|
5534
5292
|
let messagesForNextStep = [...initialMessages, ...initialResponseMessages];
|
|
5535
5293
|
const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
|
|
5294
|
+
let steeringEndReason = "finished";
|
|
5295
|
+
const shouldStartNextStep = async () => {
|
|
5296
|
+
if (steps[steps.length - 1].finishReason === "interrupted") return false;
|
|
5297
|
+
if (clientToolOutputs.length + deniedToolApprovalResponses.length !== clientToolCalls.length) {
|
|
5298
|
+
steeringEndReason = "waiting-for-tool-results";
|
|
5299
|
+
return false;
|
|
5300
|
+
}
|
|
5301
|
+
if (clientToolCalls.length === 0 && pendingDeferredToolCalls.size === 0) {
|
|
5302
|
+
if (!steeringRun?.hasPendingMessages()) return false;
|
|
5303
|
+
if (steps[steps.length - 1].finishReason === "error") {
|
|
5304
|
+
steeringEndReason = "error";
|
|
5305
|
+
return false;
|
|
5306
|
+
}
|
|
5307
|
+
}
|
|
5308
|
+
if (await isStopConditionMet({
|
|
5309
|
+
stopConditions,
|
|
5310
|
+
steps
|
|
5311
|
+
})) {
|
|
5312
|
+
steeringEndReason = "stop-condition";
|
|
5313
|
+
return false;
|
|
5314
|
+
}
|
|
5315
|
+
return true;
|
|
5316
|
+
};
|
|
5536
5317
|
do {
|
|
5537
5318
|
if (steps.length > 0) mergedAbortSignal?.throwIfAborted();
|
|
5538
5319
|
const stepTimeoutId = setAbortTimeout({
|
|
@@ -5541,6 +5322,8 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5541
5322
|
timeoutMs: stepTimeoutMs
|
|
5542
5323
|
});
|
|
5543
5324
|
const stepNumber = steps.length;
|
|
5325
|
+
const steeringMessages = steeringRun?.takeMessages() ?? [];
|
|
5326
|
+
if (steeringMessages.length > 0) messagesForNextStep = [...messagesForNextStep, ...steeringMessages];
|
|
5544
5327
|
try {
|
|
5545
5328
|
await runInTracingChannelSpan({
|
|
5546
5329
|
type: "step",
|
|
@@ -5560,6 +5343,7 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5560
5343
|
messages: stepInputMessages,
|
|
5561
5344
|
initialMessages,
|
|
5562
5345
|
responseMessages: accumulatedResponseMessages,
|
|
5346
|
+
...steeringMessages.length > 0 ? { experimental_steeringMessages: steeringMessages } : {},
|
|
5563
5347
|
runtimeContext,
|
|
5564
5348
|
toolsContext,
|
|
5565
5349
|
experimental_sandbox: sandbox
|
|
@@ -5593,6 +5377,10 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5593
5377
|
messages: prepareStepResult?.messages ?? stepInputMessages,
|
|
5594
5378
|
toolCallerMessages
|
|
5595
5379
|
});
|
|
5380
|
+
const deliveredSteeringMessages = steeringRun?.resolveTakenMessages({
|
|
5381
|
+
stepNumber,
|
|
5382
|
+
sentMessages: stepMessages
|
|
5383
|
+
}) ?? [];
|
|
5596
5384
|
const promptMessages = await convertToLanguageModelPrompt({
|
|
5597
5385
|
prompt: {
|
|
5598
5386
|
instructions: stepInstructions,
|
|
@@ -5608,29 +5396,31 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5608
5396
|
callSettings,
|
|
5609
5397
|
stepSettings: prepareStepResult
|
|
5610
5398
|
});
|
|
5399
|
+
const stepStartEvent = {
|
|
5400
|
+
callId,
|
|
5401
|
+
provider: stepModel.provider,
|
|
5402
|
+
modelId: stepModel.modelId,
|
|
5403
|
+
stepNumber,
|
|
5404
|
+
instructions: stepInstructions,
|
|
5405
|
+
messages: stepMessages,
|
|
5406
|
+
tools,
|
|
5407
|
+
toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
|
|
5408
|
+
activeTools: prepareStepResult?.activeTools ?? activeTools,
|
|
5409
|
+
toolOrder: stepToolOrder,
|
|
5410
|
+
steps: [...steps],
|
|
5411
|
+
providerOptions: stepProviderOptions,
|
|
5412
|
+
output,
|
|
5413
|
+
runtimeContext,
|
|
5414
|
+
promptMessages,
|
|
5415
|
+
stepTools,
|
|
5416
|
+
stepToolChoice,
|
|
5417
|
+
toolsContext
|
|
5418
|
+
};
|
|
5611
5419
|
await notify({
|
|
5612
|
-
event:
|
|
5613
|
-
callId,
|
|
5614
|
-
provider: stepModel.provider,
|
|
5615
|
-
modelId: stepModel.modelId,
|
|
5616
|
-
stepNumber,
|
|
5617
|
-
instructions: stepInstructions,
|
|
5618
|
-
messages: stepMessages,
|
|
5619
|
-
tools,
|
|
5620
|
-
toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
|
|
5621
|
-
activeTools: prepareStepResult?.activeTools ?? activeTools,
|
|
5622
|
-
toolOrder: stepToolOrder,
|
|
5623
|
-
steps: [...steps],
|
|
5624
|
-
providerOptions: stepProviderOptions,
|
|
5625
|
-
output,
|
|
5626
|
-
runtimeContext,
|
|
5627
|
-
promptMessages,
|
|
5628
|
-
stepTools,
|
|
5629
|
-
stepToolChoice,
|
|
5630
|
-
toolsContext
|
|
5631
|
-
},
|
|
5420
|
+
event: stepStartEvent,
|
|
5632
5421
|
callbacks: [resolvedOnStepStart, telemetryDispatcher.onStepStart]
|
|
5633
5422
|
});
|
|
5423
|
+
openStep = stepStartEvent;
|
|
5634
5424
|
const languageModelCallContext = {
|
|
5635
5425
|
provider: stepModel.provider,
|
|
5636
5426
|
modelId: stepModel.modelId,
|
|
@@ -5928,7 +5718,8 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5928
5718
|
providerMetadata: currentModelResponse.providerMetadata,
|
|
5929
5719
|
request: stepRequest,
|
|
5930
5720
|
response: stepResponse,
|
|
5931
|
-
toolsContext
|
|
5721
|
+
toolsContext,
|
|
5722
|
+
steeringMessages: deliveredSteeringMessages
|
|
5932
5723
|
});
|
|
5933
5724
|
logWarnings({
|
|
5934
5725
|
warnings: currentModelResponse.warnings ?? [],
|
|
@@ -5938,6 +5729,7 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5938
5729
|
steps.push(currentStepResult);
|
|
5939
5730
|
instructionsForNextStep = stepInstructions;
|
|
5940
5731
|
messagesForNextStep = [...stepMessages, ...stepResponseMessages];
|
|
5732
|
+
openStep = void 0;
|
|
5941
5733
|
await notify({
|
|
5942
5734
|
event: currentStepResult,
|
|
5943
5735
|
callbacks: [resolvedOnStepEnd, telemetryDispatcher.onStepEnd]
|
|
@@ -5948,10 +5740,8 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
5948
5740
|
} finally {
|
|
5949
5741
|
if (stepTimeoutId != null) clearTimeout(stepTimeoutId);
|
|
5950
5742
|
}
|
|
5951
|
-
} while (
|
|
5952
|
-
|
|
5953
|
-
steps
|
|
5954
|
-
}));
|
|
5743
|
+
} while (await shouldStartNextStep());
|
|
5744
|
+
steeringRun?.end(steeringEndReason);
|
|
5955
5745
|
const lastStep = steps[steps.length - 1];
|
|
5956
5746
|
const totalUsage = steps.reduce((totalUsage, step) => {
|
|
5957
5747
|
return addLanguageModelUsage(totalUsage, step.usage);
|
|
@@ -6028,11 +5818,20 @@ async function generateText({ model: modelArg, tools, toolChoice, instructions,
|
|
|
6028
5818
|
output: resolvedOutput
|
|
6029
5819
|
});
|
|
6030
5820
|
} catch (error) {
|
|
5821
|
+
const wrappedError = wrapGatewayError(error);
|
|
5822
|
+
steeringRun?.end(mergedAbortSignal?.aborted ? "aborted" : "error");
|
|
5823
|
+
if (openStep != null) await notify({
|
|
5824
|
+
event: {
|
|
5825
|
+
...openStep,
|
|
5826
|
+
error: wrappedError
|
|
5827
|
+
},
|
|
5828
|
+
callbacks: [onStepError, telemetryDispatcher.onStepError]
|
|
5829
|
+
});
|
|
6031
5830
|
await telemetryDispatcher.onError?.({
|
|
6032
5831
|
callId,
|
|
6033
5832
|
error
|
|
6034
5833
|
});
|
|
6035
|
-
throw
|
|
5834
|
+
throw wrappedError;
|
|
6036
5835
|
}
|
|
6037
5836
|
};
|
|
6038
5837
|
return await runInTracingChannelSpan({
|
|
@@ -6332,6 +6131,40 @@ function getResponseUIMessageId({ originalMessages, responseMessageId }) {
|
|
|
6332
6131
|
return lastMessage?.role === "assistant" ? lastMessage.id : typeof responseMessageId === "function" ? responseMessageId() : responseMessageId;
|
|
6333
6132
|
}
|
|
6334
6133
|
//#endregion
|
|
6134
|
+
//#region src/util/zod.ts
|
|
6135
|
+
const z = {
|
|
6136
|
+
array,
|
|
6137
|
+
boolean,
|
|
6138
|
+
custom,
|
|
6139
|
+
discriminatedUnion,
|
|
6140
|
+
enum: enum$1,
|
|
6141
|
+
instanceof: instanceof$1,
|
|
6142
|
+
lazy,
|
|
6143
|
+
literal,
|
|
6144
|
+
looseObject,
|
|
6145
|
+
never,
|
|
6146
|
+
null: null$1,
|
|
6147
|
+
number,
|
|
6148
|
+
object,
|
|
6149
|
+
record,
|
|
6150
|
+
string,
|
|
6151
|
+
union,
|
|
6152
|
+
unknown
|
|
6153
|
+
};
|
|
6154
|
+
//#endregion
|
|
6155
|
+
//#region src/types/json-value.ts
|
|
6156
|
+
const jsonValueSchema = z.lazy(() => z.union([
|
|
6157
|
+
z.null(),
|
|
6158
|
+
z.string(),
|
|
6159
|
+
z.number(),
|
|
6160
|
+
z.boolean(),
|
|
6161
|
+
z.record(z.string(), jsonValueSchema.optional()),
|
|
6162
|
+
z.array(jsonValueSchema)
|
|
6163
|
+
]));
|
|
6164
|
+
//#endregion
|
|
6165
|
+
//#region src/types/provider-metadata.ts
|
|
6166
|
+
const providerMetadataSchema = z.record(z.string(), z.record(z.string(), jsonValueSchema.optional()));
|
|
6167
|
+
//#endregion
|
|
6335
6168
|
//#region src/types/citation.ts
|
|
6336
6169
|
const citationSchema = z.object({
|
|
6337
6170
|
source: z.discriminatedUnion("sourceType", [z.object({
|
|
@@ -8832,6 +8665,7 @@ const originalGenerateCallId$7 = createIdGenerator$1({
|
|
|
8832
8665
|
* @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
|
|
8833
8666
|
*
|
|
8834
8667
|
* @param experimental_sandbox - The sandbox environment that is passed through to tool execution.
|
|
8668
|
+
* @param experimental_steering - A steering object that adds user messages to the generation at the next step boundary.
|
|
8835
8669
|
* @param runtimeContext - User-defined runtime context that flows through the entire generation lifecycle.
|
|
8836
8670
|
* @param experimental_refineToolInput - Optional mapping of tool names to functions that refine parsed tool inputs before tools are executed and before outputs, callbacks, and telemetry are recorded.
|
|
8837
8671
|
*
|
|
@@ -8850,6 +8684,7 @@ const originalGenerateCallId$7 = createIdGenerator$1({
|
|
|
8850
8684
|
* @param onToolExecutionEnd - Callback invoked after each tool execution completes.
|
|
8851
8685
|
* @param experimental_onToolCallFinish - Deprecated alias for `onToolExecutionEnd`.
|
|
8852
8686
|
* @param onStepEnd - Callback that is called when each step (LLM call) ends, including intermediate steps.
|
|
8687
|
+
* @param onStepError - Callback that is called when a step that started fails before it produces a step result, i.e. when the provider call throws or rejects, or when the model stream ends without output. A provider `error` chunk still ends the step with `onStepEnd`, and an abort or timeout ends it with `onAbort`.
|
|
8853
8688
|
* @param onStepFinish - Deprecated alias for `onStepEnd`.
|
|
8854
8689
|
* @param onEnd - Callback that is called when all steps are finished and the response is complete.
|
|
8855
8690
|
* @param onFinish - Deprecated alias for `onEnd`.
|
|
@@ -8858,7 +8693,7 @@ const originalGenerateCallId$7 = createIdGenerator$1({
|
|
|
8858
8693
|
* A result object for accessing different stream types and additional information.
|
|
8859
8694
|
*/
|
|
8860
8695
|
function streamText(options) {
|
|
8861
|
-
return
|
|
8696
|
+
return startStreamText(options);
|
|
8862
8697
|
}
|
|
8863
8698
|
/**
|
|
8864
8699
|
* Stream text with a single consumer, without replay or final-content getters.
|
|
@@ -8868,7 +8703,7 @@ function streamTextLowMemory(options) {
|
|
|
8868
8703
|
const telemetry = options.telemetry ?? options.experimental_telemetry;
|
|
8869
8704
|
const integrations = telemetry?.integrations != null ? asArray(telemetry.integrations) : getGlobalTelemetryIntegrations();
|
|
8870
8705
|
const collectContent = options.tools != null && Object.keys(options.tools).length > 0 || options.toolChoice != null || options.output != null || options.prepareStep != null || options.stopWhen != null || options.onEnd != null || options.onFinish != null || options.onStepEnd != null || options.onStepFinish != null || options.onAbort != null || options.onLanguageModelCallEnd != null || options.experimental_onLanguageModelCallEnd != null || telemetry?.isEnabled !== false && integrations.length > 0;
|
|
8871
|
-
return createSingleConsumerStreamTextResult(
|
|
8706
|
+
return createSingleConsumerStreamTextResult(startStreamText({
|
|
8872
8707
|
...options,
|
|
8873
8708
|
_internal: {
|
|
8874
8709
|
...options._internal,
|
|
@@ -8879,10 +8714,19 @@ function streamTextLowMemory(options) {
|
|
|
8879
8714
|
}
|
|
8880
8715
|
}));
|
|
8881
8716
|
}
|
|
8882
|
-
function
|
|
8717
|
+
function startStreamText(options) {
|
|
8718
|
+
const steeringRun = startSteeringRun(options.experimental_steering);
|
|
8719
|
+
try {
|
|
8720
|
+
return streamTextInternal(options, steeringRun);
|
|
8721
|
+
} catch (error) {
|
|
8722
|
+
steeringRun?.end("error");
|
|
8723
|
+
throw error;
|
|
8724
|
+
}
|
|
8725
|
+
}
|
|
8726
|
+
function streamTextInternal({ model, tools, toolChoice, instructions, system, prompt, messages, allowSystemInMessages, maxRetries, streamRetries, abortSignal, timeout, headers, stopWhen = isStepCount(1), experimental_sandbox: sandbox, output, toolApproval, experimental_toolCallers, experimental_toolApprovalSecret, experimental_telemetry, telemetry = experimental_telemetry, prepareStep, providerOptions, activeTools, toolOrder, toolCallConcurrency, experimental_repairToolCall, repairToolCall = experimental_repairToolCall, experimental_refineToolInput: refineToolInput, experimental_transform: transform, experimental_download: download, includeRawChunks, onChunk, onError: onErrorArg, onFinish, onEnd = onFinish, onAbort, onStepEnd, onStepError, onStepFinish, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onLanguageModelCallStart, experimental_onLanguageModelCallStart, onLanguageModelCallEnd, experimental_onLanguageModelCallEnd, onToolExecutionStart, onToolExecutionEnd, experimental_onToolCallStart, experimental_onToolCallFinish, runtimeContext = {}, toolsContext = {}, experimental_include, include = experimental_include, _internal: { now: now$2 = now, generateId = originalGenerateId$2, generateCallId = originalGenerateCallId$7, retention = {
|
|
8883
8727
|
replay: true,
|
|
8884
8728
|
collectContent: true
|
|
8885
|
-
} } = {}, ...settings }) {
|
|
8729
|
+
} } = {}, ...settings }, steeringRun) {
|
|
8886
8730
|
const resolvedToolCallConcurrency = prepareToolCallConcurrency(toolCallConcurrency);
|
|
8887
8731
|
const totalTimeoutMs = getTotalTimeoutMs(timeout);
|
|
8888
8732
|
const stepTimeoutMs = getStepTimeoutMs(timeout);
|
|
@@ -8922,6 +8766,7 @@ function streamTextInternal({ model, tools, toolChoice, instructions, system, pr
|
|
|
8922
8766
|
messages,
|
|
8923
8767
|
allowSystemInMessages,
|
|
8924
8768
|
experimental_sandbox: sandbox,
|
|
8769
|
+
steeringRun,
|
|
8925
8770
|
tools,
|
|
8926
8771
|
toolsContext,
|
|
8927
8772
|
runtimeContext,
|
|
@@ -8946,6 +8791,7 @@ function streamTextInternal({ model, tools, toolChoice, instructions, system, pr
|
|
|
8946
8791
|
onEnd,
|
|
8947
8792
|
onAbort,
|
|
8948
8793
|
onStepFinish: resolvedOnStepEnd,
|
|
8794
|
+
onStepError,
|
|
8949
8795
|
onStart: resolvedOnStart,
|
|
8950
8796
|
onStepStart: resolvedOnStepStart,
|
|
8951
8797
|
onLanguageModelCallStart: resolvedOnLanguageModelCallStart,
|
|
@@ -9143,7 +8989,7 @@ function applyStreamTextTransforms({ stream, transforms, tools, stopStream }) {
|
|
|
9143
8989
|
});
|
|
9144
8990
|
}
|
|
9145
8991
|
var DefaultStreamTextResult = class {
|
|
9146
|
-
constructor({ retention, model, telemetry, headers, settings, maxRetries: maxRetriesArg, streamRetries: streamRetriesArg, abortSignal, stepTimeoutMs, stepAbortController, firstChunkTimeoutMs, firstChunkAbortController, chunkTimeoutMs, chunkAbortController, instructions, system, prompt, messages, allowSystemInMessages, experimental_sandbox: sandbox, tools, toolChoice, transforms, activeTools, toolOrder, toolCallConcurrency, repairToolCall, refineToolInput, stopConditions, output, toolApproval, experimental_toolCallers, experimental_toolApprovalSecret, providerOptions, prepareStep, now, generateId, generateCallId, timeout, onChunk, onError, canRetryStreamViaOnError, onEnd, onAbort, onStepFinish, onStart, onStepStart, onLanguageModelCallStart, onLanguageModelCallEnd, onToolExecutionStart, onToolExecutionEnd, runtimeContext, toolsContext, download, include }) {
|
|
8992
|
+
constructor({ retention, model, telemetry, headers, settings, maxRetries: maxRetriesArg, streamRetries: streamRetriesArg, abortSignal, stepTimeoutMs, stepAbortController, firstChunkTimeoutMs, firstChunkAbortController, chunkTimeoutMs, chunkAbortController, instructions, system, prompt, messages, allowSystemInMessages, experimental_sandbox: sandbox, steeringRun, tools, toolChoice, transforms, activeTools, toolOrder, toolCallConcurrency, repairToolCall, refineToolInput, stopConditions, output, toolApproval, experimental_toolCallers, experimental_toolApprovalSecret, providerOptions, prepareStep, now, generateId, generateCallId, timeout, onChunk, onError, canRetryStreamViaOnError, onEnd, onAbort, onStepFinish, onStepError, onStart, onStepStart, onLanguageModelCallStart, onLanguageModelCallEnd, onToolExecutionStart, onToolExecutionEnd, runtimeContext, toolsContext, download, include }) {
|
|
9147
8993
|
this._totalUsage = new DelayedPromise();
|
|
9148
8994
|
this._finishReason = new DelayedPromise();
|
|
9149
8995
|
this._rawFinishReason = new DelayedPromise();
|
|
@@ -9178,6 +9024,7 @@ var DefaultStreamTextResult = class {
|
|
|
9178
9024
|
const initialResponseMessages = [];
|
|
9179
9025
|
let stepMessagesForNextStep;
|
|
9180
9026
|
let currentStepMessages = [];
|
|
9027
|
+
let currentStepSteeringMessages = [];
|
|
9181
9028
|
let isAborted = false;
|
|
9182
9029
|
let currentStepModel = model;
|
|
9183
9030
|
const createPartIdReserver = () => {
|
|
@@ -9202,6 +9049,19 @@ var DefaultStreamTextResult = class {
|
|
|
9202
9049
|
let activeReasoningContent = createIdMap();
|
|
9203
9050
|
let recordedNoOutputError;
|
|
9204
9051
|
const errorsHandledForStreamRetry = /* @__PURE__ */ new Set();
|
|
9052
|
+
let openStep;
|
|
9053
|
+
const notifyStepError = async (error) => {
|
|
9054
|
+
if (openStep == null || abortSignal?.aborted) return;
|
|
9055
|
+
const { event } = openStep;
|
|
9056
|
+
openStep = void 0;
|
|
9057
|
+
await notify({
|
|
9058
|
+
event: {
|
|
9059
|
+
...event,
|
|
9060
|
+
error
|
|
9061
|
+
},
|
|
9062
|
+
callbacks: [onStepError, telemetryDispatcher.onStepError]
|
|
9063
|
+
});
|
|
9064
|
+
};
|
|
9205
9065
|
const eventProcessor = new TransformStream({
|
|
9206
9066
|
async transform(chunk, controller) {
|
|
9207
9067
|
if (isStreamRetryBoundaryPart(chunk)) {
|
|
@@ -9225,12 +9085,15 @@ var DefaultStreamTextResult = class {
|
|
|
9225
9085
|
const error = wrapGatewayError(part.error);
|
|
9226
9086
|
if (NoOutputGeneratedError.isInstance(error)) recordedNoOutputError = error;
|
|
9227
9087
|
if (callbacksHandledForStreamRetry) errorsHandledForStreamRetry.delete(part.error);
|
|
9228
|
-
else
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
9232
|
-
|
|
9233
|
-
|
|
9088
|
+
else {
|
|
9089
|
+
if (openStep != null) openStep.error = error;
|
|
9090
|
+
await notify({
|
|
9091
|
+
event: { error },
|
|
9092
|
+
callbacks: async (event) => {
|
|
9093
|
+
await onError(event);
|
|
9094
|
+
}
|
|
9095
|
+
});
|
|
9096
|
+
}
|
|
9234
9097
|
}
|
|
9235
9098
|
if (part.type === "custom" || part.type === "source" || part.type === "tool-call" || part.type === "tool-approval-request" || part.type === "tool-approval-response" || part.type === "tool-error") {
|
|
9236
9099
|
if (retention.collectContent) recordedContent.push(part);
|
|
@@ -9355,8 +9218,10 @@ var DefaultStreamTextResult = class {
|
|
|
9355
9218
|
...part.response,
|
|
9356
9219
|
messages: cloneModelMessages(stepResponseMessages)
|
|
9357
9220
|
},
|
|
9358
|
-
providerMetadata: part.providerMetadata
|
|
9221
|
+
providerMetadata: part.providerMetadata,
|
|
9222
|
+
steeringMessages: currentStepSteeringMessages
|
|
9359
9223
|
});
|
|
9224
|
+
openStep = void 0;
|
|
9360
9225
|
await notify({
|
|
9361
9226
|
event: currentStepResult,
|
|
9362
9227
|
callbacks: [onStepFinish, telemetryDispatcher.onStepEnd]
|
|
@@ -9377,9 +9242,16 @@ var DefaultStreamTextResult = class {
|
|
|
9377
9242
|
}
|
|
9378
9243
|
},
|
|
9379
9244
|
async flush(controller) {
|
|
9245
|
+
steeringRun?.end(recordedSteps.length === 0 || recordedNoOutputError != null ? "error" : "finished");
|
|
9380
9246
|
try {
|
|
9247
|
+
const noOutputError = new NoOutputGeneratedError({ message: "No output generated. Check the stream for errors." });
|
|
9248
|
+
await notifyStepError(openStep?.error ?? noOutputError);
|
|
9381
9249
|
if (recordedSteps.length === 0 || recordedNoOutputError != null) {
|
|
9382
|
-
const error = abortSignal?.aborted ? abortSignal.reason : recordedNoOutputError ??
|
|
9250
|
+
const error = abortSignal?.aborted ? abortSignal.reason : recordedNoOutputError ?? noOutputError;
|
|
9251
|
+
if (recordedNoOutputError != null && !abortSignal?.aborted) await telemetryDispatcher.onError?.({
|
|
9252
|
+
callId,
|
|
9253
|
+
error: recordedNoOutputError
|
|
9254
|
+
});
|
|
9383
9255
|
self.rejectResultPromises(error);
|
|
9384
9256
|
return;
|
|
9385
9257
|
}
|
|
@@ -9479,6 +9351,7 @@ var DefaultStreamTextResult = class {
|
|
|
9479
9351
|
async function abort() {
|
|
9480
9352
|
isAborted = true;
|
|
9481
9353
|
removeAbortListener();
|
|
9354
|
+
steeringRun?.end("aborted");
|
|
9482
9355
|
await notify({
|
|
9483
9356
|
event: {
|
|
9484
9357
|
callId,
|
|
@@ -9509,6 +9382,8 @@ var DefaultStreamTextResult = class {
|
|
|
9509
9382
|
removeAbortListener();
|
|
9510
9383
|
if (isAbortError(error) && abortSignal?.aborted) await abort();
|
|
9511
9384
|
else {
|
|
9385
|
+
steeringRun?.end("error");
|
|
9386
|
+
await notifyStepError(wrapGatewayError(error));
|
|
9512
9387
|
await telemetryDispatcher.onError?.({
|
|
9513
9388
|
callId,
|
|
9514
9389
|
error
|
|
@@ -9519,6 +9394,7 @@ var DefaultStreamTextResult = class {
|
|
|
9519
9394
|
},
|
|
9520
9395
|
cancel(reason) {
|
|
9521
9396
|
removeAbortListener();
|
|
9397
|
+
steeringRun?.end("aborted");
|
|
9522
9398
|
return reader.cancel(reason);
|
|
9523
9399
|
}
|
|
9524
9400
|
});
|
|
@@ -9774,7 +9650,8 @@ var DefaultStreamTextResult = class {
|
|
|
9774
9650
|
const runInStepTracingChannelContext = (execute) => stepTracingChannelContext?.run(execute) ?? execute();
|
|
9775
9651
|
const responseMessagesFromPreviousSteps = recordedSteps.flatMap((step) => step.response.messages);
|
|
9776
9652
|
const accumulatedResponseMessages = [...initialResponseMessages, ...responseMessagesFromPreviousSteps];
|
|
9777
|
-
const
|
|
9653
|
+
const steeringMessages = steeringRun?.takeMessages() ?? [];
|
|
9654
|
+
const stepInputMessages = [...stepMessagesForNextStep ?? [...initialMessages, ...initialResponseMessages], ...steeringMessages];
|
|
9778
9655
|
const prepareStepResult = await prepareStep?.({
|
|
9779
9656
|
model,
|
|
9780
9657
|
steps: recordedSteps,
|
|
@@ -9784,6 +9661,7 @@ var DefaultStreamTextResult = class {
|
|
|
9784
9661
|
messages: stepInputMessages,
|
|
9785
9662
|
initialMessages,
|
|
9786
9663
|
responseMessages: accumulatedResponseMessages,
|
|
9664
|
+
...steeringMessages.length > 0 ? { experimental_steeringMessages: steeringMessages } : {},
|
|
9787
9665
|
toolsContext,
|
|
9788
9666
|
runtimeContext,
|
|
9789
9667
|
experimental_sandbox: sandbox
|
|
@@ -9818,6 +9696,10 @@ var DefaultStreamTextResult = class {
|
|
|
9818
9696
|
toolCallerMessages
|
|
9819
9697
|
});
|
|
9820
9698
|
currentStepMessages = stepMessages;
|
|
9699
|
+
currentStepSteeringMessages = steeringRun?.resolveTakenMessages({
|
|
9700
|
+
stepNumber: currentStep,
|
|
9701
|
+
sentMessages: stepMessages
|
|
9702
|
+
}) ?? [];
|
|
9821
9703
|
const stepInstructions = prepareStepResult?.instructions ?? prepareStepResult?.system ?? instructionsForNextStep;
|
|
9822
9704
|
instructionsForNextStep = stepInstructions;
|
|
9823
9705
|
const stepProviderOptions = mergeObjects(providerOptions, prepareStepResult?.providerOptions);
|
|
@@ -9856,29 +9738,31 @@ var DefaultStreamTextResult = class {
|
|
|
9856
9738
|
onStart: async ({ promptMessages }) => {
|
|
9857
9739
|
if (hasNotifiedStepStart) return;
|
|
9858
9740
|
hasNotifiedStepStart = true;
|
|
9741
|
+
const stepStartEvent = {
|
|
9742
|
+
callId,
|
|
9743
|
+
provider: stepModel.provider,
|
|
9744
|
+
modelId: stepModel.modelId,
|
|
9745
|
+
stepNumber: recordedSteps.length,
|
|
9746
|
+
instructions: stepInstructions,
|
|
9747
|
+
messages: stepMessages,
|
|
9748
|
+
tools,
|
|
9749
|
+
toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
|
|
9750
|
+
activeTools: prepareStepResult?.activeTools ?? activeTools,
|
|
9751
|
+
toolOrder: stepToolOrder,
|
|
9752
|
+
steps: [...recordedSteps],
|
|
9753
|
+
providerOptions: stepProviderOptions,
|
|
9754
|
+
runtimeContext,
|
|
9755
|
+
toolsContext,
|
|
9756
|
+
output,
|
|
9757
|
+
promptMessages,
|
|
9758
|
+
stepTools,
|
|
9759
|
+
stepToolChoice
|
|
9760
|
+
};
|
|
9859
9761
|
await notify({
|
|
9860
|
-
event:
|
|
9861
|
-
callId,
|
|
9862
|
-
provider: stepModel.provider,
|
|
9863
|
-
modelId: stepModel.modelId,
|
|
9864
|
-
stepNumber: recordedSteps.length,
|
|
9865
|
-
instructions: stepInstructions,
|
|
9866
|
-
messages: stepMessages,
|
|
9867
|
-
tools,
|
|
9868
|
-
toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
|
|
9869
|
-
activeTools: prepareStepResult?.activeTools ?? activeTools,
|
|
9870
|
-
toolOrder: stepToolOrder,
|
|
9871
|
-
steps: [...recordedSteps],
|
|
9872
|
-
providerOptions: stepProviderOptions,
|
|
9873
|
-
runtimeContext,
|
|
9874
|
-
toolsContext,
|
|
9875
|
-
output,
|
|
9876
|
-
promptMessages,
|
|
9877
|
-
stepTools,
|
|
9878
|
-
stepToolChoice
|
|
9879
|
-
},
|
|
9762
|
+
event: stepStartEvent,
|
|
9880
9763
|
callbacks: [onStepStart, telemetryDispatcher.onStepStart]
|
|
9881
9764
|
});
|
|
9765
|
+
openStep = { event: stepStartEvent };
|
|
9882
9766
|
},
|
|
9883
9767
|
_internal: {
|
|
9884
9768
|
now,
|
|
@@ -10259,15 +10143,36 @@ var DefaultStreamTextResult = class {
|
|
|
10259
10143
|
}
|
|
10260
10144
|
for (const output of stepToolOutputs) if (output.type === "tool-result" || output.type === "tool-error") pendingDeferredToolCalls.delete(output.toolCallId);
|
|
10261
10145
|
cleanupStepTimeouts();
|
|
10262
|
-
|
|
10263
|
-
|
|
10264
|
-
|
|
10265
|
-
|
|
10146
|
+
let steeringEndReason = "finished";
|
|
10147
|
+
const shouldStartNextStep = async () => {
|
|
10148
|
+
if (stepFinishReason === "interrupted") return false;
|
|
10149
|
+
if (clientToolCalls.length !== clientToolOutputs.length + deniedToolApprovalResponses.length) {
|
|
10150
|
+
steeringEndReason = "waiting-for-tool-results";
|
|
10151
|
+
return false;
|
|
10152
|
+
}
|
|
10153
|
+
if (clientToolCalls.length === 0 && pendingDeferredToolCalls.size === 0) {
|
|
10154
|
+
if (!steeringRun?.hasPendingMessages()) return false;
|
|
10155
|
+
if (stepFinishReason === "error") {
|
|
10156
|
+
steeringEndReason = "error";
|
|
10157
|
+
return false;
|
|
10158
|
+
}
|
|
10159
|
+
}
|
|
10160
|
+
if (await isStopConditionMet({
|
|
10161
|
+
stopConditions,
|
|
10162
|
+
steps: recordedSteps
|
|
10163
|
+
})) {
|
|
10164
|
+
steeringEndReason = "stop-condition";
|
|
10165
|
+
return false;
|
|
10166
|
+
}
|
|
10167
|
+
return true;
|
|
10168
|
+
};
|
|
10169
|
+
if (await shouldStartNextStep()) try {
|
|
10266
10170
|
await runInStreamTextTracingChannelContext(() => streamStep({
|
|
10267
10171
|
currentStep: currentStep + 1,
|
|
10268
10172
|
usage: combinedUsage
|
|
10269
10173
|
}));
|
|
10270
10174
|
} catch (error) {
|
|
10175
|
+
steeringRun?.end("error");
|
|
10271
10176
|
enqueueStepPart(controller, {
|
|
10272
10177
|
type: "error",
|
|
10273
10178
|
error
|
|
@@ -10275,6 +10180,7 @@ var DefaultStreamTextResult = class {
|
|
|
10275
10180
|
self.closeStream();
|
|
10276
10181
|
}
|
|
10277
10182
|
else {
|
|
10183
|
+
steeringRun?.end(steeringEndReason);
|
|
10278
10184
|
enqueueStepPart(controller, {
|
|
10279
10185
|
type: "finish",
|
|
10280
10186
|
finishReason: stepFinishReason,
|
|
@@ -10298,6 +10204,8 @@ var DefaultStreamTextResult = class {
|
|
|
10298
10204
|
usage: createNullLanguageModelUsage()
|
|
10299
10205
|
}));
|
|
10300
10206
|
})().catch(async (error) => {
|
|
10207
|
+
steeringRun?.end(abortSignal?.aborted ? "aborted" : "error");
|
|
10208
|
+
await notifyStepError(wrapGatewayError(error));
|
|
10301
10209
|
await telemetryDispatcher.onError?.({
|
|
10302
10210
|
callId,
|
|
10303
10211
|
error
|
|
@@ -10583,6 +10491,21 @@ var DefaultStreamTextResult = class {
|
|
|
10583
10491
|
}
|
|
10584
10492
|
};
|
|
10585
10493
|
//#endregion
|
|
10494
|
+
//#region src/util/is-caused-by-abort.ts
|
|
10495
|
+
/**
|
|
10496
|
+
* Checks if the error is an abort or timeout error, or is caused by one.
|
|
10497
|
+
* Providers can wrap the abort or timeout error, e.g. as the cause of an
|
|
10498
|
+
* API call error.
|
|
10499
|
+
*/
|
|
10500
|
+
function isCausedByAbort(error) {
|
|
10501
|
+
let current = error;
|
|
10502
|
+
for (let depth = 0; current != null && depth < 5; depth++) {
|
|
10503
|
+
if (isAbortError(current)) return true;
|
|
10504
|
+
current = current.cause;
|
|
10505
|
+
}
|
|
10506
|
+
return false;
|
|
10507
|
+
}
|
|
10508
|
+
//#endregion
|
|
10586
10509
|
//#region src/agent/default-stop-condition.ts
|
|
10587
10510
|
/**
|
|
10588
10511
|
* Stop conditions are only evaluated when the tool loop can otherwise continue,
|
|
@@ -10649,7 +10572,7 @@ var ToolLoopAgent = class {
|
|
|
10649
10572
|
options: validatedOptions
|
|
10650
10573
|
};
|
|
10651
10574
|
}
|
|
10652
|
-
const { onStart: _settingsStableOnStart, experimental_onStart: _settingsExperimentalOnStart, onStepStart: _settingsStableOnStepStart, experimental_onStepStart: _settingsExperimentalOnStepStart, onToolExecutionStart: _settingsOnToolExecutionStart, onToolExecutionEnd: _settingsOnToolExecutionEnd, onStepEnd: _settingsOnStepEnd, onStepFinish: _settingsOnStepFinish, onFinish: _settingsOnFinish, onEnd: _settingsOnEnd, ...settingsWithoutCallbacks } = this.settings;
|
|
10575
|
+
const { onStart: _settingsStableOnStart, experimental_onStart: _settingsExperimentalOnStart, onStepStart: _settingsStableOnStepStart, experimental_onStepStart: _settingsExperimentalOnStepStart, onToolExecutionStart: _settingsOnToolExecutionStart, onToolExecutionEnd: _settingsOnToolExecutionEnd, onStepEnd: _settingsOnStepEnd, onStepFinish: _settingsOnStepFinish, onError: _settingsOnError, onAbort: _settingsOnAbort, onFinish: _settingsOnFinish, onEnd: _settingsOnEnd, ...settingsWithoutCallbacks } = this.settings;
|
|
10653
10576
|
const baseCallArgs = {
|
|
10654
10577
|
...settingsWithoutCallbacks,
|
|
10655
10578
|
stopWhen: this.settings.stopWhen ?? createDefaultStopCondition(20),
|
|
@@ -10683,7 +10606,7 @@ var ToolLoopAgent = class {
|
|
|
10683
10606
|
/**
|
|
10684
10607
|
* Generates an output from the agent (non-streaming).
|
|
10685
10608
|
*/
|
|
10686
|
-
async generate({ abortSignal, timeout, experimental_sandbox: sandbox, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onToolExecutionStart, onToolExecutionEnd, onStepEnd, onStepFinish, onFinish, onEnd = onFinish, ...options }) {
|
|
10609
|
+
async generate({ abortSignal, timeout, experimental_sandbox: sandbox, experimental_steering: steering, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onToolExecutionStart, onToolExecutionEnd, onStepEnd, onStepFinish, onFinish, onEnd = onFinish, ...options }) {
|
|
10687
10610
|
const generate = generateText;
|
|
10688
10611
|
const preparedCall = await this.prepareCall({
|
|
10689
10612
|
...options,
|
|
@@ -10693,6 +10616,7 @@ var ToolLoopAgent = class {
|
|
|
10693
10616
|
abortSignal,
|
|
10694
10617
|
timeout: timeout ?? preparedCall.timeout,
|
|
10695
10618
|
experimental_sandbox: sandbox,
|
|
10619
|
+
experimental_steering: steering,
|
|
10696
10620
|
onStart: mergeCallbacks(this.settings.onStart ?? this.settings.experimental_onStart, onStart ?? experimental_onStart),
|
|
10697
10621
|
onStepStart: mergeCallbacks(this.settings.onStepStart ?? this.settings.experimental_onStepStart, onStepStart ?? experimental_onStepStart),
|
|
10698
10622
|
onToolExecutionStart: mergeCallbacks(this.settings.onToolExecutionStart, onToolExecutionStart),
|
|
@@ -10700,16 +10624,24 @@ var ToolLoopAgent = class {
|
|
|
10700
10624
|
onStepEnd: mergeCallbacks(this.settings.onStepEnd ?? this.settings.onStepFinish, onStepEnd ?? onStepFinish),
|
|
10701
10625
|
onEnd: mergeCallbacks(this.settings.onEnd, onEnd)
|
|
10702
10626
|
};
|
|
10703
|
-
|
|
10704
|
-
|
|
10705
|
-
|
|
10706
|
-
|
|
10707
|
-
|
|
10627
|
+
try {
|
|
10628
|
+
return await generate({
|
|
10629
|
+
...preparedCall,
|
|
10630
|
+
...callbackArgs,
|
|
10631
|
+
headers: this.agentHeaders(preparedCall)
|
|
10632
|
+
});
|
|
10633
|
+
} catch (error) {
|
|
10634
|
+
if (!isCausedByAbort(error) && !abortSignal?.aborted) await notify({
|
|
10635
|
+
event: { error },
|
|
10636
|
+
callbacks: this.settings.onError
|
|
10637
|
+
});
|
|
10638
|
+
throw error;
|
|
10639
|
+
}
|
|
10708
10640
|
}
|
|
10709
10641
|
/**
|
|
10710
10642
|
* Streams an output from the agent (streaming).
|
|
10711
10643
|
*/
|
|
10712
|
-
async stream({ abortSignal, timeout, experimental_sandbox: sandbox, experimental_transform, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onToolExecutionStart, onToolExecutionEnd, onStepEnd, onStepFinish, onFinish, onEnd = onFinish, ...options }) {
|
|
10644
|
+
async stream({ abortSignal, timeout, experimental_sandbox: sandbox, experimental_steering: steering, experimental_transform, onStart, experimental_onStart, onStepStart, experimental_onStepStart, onToolExecutionStart, onToolExecutionEnd, onStepEnd, onStepFinish, onFinish, onEnd = onFinish, ...options }) {
|
|
10713
10645
|
const stream = streamText;
|
|
10714
10646
|
const preparedCall = await this.prepareCall({
|
|
10715
10647
|
...options,
|
|
@@ -10719,13 +10651,16 @@ var ToolLoopAgent = class {
|
|
|
10719
10651
|
abortSignal,
|
|
10720
10652
|
timeout: timeout ?? preparedCall.timeout,
|
|
10721
10653
|
experimental_sandbox: sandbox,
|
|
10654
|
+
experimental_steering: steering,
|
|
10722
10655
|
experimental_transform,
|
|
10723
10656
|
onStart: mergeCallbacks(this.settings.onStart ?? this.settings.experimental_onStart, onStart ?? experimental_onStart),
|
|
10724
10657
|
onStepStart: mergeCallbacks(this.settings.onStepStart ?? this.settings.experimental_onStepStart, onStepStart ?? experimental_onStepStart),
|
|
10725
10658
|
onToolExecutionStart: mergeCallbacks(this.settings.onToolExecutionStart, onToolExecutionStart),
|
|
10726
10659
|
onToolExecutionEnd: mergeCallbacks(this.settings.onToolExecutionEnd, onToolExecutionEnd),
|
|
10727
10660
|
onStepEnd: mergeCallbacks(this.settings.onStepEnd ?? this.settings.onStepFinish, onStepEnd ?? onStepFinish),
|
|
10728
|
-
onEnd: mergeCallbacks(this.settings.onEnd, onEnd)
|
|
10661
|
+
onEnd: mergeCallbacks(this.settings.onEnd, onEnd),
|
|
10662
|
+
onError: this.settings.onError,
|
|
10663
|
+
onAbort: this.settings.onAbort
|
|
10729
10664
|
};
|
|
10730
10665
|
return await stream({
|
|
10731
10666
|
...preparedCall,
|
|
@@ -11201,7 +11136,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
11201
11136
|
//#endregion
|
|
11202
11137
|
//#region src/ui/validate-ui-messages.ts
|
|
11203
11138
|
const toolMetadataSchema = z.record(z.string(), jsonValueSchema.optional());
|
|
11204
|
-
const providerReferenceSchema = z.record(z.string(), z.string());
|
|
11139
|
+
const providerReferenceSchema$1 = z.record(z.string(), z.string());
|
|
11205
11140
|
function isEmptyObject(value) {
|
|
11206
11141
|
return value != null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0;
|
|
11207
11142
|
}
|
|
@@ -11281,7 +11216,7 @@ const uiMessagesSchema = lazySchema(() => {
|
|
|
11281
11216
|
mediaType: z.string(),
|
|
11282
11217
|
filename: z.string().optional(),
|
|
11283
11218
|
url: z.string(),
|
|
11284
|
-
providerReference: providerReferenceSchema.optional(),
|
|
11219
|
+
providerReference: providerReferenceSchema$1.optional(),
|
|
11285
11220
|
providerMetadata: providerMetadataSchema.optional()
|
|
11286
11221
|
}),
|
|
11287
11222
|
z.object({
|
|
@@ -11683,6 +11618,7 @@ async function validateUIMessagesForAgent(options) {
|
|
|
11683
11618
|
* @param abortSignal - The abort signal. Optional.
|
|
11684
11619
|
* @param timeout - Timeout in milliseconds. Optional.
|
|
11685
11620
|
* @param experimental_sandbox - The sandbox environment that is passed through to tool execution. Optional.
|
|
11621
|
+
* @param experimental_steering - A steering object that adds user messages to the run at the next step boundary. Only supported by `ToolLoopAgent`. Optional.
|
|
11686
11622
|
* @param options - The options for the agent.
|
|
11687
11623
|
* @param experimental_transform - The stream transformations. Optional.
|
|
11688
11624
|
* @param onStepEnd - Callback that is called when each step ends. Optional.
|
|
@@ -11691,7 +11627,12 @@ async function validateUIMessagesForAgent(options) {
|
|
|
11691
11627
|
*
|
|
11692
11628
|
* @returns The UI message stream.
|
|
11693
11629
|
*/
|
|
11694
|
-
async function createAgentUIStream({ agent, uiMessages, convertDataPart, options, abortSignal, timeout, experimental_sandbox: sandbox, experimental_transform, onStepEnd, onStepFinish, onUIMessageStepEnd, ...uiMessageStreamOptions }) {
|
|
11630
|
+
async function createAgentUIStream({ agent, uiMessages, convertDataPart, options, abortSignal, timeout, experimental_sandbox: sandbox, experimental_steering: steering, experimental_transform, onStepEnd, onStepFinish, onUIMessageStepEnd, ...uiMessageStreamOptions }) {
|
|
11631
|
+
if (steering != null && !(agent instanceof ToolLoopAgent)) throw new InvalidArgumentError({
|
|
11632
|
+
parameter: "experimental_steering",
|
|
11633
|
+
value: steering,
|
|
11634
|
+
message: "Steering is only supported by ToolLoopAgent."
|
|
11635
|
+
});
|
|
11695
11636
|
const validatedMessages = await validateUIMessagesForAgent({
|
|
11696
11637
|
messages: uiMessages,
|
|
11697
11638
|
tools: agent.tools
|
|
@@ -11706,6 +11647,7 @@ async function createAgentUIStream({ agent, uiMessages, convertDataPart, options
|
|
|
11706
11647
|
abortSignal,
|
|
11707
11648
|
timeout,
|
|
11708
11649
|
experimental_sandbox: sandbox,
|
|
11650
|
+
experimental_steering: steering,
|
|
11709
11651
|
experimental_transform,
|
|
11710
11652
|
onStepEnd: onStepEnd ?? onStepFinish
|
|
11711
11653
|
});
|
|
@@ -11729,6 +11671,7 @@ async function createAgentUIStream({ agent, uiMessages, convertDataPart, options
|
|
|
11729
11671
|
* @param abortSignal - Abort signal. Optional.
|
|
11730
11672
|
* @param timeout - Timeout in milliseconds. Optional.
|
|
11731
11673
|
* @param experimental_sandbox - The sandbox environment that is passed through to tool execution. Optional.
|
|
11674
|
+
* @param experimental_steering - A steering object that adds user messages to the run at the next step boundary. Only supported by `ToolLoopAgent`. Optional.
|
|
11732
11675
|
* @param options - The options for the agent. Optional.
|
|
11733
11676
|
* @param experimental_transform - Stream transformations. Optional.
|
|
11734
11677
|
* @param onStepEnd - Callback that is called when each step ends. Optional.
|
|
@@ -11764,6 +11707,7 @@ async function createAgentUIStreamResponse({ headers, status, statusText, keepAl
|
|
|
11764
11707
|
* @param abortSignal - Abort signal. Optional.
|
|
11765
11708
|
* @param timeout - Timeout in milliseconds. Optional.
|
|
11766
11709
|
* @param experimental_sandbox - The sandbox environment that is passed through to tool execution. Optional.
|
|
11710
|
+
* @param experimental_steering - A steering object that adds user messages to the run at the next step boundary. Only supported by `ToolLoopAgent`. Optional.
|
|
11767
11711
|
* @param options - The options for the agent. Optional.
|
|
11768
11712
|
* @param experimental_transform - Stream transformations. Optional.
|
|
11769
11713
|
* @param onStepEnd - Callback that is called when each step ends. Optional.
|
|
@@ -12339,11 +12283,6 @@ function validateRequests(requests) {
|
|
|
12339
12283
|
}
|
|
12340
12284
|
}
|
|
12341
12285
|
function validateBatchReference({ batchApi, batch }) {
|
|
12342
|
-
if (batch.version !== 2) throw new InvalidArgumentError({
|
|
12343
|
-
parameter: "batch",
|
|
12344
|
-
value: batch,
|
|
12345
|
-
message: "batch must be a supported batch reference"
|
|
12346
|
-
});
|
|
12347
12286
|
if (batch.provider !== batchApi.provider) throw new InvalidArgumentError({
|
|
12348
12287
|
parameter: "provider",
|
|
12349
12288
|
value: batchApi,
|
|
@@ -13059,19 +12998,20 @@ function isRecord$1(value) {
|
|
|
13059
12998
|
const prototype = Object.getPrototypeOf(value);
|
|
13060
12999
|
return prototype === Object.prototype || prototype === null;
|
|
13061
13000
|
}
|
|
13062
|
-
|
|
13063
|
-
|
|
13001
|
+
/**
|
|
13002
|
+
* Checks the JSON constraints that types cannot express: finite numbers, no
|
|
13003
|
+
* cycles, and no array holes. These would otherwise be silently changed by
|
|
13004
|
+
* `JSON.stringify` or make it throw.
|
|
13005
|
+
*/
|
|
13006
|
+
function isSerializableJSON(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
13064
13007
|
if (typeof value === "number") return Number.isFinite(value);
|
|
13065
|
-
if (typeof value !== "object"
|
|
13008
|
+
if (value == null || typeof value !== "object") return true;
|
|
13066
13009
|
if (ancestors.has(value)) return false;
|
|
13067
13010
|
ancestors.add(value);
|
|
13068
|
-
const valid =
|
|
13011
|
+
const valid = Array.isArray(value) ? Array.from(value.keys()).every((index) => index in value && isSerializableJSON(value[index], ancestors)) : Object.values(value).every((item) => isSerializableJSON(item, ancestors));
|
|
13069
13012
|
ancestors.delete(value);
|
|
13070
13013
|
return valid;
|
|
13071
13014
|
}
|
|
13072
|
-
function isInput(value) {
|
|
13073
|
-
return (typeof value === "string" || Array.isArray(value) || isRecord$1(value)) && isJSON(value);
|
|
13074
|
-
}
|
|
13075
13015
|
function invalidInput(parameter, value, message) {
|
|
13076
13016
|
throw new InvalidArgumentError({
|
|
13077
13017
|
parameter,
|
|
@@ -13080,36 +13020,26 @@ function invalidInput(parameter, value, message) {
|
|
|
13080
13020
|
});
|
|
13081
13021
|
}
|
|
13082
13022
|
function validateDecisionInput({ state, questions }) {
|
|
13083
|
-
if (
|
|
13084
|
-
|
|
13085
|
-
|
|
13086
|
-
case "text": return textPartSchema.safeParse(part).success;
|
|
13087
|
-
case "file": return filePartSchema.safeParse(part).success;
|
|
13088
|
-
case "json": return isJSON(part.value);
|
|
13089
|
-
default: return false;
|
|
13090
|
-
}
|
|
13091
|
-
}) : (typeof state === "string" || isRecord$1(state)) && isJSON(state))) invalidInput("state", state, "must be a string, JSON object, or array of text, file, or json parts");
|
|
13092
|
-
if (!isRecord$1(questions) || Object.keys(questions).length === 0) invalidInput("questions", questions, "must be a nonempty question map");
|
|
13023
|
+
if (Array.isArray(state) && Array.from(state.keys()).some((index) => !(index in state))) invalidInput("state", state, "state parts must not contain array holes");
|
|
13024
|
+
if (!isSerializableJSON(Array.isArray(state) ? state.flatMap((part) => part.type === "json" ? [part.value] : []) : state)) invalidInput("state", state, "JSON content must not contain non-finite numbers, cycles, or array holes");
|
|
13025
|
+
if (Object.keys(questions).length === 0) invalidInput("questions", questions, "must be a nonempty question map");
|
|
13093
13026
|
for (const [id, question] of Object.entries(questions)) {
|
|
13094
13027
|
const parameter = `questions.${id}`;
|
|
13095
|
-
if (!
|
|
13096
|
-
if (question.type === "box") continue;
|
|
13097
|
-
const criteria = question.criteria;
|
|
13028
|
+
if (!isSerializableJSON(question.instructions) || "criteria" in question && !isSerializableJSON(question.criteria)) invalidInput(parameter, question, "instructions and criteria must not contain non-finite numbers, cycles, or array holes");
|
|
13098
13029
|
switch (question.type) {
|
|
13099
13030
|
case "choice":
|
|
13100
|
-
if (
|
|
13031
|
+
if (Object.keys(question.criteria).length === 0) invalidInput(parameter, question, "choice criteria must be a nonempty option map");
|
|
13101
13032
|
break;
|
|
13102
13033
|
case "score":
|
|
13103
|
-
if (
|
|
13034
|
+
if (question.criteria.length === 0) invalidInput(parameter, question, "score criteria must contain at least one ordered level");
|
|
13104
13035
|
break;
|
|
13105
|
-
case "boolean":
|
|
13106
|
-
|
|
13107
|
-
if (criteria
|
|
13108
|
-
if (
|
|
13036
|
+
case "boolean": {
|
|
13037
|
+
const criteria = question.criteria;
|
|
13038
|
+
if (question.instructions == null && (criteria == null || !Object.values(criteria).some((value) => value != null))) invalidInput(parameter, question, "boolean questions require instructions or at least one non-null criterion");
|
|
13039
|
+
if (criteria != null && Object.keys(criteria).some((key) => key !== "true" && key !== "false")) invalidInput(parameter, question, "boolean criteria may only describe true and false");
|
|
13109
13040
|
break;
|
|
13110
|
-
|
|
13041
|
+
}
|
|
13111
13042
|
}
|
|
13112
|
-
if (!isJSON(criteria) || Object.values(criteria).some((value) => value !== null && !isInput(value))) invalidInput(parameter, question, "criteria descriptions must be JSON-compatible strings, objects, arrays, or null");
|
|
13113
13043
|
}
|
|
13114
13044
|
}
|
|
13115
13045
|
function invalidAnswer(answers, message) {
|
|
@@ -13612,11 +13542,6 @@ async function parseAndValidateObjectResultWithRepair(result, outputStrategy, re
|
|
|
13612
13542
|
//#endregion
|
|
13613
13543
|
//#region src/generate-object/validate-object-generation-input.ts
|
|
13614
13544
|
function validateObjectGenerationInput({ output, schema, schemaName, schemaDescription, enumValues }) {
|
|
13615
|
-
if (output != null && output !== "object" && output !== "array" && output !== "enum" && output !== "no-schema") throw new InvalidArgumentError({
|
|
13616
|
-
parameter: "output",
|
|
13617
|
-
value: output,
|
|
13618
|
-
message: "Invalid output type."
|
|
13619
|
-
});
|
|
13620
13545
|
if (output === "no-schema") {
|
|
13621
13546
|
if (schema != null) throw new InvalidArgumentError({
|
|
13622
13547
|
parameter: "schema",
|
|
@@ -13640,11 +13565,6 @@ function validateObjectGenerationInput({ output, schema, schemaName, schemaDescr
|
|
|
13640
13565
|
});
|
|
13641
13566
|
}
|
|
13642
13567
|
if (output === "object") {
|
|
13643
|
-
if (schema == null) throw new InvalidArgumentError({
|
|
13644
|
-
parameter: "schema",
|
|
13645
|
-
value: schema,
|
|
13646
|
-
message: "Schema is required for object output."
|
|
13647
|
-
});
|
|
13648
13568
|
if (enumValues != null) throw new InvalidArgumentError({
|
|
13649
13569
|
parameter: "enumValues",
|
|
13650
13570
|
value: enumValues,
|
|
@@ -13652,11 +13572,6 @@ function validateObjectGenerationInput({ output, schema, schemaName, schemaDescr
|
|
|
13652
13572
|
});
|
|
13653
13573
|
}
|
|
13654
13574
|
if (output === "array") {
|
|
13655
|
-
if (schema == null) throw new InvalidArgumentError({
|
|
13656
|
-
parameter: "schema",
|
|
13657
|
-
value: schema,
|
|
13658
|
-
message: "Element schema is required for array output."
|
|
13659
|
-
});
|
|
13660
13575
|
if (enumValues != null) throw new InvalidArgumentError({
|
|
13661
13576
|
parameter: "enumValues",
|
|
13662
13577
|
value: enumValues,
|
|
@@ -13679,12 +13594,7 @@ function validateObjectGenerationInput({ output, schema, schemaName, schemaDescr
|
|
|
13679
13594
|
value: schemaName,
|
|
13680
13595
|
message: "Schema name is not supported for enum output."
|
|
13681
13596
|
});
|
|
13682
|
-
|
|
13683
|
-
parameter: "enumValues",
|
|
13684
|
-
value: enumValues,
|
|
13685
|
-
message: "Enum values are required for enum output."
|
|
13686
|
-
});
|
|
13687
|
-
for (const value of enumValues) if (typeof value !== "string") throw new InvalidArgumentError({
|
|
13597
|
+
for (const value of enumValues ?? []) if (typeof value !== "string") throw new InvalidArgumentError({
|
|
13688
13598
|
parameter: "enumValues",
|
|
13689
13599
|
value,
|
|
13690
13600
|
message: "Enum values must be strings."
|
|
@@ -15094,17 +15004,10 @@ function toTextStream$1(text) {
|
|
|
15094
15004
|
function toSourceStream(text) {
|
|
15095
15005
|
if (text instanceof ReadableStream) return text;
|
|
15096
15006
|
if (typeof text?.[Symbol.asyncIterator] === "function") return convertAsyncIteratorToReadableStream(text[Symbol.asyncIterator]());
|
|
15097
|
-
|
|
15098
|
-
|
|
15099
|
-
|
|
15100
|
-
|
|
15101
|
-
})());
|
|
15102
|
-
}
|
|
15103
|
-
throw new InvalidArgumentError({
|
|
15104
|
-
parameter: "text",
|
|
15105
|
-
value: text,
|
|
15106
|
-
message: "Expected a string, an iterable or async iterable, or a ReadableStream of strings or text parts."
|
|
15107
|
-
});
|
|
15007
|
+
const iterable = text;
|
|
15008
|
+
return convertAsyncIteratorToReadableStream((async function* () {
|
|
15009
|
+
yield* iterable;
|
|
15010
|
+
})());
|
|
15108
15011
|
}
|
|
15109
15012
|
function toTextPart(value) {
|
|
15110
15013
|
if (typeof value === "string") return {
|
|
@@ -15254,11 +15157,7 @@ function smoothStream({ delayInMs = 10, chunking = "word", _internal: { delay: d
|
|
|
15254
15157
|
return match;
|
|
15255
15158
|
};
|
|
15256
15159
|
else {
|
|
15257
|
-
const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking
|
|
15258
|
-
if (chunkingRegex == null) throw new InvalidArgumentError$1({
|
|
15259
|
-
argument: "chunking",
|
|
15260
|
-
message: `Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`
|
|
15261
|
-
});
|
|
15160
|
+
const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
|
|
15262
15161
|
detectChunk = (buffer) => {
|
|
15263
15162
|
const lastIndex = chunkingRegex.lastIndex;
|
|
15264
15163
|
chunkingRegex.lastIndex = 0;
|
|
@@ -16586,6 +16485,285 @@ function wrapProvider({ provider, languageModelMiddleware, imageModelMiddleware
|
|
|
16586
16485
|
};
|
|
16587
16486
|
}
|
|
16588
16487
|
//#endregion
|
|
16488
|
+
//#region src/prompt/content-part.ts
|
|
16489
|
+
const fileInlineDataSchema = z.union([
|
|
16490
|
+
z.string(),
|
|
16491
|
+
z.instanceof(Uint8Array),
|
|
16492
|
+
z.instanceof(ArrayBuffer),
|
|
16493
|
+
z.custom(isBuffer, { message: "Must be a Buffer" })
|
|
16494
|
+
]);
|
|
16495
|
+
const providerReferenceSchema = z.record(z.string(), z.string());
|
|
16496
|
+
/**
|
|
16497
|
+
* @internal
|
|
16498
|
+
*/
|
|
16499
|
+
const textPartSchema = z.object({
|
|
16500
|
+
type: z.literal("text"),
|
|
16501
|
+
text: z.string(),
|
|
16502
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16503
|
+
});
|
|
16504
|
+
/**
|
|
16505
|
+
* @internal
|
|
16506
|
+
* @deprecated Use `filePartSchema` with `mediaType: 'image'` instead:
|
|
16507
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
|
|
16508
|
+
*/
|
|
16509
|
+
const imagePartSchema = z.object({
|
|
16510
|
+
type: z.literal("image"),
|
|
16511
|
+
image: z.union([
|
|
16512
|
+
fileInlineDataSchema,
|
|
16513
|
+
z.instanceof(URL),
|
|
16514
|
+
providerReferenceSchema
|
|
16515
|
+
]),
|
|
16516
|
+
mediaType: z.string().optional(),
|
|
16517
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16518
|
+
});
|
|
16519
|
+
const taggedFileDataSchema = z.discriminatedUnion("type", [
|
|
16520
|
+
z.object({
|
|
16521
|
+
type: z.literal("data"),
|
|
16522
|
+
data: fileInlineDataSchema
|
|
16523
|
+
}),
|
|
16524
|
+
z.object({
|
|
16525
|
+
type: z.literal("url"),
|
|
16526
|
+
url: z.instanceof(URL)
|
|
16527
|
+
}),
|
|
16528
|
+
z.object({
|
|
16529
|
+
type: z.literal("reference"),
|
|
16530
|
+
reference: providerReferenceSchema
|
|
16531
|
+
}),
|
|
16532
|
+
z.object({
|
|
16533
|
+
type: z.literal("text"),
|
|
16534
|
+
text: z.string()
|
|
16535
|
+
})
|
|
16536
|
+
]);
|
|
16537
|
+
const taggedReasoningFileDataSchema = z.discriminatedUnion("type", [z.object({
|
|
16538
|
+
type: z.literal("data"),
|
|
16539
|
+
data: fileInlineDataSchema
|
|
16540
|
+
}), z.object({
|
|
16541
|
+
type: z.literal("url"),
|
|
16542
|
+
url: z.instanceof(URL)
|
|
16543
|
+
})]);
|
|
16544
|
+
/**
|
|
16545
|
+
* @internal
|
|
16546
|
+
*/
|
|
16547
|
+
const filePartSchema = z.object({
|
|
16548
|
+
type: z.literal("file"),
|
|
16549
|
+
data: z.union([
|
|
16550
|
+
taggedFileDataSchema,
|
|
16551
|
+
fileInlineDataSchema,
|
|
16552
|
+
z.instanceof(URL),
|
|
16553
|
+
providerReferenceSchema
|
|
16554
|
+
]),
|
|
16555
|
+
filename: z.string().optional(),
|
|
16556
|
+
mediaType: z.string(),
|
|
16557
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16558
|
+
});
|
|
16559
|
+
/**
|
|
16560
|
+
* @internal
|
|
16561
|
+
*/
|
|
16562
|
+
const reasoningPartSchema = z.object({
|
|
16563
|
+
type: z.literal("reasoning"),
|
|
16564
|
+
text: z.string(),
|
|
16565
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16566
|
+
});
|
|
16567
|
+
/**
|
|
16568
|
+
* @internal
|
|
16569
|
+
*/
|
|
16570
|
+
const customPartSchema = z.object({
|
|
16571
|
+
type: z.literal("custom"),
|
|
16572
|
+
kind: z.string().transform((value) => value),
|
|
16573
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16574
|
+
});
|
|
16575
|
+
/**
|
|
16576
|
+
* @internal
|
|
16577
|
+
*/
|
|
16578
|
+
const reasoningFilePartSchema = z.object({
|
|
16579
|
+
type: z.literal("reasoning-file"),
|
|
16580
|
+
data: z.union([
|
|
16581
|
+
taggedReasoningFileDataSchema,
|
|
16582
|
+
fileInlineDataSchema,
|
|
16583
|
+
z.instanceof(URL)
|
|
16584
|
+
]),
|
|
16585
|
+
mediaType: z.string(),
|
|
16586
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16587
|
+
});
|
|
16588
|
+
/**
|
|
16589
|
+
* @internal
|
|
16590
|
+
*/
|
|
16591
|
+
const toolCallPartSchema = z.object({
|
|
16592
|
+
type: z.literal("tool-call"),
|
|
16593
|
+
toolCallId: z.string(),
|
|
16594
|
+
toolName: z.string(),
|
|
16595
|
+
input: z.unknown(),
|
|
16596
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
16597
|
+
providerExecuted: z.boolean().optional()
|
|
16598
|
+
});
|
|
16599
|
+
/**
|
|
16600
|
+
* @internal
|
|
16601
|
+
*/
|
|
16602
|
+
const outputSchema = z.discriminatedUnion("type", [
|
|
16603
|
+
z.object({
|
|
16604
|
+
type: z.literal("text"),
|
|
16605
|
+
value: z.string(),
|
|
16606
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16607
|
+
}),
|
|
16608
|
+
z.object({
|
|
16609
|
+
type: z.literal("json"),
|
|
16610
|
+
value: jsonValueSchema,
|
|
16611
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16612
|
+
}),
|
|
16613
|
+
z.object({
|
|
16614
|
+
type: z.literal("execution-denied"),
|
|
16615
|
+
reason: z.string().optional(),
|
|
16616
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16617
|
+
}),
|
|
16618
|
+
z.object({
|
|
16619
|
+
type: z.literal("error-text"),
|
|
16620
|
+
value: z.string(),
|
|
16621
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16622
|
+
}),
|
|
16623
|
+
z.object({
|
|
16624
|
+
type: z.literal("error-json"),
|
|
16625
|
+
value: jsonValueSchema,
|
|
16626
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16627
|
+
}),
|
|
16628
|
+
z.object({
|
|
16629
|
+
type: z.literal("content"),
|
|
16630
|
+
value: z.array(z.union([
|
|
16631
|
+
z.object({
|
|
16632
|
+
type: z.literal("text"),
|
|
16633
|
+
text: z.string(),
|
|
16634
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16635
|
+
}),
|
|
16636
|
+
z.object({
|
|
16637
|
+
type: z.literal("file"),
|
|
16638
|
+
data: taggedFileDataSchema,
|
|
16639
|
+
mediaType: z.string(),
|
|
16640
|
+
filename: z.string().optional(),
|
|
16641
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16642
|
+
}),
|
|
16643
|
+
z.object({
|
|
16644
|
+
type: z.literal("file-data"),
|
|
16645
|
+
data: z.string(),
|
|
16646
|
+
mediaType: z.string(),
|
|
16647
|
+
filename: z.string().optional(),
|
|
16648
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16649
|
+
}),
|
|
16650
|
+
z.object({
|
|
16651
|
+
type: z.literal("file-url"),
|
|
16652
|
+
url: z.string(),
|
|
16653
|
+
mediaType: z.string().optional(),
|
|
16654
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16655
|
+
}),
|
|
16656
|
+
z.object({
|
|
16657
|
+
type: z.literal("file-id"),
|
|
16658
|
+
fileId: z.union([z.string(), z.record(z.string(), z.string())]),
|
|
16659
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16660
|
+
}),
|
|
16661
|
+
z.object({
|
|
16662
|
+
type: z.literal("file-reference"),
|
|
16663
|
+
providerReference: z.record(z.string(), z.string()),
|
|
16664
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16665
|
+
}),
|
|
16666
|
+
z.object({
|
|
16667
|
+
type: z.literal("image-data"),
|
|
16668
|
+
data: z.string(),
|
|
16669
|
+
mediaType: z.string(),
|
|
16670
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16671
|
+
}),
|
|
16672
|
+
z.object({
|
|
16673
|
+
type: z.literal("image-url"),
|
|
16674
|
+
url: z.string(),
|
|
16675
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16676
|
+
}),
|
|
16677
|
+
z.object({
|
|
16678
|
+
type: z.literal("image-file-id"),
|
|
16679
|
+
fileId: z.union([z.string(), z.record(z.string(), z.string())]),
|
|
16680
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16681
|
+
}),
|
|
16682
|
+
z.object({
|
|
16683
|
+
type: z.literal("image-file-reference"),
|
|
16684
|
+
providerReference: z.record(z.string(), z.string()),
|
|
16685
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16686
|
+
}),
|
|
16687
|
+
z.object({
|
|
16688
|
+
type: z.literal("custom"),
|
|
16689
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16690
|
+
})
|
|
16691
|
+
]))
|
|
16692
|
+
})
|
|
16693
|
+
]);
|
|
16694
|
+
/**
|
|
16695
|
+
* @internal
|
|
16696
|
+
*/
|
|
16697
|
+
const toolResultPartSchema = z.object({
|
|
16698
|
+
type: z.literal("tool-result"),
|
|
16699
|
+
toolCallId: z.string(),
|
|
16700
|
+
toolName: z.string(),
|
|
16701
|
+
output: outputSchema,
|
|
16702
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16703
|
+
});
|
|
16704
|
+
/**
|
|
16705
|
+
* @internal
|
|
16706
|
+
*/
|
|
16707
|
+
const toolApprovalRequestSchema = z.object({
|
|
16708
|
+
type: z.literal("tool-approval-request"),
|
|
16709
|
+
approvalId: z.string(),
|
|
16710
|
+
toolCallId: z.string(),
|
|
16711
|
+
reason: z.string().optional(),
|
|
16712
|
+
isAutomatic: z.boolean().optional(),
|
|
16713
|
+
signature: z.string().optional(),
|
|
16714
|
+
inputSchemaInput: z.unknown().optional()
|
|
16715
|
+
});
|
|
16716
|
+
/**
|
|
16717
|
+
* @internal
|
|
16718
|
+
*/
|
|
16719
|
+
const toolApprovalResponseSchema = z.object({
|
|
16720
|
+
type: z.literal("tool-approval-response"),
|
|
16721
|
+
approvalId: z.string(),
|
|
16722
|
+
approved: z.boolean(),
|
|
16723
|
+
reason: z.string().optional()
|
|
16724
|
+
});
|
|
16725
|
+
//#endregion
|
|
16726
|
+
//#region src/prompt/message.ts
|
|
16727
|
+
const systemModelMessageSchema = z.object({
|
|
16728
|
+
role: z.literal("system"),
|
|
16729
|
+
content: z.string(),
|
|
16730
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16731
|
+
});
|
|
16732
|
+
const userModelMessageSchema = z.object({
|
|
16733
|
+
role: z.literal("user"),
|
|
16734
|
+
content: z.union([z.string(), z.array(z.union([
|
|
16735
|
+
textPartSchema,
|
|
16736
|
+
imagePartSchema,
|
|
16737
|
+
filePartSchema
|
|
16738
|
+
]))]),
|
|
16739
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16740
|
+
});
|
|
16741
|
+
const assistantModelMessageSchema = z.object({
|
|
16742
|
+
role: z.literal("assistant"),
|
|
16743
|
+
content: z.union([z.string(), z.array(z.union([
|
|
16744
|
+
textPartSchema,
|
|
16745
|
+
customPartSchema,
|
|
16746
|
+
filePartSchema,
|
|
16747
|
+
reasoningPartSchema,
|
|
16748
|
+
reasoningFilePartSchema,
|
|
16749
|
+
toolCallPartSchema,
|
|
16750
|
+
toolResultPartSchema,
|
|
16751
|
+
toolApprovalRequestSchema
|
|
16752
|
+
]))]),
|
|
16753
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16754
|
+
});
|
|
16755
|
+
const toolModelMessageSchema = z.object({
|
|
16756
|
+
role: z.literal("tool"),
|
|
16757
|
+
content: z.array(z.union([toolResultPartSchema, toolApprovalResponseSchema])),
|
|
16758
|
+
providerOptions: providerMetadataSchema.optional()
|
|
16759
|
+
});
|
|
16760
|
+
const modelMessageSchema = z.union([
|
|
16761
|
+
systemModelMessageSchema,
|
|
16762
|
+
userModelMessageSchema,
|
|
16763
|
+
assistantModelMessageSchema,
|
|
16764
|
+
toolModelMessageSchema
|
|
16765
|
+
]);
|
|
16766
|
+
//#endregion
|
|
16589
16767
|
//#region src/realtime/audio-utils.ts
|
|
16590
16768
|
/**
|
|
16591
16769
|
* Converts Float32 audio samples to a base64-encoded PCM16 string
|
|
@@ -21501,6 +21679,6 @@ async function uploadSkill({ api, files, displayTitle, providerOptions }) {
|
|
|
21501
21679
|
});
|
|
21502
21680
|
}
|
|
21503
21681
|
//#endregion
|
|
21504
|
-
export { AISDKError, AI_SDK_TELEMETRY_TRACING_CHANNEL, APICallError, AbstractChat, DefaultChatTransport, DefaultGeneratedFile, DirectChatTransport, DownloadError, EmptyResponseBodyError, AbstractRealtimeSession as Experimental_AbstractRealtimeSession, ToolLoopAgent as Experimental_Agent, ToolLoopAgent, Experimental_DecisionRefusalError, Experimental_DecisionUnsupportedQuestionTypeError, Experimental_EvaluationUnsupportedQuestionTypeError, HttpChatTransport, InvalidArgumentError, InvalidDataContentError, InvalidMessageRoleError, InvalidPromptError, InvalidResponseDataError, InvalidStreamPartError, InvalidToolApprovalError, InvalidToolApprovalSignatureError, InvalidToolInputError, JSONParseError, JsonToSseTransformStream, LoadAPIKeyError, LoadSettingError, MessageConversionError, MissingToolResultsError, NoContentGeneratedError, NoImageGeneratedError, NoObjectGeneratedError, NoOutputGeneratedError, NoSpeechGeneratedError, NoSuchModelError, NoSuchProviderError, NoSuchProviderReferenceError, NoSuchToolError, NoTranscriptGeneratedError, NoTranslationGeneratedError, NoVideoGeneratedError, output_exports as Output, RetryError, SerialJobExecutor, StreamProviderError, TextStreamChatTransport, TooManyEmbeddingValuesForCallError, ToolCallNotFoundForApprovalError, ToolCallRepairError, ToolChoiceViolationError, TypeValidationError, UIMessageStreamError, UI_MESSAGE_STREAM_HEADERS, UnsupportedFunctionalityError, UnsupportedModelVersionError, WebSocketChatTransport, addToolInputExamplesMiddleware, asSchema, assistantModelMessageSchema, callCompletionApi, consumeStream, convertDataContentToBase64String, convertFileListToFileUIParts, convertToModelMessages, cosineSimilarity, createAgentUIStream, createAgentUIStreamResponse, createDownload, createGateway, createIdGenerator, createProviderRegistry, createTextStreamResponse, createUIMessageStream, createUIMessageStreamResponse, customProvider, defaultEmbeddingSettingsMiddleware, defaultInstructionsMiddleware, defaultSettingsMiddleware, detectToolDrift, dynamicTool, embed, embedMany, cancelBatch as experimental_cancelBatch, experimental_createProviderRegistry, decide as experimental_decide, decodeRealtimeAudio as experimental_decodeRealtimeAudio, encodeRealtimeAudio as experimental_encodeRealtimeAudio, experimental_evaluate, filterActiveTools as experimental_filterActiveTools, experimental_generateSpeech, experimental_generateVideo, getBatchResults as experimental_getBatchResults, getBatchStatus as experimental_getBatchStatus, getRealtimeToolDefinitions as experimental_getRealtimeToolDefinitions, experimental_getVideoStatus, listBatches as experimental_listBatches, resampleAudio as experimental_resampleAudio, startBatch as experimental_startBatch, experimental_startVideo, streamLanguageModelCall as experimental_streamLanguageModelCall, streamSpeech as experimental_streamSpeech, streamTextLowMemory as experimental_streamTextLowMemory, streamTranscribe as experimental_streamTranscribe, streamTranslate as experimental_streamTranslate, experimental_toolCaller, experimental_transcribe, extractJsonMiddleware, extractReasoningMiddleware, fingerprintTools, gateway, generateId, generateImage, generateObject, generateSpeech, generateText, getChunkTimeoutMs, getFirstChunkTimeoutMs, getStaticToolName, getStepTimeoutMs, getTextFromDataUrl, getToolName, getToolOrDynamicToolName, getToolTimeoutMs, getTotalTimeoutMs, hasToolCall, isCustomContentUIPart, isDataUIPart, isDeepEqualData, isDynamicToolUIPart, isFileUIPart, isLoopFinished, isReasoningFileUIPart, isReasoningUIPart, isStaticToolUIPart, isStepCount, isStepCount as stepCountIs, isTextUIPart, isToolOutputErrorUIPart, isToolUIPart, jsonSchema, lastAssistantMessageIsCompleteWithApprovalResponses, lastAssistantMessageIsCompleteWithToolCalls, modelMessageSchema, parseJsonEventStream, parsePartialJson, pipeAgentUIStreamToResponse, pipeTextStreamToResponse, pipeUIMessageStreamToResponse, pruneMessages, readUIMessageStream, registerTelemetry, rerank, safeValidateUIMessages, safeValidateWebSocketChatTransportRequest, simulateReadableStream, simulateStreamingMiddleware, smoothStream, streamObject, streamText, systemModelMessageSchema, toTextStream, toUIMessageChunk, toUIMessageStream, tool, toolModelMessageSchema, toolSearch, transcribe, uiMessageChunkSchema, uploadFile, uploadSkill, userModelMessageSchema, validateUIMessages, wrapEmbeddingModel, wrapImageModel, wrapLanguageModel, wrapProvider, zodSchema };
|
|
21682
|
+
export { AISDKError, AI_SDK_TELEMETRY_TRACING_CHANNEL, APICallError, AbstractChat, DefaultChatTransport, DefaultGeneratedFile, DirectChatTransport, DownloadError, EmptyResponseBodyError, AbstractRealtimeSession as Experimental_AbstractRealtimeSession, ToolLoopAgent as Experimental_Agent, ToolLoopAgent, Experimental_DecisionRefusalError, Experimental_DecisionUnsupportedQuestionTypeError, Experimental_EvaluationUnsupportedQuestionTypeError, HttpChatTransport, InvalidArgumentError, InvalidDataContentError, InvalidMessageRoleError, InvalidPromptError, InvalidResponseDataError, InvalidStreamPartError, InvalidToolApprovalError, InvalidToolApprovalSignatureError, InvalidToolInputError, JSONParseError, JsonToSseTransformStream, LoadAPIKeyError, LoadSettingError, MessageConversionError, MissingToolResultsError, NoContentGeneratedError, NoImageGeneratedError, NoObjectGeneratedError, NoOutputGeneratedError, NoSpeechGeneratedError, NoSuchModelError, NoSuchProviderError, NoSuchProviderReferenceError, NoSuchToolError, NoTranscriptGeneratedError, NoTranslationGeneratedError, NoVideoGeneratedError, output_exports as Output, RetryError, SerialJobExecutor, StreamProviderError, TextStreamChatTransport, TooManyEmbeddingValuesForCallError, ToolCallNotFoundForApprovalError, ToolCallRepairError, ToolChoiceViolationError, TypeValidationError, UIMessageStreamError, UI_MESSAGE_STREAM_HEADERS, UnsupportedFunctionalityError, UnsupportedModelVersionError, WebSocketChatTransport, addToolInputExamplesMiddleware, asSchema, assistantModelMessageSchema, callCompletionApi, consumeStream, convertDataContentToBase64String, convertFileListToFileUIParts, convertToModelMessages, cosineSimilarity, createAgentUIStream, createAgentUIStreamResponse, createDownload, createGateway, createIdGenerator, createProviderRegistry, createTextStreamResponse, createUIMessageStream, createUIMessageStreamResponse, customProvider, defaultEmbeddingSettingsMiddleware, defaultInstructionsMiddleware, defaultSettingsMiddleware, detectToolDrift, dynamicTool, embed, embedMany, cancelBatch as experimental_cancelBatch, experimental_createProviderRegistry, createSteering as experimental_createSteering, decide as experimental_decide, decodeRealtimeAudio as experimental_decodeRealtimeAudio, encodeRealtimeAudio as experimental_encodeRealtimeAudio, experimental_evaluate, filterActiveTools as experimental_filterActiveTools, experimental_generateSpeech, experimental_generateVideo, getBatchResults as experimental_getBatchResults, getBatchStatus as experimental_getBatchStatus, getRealtimeToolDefinitions as experimental_getRealtimeToolDefinitions, experimental_getVideoStatus, listBatches as experimental_listBatches, resampleAudio as experimental_resampleAudio, startBatch as experimental_startBatch, experimental_startVideo, streamLanguageModelCall as experimental_streamLanguageModelCall, streamSpeech as experimental_streamSpeech, streamTextLowMemory as experimental_streamTextLowMemory, streamTranscribe as experimental_streamTranscribe, streamTranslate as experimental_streamTranslate, experimental_toolCaller, experimental_transcribe, extractJsonMiddleware, extractReasoningMiddleware, fingerprintTools, gateway, generateId, generateImage, generateObject, generateSpeech, generateText, getChunkTimeoutMs, getFirstChunkTimeoutMs, getStaticToolName, getStepTimeoutMs, getTextFromDataUrl, getToolName, getToolOrDynamicToolName, getToolTimeoutMs, getTotalTimeoutMs, hasToolCall, isCustomContentUIPart, isDataUIPart, isDeepEqualData, isDynamicToolUIPart, isFileUIPart, isLoopFinished, isReasoningFileUIPart, isReasoningUIPart, isStaticToolUIPart, isStepCount, isStepCount as stepCountIs, isTextUIPart, isToolOutputErrorUIPart, isToolUIPart, jsonSchema, lastAssistantMessageIsCompleteWithApprovalResponses, lastAssistantMessageIsCompleteWithToolCalls, modelMessageSchema, parseJsonEventStream, parsePartialJson, pipeAgentUIStreamToResponse, pipeTextStreamToResponse, pipeUIMessageStreamToResponse, pruneMessages, readUIMessageStream, registerTelemetry, rerank, safeValidateUIMessages, safeValidateWebSocketChatTransportRequest, simulateReadableStream, simulateStreamingMiddleware, smoothStream, streamObject, streamText, systemModelMessageSchema, toTextStream, toUIMessageChunk, toUIMessageStream, tool, toolModelMessageSchema, toolSearch, transcribe, uiMessageChunkSchema, uploadFile, uploadSkill, userModelMessageSchema, validateUIMessages, wrapEmbeddingModel, wrapImageModel, wrapLanguageModel, wrapProvider, zodSchema };
|
|
21505
21683
|
|
|
21506
21684
|
//# sourceMappingURL=index.js.map
|