agentfootprint
Version:
The explainable agent framework — backtrack a wrong answer to the exact context that caused it (evidence, not guesses). Built on footprintjs.
45 lines • 1.74 kB
JavaScript
import { ReliabilityFailFastError } from './types.js';
/** The keys of the record, listed once and checked against `AgentState`. */
export const RELIABILITY_FAIL_KEYS = [
'reliabilityFailKind',
'reliabilityFailPayload',
'reliabilityFailReason',
'reliabilityFailCauseMessage',
'reliabilityFailCauseName',
];
/**
* The fail-fast record a state holds: its present keys, or nothing when no
* fail-fast fired — so a boundary that spreads it crosses no new key otherwise.
*/
export function failFastRecordOf(state) {
if (state.reliabilityFailKind === undefined)
return undefined;
const record = {};
for (const key of RELIABILITY_FAIL_KEYS) {
if (state[key] !== undefined)
record[key] = state[key];
}
return record;
}
/**
* The error a fail-fast record becomes. The cause is rebuilt from the message
* and name the record keeps — an `Error` does not survive the state's
* `structuredClone`, so the record never held the original.
*/
export function failFastErrorOf(record, snapshot) {
const kind = record.reliabilityFailKind ?? 'unknown';
let cause;
if (record.reliabilityFailCauseMessage !== undefined) {
cause = new Error(record.reliabilityFailCauseMessage);
if (record.reliabilityFailCauseName !== undefined)
cause.name = record.reliabilityFailCauseName;
}
return new ReliabilityFailFastError({
kind,
reason: record.reliabilityFailReason ?? kind,
...(cause !== undefined && { cause }),
...(record.reliabilityFailPayload !== undefined && { payload: record.reliabilityFailPayload }),
...(snapshot !== undefined && { snapshot }),
});
}
//# sourceMappingURL=failFastRecord.js.map