UNPKG

ai

Version:

AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript

1,771 lines (1,716 loc) • 178 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name13 in all) __defProp(target, name13, { get: all[name13], enumerable: true }); }; // streams/index.ts import { formatAssistantStreamPart as formatAssistantStreamPart2, formatDataStreamPart as formatDataStreamPart6, parseAssistantStreamPart, parseDataStreamPart, processDataStream, processTextStream } from "@ai-sdk/ui-utils"; import { generateId as generateId2 } from "@ai-sdk/provider-utils"; // core/index.ts import { jsonSchema } 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; } }); try { const result = execute({ writeData(data) { controller.enqueue(formatDataStreamPart("data", [data])); }, writeMessageAnnotation(annotation) { controller.enqueue( formatDataStreamPart("message_annotations", [annotation]) ); }, merge(streamArg) { ongoingStreamPromises.push( (async () => { const reader = streamArg.getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; controller.enqueue(value); } })().catch((error) => { controller.enqueue(formatDataStreamPart("error", onError(error))); }) ); }, onError }); if (result) { ongoingStreamPromises.push( result.catch((error) => { controller.enqueue(formatDataStreamPart("error", onError(error))); }) ); } } catch (error) { controller.enqueue(formatDataStreamPart("error", onError(error))); } const waitForStreams = new Promise(async (resolve) => { while (ongoingStreamPromises.length > 0) { await ongoingStreamPromises.shift(); } resolve(); }); waitForStreams.finally(() => { controller.close(); }); 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 { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils"; // util/delay.ts async function delay(delayInMs) { return delayInMs === void 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs)); } // 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 _a13; 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((_a13 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a13 : {}).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(name13, 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: name13, tracer, attributes, fn, endWhenDone = true }) { return tracer.startActiveSpan(name13, { 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 _a13; const modelResponse = await model.doEmbed({ values: [value], abortSignal, headers }); const embedding2 = modelResponse.embeddings[0]; const usage2 = (_a13 = modelResponse.usage) != null ? _a13 : { 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 _a13; const modelResponse = await model.doEmbed({ values, abortSignal, headers }); const embeddings3 = modelResponse.embeddings; const usage2 = (_a13 = modelResponse.usage) != null ? _a13 : { 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 _a13; const modelResponse = await model.doEmbed({ values: chunk, abortSignal, headers }); const embeddings2 = modelResponse.embeddings; const usage2 = (_a13 = modelResponse.usage) != null ? _a13 : { 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; } }; // core/generate-image/generate-image.ts import { convertBase64ToUint8Array } from "@ai-sdk/provider-utils"; async function generateImage({ model, prompt, n, size, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers }) { const { retry } = prepareRetries({ maxRetries: maxRetriesArg }); const { images } = await retry( () => model.doGenerate({ prompt, n: n != null ? n : 1, abortSignal, headers, size, providerOptions: providerOptions != null ? providerOptions : {} }) ); return new DefaultGenerateImageResult({ base64Images: images }); } var DefaultGenerateImageResult = class { constructor(options) { this.images = options.base64Images.map((base64) => ({ base64, get uint8Array() { return convertBase64ToUint8Array(this.base64); } })); } get image() { return this.images[0]; } }; // core/generate-object/generate-object.ts import { createIdGenerator, safeParseJSON } from "@ai-sdk/provider-utils"; // util/download-error.ts import { AISDKError as AISDKError3 } from "@ai-sdk/provider"; var name3 = "AI_DownloadError"; var marker3 = `vercel.ai.error.${name3}`; var symbol3 = Symbol.for(marker3); var _a3; var DownloadError = class extends AISDKError3 { constructor({ url, statusCode, statusText, cause, message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}` }) { super({ name: name3, message, cause }); this[_a3] = true; this.url = url; this.statusCode = statusCode; this.statusText = statusText; } static isInstance(error) { return AISDKError3.hasMarker(error, marker3); } }; _a3 = symbol3; // util/download.ts async function download({ url, fetchImplementation = fetch }) { var _a13; const urlText = url.toString(); try { const response = await fetchImplementation(urlText); if (!response.ok) { throw new DownloadError({ url: urlText, statusCode: response.status, statusText: response.statusText }); } return { data: new Uint8Array(await response.arrayBuffer()), mimeType: (_a13 = response.headers.get("content-type")) != null ? _a13 : void 0 }; } catch (error) { if (DownloadError.isInstance(error)) { throw error; } throw new DownloadError({ url: urlText, cause: error }); } } // core/util/detect-image-mimetype.ts var mimeTypeSignatures = [ { mimeType: "image/gif", bytes: [71, 73, 70] }, { mimeType: "image/png", bytes: [137, 80, 78, 71] }, { mimeType: "image/jpeg", bytes: [255, 216] }, { mimeType: "image/webp", bytes: [82, 73, 70, 70] } ]; function detectImageMimeType(image) { for (const { bytes, mimeType } of mimeTypeSignatures) { if (image.length >= bytes.length && bytes.every((byte, index) => image[index] === byte)) { return mimeType; } } return void 0; } // core/prompt/data-content.ts import { convertBase64ToUint8Array as convertBase64ToUint8Array2, convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils"; // core/prompt/invalid-data-content-error.ts import { AISDKError as AISDKError4 } from "@ai-sdk/provider"; var name4 = "AI_InvalidDataContentError"; var marker4 = `vercel.ai.error.${name4}`; var symbol4 = Symbol.for(marker4); var _a4; var InvalidDataContentError = class extends AISDKError4 { constructor({ content, cause, message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.` }) { super({ name: name4, message, cause }); this[_a4] = true; this.content = content; } static isInstance(error) { return AISDKError4.hasMarker(error, marker4); } }; _a4 = symbol4; // 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 _a13, _b; return (_b = (_a13 = globalThis.Buffer) == null ? void 0 : _a13.isBuffer(value)) != null ? _b : false; }, { message: "Must be a Buffer" } ) ]); function convertDataContentToBase64String(content) { if (typeof content === "string") { return content; } if (content instanceof ArrayBuffer) { return convertUint8ArrayToBase64(new Uint8Array(content)); } return convertUint8ArrayToBase64(content); } function convertDataContentToUint8Array(content) { if (content instanceof Uint8Array) { return content; } if (typeof content === "string") { try { return convertBase64ToUint8Array2(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 AISDKError5 } from "@ai-sdk/provider"; var name5 = "AI_InvalidMessageRoleError"; var marker5 = `vercel.ai.error.${name5}`; var symbol5 = Symbol.for(marker5); var _a5; var InvalidMessageRoleError = class extends AISDKError5 { constructor({ role, message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".` }) { super({ name: name5, message }); this[_a5] = true; this.role = role; } static isInstance(error) { return AISDKError5.hasMarker(error, marker5); } }; _a5 = symbol5; // 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) { const role = message.role; switch (role) { case "system": { return { role: "system", content: message.content, providerMetadata: message.experimental_providerMetadata }; } case "user": { if (typeof message.content === "string") { return { role: "user", content: [{ type: "text", text: message.content }], providerMetadata: message.experimental_providerMetadata }; } return { role: "user", content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""), providerMetadata: message.experimental_providerMetadata }; } case "assistant": { if (typeof message.content === "string") { return { role: "assistant", content: [{ type: "text", text: message.content }], providerMetadata: message.experimental_providerMetadata }; } return { role: "assistant", content: message.content.filter( // remove empty text parts: (part) => part.type !== "text" || part.text !== "" ).map((part) => { const { experimental_providerMetadata, ...rest } = part; return { ...rest, providerMetadata: experimental_providerMetadata }; }), providerMetadata: message.experimental_providerMetadata }; } case "tool": { return { role: "tool", content: message.content.map((part) => ({ type: "tool-result", toolCallId: part.toolCallId, toolName: part.toolName, result: part.result, content: part.experimental_content, isError: part.isError, providerMetadata: part.experimental_providerMetadata })), providerMetadata: 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) { if (part.type === "text") { return { type: "text", text: part.text, providerMetadata: 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 (mimeType == null && normalizedData instanceof Uint8Array) { mimeType = detectImageMimeType(normalizedData); } return { type: "image", image: normalizedData, mimeType, providerMetadata: 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, mimeType, providerMetadata: 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, 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/message.ts import { z as z6 } from "zod"; // core/types/provider-metadata.ts import { z as z3 } from "zod"; // core/types/json-value.ts import { z as z2 } from "zod"; var jsonValueSchema = z2.lazy( () => z2.union([ z2.null(), z2.string(), z2.number(), z2.boolean(), z2.record(z2.string(), jsonValueSchema), z2.array(jsonValueSchema) ]) ); // core/types/provider-metadata.ts var providerMetadataSchema = z3.record( z3.string(), z3.record(z3.string(), jsonValueSchema) ); // core/prompt/content-part.ts import { z as z5 } from "zod"; // core/prompt/tool-result-content.ts import { z as z4 } from "zod"; var toolResultContentSchema = z4.array( z4.union([ z4.object({ type: z4.literal("text"), text: z4.string() }), z4.object({ type: z4.literal("image"), data: z4.string(), mimeType: z4.string().optional() }) ]) ); // core/prompt/content-part.ts var textPartSchema = z5.object({ type: z5.literal("text"), text: z5.string(), experimental_providerMetadata: providerMetadataSchema.optional() }); var imagePartSchema = z5.object({ type: z5.literal("image"), image: z5.union([dataContentSchema, z5.instanceof(URL)]), mimeType: z5.string().optional(), experimental_providerMetadata: providerMetadataSchema.optional() }); var filePartSchema = z5.object({ type: z5.literal("file"), data: z5.union([dataContentSchema, z5.instanceof(URL)]), mimeType: z5.string(), experimental_providerMetadata: providerMetadataSchema.optional() }); var toolCallPartSchema = z5.object({ type: z5.literal("tool-call"), toolCallId: z5.string(), toolName: z5.string(), args: z5.unknown() }); var toolResultPartSchema = z5.object({ type: z5.literal("tool-result"), toolCallId: z5.string(), toolName: z5.string(), result: z5.unknown(), content: toolResultContentSchema.optional(), isError: z5.boolean().optional(), experimental_providerMetadata: providerMetadataSchema.optional() }); // core/prompt/message.ts var coreSystemMessageSchema = z6.object({ role: z6.literal("system"), content: z6.string(), experimental_providerMetadata: providerMetadataSchema.optional() }); var coreUserMessageSchema = z6.object({ role: z6.literal("user"), content: z6.union([ z6.string(), z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema])) ]), experimental_providerMetadata: providerMetadataSchema.optional() }); var coreAssistantMessageSchema = z6.object({ role: z6.literal("assistant"), content: z6.union([ z6.string(), z6.array(z6.union([textPartSchema, toolCallPartSchema])) ]), experimental_providerMetadata: providerMetadataSchema.optional() }); var coreToolMessageSchema = z6.object({ role: z6.literal("tool"), content: z6.array(toolResultPartSchema), experimental_providerMetadata: providerMetadataSchema.optional() }); var coreMessageSchema = z6.union([ coreSystemMessageSchema, coreUserMessageSchema, coreAssistantMessageSchema, coreToolMessageSchema ]); // core/prompt/detect-prompt-type.ts function detectPromptType(prompt) { if (!Array.isArray(prompt)) { return "other"; } if (prompt.length === 0) { return "messages"; } const characteristics = prompt.map(detectSingleMessageCharacteristics); if (characteristics.some((c) => c === "has-ui-specific-parts")) { return "ui-messages"; } else if (characteristics.every( (c) => c === "has-core-specific-parts" || c === "message" )) { return "messages"; } else { return "other"; } } function detectSingleMessageCharacteristics(message) { if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role message.role === "data" || // UI-only role "toolInvocations" in message || // UI-specific field "experimental_attachments" in message)) { return "has-ui-specific-parts"; } else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content "experimental_providerMetadata" in message)) { return "has-core-specific-parts"; } else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) { return "message"; } else { return "other"; } } // core/prompt/attachments-to-parts.ts function attachmentsToParts(attachments) { var _a13, _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 ((_a13 = attachment.contentType) == null ? void 0 : _a13.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 AISDKError6 } from "@ai-sdk/provider"; var name6 = "AI_MessageConversionError"; var marker6 = `vercel.ai.error.${name6}`; var symbol6 = Symbol.for(marker6); var _a6; var MessageConversionError = class extends AISDKError6 { constructor({ originalMessage, message }) { super({ name: name6, message }); this[_a6] = true; this.originalMessage = originalMessage; } static isInstance(error) { return AISDKError6.hasMarker(error, marker6); } }; _a6 = symbol6; // core/prompt/convert-to-core-messages.ts function convertToCoreMessages(messages, options) { var _a13; const tools = (_a13 = options == null ? void 0 : options.tools) != null ? _a13 : {}; const coreMessages = []; for (const message of messages) { const { role, content, toolInvocations, experimental_attachments } = message; switch (role) { case "system": { coreMessages.push({ role: "system", content }); break; } case "user": { coreMessages.push({ role: "user", content: experimental_attachments ? [ { type: "text", text: content }, ...attachmentsToParts(experimental_attachments) ] : content }); break; } case "assistant": { if (toolInvocations == null) { coreMessages.push({ role: "assistant", content }); break; } coreMessages.push({ role: "assistant", content: [ { type: "text", text: content }, ...toolInvocations.map( ({ toolCallId, toolName, args }) => ({ type: "tool-call", toolCallId, toolName, args }) ) ] }); coreMessages.push({ role: "tool", content: toolInvocations.map((toolInvocation) => { if (!("result" in toolInvocation)) { throw new MessageConversionError({ originalMessage: message, message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation) }); } const { toolCallId, toolName, result } = toolInvocation; const tool2 = tools[toolName]; return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? { type: "tool-result", toolCallId, toolName, result: tool2.experimental_toToolResultContent(result), experimental_content: tool2.experimental_toToolResultContent(result) } : { type: "tool-result", toolCallId, toolName, result }; }) }); break; } case "data": { break; } default: { const _exhaustiveCheck = role; throw new MessageConversionError({ originalMessage: message, message: `Unsupported role: ${_exhaustiveCheck}` }); } } } return coreMessages; } // core/prompt/standardize-prompt.ts function standardizePrompt({ prompt, tools }) { if (prompt.prompt == null && prompt.messages == null) { throw new InvalidPromptError({ prompt, message: "prompt or messages must be defined" }); } if (prompt.prompt != null && prompt.messages != null) { throw new InvalidPromptError({ prompt, message: "prompt and messages cannot be defined at the same time" }); } if (prompt.system != null && typeof prompt.system !== "string") { throw new InvalidPromptError({ prompt, message: "system must be a string" }); } if (prompt.prompt != null) { if (typeof prompt.prompt !== "string") { throw new InvalidPromptError({ prompt, message: "prompt must be a string" }); } return { type: "prompt", system: prompt.system, messages: [ { role: "user", content: prompt.prompt } ] }; } if (prompt.messages != null) { const promptType = detectPromptType(prompt.messages); if (promptType === "other") { throw new InvalidPromptError({ prompt, message: "messages must be an array of CoreMessage or UIMessage" }); } const messages = promptType === "ui-messages" ? convertToCoreMessages(prompt.messages, { tools }) : prompt.messages; const validationResult = safeValidateTypes({ value: messages, schema: z7.array(coreMessageSchema) }); if (!validationResult.success) { throw new InvalidPromptError({ prompt, message: "messages must be an array of CoreMessage or UIMessage", cause: validationResult.error }); } return { type: "messages", messages, system: prompt.system }; } throw new Error("unreachable"); } // core/types/usage.ts function calculateLanguageModelUsage({ promptTokens, completionTokens }) { return { promptTokens, completionTokens, totalTokens: promptTokens + completionTokens }; } // core/generate-object/inject-json-instruction.ts var DEFAULT_SCHEMA_PREFIX = "JSON schema:"; var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above."; var DEFAULT_GENERIC_SUFFIX = "You MUST answer with JSON."; function injectJsonInstruction({ prompt, schema, schemaPrefix = schema != null ? DEFAULT_SCHEMA_PREFIX : void 0, schemaSuffix = schema != null ? DEFAULT_SCHEMA_SUFFIX : DEFAULT_GENERIC_SUFFIX }) { return [ prompt != null && prompt.length > 0 ? prompt : void 0, prompt != null && prompt.length > 0 ? "" : void 0, // add a newline if prompt is not null schemaPrefix, schema != null ? JSON.stringify(schema) : void 0, schemaSuffix ].filter((line) => line != null).join("\n"); } // core/generate-object/no-object-generated-error.ts import { AISDKError as AISDKError7 } from "@ai-sdk/provider"; var name7 = "AI_NoObjectGeneratedError"; var marker7 = `vercel.ai.error.${name7}`; var symbol7 = Symbol.for(marker7); var _a7; var NoObjectGeneratedError = class extends AISDKError7 { // used in isInstance constructor({ message = "No object generated." } = {}) { super({ name: name7, message }); this[_a7] = true; } static isInstance(error) { return AISDKError7.hasMarker(error, marker7); } }; _a7 = symbol7; // core/generate-object/output-strategy.ts import { isJSONArray, isJSONObject, TypeValidationError, UnsupportedFunctionalityError } from "@ai-sdk/provider"; import { safeValidateTypes as safeValidateTypes2 } from "@ai-sdk/provider-utils"; import { asSchema } from "@ai-sdk/ui-utils"; // core/util/async-iterable-stream.ts function createAsyncIterableStream(source, t