mcard-js
Version:
MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers
88 lines • 2.66 kB
JavaScript
/**
* Utility functions for CLM execution.
*/
/**
* Compare two values with floating-point tolerance for numeric types.
*/
export function resultsEqual(a, b, tolerance = 1e-9) {
// Both null or undefined
if (a === b)
return true;
if (a == null || b == null)
return false;
// Numbers - use tolerance
if (typeof a === 'number' && typeof b === 'number') {
if (Number.isNaN(a) && Number.isNaN(b))
return true;
if (!Number.isFinite(a) || !Number.isFinite(b))
return a === b;
return Math.abs(a - b) <= tolerance * Math.max(1, Math.abs(a), Math.abs(b));
}
// Arrays - compare element by element
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length)
return false;
return a.every((val, i) => resultsEqual(val, b[i], tolerance));
}
// Objects - compare properties
if (typeof a === 'object' && typeof b === 'object') {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length)
return false;
return keysA.every(key => keysB.includes(key) &&
resultsEqual(a[key], b[key], tolerance));
}
// Strings and other types - strict equality
return a === b;
}
/**
* Safely convert input to object.
*/
export function asObject(input) {
return typeof input === 'object' && input !== null ? input : {};
}
/**
* Build base context for execution.
*/
export function buildBaseContext(clm, input) {
const inputObj = asObject(input);
return {
balanced: clm.clm.balanced,
params: inputObj,
...inputObj,
};
}
/**
* Build execution result object.
*/
export function buildExecutionResult(success, result, startTime, clm, error, boundary) {
return {
success,
result,
error,
executionTime: Date.now() - startTime,
clm: {
chapter: clm.chapter?.title || 'Unknown',
concept: clm.clm.abstract?.concept || 'Unknown',
manifestation: clm.clm.concrete?.manifestation || 'Unknown',
boundary,
}
};
}
/**
* Check if runtime name indicates a recursive CLM.
*/
export function isRecursiveCLM(runtimeName) {
return runtimeName.endsWith('.clm') ||
runtimeName.endsWith('.yaml') ||
runtimeName.endsWith('.yml');
}
/**
* Check if CLM has multi-runtime configuration.
*/
export function isMultiRuntime(clm) {
const runtimesConfig = clm.clm?.concrete?.runtimes_config;
return Array.isArray(runtimesConfig) && runtimesConfig.length > 0;
}
//# sourceMappingURL=utils.js.map