eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.35 kB
JavaScript
import{toErrorMessage}from"#shared/errors.js";var AssertionCollector=class{#e=[];#t=[];recordRun(e,t=`gate`){let n={name:e.name,severity:t,threshold:void 0,kind:`deferred`,spec:e,score:0,failed:!1};return this.#e.push(n),makeHandle(n,Promise.resolve())}recordValue(t){let n={name:t.name,severity:t.severity,threshold:t.threshold,kind:`resolved`,score:0,failed:!1};this.#e.push(n);let r=t.score().then(e=>{n.score=e.score,n.message=e.message,n.metadata=e.metadata}).catch(t=>{n.score=0,n.severity=`gate`,n.threshold=void 0,n.message=toErrorMessage(t),n.failed=!0});return this.#t.push(r),makeHandle(n,r)}async finalize(e){await Promise.all(this.#t);let t=[];for(let n of this.#e){if(n.kind===`deferred`&&n.spec!==void 0){let t=await n.spec.evaluate(e);n.score=t.score,n.message=t.message,n.metadata=t.metadata}t.push({name:n.name,score:n.score,severity:n.severity,threshold:n.threshold,passed:computePassed(n.severity,n.threshold,n.score,n.failed),message:n.message,metadata:n.metadata})}return t}};function computePassed(e,t,n,r){if(r)return!1;let i=t??(e===`gate`?1:void 0);return i===void 0||n>=i}function makeHandle(e,t){let n={gate(t){return e.severity=`gate`,e.threshold=t,n},soft(t){return e.severity=`soft`,e.threshold=t,n},atLeast(t){return e.severity=`soft`,e.threshold=t,n},then(e,n){return t.then(e,n)}};return n}export{AssertionCollector};