opik-vercel 1.10.21 → 1.10.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var opik=require('opik');var k=new opik.Opik,f=class{constructor({client:n=k,tags:t=[],metadata:e={},threadId:i}={}){this.traces=new Map;this.spans=new Map;this.getSpanInput=n=>{let t={},{attributes:e}=n;return Object.keys(e).forEach(a=>{if(a==="ai.prompt"||a==="gen_ai.request"){let o=C(e[a]);o&&(t={...t,...o});}if(a.startsWith("ai.prompt.")){let o=a.replace("ai.prompt.","");t[o]=m(e[a]);}if(a.startsWith("gen_ai.request.")){let o=a.replace("gen_ai.request.","");t[o]=m(e[a]);}}),Object.keys(t).length>0||("ai.toolCall.name"in e&&(t.toolName=e["ai.toolCall.name"]),"ai.toolCall.args"in e&&(t.args=e["ai.toolCall.args"])),t};this.getSpanOutput=n=>{let{attributes:t}=n;return t["ai.response.text"]?{text:t["ai.response.text"]}:t["ai.toolCall.result"]?{result:t["ai.toolCall.result"]}:t["ai.response.toolCalls"]?{toolCalls:m(t["ai.response.toolCalls"])}:{}};this.getSpanMetadata=n=>{let{attributes:t}=n,e={};return t["gen_ai.response.model"]&&(e.model=t["gen_ai.response.model"]),t["gen_ai.system"]&&(e.system=t["gen_ai.system"]),e};this.getSpanUsage=n=>{let{attributes:t}=n,e={};return "ai.usage.promptTokens"in t&&(e.prompt_tokens=t["ai.usage.promptTokens"]),"gen_ai.usage.input_tokens"in t&&(e.prompt_tokens=t["gen_ai.usage.input_tokens"]),"ai.usage.completionTokens"in t&&(e.completion_tokens=t["ai.usage.completionTokens"]),"gen_ai.usage.output_tokens"in t&&(e.completion_tokens=t["gen_ai.usage.output_tokens"]),("prompt_tokens"in e||"completion_tokens"in e)&&(e.total_tokens=(e.prompt_tokens||0)+(e.completion_tokens||0)),e};this.getThreadId=n=>{var e;let{attributes:t}=n;return t["ai.telemetry.metadata.threadId"]!=null?(e=t["ai.telemetry.metadata.threadId"])==null?void 0:e.toString():this.threadId};this.processSpan=({otelSpan:n,parentSpan:t,trace:e})=>e.span({name:n.name,startTime:new Date(l(n.startTime)),endTime:new Date(l(n.endTime)),parentSpanId:t==null?void 0:t.data.id,input:this.getSpanInput(n),output:this.getSpanOutput(n),metadata:this.getSpanMetadata(n),usage:this.getSpanUsage(n),type:"llm"});this.shutdown=async()=>{await this.client.flush();};this.forceFlush=async()=>{await this.client.flush();};this.export=async(n,t)=>{let e=n.filter(o=>w(o)==="ai"),i=n.length-e.length;if(i>0&&opik.logger.debug(`Ignored ${i} non-AI SDK spans`),e.length===0){opik.logger.debug("No AI SDK spans found"),t({code:0});return}let a=E(e);opik.logger.debug("Exporting spans",e),Object.entries(a).forEach(([o,d])=>{var h,S;let[r,...u]=d,p=this.client.trace({startTime:new Date(l(r.startTime)),endTime:new Date(l(r.endTime)),name:(S=(h=r.attributes["ai.telemetry.metadata.traceName"])==null?void 0:h.toString())!=null?S:r.name,input:this.getSpanInput(r),output:this.getSpanOutput(r),metadata:{...this.getSpanMetadata(r),...this.metadata},tags:this.tags,usage:this.getSpanUsage(r),threadId:this.getThreadId(r)});this.traces.set(o,p),u.forEach(g=>{var b,y;let I=this.spans.get((y=(b=g.parentSpanContext)==null?void 0:b.spanId)!=null?y:""),R=this.processSpan({parentSpan:I,otelSpan:g,trace:p});this.spans.set(g.spanContext().spanId,R);});});try{await this.client.flush(),t({code:0});}catch(o){opik.logger.error("Error exporting spans",o),t({code:1,error:o instanceof Error?o:new Error("Unknown error")});}};this.client=n,this.tags=[...t],this.metadata={...e},this.threadId=i;}static getSettings(n){var e,i,a;let t={...n.metadata};return n.name&&(t.traceName=n.name),{isEnabled:(e=n.isEnabled)!=null?e:true,recordInputs:(i=n.recordInputs)!=null?i:true,recordOutputs:(a=n.recordOutputs)!=null?a:true,functionId:n.functionId,metadata:t}}};function E(s){let n={};return s.forEach(t=>{let e=t.spanContext();n[e.traceId]||(n[e.traceId]=[]),n[e.traceId].push(t);}),Object.entries(n).forEach(([t,e])=>{n[t]=O(e);}),n}function w(s){var t,e,i,a;let n=s;return (a=(i=(t=n.instrumentationScope)==null?void 0:t.name)!=null?i:(e=n.instrumentationLibrary)==null?void 0:e.name)!=null?a:void 0}function l(s){return s[0]*1e3+s[1]/1e6}function m(s){try{return JSON.parse(s)}catch{return s}}function C(s){if(typeof s=="string")try{let n=JSON.parse(s);if(n!==null&&typeof n=="object"&&!Array.isArray(n))return n}catch{return}}function O(s){var o,d;let n=new Map,t=new Map;for(let r of s){let{spanId:u}=r.spanContext(),p=(d=(o=r.parentSpanContext)==null?void 0:o.spanId)!=null?d:"";n.set(u,r),p&&(t.has(p)||t.set(p,[]),t.get(p).push(r));}let e=s.filter(r=>{var u;return !((u=r.parentSpanContext)!=null&&u.spanId)||!n.has(r.parentSpanContext.spanId)}),i=[],a=[...e];for(;a.length>0;){let r=a.shift();i.push(r);let u=r.spanContext().spanId,p=t.get(u)||[];a.push(...p);}return i}exports.OpikExporter=f;
1
+ 'use strict';var opik=require('opik');var k=new opik.Opik,f=class{constructor({client:n=k,tags:t=[],metadata:e={},threadId:i}={}){this.traces=new Map;this.spans=new Map;this.getSpanInput=n=>{let t={},{attributes:e}=n;return Object.keys(e).forEach(a=>{if(a==="ai.prompt"||a==="gen_ai.request"){let o=C(e[a]);o&&(t={...t,...o});}if(a.startsWith("ai.prompt.")){let o=a.replace("ai.prompt.","");t[o]=g(e[a]);}if(a.startsWith("gen_ai.request.")){let o=a.replace("gen_ai.request.","");t[o]=g(e[a]);}}),Object.keys(t).length>0||("ai.toolCall.name"in e&&(t.toolName=e["ai.toolCall.name"]),"ai.toolCall.args"in e&&(t.args=e["ai.toolCall.args"])),t};this.getSpanOutput=n=>{let{attributes:t}=n;return t["ai.response.text"]?{text:t["ai.response.text"]}:t["ai.response.object"]?{object:g(t["ai.response.object"])}:t["ai.toolCall.result"]?{result:t["ai.toolCall.result"]}:t["ai.response.toolCalls"]?{toolCalls:g(t["ai.response.toolCalls"])}:{}};this.getSpanMetadata=n=>{let{attributes:t}=n,e={};return t["gen_ai.response.model"]&&(e.model=t["gen_ai.response.model"]),t["gen_ai.system"]&&(e.system=t["gen_ai.system"]),e};this.getSpanUsage=n=>{let{attributes:t}=n,e={};return "ai.usage.promptTokens"in t&&(e.prompt_tokens=t["ai.usage.promptTokens"]),"gen_ai.usage.input_tokens"in t&&(e.prompt_tokens=t["gen_ai.usage.input_tokens"]),"ai.usage.completionTokens"in t&&(e.completion_tokens=t["ai.usage.completionTokens"]),"gen_ai.usage.output_tokens"in t&&(e.completion_tokens=t["gen_ai.usage.output_tokens"]),("prompt_tokens"in e||"completion_tokens"in e)&&(e.total_tokens=(e.prompt_tokens||0)+(e.completion_tokens||0)),e};this.getThreadId=n=>{var e;let{attributes:t}=n;return t["ai.telemetry.metadata.threadId"]!=null?(e=t["ai.telemetry.metadata.threadId"])==null?void 0:e.toString():this.threadId};this.processSpan=({otelSpan:n,parentSpan:t,trace:e})=>e.span({name:n.name,startTime:new Date(l(n.startTime)),endTime:new Date(l(n.endTime)),parentSpanId:t==null?void 0:t.data.id,input:this.getSpanInput(n),output:this.getSpanOutput(n),metadata:this.getSpanMetadata(n),usage:this.getSpanUsage(n),type:"llm"});this.shutdown=async()=>{await this.client.flush();};this.forceFlush=async()=>{await this.client.flush();};this.export=async(n,t)=>{let e=n.filter(o=>w(o)==="ai"),i=n.length-e.length;if(i>0&&opik.logger.debug(`Ignored ${i} non-AI SDK spans`),e.length===0){opik.logger.debug("No AI SDK spans found"),t({code:0});return}let a=E(e);opik.logger.debug("Exporting spans",e),Object.entries(a).forEach(([o,d])=>{var h,S;let[r,...u]=d,p=this.client.trace({startTime:new Date(l(r.startTime)),endTime:new Date(l(r.endTime)),name:(S=(h=r.attributes["ai.telemetry.metadata.traceName"])==null?void 0:h.toString())!=null?S:r.name,input:this.getSpanInput(r),output:this.getSpanOutput(r),metadata:{...this.getSpanMetadata(r),...this.metadata},tags:this.tags,usage:this.getSpanUsage(r),threadId:this.getThreadId(r)});this.traces.set(o,p),u.forEach(m=>{var b,y;let I=this.spans.get((y=(b=m.parentSpanContext)==null?void 0:b.spanId)!=null?y:""),R=this.processSpan({parentSpan:I,otelSpan:m,trace:p});this.spans.set(m.spanContext().spanId,R);});});try{await this.client.flush(),t({code:0});}catch(o){opik.logger.error("Error exporting spans",o),t({code:1,error:o instanceof Error?o:new Error("Unknown error")});}};this.client=n,this.tags=[...t],this.metadata={...e},this.threadId=i;}static getSettings(n){var e,i,a;let t={...n.metadata};return n.name&&(t.traceName=n.name),{isEnabled:(e=n.isEnabled)!=null?e:true,recordInputs:(i=n.recordInputs)!=null?i:true,recordOutputs:(a=n.recordOutputs)!=null?a:true,functionId:n.functionId,metadata:t}}};function E(s){let n={};return s.forEach(t=>{let e=t.spanContext();n[e.traceId]||(n[e.traceId]=[]),n[e.traceId].push(t);}),Object.entries(n).forEach(([t,e])=>{n[t]=O(e);}),n}function w(s){var t,e,i,a;let n=s;return (a=(i=(t=n.instrumentationScope)==null?void 0:t.name)!=null?i:(e=n.instrumentationLibrary)==null?void 0:e.name)!=null?a:void 0}function l(s){return s[0]*1e3+s[1]/1e6}function g(s){try{return JSON.parse(s)}catch{return s}}function C(s){if(typeof s=="string")try{let n=JSON.parse(s);if(n!==null&&typeof n=="object"&&!Array.isArray(n))return n}catch{return}}function O(s){var o,d;let n=new Map,t=new Map;for(let r of s){let{spanId:u}=r.spanContext(),p=(d=(o=r.parentSpanContext)==null?void 0:o.spanId)!=null?d:"";n.set(u,r),p&&(t.has(p)||t.set(p,[]),t.get(p).push(r));}let e=s.filter(r=>{var u;return !((u=r.parentSpanContext)!=null&&u.spanId)||!n.has(r.parentSpanContext.spanId)}),i=[],a=[...e];for(;a.length>0;){let r=a.shift();i.push(r);let u=r.spanContext().spanId,p=t.get(u)||[];a.push(...p);}return i}exports.OpikExporter=f;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {Opik,logger}from'opik';var k=new Opik,f=class{constructor({client:n=k,tags:t=[],metadata:e={},threadId:i}={}){this.traces=new Map;this.spans=new Map;this.getSpanInput=n=>{let t={},{attributes:e}=n;return Object.keys(e).forEach(a=>{if(a==="ai.prompt"||a==="gen_ai.request"){let o=C(e[a]);o&&(t={...t,...o});}if(a.startsWith("ai.prompt.")){let o=a.replace("ai.prompt.","");t[o]=m(e[a]);}if(a.startsWith("gen_ai.request.")){let o=a.replace("gen_ai.request.","");t[o]=m(e[a]);}}),Object.keys(t).length>0||("ai.toolCall.name"in e&&(t.toolName=e["ai.toolCall.name"]),"ai.toolCall.args"in e&&(t.args=e["ai.toolCall.args"])),t};this.getSpanOutput=n=>{let{attributes:t}=n;return t["ai.response.text"]?{text:t["ai.response.text"]}:t["ai.toolCall.result"]?{result:t["ai.toolCall.result"]}:t["ai.response.toolCalls"]?{toolCalls:m(t["ai.response.toolCalls"])}:{}};this.getSpanMetadata=n=>{let{attributes:t}=n,e={};return t["gen_ai.response.model"]&&(e.model=t["gen_ai.response.model"]),t["gen_ai.system"]&&(e.system=t["gen_ai.system"]),e};this.getSpanUsage=n=>{let{attributes:t}=n,e={};return "ai.usage.promptTokens"in t&&(e.prompt_tokens=t["ai.usage.promptTokens"]),"gen_ai.usage.input_tokens"in t&&(e.prompt_tokens=t["gen_ai.usage.input_tokens"]),"ai.usage.completionTokens"in t&&(e.completion_tokens=t["ai.usage.completionTokens"]),"gen_ai.usage.output_tokens"in t&&(e.completion_tokens=t["gen_ai.usage.output_tokens"]),("prompt_tokens"in e||"completion_tokens"in e)&&(e.total_tokens=(e.prompt_tokens||0)+(e.completion_tokens||0)),e};this.getThreadId=n=>{var e;let{attributes:t}=n;return t["ai.telemetry.metadata.threadId"]!=null?(e=t["ai.telemetry.metadata.threadId"])==null?void 0:e.toString():this.threadId};this.processSpan=({otelSpan:n,parentSpan:t,trace:e})=>e.span({name:n.name,startTime:new Date(l(n.startTime)),endTime:new Date(l(n.endTime)),parentSpanId:t==null?void 0:t.data.id,input:this.getSpanInput(n),output:this.getSpanOutput(n),metadata:this.getSpanMetadata(n),usage:this.getSpanUsage(n),type:"llm"});this.shutdown=async()=>{await this.client.flush();};this.forceFlush=async()=>{await this.client.flush();};this.export=async(n,t)=>{let e=n.filter(o=>w(o)==="ai"),i=n.length-e.length;if(i>0&&logger.debug(`Ignored ${i} non-AI SDK spans`),e.length===0){logger.debug("No AI SDK spans found"),t({code:0});return}let a=E(e);logger.debug("Exporting spans",e),Object.entries(a).forEach(([o,d])=>{var h,S;let[r,...u]=d,p=this.client.trace({startTime:new Date(l(r.startTime)),endTime:new Date(l(r.endTime)),name:(S=(h=r.attributes["ai.telemetry.metadata.traceName"])==null?void 0:h.toString())!=null?S:r.name,input:this.getSpanInput(r),output:this.getSpanOutput(r),metadata:{...this.getSpanMetadata(r),...this.metadata},tags:this.tags,usage:this.getSpanUsage(r),threadId:this.getThreadId(r)});this.traces.set(o,p),u.forEach(g=>{var b,y;let I=this.spans.get((y=(b=g.parentSpanContext)==null?void 0:b.spanId)!=null?y:""),R=this.processSpan({parentSpan:I,otelSpan:g,trace:p});this.spans.set(g.spanContext().spanId,R);});});try{await this.client.flush(),t({code:0});}catch(o){logger.error("Error exporting spans",o),t({code:1,error:o instanceof Error?o:new Error("Unknown error")});}};this.client=n,this.tags=[...t],this.metadata={...e},this.threadId=i;}static getSettings(n){var e,i,a;let t={...n.metadata};return n.name&&(t.traceName=n.name),{isEnabled:(e=n.isEnabled)!=null?e:true,recordInputs:(i=n.recordInputs)!=null?i:true,recordOutputs:(a=n.recordOutputs)!=null?a:true,functionId:n.functionId,metadata:t}}};function E(s){let n={};return s.forEach(t=>{let e=t.spanContext();n[e.traceId]||(n[e.traceId]=[]),n[e.traceId].push(t);}),Object.entries(n).forEach(([t,e])=>{n[t]=O(e);}),n}function w(s){var t,e,i,a;let n=s;return (a=(i=(t=n.instrumentationScope)==null?void 0:t.name)!=null?i:(e=n.instrumentationLibrary)==null?void 0:e.name)!=null?a:void 0}function l(s){return s[0]*1e3+s[1]/1e6}function m(s){try{return JSON.parse(s)}catch{return s}}function C(s){if(typeof s=="string")try{let n=JSON.parse(s);if(n!==null&&typeof n=="object"&&!Array.isArray(n))return n}catch{return}}function O(s){var o,d;let n=new Map,t=new Map;for(let r of s){let{spanId:u}=r.spanContext(),p=(d=(o=r.parentSpanContext)==null?void 0:o.spanId)!=null?d:"";n.set(u,r),p&&(t.has(p)||t.set(p,[]),t.get(p).push(r));}let e=s.filter(r=>{var u;return !((u=r.parentSpanContext)!=null&&u.spanId)||!n.has(r.parentSpanContext.spanId)}),i=[],a=[...e];for(;a.length>0;){let r=a.shift();i.push(r);let u=r.spanContext().spanId,p=t.get(u)||[];a.push(...p);}return i}export{f as OpikExporter};
1
+ import {Opik,logger}from'opik';var k=new Opik,f=class{constructor({client:n=k,tags:t=[],metadata:e={},threadId:i}={}){this.traces=new Map;this.spans=new Map;this.getSpanInput=n=>{let t={},{attributes:e}=n;return Object.keys(e).forEach(a=>{if(a==="ai.prompt"||a==="gen_ai.request"){let o=C(e[a]);o&&(t={...t,...o});}if(a.startsWith("ai.prompt.")){let o=a.replace("ai.prompt.","");t[o]=g(e[a]);}if(a.startsWith("gen_ai.request.")){let o=a.replace("gen_ai.request.","");t[o]=g(e[a]);}}),Object.keys(t).length>0||("ai.toolCall.name"in e&&(t.toolName=e["ai.toolCall.name"]),"ai.toolCall.args"in e&&(t.args=e["ai.toolCall.args"])),t};this.getSpanOutput=n=>{let{attributes:t}=n;return t["ai.response.text"]?{text:t["ai.response.text"]}:t["ai.response.object"]?{object:g(t["ai.response.object"])}:t["ai.toolCall.result"]?{result:t["ai.toolCall.result"]}:t["ai.response.toolCalls"]?{toolCalls:g(t["ai.response.toolCalls"])}:{}};this.getSpanMetadata=n=>{let{attributes:t}=n,e={};return t["gen_ai.response.model"]&&(e.model=t["gen_ai.response.model"]),t["gen_ai.system"]&&(e.system=t["gen_ai.system"]),e};this.getSpanUsage=n=>{let{attributes:t}=n,e={};return "ai.usage.promptTokens"in t&&(e.prompt_tokens=t["ai.usage.promptTokens"]),"gen_ai.usage.input_tokens"in t&&(e.prompt_tokens=t["gen_ai.usage.input_tokens"]),"ai.usage.completionTokens"in t&&(e.completion_tokens=t["ai.usage.completionTokens"]),"gen_ai.usage.output_tokens"in t&&(e.completion_tokens=t["gen_ai.usage.output_tokens"]),("prompt_tokens"in e||"completion_tokens"in e)&&(e.total_tokens=(e.prompt_tokens||0)+(e.completion_tokens||0)),e};this.getThreadId=n=>{var e;let{attributes:t}=n;return t["ai.telemetry.metadata.threadId"]!=null?(e=t["ai.telemetry.metadata.threadId"])==null?void 0:e.toString():this.threadId};this.processSpan=({otelSpan:n,parentSpan:t,trace:e})=>e.span({name:n.name,startTime:new Date(l(n.startTime)),endTime:new Date(l(n.endTime)),parentSpanId:t==null?void 0:t.data.id,input:this.getSpanInput(n),output:this.getSpanOutput(n),metadata:this.getSpanMetadata(n),usage:this.getSpanUsage(n),type:"llm"});this.shutdown=async()=>{await this.client.flush();};this.forceFlush=async()=>{await this.client.flush();};this.export=async(n,t)=>{let e=n.filter(o=>w(o)==="ai"),i=n.length-e.length;if(i>0&&logger.debug(`Ignored ${i} non-AI SDK spans`),e.length===0){logger.debug("No AI SDK spans found"),t({code:0});return}let a=E(e);logger.debug("Exporting spans",e),Object.entries(a).forEach(([o,d])=>{var h,S;let[r,...u]=d,p=this.client.trace({startTime:new Date(l(r.startTime)),endTime:new Date(l(r.endTime)),name:(S=(h=r.attributes["ai.telemetry.metadata.traceName"])==null?void 0:h.toString())!=null?S:r.name,input:this.getSpanInput(r),output:this.getSpanOutput(r),metadata:{...this.getSpanMetadata(r),...this.metadata},tags:this.tags,usage:this.getSpanUsage(r),threadId:this.getThreadId(r)});this.traces.set(o,p),u.forEach(m=>{var b,y;let I=this.spans.get((y=(b=m.parentSpanContext)==null?void 0:b.spanId)!=null?y:""),R=this.processSpan({parentSpan:I,otelSpan:m,trace:p});this.spans.set(m.spanContext().spanId,R);});});try{await this.client.flush(),t({code:0});}catch(o){logger.error("Error exporting spans",o),t({code:1,error:o instanceof Error?o:new Error("Unknown error")});}};this.client=n,this.tags=[...t],this.metadata={...e},this.threadId=i;}static getSettings(n){var e,i,a;let t={...n.metadata};return n.name&&(t.traceName=n.name),{isEnabled:(e=n.isEnabled)!=null?e:true,recordInputs:(i=n.recordInputs)!=null?i:true,recordOutputs:(a=n.recordOutputs)!=null?a:true,functionId:n.functionId,metadata:t}}};function E(s){let n={};return s.forEach(t=>{let e=t.spanContext();n[e.traceId]||(n[e.traceId]=[]),n[e.traceId].push(t);}),Object.entries(n).forEach(([t,e])=>{n[t]=O(e);}),n}function w(s){var t,e,i,a;let n=s;return (a=(i=(t=n.instrumentationScope)==null?void 0:t.name)!=null?i:(e=n.instrumentationLibrary)==null?void 0:e.name)!=null?a:void 0}function l(s){return s[0]*1e3+s[1]/1e6}function g(s){try{return JSON.parse(s)}catch{return s}}function C(s){if(typeof s=="string")try{let n=JSON.parse(s);if(n!==null&&typeof n=="object"&&!Array.isArray(n))return n}catch{return}}function O(s){var o,d;let n=new Map,t=new Map;for(let r of s){let{spanId:u}=r.spanContext(),p=(d=(o=r.parentSpanContext)==null?void 0:o.spanId)!=null?d:"";n.set(u,r),p&&(t.has(p)||t.set(p,[]),t.get(p).push(r));}let e=s.filter(r=>{var u;return !((u=r.parentSpanContext)!=null&&u.spanId)||!n.has(r.parentSpanContext.spanId)}),i=[],a=[...e];for(;a.length>0;){let r=a.shift();i.push(r);let u=r.spanContext().spanId,p=t.get(u)||[];a.push(...p);}return i}export{f as OpikExporter};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "opik-vercel",
3
3
  "description": "Opik TypeScript and JavaScript SDK integration with Vercel AI SDK",
4
- "version": "1.10.21",
4
+ "version": "1.10.23",
5
5
  "engines": {
6
6
  "node": ">=18"
7
7
  },