@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
73 lines • 2.23 kB
JavaScript
const INVALID_JSON_VALUE = Symbol('invalidJsonValue');
/**
* Creates a detached JSON-compatible snapshot of a provider usage object.
*/
export function snapshotRawUsage(rawUsage) {
if (typeof rawUsage !== 'object' ||
rawUsage === null ||
Array.isArray(rawUsage)) {
return undefined;
}
try {
const snapshot = cloneJsonValue(rawUsage, new Set());
return snapshot === INVALID_JSON_VALUE ||
snapshot === null ||
typeof snapshot !== 'object' ||
Array.isArray(snapshot)
? undefined
: snapshot;
}
catch {
return undefined;
}
}
function cloneJsonValue(value, ancestors) {
if (value === null) {
return null;
}
if (typeof value === 'string' ||
typeof value === 'boolean' ||
(typeof value === 'number' && Number.isFinite(value))) {
return value;
}
if (typeof value === 'undefined') {
return INVALID_JSON_VALUE;
}
if (typeof value !== 'object' || ancestors.has(value)) {
return INVALID_JSON_VALUE;
}
ancestors.add(value);
try {
if (Array.isArray(value)) {
const result = [];
for (const entry of value) {
const cloned = cloneJsonValue(entry, ancestors);
if (cloned === INVALID_JSON_VALUE) {
return INVALID_JSON_VALUE;
}
result.push(cloned);
}
return result;
}
const prototype = Object.getPrototypeOf(value);
if (prototype !== Object.prototype && prototype !== null) {
return INVALID_JSON_VALUE;
}
const entries = [];
for (const [key, entry] of Object.entries(value)) {
if (typeof entry === 'undefined') {
continue;
}
const cloned = cloneJsonValue(entry, ancestors);
if (cloned === INVALID_JSON_VALUE) {
return INVALID_JSON_VALUE;
}
entries.push([key, cloned]);
}
return Object.fromEntries(entries);
}
finally {
ancestors.delete(value);
}
}
//# sourceMappingURL=rawUsage.mjs.map