@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
83 lines • 3.17 kB
JavaScript
import { UserError } from "../errors.mjs";
const asyncStandardSchemaValidationErrors = new WeakSet();
/** @internal */
export class AsyncStandardSchemaValidationError extends UserError {
constructor(message) {
super(message);
asyncStandardSchemaValidationErrors.add(this);
}
}
/** @internal */
export function isAsyncStandardSchemaValidationError(error) {
return ((typeof error === 'object' || typeof error === 'function') &&
error !== null &&
asyncStandardSchemaValidationErrors.has(error));
}
export function hasStandardSchemaMarker(schema) {
try {
return ((typeof schema === 'object' || typeof schema === 'function') &&
schema !== null &&
'~standard' in schema);
}
catch {
return false;
}
}
export 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;
}
}
export function unsupportedStandardSchemaError() {
return new 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');
}
export 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 UserError(`Unable to convert the provided Standard Schema to JSON Schema.${detail}`);
}
}
export function assertStandardSchemaObjectRoot(schema, surface) {
if (schema.type !== 'object') {
throw new 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(', '));
}
export 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.mjs.map