ai-sdk-guardrails
Version:
Input and output guardrails middleware for Vercel AI SDK.
160 lines (158 loc) • 5.05 kB
JavaScript
// src/errors.ts
var GuardrailsError = class extends Error {
timestamp;
metadata;
constructor(message, metadata = {}) {
super(message);
this.timestamp = /* @__PURE__ */ new Date();
this.metadata = metadata;
Object.setPrototypeOf(this, new.target.prototype);
}
/**
* Convert the error to a serializable object for logging/reporting
*/
toJSON() {
return {
name: this.name,
code: this.code,
message: this.message,
timestamp: this.timestamp.toISOString(),
metadata: this.metadata,
stack: this.stack
};
}
/**
* Check if this error is of a specific type
*/
is(errorClass) {
return this instanceof errorClass;
}
};
var GuardrailValidationError = class extends GuardrailsError {
name = "GuardrailValidationError";
code = "GUARDRAIL_VALIDATION_FAILED";
guardrailName;
validationErrors;
constructor(guardrailName, validationErrors, metadata = {}) {
const message = `Guardrail "${guardrailName}" validation failed: ${validationErrors.map((e) => e.message).join(", ")}`;
super(message, { ...metadata, guardrailName, validationErrors });
this.guardrailName = guardrailName;
this.validationErrors = validationErrors;
}
};
var GuardrailExecutionError = class extends GuardrailsError {
name = "GuardrailExecutionError";
code = "GUARDRAIL_EXECUTION_FAILED";
guardrailName;
originalError;
constructor(guardrailName, originalError, metadata = {}) {
const message = originalError ? `Guardrail "${guardrailName}" execution failed: ${originalError.message}` : `Guardrail "${guardrailName}" execution failed`;
super(message, {
...metadata,
guardrailName,
originalError: originalError?.message
});
this.guardrailName = guardrailName;
this.originalError = originalError;
}
};
var GuardrailTimeoutError = class extends GuardrailsError {
name = "GuardrailTimeoutError";
code = "GUARDRAIL_TIMEOUT";
guardrailName;
timeoutMs;
constructor(guardrailName, timeoutMs, metadata = {}) {
const message = `Guardrail "${guardrailName}" timed out after ${timeoutMs}ms`;
super(message, { ...metadata, guardrailName, timeoutMs });
this.guardrailName = guardrailName;
this.timeoutMs = timeoutMs;
}
};
var GuardrailConfigurationError = class extends GuardrailsError {
name = "GuardrailConfigurationError";
code = "GUARDRAIL_CONFIG_INVALID";
configPath;
configErrors;
constructor(configErrors, configPath, metadata = {}) {
const message = `Guardrail configuration error${configPath ? ` in ${configPath}` : ""}: ${configErrors.join(", ")}`;
super(message, { ...metadata, configPath, configErrors });
this.configPath = configPath;
this.configErrors = configErrors;
}
};
var InputBlockedError = class extends GuardrailsError {
name = "InputBlockedError";
code = "INPUT_BLOCKED";
blockedGuardrails;
constructor(blockedGuardrails, metadata = {}) {
const guardrailNames = blockedGuardrails.map((g) => g.name).join(", ");
const message = `Input blocked by guardrail${blockedGuardrails.length > 1 ? "s" : ""}: ${guardrailNames}`;
super(message, { ...metadata, blockedGuardrails });
this.blockedGuardrails = blockedGuardrails;
}
};
var OutputBlockedError = class extends GuardrailsError {
name = "OutputBlockedError";
code = "OUTPUT_BLOCKED";
blockedGuardrails;
constructor(blockedGuardrails, metadata = {}) {
const guardrailNames = blockedGuardrails.map((g) => g.name).join(", ");
const message = `Output blocked by guardrail${blockedGuardrails.length > 1 ? "s" : ""}: ${guardrailNames}`;
super(message, { ...metadata, blockedGuardrails });
this.blockedGuardrails = blockedGuardrails;
}
};
var MiddlewareError = class extends GuardrailsError {
name = "MiddlewareError";
code = "MIDDLEWARE_ERROR";
middlewareType;
phase;
originalError;
constructor(middlewareType, phase, originalError, metadata = {}) {
const message = originalError ? `${middlewareType} middleware ${phase} error: ${originalError.message}` : `${middlewareType} middleware ${phase} error`;
super(message, {
...metadata,
middlewareType,
phase,
originalError: originalError?.message
});
this.middlewareType = middlewareType;
this.phase = phase;
this.originalError = originalError;
}
};
function isGuardrailsError(error) {
return error instanceof GuardrailsError;
}
function extractErrorInfo(error) {
if (isGuardrailsError(error)) {
return {
name: error.name,
message: error.message,
code: error.code,
metadata: error.metadata
};
}
if (error instanceof Error) {
return {
name: error.name,
message: error.message
};
}
return {
name: "UnknownError",
message: String(error)
};
}
export {
GuardrailsError,
GuardrailValidationError,
GuardrailExecutionError,
GuardrailTimeoutError,
GuardrailConfigurationError,
InputBlockedError,
OutputBlockedError,
MiddlewareError,
isGuardrailsError,
extractErrorInfo
};