ai
Version:
AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
1,788 lines (1,746 loc) • 260 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name17 in all)
__defProp(target, name17, { get: all[name17], enumerable: true });
};
// core/index.ts
import { createIdGenerator as createIdGenerator5, generateId as generateId2 } from "@ai-sdk/provider-utils";
import {
formatAssistantStreamPart,
formatDataStreamPart as formatDataStreamPart3,
jsonSchema as jsonSchema2,
parseAssistantStreamPart,
parseDataStreamPart,
processDataStream,
processTextStream,
zodSchema
} from "@ai-sdk/ui-utils";
// core/data-stream/create-data-stream.ts
import { formatDataStreamPart } from "@ai-sdk/ui-utils";
function createDataStream({
execute,
onError = () => "An error occurred."
// mask error messages for safety by default
}) {
let controller;
const ongoingStreamPromises = [];
const stream = new ReadableStream({
start(controllerArg) {
controller = controllerArg;
}
});
function safeEnqueue(data) {
try {
controller.enqueue(data);
} catch (error) {
}
}
try {
const result = execute({
write(data) {
safeEnqueue(data);
},
writeData(data) {
safeEnqueue(formatDataStreamPart("data", [data]));
},
writeMessageAnnotation(annotation) {
safeEnqueue(formatDataStreamPart("message_annotations", [annotation]));
},
writeSource(source) {
safeEnqueue(formatDataStreamPart("source", source));
},
merge(streamArg) {
ongoingStreamPromises.push(
(async () => {
const reader = streamArg.getReader();
while (true) {
const { done, value } = await reader.read();
if (done)
break;
safeEnqueue(value);
}
})().catch((error) => {
safeEnqueue(formatDataStreamPart("error", onError(error)));
})
);
},
onError
});
if (result) {
ongoingStreamPromises.push(
result.catch((error) => {
safeEnqueue(formatDataStreamPart("error", onError(error)));
})
);
}
} catch (error) {
safeEnqueue(formatDataStreamPart("error", onError(error)));
}
const waitForStreams = new Promise(async (resolve) => {
while (ongoingStreamPromises.length > 0) {
await ongoingStreamPromises.shift();
}
resolve();
});
waitForStreams.finally(() => {
try {
controller.close();
} catch (error) {
}
});
return stream;
}
// core/util/prepare-response-headers.ts
function prepareResponseHeaders(headers, {
contentType,
dataStreamVersion
}) {
const responseHeaders = new Headers(headers != null ? headers : {});
if (!responseHeaders.has("Content-Type")) {
responseHeaders.set("Content-Type", contentType);
}
if (dataStreamVersion !== void 0) {
responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
}
return responseHeaders;
}
// core/data-stream/create-data-stream-response.ts
function createDataStreamResponse({
status,
statusText,
headers,
execute,
onError
}) {
return new Response(
createDataStream({ execute, onError }).pipeThrough(new TextEncoderStream()),
{
status,
statusText,
headers: prepareResponseHeaders(headers, {
contentType: "text/plain; charset=utf-8",
dataStreamVersion: "v1"
})
}
);
}
// core/util/prepare-outgoing-http-headers.ts
function prepareOutgoingHttpHeaders(headers, {
contentType,
dataStreamVersion
}) {
const outgoingHeaders = {};
if (headers != null) {
for (const [key, value] of Object.entries(headers)) {
outgoingHeaders[key] = value;
}
}
if (outgoingHeaders["Content-Type"] == null) {
outgoingHeaders["Content-Type"] = contentType;
}
if (dataStreamVersion !== void 0) {
outgoingHeaders["X-Vercel-AI-Data-Stream"] = dataStreamVersion;
}
return outgoingHeaders;
}
// core/util/write-to-server-response.ts
function writeToServerResponse({
response,
status,
statusText,
headers,
stream
}) {
response.writeHead(status != null ? status : 200, statusText, headers);
const reader = stream.getReader();
const read = async () => {
try {
while (true) {
const { done, value } = await reader.read();
if (done)
break;
response.write(value);
}
} catch (error) {
throw error;
} finally {
response.end();
}
};
read();
}
// core/data-stream/pipe-data-stream-to-response.ts
function pipeDataStreamToResponse(response, {
status,
statusText,
headers,
execute,
onError
}) {
writeToServerResponse({
response,
status,
statusText,
headers: prepareOutgoingHttpHeaders(headers, {
contentType: "text/plain; charset=utf-8",
dataStreamVersion: "v1"
}),
stream: createDataStream({ execute, onError }).pipeThrough(
new TextEncoderStream()
)
});
}
// errors/invalid-argument-error.ts
import { AISDKError } from "@ai-sdk/provider";
var name = "AI_InvalidArgumentError";
var marker = `vercel.ai.error.${name}`;
var symbol = Symbol.for(marker);
var _a;
var InvalidArgumentError = class extends AISDKError {
constructor({
parameter,
value,
message
}) {
super({
name,
message: `Invalid argument for parameter ${parameter}: ${message}`
});
this[_a] = true;
this.parameter = parameter;
this.value = value;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker);
}
};
_a = symbol;
// util/retry-with-exponential-backoff.ts
import { APICallError } from "@ai-sdk/provider";
import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
// util/retry-error.ts
import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
var name2 = "AI_RetryError";
var marker2 = `vercel.ai.error.${name2}`;
var symbol2 = Symbol.for(marker2);
var _a2;
var RetryError = class extends AISDKError2 {
constructor({
message,
reason,
errors
}) {
super({ name: name2, message });
this[_a2] = true;
this.reason = reason;
this.errors = errors;
this.lastError = errors[errors.length - 1];
}
static isInstance(error) {
return AISDKError2.hasMarker(error, marker2);
}
};
_a2 = symbol2;
// util/retry-with-exponential-backoff.ts
var retryWithExponentialBackoff = ({
maxRetries = 2,
initialDelayInMs = 2e3,
backoffFactor = 2
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
maxRetries,
delayInMs: initialDelayInMs,
backoffFactor
});
async function _retryWithExponentialBackoff(f, {
maxRetries,
delayInMs,
backoffFactor
}, errors = []) {
try {
return await f();
} catch (error) {
if (isAbortError(error)) {
throw error;
}
if (maxRetries === 0) {
throw error;
}
const errorMessage = getErrorMessage(error);
const newErrors = [...errors, error];
const tryNumber = newErrors.length;
if (tryNumber > maxRetries) {
throw new RetryError({
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
reason: "maxRetriesExceeded",
errors: newErrors
});
}
if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
await delay(delayInMs);
return _retryWithExponentialBackoff(
f,
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
newErrors
);
}
if (tryNumber === 1) {
throw error;
}
throw new RetryError({
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
reason: "errorNotRetryable",
errors: newErrors
});
}
}
// core/prompt/prepare-retries.ts
function prepareRetries({
maxRetries
}) {
if (maxRetries != null) {
if (!Number.isInteger(maxRetries)) {
throw new InvalidArgumentError({
parameter: "maxRetries",
value: maxRetries,
message: "maxRetries must be an integer"
});
}
if (maxRetries < 0) {
throw new InvalidArgumentError({
parameter: "maxRetries",
value: maxRetries,
message: "maxRetries must be >= 0"
});
}
}
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
return {
maxRetries: maxRetriesResult,
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
};
}
// core/telemetry/assemble-operation-name.ts
function assembleOperationName({
operationId,
telemetry
}) {
return {
// standardized operation and resource name:
"operation.name": `${operationId}${(telemetry == null ? void 0 : telemetry.functionId) != null ? ` ${telemetry.functionId}` : ""}`,
"resource.name": telemetry == null ? void 0 : telemetry.functionId,
// detailed, AI SDK specific data:
"ai.operationId": operationId,
"ai.telemetry.functionId": telemetry == null ? void 0 : telemetry.functionId
};
}
// core/telemetry/get-base-telemetry-attributes.ts
function getBaseTelemetryAttributes({
model,
settings,
telemetry,
headers
}) {
var _a17;
return {
"ai.model.provider": model.provider,
"ai.model.id": model.modelId,
// settings:
...Object.entries(settings).reduce((attributes, [key, value]) => {
attributes[`ai.settings.${key}`] = value;
return attributes;
}, {}),
// add metadata as attributes:
...Object.entries((_a17 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a17 : {}).reduce(
(attributes, [key, value]) => {
attributes[`ai.telemetry.metadata.${key}`] = value;
return attributes;
},
{}
),
// request headers
...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
if (value !== void 0) {
attributes[`ai.request.headers.${key}`] = value;
}
return attributes;
}, {})
};
}
// core/telemetry/get-tracer.ts
import { trace } from "@opentelemetry/api";
// core/telemetry/noop-tracer.ts
var noopTracer = {
startSpan() {
return noopSpan;
},
startActiveSpan(name17, arg1, arg2, arg3) {
if (typeof arg1 === "function") {
return arg1(noopSpan);
}
if (typeof arg2 === "function") {
return arg2(noopSpan);
}
if (typeof arg3 === "function") {
return arg3(noopSpan);
}
}
};
var noopSpan = {
spanContext() {
return noopSpanContext;
},
setAttribute() {
return this;
},
setAttributes() {
return this;
},
addEvent() {
return this;
},
addLink() {
return this;
},
addLinks() {
return this;
},
setStatus() {
return this;
},
updateName() {
return this;
},
end() {
return this;
},
isRecording() {
return false;
},
recordException() {
return this;
}
};
var noopSpanContext = {
traceId: "",
spanId: "",
traceFlags: 0
};
// core/telemetry/get-tracer.ts
function getTracer({
isEnabled = false,
tracer
} = {}) {
if (!isEnabled) {
return noopTracer;
}
if (tracer) {
return tracer;
}
return trace.getTracer("ai");
}
// core/telemetry/record-span.ts
import { SpanStatusCode } from "@opentelemetry/api";
function recordSpan({
name: name17,
tracer,
attributes,
fn,
endWhenDone = true
}) {
return tracer.startActiveSpan(name17, { attributes }, async (span) => {
try {
const result = await fn(span);
if (endWhenDone) {
span.end();
}
return result;
} catch (error) {
try {
if (error instanceof Error) {
span.recordException({
name: error.name,
message: error.message,
stack: error.stack
});
span.setStatus({
code: SpanStatusCode.ERROR,
message: error.message
});
} else {
span.setStatus({ code: SpanStatusCode.ERROR });
}
} finally {
span.end();
}
throw error;
}
});
}
// core/telemetry/select-telemetry-attributes.ts
function selectTelemetryAttributes({
telemetry,
attributes
}) {
if ((telemetry == null ? void 0 : telemetry.isEnabled) !== true) {
return {};
}
return Object.entries(attributes).reduce((attributes2, [key, value]) => {
if (value === void 0) {
return attributes2;
}
if (typeof value === "object" && "input" in value && typeof value.input === "function") {
if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) {
return attributes2;
}
const result = value.input();
return result === void 0 ? attributes2 : { ...attributes2, [key]: result };
}
if (typeof value === "object" && "output" in value && typeof value.output === "function") {
if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) {
return attributes2;
}
const result = value.output();
return result === void 0 ? attributes2 : { ...attributes2, [key]: result };
}
return { ...attributes2, [key]: value };
}, {});
}
// core/embed/embed.ts
async function embed({
model,
value,
maxRetries: maxRetriesArg,
abortSignal,
headers,
experimental_telemetry: telemetry
}) {
const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
const baseTelemetryAttributes = getBaseTelemetryAttributes({
model,
telemetry,
headers,
settings: { maxRetries }
});
const tracer = getTracer(telemetry);
return recordSpan({
name: "ai.embed",
attributes: selectTelemetryAttributes({
telemetry,
attributes: {
...assembleOperationName({ operationId: "ai.embed", telemetry }),
...baseTelemetryAttributes,
"ai.value": { input: () => JSON.stringify(value) }
}
}),
tracer,
fn: async (span) => {
const { embedding, usage, rawResponse } = await retry(
() => (
// nested spans to align with the embedMany telemetry data:
recordSpan({
name: "ai.embed.doEmbed",
attributes: selectTelemetryAttributes({
telemetry,
attributes: {
...assembleOperationName({
operationId: "ai.embed.doEmbed",
telemetry
}),
...baseTelemetryAttributes,
// specific settings that only make sense on the outer level:
"ai.values": { input: () => [JSON.stringify(value)] }
}
}),
tracer,
fn: async (doEmbedSpan) => {
var _a17;
const modelResponse = await model.doEmbed({
values: [value],
abortSignal,
headers
});
const embedding2 = modelResponse.embeddings[0];
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
doEmbedSpan.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embeddings": {
output: () => modelResponse.embeddings.map(
(embedding3) => JSON.stringify(embedding3)
)
},
"ai.usage.tokens": usage2.tokens
}
})
);
return {
embedding: embedding2,
usage: usage2,
rawResponse: modelResponse.rawResponse
};
}
})
)
);
span.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embedding": { output: () => JSON.stringify(embedding) },
"ai.usage.tokens": usage.tokens
}
})
);
return new DefaultEmbedResult({ value, embedding, usage, rawResponse });
}
});
}
var DefaultEmbedResult = class {
constructor(options) {
this.value = options.value;
this.embedding = options.embedding;
this.usage = options.usage;
this.rawResponse = options.rawResponse;
}
};
// core/util/split-array.ts
function splitArray(array, chunkSize) {
if (chunkSize <= 0) {
throw new Error("chunkSize must be greater than 0");
}
const result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}
// core/embed/embed-many.ts
async function embedMany({
model,
values,
maxRetries: maxRetriesArg,
abortSignal,
headers,
experimental_telemetry: telemetry
}) {
const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
const baseTelemetryAttributes = getBaseTelemetryAttributes({
model,
telemetry,
headers,
settings: { maxRetries }
});
const tracer = getTracer(telemetry);
return recordSpan({
name: "ai.embedMany",
attributes: selectTelemetryAttributes({
telemetry,
attributes: {
...assembleOperationName({ operationId: "ai.embedMany", telemetry }),
...baseTelemetryAttributes,
// specific settings that only make sense on the outer level:
"ai.values": {
input: () => values.map((value) => JSON.stringify(value))
}
}
}),
tracer,
fn: async (span) => {
const maxEmbeddingsPerCall = model.maxEmbeddingsPerCall;
if (maxEmbeddingsPerCall == null) {
const { embeddings: embeddings2, usage } = await retry(() => {
return recordSpan({
name: "ai.embedMany.doEmbed",
attributes: selectTelemetryAttributes({
telemetry,
attributes: {
...assembleOperationName({
operationId: "ai.embedMany.doEmbed",
telemetry
}),
...baseTelemetryAttributes,
// specific settings that only make sense on the outer level:
"ai.values": {
input: () => values.map((value) => JSON.stringify(value))
}
}
}),
tracer,
fn: async (doEmbedSpan) => {
var _a17;
const modelResponse = await model.doEmbed({
values,
abortSignal,
headers
});
const embeddings3 = modelResponse.embeddings;
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
doEmbedSpan.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embeddings": {
output: () => embeddings3.map((embedding) => JSON.stringify(embedding))
},
"ai.usage.tokens": usage2.tokens
}
})
);
return { embeddings: embeddings3, usage: usage2 };
}
});
});
span.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embeddings": {
output: () => embeddings2.map((embedding) => JSON.stringify(embedding))
},
"ai.usage.tokens": usage.tokens
}
})
);
return new DefaultEmbedManyResult({ values, embeddings: embeddings2, usage });
}
const valueChunks = splitArray(values, maxEmbeddingsPerCall);
const embeddings = [];
let tokens = 0;
for (const chunk of valueChunks) {
const { embeddings: responseEmbeddings, usage } = await retry(() => {
return recordSpan({
name: "ai.embedMany.doEmbed",
attributes: selectTelemetryAttributes({
telemetry,
attributes: {
...assembleOperationName({
operationId: "ai.embedMany.doEmbed",
telemetry
}),
...baseTelemetryAttributes,
// specific settings that only make sense on the outer level:
"ai.values": {
input: () => chunk.map((value) => JSON.stringify(value))
}
}
}),
tracer,
fn: async (doEmbedSpan) => {
var _a17;
const modelResponse = await model.doEmbed({
values: chunk,
abortSignal,
headers
});
const embeddings2 = modelResponse.embeddings;
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
doEmbedSpan.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embeddings": {
output: () => embeddings2.map((embedding) => JSON.stringify(embedding))
},
"ai.usage.tokens": usage2.tokens
}
})
);
return { embeddings: embeddings2, usage: usage2 };
}
});
});
embeddings.push(...responseEmbeddings);
tokens += usage.tokens;
}
span.setAttributes(
selectTelemetryAttributes({
telemetry,
attributes: {
"ai.embeddings": {
output: () => embeddings.map((embedding) => JSON.stringify(embedding))
},
"ai.usage.tokens": tokens
}
})
);
return new DefaultEmbedManyResult({
values,
embeddings,
usage: { tokens }
});
}
});
}
var DefaultEmbedManyResult = class {
constructor(options) {
this.values = options.values;
this.embeddings = options.embeddings;
this.usage = options.usage;
}
};
// errors/no-image-generated-error.ts
import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
var name3 = "AI_NoImageGeneratedError";
var marker3 = `vercel.ai.error.${name3}`;
var symbol3 = Symbol.for(marker3);
var _a3;
var NoImageGeneratedError = class extends AISDKError3 {
constructor({
message = "No image generated.",
cause,
responses
}) {
super({ name: name3, message, cause });
this[_a3] = true;
this.responses = responses;
}
static isInstance(error) {
return AISDKError3.hasMarker(error, marker3);
}
};
_a3 = symbol3;
// core/generate-text/generated-file.ts
import {
convertBase64ToUint8Array,
convertUint8ArrayToBase64
} from "@ai-sdk/provider-utils";
var DefaultGeneratedFile = class {
constructor({
data,
mimeType
}) {
const isUint8Array = data instanceof Uint8Array;
this.base64Data = isUint8Array ? void 0 : data;
this.uint8ArrayData = isUint8Array ? data : void 0;
this.mimeType = mimeType;
}
// lazy conversion with caching to avoid unnecessary conversion overhead:
get base64() {
if (this.base64Data == null) {
this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
}
return this.base64Data;
}
// lazy conversion with caching to avoid unnecessary conversion overhead:
get uint8Array() {
if (this.uint8ArrayData == null) {
this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
}
return this.uint8ArrayData;
}
};
var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
constructor(options) {
super(options);
this.type = "file";
}
};
// core/util/detect-mimetype.ts
import { convertBase64ToUint8Array as convertBase64ToUint8Array2 } from "@ai-sdk/provider-utils";
var imageMimeTypeSignatures = [
{
mimeType: "image/gif",
bytesPrefix: [71, 73, 70],
base64Prefix: "R0lG"
},
{
mimeType: "image/png",
bytesPrefix: [137, 80, 78, 71],
base64Prefix: "iVBORw"
},
{
mimeType: "image/jpeg",
bytesPrefix: [255, 216],
base64Prefix: "/9j/"
},
{
mimeType: "image/webp",
bytesPrefix: [82, 73, 70, 70],
base64Prefix: "UklGRg"
},
{
mimeType: "image/bmp",
bytesPrefix: [66, 77],
base64Prefix: "Qk"
},
{
mimeType: "image/tiff",
bytesPrefix: [73, 73, 42, 0],
base64Prefix: "SUkqAA"
},
{
mimeType: "image/tiff",
bytesPrefix: [77, 77, 0, 42],
base64Prefix: "TU0AKg"
},
{
mimeType: "image/avif",
bytesPrefix: [
0,
0,
0,
32,
102,
116,
121,
112,
97,
118,
105,
102
],
base64Prefix: "AAAAIGZ0eXBhdmlm"
},
{
mimeType: "image/heic",
bytesPrefix: [
0,
0,
0,
32,
102,
116,
121,
112,
104,
101,
105,
99
],
base64Prefix: "AAAAIGZ0eXBoZWlj"
}
];
var audioMimeTypeSignatures = [
{
mimeType: "audio/mpeg",
bytesPrefix: [255, 251],
base64Prefix: "//s="
},
{
mimeType: "audio/wav",
bytesPrefix: [82, 73, 70, 70],
base64Prefix: "UklGR"
},
{
mimeType: "audio/ogg",
bytesPrefix: [79, 103, 103, 83],
base64Prefix: "T2dnUw"
},
{
mimeType: "audio/flac",
bytesPrefix: [102, 76, 97, 67],
base64Prefix: "ZkxhQw"
},
{
mimeType: "audio/aac",
bytesPrefix: [64, 21, 0, 0],
base64Prefix: "QBUA"
},
{
mimeType: "audio/mp4",
bytesPrefix: [102, 116, 121, 112],
base64Prefix: "ZnR5cA"
}
];
var stripID3 = (data) => {
const bytes = typeof data === "string" ? convertBase64ToUint8Array2(data) : data;
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
return bytes.slice(id3Size + 10);
};
function stripID3TagsIfPresent(data) {
const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
data[1] === 68 && // 'D'
data[2] === 51;
return hasId3 ? stripID3(data) : data;
}
function detectMimeType({
data,
signatures
}) {
const processedData = stripID3TagsIfPresent(data);
for (const signature of signatures) {
if (typeof processedData === "string" ? processedData.startsWith(signature.base64Prefix) : processedData.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
(byte, index) => processedData[index] === byte
)) {
return signature.mimeType;
}
}
return void 0;
}
// core/generate-image/generate-image.ts
async function generateImage({
model,
prompt,
n = 1,
size,
aspectRatio,
seed,
providerOptions,
maxRetries: maxRetriesArg,
abortSignal,
headers
}) {
var _a17;
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
const callCount = Math.ceil(n / maxImagesPerCall);
const callImageCounts = Array.from({ length: callCount }, (_, i) => {
if (i < callCount - 1) {
return maxImagesPerCall;
}
const remainder = n % maxImagesPerCall;
return remainder === 0 ? maxImagesPerCall : remainder;
});
const results = await Promise.all(
callImageCounts.map(
async (callImageCount) => retry(
() => model.doGenerate({
prompt,
n: callImageCount,
abortSignal,
headers,
size,
aspectRatio,
seed,
providerOptions: providerOptions != null ? providerOptions : {}
})
)
)
);
const images = [];
const warnings = [];
const responses = [];
for (const result of results) {
images.push(
...result.images.map(
(image) => {
var _a18;
return new DefaultGeneratedFile({
data: image,
mimeType: (_a18 = detectMimeType({
data: image,
signatures: imageMimeTypeSignatures
})) != null ? _a18 : "image/png"
});
}
)
);
warnings.push(...result.warnings);
responses.push(result.response);
}
if (!images.length) {
throw new NoImageGeneratedError({ responses });
}
return new DefaultGenerateImageResult({ images, warnings, responses });
}
var DefaultGenerateImageResult = class {
constructor(options) {
this.images = options.images;
this.warnings = options.warnings;
this.responses = options.responses;
}
get image() {
return this.images[0];
}
};
// core/generate-object/generate-object.ts
import {
JSONParseError,
TypeValidationError as TypeValidationError2
} from "@ai-sdk/provider";
import { createIdGenerator, safeParseJSON } from "@ai-sdk/provider-utils";
// errors/no-object-generated-error.ts
import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
var name4 = "AI_NoObjectGeneratedError";
var marker4 = `vercel.ai.error.${name4}`;
var symbol4 = Symbol.for(marker4);
var _a4;
var NoObjectGeneratedError = class extends AISDKError4 {
constructor({
message = "No object generated.",
cause,
text: text2,
response,
usage,
finishReason
}) {
super({ name: name4, message, cause });
this[_a4] = true;
this.text = text2;
this.response = response;
this.usage = usage;
this.finishReason = finishReason;
}
static isInstance(error) {
return AISDKError4.hasMarker(error, marker4);
}
};
_a4 = symbol4;
// util/download-error.ts
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
var name5 = "AI_DownloadError";
var marker5 = `vercel.ai.error.${name5}`;
var symbol5 = Symbol.for(marker5);
var _a5;
var DownloadError = class extends AISDKError5 {
constructor({
url,
statusCode,
statusText,
cause,
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
}) {
super({ name: name5, message, cause });
this[_a5] = true;
this.url = url;
this.statusCode = statusCode;
this.statusText = statusText;
}
static isInstance(error) {
return AISDKError5.hasMarker(error, marker5);
}
};
_a5 = symbol5;
// util/download.ts
async function download({ url }) {
var _a17;
const urlText = url.toString();
try {
const response = await fetch(urlText);
if (!response.ok) {
throw new DownloadError({
url: urlText,
statusCode: response.status,
statusText: response.statusText
});
}
return {
data: new Uint8Array(await response.arrayBuffer()),
mimeType: (_a17 = response.headers.get("content-type")) != null ? _a17 : void 0
};
} catch (error) {
if (DownloadError.isInstance(error)) {
throw error;
}
throw new DownloadError({ url: urlText, cause: error });
}
}
// core/prompt/data-content.ts
import {
convertBase64ToUint8Array as convertBase64ToUint8Array3,
convertUint8ArrayToBase64 as convertUint8ArrayToBase642
} from "@ai-sdk/provider-utils";
// core/prompt/invalid-data-content-error.ts
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
var name6 = "AI_InvalidDataContentError";
var marker6 = `vercel.ai.error.${name6}`;
var symbol6 = Symbol.for(marker6);
var _a6;
var InvalidDataContentError = class extends AISDKError6 {
constructor({
content,
cause,
message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
}) {
super({ name: name6, message, cause });
this[_a6] = true;
this.content = content;
}
static isInstance(error) {
return AISDKError6.hasMarker(error, marker6);
}
};
_a6 = symbol6;
// core/prompt/data-content.ts
import { z } from "zod";
var dataContentSchema = z.union([
z.string(),
z.instanceof(Uint8Array),
z.instanceof(ArrayBuffer),
z.custom(
// Buffer might not be available in some environments such as CloudFlare:
(value) => {
var _a17, _b;
return (_b = (_a17 = globalThis.Buffer) == null ? void 0 : _a17.isBuffer(value)) != null ? _b : false;
},
{ message: "Must be a Buffer" }
)
]);
function convertDataContentToBase64String(content) {
if (typeof content === "string") {
return content;
}
if (content instanceof ArrayBuffer) {
return convertUint8ArrayToBase642(new Uint8Array(content));
}
return convertUint8ArrayToBase642(content);
}
function convertDataContentToUint8Array(content) {
if (content instanceof Uint8Array) {
return content;
}
if (typeof content === "string") {
try {
return convertBase64ToUint8Array3(content);
} catch (error) {
throw new InvalidDataContentError({
message: "Invalid data content. Content string is not a base64-encoded media.",
content,
cause: error
});
}
}
if (content instanceof ArrayBuffer) {
return new Uint8Array(content);
}
throw new InvalidDataContentError({ content });
}
function convertUint8ArrayToText(uint8Array) {
try {
return new TextDecoder().decode(uint8Array);
} catch (error) {
throw new Error("Error decoding Uint8Array to text");
}
}
// core/prompt/invalid-message-role-error.ts
import { AISDKError as AISDKError7 } from "@ai-sdk/provider";
var name7 = "AI_InvalidMessageRoleError";
var marker7 = `vercel.ai.error.${name7}`;
var symbol7 = Symbol.for(marker7);
var _a7;
var InvalidMessageRoleError = class extends AISDKError7 {
constructor({
role,
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
}) {
super({ name: name7, message });
this[_a7] = true;
this.role = role;
}
static isInstance(error) {
return AISDKError7.hasMarker(error, marker7);
}
};
_a7 = symbol7;
// core/prompt/split-data-url.ts
function splitDataUrl(dataUrl) {
try {
const [header, base64Content] = dataUrl.split(",");
return {
mimeType: header.split(";")[0].split(":")[1],
base64Content
};
} catch (error) {
return {
mimeType: void 0,
base64Content: void 0
};
}
}
// core/prompt/convert-to-language-model-prompt.ts
async function convertToLanguageModelPrompt({
prompt,
modelSupportsImageUrls = true,
modelSupportsUrl = () => false,
downloadImplementation = download
}) {
const downloadedAssets = await downloadAssets(
prompt.messages,
downloadImplementation,
modelSupportsImageUrls,
modelSupportsUrl
);
return [
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
...prompt.messages.map(
(message) => convertToLanguageModelMessage(message, downloadedAssets)
)
];
}
function convertToLanguageModelMessage(message, downloadedAssets) {
var _a17, _b, _c, _d, _e, _f;
const role = message.role;
switch (role) {
case "system": {
return {
role: "system",
content: message.content,
providerMetadata: (_a17 = message.providerOptions) != null ? _a17 : message.experimental_providerMetadata
};
}
case "user": {
if (typeof message.content === "string") {
return {
role: "user",
content: [{ type: "text", text: message.content }],
providerMetadata: (_b = message.providerOptions) != null ? _b : message.experimental_providerMetadata
};
}
return {
role: "user",
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
providerMetadata: (_c = message.providerOptions) != null ? _c : message.experimental_providerMetadata
};
}
case "assistant": {
if (typeof message.content === "string") {
return {
role: "assistant",
content: [{ type: "text", text: message.content }],
providerMetadata: (_d = message.providerOptions) != null ? _d : message.experimental_providerMetadata
};
}
return {
role: "assistant",
content: message.content.filter(
// remove empty text parts:
(part) => part.type !== "text" || part.text !== ""
).map((part) => {
var _a18;
const providerOptions = (_a18 = part.providerOptions) != null ? _a18 : part.experimental_providerMetadata;
switch (part.type) {
case "file": {
return {
type: "file",
data: part.data instanceof URL ? part.data : convertDataContentToBase64String(part.data),
filename: part.filename,
mimeType: part.mimeType,
providerMetadata: providerOptions
};
}
case "reasoning": {
return {
type: "reasoning",
text: part.text,
signature: part.signature,
providerMetadata: providerOptions
};
}
case "redacted-reasoning": {
return {
type: "redacted-reasoning",
data: part.data,
providerMetadata: providerOptions
};
}
case "text": {
return {
type: "text",
text: part.text,
providerMetadata: providerOptions
};
}
case "tool-call": {
return {
type: "tool-call",
toolCallId: part.toolCallId,
toolName: part.toolName,
args: part.args,
providerMetadata: providerOptions
};
}
}
}),
providerMetadata: (_e = message.providerOptions) != null ? _e : message.experimental_providerMetadata
};
}
case "tool": {
return {
role: "tool",
content: message.content.map((part) => {
var _a18;
return {
type: "tool-result",
toolCallId: part.toolCallId,
toolName: part.toolName,
result: part.result,
content: part.experimental_content,
isError: part.isError,
providerMetadata: (_a18 = part.providerOptions) != null ? _a18 : part.experimental_providerMetadata
};
}),
providerMetadata: (_f = message.providerOptions) != null ? _f : message.experimental_providerMetadata
};
}
default: {
const _exhaustiveCheck = role;
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
}
}
}
async function downloadAssets(messages, downloadImplementation, modelSupportsImageUrls, modelSupportsUrl) {
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
(content) => Array.isArray(content)
).flat().filter(
(part) => part.type === "image" || part.type === "file"
).filter(
(part) => !(part.type === "image" && modelSupportsImageUrls === true)
).map((part) => part.type === "image" ? part.image : part.data).map(
(part) => (
// support string urls:
typeof part === "string" && (part.startsWith("http:") || part.startsWith("https:")) ? new URL(part) : part
)
).filter((image) => image instanceof URL).filter((url) => !modelSupportsUrl(url));
const downloadedImages = await Promise.all(
urls.map(async (url) => ({
url,
data: await downloadImplementation({ url })
}))
);
return Object.fromEntries(
downloadedImages.map(({ url, data }) => [url.toString(), data])
);
}
function convertPartToLanguageModelPart(part, downloadedAssets) {
var _a17, _b, _c, _d;
if (part.type === "text") {
return {
type: "text",
text: part.text,
providerMetadata: (_a17 = part.providerOptions) != null ? _a17 : part.experimental_providerMetadata
};
}
let mimeType = part.mimeType;
let data;
let content;
let normalizedData;
const type = part.type;
switch (type) {
case "image":
data = part.image;
break;
case "file":
data = part.data;
break;
default:
throw new Error(`Unsupported part type: ${type}`);
}
try {
content = typeof data === "string" ? new URL(data) : data;
} catch (error) {
content = data;
}
if (content instanceof URL) {
if (content.protocol === "data:") {
const { mimeType: dataUrlMimeType, base64Content } = splitDataUrl(
content.toString()
);
if (dataUrlMimeType == null || base64Content == null) {
throw new Error(`Invalid data URL format in part ${type}`);
}
mimeType = dataUrlMimeType;
normalizedData = convertDataContentToUint8Array(base64Content);
} else {
const downloadedFile = downloadedAssets[content.toString()];
if (downloadedFile) {
normalizedData = downloadedFile.data;
mimeType != null ? mimeType : mimeType = downloadedFile.mimeType;
} else {
normalizedData = content;
}
}
} else {
normalizedData = convertDataContentToUint8Array(content);
}
switch (type) {
case "image": {
if (normalizedData instanceof Uint8Array) {
mimeType = (_b = detectMimeType({
data: normalizedData,
signatures: imageMimeTypeSignatures
})) != null ? _b : mimeType;
}
return {
type: "image",
image: normalizedData,
mimeType,
providerMetadata: (_c = part.providerOptions) != null ? _c : part.experimental_providerMetadata
};
}
case "file": {
if (mimeType == null) {
throw new Error(`Mime type is missing for file part`);
}
return {
type: "file",
data: normalizedData instanceof Uint8Array ? convertDataContentToBase64String(normalizedData) : normalizedData,
filename: part.filename,
mimeType,
providerMetadata: (_d = part.providerOptions) != null ? _d : part.experimental_providerMetadata
};
}
}
}
// core/prompt/prepare-call-settings.ts
function prepareCallSettings({
maxTokens,
temperature,
topP,
topK,
presencePenalty,
frequencyPenalty,
stopSequences,
seed
}) {
if (maxTokens != null) {
if (!Number.isInteger(maxTokens)) {
throw new InvalidArgumentError({
parameter: "maxTokens",
value: maxTokens,
message: "maxTokens must be an integer"
});
}
if (maxTokens < 1) {
throw new InvalidArgumentError({
parameter: "maxTokens",
value: maxTokens,
message: "maxTokens must be >= 1"
});
}
}
if (temperature != null) {
if (typeof temperature !== "number") {
throw new InvalidArgumentError({
parameter: "temperature",
value: temperature,
message: "temperature must be a number"
});
}
}
if (topP != null) {
if (typeof topP !== "number") {
throw new InvalidArgumentError({
parameter: "topP",
value: topP,
message: "topP must be a number"
});
}
}
if (topK != null) {
if (typeof topK !== "number") {
throw new InvalidArgumentError({
parameter: "topK",
value: topK,
message: "topK must be a number"
});
}
}
if (presencePenalty != null) {
if (typeof presencePenalty !== "number") {
throw new InvalidArgumentError({
parameter: "presencePenalty",
value: presencePenalty,
message: "presencePenalty must be a number"
});
}
}
if (frequencyPenalty != null) {
if (typeof frequencyPenalty !== "number") {
throw new InvalidArgumentError({
parameter: "frequencyPenalty",
value: frequencyPenalty,
message: "frequencyPenalty must be a number"
});
}
}
if (seed != null) {
if (!Number.isInteger(seed)) {
throw new InvalidArgumentError({
parameter: "seed",
value: seed,
message: "seed must be an integer"
});
}
}
return {
maxTokens,
// TODO v5 remove default 0 for temperature
temperature: temperature != null ? temperature : 0,
topP,
topK,
presencePenalty,
frequencyPenalty,
stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
seed
};
}
// core/prompt/standardize-prompt.ts
import { InvalidPromptError } from "@ai-sdk/provider";
import { safeValidateTypes } from "@ai-sdk/provider-utils";
import { z as z7 } from "zod";
// core/prompt/attachments-to-parts.ts
function attachmentsToParts(attachments) {
var _a17, _b, _c;
const parts = [];
for (const attachment of attachments) {
let url;
try {
url = new URL(attachment.url);
} catch (error) {
throw new Error(`Invalid URL: ${attachment.url}`);
}
switch (url.protocol) {
case "http:":
case "https:": {
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
parts.push({ type: "image", image: url });
} else {
if (!attachment.contentType) {
throw new Error(
"If the attachment is not an image, it must specify a content type"
);
}
parts.push({
type: "file",
data: url,
mimeType: attachment.contentType
});
}
break;
}
case "data:": {
let header;
let base64Content;
let mimeType;
try {
[header, base64Content] = attachment.url.split(",");
mimeType = header.split(";")[0].split(":")[1];
} catch (error) {
throw new Error(`Error processing data URL: ${attachment.url}`);
}
if (mimeType == null || base64Content == null) {
throw new Error(`Invalid data URL format: ${attachment.url}`);
}
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
parts.push({
type: "image",
image: convertDataContentToUint8Array(base64Content)
});
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
parts.push({
type: "text",
text: convertUint8ArrayToText(
convertDataContentToUint8Array(base64Content)
)
});
} else {
if (!attachment.contentType) {
throw new Error(
"If the attachment is not an image or text, it must specify a content type"
);
}
parts.push({
type: "file",
data: base64Content,
mimeType: attachment.contentType
});
}
break;
}
default: {
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
}
}
}
return parts;
}
// core/prompt/message-conversion-error.ts
import { AISDKError as AISDKError8 } from "@ai-sdk/provider";
var name8 = "AI_MessageConversionError";
var marker8 = `vercel.ai.error.${name8}`;
var symbol8 = Symbol.for(marker8);
var _a8;
var MessageConversionError = class extends AISDKError8 {
constructor({
originalMessage,
message
}) {
super({ name: name8, message });
this[_a8] = true;
this.originalMessage = originalMessage;
}
static isInstance(error) {
return AISDKError8.hasMarker(error, marker8);
}
};
_a8 = symbol8;
// core/prompt/convert-to-core-messages.ts
function convertToCoreMessages(messages, options) {
var _a17, _b;
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
const coreMessages = [];
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
const isLastMessage = i === messages.length - 1;
const { role, content, experimental_attachments } = message;
switch (role) {
case "system": {
coreMessages.push({
role: "system",
content
});
break;
}
case "user": {
if (message.parts == null) {
coreMessages.push({
role: "user",
content: experimental_attachments ? [
{ type: "text", text: content },
...attachmentsToParts(experimental_attachments)
] : content
});
} else {
const textParts = message.parts.filter((part) => part.type === "text").map((part) => ({
type: "text",
text: part.text
}));
coreMessages.push({
role: "user",
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
});
}
break;
}
case "assistant": {
if (message.parts != null) {
let processBlock2 = function() {
const content2 = [];
for (const part of block) {
switch (part.type) {
case "file":
case "text": {
content2.push(part);
break;
}
case "reasoning": {
for (const detail of part.details) {
switch (detail.type) {
case "text":
content2.push({
type: "reasoning",
text: detail.text,
signature: detail.signature
});
break;
case "redacted":
content2.push({
type: "redacted-reasoning",
data: detail.data
});
break;
}
}
break;
}
case "tool-invocation":
content2.push({
type: "tool-call",
toolCallId: part.toolInvocation.toolCallId,
toolName: part.toolInvocation.toolName,
args: part.toolInvocation.args
});
break;
default: {
const _exhaustiveCheck = part;