UNPKG

eve

Version:

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

1 lines 3.76 kB
import{createLogger,formatError}from"#internal/logging.js";import{normalizeInstrumentationChannelKind,resolveInstrumentationProjection}from"#internal/instrumentation.js";import{contextStorage}from"#context/container.js";import{AuthKey,ChannelInstrumentationKey,InitiatorAuthKey,ParentSessionKey}from"#context/keys.js";import{parseJsonObject,parseJsonValue}from"#shared/json.js";const log=createLogger(`harness.instrumentation-runtime-context`);function snapshotInstrumentationRuntimeContext(e){let t=e?.get(AuthKey)??null;return{channel:snapshotForInstrumentation(e?.get(ChannelInstrumentationKey),`channel.instrumentation`),currentAuth:snapshotForInstrumentation(t,`session.auth.current`)??null,initiatorAuth:snapshotForInstrumentation(e?.get(InitiatorAuthKey)??t,`session.auth.initiator`)??null,parent:snapshotForInstrumentation(e?.get(ParentSessionKey),`session.parent`)}}function buildTelemetryRuntimeContext(e){let t=e.stepStartedResolver!==void 0,n=e.providerResolvers!==void 0&&e.providerResolvers.length>0;if(!t&&!n)return;let r=resolveStepStartedRuntimeContext(e),i=resolveProviderRuntimeContext(e),a=(e.context??snapshotInstrumentationRuntimeContext(contextStorage.getStore())).channel;return{...r,...i,"eve.channel.kind":normalizeInstrumentationChannelKind(a?.kind),"eve.environment":e.environment,"eve.session.id":e.session.sessionId,"eve.step.index":String(e.emissionState.stepIndex),"eve.turn.id":e.emissionState.turnId,"eve.turn.sequence":String(e.emissionState.sequence),"eve.version":e.eveVersion}}function buildInstrumentationStepStartedInput(e){let t=e.context??snapshotInstrumentationRuntimeContext(contextStorage.getStore()),n=t.channel;return{channel:{kind:normalizeInstrumentationChannelKind(n?.kind),metadata:snapshotForInstrumentation(n?.metadata,`channel.metadata`)??{}},modelInput:e.capturesContent?snapshotForInstrumentation(e.modelInput,`modelInput`)??{instructions:void 0,messages:[]}:{instructions:void 0,messages:[]},session:{auth:projectSessionAuth(t),id:e.session.sessionId,parent:t.parent},step:{index:e.emissionState.stepIndex},turn:{id:e.emissionState.turnId,sequence:e.emissionState.sequence}}}function filterAuthoredRuntimeContext(e,t){let n={};for(let[r,i]of Object.entries(e)){if(r.startsWith(`eve.`)){log.warn(`ignoring reserved instrumentation runtime context key`,{key:r,source:t});continue}n[r]=i}return Object.keys(n).length>0?n:void 0}function resolveStepStartedRuntimeContext(e){let t=e.stepStartedResolver;if(t===void 0)return;let n=`events["step.started"]`,r=resolveInstrumentationProjection({invoke:()=>{let n=buildInstrumentationStepStartedInput(e);return t(n)},log,source:n});if(r===void 0)return;if(!(`runtimeContext`in r)){log.warn(`ignoring instrumentation event result because runtimeContext is missing`,{source:n});return}let i;try{i=parseJsonObject(r.runtimeContext)}catch(e){log.warn(`ignoring instrumentation event result because runtimeContext is invalid`,{error:formatError(e),source:n});return}return filterAuthoredRuntimeContext(i,n)}function resolveProviderRuntimeContext(e){let t=e.providerResolvers;if(t===void 0||t.length===0)return;let n={},r=!1;for(let i of t){let t=`provider.runtimeContext`,a=resolveInstrumentationProjection({invoke:()=>{let t=buildInstrumentationStepStartedInput(e);return i(t)},log,source:t});if(a===void 0)continue;let o=filterAuthoredRuntimeContext(a,t);o!==void 0&&(Object.assign(n,o),r=!0)}return r?n:void 0}function projectSessionAuth(e){return{current:e.currentAuth,initiator:e.initiatorAuth}}function snapshotForInstrumentation(e,t){if(e!==void 0)try{return parseJsonValue(e)}catch(e){log.warn(`dropping non-serializable instrumentation snapshot`,{error:formatError(e),source:t});return}}export{buildTelemetryRuntimeContext,snapshotInstrumentationRuntimeContext};