@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
94 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncStandardSchemaValidationError = void 0;
exports.isAsyncStandardSchemaValidationError = isAsyncStandardSchemaValidationError;
exports.hasStandardSchemaMarker = hasStandardSchemaMarker;
exports.isStandardSchemaWithJSON = isStandardSchemaWithJSON;
exports.unsupportedStandardSchemaError = unsupportedStandardSchemaError;
exports.standardSchemaToJsonSchema = standardSchemaToJsonSchema;
exports.assertStandardSchemaObjectRoot = assertStandardSchemaObjectRoot;
exports.validateStandardSchema = validateStandardSchema;
const errors_1 = require("../errors.js");
const asyncStandardSchemaValidationErrors = new WeakSet();
/** @internal */
class AsyncStandardSchemaValidationError extends errors_1.UserError {
constructor(message) {
super(message);
asyncStandardSchemaValidationErrors.add(this);
}
}
exports.AsyncStandardSchemaValidationError = AsyncStandardSchemaValidationError;
/** @internal */
function isAsyncStandardSchemaValidationError(error) {
return ((typeof error === 'object' || typeof error === 'function') &&
error !== null &&
asyncStandardSchemaValidationErrors.has(error));
}
function hasStandardSchemaMarker(schema) {
try {
return ((typeof schema === 'object' || typeof schema === 'function') &&
schema !== null &&
'~standard' in schema);
}
catch {
return false;
}
}
function isStandardSchemaWithJSON(schema) {
try {
if (!hasStandardSchemaMarker(schema)) {
return false;
}
const standard = schema['~standard'];
return (standard?.version === 1 &&
typeof standard.validate === 'function' &&
typeof standard.jsonSchema?.input === 'function' &&
typeof standard.jsonSchema?.output === 'function');
}
catch {
return false;
}
}
function unsupportedStandardSchemaError() {
return new errors_1.UserError('Standard Schema values must provide both `~standard.validate` and `~standard.jsonSchema` conversion. Upgrade the schema library or use its Standard JSON Schema adapter.');
}
function isPromiseLike(value) {
return ((typeof value === 'object' || typeof value === 'function') &&
value !== null &&
typeof value.then === 'function');
}
function standardSchemaToJsonSchema(schema, io) {
try {
return schema['~standard'].jsonSchema[io]({ target: 'draft-2020-12' });
}
catch (error) {
const detail = error instanceof Error ? ` ${error.message}` : '';
throw new errors_1.UserError(`Unable to convert the provided Standard Schema to JSON Schema.${detail}`);
}
}
function assertStandardSchemaObjectRoot(schema, surface) {
if (schema.type !== 'object') {
throw new errors_1.UserError(`${surface} Standard Schema must produce a JSON Schema with \`type: "object"\` at the root. Wrap the schema in an object schema.`);
}
}
function formatIssue(issue) {
const path = issue.path
?.map((segment) => String(typeof segment === 'object' && segment !== null ? segment.key : segment))
.join('.');
return path ? `${path}: ${issue.message}` : issue.message;
}
function validationError(issues) {
return new Error(issues.map(formatIssue).join(', '));
}
function validateStandardSchema(schema, value) {
const result = schema['~standard'].validate(value);
if (isPromiseLike(result)) {
void Promise.resolve(result).catch(() => { });
throw new AsyncStandardSchemaValidationError('Standard Schema validation returned a Promise. Async Standard Schema validation is not supported.');
}
if (result.issues) {
throw validationError(result.issues);
}
return result.value;
}
//# sourceMappingURL=standardSchema.js.map