UNPKG

eve

Version:

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

1 lines • 3.04 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{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){if(!(e.providerResolvers!==void 0&&e.providerResolvers.length>0))return;let t=resolveProviderRuntimeContext(e),n=(e.context??snapshotInstrumentationRuntimeContext(contextStorage.getStore())).channel;return{...t,"eve.channel.kind":normalizeInstrumentationChannelKind(n?.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 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};