UNPKG

eve

Version:

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

1 lines • 4.72 kB
import{attemptIdempotencyKey,modelCallIdempotencyKey,toolCallIdempotencyKey}from"#instrumentation/lifecycle.js";import{structuralProviderMetadata}from"#instrumentation/content.js";function createAiSdkHookBridge(e,t,n=directRunInContext,r){let i={capturesInputs:t.capturesInputs??t.capturesContent,capturesOutputs:t.capturesOutputs??t.capturesContent,modelKeys:new Map,runtimeContext:r!==void 0&&Object.keys(r).length>0?Object.freeze({...r}):void 0,scope:e,toolKeys:new Map};return{onStart(e){i.operation=Object.freeze({modelId:e.modelId,operationId:e.operationId,provider:e.provider})},async onStepStart(e){i.stepNumber=e.stepNumber;let n=toStepAttemptStarted(i);n!==void 0&&await t.publish(n)},async onLanguageModelCallStart(e){let n=modelCallIdempotencyKey(i.scope,i.stepNumber??0);i.modelKeys.set(e.callId,n);let r=toModelCallStarted(i,n,e);await t.publish(r)},executeLanguageModelCall({callId:t,execute:r}){let a=i.modelKeys.get(t);return a===void 0?r():n({idempotencyKey:a,scope:e,type:`model.call`},r)},async onLanguageModelCallEnd(e){let n=i.modelKeys.get(e.callId);if(n===void 0)return;i.modelKeys.delete(e.callId);let r=toModelCallCompleted(i,n,e);await t.publish(r)},async onStepEnd(e){if(e.providerMetadata===void 0)return;let n=i.capturesOutputs?e.providerMetadata:structuralProviderMetadata(e.providerMetadata);await t.publish(Object.freeze({idempotencyKey:attemptIdempotencyKey(i.scope),providerMetadata:n,scope:i.scope,type:`step.attempt.metadata`}))},async onToolExecutionStart(e){let n=toolCallIdempotencyKey(i.scope,e.toolCall.toolCallId,i.stepNumber??0);i.toolKeys.set(e.toolCall.toolCallId,n);let r=toToolCallStarted(i,n,e);await t.publish(r)},executeTool({toolCallId:t,execute:r}){let a=i.toolKeys.get(t);return a===void 0?r():n({idempotencyKey:a,scope:e,type:`tool.call`},r)},async onToolExecutionEnd(e){let n=e.toolCall.toolCallId,r=i.toolKeys.get(n);if(r===void 0)return;i.toolKeys.delete(n);let a=toToolCallCompleted(i,r,e);await t.publish(a)},async onAbort(e){await failOpenOperations(e.reason)},async onError(e){await failOpenOperations(e.error)}};async function failOpenOperations(n){let r=[];for(let a of i.modelKeys.values())r.push(t.publish(Object.freeze({error:n,idempotencyKey:a,scope:e,type:`model.call.failed`})));for(let a of i.toolKeys.values())r.push(t.publish(Object.freeze({error:n,idempotencyKey:a,scope:e,type:`tool.call.failed`})));i.modelKeys.clear(),i.toolKeys.clear(),await Promise.all(r)}}const directRunInContext=(e,t)=>t();function toStepAttemptStarted(e){if(e.operation!==void 0&&e.stepNumber!==void 0)return Object.freeze({idempotencyKey:attemptIdempotencyKey(e.scope),operation:e.operation,runtimeContext:e.runtimeContext,scope:e.scope,type:`step.attempt.started`})}function toModelCallStarted(e,t,n){return Object.freeze({idempotencyKey:t,input:e.capturesInputs?Object.freeze({instructions:n.instructions,messages:Object.freeze([...n.messages])}):void 0,model:Object.freeze({modelId:n.modelId,provider:n.provider}),runtimeContext:e.runtimeContext,scope:e.scope,type:`model.call.started`})}function toModelCallCompleted(e,t,n){return Object.freeze({content:e.capturesOutputs?toContentParts(n.content):void 0,finishReason:n.finishReason,idempotencyKey:t,responseModelId:n.modelId,responseId:n.responseId,scope:e.scope,type:`model.call.completed`,usage:toUsage(n.usage)})}function toUsage(e){return Object.freeze({inputTokenDetails:Object.freeze({cacheReadTokens:e.inputTokenDetails?.cacheReadTokens,cacheWriteTokens:e.inputTokenDetails?.cacheWriteTokens}),inputTokens:e.inputTokens,outputTokens:e.outputTokens})}function toContentParts(e){let t=[];for(let n of e)switch(n.type){case`text`:case`reasoning`:t.push(Object.freeze({text:n.text,type:n.type}));break;case`tool-call`:t.push(Object.freeze({callId:n.toolCallId,input:n.input,toolName:n.toolName,type:`tool-call`}));break;case`tool-result`:t.push(Object.freeze({callId:n.toolCallId,input:n.input,output:n.output,toolName:n.toolName,type:`tool-result`}));break;case`tool-error`:t.push(Object.freeze({callId:n.toolCallId,error:n.error,input:n.input,toolName:n.toolName,type:`tool-error`}))}return Object.freeze(t)}function toToolCallStarted(e,t,n){return Object.freeze({callId:n.toolCall.toolCallId,idempotencyKey:t,input:e.capturesInputs?n.toolCall.input:void 0,scope:e.scope,toolName:n.toolCall.toolName,type:`tool.call.started`})}function toToolCallCompleted(e,t,n){return Object.freeze({idempotencyKey:t,output:toToolOutput(n.toolOutput,e.capturesOutputs),scope:e.scope,type:`tool.call.completed`})}function toToolOutput(e,t){return e.type===`tool-result`?Object.freeze(t?{output:e.output,type:`result`}:{type:`result`}):Object.freeze(t?{error:e.error,type:`error`}:{type:`error`})}export{createAiSdkHookBridge};