// @bun
var V=Object.defineProperty;var w=(z)=>z;function N(z,f){this[z]=w.bind(null,f)}var k=(z,f)=>{for(var j in f)V(z,j,{get:f[j],enumerable:!0,configurable:!0,set:N.bind(f,j)})};var b=(z,f)=>()=>(z&&(f=z(z=0)),f);var q=import.meta.require;function S(z){let f=z.match(/^(.+)\.v(\d+)$/);if(f)return{name:f[1],version:f[2]};return{name:z,version:"1.0.0"}}function P(z){return z&&typeof z==="object"?z:void 0}function h(z){let f=P(z.response);if(!f)return;return{id:f.id,modelId:f.modelId,timestamp:f.timestamp,headers:f.headers,body:f.body,messages:f.messages}}function _(z,f){let j=z?.body;if(!j)return;if(typeof j==="string")try{let D=JSON.parse(j);return typeof D[f]==="string"?D[f]:void 0}catch{return}let J=P(j);return typeof J?.[f]==="string"?J[f]:void 0}async function C(z,f,j,J,D){let{name:Q,version:U}=S(f),G=h(j),A=j.providerMetadata,B=j.warnings,W=j.rawFinishReason,K=P(j.request),X=D?.traceId??_(K,"$ai_trace_id"),Y=D?.sessionId??_(K,"$ai_session_id"),E=G?.id;for(let H of j.toolCalls??[]){let L=j.toolResults?.find((O)=>O.toolCallId===H.toolCallId),Z=P(L?.output)?.error,T={operation:{name:`${Q}.${H.toolName}`,version:U},durationMs:J??0,success:j.toolResults?.some((O)=>O.toolCallId===H.toolCallId&&O.output!==void 0)??!1,timestamp:new Date,metadata:{telemetryEvent:"span",traceId:X,sessionId:Y,spanId:H.toolCallId,parentSpanId:E,spanName:`tool.${H.toolName}`,agentId:f,actorId:D?.actorId,tenantId:D?.tenantId,workflowId:D?.workflowId,stepIndex:D?.stepIndex,toolName:H.toolName,toolCallArgs:H.input,toolResultOutput:L?.output,errorMessage:typeof Z==="string"?Z:typeof L?.output==="string"?L.output:void 0,finishReason:j.finishReason,rawFinishReason:W}};await z.collect(T)}let F={operation:{name:Q,version:U},durationMs:J??0,success:j.finishReason!=="error",timestamp:new Date,metadata:{telemetryEvent:"generation",traceId:X,sessionId:Y,spanId:G?.id,spanName:`agent.${Q}.step`,agentId:f,actorId:D?.actorId,tenantId:D?.tenantId,workflowId:D?.workflowId,stepIndex:D?.stepIndex,stepStartedAt:D?.stepStartedAt,finishReason:j.finishReason,rawFinishReason:W,tokenUsage:j.usage,totalUsage:j.totalUsage,providerMetadata:A,warnings:B,stepText:j.text,stepReasoningText:j.reasoningText,responseId:G?.id,responseModelId:G?.modelId,responseTimestamp:G?.timestamp,responseHeaders:G?.headers,responseBody:G?.body,responseMessages:G?.messages,requestBody:K?.body,requestHeaders:K?.headers,toolCallCount:j.toolCalls?.length??0,toolCalls:j.toolCalls,toolResults:j.toolResults,errorMessage:j.finishReason==="error"?j.text:void 0}};await z.collect(F)}class ${samples=[];async collect(z){this.samples.push(z)}getSamples(){return[...this.samples]}getSamplesForOperation(z){return this.samples.filter((f)=>f.operation.name===z)}clear(){this.samples.length=0}}function R(){return new $}var i;var M=b(()=>{i={collect:async()=>{}}});M();export{C as trackAgentStep,i as noopTelemetryCollector,R as createInMemoryTelemetryCollector,$ as InMemoryTelemetryCollector};
