@eko-ai/eko
Version:
Empowering language to transform human words into action.
1,769 lines (1,708 loc) • 750 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var buffer = require('buffer');
const config = {
name: "Eko",
platform: "mac",
maxReactNum: 500,
maxTokens: 16000,
compressThreshold: 80,
largeTextLength: 5000,
fileTextMaxLength: 20000,
maxDialogueImgFileNum: 1,
};
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
LogLevel[LogLevel["INFO"] = 1] = "INFO";
LogLevel[LogLevel["WARN"] = 2] = "WARN";
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
LogLevel[LogLevel["FATAL"] = 4] = "FATAL";
LogLevel[LogLevel["OFF"] = 5] = "OFF";
})(LogLevel || (LogLevel = {}));
class ConsoleTransport {
log(level, message) {
const methods = {
[LogLevel.DEBUG]: console.debug,
[LogLevel.INFO]: console.info,
[LogLevel.WARN]: console.warn,
[LogLevel.ERROR]: console.error,
[LogLevel.FATAL]: console.error,
[LogLevel.OFF]: () => { }
};
const method = methods[level] || console.log;
method(message);
}
}
class Logger {
constructor(options = {}) {
this.level = options.level ?? LogLevel.INFO;
this.prefix = options.prefix ?? '';
this.dateFormat = options.dateFormat ?? true;
this.transports = options.transport ?? [new ConsoleTransport()];
}
setLevel(level) {
this.level = level;
return this;
}
setPrefix(prefix) {
this.prefix = prefix;
return this;
}
addTransport(transport) {
this.transports.push(transport);
return this;
}
formatMessage(level, message) {
const levelNames = {
[LogLevel.DEBUG]: 'DEBUG',
[LogLevel.INFO]: 'INFO',
[LogLevel.WARN]: 'WARN',
[LogLevel.ERROR]: 'ERROR',
[LogLevel.FATAL]: 'FATAL',
[LogLevel.OFF]: 'OFF'
};
let formattedMessage = '';
if (this.dateFormat) {
formattedMessage += `[${new Date().toLocaleString()}] `;
}
formattedMessage += `[${levelNames[level] || 'UNKNOWN'}] `;
if (this.prefix) {
formattedMessage += `[${this.prefix}] `;
}
formattedMessage += message;
return formattedMessage;
}
log(level, message, ...args) {
if (level < this.level) {
return;
}
let finalMessage;
if (message instanceof Error) {
finalMessage = `${message.message}\n${message.stack}`;
}
else {
finalMessage = message;
}
if (args.length > 0) {
finalMessage += ' ' + args.map(arg => {
if (arg == null || arg == undefined) {
return arg + '';
}
else if (arg instanceof Error || (arg.stack && arg.message)) {
return `${arg.message}\n${arg.stack}`;
}
else if (typeof arg === 'object') {
return JSON.stringify(arg);
}
return String(arg);
}).join(' ');
}
const formattedMessage = this.formatMessage(level, finalMessage);
this.transports.forEach(transport => {
transport.log(level, formattedMessage);
});
}
isEnableDebug() {
return this.level <= LogLevel.DEBUG;
}
debug(message, ...args) {
this.log(LogLevel.DEBUG, message, ...args);
}
isEnableInfo() {
return this.level <= LogLevel.INFO;
}
info(message, ...args) {
this.log(LogLevel.INFO, message, ...args);
}
warn(message, ...args) {
this.log(LogLevel.WARN, message, ...args);
}
error(message, ...args) {
this.log(LogLevel.ERROR, message, ...args);
}
fatal(message, ...args) {
this.log(LogLevel.FATAL, message, ...args);
}
createChild(name, options = {}) {
const childPrefix = this.prefix ? `${this.prefix}.${name}` : name;
return new Logger({
level: options.level || this.level,
prefix: childPrefix,
dateFormat: options.dateFormat !== undefined ? options.dateFormat : this.dateFormat,
transport: options.transport || this.transports
});
}
}
const Log = new Logger();
function sleep(time) {
return new Promise((resolve) => setTimeout(() => resolve(), time));
}
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
function call_timeout(fun, timeout, error_callback) {
return new Promise(async (resolve, reject) => {
let timer = setTimeout(() => {
reject(new Error("Timeout"));
error_callback && error_callback("Timeout");
}, timeout);
try {
const result = await fun();
clearTimeout(timer);
resolve(result);
}
catch (e) {
clearTimeout(timer);
reject(e);
error_callback && error_callback(e + "");
}
});
}
function convertToolSchema(tool) {
if ("function" in tool) {
return {
type: "function",
name: tool.function.name,
description: tool.function.description,
parameters: tool.function.parameters,
};
}
else if ("input_schema" in tool) {
return {
type: "function",
name: tool.name,
description: tool.description,
parameters: tool.input_schema,
};
}
else if ("inputSchema" in tool) {
return {
type: "function",
name: tool.name,
description: tool.description,
parameters: tool.inputSchema,
};
}
else {
return {
type: "function",
name: tool.name,
description: tool.description,
parameters: tool.parameters,
};
}
}
function toImage(imageData) {
let image = null;
if (imageData.startsWith("http://") || imageData.startsWith("https://")) {
image = new URL(imageData);
}
else {
if (imageData.startsWith("data:image/")) {
imageData = imageData.substring(imageData.indexOf(",") + 1);
}
// @ts-ignore
if (typeof Buffer != "undefined") {
// @ts-ignore
const buffer = Buffer.from(imageData, "base64");
image = new Uint8Array(buffer);
}
else {
const binaryString = atob(imageData);
image = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
image[i] = binaryString.charCodeAt(i);
}
}
}
return image;
}
function mergeTools(tools1, tools2) {
let tools = [];
let toolMap2 = tools2.reduce((map, tool) => {
map[tool.name] = tool;
return map;
}, {});
let names = [];
for (let i = 0; i < tools1.length; i++) {
let tool1 = tools1[i];
let tool2 = toolMap2[tool1.name];
if (tool2) {
tools.push(tool2);
delete toolMap2[tool1.name];
}
else {
tools.push(tool1);
}
}
for (let i = 0; i < tools2.length; i++) {
let tool2 = tools2[i];
if (toolMap2[tool2.name] && names.indexOf(tool2.name) === -1) {
tools.push(tool2);
names.push(tool2.name);
}
}
return tools;
}
function mergeAgents(agents1, agents2) {
let tools = [];
let toolMap2 = agents2.reduce((map, tool) => {
map[tool.Name] = tool;
return map;
}, {});
for (let i = 0; i < agents1.length; i++) {
let tool1 = agents1[i];
let tool2 = toolMap2[tool1.Name];
if (tool2) {
tools.push(tool2);
delete toolMap2[tool1.Name];
}
else {
tools.push(tool1);
}
}
for (let i = 0; i < agents2.length; i++) {
let tool2 = agents2[i];
if (toolMap2[tool2.Name]) {
tools.push(tool2);
}
}
return tools;
}
function sub(str, maxLength, appendPoint = true) {
if (!str) {
return "";
}
if (str.length > maxLength) {
return str.substring(0, maxLength) + (appendPoint ? "..." : "");
}
return str;
}
function fixXmlTag(code) {
if (code.indexOf('&') > -1) {
code = code.replace(/&(?![a-zA-Z0-9#]+;)/g, '&');
}
function fixDoubleChar(code) {
const stack = [];
for (let i = 0; i < code.length; i++) {
let s = code[i];
if (s === "<") {
stack.push(">");
}
else if (s === ">") {
stack.pop();
}
else if (s === '"') {
if (stack[stack.length - 1] === '"') {
stack.pop();
}
else {
stack.push('"');
}
}
}
const missingParts = [];
while (stack.length > 0) {
missingParts.push(stack.pop());
}
return code + missingParts.join("");
}
let eIdx = code.lastIndexOf(" ");
let endStr = eIdx > -1 ? code.substring(eIdx + 1) : "";
if (code.endsWith("=")) {
code += '""';
}
else if (endStr == "name" ||
endStr == "input" ||
endStr == "output" ||
endStr == "items" ||
endStr == "event" ||
endStr == "loop") {
let idx1 = code.lastIndexOf(">");
let idx2 = code.lastIndexOf("<");
if (idx1 < idx2 && code.lastIndexOf(" ") > idx2) {
code += '=""';
}
}
code = fixDoubleChar(code);
const stack = [];
function isSelfClosing(tag) {
return tag.endsWith("/>");
}
for (let i = 0; i < code.length; i++) {
let s = code[i];
if (s === "<") {
const isEndTag = code[i + 1] === "/";
let endIndex = code.indexOf(">", i);
let tagContent = code.slice(i, endIndex + 1);
if (isSelfClosing(tagContent)) ;
else if (isEndTag) {
stack.pop();
}
else {
stack.push(tagContent);
}
if (endIndex == -1) {
break;
}
i = endIndex;
}
}
const missingParts = [];
while (stack.length > 0) {
const top = stack.pop();
if (top.startsWith("<")) {
let arr = top.match(/<(\w+)/);
const tagName = arr[1];
missingParts.push(`</${tagName}>`);
}
else {
missingParts.push(top);
}
}
let completedCode = code + missingParts.join("");
return completedCode;
}
class Context {
constructor(taskId, config, agents, chain) {
this.paused = false;
this.conversation = [];
this.taskId = taskId;
this.config = config;
this.agents = agents;
this.chain = chain;
this.variables = new Map();
this.controller = new AbortController();
}
async checkAborted() {
// this.controller.signal.throwIfAborted();
if (this.controller.signal.aborted) {
const error = new Error("Operation was interrupted");
error.name = "AbortError";
throw error;
}
while (this.paused) {
await sleep(500);
if (this.controller.signal.aborted) {
const error = new Error("Operation was interrupted");
error.name = "AbortError";
throw error;
}
}
}
currentAgent() {
const agentNode = this.chain.agents[this.chain.agents.length - 1];
if (!agentNode) {
return null;
}
const agent = this.agents.filter((agent) => agent.Name == agentNode.agent.name)[0];
if (!agent) {
return null;
}
const agentContext = agent["agentContext"];
return [agent, agentNode.agent, agentContext];
}
}
class AgentContext {
constructor(context, agent, agentChain) {
this.context = context;
this.agent = agent;
this.agentChain = agentChain;
this.variables = new Map();
this.consecutiveErrorNum = 0;
}
}
// src/errors/ai-sdk-error.ts
var marker = "vercel.ai.error";
var symbol = Symbol.for(marker);
var _a;
var _AISDKError = class _AISDKError extends Error {
/**
* Creates an AI SDK Error.
*
* @param {Object} params - The parameters for creating the error.
* @param {string} params.name - The name of the error.
* @param {string} params.message - The error message.
* @param {unknown} [params.cause] - The underlying cause of the error.
*/
constructor({
name: name14,
message,
cause
}) {
super(message);
this[_a] = true;
this.name = name14;
this.cause = cause;
}
/**
* Checks if the given error is an AI SDK Error.
* @param {unknown} error - The error to check.
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
*/
static isInstance(error) {
return _AISDKError.hasMarker(error, marker);
}
static hasMarker(error, marker15) {
const markerSymbol = Symbol.for(marker15);
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
}
};
_a = symbol;
var AISDKError = _AISDKError;
// src/errors/api-call-error.ts
var name = "AI_APICallError";
var marker2 = `vercel.ai.error.${name}`;
var symbol2 = Symbol.for(marker2);
var _a2;
var APICallError = class extends AISDKError {
constructor({
message,
url,
requestBodyValues,
statusCode,
responseHeaders,
responseBody,
cause,
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
statusCode === 409 || // conflict
statusCode === 429 || // too many requests
statusCode >= 500),
// server error
data
}) {
super({ name, message, cause });
this[_a2] = true;
this.url = url;
this.requestBodyValues = requestBodyValues;
this.statusCode = statusCode;
this.responseHeaders = responseHeaders;
this.responseBody = responseBody;
this.isRetryable = isRetryable;
this.data = data;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker2);
}
};
_a2 = symbol2;
// src/errors/empty-response-body-error.ts
var name2 = "AI_EmptyResponseBodyError";
var marker3 = `vercel.ai.error.${name2}`;
var symbol3 = Symbol.for(marker3);
var _a3;
var EmptyResponseBodyError = class extends AISDKError {
// used in isInstance
constructor({ message = "Empty response body" } = {}) {
super({ name: name2, message });
this[_a3] = true;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker3);
}
};
_a3 = symbol3;
// src/errors/get-error-message.ts
function getErrorMessage(error) {
if (error == null) {
return "unknown error";
}
if (typeof error === "string") {
return error;
}
if (error instanceof Error) {
return error.message;
}
return JSON.stringify(error);
}
// src/errors/invalid-argument-error.ts
var name3 = "AI_InvalidArgumentError";
var marker4 = `vercel.ai.error.${name3}`;
var symbol4 = Symbol.for(marker4);
var _a4;
var InvalidArgumentError = class extends AISDKError {
constructor({
message,
cause,
argument
}) {
super({ name: name3, message, cause });
this[_a4] = true;
this.argument = argument;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker4);
}
};
_a4 = symbol4;
// src/errors/invalid-prompt-error.ts
var name4 = "AI_InvalidPromptError";
var marker5 = `vercel.ai.error.${name4}`;
var symbol5 = Symbol.for(marker5);
var _a5;
var InvalidPromptError = class extends AISDKError {
constructor({
prompt,
message,
cause
}) {
super({ name: name4, message: `Invalid prompt: ${message}`, cause });
this[_a5] = true;
this.prompt = prompt;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker5);
}
};
_a5 = symbol5;
// src/errors/invalid-response-data-error.ts
var name5 = "AI_InvalidResponseDataError";
var marker6 = `vercel.ai.error.${name5}`;
var symbol6 = Symbol.for(marker6);
var _a6;
var InvalidResponseDataError = class extends AISDKError {
constructor({
data,
message = `Invalid response data: ${JSON.stringify(data)}.`
}) {
super({ name: name5, message });
this[_a6] = true;
this.data = data;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker6);
}
};
_a6 = symbol6;
// src/errors/json-parse-error.ts
var name6 = "AI_JSONParseError";
var marker7 = `vercel.ai.error.${name6}`;
var symbol7 = Symbol.for(marker7);
var _a7;
var JSONParseError = class extends AISDKError {
constructor({ text, cause }) {
super({
name: name6,
message: `JSON parsing failed: Text: ${text}.
Error message: ${getErrorMessage(cause)}`,
cause
});
this[_a7] = true;
this.text = text;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker7);
}
};
_a7 = symbol7;
// src/errors/load-api-key-error.ts
var name7 = "AI_LoadAPIKeyError";
var marker8 = `vercel.ai.error.${name7}`;
var symbol8 = Symbol.for(marker8);
var _a8;
var LoadAPIKeyError = class extends AISDKError {
// used in isInstance
constructor({ message }) {
super({ name: name7, message });
this[_a8] = true;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker8);
}
};
_a8 = symbol8;
// src/errors/load-setting-error.ts
var name8 = "AI_LoadSettingError";
var marker9 = `vercel.ai.error.${name8}`;
var symbol9 = Symbol.for(marker9);
var _a9;
var LoadSettingError = class extends AISDKError {
// used in isInstance
constructor({ message }) {
super({ name: name8, message });
this[_a9] = true;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker9);
}
};
_a9 = symbol9;
// src/errors/no-such-model-error.ts
var name10 = "AI_NoSuchModelError";
var marker11 = `vercel.ai.error.${name10}`;
var symbol11 = Symbol.for(marker11);
var _a11;
var NoSuchModelError = class extends AISDKError {
constructor({
errorName = name10,
modelId,
modelType,
message = `No such ${modelType}: ${modelId}`
}) {
super({ name: errorName, message });
this[_a11] = true;
this.modelId = modelId;
this.modelType = modelType;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker11);
}
};
_a11 = symbol11;
// src/errors/too-many-embedding-values-for-call-error.ts
var name11 = "AI_TooManyEmbeddingValuesForCallError";
var marker12 = `vercel.ai.error.${name11}`;
var symbol12 = Symbol.for(marker12);
var _a12;
var TooManyEmbeddingValuesForCallError = class extends AISDKError {
constructor(options) {
super({
name: name11,
message: `Too many values for a single embedding call. The ${options.provider} model "${options.modelId}" can only embed up to ${options.maxEmbeddingsPerCall} values per call, but ${options.values.length} values were provided.`
});
this[_a12] = true;
this.provider = options.provider;
this.modelId = options.modelId;
this.maxEmbeddingsPerCall = options.maxEmbeddingsPerCall;
this.values = options.values;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker12);
}
};
_a12 = symbol12;
// src/errors/type-validation-error.ts
var name12 = "AI_TypeValidationError";
var marker13 = `vercel.ai.error.${name12}`;
var symbol13 = Symbol.for(marker13);
var _a13;
var _TypeValidationError = class _TypeValidationError extends AISDKError {
constructor({ value, cause }) {
super({
name: name12,
message: `Type validation failed: Value: ${JSON.stringify(value)}.
Error message: ${getErrorMessage(cause)}`,
cause
});
this[_a13] = true;
this.value = value;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker13);
}
/**
* Wraps an error into a TypeValidationError.
* If the cause is already a TypeValidationError with the same value, it returns the cause.
* Otherwise, it creates a new TypeValidationError.
*
* @param {Object} params - The parameters for wrapping the error.
* @param {unknown} params.value - The value that failed validation.
* @param {unknown} params.cause - The original error or cause of the validation failure.
* @returns {TypeValidationError} A TypeValidationError instance.
*/
static wrap({
value,
cause
}) {
return _TypeValidationError.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError({ value, cause });
}
};
_a13 = symbol13;
var TypeValidationError = _TypeValidationError;
// src/errors/unsupported-functionality-error.ts
var name13 = "AI_UnsupportedFunctionalityError";
var marker14 = `vercel.ai.error.${name13}`;
var symbol14 = Symbol.for(marker14);
var _a14;
var UnsupportedFunctionalityError = class extends AISDKError {
constructor({
functionality,
message = `'${functionality}' functionality not supported.`
}) {
super({ name: name13, message });
this[_a14] = true;
this.functionality = functionality;
}
static isInstance(error) {
return AISDKError.hasMarker(error, marker14);
}
};
_a14 = symbol14;
let customAlphabet = (alphabet, defaultSize = 21) => {
return (size = defaultSize) => {
let id = '';
let i = size | 0;
while (i--) {
id += alphabet[(Math.random() * alphabet.length) | 0];
}
return id
}
};
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var secureJsonParse = {exports: {}};
var hasRequiredSecureJsonParse;
function requireSecureJsonParse () {
if (hasRequiredSecureJsonParse) return secureJsonParse.exports;
hasRequiredSecureJsonParse = 1;
const hasBuffer = typeof Buffer !== 'undefined';
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
function _parse (text, reviver, options) {
// Normalize arguments
if (options == null) {
if (reviver !== null && typeof reviver === 'object') {
options = reviver;
reviver = undefined;
}
}
if (hasBuffer && Buffer.isBuffer(text)) {
text = text.toString();
}
// BOM checker
if (text && text.charCodeAt(0) === 0xFEFF) {
text = text.slice(1);
}
// Parse normally, allowing exceptions
const obj = JSON.parse(text, reviver);
// Ignore null and non-objects
if (obj === null || typeof obj !== 'object') {
return obj
}
const protoAction = (options && options.protoAction) || 'error';
const constructorAction = (options && options.constructorAction) || 'error';
// options: 'error' (default) / 'remove' / 'ignore'
if (protoAction === 'ignore' && constructorAction === 'ignore') {
return obj
}
if (protoAction !== 'ignore' && constructorAction !== 'ignore') {
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
return obj
}
} else if (protoAction !== 'ignore' && constructorAction === 'ignore') {
if (suspectProtoRx.test(text) === false) {
return obj
}
} else {
if (suspectConstructorRx.test(text) === false) {
return obj
}
}
// Scan result for proto keys
return filter(obj, { protoAction, constructorAction, safe: options && options.safe })
}
function filter (obj, { protoAction = 'error', constructorAction = 'error', safe } = {}) {
let next = [obj];
while (next.length) {
const nodes = next;
next = [];
for (const node of nodes) {
if (protoAction !== 'ignore' && Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly
if (safe === true) {
return null
} else if (protoAction === 'error') {
throw new SyntaxError('Object contains forbidden prototype property')
}
delete node.__proto__; // eslint-disable-line no-proto
}
if (constructorAction !== 'ignore' &&
Object.prototype.hasOwnProperty.call(node, 'constructor') &&
Object.prototype.hasOwnProperty.call(node.constructor, 'prototype')) { // Avoid calling node.hasOwnProperty directly
if (safe === true) {
return null
} else if (constructorAction === 'error') {
throw new SyntaxError('Object contains forbidden prototype property')
}
delete node.constructor;
}
for (const key in node) {
const value = node[key];
if (value && typeof value === 'object') {
next.push(value);
}
}
}
}
return obj
}
function parse (text, reviver, options) {
const stackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 0;
try {
return _parse(text, reviver, options)
} finally {
Error.stackTraceLimit = stackTraceLimit;
}
}
function safeParse (text, reviver) {
const stackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 0;
try {
return _parse(text, reviver, { safe: true })
} catch (_e) {
return null
} finally {
Error.stackTraceLimit = stackTraceLimit;
}
}
secureJsonParse.exports = parse;
secureJsonParse.exports.default = parse;
secureJsonParse.exports.parse = parse;
secureJsonParse.exports.safeParse = safeParse;
secureJsonParse.exports.scan = filter;
return secureJsonParse.exports;
}
var secureJsonParseExports = requireSecureJsonParse();
var SecureJSON = /*@__PURE__*/getDefaultExportFromCjs(secureJsonParseExports);
// src/combine-headers.ts
function combineHeaders(...headers) {
return headers.reduce(
(combinedHeaders, currentHeaders) => ({
...combinedHeaders,
...currentHeaders != null ? currentHeaders : {}
}),
{}
);
}
// src/event-source-parser-stream.ts
function createEventSourceParserStream() {
let buffer = "";
let event = void 0;
let data = [];
let lastEventId = void 0;
let retry = void 0;
function parseLine(line, controller) {
if (line === "") {
dispatchEvent(controller);
return;
}
if (line.startsWith(":")) {
return;
}
const colonIndex = line.indexOf(":");
if (colonIndex === -1) {
handleField(line, "");
return;
}
const field = line.slice(0, colonIndex);
const valueStart = colonIndex + 1;
const value = valueStart < line.length && line[valueStart] === " " ? line.slice(valueStart + 1) : line.slice(valueStart);
handleField(field, value);
}
function dispatchEvent(controller) {
if (data.length > 0) {
controller.enqueue({
event,
data: data.join("\n"),
id: lastEventId,
retry
});
data = [];
event = void 0;
retry = void 0;
}
}
function handleField(field, value) {
switch (field) {
case "event":
event = value;
break;
case "data":
data.push(value);
break;
case "id":
lastEventId = value;
break;
case "retry":
const parsedRetry = parseInt(value, 10);
if (!isNaN(parsedRetry)) {
retry = parsedRetry;
}
break;
}
}
return new TransformStream({
transform(chunk, controller) {
const { lines, incompleteLine } = splitLines(buffer, chunk);
buffer = incompleteLine;
for (let i = 0; i < lines.length; i++) {
parseLine(lines[i], controller);
}
},
flush(controller) {
parseLine(buffer, controller);
dispatchEvent(controller);
}
});
}
function splitLines(buffer, chunk) {
const lines = [];
let currentLine = buffer;
for (let i = 0; i < chunk.length; ) {
const char = chunk[i++];
if (char === "\n") {
lines.push(currentLine);
currentLine = "";
} else if (char === "\r") {
lines.push(currentLine);
currentLine = "";
if (chunk[i] === "\n") {
i++;
}
} else {
currentLine += char;
}
}
return { lines, incompleteLine: currentLine };
}
// src/extract-response-headers.ts
function extractResponseHeaders(response) {
const headers = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
return headers;
}
var createIdGenerator = ({
prefix,
size: defaultSize = 16,
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
separator = "-"
} = {}) => {
const generator = customAlphabet(alphabet, defaultSize);
if (prefix == null) {
return generator;
}
if (alphabet.includes(separator)) {
throw new InvalidArgumentError({
argument: "separator",
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
});
}
return (size) => `${prefix}${separator}${generator(size)}`;
};
var generateId = createIdGenerator();
// src/remove-undefined-entries.ts
function removeUndefinedEntries(record) {
return Object.fromEntries(
Object.entries(record).filter(([_key, value]) => value != null)
);
}
// src/is-abort-error.ts
function isAbortError(error) {
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
}
function loadApiKey({
apiKey,
environmentVariableName,
apiKeyParameterName = "apiKey",
description
}) {
if (typeof apiKey === "string") {
return apiKey;
}
if (apiKey != null) {
throw new LoadAPIKeyError({
message: `${description} API key must be a string.`
});
}
if (typeof process === "undefined") {
throw new LoadAPIKeyError({
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
});
}
apiKey = process.env[environmentVariableName];
if (apiKey == null) {
throw new LoadAPIKeyError({
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
});
}
if (typeof apiKey !== "string") {
throw new LoadAPIKeyError({
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
});
}
return apiKey;
}
// src/load-optional-setting.ts
function loadOptionalSetting({
settingValue,
environmentVariableName
}) {
if (typeof settingValue === "string") {
return settingValue;
}
if (settingValue != null || typeof process === "undefined") {
return void 0;
}
settingValue = process.env[environmentVariableName];
if (settingValue == null || typeof settingValue !== "string") {
return void 0;
}
return settingValue;
}
function loadSetting({
settingValue,
environmentVariableName,
settingName,
description
}) {
if (typeof settingValue === "string") {
return settingValue;
}
if (settingValue != null) {
throw new LoadSettingError({
message: `${description} setting must be a string.`
});
}
if (typeof process === "undefined") {
throw new LoadSettingError({
message: `${description} setting is missing. Pass it using the '${settingName}' parameter. Environment variables is not supported in this environment.`
});
}
settingValue = process.env[environmentVariableName];
if (settingValue == null) {
throw new LoadSettingError({
message: `${description} setting is missing. Pass it using the '${settingName}' parameter or the ${environmentVariableName} environment variable.`
});
}
if (typeof settingValue !== "string") {
throw new LoadSettingError({
message: `${description} setting must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
});
}
return settingValue;
}
// src/validator.ts
var validatorSymbol = Symbol.for("vercel.ai.validator");
function validator(validate) {
return { [validatorSymbol]: true, validate };
}
function isValidator(value) {
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
}
function asValidator(value) {
return isValidator(value) ? value : zodValidator(value);
}
function zodValidator(zodSchema) {
return validator((value) => {
const result = zodSchema.safeParse(value);
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
});
}
// src/validate-types.ts
function validateTypes({
value,
schema: inputSchema
}) {
const result = safeValidateTypes({ value, schema: inputSchema });
if (!result.success) {
throw TypeValidationError.wrap({ value, cause: result.error });
}
return result.value;
}
function safeValidateTypes({
value,
schema
}) {
const validator2 = asValidator(schema);
try {
if (validator2.validate == null) {
return { success: true, value };
}
const result = validator2.validate(value);
if (result.success) {
return result;
}
return {
success: false,
error: TypeValidationError.wrap({ value, cause: result.error })
};
} catch (error) {
return {
success: false,
error: TypeValidationError.wrap({ value, cause: error })
};
}
}
// src/parse-json.ts
function parseJSON({
text,
schema
}) {
try {
const value = SecureJSON.parse(text);
if (schema == null) {
return value;
}
return validateTypes({ value, schema });
} catch (error) {
if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
throw error;
}
throw new JSONParseError({ text, cause: error });
}
}
function safeParseJSON({
text,
schema
}) {
try {
const value = SecureJSON.parse(text);
if (schema == null) {
return { success: true, value, rawValue: value };
}
const validationResult = safeValidateTypes({ value, schema });
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
} catch (error) {
return {
success: false,
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
};
}
}
function isParsableJson(input) {
try {
SecureJSON.parse(input);
return true;
} catch (e) {
return false;
}
}
function parseProviderOptions({
provider,
providerOptions,
schema
}) {
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
return void 0;
}
const parsedProviderOptions = safeValidateTypes({
value: providerOptions[provider],
schema
});
if (!parsedProviderOptions.success) {
throw new InvalidArgumentError({
argument: "providerOptions",
message: `invalid ${provider} provider options`,
cause: parsedProviderOptions.error
});
}
return parsedProviderOptions.value;
}
var getOriginalFetch2 = () => globalThis.fetch;
var postJsonToApi = async ({
url,
headers,
body,
failedResponseHandler,
successfulResponseHandler,
abortSignal,
fetch
}) => postToApi({
url,
headers: {
"Content-Type": "application/json",
...headers
},
body: {
content: JSON.stringify(body),
values: body
},
failedResponseHandler,
successfulResponseHandler,
abortSignal,
fetch
});
var postFormDataToApi = async ({
url,
headers,
formData,
failedResponseHandler,
successfulResponseHandler,
abortSignal,
fetch
}) => postToApi({
url,
headers,
body: {
content: formData,
values: Object.fromEntries(formData.entries())
},
failedResponseHandler,
successfulResponseHandler,
abortSignal,
fetch
});
var postToApi = async ({
url,
headers = {},
body,
successfulResponseHandler,
failedResponseHandler,
abortSignal,
fetch = getOriginalFetch2()
}) => {
try {
const response = await fetch(url, {
method: "POST",
headers: removeUndefinedEntries(headers),
body: body.content,
signal: abortSignal
});
const responseHeaders = extractResponseHeaders(response);
if (!response.ok) {
let errorInformation;
try {
errorInformation = await failedResponseHandler({
response,
url,
requestBodyValues: body.values
});
} catch (error) {
if (isAbortError(error) || APICallError.isInstance(error)) {
throw error;
}
throw new APICallError({
message: "Failed to process error response",
cause: error,
statusCode: response.status,
url,
responseHeaders,
requestBodyValues: body.values
});
}
throw errorInformation.value;
}
try {
return await successfulResponseHandler({
response,
url,
requestBodyValues: body.values
});
} catch (error) {
if (error instanceof Error) {
if (isAbortError(error) || APICallError.isInstance(error)) {
throw error;
}
}
throw new APICallError({
message: "Failed to process successful response",
cause: error,
statusCode: response.status,
url,
responseHeaders,
requestBodyValues: body.values
});
}
} catch (error) {
if (isAbortError(error)) {
throw error;
}
if (error instanceof TypeError && error.message === "fetch failed") {
const cause = error.cause;
if (cause != null) {
throw new APICallError({
message: `Cannot connect to API: ${cause.message}`,
cause,
url,
requestBodyValues: body.values,
isRetryable: true
// retry when network error
});
}
}
throw error;
}
};
// src/resolve.ts
async function resolve(value) {
if (typeof value === "function") {
value = value();
}
return Promise.resolve(value);
}
var createJsonErrorResponseHandler = ({
errorSchema,
errorToMessage,
isRetryable
}) => async ({ response, url, requestBodyValues }) => {
const responseBody = await response.text();
const responseHeaders = extractResponseHeaders(response);
if (responseBody.trim() === "") {
return {
responseHeaders,
value: new APICallError({
message: response.statusText,
url,
requestBodyValues,
statusCode: response.status,
responseHeaders,
responseBody,
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
})
};
}
try {
const parsedError = parseJSON({
text: responseBody,
schema: errorSchema
});
return {
responseHeaders,
value: new APICallError({
message: errorToMessage(parsedError),
url,
requestBodyValues,
statusCode: response.status,
responseHeaders,
responseBody,
data: parsedError,
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
})
};
} catch (parseError) {
return {
responseHeaders,
value: new APICallError({
message: response.statusText,
url,
requestBodyValues,
statusCode: response.status,
responseHeaders,
responseBody,
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
})
};
}
};
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
const responseHeaders = extractResponseHeaders(response);
if (response.body == null) {
throw new EmptyResponseBodyError({});
}
return {
responseHeaders,
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(createEventSourceParserStream()).pipeThrough(
new TransformStream({
transform({ data }, controller) {
if (data === "[DONE]") {
return;
}
controller.enqueue(
safeParseJSON({
text: data,
schema: chunkSchema
})
);
}
})
)
};
};
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
const responseBody = await response.text();
const parsedResult = safeParseJSON({
text: responseBody,
schema: responseSchema
});
const responseHeaders = extractResponseHeaders(response);
if (!parsedResult.success) {
throw new APICallError({
message: "Invalid JSON response",
cause: parsedResult.error,
statusCode: response.status,
responseHeaders,
responseBody,
url,
requestBodyValues
});
}
return {
responseHeaders,
value: parsedResult.value,
rawValue: parsedResult.rawValue
};
};
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
const responseHeaders = extractResponseHeaders(response);
if (!response.body) {
throw new APICallError({
message: "Response body is empty",
url,
requestBodyValues,
statusCode: response.status,
responseHeaders,
responseBody: void 0
});
}
try {
const buffer = await response.arrayBuffer();
return {
responseHeaders,
value: new Uint8Array(buffer)
};
} catch (error) {
throw new APICallError({
message: "Failed to read response as array buffer",
url,
requestBodyValues,
statusCode: response.status,
responseHeaders,
responseBody: void 0,
cause: error
});
}
};
// src/uint8-utils.ts
var { btoa, atob: atob$1 } = globalThis;
function convertBase64ToUint8Array(base64String) {
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
const latin1string = atob$1(base64Url);
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
}
function convertUint8ArrayToBase64(array) {
let latin1string = "";
for (let i = 0; i < array.length; i++) {
latin1string += String.fromCodePoint(array[i]);
}
return btoa(latin1string);
}
// src/without-trailing-slash.ts
function withoutTrailingSlash(url) {
return url == null ? void 0 : url.replace(/\/$/, "");
}
var util;
(function (util) {
util.assertEqual = (val) => val;
function assertIs(_arg) { }
util.assertIs = assertIs;
function assertNever(_x) {
throw new Error();
}
util.assertNever = assertNever;
util.arrayToEnum = (items) => {
const obj = {};
for (const item of items) {
obj[item] = item;
}
return obj;
};
util.getValidEnumValues = (obj) => {
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
const filtered = {};
for (const k of validKeys) {
filtered[k] = obj[k];
}
return util.objectValues(filtered);
};
util.objectValues = (obj) => {
return util.objectKeys(obj).map(function (e) {
return obj[e];
});
};
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
: (object) => {
const keys = [];
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
keys.push(key);
}
}
return keys;
};
util.find = (arr, checker) => {
for (const item of arr) {
if (checker(item))
return item;
}
return undefined;
};
util.isInteger = typeof Number.isInteger === "function"
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
function joinValues(array, separator = " | ") {
return array
.map((val) => (typeof val === "string" ? `'${val}'` : val))
.join(separator);
}
util.joinValues = joinValues;
util.jsonStringifyReplacer = (_, value) => {
if (typeof value === "bigint") {
return value.toString();
}
return value;
};
})(util || (util = {}));
var objectUtil;
(function (objectUtil) {
objectUtil.mergeShapes = (first, second) => {
return {
...first,
...second, // second overwrites first
};
};
})(objectUtil || (objectUtil = {}));
const ZodParsedType = util.arrayToEnum([
"string",
"nan",
"number",
"integer",
"float",
"boolean",
"date",
"bigint",
"symbol",
"function",
"undefined",
"null",
"array",
"object",
"unknown",
"promise",
"void",
"never",
"map",
"set",
]);
const getParsedType = (data) => {
const t = typeof data;
switch (t) {
case "undefined":
return ZodParsedType.undefined;
case "string":
return ZodParsedType.string;
case "number":
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
case "boolean":
return ZodParsedType.boolean;
case "function":
return ZodParsedType.function;
case "bigint":
return ZodParsedType.bigint;
case "symbol":
return ZodParsedType.symbol;
case "object":
if (Array.isArray(data)) {
return ZodParsedType.array;
}
if (data === null) {
return ZodParsedType.null;
}
if (data.then &&
typeof data.then === "function" &&
data.catch &&
typeof data.catch === "function") {
return ZodParsedType.promise;
}
if (typeof Map !== "undefined" && data instanceof Map) {
return ZodParsedType.map;
}
if (typeof Set !== "undefined" && data instanceof Set) {
return ZodParsedType.set;
}
if (typeof Date !== "undefined" && data instanceof Date) {
return ZodParsedType.date;
}
return ZodParsedType.object;
default:
return ZodParsedType.unknown;
}
};
const ZodIssueCode = util.arrayToEnum([
"invalid_type",
"invalid_literal",
"custom",
"invalid_union",
"invalid_union_discriminator",
"invalid_enum_value",
"unrecognized_keys",
"invalid_arguments",
"invalid_return_type",
"invalid_date",
"invalid_string",
"too_small",
"too_big",
"invalid_intersection_types",
"not_multiple_of",
"not_finite",
]);
const quotelessJson = (obj) => {
const json = JSON.stringify(obj, null, 2);
return json.replace(/"([^"]+)":/g, "$1:");
};
class ZodError extends Error {
get errors() {
return this.issues;
}
constructor(issues) {
super();
this.issues = [];
this.addIssue = (sub) => {
this.issues = [...this.issues, sub];
};
this.addIssues = (subs = []) => {
this.issues = [...this.issues, ...subs];
};
const actualProto = new.target.prototype;
if (Object.setPrototypeOf) {
// eslint-disable-next-line ban/ban
Object.setPrototypeOf(this, actualProto);
}
else {
this.__proto__ = actualProto;
}
this.name = "ZodError";
this.issues = issues;
}
format(_mapper) {
const mapper = _mapper ||
function (issue) {
return issue.message;
};
const fieldErrors = { _errors: [] };
const processError = (error) => {
for (const issue of error.issues) {
if (issue.code === "invalid_union") {
issue.unionErrors.map(processError);
}
else if (issue.code === "invalid_return_type") {
processError(issue.returnTypeError);
}
else if (issue.code === "invalid_arguments") {
processError(issue.argumentsError);
}
else if (issue.path.length === 0) {
fieldErrors._errors.push(mapper(issue));
}
else {
let curr = fieldErrors;
let i = 0;
while (i < issue.path.length) {
const el = issue.path[i];
const terminal = i === issue.path.length - 1;
if (!terminal) {
curr[el] = curr[el] || { _errors: [] };
// if (typeof el === "string") {
// curr[el] = curr[el] || { _errors: [] };
// } else if (typeof el === "number") {
// const errorArray: any = [];
// errorArray._errors = [];
// curr[el] = curr[el] || errorArray;
// }
}
else {
curr[el] = curr[el] || { _errors: [] };
curr[el]._errors.push(mapper(issue));
}
curr = curr[el];
i++;
}
}