UNPKG

eve

Version:

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

1 lines 4.08 kB
import{inspect}from"node:util";import{isNonEmptyString,isObject}from"#shared/guards.js";import{SpanStatusCode,trace}from"#compiled/@opentelemetry/api/index.js";import{truncateTelemetryText}from"#tracing/telemetry-budget.js";import{capturesErrorContent}from"#tracing/error-content-context.js";import{getErrorMessage}from"#compiled/@ai-sdk/provider/index.js";const LEVEL_SEVERITY={debug:10,info:20,warn:30,error:40};function createLogger(e){return{debug(t,n){write(`debug`,e,t,n)},info(t,n){write(`info`,e,t,n)},warn(t,n){write(`warn`,e,t,n)},error(t,n){write(`error`,e,t,n),recordOnActiveSpan(t,n)}}}function logError(e,t,n,r){let i=formatError(n);return e.error(t,{...r,error:i}),typeof i.errorId==`string`?i.errorId:createErrorId()}function createErrorId(){return crypto.randomUUID()}function formatError(e,t){let n={errorId:t??createErrorId(),message:extractErrorMessage(e)},r=extractErrorName(e);return r!==void 0&&(n.name=r),n.detail=inspectError(e),n}function extractErrorName(e){if(e instanceof Error)return e.name===`Error`?void 0:e.name;if(isObject(e)&&isNonEmptyString(e.name)&&e.name!==`Error`)return e.name}function extractErrorMessage(e){return isObject(e)&&!(e instanceof Error)&&typeof e.message==`string`?e.message:getErrorMessage(e)}function extractErrorId(e){if(isObject(e))return isNonEmptyString(e.errorId)?e.errorId:void 0}function formatErrorHint(e){let t=isObject(e.details)?e.details.name:void 0,n=isNonEmptyString(t)?t:void 0,r=typeof e.message==`string`?e.message.trim():``;return n&&r.length>0?` (${n}: ${truncateForDisplay(r)})`:n?` (${n})`:r.length>0?` (${truncateForDisplay(r)})`:``}function truncateForDisplay(e,t=160){return e.length<=t?e:`${e.slice(0,t-1).trimEnd()}…`}function recordErrorOnSpan(e,t){if(!capturesErrorContent()){e.setStatus({code:SpanStatusCode.ERROR});return}let n=truncateTelemetryText(t instanceof Error?t.message:getErrorMessage(t),4096),r=t instanceof Error?truncateTelemetryText(t.name,128):`Error`;e.setStatus({code:SpanStatusCode.ERROR,message:n}),e.recordException({message:n,name:r,stack:inspectError(t)})}function resolveThreshold(){let e=process.env.EVE_LOG_LEVEL?.toLowerCase();return e===`debug`||e===`info`||e===`warn`||e===`error`?LEVEL_SEVERITY[e]:LEVEL_SEVERITY.info}function isLogLevelEnabled(e){return LEVEL_SEVERITY[e]>=resolveThreshold()}let logRecordSubscriber;function setLogRecordSubscriber(e){logRecordSubscriber=e}function write(e,t,n,r){if(LEVEL_SEVERITY[e]<resolveThreshold())return;let i=logRecordSubscriber;if(i!==void 0)try{i(r===void 0?{level:e,namespace:t,message:n}:{level:e,namespace:t,message:n,fields:renderFields(r)});return}catch{}let a=e===`error`?console.error:e===`warn`?console.warn:console.log,o=`[eve:${t}] ${n}`;if(r===void 0){a(o);return}a(o,renderFields(r))}function renderFields(e){let t={};for(let[n,r]of Object.entries(e))if(r!==void 0){if(r instanceof Error){t[n]=formatError(r);continue}t[n]=r}return t}function recordOnActiveSpan(e,t){let n=trace.getActiveSpan();if(n===void 0)return;let r=t?.error;if(!capturesErrorContent()){(r instanceof Error||isFormattedError(r))&&n.setStatus({code:SpanStatusCode.ERROR});return}if(r instanceof Error){recordErrorOnSpan(n,r);return}if(isFormattedError(r)){let e=truncateTelemetryText(r.message,4096);n.setStatus({code:SpanStatusCode.ERROR,message:e}),n.recordException({message:e,name:typeof r.name==`string`?truncateTelemetryText(r.name,128):`Error`,stack:typeof r.detail==`string`?truncateTelemetryText(r.detail,8192):void 0});return}n.addEvent(e,t?renderFields(t):void 0)}function isFormattedError(e){return isObject(e)&&typeof e.errorId==`string`&&typeof e.message==`string`}function inspectError(e){return truncate(inspect(e,{breakLength:1/0,compact:!1,depth:10,maxStringLength:8192}),16384)}function truncate(e,t){if(Buffer.byteLength(e,`utf8`)<=t)return e;let n=e.slice(0,t);for(;Buffer.byteLength(n,`utf8`)>t&&n.length>0;)n=n.slice(0,-1);return`${n}<…truncated>`}export{createErrorId,createLogger,extractErrorId,formatError,formatErrorHint,inspectError,isLogLevelEnabled,logError,recordErrorOnSpan,setLogRecordSubscriber};