@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
77 lines • 2.72 kB
JavaScript
const objectConstructorSource = Function.prototype.toString.call(Object);
function isPlainObject(value) {
const prototype = Object.getPrototypeOf(value);
if (prototype === null) {
return true;
}
const constructor = Object.prototype.hasOwnProperty.call(prototype, 'constructor')
? prototype.constructor
: undefined;
return (typeof constructor === 'function' &&
Function.prototype.toString.call(constructor) === objectConstructorSource);
}
function isCloneableBuffer(value) {
const tag = Object.prototype.toString.call(value);
return tag === '[object ArrayBuffer]' || tag === '[object SharedArrayBuffer]';
}
/**
* Copies mutable data containers used by public testing call records while
* preserving runtime identities such as signals, callbacks, and class
* instances. Cycles and shared references are preserved within the snapshot.
*
*/
export function snapshotTestingValue(value, seen = new WeakMap()) {
if (typeof value !== 'object' || value === null) {
return value;
}
const existing = seen.get(value);
if (typeof existing !== 'undefined') {
return existing;
}
if (isCloneableBuffer(value)) {
const snapshot = value.slice(0);
seen.set(value, snapshot);
return snapshot;
}
if (ArrayBuffer.isView(value)) {
const buffer = snapshotTestingValue(value.buffer, seen);
let snapshot;
if (Object.prototype.toString.call(value) === '[object DataView]') {
snapshot = new DataView(buffer, value.byteOffset, value.byteLength);
}
else {
const TypedArray = value.constructor;
const length = value.length;
snapshot = new TypedArray(buffer, value.byteOffset, length);
}
seen.set(value, snapshot);
return snapshot;
}
if (Array.isArray(value)) {
const snapshot = [];
seen.set(value, snapshot);
for (const entry of value) {
snapshot.push(snapshotTestingValue(entry, seen));
}
return snapshot;
}
if (!isPlainObject(value)) {
return value;
}
const snapshot = Object.create(Object.getPrototypeOf(value));
seen.set(value, snapshot);
for (const key of Reflect.ownKeys(value)) {
const descriptor = Object.getOwnPropertyDescriptor(value, key);
if (!descriptor?.enumerable) {
continue;
}
Object.defineProperty(snapshot, key, {
configurable: true,
enumerable: true,
value: snapshotTestingValue(value[key], seen),
writable: true,
});
}
return snapshot;
}
//# sourceMappingURL=testingSnapshot.mjs.map