UNPKG

@mastra/core

Version:
66 lines (65 loc) 1.91 kB
import { i as MastraError, n as ErrorDomain, t as ErrorCategory } from "./error-MjDSls8S.js"; //#region src/stream/aisdk/v5/compat/content.ts function splitDataUrl(dataUrl) { try { const [header, base64Content] = dataUrl.split(","); return { mediaType: header?.split(";")[0]?.split(":")[1], base64Content }; } catch { return { mediaType: void 0, base64Content: void 0 }; } } /** * Flat generated-file data is `string | Uint8Array`, where strings normally * hold base64. URL-backed V4 generated files flatten to http(s) URL strings * (matching AI SDK v7's own conversion), so consumers that interpret string * data as base64 must use this check to tell the two apart. Base64 content * never parses as an http(s) URL, making the check unambiguous. */ function isUrlString(data) { if (!data.startsWith("http://") && !data.startsWith("https://")) return false; try { new URL(data); return true; } catch { return false; } } function convertToDataContent(content) { if (content instanceof Uint8Array) return { data: content, mediaType: void 0 }; if (content instanceof ArrayBuffer) return { data: new Uint8Array(content), mediaType: void 0 }; if (typeof content === "string") try { content = new URL(content); } catch {} if (content instanceof URL && content.protocol === "data:") { const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(content.toString()); if (dataUrlMediaType == null || base64Content == null) throw new MastraError({ id: "INVALID_DATA_URL_FORMAT", text: `Invalid data URL format in content ${content.toString()}`, domain: ErrorDomain.LLM, category: ErrorCategory.USER }); return { data: base64Content, mediaType: dataUrlMediaType }; } return { data: content, mediaType: void 0 }; } //#endregion export { isUrlString as n, convertToDataContent as t }; //# sourceMappingURL=content-DX_6irdy.js.map