@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
69 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeToolOutputCustomData = normalizeToolOutputCustomData;
exports.maybeExtractToolOutputCustomData = maybeExtractToolOutputCustomData;
const errors_1 = require("../errors.js");
function normalizeToolOutputCustomData(value) {
if (value == null) {
return undefined;
}
if (!isRecord(value)) {
throw new errors_1.UserError('customDataExtractor must return an object or null.');
}
if (Reflect.ownKeys(value).some((key) => typeof key !== 'string')) {
throw new errors_1.UserError('customDataExtractor must return an object with string keys.');
}
if (Object.keys(value).length === 0) {
return undefined;
}
assertJsonCompatible(value, 'customDataExtractor result');
return JSON.parse(JSON.stringify(value));
}
async function maybeExtractToolOutputCustomData(extractor, context) {
if (!extractor) {
return undefined;
}
return normalizeToolOutputCustomData(await extractor(context));
}
function isRecord(value) {
return (!!value &&
typeof value === 'object' &&
!Array.isArray(value) &&
(Object.getPrototypeOf(value) === Object.prototype ||
Object.getPrototypeOf(value) === null));
}
function assertJsonCompatible(value, path) {
if (value == null) {
return;
}
const valueType = typeof value;
if (valueType === 'string' || valueType === 'boolean') {
return;
}
if (valueType === 'number') {
if (!Number.isFinite(value)) {
throw new errors_1.UserError('customDataExtractor must return JSON-compatible data.');
}
return;
}
if (valueType === 'undefined' ||
valueType === 'function' ||
valueType === 'symbol' ||
valueType === 'bigint') {
throw new errors_1.UserError('customDataExtractor must return JSON-compatible data.');
}
if (Array.isArray(value)) {
value.forEach((entry, index) => assertJsonCompatible(entry, `${path}[${index}]`));
return;
}
if (!isRecord(value)) {
throw new errors_1.UserError('customDataExtractor must return JSON-compatible data.');
}
for (const key of Reflect.ownKeys(value)) {
if (typeof key !== 'string') {
throw new errors_1.UserError('customDataExtractor must return JSON-compatible data.');
}
assertJsonCompatible(value[key], `${path}.${key}`);
}
}
//# sourceMappingURL=customData.js.map