UNPKG

eve

Version:

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

1 lines 1.79 kB
import{AGENT_USAGE_ATTRIBUTES}from"#tracing/agent-span-contract.js";function setAgentUsage(e,t){t.inputTokens!==void 0&&e.setAttribute(AGENT_USAGE_ATTRIBUTES.inputTokens,t.inputTokens),t.outputTokens!==void 0&&e.setAttribute(AGENT_USAGE_ATTRIBUTES.outputTokens,t.outputTokens);let n=t.inputTokenDetails;n?.cacheReadTokens!==void 0&&e.setAttribute(AGENT_USAGE_ATTRIBUTES.cacheReadTokens,n.cacheReadTokens),n?.cacheWriteTokens!==void 0&&e.setAttribute(AGENT_USAGE_ATTRIBUTES.cacheWriteTokens,n.cacheWriteTokens)}function setGenAiUsage(e,t){setAgentUsage(e,t),t.inputTokens!==void 0&&e.setAttribute(`gen_ai.usage.input_tokens`,t.inputTokens),t.outputTokens!==void 0&&e.setAttribute(`gen_ai.usage.output_tokens`,t.outputTokens);let n=t.inputTokenDetails;n?.cacheReadTokens!==void 0&&e.setAttribute(`gen_ai.usage.cache_read.input_tokens`,n.cacheReadTokens),n?.cacheWriteTokens!==void 0&&e.setAttribute(`gen_ai.usage.cache_creation.input_tokens`,n.cacheWriteTokens)}function setAgentInvocationUsage(e,t){t!==void 0&&setGenAiUsage(e,t)}function readGatewayCost(e){let t=e.gateway;if(!isRecord(t))return;let n={},r=readUsd(t.cost);r!==void 0&&(n[`gen_ai.usage.cost`]=r);let i=readUsd(t.gatewayCost);i!==void 0&&(n[`gen_ai.usage.gateway_cost`]=i);let a=readUsd(t.inputInferenceCost);a!==void 0&&(n[`gen_ai.usage.input_cost`]=a);let o=readUsd(t.outputInferenceCost);return o!==void 0&&(n[`gen_ai.usage.output_cost`]=o),typeof t.generationId==`string`&&t.generationId.length>0&&(n[`gen_ai.generation.id`]=t.generationId),Object.keys(n).length===0?void 0:n}function readUsd(e){if(typeof e!=`string`)return;let t=Number(e);return Number.isFinite(t)?t:void 0}function isRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}export{readGatewayCost,setAgentInvocationUsage,setAgentUsage,setGenAiUsage};