UNPKG

@mastra/core

Version:
77 lines (76 loc) 2.11 kB
const require_error = require("./error-B-e62x-A.cjs"); //#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 require_error.MastraError({ id: "INVALID_DATA_URL_FORMAT", text: `Invalid data URL format in content ${content.toString()}`, domain: require_error.ErrorDomain.LLM, category: require_error.ErrorCategory.USER }); return { data: base64Content, mediaType: dataUrlMediaType }; } return { data: content, mediaType: void 0 }; } //#endregion Object.defineProperty(exports, "convertToDataContent", { enumerable: true, get: function() { return convertToDataContent; } }); Object.defineProperty(exports, "isUrlString", { enumerable: true, get: function() { return isUrlString; } }); //# sourceMappingURL=content-fINNgB3A.cjs.map