UNPKG

eve

Version:

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

1 lines 3.34 kB
import{inspect}from"node:util";import{isNonEmptyString,isObject}from"#shared/guards.js";import{SpanStatusCode,trace}from"#compiled/@opentelemetry/api/index.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 r=isObject(e.details)?e.details.name:void 0,i=isNonEmptyString(r)?r:void 0,a=typeof e.message==`string`?e.message.trim():``;return i&&a.length>0?` (${i}: ${truncateForDisplay(a)})`:i?` (${i})`:a.length>0?` (${truncateForDisplay(a)})`:``}function truncateForDisplay(e,t=160){return e.length<=t?e:`${e.slice(0,t-1).trimEnd()}…`}function recordErrorOnSpan(e,t){let n=t instanceof Error?t.message:getErrorMessage(t),i=t instanceof Error?t.name:`Error`;e.setStatus({code:SpanStatusCode.ERROR,message:n}),e.recordException({message:n,name:i,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()}function write(e,t,n,r){if(LEVEL_SEVERITY[e]<resolveThreshold())return;let i=e===`error`?console.error:e===`warn`?console.warn:console.log,a=`[eve:${t}] ${n}`;if(r===void 0){i(a);return}i(a,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 a=t?.error;if(a instanceof Error){recordErrorOnSpan(n,a);return}if(isFormattedError(a)){n.setStatus({code:SpanStatusCode.ERROR,message:a.message}),n.recordException({message:a.message,name:typeof a.name==`string`?a.name:`Error`,stack:typeof a.detail==`string`?a.detail: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(t){return truncate(inspect(t,{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,isLogLevelEnabled,logError,recordErrorOnSpan};