opik 2.2.96 → 2.2.98

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.
@@ -19,7 +19,7 @@ ${s}`),s}isChatPrompt(o){try{let e=JSON.parse(o);return Array.isArray(e)&&e.leng
19
19
  `),(l[u].length>0||u<l.length-1)&&(m+=kne()+l[u]);return p.call(o==="stdout"?process.stdout:process.stderr,m,i,a)}return p.call(o==="stdout"?process.stdout:process.stderr,t,i,a)}}function E7(){var p;JU||(j7=typeof((p=process.stdout)==null?void 0:p.isTTY)=="boolean"&&process.stdout.isTTY,JU=process.stdout.write.bind(process.stdout),S7=process.stderr.write.bind(process.stderr),process.stdout.write=T7(JU,"stdout"),process.stderr.write=T7(S7,"stderr"));}function C7(p){var e;let o=(e=Au.get(p))!=null?e:[];return Au.delete(p),o}var LU,VU,z7,JU,S7,v7,j7,Au,GU=n(()=>{yu();LU=[245,166,35],VU=[224,62,45],z7=20,JU=null,S7=null,v7=0,j7=false,Au=new Map;});function zne(p,o){if(p==null)return p;switch(o){case "boolean":return typeof p=="boolean"?p:$o(String(p),"boolean");case "float":case "integer":{if(typeof p=="number")return p;let e=$o(String(p),"float");if(typeof e=="number"&&Number.isNaN(e))throw new TypeError(`Cannot cast "${p}" to number`);return o==="integer"?Math.trunc(e):e}default:return typeof p=="string"?p:Array.isArray(p)||typeof p=="object"&&p!==null?JSON.stringify(p):String(p)}}var Ane,Pne,I7,Pu,dp,D7=n(()=>{Tp();mg();xu();jU();mp();WU();re();ar();bu();yu();GU();Ane=500,Pne=3e5,I7=1e4,Pu=class{constructor(o,e,t){this.shutdownRequested=false;this.heartbeatTimer=null;this.pollTimer=null;this.cancelledJobs=new Map;this.activeTasks=new Set;this.pollFailures=0;var i,a;this.api=o,this.runnerId=e,this.heartbeatIntervalMs=(i=t==null?void 0:t.heartbeatIntervalMs)!=null?i:5e3,this.backoffCapMs=(a=t==null?void 0:t.backoffCapMs)!=null?a:3e4;}start(){this.startHeartbeat(),this.startPolling();}shutdown(){this.shutdownRequested=true,this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null),this.pollTimer&&(clearTimeout(this.pollTimer),this.pollTimer=null);}startHeartbeat(){let o=setInterval(()=>{this.heartbeatTick().catch(e=>{E.debug("Heartbeat tick error",{error:e});});},this.heartbeatIntervalMs);o.unref(),this.heartbeatTimer=o;}async heartbeatTick(){var o;if(!this.shutdownRequested)try{let t=(o=(await this.api.runners.heartbeat(this.runnerId)).cancelledJobIds)!=null?o:[],i=Date.now();for(let a of t)this.cancelledJobs.set(a,i);this.pruneCancelledJobs(i);}catch(e){if(e instanceof ct){E.info("Runner deregistered (410), shutting down"),this.shutdown();return}E.debug("Heartbeat error",{error:e});}}startPolling(){this.pollTick(1e3);}pollTick(o){if(this.shutdownRequested)return;(async()=>{if(this.shutdownRequested)return;let t=o;try{let i=await this.api.runners.nextJob(this.runnerId);if(this.pollFailures=0,t=1e3,i===null){this.scheduleNextPoll(Ane,t);return}this.spawnJob(i);}catch(i){if(this.pollFailures++,this.pollFailures===1){let s=i instanceof k?i.statusCode:void 0;E.warn("Unable to reach Opik server"+(s?` (API ${s})`:"")+". Retrying...",{error:i});}else E.debug("Poll error",{error:i});let a=this.jitteredBackoff(t);t=Math.min(t*2,this.backoffCapMs),this.scheduleNextPoll(a,t);return}this.scheduleNextPoll(0,t);})().catch(t=>{this.pollFailures++,this.pollFailures===1?E.warn("Unable to reach Opik server. Retrying...",{error:t}):E.debug("Poll tick error",{error:t}),this.scheduleNextPoll(this.jitteredBackoff(o),Math.min(o*2,this.backoffCapMs));});}scheduleNextPoll(o,e){if(this.shutdownRequested)return;let t=setTimeout(()=>this.pollTick(e),o);t.unref(),this.pollTimer=t;}spawnJob(o){let e=this.executeJob(o).finally(()=>{this.activeTasks.delete(e);});this.activeTasks.add(e);}async executeJob(o){var s,c;let e=(s=o.id)!=null?s:"",t=(c=o.agentName)!=null?c:"";if(this.cancelledJobs.has(e)){E.debug(`Skipping cancelled job ${e}`),this.cancelledJobs.delete(e);return}if(!Yo().get(t)){E.error(`Unknown agent '${t}' for job ${e}`),await this.reportJobResult(e,{status:"failed",error:`Unknown agent: ${t}`,traceId:o.traceId});return}let a=ue();await this.reportJobResult(e,{status:"running",traceId:a});try{let l=await this.invokeAgent(o,e,a);await wu().catch(m=>{E.debug("Flush error after job execution",{error:m});}),await this.sendJobLogs(e),await this.reportJobResult(e,{status:"completed",result:this.normalizeResult(l),traceId:a});}catch(l){await wu().catch(()=>{}),await this.sendJobLogs(e);let m=o.timeout,u=l instanceof dp?`Job timed out after ${m}s`:l instanceof Error?`${l.name}: ${l.message}`:String(l);l instanceof dp?E.warn(`Job ${e} timed out after ${m}s`):E.error(`Job ${e} failed: ${u}`),await this.reportJobResult(e,{status:"failed",error:u,traceId:a});}}async invokeAgent(o,e,t){var h,y;let i=(h=o.agentName)!=null?h:"",a=(y=o.inputs)!=null?y:{},s=o.maskId,c=o.blueprintName,l=o.promptMasks,m=Yo().get(i),u=m.params.map(w=>{let A=a[w.name];if(!(w.presence===oo.Optional&&A===void 0&&!(w.name in a)))return zne(A,w.type)}),d=()=>R7({traceId:t,jobId:e},()=>OZ(l,()=>s||c?Ru({blueprintName:c,maskId:s},()=>m.func(...u)):m.func(...u))),f=Promise.resolve(d()),g=o.timeout;return g&&g>0?Promise.race([f,new Promise((w,A)=>{setTimeout(()=>A(new dp("Job timed out")),g*1e3).unref();})]):f}normalizeResult(o){return o!=null&&typeof o!="string"&&typeof o!="number"&&typeof o!="boolean"&&!Array.isArray(o)&&typeof o!="object"&&(o=String(o)),typeof o=="object"&&o!==null&&!Array.isArray(o)?o:{result:o}}async reportJobResult(o,e){try{await this.api.runners.reportJobResult(o,e);}catch(t){E.warn(`Failed to report result for job ${o}`,{error:t});}}async sendJobLogs(o){let e=C7(o);if(e.length!==0)try{await this.api.runners.appendJobLogs(o,{body:e});}catch{E.debug(`Failed to send logs for job ${o}`);}}pruneCancelledJobs(o){let e=o-Pne;for(let[t,i]of this.cancelledJobs)i<=e&&this.cancelledJobs.delete(t);if(this.cancelledJobs.size>I7){let t=[...this.cancelledJobs.entries()].sort((a,s)=>a[1]-s[1]),i=t.length-I7;for(let a=0;a<i;a++)this.cancelledJobs.delete(t[a][0]);}}jitteredBackoff(o){return Math.min(o,this.backoffCapMs)*(.5+Math.random()*.5)}};dp=class extends Error{constructor(o){super(o),this.name="TimeoutError";}};});function OU(){process.env.OPIK_RUNNER_MODE==="true"&&(U7||(U7=true,Sne().catch(p=>{E.error("Runner activation failed",{error:p});})));}async function Sne(){var m,u;let p=(m=process.env.OPIK_RUNNER_ID)!=null?m:"",o=(u=process.env.OPIK_PROJECT_NAME)!=null?u:"";if(!p){E.error("OPIK_RUNNER_ID not set, cannot activate runner");return}vne(p,o),E7();let e=new Rr,t=e.api;function i(d){return {description:d.docstring,language:"typescript",params:d.params.map(f=>({name:f.name,type:f.type,presence:f.presence})),timeout:0}}function a(d){let f=Yo(),g={};for(let[h,y]of f)g[h]=i(y);t.runners.registerAgents(p,{body:g}).catch(h=>{E.debug("Failed to sync agents after new registration",{error:h});});}await new Promise(d=>setImmediate(d));let s=Yo();if(s.size>0){let d={};for(let[f,g]of s)d[f]=i(g);try{await t.runners.registerAgents(p,{body:d});}catch{E.debug("Failed to register agents on startup");}}k7(a),E.info("Runner activated");let c=new Pu(t,p);c.start();let l=()=>{N7=true,E.info("Received shutdown signal, stopping runner..."),c.shutdown(),e.flush().catch(()=>{}).finally(()=>process.exit(0));};process.once("SIGTERM",l),process.once("SIGINT",l),process.on("exit",()=>{N7||console.error(`
20
20
  Warning: The process exited without blocking. The runner needs the process to stay alive to process jobs.
21
21
  Use a server framework like express or fastify to keep the process running.
22
- `);});}function vne(p,o){let e=[" \u2800\u20DD","opik ",`runner: ${p}`];o&&e.push(` project: ${o}`),console.log(e.join("")),console.log();}var U7,N7,KU=n(()=>{re();Vo();bu();D7();GU();U7=false,N7=false;});function Ene(p){return !!p&&(typeof p=="object"||typeof p=="function")&&typeof p.then=="function"}function Cne({name:p,parentSpan:o,projectName:e,trace:t,type:i="llm",environment:a}){var l;E.debug("Creating new span:",{name:p,parentSpan:o==null?void 0:o.data.id,projectName:e,type:i,environment:a});let s=t;if(s)a!==void 0&&a!==s.data.environment&&E.warn(`Nested @track requested environment "${a}", but the enclosing trace already uses "${(l=s.data.environment)!=null?l:"(none)"}". The outer environment will be used.`);else {let m=b7();s=_u().trace({name:p,projectName:e,...a!==void 0?{environment:a}:{},...m?{id:m}:{}});}let c=s.span({name:p,parentSpanId:o==null?void 0:o.data.id,projectName:e,type:i});return E.debug("Span created with ID:",c.data.id),{span:c,trace:s}}function Ine({args:p,span:o,trace:e}){if(E.debug("Starting span execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id}),p.length===0)return;let t={arguments:p};E.debug("Recording span input"),o.update({input:t}),e&&(E.debug("Recording trace input"),e.update({input:t}));}function F7({result:p,span:o,trace:e,enrichSpan:t}){E.debug("Recording successful execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id});let i=typeof p=="object"?p:{result:p},a=new Date,s={endTime:a,output:i};if(t){let c=t(p);Object.assign(s,c);}o.update(s),e&&e.update({endTime:a,output:i});}function Dne(p){var o;try{let e=p instanceof Error?p:new Error(String(p));return {message:e.message,exceptionType:e.name,traceback:p instanceof Error&&(o=e.stack)!=null?o:""}}catch{return {message:"<thrown value could not be described>",exceptionType:"Error",traceback:""}}}function B7({span:p,error:o,trace:e}){let t=Dne(o);E.error("Recording execution error:",{spanId:p.data.id,traceId:e==null?void 0:e.data.id,error:o instanceof Error?{name:t.exceptionType,message:t.message,stack:t.traceback}:o}),p.update({errorInfo:t}),p.end(),e&&(e.update({errorInfo:t}),e.end());}function zu({name:p,projectName:o,type:e,enrichSpan:t,environment:i}={},a){return function(...c){if(!Le())return a.apply(this,c);let l=Kt.getStore(),{span:m,trace:u}=Cne({name:p!=null?p:a.name||jne,parentSpan:l==null?void 0:l.span,projectName:o,trace:l==null?void 0:l.trace,type:e,environment:i}),d=!l,f=this;return Kt.run({span:m,trace:u},()=>{let g=d?u:void 0;try{Ine({args:c,span:m,trace:g});let h=a.apply(f,c);return Ene(h)?h.then(y=>(F7({span:m,result:y,trace:g,enrichSpan:t}),y),y=>{throw B7({span:m,error:y,trace:g}),y}):(F7({span:m,result:h,trace:g,enrichSpan:t}),h)}catch(h){throw B7({span:m,error:h,trace:g}),h}})}}function et(p,o){if(typeof p=="function")return zu({},p);let e=p;if(o){let t=zu(e,o);return e.entrypoint&&Une(o,t,e),t}return function(...t){if(t.length===2&&typeof t[1]=="object"&&t[1]!==null&&"kind"in t[1]){let[s,c]=t;if(c.kind!=="method")throw new Error("track decorator is only applicable to methods");return zu(e,s)}let[,,i]=t;if(!i||typeof i.value!="function")throw new Error("track decorator can only be applied to methods");let a=i.value;return i.value=zu(e,a),i}}function Une(p,o,e){var m;let t=e.name||p.name;if(!t)throw new Error("entrypoint functions must have a name. Provide one via track({ name: '...' }) or use a named function.");let i=e.projectName||_u().config.projectName,a=(m=e.params)!=null?m:A7(p),s=new Set(["string","number","float","integer","boolean"]),c=a.filter(u=>!s.has(u.type));if(c.length>0){let u=c.map(d=>`${d.name} (${d.type})`);E.warn(`Could not resolve type for parameter(s) [${u.join(", ")}] in "${t}". These parameters will default to 'string' and cannot be modified via the UI. Consider using a supported type (string, number, boolean) or choosing a different entrypoint.`);}let l=a.map(u=>({...u,type:u.type==="number"?"float":u.type}));_7({func:o,name:t,project:i,params:l,docstring:""}),OU();}function _u(){return QU===null&&(QU=new Rr),QU}var jne,Kt,Qt,QU,Nr=n(()=>{Vo();ro();re();yu();bu();KU();jne="track.decorator",Kt=new AsyncLocalStorage,Qt=()=>{let{span:p,trace:o}=Kt.getStore()||{};if(!(!p||!o))return {span:p,trace:o}};QU=null;});function Nne(p,o){var e;if(p!=null)return o==="prompt"?(e=p.commit)!=null?e:void 0:o==="prompt_commit"?UU(p,o):p}function $U(p){let{values:o,fieldNames:e,blueprintId:t,blueprintVersion:i,isFallback:a,maskId:s}=p,c={...o};return Object.defineProperties(c,{blueprintId:{value:t,enumerable:false,writable:false},blueprintVersion:{value:i,enumerable:false,writable:false},isFallback:{value:a,enumerable:false,writable:false}}),new Proxy(c,{get(m,u){return typeof u!="string"||Fne.has(u)||e.has(u)&&Bne({blueprintId:t,blueprintVersion:i,maskId:s,fieldNames:e,values:o}),Reflect.get(m,u)}})}function Bne(p){let o=Qt();if(!o)return;let{blueprintId:e,blueprintVersion:t,maskId:i,fieldNames:a,values:s}=p,c={};for(let u of a){let d=s[u];if(d===void 0)continue;let f=gu(d);c[u]={value:Nne(d,f),type:f};}let l={_blueprint_id:e,blueprint_version:t,values:c};i!==void 0&&(l._mask_id=i);let m={agent_configuration:l};o.span.update({metadata:m}),o.trace.update({metadata:m});}var Fne,M7=n(()=>{Nr();mp();Fne=new Set(["blueprintId","blueprintVersion","isFallback"]);});function L7(){let p=process.env.OPIK_CONFIG_TTL_SECONDS;if(p!==void 0){let o=parseInt(p,10);if(!isNaN(o))return o}return Mne}function V7(p,o,e,t=null){return ZU.getOrCreate(p,o,e,t)}function qU(p,o,e,t,i,a=null){let s=ZU.getOrCreate(p,o,e,a);t!==null&&s.update(t),i!==null&&e===null&&(s.setRefreshCallback(i),ZU.ensureRefreshTimerStarted());}var Mne,Wne,W7,YU,XU,ZU,J7=n(()=>{re();Mne=300,Wne=1e3,W7=100;YU=class{constructor(o){this._blueprint=null;this._lastFetchMs=null;this._refreshCallback=null;this._ttlMs=o*1e3;}setRefreshCallback(o){this._refreshCallback===null&&(this._refreshCallback=o);}update(o){this._blueprint=o,this._lastFetchMs=Date.now();}getBlueprint(){return this._blueprint}isStale(){return this._lastFetchMs===null?true:Date.now()-this._lastFetchMs>=this._ttlMs}async tryBackgroundRefresh(){if(this._refreshCallback!==null)try{let o=await this._refreshCallback();o!==null&&this.update(o);}catch(o){E.debug("Background blueprint cache refresh failed",o);}}},XU=class{constructor(){this._entries=new Map;this._intervalHandle=null;this._refreshRunning=false;}getOrCreate(o,e,t,i=null){let a=`${o}::${e!=null?e:""}::${t!=null?t:""}::${i!=null?i:""}`,s=this._entries.get(a);return s||(s=new YU(L7()),this._entries.set(a,s)),s}ensureRefreshTimerStarted(){if(this._intervalHandle!==null)return;let o=Math.max(L7()*1e3,Wne);this._intervalHandle=setInterval(()=>{this._refreshAllStale();},o),this._intervalHandle.unref();}async _refreshAllStale(){if(!this._refreshRunning){this._refreshRunning=true;try{let o=[...this._entries.values()].filter(e=>e.isStale());for(let e=0;e<o.length;e+=W7)await Promise.all(o.slice(e,e+W7).map(t=>t.tryBackgroundRefresh()));}finally{this._refreshRunning=false;}}}clear(){this._intervalHandle!==null&&(clearInterval(this._intervalHandle),this._intervalHandle=null),this._refreshRunning=false,this._entries.clear();}},ZU=new XU;});var CZ={};L(CZ,{OpikClient:()=>Rr,clients:()=>ku,resetDefaultProjectWarning:()=>Lne});function Lne(){eN=false;}var ku,eN,Vne,Su,Rr,Vo=n(()=>{pt();ro();jr();te();Gf();O8();ar();re();aU();Z8();cZ();lZ();mZ();dZ();uZ();gZ();bU();Zd();xU();TZ();Qf();kU();Xr();TU();iu();ap();Lt();Dr();HZ();vU();jU();cu();du();fU();DU();FU();fu();mp();M7();xu();J7();Nr();kd();Vd();gU();pt();ku=[],eN=false;Vne=tu+500,Su=class Su{constructor(o){this.displayTraceLog=(o,e)=>{if(e===this.lastProjectNameLogged||!this.config.apiUrl)return;let t=X8(o,this.config.apiUrl);E.info(`Started logging traces to the "${e}" project at ${zp(t)}`),this.lastProjectNameLogged=e;};this.trace=o=>{E.debug("Creating new trace with data:",o);let e=this.resolveProjectName(o.projectName),t=o.environment!==void 0?o.environment:this.config.environment,i=new Pd({id:ue(),startTime:new Date,source:"sdk",...o,projectName:e,...t!==void 0?{environment:t}:{}},this);return Le()&&(this.traceBatchQueue.create(i.data),E.debug("Trace added to the queue with ID:",i.data.id),this.displayTraceLog(i.data.id,e)),i};this.getDataset=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting dataset with name "${o}"`);try{await this.datasetBatchQueue.flush();let i=await this.api.datasets.getDatasetByIdentifier({datasetName:o,projectName:t});return new hr({...i,projectName:t},this)}catch(i){throw i instanceof k&&i.statusCode===404?new Wt(o):i}};this.createDataset=async(o,e,t)=>{let i=this.resolveProjectName(t);E.debug(`Creating dataset with name "${o}"`);let a=new hr({name:o,description:e,projectName:i},this);try{return this.datasetBatchQueue.create({name:a.name,description:a.description,id:a.id,projectName:i}),E.debug("Dataset added to the queue with name:",a.name),a}catch(s){throw E.error(`Failed to create dataset "${o}"`,{error:s}),new Error(`Error creating dataset "${o}": ${s}`)}};this.getOrCreateDataset=async(o,e,t)=>{E.debug(`Attempting to retrieve or create dataset with name: "${o}"`);try{return await this.getDataset(o,t)}catch(i){if(i instanceof Wt)return E.info(`Dataset "${o}" not found. Proceeding to create a new one.`),this.createDataset(o,e,t);throw E.error(`Error retrieving dataset "${o}":`,i),i}};this.getDatasets=async(o=100,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting all datasets (limit: ${o})`);try{await this.datasetBatchQueue.flush();let i;try{i=await this.getProjectIdByName(t);}catch{}let a=await this.api.datasets.findDatasets({size:o,...i&&{projectId:i}}),s=[];for(let c of a.content||[])s.push(new hr({...c,projectName:t},this));return E.info(`Retrieved ${s.length} datasets`),s}catch(i){throw E.error("Failed to retrieve datasets",{error:i}),new Error("Failed to retrieve datasets")}};this.deleteDataset=async(o,e)=>{E.debug(`Deleting dataset with name "${o}"`);try{let t=await this.getDataset(o,e);if(!t.id)throw new Error(`Cannot delete dataset "${o}": ID not available`);this.datasetBatchQueue.delete(t.id);}catch(t){throw E.error(`Failed to delete dataset "${o}"`,{error:t}),new Error(`Failed to delete dataset "${o}": ${t}`)}};this.createTestSuite=async o=>{E.debug(`Creating test suite with name "${o.name}"`);let{TestSuite:e}=await import('./suite-WCNEGQY5.js');return e.create(this,o)};this.getTestSuite=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting test suite with name "${o}"`);let{TestSuite:i}=await import('./suite-WCNEGQY5.js');return i.get(this,o,t)};this.getOrCreateTestSuite=async o=>{E.debug(`Attempting to retrieve or create test suite with name: "${o.name}"`);let{TestSuite:e}=await import('./suite-WCNEGQY5.js');return e.getOrCreate(this,o)};this.deleteTestSuite=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Deleting test suite with name "${o}"`);let{TestSuite:i}=await import('./suite-WCNEGQY5.js');await i.delete(this,o,t);};this.getTestSuites=async(o=1e3,e)=>{var i;let t=this.resolveProjectName(e);E.debug(`Getting all test suites (limit: ${o})`);try{await this.datasetBatchQueue.flush();let a=await this.resolveProjectId(t),{TestSuite:s}=await import('./suite-WCNEGQY5.js'),c=[],l=1,m=100;for(;c.length<o;){let d=(i=(await this.api.datasets.findDatasets({page:l,size:m,...a&&{projectId:a}})).content)!=null?i:[];if(d.length===0)break;for(let f of d){if(c.length>=o)break;f.type===Bf.EvaluationSuite&&c.push(new s(new hr({...f,projectName:t},this),this));}l++;}return E.info(`Retrieved ${c.length} test suites`),c}catch(a){throw E.error("Failed to retrieve test suites",{error:a}),new Error("Failed to retrieve test suites")}};this.createTracesAnnotationQueue=async o=>this.createAnnotationQueueInternal(o,Zr);this.createThreadsAnnotationQueue=async o=>this.createAnnotationQueueInternal(o,qr);this.getTracesAnnotationQueue=async o=>this.fetchAnnotationQueueById(o,"trace",Zr);this.getThreadsAnnotationQueue=async o=>this.fetchAnnotationQueueById(o,"thread",qr);this.getTracesAnnotationQueues=async o=>(await this.getAnnotationQueuesByScope("trace",o)).map(t=>new Zr(t,this));this.getThreadsAnnotationQueues=async o=>(await this.getAnnotationQueuesByScope("thread",o)).map(t=>new qr(t,this));this.deleteTracesAnnotationQueue=async o=>this.deleteAnnotationQueueById(o,"traces");this.deleteThreadsAnnotationQueue=async o=>this.deleteAnnotationQueueById(o,"threads");this.createExperiment=async({datasetName:o,name:e,experimentConfig:t,prompts:i,type:a=Jf.Regular,optimizationId:s,datasetVersionId:c,evaluationMethod:l,tags:m,projectName:u})=>{if(E.debug(`Creating experiment for dataset "${o}"`),!o)throw new Error("Dataset name is required to create an experiment");let[d,f]=vZ(t,i),g=this.resolveProjectName(u),h=ue(),y=new Ir({id:h,name:e,datasetName:o,prompts:i,tags:m,projectName:g},this);try{return await this.api.experiments.createExperiment({id:h,datasetName:o,name:e,metadata:d,promptVersions:f,type:a,optimizationId:s,datasetVersionId:c,tags:m,evaluationMethod:l,projectName:g}),E.debug("Experiment created with id:",h),y}catch(w){throw E.error(`Failed to create experiment for dataset "${o}"`,{error:w}),new Error(`Error creating experiment: ${w}`)}};this.updateExperiment=async(o,e)=>{if(!o)throw new Error("id is required to update an experiment");let{name:t,experimentConfig:i}=e;if(!t&&!i)throw new Error("At least one of 'name' or 'experimentConfig' must be provided to update an experiment");E.debug(`Updating experiment with ID "${o}"`);let a={};t!==void 0&&(a.name=t),i!==void 0&&(a.metadata=i);try{await this.api.experiments.updateExperiment(o,{body:a});}catch(s){throw E.error(`Failed to update experiment with ID "${o}"`,{error:s}),s}};this.getExperimentById=async o=>{var e,t;E.debug(`Getting experiment with ID "${o}"`);try{let i=await this.api.experiments.getExperimentById(o);return new Ir({id:i.id,name:i.name,datasetName:(e=i.datasetName)!=null?e:void 0,projectName:(t=i.projectName)!=null?t:void 0},this)}catch(i){throw i instanceof k&&i.statusCode===404?new ep(`No experiment found with ID '${o}'`):(E.error(`Failed to get experiment with ID "${o}"`,{error:i}),i)}};this.getExperimentsByName=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting experiments with name "${o}"`);try{let i=await this.api.experiments.streamExperiments({name:o,projectName:t});return (await Ne(i,G.ExperimentPublic)).map(s=>{var c,l;return new Ir({id:s.id,name:s.name,datasetName:(c=s.datasetName)!=null?c:void 0,projectName:(l=s.projectName)!=null?l:void 0},this)})}catch(i){throw E.error(`Failed to get experiments with name "${o}"`,{error:i}),i}};this.getExperiment=async(o,e)=>{E.debug(`Getting experiment with name "${o}"`);let t=await this.getExperimentsByName(o,e);if(t.length===0)throw new ep(o);return t[0]};this.getDatasetExperiments=async(o,e=100,t)=>{E.debug(`Getting experiments for dataset "${o}"`);let i=await this.getDataset(o,t);try{return await this.findExperimentsByDatasetId(i.id,e,a=>{var s;return new Ir({id:a.id,name:a.name,datasetName:(s=a.datasetName)!=null?s:void 0},this)})}catch(a){throw E.error(`Failed to get experiments for dataset "${o}"`,{error:a}),a}};this.getTestSuiteExperiments=async(o,e=100,t)=>{E.debug(`Getting experiments for test suite "${o}"`);let i=await this.getDataset(o,t);try{return await this.findExperimentsByDatasetId(i.id,e,a=>{var s;return new qd({id:a.id,name:a.name,datasetName:(s=a.datasetName)!=null?s:void 0,passRate:a.passRate,passedCount:a.passedCount,totalCount:a.totalCount,assertionScores:a.assertionScores},this)})}catch(a){throw E.error(`Failed to get experiments for test suite "${o}"`,{error:a}),a}};this.findExperimentsByDatasetId=async(o,e,t)=>{var c;let i=Math.min(100,e),a=[],s=1;for(;a.length<e;){let l=await this.api.experiments.findExperiments({page:s,size:i,datasetId:o}),m=(c=l==null?void 0:l.content)!=null?c:[];if(m.length===0)break;let u=e-a.length,d=Math.min(m.length,u);for(let f=0;f<d;f++)a.push(t(m[f]));if(d<m.length)break;s+=1;}return a};this.deleteExperiment=async o=>{E.debug(`Deleting experiment with ID "${o}"`);try{await this.api.experiments.deleteExperimentsById({ids:[o]});}catch(e){throw E.error(`Failed to delete experiment with ID "${o}"`,{error:e}),e}};this.createPromptInternal=async(o,e,t,i,a,s,c,l,m)=>{var u;E.debug(`Creating ${l}`,{name:o});try{let d=await VZ(this.api.prompts,o,this.api.requestOptions);a(d);let f=(u=i.type)!=null?u:Fe.MUSTACHE,g=JZ({prompt:e,metadata:i.metadata},d,f),h;if(g?(E.debug(`Creating new ${l} version`,{name:o}),h=await this.api.prompts.createPromptVersion({name:o,version:{template:e,metadata:i.metadata,type:f},templateStructure:t,projectName:m},this.api.requestOptions)):(E.debug(`Returning existing ${l} version`,{name:o}),h=d),!h.promptId)throw new Error("Invalid API response: missing promptId");let y=await this.api.prompts.getPromptById(h.promptId,{},this.api.requestOptions),w=s(y,h);return E.debug(`${l} created`,{name:o}),i.description||i.tags?await w.updateProperties({description:i.description,tags:i.tags}):w}catch(d){if(d instanceof k||d instanceof Mr)return E.warn(`Failed to sync ${l} '${o}' with the backend. The prompt will work locally but is not persisted on the server. You can retry by calling .syncWithBackend().`,{error:d}),c();throw E.error(`Failed to create ${l}`,{name:o,error:d}),d}};this.createPrompt=async o=>{let e=this.resolveProjectName(o.projectName);return this.createPromptInternal(o.name,o.prompt,Re.Text,o,()=>{},(t,i)=>pr.fromApiResponse(t,i,this,e),()=>{var t;return new pr({name:o.name,prompt:o.prompt,metadata:o.metadata,type:(t=o.type)!=null?t:Fe.MUSTACHE,description:o.description,tags:o.tags,projectName:e,synced:false},this)},"prompt",e)};this.createChatPrompt=async o=>{let e=this.resolveProjectName(o.projectName),t=JSON.stringify(o.messages);return this.createPromptInternal(o.name,t,Re.Chat,o,i=>{if(i&&i.templateStructure&&i.templateStructure!==Re.Chat)throw new Vt(o.name,i.templateStructure,Re.Chat)},(i,a)=>cr.fromApiResponse(i,a,this,e),()=>{var i;return new cr({name:o.name,messages:structuredClone(o.messages),metadata:o.metadata,type:(i=o.type)!=null?i:Fe.MUSTACHE,description:o.description,tags:o.tags,projectName:e,synced:false},this)},"chat prompt",e)};this.getPrompt=async o=>this.getPromptWithCache(o,Re.Text,(e,t,i)=>pr.fromApiResponse(e,t,this,i),"prompt");this.getChatPrompt=async o=>this.getPromptWithCache(o,Re.Chat,(e,t,i)=>cr.fromApiResponse(e,t,this,i),"chat prompt");this.getPromptWithCache=async(o,e,t,i)=>{if(o.commit&&o.version)throw new Error("Provide either `commit` or `version`, not both. Prefer `version` \u2014 `commit` is deprecated.");if(E.debug(`Getting ${i}`,o),o.commit&&o.environment)throw new Error("'commit' and 'environment' are mutually exclusive; pass at most one.");if(o.version&&o.environment)throw new Error("'version' and 'environment' are mutually exclusive; pass at most one.");let a=this.resolveProjectName(o.projectName),s=async u=>{var d;try{let f,g;if(u){if(g=await this.api.prompts.getPromptVersionById(u,{},this.api.requestOptions),!g.promptId)return null;f=await this.api.prompts.getPromptById(g.promptId,{},this.api.requestOptions);}else {let y;try{y=await this.getProjectIdByName(a);}catch{}if(f=(d=(await this.api.prompts.getPrompts({filters:JSON.stringify([{field:"name",operator:"=",value:o.name}]),size:1,...y&&{projectId:y}},this.api.requestOptions)).content)==null?void 0:d[0],!f)return E.debug(`${i.charAt(0).toUpperCase()+i.slice(1)} not found`,{name:o.name}),null;let A={name:o.name,projectName:a,...o.commit?{commit:o.commit}:{},...o.version?{versionNumber:o.version}:{},...o.environment?{environment:o.environment}:{}};g=await this.api.prompts.retrievePromptVersion(A,this.api.requestOptions);}let h=g.templateStructure;if(e===Re.Text){if(h&&h!==Re.Text)throw new Vt(o.name,h,Re.Text)}else if(!h||h!==Re.Chat)throw new Vt(o.name,h!=null?h:"undefined",Re.Chat);return t(f,g,a)}catch(f){if(f instanceof k&&f.statusCode===404)return null;throw E.error(`Failed to get ${i}`,{name:o.name,error:f}),f}},c=await su(o.name,o.commit,a,e,()=>s(),this.config.promptCacheTtlSeconds,void 0,o.version,o.environment),l=c!=null&&c.id?KZ(c.id):null,m=l?await su(o.name,o.commit,a,e,()=>s(l),this.config.promptCacheTtlSeconds,l,o.version):c;if(m!==null){let u=Qt();u&&(Er.promptAlreadyInjected(u.trace.data.metadata,m.id,m.commit)||u.trace.update({prompts:[m],appendPrompts:true}),Er.promptAlreadyInjected(u.span.data.metadata,m.id,m.commit)||u.span.update({prompts:[m],appendPrompts:true}));}return m};this.searchPrompts=async o=>{var e;E.debug("Searching prompts",{filterString:o});try{let t;if(o){let l=yr.forPrompts(o).getFilterExpressions();t=l?JSON.stringify(l):void 0;}let a=(e=(await this.api.prompts.getPrompts({filters:t,size:1e3},this.api.requestOptions)).content)!=null?e:[];return (await Promise.all(a.map(async c=>{if(!c.name)return null;try{let l=await this.api.prompts.retrievePromptVersion({name:c.name},this.api.requestOptions),m=l.templateStructure,u=this.resolveProjectName();return !m||m===Re.Text?pr.fromApiResponse(c,l,this,u):m===Re.Chat?cr.fromApiResponse(c,l,this,u):null}catch(l){return E.debug("Failed to get version for prompt",{name:c.name,error:l}),null}}))).filter(c=>c!==null)}catch(t){throw E.error("Failed to search prompts",{error:t}),t}};this.deletePrompts=async o=>{E.debug("Deleting prompts in batch",{count:o.length});try{await this.api.prompts.deletePromptsBatch({ids:o},this.api.requestOptions),nu().evictByIds(o),E.info("Successfully deleted prompts",{count:o.length});}catch(e){throw E.error("Failed to delete prompts",{count:o.length,error:e}),e}};this.setPromptEnvironments=async o=>{let e;try{e=await this.api.prompts.retrievePromptVersion({name:o.promptName,versionNumber:o.version,projectName:this.resolveProjectName(o.projectName)},this.api.requestOptions);}catch(i){throw i instanceof k&&i.statusCode===404?o.version!==void 0?new Lo(`No version '${o.version}' found for prompt '${o.promptName}'.`):new Lo(`No prompt found with name '${o.promptName}'.`):i}let t=Array.from(new Set(o.environments));try{await this.api.prompts.setPromptVersionEnvironment(e.id,{environments:t},this.api.requestOptions);}catch(i){throw i instanceof k&&(i.statusCode===404||i.statusCode===409)?new ru(`One or more environments in [${t.join(", ")}] are not registered in this workspace.`):i}nu().invalidateForPrompt(o.promptName,this.resolveProjectName(o.projectName));};this.searchTraces=async o=>{let{exclude:e,...t}=o!=null?o:{};return this.executeSearch("traces",t,lu,(i,a,s,c,l)=>c7(i,a,s,c,l,e))};this.searchThreads=async o=>this.executeSearch("threads",o!=null?o:{},mu,m7);this.searchSpans=async o=>{let{exclude:e,...t}=o!=null?o:{};return this.executeSearch("spans",t,u7,(i,a,s,c,l)=>d7(i,a,s,c,l,e))};this.createEnvironment=async(o,e)=>{let t=ue();try{await this.api.environments.createEnvironment({id:t,name:o,description:e==null?void 0:e.description,color:e==null?void 0:e.color});}catch(i){throw i instanceof k&&i.statusCode===409?new Hd(o):i}return this.api.environments.getEnvironmentById(t)};this.getEnvironments=async()=>{var e;return (e=(await this.api.environments.findEnvironments()).content)!=null?e:[]};this.updateEnvironment=async(o,e)=>{if((e==null?void 0:e.color)!==void 0&&Su.BUILTIN_ENVIRONMENT_NAMES.has(o))throw new Gd(`Cannot change the colour of the built-in environment '${o}'. Colour updates are not allowed for 'production', 'staging', or 'development'.`);let t=await this._findEnvironmentByName(o,true);return await this.api.environments.updateEnvironment(t.id,{description:e==null?void 0:e.description,color:e==null?void 0:e.color}),this.api.environments.getEnvironmentById(t.id)};this.deleteEnvironment=async o=>{let e=await this._findEnvironmentByName(o,false);e&&await this.api.environments.deleteEnvironmentsBatch({ids:[e.id]});};this._findEnvironmentByName=async(o,e)=>{let i=(await this.getEnvironments()).find(a=>a.name===o);if(!i&&e)throw new Error(`No environment found with name "${o}".`);return i};this.flush=async o=>{var t;let e=(t=o==null?void 0:o.silent)!=null?t:false;E.debug("Starting flush operation");try{await this.traceBatchQueue.flush(),await this.spanBatchQueue.flush(),await this.traceFeedbackScoresBatchQueue.flush(),await this.spanFeedbackScoresBatchQueue.flush(),await this.traceAssertionResultsBatchQueue.flush(),await this.datasetBatchQueue.flush(),e||E.info("Successfully flushed all data to Opik");}catch(i){E.error("Error during flush operation:",{error:i instanceof Error?i.message:i});}};this.createConfig=async(o,e)=>{var l,m;let t=(l=e==null?void 0:e.projectName)!=null?l:this.config.projectName;this._validatePromptProjects(o,t);let i=new Ot(t,this),a=NU(o),s=await i.getBlueprint(),c;if(s)c=await i.updateBlueprint({values:a,description:e==null?void 0:e.description});else try{c=await i.createBlueprint({values:a,description:e==null?void 0:e.description});}catch(u){if(u instanceof k&&u.statusCode===409)c=await i.updateBlueprint({values:a,description:e==null?void 0:e.description});else throw u}return (m=c.name)!=null?m:c.id};this.setConfigEnv=async o=>{var s;let e=(s=o.projectName)!=null?s:this.config.projectName,i=await new Ot(e,this).getBlueprint({name:o.version});if(!i)throw new Cr(`No config version "${o.version}" found in project "${e}".`);let a=await this.api.projects.retrieveProject({name:e});if(!(a!=null&&a.id))throw new Error(`Project "${e}" not found`);await this.api.agentConfigs.createOrUpdateEnvs({projectId:a.id,envs:[{envName:o.env,blueprintId:i.id}]});};this.updatePromptVersionTags=async(o,e)=>{var t;E.debug("Updating prompt version tags",{count:o.length,options:e});try{await this.api.prompts.updatePromptVersions({ids:o,update:{tags:(t=e==null?void 0:e.tags)!=null?t:void 0},mergeTags:e==null?void 0:e.mergeTags},this.api.requestOptions),E.debug("Successfully updated prompt version tags",{count:o.length});}catch(i){throw E.error("Failed to update prompt version tags",{count:o.length,error:i}),i}};var a;E.debug("Initializing OpikClient with config:",o),this.config=vp(o);let e={apiKey:this.config.apiKey,environment:this.config.apiUrl,workspaceName:this.config.workspaceName};o!=null&&o.headers&&(E.debug("Initializing OpikClient with additional headers:",o==null?void 0:o.headers),e.requestOptions={headers:o==null?void 0:o.headers}),this.api=new Dd(e);let t=this.config.holdUntilFlush?1440*60*1e3:this.config.batchDelayMs,i=this.config.isAttachmentExtractionActive?{minSizeBytes:(a=this.config.minBase64EmbeddedAttachmentSize)!=null?a:Ke.minBase64EmbeddedAttachmentSize,apiUrl:this.config.apiUrl,workspaceName:this.config.workspaceName,apiKey:this.config.apiKey||void 0,extraHeaders:o==null?void 0:o.headers}:void 0;this.spanBatchQueue=new jd(this.api,t,this.config.maxPayloadSizeMb,i),this.traceBatchQueue=new Cd(this.api,t,this.config.maxPayloadSizeMb,i),this.spanFeedbackScoresBatchQueue=new Ed(this.api,t),this.traceFeedbackScoresBatchQueue=new Id(this.api,t),this.traceAssertionResultsBatchQueue=new Sd(this.api,t,"TRACE"),this.datasetBatchQueue=new Ud(this.api,t),ku.push(this);}resolveProjectName(o){return o!==void 0?o:(!eN&&this.config.projectName===Ke.projectName&&(eN=true,E.warn(`No project name configured. Traces are being logged to "Default Project".
22
+ `);});}function vne(p,o){let e=[" \u2800\u20DD","opik ",`runner: ${p}`];o&&e.push(` project: ${o}`),console.log(e.join("")),console.log();}var U7,N7,KU=n(()=>{re();Vo();bu();D7();GU();U7=false,N7=false;});function Ene(p){return !!p&&(typeof p=="object"||typeof p=="function")&&typeof p.then=="function"}function Cne({name:p,parentSpan:o,projectName:e,trace:t,type:i="llm",environment:a}){var l;E.debug("Creating new span:",{name:p,parentSpan:o==null?void 0:o.data.id,projectName:e,type:i,environment:a});let s=t;if(s)a!==void 0&&a!==s.data.environment&&E.warn(`Nested @track requested environment "${a}", but the enclosing trace already uses "${(l=s.data.environment)!=null?l:"(none)"}". The outer environment will be used.`);else {let m=b7();s=_u().trace({name:p,projectName:e,...a!==void 0?{environment:a}:{},...m?{id:m}:{}});}let c=s.span({name:p,parentSpanId:o==null?void 0:o.data.id,projectName:e,type:i});return E.debug("Span created with ID:",c.data.id),{span:c,trace:s}}function Ine({args:p,span:o,trace:e}){if(E.debug("Starting span execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id}),p.length===0)return;let t={arguments:p};E.debug("Recording span input"),o.update({input:t}),e&&(E.debug("Recording trace input"),e.update({input:t}));}function F7({result:p,span:o,trace:e,enrichSpan:t}){var c,l;E.debug("Recording successful execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id});let i=typeof p=="object"?p:{result:p},a=new Date,s={endTime:a,output:i};if(t){let m=t(p);Object.assign(s,m);}s.output=(c=o.data.output)!=null?c:s.output,o.update(s),e&&e.update({endTime:a,output:(l=e.data.output)!=null?l:i});}function Dne(p){var o;try{let e=p instanceof Error?p:new Error(String(p));return {message:e.message,exceptionType:e.name,traceback:p instanceof Error&&(o=e.stack)!=null?o:""}}catch{return {message:"<thrown value could not be described>",exceptionType:"Error",traceback:""}}}function B7({span:p,error:o,trace:e}){let t=Dne(o);E.error("Recording execution error:",{spanId:p.data.id,traceId:e==null?void 0:e.data.id,error:o instanceof Error?{name:t.exceptionType,message:t.message,stack:t.traceback}:o}),p.update({errorInfo:t}),p.end(),e&&(e.update({errorInfo:t}),e.end());}function zu({name:p,projectName:o,type:e,enrichSpan:t,environment:i}={},a){return function(...c){if(!Le())return a.apply(this,c);let l=Kt.getStore(),{span:m,trace:u}=Cne({name:p!=null?p:a.name||jne,parentSpan:l==null?void 0:l.span,projectName:o,trace:l==null?void 0:l.trace,type:e,environment:i}),d=!l,f=this;return Kt.run({span:m,trace:u},()=>{let g=d?u:void 0;try{Ine({args:c,span:m,trace:g});let h=a.apply(f,c);return Ene(h)?h.then(y=>(F7({span:m,result:y,trace:g,enrichSpan:t}),y),y=>{throw B7({span:m,error:y,trace:g}),y}):(F7({span:m,result:h,trace:g,enrichSpan:t}),h)}catch(h){throw B7({span:m,error:h,trace:g}),h}})}}function et(p,o){if(typeof p=="function")return zu({},p);let e=p;if(o){let t=zu(e,o);return e.entrypoint&&Une(o,t,e),t}return function(...t){if(t.length===2&&typeof t[1]=="object"&&t[1]!==null&&"kind"in t[1]){let[s,c]=t;if(c.kind!=="method")throw new Error("track decorator is only applicable to methods");return zu(e,s)}let[,,i]=t;if(!i||typeof i.value!="function")throw new Error("track decorator can only be applied to methods");let a=i.value;return i.value=zu(e,a),i}}function Une(p,o,e){var m;let t=e.name||p.name;if(!t)throw new Error("entrypoint functions must have a name. Provide one via track({ name: '...' }) or use a named function.");let i=e.projectName||_u().config.projectName,a=(m=e.params)!=null?m:A7(p),s=new Set(["string","number","float","integer","boolean"]),c=a.filter(u=>!s.has(u.type));if(c.length>0){let u=c.map(d=>`${d.name} (${d.type})`);E.warn(`Could not resolve type for parameter(s) [${u.join(", ")}] in "${t}". These parameters will default to 'string' and cannot be modified via the UI. Consider using a supported type (string, number, boolean) or choosing a different entrypoint.`);}let l=a.map(u=>({...u,type:u.type==="number"?"float":u.type}));_7({func:o,name:t,project:i,params:l,docstring:""}),OU();}function _u(){return QU===null&&(QU=new Rr),QU}var jne,Kt,Qt,QU,Nr=n(()=>{Vo();ro();re();yu();bu();KU();jne="track.decorator",Kt=new AsyncLocalStorage,Qt=()=>{let{span:p,trace:o}=Kt.getStore()||{};if(!(!p||!o))return {span:p,trace:o}};QU=null;});function Nne(p,o){var e;if(p!=null)return o==="prompt"?(e=p.commit)!=null?e:void 0:o==="prompt_commit"?UU(p,o):p}function $U(p){let{values:o,fieldNames:e,blueprintId:t,blueprintVersion:i,isFallback:a,maskId:s}=p,c={...o};return Object.defineProperties(c,{blueprintId:{value:t,enumerable:false,writable:false},blueprintVersion:{value:i,enumerable:false,writable:false},isFallback:{value:a,enumerable:false,writable:false}}),new Proxy(c,{get(m,u){return typeof u!="string"||Fne.has(u)||e.has(u)&&Bne({blueprintId:t,blueprintVersion:i,maskId:s,fieldNames:e,values:o}),Reflect.get(m,u)}})}function Bne(p){let o=Qt();if(!o)return;let{blueprintId:e,blueprintVersion:t,maskId:i,fieldNames:a,values:s}=p,c={};for(let u of a){let d=s[u];if(d===void 0)continue;let f=gu(d);c[u]={value:Nne(d,f),type:f};}let l={_blueprint_id:e,blueprint_version:t,values:c};i!==void 0&&(l._mask_id=i);let m={agent_configuration:l};o.span.update({metadata:m}),o.trace.update({metadata:m});}var Fne,M7=n(()=>{Nr();mp();Fne=new Set(["blueprintId","blueprintVersion","isFallback"]);});function L7(){let p=process.env.OPIK_CONFIG_TTL_SECONDS;if(p!==void 0){let o=parseInt(p,10);if(!isNaN(o))return o}return Mne}function V7(p,o,e,t=null){return ZU.getOrCreate(p,o,e,t)}function qU(p,o,e,t,i,a=null){let s=ZU.getOrCreate(p,o,e,a);t!==null&&s.update(t),i!==null&&e===null&&(s.setRefreshCallback(i),ZU.ensureRefreshTimerStarted());}var Mne,Wne,W7,YU,XU,ZU,J7=n(()=>{re();Mne=300,Wne=1e3,W7=100;YU=class{constructor(o){this._blueprint=null;this._lastFetchMs=null;this._refreshCallback=null;this._ttlMs=o*1e3;}setRefreshCallback(o){this._refreshCallback===null&&(this._refreshCallback=o);}update(o){this._blueprint=o,this._lastFetchMs=Date.now();}getBlueprint(){return this._blueprint}isStale(){return this._lastFetchMs===null?true:Date.now()-this._lastFetchMs>=this._ttlMs}async tryBackgroundRefresh(){if(this._refreshCallback!==null)try{let o=await this._refreshCallback();o!==null&&this.update(o);}catch(o){E.debug("Background blueprint cache refresh failed",o);}}},XU=class{constructor(){this._entries=new Map;this._intervalHandle=null;this._refreshRunning=false;}getOrCreate(o,e,t,i=null){let a=`${o}::${e!=null?e:""}::${t!=null?t:""}::${i!=null?i:""}`,s=this._entries.get(a);return s||(s=new YU(L7()),this._entries.set(a,s)),s}ensureRefreshTimerStarted(){if(this._intervalHandle!==null)return;let o=Math.max(L7()*1e3,Wne);this._intervalHandle=setInterval(()=>{this._refreshAllStale();},o),this._intervalHandle.unref();}async _refreshAllStale(){if(!this._refreshRunning){this._refreshRunning=true;try{let o=[...this._entries.values()].filter(e=>e.isStale());for(let e=0;e<o.length;e+=W7)await Promise.all(o.slice(e,e+W7).map(t=>t.tryBackgroundRefresh()));}finally{this._refreshRunning=false;}}}clear(){this._intervalHandle!==null&&(clearInterval(this._intervalHandle),this._intervalHandle=null),this._refreshRunning=false,this._entries.clear();}},ZU=new XU;});var CZ={};L(CZ,{OpikClient:()=>Rr,clients:()=>ku,resetDefaultProjectWarning:()=>Lne});function Lne(){eN=false;}var ku,eN,Vne,Su,Rr,Vo=n(()=>{pt();ro();jr();te();Gf();O8();ar();re();aU();Z8();cZ();lZ();mZ();dZ();uZ();gZ();bU();Zd();xU();TZ();Qf();kU();Xr();TU();iu();ap();Lt();Dr();HZ();vU();jU();cu();du();fU();DU();FU();fu();mp();M7();xu();J7();Nr();kd();Vd();gU();pt();ku=[],eN=false;Vne=tu+500,Su=class Su{constructor(o){this.displayTraceLog=(o,e)=>{if(e===this.lastProjectNameLogged||!this.config.apiUrl)return;let t=X8(o,this.config.apiUrl);E.info(`Started logging traces to the "${e}" project at ${zp(t)}`),this.lastProjectNameLogged=e;};this.trace=o=>{E.debug("Creating new trace with data:",o);let e=this.resolveProjectName(o.projectName),t=o.environment!==void 0?o.environment:this.config.environment,i=new Pd({id:ue(),startTime:new Date,source:"sdk",...o,projectName:e,...t!==void 0?{environment:t}:{}},this);return Le()&&(this.traceBatchQueue.create(i.data),E.debug("Trace added to the queue with ID:",i.data.id),this.displayTraceLog(i.data.id,e)),i};this.getDataset=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting dataset with name "${o}"`);try{await this.datasetBatchQueue.flush();let i=await this.api.datasets.getDatasetByIdentifier({datasetName:o,projectName:t});return new hr({...i,projectName:t},this)}catch(i){throw i instanceof k&&i.statusCode===404?new Wt(o):i}};this.createDataset=async(o,e,t)=>{let i=this.resolveProjectName(t);E.debug(`Creating dataset with name "${o}"`);let a=new hr({name:o,description:e,projectName:i},this);try{return this.datasetBatchQueue.create({name:a.name,description:a.description,id:a.id,projectName:i}),E.debug("Dataset added to the queue with name:",a.name),a}catch(s){throw E.error(`Failed to create dataset "${o}"`,{error:s}),new Error(`Error creating dataset "${o}": ${s}`)}};this.getOrCreateDataset=async(o,e,t)=>{E.debug(`Attempting to retrieve or create dataset with name: "${o}"`);try{return await this.getDataset(o,t)}catch(i){if(i instanceof Wt)return E.info(`Dataset "${o}" not found. Proceeding to create a new one.`),this.createDataset(o,e,t);throw E.error(`Error retrieving dataset "${o}":`,i),i}};this.getDatasets=async(o=100,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting all datasets (limit: ${o})`);try{await this.datasetBatchQueue.flush();let i;try{i=await this.getProjectIdByName(t);}catch{}let a=await this.api.datasets.findDatasets({size:o,...i&&{projectId:i}}),s=[];for(let c of a.content||[])s.push(new hr({...c,projectName:t},this));return E.info(`Retrieved ${s.length} datasets`),s}catch(i){throw E.error("Failed to retrieve datasets",{error:i}),new Error("Failed to retrieve datasets")}};this.deleteDataset=async(o,e)=>{E.debug(`Deleting dataset with name "${o}"`);try{let t=await this.getDataset(o,e);if(!t.id)throw new Error(`Cannot delete dataset "${o}": ID not available`);this.datasetBatchQueue.delete(t.id);}catch(t){throw E.error(`Failed to delete dataset "${o}"`,{error:t}),new Error(`Failed to delete dataset "${o}": ${t}`)}};this.createTestSuite=async o=>{E.debug(`Creating test suite with name "${o.name}"`);let{TestSuite:e}=await import('./suite-MVHCQXFQ.js');return e.create(this,o)};this.getTestSuite=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting test suite with name "${o}"`);let{TestSuite:i}=await import('./suite-MVHCQXFQ.js');return i.get(this,o,t)};this.getOrCreateTestSuite=async o=>{E.debug(`Attempting to retrieve or create test suite with name: "${o.name}"`);let{TestSuite:e}=await import('./suite-MVHCQXFQ.js');return e.getOrCreate(this,o)};this.deleteTestSuite=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Deleting test suite with name "${o}"`);let{TestSuite:i}=await import('./suite-MVHCQXFQ.js');await i.delete(this,o,t);};this.getTestSuites=async(o=1e3,e)=>{var i;let t=this.resolveProjectName(e);E.debug(`Getting all test suites (limit: ${o})`);try{await this.datasetBatchQueue.flush();let a=await this.resolveProjectId(t),{TestSuite:s}=await import('./suite-MVHCQXFQ.js'),c=[],l=1,m=100;for(;c.length<o;){let d=(i=(await this.api.datasets.findDatasets({page:l,size:m,...a&&{projectId:a}})).content)!=null?i:[];if(d.length===0)break;for(let f of d){if(c.length>=o)break;f.type===Bf.EvaluationSuite&&c.push(new s(new hr({...f,projectName:t},this),this));}l++;}return E.info(`Retrieved ${c.length} test suites`),c}catch(a){throw E.error("Failed to retrieve test suites",{error:a}),new Error("Failed to retrieve test suites")}};this.createTracesAnnotationQueue=async o=>this.createAnnotationQueueInternal(o,Zr);this.createThreadsAnnotationQueue=async o=>this.createAnnotationQueueInternal(o,qr);this.getTracesAnnotationQueue=async o=>this.fetchAnnotationQueueById(o,"trace",Zr);this.getThreadsAnnotationQueue=async o=>this.fetchAnnotationQueueById(o,"thread",qr);this.getTracesAnnotationQueues=async o=>(await this.getAnnotationQueuesByScope("trace",o)).map(t=>new Zr(t,this));this.getThreadsAnnotationQueues=async o=>(await this.getAnnotationQueuesByScope("thread",o)).map(t=>new qr(t,this));this.deleteTracesAnnotationQueue=async o=>this.deleteAnnotationQueueById(o,"traces");this.deleteThreadsAnnotationQueue=async o=>this.deleteAnnotationQueueById(o,"threads");this.createExperiment=async({datasetName:o,name:e,experimentConfig:t,prompts:i,type:a=Jf.Regular,optimizationId:s,datasetVersionId:c,evaluationMethod:l,tags:m,projectName:u})=>{if(E.debug(`Creating experiment for dataset "${o}"`),!o)throw new Error("Dataset name is required to create an experiment");let[d,f]=vZ(t,i),g=this.resolveProjectName(u),h=ue(),y=new Ir({id:h,name:e,datasetName:o,prompts:i,tags:m,projectName:g},this);try{return await this.api.experiments.createExperiment({id:h,datasetName:o,name:e,metadata:d,promptVersions:f,type:a,optimizationId:s,datasetVersionId:c,tags:m,evaluationMethod:l,projectName:g}),E.debug("Experiment created with id:",h),y}catch(w){throw E.error(`Failed to create experiment for dataset "${o}"`,{error:w}),new Error(`Error creating experiment: ${w}`)}};this.updateExperiment=async(o,e)=>{if(!o)throw new Error("id is required to update an experiment");let{name:t,experimentConfig:i}=e;if(!t&&!i)throw new Error("At least one of 'name' or 'experimentConfig' must be provided to update an experiment");E.debug(`Updating experiment with ID "${o}"`);let a={};t!==void 0&&(a.name=t),i!==void 0&&(a.metadata=i);try{await this.api.experiments.updateExperiment(o,{body:a});}catch(s){throw E.error(`Failed to update experiment with ID "${o}"`,{error:s}),s}};this.getExperimentById=async o=>{var e,t;E.debug(`Getting experiment with ID "${o}"`);try{let i=await this.api.experiments.getExperimentById(o);return new Ir({id:i.id,name:i.name,datasetName:(e=i.datasetName)!=null?e:void 0,projectName:(t=i.projectName)!=null?t:void 0},this)}catch(i){throw i instanceof k&&i.statusCode===404?new ep(`No experiment found with ID '${o}'`):(E.error(`Failed to get experiment with ID "${o}"`,{error:i}),i)}};this.getExperimentsByName=async(o,e)=>{let t=this.resolveProjectName(e);E.debug(`Getting experiments with name "${o}"`);try{let i=await this.api.experiments.streamExperiments({name:o,projectName:t});return (await Ne(i,G.ExperimentPublic)).map(s=>{var c,l;return new Ir({id:s.id,name:s.name,datasetName:(c=s.datasetName)!=null?c:void 0,projectName:(l=s.projectName)!=null?l:void 0},this)})}catch(i){throw E.error(`Failed to get experiments with name "${o}"`,{error:i}),i}};this.getExperiment=async(o,e)=>{E.debug(`Getting experiment with name "${o}"`);let t=await this.getExperimentsByName(o,e);if(t.length===0)throw new ep(o);return t[0]};this.getDatasetExperiments=async(o,e=100,t)=>{E.debug(`Getting experiments for dataset "${o}"`);let i=await this.getDataset(o,t);try{return await this.findExperimentsByDatasetId(i.id,e,a=>{var s;return new Ir({id:a.id,name:a.name,datasetName:(s=a.datasetName)!=null?s:void 0},this)})}catch(a){throw E.error(`Failed to get experiments for dataset "${o}"`,{error:a}),a}};this.getTestSuiteExperiments=async(o,e=100,t)=>{E.debug(`Getting experiments for test suite "${o}"`);let i=await this.getDataset(o,t);try{return await this.findExperimentsByDatasetId(i.id,e,a=>{var s;return new qd({id:a.id,name:a.name,datasetName:(s=a.datasetName)!=null?s:void 0,passRate:a.passRate,passedCount:a.passedCount,totalCount:a.totalCount,assertionScores:a.assertionScores},this)})}catch(a){throw E.error(`Failed to get experiments for test suite "${o}"`,{error:a}),a}};this.findExperimentsByDatasetId=async(o,e,t)=>{var c;let i=Math.min(100,e),a=[],s=1;for(;a.length<e;){let l=await this.api.experiments.findExperiments({page:s,size:i,datasetId:o}),m=(c=l==null?void 0:l.content)!=null?c:[];if(m.length===0)break;let u=e-a.length,d=Math.min(m.length,u);for(let f=0;f<d;f++)a.push(t(m[f]));if(d<m.length)break;s+=1;}return a};this.deleteExperiment=async o=>{E.debug(`Deleting experiment with ID "${o}"`);try{await this.api.experiments.deleteExperimentsById({ids:[o]});}catch(e){throw E.error(`Failed to delete experiment with ID "${o}"`,{error:e}),e}};this.createPromptInternal=async(o,e,t,i,a,s,c,l,m)=>{var u;E.debug(`Creating ${l}`,{name:o});try{let d=await VZ(this.api.prompts,o,this.api.requestOptions);a(d);let f=(u=i.type)!=null?u:Fe.MUSTACHE,g=JZ({prompt:e,metadata:i.metadata},d,f),h;if(g?(E.debug(`Creating new ${l} version`,{name:o}),h=await this.api.prompts.createPromptVersion({name:o,version:{template:e,metadata:i.metadata,type:f},templateStructure:t,projectName:m},this.api.requestOptions)):(E.debug(`Returning existing ${l} version`,{name:o}),h=d),!h.promptId)throw new Error("Invalid API response: missing promptId");let y=await this.api.prompts.getPromptById(h.promptId,{},this.api.requestOptions),w=s(y,h);return E.debug(`${l} created`,{name:o}),i.description||i.tags?await w.updateProperties({description:i.description,tags:i.tags}):w}catch(d){if(d instanceof k||d instanceof Mr)return E.warn(`Failed to sync ${l} '${o}' with the backend. The prompt will work locally but is not persisted on the server. You can retry by calling .syncWithBackend().`,{error:d}),c();throw E.error(`Failed to create ${l}`,{name:o,error:d}),d}};this.createPrompt=async o=>{let e=this.resolveProjectName(o.projectName);return this.createPromptInternal(o.name,o.prompt,Re.Text,o,()=>{},(t,i)=>pr.fromApiResponse(t,i,this,e),()=>{var t;return new pr({name:o.name,prompt:o.prompt,metadata:o.metadata,type:(t=o.type)!=null?t:Fe.MUSTACHE,description:o.description,tags:o.tags,projectName:e,synced:false},this)},"prompt",e)};this.createChatPrompt=async o=>{let e=this.resolveProjectName(o.projectName),t=JSON.stringify(o.messages);return this.createPromptInternal(o.name,t,Re.Chat,o,i=>{if(i&&i.templateStructure&&i.templateStructure!==Re.Chat)throw new Vt(o.name,i.templateStructure,Re.Chat)},(i,a)=>cr.fromApiResponse(i,a,this,e),()=>{var i;return new cr({name:o.name,messages:structuredClone(o.messages),metadata:o.metadata,type:(i=o.type)!=null?i:Fe.MUSTACHE,description:o.description,tags:o.tags,projectName:e,synced:false},this)},"chat prompt",e)};this.getPrompt=async o=>this.getPromptWithCache(o,Re.Text,(e,t,i)=>pr.fromApiResponse(e,t,this,i),"prompt");this.getChatPrompt=async o=>this.getPromptWithCache(o,Re.Chat,(e,t,i)=>cr.fromApiResponse(e,t,this,i),"chat prompt");this.getPromptWithCache=async(o,e,t,i)=>{if(o.commit&&o.version)throw new Error("Provide either `commit` or `version`, not both. Prefer `version` \u2014 `commit` is deprecated.");if(E.debug(`Getting ${i}`,o),o.commit&&o.environment)throw new Error("'commit' and 'environment' are mutually exclusive; pass at most one.");if(o.version&&o.environment)throw new Error("'version' and 'environment' are mutually exclusive; pass at most one.");let a=this.resolveProjectName(o.projectName),s=async u=>{var d;try{let f,g;if(u){if(g=await this.api.prompts.getPromptVersionById(u,{},this.api.requestOptions),!g.promptId)return null;f=await this.api.prompts.getPromptById(g.promptId,{},this.api.requestOptions);}else {let y;try{y=await this.getProjectIdByName(a);}catch{}if(f=(d=(await this.api.prompts.getPrompts({filters:JSON.stringify([{field:"name",operator:"=",value:o.name}]),size:1,...y&&{projectId:y}},this.api.requestOptions)).content)==null?void 0:d[0],!f)return E.debug(`${i.charAt(0).toUpperCase()+i.slice(1)} not found`,{name:o.name}),null;let A={name:o.name,projectName:a,...o.commit?{commit:o.commit}:{},...o.version?{versionNumber:o.version}:{},...o.environment?{environment:o.environment}:{}};g=await this.api.prompts.retrievePromptVersion(A,this.api.requestOptions);}let h=g.templateStructure;if(e===Re.Text){if(h&&h!==Re.Text)throw new Vt(o.name,h,Re.Text)}else if(!h||h!==Re.Chat)throw new Vt(o.name,h!=null?h:"undefined",Re.Chat);return t(f,g,a)}catch(f){if(f instanceof k&&f.statusCode===404)return null;throw E.error(`Failed to get ${i}`,{name:o.name,error:f}),f}},c=await su(o.name,o.commit,a,e,()=>s(),this.config.promptCacheTtlSeconds,void 0,o.version,o.environment),l=c!=null&&c.id?KZ(c.id):null,m=l?await su(o.name,o.commit,a,e,()=>s(l),this.config.promptCacheTtlSeconds,l,o.version):c;if(m!==null){let u=Qt();u&&(Er.promptAlreadyInjected(u.trace.data.metadata,m.id,m.commit)||u.trace.update({prompts:[m],appendPrompts:true}),Er.promptAlreadyInjected(u.span.data.metadata,m.id,m.commit)||u.span.update({prompts:[m],appendPrompts:true}));}return m};this.searchPrompts=async o=>{var e;E.debug("Searching prompts",{filterString:o});try{let t;if(o){let l=yr.forPrompts(o).getFilterExpressions();t=l?JSON.stringify(l):void 0;}let a=(e=(await this.api.prompts.getPrompts({filters:t,size:1e3},this.api.requestOptions)).content)!=null?e:[];return (await Promise.all(a.map(async c=>{if(!c.name)return null;try{let l=await this.api.prompts.retrievePromptVersion({name:c.name},this.api.requestOptions),m=l.templateStructure,u=this.resolveProjectName();return !m||m===Re.Text?pr.fromApiResponse(c,l,this,u):m===Re.Chat?cr.fromApiResponse(c,l,this,u):null}catch(l){return E.debug("Failed to get version for prompt",{name:c.name,error:l}),null}}))).filter(c=>c!==null)}catch(t){throw E.error("Failed to search prompts",{error:t}),t}};this.deletePrompts=async o=>{E.debug("Deleting prompts in batch",{count:o.length});try{await this.api.prompts.deletePromptsBatch({ids:o},this.api.requestOptions),nu().evictByIds(o),E.info("Successfully deleted prompts",{count:o.length});}catch(e){throw E.error("Failed to delete prompts",{count:o.length,error:e}),e}};this.setPromptEnvironments=async o=>{let e;try{e=await this.api.prompts.retrievePromptVersion({name:o.promptName,versionNumber:o.version,projectName:this.resolveProjectName(o.projectName)},this.api.requestOptions);}catch(i){throw i instanceof k&&i.statusCode===404?o.version!==void 0?new Lo(`No version '${o.version}' found for prompt '${o.promptName}'.`):new Lo(`No prompt found with name '${o.promptName}'.`):i}let t=Array.from(new Set(o.environments));try{await this.api.prompts.setPromptVersionEnvironment(e.id,{environments:t},this.api.requestOptions);}catch(i){throw i instanceof k&&(i.statusCode===404||i.statusCode===409)?new ru(`One or more environments in [${t.join(", ")}] are not registered in this workspace.`):i}nu().invalidateForPrompt(o.promptName,this.resolveProjectName(o.projectName));};this.searchTraces=async o=>{let{exclude:e,...t}=o!=null?o:{};return this.executeSearch("traces",t,lu,(i,a,s,c,l)=>c7(i,a,s,c,l,e))};this.searchThreads=async o=>this.executeSearch("threads",o!=null?o:{},mu,m7);this.searchSpans=async o=>{let{exclude:e,...t}=o!=null?o:{};return this.executeSearch("spans",t,u7,(i,a,s,c,l)=>d7(i,a,s,c,l,e))};this.createEnvironment=async(o,e)=>{let t=ue();try{await this.api.environments.createEnvironment({id:t,name:o,description:e==null?void 0:e.description,color:e==null?void 0:e.color});}catch(i){throw i instanceof k&&i.statusCode===409?new Hd(o):i}return this.api.environments.getEnvironmentById(t)};this.getEnvironments=async()=>{var e;return (e=(await this.api.environments.findEnvironments()).content)!=null?e:[]};this.updateEnvironment=async(o,e)=>{if((e==null?void 0:e.color)!==void 0&&Su.BUILTIN_ENVIRONMENT_NAMES.has(o))throw new Gd(`Cannot change the colour of the built-in environment '${o}'. Colour updates are not allowed for 'production', 'staging', or 'development'.`);let t=await this._findEnvironmentByName(o,true);return await this.api.environments.updateEnvironment(t.id,{description:e==null?void 0:e.description,color:e==null?void 0:e.color}),this.api.environments.getEnvironmentById(t.id)};this.deleteEnvironment=async o=>{let e=await this._findEnvironmentByName(o,false);e&&await this.api.environments.deleteEnvironmentsBatch({ids:[e.id]});};this._findEnvironmentByName=async(o,e)=>{let i=(await this.getEnvironments()).find(a=>a.name===o);if(!i&&e)throw new Error(`No environment found with name "${o}".`);return i};this.flush=async o=>{var t;let e=(t=o==null?void 0:o.silent)!=null?t:false;E.debug("Starting flush operation");try{await this.traceBatchQueue.flush(),await this.spanBatchQueue.flush(),await this.traceFeedbackScoresBatchQueue.flush(),await this.spanFeedbackScoresBatchQueue.flush(),await this.traceAssertionResultsBatchQueue.flush(),await this.datasetBatchQueue.flush(),e||E.info("Successfully flushed all data to Opik");}catch(i){E.error("Error during flush operation:",{error:i instanceof Error?i.message:i});}};this.createConfig=async(o,e)=>{var l,m;let t=(l=e==null?void 0:e.projectName)!=null?l:this.config.projectName;this._validatePromptProjects(o,t);let i=new Ot(t,this),a=NU(o),s=await i.getBlueprint(),c;if(s)c=await i.updateBlueprint({values:a,description:e==null?void 0:e.description});else try{c=await i.createBlueprint({values:a,description:e==null?void 0:e.description});}catch(u){if(u instanceof k&&u.statusCode===409)c=await i.updateBlueprint({values:a,description:e==null?void 0:e.description});else throw u}return (m=c.name)!=null?m:c.id};this.setConfigEnv=async o=>{var s;let e=(s=o.projectName)!=null?s:this.config.projectName,i=await new Ot(e,this).getBlueprint({name:o.version});if(!i)throw new Cr(`No config version "${o.version}" found in project "${e}".`);let a=await this.api.projects.retrieveProject({name:e});if(!(a!=null&&a.id))throw new Error(`Project "${e}" not found`);await this.api.agentConfigs.createOrUpdateEnvs({projectId:a.id,envs:[{envName:o.env,blueprintId:i.id}]});};this.updatePromptVersionTags=async(o,e)=>{var t;E.debug("Updating prompt version tags",{count:o.length,options:e});try{await this.api.prompts.updatePromptVersions({ids:o,update:{tags:(t=e==null?void 0:e.tags)!=null?t:void 0},mergeTags:e==null?void 0:e.mergeTags},this.api.requestOptions),E.debug("Successfully updated prompt version tags",{count:o.length});}catch(i){throw E.error("Failed to update prompt version tags",{count:o.length,error:i}),i}};var a;E.debug("Initializing OpikClient with config:",o),this.config=vp(o);let e={apiKey:this.config.apiKey,environment:this.config.apiUrl,workspaceName:this.config.workspaceName};o!=null&&o.headers&&(E.debug("Initializing OpikClient with additional headers:",o==null?void 0:o.headers),e.requestOptions={headers:o==null?void 0:o.headers}),this.api=new Dd(e);let t=this.config.holdUntilFlush?1440*60*1e3:this.config.batchDelayMs,i=this.config.isAttachmentExtractionActive?{minSizeBytes:(a=this.config.minBase64EmbeddedAttachmentSize)!=null?a:Ke.minBase64EmbeddedAttachmentSize,apiUrl:this.config.apiUrl,workspaceName:this.config.workspaceName,apiKey:this.config.apiKey||void 0,extraHeaders:o==null?void 0:o.headers}:void 0;this.spanBatchQueue=new jd(this.api,t,this.config.maxPayloadSizeMb,i),this.traceBatchQueue=new Cd(this.api,t,this.config.maxPayloadSizeMb,i),this.spanFeedbackScoresBatchQueue=new Ed(this.api,t),this.traceFeedbackScoresBatchQueue=new Id(this.api,t),this.traceAssertionResultsBatchQueue=new Sd(this.api,t,"TRACE"),this.datasetBatchQueue=new Ud(this.api,t),ku.push(this);}resolveProjectName(o){return o!==void 0?o:(!eN&&this.config.projectName===Ke.projectName&&(eN=true,E.warn(`No project name configured. Traces are being logged to "Default Project".
23
23
  Set OPIK_PROJECT_NAME environment variable or pass projectName to the Opik client
24
24
  to log to a specific project.
25
25
  See https://www.comet.com/docs/opik/tracing/advanced/sdk_configuration`)),this.config.projectName)}async getProjectIdByName(o){let e=await this.api.projects.retrieveProject({name:o});if(!(e!=null&&e.id))throw new Error(`Project "${o}" not found`);return e.id}async resolveProjectId(o){if(o!==void 0)return this.getProjectIdByName(o)}async createAnnotationQueueInternal(o,e){let{name:t,projectName:i,description:a,instructions:s,commentsEnabled:c,feedbackDefinitionNames:l}=o,m=e.SCOPE;E.debug(`Creating ${m} annotation queue "${t}"`);let u=i!=null?i:this.config.projectName;try{let d=await this.getProjectIdByName(u),f=ue();return await this.api.annotationQueues.createAnnotationQueue({id:f,projectId:d,name:t,scope:m,description:a,instructions:s,commentsEnabled:c,feedbackDefinitionNames:l}),E.debug(`Created ${m} annotation queue "${t}" with ID "${f}"`),new e({id:f,name:t,projectId:d,scope:m,description:a,instructions:s,commentsEnabled:c,feedbackDefinitionNames:l},this)}catch(d){throw E.error(`Failed to create ${m} annotation queue "${t}"`,{error:d}),d}}async fetchAnnotationQueueById(o,e,t){E.debug(`Getting ${e} annotation queue with ID "${o}"`);try{let i=await this.api.annotationQueues.getAnnotationQueueById(o);if(i.scope!==e)throw new Error(`Annotation queue "${o}" is not a ${e} queue (scope: ${i.scope})`);return new t(i,this)}catch(i){if(i instanceof k){if(i.statusCode===404)throw new Ld(o);E.error(`Failed to get ${e} annotation queue with ID "${o}"`,{error:i});}throw i}}async getAnnotationQueuesByScope(o,e){let{projectName:t,maxResults:i=1e3}=e!=null?e:{};E.debug(`Getting ${o} annotation queues (project: ${t!=null?t:"all"}, limit: ${i})`);try{let a;if(t){let l=await this.getProjectIdByName(t);a=JSON.stringify([{field:"project_id",operator:"=",value:l},{field:"scope",operator:"=",value:o}]);}else a=JSON.stringify([{field:"scope",operator:"=",value:o}]);let c=(await this.api.annotationQueues.findAnnotationQueues({size:i,filters:a})).content||[];return E.info(`Retrieved ${c.length} ${o} annotation queues`),c}catch(a){throw E.error(`Failed to retrieve ${o} annotation queues`,{error:a}),a}}async deleteAnnotationQueueById(o,e){E.debug(`Deleting ${e} annotation queue with ID "${o}"`);try{await this.api.annotationQueues.deleteAnnotationQueueBatch({ids:[o]}),E.debug(`Successfully deleted ${e} annotation queue with ID "${o}"`);}catch(t){throw E.error(`Failed to delete ${e} annotation queue with ID "${o}"`,{error:t}),t}}async executeSearch(o,e,t,i){let{projectName:a,filterString:s,maxResults:c=1e3,truncate:l=true,waitForAtLeast:m,waitForTimeout:u=60}=e;E.debug(`Searching ${o}`,{projectName:a,filterString:s,maxResults:c,truncate:l,waitForAtLeast:m,waitForTimeout:u});let d=t(s),f=a!=null?a:this.config.projectName,g=()=>i(this.api,f,d,c,l);if(m===void 0)return await g();let h=await l7(g,m,u*1e3,5e3);if(h.length<m)throw new Md(`Timeout after ${u} seconds: expected ${m} ${o}, but only ${h.length} were found.`);return h}logFeedbackScores(o,e){var t;for(let i of o)e.create({...i,projectName:(t=i.projectName)!=null?t:this.config.projectName,source:Hf.Sdk});}logTracesFeedbackScores(o){this.logFeedbackScores(o,this.traceFeedbackScoresBatchQueue);}logSpansFeedbackScores(o){this.logFeedbackScores(o,this.spanFeedbackScoresBatchQueue);}getOrCreateConfig(o){return this._getOrCreateConfigImpl(o)}_validatePromptProjects(o,e){for(let[t,i]of Object.entries(o))if(i instanceof sr&&i.projectName!==void 0&&i.projectName!==e)throw new Wo(`Field "${t}": prompt project "${i.projectName}" does not match config project "${e}". All prompts referenced in a config must belong to the same project as the config.`)}async _allPromptsSynced(o){let e=Object.values(o).filter(a=>a instanceof sr&&!a.synced);if(e.length===0)return true;let t=Symbol(),i;try{if(await Promise.race([Promise.allSettled(e.map(s=>s.ready())).then(()=>{}),new Promise(s=>{i=setTimeout(()=>s(t),Vne);})])===t)return E.debug("Timed out waiting for prompt sync before creating config."),!1}finally{clearTimeout(i);}return e.every(a=>a.synced)}_makeFallbackConfig(o,e){return $U({values:o,fieldNames:new Set(Object.keys(o)),blueprintId:void 0,blueprintVersion:void 0,isFallback:true,maskId:e})}async _fetchBlueprintFromBackend(o,e){let{blueprintName:t,isLatest:i,hasNamedVersion:a,namedVersion:s,effectiveEnv:c,maskId:l,projectName:m,effectiveVersion:u,fallback:d}=e,f=V7(m,c,l!=null?l:null,u);if(!f.isStale())return f.getBlueprint();let g=null;try{t?g=await o.getBlueprint({name:t,maskId:l}):i?g=await o.getBlueprint({maskId:l}):a?g=await o.getBlueprint({name:s,maskId:l}):g=await o.getBlueprint({env:c,maskId:l});}catch(y){if(d!==void 0)return E.debug("Failed to fetch config from backend, using fallback",{error:y}),this._makeFallbackConfig(d,l);throw y}let h=l===void 0&&!a?i?()=>o.getBlueprint({maskId:void 0}):()=>o.getBlueprint({env:c,maskId:void 0}):null;return qU(m,c,l!=null?l:null,g,h,u),g}async _resolveNullBlueprint(o,e){let{projectName:t,effectiveEnv:i,effectiveVersion:a,maskId:s,hasNamedVersion:c,hasExplicitEnv:l,isExplicitBlueprintFromContext:m,isLatest:u,fallback:d}=e;if(c||l||m)throw new Cr(`No config found for project "${t}" with the specified selector`);if(!u){let h=null;try{h=await o.getBlueprint({maskId:void 0});}catch(y){if(d!==void 0)return E.debug("Failed to probe project-wide config, using fallback",{error:y}),this._makeFallbackConfig(d,s);throw y}if(h!==null)throw new Cr(`No config tagged with env="prod" in project "${t}", but other configs exist. Use setConfigEnv() to tag a version, or pass an explicit env/version.`)}if(d===void 0)throw new Cr(`No config found in project "${t}". Pass a fallback to auto-create one.`);if(this._validatePromptProjects(d,t),!await this._allPromptsSynced(d))return this._makeFallbackConfig(d,s);let g;try{g=await o.createBlueprint({values:NU(d)});}catch(h){if(h instanceof k&&h.statusCode===409){let y=await o.getBlueprint({maskId:void 0});if(!y)throw new Cr(`Failed to create or fetch config in project "${t}".`);g=y;}else throw h}return qU(t,i,s!=null?s:null,g,null,a),g}_buildConfigFromBlueprint(o,e,t){var s;let i=Object.fromEntries(o.keys().map(c=>[c,o.getRawEntry(c)]));if(e!==void 0){let c=Object.keys(e).filter(l=>i[l]===void 0);if(c.length>0){let l=(s=o.name)!=null?s:o.id;throw new Wo(`Config version "${l}" is missing expected field(s): ${c.join(", ")}. The retrieved version does not contain all fields declared in the fallback.`)}}let a=y7(i,o.values,e!==void 0?Object.keys(e):void 0);return $U({values:a,fieldNames:new Set(Object.keys(e!=null?e:a)),blueprintId:o.id,blueprintVersion:o.name,isFallback:false,maskId:t})}async _getOrCreateConfigImpl(o){var h,y,w,A;if(!Kt.getStore())throw new Error("getOrCreateConfig() must be called inside a track() function");if((o==null?void 0:o.version)!==void 0&&(o==null?void 0:o.env)!==void 0)throw new Error("Only one of 'version' or 'env' may be specified in getOrCreateConfig().");let e=o==null?void 0:o.fallback,t=(h=o==null?void 0:o.projectName)!=null?h:this.config.projectName,i=(y=MU())!=null?y:void 0,a=(w=P7())!=null?w:void 0,s=a!==void 0,c=new Ot(t,this),l=(o==null?void 0:o.version)==="latest",m=(o==null?void 0:o.version)!==void 0&&!l,u=o!=null&&o.version?null:(A=o==null?void 0:o.env)!=null?A:"prod",d=a!=null?a:m?o.version:null,f=await this._fetchBlueprintFromBackend(c,{blueprintName:a,isLatest:l,hasNamedVersion:m,namedVersion:o==null?void 0:o.version,effectiveEnv:u,maskId:i,projectName:t,effectiveVersion:d,fallback:e});if(f!==null&&!(f instanceof br))return f;let g=f;if(!g){let C=await this._resolveNullBlueprint(c,{projectName:t,effectiveEnv:u,effectiveVersion:d,maskId:i,hasNamedVersion:m,hasExplicitEnv:(o==null?void 0:o.env)!==void 0,isExplicitBlueprintFromContext:s,isLatest:l,fallback:e});if(!(C instanceof br))return C;g=C;}return this._buildConfigFromBlueprint(g,e,i)}};Su.BUILTIN_ENVIRONMENT_NAMES=new Set(["production","staging","development"]);Rr=Su;});var vu,Tu=n(()=>{vu="__opik_task_error__";});function H7(p,o){if(!o||typeof o!="object")throw new Error("Arguments must be an object");let e=p.validationSchema,i=e.extend(Object.fromEntries(Object.entries(e.shape).map(([a,s])=>[a,s.refine(c=>c!==void 0,{message:`${a} cannot be undefined`,path:[a]})]))).safeParse(o);if(!i.success){let a=i.error.issues.map(c=>c.path[0]).filter(Boolean),s=[...new Set(a)];throw new Error(Jne(p,o,s))}}function Jne(p,o,e){let t=Object.keys(o),i=e.filter(a=>!(a in o));return `Metric '${p.name}' is skipped, missing required arguments: ${i.join(", ")}. Available arguments: ${t.join(", ")}.`}var G7=n(()=>{});var ju,O7=n(()=>{re();Tu();ju=class{static calculateScoreStats(o){var i;let e=new Map;for(let a of o!=null?o:[])for(let s of (i=a==null?void 0:a.scoreResults)!=null?i:[]){if(!s)continue;let c=s.scoringFailed&&s.name===vu,l=!s.scoringFailed&&typeof s.value=="number";if(c||!s.scoringFailed&&!l)continue;let m=e.get(s.name)||{sum:0,count:0,failed:0};l?(m.sum+=s.value,m.count+=1):m.failed+=1,e.set(s.name,m);}let t=new Map;return e.forEach((a,s)=>{t.set(s,{average:a.count>0?a.sum/a.count:void 0,failed:a.failed});}),t}static formatScore(o){return o.toFixed(4)}static formatTime(o){let e=Math.floor(o/3600),t=Math.floor(o%3600/60),i=Math.floor(o%60);return `${e.toString().padStart(2,"0")}:${t.toString().padStart(2,"0")}:${i.toString().padStart(2,"0")}`}static async generateResultTable(o,e,t,i,a){if(o.length===0){E.info(`
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import {spawnSync}from'child_process';import {chalkStderr}from'chalk';import {createHash,randomBytes}from'crypto';import L,{accessSync,constants}from'fs';import f from'os';import i from'path';import U from'ini';var b="https://stats.comet.com/notify/event/",T="opik_typescript_sdk__configuration__cli",S=3e3,l="default",p="npx",h="OPIK_CLI_LAUNCHER",E="OPIK_CLI_SESSION_ID",D=9,g="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";function V(){return Array.from(randomBytes(D),n=>g[n%g.length]).join("")}var v="2.2.96";function K(n){return `${T}__${n}`}function M(n=process.env){let e=n.OPIK_ANALYTICS_ENABLE;return e===void 0?true:!["0","false","no"].includes(e.trim().toLowerCase())}var H=/^--[a-z][a-z0-9-]*$/,$=/^-[A-Za-z]$/,F=/^[a-z][a-z0-9-]*$/,j=3;function B(n){let e=[],t=[],o=false;for(let r of n){if(r==="--")break;if(r.startsWith("-")){o=true;let s=r.split("=")[0];(H.test(s)||$.test(s))&&t.push(s);continue}!o&&e.length<j&&F.test(r)&&e.push(r);}return {command:e.join(" "),flags:[...new Set(t)].join(",")}}function J(n=process.env,e=f.homedir()){var r;let t=((r=I(n.OPIK_WORKSPACE))!=null?r:z(n,e)).trim();if(t!==""&&t!==l)return t;let o=createHash("sha256").update(f.hostname()+W()).digest("hex");return `${l}_${o}`}function N(n,e=process.env){var m;let t=M(e),o=(m=e.OPIK_ANALYTICS_URL)!=null?m:b,r=t?J(e):"",{command:s,flags:c}=B(n),_=V(),u=[];return {sessionId:_,track(d,w={}){if(!t||o==="")return;let k={anonymous_id:r,event_type:K(d),event_properties:{session_id:_,launcher:p,sdk_version:v,command:s,flags:c,node_version:process.versions.node,os:process.platform,arch:process.arch,ci:!!e.CI,...w}};u.push(G(o,k));},async flush(){u.length!==0&&await Promise.race([Promise.allSettled(u),new Promise(d=>setTimeout(d,S).unref())]);}}}async function G(n,e){try{await fetch(n,{method:"POST",headers:{"Content-Type":"application/json","User-Agent":`opik-typescript-sdk/${v}`},body:JSON.stringify(e),signal:AbortSignal.timeout(S)});}catch{}}function z(n,e){var r;let t=I(n.OPIK_CONFIG_PATH),o=t===void 0?i.join(e,".opik.config"):t.trim().replace(/^~(?=$|\/|\\)/,e);try{let s=U.parse(L.readFileSync(o,"utf8")),c=(r=s==null?void 0:s.opik)==null?void 0:r.workspace;return typeof c=="string"?c:""}catch{return ""}}function I(n){return n===void 0||n.trim()===""?void 0:n}function W(){try{return f.userInfo().username}catch{return ""}}var Q=1e4;function A(n=process.platform){return n==="win32"?"uv.exe":"uv"}function nn(n=process.env,e=process.platform,t=f.homedir()){let o=[y(n.XDG_BIN_HOME),y(n.CARGO_HOME,"bin"),i.join(t,".local","bin"),i.join(t,".cargo","bin")].filter(s=>s!==void 0),r=A(e);return [...new Set(o)].map(s=>i.join(s,r))}function y(n,...e){let t=(n!=null?n:"").trim();if(!(t===""||!i.isAbsolute(t)))return i.join(t,...e)}function P(){let n=A();if(O(n))return {path:n,source:"path"};for(let e of nn())if(en(e)&&O(e))return {path:e,source:"install-dir"}}function en(n){try{return accessSync(n,constants.X_OK),!0}catch{return false}}function O(n){let e=spawnSync(n,["--version"],{stdio:"ignore",timeout:Q});return e.error===void 0&&e.status===0}var rn="https://www.comet.com/docs/opik/mcp-server#install-uv-if-you-dont-have-it";async function on(){var r;let n=process.argv.slice(2),e=N(n);e.track("invoked");let t=P();if(e.track("uv_detected",{uv_found:t!==void 0,uv_source:t==null?void 0:t.source}),t===void 0)return process.stderr.write(`
2
+ import {spawnSync}from'child_process';import {chalkStderr}from'chalk';import {createHash,randomBytes}from'crypto';import L,{accessSync,constants}from'fs';import f from'os';import i from'path';import U from'ini';var b="https://stats.comet.com/notify/event/",T="opik_typescript_sdk__configuration__cli",S=3e3,l="default",p="npx",h="OPIK_CLI_LAUNCHER",E="OPIK_CLI_SESSION_ID",D=9,g="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";function V(){return Array.from(randomBytes(D),n=>g[n%g.length]).join("")}var v="2.2.98";function K(n){return `${T}__${n}`}function M(n=process.env){let e=n.OPIK_ANALYTICS_ENABLE;return e===void 0?true:!["0","false","no"].includes(e.trim().toLowerCase())}var H=/^--[a-z][a-z0-9-]*$/,$=/^-[A-Za-z]$/,F=/^[a-z][a-z0-9-]*$/,j=3;function B(n){let e=[],t=[],o=false;for(let r of n){if(r==="--")break;if(r.startsWith("-")){o=true;let s=r.split("=")[0];(H.test(s)||$.test(s))&&t.push(s);continue}!o&&e.length<j&&F.test(r)&&e.push(r);}return {command:e.join(" "),flags:[...new Set(t)].join(",")}}function J(n=process.env,e=f.homedir()){var r;let t=((r=I(n.OPIK_WORKSPACE))!=null?r:z(n,e)).trim();if(t!==""&&t!==l)return t;let o=createHash("sha256").update(f.hostname()+W()).digest("hex");return `${l}_${o}`}function N(n,e=process.env){var m;let t=M(e),o=(m=e.OPIK_ANALYTICS_URL)!=null?m:b,r=t?J(e):"",{command:s,flags:c}=B(n),_=V(),u=[];return {sessionId:_,track(d,w={}){if(!t||o==="")return;let k={anonymous_id:r,event_type:K(d),event_properties:{session_id:_,launcher:p,sdk_version:v,command:s,flags:c,node_version:process.versions.node,os:process.platform,arch:process.arch,ci:!!e.CI,...w}};u.push(G(o,k));},async flush(){u.length!==0&&await Promise.race([Promise.allSettled(u),new Promise(d=>setTimeout(d,S).unref())]);}}}async function G(n,e){try{await fetch(n,{method:"POST",headers:{"Content-Type":"application/json","User-Agent":`opik-typescript-sdk/${v}`},body:JSON.stringify(e),signal:AbortSignal.timeout(S)});}catch{}}function z(n,e){var r;let t=I(n.OPIK_CONFIG_PATH),o=t===void 0?i.join(e,".opik.config"):t.trim().replace(/^~(?=$|\/|\\)/,e);try{let s=U.parse(L.readFileSync(o,"utf8")),c=(r=s==null?void 0:s.opik)==null?void 0:r.workspace;return typeof c=="string"?c:""}catch{return ""}}function I(n){return n===void 0||n.trim()===""?void 0:n}function W(){try{return f.userInfo().username}catch{return ""}}var Q=1e4;function A(n=process.platform){return n==="win32"?"uv.exe":"uv"}function nn(n=process.env,e=process.platform,t=f.homedir()){let o=[y(n.XDG_BIN_HOME),y(n.CARGO_HOME,"bin"),i.join(t,".local","bin"),i.join(t,".cargo","bin")].filter(s=>s!==void 0),r=A(e);return [...new Set(o)].map(s=>i.join(s,r))}function y(n,...e){let t=(n!=null?n:"").trim();if(!(t===""||!i.isAbsolute(t)))return i.join(t,...e)}function P(){let n=A();if(O(n))return {path:n,source:"path"};for(let e of nn())if(en(e)&&O(e))return {path:e,source:"install-dir"}}function en(n){try{return accessSync(n,constants.X_OK),!0}catch{return false}}function O(n){let e=spawnSync(n,["--version"],{stdio:"ignore",timeout:Q});return e.error===void 0&&e.status===0}var rn="https://www.comet.com/docs/opik/mcp-server#install-uv-if-you-dont-have-it";async function on(){var r;let n=process.argv.slice(2),e=N(n);e.track("invoked");let t=P();if(e.track("uv_detected",{uv_found:t!==void 0,uv_source:t==null?void 0:t.source}),t===void 0)return process.stderr.write(`
3
3
  ${chalkStderr.red("\u2717")} ${chalkStderr.bold("The Opik MCP configuration requires uv, which is not installed on this machine.")}
4
4
 
5
5
  How to install uv: ${chalkStderr.cyan.underline(rn)}
package/dist/index.cjs CHANGED
@@ -19,7 +19,7 @@ ${s}`),s}isChatPrompt(o){try{let e=JSON.parse(o);return Array.isArray(e)&&e.leng
19
19
  `),(l[u].length>0||u<l.length-1)&&(m+=wne()+l[u]);return p.call(o==="stdout"?process.stdout:process.stderr,m,i,a)}return p.call(o==="stdout"?process.stdout:process.stderr,t,i,a)}}function I7(){var p;OU||(C7=typeof((p=process.stdout)==null?void 0:p.isTTY)=="boolean"&&process.stdout.isTTY,OU=process.stdout.write.bind(process.stdout),T7=process.stderr.write.bind(process.stderr),process.stdout.write=E7(OU,"stdout"),process.stderr.write=E7(T7,"stderr"));}function D7(p){var e;let o=(e=Pu.get(p))!=null?e:[];return Pu.delete(p),o}var HU,GU,v7,OU,T7,j7,C7,Pu,QU=n(()=>{bu();HU=[245,166,35],GU=[224,62,45],v7=20,OU=null,T7=null,j7=0,C7=false,Pu=new Map;});function Ane(p,o){if(p==null)return p;switch(o){case "boolean":return typeof p=="boolean"?p:Xo(String(p),"boolean");case "float":case "integer":{if(typeof p=="number")return p;let e=Xo(String(p),"float");if(typeof e=="number"&&Number.isNaN(e))throw new TypeError(`Cannot cast "${p}" to number`);return o==="integer"?Math.trunc(e):e}default:return typeof p=="string"?p:Array.isArray(p)||typeof p=="object"&&p!==null?JSON.stringify(p):String(p)}}var _ne,kne,U7,zu,fp,N7=n(()=>{jp();gg();wu();IU();gp();JU();re();ar();Ru();bu();QU();_ne=500,kne=3e5,U7=1e4,zu=class{constructor(o,e,t){this.shutdownRequested=false;this.heartbeatTimer=null;this.pollTimer=null;this.cancelledJobs=new Map;this.activeTasks=new Set;this.pollFailures=0;var i,a;this.api=o,this.runnerId=e,this.heartbeatIntervalMs=(i=t==null?void 0:t.heartbeatIntervalMs)!=null?i:5e3,this.backoffCapMs=(a=t==null?void 0:t.backoffCapMs)!=null?a:3e4;}start(){this.startHeartbeat(),this.startPolling();}shutdown(){this.shutdownRequested=true,this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null),this.pollTimer&&(clearTimeout(this.pollTimer),this.pollTimer=null);}startHeartbeat(){let o=setInterval(()=>{this.heartbeatTick().catch(e=>{exports.logger.debug("Heartbeat tick error",{error:e});});},this.heartbeatIntervalMs);o.unref(),this.heartbeatTimer=o;}async heartbeatTick(){var o;if(!this.shutdownRequested)try{let t=(o=(await this.api.runners.heartbeat(this.runnerId)).cancelledJobIds)!=null?o:[],i=Date.now();for(let a of t)this.cancelledJobs.set(a,i);this.pruneCancelledJobs(i);}catch(e){if(e instanceof lt){exports.logger.info("Runner deregistered (410), shutting down"),this.shutdown();return}exports.logger.debug("Heartbeat error",{error:e});}}startPolling(){this.pollTick(1e3);}pollTick(o){if(this.shutdownRequested)return;(async()=>{if(this.shutdownRequested)return;let t=o;try{let i=await this.api.runners.nextJob(this.runnerId);if(this.pollFailures=0,t=1e3,i===null){this.scheduleNextPoll(_ne,t);return}this.spawnJob(i);}catch(i){if(this.pollFailures++,this.pollFailures===1){let s=i instanceof k?i.statusCode:void 0;exports.logger.warn("Unable to reach Opik server"+(s?` (API ${s})`:"")+". Retrying...",{error:i});}else exports.logger.debug("Poll error",{error:i});let a=this.jitteredBackoff(t);t=Math.min(t*2,this.backoffCapMs),this.scheduleNextPoll(a,t);return}this.scheduleNextPoll(0,t);})().catch(t=>{this.pollFailures++,this.pollFailures===1?exports.logger.warn("Unable to reach Opik server. Retrying...",{error:t}):exports.logger.debug("Poll tick error",{error:t}),this.scheduleNextPoll(this.jitteredBackoff(o),Math.min(o*2,this.backoffCapMs));});}scheduleNextPoll(o,e){if(this.shutdownRequested)return;let t=setTimeout(()=>this.pollTick(e),o);t.unref(),this.pollTimer=t;}spawnJob(o){let e=this.executeJob(o).finally(()=>{this.activeTasks.delete(e);});this.activeTasks.add(e);}async executeJob(o){var s,c;let e=(s=o.id)!=null?s:"",t=(c=o.agentName)!=null?c:"";if(this.cancelledJobs.has(e)){exports.logger.debug(`Skipping cancelled job ${e}`),this.cancelledJobs.delete(e);return}if(!Zo().get(t)){exports.logger.error(`Unknown agent '${t}' for job ${e}`),await this.reportJobResult(e,{status:"failed",error:`Unknown agent: ${t}`,traceId:o.traceId});return}let a=exports.generateId();await this.reportJobResult(e,{status:"running",traceId:a});try{let l=await this.invokeAgent(o,e,a);await exports.flushAll().catch(m=>{exports.logger.debug("Flush error after job execution",{error:m});}),await this.sendJobLogs(e),await this.reportJobResult(e,{status:"completed",result:this.normalizeResult(l),traceId:a});}catch(l){await exports.flushAll().catch(()=>{}),await this.sendJobLogs(e);let m=o.timeout,u=l instanceof fp?`Job timed out after ${m}s`:l instanceof Error?`${l.name}: ${l.message}`:String(l);l instanceof fp?exports.logger.warn(`Job ${e} timed out after ${m}s`):exports.logger.error(`Job ${e} failed: ${u}`),await this.reportJobResult(e,{status:"failed",error:u,traceId:a});}}async invokeAgent(o,e,t){var h,y;let i=(h=o.agentName)!=null?h:"",a=(y=o.inputs)!=null?y:{},s=o.maskId,c=o.blueprintName,l=o.promptMasks,m=Zo().get(i),u=m.params.map(w=>{let A=a[w.name];if(!(w.presence===ao.Optional&&A===void 0&&!(w.name in a)))return Ane(A,w.type)}),d=()=>w7({traceId:t,jobId:e},()=>QZ(l,()=>s||c?xu({blueprintName:c,maskId:s},()=>m.func(...u)):m.func(...u))),f=Promise.resolve(d()),g=o.timeout;return g&&g>0?Promise.race([f,new Promise((w,A)=>{setTimeout(()=>A(new fp("Job timed out")),g*1e3).unref();})]):f}normalizeResult(o){return o!=null&&typeof o!="string"&&typeof o!="number"&&typeof o!="boolean"&&!Array.isArray(o)&&typeof o!="object"&&(o=String(o)),typeof o=="object"&&o!==null&&!Array.isArray(o)?o:{result:o}}async reportJobResult(o,e){try{await this.api.runners.reportJobResult(o,e);}catch(t){exports.logger.warn(`Failed to report result for job ${o}`,{error:t});}}async sendJobLogs(o){let e=D7(o);if(e.length!==0)try{await this.api.runners.appendJobLogs(o,{body:e});}catch{exports.logger.debug(`Failed to send logs for job ${o}`);}}pruneCancelledJobs(o){let e=o-kne;for(let[t,i]of this.cancelledJobs)i<=e&&this.cancelledJobs.delete(t);if(this.cancelledJobs.size>U7){let t=[...this.cancelledJobs.entries()].sort((a,s)=>a[1]-s[1]),i=t.length-U7;for(let a=0;a<i;a++)this.cancelledJobs.delete(t[a][0]);}}jitteredBackoff(o){return Math.min(o,this.backoffCapMs)*(.5+Math.random()*.5)}};fp=class extends Error{constructor(o){super(o),this.name="TimeoutError";}};});function $U(){process.env.OPIK_RUNNER_MODE==="true"&&(F7||(F7=true,Pne().catch(p=>{exports.logger.error("Runner activation failed",{error:p});})));}async function Pne(){var m,u;let p=(m=process.env.OPIK_RUNNER_ID)!=null?m:"",o=(u=process.env.OPIK_PROJECT_NAME)!=null?u:"";if(!p){exports.logger.error("OPIK_RUNNER_ID not set, cannot activate runner");return}zne(p,o),I7();let e=new exports.Opik,t=e.api;function i(d){return {description:d.docstring,language:"typescript",params:d.params.map(f=>({name:f.name,type:f.type,presence:f.presence})),timeout:0}}function a(d){let f=Zo(),g={};for(let[h,y]of f)g[h]=i(y);t.runners.registerAgents(p,{body:g}).catch(h=>{exports.logger.debug("Failed to sync agents after new registration",{error:h});});}await new Promise(d=>setImmediate(d));let s=Zo();if(s.size>0){let d={};for(let[f,g]of s)d[f]=i(g);try{await t.runners.registerAgents(p,{body:d});}catch{exports.logger.debug("Failed to register agents on startup");}}P7(a),exports.logger.info("Runner activated");let c=new zu(t,p);c.start();let l=()=>{B7=true,exports.logger.info("Received shutdown signal, stopping runner..."),c.shutdown(),e.flush().catch(()=>{}).finally(()=>process.exit(0));};process.once("SIGTERM",l),process.once("SIGINT",l),process.on("exit",()=>{B7||console.error(`
20
20
  Warning: The process exited without blocking. The runner needs the process to stay alive to process jobs.
21
21
  Use a server framework like express or fastify to keep the process running.
22
- `);});}function zne(p,o){let e=[" \u2800\u20DD","opik ",`runner: ${p}`];o&&e.push(` project: ${o}`),console.log(e.join("")),console.log();}var F7,B7,YU=n(()=>{re();Ho();Ru();N7();QU();F7=false,B7=false;});function Tne(p){return !!p&&(typeof p=="object"||typeof p=="function")&&typeof p.then=="function"}function jne({name:p,parentSpan:o,projectName:e,trace:t,type:i="llm",environment:a}){var l;exports.logger.debug("Creating new span:",{name:p,parentSpan:o==null?void 0:o.data.id,projectName:e,type:i,environment:a});let s=t;if(s)a!==void 0&&a!==s.data.environment&&exports.logger.warn(`Nested @track requested environment "${a}", but the enclosing trace already uses "${(l=s.data.environment)!=null?l:"(none)"}". The outer environment will be used.`);else {let m=x7();s=ku().trace({name:p,projectName:e,...a!==void 0?{environment:a}:{},...m?{id:m}:{}});}let c=s.span({name:p,parentSpanId:o==null?void 0:o.data.id,projectName:e,type:i});return exports.logger.debug("Span created with ID:",c.data.id),{span:c,trace:s}}function Ene({args:p,span:o,trace:e}){if(exports.logger.debug("Starting span execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id}),p.length===0)return;let t={arguments:p};exports.logger.debug("Recording span input"),o.update({input:t}),e&&(exports.logger.debug("Recording trace input"),e.update({input:t}));}function M7({result:p,span:o,trace:e,enrichSpan:t}){exports.logger.debug("Recording successful execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id});let i=typeof p=="object"?p:{result:p},a=new Date,s={endTime:a,output:i};if(t){let c=t(p);Object.assign(s,c);}o.update(s),e&&e.update({endTime:a,output:i});}function Cne(p){var o;try{let e=p instanceof Error?p:new Error(String(p));return {message:e.message,exceptionType:e.name,traceback:p instanceof Error&&(o=e.stack)!=null?o:""}}catch{return {message:"<thrown value could not be described>",exceptionType:"Error",traceback:""}}}function W7({span:p,error:o,trace:e}){let t=Cne(o);exports.logger.error("Recording execution error:",{spanId:p.data.id,traceId:e==null?void 0:e.data.id,error:o instanceof Error?{name:t.exceptionType,message:t.message,stack:t.traceback}:o}),p.update({errorInfo:t}),p.end(),e&&(e.update({errorInfo:t}),e.end());}function Su({name:p,projectName:o,type:e,enrichSpan:t,environment:i}={},a){return function(...c){if(!Le())return a.apply(this,c);let l=Qt.getStore(),{span:m,trace:u}=jne({name:p!=null?p:a.name||vne,parentSpan:l==null?void 0:l.span,projectName:o,trace:l==null?void 0:l.trace,type:e,environment:i}),d=!l,f=this;return Qt.run({span:m,trace:u},()=>{let g=d?u:void 0;try{Ene({args:c,span:m,trace:g});let h=a.apply(f,c);return Tne(h)?h.then(y=>(M7({span:m,result:y,trace:g,enrichSpan:t}),y),y=>{throw W7({span:m,error:y,trace:g}),y}):(M7({span:m,result:h,trace:g,enrichSpan:t}),h)}catch(h){throw W7({span:m,error:h,trace:g}),h}})}}function rt(p,o){if(typeof p=="function")return Su({},p);let e=p;if(o){let t=Su(e,o);return e.entrypoint&&Ine(o,t,e),t}return function(...t){if(t.length===2&&typeof t[1]=="object"&&t[1]!==null&&"kind"in t[1]){let[s,c]=t;if(c.kind!=="method")throw new Error("track decorator is only applicable to methods");return Su(e,s)}let[,,i]=t;if(!i||typeof i.value!="function")throw new Error("track decorator can only be applied to methods");let a=i.value;return i.value=Su(e,a),i}}function Ine(p,o,e){var m;let t=e.name||p.name;if(!t)throw new Error("entrypoint functions must have a name. Provide one via track({ name: '...' }) or use a named function.");let i=e.projectName||ku().config.projectName,a=(m=e.params)!=null?m:z7(p),s=new Set(["string","number","float","integer","boolean"]),c=a.filter(u=>!s.has(u.type));if(c.length>0){let u=c.map(d=>`${d.name} (${d.type})`);exports.logger.warn(`Could not resolve type for parameter(s) [${u.join(", ")}] in "${t}". These parameters will default to 'string' and cannot be modified via the UI. Consider using a supported type (string, number, boolean) or choosing a different entrypoint.`);}let l=a.map(u=>({...u,type:u.type==="number"?"float":u.type}));A7({func:o,name:t,project:i,params:l,docstring:""}),$U();}function ku(){return XU===null&&(XU=new exports.Opik),XU}var vne,Qt;exports.getTrackContext=void 0;var XU,Nr=n(()=>{Ho();oo();re();bu();Ru();YU();vne="track.decorator",Qt=new async_hooks.AsyncLocalStorage,exports.getTrackContext=()=>{let{span:p,trace:o}=Qt.getStore()||{};if(!(!p||!o))return {span:p,trace:o}};XU=null;});function Dne(p,o){var e;if(p!=null)return o==="prompt"?(e=p.commit)!=null?e:void 0:o==="prompt_commit"?BU(p,o):p}function ZU(p){let{values:o,fieldNames:e,blueprintId:t,blueprintVersion:i,isFallback:a,maskId:s}=p,c={...o};return Object.defineProperties(c,{blueprintId:{value:t,enumerable:false,writable:false},blueprintVersion:{value:i,enumerable:false,writable:false},isFallback:{value:a,enumerable:false,writable:false}}),new Proxy(c,{get(m,u){return typeof u!="string"||Une.has(u)||e.has(u)&&Nne({blueprintId:t,blueprintVersion:i,maskId:s,fieldNames:e,values:o}),Reflect.get(m,u)}})}function Nne(p){let o=exports.getTrackContext();if(!o)return;let{blueprintId:e,blueprintVersion:t,maskId:i,fieldNames:a,values:s}=p,c={};for(let u of a){let d=s[u];if(d===void 0)continue;let f=fu(d);c[u]={value:Dne(d,f),type:f};}let l={_blueprint_id:e,blueprint_version:t,values:c};i!==void 0&&(l._mask_id=i);let m={agent_configuration:l};o.span.update({metadata:m}),o.trace.update({metadata:m});}var Une,L7=n(()=>{Nr();gp();Une=new Set(["blueprintId","blueprintVersion","isFallback"]);});function J7(){let p=process.env.OPIK_CONFIG_TTL_SECONDS;if(p!==void 0){let o=parseInt(p,10);if(!isNaN(o))return o}return Fne}function H7(p,o,e,t=null){return rN.getOrCreate(p,o,e,t)}function tN(p,o,e,t,i,a=null){let s=rN.getOrCreate(p,o,e,a);t!==null&&s.update(t),i!==null&&e===null&&(s.setRefreshCallback(i),rN.ensureRefreshTimerStarted());}var Fne,Bne,V7,qU,eN,rN,G7=n(()=>{re();Fne=300,Bne=1e3,V7=100;qU=class{constructor(o){this._blueprint=null;this._lastFetchMs=null;this._refreshCallback=null;this._ttlMs=o*1e3;}setRefreshCallback(o){this._refreshCallback===null&&(this._refreshCallback=o);}update(o){this._blueprint=o,this._lastFetchMs=Date.now();}getBlueprint(){return this._blueprint}isStale(){return this._lastFetchMs===null?true:Date.now()-this._lastFetchMs>=this._ttlMs}async tryBackgroundRefresh(){if(this._refreshCallback!==null)try{let o=await this._refreshCallback();o!==null&&this.update(o);}catch(o){exports.logger.debug("Background blueprint cache refresh failed",o);}}},eN=class{constructor(){this._entries=new Map;this._intervalHandle=null;this._refreshRunning=false;}getOrCreate(o,e,t,i=null){let a=`${o}::${e!=null?e:""}::${t!=null?t:""}::${i!=null?i:""}`,s=this._entries.get(a);return s||(s=new qU(J7()),this._entries.set(a,s)),s}ensureRefreshTimerStarted(){if(this._intervalHandle!==null)return;let o=Math.max(J7()*1e3,Bne);this._intervalHandle=setInterval(()=>{this._refreshAllStale();},o),this._intervalHandle.unref();}async _refreshAllStale(){if(!this._refreshRunning){this._refreshRunning=true;try{let o=[...this._entries.values()].filter(e=>e.isStale());for(let e=0;e<o.length;e+=V7)await Promise.all(o.slice(e,e+V7).map(t=>t.tryBackgroundRefresh()));}finally{this._refreshRunning=false;}}}clear(){this._intervalHandle!==null&&(clearInterval(this._intervalHandle),this._intervalHandle=null),this._refreshRunning=false,this._entries.clear();}},rN=new eN;});function Mne(p){return p.scoringFailed?false:p.value===1}exports.DEFAULT_EXECUTION_POLICY=void 0;exports.TestSuiteResult=void 0;var vu=n(()=>{exports.DEFAULT_EXECUTION_POLICY={runsPerItem:1,passThreshold:1};exports.TestSuiteResult=class{constructor(o){this.allItemsPassed=o.allItemsPassed,this.itemsPassed=o.itemsPassed,this.itemsTotal=o.itemsTotal,this.passRate=o.passRate,this.itemResults=o.itemResults,this.experimentId=o.experimentId,this.experimentName=o.experimentName,this.experimentUrl=o.experimentUrl,this.suiteName=o.suiteName,this.totalTime=o.totalTime;}toReportDict(){var t,i,a;let o=[];for(let[s,c]of this.itemResults){let l=new Map;for(let u of c.testResults){let d=(t=u.trialId)!=null?t:0,f=(i=l.get(d))!=null?i:[];f.push(u),l.set(d,f);}let m=[];for(let[u,d]of l){let g=d.flatMap(A=>A.scoreResults).map(A=>{var I;let C={name:A.name,passed:Mne(A),value:A.value,scoringFailed:(I=A.scoringFailed)!=null?I:false};return A.reason!==void 0&&(C.reason=A.reason),C}),h=g.length===0||g.every(A=>A.passed),y=d[0],w={trialId:u,passed:h,input:y.testCase.taskOutput.input,output:y.testCase.taskOutput.output,assertions:g};y.testCase.traceId&&(w.traceId=y.testCase.traceId),m.push(w);}o.push({datasetItemId:s,passed:c.passed,runsPassed:c.runsPassed,executionPolicy:{runsPerItem:c.configuredRunsPerItem,passThreshold:c.passThreshold},runs:m});}let e={suitePassed:this.allItemsPassed,itemsPassed:this.itemsPassed,itemsTotal:this.itemsTotal,passRate:(a=this.passRate)!=null?a:null,experimentId:this.experimentId};return this.suiteName!==void 0&&(e.suiteName=this.suiteName),this.experimentName!==void 0&&(e.experimentName=this.experimentName),this.experimentUrl!==void 0&&(e.experimentUrl=this.experimentUrl),this.totalTime!==void 0&&(e.totalTimeSeconds=Math.round(this.totalTime*1e3)/1e3),e.generatedAt=new Date().toISOString(),e.items=o,e}toDict(){return this.toReportDict()}};});function Wne(p){return p.scoreResults.length===0?true:p.scoreResults.every(o=>!!o.value)}function Lne(p){let o=new Map;for(let e of p){let t=e.testCase.datasetItemId,i=o.get(t);i?i.push(e):o.set(t,[e]);}return o}function Vne(p){var e;let o=new Map;for(let t of p){let i=(e=t.trialId)!=null?e:0,a=o.get(i);a?a.push(t):o.set(i,[t]);}return o}function oN(p,o={}){var m;let e=Lne(p.testResults),t=new Map;for(let[u,d]of e){let f=Vne(d),g=f.size,h=0;for(let[,F]of f)F.every(Wne)&&h++;let w=(m=d[0].resolvedExecutionPolicy)!=null?m:exports.DEFAULT_EXECUTION_POLICY,A=w.passThreshold,C=w.runsPerItem,I=h>=A,D=d.some(F=>F.scoreResults.length>0);t.set(u,{datasetItemId:u,passed:I,hasAssertions:D,runsPassed:h,runsTotal:g,configuredRunsPerItem:C,passThreshold:A,testResults:d});}let i=t.size,a=[...t.values()].filter(u=>u.passed).length,s=i===0||a===i,c=[...t.values()].filter(u=>u.hasAssertions),l=c.length===0?void 0:c.filter(u=>u.passed).length/c.length;return new exports.TestSuiteResult({allItemsPassed:s,itemsPassed:a,itemsTotal:i,passRate:l,itemResults:t,experimentId:p.experimentId,experimentName:p.experimentName,experimentUrl:p.resultUrl,suiteName:o.suiteName,totalTime:o.totalTime})}var iN=n(()=>{vu();});var ei,ri,aN=n(()=>{O7();ei=class ei{static getInstance(o){return ei.instance||(ei.instance=new exports.Opik(o)),ei.instance}};ei.instance=null;ri=ei;});exports.TASK_ERROR_SCORE_NAME=void 0;var ju=n(()=>{exports.TASK_ERROR_SCORE_NAME="__opik_task_error__";});function K7(p,o){if(!o||typeof o!="object")throw new Error("Arguments must be an object");let e=p.validationSchema,i=e.extend(Object.fromEntries(Object.entries(e.shape).map(([a,s])=>[a,s.refine(c=>c!==void 0,{message:`${a} cannot be undefined`,path:[a]})]))).safeParse(o);if(!i.success){let a=i.error.issues.map(c=>c.path[0]).filter(Boolean),s=[...new Set(a)];throw new Error(Jne(p,o,s))}}function Jne(p,o,e){let t=Object.keys(o),i=e.filter(a=>!(a in o));return `Metric '${p.name}' is skipped, missing required arguments: ${i.join(", ")}. Available arguments: ${t.join(", ")}.`}var Q7=n(()=>{});var Eu,$7=n(()=>{re();ju();Eu=class{static calculateScoreStats(o){var i;let e=new Map;for(let a of o!=null?o:[])for(let s of (i=a==null?void 0:a.scoreResults)!=null?i:[]){if(!s)continue;let c=s.scoringFailed&&s.name===exports.TASK_ERROR_SCORE_NAME,l=!s.scoringFailed&&typeof s.value=="number";if(c||!s.scoringFailed&&!l)continue;let m=e.get(s.name)||{sum:0,count:0,failed:0};l?(m.sum+=s.value,m.count+=1):m.failed+=1,e.set(s.name,m);}let t=new Map;return e.forEach((a,s)=>{t.set(s,{average:a.count>0?a.sum/a.count:void 0,failed:a.failed});}),t}static formatScore(o){return o.toFixed(4)}static formatTime(o){let e=Math.floor(o/3600),t=Math.floor(o%3600/60),i=Math.floor(o%60);return `${e.toString().padStart(2,"0")}:${t.toString().padStart(2,"0")}:${i.toString().padStart(2,"0")}`}static async generateResultTable(o,e,t,i,a){if(o.length===0){exports.logger.info(`
22
+ `);});}function zne(p,o){let e=[" \u2800\u20DD","opik ",`runner: ${p}`];o&&e.push(` project: ${o}`),console.log(e.join("")),console.log();}var F7,B7,YU=n(()=>{re();Ho();Ru();N7();QU();F7=false,B7=false;});function Tne(p){return !!p&&(typeof p=="object"||typeof p=="function")&&typeof p.then=="function"}function jne({name:p,parentSpan:o,projectName:e,trace:t,type:i="llm",environment:a}){var l;exports.logger.debug("Creating new span:",{name:p,parentSpan:o==null?void 0:o.data.id,projectName:e,type:i,environment:a});let s=t;if(s)a!==void 0&&a!==s.data.environment&&exports.logger.warn(`Nested @track requested environment "${a}", but the enclosing trace already uses "${(l=s.data.environment)!=null?l:"(none)"}". The outer environment will be used.`);else {let m=x7();s=ku().trace({name:p,projectName:e,...a!==void 0?{environment:a}:{},...m?{id:m}:{}});}let c=s.span({name:p,parentSpanId:o==null?void 0:o.data.id,projectName:e,type:i});return exports.logger.debug("Span created with ID:",c.data.id),{span:c,trace:s}}function Ene({args:p,span:o,trace:e}){if(exports.logger.debug("Starting span execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id}),p.length===0)return;let t={arguments:p};exports.logger.debug("Recording span input"),o.update({input:t}),e&&(exports.logger.debug("Recording trace input"),e.update({input:t}));}function M7({result:p,span:o,trace:e,enrichSpan:t}){var c,l;exports.logger.debug("Recording successful execution:",{spanId:o.data.id,traceId:e==null?void 0:e.data.id});let i=typeof p=="object"?p:{result:p},a=new Date,s={endTime:a,output:i};if(t){let m=t(p);Object.assign(s,m);}s.output=(c=o.data.output)!=null?c:s.output,o.update(s),e&&e.update({endTime:a,output:(l=e.data.output)!=null?l:i});}function Cne(p){var o;try{let e=p instanceof Error?p:new Error(String(p));return {message:e.message,exceptionType:e.name,traceback:p instanceof Error&&(o=e.stack)!=null?o:""}}catch{return {message:"<thrown value could not be described>",exceptionType:"Error",traceback:""}}}function W7({span:p,error:o,trace:e}){let t=Cne(o);exports.logger.error("Recording execution error:",{spanId:p.data.id,traceId:e==null?void 0:e.data.id,error:o instanceof Error?{name:t.exceptionType,message:t.message,stack:t.traceback}:o}),p.update({errorInfo:t}),p.end(),e&&(e.update({errorInfo:t}),e.end());}function Su({name:p,projectName:o,type:e,enrichSpan:t,environment:i}={},a){return function(...c){if(!Le())return a.apply(this,c);let l=Qt.getStore(),{span:m,trace:u}=jne({name:p!=null?p:a.name||vne,parentSpan:l==null?void 0:l.span,projectName:o,trace:l==null?void 0:l.trace,type:e,environment:i}),d=!l,f=this;return Qt.run({span:m,trace:u},()=>{let g=d?u:void 0;try{Ene({args:c,span:m,trace:g});let h=a.apply(f,c);return Tne(h)?h.then(y=>(M7({span:m,result:y,trace:g,enrichSpan:t}),y),y=>{throw W7({span:m,error:y,trace:g}),y}):(M7({span:m,result:h,trace:g,enrichSpan:t}),h)}catch(h){throw W7({span:m,error:h,trace:g}),h}})}}function rt(p,o){if(typeof p=="function")return Su({},p);let e=p;if(o){let t=Su(e,o);return e.entrypoint&&Ine(o,t,e),t}return function(...t){if(t.length===2&&typeof t[1]=="object"&&t[1]!==null&&"kind"in t[1]){let[s,c]=t;if(c.kind!=="method")throw new Error("track decorator is only applicable to methods");return Su(e,s)}let[,,i]=t;if(!i||typeof i.value!="function")throw new Error("track decorator can only be applied to methods");let a=i.value;return i.value=Su(e,a),i}}function Ine(p,o,e){var m;let t=e.name||p.name;if(!t)throw new Error("entrypoint functions must have a name. Provide one via track({ name: '...' }) or use a named function.");let i=e.projectName||ku().config.projectName,a=(m=e.params)!=null?m:z7(p),s=new Set(["string","number","float","integer","boolean"]),c=a.filter(u=>!s.has(u.type));if(c.length>0){let u=c.map(d=>`${d.name} (${d.type})`);exports.logger.warn(`Could not resolve type for parameter(s) [${u.join(", ")}] in "${t}". These parameters will default to 'string' and cannot be modified via the UI. Consider using a supported type (string, number, boolean) or choosing a different entrypoint.`);}let l=a.map(u=>({...u,type:u.type==="number"?"float":u.type}));A7({func:o,name:t,project:i,params:l,docstring:""}),$U();}function ku(){return XU===null&&(XU=new exports.Opik),XU}var vne,Qt;exports.getTrackContext=void 0;var XU,Nr=n(()=>{Ho();oo();re();bu();Ru();YU();vne="track.decorator",Qt=new async_hooks.AsyncLocalStorage,exports.getTrackContext=()=>{let{span:p,trace:o}=Qt.getStore()||{};if(!(!p||!o))return {span:p,trace:o}};XU=null;});function Dne(p,o){var e;if(p!=null)return o==="prompt"?(e=p.commit)!=null?e:void 0:o==="prompt_commit"?BU(p,o):p}function ZU(p){let{values:o,fieldNames:e,blueprintId:t,blueprintVersion:i,isFallback:a,maskId:s}=p,c={...o};return Object.defineProperties(c,{blueprintId:{value:t,enumerable:false,writable:false},blueprintVersion:{value:i,enumerable:false,writable:false},isFallback:{value:a,enumerable:false,writable:false}}),new Proxy(c,{get(m,u){return typeof u!="string"||Une.has(u)||e.has(u)&&Nne({blueprintId:t,blueprintVersion:i,maskId:s,fieldNames:e,values:o}),Reflect.get(m,u)}})}function Nne(p){let o=exports.getTrackContext();if(!o)return;let{blueprintId:e,blueprintVersion:t,maskId:i,fieldNames:a,values:s}=p,c={};for(let u of a){let d=s[u];if(d===void 0)continue;let f=fu(d);c[u]={value:Dne(d,f),type:f};}let l={_blueprint_id:e,blueprint_version:t,values:c};i!==void 0&&(l._mask_id=i);let m={agent_configuration:l};o.span.update({metadata:m}),o.trace.update({metadata:m});}var Une,L7=n(()=>{Nr();gp();Une=new Set(["blueprintId","blueprintVersion","isFallback"]);});function J7(){let p=process.env.OPIK_CONFIG_TTL_SECONDS;if(p!==void 0){let o=parseInt(p,10);if(!isNaN(o))return o}return Fne}function H7(p,o,e,t=null){return rN.getOrCreate(p,o,e,t)}function tN(p,o,e,t,i,a=null){let s=rN.getOrCreate(p,o,e,a);t!==null&&s.update(t),i!==null&&e===null&&(s.setRefreshCallback(i),rN.ensureRefreshTimerStarted());}var Fne,Bne,V7,qU,eN,rN,G7=n(()=>{re();Fne=300,Bne=1e3,V7=100;qU=class{constructor(o){this._blueprint=null;this._lastFetchMs=null;this._refreshCallback=null;this._ttlMs=o*1e3;}setRefreshCallback(o){this._refreshCallback===null&&(this._refreshCallback=o);}update(o){this._blueprint=o,this._lastFetchMs=Date.now();}getBlueprint(){return this._blueprint}isStale(){return this._lastFetchMs===null?true:Date.now()-this._lastFetchMs>=this._ttlMs}async tryBackgroundRefresh(){if(this._refreshCallback!==null)try{let o=await this._refreshCallback();o!==null&&this.update(o);}catch(o){exports.logger.debug("Background blueprint cache refresh failed",o);}}},eN=class{constructor(){this._entries=new Map;this._intervalHandle=null;this._refreshRunning=false;}getOrCreate(o,e,t,i=null){let a=`${o}::${e!=null?e:""}::${t!=null?t:""}::${i!=null?i:""}`,s=this._entries.get(a);return s||(s=new qU(J7()),this._entries.set(a,s)),s}ensureRefreshTimerStarted(){if(this._intervalHandle!==null)return;let o=Math.max(J7()*1e3,Bne);this._intervalHandle=setInterval(()=>{this._refreshAllStale();},o),this._intervalHandle.unref();}async _refreshAllStale(){if(!this._refreshRunning){this._refreshRunning=true;try{let o=[...this._entries.values()].filter(e=>e.isStale());for(let e=0;e<o.length;e+=V7)await Promise.all(o.slice(e,e+V7).map(t=>t.tryBackgroundRefresh()));}finally{this._refreshRunning=false;}}}clear(){this._intervalHandle!==null&&(clearInterval(this._intervalHandle),this._intervalHandle=null),this._refreshRunning=false,this._entries.clear();}},rN=new eN;});function Mne(p){return p.scoringFailed?false:p.value===1}exports.DEFAULT_EXECUTION_POLICY=void 0;exports.TestSuiteResult=void 0;var vu=n(()=>{exports.DEFAULT_EXECUTION_POLICY={runsPerItem:1,passThreshold:1};exports.TestSuiteResult=class{constructor(o){this.allItemsPassed=o.allItemsPassed,this.itemsPassed=o.itemsPassed,this.itemsTotal=o.itemsTotal,this.passRate=o.passRate,this.itemResults=o.itemResults,this.experimentId=o.experimentId,this.experimentName=o.experimentName,this.experimentUrl=o.experimentUrl,this.suiteName=o.suiteName,this.totalTime=o.totalTime;}toReportDict(){var t,i,a;let o=[];for(let[s,c]of this.itemResults){let l=new Map;for(let u of c.testResults){let d=(t=u.trialId)!=null?t:0,f=(i=l.get(d))!=null?i:[];f.push(u),l.set(d,f);}let m=[];for(let[u,d]of l){let g=d.flatMap(A=>A.scoreResults).map(A=>{var I;let C={name:A.name,passed:Mne(A),value:A.value,scoringFailed:(I=A.scoringFailed)!=null?I:false};return A.reason!==void 0&&(C.reason=A.reason),C}),h=g.length===0||g.every(A=>A.passed),y=d[0],w={trialId:u,passed:h,input:y.testCase.taskOutput.input,output:y.testCase.taskOutput.output,assertions:g};y.testCase.traceId&&(w.traceId=y.testCase.traceId),m.push(w);}o.push({datasetItemId:s,passed:c.passed,runsPassed:c.runsPassed,executionPolicy:{runsPerItem:c.configuredRunsPerItem,passThreshold:c.passThreshold},runs:m});}let e={suitePassed:this.allItemsPassed,itemsPassed:this.itemsPassed,itemsTotal:this.itemsTotal,passRate:(a=this.passRate)!=null?a:null,experimentId:this.experimentId};return this.suiteName!==void 0&&(e.suiteName=this.suiteName),this.experimentName!==void 0&&(e.experimentName=this.experimentName),this.experimentUrl!==void 0&&(e.experimentUrl=this.experimentUrl),this.totalTime!==void 0&&(e.totalTimeSeconds=Math.round(this.totalTime*1e3)/1e3),e.generatedAt=new Date().toISOString(),e.items=o,e}toDict(){return this.toReportDict()}};});function Wne(p){return p.scoreResults.length===0?true:p.scoreResults.every(o=>!!o.value)}function Lne(p){let o=new Map;for(let e of p){let t=e.testCase.datasetItemId,i=o.get(t);i?i.push(e):o.set(t,[e]);}return o}function Vne(p){var e;let o=new Map;for(let t of p){let i=(e=t.trialId)!=null?e:0,a=o.get(i);a?a.push(t):o.set(i,[t]);}return o}function oN(p,o={}){var m;let e=Lne(p.testResults),t=new Map;for(let[u,d]of e){let f=Vne(d),g=f.size,h=0;for(let[,F]of f)F.every(Wne)&&h++;let w=(m=d[0].resolvedExecutionPolicy)!=null?m:exports.DEFAULT_EXECUTION_POLICY,A=w.passThreshold,C=w.runsPerItem,I=h>=A,D=d.some(F=>F.scoreResults.length>0);t.set(u,{datasetItemId:u,passed:I,hasAssertions:D,runsPassed:h,runsTotal:g,configuredRunsPerItem:C,passThreshold:A,testResults:d});}let i=t.size,a=[...t.values()].filter(u=>u.passed).length,s=i===0||a===i,c=[...t.values()].filter(u=>u.hasAssertions),l=c.length===0?void 0:c.filter(u=>u.passed).length/c.length;return new exports.TestSuiteResult({allItemsPassed:s,itemsPassed:a,itemsTotal:i,passRate:l,itemResults:t,experimentId:p.experimentId,experimentName:p.experimentName,experimentUrl:p.resultUrl,suiteName:o.suiteName,totalTime:o.totalTime})}var iN=n(()=>{vu();});var ei,ri,aN=n(()=>{O7();ei=class ei{static getInstance(o){return ei.instance||(ei.instance=new exports.Opik(o)),ei.instance}};ei.instance=null;ri=ei;});exports.TASK_ERROR_SCORE_NAME=void 0;var ju=n(()=>{exports.TASK_ERROR_SCORE_NAME="__opik_task_error__";});function K7(p,o){if(!o||typeof o!="object")throw new Error("Arguments must be an object");let e=p.validationSchema,i=e.extend(Object.fromEntries(Object.entries(e.shape).map(([a,s])=>[a,s.refine(c=>c!==void 0,{message:`${a} cannot be undefined`,path:[a]})]))).safeParse(o);if(!i.success){let a=i.error.issues.map(c=>c.path[0]).filter(Boolean),s=[...new Set(a)];throw new Error(Jne(p,o,s))}}function Jne(p,o,e){let t=Object.keys(o),i=e.filter(a=>!(a in o));return `Metric '${p.name}' is skipped, missing required arguments: ${i.join(", ")}. Available arguments: ${t.join(", ")}.`}var Q7=n(()=>{});var Eu,$7=n(()=>{re();ju();Eu=class{static calculateScoreStats(o){var i;let e=new Map;for(let a of o!=null?o:[])for(let s of (i=a==null?void 0:a.scoreResults)!=null?i:[]){if(!s)continue;let c=s.scoringFailed&&s.name===exports.TASK_ERROR_SCORE_NAME,l=!s.scoringFailed&&typeof s.value=="number";if(c||!s.scoringFailed&&!l)continue;let m=e.get(s.name)||{sum:0,count:0,failed:0};l?(m.sum+=s.value,m.count+=1):m.failed+=1,e.set(s.name,m);}let t=new Map;return e.forEach((a,s)=>{t.set(s,{average:a.count>0?a.sum/a.count:void 0,failed:a.failed});}),t}static formatScore(o){return o.toFixed(4)}static formatTime(o){let e=Math.floor(o/3600),t=Math.floor(o%3600/60),i=Math.floor(o%60);return `${e.toString().padStart(2,"0")}:${t.toString().padStart(2,"0")}:${i.toString().padStart(2,"0")}`}static async generateResultTable(o,e,t,i,a){if(o.length===0){exports.logger.info(`
23
23
  No test results available to display.`);return}let s=[...t.keys()].sort(),c=this.formatTime(i),l=[];if(a&&l.push(yp__default.default.bold.cyan(Sp(a,"View results in Opik dashboard")),""),l.push(yp__default.default.bold(`Total time: ${c}`),yp__default.default.bold(`Number of samples: ${o.length}`)),s.length>0){l.push("");for(let f of s){let{average:g,failed:h}=t.get(f),y=g===void 0?"None":this.formatScore(g),w=yp__default.default.green(`${f}: ${y} (avg)`);h>0&&(w+=yp__default.default.red(` - ${h} failed`)),l.push(w);}}let m=l.join(`
24
24
  `),u=await e.ensureNameLoaded(),d=Hne__default.default(m,{title:`${u} (${o.length} samples)`,titleAlignment:"left",padding:1,margin:0,borderColor:"cyan",borderStyle:"round"});exports.logger.info(`
25
25
  `+d+`
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {Ia}from'./chunk-T4MNYA6L.js';export{va as AgentTaskCompletionJudge,ua as AgentToolCorrectnessJudge,ia as AnswerRelevance,ea as BaseLLMJudgeMetric,F as BaseMetric,G as BaseSuiteEvaluator,s as ChatPrompt,xa as ComplianceRiskJudge,l as ConfigMismatchError,k as ConfigNotFoundError,ba as Contains,B as DEFAULT_EXECUTION_POLICY,m as Dataset,i as DatasetVersion,j as DatasetVersionNotFoundError,pa as DemographicBiasJudge,na as DialogueHelpfulnessJudge,aa as ExactMatch,ja as GEval,ka as GEvalPreset,ra as GenderBiasJudge,ha as Hallucination,da as IsJson,T as LLMJudge,K as ModelConfigurationError,I as ModelError,J as ModelGenerationError,fa as Moderation,Fa as OPIK_PARENT_SPAN_ID_HEADER,Ea as OPIK_TRACE_ID_HEADER,Da as Opik,H as OpikBaseModel,t as OpikQueryLanguage,g as OpikSpanType,qa as PoliticalBiasJudge,r as Prompt,n as PromptType,wa as PromptUncertaintyJudge,oa as QARelevanceJudge,ca as RegexMatch,ta as RegionalBiasJudge,sa as ReligiousBiasJudge,S as ResponseSchema,Q as SYSTEM_PROMPT,ma as SummarizationCoherenceJudge,la as SummarizationConsistencyJudge,E as TASK_ERROR_SCORE_NAME,Ba as TestSuite,C as TestSuiteResult,v as ThreadsAnnotationQueue,u as TracesAnnotationQueue,R as USER_PROMPT_TEMPLATE,ga as Usefulness,M as VercelAIChatModel,y as activateRunner,w as agentConfigContext,D as buildSuiteResult,N as createModel,O as createModelFromInstance,V as deserializeEvaluators,L as detectProvider,c as disableLogger,_ as evaluate,$ as evaluatePrompt,Y as evaluateTestSuite,x as flushAll,h as generateId,Ga as getDistributedTraceHeaders,o as getGlobalClient,z as getTrackContext,e as isTracingActive,a as logger,q as resetGlobalClient,f as resetTracingToConfigDefault,ya as resolveEvaluators,W as resolveExecutionPolicy,X as resolveItemExecutionPolicy,P as resolveModel,Z as runTests,U as serializeEvaluators,p as setGlobalClient,b as setLoggerLevel,d as setTracingActive,A as track,za as validateEvaluators,Aa as validateExecutionPolicy,Ha as z}from'./chunk-T4MNYA6L.js';Ia();
1
+ import {Ia}from'./chunk-XCNOTM5S.js';export{va as AgentTaskCompletionJudge,ua as AgentToolCorrectnessJudge,ia as AnswerRelevance,ea as BaseLLMJudgeMetric,F as BaseMetric,G as BaseSuiteEvaluator,s as ChatPrompt,xa as ComplianceRiskJudge,l as ConfigMismatchError,k as ConfigNotFoundError,ba as Contains,B as DEFAULT_EXECUTION_POLICY,m as Dataset,i as DatasetVersion,j as DatasetVersionNotFoundError,pa as DemographicBiasJudge,na as DialogueHelpfulnessJudge,aa as ExactMatch,ja as GEval,ka as GEvalPreset,ra as GenderBiasJudge,ha as Hallucination,da as IsJson,T as LLMJudge,K as ModelConfigurationError,I as ModelError,J as ModelGenerationError,fa as Moderation,Fa as OPIK_PARENT_SPAN_ID_HEADER,Ea as OPIK_TRACE_ID_HEADER,Da as Opik,H as OpikBaseModel,t as OpikQueryLanguage,g as OpikSpanType,qa as PoliticalBiasJudge,r as Prompt,n as PromptType,wa as PromptUncertaintyJudge,oa as QARelevanceJudge,ca as RegexMatch,ta as RegionalBiasJudge,sa as ReligiousBiasJudge,S as ResponseSchema,Q as SYSTEM_PROMPT,ma as SummarizationCoherenceJudge,la as SummarizationConsistencyJudge,E as TASK_ERROR_SCORE_NAME,Ba as TestSuite,C as TestSuiteResult,v as ThreadsAnnotationQueue,u as TracesAnnotationQueue,R as USER_PROMPT_TEMPLATE,ga as Usefulness,M as VercelAIChatModel,y as activateRunner,w as agentConfigContext,D as buildSuiteResult,N as createModel,O as createModelFromInstance,V as deserializeEvaluators,L as detectProvider,c as disableLogger,_ as evaluate,$ as evaluatePrompt,Y as evaluateTestSuite,x as flushAll,h as generateId,Ga as getDistributedTraceHeaders,o as getGlobalClient,z as getTrackContext,e as isTracingActive,a as logger,q as resetGlobalClient,f as resetTracingToConfigDefault,ya as resolveEvaluators,W as resolveExecutionPolicy,X as resolveItemExecutionPolicy,P as resolveModel,Z as runTests,U as serializeEvaluators,p as setGlobalClient,b as setLoggerLevel,d as setTracingActive,A as track,za as validateEvaluators,Aa as validateExecutionPolicy,Ha as z}from'./chunk-XCNOTM5S.js';Ia();
@@ -1 +1 @@
1
- import {Ca}from'./chunk-T4MNYA6L.js';export{B as DEFAULT_EXECUTION_POLICY,Ba as TestSuite,C as TestSuiteResult,D as buildSuiteResult,V as deserializeEvaluators,Y as evaluateTestSuite,W as resolveExecutionPolicy,X as resolveItemExecutionPolicy,Z as runTests,U as serializeEvaluators}from'./chunk-T4MNYA6L.js';Ca();
1
+ import {Ca}from'./chunk-XCNOTM5S.js';export{B as DEFAULT_EXECUTION_POLICY,Ba as TestSuite,C as TestSuiteResult,D as buildSuiteResult,V as deserializeEvaluators,Y as evaluateTestSuite,W as resolveExecutionPolicy,X as resolveItemExecutionPolicy,Z as runTests,U as serializeEvaluators}from'./chunk-XCNOTM5S.js';Ca();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "opik",
3
3
  "description": "Opik TypeScript and JavaScript SDK",
4
- "version": "2.2.96",
4
+ "version": "2.2.98",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/comet-ml/opik.git",