UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.12 kB
import{createHash}from"node:crypto";import{actionIdempotencyKey}from"#instrumentation/lifecycle.js";var AgentSpanIdGenerator=class{#e;#t;allocateSpanId(){return randomHexId(16)}deriveSpanId(e){return deriveAgentSpanId(e)}deriveTraceId(e){let t=createHash(`sha256`).update(e).digest(`hex`).slice(0,32);return/^0+$/u.test(t)?`00000000000000000000000000000001`:t}generateSpanId(){let e=this.#e;return e===void 0?randomHexId(16):(this.#e=void 0,e)}generateTraceId(){let e=this.#t;return e===void 0?randomHexId(32):(this.#t=void 0,e)}withSpanId(e,t){this.#e=e;try{return t()}finally{this.#e=void 0}}withTraceId(e,t){this.#t=e;try{return t()}finally{this.#t=void 0}}};function deriveAgentSpanId(e){let t=createHash(`sha256`).update(e).digest(`hex`).slice(0,16);return/^0+$/u.test(t)?`0000000000000001`:t}function deriveAgentActionSpanId(e,t,n){return deriveAgentSpanId(`action:${actionIdempotencyKey(e,t,n)}`)}function randomHexId(e){let t;do{t=``;for(let n=0;n<e;n+=1)t+=`0123456789abcdef`[Math.trunc(Math.random()*16)]}while(!/[1-9a-f]/u.test(t));return t}export{AgentSpanIdGenerator,deriveAgentActionSpanId,deriveAgentSpanId};