/**
 * Skipped minification because the original files appears to be already minified.
 * Original file: /npm/@openassistant/utils@1.0.0-alpha.0/dist/index.cjs.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var f=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var I=(n,t,e)=>t in n?f(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var F=(n,t)=>{for(var e in t)f(n,e,{get:t[e],enumerable:!0})},P=(n,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of S(t))!M.call(n,s)&&s!==e&&f(n,s,{get:()=>t[s],enumerable:!(o=k(t,s))||o.enumerable});return n};var R=n=>P(f({},"__esModule",{value:!0}),n);var i=(n,t,e)=>I(n,typeof t!="symbol"?t+"":t,e);var H={};F(H,{ConversationCache:()=>w,ToolCache:()=>g,ToolOutputManager:()=>m,cacheData:()=>A,convertToLangchainTool:()=>j,convertToVercelAiTool:()=>G,convertToVercelAiToolV5:()=>$,formatNumberOrString:()=>z,generateId:()=>B,getBoundsFromGeoJSON:()=>L,getCachedData:()=>D,getValuesFromGeoJSON:()=>E,numericFormatter:()=>N,removeCachedData:()=>q});module.exports=R(H);var l=class l{constructor(){i(this,"cachedResults",{})}static getInstance(){return l.instance||(l.instance=new l),l.instance}get toolCache(){return this.cachedResults}addDataset(t,e){if(t&&e&&typeof e=="object"&&"datasetName"in e&&e.datasetName){let o=e.datasetName;if(o&&typeof o=="string"){let s=e[o];this.cachedResults={...this.cachedResults,[o]:s}}}}clearCache(){this.cachedResults={}}removeDataset(t){let e={...this.cachedResults};delete e[t],this.cachedResults=e}hasDataset(t){return t in this.cachedResults}getDataset(t){return this.cachedResults[t]}};i(l,"instance");var g=l;var d="__openassistant_cache__",C=(()=>{if(typeof globalThis<"u")return globalThis;if(typeof global<"u")return global;if(typeof window<"u")return window;throw new Error("Unable to locate global object")})();C[d]||(C[d]={data:{},get(n){return this.data[n]},set(n,t){this.data[n]=t},delete(n){delete this.data[n]}});var y=C[d];function A(n,t){y.set(n,t)}function D(n){return y.get(n)||null}function q(n){y.delete(n)}function E(n,t){if(!n)return[];if(n.type==="Feature"){let e=n.properties?.[t];return e!==void 0?[e]:[]}return n.type==="FeatureCollection"?n.features.map(e=>e.properties?.[t]).filter(e=>e!==void 0):[]}function L(n){let t=1/0,e=1/0,o=-1/0,s=-1/0,r=a=>{a.forEach(([u,h])=>{t=Math.min(t,h),e=Math.min(e,u),o=Math.max(o,h),s=Math.max(s,u)})},c=a=>{switch(a.type){case"Point":{let[u,h]=a.coordinates;t=Math.min(t,h),e=Math.min(e,u),o=Math.max(o,h),s=Math.max(s,u);break}case"MultiPoint":r(a.coordinates);break;case"LineString":r(a.coordinates);break;case"MultiLineString":a.coordinates.forEach(r);break;case"Polygon":a.coordinates.forEach(r);break;case"MultiPolygon":a.coordinates.forEach(u=>{u.forEach(r)});break}};"features"in n?n.features.forEach(a=>{c(a.geometry)}):c(n.geometry);let x=o-t,v=s-e,O=Math.max(x,v),T=Math.floor(12-Math.log2(O));return{bounds:[[t,e],[o,s]],zoom:Math.max(1,Math.min(18,T))}}var N=n=>Intl.NumberFormat("en-US",{notation:"compact",maximumFractionDigits:1}).format(n);function z(n,t,e={maximumFractionDigits:4,minimumFractionDigits:0}){return typeof n=="number"?new Intl.NumberFormat(t,e).format(n):n}var b=class{constructor(){i(this,"queue",[]);i(this,"locked",!1)}async acquire(){return new Promise(t=>{let e=()=>{this.locked=!1;let s=this.queue.shift();s&&(this.locked=!0,s())},o=()=>t(e);this.locked?this.queue.push(o):(this.locked=!0,o())})}},m=class{constructor(){i(this,"toolOutputs",[]);i(this,"mutex",new b);i(this,"currentSessionId",null)}async startSession(){let t=await this.mutex.acquire();try{return this.currentSessionId=Date.now().toString()+"-"+Math.random().toString(36).substr(2,9),this.currentSessionId}finally{t()}}async endSession(){let t=await this.mutex.acquire();try{this.currentSessionId=null}finally{t()}}async addToolOutput(t,e){let o=await this.mutex.acquire();try{this.toolOutputs.push({toolCallId:t,data:e,sessionId:this.currentSessionId||void 0})}finally{o()}}async getLastToolOutput(){let t=await this.mutex.acquire();try{return this.toolOutputs.length>0?this.toolOutputs[this.toolOutputs.length-1]:null}finally{t()}}async getLastToolOutputFromCurrentSession(){let t=await this.mutex.acquire();try{if(!this.currentSessionId)return null;for(let e=this.toolOutputs.length-1;e>=0;e--){let o=this.toolOutputs[e];if(o.sessionId===this.currentSessionId)return o}return null}finally{t()}}async hasToolOutputsInCurrentSession(){let t=await this.mutex.acquire();try{return this.currentSessionId?this.toolOutputs.some(e=>e.sessionId===this.currentSessionId):!1}finally{t()}}async getToolOutputsFromCurrentSession(){let t=await this.mutex.acquire();try{return this.currentSessionId?this.toolOutputs.filter(e=>e.sessionId===this.currentSessionId):[]}finally{t()}}async findDataByDatasetName(t){let e=await this.mutex.acquire();try{for(let o of this.toolOutputs){let{data:s}=o;if(s&&typeof s=="object"&&!Array.isArray(s)){let r=s;if(t in r)return r[t]}}return null}finally{e()}}async getAllToolOutputs(){let t=await this.mutex.acquire();try{return[...this.toolOutputs]}finally{t()}}async clearAll(){let t=await this.mutex.acquire();try{this.toolOutputs=[]}finally{t()}}async hasToolOutputs(){let t=await this.mutex.acquire();try{return this.toolOutputs.length>0}finally{t()}}createOnToolCompletedCallback(){return async(t,e)=>{e!==void 0&&await this.addToolOutput(t,e)}}};var w=class{constructor(t={}){i(this,"conversationCaches",new Map);i(this,"conversationTimestamps",new Map);i(this,"config");this.config={maxConversations:t.maxConversations??100,ttlMs:t.ttlMs??1e3*60*60*2,cleanupProbability:t.cleanupProbability??.1,enableLogging:t.enableLogging??!1}}simpleHash(t){let e=0;for(let o=0;o<t.length;o++){let s=t.charCodeAt(o);e=(e<<5)-e+s,e=e&e}return Math.abs(e).toString(16).padStart(8,"0").substring(0,16)}async getToolOutputManager(t){Math.random()<this.config.cleanupProbability&&this.cleanupOldConversations();let e=this.conversationCaches.get(t);return e?this.config.enableLogging&&console.log(`ConversationCache: Reusing existing ToolOutputManager for conversation: ${t}`):(e=new m,this.conversationCaches.set(t,e),this.config.enableLogging&&console.log(`ConversationCache: Created new ToolOutputManager for conversation: ${t}`)),this.conversationTimestamps.set(t,Date.now()),e}cleanupOldConversations(){let t=Date.now();for(let[e,o]of this.conversationTimestamps.entries())t-o>this.config.ttlMs&&(this.conversationCaches.delete(e),this.conversationTimestamps.delete(e),this.config.enableLogging&&console.log(`ConversationCache: Cleaned up expired conversation: ${e}`));if(this.conversationCaches.size>this.config.maxConversations){let o=Array.from(this.conversationTimestamps.entries()).sort(([,s],[,r])=>s-r).slice(0,this.conversationCaches.size-this.config.maxConversations);for(let[s]of o)this.conversationCaches.delete(s),this.conversationTimestamps.delete(s),this.config.enableLogging&&console.log(`ConversationCache: Cleaned up oldest conversation: ${s}`)}}async getStatus(){let t=Date.now(),e=[];for(let[o,s]of this.conversationTimestamps.entries()){let r=this.conversationCaches.get(o),c=r?(await r.getAllToolOutputs()).length:0;e.push({id:o,ageMinutes:Math.round((t-s)/(1e3*60)),hasCache:this.conversationCaches.has(o),toolOutputCount:c})}return{totalConversations:this.conversationCaches.size,conversations:e,config:{maxConversations:this.config.maxConversations,ttlHours:this.config.ttlMs/(1e3*60*60),cleanupProbability:this.config.cleanupProbability}}}clearAll(){this.conversationCaches.clear(),this.conversationTimestamps.clear(),this.config.enableLogging&&console.log("ConversationCache: Cleared all conversations")}getConfig(){return{...this.config}}};var p=require("zod");function $(n){return{name:n.name,description:n.description,inputSchema:n.parameters,outputSchema:p.z.object({success:p.z.boolean(),result:p.z.string(),error:p.z.string().optional()}),execute:async(t,e)=>{let{toolCallId:o}=e;try{let s=await n.execute(t,{...e,context:n.context}),{additionalData:r,llmResult:c}=s;return r&&o&&n.onToolCompleted&&n.onToolCompleted(o,r),c}catch(s){return console.error(`Execute tool failed: ${s}`),{success:!1,error:`Execute tool failed: ${s}`}}}}}function G(n){return{name:n.name,description:n.description,parameters:n.parameters,execute:async(t,e)=>{let{toolCallId:o}=e;try{let s=await n.execute(t,{...e,context:n.context}),{additionalData:r,llmResult:c}=s;return r&&o&&n.onToolCompleted&&n.onToolCompleted(o,r),c}catch(s){return console.error(`Execute tool failed: ${s}`),{success:!1,error:`Execute tool failed: ${s}`}}}}}function j(n){return{name:n.name,description:n.description,schema:n.parameters,func:async t=>{try{let e=await n.execute(t,{toolCallId:"langchain",context:n.context&&typeof n.context=="object"?{...n.context}:{}}),{additionalData:o,llmResult:s}=e;return o&&n.onToolCompleted&&n.onToolCompleted("langchain",o),s}catch(e){throw new Error(`Execute tool failed: ${e}`)}}}}function B(){return Math.random().toString(36).substring(2,15)}0&&(module.exports={ConversationCache,ToolCache,ToolOutputManager,cacheData,convertToLangchainTool,convertToVercelAiTool,convertToVercelAiToolV5,formatNumberOrString,generateId,getBoundsFromGeoJSON,getCachedData,getValuesFromGeoJSON,numericFormatter,removeCachedData});
//# sourceMappingURL=index.cjs.js.map
