eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.78 kB
JavaScript
import{ROOT_CONTEXT,SpanKind,trace}from"#compiled/@opentelemetry/api/index.js";import{actionIdempotencyKey}from"#instrumentation/lifecycle.js";import{agentSpanNamingAttributes}from"#tracing/agent-span-naming.js";import{agentTraceIdentityAttributes}from"#tracing/agent-otel-attributes.js";import{contentAttribute}from"#tracing/agent-otel-content.js";import{withChannelAudience}from"#tracing/channel-audience-context.js";import{recordAgentSpanError}from"#tracing/agent-span-error.js";function createAgentToolInstrumentation(e){let t=new Map,n=new Map,onStarted=async t=>{let n=actionIdempotencyKey(t.scope.sessionId,t.scope.turnId,t.callId),r=e.resolveFallback(t),i=r===void 0?void 0:reserve(t,n,r),a=await e.actionContextFor(t.scope.sessionId,t.scope.turnId,t.callId);if(i===void 0){if(a===void 0)return;i=reserve(t,n,a)}a!==void 0&&startSpan(i,a.context)},onTerminal=async t=>{let r=n.get(t.scope.attemptId)?.get(t.idempotencyKey);if(r!==void 0){if(r.terminal=t,r.span===void 0){let t=await e.actionContextFor(r.event.scope.sessionId,r.event.scope.turnId,r.event.callId);t!==void 0&&startSpan(r,t.context)}finishIfReady(r)}};return{async actionStarted(n){let r=t.get(n.idempotencyKey);if(r===void 0||r.span!==void 0||r.finished===!0)return;let i=await e.actionContextFor(n.scope.sessionId,n.scope.turnId,n.callId);i!==void 0&&(startSpan(r,i.context),finishIfReady(r))},contextFor:(e,t)=>n.get(e)?.get(t)?.context,drain(e,t){let r=n.get(e);if(r!==void 0){for(let e of r.values())e.finished!==!0&&(e.span===void 0&&startSpan(e,e.fallbackParent),finish(e,t));n.delete(e)}},events:{"tool.call.completed":onTerminal,"tool.call.failed":onTerminal,"tool.call.started":onStarted}};function getAttemptStates(e){let t=n.get(e);return t===void 0&&(t=new Map,n.set(e,t)),t}function reserve(n,r,i){let a=e.idGenerator.deriveSpanId(`tool:${n.idempotencyKey}`),o={actionKey:r,attemptId:n.scope.attemptId,context:withChannelAudience(contextFromSpanContext({isRemote:!1,spanId:a,traceFlags:i.spanContext.traceFlags,traceId:i.spanContext.traceId}),n.scope.channelAudience),event:n,fallbackParent:i.context,idempotencyKey:n.idempotencyKey,spanId:a,startTimeMs:Date.now()};return getAttemptStates(n.scope.attemptId).set(n.idempotencyKey,o),t.set(r,o),o}function startSpan(t,n){if(t.span===void 0&&t.finished!==!0&&(t.span=e.idGenerator.withSpanId(t.spanId,()=>e.tracer.startSpan(`execute_tool ${t.event.toolName}`,{attributes:toolAttributes(t.event),kind:SpanKind.INTERNAL,startTime:t.startTimeMs},n)),e.recordInputs)){let e=contentAttribute(t.event.input);e!==void 0&&t.span.setAttribute(`gen_ai.tool.call.arguments`,e)}}function finishIfReady(e){e.span!==void 0&&e.terminal!==void 0&&finish(e)}function finish(r,i){let a=r.span;if(a===void 0||r.finished===!0)return;r.finished=!0;let o=r.terminal;if(i!==void 0)recordAgentSpanError(a,i.error);else if(o?.type===`tool.call.failed`)recordAgentSpanError(a,o.error);else if(o?.output.type===`error`)recordAgentSpanError(a,o.output.error);else if(o!==void 0&&e.recordOutputs){let e=contentAttribute(o.output.output);e!==void 0&&a.setAttribute(`gen_ai.tool.call.result`,e)}a.end(),t.delete(r.actionKey);let s=n.get(r.attemptId);s?.delete(r.idempotencyKey),s?.size===0&&n.delete(r.attemptId)}}function toolAttributes(e){return{...e.scope.functionId===void 0?{}:{"gen_ai.agent.name":e.scope.functionId},"gen_ai.operation.name":`execute_tool`,"gen_ai.tool.call.id":e.callId,"gen_ai.tool.name":e.toolName,"gen_ai.tool.type":`function`,...agentSpanNamingAttributes(`execute_tool ${e.toolName}`,`execute_tool`),...agentTraceIdentityAttributes({rootSessionId:e.scope.rootSessionId??e.scope.sessionId,sessionId:e.scope.sessionId})}}function contextFromSpanContext(e){return trace.setSpan(ROOT_CONTEXT,trace.wrapSpanContext(e))}export{createAgentToolInstrumentation};