@ax-llm/ax 19.0.15 → 19.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -1,67 +1,67 @@
1
- "use strict";var cc=Object.create;var zr=Object.defineProperty;var pc=Object.getOwnPropertyDescriptor;var dc=Object.getOwnPropertyNames;var mc=Object.getPrototypeOf,gc=Object.prototype.hasOwnProperty;var hc=(i,e)=>{for(var t in e)zr(i,t,{get:e[t],enumerable:!0})},ra=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of dc(e))!gc.call(i,r)&&r!==t&&zr(i,r,{get:()=>e[r],enumerable:!(n=pc(e,r))||n.enumerable});return i};var er=(i,e,t)=>(t=i!=null?cc(mc(i)):{},ra(e||!i||!i.__esModule?zr(t,"default",{value:i,enumerable:!0}):t,i)),fc=i=>ra(zr({},"__esModule",{value:!0}),i);var zd={};hc(zd,{AxACE:()=>ss,AxACEOptimizedProgram:()=>Lr,AxAI:()=>br,AxAIAnthropic:()=>rn,AxAIAnthropicModel:()=>rr,AxAIAnthropicVertexModel:()=>or,AxAIAzureOpenAI:()=>un,AxAICohere:()=>cn,AxAICohereEmbedModel:()=>lr,AxAICohereModel:()=>ar,AxAIDeepSeek:()=>pn,AxAIDeepSeekModel:()=>ur,AxAIGoogleGemini:()=>dn,AxAIGoogleGeminiEmbedModel:()=>ao,AxAIGoogleGeminiEmbedTypes:()=>Ws,AxAIGoogleGeminiModel:()=>cr,AxAIGoogleGeminiSafetyCategory:()=>lo,AxAIGoogleGeminiSafetyThreshold:()=>uo,AxAIGrok:()=>wn,AxAIGrokEmbedModels:()=>ii,AxAIGrokModel:()=>yr,AxAIGroq:()=>gn,AxAIGroqModel:()=>pr,AxAIHuggingFace:()=>hn,AxAIHuggingFaceModel:()=>ho,AxAIMistral:()=>fn,AxAIMistralEmbedModels:()=>Xs,AxAIMistralModel:()=>dr,AxAIOllama:()=>xn,AxAIOpenAI:()=>ln,AxAIOpenAIBase:()=>De,AxAIOpenAIEmbedModel:()=>on,AxAIOpenAIModel:()=>ir,AxAIOpenAIResponses:()=>yn,AxAIOpenAIResponsesBase:()=>mr,AxAIOpenAIResponsesImpl:()=>An,AxAIOpenAIResponsesModel:()=>sn,AxAIOpenRouter:()=>bn,AxAIRefusalError:()=>Me,AxAIReka:()=>In,AxAIRekaModel:()=>hr,AxAIServiceAbortedError:()=>Le,AxAIServiceAuthenticationError:()=>Pt,AxAIServiceError:()=>Je,AxAIServiceNetworkError:()=>He,AxAIServiceResponseError:()=>Et,AxAIServiceStatusError:()=>tt,AxAIServiceStreamTerminatedError:()=>lt,AxAIServiceTimeoutError:()=>st,AxAITogether:()=>Cn,AxAITogetherModel:()=>xr,AxAIWebLLM:()=>Tn,AxAIWebLLMModel:()=>Ar,AxAgent:()=>jr,AxApacheTika:()=>Jo,AxAssertionError:()=>wt,AxBalancer:()=>no,AxBaseAI:()=>Be,AxBaseOptimizer:()=>rt,AxBootstrapFewShot:()=>Hn,AxContentProcessingError:()=>yt,AxDB:()=>Oo,AxDBBase:()=>ct,AxDBCloudflare:()=>Rn,AxDBManager:()=>Mo,AxDBMemory:()=>Nt,AxDBPinecone:()=>Sn,AxDBWeaviate:()=>kn,AxDefaultCostTracker:()=>Fr,AxDefaultResultReranker:()=>Vo,AxDockerSession:()=>ps,AxEmbeddingAdapter:()=>ds,AxEvalUtil:()=>fu,AxFlow:()=>Gr,AxFlowDependencyAnalyzer:()=>Kn,AxFlowExecutionPlanner:()=>Vn,AxFlowSubContextImpl:()=>Yn,AxFlowTypedSubContextImpl:()=>us,AxFluentFieldType:()=>qe,AxFunctionError:()=>Sr,AxFunctionProcessor:()=>vr,AxGEPA:()=>qn,AxGEPAFlow:()=>is,AxGen:()=>Ee,AxGenerateError:()=>Er,AxHFDataLoader:()=>os,AxInstanceRegistry:()=>_n,AxJSRuntime:()=>Xt,AxJSRuntimePermission:()=>Wi,AxJudge:()=>Bn,AxLLMRequestTypeValues:()=>Ms,AxLearn:()=>rs,AxMCPClient:()=>hs,AxMCPHTTPSSETransport:()=>As,AxMCPStreambleHTTPTransport:()=>xs,AxMediaNotSupportedError:()=>ut,AxMemory:()=>vn,AxMiPRO:()=>ls,AxMockAIService:()=>Ao,AxMultiServiceRouter:()=>yo,AxOptimizedProgramImpl:()=>kt,AxProgram:()=>Dt,AxPromptTemplate:()=>Vt,AxProviderRouter:()=>To,AxRateLimiterTokenUsage:()=>mn,AxSignature:()=>Ge,AxSignatureBuilder:()=>Or,AxSimpleClassifier:()=>Yo,AxSimpleClassifierClass:()=>Qo,AxSpanKindValues:()=>Es,AxStepContextImpl:()=>Dn,AxStopFunctionCallException:()=>Ht,AxStringUtil:()=>Wo,AxSynth:()=>zn,AxTestPrompt:()=>Xo,AxTokenLimitError:()=>tr,AxTraceLogger:()=>jn,agent:()=>oc,ai:()=>hl,ax:()=>Ye,axAIAnthropicDefaultConfig:()=>Us,axAIAnthropicVertexDefaultConfig:()=>ja,axAIAzureOpenAIBestConfig:()=>Ka,axAIAzureOpenAICreativeConfig:()=>za,axAIAzureOpenAIDefaultConfig:()=>js,axAIAzureOpenAIFastConfig:()=>Ha,axAICohereCreativeConfig:()=>Ya,axAICohereDefaultConfig:()=>Hs,axAIDeepSeekCodeConfig:()=>Xa,axAIDeepSeekDefaultConfig:()=>Ks,axAIGoogleGeminiDefaultConfig:()=>Js,axAIGoogleGeminiDefaultCreativeConfig:()=>nl,axAIGrokBestConfig:()=>gl,axAIGrokDefaultConfig:()=>ko,axAIHuggingFaceCreativeConfig:()=>rl,axAIHuggingFaceDefaultConfig:()=>Ys,axAIMistralBestConfig:()=>ol,axAIMistralDefaultConfig:()=>xo,axAIOllamaDefaultConfig:()=>Zs,axAIOllamaDefaultCreativeConfig:()=>sl,axAIOpenAIBestConfig:()=>Zr,axAIOpenAICreativeConfig:()=>eo,axAIOpenAIDefaultConfig:()=>jt,axAIOpenAIFastConfig:()=>to,axAIOpenAIResponsesBestConfig:()=>il,axAIOpenAIResponsesCreativeConfig:()=>al,axAIOpenAIResponsesDefaultConfig:()=>gr,axAIOpenRouterDefaultConfig:()=>ti,axAIRekaBestConfig:()=>cl,axAIRekaCreativeConfig:()=>pl,axAIRekaDefaultConfig:()=>fr,axAIRekaFastConfig:()=>dl,axAITogetherDefaultConfig:()=>ri,axAIWebLLMCreativeConfig:()=>ml,axAIWebLLMDefaultConfig:()=>si,axAnalyzeChatPromptRequirements:()=>ll,axAnalyzeRequestRequirements:()=>qt,axBaseAIDefaultConfig:()=>Te,axBaseAIDefaultCreativeConfig:()=>je,axBuildActorDefinition:()=>bs,axBuildResponderDefinition:()=>Is,axCheckMetricsHealth:()=>fl,axCreateDefaultColorLogger:()=>Os,axCreateDefaultOptimizerColorLogger:()=>_i,axCreateDefaultOptimizerTextLogger:()=>yu,axCreateDefaultTextLogger:()=>pa,axCreateFlowColorLogger:()=>Dr,axCreateFlowTextLogger:()=>ku,axCreateJSRuntime:()=>Uu,axDefaultFlowLogger:()=>vu,axDefaultMetricsConfig:()=>ci,axDefaultOptimizerLogger:()=>Pr,axDefaultOptimizerMetricsConfig:()=>Ni,axGetCompatibilityReport:()=>Wa,axGetFormatCompatibility:()=>Ja,axGetMetricsConfig:()=>Al,axGetOptimizerMetricsConfig:()=>Iu,axGetProvidersWithMediaSupport:()=>Va,axGlobals:()=>me,axModelInfoAnthropic:()=>sr,axModelInfoCohere:()=>so,axModelInfoDeepSeek:()=>io,axModelInfoGoogleGemini:()=>co,axModelInfoGrok:()=>So,axModelInfoGroq:()=>mo,axModelInfoHuggingFace:()=>go,axModelInfoMistral:()=>fo,axModelInfoOpenAI:()=>an,axModelInfoOpenAIResponses:()=>Xr,axModelInfoReka:()=>Co,axModelInfoTogether:()=>wo,axModelInfoWebLLM:()=>Ro,axProcessContentForProvider:()=>Io,axRAG:()=>lc,axScoreProvidersForRequest:()=>ro,axSelectOptimalProvider:()=>oo,axSpanAttributes:()=>he,axSpanEvents:()=>bt,axUpdateMetricsConfig:()=>xl,axUpdateOptimizerMetricsConfig:()=>bu,axValidateChatRequestMessage:()=>tn,axValidateChatResponseResult:()=>Yr,axValidateProviderCapabilities:()=>qs,axWorkerRuntime:()=>ms,f:()=>xe,flow:()=>cs,s:()=>Cu});module.exports=fc(zd);function at({model:i,modelInfo:e,models:t}){let n=t?.find(u=>u.key===i),r=n&&"model"in n?n.model:i,o=e.find(u=>u.name===i);if(o)return o;let s=e.find(u=>u.name===r);if(s)return s;let a=r.replace(/^(anthropic\.|openai\.)/,"").replace(/-latest$/,"").replace(/-\d{8}$/,"").replace(/-v\d+:\d+$/,"").replace(/@\d{8}$/,"").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/,"").replace(/-v\d+@\d{8}$/,"").replace(/-v\d+$/,""),l=e.find(u=>u.name===a);return l||null}var Ss=(()=>{if(globalThis.crypto&&typeof globalThis.crypto.randomUUID=="function")return globalThis.crypto;throw new Error("Web Crypto API with randomUUID support not available. Requires Node.js 16+ or modern browser.")})();function Ve(){return Ss.randomUUID()}async function xc(i){let e=new TextEncoder,t=typeof i=="string"?e.encode(i):i,n=await Ss.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(s=>s.toString(16).padStart(2,"0")).join("")}var Rs=class{data="";update(e){return this.data+=e,this}digest(e){if(e!=="hex")throw new Error("Only hex encoding is supported");let n=new TextEncoder().encode(this.data),r=0;for(let o=0;o<n.length;o++){let s=n[o];r=(r<<5)-r+s,r=r&r}return Math.abs(r).toString(16).padStart(8,"0")}async digestAsync(){return xc(this.data)}};function ot(i){if(i!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new Rs}function oa(){return Ss}var Hr=class extends TransformStream{buffer="";currentEvent={rawData:""};dataParser;onError;constructor(e={}){super({transform:(t,n)=>this.handleChunk(t,n),flush:t=>this.handleFlush(t)}),this.dataParser=e.dataParser||JSON.parse,this.onError=e.onError||((t,n)=>{console.warn("Failed to parse event data:",t),console.log("Raw data that failed to parse:",n)})}handleChunk(e,t){this.buffer+=e,this.processBuffer(t)}handleFlush(e){this.processBuffer(e),this.currentEvent.rawData&&this.processEvent(e)}processBuffer(e){let n=this.buffer.replace(/\r\n|\r/g,`
1
+ "use strict";var Hc=Object.create;var Br=Object.defineProperty;var Kc=Object.getOwnPropertyDescriptor;var Wc=Object.getOwnPropertyNames;var Vc=Object.getPrototypeOf,Jc=Object.prototype.hasOwnProperty;var Qc=(s,e)=>{for(var t in e)Br(s,t,{get:e[t],enumerable:!0})},ga=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Wc(e))!Jc.call(s,r)&&r!==t&&Br(s,r,{get:()=>e[r],enumerable:!(n=Kc(e,r))||n.enumerable});return s};var Yn=(s,e,t)=>(t=s!=null?Hc(Vc(s)):{},ga(e||!s||!s.__esModule?Br(t,"default",{value:s,enumerable:!0}):t,s)),Yc=s=>ga(Br({},"__esModule",{value:!0}),s);var Xm={};Qc(Xm,{AxACE:()=>Mn,AxACEOptimizedProgram:()=>Mr,AxAI:()=>xr,AxAIAnthropic:()=>Kt,AxAIAnthropicModel:()=>er,AxAIAnthropicVertexModel:()=>tr,AxAIAzureOpenAI:()=>Yt,AxAICohere:()=>Xt,AxAICohereEmbedModel:()=>sr,AxAICohereModel:()=>or,AxAIDeepSeek:()=>Zt,AxAIDeepSeekModel:()=>ir,AxAIGoogleGemini:()=>en,AxAIGoogleGeminiEmbedModel:()=>oo,AxAIGoogleGeminiEmbedTypes:()=>Ws,AxAIGoogleGeminiModel:()=>ar,AxAIGoogleGeminiSafetyCategory:()=>so,AxAIGoogleGeminiSafetyThreshold:()=>io,AxAIGrok:()=>mn,AxAIGrokEmbedModels:()=>ii,AxAIGrokModel:()=>hr,AxAIGroq:()=>nn,AxAIGroqModel:()=>ur,AxAIHuggingFace:()=>rn,AxAIHuggingFaceModel:()=>po,AxAIMistral:()=>on,AxAIMistralEmbedModels:()=>Xs,AxAIMistralModel:()=>lr,AxAIOllama:()=>sn,AxAIOpenAI:()=>Qt,AxAIOpenAIBase:()=>Se,AxAIOpenAIEmbedModel:()=>Wt,AxAIOpenAIModel:()=>rr,AxAIOpenAIResponses:()=>un,AxAIOpenAIResponsesBase:()=>cr,AxAIOpenAIResponsesImpl:()=>an,AxAIOpenAIResponsesModel:()=>Vt,AxAIOpenRouter:()=>ln,AxAIRefusalError:()=>Ce,AxAIReka:()=>cn,AxAIRekaModel:()=>dr,AxAIServiceAbortedError:()=>ke,AxAIServiceAuthenticationError:()=>xt,AxAIServiceError:()=>Ue,AxAIServiceNetworkError:()=>Le,AxAIServiceResponseError:()=>ht,AxAIServiceStatusError:()=>je,AxAIServiceStreamTerminatedError:()=>Ve,AxAIServiceTimeoutError:()=>Ke,AxAITogether:()=>pn,AxAITogetherModel:()=>gr,AxAIWebLLM:()=>dn,AxAIWebLLMModel:()=>fr,AxAgent:()=>Dr,AxApacheTika:()=>Wo,AxAssertionError:()=>ct,AxBalancer:()=>Zr,AxBaseAI:()=>Oe,AxBaseOptimizer:()=>et,AxBootstrapFewShot:()=>Nn,AxContentProcessingError:()=>ot,AxDB:()=>ko,AxDBBase:()=>Qe,AxDBCloudflare:()=>gn,AxDBManager:()=>So,AxDBMemory:()=>wt,AxDBPinecone:()=>fn,AxDBWeaviate:()=>hn,AxDefaultCostTracker:()=>Er,AxDefaultResultReranker:()=>Ko,AxDockerSession:()=>as,AxEmbeddingAdapter:()=>us,AxEvalUtil:()=>Ol,AxFlow:()=>_r,AxFlowDependencyAnalyzer:()=>Ln,AxFlowExecutionPlanner:()=>Dn,AxFlowSubContextImpl:()=>Bn,AxFlowTypedSubContextImpl:()=>ss,AxFluentFieldType:()=>Ne,AxFunctionError:()=>Tr,AxFunctionProcessor:()=>Rr,AxGEPA:()=>Fn,AxGen:()=>Ie,AxGenerateError:()=>vr,AxHFDataLoader:()=>ns,AxInstanceRegistry:()=>Cn,AxJSRuntime:()=>Ut,AxJSRuntimePermission:()=>Xi,AxJudge:()=>On,AxLLMRequestTypeValues:()=>Es,AxLearn:()=>ts,AxMCPClient:()=>ds,AxMCPHTTPSSETransport:()=>fs,AxMCPStreambleHTTPTransport:()=>gs,AxMediaNotSupportedError:()=>Je,AxMemory:()=>xn,AxMiPRO:()=>os,AxMockAIService:()=>fo,AxMultiServiceRouter:()=>ho,AxOptimizedProgramImpl:()=>mt,AxProgram:()=>vt,AxPromptTemplate:()=>$t,AxProviderRouter:()=>bo,AxRateLimiterTokenUsage:()=>tn,AxSignature:()=>Re,AxSignatureBuilder:()=>kr,AxSimpleClassifier:()=>Jo,AxSimpleClassifierClass:()=>Vo,AxSpanKindValues:()=>Ms,AxStepContextImpl:()=>kn,AxStopFunctionCallException:()=>_t,AxStringUtil:()=>Ho,AxSynth:()=>_n,AxTestPrompt:()=>Qo,AxTokenLimitError:()=>Xn,AxTraceLogger:()=>En,agent:()=>Fc,ai:()=>ku,ax:()=>qe,axAIAnthropicDefaultConfig:()=>Us,axAIAnthropicVertexDefaultConfig:()=>Za,axAIAzureOpenAIBestConfig:()=>ru,axAIAzureOpenAICreativeConfig:()=>tu,axAIAzureOpenAIDefaultConfig:()=>js,axAIAzureOpenAIFastConfig:()=>nu,axAICohereCreativeConfig:()=>uu,axAICohereDefaultConfig:()=>Hs,axAIDeepSeekCodeConfig:()=>lu,axAIDeepSeekDefaultConfig:()=>Ks,axAIGoogleGeminiDefaultConfig:()=>Js,axAIGoogleGeminiDefaultCreativeConfig:()=>mu,axAIGrokBestConfig:()=>Ru,axAIGrokDefaultConfig:()=>wo,axAIHuggingFaceCreativeConfig:()=>gu,axAIHuggingFaceDefaultConfig:()=>Ys,axAIMistralBestConfig:()=>fu,axAIMistralDefaultConfig:()=>go,axAIOllamaDefaultConfig:()=>Zs,axAIOllamaDefaultCreativeConfig:()=>hu,axAIOpenAIBestConfig:()=>Qr,axAIOpenAICreativeConfig:()=>Yr,axAIOpenAIDefaultConfig:()=>Mt,axAIOpenAIFastConfig:()=>Xr,axAIOpenAIResponsesBestConfig:()=>xu,axAIOpenAIResponsesCreativeConfig:()=>Au,axAIOpenAIResponsesDefaultConfig:()=>pr,axAIOpenRouterDefaultConfig:()=>ti,axAIRekaBestConfig:()=>Iu,axAIRekaCreativeConfig:()=>Cu,axAIRekaDefaultConfig:()=>mr,axAIRekaFastConfig:()=>Tu,axAITogetherDefaultConfig:()=>ri,axAIWebLLMCreativeConfig:()=>wu,axAIWebLLMDefaultConfig:()=>si,axAnalyzeChatPromptRequirements:()=>yu,axAnalyzeRequestRequirements:()=>Pt,axBaseAIDefaultConfig:()=>ye,axBaseAIDefaultCreativeConfig:()=>_e,axBuildActorDefinition:()=>As,axBuildResponderDefinition:()=>ys,axCheckMetricsHealth:()=>Su,axCreateDefaultColorLogger:()=>Os,axCreateDefaultOptimizerColorLogger:()=>Ni,axCreateDefaultOptimizerTextLogger:()=>Fl,axCreateDefaultTextLogger:()=>Ca,axCreateFlowColorLogger:()=>Fr,axCreateFlowTextLogger:()=>jl,axCreateJSRuntime:()=>tc,axDefaultFlowLogger:()=>zl,axDefaultMetricsConfig:()=>ci,axDefaultOptimizerLogger:()=>Or,axDefaultOptimizerMetricsConfig:()=>Li,axGetCompatibilityReport:()=>ou,axGetFormatCompatibility:()=>iu,axGetMetricsConfig:()=>Ou,axGetOptimizerMetricsConfig:()=>Nl,axGetProvidersWithMediaSupport:()=>su,axGlobals:()=>re,axModelInfoAnthropic:()=>nr,axModelInfoCohere:()=>no,axModelInfoDeepSeek:()=>ro,axModelInfoGoogleGemini:()=>ao,axModelInfoGrok:()=>To,axModelInfoGroq:()=>lo,axModelInfoHuggingFace:()=>co,axModelInfoMistral:()=>mo,axModelInfoOpenAI:()=>Jt,axModelInfoOpenAIResponses:()=>Jr,axModelInfoReka:()=>yo,axModelInfoTogether:()=>Io,axModelInfoWebLLM:()=>Co,axProcessContentForProvider:()=>Ao,axRAG:()=>Dc,axScoreProvidersForRequest:()=>eo,axSelectOptimalProvider:()=>to,axSpanAttributes:()=>ae,axSpanEvents:()=>st,axUpdateMetricsConfig:()=>vu,axUpdateOptimizerMetricsConfig:()=>_l,axValidateChatRequestMessage:()=>qt,axValidateChatResponseResult:()=>Vr,axValidateProviderCapabilities:()=>zs,axWorkerRuntime:()=>cs,f:()=>ue,flow:()=>is,fn:()=>gl,s:()=>Ml});module.exports=Yc(Xm);function We({model:s,modelInfo:e,models:t}){let n=t?.find(l=>l.key===s),r=n&&"model"in n?n.model:s,o=e.find(l=>l.name===s);if(o)return o;let i=e.find(l=>l.name===r);if(i)return i;let a=r.replace(/^(anthropic\.|openai\.)/,"").replace(/-latest$/,"").replace(/-\d{8}$/,"").replace(/-v\d+:\d+$/,"").replace(/@\d{8}$/,"").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/,"").replace(/-v\d+@\d{8}$/,"").replace(/-v\d+$/,""),u=e.find(l=>l.name===a);return u||null}var ks=(()=>{if(globalThis.crypto&&typeof globalThis.crypto.randomUUID=="function")return globalThis.crypto;throw new Error("Web Crypto API with randomUUID support not available. Requires Node.js 16+ or modern browser.")})();function Ge(){return ks.randomUUID()}async function Xc(s){let e=new TextEncoder,t=typeof s=="string"?e.encode(s):s,n=await ks.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("")}var Rs=class{data="";update(e){return this.data+=e,this}digest(e){if(e!=="hex")throw new Error("Only hex encoding is supported");let n=new TextEncoder().encode(this.data),r=0;for(let o=0;o<n.length;o++){let i=n[o];r=(r<<5)-r+i,r=r&r}return Math.abs(r).toString(16).padStart(8,"0")}async digestAsync(){return Xc(this.data)}};function He(s){if(s!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new Rs}function fa(){return ks}var jr=class extends TransformStream{buffer="";currentEvent={rawData:""};dataParser;onError;constructor(e={}){super({transform:(t,n)=>this.handleChunk(t,n),flush:t=>this.handleFlush(t)}),this.dataParser=e.dataParser||JSON.parse,this.onError=e.onError||((t,n)=>{console.warn("Failed to parse event data:",t),console.log("Raw data that failed to parse:",n)})}handleChunk(e,t){this.buffer+=e,this.processBuffer(t)}handleFlush(e){this.processBuffer(e),this.currentEvent.rawData&&this.processEvent(e)}processBuffer(e){let n=this.buffer.replace(/\r\n|\r/g,`
2
2
  `).split(`
3
3
  `);this.buffer=n.pop()||"";for(let r of n)r===""?this.processEvent(e):this.parseLine(r)}parseLine(e){if(e.startsWith(":"))return;let t=e.indexOf(":");if(t===-1){this.currentEvent.rawData+=(this.currentEvent.rawData&&!this.currentEvent.rawData.endsWith(`
4
4
  `)?`
5
5
  `:"")+e.trim();return}let n=e.slice(0,t).trim(),r=e.slice(t+1).trim();switch(n){case"event":this.currentEvent.event=r;break;case"data":this.currentEvent.rawData+=(this.currentEvent.rawData&&!this.currentEvent.rawData.endsWith(`
6
6
  `)?`
7
- `:"")+r;break;case"id":this.currentEvent.id=r;break;case"retry":{let o=Number.parseInt(r,10);Number.isNaN(o)||(this.currentEvent.retry=o);break}}}processEvent(e){if(this.currentEvent.rawData){if(this.currentEvent.event||(this.currentEvent.event="message"),this.currentEvent.rawData.trim()==="[DONE]"){this.currentEvent={rawData:""};return}try{let t=this.dataParser(this.currentEvent.rawData);e.enqueue(t)}catch(t){this.onError(t,this.currentEvent.rawData)}this.currentEvent={rawData:""}}}};var ks=class{decoder;constructor(){this.decoder=new TextDecoder}transform(e,t){if(!(e instanceof ArrayBuffer||ArrayBuffer.isView(e)))throw new TypeError("Input data must be a BufferSource");let n=this.decoder.decode(e,{stream:!0});n.length!==0&&t.enqueue(n)}flush(e){let t=this.decoder.decode();t.length!==0&&e.enqueue(t)}},Kr=class extends TransformStream{constructor(){super(new ks)}};var Ac={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},yc=globalThis.TextDecoderStream??Kr,Je=class extends Error{constructor(t,n,r,o,s={},a=!0){super(t);this.url=n;this.requestBody=r;this.responseBody=o;this.name="AxAIServiceError",this.timestamp=new Date().toISOString(),this.errorId=Ve(),this.context=s,this.includeRequestBodyInErrors=a,this.stack=this.toString()}timestamp;errorId;context;includeRequestBodyInErrors;toString(){let t=[`${this.name}: ${this.message}`,`URL: ${this.url}`];return this.includeRequestBodyInErrors&&t.push(`Request Body: ${JSON.stringify(this.requestBody,null,2)}`),t.push(`Response Body: ${JSON.stringify(this.responseBody,null,2)}`,`Context: ${JSON.stringify(this.context,null,2)}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`),t.join(`
8
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},tt=class extends Je{constructor(t,n,r,o,s,a,l,u=!0){let c=l?` (after ${l} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:s,...a},{},u);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},He=class extends Je{constructor(t,n,r,o,s,a=!0){super(`Network Error: ${t.message}`,n,r,o,{originalErrorName:t.name,originalErrorStack:t.stack,...s},a);this.originalError=t;this.name="AxAIServiceNetworkError",this.stack=t.stack}},Et=class extends Je{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},lt=class extends Je{constructor(t,n,r,o,s=!0){super("Stream terminated unexpectedly by remote host",t,n,void 0,{lastChunk:r,...o},s);this.lastChunk=r;this.name="AxAIServiceStreamTerminatedError"}},st=class extends Je{constructor(e,t,n,r,o=!0){super(`Request timed out after ${t}ms`,e,n,void 0,{timeoutMs:t,...r},o),this.name="AxAIServiceTimeoutError"}},tr=class extends tt{constructor(e,t,n,r,o,s,a=!0){super(e,t,n,r,o,s,void 0,a),this.name="AxTokenLimitError"}},Le=class extends Je{constructor(e,t,n,r,o=!0){super(`Request aborted${t?`: ${t}`:""}`,e,n,void 0,{abortReason:t,...r},o),this.name="AxAIServiceAbortedError"}},Pt=class extends Je{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Me=class extends Error{constructor(t,n,r){super(`Model refused to fulfill request: ${t}`);this.refusalMessage=t;this.model=n;this.requestId=r;this.name="AxAIRefusalError",this.timestamp=new Date().toISOString(),this.errorId=Ve()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Refusal: ${this.refusalMessage}`,this.model?`Model: ${this.model}`:"",this.requestId?`Request ID: ${this.requestId}`:"",`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].filter(Boolean).join(`
9
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},ut=class extends Error{constructor(t,n,r=!1){super(`${t} not supported by ${n}${r?" (fallback available)":""}`);this.mediaType=t;this.provider=n;this.fallbackAvailable=r;this.name="AxMediaNotSupportedError",this.timestamp=new Date().toISOString(),this.errorId=Ve()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Media Type: ${this.mediaType}`,`Provider: ${this.provider}`,`Fallback Available: ${this.fallbackAvailable}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].join(`
10
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},yt=class extends Error{constructor(t,n,r){super(`Failed to process ${n} during ${r}: ${t.message}`);this.originalError=t;this.contentType=n;this.processingStep=r;this.name="AxContentProcessingError",this.timestamp=new Date().toISOString(),this.errorId=Ve()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Content Type: ${this.contentType}`,`Processing Step: ${this.processingStep}`,`Original Error: ${this.originalError.message}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].join(`
11
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}};async function vs(i){try{return i.headers.get("content-type")?.includes("application/json")?await i.json():await i.clone().text()}catch(e){return`[ReadableStream - read failed: ${e.message}]`}}function sa(i,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**i)*(.75+Math.random()*.5)}function bc(i){if(!i)return;let e=Number(i);if(!Number.isNaN(e))return e*1e3;let t=Date.parse(i);if(!Number.isNaN(t)){let n=t-Date.now();return Math.max(0,n)}}function Ic(){return{startTime:Date.now(),retryCount:0}}function ia(i){i.retryCount++,i.lastRetryTime=Date.now()}function aa(i,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:i instanceof He&&!(i instanceof Pt)}var Qe=async(i,e)=>{if(i.localCall)return await i.localCall(e,i.stream);if(!i.url)throw new Error("API URL is required when localCall is not provided");let t={...Ac,...i.retry},n=i.timeout,r=Ic(),o=i.verbose??!1,s=i.includeRequestBodyInErrors??!0,a,l=new URL(i.url),u=`${[l.pathname,i.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${l.search}`,c=new URL(u,l);if(i.corsProxy){let m=c.href;c=new URL(`${i.corsProxy}?url=${encodeURIComponent(m)}`)}let p=Ve();if(i.validateRequest&&!await i.validateRequest(e))throw new Et("Invalid request data",c.href,e,{validation:"request"},s);i.span?.setAttributes({"http.request.method":i.put?"PUT":"POST","url.full":c.href,"request.id":p,"request.startTime":r.startTime});let d=0;for(;;){let m=new AbortController;if(i.abortSignal){if(i.abortSignal.aborted)throw new Le(c.href,i.abortSignal.reason,e,{metrics:r},s);let g=()=>{m.abort(i.abortSignal.reason||"User aborted request")};i.abortSignal.addEventListener("abort",g,{once:!0});let h=m.abort.bind(m);m.abort=f=>{i.abortSignal.removeEventListener("abort",g),h(f)}}n&&(a=setTimeout(()=>{m.abort("Request timeout")},n));try{o&&console.log(`
7
+ `:"")+r;break;case"id":this.currentEvent.id=r;break;case"retry":{let o=Number.parseInt(r,10);Number.isNaN(o)||(this.currentEvent.retry=o);break}}}processEvent(e){if(this.currentEvent.rawData){if(this.currentEvent.event||(this.currentEvent.event="message"),this.currentEvent.rawData.trim()==="[DONE]"){this.currentEvent={rawData:""};return}try{let t=this.dataParser(this.currentEvent.rawData);e.enqueue(t)}catch(t){this.onError(t,this.currentEvent.rawData)}this.currentEvent={rawData:""}}}};var Ss=class{decoder;constructor(){this.decoder=new TextDecoder}transform(e,t){if(!(e instanceof ArrayBuffer||ArrayBuffer.isView(e)))throw new TypeError("Input data must be a BufferSource");let n=this.decoder.decode(e,{stream:!0});n.length!==0&&t.enqueue(n)}flush(e){let t=this.decoder.decode();t.length!==0&&e.enqueue(t)}},zr=class extends TransformStream{constructor(){super(new Ss)}};var Zc={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},ep=globalThis.TextDecoderStream??zr,Ue=class extends Error{constructor(t,n,r,o,i={},a=!0){super(t);this.url=n;this.requestBody=r;this.responseBody=o;this.name="AxAIServiceError",this.timestamp=new Date().toISOString(),this.errorId=Ge(),this.context=i,this.includeRequestBodyInErrors=a,this.stack=this.toString()}timestamp;errorId;context;includeRequestBodyInErrors;toString(){let t=[`${this.name}: ${this.message}`,`URL: ${this.url}`];return this.includeRequestBodyInErrors&&t.push(`Request Body: ${JSON.stringify(this.requestBody,null,2)}`),t.push(`Response Body: ${JSON.stringify(this.responseBody,null,2)}`,`Context: ${JSON.stringify(this.context,null,2)}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`),t.join(`
8
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},je=class extends Ue{constructor(t,n,r,o,i,a,u,l=!0){let c=u?` (after ${u} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:i,...a},{},l);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},Le=class extends Ue{constructor(t,n,r,o,i,a=!0){super(`Network Error: ${t.message}`,n,r,o,{originalErrorName:t.name,originalErrorStack:t.stack,...i},a);this.originalError=t;this.name="AxAIServiceNetworkError",this.stack=t.stack}},ht=class extends Ue{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},Ve=class extends Ue{constructor(t,n,r,o,i=!0){super("Stream terminated unexpectedly by remote host",t,n,void 0,{lastChunk:r,...o},i);this.lastChunk=r;this.name="AxAIServiceStreamTerminatedError"}},Ke=class extends Ue{constructor(e,t,n,r,o=!0){super(`Request timed out after ${t}ms`,e,n,void 0,{timeoutMs:t,...r},o),this.name="AxAIServiceTimeoutError"}},Xn=class extends je{constructor(e,t,n,r,o,i,a=!0){super(e,t,n,r,o,i,void 0,a),this.name="AxTokenLimitError"}},ke=class extends Ue{constructor(e,t,n,r,o=!0){super(`Request aborted${t?`: ${t}`:""}`,e,n,void 0,{abortReason:t,...r},o),this.name="AxAIServiceAbortedError"}},xt=class extends Ue{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Ce=class extends Error{constructor(t,n,r){super(`Model refused to fulfill request: ${t}`);this.refusalMessage=t;this.model=n;this.requestId=r;this.name="AxAIRefusalError",this.timestamp=new Date().toISOString(),this.errorId=Ge()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Refusal: ${this.refusalMessage}`,this.model?`Model: ${this.model}`:"",this.requestId?`Request ID: ${this.requestId}`:"",`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].filter(Boolean).join(`
9
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},Je=class extends Error{constructor(t,n,r=!1){super(`${t} not supported by ${n}${r?" (fallback available)":""}`);this.mediaType=t;this.provider=n;this.fallbackAvailable=r;this.name="AxMediaNotSupportedError",this.timestamp=new Date().toISOString(),this.errorId=Ge()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Media Type: ${this.mediaType}`,`Provider: ${this.provider}`,`Fallback Available: ${this.fallbackAvailable}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].join(`
10
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},ot=class extends Error{constructor(t,n,r){super(`Failed to process ${n} during ${r}: ${t.message}`);this.originalError=t;this.contentType=n;this.processingStep=r;this.name="AxContentProcessingError",this.timestamp=new Date().toISOString(),this.errorId=Ge()}timestamp;errorId;toString(){return[`${this.name}: ${this.message}`,`Content Type: ${this.contentType}`,`Processing Step: ${this.processingStep}`,`Original Error: ${this.originalError.message}`,`Timestamp: ${this.timestamp}`,`Error ID: ${this.errorId}`].join(`
11
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}};async function vs(s){try{return s.headers.get("content-type")?.includes("application/json")?await s.json():await s.clone().text()}catch(e){return`[ReadableStream - read failed: ${e.message}]`}}function ha(s,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**s)*(.75+Math.random()*.5)}function tp(s){if(!s)return;let e=Number(s);if(!Number.isNaN(e))return e*1e3;let t=Date.parse(s);if(!Number.isNaN(t)){let n=t-Date.now();return Math.max(0,n)}}function np(){return{startTime:Date.now(),retryCount:0}}function xa(s){s.retryCount++,s.lastRetryTime=Date.now()}function Aa(s,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:s instanceof Le&&!(s instanceof xt)}var Be=async(s,e)=>{if(s.localCall)return await s.localCall(e,s.stream);if(!s.url)throw new Error("API URL is required when localCall is not provided");let t={...Zc,...s.retry},n=s.timeout,r=np(),o=s.verbose??!1,i=s.includeRequestBodyInErrors??!0,a,u=new URL(s.url),l=`${[u.pathname,s.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${u.search}`,c=new URL(l,u);if(s.corsProxy){let m=c.href;c=new URL(`${s.corsProxy}?url=${encodeURIComponent(m)}`)}let p=Ge();if(s.validateRequest&&!await s.validateRequest(e))throw new ht("Invalid request data",c.href,e,{validation:"request"},i);s.span?.setAttributes({"http.request.method":s.put?"PUT":"POST","url.full":c.href,"request.id":p,"request.startTime":r.startTime});let d=0;for(;;){let m=new AbortController;if(s.abortSignal){if(s.abortSignal.aborted)throw new ke(c.href,s.abortSignal.reason,e,{metrics:r},i);let g=()=>{m.abort(s.abortSignal.reason||"User aborted request")};s.abortSignal.addEventListener("abort",g,{once:!0});let f=m.abort.bind(m);m.abort=x=>{s.abortSignal.removeEventListener("abort",g),f(x)}}n&&(a=setTimeout(()=>{m.abort("Request timeout")},n));try{o&&console.log(`
12
12
  --- [AxAI API Request] ---
13
13
  `,`URL: ${c.href}
14
- `,`Method: ${i.put?"PUT":"POST"}
15
- `,"Headers:",JSON.stringify({"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...i.headers},null,2),`
14
+ `,`Method: ${s.put?"PUT":"POST"}
15
+ `,"Headers:",JSON.stringify({"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...s.headers},null,2),`
16
16
  Body:`,JSON.stringify(e,null,2),`
17
17
  ------------------------
18
- `);let g=await(i.fetch??fetch)(c,{method:i.put?"PUT":"POST",headers:{"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...i.headers},body:JSON.stringify(e),signal:m.signal});if(a&&clearTimeout(a),g.status===401||g.status===403){let w=await vs(g);throw new Pt(c.href,e,w,{metrics:r},s)}if(g.status===400){let w=await vs(g),A=w,T=!1;if(A?.error?.code==="context_length_exceeded")T=!0;else if(A?.type==="invalid_request_error"&&(A?.error?.message?.includes("prompt is too long")||A?.error?.message?.includes("max_tokens")||A?.error?.message?.includes("token limit")))T=!0;else if(A?.error?.code===400&&A?.error?.status==="INVALID_ARGUMENT"&&(A?.error?.message?.includes("token")||A?.error?.message?.includes("limit")))T=!0;else{let k=JSON.stringify(w).toLowerCase();(k.includes("token")&&k.includes("limit")||k.includes("context length")||k.includes("prompt is too long"))&&(T=!0)}if(T)throw new tr(g.status,g.statusText,c.href,e,w,{metrics:r},s)}if(g.status>=400&&aa(new Error,g.status,d,t)){let w=sa(d,t),A=bc(g.headers.get("Retry-After"));A!==void 0&&A<=t.maxDelayMs&&(w=A,o&&console.log(`[AxAI] Respecting Retry-After header: ${w}ms`)),d++,ia(r),i.span?.addEvent("retry",{attempt:d,delay:w,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(T=>setTimeout(T,w));continue}if(g.status>=400){let w=await vs(g);throw new tt(g.status,g.statusText,c.href,e,w,{metrics:r},d>0?d:void 0,s)}if(!i.stream){let w=await g.json();if(o&&console.log(`
18
+ `);let g=await(s.fetch??fetch)(c,{method:s.put?"PUT":"POST",headers:{"Content-Type":"application/json","X-Request-ID":p,"X-Retry-Count":d.toString(),...s.headers},body:JSON.stringify(e),signal:m.signal});if(a&&clearTimeout(a),g.status===401||g.status===403){let b=await vs(g);throw new xt(c.href,e,b,{metrics:r},i)}if(g.status===400){let b=await vs(g),I=b,T=!1;if(I?.error?.code==="context_length_exceeded")T=!0;else if(I?.type==="invalid_request_error"&&(I?.error?.message?.includes("prompt is too long")||I?.error?.message?.includes("max_tokens")||I?.error?.message?.includes("token limit")))T=!0;else if(I?.error?.code===400&&I?.error?.status==="INVALID_ARGUMENT"&&(I?.error?.message?.includes("token")||I?.error?.message?.includes("limit")))T=!0;else{let R=JSON.stringify(b).toLowerCase();(R.includes("token")&&R.includes("limit")||R.includes("context length")||R.includes("prompt is too long"))&&(T=!0)}if(T)throw new Xn(g.status,g.statusText,c.href,e,b,{metrics:r},i)}if(g.status>=400&&Aa(new Error,g.status,d,t)){let b=ha(d,t),I=tp(g.headers.get("Retry-After"));I!==void 0&&I<=t.maxDelayMs&&(b=I,o&&console.log(`[AxAI] Respecting Retry-After header: ${b}ms`)),d++,xa(r),s.span?.addEvent("retry",{attempt:d,delay:b,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(T=>setTimeout(T,b));continue}if(g.status>=400){let b=await vs(g);throw new je(g.status,g.statusText,c.href,e,b,{metrics:r},d>0?d:void 0,i)}if(!s.stream){let b=await g.json();if(o&&console.log(`
19
19
  --- [AxAI API Response] ---
20
20
  `,`Status: ${g.status} ${g.statusText}
21
- `,"Body:",JSON.stringify(w,null,2),`
21
+ `,"Body:",JSON.stringify(b,null,2),`
22
22
  -------------------------
23
- `),i.validateResponse&&!await i.validateResponse(w))throw new Et("Invalid response data",c.href,e,{validation:"response"},s);return i.span?.setAttributes({"response.time":Date.now()-r.startTime,"response.retries":r.retryCount}),w}if(o&&console.log(`
23
+ `),s.validateResponse&&!await s.validateResponse(b))throw new ht("Invalid response data",c.href,e,{validation:"response"},i);return s.span?.setAttributes({"response.time":Date.now()-r.startTime,"response.retries":r.retryCount}),b}if(o&&console.log(`
24
24
  --- [AxAI API Streaming Response Started] ---
25
25
  `,`Status: ${g.status} ${g.statusText}
26
26
  `,`
27
27
  -------------------------------------------
28
- `),!g.body)throw new Et("Response body is null",c.href,e,{metrics:r},s);let h,f=0;if(typeof window<"u"&&typeof EventSource<"u")return new ReadableStream({start(w){let A=g.body.getReader(),T=new TextDecoder,k="";async function E(){try{for(;;){let{done:M,value:$}=await A.read();if(M){I=!0,w.close();break}k+=T.decode($,{stream:!0});let B=k.split(`
29
-
30
- `);k=B.pop()||"";for(let V of B){if(!V.trim())continue;let O=V.split(`
31
- `),j="",F="message";for(let W of O)W.startsWith("data: ")?j=W.slice(6):W.startsWith("event: ")&&(F=W.slice(7));if(j){if(j==="[DONE]"){w.close();return}try{let W=JSON.parse(j);h=W,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),w.enqueue(W),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":F})}catch(W){o&&console.warn("Skipping non-JSON SSE data:",j,W)}}}}}catch(M){let $=M,B={...r,streamDuration:Date.now()-r.startTime};$.name==="AbortError"||$.message?.includes("aborted")?w.error(new lt(c.href,e,h,{streamMetrics:B},s)):w.error(new He($,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:B},s))}finally{A.releaseLock()}}E()}});let x=new TransformStream({transform(w,A){h=w,f++,r.streamChunks=f,r.lastChunkTime=Date.now(),A.enqueue(w),i.span?.addEvent("stream.chunk",{"stream.chunks":f,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),I=!1;return new ReadableStream({start(w){let A=g.body.pipeThrough(new yc).pipeThrough(new Hr).pipeThrough(x).getReader();async function T(){try{for(;;){let{done:k,value:E}=await A.read();if(k){I||(I=!0,w.close());break}if(I)break;w.enqueue(E)}}catch(k){let E=k,M={...r,streamDuration:Date.now()-r.startTime};throw E.name==="AbortError"||E.message?.includes("aborted")?w.error(new lt(c.href,e,h,{streamMetrics:M},s)):E instanceof TypeError&&E.message.includes("cancelled")?w.error(new lt(c.href,e,h,{streamMetrics:M,cancelReason:"Stream cancelled by client"},s)):w.error(new He(E,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:M},s)),E}finally{a&&clearTimeout(a),A.releaseLock()}}T()},cancel(){I=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw i.abortSignal?.aborted?new Le(c.href,i.abortSignal.reason,e,{metrics:r},s):new st(c.href,n||0,e,{metrics:r},s);let h=g;if(!(g instanceof Je)&&g instanceof Error&&(h=new He(g,c.href,e,void 0,{metrics:r},s)),i.span?.isRecording()&&(i.span.recordException(h),i.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),h instanceof He&&aa(h,void 0,d,t)){let f=sa(d,t);d++,ia(r),i.span?.addEvent("retry",{attempt:d,delay:f,error:h.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(C=>setTimeout(C,f));continue}throw h instanceof Je&&(h.context.metrics=r),h}finally{a!==void 0&&clearTimeout(a)}}};var nn=require("@opentelemetry/api");var me={signatureStrict:!0,useStructuredPrompt:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,abortSignal:void 0,customLabels:void 0,cachingFunction:void 0,functionResultFormatter:i=>typeof i=="string"?i:i==null?"":JSON.stringify(i,null,2)};var nt=class{ANSI_WHITE_BRIGHT="\x1B[97m";ANSI_GREEN_BRIGHT="\x1B[92m";ANSI_BLUE_BRIGHT="\x1B[94m";ANSI_RED_BRIGHT="\x1B[91m";ANSI_YELLOW_BRIGHT="\x1B[93m";ANSI_YELLOW="\x1B[93m";ANSI_RED="\x1B[91m";ANSI_RESET="\x1B[0m";ANSI_ORANGE="\x1B[38;5;208m";ANSI_WHITE="\x1B[37m";ANSI_CYAN_BRIGHT="\x1B[96m";ANSI_MAGENTA_BRIGHT="\x1B[95m";ANSI_GRAY="\x1B[90m";ANSI_GREEN="\x1B[32m";ANSI_CYAN="\x1B[36m";ANSI_MAGENTA="\x1B[35m";ANSI_BLUE="\x1B[34m";ANSI_YELLOW_DIM="\x1B[33m";colorize(e,t){return`${t}${e}${this.ANSI_RESET}`}whiteBright(e){return this.colorize(e,this.ANSI_WHITE_BRIGHT)}greenBright(e){return this.colorize(e,this.ANSI_GREEN_BRIGHT)}blueBright(e){return this.colorize(e,this.ANSI_BLUE_BRIGHT)}redBright(e){return this.colorize(e,this.ANSI_RED_BRIGHT)}white(e){return this.colorize(e,this.ANSI_WHITE)}yellow(e){return this.colorize(e,this.ANSI_YELLOW)}yellowBright(e){return this.colorize(e,this.ANSI_YELLOW_BRIGHT)}red(e){return this.colorize(e,this.ANSI_RED)}orange(e){return this.colorize(e,this.ANSI_ORANGE)}cyanBright(e){return this.colorize(e,this.ANSI_CYAN_BRIGHT)}magentaBright(e){return this.colorize(e,this.ANSI_MAGENTA_BRIGHT)}gray(e){return this.colorize(e,this.ANSI_GRAY)}green(e){return this.colorize(e,this.ANSI_GREEN)}cyan(e){return this.colorize(e,this.ANSI_CYAN)}magenta(e){return this.colorize(e,this.ANSI_MAGENTA)}blue(e){return this.colorize(e,this.ANSI_BLUE)}yellowDim(e){return this.colorize(e,this.ANSI_YELLOW_DIM)}};var om=new nt,la=i=>{console.log(i)},ua=(i,e,t)=>{let n=(r,o)=>t&&o&&o in t?t[o](r):r;switch(i.role){case"system":return`${n("[ SYSTEM ]","magentaBright")}
32
- ${n(i.content,"magenta")}`;case"function":return`${n("[ FUNCTION RESULT ]","yellow")}
33
- ${n(i.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","greenBright")}
34
- `;if(typeof i.content=="string")return r+n(i.content,"green");let o=i.content.map(s=>{if(s.type==="text")return n(s.text,"green");if(s.type==="image"){let a=e?"[Image]":`[Image: ${s.image}]`;return n(a,"green")}if(s.type==="audio"){let a=e?"[Audio]":`[Audio: ${s.data}]`;return n(a,"green")}return n("[Unknown content type]","gray")});return r+o.join(`
35
- `)}case"assistant":{let r=n("[ ASSISTANT","cyanBright");i.name&&(r+=` ${i.name}`),r+=" ]";let o=`${r}
36
- `;return i.content&&(o+=`${n(i.content,"cyan")}
37
- `),i.functionCalls&&i.functionCalls.length>0&&(o+=`${n("[ FUNCTION CALLS ]","yellow")}
38
- `,i.functionCalls.forEach((s,a)=>{let l=typeof s.function.params=="string"?s.function.params:JSON.stringify(s.function.params,null,2);o+=n(`${a+1}. ${s.function.name}(${l}) [id: ${s.id}]`,"yellowDim"),a<(i.functionCalls?.length??0)-1&&(o+=`
28
+ `),!g.body)throw new ht("Response body is null",c.href,e,{metrics:r},i);let f,x=0;if(typeof window<"u"&&typeof EventSource<"u")return new ReadableStream({start(b){let I=g.body.getReader(),T=new TextDecoder,R="";async function k(){try{for(;;){let{done:v,value:O}=await I.read();if(v){A=!0,b.close();break}R+=T.decode(O,{stream:!0});let P=R.split(`
29
+
30
+ `);R=P.pop()||"";for(let $ of P){if(!$.trim())continue;let E=$.split(`
31
+ `),F="",_="message";for(let j of E)j.startsWith("data: ")?F=j.slice(6):j.startsWith("event: ")&&(_=j.slice(7));if(F){if(F==="[DONE]"){b.close();return}try{let j=JSON.parse(F);f=j,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),b.enqueue(j),s.span?.addEvent("stream.chunk",{"stream.chunks":x,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":_})}catch(j){o&&console.warn("Skipping non-JSON SSE data:",F,j)}}}}}catch(v){let O=v,P={...r,streamDuration:Date.now()-r.startTime};O.name==="AbortError"||O.message?.includes("aborted")?b.error(new Ve(c.href,e,f,{streamMetrics:P},i)):b.error(new Le(O,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:P},i))}finally{I.releaseLock()}}k()}});let h=new TransformStream({transform(b,I){f=b,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),I.enqueue(b),s.span?.addEvent("stream.chunk",{"stream.chunks":x,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),A=!1;return new ReadableStream({start(b){let I=g.body.pipeThrough(new ep).pipeThrough(new jr).pipeThrough(h).getReader();async function T(){try{for(;;){let{done:R,value:k}=await I.read();if(R){A||(A=!0,b.close());break}if(A)break;b.enqueue(k)}}catch(R){let k=R,v={...r,streamDuration:Date.now()-r.startTime};throw k.name==="AbortError"||k.message?.includes("aborted")?b.error(new Ve(c.href,e,f,{streamMetrics:v},i)):k instanceof TypeError&&k.message.includes("cancelled")?b.error(new Ve(c.href,e,f,{streamMetrics:v,cancelReason:"Stream cancelled by client"},i)):b.error(new Le(k,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:v},i)),k}finally{a&&clearTimeout(a),I.releaseLock()}}T()},cancel(){A=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw s.abortSignal?.aborted?new ke(c.href,s.abortSignal.reason,e,{metrics:r},i):new Ke(c.href,n||0,e,{metrics:r},i);let f=g;if(!(g instanceof Ue)&&g instanceof Error&&(f=new Le(g,c.href,e,void 0,{metrics:r},i)),s.span?.isRecording()&&(s.span.recordException(f),s.span.setAttributes({"error.time":Date.now()-r.startTime,"error.retries":r.retryCount})),f instanceof Le&&Aa(f,void 0,d,t)){let x=ha(d,t);d++,xa(r),s.span?.addEvent("retry",{attempt:d,delay:x,error:f.message,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(y=>setTimeout(y,x));continue}throw f instanceof Ue&&(f.context.metrics=r),f}finally{a!==void 0&&clearTimeout(a)}}};var Ht=require("@opentelemetry/api");var re={signatureStrict:!0,useStructuredPrompt:!0,tracer:void 0,meter:void 0,logger:void 0,optimizerLogger:void 0,debug:void 0,abortSignal:void 0,customLabels:void 0,cachingFunction:void 0,functionResultFormatter:s=>typeof s=="string"?s:s==null?"":JSON.stringify(s,null,2)};var ze=class{ANSI_WHITE_BRIGHT="\x1B[97m";ANSI_GREEN_BRIGHT="\x1B[92m";ANSI_BLUE_BRIGHT="\x1B[94m";ANSI_RED_BRIGHT="\x1B[91m";ANSI_YELLOW_BRIGHT="\x1B[93m";ANSI_YELLOW="\x1B[93m";ANSI_RED="\x1B[91m";ANSI_RESET="\x1B[0m";ANSI_ORANGE="\x1B[38;5;208m";ANSI_WHITE="\x1B[37m";ANSI_CYAN_BRIGHT="\x1B[96m";ANSI_MAGENTA_BRIGHT="\x1B[95m";ANSI_GRAY="\x1B[90m";ANSI_GREEN="\x1B[32m";ANSI_CYAN="\x1B[36m";ANSI_MAGENTA="\x1B[35m";ANSI_BLUE="\x1B[34m";ANSI_YELLOW_DIM="\x1B[33m";colorize(e,t){return`${t}${e}${this.ANSI_RESET}`}whiteBright(e){return this.colorize(e,this.ANSI_WHITE_BRIGHT)}greenBright(e){return this.colorize(e,this.ANSI_GREEN_BRIGHT)}blueBright(e){return this.colorize(e,this.ANSI_BLUE_BRIGHT)}redBright(e){return this.colorize(e,this.ANSI_RED_BRIGHT)}white(e){return this.colorize(e,this.ANSI_WHITE)}yellow(e){return this.colorize(e,this.ANSI_YELLOW)}yellowBright(e){return this.colorize(e,this.ANSI_YELLOW_BRIGHT)}red(e){return this.colorize(e,this.ANSI_RED)}orange(e){return this.colorize(e,this.ANSI_ORANGE)}cyanBright(e){return this.colorize(e,this.ANSI_CYAN_BRIGHT)}magentaBright(e){return this.colorize(e,this.ANSI_MAGENTA_BRIGHT)}gray(e){return this.colorize(e,this.ANSI_GRAY)}green(e){return this.colorize(e,this.ANSI_GREEN)}cyan(e){return this.colorize(e,this.ANSI_CYAN)}magenta(e){return this.colorize(e,this.ANSI_MAGENTA)}blue(e){return this.colorize(e,this.ANSI_BLUE)}yellowDim(e){return this.colorize(e,this.ANSI_YELLOW_DIM)}};var pg=new ze,ya=s=>{console.log(s)},ba=(s,e,t)=>{let n=(r,o)=>t&&o&&o in t?t[o](r):r;switch(s.role){case"system":return`${n("[ SYSTEM ]","magentaBright")}
32
+ ${n(s.content,"magenta")}`;case"function":return`${n("[ FUNCTION RESULT ]","yellow")}
33
+ ${n(s.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","greenBright")}
34
+ `;if(typeof s.content=="string")return r+n(s.content,"green");let o=s.content.map(i=>{if(i.type==="text")return n(i.text,"green");if(i.type==="image"){let a=e?"[Image]":`[Image: ${i.image}]`;return n(a,"green")}if(i.type==="audio"){let a=e?"[Audio]":`[Audio: ${i.data}]`;return n(a,"green")}return n("[Unknown content type]","gray")});return r+o.join(`
35
+ `)}case"assistant":{let r=n("[ ASSISTANT","cyanBright");s.name&&(r+=` ${s.name}`),r+=" ]";let o=`${r}
36
+ `;return s.content&&(o+=`${n(s.content,"cyan")}
37
+ `),s.functionCalls&&s.functionCalls.length>0&&(o+=`${n("[ FUNCTION CALLS ]","yellow")}
38
+ `,s.functionCalls.forEach((i,a)=>{let u=typeof i.function.params=="string"?i.function.params:JSON.stringify(i.function.params,null,2);o+=n(`${a+1}. ${i.function.name}(${u}) [id: ${i.id}]`,"yellowDim"),a<(s.functionCalls?.length??0)-1&&(o+=`
39
39
  `)}),o+=`
40
- `),!i.content&&(!i.functionCalls||i.functionCalls.length===0)&&(o+=n("[No content]","gray")),o}default:return`${n("[ UNKNOWN ]","redBright")}
41
- ${n(JSON.stringify(i),"gray")}`}},Os=(i=la)=>{let e=new nt,t=e.gray(`${"\u2500".repeat(60)}
40
+ `),!s.content&&(!s.functionCalls||s.functionCalls.length===0)&&(o+=n("[No content]","gray")),o}default:return`${n("[ UNKNOWN ]","redBright")}
41
+ ${n(JSON.stringify(s),"gray")}`}},Os=(s=ya)=>{let e=new ze,t=e.gray(`${"\u2500".repeat(60)}
42
42
  `);return n=>{let r=n,o="";switch(r.name){case"ChatRequestChatPrompt":o=`
43
43
  ${e.blueBright(`[ CHAT REQUEST Step ${r.step} ]`)}
44
44
  ${t}
45
- `,r.value.forEach((s,a)=>{o+=ua(s,void 0,e),a<r.value.length-1&&(o+=`
45
+ `,r.value.forEach((i,a)=>{o+=ba(i,void 0,e),a<r.value.length-1&&(o+=`
46
46
  ${t}
47
47
  `)}),o+=`
48
48
  ${t}`;break;case"FunctionResults":o=`
49
49
  ${e.yellow("[ FUNCTION RESULTS ]")}
50
- `,r.value.forEach((s,a)=>{o+=e.yellowDim(`Function: ${s.functionId}
51
- Result: ${s.result}`),a<r.value.length-1&&(o+=`
50
+ `,r.value.forEach((i,a)=>{o+=e.yellowDim(`Function: ${i.functionId}
51
+ Result: ${i.result}`),a<r.value.length-1&&(o+=`
52
52
  ${t}
53
53
  `)});break;case"ChatResponseResults":o=`
54
54
  ${e.cyanBright("[ CHAT RESPONSE ]")}
55
- `,r.value.forEach((s,a)=>{let l=[],u=s.thoughtBlocks?.some(c=>c.encrypted);s.thought&&l.push(e.gray(`[THOUGHT${u?" (redacted)":""}]
56
- ${s.thought}`)),s.content&&l.push(e.cyan(s.content)),l.length===0&&l.push(e.gray("[No content]")),o+=l.join(`
55
+ `,r.value.forEach((i,a)=>{let u=[],l=i.thoughtBlocks?.some(c=>c.encrypted);i.thought&&u.push(e.gray(`[THOUGHT${l?" (redacted)":""}]
56
+ ${i.thought}`)),i.content&&u.push(e.cyan(i.content)),u.length===0&&u.push(e.gray("[No content]")),o+=u.join(`
57
57
  `),a<r.value.length-1&&(o+=`
58
58
  ${t}
59
- `)});break;case"ChatResponseStreamingResult":{let s=r.value.thought,a=s||r.value.delta||r.value.content||"";o=s?e.gray(`[THOUGHT]
60
- ${s}`):e.cyanBright(a);break}case"ChatResponseStreamingDoneResult":{o=`
59
+ `)});break;case"ChatResponseStreamingResult":{let i=r.value.thought,a=i||r.value.delta||r.value.content||"";o=i?e.gray(`[THOUGHT]
60
+ ${i}`):e.cyanBright(a);break}case"ChatResponseStreamingDoneResult":{o=`
61
61
  ${e.cyanBright("[ CHAT RESPONSE ]")}
62
62
  ${t}
63
- `,r.value.content&&(o+=e.cyanBright(r.value.content));let s=r.value.thoughtBlocks?.some(a=>a.encrypted);r.value.thought&&(o+=`
64
- `,o+=e.gray(`[THOUGHT${s?" (redacted)":""}]
63
+ `,r.value.content&&(o+=e.cyanBright(r.value.content));let i=r.value.thoughtBlocks?.some(a=>a.encrypted);r.value.thought&&(o+=`
64
+ `,o+=e.gray(`[THOUGHT${i?" (redacted)":""}]
65
65
  `+r.value.thought)),r.value.functionCalls&&(o+=e.cyanBright(JSON.stringify(r.value.functionCalls,null,2)));break}case"FunctionError":o=`
66
66
  ${e.redBright(`[ FUNCTION ERROR #${r.index} ]`)}
67
67
  ${t}
@@ -80,48 +80,48 @@ ${e.green(`Selected sample ${r.selectedIndex+1} of ${r.sampleCount} (${r.latency
80
80
  ${t}
81
81
  ${e.white(r.value)}`;break;case"EmbedRequest":o=`${e.orange(`[ EMBED REQUEST ${r.embedModel} ]`)}
82
82
  ${t}
83
- `,r.value.forEach((s,a)=>{o+=e.white(`Text ${a+1}: ${s.substring(0,100)}${s.length>100?"...":""}`),a<r.value.length-1&&(o+=`
83
+ `,r.value.forEach((i,a)=>{o+=e.white(`Text ${a+1}: ${i.substring(0,100)}${i.length>100?"...":""}`),a<r.value.length-1&&(o+=`
84
84
  ${t}
85
85
  `)});break;case"EmbedResponse":o=`${e.orange(`[ EMBED RESPONSE (${r.totalEmbeddings} embeddings) ]`)}
86
86
  ${t}
87
- `,r.value.forEach((s,a)=>{o+=e.white(`Embedding ${a+1}: [${s.sample.join(", ")}${s.truncated?", ...":""}] (length: ${s.length})`),a<r.value.length-1&&(o+=`
87
+ `,r.value.forEach((i,a)=>{o+=e.white(`Embedding ${a+1}: [${i.sample.join(", ")}${i.truncated?", ...":""}] (length: ${i.length})`),a<r.value.length-1&&(o+=`
88
88
  ${t}
89
89
  `)});break;case"ChatResponseUsage":{o=`${e.greenBright(`
90
90
  [ CHAT RESPONSE USAGE ]`)}
91
- `;let s=r.value;o+=`${e.white("AI:")} ${s.ai}
92
- `,o+=`${e.white("Model:")} ${s.model}
93
- `,s.tokens&&(o+=`${e.white("Total Tokens:")} ${s.tokens.totalTokens}
94
- `,o+=`${e.white("Prompt Tokens:")} ${s.tokens.promptTokens}
95
- `,o+=`${e.white("Completion Tokens:")} ${s.tokens.completionTokens}
96
- `,s.tokens.thoughtsTokens!==void 0&&(o+=`${e.white("Thoughts Tokens:")} ${s.tokens.thoughtsTokens}
97
- `),s.tokens.reasoningTokens!==void 0&&(o+=`${e.white("Reasoning Tokens:")} ${s.tokens.reasoningTokens}
98
- `),s.tokens.cacheCreationTokens!==void 0&&(o+=`${e.white("Cache Creation Tokens:")} ${s.tokens.cacheCreationTokens}
99
- `),s.tokens.cacheReadTokens!==void 0&&(o+=`${e.white("Cache Read Tokens:")} ${s.tokens.cacheReadTokens}
100
- `),s.tokens.serviceTier!==void 0&&(o+=`${e.white("Service Tier:")} ${s.tokens.serviceTier}
91
+ `;let i=r.value;o+=`${e.white("AI:")} ${i.ai}
92
+ `,o+=`${e.white("Model:")} ${i.model}
93
+ `,i.tokens&&(o+=`${e.white("Total Tokens:")} ${i.tokens.totalTokens}
94
+ `,o+=`${e.white("Prompt Tokens:")} ${i.tokens.promptTokens}
95
+ `,o+=`${e.white("Completion Tokens:")} ${i.tokens.completionTokens}
96
+ `,i.tokens.thoughtsTokens!==void 0&&(o+=`${e.white("Thoughts Tokens:")} ${i.tokens.thoughtsTokens}
97
+ `),i.tokens.reasoningTokens!==void 0&&(o+=`${e.white("Reasoning Tokens:")} ${i.tokens.reasoningTokens}
98
+ `),i.tokens.cacheCreationTokens!==void 0&&(o+=`${e.white("Cache Creation Tokens:")} ${i.tokens.cacheCreationTokens}
99
+ `),i.tokens.cacheReadTokens!==void 0&&(o+=`${e.white("Cache Read Tokens:")} ${i.tokens.cacheReadTokens}
100
+ `),i.tokens.serviceTier!==void 0&&(o+=`${e.white("Service Tier:")} ${i.tokens.serviceTier}
101
101
  `)),o+=t;break}case"ChatResponseCitations":{o=`${e.blueBright(`
102
102
  [ CHAT RESPONSE CITATIONS ]`)}
103
- `,r.value.forEach(s=>{o+=`${e.white("- ")}${e.cyan(s.title||s.url)}
104
- `,s.description&&(o+=` ${e.gray(s.description)}
105
- `)}),o+=t;break}default:o=e.gray(JSON.stringify(r,null,2))}i(o)}},ca=Os(),pa=(i=la)=>{let e="\u2500".repeat(60);return t=>{let n=t,r="";switch(n.name){case"ChatRequestChatPrompt":r=`
103
+ `,r.value.forEach(i=>{o+=`${e.white("- ")}${e.cyan(i.title||i.url)}
104
+ `,i.description&&(o+=` ${e.gray(i.description)}
105
+ `)}),o+=t;break}default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},Ia=Os(),Ca=(s=ya)=>{let e="\u2500".repeat(60);return t=>{let n=t,r="";switch(n.name){case"ChatRequestChatPrompt":r=`
106
106
  [ CHAT REQUEST Step ${n.step} ]
107
107
  ${e}
108
- `,n.value.forEach((o,s)=>{r+=ua(o),s<n.value.length-1&&(r+=`
108
+ `,n.value.forEach((o,i)=>{r+=ba(o),i<n.value.length-1&&(r+=`
109
109
  ${e}
110
110
  `)}),r+=`
111
111
  ${e}`;break;case"FunctionResults":r=`
112
112
  [ FUNCTION RESULTS ]
113
113
  ${e}
114
- `,n.value.forEach((o,s)=>{r+=`Function: ${o.functionId}
115
- Result: ${o.result}`,s<n.value.length-1&&(r+=`
114
+ `,n.value.forEach((o,i)=>{r+=`Function: ${o.functionId}
115
+ Result: ${o.result}`,i<n.value.length-1&&(r+=`
116
116
  ${e}
117
117
  `)});break;case"ChatResponseResults":r=`
118
118
  [ CHAT RESPONSE ]
119
- `,n.value.forEach((o,s)=>{let a=[],l=o.thoughtBlocks?.some(u=>u.encrypted);o.thought&&a.push(`[thought${l?" (redacted)":""}] ${o.thought}`),o.content&&a.push(o.content),a.length===0&&a.push("[No content]"),r+=a.join(`
120
- `),s<n.value.length-1&&(r+=`
119
+ `,n.value.forEach((o,i)=>{let a=[],u=o.thoughtBlocks?.some(l=>l.encrypted);o.thought&&a.push(`[thought${u?" (redacted)":""}] ${o.thought}`),o.content&&a.push(o.content),a.length===0&&a.push("[No content]"),r+=a.join(`
120
+ `),i<n.value.length-1&&(r+=`
121
121
  ${e}
122
122
  `)});break;case"ChatResponseStreamingResult":return;case"ChatResponseStreamingDoneResult":{r=`
123
123
  [ CHAT RESPONSE ]
124
- `,n.value.content&&(r+=n.value.content);let o=n.value.thoughtBlocks?.some(s=>s.encrypted);n.value.thought&&(r+=`
124
+ `,n.value.content&&(r+=n.value.content);let o=n.value.thoughtBlocks?.some(i=>i.encrypted);n.value.thought&&(r+=`
125
125
  `,r+=`[thought${o?" (redacted)":""}] `+n.value.thought),n.value.functionCalls&&(r+=JSON.stringify(n.value.functionCalls,null,2));break}case"FunctionError":r=`
126
126
  [ FUNCTION ERROR #${n.index} ]
127
127
  ${e}
@@ -140,11 +140,11 @@ Selected sample ${n.selectedIndex+1} of ${n.sampleCount} (${n.latency.toFixed(2)
140
140
  ${e}
141
141
  ${n.value}`;break;case"EmbedRequest":r=`[ EMBED REQUEST ${n.embedModel} ]
142
142
  ${e}
143
- `,n.value.forEach((o,s)=>{r+=`Text ${s+1}: ${o.substring(0,100)}${o.length>100?"...":""}`,s<n.value.length-1&&(r+=`
143
+ `,n.value.forEach((o,i)=>{r+=`Text ${i+1}: ${o.substring(0,100)}${o.length>100?"...":""}`,i<n.value.length-1&&(r+=`
144
144
  ${e}
145
145
  `)});break;case"EmbedResponse":r=`[ EMBED RESPONSE (${n.totalEmbeddings} embeddings) ]
146
146
  ${e}
147
- `,n.value.forEach((o,s)=>{r+=`Embedding ${s+1}: [${o.sample.join(", ")}${o.truncated?", ...":""}] (length: ${o.length})`,s<n.value.length-1&&(r+=`
147
+ `,n.value.forEach((o,i)=>{r+=`Embedding ${i+1}: [${o.sample.join(", ")}${o.truncated?", ...":""}] (length: ${o.length})`,i<n.value.length-1&&(r+=`
148
148
  ${e}
149
149
  `)});break;case"ChatResponseUsage":{r=`
150
150
  [ CHAT RESPONSE USAGE ]
@@ -164,23 +164,23 @@ ${e}
164
164
  `,n.value.forEach(o=>{r+=`- ${o.title||o.url}
165
165
  `,o.description&&(r+=` ${o.description}
166
166
  `)}),r+=`${e}
167
- `;break}default:r=JSON.stringify(n,null,2)}i(r)}};var he={LLM_SYSTEM:"gen_ai.system",LLM_OPERATION_NAME:"gen_ai.operation.name",LLM_REQUEST_MODEL:"gen_ai.request.model",LLM_REQUEST_MAX_TOKENS:"gen_ai.request.max_tokens",LLM_REQUEST_TEMPERATURE:"gen_ai.request.temperature",LLM_REQUEST_TOP_K:"gen_ai.request.top_k",LLM_REQUEST_FREQUENCY_PENALTY:"gen_ai.request.frequency_penalty",LLM_REQUEST_PRESENCE_PENALTY:"gen_ai.request.presence_penalty",LLM_REQUEST_STOP_SEQUENCES:"gen_ai.request.stop_sequences",LLM_REQUEST_LLM_IS_STREAMING:"gen_ai.request.llm_is_streaming",LLM_REQUEST_TOP_P:"gen_ai.request.top_p",LLM_USAGE_INPUT_TOKENS:"gen_ai.usage.input_tokens",LLM_USAGE_OUTPUT_TOKENS:"gen_ai.usage.output_tokens",LLM_USAGE_TOTAL_TOKENS:"gen_ai.usage.total_tokens",LLM_USAGE_THOUGHTS_TOKENS:"gen_ai.usage.thoughts_tokens",DB_SYSTEM:"db.system",DB_TABLE:"db.table",DB_NAMESPACE:"db.namespace",DB_ID:"db.id",DB_QUERY_TEXT:"db.query.text",DB_VECTOR:"db.vector",DB_OPERATION_NAME:"db.operation.name",DB_VECTOR_QUERY_TOP_K:"db.vector.query.top_k",DB_QUERY_EMBEDDINGS:"db.query.embeddings",DB_QUERY_RESULT:"db.query.result",DB_QUERY_EMBEDDINGS_VECTOR:"db.query.embeddings.vector",DB_QUERY_RESULT_ID:"db.query.result.id",DB_QUERY_RESULT_SCORE:"db.query.result.score",DB_QUERY_RESULT_DISTANCE:"db.query.result.distance",DB_QUERY_RESULT_METADATA:"db.query.result.metadata",DB_QUERY_RESULT_VECTOR:"db.query.result.vector",DB_QUERY_RESULT_DOCUMENT:"db.query.result.document"},bt={GEN_AI_USER_MESSAGE:"gen_ai.user.message",GEN_AI_SYSTEM_MESSAGE:"gen_ai.system.message",GEN_AI_ASSISTANT_MESSAGE:"gen_ai.assistant.message",GEN_AI_TOOL_MESSAGE:"gen_ai.tool.message",GEN_AI_CHOICE:"gen_ai.choice",GEN_AI_USAGE:"gen_ai.usage"},Ms=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(Ms||{}),Es=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Es||{});var Ps=class{buffer;doneCallback;transformFn;constructor(e,t){this.transformFn=e,this.doneCallback=t,this.buffer=t?[]:void 0}async transform(e,t){let n=this.transformFn(e);n&&(t.enqueue(n),this.buffer?.push(n))}async flush(e){await this.doneCallback?.(this.buffer??[]),e.terminate()}},Wr=class extends TransformStream{constructor(e,t){super(new Ps(e,t))}};function Vr(i,e){for(let t of e){let n=i.find(r=>r.id===t.id);n?(typeof t.function.name=="string"&&t.function.name.length>0&&(n.function.name+=t.function.name),typeof t.function.params=="string"&&t.function.params.length>0&&(n.function.params+=t.function.params),typeof t.function.params=="object"&&(n.function.params=t.function.params)):i.push(t)}}var da=(i,e,t,n)=>{let r=n?i.filter(s=>s.role!=="system"):[...i];t({name:"ChatRequestChatPrompt",step:e,value:r})};var ma=(i,e)=>{if(!i.results)return;let t={name:"ChatResponseResults",value:i.results};e(t)};function ga(i,e){let t=new Map;for(let n of i)for(let r of n.results){if(!r)continue;let o=t.get(r.index);o?(r.content&&(o.content=(o.content??"")+r.content),r.thought&&(o.thought=(o.thought??"")+r.thought),r.finishReason&&(o.finishReason=r.finishReason),r.functionCalls&&(o.functionCalls?Vr(o.functionCalls,structuredClone(r.functionCalls)):o.functionCalls=structuredClone(r.functionCalls))):(o=structuredClone(r),t.set(r.index,o))}for(let n of t.values()){let r={name:"ChatResponseStreamingDoneResult",index:n.index,value:n};e(r)}}var ha=(i,e)=>{e({name:"FunctionResults",value:i})},Fs=(i,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:i})},fa=(i,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:i})},xa=(i,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:i})},Aa=(i,e,t)=>{t({name:"RefusalError",index:e,error:i})};var ya=(i,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:i})},ba=(i,e)=>{let t=i.slice(0,3).map(r=>({length:r.length,sample:r.slice(0,5),truncated:r.length>5})),n={name:"EmbedResponse",totalEmbeddings:i.length,value:t};e(n)},Ia=(i,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:i,selectedIndex:e,latency:t})};var Qr=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n);e[t]=r.length>100?r.substring(0,100):r}return e},Bt=(...i)=>{let e={};for(let t of i)t&&Object.assign(e,t);return e},Jr,Ca=i=>{if(Jr)return Jr;if(i)return Jr=Cc(i),Jr};var Cc=i=>({latencyHistogram:i.createHistogram("ax_llm_request_duration_ms",{description:"Duration of LLM requests in milliseconds",unit:"ms"}),errorCounter:i.createCounter("ax_llm_errors_total",{description:"Total number of LLM request errors"}),requestCounter:i.createCounter("ax_llm_requests_total",{description:"Total number of LLM requests"}),tokenCounter:i.createCounter("ax_llm_tokens_total",{description:"Total number of LLM tokens consumed"}),inputTokenCounter:i.createCounter("ax_llm_input_tokens_total",{description:"Total number of input/prompt tokens consumed"}),outputTokenCounter:i.createCounter("ax_llm_output_tokens_total",{description:"Total number of output/completion tokens generated"}),errorRateGauge:i.createGauge("ax_llm_error_rate",{description:"Current error rate as a percentage (0-100)"}),meanLatencyGauge:i.createGauge("ax_llm_mean_latency_ms",{description:"Mean latency of LLM requests in milliseconds",unit:"ms"}),p95LatencyGauge:i.createGauge("ax_llm_p95_latency_ms",{description:"95th percentile latency of LLM requests in milliseconds",unit:"ms"}),p99LatencyGauge:i.createGauge("ax_llm_p99_latency_ms",{description:"99th percentile latency of LLM requests in milliseconds",unit:"ms"}),streamingRequestsCounter:i.createCounter("ax_llm_streaming_requests_total",{description:"Total number of streaming LLM requests"}),functionCallsCounter:i.createCounter("ax_llm_function_calls_total",{description:"Total number of function/tool calls made"}),functionCallLatencyHistogram:i.createHistogram("ax_llm_function_call_latency_ms",{description:"Latency of function calls in milliseconds",unit:"ms"}),requestSizeHistogram:i.createHistogram("ax_llm_request_size_bytes",{description:"Size of LLM request payloads in bytes",unit:"By"}),responseSizeHistogram:i.createHistogram("ax_llm_response_size_bytes",{description:"Size of LLM response payloads in bytes",unit:"By"}),temperatureGauge:i.createGauge("ax_llm_temperature_gauge",{description:"Temperature setting used for LLM requests"}),maxTokensGauge:i.createGauge("ax_llm_max_tokens_gauge",{description:"Maximum tokens setting used for LLM requests"}),estimatedCostCounter:i.createCounter("ax_llm_estimated_cost_total",{description:"Estimated cost of LLM requests in USD",unit:"$"}),promptLengthHistogram:i.createHistogram("ax_llm_prompt_length_chars",{description:"Length of prompts in characters"}),contextWindowUsageGauge:i.createGauge("ax_llm_context_window_usage_ratio",{description:"Context window utilization ratio (0-1)"}),timeoutsCounter:i.createCounter("ax_llm_timeouts_total",{description:"Total number of timed out LLM requests"}),abortsCounter:i.createCounter("ax_llm_aborts_total",{description:"Total number of aborted LLM requests"}),thinkingBudgetUsageCounter:i.createCounter("ax_llm_thinking_budget_usage_total",{description:"Total thinking budget tokens used"}),multimodalRequestsCounter:i.createCounter("ax_llm_multimodal_requests_total",{description:"Total number of multimodal requests (with images/audio)"}),cacheReadTokensCounter:i.createCounter("ax_llm_cache_read_tokens_total",{description:"Total number of tokens read from cache (prompt caching)"}),cacheWriteTokensCounter:i.createCounter("ax_llm_cache_write_tokens_total",{description:"Total number of tokens written to cache (prompt caching)"})}),Ta=(i,e,t,n,r,o)=>{try{if(i.latencyHistogram){let s=Qr({operation:e,ai_service:n,...r?{model:r}:{},...o});i.latencyHistogram.record(t,s)}}catch(s){console.warn("Failed to record latency metric:",s)}},wa=(i,e,t,n,r,o,s,a)=>{let l={operation:e,ai_service:o,...s?{model:s}:{},...a};i.meanLatencyGauge&&i.meanLatencyGauge.record(t,l),i.p95LatencyGauge&&i.p95LatencyGauge.record(n,l),i.p99LatencyGauge&&i.p99LatencyGauge.record(r,l)},Ra=(i,e,t,n,r)=>{try{if(i.errorCounter){let o=Qr({operation:e,ai_service:t,...n?{model:n}:{},...r});i.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},Sa=(i,e,t,n,r,o)=>{i.errorRateGauge&&i.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ka=(i,e,t,n,r)=>{i.requestCounter&&i.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},nr=(i,e,t,n,r,o)=>{try{let s=Qr({ai_service:n,...r?{model:r}:{},...o});i.tokenCounter&&i.tokenCounter.add(t,{token_type:e,...s}),e==="input"&&i.inputTokenCounter&&i.inputTokenCounter.add(t,s),e==="output"&&i.outputTokenCounter&&i.outputTokenCounter.add(t,s)}catch(s){console.warn("Failed to record token metric:",s)}},va=(i,e,t,n,r,o)=>{t&&i.streamingRequestsCounter&&i.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Oa=(i,e,t,n,r,o)=>{let s={function_name:e,...n?{ai_service:n}:{},...r?{model:r}:{},...o};i.functionCallsCounter&&i.functionCallsCounter.add(1,s),t&&i.functionCallLatencyHistogram&&i.functionCallLatencyHistogram.record(t,s)},_s=(i,e,t,n,r,o)=>{i.requestSizeHistogram&&i.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ns=(i,e,t,n,r,o)=>{i.responseSizeHistogram&&i.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ma=(i,e,t,n,r,o)=>{let s={...n?{ai_service:n}:{},...r?{model:r}:{},...o};e!==void 0&&i.temperatureGauge&&i.temperatureGauge.record(e,s),t!==void 0&&i.maxTokensGauge&&i.maxTokensGauge.record(t,s)},$s=(i,e,t,n,r,o)=>{i.estimatedCostCounter&&i.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ea=(i,e,t,n,r)=>{i.promptLengthHistogram&&i.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{},...r})},Pa=(i,e,t,n,r)=>{i.contextWindowUsageGauge&&i.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},Fa=(i,e,t,n,r)=>{i.timeoutsCounter&&i.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},_a=(i,e,t,n,r)=>{i.abortsCounter&&i.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Na=(i,e,t,n,r)=>{i.thinkingBudgetUsageCounter&&i.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},$a=(i,e,t,n,r,o)=>{(e||t)&&i.multimodalRequestsCounter&&i.multimodalRequestsCounter.add(1,{ai_service:n,has_images:e.toString(),has_audio:t.toString(),...r?{model:r}:{},...o})},Ls=(i,e,t,n,r,o)=>{try{if(t<=0)return;let s=Qr({ai_service:n,...r?{model:r}:{},...o});e==="read"&&i.cacheReadTokensCounter&&i.cacheReadTokensCounter.add(t,s),e==="write"&&i.cacheWriteTokensCounter&&i.cacheWriteTokensCounter.add(t,s)}catch(s){console.warn("Failed to record cache token metric:",s)}};function La(i){try{return JSON.stringify(i,null,2)}catch{return String(i)}}function It(i,e={}){let t=[i];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${La(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${La(e.item)}`),new Error(t.join(`
168
- `))}function tn(i){let e=n=>JSON.stringify(n,null,2);if(!i)throw new Error(`Chat request message item cannot be null or undefined, received: ${e(i)}`);let t=typeof i=="object"&&i!==null&&"role"in i&&typeof i.role=="string"?i.role:void 0;if(!t)throw new Error(`Chat request message must have a role, received: ${e(t)}`);switch(t){case"system":{let n=typeof i=="object"&&i!==null&&"content"in i&&typeof i.content=="string"?i.content:void 0;if(!n||n.trim()==="")throw new Error(`System message content cannot be empty or whitespace-only, received: ${e(n)}`);break}case"user":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0;if(n===void 0)throw new Error(`User message content cannot be undefined, received: ${e(n)}`);if(typeof n=="string"){if(n.trim()==="")throw new Error(`User message content cannot be empty or whitespace-only, received: ${e(n)}`)}else if(Array.isArray(n)){if(n.length===0)throw new Error(`User message content array cannot be empty, received: ${e(n)}`);for(let r=0;r<n.length;r++){let o=n[r];if(!o||typeof o!="object")throw new Error(`User message content item at index ${r} must be an object, received: ${e(o)}`);let s=typeof o=="object"&&o!==null&&"type"in o&&typeof o.type=="string"?o.type:void 0;if(!s)throw new Error(`User message content item at index ${r} must have a type, received: ${e(s)}`);switch(s){case"text":{let a="text"in o&&typeof o.text=="string"?o.text:void 0;if(!a||a.trim()==="")throw new Error(`User message text content at index ${r} cannot be empty or whitespace-only, received: ${e(a)}`);break}case"image":{let a="image"in o&&typeof o.image=="string"?o.image:void 0,l="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:void 0;if(!a||a.trim()==="")throw new Error(`User message image content at index ${r} cannot be empty, received: ${e(a)}`);if(!l||l.trim()==="")throw new Error(`User message image content at index ${r} must have a mimeType, received: ${e(l)}`);break}case"audio":{let a="data"in o&&typeof o.data=="string"?o.data:void 0;if(!a||a.trim()==="")throw new Error(`User message audio content at index ${r} cannot be empty, received: ${e(a)}`);break}case"file":{let a="fileUri"in o&&typeof o.fileUri=="string",l="data"in o&&typeof o.data=="string";if(!a&&!l)throw new Error(`User message file content at index ${r} must have either 'data' or 'fileUri', received: ${e(o)}`);if(a&&l)throw new Error(`User message file content at index ${r} cannot have both 'data' and 'fileUri', received: ${e(o)}`);if(a){let c=o.fileUri;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(c)}`)}if(l){let c=o.data;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(c)}`)}let u="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!u||u.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(u)}`);break}case"url":{let a="url"in o&&typeof o.url=="string"?o.url:void 0;if(!a||a.trim()==="")throw new Error(`User message url content at index ${r} cannot be empty, received: ${e(a)}`);break}default:throw new Error(`User message content item at index ${r} has unsupported type: ${e(s)}`)}}}else throw new Error(`User message content must be a string or array of content objects, received: ${e(n)}`);break}case"assistant":{let n=typeof i=="object"&&i!==null&&"content"in i?i.content:void 0,r=typeof i=="object"&&i!==null&&"functionCalls"in i?i.functionCalls:void 0,o=typeof i=="object"&&i!==null&&"thoughtBlocks"in i?i.thoughtBlocks:void 0,s=typeof n=="string"&&n.trim()!=="",a=Array.isArray(r)&&r.length>0,l=Array.isArray(o)&&o.length>0;if(!s&&!a&&!l&&It("Assistant message must include non-empty content, at least one function call, or thought blocks",{fieldPath:"content | functionCalls | thoughtBlocks",value:{content:n,functionCalls:r,thoughtBlocks:o},item:i}),n!==void 0&&typeof n!="string"&&It("Assistant message content must be a string",{fieldPath:"content",value:n,item:i}),r!==void 0&&!Array.isArray(r)&&It("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:i}),Array.isArray(r))for(let u=0;u<r.length;u++){let c=r[u];if((!c||typeof c!="object")&&It("functionCalls entry must be an object",{fieldPath:`functionCalls[${u}]`,value:c,item:i}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&It("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${u}].id`,value:c.id,item:i}),(!("type"in c)||c.type!=="function")&&It("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${u}].type`,value:c.type,item:i}),!("function"in c)||!c.function)It("functionCalls entry must include a function object",{fieldPath:`functionCalls[${u}].function`,value:c.function,item:i});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&It("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${u}].function.name`,value:p?.name,item:i}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&It("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${u}].function.params`,value:p.params,item:i})}}if(i.name!==void 0){let u=i.name;(typeof u!="string"||u.trim()==="")&&It("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:u,item:i})}break}case"function":{let n=typeof i=="object"&&i!==null&&"functionId"in i&&typeof i.functionId=="string"?i.functionId:void 0,r=typeof i=="object"&&i!==null&&"result"in i?i.result:void 0;if(!n||n.trim()==="")throw new Error(`Function message must have a non-empty functionId, received: ${e(n)}`);if(r==null)throw new Error(`Function message must have a result, received: ${e(r)}`);if(typeof r!="string")throw new Error(`Function message result must be a string, received: ${e(r)}`);i.isError!==void 0&&typeof i.isError!="boolean"&&It("Function message isError must be a boolean when provided",{fieldPath:"isError",value:i.isError,item:i});break}default:throw new Error(`Unsupported message role: ${e(t)}`)}}function Yr(i){let e=n=>JSON.stringify(n,null,2),t=Array.isArray(i)?i:[i];if(t.length===0)throw new Error(`Chat response results cannot be empty, received: ${e(t)}`);for(let n=0;n<t.length;n++){let r=t[n];if(!r)throw new Error(`Chat response result at index ${n} cannot be null or undefined, received: ${e(r)}`);if(typeof r.index!="number")throw new Error(`Chat response result at index ${n} must have a numeric index, received: ${e(r.index)}`);if(r.index<0)throw new Error(`Chat response result at index ${n} must have a non-negative index, received: ${e(r.index)}`);if(!r.content&&!r.thought&&(!r.thoughtBlocks||r.thoughtBlocks.length===0)&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, thoughtBlocks, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,thoughtBlocks:r.thoughtBlocks,functionCalls:r.functionCalls,finishReason:r.finishReason})}`);if(r.content!==void 0&&typeof r.content!="string")throw new Error(`Chat response result content at index ${n} must be a string, received: ${e(r.content)}`);if(r.thought!==void 0&&typeof r.thought!="string")throw new Error(`Chat response result thought at index ${n} must be a string, received: ${e(r.thought)}`);if(r.thoughtBlocks!==void 0){if(!Array.isArray(r.thoughtBlocks))throw new Error(`Chat response result thoughtBlocks at index ${n} must be an array, received: ${e(r.thoughtBlocks)}`);for(let o=0;o<r.thoughtBlocks.length;o++){let s=r.thoughtBlocks[o];if(typeof s!="object"||s===null)throw new Error(`Chat response result thoughtBlocks[${o}] at index ${n} must be an object, received: ${e(s)}`);if(typeof s.data!="string")throw new Error(`Chat response result thoughtBlocks[${o}].data at index ${n} must be a string, received: ${e(s.data)}`);if(typeof s.encrypted!="boolean")throw new Error(`Chat response result thoughtBlocks[${o}].encrypted at index ${n} must be a boolean, received: ${e(s.encrypted)}`);if(s.signature!==void 0&&typeof s.signature!="string")throw new Error(`Chat response result thoughtBlocks[${o}].signature at index ${n} must be a string when provided, received: ${e(s.signature)}`)}}if(r.name!==void 0){if(typeof r.name!="string")throw new Error(`Chat response result name at index ${n} must be a string, received: ${e(r.name)}`);if(r.name.trim()==="")throw new Error(`Chat response result name at index ${n} cannot be empty or whitespace-only, received: ${e(r.name)}`)}if(r.annotations!==void 0){if(!Array.isArray(r.annotations))throw new Error(`Chat response result annotations at index ${n} must be an array, received: ${e(r.annotations)}`);for(let o=0;o<r.annotations.length;o++){let s=r.annotations[o];if(!s||typeof s!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must be an object, received: ${e(s)}`);if(s.type!=="url_citation")throw new Error(`Chat response result annotation at index ${n}[${o}] must have type 'url_citation', received: ${e(s.type)}`);if(!s.url_citation||typeof s.url_citation!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must have a valid url_citation object, received: ${e(s.url_citation)}`);if(typeof s.url_citation.url!="string")throw new Error(`Chat response result annotation at index ${n}[${o}] url_citation.url must be a string, received: ${e(s.url_citation.url)}`)}}if(r.id!==void 0){if(typeof r.id!="string")throw new Error(`Chat response result id at index ${n} must be a string, received: ${e(r.id)}`);if(r.id.trim()==="")throw new Error(`Chat response result id at index ${n} cannot be empty or whitespace-only, received: ${e(r.id)}`)}if(r.functionCalls!==void 0){if(!Array.isArray(r.functionCalls))throw new Error(`Chat response result functionCalls at index ${n} must be an array, received: ${e(r.functionCalls)}`);for(let o=0;o<r.functionCalls.length;o++){let s=r.functionCalls[o];if(!s)throw new Error(`Function call at index ${o} in result ${n} cannot be null or undefined, received: ${e(s)}`);if(!s.id||typeof s.id!="string"||s.id.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty string id, received: ${e(s.id)}`);if(s.type!=="function")throw new Error(`Function call at index ${o} in result ${n} must have type 'function', received: ${e(s.type)}`);if(!s.function)throw new Error(`Function call at index ${o} in result ${n} must have a function object, received: ${e(s.function)}`);if(!s.function.name||typeof s.function.name!="string"||s.function.name.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty function name, received: ${e(s.function.name)}`);if(s.function.params!==void 0&&typeof s.function.params!="string"&&typeof s.function.params!="object")throw new Error(`Function call params at index ${o} in result ${n} must be a string or object, received: ${e(s.function.params)}`)}}if(r.finishReason!==void 0){let o=["stop","length","function_call","content_filter","error"];if(!o.includes(r.finishReason))throw new Error(`Chat response result finishReason at index ${n} must be one of: ${o.join(", ")}, received: ${e(r.finishReason)}`)}}}var Ds=new Map;function Tc(i){return`${i.providerName}:${i.contentHash}`}function wc(i,e){e.type==="text"?i.update(`text:${e.text}`):e.type==="image"?i.update(`image:${e.mimeType}:${e.image.slice(0,100)}`):e.type==="audio"?i.update(`audio:${e.format}:${e.data.slice(0,100)}`):e.type==="file"&&("fileUri"in e?i.update(`file:${e.mimeType}:${e.fileUri}`):i.update(`file:${e.mimeType}:${e.data.slice(0,100)}`))}function Rc(i){let e=ot("sha256"),t=-1;for(let n=i.length-1;n>=0;n--){let r=i[n];if("cache"in r&&r.cache){t=n;break}}for(let n=0;n<i.length;n++){let r=i[n];if(r.role==="system"){e.update(`system:${r.content}`);continue}if(t>=0&&n<=t)if(r.role==="user"){if(typeof r.content=="string")e.update(`user:${r.content}`);else if(Array.isArray(r.content))for(let o of r.content)wc(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var Te=()=>structuredClone({temperature:0}),je=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Be=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:s,options:a={},supportFor:l,models:u}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=l,this.tracer=a.tracer??me.tracer,this.meter=a.meter??me.meter,this.modelInfo=o,this.models=u,this.id=Ve();let c=this.getModel(s.model)??s.model,p=this.getEmbedModel(s.embedModel)??s.embedModel;if(this.defaults={model:c,embedModel:p},!s.model||typeof s.model!="string"||s.model==="")throw new Error("No model defined");this.setOptions(a),u&&Sc(u)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=me.logger??ca;corsProxy;retry;customLabels;modelInfo;modelUsage;embedModelUsage;defaults;lastUsedModelConfig;lastUsedChatModel;lastUsedEmbedModel;apiURL;name;id;headers;supportFor;metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getMetricsInstruments(){return Ca(this.meter)}setName(e){this.name=e}getId(){return this.id}setAPIURL(e){this.apiURL=e}setHeaders(e){this.headers=e}get debug(){return this.#e}setOptions(e){this.#e=e.debug??me.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??me.tracer,this.meter=e.meter??me.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??me.logger??this.logger,this.corsProxy=e.corsProxy,this.retry=e.retry,this.customLabels=e.customLabels}getOptions(){return{debug:this.#e,verbose:this.#t,rateLimiter:this.rt,fetch:this.fetch,tracer:this.tracer,meter:this.meter,timeout:this.timeout,excludeContentFromTrace:this.excludeContentFromTrace,abortSignal:this.abortSignal,logger:this.logger,corsProxy:this.corsProxy,retry:this.retry,customLabels:this.customLabels}}getLogger(){return this.logger}getMergedCustomLabels(e){return Bt(me.customLabels,this.customLabels,e)}getModelList(){let e=[];for(let t of this.models??[])t.isInternal||("model"in t&&t.model&&e.push({key:t.key,description:t.description,model:t.model}),"embedModel"in t&&t.embedModel&&e.push({key:t.key,description:t.description,embedModel:t.embedModel}));return e}getName(){return this.name}getFeatures(e){return typeof this.supportFor=="function"?this.supportFor(e??this.defaults.model):this.supportFor}getLastUsedChatModel(){return this.lastUsedChatModel}getLastUsedEmbedModel(){return this.lastUsedEmbedModel}getLastUsedModelConfig(){return this.lastUsedModelConfig}calculatePercentile(e,t){if(e.length===0)return 0;let n=[...e].sort((o,s)=>o-s),r=Math.ceil(t/100*n.length)-1;return n[r]??0}updateLatencyMetrics(e,t,n){let r=this.metrics.latency[e];r.samples.push(t),r.samples.length>1e3&&r.samples.shift(),r.mean=r.samples.reduce((s,a)=>s+a,0)/r.samples.length,r.p95=this.calculatePercentile(r.samples,95),r.p99=this.calculatePercentile(r.samples,99);let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);Ta(o,e,t,this.name,s,a),wa(o,e,r.mean,r.p95,r.p99,this.name,s,a)}}updateErrorMetrics(e,t,n){let r=this.metrics.errors[e];r.total++,t&&r.count++,r.rate=r.count/r.total;let o=this.getMetricsInstruments();if(o){let s=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);ka(o,e,this.name,s,a),t&&Ra(o,e,this.name,s,a),Sa(o,e,r.rate,this.name,s,a)}}recordTokenUsage(e,t){let n=this.getMetricsInstruments();if(n&&e?.tokens){let{promptTokens:r,completionTokens:o,totalTokens:s,thoughtsTokens:a,cacheReadTokens:l,cacheCreationTokens:u}=e.tokens,c=this.getMergedCustomLabels(t);r&&nr(n,"input",r,this.name,e.model,c),o&&nr(n,"output",o,this.name,e.model,c),s&&nr(n,"total",s,this.name,e.model,c),a&&nr(n,"thoughts",a,this.name,e.model,c),l&&Ls(n,"read",l,this.name,e.model,c),u&&Ls(n,"write",u,this.name,e.model,c)}}calculateRequestSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}calculateResponseSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}detectMultimodalContent(e){let t=!1,n=!1;if(e.chatPrompt&&Array.isArray(e.chatPrompt)){for(let r of e.chatPrompt)if(r.role==="user"&&Array.isArray(r.content))for(let o of r.content)o.type==="image"?t=!0:o.type==="audio"&&(n=!0)}return{hasImages:t,hasAudio:n}}calculatePromptLength(e){let t=0;if(e.chatPrompt&&Array.isArray(e.chatPrompt))for(let n of e.chatPrompt)if(n.role==="system"||n.role==="assistant")n.content&&(t+=n.content.length);else if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)r.type==="text"&&(t+=r.text.length)}else n.role==="function"&&n.result&&(t+=n.result.length);return t}calculateContextWindowUsage(e,t){if(!t?.tokens?.promptTokens)return 0;let n=this.modelInfo.find(r=>r.name===e);return n?.contextWindow?t.tokens.promptTokens/n.contextWindow:0}estimateCost(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}estimateCostByName(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(l=>l.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,s=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*s/1e6+o*a/1e6}recordFunctionCallMetrics(e,t,n){let r=this.getMetricsInstruments();if(!(!r||!e))for(let o of e)o&&typeof o=="object"&&"function"in o&&o.function&&typeof o.function=="object"&&"name"in o.function&&Oa(r,o.function.name,void 0,this.name,t,this.getMergedCustomLabels(n))}recordTimeoutMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;Fa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordAbortMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;_a(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,s=this.lastUsedModelConfig,a=this.getMergedCustomLabels(t?.customLabels),l=s?.stream??!1;va(r,"chat",l,this.name,o,a);let{hasImages:u,hasAudio:c}=this.detectMultimodalContent(e);$a(r,u,c,this.name,o,a);let p=this.calculatePromptLength(e);Ea(r,p,this.name,o,a),Ma(r,s?.temperature,s?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&Na(r,this.modelUsage.tokens.thoughtsTokens,this.name,o,a);let d=this.calculateRequestSize(e);if(_s(r,"chat",d,this.name,o,a),n&&!l){let m=n,g=this.calculateResponseSize(m);if(Ns(r,"chat",g,this.name,o,a),m.results)for(let C of m.results)C.functionCalls&&this.recordFunctionCallMetrics(C.functionCalls,this.lastUsedChatModel,a);let h=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);h>0&&Pa(r,h,this.name,o,a);let f=this.estimateCost(this.lastUsedChatModel,m.modelUsage);f>0&&$s(r,"chat",f,this.name,o,a)}}recordEmbedMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedEmbedModel,s=this.getMergedCustomLabels(n?.customLabels),a=this.calculateRequestSize(e);_s(r,"embed",a,this.name,o,s);let l=this.calculateResponseSize(t);Ns(r,"embed",l,this.name,o,s);let u=this.estimateCostByName(o,t.modelUsage);u>0&&$s(r,"embed",u,this.name,o,s)}getMetrics(){return structuredClone(this.metrics)}async chat(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.model),a=s?s.thinkingTokenBudget:void 0,l={...s?{thinkingTokenBudget:a,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...Object.fromEntries(Object.entries(t??{}).filter(([,u])=>u!==void 0))};try{return o=await this._chat1(e,l),o}catch(u){throw r=!0,u instanceof Error&&(u.message.includes("timeout")||u.name==="TimeoutError"?this.recordTimeoutMetric("chat",l?.customLabels):(u.message.includes("abort")||u.name==="AbortError")&&this.recordAbortMetric("chat",l?.customLabels)),u}finally{let u=performance.now()-n;this.updateLatencyMetrics("chat",u,l?.customLabels),this.updateErrorMetrics("chat",r,l?.customLabels),r||this.recordChatMetrics(e,l,o)}}async _chat1(e,t){let n=this.getModel(e.model)??e.model??this.defaults.model;if(Array.isArray(e.chatPrompt))for(let u of e.chatPrompt)tn(u);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},s=this.modelInfo.find(u=>u.name===n);if(s?.notSupported?.temperature&&"temperature"in o&&delete o.temperature,s?.notSupported?.topP&&"topP"in o&&delete o.topP,t?.thinkingTokenBudget&&!this.getFeatures(n).hasThinkingBudget)throw new Error(`Model ${n} does not support thinkingTokenBudget.`);if(t?.showThoughts&&!this.getFeatures(n).hasShowThoughts)throw new Error(`Model ${n} does not support showThoughts.`);if(this.modelInfo.find(u=>u.name===n)?.isExpensive&&t?.useExpensiveModel!=="yes")throw new Error(`Model ${n} is marked as expensive and requires explicit confirmation. Set useExpensiveModel: "yes" to proceed.`);return o.stream=(t?.stream!==void 0?t.stream:o.stream)??!0,this.getFeatures(n).streaming||(o.stream=!1),this.tracer?await this.tracer.startActiveSpan("AI Chat Request",{kind:nn.SpanKind.SERVER,attributes:{[he.LLM_SYSTEM]:this.name,[he.LLM_OPERATION_NAME]:"chat",[he.LLM_REQUEST_MODEL]:n,[he.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[he.LLM_REQUEST_TEMPERATURE]:o.temperature,[he.LLM_REQUEST_TOP_P]:o.topP??"Not set",[he.LLM_REQUEST_TOP_K]:o.topK??"Not set",[he.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[he.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[he.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[he.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??nn.context.active(),async u=>await this._chat2(n,o,e,t,u)):await this._chat2(n,o,e,t)}cleanupFunctionSchema(e){let t={...e};if(t.parameters){let n={...t.parameters};Array.isArray(n.required)&&n.required.length===0&&delete n.required,n.properties&&Object.keys(n.properties).length===0&&delete n.properties,Object.keys(n).length===0||Object.keys(n).length===1&&n.type==="object"?delete t.parameters:t.parameters=n}return t}async _chat2(e,t,n,r,o){if(!this.aiImpl.createChatReq)throw new Error("createChatReq not implemented");let s=r?.debug??this.#e,a=r?.verbose??this.#t,l;n.functions&&n.functions.length>0&&(l=n.functions.map(I=>this.cleanupFunctionSchema(I)));let u={...n,model:e,functions:l,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,s&&da(u.chatPrompt,r?.stepIndex??0,r?.logger??this.logger,r?.debugHideSystemPrompt);let c=this.getFeatures(e).functions,p=r?.functionCallMode??"auto",m=p==="prompt"||p==="auto"&&!c?{...u,chatPrompt:u.chatPrompt.map(I=>{if(I.role==="assistant"){let{content:w,name:A,cache:T}=I;return{role:"assistant",content:w,name:A,cache:T}}return I.role==="function"?{role:"user",content:I.result}:I}),functions:[]}:u,g=await this.handleContextCaching(e,m,r,o),h=async()=>{if(g?.preparedRequest){let{apiConfig:T,request:k}=g.preparedRequest;return o?.isRecording()&&Da(n,o,this.excludeContentFromTrace),await Qe({name:T.name,url:this.apiURL,localCall:T.localCall,headers:await this.buildHeaders(T.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},k)}let[I,w]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&Da(n,o,this.excludeContentFromTrace),await Qe({name:I.name,url:this.apiURL,localCall:I.localCall,headers:await this.buildHeaders(I.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},w)},f=r?.rateLimiter??this.rt,C=f?await f(h,{modelUsage:this.modelUsage}):await h();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let I=this.aiImpl.createChatStreamResp.bind(this),w=E=>M=>{let $=I(M,E);if($.sessionId=r?.sessionId,!$.modelUsage){let B=this.aiImpl.getTokenUsage();B&&($.modelUsage={ai:this.name,model:e,tokens:B})}return this.modelUsage=$.modelUsage,this.recordTokenUsage($.modelUsage,r?.customLabels),o?.isRecording()&&Ga($,o,this.excludeContentFromTrace),$},A=async E=>{o?.isRecording()&&o.end(),s&&ga(E,r?.logger??this.logger)};if(typeof window<"u"){let E=C,M={},$=[],B=r?.abortSignal??this.abortSignal;return new ReadableStream({start:V=>{let O=E.getReader(),j=()=>{try{O.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{V.error(new DOMException("Aborted","AbortError"))}catch{V.error(new Error("Aborted"))}};if(B){if(B.aborted){j();return}B.addEventListener("abort",j,{once:!0})}async function F(){try{for(;;){let{done:W,value:P}=await O.read();if(W){A&&await A($),V.close();break}let D=w(M)(P);D&&($.push(D),V.enqueue(D))}}catch(W){if(V.error(W),o?.isRecording())try{o.end()}catch{}}finally{if(O.releaseLock(),B)try{B.removeEventListener("abort",j)}catch{}}}F()}})}return C.pipeThrough(new Wr(w({}),A))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let x=this.aiImpl.createChatResp(C);if(x.sessionId=r?.sessionId,!x.modelUsage){let I=this.aiImpl.getTokenUsage();I&&(x.modelUsage={ai:this.name,model:e,tokens:I})}return x.modelUsage&&(this.modelUsage=x.modelUsage,this.recordTokenUsage(x.modelUsage,r?.customLabels)),o?.isRecording()&&(Ga(x,o,this.excludeContentFromTrace),o.end()),s&&ma(x,r?.logger??this.logger),x}async embed(e,t){let n=performance.now(),r=!1,o,s=this.getModelByKey(e.embedModel),a={...s?{thinkingTokenBudget:s.thinkingTokenBudget,showThoughts:s.showThoughts,stream:s.stream,debug:s.debug,useExpensiveModel:s.useExpensiveModel}:void 0,...t};try{return o=await this._embed1(e,a),o}catch(l){throw r=!0,l instanceof Error&&(l.message.includes("timeout")||l.name==="TimeoutError"?this.recordTimeoutMetric("embed",a?.customLabels):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("embed",a?.customLabels)),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("embed",l,a?.customLabels),this.updateErrorMetrics("embed",r,a?.customLabels),!r&&o&&this.recordEmbedMetrics(e,o,a)}}async _embed1(e,t){let n=this.getEmbedModel(e.embedModel)??e.embedModel??this.defaults.embedModel;if(!n)throw new Error("No embed model defined");return this.tracer?await this.tracer.startActiveSpan("AI Embed Request",{kind:nn.SpanKind.SERVER,attributes:{[he.LLM_SYSTEM]:this.name,[he.LLM_OPERATION_NAME]:"embeddings",[he.LLM_REQUEST_MODEL]:n}},t?.traceContext??nn.context.active(),async r=>await this._embed2(n,e,t,r)):await this._embed2(n,e,t)}async _embed2(e,t,n,r){if(!this.aiImpl.createEmbedReq)throw new Error("createEmbedReq not implemented");if(!this.aiImpl.createEmbedResp)throw new Error("createEmbedResp not implemented");let o=this.aiImpl.createEmbedReq.bind(this.aiImpl),s=n?.debug??this.#e,a=n?.verbose??this.#t,l={...t,embedModel:e};this.lastUsedEmbedModel=e,s&&ya(l.texts??[],e,n?.logger??this.logger);let u=async()=>{let[m,g]=await o(l);return await Qe({name:m.name,url:this.apiURL,localCall:m.localCall,headers:await this.buildHeaders(m.headers),verbose:a,fetch:this.fetch,timeout:this.timeout,span:r,abortSignal:n?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:n?.retry??this.retry},g)},c=n?.rateLimiter??this.rt,p=c?await c(u,{modelUsage:this.embedModelUsage}):await u(),d=this.aiImpl.createEmbedResp?.(p);if(d.sessionId=n?.sessionId,!d.modelUsage){let m=this.aiImpl.getTokenUsage();m&&(d.modelUsage={ai:this.name,model:e,tokens:m})}return this.embedModelUsage=d.modelUsage,this.recordTokenUsage(d.modelUsage,n?.customLabels),r?.isRecording()&&d.modelUsage?.tokens&&r.addEvent(bt.GEN_AI_USAGE,{[he.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[he.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[he.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),s&&ba(d.embeddings,n?.logger??this.logger),r?.end(),d}async buildHeaders(e={}){return{...e,...await this.headers()}}getModelByKey(e){return e?this.models?.find(n=>n.key===e):void 0}getModel(e){let t=this.getModelByKey(e);return t&&"model"in t?t.model:void 0}getEmbedModel(e){let t=this.getModelByKey(e);return t&&"embedModel"in t?t.embedModel:void 0}async handleContextCaching(e,t,n,r){let o=n?.contextCache;if(!o)return null;let s=this.aiImpl.supportsContextCache?.(e)??!1,a=this.aiImpl.supportsImplicitCaching?.(e)??!1;if(!s&&!a)throw new Error(`Context caching is not supported by this provider/model (${this.getName()}/${e}). Remove the contextCache option or use a provider that supports caching.`);if(!s)return null;let l=o.ttlSeconds??3600,u=o.refreshWindowSeconds??300,c=o.minTokens??2048;if(o.name)return this.useCacheByName(e,t,o.name,n,r);let p=Rc(t.chatPrompt);if(!p||p===ot("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=Tc(d),g=Date.now(),h=o.registry,f=h?await h.get(m):Ds.get(m);if(f&&f.expiresAt>g){if(f.expiresAt-g<u*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(f.cacheName,l),n,r);let w={cacheName:f.cacheName,expiresAt:g+l*1e3,tokenCount:f.tokenCount};h?await h.set(m,w):Ds.set(m,{...w,contentHash:p,lastTouchedAt:g})}return this.useCacheByName(e,t,f.cacheName,n,r)}if(this.estimateCacheableTokens(t.chatPrompt)<c)return null;let x=this.aiImpl.buildCacheCreateOp?.(t,n);if(x){let I=await this.executeCacheOperation(x,n,r);if(I){let w={cacheName:I.name,expiresAt:new Date(I.expiresAt).getTime(),tokenCount:I.tokenCount};return h?await h.set(m,w):Ds.set(m,{...w,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,I.name,n,r)}}return null}async useCacheByName(e,t,n,r,o){if(this.aiImpl.prepareCachedChatReq){let s=await this.aiImpl.prepareCachedChatReq(t,r??{},n);return{preparedRequest:{apiConfig:s.apiConfig,request:s.request}}}return null}async executeCacheOperation(e,t,n){let r=t?.verbose??this.#t;try{n?.addEvent("context_cache.operation",{type:e.type,endpoint:e.apiConfig.name});let o=await Qe({name:e.apiConfig.name,url:this.apiURL,localCall:e.apiConfig.localCall,headers:await this.buildHeaders(e.apiConfig.headers),stream:!1,timeout:this.timeout,verbose:r,fetch:this.fetch,span:n,abortSignal:t?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:t?.retry??this.retry},e.request);return e.parseResponse(o)}catch(o){n?.addEvent("context_cache.error",{type:e.type,error:o instanceof Error?o.message:String(o)});return}}estimateCacheableTokens(e){let t=0;for(let n of e){if(n.role==="system"){t+=n.content.length;continue}if("cache"in n&&n.cache)if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)"cache"in r&&r.cache&&(r.type==="text"?t+=r.text.length:r.type==="image"?t+=1e3:r.type==="audio"?t+=2e3:r.type==="file"&&(t+=500))}else n.role==="assistant"&&n.content&&(t+=n.content.length)}return Math.ceil(t/4)}};function Da(i,e,t){let n=[];if(i.chatPrompt&&Array.isArray(i.chatPrompt)&&i.chatPrompt.length>0)for(let o of i.chatPrompt)switch(o.role){case"system":if(o.content){let s={};t||(s.content=o.content),e.addEvent(bt.GEN_AI_SYSTEM_MESSAGE,s)}break;case"user":if(typeof o.content=="string")n.push(o.content);else if(Array.isArray(o.content))for(let s of o.content)s.type==="text"&&n.push(s.text);break;case"assistant":{let s=o.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params}));if(s&&s.length>0){let a={function_calls:JSON.stringify(s,null,2)};!t&&o.content&&(a.content=o.content),e.addEvent(bt.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(bt.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let s={id:o.functionId};t||(s.content=o.result),e.addEvent(bt.GEN_AI_TOOL_MESSAGE,s);break}}let r={};t||(r.content=n.join(`
169
- `)),e.addEvent(bt.GEN_AI_USER_MESSAGE,r)}function Ga(i,e,t){if(i.modelUsage?.tokens){let n=i.modelUsage.tokens.thoughtsTokens?{[he.LLM_USAGE_THOUGHTS_TOKENS]:i.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(bt.GEN_AI_USAGE,{[he.LLM_USAGE_INPUT_TOKENS]:i.modelUsage.tokens.promptTokens,[he.LLM_USAGE_OUTPUT_TOKENS]:i.modelUsage.tokens.completionTokens??0,[he.LLM_USAGE_TOTAL_TOKENS]:i.modelUsage.tokens.totalTokens,...n})}if(i.results)for(let n=0;n<i.results.length;n++){let r=i.results[n];if(!r||!r.content&&!r.thought&&!r.functionCalls?.length&&!r.finishReason)continue;let o=r.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params})),s={};o&&o.length>0?(t||(s.content=r.content),s.tool_calls=o):t||(s.content=r.content??""),e.addEvent(bt.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(s,null,2)})}}function Ua(i){let e=0;for(let t of i){if(!t||typeof t!="object")throw new Error(`AxMessage array validation failed: Item at index ${e} is not a valid message object`);if(t.role!=="user"&&t.role!=="assistant")throw new Error(`AxMessage array validation failed: Item at index ${e} has invalid role: ${t.role}`);e++}}function Sc(i){let e=new Set;for(let t of i){if(e.has(t.key))throw new Error(`Duplicate model key detected: "${t.key}". Each model key must be unique.`);e.add(t.key)}}var rr=(f=>(f.Claude46Opus="claude-opus-4-6",f.Claude46Sonnet="claude-sonnet-4-6",f.Claude45Opus="claude-opus-4-5-20251101",f.Claude41Opus="claude-opus-4-1-20250805",f.Claude4Opus="claude-opus-4-20250514",f.Claude4Sonnet="claude-sonnet-4-20250514",f.Claude45Sonnet="claude-sonnet-4-5-20250929",f.Claude45Haiku="claude-haiku-4-5",f.Claude37Sonnet="claude-3-7-sonnet-latest",f.Claude35Sonnet="claude-3-5-sonnet-latest",f.Claude35Haiku="claude-3-5-haiku-latest",f.Claude3Opus="claude-3-opus-latest",f.Claude3Sonnet="claude-3-sonnet-20240229",f.Claude3Haiku="claude-3-haiku-20240307",f.Claude21="claude-2.1",f.ClaudeInstant12="claude-instant-1.2",f))(rr||{}),or=(g=>(g.Claude46Opus="claude-opus-4-6",g.Claude46Sonnet="claude-sonnet-4-6",g.Claude45Opus="claude-opus-4-5@20251101",g.Claude41Opus="claude-opus-4-1@20250805",g.Claude4Opus="claude-opus-4@20250514",g.Claude45Sonnet="claude-sonnet-4-5@20250929",g.Claude4Sonnet="claude-sonnet-4@20250514",g.Claude37Sonnet="claude-3-7-sonnet@20250219",g.Claude35SonnetV2="claude-3-5-sonnet-v2@20241022",g.Claude45Haiku="claude-haiku-4-5@20251001",g.Claude35Haiku="claude-3-5-haiku@20241022",g.Claude35Sonnet="claude-3-5-sonnet@20240620",g.Claude3Opus="claude-3-opus@20240229",g.Claude3Haiku="claude-3-haiku@20240307",g))(or||{});var sr=[{name:"claude-opus-4-6",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:128e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-6",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:128e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-6",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-6",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-5-20251101",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-5@20251101",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-5-20250929",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-5@20250929",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-haiku-4-5",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-haiku-4-5@20251001",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-opus-4-1-20250805",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-1@20250805",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4@20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4@20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-7-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-7-sonnet@20250219",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-5-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{structuredOutputs:!0}},{name:"claude-3-5-sonnet@20240620",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{structuredOutputs:!0}},{name:"claude-3-5-sonnet-v2@20241022",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-5-haiku-latest",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:4,maxTokens:8192},{name:"claude-3-5-haiku@20241022",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:8192},{name:"claude-3-opus-latest",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-opus@20240229",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-sonnet-20240229",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-haiku-20240307",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:1.25,maxTokens:4096},{name:"claude-3-haiku@20240307",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:1.25,maxTokens:4096},{name:"claude-2.1",currency:"usd",promptTokenCostPer1M:8,completionTokenCostPer1M:25,maxTokens:4096},{name:"claude-instant-1.2",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:2.24,maxTokens:4096}];var _t=i=>{if(!i||typeof i!="object")return i;let e={...i};if(e.type==="object"||Array.isArray(e.type)&&e.type.includes("object")){if(!e.properties||Object.keys(e.properties).length===0)return e;e.additionalProperties===void 0&&(e.additionalProperties=!1)}return delete e.optional,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([n,r])=>[n,_t(r)]))),e.items&&(e.items=_t(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>_t(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>_t(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>_t(n))),e},Us=()=>structuredClone({model:"claude-3-7-sonnet-latest",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},effortLevelMapping:{minimal:"low",low:"low",medium:"medium",high:"high",highest:"max"},...Te()}),ja=()=>structuredClone({model:"claude-3-7-sonnet@20250219",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},effortLevelMapping:{minimal:"low",low:"low",medium:"medium",high:"high",highest:"max"},...Te()}),Gs=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;usedStructuredOutput=!1;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens??4096,temperature:e.temperature,topP:e.topP,topK:e.topK,stream:e.stream,stopSequences:e.stopSequences,endSequences:e.endSequences,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,n:e.n}}createChatReq=async(e,t)=>{this.currentPromptConfig=t;let n=e.model,r=e.modelConfig?.stream??this.config.stream,o;this.isVertex?o={name:r?`/models/${n}:streamRawPredict?alt=sse`:`/models/${n}:rawPredict`}:o={name:"/messages"};let s;if(e.functionCall&&e.functions&&e.functions.length>0)if(typeof e.functionCall=="string")switch(e.functionCall){case"auto":s={tool_choice:{type:"auto"}};break;case"required":s={tool_choice:{type:"any"}};break;case"none":throw new Error("functionCall none not supported")}else if("function"in e.functionCall)s={tool_choice:{type:"tool",name:e.functionCall.function.name}};else throw new Error("Invalid function call type, must be string or object");let a=e.chatPrompt.some(F=>"cache"in F&&F.cache)||e.functions?.some(F=>F.cache),l=e.chatPrompt.filter(F=>F.role==="system"),u=l.map((F,W)=>({type:"text",text:F.content,...F.cache||a&&W===l.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter(F=>F.role!=="system"),p=e.functions?.map((F,W,P)=>{let D={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},K=F.parameters?_t(F.parameters):void 0;return K===void 0||K&&typeof K=="object"&&Object.keys(K).length===0?K={...D}:K&&typeof K=="object"&&K.type==="object"&&(!("properties"in K)||!K.properties||Object.keys(K.properties).length===0)&&(K={...K,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:F.name,description:F.description,input_schema:K,...F.cache||a&&W===P.length-1?{cache_control:{type:"ephemeral"}}:{}}}),m=(this.config.tools??[]).map(F=>F&&typeof F=="object"&&"type"in F?F:{name:F.name,description:F.description,input_schema:F.input_schema?_t(F.input_schema):void 0,...F.cache_control?{cache_control:F.cache_control}:{}}),g=[...p??[],...m];g.length===0&&(g=void 0);let h=e.modelConfig?.maxTokens??this.config.maxTokens,f=e.modelConfig?.stopSequences??this.config.stopSequences,C=e.modelConfig?.temperature,x=e.modelConfig?.topP,I=e.modelConfig?.topK??this.config.topK,w=e.modelConfig?.n??this.config.n;if(w&&w>1)throw new Error("Anthropic does not support sampling (n > 1)");let A=F=>F.includes("claude-opus-4-6"),T=F=>F.includes("claude-opus-4-5"),k,E,M=n;if(t?.thinkingTokenBudget){let F=this.config.thinkingTokenBudgetLevels,W=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")k=void 0,E=void 0;else{let P=t.thinkingTokenBudget;if(A(M))k={type:"adaptive"},E={effort:W?.[P]??"medium"};else if(T(M)){k={type:"enabled",budget_tokens:F?.[P]??1e4};let K=W?.[P]??"medium";K==="max"&&(K="high"),E={effort:K}}else k={type:"enabled",budget_tokens:F?.[P]??1e4}}}let B=kc(c,!!k);B.some(F=>F.role==="assistant"&&Array.isArray(F.content)&&F.content.length>0&&F.content[0]?.type==="tool_use")&&(k=void 0,E=void 0);let O;if(this.usedStructuredOutput=!1,e.responseFormat&&e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let F=e.responseFormat.schema.schema||e.responseFormat.schema;O={type:"json_schema",schema:_t(F)},this.usedStructuredOutput=!0}let j={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...h?{max_tokens:h}:{},...f&&f.length>0?{stop_sequences:f}:{},...C!==void 0&&!k?{temperature:C}:{},...x!==void 0&&(!k||x>=.95)?{top_p:x}:{},...I&&!k?{top_k:I}:{},...s,...g?{tools:g}:{},...r?{stream:!0}:{},...u?{system:u}:{},...k?{thinking:k}:{},...E?{output_config:E}:{},...O?{output_format:O}:{},messages:B};return[o,j]};createChatResp=e=>{if(e.type==="error")throw new Me(e.error.message,void 0,void 0);let t=Ba(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o=[],s=[],a=[];for(let c of e.content)switch(c.type){case"text":if(r+=c.text??"",Array.isArray(c.citations))for(let p of c.citations)p?.url&&a.push({url:String(p.url),title:typeof p.title=="string"?p.title:void 0,snippet:typeof p.cited_text=="string"?p.cited_text:void 0});break;case"thinking":if(n){let p=c.thinking??"",d=c.signature;s.push({data:p,encrypted:!1,...typeof d=="string"?{signature:d}:{}})}break;case"redacted_thinking":if(n){let p=c.data??"",d=c.signature;s.push({data:p,encrypted:!0,...typeof d=="string"?{signature:d}:{}})}break;case"tool_use":o.push({id:c.id,type:"function",function:{name:c.name,params:c.input}});break}let l={index:0,id:e.id,finishReason:t};r&&(l.content=r),s.length>0&&(l.thoughtBlocks=s,l.thought=s.map(c=>c.data).join("")),o.length>0&&(l.functionCalls=o),a.length>0&&(l.citations=a);let u=[l];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens+(e.usage.cache_creation_input_tokens||0)+(e.usage.cache_read_input_tokens||0),cacheCreationTokens:e.usage.cache_creation_input_tokens,cacheReadTokens:e.usage.cache_read_input_tokens},{results:u,remoteId:e.id}};createChatStreamResp=(e,t)=>{if(!("type"in e))throw new Error("Invalid Anthropic streaming event");let n=t;if(n.indexIdMap||(n.indexIdMap={}),e.type==="error"){let{error:o}=e;throw new Me(o.message,void 0,void 0)}let r=0;if(e.type==="message_start"){let{message:o}=e,s=[{index:r,content:"",id:o.id}];return this.tokensUsed={promptTokens:o.usage?.input_tokens??0,completionTokens:o.usage?.output_tokens??0,totalTokens:(o.usage?.input_tokens??0)+(o.usage?.output_tokens??0)+(o.usage?.cache_creation_input_tokens??0)+(o.usage?.cache_read_input_tokens??0),cacheCreationTokens:o.usage?.cache_creation_input_tokens,cacheReadTokens:o.usage?.cache_read_input_tokens},{results:s}}if(e.type==="content_block_start"){let{content_block:o}=e;if(o.type==="text"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="tool_use"&&typeof o.id=="string"&&typeof e.index=="number"&&!n.indexIdMap[e.index]){n.indexIdMap[e.index]=o.id;let s=[{id:o.id,type:"function",function:{name:o.name,params:""}}];return{results:[{index:r,functionCalls:s}]}}if(o.type==="web_search_tool_result"||o.type==="server_tool_use")return{results:[{index:r,content:""}]}}if(e.type==="content_block_delta"){let{delta:o}=e;if(o.type==="citations_delta"){let s=o.citation;if(s&&typeof s.url=="string"&&s.url.length>0){let a=[{url:String(s.url),title:typeof s.title=="string"?s.title:void 0,snippet:typeof s.cited_text=="string"?s.cited_text:void 0}];return{results:[{index:r,content:"",citations:a}]}}return{results:[{index:r,content:""}]}}if(o.type==="text_delta"){let s=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&s.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...s.length?{citations:s}:{}}]}}if(o.type==="thinking_delta")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,thoughtBlocks:[{data:"",encrypted:!1,signature:o.signature}]}]};if(o.type==="input_json_delta"){let s=n.indexIdMap[e.index];if(!s)return{results:[{index:r,content:""}]};let a=[{id:s,type:"function",function:{name:"",params:o.partial_json}}];return{results:[{index:r,functionCalls:a}]}}}if(e.type==="message_delta"){let{delta:o,usage:s}=e;return this.tokensUsed={promptTokens:this.tokensUsed?.promptTokens??0,completionTokens:s.output_tokens,totalTokens:(this.tokensUsed?.promptTokens??0)+s.output_tokens+(this.tokensUsed?.cacheCreationTokens??0)+(this.tokensUsed?.cacheReadTokens??0),cacheCreationTokens:this.tokensUsed?.cacheCreationTokens,cacheReadTokens:this.tokensUsed?.cacheReadTokens},{results:[{index:r,content:"",finishReason:Ba(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},rn=class i extends Be{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,config:r,options:o,models:s}){let a=t!==void 0&&n!==void 0,l,u;if(a){if(!e)throw new Error("Anthropic Vertex API key not set");if(typeof e!="function")throw new Error("Anthropic Vertex API key must be a function for token-based authentication");l=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,u=async()=>({Authorization:`Bearer ${await e()}`,"anthropic-beta":"web-search-2025-03-05"})}else{if(!e)throw new Error("Anthropic API key not set");l="https://api.anthropic.com/v1",u=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"structured-outputs-2025-11-13, web-search-2025-03-05","x-api-key":typeof e=="function"?await e():e})}let c={...Us(),...r},p=new Gs(c,a),d=g=>{let h=at({model:g,modelInfo:sr,models:s});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:h?.supported?.structuredOutputs??!1,functionCot:!0,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:5*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!0,types:["ephemeral"],cacheBreakpoints:!1},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}},m=s?.map(g=>{let h=g,f=h?.config;if(!f)return g;let C={};f.maxTokens!==void 0&&(C.maxTokens=f.maxTokens),f.temperature!==void 0&&(C.temperature=f.temperature),f.topP!==void 0&&(C.topP=f.topP),f.topK!==void 0&&(C.topK=f.topK),f.presencePenalty!==void 0&&(C.presencePenalty=f.presencePenalty),f.frequencyPenalty!==void 0&&(C.frequencyPenalty=f.frequencyPenalty),f.stopSequences!==void 0&&(C.stopSequences=f.stopSequences),f.endSequences!==void 0&&(C.endSequences=f.endSequences),f.stream!==void 0&&(C.stream=f.stream),f.n!==void 0&&(C.n=f.n);let x={...h};Object.keys(C).length>0&&(x.modelConfig={...h.modelConfig??{},...C});let I=f.thinking?.thinkingTokenBudget;if(typeof I=="number"){let w=c.thinkingTokenBudgetLevels,A=[["minimal",w?.minimal??1024],["low",w?.low??5e3],["medium",w?.medium??1e4],["high",w?.high??2e4],["highest",w?.highest??32e3]],T="minimal",k=Number.POSITIVE_INFINITY;for(let[E,M]of A){let $=Math.abs(I-M);$<k&&(k=$,T=E)}x.thinkingTokenBudget=T}return f.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!f.thinking.includeThoughts),x});super(p,{name:"Anthropic",apiURL:l,headers:u,modelInfo:sr,defaults:{model:c.model},options:o,supportFor:d,models:m??s})}};function kc(i,e){let t=i.map(r=>{switch(r.role){case"function":return{role:"user",content:[{type:"tool_result",content:r.result,tool_use_id:r.functionId,...r.isError?{is_error:!0}:{},...r.cache?{cache:{type:"ephemeral"}}:{}}]};case"user":return typeof r.content=="string"?{role:"user",content:r.content,...r.cache?{cache_control:{type:"ephemeral"}}:{}}:{role:"user",content:r.content.map(s=>{switch(s.type){case"text":return{type:"text",text:s.text,...s.cache?{cache_control:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:s.mimeType,data:s.image},...s.cache?{cache_control:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",s=[],a=r.thoughtBlocks;if(Array.isArray(a)&&a.length>0)for(let l of a)l.encrypted?s.push(l.signature?{type:"redacted_thinking",data:l.data,signature:l.signature}:{type:"redacted_thinking",data:l.data}):s.push(l.signature?{type:"thinking",thinking:l.data,signature:l.signature}:{type:"thinking",thinking:l.data});if(typeof r.content=="string"&&(s.length>0?o=[...s,{type:"text",text:r.content}]:o=r.content),typeof r.functionCalls<"u"&&(o=r.functionCalls.map(l=>{let u={};if(typeof l.function.params=="string"){let c=l.function.params;if(c.trim().length===0)u={};else try{u=JSON.parse(c)}catch{throw new Error(`Failed to parse function params JSON: ${c}`)}}else typeof l.function.params=="object"&&(u=l.function.params);return{type:"tool_use",id:l.id,name:l.function.name,input:u,...r.cache?{cache_control:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&s.length>0&&(o=[...s,...o])),r.cache){if(typeof o=="string")o=[{type:"text",text:o,cache_control:{type:"ephemeral"}}];else if(Array.isArray(o)&&o.length>0){let l=o.length-1,u=o[l];u&&u.type==="text"&&(o[l]={...u,cache_control:{type:"ephemeral"}})}}return{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=vc(t);return Oc(n)}function vc(i){let e=[];for(let[t,n]of i.entries()){if(n.role!=="assistant"){e.push(n);continue}if(t>0&&i.at(t-1)?.role==="assistant"){let r=e.pop();e.push({...r||{},...n})}else e.push(n)}return e}function Oc(i){return i.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function Ba(i){if(i)switch(i){case"stop_sequence":return"stop";case"max_tokens":return"length";case"tool_use":return"function_call";case"end_turn":return"stop";default:return"stop"}}var ir=(D=>(D.GPT4="gpt-4",D.GPT41="gpt-4.1",D.GPT41Mini="gpt-4.1-mini",D.GPT41Nano="gpt-4.1-nano",D.GPT4O="gpt-4o",D.GPT4OMini="gpt-4o-mini",D.GPT4ChatGPT4O="chatgpt-4o-latest",D.GPT4Turbo="gpt-4-turbo",D.GPT35Turbo="gpt-3.5-turbo",D.GPT35TurboInstruct="gpt-3.5-turbo-instruct",D.GPT35TextDavinci002="text-davinci-002",D.GPT3TextBabbage002="text-babbage-002",D.GPT3TextAda001="text-ada-001",D.GPT5="gpt-5",D.GPT5Nano="gpt-5-nano",D.GPT5Mini="gpt-5-mini",D.GPT5Chat="gpt-5-chat",D.GPT5ChatLatest="gpt-5-chat-latest",D.GPT5Codex="gpt-5-codex",D.GPT5Pro="gpt-5-pro",D.GPT51="gpt-5.1",D.GPT51ChatLatest="gpt-5.1-chat-latest",D.GPT51Codex="gpt-5.1-codex",D.GPT51CodexMini="gpt-5.1-codex-mini",D.GPT51CodexMax="gpt-5.1-codex-max",D.GPT52="gpt-5.2",D.GPT52ChatLatest="gpt-5.2-chat-latest",D.GPT52Codex="gpt-5.2-codex",D.GPT52Pro="gpt-5.2-pro",D.O1="o1",D.O1Mini="o1-mini",D.O3="o3",D.O3Mini="o3-mini",D.O4Mini="o4-mini",D))(ir||{}),on=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(on||{});var sn=(N=>(N.GPT4="gpt-4",N.GPT41="gpt-4.1",N.GPT41Mini="gpt-4.1-mini",N.GPT41Nano="gpt-4.1-nano",N.GPT4O="gpt-4o",N.GPT4OMini="gpt-4o-mini",N.GPT4ChatGPT4O="chatgpt-4o-latest",N.GPT4Turbo="gpt-4-turbo",N.GPT35Turbo="gpt-3.5-turbo",N.GPT35TurboInstruct="gpt-3.5-turbo-instruct",N.GPT35TextDavinci002="text-davinci-002",N.GPT3TextBabbage002="text-babbage-002",N.GPT3TextAda001="text-ada-001",N.GPT5="gpt-5",N.GPT5Nano="gpt-5-nano",N.GPT5Mini="gpt-5-mini",N.GPT5Chat="gpt-5-chat",N.GPT5ChatLatest="gpt-5-chat-latest",N.GPT5Codex="gpt-5-codex",N.GPT5Pro="gpt-5-pro",N.GPT51="gpt-5.1",N.GPT51ChatLatest="gpt-5.1-chat-latest",N.GPT51Codex="gpt-5.1-codex",N.GPT51CodexMini="gpt-5.1-codex-mini",N.GPT51CodexMax="gpt-5.1-codex-max",N.GPT52="gpt-5.2",N.GPT52ChatLatest="gpt-5.2-chat-latest",N.GPT52Codex="gpt-5.2-codex",N.GPT52Pro="gpt-5.2-pro",N.O1Pro="o1-pro",N.O1="o1",N.O1Mini="o1-mini",N.O3Pro="o3-pro",N.O3="o3",N.O3Mini="o3-mini",N.O4Mini="o4-mini",N))(sn||{});var an=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,supported:{structuredOutputs:!0}},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6,supported:{structuredOutputs:!0}},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6,supported:{structuredOutputs:!0}},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,supported:{structuredOutputs:!0}},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.4,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-pro",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:120,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-codex",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:40,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex-max",currency:"usd",promptTokenCostPer1M:2.5,completionTokenCostPer1M:20,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-chat-latest",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-codex",currency:"usd",promptTokenCostPer1M:1.5,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:160,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{structuredOutputs:!0}},{name:"o1-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:14.4,supported:{structuredOutputs:!0}},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{structuredOutputs:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{structuredOutputs:!0}},{name:"text-embedding-ada-002",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"text-embedding-3-small",currency:"usd",promptTokenCostPer1M:.02,completionTokenCostPer1M:.02},{name:"text-embedding-3-large",currency:"usd",promptTokenCostPer1M:.13,completionTokenCostPer1M:.13}],Xr=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,supported:{structuredOutputs:!0}},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6,supported:{structuredOutputs:!0}},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6,supported:{structuredOutputs:!0}},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,supported:{structuredOutputs:!0}},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.4,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-pro",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:120,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-codex",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:40,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex-max",currency:"usd",promptTokenCostPer1M:2.5,completionTokenCostPer1M:20,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-chat-latest",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-codex",currency:"usd",promptTokenCostPer1M:1.5,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:160,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o1-pro",currency:"usd",promptTokenCostPer1M:150,completionTokenCostPer1M:600,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0},isExpensive:!0},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o3-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:80,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0},isExpensive:!0},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o3-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}}];var Mc=i=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(i)||e.includes(i)},jt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...Te()}),Zr=()=>structuredClone({...jt(),model:"gpt-5"}),eo=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...je()}),to=()=>({...jt(),model:"gpt-5-nano"}),Bs=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.chatReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=(e,t)=>{let n=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let r={name:"/chat/completions"},o=e.functions?.map(m=>({type:"function",function:{name:m.name,description:m.description,parameters:m.parameters}})),s=!e.functionCall&&e.functions&&e.functions.length>0?"auto":e.functionCall,a=Ec(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,u=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=Mc(n),d={model:n,messages:a,...e.responseFormat?{response_format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:e.responseFormat}:this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...s?{tool_choice:s}:{},...p?{}:{...(e.modelConfig?.maxTokens??this.config.maxTokens)!==void 0?{max_completion_tokens:e.modelConfig?.maxTokens??this.config.maxTokens}:{},...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},...(e.modelConfig?.n??this.config.n)!==void 0?{n:e.modelConfig?.n??this.config.n}:{},...(e.modelConfig?.presencePenalty??this.config.presencePenalty)!==void 0?{presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty}:{},...l!==void 0?{frequency_penalty:l}:{}},...(e.modelConfig?.stopSequences??this.config.stop)&&(e.modelConfig?.stopSequences??this.config.stop).length>0?{stop:e.modelConfig?.stopSequences??this.config.stop}:{},...this.config.logitBias!==void 0?{logit_bias:this.config.logitBias}:{},...u&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...c?{store:c}:{},...this.config.serviceTier?{service_tier:this.config.serviceTier}:{},...this.config.user?{user:this.config.user}:{}};if(this.config.reasoningEffort&&(d.reasoning_effort=this.config.reasoningEffort),this.config.webSearchOptions&&(d.web_search_options={...this.config.webSearchOptions.searchContextSize&&{search_context_size:this.config.webSearchOptions.searchContextSize},...this.config.webSearchOptions.userLocation&&{user_location:{approximate:{type:"approximate",...this.config.webSearchOptions.userLocation.approximate.city&&{city:this.config.webSearchOptions.userLocation.approximate.city},...this.config.webSearchOptions.userLocation.approximate.country&&{country:this.config.webSearchOptions.userLocation.approximate.country},...this.config.webSearchOptions.userLocation.approximate.region&&{region:this.config.webSearchOptions.userLocation.approximate.region},...this.config.webSearchOptions.userLocation.approximate.timezone&&{timezone:this.config.webSearchOptions.userLocation.approximate.timezone}}}}}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d.reasoning_effort=void 0;break;case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":d.reasoning_effort="high";break;case"high":d.reasoning_effort="high";break;case"highest":d.reasoning_effort="high";break}if(!d.reasoning_effort&&t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":case"high":case"highest":d.reasoning_effort="high";break}return this.chatReqUpdater&&(d=this.chatReqUpdater(d)),[r,d]};createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]};createChatResp(e){let{id:t,usage:n,choices:r,error:o}=e;if(o)throw o;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{results:r.map(a=>{if(a.message.refusal)throw new Me(a.message.refusal,e.model,e.id);let l=qa(a.finish_reason),u=a.message.tool_calls?.map(({id:c,function:{arguments:p,name:d}})=>({id:c,type:"function",function:{name:d,params:p}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(c=>c?.type==="url_citation"&&c.url_citation).map(c=>({url:c.url_citation?.url,title:c.url_citation?.title,description:c.url_citation?.description})),functionCalls:u,finishReason:l}}),remoteId:t}}createChatStreamResp(e,t){let{id:n,usage:r,choices:o}=e;this.tokensUsed=r?{promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens,totalTokens:r.total_tokens}:void 0;let s=t;return s.indexIdMap||(s.indexIdMap={}),{results:o.map(({index:l,delta:{content:u,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:h})=>{if(p)throw new Me(p,void 0,n);let f=qa(h),C=d?.map(({id:x,index:I,function:{name:w,arguments:A}})=>{typeof x=="string"&&typeof I=="number"&&!s.indexIdMap[I]&&(s.indexIdMap[I]=x);let T=s.indexIdMap[I];return T?{id:T,type:"function",function:{name:w,params:A}}:null}).filter(x=>x!==null);return{index:l,content:u??void 0,role:c,thought:m,citations:g?.filter(x=>x?.type==="url_citation"&&x.url_citation).map(x=>({url:x.url_citation?.url,title:x.url_citation?.title,description:x.url_citation?.description})),functionCalls:C,finishReason:f,id:n}})}}createEmbedResp(e){let{data:t,usage:n}=e;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{embeddings:t.map(r=>r.embedding)}}},qa=i=>{switch(i){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function Ec(i){return i.chatPrompt.map(t=>{switch(t.role){case"system":return{role:"system",content:t.content};case"user":{let n=Array.isArray(t.content)?t.content.map(r=>{switch(r.type){case"text":return{type:"text",text:r.text};case"image":return{type:"image_url",image_url:{url:`data:${r.mimeType};base64,${r.image}`,details:r.details??"auto"}};case"audio":return{type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}};default:throw new Error("Invalid content type")}}):t.content;return{role:"user",...t.name?{name:t.name}:{},content:n}}case"assistant":{let n=t.functionCalls?.map(r=>({id:r.id,type:"function",function:{name:r.function.name,arguments:typeof r.function.params=="object"?JSON.stringify(r.function.params):r.function.params}}));if(n&&n.length>0)return{role:"assistant",...t.content?{content:t.content}:{},name:t.name,tool_calls:n};if(t.content===void 0)throw new Error("Assistant content is required when no tool calls are provided");return{role:"assistant",content:t.content,...t.name?{name:t.name}:{}}}case"function":return{role:"tool",content:t.result,tool_call_id:t.functionId};default:throw new Error("Invalid role")}})}var De=class extends Be{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:s,chatReqUpdater:a,supportFor:l}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new Bs(t,n?.streamingUsage??!0,a);super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model,embedModel:t.embedModel},options:n,supportFor:l,models:s})}},ln=class extends De{constructor({apiKey:e,apiURL:t,config:n,options:r,models:o,modelInfo:s}){if(!e||e==="")throw new Error("OpenAI API key not set");s=[...an,...s??[]];let a=u=>{let c=at({model:u,modelInfo:s,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,structuredOutputs:c?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","ogg"],maxDuration:25*60},files:{supported:!0,formats:["text/plain","application/pdf","image/jpeg","image/png"],maxSize:512*1024*1024,uploadMethod:"upload"},urls:{supported:!1,webSearch:!0,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:c?.supported?.thinkingBudget??!1,multiTurn:!0}},l=o?.map(u=>{let c=u,p=c?.config;if(!p)return u;let d={};p.maxTokens!==void 0&&(d.maxTokens=p.maxTokens),p.temperature!==void 0&&(d.temperature=p.temperature),p.topP!==void 0&&(d.topP=p.topP),p.presencePenalty!==void 0&&(d.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(d.frequencyPenalty=p.frequencyPenalty);let m=p.stopSequences??p.stop;m!==void 0&&(d.stopSequences=m),p.n!==void 0&&(d.n=p.n),p.stream!==void 0&&(d.stream=p.stream);let g={...c};Object.keys(d).length>0&&(g.modelConfig={...c.modelConfig??{},...d});let h=p?.thinking?.thinkingTokenBudget;if(typeof h=="number"){let f=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],C="minimal",x=Number.POSITIVE_INFINITY;for(let[I,w]of f){let A=Math.abs(h-w);A<x&&(x=A,C=I)}g.thinkingTokenBudget=C}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...jt(),...n},options:r,modelInfo:s,models:l??o,supportFor:a}),super.setName("OpenAI")}};var Pc=i=>{let e=i.trim();if(!e)return e;if(e.includes("api-version=")){let t=e.indexOf("api-version="),n=e.slice(t);return new URLSearchParams(n).get("api-version")??e}return e},Fc=i=>{let e=i.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},js=jt,za=eo,Ha=to,Ka=Zr,un=class extends De{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:s,models:a,modelInfo:l}){if(!e||e==="")throw new Error("Azure OpenAPI API key not set");if(!t||t==="")throw new Error("Azure OpenAPI resource name not set");if(!n||n==="")throw new Error("Azure OpenAPI deployment id not set");let u={...js(),...o},c=Pc(r),p=Fc(c);l=[...an,...l??[]];let d=g=>{let h=at({model:g,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:h?.supported?.thinkingBudget??!1,hasShowThoughts:h?.supported?.showThoughts??!1,structuredOutputs:p&&(h?.supported?.structuredOutputs??!1),functionCot:!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:h?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:u,options:s,models:a,modelInfo:l,supportFor:d});let m=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${c}`,m).href),super.setHeaders(async()=>({"api-key":e}))}};var no=class i{services;currentServiceIndex=0;currentService;debug;initialBackoffMs;maxBackoffMs;maxRetries;serviceFailures=new Map;constructor(e,t){if(e.length===0)throw new Error("No AI services provided.");_c(e),this.services=[...e].sort(t?.comparator??i.metricComparator);let n=this.services[this.currentServiceIndex];if(n===void 0)throw new Error("Error initializing the AI services.");this.currentService=n,this.debug=t?.debug??!0,this.initialBackoffMs=t?.initialBackoffMs??1e3,this.maxBackoffMs=t?.maxBackoffMs??32e3,this.maxRetries=t?.maxRetries??3}static create(e,t){return new i(e,t)}getLastUsedChatModel(){return this.currentService.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.currentService.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.currentService.getLastUsedModelConfig()}static inputOrderComparator=()=>0;static metricComparator=(e,t)=>{let n=e.getMetrics(),r=t.getMetrics();return n.latency.chat.mean-r.latency.chat.mean};getModelList(){for(let e of this.services){let t=e.getModelList();if(t)return t}}getNextService(e,t){let n=t+1,r=e[n];return r===void 0?{service:void 0,index:n}:{service:r,index:n}}reset(){this.currentServiceIndex=0;let e=this.services[this.currentServiceIndex];if(e===void 0)throw new Error("No AI services provided.");this.currentService=e}getName(){return this.currentService.getName()}getId(){return this.currentService.getId()}getFeatures(e){let t={functions:!1,streaming:!1,thinking:!1,multiTurn:!1,structuredOutputs:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]}};for(let n of this.services){let r=n.getFeatures(e);r.functions&&(t.functions=!0),r.streaming&&(t.streaming=!0),r.thinking&&(t.thinking=!0),r.multiTurn&&(t.multiTurn=!0),r.structuredOutputs&&(t.structuredOutputs=!0),r.functionCot&&(t.functionCot=!0),r.hasThinkingBudget&&(t.hasThinkingBudget=!0),r.hasShowThoughts&&(t.hasShowThoughts=!0),r.media.images.supported&&(t.media.images.supported=!0),t.media.images.formats=Array.from(new Set([...t.media.images.formats,...r.media.images.formats])),r.media.audio.supported&&(t.media.audio.supported=!0),t.media.audio.formats=Array.from(new Set([...t.media.audio.formats,...r.media.audio.formats])),r.media.files.supported&&(t.media.files.supported=!0),t.media.files.formats=Array.from(new Set([...t.media.files.formats,...r.media.files.formats])),r.media.files.uploadMethod!=="none"&&(t.media.files.uploadMethod=r.media.files.uploadMethod),r.media.urls.supported&&(t.media.urls.supported=!0),r.media.urls.webSearch&&(t.media.urls.webSearch=!0),r.media.urls.contextFetching&&(t.media.urls.contextFetching=!0),r.caching.supported&&(t.caching.supported=!0),t.caching.types=Array.from(new Set([...t.caching.types,...r.caching.types]))}return t}getMetrics(){let e={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}},t=0,n=0,r=0,o=0;for(let s of this.services){let a=s.getMetrics();e.errors.chat.count+=a.errors.chat.count,e.errors.chat.total+=a.errors.chat.total,e.errors.embed.count+=a.errors.embed.count,e.errors.embed.total+=a.errors.embed.total;let l=a.latency.chat.samples.length;l>0&&(t+=a.latency.chat.mean*l,n+=l);let u=a.latency.embed.samples.length;u>0&&(r+=a.latency.embed.mean*u,o+=u)}e.errors.chat.total>0&&(e.errors.chat.rate=e.errors.chat.count/e.errors.chat.total),e.errors.embed.total>0&&(e.errors.embed.rate=e.errors.embed.count/e.errors.embed.total),n>0&&(e.latency.chat.mean=t/n),o>0&&(e.latency.embed.mean=r/o);for(let s of this.services){let a=s.getMetrics();e.latency.chat.p95=Math.max(e.latency.chat.p95,a.latency.chat.p95),e.latency.chat.p99=Math.max(e.latency.chat.p99,a.latency.chat.p99),e.latency.embed.p95=Math.max(e.latency.embed.p95,a.latency.embed.p95),e.latency.embed.p99=Math.max(e.latency.embed.p99,a.latency.embed.p99)}return e}canRetryService(e){let t=this.serviceFailures.get(e.getId());if(!t)return!0;let{retries:n,lastFailureTime:r}=t,o=Date.now()-r,s=Math.min(this.initialBackoffMs*2**n,this.maxBackoffMs);return o>=s}handleFailure(e,t){let r=(this.serviceFailures.get(e.getId())?.retries??0)+1;this.serviceFailures.set(e.getId(),{retries:r,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${e.getName()} failed (retry ${r}/${this.maxRetries})`,t)}handleSuccess(e){this.serviceFailures.delete(e.getId())}async chat(e,t){let n=e.responseFormat?.type==="json_schema",r=e.capabilities,o=r?.requiresImages,s=r?.requiresAudio,a=this.services,l=e.model;if((n||o||s)&&(a=this.services.filter(p=>{let d=p.getFeatures(l);return!(n&&!d.structuredOutputs||o&&!d.media.images.supported||s&&!d.media.audio.supported)}),a.length===0)){let p=[];throw n&&p.push("structured outputs"),o&&p.push("images"),s&&p.push("audio"),new Error(`No services available that support required capabilities: ${p.join(", ")}.`)}let u=0,c=a[u];if(!c)throw new Error("No matching AI services available for request.");for(this.currentService=c;;){if(!this.canRetryService(c)){let p=this.getNextService(a,u);if(!p.service)throw new Error(`All candidate services exhausted (tried ${a.length} service(s))`);c=p.service,u=p.index,this.currentService=c;continue}try{let p=await c.chat(e,t);return this.handleSuccess(c),p}catch(p){if(!(p instanceof Je))throw p;switch(p.constructor){case Pt:throw p;case tt:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case He:break;case Et:break;case lt:break;case st:break;default:throw p}if(this.handleFailure(c,p),(this.serviceFailures.get(c.getId())?.retries??0)>=this.maxRetries){let m=this.getNextService(a,u);if(this.debug&&console.warn(`AxBalancer: Switching to service ${m.service?.getName()??"none"}`,p),!m.service)throw p;c=m.service,u=m.index,this.currentService=c}}}}async embed(e,t){this.reset();let n=this.currentServiceIndex;for(;;){if(!this.canRetryService(this.currentService)){let r=this.getNextService(this.services,n);if(!r.service)throw new Error(`All services exhausted (tried ${this.services.length} service(s))`);this.currentService=r.service,n=r.index,this.currentServiceIndex=n;continue}try{let r=await this.currentService.embed(e,t);return this.handleSuccess(this.currentService),r}catch(r){if(!(r instanceof Je)||r instanceof tt&&![408,429,500,502,503,504].includes(r.status)||r instanceof Pt)throw r;if(this.handleFailure(this.currentService,r),(this.serviceFailures.get(this.currentService.getId())?.retries??0)>=this.maxRetries){let s=this.getNextService(this.services,n);if(!s.service)throw r;this.currentService=s.service,n=s.index,this.currentServiceIndex=n}}}}setOptions(e){for(let t of this.services)t.setOptions(e);this.currentService.setOptions(e),this.debug=e.debug??this.debug}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function _c(i){let e=i.find(r=>r.getModelList()!==void 0);if(!e)return;let t=e.getModelList();if(!t)throw new Error("No model list found in any service.");let n=new Set(t.map(r=>r.key));for(let r=0;r<i.length;r++){let o=i[r];if(!o)throw new Error(`Service at index ${r} is undefined`);let s=o.getModelList();if(!s)throw new Error(`Service at index ${r} (${o.getName()}) has no model list while another service does.`);let a=new Set(s.map(l=>l.key));for(let l of n)if(!a.has(l))throw new Error(`Service at index ${r} (${o.getName()}) is missing model "${l}"`);for(let l of a)if(!n.has(l))throw new Error(`Service at index ${r} (${o.getName()}) has extra model "${l}"`)}}function qt(i){let e=!1,t=!1,n=!1,r=!1,o=!1,s=!1,a=!1,l=new Set,u=0;if(i.chatPrompt&&Array.isArray(i.chatPrompt))for(let c of i.chatPrompt){if(c.role==="user"&&Array.isArray(c.content))for(let p of c.content)switch(l.add(p.type),p.type){case"image":e=!0,p.cache&&(a=!0),u+=85;break;case"audio":t=!0,p.cache&&(a=!0),u+=p.duration||60;break;case"file":n=!0,p.cache&&(a=!0),u+=Math.ceil((p.extractedText?.length||1e3)/4);break;case"url":r=!0,p.cache&&(a=!0),u+=Math.ceil((p.cachedContent?.length||2e3)/4);break;case"text":p.cache&&(a=!0),u+=Math.ceil(p.text.length/4);break}else"content"in c&&typeof c.content=="string"&&(u+=Math.ceil(c.content.length/4));"cache"in c&&c.cache&&(a=!0)}return i.functions&&i.functions.length>0&&(o=!0),i.modelConfig?.stream===!0&&(s=!0),i.capabilities&&(i.capabilities.requiresImages&&(e=!0),i.capabilities.requiresAudio&&(t=!0),i.capabilities.requiresFiles&&(n=!0),i.capabilities.requiresWebSearch&&(r=!0)),{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r,requiresFunctions:o,requiresStreaming:s,requiresCaching:a,contentTypes:l,estimatedTokens:u}}function qs(i,e){let t=i.getFeatures(),n=[],r=[],o=[];if(e.hasImages&&!t.media.images.supported&&(n.push("Image support"),o.push("Use altText for images or imageToText service")),e.hasAudio&&!t.media.audio.supported&&(n.push("Audio support"),o.push("Pre-transcribe audio or use transcription field")),e.hasFiles&&!t.media.files.supported&&(n.push("File support"),o.push("Pre-extract text content or use extractedText field")),e.hasUrls&&!t.media.urls.supported&&(n.push("URL/Web search support"),o.push("Pre-fetch content or use cachedContent field")),e.requiresFunctions&&!t.functions&&n.push("Function calling"),e.requiresStreaming&&!t.streaming&&(n.push("Streaming responses"),o.push("Use non-streaming mode")),e.requiresCaching&&!t.caching.supported&&(n.push("Content caching"),o.push("Repeated content will not be cached")),e.hasImages&&t.media.images.supported){let a=t.media.images.maxSize;a&&a<10*1024*1024&&r.push(`Image size limit is ${Math.round(a/(1024*1024))}MB`)}if(e.hasAudio&&t.media.audio.supported){let a=t.media.audio.maxDuration;a&&a<600&&r.push(`Audio duration limit is ${Math.round(a/60)} minutes`)}return{isSupported:n.length===0,missingCapabilities:n,warnings:r,alternatives:o}}function ro(i,e){return i.map(t=>{let n=t.getFeatures(),r=qs(t,e),o=0,s=[];return o+=10,e.hasImages&&n.media.images.supported&&(o+=25,s.push("Images"),n.media.images.detailLevels?.includes("high")&&(o+=5),n.media.images.maxSize&&n.media.images.maxSize>10*1024*1024&&(o+=3)),e.hasAudio&&n.media.audio.supported&&(o+=25,s.push("Audio"),n.media.audio.maxDuration&&n.media.audio.maxDuration>600&&(o+=5)),e.hasFiles&&n.media.files.supported&&(o+=25,s.push("Files"),n.media.files.uploadMethod==="cloud"&&(o+=3)),e.hasUrls&&n.media.urls.supported&&(o+=25,s.push("URLs"),n.media.urls.webSearch&&(o+=5)),e.requiresFunctions&&n.functions&&(o+=15,s.push("Functions"),n.functionCot&&(o+=3)),e.requiresStreaming&&n.streaming&&(o+=10,s.push("Streaming")),e.requiresCaching&&n.caching.supported&&(o+=8,s.push("Caching"),n.caching.types.includes("persistent")&&(o+=3)),n.thinking&&(o+=2),n.multiTurn&&(o+=2),n.hasThinkingBudget&&(o+=1),n.hasShowThoughts&&(o+=1),o-=r.missingCapabilities.length*10,{provider:t,score:o,missingCapabilities:r.missingCapabilities,supportedCapabilities:s}}).sort((t,n)=>n.score-t.score)}function oo(i,e,t={}){if(e.length===0)throw new Error("No providers available");let n=qt(i),r=ro(e,n);if(t.requireExactMatch){let o=r.filter(s=>s.missingCapabilities.length===0);if(o.length===0)throw new Error(`No providers fully support the request requirements: ${r[0]?.missingCapabilities.join(", ")||"unknown requirements"}`);return o[0].provider}if(!t.allowDegradation){let o=r[0];if(o.missingCapabilities.length>0)throw new Error(`Best available provider (${o.provider.getName()}) is missing: ${o.missingCapabilities.join(", ")}`)}return r[0].provider}function Wa(i,e){let t=qt(i),n=ro(e,t),r=n[0]?.provider||null,o=[t.hasImages&&"images",t.hasAudio&&"audio",t.hasFiles&&"files",t.hasUrls&&"URLs",t.requiresFunctions&&"functions",t.requiresStreaming&&"streaming",t.requiresCaching&&"caching"].filter(Boolean).length,s=r?n[0].supportedCapabilities.length:0,a=r?`${r.getName()} supports ${s}/${o} requirements (${Math.round(s/Math.max(o,1)*100)}% compatibility)`:"No suitable providers found";return{requirements:t,providerScores:n,recommendedProvider:r,summary:a}}function Va(i,e){return i.filter(t=>t.getFeatures().media[e].supported)}function Ja(i,e){let t={};for(let n of i){let o=n.getFeatures().media[e];if(o.supported)for(let s of o.formats)t[s]||(t[s]=[]),t[s].push(n)}return t}var ar=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(ar||{}),lr=(r=>(r.EmbedEnglishV30="embed-english-v3.0",r.EmbedEnglishLightV30="embed-english-light-v3.0",r.EmbedMultiLingualV30="embed-multilingual-v3.0",r.EmbedMultiLingualLightV30="embed-multilingual-light-v3.0",r))(lr||{});var so=[{name:"command-r-plus",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"command-r",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"command",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"command-light",currency:"usd",promptTokenCostPer1M:.3,completionTokenCostPer1M:.6},{name:"embed-english-light-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-english-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-multilingual-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-multilingual-light-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1}];function Nc(i){if(Array.isArray(i)){let t=["string","number","integer","boolean","object","array"].find(n=>i.includes(n));return t||(i.find(n=>n!=="null")??i[0]??"string")}return typeof i=="string"?i:"string"}var Hs=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...Te()}),Ya=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...je()}),zs=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,frequencyPenalty:e.frequencyPenalty,presencePenalty:e.presencePenalty,endSequences:e.endSequences,stopSequences:e.stopSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.at(-1),r=e.chatPrompt.slice(0,-1),o;n&&n.role==="user"&&typeof n.content=="string"&&(o=n?.content);let s=$c(r),a=e.functions?.map(p=>{let d={};if(p.parameters?.properties)for(let[m,g]of Object.entries(p.parameters.properties))d[m]={description:g.description,type:Nc(g.type),required:p.parameters.required?.includes(m)??!1};return{name:p.name,description:p.description,parameter_definitions:d}}),l=e.chatPrompt.filter(p=>p.role==="function").map(p=>{let d=a?.find(m=>m.name===p.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:p.result??""}]}}),u={name:"/chat"},c={message:o,model:t,tools:a,...l&&!o?{tool_results:l}:{},chat_history:s,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},k:e.modelConfig?.topK??this.config.topK,...e.modelConfig?.topP!==void 0?{p:e.modelConfig.topP}:{},frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,end_sequences:this.config.endSequences,stop_sequences:e.modelConfig?.stopSequences??this.config.stopSequences};return[u,c]}createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embed"},r={model:t,texts:e.texts??[],input_type:"classification",truncate:""};return[n,r]};createChatResp=e=>{this.tokensUsed=e.meta.billed_units?{promptTokens:e.meta.billed_units.input_tokens,completionTokens:e.meta.billed_units.output_tokens,totalTokens:e.meta.billed_units.input_tokens+e.meta.billed_units.output_tokens}:void 0;let t;if("finish_reason"in e)switch(e.finish_reason){case"COMPLETE":t="stop";break;case"MAX_TOKENS":t="length";break;case"ERROR":throw new Error("Finish reason: ERROR");case"ERROR_TOXIC":throw new Error("Finish reason: CONTENT_FILTER");default:t="stop";break}let n;return"tool_calls"in e&&(n=e.tool_calls?.map(o=>({id:o.name,type:"function",function:{name:o.name,params:o.parameters}}))),{results:[{index:0,id:e.generation_id,content:e.text,functionCalls:n,finishReason:t}],remoteId:e.response_id}};createChatStreamResp=(e,t)=>{let n=t;e.event_type==="stream-start"&&(n.generation_id=e.generation_id),this.tokensUsed={promptTokens:0,completionTokens:e.meta.billed_units?.output_tokens??0,totalTokens:e.meta.billed_units?.output_tokens??0};let{results:r}=this.createChatResp(e),o=r[0];if(!o)throw new Error("No result");return o.id=n.generation_id??"",{results:r}};createEmbedResp(e){return{remoteId:e.id,embeddings:e.embeddings}}},cn=class extends Be{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...Hs(),...t},s=new zs(o),a=r?.map(l=>{let u=l,c=u?.config;if(!c)return l;let p={};c.maxTokens!==void 0&&(p.maxTokens=c.maxTokens),c.temperature!==void 0&&(p.temperature=c.temperature),c.topP!==void 0&&(p.topP=c.topP),c.topK!==void 0&&(p.topK=c.topK),c.presencePenalty!==void 0&&(p.presencePenalty=c.presencePenalty),c.frequencyPenalty!==void 0&&(p.frequencyPenalty=c.frequencyPenalty),c.stopSequences!==void 0&&(p.stopSequences=c.stopSequences),c.endSequences!==void 0&&(p.endSequences=c.endSequences),c.stream!==void 0&&(p.stream=c.stream),c.n!==void 0&&(p.n=c.n);let d={...u};return Object.keys(p).length>0&&(d.modelConfig={...u.modelConfig??{},...p}),d});super(s,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:so,defaults:{model:o.model},supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[],maxSize:0,detailLevels:[]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},options:n,models:a??r})}};function $c(i){return i.map(e=>{let t="";if(e.role==="system"||e.role==="assistant"||e.role==="user")if(typeof e.content=="string")t=e.content;else throw new Error("Multi-modal content not supported");switch(e.role){case"user":return{role:"USER",message:t};case"system":return{role:"SYSTEM",message:t};case"assistant":{let n=Qa(e.functionCalls);return{role:"CHATBOT",message:t,tool_calls:n}}case"function":{let n=i.map(s=>{if(s.role==="assistant")return s.functionCalls?.find(a=>a.id===e.functionId)}).filter(s=>s!==void 0),r=Qa(n)?.at(0);if(!r)throw new Error("Function call not found");let o=[{result:e.result}];return{role:"TOOL",tool_results:[{call:r,outputs:o}]}}default:throw new Error("Unknown role")}})}function Qa(i){return i?.map(e=>{let t;if(typeof e.function.params=="string"){let n=e.function.params;if(n.trim().length===0)t={};else try{t=JSON.parse(n)}catch{throw new Error(`Failed to parse function params JSON: ${n}`)}}else t=e.function.params;return{name:e.function.name,parameters:t}})}var ur=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(ur||{});var io=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var Ks=()=>structuredClone({model:"deepseek-chat",...Te()}),Xa=()=>structuredClone({model:"deepseek-coder",...je()}),pn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let s={...Ks(),...t};o=[...io,...o??[]],super({apiKey:e,config:s,options:n,apiURL:"https://api.deepseek.com",modelInfo:o,supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r}),super.setName("DeepSeek")}};var cr=(w=>(w.Gemini31Pro="gemini-3.1-pro-preview",w.Gemini3FlashLite="gemini-3.1-flash-lite-preview",w.Gemini3Flash="gemini-3-flash-preview",w.Gemini3Pro="gemini-3.1-pro-preview",w.Gemini3ProImage="gemini-3-pro-image-preview",w.Gemini25Pro="gemini-2.5-pro",w.Gemini25Flash="gemini-2.5-flash",w.Gemini25FlashLite="gemini-2.5-flash-lite",w.Gemini20Flash="gemini-2.0-flash",w.Gemini20FlashLite="gemini-2.0-flash-lite",w.Gemini20ProExp="gemini-2.0-pro-exp-02-05",w.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",w.Gemini1Pro="gemini-1.0-pro",w.Gemini15Flash="gemini-1.5-flash",w.Gemini15Flash002="gemini-1.5-flash-002",w.Gemini15Flash8B="gemini-1.5-flash-8b",w.Gemini15Pro="gemini-1.5-pro",w.GeminiFlashLatest="gemini-flash-latest",w.GeminiFlashLiteLatest="gemini-flash-lite-latest",w.GeminiProLatest="gemini-pro-latest",w))(cr||{}),ao=(o=>(o.GeminiEmbedding001="gemini-embedding-001",o.GeminiEmbedding="gemini-embedding-exp",o.TextEmbeddingLarge="text-embedding-large-exp-03-07",o.TextEmbedding004="text-embedding-004",o.TextEmbedding005="text-embedding-005",o))(ao||{}),lo=(r=>(r.HarmCategoryHarassment="HARM_CATEGORY_HARASSMENT",r.HarmCategoryHateSpeech="HARM_CATEGORY_HATE_SPEECH",r.HarmCategorySexuallyExplicit="HARM_CATEGORY_SEXUALLY_EXPLICIT",r.HarmCategoryDangerousContent="HARM_CATEGORY_DANGEROUS_CONTENT",r))(lo||{}),uo=(o=>(o.BlockNone="BLOCK_NONE",o.BlockOnlyHigh="BLOCK_ONLY_HIGH",o.BlockMediumAndAbove="BLOCK_MEDIUM_AND_ABOVE",o.BlockLowAndAbove="BLOCK_LOW_AND_ABOVE",o.BlockDefault="HARM_BLOCK_THRESHOLD_UNSPECIFIED",o))(uo||{}),Ws=(l=>(l.SemanticSimilarity="SEMANTIC_SIMILARITY",l.Classification="CLASSIFICATION",l.Clustering="CLUSTERING",l.RetrievalDocument="RETRIEVAL_DOCUMENT",l.RetrievalQuery="RETRIEVAL_QUERY",l.QuestionAnswering="QUESTION_ANSWERING",l.FactVerification="FACT_VERIFICATION",l.CodeRetrievalQuery="CODE_RETRIEVAL_QUERY",l))(Ws||{}),Za=["gemini-3.1-pro-preview","gemini-3.1-pro-preview-customtools","gemini-3.1-flash-lite-preview","gemini-3-flash-preview","gemini-2.5-pro","gemini-2.5-flash","gemini-2.5-flash-lite","gemini-2.0-flash","gemini-2.0-flash-lite","gemini-flash-latest","gemini-flash-lite-latest"];var co=[{name:"gemini-3.1-pro-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:12,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3-flash-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3.1-flash-lite-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.25,completionTokenCostPer1M:1.5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3-pro-image-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:.134,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2.5,completionTokenCostPer1M:15,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-pro-exp-02-05",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-flash-thinking-exp-01-21",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:15,completionTokenCostPer1M:3.5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.01,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gemini-2.0-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{structuredOutputs:!0}},{name:"gemini-1.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{structuredOutputs:!0}},{name:"gemini-1.5-flash-8b",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.0375,completionTokenCostPer1M:.15,supported:{structuredOutputs:!0}},{name:"gemini-1.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{structuredOutputs:!0}},{name:"gemini-1.0-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,supported:{structuredOutputs:!0}},{name:"gemini-flash-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-flash-lite-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-pro-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}}];var zt=i=>i.includes("gemini-3"),el=i=>i.includes("gemini-3")&&i.includes("pro"),po=i=>{if(!i||typeof i!="object")return i;let e={...i};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.maximum,delete e.oneOf,delete e.anyOf,Array.isArray(e.type)&&(e.type=e.type.includes("object")?"object":e.type[0]??"string"),e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,po(n)]))),e.items&&(e.items=po(e.items)),e},tl=[{category:"HARM_CATEGORY_HARASSMENT",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_HATE_SPEECH",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_SEXUALLY_EXPLICIT",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_DANGEROUS_CONTENT",threshold:"BLOCK_NONE"}],Js=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:tl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...Te()}),nl=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:tl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...je()}),Vs=class{constructor(e,t,n,r,o){this.config=e;this.isVertex=t;this.endpointId=n;this.apiKey=r;this.options=o;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");let s=this.config.model;if(zt(s)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${s}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if(el(s)&&this.config.thinking?.thinkingLevel){let a=this.config.thinking.thinkingLevel;if(a!=="low"&&a!=="high")throw new Error(`Gemini 3 Pro (${s}) only supports thinkingLevel 'low' or 'high', got '${a}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let x=r?"&":"?",I=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${x}key=${I}`}let s=e.chatPrompt.filter(x=>x.role==="system").map(x=>x.content),a=s.length>0?{role:"user",parts:[{text:s.join(" ")}]}:void 0,l=[],u=e.chatPrompt.filter(x=>x.role!=="system");for(let x=0;x<u.length;x++){let I=u[x];switch(I.role){case"user":{let w=Array.isArray(I.content)?I.content.map((A,T)=>{switch(A.type){case"text":return{text:A.text};case"image":return{inlineData:{mimeType:A.mimeType,data:A.image}};case"audio":return{inlineData:{mimeType:`audio/${A.format??"mp3"}`,data:A.data}};case"file":return"fileUri"in A?{fileData:{mimeType:A.mimeType,fileUri:A.fileUri}}:{inlineData:{mimeType:A.mimeType,data:A.data}};default:throw new Error(`Chat prompt content type not supported (index: ${T})`)}}):[{text:I.content}];l.push({role:"user",parts:w});break}case"assistant":{let w=[],A=I.thoughtBlocks,T=I.functionCalls&&I.functionCalls.length>0,k=A?.[0],E=A?.map($=>$.data).join("")??"",M=k?.signature;if(E&&w.push({...T?{}:{thought:!0},text:E,...M&&!T?{thought_signature:M}:{}}),I.functionCalls){let $=I.functionCalls.map((B,V)=>{let O;if(typeof B.function.params=="string"){let F=B.function.params;if(F.trim().length===0)O={};else try{O=JSON.parse(F)}catch{throw new Error(`Failed to parse function params JSON: ${F}`)}}else O=B.function.params;let j={functionCall:{name:B.function.name,args:O}};return M&&V===0&&(j.thought_signature=M),j});w.push(...$)}if(I.content&&w.push({text:I.content}),w.length===0)throw new Error("Assistant content is empty");l.push({role:"model",parts:w});break}case"function":{let w=[],A=I,T=x;for(;;){if(!("functionId"in A))throw new Error(`Chat prompt functionId is empty (index: ${T})`);if(w.push({functionResponse:{name:A.functionId,response:{result:A.result}}}),T+1<u.length&&u[T+1].role==="function")T++,A=u[T];else break}x=T,l.push({role:"user",parts:w});break}default:throw new Error(`Invalid role: ${JSON.stringify(I)} (index: ${x})`)}}let c=[];if(e.functions&&e.functions.length>0){let x=e.functions.map(I=>{let w={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},A=I.parameters?po(I.parameters):void 0;return A===void 0||A&&typeof A=="object"&&Object.keys(A).length===0?A={...w}:A&&typeof A=="object"&&A.type==="object"&&(!("properties"in A)||!A.properties||Object.keys(A.properties).length===0)&&(A={...A,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:I.name,description:I.description,parameters:A}});c.push({function_declarations:x})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let x=this.options.googleMaps,I=x?.enableWidget!==void 0?{enableWidget:x.enableWidget}:{};c.push({google_maps:I})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,d=Array.isArray(c)?c.some(x=>x&&Array.isArray(x.function_declarations)&&x.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let x=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...x}}else d&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let m={};if(this.config.thinking?.includeThoughts&&(m.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(m.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&zt(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let x=this.getEffectiveMappings(n),I=x.thinkingTokenBudgetLevels;if(zt(n)){let A=el(n),T=x.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=T?.minimal??"minimal";else{let k=t.thinkingTokenBudget,E=T?.[k];E||(E=k==="highest"?"high":k),m.thinkingLevel=E}if(A&&m.thinkingLevel){let k=m.thinkingLevel;k!=="low"&&k!=="high"&&(m.thinkingLevel=k==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=I?.minimal??200;break;case"low":m.thinkingBudget=I?.low??800;break;case"medium":m.thinkingBudget=I?.medium??5e3;break;case"high":m.thinkingBudget=I?.high??1e4;break;case"highest":m.thinkingBudget=I?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,zt(n)||delete m.thinkingLevel,zt(n)&&delete m.thinkingBudget;let g=e.modelConfig?.maxTokens??this.config.maxTokens;if(m.thinkingLevel&&g!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(m.includeThoughts=t.showThoughts);let h={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(m).length>0?{thinkingConfig:m}:{}};if(zt(n)&&(h.temperature===void 0||h.temperature<1)&&(h.temperature=1),e.responseFormat){if(h.responseMimeType="application/json",e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let x=e.responseFormat.schema.schema||e.responseFormat.schema;h.responseSchema=po(x)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(h.responseMimeType="application/json");let f=this.config.safetySettings;return[o,{contents:l,tools:c,toolConfig:p,systemInstruction:a,generationConfig:h,safetySettings:f}]};createEmbedReq=async e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n,r;if(this.isVertex)this.endpointId?n={name:`/${this.endpointId}:predict`}:n={name:`/models/${t}:predict`},r={instances:e.texts.map(o=>({content:o,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}};else{let o=typeof this.apiKey=="function"?this.apiKey():this.apiKey;n={name:`/models/${t}:batchEmbedContents?key=${o}`},r={requests:e.texts.map(s=>({model:`models/${t}`,content:{parts:[{text:s}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[n,r]};createChatResp=e=>{let t,n=e.candidates?.map(o=>{let s={index:0};switch(o.finishReason){case"MAX_TOKENS":s.finishReason="length";break;case"STOP":s.finishReason="stop";break;case"SAFETY":throw new Me("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Me("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Me("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Me("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Me("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Me("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Me("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Me("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Me("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return s;for(let u of o.content.parts){if("text"in u){if("thought"in u&&u.thought||u.thought===!0){s.thought=u.text;let c=u.thoughtSignature||u.thought_signature;s.thoughtBlocks||(s.thoughtBlocks=[]),s.thoughtBlocks.push({data:u.text,encrypted:!1,...c?{signature:c}:{}})}else s.content=u.text;continue}if("functionCall"in u){let c=u.thoughtSignature||u.thought_signature;if(c)if(!s.thoughtBlocks||s.thoughtBlocks.length===0)s.thoughtBlocks=[{data:"",encrypted:!1,signature:c}];else{let p=s.thoughtBlocks[s.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=c)}s.functionCalls=[...s.functionCalls??[],{id:Ve(),type:"function",function:{name:u.functionCall.name,params:u.functionCall.args}}]}}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let u=c=>c?`${c.year}-${String(c.month).padStart(2,"0")}-${String(c.day).padStart(2,"0")}`:void 0;s.citations=a.filter(c=>typeof c?.uri=="string").map(c=>({url:c.uri,title:c.title,license:c.license,publicationDate:u(c.publicationDate)}))}let l=o.groundingMetadata;if(l){if(Array.isArray(l.groundingChunks)){let u=l.groundingChunks.map(c=>c?.maps).filter(c=>c&&typeof c.uri=="string").map(c=>({url:c.uri,title:c.title}));u.length&&(s.citations=[...s.citations??[],...u])}typeof l.googleMapsWidgetContextToken=="string"&&(t=l.googleMapsWidgetContextToken)}return s});e.usageMetadata&&(this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...e.usageMetadata.cachedContentTokenCount!==void 0?{cacheReadTokens:e.usageMetadata.cachedContentTokenCount}:{}});let r={results:n};return t&&(r.providerMetadata={...r.providerMetadata,google:{...r.providerMetadata?.google??{},mapsWidgetContextToken:t}}),r};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return Za.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,r=t.contextCache?.ttlSeconds??3600,{systemInstruction:o,contents:s}=this.extractCacheableContent(e.chatPrompt);if(!o&&(!s||s.length===0))return;let a={model:this.isVertex?n:`models/${n}`,ttl:`${r}s`,displayName:`ax-cache-${Date.now()}`};o&&(a.systemInstruction=o),s&&s.length>0&&(a.contents=s);let l;if(this.isVertex)l="/cachedContents";else{l="/cachedContents";let u=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;l+=`?key=${u}`}return{type:"create",apiConfig:{name:l},request:a,parseResponse:u=>{let c=u;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},r=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;r+=`?key=${o}`}return{type:"update",apiConfig:{name:r,headers:{"Content-Type":"application/json"}},request:n,parseResponse:o=>{let s=o;if(s?.name)return{name:s.name,expiresAt:s.expireTime,tokenCount:s.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let n=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${n}`}return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,{dynamicContents:s,dynamicSystemInstruction:a}=this.extractDynamicContent(e.chatPrompt),l;if(this.endpointId?l={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:l={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`},!this.isVertex){let d=o?"&":"?",m=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;l.name+=`${d}key=${m}`}let u={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain"};zt(r)&&(u.temperature===void 0||u.temperature<1)&&(u.temperature=1);let c=this.config.safetySettings,p={contents:s,cachedContent:n,generationConfig:u,safetySettings:c};return a&&(p.systemInstruction=a),{apiConfig:l,request:p}};extractCacheableContent(e){let t,n=[],r=-1;for(let o=e.length-1;o>=0;o--){let s=e[o];if("cache"in s&&s.cache){r=o;break}}for(let o=0;o<e.length;o++){let s=e[o];if(s.role==="system"){t={role:"user",parts:[{text:s.content}]};continue}if(r>=0&&o<=r)if(s.role==="user"){let a=[];if(typeof s.content=="string")a.push({text:s.content});else if(Array.isArray(s.content))for(let l of s.content)switch(l.type){case"text":a.push({text:l.text});break;case"image":a.push({inlineData:{mimeType:l.mimeType,data:l.image}});break;case"audio":a.push({inlineData:{mimeType:`audio/${l.format??"mp3"}`,data:l.data}});break;case"file":"fileUri"in l?a.push({fileData:{mimeType:l.mimeType,fileUri:l.fileUri}}):a.push({inlineData:{mimeType:l.mimeType,data:l.data}});break}a.length>0&&n.push({role:"user",parts:a})}else s.role==="assistant"&&s.content&&n.push({role:"model",parts:[{text:s.content}]})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[];for(let r of e)if(r.role!=="system"&&!("cache"in r&&r.cache))if(r.role==="user"){let o=[];if(typeof r.content=="string")o.push({text:r.content});else if(Array.isArray(r.content)){for(let s of r.content)if(!("cache"in s&&s.cache))switch(s.type){case"text":o.push({text:s.text});break;case"image":o.push({inlineData:{mimeType:s.mimeType,data:s.image}});break;case"audio":o.push({inlineData:{mimeType:`audio/${s.format??"mp3"}`,data:s.data}});break;case"file":"fileUri"in s?o.push({fileData:{mimeType:s.mimeType,fileUri:s.fileUri}}):o.push({inlineData:{mimeType:s.mimeType,data:s.data}});break}}o.length>0&&n.push({role:"user",parts:o})}else if(r.role==="assistant"){let o=[];if(r.content&&o.push({text:r.content}),r.functionCalls)for(let s of r.functionCalls){let a;if(typeof s.function.params=="string")try{a=JSON.parse(s.function.params)}catch{a={}}else a=s.function.params??{};o.push({functionCall:{name:s.function.name,args:a}})}o.length>0&&n.push({role:"model",parts:o})}else r.role==="function"&&n.push({role:"user",parts:[{functionResponse:{name:r.functionId,response:{result:r.result}}}]});return{dynamicContents:n,dynamicSystemInstruction:void 0}}},dn=class i extends Be{static create(e){return new i(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:s,models:a,modelInfo:l}){let u=t!==void 0&&n!==void 0,c,p;if(u){if(!e)throw new Error("GoogleGemini Vertex API key not set");if(typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let f;r?f="endpoints":f="publishers/google",c=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${f}`,p=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");c="https://generativelanguage.googleapis.com/v1beta",p=async()=>({})}let d={...Js(),...o},m=new Vs(d,u,r,e,s);l=[...co,...l??[]];let g=f=>{let C=at({model:f,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:C?.supported?.thinkingBudget??!1,hasShowThoughts:C?.supported?.showThoughts??!1,structuredOutputs:C?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","aac","ogg"],maxDuration:9.5*60},files:{supported:!0,formats:["application/pdf","text/plain","text/csv","text/html","text/xml"],maxSize:2*1024*1024*1024,uploadMethod:"cloud"},urls:{supported:!0,webSearch:!0,contextFetching:!0}},caching:{supported:m.supportsContextCache(f),types:["persistent"]},thinking:C?.supported?.thinkingBudget??!1,multiTurn:!0}},h=a?.map(f=>{let C=f,x=C?.config;if(!x)return f;let I={};x.maxTokens!==void 0&&(I.maxTokens=x.maxTokens),x.temperature!==void 0&&(I.temperature=x.temperature),x.topP!==void 0&&(I.topP=x.topP),x.topK!==void 0&&(I.topK=x.topK),x.presencePenalty!==void 0&&(I.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(I.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(I.stopSequences=x.stopSequences),x.endSequences!==void 0&&(I.endSequences=x.endSequences),x.stream!==void 0&&(I.stream=x.stream),x.n!==void 0&&(I.n=x.n);let w={...C};Object.keys(I).length>0&&(w.modelConfig={...C.modelConfig??{},...I});let A=x.thinking?.thinkingTokenBudget;if(typeof A=="number"){let T=d.thinkingTokenBudgetLevels,k=[["minimal",T?.minimal??200],["low",T?.low??800],["medium",T?.medium??5e3],["high",T?.high??1e4],["highest",T?.highest??24500]],E="minimal",M=Number.POSITIVE_INFINITY;for(let[$,B]of k){let V=Math.abs(A-B);V<M&&(M=V,E=$)}w.thinkingTokenBudget=E}return x.thinking?.includeThoughts!==void 0&&(w.showThoughts=!!x.thinking.includeThoughts),x.thinkingLevelMapping&&(w.thinkingLevelMapping=x.thinkingLevelMapping),x.thinkingTokenBudgetLevels&&(w.thinkingTokenBudgetLevels=x.thinkingTokenBudgetLevels),w});h?m.setModels(h):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:s,supportFor:g,models:h??a})}};var Lc=new nt,mn=class{options;maxTokens;refillRate;currentTokens;lastRefillTime;constructor(e,t,n){this.maxTokens=e,this.refillRate=t,this.currentTokens=e,this.lastRefillTime=Date.now(),this.options=n}refillTokens(){let e=Date.now(),n=(e-this.lastRefillTime)/1e3*this.refillRate;this.currentTokens=Math.min(this.maxTokens,this.currentTokens+n),this.lastRefillTime=e}async waitUntilTokensAvailable(e){if(this.refillTokens(),this.currentTokens>=e){this.currentTokens-=e;return}return this.options?.debug&&console.log(Lc.red(`Rate limiter: Waiting for ${e-this.currentTokens} tokens`)),await new Promise(t=>setTimeout(t,100)),this.waitUntilTokensAvailable(e)}async acquire(e){await this.waitUntilTokensAvailable(e)}};var pr=(r=>(r.Llama3_8B="llama3-8b-8192",r.Llama33_70B="llama-3.3-70b-versatile",r.Mixtral_8x7B="mixtral-8x7b-32768",r.Gemma2_9B="gemma2-9b-it",r))(pr||{});var mo=[{name:"gemma2-9b-it",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.2,completionTokenCostPer1M:.2},{name:"llama-3.3-70b-versatile",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.59,completionTokenCostPer1M:.79},{name:"llama3-8b-8192",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.05,completionTokenCostPer1M:.08},{name:"mixtral-8x7b-32768",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.24,completionTokenCostPer1M:.24}];var Dc=()=>structuredClone({model:"llama-3.3-70b-versatile",...Te()}),gn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let s={...Dc(),...t},a={...n,streamingUsage:!1};o=[...mo,...o??[]];let l={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,options:a,modelInfo:o,apiURL:"https://api.groq.com/openai/v1",models:r,supportFor:l}),super.setName("Groq"),this.setOptions(a)}setOptions=e=>{let t=this.newRateLimiter(e);super.setOptions({...e,rateLimiter:t})};newRateLimiter=e=>{if(e?.rateLimiter)return e.rateLimiter;let t=e?.tokensPerMinute??4800,n=new mn(t,t/60,{debug:e?.debug});return async(o,s)=>{let a=s.modelUsage?.tokens?.totalTokens||0;return await n.acquire(a),await o()}}};var go=[];var ho=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(ho||{});var Ys=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...Te()}),rl=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...je()}),Qs=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,n:e.n,presencePenalty:e.presencePenalty}}createChatReq=e=>{let t=e.model,n=e.functions?`Functions:
167
+ `;break}default:r=JSON.stringify(n,null,2)}s(r)}};var ae={LLM_SYSTEM:"gen_ai.system",LLM_OPERATION_NAME:"gen_ai.operation.name",LLM_REQUEST_MODEL:"gen_ai.request.model",LLM_REQUEST_MAX_TOKENS:"gen_ai.request.max_tokens",LLM_REQUEST_TEMPERATURE:"gen_ai.request.temperature",LLM_REQUEST_TOP_K:"gen_ai.request.top_k",LLM_REQUEST_FREQUENCY_PENALTY:"gen_ai.request.frequency_penalty",LLM_REQUEST_PRESENCE_PENALTY:"gen_ai.request.presence_penalty",LLM_REQUEST_STOP_SEQUENCES:"gen_ai.request.stop_sequences",LLM_REQUEST_LLM_IS_STREAMING:"gen_ai.request.llm_is_streaming",LLM_REQUEST_TOP_P:"gen_ai.request.top_p",LLM_USAGE_INPUT_TOKENS:"gen_ai.usage.input_tokens",LLM_USAGE_OUTPUT_TOKENS:"gen_ai.usage.output_tokens",LLM_USAGE_TOTAL_TOKENS:"gen_ai.usage.total_tokens",LLM_USAGE_THOUGHTS_TOKENS:"gen_ai.usage.thoughts_tokens",DB_SYSTEM:"db.system",DB_TABLE:"db.table",DB_NAMESPACE:"db.namespace",DB_ID:"db.id",DB_QUERY_TEXT:"db.query.text",DB_VECTOR:"db.vector",DB_OPERATION_NAME:"db.operation.name",DB_VECTOR_QUERY_TOP_K:"db.vector.query.top_k",DB_QUERY_EMBEDDINGS:"db.query.embeddings",DB_QUERY_RESULT:"db.query.result",DB_QUERY_EMBEDDINGS_VECTOR:"db.query.embeddings.vector",DB_QUERY_RESULT_ID:"db.query.result.id",DB_QUERY_RESULT_SCORE:"db.query.result.score",DB_QUERY_RESULT_DISTANCE:"db.query.result.distance",DB_QUERY_RESULT_METADATA:"db.query.result.metadata",DB_QUERY_RESULT_VECTOR:"db.query.result.vector",DB_QUERY_RESULT_DOCUMENT:"db.query.result.document"},st={GEN_AI_USER_MESSAGE:"gen_ai.user.message",GEN_AI_SYSTEM_MESSAGE:"gen_ai.system.message",GEN_AI_ASSISTANT_MESSAGE:"gen_ai.assistant.message",GEN_AI_TOOL_MESSAGE:"gen_ai.tool.message",GEN_AI_CHOICE:"gen_ai.choice",GEN_AI_USAGE:"gen_ai.usage"},Es=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(Es||{}),Ms=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Ms||{});var Ps=class{buffer;doneCallback;transformFn;constructor(e,t){this.transformFn=e,this.doneCallback=t,this.buffer=t?[]:void 0}async transform(e,t){let n=this.transformFn(e);n&&(t.enqueue(n),this.buffer?.push(n))}async flush(e){await this.doneCallback?.(this.buffer??[]),e.terminate()}},qr=class extends TransformStream{constructor(e,t){super(new Ps(e,t))}};function Hr(s,e){for(let t of e){let n=s.find(r=>r.id===t.id);n?(typeof t.function.name=="string"&&t.function.name.length>0&&(n.function.name+=t.function.name),typeof t.function.params=="string"&&t.function.params.length>0&&(n.function.params+=t.function.params),typeof t.function.params=="object"&&(n.function.params=t.function.params)):s.push(t)}}var Ta=(s,e,t,n)=>{let r=n?s.filter(i=>i.role!=="system"):[...s];t({name:"ChatRequestChatPrompt",step:e,value:r})};var wa=(s,e)=>{if(!s.results)return;let t={name:"ChatResponseResults",value:s.results};e(t)};function Ra(s,e){let t=new Map;for(let n of s)for(let r of n.results){if(!r)continue;let o=t.get(r.index);o?(r.content&&(o.content=(o.content??"")+r.content),r.thought&&(o.thought=(o.thought??"")+r.thought),r.finishReason&&(o.finishReason=r.finishReason),r.functionCalls&&(o.functionCalls?Hr(o.functionCalls,structuredClone(r.functionCalls)):o.functionCalls=structuredClone(r.functionCalls))):(o=structuredClone(r),t.set(r.index,o))}for(let n of t.values()){let r={name:"ChatResponseStreamingDoneResult",index:n.index,value:n};e(r)}}var ka=(s,e)=>{e({name:"FunctionResults",value:s})},Fs=(s,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:s})},Sa=(s,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:s})},va=(s,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:s})},Oa=(s,e,t)=>{t({name:"RefusalError",index:e,error:s})};var Ea=(s,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:s})},Ma=(s,e)=>{let t=s.slice(0,3).map(r=>({length:r.length,sample:r.slice(0,5),truncated:r.length>5})),n={name:"EmbedResponse",totalEmbeddings:s.length,value:t};e(n)},Pa=(s,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:s,selectedIndex:e,latency:t})};var Wr=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n);e[t]=r.length>100?r.substring(0,100):r}return e},Et=(...s)=>{let e={};for(let t of s)t&&Object.assign(e,t);return e},Kr,Fa=s=>{if(Kr)return Kr;if(s)return Kr=rp(s),Kr};var rp=s=>({latencyHistogram:s.createHistogram("ax_llm_request_duration_ms",{description:"Duration of LLM requests in milliseconds",unit:"ms"}),errorCounter:s.createCounter("ax_llm_errors_total",{description:"Total number of LLM request errors"}),requestCounter:s.createCounter("ax_llm_requests_total",{description:"Total number of LLM requests"}),tokenCounter:s.createCounter("ax_llm_tokens_total",{description:"Total number of LLM tokens consumed"}),inputTokenCounter:s.createCounter("ax_llm_input_tokens_total",{description:"Total number of input/prompt tokens consumed"}),outputTokenCounter:s.createCounter("ax_llm_output_tokens_total",{description:"Total number of output/completion tokens generated"}),errorRateGauge:s.createGauge("ax_llm_error_rate",{description:"Current error rate as a percentage (0-100)"}),meanLatencyGauge:s.createGauge("ax_llm_mean_latency_ms",{description:"Mean latency of LLM requests in milliseconds",unit:"ms"}),p95LatencyGauge:s.createGauge("ax_llm_p95_latency_ms",{description:"95th percentile latency of LLM requests in milliseconds",unit:"ms"}),p99LatencyGauge:s.createGauge("ax_llm_p99_latency_ms",{description:"99th percentile latency of LLM requests in milliseconds",unit:"ms"}),streamingRequestsCounter:s.createCounter("ax_llm_streaming_requests_total",{description:"Total number of streaming LLM requests"}),functionCallsCounter:s.createCounter("ax_llm_function_calls_total",{description:"Total number of function/tool calls made"}),functionCallLatencyHistogram:s.createHistogram("ax_llm_function_call_latency_ms",{description:"Latency of function calls in milliseconds",unit:"ms"}),requestSizeHistogram:s.createHistogram("ax_llm_request_size_bytes",{description:"Size of LLM request payloads in bytes",unit:"By"}),responseSizeHistogram:s.createHistogram("ax_llm_response_size_bytes",{description:"Size of LLM response payloads in bytes",unit:"By"}),temperatureGauge:s.createGauge("ax_llm_temperature_gauge",{description:"Temperature setting used for LLM requests"}),maxTokensGauge:s.createGauge("ax_llm_max_tokens_gauge",{description:"Maximum tokens setting used for LLM requests"}),estimatedCostCounter:s.createCounter("ax_llm_estimated_cost_total",{description:"Estimated cost of LLM requests in USD",unit:"$"}),promptLengthHistogram:s.createHistogram("ax_llm_prompt_length_chars",{description:"Length of prompts in characters"}),contextWindowUsageGauge:s.createGauge("ax_llm_context_window_usage_ratio",{description:"Context window utilization ratio (0-1)"}),timeoutsCounter:s.createCounter("ax_llm_timeouts_total",{description:"Total number of timed out LLM requests"}),abortsCounter:s.createCounter("ax_llm_aborts_total",{description:"Total number of aborted LLM requests"}),thinkingBudgetUsageCounter:s.createCounter("ax_llm_thinking_budget_usage_total",{description:"Total thinking budget tokens used"}),multimodalRequestsCounter:s.createCounter("ax_llm_multimodal_requests_total",{description:"Total number of multimodal requests (with images/audio)"}),cacheReadTokensCounter:s.createCounter("ax_llm_cache_read_tokens_total",{description:"Total number of tokens read from cache (prompt caching)"}),cacheWriteTokensCounter:s.createCounter("ax_llm_cache_write_tokens_total",{description:"Total number of tokens written to cache (prompt caching)"})}),_a=(s,e,t,n,r,o)=>{try{if(s.latencyHistogram){let i=Wr({operation:e,ai_service:n,...r?{model:r}:{},...o});s.latencyHistogram.record(t,i)}}catch(i){console.warn("Failed to record latency metric:",i)}},Na=(s,e,t,n,r,o,i,a)=>{let u={operation:e,ai_service:o,...i?{model:i}:{},...a};s.meanLatencyGauge&&s.meanLatencyGauge.record(t,u),s.p95LatencyGauge&&s.p95LatencyGauge.record(n,u),s.p99LatencyGauge&&s.p99LatencyGauge.record(r,u)},La=(s,e,t,n,r)=>{try{if(s.errorCounter){let o=Wr({operation:e,ai_service:t,...n?{model:n}:{},...r});s.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},$a=(s,e,t,n,r,o)=>{s.errorRateGauge&&s.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Da=(s,e,t,n,r)=>{s.requestCounter&&s.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Zn=(s,e,t,n,r,o)=>{try{let i=Wr({ai_service:n,...r?{model:r}:{},...o});s.tokenCounter&&s.tokenCounter.add(t,{token_type:e,...i}),e==="input"&&s.inputTokenCounter&&s.inputTokenCounter.add(t,i),e==="output"&&s.outputTokenCounter&&s.outputTokenCounter.add(t,i)}catch(i){console.warn("Failed to record token metric:",i)}},Ga=(s,e,t,n,r,o)=>{t&&s.streamingRequestsCounter&&s.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ua=(s,e,t,n,r,o)=>{let i={function_name:e,...n?{ai_service:n}:{},...r?{model:r}:{},...o};s.functionCallsCounter&&s.functionCallsCounter.add(1,i),t&&s.functionCallLatencyHistogram&&s.functionCallLatencyHistogram.record(t,i)},_s=(s,e,t,n,r,o)=>{s.requestSizeHistogram&&s.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ns=(s,e,t,n,r,o)=>{s.responseSizeHistogram&&s.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ba=(s,e,t,n,r,o)=>{let i={...n?{ai_service:n}:{},...r?{model:r}:{},...o};e!==void 0&&s.temperatureGauge&&s.temperatureGauge.record(e,i),t!==void 0&&s.maxTokensGauge&&s.maxTokensGauge.record(t,i)},Ls=(s,e,t,n,r,o)=>{s.estimatedCostCounter&&s.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},ja=(s,e,t,n,r)=>{s.promptLengthHistogram&&s.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{},...r})},za=(s,e,t,n,r)=>{s.contextWindowUsageGauge&&s.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},qa=(s,e,t,n,r)=>{s.timeoutsCounter&&s.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Ha=(s,e,t,n,r)=>{s.abortsCounter&&s.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Ka=(s,e,t,n,r)=>{s.thinkingBudgetUsageCounter&&s.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},Wa=(s,e,t,n,r,o)=>{(e||t)&&s.multimodalRequestsCounter&&s.multimodalRequestsCounter.add(1,{ai_service:n,has_images:e.toString(),has_audio:t.toString(),...r?{model:r}:{},...o})},$s=(s,e,t,n,r,o)=>{try{if(t<=0)return;let i=Wr({ai_service:n,...r?{model:r}:{},...o});e==="read"&&s.cacheReadTokensCounter&&s.cacheReadTokensCounter.add(t,i),e==="write"&&s.cacheWriteTokensCounter&&s.cacheWriteTokensCounter.add(t,i)}catch(i){console.warn("Failed to record cache token metric:",i)}};function Va(s){try{return JSON.stringify(s,null,2)}catch{return String(s)}}function it(s,e={}){let t=[s];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${Va(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${Va(e.item)}`),new Error(t.join(`
168
+ `))}function qt(s){let e=n=>JSON.stringify(n,null,2);if(!s)throw new Error(`Chat request message item cannot be null or undefined, received: ${e(s)}`);let t=typeof s=="object"&&s!==null&&"role"in s&&typeof s.role=="string"?s.role:void 0;if(!t)throw new Error(`Chat request message must have a role, received: ${e(t)}`);switch(t){case"system":{let n=typeof s=="object"&&s!==null&&"content"in s&&typeof s.content=="string"?s.content:void 0;if(!n||n.trim()==="")throw new Error(`System message content cannot be empty or whitespace-only, received: ${e(n)}`);break}case"user":{let n=typeof s=="object"&&s!==null&&"content"in s?s.content:void 0;if(n===void 0)throw new Error(`User message content cannot be undefined, received: ${e(n)}`);if(typeof n=="string"){if(n.trim()==="")throw new Error(`User message content cannot be empty or whitespace-only, received: ${e(n)}`)}else if(Array.isArray(n)){if(n.length===0)throw new Error(`User message content array cannot be empty, received: ${e(n)}`);for(let r=0;r<n.length;r++){let o=n[r];if(!o||typeof o!="object")throw new Error(`User message content item at index ${r} must be an object, received: ${e(o)}`);let i=typeof o=="object"&&o!==null&&"type"in o&&typeof o.type=="string"?o.type:void 0;if(!i)throw new Error(`User message content item at index ${r} must have a type, received: ${e(i)}`);switch(i){case"text":{let a="text"in o&&typeof o.text=="string"?o.text:void 0;if(!a||a.trim()==="")throw new Error(`User message text content at index ${r} cannot be empty or whitespace-only, received: ${e(a)}`);break}case"image":{let a="image"in o&&typeof o.image=="string"?o.image:void 0,u="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:void 0;if(!a||a.trim()==="")throw new Error(`User message image content at index ${r} cannot be empty, received: ${e(a)}`);if(!u||u.trim()==="")throw new Error(`User message image content at index ${r} must have a mimeType, received: ${e(u)}`);break}case"audio":{let a="data"in o&&typeof o.data=="string"?o.data:void 0;if(!a||a.trim()==="")throw new Error(`User message audio content at index ${r} cannot be empty, received: ${e(a)}`);break}case"file":{let a="fileUri"in o&&typeof o.fileUri=="string",u="data"in o&&typeof o.data=="string";if(!a&&!u)throw new Error(`User message file content at index ${r} must have either 'data' or 'fileUri', received: ${e(o)}`);if(a&&u)throw new Error(`User message file content at index ${r} cannot have both 'data' and 'fileUri', received: ${e(o)}`);if(a){let c=o.fileUri;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} fileUri cannot be empty, received: ${e(c)}`)}if(u){let c=o.data;if(!c||c.trim()==="")throw new Error(`User message file content at index ${r} data cannot be empty, received: ${e(c)}`)}let l="mimeType"in o&&typeof o.mimeType=="string"?o.mimeType:null;if(!l||l.trim()==="")throw new Error(`User message file content at index ${r} must have a mimeType, received: ${e(l)}`);break}case"url":{let a="url"in o&&typeof o.url=="string"?o.url:void 0;if(!a||a.trim()==="")throw new Error(`User message url content at index ${r} cannot be empty, received: ${e(a)}`);break}default:throw new Error(`User message content item at index ${r} has unsupported type: ${e(i)}`)}}}else throw new Error(`User message content must be a string or array of content objects, received: ${e(n)}`);break}case"assistant":{let n=typeof s=="object"&&s!==null&&"content"in s?s.content:void 0,r=typeof s=="object"&&s!==null&&"functionCalls"in s?s.functionCalls:void 0,o=typeof s=="object"&&s!==null&&"thoughtBlocks"in s?s.thoughtBlocks:void 0,i=typeof n=="string"&&n.trim()!=="",a=Array.isArray(r)&&r.length>0,u=Array.isArray(o)&&o.length>0;if(!i&&!a&&!u&&it("Assistant message must include non-empty content, at least one function call, or thought blocks",{fieldPath:"content | functionCalls | thoughtBlocks",value:{content:n,functionCalls:r,thoughtBlocks:o},item:s}),n!==void 0&&typeof n!="string"&&it("Assistant message content must be a string",{fieldPath:"content",value:n,item:s}),r!==void 0&&!Array.isArray(r)&&it("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:s}),Array.isArray(r))for(let l=0;l<r.length;l++){let c=r[l];if((!c||typeof c!="object")&&it("functionCalls entry must be an object",{fieldPath:`functionCalls[${l}]`,value:c,item:s}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&it("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${l}].id`,value:c.id,item:s}),(!("type"in c)||c.type!=="function")&&it("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${l}].type`,value:c.type,item:s}),!("function"in c)||!c.function)it("functionCalls entry must include a function object",{fieldPath:`functionCalls[${l}].function`,value:c.function,item:s});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&it("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${l}].function.name`,value:p?.name,item:s}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&it("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${l}].function.params`,value:p.params,item:s})}}if(s.name!==void 0){let l=s.name;(typeof l!="string"||l.trim()==="")&&it("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:l,item:s})}break}case"function":{let n=typeof s=="object"&&s!==null&&"functionId"in s&&typeof s.functionId=="string"?s.functionId:void 0,r=typeof s=="object"&&s!==null&&"result"in s?s.result:void 0;if(!n||n.trim()==="")throw new Error(`Function message must have a non-empty functionId, received: ${e(n)}`);if(r==null)throw new Error(`Function message must have a result, received: ${e(r)}`);if(typeof r!="string")throw new Error(`Function message result must be a string, received: ${e(r)}`);s.isError!==void 0&&typeof s.isError!="boolean"&&it("Function message isError must be a boolean when provided",{fieldPath:"isError",value:s.isError,item:s});break}default:throw new Error(`Unsupported message role: ${e(t)}`)}}function Vr(s){let e=n=>JSON.stringify(n,null,2),t=Array.isArray(s)?s:[s];if(t.length===0)throw new Error(`Chat response results cannot be empty, received: ${e(t)}`);for(let n=0;n<t.length;n++){let r=t[n];if(!r)throw new Error(`Chat response result at index ${n} cannot be null or undefined, received: ${e(r)}`);if(typeof r.index!="number")throw new Error(`Chat response result at index ${n} must have a numeric index, received: ${e(r.index)}`);if(r.index<0)throw new Error(`Chat response result at index ${n} must have a non-negative index, received: ${e(r.index)}`);if(!r.content&&!r.thought&&(!r.thoughtBlocks||r.thoughtBlocks.length===0)&&!r.functionCalls&&!r.finishReason)throw new Error(`Chat response result at index ${n} must have at least one of: content, thought, thoughtBlocks, functionCalls, or finishReason, received: ${e({content:r.content,thought:r.thought,thoughtBlocks:r.thoughtBlocks,functionCalls:r.functionCalls,finishReason:r.finishReason})}`);if(r.content!==void 0&&typeof r.content!="string")throw new Error(`Chat response result content at index ${n} must be a string, received: ${e(r.content)}`);if(r.thought!==void 0&&typeof r.thought!="string")throw new Error(`Chat response result thought at index ${n} must be a string, received: ${e(r.thought)}`);if(r.thoughtBlocks!==void 0){if(!Array.isArray(r.thoughtBlocks))throw new Error(`Chat response result thoughtBlocks at index ${n} must be an array, received: ${e(r.thoughtBlocks)}`);for(let o=0;o<r.thoughtBlocks.length;o++){let i=r.thoughtBlocks[o];if(typeof i!="object"||i===null)throw new Error(`Chat response result thoughtBlocks[${o}] at index ${n} must be an object, received: ${e(i)}`);if(typeof i.data!="string")throw new Error(`Chat response result thoughtBlocks[${o}].data at index ${n} must be a string, received: ${e(i.data)}`);if(typeof i.encrypted!="boolean")throw new Error(`Chat response result thoughtBlocks[${o}].encrypted at index ${n} must be a boolean, received: ${e(i.encrypted)}`);if(i.signature!==void 0&&typeof i.signature!="string")throw new Error(`Chat response result thoughtBlocks[${o}].signature at index ${n} must be a string when provided, received: ${e(i.signature)}`)}}if(r.name!==void 0){if(typeof r.name!="string")throw new Error(`Chat response result name at index ${n} must be a string, received: ${e(r.name)}`);if(r.name.trim()==="")throw new Error(`Chat response result name at index ${n} cannot be empty or whitespace-only, received: ${e(r.name)}`)}if(r.annotations!==void 0){if(!Array.isArray(r.annotations))throw new Error(`Chat response result annotations at index ${n} must be an array, received: ${e(r.annotations)}`);for(let o=0;o<r.annotations.length;o++){let i=r.annotations[o];if(!i||typeof i!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must be an object, received: ${e(i)}`);if(i.type!=="url_citation")throw new Error(`Chat response result annotation at index ${n}[${o}] must have type 'url_citation', received: ${e(i.type)}`);if(!i.url_citation||typeof i.url_citation!="object")throw new Error(`Chat response result annotation at index ${n}[${o}] must have a valid url_citation object, received: ${e(i.url_citation)}`);if(typeof i.url_citation.url!="string")throw new Error(`Chat response result annotation at index ${n}[${o}] url_citation.url must be a string, received: ${e(i.url_citation.url)}`)}}if(r.id!==void 0){if(typeof r.id!="string")throw new Error(`Chat response result id at index ${n} must be a string, received: ${e(r.id)}`);if(r.id.trim()==="")throw new Error(`Chat response result id at index ${n} cannot be empty or whitespace-only, received: ${e(r.id)}`)}if(r.functionCalls!==void 0){if(!Array.isArray(r.functionCalls))throw new Error(`Chat response result functionCalls at index ${n} must be an array, received: ${e(r.functionCalls)}`);for(let o=0;o<r.functionCalls.length;o++){let i=r.functionCalls[o];if(!i)throw new Error(`Function call at index ${o} in result ${n} cannot be null or undefined, received: ${e(i)}`);if(!i.id||typeof i.id!="string"||i.id.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty string id, received: ${e(i.id)}`);if(i.type!=="function")throw new Error(`Function call at index ${o} in result ${n} must have type 'function', received: ${e(i.type)}`);if(!i.function)throw new Error(`Function call at index ${o} in result ${n} must have a function object, received: ${e(i.function)}`);if(!i.function.name||typeof i.function.name!="string"||i.function.name.trim()==="")throw new Error(`Function call at index ${o} in result ${n} must have a non-empty function name, received: ${e(i.function.name)}`);if(i.function.params!==void 0&&typeof i.function.params!="string"&&typeof i.function.params!="object")throw new Error(`Function call params at index ${o} in result ${n} must be a string or object, received: ${e(i.function.params)}`)}}if(r.finishReason!==void 0){let o=["stop","length","function_call","content_filter","error"];if(!o.includes(r.finishReason))throw new Error(`Chat response result finishReason at index ${n} must be one of: ${o.join(", ")}, received: ${e(r.finishReason)}`)}}}var Ds=new Map;function op(s){return`${s.providerName}:${s.contentHash}`}function sp(s,e){e.type==="text"?s.update(`text:${e.text}`):e.type==="image"?s.update(`image:${e.mimeType}:${e.image.slice(0,100)}`):e.type==="audio"?s.update(`audio:${e.format}:${e.data.slice(0,100)}`):e.type==="file"&&("fileUri"in e?s.update(`file:${e.mimeType}:${e.fileUri}`):s.update(`file:${e.mimeType}:${e.data.slice(0,100)}`))}function ip(s){let e=He("sha256"),t=-1;for(let n=s.length-1;n>=0;n--){let r=s[n];if("cache"in r&&r.cache){t=n;break}}for(let n=0;n<s.length;n++){let r=s[n];if(r.role==="system"){e.update(`system:${r.content}`);continue}if(t>=0&&n<=t)if(r.role==="user"){if(typeof r.content=="string")e.update(`user:${r.content}`);else if(Array.isArray(r.content))for(let o of r.content)sp(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var ye=()=>structuredClone({temperature:0}),_e=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Oe=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:i,options:a={},supportFor:u,models:l}){this.aiImpl=e;this.name=t,this.apiURL=n||"",this.headers=r,this.supportFor=u,this.tracer=a.tracer??re.tracer,this.meter=a.meter??re.meter,this.modelInfo=o,this.models=l,this.id=Ge();let c=this.getModel(i.model)??i.model,p=this.getEmbedModel(i.embedModel)??i.embedModel;if(this.defaults={model:c,embedModel:p},!i.model||typeof i.model!="string"||i.model==="")throw new Error("No model defined");this.setOptions(a),l&&ap(l)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=re.logger??Ia;corsProxy;retry;customLabels;modelInfo;modelUsage;embedModelUsage;defaults;lastUsedModelConfig;lastUsedChatModel;lastUsedEmbedModel;apiURL;name;id;headers;supportFor;metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getMetricsInstruments(){return Fa(this.meter)}setName(e){this.name=e}getId(){return this.id}setAPIURL(e){this.apiURL=e}setHeaders(e){this.headers=e}get debug(){return this.#e}setOptions(e){this.#e=e.debug??re.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??re.tracer,this.meter=e.meter??re.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??re.logger??this.logger,this.corsProxy=e.corsProxy,this.retry=e.retry,this.customLabels=e.customLabels}getOptions(){return{debug:this.#e,verbose:this.#t,rateLimiter:this.rt,fetch:this.fetch,tracer:this.tracer,meter:this.meter,timeout:this.timeout,excludeContentFromTrace:this.excludeContentFromTrace,abortSignal:this.abortSignal,logger:this.logger,corsProxy:this.corsProxy,retry:this.retry,customLabels:this.customLabels}}getLogger(){return this.logger}getMergedCustomLabels(e){return Et(re.customLabels,this.customLabels,e)}getModelList(){let e=[];for(let t of this.models??[])t.isInternal||("model"in t&&t.model&&e.push({key:t.key,description:t.description,model:t.model}),"embedModel"in t&&t.embedModel&&e.push({key:t.key,description:t.description,embedModel:t.embedModel}));return e}getName(){return this.name}getFeatures(e){return typeof this.supportFor=="function"?this.supportFor(e??this.defaults.model):this.supportFor}getLastUsedChatModel(){return this.lastUsedChatModel}getLastUsedEmbedModel(){return this.lastUsedEmbedModel}getLastUsedModelConfig(){return this.lastUsedModelConfig}calculatePercentile(e,t){if(e.length===0)return 0;let n=[...e].sort((o,i)=>o-i),r=Math.ceil(t/100*n.length)-1;return n[r]??0}updateLatencyMetrics(e,t,n){let r=this.metrics.latency[e];r.samples.push(t),r.samples.length>1e3&&r.samples.shift(),r.mean=r.samples.reduce((i,a)=>i+a,0)/r.samples.length,r.p95=this.calculatePercentile(r.samples,95),r.p99=this.calculatePercentile(r.samples,99);let o=this.getMetricsInstruments();if(o){let i=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);_a(o,e,t,this.name,i,a),Na(o,e,r.mean,r.p95,r.p99,this.name,i,a)}}updateErrorMetrics(e,t,n){let r=this.metrics.errors[e];r.total++,t&&r.count++,r.rate=r.count/r.total;let o=this.getMetricsInstruments();if(o){let i=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel,a=this.getMergedCustomLabels(n);Da(o,e,this.name,i,a),t&&La(o,e,this.name,i,a),$a(o,e,r.rate,this.name,i,a)}}recordTokenUsage(e,t){let n=this.getMetricsInstruments();if(n&&e?.tokens){let{promptTokens:r,completionTokens:o,totalTokens:i,thoughtsTokens:a,cacheReadTokens:u,cacheCreationTokens:l}=e.tokens,c=this.getMergedCustomLabels(t);r&&Zn(n,"input",r,this.name,e.model,c),o&&Zn(n,"output",o,this.name,e.model,c),i&&Zn(n,"total",i,this.name,e.model,c),a&&Zn(n,"thoughts",a,this.name,e.model,c),u&&$s(n,"read",u,this.name,e.model,c),l&&$s(n,"write",l,this.name,e.model,c)}}calculateRequestSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}calculateResponseSize(e){try{return new TextEncoder().encode(JSON.stringify(e)).length}catch{return 0}}detectMultimodalContent(e){let t=!1,n=!1;if(e.chatPrompt&&Array.isArray(e.chatPrompt)){for(let r of e.chatPrompt)if(r.role==="user"&&Array.isArray(r.content))for(let o of r.content)o.type==="image"?t=!0:o.type==="audio"&&(n=!0)}return{hasImages:t,hasAudio:n}}calculatePromptLength(e){let t=0;if(e.chatPrompt&&Array.isArray(e.chatPrompt))for(let n of e.chatPrompt)if(n.role==="system"||n.role==="assistant")n.content&&(t+=n.content.length);else if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)r.type==="text"&&(t+=r.text.length)}else n.role==="function"&&n.result&&(t+=n.result.length);return t}calculateContextWindowUsage(e,t){if(!t?.tokens?.promptTokens)return 0;let n=this.modelInfo.find(r=>r.name===e);return n?.contextWindow?t.tokens.promptTokens/n.contextWindow:0}estimateCost(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(u=>u.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,i=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*i/1e6+o*a/1e6}estimateCostByName(e,t){if(!t?.tokens)return 0;let n=this.modelInfo.find(u=>u.name===e);if(!n||!n.promptTokenCostPer1M&&!n.completionTokenCostPer1M)return 0;let{promptTokens:r=0,completionTokens:o=0}=t.tokens,i=n.promptTokenCostPer1M||0,a=n.completionTokenCostPer1M||0;return r*i/1e6+o*a/1e6}recordFunctionCallMetrics(e,t,n){let r=this.getMetricsInstruments();if(!(!r||!e))for(let o of e)o&&typeof o=="object"&&"function"in o&&o.function&&typeof o.function=="object"&&"name"in o.function&&Ua(r,o.function.name,void 0,this.name,t,this.getMergedCustomLabels(n))}recordTimeoutMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;qa(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordAbortMetric(e,t){let n=this.getMetricsInstruments();if(n){let r=e==="chat"?this.lastUsedChatModel:this.lastUsedEmbedModel;Ha(n,e,this.name,r,this.getMergedCustomLabels(t))}}recordChatMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedChatModel,i=this.lastUsedModelConfig,a=this.getMergedCustomLabels(t?.customLabels),u=i?.stream??!1;Ga(r,"chat",u,this.name,o,a);let{hasImages:l,hasAudio:c}=this.detectMultimodalContent(e);Wa(r,l,c,this.name,o,a);let p=this.calculatePromptLength(e);ja(r,p,this.name,o,a),Ba(r,i?.temperature,i?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&Ka(r,this.modelUsage.tokens.thoughtsTokens,this.name,o,a);let d=this.calculateRequestSize(e);if(_s(r,"chat",d,this.name,o,a),n&&!u){let m=n,g=this.calculateResponseSize(m);if(Ns(r,"chat",g,this.name,o,a),m.results)for(let y of m.results)y.functionCalls&&this.recordFunctionCallMetrics(y.functionCalls,this.lastUsedChatModel,a);let f=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);f>0&&za(r,f,this.name,o,a);let x=this.estimateCost(this.lastUsedChatModel,m.modelUsage);x>0&&Ls(r,"chat",x,this.name,o,a)}}recordEmbedMetrics(e,t,n){let r=this.getMetricsInstruments();if(!r)return;let o=this.lastUsedEmbedModel,i=this.getMergedCustomLabels(n?.customLabels),a=this.calculateRequestSize(e);_s(r,"embed",a,this.name,o,i);let u=this.calculateResponseSize(t);Ns(r,"embed",u,this.name,o,i);let l=this.estimateCostByName(o,t.modelUsage);l>0&&Ls(r,"embed",l,this.name,o,i)}getMetrics(){return structuredClone(this.metrics)}async chat(e,t){let n=performance.now(),r=!1,o,i=this.getModelByKey(e.model),a=i?i.thinkingTokenBudget:void 0,u={...i?{thinkingTokenBudget:a,showThoughts:i.showThoughts,stream:i.stream,debug:i.debug,useExpensiveModel:i.useExpensiveModel}:void 0,...Object.fromEntries(Object.entries(t??{}).filter(([,l])=>l!==void 0))};try{return o=await this._chat1(e,u),o}catch(l){throw r=!0,l instanceof Error&&(l.message.includes("timeout")||l.name==="TimeoutError"?this.recordTimeoutMetric("chat",u?.customLabels):(l.message.includes("abort")||l.name==="AbortError")&&this.recordAbortMetric("chat",u?.customLabels)),l}finally{let l=performance.now()-n;this.updateLatencyMetrics("chat",l,u?.customLabels),this.updateErrorMetrics("chat",r,u?.customLabels),r||this.recordChatMetrics(e,u,o)}}async _chat1(e,t){let n=this.getModel(e.model)??e.model??this.defaults.model;if(Array.isArray(e.chatPrompt))for(let l of e.chatPrompt)qt(l);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},i=this.modelInfo.find(l=>l.name===n);if(i?.notSupported?.temperature&&"temperature"in o&&delete o.temperature,i?.notSupported?.topP&&"topP"in o&&delete o.topP,t?.thinkingTokenBudget&&!this.getFeatures(n).hasThinkingBudget)throw new Error(`Model ${n} does not support thinkingTokenBudget.`);if(t?.showThoughts&&!this.getFeatures(n).hasShowThoughts)throw new Error(`Model ${n} does not support showThoughts.`);if(this.modelInfo.find(l=>l.name===n)?.isExpensive&&t?.useExpensiveModel!=="yes")throw new Error(`Model ${n} is marked as expensive and requires explicit confirmation. Set useExpensiveModel: "yes" to proceed.`);return o.stream=(t?.stream!==void 0?t.stream:o.stream)??!0,this.getFeatures(n).streaming||(o.stream=!1),this.tracer?await this.tracer.startActiveSpan("AI Chat Request",{kind:Ht.SpanKind.SERVER,attributes:{[ae.LLM_SYSTEM]:this.name,[ae.LLM_OPERATION_NAME]:"chat",[ae.LLM_REQUEST_MODEL]:n,[ae.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[ae.LLM_REQUEST_TEMPERATURE]:o.temperature,[ae.LLM_REQUEST_TOP_P]:o.topP??"Not set",[ae.LLM_REQUEST_TOP_K]:o.topK??"Not set",[ae.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[ae.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[ae.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[ae.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??Ht.context.active(),async l=>await this._chat2(n,o,e,t,l)):await this._chat2(n,o,e,t)}cleanupFunctionSchema(e){let t={...e};if(t.parameters){let n={...t.parameters};Array.isArray(n.required)&&n.required.length===0&&delete n.required,n.properties&&Object.keys(n.properties).length===0&&delete n.properties,Object.keys(n).length===0||Object.keys(n).length===1&&n.type==="object"?delete t.parameters:t.parameters=n}return t}async _chat2(e,t,n,r,o){if(!this.aiImpl.createChatReq)throw new Error("createChatReq not implemented");let i=r?.debug??this.#e,a=r?.verbose??this.#t,u;n.functions&&n.functions.length>0&&(u=n.functions.map(A=>this.cleanupFunctionSchema(A)));let l={...n,model:e,functions:u,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,i&&Ta(l.chatPrompt,r?.stepIndex??0,r?.logger??this.logger,r?.debugHideSystemPrompt);let c=this.getFeatures(e).functions,p=r?.functionCallMode??"auto",m=p==="prompt"||p==="auto"&&!c?{...l,chatPrompt:l.chatPrompt.map(A=>{if(A.role==="assistant"){let{content:b,name:I,cache:T}=A;return{role:"assistant",content:b,name:I,cache:T}}return A.role==="function"?{role:"user",content:A.result}:A}),functions:[]}:l,g=await this.handleContextCaching(e,m,r,o),f=async()=>{if(g?.preparedRequest){let{apiConfig:T,request:R}=g.preparedRequest;return o?.isRecording()&&Ja(n,o,this.excludeContentFromTrace),await Be({name:T.name,url:this.apiURL,localCall:T.localCall,headers:await this.buildHeaders(T.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},R)}let[A,b]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&Ja(n,o,this.excludeContentFromTrace),await Be({name:A.name,url:this.apiURL,localCall:A.localCall,headers:await this.buildHeaders(A.headers),stream:t.stream,timeout:this.timeout,verbose:a,fetch:this.fetch,span:o,abortSignal:r?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:r?.retry??this.retry},b)},x=r?.rateLimiter??this.rt,y=x?await x(f,{modelUsage:this.modelUsage}):await f();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let A=this.aiImpl.createChatStreamResp.bind(this),b=k=>v=>{let O=A(v,k);if(O.sessionId=r?.sessionId,!O.modelUsage){let P=this.aiImpl.getTokenUsage();P&&(O.modelUsage={ai:this.name,model:e,tokens:P})}return this.modelUsage=O.modelUsage,this.recordTokenUsage(O.modelUsage,r?.customLabels),o?.isRecording()&&Qa(O,o,this.excludeContentFromTrace),O},I=async k=>{o?.isRecording()&&o.end(),i&&Ra(k,r?.logger??this.logger)};if(typeof window<"u"){let k=y,v={},O=[],P=r?.abortSignal??this.abortSignal;return new ReadableStream({start:$=>{let E=k.getReader(),F=()=>{try{E.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{$.error(new DOMException("Aborted","AbortError"))}catch{$.error(new Error("Aborted"))}};if(P){if(P.aborted){F();return}P.addEventListener("abort",F,{once:!0})}async function _(){try{for(;;){let{done:j,value:H}=await E.read();if(j){I&&await I(O),$.close();break}let L=b(v)(H);L&&(O.push(L),$.enqueue(L))}}catch(j){if($.error(j),o?.isRecording())try{o.end()}catch{}}finally{if(E.releaseLock(),P)try{P.removeEventListener("abort",F)}catch{}}}_()}})}return y.pipeThrough(new qr(b({}),I))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let h=this.aiImpl.createChatResp(y);if(h.sessionId=r?.sessionId,!h.modelUsage){let A=this.aiImpl.getTokenUsage();A&&(h.modelUsage={ai:this.name,model:e,tokens:A})}return h.modelUsage&&(this.modelUsage=h.modelUsage,this.recordTokenUsage(h.modelUsage,r?.customLabels)),o?.isRecording()&&(Qa(h,o,this.excludeContentFromTrace),o.end()),i&&wa(h,r?.logger??this.logger),h}async embed(e,t){let n=performance.now(),r=!1,o,i=this.getModelByKey(e.embedModel),a={...i?{thinkingTokenBudget:i.thinkingTokenBudget,showThoughts:i.showThoughts,stream:i.stream,debug:i.debug,useExpensiveModel:i.useExpensiveModel}:void 0,...t};try{return o=await this._embed1(e,a),o}catch(u){throw r=!0,u instanceof Error&&(u.message.includes("timeout")||u.name==="TimeoutError"?this.recordTimeoutMetric("embed",a?.customLabels):(u.message.includes("abort")||u.name==="AbortError")&&this.recordAbortMetric("embed",a?.customLabels)),u}finally{let u=performance.now()-n;this.updateLatencyMetrics("embed",u,a?.customLabels),this.updateErrorMetrics("embed",r,a?.customLabels),!r&&o&&this.recordEmbedMetrics(e,o,a)}}async _embed1(e,t){let n=this.getEmbedModel(e.embedModel)??e.embedModel??this.defaults.embedModel;if(!n)throw new Error("No embed model defined");return this.tracer?await this.tracer.startActiveSpan("AI Embed Request",{kind:Ht.SpanKind.SERVER,attributes:{[ae.LLM_SYSTEM]:this.name,[ae.LLM_OPERATION_NAME]:"embeddings",[ae.LLM_REQUEST_MODEL]:n}},t?.traceContext??Ht.context.active(),async r=>await this._embed2(n,e,t,r)):await this._embed2(n,e,t)}async _embed2(e,t,n,r){if(!this.aiImpl.createEmbedReq)throw new Error("createEmbedReq not implemented");if(!this.aiImpl.createEmbedResp)throw new Error("createEmbedResp not implemented");let o=this.aiImpl.createEmbedReq.bind(this.aiImpl),i=n?.debug??this.#e,a=n?.verbose??this.#t,u={...t,embedModel:e};this.lastUsedEmbedModel=e,i&&Ea(u.texts??[],e,n?.logger??this.logger);let l=async()=>{let[m,g]=await o(u);return await Be({name:m.name,url:this.apiURL,localCall:m.localCall,headers:await this.buildHeaders(m.headers),verbose:a,fetch:this.fetch,timeout:this.timeout,span:r,abortSignal:n?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:n?.retry??this.retry},g)},c=n?.rateLimiter??this.rt,p=c?await c(l,{modelUsage:this.embedModelUsage}):await l(),d=this.aiImpl.createEmbedResp?.(p);if(d.sessionId=n?.sessionId,!d.modelUsage){let m=this.aiImpl.getTokenUsage();m&&(d.modelUsage={ai:this.name,model:e,tokens:m})}return this.embedModelUsage=d.modelUsage,this.recordTokenUsage(d.modelUsage,n?.customLabels),r?.isRecording()&&d.modelUsage?.tokens&&r.addEvent(st.GEN_AI_USAGE,{[ae.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[ae.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[ae.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),i&&Ma(d.embeddings,n?.logger??this.logger),r?.end(),d}async buildHeaders(e={}){return{...e,...await this.headers()}}getModelByKey(e){return e?this.models?.find(n=>n.key===e):void 0}getModel(e){let t=this.getModelByKey(e);return t&&"model"in t?t.model:void 0}getEmbedModel(e){let t=this.getModelByKey(e);return t&&"embedModel"in t?t.embedModel:void 0}async handleContextCaching(e,t,n,r){let o=n?.contextCache;if(!o)return null;let i=this.aiImpl.supportsContextCache?.(e)??!1,a=this.aiImpl.supportsImplicitCaching?.(e)??!1;if(!i&&!a)throw new Error(`Context caching is not supported by this provider/model (${this.getName()}/${e}). Remove the contextCache option or use a provider that supports caching.`);if(!i)return null;let u=o.ttlSeconds??3600,l=o.refreshWindowSeconds??300,c=o.minTokens??2048;if(o.name)return this.useCacheByName(e,t,o.name,n,r);let p=ip(t.chatPrompt);if(!p||p===He("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=op(d),g=Date.now(),f=o.registry,x=f?await f.get(m):Ds.get(m);if(x&&x.expiresAt>g){if(x.expiresAt-g<l*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(x.cacheName,u),n,r);let b={cacheName:x.cacheName,expiresAt:g+u*1e3,tokenCount:x.tokenCount};f?await f.set(m,b):Ds.set(m,{...b,contentHash:p,lastTouchedAt:g})}return this.useCacheByName(e,t,x.cacheName,n,r)}if(this.estimateCacheableTokens(t.chatPrompt)<c)return null;let h=this.aiImpl.buildCacheCreateOp?.(t,n);if(h){let A=await this.executeCacheOperation(h,n,r);if(A){let b={cacheName:A.name,expiresAt:new Date(A.expiresAt).getTime(),tokenCount:A.tokenCount};return f?await f.set(m,b):Ds.set(m,{...b,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,A.name,n,r)}}return null}async useCacheByName(e,t,n,r,o){if(this.aiImpl.prepareCachedChatReq){let i=await this.aiImpl.prepareCachedChatReq(t,r??{},n);return{preparedRequest:{apiConfig:i.apiConfig,request:i.request}}}return null}async executeCacheOperation(e,t,n){let r=t?.verbose??this.#t;try{n?.addEvent("context_cache.operation",{type:e.type,endpoint:e.apiConfig.name});let o=await Be({name:e.apiConfig.name,url:this.apiURL,localCall:e.apiConfig.localCall,headers:await this.buildHeaders(e.apiConfig.headers),stream:!1,timeout:this.timeout,verbose:r,fetch:this.fetch,span:n,abortSignal:t?.abortSignal??this.abortSignal,corsProxy:this.corsProxy,retry:t?.retry??this.retry},e.request);return e.parseResponse(o)}catch(o){n?.addEvent("context_cache.error",{type:e.type,error:o instanceof Error?o.message:String(o)});return}}estimateCacheableTokens(e){let t=0;for(let n of e){if(n.role==="system"){t+=n.content.length;continue}if("cache"in n&&n.cache)if(n.role==="user"){if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content)"cache"in r&&r.cache&&(r.type==="text"?t+=r.text.length:r.type==="image"?t+=1e3:r.type==="audio"?t+=2e3:r.type==="file"&&(t+=500))}else n.role==="assistant"&&n.content&&(t+=n.content.length)}return Math.ceil(t/4)}};function Ja(s,e,t){let n=[];if(s.chatPrompt&&Array.isArray(s.chatPrompt)&&s.chatPrompt.length>0)for(let o of s.chatPrompt)switch(o.role){case"system":if(o.content){let i={};t||(i.content=o.content),e.addEvent(st.GEN_AI_SYSTEM_MESSAGE,i)}break;case"user":if(typeof o.content=="string")n.push(o.content);else if(Array.isArray(o.content))for(let i of o.content)i.type==="text"&&n.push(i.text);break;case"assistant":{let i=o.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params}));if(i&&i.length>0){let a={function_calls:JSON.stringify(i,null,2)};!t&&o.content&&(a.content=o.content),e.addEvent(st.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(st.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let i={id:o.functionId};t||(i.content=o.result),e.addEvent(st.GEN_AI_TOOL_MESSAGE,i);break}}let r={};t||(r.content=n.join(`
169
+ `)),e.addEvent(st.GEN_AI_USER_MESSAGE,r)}function Qa(s,e,t){if(s.modelUsage?.tokens){let n=s.modelUsage.tokens.thoughtsTokens?{[ae.LLM_USAGE_THOUGHTS_TOKENS]:s.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(st.GEN_AI_USAGE,{[ae.LLM_USAGE_INPUT_TOKENS]:s.modelUsage.tokens.promptTokens,[ae.LLM_USAGE_OUTPUT_TOKENS]:s.modelUsage.tokens.completionTokens??0,[ae.LLM_USAGE_TOTAL_TOKENS]:s.modelUsage.tokens.totalTokens,...n})}if(s.results)for(let n=0;n<s.results.length;n++){let r=s.results[n];if(!r||!r.content&&!r.thought&&!r.functionCalls?.length&&!r.finishReason)continue;let o=r.functionCalls?.map(a=>({id:a.id,type:a.type,function:a.function.name,arguments:a.function.params})),i={};o&&o.length>0?(t||(i.content=r.content),i.tool_calls=o):t||(i.content=r.content??""),e.addEvent(st.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(i,null,2)})}}function Ya(s){let e=0;for(let t of s){if(!t||typeof t!="object")throw new Error(`AxMessage array validation failed: Item at index ${e} is not a valid message object`);if(t.role!=="user"&&t.role!=="assistant")throw new Error(`AxMessage array validation failed: Item at index ${e} has invalid role: ${t.role}`);e++}}function ap(s){let e=new Set;for(let t of s){if(e.has(t.key))throw new Error(`Duplicate model key detected: "${t.key}". Each model key must be unique.`);e.add(t.key)}}var er=(x=>(x.Claude46Opus="claude-opus-4-6",x.Claude46Sonnet="claude-sonnet-4-6",x.Claude45Opus="claude-opus-4-5-20251101",x.Claude41Opus="claude-opus-4-1-20250805",x.Claude4Opus="claude-opus-4-20250514",x.Claude4Sonnet="claude-sonnet-4-20250514",x.Claude45Sonnet="claude-sonnet-4-5-20250929",x.Claude45Haiku="claude-haiku-4-5",x.Claude37Sonnet="claude-3-7-sonnet-latest",x.Claude35Sonnet="claude-3-5-sonnet-latest",x.Claude35Haiku="claude-3-5-haiku-latest",x.Claude3Opus="claude-3-opus-latest",x.Claude3Sonnet="claude-3-sonnet-20240229",x.Claude3Haiku="claude-3-haiku-20240307",x.Claude21="claude-2.1",x.ClaudeInstant12="claude-instant-1.2",x))(er||{}),tr=(g=>(g.Claude46Opus="claude-opus-4-6",g.Claude46Sonnet="claude-sonnet-4-6",g.Claude45Opus="claude-opus-4-5@20251101",g.Claude41Opus="claude-opus-4-1@20250805",g.Claude4Opus="claude-opus-4@20250514",g.Claude45Sonnet="claude-sonnet-4-5@20250929",g.Claude4Sonnet="claude-sonnet-4@20250514",g.Claude37Sonnet="claude-3-7-sonnet@20250219",g.Claude35SonnetV2="claude-3-5-sonnet-v2@20241022",g.Claude45Haiku="claude-haiku-4-5@20251001",g.Claude35Haiku="claude-3-5-haiku@20241022",g.Claude35Sonnet="claude-3-5-sonnet@20240620",g.Claude3Opus="claude-3-opus@20240229",g.Claude3Haiku="claude-3-haiku@20240307",g))(tr||{});var nr=[{name:"claude-opus-4-6",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:128e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-6",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:128e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-6",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-6",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-5-20251101",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-5@20251101",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:25,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-5-20250929",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-5@20250929",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-haiku-4-5",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-haiku-4-5@20251001",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:2e5,supported:{thinkingBudget:!0,showThoughts:!0}},{name:"claude-opus-4-1-20250805",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-1@20250805",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4-20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-opus-4@20250514",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:32e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4-20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-sonnet-4@20250514",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-7-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-7-sonnet@20250219",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:64e3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-5-sonnet-latest",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{structuredOutputs:!0}},{name:"claude-3-5-sonnet@20240620",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{structuredOutputs:!0}},{name:"claude-3-5-sonnet-v2@20241022",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:8192,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"claude-3-5-haiku-latest",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:4,maxTokens:8192},{name:"claude-3-5-haiku@20241022",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:5,maxTokens:8192},{name:"claude-3-opus-latest",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-opus@20240229",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:75,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-sonnet-20240229",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15,maxTokens:4096,supported:{structuredOutputs:!0}},{name:"claude-3-haiku-20240307",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:1.25,maxTokens:4096},{name:"claude-3-haiku@20240307",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:1.25,maxTokens:4096},{name:"claude-2.1",currency:"usd",promptTokenCostPer1M:8,completionTokenCostPer1M:25,maxTokens:4096},{name:"claude-instant-1.2",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:2.24,maxTokens:4096}];var Tt=s=>{if(!s||typeof s!="object")return s;let e={...s};if(e.type==="object"||Array.isArray(e.type)&&e.type.includes("object")){if(!e.properties||Object.keys(e.properties).length===0)return e;e.additionalProperties===void 0&&(e.additionalProperties=!1)}return delete e.optional,e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([n,r])=>[n,Tt(r)]))),e.items&&(e.items=Tt(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>Tt(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>Tt(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>Tt(n))),e},Us=()=>structuredClone({model:"claude-3-7-sonnet-latest",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},effortLevelMapping:{minimal:"low",low:"low",medium:"medium",high:"high",highest:"max"},...ye()}),Za=()=>structuredClone({model:"claude-3-7-sonnet@20250219",maxTokens:4e4,thinkingTokenBudgetLevels:{minimal:1024,low:5e3,medium:1e4,high:2e4,highest:32e3},effortLevelMapping:{minimal:"low",low:"low",medium:"medium",high:"high",highest:"max"},...ye()}),Gs=class{constructor(e,t){this.config=e;this.isVertex=t}tokensUsed;currentPromptConfig;usedStructuredOutput=!1;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens??4096,temperature:e.temperature,topP:e.topP,topK:e.topK,stream:e.stream,stopSequences:e.stopSequences,endSequences:e.endSequences,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,n:e.n}}createChatReq=async(e,t)=>{this.currentPromptConfig=t;let n=e.model,r=e.modelConfig?.stream??this.config.stream,o;this.isVertex?o={name:r?`/models/${n}:streamRawPredict?alt=sse`:`/models/${n}:rawPredict`}:o={name:"/messages"};let i;if(e.functionCall&&e.functions&&e.functions.length>0)if(typeof e.functionCall=="string")switch(e.functionCall){case"auto":i={tool_choice:{type:"auto"}};break;case"required":i={tool_choice:{type:"any"}};break;case"none":throw new Error("functionCall none not supported")}else if("function"in e.functionCall)i={tool_choice:{type:"tool",name:e.functionCall.function.name}};else throw new Error("Invalid function call type, must be string or object");let a=e.chatPrompt.some(_=>"cache"in _&&_.cache)||e.functions?.some(_=>_.cache),u=e.chatPrompt.filter(_=>_.role==="system"),l=u.map((_,j)=>({type:"text",text:_.content,..._.cache||a&&j===u.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter(_=>_.role!=="system"),p=e.functions?.map((_,j,H)=>{let L={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},X=_.parameters?Tt(_.parameters):void 0;return X===void 0||X&&typeof X=="object"&&Object.keys(X).length===0?X={...L}:X&&typeof X=="object"&&X.type==="object"&&(!("properties"in X)||!X.properties||Object.keys(X.properties).length===0)&&(X={...X,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:_.name,description:_.description,input_schema:X,..._.cache||a&&j===H.length-1?{cache_control:{type:"ephemeral"}}:{}}}),m=(this.config.tools??[]).map(_=>_&&typeof _=="object"&&"type"in _?_:{name:_.name,description:_.description,input_schema:_.input_schema?Tt(_.input_schema):void 0,..._.cache_control?{cache_control:_.cache_control}:{}}),g=[...p??[],...m];g.length===0&&(g=void 0);let f=e.modelConfig?.maxTokens??this.config.maxTokens,x=e.modelConfig?.stopSequences??this.config.stopSequences,y=e.modelConfig?.temperature,h=e.modelConfig?.topP,A=e.modelConfig?.topK??this.config.topK,b=e.modelConfig?.n??this.config.n;if(b&&b>1)throw new Error("Anthropic does not support sampling (n > 1)");let I=_=>_.includes("claude-opus-4-6"),T=_=>_.includes("claude-opus-4-5"),R,k,v=n;if(t?.thinkingTokenBudget){let _=this.config.thinkingTokenBudgetLevels,j=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")R=void 0,k=void 0;else{let H=t.thinkingTokenBudget;if(I(v))R={type:"adaptive"},k={effort:j?.[H]??"medium"};else if(T(v)){R={type:"enabled",budget_tokens:_?.[H]??1e4};let X=j?.[H]??"medium";X==="max"&&(X="high"),k={effort:X}}else R={type:"enabled",budget_tokens:_?.[H]??1e4}}}let P=up(c,!!R);P.some(_=>_.role==="assistant"&&Array.isArray(_.content)&&_.content.length>0&&_.content[0]?.type==="tool_use")&&(R=void 0,k=void 0);let E;if(this.usedStructuredOutput=!1,e.responseFormat&&e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let _=e.responseFormat.schema.schema||e.responseFormat.schema;E={type:"json_schema",schema:Tt(_)},this.usedStructuredOutput=!0}let F={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...f?{max_tokens:f}:{},...x&&x.length>0?{stop_sequences:x}:{},...y!==void 0&&!R?{temperature:y}:{},...h!==void 0&&(!R||h>=.95)?{top_p:h}:{},...A&&!R?{top_k:A}:{},...i,...g?{tools:g}:{},...r?{stream:!0}:{},...l?{system:l}:{},...R?{thinking:R}:{},...k?{output_config:k}:{},...E?{output_format:E}:{},messages:P};return[o,F]};createChatResp=e=>{if(e.type==="error")throw new Ce(e.error.message,void 0,void 0);let t=Xa(e.stop_reason),n=this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1,r="",o=[],i=[],a=[];for(let c of e.content)switch(c.type){case"text":if(r+=c.text??"",Array.isArray(c.citations))for(let p of c.citations)p?.url&&a.push({url:String(p.url),title:typeof p.title=="string"?p.title:void 0,snippet:typeof p.cited_text=="string"?p.cited_text:void 0});break;case"thinking":if(n){let p=c.thinking??"",d=c.signature;i.push({data:p,encrypted:!1,...typeof d=="string"?{signature:d}:{}})}break;case"redacted_thinking":if(n){let p=c.data??"",d=c.signature;i.push({data:p,encrypted:!0,...typeof d=="string"?{signature:d}:{}})}break;case"tool_use":o.push({id:c.id,type:"function",function:{name:c.name,params:c.input}});break}let u={index:0,id:e.id,finishReason:t};r&&(u.content=r),i.length>0&&(u.thoughtBlocks=i,u.thought=i.map(c=>c.data).join("")),o.length>0&&(u.functionCalls=o),a.length>0&&(u.citations=a);let l=[u];return this.tokensUsed={promptTokens:e.usage.input_tokens,completionTokens:e.usage.output_tokens,totalTokens:e.usage.input_tokens+e.usage.output_tokens+(e.usage.cache_creation_input_tokens||0)+(e.usage.cache_read_input_tokens||0),cacheCreationTokens:e.usage.cache_creation_input_tokens,cacheReadTokens:e.usage.cache_read_input_tokens},{results:l,remoteId:e.id}};createChatStreamResp=(e,t)=>{if(!("type"in e))throw new Error("Invalid Anthropic streaming event");let n=t;if(n.indexIdMap||(n.indexIdMap={}),e.type==="error"){let{error:o}=e;throw new Ce(o.message,void 0,void 0)}let r=0;if(e.type==="message_start"){let{message:o}=e,i=[{index:r,content:"",id:o.id}];return this.tokensUsed={promptTokens:o.usage?.input_tokens??0,completionTokens:o.usage?.output_tokens??0,totalTokens:(o.usage?.input_tokens??0)+(o.usage?.output_tokens??0)+(o.usage?.cache_creation_input_tokens??0)+(o.usage?.cache_read_input_tokens??0),cacheCreationTokens:o.usage?.cache_creation_input_tokens,cacheReadTokens:o.usage?.cache_read_input_tokens},{results:i}}if(e.type==="content_block_start"){let{content_block:o}=e;if(o.type==="text"){let i=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&i.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...i.length?{citations:i}:{}}]}}if(o.type==="thinking")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="tool_use"&&typeof o.id=="string"&&typeof e.index=="number"&&!n.indexIdMap[e.index]){n.indexIdMap[e.index]=o.id;let i=[{id:o.id,type:"function",function:{name:o.name,params:""}}];return{results:[{index:r,functionCalls:i}]}}if(o.type==="web_search_tool_result"||o.type==="server_tool_use")return{results:[{index:r,content:""}]}}if(e.type==="content_block_delta"){let{delta:o}=e;if(o.type==="citations_delta"){let i=o.citation;if(i&&typeof i.url=="string"&&i.url.length>0){let a=[{url:String(i.url),title:typeof i.title=="string"?i.title:void 0,snippet:typeof i.cited_text=="string"?i.cited_text:void 0}];return{results:[{index:r,content:"",citations:a}]}}return{results:[{index:r,content:""}]}}if(o.type==="text_delta"){let i=[];if(Array.isArray(o.citations))for(let a of o.citations)a?.url&&i.push({url:String(a.url),title:typeof a.title=="string"?a.title:void 0,snippet:typeof a.cited_text=="string"?a.cited_text:void 0});return{results:[{index:r,content:o.text,...i.length?{citations:i}:{}}]}}if(o.type==="thinking_delta")return this.currentPromptConfig?.thinkingTokenBudget!=="none"&&this.currentPromptConfig?.showThoughts!==!1?{results:[{index:r,thought:o.thinking,thoughtBlocks:[{data:o.thinking,encrypted:!1}]}]}:{results:[{index:r,content:""}]};if(o.type==="signature_delta")return{results:[{index:r,thoughtBlocks:[{data:"",encrypted:!1,signature:o.signature}]}]};if(o.type==="input_json_delta"){let i=n.indexIdMap[e.index];if(!i)return{results:[{index:r,content:""}]};let a=[{id:i,type:"function",function:{name:"",params:o.partial_json}}];return{results:[{index:r,functionCalls:a}]}}}if(e.type==="message_delta"){let{delta:o,usage:i}=e;return this.tokensUsed={promptTokens:this.tokensUsed?.promptTokens??0,completionTokens:i.output_tokens,totalTokens:(this.tokensUsed?.promptTokens??0)+i.output_tokens+(this.tokensUsed?.cacheCreationTokens??0)+(this.tokensUsed?.cacheReadTokens??0),cacheCreationTokens:this.tokensUsed?.cacheCreationTokens,cacheReadTokens:this.tokensUsed?.cacheReadTokens},{results:[{index:r,content:"",finishReason:Xa(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},Kt=class s extends Oe{static create(e){return new s(e)}constructor({apiKey:e,projectId:t,region:n,config:r,options:o,models:i}){let a=t!==void 0&&n!==void 0,u,l;if(a){if(!e)throw new Error("Anthropic Vertex API key not set");if(typeof e!="function")throw new Error("Anthropic Vertex API key must be a function for token-based authentication");u=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/publishers/anthropic/`,l=async()=>({Authorization:`Bearer ${await e()}`,"anthropic-beta":"web-search-2025-03-05"})}else{if(!e)throw new Error("Anthropic API key not set");u="https://api.anthropic.com/v1",l=async()=>({"anthropic-version":"2023-06-01","anthropic-beta":"structured-outputs-2025-11-13, web-search-2025-03-05","x-api-key":typeof e=="function"?await e():e})}let c={...Us(),...r},p=new Gs(c,a),d=g=>{let f=We({model:g,modelInfo:nr,models:i});return{functions:!0,streaming:!0,hasThinkingBudget:f?.supported?.thinkingBudget??!1,hasShowThoughts:f?.supported?.showThoughts??!1,structuredOutputs:f?.supported?.structuredOutputs??!1,functionCot:!0,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:5*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!0,types:["ephemeral"],cacheBreakpoints:!1},thinking:f?.supported?.thinkingBudget??!1,multiTurn:!0}},m=i?.map(g=>{let f=g,x=f?.config;if(!x)return g;let y={};x.maxTokens!==void 0&&(y.maxTokens=x.maxTokens),x.temperature!==void 0&&(y.temperature=x.temperature),x.topP!==void 0&&(y.topP=x.topP),x.topK!==void 0&&(y.topK=x.topK),x.presencePenalty!==void 0&&(y.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(y.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(y.stopSequences=x.stopSequences),x.endSequences!==void 0&&(y.endSequences=x.endSequences),x.stream!==void 0&&(y.stream=x.stream),x.n!==void 0&&(y.n=x.n);let h={...f};Object.keys(y).length>0&&(h.modelConfig={...f.modelConfig??{},...y});let A=x.thinking?.thinkingTokenBudget;if(typeof A=="number"){let b=c.thinkingTokenBudgetLevels,I=[["minimal",b?.minimal??1024],["low",b?.low??5e3],["medium",b?.medium??1e4],["high",b?.high??2e4],["highest",b?.highest??32e3]],T="minimal",R=Number.POSITIVE_INFINITY;for(let[k,v]of I){let O=Math.abs(A-v);O<R&&(R=O,T=k)}h.thinkingTokenBudget=T}return x.thinking?.includeThoughts!==void 0&&(h.showThoughts=!!x.thinking.includeThoughts),h});super(p,{name:"Anthropic",apiURL:u,headers:l,modelInfo:nr,defaults:{model:c.model},options:o,supportFor:d,models:m??i})}};function up(s,e){let t=s.map(r=>{switch(r.role){case"function":return{role:"user",content:[{type:"tool_result",content:r.result,tool_use_id:r.functionId,...r.isError?{is_error:!0}:{},...r.cache?{cache:{type:"ephemeral"}}:{}}]};case"user":return typeof r.content=="string"?{role:"user",content:r.content,...r.cache?{cache_control:{type:"ephemeral"}}:{}}:{role:"user",content:r.content.map(i=>{switch(i.type){case"text":return{type:"text",text:i.text,...i.cache?{cache_control:{type:"ephemeral"}}:{}};case"image":return{type:"image",source:{type:"base64",media_type:i.mimeType,data:i.image},...i.cache?{cache_control:{type:"ephemeral"}}:{}};default:throw new Error("Invalid content type")}})};case"assistant":{let o="",i=[],a=r.thoughtBlocks;if(Array.isArray(a)&&a.length>0)for(let u of a)u.encrypted?i.push(u.signature?{type:"redacted_thinking",data:u.data,signature:u.signature}:{type:"redacted_thinking",data:u.data}):i.push(u.signature?{type:"thinking",thinking:u.data,signature:u.signature}:{type:"thinking",thinking:u.data});if(typeof r.content=="string"&&(i.length>0?o=[...i,{type:"text",text:r.content}]:o=r.content),typeof r.functionCalls<"u"&&(o=r.functionCalls.map(u=>{let l={};if(typeof u.function.params=="string"){let c=u.function.params;if(c.trim().length===0)l={};else try{l=JSON.parse(c)}catch{throw new Error(`Failed to parse function params JSON: ${c}`)}}else typeof u.function.params=="object"&&(l=u.function.params);return{type:"tool_use",id:u.id,name:u.function.name,input:l,...r.cache?{cache_control:{type:"ephemeral"}}:{}}}),Array.isArray(o)&&i.length>0&&(o=[...i,...o])),r.cache){if(typeof o=="string")o=[{type:"text",text:o,cache_control:{type:"ephemeral"}}];else if(Array.isArray(o)&&o.length>0){let u=o.length-1,l=o[u];l&&l.type==="text"&&(o[u]={...l,cache_control:{type:"ephemeral"}})}}return{role:"assistant",content:o}}default:throw new Error("Invalid role")}}),n=lp(t);return cp(n)}function lp(s){let e=[];for(let[t,n]of s.entries()){if(n.role!=="assistant"){e.push(n);continue}if(t>0&&s.at(t-1)?.role==="assistant"){let r=e.pop();e.push({...r||{},...n})}else e.push(n)}return e}function cp(s){return s.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function Xa(s){if(s)switch(s){case"stop_sequence":return"stop";case"max_tokens":return"length";case"tool_use":return"function_call";case"end_turn":return"stop";default:return"stop"}}var rr=(L=>(L.GPT4="gpt-4",L.GPT41="gpt-4.1",L.GPT41Mini="gpt-4.1-mini",L.GPT41Nano="gpt-4.1-nano",L.GPT4O="gpt-4o",L.GPT4OMini="gpt-4o-mini",L.GPT4ChatGPT4O="chatgpt-4o-latest",L.GPT4Turbo="gpt-4-turbo",L.GPT35Turbo="gpt-3.5-turbo",L.GPT35TurboInstruct="gpt-3.5-turbo-instruct",L.GPT35TextDavinci002="text-davinci-002",L.GPT3TextBabbage002="text-babbage-002",L.GPT3TextAda001="text-ada-001",L.GPT5="gpt-5",L.GPT5Nano="gpt-5-nano",L.GPT5Mini="gpt-5-mini",L.GPT5Chat="gpt-5-chat",L.GPT5ChatLatest="gpt-5-chat-latest",L.GPT5Codex="gpt-5-codex",L.GPT5Pro="gpt-5-pro",L.GPT51="gpt-5.1",L.GPT51ChatLatest="gpt-5.1-chat-latest",L.GPT51Codex="gpt-5.1-codex",L.GPT51CodexMini="gpt-5.1-codex-mini",L.GPT51CodexMax="gpt-5.1-codex-max",L.GPT52="gpt-5.2",L.GPT52ChatLatest="gpt-5.2-chat-latest",L.GPT52Codex="gpt-5.2-codex",L.GPT52Pro="gpt-5.2-pro",L.O1="o1",L.O1Mini="o1-mini",L.O3="o3",L.O3Mini="o3-mini",L.O4Mini="o4-mini",L))(rr||{}),Wt=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(Wt||{});var Vt=(U=>(U.GPT4="gpt-4",U.GPT41="gpt-4.1",U.GPT41Mini="gpt-4.1-mini",U.GPT41Nano="gpt-4.1-nano",U.GPT4O="gpt-4o",U.GPT4OMini="gpt-4o-mini",U.GPT4ChatGPT4O="chatgpt-4o-latest",U.GPT4Turbo="gpt-4-turbo",U.GPT35Turbo="gpt-3.5-turbo",U.GPT35TurboInstruct="gpt-3.5-turbo-instruct",U.GPT35TextDavinci002="text-davinci-002",U.GPT3TextBabbage002="text-babbage-002",U.GPT3TextAda001="text-ada-001",U.GPT5="gpt-5",U.GPT5Nano="gpt-5-nano",U.GPT5Mini="gpt-5-mini",U.GPT5Chat="gpt-5-chat",U.GPT5ChatLatest="gpt-5-chat-latest",U.GPT5Codex="gpt-5-codex",U.GPT5Pro="gpt-5-pro",U.GPT51="gpt-5.1",U.GPT51ChatLatest="gpt-5.1-chat-latest",U.GPT51Codex="gpt-5.1-codex",U.GPT51CodexMini="gpt-5.1-codex-mini",U.GPT51CodexMax="gpt-5.1-codex-max",U.GPT52="gpt-5.2",U.GPT52ChatLatest="gpt-5.2-chat-latest",U.GPT52Codex="gpt-5.2-codex",U.GPT52Pro="gpt-5.2-pro",U.O1Pro="o1-pro",U.O1="o1",U.O1Mini="o1-mini",U.O3Pro="o3-pro",U.O3="o3",U.O3Mini="o3-mini",U.O4Mini="o4-mini",U))(Vt||{});var Jt=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,supported:{structuredOutputs:!0}},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6,supported:{structuredOutputs:!0}},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6,supported:{structuredOutputs:!0}},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,supported:{structuredOutputs:!0}},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.4,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-pro",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:120,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5-codex",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:40,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.1-codex-max",currency:"usd",promptTokenCostPer1M:2.5,completionTokenCostPer1M:20,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-chat-latest",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-codex",currency:"usd",promptTokenCostPer1M:1.5,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"gpt-5.2-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:160,notSupported:{temperature:!0,topP:!0},supported:{structuredOutputs:!0}},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{structuredOutputs:!0}},{name:"o1-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:14.4,supported:{structuredOutputs:!0}},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{structuredOutputs:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{structuredOutputs:!0}},{name:"text-embedding-ada-002",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"text-embedding-3-small",currency:"usd",promptTokenCostPer1M:.02,completionTokenCostPer1M:.02},{name:"text-embedding-3-large",currency:"usd",promptTokenCostPer1M:.13,completionTokenCostPer1M:.13}],Jr=[{name:"gpt-4",currency:"usd",promptTokenCostPer1M:30,completionTokenCostPer1M:60},{name:"gpt-4.1",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,supported:{structuredOutputs:!0}},{name:"gpt-4.1-mini",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1.6,supported:{structuredOutputs:!0}},{name:"gpt-4.1-nano",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gpt-4o",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4o-mini",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6,supported:{structuredOutputs:!0}},{name:"chatgpt-4o-latest",currency:"usd",promptTokenCostPer1M:5,completionTokenCostPer1M:15,supported:{structuredOutputs:!0}},{name:"gpt-4-turbo",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:30,supported:{structuredOutputs:!0}},{name:"gpt-3.5-turbo",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"gpt-5-nano",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.4,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-chat",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-pro",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:120,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5-codex",currency:"usd",promptTokenCostPer1M:10,completionTokenCostPer1M:40,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-chat-latest",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex",currency:"usd",promptTokenCostPer1M:1.25,completionTokenCostPer1M:10,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex-mini",currency:"usd",promptTokenCostPer1M:.25,completionTokenCostPer1M:2,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.1-codex-max",currency:"usd",promptTokenCostPer1M:2.5,completionTokenCostPer1M:20,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-chat-latest",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:8,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-codex",currency:"usd",promptTokenCostPer1M:1.5,completionTokenCostPer1M:6,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gpt-5.2-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:160,notSupported:{temperature:!0,topP:!0},supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o1-pro",currency:"usd",promptTokenCostPer1M:150,completionTokenCostPer1M:600,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0},isExpensive:!0},{name:"o1",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o3-pro",currency:"usd",promptTokenCostPer1M:20,completionTokenCostPer1M:80,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0},isExpensive:!0},{name:"o3",currency:"usd",promptTokenCostPer1M:15,completionTokenCostPer1M:60,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o3-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"o4-mini",currency:"usd",promptTokenCostPer1M:1.1,completionTokenCostPer1M:4.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}}];var pp=s=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(s)||e.includes(s)},Mt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...ye()}),Qr=()=>structuredClone({...Mt(),model:"gpt-5"}),Yr=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",..._e()}),Xr=()=>({...Mt(),model:"gpt-5-nano"}),Bs=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.chatReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=(e,t)=>{let n=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let r={name:"/chat/completions"},o=e.functions?.map(m=>({type:"function",function:{name:m.name,description:m.description,parameters:m.parameters}})),i=!e.functionCall&&e.functions&&e.functions.length>0?"auto":e.functionCall,a=dp(e),u=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,l=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=pp(n),d={model:n,messages:a,...e.responseFormat?{response_format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:e.responseFormat}:this.config?.responseFormat?{response_format:{type:this.config.responseFormat}}:{},...o?{tools:o}:{},...i?{tool_choice:i}:{},...p?{}:{...(e.modelConfig?.maxTokens??this.config.maxTokens)!==void 0?{max_completion_tokens:e.modelConfig?.maxTokens??this.config.maxTokens}:{},...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},...(e.modelConfig?.n??this.config.n)!==void 0?{n:e.modelConfig?.n??this.config.n}:{},...(e.modelConfig?.presencePenalty??this.config.presencePenalty)!==void 0?{presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty}:{},...u!==void 0?{frequency_penalty:u}:{}},...(e.modelConfig?.stopSequences??this.config.stop)&&(e.modelConfig?.stopSequences??this.config.stop).length>0?{stop:e.modelConfig?.stopSequences??this.config.stop}:{},...this.config.logitBias!==void 0?{logit_bias:this.config.logitBias}:{},...l&&this.streamingUsage?{stream:!0,stream_options:{include_usage:!0}}:{},...c?{store:c}:{},...this.config.serviceTier?{service_tier:this.config.serviceTier}:{},...this.config.user?{user:this.config.user}:{}};if(this.config.reasoningEffort&&(d.reasoning_effort=this.config.reasoningEffort),this.config.webSearchOptions&&(d.web_search_options={...this.config.webSearchOptions.searchContextSize&&{search_context_size:this.config.webSearchOptions.searchContextSize},...this.config.webSearchOptions.userLocation&&{user_location:{approximate:{type:"approximate",...this.config.webSearchOptions.userLocation.approximate.city&&{city:this.config.webSearchOptions.userLocation.approximate.city},...this.config.webSearchOptions.userLocation.approximate.country&&{country:this.config.webSearchOptions.userLocation.approximate.country},...this.config.webSearchOptions.userLocation.approximate.region&&{region:this.config.webSearchOptions.userLocation.approximate.region},...this.config.webSearchOptions.userLocation.approximate.timezone&&{timezone:this.config.webSearchOptions.userLocation.approximate.timezone}}}}}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d.reasoning_effort=void 0;break;case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":d.reasoning_effort="high";break;case"high":d.reasoning_effort="high";break;case"highest":d.reasoning_effort="high";break}if(!d.reasoning_effort&&t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"minimal":d.reasoning_effort="minimal";break;case"low":d.reasoning_effort="medium";break;case"medium":case"high":case"highest":d.reasoning_effort="high";break}return this.chatReqUpdater&&(d=this.chatReqUpdater(d)),[r,d]};createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]};createChatResp(e){let{id:t,usage:n,choices:r,error:o}=e;if(o)throw o;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{results:r.map(a=>{if(a.message.refusal)throw new Ce(a.message.refusal,e.model,e.id);let u=eu(a.finish_reason),l=a.message.tool_calls?.map(({id:c,function:{arguments:p,name:d}})=>({id:c,type:"function",function:{name:d,params:p}}));return{index:a.index,id:`${a.index}`,content:a.message.content??void 0,thought:a.message.reasoning_content,citations:a.message.annotations?.filter(c=>c?.type==="url_citation"&&c.url_citation).map(c=>({url:c.url_citation?.url,title:c.url_citation?.title,description:c.url_citation?.description})),functionCalls:l,finishReason:u}}),remoteId:t}}createChatStreamResp(e,t){let{id:n,usage:r,choices:o}=e;this.tokensUsed=r?{promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens,totalTokens:r.total_tokens}:void 0;let i=t;return i.indexIdMap||(i.indexIdMap={}),{results:o.map(({index:u,delta:{content:l,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:f})=>{if(p)throw new Ce(p,void 0,n);let x=eu(f),y=d?.map(({id:h,index:A,function:{name:b,arguments:I}})=>{typeof h=="string"&&typeof A=="number"&&!i.indexIdMap[A]&&(i.indexIdMap[A]=h);let T=i.indexIdMap[A];return T?{id:T,type:"function",function:{name:b,params:I}}:null}).filter(h=>h!==null);return{index:u,content:l??void 0,role:c,thought:m,citations:g?.filter(h=>h?.type==="url_citation"&&h.url_citation).map(h=>({url:h.url_citation?.url,title:h.url_citation?.title,description:h.url_citation?.description})),functionCalls:y,finishReason:x,id:n}})}}createEmbedResp(e){let{data:t,usage:n}=e;return this.tokensUsed=n?{promptTokens:n.prompt_tokens,completionTokens:n.completion_tokens,totalTokens:n.total_tokens}:void 0,{embeddings:t.map(r=>r.embedding)}}},eu=s=>{switch(s){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function dp(s){return s.chatPrompt.map(t=>{switch(t.role){case"system":return{role:"system",content:t.content};case"user":{let n=Array.isArray(t.content)?t.content.map(r=>{switch(r.type){case"text":return{type:"text",text:r.text};case"image":return{type:"image_url",image_url:{url:`data:${r.mimeType};base64,${r.image}`,details:r.details??"auto"}};case"audio":return{type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}};default:throw new Error("Invalid content type")}}):t.content;return{role:"user",...t.name?{name:t.name}:{},content:n}}case"assistant":{let n=t.functionCalls?.map(r=>({id:r.id,type:"function",function:{name:r.function.name,arguments:typeof r.function.params=="object"?JSON.stringify(r.function.params):r.function.params}}));if(n&&n.length>0)return{role:"assistant",...t.content?{content:t.content}:{},name:t.name,tool_calls:n};if(t.content===void 0)throw new Error("Assistant content is required when no tool calls are provided");return{role:"assistant",content:t.content,...t.name?{name:t.name}:{}}}case"function":return{role:"tool",content:t.result,tool_call_id:t.functionId};default:throw new Error("Invalid role")}})}var Se=class extends Oe{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:i,chatReqUpdater:a,supportFor:u}){if(!e||e==="")throw new Error("OpenAI API key not set");let l=new Bs(t,n?.streamingUsage??!0,a);super(l,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model,embedModel:t.embedModel},options:n,supportFor:u,models:i})}},Qt=class extends Se{constructor({apiKey:e,apiURL:t,config:n,options:r,models:o,modelInfo:i}){if(!e||e==="")throw new Error("OpenAI API key not set");i=[...Jt,...i??[]];let a=l=>{let c=We({model:l,modelInfo:i,models:o});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,structuredOutputs:c?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","ogg"],maxDuration:25*60},files:{supported:!0,formats:["text/plain","application/pdf","image/jpeg","image/png"],maxSize:512*1024*1024,uploadMethod:"upload"},urls:{supported:!1,webSearch:!0,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:c?.supported?.thinkingBudget??!1,multiTurn:!0}},u=o?.map(l=>{let c=l,p=c?.config;if(!p)return l;let d={};p.maxTokens!==void 0&&(d.maxTokens=p.maxTokens),p.temperature!==void 0&&(d.temperature=p.temperature),p.topP!==void 0&&(d.topP=p.topP),p.presencePenalty!==void 0&&(d.presencePenalty=p.presencePenalty),p.frequencyPenalty!==void 0&&(d.frequencyPenalty=p.frequencyPenalty);let m=p.stopSequences??p.stop;m!==void 0&&(d.stopSequences=m),p.n!==void 0&&(d.n=p.n),p.stream!==void 0&&(d.stream=p.stream);let g={...c};Object.keys(d).length>0&&(g.modelConfig={...c.modelConfig??{},...d});let f=p?.thinking?.thinkingTokenBudget;if(typeof f=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],y="minimal",h=Number.POSITIVE_INFINITY;for(let[A,b]of x){let I=Math.abs(f-b);I<h&&(h=I,y=A)}g.thinkingTokenBudget=y}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...Mt(),...n},options:r,modelInfo:i,models:u??o,supportFor:a}),super.setName("OpenAI")}};var mp=s=>{let e=s.trim();if(!e)return e;if(e.includes("api-version=")){let t=e.indexOf("api-version="),n=e.slice(t);return new URLSearchParams(n).get("api-version")??e}return e},gp=s=>{let e=s.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},js=Mt,tu=Yr,nu=Xr,ru=Qr,Yt=class extends Se{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:i,models:a,modelInfo:u}){if(!e||e==="")throw new Error("Azure OpenAPI API key not set");if(!t||t==="")throw new Error("Azure OpenAPI resource name not set");if(!n||n==="")throw new Error("Azure OpenAPI deployment id not set");let l={...js(),...o},c=mp(r),p=gp(c);u=[...Jt,...u??[]];let d=g=>{let f=We({model:g,modelInfo:u,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:f?.supported?.thinkingBudget??!1,hasShowThoughts:f?.supported?.showThoughts??!1,structuredOutputs:p&&(f?.supported?.structuredOutputs??!1),functionCot:!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:f?.supported?.thinkingBudget??!1,multiTurn:!0}};super({apiKey:e,config:l,options:i,models:a,modelInfo:u,supportFor:d});let m=t.includes("://")?t:`https://${t}.openai.azure.com/`;super.setName("Azure OpenAI"),super.setAPIURL(new URL(`/openai/deployments/${n}?api-version=${c}`,m).href),super.setHeaders(async()=>({"api-key":e}))}};var Zr=class s{services;currentServiceIndex=0;currentService;debug;initialBackoffMs;maxBackoffMs;maxRetries;serviceFailures=new Map;constructor(e,t){if(e.length===0)throw new Error("No AI services provided.");fp(e),this.services=[...e].sort(t?.comparator??s.metricComparator);let n=this.services[this.currentServiceIndex];if(n===void 0)throw new Error("Error initializing the AI services.");this.currentService=n,this.debug=t?.debug??!0,this.initialBackoffMs=t?.initialBackoffMs??1e3,this.maxBackoffMs=t?.maxBackoffMs??32e3,this.maxRetries=t?.maxRetries??3}static create(e,t){return new s(e,t)}getLastUsedChatModel(){return this.currentService.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.currentService.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.currentService.getLastUsedModelConfig()}static inputOrderComparator=()=>0;static metricComparator=(e,t)=>{let n=e.getMetrics(),r=t.getMetrics();return n.latency.chat.mean-r.latency.chat.mean};getModelList(){for(let e of this.services){let t=e.getModelList();if(t)return t}}getNextService(e,t){let n=t+1,r=e[n];return r===void 0?{service:void 0,index:n}:{service:r,index:n}}reset(){this.currentServiceIndex=0;let e=this.services[this.currentServiceIndex];if(e===void 0)throw new Error("No AI services provided.");this.currentService=e}getName(){return this.currentService.getName()}getId(){return this.currentService.getId()}getFeatures(e){let t={functions:!1,streaming:!1,thinking:!1,multiTurn:!1,structuredOutputs:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]}};for(let n of this.services){let r=n.getFeatures(e);r.functions&&(t.functions=!0),r.streaming&&(t.streaming=!0),r.thinking&&(t.thinking=!0),r.multiTurn&&(t.multiTurn=!0),r.structuredOutputs&&(t.structuredOutputs=!0),r.functionCot&&(t.functionCot=!0),r.hasThinkingBudget&&(t.hasThinkingBudget=!0),r.hasShowThoughts&&(t.hasShowThoughts=!0),r.media.images.supported&&(t.media.images.supported=!0),t.media.images.formats=Array.from(new Set([...t.media.images.formats,...r.media.images.formats])),r.media.audio.supported&&(t.media.audio.supported=!0),t.media.audio.formats=Array.from(new Set([...t.media.audio.formats,...r.media.audio.formats])),r.media.files.supported&&(t.media.files.supported=!0),t.media.files.formats=Array.from(new Set([...t.media.files.formats,...r.media.files.formats])),r.media.files.uploadMethod!=="none"&&(t.media.files.uploadMethod=r.media.files.uploadMethod),r.media.urls.supported&&(t.media.urls.supported=!0),r.media.urls.webSearch&&(t.media.urls.webSearch=!0),r.media.urls.contextFetching&&(t.media.urls.contextFetching=!0),r.caching.supported&&(t.caching.supported=!0),t.caching.types=Array.from(new Set([...t.caching.types,...r.caching.types]))}return t}getMetrics(){let e={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}},t=0,n=0,r=0,o=0;for(let i of this.services){let a=i.getMetrics();e.errors.chat.count+=a.errors.chat.count,e.errors.chat.total+=a.errors.chat.total,e.errors.embed.count+=a.errors.embed.count,e.errors.embed.total+=a.errors.embed.total;let u=a.latency.chat.samples.length;u>0&&(t+=a.latency.chat.mean*u,n+=u);let l=a.latency.embed.samples.length;l>0&&(r+=a.latency.embed.mean*l,o+=l)}e.errors.chat.total>0&&(e.errors.chat.rate=e.errors.chat.count/e.errors.chat.total),e.errors.embed.total>0&&(e.errors.embed.rate=e.errors.embed.count/e.errors.embed.total),n>0&&(e.latency.chat.mean=t/n),o>0&&(e.latency.embed.mean=r/o);for(let i of this.services){let a=i.getMetrics();e.latency.chat.p95=Math.max(e.latency.chat.p95,a.latency.chat.p95),e.latency.chat.p99=Math.max(e.latency.chat.p99,a.latency.chat.p99),e.latency.embed.p95=Math.max(e.latency.embed.p95,a.latency.embed.p95),e.latency.embed.p99=Math.max(e.latency.embed.p99,a.latency.embed.p99)}return e}canRetryService(e){let t=this.serviceFailures.get(e.getId());if(!t)return!0;let{retries:n,lastFailureTime:r}=t,o=Date.now()-r,i=Math.min(this.initialBackoffMs*2**n,this.maxBackoffMs);return o>=i}handleFailure(e,t){let r=(this.serviceFailures.get(e.getId())?.retries??0)+1;this.serviceFailures.set(e.getId(),{retries:r,lastFailureTime:Date.now()}),this.debug&&console.warn(`AxBalancer: Service ${e.getName()} failed (retry ${r}/${this.maxRetries})`,t)}handleSuccess(e){this.serviceFailures.delete(e.getId())}async chat(e,t){let n=e.responseFormat?.type==="json_schema",r=e.capabilities,o=r?.requiresImages,i=r?.requiresAudio,a=this.services,u=e.model;if((n||o||i)&&(a=this.services.filter(p=>{let d=p.getFeatures(u);return!(n&&!d.structuredOutputs||o&&!d.media.images.supported||i&&!d.media.audio.supported)}),a.length===0)){let p=[];throw n&&p.push("structured outputs"),o&&p.push("images"),i&&p.push("audio"),new Error(`No services available that support required capabilities: ${p.join(", ")}.`)}let l=0,c=a[l];if(!c)throw new Error("No matching AI services available for request.");for(this.currentService=c;;){if(!this.canRetryService(c)){let p=this.getNextService(a,l);if(!p.service)throw new Error(`All candidate services exhausted (tried ${a.length} service(s))`);c=p.service,l=p.index,this.currentService=c;continue}try{let p=await c.chat(e,t);return this.handleSuccess(c),p}catch(p){if(!(p instanceof Ue))throw p;switch(p.constructor){case xt:throw p;case je:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case Le:break;case ht:break;case Ve:break;case Ke:break;default:throw p}if(this.handleFailure(c,p),(this.serviceFailures.get(c.getId())?.retries??0)>=this.maxRetries){let m=this.getNextService(a,l);if(this.debug&&console.warn(`AxBalancer: Switching to service ${m.service?.getName()??"none"}`,p),!m.service)throw p;c=m.service,l=m.index,this.currentService=c}}}}async embed(e,t){this.reset();let n=this.currentServiceIndex;for(;;){if(!this.canRetryService(this.currentService)){let r=this.getNextService(this.services,n);if(!r.service)throw new Error(`All services exhausted (tried ${this.services.length} service(s))`);this.currentService=r.service,n=r.index,this.currentServiceIndex=n;continue}try{let r=await this.currentService.embed(e,t);return this.handleSuccess(this.currentService),r}catch(r){if(!(r instanceof Ue)||r instanceof je&&![408,429,500,502,503,504].includes(r.status)||r instanceof xt)throw r;if(this.handleFailure(this.currentService,r),(this.serviceFailures.get(this.currentService.getId())?.retries??0)>=this.maxRetries){let i=this.getNextService(this.services,n);if(!i.service)throw r;this.currentService=i.service,n=i.index,this.currentServiceIndex=n}}}}setOptions(e){for(let t of this.services)t.setOptions(e);this.currentService.setOptions(e),this.debug=e.debug??this.debug}getOptions(){return this.currentService.getOptions()}getLogger(){return this.currentService.getLogger()}};function fp(s){let e=s.find(r=>r.getModelList()!==void 0);if(!e)return;let t=e.getModelList();if(!t)throw new Error("No model list found in any service.");let n=new Set(t.map(r=>r.key));for(let r=0;r<s.length;r++){let o=s[r];if(!o)throw new Error(`Service at index ${r} is undefined`);let i=o.getModelList();if(!i)throw new Error(`Service at index ${r} (${o.getName()}) has no model list while another service does.`);let a=new Set(i.map(u=>u.key));for(let u of n)if(!a.has(u))throw new Error(`Service at index ${r} (${o.getName()}) is missing model "${u}"`);for(let u of a)if(!n.has(u))throw new Error(`Service at index ${r} (${o.getName()}) has extra model "${u}"`)}}function Pt(s){let e=!1,t=!1,n=!1,r=!1,o=!1,i=!1,a=!1,u=new Set,l=0;if(s.chatPrompt&&Array.isArray(s.chatPrompt))for(let c of s.chatPrompt){if(c.role==="user"&&Array.isArray(c.content))for(let p of c.content)switch(u.add(p.type),p.type){case"image":e=!0,p.cache&&(a=!0),l+=85;break;case"audio":t=!0,p.cache&&(a=!0),l+=p.duration||60;break;case"file":n=!0,p.cache&&(a=!0),l+=Math.ceil((p.extractedText?.length||1e3)/4);break;case"url":r=!0,p.cache&&(a=!0),l+=Math.ceil((p.cachedContent?.length||2e3)/4);break;case"text":p.cache&&(a=!0),l+=Math.ceil(p.text.length/4);break}else"content"in c&&typeof c.content=="string"&&(l+=Math.ceil(c.content.length/4));"cache"in c&&c.cache&&(a=!0)}return s.functions&&s.functions.length>0&&(o=!0),s.modelConfig?.stream===!0&&(i=!0),s.capabilities&&(s.capabilities.requiresImages&&(e=!0),s.capabilities.requiresAudio&&(t=!0),s.capabilities.requiresFiles&&(n=!0),s.capabilities.requiresWebSearch&&(r=!0)),{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r,requiresFunctions:o,requiresStreaming:i,requiresCaching:a,contentTypes:u,estimatedTokens:l}}function zs(s,e){let t=s.getFeatures(),n=[],r=[],o=[];if(e.hasImages&&!t.media.images.supported&&(n.push("Image support"),o.push("Use altText for images or imageToText service")),e.hasAudio&&!t.media.audio.supported&&(n.push("Audio support"),o.push("Pre-transcribe audio or use transcription field")),e.hasFiles&&!t.media.files.supported&&(n.push("File support"),o.push("Pre-extract text content or use extractedText field")),e.hasUrls&&!t.media.urls.supported&&(n.push("URL/Web search support"),o.push("Pre-fetch content or use cachedContent field")),e.requiresFunctions&&!t.functions&&n.push("Function calling"),e.requiresStreaming&&!t.streaming&&(n.push("Streaming responses"),o.push("Use non-streaming mode")),e.requiresCaching&&!t.caching.supported&&(n.push("Content caching"),o.push("Repeated content will not be cached")),e.hasImages&&t.media.images.supported){let a=t.media.images.maxSize;a&&a<10*1024*1024&&r.push(`Image size limit is ${Math.round(a/(1024*1024))}MB`)}if(e.hasAudio&&t.media.audio.supported){let a=t.media.audio.maxDuration;a&&a<600&&r.push(`Audio duration limit is ${Math.round(a/60)} minutes`)}return{isSupported:n.length===0,missingCapabilities:n,warnings:r,alternatives:o}}function eo(s,e){return s.map(t=>{let n=t.getFeatures(),r=zs(t,e),o=0,i=[];return o+=10,e.hasImages&&n.media.images.supported&&(o+=25,i.push("Images"),n.media.images.detailLevels?.includes("high")&&(o+=5),n.media.images.maxSize&&n.media.images.maxSize>10*1024*1024&&(o+=3)),e.hasAudio&&n.media.audio.supported&&(o+=25,i.push("Audio"),n.media.audio.maxDuration&&n.media.audio.maxDuration>600&&(o+=5)),e.hasFiles&&n.media.files.supported&&(o+=25,i.push("Files"),n.media.files.uploadMethod==="cloud"&&(o+=3)),e.hasUrls&&n.media.urls.supported&&(o+=25,i.push("URLs"),n.media.urls.webSearch&&(o+=5)),e.requiresFunctions&&n.functions&&(o+=15,i.push("Functions"),n.functionCot&&(o+=3)),e.requiresStreaming&&n.streaming&&(o+=10,i.push("Streaming")),e.requiresCaching&&n.caching.supported&&(o+=8,i.push("Caching"),n.caching.types.includes("persistent")&&(o+=3)),n.thinking&&(o+=2),n.multiTurn&&(o+=2),n.hasThinkingBudget&&(o+=1),n.hasShowThoughts&&(o+=1),o-=r.missingCapabilities.length*10,{provider:t,score:o,missingCapabilities:r.missingCapabilities,supportedCapabilities:i}}).sort((t,n)=>n.score-t.score)}function to(s,e,t={}){if(e.length===0)throw new Error("No providers available");let n=Pt(s),r=eo(e,n);if(t.requireExactMatch){let o=r.filter(i=>i.missingCapabilities.length===0);if(o.length===0)throw new Error(`No providers fully support the request requirements: ${r[0]?.missingCapabilities.join(", ")||"unknown requirements"}`);return o[0].provider}if(!t.allowDegradation){let o=r[0];if(o.missingCapabilities.length>0)throw new Error(`Best available provider (${o.provider.getName()}) is missing: ${o.missingCapabilities.join(", ")}`)}return r[0].provider}function ou(s,e){let t=Pt(s),n=eo(e,t),r=n[0]?.provider||null,o=[t.hasImages&&"images",t.hasAudio&&"audio",t.hasFiles&&"files",t.hasUrls&&"URLs",t.requiresFunctions&&"functions",t.requiresStreaming&&"streaming",t.requiresCaching&&"caching"].filter(Boolean).length,i=r?n[0].supportedCapabilities.length:0,a=r?`${r.getName()} supports ${i}/${o} requirements (${Math.round(i/Math.max(o,1)*100)}% compatibility)`:"No suitable providers found";return{requirements:t,providerScores:n,recommendedProvider:r,summary:a}}function su(s,e){return s.filter(t=>t.getFeatures().media[e].supported)}function iu(s,e){let t={};for(let n of s){let o=n.getFeatures().media[e];if(o.supported)for(let i of o.formats)t[i]||(t[i]=[]),t[i].push(n)}return t}var or=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(or||{}),sr=(r=>(r.EmbedEnglishV30="embed-english-v3.0",r.EmbedEnglishLightV30="embed-english-light-v3.0",r.EmbedMultiLingualV30="embed-multilingual-v3.0",r.EmbedMultiLingualLightV30="embed-multilingual-light-v3.0",r))(sr||{});var no=[{name:"command-r-plus",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"command-r",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"command",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5},{name:"command-light",currency:"usd",promptTokenCostPer1M:.3,completionTokenCostPer1M:.6},{name:"embed-english-light-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-english-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-multilingual-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1},{name:"embed-multilingual-light-v3.0",currency:"usd",promptTokenCostPer1M:.1,completionTokenCostPer1M:.1}];function hp(s){if(Array.isArray(s)){let t=["string","number","integer","boolean","object","array"].find(n=>s.includes(n));return t||(s.find(n=>n!=="null")??s[0]??"string")}return typeof s=="string"?s:"string"}var Hs=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...ye()}),uu=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",..._e()}),qs=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,frequencyPenalty:e.frequencyPenalty,presencePenalty:e.presencePenalty,endSequences:e.endSequences,stopSequences:e.stopSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.at(-1),r=e.chatPrompt.slice(0,-1),o;n&&n.role==="user"&&typeof n.content=="string"&&(o=n?.content);let i=xp(r),a=e.functions?.map(p=>{let d={};if(p.parameters?.properties)for(let[m,g]of Object.entries(p.parameters.properties))d[m]={description:g.description,type:hp(g.type),required:p.parameters.required?.includes(m)??!1};return{name:p.name,description:p.description,parameter_definitions:d}}),u=e.chatPrompt.filter(p=>p.role==="function").map(p=>{let d=a?.find(m=>m.name===p.functionId);if(!d)throw new Error("Function not found");return{call:{name:d.name,parameters:d.parameter_definitions},outputs:[{result:p.result??""}]}}),l={name:"/chat"},c={message:o,model:t,tools:a,...u&&!o?{tool_results:u}:{},chat_history:i,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},k:e.modelConfig?.topK??this.config.topK,...e.modelConfig?.topP!==void 0?{p:e.modelConfig.topP}:{},frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,end_sequences:this.config.endSequences,stop_sequences:e.modelConfig?.stopSequences??this.config.stopSequences};return[l,c]}createEmbedReq=e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embed"},r={model:t,texts:e.texts??[],input_type:"classification",truncate:""};return[n,r]};createChatResp=e=>{this.tokensUsed=e.meta.billed_units?{promptTokens:e.meta.billed_units.input_tokens,completionTokens:e.meta.billed_units.output_tokens,totalTokens:e.meta.billed_units.input_tokens+e.meta.billed_units.output_tokens}:void 0;let t;if("finish_reason"in e)switch(e.finish_reason){case"COMPLETE":t="stop";break;case"MAX_TOKENS":t="length";break;case"ERROR":throw new Error("Finish reason: ERROR");case"ERROR_TOXIC":throw new Error("Finish reason: CONTENT_FILTER");default:t="stop";break}let n;return"tool_calls"in e&&(n=e.tool_calls?.map(o=>({id:o.name,type:"function",function:{name:o.name,params:o.parameters}}))),{results:[{index:0,id:e.generation_id,content:e.text,functionCalls:n,finishReason:t}],remoteId:e.response_id}};createChatStreamResp=(e,t)=>{let n=t;e.event_type==="stream-start"&&(n.generation_id=e.generation_id),this.tokensUsed={promptTokens:0,completionTokens:e.meta.billed_units?.output_tokens??0,totalTokens:e.meta.billed_units?.output_tokens??0};let{results:r}=this.createChatResp(e),o=r[0];if(!o)throw new Error("No result");return o.id=n.generation_id??"",{results:r}};createEmbedResp(e){return{remoteId:e.id,embeddings:e.embeddings}}},Xt=class extends Oe{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...Hs(),...t},i=new qs(o),a=r?.map(u=>{let l=u,c=l?.config;if(!c)return u;let p={};c.maxTokens!==void 0&&(p.maxTokens=c.maxTokens),c.temperature!==void 0&&(p.temperature=c.temperature),c.topP!==void 0&&(p.topP=c.topP),c.topK!==void 0&&(p.topK=c.topK),c.presencePenalty!==void 0&&(p.presencePenalty=c.presencePenalty),c.frequencyPenalty!==void 0&&(p.frequencyPenalty=c.frequencyPenalty),c.stopSequences!==void 0&&(p.stopSequences=c.stopSequences),c.endSequences!==void 0&&(p.endSequences=c.endSequences),c.stream!==void 0&&(p.stream=c.stream),c.n!==void 0&&(p.n=c.n);let d={...l};return Object.keys(p).length>0&&(d.modelConfig={...l.modelConfig??{},...p}),d});super(i,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:no,defaults:{model:o.model},supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[],maxSize:0,detailLevels:[]},audio:{supported:!1,formats:[],maxDuration:0},files:{supported:!1,formats:[],maxSize:0,uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},options:n,models:a??r})}};function xp(s){return s.map(e=>{let t="";if(e.role==="system"||e.role==="assistant"||e.role==="user")if(typeof e.content=="string")t=e.content;else throw new Error("Multi-modal content not supported");switch(e.role){case"user":return{role:"USER",message:t};case"system":return{role:"SYSTEM",message:t};case"assistant":{let n=au(e.functionCalls);return{role:"CHATBOT",message:t,tool_calls:n}}case"function":{let n=s.map(i=>{if(i.role==="assistant")return i.functionCalls?.find(a=>a.id===e.functionId)}).filter(i=>i!==void 0),r=au(n)?.at(0);if(!r)throw new Error("Function call not found");let o=[{result:e.result}];return{role:"TOOL",tool_results:[{call:r,outputs:o}]}}default:throw new Error("Unknown role")}})}function au(s){return s?.map(e=>{let t;if(typeof e.function.params=="string"){let n=e.function.params;if(n.trim().length===0)t={};else try{t=JSON.parse(n)}catch{throw new Error(`Failed to parse function params JSON: ${n}`)}}else t=e.function.params;return{name:e.function.name,parameters:t}})}var ir=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(ir||{});var ro=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var Ks=()=>structuredClone({model:"deepseek-chat",...ye()}),lu=()=>structuredClone({model:"deepseek-coder",..._e()}),Zt=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("DeepSeek API key not set");let i={...Ks(),...t};o=[...ro,...o??[]],super({apiKey:e,config:i,options:n,apiURL:"https://api.deepseek.com",modelInfo:o,supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r}),super.setName("DeepSeek")}};var ar=(b=>(b.Gemini31Pro="gemini-3.1-pro-preview",b.Gemini3FlashLite="gemini-3.1-flash-lite-preview",b.Gemini3Flash="gemini-3-flash-preview",b.Gemini3Pro="gemini-3.1-pro-preview",b.Gemini3ProImage="gemini-3-pro-image-preview",b.Gemini25Pro="gemini-2.5-pro",b.Gemini25Flash="gemini-2.5-flash",b.Gemini25FlashLite="gemini-2.5-flash-lite",b.Gemini20Flash="gemini-2.0-flash",b.Gemini20FlashLite="gemini-2.0-flash-lite",b.Gemini20ProExp="gemini-2.0-pro-exp-02-05",b.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",b.Gemini1Pro="gemini-1.0-pro",b.Gemini15Flash="gemini-1.5-flash",b.Gemini15Flash002="gemini-1.5-flash-002",b.Gemini15Flash8B="gemini-1.5-flash-8b",b.Gemini15Pro="gemini-1.5-pro",b.GeminiFlashLatest="gemini-flash-latest",b.GeminiFlashLiteLatest="gemini-flash-lite-latest",b.GeminiProLatest="gemini-pro-latest",b))(ar||{}),oo=(o=>(o.GeminiEmbedding001="gemini-embedding-001",o.GeminiEmbedding="gemini-embedding-exp",o.TextEmbeddingLarge="text-embedding-large-exp-03-07",o.TextEmbedding004="text-embedding-004",o.TextEmbedding005="text-embedding-005",o))(oo||{}),so=(r=>(r.HarmCategoryHarassment="HARM_CATEGORY_HARASSMENT",r.HarmCategoryHateSpeech="HARM_CATEGORY_HATE_SPEECH",r.HarmCategorySexuallyExplicit="HARM_CATEGORY_SEXUALLY_EXPLICIT",r.HarmCategoryDangerousContent="HARM_CATEGORY_DANGEROUS_CONTENT",r))(so||{}),io=(o=>(o.BlockNone="BLOCK_NONE",o.BlockOnlyHigh="BLOCK_ONLY_HIGH",o.BlockMediumAndAbove="BLOCK_MEDIUM_AND_ABOVE",o.BlockLowAndAbove="BLOCK_LOW_AND_ABOVE",o.BlockDefault="HARM_BLOCK_THRESHOLD_UNSPECIFIED",o))(io||{}),Ws=(u=>(u.SemanticSimilarity="SEMANTIC_SIMILARITY",u.Classification="CLASSIFICATION",u.Clustering="CLUSTERING",u.RetrievalDocument="RETRIEVAL_DOCUMENT",u.RetrievalQuery="RETRIEVAL_QUERY",u.QuestionAnswering="QUESTION_ANSWERING",u.FactVerification="FACT_VERIFICATION",u.CodeRetrievalQuery="CODE_RETRIEVAL_QUERY",u))(Ws||{}),cu=["gemini-3.1-pro-preview","gemini-3.1-pro-preview-customtools","gemini-3.1-flash-lite-preview","gemini-3-flash-preview","gemini-2.5-pro","gemini-2.5-flash","gemini-2.5-flash-lite","gemini-2.0-flash","gemini-2.0-flash-lite","gemini-flash-latest","gemini-flash-lite-latest"];var ao=[{name:"gemini-3.1-pro-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:12,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3-flash-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3.1-flash-lite-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.25,completionTokenCostPer1M:1.5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-3-pro-image-preview",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2,completionTokenCostPer1M:.134,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:2.5,completionTokenCostPer1M:15,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-pro-exp-02-05",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-flash-thinking-exp-01-21",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:15,completionTokenCostPer1M:3.5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.5-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.1,completionTokenCostPer1M:.4,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-2.0-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.01,completionTokenCostPer1M:.4,supported:{structuredOutputs:!0}},{name:"gemini-2.0-flash-lite",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{structuredOutputs:!0}},{name:"gemini-1.5-flash",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{structuredOutputs:!0}},{name:"gemini-1.5-flash-8b",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.0375,completionTokenCostPer1M:.15,supported:{structuredOutputs:!0}},{name:"gemini-1.5-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{structuredOutputs:!0}},{name:"gemini-1.0-pro",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.5,completionTokenCostPer1M:1.5,supported:{structuredOutputs:!0}},{name:"gemini-flash-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:.075,completionTokenCostPer1M:.3,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-flash-lite-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:0,completionTokenCostPer1M:0,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}},{name:"gemini-pro-latest",currency:"usd",characterIsToken:!1,promptTokenCostPer1M:1.25,completionTokenCostPer1M:5,supported:{thinkingBudget:!0,showThoughts:!0,structuredOutputs:!0}}];var Ft=s=>s.includes("gemini-3"),pu=s=>s.includes("gemini-3")&&s.includes("pro"),uo=s=>{if(!s||typeof s!="object")return s;let e={...s};return delete e.additionalProperties,delete e.default,delete e.optional,delete e.maximum,delete e.oneOf,delete e.anyOf,Array.isArray(e.type)&&(e.type=e.type.includes("object")?"object":e.type[0]??"string"),e.properties&&typeof e.properties=="object"&&(e.properties=Object.fromEntries(Object.entries(e.properties).map(([t,n])=>[t,uo(n)]))),e.items&&(e.items=uo(e.items)),e},du=[{category:"HARM_CATEGORY_HARASSMENT",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_HATE_SPEECH",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_SEXUALLY_EXPLICIT",threshold:"BLOCK_NONE"},{category:"HARM_CATEGORY_DANGEROUS_CONTENT",threshold:"BLOCK_NONE"}],Js=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:du,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...ye()}),mu=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:du,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},..._e()}),Vs=class{constructor(e,t,n,r,o){this.config=e;this.isVertex=t;this.endpointId=n;this.apiKey=r;this.options=o;if(!this.isVertex&&this.config.autoTruncate)throw new Error("Auto truncate is not supported for GoogleGemini");let i=this.config.model;if(Ft(i)){if(this.config.thinking?.thinkingTokenBudget!==void 0&&typeof this.config.thinking.thinkingTokenBudget=="number")throw new Error(`Gemini 3 models (${i}) do not support numeric thinkingTokenBudget. Use thinkingLevel ('low', 'medium', 'high') instead, or pass thinkingTokenBudget as a string level via options.`);if(pu(i)&&this.config.thinking?.thinkingLevel){let a=this.config.thinking.thinkingLevel;if(a!=="low"&&a!=="high")throw new Error(`Gemini 3 Pro (${i}) only supports thinkingLevel 'low' or 'high', got '${a}'. Use 'low' for less thinking or 'high' for more thinking.`)}}}tokensUsed;models;setModels(e){this.models=e}getEffectiveMappings(e){let t=this.models?.find(n=>n.model===e);return{thinkingLevelMapping:{...this.config.thinkingLevelMapping,...t?.thinkingLevelMapping??{}},thinkingTokenBudgetLevels:{...this.config.thinkingTokenBudgetLevels,...t?.thinkingTokenBudgetLevels??{}}}}getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq=async(e,t)=>{let n=e.model,r=e.modelConfig?.stream??this.config.stream;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let o;if(this.endpointId?o={name:r?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:o={name:r?`/models/${n}:streamGenerateContent?alt=sse`:`/models/${n}:generateContent`},!this.isVertex){let h=r?"&":"?",A=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${h}key=${A}`}let i=e.chatPrompt.filter(h=>h.role==="system").map(h=>h.content),a=i.length>0?{role:"user",parts:[{text:i.join(" ")}]}:void 0,u=[],l=e.chatPrompt.filter(h=>h.role!=="system");for(let h=0;h<l.length;h++){let A=l[h];switch(A.role){case"user":{let b=Array.isArray(A.content)?A.content.map((I,T)=>{switch(I.type){case"text":return{text:I.text};case"image":return{inlineData:{mimeType:I.mimeType,data:I.image}};case"audio":return{inlineData:{mimeType:`audio/${I.format??"mp3"}`,data:I.data}};case"file":return"fileUri"in I?{fileData:{mimeType:I.mimeType,fileUri:I.fileUri}}:{inlineData:{mimeType:I.mimeType,data:I.data}};default:throw new Error(`Chat prompt content type not supported (index: ${T})`)}}):[{text:A.content}];u.push({role:"user",parts:b});break}case"assistant":{let b=[],I=A.thoughtBlocks,T=A.functionCalls&&A.functionCalls.length>0,R=I?.[0],k=I?.map(O=>O.data).join("")??"",v=R?.signature;if(k&&b.push({...T?{}:{thought:!0},text:k,...v&&!T?{thought_signature:v}:{}}),A.functionCalls){let O=A.functionCalls.map((P,$)=>{let E;if(typeof P.function.params=="string"){let _=P.function.params;if(_.trim().length===0)E={};else try{E=JSON.parse(_)}catch{throw new Error(`Failed to parse function params JSON: ${_}`)}}else E=P.function.params;let F={functionCall:{name:P.function.name,args:E}};return v&&$===0&&(F.thought_signature=v),F});b.push(...O)}if(A.content&&b.push({text:A.content}),b.length===0)throw new Error("Assistant content is empty");u.push({role:"model",parts:b});break}case"function":{let b=[],I=A,T=h;for(;;){if(!("functionId"in I))throw new Error(`Chat prompt functionId is empty (index: ${T})`);if(b.push({functionResponse:{name:I.functionId,response:{result:I.result}}}),T+1<l.length&&l[T+1].role==="function")T++,I=l[T];else break}h=T,u.push({role:"user",parts:b});break}default:throw new Error(`Invalid role: ${JSON.stringify(A)} (index: ${h})`)}}let c=[];if(e.functions&&e.functions.length>0){let h=e.functions.map(A=>{let b={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},I=A.parameters?uo(A.parameters):void 0;return I===void 0||I&&typeof I=="object"&&Object.keys(I).length===0?I={...b}:I&&typeof I=="object"&&I.type==="object"&&(!("properties"in I)||!I.properties||Object.keys(I.properties).length===0)&&(I={...I,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:A.name,description:A.description,parameters:I}});c.push({function_declarations:h})}if(this.options?.codeExecution&&c.push({code_execution:{}}),this.options?.googleSearchRetrieval&&c.push({google_search_retrieval:{dynamic_retrieval_config:this.options.googleSearchRetrieval}}),this.options?.googleSearch&&c.push({google_search:{}}),this.options?.googleMaps){let h=this.options.googleMaps,A=h?.enableWidget!==void 0?{enableWidget:h.enableWidget}:{};c.push({google_maps:A})}this.options?.urlContext&&c.push({url_context:{}}),c.length===0&&(c=void 0);let p,d=Array.isArray(c)?c.some(h=>h&&Array.isArray(h.function_declarations)&&h.function_declarations.length>0):!1;if(e.functionCall)if(e.functionCall==="none")p={function_calling_config:{mode:"NONE"}};else if(e.functionCall==="auto")p={function_calling_config:{mode:"AUTO"}};else if(e.functionCall==="required")p={function_calling_config:{mode:"ANY"}};else{let h=e.functionCall.function?.name?{allowedFunctionNames:[e.functionCall.function.name]}:{};p={function_calling_config:{mode:"ANY"},...h}}else d&&(p={function_calling_config:{mode:"AUTO"}});this.options?.retrievalConfig&&(p={...p??{},retrievalConfig:{...this.options.retrievalConfig.latLng?{latLng:this.options.retrievalConfig.latLng}:{}}});let m={};if(this.config.thinking?.includeThoughts&&(m.includeThoughts=!0),this.config.thinking?.thinkingTokenBudget&&(m.thinkingBudget=this.config.thinking.thinkingTokenBudget),this.config.thinking?.thinkingLevel&&Ft(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let h=this.getEffectiveMappings(n),A=h.thinkingTokenBudgetLevels;if(Ft(n)){let I=pu(n),T=h.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=T?.minimal??"minimal";else{let R=t.thinkingTokenBudget,k=T?.[R];k||(k=R==="highest"?"high":R),m.thinkingLevel=k}if(I&&m.thinkingLevel){let R=m.thinkingLevel;R!=="low"&&R!=="high"&&(m.thinkingLevel=R==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=A?.minimal??200;break;case"low":m.thinkingBudget=A?.low??800;break;case"medium":m.thinkingBudget=A?.medium??5e3;break;case"high":m.thinkingBudget=A?.high??1e4;break;case"highest":m.thinkingBudget=A?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,Ft(n)||delete m.thinkingLevel,Ft(n)&&delete m.thinkingBudget;let g=e.modelConfig?.maxTokens??this.config.maxTokens;if(m.thinkingLevel&&g!==void 0)throw new Error("Cannot set maxTokens when using thinkingLevel with Gemini models. When thinking is enabled, the model manages output tokens automatically. Remove the maxTokens setting or disable thinking.");t?.showThoughts!==void 0&&t?.thinkingTokenBudget!=="none"&&(m.includeThoughts=t.showThoughts);let f={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain",...Object.keys(m).length>0?{thinkingConfig:m}:{}};if(Ft(n)&&(f.temperature===void 0||f.temperature<1)&&(f.temperature=1),e.responseFormat){if(f.responseMimeType="application/json",e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let h=e.responseFormat.schema.schema||e.responseFormat.schema;f.responseSchema=uo(h)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(f.responseMimeType="application/json");let x=this.config.safetySettings;return[o,{contents:u,tools:c,toolConfig:p,systemInstruction:a,generationConfig:f,safetySettings:x}]};createEmbedReq=async e=>{let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n,r;if(this.isVertex)this.endpointId?n={name:`/${this.endpointId}:predict`}:n={name:`/models/${t}:predict`},r={instances:e.texts.map(o=>({content:o,...this.config.embedType&&{taskType:this.config.embedType}})),parameters:{autoTruncate:this.config.autoTruncate,outputDimensionality:this.config.dimensions}};else{let o=typeof this.apiKey=="function"?this.apiKey():this.apiKey;n={name:`/models/${t}:batchEmbedContents?key=${o}`},r={requests:e.texts.map(i=>({model:`models/${t}`,content:{parts:[{text:i}]},outputDimensionality:this.config.dimensions,...this.config.embedType&&{taskType:this.config.embedType}}))}}return[n,r]};createChatResp=e=>{let t,n=e.candidates?.map(o=>{let i={index:0};switch(o.finishReason){case"MAX_TOKENS":i.finishReason="length";break;case"STOP":i.finishReason="stop";break;case"SAFETY":throw new Ce("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Ce("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Ce("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Ce("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Ce("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Ce("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Ce("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Ce("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Ce("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return i;for(let l of o.content.parts){if("text"in l){if("thought"in l&&l.thought||l.thought===!0){i.thought=l.text;let c=l.thoughtSignature||l.thought_signature;i.thoughtBlocks||(i.thoughtBlocks=[]),i.thoughtBlocks.push({data:l.text,encrypted:!1,...c?{signature:c}:{}})}else i.content=l.text;continue}if("functionCall"in l){let c=l.thoughtSignature||l.thought_signature;if(c)if(!i.thoughtBlocks||i.thoughtBlocks.length===0)i.thoughtBlocks=[{data:"",encrypted:!1,signature:c}];else{let p=i.thoughtBlocks[i.thoughtBlocks.length-1];p&&!p.signature&&(p.signature=c)}i.functionCalls=[...i.functionCalls??[],{id:Ge(),type:"function",function:{name:l.functionCall.name,params:l.functionCall.args}}]}}let a=o.citationMetadata?.citations;if(Array.isArray(a)&&a.length){let l=c=>c?`${c.year}-${String(c.month).padStart(2,"0")}-${String(c.day).padStart(2,"0")}`:void 0;i.citations=a.filter(c=>typeof c?.uri=="string").map(c=>({url:c.uri,title:c.title,license:c.license,publicationDate:l(c.publicationDate)}))}let u=o.groundingMetadata;if(u){if(Array.isArray(u.groundingChunks)){let l=u.groundingChunks.map(c=>c?.maps).filter(c=>c&&typeof c.uri=="string").map(c=>({url:c.uri,title:c.title}));l.length&&(i.citations=[...i.citations??[],...l])}typeof u.googleMapsWidgetContextToken=="string"&&(t=u.googleMapsWidgetContextToken)}return i});e.usageMetadata&&(this.tokensUsed={totalTokens:e.usageMetadata.totalTokenCount,promptTokens:e.usageMetadata.promptTokenCount,completionTokens:e.usageMetadata.candidatesTokenCount,thoughtsTokens:e.usageMetadata.thoughtsTokenCount,...e.usageMetadata.cachedContentTokenCount!==void 0?{cacheReadTokens:e.usageMetadata.cachedContentTokenCount}:{}});let r={results:n};return t&&(r.providerMetadata={...r.providerMetadata,google:{...r.providerMetadata?.google??{},mapsWidgetContextToken:t}}),r};createChatStreamResp=e=>this.createChatResp(e);createEmbedResp=e=>{let t;return this.isVertex?t=e.predictions.map(n=>n.embeddings.values):t=e.embeddings.map(n=>n.values),{embeddings:t}};supportsContextCache=e=>{let t=e;return cu.some(n=>t.includes(n)||n.includes(t))};buildCacheCreateOp=(e,t)=>{let n=e.model,r=t.contextCache?.ttlSeconds??3600,{systemInstruction:o,contents:i}=this.extractCacheableContent(e.chatPrompt);if(!o&&(!i||i.length===0))return;let a={model:this.isVertex?n:`models/${n}`,ttl:`${r}s`,displayName:`ax-cache-${Date.now()}`};o&&(a.systemInstruction=o),i&&i.length>0&&(a.contents=i);let u;if(this.isVertex)u="/cachedContents";else{u="/cachedContents";let l=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;u+=`?key=${l}`}return{type:"create",apiConfig:{name:u},request:a,parseResponse:l=>{let c=l;if(c?.name)return{name:c.name,expiresAt:c.expireTime,tokenCount:c.usageMetadata?.totalTokenCount}}}};buildCacheUpdateTTLOp=(e,t)=>{let n={ttl:`${t}s`},r=`/${e}`;if(!this.isVertex&&this.apiKey){let o=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;r+=`?key=${o}`}return{type:"update",apiConfig:{name:r,headers:{"Content-Type":"application/json"}},request:n,parseResponse:o=>{let i=o;if(i?.name)return{name:i.name,expiresAt:i.expireTime,tokenCount:i.usageMetadata?.totalTokenCount}}}};buildCacheDeleteOp=e=>{let t=`/${e}`;if(!this.isVertex&&this.apiKey){let n=typeof this.apiKey=="function"?"ASYNC_KEY":this.apiKey;t+=`?key=${n}`}return{type:"delete",apiConfig:{name:t,headers:{"Content-Type":"application/json"}},request:{},parseResponse:()=>{}}};prepareCachedChatReq=async(e,t,n)=>{let r=e.model,o=e.modelConfig?.stream??this.config.stream,{dynamicContents:i,dynamicSystemInstruction:a}=this.extractDynamicContent(e.chatPrompt),u;if(this.endpointId?u={name:o?`/${this.endpointId}:streamGenerateContent?alt=sse`:`/${this.endpointId}:generateContent`}:u={name:o?`/models/${r}:streamGenerateContent?alt=sse`:`/models/${r}:generateContent`},!this.isVertex){let d=o?"&":"?",m=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;u.name+=`${d}key=${m}`}let l={maxOutputTokens:e.modelConfig?.maxTokens??this.config.maxTokens,temperature:e.modelConfig?.temperature??this.config.temperature,...e.modelConfig?.topP!==void 0?{topP:e.modelConfig.topP}:{},topK:e.modelConfig?.topK??this.config.topK,frequencyPenalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,candidateCount:1,stopSequences:e.modelConfig?.stopSequences??this.config.stopSequences,responseMimeType:"text/plain"};Ft(r)&&(l.temperature===void 0||l.temperature<1)&&(l.temperature=1);let c=this.config.safetySettings,p={contents:i,cachedContent:n,generationConfig:l,safetySettings:c};return a&&(p.systemInstruction=a),{apiConfig:u,request:p}};extractCacheableContent(e){let t,n=[],r=-1;for(let o=e.length-1;o>=0;o--){let i=e[o];if("cache"in i&&i.cache){r=o;break}}for(let o=0;o<e.length;o++){let i=e[o];if(i.role==="system"){t={role:"user",parts:[{text:i.content}]};continue}if(r>=0&&o<=r)if(i.role==="user"){let a=[];if(typeof i.content=="string")a.push({text:i.content});else if(Array.isArray(i.content))for(let u of i.content)switch(u.type){case"text":a.push({text:u.text});break;case"image":a.push({inlineData:{mimeType:u.mimeType,data:u.image}});break;case"audio":a.push({inlineData:{mimeType:`audio/${u.format??"mp3"}`,data:u.data}});break;case"file":"fileUri"in u?a.push({fileData:{mimeType:u.mimeType,fileUri:u.fileUri}}):a.push({inlineData:{mimeType:u.mimeType,data:u.data}});break}a.length>0&&n.push({role:"user",parts:a})}else i.role==="assistant"&&i.content&&n.push({role:"model",parts:[{text:i.content}]})}return{systemInstruction:t,contents:n}}extractDynamicContent(e){let n=[];for(let r of e)if(r.role!=="system"&&!("cache"in r&&r.cache))if(r.role==="user"){let o=[];if(typeof r.content=="string")o.push({text:r.content});else if(Array.isArray(r.content)){for(let i of r.content)if(!("cache"in i&&i.cache))switch(i.type){case"text":o.push({text:i.text});break;case"image":o.push({inlineData:{mimeType:i.mimeType,data:i.image}});break;case"audio":o.push({inlineData:{mimeType:`audio/${i.format??"mp3"}`,data:i.data}});break;case"file":"fileUri"in i?o.push({fileData:{mimeType:i.mimeType,fileUri:i.fileUri}}):o.push({inlineData:{mimeType:i.mimeType,data:i.data}});break}}o.length>0&&n.push({role:"user",parts:o})}else if(r.role==="assistant"){let o=[];if(r.content&&o.push({text:r.content}),r.functionCalls)for(let i of r.functionCalls){let a;if(typeof i.function.params=="string")try{a=JSON.parse(i.function.params)}catch{a={}}else a=i.function.params??{};o.push({functionCall:{name:i.function.name,args:a}})}o.length>0&&n.push({role:"model",parts:o})}else r.role==="function"&&n.push({role:"user",parts:[{functionResponse:{name:r.functionId,response:{result:r.result}}}]});return{dynamicContents:n,dynamicSystemInstruction:void 0}}},en=class s extends Oe{static create(e){return new s(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:i,models:a,modelInfo:u}){let l=t!==void 0&&n!==void 0,c,p;if(l){if(!e)throw new Error("GoogleGemini Vertex API key not set");if(typeof e!="function")throw new Error("GoogleGemini Vertex API key must be a function for token-based authentication");let x;r?x="endpoints":x="publishers/google",c=`https://${n==="global"?"aiplatform":`${n}-aiplatform`}.googleapis.com/v1/projects/${t}/locations/${n}/${x}`,p=async()=>({Authorization:`Bearer ${typeof e=="function"?await e():e}`})}else{if(!e)throw new Error("GoogleGemini AI API key not set");c="https://generativelanguage.googleapis.com/v1beta",p=async()=>({})}let d={...Js(),...o},m=new Vs(d,l,r,e,i);u=[...ao,...u??[]];let g=x=>{let y=We({model:x,modelInfo:u,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:y?.supported?.thinkingBudget??!1,hasShowThoughts:y?.supported?.showThoughts??!1,structuredOutputs:y?.supported?.structuredOutputs??!1,media:{images:{supported:!0,formats:["image/jpeg","image/png","image/gif","image/webp"],maxSize:20*1024*1024,detailLevels:["high","low","auto"]},audio:{supported:!0,formats:["wav","mp3","aac","ogg"],maxDuration:9.5*60},files:{supported:!0,formats:["application/pdf","text/plain","text/csv","text/html","text/xml"],maxSize:2*1024*1024*1024,uploadMethod:"cloud"},urls:{supported:!0,webSearch:!0,contextFetching:!0}},caching:{supported:m.supportsContextCache(x),types:["persistent"]},thinking:y?.supported?.thinkingBudget??!1,multiTurn:!0}},f=a?.map(x=>{let y=x,h=y?.config;if(!h)return x;let A={};h.maxTokens!==void 0&&(A.maxTokens=h.maxTokens),h.temperature!==void 0&&(A.temperature=h.temperature),h.topP!==void 0&&(A.topP=h.topP),h.topK!==void 0&&(A.topK=h.topK),h.presencePenalty!==void 0&&(A.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(A.frequencyPenalty=h.frequencyPenalty),h.stopSequences!==void 0&&(A.stopSequences=h.stopSequences),h.endSequences!==void 0&&(A.endSequences=h.endSequences),h.stream!==void 0&&(A.stream=h.stream),h.n!==void 0&&(A.n=h.n);let b={...y};Object.keys(A).length>0&&(b.modelConfig={...y.modelConfig??{},...A});let I=h.thinking?.thinkingTokenBudget;if(typeof I=="number"){let T=d.thinkingTokenBudgetLevels,R=[["minimal",T?.minimal??200],["low",T?.low??800],["medium",T?.medium??5e3],["high",T?.high??1e4],["highest",T?.highest??24500]],k="minimal",v=Number.POSITIVE_INFINITY;for(let[O,P]of R){let $=Math.abs(I-P);$<v&&(v=$,k=O)}b.thinkingTokenBudget=k}return h.thinking?.includeThoughts!==void 0&&(b.showThoughts=!!h.thinking.includeThoughts),h.thinkingLevelMapping&&(b.thinkingLevelMapping=h.thinkingLevelMapping),h.thinkingTokenBudgetLevels&&(b.thinkingTokenBudgetLevels=h.thinkingTokenBudgetLevels),b});f?m.setModels(f):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:u,defaults:{model:d.model,embedModel:d.embedModel},options:i,supportFor:g,models:f??a})}};var Ap=new ze,tn=class{options;maxTokens;refillRate;currentTokens;lastRefillTime;constructor(e,t,n){this.maxTokens=e,this.refillRate=t,this.currentTokens=e,this.lastRefillTime=Date.now(),this.options=n}refillTokens(){let e=Date.now(),n=(e-this.lastRefillTime)/1e3*this.refillRate;this.currentTokens=Math.min(this.maxTokens,this.currentTokens+n),this.lastRefillTime=e}async waitUntilTokensAvailable(e){if(this.refillTokens(),this.currentTokens>=e){this.currentTokens-=e;return}return this.options?.debug&&console.log(Ap.red(`Rate limiter: Waiting for ${e-this.currentTokens} tokens`)),await new Promise(t=>setTimeout(t,100)),this.waitUntilTokensAvailable(e)}async acquire(e){await this.waitUntilTokensAvailable(e)}};var ur=(r=>(r.Llama3_8B="llama3-8b-8192",r.Llama33_70B="llama-3.3-70b-versatile",r.Mixtral_8x7B="mixtral-8x7b-32768",r.Gemma2_9B="gemma2-9b-it",r))(ur||{});var lo=[{name:"gemma2-9b-it",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.2,completionTokenCostPer1M:.2},{name:"llama-3.3-70b-versatile",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.59,completionTokenCostPer1M:.79},{name:"llama3-8b-8192",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.05,completionTokenCostPer1M:.08},{name:"mixtral-8x7b-32768",currency:"usd",characterIsToken:!0,promptTokenCostPer1M:.24,completionTokenCostPer1M:.24}];var yp=()=>structuredClone({model:"llama-3.3-70b-versatile",...ye()}),nn=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Groq API key not set");let i={...yp(),...t},a={...n,streamingUsage:!1};o=[...lo,...o??[]];let u={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:i,options:a,modelInfo:o,apiURL:"https://api.groq.com/openai/v1",models:r,supportFor:u}),super.setName("Groq"),this.setOptions(a)}setOptions=e=>{let t=this.newRateLimiter(e);super.setOptions({...e,rateLimiter:t})};newRateLimiter=e=>{if(e?.rateLimiter)return e.rateLimiter;let t=e?.tokensPerMinute??4800,n=new tn(t,t/60,{debug:e?.debug});return async(o,i)=>{let a=i.modelUsage?.tokens?.totalTokens||0;return await n.acquire(a),await o()}}};var co=[];var po=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(po||{});var Ys=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...ye()}),gu=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",..._e()}),Qs=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,n:e.n,presencePenalty:e.presencePenalty}}createChatReq=e=>{let t=e.model,n=e.functions?`Functions:
170
170
  ${JSON.stringify(e.functions,null,2)}
171
- `:"",r=e.chatPrompt?.map(l=>{switch(l.role){case"user":return`User: ${l.content}`;case"system":return`System: ${l.content}`;case"function":return`Function Result: ${l.result}`;case"assistant":{let u=l.functionCalls?.map(c=>{let p=typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params);return`${c.function.name}(${p})`}).join(`
172
- `);return u?`Assistant: ${l.content}
171
+ `:"",r=e.chatPrompt?.map(u=>{switch(u.role){case"user":return`User: ${u.content}`;case"system":return`System: ${u.content}`;case"function":return`Function Result: ${u.result}`;case"assistant":{let l=u.functionCalls?.map(c=>{let p=typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params);return`${c.function.name}(${p})`}).join(`
172
+ `);return l?`Assistant: ${u.content}
173
173
  Functions:
174
- ${u}`:`Assistant: ${l.content}`}default:throw new Error("Unknown role")}}).join(`
175
- `),o=`${n} ${r}`.trim(),s={name:"/models"},a={model:t,inputs:o,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[s,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},hn=class extends Be{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...Ys(),...t},s=new Qs(o);super(s,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:go,defaults:{model:o.model},options:n,supportFor:{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r})}};var dr=(l=>(l.Mistral7B="open-mistral-7b",l.Mistral8x7B="open-mixtral-8x7b",l.MistralSmall="mistral-small-latest",l.MistralNemo="mistral-nemo-latest",l.MistralLarge="mistral-large-latest",l.Codestral="codestral-latest",l.OpenCodestralMamba="open-codestral-mamba",l.OpenMistralNemo="open-mistral-nemo-latest",l))(dr||{}),Xs=(e=>(e.MistralEmbed="mistral-embed",e))(Xs||{});var fo=[{name:"open-mistral-7b",currency:"USD",promptTokenCostPer1M:.25,completionTokenCostPer1M:.25},{name:"open-mixtral-8x7b",currency:"USD",promptTokenCostPer1M:.7,completionTokenCostPer1M:.7},{name:"mistral-nemo-latest",currency:"USD",promptTokenCostPer1M:.15,completionTokenCostPer1M:.15},{name:"mistral-small-latest",currency:"USD",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"mistral-large-latest",currency:"USD",promptTokenCostPer1M:2,completionTokenCostPer1M:6},{name:"codestral-latest",currency:"USD",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"open-codestral-mamba",currency:"USD",promptTokenCostPer1M:.25,completionTokenCostPer1M:.25},{name:"open-mistral-nemo-latest",currency:"USD",promptTokenCostPer1M:.3,completionTokenCostPer1M:.3}];var xo=()=>structuredClone({model:"mistral-small-latest",...Te(),topP:1}),ol=()=>structuredClone({...xo(),model:"mistral-large-latest"}),fn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Mistral API key not set");let s={...xo(),...t};o=[...fo,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},l=u=>{let{max_completion_tokens:c,messages:p,...d}=u;return{...d,messages:this.updateMessages(p),max_tokens:c}};super({apiKey:e,config:s,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Mistral")}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let r=n.content.map(o=>typeof o=="object"&&o!==null&&"image_url"in o?{type:"image_url",image_url:{url:o.image_url?.url}}:o);t.push({...n,content:r})}else t.push(n);return t}};var Ao=class{constructor(e={}){this.config=e;this.config.id=this.config.id??Ve()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((r,o)=>r+o,0)/n.length,n.length>0){let r=[...n].sort((a,l)=>a-l),o=Math.max(0,Math.floor(r.length*.95)-1);this.metrics.latency[e].p95=r[o]??t;let s=Math.max(0,Math.floor(r.length*.99)-1);this.metrics.latency[e].p99=r[s]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let r=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=r>0?this.metrics.errors[e].count/r:0}}};var yo=class i{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:o,description:s,isInternal:a}=n;this.services.set(n.key,{service:o,description:s,isInternal:a})}else{let o=n.getModelList();if(!o)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let s of o){if(this.services.has(s.key)){let a=this.services.get(s.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${s.key} as service ${a?.getName()}`)}if("model"in s&&typeof s.model)this.services.set(s.key,{description:s.description,service:n,model:s.model});else if("embedModel"in s&&s.embedModel)this.services.set(s.key,{description:s.description,service:n,embedModel:s.embedModel});else throw new Error(`Key ${s.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new i(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{model:o,...s}=e;return await r.service.chat(s,t)}return await r.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{embedModel:o,...s}=e;return await r.service.embed(s,t)}return await r.service.embed({embedModel:n,...e},t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};var Zs=()=>structuredClone({...Te(),model:"nous-hermes2",embedModel:"all-minilm"}),sl=()=>structuredClone({...je(),model:"nous-hermes2",embedModel:"all-minilm"}),xn=class extends De{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let s={...Zs(),...n};super({apiKey:e,options:r,config:s,apiURL:t,models:o,modelInfo:[],supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}),super.setName("Ollama")}};var Gc=i=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(i),An=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let r of e){if(r.type==="text"){t==="assistant"?n.push({type:"output_text",text:r.text}):n.push({type:"input_text",text:r.text});continue}if(t==="assistant")continue;if(r.type==="image"){let s=`data:${r.mimeType};base64,${r.image}`;n.push({type:"input_image",image_url:{url:s,details:r.details??"auto"}});continue}if(r.type==="audio"){n.push({type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}});continue}let o=r;throw new Error(`Unsupported content part: ${JSON.stringify(o)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let r of e){if(t&&r.role==="system")continue;let o;if(r.role==="system"||r.role==="user"||r.role==="assistant"&&r.content)if(typeof r.content=="string")r.role==="system"?o=r.content:r.role==="assistant"?o=[{type:"output_text",text:r.content}]:o=[{type:"input_text",text:r.content}];else if(Array.isArray(r.content))o=this.mapInternalContentToResponsesInput(r.content,r.role==="assistant"?"assistant":"user");else{if(!(r.role==="assistant"&&!r.content&&r.functionCalls))throw new Error(`Invalid content type for role ${r.role}`);o=""}else r.role,o="";switch(r.role){case"system":n.push({type:"message",role:"system",content:o});break;case"user":n.push({type:"message",role:"user",content:o,name:r.name});break;case"assistant":if(r.content||r.functionCalls){let s={type:"message",role:"assistant",content:""};if(r.content&&(s.content=o),r.name&&(s.name=r.name),r.content&&n.push(s),r.functionCalls)for(let a of r.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:r.functionId,output:r.result});break;default:{let s=r.role;throw new Error(`Invalid role in chat prompt: ${s}`)}}}return n}createChatReq(e,t){let n=e.model,r={name:"/responses"},o=null,s=!1;if(e.chatPrompt){for(let C of e.chatPrompt)if(C.role==="system"&&typeof C.content=="string"){o=C.content,s=!0;break}}let a=o??this.config.systemPrompt??null,l=e.functions?.map(C=>({type:"function",name:C.name,description:C.description,parameters:C.parameters??{}})),u=[],c=Gc(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":case"highest":d="high";break}let m={model:n,input:"",instructions:a,tools:l?.length?l:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...c?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:u.length>0?u:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(m.user=this.config.user),this.config.parallelToolCalls!==void 0&&(m.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?m.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(m.text={format:{type:this.config.responseFormat}}),this.config.seed&&(m.seed=this.config.seed);let g=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,s):[];if(g.length>0)m.input=g;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)m.input=e.chatPrompt[0].content;else if(g.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let h=m.reasoning??{};if(this.config.reasoningEffort&&(h={...h,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":h={};break;case"minimal":h={...h,effort:"minimal"};break;case"low":h={...h,effort:"medium"};break;case"medium":case"high":case"highest":h={...h,effort:"high"};break}Object.keys(h).length>0&&h.effort?m.reasoning=h:m.reasoning=void 0;let f=m;return this.responsesReqUpdater&&(f=this.responsesReqUpdater(f)),[r,f]}createChatResp(e){let{id:t,output:n,usage:r}=e;r&&(this.tokensUsed={promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens??r.output_tokens??0,totalTokens:r.total_tokens});let o={};for(let s of n??[])switch(s.type){case"message":o.id=s.id,o.content=ei(s.content,t),o.finishReason=s.status==="completed"?"stop":"content_filter",o.citations=bo(s.content);break;case"reasoning":o.id=s.id,s.encrypted_content?o.thought=s.encrypted_content:o.thought=s.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
176
- `);break;case"file_search_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"file_search",params:{queries:s.queries,results:s.results}}}],o.finishReason="function_call";break;case"web_search_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"web_search",params:{queries:s.queries}}}],o.finishReason="function_call";break;case"computer_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"computer_use",params:{action:s.action}}}],o.finishReason="function_call";break;case"code_interpreter_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"code_interpreter",params:{code:s.code,results:s.results}}}],o.finishReason="function_call";break;case"image_generation_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"image_generation",params:{result:s.result}}}],o.finishReason="function_call";break;case"local_shell_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"local_shell",params:{action:s.action}}}],o.finishReason="function_call";break;case"mcp_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:"mcp",params:{name:s.name,args:s.args,serverLabel:s.server_label,output:s.output,error:s.error}}}],o.finishReason="function_call";break;case"function_call":o.id=s.id,o.functionCalls=[{id:s.id,type:"function",function:{name:s.name,params:s.arguments}}],o.finishReason="function_call";break}return{results:[{...o,index:0}],remoteId:t}}createChatStreamResp(e){let t=e,n={index:0,id:"",content:"",finishReason:"stop"},r;switch(t.type){case"response.created":case"response.in_progress":case"response.queued":r=t.response.id,n.id=`${t.response.id}_res_0`;break;case"response.output_item.added":switch(t.item.type){case"message":n.id=t.item.id,n.content=ei(t.item.content,t.item.id),n.citations=bo(t.item.content);break;case"function_call":n.id=t.item.id,n.functionCalls=[{id:t.item.id,type:"function",function:{name:t.item.name,params:t.item.arguments}}];break;case"file_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"file_search",params:{queries:o.queries||[],results:o.results?.map(s=>({fileId:s.file_id,filename:s.filename,score:s.score,text:s.text,attributes:s.attributes}))}}}]}break;case"web_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"web_search",params:{queries:o.queries||[]}}}]}break;case"computer_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"computer_use",params:{action:o.action||{}}}}]}break;case"code_interpreter_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"code_interpreter",params:{code:o.code||"",results:o.results}}}]}break;case"image_generation_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"image_generation",params:{result:o.result}}}]}break;case"local_shell_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"local_shell",params:{action:o.action||{}}}}]}break;case"mcp_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"mcp",params:{name:o.name||"",args:o.args||"",serverLabel:o.server_label||"",output:o.output,error:o.error}}}]}break}break;case"response.content_part.added":n.id=t.item_id,n.content=ei([t.part],t.item_id),n.citations=bo([t.part]);break;case"response.output_text.delta":n.id=t.item_id,n.content=t.delta;break;case"response.output_text.done":break;case"response.function_call_arguments.delta":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.delta}}];break;case"response.reasoning_summary_text.delta":n.id=t.item_id,n.thought=t.delta;break;case"response.file_search_call.in_progress":case"response.file_search_call.searching":n.id=t.item_id,n.finishReason="function_call";break;case"response.file_search_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.web_search_call.in_progress":case"response.web_search_call.searching":n.id=t.item_id,n.finishReason="function_call";break;case"response.web_search_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.in_progress":case"response.image_generation_call.generating":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.partial_image":n.id=t.item_id,n.finishReason="function_call";break;case"response.mcp_call.in_progress":n.id=t.item_id,n.finishReason="function_call";break;case"response.mcp_call.arguments.delta":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.delta}}];break;case"response.mcp_call.arguments.done":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.arguments}}];break;case"response.mcp_call.completed":case"response.mcp_call.failed":n.id="mcp_call_event",n.finishReason="function_call";break;case"response.mcp_list_tools.in_progress":case"response.mcp_list_tools.completed":case"response.mcp_list_tools.failed":n.id="mcp_list_tools_event",n.finishReason="function_call";break;case"response.output_item.done":switch(t.item.type){case"message":if(n.id=t.item.id,n.finishReason=t.item.status==="completed"?"stop":"error",!n.citations||n.citations.length===0){let o=bo(t.item.content||[]);o&&(n.citations=o)}break;case"function_call":case"file_search_call":case"web_search_call":case"computer_call":case"code_interpreter_call":case"image_generation_call":case"local_shell_call":case"mcp_call":n.id=t.item.id,n.finishReason="function_call";break}break;case"response.completed":t.response.usage&&(this.tokensUsed={promptTokens:t.response.usage.prompt_tokens,completionTokens:t.response.usage.completion_tokens??t.response.usage.output_tokens??0,totalTokens:t.response.usage.total_tokens}),r=t.response.id,n.id=`${t.response.id}_completed`,n.finishReason="stop";break;case"response.failed":r=t.response.id,n.id=`${t.response.id}_failed`,n.finishReason="error";break;case"response.incomplete":r=t.response.id,n.id=`${t.response.id}_incomplete`,n.finishReason="length";break;case"error":n.id="error",n.content=`Error: ${t.message}`,n.finishReason="error";break;default:n.id="unknown";break}return{results:[n],remoteId:r}}createEmbedReq(e){let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]}},ei=(i,e)=>{let t=i.filter(n=>n.type==="refusal");if(t.length>0){let n=t.map(r=>r.refusal).join(`
177
- `);throw new Me(n,void 0,e)}return i.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
- `)};function bo(i){let e=[];for(let t of i??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var gr=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),il=()=>({...gr(),model:"gpt-4o",temperature:.5}),al=()=>({...gr(),model:"gpt-4o",temperature:.9}),mr=class extends Be{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:s,responsesReqUpdater:a,supportFor:l={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new An(t,n?.streamingUsage??!0,a),c=s?.map(p=>{let d=p,m=d?.config;if(!m)return p;let g={};m.maxTokens!==void 0&&(g.maxTokens=m.maxTokens),m.temperature!==void 0&&(g.temperature=m.temperature),m.topP!==void 0&&(g.topP=m.topP),m.presencePenalty!==void 0&&(g.presencePenalty=m.presencePenalty),m.frequencyPenalty!==void 0&&(g.frequencyPenalty=m.frequencyPenalty);let h=m.stopSequences??m.stop;h!==void 0&&(g.stopSequences=h),m.n!==void 0&&(g.n=m.n),m.stream!==void 0&&(g.stream=m.stream);let f={...d};Object.keys(g).length>0&&(f.modelConfig={...d.modelConfig??{},...g});let C=m?.thinking?.thinkingTokenBudget;if(typeof C=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],I="minimal",w=Number.POSITIVE_INFINITY;for(let[A,T]of x){let k=Math.abs(C-T);k<w&&(w=k,I=A)}f.thinkingTokenBudget=I}return m?.thinking?.includeThoughts!==void 0&&(f.showThoughts=!!m.thinking.includeThoughts),f});super(u,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model??"gpt-4o",embedModel:t.embedModel??"text-embedding-ada-002"},options:n,supportFor:l,models:c??s})}},yn=class extends mr{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...Xr,...o??[]];let s=a=>{let l=at({model:a,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:l?.supported?.thinkingBudget??!1,hasShowThoughts:l?.supported?.showThoughts??!1,structuredOutputs:l?.supported?.structuredOutputs??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}};super({apiKey:e,config:{...gr(),...t},options:n,modelInfo:o,models:r,supportFor:s})}};var ti=()=>structuredClone({model:"openrouter/auto",...Te()}),bn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o,referer:s,title:a}){if(!e||e==="")throw new Error("OpenRouter API key not set");let l={...ti(),...t},u={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=o??[];super({apiKey:e,config:l,options:n,apiURL:"https://openrouter.ai/api/v1",modelInfo:c,models:r,supportFor:u}),super.setName("OpenRouter"),super.setHeaders(async()=>{let p={Authorization:`Bearer ${e}`};return s&&(p["HTTP-Referer"]=s),a&&(p["X-Title"]=a),p})}};async function Io(i,e,t={}){if(typeof i=="string")return[{type:"text",text:i}];if(!Array.isArray(i))return[{type:"text",text:String(i)}];let n=e.getFeatures(),r=[];for(let o of i)try{switch(o.type){case"text":r.push({type:"text",text:o.text});break;case"image":if(n.media.images.supported)o.altText?r.push({type:"text",text:`[Image: ${o.altText}]`}):r.push({type:"text",text:"[Image content]"});else if(o.altText)r.push({type:"text",text:o.altText});else if(t.imageToText)try{let s=await t.imageToText(o.image);r.push({type:"text",text:s})}catch(s){throw new yt(s,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new ut("Images",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)o.transcription?r.push({type:"text",text:o.transcription}):r.push({type:"text",text:"[Audio content]"});else if(o.transcription)r.push({type:"text",text:o.transcription});else if(t.audioToText)try{let s=await t.audioToText(o.data,o.format);r.push({type:"text",text:s})}catch(s){throw new yt(s,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new ut("Audio",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)o.extractedText?r.push({type:"text",text:o.extractedText}):r.push({type:"text",text:`[File: ${o.filename}]`});else if(o.extractedText)r.push({type:"text",text:o.extractedText});else if(t.fileToText)try{let s=await t.fileToText(o.data,o.mimeType);r.push({type:"text",text:s})}catch(s){throw new yt(s,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new ut("Files",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:`[File: ${o.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)o.cachedContent?r.push({type:"text",text:o.cachedContent}):r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`});else if(o.cachedContent)r.push({type:"text",text:o.cachedContent});else if(t.urlToText)try{let s=await t.urlToText(o.url);r.push({type:"text",text:s})}catch(s){throw new yt(s,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new ut("URLs",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`})}break;default:typeof o=="object"&&o.text?r.push({type:"text",text:o.text}):r.push({type:"text",text:String(o)})}}catch(s){throw s instanceof ut||s instanceof yt?s:new yt(s,o.type||"unknown","content processing")}return r}function ll(i){let e=!1,t=!1,n=!1,r=!1;for(let o of i)if(o.role==="user"&&Array.isArray(o.content))for(let s of o.content)switch(s.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":r=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r}}var hr=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(hr||{});var Co=[{name:"reka-core",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"reka-flash",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:2},{name:"reka-edge",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1}];var fr=()=>structuredClone({model:"reka-core",...Te()}),cl=()=>structuredClone({...fr(),model:"reka-core"}),pl=()=>structuredClone({model:"reka-core",...je()}),dl=()=>({...fr(),model:"reka-flash"}),ni=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=e=>{let t=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let n={name:"/chat/completions"},r=Uc(e),o=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,s=e.modelConfig?.stream??this.config.stream,a={model:t,messages:r,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},top_k:e.modelConfig?.n??this.config.n,...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},stop:e.modelConfig?.stopSequences??this.config.stop,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...o?{frequency_penalty:o}:{},...s?{stream:!0}:{}};return[n,a]};createChatResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=ul(s.finish_reason),u;return typeof s.message.content=="string"?u=s.message.content:u=s.message.content.text,{index:a,id:`${t}`,content:u,finishReason:l}}),remoteId:t}};createChatStreamResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((s,a)=>{let l=ul(s.finish_reason),u;return typeof s.chunk.content=="string"?u=s.chunk.content:u=s.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:l}})}}},ul=i=>{switch(i){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function Uc(i){return i.chatPrompt.map(e=>{switch(e.role){case"system":return{role:"user",content:e.content};case"user":return Array.isArray(e.content)?{role:"user",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})}:{role:"user",content:e.content};case"assistant":if(Array.isArray(e.content))return{role:"assistant",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})};if(!e.content)throw new Error("Assistant content is empty");return{role:"user",content:e.content};default:throw new Error("Invalid role")}})}var In=class extends Be{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=Co,models:s}){if(!e||e==="")throw new Error("Reka API key not set");let a={...fr(),...t},l=new ni(a);super(l,{name:"Reka",apiURL:r||"https://api.reka.ai/v1/chat",headers:async()=>({"X-Api-Key":e}),modelInfo:o,defaults:{model:a.model},options:n,supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:s})}};var To=class{providers;processingServices;config;constructor(e){this.providers=[e.providers.primary,...e.providers.alternatives],this.processingServices=e.processing,this.config=e.routing}async chat(e,t={}){let n=await this.selectProviderWithDegradation(e,t.routingOptions||{}),r=await this.preprocessRequest(e,n.provider,t.processingOptions);try{return{response:await n.provider.chat(r,t),routing:n}}catch(o){if(o instanceof ut&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw o}}async preprocessRequest(e,t,n){let r={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},o=[];for(let s of e.chatPrompt)if(s.role==="user"&&Array.isArray(s.content)){let a=await Io(s.content,t,r);a.every(u=>u.type==="text")&&a.length===1?o.push({...s,content:a[0].text}):o.push({...s,content:a.map(u=>({type:"text",text:u.text}))})}else o.push(s);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=qt(e),r=[],o=[],s=[];try{let a=oo(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),l=a.getFeatures();return n.hasImages&&!l.media.images.supported&&(o.push("Images will be converted to text descriptions"),r.push("Image-to-text conversion")),n.hasAudio&&!l.media.audio.supported&&(o.push("Audio will be transcribed to text"),r.push("Audio-to-text transcription")),n.hasFiles&&!l.media.files.supported&&(o.push("File content will be extracted to text"),r.push("File-to-text extraction")),n.hasUrls&&!l.media.urls.supported&&(o.push("URL content will be pre-fetched"),r.push("URL content fetching")),n.requiresStreaming&&!l.streaming&&s.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!l.caching.supported&&s.push("Content caching not supported"),{provider:a,processingApplied:r,degradations:o,warnings:s}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let r of t)try{let o={provider:r,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},s=await this.preprocessRequest(e,r,{fallbackBehavior:"degrade"});return{response:await r.chat(s,n),routing:o}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=qt(e),n=[],r=[];try{let o=await this.selectProviderWithDegradation(e,{});return o.degradations.length>0&&(n.push(...o.degradations),r.push("Consider using a provider that natively supports all media types")),o.warnings.length>0&&n.push(...o.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(n.push("No image processing service available and no providers support images"),r.push("Add imageToText processing service or use image-capable provider"))),t.hasAudio&&this.processingServices.audioToText===void 0&&(this.providers.some(a=>a.getFeatures().media.audio.supported)||(n.push("No audio processing service available and no providers support audio"),r.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:n.length===0||o.degradations.length>0,issues:n,recommendations:r}}catch(o){return{canHandle:!1,issues:[`Cannot route request: ${o instanceof Error?o.message:"Unknown error"}`],recommendations:["Add more providers or processing services to handle this request"]}}}getRoutingStats(){let e={};for(let t of this.providers){let n=t.getFeatures(),r=t.getName();n.functions&&(e.Functions=e.Functions||[],e.Functions.push(r)),n.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(r)),n.media.images.supported&&(e.Images=e.Images||[],e.Images.push(r)),n.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(r)),n.media.files.supported&&(e.Files=e.Files||[],e.Files.push(r)),n.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(r)),n.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(r))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var xr=(x=>(x.KimiK25="moonshotai/Kimi-K2.5",x.KimiK2Instruct0905="moonshotai/Kimi-K2-Instruct-0905",x.KimiK2Thinking="moonshotai/Kimi-K2-Thinking",x.DeepSeekV31="deepseek-ai/DeepSeek-V3.1",x.DeepSeekR1="deepseek-ai/DeepSeek-R1",x.GPTOSS120B="openai/gpt-oss-120b",x.GPTOSS20B="openai/gpt-oss-20b",x.Qwen35_397B="Qwen/Qwen3.5-397B-A17B",x.Qwen3CoderNext="Qwen/Qwen3-Coder-Next-FP8",x.Qwen3Coder480B="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",x.Qwen3_235BInstruct2507="Qwen/Qwen3-235B-A22B-Instruct-2507-tput",x.Qwen3_235BThinking2507="Qwen/Qwen3-235B-A22B-Thinking-2507",x.Qwen3Next80BInstruct="Qwen/Qwen3-Next-80B-A3B-Instruct",x.Qwen3Next80BThinking="Qwen/Qwen3-Next-80B-A3B-Thinking",x.GLM5="zai-org/GLM-5",x.GLM47="zai-org/GLM-4.7",x.Llama4Maverick="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",x.Llama33_70B="meta-llama/Llama-3.3-70B-Instruct-Turbo",x))(xr||{});var wo=[{name:"moonshotai/Kimi-K2.5",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:2.8},{name:"moonshotai/Kimi-K2-Instruct-0905",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:3},{name:"moonshotai/Kimi-K2-Thinking",currency:"usd",promptTokenCostPer1M:1.2,completionTokenCostPer1M:4},{name:"deepseek-ai/DeepSeek-V3.1",currency:"usd",promptTokenCostPer1M:.6,completionTokenCostPer1M:1.7},{name:"deepseek-ai/DeepSeek-R1",currency:"usd",promptTokenCostPer1M:1.4,completionTokenCostPer1M:2.2},{name:"openai/gpt-oss-120b",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"openai/gpt-oss-20b",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.2},{name:"Qwen/Qwen3.5-397B-A17B",currency:"usd",promptTokenCostPer1M:.6,completionTokenCostPer1M:3.6},{name:"Qwen/Qwen3-Coder-Next-FP8",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.2},{name:"Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:2},{name:"Qwen/Qwen3-235B-A22B-Instruct-2507-tput",currency:"usd",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"Qwen/Qwen3-235B-A22B-Thinking-2507",currency:"usd",promptTokenCostPer1M:.65,completionTokenCostPer1M:3},{name:"Qwen/Qwen3-Next-80B-A3B-Instruct",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:1.5},{name:"Qwen/Qwen3-Next-80B-A3B-Thinking",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:1.5},{name:"zai-org/GLM-5",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:3.2},{name:"zai-org/GLM-4.7",currency:"usd",promptTokenCostPer1M:.45,completionTokenCostPer1M:2},{name:"meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",currency:"usd",promptTokenCostPer1M:.27,completionTokenCostPer1M:.85},{name:"meta-llama/Llama-3.3-70B-Instruct-Turbo",currency:"usd",promptTokenCostPer1M:.88,completionTokenCostPer1M:.88}];var ri=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...Te()}),Cn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Together API key not set");let s={...ri(),...t};o=[...wo,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:s,options:n,apiURL:"https://api.together.xyz/v1",modelInfo:o,models:r,supportFor:a}),super.setName("Together")}};var Ar=(d=>(d.Llama31_8B_Instruct="Llama-3.1-8B-Instruct-q4f32_1-MLC",d.Llama31_70B_Instruct="Llama-3.1-70B-Instruct-q4f16_1-MLC",d.Llama32_1B_Instruct="Llama-3.2-1B-Instruct-q4f32_1-MLC",d.Llama32_3B_Instruct="Llama-3.2-3B-Instruct-q4f32_1-MLC",d.Mistral7B_Instruct="Mistral-7B-Instruct-v0.3-q4f32_1-MLC",d.Phi35_Mini_Instruct="Phi-3.5-mini-instruct-q4f32_1-MLC",d.Gemma2_2B_Instruct="gemma-2-2b-it-q4f32_1-MLC",d.Gemma2_9B_Instruct="gemma-2-9b-it-q4f32_1-MLC",d.Qwen2_5_0_5B_Instruct="Qwen2.5-0.5B-Instruct-q4f32_1-MLC",d.Qwen2_5_1_5B_Instruct="Qwen2.5-1.5B-Instruct-q4f32_1-MLC",d.Qwen2_5_3B_Instruct="Qwen2.5-3B-Instruct-q4f32_1-MLC",d.Qwen2_5_7B_Instruct="Qwen2.5-7B-Instruct-q4f32_1-MLC",d))(Ar||{});var Ro=[{name:"Llama-3.1-8B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"Llama-3.1-70B-Instruct-q4f16_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096,isExpensive:!0},{name:"Llama-3.2-1B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Llama-3.2-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Mistral-7B-Instruct-v0.3-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096},{name:"Phi-3.5-mini-instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"gemma-2-2b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"gemma-2-9b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"Qwen2.5-0.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-1.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-7B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096}];var si=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...Te()}),ml=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...je()}),oi=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.map(a=>{if(a.role==="function")return{role:"function",name:a.functionId,content:typeof a.result=="string"?a.result:JSON.stringify(a.result)};let l="";typeof a.content=="string"?l=a.content:Array.isArray(a.content)&&(l=a.content.filter(c=>c.type==="text").map(c=>c.text).join(`
179
- `));let u={role:a.role,content:l};return a.role==="assistant"&&a.functionCalls?.length?{...u,tool_calls:a.functionCalls.map(c=>({id:c.id,type:"function",function:{name:c.function.name,arguments:typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params||{})}}))}:u}),r=e.functions?.map(a=>({type:"function",function:{name:a.name,description:a.description,parameters:a.parameters||{type:"object",properties:{}}}})),o={name:"/chat/completions",localCall:async(a,l)=>{try{let u=await this.engine.chat.completions.create({...a,stream:l||!1});return l?new ReadableStream({async start(c){try{for await(let p of u)c.enqueue(p);c.close()}catch(p){c.error(p)}}}):u}catch(u){throw new Error(`WebLLM API error: ${u}`)}}},s={model:t,messages:n,...r?.length?{tools:r}:{},max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,stop:e.modelConfig?.stopSequences??this.config.stopSequences,stream:e.modelConfig?.stream??this.config.stream,n:e.modelConfig?.n??this.config.n};return[o,s]}createEmbedReq=e=>{throw new Error("WebLLM does not support embeddings")};createChatResp=e=>(this.tokensUsed={promptTokens:e.usage?.prompt_tokens??0,completionTokens:e.usage?.completion_tokens??0,totalTokens:e.usage?.total_tokens??0},{results:e.choices.map((n,r)=>{let o="stop";switch(n.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.message.tool_calls?.map(a=>({id:a.id,type:"function",function:{name:a.function.name,params:a.function.arguments}}));return{index:r,id:e.id,content:n.message.content||"",functionCalls:s,finishReason:o}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let n=t,r=e.choices[0];if(!r)throw new Error("No choice in WebLLM stream response");if(r.delta.content&&(n.content=(n.content||"")+r.delta.content),r.delta.tool_calls){n.toolCalls||(n.toolCalls=[]);for(let l of r.delta.tool_calls){let u=n.toolCalls[l.index];u?l.function?.arguments&&(u.function.arguments=(u.function?.arguments||"")+l.function.arguments):n.toolCalls[l.index]={id:l.id,type:l.type,function:{name:l.function?.name,arguments:l.function?.arguments||""}}}}e.usage&&(this.tokensUsed={promptTokens:e.usage.prompt_tokens,completionTokens:e.usage.completion_tokens,totalTokens:e.usage.total_tokens});let o;if(r.finish_reason)switch(r.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let s=n.toolCalls?.map(l=>({id:l.id||"",type:"function",function:{name:l.function?.name||"",params:l.function?.arguments||""}}));return{results:[{index:0,id:e.id,content:n.content||"",functionCalls:s,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},Tn=class extends Be{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...si(),...t},s=new oi(o,e);super(s,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:Ro,defaults:{model:o.model},supportFor:a=>({functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}),options:n,models:r})}};var yr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(yr||{}),ii=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(ii||{});var So=[{name:"grok-3",currency:"USD",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"grok-3-mini",currency:"USD",promptTokenCostPer1M:.3,completionTokenCostPer1M:.5,supported:{thinkingBudget:!0}},{name:"grok-3-fast",currency:"USD",promptTokenCostPer1M:5,completionTokenCostPer1M:25},{name:"grok-3-mini-fast",currency:"USD",promptTokenCostPer1M:.6,completionTokenCostPer1M:4,supported:{thinkingBudget:!0}}];var ko=()=>structuredClone({model:"grok-3-mini",...Te()}),gl=()=>structuredClone({...ko(),model:"grok-3"}),wn=class extends De{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Grok API key not set");let s={...ko(),...t};o=[...So,...o??[]];let a=u=>{let c=at({model:u,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}},l=u=>{if(n?.searchParameters){let c=n.searchParameters;return{...u,search_parameters:{mode:c.mode,return_citations:c.returnCitations,from_date:c.fromDate,to_date:c.toDate,max_search_results:c.maxSearchResults,sources:c.sources?.map(p=>({type:p.type,country:p.country,excluded_websites:p.excludedWebsites,allowed_websites:p.allowedWebsites,safe_search:p.safeSearch,x_handles:p.xHandles,links:p.links}))}}}return u};super({apiKey:e,config:s,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Grok")}};function hl(i){return br.create(i)}var br=class i{ai;static create(e){return new i(e)}constructor(e){switch(e.name){case"openai":this.ai=new ln(e);break;case"openai-responses":this.ai=new yn(e);break;case"azure-openai":this.ai=new un(e);break;case"grok":this.ai=new wn(e);break;case"huggingface":this.ai=new hn(e);break;case"groq":this.ai=new gn(e);break;case"together":this.ai=new Cn(e);break;case"openrouter":this.ai=new bn(e);break;case"cohere":this.ai=new cn(e);break;case"google-gemini":this.ai=new dn(e);break;case"anthropic":this.ai=new rn(e);break;case"mistral":this.ai=new fn(e);break;case"deepseek":this.ai=new pn(e);break;case"ollama":this.ai=new xn(e);break;case"reka":this.ai=new In(e);break;case"webllm":this.ai=new Tn(e);break;default:throw new Error("Unknown AI")}}getName(){return this.ai.getName()}getId(){return this.ai.getId()}getFeatures(e){return this.ai.getFeatures(e)}getModelList(){return this.ai.getModelList()}getLastUsedChatModel(){return this.ai.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.ai.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.ai.getLastUsedModelConfig()}getMetrics(){return this.ai.getMetrics()}async chat(e,t){return await this.ai.chat(e,t)}async embed(e,t){return await this.ai.embed(e,t)}setOptions(e){this.ai.setOptions(e)}getOptions(){return this.ai.getOptions()}getLogger(){return this.ai.getLogger()}};var vo=require("@opentelemetry/api");var ct=class{name;fetch;tracer;_upsert;_batchUpsert;_query;constructor({name:e,fetch:t,tracer:n}){this.name=e,this.fetch=t,this.tracer=n}async upsert(e,t){if(!this._upsert)throw new Error("upsert() not implemented");return this.tracer?await this.tracer.startActiveSpan("DB Upsert Request",{kind:vo.SpanKind.SERVER,attributes:{[he.DB_SYSTEM]:this.name,[he.DB_OPERATION_NAME]:"upsert",[he.DB_TABLE]:e.table,[he.DB_NAMESPACE]:e.namespace,[he.DB_OPERATION_NAME]:t?"update":"insert"}},async n=>{try{return await this._upsert(e,t,{span:n})}finally{n.end()}}):await this._upsert(e,t)}async batchUpsert(e,t){if(!this._batchUpsert)throw new Error("batchUpsert() not implemented");if(e.length===0)throw new Error("Batch request is empty");if(!e[0])throw new Error("Batch request is invalid first element is undefined");return this.tracer?await this.tracer.startActiveSpan("DB Batch Upsert Request",{kind:vo.SpanKind.SERVER,attributes:{[he.DB_SYSTEM]:this.name,[he.DB_OPERATION_NAME]:"upsert",[he.DB_TABLE]:e[0].table,[he.DB_NAMESPACE]:e[0].namespace,[he.DB_OPERATION_NAME]:t?"update":"insert"}},async n=>{try{return await this._batchUpsert(e,t,{span:n})}finally{n.end()}}):await this._batchUpsert(e,t)}async query(e){if(!this._query)throw new Error("query() not implemented");return this.tracer?await this.tracer.startActiveSpan("DB Query Request",{kind:vo.SpanKind.SERVER,attributes:{[he.DB_SYSTEM]:this.name,[he.DB_OPERATION_NAME]:"upsert",[he.DB_TABLE]:e.table,[he.DB_NAMESPACE]:e.namespace,[he.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var ai="https://api.cloudflare.com/client/v4/accounts/",Rn=class extends ct{apiKey;accountId;constructor({apiKey:e,accountId:t,fetch:n,tracer:r}){if(!e||!t)throw new Error("Cloudflare credentials not set");super({name:"Cloudflare",fetch:n,tracer:r}),this.apiKey=e,this.accountId=t}_upsert=async(e,t,n)=>{let r=await Qe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/upsert`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},{id:e.id,values:e.values,namespace:e.namespace,metadata:e.metadata});if(r.errors)throw new Error(`Cloudflare upsert failed: ${r.errors.map(({message:o})=>o).join(", ")}`);return{ids:r.result.ids}};batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length<1)throw new Error("Batch request is empty");if(!e[0]||!e[0].table)throw new Error("Table name is empty");let r=e[0].table,o=await Qe({url:new URL(`${this.accountId}/vectorize/indexes/${r}/upsert`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},e.map(s=>({id:s.id,values:s.values,namespace:s.namespace,metadata:s.metadata})));if(o.errors)throw new Error(`Cloudflare batch upsert failed: ${o.errors.map(({message:s})=>s).join(", ")}`);return{ids:o.result.ids}};query=async(e,t)=>{let n=await Qe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:t?.span},{vector:e.values,topK:e.limit||10,returnValues:!0});if(n.errors)throw new Error(`Cloudflare query failed: ${n.errors.map(({message:o})=>o).join(", ")}`);return{matches:n.result.matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,values:a,metadata:l}))}}};var Nt=class extends ct{state;constructor({tracer:e}={}){super({name:"Memory",tracer:e}),this.state={}}_upsert=async(e,t,n)=>{if(!this.state[e.table])this.state[e.table]={[e.id]:e};else{let r=this.state[e.table];if(!r)throw new Error(`Table not found: ${e.table}`);r[e.id]=e}return{ids:[e.id]}};_batchUpsert=async(e,t,n)=>{let r=[];for(let o of e){let s=await this.upsert(o,t);r.push(...s.ids)}return{ids:r}};_query=async(e,t)=>{let n=this.state[e.table];if(!n)return{matches:[]};let r=[];return Object.entries(n).forEach(([o,s])=>{if(e.values&&s.values){let a=Bc(e.values,s.values);r.push({id:o,score:a,metadata:s.metadata})}}),r.sort((o,s)=>o.score-s.score),e.limit&&(r.length=e.limit),{matches:r}};getDB=()=>structuredClone(this.state);setDB=e=>{this.state=structuredClone(e)};clearDB=()=>{this.state={}}},Bc=(i,e)=>{if(i.length!==e.length)throw new Error("Vectors must be of the same length.");let t=0,n=0,r=0,o=!0,s=!0,a=new Float64Array(i),l=new Float64Array(e);for(let d=0;d<a.length;d++)t+=a[d]*l[d],n+=a[d]*a[d],r+=l[d]*l[d],a[d]!==0&&(o=!1),l[d]!==0&&(s=!1);if(o||s)return 1;let u=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(u*c)};var jc=i=>({namespace:i.namespace,topK:i.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:i.values??[],id:i.id}),Sn=class extends ct{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Pinecone API key not set");super({name:"Pinecone",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>(await this._batchUpsert([e],t,n),{ids:[e.id]});_batchUpsert=async(e,t,n)=>{if(e.length===0)throw new Error("Batch request is empty");return await Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/vectors/upsert",fetch:this.fetch,span:n?.span},e.map(({id:r,values:o=[],metadata:s})=>({id:r,values:o,metadata:s}))),{ids:e.map(({id:r})=>r)}};query=async(e,t)=>{if(e.text)throw new Error("Pinecone does not support text");return{matches:(await Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},jc(e))).matches.map(({id:o,score:s,values:a,metadata:l})=>({id:o,score:s,metadata:l,values:a}))}}};var kn=class extends ct{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Weaviate API key not set");super({name:"Weaviate",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>{let r=await Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:`/v1/objects/${e.table}/${e.id}`,put:!!t,fetch:this.fetch,span:n?.span},{id:e.id,class:e.table,tenant:e.namespace,vector:e.values,properties:e.metadata??{}});if(r?.result?.errors)throw new Error(`Weaviate upsert failed: ${r.result.errors.error.map(({message:o})=>o).join(", ")}`);return{ids:[r.id]}};_batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length===0)throw new Error("Batch request is empty");let r=e.map(s=>({id:s.id,class:s.table,tenant:s.namespace,vector:s.values,properties:s.metadata??{}})),o=await Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/batch/objects",fetch:this.fetch,span:n?.span},{objects:r});if(o?.some(({result:s})=>s?.errors))throw new Error(`Weaviate batch upsert failed: ${o.map(({result:s})=>s?.errors?.error.map(({message:a})=>a).join(", ")).join(", ")}`);return{ids:o.map(({id:s})=>s)}};_query=async(e,t)=>{let n="";if(e.columns&&e.columns.length===0)throw new Error("Weaviate requires at least one column");if(e.values)n=`nearVector: {
174
+ ${l}`:`Assistant: ${u.content}`}default:throw new Error("Unknown role")}}).join(`
175
+ `),o=`${n} ${r}`.trim(),i={name:"/models"},a={model:t,inputs:o,parameters:{max_new_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,repetition_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},top_k:e.modelConfig?.topK??this.config.topK,return_full_text:this.config.returnFullText,num_return_sequences:this.config.n,do_sample:this.config.doSample,max_time:this.config.maxTime},options:{use_cache:this.config.useCache,wait_for_model:this.config.waitForModel}};return[i,a]};createChatResp=e=>({results:[{index:0,content:e.generated_text}]})},rn=class extends Oe{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...Ys(),...t},i=new Qs(o);super(i,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:co,defaults:{model:o.model},options:n,supportFor:{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:r})}};var lr=(u=>(u.Mistral7B="open-mistral-7b",u.Mistral8x7B="open-mixtral-8x7b",u.MistralSmall="mistral-small-latest",u.MistralNemo="mistral-nemo-latest",u.MistralLarge="mistral-large-latest",u.Codestral="codestral-latest",u.OpenCodestralMamba="open-codestral-mamba",u.OpenMistralNemo="open-mistral-nemo-latest",u))(lr||{}),Xs=(e=>(e.MistralEmbed="mistral-embed",e))(Xs||{});var mo=[{name:"open-mistral-7b",currency:"USD",promptTokenCostPer1M:.25,completionTokenCostPer1M:.25},{name:"open-mixtral-8x7b",currency:"USD",promptTokenCostPer1M:.7,completionTokenCostPer1M:.7},{name:"mistral-nemo-latest",currency:"USD",promptTokenCostPer1M:.15,completionTokenCostPer1M:.15},{name:"mistral-small-latest",currency:"USD",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"mistral-large-latest",currency:"USD",promptTokenCostPer1M:2,completionTokenCostPer1M:6},{name:"codestral-latest",currency:"USD",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"open-codestral-mamba",currency:"USD",promptTokenCostPer1M:.25,completionTokenCostPer1M:.25},{name:"open-mistral-nemo-latest",currency:"USD",promptTokenCostPer1M:.3,completionTokenCostPer1M:.3}];var go=()=>structuredClone({model:"mistral-small-latest",...ye(),topP:1}),fu=()=>structuredClone({...go(),model:"mistral-large-latest"}),on=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Mistral API key not set");let i={...go(),...t};o=[...mo,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},u=l=>{let{max_completion_tokens:c,messages:p,...d}=l;return{...d,messages:this.updateMessages(p),max_tokens:c}};super({apiKey:e,config:i,options:n,apiURL:"https://api.mistral.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:u}),super.setName("Mistral")}updateMessages(e){let t=[];if(!Array.isArray(e))return e;for(let n of e)if(n.role==="user"&&Array.isArray(n.content)){let r=n.content.map(o=>typeof o=="object"&&o!==null&&"image_url"in o?{type:"image_url",image_url:{url:o.image_url?.url}}:o);t.push({...n,content:r})}else t.push(n);return t}};var fo=class{constructor(e={}){this.config=e;this.config.id=this.config.id??Ge()}metrics={latency:{chat:{mean:0,p95:0,p99:0,samples:[]},embed:{mean:0,p95:0,p99:0,samples:[]}},errors:{chat:{count:0,rate:0,total:0},embed:{count:0,rate:0,total:0}}};getLastUsedChatModel(){return this.config.modelInfo?.name??"mock-model"}getLastUsedEmbedModel(){return this.config.embedModelInfo?.name??"mock-embed-model"}getLastUsedModelConfig(){return this.config.modelInfo?{maxTokens:this.config.modelInfo.maxTokens,temperature:.7,stream:this.config.features?.streaming??!1}:void 0}getName(){return this.config.name??"mock-ai-service"}getId(){return this.config.id??"mock-ai-service-id"}getFeatures(e){let t=this.config.features?.media;return{functions:this.config.features?.functions??!1,streaming:this.config.features?.streaming??!1,structuredOutputs:this.config.features?.structuredOutputs??!1,media:{images:{supported:t?.images?.supported??!1,formats:t?.images?.formats??[]},audio:{supported:t?.audio?.supported??!1,formats:t?.audio?.formats??[]},files:{supported:t?.files?.supported??!1,formats:t?.files?.formats??[],uploadMethod:t?.files?.uploadMethod??"none"},urls:{supported:t?.urls?.supported??!1,webSearch:t?.urls?.webSearch??!1,contextFetching:t?.urls?.contextFetching??!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getModelList(){return this.config.models}getMetrics(){return this.metrics}async chat(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock chat error");return this.updateMetrics("chat"),typeof this.config.chatResponse=="function"?await this.config.chatResponse(e):this.config.chatResponse??{results:[{index:0,content:"Mock response",finishReason:"stop"}],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:10,completionTokens:5,totalTokens:15}}}}async embed(e,t){if(this.config.latencyMs&&await new Promise(n=>setTimeout(n,this.config.latencyMs)),this.config.shouldError)throw new Error(this.config.errorMessage??"Mock embed error");return this.updateMetrics("embed"),typeof this.config.embedResponse=="function"?this.config.embedResponse(e):this.config.embedResponse??{embeddings:[[.1,.2,.3]],modelUsage:{ai:this.getName(),model:"mock-model",tokens:{promptTokens:5,completionTokens:0,totalTokens:5}}}}setOptions(e){this.config.options=e}getOptions(){return this.config.options??{}}getLogger(){return this.config.options?.logger??(e=>{console.log(e)})}updateMetrics(e){let t=this.config.latencyMs??0;this.metrics.latency[e].samples.push(t);let n=this.metrics.latency[e].samples;if(this.metrics.latency[e].mean=n.reduce((r,o)=>r+o,0)/n.length,n.length>0){let r=[...n].sort((a,u)=>a-u),o=Math.max(0,Math.floor(r.length*.95)-1);this.metrics.latency[e].p95=r[o]??t;let i=Math.max(0,Math.floor(r.length*.99)-1);this.metrics.latency[e].p99=r[i]??t}if(this.config.shouldError){this.metrics.errors[e].count++,this.metrics.errors[e].total++;let r=this.metrics.latency[e].samples.length;this.metrics.errors[e].rate=r>0?this.metrics.errors[e].count/r:0}}};var ho=class s{options;lastUsedService;services=new Map;constructor(e){if(e.length===0)throw new Error("No AI services provided.");for(let[t,n]of e.entries())if("key"in n){if(this.services.has(n.key))throw new Error(`Duplicate model key: ${n.key}`);let{service:o,description:i,isInternal:a}=n;this.services.set(n.key,{service:o,description:i,isInternal:a})}else{let o=n.getModelList();if(!o)throw new Error(`Service ${t} \`${n.getName()}\` has no model list.`);for(let i of o){if(this.services.has(i.key)){let a=this.services.get(i.key)?.service;throw new Error(`Service ${t} \`${n.getName()}\` has duplicate model key: ${i.key} as service ${a?.getName()}`)}if("model"in i&&typeof i.model)this.services.set(i.key,{description:i.description,service:n,model:i.model});else if("embedModel"in i&&i.embedModel)this.services.set(i.key,{description:i.description,service:n,embedModel:i.embedModel});else throw new Error(`Key ${i.key} in model list for service ${t} \`${n.getName()}\` is missing a model or embedModel property.`)}}}static create(e){return new s(e)}getLastUsedChatModel(){return this.lastUsedService?.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.lastUsedService?.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.lastUsedService?.getLastUsedModelConfig()}async chat(e,t){let n=e.model;if(!n)throw new Error("Model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{model:o,...i}=e;return await r.service.chat(i,t)}return await r.service.chat({model:n,...e},t)}async embed(e,t){let n=e.embedModel;if(!n)throw new Error("Embed model key must be specified for multi-service");let r=this.services.get(n);if(!r)throw new Error(`No service found for embed model key: ${n}`);if(this.lastUsedService=r.service,!r.model){let{embedModel:o,...i}=e;return await r.service.embed(i,t)}return await r.service.embed({embedModel:n,...e},t)}getId(){return`MultiServiceRouter:${Array.from(this.services.values()).map(e=>e.service.getId()).join(",")}`}getName(){return"MultiServiceRouter"}getModelList(){return Array.from(this.services).filter(([,e])=>!e.isInternal).map(([e,t])=>{if(t.model)return{key:e,description:t.description,model:t.model};if(t.embedModel)return{key:e,description:t.description,embedModel:t.embedModel};throw new Error(`Service ${e} has no model or embedModel`)})}getFeatures(e){if(e){let t=this.services.get(e);if(t)return t.service.getFeatures(e)}return{functions:!1,streaming:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}getMetrics(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e="service"in t?t.service:t)}if(!e)throw new Error("No service available to get metrics.");return e.getMetrics()}setOptions(e){for(let t of this.services.values())t.service.setOptions(e);this.options=e}getOptions(){return this.options??{}}getLogger(){let e=this.lastUsedService;if(!e){let t=this.services.values().next().value;t&&(e=t.service)}if(!e)throw new Error("No service available to get logger.");return e.getLogger()}setServiceEntry(e,t){this.services.set(e,t)}};var Zs=()=>structuredClone({...ye(),model:"nous-hermes2",embedModel:"all-minilm"}),hu=()=>structuredClone({..._e(),model:"nous-hermes2",embedModel:"all-minilm"}),sn=class extends Se{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let i={...Zs(),...n};super({apiKey:e,options:r,config:i,apiURL:t,models:o,modelInfo:[],supportFor:{functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}),super.setName("Ollama")}};var bp=s=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(s),an=class{constructor(e,t,n){this.config=e;this.streamingUsage=t;this.responsesReqUpdater=n}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,stopSequences:e.stopSequences,topP:e.topP,stream:e.stream}}mapInternalContentToResponsesInput(e,t){let n=[];for(let r of e){if(r.type==="text"){t==="assistant"?n.push({type:"output_text",text:r.text}):n.push({type:"input_text",text:r.text});continue}if(t==="assistant")continue;if(r.type==="image"){let i=`data:${r.mimeType};base64,${r.image}`;n.push({type:"input_image",image_url:{url:i,details:r.details??"auto"}});continue}if(r.type==="audio"){n.push({type:"input_audio",input_audio:{data:r.data,format:r.format==="wav"?"wav":void 0}});continue}let o=r;throw new Error(`Unsupported content part: ${JSON.stringify(o)}`)}return n}createResponsesReqInternalInput(e,t=!1){let n=[];for(let r of e){if(t&&r.role==="system")continue;let o;if(r.role==="system"||r.role==="user"||r.role==="assistant"&&r.content)if(typeof r.content=="string")r.role==="system"?o=r.content:r.role==="assistant"?o=[{type:"output_text",text:r.content}]:o=[{type:"input_text",text:r.content}];else if(Array.isArray(r.content))o=this.mapInternalContentToResponsesInput(r.content,r.role==="assistant"?"assistant":"user");else{if(!(r.role==="assistant"&&!r.content&&r.functionCalls))throw new Error(`Invalid content type for role ${r.role}`);o=""}else r.role,o="";switch(r.role){case"system":n.push({type:"message",role:"system",content:o});break;case"user":n.push({type:"message",role:"user",content:o,name:r.name});break;case"assistant":if(r.content||r.functionCalls){let i={type:"message",role:"assistant",content:""};if(r.content&&(i.content=o),r.name&&(i.name=r.name),r.content&&n.push(i),r.functionCalls)for(let a of r.functionCalls)n.push({type:"function_call",call_id:a.id,name:a.function.name,arguments:typeof a.function.params=="object"?JSON.stringify(a.function.params):a.function.params||""})}break;case"function":n.push({type:"function_call_output",call_id:r.functionId,output:r.result});break;default:{let i=r.role;throw new Error(`Invalid role in chat prompt: ${i}`)}}}return n}createChatReq(e,t){let n=e.model,r={name:"/responses"},o=null,i=!1;if(e.chatPrompt){for(let y of e.chatPrompt)if(y.role==="system"&&typeof y.content=="string"){o=y.content,i=!0;break}}let a=o??this.config.systemPrompt??null,u=e.functions?.map(y=>({type:"function",name:y.name,description:y.description,parameters:y.parameters??{}})),l=[],c=bp(n),p=this.config.reasoningSummary;t?.showThoughts?p||(p="auto"):p=void 0;let d=this.config.reasoningEffort;if(t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":d=void 0;break;case"minimal":d="minimal";break;case"low":d="medium";break;case"medium":case"high":case"highest":d="high";break}let m={model:n,input:"",instructions:a,tools:u?.length?u:void 0,tool_choice:e.functionCall==="none"||e.functionCall==="auto"||e.functionCall==="required"?e.functionCall:typeof e.functionCall=="object"&&e.functionCall.function?{type:"function",name:e.functionCall.function.name}:void 0,...c?{max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0}:{...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty??void 0,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty??void 0,max_output_tokens:e.modelConfig?.maxTokens??this.config.maxTokens??void 0},stream:e.modelConfig?.stream??this.config.stream??!1,background:void 0,include:l.length>0?l:void 0,metadata:void 0,parallel_tool_calls:this.config.parallelToolCalls,previous_response_id:void 0,...d?{reasoning:{effort:d,summary:p}}:{},service_tier:this.config.serviceTier,store:this.config.store,text:void 0,truncation:void 0,user:this.config.user,seed:this.config.seed};this.config.user&&(m.user=this.config.user),this.config.parallelToolCalls!==void 0&&(m.parallel_tool_calls=this.config.parallelToolCalls),e.responseFormat?m.text={format:e.responseFormat.type==="json_schema"?{type:"json_schema",json_schema:e.responseFormat.schema}:{type:e.responseFormat.type}}:this.config.responseFormat&&(m.text={format:{type:this.config.responseFormat}}),this.config.seed&&(m.seed=this.config.seed);let g=e.chatPrompt?this.createResponsesReqInternalInput(e.chatPrompt,i):[];if(g.length>0)m.input=g;else if(e.chatPrompt&&e.chatPrompt.length===1&&e.chatPrompt[0]?.role==="user"&&e.chatPrompt[0]?.content&&typeof e.chatPrompt[0].content=="string"&&!a)m.input=e.chatPrompt[0].content;else if(g.length===0&&!a)throw new Error("Responses API request must have input or instructions.");let f=m.reasoning??{};if(this.config.reasoningEffort&&(f={...f,effort:this.config.reasoningEffort}),t?.thinkingTokenBudget)switch(t.thinkingTokenBudget){case"none":f={};break;case"minimal":f={...f,effort:"minimal"};break;case"low":f={...f,effort:"medium"};break;case"medium":case"high":case"highest":f={...f,effort:"high"};break}Object.keys(f).length>0&&f.effort?m.reasoning=f:m.reasoning=void 0;let x=m;return this.responsesReqUpdater&&(x=this.responsesReqUpdater(x)),[r,x]}createChatResp(e){let{id:t,output:n,usage:r}=e;r&&(this.tokensUsed={promptTokens:r.prompt_tokens,completionTokens:r.completion_tokens??r.output_tokens??0,totalTokens:r.total_tokens});let o={};for(let i of n??[])switch(i.type){case"message":o.id=i.id,o.content=ei(i.content,t),o.finishReason=i.status==="completed"?"stop":"content_filter",o.citations=xo(i.content);break;case"reasoning":o.id=i.id,i.encrypted_content?o.thought=i.encrypted_content:o.thought=i.summary.map(a=>typeof a=="object"?JSON.stringify(a):a).join(`
176
+ `);break;case"file_search_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"file_search",params:{queries:i.queries,results:i.results}}}],o.finishReason="function_call";break;case"web_search_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"web_search",params:{queries:i.queries}}}],o.finishReason="function_call";break;case"computer_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"computer_use",params:{action:i.action}}}],o.finishReason="function_call";break;case"code_interpreter_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"code_interpreter",params:{code:i.code,results:i.results}}}],o.finishReason="function_call";break;case"image_generation_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"image_generation",params:{result:i.result}}}],o.finishReason="function_call";break;case"local_shell_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"local_shell",params:{action:i.action}}}],o.finishReason="function_call";break;case"mcp_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:"mcp",params:{name:i.name,args:i.args,serverLabel:i.server_label,output:i.output,error:i.error}}}],o.finishReason="function_call";break;case"function_call":o.id=i.id,o.functionCalls=[{id:i.id,type:"function",function:{name:i.name,params:i.arguments}}],o.finishReason="function_call";break}return{results:[{...o,index:0}],remoteId:t}}createChatStreamResp(e){let t=e,n={index:0,id:"",content:"",finishReason:"stop"},r;switch(t.type){case"response.created":case"response.in_progress":case"response.queued":r=t.response.id,n.id=`${t.response.id}_res_0`;break;case"response.output_item.added":switch(t.item.type){case"message":n.id=t.item.id,n.content=ei(t.item.content,t.item.id),n.citations=xo(t.item.content);break;case"function_call":n.id=t.item.id,n.functionCalls=[{id:t.item.id,type:"function",function:{name:t.item.name,params:t.item.arguments}}];break;case"file_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"file_search",params:{queries:o.queries||[],results:o.results?.map(i=>({fileId:i.file_id,filename:i.filename,score:i.score,text:i.text,attributes:i.attributes}))}}}]}break;case"web_search_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"web_search",params:{queries:o.queries||[]}}}]}break;case"computer_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"computer_use",params:{action:o.action||{}}}}]}break;case"code_interpreter_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"code_interpreter",params:{code:o.code||"",results:o.results}}}]}break;case"image_generation_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"image_generation",params:{result:o.result}}}]}break;case"local_shell_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"local_shell",params:{action:o.action||{}}}}]}break;case"mcp_call":{let o=t.item;n.id=t.item.id,n.functionCalls=[{id:o.id,type:"function",function:{name:"mcp",params:{name:o.name||"",args:o.args||"",serverLabel:o.server_label||"",output:o.output,error:o.error}}}]}break}break;case"response.content_part.added":n.id=t.item_id,n.content=ei([t.part],t.item_id),n.citations=xo([t.part]);break;case"response.output_text.delta":n.id=t.item_id,n.content=t.delta;break;case"response.output_text.done":break;case"response.function_call_arguments.delta":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.delta}}];break;case"response.reasoning_summary_text.delta":n.id=t.item_id,n.thought=t.delta;break;case"response.file_search_call.in_progress":case"response.file_search_call.searching":n.id=t.item_id,n.finishReason="function_call";break;case"response.file_search_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.web_search_call.in_progress":case"response.web_search_call.searching":n.id=t.item_id,n.finishReason="function_call";break;case"response.web_search_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.in_progress":case"response.image_generation_call.generating":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.completed":n.id=t.item_id,n.finishReason="function_call";break;case"response.image_generation_call.partial_image":n.id=t.item_id,n.finishReason="function_call";break;case"response.mcp_call.in_progress":n.id=t.item_id,n.finishReason="function_call";break;case"response.mcp_call.arguments.delta":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.delta}}];break;case"response.mcp_call.arguments.done":n.id=t.item_id,n.functionCalls=[{id:t.item_id,type:"function",function:{name:"",params:t.arguments}}];break;case"response.mcp_call.completed":case"response.mcp_call.failed":n.id="mcp_call_event",n.finishReason="function_call";break;case"response.mcp_list_tools.in_progress":case"response.mcp_list_tools.completed":case"response.mcp_list_tools.failed":n.id="mcp_list_tools_event",n.finishReason="function_call";break;case"response.output_item.done":switch(t.item.type){case"message":if(n.id=t.item.id,n.finishReason=t.item.status==="completed"?"stop":"error",!n.citations||n.citations.length===0){let o=xo(t.item.content||[]);o&&(n.citations=o)}break;case"function_call":case"file_search_call":case"web_search_call":case"computer_call":case"code_interpreter_call":case"image_generation_call":case"local_shell_call":case"mcp_call":n.id=t.item.id,n.finishReason="function_call";break}break;case"response.completed":t.response.usage&&(this.tokensUsed={promptTokens:t.response.usage.prompt_tokens,completionTokens:t.response.usage.completion_tokens??t.response.usage.output_tokens??0,totalTokens:t.response.usage.total_tokens}),r=t.response.id,n.id=`${t.response.id}_completed`,n.finishReason="stop";break;case"response.failed":r=t.response.id,n.id=`${t.response.id}_failed`,n.finishReason="error";break;case"response.incomplete":r=t.response.id,n.id=`${t.response.id}_incomplete`,n.finishReason="length";break;case"error":n.id="error",n.content=`Error: ${t.message}`,n.finishReason="error";break;default:n.id="unknown";break}return{results:[n],remoteId:r}}createEmbedReq(e){let t=e.embedModel;if(!t)throw new Error("Embed model not set");if(!e.texts||e.texts.length===0)throw new Error("Embed texts is empty");let n={name:"/embeddings"},r={model:t,input:e.texts,dimensions:this.config.dimensions};return[n,r]}},ei=(s,e)=>{let t=s.filter(n=>n.type==="refusal");if(t.length>0){let n=t.map(r=>r.refusal).join(`
177
+ `);throw new Ce(n,void 0,e)}return s.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
+ `)};function xo(s){let e=[];for(let t of s??[])if(t?.type==="output_text"&&Array.isArray(t.annotations))for(let n of t.annotations)n&&n.type==="url_citation"&&typeof n.url=="string"&&e.push({url:n.url,title:n.title,description:n.description});return e.length?e:void 0}var pr=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),xu=()=>({...pr(),model:"gpt-4o",temperature:.5}),Au=()=>({...pr(),model:"gpt-4o",temperature:.9}),cr=class extends Oe{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:i,responsesReqUpdater:a,supportFor:u={functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}}){if(!e||e==="")throw new Error("OpenAI API key not set");let l=new an(t,n?.streamingUsage??!0,a),c=i?.map(p=>{let d=p,m=d?.config;if(!m)return p;let g={};m.maxTokens!==void 0&&(g.maxTokens=m.maxTokens),m.temperature!==void 0&&(g.temperature=m.temperature),m.topP!==void 0&&(g.topP=m.topP),m.presencePenalty!==void 0&&(g.presencePenalty=m.presencePenalty),m.frequencyPenalty!==void 0&&(g.frequencyPenalty=m.frequencyPenalty);let f=m.stopSequences??m.stop;f!==void 0&&(g.stopSequences=f),m.n!==void 0&&(g.n=m.n),m.stream!==void 0&&(g.stream=m.stream);let x={...d};Object.keys(g).length>0&&(x.modelConfig={...d.modelConfig??{},...g});let y=m?.thinking?.thinkingTokenBudget;if(typeof y=="number"){let h=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",b=Number.POSITIVE_INFINITY;for(let[I,T]of h){let R=Math.abs(y-T);R<b&&(b=R,A=I)}x.thinkingTokenBudget=A}return m?.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!m.thinking.includeThoughts),x});super(l,{name:"OpenAI",apiURL:r||"https://api.openai.com/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:o,defaults:{model:t.model??"gpt-4o",embedModel:t.embedModel??"text-embedding-ada-002"},options:n,supportFor:u,models:c??i})}},un=class extends cr{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...Jr,...o??[]];let i=a=>{let u=We({model:a,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:u?.supported?.thinkingBudget??!1,hasShowThoughts:u?.supported?.showThoughts??!1,structuredOutputs:u?.supported?.structuredOutputs??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}};super({apiKey:e,config:{...pr(),...t},options:n,modelInfo:o,models:r,supportFor:i})}};var ti=()=>structuredClone({model:"openrouter/auto",...ye()}),ln=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o,referer:i,title:a}){if(!e||e==="")throw new Error("OpenRouter API key not set");let u={...ti(),...t},l={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},c=o??[];super({apiKey:e,config:u,options:n,apiURL:"https://openrouter.ai/api/v1",modelInfo:c,models:r,supportFor:l}),super.setName("OpenRouter"),super.setHeaders(async()=>{let p={Authorization:`Bearer ${e}`};return i&&(p["HTTP-Referer"]=i),a&&(p["X-Title"]=a),p})}};async function Ao(s,e,t={}){if(typeof s=="string")return[{type:"text",text:s}];if(!Array.isArray(s))return[{type:"text",text:String(s)}];let n=e.getFeatures(),r=[];for(let o of s)try{switch(o.type){case"text":r.push({type:"text",text:o.text});break;case"image":if(n.media.images.supported)o.altText?r.push({type:"text",text:`[Image: ${o.altText}]`}):r.push({type:"text",text:"[Image content]"});else if(o.altText)r.push({type:"text",text:o.altText});else if(t.imageToText)try{let i=await t.imageToText(o.image);r.push({type:"text",text:i})}catch(i){throw new ot(i,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new Je("Images",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:"[Image content not supported by this provider]"})}break;case"audio":if(n.media.audio.supported)o.transcription?r.push({type:"text",text:o.transcription}):r.push({type:"text",text:"[Audio content]"});else if(o.transcription)r.push({type:"text",text:o.transcription});else if(t.audioToText)try{let i=await t.audioToText(o.data,o.format);r.push({type:"text",text:i})}catch(i){throw new ot(i,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new Je("Audio",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:"[Audio content not supported by this provider]"})}break;case"file":if(n.media.files.supported)o.extractedText?r.push({type:"text",text:o.extractedText}):r.push({type:"text",text:`[File: ${o.filename}]`});else if(o.extractedText)r.push({type:"text",text:o.extractedText});else if(t.fileToText)try{let i=await t.fileToText(o.data,o.mimeType);r.push({type:"text",text:i})}catch(i){throw new ot(i,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new Je("Files",e.getName(),!1);case"skip":continue;default:r.push({type:"text",text:`[File: ${o.filename} - content not accessible by this provider]`})}break;case"url":if(n.media.urls.supported)o.cachedContent?r.push({type:"text",text:o.cachedContent}):r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`});else if(o.cachedContent)r.push({type:"text",text:o.cachedContent});else if(t.urlToText)try{let i=await t.urlToText(o.url);r.push({type:"text",text:i})}catch(i){throw new ot(i,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new Je("URLs",e.getName(),!1);case"skip":continue;case"degrade":r.push({type:"text",text:`[Link: ${o.url}${o.title?` - ${o.title}`:""}]`})}break;default:typeof o=="object"&&o.text?r.push({type:"text",text:o.text}):r.push({type:"text",text:String(o)})}}catch(i){throw i instanceof Je||i instanceof ot?i:new ot(i,o.type||"unknown","content processing")}return r}function yu(s){let e=!1,t=!1,n=!1,r=!1;for(let o of s)if(o.role==="user"&&Array.isArray(o.content))for(let i of o.content)switch(i.type){case"image":e=!0;break;case"audio":t=!0;break;case"file":n=!0;break;case"url":r=!0;break}return{hasImages:e,hasAudio:t,hasFiles:n,hasUrls:r}}var dr=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(dr||{});var yo=[{name:"reka-core",currency:"usd",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"reka-flash",currency:"usd",promptTokenCostPer1M:.8,completionTokenCostPer1M:2},{name:"reka-edge",currency:"usd",promptTokenCostPer1M:.4,completionTokenCostPer1M:1}];var mr=()=>structuredClone({model:"reka-core",...ye()}),Iu=()=>structuredClone({...mr(),model:"reka-core"}),Cu=()=>structuredClone({model:"reka-core",..._e()}),Tu=()=>({...mr(),model:"reka-flash"}),ni=class{constructor(e){this.config=e}tokensUsed;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,topP:e.topP,n:e.n,stream:e.stream}}createChatReq=e=>{let t=e.model;if(!e.chatPrompt||e.chatPrompt.length===0)throw new Error("Chat prompt is empty");let n={name:"/chat/completions"},r=Ip(e),o=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,i=e.modelConfig?.stream??this.config.stream,a={model:t,messages:r,max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},top_k:e.modelConfig?.n??this.config.n,...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},stop:e.modelConfig?.stopSequences??this.config.stop,presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,...o?{frequency_penalty:o}:{},...i?{stream:!0}:{}};return[n,a]};createChatResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((i,a)=>{let u=bu(i.finish_reason),l;return typeof i.message.content=="string"?l=i.message.content:l=i.message.content.text,{index:a,id:`${t}`,content:l,finishReason:u}}),remoteId:t}};createChatStreamResp=e=>{let{id:t,usage:n,responses:r}=e;return this.tokensUsed=n?{promptTokens:n.input_tokens,completionTokens:n.output_tokens,totalTokens:n.input_tokens+n.output_tokens}:void 0,{results:r.map((i,a)=>{let u=bu(i.finish_reason),l;return typeof i.chunk.content=="string"?l=i.chunk.content:l=i.chunk.content.text,{index:a,id:`${t}`,content:l,finishReason:u}})}}},bu=s=>{switch(s){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function Ip(s){return s.chatPrompt.map(e=>{switch(e.role){case"system":return{role:"user",content:e.content};case"user":return Array.isArray(e.content)?{role:"user",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})}:{role:"user",content:e.content};case"assistant":if(Array.isArray(e.content))return{role:"assistant",content:e.content.map(t=>{switch(t.type){case"text":return{type:"text",text:t.text};case"image":throw new Error("Image type not supported");default:throw new Error("Invalid content type")}})};if(!e.content)throw new Error("Assistant content is empty");return{role:"user",content:e.content};default:throw new Error("Invalid role")}})}var cn=class extends Oe{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=yo,models:i}){if(!e||e==="")throw new Error("Reka API key not set");let a={...mr(),...t},u=new ni(a);super(u,{name:"Reka",apiURL:r||"https://api.reka.ai/v1/chat",headers:async()=>({"X-Api-Key":e}),modelInfo:o,defaults:{model:a.model},options:n,supportFor:{functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0},models:i})}};var bo=class{providers;processingServices;config;constructor(e){this.providers=[e.providers.primary,...e.providers.alternatives],this.processingServices=e.processing,this.config=e.routing}async chat(e,t={}){let n=await this.selectProviderWithDegradation(e,t.routingOptions||{}),r=await this.preprocessRequest(e,n.provider,t.processingOptions);try{return{response:await n.provider.chat(r,t),routing:n}}catch(o){if(o instanceof Je&&t.fallbackProviders?.length)return await this.tryFallbackProviders(e,t.fallbackProviders,t);throw o}}async preprocessRequest(e,t,n){let r={...n,fallbackBehavior:n?.fallbackBehavior||"degrade",imageToText:n?.imageToText||this.processingServices.imageToText,audioToText:n?.audioToText||this.processingServices.audioToText,fileToText:n?.fileToText||this.processingServices.fileToText,urlToText:n?.urlToText||this.processingServices.urlToText},o=[];for(let i of e.chatPrompt)if(i.role==="user"&&Array.isArray(i.content)){let a=await Ao(i.content,t,r);a.every(l=>l.type==="text")&&a.length===1?o.push({...i,content:a[0].text}):o.push({...i,content:a.map(l=>({type:"text",text:l.text}))})}else o.push(i);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=Pt(e),r=[],o=[],i=[];try{let a=to(e,this.providers,{requireExactMatch:t.requireExactMatch??this.config.capability.requireExactMatch,allowDegradation:t.allowDegradation??this.config.capability.allowDegradation}),u=a.getFeatures();return n.hasImages&&!u.media.images.supported&&(o.push("Images will be converted to text descriptions"),r.push("Image-to-text conversion")),n.hasAudio&&!u.media.audio.supported&&(o.push("Audio will be transcribed to text"),r.push("Audio-to-text transcription")),n.hasFiles&&!u.media.files.supported&&(o.push("File content will be extracted to text"),r.push("File-to-text extraction")),n.hasUrls&&!u.media.urls.supported&&(o.push("URL content will be pre-fetched"),r.push("URL content fetching")),n.requiresStreaming&&!u.streaming&&i.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!u.caching.supported&&i.push("Content caching not supported"),{provider:a,processingApplied:r,degradations:o,warnings:i}}catch(a){throw new Error(`Provider selection failed: ${a instanceof Error?a.message:"Unknown error"}`)}}async tryFallbackProviders(e,t,n){for(let r of t)try{let o={provider:r,processingApplied:["Fallback provider selection"],degradations:["Using fallback provider due to primary provider failure"],warnings:[]},i=await this.preprocessRequest(e,r,{fallbackBehavior:"degrade"});return{response:await r.chat(i,n),routing:o}}catch{}throw new Error("All fallback providers failed")}async getRoutingRecommendation(e){return await this.selectProviderWithDegradation(e,{})}async validateRequest(e){let t=Pt(e),n=[],r=[];try{let o=await this.selectProviderWithDegradation(e,{});return o.degradations.length>0&&(n.push(...o.degradations),r.push("Consider using a provider that natively supports all media types")),o.warnings.length>0&&n.push(...o.warnings),t.hasImages&&this.processingServices.imageToText===void 0&&(this.providers.some(a=>a.getFeatures().media.images.supported)||(n.push("No image processing service available and no providers support images"),r.push("Add imageToText processing service or use image-capable provider"))),t.hasAudio&&this.processingServices.audioToText===void 0&&(this.providers.some(a=>a.getFeatures().media.audio.supported)||(n.push("No audio processing service available and no providers support audio"),r.push("Add audioToText processing service or use audio-capable provider"))),{canHandle:n.length===0||o.degradations.length>0,issues:n,recommendations:r}}catch(o){return{canHandle:!1,issues:[`Cannot route request: ${o instanceof Error?o.message:"Unknown error"}`],recommendations:["Add more providers or processing services to handle this request"]}}}getRoutingStats(){let e={};for(let t of this.providers){let n=t.getFeatures(),r=t.getName();n.functions&&(e.Functions=e.Functions||[],e.Functions.push(r)),n.streaming&&(e.Streaming=e.Streaming||[],e.Streaming.push(r)),n.media.images.supported&&(e.Images=e.Images||[],e.Images.push(r)),n.media.audio.supported&&(e.Audio=e.Audio||[],e.Audio.push(r)),n.media.files.supported&&(e.Files=e.Files||[],e.Files.push(r)),n.media.urls.supported&&(e.URLs=e.URLs||[],e.URLs.push(r)),n.caching.supported&&(e.Caching=e.Caching||[],e.Caching.push(r))}return{totalProviders:this.providers.length,capabilityMatrix:e,recommendedProvider:this.providers[0]?.getName()||"None"}}};var gr=(h=>(h.KimiK25="moonshotai/Kimi-K2.5",h.KimiK2Instruct0905="moonshotai/Kimi-K2-Instruct-0905",h.KimiK2Thinking="moonshotai/Kimi-K2-Thinking",h.DeepSeekV31="deepseek-ai/DeepSeek-V3.1",h.DeepSeekR1="deepseek-ai/DeepSeek-R1",h.GPTOSS120B="openai/gpt-oss-120b",h.GPTOSS20B="openai/gpt-oss-20b",h.Qwen35_397B="Qwen/Qwen3.5-397B-A17B",h.Qwen3CoderNext="Qwen/Qwen3-Coder-Next-FP8",h.Qwen3Coder480B="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",h.Qwen3_235BInstruct2507="Qwen/Qwen3-235B-A22B-Instruct-2507-tput",h.Qwen3_235BThinking2507="Qwen/Qwen3-235B-A22B-Thinking-2507",h.Qwen3Next80BInstruct="Qwen/Qwen3-Next-80B-A3B-Instruct",h.Qwen3Next80BThinking="Qwen/Qwen3-Next-80B-A3B-Thinking",h.GLM5="zai-org/GLM-5",h.GLM47="zai-org/GLM-4.7",h.Llama4Maverick="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",h.Llama33_70B="meta-llama/Llama-3.3-70B-Instruct-Turbo",h))(gr||{});var Io=[{name:"moonshotai/Kimi-K2.5",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:2.8},{name:"moonshotai/Kimi-K2-Instruct-0905",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:3},{name:"moonshotai/Kimi-K2-Thinking",currency:"usd",promptTokenCostPer1M:1.2,completionTokenCostPer1M:4},{name:"deepseek-ai/DeepSeek-V3.1",currency:"usd",promptTokenCostPer1M:.6,completionTokenCostPer1M:1.7},{name:"deepseek-ai/DeepSeek-R1",currency:"usd",promptTokenCostPer1M:1.4,completionTokenCostPer1M:2.2},{name:"openai/gpt-oss-120b",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:.6},{name:"openai/gpt-oss-20b",currency:"usd",promptTokenCostPer1M:.05,completionTokenCostPer1M:.2},{name:"Qwen/Qwen3.5-397B-A17B",currency:"usd",promptTokenCostPer1M:.6,completionTokenCostPer1M:3.6},{name:"Qwen/Qwen3-Coder-Next-FP8",currency:"usd",promptTokenCostPer1M:.5,completionTokenCostPer1M:1.2},{name:"Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",currency:"usd",promptTokenCostPer1M:2,completionTokenCostPer1M:2},{name:"Qwen/Qwen3-235B-A22B-Instruct-2507-tput",currency:"usd",promptTokenCostPer1M:.2,completionTokenCostPer1M:.6},{name:"Qwen/Qwen3-235B-A22B-Thinking-2507",currency:"usd",promptTokenCostPer1M:.65,completionTokenCostPer1M:3},{name:"Qwen/Qwen3-Next-80B-A3B-Instruct",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:1.5},{name:"Qwen/Qwen3-Next-80B-A3B-Thinking",currency:"usd",promptTokenCostPer1M:.15,completionTokenCostPer1M:1.5},{name:"zai-org/GLM-5",currency:"usd",promptTokenCostPer1M:1,completionTokenCostPer1M:3.2},{name:"zai-org/GLM-4.7",currency:"usd",promptTokenCostPer1M:.45,completionTokenCostPer1M:2},{name:"meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",currency:"usd",promptTokenCostPer1M:.27,completionTokenCostPer1M:.85},{name:"meta-llama/Llama-3.3-70B-Instruct-Turbo",currency:"usd",promptTokenCostPer1M:.88,completionTokenCostPer1M:.88}];var ri=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...ye()}),pn=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Together API key not set");let i={...ri(),...t};o=[...Io,...o??[]];let a={functions:!0,streaming:!0,hasThinkingBudget:!1,hasShowThoughts:!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0};super({apiKey:e,config:i,options:n,apiURL:"https://api.together.xyz/v1",modelInfo:o,models:r,supportFor:a}),super.setName("Together")}};var fr=(d=>(d.Llama31_8B_Instruct="Llama-3.1-8B-Instruct-q4f32_1-MLC",d.Llama31_70B_Instruct="Llama-3.1-70B-Instruct-q4f16_1-MLC",d.Llama32_1B_Instruct="Llama-3.2-1B-Instruct-q4f32_1-MLC",d.Llama32_3B_Instruct="Llama-3.2-3B-Instruct-q4f32_1-MLC",d.Mistral7B_Instruct="Mistral-7B-Instruct-v0.3-q4f32_1-MLC",d.Phi35_Mini_Instruct="Phi-3.5-mini-instruct-q4f32_1-MLC",d.Gemma2_2B_Instruct="gemma-2-2b-it-q4f32_1-MLC",d.Gemma2_9B_Instruct="gemma-2-9b-it-q4f32_1-MLC",d.Qwen2_5_0_5B_Instruct="Qwen2.5-0.5B-Instruct-q4f32_1-MLC",d.Qwen2_5_1_5B_Instruct="Qwen2.5-1.5B-Instruct-q4f32_1-MLC",d.Qwen2_5_3B_Instruct="Qwen2.5-3B-Instruct-q4f32_1-MLC",d.Qwen2_5_7B_Instruct="Qwen2.5-7B-Instruct-q4f32_1-MLC",d))(fr||{});var Co=[{name:"Llama-3.1-8B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"Llama-3.1-70B-Instruct-q4f16_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096,isExpensive:!0},{name:"Llama-3.2-1B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Llama-3.2-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:2048},{name:"Mistral-7B-Instruct-v0.3-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096},{name:"Phi-3.5-mini-instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:128e3,maxTokens:4096},{name:"gemma-2-2b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"gemma-2-9b-it-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:8192,maxTokens:2048},{name:"Qwen2.5-0.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-1.5B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-3B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:2048},{name:"Qwen2.5-7B-Instruct-q4f32_1-MLC",currency:"usd",promptTokenCostPer1M:0,completionTokenCostPer1M:0,contextWindow:32768,maxTokens:4096}];var si=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...ye()}),wu=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",..._e()}),oi=class{constructor(e,t){this.config=e;this.engine=t}tokensUsed;engine;getTokenUsage(){return this.tokensUsed}getModelConfig(){let{config:e}=this;return{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,topK:e.topK,presencePenalty:e.presencePenalty,frequencyPenalty:e.frequencyPenalty,stopSequences:e.stopSequences,endSequences:e.endSequences,stream:e.stream,n:e.n}}createChatReq(e){let t=e.model,n=e.chatPrompt.map(a=>{if(a.role==="function")return{role:"function",name:a.functionId,content:typeof a.result=="string"?a.result:JSON.stringify(a.result)};let u="";typeof a.content=="string"?u=a.content:Array.isArray(a.content)&&(u=a.content.filter(c=>c.type==="text").map(c=>c.text).join(`
179
+ `));let l={role:a.role,content:u};return a.role==="assistant"&&a.functionCalls?.length?{...l,tool_calls:a.functionCalls.map(c=>({id:c.id,type:"function",function:{name:c.function.name,arguments:typeof c.function.params=="string"?c.function.params:JSON.stringify(c.function.params||{})}}))}:l}),r=e.functions?.map(a=>({type:"function",function:{name:a.name,description:a.description,parameters:a.parameters||{type:"object",properties:{}}}})),o={name:"/chat/completions",localCall:async(a,u)=>{try{let l=await this.engine.chat.completions.create({...a,stream:u||!1});return u?new ReadableStream({async start(c){try{for await(let p of l)c.enqueue(p);c.close()}catch(p){c.error(p)}}}):l}catch(l){throw new Error(`WebLLM API error: ${l}`)}}},i={model:t,messages:n,...r?.length?{tools:r}:{},max_tokens:e.modelConfig?.maxTokens??this.config.maxTokens,...e.modelConfig?.temperature!==void 0?{temperature:e.modelConfig.temperature}:{},...e.modelConfig?.topP!==void 0?{top_p:e.modelConfig.topP}:{},presence_penalty:e.modelConfig?.presencePenalty??this.config.presencePenalty,frequency_penalty:e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,stop:e.modelConfig?.stopSequences??this.config.stopSequences,stream:e.modelConfig?.stream??this.config.stream,n:e.modelConfig?.n??this.config.n};return[o,i]}createEmbedReq=e=>{throw new Error("WebLLM does not support embeddings")};createChatResp=e=>(this.tokensUsed={promptTokens:e.usage?.prompt_tokens??0,completionTokens:e.usage?.completion_tokens??0,totalTokens:e.usage?.total_tokens??0},{results:e.choices.map((n,r)=>{let o="stop";switch(n.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let i=n.message.tool_calls?.map(a=>({id:a.id,type:"function",function:{name:a.function.name,params:a.function.arguments}}));return{index:r,id:e.id,content:n.message.content||"",functionCalls:i,finishReason:o}}),remoteId:e.id});createChatStreamResp=(e,t)=>{let n=t,r=e.choices[0];if(!r)throw new Error("No choice in WebLLM stream response");if(r.delta.content&&(n.content=(n.content||"")+r.delta.content),r.delta.tool_calls){n.toolCalls||(n.toolCalls=[]);for(let u of r.delta.tool_calls){let l=n.toolCalls[u.index];l?u.function?.arguments&&(l.function.arguments=(l.function?.arguments||"")+u.function.arguments):n.toolCalls[u.index]={id:u.id,type:u.type,function:{name:u.function?.name,arguments:u.function?.arguments||""}}}}e.usage&&(this.tokensUsed={promptTokens:e.usage.prompt_tokens,completionTokens:e.usage.completion_tokens,totalTokens:e.usage.total_tokens});let o;if(r.finish_reason)switch(r.finish_reason){case"stop":o="stop";break;case"length":o="length";break;case"tool_calls":o="function_call";break;case"content_filter":o="content_filter";break;default:o="stop";break}let i=n.toolCalls?.map(u=>({id:u.id||"",type:"function",function:{name:u.function?.name||"",params:u.function?.arguments||""}}));return{results:[{index:0,id:e.id,content:n.content||"",functionCalls:i,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},dn=class extends Oe{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...si(),...t},i=new oi(o,e);super(i,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:Co,defaults:{model:o.model},supportFor:a=>({functions:!0,streaming:!0,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}),options:n,models:r})}};var hr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(hr||{}),ii=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(ii||{});var To=[{name:"grok-3",currency:"USD",promptTokenCostPer1M:3,completionTokenCostPer1M:15},{name:"grok-3-mini",currency:"USD",promptTokenCostPer1M:.3,completionTokenCostPer1M:.5,supported:{thinkingBudget:!0}},{name:"grok-3-fast",currency:"USD",promptTokenCostPer1M:5,completionTokenCostPer1M:25},{name:"grok-3-mini-fast",currency:"USD",promptTokenCostPer1M:.6,completionTokenCostPer1M:4,supported:{thinkingBudget:!0}}];var wo=()=>structuredClone({model:"grok-3-mini",...ye()}),Ru=()=>structuredClone({...wo(),model:"grok-3"}),mn=class extends Se{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("Grok API key not set");let i={...wo(),...t};o=[...To,...o??[]];let a=l=>{let c=We({model:l,modelInfo:o,models:r});return{functions:!0,streaming:!0,hasThinkingBudget:c?.supported?.thinkingBudget??!1,hasShowThoughts:c?.supported?.showThoughts??!1,media:{images:{supported:!1,formats:[]},audio:{supported:!1,formats:[]},files:{supported:!1,formats:[],uploadMethod:"none"},urls:{supported:!1,webSearch:!1,contextFetching:!1}},caching:{supported:!1,types:[]},thinking:!1,multiTurn:!0}},u=l=>{if(n?.searchParameters){let c=n.searchParameters;return{...l,search_parameters:{mode:c.mode,return_citations:c.returnCitations,from_date:c.fromDate,to_date:c.toDate,max_search_results:c.maxSearchResults,sources:c.sources?.map(p=>({type:p.type,country:p.country,excluded_websites:p.excludedWebsites,allowed_websites:p.allowedWebsites,safe_search:p.safeSearch,x_handles:p.xHandles,links:p.links}))}}}return l};super({apiKey:e,config:i,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:u}),super.setName("Grok")}};function ku(s){return xr.create(s)}var xr=class s{ai;static create(e){return new s(e)}constructor(e){switch(e.name){case"openai":this.ai=new Qt(e);break;case"openai-responses":this.ai=new un(e);break;case"azure-openai":this.ai=new Yt(e);break;case"grok":this.ai=new mn(e);break;case"huggingface":this.ai=new rn(e);break;case"groq":this.ai=new nn(e);break;case"together":this.ai=new pn(e);break;case"openrouter":this.ai=new ln(e);break;case"cohere":this.ai=new Xt(e);break;case"google-gemini":this.ai=new en(e);break;case"anthropic":this.ai=new Kt(e);break;case"mistral":this.ai=new on(e);break;case"deepseek":this.ai=new Zt(e);break;case"ollama":this.ai=new sn(e);break;case"reka":this.ai=new cn(e);break;case"webllm":this.ai=new dn(e);break;default:throw new Error("Unknown AI")}}getName(){return this.ai.getName()}getId(){return this.ai.getId()}getFeatures(e){return this.ai.getFeatures(e)}getModelList(){return this.ai.getModelList()}getLastUsedChatModel(){return this.ai.getLastUsedChatModel()}getLastUsedEmbedModel(){return this.ai.getLastUsedEmbedModel()}getLastUsedModelConfig(){return this.ai.getLastUsedModelConfig()}getMetrics(){return this.ai.getMetrics()}async chat(e,t){return await this.ai.chat(e,t)}async embed(e,t){return await this.ai.embed(e,t)}setOptions(e){this.ai.setOptions(e)}getOptions(){return this.ai.getOptions()}getLogger(){return this.ai.getLogger()}};var Ro=require("@opentelemetry/api");var Qe=class{name;fetch;tracer;_upsert;_batchUpsert;_query;constructor({name:e,fetch:t,tracer:n}){this.name=e,this.fetch=t,this.tracer=n}async upsert(e,t){if(!this._upsert)throw new Error("upsert() not implemented");return this.tracer?await this.tracer.startActiveSpan("DB Upsert Request",{kind:Ro.SpanKind.SERVER,attributes:{[ae.DB_SYSTEM]:this.name,[ae.DB_OPERATION_NAME]:"upsert",[ae.DB_TABLE]:e.table,[ae.DB_NAMESPACE]:e.namespace,[ae.DB_OPERATION_NAME]:t?"update":"insert"}},async n=>{try{return await this._upsert(e,t,{span:n})}finally{n.end()}}):await this._upsert(e,t)}async batchUpsert(e,t){if(!this._batchUpsert)throw new Error("batchUpsert() not implemented");if(e.length===0)throw new Error("Batch request is empty");if(!e[0])throw new Error("Batch request is invalid first element is undefined");return this.tracer?await this.tracer.startActiveSpan("DB Batch Upsert Request",{kind:Ro.SpanKind.SERVER,attributes:{[ae.DB_SYSTEM]:this.name,[ae.DB_OPERATION_NAME]:"upsert",[ae.DB_TABLE]:e[0].table,[ae.DB_NAMESPACE]:e[0].namespace,[ae.DB_OPERATION_NAME]:t?"update":"insert"}},async n=>{try{return await this._batchUpsert(e,t,{span:n})}finally{n.end()}}):await this._batchUpsert(e,t)}async query(e){if(!this._query)throw new Error("query() not implemented");return this.tracer?await this.tracer.startActiveSpan("DB Query Request",{kind:Ro.SpanKind.SERVER,attributes:{[ae.DB_SYSTEM]:this.name,[ae.DB_OPERATION_NAME]:"upsert",[ae.DB_TABLE]:e.table,[ae.DB_NAMESPACE]:e.namespace,[ae.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var ai="https://api.cloudflare.com/client/v4/accounts/",gn=class extends Qe{apiKey;accountId;constructor({apiKey:e,accountId:t,fetch:n,tracer:r}){if(!e||!t)throw new Error("Cloudflare credentials not set");super({name:"Cloudflare",fetch:n,tracer:r}),this.apiKey=e,this.accountId=t}_upsert=async(e,t,n)=>{let r=await Be({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/upsert`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},{id:e.id,values:e.values,namespace:e.namespace,metadata:e.metadata});if(r.errors)throw new Error(`Cloudflare upsert failed: ${r.errors.map(({message:o})=>o).join(", ")}`);return{ids:r.result.ids}};batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length<1)throw new Error("Batch request is empty");if(!e[0]||!e[0].table)throw new Error("Table name is empty");let r=e[0].table,o=await Be({url:new URL(`${this.accountId}/vectorize/indexes/${r}/upsert`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:n?.span},e.map(i=>({id:i.id,values:i.values,namespace:i.namespace,metadata:i.metadata})));if(o.errors)throw new Error(`Cloudflare batch upsert failed: ${o.errors.map(({message:i})=>i).join(", ")}`);return{ids:o.result.ids}};query=async(e,t)=>{let n=await Be({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,ai),headers:{"X-Auth-Key":this.apiKey},fetch:this.fetch,span:t?.span},{vector:e.values,topK:e.limit||10,returnValues:!0});if(n.errors)throw new Error(`Cloudflare query failed: ${n.errors.map(({message:o})=>o).join(", ")}`);return{matches:n.result.matches.map(({id:o,score:i,values:a,metadata:u})=>({id:o,score:i,values:a,metadata:u}))}}};var wt=class extends Qe{state;constructor({tracer:e}={}){super({name:"Memory",tracer:e}),this.state={}}_upsert=async(e,t,n)=>{if(!this.state[e.table])this.state[e.table]={[e.id]:e};else{let r=this.state[e.table];if(!r)throw new Error(`Table not found: ${e.table}`);r[e.id]=e}return{ids:[e.id]}};_batchUpsert=async(e,t,n)=>{let r=[];for(let o of e){let i=await this.upsert(o,t);r.push(...i.ids)}return{ids:r}};_query=async(e,t)=>{let n=this.state[e.table];if(!n)return{matches:[]};let r=[];return Object.entries(n).forEach(([o,i])=>{if(e.values&&i.values){let a=Cp(e.values,i.values);r.push({id:o,score:a,metadata:i.metadata})}}),r.sort((o,i)=>o.score-i.score),e.limit&&(r.length=e.limit),{matches:r}};getDB=()=>structuredClone(this.state);setDB=e=>{this.state=structuredClone(e)};clearDB=()=>{this.state={}}},Cp=(s,e)=>{if(s.length!==e.length)throw new Error("Vectors must be of the same length.");let t=0,n=0,r=0,o=!0,i=!0,a=new Float64Array(s),u=new Float64Array(e);for(let d=0;d<a.length;d++)t+=a[d]*u[d],n+=a[d]*a[d],r+=u[d]*u[d],a[d]!==0&&(o=!1),u[d]!==0&&(i=!1);if(o||i)return 1;let l=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(l*c)};var Tp=s=>({namespace:s.namespace,topK:s.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:s.values??[],id:s.id}),fn=class extends Qe{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Pinecone API key not set");super({name:"Pinecone",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>(await this._batchUpsert([e],t,n),{ids:[e.id]});_batchUpsert=async(e,t,n)=>{if(e.length===0)throw new Error("Batch request is empty");return await Be({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/vectors/upsert",fetch:this.fetch,span:n?.span},e.map(({id:r,values:o=[],metadata:i})=>({id:r,values:o,metadata:i}))),{ids:e.map(({id:r})=>r)}};query=async(e,t)=>{if(e.text)throw new Error("Pinecone does not support text");return{matches:(await Be({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},Tp(e))).matches.map(({id:o,score:i,values:a,metadata:u})=>({id:o,score:i,metadata:u,values:a}))}}};var hn=class extends Qe{apiKey;apiURL;constructor({apiKey:e,host:t,fetch:n,tracer:r}){if(!e||e==="")throw new Error("Weaviate API key not set");super({name:"Weaviate",fetch:n,tracer:r}),this.apiKey=e,this.apiURL=t}_upsert=async(e,t,n)=>{let r=await Be({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:`/v1/objects/${e.table}/${e.id}`,put:!!t,fetch:this.fetch,span:n?.span},{id:e.id,class:e.table,tenant:e.namespace,vector:e.values,properties:e.metadata??{}});if(r?.result?.errors)throw new Error(`Weaviate upsert failed: ${r.result.errors.error.map(({message:o})=>o).join(", ")}`);return{ids:[r.id]}};_batchUpsert=async(e,t,n)=>{if(t)throw new Error("Weaviate does not support batch update");if(e.length===0)throw new Error("Batch request is empty");let r=e.map(i=>({id:i.id,class:i.table,tenant:i.namespace,vector:i.values,properties:i.metadata??{}})),o=await Be({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/batch/objects",fetch:this.fetch,span:n?.span},{objects:r});if(o?.some(({result:i})=>i?.errors))throw new Error(`Weaviate batch upsert failed: ${o.map(({result:i})=>i?.errors?.error.map(({message:a})=>a).join(", ")).join(", ")}`);return{ids:o.map(({id:i})=>i)}};_query=async(e,t)=>{let n="";if(e.columns&&e.columns.length===0)throw new Error("Weaviate requires at least one column");if(e.values)n=`nearVector: {
180
180
  vector: [${e.values.join(",")}],
181
181
  }`;else if(e.text)n=`nearText: {
182
182
  concepts: ['${e.text}'],
183
- }`;else throw new Error("Weaviate requires either text or values");let r=await Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/graphql",fetch:this.fetch,span:t?.span},{query:`{
183
+ }`;else throw new Error("Weaviate requires either text or values");let r=await Be({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/v1/graphql",fetch:this.fetch,span:t?.span},{query:`{
184
184
  Get {
185
185
  ${e.table} (
186
186
  limit: ${e.limit||10},
@@ -190,31 +190,31 @@ ${u}`:`Assistant: ${l.content}`}default:throw new Error("Unknown role")}}).join(
190
190
  `)}
191
191
  }
192
192
  }
193
- }`});if(r.errors)throw new Error(`Weaviate query failed: ${r.errors.map(({message:a})=>a).join(", ")}`);let o=r.data.Get[e.table];return o?{matches:o.map(a=>({id:a.id,score:1,metadata:a}))}:{matches:[]}}};var Oo=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new kn(e);break;case"pinecone":this.db=new Sn(e);break;case"cloudflare":this.db=new Rn(e);break;case"memory":this.db=new Nt(e);break;default:throw new Error("Unknown DB")}}async upsert(e,t){return await this.db.upsert(e,t)}async batchUpsert(e,t){return await this.db.batchUpsert(e,t)}async query(e){return await this.db.query(e)}};var li="_internal",Mo=class{ai;db;chunker;rewriter;reranker;constructor({ai:e,db:t,config:n}){this.ai=e,this.db=t,this.chunker=n?.chunker??this.defaultChunker,this.reranker=n?.reranker,this.rewriter=n?.rewriter}defaultChunker=e=>e.split(/\n\n+/);insert=async(e,t)=>{try{let n=Array.isArray(e)?e.join(`
193
+ }`});if(r.errors)throw new Error(`Weaviate query failed: ${r.errors.map(({message:a})=>a).join(", ")}`);let o=r.data.Get[e.table];return o?{matches:o.map(a=>({id:a.id,score:1,metadata:a}))}:{matches:[]}}};var ko=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new hn(e);break;case"pinecone":this.db=new fn(e);break;case"cloudflare":this.db=new gn(e);break;case"memory":this.db=new wt(e);break;default:throw new Error("Unknown DB")}}async upsert(e,t){return await this.db.upsert(e,t)}async batchUpsert(e,t){return await this.db.batchUpsert(e,t)}async query(e){return await this.db.query(e)}};var ui="_internal",So=class{ai;db;chunker;rewriter;reranker;constructor({ai:e,db:t,config:n}){this.ai=e,this.db=t,this.chunker=n?.chunker??this.defaultChunker,this.reranker=n?.reranker,this.rewriter=n?.rewriter}defaultChunker=e=>e.split(/\n\n+/);insert=async(e,t)=>{try{let n=Array.isArray(e)?e.join(`
194
194
 
195
- `):e,r=this.chunker(n).filter(u=>u.length>0),o=t?.maxWordsPerChunk,s=t?.minWordsPerChunk,a=qc({initialChunks:r,minWordsPerChunk:s,maxWordsPerChunk:o}),l=t?.batchSize??10;for(let u=0;u<a.length;u+=l){let c=a.slice(u,u+l),d=(await this.ai.embed({texts:c},{abortSignal:t?.abortSignal})).embeddings.map((m,g)=>({id:`chunk_${Date.now()+g}`,table:li,values:m,metadata:{text:c[g]??""}})).filter(m=>m.metadata?.text&&m.metadata?.text.length>0);await this.db.batchUpsert(d)}}catch(n){throw new Error(`Error processing text: ${n}`)}};query=async(e,{topPercent:t,abortSignal:n}={})=>{let r=Array.isArray(e)?e:[e];if(typeof r[0]=="string"&&this.rewriter)for(let[l,u]of r.entries()){let{rewrittenQuery:c}=await this.rewriter.forward(this.ai,{query:u});r[l]=c}let o;typeof r[0]=="string"?o=(await this.ai.embed({texts:r},{abortSignal:n})).embeddings.map(u=>this.db.query({table:li,values:u})):o=r.map(l=>this.db.query({table:li,values:l}));let s=await Promise.all(o),a=[];for(let{matches:l}of s){let u=l.filter(d=>d.metadata?.text&&d.metadata?.text.length>0).map(({score:d,metadata:m})=>({score:d,text:m?.text??""})),c=t&&t>1?t/100:t,p=c?zc(u,c):u;if(this.reranker){let{rankedItems:d}=await this.reranker.forward(this.ai,{query:r[0],items:p.map(g=>g.text)}),m=d.map(g=>p.find(h=>h.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},qc=({initialChunks:i,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return i.forEach(s=>{let a=s.split(/\s+/),l=a.length;if(o+l<=e)r+=`${s}
195
+ `):e,r=this.chunker(n).filter(l=>l.length>0),o=t?.maxWordsPerChunk,i=t?.minWordsPerChunk,a=wp({initialChunks:r,minWordsPerChunk:i,maxWordsPerChunk:o}),u=t?.batchSize??10;for(let l=0;l<a.length;l+=u){let c=a.slice(l,l+u),d=(await this.ai.embed({texts:c},{abortSignal:t?.abortSignal})).embeddings.map((m,g)=>({id:`chunk_${Date.now()+g}`,table:ui,values:m,metadata:{text:c[g]??""}})).filter(m=>m.metadata?.text&&m.metadata?.text.length>0);await this.db.batchUpsert(d)}}catch(n){throw new Error(`Error processing text: ${n}`)}};query=async(e,{topPercent:t,abortSignal:n}={})=>{let r=Array.isArray(e)?e:[e];if(typeof r[0]=="string"&&this.rewriter)for(let[u,l]of r.entries()){let{rewrittenQuery:c}=await this.rewriter.forward(this.ai,{query:l});r[u]=c}let o;typeof r[0]=="string"?o=(await this.ai.embed({texts:r},{abortSignal:n})).embeddings.map(l=>this.db.query({table:ui,values:l})):o=r.map(u=>this.db.query({table:ui,values:u}));let i=await Promise.all(o),a=[];for(let{matches:u}of i){let l=u.filter(d=>d.metadata?.text&&d.metadata?.text.length>0).map(({score:d,metadata:m})=>({score:d,text:m?.text??""})),c=t&&t>1?t/100:t,p=c?Rp(l,c):l;if(this.reranker){let{rankedItems:d}=await this.reranker.forward(this.ai,{query:r[0],items:p.map(g=>g.text)}),m=d.map(g=>p.find(f=>f.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},wp=({initialChunks:s,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return s.forEach(i=>{let a=i.split(/\s+/),u=a.length;if(o+u<=e)r+=`${i}
196
196
 
197
- `,o+=l;else if(o>0&&o+l<=e*1.5)r+=`${s}
197
+ `,o+=u;else if(o>0&&o+u<=e*1.5)r+=`${i}
198
198
 
199
- `,o+=l;else if(o>t&&(n.push(r.trim()),r="",o=0),l>e){let u=a;for(;u.length>e*1.5;){let c=u.splice(0,e);n.push(c.join(" "))}u.length>0&&(r+=`${u.join(" ")}
199
+ `,o+=u;else if(o>t&&(n.push(r.trim()),r="",o=0),u>e){let l=a;for(;l.length>e*1.5;){let c=l.splice(0,e);n.push(c.join(" "))}l.length>0&&(r+=`${l.join(" ")}
200
200
 
201
- `,o+=u.length)}else r=`${s}
201
+ `,o+=l.length)}else r=`${i}
202
202
 
203
- `,o=l}),(o>t||n.length===0)&&n.push(r.trim()),n},zc=(i,e=.1)=>{let t=[...i].sort((r,o)=>r.score-o.score),n=Math.ceil(t.length*e);return t.slice(0,n)};var Un=require("@opentelemetry/api");var Ir=class{data=[];seenTags=new Set;addRequest(e,t){this.data.push(...e.map(n=>{let r=structuredClone(n);return{role:n.role,chat:[{index:t,value:r}]}}))}addFunctionResults(e){let t=e.map(({index:r,...o})=>({index:r,value:structuredClone(o)})),n=this.getLast();n?.role==="function"?n.chat.push(...t):this.data.push({role:"function",chat:t})}addResponse(e){let t=e.map(({index:n,...r})=>({index:n,value:structuredClone(r)}));this.data.push({role:"assistant",chat:t})}updateResult({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o,index:s}){let a=this.data.at(-1);if(!a||a.role!=="assistant"||a.role==="assistant"&&!a.updatable){this.data.push({role:"assistant",updatable:!0,chat:[{index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})}]});return}let l=a.chat.find(u=>u.index===s);if(!l){a.chat.push({index:s,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})});return}if(typeof e=="string"&&e.trim()!==""&&(l.value.content=e),typeof t=="string"&&t.trim()!==""&&(l.value.name=t),Array.isArray(n)&&n.length>0&&(l.value.functionCalls=n),typeof r=="string"&&r.trim()!==""){let u=l.value.thought;l.value.thought=typeof u=="string"?u+r:r}if(Array.isArray(o)&&o.length>0){let u=l.value.thoughtBlocks??[];for(let c of o){let p=u.length>0?u[u.length-1]:void 0;!c.signature&&c.data?p&&!p.signature?(p.data=(p.data??"")+c.data,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):c.signature?p&&!p.signature?(p.data=(p.data??"")+c.data,p.signature=c.signature,c.encrypted&&(p.encrypted=!0)):u.push(structuredClone(c)):u.length===0&&u.push(structuredClone(c))}l.value.thoughtBlocks=u,u.length>0&&(l.value.thought=u.map(c=>c.data).join(""))}}addTag(e){let t=this.data.at(-1);t&&(t.tags||(t.tags=[]),t.tags.includes(e)||t.tags.push(e),this.seenTags.add(e))}rewindToTag(e){let t=this.data.findIndex(n=>n.tags?.includes(e));if(t===-1){if(!this.seenTags.has(e))throw new Error(`Tag "${e}" not found`);return[]}return this.data.splice(t)}removeByTag(e){let t=this.data.reduce((n,r,o)=>(r.tags?.includes(e)&&n.push(o),n),[]);return t.length===0?[]:t.reverse().map(n=>this.data.splice(n,1).at(0)).filter(n=>n!==void 0).reverse()}history(e){let t=[];for(let{role:n,chat:r}of this.data){let o;n==="function"?o=r.filter(s=>s.index===e).map(s=>s.value):o=r.find(s=>s.index===e)?.value,Array.isArray(o)&&o.length>0?t.push(...o.map(s=>({...s,role:n}))):typeof o=="object"&&o!==null&&t.push({...o,role:n})}return t}getLast(){return this.data.at(-1)}reset(){this.data=[],this.seenTags=new Set}},vn=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new Ir}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new Ir),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)tn(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){Yr(e),this.getMemory(t).addResponse(e)}addFunctionResults(e,t){this.getMemory(t).addFunctionResults(e)}updateResult(e,t){this.getMemory(t).updateResult(e)}addTag(e,t){this.getMemory(t).addTag(e)}rewindToTag(e,t){return this.getMemory(t).rewindToTag(e)}removeByTag(e,t){return this.getMemory(t).removeByTag(e)}history(e,t){return this.getMemory(t).history(e)}getLast(e){return this.getMemory(e).getLast()}reset(e){e?this.memories.set(e,new Ir):this.defaultMemory.reset()}};function Ft(i,e){if(!i&&!e)return;if(!i)return e;if(!e||i===e||i.aborted)return i;if(e.aborted)return e;if(typeof AbortSignal.any=="function")return AbortSignal.any([i,e]);let t=new AbortController,n=()=>{t.abort(i.aborted?i.reason:e.reason),r()},r=()=>{i.removeEventListener("abort",n),e.removeEventListener("abort",n)};return i.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),t.signal}var wt=class extends Error{constructor({message:e}){super(e),this.name="AxAssertionError"}getFixingInstructions=()=>{let e=[],t=this.message.trim();return e.push({name:"error",title:"Follow these instructions",description:t+(t.endsWith(".")?"":".")}),e};toString(){return`${this.name}: ${this.message}`}[Symbol.for("nodejs.util.inspect.custom")](e,t){return this.toString()}},On=async(i,e)=>{for(let t of i){let{fn:n,message:r}=t,o=await n(e);if(o!==void 0){if(typeof o=="string")throw new wt({message:o});if(!o)throw r?new wt({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},ui=async(i,e,t,n=!1)=>{if(!e.currField||e.s===-1||!i||i.length===0)return;let r=i.filter(s=>s.fieldName===e.currField?.name);if(r.length===0)return;let o=t.substring(e.s);for(let s of r){let{message:a,fn:l}=s,u=await l(o,n);if(u!==void 0){if(typeof u=="string")throw new wt({message:u});if(!u&&a)throw new wt({message:a})}}};var ci={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},Cr,pi=i=>{if(Cr)return Cr;let e=i??me.meter;if(e)return Cr=Hc(e),Cr};var fl=()=>{let i=[];return me.meter||i.push("Global meter not initialized"),!Cr&&me.meter&&i.push("Metrics instruments not created despite available meter"),{healthy:i.length===0,issues:i}},Hc=i=>({generationLatencyHistogram:i.createHistogram("ax_gen_generation_duration_ms",{description:"End-to-end duration of AxGen generation requests",unit:"ms"}),generationRequestsCounter:i.createCounter("ax_gen_generation_requests_total",{description:"Total number of AxGen generation requests"}),generationErrorsCounter:i.createCounter("ax_gen_generation_errors_total",{description:"Total number of failed AxGen generations"}),multiStepGenerationsCounter:i.createCounter("ax_gen_multistep_generations_total",{description:"Total number of generations that required multiple steps"}),stepsPerGenerationHistogram:i.createHistogram("ax_gen_steps_per_generation",{description:"Number of steps taken per generation"}),maxStepsReachedCounter:i.createCounter("ax_gen_max_steps_reached_total",{description:"Total number of generations that hit max steps limit"}),validationErrorsCounter:i.createCounter("ax_gen_validation_errors_total",{description:"Total number of validation errors encountered"}),assertionErrorsCounter:i.createCounter("ax_gen_assertion_errors_total",{description:"Total number of assertion errors encountered"}),errorCorrectionAttemptsHistogram:i.createHistogram("ax_gen_error_correction_attempts",{description:"Number of error correction attempts per generation"}),errorCorrectionSuccessCounter:i.createCounter("ax_gen_error_correction_success_total",{description:"Total number of successful error corrections"}),errorCorrectionFailureCounter:i.createCounter("ax_gen_error_correction_failure_total",{description:"Total number of failed error corrections"}),maxRetriesReachedCounter:i.createCounter("ax_gen_max_retries_reached_total",{description:"Total number of generations that hit max retries limit"}),functionsEnabledGenerationsCounter:i.createCounter("ax_gen_functions_enabled_generations_total",{description:"Total number of generations with functions enabled"}),functionCallStepsCounter:i.createCounter("ax_gen_function_call_steps_total",{description:"Total number of steps that included function calls"}),functionsExecutedPerGenerationHistogram:i.createHistogram("ax_gen_functions_executed_per_generation",{description:"Number of unique functions executed per generation"}),functionErrorCorrectionCounter:i.createCounter("ax_gen_function_error_correction_total",{description:"Total number of function-related error corrections"}),fieldProcessorsExecutedCounter:i.createCounter("ax_gen_field_processors_executed_total",{description:"Total number of field processors executed"}),streamingFieldProcessorsExecutedCounter:i.createCounter("ax_gen_streaming_field_processors_executed_total",{description:"Total number of streaming field processors executed"}),streamingGenerationsCounter:i.createCounter("ax_gen_streaming_generations_total",{description:"Total number of streaming generations"}),streamingDeltasEmittedCounter:i.createCounter("ax_gen_streaming_deltas_emitted_total",{description:"Total number of streaming deltas emitted"}),streamingFinalizationLatencyHistogram:i.createHistogram("ax_gen_streaming_finalization_duration_ms",{description:"Duration of streaming response finalization",unit:"ms"}),samplesGeneratedHistogram:i.createHistogram("ax_gen_samples_generated",{description:"Number of samples generated per request"}),resultPickerUsageCounter:i.createCounter("ax_gen_result_picker_usage_total",{description:"Total number of times result picker was used"}),resultPickerLatencyHistogram:i.createHistogram("ax_gen_result_picker_duration_ms",{description:"Duration of result picker execution",unit:"ms"}),inputFieldsGauge:i.createGauge("ax_gen_input_fields",{description:"Number of input fields in signature"}),outputFieldsGauge:i.createGauge("ax_gen_output_fields",{description:"Number of output fields in signature"}),examplesUsedGauge:i.createGauge("ax_gen_examples_used",{description:"Number of examples used in generation"}),demosUsedGauge:i.createGauge("ax_gen_demos_used",{description:"Number of demos used in generation"}),promptRenderLatencyHistogram:i.createHistogram("ax_gen_prompt_render_duration_ms",{description:"Duration of prompt template rendering",unit:"ms"}),extractionLatencyHistogram:i.createHistogram("ax_gen_extraction_duration_ms",{description:"Duration of value extraction from responses",unit:"ms"}),assertionLatencyHistogram:i.createHistogram("ax_gen_assertion_duration_ms",{description:"Duration of assertion checking",unit:"ms"}),stateCreationLatencyHistogram:i.createHistogram("ax_gen_state_creation_duration_ms",{description:"Duration of state creation for multiple samples",unit:"ms"}),memoryUpdateLatencyHistogram:i.createHistogram("ax_gen_memory_update_duration_ms",{description:"Duration of memory updates during generation",unit:"ms"})}),Eo=ci,xl=i=>{Eo={...Eo,...i}},Al=()=>({...Eo}),Ct=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=Eo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var yl=(i,e,t,n,r,o,s)=>{try{let a=Ct({success:t.toString(),...n?{signature:n}:{},...r?{ai_service:r}:{},...o?{model:o}:{},...s});i.generationLatencyHistogram&&i.generationLatencyHistogram.record(e,a),i.generationRequestsCounter&&i.generationRequestsCounter.add(1,a),!t&&i.generationErrorsCounter&&i.generationErrorsCounter.add(1,a)}catch(a){console.warn("Failed to record generation metric:",a)}},Po=(i,e,t,n,r)=>{try{let o=Ct({...n?{signature:n}:{},...r});e>1&&i.multiStepGenerationsCounter&&i.multiStepGenerationsCounter.add(1,o),i.stepsPerGenerationHistogram&&i.stepsPerGenerationHistogram.record(e,o),e>=t&&i.maxStepsReachedCounter&&i.maxStepsReachedCounter.add(1,o)}catch(o){console.warn("Failed to record multi-step metric:",o)}},di=(i,e,t,n)=>{try{let r=Ct({error_type:e,...t?{signature:t}:{},...n});e==="validation"&&i.validationErrorsCounter&&i.validationErrorsCounter.add(1,r),e==="assertion"&&i.assertionErrorsCounter&&i.assertionErrorsCounter.add(1,r)}catch(r){console.warn("Failed to record validation error metric:",r)}},bl=(i,e,t)=>{try{let n=Ct({error_type:"refusal",...e?{signature:e}:{},...t});i.validationErrorsCounter&&i.validationErrorsCounter.add(1,n)}catch(n){console.warn("Failed to record refusal error metric:",n)}},mi=(i,e,t,n,r,o)=>{try{let s=Ct({success:t.toString(),...r?{signature:r}:{},...o});i.errorCorrectionAttemptsHistogram&&i.errorCorrectionAttemptsHistogram.record(e,s),t&&i.errorCorrectionSuccessCounter&&i.errorCorrectionSuccessCounter.add(1,s),t||(i.errorCorrectionFailureCounter&&i.errorCorrectionFailureCounter.add(1,s),e>=n&&i.maxRetriesReachedCounter&&i.maxRetriesReachedCounter.add(1,s))}catch(s){console.warn("Failed to record error correction metric:",s)}},Il=(i,e,t,n,r=!1,o,s)=>{try{let a=Ct({functions_enabled:e.toString(),had_function_calls:n.toString(),...o?{signature:o}:{},...s});e&&i.functionsEnabledGenerationsCounter&&i.functionsEnabledGenerationsCounter.add(1,a),n&&i.functionCallStepsCounter&&i.functionCallStepsCounter.add(1,a),t>0&&i.functionsExecutedPerGenerationHistogram&&i.functionsExecutedPerGenerationHistogram.record(t,a),r&&i.functionErrorCorrectionCounter&&i.functionErrorCorrectionCounter.add(1,a)}catch(a){console.warn("Failed to record function calling metric:",a)}},Cl=(i,e,t,n,r)=>{try{let o=Ct({...n?{signature:n}:{},...r});e>0&&i.fieldProcessorsExecutedCounter&&i.fieldProcessorsExecutedCounter.add(e,o),t>0&&i.streamingFieldProcessorsExecutedCounter&&i.streamingFieldProcessorsExecutedCounter.add(t,o)}catch(o){console.warn("Failed to record field processing metric:",o)}},Tl=(i,e,t,n,r,o)=>{try{let s=Ct({is_streaming:e.toString(),...r?{signature:r}:{},...o});e&&i.streamingGenerationsCounter&&i.streamingGenerationsCounter.add(1,s),t>0&&i.streamingDeltasEmittedCounter&&i.streamingDeltasEmittedCounter.add(t,s),n&&i.streamingFinalizationLatencyHistogram&&i.streamingFinalizationLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record streaming metric:",s)}},wl=(i,e,t,n,r,o)=>{try{let s=Ct({result_picker_used:t.toString(),...r?{signature:r}:{},...o});i.samplesGeneratedHistogram&&i.samplesGeneratedHistogram.record(e,s),t&&i.resultPickerUsageCounter&&i.resultPickerUsageCounter.add(1,s),n&&i.resultPickerLatencyHistogram&&i.resultPickerLatencyHistogram.record(n,s)}catch(s){console.warn("Failed to record samples metric:",s)}},Rl=(i,e,t,n,r,o,s)=>{try{let a=Ct({...o?{signature:o}:{},...s});i.inputFieldsGauge&&i.inputFieldsGauge.record(e,a),i.outputFieldsGauge&&i.outputFieldsGauge.record(t,a),i.examplesUsedGauge&&i.examplesUsedGauge.record(n,a),i.demosUsedGauge&&i.demosUsedGauge.record(r,a)}catch(a){console.warn("Failed to record signature complexity metrics:",a)}},Fo=(i,e,t,n,r)=>{try{let o=Ct({metric_type:e,...n?{signature:n}:{},...r});switch(e){case"prompt_render":i.promptRenderLatencyHistogram&&i.promptRenderLatencyHistogram.record(t,o);break;case"extraction":i.extractionLatencyHistogram&&i.extractionLatencyHistogram.record(t,o);break;case"assertion":i.assertionLatencyHistogram&&i.assertionLatencyHistogram.record(t,o);break;case"state_creation":i.stateCreationLatencyHistogram&&i.stateCreationLatencyHistogram.record(t,o);break;case"memory_update":i.memoryUpdateLatencyHistogram&&i.memoryUpdateLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record performance metric:",o)}};var Mn=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"object":return"object";default:return"string"}})();return i?.isArray?`array of ${e}s`:e},Ke=class extends Error{constructor(e){super(e),this.name="ValidationError"}getFixingInstructions=()=>[{name:"outputError",title:"Invalid Field",description:this.message}];toString(){return`${this.name}: ${this.message}`}[Symbol.for("nodejs.util.inspect.custom")](e,t){return this.toString()}},Sl=i=>{let t=i.map(n=>`'${n.title}' (${Mn(n.type)})`).join(", ");return new Ke(`Required field not found: ${t}. Add a line starting with the exact label followed by a colon (e.g., "${i[0]?.title}:") and then provide a valid ${Mn(i[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},_o=i=>new Ke(`Expected (Required) field not found: '${i.title}'. Begin a new section with "${i.title}:" and then provide a valid ${Mn(i.type)} value directly after.`);var No=i=>new Ke(`Required field is missing: '${i.title}'. After the "${i.title}:" label, provide a non-empty ${Mn(i.type)}. Do not use null, undefined, or leave it blank.`),kl=(i,e)=>new Ke(`Invalid JSON: ${e} in field '${i.title}'. Return only valid JSON. Prefer a fenced code block containing a single JSON object or array with no trailing text.`),vl=(i,e)=>new Ke(`Invalid Array: ${e} for '${i.title}'. Provide a JSON array of ${Mn(i.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),Ol=(i,e,t)=>new Ke(`Field '${i.title}' has an invalid value '${e}': ${t}. Provide a ${Mn(i.type)}. Ensure formatting exactly matches the expected type.`),Ml=(i,e,t)=>new Ke(`Invalid date for '${i.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),El=(i,e,t)=>new Ke(`Invalid date/time for '${i.title}': ${t}. Use the format YYYY-MM-DD HH:mm or YYYY-MM-DD HH:mm:ss followed by a valid timezone (e.g., America/New_York). You provided: ${e}.`),gi=(i,e,t)=>new Ke(`Invalid URL for '${i.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),En=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minLength"?r+=`String must be at least ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="maxLength"?r+=`String must be at most ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="pattern"?r+=`String must match pattern /${n}/. You provided: "${e}".`:t==="format"&&(r+=`String must be a ${n}. You provided: "${e}".`),new Ke(r)},hi=(i,e,t,n)=>{let r=`Field '${i.title}' failed validation: `;return t==="minimum"?r+=`Number must be at least ${n}. You provided: ${e}.`:t==="maximum"&&(r+=`Number must be at most ${n}. You provided: ${e}.`),new Ke(r)};var Pl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";fa(i,e,u,n)}return r&&di(r,"validation",o,a),s&&s.addEvent("validation.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},Fl=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{let l=i.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";xa(i,e,u,n)}return r&&di(r,"assertion",o,a),s&&s.addEvent("assertion.error",{message:i.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},_l=({error:i,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:s,customLabels:a})=>{t&&n&&Aa(i,e,n),r&&bl(r,o,a),s&&s.addEvent("refusal.error",{message:i.toString()})};var $t=er(require("dayjs"),1),Nl=er(require("dayjs/plugin/customParseFormat.js"),1),$l=er(require("dayjs/plugin/timezone.js"),1),Ll=er(require("dayjs/plugin/utc.js"),1);$t.default.extend(Ll.default);$t.default.extend($l.default);$t.default.extend(Nl.default);function Dl(i,e,t=!1){try{return Kc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw Ml(i,e,r)}}function Kc(i){if(!(0,$t.default)(i,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return $t.default.utc(i,"YYYY-MM-DD").startOf("day").toDate()}function Gl(i,e,t=!1){try{return Wc(e)}catch(n){if(i.isOptional&&!t)return;let r=n.message;throw El(i,e,r)}}function Wc(i){let e=/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/,t=i.match(e);if(!t)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');let[,n,r]=t;if(!n||!r)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');try{let o=n.includes(":")&&n.split(":").length===3?"YYYY-MM-DD HH:mm:ss":"YYYY-MM-DD HH:mm",s=$t.default.tz(n,o,r);if(!s.isValid())throw new Error("Invalid date and time values. Please ensure all components are correct.");return s.utc().toDate()}catch{throw new Error(`Unrecognized time zone ${r}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`)}}var Ul=i=>(0,$t.default)(i).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var Hf=new nt,Tr=(i,e)=>{let t=i.type??{name:"string",isArray:!1},n=(u,c)=>{switch(u){case"class":return typeof c=="string";case"code":return typeof c=="string";case"string":return typeof c=="string";case"number":return typeof c=="number";case"boolean":return typeof c=="boolean";case"date":return c instanceof Date||typeof c=="string";case"datetime":return c instanceof Date||typeof c=="string";case"json":return typeof c=="object"||typeof c=="string";case"object":return typeof c=="object";default:return!1}},r=u=>!(!u||typeof u!="object"||!("mimeType"in u)||!("data"in u));if(i.type?.name==="image"){let u;if(Array.isArray(e)){for(let c of e)if(!r(c)){u="object ({ mimeType: string; data: string })";break}}else r(e)||(u="object ({ mimeType: string; data: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let o=u=>!(!u||typeof u!="object"||!("data"in u));if(i.type?.name==="audio"){let u;if(Array.isArray(e)){for(let c of e)if(!o(c)){u="object ({ data: string; format?: string })";break}}else o(e)||(u="object ({ data: string; format?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let s=u=>{if(!u||typeof u!="object"||!("mimeType"in u))return!1;let c="data"in u,p="fileUri"in u;return!(!c&&!p||c&&p)};if(i.type?.name==="file"){let u;if(Array.isArray(e)){for(let c of e)if(!s(c)){u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else s(e)||(u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let a=u=>typeof u=="string"?!0:!(!u||typeof u!="object"||!("url"in u));if(i.type?.name==="url"){let u;if(Array.isArray(e)){for(let c of e)if(!a(c)){u="string or object ({ url: string; title?: string; description?: string })";break}}else a(e)||(u="string or object ({ url: string; title?: string; description?: string })");if(u)throw new Error(`Validation failed: Expected '${i.name}' to be type '${u}' instead got '${e}'`);return}let l=!0;if(t.isArray){if(!Array.isArray(e))l=!1;else for(let u of e)if(!n(t.name,u)){l=!1;break}}else l=n(t.name,e);if(!l){let u=Array.isArray(e)?"array":typeof e;throw new Error(`Validation failed: Expected '${i.name}' to be a ${i.type?.isArray?"an array of ":""}${t.name} instead got '${u}' (${JSON.stringify(e)})`)}};function wr(i){let e={};for(let t of i){let n=`${t.ai}:${t.model}`;if(!e[n]){e[n]={...t};continue}let r=e[n];if(r){let o=r.tokens??{promptTokens:0,completionTokens:0,totalTokens:0};o.promptTokens+=t?.tokens?.promptTokens??0,o.completionTokens+=t?.tokens?.completionTokens??0,o.totalTokens+=t?.tokens?.totalTokens??0,r.tokens=o;let s=r.citations??[],a=t.citations??[];if(a.length){let l=new Set(s.map(u=>u.url));for(let u of a)u?.url&&!l.has(u.url)&&(s.push(u),l.add(u.url));r.citations=s}}}return Object.values(e)}var Bl=i=>{if(!i.trim())return[];let e=new Set(["-","*","+"]),t=/^\d+[\s]*[.)\]]\s*/,n=i.split(`
204
- `),r=[];for(let o of n){let s=o.trim();if(s){if(s[0]&&e.has(s[0]))r.push(s.slice(1).trim());else if(t.test(s))r.push(s.replace(t,"").trim());else if(r.length!==0)throw new Error("Could not parse markdown list: mixed content detected")}}if(r.length===0)throw new Error("Could not parse markdown list: no valid list items found");return r};function xi(i,e){let{index:t,delta:n,version:r}=e,o=i.find(a=>a.index===t)?.delta;if(!o)return i.push({index:t,delta:n,version:r}),i;for(let a of Object.keys(n)){let l=o[a],u=n[a];l===void 0&&Array.isArray(u)?o[a]=[...u]:Array.isArray(l)&&Array.isArray(u)?o[a]=[...l,...u]:(l===void 0||typeof l=="string")&&typeof u=="string"?o[a]=`${l??""}${u}`:o[a]=u}let s=i.find(a=>a.index===t);return s&&(s.version=r),i}var fi=class{cache=new Map;maxSize;constructor(e){this.maxSize=e}get(e){let t=this.cache.get(e);return t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){let n=this.cache.keys().next().value;n&&this.cache.delete(n)}this.cache.set(e,t)}},Vc=new fi(500);function jl(i,e,t=0,n=Vc){if(/^```[a-zA-Z]*\s*$/.test(i))return-4;if(/^[\s`]*$/.test(i))return-3;let r=i.indexOf(e,t);if(r!==-1)return r;let o=n.get(e)??Array.from({length:e.length},(a,l)=>e.slice(0,l+1));n.get(e)||n.set(e,o);let s=-1;for(let a=o.length-1;a>=0;a--){let l=o[a];if(i.endsWith(l)){s=a;break}}return s>=0?-2:-1}function Lt(i,e){if(typeof i!="string")throw gi(e,String(i),"URL must be a string");try{new URL(i)}catch{throw gi(e,i,"Invalid URL format. Expected a valid URL like https://example.com")}}function pt(i,e){if(typeof i!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&i.length<t.minLength)throw En(e,i,"minLength",t.minLength);if(t.maxLength!==void 0&&i.length>t.maxLength)throw En(e,i,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(i))throw En(e,i,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(i))throw En(e,i,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(i)}catch{throw En(e,i,"format","valid URL")}}}function dt(i,e){if(typeof i!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&i<t.minimum)throw hi(e,i,"minimum",t.minimum);if(t.maximum!==void 0&&i>t.maximum)throw hi(e,i,"maximum",t.maximum)}}var Do=(i,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,s=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};bi(i,e,a,t,{strictMode:r,skipEarlyFail:s,treatAllFieldsOptional:o}),Ii(i,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let l of i.getOutputFields())l.isInternal&&delete e[l.name]},Ai=(i,e,t)=>{let n=[];for(let r of t)r&&!r.isOptional&&e[r.name]===void 0&&n.push(r);if(n.length>0)throw Sl(n)},bi=(i,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let s=i.getOutputFields(),a;for(;;){let l=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&l.add(t.currFieldIndex);let u=s.map((g,h)=>({field:g,index:h})).filter(({index:g})=>!l.has(g)),c,p,d=-1,m=0;for(let{index:g,field:h}of u){let C=`${(t.extractedFields.length===0?"":`
205
- `)+h.title}:`,x=jl(n,C,t.s);if(x===-2||x===-3)return!0;if(x===-4)return t.inBlock=!0,!0;x>=0&&(d===-1||x<d)&&(d=x,m=C.length,c=g,p=h)}if(d===-1){if(o)return;if(!r&&t.currField===void 0&&t.extractedFields.length===0&&s.length===1){t.inAssumedField=!0,t.currField=s[0],t.currFieldIndex=0,t.s=0,t.extractedFields.includes(s[0])||t.extractedFields.push(s[0]),t.streamedIndex[s[0].name]===void 0&&(t.streamedIndex[s[0].name]=0);return}if(r&&t.currField===void 0&&t.extractedFields.length===0){let g=s.find(h=>!h.isOptional);if(g)throw _o(g)}break}if(a&&p&&a.name!==p.name)throw _o(a);if(t.currField!==void 0&&t.inAssumedField&&(t.inAssumedField=!1,t.streamedIndex[t.currField.name]=0,t.currField=void 0),t.currField){let g=n.substring(t.s,d).trim(),h=$o(t.currField,g);h!==void 0&&(e[t.currField.name]=h),t.prevFields?t.prevFields?.push({field:t.currField,s:t.s,e:d}):t.prevFields=[{field:t.currField,s:t.s,e:d}]}t.s=d+m,p!==void 0&&c!==void 0&&(t.currField=p,t.currFieldIndex=c),p&&!t.extractedFields.includes(p)&&t.extractedFields.push(p),p&&t.streamedIndex[p.name]===void 0&&(t.streamedIndex[p.name]=0)}},Ii=(i,e,t,n,r)=>{let o=r?.strictMode??!1,s=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,l=r?.forceFinalize??!1;if(t.currField){let u=n.length,c=i.getOutputFields();for(let m of c){if(m.name===t.currField.name)continue;let g=`
206
- ${m.title}:`,h=n.indexOf(g,t.s);h!==-1&&h<u&&(u=h)}let p=n.substring(t.s,u).trim(),d=$o(t.currField,p);d!==void 0&&(e[t.currField.name]=d)}if(o&&!t.currField&&t.extractedFields.length===0&&n.trim()){let p=i.getOutputFields().find(d=>!d.isOptional);if(p)throw _o(p)}if(Jc(i,e,n,t),!s){let u=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?Ai(t,e,i.getOutputFields()):u||Ai(t,e,i.getOutputFields())}},Jc=(i,e,t,n)=>{let r=i.getOutputFields();if(r.length===1){let s=r[0];if(s){let a=`${s.title}:`,l=t.indexOf(a);if(l!==-1){let u=l+a.length,c=`
207
- ${s.title}:`,p=t.indexOf(c,u),d=t.substring(u,p===-1?t.length:p).trim();if(d)try{let m=$o(s,d);if(m!==void 0){e[s.name]=m;return}}catch{}}}}let o=t.split(`
208
- `);for(let s of r){if(s.name in e)continue;let a=`${s.title}:`;for(let l of o){let u=l.trim();if(u.startsWith(a)){let c=u.substring(a.length).trim();if(c)try{let p=$o(s,c);if(p!==void 0){e[s.name]=p;break}}catch(p){if(!s.isOptional)throw p}break}}}},ql=(i,e,t=!1)=>{switch(i.type?.name){case"code":return yi(e);case"string":return e;case"number":{let n=Number(e);if(Number.isNaN(n)){if(i.isOptional&&!t)return;throw new Error("Invalid number")}return n}case"boolean":{if(typeof e=="boolean")return e;let n=e.toLowerCase();if(n==="true")return!0;if(n==="false")return!1;if(i.isOptional&&!t)return;throw new Error("Invalid boolean")}case"date":return Dl(i,e,t);case"datetime":return Gl(i,e,t);case"class":{let n=e;if(i.type.options&&!i.type.options.includes(n)){if(i.isOptional)return;throw new Error(`Invalid class '${e}', expected one of the following: ${i.type.options.join(", ")}`)}return n}default:return e}};function*zl(i,e,t,n,r,o){let{name:s,isInternal:a}=e,{isArray:l,name:u}=e.type??{};if(a||l||u&&u!=="string"&&u!=="code")return;let c=r.streamedIndex[s]??0,p=c===0,d=(t<0?0:t)+c,m=i.substring(d,n);if(m.length===0)return;let g=m.replace(/\s+$/,"");r.currField?.type?.name==="code"&&(g=g.replace(/\s*```\s*$/,""));let h=p?g.trimStart():g;r.currField?.type?.name==="code"&&(h=h.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,"")),h.length>0&&(yield{index:o,delta:{[s]:h}},r.streamedIndex[s]=c+g.length)}function*Ci(i,e,t,n,r){for(let s of n.prevFields??[]){let{field:a,s:l,e:u}=s;yield*zl(e,a,l,u,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(i.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*zl(e,n.currField,n.s,e.length,n,r);let o=i.getOutputFields();for(let s of Object.keys(t)){let a=o.find(c=>c.name===s);if(!a||a.isInternal)continue;let l=t[s];if(Array.isArray(l)){let c=n.streamedIndex?.[s]??0,p=l.slice(c);p&&p.length>0&&(yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=c+p.length);continue}let u=typeof l=="string"?l:void 0;if(!n.streamedIndex[s])yield{index:r,delta:{[s]:l}},n.streamedIndex[s]=u?u.length:1;else if(u){let c=n.streamedIndex[s];if(u.length>c){let p=u.substring(c);yield{index:r,delta:{[s]:p}},n.streamedIndex[s]=u.length}}}}function $o(i,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(i.isOptional)return;throw No(i)}let t;if(i.type?.name==="json"&&!i.type?.isArray)try{let r=yi(e);return t=JSON.parse(r),t}catch(r){throw kl(i,r.message)}if(i.type?.isArray)try{try{t=JSON.parse(e)}catch{t=Bl(e)}if(!Array.isArray(t))throw new Error("Expected an array")}catch(r){let o=r.message;if(o.includes("no valid list items found")||o==="Expected an array")t=[e];else throw vl(i,o)}try{if(Array.isArray(t)){for(let[r,o]of t.entries())if(o!==void 0){let s=typeof o=="string"?o.trim():o;if(typeof s=="string"&&(i.type?.name==="object"||i.type?.name==="json"))try{let a=yi(s);s=JSON.parse(a)}catch{}t[r]=ql(i,s,!0)}}else t=ql(i,e)}catch(r){throw Ol(i,e,r.message)}if(typeof t=="string"&&t==="")return;let n=i.type;if(n&&t!==void 0&&(n.name==="url"&&Lt(t,i),(n.name==="string"||n.name==="code")&&pt(t,i),n.name==="number"&&dt(t,i),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?pt(r,i):n.name==="number"&&dt(r,i));return t}function Pn(i,e,t){let n=i.getOutputFields();for(let r of n){let o=e[r.name];if(o==null){if(!r.isOptional&&!t?.allowMissingRequired)throw No(r);continue}let s=r.type;if(s){if(s.name==="url"&&Lt(o,r),(s.name==="string"||s.name==="code")&&pt(o,r),s.name==="number"&&dt(o,r),s.isArray&&Array.isArray(o))for(let a of o)a!=null&&(s.name==="url"?Lt(a,r):s.name==="string"||s.name==="code"?pt(a,r):s.name==="number"&&dt(a,r));if(s.name==="object"&&s.fields&&typeof o=="object"&&!Array.isArray(o)&&Lo(r,o,t),s.isArray&&s.fields&&Array.isArray(o)&&s.name==="object")for(let a of o)a&&typeof a=="object"&&Lo(r,a,t)}}}function Lo(i,e,t){let n=i.type?.fields;if(!(!n||typeof n!="object"))for(let[r,o]of Object.entries(n)){let s={name:r,title:r,description:o.description,type:{name:o.type,isArray:o.isArray,options:o.options,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,patternDescription:o.patternDescription,format:o.format},isOptional:o.isOptional??!1,isInternal:o.isInternal??!1},a=e[s.name];if(a==null){if(!s.isOptional&&!t?.allowMissingRequired)throw No(s);continue}let l=s.type;if(l){if(l.name==="url"&&Lt(a,s),(l.name==="string"||l.name==="code")&&pt(a,s),l.name==="number"&&dt(a,s),l.isArray&&Array.isArray(a))for(let u of a)u!=null&&(l.name==="url"?Lt(u,s):l.name==="string"||l.name==="code"?pt(u,s):l.name==="number"&&dt(u,s));if(l.name==="object"&&l.fields&&typeof a=="object"&&!Array.isArray(a)&&Lo(s,a,t),l.isArray&&l.fields&&Array.isArray(a)&&l.name==="object")for(let u of a)u&&typeof u=="object"&&Lo(s,u,t)}}}var yi=i=>{let t=/```([A-Za-z]*)\s*([\s\S]*?)\s*```/g.exec(i);return t?t.length===3?t[2]:t.length===2?t[1]:i:i};async function Rr(i,e,t,n){for(let r of i){if(e[r.field.name]===void 0)continue;let o=r.process,s=await o(e[r.field.name],{sessionId:n,values:e,done:!0});Hl(r.field,t,s,n)}}async function Ti(i,e,t,n,r,o,s=!1){for(let a of i){if(t.currField?.name!==a.field.name)continue;let l=e.substring(t.s);t.currField?.type?.name==="code"&&(l=l.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,""),l=l.replace(/\s*```\s*$/,""));let u=a.process,c=await u(l,{sessionId:o,values:r,done:s});Hl(t.currField,n,c,o)}}var Hl=(i,e,t,n)=>{if(t===void 0||typeof t=="string"&&(t===""||/^(null|undefined)\s*$/i.test(t)))return;let r=String(t),o=r;e.addRequest([{role:"user",content:[{type:"text",text:o}]}],n),e.addTag("processor",n)};function Kl(i,e){if(!e)return i;let t=[];if(e.format==="email"&&t.push("Must be a valid email address format"),(e.format==="uri"||e.format==="url"||e.name==="url")&&t.push("Must be a valid URL format"),(e.name==="string"||e.name==="code"||e.name==="url"||e.name==="date"||e.name==="datetime")&&(e.minLength!==void 0&&e.maxLength!==void 0?t.push(`Minimum length: ${e.minLength} characters, maximum length: ${e.maxLength} characters`):e.minLength!==void 0?t.push(`Minimum length: ${e.minLength} characters`):e.maxLength!==void 0&&t.push(`Maximum length: ${e.maxLength} characters`)),e.name==="number"&&(e.minimum!==void 0&&e.maximum!==void 0?t.push(`Minimum value: ${e.minimum}, maximum value: ${e.maximum}`):e.minimum!==void 0?t.push(`Minimum value: ${e.minimum}`):e.maximum!==void 0&&t.push(`Maximum value: ${e.maximum}`)),e.pattern!==void 0){if(!e.patternDescription)throw new Error(`Field with pattern '${e.pattern}' must include a patternDescription to explain the pattern to the LLM`);t.push(e.patternDescription)}if(e.name==="date"&&t.push("Format: YYYY-MM-DD"),e.name==="datetime"&&t.push("Format: ISO 8601 date-time"),t.length===0)return i;let n=t.join(". ");return!i||i.trim().length===0?n:`${i.trim().endsWith(".")?i.trim():`${i.trim()}.`} ${n}`}function Fn(i,e="Schema"){if("name"in i&&"type"in i)return Go(i);let t={},n=[];for(let r of i){if(r.isInternal)continue;let o=Go(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function Go(i,e=!1){let t=i.type,n=Kl(i.description,t);if(e&&t?.name&&(t.name==="image"||t.name==="audio"||t.name==="file"))throw new Error(`Media type '${t.name}' is not allowed in nested object fields. Media types (image, audio, file) can only be used as top-level input fields. Field: ${i.name}`);let r={};if(n&&(r.description=n),t?.isArray)if(r.type="array",t.fields){r.items={type:"object",properties:{},required:[],additionalProperties:!1},t.description&&(r.items.description=t.description);for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.items.properties[o]=Go(a,!0),s.isOptional||r.items.required.push(o)}}else if(t.name==="class"&&t.options)r.items={type:"string",enum:t.options};else{let o=Kl(t.description||i.description,t);r.items={type:Wl(t.name)},o&&(r.items.description=o),t.name==="string"||t.name==="code"||t.name==="url"||t.name==="date"||t.name==="datetime"?(t.minLength!==void 0&&(r.items.minLength=t.minLength),t.maxLength!==void 0&&(r.items.maxLength=t.maxLength),t.pattern!==void 0&&(r.items.pattern=t.pattern),t.format!==void 0&&(r.items.format=t.format)):t.name==="number"&&(t.minimum!==void 0&&(r.items.minimum=t.minimum),t.maximum!==void 0&&(r.items.maximum=t.maximum))}else if(t?.name==="object"&&t.fields){r.type="object",r.properties={},r.required=[],r.additionalProperties=!1;for(let[o,s]of Object.entries(t.fields)){let a={name:o,description:s.description,type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format},isOptional:s.isOptional,isInternal:s.isInternal};r.properties[o]=Go(a,!0),s.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=Wl(t?.name??"string"),t?.name==="string"||t?.name==="code"||t?.name==="url"||t?.name==="date"||t?.name==="datetime"?(t.minLength!==void 0&&(r.minLength=t.minLength),t.maxLength!==void 0&&(r.maxLength=t.maxLength),t.pattern!==void 0&&(r.pattern=t.pattern),t.format!==void 0&&(r.format=t.format),t.name==="url"&&!t.format&&(r.format="uri"),t.name==="date"&&!t.format&&(r.format="date"),t.name==="datetime"&&!t.format&&(r.format="date-time")):t?.name==="number"&&(t.minimum!==void 0&&(r.minimum=t.minimum),t.maximum!==void 0&&(r.maximum=t.maximum)));return r}function Wl(i){switch(i){case"string":case"code":case"url":case"date":case"datetime":case"image":case"audio":case"file":return"string";case"number":return"number";case"boolean":return"boolean";case"json":case"object":return["object","array","string","number","boolean","null"];default:return"string"}}function Uo(i){if(!i||typeof i!="object")throw new Error("Schema must be an object");if(i.type==="array"){if(!i.items)throw new Error('Array schema is missing an "items" definition (required by JSON Schema and all LLM providers for function tools)');Uo(i.items)}else if(i.type==="object"&&i.properties)for(let e of Object.values(i.properties))Uo(e)}var Ht=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},Sr=class extends Error{constructor(t){super();this.fields=t;this.name="AxFunctionError"}getFields=()=>this.fields;toString(){return[`${this.name}: Function validation error`,...this.fields.map(t=>` - ${t.field}: ${t.message}`)].join(`
209
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},kr=class extends Error{constructor(t,n,r){super();this.fields=t;this.func=n;this.funcId=r}getFunctionId=()=>this.funcId;getFieldDescription(t){if(!this.func.parameters?.properties?.[t])return"";let n=this.func.parameters.properties[t],r=n.description;return n.enum?.length&&(r+=` Allowed values are: ${n.enum.join(", ")}`),r}getFixingInstructions=()=>{let t=this.fields.map(n=>{let r=this.getFieldDescription(n.field)||"";return`- \`${n.field}\` - ${n.message} (${r}).`});return`Errors In Function Arguments: Fix the following invalid arguments to '${this.func.name}'
203
+ `,o=u}),(o>t||n.length===0)&&n.push(r.trim()),n},Rp=(s,e=.1)=>{let t=[...s].sort((r,o)=>r.score-o.score),n=Math.ceil(t.length*e);return t.slice(0,n)};var vn=require("@opentelemetry/api");var Ar=class{data=[];seenTags=new Set;addRequest(e,t){this.data.push(...e.map(n=>{let r=structuredClone(n);return{role:n.role,chat:[{index:t,value:r}]}}))}addFunctionResults(e){let t=e.map(({index:r,...o})=>({index:r,value:structuredClone(o)})),n=this.getLast();n?.role==="function"?n.chat.push(...t):this.data.push({role:"function",chat:t})}addResponse(e){let t=e.map(({index:n,...r})=>({index:n,value:structuredClone(r)}));this.data.push({role:"assistant",chat:t})}updateResult({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o,index:i}){let a=this.data.at(-1);if(!a||a.role!=="assistant"||a.role==="assistant"&&!a.updatable){this.data.push({role:"assistant",updatable:!0,chat:[{index:i,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})}]});return}let u=a.chat.find(l=>l.index===i);if(!u){a.chat.push({index:i,value:structuredClone({content:e,name:t,functionCalls:n,thought:r,thoughtBlocks:o})});return}if(typeof e=="string"&&e.trim()!==""&&(u.value.content=e),typeof t=="string"&&t.trim()!==""&&(u.value.name=t),Array.isArray(n)&&n.length>0&&(u.value.functionCalls=n),typeof r=="string"&&r.trim()!==""){let l=u.value.thought;u.value.thought=typeof l=="string"?l+r:r}if(Array.isArray(o)&&o.length>0){let l=u.value.thoughtBlocks??[];for(let c of o){let p=l.length>0?l[l.length-1]:void 0;!c.signature&&c.data?p&&!p.signature?(p.data=(p.data??"")+c.data,c.encrypted&&(p.encrypted=!0)):l.push(structuredClone(c)):c.signature?p&&!p.signature?(p.data=(p.data??"")+c.data,p.signature=c.signature,c.encrypted&&(p.encrypted=!0)):l.push(structuredClone(c)):l.length===0&&l.push(structuredClone(c))}u.value.thoughtBlocks=l,l.length>0&&(u.value.thought=l.map(c=>c.data).join(""))}}addTag(e){let t=this.data.at(-1);t&&(t.tags||(t.tags=[]),t.tags.includes(e)||t.tags.push(e),this.seenTags.add(e))}rewindToTag(e){let t=this.data.findIndex(n=>n.tags?.includes(e));if(t===-1){if(!this.seenTags.has(e))throw new Error(`Tag "${e}" not found`);return[]}return this.data.splice(t)}removeByTag(e){let t=this.data.reduce((n,r,o)=>(r.tags?.includes(e)&&n.push(o),n),[]);return t.length===0?[]:t.reverse().map(n=>this.data.splice(n,1).at(0)).filter(n=>n!==void 0).reverse()}history(e){let t=[];for(let{role:n,chat:r}of this.data){let o;n==="function"?o=r.filter(i=>i.index===e).map(i=>i.value):o=r.find(i=>i.index===e)?.value,Array.isArray(o)&&o.length>0?t.push(...o.map(i=>({...i,role:n}))):typeof o=="object"&&o!==null&&t.push({...o,role:n})}return t}getLast(){return this.data.at(-1)}reset(){this.data=[],this.seenTags=new Set}},xn=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new Ar}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new Ar),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)qt(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){Vr(e),this.getMemory(t).addResponse(e)}addFunctionResults(e,t){this.getMemory(t).addFunctionResults(e)}updateResult(e,t){this.getMemory(t).updateResult(e)}addTag(e,t){this.getMemory(t).addTag(e)}rewindToTag(e,t){return this.getMemory(t).rewindToTag(e)}removeByTag(e,t){return this.getMemory(t).removeByTag(e)}history(e,t){return this.getMemory(t).history(e)}getLast(e){return this.getMemory(e).getLast()}reset(e){e?this.memories.set(e,new Ar):this.defaultMemory.reset()}};function At(s,e){if(!s&&!e)return;if(!s)return e;if(!e||s===e||s.aborted)return s;if(e.aborted)return e;if(typeof AbortSignal.any=="function")return AbortSignal.any([s,e]);let t=new AbortController,n=()=>{t.abort(s.aborted?s.reason:e.reason),r()},r=()=>{s.removeEventListener("abort",n),e.removeEventListener("abort",n)};return s.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),t.signal}var ct=class extends Error{constructor({message:e}){super(e),this.name="AxAssertionError"}getFixingInstructions=()=>{let e=[],t=this.message.trim();return e.push({name:"error",title:"Follow these instructions",description:t+(t.endsWith(".")?"":".")}),e};toString(){return`${this.name}: ${this.message}`}[Symbol.for("nodejs.util.inspect.custom")](e,t){return this.toString()}},An=async(s,e)=>{for(let t of s){let{fn:n,message:r}=t,o=await n(e);if(o!==void 0){if(typeof o=="string")throw new ct({message:o});if(!o)throw r?new ct({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},li=async(s,e,t,n=!1)=>{if(!e.currField||e.s===-1||!s||s.length===0)return;let r=s.filter(i=>i.fieldName===e.currField?.name);if(r.length===0)return;let o=t.substring(e.s);for(let i of r){let{message:a,fn:u}=i,l=await u(o,n);if(l!==void 0){if(typeof l=="string")throw new ct({message:l});if(!l&&a)throw new ct({message:a})}}};var ci={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},yr,pi=s=>{if(yr)return yr;let e=s??re.meter;if(e)return yr=kp(e),yr};var Su=()=>{let s=[];return re.meter||s.push("Global meter not initialized"),!yr&&re.meter&&s.push("Metrics instruments not created despite available meter"),{healthy:s.length===0,issues:s}},kp=s=>({generationLatencyHistogram:s.createHistogram("ax_gen_generation_duration_ms",{description:"End-to-end duration of AxGen generation requests",unit:"ms"}),generationRequestsCounter:s.createCounter("ax_gen_generation_requests_total",{description:"Total number of AxGen generation requests"}),generationErrorsCounter:s.createCounter("ax_gen_generation_errors_total",{description:"Total number of failed AxGen generations"}),multiStepGenerationsCounter:s.createCounter("ax_gen_multistep_generations_total",{description:"Total number of generations that required multiple steps"}),stepsPerGenerationHistogram:s.createHistogram("ax_gen_steps_per_generation",{description:"Number of steps taken per generation"}),maxStepsReachedCounter:s.createCounter("ax_gen_max_steps_reached_total",{description:"Total number of generations that hit max steps limit"}),validationErrorsCounter:s.createCounter("ax_gen_validation_errors_total",{description:"Total number of validation errors encountered"}),assertionErrorsCounter:s.createCounter("ax_gen_assertion_errors_total",{description:"Total number of assertion errors encountered"}),errorCorrectionAttemptsHistogram:s.createHistogram("ax_gen_error_correction_attempts",{description:"Number of error correction attempts per generation"}),errorCorrectionSuccessCounter:s.createCounter("ax_gen_error_correction_success_total",{description:"Total number of successful error corrections"}),errorCorrectionFailureCounter:s.createCounter("ax_gen_error_correction_failure_total",{description:"Total number of failed error corrections"}),maxRetriesReachedCounter:s.createCounter("ax_gen_max_retries_reached_total",{description:"Total number of generations that hit max retries limit"}),functionsEnabledGenerationsCounter:s.createCounter("ax_gen_functions_enabled_generations_total",{description:"Total number of generations with functions enabled"}),functionCallStepsCounter:s.createCounter("ax_gen_function_call_steps_total",{description:"Total number of steps that included function calls"}),functionsExecutedPerGenerationHistogram:s.createHistogram("ax_gen_functions_executed_per_generation",{description:"Number of unique functions executed per generation"}),functionErrorCorrectionCounter:s.createCounter("ax_gen_function_error_correction_total",{description:"Total number of function-related error corrections"}),fieldProcessorsExecutedCounter:s.createCounter("ax_gen_field_processors_executed_total",{description:"Total number of field processors executed"}),streamingFieldProcessorsExecutedCounter:s.createCounter("ax_gen_streaming_field_processors_executed_total",{description:"Total number of streaming field processors executed"}),streamingGenerationsCounter:s.createCounter("ax_gen_streaming_generations_total",{description:"Total number of streaming generations"}),streamingDeltasEmittedCounter:s.createCounter("ax_gen_streaming_deltas_emitted_total",{description:"Total number of streaming deltas emitted"}),streamingFinalizationLatencyHistogram:s.createHistogram("ax_gen_streaming_finalization_duration_ms",{description:"Duration of streaming response finalization",unit:"ms"}),samplesGeneratedHistogram:s.createHistogram("ax_gen_samples_generated",{description:"Number of samples generated per request"}),resultPickerUsageCounter:s.createCounter("ax_gen_result_picker_usage_total",{description:"Total number of times result picker was used"}),resultPickerLatencyHistogram:s.createHistogram("ax_gen_result_picker_duration_ms",{description:"Duration of result picker execution",unit:"ms"}),inputFieldsGauge:s.createGauge("ax_gen_input_fields",{description:"Number of input fields in signature"}),outputFieldsGauge:s.createGauge("ax_gen_output_fields",{description:"Number of output fields in signature"}),examplesUsedGauge:s.createGauge("ax_gen_examples_used",{description:"Number of examples used in generation"}),demosUsedGauge:s.createGauge("ax_gen_demos_used",{description:"Number of demos used in generation"}),promptRenderLatencyHistogram:s.createHistogram("ax_gen_prompt_render_duration_ms",{description:"Duration of prompt template rendering",unit:"ms"}),extractionLatencyHistogram:s.createHistogram("ax_gen_extraction_duration_ms",{description:"Duration of value extraction from responses",unit:"ms"}),assertionLatencyHistogram:s.createHistogram("ax_gen_assertion_duration_ms",{description:"Duration of assertion checking",unit:"ms"}),stateCreationLatencyHistogram:s.createHistogram("ax_gen_state_creation_duration_ms",{description:"Duration of state creation for multiple samples",unit:"ms"}),memoryUpdateLatencyHistogram:s.createHistogram("ax_gen_memory_update_duration_ms",{description:"Duration of memory updates during generation",unit:"ms"})}),vo=ci,vu=s=>{vo={...vo,...s}},Ou=()=>({...vo}),at=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=vo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var Eu=(s,e,t,n,r,o,i)=>{try{let a=at({success:t.toString(),...n?{signature:n}:{},...r?{ai_service:r}:{},...o?{model:o}:{},...i});s.generationLatencyHistogram&&s.generationLatencyHistogram.record(e,a),s.generationRequestsCounter&&s.generationRequestsCounter.add(1,a),!t&&s.generationErrorsCounter&&s.generationErrorsCounter.add(1,a)}catch(a){console.warn("Failed to record generation metric:",a)}},Oo=(s,e,t,n,r)=>{try{let o=at({...n?{signature:n}:{},...r});e>1&&s.multiStepGenerationsCounter&&s.multiStepGenerationsCounter.add(1,o),s.stepsPerGenerationHistogram&&s.stepsPerGenerationHistogram.record(e,o),e>=t&&s.maxStepsReachedCounter&&s.maxStepsReachedCounter.add(1,o)}catch(o){console.warn("Failed to record multi-step metric:",o)}},di=(s,e,t,n)=>{try{let r=at({error_type:e,...t?{signature:t}:{},...n});e==="validation"&&s.validationErrorsCounter&&s.validationErrorsCounter.add(1,r),e==="assertion"&&s.assertionErrorsCounter&&s.assertionErrorsCounter.add(1,r)}catch(r){console.warn("Failed to record validation error metric:",r)}},Mu=(s,e,t)=>{try{let n=at({error_type:"refusal",...e?{signature:e}:{},...t});s.validationErrorsCounter&&s.validationErrorsCounter.add(1,n)}catch(n){console.warn("Failed to record refusal error metric:",n)}},mi=(s,e,t,n,r,o)=>{try{let i=at({success:t.toString(),...r?{signature:r}:{},...o});s.errorCorrectionAttemptsHistogram&&s.errorCorrectionAttemptsHistogram.record(e,i),t&&s.errorCorrectionSuccessCounter&&s.errorCorrectionSuccessCounter.add(1,i),t||(s.errorCorrectionFailureCounter&&s.errorCorrectionFailureCounter.add(1,i),e>=n&&s.maxRetriesReachedCounter&&s.maxRetriesReachedCounter.add(1,i))}catch(i){console.warn("Failed to record error correction metric:",i)}},Pu=(s,e,t,n,r=!1,o,i)=>{try{let a=at({functions_enabled:e.toString(),had_function_calls:n.toString(),...o?{signature:o}:{},...i});e&&s.functionsEnabledGenerationsCounter&&s.functionsEnabledGenerationsCounter.add(1,a),n&&s.functionCallStepsCounter&&s.functionCallStepsCounter.add(1,a),t>0&&s.functionsExecutedPerGenerationHistogram&&s.functionsExecutedPerGenerationHistogram.record(t,a),r&&s.functionErrorCorrectionCounter&&s.functionErrorCorrectionCounter.add(1,a)}catch(a){console.warn("Failed to record function calling metric:",a)}},Fu=(s,e,t,n,r)=>{try{let o=at({...n?{signature:n}:{},...r});e>0&&s.fieldProcessorsExecutedCounter&&s.fieldProcessorsExecutedCounter.add(e,o),t>0&&s.streamingFieldProcessorsExecutedCounter&&s.streamingFieldProcessorsExecutedCounter.add(t,o)}catch(o){console.warn("Failed to record field processing metric:",o)}},_u=(s,e,t,n,r,o)=>{try{let i=at({is_streaming:e.toString(),...r?{signature:r}:{},...o});e&&s.streamingGenerationsCounter&&s.streamingGenerationsCounter.add(1,i),t>0&&s.streamingDeltasEmittedCounter&&s.streamingDeltasEmittedCounter.add(t,i),n&&s.streamingFinalizationLatencyHistogram&&s.streamingFinalizationLatencyHistogram.record(n,i)}catch(i){console.warn("Failed to record streaming metric:",i)}},Nu=(s,e,t,n,r,o)=>{try{let i=at({result_picker_used:t.toString(),...r?{signature:r}:{},...o});s.samplesGeneratedHistogram&&s.samplesGeneratedHistogram.record(e,i),t&&s.resultPickerUsageCounter&&s.resultPickerUsageCounter.add(1,i),n&&s.resultPickerLatencyHistogram&&s.resultPickerLatencyHistogram.record(n,i)}catch(i){console.warn("Failed to record samples metric:",i)}},Lu=(s,e,t,n,r,o,i)=>{try{let a=at({...o?{signature:o}:{},...i});s.inputFieldsGauge&&s.inputFieldsGauge.record(e,a),s.outputFieldsGauge&&s.outputFieldsGauge.record(t,a),s.examplesUsedGauge&&s.examplesUsedGauge.record(n,a),s.demosUsedGauge&&s.demosUsedGauge.record(r,a)}catch(a){console.warn("Failed to record signature complexity metrics:",a)}},Eo=(s,e,t,n,r)=>{try{let o=at({metric_type:e,...n?{signature:n}:{},...r});switch(e){case"prompt_render":s.promptRenderLatencyHistogram&&s.promptRenderLatencyHistogram.record(t,o);break;case"extraction":s.extractionLatencyHistogram&&s.extractionLatencyHistogram.record(t,o);break;case"assertion":s.assertionLatencyHistogram&&s.assertionLatencyHistogram.record(t,o);break;case"state_creation":s.stateCreationLatencyHistogram&&s.stateCreationLatencyHistogram.record(t,o);break;case"memory_update":s.memoryUpdateLatencyHistogram&&s.memoryUpdateLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record performance metric:",o)}};var yn=s=>{let e=(()=>{switch(s?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"object":return"object";default:return"string"}})();return s?.isArray?`array of ${e}s`:e},$e=class extends Error{constructor(e){super(e),this.name="ValidationError"}getFixingInstructions=()=>[{name:"outputError",title:"Invalid Field",description:this.message}];toString(){return`${this.name}: ${this.message}`}[Symbol.for("nodejs.util.inspect.custom")](e,t){return this.toString()}},$u=s=>{let t=s.map(n=>`'${n.title}' (${yn(n.type)})`).join(", ");return new $e(`Required field not found: ${t}. Add a line starting with the exact label followed by a colon (e.g., "${s[0]?.title}:") and then provide a valid ${yn(s[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},Mo=s=>new $e(`Expected (Required) field not found: '${s.title}'. Begin a new section with "${s.title}:" and then provide a valid ${yn(s.type)} value directly after.`);var Po=s=>new $e(`Required field is missing: '${s.title}'. After the "${s.title}:" label, provide a non-empty ${yn(s.type)}. Do not use null, undefined, or leave it blank.`),Du=(s,e)=>new $e(`Invalid JSON: ${e} in field '${s.title}'. Return only valid JSON. Prefer a fenced code block containing a single JSON object or array with no trailing text.`),Gu=(s,e)=>new $e(`Invalid Array: ${e} for '${s.title}'. Provide a JSON array of ${yn(s.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),Uu=(s,e,t)=>new $e(`Field '${s.title}' has an invalid value '${e}': ${t}. Provide a ${yn(s.type)}. Ensure formatting exactly matches the expected type.`),Bu=(s,e,t)=>new $e(`Invalid date for '${s.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),ju=(s,e,t)=>new $e(`Invalid date/time for '${s.title}': ${t}. Use the format YYYY-MM-DD HH:mm or YYYY-MM-DD HH:mm:ss followed by a valid timezone (e.g., America/New_York). You provided: ${e}.`),gi=(s,e,t)=>new $e(`Invalid URL for '${s.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),bn=(s,e,t,n)=>{let r=`Field '${s.title}' failed validation: `;return t==="minLength"?r+=`String must be at least ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="maxLength"?r+=`String must be at most ${n} characters long. You provided: "${e}" (${e.length} characters).`:t==="pattern"?r+=`String must match pattern /${n}/. You provided: "${e}".`:t==="format"&&(r+=`String must be a ${n}. You provided: "${e}".`),new $e(r)},fi=(s,e,t,n)=>{let r=`Field '${s.title}' failed validation: `;return t==="minimum"?r+=`Number must be at least ${n}. You provided: ${e}.`:t==="maximum"&&(r+=`Number must be at most ${n}. You provided: ${e}.`),new $e(r)};var zu=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let u=s.getFixingInstructions();if(t&&n){let l=u?.map(c=>c.title).join(", ")??"";Sa(s,e,l,n)}return r&&di(r,"validation",o,a),i&&i.addEvent("validation.error",{message:s.toString(),fixing_instructions:u?.map(l=>l.title).join(", ")??""}),u},qu=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let u=s.getFixingInstructions();if(t&&n){let l=u?.map(c=>c.title).join(", ")??"";va(s,e,l,n)}return r&&di(r,"assertion",o,a),i&&i.addEvent("assertion.error",{message:s.toString(),fixing_instructions:u?.map(l=>l.title).join(", ")??""}),u},Hu=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{t&&n&&Oa(s,e,n),r&&Mu(r,o,a),i&&i.addEvent("refusal.error",{message:s.toString()})};var Rt=Yn(require("dayjs"),1),Ku=Yn(require("dayjs/plugin/customParseFormat.js"),1),Wu=Yn(require("dayjs/plugin/timezone.js"),1),Vu=Yn(require("dayjs/plugin/utc.js"),1);Rt.default.extend(Vu.default);Rt.default.extend(Wu.default);Rt.default.extend(Ku.default);function Ju(s,e,t=!1){try{return Sp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw Bu(s,e,r)}}function Sp(s){if(!(0,Rt.default)(s,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return Rt.default.utc(s,"YYYY-MM-DD").startOf("day").toDate()}function Qu(s,e,t=!1){try{return vp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw ju(s,e,r)}}function vp(s){let e=/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/,t=s.match(e);if(!t)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');let[,n,r]=t;if(!n||!r)throw new Error('Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.');try{let o=n.includes(":")&&n.split(":").length===3?"YYYY-MM-DD HH:mm:ss":"YYYY-MM-DD HH:mm",i=Rt.default.tz(n,o,r);if(!i.isValid())throw new Error("Invalid date and time values. Please ensure all components are correct.");return i.utc().toDate()}catch{throw new Error(`Unrecognized time zone ${r}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`)}}var Yu=s=>(0,Rt.default)(s).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var Xx=new ze,br=(s,e)=>{let t=s.type??{name:"string",isArray:!1},n=(l,c)=>{switch(l){case"class":return typeof c=="string";case"code":return typeof c=="string";case"string":return typeof c=="string";case"number":return typeof c=="number";case"boolean":return typeof c=="boolean";case"date":return c instanceof Date||typeof c=="string";case"datetime":return c instanceof Date||typeof c=="string";case"json":return typeof c=="object"||typeof c=="string";case"object":return typeof c=="object";default:return!1}},r=l=>!(!l||typeof l!="object"||!("mimeType"in l)||!("data"in l));if(s.type?.name==="image"){let l;if(Array.isArray(e)){for(let c of e)if(!r(c)){l="object ({ mimeType: string; data: string })";break}}else r(e)||(l="object ({ mimeType: string; data: string })");if(l)throw new Error(`Validation failed: Expected '${s.name}' to be type '${l}' instead got '${e}'`);return}let o=l=>!(!l||typeof l!="object"||!("data"in l));if(s.type?.name==="audio"){let l;if(Array.isArray(e)){for(let c of e)if(!o(c)){l="object ({ data: string; format?: string })";break}}else o(e)||(l="object ({ data: string; format?: string })");if(l)throw new Error(`Validation failed: Expected '${s.name}' to be type '${l}' instead got '${e}'`);return}let i=l=>{if(!l||typeof l!="object"||!("mimeType"in l))return!1;let c="data"in l,p="fileUri"in l;return!(!c&&!p||c&&p)};if(s.type?.name==="file"){let l;if(Array.isArray(e)){for(let c of e)if(!i(c)){l="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else i(e)||(l="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(l)throw new Error(`Validation failed: Expected '${s.name}' to be type '${l}' instead got '${e}'`);return}let a=l=>typeof l=="string"?!0:!(!l||typeof l!="object"||!("url"in l));if(s.type?.name==="url"){let l;if(Array.isArray(e)){for(let c of e)if(!a(c)){l="string or object ({ url: string; title?: string; description?: string })";break}}else a(e)||(l="string or object ({ url: string; title?: string; description?: string })");if(l)throw new Error(`Validation failed: Expected '${s.name}' to be type '${l}' instead got '${e}'`);return}let u=!0;if(t.isArray){if(!Array.isArray(e))u=!1;else for(let l of e)if(!n(t.name,l)){u=!1;break}}else u=n(t.name,e);if(!u){let l=Array.isArray(e)?"array":typeof e;throw new Error(`Validation failed: Expected '${s.name}' to be a ${s.type?.isArray?"an array of ":""}${t.name} instead got '${l}' (${JSON.stringify(e)})`)}};function Ir(s){let e={};for(let t of s){let n=`${t.ai}:${t.model}`;if(!e[n]){e[n]={...t};continue}let r=e[n];if(r){let o=r.tokens??{promptTokens:0,completionTokens:0,totalTokens:0};o.promptTokens+=t?.tokens?.promptTokens??0,o.completionTokens+=t?.tokens?.completionTokens??0,o.totalTokens+=t?.tokens?.totalTokens??0,r.tokens=o;let i=r.citations??[],a=t.citations??[];if(a.length){let u=new Set(i.map(l=>l.url));for(let l of a)l?.url&&!u.has(l.url)&&(i.push(l),u.add(l.url));r.citations=i}}}return Object.values(e)}var Xu=s=>{if(!s.trim())return[];let e=new Set(["-","*","+"]),t=/^\d+[\s]*[.)\]]\s*/,n=s.split(`
204
+ `),r=[];for(let o of n){let i=o.trim();if(i){if(i[0]&&e.has(i[0]))r.push(i.slice(1).trim());else if(t.test(i))r.push(i.replace(t,"").trim());else if(r.length!==0)throw new Error("Could not parse markdown list: mixed content detected")}}if(r.length===0)throw new Error("Could not parse markdown list: no valid list items found");return r};function xi(s,e){let{index:t,delta:n,version:r}=e,o=s.find(a=>a.index===t)?.delta;if(!o)return s.push({index:t,delta:n,version:r}),s;for(let a of Object.keys(n)){let u=o[a],l=n[a];u===void 0&&Array.isArray(l)?o[a]=[...l]:Array.isArray(u)&&Array.isArray(l)?o[a]=[...u,...l]:(u===void 0||typeof u=="string")&&typeof l=="string"?o[a]=`${u??""}${l}`:o[a]=l}let i=s.find(a=>a.index===t);return i&&(i.version=r),s}var hi=class{cache=new Map;maxSize;constructor(e){this.maxSize=e}get(e){let t=this.cache.get(e);return t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){let n=this.cache.keys().next().value;n&&this.cache.delete(n)}this.cache.set(e,t)}},Op=new hi(500);function Zu(s,e,t=0,n=Op){if(/^```[a-zA-Z]*\s*$/.test(s))return-4;if(/^[\s`]*$/.test(s))return-3;let r=s.indexOf(e,t);if(r!==-1)return r;let o=n.get(e)??Array.from({length:e.length},(a,u)=>e.slice(0,u+1));n.get(e)||n.set(e,o);let i=-1;for(let a=o.length-1;a>=0;a--){let u=o[a];if(s.endsWith(u)){i=a;break}}return i>=0?-2:-1}function kt(s,e){if(typeof s!="string")throw gi(e,String(s),"URL must be a string");try{new URL(s)}catch{throw gi(e,s,"Invalid URL format. Expected a valid URL like https://example.com")}}function Ye(s,e){if(typeof s!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&s.length<t.minLength)throw bn(e,s,"minLength",t.minLength);if(t.maxLength!==void 0&&s.length>t.maxLength)throw bn(e,s,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(s))throw bn(e,s,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s))throw bn(e,s,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(s)}catch{throw bn(e,s,"format","valid URL")}}}function Xe(s,e){if(typeof s!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&s<t.minimum)throw fi(e,s,"minimum",t.minimum);if(t.maximum!==void 0&&s>t.maximum)throw fi(e,s,"maximum",t.maximum)}}var No=(s,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,i=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};bi(s,e,a,t,{strictMode:r,skipEarlyFail:i,treatAllFieldsOptional:o}),Ii(s,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let u of s.getOutputFields())u.isInternal&&delete e[u.name]},Ai=(s,e,t)=>{let n=[];for(let r of t)r&&!r.isOptional&&e[r.name]===void 0&&n.push(r);if(n.length>0)throw $u(n)},bi=(s,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let i=s.getOutputFields(),a;for(;;){let u=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&u.add(t.currFieldIndex);let l=i.map((g,f)=>({field:g,index:f})).filter(({index:g})=>!u.has(g)),c,p,d=-1,m=0;for(let{index:g,field:f}of l){let y=`${(t.extractedFields.length===0?"":`
205
+ `)+f.title}:`,h=Zu(n,y,t.s);if(h===-2||h===-3)return!0;if(h===-4)return t.inBlock=!0,!0;h>=0&&(d===-1||h<d)&&(d=h,m=y.length,c=g,p=f)}if(d===-1){if(o)return;if(!r&&t.currField===void 0&&t.extractedFields.length===0&&i.length===1){t.inAssumedField=!0,t.currField=i[0],t.currFieldIndex=0,t.s=0,t.extractedFields.includes(i[0])||t.extractedFields.push(i[0]),t.streamedIndex[i[0].name]===void 0&&(t.streamedIndex[i[0].name]=0);return}if(r&&t.currField===void 0&&t.extractedFields.length===0){let g=i.find(f=>!f.isOptional);if(g)throw Mo(g)}break}if(a&&p&&a.name!==p.name)throw Mo(a);if(t.currField!==void 0&&t.inAssumedField&&(t.inAssumedField=!1,t.streamedIndex[t.currField.name]=0,t.currField=void 0),t.currField){let g=n.substring(t.s,d).trim(),f=Fo(t.currField,g);f!==void 0&&(e[t.currField.name]=f),t.prevFields?t.prevFields?.push({field:t.currField,s:t.s,e:d}):t.prevFields=[{field:t.currField,s:t.s,e:d}]}t.s=d+m,p!==void 0&&c!==void 0&&(t.currField=p,t.currFieldIndex=c),p&&!t.extractedFields.includes(p)&&t.extractedFields.push(p),p&&t.streamedIndex[p.name]===void 0&&(t.streamedIndex[p.name]=0)}},Ii=(s,e,t,n,r)=>{let o=r?.strictMode??!1,i=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,u=r?.forceFinalize??!1;if(t.currField){let l=n.length,c=s.getOutputFields();for(let m of c){if(m.name===t.currField.name)continue;let g=`
206
+ ${m.title}:`,f=n.indexOf(g,t.s);f!==-1&&f<l&&(l=f)}let p=n.substring(t.s,l).trim(),d=Fo(t.currField,p);d!==void 0&&(e[t.currField.name]=d)}if(o&&!t.currField&&t.extractedFields.length===0&&n.trim()){let p=s.getOutputFields().find(d=>!d.isOptional);if(p)throw Mo(p)}if(Ep(s,e,n,t),!i){let l=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||u?Ai(t,e,s.getOutputFields()):l||Ai(t,e,s.getOutputFields())}},Ep=(s,e,t,n)=>{let r=s.getOutputFields();if(r.length===1){let i=r[0];if(i){let a=`${i.title}:`,u=t.indexOf(a);if(u!==-1){let l=u+a.length,c=`
207
+ ${i.title}:`,p=t.indexOf(c,l),d=t.substring(l,p===-1?t.length:p).trim();if(d)try{let m=Fo(i,d);if(m!==void 0){e[i.name]=m;return}}catch{}}}}let o=t.split(`
208
+ `);for(let i of r){if(i.name in e)continue;let a=`${i.title}:`;for(let u of o){let l=u.trim();if(l.startsWith(a)){let c=l.substring(a.length).trim();if(c)try{let p=Fo(i,c);if(p!==void 0){e[i.name]=p;break}}catch(p){if(!i.isOptional)throw p}break}}}},el=(s,e,t=!1)=>{switch(s.type?.name){case"code":return yi(e);case"string":return e;case"number":{let n=Number(e);if(Number.isNaN(n)){if(s.isOptional&&!t)return;throw new Error("Invalid number")}return n}case"boolean":{if(typeof e=="boolean")return e;let n=e.toLowerCase();if(n==="true")return!0;if(n==="false")return!1;if(s.isOptional&&!t)return;throw new Error("Invalid boolean")}case"date":return Ju(s,e,t);case"datetime":return Qu(s,e,t);case"class":{let n=e;if(s.type.options&&!s.type.options.includes(n)){if(s.isOptional)return;throw new Error(`Invalid class '${e}', expected one of the following: ${s.type.options.join(", ")}`)}return n}default:return e}};function*tl(s,e,t,n,r,o){let{name:i,isInternal:a}=e,{isArray:u,name:l}=e.type??{};if(a||u||l&&l!=="string"&&l!=="code")return;let c=r.streamedIndex[i]??0,p=c===0,d=(t<0?0:t)+c,m=s.substring(d,n);if(m.length===0)return;let g=m.replace(/\s+$/,"");r.currField?.type?.name==="code"&&(g=g.replace(/\s*```\s*$/,""));let f=p?g.trimStart():g;r.currField?.type?.name==="code"&&(f=f.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,"")),f.length>0&&(yield{index:o,delta:{[i]:f}},r.streamedIndex[i]=c+g.length)}function*Ci(s,e,t,n,r){for(let i of n.prevFields??[]){let{field:a,s:u,e:l}=i;yield*tl(e,a,u,l,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(s.getOutputFields().filter(u=>!u.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*tl(e,n.currField,n.s,e.length,n,r);let o=s.getOutputFields();for(let i of Object.keys(t)){let a=o.find(c=>c.name===i);if(!a||a.isInternal)continue;let u=t[i];if(Array.isArray(u)){let c=n.streamedIndex?.[i]??0,p=u.slice(c);p&&p.length>0&&(yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=c+p.length);continue}let l=typeof u=="string"?u:void 0;if(!n.streamedIndex[i])yield{index:r,delta:{[i]:u}},n.streamedIndex[i]=l?l.length:1;else if(l){let c=n.streamedIndex[i];if(l.length>c){let p=l.substring(c);yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=l.length}}}}function Fo(s,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(s.isOptional)return;throw Po(s)}let t;if(s.type?.name==="json"&&!s.type?.isArray)try{let r=yi(e);return t=JSON.parse(r),t}catch(r){throw Du(s,r.message)}if(s.type?.isArray)try{try{t=JSON.parse(e)}catch{t=Xu(e)}if(!Array.isArray(t))throw new Error("Expected an array")}catch(r){let o=r.message;if(o.includes("no valid list items found")||o==="Expected an array")t=[e];else throw Gu(s,o)}try{if(Array.isArray(t)){for(let[r,o]of t.entries())if(o!==void 0){let i=typeof o=="string"?o.trim():o;if(typeof i=="string"&&(s.type?.name==="object"||s.type?.name==="json"))try{let a=yi(i);i=JSON.parse(a)}catch{}t[r]=el(s,i,!0)}}else t=el(s,e)}catch(r){throw Uu(s,e,r.message)}if(typeof t=="string"&&t==="")return;let n=s.type;if(n&&t!==void 0&&(n.name==="url"&&kt(t,s),(n.name==="string"||n.name==="code")&&Ye(t,s),n.name==="number"&&Xe(t,s),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?Ye(r,s):n.name==="number"&&Xe(r,s));return t}function In(s,e,t){let n=s.getOutputFields();for(let r of n){let o=e[r.name];if(o==null){if(!r.isOptional&&!t?.allowMissingRequired)throw Po(r);continue}let i=r.type;if(i){if(i.name==="url"&&kt(o,r),(i.name==="string"||i.name==="code")&&Ye(o,r),i.name==="number"&&Xe(o,r),i.isArray&&Array.isArray(o))for(let a of o)a!=null&&(i.name==="url"?kt(a,r):i.name==="string"||i.name==="code"?Ye(a,r):i.name==="number"&&Xe(a,r));if(i.name==="object"&&i.fields&&typeof o=="object"&&!Array.isArray(o)&&_o(r,o,t),i.isArray&&i.fields&&Array.isArray(o)&&i.name==="object")for(let a of o)a&&typeof a=="object"&&_o(r,a,t)}}}function _o(s,e,t){let n=s.type?.fields;if(!(!n||typeof n!="object"))for(let[r,o]of Object.entries(n)){let i={name:r,title:r,description:o.description,type:{name:o.type,isArray:o.isArray,options:o.options,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,patternDescription:o.patternDescription,format:o.format},isOptional:o.isOptional??!1,isInternal:o.isInternal??!1},a=e[i.name];if(a==null){if(!i.isOptional&&!t?.allowMissingRequired)throw Po(i);continue}let u=i.type;if(u){if(u.name==="url"&&kt(a,i),(u.name==="string"||u.name==="code")&&Ye(a,i),u.name==="number"&&Xe(a,i),u.isArray&&Array.isArray(a))for(let l of a)l!=null&&(u.name==="url"?kt(l,i):u.name==="string"||u.name==="code"?Ye(l,i):u.name==="number"&&Xe(l,i));if(u.name==="object"&&u.fields&&typeof a=="object"&&!Array.isArray(a)&&_o(i,a,t),u.isArray&&u.fields&&Array.isArray(a)&&u.name==="object")for(let l of a)l&&typeof l=="object"&&_o(i,l,t)}}}var yi=s=>{let t=/```([A-Za-z]*)\s*([\s\S]*?)\s*```/g.exec(s);return t?t.length===3?t[2]:t.length===2?t[1]:s:s};async function Cr(s,e,t,n){for(let r of s){if(e[r.field.name]===void 0)continue;let o=r.process,i=await o(e[r.field.name],{sessionId:n,values:e,done:!0});nl(r.field,t,i,n)}}async function Ti(s,e,t,n,r,o,i=!1){for(let a of s){if(t.currField?.name!==a.field.name)continue;let u=e.substring(t.s);t.currField?.type?.name==="code"&&(u=u.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/,""),u=u.replace(/\s*```\s*$/,""));let l=a.process,c=await l(u,{sessionId:o,values:r,done:i});nl(t.currField,n,c,o)}}var nl=(s,e,t,n)=>{if(t===void 0||typeof t=="string"&&(t===""||/^(null|undefined)\s*$/i.test(t)))return;let r=String(t),o=r;e.addRequest([{role:"user",content:[{type:"text",text:o}]}],n),e.addTag("processor",n)};function rl(s,e){if(!e)return s;let t=[];if(e.format==="email"&&t.push("Must be a valid email address format"),(e.format==="uri"||e.format==="url"||e.name==="url")&&t.push("Must be a valid URL format"),(e.name==="string"||e.name==="code"||e.name==="url"||e.name==="date"||e.name==="datetime")&&(e.minLength!==void 0&&e.maxLength!==void 0?t.push(`Minimum length: ${e.minLength} characters, maximum length: ${e.maxLength} characters`):e.minLength!==void 0?t.push(`Minimum length: ${e.minLength} characters`):e.maxLength!==void 0&&t.push(`Maximum length: ${e.maxLength} characters`)),e.name==="number"&&(e.minimum!==void 0&&e.maximum!==void 0?t.push(`Minimum value: ${e.minimum}, maximum value: ${e.maximum}`):e.minimum!==void 0?t.push(`Minimum value: ${e.minimum}`):e.maximum!==void 0&&t.push(`Maximum value: ${e.maximum}`)),e.pattern!==void 0){if(!e.patternDescription)throw new Error(`Field with pattern '${e.pattern}' must include a patternDescription to explain the pattern to the LLM`);t.push(e.patternDescription)}if(e.name==="date"&&t.push("Format: YYYY-MM-DD"),e.name==="datetime"&&t.push("Format: ISO 8601 date-time"),t.length===0)return s;let n=t.join(". ");return!s||s.trim().length===0?n:`${s.trim().endsWith(".")?s.trim():`${s.trim()}.`} ${n}`}function St(s,e="Schema"){if("name"in s&&"type"in s)return Lo(s);let t={},n=[];for(let r of s){if(r.isInternal)continue;let o=Lo(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function Lo(s,e=!1){let t=s.type,n=rl(s.description,t);if(e&&t?.name&&(t.name==="image"||t.name==="audio"||t.name==="file"))throw new Error(`Media type '${t.name}' is not allowed in nested object fields. Media types (image, audio, file) can only be used as top-level input fields. Field: ${s.name}`);let r={};if(n&&(r.description=n),t?.isArray)if(r.type="array",t.fields){r.items={type:"object",properties:{},required:[],additionalProperties:!1},t.description&&(r.items.description=t.description);for(let[o,i]of Object.entries(t.fields)){let a={name:o,description:i.description,type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0,fields:i.fields,minLength:i.minLength,maxLength:i.maxLength,minimum:i.minimum,maximum:i.maximum,pattern:i.pattern,patternDescription:i.patternDescription,format:i.format},isOptional:i.isOptional,isInternal:i.isInternal};r.items.properties[o]=Lo(a,!0),i.isOptional||r.items.required.push(o)}}else if(t.name==="class"&&t.options)r.items={type:"string",enum:t.options};else{let o=rl(t.description||s.description,t);r.items={type:ol(t.name)},o&&(r.items.description=o),t.name==="string"||t.name==="code"||t.name==="url"||t.name==="date"||t.name==="datetime"?(t.minLength!==void 0&&(r.items.minLength=t.minLength),t.maxLength!==void 0&&(r.items.maxLength=t.maxLength),t.pattern!==void 0&&(r.items.pattern=t.pattern),t.format!==void 0&&(r.items.format=t.format)):t.name==="number"&&(t.minimum!==void 0&&(r.items.minimum=t.minimum),t.maximum!==void 0&&(r.items.maximum=t.maximum))}else if(t?.name==="object"&&t.fields){r.type="object",r.properties={},r.required=[],r.additionalProperties=!1;for(let[o,i]of Object.entries(t.fields)){let a={name:o,description:i.description,type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0,fields:i.fields,minLength:i.minLength,maxLength:i.maxLength,minimum:i.minimum,maximum:i.maximum,pattern:i.pattern,patternDescription:i.patternDescription,format:i.format},isOptional:i.isOptional,isInternal:i.isInternal};r.properties[o]=Lo(a,!0),i.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=ol(t?.name??"string"),t?.name==="string"||t?.name==="code"||t?.name==="url"||t?.name==="date"||t?.name==="datetime"?(t.minLength!==void 0&&(r.minLength=t.minLength),t.maxLength!==void 0&&(r.maxLength=t.maxLength),t.pattern!==void 0&&(r.pattern=t.pattern),t.format!==void 0&&(r.format=t.format),t.name==="url"&&!t.format&&(r.format="uri"),t.name==="date"&&!t.format&&(r.format="date"),t.name==="datetime"&&!t.format&&(r.format="date-time")):t?.name==="number"&&(t.minimum!==void 0&&(r.minimum=t.minimum),t.maximum!==void 0&&(r.maximum=t.maximum)));return r}function ol(s){switch(s){case"string":case"code":case"url":case"date":case"datetime":case"image":case"audio":case"file":return"string";case"number":return"number";case"boolean":return"boolean";case"json":case"object":return["object","array","string","number","boolean","null"];default:return"string"}}function $o(s){if(!s||typeof s!="object")throw new Error("Schema must be an object");if(s.type==="array"){if(!s.items)throw new Error('Array schema is missing an "items" definition (required by JSON Schema and all LLM providers for function tools)');$o(s.items)}else if(s.type==="object"&&s.properties)for(let e of Object.values(s.properties))$o(e)}var _t=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},Tr=class extends Error{constructor(t){super();this.fields=t;this.name="AxFunctionError"}getFields=()=>this.fields;toString(){return[`${this.name}: Function validation error`,...this.fields.map(t=>` - ${t.field}: ${t.message}`)].join(`
209
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},wr=class extends Error{constructor(t,n,r){super();this.fields=t;this.func=n;this.funcId=r}getFunctionId=()=>this.funcId;getFieldDescription(t){if(!this.func.parameters?.properties?.[t])return"";let n=this.func.parameters.properties[t],r=n.description;return n.enum?.length&&(r+=` Allowed values are: ${n.enum.join(", ")}`),r}getFixingInstructions=()=>{let t=this.fields.map(n=>{let r=this.getFieldDescription(n.field)||"";return`- \`${n.field}\` - ${n.message} (${r}).`});return`Errors In Function Arguments: Fix the following invalid arguments to '${this.func.name}'
210
210
  ${t.join(`
211
211
  `)}`};toString(){return[`${this.name}: Function execution error in '${this.func.name}'`,...this.fields.map(t=>{let n=this.getFieldDescription(t.field);return` - ${t.field}: ${t.message}${n?` (${n})`:""}`}),this.funcId?` Function ID: ${this.funcId}`:""].join(`
212
- `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},vr=class{funcList=[];constructor(e){this.funcList=e}executeFunction=async(e,t,n)=>{let r;if(typeof t.args=="string"&&t.args.length>0)try{r=JSON.parse(t.args)}catch(u){throw new Error(`Invalid function arguments: ${t.args}`,{cause:u})}else r=t.args;let o=n?{sessionId:n.sessionId,traceId:n.traceId,ai:n.ai,step:n.step,abortSignal:n.abortSignal}:void 0,s;e.parameters?s=e.func.length===2?await e.func(r,o):await e.func(r):s=e.func.length===1?await e.func(o):await e.func();let l=(n?.functionResultFormatter??me.functionResultFormatter)(s);return{formatted:String(l),rawResult:s,parsedArgs:r}};executeWithDetails=async(e,t)=>{let n=s=>s.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),r=n(e.name),o=this.funcList.find(s=>s.name===e.name);if(o||(o=this.funcList.find(s=>n(s.name)===r)),!o)throw new Error(`Function not found: ${e.name}`);if(!o.func)throw new Error(`No handler for function: ${e.name}`);try{return await this.executeFunction(o,e,t)}catch(s){throw s instanceof Sr?new kr(s.getFields(),o,e.id):s}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Kt=(i,e)=>{if(i.length===0)return[...e??[]];let t=i.map(n=>"toFunction"in n?n.toFunction():n).flat();for(let n of t.filter(r=>r.parameters))if(n.parameters)try{Uo(n.parameters)}catch(r){throw r instanceof Error?new Error(`Function '${n.name}' parameters schema is invalid.
212
+ `)}[Symbol.for("nodejs.util.inspect.custom")](t,n){return this.toString()}},Rr=class{funcList=[];constructor(e){this.funcList=e}executeFunction=async(e,t,n)=>{let r;if(typeof t.args=="string"&&t.args.length>0)try{r=JSON.parse(t.args)}catch(l){throw new Error(`Invalid function arguments: ${t.args}`,{cause:l})}else r=t.args;let o=n?{sessionId:n.sessionId,traceId:n.traceId,ai:n.ai,step:n.step,abortSignal:n.abortSignal}:void 0,i;e.parameters?i=e.func.length===2?await e.func(r,o):await e.func(r):i=e.func.length===1?await e.func(o):await e.func();let u=(n?.functionResultFormatter??re.functionResultFormatter)(i);return{formatted:String(u),rawResult:i,parsedArgs:r}};executeWithDetails=async(e,t)=>{let n=i=>i.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),r=n(e.name),o=this.funcList.find(i=>i.name===e.name);if(o||(o=this.funcList.find(i=>n(i.name)===r)),!o)throw new Error(`Function not found: ${e.name}`);if(!o.func)throw new Error(`No handler for function: ${e.name}`);try{return await this.executeFunction(o,e,t)}catch(i){throw i instanceof Tr?new wr(i.getFields(),o,e.id):i}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Nt=(s,e)=>{if(s.length===0)return[...e??[]];let t=s.map(n=>"toFunction"in n?n.toFunction():n).flat();for(let n of t.filter(r=>r.parameters))if(n.parameters)try{$o(n.parameters)}catch(r){throw r instanceof Error?new Error(`Function '${n.name}' parameters schema is invalid.
213
213
  ${r.message}
214
- Tip: Arrays must include an "items" schema (e.g., { items: { type: "string" } } or items: { type: "object", properties: { ... } }).`,{cause:r}):r}return[...e??[],...t]},Bo=async({ai:i,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:s,excludeContentFromTrace:a,index:l,functionResultFormatter:u,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let h=new vr(e),f=new Set,C=[],x=T=>{let k=$=>$.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),E=k(T),M=e.find($=>$.name===T);return M||(M=e.find($=>k($.name)===E)),M},I=t.map(T=>{if(!T.id)throw new Error(`Function ${T.name} did not return an ID`);let k=i.getOptions().tracer??me.tracer;return k?k.startActiveSpan(`Tool: ${T.name}`,async E=>{try{E?.setAttributes?.({"tool.name":T.name,"tool.mode":"native","function.id":T.id,"session.id":r??""});let{formatted:M,rawResult:$,parsedArgs:B}=await h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:E?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,B,$),d?.includes(T.name.toLowerCase())){let V=x(T.name);V&&C.push({func:V,args:B,result:$})}if(a?E.addEvent("gen_ai.tool.message",{name:T.name}):E.addEvent("gen_ai.tool.message",{name:T.name,args:T.args,result:M??""}),s){let V={name:T.name};a||(V.args=T.args,V.result=M??""),s.addEvent("function.call",V)}return{result:M??"",role:"function",functionId:T.id,index:l}}catch(M){if(E?.recordException?.(M),M instanceof kr){let $=M.getFixingInstructions(),B={name:T.name,message:M.toString()};return a||(B.args=T.args,B.fixing_instructions=$),E?.addEvent?.("function.error",B),p&&Fs(M,l,$,c),{functionId:T.id,isError:!0,index:l,result:$,role:"function"}}throw M}finally{E?.end?.()}}):h.executeWithDetails(T,{sessionId:r,ai:i,functionResultFormatter:u,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:E,rawResult:M,parsedArgs:$})=>{if(f.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,$,M),d?.includes(T.name.toLowerCase())){let B=x(T.name);B&&C.push({func:B,args:$,result:M})}if(s){let B={name:T.name};a||(B.args=T.args,B.result=E??""),s.addEvent("function.call",B)}return{result:E??"",role:"function",functionId:T.id,index:l}}).catch(E=>{if(!(E instanceof kr))throw E;let M=E.getFixingInstructions();if(s){let $={name:T.name,message:E.toString()};a||($.args=T.args,$.fixing_instructions=M),s.addEvent("function.error",$)}return p&&Fs(E,l,M,c),{functionId:T.id,isError:!0,index:l,result:M,role:"function"}})}),A=(await Promise.all(I)).map(T=>T.result===void 0||T.result===""?{...T,result:"done"}:T);if(n.addFunctionResults(A,r),p){let T=A.filter(k=>!k.isError);T.length>0&&ha(T,c)}if(C.length>0)throw new Ht(C);return f};function wi(i,e,t,n){if(!e||e.length===0)return;if(!i.getFeatures(n).functions)throw new Error("Functions are not supported by the AI service");return e.map(o=>({id:o.id,name:o.function.name,args:o.function.params}))}function Vl(i,e,t,n){let r=e;return!t&&(r==="required"||typeof r=="function")?{functions:[],functionCall:void 0}:i?{functions:i.map(s=>"toFunction"in s?s.toFunction():s).flat(),functionCall:r}:{functions:[],functionCall:r}}function Qc(i){let e=0,t=!1,n=!1,r=!1,o=!1,s=[];for(let a=0;a<i.length;a++){let l=i[a];if(n){n=!1;continue}if(l==="\\"){n=!0;continue}if(l==='"'){t=!t;continue}t||(l==="{"?(s.push("{"),e++):l==="["?(s.push("["),e++):l==="}"?s.length>0&&s[s.length-1]==="{"&&(s.pop(),e--):l==="]"&&s.length>0&&s[s.length-1]==="["&&(s.pop(),e--))}if(s.length>0){let a=s[s.length-1];r=a==="[",o=a==="{"}return{nestingLevel:e,inString:t,inArray:r,inObject:o}}function Jl(i){if(!i.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(i),partialMarker:null}}catch{}let e=Qc(i),t=Yc(i);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function Yc(i){let e=i.trim();for(e.endsWith(",")&&(e=e.slice(0,-1)),e.match(/,\s*"[^"]*"\s*:\s*$/)?e=e.replace(/,\s*"[^"]*"\s*:\s*$/,""):e.match(/\{\s*"[^"]*"\s*:\s*$/)&&(e=e.replace(/"[^"]*"\s*:\s*$/,""));e.match(/[0-9][eE.+-]$/)||e.match(/[eE][+-]$/);)e=e.slice(0,-1);e=e.replace(/,(\s*[}\]])/g,"$1"),e.match(/t(r(u(e)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("true")&&e.match(/[:[,]\s*t(r(u(e)?)?)?$/)&&(e=e.replace(/t(r(u(e)?)?)?$/,"true")),e.match(/f(a(l(s(e)?)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("false")&&e.match(/[:[,]\s*f(a(l(s(e)?)?)?)?$/)&&(e=e.replace(/f(a(l(s(e)?)?)?)?$/,"false")),e.match(/n(u(l(l)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("null")&&e.match(/[:[,]\s*n(u(l(l)?)?)?$/)&&(e=e.replace(/n(u(l(l)?)?)?$/,"null"));let t=[],n=!1,r=!1;for(let o=0;o<e.length;o++){let s=e[o];if(r){r=!1;continue}if(s==="\\"){r=!0;continue}if(s==='"'){n=!n;continue}n||(s==="{"?t.push("}"):s==="["?t.push("]"):s==="}"?t.length>0&&t[t.length-1]==="}"&&t.pop():s==="]"&&t.length>0&&t[t.length-1]==="]"&&t.pop())}for(r&&(e=e.slice(0,-1)),n&&(e+='"'),t.length>0&&t[t.length-1]==="}"&&e.match(/,\s*"[^"]*"\s*$/)&&(e=e.replace(/,\s*"[^"]*"\s*$/,""));t.length>0;)e+=t.pop();return e}async function*Ql({res:i,usage:e,states:t,debug:n,stepContext:r,...o}){let s=(o.ai.getFeatures().functionCot??!1)&&o.functions!==void 0&&o.functions.length>0,a,l=[],u=i.getReader();try{for(;;){let{done:c,value:p}=await u.read();if(c)break;let d=p;d.modelUsage&&(a=d.modelUsage);for(let m of d.results){if(Array.isArray(m.citations))for(let h of m.citations)h?.url&&l.push({url:h.url,title:h.title,description:h.description,license:h.license,publicationDate:h.publicationDate,snippet:h.snippet});if((!m.content||m.content==="")&&(!m.thought||m.thought==="")&&(!m.thoughtBlocks||m.thoughtBlocks.length===0)&&(!m.functionCalls||m.functionCalls.length===0))continue;let g=t.find(h=>h.index===m.index);if(!g)throw new Error(`No state found for result (index: ${m.index})`);yield*Xc({...o,result:m,skipEarlyFail:s,state:g,debug:n})}}}finally{u.releaseLock()}for(let c of t)yield*Zc({...o,state:c,debug:n,stepContext:r});if(a){if(l.length){let c=Array.from(new Map(l.filter(p=>p.url).map(p=>[p.url,p])).values());a.citations=c}if(e.push(a),n&&o.logger){let c=structuredClone(a);delete c.citations,o.logger({name:"ChatResponseUsage",value:c}),a.citations&&a.citations.length>0&&o.logger({name:"ChatResponseCitations",value:a.citations})}}}async function*Xc({result:i,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:s,signature:a,streamingFieldProcessors:l,thoughtFieldName:u,streamingAsserts:c,asserts:p}){if(i.functionCalls&&i.functionCalls.length>0)Vr(s.functionCalls,i.functionCalls),e.updateResult({name:i.name,content:i.content,functionCalls:s.functionCalls,thoughtBlocks:i.thoughtBlocks,delta:i.functionCalls?.[0]?.function?.params,index:i.index},t);else if(i.content&&i.content.length>0){i.thought&&i.thought.length>0&&(yield{index:i.index,delta:{[u]:i.thought}}),s.content+=i.content,e.updateResult({name:i.name,content:s.content,thoughtBlocks:i.thoughtBlocks,delta:i.content,index:i.index},t);let d=a.getOutputFields();if(a.hasComplexFields()){let{parsed:h,partialMarker:f}=Jl(s.content);if(h&&typeof h=="object"){let C={},x={};for(let I of Object.keys(h))if(d.some(w=>w.name===I)){let w=h[I],A=s.values[I];if(Array.isArray(w)&&w.length>0&&ep(w,f)&&(w=w.slice(0,-1)),x[I]=w,typeof w=="string"&&typeof A=="string"&&w.startsWith(A)){let T=w.slice(A.length);T&&(C[I]=T)}else Array.isArray(w)&&Array.isArray(A)?w.length>A.length&&(C[I]=w.slice(A.length)):JSON.stringify(w)!==JSON.stringify(A)&&(Array.isArray(w)||(C[I]=w))}try{Pn(a,x,{allowMissingRequired:!0})}catch{}Object.assign(s.values,x),Object.keys(C).length>0&&(yield{index:i.index,delta:C});return}}if(bi(a,s.values,s.xstate,s.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await ui(c,s.xstate,s.content),l.length!==0&&await Ti(l,s.content,s.xstate,e,s.values,t),yield*Ci(a,s.content,s.values,s.xstate,i.index),await On(p,s.values)}else i.thought&&i.thought.length>0?(s.values[u]=(s.values[u]??"")+i.thought,yield{index:i.index,delta:{[u]:i.thought}},e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thought:i.thought,thoughtBlocks:i.thoughtBlocks},t)):i.thoughtBlocks&&i.thoughtBlocks.length>0&&e.updateResult({name:i.name,content:s.content,delta:"",index:i.index,thoughtBlocks:i.thoughtBlocks},t);if(i.finishReason==="length")throw new Error(`Max tokens reached before completion
215
- Content: ${s.content}`)}async function*Zc({state:i,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:s,traceId:a,span:l,strictMode:u,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:h,signatureToolCallingManager:f,logger:C,debug:x,stopFunctionNames:I,stepContext:w,abortSignal:A}){let T=f?void 0:wi(t,i.functionCalls,i.values,n);if(T){if(!r)throw new Error("Functions are not defined");let k=await Bo({ai:t,functionList:r,functionCalls:T,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:C,debug:x,stopFunctionNames:I,step:w,abortSignal:A});i.functionsExecuted=new Set([...i.functionsExecuted,...k]),i.functionCalls=[]}else{let k=e.getOutputFields(),E=e.hasComplexFields(),M=!1;if(E)try{let $=JSON.parse(i.content),B={};for(let V of Object.keys($))if(k.some(O=>O.name===V)){let O=$[V],j=i.values[V];if(typeof O=="string"&&typeof j=="string"&&O.startsWith(j)){let F=O.slice(j.length);F&&(B[V]=F)}else Array.isArray(O)&&Array.isArray(j)?O.length>j.length&&(B[V]=O.slice(j.length)):JSON.stringify(O)!==JSON.stringify(j)&&(Array.isArray(O)||(B[V]=O))}try{Pn(e,B,{allowMissingRequired:!0})}catch(V){let O=(V.message||"").toLowerCase();if(O.includes("at least")||O.includes("at most")||O.includes("must match pattern")||O.includes("invalid url")||O.includes("required")||O.includes("missing")||O.includes("valid email")||O.includes("number must be"))throw V}for(let V of Object.keys($))k.some(O=>O.name===V)&&(i.values[V]=$[V]);yield{index:i.index,delta:B},M=!0}catch($){let B=($.message||"").toLowerCase();if(B.includes("at least")||B.includes("at most")||B.includes("must match pattern")||B.includes("invalid url")||B.includes("required")||B.includes("missing")||B.includes("valid email")||B.includes("number must be"))throw $}if(!M){let $=f!==void 0;Ii(e,i.values,i.xstate,i.content,{strictMode:u,treatAllFieldsOptional:$,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(f){let $=await f.processResults(i.values);if($&&$.length>0){if(!r)throw new Error("Functions are not defined");let B=await Bo({ai:t,functionList:r,functionCalls:$,mem:o,sessionId:s,traceId:a,span:l,index:i.index,excludeContentFromTrace:c,functionResultFormatter:h,logger:C,debug:x,stopFunctionNames:I,step:w,abortSignal:A});i.functionsExecuted=new Set([...i.functionsExecuted,...B]),o.updateResult({name:void 0,content:i.content,functionCalls:$.map(V=>({id:V.id,type:"function",function:{name:V.name,params:V.args}})),index:i.index},s);return}}await ui(p,i.xstate,i.content,!0),await On(d,i.values),m.length&&await Rr(m,i.values,o,s),g.length!==0&&await Ti(g,i.content,i.xstate,o,i.values,s,!0),yield*Ci(e,i.content,i.values,i.xstate,i.index)}}async function*Yl({ai:i,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:s,strictMode:a,states:l,usage:u,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:h,logger:f,debug:C,signatureToolCallingManager:x,stopFunctionNames:I,disableMemoryCleanup:w,stepContext:A,abortSignal:T}){let k=e.results??[],E=x!==void 0;t.addResponse(k,n);let M=[];for(let O of k)if(Array.isArray(O?.citations))for(let j of O.citations)j?.url&&M.push({url:j.url,title:j.title,description:j.description,license:j.license,publicationDate:j.publicationDate,snippet:j.snippet});for(let O of k){let j=l[O.index];if(!j)throw new Error(`No state found for result (index: ${O.index})`);if(e.modelUsage){let F=Array.from(new Map(M.filter(P=>P.url).map(P=>[P.url,P])).values()),W={...e.modelUsage,...F.length?{citations:F}:{}};if(u.push(W),C&&f){let P=structuredClone(W);delete P.citations,f({name:"ChatResponseUsage",value:P}),W.citations&&W.citations.length>0&&f({name:"ChatResponseCitations",value:W.citations})}}if(x&&O.content){O.thought&&O.thought.length>0&&(j.values[m]=O.thought),Do(g,j.values,O.content,{strictMode:a,treatAllFieldsOptional:E});let W=(await x.processResults(j.values))?.map(P=>({id:P.id,type:"function",function:{name:P.name,params:P.args}}));W&&W.length>0&&t.updateResult({name:O.name,content:O.content,functionCalls:W,index:O.index},n)}if(O.functionCalls?.length){let F=wi(i,O.functionCalls,j.values);if(F&&F.length>0){if(!o)throw new Error("Functions are not defined");let W;try{W=await Bo({ai:i,functionList:o,functionCalls:F,mem:t,sessionId:n,traceId:r,span:s,excludeContentFromTrace:c,index:O.index,functionResultFormatter:h,logger:f,debug:C,stopFunctionNames:I,step:A,abortSignal:T})}catch(P){throw t.addRequest([{role:"user",content:"The previous tool call failed. Fix arguments and try again, ensuring required fields match schema."}],n),t.addTag("correction",n),P}j.functionsExecuted=new Set([...j.functionsExecuted,...W])}}else if(O.content){O.thought&&O.thought.length>0&&(j.values[m]=O.thought);let F=g.getOutputFields();if(g.hasComplexFields())try{let P=JSON.parse(O.content),D={};for(let K of Object.keys(P))F.some(N=>N.name===K)&&(D[K]=P[K]);Pn(g,D),Object.assign(j.values,D)}catch(P){let D=P instanceof SyntaxError;if((P.name?.includes("ValidationError")||P.name?.includes("Error"))&&!D){let N=(P.message||"").toLowerCase();if(N.includes("at least")||N.includes("at most")||N.includes("must match pattern")||N.includes("invalid url")||N.includes("required")||N.includes("missing")||N.includes("valid email")||N.includes("number must be"))throw P}if(D)Do(g,j.values,O.content,{strictMode:a,treatAllFieldsOptional:E});else throw P}else Do(g,j.values,O.content,{strictMode:a,treatAllFieldsOptional:E})}if(await On(p,j.values),w||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Rr(d,j.values,t,n),O.finishReason==="length")throw new Error(`Max tokens reached before completion
216
- Content: ${O.content}`)}let $=l.map(O=>O.values);for(let O of $)for(let j of g.getOutputFields())j.isInternal&&delete O[j.name];let B=g.getOutputFields(),V=$.map((O,j)=>{let F={};for(let W of B)W.isInternal||(F[W.name]=O[W.name]);return O[m]!==void 0&&(F[m]=O[m]),{index:j,delta:F}});for(let O of V)yield O}function ep(i,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function Xl(i,e,t,n){let r=i.getLast(n);if(!r)return!0;for(let[o,s]of t.entries()){let a=e?Array.from(e).some(p=>s.functionsExecuted.has(p)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let u=r.role==="function",c=r.tags?r.tags.some(p=>p==="processor"):!1;if(u&&e&&a||!(u||c))return!1}return!0}var tp=["none","minimal","low","medium","high","highest"];function Ri(i,e,t){let n={};if(e.model!==!1){let a=i.getModelList();if(a&&a.length>0){let l=a.filter(u=>"model"in u);if(l.length>0){let u=l.map(p=>p.key),c=l.map(p=>`${p.key} (${p.description})`);n.model={type:"string",enum:u,description:`${t&&u.includes(t)?`Currently using model: ${t}. `:""}Switch model for the next step. Prefer faster/cheaper models for simple tasks; use more capable models for complex reasoning, math, or multi-step analysis. Available: ${c.join(", ")}`}}}}e.thinkingBudget!==!1&&(n.thinkingBudget={type:"string",enum:[...tp],description:"Reasoning depth for the next step. none/minimal: simple lookups or reformatting. low/medium: moderate analysis, summarization. high/highest: math, logic, code analysis, or multi-step reasoning. Higher budgets use more tokens."}),e.temperature&&(n.temperature={type:"number",description:"Sampling temperature for the next step. Lower values (0\u20130.3) for deterministic tasks like math or code; higher values (0.7\u20131.0) for creative or exploratory tasks."});let r;if(e.functions&&e.functions.length>0){r=Kt(e.functions);let a=r.map(u=>u.name),l=r.map(u=>`${u.name} (${u.description})`);n.addFunctions={type:"array",items:{type:"string",enum:a},description:`Activate tools you need for the current sub-task. Only add what you will use immediately \u2014 fewer active tools means less noise. Available: ${l.join(", ")}`},n.removeFunctions={type:"array",items:{type:"string",enum:a},description:"Remove tools you are done with to reduce context size and maintain focus on remaining work."}}let o=r;return{name:"adjustGeneration",description:"Adjust model, reasoning depth, or active tools for the next step. Call when task complexity changes \u2014 upgrade for hard reasoning or analysis, downgrade for simple follow-ups. Only call when there is a clear reason to change.",parameters:Object.keys(n).length>0?{type:"object",properties:n}:void 0,func:(a,l)=>{let u=l?.step;if(!u)return"Generation parameters adjusted for next response.";if(a?.model&&u.setModel(a.model),a?.thinkingBudget&&u.setThinkingBudget(a.thinkingBudget),a?.temperature!==void 0&&u.setTemperature(a.temperature),a?.addFunctions?.length&&o){let c=o.filter(p=>a.addFunctions.includes(p.name));c.length>0&&u.addFunctions(c)}return a?.removeFunctions?.length&&u.removeFunctions(...a.removeFunctions),"Generation parameters adjusted for next response."}}}var _n=class{reg;constructor(){this.reg=new Set}register(e){this.reg.add(e)}*[Symbol.iterator](){let e=Array.from(this.reg);for(let t=0;t<e.length;t++)yield e[t]}};var ge=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},Si=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new ge("Empty signature provided",0,"",'A signature must contain at least input and output fields separated by "->". Example: "userQuery:string -> aiResponse:string"')}parse(){try{this.skipWhitespace();let e=this.parseParsedString();this.skipWhitespace(),this.currentSection="inputs";let t=this.parseFieldList(this.parseInputField.bind(this),"input");if(this.skipWhitespace(),this.position>=this.input.length)throw new ge("Incomplete signature: Missing output section",this.position,this.getErrorContext(),'Add "->" followed by output fields. Example: "-> responseText:string"');if(this.expectArrow(),this.skipWhitespace(),this.position>=this.input.length)throw new ge('Incomplete signature: No output fields specified after "->"',this.position,this.getErrorContext(),'Add at least one output field. Example: "-> responseText:string"');this.currentSection="outputs";let n=this.parseFieldList(this.parseOutputField.bind(this),"output");if(this.skipWhitespace(),this.position<this.input.length){let r=this.input.slice(this.position);throw new ge(`Unexpected content after signature: "${r}"`,this.position,this.getErrorContext(),"Remove any extra content after the output fields")}return this.validateParsedSignature({desc:e?.trim(),inputs:t,outputs:n}),{desc:e?.trim(),inputs:t,outputs:n}}catch(e){if(e instanceof ge)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new ge(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new ge(`Duplicate input field name: "${r.name}"`,0,"","Each field name must be unique within the signature");t.add(r.name)}let n=new Set;for(let r of e.outputs){if(n.has(r.name))throw new ge(`Duplicate output field name: "${r.name}"`,0,"","Each field name must be unique within the signature");n.add(r.name)}for(let r of e.outputs)if(t.has(r.name))throw new ge(`Field name "${r.name}" appears in both inputs and outputs`,0,"","Use different names for input and output fields to avoid confusion");if(e.inputs.length===0)throw new ge("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new ge("Signature must have at least one output field",0,"",'Add an output field after "->". Example: "... -> responseText:string"')}getErrorContext(){let e=Math.max(0,this.position-25),t=Math.min(this.input.length,this.position+25),n=this.input.slice(e,this.position),r=this.input.slice(this.position,t),o=`${" ".repeat(n.length)}^`;return[`Position ${this.position} in signature:`,`"${n}${r}"`,` ${o}`].join(`
217
- `)}parseFieldList(e,t){let n=[];if(this.skipWhitespace(),this.position>=this.input.length)throw new ge(`Empty ${t} section: Expected at least one field`,this.position,this.getErrorContext(),`Add a ${t} field. Example: ${t==="input"?"userInput:string":"responseText:string"}`);try{n.push(e())}catch(r){throw r instanceof ge?r:new ge(`Invalid first ${t} field: ${r instanceof Error?r.message:"Unknown error"}`,this.position,this.getErrorContext())}for(this.skipWhitespace();this.position<this.input.length&&!(this.input[this.position]==="-"&&this.position+1<this.input.length&&this.input[this.position+1]===">");)if(this.match(",")){if(this.skipWhitespace(),this.position>=this.input.length)throw new ge(`Unexpected end of input after comma in ${t} section`,this.position,this.getErrorContext(),`Add another ${t} field after the comma`);try{n.push(e())}catch(r){throw r instanceof ge?r:new ge(`Invalid ${t} field after comma: ${r instanceof Error?r.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace()}else break;return n}parseInputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"input");let t;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!"))throw new ge(`Input field "${e}" cannot use the internal marker "!"`,this.position-1,this.getErrorContext(),"Internal markers (!) are only allowed on output fields");break}let n;if(this.skipWhitespace(),this.match(":")){if(this.skipWhitespace(),/^class\b/.test(this.input.slice(this.position)))throw new ge(`Input field "${e}" cannot use the "class" type`,this.position,this.getErrorContext(),'Class types are only allowed on output fields. Use "string" type for input classifications');try{let o=this.parseTypeNotClass(),s=this.match("[]");n={name:o,isArray:s}}catch(o){throw o instanceof ge?o:new ge(`Input field "${e}": ${o instanceof Error?o.message:"Unknown error"}`,this.position,this.getErrorContext())}}this.skipWhitespace();let r=this.parseParsedString();return{name:e,desc:r?.trim(),type:n,isOptional:t}}parseOutputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"output");let t=!1,n=!1;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!")){n=!0;continue}break}let r;if(this.skipWhitespace(),this.match(":"))if(this.skipWhitespace(),this.match("class")){let s=this.match("[]");this.skipWhitespace();let a=this.parseParsedString();if(!a)throw new ge(`Output field "${e}": Missing class options after "class" type`,this.position,this.getErrorContext(),'Add class names in quotes. Example: class "positive, negative, neutral"');let l=a.split(/[,|]/).map(u=>u.trim()).filter(u=>u.length>0);if(l.length===0)throw new ge(`Output field "${e}": Empty class list provided`,this.position,this.getErrorContext(),'Provide at least one class option. Example: "positive, negative"');r={name:"class",isArray:s,options:l}}else try{let s=this.parseTypeNotClass(),a=this.match("[]");if(r={name:s,isArray:a},s==="image"&&a)throw new ge(`Output field "${e}": Arrays of images are not supported`,this.position,this.getErrorContext(),'Use a single image type instead: "image"');if(s==="audio"&&a)throw new ge(`Output field "${e}": Arrays of audio are not supported`,this.position,this.getErrorContext(),'Use a single audio type instead: "audio"');if(s==="image")throw new ge(`Output field "${e}": Image type is not supported in output fields`,this.position,this.getErrorContext(),"Image types can only be used in input fields");if(s==="audio")throw new ge(`Output field "${e}": Audio type is not supported in output fields`,this.position,this.getErrorContext(),"Audio types can only be used in input fields")}catch(s){throw s instanceof ge?s:new ge(`Output field "${e}": ${s instanceof Error?s.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace();let o=this.parseParsedString();return{name:e,desc:o?.trim(),type:r,isOptional:t,isInternal:n}}validateFieldName(e,t){if(me.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(e.toLowerCase())){let s=t==="input"?["userInput","questionText","documentContent","messageText"]:["responseText","analysisResult","categoryType","summaryText"];throw new ge(`Field name "${e}" is too generic`,this.position,this.getErrorContext(),`Use a more descriptive name. Examples: ${s.join(", ")}`)}let n=/^[a-z][a-zA-Z0-9]*$/,r=/^[a-z]+(_[a-z0-9]+)*$/;if(!n.test(e)&&!r.test(e))throw new ge(`Invalid field name "${e}"`,this.position,this.getErrorContext(),'Field names must be in camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(e.length<2)throw new ge(`Field name "${e}" is too short`,this.position,this.getErrorContext(),"Field names must be at least 2 characters long");if(e.length>50)throw new ge(`Field name "${e}" is too long (${e.length} characters)`,this.position,this.getErrorContext(),"Field names should be 50 characters or less")}parseTypeNotClass(){let e=["string","number","boolean","json","image","audio","file","url","datetime","date","code","object"],t=e.find(n=>this.match(n));if(!t){let n=this.input.slice(this.position).match(/^\w+/)?.[0]||"",r=this.suggestType(n),o=`Invalid type "${n||"empty"}"`,s=r?`. Did you mean "${r}"?`:"",a=`${o}${s}`;throw new ge(a,this.position,this.getErrorContext(),`Expected one of: ${e.join(", ")}`)}return t}suggestType(e){return{str:"string",text:"string",int:"number",integer:"number",float:"number",double:"number",bool:"boolean",object:"json",dict:"json",timestamp:"datetime",time:"datetime",img:"image",picture:"image",sound:"audio",voice:"audio",classification:"class",category:"class"}[e.toLowerCase()]||null}parseParsedIdentifier(){this.skipWhitespace();let e=/^[a-zA-Z_][a-zA-Z_0-9]*/.exec(this.input.slice(this.position));if(e)return this.position+=e[0].length,e[0];let t=/^\S+/.exec(this.input.slice(this.position)),n=t?t[0]:"";throw n===""?new ge("Expected field name but found end of input",this.position,this.getErrorContext(),"Add a field name. Field names must start with a letter or underscore"):/^\d/.test(n)?new ge(`Invalid field name "${n}" - cannot start with a number`,this.position,this.getErrorContext(),'Field names must start with a letter or underscore. Example: "userInput" or "_internal"'):new ge(`Invalid field name "${n}"`,this.position,this.getErrorContext(),"Field names must start with a letter or underscore and contain only letters, numbers, or underscores")}parseParsedString(){let e=["'",'"'];for(let t of e)if(this.match(t)){let n="",r=!1,o=this.position-1;for(;this.position<this.input.length;){let a=this.input[this.position];if(this.position++,r)n+=a,r=!1;else if(a==="\\")r=!0;else{if(a===t)return n;n+=a}}let s=this.input.slice(o,Math.min(this.position,o+20));throw new ge(`Unterminated string starting at position ${o}`,o,this.getErrorContext(),`Add closing ${t} to complete the string: ${s}${t}`)}}skipWhitespace(){let e=/^[\s\t\r\n]+/.exec(this.input.slice(this.position));e&&(this.position+=e[0].length)}match(e){let t;if(typeof e=="string"){if(this.input.startsWith(e,this.position))return this.position+=e.length,!0}else if(t=e.exec(this.input.slice(this.position)),t)return this.position+=t[0].length,!0;return!1}expectArrow(){if(!this.match("->")){let e=this.input.slice(this.position,this.position+10),t=e.includes(">")?'Use "->" (dash followed by greater-than)':e.includes("-")?'Add ">" after the dash':'Add "->" to separate input and output fields';throw new ge(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function Zl(i){return new Si(i).parse()}var Or=class{inputFields=[];outputFields=[];desc;input(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,ki(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0,isCached:t.isCached||void 0};return n?this.inputFields.unshift(r):this.inputFields.push(r),this}output(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,s])=>[o,ki(s)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}addInputFields(e){for(let t of e)this.inputFields.push(t);return this}addOutputFields(e){for(let t of e)this.outputFields.push(t);return this}description(e){return this.desc=e,this}useStructured(){return this._useStructuredOutputs=!0,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields},t=new Ge(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},qe=class i{type;isArray;options;description;isOptional;isInternal;isCached;fields;minLength;maxLength;minimum;maximum;pattern;patternDescription;format;itemDescription;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.itemDescription=e.itemDescription,this.isOptional=e.isOptional,this.isInternal=e.isInternal,this.isCached=e.isCached,this.fields=e.fields,this.minLength=e.minLength,this.maxLength=e.maxLength,this.minimum=e.minimum,this.maximum=e.maximum,this.pattern=e.pattern,this.patternDescription=e.patternDescription,this.format=e.format}optional(){return new i({...this,isOptional:!0})}array(e){return new i({...this,isArray:!0,description:e||this.description,itemDescription:e?this.description:void 0})}internal(){return new i({...this,isInternal:!0})}cache(){return new i({...this,isCached:!0})}min(e){return this.type==="string"?new i({...this,minLength:e}):this.type==="number"?new i({...this,minimum:e}):this}max(e){return this.type==="string"?new i({...this,maxLength:e}):this.type==="number"?new i({...this,maximum:e}):this}email(){return this.type==="string"?new i({...this,format:"email"}):this}url(){return this.type==="string"?new i({...this,format:"uri"}):this}regex(e,t){return this.type==="string"?new i({...this,pattern:e,patternDescription:t}):this}date(){return this.type==="string"?new i({...this,format:"date"}):this}datetime(){return this.type==="string"?new i({...this,format:"date-time"}):this}},xe=Object.assign(()=>new Or,{string:i=>new qe({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),number:i=>new qe({type:"number",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),boolean:i=>new qe({type:"boolean",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),json:i=>new qe({type:"json",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),datetime:i=>new qe({type:"datetime",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),date:i=>new qe({type:"date",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),class:(i,e)=>new qe({type:"class",isArray:!1,options:i,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:i=>new qe({type:"image",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),audio:i=>new qe({type:"audio",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),file:i=>new qe({type:"file",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),url:i=>new qe({type:"url",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1}),email:i=>new qe({type:"string",isArray:!1,description:i,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(i,e)=>new qe({type:"code",isArray:!1,description:e||i,isOptional:!1,isInternal:!1,isCached:!1}),object:(i,e)=>new qe({type:"object",isArray:!1,fields:i,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function ki(i){return{type:i.type,isArray:i.isArray,options:i.options,description:i.description,isOptional:i.isOptional,isInternal:i.isInternal,minLength:i.minLength,maxLength:i.maxLength,minimum:i.minimum,maximum:i.maximum,pattern:i.pattern,patternDescription:i.patternDescription,format:i.format,fields:i.fields?Object.fromEntries(Object.entries(i.fields).map(([e,t])=>[e,ki(t)])):void 0}}function jo(i){return{type:{name:i.type,isArray:i.isArray,options:i.options?[...i.options]:void 0,fields:i.fields},description:i.description,isOptional:i.isOptional,isInternal:i.isInternal}}var te=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},Ge=class i{description;inputFields;outputFields;sigHash;sigString;validatedAtHash;constructor(e){if(!e){this.inputFields=[],this.outputFields=[],this.sigHash="",this.sigString="";return}if(typeof e=="string"){let t;try{t=Zl(e)}catch(n){if(n instanceof Error){let r="suggestion"in n&&typeof n.suggestion=="string"?n.suggestion:'Please check the signature format. Example: "userInput:string -> responseText:string"';throw new te(`Invalid Signature: ${n.message}`,void 0,r)}throw new te(`Invalid Signature: ${e}`,void 0,'Please check the signature format. Example: "userInput:string -> responseText:string"')}this.description=t.desc,this.inputFields=t.inputs.map(n=>this.parseParsedField(n)),this.outputFields=t.outputs.map(n=>this.parseParsedField(n)),[this.sigHash,this.sigString]=this.updateHash()}else if(e instanceof i)this.description=e.getDescription(),this.inputFields=structuredClone(e.getInputFields()),this.outputFields=structuredClone(e.getOutputFields()),this.sigHash=e.hash(),this.sigString=e.toString(),e.validatedAtHash===this.sigHash&&(this.validatedAtHash=this.sigHash),this._forceComplexFields=e._forceComplexFields,this._hasComplexFields=e._hasComplexFields;else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new te("Invalid signature object: missing inputs or outputs",void 0,'Signature object must have "inputs" and "outputs" arrays. Example: { inputs: [...], outputs: [...] }');if(!Array.isArray(e.inputs)||!Array.isArray(e.outputs))throw new te("Invalid signature object: inputs and outputs must be arrays",void 0,'Both "inputs" and "outputs" must be arrays of AxField objects');try{this.description=e.description,this.inputFields=e.inputs.map(t=>this.parseField(t)),this.outputFields=e.outputs.map(t=>this.parseField(t)),[this.sigHash,this.sigString]=this.updateHash()}catch(t){throw t instanceof te?t:new te(`Failed to create signature from object: ${t instanceof Error?t.message:"Unknown error"}`,void 0,"Check that all fields in inputs and outputs arrays are valid AxField objects")}}else throw new te("Invalid signature argument type",void 0,"Signature must be a string, another AxSignature instance, or an object with inputs and outputs arrays")}static create(e){return new i(e)}parseParsedField=e=>{if(!e.name||e.name.length===0)throw new te("Field name is required",e.name,'Every field must have a descriptive name. Example: "userInput", "responseText"');let t=this.toTitle(e.name);return{name:e.name,title:t,description:"desc"in e?e.desc:void 0,type:e.type??{name:"string",isArray:!1},..."isInternal"in e?{isInternal:e.isInternal}:{},..."isOptional"in e?{isOptional:e.isOptional}:{}}};parseField=e=>{let t=!e.title||e.title.length===0?this.toTitle(e.name):e.title;if(e.type&&(!e.type.name||e.type.name.length===0))throw new te("Field type name is required",e.name,"Specify a valid type. Available types: string, number, boolean, json, image, audio, file, url, date, datetime, class, code");return{...e,title:t}};setDescription=e=>{if(typeof e!="string")throw new te("Description must be a string",void 0,"Provide a string description for the signature");this.description=e,this.invalidateValidationCache(),this.updateHashLight()};addInputField=e=>{try{let t=this.parseField(e);Rt(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Duplicate input field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.inputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);Rt(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new te(`Duplicate output field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.inputFields)if(n.name===t.name)throw new te(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.outputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new te("Input fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return Rt(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new te("Output fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return Rt(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof te?t:new te(`Failed to set output fields: ${t instanceof Error?t.message:"Unknown error"}`)}};getInputFields=()=>this.inputFields;getOutputFields=()=>this.outputFields;getDescription=()=>this.description;appendInputField=(e,t)=>{let n=new i(this);return n.addInputField({name:e,...jo(t)}),n};prependInputField=(e,t)=>{let n=new i(this),r={name:e,...jo(t)},o=n.parseField(r);Rt(o,"input");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Duplicate input field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.inputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};appendOutputField=(e,t)=>{let n=new i(this);return n.addOutputField({name:e,...jo(t)}),n};prependOutputField=(e,t)=>{let n=new i(this),r={name:e,...jo(t)},o=n.parseField(r);Rt(o,"output");for(let s of n.outputFields)if(s.name===o.name)throw new te(`Duplicate output field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let s of n.inputFields)if(s.name===o.name)throw new te(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.outputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};invalidateValidationCache=()=>{this.validatedAtHash=void 0,this._hasComplexFields=void 0};toTitle=e=>{let t=e.replace(/_/g," ");return t=t.replace(/([A-Z]|[0-9]+)/g," $1").trim(),t.charAt(0).toUpperCase()+t.slice(1)};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{Rt(e,"input")}),this.getOutputFields().forEach(e=>{Rt(e,"output")}),this.sigHash=ot("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=tu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{Rt(e,"input")}),this.getOutputFields().forEach(e=>{Rt(e,"output")}),this.validateSignatureConsistency(),this.sigHash=ot("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=tu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof te?e:new te(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};validateSignatureConsistency(){let e=new Set;for(let n of this.inputFields){if(e.has(n.name))throw new te(`Duplicate input field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");e.add(n.name)}let t=new Set;for(let n of this.outputFields){if(t.has(n.name))throw new te(`Duplicate output field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");t.add(n.name)}for(let n of this.outputFields)if(e.has(n.name))throw new te(`Field name "${n.name}" appears in both inputs and outputs`,n.name,"Use different names for input and output fields to avoid confusion");if(this.inputFields.length===0)throw new te("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new te("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}_forceComplexFields=!1;_hasComplexFields;hasComplexFields=()=>this._hasComplexFields!==void 0?this._hasComplexFields:(this._hasComplexFields=this.computeHasComplexFields(),this._hasComplexFields);computeHasComplexFields=()=>this._forceComplexFields?!0:this.outputFields.some(e=>e.type?.name==="object"||e.type?.isArray&&e.type.fields!==void 0);validate=()=>{if(this.validatedAtHash===this.sigHash)return!0;try{return this.updateHash(),this.validatedAtHash=this.sigHash,!0}catch(e){throw this.validatedAtHash=void 0,e}};hash=()=>this.sigHash;toString=()=>this.sigString;toJSON=()=>({id:this.hash(),description:this.description,inputFields:this.inputFields,outputFields:this.outputFields});toJSONSchema=()=>{let e=[...this.inputFields,...this.outputFields];return Fn(e,this.description??"Schema")};toInputJSONSchema=()=>Fn(this.inputFields,this.description??"Schema")};function eu(i){let e=i.name;return i.isOptional&&(e+="?"),i.isInternal&&(e+="!"),i.type&&(e+=`:${i.type.name}`,i.type.isArray&&(e+="[]"),i.type.name==="class"&&i.type.options&&(e+=` "${i.type.options.join(" | ")}"`)),i.description&&i.type?.name!=="class"&&(e+=` "${i.description}"`),e}function tu(i,e,t){let n=i?`"${i}" `:"",r=e.map(eu).join(", "),o=t.map(eu).join(", ");return`${n}${r} -> ${o}`}function np(i){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(i)||t.test(i)}function Rt(i,e){if(!i.name||i.name.length===0)throw new te("Field name cannot be blank",i.name,"Every field must have a descriptive name");if(!np(i.name))throw new te(`Invalid field name '${i.name}' - must be camelCase or snake_case`,i.name,'Use camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(me.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(i.name.toLowerCase())){let n=e==="input"?["userInput","questionText","documentContent","messageText","queryString"]:["responseText","analysisResult","categoryType","summaryText","outputData"];throw new te(`Field name '${i.name}' is too generic`,i.name,`Use a more descriptive name. Examples for ${e} fields: ${n.join(", ")}`)}if(i.name.length<2)throw new te(`Field name '${i.name}' is too short`,i.name,"Field names must be at least 2 characters long");if(i.name.length>50)throw new te(`Field name '${i.name}' is too long (${i.name.length} characters)`,i.name,"Field names should be 50 characters or less");i.type&&rp(i,e)}function rp(i,e){if(!i.type)return;let{type:t}=i;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new te(`${t.name} type is not supported in output fields`,i.name,`${t.name} types can only be used in input fields`);if(t.name==="class"){if(e==="input")throw new te("Class type is not supported in input fields",i.name,'Class types are only allowed on output fields. Use "string" type for input classifications');if(!t.options||t.options.length===0)throw new te("Class type requires options",i.name,'Provide class options. Example: class "positive, negative, neutral"');for(let r of t.options){if(!r||r.trim().length===0)throw new te("Empty class option found",i.name,"All class options must be non-empty strings");let o=r.trim();if(o.includes(",")||o.includes("|"))throw new te(`Invalid class option "${o}"`,i.name,"Class options cannot contain commas (,) or pipes (|) as they are used to separate options")}if(new Set(t.options.map(r=>r.trim().toLowerCase())).size!==t.options.length)throw new te("Duplicate class options found",i.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new te("Arrays of code are not commonly supported",i.name,"Consider using a single code field or an array of strings instead");if(i.isInternal&&e==="input")throw new te("Internal marker (!) is not allowed on input fields",i.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&vi(t.fields,i.name,e)}function vi(i,e,t,n=1){for(let[r,o]of Object.entries(i)){let s=`${e}.${r}`;if(o.type==="image"||o.type==="audio"||o.type==="file")throw new te(`${o.type} type is not allowed in nested object fields`,s,`Media types (image, audio, file) can only be used as top-level input fields, not within objects. Found at depth ${n}.`);o.type==="object"&&o.fields&&vi(o.fields,s,t,n+1),o.isArray&&o.fields&&vi(o.fields,`${s}[]`,t,n+1)}}var Dt=class i{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new Ge(e),t?.description&&this.signature.setDescription(t.description),t?.traceLabel&&(this.traceLabel=t.traceLabel),e&&this.signature.validate(),this.sigHash=this.signature?.hash(),this.children=new _n,this.key={id:"root"}}getSignature(){return new Ge(this.signature)}setSignature(e){this.signature=new Ge(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash()}getId(){return this.key.id}register(e,t){let n=t??`p${this.childCount}`;this.childCount++,e.setId([this.key.id,n].join(".")),this.childNames.set(e,n),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let[t,n]of this.childNames)t.setId([e,n].join("."))}setExamples(e,t){let n=[];if("programId"in e&&e.programId===this.key.id&&(n=e.traces),Array.isArray(e)&&(n=e),n){this.examplesOptions=t;let r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()];this.examples=n.map(s=>{let a={};for(let l of o){let u=s[l.name];u!==void 0&&(Tr(l,u),a[l.name]=u)}return a})}}getTraces(){let e=[];this.trace&&e.push({trace:this.trace,programId:this.key.id});for(let t of Array.from(this.children)){let n=t?.getTraces();e=[...e,...n??[]]}return e}getUsage(){let e=[...this.usage??[]];for(let t of Array.from(this.children)){let n=t?.getUsage();e=[...e,...n??[]]}return wr(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}static _propagating=!1;setDemos(e,t){if(!i._propagating&&e.length>0){let u=new Set(this.namedPrograms().map(p=>p.id)),c=[...new Set(e.map(p=>p.programId))].filter(p=>!u.has(p));if(c.length>0){let p=[...u].join(", ");throw new Error(`Unknown program ID(s) in demos: ${c.join(", ")}. Valid IDs: ${p}. Use namedPrograms() to discover available IDs.`)}}let n=e.filter(u=>u.programId===this.key.id).map(u=>u.traces).flat(),r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()],s=new Set(r.getInputFields().map(u=>u.name)),a=new Set(r.getOutputFields().map(u=>u.name));this.demos=n.map((u,c)=>{let p={};for(let g of o){let h=u[g.name];h!==void 0&&(Tr(g,h),p[g.name]=h)}let d=Object.keys(p).some(g=>s.has(g));if(!Object.keys(p).some(g=>a.has(g)))throw new Error(`Demo trace[${c}] for '${this.key.id}' has no output field values. Expected at least one of: ${[...a].join(", ")}`);if(!d)throw new Error(`Demo trace[${c}] for '${this.key.id}' has no input field values. Expected at least one of: ${[...s].join(", ")}. Provide input context so the demo renders as a complete few-shot example.`);return p}),t?.modelConfig&&(this._optimizedModelConfig=t.modelConfig);let l=i._propagating;i._propagating=!0;try{for(let u of Array.from(this.children))u?.setDemos(e,t)}finally{i._propagating=l}}namedPrograms(){let e=[];[...this.signature.getInputFields(),...this.signature.getOutputFields()].length>0&&e.push({id:this.key.id,signature:this.signature.toString()});for(let n of Array.from(this.children))n&&"namedPrograms"in n&&typeof n.namedPrograms=="function"?e.push(...n.namedPrograms()):n&&e.push({id:n.getId()});return e}applyOptimization(e){this.setDemos(e.demos??[],{modelConfig:e.modelConfig})}};var nu={"dsp/example-disclaimer.md":`## Example Demonstrations
214
+ Tip: Arrays must include an "items" schema (e.g., { items: { type: "string" } } or items: { type: "object", properties: { ... } }).`,{cause:r}):r}return[...e??[],...t]},Do=async({ai:s,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:i,excludeContentFromTrace:a,index:u,functionResultFormatter:l,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let f=new Rr(e),x=new Set,y=[],h=T=>{let R=O=>O.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),k=R(T),v=e.find(O=>O.name===T);return v||(v=e.find(O=>R(O.name)===k)),v},A=t.map(T=>{if(!T.id)throw new Error(`Function ${T.name} did not return an ID`);let R=s.getOptions().tracer??re.tracer;return R?R.startActiveSpan(`Tool: ${T.name}`,async k=>{try{k?.setAttributes?.({"tool.name":T.name,"tool.mode":"native","function.id":T.id,"session.id":r??""});let{formatted:v,rawResult:O,parsedArgs:P}=await f.executeWithDetails(T,{sessionId:r,ai:s,functionResultFormatter:l,traceId:k?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(x.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,P,O),d?.includes(T.name.toLowerCase())){let $=h(T.name);$&&y.push({func:$,args:P,result:O})}if(a?k.addEvent("gen_ai.tool.message",{name:T.name}):k.addEvent("gen_ai.tool.message",{name:T.name,args:T.args,result:v??""}),i){let $={name:T.name};a||($.args=T.args,$.result=v??""),i.addEvent("function.call",$)}return{result:v??"",role:"function",functionId:T.id,index:u}}catch(v){if(k?.recordException?.(v),v instanceof wr){let O=v.getFixingInstructions(),P={name:T.name,message:v.toString()};return a||(P.args=T.args,P.fixing_instructions=O),k?.addEvent?.("function.error",P),p&&Fs(v,u,O,c),{functionId:T.id,isError:!0,index:u,result:O,role:"function"}}throw v}finally{k?.end?.()}}):f.executeWithDetails(T,{sessionId:r,ai:s,functionResultFormatter:l,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:k,rawResult:v,parsedArgs:O})=>{if(x.add(T.name.toLowerCase()),m?._recordFunctionCall(T.name,O,v),d?.includes(T.name.toLowerCase())){let P=h(T.name);P&&y.push({func:P,args:O,result:v})}if(i){let P={name:T.name};a||(P.args=T.args,P.result=k??""),i.addEvent("function.call",P)}return{result:k??"",role:"function",functionId:T.id,index:u}}).catch(k=>{if(!(k instanceof wr))throw k;let v=k.getFixingInstructions();if(i){let O={name:T.name,message:k.toString()};a||(O.args=T.args,O.fixing_instructions=v),i.addEvent("function.error",O)}return p&&Fs(k,u,v,c),{functionId:T.id,isError:!0,index:u,result:v,role:"function"}})}),I=(await Promise.all(A)).map(T=>T.result===void 0||T.result===""?{...T,result:"done"}:T);if(n.addFunctionResults(I,r),p){let T=I.filter(R=>!R.isError);T.length>0&&ka(T,c)}if(y.length>0)throw new _t(y);return x};function wi(s,e,t,n){if(!e||e.length===0)return;if(!s.getFeatures(n).functions)throw new Error("Functions are not supported by the AI service");return e.map(o=>({id:o.id,name:o.function.name,args:o.function.params}))}function sl(s,e,t,n){let r=e;return!t&&(r==="required"||typeof r=="function")?{functions:[],functionCall:void 0}:s?{functions:s.map(i=>"toFunction"in i?i.toFunction():i).flat(),functionCall:r}:{functions:[],functionCall:r}}function Mp(s){let e=0,t=!1,n=!1,r=!1,o=!1,i=[];for(let a=0;a<s.length;a++){let u=s[a];if(n){n=!1;continue}if(u==="\\"){n=!0;continue}if(u==='"'){t=!t;continue}t||(u==="{"?(i.push("{"),e++):u==="["?(i.push("["),e++):u==="}"?i.length>0&&i[i.length-1]==="{"&&(i.pop(),e--):u==="]"&&i.length>0&&i[i.length-1]==="["&&(i.pop(),e--))}if(i.length>0){let a=i[i.length-1];r=a==="[",o=a==="{"}return{nestingLevel:e,inString:t,inArray:r,inObject:o}}function il(s){if(!s.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(s),partialMarker:null}}catch{}let e=Mp(s),t=Pp(s);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function Pp(s){let e=s.trim();for(e.endsWith(",")&&(e=e.slice(0,-1)),e.match(/,\s*"[^"]*"\s*:\s*$/)?e=e.replace(/,\s*"[^"]*"\s*:\s*$/,""):e.match(/\{\s*"[^"]*"\s*:\s*$/)&&(e=e.replace(/"[^"]*"\s*:\s*$/,""));e.match(/[0-9][eE.+-]$/)||e.match(/[eE][+-]$/);)e=e.slice(0,-1);e=e.replace(/,(\s*[}\]])/g,"$1"),e.match(/t(r(u(e)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("true")&&e.match(/[:[,]\s*t(r(u(e)?)?)?$/)&&(e=e.replace(/t(r(u(e)?)?)?$/,"true")),e.match(/f(a(l(s(e)?)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("false")&&e.match(/[:[,]\s*f(a(l(s(e)?)?)?)?$/)&&(e=e.replace(/f(a(l(s(e)?)?)?)?$/,"false")),e.match(/n(u(l(l)?)?)?$/)&&!e.endsWith('"')&&!e.endsWith("null")&&e.match(/[:[,]\s*n(u(l(l)?)?)?$/)&&(e=e.replace(/n(u(l(l)?)?)?$/,"null"));let t=[],n=!1,r=!1;for(let o=0;o<e.length;o++){let i=e[o];if(r){r=!1;continue}if(i==="\\"){r=!0;continue}if(i==='"'){n=!n;continue}n||(i==="{"?t.push("}"):i==="["?t.push("]"):i==="}"?t.length>0&&t[t.length-1]==="}"&&t.pop():i==="]"&&t.length>0&&t[t.length-1]==="]"&&t.pop())}for(r&&(e=e.slice(0,-1)),n&&(e+='"'),t.length>0&&t[t.length-1]==="}"&&e.match(/,\s*"[^"]*"\s*$/)&&(e=e.replace(/,\s*"[^"]*"\s*$/,""));t.length>0;)e+=t.pop();return e}async function*al({res:s,usage:e,states:t,debug:n,stepContext:r,...o}){let i=(o.ai.getFeatures().functionCot??!1)&&o.functions!==void 0&&o.functions.length>0,a,u=[],l=s.getReader();try{for(;;){let{done:c,value:p}=await l.read();if(c)break;let d=p;d.modelUsage&&(a=d.modelUsage);for(let m of d.results){if(Array.isArray(m.citations))for(let f of m.citations)f?.url&&u.push({url:f.url,title:f.title,description:f.description,license:f.license,publicationDate:f.publicationDate,snippet:f.snippet});if((!m.content||m.content==="")&&(!m.thought||m.thought==="")&&(!m.thoughtBlocks||m.thoughtBlocks.length===0)&&(!m.functionCalls||m.functionCalls.length===0))continue;let g=t.find(f=>f.index===m.index);if(!g)throw new Error(`No state found for result (index: ${m.index})`);yield*Fp({...o,result:m,skipEarlyFail:i,state:g,debug:n})}}}finally{l.releaseLock()}for(let c of t)yield*_p({...o,state:c,debug:n,stepContext:r});if(a){if(u.length){let c=Array.from(new Map(u.filter(p=>p.url).map(p=>[p.url,p])).values());a.citations=c}if(e.push(a),n&&o.logger){let c=structuredClone(a);delete c.citations,o.logger({name:"ChatResponseUsage",value:c}),a.citations&&a.citations.length>0&&o.logger({name:"ChatResponseCitations",value:a.citations})}}}async function*Fp({result:s,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:i,signature:a,streamingFieldProcessors:u,thoughtFieldName:l,streamingAsserts:c,asserts:p}){if(s.functionCalls&&s.functionCalls.length>0)Hr(i.functionCalls,s.functionCalls),e.updateResult({name:s.name,content:s.content,functionCalls:i.functionCalls,thoughtBlocks:s.thoughtBlocks,delta:s.functionCalls?.[0]?.function?.params,index:s.index},t);else if(s.content&&s.content.length>0){s.thought&&s.thought.length>0&&(yield{index:s.index,delta:{[l]:s.thought}}),i.content+=s.content,e.updateResult({name:s.name,content:i.content,thoughtBlocks:s.thoughtBlocks,delta:s.content,index:s.index},t);let d=a.getOutputFields();if(a.hasComplexFields()){let{parsed:f,partialMarker:x}=il(i.content);if(f&&typeof f=="object"){let y={},h={};for(let A of Object.keys(f))if(d.some(b=>b.name===A)){let b=f[A],I=i.values[A];if(Array.isArray(b)&&b.length>0&&Np(b,x)&&(b=b.slice(0,-1)),h[A]=b,typeof b=="string"&&typeof I=="string"&&b.startsWith(I)){let T=b.slice(I.length);T&&(y[A]=T)}else Array.isArray(b)&&Array.isArray(I)?b.length>I.length&&(y[A]=b.slice(I.length)):JSON.stringify(b)!==JSON.stringify(I)&&(Array.isArray(b)||(y[A]=b))}try{In(a,h,{allowMissingRequired:!0})}catch{}Object.assign(i.values,h),Object.keys(y).length>0&&(yield{index:s.index,delta:y});return}}if(bi(a,i.values,i.xstate,i.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await li(c,i.xstate,i.content),u.length!==0&&await Ti(u,i.content,i.xstate,e,i.values,t),yield*Ci(a,i.content,i.values,i.xstate,s.index),await An(p,i.values)}else s.thought&&s.thought.length>0?(i.values[l]=(i.values[l]??"")+s.thought,yield{index:s.index,delta:{[l]:s.thought}},e.updateResult({name:s.name,content:i.content,delta:"",index:s.index,thought:s.thought,thoughtBlocks:s.thoughtBlocks},t)):s.thoughtBlocks&&s.thoughtBlocks.length>0&&e.updateResult({name:s.name,content:i.content,delta:"",index:s.index,thoughtBlocks:s.thoughtBlocks},t);if(s.finishReason==="length")throw new Error(`Max tokens reached before completion
215
+ Content: ${i.content}`)}async function*_p({state:s,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:i,traceId:a,span:u,strictMode:l,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:f,signatureToolCallingManager:x,logger:y,debug:h,stopFunctionNames:A,stepContext:b,abortSignal:I}){let T=x?void 0:wi(t,s.functionCalls,s.values,n);if(T){if(!r)throw new Error("Functions are not defined");let R=await Do({ai:t,functionList:r,functionCalls:T,mem:o,sessionId:i,traceId:a,span:u,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:y,debug:h,stopFunctionNames:A,step:b,abortSignal:I});s.functionsExecuted=new Set([...s.functionsExecuted,...R]),s.functionCalls=[]}else{let R=e.getOutputFields(),k=e.hasComplexFields(),v=!1;if(k)try{let O=JSON.parse(s.content),P={};for(let $ of Object.keys(O))if(R.some(E=>E.name===$)){let E=O[$],F=s.values[$];if(typeof E=="string"&&typeof F=="string"&&E.startsWith(F)){let _=E.slice(F.length);_&&(P[$]=_)}else Array.isArray(E)&&Array.isArray(F)?E.length>F.length&&(P[$]=E.slice(F.length)):JSON.stringify(E)!==JSON.stringify(F)&&(Array.isArray(E)||(P[$]=E))}try{In(e,P,{allowMissingRequired:!0})}catch($){let E=($.message||"").toLowerCase();if(E.includes("at least")||E.includes("at most")||E.includes("must match pattern")||E.includes("invalid url")||E.includes("required")||E.includes("missing")||E.includes("valid email")||E.includes("number must be"))throw $}for(let $ of Object.keys(O))R.some(E=>E.name===$)&&(s.values[$]=O[$]);yield{index:s.index,delta:P},v=!0}catch(O){let P=(O.message||"").toLowerCase();if(P.includes("at least")||P.includes("at most")||P.includes("must match pattern")||P.includes("invalid url")||P.includes("required")||P.includes("missing")||P.includes("valid email")||P.includes("number must be"))throw O}if(!v){let O=x!==void 0;Ii(e,s.values,s.xstate,s.content,{strictMode:l,treatAllFieldsOptional:O,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(x){let O=await x.processResults(s.values);if(O&&O.length>0){if(!r)throw new Error("Functions are not defined");let P=await Do({ai:t,functionList:r,functionCalls:O,mem:o,sessionId:i,traceId:a,span:u,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:y,debug:h,stopFunctionNames:A,step:b,abortSignal:I});s.functionsExecuted=new Set([...s.functionsExecuted,...P]),o.updateResult({name:void 0,content:s.content,functionCalls:O.map($=>({id:$.id,type:"function",function:{name:$.name,params:$.args}})),index:s.index},i);return}}await li(p,s.xstate,s.content,!0),await An(d,s.values),m.length&&await Cr(m,s.values,o,i),g.length!==0&&await Ti(g,s.content,s.xstate,o,s.values,i,!0),yield*Ci(e,s.content,s.values,s.xstate,s.index)}}async function*ul({ai:s,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:i,strictMode:a,states:u,usage:l,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:f,logger:x,debug:y,signatureToolCallingManager:h,stopFunctionNames:A,disableMemoryCleanup:b,stepContext:I,abortSignal:T}){let R=e.results??[],k=h!==void 0;t.addResponse(R,n);let v=[];for(let E of R)if(Array.isArray(E?.citations))for(let F of E.citations)F?.url&&v.push({url:F.url,title:F.title,description:F.description,license:F.license,publicationDate:F.publicationDate,snippet:F.snippet});for(let E of R){let F=u[E.index];if(!F)throw new Error(`No state found for result (index: ${E.index})`);if(e.modelUsage){let _=Array.from(new Map(v.filter(H=>H.url).map(H=>[H.url,H])).values()),j={...e.modelUsage,..._.length?{citations:_}:{}};if(l.push(j),y&&x){let H=structuredClone(j);delete H.citations,x({name:"ChatResponseUsage",value:H}),j.citations&&j.citations.length>0&&x({name:"ChatResponseCitations",value:j.citations})}}if(h&&E.content){E.thought&&E.thought.length>0&&(F.values[m]=E.thought),No(g,F.values,E.content,{strictMode:a,treatAllFieldsOptional:k});let j=(await h.processResults(F.values))?.map(H=>({id:H.id,type:"function",function:{name:H.name,params:H.args}}));j&&j.length>0&&t.updateResult({name:E.name,content:E.content,functionCalls:j,index:E.index},n)}if(E.functionCalls?.length){let _=wi(s,E.functionCalls,F.values);if(_&&_.length>0){if(!o)throw new Error("Functions are not defined");let j;try{j=await Do({ai:s,functionList:o,functionCalls:_,mem:t,sessionId:n,traceId:r,span:i,excludeContentFromTrace:c,index:E.index,functionResultFormatter:f,logger:x,debug:y,stopFunctionNames:A,step:I,abortSignal:T})}catch(H){throw t.addRequest([{role:"user",content:"The previous tool call failed. Fix arguments and try again, ensuring required fields match schema."}],n),t.addTag("correction",n),H}F.functionsExecuted=new Set([...F.functionsExecuted,...j])}}else if(E.content){E.thought&&E.thought.length>0&&(F.values[m]=E.thought);let _=g.getOutputFields();if(g.hasComplexFields())try{let H=JSON.parse(E.content),L={};for(let X of Object.keys(H))_.some(U=>U.name===X)&&(L[X]=H[X]);In(g,L),Object.assign(F.values,L)}catch(H){let L=H instanceof SyntaxError;if((H.name?.includes("ValidationError")||H.name?.includes("Error"))&&!L){let U=(H.message||"").toLowerCase();if(U.includes("at least")||U.includes("at most")||U.includes("must match pattern")||U.includes("invalid url")||U.includes("required")||U.includes("missing")||U.includes("valid email")||U.includes("number must be"))throw H}if(L)No(g,F.values,E.content,{strictMode:a,treatAllFieldsOptional:k});else throw H}else No(g,F.values,E.content,{strictMode:a,treatAllFieldsOptional:k})}if(await An(p,F.values),b||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Cr(d,F.values,t,n),E.finishReason==="length")throw new Error(`Max tokens reached before completion
216
+ Content: ${E.content}`)}let O=u.map(E=>E.values);for(let E of O)for(let F of g.getOutputFields())F.isInternal&&delete E[F.name];let P=g.getOutputFields(),$=O.map((E,F)=>{let _={};for(let j of P)j.isInternal||(_[j.name]=E[j.name]);return E[m]!==void 0&&(_[m]=E[m]),{index:F,delta:_}});for(let E of $)yield E}function Np(s,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function ll(s,e,t,n){let r=s.getLast(n);if(!r)return!0;for(let[o,i]of t.entries()){let a=e?Array.from(e).some(p=>i.functionsExecuted.has(p)):!1;if(!r.chat[o])throw new Error(`No chat message found for result (index: ${o})`);let l=r.role==="function",c=r.tags?r.tags.some(p=>p==="processor"):!1;if(l&&e&&a||!(l||c))return!1}return!0}var Lp=["none","minimal","low","medium","high","highest"];function Ri(s,e,t){let n={};if(e.model!==!1){let a=s.getModelList();if(a&&a.length>0){let u=a.filter(l=>"model"in l);if(u.length>0){let l=u.map(p=>p.key),c=u.map(p=>`${p.key} (${p.description})`);n.model={type:"string",enum:l,description:`${t&&l.includes(t)?`Currently using model: ${t}. `:""}Switch model for the next step. Prefer faster/cheaper models for simple tasks; use more capable models for complex reasoning, math, or multi-step analysis. Available: ${c.join(", ")}`}}}}e.thinkingBudget!==!1&&(n.thinkingBudget={type:"string",enum:[...Lp],description:"Reasoning depth for the next step. none/minimal: simple lookups or reformatting. low/medium: moderate analysis, summarization. high/highest: math, logic, code analysis, or multi-step reasoning. Higher budgets use more tokens."}),e.temperature&&(n.temperature={type:"number",description:"Sampling temperature for the next step. Lower values (0\u20130.3) for deterministic tasks like math or code; higher values (0.7\u20131.0) for creative or exploratory tasks."});let r;if(e.functions&&e.functions.length>0){r=Nt(e.functions);let a=r.map(l=>l.name),u=r.map(l=>`${l.name} (${l.description})`);n.addFunctions={type:"array",items:{type:"string",enum:a},description:`Activate tools you need for the current sub-task. Only add what you will use immediately \u2014 fewer active tools means less noise. Available: ${u.join(", ")}`},n.removeFunctions={type:"array",items:{type:"string",enum:a},description:"Remove tools you are done with to reduce context size and maintain focus on remaining work."}}let o=r;return{name:"adjustGeneration",description:"Adjust model, reasoning depth, or active tools for the next step. Call when task complexity changes \u2014 upgrade for hard reasoning or analysis, downgrade for simple follow-ups. Only call when there is a clear reason to change.",parameters:Object.keys(n).length>0?{type:"object",properties:n}:void 0,func:(a,u)=>{let l=u?.step;if(!l)return"Generation parameters adjusted for next response.";if(a?.model&&l.setModel(a.model),a?.thinkingBudget&&l.setThinkingBudget(a.thinkingBudget),a?.temperature!==void 0&&l.setTemperature(a.temperature),a?.addFunctions?.length&&o){let c=o.filter(p=>a.addFunctions.includes(p.name));c.length>0&&l.addFunctions(c)}return a?.removeFunctions?.length&&l.removeFunctions(...a.removeFunctions),"Generation parameters adjusted for next response."}}}var Cn=class{reg;constructor(){this.reg=new Set}register(e){this.reg.add(e)}*[Symbol.iterator](){let e=Array.from(this.reg);for(let t=0;t<e.length;t++)yield e[t]}};var oe=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},ki=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new oe("Empty signature provided",0,"",'A signature must contain at least input and output fields separated by "->". Example: "userQuery:string -> aiResponse:string"')}parse(){try{this.skipWhitespace();let e=this.parseParsedString();this.skipWhitespace(),this.currentSection="inputs";let t=this.parseFieldList(this.parseInputField.bind(this),"input");if(this.skipWhitespace(),this.position>=this.input.length)throw new oe("Incomplete signature: Missing output section",this.position,this.getErrorContext(),'Add "->" followed by output fields. Example: "-> responseText:string"');if(this.expectArrow(),this.skipWhitespace(),this.position>=this.input.length)throw new oe('Incomplete signature: No output fields specified after "->"',this.position,this.getErrorContext(),'Add at least one output field. Example: "-> responseText:string"');this.currentSection="outputs";let n=this.parseFieldList(this.parseOutputField.bind(this),"output");if(this.skipWhitespace(),this.position<this.input.length){let r=this.input.slice(this.position);throw new oe(`Unexpected content after signature: "${r}"`,this.position,this.getErrorContext(),"Remove any extra content after the output fields")}return this.validateParsedSignature({desc:e?.trim(),inputs:t,outputs:n}),{desc:e?.trim(),inputs:t,outputs:n}}catch(e){if(e instanceof oe)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new oe(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new oe(`Duplicate input field name: "${r.name}"`,0,"","Each field name must be unique within the signature");t.add(r.name)}let n=new Set;for(let r of e.outputs){if(n.has(r.name))throw new oe(`Duplicate output field name: "${r.name}"`,0,"","Each field name must be unique within the signature");n.add(r.name)}for(let r of e.outputs)if(t.has(r.name))throw new oe(`Field name "${r.name}" appears in both inputs and outputs`,0,"","Use different names for input and output fields to avoid confusion");if(e.inputs.length===0)throw new oe("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new oe("Signature must have at least one output field",0,"",'Add an output field after "->". Example: "... -> responseText:string"')}getErrorContext(){let e=Math.max(0,this.position-25),t=Math.min(this.input.length,this.position+25),n=this.input.slice(e,this.position),r=this.input.slice(this.position,t),o=`${" ".repeat(n.length)}^`;return[`Position ${this.position} in signature:`,`"${n}${r}"`,` ${o}`].join(`
217
+ `)}parseFieldList(e,t){let n=[];if(this.skipWhitespace(),this.position>=this.input.length)throw new oe(`Empty ${t} section: Expected at least one field`,this.position,this.getErrorContext(),`Add a ${t} field. Example: ${t==="input"?"userInput:string":"responseText:string"}`);try{n.push(e())}catch(r){throw r instanceof oe?r:new oe(`Invalid first ${t} field: ${r instanceof Error?r.message:"Unknown error"}`,this.position,this.getErrorContext())}for(this.skipWhitespace();this.position<this.input.length&&!(this.input[this.position]==="-"&&this.position+1<this.input.length&&this.input[this.position+1]===">");)if(this.match(",")){if(this.skipWhitespace(),this.position>=this.input.length)throw new oe(`Unexpected end of input after comma in ${t} section`,this.position,this.getErrorContext(),`Add another ${t} field after the comma`);try{n.push(e())}catch(r){throw r instanceof oe?r:new oe(`Invalid ${t} field after comma: ${r instanceof Error?r.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace()}else break;return n}parseInputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"input");let t;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!"))throw new oe(`Input field "${e}" cannot use the internal marker "!"`,this.position-1,this.getErrorContext(),"Internal markers (!) are only allowed on output fields");break}let n;if(this.skipWhitespace(),this.match(":")){if(this.skipWhitespace(),/^class\b/.test(this.input.slice(this.position)))throw new oe(`Input field "${e}" cannot use the "class" type`,this.position,this.getErrorContext(),'Class types are only allowed on output fields. Use "string" type for input classifications');try{let o=this.parseTypeNotClass(),i=this.match("[]");n={name:o,isArray:i}}catch(o){throw o instanceof oe?o:new oe(`Input field "${e}": ${o instanceof Error?o.message:"Unknown error"}`,this.position,this.getErrorContext())}}this.skipWhitespace();let r=this.parseParsedString();return{name:e,desc:r?.trim(),type:n,isOptional:t}}parseOutputField(){this.skipWhitespace();let e=this.parseParsedIdentifier();this.currentFieldName=e,this.validateFieldName(e,"output");let t=!1,n=!1;for(;;){if(this.match("?")){t=!0;continue}if(this.match("!")){n=!0;continue}break}let r;if(this.skipWhitespace(),this.match(":"))if(this.skipWhitespace(),this.match("class")){let i=this.match("[]");this.skipWhitespace();let a=this.parseParsedString();if(!a)throw new oe(`Output field "${e}": Missing class options after "class" type`,this.position,this.getErrorContext(),'Add class names in quotes. Example: class "positive, negative, neutral"');let u=a.split(/[,|]/).map(l=>l.trim()).filter(l=>l.length>0);if(u.length===0)throw new oe(`Output field "${e}": Empty class list provided`,this.position,this.getErrorContext(),'Provide at least one class option. Example: "positive, negative"');r={name:"class",isArray:i,options:u}}else try{let i=this.parseTypeNotClass(),a=this.match("[]");if(r={name:i,isArray:a},i==="image"&&a)throw new oe(`Output field "${e}": Arrays of images are not supported`,this.position,this.getErrorContext(),'Use a single image type instead: "image"');if(i==="audio"&&a)throw new oe(`Output field "${e}": Arrays of audio are not supported`,this.position,this.getErrorContext(),'Use a single audio type instead: "audio"');if(i==="image")throw new oe(`Output field "${e}": Image type is not supported in output fields`,this.position,this.getErrorContext(),"Image types can only be used in input fields");if(i==="audio")throw new oe(`Output field "${e}": Audio type is not supported in output fields`,this.position,this.getErrorContext(),"Audio types can only be used in input fields")}catch(i){throw i instanceof oe?i:new oe(`Output field "${e}": ${i instanceof Error?i.message:"Unknown error"}`,this.position,this.getErrorContext())}this.skipWhitespace();let o=this.parseParsedString();return{name:e,desc:o?.trim(),type:r,isOptional:t,isInternal:n}}validateFieldName(e,t){if(re.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(e.toLowerCase())){let i=t==="input"?["userInput","questionText","documentContent","messageText"]:["responseText","analysisResult","categoryType","summaryText"];throw new oe(`Field name "${e}" is too generic`,this.position,this.getErrorContext(),`Use a more descriptive name. Examples: ${i.join(", ")}`)}let n=/^[a-z][a-zA-Z0-9]*$/,r=/^[a-z]+(_[a-z0-9]+)*$/;if(!n.test(e)&&!r.test(e))throw new oe(`Invalid field name "${e}"`,this.position,this.getErrorContext(),'Field names must be in camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(e.length<2)throw new oe(`Field name "${e}" is too short`,this.position,this.getErrorContext(),"Field names must be at least 2 characters long");if(e.length>50)throw new oe(`Field name "${e}" is too long (${e.length} characters)`,this.position,this.getErrorContext(),"Field names should be 50 characters or less")}parseTypeNotClass(){let e=["string","number","boolean","json","image","audio","file","url","datetime","date","code","object"],t=e.find(n=>this.match(n));if(!t){let n=this.input.slice(this.position).match(/^\w+/)?.[0]||"",r=this.suggestType(n),o=`Invalid type "${n||"empty"}"`,i=r?`. Did you mean "${r}"?`:"",a=`${o}${i}`;throw new oe(a,this.position,this.getErrorContext(),`Expected one of: ${e.join(", ")}`)}return t}suggestType(e){return{str:"string",text:"string",int:"number",integer:"number",float:"number",double:"number",bool:"boolean",object:"json",dict:"json",timestamp:"datetime",time:"datetime",img:"image",picture:"image",sound:"audio",voice:"audio",classification:"class",category:"class"}[e.toLowerCase()]||null}parseParsedIdentifier(){this.skipWhitespace();let e=/^[a-zA-Z_][a-zA-Z_0-9]*/.exec(this.input.slice(this.position));if(e)return this.position+=e[0].length,e[0];let t=/^\S+/.exec(this.input.slice(this.position)),n=t?t[0]:"";throw n===""?new oe("Expected field name but found end of input",this.position,this.getErrorContext(),"Add a field name. Field names must start with a letter or underscore"):/^\d/.test(n)?new oe(`Invalid field name "${n}" - cannot start with a number`,this.position,this.getErrorContext(),'Field names must start with a letter or underscore. Example: "userInput" or "_internal"'):new oe(`Invalid field name "${n}"`,this.position,this.getErrorContext(),"Field names must start with a letter or underscore and contain only letters, numbers, or underscores")}parseParsedString(){let e=["'",'"'];for(let t of e)if(this.match(t)){let n="",r=!1,o=this.position-1;for(;this.position<this.input.length;){let a=this.input[this.position];if(this.position++,r)n+=a,r=!1;else if(a==="\\")r=!0;else{if(a===t)return n;n+=a}}let i=this.input.slice(o,Math.min(this.position,o+20));throw new oe(`Unterminated string starting at position ${o}`,o,this.getErrorContext(),`Add closing ${t} to complete the string: ${i}${t}`)}}skipWhitespace(){let e=/^[\s\t\r\n]+/.exec(this.input.slice(this.position));e&&(this.position+=e[0].length)}match(e){let t;if(typeof e=="string"){if(this.input.startsWith(e,this.position))return this.position+=e.length,!0}else if(t=e.exec(this.input.slice(this.position)),t)return this.position+=t[0].length,!0;return!1}expectArrow(){if(!this.match("->")){let e=this.input.slice(this.position,this.position+10),t=e.includes(">")?'Use "->" (dash followed by greater-than)':e.includes("-")?'Add ">" after the dash':'Add "->" to separate input and output fields';throw new oe(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function cl(s){return new ki(s).parse()}var kr=class{inputFields=[];outputFields=[];desc;input(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,i])=>[o,Uo(i)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0,isCached:t.isCached||void 0};return n?this.inputFields.unshift(r):this.inputFields.push(r),this}output(e,t,n=!1){let r={name:e,type:{name:t.type,isArray:t.isArray||void 0,options:t.options?[...t.options]:void 0,minLength:t.minLength,maxLength:t.maxLength,minimum:t.minimum,maximum:t.maximum,pattern:t.pattern,patternDescription:t.patternDescription,format:t.format,description:t.itemDescription,fields:t.fields?Object.fromEntries(Object.entries(t.fields).map(([o,i])=>[o,Uo(i)])):void 0},description:t.description,isOptional:t.isOptional||void 0,isInternal:t.isInternal||void 0};return n?this.outputFields.unshift(r):this.outputFields.push(r),this}addInputFields(e){for(let t of e)this.inputFields.push(t);return this}addOutputFields(e){for(let t of e)this.outputFields.push(t);return this}description(e){return this.desc=e,this}useStructured(){return this._useStructuredOutputs=!0,this}build(){let e={description:this.desc,inputs:this.inputFields,outputs:this.outputFields},t=new Re(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},Ne=class s{type;isArray;options;description;isOptional;isInternal;isCached;fields;minLength;maxLength;minimum;maximum;pattern;patternDescription;format;itemDescription;constructor(e){this.type=e.type,this.isArray=e.isArray,this.options=e.options,this.description=e.description,this.itemDescription=e.itemDescription,this.isOptional=e.isOptional,this.isInternal=e.isInternal,this.isCached=e.isCached,this.fields=e.fields,this.minLength=e.minLength,this.maxLength=e.maxLength,this.minimum=e.minimum,this.maximum=e.maximum,this.pattern=e.pattern,this.patternDescription=e.patternDescription,this.format=e.format}optional(){return new s({...this,isOptional:!0})}array(e){return new s({...this,isArray:!0,description:e||this.description,itemDescription:e?this.description:void 0})}internal(){return new s({...this,isInternal:!0})}cache(){return new s({...this,isCached:!0})}min(e){return this.type==="string"?new s({...this,minLength:e}):this.type==="number"?new s({...this,minimum:e}):this}max(e){return this.type==="string"?new s({...this,maxLength:e}):this.type==="number"?new s({...this,maximum:e}):this}email(){return this.type==="string"?new s({...this,format:"email"}):this}url(){return this.type==="string"?new s({...this,format:"uri"}):this}regex(e,t){return this.type==="string"?new s({...this,pattern:e,patternDescription:t}):this}date(){return this.type==="string"?new s({...this,format:"date"}):this}datetime(){return this.type==="string"?new s({...this,format:"date-time"}):this}},ue=Object.assign(()=>new kr,{string:s=>new Ne({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),number:s=>new Ne({type:"number",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),boolean:s=>new Ne({type:"boolean",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),json:s=>new Ne({type:"json",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),datetime:s=>new Ne({type:"datetime",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),date:s=>new Ne({type:"date",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),class:(s,e)=>new Ne({type:"class",isArray:!1,options:s,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:s=>new Ne({type:"image",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),audio:s=>new Ne({type:"audio",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),file:s=>new Ne({type:"file",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),url:s=>new Ne({type:"url",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),email:s=>new Ne({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(s,e)=>new Ne({type:"code",isArray:!1,description:e||s,isOptional:!1,isInternal:!1,isCached:!1}),object:(s,e)=>new Ne({type:"object",isArray:!1,fields:s,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function Uo(s){return{type:s.type,isArray:s.isArray,options:s.options,description:s.description,isOptional:s.isOptional,isInternal:s.isInternal,minLength:s.minLength,maxLength:s.maxLength,minimum:s.minimum,maximum:s.maximum,pattern:s.pattern,patternDescription:s.patternDescription,format:s.format,fields:s.fields?Object.fromEntries(Object.entries(s.fields).map(([e,t])=>[e,Uo(t)])):void 0}}function Go(s){return{type:{name:s.type,isArray:s.isArray,options:s.options?[...s.options]:void 0,fields:s.fields},description:s.description,isOptional:s.isOptional,isInternal:s.isInternal}}function Si(s,e){return{name:s,type:{name:e.type,isArray:e.isArray||void 0,options:e.options?[...e.options]:void 0,minLength:e.minLength,maxLength:e.maxLength,minimum:e.minimum,maximum:e.maximum,pattern:e.pattern,patternDescription:e.patternDescription,format:e.format,description:e.itemDescription,fields:e.fields?Object.fromEntries(Object.entries(e.fields).map(([t,n])=>[t,Uo(n)])):void 0},description:e.description,isOptional:e.isOptional||void 0,isInternal:e.isInternal||void 0,isCached:e.isCached||void 0}}function pl(s){return s.length===0?{type:"object",properties:{}}:St(s,"Schema")}function $p(s){let e=Si("__value",s);return St([e],"Schema").properties?.__value??{type:"json"}}var vi=class{name;desc;ns;argFields=[];returnFields=[];returnFieldType;returnMode;fnHandler;fnExamples=[];constructor(e){this.name=e}description(e){return this.desc=e,this}namespace(e){return this.ns=e,this}arg(e,t){return this.argFields.push(Si(e,t)),this}args(e,t){return this.arg(e,t)}returns(e){if(this.returnMode==="fields")throw new Error("Cannot use fn().returns(...) after fn().returnsField(...); choose exactly one return schema style");return this.returnMode="single",this.returnFieldType=e,this}returnsField(e,t){if(this.returnMode==="single")throw new Error("Cannot use fn().returnsField(...) after fn().returns(...); choose exactly one return schema style");return this.returnMode="fields",this.returnFields.push(Si(e,t)),this}example(e){return this.fnExamples.push(e),this}examples(e){return this.fnExamples.push(...e),this}handler(e){return this.fnHandler=e,this}build(){let e=this.name.trim(),t=this.desc?.trim(),n=this.ns?.trim();if(!e)throw new Error("fn() requires a non-empty function name");if(!t)throw new Error(`Function "${e}" must define a non-empty description`);if(!this.fnHandler)throw new Error(`Function "${e}" must define a handler`);if(this.fnExamples.some(o=>!o.code.trim()))throw new Error(`Function "${e}" examples must define non-empty code`);return{name:e,description:t,...n?{namespace:n}:{},parameters:pl(this.argFields),...this.returnMode==="single"&&this.returnFieldType?{returns:$p(this.returnFieldType)}:this.returnMode==="fields"?{returns:pl(this.returnFields)}:{},...this.fnExamples.length>0?{examples:this.fnExamples.map(o=>({...o}))}:{},func:this.fnHandler}}},gl=s=>new vi(s),J=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},Re=class s{description;inputFields;outputFields;sigHash;sigString;validatedAtHash;constructor(e){if(!e){this.inputFields=[],this.outputFields=[],this.sigHash="",this.sigString="";return}if(typeof e=="string"){let t;try{t=cl(e)}catch(n){if(n instanceof Error){let r="suggestion"in n&&typeof n.suggestion=="string"?n.suggestion:'Please check the signature format. Example: "userInput:string -> responseText:string"';throw new J(`Invalid Signature: ${n.message}`,void 0,r)}throw new J(`Invalid Signature: ${e}`,void 0,'Please check the signature format. Example: "userInput:string -> responseText:string"')}this.description=t.desc,this.inputFields=t.inputs.map(n=>this.parseParsedField(n)),this.outputFields=t.outputs.map(n=>this.parseParsedField(n)),[this.sigHash,this.sigString]=this.updateHash()}else if(e instanceof s)this.description=e.getDescription(),this.inputFields=structuredClone(e.getInputFields()),this.outputFields=structuredClone(e.getOutputFields()),this.sigHash=e.hash(),this.sigString=e.toString(),e.validatedAtHash===this.sigHash&&(this.validatedAtHash=this.sigHash),this._forceComplexFields=e._forceComplexFields,this._hasComplexFields=e._hasComplexFields;else if(typeof e=="object"&&e!==null){if(!("inputs"in e)||!("outputs"in e))throw new J("Invalid signature object: missing inputs or outputs",void 0,'Signature object must have "inputs" and "outputs" arrays. Example: { inputs: [...], outputs: [...] }');if(!Array.isArray(e.inputs)||!Array.isArray(e.outputs))throw new J("Invalid signature object: inputs and outputs must be arrays",void 0,'Both "inputs" and "outputs" must be arrays of AxField objects');try{this.description=e.description,this.inputFields=e.inputs.map(t=>this.parseField(t)),this.outputFields=e.outputs.map(t=>this.parseField(t)),[this.sigHash,this.sigString]=this.updateHash()}catch(t){throw t instanceof J?t:new J(`Failed to create signature from object: ${t instanceof Error?t.message:"Unknown error"}`,void 0,"Check that all fields in inputs and outputs arrays are valid AxField objects")}}else throw new J("Invalid signature argument type",void 0,"Signature must be a string, another AxSignature instance, or an object with inputs and outputs arrays")}static create(e){return new s(e)}parseParsedField=e=>{if(!e.name||e.name.length===0)throw new J("Field name is required",e.name,'Every field must have a descriptive name. Example: "userInput", "responseText"');let t=this.toTitle(e.name);return{name:e.name,title:t,description:"desc"in e?e.desc:void 0,type:e.type??{name:"string",isArray:!1},..."isInternal"in e?{isInternal:e.isInternal}:{},..."isOptional"in e?{isOptional:e.isOptional}:{}}};parseField=e=>{let t=!e.title||e.title.length===0?this.toTitle(e.name):e.title;if(e.type&&(!e.type.name||e.type.name.length===0))throw new J("Field type name is required",e.name,"Specify a valid type. Available types: string, number, boolean, json, image, audio, file, url, date, datetime, class, code");return{...e,title:t}};setDescription=e=>{if(typeof e!="string")throw new J("Description must be a string",void 0,"Provide a string description for the signature");this.description=e,this.invalidateValidationCache(),this.updateHashLight()};addInputField=e=>{try{let t=this.parseField(e);pt(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new J(`Duplicate input field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.outputFields)if(n.name===t.name)throw new J(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.inputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof J?t:new J(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);pt(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new J(`Duplicate output field name: "${t.name}"`,t.name,"Each field name must be unique within the signature");for(let n of this.inputFields)if(n.name===t.name)throw new J(`Field name "${t.name}" appears in both inputs and outputs`,t.name,"Use different names for input and output fields to avoid confusion");this.outputFields.push(t),this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof J?t:new J(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new J("Input fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return pt(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof J?t:new J(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new J("Output fields must be an array",void 0,"Provide an array of field objects");try{let t=e.map(n=>{let r=this.parseField(n);return pt(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof J?t:new J(`Failed to set output fields: ${t instanceof Error?t.message:"Unknown error"}`)}};getInputFields=()=>this.inputFields;getOutputFields=()=>this.outputFields;getDescription=()=>this.description;appendInputField=(e,t)=>{let n=new s(this);return n.addInputField({name:e,...Go(t)}),n};prependInputField=(e,t)=>{let n=new s(this),r={name:e,...Go(t)},o=n.parseField(r);pt(o,"input");for(let i of n.inputFields)if(i.name===o.name)throw new J(`Duplicate input field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let i of n.outputFields)if(i.name===o.name)throw new J(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.inputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};appendOutputField=(e,t)=>{let n=new s(this);return n.addOutputField({name:e,...Go(t)}),n};prependOutputField=(e,t)=>{let n=new s(this),r={name:e,...Go(t)},o=n.parseField(r);pt(o,"output");for(let i of n.outputFields)if(i.name===o.name)throw new J(`Duplicate output field name: "${o.name}"`,o.name,"Each field name must be unique within the signature");for(let i of n.inputFields)if(i.name===o.name)throw new J(`Field name "${o.name}" appears in both inputs and outputs`,o.name,"Use different names for input and output fields to avoid confusion");return n.outputFields.unshift(o),n.invalidateValidationCache(),n.updateHashLight(),n};invalidateValidationCache=()=>{this.validatedAtHash=void 0,this._hasComplexFields=void 0};toTitle=e=>{let t=e.replace(/_/g," ");return t=t.replace(/([A-Z]|[0-9]+)/g," $1").trim(),t.charAt(0).toUpperCase()+t.slice(1)};updateHashLight=()=>{try{return this.getInputFields().forEach(e=>{pt(e,"input")}),this.getOutputFields().forEach(e=>{pt(e,"output")}),this.sigHash=He("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=ml(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof J?e:new J(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{pt(e,"input")}),this.getOutputFields().forEach(e=>{pt(e,"output")}),this.validateSignatureConsistency(),this.sigHash=He("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=ml(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof J?e:new J(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};validateSignatureConsistency(){let e=new Set;for(let n of this.inputFields){if(e.has(n.name))throw new J(`Duplicate input field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");e.add(n.name)}let t=new Set;for(let n of this.outputFields){if(t.has(n.name))throw new J(`Duplicate output field name: "${n.name}"`,n.name,"Each field name must be unique within the signature");t.add(n.name)}for(let n of this.outputFields)if(e.has(n.name))throw new J(`Field name "${n.name}" appears in both inputs and outputs`,n.name,"Use different names for input and output fields to avoid confusion");if(this.inputFields.length===0)throw new J("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new J("Signature must have at least one output field",void 0,'Add an output field. Example: "... -> responseText:string"')}_forceComplexFields=!1;_hasComplexFields;hasComplexFields=()=>this._hasComplexFields!==void 0?this._hasComplexFields:(this._hasComplexFields=this.computeHasComplexFields(),this._hasComplexFields);computeHasComplexFields=()=>this._forceComplexFields?!0:this.outputFields.some(e=>e.type?.name==="object"||e.type?.isArray&&e.type.fields!==void 0);validate=()=>{if(this.validatedAtHash===this.sigHash)return!0;try{return this.updateHash(),this.validatedAtHash=this.sigHash,!0}catch(e){throw this.validatedAtHash=void 0,e}};hash=()=>this.sigHash;toString=()=>this.sigString;toJSON=()=>({id:this.hash(),description:this.description,inputFields:this.inputFields,outputFields:this.outputFields});toJSONSchema=()=>{let e=[...this.inputFields,...this.outputFields];return St(e,this.description??"Schema")};toInputJSONSchema=()=>St(this.inputFields,this.description??"Schema")};function dl(s){let e=s.name;return s.isOptional&&(e+="?"),s.isInternal&&(e+="!"),s.type&&(e+=`:${s.type.name}`,s.type.isArray&&(e+="[]"),s.type.name==="class"&&s.type.options&&(e+=` "${s.type.options.join(" | ")}"`)),s.description&&s.type?.name!=="class"&&(e+=` "${s.description}"`),e}function ml(s,e,t){let n=s?`"${s}" `:"",r=e.map(dl).join(", "),o=t.map(dl).join(", ");return`${n}${r} -> ${o}`}function Dp(s){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(s)||t.test(s)}function pt(s,e){if(!s.name||s.name.length===0)throw new J("Field name cannot be blank",s.name,"Every field must have a descriptive name");if(!Dp(s.name))throw new J(`Invalid field name '${s.name}' - must be camelCase or snake_case`,s.name,'Use camelCase (e.g., "userInput") or snake_case (e.g., "user_input")');if(re.signatureStrict&&["text","object","image","string","number","boolean","json","array","datetime","date","time","type","class","input","output","data","value","result","response","request","item","element"].includes(s.name.toLowerCase())){let n=e==="input"?["userInput","questionText","documentContent","messageText","queryString"]:["responseText","analysisResult","categoryType","summaryText","outputData"];throw new J(`Field name '${s.name}' is too generic`,s.name,`Use a more descriptive name. Examples for ${e} fields: ${n.join(", ")}`)}if(s.name.length<2)throw new J(`Field name '${s.name}' is too short`,s.name,"Field names must be at least 2 characters long");if(s.name.length>50)throw new J(`Field name '${s.name}' is too long (${s.name.length} characters)`,s.name,"Field names should be 50 characters or less");s.type&&Gp(s,e)}function Gp(s,e){if(!s.type)return;let{type:t}=s;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new J(`${t.name} type is not supported in output fields`,s.name,`${t.name} types can only be used in input fields`);if(t.name==="class"){if(e==="input")throw new J("Class type is not supported in input fields",s.name,'Class types are only allowed on output fields. Use "string" type for input classifications');if(!t.options||t.options.length===0)throw new J("Class type requires options",s.name,'Provide class options. Example: class "positive, negative, neutral"');for(let r of t.options){if(!r||r.trim().length===0)throw new J("Empty class option found",s.name,"All class options must be non-empty strings");let o=r.trim();if(o.includes(",")||o.includes("|"))throw new J(`Invalid class option "${o}"`,s.name,"Class options cannot contain commas (,) or pipes (|) as they are used to separate options")}if(new Set(t.options.map(r=>r.trim().toLowerCase())).size!==t.options.length)throw new J("Duplicate class options found",s.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new J("Arrays of code are not commonly supported",s.name,"Consider using a single code field or an array of strings instead");if(s.isInternal&&e==="input")throw new J("Internal marker (!) is not allowed on input fields",s.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&Oi(t.fields,s.name,e)}function Oi(s,e,t,n=1){for(let[r,o]of Object.entries(s)){let i=`${e}.${r}`;if(o.type==="image"||o.type==="audio"||o.type==="file")throw new J(`${o.type} type is not allowed in nested object fields`,i,`Media types (image, audio, file) can only be used as top-level input fields, not within objects. Found at depth ${n}.`);o.type==="object"&&o.fields&&Oi(o.fields,i,t,n+1),o.isArray&&o.fields&&Oi(o.fields,`${i}[]`,t,n+1)}}var vt=class s{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new Re(e),t?.description&&this.signature.setDescription(t.description),t?.traceLabel&&(this.traceLabel=t.traceLabel),e&&this.signature.validate(),this.sigHash=this.signature?.hash(),this.children=new Cn,this.key={id:"root"}}getSignature(){return new Re(this.signature)}setSignature(e){this.signature=new Re(e),e&&this.signature.validate(),this.updateSignatureHash()}setDescription(e){this.signature.setDescription(e),this.updateSignatureHash()}updateSignatureHash(){this.sigHash=this.signature.hash()}getId(){return this.key.id}register(e,t){let n=t??`p${this.childCount}`;this.childCount++,e.setId([this.key.id,n].join(".")),this.childNames.set(e,n),this.children.register(e)}setId(e){this.key={id:e,custom:!0};for(let[t,n]of this.childNames)t.setId([e,n].join("."))}setExamples(e,t){let n=[];if("programId"in e&&e.programId===this.key.id&&(n=e.traces),Array.isArray(e)&&(n=e),n){this.examplesOptions=t;let r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()];this.examples=n.map(i=>{let a={};for(let u of o){let l=i[u.name];l!==void 0&&(br(u,l),a[u.name]=l)}return a})}}getTraces(){let e=[];this.trace&&e.push({trace:this.trace,programId:this.key.id});for(let t of Array.from(this.children)){let n=t?.getTraces();e=[...e,...n??[]]}return e}getUsage(){let e=[...this.usage??[]];for(let t of Array.from(this.children)){let n=t?.getUsage();e=[...e,...n??[]]}return Ir(e)}resetUsage(){this.usage=[];for(let e of Array.from(this.children))e?.resetUsage()}static _propagating=!1;setDemos(e,t){if(!s._propagating&&e.length>0){let l=new Set(this.namedPrograms().map(p=>p.id)),c=[...new Set(e.map(p=>p.programId))].filter(p=>!l.has(p));if(c.length>0){let p=[...l].join(", ");throw new Error(`Unknown program ID(s) in demos: ${c.join(", ")}. Valid IDs: ${p}. Use namedPrograms() to discover available IDs.`)}}let n=e.filter(l=>l.programId===this.key.id).map(l=>l.traces).flat(),r=this.signature,o=[...r.getInputFields(),...r.getOutputFields()],i=new Set(r.getInputFields().map(l=>l.name)),a=new Set(r.getOutputFields().map(l=>l.name));this.demos=n.map((l,c)=>{let p={};for(let g of o){let f=l[g.name];f!==void 0&&(br(g,f),p[g.name]=f)}let d=Object.keys(p).some(g=>i.has(g));if(!Object.keys(p).some(g=>a.has(g)))throw new Error(`Demo trace[${c}] for '${this.key.id}' has no output field values. Expected at least one of: ${[...a].join(", ")}`);if(!d)throw new Error(`Demo trace[${c}] for '${this.key.id}' has no input field values. Expected at least one of: ${[...i].join(", ")}. Provide input context so the demo renders as a complete few-shot example.`);return p}),t?.modelConfig&&(this._optimizedModelConfig=t.modelConfig);let u=s._propagating;s._propagating=!0;try{for(let l of Array.from(this.children))l?.setDemos(e,t)}finally{s._propagating=u}}namedPrograms(){let e=[];[...this.signature.getInputFields(),...this.signature.getOutputFields()].length>0&&e.push({id:this.key.id,signature:this.signature.toString()});for(let n of Array.from(this.children))n&&"namedPrograms"in n&&typeof n.namedPrograms=="function"?e.push(...n.namedPrograms()):n&&e.push({id:n.getId()});return e}namedProgramInstances(){let e=[];[...this.signature.getInputFields(),...this.signature.getOutputFields()].length>0&&e.push({id:this.key.id,program:this,signature:this.signature.toString()});for(let n of Array.from(this.children))n&&"namedProgramInstances"in n&&typeof n.namedProgramInstances=="function"?e.push(...n.namedProgramInstances()):n&&e.push({id:n.getId(),program:n});return e}applyOptimization(e){let t=e.demos!==void 0,n=e.modelConfig!==void 0;(t||n)&&this.setDemos(e.demos??[],{modelConfig:e.modelConfig});let r=this;if(typeof r.setInstruction=="function"){let i=e.instructionMap?.[this.key.id],a=typeof i=="string"?i:e.instruction;typeof a=="string"&&a.length>0&&r.setInstruction(a)}let o={...e,...t?{demos:void 0}:{},...n?{modelConfig:void 0}:{}};for(let i of Array.from(this.children))i?.applyOptimization(o)}};var fl={"dsp/example-disclaimer.md":`## Example Demonstrations
218
218
  The conversation history preceding the final user query consists of **few-shot examples** (demonstrations).
219
219
  - These alternating User/Assistant messages are provided **solely** to illustrate the correct reasoning steps, function usage, and output format.
220
220
  - **Do not** treat the specific data, entities, or facts in these examples as valid context for the current task.
@@ -261,7 +261,7 @@ REAL USER QUERY:
261
261
  - Do not use code blocks.
262
262
  `,"rlm/actor.md":`## Code Generation Agent
263
263
 
264
- You are a code generation agent called the \`actor\`. Your ONLY job is to write simple JavaScript code to solve problems, complete tasks and gather information. Use \`console.log\` to inspect variables and return values before writing more code that depends on those values. There is another agent called the \`responder\` that will synthesize final answers from the information you gather. You NEVER generate final answers directly \u2014 you can only write code to explore and analyze the context, call tools, and ask for clarification.
264
+ You are a code generation agent called the \`actor\`. Your ONLY job is to write simple JavaScript code to solve problems, complete tasks and gather information. Treat the JavaScript runtime as a long-running REPL session: variables, functions, imports, and computed values from earlier successful turns remain available unless you are explicitly told the runtime restarted. Use \`console.log\` to inspect variables and return values before writing more code that depends on those values. There is another agent called the \`responder\` that will synthesize final answers from the information you gather. You NEVER generate final answers directly \u2014 you can only write code to explore and analyze the context, call tools, and ask for clarification.
265
265
 
266
266
  ### Runtime Field Access
267
267
  In JavaScript code, context fields map to \`inputs.<fieldName>\` as follows:
@@ -281,6 +281,7 @@ The responder is looking to produce the following output fields: {{ responderOut
281
281
  {{ if discoveryMode }}
282
282
  - \`await listModuleFunctions(modules:string | string[]) : string\` \u2014 Returns markdown listing available callables for one or more modules.
283
283
  - \`await getFunctionDefinitions(functions:string | string[]) : string\` \u2014 Returns markdown with API description and call signature for one or more callables.
284
+ - In \`### Available Modules\`, grouped modules are shown as \`<namespace> - <selection criteria>\` when selection criteria is defined.
284
285
  - When you need multiple modules, prefer one batched call such as \`await listModuleFunctions(['timeRange', 'schedulingOrganizer'])\`.
285
286
  - When you need multiple callable definitions, prefer one batched call to \`getFunctionDefinitions([...])\`.
286
287
  - Treat discovery results as markdown meant for direct \`console.log(...)\` inspection.
@@ -311,8 +312,14 @@ The responder is looking to produce the following output fields: {{ responderOut
311
312
  - Only \`final(...args)\` and \`ask_clarification(...args)\` transmit payload to the responder.
312
313
  - Runtime output may be truncated. If output is incomplete, rerun with narrower scope.
313
314
 
315
+ - Reuse the existing runtime state instead of recreating it. Do not re-declare or recompute values that are already available unless you need to intentionally overwrite them or the runtime was reset.
316
+ - Think in continuation steps: inspect what already exists, extend it with the next small piece of code, and keep building on prior executed work.
317
+ {{ if hasLiveRuntimeState }}
314
318
  - The runtime session is the source of truth for current state. If a \`Live Runtime State\` block is present, trust it over older action log details.
319
+ {{ /if }}
320
+ {{ if hasCompressedActionReplay }}
315
321
  - Prior actions may be summarized or omitted. Only depend on old code when it is still shown in full; otherwise use the summary plus current runtime state.
322
+ {{ /if }}
316
323
  {{ if enforceIncrementalConsoleTurns }}
317
324
  - Treat each turn as one observable step.
318
325
  - If you are not calling \`final(...)\` or \`ask_clarification(...)\`, your code must include exactly one \`console.log(...)\` and stop immediately after it.
@@ -332,12 +339,12 @@ You synthesize a final answer from the provided actorResult payload. The payload
332
339
  1. Base your answer ONLY on evidence from actorResult payload arguments.
333
340
  2. If actorResult lacks sufficient information, provide the best possible answer from available evidence.
334
341
  3. If actorResult.type is \`ask_clarification\`, ask for the missing information clearly in your output fields.
335
- `};var Oi=/{{\s*([^}]+?)\s*}}/g,ru=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,ou=new Map;function St(i,e,t,n){let o=e.slice(0,t).split(`
336
- `),s=o.length,a=(o.at(-1)?.length??0)+1;return`${i}:${s}:${a} ${n}`}function op(i){let e=[],t=0;Oi.lastIndex=0;let n=Oi.exec(i);for(;n;){let[r,o]=n,s=n.index;s>t&&e.push({type:"text",value:i.slice(t,s)}),e.push({type:"tag",value:o.trim(),index:s}),t=s+r.length,n=Oi.exec(i)}return t<i.length&&e.push({type:"text",value:i.slice(t)}),e}function Mi(i,e,t,n=0,r=new Set){let o=[],s=n;for(;s<i.length;){let a=i[s];if(a.type==="text"){o.push({type:"text",value:a.value}),s++;continue}let l=a.value;if(r.has(l))return{nodes:o,nextIndex:s,terminator:l};if(l.startsWith("if ")){let u=l.slice(3).trim();if(!ru.test(u))throw new Error(St(t,e,a.index,`Invalid if condition '${u}'`));let c=Mi(i,e,t,s+1,new Set(["else","/if"]));if(!c.terminator)throw new Error(St(t,e,a.index,"Unclosed 'if' block"));let p=[],d=c.nextIndex+1;if(c.terminator==="else"){let m=Mi(i,e,t,c.nextIndex+1,new Set(["/if"]));if(m.terminator!=="/if")throw new Error(St(t,e,a.index,"Unclosed 'if' block"));p=m.nodes,d=m.nextIndex+1}o.push({type:"if",condition:u,thenNodes:c.nodes,elseNodes:p,index:a.index}),s=d;continue}if(l==="else")throw new Error(St(t,e,a.index,"Unexpected 'else'"));if(l==="/if")throw new Error(St(t,e,a.index,"Unexpected '/if'"));if(l.startsWith("include "))throw new Error(St(t,e,a.index,"Unexpected 'include' directive at runtime (includes must be compiled)"));if(!ru.test(l))throw new Error(St(t,e,a.index,`Invalid tag '${l}'`));o.push({type:"var",name:l,index:a.index}),s++}return{nodes:o,nextIndex:s}}function su(i,e,t,n,r){let o=e.split("."),s=i;for(let a of o){if(s===null||typeof s!="object"||!(a in s))throw new Error(St(n,t,r,`Missing template variable '${e}'`));s=s[a]}return s}function Ei(i,e,t,n){let r="";for(let o of i){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let a=su(e,o.name,t,n,o.index);if(typeof a!="string"&&typeof a!="number"&&typeof a!="boolean")throw new Error(St(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(a);continue}let s=su(e,o.condition,t,n,o.index);if(typeof s!="boolean")throw new Error(St(n,t,o.index,`Condition '${o.condition}' must be boolean`));s?r+=Ei(o.thenNodes,e,t,n):r+=Ei(o.elseNodes,e,t,n)}return r}function sp(i,e){let t=op(i),n=Mi(t,i,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function mt(i,e={}){let t=nu[i],n=`template:${i}`;if(!t)throw new Error(`Unknown template id: ${String(i)}`);let r=ou.get(i);return r||(r=sp(t,n),ou.set(i,r)),Ei(r,e,t,n)}var iu=mt("dsp/function-call-instructions.md"),ip=mt("dsp/strict-output-formatting-rules.md"),ap=mt("dsp/example-disclaimer.md"),Nn=mt("dsp/example-separator.md"),Vt=class{sig;fieldTemplates;task;customInstruction;setInstruction(e){this.customInstruction=e,this.task={type:"text",text:e}}getInstruction(){return this.customInstruction}thoughtFieldName;functions;contextCache;examplesInSystem;ignoreBreakpoints;structuredOutputFunctionName;constructor(e,t,n){this.sig=e,this.fieldTemplates=n,this.thoughtFieldName=t?.thoughtFieldName??"thought",this.functions=t?.functions,this.contextCache=t?.contextCache,this.examplesInSystem=t?.examplesInSystem??!1,this.ignoreBreakpoints=t?.ignoreBreakpoints??!1,this.structuredOutputFunctionName=t?.structuredOutputFunctionName,me.useStructuredPrompt?this.task=this.buildStructuredPrompt():this.task=this.buildLegacyPrompt()}getFieldNameToTitleMap=()=>{let e=new Map;for(let t of this.sig.getInputFields())e.set(t.name,t.title);for(let t of this.sig.getOutputFields())e.set(t.name,t.title);return e};sortFieldsCachedFirst=e=>[...e].sort((t,n)=>t.isCached&&!n.isCached?-1:!t.isCached&&n.isCached?1:0);buildLegacyPrompt(){let e=[],t=this.sig.hasComplexFields(),n=qo(this.sig.getInputFields()),r=qo(this.sig.getOutputFields());e.push(`You will be provided with the following fields: ${n}. Your task is to generate new fields: ${r}.`);let s=this.functions?.map(c=>"toFunction"in c?c.toFunction():c)?.flat()?.map(c=>`- \`${c.name}\`: ${$n(c.description)}`).join(`
337
- `);s&&s.length>0&&e.push(`## Available Functions
338
- ${s}`);let a=this.getFieldNameToTitleMap(),l=au(this.sig.getInputFields(),a);if(e.push(`## Input Fields
339
- ${l}`),!t){let c=lu(this.sig.getOutputFields(),a);e.push(`## Output Fields
340
- ${c}`)}s&&s.length>0&&e.push(iu.trim()),t&&this.structuredOutputFunctionName?e.push(mt("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push(ip.trim());let u=this.sig.getDescription();if(u){let c=$n(u);c=zo(c,a),e.push(c)}return{type:"text",text:e.join(`
342
+ `};var Ei=/{{\s*([^}]+?)\s*}}/g,hl=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,xl=new Map;function dt(s,e,t,n){let o=e.slice(0,t).split(`
343
+ `),i=o.length,a=(o.at(-1)?.length??0)+1;return`${s}:${i}:${a} ${n}`}function Up(s){let e=[],t=0;Ei.lastIndex=0;let n=Ei.exec(s);for(;n;){let[r,o]=n,i=n.index;i>t&&e.push({type:"text",value:s.slice(t,i)}),e.push({type:"tag",value:o.trim(),index:i}),t=i+r.length,n=Ei.exec(s)}return t<s.length&&e.push({type:"text",value:s.slice(t)}),e}function Mi(s,e,t,n=0,r=new Set){let o=[],i=n;for(;i<s.length;){let a=s[i];if(a.type==="text"){o.push({type:"text",value:a.value}),i++;continue}let u=a.value;if(r.has(u))return{nodes:o,nextIndex:i,terminator:u};if(u.startsWith("if ")){let l=u.slice(3).trim();if(!hl.test(l))throw new Error(dt(t,e,a.index,`Invalid if condition '${l}'`));let c=Mi(s,e,t,i+1,new Set(["else","/if"]));if(!c.terminator)throw new Error(dt(t,e,a.index,"Unclosed 'if' block"));let p=[],d=c.nextIndex+1;if(c.terminator==="else"){let m=Mi(s,e,t,c.nextIndex+1,new Set(["/if"]));if(m.terminator!=="/if")throw new Error(dt(t,e,a.index,"Unclosed 'if' block"));p=m.nodes,d=m.nextIndex+1}o.push({type:"if",condition:l,thenNodes:c.nodes,elseNodes:p,index:a.index}),i=d;continue}if(u==="else")throw new Error(dt(t,e,a.index,"Unexpected 'else'"));if(u==="/if")throw new Error(dt(t,e,a.index,"Unexpected '/if'"));if(u.startsWith("include "))throw new Error(dt(t,e,a.index,"Unexpected 'include' directive at runtime (includes must be compiled)"));if(!hl.test(u))throw new Error(dt(t,e,a.index,`Invalid tag '${u}'`));o.push({type:"var",name:u,index:a.index}),i++}return{nodes:o,nextIndex:i}}function Al(s,e,t,n,r){let o=e.split("."),i=s;for(let a of o){if(i===null||typeof i!="object"||!(a in i))throw new Error(dt(n,t,r,`Missing template variable '${e}'`));i=i[a]}return i}function Pi(s,e,t,n){let r="";for(let o of s){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let a=Al(e,o.name,t,n,o.index);if(typeof a!="string"&&typeof a!="number"&&typeof a!="boolean")throw new Error(dt(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(a);continue}let i=Al(e,o.condition,t,n,o.index);if(typeof i!="boolean")throw new Error(dt(n,t,o.index,`Condition '${o.condition}' must be boolean`));i?r+=Pi(o.thenNodes,e,t,n):r+=Pi(o.elseNodes,e,t,n)}return r}function Bp(s,e){let t=Up(s),n=Mi(t,s,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function Ze(s,e={}){let t=fl[s],n=`template:${s}`;if(!t)throw new Error(`Unknown template id: ${String(s)}`);let r=xl.get(s);return r||(r=Bp(t,n),xl.set(s,r)),Pi(r,e,t,n)}var yl=Ze("dsp/function-call-instructions.md"),jp=Ze("dsp/strict-output-formatting-rules.md"),zp=Ze("dsp/example-disclaimer.md"),Tn=Ze("dsp/example-separator.md"),$t=class{sig;fieldTemplates;task;customInstruction;setInstruction(e){this.customInstruction=e,this.task={type:"text",text:e}}getInstruction(){return this.customInstruction}thoughtFieldName;functions;contextCache;examplesInSystem;ignoreBreakpoints;structuredOutputFunctionName;constructor(e,t,n){this.sig=e,this.fieldTemplates=n,this.thoughtFieldName=t?.thoughtFieldName??"thought",this.functions=t?.functions,this.contextCache=t?.contextCache,this.examplesInSystem=t?.examplesInSystem??!1,this.ignoreBreakpoints=t?.ignoreBreakpoints??!1,this.structuredOutputFunctionName=t?.structuredOutputFunctionName,re.useStructuredPrompt?this.task=this.buildStructuredPrompt():this.task=this.buildLegacyPrompt()}getFieldNameToTitleMap=()=>{let e=new Map;for(let t of this.sig.getInputFields())e.set(t.name,t.title);for(let t of this.sig.getOutputFields())e.set(t.name,t.title);return e};sortFieldsCachedFirst=e=>[...e].sort((t,n)=>t.isCached&&!n.isCached?-1:!t.isCached&&n.isCached?1:0);buildLegacyPrompt(){let e=[],t=this.sig.hasComplexFields(),n=Bo(this.sig.getInputFields()),r=Bo(this.sig.getOutputFields());e.push(`You will be provided with the following fields: ${n}. Your task is to generate new fields: ${r}.`);let i=this.functions?.map(c=>"toFunction"in c?c.toFunction():c)?.flat()?.map(c=>`- \`${c.name}\`: ${wn(c.description)}`).join(`
344
+ `);i&&i.length>0&&e.push(`## Available Functions
345
+ ${i}`);let a=this.getFieldNameToTitleMap(),u=bl(this.sig.getInputFields(),a);if(e.push(`## Input Fields
346
+ ${u}`),!t){let c=Il(this.sig.getOutputFields(),a);e.push(`## Output Fields
347
+ ${c}`)}i&&i.length>0&&e.push(yl.trim()),t&&this.structuredOutputFunctionName?e.push(Ze("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push(jp.trim());let l=this.sig.getDescription();if(l){let c=wn(l);c=jo(c,a),e.push(c)}return{type:"text",text:e.join(`
341
348
 
342
349
  `)}}buildStructuredPrompt(){let e=[],t=this.sig.hasComplexFields();e.push("<identity>"),e.push(this.buildIdentitySection()),e.push("</identity>");let n=this.buildTaskDefinitionSection();n&&(e.push(`
343
350
  <task_definition>`),e.push(n),e.push("</task_definition>"));let r=this.functions?.flatMap(o=>"toFunction"in o?o.toFunction():o);return r&&r.length>0&&(e.push(`
@@ -345,74 +352,74 @@ ${c}`)}s&&s.length>0&&e.push(iu.trim()),t&&this.structuredOutputFunctionName?e.p
345
352
  <input_fields>`),e.push(this.buildInputFieldsSection()),e.push("</input_fields>"),t||(e.push(`
346
353
  <output_fields>`),e.push(this.buildOutputFieldsSection()),e.push("</output_fields>")),e.push(`
347
354
  <formatting_rules>`),e.push(this.buildFormattingRulesSection()),e.push("</formatting_rules>"),{type:"text",text:e.join(`
348
- `)}}buildIdentitySection(){let e=qo(this.sig.getInputFields()),t=qo(this.sig.getOutputFields());return`You will be provided with the following fields: ${e}. Your task is to generate new fields: ${t}.`}buildTaskDefinitionSection(){let e=this.sig.getDescription();if(!e)return"";let t=this.getFieldNameToTitleMap(),n=$n(e);return n=zo(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
349
- `);let n=e.map(r=>`- \`${r.name}\`: ${$n(r.description??"")}`).join(`
355
+ `)}}buildIdentitySection(){let e=Bo(this.sig.getInputFields()),t=Bo(this.sig.getOutputFields());return`You will be provided with the following fields: ${e}. Your task is to generate new fields: ${t}.`}buildTaskDefinitionSection(){let e=this.sig.getDescription();if(!e)return"";let t=this.getFieldNameToTitleMap(),n=wn(e);return n=jo(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
356
+ `);let n=e.map(r=>`- \`${r.name}\`: ${wn(r.description??"")}`).join(`
350
357
  `);return t.push(n),t.push(`
351
- ${iu.trim()}`),t.join(`
358
+ ${yl.trim()}`),t.join(`
352
359
  `)}buildInputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Input Fields**: The following fields will be provided to you:
353
360
 
354
- ${au(this.sig.getInputFields(),e)}`}buildOutputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Output Fields**: You must generate the following fields:
361
+ ${bl(this.sig.getInputFields(),e)}`}buildOutputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Output Fields**: You must generate the following fields:
355
362
 
356
- ${lu(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?mt("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?mt("dsp/formatting-rules-structured-json.md").trim():mt("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),s;if(r)s=o;else{let l=[...t,...n];if(this.contextCache&&l.length>0){let u=l.length-1,c=l[u];c&&(l[u]={...c,cache:!0})}s=[...l,...o]}let a=s.filter(l=>l!==void 0);return a.every(l=>l.type==="text")?a.map(l=>l.text).join(`
357
- `):a.reduce(Wt(`
363
+ ${Il(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?Ze("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?Ze("dsp/formatting-rules-structured-json.md").trim():Ze("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),i;if(r)i=o;else{let u=[...t,...n];if(this.contextCache&&u.length>0){let l=u.length-1,c=u[l];c&&(u[l]={...c,cache:!0})}i=[...u,...o]}let a=i.filter(u=>u!==void 0);return a.every(u=>u.type==="text")?a.map(u=>u.text).join(`
364
+ `):a.reduce(Lt(`
358
365
  `),[])};render=(e,{examples:t,demos:n})=>{if(!this.examplesInSystem)return this.renderWithMessagePairs(e,{examples:t,demos:n});let r=t?[{type:"text",text:`
359
366
 
360
367
  ## Examples
361
- `},...this.renderExamples(t)]:[],o=n?this.renderDemos(n):[],s=r.every(d=>d.type==="text"),a=o.every(d=>d.type==="text"),l=s&&a,u=this.task.text;if(l){let d=[{type:"text",text:u},...r,...o];d.reduce(Wt(""),[]),d?.[0]&&(u=d[0].text)}let c={role:"system",content:u,cache:!!this.contextCache};if(Array.isArray(e)){let d=[],m=e,g=!0;for(let h of m){let f;if(g?(f=this.renderSingleValueUserContent(h.values,r,o,l),g=!1):f=this.renderSingleValueUserContent(h.values,[],[],!1),h.role==="user"){d.push({role:"user",content:f});continue}if(h.role!=="assistant")throw new Error("Invalid message role");if(typeof f!="string")throw new Error("Assistant message cannot contain non-text content like images, files,etc");d.push({role:"assistant",content:f})}return[c,...d]}let p=this.renderSingleValueUserContent(e,r,o,l);return[c,{role:"user",content:p}]};renderWithMessagePairs=(e,{examples:t,demos:n})=>{let r=t&&t.length>0||n&&n.length>0,s={role:"system",content:r?`${this.task.text}
362
- ${ap}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],l=n?this.renderDemosAsMessages(n):[],u=[];for(let A of[...a,...l])u.push(A.userMessage),u.push(A.assistantMessage),A.functionResultMessage&&u.push(A.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&u.length>0&&p){let A=u.length-1,T=u[A];T?.role==="assistant"&&(u[A]={...T,cache:!0})}if(Array.isArray(e)){let A=[],T=e,k=!0;for(let E of T){let M=this.renderInputFields(E.values),$=M.every(B=>B.type==="text")?M.map(B=>B.text).join(`
363
- `):M.reduce(Wt(`
364
- `),[]);if(E.role==="user"){k&&r&&(typeof $=="string"?$=Nn+$:$=[{type:"text",text:Nn},...$],k=!1),A.push({role:"user",content:$});continue}if(E.role!=="assistant")throw new Error("Invalid message role");if(typeof $!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");A.push({role:"assistant",content:$})}return[s,...u,...A]}let d=this.sig.getInputFields(),m=d.filter(A=>A.isCached),g=d.filter(A=>!A.isCached),h=m.length>0;if(this.contextCache&&h&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let A=m.map(M=>this.renderInField(M,e,void 0)).filter(M=>M!==void 0).flat();A.filter(M=>M.type==="text").forEach(M=>{M.text=`${M.text}
365
- `});let T=A.every(M=>M.type==="text")?A.map(M=>M.text).join(`
366
- `):A.reduce(Wt(`
367
- `),[]);r&&(typeof T=="string"?T=Nn+T:T=[{type:"text",text:Nn},...T]);let k=g.map(M=>this.renderInField(M,e,void 0)).filter(M=>M!==void 0).flat();k.filter(M=>M.type==="text").forEach(M=>{M.text=`${M.text}
368
- `});let E=k.every(M=>M.type==="text")?k.map(M=>M.text).join(`
369
- `):k.reduce(Wt(`
370
- `),[]);return[s,...u,{role:"user",content:T,cache:!0},{role:"user",content:E}]}let x=this.sortFieldsCachedFirst(d).map(A=>this.renderInField(A,e,void 0)).filter(A=>A!==void 0).flat();x.filter(A=>A.type==="text").forEach(A=>{A.text=`${A.text}
371
- `});let I=x.every(A=>A.type==="text")?x.map(A=>A.text).join(`
372
- `):x.reduce(Wt(`
373
- `),[]);r&&(typeof I=="string"?I=Nn+I:I=[{type:"text",text:Nn},...I]);let w=h&&g.length===0&&this.contextCache;return[s,...u,{role:"user",content:I,...w?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((s,a)=>{let l=a.title;return s[l]||(s[l]=[]),s[l].push(a),s},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([s,a])=>{if(a.length===1){let l=a[0];return l.type?.name==="object"||l.type?.isArray&&l.type.fields?{title:s,name:l.name,description:`${l.description}
374
- IMPORTANT: Provide the FULL JSON object for this field, matching the schema exactly.`}:{title:s,name:l.name,description:l.description}}if(a.length>1){let l=a.map(u=>`- ${u.description}`).join(`
375
- `);return{title:s,name:a[0].name,description:l}}}).filter(Boolean).forEach(s=>{let a=this.fieldTemplates?.[s.name]??this.defaultRenderInField;t.push(...a(s,s.description))}),t};renderExamples=e=>{let t=[],n={isExample:!0},r=this.sig.hasComplexFields();for(let[o,s]of e.entries()){if(r){let c=this.sig.getInputFields().map(h=>this.renderInField(h,s,{...n,isInputField:!0})).filter(h=>h!==void 0).flat(),p=this.sig.getOutputFields(),d={};for(let h of p)h.name in s&&(d[h.name]=s[h.name]);let m=JSON.stringify(d,null,2),g=[...c,{type:"text",text:`\`\`\`json
368
+ `},...this.renderExamples(t)]:[],o=n?this.renderDemos(n):[],i=r.every(d=>d.type==="text"),a=o.every(d=>d.type==="text"),u=i&&a,l=this.task.text;if(u){let d=[{type:"text",text:l},...r,...o];d.reduce(Lt(""),[]),d?.[0]&&(l=d[0].text)}let c={role:"system",content:l,cache:!!this.contextCache};if(Array.isArray(e)){let d=[],m=e,g=!0;for(let f of m){let x;if(g?(x=this.renderSingleValueUserContent(f.values,r,o,u),g=!1):x=this.renderSingleValueUserContent(f.values,[],[],!1),f.role==="user"){d.push({role:"user",content:x});continue}if(f.role!=="assistant")throw new Error("Invalid message role");if(typeof x!="string")throw new Error("Assistant message cannot contain non-text content like images, files,etc");d.push({role:"assistant",content:x})}return[c,...d]}let p=this.renderSingleValueUserContent(e,r,o,u);return[c,{role:"user",content:p}]};renderWithMessagePairs=(e,{examples:t,demos:n})=>{let r=t&&t.length>0||n&&n.length>0,i={role:"system",content:r?`${this.task.text}
369
+ ${zp}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],u=n?this.renderDemosAsMessages(n):[],l=[];for(let I of[...a,...u])l.push(I.userMessage),l.push(I.assistantMessage),I.functionResultMessage&&l.push(I.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&l.length>0&&p){let I=l.length-1,T=l[I];T?.role==="assistant"&&(l[I]={...T,cache:!0})}if(Array.isArray(e)){let I=[],T=e,R=!0;for(let k of T){let v=this.renderInputFields(k.values),O=v.every(P=>P.type==="text")?v.map(P=>P.text).join(`
370
+ `):v.reduce(Lt(`
371
+ `),[]);if(k.role==="user"){R&&r&&(typeof O=="string"?O=Tn+O:O=[{type:"text",text:Tn},...O],R=!1),I.push({role:"user",content:O});continue}if(k.role!=="assistant")throw new Error("Invalid message role");if(typeof O!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");I.push({role:"assistant",content:O})}return[i,...l,...I]}let d=this.sig.getInputFields(),m=d.filter(I=>I.isCached),g=d.filter(I=>!I.isCached),f=m.length>0;if(this.contextCache&&f&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let I=m.map(v=>this.renderInField(v,e,void 0)).filter(v=>v!==void 0).flat();I.filter(v=>v.type==="text").forEach(v=>{v.text=`${v.text}
372
+ `});let T=I.every(v=>v.type==="text")?I.map(v=>v.text).join(`
373
+ `):I.reduce(Lt(`
374
+ `),[]);r&&(typeof T=="string"?T=Tn+T:T=[{type:"text",text:Tn},...T]);let R=g.map(v=>this.renderInField(v,e,void 0)).filter(v=>v!==void 0).flat();R.filter(v=>v.type==="text").forEach(v=>{v.text=`${v.text}
375
+ `});let k=R.every(v=>v.type==="text")?R.map(v=>v.text).join(`
376
+ `):R.reduce(Lt(`
377
+ `),[]);return[i,...l,{role:"user",content:T,cache:!0},{role:"user",content:k}]}let h=this.sortFieldsCachedFirst(d).map(I=>this.renderInField(I,e,void 0)).filter(I=>I!==void 0).flat();h.filter(I=>I.type==="text").forEach(I=>{I.text=`${I.text}
378
+ `});let A=h.every(I=>I.type==="text")?h.map(I=>I.text).join(`
379
+ `):h.reduce(Lt(`
380
+ `),[]);r&&(typeof A=="string"?A=Tn+A:A=[{type:"text",text:Tn},...A]);let b=f&&g.length===0&&this.contextCache;return[i,...l,{role:"user",content:A,...b?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((i,a)=>{let u=a.title;return i[u]||(i[u]=[]),i[u].push(a),i},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([i,a])=>{if(a.length===1){let u=a[0];return u.type?.name==="object"||u.type?.isArray&&u.type.fields?{title:i,name:u.name,description:`${u.description}
381
+ IMPORTANT: Provide the FULL JSON object for this field, matching the schema exactly.`}:{title:i,name:u.name,description:u.description}}if(a.length>1){let u=a.map(l=>`- ${l.description}`).join(`
382
+ `);return{title:i,name:a[0].name,description:u}}}).filter(Boolean).forEach(i=>{let a=this.fieldTemplates?.[i.name]??this.defaultRenderInField;t.push(...a(i,i.description))}),t};renderExamples=e=>{let t=[],n={isExample:!0},r=this.sig.hasComplexFields();for(let[o,i]of e.entries()){if(r){let c=this.sig.getInputFields().map(f=>this.renderInField(f,i,{...n,isInputField:!0})).filter(f=>f!==void 0).flat(),p=this.sig.getOutputFields(),d={};for(let f of p)f.name in i&&(d[f.name]=i[f.name]);let m=JSON.stringify(d,null,2),g=[...c,{type:"text",text:`\`\`\`json
376
383
  ${m}
377
384
  \`\`\`
378
385
  `}];o>0&&g.length>0&&g[0]?.type==="text"&&t.push({type:"text",text:`---
379
386
 
380
- `}),g.forEach(h=>{h&&("text"in h&&(h.text=`${h.text}
381
- `),t.push(h))});continue}let a=this.sig.getInputFields().map(c=>this.renderInField(c,s,{...n,isInputField:!0})).filter(c=>c!==void 0).flat(),l=this.sig.getOutputFields().map(c=>this.renderInField(c,s,{...n,isInputField:!1})).filter(c=>c!==void 0).flat(),u=[...a,...l];o>0&&u.length>0&&u[0]?.type==="text"&&t.push({type:"text",text:`---
387
+ `}),g.forEach(f=>{f&&("text"in f&&(f.text=`${f.text}
388
+ `),t.push(f))});continue}let a=this.sig.getInputFields().map(c=>this.renderInField(c,i,{...n,isInputField:!0})).filter(c=>c!==void 0).flat(),u=this.sig.getOutputFields().map(c=>this.renderInField(c,i,{...n,isInputField:!1})).filter(c=>c!==void 0).flat(),l=[...a,...u];o>0&&l.length>0&&l[0]?.type==="text"&&t.push({type:"text",text:`---
382
389
 
383
- `}),u.forEach(c=>{"text"in c&&(c.text=`${c.text}
384
- `),t.push(c)})}return t};renderDemos=e=>{let t=[],n=this.sig.getInputFields(),r=this.sig.getOutputFields(),o={isExample:!0},s=this.sig.hasComplexFields();for(let a of e){if(s){let p=n.map(h=>this.renderInField(h,a,{...o,isInputField:!0})).filter(h=>h!==void 0).flat(),d={};for(let h of r)h.name in a&&(d[h.name]=a[h.name]);let m=JSON.stringify(d,null,2);[...p,{type:"text",text:`\`\`\`json
390
+ `}),l.forEach(c=>{"text"in c&&(c.text=`${c.text}
391
+ `),t.push(c)})}return t};renderDemos=e=>{let t=[],n=this.sig.getInputFields(),r=this.sig.getOutputFields(),o={isExample:!0},i=this.sig.hasComplexFields();for(let a of e){if(i){let p=n.map(f=>this.renderInField(f,a,{...o,isInputField:!0})).filter(f=>f!==void 0).flat(),d={};for(let f of r)f.name in a&&(d[f.name]=a[f.name]);let m=JSON.stringify(d,null,2);[...p,{type:"text",text:`\`\`\`json
385
392
  ${m}
386
393
  \`\`\`
387
- `}].slice(0,-1).forEach(h=>{"text"in h&&(h.text=`${h.text}
388
- `),t.push(h)});continue}let l=n.map(p=>this.renderInField(p,a,{...o,isInputField:!0})).filter(p=>p!==void 0).flat(),u=r.map(p=>this.renderInField(p,a,{...o,isInputField:!1})).filter(p=>p!==void 0).flat();[...l,...u].slice(0,-1).forEach(p=>{"text"in p&&(p.text=`${p.text}
389
- `),t.push(p)})}return t};renderExamplesAsMessages=e=>{let t=[],n={isExample:!0},r=this.sig.hasComplexFields();for(let o of e){let a=this.sortFieldsCachedFirst(this.sig.getInputFields()).map(d=>this.renderInField(d,o,{...n,isInputField:!0})).filter(d=>d!==void 0).flat(),l=a.every(d=>d.type==="text")?a.map(d=>d.text).join(`
390
- `):a.reduce(Wt(`
391
- `),[]);if(r&&this.structuredOutputFunctionName){let d={};for(let h of this.sig.getOutputFields())h.name in o&&(d[h.name]=o[h.name]);if(typeof l=="string"&&l.trim()===""||Array.isArray(l)&&l.length===0||Object.keys(d).length===0)continue;let g=`example-${t.length}`;t.push({userMessage:{role:"user",content:l},assistantMessage:{role:"assistant",functionCalls:[{id:g,type:"function",function:{name:this.structuredOutputFunctionName,params:d}}]},functionResultMessage:{role:"function",result:"done",functionId:g}});continue}let u;if(r){let d={};for(let m of this.sig.getOutputFields())m.name in o&&(d[m.name]=o[m.name]);u=JSON.stringify(d,null,2)}else u=this.sig.getOutputFields().map(m=>this.renderInField(m,o,{...n,isInputField:!1})).filter(m=>m!==void 0).flat().filter(m=>m.type==="text").map(m=>m.text).join(`
392
- `);let c=u.trim()==="";typeof l=="string"&&l.trim()===""||Array.isArray(l)&&l.length===0||c||t.push({userMessage:{role:"user",content:l},assistantMessage:{role:"assistant",content:u}})}return t};renderDemosAsMessages=e=>this.renderExamplesAsMessages(e);renderInputFields=e=>{let n=this.sortFieldsCachedFirst(this.sig.getInputFields()).map(r=>this.renderInField(r,e,void 0)).filter(r=>r!==void 0).flat();return n.filter(r=>r.type==="text").forEach(r=>{r.text=`${r.text}
393
- `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(cp(e,r,n))return;e.type&&Tr(e,r);let o=lp(e,r);return(this.fieldTemplates?.[e.name]??this.defaultRenderInField)(e,o)};defaultRenderInField=(e,t)=>{if(e.type?.name==="image"){let r=s=>{if(!s)throw new Error("Image field value is required.");if(typeof s!="object")throw new Error("Image field value must be an object.");if(!("mimeType"in s))throw new Error("Image field must have mimeType");if(!("data"in s))throw new Error("Image field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Image field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"image",mimeType:a.mimeType,image:a.data}}))}else{let s=r(t);o.push({type:"image",mimeType:s.mimeType,image:s.data})}return o}if(e.type?.name==="audio"){let r=s=>{if(!s)throw new Error("Audio field value is required.");if(typeof s!="object")throw new Error("Audio field value must be an object.");if(!("data"in s))throw new Error("Audio field must have data");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Audio field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"audio",format:a.format??"wav",data:a.data}}))}else{let s=r(t);o.push({type:"audio",format:s.format??"wav",data:s.data})}return o}if(e.type?.name==="file"){let r=s=>{if(!s)throw new Error("File field value is required.");if(typeof s!="object")throw new Error("File field value must be an object.");if(!("mimeType"in s))throw new Error("File field must have mimeType");let a="data"in s,l="fileUri"in s;if(!a&&!l)throw new Error("File field must have either data or fileUri");if(a&&l)throw new Error("File field cannot have both data and fileUri");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("File field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return"fileUri"in a?{type:"file",mimeType:a.mimeType,fileUri:a.fileUri}:{type:"file",mimeType:a.mimeType,data:a.data}}))}else{let s=r(t);o.push("fileUri"in s?{type:"file",mimeType:s.mimeType,fileUri:s.fileUri}:{type:"file",mimeType:s.mimeType,data:s.data})}return o}if(e.type?.name==="url"){let r=s=>{if(!s)throw new Error("URL field value is required.");if(typeof s=="string")return{url:s};if(typeof s!="object")throw new Error("URL field value must be a string or object.");if(!("url"in s))throw new Error("URL field must have url property");return s},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("URL field value must be an array.");o=o.concat(t.map(s=>{let a=r(s);return{type:"url",url:a.url,...a.title?{title:a.title}:{},...a.description?{description:a.description}:{}}}))}else{let s=r(t);o.push({type:"url",url:s.url,...s.title?{title:s.title}:{},...s.description?{description:s.description}:{}})}return o}let n=[e.title,": "];return Array.isArray(t)?(n.push(`
394
+ `}].slice(0,-1).forEach(f=>{"text"in f&&(f.text=`${f.text}
395
+ `),t.push(f)});continue}let u=n.map(p=>this.renderInField(p,a,{...o,isInputField:!0})).filter(p=>p!==void 0).flat(),l=r.map(p=>this.renderInField(p,a,{...o,isInputField:!1})).filter(p=>p!==void 0).flat();[...u,...l].slice(0,-1).forEach(p=>{"text"in p&&(p.text=`${p.text}
396
+ `),t.push(p)})}return t};renderExamplesAsMessages=e=>{let t=[],n={isExample:!0},r=this.sig.hasComplexFields();for(let o of e){let a=this.sortFieldsCachedFirst(this.sig.getInputFields()).map(d=>this.renderInField(d,o,{...n,isInputField:!0})).filter(d=>d!==void 0).flat(),u=a.every(d=>d.type==="text")?a.map(d=>d.text).join(`
397
+ `):a.reduce(Lt(`
398
+ `),[]);if(r&&this.structuredOutputFunctionName){let d={};for(let f of this.sig.getOutputFields())f.name in o&&(d[f.name]=o[f.name]);if(typeof u=="string"&&u.trim()===""||Array.isArray(u)&&u.length===0||Object.keys(d).length===0)continue;let g=`example-${t.length}`;t.push({userMessage:{role:"user",content:u},assistantMessage:{role:"assistant",functionCalls:[{id:g,type:"function",function:{name:this.structuredOutputFunctionName,params:d}}]},functionResultMessage:{role:"function",result:"done",functionId:g}});continue}let l;if(r){let d={};for(let m of this.sig.getOutputFields())m.name in o&&(d[m.name]=o[m.name]);l=JSON.stringify(d,null,2)}else l=this.sig.getOutputFields().map(m=>this.renderInField(m,o,{...n,isInputField:!1})).filter(m=>m!==void 0).flat().filter(m=>m.type==="text").map(m=>m.text).join(`
399
+ `);let c=l.trim()==="";typeof u=="string"&&u.trim()===""||Array.isArray(u)&&u.length===0||c||t.push({userMessage:{role:"user",content:u},assistantMessage:{role:"assistant",content:l}})}return t};renderDemosAsMessages=e=>this.renderExamplesAsMessages(e);renderInputFields=e=>{let n=this.sortFieldsCachedFirst(this.sig.getInputFields()).map(r=>this.renderInField(r,e,void 0)).filter(r=>r!==void 0).flat();return n.filter(r=>r.type==="text").forEach(r=>{r.text=`${r.text}
400
+ `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(Kp(e,r,n))return;e.type&&br(e,r);let o=qp(e,r);return(this.fieldTemplates?.[e.name]??this.defaultRenderInField)(e,o)};defaultRenderInField=(e,t)=>{if(e.type?.name==="image"){let r=i=>{if(!i)throw new Error("Image field value is required.");if(typeof i!="object")throw new Error("Image field value must be an object.");if(!("mimeType"in i))throw new Error("Image field must have mimeType");if(!("data"in i))throw new Error("Image field must have data");return i},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Image field value must be an array.");o=o.concat(t.map(i=>{let a=r(i);return{type:"image",mimeType:a.mimeType,image:a.data}}))}else{let i=r(t);o.push({type:"image",mimeType:i.mimeType,image:i.data})}return o}if(e.type?.name==="audio"){let r=i=>{if(!i)throw new Error("Audio field value is required.");if(typeof i!="object")throw new Error("Audio field value must be an object.");if(!("data"in i))throw new Error("Audio field must have data");return i},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("Audio field value must be an array.");o=o.concat(t.map(i=>{let a=r(i);return{type:"audio",format:a.format??"wav",data:a.data}}))}else{let i=r(t);o.push({type:"audio",format:i.format??"wav",data:i.data})}return o}if(e.type?.name==="file"){let r=i=>{if(!i)throw new Error("File field value is required.");if(typeof i!="object")throw new Error("File field value must be an object.");if(!("mimeType"in i))throw new Error("File field must have mimeType");let a="data"in i,u="fileUri"in i;if(!a&&!u)throw new Error("File field must have either data or fileUri");if(a&&u)throw new Error("File field cannot have both data and fileUri");return i},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("File field value must be an array.");o=o.concat(t.map(i=>{let a=r(i);return"fileUri"in a?{type:"file",mimeType:a.mimeType,fileUri:a.fileUri}:{type:"file",mimeType:a.mimeType,data:a.data}}))}else{let i=r(t);o.push("fileUri"in i?{type:"file",mimeType:i.mimeType,fileUri:i.fileUri}:{type:"file",mimeType:i.mimeType,data:i.data})}return o}if(e.type?.name==="url"){let r=i=>{if(!i)throw new Error("URL field value is required.");if(typeof i=="string")return{url:i};if(typeof i!="object")throw new Error("URL field value must be a string or object.");if(!("url"in i))throw new Error("URL field must have url property");return i},o=[{type:"text",text:`${e.title}: `}];if(e.type.isArray){if(!Array.isArray(t))throw new Error("URL field value must be an array.");o=o.concat(t.map(i=>{let a=r(i);return{type:"url",url:a.url,...a.title?{title:a.title}:{},...a.description?{description:a.description}:{}}}))}else{let i=r(t);o.push({type:"url",url:i.url,...i.title?{title:i.title}:{},...i.description?{description:i.description}:{}})}return o}let n=[e.title,": "];return Array.isArray(t)?(n.push(`
394
401
  `),n.push(t.map(r=>`- ${r}`).join(`
395
- `))):n.push(t),[{type:"text",text:n.join("")}]}},qo=i=>i.map(e=>`\`${e.title}\``).join(", "),au=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?Ln(n.type):"string",s=n.isOptional?`This optional ${o} field may be omitted`:`${/^[aeiou]/i.test(o)?"An":"A"} ${o} field`,a="";if(n.description){let l=$n(n.description);e&&(l=zo(l,e)),a=` ${l}`}return`${r}: (${s})${a}`.trim()}).join(`
396
- `),lu=(i,e)=>i.map(n=>{let r=n.title,o=n.type?.name?Ln(n.type):"string",s=n.isOptional?`Only include this ${o} field if its value is available`:`This ${o} field must be included`,a="";if(n.description&&n.description.length>0){let l=n.type?.name==="class"?n.description:$n(n.description);e&&(l=zo(l,e)),a=` ${l}`}return n.type?.options&&n.type.options.length>0&&(a.length>0&&(a+=". "),a+=`Allowed values: ${n.type.options.join(", ")}`),`${r}: (${s})${a}`.trim()}).join(`
397
- `),lp=(i,e)=>{if(i.type?.name==="date"&&e instanceof Date){let t=e.toISOString();return t.slice(0,t.indexOf("T"))}return i.type?.name==="datetime"&&e instanceof Date?Ul(e):i.type?.name==="image"&&typeof e=="object"||i.type?.name==="audio"&&typeof e=="object"||i.type?.name==="file"&&typeof e=="object"||i.type?.name==="url"&&(typeof e=="string"||typeof e=="object")||typeof e=="string"?e:JSON.stringify(e,null,2)};function up(i){return`{ ${Object.entries(i).map(([t,n])=>{let r=n.isOptional?"?":"",o=Ln({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var Ln=i=>{let e=(()=>{switch(i?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean (true or false)";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"file":return"file (with filename, mimeType, and data)";case"url":return"URL (string or object with url, title, description)";case"object":return i?.fields?`object ${up(i.fields)}`:"object";default:return"string"}})();return i?.isArray?`json array of ${e} items`:e};function Wt(i){return(e,t)=>{if(t.type==="text"){let n=e.length>0?e[e.length-1]:null;n&&n.type==="text"?n.text+=i+t.text:e.push(t)}else e.push(t);return e}}var cp=(i,e,t)=>{if(typeof e=="boolean"||i?.type?.name==="number"&&typeof e=="number")return!1;if(!e||(Array.isArray(e)||typeof e=="string")&&e.length===0){if(t?.isExample||i.isOptional||i.isInternal)return!0;let n=t?.isInputField!==!1?"input":"output";throw new Error(`Value for ${n} field '${i.name}' is required.`)}return!1};function $n(i){let e=i.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function zo(i,e){if(e.size===0)return i;let t=i,n=Array.from(e.keys()).sort((r,o)=>o.length-r.length);for(let r of n){let o=e.get(r),s=new RegExp(`\`${r}\``,"g");t=t.replace(s,`\`${o}\``);let a=new RegExp(`"${r}"`,"g");t=t.replace(a,`"${o}"`);let l=new RegExp(`'${r}'`,"g");t=t.replace(l,`'${o}'`);let u=new RegExp(`\\[${r}\\]`,"g");t=t.replace(u,`[${o}]`);let c=new RegExp(`\\(${r}\\)`,"g");t=t.replace(c,`(${o})`);let p=new RegExp(`\\$${r}\\b`,"g");t=t.replace(p,`\`${o}\``)}return t}function pp(i,e){let t=i.history(0,e),n=t.some(o=>o.role==="function");return t.some(o=>o.role==="assistant"&&"functionCalls"in o&&Array.isArray(o.functionCalls)&&o.functionCalls.length>0)&&n}function dp(i,e){let t=i.history(0,e),n=[],r=t.filter(s=>s.role==="assistant"&&"functionCalls"in s&&Array.isArray(s.functionCalls)&&s.functionCalls.length>0),o=t.filter(s=>s.role==="function");for(let s of r)if("functionCalls"in s&&s.functionCalls)for(let a of s.functionCalls){let l=o.find(u=>"functionId"in u&&u.functionId===a.id);l&&"result"in l&&"functionId"in l&&n.push({index:n.length,functionName:a.function.name,functionId:a.id,args:a.function.params||"",result:String(l.result),isError:"isError"in l?!!l.isError:!1})}return n}async function Ho(i,e,t,n){if(!e?.resultPicker||i.length<=1)return 0;let r=e.resultPicker;if((t?pp(t,n):!1)&&t){let l=dp(t,n),u=await r({type:"function",results:l});if(u<0||u>=l.length)throw new Error(`Result picker returned invalid index: ${u}. Must be between 0 and ${l.length-1}`);return u}let s=i.map((l,u)=>({index:u,sample:l.delta})),a=await r({type:"fields",results:s});if(a<0||a>=i.length)throw new Error(`Result picker returned invalid index: ${a}. Must be between 0 and ${i.length-1}`);return a}async function uu(i,e,t){let n=i?.getLast(e);if(!n||n.role!=="assistant"||n.chat.length<=1)return 0;let r=n.chat.map(s=>({version:0,index:s.index,delta:s.value}));return await Ho(r,t,i,e)}var Dn=class{_stepIndex=0;maxSteps;_functionsExecuted=new Set;_lastFunctionCalls=[];_usage={promptTokens:0,completionTokens:0,totalTokens:0};state=new Map;_pendingOptions={};_functionsToAdd=[];_functionsToRemove=[];_stopRequested=!1;_stopResultValues;constructor(e){this.maxSteps=e}get stepIndex(){return this._stepIndex}get isFirstStep(){return this._stepIndex===0}get functionsExecuted(){return this._functionsExecuted}get lastFunctionCalls(){return this._lastFunctionCalls}get usage(){return this._usage}setModel(e){this._pendingOptions.model=e}setThinkingBudget(e){this._pendingOptions.thinkingTokenBudget=e}setTemperature(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.temperature=e}setMaxTokens(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.maxTokens=e}setOptions(e){Object.assign(this._pendingOptions,e)}addFunctions(e){this._functionsToAdd.push(...e)}removeFunctions(...e){this._functionsToRemove.push(...e)}stop(e){this._stopRequested=!0,this._stopResultValues=e}_beginStep(e){this._stepIndex=e,this._functionsExecuted=new Set,this._lastFunctionCalls=[]}_recordFunctionCall(e,t,n){this._functionsExecuted.add(e.toLowerCase()),this._lastFunctionCalls.push({name:e,args:t,result:n})}_addUsage(e,t,n){this._usage.promptTokens+=e,this._usage.completionTokens+=t,this._usage.totalTokens+=n}_consumePendingOptions(){if(Object.keys(this._pendingOptions).length===0)return;let e=this._pendingOptions;return this._pendingOptions={},e}_consumeFunctionsToAdd(){if(this._functionsToAdd.length===0)return;let e=this._functionsToAdd;return this._functionsToAdd=[],e}_consumeFunctionsToRemove(){if(this._functionsToRemove.length===0)return;let e=this._functionsToRemove;return this._functionsToRemove=[],e}get _isStopRequested(){return this._stopRequested}get _stopValues(){return this._stopResultValues}};var Ko=class{tools;logger;constructor(e,t){this.tools=new Map(e.map(n=>[n.name,n])),this.logger=t}getToolParamFieldMap(){let e=new Map;for(let[,t]of this.tools.entries())if(t.parameters?.properties&&Object.keys(t.parameters.properties).length>0){let{paramFieldMap:n}=mp(t);e.set(t.name,n)}else e.set(t.name,new Map);return e}async route(e,t){let n=[],r={},o=new Map,s=new Map;for(let[a,l]of this.tools.entries())s.set(a,this.buildSanitizedFieldMap(l));for(let[a,l]of Object.entries(e)){let u=this.tools.get(this.normalizeToolName(a));if(u){l!=null&&typeof l=="object"&&o.set(u.name,l);continue}r[a]=l}for(let[a,l]of Object.entries(e))for(let[u,c]of this.tools.entries()){let p=s.get(u);if(!p)continue;let d=p.get(a);if(!d)continue;let m=o.get(c.name)??{};this.setNested(m,d,l),o.set(c.name,m)}for(let[a,l]of this.tools.entries()){let u=o.get(l.name);if(!(!u||Object.keys(u).length===0)){if(l.parameters&&l.parameters.type==="object"){let p=(l.parameters.required||[]).filter(d=>u[d]===void 0);if(p.length>0)throw new Ke(`Missing required arguments for tool '${l.name}': ${p.join(", ")}`)}n.push({id:l.name,name:l.name,args:JSON.stringify(u)})}}return{functionCalls:n,remainingFields:r}}normalizeToolName(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}sanitizeFieldName(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}buildSanitizedFieldMap(e){let t=new Map;if(!e.parameters||!("properties"in e.parameters))return t;let n=(r,o)=>{for(let[s,a]of Object.entries(r)){let l=[...o,s];if(a&&a.type==="object"&&a.properties)n(a.properties,l);else{let u=`${e.name}.${l.join(".")}`,c=this.sanitizeFieldName(u);t.set(c,l)}}};return n(e.parameters.properties??{},[]),t}setNested(e,t,n){let r=e;for(let o=0;o<t.length-1;o++){let s=t[o],a=r[s];(typeof a!="object"||a===null)&&(r[s]={}),r=r[s]}r[t[t.length-1]]=n}isToolField(e){return this.tools.has(this.normalizeToolName(e))}getToolFieldNames(){return Array.from(this.tools.keys()).map(e=>e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,""))}};function mp(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=gp(c);e.push({name:fp(d),title:hp(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function gp(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function hp(i,e){return`${i} ${e.replace(/\./g," ")}`}function fp(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function du(i,e,t){let n=new Ge(e);if(t){let r=t.getToolParamFieldMap();for(let o of i){let s=r.get(o.name);if(s&&s.size>0)for(let a of s.values())n.getOutputFields().some(u=>u.name===a.name)||n.addOutputField(a);else{let a=Pi(o.name),l=pu(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:cu(o.name),type:l,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}}return{signature:n,toolParamFieldMap:r}}else{let r=new Map;for(let o of i)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:s,paramFieldMap:a}=xp(o);r.set(o.name,a);for(let l of s)n.getOutputFields().some(c=>c.name===l.name)||n.addOutputField(l)}else{let s=Pi(o.name),a=pu(o.parameters);n.getOutputFields().some(u=>u.name===s)||n.addOutputField({name:s,title:cu(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function xp(i){let e=[],t=new Map;if(!i.parameters||!i.parameters.properties)return{fields:e,paramFieldMap:t};let n=i.parameters.properties,r=i.parameters.required||[],o=(s,a,l)=>{for(let[u,c]of Object.entries(s)){let p=a?`${a}.${u}`:u,d=`${i.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Ap(c),g={name:Pi(d),title:yp(i.name,p),type:m,description:c.description||`${u} parameter for ${i.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Ap(i){switch(i.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=i.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function yp(i,e){return`${i} ${e.replace(/\./g," ")}`}function Pi(i){return i.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function cu(i){return i.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function pu(i){return!i||!i.properties||Object.keys(i.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var Mr=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Ko(e)}processSignature(e){let{signature:t}=du(this.tools,e),n=new Set(t.getOutputFields().map(o=>o.name)),r=new Set(e.getOutputFields().map(o=>o.name));return this.injectedToolFieldNames=new Set([...n].filter(o=>!r.has(o))),t}async processResults(e,t){let{functionCalls:n}=await this.router.route(e,t);return n.length>0?n:void 0}getInjectedToolFieldNames(){return Array.from(this.injectedToolFieldNames)}getRouter(){return this.router}};var Gn="__finalResult",Ee=class i extends Dt{clone=()=>new i(this.signature,this.options);promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;structuredOutputFunctionFallback=!1;activeAbortControllers=new Set;_stopRequested=!1;constructor(e,t){super(e,{description:t?.description,traceLabel:t?.traceLabel}),this.options=t,this.thoughtFieldName=t?.thoughtFieldName??"thought";let n={functions:t?.functions,thoughtFieldName:this.thoughtFieldName};this.promptTemplate=new(t?.promptTemplate??Vt)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Kt(t.functions):[],this.usage=[]}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}setInstruction(e){this.promptTemplate.setInstruction(e)}getInstruction(){return this.promptTemplate.getInstruction()}getSignatureName(){return this.signature.getDescription()||"unknown_signature"}getMetricsInstruments(){return pi()}getMergedCustomLabels(e,t){return Bt(me.customLabels,e?.getOptions?.()?.customLabels,t?.customLabels)}updateMeter(e){pi(e)}createStates(e){return Array.from({length:e},(t,n)=>({index:n,functionCalls:[],values:{},content:"",functionsExecuted:new Set,xstate:{extractedFields:[],streamedIndex:{},s:-1}}))}addAssert(e,t){this.asserts.push({fn:e,message:t})}addStreamingAssert(e,t,n){let r=this.signature.getOutputFields().find(a=>a.name===e);if(!r)throw new Error(`addStreamingAssert: field ${String(e)} not found in output signature`);let o=r.type?.name;if(!(!o||o==="string"||o==="code"))throw new Error(`addStreamingAssert: field ${String(e)} must be a string field for streaming assertions`);this.streamingAsserts.push({fieldName:String(e),fn:t,message:n})}addFieldProcessorInternal(e,t,n=!1){let r=this.signature.getOutputFields().find(o=>o.name===e);if(!r)throw new Error(`addFieldProcessor: field ${e} not found`);if(n){let o=r.type?.name;if(!(!o||o==="string"||o==="code"))throw new Error(`addFieldProcessor: field ${e} must be a text field`);this.streamingFieldProcessors.push({field:r,process:t})}else this.fieldProcessors.push({field:r,process:t})}addStreamingFieldProcessor(e,t){this.addFieldProcessorInternal(String(e),t,!0)}addFieldProcessor(e,t){this.addFieldProcessorInternal(String(e),t,!1)}async forwardSendRequest({ai:e,mem:t,options:n,traceContext:r,functions:o,functionCall:s,stepIndex:a}){let{sessionId:l,model:u,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await uu(t,l,{resultPicker:n?.resultPicker}),h=t?.history(g,l)??[];if(h.length===0)throw new Error("No chat prompt found");let f={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},C=this.isDebug(e,n),x=a===0,I=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let w,A=this.signature.getOutputFields();if(this.signature.hasComplexFields()&&!this.structuredOutputFunctionFallback){if(!e.getFeatures(u)?.structuredOutputs)throw new Error(`Complex structured outputs (object/array types) require a provider that supports structured outputs. Current provider/model (${u}) does not support this feature. Supported providers: OpenAI (GPT-4o, GPT-4.1+), Google Gemini, Anthropic (Sonnet/Opus).`);w={type:"json_schema",schema:{name:"output",strict:!0,schema:Fn(A)}}}let k=n?.contextCache?.cacheBreakpoint??"after-examples",E=e.getFeatures?.(u)?.caching?.cacheBreakpoints===!1,M=n?.contextCache&&(E||k==="after-functions"||k==="after-examples"),$=o?.length&&M?o.map((V,O)=>({...V,cache:O===o.length-1})):o;return await e.chat({chatPrompt:h,functions:$,functionCall:s,modelConfig:f,model:u,responseFormat:w},{sessionId:l,rateLimiter:c,stream:p,debug:C,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!x,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??me.abortSignal,stepIndex:a,logger:I,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto",retry:n?.retry??this.options?.retry,customLabels:n?.customLabels})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:s,states:a,stopFunctionNames:l,stepContext:u}){let{sessionId:c,functions:p}=n??{},d=n?.functionResultFormatter??this.options?.functionResultFormatter,m=n?.functionCall??this.options?.functionCall,g=this.signatureToolCallingManager,h=n?.strictMode??!1,f=n.model,C=this.usage,x=r===0,I=this.isDebug(e,n),w=this.getLogger(e,n),{functions:A,functionCall:T}=Vl(p,m,x,n);this.structuredOutputFunctionFallback&&A.filter(M=>M.name!==Gn).length===0&&(T={type:"function",function:{name:Gn}});let k=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:s,functions:A,functionCall:T,stepIndex:r});k instanceof ReadableStream?yield*Ql({ai:e,model:f,res:k,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:A,strictMode:h,span:o,states:a,usage:C,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:w,debug:I,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal}):yield*Yl({ai:e,model:f,res:k,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:A,span:o,strictMode:h,states:a,usage:C,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:w,debug:I,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,s){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(J=>J.toLowerCase()):a?[a.toLowerCase()]:void 0,u=r.maxRetries??this.options?.maxRetries??3,c=r.maxSteps??this.options?.maxSteps??25,p=r.mem??this.options?.mem??new vn,d=r.functions?Kt(r.functions):[...this.functions],m=new Dn(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let oe=e.getModelList()?.filter(re=>"model"in re);if(!oe||oe.length<2)throw new Error("Self-tuning with model selection requires the AI service to have a `models` list with at least 2 chat models. Either configure models on your AI service or disable model selection with `selfTuning: { model: false }`.")}let J=Ri(e,g,r.model?String(r.model):void 0);d.push(J)}let h={...r},f=r.stepHooks,C=d&&d.length>0,x=r.functionCallMode??this.options?.functionCallMode??"auto";C&&x==="prompt"&&(this.signatureToolCallingManager=new Mr(d)),C&&x==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new Mr(d));let I,w,A=this.options?.promptTemplate??Vt;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let T=this.signature.hasComplexFields(),k=e.getFeatures?.(r.model),E=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=T&&(E==="function"||E==="auto"&&!k?.structuredOutputs),this.structuredOutputFunctionFallback){let J={name:Gn,description:"Return the final result. Call this function with the complete output data.",parameters:Fn(this.signature.getOutputFields()),func:async()=>"done"};d.push(J),l=[...l??[],Gn.toLowerCase()]}let M=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,$={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:M,structuredOutputFunctionName:this.structuredOutputFunctionFallback?Gn:void 0};this.promptTemplate=new A(this.signature,$);let B,V=performance.now();Array.isArray(t)?(Ua(t),B=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):B=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let O=performance.now()-V,j=this.getMetricsInstruments(),F=this.getMergedCustomLabels(e,r);j&&Fo(j,"prompt_render",O,this.getSignatureName(),F);let W=performance.now();p.addRequest(B,r.sessionId);let P=performance.now()-W;j&&Fo(j,"memory_update",P,this.getSignatureName(),F);let D=new Map;n.forEach(J=>{D.set(J.index,{})});let K=()=>{let J=m._consumePendingOptions();if(J){let{modelConfig:re,...le}=J;h={...h,...le},re&&(h.modelConfig={...h.modelConfig,...re})}let ye=m._consumeFunctionsToAdd();if(ye){let re=Kt(ye);for(let le of re)d.some(we=>we.name===le.name)||d.push(le)}let oe=m._consumeFunctionsToRemove();if(oe){let re=new Set(oe.map(le=>le.toLowerCase()));for(let le=d.length-1;le>=0;le--)re.has(d[le].name.toLowerCase())&&d.splice(le,1)}},N=r?.abortSignal??me.abortSignal;e:for(let J=0;J<c;J++){if(m._beginStep(J),K(),g&&g.model!==!1){let oe=d.findIndex(re=>re.name==="adjustGeneration");if(oe!==-1){let re=h.model?String(h.model):void 0;d[oe]=Ri(e,g,re)}}if(m._isStopRequested)break;if(N?.aborted)throw new Le("between-steps",N.reason??"Aborted between steps");if(f?.beforeStep&&(await f.beforeStep(m),K(),m._isStopRequested))break;let ye=u;for(let oe=0;oe<=ye;oe++)try{let re=u+1;for(let we=0;we<re;we++){n.forEach(se=>{se.content="",se.values={},se.functionCalls=[],se.functionsExecuted=new Set,se.xstate={extractedFields:[],streamedIndex:{},s:-1}}),we>0&&D.forEach((se,ve)=>{D.set(ve,{})});let Ue=new Map;n.forEach(se=>{Ue.set(se.index,{})});try{let se=this.forwardCore({options:{...h,functions:d},ai:e,mem:p,stepIndex:J,span:o,traceContext:s,states:n,stopFunctionNames:l,stepContext:m}),ve=!1;try{for await(let Z of se)if(Z!==void 0){let Oe=Z.index,b=Z.delta,S=Ue.get(Oe)??{},R=D.get(Oe)??{},y={},_=!1;for(let q of Object.keys(b)){let L=b[q],G=S[q],U;typeof L=="string"&&(typeof G=="string"||G===void 0)?U=(G??"")+L:Array.isArray(L)&&(Array.isArray(G)||G===void 0)?U=[...G??[],...L]:U=L,S[q]=U;let H=U,Y=R[q];if(typeof H=="string"&&typeof Y=="string")if(H.startsWith(Y)){let ne=H.slice(Y.length);ne&&(y[q]=ne,_=!0,R[q]=H)}else Y.startsWith(H)||H!==Y&&(y[q]=H,_=!0,R[q]=H);else if(Array.isArray(H)&&Array.isArray(Y)){if(H.length>Y.length){let ne=H.slice(Y.length);y[q]=ne,_=!0,R[q]=H}}else JSON.stringify(H)!==JSON.stringify(Y)&&(y[q]=H,_=!0,R[q]=H)}_&&(yield{version:we,index:Z.index,delta:y})}}catch(Z){if(Z instanceof Ht){if(ve=!0,this.structuredOutputFunctionFallback){let Oe=Z.calls.find(b=>b.func.name===Gn);if(Oe?.args){let b=Oe.args;Pn(this.signature,b);let S=this.signature.getOutputFields();for(let R of n){let y={};for(let _ of S)_.name in b&&!_.isInternal&&(y[_.name]=b[_.name],R.values[_.name]=b[_.name]);yield{version:we,index:R.index,delta:y}}for(let R of n)await On(this.asserts,R.values);if(this.fieldProcessors.length>0)for(let R of n)await Rr(this.fieldProcessors,R.values,p,r.sessionId)}}}else throw Z}if(this.usage.length>0){let Z=this.usage[this.usage.length-1];Z?.tokens&&m._addUsage(Z.tokens.promptTokens??0,Z.tokens.completionTokens??0,Z.tokens.totalTokens??0)}n.some(Z=>Z.functionsExecuted.size>0)&&f?.afterFunctionExecution&&(await f.afterFunctionExecution(m),K());let ce=ve||m._isStopRequested?!1:Xl(p,l,n,h?.sessionId);if(f?.afterStep&&(await f.afterStep(m),K()),ce&&!m._isStopRequested&&!N?.aborted){let Z=this.getMetricsInstruments();Z&&Po(Z,J+1,c,this.getSignatureName(),F);continue e}if(N?.aborted)throw new Le("mid-step",N.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let be=this.getMetricsInstruments();if(be){Po(be,J+1,c,this.getSignatureName(),F);let Z=new Set;n.forEach(Oe=>{Oe.functionsExecuted.forEach(b=>Z.add(b))}),Z.size>0&&Il(be,!0,Z.size,!0,!1,this.getSignatureName(),F),Cl(be,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),F)}return}catch(se){if(se instanceof Le)throw se;w=se;let ve,ie=this.isDebug(e,r),ce=this.getLogger(e,r),be=this.getMetricsInstruments(),Z=this.getSignatureName(),Oe={error:se,errCount:we,logger:ce,metricsInstruments:be,signatureName:Z,span:o,debug:ie,customLabels:F};if(o?.recordException(se),se instanceof Ke)ve=Pl(Oe),I=se;else if(se instanceof wt)ve=Fl(Oe),I=se;else if(se instanceof Me)_l(Oe);else{if(se instanceof lt)throw se;{let b=se,S=b instanceof tt&&b.status>=500&&b.status<600,R=b instanceof He,y=b instanceof st;throw S||R||y?se:Fi(se,e,this.signature)}}if(ve&&(p.addTag("error",r.sessionId),p.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(ve)}],r.sessionId),p.addTag("correction",r.sessionId),this.signature.hasComplexFields()))for(let S of n)S.content="",S.values={},S.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let le=this.getMetricsInstruments();throw le&&mi(le,u,!1,u,this.getSignatureName(),F),Fi(new Error(`Unable to fix validation error: ${(I??w)?.message??(I??w)?.toString()??"unknown error"}
402
+ `))):n.push(t),[{type:"text",text:n.join("")}]}},Bo=s=>s.map(e=>`\`${e.title}\``).join(", "),bl=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Rn(n.type):"string",i=n.isOptional?`This optional ${o} field may be omitted`:`${/^[aeiou]/i.test(o)?"An":"A"} ${o} field`,a="";if(n.description){let u=wn(n.description);e&&(u=jo(u,e)),a=` ${u}`}return`${r}: (${i})${a}`.trim()}).join(`
403
+ `),Il=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Rn(n.type):"string",i=n.isOptional?`Only include this ${o} field if its value is available`:`This ${o} field must be included`,a="";if(n.description&&n.description.length>0){let u=n.type?.name==="class"?n.description:wn(n.description);e&&(u=jo(u,e)),a=` ${u}`}return n.type?.options&&n.type.options.length>0&&(a.length>0&&(a+=". "),a+=`Allowed values: ${n.type.options.join(", ")}`),`${r}: (${i})${a}`.trim()}).join(`
404
+ `),qp=(s,e)=>{if(s.type?.name==="date"&&e instanceof Date){let t=e.toISOString();return t.slice(0,t.indexOf("T"))}return s.type?.name==="datetime"&&e instanceof Date?Yu(e):s.type?.name==="image"&&typeof e=="object"||s.type?.name==="audio"&&typeof e=="object"||s.type?.name==="file"&&typeof e=="object"||s.type?.name==="url"&&(typeof e=="string"||typeof e=="object")||typeof e=="string"?e:JSON.stringify(e,null,2)};function Hp(s){return`{ ${Object.entries(s).map(([t,n])=>{let r=n.isOptional?"?":"",o=Rn({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var Rn=s=>{let e=(()=>{switch(s?.name){case"string":return"string";case"number":return"number";case"boolean":return"boolean (true or false)";case"date":return'date ("YYYY-MM-DD" format)';case"datetime":return'date time ("YYYY-MM-DD HH:mm Timezone" format)';case"json":return"JSON object";case"class":return"classification class";case"code":return"code";case"file":return"file (with filename, mimeType, and data)";case"url":return"URL (string or object with url, title, description)";case"object":return s?.fields?`object ${Hp(s.fields)}`:"object";default:return"string"}})();return s?.isArray?`json array of ${e} items`:e};function Lt(s){return(e,t)=>{if(t.type==="text"){let n=e.length>0?e[e.length-1]:null;n&&n.type==="text"?n.text+=s+t.text:e.push(t)}else e.push(t);return e}}var Kp=(s,e,t)=>{if(typeof e=="boolean"||s?.type?.name==="number"&&typeof e=="number")return!1;if(!e||(Array.isArray(e)||typeof e=="string")&&e.length===0){if(t?.isExample||s.isOptional||s.isInternal)return!0;let n=t?.isInputField!==!1?"input":"output";throw new Error(`Value for ${n} field '${s.name}' is required.`)}return!1};function wn(s){let e=s.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function jo(s,e){if(e.size===0)return s;let t=s,n=Array.from(e.keys()).sort((r,o)=>o.length-r.length);for(let r of n){let o=e.get(r),i=new RegExp(`\`${r}\``,"g");t=t.replace(i,`\`${o}\``);let a=new RegExp(`"${r}"`,"g");t=t.replace(a,`"${o}"`);let u=new RegExp(`'${r}'`,"g");t=t.replace(u,`'${o}'`);let l=new RegExp(`\\[${r}\\]`,"g");t=t.replace(l,`[${o}]`);let c=new RegExp(`\\(${r}\\)`,"g");t=t.replace(c,`(${o})`);let p=new RegExp(`\\$${r}\\b`,"g");t=t.replace(p,`\`${o}\``)}return t}function Wp(s,e){let t=s.history(0,e),n=t.some(o=>o.role==="function");return t.some(o=>o.role==="assistant"&&"functionCalls"in o&&Array.isArray(o.functionCalls)&&o.functionCalls.length>0)&&n}function Vp(s,e){let t=s.history(0,e),n=[],r=t.filter(i=>i.role==="assistant"&&"functionCalls"in i&&Array.isArray(i.functionCalls)&&i.functionCalls.length>0),o=t.filter(i=>i.role==="function");for(let i of r)if("functionCalls"in i&&i.functionCalls)for(let a of i.functionCalls){let u=o.find(l=>"functionId"in l&&l.functionId===a.id);u&&"result"in u&&"functionId"in u&&n.push({index:n.length,functionName:a.function.name,functionId:a.id,args:a.function.params||"",result:String(u.result),isError:"isError"in u?!!u.isError:!1})}return n}async function zo(s,e,t,n){if(!e?.resultPicker||s.length<=1)return 0;let r=e.resultPicker;if((t?Wp(t,n):!1)&&t){let u=Vp(t,n),l=await r({type:"function",results:u});if(l<0||l>=u.length)throw new Error(`Result picker returned invalid index: ${l}. Must be between 0 and ${u.length-1}`);return l}let i=s.map((u,l)=>({index:l,sample:u.delta})),a=await r({type:"fields",results:i});if(a<0||a>=s.length)throw new Error(`Result picker returned invalid index: ${a}. Must be between 0 and ${s.length-1}`);return a}async function Cl(s,e,t){let n=s?.getLast(e);if(!n||n.role!=="assistant"||n.chat.length<=1)return 0;let r=n.chat.map(i=>({version:0,index:i.index,delta:i.value}));return await zo(r,t,s,e)}var kn=class{_stepIndex=0;maxSteps;_functionsExecuted=new Set;_lastFunctionCalls=[];_usage={promptTokens:0,completionTokens:0,totalTokens:0};state=new Map;_pendingOptions={};_functionsToAdd=[];_functionsToRemove=[];_stopRequested=!1;_stopResultValues;constructor(e){this.maxSteps=e}get stepIndex(){return this._stepIndex}get isFirstStep(){return this._stepIndex===0}get functionsExecuted(){return this._functionsExecuted}get lastFunctionCalls(){return this._lastFunctionCalls}get usage(){return this._usage}setModel(e){this._pendingOptions.model=e}setThinkingBudget(e){this._pendingOptions.thinkingTokenBudget=e}setTemperature(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.temperature=e}setMaxTokens(e){this._pendingOptions.modelConfig||(this._pendingOptions.modelConfig={}),this._pendingOptions.modelConfig.maxTokens=e}setOptions(e){Object.assign(this._pendingOptions,e)}addFunctions(e){this._functionsToAdd.push(...e)}removeFunctions(...e){this._functionsToRemove.push(...e)}stop(e){this._stopRequested=!0,this._stopResultValues=e}_beginStep(e){this._stepIndex=e,this._functionsExecuted=new Set,this._lastFunctionCalls=[]}_recordFunctionCall(e,t,n){this._functionsExecuted.add(e.toLowerCase()),this._lastFunctionCalls.push({name:e,args:t,result:n})}_addUsage(e,t,n){this._usage.promptTokens+=e,this._usage.completionTokens+=t,this._usage.totalTokens+=n}_consumePendingOptions(){if(Object.keys(this._pendingOptions).length===0)return;let e=this._pendingOptions;return this._pendingOptions={},e}_consumeFunctionsToAdd(){if(this._functionsToAdd.length===0)return;let e=this._functionsToAdd;return this._functionsToAdd=[],e}_consumeFunctionsToRemove(){if(this._functionsToRemove.length===0)return;let e=this._functionsToRemove;return this._functionsToRemove=[],e}get _isStopRequested(){return this._stopRequested}get _stopValues(){return this._stopResultValues}};var qo=class{tools;logger;constructor(e,t){this.tools=new Map(e.map(n=>[n.name,n])),this.logger=t}getToolParamFieldMap(){let e=new Map;for(let[,t]of this.tools.entries())if(t.parameters?.properties&&Object.keys(t.parameters.properties).length>0){let{paramFieldMap:n}=Jp(t);e.set(t.name,n)}else e.set(t.name,new Map);return e}async route(e,t){let n=[],r={},o=new Map,i=new Map;for(let[a,u]of this.tools.entries())i.set(a,this.buildSanitizedFieldMap(u));for(let[a,u]of Object.entries(e)){let l=this.tools.get(this.normalizeToolName(a));if(l){u!=null&&typeof u=="object"&&o.set(l.name,u);continue}r[a]=u}for(let[a,u]of Object.entries(e))for(let[l,c]of this.tools.entries()){let p=i.get(l);if(!p)continue;let d=p.get(a);if(!d)continue;let m=o.get(c.name)??{};this.setNested(m,d,u),o.set(c.name,m)}for(let[a,u]of this.tools.entries()){let l=o.get(u.name);if(!(!l||Object.keys(l).length===0)){if(u.parameters&&u.parameters.type==="object"){let p=(u.parameters.required||[]).filter(d=>l[d]===void 0);if(p.length>0)throw new $e(`Missing required arguments for tool '${u.name}': ${p.join(", ")}`)}n.push({id:u.name,name:u.name,args:JSON.stringify(l)})}}return{functionCalls:n,remainingFields:r}}normalizeToolName(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}sanitizeFieldName(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}buildSanitizedFieldMap(e){let t=new Map;if(!e.parameters||!("properties"in e.parameters))return t;let n=(r,o)=>{for(let[i,a]of Object.entries(r)){let u=[...o,i];if(a&&a.type==="object"&&a.properties)n(a.properties,u);else{let l=`${e.name}.${u.join(".")}`,c=this.sanitizeFieldName(l);t.set(c,u)}}};return n(e.parameters.properties??{},[]),t}setNested(e,t,n){let r=e;for(let o=0;o<t.length-1;o++){let i=t[o],a=r[i];(typeof a!="object"||a===null)&&(r[i]={}),r=r[i]}r[t[t.length-1]]=n}isToolField(e){return this.tools.has(this.normalizeToolName(e))}getToolFieldNames(){return Array.from(this.tools.keys()).map(e=>e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,""))}};function Jp(s){let e=[],t=new Map;if(!s.parameters||!s.parameters.properties)return{fields:e,paramFieldMap:t};let n=s.parameters.properties,r=s.parameters.required||[],o=(i,a,u)=>{for(let[l,c]of Object.entries(i)){let p=a?`${a}.${l}`:l,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Qp(c);e.push({name:Xp(d),title:Yp(s.name,p),type:m,description:c.description||`${l} parameter for ${s.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Qp(s){switch(s.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=s.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function Yp(s,e){return`${s} ${e.replace(/\./g," ")}`}function Xp(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Rl(s,e,t){let n=new Re(e);if(t){let r=t.getToolParamFieldMap();for(let o of s){let i=r.get(o.name);if(i&&i.size>0)for(let a of i.values())n.getOutputFields().some(l=>l.name===a.name)||n.addOutputField(a);else{let a=Fi(o.name),u=wl(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:Tl(o.name),type:u,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}}return{signature:n,toolParamFieldMap:r}}else{let r=new Map;for(let o of s)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:i,paramFieldMap:a}=Zp(o);r.set(o.name,a);for(let u of i)n.getOutputFields().some(c=>c.name===u.name)||n.addOutputField(u)}else{let i=Fi(o.name),a=wl(o.parameters);n.getOutputFields().some(l=>l.name===i)||n.addOutputField({name:i,title:Tl(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function Zp(s){let e=[],t=new Map;if(!s.parameters||!s.parameters.properties)return{fields:e,paramFieldMap:t};let n=s.parameters.properties,r=s.parameters.required||[],o=(i,a,u)=>{for(let[l,c]of Object.entries(i)){let p=a?`${a}.${l}`:l,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=ed(c),g={name:Fi(d),title:td(s.name,p),type:m,description:c.description||`${l} parameter for ${s.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function ed(s){switch(s.type){case"string":return{name:"string",isArray:!1};case"number":case"integer":return{name:"number",isArray:!1};case"boolean":return{name:"boolean",isArray:!1};case"array":{let e=s.items;if(e?.type)switch(e.type){case"string":return{name:"string",isArray:!0};case"number":case"integer":return{name:"number",isArray:!0};case"boolean":return{name:"boolean",isArray:!0};default:return{name:"json",isArray:!0}}return{name:"json",isArray:!0}}case"object":return{name:"json",isArray:!1};default:return{name:"string",isArray:!1}}}function td(s,e){return`${s} ${e.replace(/\./g," ")}`}function Fi(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Tl(s){return s.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function wl(s){return!s||!s.properties||Object.keys(s.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var Sr=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new qo(e)}processSignature(e){let{signature:t}=Rl(this.tools,e),n=new Set(t.getOutputFields().map(o=>o.name)),r=new Set(e.getOutputFields().map(o=>o.name));return this.injectedToolFieldNames=new Set([...n].filter(o=>!r.has(o))),t}async processResults(e,t){let{functionCalls:n}=await this.router.route(e,t);return n.length>0?n:void 0}getInjectedToolFieldNames(){return Array.from(this.injectedToolFieldNames)}getRouter(){return this.router}};var Sn="__finalResult",Ie=class s extends vt{clone=()=>new s(this.signature,this.options);promptTemplate;asserts;streamingAsserts;options;functions;fieldProcessors=[];streamingFieldProcessors=[];excludeContentFromTrace=!1;thoughtFieldName;signatureToolCallingManager;structuredOutputFunctionFallback=!1;activeAbortControllers=new Set;_stopRequested=!1;constructor(e,t){super(e,{description:t?.description,traceLabel:t?.traceLabel}),this.options=t,this.thoughtFieldName=t?.thoughtFieldName??"thought";let n={functions:t?.functions,thoughtFieldName:this.thoughtFieldName};this.promptTemplate=new(t?.promptTemplate??$t)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Nt(t.functions):[],this.usage=[]}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}setInstruction(e){this.promptTemplate.setInstruction(e)}getInstruction(){return this.promptTemplate.getInstruction()}getSignatureName(){return this.signature.getDescription()||"unknown_signature"}getMetricsInstruments(){return pi()}getMergedCustomLabels(e,t){return Et(re.customLabels,e?.getOptions?.()?.customLabels,t?.customLabels)}updateMeter(e){pi(e)}createStates(e){return Array.from({length:e},(t,n)=>({index:n,functionCalls:[],values:{},content:"",functionsExecuted:new Set,xstate:{extractedFields:[],streamedIndex:{},s:-1}}))}addAssert(e,t){this.asserts.push({fn:e,message:t})}addStreamingAssert(e,t,n){let r=this.signature.getOutputFields().find(a=>a.name===e);if(!r)throw new Error(`addStreamingAssert: field ${String(e)} not found in output signature`);let o=r.type?.name;if(!(!o||o==="string"||o==="code"))throw new Error(`addStreamingAssert: field ${String(e)} must be a string field for streaming assertions`);this.streamingAsserts.push({fieldName:String(e),fn:t,message:n})}addFieldProcessorInternal(e,t,n=!1){let r=this.signature.getOutputFields().find(o=>o.name===e);if(!r)throw new Error(`addFieldProcessor: field ${e} not found`);if(n){let o=r.type?.name;if(!(!o||o==="string"||o==="code"))throw new Error(`addFieldProcessor: field ${e} must be a text field`);this.streamingFieldProcessors.push({field:r,process:t})}else this.fieldProcessors.push({field:r,process:t})}addStreamingFieldProcessor(e,t){this.addFieldProcessorInternal(String(e),t,!0)}addFieldProcessor(e,t){this.addFieldProcessorInternal(String(e),t,!1)}async forwardSendRequest({ai:e,mem:t,options:n,traceContext:r,functions:o,functionCall:i,stepIndex:a}){let{sessionId:u,model:l,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Cl(t,u,{resultPicker:n?.resultPicker}),f=t?.history(g,u)??[];if(f.length===0)throw new Error("No chat prompt found");let x={...n?.modelConfig,...n?.sampleCount?{n:n.sampleCount}:{},...n?.sampleCount&&n?.modelConfig?.temperature===1?{temperature:.8}:{}},y=this.isDebug(e,n),h=a===0,A=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let b,I=this.signature.getOutputFields();if(this.signature.hasComplexFields()&&!this.structuredOutputFunctionFallback){if(!e.getFeatures(l)?.structuredOutputs)throw new Error(`Complex structured outputs (object/array types) require a provider that supports structured outputs. Current provider/model (${l}) does not support this feature. Supported providers: OpenAI (GPT-4o, GPT-4.1+), Google Gemini, Anthropic (Sonnet/Opus).`);b={type:"json_schema",schema:{name:"output",strict:!0,schema:St(I)}}}let R=n?.contextCache?.cacheBreakpoint??"after-examples",k=e.getFeatures?.(l)?.caching?.cacheBreakpoints===!1,v=n?.contextCache&&(k||R==="after-functions"||R==="after-examples"),O=o?.length&&v?o.map(($,E)=>({...$,cache:E===o.length-1})):o;return await e.chat({chatPrompt:f,functions:O,functionCall:i,modelConfig:x,model:l,responseFormat:b},{sessionId:u,rateLimiter:c,stream:p,debug:y,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!h,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??re.abortSignal,stepIndex:a,logger:A,functionCallMode:n?.functionCallMode??this.options?.functionCallMode??"auto",retry:n?.retry??this.options?.retry,customLabels:n?.customLabels})}async*forwardCore({ai:e,mem:t,options:n,stepIndex:r,span:o,traceContext:i,states:a,stopFunctionNames:u,stepContext:l}){let{sessionId:c,functions:p}=n??{},d=n?.functionResultFormatter??this.options?.functionResultFormatter,m=n?.functionCall??this.options?.functionCall,g=this.signatureToolCallingManager,f=n?.strictMode??!1,x=n.model,y=this.usage,h=r===0,A=this.isDebug(e,n),b=this.getLogger(e,n),{functions:I,functionCall:T}=sl(p,m,h,n);this.structuredOutputFunctionFallback&&I.filter(v=>v.name!==Sn).length===0&&(T={type:"function",function:{name:Sn}});let R=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:i,functions:I,functionCall:T,stepIndex:r});R instanceof ReadableStream?yield*al({ai:e,model:x,res:R,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:I,strictMode:f,span:o,states:a,usage:y,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:b,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:u,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:l,abortSignal:n.abortSignal}):yield*ul({ai:e,model:x,res:R,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:I,span:o,strictMode:f,states:a,usage:y,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:b,debug:A,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:u,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:l,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,i){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,u=Array.isArray(a)?a.map(se=>se.toLowerCase()):a?[a.toLowerCase()]:void 0,l=r.maxRetries??this.options?.maxRetries??3,c=r.maxSteps??this.options?.maxSteps??25,p=r.mem??this.options?.mem??new xn,d=r.functions?Nt(r.functions):[...this.functions],m=new kn(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let G=e.getModelList()?.filter(Z=>"model"in Z);if(!G||G.length<2)throw new Error("Self-tuning with model selection requires the AI service to have a `models` list with at least 2 chat models. Either configure models on your AI service or disable model selection with `selfTuning: { model: false }`.")}let se=Ri(e,g,r.model?String(r.model):void 0);d.push(se)}let f={...r},x=r.stepHooks,y=d&&d.length>0,h=r.functionCallMode??this.options?.functionCallMode??"auto";y&&h==="prompt"&&(this.signatureToolCallingManager=new Sr(d)),y&&h==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new Sr(d));let A,b,I=this.options?.promptTemplate??$t;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let T=this.signature.hasComplexFields(),R=e.getFeatures?.(r.model),k=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=T&&(k==="function"||k==="auto"&&!R?.structuredOutputs),this.structuredOutputFunctionFallback){let se={name:Sn,description:"Return the final result. Call this function with the complete output data.",parameters:St(this.signature.getOutputFields()),func:async()=>"done"};d.push(se),u=[...u??[],Sn.toLowerCase()]}let v=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,O={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:v,structuredOutputFunctionName:this.structuredOutputFunctionFallback?Sn:void 0};this.promptTemplate=new I(this.signature,O);let P,$=performance.now();Array.isArray(t)?(Ya(t),P=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):P=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let E=performance.now()-$,F=this.getMetricsInstruments(),_=this.getMergedCustomLabels(e,r);F&&Eo(F,"prompt_render",E,this.getSignatureName(),_);let j=performance.now();p.addRequest(P,r.sessionId);let H=performance.now()-j;F&&Eo(F,"memory_update",H,this.getSignatureName(),_);let L=new Map;n.forEach(se=>{L.set(se.index,{})});let X=()=>{let se=m._consumePendingOptions();if(se){let{modelConfig:Z,...K}=se;f={...f,...K},Z&&(f.modelConfig={...f.modelConfig,...Z})}let be=m._consumeFunctionsToAdd();if(be){let Z=Nt(be);for(let K of Z)d.some(Y=>Y.name===K.name)||d.push(K)}let G=m._consumeFunctionsToRemove();if(G){let Z=new Set(G.map(K=>K.toLowerCase()));for(let K=d.length-1;K>=0;K--)Z.has(d[K].name.toLowerCase())&&d.splice(K,1)}},U=r?.abortSignal??re.abortSignal;e:for(let se=0;se<c;se++){if(m._beginStep(se),X(),g&&g.model!==!1){let G=d.findIndex(Z=>Z.name==="adjustGeneration");if(G!==-1){let Z=f.model?String(f.model):void 0;d[G]=Ri(e,g,Z)}}if(m._isStopRequested)break;if(U?.aborted)throw new ke("between-steps",U.reason??"Aborted between steps");if(x?.beforeStep&&(await x.beforeStep(m),X(),m._isStopRequested))break;let be=l;for(let G=0;G<=be;G++)try{let Z=l+1;for(let Y=0;Y<Z;Y++){n.forEach(V=>{V.content="",V.values={},V.functionCalls=[],V.functionsExecuted=new Set,V.xstate={extractedFields:[],streamedIndex:{},s:-1}}),Y>0&&L.forEach((V,le)=>{L.set(le,{})});let me=new Map;n.forEach(V=>{me.set(V.index,{})});try{let V=this.forwardCore({options:{...f,functions:d},ai:e,mem:p,stepIndex:se,span:o,traceContext:i,states:n,stopFunctionNames:u,stepContext:m}),le=!1;try{for await(let ne of V)if(ne!==void 0){let he=ne.index,C=ne.delta,S=me.get(he)??{},M=L.get(he)??{},w={},B=!1;for(let N of Object.keys(C)){let D=C[N],q=S[N],te;typeof D=="string"&&(typeof q=="string"||q===void 0)?te=(q??"")+D:Array.isArray(D)&&(Array.isArray(q)||q===void 0)?te=[...q??[],...D]:te=D,S[N]=te;let ce=te,W=M[N];if(typeof ce=="string"&&typeof W=="string")if(ce.startsWith(W)){let ee=ce.slice(W.length);ee&&(w[N]=ee,B=!0,M[N]=ce)}else W.startsWith(ce)||ce!==W&&(w[N]=ce,B=!0,M[N]=ce);else if(Array.isArray(ce)&&Array.isArray(W)){if(ce.length>W.length){let ee=ce.slice(W.length);w[N]=ee,B=!0,M[N]=ce}}else JSON.stringify(ce)!==JSON.stringify(W)&&(w[N]=ce,B=!0,M[N]=ce)}B&&(yield{version:Y,index:ne.index,delta:w})}}catch(ne){if(ne instanceof _t){if(le=!0,this.structuredOutputFunctionFallback){let he=ne.calls.find(C=>C.func.name===Sn);if(he?.args){let C=he.args;In(this.signature,C);let S=this.signature.getOutputFields();for(let M of n){let w={};for(let B of S)B.name in C&&!B.isInternal&&(w[B.name]=C[B.name],M.values[B.name]=C[B.name]);yield{version:Y,index:M.index,delta:w}}for(let M of n)await An(this.asserts,M.values);if(this.fieldProcessors.length>0)for(let M of n)await Cr(this.fieldProcessors,M.values,p,r.sessionId)}}}else throw ne}if(this.usage.length>0){let ne=this.usage[this.usage.length-1];ne?.tokens&&m._addUsage(ne.tokens.promptTokens??0,ne.tokens.completionTokens??0,ne.tokens.totalTokens??0)}n.some(ne=>ne.functionsExecuted.size>0)&&x?.afterFunctionExecution&&(await x.afterFunctionExecution(m),X());let ie=le||m._isStopRequested?!1:ll(p,u,n,f?.sessionId);if(x?.afterStep&&(await x.afterStep(m),X()),ie&&!m._isStopRequested&&!U?.aborted){let ne=this.getMetricsInstruments();ne&&Oo(ne,se+1,c,this.getSignatureName(),_);continue e}if(U?.aborted)throw new ke("mid-step",U.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let pe=this.getMetricsInstruments();if(pe){Oo(pe,se+1,c,this.getSignatureName(),_);let ne=new Set;n.forEach(he=>{he.functionsExecuted.forEach(C=>ne.add(C))}),ne.size>0&&Pu(pe,!0,ne.size,!0,!1,this.getSignatureName(),_),Fu(pe,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),_)}return}catch(V){if(V instanceof ke)throw V;b=V;let le,Te=this.isDebug(e,r),ie=this.getLogger(e,r),pe=this.getMetricsInstruments(),ne=this.getSignatureName(),he={error:V,errCount:Y,logger:ie,metricsInstruments:pe,signatureName:ne,span:o,debug:Te,customLabels:_};if(o?.recordException(V),V instanceof $e)le=zu(he),A=V;else if(V instanceof ct)le=qu(he),A=V;else if(V instanceof Ce)Hu(he);else{if(V instanceof Ve)throw V;{let C=V,S=C instanceof je&&C.status>=500&&C.status<600,M=C instanceof Le,w=C instanceof Ke;throw S||M||w?V:_i(V,e,this.signature)}}if(le&&(p.addTag("error",r.sessionId),p.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(le)}],r.sessionId),p.addTag("correction",r.sessionId),this.signature.hasComplexFields()))for(let S of n)S.content="",S.values={},S.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let K=this.getMetricsInstruments();throw K&&mi(K,l,!1,l,this.getSignatureName(),_),_i(new Error(`Unable to fix validation error: ${(A??b)?.message??(A??b)?.toString()??"unknown error"}
398
405
 
399
406
  LLM Output:
400
- ${n.map(we=>we.content).join(`
407
+ ${n.map(Y=>Y.content).join(`
401
408
  ---
402
- `)}`),e,this.signature)}catch(re){let le=re,we=le instanceof tt&&le.status>=500&&le.status<600,Ue=le instanceof He,se=le instanceof st,ve=le instanceof lt;if((we||Ue||se||ve)&&oe<ye){let ce=this.isDebug(e,r),be=this.getLogger(e,r),b=Math.min(6e4,1e3*Math.pow(2,oe));ce&&be&&be({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${oe+1}/${ye+1}): ${le.message}. Retrying in ${b}ms...`}),o?.addEvent("infrastructure.retry",{attempt:oe+1,maxRetries:ye,delay:b,errorType:le instanceof tt?"status_error":le instanceof He?"network_error":le instanceof st?"timeout_error":"stream_terminated",errorMessage:le.message}),await new Promise((S,R)=>{let y=!1,_,q=()=>{N&&_&&N.removeEventListener("abort",_)},G=setTimeout(()=>{y||(y=!0,q(),S())},b);if(N){if(_=()=>{y||(y=!0,clearTimeout(G),q(),R(new Le("infrastructure-retry-backoff",N.reason?String(N.reason):"Aborted during retry backoff")))},N.aborted){_();return}N.addEventListener("abort",_,{once:!0})}});continue}throw re}}throw j&&Po(j,c,c,this.getSignatureName(),F),Fi(new Error(`Max steps reached: ${c}`),e,this.signature)}validateInputs(e){let t=this.signature.getInputFields();for(let n of t){if(n.isInternal)continue;let r=e[n.name];if(n.isOptional&&r===void 0)continue;let o=n.type;if(o&&(o.name==="url"&&Lt(r,n),o.name,o.name,(o.name==="string"||o.name==="code")&&pt(r,n),o.name==="number"&&dt(r,n),o.name==="object"&&o.fields&&typeof r=="object"&&r!==null&&this.validateObjectFields(r,o.fields,n.name),o.isArray&&Array.isArray(r)))for(let s=0;s<r.length;s++){let a=r[s];o.name==="string"||o.name==="code"?pt(a,n):o.name==="number"?dt(a,n):o.fields&&typeof a=="object"&&a!==null&&this.validateObjectFields(a,o.fields,`${n.name}[${s}]`)}}}validateObjectFields(e,t,n){for(let[r,o]of Object.entries(t)){let s=e[r];if(o.isOptional&&s===void 0)continue;let a={name:`${n}.${r}`,type:{name:o.type,isArray:o.isArray,options:o.options?[...o.options]:void 0,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,format:o.format},description:o.description,isOptional:o.isOptional};if(o.type==="string"||o.type==="code"?pt(s,a):o.type==="number"?dt(s,a):o.type==="object"&&o.fields&&typeof s=="object"&&s!==null&&this.validateObjectFields(s,o.fields,a.name),o.isArray&&Array.isArray(s))for(let l=0;l<s.length;l++){let u=s[l];o.type==="string"||o.type==="code"?pt(u,a):o.type==="number"?dt(u,a):o.fields&&typeof u=="object"&&u!==null&&this.validateObjectFields(u,o.fields,`${a.name}[${l}]`)}}}async*_forward1(e,t,n){(!Array.isArray(t)||!t.every(a=>"role"in a))&&this.validateInputs(t);let r=new AbortController;this.activeAbortControllers.add(r),this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=Ft(r.signal,n?.abortSignal??me.abortSignal),s=o?{...n,abortSignal:o}:n;try{let a=performance.now(),l=this.createStates(n.sampleCount??1),u=performance.now()-a,c=this.getMetricsInstruments(),p=this.getMergedCustomLabels(e,n);c&&Fo(c,"state_creation",u,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Kt(n.functions,this.functions)),!d){yield*this._forward2(e,t,l,{...s,functions:m});return}let g=m?.map(A=>A.name).join(","),h={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...g?{provided_functions:g}:{},...n?.model?{model:n.model}:{},...n?.thinkingTokenBudget?{thinking_token_budget:n.thinkingTokenBudget}:{},...n?.showThoughts?{show_thoughts:n.showThoughts}:{},...n?.maxSteps?{max_steps:n.maxSteps}:{},...n?.maxRetries?{max_retries:n.maxRetries}:{}},f=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,C=f?`AxGen > ${f}`:"AxGen",x=d.startSpan(C,{kind:Un.SpanKind.SERVER,attributes:h}),I=Un.context.active(),w=Un.trace.setSpan(I,x);try{if(this.excludeContentFromTrace||x.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,l,{...s,functions:m},x,w),!this.excludeContentFromTrace){let A=l.map(k=>k.values),T=A.length===1?A[0]:A;x.addEvent("output",{content:JSON.stringify(T,null,2)})}}finally{x.end()}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async forward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??me.cachingFunction,o=(()=>{if(!r)return;let d=this.signature.getInputFields().map(m=>m.name);return this.computeCacheKey(t,d)})();if(r&&o){let d=await r(o);if(d!==void 0)return d}let s=performance.now(),a=this.getSignatureName(),l=n?.stream??!1,u=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&Rl(d,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,a,m);let g=this._forward1(e,t,n??{}),h=[],f=0,C=0;for await(let E of g)E.version!==f&&(h=[]),f=E.version,h=xi(h,E),C++;c=f;let x=performance.now();p=!!n?.resultPicker;let I=await Ho(h,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),w=performance.now()-x,T=h[I]?.delta??{},k=Array.isArray(t)?{}:t??{};if(this.trace={...k,...T},p&&this.isDebug(e,n)){let E=this.getLogger(e,n);Ia(h.length,I,w,E)}if(u=!0,d&&(wl(d,h.length,p,p?w:void 0,a,m),Tl(d,l,C,void 0,a,m)),r&&o)try{await r(o,T)}catch{}return T}catch(d){throw u=!1,d}finally{let d=performance.now()-s,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&(yl(m,d,u,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&mi(m,c,u,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??me.cachingFunction,o=(()=>{if(!r)return;let p=this.signature.getInputFields().map(d=>d.name);return this.computeCacheKey(t,p)})();if(r&&o){let p;try{p=await r(o)}catch{}if(p!==void 0){yield{version:0,index:0,delta:p};return}}if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let s=this._forward1(e,t,{...n,stream:!0}),a=[],l=0;for await(let p of s)p.version!==l&&(a=[]),l=p.version,a=xi(a,p);let u=await Ho(a,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),c=a[u];if(c){if(r&&o)try{await r(o,c.delta)}catch{}yield{version:l,index:u,delta:c.delta}}}setExamples(e,t){super.setExamples(e,t)}isDebug(e,t){return t?.debug??this.options?.debug??me.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??me.logger??e.getLogger()}computeCacheKey(e,t){let n=ot("sha256");n.update(this.signature.hash()??"");let r=o=>{let s=typeof o;if(n.update(`|${s}|`),o==null){n.update("null");return}if(s==="string"||s==="number"||s==="boolean"){n.update(String(o));return}if(Array.isArray(o)){n.update("[");for(let a of o)r(a);n.update("]");return}if(typeof o=="object"&&o!==null&&"mimeType"in o&&"data"in o){let a=o;n.update(a.mimeType??"");let l=ot("sha256").update(a.data??"").digest("hex");n.update(l);return}if(typeof o=="object"){let a=o,l=Object.keys(a).sort();for(let u of l)n.update(`{${u}}`),r(a[u]);return}n.update(String(o))};if(Array.isArray(e))for(let o of e){n.update(`role:${o.role}`);let s=t.map(a=>o.values?.[a]);for(let a of s)r(a)}else{let o=t.map(s=>e?.[s]);for(let s of o)r(s)}return n.digest("hex")}},Er=class extends Error{details;constructor(e,t,n){super(e),this.name="AxGenerateError",this.details=t,n?.cause&&(this.cause=n.cause)}toJSON(){let e=this.cause;return{name:this.name,message:this.message,details:this.details,cause:e?{name:e.name,message:e.message,stack:e.stack}:void 0,stack:this.stack}}};function Fi(i,e,t){let n=i instanceof Error?i:new Error(String(i));if(n instanceof Le)return n;let r=(n.message||"").toLowerCase();if(r.includes("at least")||r.includes("at most")||r.includes("must match pattern")||r.includes("invalid url")||r.includes("required")||r.includes("missing")||r.includes("valid email")||r.includes("number must be")||n.name==="ValidationError"||n.name==="AssertionError"||n.name==="AxAssertionError"||n.stack?.includes("asserts.ts"))return n;let s=e.getLastUsedChatModel(),a=e.getLastUsedModelConfig(),l={model:s,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Er(`Generate failed: ${n.message}`,l,{cause:n})}var bp=i=>i.replace(/^\W+|\W+$/g,""),Ip=(i,e)=>{let t=i.search(e);if(t===-1)return[i];let n=i.match(e);if(!n)throw new Error("Match failed unexpectedly.");let r=i.substring(0,t),o=i.substring(t+n[0].length);return[r,o]},Cp=i=>{let e=new Set,t=[];for(let n of i)e.has(n)||(e.add(n),t.push(n));return t},Tp=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);if(!e||e.length<3)throw new Error('line must start with a number, a dot and then text. e.g. "1. hello"');let t=Number.parseInt(e[1],10),n=e[2].trim();return{id:t,text:n}},wp=i=>{let e=i.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():i},Rp=(i,e)=>{let t=[];for(let n=0;n<i.length;n+=e)t.push(i.slice(n,n+e));return t},Wo={trimNonAlphaNum:bp,splitIntoTwo:Ip,dedup:Cp,extractIdAndText:Tp,extractIndexPrefixedText:wp,batchArray:Rp};var Vo=class extends Ee{constructor(e){super(`"You are a re-ranker assistant tasked with evaluating a set of content items in relation to a specific question. Your role involves critically analyzing each content item to determine its relevance to the question and re-ranking them accordingly. This process includes assigning a relevance score from 0 to 10 to each content item based on how well it answers the question, its coverage of the topic, and the reliability of its information. This re-ranked list should start with the content item that is most relevant to the question and end with the least relevant. Output only the list."
403
- query: string, items: string[] -> rankedItems: string[] "list of id, 5-words Rationale, relevance score"`,e)}forward=async(e,t,n)=>{let{rankedItems:r}=await super.forward(e,t,n),o=r.map(a=>{let{id:l}=Wo.extractIdAndText(a);return l});return{rankedItems:t.items.map((a,l)=>{let u=o[l];return u!==void 0?t.items[u]:void 0}).filter(a=>a!==void 0)}}};var Jo=class{tikaUrl;fetch;constructor(e){let t=e??{url:"http://localhost:9998/"};this.tikaUrl=new URL("/tika",t.url),this.fetch=t.fetch}async _convert(e,t){if(!e)throw new Error("Failed to read file data");let n=t?.format==="html"?"text/html":"text/plain";try{let r={body:e,headers:{Accept:n},method:"PUT"};typeof window>"u"&&typeof process<"u"&&(r.duplex="half");let o=await(this.fetch??fetch)(this.tikaUrl,r);if(!o.ok)throw new Error(`Failed to upload file: ${o.statusText}`);return await o.text()}catch(r){throw new Error(`Error converting file: ${r}`)}}async convert(e,t){let n=[],r=t?.batchSize??10;for(let o=0;o<e.length;o+=r){let a=e.slice(o,o+r).map(u=>this._convert(u,{format:t?.format})),l=await Promise.all(a);n.push(...l)}return n}};var CA=new nt,Qo=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},Yo=class{ai;db;debug;constructor(e){this.db=new Nt,this.ai=e}getState(){return this.db.getDB()}setState(e){this.db.setDB(e)}setClasses=async(e,t)=>{for(let n of e){let r=await this.ai.embed({texts:n.getContext()},{abortSignal:t?.abortSignal});await this.db.upsert({id:n.getName(),table:"classes",values:r.embeddings[0]})}};async forward(e,t){let{embeddings:n}=await this.ai.embed({texts:[e]},{abortSignal:t?.abortSignal}),o=(await this.db.query({table:"classes",values:n[0]})).matches;if(typeof t?.cutoff=="number"){let{cutoff:a}=t;o=o.filter(l=>l.score<=a)}let s=o.at(0);return s?s.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var mu=new Set(["0o","0s","3a","3b","3d","6b","6o","a","a1","a2","a3","a4","ab","able","about","above","abst","ac","accordance","according","accordingly","across","act","actually","ad","added","adj","ae","af","affected","affecting","affects","after","afterwards","ag","again","against","ah","ain","ain't","aj","al","all","allow","allows","almost","alone","along","already","also","although","always","am","among","amongst","amoungst","amount","an","and","announce","another","any","anybody","anyhow","anymore","anyone","anything","anyway","anyways","anywhere","ao","ap","apart","apparently","appear","appreciate","appropriate","approximately","ar","are","aren","arent","aren't","arise","around","as","a's","aside","ask","asking","associated","at","au","auth","av","available","aw","away","awfully","ax","ay","az","b","b1","b2","b3","ba","back","bc","bd","be","became","because","become","becomes","becoming","been","before","beforehand","begin","beginning","beginnings","begins","behind","being","believe","below","beside","besides","best","better","between","beyond","bi","bill","biol","bj","bk","bl","bn","both","bottom","bp","br","brief","briefly","bs","bt","bu","but","bx","by","c","c1","c2","c3","ca","call","came","can","cannot","cant","can't","cause","causes","cc","cd","ce","certain","certainly","cf","cg","ch","changes","ci","cit","cj","cl","clearly","cm","c'mon","cn","co","com","come","comes","con","concerning","consequently","consider","considering","contain","containing","contains","corresponding","could","couldn","couldnt","couldn't","course","cp","cq","cr","cry","cs","c's","ct","cu","currently","cv","cx","cy","cz","d","d2","da","date","dc","dd","de","definitely","describe","described","despite","detail","df","di","did","didn","didn't","different","dj","dk","dl","do","does","doesn","doesn't","doing","don","done","don't","down","downwards","dp","dr","ds","dt","du","due","during","dx","dy","e","e2","e3","ea","each","ec","ed","edu","ee","ef","effect","eg","ei","eight","eighty","either","ej","el","eleven","else","elsewhere","em","empty","en","end","ending","enough","entirely","eo","ep","eq","er","es","especially","est","et","et-al","etc","eu","ev","even","ever","every","everybody","everyone","everything","everywhere","ex","exactly","example","except","ey","f","f2","fa","far","fc","few","ff","fi","fifteen","fifth","fify","fill","find","fire","first","five","fix","fj","fl","fn","fo","followed","following","follows","for","former","formerly","forth","forty","found","four","fr","from","front","ft","fu","full","further","furthermore","fy","g","ga","gave","ge","get","gets","getting","gi","give","given","gives","giving","gj","gl","go","goes","going","gone","got","gotten","gr","greetings","gs","gy","h","h2","h3","had","hadn","hadn't","happens","hardly","has","hasn","hasnt","hasn't","have","haven","haven't","having","he","hed","he'd","he'll","hello","help","hence","her","here","hereafter","hereby","herein","heres","here's","hereupon","hers","herself","hes","he's","hh","hi","hid","him","himself","his","hither","hj","ho","home","hopefully","how","howbeit","however","how's","hr","hs","http","hu","hundred","hy","i","i2","i3","i4","i6","i7","i8","ia","ib","ibid","ic","id","i'd","ie","if","ig","ignored","ih","ii","ij","il","i'll","im","i'm","immediate","immediately","importance","important","in","inasmuch","inc","indeed","index","indicate","indicated","indicates","information","inner","insofar","instead","interest","into","invention","inward","io","ip","iq","ir","is","isn","isn't","it","itd","it'd","it'll","its","it's","itself","iv","i've","ix","iy","iz","j","jj","jr","js","jt","ju","just","k","ke","keep","keeps","kept","kg","kj","km","know","known","knows","ko","l","l2","la","largely","last","lately","later","latter","latterly","lb","lc","le","least","les","less","lest","let","lets","let's","lf","like","liked","likely","line","little","lj","ll","ll","ln","lo","look","looking","looks","los","lr","ls","lt","ltd","m","m2","ma","made","mainly","make","makes","many","may","maybe","me","mean","means","meantime","meanwhile","merely","mg","might","mightn","mightn't","mill","million","mine","miss","ml","mn","mo","more","moreover","most","mostly","move","mr","mrs","ms","mt","mu","much","mug","must","mustn","mustn't","my","myself","model","n","n2","na","name","namely","nay","nc","nd","ne","near","nearly","necessarily","necessary","need","needn","needn't","needs","neither","never","nevertheless","new","next","ng","ni","nine","ninety","nj","nl","nn","no","nobody","non","none","nonetheless","noone","nor","normally","nos","not","noted","nothing","novel","now","nowhere","nr","ns","nt","ny","o","oa","ob","obtain","obtained","obviously","oc","od","of","off","often","og","oh","oi","oj","ok","okay","ol","old","om","omitted","on","once","one","ones","only","onto","oo","op","oq","or","ord","os","ot","other","others","otherwise","ou","ought","our","ours","ourselves","out","outside","over","overall","ow","owing","own","ox","oz","p","p1","p2","p3","page","pagecount","pages","par","part","particular","particularly","pas","past","pc","pd","pe","per","perhaps","pf","ph","pi","pj","pk","pl","placed","please","plus","pm","pn","po","poorly","possible","possibly","potentially","pp","pq","pr","predominantly","present","presumably","previously","primarily","probably","promptly","proud","provides","ps","pt","pu","put","py","q","qj","qu","que","quickly","quite","qv","r","r2","ra","ran","rather","rc","rd","re","readily","really","reasonably","recent","recently","ref","refs","regarding","regardless","regards","related","relatively","research","research-articl","respectively","resulted","resulting","results","rf","rh","ri","right","rj","rl","rm","rn","ro","rq","rr","rs","rt","ru","run","rv","ry","s","s2","sa","said","same","saw","say","saying","says","sc","sd","se","sec","second","secondly","section","see","seeing","seem","seemed","seeming","seems","seen","self","selves","sensible","sent","serious","seriously","seven","several","sf","shall","shan","shan't","she","shed","she'd","she'll","shes","she's","should","shouldn","shouldn't","should've","show","showed","shown","showns","shows","si","side","significant","significantly","similar","similarly","since","sincere","six","sixty","sj","sl","slightly","sm","sn","so","some","somebody","somehow","someone","somethan","something","sometime","sometimes","somewhat","somewhere","soon","sorry","sp","specifically","specified","specify","specifying","sq","sr","ss","st","still","stop","strongly","sub","substantially","successfully","such","sufficiently","suggest","sup","sure","sy","system","sz","t","t1","t2","t3","take","taken","taking","tb","tc","td","te","tell","ten","tends","tf","th","than","thank","thanks","thanx","that","that'll","thats","that's","that've","the","their","theirs","them","themselves","then","thence","there","thereafter","thereby","thered","therefore","therein","there'll","thereof","therere","theres","there's","thereto","thereupon","there've","these","they","theyd","they'd","they'll","theyre","they're","they've","thickv","thin","think","third","this","thorough","thoroughly","those","thou","though","thoughh","thousand","three","throug","through","throughout","thru","thus","ti","til","tip","tj","tl","tm","tn","to","together","too","took","top","toward","towards","tp","tq","tr","tried","tries","truly","try","trying","ts","t's","tt","tv","twelve","twenty","twice","two","tx","u","u201d","ue","ui","uj","uk","um","un","under","unfortunately","unless","unlike","unlikely","until","unto","uo","up","upon","ups","ur","us","use","used","useful","usefully","usefulness","uses","using","usually","ut","v","va","value","various","vd","ve","ve","very","via","viz","vj","vo","vol","vols","volumtype","vq","vs","vt","vu","w","wa","want","wants","was","wasn","wasnt","wasn't","way","we","wed","we'd","welcome","well","we'll","well-b","went","were","we're","weren","werent","weren't","we've","what","whatever","what'll","whats","what's","when","whence","whenever","when's","where","whereafter","whereas","whereby","wherein","wheres","where's","whereupon","wherever","whether","which","while","whim","whither","who","whod","whoever","whole","who'll","whom","whomever","whos","who's","whose","why","why's","wi","widely","will","willing","wish","with","within","without","wo","won","wonder","wont","won't","words","world","would","wouldn","wouldnt","wouldn't","www","x","x1","x2","x3","xf","xi","xj","xk","xl","xn","xo","xs","xt","xv","xx","y","y2","yes","yet","yj","yl","you","youd","you'd","you'll","your","youre","you're","yours","yourself","yourselves","you've","yr","ys","yt","z","zero","zi","zz","task"]);function gu(i,e){return i.filter(t=>!e.has(t))}function hu(i){let e={};for(let t of i)e[t]=(e[t]||0)+1;return e}function Jt(i){let e=i.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function Sp(i,e){return Jt(i)===Jt(e)?1:0}function kp(i,e){let t=Jt(i).split(" "),n=Jt(e).split(" "),r=hu(t),o=hu(n),s=0;for(let u in r){let c=r[u]??0,p=o[u]??0;s+=Math.min(c,p)}if(s===0)return 0;let a=s/t.length,l=s/n.length;return 2*a*l/(a+l)}function vp(i,e,t,n=!1){let r=Jt(i).split(" "),o=Jt(e).split(" "),s=Jt(t).split(" "),a=new Set([...mu,...r]);o=gu(o,a),s=gu(s,a);let l=0,u=l/o.length,c=l/s.length,p=2*u*c/(u+c);return n?c:p}var fu={emScore:Sp,f1Score:kp,novelF1ScoreOptimized:vp};var Xo=class{ai;program;examples;constructor({ai:e,program:t,examples:n=[]}){if(n.length===0)throw new Error("No examples found");this.ai=e,this.program=t,this.examples=n}async run(e){let t=Date.now(),n=this.examples.length,r=0;for(let s=0;s<n;s++){let a=this.examples[s];if(!a)throw new Error("Invalid example");try{let l=await this.program.forward(this.ai,a,{maxRetries:1}),u=await e({prediction:l,example:a});r+=u}catch(l){console.warn(`Program evaluation failed for example ${s}: ${l instanceof Error?l.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
409
+ `)}`),e,this.signature)}catch(Z){let K=Z,Y=K instanceof je&&K.status>=500&&K.status<600,me=K instanceof Le,V=K instanceof Ke,le=K instanceof Ve;if((Y||me||V||le)&&G<be){let ie=this.isDebug(e,r),pe=this.getLogger(e,r),C=Math.min(6e4,1e3*Math.pow(2,G));ie&&pe&&pe({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${G+1}/${be+1}): ${K.message}. Retrying in ${C}ms...`}),o?.addEvent("infrastructure.retry",{attempt:G+1,maxRetries:be,delay:C,errorType:K instanceof je?"status_error":K instanceof Le?"network_error":K instanceof Ke?"timeout_error":"stream_terminated",errorMessage:K.message}),await new Promise((S,M)=>{let w=!1,B,N=()=>{U&&B&&U.removeEventListener("abort",B)},q=setTimeout(()=>{w||(w=!0,N(),S())},C);if(U){if(B=()=>{w||(w=!0,clearTimeout(q),N(),M(new ke("infrastructure-retry-backoff",U.reason?String(U.reason):"Aborted during retry backoff")))},U.aborted){B();return}U.addEventListener("abort",B,{once:!0})}});continue}throw Z}}throw F&&Oo(F,c,c,this.getSignatureName(),_),_i(new Error(`Max steps reached: ${c}`),e,this.signature)}validateInputs(e){let t=this.signature.getInputFields();for(let n of t){if(n.isInternal)continue;let r=e[n.name];if(n.isOptional&&r===void 0)continue;let o=n.type;if(o&&(o.name==="url"&&kt(r,n),o.name,o.name,(o.name==="string"||o.name==="code")&&Ye(r,n),o.name==="number"&&Xe(r,n),o.name==="object"&&o.fields&&typeof r=="object"&&r!==null&&this.validateObjectFields(r,o.fields,n.name),o.isArray&&Array.isArray(r)))for(let i=0;i<r.length;i++){let a=r[i];o.name==="string"||o.name==="code"?Ye(a,n):o.name==="number"?Xe(a,n):o.fields&&typeof a=="object"&&a!==null&&this.validateObjectFields(a,o.fields,`${n.name}[${i}]`)}}}validateObjectFields(e,t,n){for(let[r,o]of Object.entries(t)){let i=e[r];if(o.isOptional&&i===void 0)continue;let a={name:`${n}.${r}`,type:{name:o.type,isArray:o.isArray,options:o.options?[...o.options]:void 0,fields:o.fields,minLength:o.minLength,maxLength:o.maxLength,minimum:o.minimum,maximum:o.maximum,pattern:o.pattern,format:o.format},description:o.description,isOptional:o.isOptional};if(o.type==="string"||o.type==="code"?Ye(i,a):o.type==="number"?Xe(i,a):o.type==="object"&&o.fields&&typeof i=="object"&&i!==null&&this.validateObjectFields(i,o.fields,a.name),o.isArray&&Array.isArray(i))for(let u=0;u<i.length;u++){let l=i[u];o.type==="string"||o.type==="code"?Ye(l,a):o.type==="number"?Xe(l,a):o.fields&&typeof l=="object"&&l!==null&&this.validateObjectFields(l,o.fields,`${a.name}[${u}]`)}}}async*_forward1(e,t,n){(!Array.isArray(t)||!t.every(a=>"role"in a))&&this.validateInputs(t);let r=new AbortController;this.activeAbortControllers.add(r),this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=At(r.signal,n?.abortSignal??re.abortSignal),i=o?{...n,abortSignal:o}:n;try{let a=performance.now(),u=this.createStates(n.sampleCount??1),l=performance.now()-a,c=this.getMetricsInstruments(),p=this.getMergedCustomLabels(e,n);c&&Eo(c,"state_creation",l,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Nt(n.functions,this.functions)),!d){yield*this._forward2(e,t,u,{...i,functions:m});return}let g=m?.map(I=>I.name).join(","),f={signature:JSON.stringify(this.signature.toJSON(),null,2),...this.examples?{examples:JSON.stringify(this.examples,null,2)}:{},...g?{provided_functions:g}:{},...n?.model?{model:n.model}:{},...n?.thinkingTokenBudget?{thinking_token_budget:n.thinkingTokenBudget}:{},...n?.showThoughts?{show_thoughts:n.showThoughts}:{},...n?.maxSteps?{max_steps:n.maxSteps}:{},...n?.maxRetries?{max_retries:n.maxRetries}:{}},x=this.traceLabel&&n.traceLabel?`${this.traceLabel} > ${n.traceLabel}`:n.traceLabel??this.traceLabel,y=x?`AxGen > ${x}`:"AxGen",h=d.startSpan(y,{kind:vn.SpanKind.SERVER,attributes:f}),A=vn.context.active(),b=vn.trace.setSpan(A,h);try{if(this.excludeContentFromTrace||h.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,u,{...i,functions:m},h,b),!this.excludeContentFromTrace){let I=u.map(R=>R.values),T=I.length===1?I[0]:I;h.addEvent("output",{content:JSON.stringify(T,null,2)})}}finally{h.end()}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async forward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??re.cachingFunction,o=(()=>{if(!r)return;let d=this.signature.getInputFields().map(m=>m.name);return this.computeCacheKey(t,d)})();if(r&&o){let d=await r(o);if(d!==void 0)return d}let i=performance.now(),a=this.getSignatureName(),u=n?.stream??!1,l=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&Lu(d,this.signature.getInputFields().length,this.signature.getOutputFields().length,this.examples?.length??0,this.demos?.length??0,a,m);let g=this._forward1(e,t,n??{}),f=[],x=0,y=0;for await(let k of g)k.version!==x&&(f=[]),x=k.version,f=xi(f,k),y++;c=x;let h=performance.now();p=!!n?.resultPicker;let A=await zo(f,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),b=performance.now()-h,T=f[A]?.delta??{},R=Array.isArray(t)?{}:t??{};if(this.trace={...R,...T},p&&this.isDebug(e,n)){let k=this.getLogger(e,n);Pa(f.length,A,b,k)}if(l=!0,d&&(Nu(d,f.length,p,p?b:void 0,a,m),_u(d,u,y,void 0,a,m)),r&&o)try{await r(o,T)}catch{}return T}catch(d){throw l=!1,d}finally{let d=performance.now()-i,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&(Eu(m,d,l,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&mi(m,c,l,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??re.cachingFunction,o=(()=>{if(!r)return;let p=this.signature.getInputFields().map(d=>d.name);return this.computeCacheKey(t,p)})();if(r&&o){let p;try{p=await r(o)}catch{}if(p!==void 0){yield{version:0,index:0,delta:p};return}}if(!n?.resultPicker){yield*this._forward1(e,t,{...n,stream:!0});return}let i=this._forward1(e,t,{...n,stream:!0}),a=[],u=0;for await(let p of i)p.version!==u&&(a=[]),u=p.version,a=xi(a,p);let l=await zo(a,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),c=a[l];if(c){if(r&&o)try{await r(o,c.delta)}catch{}yield{version:u,index:l,delta:c.delta}}}setExamples(e,t){super.setExamples(e,t)}isDebug(e,t){return t?.debug??this.options?.debug??re.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??re.logger??e.getLogger()}computeCacheKey(e,t){let n=He("sha256");n.update(this.signature.hash()??"");let r=o=>{let i=typeof o;if(n.update(`|${i}|`),o==null){n.update("null");return}if(i==="string"||i==="number"||i==="boolean"){n.update(String(o));return}if(Array.isArray(o)){n.update("[");for(let a of o)r(a);n.update("]");return}if(typeof o=="object"&&o!==null&&"mimeType"in o&&"data"in o){let a=o;n.update(a.mimeType??"");let u=He("sha256").update(a.data??"").digest("hex");n.update(u);return}if(typeof o=="object"){let a=o,u=Object.keys(a).sort();for(let l of u)n.update(`{${l}}`),r(a[l]);return}n.update(String(o))};if(Array.isArray(e))for(let o of e){n.update(`role:${o.role}`);let i=t.map(a=>o.values?.[a]);for(let a of i)r(a)}else{let o=t.map(i=>e?.[i]);for(let i of o)r(i)}return n.digest("hex")}},vr=class extends Error{details;constructor(e,t,n){super(e),this.name="AxGenerateError",this.details=t,n?.cause&&(this.cause=n.cause)}toJSON(){let e=this.cause;return{name:this.name,message:this.message,details:this.details,cause:e?{name:e.name,message:e.message,stack:e.stack}:void 0,stack:this.stack}}};function _i(s,e,t){let n=s instanceof Error?s:new Error(String(s));if(n instanceof ke)return n;let r=(n.message||"").toLowerCase();if(r.includes("at least")||r.includes("at most")||r.includes("must match pattern")||r.includes("invalid url")||r.includes("required")||r.includes("missing")||r.includes("valid email")||r.includes("number must be")||n.name==="ValidationError"||n.name==="AssertionError"||n.name==="AxAssertionError"||n.stack?.includes("asserts.ts"))return n;let i=e.getLastUsedChatModel(),a=e.getLastUsedModelConfig(),u={model:i,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new vr(`Generate failed: ${n.message}`,u,{cause:n})}var nd=s=>s.replace(/^\W+|\W+$/g,""),rd=(s,e)=>{let t=s.search(e);if(t===-1)return[s];let n=s.match(e);if(!n)throw new Error("Match failed unexpectedly.");let r=s.substring(0,t),o=s.substring(t+n[0].length);return[r,o]},od=s=>{let e=new Set,t=[];for(let n of s)e.has(n)||(e.add(n),t.push(n));return t},sd=s=>{let e=s.match(/^(\d+)[.,\s]+(.*)$/);if(!e||e.length<3)throw new Error('line must start with a number, a dot and then text. e.g. "1. hello"');let t=Number.parseInt(e[1],10),n=e[2].trim();return{id:t,text:n}},id=s=>{let e=s.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():s},ad=(s,e)=>{let t=[];for(let n=0;n<s.length;n+=e)t.push(s.slice(n,n+e));return t},Ho={trimNonAlphaNum:nd,splitIntoTwo:rd,dedup:od,extractIdAndText:sd,extractIndexPrefixedText:id,batchArray:ad};var Ko=class extends Ie{constructor(e){super(`"You are a re-ranker assistant tasked with evaluating a set of content items in relation to a specific question. Your role involves critically analyzing each content item to determine its relevance to the question and re-ranking them accordingly. This process includes assigning a relevance score from 0 to 10 to each content item based on how well it answers the question, its coverage of the topic, and the reliability of its information. This re-ranked list should start with the content item that is most relevant to the question and end with the least relevant. Output only the list."
410
+ query: string, items: string[] -> rankedItems: string[] "list of id, 5-words Rationale, relevance score"`,e)}forward=async(e,t,n)=>{let{rankedItems:r}=await super.forward(e,t,n),o=r.map(a=>{let{id:u}=Ho.extractIdAndText(a);return u});return{rankedItems:t.items.map((a,u)=>{let l=o[u];return l!==void 0?t.items[l]:void 0}).filter(a=>a!==void 0)}}};var Wo=class{tikaUrl;fetch;constructor(e){let t=e??{url:"http://localhost:9998/"};this.tikaUrl=new URL("/tika",t.url),this.fetch=t.fetch}async _convert(e,t){if(!e)throw new Error("Failed to read file data");let n=t?.format==="html"?"text/html":"text/plain";try{let r={body:e,headers:{Accept:n},method:"PUT"};typeof window>"u"&&typeof process<"u"&&(r.duplex="half");let o=await(this.fetch??fetch)(this.tikaUrl,r);if(!o.ok)throw new Error(`Failed to upload file: ${o.statusText}`);return await o.text()}catch(r){throw new Error(`Error converting file: ${r}`)}}async convert(e,t){let n=[],r=t?.batchSize??10;for(let o=0;o<e.length;o+=r){let a=e.slice(o,o+r).map(l=>this._convert(l,{format:t?.format})),u=await Promise.all(a);n.push(...u)}return n}};var Oy=new ze,Vo=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},Jo=class{ai;db;debug;constructor(e){this.db=new wt,this.ai=e}getState(){return this.db.getDB()}setState(e){this.db.setDB(e)}setClasses=async(e,t)=>{for(let n of e){let r=await this.ai.embed({texts:n.getContext()},{abortSignal:t?.abortSignal});await this.db.upsert({id:n.getName(),table:"classes",values:r.embeddings[0]})}};async forward(e,t){let{embeddings:n}=await this.ai.embed({texts:[e]},{abortSignal:t?.abortSignal}),o=(await this.db.query({table:"classes",values:n[0]})).matches;if(typeof t?.cutoff=="number"){let{cutoff:a}=t;o=o.filter(u=>u.score<=a)}let i=o.at(0);return i?i.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var kl=new Set(["0o","0s","3a","3b","3d","6b","6o","a","a1","a2","a3","a4","ab","able","about","above","abst","ac","accordance","according","accordingly","across","act","actually","ad","added","adj","ae","af","affected","affecting","affects","after","afterwards","ag","again","against","ah","ain","ain't","aj","al","all","allow","allows","almost","alone","along","already","also","although","always","am","among","amongst","amoungst","amount","an","and","announce","another","any","anybody","anyhow","anymore","anyone","anything","anyway","anyways","anywhere","ao","ap","apart","apparently","appear","appreciate","appropriate","approximately","ar","are","aren","arent","aren't","arise","around","as","a's","aside","ask","asking","associated","at","au","auth","av","available","aw","away","awfully","ax","ay","az","b","b1","b2","b3","ba","back","bc","bd","be","became","because","become","becomes","becoming","been","before","beforehand","begin","beginning","beginnings","begins","behind","being","believe","below","beside","besides","best","better","between","beyond","bi","bill","biol","bj","bk","bl","bn","both","bottom","bp","br","brief","briefly","bs","bt","bu","but","bx","by","c","c1","c2","c3","ca","call","came","can","cannot","cant","can't","cause","causes","cc","cd","ce","certain","certainly","cf","cg","ch","changes","ci","cit","cj","cl","clearly","cm","c'mon","cn","co","com","come","comes","con","concerning","consequently","consider","considering","contain","containing","contains","corresponding","could","couldn","couldnt","couldn't","course","cp","cq","cr","cry","cs","c's","ct","cu","currently","cv","cx","cy","cz","d","d2","da","date","dc","dd","de","definitely","describe","described","despite","detail","df","di","did","didn","didn't","different","dj","dk","dl","do","does","doesn","doesn't","doing","don","done","don't","down","downwards","dp","dr","ds","dt","du","due","during","dx","dy","e","e2","e3","ea","each","ec","ed","edu","ee","ef","effect","eg","ei","eight","eighty","either","ej","el","eleven","else","elsewhere","em","empty","en","end","ending","enough","entirely","eo","ep","eq","er","es","especially","est","et","et-al","etc","eu","ev","even","ever","every","everybody","everyone","everything","everywhere","ex","exactly","example","except","ey","f","f2","fa","far","fc","few","ff","fi","fifteen","fifth","fify","fill","find","fire","first","five","fix","fj","fl","fn","fo","followed","following","follows","for","former","formerly","forth","forty","found","four","fr","from","front","ft","fu","full","further","furthermore","fy","g","ga","gave","ge","get","gets","getting","gi","give","given","gives","giving","gj","gl","go","goes","going","gone","got","gotten","gr","greetings","gs","gy","h","h2","h3","had","hadn","hadn't","happens","hardly","has","hasn","hasnt","hasn't","have","haven","haven't","having","he","hed","he'd","he'll","hello","help","hence","her","here","hereafter","hereby","herein","heres","here's","hereupon","hers","herself","hes","he's","hh","hi","hid","him","himself","his","hither","hj","ho","home","hopefully","how","howbeit","however","how's","hr","hs","http","hu","hundred","hy","i","i2","i3","i4","i6","i7","i8","ia","ib","ibid","ic","id","i'd","ie","if","ig","ignored","ih","ii","ij","il","i'll","im","i'm","immediate","immediately","importance","important","in","inasmuch","inc","indeed","index","indicate","indicated","indicates","information","inner","insofar","instead","interest","into","invention","inward","io","ip","iq","ir","is","isn","isn't","it","itd","it'd","it'll","its","it's","itself","iv","i've","ix","iy","iz","j","jj","jr","js","jt","ju","just","k","ke","keep","keeps","kept","kg","kj","km","know","known","knows","ko","l","l2","la","largely","last","lately","later","latter","latterly","lb","lc","le","least","les","less","lest","let","lets","let's","lf","like","liked","likely","line","little","lj","ll","ll","ln","lo","look","looking","looks","los","lr","ls","lt","ltd","m","m2","ma","made","mainly","make","makes","many","may","maybe","me","mean","means","meantime","meanwhile","merely","mg","might","mightn","mightn't","mill","million","mine","miss","ml","mn","mo","more","moreover","most","mostly","move","mr","mrs","ms","mt","mu","much","mug","must","mustn","mustn't","my","myself","model","n","n2","na","name","namely","nay","nc","nd","ne","near","nearly","necessarily","necessary","need","needn","needn't","needs","neither","never","nevertheless","new","next","ng","ni","nine","ninety","nj","nl","nn","no","nobody","non","none","nonetheless","noone","nor","normally","nos","not","noted","nothing","novel","now","nowhere","nr","ns","nt","ny","o","oa","ob","obtain","obtained","obviously","oc","od","of","off","often","og","oh","oi","oj","ok","okay","ol","old","om","omitted","on","once","one","ones","only","onto","oo","op","oq","or","ord","os","ot","other","others","otherwise","ou","ought","our","ours","ourselves","out","outside","over","overall","ow","owing","own","ox","oz","p","p1","p2","p3","page","pagecount","pages","par","part","particular","particularly","pas","past","pc","pd","pe","per","perhaps","pf","ph","pi","pj","pk","pl","placed","please","plus","pm","pn","po","poorly","possible","possibly","potentially","pp","pq","pr","predominantly","present","presumably","previously","primarily","probably","promptly","proud","provides","ps","pt","pu","put","py","q","qj","qu","que","quickly","quite","qv","r","r2","ra","ran","rather","rc","rd","re","readily","really","reasonably","recent","recently","ref","refs","regarding","regardless","regards","related","relatively","research","research-articl","respectively","resulted","resulting","results","rf","rh","ri","right","rj","rl","rm","rn","ro","rq","rr","rs","rt","ru","run","rv","ry","s","s2","sa","said","same","saw","say","saying","says","sc","sd","se","sec","second","secondly","section","see","seeing","seem","seemed","seeming","seems","seen","self","selves","sensible","sent","serious","seriously","seven","several","sf","shall","shan","shan't","she","shed","she'd","she'll","shes","she's","should","shouldn","shouldn't","should've","show","showed","shown","showns","shows","si","side","significant","significantly","similar","similarly","since","sincere","six","sixty","sj","sl","slightly","sm","sn","so","some","somebody","somehow","someone","somethan","something","sometime","sometimes","somewhat","somewhere","soon","sorry","sp","specifically","specified","specify","specifying","sq","sr","ss","st","still","stop","strongly","sub","substantially","successfully","such","sufficiently","suggest","sup","sure","sy","system","sz","t","t1","t2","t3","take","taken","taking","tb","tc","td","te","tell","ten","tends","tf","th","than","thank","thanks","thanx","that","that'll","thats","that's","that've","the","their","theirs","them","themselves","then","thence","there","thereafter","thereby","thered","therefore","therein","there'll","thereof","therere","theres","there's","thereto","thereupon","there've","these","they","theyd","they'd","they'll","theyre","they're","they've","thickv","thin","think","third","this","thorough","thoroughly","those","thou","though","thoughh","thousand","three","throug","through","throughout","thru","thus","ti","til","tip","tj","tl","tm","tn","to","together","too","took","top","toward","towards","tp","tq","tr","tried","tries","truly","try","trying","ts","t's","tt","tv","twelve","twenty","twice","two","tx","u","u201d","ue","ui","uj","uk","um","un","under","unfortunately","unless","unlike","unlikely","until","unto","uo","up","upon","ups","ur","us","use","used","useful","usefully","usefulness","uses","using","usually","ut","v","va","value","various","vd","ve","ve","very","via","viz","vj","vo","vol","vols","volumtype","vq","vs","vt","vu","w","wa","want","wants","was","wasn","wasnt","wasn't","way","we","wed","we'd","welcome","well","we'll","well-b","went","were","we're","weren","werent","weren't","we've","what","whatever","what'll","whats","what's","when","whence","whenever","when's","where","whereafter","whereas","whereby","wherein","wheres","where's","whereupon","wherever","whether","which","while","whim","whither","who","whod","whoever","whole","who'll","whom","whomever","whos","who's","whose","why","why's","wi","widely","will","willing","wish","with","within","without","wo","won","wonder","wont","won't","words","world","would","wouldn","wouldnt","wouldn't","www","x","x1","x2","x3","xf","xi","xj","xk","xl","xn","xo","xs","xt","xv","xx","y","y2","yes","yet","yj","yl","you","youd","you'd","you'll","your","youre","you're","yours","yourself","yourselves","you've","yr","ys","yt","z","zero","zi","zz","task"]);function Sl(s,e){return s.filter(t=>!e.has(t))}function vl(s){let e={};for(let t of s)e[t]=(e[t]||0)+1;return e}function Dt(s){let e=s.normalize("NFD");return e=e.replace(/\b(a|an|the)\b/g," "),e=e.split(/\s+/).join(" "),e=e.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g,""),e.toLowerCase()}function ud(s,e){return Dt(s)===Dt(e)?1:0}function ld(s,e){let t=Dt(s).split(" "),n=Dt(e).split(" "),r=vl(t),o=vl(n),i=0;for(let l in r){let c=r[l]??0,p=o[l]??0;i+=Math.min(c,p)}if(i===0)return 0;let a=i/t.length,u=i/n.length;return 2*a*u/(a+u)}function cd(s,e,t,n=!1){let r=Dt(s).split(" "),o=Dt(e).split(" "),i=Dt(t).split(" "),a=new Set([...kl,...r]);o=Sl(o,a),i=Sl(i,a);let u=0,l=u/o.length,c=u/i.length,p=2*l*c/(l+c);return n?c:p}var Ol={emScore:ud,f1Score:ld,novelF1ScoreOptimized:cd};var Qo=class{ai;program;examples;constructor({ai:e,program:t,examples:n=[]}){if(n.length===0)throw new Error("No examples found");this.ai=e,this.program=t,this.examples=n}async run(e){let t=Date.now(),n=this.examples.length,r=0;for(let i=0;i<n;i++){let a=this.examples[i];if(!a)throw new Error("Invalid example");try{let u=await this.program.forward(this.ai,a,{maxRetries:1}),l=await e({prediction:u,example:a});r+=l}catch(u){console.warn(`Program evaluation failed for example ${i}: ${u instanceof Error?u.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
404
411
  Performance: `,r,"/",n,"Average Score: ",o,`
405
- `)}};function Zo(i){return i.map(e=>{let t=e.type?.name??"string",n=e.description?`: ${e.description}`:"";return`- ${e.name} (${t})${n}`}).join(`
406
- `)}function Op(i,e){if(i===e)return!0;if(typeof i!=typeof e)return!1;if(typeof i=="object"&&i!==null&&e!==null){let t=JSON.stringify(i),n=JSON.stringify(e);return t===n}return!1}function Mp(i){return i==null?!1:!!(typeof i=="string"||typeof i=="number"||typeof i=="boolean"||Array.isArray(i)&&i.every(e=>typeof e!="object"))}var Bn=class{signature;options;constructor(e,t){this.signature=e,this.options={ai:t.ai,randomizeOrder:t.randomizeOrder??!0,model:t.model,criteria:t.criteria}}async evaluate(e,t,n){return n&&Mp(n)?this.runAbsolute(t,n):n?this.runRelativistic(e,t,n):this.runReferenceFree(e,t)}async runAbsolute(e,t){let n=this.signature.getOutputFields(),r=0,o=0,s=[];for(let u of n){let c=e[u.name],p=t[u.name];p!==void 0&&(o++,Op(c,p)?r++:s.push(`${u.name}: expected "${p}", got "${c}"`))}let a=o>0?r/o:0,l=a===1?"All fields match expected values exactly.":`Mismatches found: ${s.join("; ")}`;return{score:a,reasoning:l,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getDescription(),o=this.signature.getInputFields(),s=this.signature.getOutputFields(),a=this.options.randomizeOrder?Math.random()>.5:!0,l=a?t:n,u=a?n:t,c=`
412
+ `)}};function Yo(s){return s.map(e=>{let t=e.type?.name??"string",n=e.description?`: ${e.description}`:"";return`- ${e.name} (${t})${n}`}).join(`
413
+ `)}function pd(s,e){if(s===e)return!0;if(typeof s!=typeof e)return!1;if(typeof s=="object"&&s!==null&&e!==null){let t=JSON.stringify(s),n=JSON.stringify(e);return t===n}return!1}function dd(s){return s==null?!1:!!(typeof s=="string"||typeof s=="number"||typeof s=="boolean"||Array.isArray(s)&&s.every(e=>typeof e!="object"))}var On=class{signature;options;constructor(e,t){this.signature=e,this.options={ai:t.ai,randomizeOrder:t.randomizeOrder??!0,model:t.model,criteria:t.criteria}}async evaluate(e,t,n){return n&&dd(n)?this.runAbsolute(t,n):n?this.runRelativistic(e,t,n):this.runReferenceFree(e,t)}async runAbsolute(e,t){let n=this.signature.getOutputFields(),r=0,o=0,i=[];for(let l of n){let c=e[l.name],p=t[l.name];p!==void 0&&(o++,pd(c,p)?r++:i.push(`${l.name}: expected "${p}", got "${c}"`))}let a=o>0?r/o:0,u=a===1?"All fields match expected values exactly.":`Mismatches found: ${i.join("; ")}`;return{score:a,reasoning:u,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getDescription(),o=this.signature.getInputFields(),i=this.signature.getOutputFields(),a=this.options.randomizeOrder?Math.random()>.5:!0,u=a?t:n,l=a?n:t,c=`
407
414
  You are an impartial judge comparing two AI responses to the same input.
408
415
 
409
416
  **Task Description:** ${r||"Complete the task based on the input."}
410
417
 
411
418
  **Input Fields:**
412
- ${Zo(o)}
419
+ ${Yo(o)}
413
420
 
414
421
  **Output Fields:**
415
- ${Zo(s)}
422
+ ${Yo(i)}
416
423
 
417
424
  **Instructions:**
418
425
  1. Carefully analyze both Response A and Response B
@@ -421,14 +428,14 @@ ${Zo(s)}
421
428
  4. Provide clear reasoning for your decision
422
429
 
423
430
  **Important:** Do not be swayed by response length alone. Focus on quality and correctness.
424
- `.trim(),p=new Ee(`
431
+ `.trim(),p=new Ie(`
425
432
  input:string "The original input",
426
433
  response_a:string "Response A",
427
434
  response_b:string "Response B"
428
435
  ->
429
436
  winner:class "A, B, Tie" "Which response is better",
430
437
  reasoning:string "Detailed explanation for the decision"
431
- `);p.setInstruction(c);let d=await p.forward(this.options.ai,{input:JSON.stringify(e),response_a:JSON.stringify(l),response_b:JSON.stringify(u)},{model:this.options.model}),m,g,h=d.winner.toUpperCase();return h==="A"?m=a?"student":"teacher":h==="B"?m=a?"teacher":"student":m="tie",m==="student"?g=1:m==="tie"?g=.5:g=0,{score:g,reasoning:d.reasoning,mode:"relativistic",winner:m}}async runReferenceFree(e,t){let n=this.signature.getDescription(),r=this.signature.getInputFields(),o=this.signature.getOutputFields(),s=this.options.criteria||`
438
+ `);p.setInstruction(c);let d=await p.forward(this.options.ai,{input:JSON.stringify(e),response_a:JSON.stringify(u),response_b:JSON.stringify(l)},{model:this.options.model}),m,g,f=d.winner.toUpperCase();return f==="A"?m=a?"student":"teacher":f==="B"?m=a?"teacher":"student":m="tie",m==="student"?g=1:m==="tie"?g=.5:g=0,{score:g,reasoning:d.reasoning,mode:"relativistic",winner:m}}async runReferenceFree(e,t){let n=this.signature.getDescription(),r=this.signature.getInputFields(),o=this.signature.getOutputFields(),i=this.options.criteria||`
432
439
  Based on the task description and output requirements:
433
440
  - Accuracy: Is the response factually correct and complete?
434
441
  - Relevance: Does it address the input appropriately?
@@ -440,13 +447,13 @@ You are evaluating the quality of an AI response.
440
447
  **Task Description:** ${n||"Complete the task based on the input."}
441
448
 
442
449
  **Input Fields:**
443
- ${Zo(r)}
450
+ ${Yo(r)}
444
451
 
445
452
  **Output Fields:**
446
- ${Zo(o)}
453
+ ${Yo(o)}
447
454
 
448
455
  **Evaluation Criteria:**
449
- ${s}
456
+ ${i}
450
457
 
451
458
  **Quality Tiers:**
452
459
  - excellent: Exceptional response that fully addresses all criteria with high quality
@@ -456,21 +463,21 @@ ${s}
456
463
  - unacceptable: Response is wrong, harmful, or completely off-topic
457
464
 
458
465
  First explain your reasoning, then classify the response into one of the quality tiers.
459
- `.trim(),l=new Ee(`
466
+ `.trim(),u=new Ie(`
460
467
  input:string "The original input",
461
468
  response:string "The AI response to evaluate"
462
469
  ->
463
470
  reasoning:string "Detailed explanation for the quality assessment",
464
471
  quality:class "excellent, good, acceptable, poor, unacceptable" "Quality tier"
465
- `);l.setInstruction(a);let u=await l.forward(this.options.ai,{input:JSON.stringify(e),response:JSON.stringify(t)},{model:this.options.model}),c={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},p=u.quality.toLowerCase();return{score:c[p]??.5,reasoning:u.reasoning,mode:"reference-free",qualityTier:p}}toMetricFn(){return async e=>{let{example:t,prediction:n}=e,r={};for(let u of this.signature.getInputFields())u.name in t&&(r[u.name]=t[u.name]);let o={},s=!1;for(let u of this.signature.getOutputFields())u.name in t&&(o[u.name]=t[u.name],s=!0);let a=n;return(await this.evaluate(r,a,s?o:void 0)).score}}getSignature(){return this.signature}};function xu(){let i=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${i}-${e}`}var jn=class i{gen;options;constructor(e,t){this.gen=e,this.options={name:t.name,storage:t.storage,logInputs:t.logInputs??!0,logOutputs:t.logOutputs??!0,metadata:t.metadata,onTrace:t.onTrace,throwOnError:t.throwOnError??!1}}async*streamingForward(e,t,n){let r=xu(),o=new Date,s,a;try{let l=this.gen.streamingForward(e,t,n);for await(let u of l)u.partial?s=u.partial:u.delta,yield u}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async forward(e,t,n){let r=xu(),o=new Date,s,a;try{return s=await this.gen.forward(e,t,n),s}catch(l){throw a=l instanceof Error?l.message:String(l),l}finally{let l=new Date,u=l.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&s?s:{},startTime:o,endTime:l,durationMs:u,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async saveTrace(e){try{await this.options.storage.save(this.options.name,e),this.options.onTrace&&this.options.onTrace(e)}catch(t){if(this.options.throwOnError)throw t;console.warn("AxTraceLogger: Failed to save trace:",t)}}getGen(){return this.gen}getName(){return this.options.name}getStorage(){return this.options.storage}setMetadata(e){this.options.metadata=e}clone(e){return new i(e??this.gen.clone(),{name:this.options.name,storage:this.options.storage,logInputs:this.options.logInputs,logOutputs:this.options.logOutputs,metadata:this.options.metadata,onTrace:this.options.onTrace,throwOnError:this.options.throwOnError})}};var Au=i=>{console.log(i)},_i=(i=Au)=>{let e=new nt,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
472
+ `);u.setInstruction(a);let l=await u.forward(this.options.ai,{input:JSON.stringify(e),response:JSON.stringify(t)},{model:this.options.model}),c={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},p=l.quality.toLowerCase();return{score:c[p]??.5,reasoning:l.reasoning,mode:"reference-free",qualityTier:p}}toMetricFn(){return async e=>{let{example:t,prediction:n}=e,r={};for(let l of this.signature.getInputFields())l.name in t&&(r[l.name]=t[l.name]);let o={},i=!1;for(let l of this.signature.getOutputFields())l.name in t&&(o[l.name]=t[l.name],i=!0);let a=n;return(await this.evaluate(r,a,i?o:void 0)).score}}getSignature(){return this.signature}};function El(){let s=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${s}-${e}`}var En=class s{gen;options;constructor(e,t){this.gen=e,this.options={name:t.name,storage:t.storage,logInputs:t.logInputs??!0,logOutputs:t.logOutputs??!0,metadata:t.metadata,onTrace:t.onTrace,throwOnError:t.throwOnError??!1}}async*streamingForward(e,t,n){let r=El(),o=new Date,i,a;try{let u=this.gen.streamingForward(e,t,n);for await(let l of u)l.partial?i=l.partial:l.delta,yield l}catch(u){throw a=u instanceof Error?u.message:String(u),u}finally{let u=new Date,l=u.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&i?i:{},startTime:o,endTime:u,durationMs:l,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async forward(e,t,n){let r=El(),o=new Date,i,a;try{return i=await this.gen.forward(e,t,n),i}catch(u){throw a=u instanceof Error?u.message:String(u),u}finally{let u=new Date,l=u.getTime()-o.getTime(),c={type:"trace",id:r,name:this.options.name,input:this.options.logInputs?t:{},output:this.options.logOutputs&&i?i:{},startTime:o,endTime:u,durationMs:l,model:n?.model??void 0,metadata:this.options.metadata,error:a};this.saveTrace(c)}}async saveTrace(e){try{await this.options.storage.save(this.options.name,e),this.options.onTrace&&this.options.onTrace(e)}catch(t){if(this.options.throwOnError)throw t;console.warn("AxTraceLogger: Failed to save trace:",t)}}getGen(){return this.gen}getName(){return this.options.name}getStorage(){return this.options.storage}setMetadata(e){this.options.metadata=e}clone(e){return new s(e??this.gen.clone(),{name:this.options.name,storage:this.options.storage,logInputs:this.options.logInputs,logOutputs:this.options.logOutputs,metadata:this.options.metadata,onTrace:this.options.onTrace,throwOnError:this.options.throwOnError})}};function Ml(s){return Re.create(s)}function qe(s,e){let t=typeof s=="string"?Re.create(s):s;return new Ie(t,e)}var Pl=s=>{console.log(s)},Ni=(s=Pl)=>{let e=new ze,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
466
473
  ${e.blueBright("\u25CF ")}${e.whiteBright("Optimization Started")}
467
474
  ${t}
468
475
  ${e.white("Optimizer:")} ${e.cyan(r.value.optimizerType)}
469
476
  ${e.white("Examples:")} ${e.green(r.value.exampleCount.toString())} training, ${e.green(r.value.validationCount.toString())} validation
470
477
  ${e.white("Config:")} ${e.white(JSON.stringify(r.value.config).slice(0,80))}${JSON.stringify(r.value.config).length>80?"...":""}
471
478
  ${n}
472
- `;break;case"RoundProgress":{let s=r.value.configuration||{},a=[];s.temperature!==void 0&&typeof s.temperature=="number"&&a.push(`T=${s.temperature.toFixed(2)}`),s.bootstrappedDemos!==void 0&&a.push(`demos=${s.bootstrappedDemos}`),Object.entries(s).forEach(([p,d])=>{p!=="temperature"&&p!=="bootstrappedDemos"&&p!=="trialNumber"&&typeof d=="number"&&a.push(`${p}=${d.toFixed(2)}`)});let l=r.value.currentScore-r.value.bestScore,u=l>0?e.greenBright(` \u2191${l.toFixed(3)}`):l<0?e.red(` \u2193${Math.abs(l).toFixed(3)}`):"",c=typeof r.value.totalRounds=="number"&&r.value.totalRounds>0?r.value.totalRounds:typeof s.totalRounds=="number"&&s.totalRounds>0?s.totalRounds:0;o=`${e.yellow("\u25CF ")}${e.whiteBright(`Round ${r.value.round}/${c}`)}`+(s.trialNumber!==void 0?e.gray(` [Trial #${s.trialNumber}]`):"")+`
473
- ${e.white("Score:")} ${e.green(r.value.currentScore.toFixed(3))} ${e.white("(best:")} ${e.greenBright(r.value.bestScore.toFixed(3))}${e.white(")")}${u}
479
+ `;break;case"RoundProgress":{let i=r.value.configuration||{},a=[];i.temperature!==void 0&&typeof i.temperature=="number"&&a.push(`T=${i.temperature.toFixed(2)}`),i.bootstrappedDemos!==void 0&&a.push(`demos=${i.bootstrappedDemos}`),Object.entries(i).forEach(([p,d])=>{p!=="temperature"&&p!=="bootstrappedDemos"&&p!=="trialNumber"&&typeof d=="number"&&a.push(`${p}=${d.toFixed(2)}`)});let u=r.value.currentScore-r.value.bestScore,l=u>0?e.greenBright(` \u2191${u.toFixed(3)}`):u<0?e.red(` \u2193${Math.abs(u).toFixed(3)}`):"",c=typeof r.value.totalRounds=="number"&&r.value.totalRounds>0?r.value.totalRounds:typeof i.totalRounds=="number"&&i.totalRounds>0?i.totalRounds:0;o=`${e.yellow("\u25CF ")}${e.whiteBright(`Round ${r.value.round}/${c}`)}`+(i.trialNumber!==void 0?e.gray(` [Trial #${i.trialNumber}]`):"")+`
480
+ ${e.white("Score:")} ${e.green(r.value.currentScore.toFixed(3))} ${e.white("(best:")} ${e.greenBright(r.value.bestScore.toFixed(3))}${e.white(")")}${l}
474
481
  `+(a.length>0?` ${e.white("Config:")} ${e.cyan(a.join(", "))}
475
482
  `:"")}break;case"EarlyStopping":o=`
476
483
  ${e.red("\u25CF ")}${e.whiteBright("Early Stopping")}
@@ -479,15 +486,15 @@ ${t}
479
486
  ${e.white("Reason:")} ${e.yellow(r.value.reason)}
480
487
  ${e.white("Final Score:")} ${e.green(r.value.finalScore.toFixed(3))}
481
488
  ${n}
482
- `;break;case"OptimizationComplete":{let s="";r.value.explanation&&(s+=`
489
+ `;break;case"OptimizationComplete":{let i="";r.value.explanation&&(i+=`
483
490
  ${e.blueBright("\u{1F4CA} Summary:")}
484
491
  ${e.white(r.value.explanation)}
485
- `),r.value.performanceAssessment&&(s+=`
492
+ `),r.value.performanceAssessment&&(i+=`
486
493
  ${e.yellowBright("\u26A1 Performance:")}
487
494
  ${e.white(r.value.performanceAssessment)}
488
- `),r.value.recommendations&&r.value.recommendations.length>0&&(s+=`
495
+ `),r.value.recommendations&&r.value.recommendations.length>0&&(i+=`
489
496
  ${e.greenBright("\u{1F4A1} Recommendations:")}
490
- `,r.value.recommendations.forEach((a,l)=>{s+=` ${e.white(`${l+1}.`)} ${e.white(a)}
497
+ `,r.value.recommendations.forEach((a,u)=>{i+=` ${e.white(`${u+1}.`)} ${e.white(a)}
491
498
  `})),o=`
492
499
  ${e.green("\u25CF ")}${e.whiteBright("Optimization Complete")}
493
500
  ${t}
@@ -495,9 +502,9 @@ ${t}
495
502
  ${e.white("Best Config:")} ${e.cyan(JSON.stringify(r.value.bestConfiguration).slice(0,80))}${JSON.stringify(r.value.bestConfiguration).length>80?"...":""}
496
503
  ${e.white("Total Calls:")} ${e.white(r.value.stats?.totalCalls?.toString()||"N/A")}
497
504
  ${e.white("Success Rate:")} ${e.green(`${((r.value.stats?.successfulDemos||0)/Math.max(r.value.stats?.totalCalls||1,1)*100).toFixed(1)}%`)}
498
- `+s+`${n}
505
+ `+i+`${n}
499
506
  `}break;case"ConfigurationProposal":o=`${e.magenta("\u25CF ")}${e.whiteBright(`${r.value.type} Proposals`)} ${e.white(`(${r.value.count})`)}
500
- ${e.white("Candidates:")} ${e.white(r.value.proposals.slice(0,2).map(s=>typeof s=="string"?`"${s.slice(0,40)}..."`:`${JSON.stringify(s).slice(0,40)}...`).join(", "))}
507
+ ${e.white("Candidates:")} ${e.white(r.value.proposals.slice(0,2).map(i=>typeof i=="string"?`"${i.slice(0,40)}..."`:`${JSON.stringify(i).slice(0,40)}...`).join(", "))}
501
508
  `;break;case"BootstrappedDemos":o=`${e.cyan("\u25CF ")}${e.whiteBright("Bootstrapped Demos")} ${e.white(`(${r.value.count})`)}
502
509
  ${e.white("Generated:")} ${e.green(r.value.count.toString())} demonstration examples
503
510
  `;break;case"BestConfigFound":o=`${e.green("\u25CF ")}${e.whiteBright("Best Configuration Found")}
@@ -505,7 +512,7 @@ ${t}
505
512
  ${e.white("Config:")} ${e.cyan(JSON.stringify(r.value.config).slice(0,80))}${JSON.stringify(r.value.config).length>80?"...":""}
506
513
  `;break;default:o=`${e.red("\u25CF ")}${e.whiteBright("Unknown Event")}
507
514
  ${e.white(JSON.stringify(r).slice(0,100))}${JSON.stringify(r).length>100?"...":""}
508
- `}i(o)}},yu=(i=Au)=>{let e="\u2500".repeat(60);return t=>{let n="";switch(t.name){case"OptimizationStart":n=`[ OPTIMIZATION START: ${t.value.optimizerType} ]
515
+ `}s(o)}},Fl=(s=Pl)=>{let e="\u2500".repeat(60);return t=>{let n="";switch(t.name){case"OptimizationStart":n=`[ OPTIMIZATION START: ${t.value.optimizerType} ]
509
516
  ${e}
510
517
  Config: ${JSON.stringify(t.value.config,null,2)}
511
518
  Examples: ${t.value.exampleCount}, Validation: ${t.value.validationCount}
@@ -531,72 +538,77 @@ Score: ${t.value.score.toFixed(3)}
531
538
  Config: ${JSON.stringify(t.value.config)}
532
539
  ${e}`;break;default:n=`[ UNKNOWN OPTIMIZER EVENT ]
533
540
  ${JSON.stringify(t)}
534
- ${e}`}i(n)}},Pr=_i();var Ni={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},es,Ep=i=>{if(es)return es;if(i)return es=Pp(i),es};var ts=Ni,bu=i=>{ts={...ts,...i}},Iu=()=>({...ts}),Pp=i=>({optimizationLatencyHistogram:i.createHistogram("ax_optimizer_optimization_duration_ms",{description:"End-to-end duration of optimization runs",unit:"ms"}),optimizationRequestsCounter:i.createCounter("ax_optimizer_optimization_requests_total",{description:"Total number of optimization requests"}),optimizationErrorsCounter:i.createCounter("ax_optimizer_optimization_errors_total",{description:"Total number of failed optimizations"}),convergenceRoundsHistogram:i.createHistogram("ax_optimizer_convergence_rounds",{description:"Number of rounds until convergence"}),convergenceScoreGauge:i.createGauge("ax_optimizer_convergence_score",{description:"Current best score during optimization"}),convergenceImprovementGauge:i.createGauge("ax_optimizer_convergence_improvement",{description:"Improvement in score from baseline"}),stagnationRoundsGauge:i.createGauge("ax_optimizer_stagnation_rounds",{description:"Number of rounds without improvement"}),earlyStoppingCounter:i.createCounter("ax_optimizer_early_stopping_total",{description:"Total number of early stopping events"}),tokenUsageCounter:i.createCounter("ax_optimizer_token_usage_total",{description:"Total tokens used during optimization"}),costUsageCounter:i.createCounter("ax_optimizer_cost_usage_total",{description:"Total cost incurred during optimization",unit:"$"}),memoryUsageGauge:i.createGauge("ax_optimizer_memory_usage_bytes",{description:"Peak memory usage during optimization",unit:"By"}),optimizationDurationHistogram:i.createHistogram("ax_optimizer_duration_ms",{description:"Duration of optimization runs",unit:"ms"}),teacherStudentUsageCounter:i.createCounter("ax_optimizer_teacher_student_usage_total",{description:"Total number of teacher-student interactions"}),teacherStudentLatencyHistogram:i.createHistogram("ax_optimizer_teacher_student_latency_ms",{description:"Latency of teacher-student interactions",unit:"ms"}),teacherStudentScoreImprovementGauge:i.createGauge("ax_optimizer_teacher_student_score_improvement",{description:"Score improvement from teacher-student interactions"}),checkpointSaveCounter:i.createCounter("ax_optimizer_checkpoint_save_total",{description:"Total number of checkpoint saves"}),checkpointLoadCounter:i.createCounter("ax_optimizer_checkpoint_load_total",{description:"Total number of checkpoint loads"}),checkpointSaveLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_save_latency_ms",{description:"Latency of checkpoint save operations",unit:"ms"}),checkpointLoadLatencyHistogram:i.createHistogram("ax_optimizer_checkpoint_load_latency_ms",{description:"Latency of checkpoint load operations",unit:"ms"}),paretoOptimizationsCounter:i.createCounter("ax_optimizer_pareto_optimizations_total",{description:"Total number of Pareto optimizations"}),paretoFrontSizeHistogram:i.createHistogram("ax_optimizer_pareto_front_size",{description:"Size of Pareto frontier"}),paretoHypervolumeGauge:i.createGauge("ax_optimizer_pareto_hypervolume",{description:"Hypervolume of Pareto frontier"}),paretoSolutionsGeneratedHistogram:i.createHistogram("ax_optimizer_pareto_solutions_generated",{description:"Number of solutions generated for Pareto optimization"}),programInputFieldsGauge:i.createGauge("ax_optimizer_program_input_fields",{description:"Number of input fields in optimized program"}),programOutputFieldsGauge:i.createGauge("ax_optimizer_program_output_fields",{description:"Number of output fields in optimized program"}),examplesCountGauge:i.createGauge("ax_optimizer_examples_count",{description:"Number of training examples used"}),validationSetSizeGauge:i.createGauge("ax_optimizer_validation_set_size",{description:"Size of validation set used"}),evaluationLatencyHistogram:i.createHistogram("ax_optimizer_evaluation_latency_ms",{description:"Latency of program evaluations",unit:"ms"}),demoGenerationLatencyHistogram:i.createHistogram("ax_optimizer_demo_generation_latency_ms",{description:"Latency of demo generation",unit:"ms"}),metricComputationLatencyHistogram:i.createHistogram("ax_optimizer_metric_computation_latency_ms",{description:"Latency of metric computation",unit:"ms"}),optimizerTypeGauge:i.createGauge("ax_optimizer_type",{description:"Type of optimizer being used"}),targetScoreGauge:i.createGauge("ax_optimizer_target_score",{description:"Target score for optimization"}),maxRoundsGauge:i.createGauge("ax_optimizer_max_rounds",{description:"Maximum rounds for optimization"})}),Tt=i=>{let e={};for(let[t,n]of Object.entries(i))if(n!=null){let r=String(n),o=ts.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},Fp=(i,e,t,n,r,o)=>{try{let s=Tt({success:t.toString(),optimizer_type:n,...r?{program_signature:r}:{},...o});i.optimizationLatencyHistogram&&i.optimizationLatencyHistogram.record(e,s),i.optimizationRequestsCounter&&i.optimizationRequestsCounter.add(1,s),!t&&i.optimizationErrorsCounter&&i.optimizationErrorsCounter.add(1,s)}catch(s){console.warn("Failed to record optimization metric:",s)}},_p=(i,e,t,n,r,o,s)=>{try{let a=Tt({optimizer_type:o,...s});i.convergenceRoundsHistogram&&i.convergenceRoundsHistogram.record(e,a),i.convergenceScoreGauge&&i.convergenceScoreGauge.record(t,a),i.convergenceImprovementGauge&&i.convergenceImprovementGauge.record(n,a),i.stagnationRoundsGauge&&i.stagnationRoundsGauge.record(r,a)}catch(a){console.warn("Failed to record convergence metric:",a)}},Np=(i,e,t,n)=>{try{let r=Tt({reason:e,optimizer_type:t,...n});i.earlyStoppingCounter&&i.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},$p=(i,e,t,n,r,o)=>{try{let s=Tt({optimizer_type:n,...o});i.tokenUsageCounter&&i.tokenUsageCounter.add(e,s),i.costUsageCounter&&i.costUsageCounter.add(t,s),r!==void 0&&i.memoryUsageGauge&&i.memoryUsageGauge.record(r,s)}catch(s){console.warn("Failed to record resource usage metric:",s)}},Lp=(i,e,t,n)=>{try{let r=Tt({optimizer_type:t,...n});i.optimizationDurationHistogram&&i.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},Dp=(i,e,t,n,r)=>{try{let o=Tt({optimizer_type:n,...r});i.teacherStudentUsageCounter&&i.teacherStudentUsageCounter.add(1,o),i.teacherStudentLatencyHistogram&&i.teacherStudentLatencyHistogram.record(e,o),i.teacherStudentScoreImprovementGauge&&i.teacherStudentScoreImprovementGauge.record(t,o)}catch(o){console.warn("Failed to record teacher-student metric:",o)}},Gp=(i,e,t,n,r,o)=>{try{let s=Tt({operation:e,success:n.toString(),optimizer_type:r,...o});e==="save"?(i.checkpointSaveCounter&&i.checkpointSaveCounter.add(1,s),i.checkpointSaveLatencyHistogram&&i.checkpointSaveLatencyHistogram.record(t,s)):(i.checkpointLoadCounter&&i.checkpointLoadCounter.add(1,s),i.checkpointLoadLatencyHistogram&&i.checkpointLoadLatencyHistogram.record(t,s))}catch(s){console.warn("Failed to record checkpoint metric:",s)}},Up=(i,e,t,n,r,o)=>{try{let s=Tt({optimizer_type:n,...o});i.paretoOptimizationsCounter&&i.paretoOptimizationsCounter.add(1,s),i.paretoFrontSizeHistogram&&i.paretoFrontSizeHistogram.record(e,s),r!==void 0&&i.paretoHypervolumeGauge&&i.paretoHypervolumeGauge.record(r,s),i.paretoSolutionsGeneratedHistogram&&i.paretoSolutionsGeneratedHistogram.record(t,s)}catch(s){console.warn("Failed to record Pareto metric:",s)}},Bp=(i,e,t,n,r,o,s)=>{try{let a=Tt({optimizer_type:o,...s});i.programInputFieldsGauge&&i.programInputFieldsGauge.record(e,a),i.programOutputFieldsGauge&&i.programOutputFieldsGauge.record(t,a),i.examplesCountGauge&&i.examplesCountGauge.record(n,a),i.validationSetSizeGauge&&i.validationSetSizeGauge.record(r,a)}catch(a){console.warn("Failed to record program complexity metric:",a)}},jp=(i,e,t,n,r)=>{try{let o=Tt({metric_type:e,optimizer_type:n,...r});switch(e){case"evaluation":i.evaluationLatencyHistogram&&i.evaluationLatencyHistogram.record(t,o);break;case"demo_generation":i.demoGenerationLatencyHistogram&&i.demoGenerationLatencyHistogram.record(t,o);break;case"metric_computation":i.metricComputationLatencyHistogram&&i.metricComputationLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record optimizer performance metric:",o)}},qp=(i,e,t,n,r)=>{try{let o=Tt({optimizer_type:e,...r});i.optimizerTypeGauge&&i.optimizerTypeGauge.record(1,o),t!==void 0&&i.targetScoreGauge&&i.targetScoreGauge.record(t,o),n!==void 0&&i.maxRoundsGauge&&i.maxRoundsGauge.record(n,o)}catch(o){console.warn("Failed to record optimizer configuration metric:",o)}},kt=class{bestScore;stats;instruction;demos;examples;modelConfig;optimizerType;optimizationTime;totalRounds;converged;scoreHistory;configurationHistory;constructor(e){this.bestScore=e.bestScore,this.stats=e.stats,this.instruction=e.instruction,this.demos=e.demos,this.examples=e.examples,this.modelConfig=e.modelConfig,this.optimizerType=e.optimizerType,this.optimizationTime=e.optimizationTime,this.totalRounds=e.totalRounds,this.converged=e.converged,this.scoreHistory=e.scoreHistory,this.configurationHistory=e.configurationHistory}applyTo(e){this.demos&&this.demos.length>0?e.setDemos(this.demos,{modelConfig:this.modelConfig}):this.modelConfig&&e.setDemos([],{modelConfig:this.modelConfig})}},Fr=class{tokenUsage={};totalTokens=0;costPerModel;maxCost;maxTokens;constructor(e){this.costPerModel=e?.costPerModel??{},this.maxCost=e?.maxCost,this.maxTokens=e?.maxTokens}trackTokens(e,t){this.tokenUsage[t]=(this.tokenUsage[t]||0)+e,this.totalTokens+=e}getCurrentCost(){let e=0;for(let[t,n]of Object.entries(this.tokenUsage)){let r=this.costPerModel[t]||.001;e+=n/1e3*r}return e}getTokenUsage(){return{...this.tokenUsage}}getTotalTokens(){return this.totalTokens}isLimitReached(){return this.maxTokens!==void 0&&this.totalTokens>=this.maxTokens||this.maxCost!==void 0&&this.getCurrentCost()>=this.maxCost}reset(){this.tokenUsage={},this.totalTokens=0}},rt=class{studentAI;teacherAI;targetScore;minSuccessRate;onProgress;onEarlyStop;costTracker;seed;checkpointSave;checkpointLoad;checkpointInterval;resumeFromCheckpoint;logger;verbose;debugOptimizer;optimizerLogger;currentRound=0;scoreHistory=[];configurationHistory=[];stats;metricsInstruments;resultExplainer;constructor(e){this.studentAI=e.studentAI,this.teacherAI=e.teacherAI,this.targetScore=e.targetScore,this.minSuccessRate=e.minSuccessRate,this.onProgress=e.onProgress,this.onEarlyStop=e.onEarlyStop,this.seed=e.seed,this.checkpointSave=e.checkpointSave,this.checkpointLoad=e.checkpointLoad,this.checkpointInterval=e.checkpointInterval??10,this.resumeFromCheckpoint=e.resumeFromCheckpoint,this.logger=e.logger,this.verbose=e.verbose;let t=new Fr({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=Ep(me.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?Pr:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Bt(me.customLabels,this.studentAI?.getOptions?.()?.customLabels,this.teacherAI?.getOptions?.()?.customLabels,e?.customLabels)}initializeResultExplainer(){this.resultExplainer=void 0}initializeStats(){return{totalCalls:0,successfulDemos:0,estimatedTokenUsage:0,earlyStopped:!1,resourceUsage:{totalTokens:0,totalTime:0,avgLatencyPerEval:0,costByModel:{}},convergenceInfo:{converged:!1,finalImprovement:0,stagnationRounds:0,convergenceThreshold:.01},bestScore:0,bestConfiguration:{}}}setupRandomSeed(){this.seed!==void 0&&(Math.random=(()=>{let e=this.seed;return()=>(e=(e*9301+49297)%233280,e/233280)})())}checkCostLimits(){return this.costTracker?.isLimitReached()??!1}checkTargetScore(e){return this.targetScore!==void 0&&e>=this.targetScore}updateResourceUsage(e,t=0){this.stats.resourceUsage.totalTime=Date.now()-e,this.stats.resourceUsage.totalTokens+=t,this.stats.totalCalls>0&&(this.stats.resourceUsage.avgLatencyPerEval=this.stats.resourceUsage.totalTime/this.stats.totalCalls)}triggerEarlyStopping(e,t,n){this.stats.earlyStopped=!0,this.stats.earlyStopping={bestScoreRound:t,patienceExhausted:e.includes("improvement"),reason:e},this.recordEarlyStoppingMetrics(e,"unknown",n),this.onEarlyStop&&this.onEarlyStop(e,this.stats),this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:e,finalScore:this.stats.bestScore??0,round:t}})}validateExamples(e,t=!0){if(!e||e.length===0)throw new Error("At least 1 example is required for optimization");if(t&&e.length<2)throw new Error("At least 2 examples are required for optimization with auto-splitting. Provide more examples to enable proper train/validation split.");let n=t?10:5;e.length<n&&this.verbose&&console.warn(`[Ax Optimizer] Warning: Only ${e.length} examples provided. Consider providing more examples (${n}+ recommended) for better optimization results.`)}getAIService(e=!1,t){return e&&t?.overrideTeacherAI?t.overrideTeacherAI:e&&this.teacherAI?this.teacherAI:this.studentAI}hasTeacherAI(e){return e?.overrideTeacherAI!==void 0||this.teacherAI!==void 0}getTeacherOrStudentAI(e){return e?.overrideTeacherAI||this.teacherAI||this.studentAI}async executeWithTeacher(e,t=!0,n){let r=this.getAIService(t,n);return await e(r)}async*compileStream(e,t,n,r){let o=Date.now(),s=this.constructor.name,a=e.getSignature().toString();this.recordOptimizationStart(s,a,r);let l,u=(g,h,f,C,x,I,w,A={},T)=>{this.getOptimizerLogger(T)?.({name:"RoundProgress",value:{round:g,totalRounds:T?.maxIterations??0,currentScore:h,bestScore:I,configuration:f}}),this.updateOptimizationProgress(g,h,f,C,x,I,w,A,T)},c=(g,h)=>{l=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),u(g.round,g.currentScore,g.currentConfiguration||{},s,{},g.bestScore,g.bestConfiguration,g.convergenceInfo,r)},d=await this.compile(e,t,n,{...r,overrideOnProgress:p,overrideOnEarlyStop:c}),m=Date.now()-o;return this.recordOptimizationComplete(m,!0,s,a,r),l&&this.getLogger(r)?.({name:"Notification",id:"optimization_early_stop",value:`Optimization stopped early due to ${l}`}),{demos:d.demos,stats:d.stats,bestScore:d.bestScore,finalConfiguration:d.finalConfiguration,scoreHistory:d.scoreHistory,configurationHistory:d.configurationHistory}}async compilePareto(e,t,n,r){let o=this.constructor.name,s=Date.now(),a=await this.generateWeightedSolutions(e,t,n,r),l=await this.generateConstraintSolutions(e,t,n,r),u=[...a,...l],c=this.findParetoFrontier(u),p=this.calculateHypervolume(c);this.updateResourceUsage(s),this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(c.length,u.length,"base_optimizer",p,r);let d=c.length>0?Math.max(...c.map(m=>Math.max(...Object.values(m.scores)))):0;return{demos:c.length>0?[...c[0].demos]:void 0,stats:this.stats,bestScore:d,paretoFront:c,hypervolume:p,paretoFrontSize:c.length,finalConfiguration:{paretoFrontSize:c.length,hypervolume:p,strategy:"weighted_combinations_and_constraints",numSolutions:u.length}}}async generateWeightedSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for Pareto optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l),c=this.generateWeightCombinations(u);for(let p=0;p<c.length;p++){let d=c[p],m=async({prediction:g,example:h})=>{let f=await n({prediction:g,example:h}),C=0;for(let[x,I]of Object.entries(f))C+=I*(d[x]||0);return C};try{let g=await this.compile(e,t,m,{...r,verbose:!1}),h=await this.evaluateWithMultiObjective(e,g,n,t);o.push({scores:h,demos:g.demos,configuration:{...g.finalConfiguration,weights:d,strategy:"weighted_combination"}})}catch{}}return o}async generateConstraintSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for multi-objective optimization");let s=t[0],a=await e.forward(this.getAIService(!1,r),s),l=await n({prediction:a,example:s}),u=Object.keys(l);for(let c of u){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),h=g[c]||0,f=0;for(let[C,x]of Object.entries(g))C!==c&&x<.3&&(f+=(.3-x)*2);return h-f};try{let d=await this.compile(e,t,p,{...r,verbose:!1}),m=await this.evaluateWithMultiObjective(e,d,n,t);o.push({scores:m,demos:d.demos,configuration:{...d.finalConfiguration,primaryObjective:c,strategy:"constraint_based"}})}catch{}}return o}generateWeightCombinations(e){let t=[];for(let r of e){let o={};for(let s of e)o[s]=s===r?1:0;t.push(o)}let n={};for(let r of e)n[r]=1/e.length;if(t.push(n),e.length===2){let[r,o]=e;for(let s=.1;s<=.9;s+=.2){let a=1-s;t.push({[r]:s,[o]:a})}}if(e.length===3){let[r,o,s]=e;t.push({[r]:.5,[o]:.3,[s]:.2},{[r]:.3,[o]:.5,[s]:.2},{[r]:.2,[o]:.3,[s]:.5})}return t}async evaluateWithMultiObjective(e,t,n,r){let o=new Ee(e.getSignature());t.demos&&o.setDemos(t.demos);let s=[],a=Math.max(1,Math.min(5,Math.floor(r.length*.2))),l=r.slice(-a),u={},c=l;for(let d of c)try{let m=await o.forward(this.studentAI,d),g=await n({prediction:m,example:d});for(let[h,f]of Object.entries(g))u[h]||(u[h]=[]),u[h].push(f)}catch{}let p={};for(let[d,m]of Object.entries(u))p[d]=m.length>0?m.reduce((g,h)=>g+h,0)/m.length:0;return p}findParetoFrontier(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n],o=!1,s=0;for(let a=0;a<e.length;a++){if(n===a)continue;let l=e[a];if(this.dominates(l.scores,r.scores)){o=!0;break}this.dominates(r.scores,l.scores)&&s++}o||t.push({demos:r.demos||[],scores:r.scores,configuration:r.configuration,dominatedSolutions:s})}return t}dominates(e,t){let n=Object.keys(e),r=!0,o=!1;for(let s of n){let a=e[s]||0,l=t[s]||0;if(a<l){r=!1;break}a>l&&(o=!0)}return r&&o}calculateHypervolume(e){if(e.length===0)return;let t=e[0],n=Object.keys(t.scores);if(n.length===2){let[r,o]=n,s=0,a=[...e].sort((u,c)=>(c.scores[r]||0)-(u.scores[r]||0)),l=0;for(let u of a){let c=u.scores[r]||0,p=u.scores[o]||0;s+=c*(p-l),l=Math.max(l,p)}return s}}async saveCheckpoint(e,t,n,r,o={},s){let a=s?.overrideCheckpointSave||this.checkpointSave;if(!a)return;let l=Date.now(),u=!1,c;try{let p={version:"1.0.0",timestamp:Date.now(),optimizerType:e,optimizerConfig:t,currentRound:this.currentRound,totalRounds:this.stats.resourceUsage.totalTime>0?this.currentRound:0,bestScore:n,bestConfiguration:r,scoreHistory:[...this.scoreHistory],configurationHistory:[...this.configurationHistory],stats:{...this.stats},optimizerState:o,examples:[]};c=await a(p),u=!0}catch(p){throw u=!1,p}finally{let p=Date.now()-l;this.recordCheckpointMetrics("save",p,u,e,s)}return c}async loadCheckpoint(e,t){let n=t?.overrideCheckpointLoad||this.checkpointLoad;if(!n)return null;let r=Date.now(),o=!1,s=null;try{s=await n(e),o=s!==null}catch(a){throw o=!1,a}finally{let a=Date.now()-r;this.recordCheckpointMetrics("load",a,o,"unknown",t)}return s}restoreFromCheckpoint(e){this.currentRound=e.currentRound,this.scoreHistory=[...e.scoreHistory],this.configurationHistory=[...e.configurationHistory],this.stats={...e.stats}}shouldSaveCheckpoint(e,t){let n=t?.overrideCheckpointInterval||this.checkpointInterval;return n!==void 0&&e%n===0}async updateOptimizationProgress(e,t,n,r,o,s,a,l={},u){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,u)&&await this.saveCheckpoint(r,o,s,a,l,u),this.getOptimizerLogger(u)?.({name:"RoundProgress",value:{round:e,totalRounds:u?.maxIterations??0,currentScore:t,bestScore:s,configuration:n}})}async saveFinalCheckpoint(e,t,n,r,o={},s){s?.saveCheckpointOnComplete!==!1&&await this.saveCheckpoint(e,t,n,r,{...o,final:!0},s)}getLogger(e){if(this.isLoggingEnabled(e))return this.logger?this.logger:this.studentAI.getLogger()}isLoggingEnabled(e){return e?.verbose!==void 0?e.verbose:this.verbose??!0}recordOptimizationStart(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);if(t){let o=(t.match(/input:/g)||[]).length,s=(t.match(/output:/g)||[]).length;Bp(this.metricsInstruments,o,s,0,0,e,r)}qp(this.metricsInstruments,e,this.targetScore,void 0,r)}recordOptimizationComplete(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);Fp(this.metricsInstruments,e,t,n,r,s),Lp(this.metricsInstruments,e,n,s);let a=this.costTracker?.getCurrentCost()??0,l=this.costTracker?.getTotalTokens()??0;$p(this.metricsInstruments,l,a,n,void 0,s)}recordConvergenceMetrics(e,t,n,r,o,s){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(s);_p(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);Np(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);Dp(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);Gp(this.metricsInstruments,e,t,n,r,s)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let s=this.getMergedCustomLabels(o);Up(this.metricsInstruments,e,t,n,r,s)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);jp(this.metricsInstruments,e,t,n,o)}isOptimizerLoggingEnabled(e){return this.debugOptimizer||(e?.verbose??this.verbose??!1)}getOptimizerLogger(e){if(this.isOptimizerLoggingEnabled(e))return this.optimizerLogger??me.optimizerLogger??Pr}getStats(){return{...this.stats}}async explainOptimizationResults(e,t,n){let r=this.stats.convergenceInfo.converged,o=this.stats.totalCalls,s=o>0?this.stats.successfulDemos/o*100:0,a=`Optimization finished with best score ${e.toFixed(3)}${t?` using configuration ${JSON.stringify(t)}`:""}. Convergence: ${r?"yes":"no"}. Success rate: ${s.toFixed(1)}%.`,l=[];if(r||l.push("Increase numTrials or relax earlyStoppingTrials to allow further improvement."),typeof this.targetScore=="number"&&e<this.targetScore&&l.push("Tighten the metric or supply more/better-labeled examples to reach targetScore."),t&&"bootstrappedDemos"in t){let c=t.bootstrappedDemos;typeof c=="number"&&c===0&&l.push("Consider allowing a small number of bootstrapped demos to boost performance.")}l.length===0&&l.push("Re-run with more trials or different acquisition settings to explore more of the space.");let u=`Tokens used: ${this.stats.resourceUsage.totalTokens}, rounds: ${this.currentRound}, stagnationRounds: ${this.stats.convergenceInfo.stagnationRounds}.`;return{humanExplanation:a,recommendations:l,performanceAssessment:u}}async logOptimizationComplete(e,t,n,r,o){let s=this.getOptimizerLogger(r);s&&s(o?{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",explanation:o.humanExplanation,recommendations:o.recommendations,performanceAssessment:o.performanceAssessment,stats:this.stats}}:{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",stats:this.stats}})}reset(){this.stats=this.initializeStats(),this.costTracker?.reset(),this.currentRound=0,this.scoreHistory=[],this.configurationHistory=[]}};function Cu(i){return Ge.create(i)}function Ye(i,e){let t=typeof i=="string"?Ge.create(i):i;return new Ee(t,e)}function Tu(i,e,t=0){let n=new Set([...Object.keys(i),...Object.keys(e)]),r=!0,o=!1;for(let s of n){let a=i[s]??0,l=e[s]??0;if(a+t<l){r=!1;break}a>l+t&&(o=!0)}return r&&o}function vt(i,e=0){let t=[];for(let n=0;n<i.length;n++){let r=0,o=!1;for(let s=0;s<i.length;s++)if(n!==s){if(Tu(i[s].scores,i[n].scores,e)){o=!0;break}Tu(i[n].scores,i[s].scores,e)&&r++}o||t.push({idx:i[n].idx,scores:i[n].scores,dominated:r})}return t}function gt(i){if(i.length===0)return;let e=Object.keys(i[0]??{});if(e.length!==2)return;let[t,n]=e,r=[...i].sort((a,l)=>(l[t]??0)-(a[t]??0)),o=0,s=0;for(let a of r){let l=a[t]??0,u=a[n]??0,c=Math.max(u-s,0);o+=l*c,s=Math.max(s,u)}return o}function Qt(i){if(i.length===0)return 0;let e=0;for(let t of i)e+=t;return e/i.length}function ns(i){let e={},t={};for(let r of i)for(let[o,s]of Object.entries(r))e[o]=(e[o]||0)+(typeof s=="number"?s:0),t[o]=(t[o]||0)+1;let n={};for(let[r,o]of Object.entries(e))n[r]=o/Math.max(t[r]||1,1);return n}function _r(i,e){let t=new Set;for(let c of i)for(let p of c)t.add(p);let r=[...Array.from(t)].sort((c,p)=>(e[c]??0)-(e[p]??0)),o=new Set,s=(c,p)=>{for(let d of i){if(!d.has(c))continue;let m=!1;for(let g of p)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let c of r){if(o.has(c))continue;let p=new Set(r.filter(d=>d!==c&&!o.has(d)));if(s(c,p)){o.add(c),a=!0;break}}}let l=r.filter(c=>!o.has(c)),u=new Set(l);return i.map(c=>{let p=new Set;for(let d of c)u.has(d)&&p.add(d);return p})}function Nr(i,e,t){let n=_r(i,e),r={};for(let l of n)for(let u of l)r[u]=(r[u]||0)+1;let o=[];for(let[l,u]of Object.entries(r)){let c=Number(l);for(let p=0;p<u;p++)o.push(c)}if(o.length===0)return 0;let s=typeof t=="function"?t():Math.random(),a=Math.floor(s*o.length);return o[a]}var qn=class i extends rt{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;paretoSetSize;crossoverEvery;tieEpsilon;feedbackMemorySize;feedbackMemory=[];mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;static REFLECTION_PROMPT_TEMPLATE="I provided an assistant with the following instructions to perform a task for me:\n```\n<curr_instructions>\n```\n\nThe following are examples of different task inputs provided to the assistant along with the assistant's response for each of them, and some feedback on how the assistant's response could be better:\n```\n<inputs_outputs_feedback>\n```\n\nYour task is to write a new instruction for the assistant. Read the inputs carefully and identify the input format and infer detailed task description about the task I wish to solve with the assistant. Read all the assistant responses and the corresponding feedback. Identify all niche and domain specific factual information about the task and include it in the instruction, as a lot of it may not be available to the assistant in the future. The assistant may have utilized a generalizable strategy to solve the task, if so, include that in the instruction as well. Provide the new instructions within ``` blocks.";rngState=123456789;samplerState={epoch:-1,shuffled:[],freq:new Map};localScoreHistory=[];localConfigurationHistory=[];constructor(e){super(e);let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??20,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.crossoverEvery;this.crossoverEvery=Math.max(0,Math.floor(o??Math.max(3,Math.floor(this.numTrials/4))));let s=e?.tieEpsilon;this.tieEpsilon=Number.isFinite(s)?s:0;let a=e?.feedbackMemorySize;this.feedbackMemorySize=Math.max(0,Math.floor(a??4));let l=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(l??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.localScoreHistory=[],this.localConfigurationHistory=[],this.feedbackMemory=[],this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}async compile(e,t,n,r){let o=Date.now();this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let s=r?.validationExamples,a=r?.feedbackExamples,l=(s&&s.length>0?s:t).slice(0,this.paretoSetSize),u=a&&a.length>0?a:t,c=async(P,D)=>{try{e.setInstruction?.(P);let K=await e.forward(this.studentAI,D,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:K,example:D})||{}}catch{return{}}},p=async(P,D)=>{let K=[];for(let N of D)K.push(await c(P,N));return ns(K)},d=await this.getBaseInstruction(e),m=[{instruction:d,parent:void 0,scores:await p(d,l)}],g=P=>{let D=r?.paretoMetricKey,K=r?.paretoScalarize;if(typeof K=="function")return K(P);if(D)return Number.isFinite(P[D])?P[D]:0;let N=Object.values(P);return N.length?N.reduce((J,ye)=>J+ye,0)/N.length:0},h=[],f=async(P,D)=>{let K=[];for(let N of D){let J=await c(P,N);K.push(g(J))}return K};h.push(await f(d,l));let C=()=>{let P=h[0]?.length??0,D=[];for(let N=0;N<P;N++){let J=Number.NEGATIVE_INFINITY,ye=new Set;for(let oe=0;oe<h.length;oe++){let re=h[oe][N];re>J+this.tieEpsilon?(J=re,ye.clear(),ye.add(oe)):Math.abs(re-J)<=this.tieEpsilon&&ye.add(oe)}D.push(ye)}let K=h.map(N=>Qt(N));return Nr(D,K)},x=r?.maxMetricCalls;if(!Number.isFinite(x)||x<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let I=Math.floor(x),w=this.getOptimizerLogger(r),A=r?.verbose??this.verbose?P=>console.log(`[GEPA] ${P}`):P=>{};w?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:l.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax}}}),A(`Starting GEPA optimization: ${t.length} train, ${l.length} validation, maxCalls=${I}`);let T=0,k=vt(m.map((P,D)=>({idx:D,scores:P.scores})),this.tieEpsilon).map(P=>P.idx),E;for(let P=0;P<this.numTrials&&!(I!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(I)));P++){let D=h[0]?.length??0,K=[];for(let R=0;R<D;R++){let y=Number.NEGATIVE_INFINITY,_=new Set;for(let q=0;q<h.length;q++){let L=h[q][R];L>y+this.tieEpsilon?(y=L,_.clear(),_.add(q)):Math.abs(L-y)<=this.tieEpsilon&&_.add(q)}K.push(_)}let N=h.map(R=>Qt(R));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let R=U=>{let H=[],Y=U;for(;Y!==void 0;)H.push(Y),Y=m[Y]?.parent;return H},y=U=>U.length?U[Math.floor(this.rand()*U.length)]:void 0,_=_r(K,N),q=new Set;for(let U of _)for(let H of U)q.add(H);let L=Array.from(q),G;for(let U=0;U<10&&!G&&!(L.length<2);U++){let H=y(L),Y=y(L);if(H===Y)continue;Y<H&&([H,Y]=[Y,H]);let ne=new Set(R(H)),ee=new Set(R(Y));if(ne.has(Y)||ee.has(H))continue;let pe=[...ne].filter(Pe=>ee.has(Pe));if(pe.length===0)continue;let fe=pe.map(Pe=>Math.max(1e-9,N[Pe])),Ie=this.rand()*fe.reduce((Pe,Q)=>Pe+Q,0),Se=pe[pe.length-1];for(let Pe=0;Pe<pe.length;Pe++){if(Ie<fe[Pe]){Se=pe[Pe];break}Ie-=fe[Pe]}G={i:H,j:Y,a:Se}}if(this.lastIterFoundNewProgram=!1,G){let{i:U,j:H,a:Y}=G,ne=N[Y],ee=N[U],pe=N[H],fe=m[Y].instruction,Ie=m[U].instruction,Se=m[H].instruction,Pe=Ie===fe&&Se!==Ie||Se===fe&&Ie!==Se,Q=ne<=Math.min(ee,pe)&&Pe,X="",ue="i",de=!1;if(Q){let Re=`${U}|${H}|${Y}`;if(this.mergeAttemptKeys.has(Re))Q=!1;else{Ie===fe&&Se!==Ie?(X=Se,ue="j"):Se===fe&&Ie!==Se?(X=Ie,ue="i"):Ie!==fe&&Se!==fe&&Ie!==Se?ee>pe||ee===pe&&this.rand()<.5?(X=Ie,ue="i"):(X=Se,ue="j"):(X=Ie,ue="i");let Xe=`${Math.min(U,H)}|${Math.max(U,H)}|${ue}`;if(this.mergeCompositionKeys.has(Xe))Q=!1;else{this.mergeAttemptKeys.add(Re),this.mergeCompositionKeys.add(Xe);let _e=h[U],Ze=h[H],ft=Array.from({length:_e.length},(Ae,ze)=>ze),it=ft.filter(Ae=>(_e[Ae]??0)>(Ze[Ae]??0)),xt=ft.filter(Ae=>(Ze[Ae]??0)>(_e[Ae]??0)),Ot=ft.filter(Ae=>!(it.includes(Ae)||xt.includes(Ae))),v=5,z=Math.ceil(v/3),Ce=(Ae,ze)=>{if(ze<=0||Ae.length===0)return[];if(Ae.length<=ze)return[...Ae];let Gt=[],en=new Set;for(;Gt.length<ze;){let We=Math.floor(this.rand()*Ae.length);en.has(We)||(en.add(We),Gt.push(Ae[We]))}return Gt},ke=[];ke.push(...Ce(it,Math.min(z,it.length))),ke.push(...Ce(xt,Math.min(z,xt.length)));let ae=v-ke.length;ke.push(...Ce(Ot,Math.max(0,ae)));let Ne=v-ke.length;if(Ne>0){let Ae=ft.filter(ze=>!ke.includes(ze));ke.push(...Ce(Ae,Math.min(Ne,Ae.length)))}let Fe=ke.slice(0,Math.min(v,ft.length)),Mt=Fe.map(Ae=>l[Ae]);de=!0;let At=(await f(X,Mt)).reduce((Ae,ze)=>Ae+ze,0),Zt=Fe.reduce((Ae,ze)=>Ae+(_e[ze]??0),0),et=Fe.reduce((Ae,ze)=>Ae+(Ze[ze]??0),0);if(At>=Math.max(Zt,et)+this.tieEpsilon){A(`Iteration ${P+1}: Merge accepted (programs ${U} + ${H} via ancestor ${Y})`);let Ae=await p(X,l);m.push({instruction:X,parent:Y,scores:Ae}),h.push(await f(X,l));let ze=k.length,Gt=gt(k.map(We=>m[We].scores))??0;k=vt(m.map((We,Ut)=>({idx:Ut,scores:We.scores})),this.tieEpsilon).map(We=>We.idx);let en=gt(k.map(We=>m[We].scores))??0;(k.length>ze||en>Gt+1e-6)&&(T=0),this.mergesDue-=1,this.totalMergesTested+=1}}}}if(de)continue}}let J=Nr(K,N,()=>this.rand()),ye=this.minibatch?this.nextMinibatchIndices(u.length,P).map(R=>u[R]):u;if(r?.skipPerfectScore??!0){let R=Number(r?.perfectScore??1),y=await f(m[J].instruction,ye);if(y.length>0&&y.every(_=>_>=R))continue}let oe=!1,re=m[J].instruction,le="reflective_mutation",we,Ue;if(oe){let R=(J+1)%m.length;re=await this.mergeInstructions(m[J].instruction,m[R].instruction,r),le="merge",this.mergesUsed+=1}else{let R=r?.gepaAdapter;if(R){try{let y={instruction:m[J].instruction},_=await R.evaluate(ye,y,!0);we=Array.isArray(_?.scores)?_.scores.reduce((U,H)=>U+(Number(H)||0),0):void 0;let q=R.make_reflective_dataset(y,_,["instruction"]),L=await R.propose_new_texts?.(y,q,["instruction"]),G=L?.instruction??(L?Object.values(L)[0]:void 0);typeof G=="string"&&G.length>0?re=G:re=await this.reflectInstruction(m[J].instruction,e,ye,async({prediction:U,example:H})=>{let Y=await n({prediction:U,example:H}),ne=Object.values(Y||{});return ne.length?ne.reduce((ee,pe)=>ee+pe,0)/ne.length:0},r)}catch{re=await this.reflectInstruction(m[J].instruction,e,ye,async({prediction:y,example:_})=>{let q=await n({prediction:y,example:_}),L=Object.values(q||{});return L.length?L.reduce((G,U)=>G+U,0)/L.length:0},r)}if(we!==void 0)try{let y=await R.evaluate(ye,{instruction:re},!1);Ue=Array.isArray(y?.scores)?y.scores.reduce((_,q)=>_+(Number(q)||0),0):void 0}catch{}}else re=await this.reflectInstruction(m[J].instruction,e,ye,async({prediction:y,example:_})=>{let q=await n({prediction:y,example:_}),L=Object.values(q||{});return L.length?L.reduce((G,U)=>G+U,0)/L.length:0},r)}let se=await f(m[J].instruction,ye),ve=await f(re,ye),ie=se.reduce((R,y)=>R+y,0),ce=ve.reduce((R,y)=>R+y,0);if(this.currentRound=P+1,await this.updateOptimizationProgress(this.currentRound,ce,{instructionLen:re.length,parent:J,totalRounds:this.numTrials},"GEPA",{strategy:le,paretoSetSize:l.length},ce,{instructionLen:m[J].instruction.length,idx:J},{...r??{},maxIterations:this.numTrials}),!(ce>ie+this.tieEpsilon&&(we===void 0||Ue===void 0||Ue>we+this.tieEpsilon))){if(A(`Iteration ${P+1}: Rejected (child=${ce.toFixed(3)} <= parent=${ie.toFixed(3)})`),++T>=this.earlyStoppingTrials){A(`Early stopping: ${T} iterations without improvement`);break}continue}A(`Iteration ${P+1}: Accepted (child=${ce.toFixed(3)} > parent=${ie.toFixed(3)})`);let Z=await p(re,l);m.push({instruction:re,parent:J,scores:Z}),h.push(await f(re,l));let Oe=k.length,b=gt(k.map(R=>m[R].scores))??0;k=vt(m.map((R,y)=>({idx:y,scores:R.scores})),this.tieEpsilon).map(R=>R.idx);let S=gt(k.map(R=>m[R].scores))??0;if(k.length>Oe||S>b+1e-6)T=0,A(`Iteration ${P+1}: Archive improved (size=${k.length}, hv=${S.toFixed(4)})`);else if(T++,A(`Iteration ${P+1}: Archive unchanged (stagnation=${T}/${this.earlyStoppingTrials})`),T>=this.earlyStoppingTrials){A(`Early stopping: ${T} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let M=vt(m.map((P,D)=>({idx:D,scores:P.scores})),this.tieEpsilon),$=M.length>0?Math.max(...M.map(P=>g(P.scores))):0,B;if(M.length>0){let P=Number.NEGATIVE_INFINITY;for(let D of M){let K=g(D.scores);K>P&&(P=K,B=D.idx)}}let V=gt(M.map(P=>P.scores));this.stats.convergenceInfo.converged=!0;let O=this.getMergedCustomLabels(r);this.recordParetoMetrics(M.length,m.length,"GEPA",V,O);let j=Date.now()-o,F=typeof B=="number"?new kt({bestScore:$,stats:this.stats,instruction:m[B].instruction,demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:j,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,W=this.generateOptimizationReport(M,V,$);return{demos:[],stats:this.stats,bestScore:$,paretoFront:M.map(P=>({demos:[],scores:P.scores,configuration:{candidate:P.idx,instruction:m[P.idx].instruction},dominatedSolutions:P.dominated})),paretoFrontSize:M.length,hypervolume:V,finalConfiguration:{strategy:"gepa",candidates:m.length},optimizedProgram:F,report:W}}configureAuto(e){switch(e){case"light":this.numTrials=10,this.minibatch=!0,this.minibatchSize=15;break;case"medium":this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numTrials=35,this.minibatch=!0,this.minibatchSize=35;break}}async getBaseInstruction(e){let t=e.getInstruction?.();if(t&&t.length>0)return t;let r=e.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?Qt(o):0}async evaluateOne(e,t,n,r){try{e.setInstruction?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});if(typeof s=="number"&&!Number.isNaN(s)){let a=typeof this.targetScore=="number"?this.targetScore:.5;return s>=a&&(this.stats.successfulDemos+=1),s}return 0}catch(o){return this.getLogger()?.({name:"Notification",id:"gepa_eval",value:String(o)}),0}}async reflectInstruction(e,t,n,r,o,s){let a=s??[];if(a.length===0)for(let m of n)try{t.setInstruction?.(e);let g=await t.forward(this.studentAI,m,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let h=await r({prediction:g,example:m});a.push({input:m,prediction:g,score:typeof h=="number"?h:0})}catch{a.push({input:m,prediction:{},score:0})}let l=o?.overrideTeacherAI??this.teacherAI??this.studentAI,u=o?.feedbackFn,c=()=>{let m=[];for(let g=0;g<a.length;g++){let h=a[g],f=`# Example ${g+1}
535
- `;if(f+=`## Inputs
536
- `,typeof h.input=="object"&&h.input!==null)for(let[x,I]of Object.entries(h.input))f+=`### ${x}
541
+ ${e}`}s(n)}},Or=Ni();var Li={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},Xo,md=s=>{if(Xo)return Xo;if(s)return Xo=gd(s),Xo};var Zo=Li,_l=s=>{Zo={...Zo,...s}},Nl=()=>({...Zo}),gd=s=>({optimizationLatencyHistogram:s.createHistogram("ax_optimizer_optimization_duration_ms",{description:"End-to-end duration of optimization runs",unit:"ms"}),optimizationRequestsCounter:s.createCounter("ax_optimizer_optimization_requests_total",{description:"Total number of optimization requests"}),optimizationErrorsCounter:s.createCounter("ax_optimizer_optimization_errors_total",{description:"Total number of failed optimizations"}),convergenceRoundsHistogram:s.createHistogram("ax_optimizer_convergence_rounds",{description:"Number of rounds until convergence"}),convergenceScoreGauge:s.createGauge("ax_optimizer_convergence_score",{description:"Current best score during optimization"}),convergenceImprovementGauge:s.createGauge("ax_optimizer_convergence_improvement",{description:"Improvement in score from baseline"}),stagnationRoundsGauge:s.createGauge("ax_optimizer_stagnation_rounds",{description:"Number of rounds without improvement"}),earlyStoppingCounter:s.createCounter("ax_optimizer_early_stopping_total",{description:"Total number of early stopping events"}),tokenUsageCounter:s.createCounter("ax_optimizer_token_usage_total",{description:"Total tokens used during optimization"}),costUsageCounter:s.createCounter("ax_optimizer_cost_usage_total",{description:"Total cost incurred during optimization",unit:"$"}),memoryUsageGauge:s.createGauge("ax_optimizer_memory_usage_bytes",{description:"Peak memory usage during optimization",unit:"By"}),optimizationDurationHistogram:s.createHistogram("ax_optimizer_duration_ms",{description:"Duration of optimization runs",unit:"ms"}),teacherStudentUsageCounter:s.createCounter("ax_optimizer_teacher_student_usage_total",{description:"Total number of teacher-student interactions"}),teacherStudentLatencyHistogram:s.createHistogram("ax_optimizer_teacher_student_latency_ms",{description:"Latency of teacher-student interactions",unit:"ms"}),teacherStudentScoreImprovementGauge:s.createGauge("ax_optimizer_teacher_student_score_improvement",{description:"Score improvement from teacher-student interactions"}),checkpointSaveCounter:s.createCounter("ax_optimizer_checkpoint_save_total",{description:"Total number of checkpoint saves"}),checkpointLoadCounter:s.createCounter("ax_optimizer_checkpoint_load_total",{description:"Total number of checkpoint loads"}),checkpointSaveLatencyHistogram:s.createHistogram("ax_optimizer_checkpoint_save_latency_ms",{description:"Latency of checkpoint save operations",unit:"ms"}),checkpointLoadLatencyHistogram:s.createHistogram("ax_optimizer_checkpoint_load_latency_ms",{description:"Latency of checkpoint load operations",unit:"ms"}),paretoOptimizationsCounter:s.createCounter("ax_optimizer_pareto_optimizations_total",{description:"Total number of Pareto optimizations"}),paretoFrontSizeHistogram:s.createHistogram("ax_optimizer_pareto_front_size",{description:"Size of Pareto frontier"}),paretoHypervolumeGauge:s.createGauge("ax_optimizer_pareto_hypervolume",{description:"Hypervolume of Pareto frontier"}),paretoSolutionsGeneratedHistogram:s.createHistogram("ax_optimizer_pareto_solutions_generated",{description:"Number of solutions generated for Pareto optimization"}),programInputFieldsGauge:s.createGauge("ax_optimizer_program_input_fields",{description:"Number of input fields in optimized program"}),programOutputFieldsGauge:s.createGauge("ax_optimizer_program_output_fields",{description:"Number of output fields in optimized program"}),examplesCountGauge:s.createGauge("ax_optimizer_examples_count",{description:"Number of training examples used"}),validationSetSizeGauge:s.createGauge("ax_optimizer_validation_set_size",{description:"Size of validation set used"}),evaluationLatencyHistogram:s.createHistogram("ax_optimizer_evaluation_latency_ms",{description:"Latency of program evaluations",unit:"ms"}),demoGenerationLatencyHistogram:s.createHistogram("ax_optimizer_demo_generation_latency_ms",{description:"Latency of demo generation",unit:"ms"}),metricComputationLatencyHistogram:s.createHistogram("ax_optimizer_metric_computation_latency_ms",{description:"Latency of metric computation",unit:"ms"}),optimizerTypeGauge:s.createGauge("ax_optimizer_type",{description:"Type of optimizer being used"}),targetScoreGauge:s.createGauge("ax_optimizer_target_score",{description:"Target score for optimization"}),maxRoundsGauge:s.createGauge("ax_optimizer_max_rounds",{description:"Maximum rounds for optimization"})}),ut=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=Zo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},fd=(s,e,t,n,r,o)=>{try{let i=ut({success:t.toString(),optimizer_type:n,...r?{program_signature:r}:{},...o});s.optimizationLatencyHistogram&&s.optimizationLatencyHistogram.record(e,i),s.optimizationRequestsCounter&&s.optimizationRequestsCounter.add(1,i),!t&&s.optimizationErrorsCounter&&s.optimizationErrorsCounter.add(1,i)}catch(i){console.warn("Failed to record optimization metric:",i)}},hd=(s,e,t,n,r,o,i)=>{try{let a=ut({optimizer_type:o,...i});s.convergenceRoundsHistogram&&s.convergenceRoundsHistogram.record(e,a),s.convergenceScoreGauge&&s.convergenceScoreGauge.record(t,a),s.convergenceImprovementGauge&&s.convergenceImprovementGauge.record(n,a),s.stagnationRoundsGauge&&s.stagnationRoundsGauge.record(r,a)}catch(a){console.warn("Failed to record convergence metric:",a)}},xd=(s,e,t,n)=>{try{let r=ut({reason:e,optimizer_type:t,...n});s.earlyStoppingCounter&&s.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},Ad=(s,e,t,n,r,o)=>{try{let i=ut({optimizer_type:n,...o});s.tokenUsageCounter&&s.tokenUsageCounter.add(e,i),s.costUsageCounter&&s.costUsageCounter.add(t,i),r!==void 0&&s.memoryUsageGauge&&s.memoryUsageGauge.record(r,i)}catch(i){console.warn("Failed to record resource usage metric:",i)}},yd=(s,e,t,n)=>{try{let r=ut({optimizer_type:t,...n});s.optimizationDurationHistogram&&s.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},bd=(s,e,t,n,r)=>{try{let o=ut({optimizer_type:n,...r});s.teacherStudentUsageCounter&&s.teacherStudentUsageCounter.add(1,o),s.teacherStudentLatencyHistogram&&s.teacherStudentLatencyHistogram.record(e,o),s.teacherStudentScoreImprovementGauge&&s.teacherStudentScoreImprovementGauge.record(t,o)}catch(o){console.warn("Failed to record teacher-student metric:",o)}},Id=(s,e,t,n,r,o)=>{try{let i=ut({operation:e,success:n.toString(),optimizer_type:r,...o});e==="save"?(s.checkpointSaveCounter&&s.checkpointSaveCounter.add(1,i),s.checkpointSaveLatencyHistogram&&s.checkpointSaveLatencyHistogram.record(t,i)):(s.checkpointLoadCounter&&s.checkpointLoadCounter.add(1,i),s.checkpointLoadLatencyHistogram&&s.checkpointLoadLatencyHistogram.record(t,i))}catch(i){console.warn("Failed to record checkpoint metric:",i)}},Cd=(s,e,t,n,r,o)=>{try{let i=ut({optimizer_type:n,...o});s.paretoOptimizationsCounter&&s.paretoOptimizationsCounter.add(1,i),s.paretoFrontSizeHistogram&&s.paretoFrontSizeHistogram.record(e,i),r!==void 0&&s.paretoHypervolumeGauge&&s.paretoHypervolumeGauge.record(r,i),s.paretoSolutionsGeneratedHistogram&&s.paretoSolutionsGeneratedHistogram.record(t,i)}catch(i){console.warn("Failed to record Pareto metric:",i)}},Td=(s,e,t,n,r,o,i)=>{try{let a=ut({optimizer_type:o,...i});s.programInputFieldsGauge&&s.programInputFieldsGauge.record(e,a),s.programOutputFieldsGauge&&s.programOutputFieldsGauge.record(t,a),s.examplesCountGauge&&s.examplesCountGauge.record(n,a),s.validationSetSizeGauge&&s.validationSetSizeGauge.record(r,a)}catch(a){console.warn("Failed to record program complexity metric:",a)}},wd=(s,e,t,n,r)=>{try{let o=ut({metric_type:e,optimizer_type:n,...r});switch(e){case"evaluation":s.evaluationLatencyHistogram&&s.evaluationLatencyHistogram.record(t,o);break;case"demo_generation":s.demoGenerationLatencyHistogram&&s.demoGenerationLatencyHistogram.record(t,o);break;case"metric_computation":s.metricComputationLatencyHistogram&&s.metricComputationLatencyHistogram.record(t,o);break}}catch(o){console.warn("Failed to record optimizer performance metric:",o)}},Rd=(s,e,t,n,r)=>{try{let o=ut({optimizer_type:e,...r});s.optimizerTypeGauge&&s.optimizerTypeGauge.record(1,o),t!==void 0&&s.targetScoreGauge&&s.targetScoreGauge.record(t,o),n!==void 0&&s.maxRoundsGauge&&s.maxRoundsGauge.record(n,o)}catch(o){console.warn("Failed to record optimizer configuration metric:",o)}},mt=class{bestScore;stats;instruction;instructionMap;demos;examples;modelConfig;optimizerType;optimizationTime;totalRounds;converged;scoreHistory;configurationHistory;constructor(e){this.bestScore=e.bestScore,this.stats=e.stats,this.instruction=e.instruction,this.instructionMap=e.instructionMap,this.demos=e.demos,this.examples=e.examples,this.modelConfig=e.modelConfig,this.optimizerType=e.optimizerType,this.optimizationTime=e.optimizationTime,this.totalRounds=e.totalRounds,this.converged=e.converged,this.scoreHistory=e.scoreHistory,this.configurationHistory=e.configurationHistory}applyTo(e){e.applyOptimization?.(this)}},Er=class{tokenUsage={};totalTokens=0;costPerModel;maxCost;maxTokens;constructor(e){this.costPerModel=e?.costPerModel??{},this.maxCost=e?.maxCost,this.maxTokens=e?.maxTokens}trackTokens(e,t){this.tokenUsage[t]=(this.tokenUsage[t]||0)+e,this.totalTokens+=e}getCurrentCost(){let e=0;for(let[t,n]of Object.entries(this.tokenUsage)){let r=this.costPerModel[t]||.001;e+=n/1e3*r}return e}getTokenUsage(){return{...this.tokenUsage}}getTotalTokens(){return this.totalTokens}isLimitReached(){return this.maxTokens!==void 0&&this.totalTokens>=this.maxTokens||this.maxCost!==void 0&&this.getCurrentCost()>=this.maxCost}reset(){this.tokenUsage={},this.totalTokens=0}},et=class{studentAI;teacherAI;targetScore;minSuccessRate;onProgress;onEarlyStop;costTracker;seed;checkpointSave;checkpointLoad;checkpointInterval;resumeFromCheckpoint;logger;verbose;debugOptimizer;optimizerLogger;currentRound=0;scoreHistory=[];configurationHistory=[];stats;metricsInstruments;resultExplainer;constructor(e){this.studentAI=e.studentAI,this.teacherAI=e.teacherAI,this.targetScore=e.targetScore,this.minSuccessRate=e.minSuccessRate,this.onProgress=e.onProgress,this.onEarlyStop=e.onEarlyStop,this.seed=e.seed,this.checkpointSave=e.checkpointSave,this.checkpointLoad=e.checkpointLoad,this.checkpointInterval=e.checkpointInterval??10,this.resumeFromCheckpoint=e.resumeFromCheckpoint,this.logger=e.logger,this.verbose=e.verbose;let t=new Er({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=md(re.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?Or:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Et(re.customLabels,this.studentAI?.getOptions?.()?.customLabels,this.teacherAI?.getOptions?.()?.customLabels,e?.customLabels)}initializeResultExplainer(){this.resultExplainer=void 0}initializeStats(){return{totalCalls:0,successfulDemos:0,estimatedTokenUsage:0,earlyStopped:!1,resourceUsage:{totalTokens:0,totalTime:0,avgLatencyPerEval:0,costByModel:{}},convergenceInfo:{converged:!1,finalImprovement:0,stagnationRounds:0,convergenceThreshold:.01},bestScore:0,bestConfiguration:{}}}setupRandomSeed(){this.seed!==void 0&&(Math.random=(()=>{let e=this.seed;return()=>(e=(e*9301+49297)%233280,e/233280)})())}checkCostLimits(){return this.costTracker?.isLimitReached()??!1}checkTargetScore(e){return this.targetScore!==void 0&&e>=this.targetScore}updateResourceUsage(e,t=0){this.stats.resourceUsage.totalTime=Date.now()-e,this.stats.resourceUsage.totalTokens+=t,this.stats.totalCalls>0&&(this.stats.resourceUsage.avgLatencyPerEval=this.stats.resourceUsage.totalTime/this.stats.totalCalls)}triggerEarlyStopping(e,t,n){this.stats.earlyStopped=!0,this.stats.earlyStopping={bestScoreRound:t,patienceExhausted:e.includes("improvement"),reason:e},this.recordEarlyStoppingMetrics(e,"unknown",n),this.onEarlyStop&&this.onEarlyStop(e,this.stats),this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:e,finalScore:this.stats.bestScore??0,round:t}})}validateExamples(e,t=!0){if(!e||e.length===0)throw new Error("At least 1 example is required for optimization");if(t&&e.length<2)throw new Error("At least 2 examples are required for optimization with auto-splitting. Provide more examples to enable proper train/validation split.");let n=t?10:5;e.length<n&&this.verbose&&console.warn(`[Ax Optimizer] Warning: Only ${e.length} examples provided. Consider providing more examples (${n}+ recommended) for better optimization results.`)}getAIService(e=!1,t){return e&&t?.overrideTeacherAI?t.overrideTeacherAI:e&&this.teacherAI?this.teacherAI:this.studentAI}hasTeacherAI(e){return e?.overrideTeacherAI!==void 0||this.teacherAI!==void 0}getTeacherOrStudentAI(e){return e?.overrideTeacherAI||this.teacherAI||this.studentAI}async executeWithTeacher(e,t=!0,n){let r=this.getAIService(t,n);return await e(r)}async*compileStream(e,t,n,r){let o=Date.now(),i=this.constructor.name,a=e.getSignature().toString();this.recordOptimizationStart(i,a,r);let u,l=(g,f,x,y,h,A,b,I={},T)=>{this.getOptimizerLogger(T)?.({name:"RoundProgress",value:{round:g,totalRounds:T?.maxIterations??0,currentScore:f,bestScore:A,configuration:x}}),this.updateOptimizationProgress(g,f,x,y,h,A,b,I,T)},c=(g,f)=>{u=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),l(g.round,g.currentScore,g.currentConfiguration||{},i,{},g.bestScore,g.bestConfiguration,g.convergenceInfo,r)},d=await this.compile(e,t,n,{...r,overrideOnProgress:p,overrideOnEarlyStop:c}),m=Date.now()-o;return this.recordOptimizationComplete(m,!0,i,a,r),u&&this.getLogger(r)?.({name:"Notification",id:"optimization_early_stop",value:`Optimization stopped early due to ${u}`}),{demos:d.demos,stats:d.stats,bestScore:d.bestScore,finalConfiguration:d.finalConfiguration,scoreHistory:d.scoreHistory,configurationHistory:d.configurationHistory}}async compilePareto(e,t,n,r){let o=this.constructor.name,i=Date.now(),a=await this.generateWeightedSolutions(e,t,n,r),u=await this.generateConstraintSolutions(e,t,n,r),l=[...a,...u],c=this.findParetoFrontier(l),p=this.calculateHypervolume(c);this.updateResourceUsage(i),this.stats.convergenceInfo.converged=!0,this.recordParetoMetrics(c.length,l.length,"base_optimizer",p,r);let d=c.length>0?Math.max(...c.map(m=>Math.max(...Object.values(m.scores)))):0;return{demos:c.length>0?[...c[0].demos]:void 0,stats:this.stats,bestScore:d,paretoFront:c,hypervolume:p,paretoFrontSize:c.length,finalConfiguration:{paretoFrontSize:c.length,hypervolume:p,strategy:"weighted_combinations_and_constraints",numSolutions:l.length}}}async generateWeightedSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for Pareto optimization");let i=t[0],a=await e.forward(this.getAIService(!1,r),i),u=await n({prediction:a,example:i}),l=Object.keys(u),c=this.generateWeightCombinations(l);for(let p=0;p<c.length;p++){let d=c[p],m=async({prediction:g,example:f})=>{let x=await n({prediction:g,example:f}),y=0;for(let[h,A]of Object.entries(x))y+=A*(d[h]||0);return y};try{let g=await this.compile(e,t,m,{...r,verbose:!1}),f=await this.evaluateWithMultiObjective(e,g,n,t);o.push({scores:f,demos:g.demos,configuration:{...g.finalConfiguration,weights:d,strategy:"weighted_combination"}})}catch{}}return o}async generateConstraintSolutions(e,t,n,r){let o=[];if(!t||t.length===0)throw new Error("No examples provided for multi-objective optimization");let i=t[0],a=await e.forward(this.getAIService(!1,r),i),u=await n({prediction:a,example:i}),l=Object.keys(u);for(let c of l){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),f=g[c]||0,x=0;for(let[y,h]of Object.entries(g))y!==c&&h<.3&&(x+=(.3-h)*2);return f-x};try{let d=await this.compile(e,t,p,{...r,verbose:!1}),m=await this.evaluateWithMultiObjective(e,d,n,t);o.push({scores:m,demos:d.demos,configuration:{...d.finalConfiguration,primaryObjective:c,strategy:"constraint_based"}})}catch{}}return o}generateWeightCombinations(e){let t=[];for(let r of e){let o={};for(let i of e)o[i]=i===r?1:0;t.push(o)}let n={};for(let r of e)n[r]=1/e.length;if(t.push(n),e.length===2){let[r,o]=e;for(let i=.1;i<=.9;i+=.2){let a=1-i;t.push({[r]:i,[o]:a})}}if(e.length===3){let[r,o,i]=e;t.push({[r]:.5,[o]:.3,[i]:.2},{[r]:.3,[o]:.5,[i]:.2},{[r]:.2,[o]:.3,[i]:.5})}return t}async evaluateWithMultiObjective(e,t,n,r){let o=new Ie(e.getSignature());t.demos&&o.setDemos(t.demos);let i=[],a=Math.max(1,Math.min(5,Math.floor(r.length*.2))),u=r.slice(-a),l={},c=u;for(let d of c)try{let m=await o.forward(this.studentAI,d),g=await n({prediction:m,example:d});for(let[f,x]of Object.entries(g))l[f]||(l[f]=[]),l[f].push(x)}catch{}let p={};for(let[d,m]of Object.entries(l))p[d]=m.length>0?m.reduce((g,f)=>g+f,0)/m.length:0;return p}findParetoFrontier(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n],o=!1,i=0;for(let a=0;a<e.length;a++){if(n===a)continue;let u=e[a];if(this.dominates(u.scores,r.scores)){o=!0;break}this.dominates(r.scores,u.scores)&&i++}o||t.push({demos:r.demos||[],scores:r.scores,configuration:r.configuration,dominatedSolutions:i})}return t}dominates(e,t){let n=Object.keys(e),r=!0,o=!1;for(let i of n){let a=e[i]||0,u=t[i]||0;if(a<u){r=!1;break}a>u&&(o=!0)}return r&&o}calculateHypervolume(e){if(e.length===0)return;let t=e[0],n=Object.keys(t.scores);if(n.length===2){let[r,o]=n,i=0,a=[...e].sort((l,c)=>(c.scores[r]||0)-(l.scores[r]||0)),u=0;for(let l of a){let c=l.scores[r]||0,p=l.scores[o]||0;i+=c*(p-u),u=Math.max(u,p)}return i}}async saveCheckpoint(e,t,n,r,o={},i){let a=i?.overrideCheckpointSave||this.checkpointSave;if(!a)return;let u=Date.now(),l=!1,c;try{let p={version:"1.0.0",timestamp:Date.now(),optimizerType:e,optimizerConfig:t,currentRound:this.currentRound,totalRounds:this.stats.resourceUsage.totalTime>0?this.currentRound:0,bestScore:n,bestConfiguration:r,scoreHistory:[...this.scoreHistory],configurationHistory:[...this.configurationHistory],stats:{...this.stats},optimizerState:o,examples:[]};c=await a(p),l=!0}catch(p){throw l=!1,p}finally{let p=Date.now()-u;this.recordCheckpointMetrics("save",p,l,e,i)}return c}async loadCheckpoint(e,t){let n=t?.overrideCheckpointLoad||this.checkpointLoad;if(!n)return null;let r=Date.now(),o=!1,i=null;try{i=await n(e),o=i!==null}catch(a){throw o=!1,a}finally{let a=Date.now()-r;this.recordCheckpointMetrics("load",a,o,"unknown",t)}return i}restoreFromCheckpoint(e){this.currentRound=e.currentRound,this.scoreHistory=[...e.scoreHistory],this.configurationHistory=[...e.configurationHistory],this.stats={...e.stats}}shouldSaveCheckpoint(e,t){let n=t?.overrideCheckpointInterval||this.checkpointInterval;return n!==void 0&&e%n===0}async updateOptimizationProgress(e,t,n,r,o,i,a,u={},l){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,l)&&await this.saveCheckpoint(r,o,i,a,u,l),this.getOptimizerLogger(l)?.({name:"RoundProgress",value:{round:e,totalRounds:l?.maxIterations??0,currentScore:t,bestScore:i,configuration:n}})}async saveFinalCheckpoint(e,t,n,r,o={},i){i?.saveCheckpointOnComplete!==!1&&await this.saveCheckpoint(e,t,n,r,{...o,final:!0},i)}getLogger(e){if(this.isLoggingEnabled(e))return this.logger?this.logger:this.studentAI.getLogger()}isLoggingEnabled(e){return e?.verbose!==void 0?e.verbose:this.verbose??!0}recordOptimizationStart(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);if(t){let o=(t.match(/input:/g)||[]).length,i=(t.match(/output:/g)||[]).length;Td(this.metricsInstruments,o,i,0,0,e,r)}Rd(this.metricsInstruments,e,this.targetScore,void 0,r)}recordOptimizationComplete(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);fd(this.metricsInstruments,e,t,n,r,i),yd(this.metricsInstruments,e,n,i);let a=this.costTracker?.getCurrentCost()??0,u=this.costTracker?.getTotalTokens()??0;Ad(this.metricsInstruments,u,a,n,void 0,i)}recordConvergenceMetrics(e,t,n,r,o,i){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(i);hd(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);xd(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);bd(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Id(this.metricsInstruments,e,t,n,r,i)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Cd(this.metricsInstruments,e,t,n,r,i)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);wd(this.metricsInstruments,e,t,n,o)}isOptimizerLoggingEnabled(e){return this.debugOptimizer||(e?.verbose??this.verbose??!1)}getOptimizerLogger(e){if(this.isOptimizerLoggingEnabled(e))return this.optimizerLogger??re.optimizerLogger??Or}getStats(){return{...this.stats}}async explainOptimizationResults(e,t,n){let r=this.stats.convergenceInfo.converged,o=this.stats.totalCalls,i=o>0?this.stats.successfulDemos/o*100:0,a=`Optimization finished with best score ${e.toFixed(3)}${t?` using configuration ${JSON.stringify(t)}`:""}. Convergence: ${r?"yes":"no"}. Success rate: ${i.toFixed(1)}%.`,u=[];if(r||u.push("Increase numTrials or relax earlyStoppingTrials to allow further improvement."),typeof this.targetScore=="number"&&e<this.targetScore&&u.push("Tighten the metric or supply more/better-labeled examples to reach targetScore."),t&&"bootstrappedDemos"in t){let c=t.bootstrappedDemos;typeof c=="number"&&c===0&&u.push("Consider allowing a small number of bootstrapped demos to boost performance.")}u.length===0&&u.push("Re-run with more trials or different acquisition settings to explore more of the space.");let l=`Tokens used: ${this.stats.resourceUsage.totalTokens}, rounds: ${this.currentRound}, stagnationRounds: ${this.stats.convergenceInfo.stagnationRounds}.`;return{humanExplanation:a,recommendations:u,performanceAssessment:l}}async logOptimizationComplete(e,t,n,r,o){let i=this.getOptimizerLogger(r);i&&i(o?{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",explanation:o.humanExplanation,recommendations:o.recommendations,performanceAssessment:o.performanceAssessment,stats:this.stats}}:{name:"OptimizationComplete",value:{optimizerType:e,bestScore:t,bestConfiguration:n||{},totalCalls:this.stats.totalCalls,successRate:this.stats.totalCalls>0?`${(this.stats.successfulDemos/this.stats.totalCalls*100).toFixed(1)}%`:"N/A",stats:this.stats}})}reset(){this.stats=this.initializeStats(),this.costTracker?.reset(),this.currentRound=0,this.scoreHistory=[],this.configurationHistory=[]}};var Ll=Yn(require("crypto"),1);function es(s){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:s}}function lt(s){return JSON.parse(JSON.stringify(s))}function kd(s){return Math.ceil(s.length/4)}function $i(s,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:i=!0,enableAutoPrune:a=!1,protectedBulletIds:u}=t??{},l=new Date().toISOString(),c=u??new Set;for(let p of e){if(!p.section)continue;if(!s.sections[p.section]){if(!i)continue;s.sections[p.section]=[]}let d=s.sections[p.section];switch(p.type){case"ADD":{if(d.length>=o){if(!a)continue;let f=vd(d,c);if(!f)continue;n.push(f.id),r.push({type:"REMOVE",section:p.section,bulletId:f.id,metadata:{...f.metadata??{},autoPruned:!0,removedAt:l}})}let m=p.bulletId??Sd(p.section),g={id:m,section:p.section,content:p.content??"",helpfulCount:0,harmfulCount:0,createdAt:l,updatedAt:l,metadata:p.metadata?{...p.metadata}:void 0};d.push(g),n.push(m);break}case"UPDATE":{let m=d.find(g=>g.id===p.bulletId);if(!m)continue;typeof p.content=="string"&&(m.content=p.content),m.updatedAt=l,p.metadata&&(m.metadata={...m.metadata??{},...p.metadata}),n.push(m.id);break}case"REMOVE":{let m=d.findIndex(g=>g.id===p.bulletId);if(m>=0){let[g]=d.splice(m,1);g&&n.push(g.id)}break}}}return Ui(s),s.updatedAt=l,{updatedBulletIds:n,autoRemoved:r}}function Di(s,e,t){for(let n of Object.values(s.sections)){let r=n.find(o=>o.id===e);if(r){t==="helpful"?r.helpfulCount+=1:t==="harmful"&&(r.harmfulCount+=1),r.updatedAt=new Date().toISOString(),Ui(s);return}}}function Gt(s){let e=s.description?`## Context Playbook
542
+ ${s.description.trim()}
543
+ `:`## Context Playbook
544
+ `,t=Object.entries(s.sections).map(([n,r])=>{let o=r.map(i=>`- [${i.id}] ${i.content}`).join(`
545
+ `);return o?`### ${n}
546
+ ${o}`:`### ${n}
547
+ _(empty)_`}).join(`
548
+
549
+ `);return`${e}
550
+ ${t}`.trim()}function Sd(s){let e=s.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=Ll.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function vd(s,e){let t=-1,n;for(let o=0;o<s.length;o+=1){let i=s[o];if(e.has(i.id))continue;let a=i.helpfulCount??0,u=i.harmfulCount??0,l=a-u*2,c=Date.parse(i.updatedAt??i.createdAt),p=[l,a,Number.isFinite(c)?c:Number.POSITIVE_INFINITY];if(!n){t=o,n=p;continue}let d=s[t],m=d.helpfulCount??0,g=d.harmfulCount??0,f=m-g*2,x=Date.parse(d.updatedAt??d.createdAt),y=[f,m,Number.isFinite(x)?x:Number.POSITIVE_INFINITY];(p[0]<y[0]||p[0]===y[0]&&p[1]<y[1]||p[0]===y[0]&&p[1]===y[1]&&p[2]<y[2])&&(t=o,n=p)}if(t===-1)return;let[r]=s.splice(t,1);return r}function Gi(s,e=.95){for(let[t,n]of Object.entries(s.sections)){let r=new Map,o=[];for(let i of n){let a=i.content.trim().toLowerCase(),u=r.get(a);u?(u.helpfulCount+=i.helpfulCount,u.harmfulCount+=i.harmfulCount,u.updatedAt=i.updatedAt):(r.set(a,i),o.push(i))}s.sections[t]=o}Ui(s)}function Ui(s){let e=0,t=0,n=0,r=0;for(let o of Object.values(s.sections))for(let i of o)e+=1,t+=i.helpfulCount,n+=i.harmfulCount,r+=kd(i.content);s.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var Od={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Mr=class extends mt{playbook;artifact;baseInstruction;constructor(e){super({bestScore:e.bestScore,stats:e.stats,instruction:e.instruction,demos:e.demos,examples:e.examples,modelConfig:e.modelConfig,optimizerType:e.optimizerType,optimizationTime:e.optimizationTime,totalRounds:e.totalRounds,converged:e.converged,scoreHistory:e.scoreHistory,configurationHistory:e.configurationHistory}),this.playbook=lt(e.playbook),this.artifact=e.artifact,this.baseInstruction=e.baseInstruction}applyTo(e){super.applyTo(e);let t=e.getSignature(),r=[(this.baseInstruction??t.getDescription()??"").trim(),"",Gt(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
551
+
552
+ `);e.setDescription(r)}},Mn=class extends et{aceConfig;playbook;baseInstruction;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...Od,...t},this.playbook=t?.initialPlaybook!==void 0?lt(t.initialPlaybook):es()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?lt(this.aceConfig.initialPlaybook):es(),this.baseInstruction=void 0,this.generatorHistory=[],this.deltaHistory=[]}hydrate(e,t){this.program=e,this.baseInstruction=t?.baseInstruction??e.getSignature().getDescription()??void 0,this.playbook=t?.playbook!==void 0?lt(t.playbook):this.aceConfig.initialPlaybook!==void 0?lt(this.aceConfig.initialPlaybook):es(),this.generatorHistory=[...t?.artifact?.feedback??[]],this.deltaHistory=[...t?.artifact?.history??[]]}getPlaybook(){return lt(this.playbook)}getBaseInstruction(){return this.baseInstruction}getArtifact(){return{playbook:lt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]}}applyCurrentState(e){let t=e??this.program;if(!t)throw new Error("AxACE: no program available to apply playbook state");let n=this.baseInstruction??t.getSignature().getDescription()??"";t.setDescription?.(this.composeInstruction(n,this.playbook))}configureAuto(e){switch(e){case"light":this.aceConfig.maxEpochs=1,this.aceConfig.maxReflectorRounds=1;break;case"medium":this.aceConfig.maxEpochs=2,this.aceConfig.maxReflectorRounds=2;break;case"heavy":this.aceConfig.maxEpochs=3,this.aceConfig.maxReflectorRounds=3;break}}async compile(e,t,n,r){let o=r?.aceOptions;o&&(Object.assign(this.aceConfig,{maxEpochs:o.maxEpochs??this.aceConfig.maxEpochs,maxReflectorRounds:o.maxReflectorRounds??this.aceConfig.maxReflectorRounds,maxSectionSize:o.maxSectionSize??this.aceConfig.maxSectionSize,similarityThreshold:o.similarityThreshold??this.aceConfig.similarityThreshold,allowDynamicSections:o.allowDynamicSections??this.aceConfig.allowDynamicSections}),o.initialPlaybook&&(this.playbook=lt(o.initialPlaybook)));let i=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),u=e.getSignature().getDescription()??"";this.baseInstruction=a??u,this.generatorHistory=[],this.deltaHistory=[];let l=Number.NEGATIVE_INFINITY,c=0,p=Math.max(this.aceConfig.maxEpochs,1),d=p*t.length;try{for(let y=0;y<p;y++)for(let h=0;h<t.length;h++){let A=t[h],b=this.composeInstruction(a??u,this.playbook);e.setDescription?.(b);let I=await e.forward(this.studentAI,A);this.stats.totalCalls+=1;let T=await n({prediction:I,example:A});typeof T=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,T),l=Math.max(l,T));let R=I?.severity,k=A?.severity,v=this.createGeneratorOutput(I,A),O=k&&R&&k!==R,P=await this.runReflectionRounds({example:A,generatorOutput:v,feedback:k&&R&&k!==R?`Expected severity "${k}" but model predicted "${R}".`:void 0}),$=await this.runCurator({program:e,example:A,reflection:P,playbook:this.playbook}),E=this.normalizeCuratorOperations($?.operations);E.length===0&&O&&(E=this.inferOperationsFromReflection(P)),E=this.resolveCuratorOperationTargets(E,this.playbook,P,v);let F=$||E.length>0?{...$??{},operations:E}:void 0,_=[];if(E.length>0){let U=this.collectProtectedBulletIds(E),se=$i(this.playbook,E,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:U});_=se.updatedBulletIds,se.autoRemoved.length>0&&(E.push(...se.autoRemoved),F&&(F.operations=E))}if(P?.bulletTags)for(let U of P.bulletTags)Di(this.playbook,U.id,U.tag);E.length>0&&_.length>0&&Gi(this.playbook,this.aceConfig.similarityThreshold);let j={example:A,prediction:I,score:typeof T=="number"?T:0,generatorOutput:v,reflection:P,curator:F,timestamp:new Date().toISOString()};this.generatorHistory.push(j),_.length>0&&F?.operations?.length&&this.deltaHistory.push({epoch:y,exampleIndex:h,operations:F.operations}),c+=1,this.currentRound=c;let H=typeof T=="number"&&Number.isFinite(T)?T:0,L=Number.isFinite(l)?l:H,X={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,H,{epoch:y,exampleIndex:h,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},L,{playbookBullets:this.playbook.stats.bulletCount},void 0,X),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,H)}}finally{e.setDescription?.(u)}let m=Date.now()-i;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(l)?l:0;let g={playbook:lt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},f=new Mr({baseInstruction:a??u,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(l)?l:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:c,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(l)?l:0,finalConfiguration:{strategy:"ace",epochs:p},optimizedProgram:f,playbook:lt(this.playbook),artifact:g}}async applyOnlineUpdate(e){if(!this.program)throw new Error("AxACE: `compile` must be run before `applyOnlineUpdate`");let t=this.createGeneratorOutput(e.prediction,e.example),n=e.prediction?.severity,r=e.example?.severity,o=await this.runReflectionRounds({example:e.example,generatorOutput:t,feedback:e.feedback??(r&&n&&r!==n?`Expected severity "${r}" but model predicted "${n}".`:void 0)}),i=await this.runCurator({program:this.program,example:e.example,reflection:o,playbook:this.playbook}),a=this.normalizeCuratorOperations(i?.operations),u=r&&n&&r!==n;a.length===0&&u&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let l=i||a.length>0?{...i??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)Di(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=$i(this.playbook,a,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:p});d.autoRemoved.length>0&&(a.push(...d.autoRemoved),l&&(l.operations=a)),Gi(this.playbook,this.aceConfig.similarityThreshold)}let c={example:e.example,prediction:e.prediction,score:0,generatorOutput:t,reflection:o,curator:l,timestamp:new Date().toISOString()};return this.generatorHistory.push(c),l}composeInstruction(e,t){return[e.trim(),"",Gt(t)].filter(r=>r.trim().length>0).join(`
553
+
554
+ `)}async extractProgramInstruction(e){try{return e.getSignature().getDescription()??void 0}catch{return}}createGeneratorOutput(e,t){let n=e?.thought?.toString()??"",r=Array.isArray(e?.bullet_ids)?e?.bullet_ids:[];return{reasoning:n,answer:e,bulletIds:r,trajectory:JSON.stringify({example:t,prediction:e}),metadata:{predictedSeverity:e?.severity,expectedSeverity:t?.severity}}}resolveCuratorOperationTargets(e,t,n,r){if(!e.length)return e;let o=[],i=new Set(e.map(c=>c.bulletId).filter(c=>typeof c=="string")),a=new Map,u=(c,p)=>{if(i.has(c))return;let d=this.locateBullet(t,c);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[p].push(d.id),a.set(d.section,m)};for(let c of n?.bulletTags??[]){let p=c.tag==="harmful"?"harmful":"primary";u(c.id,p)}if(r?.bulletIds)for(let c of r.bulletIds)u(c,"generator");let l=c=>{let p=a.get(c);if(!p)return this.locateFallbackBullet(t,c,i);let d=g=>{for(;g.length>0;){let f=g.shift();if(!i.has(f))return f}},m=d(p.harmful)??d(p.primary)??d(p.generator);return m||this.locateFallbackBullet(t,c,i)};for(let c of e){if((c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId){let p=l(c.section);p&&(c.bulletId=p,i.add(p))}(c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId||o.push(c)}return o}locateBullet(e,t){for(let n of Object.values(e.sections)){let r=n.find(o=>o.id===t);if(r)return r}}locateFallbackBullet(e,t,n){let r=e.sections[t]??[];for(let o of r)if(!n.has(o.id))return o.id}collectProtectedBulletIds(e){let t=new Set;for(let n of e)n.type==="UPDATE"&&n.bulletId&&t.add(n.bulletId);return t}normalizeCuratorOperations(e){if(!e)return[];if(Array.isArray(e)){let t=[],n=new Set;for(let r of e){if(!r||typeof r!="object")continue;let o=r.type??"ADD",i=typeof o=="string"?o.toUpperCase():"ADD",a=i==="UPDATE"?"UPDATE":i==="REMOVE"?"REMOVE":"ADD",u=r.section??"Guidelines",l=typeof u=="string"&&u.trim().length>0?u.trim():"Guidelines",c=r.content??"",p=typeof c=="string"?c.trim():"";if(a!=="REMOVE"&&p.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,f=[a,l,p,m??""].join(":");if(n.has(f))continue;n.add(f);let x={type:a,section:l};a!=="REMOVE"&&(x.content=p),m&&(x.bulletId=m);let y=r.metadata;y&&typeof y=="object"&&(x.metadata={...y}),t.push(x)}return t}if(typeof e=="string")try{let t=JSON.parse(e);return this.normalizeCuratorOperations(t)}catch{return[]}if(typeof e=="object"){let t=e;if(t&&Array.isArray(t.operations))return this.normalizeCuratorOperations(t.operations);if(t&&typeof t.operations=="string")try{let n=JSON.parse(t.operations);return this.normalizeCuratorOperations(n)}catch{return[]}return[]}return[]}inferOperationsFromReflection(e){if(!e)return[];let t=[],n=new Set,r=(o,i)=>{if(!i)return;let a=i.trim();if(!a||a.toLowerCase().startsWith("no error"))return;let l=`${o}:${a}`;n.has(l)||(n.add(l),t.push({type:"ADD",section:o,content:a}))};return r("Guidelines",e.keyInsight),r("Response Strategies",e.correctApproach),r("Common Pitfalls",e.errorIdentification),r("Root Cause Notes",e.rootCauseAnalysis),t}async runReflectionRounds({example:e,generatorOutput:t,feedback:n}){let r=Math.max(this.aceConfig.maxReflectorRounds,1),o;for(let i=0;i<r;i++){let a=await this.runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:o});if(!a)break;o=a;let u=a.errorIdentification?.toLowerCase().trim()??"";if(a.metadata?.resolved===!0||u.length===0||u.startsWith("no error")||u.startsWith("resolved"))break}return o}async runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:r}){let o=this.getOrCreateReflectorProgram(),i=this.teacherAI??this.studentAI;try{let a={severity:e?.severity,policyHint:e?.policyHint},c=(this.program?.getSignature()?.getInputFields()??[]).reduce((d,m)=>(m.name in e&&(d[m.name]=e[m.name]),d),{});return await o.forward(i,{question:JSON.stringify(c),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:Gt(this.playbook),structured:this.playbook}),expected_answer:a.severity||a.policyHint?JSON.stringify(a):void 0,feedback:n,previous_reflection:r?JSON.stringify(r):void 0})}catch(a){this.verbose&&console.warn("[AxACE] Reflector error:",a instanceof Error?a.message:a);return}}async runCurator({program:e,example:t,reflection:n,playbook:r}){if(!n)return;let o=this.getOrCreateCuratorProgram(),i=this.teacherAI??this.studentAI,l=e.getSignature().getInputFields().reduce((c,p)=>(p.name in t&&(c[p.name]=t[p.name]),c),{});try{return await o.forward(i,{playbook:JSON.stringify({markdown:Gt(r),structured:r}),reflection:JSON.stringify(n),question_context:JSON.stringify(l),token_budget:1024})}catch(c){this.verbose&&console.warn("[AxACE] Curator error:",c instanceof Error?c.message:c);return}}getOrCreateReflectorProgram(){if(!this.reflectorProgram){let e=ue().input("question",ue.string("Original task input serialized as JSON")).input("generator_answer",ue.string("Generator output serialized as JSON")).input("generator_reasoning",ue.string("Generator reasoning trace").optional()).input("playbook",ue.string("Current context playbook rendered as markdown")).input("expected_answer",ue.string("Expected output when ground truth is available").optional()).input("feedback",ue.string("External feedback or reward signal").optional()).input("previous_reflection",ue.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",ue.string("Step-by-step analysis of generator performance")).output("errorIdentification",ue.string("Specific mistakes detected")).output("rootCauseAnalysis",ue.string("Underlying cause of the error")).output("correctApproach",ue.string("What the generator should do differently")).output("keyInsight",ue.string("Reusable insight to remember")).output("bulletTags",ue.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=qe(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=ue().input("playbook",ue.string("Current playbook serialized as JSON")).input("reflection",ue.string("Latest reflection output serialized as JSON")).input("question_context",ue.string("Original task input serialized as JSON")).input("token_budget",ue.number("Approximate token budget for curator response").optional()).output("reasoning",ue.string("Justification for the proposed updates")).output("operations",ue.json("List of operations with type/section/content fields")).build();this.curatorProgram=qe(e)}return this.curatorProgram}};function $l(s,e,t=0){let n=new Set([...Object.keys(s),...Object.keys(e)]),r=!0,o=!1;for(let i of n){let a=s[i]??0,u=e[i]??0;if(a+t<u){r=!1;break}a>u+t&&(o=!0)}return r&&o}function Pr(s,e=0){let t=[];for(let n=0;n<s.length;n++){let r=0,o=!1;for(let i=0;i<s.length;i++)if(n!==i){if($l(s[i].scores,s[n].scores,e)){o=!0;break}$l(s[n].scores,s[i].scores,e)&&r++}o||t.push({idx:s[n].idx,scores:s[n].scores,dominated:r})}return t}function Pn(s){if(s.length===0)return;let e=Object.keys(s[0]??{});if(e.length!==2)return;let[t,n]=e,r=[...s].sort((a,u)=>(u[t]??0)-(a[t]??0)),o=0,i=0;for(let a of r){let u=a[t]??0,l=a[n]??0,c=Math.max(l-i,0);o+=u*c,i=Math.max(i,l)}return o}function Bi(s){if(s.length===0)return 0;let e=0;for(let t of s)e+=t;return e/s.length}function Dl(s){let e={},t={};for(let r of s)for(let[o,i]of Object.entries(r))e[o]=(e[o]||0)+(typeof i=="number"?i:0),t[o]=(t[o]||0)+1;let n={};for(let[r,o]of Object.entries(e))n[r]=o/Math.max(t[r]||1,1);return n}function ji(s,e){let t=new Set;for(let c of s)for(let p of c)t.add(p);let r=[...Array.from(t)].sort((c,p)=>(e[c]??0)-(e[p]??0)),o=new Set,i=(c,p)=>{for(let d of s){if(!d.has(c))continue;let m=!1;for(let g of p)if(d.has(g)){m=!0;break}if(!m)return!1}return!0},a=!0;for(;a;){a=!1;for(let c of r){if(o.has(c))continue;let p=new Set(r.filter(d=>d!==c&&!o.has(d)));if(i(c,p)){o.add(c),a=!0;break}}}let u=r.filter(c=>!o.has(c)),l=new Set(u);return s.map(c=>{let p=new Set;for(let d of c)l.has(d)&&p.add(d);return p})}function Gl(s,e,t){let n=ji(s,e),r={};for(let u of n)for(let l of u)r[l]=(r[l]||0)+1;let o=[];for(let[u,l]of Object.entries(r)){let c=Number(u);for(let p=0;p<l;p++)o.push(c)}if(o.length===0)return 0;let i=typeof t=="function"?t():Math.random(),a=Math.floor(i*o.length);return o[a]}var Fn=class s extends et{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;paretoSetSize;crossoverEvery;tieEpsilon;feedbackMemorySize;feedbackMemory=[];mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;static REFLECTION_PROMPT_TEMPLATE="I provided an assistant with the following instructions to perform a task for me:\n```\n<curr_instructions>\n```\n\nThe following are examples of different task inputs provided to the assistant along with the assistant's response for each of them, and some feedback on how the assistant's response could be better:\n```\n<inputs_outputs_feedback>\n```\n\nYour task is to write a new instruction for the assistant. Read the inputs carefully and identify the input format and infer detailed task description about the task I wish to solve with the assistant. Read all the assistant responses and the corresponding feedback. Identify all niche and domain specific factual information about the task and include it in the instruction, as a lot of it may not be available to the assistant in the future. The assistant may have utilized a generalizable strategy to solve the task, if so, include that in the instruction as well. Provide the new instructions within ``` blocks.";rngState=123456789;samplerState={epoch:-1,shuffled:[],freq:new Map};localScoreHistory=[];localConfigurationHistory=[];constructor(e){super(e);let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??20,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.crossoverEvery;this.crossoverEvery=Math.max(0,Math.floor(o??Math.max(3,Math.floor(this.numTrials/4))));let i=e?.tieEpsilon;this.tieEpsilon=Number.isFinite(i)?i:0;let a=e?.feedbackMemorySize;this.feedbackMemorySize=Math.max(0,Math.floor(a??4));let u=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(u??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.localScoreHistory=[],this.localConfigurationHistory=[],this.feedbackMemory=[],this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}async compile(e,t,n,r){let o=Date.now();this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let i=r?.maxMetricCalls;if(!Number.isFinite(i)||i<=0)throw new Error("AxGEPA: options.maxMetricCalls must be set to a positive integer");let a=Math.floor(i),u=r?.validationExamples,l=r?.feedbackExamples,c=(u&&u.length>0?u:t).slice(0,this.paretoSetSize),p=G=>{let Z=Object.keys(G).sort().reduce((K,Y)=>(K[Y]=G[Y],K),{});return JSON.stringify(Z)},d=new Set(t.map(G=>p(G))),m=l&&l.length>0?l.filter(G=>d.has(p(G))):t,g=m.length>0?m:t,f=this.getInstructionTargets(e);if(f.length===0)throw new Error("AxGEPA: program has no instruction-bearing nodes to optimize");let x=f.map(G=>G.id),y=G=>{for(let Z of f){let K=G[Z.id];typeof K=="string"&&Z.program.setInstruction?.(K)}},h=async(G,Z)=>{let K=await n({prediction:G,example:Z});if(typeof K=="number")return Number.isFinite(K)?{score:K}:{};if(!K||typeof K!="object")return{};let Y={};for(let[me,V]of Object.entries(K))typeof V=="number"&&Number.isFinite(V)&&(Y[me]=V);return Y},A=G=>{let Z=r?.paretoMetricKey,K=r?.paretoScalarize;if(typeof K=="function")return K(G);if(Z)return Number.isFinite(G[Z])?G[Z]:0;let Y=Object.values(G);return Y.length?Y.reduce((me,V)=>me+V,0)/Y.length:0},b=async(G,Z,K,Y=!1)=>{let me=Z.length;if(this.stats.totalCalls+me>a){if(Y)throw new Error(`AxGEPA: options.maxMetricCalls=${a} is too small to evaluate the initial Pareto set; need at least ${me} metric calls`);return}let V=[];for(let le of Z){y(G);let Te=await e.forward(this.studentAI,le,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let ie=await h(Te,le);V.push({input:le,prediction:Te,scores:ie,scalar:A(ie)})}return{rows:V,avg:Dl(V.map(le=>le.scores)),scalars:V.map(le=>le.scalar),sum:V.reduce((le,Te)=>le+Te.scalar,0)}},I={};for(let G of f)I[G.id]=await this.getBaseInstruction(G.program);let T=await b(I,c,"initial Pareto evaluation",!0),R=[{cfg:{...I},parent:void 0,scores:T.avg}],k=[T.scalars],v=this.getOptimizerLogger(r),O=r?.verbose??this.verbose?G=>console.log(`[GEPA] ${G}`):G=>{};v?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax,tunableCount:f.length}}}),O(`Starting GEPA optimization: ${t.length} train, ${c.length} validation, maxCalls=${a}`);let P=0,$=new Set,E=Pr(R.map((G,Z)=>({idx:Z,scores:G.scores})),this.tieEpsilon).map(G=>G.idx),F;for(let G=0;G<this.numTrials&&!(a!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(a)));G++){let Z=k[0]?.length??0,K=[];for(let W=0;W<Z;W++){let ee=Number.NEGATIVE_INFINITY,fe=new Set;for(let we=0;we<k.length;we++){let ve=k[we][W];ve>ee+this.tieEpsilon?(ee=ve,fe.clear(),fe.add(we)):Math.abs(ve-ee)<=this.tieEpsilon&&fe.add(we)}K.push(fe)}let Y=k.map(W=>Bi(W));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let W=Ee=>{let ge=[],z=Ee;for(;z!==void 0;)ge.push(z),z=R[z]?.parent;return ge},ee=Ee=>Ee.length?Ee[Math.floor(this.rand()*Ee.length)]:void 0,fe=ji(K,Y),we=new Set;for(let Ee of fe)for(let ge of Ee)we.add(ge);let ve=Array.from(we),nt;for(let Ee=0;Ee<10&&!nt&&!(ve.length<2);Ee++){let ge=ee(ve),z=ee(ve);if(ge===z)continue;z<ge&&([ge,z]=[z,ge]);let Q=new Set(W(ge)),de=new Set(W(z));if(Q.has(z)||de.has(ge))continue;let Me=[...Q].filter(Fe=>de.has(Fe));if(Me.length===0)continue;let xe=[];for(let Fe of Me){let ft=R[Fe].cfg,Bt=R[ge].cfg,bt=R[z].cfg,jt=!1,Vn=new Set([...Object.keys(ft),...Object.keys(Bt),...Object.keys(bt)]);for(let Jn of Vn){let Ot=ft[Jn],zt=Bt[Jn],It=bt[Jn];if(zt===Ot&&It!==zt||It===Ot&&zt!==It){jt=!0;break}}jt&&xe.push(Fe)}if(xe.length===0)continue;let Pe=xe.map(Fe=>Math.max(1e-9,Y[Fe])),gt=this.rand()*Pe.reduce((Fe,ft)=>Fe+ft,0),Wn=xe[xe.length-1];for(let Fe=0;Fe<xe.length;Fe++){if(gt<Pe[Fe]){Wn=xe[Fe];break}gt-=Pe[Fe]}nt={i:ge,j:z,a:Wn}}if(this.lastIterFoundNewProgram=!1,nt){let Ee=!1,{i:ge,j:z,a:Q}=nt,de=Y[Q],Me=Y[ge],xe=Y[z];if(de>Math.min(Me,xe))continue;let Pe=`${ge}|${z}|${Q}`;if(this.mergeAttemptKeys.has(Pe)||(this.mergeAttemptKeys.add(Pe),$.has(Pe)))continue;let{cfg:gt,descSig:Wn}=this.systemAwareMergeWithSig(R,ge,z,(Ae,De)=>Y[Ae]>=Y[De]?Ae:De),Fe=`${Math.min(ge,z)}|${Math.max(ge,z)}|${Wn}`;if(this.mergeCompositionKeys.has(Fe))continue;this.mergeCompositionKeys.add(Fe);let ft=k[ge],Bt=k[z],bt=Array.from({length:ft.length},(Ae,De)=>De),jt=bt.filter(Ae=>(ft[Ae]??0)>(Bt[Ae]??0)),Vn=bt.filter(Ae=>(Bt[Ae]??0)>(ft[Ae]??0)),Jn=bt.filter(Ae=>!(jt.includes(Ae)||Vn.includes(Ae))),Ot=5,zt=Math.ceil(Ot/3),It=(Ae,De)=>{if(De<=0||Ae.length===0)return[];if(Ae.length<=De)return[...Ae];let Qn=[],Ur=new Set;for(;Qn.length<De;){let rt=Math.floor(this.rand()*Ae.length);Ur.has(rt)||(Ur.add(rt),Qn.push(Ae[rt]))}return Qn},Ct=[];Ct.push(...It(jt,Math.min(zt,jt.length))),Ct.push(...It(Vn,Math.min(zt,Vn.length)));let Gc=Ot-Ct.length;Ct.push(...It(Jn,Math.max(0,Gc)));let da=Ot-Ct.length;if(da>0){let Ae=bt.filter(De=>!Ct.includes(De));Ct.push(...It(Ae,Math.min(da,Ae.length)))}let ws=Ct.slice(0,Math.min(Ot,bt.length)),Uc=ws.map(Ae=>c[Ae]),ma=await b(gt,Uc,"merge subsample");if(!ma)break;let Bc=ma.sum,jc=ws.reduce((Ae,De)=>Ae+(ft[De]??0),0),zc=ws.reduce((Ae,De)=>Ae+(Bt[De]??0),0);if(Bc>=Math.max(jc,zc)+this.minImprovementThreshold){O(`Iteration ${G+1}: Merge accepted (programs ${ge} + ${z} via ancestor ${Q})`);let Ae=await b(gt,c,"merge validation");if(!Ae)break;R.push({cfg:{...gt},parent:Q,scores:Ae.avg}),k.push(Ae.scalars);let De=E.length,Qn=Pn(E.map(rt=>R[rt].scores))??0;E=Pr(R.map((rt,qc)=>({idx:qc,scores:rt.scores})),this.tieEpsilon).map(rt=>rt.idx);let Ur=Pn(E.map(rt=>R[rt].scores))??0;(E.length>De||Ur>Qn+1e-6)&&(P=0),this.mergesDue-=1,this.totalMergesTested+=1,$.add(Pe),Ee=!0}if(Ee)continue}}let me=Gl(K,Y,()=>this.rand());this.lastIterFoundNewProgram=!1;let V=this.minibatch?this.nextMinibatchIndices(g.length,G).map(W=>g[W]):g,le=await b(R[me].cfg,V,"parent minibatch");if(!le)break;if(r?.skipPerfectScore??!0){let W=Number(r?.perfectScore??1);if(le.scalars.length>0&&le.scalars.every(ee=>ee>=W))continue}let Te={...R[me].cfg},ie="reflective_mutation",pe,ne,he=f[G%f.length],C=R[me].cfg[he.id],S=r?.gepaAdapter,M,w=le.rows.map(W=>({input:W.input,prediction:W.prediction,score:W.scalar}));if(S)try{let W=await S.evaluate(V,{...R[me].cfg},!0);pe=Array.isArray(W?.scores)?W.scores.reduce((ve,nt)=>ve+(Number(nt)||0),0):void 0;let ee=S.make_reflective_dataset({...R[me].cfg},W,[he.id]),fe=await S.propose_new_texts?.({...R[me].cfg},ee,[he.id]),we=fe?.[he.id]??(fe?Object.values(fe)[0]:void 0);typeof we=="string"&&we.length>0&&(M=we)}catch{}if(M||(M=await this.reflectTargetInstruction(he.id,C,e,y,{...R[me].cfg},V,async({prediction:W,example:ee})=>A(await h(W,ee)),r,w)),Te[he.id]=M,S&&pe!==void 0)try{let W=await S.evaluate(V,Te,!1);ne=Array.isArray(W?.scores)?W.scores.reduce((ee,fe)=>ee+(Number(fe)||0),0):void 0}catch{}let B=await b(Te,V,"child minibatch");if(!B)break;if(this.currentRound=G+1,await this.updateOptimizationProgress(this.currentRound,B.sum,{instructionLen:M.length,target:he.id,parent:me,totalRounds:this.numTrials},"GEPA",{strategy:ie,paretoSetSize:c.length,tunableCount:f.length},B.sum,{instructionLen:C.length,idx:me},{...r??{},maxIterations:this.numTrials}),!(B.sum>le.sum+this.minImprovementThreshold&&(pe===void 0||ne===void 0||ne>pe+this.minImprovementThreshold))){if(O(`Iteration ${G+1}: Rejected (child=${B.sum.toFixed(3)} <= parent=${le.sum.toFixed(3)})`),++P>=this.earlyStoppingTrials){O(`Early stopping: ${P} iterations without improvement`);break}continue}O(`Iteration ${G+1}: Accepted (child=${B.sum.toFixed(3)} > parent=${le.sum.toFixed(3)})`);let D=await b(Te,c,"validation evaluation");if(!D)break;R.push({cfg:{...Te},parent:me,scores:D.avg}),k.push(D.scalars);let q=E.length,te=Pn(E.map(W=>R[W].scores))??0;E=Pr(R.map((W,ee)=>({idx:ee,scores:W.scores})),this.tieEpsilon).map(W=>W.idx);let ce=Pn(E.map(W=>R[W].scores))??0;if(E.length>q||ce>te+1e-6)P=0,O(`Iteration ${G+1}: Archive improved (size=${E.length}, hv=${ce.toFixed(4)})`);else if(P++,O(`Iteration ${G+1}: Archive unchanged (stagnation=${P}/${this.earlyStoppingTrials})`),P>=this.earlyStoppingTrials){O(`Early stopping: ${P} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let _=Pr(R.map((G,Z)=>({idx:Z,scores:G.scores})),this.tieEpsilon),j=_.length>0?Math.max(..._.map(G=>A(G.scores))):0,H;if(_.length>0){let G=Number.NEGATIVE_INFINITY;for(let Z of _){let K=A(Z.scores);K>G&&(G=K,H=Z.idx)}}let L=Pn(_.map(G=>G.scores));this.stats.convergenceInfo.converged=!0;let X=this.getMergedCustomLabels(r);this.recordParetoMetrics(_.length,R.length,"GEPA",L,X);let U=Date.now()-o,se=typeof H=="number"?new mt({bestScore:j,stats:this.stats,instruction:f.length===1?R[H].cfg[x[0]]:void 0,instructionMap:{...R[H].cfg},demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:U,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,be=this.generateOptimizationReport(_,L,j,R.length);return{demos:[],stats:this.stats,bestScore:j,paretoFront:_.map(G=>({demos:[],scores:G.scores,configuration:{candidate:G.idx,instructionMap:{...R[G.idx].cfg},...f.length===1?{instruction:R[G.idx].cfg[x[0]]}:{}},dominatedSolutions:G.dominated})),paretoFrontSize:_.length,hypervolume:L,finalConfiguration:{strategy:"gepa",candidates:R.length,tunables:f.length},optimizedProgram:se,report:be}}configureAuto(e){switch(e){case"light":this.numTrials=10,this.minibatch=!0,this.minibatchSize=15;break;case"medium":this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numTrials=35,this.minibatch=!0,this.minibatchSize=35;break}}async getBaseInstruction(e){let t=e.getInstruction?.();if(t&&t.length>0)return t;let r=e.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}getInstructionTargets(e){let t=new Set,n=[],r=(o,i)=>{let a=i;!o||t.has(o)||typeof a?.setInstruction!="function"||(t.add(o),n.push({id:o,program:a,signature:a.getSignature?.()?.toString?.()}))};if("namedProgramInstances"in e&&typeof e.namedProgramInstances=="function"){let o=e.namedProgramInstances()??[];for(let i of o)r(i?.id,i?.program)}return r(e.getId?.(),e),n}async evaluateOnSet(e,t,n,r){let o=[];for(let i of n){let a=await this.evaluateOne(e,t,i,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?Bi(o):0}async evaluateOne(e,t,n,r){try{e.setInstruction?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let i=await r({prediction:o,example:n});if(typeof i=="number"&&!Number.isNaN(i)){let a=typeof this.targetScore=="number"?this.targetScore:.5;return i>=a&&(this.stats.successfulDemos+=1),i}return 0}catch(o){return this.getLogger()?.({name:"Notification",id:"gepa_eval",value:String(o)}),0}}async reflectTargetInstruction(e,t,n,r,o,i,a,u,l){let c=l?[...l]:[];if(c.length===0)for(let h of i)try{o[e]=t,r(o);let A=await n.forward(this.studentAI,h,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let b=await a({prediction:A,example:h});c.push({input:h,prediction:A,score:typeof b=="number"?b:0})}catch{c.push({input:h,prediction:{},score:0})}let p=u?.overrideTeacherAI??this.teacherAI??this.studentAI,d=qe('targetId:string "Target program ID", minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback when available" -> feedbackSummary:string "Concise program-focused feedback"'),g=[...(u?.feedbackNotes??[]).filter(h=>typeof h=="string"&&h.trim().length>0)],f=u?.feedbackFn;if(typeof f=="function")for(let h of c){let A=f({prediction:h.prediction,example:h.input});A&&(Array.isArray(A)?g.push(...A):g.push(A))}let x="";try{x=(await d.forward(p,{targetId:e,minibatch:c,evalFeedback:g}))?.feedbackSummary?.trim()||""}catch{}let y=qe('targetId:string "Target program ID", currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction (1-6 sentences) for the target program"');try{let A=(await y.forward(p,{targetId:e,currentInstruction:t,feedbackSummary:x,minibatch:c}))?.newInstruction?.trim();if(A&&A.length>16)return A}catch{}return`${t.trim()} Focus on step-by-step, target-specific reasoning and factual grounding.`.slice(0,2e3)}async reflectInstruction(e,t,n,r,o,i){let a=i??[];if(a.length===0)for(let g of n)try{t.setInstruction?.(e);let f=await t.forward(this.studentAI,g,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let x=await r({prediction:f,example:g});a.push({input:g,prediction:f,score:typeof x=="number"?x:0})}catch{a.push({input:g,prediction:{},score:0})}let u=o?.overrideTeacherAI??this.teacherAI??this.studentAI,l=o?.feedbackFn,c=(o?.feedbackNotes??[]).filter(g=>typeof g=="string"&&g.trim().length>0),p=()=>{let g=[];for(let x=0;x<a.length;x++){let y=a[x],h=`# Example ${x+1}
555
+ `;if(h+=`## Inputs
556
+ `,typeof y.input=="object"&&y.input!==null)for(let[b,I]of Object.entries(y.input))h+=`### ${b}
537
557
  ${String(I).trim()}
538
558
 
539
- `;else f+=`${String(h.input).trim()}
559
+ `;else h+=`${String(y.input).trim()}
540
560
 
541
- `;if(f+=`## Generated Outputs
542
- `,typeof h.prediction=="object"&&h.prediction!==null)for(let[x,I]of Object.entries(h.prediction))f+=`### ${x}
561
+ `;if(h+=`## Generated Outputs
562
+ `,typeof y.prediction=="object"&&y.prediction!==null)for(let[b,I]of Object.entries(y.prediction))h+=`### ${b}
543
563
  ${String(I).trim()}
544
564
 
545
- `;else f+=`${String(h.prediction).trim()}
565
+ `;else h+=`${String(y.prediction).trim()}
546
566
 
547
- `;f+=`## Feedback
548
- `;let C=`This trajectory got a score of ${h.score.toFixed(3)}.`;if(typeof u=="function")try{let x=u({prediction:h.prediction,example:h.input});x&&(C=Array.isArray(x)?x.join(`
549
- `):x)}catch{}f+=`${C}
550
- `,m.push(f)}return m.join(`
567
+ `;h+=`## Feedback
568
+ `;let A=`This trajectory got a score of ${y.score.toFixed(3)}.`;if(typeof l=="function")try{let b=l({prediction:y.prediction,example:y.input});b&&(A=Array.isArray(b)?b.join(`
569
+ `):b)}catch{}h+=`${A}
570
+ `,g.push(h)}return[...c.map((x,y)=>`# Additional Feedback ${y+1}
571
+ ${x}`),...g].join(`
551
572
 
552
- `)},p=i.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",c());try{let m=await l.chat({chatPrompt:[{role:"user",content:p}],model:o?.reflectionModel},{stream:!1});if(typeof m.getReader=="function")throw new Error("Streaming response not expected for reflection");let h=m.results?.[0]?.content;if(typeof h=="string"){let f=this.extractInstructionFromBackticks(h);if(f&&f.length>16){let C=`Iteration feedback: ${a.map(x=>`score=${x.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(C),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),f}}}catch{}let d=Ye('currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", recentFeedback?:string[] "Past feedback memory", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction within 1-6 sentences."');try{let g=(await d.forward(l,{currentInstruction:e,feedbackSummary:this.feedbackMemory[0]||"",recentFeedback:this.feedbackMemory,minibatch:a}))?.newInstruction?.trim();if(g&&g.length>16)return g}catch{}return`${e.trim()} Focus on step-by-step evidence-based reasoning. Avoid hallucinations.`.slice(0,2e3)}extractInstructionFromBackticks(e){let t=e.indexOf("```")+3,n=e.lastIndexOf("```");if(t>=n){let s=e.trim();if(s.startsWith("```")){let a=s.match(/^```\S*\n?/);if(a)return s.slice(a[0].length).trim()}else if(s.endsWith("```"))return s.slice(0,-3).trim();return s}let r=e.slice(t,n),o=r.match(/^\S*\n/);return o&&(r=r.slice(o[0].length)),r.trim()}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}generateOptimizationReport(e,t,n){let r=e.length>0?e.reduce((u,c)=>{let p=Object.values(u.scores).reduce((m,g)=>m+g,0);return Object.values(c.scores).reduce((m,g)=>m+g,0)>p?c:u}):void 0,o={};if(r)for(let[u,c]of Object.entries(r.scores))o[u]={value:c,percentage:c*100};let s=[];if(e.length>1){let u=[...e].sort((c,p)=>p.dominated-c.dominated).slice(0,3);for(let c of u)s.push({...c.scores})}let a="good",l=[];if(e.length===1)a="single",l.push("Increase numTrials (current seems low)"),l.push("Add more training examples"),l.push("Adjust earlyStoppingTrials");else if(e.length<3)a="limited",l.push("More optimization trials"),l.push("Larger validation set");else{a="good";let u=Object.keys(e[0]?.scores||{});for(let c of u)l.push(`High ${c}: Choose solution with best ${c} score`);l.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(l.push("Quick run detected - use numTrials: 30+ for production"),l.push("Provide 50+ training examples"),l.push("Use 20+ validation examples")),{summary:"GEPA Multi-Objective Optimization Complete",bestSolution:{overallScore:n??0,objectives:o},paretoFrontier:{solutionCount:e.length,objectiveSpaceCoverage:(t??0)*100,hypervolume:t??0,tradeoffs:s.length>0?s:void 0},statistics:{totalEvaluations:this.stats.totalCalls,candidatesExplored:e.length,converged:this.stats.convergenceInfo?.converged??!1},recommendations:{status:a,suggestions:l}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=Ye(`instructionA:string "Parent A instruction",
573
+ `)},d=s.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",p());try{let g=await u.chat({chatPrompt:[{role:"user",content:d}],model:o?.reflectionModel},{stream:!1});if(typeof g.getReader=="function")throw new Error("Streaming response not expected for reflection");let x=g.results?.[0]?.content;if(typeof x=="string"){let y=this.extractInstructionFromBackticks(x);if(y&&y.length>16){let h=`Iteration feedback: ${a.map(A=>`score=${A.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(h),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),y}}}catch{}let m=qe('currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", recentFeedback?:string[] "Past feedback memory", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction within 1-6 sentences."');try{let f=(await m.forward(u,{currentInstruction:e,feedbackSummary:this.feedbackMemory[0]||"",recentFeedback:this.feedbackMemory,minibatch:a}))?.newInstruction?.trim();if(f&&f.length>16)return f}catch{}return`${e.trim()} Focus on step-by-step evidence-based reasoning. Avoid hallucinations.`.slice(0,2e3)}extractInstructionFromBackticks(e){let t=e.indexOf("```")+3,n=e.lastIndexOf("```");if(t>=n){let i=e.trim();if(i.startsWith("```")){let a=i.match(/^```\S*\n?/);if(a)return i.slice(a[0].length).trim()}else if(i.endsWith("```"))return i.slice(0,-3).trim();return i}let r=e.slice(t,n),o=r.match(/^\S*\n/);return o&&(r=r.slice(o[0].length)),r.trim()}updateSamplerShuffled(e){let t=Array.from({length:e},(u,l)=>l);for(let u=t.length-1;u>0;u--){let l=Math.floor(this.rand()*(u+1));[t[u],t[l]]=[t[l],t[u]]}for(let u of t)this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,i=Array.from({length:e},(u,l)=>l).sort((u,l)=>(this.samplerState.freq.get(u)??0)-(this.samplerState.freq.get(l)??0)),a=[...t];for(let u=0;u<o;u++){let l=i[u%i.length];a.push(l),this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let i=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(i,i+n)}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}systemAwareMergeWithSig(e,t,n,r){let o=x=>{let y=[],h=x;for(;h!==void 0;)y.push(h),h=e[h]?.parent;return y},i=o(t),a=o(n),l=i.find(x=>a.includes(x))??t,c=e[l].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=[],f=Array.from(new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)])).sort();for(let x of f){let y=c[x],h=p[x],A=d[x];if(h===y&&A!==h)m[x]=A,g.push("j");else if(A===y&&h!==A)m[x]=h,g.push("i");else if(h!==A&&h!==y&&A!==y){let b=r(t,n);m[x]=b===t?h:A,g.push(b===t?"i":"j")}else m[x]=h??A??y,g.push("i")}return{cfg:m,descSig:g.join("|")}}generateOptimizationReport(e,t,n,r){let o=e.length>0?e.reduce((c,p)=>{let d=Object.values(c.scores).reduce((g,f)=>g+f,0);return Object.values(p.scores).reduce((g,f)=>g+f,0)>d?p:c}):void 0,i={};if(o)for(let[c,p]of Object.entries(o.scores))i[c]={value:p,percentage:p*100};let a=[];if(e.length>1){let c=[...e].sort((p,d)=>d.dominated-p.dominated).slice(0,3);for(let p of c)a.push({...p.scores})}let u="good",l=[];if(e.length===1)u="single",l.push("Increase numTrials (current seems low)"),l.push("Add more training examples"),l.push("Adjust earlyStoppingTrials");else if(e.length<3)u="limited",l.push("More optimization trials"),l.push("Larger validation set");else{u="good";let c=Object.keys(e[0]?.scores||{});for(let p of c)l.push(`High ${p}: Choose solution with best ${p} score`);l.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(l.push("Quick run detected - use numTrials: 30+ for production"),l.push("Provide 50+ training examples"),l.push("Use 20+ validation examples")),{summary:"GEPA Multi-Objective Optimization Complete",bestSolution:{overallScore:n??0,objectives:i},paretoFrontier:{solutionCount:e.length,objectiveSpaceCoverage:(t??0)*100,hypervolume:t??0,tradeoffs:a.length>0?a:void 0},statistics:{totalEvaluations:this.stats.totalCalls,candidatesExplored:r,converged:this.stats.convergenceInfo?.converged??!1},recommendations:{status:u,suggestions:l}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=qe(`instructionA:string "Parent A instruction",
553
574
  instructionB:string "Parent B instruction",
554
575
  recentFeedback?:string[] "Past feedback memory"
555
- -> mergedInstruction:string "Merged instruction (1-6 sentences) combining strengths, fixing weaknesses"`);try{let a=(await o.forward(r,{instructionA:e,instructionB:t,recentFeedback:this.feedbackMemory}))?.mergedInstruction?.trim();if(a&&a.length>16)return a}catch{}return(e.length>=t.length?e:t).slice(0,2e3)}};function wu(i){return i.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.isOptional?" (optional)":"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
556
- `)}function zp(i){return i.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.type?.options?` (options: ${e.type.options.join(", ")})`:"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
557
- `)}var zn=class{signature;options;constructor(e,t){this.signature=e,this.options={teacher:t.teacher,diversity:t.diversity??"none",temperature:t.temperature??.8,domain:t.domain,edgeCases:t.edgeCases,model:t.model}}async generate(e,t){let n=Date.now(),r=t?.batchSize??Math.min(e,10),o=[],s=0,a=0;for(let l=0;l<e;l+=r){let u=Math.min(r,e-l),c=await this.generateInputs(u);for(let p of c){a++;try{let d=await this.labelInput(p);o.push({input:p,expected:d,category:"normal"}),s++}catch(d){console.warn("AxSynth: Failed to label input:",d)}}}if(this.options.edgeCases&&this.options.edgeCases.length>0){let l=await this.generateEdgeCaseInputs(Math.ceil(e*.2));for(let u of l){a++;try{let c=await this.labelInput(u);o.push({input:u,expected:c,category:"edge_case"}),s++}catch(c){console.warn("AxSynth: Failed to label edge case input:",c)}}}return{examples:o,stats:{requested:e,generated:o.length,labelingSuccessRate:a>0?s/a:0,durationMs:Date.now()-n}}}async generateInputs(e){let t=this.signature.getInputFields(),n=this.signature.getOutputFields(),r=this.signature.getDescription(),o=`
576
+ -> mergedInstruction:string "Merged instruction (1-6 sentences) combining strengths, fixing weaknesses"`);try{let a=(await o.forward(r,{instructionA:e,instructionB:t,recentFeedback:this.feedbackMemory}))?.mergedInstruction?.trim();if(a&&a.length>16)return a}catch{}return(e.length>=t.length?e:t).slice(0,2e3)}};function Ul(s){return s.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.isOptional?" (optional)":"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
577
+ `)}function Ed(s){return s.map(e=>{let t=e.type?.name??"string",n=e.type?.isArray?"[]":"",r=e.type?.options?` (options: ${e.type.options.join(", ")})`:"",o=e.description?`: ${e.description}`:"";return`- ${e.name}: ${t}${n}${r}${o}`}).join(`
578
+ `)}var _n=class{signature;options;constructor(e,t){this.signature=e,this.options={teacher:t.teacher,diversity:t.diversity??"none",temperature:t.temperature??.8,domain:t.domain,edgeCases:t.edgeCases,model:t.model}}async generate(e,t){let n=Date.now(),r=t?.batchSize??Math.min(e,10),o=[],i=0,a=0;for(let u=0;u<e;u+=r){let l=Math.min(r,e-u),c=await this.generateInputs(l);for(let p of c){a++;try{let d=await this.labelInput(p);o.push({input:p,expected:d,category:"normal"}),i++}catch(d){console.warn("AxSynth: Failed to label input:",d)}}}if(this.options.edgeCases&&this.options.edgeCases.length>0){let u=await this.generateEdgeCaseInputs(Math.ceil(e*.2));for(let l of u){a++;try{let c=await this.labelInput(l);o.push({input:l,expected:c,category:"edge_case"}),i++}catch(c){console.warn("AxSynth: Failed to label edge case input:",c)}}}return{examples:o,stats:{requested:e,generated:o.length,labelingSuccessRate:a>0?i/a:0,durationMs:Date.now()-n}}}async generateInputs(e){let t=this.signature.getInputFields(),n=this.signature.getOutputFields(),r=this.signature.getDescription(),o=`
558
579
  You are generating realistic input data for an AI system.
559
580
 
560
581
  ${r?`Task description: ${r}`:""}
561
582
  ${this.options.domain?`Domain: ${this.options.domain}`:""}
562
583
 
563
584
  The system expects these INPUT fields:
564
- ${wu(t)}
585
+ ${Ul(t)}
565
586
 
566
587
  The system produces these OUTPUT fields:
567
- ${zp(n)}
588
+ ${Ed(n)}
568
589
 
569
590
  Generate ${e} diverse, realistic input examples as a JSON array.
570
591
  Each example should be an object with the input fields defined above.
571
592
  Make the examples varied and realistic for the domain.
572
593
 
573
594
  Output ONLY the JSON array, no explanation.
574
- `.trim(),s=new Ee("count:number -> examples:json");s.setInstruction(o);try{let a=await s.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(a.examples)?a.examples.slice(0,e):[]}catch(a){return console.warn("AxSynth: Input generation failed:",a),[]}}async generateEdgeCaseInputs(e){let t=this.signature.getInputFields(),n=this.options.edgeCases??[],r=`
595
+ `.trim(),i=new Ie("count:number -> examples:json");i.setInstruction(o);try{let a=await i.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(a.examples)?a.examples.slice(0,e):[]}catch(a){return console.warn("AxSynth: Input generation failed:",a),[]}}async generateEdgeCaseInputs(e){let t=this.signature.getInputFields(),n=this.options.edgeCases??[],r=`
575
596
  You are generating challenging edge case input data to test an AI system's robustness.
576
597
 
577
598
  The system expects these INPUT fields:
578
- ${wu(t)}
599
+ ${Ul(t)}
579
600
 
580
601
  Generate ${e} edge case examples as a JSON array.
581
602
  Focus on these types of edge cases:
582
- ${n.map(s=>`- ${s}`).join(`
603
+ ${n.map(i=>`- ${i}`).join(`
583
604
  `)}
584
605
 
585
606
  Output ONLY the JSON array, no explanation.
586
- `.trim(),o=new Ee("count:number -> examples:json");o.setInstruction(r);try{let s=await o.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(s.examples)?s.examples.slice(0,e):[]}catch(s){return console.warn("AxSynth: Edge case generation failed:",s),[]}}async labelInput(e){return await new Ee(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var rs=class i{gen;options;tracer;currentScore;constructor(e,t){this.gen=e,this.options={...t,enableTracing:t.enableTracing??!0},this.tracer=new jn(e,{name:this.options.name,storage:this.options.storage,metadata:this.options.metadata,onTrace:this.options.onTrace}),this.loadLatestCheckpoint()}async forward(e,t,n){return this.options.enableTracing?this.tracer.forward(e,t,n):this.gen.forward(e,t,n)}async*streamingForward(e,t,n){this.options.enableTracing?yield*this.tracer.streamingForward(e,t,n):yield*this.gen.streamingForward(e,t,n)}getUsage(){return this.gen.getUsage()}resetUsage(){this.gen.resetUsage()}getSignature(){return this.gen.getSignature()}setInstruction(e){this.gen.setInstruction(e)}getInstruction(){return this.gen.getInstruction()}updateMeter(e){this.gen.updateMeter(e)}addAssert(e,t){this.gen.addAssert(e,t)}addStreamingAssert(e,t,n){this.gen.addStreamingAssert(e,t,n)}addFieldProcessor(e,t){this.gen.addFieldProcessor(e,t)}addStreamingFieldProcessor(e,t){this.gen.addStreamingFieldProcessor(e,t)}clone(){return new i(this.gen.clone(),this.options)}async optimize(e={}){let t=Date.now(),n={...this.options,...e},r=n.teacher,o=n.budget??20,s=[...n.examples??[]];if(n.useTraces!==!1){let T=(await this.getTraces()).filter(k=>k.input&&k.output).map(k=>({...k.input,...k.output}));s=[...s,...T]}if(n.generateExamples||s.length===0){let A=n.synthCount??20,T={teacher:r,...n.synthOptions},M=(await new zn(this.gen.getSignature(),T).generate(A)).examples.map($=>({...$.input,...$.expected}));s=[...s,...M]}if(s.length===0)throw new Error("No training examples available. Provide examples, enable trace usage, or enable example generation.");let a=n.validationSplit??.2,l=[...s].sort(()=>Math.random()-.5),u=Math.floor(l.length*(1-a)),c=l.slice(0,u),p=l.slice(u),d=n.metric;if(!d){let A={ai:r,criteria:n.criteria,...n.judgeOptions};d=new Bn(this.gen.getSignature(),A).toMetricFn()}let m=new qn({studentAI:r,numTrials:o,minibatch:!0,minibatchSize:10}),g=o*Math.max(c.length,10),h=await m.compile(this.gen,c,d,{validationExamples:p,maxMetricCalls:g});if(!h.optimizedProgram)throw new Error("GEPA optimization failed: no optimizedProgram returned");h.optimizedProgram.instruction&&this.gen.setInstruction(h.optimizedProgram.instruction),this.currentScore=h.optimizedProgram.bestScore,this.tracer=this.tracer.clone(this.gen);let f=this.currentScore,C=f,I=((await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0]?.version??0)+1,w={type:"checkpoint",name:this.options.name,version:I,createdAt:new Date,instruction:this.gen.getInstruction(),score:f,optimizerType:"gepa",metadata:{budget:o,trainingExamples:c.length,durationMs:Date.now()-t}};return await this.options.storage.save(this.options.name,w),{score:f,improvement:C,checkpointVersion:I,stats:{trainingExamples:c.length,validationExamples:p.length,durationMs:Date.now()-t}}}async loadLatestCheckpoint(){try{let t=(await this.options.storage.load(this.options.name,{type:"checkpoint",limit:1}))[0];t?.instruction&&(this.gen.setInstruction(t.instruction),this.currentScore=t.score,this.tracer=this.tracer.clone(this.gen))}catch{}}getGen(){return this.gen}getStorage(){return this.options.storage}async getTraces(e){return this.options.storage.load(this.options.name,{type:"trace",...e})}async addFeedback(e,t){let n=await this.options.storage.load(this.options.name,{id:e,type:"trace",limit:1});if(n.length>0){let r=n[0];r.feedback={...r.feedback,...t},await this.options.storage.save(this.options.name,r)}}};var os=class{rows=[];baseUrl;dataset;split;config;options;constructor({dataset:e,split:t,config:n,options:r}){this.baseUrl="https://datasets-server.huggingface.co/rows",this.dataset=e,this.split=t,this.config=n,this.options=r}async fetchDataFromAPI(e){try{let t=await fetch(e);if(!t.ok)throw new Error(`Error fetching data: ${t.statusText}`);let n=await t.json();if(!n?.rows)throw new Error("Invalid data format");return n.rows}catch(t){throw console.error("Error fetching data from API:",t),t}}async loadData(){let e=this.options?.offset??0,t=this.options?.length??100,n=encodeURIComponent(this.dataset),r=`${this.baseUrl}?dataset=${n}&config=${this.config}&split=${this.split}&offset=${e}&length=${t}`;return this.rows=await this.fetchDataFromAPI(r),this.rows}setData(e){this.rows=e}getData(){return this.rows}async getRows({count:e,fields:t,renameMap:n}){if(this.rows.length===0)throw new Error("No data loaded, call loadData or setData first.");return this.rows.slice(0,e).map(o=>{let s={};return t.forEach(a=>{let l=a.split("."),u=o.row;for(let p of l)Object.hasOwn(u,p)&&(u=u[p]);if(!u)return;let c=n&&a in n?n[a]:a;if(!c)throw new Error(`Invalid field name: ${a}`);s[c]=u}),s}).filter(o=>Object.keys(o).length!==0)}};var Ru=er(require("crypto"),1);function $i(i){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:i}}function Yt(i){return JSON.parse(JSON.stringify(i))}function Hp(i){return Math.ceil(i.length/4)}function Li(i,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:s=!0,enableAutoPrune:a=!1,protectedBulletIds:l}=t??{},u=new Date().toISOString(),c=l??new Set;for(let p of e){if(!p.section)continue;if(!i.sections[p.section]){if(!s)continue;i.sections[p.section]=[]}let d=i.sections[p.section];switch(p.type){case"ADD":{if(d.length>=o){if(!a)continue;let h=Wp(d,c);if(!h)continue;n.push(h.id),r.push({type:"REMOVE",section:p.section,bulletId:h.id,metadata:{...h.metadata??{},autoPruned:!0,removedAt:u}})}let m=p.bulletId??Kp(p.section),g={id:m,section:p.section,content:p.content??"",helpfulCount:0,harmfulCount:0,createdAt:u,updatedAt:u,metadata:p.metadata?{...p.metadata}:void 0};d.push(g),n.push(m);break}case"UPDATE":{let m=d.find(g=>g.id===p.bulletId);if(!m)continue;typeof p.content=="string"&&(m.content=p.content),m.updatedAt=u,p.metadata&&(m.metadata={...m.metadata??{},...p.metadata}),n.push(m.id);break}case"REMOVE":{let m=d.findIndex(g=>g.id===p.bulletId);if(m>=0){let[g]=d.splice(m,1);g&&n.push(g.id)}break}}}return Ui(i),i.updatedAt=u,{updatedBulletIds:n,autoRemoved:r}}function Di(i,e,t){for(let n of Object.values(i.sections)){let r=n.find(o=>o.id===e);if(r){t==="helpful"?r.helpfulCount+=1:t==="harmful"&&(r.harmfulCount+=1),r.updatedAt=new Date().toISOString(),Ui(i);return}}}function $r(i){let e=i.description?`## Context Playbook
587
- ${i.description.trim()}
588
- `:`## Context Playbook
589
- `,t=Object.entries(i.sections).map(([n,r])=>{let o=r.map(s=>`- [${s.id}] ${s.content}`).join(`
590
- `);return o?`### ${n}
591
- ${o}`:`### ${n}
592
- _(empty)_`}).join(`
593
-
594
- `);return`${e}
595
- ${t}`.trim()}function Kp(i){let e=i.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=Ru.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function Wp(i,e){let t=-1,n;for(let o=0;o<i.length;o+=1){let s=i[o];if(e.has(s.id))continue;let a=s.helpfulCount??0,l=s.harmfulCount??0,u=a-l*2,c=Date.parse(s.updatedAt??s.createdAt),p=[u,a,Number.isFinite(c)?c:Number.POSITIVE_INFINITY];if(!n){t=o,n=p;continue}let d=i[t],m=d.helpfulCount??0,g=d.harmfulCount??0,h=m-g*2,f=Date.parse(d.updatedAt??d.createdAt),C=[h,m,Number.isFinite(f)?f:Number.POSITIVE_INFINITY];(p[0]<C[0]||p[0]===C[0]&&p[1]<C[1]||p[0]===C[0]&&p[1]===C[1]&&p[2]<C[2])&&(t=o,n=p)}if(t===-1)return;let[r]=i.splice(t,1);return r}function Gi(i,e=.95){for(let[t,n]of Object.entries(i.sections)){let r=new Map,o=[];for(let s of n){let a=s.content.trim().toLowerCase(),l=r.get(a);l?(l.helpfulCount+=s.helpfulCount,l.harmfulCount+=s.harmfulCount,l.updatedAt=s.updatedAt):(r.set(a,s),o.push(s))}i.sections[t]=o}Ui(i)}function Ui(i){let e=0,t=0,n=0,r=0;for(let o of Object.values(i.sections))for(let s of o)e+=1,t+=s.helpfulCount,n+=s.harmfulCount,r+=Hp(s.content);i.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var Vp={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Lr=class extends kt{playbook;artifact;baseInstruction;constructor(e){super({bestScore:e.bestScore,stats:e.stats,instruction:e.instruction,demos:e.demos,examples:e.examples,modelConfig:e.modelConfig,optimizerType:e.optimizerType,optimizationTime:e.optimizationTime,totalRounds:e.totalRounds,converged:e.converged,scoreHistory:e.scoreHistory,configurationHistory:e.configurationHistory}),this.playbook=Yt(e.playbook),this.artifact=e.artifact,this.baseInstruction=e.baseInstruction}applyTo(e){super.applyTo(e);let t=e.getSignature(),r=[(this.baseInstruction??t.getDescription()??"").trim(),"",$r(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
607
+ `.trim(),o=new Ie("count:number -> examples:json");o.setInstruction(r);try{let i=await o.forward(this.options.teacher,{count:e},{model:this.options.model});return Array.isArray(i.examples)?i.examples.slice(0,e):[]}catch(i){return console.warn("AxSynth: Edge case generation failed:",i),[]}}async labelInput(e){return await new Ie(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var Md="batch",zi={feedbackWindowSize:25,maxRecentTraces:100,updateBudget:4},ts=class s{gen;options;tracer;currentScore;currentState;readyPromise;playbookOptimizer;constructor(e,t){this.gen=e,this.options={...t,enableTracing:t.enableTracing??!0,mode:t.mode??Md,continuousOptions:{feedbackWindowSize:t.continuousOptions?.feedbackWindowSize??zi.feedbackWindowSize,maxRecentTraces:t.continuousOptions?.maxRecentTraces??zi.maxRecentTraces,updateBudget:t.continuousOptions?.updateBudget??zi.updateBudget}},this.tracer=new En(e,{name:this.options.name,storage:this.options.storage,metadata:this.options.metadata,onTrace:this.options.onTrace}),this.readyPromise=this.loadLatestCheckpoint()}async ready(){await this.readyPromise}async forward(e,t,n){return await this.ready(),this.options.enableTracing?this.tracer.forward(e,t,n):this.gen.forward(e,t,n)}async*streamingForward(e,t,n){await this.ready(),this.options.enableTracing?yield*this.tracer.streamingForward(e,t,n):yield*this.gen.streamingForward(e,t,n)}getUsage(){return this.gen.getUsage()}resetUsage(){this.gen.resetUsage()}getSignature(){return this.gen.getSignature()}setInstruction(e){this.gen.setInstruction(e)}getInstruction(){return this.currentState?.mode==="playbook"||this.options.mode==="playbook"?this.currentState?.instruction??this.gen.getSignature().getDescription()??this.gen.getInstruction():this.gen.getInstruction()}updateMeter(e){this.gen.updateMeter(e)}addAssert(e,t){this.gen.addAssert(e,t)}addStreamingAssert(e,t,n){this.gen.addStreamingAssert(e,t,n)}addFieldProcessor(e,t){this.gen.addFieldProcessor(e,t)}addStreamingFieldProcessor(e,t){this.gen.addStreamingFieldProcessor(e,t)}clone(){return new s(this.gen.clone(),this.options)}async optimize(e={}){await this.ready();let t=this.mergeConfig(e);return t.mode==="playbook"?this.optimizePlaybook(t):(await this.runPromptOptimization(t)).result}async applyUpdate(e,t={}){await this.ready();let n=this.mergeConfig(t),r=n.mode==="continuous"?{...n,budget:t.budget??n.continuousOptions.updateBudget}:n;if(r.mode==="batch")throw new Error("AxLearn: applyUpdate() is only available in continuous or playbook modes");return r.mode==="playbook"?this.applyPlaybookUpdate(r,e):(await this.runPromptOptimization(r,e)).result}getGen(){return this.gen}getStorage(){return this.options.storage}async getTraces(e){return await this.ready(),this.options.storage.load(this.options.name,{type:"trace",...e})}async addFeedback(e,t){await this.ready();let r=(await this.options.storage.load(this.options.name,{type:"trace"})).find(o=>o.id===e);r&&(r.feedback={...r.feedback,...t},await this.options.storage.save(this.options.name,r))}createPromptOptimizer(e,t){return new Fn({studentAI:this.requireRuntimeAI(e.runtimeAI),teacherAI:e.teacher,numTrials:e.budget,minibatch:!0,minibatchSize:10,onProgress:this.createOptimizerProgressHandler(t,e.onProgress)})}createPlaybookOptimizer(e,t){return new Mn({studentAI:this.requireRuntimeAI(e.runtimeAI),teacherAI:e.teacher,onProgress:this.createOptimizerProgressHandler(t,e.onProgress)},e.playbookOptions)}mergeConfig(e){return{runtimeAI:e.runtimeAI??this.options.runtimeAI,mode:this.options.mode,teacher:this.options.teacher,budget:e.budget??this.options.budget??20,metric:e.metric??this.options.metric,judgeOptions:e.judgeOptions??this.options.judgeOptions,criteria:e.criteria??this.options.criteria,examples:e.examples??this.options.examples,useTraces:e.useTraces??this.options.useTraces??!0,generateExamples:e.generateExamples??this.options.generateExamples??!1,synthCount:e.synthCount??this.options.synthCount,synthOptions:e.synthOptions??this.options.synthOptions,validationSplit:e.validationSplit??this.options.validationSplit??.2,continuousOptions:{feedbackWindowSize:e.continuousOptions?.feedbackWindowSize??this.options.continuousOptions.feedbackWindowSize,maxRecentTraces:e.continuousOptions?.maxRecentTraces??this.options.continuousOptions.maxRecentTraces,updateBudget:e.continuousOptions?.updateBudget??this.options.continuousOptions.updateBudget},playbookOptions:e.playbookOptions??this.options.playbookOptions,onProgress:e.onProgress??this.options.onProgress}}requireRuntimeAI(e){if(e)return e;throw new Error("AxLearn: runtimeAI is required for optimize()/applyUpdate(). Provide it in the constructor or per-call overrides.")}createOptimizerProgressHandler(e,t){if(t)return n=>{t({round:n.round,totalRounds:n.totalRounds,score:n.bestScore,improvement:n.bestScore-e})}}async resolveMetric(e){if(e.metric)return e.metric;let t={ai:e.teacher,criteria:e.criteria,...e.judgeOptions};return new On(this.gen.getSignature(),t).toMetricFn()}async runPromptOptimization(e,t){let n=Date.now(),r=this.currentScore??0,o=e.budget,i=await this.resolveMetric(e),a=await this.prepareDataset(e,t);if(a.examples.length<2)throw new Error("AxLearn: at least 2 usable examples are required after filtering traces/examples.");if(t!==void 0&&e.mode==="continuous"&&a.feedbackExamples.length===0)throw new Error("AxLearn: continuous updates require explicit feedback or stored feedback-bearing traces.");let{trainingExamples:u,validationExamples:l}=this.splitExamples(a.examples,a.feedbackScoredExamples,e.validationSplit,e.mode),p=await this.createPromptOptimizer({...e,budget:o},r).compile(this.gen,u,i,{validationExamples:l,feedbackExamples:a.feedbackExamples.length>0?a.feedbackExamples:void 0,feedbackFn:a.feedbackTextByKey.size>0?({example:y})=>a.feedbackTextByKey.get(this.exampleKey(y)):void 0,feedbackNotes:a.feedbackNotes.length>0?a.feedbackNotes:void 0,maxMetricCalls:this.computeMetricBudget(o,l.length)});if(!p.optimizedProgram)throw new Error("AxLearn: prompt optimization failed with no result");this.gen.applyOptimization?.(p.optimizedProgram),this.currentScore=p.optimizedProgram.bestScore,this.tracer=this.tracer.clone(this.gen);let d=this.createPromptState(e.mode,this.currentScore,a.feedbackExamples.length),m={feedbackExamples:a.feedbackExamples.length,lastUpdateAt:d.continuous?.lastUpdateAt},g={trainingExamples:u.length,validationExamples:l.length,feedbackExamples:a.feedbackExamples.length,durationMs:Date.now()-n},f=await this.saveCheckpoint({mode:e.mode,score:this.currentScore,state:d,stats:g,budget:o});return{result:{mode:e.mode,score:this.currentScore,improvement:this.currentScore-r,checkpointVersion:f,stats:{...g,mode:e.mode},state:d,artifact:m},optimizedProgram:p.optimizedProgram}}async optimizePlaybook(e){let t=Date.now();this.requireRuntimeAI(e.runtimeAI);let n=this.currentScore??0,r=await this.resolveMetric(e),o=await this.prepareDataset(e);if(o.examples.length<2)throw new Error("AxLearn: at least 2 usable examples are required for playbook mode.");let{trainingExamples:i,validationExamples:a}=this.splitExamples(o.examples,o.feedbackExamples,e.validationSplit,e.mode),u=this.createPlaybookOptimizer(e,n),l=await u.compile(this.gen,i,r,{aceOptions:e.playbookOptions});l.optimizedProgram?.applyTo(this.gen),this.playbookOptimizer=u,this.currentScore=l.bestScore,this.tracer=this.tracer.clone(this.gen);let c=l.playbook,p=l.artifact,d=this.summarizePlaybookArtifact(p),m=this.createPlaybookState(c,d,u.getBaseInstruction()),g={trainingExamples:i.length,validationExamples:a.length,feedbackExamples:o.feedbackExamples.length,durationMs:Date.now()-t},f=await this.saveCheckpoint({mode:"playbook",score:this.currentScore,state:m,stats:g,budget:e.budget});return{mode:"playbook",score:this.currentScore,improvement:this.currentScore-n,checkpointVersion:f,stats:{...g,mode:"playbook"},state:m,artifact:{playbook:c,playbookSummary:d,feedbackExamples:o.feedbackExamples.length}}}async applyPlaybookUpdate(e,t){let n=Date.now();this.requireRuntimeAI(e.runtimeAI);let r=this.currentScore??0,o=this.getOrCreatePlaybookOptimizer(e,r);await o.applyOnlineUpdate({example:t.example,prediction:t.prediction,feedback:this.feedbackToText(t.feedback)}),o.applyCurrentState(this.gen),this.tracer=this.tracer.clone(this.gen);let i=o.getArtifact(),a=o.getPlaybook(),u=this.summarizePlaybookArtifact(i),l=this.createPlaybookState(a,u,o.getBaseInstruction()),c=this.currentScore??r,p={trainingExamples:1,validationExamples:1,feedbackExamples:1,durationMs:Date.now()-n},d=await this.saveCheckpoint({mode:"playbook",score:c,state:l,stats:p,budget:e.playbookOptions?.maxEpochs});return e.onProgress?.({round:1,totalRounds:1,score:c,improvement:c-r}),{mode:"playbook",score:c,improvement:c-r,checkpointVersion:d,stats:{...p,mode:"playbook"},state:l,artifact:{playbook:a,playbookSummary:u,feedbackExamples:1}}}getOrCreatePlaybookOptimizer(e,t){return this.playbookOptimizer||(this.playbookOptimizer=this.createPlaybookOptimizer(e,t),this.playbookOptimizer.hydrate(this.gen,{baseInstruction:this.currentState?.baseInstruction??this.gen.getSignature().getDescription()??void 0,playbook:this.currentState?.playbook})),this.playbookOptimizer}async prepareDataset(e,t){let n=[],r=[],o=[],i=new Map,a=[],u=new Set,l=new Set,c=new Set,p=(d,m)=>{let g=this.normalizeExample(d);if(!g)return;let f=this.hasInputFields(g),x=this.hasOutputFields(g);if(!f||m?.requireOutput&&!x)return;let y=this.exampleKey(g);m?.includeInExamples!==!1&&!u.has(y)&&(u.add(y),n.push(g));let A=m?.feedback?.trim();if(A){let b=i.get(y);b?b!==A&&i.set(y,`${b}
608
+ ${A}`):i.set(y,A),m?.includeInFeedbackSet!==!1&&!l.has(y)&&(r.push(g),l.add(y)),m?.feedbackEligibleForScoring&&!c.has(y)&&(o.push(g),c.add(y))}return g};for(let d of e.examples??[])p(d);if(e.useTraces){let d=await this.loadRelevantTraces(e);for(let m of d)p(m.example,{feedback:m.feedback,requireOutput:!0,feedbackEligibleForScoring:!0})}if(t){let d=this.feedbackToText(t.feedback),m=t.example,g=this.hasOutputFields(m),f=this.buildObservedUpdateExample(m,t.prediction);if(d&&f){p(f,{feedback:d,requireOutput:!0,includeInExamples:!1,feedbackEligibleForScoring:!1});let x=this.formatObservedUpdateFeedback(m,t.prediction,d);x&&a.push(x)}g&&p(m,{feedback:d,feedbackEligibleForScoring:!0})}if(e.generateExamples||n.length===0){let d=e.synthCount??20,g=await new _n(this.gen.getSignature(),{teacher:e.teacher,...e.synthOptions}).generate(d);for(let f of g.examples)p({...f.input,...f.expected})}return e.mode==="continuous"&&o.length>0?{examples:[...o,...n.filter(m=>!o.some(g=>this.exampleKey(g)===this.exampleKey(m)))],feedbackExamples:r.slice(0,e.continuousOptions.feedbackWindowSize),feedbackScoredExamples:o.slice(0,e.continuousOptions.feedbackWindowSize),feedbackTextByKey:i,feedbackNotes:a}:{examples:n,feedbackExamples:r.slice(0,e.continuousOptions.feedbackWindowSize),feedbackScoredExamples:o.slice(0,e.continuousOptions.feedbackWindowSize),feedbackTextByKey:i,feedbackNotes:a}}async loadRelevantTraces(e){let n=[...await this.options.storage.load(this.options.name,{type:"trace"})].sort((i,a)=>{let u=new Date(i.endTime??i.startTime).getTime();return new Date(a.endTime??a.startTime).getTime()-u}),r=e.mode==="continuous"?n.slice(0,e.continuousOptions.maxRecentTraces):n,o=new Map;for(let i of r){if(i.error)continue;let a=this.normalizeExample({...i.input??{},...i.output??{}});if(!a||!this.hasOutputFields(a))continue;let u=this.exampleKey(a),l=this.feedbackToText(i.feedback),c=o.get(u);if(!c){o.set(u,{example:a,feedback:l});continue}!c.feedback&&l&&(c.feedback=l)}return[...o.values()]}normalizeExample(e){let t={},n=this.gen.getSignature(),r=[...n.getInputFields(),...n.getOutputFields()];for(let o of r)o.name in e&&e[o.name]!==void 0&&(t[o.name]=e[o.name]);if(Object.keys(t).length!==0)return t}buildObservedUpdateExample(e,t){return this.normalizeExample({...this.pickInputFields(e),...this.pickOutputFields(t)})}pickInputFields(e){let t={};for(let n of this.gen.getSignature().getInputFields())e[n.name]!==void 0&&(t[n.name]=e[n.name]);return t}pickOutputFields(e){let t={};for(let n of this.gen.getSignature().getOutputFields())e[n.name]!==void 0&&(t[n.name]=e[n.name]);return t}hasInputFields(e){return this.gen.getSignature().getInputFields().some(t=>e[t.name]!==void 0)}hasOutputFields(e){return this.gen.getSignature().getOutputFields().some(t=>e[t.name]!==void 0)}exampleKey(e){let t=this.gen.getSignature(),r=[...t.getInputFields(),...t.getOutputFields()].map(o=>o.name).reduce((o,i)=>(e[i]!==void 0&&(o[i]=e[i]),o),{});return JSON.stringify(r)}feedbackToText(e){if(!e)return;if(typeof e=="string")return e.trim()||void 0;let t=[];return typeof e.score=="number"&&t.push(`User score: ${e.score}.`),e.label&&t.push(`User label: ${e.label}.`),e.comment&&t.push(e.comment.trim()),t.join(" ").trim()||void 0}formatObservedUpdateFeedback(e,t,n){let r=this.pickInputFields(e),o=this.pickOutputFields(t);return["Observed continuous update event.",`Inputs: ${JSON.stringify(r)}`,`Observed output: ${JSON.stringify(o)}`,`User feedback: ${n}`].join(`
609
+ `)}splitExamples(e,t,n,r){let o=this.shuffleExamples(e),i=Number.isFinite(n)?Math.min(Math.max(n,.05),.5):.2,a=Math.min(o.length-1,Math.max(1,Math.round(o.length*i)));if(r==="continuous"&&t.length>0){let u=new Set(t.map(p=>this.exampleKey(p))),l=[];for(let p of o)!u.has(this.exampleKey(p))&&l.length<a&&l.push(p);for(let p of o){if(l.length>=a)break;l.some(d=>this.exampleKey(d)===this.exampleKey(p))||l.push(p)}let c=new Set(l.map(p=>this.exampleKey(p)));return{trainingExamples:o.filter(p=>!c.has(this.exampleKey(p))),validationExamples:l}}return{trainingExamples:o.slice(0,o.length-a),validationExamples:o.slice(o.length-a)}}shuffleExamples(e){let t=[...e];for(let n=t.length-1;n>0;n-=1){let r=Math.floor(Math.random()*(n+1));[t[n],t[r]]=[t[r],t[n]]}return t}computeMetricBudget(e,t){let n=Math.max(e,1),r=Math.max(t,1);return Math.max(r*(n+2),20)}createPromptState(e,t,n){let r=this.gen.getInstruction(),o={mode:e,instruction:r,baseInstruction:r,score:t};return e==="continuous"&&(o.continuous={feedbackTraceCount:n,lastUpdateAt:new Date().toISOString()}),this.currentState=o,o}createPlaybookState(e,t,n){let r=this.gen.getSignature().getDescription()??this.gen.getInstruction(),o={mode:"playbook",instruction:r,baseInstruction:n??this.currentState?.baseInstruction??r,score:this.currentScore,playbook:e,artifactSummary:t};return this.currentState=o,o}summarizePlaybookArtifact(e){return{feedbackEvents:e.feedback.length,historyBatches:e.history.length,bulletCount:e.playbook.stats.bulletCount,updatedAt:e.playbook.updatedAt}}async saveCheckpoint(e){let r=(await this.options.storage.load(this.options.name,{type:"checkpoint"})).reduce((i,a)=>Math.max(i,a.version??0),0)+1,o={type:"checkpoint",name:this.options.name,version:r,createdAt:new Date,instruction:e.state.instruction??this.gen.getInstruction(),score:e.score,optimizerType:"learn",learnState:e.state,metadata:{mode:e.mode,budget:e.budget,trainingExamples:e.stats.trainingExamples,validationExamples:e.stats.validationExamples,feedbackExamples:e.stats.feedbackExamples,durationMs:e.stats.durationMs}};return await this.options.storage.save(this.options.name,o),this.currentScore=e.score,this.currentState=e.state,r}async loadLatestCheckpoint(){try{let e=await this.options.storage.load(this.options.name,{type:"checkpoint"});if(e.length===0)return;let t=[...e].sort((r,o)=>{let i=(o.version??0)-(r.version??0);return i!==0?i:new Date(o.createdAt).getTime()-new Date(r.createdAt).getTime()})[0];if(!t)return;let n=t.learnState?.instruction??t.instruction??void 0;t.learnState?.mode==="playbook"&&t.learnState.playbook?this.applyRestoredPlaybook(t.learnState.playbook,t.learnState.baseInstruction,n):n&&this.gen.setInstruction(n),this.currentScore=t.learnState?.score??t.score,this.currentState=t.learnState,t.learnState?.mode==="playbook"&&(this.playbookOptimizer=void 0),this.tracer=this.tracer.clone(this.gen)}catch{}}applyRestoredPlaybook(e,t,n){let o=[(t??this.gen.getSignature().getDescription()??n??"").trim(),"",Gt(e)].filter(i=>i&&i.trim().length>0).join(`
596
610
 
597
- `);e.setDescription(r)}},ss=class extends rt{aceConfig;playbook;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...Vp,...t},this.playbook=t?.initialPlaybook!==void 0?Yt(t.initialPlaybook):$i()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?Yt(this.aceConfig.initialPlaybook):$i(),this.generatorHistory=[],this.deltaHistory=[]}configureAuto(e){switch(e){case"light":this.aceConfig.maxEpochs=1,this.aceConfig.maxReflectorRounds=1;break;case"medium":this.aceConfig.maxEpochs=2,this.aceConfig.maxReflectorRounds=2;break;case"heavy":this.aceConfig.maxEpochs=3,this.aceConfig.maxReflectorRounds=3;break}}async compile(e,t,n,r){let o=r?.aceOptions;o&&(Object.assign(this.aceConfig,{maxEpochs:o.maxEpochs??this.aceConfig.maxEpochs,maxReflectorRounds:o.maxReflectorRounds??this.aceConfig.maxReflectorRounds,maxSectionSize:o.maxSectionSize??this.aceConfig.maxSectionSize,similarityThreshold:o.similarityThreshold??this.aceConfig.similarityThreshold,allowDynamicSections:o.allowDynamicSections??this.aceConfig.allowDynamicSections}),o.initialPlaybook&&(this.playbook=Yt(o.initialPlaybook)));let s=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.generatorHistory=[],this.deltaHistory=[];let u=Number.NEGATIVE_INFINITY,c=0,p=Math.max(this.aceConfig.maxEpochs,1),d=p*t.length;try{for(let C=0;C<p;C++)for(let x=0;x<t.length;x++){let I=t[x],w=this.composeInstruction(a??l,this.playbook);e.setDescription?.(w);let A=await e.forward(this.studentAI,I);this.stats.totalCalls+=1;let T=await n({prediction:A,example:I});typeof T=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,T),u=Math.max(u,T));let k=A?.severity,E=I?.severity,M=this.createGeneratorOutput(A,I),$=E&&k&&E!==k,B=await this.runReflectionRounds({example:I,generatorOutput:M,feedback:E&&k&&E!==k?`Expected severity "${E}" but model predicted "${k}".`:void 0}),V=await this.runCurator({program:e,example:I,reflection:B,playbook:this.playbook}),O=this.normalizeCuratorOperations(V?.operations);O.length===0&&$&&(O=this.inferOperationsFromReflection(B)),O=this.resolveCuratorOperationTargets(O,this.playbook,B,M);let j=V||O.length>0?{...V??{},operations:O}:void 0,F=[];if(O.length>0){let N=this.collectProtectedBulletIds(O),J=Li(this.playbook,O,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:N});F=J.updatedBulletIds,J.autoRemoved.length>0&&(O.push(...J.autoRemoved),j&&(j.operations=O))}if(B?.bulletTags)for(let N of B.bulletTags)Di(this.playbook,N.id,N.tag);O.length>0&&F.length>0&&Gi(this.playbook,this.aceConfig.similarityThreshold);let W={example:I,prediction:A,score:typeof T=="number"?T:0,generatorOutput:M,reflection:B,curator:j,timestamp:new Date().toISOString()};this.generatorHistory.push(W),F.length>0&&j?.operations?.length&&this.deltaHistory.push({epoch:C,exampleIndex:x,operations:j.operations}),c+=1,this.currentRound=c;let P=typeof T=="number"&&Number.isFinite(T)?T:0,D=Number.isFinite(u)?u:P,K={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,P,{epoch:C,exampleIndex:x,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},D,{playbookBullets:this.playbook.stats.bulletCount},void 0,K),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,P)}}finally{e.setDescription?.(l)}let m=Date.now()-s;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(u)?u:0;let g={playbook:Yt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},h=new Lr({baseInstruction:a??l,playbook:this.playbook,artifact:g,bestScore:Number.isFinite(u)?u:0,stats:this.stats,optimizerType:"ACE",optimizationTime:m,totalRounds:c,converged:this.stats.convergenceInfo.converged});return{stats:this.stats,bestScore:Number.isFinite(u)?u:0,finalConfiguration:{strategy:"ace",epochs:p},optimizedProgram:h,playbook:Yt(this.playbook),artifact:g}}async applyOnlineUpdate(e){if(!this.program)throw new Error("AxACE: `compile` must be run before `applyOnlineUpdate`");let t=this.createGeneratorOutput(e.prediction,e.example),n=e.prediction?.severity,r=e.example?.severity,o=await this.runReflectionRounds({example:e.example,generatorOutput:t,feedback:e.feedback??(r&&n&&r!==n?`Expected severity "${r}" but model predicted "${n}".`:void 0)}),s=await this.runCurator({program:this.program,example:e.example,reflection:o,playbook:this.playbook}),a=this.normalizeCuratorOperations(s?.operations),l=r&&n&&r!==n;a.length===0&&l&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let u=s||a.length>0?{...s??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)Di(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=Li(this.playbook,a,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:p});d.autoRemoved.length>0&&(a.push(...d.autoRemoved),u&&(u.operations=a)),Gi(this.playbook,this.aceConfig.similarityThreshold)}let c={example:e.example,prediction:e.prediction,score:0,generatorOutput:t,reflection:o,curator:u,timestamp:new Date().toISOString()};return this.generatorHistory.push(c),u}composeInstruction(e,t){return[e.trim(),"",$r(t)].filter(r=>r.trim().length>0).join(`
598
-
599
- `)}async extractProgramInstruction(e){try{return e.getSignature().getDescription()??void 0}catch{return}}createGeneratorOutput(e,t){let n=e?.thought?.toString()??"",r=Array.isArray(e?.bullet_ids)?e?.bullet_ids:[];return{reasoning:n,answer:e,bulletIds:r,trajectory:JSON.stringify({example:t,prediction:e}),metadata:{predictedSeverity:e?.severity,expectedSeverity:t?.severity}}}resolveCuratorOperationTargets(e,t,n,r){if(!e.length)return e;let o=[],s=new Set(e.map(c=>c.bulletId).filter(c=>typeof c=="string")),a=new Map,l=(c,p)=>{if(s.has(c))return;let d=this.locateBullet(t,c);if(!d)return;let m=a.get(d.section)??{harmful:[],primary:[],generator:[]};m[p].push(d.id),a.set(d.section,m)};for(let c of n?.bulletTags??[]){let p=c.tag==="harmful"?"harmful":"primary";l(c.id,p)}if(r?.bulletIds)for(let c of r.bulletIds)l(c,"generator");let u=c=>{let p=a.get(c);if(!p)return this.locateFallbackBullet(t,c,s);let d=g=>{for(;g.length>0;){let h=g.shift();if(!s.has(h))return h}},m=d(p.harmful)??d(p.primary)??d(p.generator);return m||this.locateFallbackBullet(t,c,s)};for(let c of e){if((c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId){let p=u(c.section);p&&(c.bulletId=p,s.add(p))}(c.type==="UPDATE"||c.type==="REMOVE")&&!c.bulletId||o.push(c)}return o}locateBullet(e,t){for(let n of Object.values(e.sections)){let r=n.find(o=>o.id===t);if(r)return r}}locateFallbackBullet(e,t,n){let r=e.sections[t]??[];for(let o of r)if(!n.has(o.id))return o.id}collectProtectedBulletIds(e){let t=new Set;for(let n of e)n.type==="UPDATE"&&n.bulletId&&t.add(n.bulletId);return t}normalizeCuratorOperations(e){if(!e)return[];if(Array.isArray(e)){let t=[],n=new Set;for(let r of e){if(!r||typeof r!="object")continue;let o=r.type??"ADD",s=typeof o=="string"?o.toUpperCase():"ADD",a=s==="UPDATE"?"UPDATE":s==="REMOVE"?"REMOVE":"ADD",l=r.section??"Guidelines",u=typeof l=="string"&&l.trim().length>0?l.trim():"Guidelines",c=r.content??"",p=typeof c=="string"?c.trim():"";if(a!=="REMOVE"&&p.length===0)continue;let d=r.bulletId??r.id,m=typeof d=="string"&&d.trim().length>0?d.trim():void 0,h=[a,u,p,m??""].join(":");if(n.has(h))continue;n.add(h);let f={type:a,section:u};a!=="REMOVE"&&(f.content=p),m&&(f.bulletId=m);let C=r.metadata;C&&typeof C=="object"&&(f.metadata={...C}),t.push(f)}return t}if(typeof e=="string")try{let t=JSON.parse(e);return this.normalizeCuratorOperations(t)}catch{return[]}if(typeof e=="object"){let t=e;if(t&&Array.isArray(t.operations))return this.normalizeCuratorOperations(t.operations);if(t&&typeof t.operations=="string")try{let n=JSON.parse(t.operations);return this.normalizeCuratorOperations(n)}catch{return[]}return[]}return[]}inferOperationsFromReflection(e){if(!e)return[];let t=[],n=new Set,r=(o,s)=>{if(!s)return;let a=s.trim();if(!a||a.toLowerCase().startsWith("no error"))return;let u=`${o}:${a}`;n.has(u)||(n.add(u),t.push({type:"ADD",section:o,content:a}))};return r("Guidelines",e.keyInsight),r("Response Strategies",e.correctApproach),r("Common Pitfalls",e.errorIdentification),r("Root Cause Notes",e.rootCauseAnalysis),t}async runReflectionRounds({example:e,generatorOutput:t,feedback:n}){let r=Math.max(this.aceConfig.maxReflectorRounds,1),o;for(let s=0;s<r;s++){let a=await this.runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:o});if(!a)break;o=a;let l=a.errorIdentification?.toLowerCase().trim()??"";if(a.metadata?.resolved===!0||l.length===0||l.startsWith("no error")||l.startsWith("resolved"))break}return o}async runReflector({example:e,generatorOutput:t,feedback:n,previousReflection:r}){let o=this.getOrCreateReflectorProgram(),s=this.teacherAI??this.studentAI;try{let a={severity:e?.severity,policyHint:e?.policyHint},c=(this.program?.getSignature()?.getInputFields()??[]).reduce((d,m)=>(m.name in e&&(d[m.name]=e[m.name]),d),{});return await o.forward(s,{question:JSON.stringify(c),generator_answer:JSON.stringify(t.answer),generator_reasoning:t.reasoning,playbook:JSON.stringify({markdown:$r(this.playbook),structured:this.playbook}),expected_answer:a.severity||a.policyHint?JSON.stringify(a):void 0,feedback:n,previous_reflection:r?JSON.stringify(r):void 0})}catch(a){this.verbose&&console.warn("[AxACE] Reflector error:",a instanceof Error?a.message:a);return}}async runCurator({program:e,example:t,reflection:n,playbook:r}){if(!n)return;let o=this.getOrCreateCuratorProgram(),s=this.teacherAI??this.studentAI,u=e.getSignature().getInputFields().reduce((c,p)=>(p.name in t&&(c[p.name]=t[p.name]),c),{});try{return await o.forward(s,{playbook:JSON.stringify({markdown:$r(r),structured:r}),reflection:JSON.stringify(n),question_context:JSON.stringify(u),token_budget:1024})}catch(c){this.verbose&&console.warn("[AxACE] Curator error:",c instanceof Error?c.message:c);return}}getOrCreateReflectorProgram(){if(!this.reflectorProgram){let e=xe().input("question",xe.string("Original task input serialized as JSON")).input("generator_answer",xe.string("Generator output serialized as JSON")).input("generator_reasoning",xe.string("Generator reasoning trace").optional()).input("playbook",xe.string("Current context playbook rendered as markdown")).input("expected_answer",xe.string("Expected output when ground truth is available").optional()).input("feedback",xe.string("External feedback or reward signal").optional()).input("previous_reflection",xe.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",xe.string("Step-by-step analysis of generator performance")).output("errorIdentification",xe.string("Specific mistakes detected")).output("rootCauseAnalysis",xe.string("Underlying cause of the error")).output("correctApproach",xe.string("What the generator should do differently")).output("keyInsight",xe.string("Reusable insight to remember")).output("bulletTags",xe.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=Ye(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=xe().input("playbook",xe.string("Current playbook serialized as JSON")).input("reflection",xe.string("Latest reflection output serialized as JSON")).input("question_context",xe.string("Original task input serialized as JSON")).input("token_budget",xe.number("Approximate token budget for curator response").optional()).output("reasoning",xe.string("Justification for the proposed updates")).output("operations",xe.json("List of operations with type/section/content fields")).build();this.curatorProgram=Ye(e)}return this.curatorProgram}};var Hn=class extends rt{maxRounds;maxDemos;maxExamples;batchSize;earlyStoppingPatience;costMonitoring;maxTokensPerGeneration;verboseMode;debugMode;traces=[];constructor(e){super(e);let t=e.options||{};this.maxRounds=t.maxRounds??3,this.maxDemos=t.maxDemos??4,this.maxExamples=t.maxExamples??16,this.batchSize=t.batchSize??1,this.earlyStoppingPatience=t.earlyStoppingPatience??0,this.costMonitoring=t.costMonitoring??!1,this.maxTokensPerGeneration=t.maxTokensPerGeneration??0,this.verboseMode=t.verboseMode??!0,this.debugMode=t.debugMode??!1}async compileRound(e,t,n,r,o){let s=Date.now(),a=o?.maxDemos??this.maxDemos,l={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(l.modelConfig.max_tokens=this.maxTokensPerGeneration);let u=Qp([...t],this.maxExamples),c=this.traces.length,p=e.getId();for(let d=0;d<u.length;d+=this.batchSize){d>0&&(l.modelConfig.temperature=.7+.001*d);let m=u.slice(d,d+this.batchSize);for(let g of m){if(!g||typeof g!="object")continue;let h=t.filter(x=>x!==g);e.setDemos([{traces:h,programId:p}]);let f=this.getTeacherOrStudentAI();this.stats.totalCalls++;let C;try{let x={...l,maxRetries:1};C=await e.forward(f,g,x),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(C).length/4),await r({prediction:C,example:g})>=.5&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(x){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${x instanceof Error?x.message:"Unknown error"}`),C={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let m=this.traces.length-c;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:m>0?n:0,patienceExhausted:!1,reason:"No improvement detected"};else if(m>0)this.stats.earlyStopping.bestScoreRound=n;else if(n-this.stats.earlyStopping.bestScoreRound>=this.earlyStoppingPatience){this.stats.earlyStopping.patienceExhausted=!0,this.stats.earlyStopped=!0,this.stats.earlyStopping.reason=`No improvement for ${this.earlyStoppingPatience} rounds`;return}}}async compile(e,t,n,r){this.validateExamples(t,!1);let o=r?.maxIterations??this.maxRounds;this.traces=[],this.reset();for(let l=0;l<o&&(await this.compileRound(e,t,l,n,r),!this.stats.earlyStopped);l++);if(this.traces.length===0)throw new Error("No demonstrations found. Either provide more examples or improve the existing ones.");let s=Jp(this.traces),a=0;return this.traces.length>0&&(a=this.stats.successfulDemos/Math.max(1,this.stats.totalCalls)),await this.logOptimizationComplete("BootstrapFewShot",a,{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a,demosGenerated:s.length,tracesCollected:this.traces.length},r),{demos:s,stats:this.stats,bestScore:a,finalConfiguration:{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a}}}};function Jp(i){let e=new Map;for(let n of i)if(e.has(n.programId)){let r=e.get(n.programId);r&&r.push(n.trace)}else e.set(n.programId,[n.trace]);let t=[];return e.forEach((n,r)=>{t.push({traces:n,programId:r})}),t}var Qp=(i,e)=>{let t=[...i];for(let n=t.length-1;n>0;n--){let r=Math.floor(Math.random()*(n+1)),o=t[n],s=t[r];if(!o||!s)throw new Error("Invalid array elements");[t[n],t[r]]=[s,o]}return t.slice(0,e)};var is=class extends rt{numTrials;minibatch;minibatchSize;earlyStoppingTrials;minImprovementThreshold;sampleCount;crossoverEvery;tieEpsilon;paretoSetSize;mergeMax;mergesUsed=0;mergesDue=0;totalMergesTested=0;lastIterFoundNewProgram=!1;rngState;mergeAttemptKeys=new Set;mergeCompositionKeys=new Set;samplerState={epoch:-1,shuffled:[],freq:new Map};constructor(e){super(e),this.numTrials=e.numTrials??24,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??8,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??0,this.sampleCount=e.sampleCount??1,this.crossoverEvery=Math.max(0,Math.floor(e?.crossoverEvery??Math.max(3,Math.floor(this.numTrials/3)))),this.tieEpsilon=Number.isFinite(e?.tieEpsilon)?Number(e?.tieEpsilon):0;let t=e?.seed,n=Number.isFinite(t)?Math.floor(Number(t)):0;this.rngState=n&&n!==0?n:123456789;let r=e?.paretoSetSize;this.paretoSetSize=r&&r>0?Math.min(1e3,Math.max(5,Math.floor(r))):Math.max(10,Math.min(200,this.minibatchSize*3));let o=e?.mergeMax;this.mergeMax=Math.max(0,Math.floor(o??5)),this.mergesUsed=0,this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold,this.mergesUsed=0,this.mergesDue=0,this.totalMergesTested=0,this.lastIterFoundNewProgram=!1,this.mergeAttemptKeys.clear(),this.mergeCompositionKeys.clear(),this.samplerState.epoch=-1,this.samplerState.shuffled=[],this.samplerState.freq.clear()}configureAuto(e){switch(e){case"light":this.numTrials=8,this.minibatch=!0,this.minibatchSize=6;break;case"medium":this.numTrials=16,this.minibatch=!0,this.minibatchSize=10;break;case"heavy":this.numTrials=28,this.minibatch=!0,this.minibatchSize=14;break}}async compile(e,t,n,r){let o=Date.now(),s=e;this.validateExamples(t),r?.auto&&this.configureAuto(r.auto);let a=s.getNodePrograms?.();if(!a||a.length===0)throw new Error("AxGEPAFlow: flow has no nodes to optimize");let l=r?.validationExamples,u=r?.feedbackExamples,c=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),p=u&&u.length>0?u:t;this.getOptimizerLogger(r)?.({name:"OptimizationStart",value:{optimizerType:"GEPA-Flow",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch}}});let m=async(O,j)=>{try{s.setAllNodeInstructions?.(O);let F=await s.forward(this.studentAI,j,{sampleCount:this.sampleCount});return this.stats.totalCalls+=1,await n({prediction:F,example:j})||{}}catch{return{}}},g=async(O,j)=>{let F=[];for(let W of j)F.push(await m(O,W));return ns(F)},h={};for(let O of a)h[O.name]=await this.getBaseInstruction(O.program);let f=[{cfg:{...h},parent:void 0,scores:await g(h,c)}],C=O=>{let j=r?.paretoMetricKey,F=r?.paretoScalarize;if(typeof F=="function")return F(O);if(j)return Number.isFinite(O[j])?O[j]:0;let W=Object.values(O);return W.length?W.reduce((P,D)=>P+D,0)/W.length:0},x=[],I=async(O,j)=>{let F=[];for(let W of j){let P=await m(O,W);F.push(C(P))}return F};x.push(await I(h,c));let w=vt(f.map((O,j)=>({idx:j,scores:O.scores})),this.tieEpsilon).map(O=>O.idx),A=0,T=new Set,k=r?.maxMetricCalls;if(!Number.isFinite(k)||k<=0)throw new Error("AxGEPA-Flow: options.maxMetricCalls must be set to a positive integer");let E=Math.floor(k);for(let O=0;O<this.numTrials&&!(E!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(E)));O++){let j=x[0]?.length??0,F=[];for(let b=0;b<j;b++){let S=Number.NEGATIVE_INFINITY,R=new Set;for(let y=0;y<x.length;y++){let _=x[y][b];_>S+this.tieEpsilon?(S=_,R.clear(),R.add(y)):Math.abs(_-S)<=this.tieEpsilon&&R.add(y)}F.push(R)}let W=x.map(b=>Qt(b));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let b=_r(F,W),S=new Set;for(let L of b)for(let G of L)S.add(G);let R=Array.from(S),y=L=>{let G=[],U=L;for(;U!==void 0;)G.push(U),U=f[U]?.parent;return G},_=L=>L.length?L[Math.floor(this.rand()*L.length)]:void 0,q;for(let L=0;L<10&&!q&&!(R.length<2);L++){let G=_(R),U=_(R);if(G===U)continue;U<G&&([G,U]=[U,G]);let H=new Set(y(G)),Y=new Set(y(U));if(H.has(U)||Y.has(G))continue;let ne=[...H].filter(de=>Y.has(de));if(ne.length===0)continue;let ee=[];for(let de of ne){let Re=f[de].cfg,Xe=f[G].cfg,_e=f[U].cfg,Ze=!1,ft=new Set([...Object.keys(Re),...Object.keys(Xe),...Object.keys(_e)]);for(let it of ft){let xt=Re[it],Ot=Xe[it],v=_e[it];if(Ot===xt&&v!==Ot||v===xt&&Ot!==v){Ze=!0;break}}Ze&&ee.push(de)}if(ee.length===0)continue;let pe=ee.map(de=>Math.max(1e-9,W[de])),fe=this.rand()*pe.reduce((de,Re)=>de+Re,0),Ie=ee[ee.length-1];for(let de=0;de<ee.length;de++){if(fe<pe[de]){Ie=ee[de];break}fe-=pe[de]}let Se=W[Ie],Pe=W[G],Q=W[U];if(Se>Math.min(Pe,Q))continue;let X=`${G}|${U}|${Ie}`;if(this.mergeAttemptKeys.has(X))continue;this.mergeAttemptKeys.add(X);let ue=`${G}|${U}|${Ie}`;T.has(ue)||(q={i:G,j:U,a:Ie})}if(this.lastIterFoundNewProgram=!1,q){let{i:L,j:G,a:U}=q,{cfg:H,descSig:Y}=this.systemAwareMergeWithSig(f,L,G,(v,z)=>W[v]>=W[z]?v:z),ne=`${Math.min(L,G)}|${Math.max(L,G)}|${Y}`;if(this.mergeCompositionKeys.has(ne))continue;this.mergeCompositionKeys.add(ne);let ee=x[L],pe=x[G],fe=Array.from({length:ee.length},(v,z)=>z),Ie=fe.filter(v=>(ee[v]??0)>(pe[v]??0)),Se=fe.filter(v=>(pe[v]??0)>(ee[v]??0)),Pe=fe.filter(v=>!(Ie.includes(v)||Se.includes(v))),Q=5,X=Math.ceil(Q/3),ue=(v,z)=>{if(z<=0||v.length===0)return[];if(v.length<=z)return[...v];let Ce=[],ke=new Set;for(;Ce.length<z;){let ae=Math.floor(this.rand()*v.length);ke.has(ae)||(ke.add(ae),Ce.push(v[ae]))}return Ce},de=[];de.push(...ue(Ie,Math.min(X,Ie.length))),de.push(...ue(Se,Math.min(X,Se.length)));let Re=Q-de.length;de.push(...ue(Pe,Math.max(0,Re)));let Xe=Q-de.length;if(Xe>0){let v=fe.filter(z=>!de.includes(z));de.push(...ue(v,Math.min(Xe,v.length)))}let _e=de.slice(0,Math.min(Q,fe.length)),Ze=_e.map(v=>c[v]),it=(await I(H,Ze)).reduce((v,z)=>v+z,0),xt=_e.reduce((v,z)=>v+(ee[z]??0),0),Ot=_e.reduce((v,z)=>v+(pe[z]??0),0);if(it>=Math.max(xt,Ot)+this.tieEpsilon){let v=await g(H,c);f.push({cfg:H,parent:U,scores:v}),x.push(await I(H,c));let z=w.length,Ce=gt(w.map(ae=>f[ae].scores))??0;w=vt(f.map((ae,Ne)=>({idx:Ne,scores:ae.scores})),this.tieEpsilon).map(ae=>ae.idx);let ke=gt(w.map(ae=>f[ae].scores))??0;(w.length>z||ke>Ce+1e-6)&&(A=0),this.mergesDue-=1,this.totalMergesTested+=1,T.add(`${Math.min(L,G)}|${Math.max(L,G)}|${U}`)}continue}}let P=Nr(F,W,()=>this.rand());this.lastIterFoundNewProgram=!1;let D=this.minibatch?this.nextMinibatchIndices(p.length,O).map(b=>p[b]):p;if(r?.skipPerfectScore??!0){let b=Number(r?.perfectScore??1),S=await I(f[P].cfg,D);if(S.length>0&&S.every(R=>R>=b))continue}let K=!1,N={...f[P].cfg},J="reflective_mutation",ye=O%a.length,oe=a[ye],re,le;if(K&&this.mergesUsed<this.mergeMax){let b=(P+1)%f.length,S=L=>{let G=[],U=L;for(;U!==void 0;)G.push(U),U=f[U]?.parent;return G},R=S(P),y=S(b),_=R.find(L=>y.includes(L)),q=!0;if(_||(q=!1),(y.includes(P)||R.includes(b))&&(q=!1),q){let L=f[_].cfg,G=f[P].cfg,U=f[b].cfg,H=!1,Y=new Set([...Object.keys(L),...Object.keys(G),...Object.keys(U)]);for(let ne of Y){let ee=L[ne],pe=G[ne],fe=U[ne];if(pe===ee&&fe!==pe||fe===ee&&pe!==fe){H=!0;break}}H||(q=!1)}if(q){let L=Math.min(P,b),G=Math.max(P,b),U=`${L}|${G}|${_}`;if(!T.has(U)){let H=C(f[_].scores),Y=C(f[P].scores),ne=C(f[b].scores);H<=Math.min(Y,ne)&&(N=this.systemAwareMerge(f,P,b,(ee,pe)=>{let fe=C(f[ee].scores),Ie=C(f[pe].scores);return fe>=Ie?ee:pe}),J="system_merge",this.mergesUsed+=1,T.add(U))}}else{let L=f[P].cfg[oe.name],G=r?.gepaAdapter,U;if(G)try{let H=await G.evaluate(D,{...f[P].cfg},!0);re=Array.isArray(H?.scores)?H.scores.reduce((pe,fe)=>pe+(Number(fe)||0),0):void 0;let Y=G.make_reflective_dataset({...f[P].cfg},H,[oe.name]),ee=(await G.propose_new_texts?.({...f[P].cfg},Y,[oe.name]))?.[oe.name];typeof ee=="string"&&ee.length>0&&(U=ee)}catch{}if(U||(U=await this.reflectModuleInstruction(oe.name,L,s,a,{...f[P].cfg},D,async({prediction:H,example:Y})=>{let ne=await n({prediction:H,example:Y}),ee=Object.values(ne||{});return ee.length?ee.reduce((pe,fe)=>pe+fe,0)/ee.length:0},r)),N[oe.name]=U,G&&re!==void 0)try{let H=await G.evaluate(D,N,!1);le=Array.isArray(H?.scores)?H.scores.reduce((Y,ne)=>Y+(Number(ne)||0),0):void 0}catch{}}}else{let b=f[P].cfg[oe.name],S=r?.gepaAdapter,R;if(S)try{let y=await S.evaluate(D,{...f[P].cfg},!0);re=Array.isArray(y?.scores)?y.scores.reduce((G,U)=>G+(Number(U)||0),0):void 0;let _=S.make_reflective_dataset({...f[P].cfg},y,[oe.name]),L=(await S.propose_new_texts?.({...f[P].cfg},_,[oe.name]))?.[oe.name];typeof L=="string"&&L.length>0&&(R=L)}catch{}if(R||(R=await this.reflectModuleInstruction(oe.name,b,s,a,{...f[P].cfg},D,async({prediction:y,example:_})=>{let q=await n({prediction:y,example:_}),L=Object.values(q||{});return L.length?L.reduce((G,U)=>G+U,0)/L.length:0},r)),N[oe.name]=R,S&&re!==void 0)try{let y=await S.evaluate(D,N,!1);le=Array.isArray(y?.scores)?y.scores.reduce((_,q)=>_+(Number(q)||0),0):void 0}catch{}}let we=await I(f[P].cfg,D),Ue=await I(N,D),se=we.reduce((b,S)=>b+S,0),ve=Ue.reduce((b,S)=>b+S,0);if(this.currentRound=O+1,await this.updateOptimizationProgress(this.currentRound,ve,{modules:a.length,mutatedModule:oe.name,totalRounds:this.numTrials},"GEPA-Flow",{strategy:J,paretoSetSize:c.length},ve,{idx:P},{...r??{},maxIterations:this.numTrials}),!(ve>se+this.tieEpsilon&&(re===void 0||le===void 0||le>re+this.tieEpsilon))){if(++A>=this.earlyStoppingTrials)break;continue}let ce=await g(N,c);f.push({cfg:N,parent:P,scores:ce}),x.push(await I(N,c));let be=w.length,Z=gt(w.map(b=>f[b].scores))??0;w=vt(f.map((b,S)=>({idx:S,scores:b.scores})),this.tieEpsilon).map(b=>b.idx);let Oe=gt(w.map(b=>f[b].scores))??0;if(w.length>be||Oe>Z+1e-6)A=0;else if(A++,A>=this.earlyStoppingTrials)break;this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let M=vt(f.map((O,j)=>({idx:j,scores:O.scores})),this.tieEpsilon),$=M.length>0?Math.max(...M.map(O=>C(O.scores))):0,B=gt(M.map(O=>O.scores));this.stats.convergenceInfo.converged=!0;let V=this.getMergedCustomLabels(r);return this.recordParetoMetrics(M.length,f.length,"GEPA-Flow",B,V),{demos:[],stats:this.stats,bestScore:$,paretoFront:M.map(O=>({demos:[],scores:O.scores,configuration:{candidate:O.idx},dominatedSolutions:O.dominated})),paretoFrontSize:M.length,hypervolume:B,finalConfiguration:{strategy:"gepa_flow_pareto",candidates:f.length}}}async getBaseInstruction(e){let t=e?.getInstruction?.();if(t&&t.length>0)return t;let r=e?.getSignature?.()?.getDescription?.();return r&&r.length>0?r:"Follow the task precisely. Be concise, correct, and consistent."}async evaluateOnSet(e,t,n,r){let o=[];for(let s of n){let a=await this.evaluateOne(e,t,s,r);o.push(a)}return o}async evaluateAvg(e,t,n,r){let o=await this.evaluateOnSet(e,t,n,r);return o.length>0?Qt(o):0}async evaluateOne(e,t,n,r){try{e.setAllNodeInstructions?.(t);let o=await e.forward(this.studentAI,n,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let s=await r({prediction:o,example:n});return typeof s=="number"&&!Number.isNaN(s)?((typeof this.targetScore=="number"?s>=this.targetScore:s>=.5)&&(this.stats.successfulDemos+=1),s):0}catch{return 0}}async reflectModuleInstruction(e,t,n,r,o,s,a,l){let u=[],c=[];for(let C of s)try{o[e]=t,n.setAllNodeInstructions?.(o);let x=await n.forward(this.studentAI,C,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let I=await a({prediction:x,example:C});c.push({input:C,prediction:x,score:typeof I=="number"?I:0})}catch{c.push({input:C,prediction:{},score:0})}let p=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=Ye('moduleName:string "Target module", minibatch:json "Array of {input,prediction,score}", evalFeedback?:string[] "Evaluator feedback when available" -> feedbackSummary:string "Concise module-focused feedback"'),m=[],g=l?.feedbackFn;if(typeof g=="function")for(let C of c){let x=g({prediction:C.prediction,example:C.input});x&&(Array.isArray(x)?m.push(...x):m.push(x))}let h="";try{h=(await d.forward(p,{moduleName:e,minibatch:c,evalFeedback:m}))?.feedbackSummary?.trim()||""}catch{}let f=Ye('moduleName:string "Target module", currentInstruction:string "Current instruction", feedbackSummary?:string "Summarized feedback", minibatch:json "Array of {input,prediction,score}" -> newInstruction:string "Improved instruction (1-6 sentences) for the module"');try{let x=(await f.forward(p,{moduleName:e,currentInstruction:t,feedbackSummary:h,minibatch:c}))?.newInstruction?.trim();if(x&&x.length>16)return x}catch{}return`${t.trim()} Focus on step-by-step, module-specific reasoning and factual grounding.`.slice(0,2e3)}updateSamplerShuffled(e){let t=Array.from({length:e},(l,u)=>u);for(let l=t.length-1;l>0;l--){let u=Math.floor(this.rand()*(l+1));[t[l],t[u]]=[t[u],t[l]]}for(let l of t)this.samplerState.freq.set(l,(this.samplerState.freq.get(l)??0)+1);let n=this.minibatchSize,r=e%n,o=r===0?0:n-r,s=Array.from({length:e},(l,u)=>u).sort((l,u)=>(this.samplerState.freq.get(l)??0)-(this.samplerState.freq.get(u)??0)),a=[...t];for(let l=0;l<o;l++){let u=s[l%s.length];a.push(u),this.samplerState.freq.set(u,(this.samplerState.freq.get(u)??0)+1)}this.samplerState.shuffled=a,this.samplerState.epoch+=1}nextMinibatchIndices(e,t){this.samplerState.epoch===-1&&(this.samplerState.epoch=0,this.updateSamplerShuffled(e));let n=this.minibatchSize,r=Math.max(1,Math.floor(this.samplerState.shuffled.length/n)),o=Math.floor(t/r);for(;o>=this.samplerState.epoch;)this.updateSamplerShuffled(e);let s=t*n%this.samplerState.shuffled.length;return this.samplerState.shuffled.slice(s,s+n)}systemAwareMergeWithSig(e,t,n,r){let o=f=>{let C=[],x=f;for(;x!==void 0;)C.push(x),x=e[x]?.parent;return C},s=o(t),a=o(n),u=s.find(f=>a.includes(f))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=[],h=Array.from(new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)])).sort();for(let f of h){let C=c[f],x=p[f],I=d[f];if(x===C&&I!==x)m[f]=I,g.push("j");else if(I===C&&x!==I)m[f]=x,g.push("i");else if(x!==I&&x!==C&&I!==C){let w=r(t,n);m[f]=w===t?x:I,g.push(w===t?"i":"j")}else m[f]=x??I??C,g.push("i")}return{cfg:m,descSig:g.join("|")}}rand(){return this.rngState^=this.rngState<<13,this.rngState^=this.rngState>>>17,this.rngState^=this.rngState<<5,(this.rngState>>>0)/4294967296}systemAwareMerge(e,t,n,r){let o=h=>{let f=[],C=h;for(;C!==void 0;)f.push(C),C=e[C]?.parent;return f},s=o(t),a=o(n),u=s.find(h=>a.includes(h))??t,c=e[u].cfg,p=e[t].cfg,d=e[n].cfg,m={},g=new Set([...Object.keys(c),...Object.keys(p),...Object.keys(d)]);for(let h of g){let f=c[h],C=p[h],x=d[h];if(C===f&&x!==C)m[h]=x;else if(x===f&&C!==x)m[h]=C;else if(C!==x&&C!==f&&x!==f){let I=r(t,n);m[h]=I===t?C:x}else m[h]=C??x??f}return m}};var as=class{endpoint;timeout;retryAttempts;retryDelay;logger;constructor(e){this.endpoint=e.endpoint.replace(/\/$/,""),this.timeout=e.timeout??3e4,this.retryAttempts=e.retryAttempts??3,this.retryDelay=e.retryDelay??1e3,this.logger=e.logger}async healthCheck(){try{return(await this.fetchWithRetry("/health",{method:"GET"})).ok}catch(e){return this.logger?.({name:"Notification",id:"health_check_failed",value:`Health check failed: ${e}`}),!1}}async createOptimizationJob(e){let t=await this.fetchWithRetry("/optimize",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to create optimization job: ${n}`)}return t.json()}async getJobStatus(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get job status: ${n}`)}return t.json()}async cancelJob(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to cancel job: ${n}`)}}async suggestParameters(e){let t=await this.fetchWithRetry(`/studies/${e}/suggest`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){let n=await t.text();throw new Error(`Failed to suggest parameters: ${n}`)}return t.json()}async evaluateTrial(e){let t=await this.fetchWithRetry(`/studies/${e.study_name}/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to evaluate trial: ${n}`)}}async getStudyResults(e){let t=await this.fetchWithRetry(`/studies/${e}/results`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get study results: ${n}`)}return t.json()}async deleteStudy(e){let t=await this.fetchWithRetry(`/studies/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to delete study: ${n}`)}}async listStudies(){let e=await this.fetchWithRetry("/studies",{method:"GET"});if(!e.ok){let t=await e.text();throw new Error(`Failed to list studies: ${t}`)}return e.json()}async waitForJobCompletion(e,t=2e3,n=3e5){let r=Date.now();for(;Date.now()-r<n;){let o=await this.getJobStatus(e);if(["completed","failed","cancelled"].includes(o.status))return o;this.logger?.({name:"Notification",id:"job_status",value:`Job ${e} status: ${o.status}, waiting...`}),await this.sleep(t)}throw new Error(`Job ${e} did not complete within ${n}ms`)}async fetchWithRetry(e,t){let n=`${this.endpoint}${e}`,r=null;for(let o=0;o<this.retryAttempts;o++)try{let s=new AbortController,a=setTimeout(()=>s.abort(),this.timeout),l=await fetch(n,{...t,signal:s.signal});return clearTimeout(a),l}catch(s){r=s,this.logger?.({name:"Notification",id:"retry_attempt",value:`Attempt ${o+1} failed: ${s}`}),o<this.retryAttempts-1&&await this.sleep(this.retryDelay*Math.pow(2,o))}throw new Error(`Request failed after ${this.retryAttempts} attempts: ${r?.message}`)}sleep(e){return new Promise(t=>setTimeout(t,e))}};var ls=class extends rt{maxBootstrappedDemos;maxLabeledDemos;numCandidates;initTemperature;numTrials;minibatch;minibatchSize;minibatchFullEvalSteps;programAwareProposer;dataAwareProposer;viewDataBatchSize;tipAwareProposer;fewshotAwareProposer;earlyStoppingTrials;minImprovementThreshold;bayesianOptimization;acquisitionFunction;explorationWeight;optimizeTopP;sampleCount;pythonClient;localScoreHistory=[];localConfigurationHistory=[];customResultPicker;constructor(e){if(super(e),this.numCandidates=e.numCandidates??5,this.initTemperature=e.initTemperature??.7,this.maxBootstrappedDemos=e.maxBootstrappedDemos??3,this.maxLabeledDemos=e.maxLabeledDemos??4,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??25,this.minibatchFullEvalSteps=e.minibatchFullEvalSteps??10,this.programAwareProposer=e.programAwareProposer??!0,this.dataAwareProposer=e.dataAwareProposer??!0,this.viewDataBatchSize=e.viewDataBatchSize??10,this.tipAwareProposer=e.tipAwareProposer??!0,this.fewshotAwareProposer=e.fewshotAwareProposer??!0,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??.01,this.bayesianOptimization=e.bayesianOptimization??!0,this.acquisitionFunction=e.acquisitionFunction??"expected_improvement",this.explorationWeight=e.explorationWeight??.1,this.optimizeTopP=e.optimizeTopP??!1,this.sampleCount=e.sampleCount??1,this.customResultPicker=e.resultPicker,e.optimizerEndpoint){let t={endpoint:e.optimizerEndpoint,timeout:e.optimizerTimeout??3e4,retryAttempts:e.optimizerRetries??3,logger:n=>{this.logger?.({name:"Notification",id:"python_client",value:typeof n=="string"?n:JSON.stringify(n)})}};this.pythonClient=new as(t)}this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}defaultResultPicker=async e=>{if(e.type==="function"){let o=e.results.findIndex(s=>!s.isError);return o>=0?o:0}let t=new Map;for(let o of e.results){let s=JSON.stringify(o.sample??{}),a=t.get(s);a?a.count+=1:t.set(s,{count:1,firstIndex:o.index})}let n="",r={count:-1,firstIndex:0};for(let[o,s]of t.entries())s.count>r.count&&(r=s,n=o);return t.get(n)?.firstIndex??0};configureAuto(e){switch(e){case"light":this.numCandidates=3,this.numTrials=10,this.minibatch=!0,this.minibatchSize=20;break;case"medium":this.numCandidates=5,this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numCandidates=7,this.numTrials=30,this.minibatch=!0,this.minibatchSize=30;break}}generateTips(){return["Be very specific and detailed in your instructions.","Focus on step-by-step reasoning in your instructions.","Provide clear constraints and guidelines in your instructions.","Keep your instructions concise and to the point.","Emphasize accuracy and precision in your instructions.","Include examples of good outputs in your instructions.","Focus on handling edge cases in your instructions.","Explicitly outline the reasoning process in your instructions."]}async generateProgramSummary(e,t){let r=`
611
+ `);if(typeof this.gen.setDescription=="function"){this.gen.setDescription(o);return}o&&this.gen.setInstruction(o)}};var ns=class{rows=[];baseUrl;dataset;split;config;options;constructor({dataset:e,split:t,config:n,options:r}){this.baseUrl="https://datasets-server.huggingface.co/rows",this.dataset=e,this.split=t,this.config=n,this.options=r}async fetchDataFromAPI(e){try{let t=await fetch(e);if(!t.ok)throw new Error(`Error fetching data: ${t.statusText}`);let n=await t.json();if(!n?.rows)throw new Error("Invalid data format");return n.rows}catch(t){throw console.error("Error fetching data from API:",t),t}}async loadData(){let e=this.options?.offset??0,t=this.options?.length??100,n=encodeURIComponent(this.dataset),r=`${this.baseUrl}?dataset=${n}&config=${this.config}&split=${this.split}&offset=${e}&length=${t}`;return this.rows=await this.fetchDataFromAPI(r),this.rows}setData(e){this.rows=e}getData(){return this.rows}async getRows({count:e,fields:t,renameMap:n}){if(this.rows.length===0)throw new Error("No data loaded, call loadData or setData first.");return this.rows.slice(0,e).map(o=>{let i={};return t.forEach(a=>{let u=a.split("."),l=o.row;for(let p of u)Object.hasOwn(l,p)&&(l=l[p]);if(!l)return;let c=n&&a in n?n[a]:a;if(!c)throw new Error(`Invalid field name: ${a}`);i[c]=l}),i}).filter(o=>Object.keys(o).length!==0)}};var Nn=class extends et{maxRounds;maxDemos;maxExamples;batchSize;earlyStoppingPatience;costMonitoring;maxTokensPerGeneration;verboseMode;debugMode;traces=[];constructor(e){super(e);let t=e.options||{};this.maxRounds=t.maxRounds??3,this.maxDemos=t.maxDemos??4,this.maxExamples=t.maxExamples??16,this.batchSize=t.batchSize??1,this.earlyStoppingPatience=t.earlyStoppingPatience??0,this.costMonitoring=t.costMonitoring??!1,this.maxTokensPerGeneration=t.maxTokensPerGeneration??0,this.verboseMode=t.verboseMode??!0,this.debugMode=t.debugMode??!1}async compileRound(e,t,n,r,o){let i=Date.now(),a=o?.maxDemos??this.maxDemos,u={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(u.modelConfig.max_tokens=this.maxTokensPerGeneration);let l=Fd([...t],this.maxExamples),c=this.traces.length,p=e.getId();for(let d=0;d<l.length;d+=this.batchSize){d>0&&(u.modelConfig.temperature=.7+.001*d);let m=l.slice(d,d+this.batchSize);for(let g of m){if(!g||typeof g!="object")continue;let f=t.filter(h=>h!==g);e.setDemos([{traces:f,programId:p}]);let x=this.getTeacherOrStudentAI();this.stats.totalCalls++;let y;try{let h={...u,maxRetries:1};y=await e.forward(x,g,h),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(y).length/4),await r({prediction:y,example:g})>=.5&&(this.traces=[...this.traces,...e.getTraces()],this.stats.successfulDemos++)}catch(h){(this.verboseMode||this.debugMode)&&console.warn(`Student model failed during bootstrap: ${h instanceof Error?h.message:"Unknown error"}`),y={}}if(this.traces.length>=a)return}}if(this.earlyStoppingPatience>0){let m=this.traces.length-c;if(!this.stats.earlyStopping)this.stats.earlyStopping={bestScoreRound:m>0?n:0,patienceExhausted:!1,reason:"No improvement detected"};else if(m>0)this.stats.earlyStopping.bestScoreRound=n;else if(n-this.stats.earlyStopping.bestScoreRound>=this.earlyStoppingPatience){this.stats.earlyStopping.patienceExhausted=!0,this.stats.earlyStopped=!0,this.stats.earlyStopping.reason=`No improvement for ${this.earlyStoppingPatience} rounds`;return}}}async compile(e,t,n,r){this.validateExamples(t,!1);let o=r?.maxIterations??this.maxRounds;this.traces=[],this.reset();for(let u=0;u<o&&(await this.compileRound(e,t,u,n,r),!this.stats.earlyStopped);u++);if(this.traces.length===0)throw new Error("No demonstrations found. Either provide more examples or improve the existing ones.");let i=Pd(this.traces),a=0;return this.traces.length>0&&(a=this.stats.successfulDemos/Math.max(1,this.stats.totalCalls)),await this.logOptimizationComplete("BootstrapFewShot",a,{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a,demosGenerated:i.length,tracesCollected:this.traces.length},r),{demos:i,stats:this.stats,bestScore:a,finalConfiguration:{maxRounds:this.maxRounds,maxDemos:this.maxDemos,batchSize:this.batchSize,successRate:a}}}};function Pd(s){let e=new Map;for(let n of s)if(e.has(n.programId)){let r=e.get(n.programId);r&&r.push(n.trace)}else e.set(n.programId,[n.trace]);let t=[];return e.forEach((n,r)=>{t.push({traces:n,programId:r})}),t}var Fd=(s,e)=>{let t=[...s];for(let n=t.length-1;n>0;n--){let r=Math.floor(Math.random()*(n+1)),o=t[n],i=t[r];if(!o||!i)throw new Error("Invalid array elements");[t[n],t[r]]=[i,o]}return t.slice(0,e)};var rs=class{endpoint;timeout;retryAttempts;retryDelay;logger;constructor(e){this.endpoint=e.endpoint.replace(/\/$/,""),this.timeout=e.timeout??3e4,this.retryAttempts=e.retryAttempts??3,this.retryDelay=e.retryDelay??1e3,this.logger=e.logger}async healthCheck(){try{return(await this.fetchWithRetry("/health",{method:"GET"})).ok}catch(e){return this.logger?.({name:"Notification",id:"health_check_failed",value:`Health check failed: ${e}`}),!1}}async createOptimizationJob(e){let t=await this.fetchWithRetry("/optimize",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to create optimization job: ${n}`)}return t.json()}async getJobStatus(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get job status: ${n}`)}return t.json()}async cancelJob(e){let t=await this.fetchWithRetry(`/jobs/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to cancel job: ${n}`)}}async suggestParameters(e){let t=await this.fetchWithRetry(`/studies/${e}/suggest`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){let n=await t.text();throw new Error(`Failed to suggest parameters: ${n}`)}return t.json()}async evaluateTrial(e){let t=await this.fetchWithRetry(`/studies/${e.study_name}/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let n=await t.text();throw new Error(`Failed to evaluate trial: ${n}`)}}async getStudyResults(e){let t=await this.fetchWithRetry(`/studies/${e}/results`,{method:"GET"});if(!t.ok){let n=await t.text();throw new Error(`Failed to get study results: ${n}`)}return t.json()}async deleteStudy(e){let t=await this.fetchWithRetry(`/studies/${e}`,{method:"DELETE"});if(!t.ok){let n=await t.text();throw new Error(`Failed to delete study: ${n}`)}}async listStudies(){let e=await this.fetchWithRetry("/studies",{method:"GET"});if(!e.ok){let t=await e.text();throw new Error(`Failed to list studies: ${t}`)}return e.json()}async waitForJobCompletion(e,t=2e3,n=3e5){let r=Date.now();for(;Date.now()-r<n;){let o=await this.getJobStatus(e);if(["completed","failed","cancelled"].includes(o.status))return o;this.logger?.({name:"Notification",id:"job_status",value:`Job ${e} status: ${o.status}, waiting...`}),await this.sleep(t)}throw new Error(`Job ${e} did not complete within ${n}ms`)}async fetchWithRetry(e,t){let n=`${this.endpoint}${e}`,r=null;for(let o=0;o<this.retryAttempts;o++)try{let i=new AbortController,a=setTimeout(()=>i.abort(),this.timeout),u=await fetch(n,{...t,signal:i.signal});return clearTimeout(a),u}catch(i){r=i,this.logger?.({name:"Notification",id:"retry_attempt",value:`Attempt ${o+1} failed: ${i}`}),o<this.retryAttempts-1&&await this.sleep(this.retryDelay*Math.pow(2,o))}throw new Error(`Request failed after ${this.retryAttempts} attempts: ${r?.message}`)}sleep(e){return new Promise(t=>setTimeout(t,e))}};var os=class extends et{maxBootstrappedDemos;maxLabeledDemos;numCandidates;initTemperature;numTrials;minibatch;minibatchSize;minibatchFullEvalSteps;programAwareProposer;dataAwareProposer;viewDataBatchSize;tipAwareProposer;fewshotAwareProposer;earlyStoppingTrials;minImprovementThreshold;bayesianOptimization;acquisitionFunction;explorationWeight;optimizeTopP;sampleCount;pythonClient;localScoreHistory=[];localConfigurationHistory=[];customResultPicker;constructor(e){if(super(e),this.numCandidates=e.numCandidates??5,this.initTemperature=e.initTemperature??.7,this.maxBootstrappedDemos=e.maxBootstrappedDemos??3,this.maxLabeledDemos=e.maxLabeledDemos??4,this.numTrials=e.numTrials??30,this.minibatch=e.minibatch??!0,this.minibatchSize=e.minibatchSize??25,this.minibatchFullEvalSteps=e.minibatchFullEvalSteps??10,this.programAwareProposer=e.programAwareProposer??!0,this.dataAwareProposer=e.dataAwareProposer??!0,this.viewDataBatchSize=e.viewDataBatchSize??10,this.tipAwareProposer=e.tipAwareProposer??!0,this.fewshotAwareProposer=e.fewshotAwareProposer??!0,this.earlyStoppingTrials=e.earlyStoppingTrials??5,this.minImprovementThreshold=e.minImprovementThreshold??.01,this.bayesianOptimization=e.bayesianOptimization??!0,this.acquisitionFunction=e.acquisitionFunction??"expected_improvement",this.explorationWeight=e.explorationWeight??.1,this.optimizeTopP=e.optimizeTopP??!1,this.sampleCount=e.sampleCount??1,this.customResultPicker=e.resultPicker,e.optimizerEndpoint){let t={endpoint:e.optimizerEndpoint,timeout:e.optimizerTimeout??3e4,retryAttempts:e.optimizerRetries??3,logger:n=>{this.logger?.({name:"Notification",id:"python_client",value:typeof n=="string"?n:JSON.stringify(n)})}};this.pythonClient=new rs(t)}this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}defaultResultPicker=async e=>{if(e.type==="function"){let o=e.results.findIndex(i=>!i.isError);return o>=0?o:0}let t=new Map;for(let o of e.results){let i=JSON.stringify(o.sample??{}),a=t.get(i);a?a.count+=1:t.set(i,{count:1,firstIndex:o.index})}let n="",r={count:-1,firstIndex:0};for(let[o,i]of t.entries())i.count>r.count&&(r=i,n=o);return t.get(n)?.firstIndex??0};configureAuto(e){switch(e){case"light":this.numCandidates=3,this.numTrials=10,this.minibatch=!0,this.minibatchSize=20;break;case"medium":this.numCandidates=5,this.numTrials=20,this.minibatch=!0,this.minibatchSize=25;break;case"heavy":this.numCandidates=7,this.numTrials=30,this.minibatch=!0,this.minibatchSize=30;break}}generateTips(){return["Be very specific and detailed in your instructions.","Focus on step-by-step reasoning in your instructions.","Provide clear constraints and guidelines in your instructions.","Keep your instructions concise and to the point.","Emphasize accuracy and precision in your instructions.","Include examples of good outputs in your instructions.","Focus on handling edge cases in your instructions.","Explicitly outline the reasoning process in your instructions."]}async generateProgramSummary(e,t){let r=`
600
612
  Analyze this language model program and provide a concise summary of its purpose and structure.
601
613
 
602
614
  Program Signature: ${e.getSignature()}
@@ -606,11 +618,11 @@ Provide a 2-3 sentence summary focusing on:
606
618
  2. The input-output relationship
607
619
  3. Any special constraints or requirements
608
620
 
609
- Summary:`;try{let o=await t.chat({chatPrompt:[{role:"user",content:r}]});return"results"in o&&o.results[0]?.content?.trim()||"General language model program"}catch{return"General language model program"}}async generateDatasetSummary(e,t){if(e.length===0)return"No examples available";let n=Math.min(this.viewDataBatchSize,e.length),s=`
621
+ Summary:`;try{let o=await t.chat({chatPrompt:[{role:"user",content:r}]});return"results"in o&&o.results[0]?.content?.trim()||"General language model program"}catch{return"General language model program"}}async generateDatasetSummary(e,t){if(e.length===0)return"No examples available";let n=Math.min(this.viewDataBatchSize,e.length),i=`
610
622
  Analyze this dataset and provide a concise summary of its characteristics.
611
623
 
612
624
  Sample Examples:
613
- ${e.slice(0,n).map((a,l)=>`Example ${l+1}: ${JSON.stringify(a)}`).join(`
625
+ ${e.slice(0,n).map((a,u)=>`Example ${u+1}: ${JSON.stringify(a)}`).join(`
614
626
  `)}
615
627
 
616
628
  Provide a 2-3 sentence summary focusing on:
@@ -618,10 +630,10 @@ Provide a 2-3 sentence summary focusing on:
618
630
  2. Common patterns or structures in the examples
619
631
  3. Key challenges or requirements for processing this data
620
632
 
621
- Dataset Summary:`;try{let a=await t.chat({chatPrompt:[{role:"user",content:s}]});return"results"in a&&a.results[0]?.content?.trim()||"General dataset"}catch{return"General dataset"}}async generateInstruction({tip:e,candidateIndex:t,ai:n,programSummary:r,datasetSummary:o,previousInstructions:s=[]}){let a="";this.programAwareProposer&&r&&(a+=`
633
+ Dataset Summary:`;try{let a=await t.chat({chatPrompt:[{role:"user",content:i}]});return"results"in a&&a.results[0]?.content?.trim()||"General dataset"}catch{return"General dataset"}}async generateInstruction({tip:e,candidateIndex:t,ai:n,programSummary:r,datasetSummary:o,previousInstructions:i=[]}){let a="";this.programAwareProposer&&r&&(a+=`
622
634
  Program Context: ${r}`),this.dataAwareProposer&&o&&(a+=`
623
- Dataset Context: ${o}`),this.fewshotAwareProposer&&s.length>0&&(a+=`
624
- Previous Instructions (avoid repeating): ${s.slice(-3).join("; ")}`);let l=`
635
+ Dataset Context: ${o}`),this.fewshotAwareProposer&&i.length>0&&(a+=`
636
+ Previous Instructions (avoid repeating): ${i.slice(-3).join("; ")}`);let u=`
625
637
  Generate a high-quality instruction for a language model program.
626
638
 
627
639
  ${a}
@@ -636,12 +648,12 @@ Requirements:
636
648
  5. Keep it concise but comprehensive
637
649
 
638
650
  Generate a single, well-crafted instruction:
639
- Instruction:`;try{let m=(await Ye('programSummary?:string "Program context" , datasetSummary?:string "Dataset context" , tip?:string "Generation tip" -> instructionText:string "Well-crafted instruction for the program"').forward(n,{programSummary:r??"",datasetSummary:o??"",tip:e??""})).instructionText;if(m&&m.trim().length>10)return m.trim()}catch{}let u=["Analyze the input systematically and provide a precise, well-reasoned response.","Think through this step-by-step, considering all relevant factors before responding.","Examine the input carefully and generate an accurate, detailed answer.","Process the information methodically and deliver a clear, comprehensive response.","Consider the context thoroughly and provide a thoughtful, accurate answer."],c=u[t%u.length]||u[0];return e&&(c=`${c} ${e}`),c}async proposeInstructionCandidates(e,t,n=[]){let r=[],o=this.getTeacherOrStudentAI(t),s,a;this.programAwareProposer&&(s=await this.generateProgramSummary(e,o)),this.dataAwareProposer&&(a=await this.generateDatasetSummary([...n],o));let l=this.tipAwareProposer?this.generateTips():[];for(let u=0;u<this.numCandidates;u++){let c=l.length>0?u%l.length:-1,p=c>=0?l[c]:void 0,d=await this.generateInstruction({tip:p,candidateIndex:u,ai:o,programSummary:s,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Hn({studentAI:this.studentAI,options:{maxDemos:this.maxBootstrappedDemos,maxRounds:3,verboseMode:this.verbose??!1}}).compile(e,n,t,{maxDemos:this.maxBootstrappedDemos})).demos||[]}selectLabeledExamples(e){let t=[],n=new Set;for(;n.size<this.maxLabeledDemos&&n.size<e.length;){let r=Math.floor(Math.random()*e.length);if(!n.has(r)){n.add(r);let o=e[r];o&&t.push(o)}}return t}applyConfigToProgram(e,t,n,r){if(e.setInstruction&&e.setInstruction(t.instruction),e.setDemos){let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0&&e.getId){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}}async compile(e,t,n,r){let o=Date.now();if(this.validateExamples(t),this.setupRandomSeed(),r?.auto&&this.configureAuto(r.auto),!this.pythonClient)throw new Error("AxMiPRO v2 requires the Python optimizer service. Please configure optimizerEndpoint.");if(!await this.pythonClient.healthCheck())throw new Error("Python optimizer service is not available or unhealthy");return await this.compilePython(e,t,n,r)}applyConfigToAxGen(e,t,n,r){"setInstruction"in e&&typeof e.setInstruction=="function"&&e.setInstruction(t.instruction);let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0){let s=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:s})}o.length>0&&e.setDemos(o)}getConfiguration(){return{numCandidates:this.numCandidates,initTemperature:this.initTemperature,maxBootstrappedDemos:this.maxBootstrappedDemos,maxLabeledDemos:this.maxLabeledDemos,numTrials:this.numTrials,minibatch:this.minibatch,minibatchSize:this.minibatchSize,minibatchFullEvalSteps:this.minibatchFullEvalSteps,programAwareProposer:this.programAwareProposer,dataAwareProposer:this.dataAwareProposer,tipAwareProposer:this.tipAwareProposer,fewshotAwareProposer:this.fewshotAwareProposer,earlyStoppingTrials:this.earlyStoppingTrials,minImprovementThreshold:this.minImprovementThreshold,bayesianOptimization:this.bayesianOptimization,acquisitionFunction:this.acquisitionFunction,explorationWeight:this.explorationWeight,sampleCount:this.sampleCount}}updateConfiguration(e){e.numCandidates!==void 0&&(this.numCandidates=e.numCandidates),e.initTemperature!==void 0&&(this.initTemperature=e.initTemperature),e.maxBootstrappedDemos!==void 0&&(this.maxBootstrappedDemos=e.maxBootstrappedDemos),e.maxLabeledDemos!==void 0&&(this.maxLabeledDemos=e.maxLabeledDemos),e.numTrials!==void 0&&(this.numTrials=e.numTrials),e.minibatch!==void 0&&(this.minibatch=e.minibatch),e.minibatchSize!==void 0&&(this.minibatchSize=e.minibatchSize),e.earlyStoppingTrials!==void 0&&(this.earlyStoppingTrials=e.earlyStoppingTrials),e.minImprovementThreshold!==void 0&&(this.minImprovementThreshold=e.minImprovementThreshold),e.sampleCount!==void 0&&(this.sampleCount=e.sampleCount)}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}validateProgram(e){let t=[],n=[];return{isValid:t.length===0,issues:t,suggestions:n}}async compilePython(e,t,n,r){if(!this.pythonClient)throw new Error("Python client not initialized");let o=Date.now();this.localScoreHistory=[],this.localConfigurationHistory=[];let s=await this.proposeInstructionCandidates(e,r,t),a=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,l={study_name:a,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},{name:"instruction",type:"categorical",choices:s},{name:"labeledExamples",type:"int",low:0,high:this.maxLabeledDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},u=await this.pythonClient.createOptimizationJob(l);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:u.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,d,m=0,g=0;for(let T=0;T<this.numTrials;T++)try{let k=await this.pythonClient.suggestParameters(a),E=k.params.temperature,M=k.params.bootstrappedDemos,$=k.params.instruction,B=k.params.labeledExamples,V=this.optimizeTopP?k.params.topP:void 0;if(E===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(k)}`);if(M===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(k)}`);if($===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(k)}`);if(B===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(k)}`);let O=e.clone();O.setInstruction($);let j=O.getId();O.setDemos([{traces:this.selectLabeledExamples(t).slice(0,B),programId:j}]);let W=!this.minibatch||this.minibatchFullEvalSteps>0&&T%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let K=Math.min(this.minibatchSize,t.length),N=new Set;for(;N.size<K;)N.add(Math.floor(Math.random()*t.length));return Array.from(N).map(J=>t[J])})(),P=await this.evaluateConfiguration(O,n,{temperature:E,bootstrappedDemos:M,topP:V},W);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:k.trial_number,value:P}),P>p+this.minImprovementThreshold?(p=P,d={temperature:E,bootstrappedDemos:M,...V!==void 0?{topP:V}:{},trialNumber:k.trial_number},g=0):g+=1,this.currentRound=T+1;let D={temperature:E,bootstrappedDemos:M,...V!==void 0?{topP:V}:{},trialNumber:k.trial_number};if(this.localScoreHistory.push(P),this.localConfigurationHistory.push(D),await this.updateOptimizationProgress(this.currentRound,P,D,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:T+1,totalRounds:this.numTrials,currentScore:P,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:m,totalExamples:t.length}),this.earlyStoppingTrials>0&&g>=this.earlyStoppingTrials){this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:`No improvement \u2265 ${this.minImprovementThreshold} for ${this.earlyStoppingTrials} trials`,finalScore:p,round:this.currentRound}}),this.onEarlyStop?.(`No improvement for ${this.earlyStoppingTrials} trials`,this.stats);break}}catch{}let h=p,f={},C=[];try{let T=await this.pythonClient.getStudyResults(a);if(h=T.best_value||p,f=T.best_params||{},f&&Object.keys(f).length>0){let k=f.bootstrappedDemos||0;k>0&&(C=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),C=C.slice(0,k))}}catch{}let x;try{let k=await Ye('optimizerType:string "Optimizer name" , bestScore:number "Final best score" , totalCalls:number "Total eval calls" , successfulDemos:number "Successful evals" , bestConfig:json "Best configuration" -> humanExplanation:string "Readable summary", recommendations:string[] "Next steps", performanceAssessment:string "Performance notes"').forward(this.studentAI,{optimizerType:"MiPRO (Python)",bestScore:h,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:f||{}});x={humanExplanation:k.humanExplanation??"",recommendations:k.recommendations??[],performanceAssessment:k.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",h,f,r,x);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=h;let I=new Ee(e.getSignature());f.instruction&&I.setInstruction(f.instruction);let w=f.temperature?{temperature:f.temperature}:void 0;I.setDemos(C,{modelConfig:w});let A=new kt({bestScore:h,stats:this.stats,instruction:f.instruction,demos:C,examples:[],modelConfig:{temperature:f.temperature},optimizerType:"MiPRO (Python)",optimizationTime:Date.now()-o,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged,scoreHistory:[...this.localScoreHistory],configurationHistory:[...this.localConfigurationHistory]});return this.generateOptimizationReport(h,C.length),{bestScore:h,demos:C,stats:this.stats,optimizedGen:I,optimizedProgram:A,finalConfiguration:{temperature:f.temperature,bootstrappedDemos:f.bootstrappedDemos||0,...f}}}generateOptimizationReport(e,t){console.log(`
651
+ Instruction:`;try{let m=(await qe('programSummary?:string "Program context" , datasetSummary?:string "Dataset context" , tip?:string "Generation tip" -> instructionText:string "Well-crafted instruction for the program"').forward(n,{programSummary:r??"",datasetSummary:o??"",tip:e??""})).instructionText;if(m&&m.trim().length>10)return m.trim()}catch{}let l=["Analyze the input systematically and provide a precise, well-reasoned response.","Think through this step-by-step, considering all relevant factors before responding.","Examine the input carefully and generate an accurate, detailed answer.","Process the information methodically and deliver a clear, comprehensive response.","Consider the context thoroughly and provide a thoughtful, accurate answer."],c=l[t%l.length]||l[0];return e&&(c=`${c} ${e}`),c}async proposeInstructionCandidates(e,t,n=[]){let r=[],o=this.getTeacherOrStudentAI(t),i,a;this.programAwareProposer&&(i=await this.generateProgramSummary(e,o)),this.dataAwareProposer&&(a=await this.generateDatasetSummary([...n],o));let u=this.tipAwareProposer?this.generateTips():[];for(let l=0;l<this.numCandidates;l++){let c=u.length>0?l%u.length:-1,p=c>=0?u[c]:void 0,d=await this.generateInstruction({tip:p,candidateIndex:l,ai:o,programSummary:i,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Nn({studentAI:this.studentAI,options:{maxDemos:this.maxBootstrappedDemos,maxRounds:3,verboseMode:this.verbose??!1}}).compile(e,n,t,{maxDemos:this.maxBootstrappedDemos})).demos||[]}selectLabeledExamples(e){let t=[],n=new Set;for(;n.size<this.maxLabeledDemos&&n.size<e.length;){let r=Math.floor(Math.random()*e.length);if(!n.has(r)){n.add(r);let o=e[r];o&&t.push(o)}}return t}applyConfigToProgram(e,t,n,r){if(e.setInstruction&&e.setInstruction(t.instruction),e.setDemos){let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0&&e.getId){let i=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:i})}o.length>0&&e.setDemos(o)}}async compile(e,t,n,r){let o=Date.now();if(this.validateExamples(t),this.setupRandomSeed(),r?.auto&&this.configureAuto(r.auto),!this.pythonClient)throw new Error("AxMiPRO v2 requires the Python optimizer service. Please configure optimizerEndpoint.");if(!await this.pythonClient.healthCheck())throw new Error("Python optimizer service is not available or unhealthy");return await this.compilePython(e,t,n,r)}applyConfigToAxGen(e,t,n,r){"setInstruction"in e&&typeof e.setInstruction=="function"&&e.setInstruction(t.instruction);let o=[...n.slice(0,t.bootstrappedDemos)];if(t.labeledExamples>0){let i=e.getId();o.push({traces:r.slice(0,t.labeledExamples),programId:i})}o.length>0&&e.setDemos(o)}getConfiguration(){return{numCandidates:this.numCandidates,initTemperature:this.initTemperature,maxBootstrappedDemos:this.maxBootstrappedDemos,maxLabeledDemos:this.maxLabeledDemos,numTrials:this.numTrials,minibatch:this.minibatch,minibatchSize:this.minibatchSize,minibatchFullEvalSteps:this.minibatchFullEvalSteps,programAwareProposer:this.programAwareProposer,dataAwareProposer:this.dataAwareProposer,tipAwareProposer:this.tipAwareProposer,fewshotAwareProposer:this.fewshotAwareProposer,earlyStoppingTrials:this.earlyStoppingTrials,minImprovementThreshold:this.minImprovementThreshold,bayesianOptimization:this.bayesianOptimization,acquisitionFunction:this.acquisitionFunction,explorationWeight:this.explorationWeight,sampleCount:this.sampleCount}}updateConfiguration(e){e.numCandidates!==void 0&&(this.numCandidates=e.numCandidates),e.initTemperature!==void 0&&(this.initTemperature=e.initTemperature),e.maxBootstrappedDemos!==void 0&&(this.maxBootstrappedDemos=e.maxBootstrappedDemos),e.maxLabeledDemos!==void 0&&(this.maxLabeledDemos=e.maxLabeledDemos),e.numTrials!==void 0&&(this.numTrials=e.numTrials),e.minibatch!==void 0&&(this.minibatch=e.minibatch),e.minibatchSize!==void 0&&(this.minibatchSize=e.minibatchSize),e.earlyStoppingTrials!==void 0&&(this.earlyStoppingTrials=e.earlyStoppingTrials),e.minImprovementThreshold!==void 0&&(this.minImprovementThreshold=e.minImprovementThreshold),e.sampleCount!==void 0&&(this.sampleCount=e.sampleCount)}reset(){super.reset(),this.stats.convergenceInfo.convergenceThreshold=this.minImprovementThreshold}validateProgram(e){let t=[],n=[];return{isValid:t.length===0,issues:t,suggestions:n}}async compilePython(e,t,n,r){if(!this.pythonClient)throw new Error("Python client not initialized");let o=Date.now();this.localScoreHistory=[],this.localConfigurationHistory=[];let i=await this.proposeInstructionCandidates(e,r,t),a=`mipro_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,u={study_name:a,parameters:[{name:"temperature",type:"float",low:.1,high:2},{name:"bootstrappedDemos",type:"int",low:0,high:this.maxBootstrappedDemos},{name:"instruction",type:"categorical",choices:i},{name:"labeledExamples",type:"int",low:0,high:this.maxLabeledDemos},...this.optimizeTopP?[{name:"topP",type:"float",low:.7,high:1}]:[]],objective:{name:"score",direction:"maximize"},n_trials:this.numTrials,sampler:"TPESampler",pruner:this.minibatch?"MedianPruner":void 0},l=await this.pythonClient.createOptimizationJob(u);this.getOptimizerLogger()?.({name:"OptimizationStart",value:{optimizerType:"MiPRO (Python)",exampleCount:t.length,validationCount:0,config:{jobId:l.job_id,numTrials:this.numTrials}}});let p=Number.NEGATIVE_INFINITY,d,m=0,g=0;for(let T=0;T<this.numTrials;T++)try{let R=await this.pythonClient.suggestParameters(a),k=R.params.temperature,v=R.params.bootstrappedDemos,O=R.params.instruction,P=R.params.labeledExamples,$=this.optimizeTopP?R.params.topP:void 0;if(k===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(R)}`);if(v===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(R)}`);if(O===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(R)}`);if(P===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(R)}`);let E=e.clone();E.setInstruction(O);let F=E.getId();E.setDemos([{traces:this.selectLabeledExamples(t).slice(0,P),programId:F}]);let j=!this.minibatch||this.minibatchFullEvalSteps>0&&T%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let X=Math.min(this.minibatchSize,t.length),U=new Set;for(;U.size<X;)U.add(Math.floor(Math.random()*t.length));return Array.from(U).map(se=>t[se])})(),H=await this.evaluateConfiguration(E,n,{temperature:k,bootstrappedDemos:v,topP:$},j);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:R.trial_number,value:H}),H>p+this.minImprovementThreshold?(p=H,d={temperature:k,bootstrappedDemos:v,...$!==void 0?{topP:$}:{},trialNumber:R.trial_number},g=0):g+=1,this.currentRound=T+1;let L={temperature:k,bootstrappedDemos:v,...$!==void 0?{topP:$}:{},trialNumber:R.trial_number};if(this.localScoreHistory.push(H),this.localConfigurationHistory.push(L),await this.updateOptimizationProgress(this.currentRound,H,L,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:T+1,totalRounds:this.numTrials,currentScore:H,bestScore:p,tokensUsed:this.stats.estimatedTokenUsage,timeElapsed:Date.now()-o,successfulExamples:m,totalExamples:t.length}),this.earlyStoppingTrials>0&&g>=this.earlyStoppingTrials){this.getOptimizerLogger()?.({name:"EarlyStopping",value:{reason:`No improvement \u2265 ${this.minImprovementThreshold} for ${this.earlyStoppingTrials} trials`,finalScore:p,round:this.currentRound}}),this.onEarlyStop?.(`No improvement for ${this.earlyStoppingTrials} trials`,this.stats);break}}catch{}let f=p,x={},y=[];try{let T=await this.pythonClient.getStudyResults(a);if(f=T.best_value||p,x=T.best_params||{},x&&Object.keys(x).length>0){let R=x.bootstrappedDemos||0;R>0&&(y=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),y=y.slice(0,R))}}catch{}let h;try{let R=await qe('optimizerType:string "Optimizer name" , bestScore:number "Final best score" , totalCalls:number "Total eval calls" , successfulDemos:number "Successful evals" , bestConfig:json "Best configuration" -> humanExplanation:string "Readable summary", recommendations:string[] "Next steps", performanceAssessment:string "Performance notes"').forward(this.studentAI,{optimizerType:"MiPRO (Python)",bestScore:f,totalCalls:this.stats.totalCalls,successfulDemos:this.stats.successfulDemos,bestConfig:x||{}});h={humanExplanation:R.humanExplanation??"",recommendations:R.recommendations??[],performanceAssessment:R.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",f,x,r,h);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=f;let A=new Ie(e.getSignature());x.instruction&&A.setInstruction(x.instruction);let b=x.temperature?{temperature:x.temperature}:void 0;A.setDemos(y,{modelConfig:b});let I=new mt({bestScore:f,stats:this.stats,instruction:x.instruction,demos:y,examples:[],modelConfig:{temperature:x.temperature},optimizerType:"MiPRO (Python)",optimizationTime:Date.now()-o,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged,scoreHistory:[...this.localScoreHistory],configurationHistory:[...this.localConfigurationHistory]});return this.generateOptimizationReport(f,y.length),{bestScore:f,demos:y,stats:this.stats,optimizedGen:A,optimizedProgram:I,finalConfiguration:{temperature:x.temperature,bootstrappedDemos:x.bootstrappedDemos||0,...x}}}generateOptimizationReport(e,t){console.log(`
640
652
  \u{1F389} MiPRO Optimization Complete!
641
653
  `),console.log("\u2705 Improvements:"),e!==void 0&&e>0&&console.log(`\u2022 Best score achieved: ${e.toFixed(3)}`),t>0&&console.log(`\u2022 Generated ${t} optimized demonstrations`),console.log("\u2022 Systematic prompt and example optimization"),console.log(`\u2022 Automated instruction refinement process
642
654
  `),console.log("\u26A0\uFE0F Limitations:"),this.stats.totalCalls<50&&console.log("\u2022 Relatively few optimization trials performed"),t<5&&console.log("\u2022 Limited number of demonstrations generated"),console.log("\u2022 Results depend on teacher model quality"),console.log(`\u2022 Optimization time increases with example complexity
643
655
  `),console.log("\u{1F50D} Key Issues:"),e!==void 0&&e<.7&&console.log("\u2022 Final performance may still have room for improvement"),this.stats.convergenceInfo?.converged===!1&&console.log("\u2022 Optimization may not have fully converged"),console.log("\u2022 Evaluation metrics may need domain-specific tuning"),console.log(`\u2022 Bootstrap quality depends on initial examples
644
- `),console.log("\u{1F4A1} What This Means:"),console.log("\u2022 MiPRO successfully automated prompt engineering"),console.log("\u2022 Optimized instructions and examples improve model performance"),console.log("\u2022 Framework reduces manual prompt engineering effort"),console.log("\u2022 More training data and iterations would likely improve results")}async evaluateConfiguration(e,t,n,r){let o=0,s=0,a=0,l=r;for(let u of l)try{let c=await e.forward(this.studentAI,u,{modelConfig:{temperature:n.temperature,...n.topP!==void 0?{topP:n.topP}:{}},sampleCount:this.sampleCount,resultPicker:this.sampleCount>1?this.customResultPicker??this.defaultResultPicker:void 0});this.stats.totalCalls+=1;let p=await t({prediction:c,example:u});if(typeof p=="number"&&!Number.isNaN(p)){o+=p,s++;let d=typeof this.targetScore=="number"?this.targetScore:.5;p>=d&&a++}}catch(c){this.getLogger()?.({name:"Notification",id:"mipro_evaluate",value:typeof c=="string"?c:String(c)})}return this.stats.successfulDemos+=a,s>0?o/s:0}};var Kn=class{analyzeMappingDependencies(e,t){if(!e||typeof e!="function")return[];let n=[];try{let r=e.toString(),o=Array.from(r.matchAll(/state\.(\w+)/g));for(let s of o)s[1]&&!n.includes(s[1])&&n.push(s[1]);if(n.length===0)try{let s=this.createDependencyTracker(n);e(s)}catch{}}catch(r){console.debug("Dependency analysis failed:",r)}return n}createTrackingProxy(e,t){let n=this;return new Proxy(e,{get(r,o){typeof o=="string"&&!t.includes(o)&&t.push(o);let s=r[o];return s&&typeof s=="object"?n.createTrackingProxy(s,t):s},has(r,o){return typeof o=="string"&&!t.includes(o)&&t.push(o),o in r}})}parseStaticDependencies(e){let t=[];try{let n=Array.from(e.matchAll(/state\.(\w+)/g));for(let s of n)s[1]&&!t.includes(s[1])&&t.push(s[1]);let r=Array.from(e.matchAll(/\$\{state\.(\w+)\}/g));for(let s of r)s[1]&&!t.includes(s[1])&&t.push(s[1]);let o=Array.from(e.matchAll(/\{\s*(\w+)(?:\s*,\s*(\w+))*\s*\}\s*=\s*state/g));for(let s of o)for(let a=1;a<s.length;a++)s[a]&&!t.includes(s[a])&&t.push(s[a])}catch(n){console.debug("Static dependency parsing failed:",n)}return t}createDependencyTracker(e){return new Proxy({},{get(t,n){return typeof n=="string"&&!e.includes(n)&&e.push(n),new Proxy({},{get:()=>{}})}})}};async function Wn(i,e,t){if(!t||t<=0||t>=i.length){let r=i.map((o,s)=>e(o,s));return Promise.all(r)}let n=new Array(i.length);for(let r=0;r<i.length;r+=t){let s=i.slice(r,r+t).map((l,u)=>{let c=r+u;return e(l,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(s);for(let{result:l,originalIndex:u}of a)n[u]=l}return n}var Vn=class{steps=[];parallelGroups=[];analyzer=new Kn;initialFields=new Set;addExecutionStep(e,t,n,r,o,s,a){let l=[],u=[],c=r||"map";if(t&&n)c="execute",l=this.analyzer.analyzeMappingDependencies(n,t),u=[`${t}Result`];else if(c==="map"&&o)u=this.analyzeMapTransformation(o),l=this.getAllProducedFields();else if(c==="parallel-map"){if(Array.isArray(o)){let d=new Set;for(let m of o)this.analyzeMapTransformation(m).forEach(h=>d.add(h));u=Array.from(d)}else o?u=this.analyzeMapTransformation(o):u=["_parallelMapResult"];l=this.getAllProducedFields()}else if(c==="merge"){if(s?.resultKey)u=[s.resultKey];else{let m=this.analyzeBranchMergeFields();u=m.length>0?m:["_mergedResult"]}e.toString().includes("_parallelResults")?l=["_parallelResults"]:l=this.getAllProducedFields()}else if(c==="parallel")u=["_parallelResults"],l=this.getAllProducedFields();else if(c==="derive")if(a?.outputFieldName&&a?.inputFieldName){u=[a.outputFieldName];let d=o?this.analyzer.analyzeMappingDependencies(o,"derive"):[];l=[a.inputFieldName,...d].filter((m,g,h)=>h.indexOf(m)===g)}else u=["_deriveResult"],l=this.getAllProducedFields();else e.toString().includes("transform(")?(c="map",l=this.getAllProducedFields(),u=["_mapResult"]):e.toString().includes("_parallelResults")&&(u=["_parallelResults"],l=this.getAllProducedFields());for(let d of l)this.getAllProducedFields().includes(d)||this.initialFields.add(d);let p={type:c,nodeName:t,dependencies:l,produces:u,stepFunction:e,stepIndex:this.steps.length};this.steps.push(p)}analyzeStepFunctionProduction(e){try{let t=this.analyzeStepFunctionSource(e);if(t.length>0&&!t.includes("_stepResult"))return t}catch(t){console.debug("Step function source analysis failed:",t)}try{let t=this.createMockState(),n=Object.keys(t),o=e(t,{mainAi:{getOptions:()=>({trace:!1}),forward:()=>Promise.resolve({text:"mock"})},mainOptions:void 0});if(o&&typeof o=="object"&&"then"in o)return this.analyzeStepFunctionSource(e);if(o&&typeof o=="object"&&!Array.isArray(o)){let a=Object.keys(o).filter(l=>!n.includes(l));if(a.length>0)return a}}catch(t){console.debug("Step function dynamic analysis failed:",t)}return this.analyzeStepFunctionSource(e)}analyzeStepFunctionSource(e){try{let t=e.toString(),n=t.match(/\{\s*\.\.\.state\s*,\s*(\w+)\s*:/g);if(n){let o=n.map(s=>{let a=s.match(/(\w+)\s*:/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}let r=t.match(/state\.(\w+)\s*=/g);if(r){let o=r.map(s=>{let a=s.match(/state\.(\w+)\s*=/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}}catch(t){console.debug("Step function source analysis failed:",t)}return["_stepResult"]}analyzeMapTransformation(e){try{let n=e.toString().split(/\{[\s\S]*?\}/).flatMap(()=>[])}catch{}return["_mapResult"]}createMockState(){let e={};for(let t of this.initialFields)e[t]=this.createMockValue(t);for(let t of this.steps)for(let n of t.produces)n.endsWith("Result")?e[n]={text:"mockText",value:"mockValue",result:"mockResult",data:"mockData",processedText:"mockProcessedText",sentimentValue:"mockSentiment",confidenceScore:.8,isComplex:!1,mockValue:"mockValue",responseText:"mockResponseText",inputText:"mockInputText"}:e[n]=this.createMockValue(n);return e}createMockValue(e){return e.includes("List")||e.includes("Array")||e.endsWith("s")?["mockItem1","mockItem2"]:e.includes("count")||e.includes("Count")||e.includes("index")||e.includes("Index")?0:e.includes("is")||e.includes("has")||e.includes("can")?!1:"mockValue"}analyzeBranchMergeFields(){let e=this.steps.slice(-5).filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces);return e.length>0?e:this.steps.filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces)}setInitialFields(e){this.initialFields=new Set(e),this.rebuildParallelGroups()}rebuildParallelGroups(){this.parallelGroups=[];let e=new Set,t=new Set(this.initialFields),n=0;for(;e.size<this.steps.length;){let r=[];for(let o of this.steps){if(e.has(o.stepIndex))continue;if(o.dependencies.length===0||o.dependencies.every(a=>t.has(a))){if(o.type==="merge"&&r.length>0)continue;if(r.push(o),e.add(o.stepIndex),o.type==="merge")break}}if(r.length>0){for(let o of r)o.produces.forEach(s=>t.add(s));this.parallelGroups.push({level:n,steps:r}),n++}else{let o=this.steps.filter(s=>!e.has(s.stepIndex));if(o.length>0){let s=o[0];e.add(s.stepIndex),s.produces.forEach(a=>t.add(a)),this.parallelGroups.push({level:n,steps:[s]}),n++}else break}}}getAllProducedFields(){let e=[];for(let t of this.steps)e.push(...t.produces);return e}createOptimizedExecution(e){let t=[];for(let n of this.parallelGroups)if(n.steps.length===1){let r=n.steps[0];r&&t.push(r.stepFunction)}else if(n.steps.length>1){let r=async(o,s)=>{let a=await Wn(n.steps,async c=>await c.stepFunction(o,s),e);if(a.some(c=>c&&typeof c=="object"&&"_parallelResults"in c)){let c=a.find(p=>p&&typeof p=="object"&&"_parallelResults"in p);return c||o}let u=o;for(let c of a)u={...u,...c};return u};t.push(r)}return t}getOptimizedExecutionSteps(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),this.createOptimizedExecution()}getExecutionPlan(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),{totalSteps:this.steps.length,parallelGroups:this.parallelGroups.length,maxParallelism:this.steps.length===0?1:Math.max(...this.parallelGroups.map(e=>e.steps.length),0),steps:this.steps,groups:this.parallelGroups}}};var Xn=require("@opentelemetry/api");var Su=i=>{console.log(i)},Jn=(i,e=!1)=>{if(e)return"[State hidden]";let t={};for(let[n,r]of Object.entries(i))if(typeof r=="string"&&r.length>100)t[n]=`${r.substring(0,100)}...`;else if(Array.isArray(r)&&r.length>3)t[n]=[...r.slice(0,3),`... (${r.length-3} more)`];else if(typeof r=="object"&&r!==null){let o=JSON.stringify(r);o.length>200?t[n]=`${o.substring(0,200)}...`:t[n]=r}else t[n]=r;return JSON.stringify(t,null,2)},Qn=i=>i<1e3?`${i.toFixed(1)}ms`:i<6e4?`${(i/1e3).toFixed(2)}s`:`${(i/6e4).toFixed(2)}min`,Dr=(i=Su)=>{let e=new nt,t=e.gray(`${"\u2501".repeat(80)}
656
+ `),console.log("\u{1F4A1} What This Means:"),console.log("\u2022 MiPRO successfully automated prompt engineering"),console.log("\u2022 Optimized instructions and examples improve model performance"),console.log("\u2022 Framework reduces manual prompt engineering effort"),console.log("\u2022 More training data and iterations would likely improve results")}async evaluateConfiguration(e,t,n,r){let o=0,i=0,a=0,u=r;for(let l of u)try{let c=await e.forward(this.studentAI,l,{modelConfig:{temperature:n.temperature,...n.topP!==void 0?{topP:n.topP}:{}},sampleCount:this.sampleCount,resultPicker:this.sampleCount>1?this.customResultPicker??this.defaultResultPicker:void 0});this.stats.totalCalls+=1;let p=await t({prediction:c,example:l});if(typeof p=="number"&&!Number.isNaN(p)){o+=p,i++;let d=typeof this.targetScore=="number"?this.targetScore:.5;p>=d&&a++}}catch(c){this.getLogger()?.({name:"Notification",id:"mipro_evaluate",value:typeof c=="string"?c:String(c)})}return this.stats.successfulDemos+=a,i>0?o/i:0}};var Ln=class{analyzeMappingDependencies(e,t){if(!e||typeof e!="function")return[];let n=[];try{let r=e.toString(),o=Array.from(r.matchAll(/state\.(\w+)/g));for(let i of o)i[1]&&!n.includes(i[1])&&n.push(i[1]);if(n.length===0)try{let i=this.createDependencyTracker(n);e(i)}catch{}}catch(r){console.debug("Dependency analysis failed:",r)}return n}createTrackingProxy(e,t){let n=this;return new Proxy(e,{get(r,o){typeof o=="string"&&!t.includes(o)&&t.push(o);let i=r[o];return i&&typeof i=="object"?n.createTrackingProxy(i,t):i},has(r,o){return typeof o=="string"&&!t.includes(o)&&t.push(o),o in r}})}parseStaticDependencies(e){let t=[];try{let n=Array.from(e.matchAll(/state\.(\w+)/g));for(let i of n)i[1]&&!t.includes(i[1])&&t.push(i[1]);let r=Array.from(e.matchAll(/\$\{state\.(\w+)\}/g));for(let i of r)i[1]&&!t.includes(i[1])&&t.push(i[1]);let o=Array.from(e.matchAll(/\{\s*(\w+)(?:\s*,\s*(\w+))*\s*\}\s*=\s*state/g));for(let i of o)for(let a=1;a<i.length;a++)i[a]&&!t.includes(i[a])&&t.push(i[a])}catch(n){console.debug("Static dependency parsing failed:",n)}return t}createDependencyTracker(e){return new Proxy({},{get(t,n){return typeof n=="string"&&!e.includes(n)&&e.push(n),new Proxy({},{get:()=>{}})}})}};async function $n(s,e,t){if(!t||t<=0||t>=s.length){let r=s.map((o,i)=>e(o,i));return Promise.all(r)}let n=new Array(s.length);for(let r=0;r<s.length;r+=t){let i=s.slice(r,r+t).map((u,l)=>{let c=r+l;return e(u,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(i);for(let{result:u,originalIndex:l}of a)n[l]=u}return n}var Dn=class{steps=[];parallelGroups=[];analyzer=new Ln;initialFields=new Set;addExecutionStep(e,t,n,r,o,i,a){let u=[],l=[],c=r||"map";if(t&&n)c="execute",u=this.analyzer.analyzeMappingDependencies(n,t),l=[`${t}Result`];else if(c==="map"&&o)l=this.analyzeMapTransformation(o),u=this.getAllProducedFields();else if(c==="parallel-map"){if(Array.isArray(o)){let d=new Set;for(let m of o)this.analyzeMapTransformation(m).forEach(f=>d.add(f));l=Array.from(d)}else o?l=this.analyzeMapTransformation(o):l=["_parallelMapResult"];u=this.getAllProducedFields()}else if(c==="merge"){if(i?.resultKey)l=[i.resultKey];else{let m=this.analyzeBranchMergeFields();l=m.length>0?m:["_mergedResult"]}e.toString().includes("_parallelResults")?u=["_parallelResults"]:u=this.getAllProducedFields()}else if(c==="parallel")l=["_parallelResults"],u=this.getAllProducedFields();else if(c==="derive")if(a?.outputFieldName&&a?.inputFieldName){l=[a.outputFieldName];let d=o?this.analyzer.analyzeMappingDependencies(o,"derive"):[];u=[a.inputFieldName,...d].filter((m,g,f)=>f.indexOf(m)===g)}else l=["_deriveResult"],u=this.getAllProducedFields();else e.toString().includes("transform(")?(c="map",u=this.getAllProducedFields(),l=["_mapResult"]):e.toString().includes("_parallelResults")&&(l=["_parallelResults"],u=this.getAllProducedFields());for(let d of u)this.getAllProducedFields().includes(d)||this.initialFields.add(d);let p={type:c,nodeName:t,dependencies:u,produces:l,stepFunction:e,stepIndex:this.steps.length};this.steps.push(p)}analyzeStepFunctionProduction(e){try{let t=this.analyzeStepFunctionSource(e);if(t.length>0&&!t.includes("_stepResult"))return t}catch(t){console.debug("Step function source analysis failed:",t)}try{let t=this.createMockState(),n=Object.keys(t),o=e(t,{mainAi:{getOptions:()=>({trace:!1}),forward:()=>Promise.resolve({text:"mock"})},mainOptions:void 0});if(o&&typeof o=="object"&&"then"in o)return this.analyzeStepFunctionSource(e);if(o&&typeof o=="object"&&!Array.isArray(o)){let a=Object.keys(o).filter(u=>!n.includes(u));if(a.length>0)return a}}catch(t){console.debug("Step function dynamic analysis failed:",t)}return this.analyzeStepFunctionSource(e)}analyzeStepFunctionSource(e){try{let t=e.toString(),n=t.match(/\{\s*\.\.\.state\s*,\s*(\w+)\s*:/g);if(n){let o=n.map(i=>{let a=i.match(/(\w+)\s*:/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}let r=t.match(/state\.(\w+)\s*=/g);if(r){let o=r.map(i=>{let a=i.match(/state\.(\w+)\s*=/);return a?a[1]:null}).filter(Boolean);if(o.length>0)return o}}catch(t){console.debug("Step function source analysis failed:",t)}return["_stepResult"]}analyzeMapTransformation(e){try{let n=e.toString().split(/\{[\s\S]*?\}/).flatMap(()=>[])}catch{}return["_mapResult"]}createMockState(){let e={};for(let t of this.initialFields)e[t]=this.createMockValue(t);for(let t of this.steps)for(let n of t.produces)n.endsWith("Result")?e[n]={text:"mockText",value:"mockValue",result:"mockResult",data:"mockData",processedText:"mockProcessedText",sentimentValue:"mockSentiment",confidenceScore:.8,isComplex:!1,mockValue:"mockValue",responseText:"mockResponseText",inputText:"mockInputText"}:e[n]=this.createMockValue(n);return e}createMockValue(e){return e.includes("List")||e.includes("Array")||e.endsWith("s")?["mockItem1","mockItem2"]:e.includes("count")||e.includes("Count")||e.includes("index")||e.includes("Index")?0:e.includes("is")||e.includes("has")||e.includes("can")?!1:"mockValue"}analyzeBranchMergeFields(){let e=this.steps.slice(-5).filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces);return e.length>0?e:this.steps.filter(t=>t.type==="execute"&&t.nodeName).flatMap(t=>t.produces)}setInitialFields(e){this.initialFields=new Set(e),this.rebuildParallelGroups()}rebuildParallelGroups(){this.parallelGroups=[];let e=new Set,t=new Set(this.initialFields),n=0;for(;e.size<this.steps.length;){let r=[];for(let o of this.steps){if(e.has(o.stepIndex))continue;if(o.dependencies.length===0||o.dependencies.every(a=>t.has(a))){if(o.type==="merge"&&r.length>0)continue;if(r.push(o),e.add(o.stepIndex),o.type==="merge")break}}if(r.length>0){for(let o of r)o.produces.forEach(i=>t.add(i));this.parallelGroups.push({level:n,steps:r}),n++}else{let o=this.steps.filter(i=>!e.has(i.stepIndex));if(o.length>0){let i=o[0];e.add(i.stepIndex),i.produces.forEach(a=>t.add(a)),this.parallelGroups.push({level:n,steps:[i]}),n++}else break}}}getAllProducedFields(){let e=[];for(let t of this.steps)e.push(...t.produces);return e}createOptimizedExecution(e){let t=[];for(let n of this.parallelGroups)if(n.steps.length===1){let r=n.steps[0];r&&t.push(r.stepFunction)}else if(n.steps.length>1){let r=async(o,i)=>{let a=await $n(n.steps,async c=>await c.stepFunction(o,i),e);if(a.some(c=>c&&typeof c=="object"&&"_parallelResults"in c)){let c=a.find(p=>p&&typeof p=="object"&&"_parallelResults"in p);return c||o}let l=o;for(let c of a)l={...l,...c};return l};t.push(r)}return t}getOptimizedExecutionSteps(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),this.createOptimizedExecution()}getExecutionPlan(){return this.parallelGroups.length===0&&this.steps.length>0&&this.rebuildParallelGroups(),{totalSteps:this.steps.length,parallelGroups:this.parallelGroups.length,maxParallelism:this.steps.length===0?1:Math.max(...this.parallelGroups.map(e=>e.steps.length),0),steps:this.steps,groups:this.parallelGroups}}};var jn=require("@opentelemetry/api");var Bl=s=>{console.log(s)},Gn=(s,e=!1)=>{if(e)return"[State hidden]";let t={};for(let[n,r]of Object.entries(s))if(typeof r=="string"&&r.length>100)t[n]=`${r.substring(0,100)}...`;else if(Array.isArray(r)&&r.length>3)t[n]=[...r.slice(0,3),`... (${r.length-3} more)`];else if(typeof r=="object"&&r!==null){let o=JSON.stringify(r);o.length>200?t[n]=`${o.substring(0,200)}...`:t[n]=r}else t[n]=r;return JSON.stringify(t,null,2)},Un=s=>s<1e3?`${s.toFixed(1)}ms`:s<6e4?`${(s/1e3).toFixed(2)}s`:`${(s/6e4).toFixed(2)}min`,Fr=(s=Bl)=>{let e=new ze,t=e.gray(`${"\u2501".repeat(80)}
645
657
  `),n=e.gray(`${"\u2500".repeat(40)}
646
658
  `);return r=>{let o="";switch(r.name){case"FlowStart":o=`
647
659
  ${e.blueBright("\u{1F504} [ AXFLOW START ]")}
@@ -650,16 +662,16 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
650
662
  `,o+=`${e.white("Parallel Groups:")} ${e.yellow(r.parallelGroups.toString())}
651
663
  `,o+=`${e.white("Max Parallelism:")} ${e.yellow(r.maxParallelism.toString())}
652
664
  `,o+=`${e.white("Auto-Parallel:")} ${r.autoParallelEnabled?e.green("enabled"):e.red("disabled")}
653
- `,o+=t;break;case"StepStart":{let s=r.stepType==="execute"?"\u26A1":r.stepType==="map"?"\u{1F504}":r.stepType==="merge"?"\u{1F500}":r.stepType==="parallel"?"\u2696\uFE0F":"\u{1F4CB}";o=`${e.greenBright(`${s} [ STEP ${r.stepIndex} START ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=`
665
+ `,o+=t;break;case"StepStart":{let i=r.stepType==="execute"?"\u26A1":r.stepType==="map"?"\u{1F504}":r.stepType==="merge"?"\u{1F500}":r.stepType==="parallel"?"\u2696\uFE0F":"\u{1F4CB}";o=`${e.greenBright(`${i} [ STEP ${r.stepIndex} START ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=`
654
666
  `,r.dependencies.length>0&&(o+=`${e.white("Dependencies:")} ${e.gray(r.dependencies.join(", "))}
655
667
  `),r.produces.length>0&&(o+=`${e.white("Produces:")} ${e.cyan(r.produces.join(", "))}
656
- `),o+=`${e.white("State:")} ${e.gray(Jn(r.state,!0))}
657
- `,o+=n;break}case"StepComplete":{let s=(r.stepType==="execute"||r.stepType==="map"||r.stepType==="merge"||r.stepType==="parallel","\u2705");o=`${e.greenBright(`${s} [ STEP ${r.stepIndex} COMPLETE ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=` ${e.magenta(`in ${Qn(r.executionTime)}`)}
668
+ `),o+=`${e.white("State:")} ${e.gray(Gn(r.state,!0))}
669
+ `,o+=n;break}case"StepComplete":{let i=(r.stepType==="execute"||r.stepType==="map"||r.stepType==="merge"||r.stepType==="parallel","\u2705");o=`${e.greenBright(`${i} [ STEP ${r.stepIndex} COMPLETE ]`)} ${e.white(`(${r.stepType})`)}`,r.nodeName&&(o+=` ${e.cyanBright(`Node: ${r.nodeName}`)}`),o+=` ${e.magenta(`in ${Un(r.executionTime)}`)}
658
670
  `,r.newFields&&r.newFields.length>0&&(o+=`${e.white("New Fields:")} ${e.green(r.newFields.join(", "))}
659
671
  `),r.result&&r.nodeName&&(o+=`${e.white("Result:")} ${e.yellow(JSON.stringify(r.result,null,2))}
660
672
  `),o+=n;break}case"ParallelGroupStart":o=`${e.blueBright("\u2696\uFE0F [ PARALLEL GROUP START ]")} ${e.white(`Level ${r.groupLevel}`)}
661
673
  `,o+=`${e.white("Steps:")} ${e.yellow(r.stepsCount.toString())} ${e.gray(`(${r.stepTypes.join(", ")})`)}
662
- `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Qn(r.executionTime)}`)}
674
+ `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Un(r.executionTime)}`)}
663
675
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsCount.toString())}
664
676
  `,o+=n;break;case"BranchEvaluation":o=`${e.yellow("\u{1F500} [ BRANCH EVALUATION ]")}
665
677
  `,o+=`${e.white("Branch Value:")} ${e.cyan(JSON.stringify(r.branchValue))}
@@ -667,16 +679,16 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
667
679
  `,r.hasMatchingBranch&&(o+=`${e.white("Branch Steps:")} ${e.yellow(r.branchStepsCount.toString())}
668
680
  `),o+=n;break;case"FlowComplete":o=`
669
681
  ${e.greenBright("\u2705 [ AXFLOW COMPLETE ]")}
670
- ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Qn(r.totalExecutionTime))}
682
+ ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Un(r.totalExecutionTime))}
671
683
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsExecuted.toString())}
672
684
  `,o+=`${e.white("Output Fields:")} ${e.green(r.outputFields.join(", "))}
673
- `,o+=`${e.white("Final State:")} ${e.gray(Jn(r.finalState,!0))}
685
+ `,o+=`${e.white("Final State:")} ${e.gray(Gn(r.finalState,!0))}
674
686
  `,o+=t;break;case"FlowError":o=`
675
687
  ${e.redBright("\u274C [ AXFLOW ERROR ]")}
676
688
  ${t}`,r.stepIndex!==void 0&&(o+=`${e.white("Step:")} ${e.yellow(r.stepIndex.toString())}`,r.stepType&&(o+=` ${e.gray(`(${r.stepType})`)}`),r.nodeName&&(o+=` ${e.cyan(`Node: ${r.nodeName}`)}`),o+=`
677
689
  `),o+=`${e.white("Error:")} ${e.red(r.error)}
678
- `,r.state&&(o+=`${e.white("State:")} ${e.gray(Jn(r.state,!0))}
679
- `),o+=t;break;default:o=e.gray(JSON.stringify(r,null,2))}i(o)}},ku=(i=Su)=>{let e="=".repeat(80),t="-".repeat(40);return n=>{let r="";switch(n.name){case"FlowStart":r=`
690
+ `,r.state&&(o+=`${e.white("State:")} ${e.gray(Gn(r.state,!0))}
691
+ `),o+=t;break;default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},jl=(s=Bl)=>{let e="=".repeat(80),t="-".repeat(40);return n=>{let r="";switch(n.name){case"FlowStart":r=`
680
692
  [ AXFLOW START ]
681
693
  ${e}
682
694
  `,r+=`Input Fields: ${n.inputFields.join(", ")}
@@ -688,16 +700,16 @@ ${e}
688
700
  `;break;case"StepStart":r=`[ STEP ${n.stepIndex} START ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
689
701
  `,n.dependencies.length>0&&(r+=`Dependencies: ${n.dependencies.join(", ")}
690
702
  `),n.produces.length>0&&(r+=`Produces: ${n.produces.join(", ")}
691
- `),r+=`State: ${Jn(n.state,!0)}
703
+ `),r+=`State: ${Gn(n.state,!0)}
692
704
  `,r+=`${t}
693
- `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Qn(n.executionTime)}
705
+ `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Un(n.executionTime)}
694
706
  `,n.newFields&&n.newFields.length>0&&(r+=`New Fields: ${n.newFields.join(", ")}
695
707
  `),n.result&&n.nodeName&&(r+=`Result: ${JSON.stringify(n.result,null,2)}
696
708
  `),r+=`${t}
697
709
  `;break;case"ParallelGroupStart":r=`[ PARALLEL GROUP START ] Level ${n.groupLevel}
698
710
  `,r+=`Steps: ${n.stepsCount} (${n.stepTypes.join(", ")})
699
711
  `,r+=`${t}
700
- `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Qn(n.executionTime)}
712
+ `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Un(n.executionTime)}
701
713
  `,r+=`Steps Executed: ${n.stepsCount}
702
714
  `,r+=`${t}
703
715
  `;break;case"BranchEvaluation":r=`[ BRANCH EVALUATION ]
@@ -708,153 +720,290 @@ ${e}
708
720
  `;break;case"FlowComplete":r=`
709
721
  [ AXFLOW COMPLETE ]
710
722
  ${e}
711
- `,r+=`Total Time: ${Qn(n.totalExecutionTime)}
723
+ `,r+=`Total Time: ${Un(n.totalExecutionTime)}
712
724
  `,r+=`Steps Executed: ${n.stepsExecuted}
713
725
  `,r+=`Output Fields: ${n.outputFields.join(", ")}
714
- `,r+=`Final State: ${Jn(n.finalState,!0)}
726
+ `,r+=`Final State: ${Gn(n.finalState,!0)}
715
727
  `,r+=`${e}
716
728
  `;break;case"FlowError":r=`
717
729
  [ AXFLOW ERROR ]
718
730
  ${e}
719
731
  `,n.stepIndex!==void 0&&(r+=`Step: ${n.stepIndex}`,n.stepType&&(r+=` (${n.stepType})`),n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
720
732
  `),r+=`Error: ${n.error}
721
- `,n.state&&(r+=`State: ${Jn(n.state,!0)}
733
+ `,n.state&&(r+=`State: ${Gn(n.state,!0)}
722
734
  `),r+=`${e}
723
- `;break;default:r=JSON.stringify(n,null,2)}i(r)}},vu=Dr(),Ou=i=>{let e=new Map;return{logger:i,startTiming:t=>{e.set(t,Date.now())},endTiming:t=>{let n=e.get(t);if(!n)return 0;let r=Date.now()-n;return e.delete(t),r},getCurrentTime:()=>Date.now()}};var Yn=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}},us=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,s)=>{let a=n?.ai??s.mainAi,l=n?.options??s.mainOptions,u=t(o),c=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,u,{...l,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}};var Gr=class i{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Vn;program;flowName;nodeUsage=new Map;nodeTraces=new Map;flowLogger;timingLogger;defaultAIOptions;activeAbortControllers=new Set;_stopRequested=!1;toCamelCase(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}extractInputValues(e){if(!Array.isArray(e))return e;let t=e.filter(n=>n.role==="user").pop();if(!t)throw new Error("No user message found in values array");return t.values}getCacheKey(e,t){if(!t)return;this.ensureProgram();let n=this.program.getSignature(),r=n.getInputFields().map(l=>l.name),o=this.extractInputValues(e),s=ot("sha256");s.update(n.hash()??"");let a=l=>{let u=typeof l;if(s.update(`|${u}|`),l==null){s.update("null");return}if(u==="string"||u==="number"||u==="boolean"){s.update(String(l));return}if(Array.isArray(l)){s.update("[");for(let c of l)a(c);s.update("]");return}if(typeof l=="object"&&l!==null&&"mimeType"in l&&"data"in l){let c=l;s.update(c.mimeType??"");let p=ot("sha256").update(c.data??"").digest("hex");s.update(p);return}if(typeof l=="object"){let c=l,p=Object.keys(c).sort();for(let d of p)s.update(`{${d}}`),a(c[d]);return}s.update(String(l))};for(let l of r)a(o?.[l]);return s.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},s=0;for(let a=0;a<e.length;a++){let l=e[a];if(!l)continue;let u=n.mainOptions?.abortSignal;if(u?.aborted)throw new Le("flow-between-steps",u.reason??"Flow aborted between steps");let c=this.getStepType(l,a),p=this.getStepMetadata(l,a),d=Object.keys(o);this.flowLogger&&this.flowLogger({name:"StepStart",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,dependencies:p.dependencies,produces:p.produces,state:{...o}});let m=Date.now();this.timingLogger?.startTiming(`step-${a}`);try{o=await l(o,n),s++;let h=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,C=Object.keys(o).filter(I=>!d.includes(I)),x;if(c==="execute"&&p.nodeName&&C.length>0){let I=`${p.nodeName}Result`;x=o[I]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:h,state:{...o},newFields:C,result:x})}catch(g){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:g instanceof Error?g.message:String(g),stepIndex:a,stepType:c,nodeName:p.nodeName,state:{...o}}),g}}return{finalState:o,stepsExecuted:s}}getStepType(e,t){let n=this.executionPlanner.getExecutionPlan().steps.find(o=>o.stepIndex===t);if(n)return n.type;let r=e.toString();return r.includes("nodeName")||r.includes("nodeProgram")?"execute":r.includes("_parallelResults")||r.includes("processBatches")?"parallel":r.includes("branchValue")||r.includes("branches.get")||r.includes("mergeFunction")?"merge":r.includes("transform(")||r.includes("...state,")?"map":r.includes("inputValue")&&r.includes("transformFn")?"derive":r.includes("condition(")&&r.includes("iterations")?r.includes("while")?"while":"feedback":r.includes("branchSteps")||r.includes("currentState")?"branch":"other"}getStepMetadata(e,t){let r=this.executionPlanner.getExecutionPlan().steps.find(a=>a.stepIndex===t);if(r)return{nodeName:r.nodeName,dependencies:r.dependencies,produces:r.produces};let o=e.toString();return{nodeName:this.extractNodeNameFromSource(o),dependencies:[],produces:[]}}extractNodeNameFromSource(e){let t=e.match(/nodeName['"]?\s*[=:]\s*['"](\w+)['"]/);if(t)return t[1];let n=e.match(/nodeProgram\.get\(['"](\w+)['"]\)/);if(n)return n[1]}inferSignatureFromFlow(){let e=this.executionPlanner.getExecutionPlan();if(this.nodeGenerators.size===0&&e.steps.length===0)return xe().input("userInput",xe.string("User input to the flow")).output("flowOutput",xe.string("Output from the flow")).build();let t=new Set,n=new Set;for(let c of e.steps)c.produces.forEach(p=>t.add(p)),c.dependencies.forEach(p=>n.add(p));let r=new Set;for(let c of Array.from(n))t.has(c)||r.add(c);let o=new Set,s=e.steps[e.steps.length-1];if(s&&(s.type==="map"||s.type==="merge")){if(s.produces.forEach(c=>{c.startsWith("_")||o.add(c)}),s.type==="merge"&&s.produces.includes("_mergedResult"))for(let c of e.steps)c.type==="execute"&&c.produces.length>0&&c.produces.forEach(p=>o.add(p))}else for(let c of Array.from(t)){let p=!1;for(let d of e.steps)if(d.dependencies.includes(c)){p=!0;break}if(!p)if(c.endsWith("Result")){let d=c.replace("Result",""),m=this.nodeGenerators.get(d);if(m){let h=m.getSignature().getOutputFields();for(let f of h)o.add(f.name)}else o.add(c)}else o.add(c)}if(r.size===0&&o.size===0){let c=[],p=[];for(let[m,g]of Array.from(this.nodeGenerators)){let h=g.getSignature();for(let f of h.getInputFields()){let C=this.toCamelCase(`${m}_${f.name}`);c.push({name:C,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}for(let f of h.getOutputFields()){let C=this.toCamelCase(`${m}_${f.name}`);p.push({name:C,type:f.type,description:f.description,isOptional:f.isOptional,isInternal:f.isInternal})}}let d=new Ge;return c.length>0?d.setInputFields(c):d.addInputField({name:"userInput",type:{name:"string"},description:"User input to the flow"}),p.length>0?d.setOutputFields(p):d.addOutputField({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),d}let a=new Ge,l=[];for(let c of Array.from(r))l.push({name:c,type:{name:"string"},description:`Input field: ${c}`});l.length===0&&l.push({name:"userInput",type:{name:"string"},description:"User input to the flow"});let u=[];for(let c of Array.from(o))c.startsWith("_")||u.push({name:c,type:{name:"string"},description:`Output field: ${c}`});return u.length===0&&u.push({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),a.setInputFields(l),a.setOutputFields(u),a}constructor(e){i._ctorWarned||(console.warn("[AxFlow] new AxFlow() is deprecated. Use flow() factory instead."),i._ctorWarned=!0),this.autoParallelConfig={enabled:e?.autoParallel!==!1,batchSize:e?.batchSize||10},e?.logger?this.flowLogger=e.logger:e?.debug===!0?this.flowLogger=Dr():this.flowLogger=void 0,this.timingLogger=this.flowLogger?Ou(this.flowLogger):void 0,(e?.tracer||e?.meter)&&(this.defaultAIOptions={tracer:e.tracer,meter:e.meter})}static create(e){return new i(e)}ensureProgram(){let e=this.inferSignatureFromFlow();if(!this.program){this.program=new Dt(e);for(let[t,n]of Array.from(this.nodeGenerators))this.program.register(n,t);return}this.program.setSignature(e)}getId(){return this.ensureProgram(),this.program.getId()}setId(e){this.ensureProgram(),this.program.setId(e)}namedPrograms(){return this.ensureProgram(),this.program.namedPrograms()}getTraces(){let e=[];for(let[t,n]of Array.from(this.nodeTraces))e.push(...n);return e}setDemos(e,t){this.ensureProgram(),this.program.setDemos(e,t)}description(e,t){return this.ensureProgram(),this.flowName=e,this.program.setDescription(t),this}toFunction(){this.ensureProgram();let e=this.program.getSignature(),n=(this.flowName??(e.getDescription()?.trim().split(`
724
- `)[0]||"axFlow")).replace(/\s+/g,"_"),r=this.toCamelCase(n),o=async(s,a)=>{let l=a?.ai;if(!l)throw new Error("AI service is required to run the flow");let u=await this.forward(l,s??{}),c=e.getOutputFields(),p=u??{};return Object.keys(p).map(d=>{let m=c.find(g=>g.name===d);return m&&m.title?`${m.title}: ${p[d]}`:`${d}: ${p[d]}`}).join(`
725
- `)};return{name:r,description:e.getDescription()??"Execute this AxFlow",parameters:e.toJSONSchema(),func:o}}getUsage(){let e=[];for(let[t,n]of Array.from(this.nodeUsage))e.push(...n);return wr(e)}resetUsage(){this.nodeUsage.clear();for(let[e,t]of Array.from(this.nodeGenerators))t&&"resetUsage"in t&&t.resetUsage()}resetTraces(){this.nodeTraces.clear()}getUsageReport(){let e={};for(let[t,n]of Array.from(this.nodeUsage))e[t]=wr(n);return e}getNodePrograms(){return Array.from(this.nodeGenerators).map(([e,t])=>({name:e,program:t}))}setNodeInstruction(e,t){let n=this.nodeGenerators.get(e);if(!n)return!1;let r=n;if(typeof r.setInstruction=="function")try{return r.setInstruction(t),!0}catch{return!1}return!1}setAllNodeInstructions(e){for(let[t,n]of Object.entries(e))this.setNodeInstruction(t,n)}getTracesReport(){let e={};for(let[t,n]of Array.from(this.nodeTraces))e[t]=n;return e}async*streamingForward(e,t,n){let r=n?.cachingFunction??me.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let a;try{a=await r(o)}catch{}if(a!==void 0){yield{version:0,index:0,delta:a};return}}let s=await this.forward(e,t,n);if(r&&o)try{await r(o,s)}catch{}yield{version:1,index:0,delta:s}}async forward(e,t,n){let r=n?.cachingFunction??me.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let c;try{c=await r(o)}catch{}if(c!==void 0)return c}let s=Date.now();this.timingLogger?.startTiming("flow-execution");let a={},l,u;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let I=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:s,inputFields:Object.keys(c),totalSteps:I.totalSteps,parallelGroups:I.parallelGroups,maxParallelism:I.maxParallelism,autoParallelEnabled:I.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let I=this.getExecutionPlan(),w=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";l=p.startSpan(w,{kind:Xn.SpanKind.INTERNAL,attributes:{total_steps:I.totalSteps,parallel_groups:I.parallelGroups,max_parallelism:I.maxParallelism,auto_parallel_enabled:I.autoParallelEnabled}});let A=d??Xn.context.active();m=Xn.trace.setSpan(A,l)}u=new AbortController,this.activeAbortControllers.add(u),this._stopRequested&&u.abort("Stopped by user (pre-forward)");let g=Ft(n?.abortSignal,n?.abortController?.signal),h=Ft(u.signal,Ft(g,me.abortSignal)),f={mainAi:e,mainOptions:(()=>{let I={...this.defaultAIOptions??{},...n};return n?.model&&(I.model=String(n.model)),p&&(I.tracer=p),m&&(I.traceContext=m),h&&(I.abortSignal=h),Object.keys(I).length>0?I:void 0})()},C=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,x=0;if(C){this.executionPlanner.setInitialFields(Object.keys(c));let I=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),w=await this.executeStepsWithLogging(I,a,f,!0);a=w.finalState,x=w.stepsExecuted}else{let I=await this.executeStepsWithLogging(this.flowDefinition,a,f,!1);a=I.finalState,x=I.stepsExecuted}if(this.flowLogger){let I=this.timingLogger?.endTiming("flow-execution")??Date.now()-s;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:I,finalState:a,outputFields:Object.keys(a),stepsExecuted:x})}if(r&&o)try{await r(o,a)}catch{}return a}catch(c){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:c instanceof Error?c.message:String(c),state:a}),c}finally{l&&l.end(),u&&this.activeAbortControllers.delete(u),this._stopRequested=!1}}node(e,t){if(this.nodes.has(e)||this.nodeGenerators.has(e))throw new Error(`Node '${e}' is already defined. Use a unique node name in this flow.`);if(typeof t=="string"||t instanceof Ge){let n=t;if(!n)throw new Error(`Invalid signature for node '${e}': signature cannot be empty`);this.nodes.set(e,{inputs:{},outputs:{}});let r=Ye(n);this.nodeGenerators.set(e,r),this.ensureProgram(),this.program.register(r,e)}else if(typeof t=="function"){this.nodes.set(e,{inputs:{},outputs:{}});let n=new t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else if(t&&typeof t=="object"&&"forward"in t){this.nodes.set(e,{inputs:{},outputs:{}});let n=t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else throw new Error(`Invalid second argument for node '${e}': expected string, AxSignature, AxProgrammable instance, or constructor function`);return this}n(e,t){return this.node(e,t)}map(e,t){if(t?.parallel){let n=Array.isArray(e)?e:[e],r=async o=>(await Wn(n,async(l,u)=>{let c=l(o);return Promise.resolve(c)},this.autoParallelConfig.batchSize)).reduce((l,u)=>({...l,...u}),o);if(this.branchContext?.currentBranchValue!==void 0){let o=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];o.push(r),this.branchContext.branches.set(this.branchContext.currentBranchValue,o)}else this.flowDefinition.push(r),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(r,void 0,void 0,"parallel-map",n)}else{let n=async r=>{if(Array.isArray(e))throw new Error("Array of transforms requires parallel: true option");let o=e(r);return Promise.resolve(o)};if(this.branchContext?.currentBranchValue!==void 0){let r=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];r.push(n),this.branchContext.branches.set(this.branchContext.currentBranchValue,r)}else this.flowDefinition.push(n),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(n,void 0,void 0,"map",e)}return this.nodeGenerators.size>0&&this.ensureProgram(),this}m(e,t){return this.map(e,t)}returns(e){let t=async n=>{let r=e(n);return Promise.resolve(r)};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}r(e){return this.returns(e)}label(e){if(this.branchContext?.currentBranchValue!==void 0)throw new Error("Cannot create labels inside branch blocks");return this.stepLabels.set(e,this.flowDefinition.length),this}l(e){return this.label(e)}execute(e,t,n){if(!this.nodes.has(e))throw new Error(`Node '${e}' not found. Make sure to define it with .node() first.`);let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);let o=async(s,a)=>{let l=n?.ai??a.mainAi,u={...a.mainOptions??{},...n?.options??{}},c=t(s),p=u?.traceLabel?`Node:${e} (${u.traceLabel})`:`Node:${e}`,d;if("forward"in r&&typeof r.forward=="function"){if(d=await r.forward(l,c,{...u,traceLabel:p}),"getUsage"in r&&typeof r.getUsage=="function"){let m=r.getUsage();if(m&&m.length>0){let g=this.nodeUsage.get(e)||[];this.nodeUsage.set(e,[...g,...m])}}if("getTraces"in r&&typeof r.getTraces=="function"){let m=r.getTraces();if(m&&m.length>0){let g=this.nodeTraces.get(e)||[];this.nodeTraces.set(e,[...g,...m])}}}else throw new Error(`Node program for '${e}' does not have a forward method`);return{...s,[`${e}Result`]:d}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,e,t);return this.ensureProgram(),this}applyOptimization(e){this.program&&"applyOptimization"in this.program&&this.program.applyOptimization(e);for(let[t,n]of Array.from(this.nodeGenerators))n&&"applyOptimization"in n&&typeof n.applyOptimization=="function"&&n.applyOptimization(e)}e(e,t,n){return this.execute(e,t,n)}branch(e){if(this.branchContext)throw new Error("Nested branches are not supported");return this.branchContext={predicate:t=>e(t),branches:new Map,currentBranchValue:void 0},this}b(e){return this.branch(e)}when(e){if(!this.branchContext)throw new Error("when() called without matching branch()");return this.branchContext.currentBranchValue=e,this.branchContext.branches.set(e,[]),this}w(e){return this.when(e)}merge(){if(!this.branchContext)throw new Error("merge() called without matching branch()");let e=this.branchContext;this.branchContext=null;let t=async(n,r)=>{let o=e.predicate(n),s=e.branches.get(o);if(this.flowLogger&&this.flowLogger({name:"BranchEvaluation",timestamp:Date.now(),branchValue:o,hasMatchingBranch:!!s,branchStepsCount:s?.length??0}),!s)return n;let a=n;for(let l of s)a=await l(a,r);return a};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"merge"),this.ensureProgram(),this}mg(){return this.merge()}parallel(e){let t=async(n,r)=>{let o=await Wn(e,async(s,a)=>{let l=new Yn(this.nodeGenerators);return await s(l).executeSteps(n,r)},this.autoParallelConfig.batchSize);return{...n,_parallelResults:o}};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"parallel",void 0,void 0),this.ensureProgram(),{merge:(n,r)=>{let o=s=>{let a=s._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let l=r(...a),u={...s};return delete u._parallelResults,u[n]=l,u};return this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"merge",void 0,{resultKey:n,mergeFunction:r}),this.ensureProgram(),this}}}p(e){return this.parallel(e)}feedback(e,t,n=10){if(!this.stepLabels.has(t))throw new Error(`Label '${t}' not found. Make sure to define it with .label() before the feedback point.`);let r=this.stepLabels.get(t),o=this.flowDefinition.length;return this.flowDefinition.push(async(s,a)=>{let l=s,u=1,c=`_feedback_${t}_iterations`;for(typeof l[c]!="number"&&(l={...l,[c]:1});e(l)&&u<n;){u++,l={...l,[c]:u};for(let p=r;p<o;p++){let d=this.flowDefinition[p];d&&(l=await d(l,a))}}return l}),this.nodeGenerators.size>0&&this.ensureProgram(),this}fb(e,t,n=10){return this.feedback(e,t,n)}while(e,t=100){let n=this.flowDefinition.length;this.loopStack.push(n);let r=Object.assign(o=>o,{_condition:e,_maxIterations:t,_isLoopStart:!0});return this.flowDefinition.push(r),this.nodeGenerators.size>0&&this.ensureProgram(),this}wh(e,t=100){return this.while(e,t)}endWhile(){if(this.loopStack.length===0)throw new Error("endWhile() called without matching while()");let e=this.loopStack.pop(),t=this.flowDefinition[e];if(!t||!("_isLoopStart"in t))throw new Error("Loop start step not found or invalid");let n=t._condition,r=t._maxIterations,o=this.flowDefinition.splice(e+1);return this.flowDefinition[e]=async(s,a)=>{let l=s,u=0;for(;n(l)&&u<r;){u++;for(let c of o)l=await c(l,a)}if(u>=r&&n(l))throw new Error(`While loop exceeded maximum iterations (${r}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`);return l},this.nodeGenerators.size>0&&this.ensureProgram(),this}end(){return this.endWhile()}derive(e,t,n,r){let o=async s=>{let a=s[t];if(a===void 0)throw new Error(`Input field '${t}' not found in state`);let l;if(Array.isArray(a))if(this.autoParallelConfig.enabled){let u=r?.batchSize||this.autoParallelConfig.batchSize;l=await Wn(a,async(c,p)=>n(c,p,s),u)}else l=a.map((u,c)=>n(u,c,s));else l=n(a,void 0,s);return{...s,[e]:l}};if(this.branchContext?.currentBranchValue!==void 0){let s=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];s.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,s)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"derive",n,void 0,{inputFieldName:t,outputFieldName:e,batchSize:r?.batchSize});return this.ensureProgram(),this}getExecutionPlan(){let e=this.executionPlanner.getExecutionPlan();return{totalSteps:e.totalSteps,parallelGroups:e.parallelGroups,maxParallelism:e.maxParallelism,autoParallelEnabled:this.autoParallelConfig.enabled,steps:e.steps,groups:e.groups}}getSignature(){return this.ensureProgram(),this.program.getSignature()}nodeExtended(e,t,n){let o=typeof t=="string"?Ge.create(t):t;if(n.prependInputs)for(let s of n.prependInputs)o=o.prependInputField(s.name,s.type);if(n.appendInputs)for(let s of n.appendInputs)o=o.appendInputField(s.name,s.type);if(n.prependOutputs)for(let s of n.prependOutputs)o=o.prependOutputField(s.name,s.type);if(n.appendOutputs)for(let s of n.appendOutputs)o=o.appendOutputField(s.name,s.type);return this.node(e,o)}nx(e,t,n){return this.nodeExtended(e,t,n)}mapOutput(e){let t=async n=>{let r=e(n);return{...n,...r}};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}mo(e){return this.mapOutput(e)}};function cs(i){return Gr.create(i)}var ps=class{apiUrl;containerId=null;constructor(e="http://localhost:2375"){this.apiUrl=e}async pullImage(e){let t=await this.fetchDockerAPI(`/images/create?fromImage=${encodeURIComponent(e)}`,{method:"POST"});if(!t.ok)throw new Error(`Failed to pull image: ${t.statusText}`);await t.text()}async createContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let o=t.map(u=>`${u.hostPath}:${u.containerPath}`);n||await this.pullImage(e);let s={Image:e,Tty:!0,OpenStdin:!1,AttachStdin:!1,AttachStdout:!1,AttachStderr:!1,HostConfig:{Binds:o},Labels:{}};r&&(s.Labels["com.example.tag"]=r);let a=await this.fetchDockerAPI("/containers/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!a.ok)throw new Error(`Failed to create container: ${a.statusText}`);let l=await a.json();return this.containerId=l.Id,l}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let s=(await this.listContainers(!0)).filter(l=>l.Labels&&l.Labels["com.example.tag"]===r);if(s&&s.length>0){let l=Math.floor(Math.random()*s.length),u=s[l];if(u)return await this.connectToContainer(u.Id),{Id:u.Id,isNew:!1}}return{Id:(await this.createContainer({imageName:e,volumes:t,doNotPullImage:n,tag:r})).Id,isNew:!0}}async startContainer(){if(!this.containerId)throw new Error("No container created or connected");let e=await this.fetchDockerAPI(`/containers/${this.containerId}/start`,{method:"POST"});if(!e.ok)throw new Error(`Failed to start container: ${e.statusText}`)}async connectToContainer(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to connect to container: ${t.statusText}`);this.containerId=e}async stopContainers({tag:e,remove:t,timeout:n=10}){let r=[],o=await this.listContainers(!0),s=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of s){if(a.State.Status==="running"){let l=await this.fetchDockerAPI(`/containers/${a.Id}/stop?t=${n}`,{method:"POST"});if(!l.ok){console.warn(`Failed to stop container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"stopped"})}if(t){let l=await this.fetchDockerAPI(`/containers/${a.Id}`,{method:"DELETE"});if(!l.ok){console.warn(`Failed to remove container ${a.Id}: ${l.statusText}`);continue}r.push({Id:a.Id,Action:"removed"})}}return r}async listContainers(e=!1){return(await this.fetchDockerAPI(`/containers/json?all=${e}`,{method:"GET"})).json()}async getContainerLogs(){if(!this.containerId)throw new Error("No container created or connected");return(await this.fetchDockerAPI(`/containers/${this.containerId}/logs?stdout=true&stderr=true`,{method:"GET"})).text()}async executeCommand(e){if(!this.containerId)throw new Error("No container created or connected");(await this.getContainerInfo(this.containerId)).State.Status!=="running"&&(await this.startContainer(),await this.waitForContainerToBeRunning(this.containerId));let n=await this.fetchDockerAPI(`/containers/${this.containerId}/exec`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Cmd:["sh","-c",e],AttachStdout:!0,AttachStderr:!0})});if(!n.ok)throw new Error(`Failed to create exec instance: ${n.statusText}`);let r=await n.json(),o=await this.fetchDockerAPI(`/exec/${r.Id}/start`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Detach:!1,Tty:!1})});if(!o.ok)throw new Error(`Failed to start exec instance: ${o.statusText}`);return await o.text()}async getContainerInfo(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to get container info: ${t.statusText}`);return t.json()}async waitForContainerToBeRunning(e,t=3e4){let n=Date.now();for(;Date.now()-n<t;){if((await this.getContainerInfo(e)).State.Status==="running")return;await new Promise(o=>setTimeout(o,1e3))}throw new Error("Timeout waiting for container to start")}async fetchDockerAPI(e,t){let n=new URL(e,this.apiUrl).toString();return await fetch(n,t)}toFunction(){return{name:"commandExecution",description:"Use this function to execute shell commands, scripts, and programs. This function enables interaction with the file system, running system utilities, and performing tasks that require a shell interface.",parameters:{type:"object",properties:{command:{type:"string",description:'Shell command to execute. eg. `ls -l` or `echo "Hello, World!"`.'}},required:["command"]},func:async({command:e})=>await this.executeCommand(e)}}};var ds=class{aiService;info;func;constructor({ai:e,info:t,func:n}){this.aiService=e,this.info=t,this.func=n}async embedAdapter(e,t){let r=(await this.aiService.embed({texts:[e]},{sessionId:t?.sessionId,abortSignal:t?.abortSignal})).embeddings.at(0);if(!r)throw new Error("Failed to embed text");return this.func.length===2?this.func(r,t):this.func(r)}toFunction(){return{name:this.info.name,description:this.info.description,parameters:{type:"object",properties:{text:{type:"string",description:this.info.argumentDescription}},required:["text"]},func:({text:e},t)=>this.embedAdapter(e,t)}}};function ms(i){let e=typeof self<"u"?self:globalThis,t=Object.getPrototypeOf(async()=>{}).constructor,n=i.functionRefKey,r="return",o="stdout",s=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,a=/^\s*return\s+([^\n;]+?)\s*;?\s*$/,l={network:["fetch","XMLHttpRequest","WebSocket","EventSource"],storage:["indexedDB","caches"],"code-loading":["importScripts"],communication:["BroadcastChannel"],timing:["performance"],workers:["Worker","SharedWorker"]},u=()=>{let b;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(b=process.getBuiltinModule.bind(process)),!b)try{b=new Function('return typeof require==="function"?require:undefined')()}catch{b=void 0}if(!(typeof b=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:b("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:c,parentPort:p}=u(),d=()=>{if(!p&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:R=>{if(p){p.postMessage(R);return}e.postMessage(R)},setOnMessage:R=>{if(p){p.on("message",y=>R({data:y}));return}e.onmessage=R}}},{send:m,setOnMessage:g}=d(),h=b=>b&&(/\r?\n$/.test(b)?b:`${b}
726
- `),f=b=>{let S=b.trim();return S.startsWith("//")||S.startsWith("/*")||S.startsWith("*")},C=b=>{let S=b.length-1;for(;S>=0;){let R=b[S].trim();if(R&&!f(R))break;S-=1}return S},x=b=>{if(!b)return!0;let S=b.split(`
727
- `).find(R=>R.trim().length>0)?.trim();return!S||s.test(S)?!0:S.startsWith("{")&&!S.startsWith("({")||S==="}"||S==="};"||f(S)},I=(b,S)=>{let R=b,y=S.trim().replace(/;\s*$/,"");if(!y)return null;let _=y.lastIndexOf(";");if(_!==-1){let q=y.slice(_+1).trim(),L=y.slice(0,_).trim();q&&(q.startsWith("//")||q.startsWith("/*")?L&&(y=L):(L&&(R=R?`${R}
728
- ${L};`:`${L};`),y=q))}return x(y)?null:{head:R,expression:y}},w=(b,S,R)=>{let y=b.slice(0,S).join(`
729
- `),_=b.slice(S,R+1).join(`
730
- `).trim();if(!_)return null;if(!_.includes(`
731
- `)){let q=I(y,_);return q?q.head?`${q.head}
735
+ `;break;default:r=JSON.stringify(n,null,2)}s(r)}},zl=Fr(),ql=s=>{let e=new Map;return{logger:s,startTiming:t=>{e.set(t,Date.now())},endTiming:t=>{let n=e.get(t);if(!n)return 0;let r=Date.now()-n;return e.delete(t),r},getCurrentTime:()=>Date.now()}};var Bn=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,i)=>{let a=n?.ai??i.mainAi,u=n?.options??i.mainOptions,l=t(o),c=u?.traceLabel?`Node:${e} (${u.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,l,{...u,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}},ss=class{constructor(e){this.nodeGenerators=e}steps=[];execute(e,t,n){let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);return this.steps.push(async(o,i)=>{let a=n?.ai??i.mainAi,u=n?.options??i.mainOptions,l=t(o),c=u?.traceLabel?`Node:${e} (${u.traceLabel})`:`Node:${e}`,p;if("forward"in r&&typeof r.forward=="function")p=await r.forward(a,l,{...u,traceLabel:c});else throw new Error(`Node program for '${e}' does not have a forward method`);return{...o,[`${e}Result`]:p}}),this}map(e){return this.steps.push(t=>e(t)),this}async executeSteps(e,t){let n=e;for(let r of this.steps)n=await r(n,t);return n}};var _r=class s{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Dn;program;flowName;nodeUsage=new Map;nodeTraces=new Map;flowLogger;timingLogger;defaultAIOptions;activeAbortControllers=new Set;_stopRequested=!1;toCamelCase(e){return e.replace(/_([a-z])/g,(t,n)=>n.toUpperCase())}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user")}extractInputValues(e){if(!Array.isArray(e))return e;let t=e.filter(n=>n.role==="user").pop();if(!t)throw new Error("No user message found in values array");return t.values}getCacheKey(e,t){if(!t)return;this.ensureProgram();let n=this.program.getSignature(),r=n.getInputFields().map(u=>u.name),o=this.extractInputValues(e),i=He("sha256");i.update(n.hash()??"");let a=u=>{let l=typeof u;if(i.update(`|${l}|`),u==null){i.update("null");return}if(l==="string"||l==="number"||l==="boolean"){i.update(String(u));return}if(Array.isArray(u)){i.update("[");for(let c of u)a(c);i.update("]");return}if(typeof u=="object"&&u!==null&&"mimeType"in u&&"data"in u){let c=u;i.update(c.mimeType??"");let p=He("sha256").update(c.data??"").digest("hex");i.update(p);return}if(typeof u=="object"){let c=u,p=Object.keys(c).sort();for(let d of p)i.update(`{${d}}`),a(c[d]);return}i.update(String(u))};for(let u of r)a(o?.[u]);return i.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},i=0;for(let a=0;a<e.length;a++){let u=e[a];if(!u)continue;let l=n.mainOptions?.abortSignal;if(l?.aborted)throw new ke("flow-between-steps",l.reason??"Flow aborted between steps");let c=this.getStepType(u,a),p=this.getStepMetadata(u,a),d=Object.keys(o);this.flowLogger&&this.flowLogger({name:"StepStart",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,dependencies:p.dependencies,produces:p.produces,state:{...o}});let m=Date.now();this.timingLogger?.startTiming(`step-${a}`);try{o=await u(o,n),i++;let f=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,y=Object.keys(o).filter(A=>!d.includes(A)),h;if(c==="execute"&&p.nodeName&&y.length>0){let A=`${p.nodeName}Result`;h=o[A]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:f,state:{...o},newFields:y,result:h})}catch(g){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:g instanceof Error?g.message:String(g),stepIndex:a,stepType:c,nodeName:p.nodeName,state:{...o}}),g}}return{finalState:o,stepsExecuted:i}}getStepType(e,t){let n=this.executionPlanner.getExecutionPlan().steps.find(o=>o.stepIndex===t);if(n)return n.type;let r=e.toString();return r.includes("nodeName")||r.includes("nodeProgram")?"execute":r.includes("_parallelResults")||r.includes("processBatches")?"parallel":r.includes("branchValue")||r.includes("branches.get")||r.includes("mergeFunction")?"merge":r.includes("transform(")||r.includes("...state,")?"map":r.includes("inputValue")&&r.includes("transformFn")?"derive":r.includes("condition(")&&r.includes("iterations")?r.includes("while")?"while":"feedback":r.includes("branchSteps")||r.includes("currentState")?"branch":"other"}getStepMetadata(e,t){let r=this.executionPlanner.getExecutionPlan().steps.find(a=>a.stepIndex===t);if(r)return{nodeName:r.nodeName,dependencies:r.dependencies,produces:r.produces};let o=e.toString();return{nodeName:this.extractNodeNameFromSource(o),dependencies:[],produces:[]}}extractNodeNameFromSource(e){let t=e.match(/nodeName['"]?\s*[=:]\s*['"](\w+)['"]/);if(t)return t[1];let n=e.match(/nodeProgram\.get\(['"](\w+)['"]\)/);if(n)return n[1]}inferSignatureFromFlow(){let e=this.executionPlanner.getExecutionPlan();if(this.nodeGenerators.size===0&&e.steps.length===0)return ue().input("userInput",ue.string("User input to the flow")).output("flowOutput",ue.string("Output from the flow")).build();let t=new Set,n=new Set;for(let c of e.steps)c.produces.forEach(p=>t.add(p)),c.dependencies.forEach(p=>n.add(p));let r=new Set;for(let c of Array.from(n))t.has(c)||r.add(c);let o=new Set,i=e.steps[e.steps.length-1];if(i&&(i.type==="map"||i.type==="merge")){if(i.produces.forEach(c=>{c.startsWith("_")||o.add(c)}),i.type==="merge"&&i.produces.includes("_mergedResult"))for(let c of e.steps)c.type==="execute"&&c.produces.length>0&&c.produces.forEach(p=>o.add(p))}else for(let c of Array.from(t)){let p=!1;for(let d of e.steps)if(d.dependencies.includes(c)){p=!0;break}if(!p)if(c.endsWith("Result")){let d=c.replace("Result",""),m=this.nodeGenerators.get(d);if(m){let f=m.getSignature().getOutputFields();for(let x of f)o.add(x.name)}else o.add(c)}else o.add(c)}if(r.size===0&&o.size===0){let c=[],p=[];for(let[m,g]of Array.from(this.nodeGenerators)){let f=g.getSignature();for(let x of f.getInputFields()){let y=this.toCamelCase(`${m}_${x.name}`);c.push({name:y,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}for(let x of f.getOutputFields()){let y=this.toCamelCase(`${m}_${x.name}`);p.push({name:y,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}}let d=new Re;return c.length>0?d.setInputFields(c):d.addInputField({name:"userInput",type:{name:"string"},description:"User input to the flow"}),p.length>0?d.setOutputFields(p):d.addOutputField({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),d}let a=new Re,u=[];for(let c of Array.from(r))u.push({name:c,type:{name:"string"},description:`Input field: ${c}`});u.length===0&&u.push({name:"userInput",type:{name:"string"},description:"User input to the flow"});let l=[];for(let c of Array.from(o))c.startsWith("_")||l.push({name:c,type:{name:"string"},description:`Output field: ${c}`});return l.length===0&&l.push({name:"flowOutput",type:{name:"string"},description:"Output from the flow"}),a.setInputFields(u),a.setOutputFields(l),a}constructor(e){s._ctorWarned||(console.warn("[AxFlow] new AxFlow() is deprecated. Use flow() factory instead."),s._ctorWarned=!0),this.autoParallelConfig={enabled:e?.autoParallel!==!1,batchSize:e?.batchSize||10},e?.logger?this.flowLogger=e.logger:e?.debug===!0?this.flowLogger=Fr():this.flowLogger=void 0,this.timingLogger=this.flowLogger?ql(this.flowLogger):void 0,(e?.tracer||e?.meter)&&(this.defaultAIOptions={tracer:e.tracer,meter:e.meter})}static create(e){return new s(e)}ensureProgram(){let e=this.inferSignatureFromFlow();if(!this.program){this.program=new vt(e);for(let[t,n]of Array.from(this.nodeGenerators))this.program.register(n,t);return}this.program.setSignature(e)}getId(){return this.ensureProgram(),this.program.getId()}setId(e){this.ensureProgram(),this.program.setId(e)}namedPrograms(){return this.ensureProgram(),this.program.namedPrograms()}namedProgramInstances(){return this.ensureProgram(),this.program.namedProgramInstances()}getTraces(){let e=[];for(let[t,n]of Array.from(this.nodeTraces))e.push(...n);return e}setDemos(e,t){this.ensureProgram(),this.program.setDemos(e,t)}description(e,t){return this.ensureProgram(),this.flowName=e,this.program.setDescription(t),this}toFunction(){this.ensureProgram();let e=this.program.getSignature(),n=(this.flowName??(e.getDescription()?.trim().split(`
736
+ `)[0]||"axFlow")).replace(/\s+/g,"_"),r=this.toCamelCase(n),o=async(i,a)=>{let u=a?.ai;if(!u)throw new Error("AI service is required to run the flow");let l=await this.forward(u,i??{}),c=e.getOutputFields(),p=l??{};return Object.keys(p).map(d=>{let m=c.find(g=>g.name===d);return m&&m.title?`${m.title}: ${p[d]}`:`${d}: ${p[d]}`}).join(`
737
+ `)};return{name:r,description:e.getDescription()??"Execute this AxFlow",parameters:e.toJSONSchema(),func:o}}getUsage(){let e=[];for(let[t,n]of Array.from(this.nodeUsage))e.push(...n);return Ir(e)}resetUsage(){this.nodeUsage.clear();for(let[e,t]of Array.from(this.nodeGenerators))t&&"resetUsage"in t&&t.resetUsage()}resetTraces(){this.nodeTraces.clear()}getUsageReport(){let e={};for(let[t,n]of Array.from(this.nodeUsage))e[t]=Ir(n);return e}getNodePrograms(){return Array.from(this.nodeGenerators).map(([e,t])=>({name:e,program:t}))}setNodeInstruction(e,t){let n=this.nodeGenerators.get(e);if(!n)return!1;let r=n;if(typeof r.setInstruction=="function")try{return r.setInstruction(t),!0}catch{return!1}return!1}setAllNodeInstructions(e){for(let[t,n]of Object.entries(e))this.setNodeInstruction(t,n)}getTracesReport(){let e={};for(let[t,n]of Array.from(this.nodeTraces))e[t]=n;return e}async*streamingForward(e,t,n){let r=n?.cachingFunction??re.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let a;try{a=await r(o)}catch{}if(a!==void 0){yield{version:0,index:0,delta:a};return}}let i=await this.forward(e,t,n);if(r&&o)try{await r(o,i)}catch{}yield{version:1,index:0,delta:i}}async forward(e,t,n){let r=n?.cachingFunction??re.cachingFunction,o=this.getCacheKey(t,r);if(r&&o){let c;try{c=await r(o)}catch{}if(c!==void 0)return c}let i=Date.now();this.timingLogger?.startTiming("flow-execution");let a={},u,l;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let A=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:i,inputFields:Object.keys(c),totalSteps:A.totalSteps,parallelGroups:A.parallelGroups,maxParallelism:A.maxParallelism,autoParallelEnabled:A.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let A=this.getExecutionPlan(),b=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";u=p.startSpan(b,{kind:jn.SpanKind.INTERNAL,attributes:{total_steps:A.totalSteps,parallel_groups:A.parallelGroups,max_parallelism:A.maxParallelism,auto_parallel_enabled:A.autoParallelEnabled}});let I=d??jn.context.active();m=jn.trace.setSpan(I,u)}l=new AbortController,this.activeAbortControllers.add(l),this._stopRequested&&l.abort("Stopped by user (pre-forward)");let g=At(n?.abortSignal,n?.abortController?.signal),f=At(l.signal,At(g,re.abortSignal)),x={mainAi:e,mainOptions:(()=>{let A={...this.defaultAIOptions??{},...n};return n?.model&&(A.model=String(n.model)),p&&(A.tracer=p),m&&(A.traceContext=m),f&&(A.abortSignal=f),Object.keys(A).length>0?A:void 0})()},y=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,h=0;if(y){this.executionPlanner.setInitialFields(Object.keys(c));let A=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),b=await this.executeStepsWithLogging(A,a,x,!0);a=b.finalState,h=b.stepsExecuted}else{let A=await this.executeStepsWithLogging(this.flowDefinition,a,x,!1);a=A.finalState,h=A.stepsExecuted}if(this.flowLogger){let A=this.timingLogger?.endTiming("flow-execution")??Date.now()-i;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:A,finalState:a,outputFields:Object.keys(a),stepsExecuted:h})}if(r&&o)try{await r(o,a)}catch{}return a}catch(c){throw this.flowLogger&&this.flowLogger({name:"FlowError",timestamp:Date.now(),error:c instanceof Error?c.message:String(c),state:a}),c}finally{u&&u.end(),l&&this.activeAbortControllers.delete(l),this._stopRequested=!1}}node(e,t){if(this.nodes.has(e)||this.nodeGenerators.has(e))throw new Error(`Node '${e}' is already defined. Use a unique node name in this flow.`);if(typeof t=="string"||t instanceof Re){let n=t;if(!n)throw new Error(`Invalid signature for node '${e}': signature cannot be empty`);this.nodes.set(e,{inputs:{},outputs:{}});let r=qe(n);this.nodeGenerators.set(e,r),this.ensureProgram(),this.program.register(r,e)}else if(typeof t=="function"){this.nodes.set(e,{inputs:{},outputs:{}});let n=new t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else if(t&&typeof t=="object"&&"forward"in t){this.nodes.set(e,{inputs:{},outputs:{}});let n=t;this.nodeGenerators.set(e,n),this.ensureProgram(),this.program.register(n,e)}else throw new Error(`Invalid second argument for node '${e}': expected string, AxSignature, AxProgrammable instance, or constructor function`);return this}n(e,t){return this.node(e,t)}map(e,t){if(t?.parallel){let n=Array.isArray(e)?e:[e],r=async o=>(await $n(n,async(u,l)=>{let c=u(o);return Promise.resolve(c)},this.autoParallelConfig.batchSize)).reduce((u,l)=>({...u,...l}),o);if(this.branchContext?.currentBranchValue!==void 0){let o=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];o.push(r),this.branchContext.branches.set(this.branchContext.currentBranchValue,o)}else this.flowDefinition.push(r),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(r,void 0,void 0,"parallel-map",n)}else{let n=async r=>{if(Array.isArray(e))throw new Error("Array of transforms requires parallel: true option");let o=e(r);return Promise.resolve(o)};if(this.branchContext?.currentBranchValue!==void 0){let r=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];r.push(n),this.branchContext.branches.set(this.branchContext.currentBranchValue,r)}else this.flowDefinition.push(n),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(n,void 0,void 0,"map",e)}return this.nodeGenerators.size>0&&this.ensureProgram(),this}m(e,t){return this.map(e,t)}returns(e){let t=async n=>{let r=e(n);return Promise.resolve(r)};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}r(e){return this.returns(e)}label(e){if(this.branchContext?.currentBranchValue!==void 0)throw new Error("Cannot create labels inside branch blocks");return this.stepLabels.set(e,this.flowDefinition.length),this}l(e){return this.label(e)}execute(e,t,n){if(!this.nodes.has(e))throw new Error(`Node '${e}' not found. Make sure to define it with .node() first.`);let r=this.nodeGenerators.get(e);if(!r)throw new Error(`Node program for '${e}' not found.`);let o=async(i,a)=>{let u=n?.ai??a.mainAi,l={...a.mainOptions??{},...n?.options??{}},c=t(i),p=l?.traceLabel?`Node:${e} (${l.traceLabel})`:`Node:${e}`,d;if("forward"in r&&typeof r.forward=="function"){if(d=await r.forward(u,c,{...l,traceLabel:p}),"getUsage"in r&&typeof r.getUsage=="function"){let m=r.getUsage();if(m&&m.length>0){let g=this.nodeUsage.get(e)||[];this.nodeUsage.set(e,[...g,...m])}}if("getTraces"in r&&typeof r.getTraces=="function"){let m=r.getTraces();if(m&&m.length>0){let g=this.nodeTraces.get(e)||[];this.nodeTraces.set(e,[...g,...m])}}}else throw new Error(`Node program for '${e}' does not have a forward method`);return{...i,[`${e}Result`]:d}};if(this.branchContext?.currentBranchValue!==void 0){let i=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];i.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,i)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,e,t);return this.ensureProgram(),this}applyOptimization(e){this.program&&"applyOptimization"in this.program&&this.program.applyOptimization(e)}e(e,t,n){return this.execute(e,t,n)}branch(e){if(this.branchContext)throw new Error("Nested branches are not supported");return this.branchContext={predicate:t=>e(t),branches:new Map,currentBranchValue:void 0},this}b(e){return this.branch(e)}when(e){if(!this.branchContext)throw new Error("when() called without matching branch()");return this.branchContext.currentBranchValue=e,this.branchContext.branches.set(e,[]),this}w(e){return this.when(e)}merge(){if(!this.branchContext)throw new Error("merge() called without matching branch()");let e=this.branchContext;this.branchContext=null;let t=async(n,r)=>{let o=e.predicate(n),i=e.branches.get(o);if(this.flowLogger&&this.flowLogger({name:"BranchEvaluation",timestamp:Date.now(),branchValue:o,hasMatchingBranch:!!i,branchStepsCount:i?.length??0}),!i)return n;let a=n;for(let u of i)a=await u(a,r);return a};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"merge"),this.ensureProgram(),this}mg(){return this.merge()}parallel(e){let t=async(n,r)=>{let o=await $n(e,async(i,a)=>{let u=new Bn(this.nodeGenerators);return await i(u).executeSteps(n,r)},this.autoParallelConfig.batchSize);return{...n,_parallelResults:o}};return this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"parallel",void 0,void 0),this.ensureProgram(),{merge:(n,r)=>{let o=i=>{let a=i._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let u=r(...a),l={...i};return delete l._parallelResults,l[n]=u,l};return this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"merge",void 0,{resultKey:n,mergeFunction:r}),this.ensureProgram(),this}}}p(e){return this.parallel(e)}feedback(e,t,n=10){if(!this.stepLabels.has(t))throw new Error(`Label '${t}' not found. Make sure to define it with .label() before the feedback point.`);let r=this.stepLabels.get(t),o=this.flowDefinition.length;return this.flowDefinition.push(async(i,a)=>{let u=i,l=1,c=`_feedback_${t}_iterations`;for(typeof u[c]!="number"&&(u={...u,[c]:1});e(u)&&l<n;){l++,u={...u,[c]:l};for(let p=r;p<o;p++){let d=this.flowDefinition[p];d&&(u=await d(u,a))}}return u}),this.nodeGenerators.size>0&&this.ensureProgram(),this}fb(e,t,n=10){return this.feedback(e,t,n)}while(e,t=100){let n=this.flowDefinition.length;this.loopStack.push(n);let r=Object.assign(o=>o,{_condition:e,_maxIterations:t,_isLoopStart:!0});return this.flowDefinition.push(r),this.nodeGenerators.size>0&&this.ensureProgram(),this}wh(e,t=100){return this.while(e,t)}endWhile(){if(this.loopStack.length===0)throw new Error("endWhile() called without matching while()");let e=this.loopStack.pop(),t=this.flowDefinition[e];if(!t||!("_isLoopStart"in t))throw new Error("Loop start step not found or invalid");let n=t._condition,r=t._maxIterations,o=this.flowDefinition.splice(e+1);return this.flowDefinition[e]=async(i,a)=>{let u=i,l=0;for(;n(u)&&l<r;){l++;for(let c of o)u=await c(u,a)}if(l>=r&&n(u))throw new Error(`While loop exceeded maximum iterations (${r}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`);return u},this.nodeGenerators.size>0&&this.ensureProgram(),this}end(){return this.endWhile()}derive(e,t,n,r){let o=async i=>{let a=i[t];if(a===void 0)throw new Error(`Input field '${t}' not found in state`);let u;if(Array.isArray(a))if(this.autoParallelConfig.enabled){let l=r?.batchSize||this.autoParallelConfig.batchSize;u=await $n(a,async(c,p)=>n(c,p,i),l)}else u=a.map((l,c)=>n(l,c,i));else u=n(a,void 0,i);return{...i,[e]:u}};if(this.branchContext?.currentBranchValue!==void 0){let i=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];i.push(o),this.branchContext.branches.set(this.branchContext.currentBranchValue,i)}else this.flowDefinition.push(o),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(o,void 0,void 0,"derive",n,void 0,{inputFieldName:t,outputFieldName:e,batchSize:r?.batchSize});return this.ensureProgram(),this}getExecutionPlan(){let e=this.executionPlanner.getExecutionPlan();return{totalSteps:e.totalSteps,parallelGroups:e.parallelGroups,maxParallelism:e.maxParallelism,autoParallelEnabled:this.autoParallelConfig.enabled,steps:e.steps,groups:e.groups}}getSignature(){return this.ensureProgram(),this.program.getSignature()}nodeExtended(e,t,n){let o=typeof t=="string"?Re.create(t):t;if(n.prependInputs)for(let i of n.prependInputs)o=o.prependInputField(i.name,i.type);if(n.appendInputs)for(let i of n.appendInputs)o=o.appendInputField(i.name,i.type);if(n.prependOutputs)for(let i of n.prependOutputs)o=o.prependOutputField(i.name,i.type);if(n.appendOutputs)for(let i of n.appendOutputs)o=o.appendOutputField(i.name,i.type);return this.node(e,o)}nx(e,t,n){return this.nodeExtended(e,t,n)}mapOutput(e){let t=async n=>{let r=e(n);return{...n,...r}};if(this.branchContext?.currentBranchValue!==void 0){let n=this.branchContext.branches.get(this.branchContext.currentBranchValue)||[];n.push(t),this.branchContext.branches.set(this.branchContext.currentBranchValue,n)}else this.flowDefinition.push(t),this.autoParallelConfig.enabled&&this.executionPlanner.addExecutionStep(t,void 0,void 0,"map",e);return this.nodeGenerators.size>0&&this.ensureProgram(),this}mo(e){return this.mapOutput(e)}};function is(s){return _r.create(s)}var as=class{apiUrl;containerId=null;constructor(e="http://localhost:2375"){this.apiUrl=e}async pullImage(e){let t=await this.fetchDockerAPI(`/images/create?fromImage=${encodeURIComponent(e)}`,{method:"POST"});if(!t.ok)throw new Error(`Failed to pull image: ${t.statusText}`);await t.text()}async createContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let o=t.map(l=>`${l.hostPath}:${l.containerPath}`);n||await this.pullImage(e);let i={Image:e,Tty:!0,OpenStdin:!1,AttachStdin:!1,AttachStdout:!1,AttachStderr:!1,HostConfig:{Binds:o},Labels:{}};r&&(i.Labels["com.example.tag"]=r);let a=await this.fetchDockerAPI("/containers/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});if(!a.ok)throw new Error(`Failed to create container: ${a.statusText}`);let u=await a.json();return this.containerId=u.Id,u}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let i=(await this.listContainers(!0)).filter(u=>u.Labels&&u.Labels["com.example.tag"]===r);if(i&&i.length>0){let u=Math.floor(Math.random()*i.length),l=i[u];if(l)return await this.connectToContainer(l.Id),{Id:l.Id,isNew:!1}}return{Id:(await this.createContainer({imageName:e,volumes:t,doNotPullImage:n,tag:r})).Id,isNew:!0}}async startContainer(){if(!this.containerId)throw new Error("No container created or connected");let e=await this.fetchDockerAPI(`/containers/${this.containerId}/start`,{method:"POST"});if(!e.ok)throw new Error(`Failed to start container: ${e.statusText}`)}async connectToContainer(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to connect to container: ${t.statusText}`);this.containerId=e}async stopContainers({tag:e,remove:t,timeout:n=10}){let r=[],o=await this.listContainers(!0),i=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of i){if(a.State.Status==="running"){let u=await this.fetchDockerAPI(`/containers/${a.Id}/stop?t=${n}`,{method:"POST"});if(!u.ok){console.warn(`Failed to stop container ${a.Id}: ${u.statusText}`);continue}r.push({Id:a.Id,Action:"stopped"})}if(t){let u=await this.fetchDockerAPI(`/containers/${a.Id}`,{method:"DELETE"});if(!u.ok){console.warn(`Failed to remove container ${a.Id}: ${u.statusText}`);continue}r.push({Id:a.Id,Action:"removed"})}}return r}async listContainers(e=!1){return(await this.fetchDockerAPI(`/containers/json?all=${e}`,{method:"GET"})).json()}async getContainerLogs(){if(!this.containerId)throw new Error("No container created or connected");return(await this.fetchDockerAPI(`/containers/${this.containerId}/logs?stdout=true&stderr=true`,{method:"GET"})).text()}async executeCommand(e){if(!this.containerId)throw new Error("No container created or connected");(await this.getContainerInfo(this.containerId)).State.Status!=="running"&&(await this.startContainer(),await this.waitForContainerToBeRunning(this.containerId));let n=await this.fetchDockerAPI(`/containers/${this.containerId}/exec`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Cmd:["sh","-c",e],AttachStdout:!0,AttachStderr:!0})});if(!n.ok)throw new Error(`Failed to create exec instance: ${n.statusText}`);let r=await n.json(),o=await this.fetchDockerAPI(`/exec/${r.Id}/start`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({Detach:!1,Tty:!1})});if(!o.ok)throw new Error(`Failed to start exec instance: ${o.statusText}`);return await o.text()}async getContainerInfo(e){let t=await this.fetchDockerAPI(`/containers/${e}/json`);if(!t.ok)throw new Error(`Failed to get container info: ${t.statusText}`);return t.json()}async waitForContainerToBeRunning(e,t=3e4){let n=Date.now();for(;Date.now()-n<t;){if((await this.getContainerInfo(e)).State.Status==="running")return;await new Promise(o=>setTimeout(o,1e3))}throw new Error("Timeout waiting for container to start")}async fetchDockerAPI(e,t){let n=new URL(e,this.apiUrl).toString();return await fetch(n,t)}toFunction(){return{name:"commandExecution",description:"Use this function to execute shell commands, scripts, and programs. This function enables interaction with the file system, running system utilities, and performing tasks that require a shell interface.",parameters:{type:"object",properties:{command:{type:"string",description:'Shell command to execute. eg. `ls -l` or `echo "Hello, World!"`.'}},required:["command"]},func:async({command:e})=>await this.executeCommand(e)}}};var us=class{aiService;info;func;constructor({ai:e,info:t,func:n}){this.aiService=e,this.info=t,this.func=n}async embedAdapter(e,t){let r=(await this.aiService.embed({texts:[e]},{sessionId:t?.sessionId,abortSignal:t?.abortSignal})).embeddings.at(0);if(!r)throw new Error("Failed to embed text");return this.func.length===2?this.func(r,t):this.func(r)}toFunction(){return{name:this.info.name,description:this.info.description,parameters:{type:"object",properties:{text:{type:"string",description:this.info.argumentDescription}},required:["text"]},func:({text:e},t)=>this.embedAdapter(e,t)}}};function zn(s){return!!s&&/[A-Za-z0-9_$]/.test(s)}function Nr(s){let e="",t=0,n="normal",r=!1;for(;t<s.length;){let o=s[t]??"",i=s[t+1]??"";if(n==="lineComment"){o===`
738
+ `?(e+=`
739
+ `,n="normal"):e+=" ",t++;continue}if(n==="blockComment"){o==="*"&&i==="/"?(e+=" ",t+=2,n="normal"):(e+=o===`
740
+ `?`
741
+ `:" ",t++);continue}if(n==="single"||n==="double"||n==="template"){let a=n==="single"?"'":n==="double"?'"':"`";if(r){e+=o===`
742
+ `?`
743
+ `:" ",r=!1,t++;continue}if(o==="\\"){e+=" ",r=!0,t++;continue}if(o===a){e+=" ",n="normal",t++;continue}e+=o===`
744
+ `?`
745
+ `:" ",t++;continue}if(o==="/"&&i==="/"){e+=" ",t+=2,n="lineComment";continue}if(o==="/"&&i==="*"){e+=" ",t+=2,n="blockComment";continue}if(o==="'"){e+=" ",t++,n="single";continue}if(o==='"'){e+=" ",t++,n="double";continue}if(o==="`"){e+=" ",t++,n="template";continue}e+=o,t++}return e}function ls(s){let e=[],t=s.length,n=0,r=0,o=0,i=y=>{if(n++,y==="`"){let h=0;for(;n<t;){let A=s[n];if(A==="\\"){n+=2;continue}if(h>0){A==="{"?h++:A==="}"&&h--,n++;continue}if(A==="$"&&n+1<t&&s[n+1]==="{"){h++,n+=2;continue}if(A==="`"){n++;return}n++}return}for(;n<t;){let h=s[n];if(h==="\\"){n+=2;continue}if(h===y){n++;return}n++}},a=()=>{for(n+=2;n<t&&s[n]!==`
746
+ `;)n++},u=()=>{for(n+=2;n<t;){if(s[n]==="*"&&n+1<t&&s[n+1]==="/"){n+=2;return}n++}},l=()=>{let y=n;for(;n<t&&zn(s[n]);)n++;return s.slice(y,n)},c=()=>{let y=n;for(;n<t;){let h=s[n];if(h===" "||h===" "||h===`
747
+ `||h==="\r"){n++;continue}if(h==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){u();continue}}break}return n>y},p=y=>{let h=1;for(;n<t&&h>0;){if(c(),n>=t)return;let A=s[n];if(A===y){h--,n++;continue}if(A==="{"||A==="["){let b=A==="{"?"}":"]";n++,p(b);continue}if(A==="."&&n+2<t&&s[n+1]==="."&&s[n+2]==="."){if(n+=3,c(),n<t&&zn(s[n])){let b=l();b&&e.push(b)}continue}if(A===","){n++;continue}if(A==="="){n++;let b=0;for(;n<t;){let I=s[n];if(I==="'"||I==='"'||I==="`"){i(I);continue}if(I==="("||I==="["||I==="{"){b++,n++;continue}if(I===")"||I==="]"||I==="}"){if(b>0){b--,n++;continue}break}if(I===","&&b===0)break;n++}continue}if(zn(A)){let b=l();if(c(),n<t&&s[n]===":"){if(n++,c(),n<t){let I=s[n];if(I==="{"||I==="["){let T=I==="{"?"}":"]";n++,p(T)}else if(zn(I)){let T=l();T&&e.push(T)}}}else b&&e.push(b);continue}n++}},d=()=>{let y=0;for(;n<t;){let h=s[n];if(h==="'"||h==='"'||h==="`"){i(h);continue}if(h==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){u();continue}}if(h==="("||h==="["||h==="{"){y++,n++;continue}if(h===")"||h==="]"||h==="}"){if(y>0){y--,n++;continue}return!1}if(h===","&&y===0)return n++,!0;if(h===";"&&y===0)return n++,!1;if(h===`
748
+ `&&y===0){let A=n;return n++,c(),n<t&&s[n]===","?(n++,!0):(n=A,!1)}n++}return!1},m=()=>{for(;n<t;){if(c(),n>=t)return;let y=s[n];if(y==="{"){if(n++,p("}"),!d())return;continue}if(y==="["){if(n++,p("]"),!d())return;continue}if(zn(y)){let h=l();if(h&&e.push(h),!d())return;continue}return}},g=y=>{if(y===0)return!0;let h=y-1;for(;h>=0;){let A=s[h];if(A===" "||A===" "||A==="\r"){h--;continue}return A===`
749
+ `||A===";"||A==="{"||A==="}"}return!0};for(;n<t;){let y=s[n];if(y==="'"||y==='"'||y==="`"){i(y);continue}if(y==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){u();continue}}if(y==="{"){r++,n++;continue}if(y==="}"){r--,n++;continue}if(y==="("){o++,n++;continue}if(y===")"){o--,n++;continue}if(r===0&&o===0&&zn(y)){let h=n,A=l();(A==="var"||A==="let"||A==="const")&&n<t&&(s[n]===" "||s[n]===" "||s[n]===`
750
+ `)&&g(h)&&m();continue}n++}let f=new Set,x=[];for(let y of e)f.has(y)||(f.add(y),x.push(y));return x}function cs(s){let e=typeof self<"u"?self:globalThis,t=Object.getPrototypeOf(async()=>{}).constructor,n=s.functionRefKey,r="return",o="stdout",i=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,a=/^\s*return\s+([^\n;]+?)\s*;?\s*$/,u={network:["fetch","XMLHttpRequest","WebSocket","EventSource"],storage:["indexedDB","caches"],"code-loading":["importScripts"],communication:["BroadcastChannel"],timing:["performance"],workers:["Worker","SharedWorker"]},l=()=>{let C;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(C=process.getBuiltinModule.bind(process)),!C)try{C=new Function('return typeof require==="function"?require:undefined')()}catch{C=void 0}if(!(typeof C=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:C("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:c,parentPort:p}=l(),d=()=>{if(!p&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:M=>{if(p){p.postMessage(M);return}e.postMessage(M)},setOnMessage:M=>{if(p){p.on("message",w=>M({data:w}));return}e.onmessage=M}}},{send:m,setOnMessage:g}=d(),f=C=>C&&(/\r?\n$/.test(C)?C:`${C}
751
+ `),x=C=>{let S=C.trim();return S.startsWith("//")||S.startsWith("/*")||S.startsWith("*")},y=C=>{let S=C.length-1;for(;S>=0;){let M=C[S].trim();if(M&&!x(M))break;S-=1}return S},h=C=>{if(!C)return!0;let S=C.split(`
752
+ `).find(M=>M.trim().length>0)?.trim();return!S||i.test(S)?!0:S.startsWith("{")&&!S.startsWith("({")||S==="}"||S==="};"||x(S)},A=(C,S)=>{let M=C,w=S.trim().replace(/;\s*$/,"");if(!w)return null;let B=w.lastIndexOf(";");if(B!==-1){let N=w.slice(B+1).trim(),D=w.slice(0,B).trim();N&&(N.startsWith("//")||N.startsWith("/*")?D&&(w=D):(D&&(M=M?`${M}
753
+ ${D};`:`${D};`),w=N))}return h(w)?null:{head:M,expression:w}},b=(C,S,M)=>{let w=C.slice(0,S).join(`
754
+ `),B=C.slice(S,M+1).join(`
755
+ `).trim();if(!B)return null;if(!B.includes(`
756
+ `)){let N=A(w,B);return N?N.head?`${N.head}
732
757
  return (
733
- ${q.expression}
758
+ ${N.expression}
734
759
  );`:`return (
735
- ${q.expression}
736
- );`:null}return x(_)?null:y?`${y}
760
+ ${N.expression}
761
+ );`:null}return h(B)?null:w?`${w}
737
762
  return (
738
- ${_}
763
+ ${B}
739
764
  );`:`return (
740
- ${_}
741
- );`},A=b=>{try{return new t(b),!0}catch{return!1}},T=b=>{let S=b.split(`
742
- `),R=C(S);if(R<0)return b;let y=new Set;for(let _=R;_>=0;_-=1){let q=S[_]??"";if(!q.trim()||f(q))continue;let L=w(S,_,R);if(L&&!y.has(L)&&(y.add(L),A(L)))return L}return b},k=b=>{let S=a.exec(b);return S&&(S[1]||"").trim()||b},E=b=>{let S=[],R=b.length,y=0,_=0,q=0,L=Q=>Q>="a"&&Q<="z"||Q>="A"&&Q<="Z"||Q>="0"&&Q<="9"||Q==="_"||Q==="$",G=Q=>{if(y++,Q==="`"){let X=0;for(;y<R;){let ue=b[y];if(ue==="\\"){y+=2;continue}if(X>0){ue==="{"?X++:ue==="}"&&X--,y++;continue}if(ue==="$"&&y+1<R&&b[y+1]==="{"){X++,y+=2;continue}if(ue==="`"){y++;return}y++}}else for(;y<R;){let X=b[y];if(X==="\\"){y+=2;continue}if(X===Q){y++;return}y++}},U=()=>{for(y+=2;y<R&&b[y]!==`
743
- `;)y++},H=()=>{for(y+=2;y<R;){if(b[y]==="*"&&y+1<R&&b[y+1]==="/"){y+=2;return}y++}},Y=()=>{let Q=y;for(;y<R&&L(b[y]);)y++;return b.slice(Q,y)},ne=()=>{let Q=y;for(;y<R;){let X=b[y];if(X===" "||X===" "||X===`
744
- `||X==="\r"){y++;continue}if(X==="/"&&y+1<R){if(b[y+1]==="/"){U();continue}if(b[y+1]==="*"){H();continue}}break}return y>Q},ee=Q=>{let X=1;for(;y<R&&X>0;){if(ne(),y>=R)return;let ue=b[y];if(ue===Q){X--,y++;continue}if(ue==="{"||ue==="["){let de=ue==="{"?"}":"]";y++,X++,y--,X--,y++,ee(de);continue}if(ue==="."&&y+2<R&&b[y+1]==="."&&b[y+2]==="."){if(y+=3,ne(),y<R&&L(b[y])){let de=Y();de&&S.push(de)}continue}if(ue===","){y++;continue}if(ue==="="){y++;let de=0;for(;y<R;){let Re=b[y];if(Re==="'"||Re==='"'||Re==="`"){G(Re);continue}if(Re==="("||Re==="["||Re==="{"){de++,y++;continue}if(Re===")"||Re==="]"||Re==="}"){if(de>0){de--,y++;continue}break}if(Re===","&&de===0)break;y++}continue}if(L(ue)){let de=Y();if(ne(),y<R&&b[y]===":"){if(y++,ne(),y<R){let Re=b[y];if(Re==="{"||Re==="["){let Xe=Re==="{"?"}":"]";y++,ee(Xe)}else if(L(Re)){let Xe=Y();Xe&&S.push(Xe)}}}else de&&S.push(de);continue}y++}},pe=()=>{let Q=0;for(;y<R;){let X=b[y];if(X==="'"||X==='"'||X==="`"){G(X);continue}if(X==="/"&&y+1<R){if(b[y+1]==="/"){U();continue}if(b[y+1]==="*"){H();continue}}if(X==="("||X==="["||X==="{"){Q++,y++;continue}if(X===")"||X==="]"||X==="}"){if(Q>0){Q--,y++;continue}return!1}if(X===","&&Q===0)return y++,!0;if(X===";"&&Q===0)return y++,!1;if(X===`
745
- `&&Q===0){let ue=y;return y++,ne(),y<R&&b[y]===","?(y++,!0):(y=ue,!1)}y++}return!1},fe=()=>{for(;y<R;){if(ne(),y>=R)return;let Q=b[y];if(Q==="{"){if(y++,ee("}"),!pe())return;continue}if(Q==="["){if(y++,ee("]"),!pe())return;continue}if(L(Q)){let X=Y();if(X&&S.push(X),!pe())return;continue}return}},Ie=Q=>{if(Q===0)return!0;let X=Q-1;for(;X>=0;){let ue=b[X];if(ue===" "||ue===" "||ue==="\r"){X--;continue}return ue===`
746
- `||ue===";"||ue==="{"||ue==="}"}return!0};for(;y<R;){let Q=b[y];if(Q==="'"||Q==='"'||Q==="`"){G(Q);continue}if(Q==="/"&&y+1<R){if(b[y+1]==="/"){U();continue}if(b[y+1]==="*"){H();continue}}if(Q==="{"){_++,y++;continue}if(Q==="}"){_--,y++;continue}if(Q==="("){q++,y++;continue}if(Q===")"){q--,y++;continue}if(_===0&&q===0&&L(Q)){let X=y,ue=Y();if((ue==="var"||ue==="let"||ue==="const")&&y<R&&(b[y]===" "||b[y]===" "||b[y]===`
747
- `)&&Ie(X)){fe();continue}continue}y++}let Se=new Set,Pe=[];for(let Q of S)Se.has(Q)||(Se.add(Q),Pe.push(Q));return Pe},M=b=>b.length===0?"":`
748
- try { ${b.map(R=>`globalThis[${JSON.stringify(R)}] = ${R};`).join(" ")} } catch (_ax_e) {} void 0;`,$=b=>{if(typeof b=="string")return b;try{return JSON.stringify(b,null,2)}catch{return String(b)}},B=(b,S)=>{let R=e.console&&typeof e.console=="object"?e.console:null,y=R?.[b],_=typeof y=="function"?(...L)=>y.apply(R,L):null,q=(...L)=>{S.push(L.map($).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[b]=q,()=>{if(!(!e.console||typeof e.console!="object")){if(_){e.console[b]=_;return}try{delete e.console[b]}catch{e.console[b]=void 0}}}},V=b=>{let S=[];return we&&(S.push(B("log",b)),S.push(B("info",b)),S.push(B("warn",b)),S.push(B("error",b))),S},O=()=>{let b=[],S=V(b),R=e.print;return le===o&&(e.print=(..._)=>{b.push(_.map($).join(" "))}),{output:b,cleanup:()=>{for(let _ of S)try{_()}catch{}if(le===o)if(R===void 0)try{delete e.print}catch{e.print=void 0}else e.print=R}}},j=b=>{for(let S of b)try{Object.defineProperty(e,S,{value:void 0,writable:!1,configurable:!1})}catch{}},F=b=>{let S=new Set(Array.isArray(b)?b:[]);for(let[R,y]of Object.entries(l))S.has(R)||j(y)},W=b=>{c&&!b&&j(["process","require"])},P=i.maxErrorCauseDepth,D=(b,S=0,R=new Set)=>{if(S>P)return{name:"Error",message:"[cause chain truncated]"};if(b&&typeof b=="object"){if(R.has(b))return{name:"Error",message:"[circular]"};R.add(b)}let y=b,_=y?.name!=null?String(y.name):"Error",q=y?.message!=null?String(y.message):J(b),L=typeof y?.stack=="string"?y.stack:void 0,G;if(typeof y?.cause<"u"&&S<P)try{let H=y.cause;H instanceof Error||H&&typeof H=="object"&&("message"in H||"name"in H)?G=D(H,S+1,R):G={name:"Error",message:J(H)}}catch{G={name:"Error",message:J(y.cause)}}let U={name:_,message:q};if(L!==void 0&&(U.stack=L),G!==void 0&&(U.cause=G),typeof y?.data<"u")try{U.data=typeof structuredClone=="function"?structuredClone(y.data):y.data}catch{}return U},K=b=>{if(typeof b=="string")return new Error(b);if(!b||typeof b!="object")return new Error(String(b));let S=b,R=new Error(S.message!=null?String(S.message):"");return R.name=S.name!=null?String(S.name):"Error",typeof S.stack=="string"&&(R.stack=S.stack),S.cause!==void 0&&(R.cause=K(S.cause)),S.data!==void 0&&(R.data=S.data),R},N=b=>{let S=globalThis.AggregateError,R=typeof S=="function"&&b instanceof S;return b instanceof SyntaxError||b instanceof TypeError||b instanceof RangeError||b instanceof ReferenceError||R||b instanceof EvalError||b instanceof URIError},J=b=>{if(b==null||typeof b!="object")return String(b);try{return JSON.stringify(b,null,2)}catch{return String(b)}},ye=(b,S,R=0)=>{let y=b,_=y?.name!=null?String(y.name):"Error",q=y?.message!=null?String(y.message):J(b),L=[`${_}: ${q}`],G;if(typeof y?.stack=="string"){let U=y.stack.match(/<anonymous>:(\d+):(\d+)/);U&&(G=Math.max(1,Number(U[1])-R),L.push(` at line ${G}, column ${U[2]}`))}if(S&&G!==void 0&&G>=1&&G<=S.split(`
749
- `).length){let U=S.split(`
750
- `),H=Math.max(0,G-2),Y=Math.min(U.length,G+1),ee=U.slice(H,Y).map((pe,fe)=>` ${String(H+fe+1).padStart(3)}| ${pe}`).join(`
751
- `);L.push(`Source:
752
- ${ee}`)}if(y?.data!==void 0&&L.push(`Data: ${J(y.data)}`),y?.cause!==void 0){let U=(H,Y)=>{if(Y>4)return"[cause chain truncated]";let ne=H,ee=ne?.name!=null?String(ne.name):"Error",pe=ne?.message!=null?String(ne.message):J(H),fe=[`${ee}: ${pe}`];return ne?.data!==void 0&&fe.push(`Data: ${J(ne.data)}`),ne?.cause!==void 0&&fe.push(`Caused by: ${U(ne.cause,Y+1)}`),fe.join(`
753
- `)};L.push(`Caused by: ${U(y.cause,1)}`)}return L.join(`
754
- `)},oe=new Map,re=0,le=r,we=!1,Ue=b=>(...S)=>{let R=++re;return new Promise((y,_)=>{oe.set(R,{resolve:y,reject:_}),m({type:"fn-call",id:R,name:b,args:S})})},se=b=>{if(!b||typeof b!="object")return b;if(Array.isArray(b)){for(let R=0;R<b.length;R+=1)b[R]=se(b[R]);return b}let S=b;if(n in S){let R=S[n];return typeof R=="string"?Ue(R):void 0}for(let[R,y]of Object.entries(S))S[R]=se(y);return b},ve=b=>{if(!b||typeof b!="object"||Array.isArray(b))return!1;let S=Object.getPrototypeOf(b);return S===Object.prototype||S===null},ie=b=>{for(let[S,R]of Object.entries(b)){let y=se(R),_=e[S];if(ve(_)&&ve(y)){for(let q of Object.keys(_))Object.hasOwn(y,q)||delete _[q];for(let[q,L]of Object.entries(y))_[q]=L;continue}e[S]=y}},ce=b=>{if(b.globals&&typeof b.globals=="object"&&ie(b.globals),Array.isArray(b.fnNames))for(let S of b.fnNames)typeof S=="string"&&(e[S]=Ue(S))},be=async b=>{let S=h(b),R=[];try{R=E(b)}catch{R=[]}let y=M(R),_=S;try{_=T(S)}catch{_=S}let q=_;if(y){let U=_.lastIndexOf(`
755
- return (`);U!==-1?q=_.slice(0,U)+y+_.slice(U):q=_+y}let L=A(q)?q:A(_)?_:S;return await new t(L)()},Z=b=>{let S=k(b),R=[];try{R=E(b)}catch{R=[]}let y=M(R),_=y?S+y:S;return(0,eval)(_)},Oe=(b,S)=>{if(le!==o)return b;let R=S.join(`
756
- `).trim();return R||b};g(async b=>{let S=b.data;if(!S||typeof S!="object"||typeof S.type!="string")return;if(S.type==="init"){le=S.outputMode===o?o:r,we=S.captureConsole!==void 0?!!S.captureConsole:le===o;let G=S.allowUnsafeNodeHostAccess===!0;ce(S),F(S.permissions),W(G);return}if(S.type==="fn-result"){if(typeof S.id!="number")return;let G=oe.get(S.id);G&&(oe.delete(S.id),S.error!==void 0?G.reject(K(S.error)):G.resolve(S.value));return}if(S.type==="update-globals"){if(typeof S.id!="number")return;try{S.globals&&typeof S.globals=="object"&&ie(S.globals),m({type:"result",id:S.id,value:void 0})}catch(G){m({type:"result",id:S.id,error:D(G)})}return}if(S.type!=="execute"||typeof S.id!="number"||typeof S.code!="string")return;let R=S.id,y=S.code,_=/\bawait\b/.test(y),{output:q,cleanup:L}=O();try{let G=_?await be(y):Z(y),U=Oe(G,q);try{m({type:"result",id:R,value:U})}catch{m({type:"result",id:R,value:String(U)})}}catch(G){N(G)?m({type:"result",id:R,value:ye(G,y,_?2:0)}):m({type:"result",id:R,error:D(G)})}finally{L()}})}var Bi=4,Mu=16,ji="__ax_rlm_fn_ref__",qi=16;function Eu(){let i={functionRefKey:ji,maxErrorCauseDepth:qi},e=`(${ms.toString()})(${JSON.stringify(i)});
765
+ ${B}
766
+ );`},I=C=>{try{return new t(C),!0}catch{return!1}},T=C=>{let S=C.split(`
767
+ `),M=y(S);if(M<0)return C;let w=new Set;for(let B=M;B>=0;B-=1){let N=S[B]??"";if(!N.trim()||x(N))continue;let D=b(S,B,M);if(D&&!w.has(D)&&(w.add(D),I(D)))return D}return C},R=C=>{let S=a.exec(C);return S&&(S[1]||"").trim()||C},k=C=>{let S=[],M=C.length,w=0,B=0,N=0,D=z=>z>="a"&&z<="z"||z>="A"&&z<="Z"||z>="0"&&z<="9"||z==="_"||z==="$",q=z=>{if(w++,z==="`"){let Q=0;for(;w<M;){let de=C[w];if(de==="\\"){w+=2;continue}if(Q>0){de==="{"?Q++:de==="}"&&Q--,w++;continue}if(de==="$"&&w+1<M&&C[w+1]==="{"){Q++,w+=2;continue}if(de==="`"){w++;return}w++}}else for(;w<M;){let Q=C[w];if(Q==="\\"){w+=2;continue}if(Q===z){w++;return}w++}},te=()=>{for(w+=2;w<M&&C[w]!==`
768
+ `;)w++},ce=()=>{for(w+=2;w<M;){if(C[w]==="*"&&w+1<M&&C[w+1]==="/"){w+=2;return}w++}},W=()=>{let z=w;for(;w<M&&D(C[w]);)w++;return C.slice(z,w)},ee=()=>{let z=w;for(;w<M;){let Q=C[w];if(Q===" "||Q===" "||Q===`
769
+ `||Q==="\r"){w++;continue}if(Q==="/"&&w+1<M){if(C[w+1]==="/"){te();continue}if(C[w+1]==="*"){ce();continue}}break}return w>z},fe=z=>{let Q=1;for(;w<M&&Q>0;){if(ee(),w>=M)return;let de=C[w];if(de===z){Q--,w++;continue}if(de==="{"||de==="["){let Me=de==="{"?"}":"]";w++,Q++,w--,Q--,w++,fe(Me);continue}if(de==="."&&w+2<M&&C[w+1]==="."&&C[w+2]==="."){if(w+=3,ee(),w<M&&D(C[w])){let Me=W();Me&&S.push(Me)}continue}if(de===","){w++;continue}if(de==="="){w++;let Me=0;for(;w<M;){let xe=C[w];if(xe==="'"||xe==='"'||xe==="`"){q(xe);continue}if(xe==="("||xe==="["||xe==="{"){Me++,w++;continue}if(xe===")"||xe==="]"||xe==="}"){if(Me>0){Me--,w++;continue}break}if(xe===","&&Me===0)break;w++}continue}if(D(de)){let Me=W();if(ee(),w<M&&C[w]===":"){if(w++,ee(),w<M){let xe=C[w];if(xe==="{"||xe==="["){let Pe=xe==="{"?"}":"]";w++,fe(Pe)}else if(D(xe)){let Pe=W();Pe&&S.push(Pe)}}}else Me&&S.push(Me);continue}w++}},we=()=>{let z=0;for(;w<M;){let Q=C[w];if(Q==="'"||Q==='"'||Q==="`"){q(Q);continue}if(Q==="/"&&w+1<M){if(C[w+1]==="/"){te();continue}if(C[w+1]==="*"){ce();continue}}if(Q==="("||Q==="["||Q==="{"){z++,w++;continue}if(Q===")"||Q==="]"||Q==="}"){if(z>0){z--,w++;continue}return!1}if(Q===","&&z===0)return w++,!0;if(Q===";"&&z===0)return w++,!1;if(Q===`
770
+ `&&z===0){let de=w;return w++,ee(),w<M&&C[w]===","?(w++,!0):(w=de,!1)}w++}return!1},ve=()=>{for(;w<M;){if(ee(),w>=M)return;let z=C[w];if(z==="{"){if(w++,fe("}"),!we())return;continue}if(z==="["){if(w++,fe("]"),!we())return;continue}if(D(z)){let Q=W();if(Q&&S.push(Q),!we())return;continue}return}},nt=z=>{if(z===0)return!0;let Q=z-1;for(;Q>=0;){let de=C[Q];if(de===" "||de===" "||de==="\r"){Q--;continue}return de===`
771
+ `||de===";"||de==="{"||de==="}"}return!0};for(;w<M;){let z=C[w];if(z==="'"||z==='"'||z==="`"){q(z);continue}if(z==="/"&&w+1<M){if(C[w+1]==="/"){te();continue}if(C[w+1]==="*"){ce();continue}}if(z==="{"){B++,w++;continue}if(z==="}"){B--,w++;continue}if(z==="("){N++,w++;continue}if(z===")"){N--,w++;continue}if(B===0&&N===0&&D(z)){let Q=w,de=W();if((de==="var"||de==="let"||de==="const")&&w<M&&(C[w]===" "||C[w]===" "||C[w]===`
772
+ `)&&nt(Q)){ve();continue}continue}w++}let Ee=new Set,ge=[];for(let z of S)Ee.has(z)||(Ee.add(z),ge.push(z));return ge},v=C=>C.length===0?"":`
773
+ try { ${C.map(M=>`globalThis[${JSON.stringify(M)}] = ${M};`).join(" ")} } catch (_ax_e) {} void 0;`,O=C=>{if(typeof C=="string")return C;try{return JSON.stringify(C,null,2)}catch{return String(C)}},P=(C,S)=>{let M=e.console&&typeof e.console=="object"?e.console:null,w=M?.[C],B=typeof w=="function"?(...D)=>w.apply(M,D):null,N=(...D)=>{S.push(D.map(O).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[C]=N,()=>{if(!(!e.console||typeof e.console!="object")){if(B){e.console[C]=B;return}try{delete e.console[C]}catch{e.console[C]=void 0}}}},$=C=>{let S=[];return Y&&(S.push(P("log",C)),S.push(P("info",C)),S.push(P("warn",C)),S.push(P("error",C))),S},E=()=>{let C=[],S=$(C),M=e.print;return K===o&&(e.print=(...B)=>{C.push(B.map(O).join(" "))}),{output:C,cleanup:()=>{for(let B of S)try{B()}catch{}if(K===o)if(M===void 0)try{delete e.print}catch{e.print=void 0}else e.print=M}}},F=C=>{for(let S of C)try{Object.defineProperty(e,S,{value:void 0,writable:!1,configurable:!1})}catch{}},_=C=>{let S=new Set(Array.isArray(C)?C:[]);for(let[M,w]of Object.entries(u))S.has(M)||F(w)},j=C=>{c&&!C&&F(["process","require"])},H=s.maxErrorCauseDepth,L=(C,S=0,M=new Set)=>{if(S>H)return{name:"Error",message:"[cause chain truncated]"};if(C&&typeof C=="object"){if(M.has(C))return{name:"Error",message:"[circular]"};M.add(C)}let w=C,B=w?.name!=null?String(w.name):"Error",N=w?.message!=null?String(w.message):se(C),D=typeof w?.stack=="string"?w.stack:void 0,q;if(typeof w?.cause<"u"&&S<H)try{let ce=w.cause;ce instanceof Error||ce&&typeof ce=="object"&&("message"in ce||"name"in ce)?q=L(ce,S+1,M):q={name:"Error",message:se(ce)}}catch{q={name:"Error",message:se(w.cause)}}let te={name:B,message:N};if(D!==void 0&&(te.stack=D),q!==void 0&&(te.cause=q),typeof w?.data<"u")try{te.data=typeof structuredClone=="function"?structuredClone(w.data):w.data}catch{}return te},X=C=>{if(typeof C=="string")return new Error(C);if(!C||typeof C!="object")return new Error(String(C));let S=C,M=new Error(S.message!=null?String(S.message):"");return M.name=S.name!=null?String(S.name):"Error",typeof S.stack=="string"&&(M.stack=S.stack),S.cause!==void 0&&(M.cause=X(S.cause)),S.data!==void 0&&(M.data=S.data),M},U=C=>{let S=globalThis.AggregateError,M=typeof S=="function"&&C instanceof S;return C instanceof SyntaxError||C instanceof TypeError||C instanceof RangeError||C instanceof ReferenceError||M||C instanceof EvalError||C instanceof URIError},se=C=>{if(C==null||typeof C!="object")return String(C);try{return JSON.stringify(C,null,2)}catch{return String(C)}},be=(C,S,M=0)=>{let w=C,B=w?.name!=null?String(w.name):"Error",N=w?.message!=null?String(w.message):se(C),D=[`${B}: ${N}`],q;if(typeof w?.stack=="string"){let te=w.stack.match(/<anonymous>:(\d+):(\d+)/);te&&(q=Math.max(1,Number(te[1])-M),D.push(` at line ${q}, column ${te[2]}`))}if(S&&q!==void 0&&q>=1&&q<=S.split(`
774
+ `).length){let te=S.split(`
775
+ `),ce=Math.max(0,q-2),W=Math.min(te.length,q+1),fe=te.slice(ce,W).map((we,ve)=>` ${String(ce+ve+1).padStart(3)}| ${we}`).join(`
776
+ `);D.push(`Source:
777
+ ${fe}`)}if(w?.data!==void 0&&D.push(`Data: ${se(w.data)}`),w?.cause!==void 0){let te=(ce,W)=>{if(W>4)return"[cause chain truncated]";let ee=ce,fe=ee?.name!=null?String(ee.name):"Error",we=ee?.message!=null?String(ee.message):se(ce),ve=[`${fe}: ${we}`];return ee?.data!==void 0&&ve.push(`Data: ${se(ee.data)}`),ee?.cause!==void 0&&ve.push(`Caused by: ${te(ee.cause,W+1)}`),ve.join(`
778
+ `)};D.push(`Caused by: ${te(w.cause,1)}`)}return D.join(`
779
+ `)},G=new Map,Z=0,K=r,Y=!1,me=C=>(...S)=>{let M=++Z;return new Promise((w,B)=>{G.set(M,{resolve:w,reject:B}),m({type:"fn-call",id:M,name:C,args:S})})},V=C=>{if(!C||typeof C!="object")return C;if(Array.isArray(C)){for(let M=0;M<C.length;M+=1)C[M]=V(C[M]);return C}let S=C;if(n in S){let M=S[n];return typeof M=="string"?me(M):void 0}for(let[M,w]of Object.entries(S))S[M]=V(w);return C},le=C=>{if(!C||typeof C!="object"||Array.isArray(C))return!1;let S=Object.getPrototypeOf(C);return S===Object.prototype||S===null},Te=C=>{for(let[S,M]of Object.entries(C)){let w=V(M),B=e[S];if(le(B)&&le(w)){for(let N of Object.keys(B))Object.hasOwn(w,N)||delete B[N];for(let[N,D]of Object.entries(w))B[N]=D;continue}e[S]=w}},ie=C=>{if(C.globals&&typeof C.globals=="object"&&Te(C.globals),Array.isArray(C.fnNames))for(let S of C.fnNames)typeof S=="string"&&(e[S]=me(S))},pe=async C=>{let S=f(C),M=[];try{M=k(C)}catch{M=[]}let w=v(M),B=S;try{B=T(S)}catch{B=S}let N=B;if(w){let te=B.lastIndexOf(`
780
+ return (`);te!==-1?N=B.slice(0,te)+w+B.slice(te):N=B+w}let D=I(N)?N:I(B)?B:S;return await new t(D)()},ne=C=>{let S=R(C),M=[];try{M=k(C)}catch{M=[]}let w=v(M),B=w?S+w:S;return(0,eval)(B)},he=(C,S)=>{if(K!==o)return C;let M=S.join(`
781
+ `).trim();return M||C};g(async C=>{let S=C.data;if(!S||typeof S!="object"||typeof S.type!="string")return;if(S.type==="init"){K=S.outputMode===o?o:r,Y=S.captureConsole!==void 0?!!S.captureConsole:K===o;let q=S.allowUnsafeNodeHostAccess===!0;ie(S),_(S.permissions),j(q);return}if(S.type==="fn-result"){if(typeof S.id!="number")return;let q=G.get(S.id);q&&(G.delete(S.id),S.error!==void 0?q.reject(X(S.error)):q.resolve(S.value));return}if(S.type==="update-globals"){if(typeof S.id!="number")return;try{S.globals&&typeof S.globals=="object"&&Te(S.globals),m({type:"result",id:S.id,value:void 0})}catch(q){m({type:"result",id:S.id,error:L(q)})}return}if(S.type!=="execute"||typeof S.id!="number"||typeof S.code!="string")return;let M=S.id,w=S.code,B=/\bawait\b/.test(w),{output:N,cleanup:D}=E();try{let q=B?await pe(w):ne(w),te=he(q,N);try{m({type:"result",id:M,value:te})}catch{m({type:"result",id:M,value:String(te)})}}catch(q){U(q)?m({type:"result",id:M,value:be(q,w,B?2:0)}):m({type:"result",id:M,error:L(q)})}finally{D()}})}var qi=4,Hl=16,Hi="__ax_rlm_fn_ref__",Ki=16;function Kl(){let s={functionRefKey:Hi,maxErrorCauseDepth:Ki},e=`(${cs.toString()})(${JSON.stringify(s)});
757
782
  `,t="";return e.includes("__name")&&(t+=`var __name=(fn,_n)=>fn;
758
- `),t?`${t}${e}`:e}var Pu=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",gs=()=>typeof process<"u"&&!!process.versions?.node,Yp=()=>!!globalThis.Deno?.version?.deno,Xp=()=>globalThis.Deno?.version?.deno??null,Zp=i=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(i.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},Fu=i=>Number.isFinite(i)?Math.max(1,Math.min(Mu,Math.floor(i))):Bi,ed=()=>{if(!gs())return null;let e=globalThis.process?.getBuiltinModule;if(typeof e!="function")return null;let n=e("node:os")?.availableParallelism;if(typeof n!="function")return null;let r=n();return Number.isFinite(r)&&r>0?r:null},td=i=>{if(i!==void 0)return Fu(i);let e=ed();return e?Fu(Math.ceil(e/2)):Bi},nd=i=>i?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",rd=i=>{let e=new Set(i),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},od=(i,e)=>{let t=Xp(),n=t?Zp(t):null;if(n?n.major>=1:!0)try{return new Worker(i,{type:"module",deno:{permissions:rd(e)}})}catch{}return new Worker(i,{type:"module"})},_u=(i,e)=>{let t=new Blob([i],{type:"application/javascript"}),n=URL.createObjectURL(t),r=Yp()?od(n,e):new Worker(n),o=!1,s=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:l=>r.postMessage(l),terminate:()=>{r.terminate(),s()},onmessage:null,onerror:null};return r.onmessage=l=>{a.onmessage?.({data:l.data})},r.onerror=l=>{a.onerror?.(new Error(l.message||"Worker error"))},a},Hi=async i=>{let e="node:worker_threads",{Worker:t}=await import(e),n=new t(i,{eval:!0}),r=null,o=null,s=!1;n.on("error",l=>{o?o(l):r=l}),n.on("exit",l=>{if(s=!0,l!==0&&!r){let u=new Error(`Worker exited with code ${l}`);o?o(u):r=u}}),n.on("message",l=>{a.onmessage?.({data:l})});let a={postMessage:l=>n.postMessage(l),terminate:()=>{n.terminate()},onmessage:null,get onerror(){return o},set onerror(l){if(o=l,l&&r){let u=r;r=null,l(u)}},get exited(){return s}};return a},Ki=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(gs())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,Hi(this.source).then(e=>{e.onmessage=null,e.onerror=null,this.idle.push(e)}).catch(()=>{}).finally(()=>{this.pendingCreates-=1})}async acquire(){for(;this.idle.length>0;){let e=this.idle.pop();if(!e.exited)return this.warm(),e}return this.warm(),Hi(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},Nu=new Map,sd=(i,e)=>`${e}:${i}`,id=(i,e)=>{let t=sd(i,e),n=Nu.get(t);if(n)return n;let r=new Ki(i,e);return Nu.set(t,r),r},$u=(i,e)=>{let t={},n=new Map,r=0,o=new WeakMap,s=(a,l)=>{if(typeof a=="function"){let g=`fn_${e?.nextFnId?e.nextFnId():++r}_${l||"root"}`;return n.set(g,a),{[ji]:g}}if(!a||typeof a!="object")return a;if(o.has(a))return o.get(a);if(Array.isArray(a)){let m=[];o.set(a,m);for(let g=0;g<a.length;g+=1)m[g]=s(a[g],`${l}[${g}]`);return m}let u=Object.getPrototypeOf(a);if(!(u===Object.prototype||u===null))return a;let p=a,d={};o.set(a,d);for(let[m,g]of Object.entries(p))d[m]=s(g,l?`${l}.${m}`:m);return d};if(i)for(let[a,l]of Object.entries(i))t[a]=s(l,a);return{serializableGlobals:t,fnMap:n}},Lu=i=>{if(typeof structuredClone=="function")try{structuredClone(i)}catch(e){let t=e instanceof Error?e.message:String(e);throw new Error(`RLM globals must be structured-cloneable: ${t}`)}};function zi(i){if(i==null||typeof i!="object")return String(i);try{return JSON.stringify(i,null,2)}catch{return String(i)}}function Du(i,e=qi,t=0,n=new Set){if(t>e)return{name:"Error",message:"[cause chain truncated]"};if(i!==null&&typeof i=="object"){if(n.has(i))return{name:"Error",message:"[circular]"};n.add(i)}let r=i!==null&&typeof i=="object"&&i.name!=null?String(i.name):"Error",o=i!==null&&typeof i=="object"&&i.message!=null?String(i.message):zi(i),s=i!==null&&typeof i=="object"&&typeof i.stack=="string"?i.stack:void 0,a,l=i;if(l&&typeof l=="object"&&l.cause!==void 0&&t<e)try{let p=l.cause;p instanceof Error||p!==null&&typeof p=="object"&&("message"in p||"name"in p)?a=Du(p,e,t+1,n):a={name:"Error",message:zi(p)}}catch{a={name:"Error",message:zi(l.cause)}}let u={name:r,message:o};s!==void 0&&(u.stack=s),a!==void 0&&(u.cause=a);let c=i;if(c&&typeof c=="object"&&"data"in c&&c.data!==void 0)try{typeof structuredClone=="function"?u.data=structuredClone(c.data):u.data=c.data}catch{}return u}function Gu(i){if(typeof i=="string")return new Error(i);if(!i||typeof i!="object")return new Error(String(i));let e=i.message!=null?String(i.message):"Unknown error",t=new Error(e);return t.name=i.name!=null?String(i.name):"Error",typeof i.stack=="string"&&(t.stack=i.stack),i.cause!==void 0&&(t.cause=Gu(i.cause)),i.data!==void 0&&(t.data=i.data),t}var Wi=(s=>(s.NETWORK="network",s.STORAGE="storage",s.CODE_LOADING="code-loading",s.COMMUNICATION="communication",s.TIMING="timing",s.WORKERS="workers",s))(Wi||{}),Xt=class{language="JavaScript";timeout;permissions;allowUnsafeNodeHostAccess;nodeWorkerPoolSize;debugNodeWorkerPool;outputMode;captureConsole;constructor(e){this.timeout=e?.timeout??3e4,this.permissions=e?.permissions??[],this.allowUnsafeNodeHostAccess=e?.allowUnsafeNodeHostAccess??!1,this.outputMode=e?.outputMode??"stdout",this.captureConsole=e?.captureConsole??this.outputMode==="stdout",this.nodeWorkerPoolSize=td(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=nd(e)}getUsageInstructions(){return["Don't wrap async code in (async()=>{ ... })() \u2014 the runtime automatically handles async execution.","State is session-scoped: all top-level declarations (`var`, `let`, `const`) persist across calls.","Bare assignment (e.g. `x = 1`) also persists via `globalThis`.",...this.outputMode==="stdout"?["Use `console.log(...)` output is captured as the execution result so use it to inspect intermediate values between steps instead of `return`."]:["Use `return` or a trailing expression to produce the execution result."]].map(t=>`- ${t}`).join(`
759
- `)}createSession(e){let t=Eu(),n=gs()?id(t,this.nodeWorkerPoolSize):null;n&&this.debugNodeWorkerPool&&console.debug(`[AxJSRuntime] Node worker pool size: ${this.nodeWorkerPoolSize}`),n?.warm();let r=null,o=null,s=null,a=!1,l=this.timeout,u=0,{serializableGlobals:c,fnMap:p}=$u(e,{nextFnId:()=>++u});Lu(c);let d=new Map,m=0,g=A=>{let T=A.data;if(!T||typeof T!="object"||!("type"in T)||T.type===void 0)return;let k=T;if(k.type==="result"){if(typeof k.id!="number")return;let E=d.get(k.id);E&&(d.delete(k.id),k.error!==void 0?E.reject(Gu(k.error)):E.resolve(k.value));return}if(k.type==="fn-call"){if(typeof k.id!="number"||typeof k.name!="string")return;let E=p.get(k.name);if(!E){r?.postMessage({type:"fn-result",id:k.id,error:`Function "${k.name}" not found`});return}Promise.resolve().then(()=>E(...k.args??[])).then(M=>{r?.postMessage({type:"fn-result",id:k.id,value:M})}).catch(M=>{r?.postMessage({type:"fn-result",id:k.id,error:Du(M)})})}},h=()=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null),s=null},f=()=>{a=!0,h();for(let A of d.values())A.reject(new Error("Worker terminated"));d.clear()},C=A=>{h();for(let T of d.values())T.reject(A);d.clear()},x=A=>{A.postMessage({type:"init",globals:c,fnNames:[...p.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})};if(Pu()){r=_u(t,this.permissions),o="browser",r.onmessage=g,r.onerror=C;try{x(r)}catch(A){throw f(),A}}let I=async()=>{if(!r){if(a)throw new Error("Session is closed");if(Pu()){r=_u(t,this.permissions),o="browser",r.onmessage=g,r.onerror=C;try{x(r)}catch(A){throw f(),A}return}if(!gs())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");s||(s=(n?n.acquire():Hi(t)).then(A=>{if(a)throw n?n.release(A):A.terminate(),new Error("Session is closed");r=A,o="node",r.onmessage=g,r.onerror=C;try{x(r)}catch(T){throw n?n.release(A):A.terminate(),r=null,o=null,T}})),await s}},w=(A,T)=>{if(a)return Promise.reject(new Error("Session is closed"));let k=T.signal;if(k?.aborted)return Promise.reject(new Error(`Aborted: ${k.reason??"execution aborted"}`));let E=++m;return new Promise((M,$)=>{let B=setTimeout(()=>{d.delete(E),h();for(let F of d.values())F.reject(new Error("Worker terminated"));d.clear(),$(new Error(T.timeoutMessage))},l),V=M,O=$,j=()=>{};if(d.set(E,{resolve:F=>{clearTimeout(B),j(),V(F)},reject:F=>{clearTimeout(B),j(),O(F)}}),k){let F=()=>{clearTimeout(B),d.delete(E),f(),O(new Error(`Aborted: ${k.reason??"execution aborted"}`))};k.addEventListener("abort",F,{once:!0}),j=()=>{k.removeEventListener("abort",F)}}I().then(()=>{if(!r)throw new Error("Worker unavailable");r.postMessage({...A,id:E})}).catch(F=>{d.get(E)&&(d.delete(E),clearTimeout(B),j(),O(F))})})};return{execute(A,T){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(A))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let k=T?.reservedNames;if(k){for(let E of k)if(new RegExp(`(?:^|[;\\n])\\s*(?:(?:var|let|const)\\s+)?${E}\\s*=`).test(A))return Promise.resolve(`[ERROR] Cannot assign to or redeclare reserved runtime variable '${E}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${E}\`.`)}return w({type:"execute",code:A},{signal:T?.signal,timeoutMessage:"Execution timed out"})},async patchGlobals(A,T){if(!A||typeof A!="object"||Array.isArray(A))throw new Error("patchGlobals expects an object");let{serializableGlobals:k,fnMap:E}=$u(A,{nextFnId:()=>++u});if(Lu(k),Object.keys(k).length!==0){await w({type:"update-globals",globals:k},{signal:T?.signal,timeoutMessage:"Global patch timed out"});for(let[M,$]of Object.entries(k))c[M]=$;for(let[M,$]of E.entries())p.set(M,$)}},close(){f()}}}toFunction(){return{name:"javascriptInterpreter",description:"Execute JavaScript code in a persistent session and return output.",parameters:{type:"object",properties:{code:{type:"string",description:"JavaScript code to execute."}},required:["code"]},func:async({code:e},t)=>{let n=this.createSession();try{return await n.execute(e,{signal:t?.abortSignal})}finally{n.close()}}}}};function Uu(i){return new Xt(i)}var hs=class{constructor(e,t={}){this.transport=e;this.options=t;this.logger=t.logger??(n=>{console.log(typeof n=="string"?n:JSON.stringify(n,null,2))})}functions=[];promptFunctions=[];resourceFunctions=[];activeRequests=new Map;capabilities={};logger;async init(){"connect"in this.transport&&await this.transport.connect?.();let{result:e}=await this.sendRequest("initialize",{protocolVersion:"2024-11-05",capabilities:{roots:{listChanged:!0},sampling:{}},clientInfo:{name:"AxMCPClient",version:"1.0.0"}}),t="2024-11-05";if(e.protocolVersion!==t)throw new Error(`Protocol version mismatch. Expected ${t} but got ${e.protocolVersion}`);e.capabilities.tools&&(this.capabilities.tools=!0),e.capabilities.resources&&(this.capabilities.resources=!0),e.capabilities.prompts&&(this.capabilities.prompts=!0),await this.sendNotification("notifications/initialized"),this.capabilities.tools&&await this.discoverFunctions(),this.capabilities.prompts&&await this.discoverPromptFunctions(),this.capabilities.resources&&await this.discoverResourceFunctions()}async discoverFunctions(){let{result:e}=await this.sendRequest("tools/list");this.functions=e.tools.map(t=>{let n=this.options.functionOverrides?.find(o=>o.name===t.name),r=t.inputSchema.properties?{properties:t.inputSchema.properties,required:t.inputSchema.required??[],type:t.inputSchema.type}:void 0;return{name:n?.updates.name??t.name,description:n?.updates.description??t.description,parameters:r,func:async o=>{let{result:s}=await this.sendRequest("tools/call",{name:t.name,arguments:o});return s}}})}async discoverPromptFunctions(){let e;do{let t=await this.listPrompts(e);for(let n of t.prompts??[])this.promptFunctions.push(this.promptToFunction(n));e=t.nextCursor}while(e)}async discoverResourceFunctions(){let e;do{let t=await this.listResources(e);for(let n of t.resources??[])this.resourceFunctions.push(this.resourceToFunction(n));e=t.nextCursor}while(e);e=void 0;do{let t=await this.listResourceTemplates(e);for(let n of t.resourceTemplates??[])this.resourceFunctions.push(this.resourceTemplateToFunction(n));e=t.nextCursor}while(e)}promptToFunction(e){let t=`prompt_${e.name}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=e.arguments?.length?{type:"object",properties:Object.fromEntries(e.arguments.map(o=>[o.name,{type:"string",description:o.description??""}])),required:e.arguments.filter(o=>o.required).map(o=>o.name)}:void 0;return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Get the ${e.name} prompt`,parameters:r,func:async o=>{let s=await this.getPrompt(e.name,o);return this.formatPromptMessages(s.messages)}}}resourceToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(r=>r.name===t);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:void 0,func:async()=>{let r=await this.readResource(e.uri);return this.formatResourceContents(r.contents)}}}resourceTemplateToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=this.parseUriTemplate(e.uriTemplate);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:r.length?{type:"object",properties:Object.fromEntries(r.map(o=>[o,{type:"string",description:`Value for ${o}`}])),required:r}:void 0,func:async o=>{let s=this.expandUriTemplate(e.uriTemplate,o??{}),a=await this.readResource(s);return this.formatResourceContents(a.contents)}}}formatPromptMessages(e){return e.map(t=>{let n=t.role==="user"?"User":"Assistant",r=this.extractContent(t.content);return`${n}: ${r}`}).join(`
783
+ `),t?`${t}${e}`:e}var Wl=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",ps=()=>typeof process<"u"&&!!process.versions?.node,_d=()=>!!globalThis.Deno?.version?.deno,Nd=()=>globalThis.Deno?.version?.deno??null,Ld=s=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(s.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},Vl=s=>Number.isFinite(s)?Math.max(1,Math.min(Hl,Math.floor(s))):qi,$d=()=>{if(!ps())return null;let e=globalThis.process?.getBuiltinModule;if(typeof e!="function")return null;let n=e("node:os")?.availableParallelism;if(typeof n!="function")return null;let r=n();return Number.isFinite(r)&&r>0?r:null},Dd=s=>{if(s!==void 0)return Vl(s);let e=$d();return e?Vl(Math.ceil(e/2)):qi},Gd=s=>s?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",Ud=s=>{let e=new Set(s),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},Bd=(s,e)=>{let t=Nd(),n=t?Ld(t):null;if(n?n.major>=1:!0)try{return new Worker(s,{type:"module",deno:{permissions:Ud(e)}})}catch{}return new Worker(s,{type:"module"})},Jl=(s,e)=>{let t=new Blob([s],{type:"application/javascript"}),n=URL.createObjectURL(t),r=_d()?Bd(n,e):new Worker(n),o=!1,i=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:u=>r.postMessage(u),terminate:()=>{r.terminate(),i()},onmessage:null,onerror:null};return r.onmessage=u=>{a.onmessage?.({data:u.data})},r.onerror=u=>{a.onerror?.(new Error(u.message||"Worker error"))},a},Qi=async s=>{let e="node:worker_threads",{Worker:t}=await import(e),n=new t(s,{eval:!0}),r=null,o=null,i=!1;n.on("error",u=>{o?o(u):r=u}),n.on("exit",u=>{if(i=!0,u!==0&&!r){let l=new Error(`Worker exited with code ${u}`);o?o(l):r=l}}),n.on("message",u=>{a.onmessage?.({data:u})});let a={postMessage:u=>n.postMessage(u),terminate:()=>{n.terminate()},onmessage:null,get onerror(){return o},set onerror(u){if(o=u,u&&r){let l=r;r=null,u(l)}},get exited(){return i}};return a},Yi=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(ps())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,Qi(this.source).then(e=>{e.onmessage=null,e.onerror=null,this.idle.push(e)}).catch(()=>{}).finally(()=>{this.pendingCreates-=1})}async acquire(){for(;this.idle.length>0;){let e=this.idle.pop();if(!e.exited)return this.warm(),e}return this.warm(),Qi(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},Ql=new Map,jd=(s,e)=>`${e}:${s}`,zd=(s,e)=>{let t=jd(s,e),n=Ql.get(t);if(n)return n;let r=new Yi(s,e);return Ql.set(t,r),r},Yl=(s,e)=>{let t={},n=new Map,r=0,o=new WeakMap,i=(a,u)=>{if(typeof a=="function"){let g=`fn_${e?.nextFnId?e.nextFnId():++r}_${u||"root"}`;return n.set(g,a),{[Hi]:g}}if(!a||typeof a!="object")return a;if(o.has(a))return o.get(a);if(Array.isArray(a)){let m=[];o.set(a,m);for(let g=0;g<a.length;g+=1)m[g]=i(a[g],`${u}[${g}]`);return m}let l=Object.getPrototypeOf(a);if(!(l===Object.prototype||l===null))return a;let p=a,d={};o.set(a,d);for(let[m,g]of Object.entries(p))d[m]=i(g,u?`${u}.${m}`:m);return d};if(s)for(let[a,u]of Object.entries(s))t[a]=i(u,a);return{serializableGlobals:t,fnMap:n}},Xl=s=>{if(typeof structuredClone=="function")try{structuredClone(s)}catch(e){let t=e instanceof Error?e.message:String(e);throw new Error(`RLM globals must be structured-cloneable: ${t}`)}},Wi=s=>!!s&&/[A-Za-z0-9_$]/.test(s),Vi=s=>!!s&&/[A-Za-z_$]/.test(s),qd=(s,e)=>{let t=new Set(e);for(let m of ls(s))if(t.has(m))return m;let n=Nr(s),r=n.length,o=0,i=0,a=0,u=m=>{let g=m;for(;g<r&&/\s/.test(n[g]??"");)g++;return g},l=m=>{let g=m;for(;g>=0&&/\s/.test(n[g]??"");)g--;return g},c=m=>{let g=l(m-1);if(g<0)return!0;let f=n[g];return f===`
784
+ `||f===";"||f==="{"||f==="}"},p=()=>{let m=o;for(;o<r&&Wi(n[o]);)o++;return n.slice(m,o)},d=m=>{let g=n.slice(m,m+2);return n.slice(m,m+3)==="==="||g==="=="||g==="=>"?!1:n[m]==="="||["+=","-=","*=","/=","%=","&=","|=","^=","&&=","||=","??=","**=","<<=",">>=",">>>="].some(x=>n.startsWith(x,m))};for(;o<r;){let m=n[o];if(m==="{"){i++,o++;continue}if(m==="}"){i--,o++;continue}if(m==="("){a++,o++;continue}if(m===")"){a--,o++;continue}if(i===0&&a===0&&Vi(m)){let g=o,f=p();if((f==="function"||f==="class")&&c(g)){let x=u(o);if(Vi(n[x])){let y=x+1;for(;y<r&&Wi(n[y]);)y++;let h=n.slice(x,y);if(t.has(h))return h}continue}if(f==="async"&&c(g)){let x=u(o);if(n.startsWith("function",x)){let y=u(x+8);if(Vi(n[y])){let h=y+1;for(;h<r&&Wi(n[h]);)h++;let A=n.slice(y,h);if(t.has(A))return A}}continue}if(t.has(f)){let x=l(g-1),y=x>=0?n[x]:void 0,h=u(o),A=y==="."||y==="?"||y==="["&&n[h]==="]",b=n.startsWith("++",h)||n.startsWith("--",h)||x>0&&(n.slice(x-1,x+1)==="++"||n.slice(x-1,x+1)==="--");if(!A&&(b||d(h)))return f}continue}o++}};function Ji(s){if(s==null||typeof s!="object")return String(s);try{return JSON.stringify(s,null,2)}catch{return String(s)}}function Zl(s,e=Ki,t=0,n=new Set){if(t>e)return{name:"Error",message:"[cause chain truncated]"};if(s!==null&&typeof s=="object"){if(n.has(s))return{name:"Error",message:"[circular]"};n.add(s)}let r=s!==null&&typeof s=="object"&&s.name!=null?String(s.name):"Error",o=s!==null&&typeof s=="object"&&s.message!=null?String(s.message):Ji(s),i=s!==null&&typeof s=="object"&&typeof s.stack=="string"?s.stack:void 0,a,u=s;if(u&&typeof u=="object"&&u.cause!==void 0&&t<e)try{let p=u.cause;p instanceof Error||p!==null&&typeof p=="object"&&("message"in p||"name"in p)?a=Zl(p,e,t+1,n):a={name:"Error",message:Ji(p)}}catch{a={name:"Error",message:Ji(u.cause)}}let l={name:r,message:o};i!==void 0&&(l.stack=i),a!==void 0&&(l.cause=a);let c=s;if(c&&typeof c=="object"&&"data"in c&&c.data!==void 0)try{typeof structuredClone=="function"?l.data=structuredClone(c.data):l.data=c.data}catch{}return l}function ec(s){if(typeof s=="string")return new Error(s);if(!s||typeof s!="object")return new Error(String(s));let e=s.message!=null?String(s.message):"Unknown error",t=new Error(e);return t.name=s.name!=null?String(s.name):"Error",typeof s.stack=="string"&&(t.stack=s.stack),s.cause!==void 0&&(t.cause=ec(s.cause)),s.data!==void 0&&(t.data=s.data),t}var Xi=(i=>(i.NETWORK="network",i.STORAGE="storage",i.CODE_LOADING="code-loading",i.COMMUNICATION="communication",i.TIMING="timing",i.WORKERS="workers",i))(Xi||{}),Ut=class{language="JavaScript";timeout;permissions;allowUnsafeNodeHostAccess;nodeWorkerPoolSize;debugNodeWorkerPool;outputMode;captureConsole;constructor(e){this.timeout=e?.timeout??3e4,this.permissions=e?.permissions??[],this.allowUnsafeNodeHostAccess=e?.allowUnsafeNodeHostAccess??!1,this.outputMode=e?.outputMode??"stdout",this.captureConsole=e?.captureConsole??this.outputMode==="stdout",this.nodeWorkerPoolSize=Dd(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=Gd(e)}getUsageInstructions(){return["Don't wrap async code in (async()=>{ ... })() \u2014 the runtime automatically handles async execution.","State is session-scoped: all top-level declarations (`var`, `let`, `const`) persist across calls.","Bare assignment (e.g. `x = 1`) also persists via `globalThis`.",...this.outputMode==="stdout"?["Use `console.log(...)` output is captured as the execution result so use it to inspect intermediate values between steps instead of `return`."]:["Use `return` or a trailing expression to produce the execution result."]].map(t=>`- ${t}`).join(`
785
+ `)}createSession(e){let t=Kl(),n=ps()?zd(t,this.nodeWorkerPoolSize):null;n&&this.debugNodeWorkerPool&&console.debug(`[AxJSRuntime] Node worker pool size: ${this.nodeWorkerPoolSize}`),n?.warm();let r=null,o=null,i=null,a=!1,u=this.timeout,l=0,{serializableGlobals:c,fnMap:p}=Yl(e,{nextFnId:()=>++l});Xl(c);let d=new Map,m=0,g=[],f=null,x=k=>{let v=k.data;if(!v||typeof v!="object"||!("type"in v)||v.type===void 0)return;let O=v;if(O.type==="result"){if(typeof O.id!="number")return;let P=d.get(O.id);P&&(d.delete(O.id),O.error!==void 0?P.reject(ec(O.error)):P.resolve(O.value));return}if(O.type==="fn-call"){if(typeof O.id!="number"||typeof O.name!="string")return;let P=p.get(O.name);if(!P){r?.postMessage({type:"fn-result",id:O.id,error:`Function "${O.name}" not found`});return}Promise.resolve().then(()=>P(...O.args??[])).then($=>{r?.postMessage({type:"fn-result",id:O.id,value:$})}).catch($=>{r?.postMessage({type:"fn-result",id:O.id,error:Zl($)})})}},y=()=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null),i=null},h=()=>{a=!0,y();for(let k of g)!k.started&&!k.settled&&(k.settled=!0,k.signal&&k.onAbort&&k.signal.removeEventListener("abort",k.onAbort),k.reject(new Error("Worker terminated")));g.length=0;for(let k of d.values())k.reject(new Error("Worker terminated"));d.clear()},A=k=>{y();for(let v of d.values())v.reject(k);d.clear()},b=k=>{k.postMessage({type:"init",globals:c,fnNames:[...p.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})};if(Wl()){r=Jl(t,this.permissions),o="browser",r.onmessage=x,r.onerror=A;try{b(r)}catch(k){throw h(),k}}let I=async()=>{if(!r){if(a)throw new Error("Session is closed");if(Wl()){r=Jl(t,this.permissions),o="browser",r.onmessage=x,r.onerror=A;try{b(r)}catch(k){throw h(),k}return}if(!ps())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");i||(i=(n?n.acquire():Qi(t)).then(k=>{if(a)throw n?n.release(k):k.terminate(),new Error("Session is closed");r=k,o="node",r.onmessage=x,r.onerror=A;try{b(r)}catch(v){throw n?n.release(k):k.terminate(),r=null,o=null,v}})),await i}},T=(k,v)=>{if(a)return Promise.reject(new Error("Session is closed"));let O=v.signal;if(O?.aborted)return Promise.reject(new Error(`Aborted: ${O.reason??"execution aborted"}`));let P=++m;return new Promise(($,E)=>{let F=$,_=E,j,H=()=>{};if(d.set(P,{resolve:L=>{j&&clearTimeout(j),H(),F(L)},reject:L=>{j&&clearTimeout(j),H(),_(L)}}),O){let L=()=>{clearTimeout(j),d.delete(P),h(),_(new Error(`Aborted: ${O.reason??"execution aborted"}`))};O.addEventListener("abort",L,{once:!0}),H=()=>{O.removeEventListener("abort",L)}}I().then(()=>{if(!r)throw new Error("Worker unavailable");j=setTimeout(()=>{d.delete(P),y();for(let L of d.values())L.reject(new Error("Worker terminated"));d.clear(),E(new Error(v.timeoutMessage))},u),r.postMessage({...k,id:P})}).catch(L=>{d.get(P)&&(d.delete(P),clearTimeout(j),H(),_(L))})})},R=(k,v)=>a?Promise.reject(new Error("Session is closed")):k?.aborted?Promise.reject(new Error(`Aborted: ${k.reason??"execution aborted"}`)):new Promise((O,P)=>{let $={started:!1,settled:!1,signal:k,resolve:O,reject:P,operation:v};if(k){let F=()=>{if($.settled)return;$.settled=!0;let _=g.indexOf($);_!==-1&&g.splice(_,1),k.removeEventListener("abort",F),P(new Error(`Aborted: ${k.reason??"execution aborted"}`))};$.onAbort=F,k.addEventListener("abort",F,{once:!0})}g.push($);let E=()=>{if(f)return;let F=g.find(j=>!j.started&&!j.settled);if(!F)return;let _=()=>{f=null,E()};f=(async()=>{if(!F.settled){if(a){F.settled=!0,F.signal&&F.onAbort&&F.signal.removeEventListener("abort",F.onAbort),F.reject(new Error("Worker terminated"));return}if(F.signal?.aborted){F.settled=!0,F.onAbort&&F.signal.removeEventListener("abort",F.onAbort),F.reject(new Error(`Aborted: ${F.signal.reason??"execution aborted"}`));return}F.started=!0;try{let j=await F.operation();if(F.settled)return;F.settled=!0,F.signal&&F.onAbort&&F.signal.removeEventListener("abort",F.onAbort),F.resolve(j)}catch(j){if(F.settled)return;F.settled=!0,F.signal&&F.onAbort&&F.signal.removeEventListener("abort",F.onAbort),F.reject(j)}finally{let j=g.indexOf(F);j!==-1&&g.splice(j,1),_()}}})().catch(()=>{_()})};E()});return{execute(k,v){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(k))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let O=v?.reservedNames;if(O){let P=qd(k,O);if(P)return Promise.resolve(`[ERROR] Cannot assign to, redeclare, or shadow reserved runtime variable '${P}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${P}\`.`)}return R(v?.signal,()=>T({type:"execute",code:k},{signal:v?.signal,timeoutMessage:"Execution timed out"}))},async patchGlobals(k,v){if(!k||typeof k!="object"||Array.isArray(k))throw new Error("patchGlobals expects an object");let{serializableGlobals:O,fnMap:P}=Yl(k,{nextFnId:()=>++l});if(Xl(O),Object.keys(O).length!==0){await R(v?.signal,()=>T({type:"update-globals",globals:O},{signal:v?.signal,timeoutMessage:"Global patch timed out"}));for(let[$,E]of Object.entries(O))c[$]=E;for(let[$,E]of P.entries())p.set($,E)}},close(){h()}}}toFunction(){return{name:"javascriptInterpreter",description:"Execute JavaScript code in a persistent session and return output.",parameters:{type:"object",properties:{code:{type:"string",description:"JavaScript code to execute."}},required:["code"]},func:async({code:e},t)=>{let n=this.createSession();try{return await n.execute(e,{signal:t?.abortSignal})}finally{n.close()}}}}};function tc(s){return new Ut(s)}var ds=class{constructor(e,t={}){this.transport=e;this.options=t;this.logger=t.logger??(n=>{console.log(typeof n=="string"?n:JSON.stringify(n,null,2))})}functions=[];promptFunctions=[];resourceFunctions=[];activeRequests=new Map;capabilities={};logger;async init(){"connect"in this.transport&&await this.transport.connect?.();let{result:e}=await this.sendRequest("initialize",{protocolVersion:"2024-11-05",capabilities:{roots:{listChanged:!0},sampling:{}},clientInfo:{name:"AxMCPClient",version:"1.0.0"}}),t="2024-11-05";if(e.protocolVersion!==t)throw new Error(`Protocol version mismatch. Expected ${t} but got ${e.protocolVersion}`);e.capabilities.tools&&(this.capabilities.tools=!0),e.capabilities.resources&&(this.capabilities.resources=!0),e.capabilities.prompts&&(this.capabilities.prompts=!0),await this.sendNotification("notifications/initialized"),this.capabilities.tools&&await this.discoverFunctions(),this.capabilities.prompts&&await this.discoverPromptFunctions(),this.capabilities.resources&&await this.discoverResourceFunctions()}async discoverFunctions(){let{result:e}=await this.sendRequest("tools/list");this.functions=e.tools.map(t=>{let n=this.options.functionOverrides?.find(o=>o.name===t.name),r=t.inputSchema.properties?{properties:t.inputSchema.properties,required:t.inputSchema.required??[],type:t.inputSchema.type}:void 0;return{name:n?.updates.name??t.name,description:n?.updates.description??t.description,parameters:r,func:async o=>{let{result:i}=await this.sendRequest("tools/call",{name:t.name,arguments:o});return i}}})}async discoverPromptFunctions(){let e;do{let t=await this.listPrompts(e);for(let n of t.prompts??[])this.promptFunctions.push(this.promptToFunction(n));e=t.nextCursor}while(e)}async discoverResourceFunctions(){let e;do{let t=await this.listResources(e);for(let n of t.resources??[])this.resourceFunctions.push(this.resourceToFunction(n));e=t.nextCursor}while(e);e=void 0;do{let t=await this.listResourceTemplates(e);for(let n of t.resourceTemplates??[])this.resourceFunctions.push(this.resourceTemplateToFunction(n));e=t.nextCursor}while(e)}promptToFunction(e){let t=`prompt_${e.name}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=e.arguments?.length?{type:"object",properties:Object.fromEntries(e.arguments.map(o=>[o.name,{type:"string",description:o.description??""}])),required:e.arguments.filter(o=>o.required).map(o=>o.name)}:void 0;return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Get the ${e.name} prompt`,parameters:r,func:async o=>{let i=await this.getPrompt(e.name,o);return this.formatPromptMessages(i.messages)}}}resourceToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(r=>r.name===t);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:void 0,func:async()=>{let r=await this.readResource(e.uri);return this.formatResourceContents(r.contents)}}}resourceTemplateToFunction(e){let t=`resource_${this.sanitizeName(e.name)}`,n=this.options.functionOverrides?.find(o=>o.name===t),r=this.parseUriTemplate(e.uriTemplate);return{name:n?.updates.name??t,description:n?.updates.description??e.description??`Read ${e.name}`,parameters:r.length?{type:"object",properties:Object.fromEntries(r.map(o=>[o,{type:"string",description:`Value for ${o}`}])),required:r}:void 0,func:async o=>{let i=this.expandUriTemplate(e.uriTemplate,o??{}),a=await this.readResource(i);return this.formatResourceContents(a.contents)}}}formatPromptMessages(e){return e.map(t=>{let n=t.role==="user"?"User":"Assistant",r=this.extractContent(t.content);return`${n}: ${r}`}).join(`
760
786
 
761
787
  `)}extractContent(e){if(e.type==="text")return e.text;if(e.type==="image")return`[Image: ${e.mimeType}]`;if(e.type==="resource"){let t=e.resource;return"text"in t?t.text:`[Binary: ${t.uri}]`}return""}formatResourceContents(e){return e.map(t=>"text"in t?t.text:`[Binary: ${t.uri}]`).join(`
762
- `)}sanitizeName(e){return e.replace(/[^a-zA-Z0-9_]/g,"_")}parseUriTemplate(e){return(e.match(/\{([^}]+)\}/g)??[]).map(n=>n.slice(1,-1))}expandUriTemplate(e,t){return e.replace(/\{([^}]+)\}/g,(n,r)=>t[r]??"")}async ping(e=3e3){let t=this.sendRequest("ping"),n=new Promise((s,a)=>setTimeout(()=>a(new Error("Ping response timeout exceeded")),e)),r=await Promise.race([t,n]),{result:o}=r;if(typeof o!="object"||o===null||Object.keys(o).length!==0)throw new Error(`Unexpected ping response: ${JSON.stringify(o)}`)}toFunction(){return[...this.functions,...this.promptFunctions,...this.resourceFunctions]}getCapabilities(){return{tools:this.capabilities.tools??!1,resources:this.capabilities.resources??!1,prompts:this.capabilities.prompts??!1}}hasToolsCapability(){return this.capabilities.tools??!1}hasPromptsCapability(){return this.capabilities.prompts??!1}hasResourcesCapability(){return this.capabilities.resources??!1}async listPrompts(e){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("prompts/list",t);return n}async getPrompt(e,t){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let{result:n}=await this.sendRequest("prompts/get",{name:e,arguments:t});return n}async listResources(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/list",t);return n}async listResourceTemplates(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/templates/list",t);return n}async readResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let{result:t}=await this.sendRequest("resources/read",{uri:e});return t}async subscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/subscribe",{uri:e})}async unsubscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/unsubscribe",{uri:e})}cancelRequest(e){if(this.activeRequests.has(e)){this.sendNotification("notifications/cancelled",{requestId:e,reason:"Client cancelled request"});let t=this.activeRequests.get(e);t&&t.reject(new Error(`Request ${e} cancelled`)),this.activeRequests.delete(e)}}async sendRequest(e,t={}){let n=Ve(),r={jsonrpc:"2.0",id:n,method:e,params:t},o=new Promise((a,l)=>{this.activeRequests.set(n,{reject:l}),this.transport.send(r).then(u=>{if(this.activeRequests.delete(n),u!==null&&typeof u=="object"&&"error"in u){let c=u;l(new Error(`RPC Error ${c.error.code}: ${c.error.message}`))}else u!==null&&typeof u=="object"&&"result"in u?a({result:u.result}):l(new Error("Invalid response no result or error"))}).catch(u=>{this.activeRequests.delete(n),l(u)})}),{result:s}=await o;return{id:n,result:s}}async sendNotification(e,t={}){let n={jsonrpc:"2.0",method:e,params:t},{debug:r}=this.options;if(r){let o={name:"Notification",id:"mcp_notification",value:`Sending notification: ${JSON.stringify(n,null,2)}`};this.logger(o)}await this.transport.sendNotification(n)}};function Bu(i){if(typeof Buffer<"u")return Buffer.from(i).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"");let e="";for(let n=0;n<i.length;n++)e+=String.fromCharCode(i[n]);return(typeof btoa=="function"?btoa(e):"").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}async function ju(i){let t=new TextEncoder().encode(i),n=await oa().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function Vi(){return Bu(await ju(Ve()+Math.random().toString(36)))}async function qu(i){return Bu(await ju(i))}async function fs(i,e){let t=await fetch(i,{headers:e});if(!t.ok)throw new Error(`HTTP ${t.status} fetching ${i}: ${t.statusText}`);return await t.json()}function zu(i){let e=new URLSearchParams;for(let[t,n]of Object.entries(i))n!==void 0&&e.set(t,n);return e.toString()}function Ur(i){return i.endsWith("/")?i.slice(0,-1):i}function ad(i){if(!i)return null;let e=i.match(/resource_metadata\s*=\s*"([^"]+)"/i)||i.match(/resource_metadata\s*=\s*([^,\s]+)/i);return e?e[1]:null}async function Hu(i,e){let t=ad(e);if(t){let a=await fs(t),l=Ur(new URL(i).toString().split("?")[0]),u=Ur(a.resource??"");if(!u||u!==l)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${l} but got ${u}`);let c=Array.isArray(a.authorization_servers)?a.authorization_servers:[];if(c.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:l,issuers:c}}let n=new URL(i),r=n.pathname.replace(/\/+$/,""),o=[];r&&r!=="/"&&o.push({url:`${n.origin}/.well-known/oauth-protected-resource${r}`,expected:`${n.origin}${r}`}),o.push({url:`${n.origin}/.well-known/oauth-protected-resource`,expected:`${n.origin}`});let s;for(let a of o)try{let l=await fs(a.url),u=Ur(l.resource??""),c=Ur(a.expected);if(!u||u!==c)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${c} but got ${u}`);let p=Array.isArray(l.authorization_servers)?l.authorization_servers:[];if(p.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:c,issuers:p}}catch(l){s=l}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(s)}`)}async function Ku(i){let e=new URL(i),t=e.pathname.replace(/^\/+/,""),n=[];t?(n.push(`${e.origin}/.well-known/oauth-authorization-server/${t}`),n.push(`${e.origin}/.well-known/openid-configuration/${t}`),n.push(`${e.origin}/${t.replace(/\/+$/,"")}/.well-known/openid-configuration`)):(n.push(`${e.origin}/.well-known/oauth-authorization-server`),n.push(`${e.origin}/.well-known/openid-configuration`));let r;for(let o of n)try{let s=await fs(o);if(!s.authorization_endpoint||!s.token_endpoint)throw new Error("AS metadata missing endpoints");let a=s.code_challenge_methods_supported;if(!a||!a.includes("S256"))throw new Error("Authorization server does not advertise PKCE S256 support");return s}catch(s){r=s}throw new Error(`Failed to discover AS metadata for ${i}: ${String(r)}`)}var Zn=class{constructor(e){this.oauth=e}tokenCache=new Map;asMetaCache=new Map;key(e,t){return`${e}::${t}`}async getStoredToken(e,t){let n=this.key(e,t);if(this.tokenCache.has(n))return this.tokenCache.get(n);let r=await this.oauth?.tokenStore?.getToken?.(n);return r&&this.tokenCache.set(n,r),r??null}async setStoredToken(e,t,n){let r=this.key(e,t);this.tokenCache.set(r,n),await this.oauth?.tokenStore?.setToken?.(r,n)}async clearStoredToken(e,t){let n=this.key(e,t);this.tokenCache.delete(n),await this.oauth?.tokenStore?.clearToken?.(n)}isExpired(e){return e?Date.now()>e-6e4:!1}async getASMeta(e){if(this.asMetaCache.has(e))return this.asMetaCache.get(e);let t=await Ku(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await Hu(e.requestedUrl,e.wwwAuthenticate),r=this.oauth.selectAuthorizationServer?await this.oauth.selectAuthorizationServer(n,{}):n[0],o=await this.getASMeta(r),s=e.currentToken??await this.getStoredToken(t,r);if(s?.accessToken&&!this.isExpired(s.expiresAt))return{token:s,issuer:r,asMeta:o,resource:t};if(s?.refreshToken)try{let x=await this.refreshToken(s.refreshToken,t,r,o);return await this.setStoredToken(t,r,x),{token:x,issuer:r,asMeta:o,resource:t}}catch{await this.clearStoredToken(t,r)}let a=this.oauth.redirectUri??"http://localhost:8787/callback",l=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),u=await Vi(),c=await qu(u),p=await Vi(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${zu({response_type:"code",client_id:l.client_id,redirect_uri:a,scope:d,state:p,code_challenge:c,code_challenge_method:"S256",resource:t})}`;if(!this.oauth.onAuthCode)throw new Error(`Authorization required. Provide oauth.onAuthCode to complete the flow. Navigate to: ${m}`);let{code:g,redirectUri:h}=await this.oauth.onAuthCode(m),f=h??a,C=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:u,client:l,redirectUri:f,resource:t});return await this.setStoredToken(t,r,C),{token:C,issuer:r,asMeta:o,resource:t}}async dynamicClientRegistration(e,t){if(!e.registration_endpoint)throw new Error("Authorization server does not support dynamic client registration and no clientId was provided.");let r={application_type:t.startsWith("http://localhost")?"native":"web",client_name:"Ax MCP Client",redirect_uris:[t],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"},o=await fetch(e.registration_endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!o.ok)throw new Error(`Dynamic client registration failed: ${o.status} ${o.statusText}`);let s=await o.json();if(!s.client_id)throw new Error("Dynamic client registration did not return client_id");return s}async exchangeCodeForToken(e){let t=new URLSearchParams;t.set("grant_type","authorization_code"),t.set("code",e.code),t.set("redirect_uri",e.redirectUri),t.set("client_id",e.client.client_id),t.set("code_verifier",e.codeVerifier),t.set("resource",e.resource),e.client.client_secret&&t.set("client_secret",e.client.client_secret);let n=await fetch(e.asMeta.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!n.ok)throw new Error(`Token exchange failed: ${n.status} ${n.statusText}`);let r=await n.json();if(!r.access_token)throw new Error("No access_token in token response");let o=r.expires_in?Date.now()+r.expires_in*1e3:void 0;return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:o}}async refreshToken(e,t,n,r){let o=new URLSearchParams;o.set("grant_type","refresh_token"),o.set("refresh_token",e),o.set("resource",t),this.oauth?.clientId&&o.set("client_id",this.oauth.clientId),this.oauth?.clientSecret&&o.set("client_secret",this.oauth.clientSecret);let s=await fetch(r.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:o.toString()});if(!s.ok)throw new Error(`Token refresh failed: ${s.status} ${s.statusText}`);let a=await s.json();if(!a.access_token)throw new Error("No access_token in refresh response");let l=a.expires_in?Date.now()+a.expires_in*1e3:void 0;return{accessToken:a.access_token,refreshToken:a.refresh_token??e,expiresAt:l}}};var xs=class{mcpEndpoint;sessionId;eventSource;pendingRequests=new Map;messageHandler;customHeaders;oauthHelper;currentToken;currentIssuer;constructor(e,t){this.mcpEndpoint=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new Zn(t?.oauth)}setHeaders(e){this.customHeaders={...e}}setAuthorization(e){this.customHeaders.Authorization=e}getHeaders(){return{...this.customHeaders}}buildHeaders(e){let t={...this.customHeaders,...e};return this.sessionId&&(t["Mcp-Session-Id"]=this.sessionId),t}setMessageHandler(e){this.messageHandler=e}async connect(){return Promise.resolve()}async openListeningStream(){return new Promise((e,t)=>{let n=this.buildHeaders({Accept:"text/event-stream"}),r=new URL(this.mcpEndpoint);if(Object.keys(this.customHeaders).length>0){this.openListeningStreamWithFetch(n).then(e).catch(t);return}this.eventSource=new EventSource(r.toString()),this.eventSource.onopen=()=>e(),this.eventSource.onmessage=o=>{try{let s=JSON.parse(o.data);this.messageHandler&&this.messageHandler(s)}catch(s){console.error("Failed to parse SSE message:",s)}},this.eventSource.onerror=()=>t(new Error("Failed to establish SSE connection"))})}async openListeningStreamWithFetch(e){let t=await fetch(this.mcpEndpoint,{method:"GET",headers:e});if(t.status===401){let a=t.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,this.openListeningStreamWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!t.ok)throw new Error(`Failed to open SSE stream: ${t.status} ${t.statusText}`);if(!t.body)throw new Error("No response body available for SSE stream");let n=t.body.getReader(),r=new TextDecoder,o="",s=async()=>{try{let{done:a,value:l}=await n.read();if(a){n.releaseLock();return}o+=r.decode(l,{stream:!0});let u=o.split(`
763
- `);o=u.pop()||"";for(let c of u)if(c.startsWith("data: ")){let p=c.slice(6);if(p==="[DONE]")return;try{let d=JSON.parse(p);this.messageHandler&&this.messageHandler(d)}catch(d){console.error("Failed to parse SSE data:",d)}}await s()}catch(a){throw n.releaseLock(),a}};await s()}async send(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let a=r.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);let o=r.headers.get("Mcp-Session-Id");o&&(this.sessionId=o);let s=r.headers.get("Content-Type");if(s?.includes("text/event-stream"))return this.handleSSEResponse(r,e.id);if(s?.includes("application/json"))return r.json();throw new Error(`Unexpected content type: ${s}`)}async handleSSEResponse(e,t){return new Promise((n,r)=>{let o=e.body?.getReader();if(!o){r(new Error("No response body reader available"));return}let s=new TextDecoder,a="",l=async()=>{try{let{done:u,value:c}=await o.read();if(u){o.releaseLock();return}a+=s.decode(c,{stream:!0});let p=a.split(`
764
- `);a=p.pop()||"";for(let d of p)if(d.startsWith("data: ")){let m=d.slice(6);if(m==="[DONE]")return;try{let g=JSON.parse(m);if("id"in g&&g.id===t){n(g);return}this.messageHandler&&this.messageHandler(g)}catch(g){console.error("Failed to parse SSE data:",g)}}await l()}catch(u){o.releaseLock(),r(u)}};l().catch(r)})}async sendNotification(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}async terminateSession(){if(this.sessionId)try{let e=this.buildHeaders({});(await fetch(this.mcpEndpoint,{method:"DELETE",headers:e})).status===405&&console.info("Server does not support explicit session termination")}catch(e){console.error("Failed to terminate session:",e)}finally{this.sessionId=void 0}}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0)}};var As=class{endpoint=null;sseUrl;eventSource;customHeaders={};oauthHelper;currentToken;currentIssuer;sseAbort;pendingRequests=new Map;messageHandler;endpointReady;constructor(e,t){this.sseUrl=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new Zn(t?.oauth)}buildHeaders(e){return{...this.customHeaders,...e}}async openSSEWithFetch(e){let t=new AbortController;this.sseAbort=t;let n=await fetch(this.sseUrl,{method:"GET",headers:e,signal:t.signal});if(n.status===401){let o=n.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,this.openSSEWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!n.ok)throw new Error("Failed to establish SSE connection");let r=this.createEndpointReady();this.consumeSSEStream(n),await r}createEndpointReady(){if(!this.endpointReady){let e,t=new Promise(n=>{e=n});this.endpointReady={resolve:e,promise:t}}return this.endpointReady.promise}async consumeSSEStream(e){if(!e.body)throw new Error("No response body available for SSE stream");let t=e.body.getReader(),n=new TextDecoder,r="",o=null;for(;;){let{done:s,value:a}=await t.read();if(s)break;r+=n.decode(a,{stream:!0});let l=r.split(`
765
- `);r=l.pop()||"";for(let u of l)if(u.startsWith("event: "))o=u.slice(7).trim();else if(u.startsWith("data: ")){let c=u.slice(6);if(o==="endpoint"){let p=c.trim(),d;try{let m=JSON.parse(p);typeof m=="string"?d=m:m&&typeof m=="object"&&"uri"in m&&(d=m.uri)}catch{d=p}if(!d)throw new Error("Endpoint URI missing in SSE event data");/^https?:\/\//i.test(d)||(d=new URL(this.sseUrl).origin+(d.startsWith("/")?d:`/${d}`)),this.endpoint=d,this.endpointReady&&(this.endpointReady.resolve(),this.endpointReady=void 0)}else{let p=c.trim();try{let d=JSON.parse(p);if(d&&typeof d=="object"&&"id"in d){let m=d.id,g=this.pendingRequests.get(m);g?(g.resolve(d),this.pendingRequests.delete(m)):this.messageHandler&&this.messageHandler(d)}else this.messageHandler&&this.messageHandler(d)}catch{}}}else u.trim()===""&&(o=null)}}async connect(){let e=this.buildHeaders({Accept:"text/event-stream"});await this.openSSEWithFetch(e)}async send(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=new Promise((a,l)=>{this.pendingRequests.set(e.id,{resolve:a,reject:l})}),o=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(o.status===401){let a=o.headers.get("WWW-Authenticate"),l=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:a,currentToken:null});if(!l)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${l.token.accessToken}`,o=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!o.ok)throw this.pendingRequests.delete(e.id),new Error(`HTTP error ${o.status}: ${o.statusText}`);if(o.headers.get("Content-Type")?.includes("application/json")){let a=await o.json();return this.pendingRequests.delete(e.id),a}return r}async sendNotification(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),s=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!s)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${s.token.accessToken}`,r=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!r.ok)throw new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.sseAbort&&(this.sseAbort.abort(),this.sseAbort=void 0)}};function Wu(i){return i.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??i.slice(0,80)}function Ju(i){let e=[],t=/(?:^|[\n;])\s*(?:var|let|const)\s+(\w+)/g,n=t.exec(i);for(;n!==null;)n[1]&&e.push(n[1]),n=t.exec(i);return e}var ld=new Set(["var","let","const","function","return","if","else","for","while","do","switch","case","break","continue","try","catch","finally","throw","new","delete","typeof","void","in","of","instanceof","this","class","extends","super","import","export","default","from","as","async","await","yield","true","false","null","undefined","console","log"]);function Qu(i){let e=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,t=new Set,n=e.exec(i);for(;n!==null;)n[1]&&!ld.has(n[1])&&t.add(n[1]),n=e.exec(i);return t}function Ji(i,e=120){let t=i.replace(/\s+/g," ").trim();return t.length<=e?t:`${t.slice(0,e-3)}...`}function ud(i){return/\b(final|ask_clarification)\s*\(/.test(i)}function cd(i){return i.tags.includes("error")?"error":ud(i.code)?"finalize":/\b(llmQuery|listModuleFunctions|getFunctionDefinitions)\s*\(/.test(i.code)?"query":(i.producedVars?.length??0)>0?"transform":"explore"}function pd(i){let e=i.producedVars??[];if(e.length>0)return`Created live runtime values: ${e.join(", ")}`;switch(i.stepKind){case"query":return"Gathered external or semantic evidence without creating durable runtime values";case"finalize":return"Prepared completion payload for the responder";case"error":return"Did not produce a durable runtime state update";default:return"Inspected runtime state without creating durable runtime values"}}function ys(i){if(i.tombstone)return i.tombstone;let e=i.stepKind==="error"?"Error step":i.stepKind==="query"?"Query step":i.stepKind==="transform"?"Transform step":i.stepKind==="finalize"?"Finalize step":"Explore step",t=Ji(i.output||"(no output)"),n=Ji(i.actorFieldsOutput.replace(/^Actor fields:\s*/i,""),80),r=i.stateDelta??"No durable runtime state update",o=n?` Actor fields: ${n}.`:"";return`[SUMMARY]: ${e}. ${r}. Result: ${t}.${o}`}function Qi(i){i.producedVars||(i.producedVars=Ju(i.code)),i.referencedVars||(i.referencedVars=[...Qu(i.code)]),i.stepKind||(i.stepKind=cd(i)),i.stateDelta||(i.stateDelta=pd(i)),i.summary||(i.summary=ys(i))}function dd(i){let e=Array.from({length:i.length},()=>new Set),t=new Set;for(let n=i.length-1;n>=0;n--){e[n]=new Set(t);for(let r of i[n]?.referencedVars??[])t.add(r)}return e}function md(i,e){let t=e.actionReplay??"full",n=Math.max(e.recentFullActions??1,0),r=e.successSummarization??!1;for(let a of i)Qi(a);let o=dd(i),s=Math.max(i.length-n,0);i.forEach((a,l)=>{if(a.tombstone){a.replayMode="summary";return}if(t==="full"){a.replayMode="full";return}let u=l>=s,c=a.tags.includes("error"),p=a.output.startsWith("[POLICY]"),d=o[l]??new Set,m=a.producedVars??[],g=m.some(C=>d.has(C));if(u||c||p){a.replayMode="full";return}if(t==="adaptive"&&g){a.replayMode="full";return}let h=a.summary??ys(a),f=!a.tags.includes("error")&&m.length===0&&Ji(a.output).length<=20;if(!a.tags.includes("error")&&!r){a.replayMode="omit";return}if(h&&!f){a.replayMode="summary";return}a.replayMode="omit"})}function gd(i,e){let t=i.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){i.rank=0,Br(i,"dead-end");return}if(t&&n){let r=Wu(i.output),o=Wu(e.output);r!==o?(i.rank=3,Br(i,"pivot")):(i.rank=0,Br(i,"dead-end"));return}if(!t&&!n){let r=Ju(i.code),o=Qu(e.code);r.filter(a=>o.has(a)).length>0?(i.rank=5,Br(i,"foundational")):(i.rank=1,Br(i,"superseded"));return}}function Br(i,e){i.tags.includes(e)||i.tags.push(e)}async function hd(i,e,t,n){let r=`Summarize this resolved error in exactly one line (20-40 tokens).
766
-
767
- Error code:
768
- \`\`\`javascript
769
- ${t.code.slice(0,500)}
770
- \`\`\`
771
-
772
- Error output:
773
- ${t.output.slice(0,300)}
788
+ `)}sanitizeName(e){return e.replace(/[^a-zA-Z0-9_]/g,"_")}parseUriTemplate(e){return(e.match(/\{([^}]+)\}/g)??[]).map(n=>n.slice(1,-1))}expandUriTemplate(e,t){return e.replace(/\{([^}]+)\}/g,(n,r)=>t[r]??"")}async ping(e=3e3){let t=this.sendRequest("ping"),n=new Promise((i,a)=>setTimeout(()=>a(new Error("Ping response timeout exceeded")),e)),r=await Promise.race([t,n]),{result:o}=r;if(typeof o!="object"||o===null||Object.keys(o).length!==0)throw new Error(`Unexpected ping response: ${JSON.stringify(o)}`)}toFunction(){return[...this.functions,...this.promptFunctions,...this.resourceFunctions]}getCapabilities(){return{tools:this.capabilities.tools??!1,resources:this.capabilities.resources??!1,prompts:this.capabilities.prompts??!1}}hasToolsCapability(){return this.capabilities.tools??!1}hasPromptsCapability(){return this.capabilities.prompts??!1}hasResourcesCapability(){return this.capabilities.resources??!1}async listPrompts(e){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("prompts/list",t);return n}async getPrompt(e,t){if(!this.capabilities.prompts)throw new Error("Prompts are not supported");let{result:n}=await this.sendRequest("prompts/get",{name:e,arguments:t});return n}async listResources(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/list",t);return n}async listResourceTemplates(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let t=e?{cursor:e}:void 0,{result:n}=await this.sendRequest("resources/templates/list",t);return n}async readResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");let{result:t}=await this.sendRequest("resources/read",{uri:e});return t}async subscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/subscribe",{uri:e})}async unsubscribeResource(e){if(!this.capabilities.resources)throw new Error("Resources are not supported");await this.sendRequest("resources/unsubscribe",{uri:e})}cancelRequest(e){if(this.activeRequests.has(e)){this.sendNotification("notifications/cancelled",{requestId:e,reason:"Client cancelled request"});let t=this.activeRequests.get(e);t&&t.reject(new Error(`Request ${e} cancelled`)),this.activeRequests.delete(e)}}async sendRequest(e,t={}){let n=Ge(),r={jsonrpc:"2.0",id:n,method:e,params:t},o=new Promise((a,u)=>{this.activeRequests.set(n,{reject:u}),this.transport.send(r).then(l=>{if(this.activeRequests.delete(n),l!==null&&typeof l=="object"&&"error"in l){let c=l;u(new Error(`RPC Error ${c.error.code}: ${c.error.message}`))}else l!==null&&typeof l=="object"&&"result"in l?a({result:l.result}):u(new Error("Invalid response no result or error"))}).catch(l=>{this.activeRequests.delete(n),u(l)})}),{result:i}=await o;return{id:n,result:i}}async sendNotification(e,t={}){let n={jsonrpc:"2.0",method:e,params:t},{debug:r}=this.options;if(r){let o={name:"Notification",id:"mcp_notification",value:`Sending notification: ${JSON.stringify(n,null,2)}`};this.logger(o)}await this.transport.sendNotification(n)}};function nc(s){if(typeof Buffer<"u")return Buffer.from(s).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"");let e="";for(let n=0;n<s.length;n++)e+=String.fromCharCode(s[n]);return(typeof btoa=="function"?btoa(e):"").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}async function rc(s){let t=new TextEncoder().encode(s),n=await fa().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function Zi(){return nc(await rc(Ge()+Math.random().toString(36)))}async function oc(s){return nc(await rc(s))}async function ms(s,e){let t=await fetch(s,{headers:e});if(!t.ok)throw new Error(`HTTP ${t.status} fetching ${s}: ${t.statusText}`);return await t.json()}function sc(s){let e=new URLSearchParams;for(let[t,n]of Object.entries(s))n!==void 0&&e.set(t,n);return e.toString()}function Lr(s){return s.endsWith("/")?s.slice(0,-1):s}function Hd(s){if(!s)return null;let e=s.match(/resource_metadata\s*=\s*"([^"]+)"/i)||s.match(/resource_metadata\s*=\s*([^,\s]+)/i);return e?e[1]:null}async function ic(s,e){let t=Hd(e);if(t){let a=await ms(t),u=Lr(new URL(s).toString().split("?")[0]),l=Lr(a.resource??"");if(!l||l!==u)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${u} but got ${l}`);let c=Array.isArray(a.authorization_servers)?a.authorization_servers:[];if(c.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:u,issuers:c}}let n=new URL(s),r=n.pathname.replace(/\/+$/,""),o=[];r&&r!=="/"&&o.push({url:`${n.origin}/.well-known/oauth-protected-resource${r}`,expected:`${n.origin}${r}`}),o.push({url:`${n.origin}/.well-known/oauth-protected-resource`,expected:`${n.origin}`});let i;for(let a of o)try{let u=await ms(a.url),l=Lr(u.resource??""),c=Lr(a.expected);if(!l||l!==c)throw new Error(`Protected resource metadata 'resource' mismatch. Expected ${c} but got ${l}`);let p=Array.isArray(u.authorization_servers)?u.authorization_servers:[];if(p.length===0)throw new Error("No authorization_servers advertised by protected resource");return{resource:c,issuers:p}}catch(u){i=u}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(i)}`)}async function ac(s){let e=new URL(s),t=e.pathname.replace(/^\/+/,""),n=[];t?(n.push(`${e.origin}/.well-known/oauth-authorization-server/${t}`),n.push(`${e.origin}/.well-known/openid-configuration/${t}`),n.push(`${e.origin}/${t.replace(/\/+$/,"")}/.well-known/openid-configuration`)):(n.push(`${e.origin}/.well-known/oauth-authorization-server`),n.push(`${e.origin}/.well-known/openid-configuration`));let r;for(let o of n)try{let i=await ms(o);if(!i.authorization_endpoint||!i.token_endpoint)throw new Error("AS metadata missing endpoints");let a=i.code_challenge_methods_supported;if(!a||!a.includes("S256"))throw new Error("Authorization server does not advertise PKCE S256 support");return i}catch(i){r=i}throw new Error(`Failed to discover AS metadata for ${s}: ${String(r)}`)}var qn=class{constructor(e){this.oauth=e}tokenCache=new Map;asMetaCache=new Map;key(e,t){return`${e}::${t}`}async getStoredToken(e,t){let n=this.key(e,t);if(this.tokenCache.has(n))return this.tokenCache.get(n);let r=await this.oauth?.tokenStore?.getToken?.(n);return r&&this.tokenCache.set(n,r),r??null}async setStoredToken(e,t,n){let r=this.key(e,t);this.tokenCache.set(r,n),await this.oauth?.tokenStore?.setToken?.(r,n)}async clearStoredToken(e,t){let n=this.key(e,t);this.tokenCache.delete(n),await this.oauth?.tokenStore?.clearToken?.(n)}isExpired(e){return e?Date.now()>e-6e4:!1}async getASMeta(e){if(this.asMetaCache.has(e))return this.asMetaCache.get(e);let t=await ac(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await ic(e.requestedUrl,e.wwwAuthenticate),r=this.oauth.selectAuthorizationServer?await this.oauth.selectAuthorizationServer(n,{}):n[0],o=await this.getASMeta(r),i=e.currentToken??await this.getStoredToken(t,r);if(i?.accessToken&&!this.isExpired(i.expiresAt))return{token:i,issuer:r,asMeta:o,resource:t};if(i?.refreshToken)try{let h=await this.refreshToken(i.refreshToken,t,r,o);return await this.setStoredToken(t,r,h),{token:h,issuer:r,asMeta:o,resource:t}}catch{await this.clearStoredToken(t,r)}let a=this.oauth.redirectUri??"http://localhost:8787/callback",u=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),l=await Zi(),c=await oc(l),p=await Zi(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${sc({response_type:"code",client_id:u.client_id,redirect_uri:a,scope:d,state:p,code_challenge:c,code_challenge_method:"S256",resource:t})}`;if(!this.oauth.onAuthCode)throw new Error(`Authorization required. Provide oauth.onAuthCode to complete the flow. Navigate to: ${m}`);let{code:g,redirectUri:f}=await this.oauth.onAuthCode(m),x=f??a,y=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:l,client:u,redirectUri:x,resource:t});return await this.setStoredToken(t,r,y),{token:y,issuer:r,asMeta:o,resource:t}}async dynamicClientRegistration(e,t){if(!e.registration_endpoint)throw new Error("Authorization server does not support dynamic client registration and no clientId was provided.");let r={application_type:t.startsWith("http://localhost")?"native":"web",client_name:"Ax MCP Client",redirect_uris:[t],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"},o=await fetch(e.registration_endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!o.ok)throw new Error(`Dynamic client registration failed: ${o.status} ${o.statusText}`);let i=await o.json();if(!i.client_id)throw new Error("Dynamic client registration did not return client_id");return i}async exchangeCodeForToken(e){let t=new URLSearchParams;t.set("grant_type","authorization_code"),t.set("code",e.code),t.set("redirect_uri",e.redirectUri),t.set("client_id",e.client.client_id),t.set("code_verifier",e.codeVerifier),t.set("resource",e.resource),e.client.client_secret&&t.set("client_secret",e.client.client_secret);let n=await fetch(e.asMeta.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!n.ok)throw new Error(`Token exchange failed: ${n.status} ${n.statusText}`);let r=await n.json();if(!r.access_token)throw new Error("No access_token in token response");let o=r.expires_in?Date.now()+r.expires_in*1e3:void 0;return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresAt:o}}async refreshToken(e,t,n,r){let o=new URLSearchParams;o.set("grant_type","refresh_token"),o.set("refresh_token",e),o.set("resource",t),this.oauth?.clientId&&o.set("client_id",this.oauth.clientId),this.oauth?.clientSecret&&o.set("client_secret",this.oauth.clientSecret);let i=await fetch(r.token_endpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:o.toString()});if(!i.ok)throw new Error(`Token refresh failed: ${i.status} ${i.statusText}`);let a=await i.json();if(!a.access_token)throw new Error("No access_token in refresh response");let u=a.expires_in?Date.now()+a.expires_in*1e3:void 0;return{accessToken:a.access_token,refreshToken:a.refresh_token??e,expiresAt:u}}};var gs=class{mcpEndpoint;sessionId;eventSource;pendingRequests=new Map;messageHandler;customHeaders;oauthHelper;currentToken;currentIssuer;constructor(e,t){this.mcpEndpoint=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new qn(t?.oauth)}setHeaders(e){this.customHeaders={...e}}setAuthorization(e){this.customHeaders.Authorization=e}getHeaders(){return{...this.customHeaders}}buildHeaders(e){let t={...this.customHeaders,...e};return this.sessionId&&(t["Mcp-Session-Id"]=this.sessionId),t}setMessageHandler(e){this.messageHandler=e}async connect(){return Promise.resolve()}async openListeningStream(){return new Promise((e,t)=>{let n=this.buildHeaders({Accept:"text/event-stream"}),r=new URL(this.mcpEndpoint);if(Object.keys(this.customHeaders).length>0){this.openListeningStreamWithFetch(n).then(e).catch(t);return}this.eventSource=new EventSource(r.toString()),this.eventSource.onopen=()=>e(),this.eventSource.onmessage=o=>{try{let i=JSON.parse(o.data);this.messageHandler&&this.messageHandler(i)}catch(i){console.error("Failed to parse SSE message:",i)}},this.eventSource.onerror=()=>t(new Error("Failed to establish SSE connection"))})}async openListeningStreamWithFetch(e){let t=await fetch(this.mcpEndpoint,{method:"GET",headers:e});if(t.status===401){let a=t.headers.get("WWW-Authenticate"),u=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!u)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${u.token.accessToken}`,this.openListeningStreamWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!t.ok)throw new Error(`Failed to open SSE stream: ${t.status} ${t.statusText}`);if(!t.body)throw new Error("No response body available for SSE stream");let n=t.body.getReader(),r=new TextDecoder,o="",i=async()=>{try{let{done:a,value:u}=await n.read();if(a){n.releaseLock();return}o+=r.decode(u,{stream:!0});let l=o.split(`
789
+ `);o=l.pop()||"";for(let c of l)if(c.startsWith("data: ")){let p=c.slice(6);if(p==="[DONE]")return;try{let d=JSON.parse(p);this.messageHandler&&this.messageHandler(d)}catch(d){console.error("Failed to parse SSE data:",d)}}await i()}catch(a){throw n.releaseLock(),a}};await i()}async send(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let a=r.headers.get("WWW-Authenticate"),u=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:a,currentToken:null});if(!u)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${u.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);let o=r.headers.get("Mcp-Session-Id");o&&(this.sessionId=o);let i=r.headers.get("Content-Type");if(i?.includes("text/event-stream"))return this.handleSSEResponse(r,e.id);if(i?.includes("application/json"))return r.json();throw new Error(`Unexpected content type: ${i}`)}async handleSSEResponse(e,t){return new Promise((n,r)=>{let o=e.body?.getReader();if(!o){r(new Error("No response body reader available"));return}let i=new TextDecoder,a="",u=async()=>{try{let{done:l,value:c}=await o.read();if(l){o.releaseLock();return}a+=i.decode(c,{stream:!0});let p=a.split(`
790
+ `);a=p.pop()||"";for(let d of p)if(d.startsWith("data: ")){let m=d.slice(6);if(m==="[DONE]")return;try{let g=JSON.parse(m);if("id"in g&&g.id===t){n(g);return}this.messageHandler&&this.messageHandler(g)}catch(g){console.error("Failed to parse SSE data:",g)}}await u()}catch(l){o.releaseLock(),r(l)}};u().catch(r)})}async sendNotification(e){let t=this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),n=JSON.stringify(e),r=await fetch(this.mcpEndpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),i=await this.oauthHelper.ensureAccessToken({requestedUrl:this.mcpEndpoint,wwwAuthenticate:o,currentToken:null});if(!i)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${i.token.accessToken}`,r=await fetch(this.mcpEndpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json",Accept:"application/json, text/event-stream"}),body:n})}if(!r.ok)throw r.status===404&&this.sessionId?(this.sessionId=void 0,new Error("Session expired. Please reinitialize.")):new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}async terminateSession(){if(this.sessionId)try{let e=this.buildHeaders({});(await fetch(this.mcpEndpoint,{method:"DELETE",headers:e})).status===405&&console.info("Server does not support explicit session termination")}catch(e){console.error("Failed to terminate session:",e)}finally{this.sessionId=void 0}}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0)}};var fs=class{endpoint=null;sseUrl;eventSource;customHeaders={};oauthHelper;currentToken;currentIssuer;sseAbort;pendingRequests=new Map;messageHandler;endpointReady;constructor(e,t){this.sseUrl=e,this.customHeaders={...t?.headers??{}},t?.authorization&&(this.customHeaders.Authorization=t.authorization),this.oauthHelper=new qn(t?.oauth)}buildHeaders(e){return{...this.customHeaders,...e}}async openSSEWithFetch(e){let t=new AbortController;this.sseAbort=t;let n=await fetch(this.sseUrl,{method:"GET",headers:e,signal:t.signal});if(n.status===401){let o=n.headers.get("WWW-Authenticate"),i=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!i)throw new Error("HTTP 401: Unauthorized");return this.customHeaders.Authorization=`Bearer ${i.token.accessToken}`,this.openSSEWithFetch(this.buildHeaders({Accept:"text/event-stream"}))}if(!n.ok)throw new Error("Failed to establish SSE connection");let r=this.createEndpointReady();this.consumeSSEStream(n),await r}createEndpointReady(){if(!this.endpointReady){let e,t=new Promise(n=>{e=n});this.endpointReady={resolve:e,promise:t}}return this.endpointReady.promise}async consumeSSEStream(e){if(!e.body)throw new Error("No response body available for SSE stream");let t=e.body.getReader(),n=new TextDecoder,r="",o=null;for(;;){let{done:i,value:a}=await t.read();if(i)break;r+=n.decode(a,{stream:!0});let u=r.split(`
791
+ `);r=u.pop()||"";for(let l of u)if(l.startsWith("event: "))o=l.slice(7).trim();else if(l.startsWith("data: ")){let c=l.slice(6);if(o==="endpoint"){let p=c.trim(),d;try{let m=JSON.parse(p);typeof m=="string"?d=m:m&&typeof m=="object"&&"uri"in m&&(d=m.uri)}catch{d=p}if(!d)throw new Error("Endpoint URI missing in SSE event data");/^https?:\/\//i.test(d)||(d=new URL(this.sseUrl).origin+(d.startsWith("/")?d:`/${d}`)),this.endpoint=d,this.endpointReady&&(this.endpointReady.resolve(),this.endpointReady=void 0)}else{let p=c.trim();try{let d=JSON.parse(p);if(d&&typeof d=="object"&&"id"in d){let m=d.id,g=this.pendingRequests.get(m);g?(g.resolve(d),this.pendingRequests.delete(m)):this.messageHandler&&this.messageHandler(d)}else this.messageHandler&&this.messageHandler(d)}catch{}}}else l.trim()===""&&(o=null)}}async connect(){let e=this.buildHeaders({Accept:"text/event-stream"});await this.openSSEWithFetch(e)}async send(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=new Promise((a,u)=>{this.pendingRequests.set(e.id,{resolve:a,reject:u})}),o=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(o.status===401){let a=o.headers.get("WWW-Authenticate"),u=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:a,currentToken:null});if(!u)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${u.token.accessToken}`,o=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!o.ok)throw this.pendingRequests.delete(e.id),new Error(`HTTP error ${o.status}: ${o.statusText}`);if(o.headers.get("Content-Type")?.includes("application/json")){let a=await o.json();return this.pendingRequests.delete(e.id),a}return r}async sendNotification(e){if(!this.endpoint)throw new Error("HTTPTransport endpoint is not initialized. Call connect() first.");let t=this.buildHeaders({"Content-Type":"application/json"}),n=JSON.stringify(e),r=await fetch(this.endpoint,{method:"POST",headers:t,body:n});if(r.status===401){let o=r.headers.get("WWW-Authenticate"),i=await this.oauthHelper.ensureAccessToken({requestedUrl:this.sseUrl,wwwAuthenticate:o,currentToken:null});if(!i)throw new Error("HTTP 401: Unauthorized");this.customHeaders.Authorization=`Bearer ${i.token.accessToken}`,r=await fetch(this.endpoint,{method:"POST",headers:this.buildHeaders({"Content-Type":"application/json"}),body:n})}if(!r.ok)throw new Error(`HTTP error ${r.status}: ${r.statusText}`);r.status!==202&&console.warn(`Unexpected status for notification: ${r.status}`)}close(){this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.sseAbort&&(this.sseAbort.abort(),this.sseAbort=void 0)}};function ea(s){return s.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??s.slice(0,80)}function na(s){return ls(s)}var Kd=new Set(["var","let","const","function","return","if","else","for","while","do","switch","case","break","continue","try","catch","finally","throw","new","delete","typeof","void","in","of","instanceof","this","class","extends","super","import","export","default","from","as","async","await","yield","true","false","null","undefined","console","log"]);function lc(s){let e=Nr(s),t=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,n=new Set,r=t.exec(e);for(;r!==null;)r[1]&&!Kd.has(r[1])&&n.add(r[1]),r=t.exec(e);return n}function cc(s){let e=lc(s);for(let t of na(s))e.delete(t);return e}var Wd=new Set(["dead-end","foundational","pivot","superseded"]);function Hn(s,e=120){let t=s.replace(/\s+/g," ").trim();return t.length<=e?t:`${t.slice(0,e-3)}...`}function Vd(s){return/\b(final|ask_clarification)\s*\(/.test(s)}function Jd(s){return s.tags.includes("error")?"error":Vd(s.code)?"finalize":/\b(llmQuery|listModuleFunctions|getFunctionDefinitions)\s*\(/.test(s.code)?"query":(s.producedVars?.length??0)>0?"transform":"explore"}function Qd(s){let e=s.producedVars??[];if(e.length>0)return`Created live runtime values: ${e.join(", ")}`;switch(s.stepKind){case"query":return"Gathered external or semantic evidence without creating durable runtime values";case"finalize":return"Prepared completion payload for the responder";case"error":return"Did not produce a durable runtime state update";default:return"Inspected runtime state without creating durable runtime values"}}function ra(s){if(s.tombstone)return s.tombstone;let e=s.stepKind==="error"?"Error step":s.stepKind==="query"?"Query step":s.stepKind==="transform"?"Transform step":s.stepKind==="finalize"?"Finalize step":"Explore step",t=Hn(s.output||"(no output)"),n=Hn(s.actorFieldsOutput.replace(/^Actor fields:\s*/i,""),80),r=s.stateDelta??"No durable runtime state update",o=n?` Actor fields: ${n}.`:"";return`[SUMMARY]: ${e}. ${r}. Result: ${t}.${o}`}function Yd(s){s.rank=void 0,s.tags=s.tags.filter(e=>!Wd.has(e))}function yt(s){s.producedVars||(s.producedVars=na(s.code)),s.referencedVars||(s.referencedVars=[...lc(s.code)]),s.stepKind||(s.stepKind=Jd(s)),s.stateDelta||(s.stateDelta=Qd(s)),s.summary||(s.summary=ra(s))}function pc(s,e){let t=[...s.matchAll(e)];return t.length===0?[]:t.map((n,r)=>{let o=n[1]?.trim(),i=n.index??0,a=t[r+1]?.index??s.length;if(o)return{key:o,text:s.slice(i,a).trim()}}).filter(n=>!!n)}function Xd(s){return/\blistModuleFunctions\s*\(/.test(s.code)?pc(s.output,/^### Module `([^`]+)`/gm).map(e=>({module:e.key,text:e.text})):[]}function Zd(s){return/\bgetFunctionDefinitions\s*\(/.test(s.code)?pc(s.output,/^### `([^`]+)`/gm).map(e=>({qualifiedName:e.key,text:e.text})):[]}function em(s){let e=Nr(s),t=new Set,n=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\.([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g,r=n.exec(e);for(;r;){let o=r[1],i=r[2];o&&i&&t.add(`${o}.${i}`),r=n.exec(e)}return[...t]}function oa(s){s._discoveryModuleSections||(s._discoveryModuleSections=Xd(s)),s._discoveryFunctionSections||(s._discoveryFunctionSections=Zd(s)),s._directQualifiedCalls||(s._directQualifiedCalls=em(s.code))}function tm(s){return oa(s),s._directQualifiedCalls?.find(Boolean)}function dc(s){let e=new Map;for(let t of s){let n=t;yt(n);let r=tm(n);for(let i of n.producedVars??[])e.set(i,{createdTurn:n.turn,stepKind:n.stepKind,source:r});let o=cc(n.code);for(let i of o){let a=e.get(i);a&&(a.lastReadTurn=Math.max(a.lastReadTurn??a.createdTurn,n.turn))}}return e}function nm(s){let e=Array.from({length:s.length},()=>new Set),t=new Set;for(let n=s.length-1;n>=0;n--){e[n]=new Set(t);let r=s[n];if(!(!r||r.tags.includes("error"))){oa(r);for(let o of r._directQualifiedCalls??[])t.add(o)}}return e}function rm(s,e){let t=s;oa(t);let n=t._discoveryFunctionSections??[];if(n.length>0){let o=n.filter(i=>!e.has(i.qualifiedName));return o.length===n.length?t:o.length===0?void 0:{...t,output:o.map(i=>i.text).join(`
792
+
793
+ `),summary:void 0,_discoveryFunctionSections:o}}let r=t._discoveryModuleSections??[];if(r.length>0){let o=new Set([...e].map(a=>a.split(".")[0])),i=r.filter(a=>!o.has(a.module));return i.length===r.length?t:i.length===0?void 0:{...t,output:i.map(a=>a.text).join(`
794
+
795
+ `),summary:void 0,_discoveryModuleSections:i}}return t}function hs(s,e){if(!e?.pruneUsedDocs||s.length===0)return[...s];let t=nm(s);return s.flatMap((n,r)=>{let o=rm(n,t[r]??new Set);return o?[o]:[]})}function om(s){let e=Array.from({length:s.length},()=>new Set),t=new Set;for(let n=s.length-1;n>=0;n--){e[n]=new Set(t);for(let r of s[n]?.referencedVars??[])t.add(r)}return e}function sm(s,e){let t=e.actionReplay??"full",n=Math.max(e.recentFullActions??1,0);for(let i of s)yt(i);let r=om(s),o=Math.max(s.length-n,0);s.forEach((i,a)=>{if(i.tombstone){i.replayMode="full";return}if(t==="full"){i.replayMode="full";return}let u=a>=o,l=i.tags.includes("error"),c=i.output.startsWith("[POLICY]"),p=r[a]??new Set,m=(i.producedVars??[]).some(g=>p.has(g));if(u||l||c){i.replayMode="full";return}if(t==="adaptive"&&m){i.replayMode="full";return}i.replayMode="omit"})}function im(s,e){yt(s),yt(e),Yd(s);let t=s.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){s.rank=0,$r(s,"dead-end");return}if(t&&n){let r=ea(s.output),o=ea(e.output);r!==o?(s.rank=3,$r(s,"pivot")):(s.rank=0,$r(s,"dead-end"));return}if(!t&&!n){let r=s.producedVars??na(s.code);if(r.length===0||s.stepKind==="explore"||s.stepKind==="query")return;let o=cc(e.code);if(r.filter(a=>o.has(a)).length>0){s.rank=5,$r(s,"foundational");return}s.stepKind==="transform"&&(s.rank=1,$r(s,"superseded"));return}}function $r(s,e){s.tags.includes(e)||s.tags.push(e)}function ta(s,e){let t=s.output.split(`
796
+ `).map(r=>r.trim()).filter(Boolean).find(r=>/\b\w+Error:/.test(r)&&!r.startsWith("["))??s.output.split(`
797
+ `).map(r=>r.trim()).filter(r=>!!r&&!r.startsWith("[")&&!r.startsWith("...")).at(-1)??ea(s.output);return`[TOMBSTONE]: Resolved ${Hn(t,96)} in turn ${e.turn}.`}function am(s){return s.startsWith("[TOMBSTONE]: Resolved ")}var um=`You are an internal AxAgent tombstone summarizer.
798
+
799
+ Write the output field \`tombstone\` as exactly one concise line.
800
+ - Start with \`[TOMBSTONE]:\`
801
+ - Summarize the resolved error and the successful fix.
802
+ - Mention one failed approach to avoid when possible.
803
+ - Do not include code fences, bullet points, or extra prose.
804
+ - Keep it roughly 20-40 tokens.`,lm=`You are an internal AxAgent checkpoint summarizer.
805
+
806
+ Write the output field \`checkpointSummary\` as plain text with exactly these labels in this order:
807
+ Objective:
808
+ Durable state:
809
+ Evidence:
810
+ Conclusions:
811
+ Actor fields:
812
+ Next step:
774
813
 
775
- Resolution code:
776
- \`\`\`javascript
777
- ${n.code.slice(0,500)}
778
- \`\`\`
814
+ Rules:
815
+ - Keep only information needed to continue the task.
816
+ - Do not restate raw code or quote large outputs.
817
+ - Use "none" when a section has nothing worth preserving.
818
+ - Be concise and factual.`;function mc(s){let{mem:e,description:t,maxSteps:n,...r}=s??{};return r}function gc(s,e,t){let n=mc(t);return{...n,description:s,traceLabel:n.traceLabel??e,maxSteps:1}}function fc(s){return{...mc(s),maxSteps:1}}async function cm(s,e,t,n,r){let o=new Ie("errorCode:string, errorOutput:string, resolutionCode:string -> tombstone:string",{...gc(um,"ax-agent-tombstone-summary",e)});try{let i=await o.forward(s,{errorCode:n.code.slice(0,500),errorOutput:n.output.slice(0,300),resolutionCode:r.code.slice(0,500)},fc(t));return(typeof i.tombstone=="string"?i.tombstone.trim():String(i.tombstone).trim())||ta(n,r)}catch{return ta(n,r)}}function pm(s){return s.map(e=>{yt(e);let t=e.actorFieldsOutput.replace(/^Actor fields:\s*/i,"").trim();return[`Turn: ${e.turn}`,`Step kind: ${e.stepKind??"explore"}`,`Referenced inputs: ${(e.referencedVars??[]).join(", ")||"none"}`,`Durable values created: ${(e.producedVars??[]).join(", ")||"none"}`,`State delta: ${e.stateDelta??"none"}`,`Observed result: ${Hn(e.output||"(no output)",240)}`,`Actor fields: ${t||"none"}`,`Code excerpt: ${Hn(e.code||"(no code)",240)}`].join(`
819
+ `)}).join(`
779
820
 
780
- Format: [TOMBSTONE]: Resolved [Error Type] in [Module]. Fix: [1-line-summary]. Avoid: [failed-approach].`;try{let o=await i.chat({chatPrompt:[{role:"system",content:"You are a concise code summarizer."},{role:"user",content:r}],...e?.model?{model:e.model}:{},...e?.modelConfig?{modelConfig:e.modelConfig}:{}});if(!fd(o)&&o.results?.[0]?.content){let s=o.results[0].content;return typeof s=="string"?s.trim():String(s)}return Vu}catch{return Vu}}var Vu="[TOMBSTONE]: Error was resolved in subsequent turn.";function fd(i){return typeof i=="object"&&i!==null&&"getReader"in i}async function Yi(i,e,t,n){let r=i[e];if(!r)return;Qi(r);let o=r.tags.includes("error");if(t.hindsightEvaluation&&i.length>=2){let s=i[i.length-2];gd(s,r)}if(t.tombstoning&&n){for(let s of i)if(s.tags.includes("error")&&!s.tombstone&&!s._tombstonePromise){let a=i.indexOf(s),l=i[a+1];if(l&&!l.tags.includes("error")){let u=typeof t.tombstoning=="object"?t.tombstoning:void 0;s._tombstonePromise=hd(n,u,s,l),s._tombstonePromise.then(c=>{s.tombstone=c}).catch(()=>{}).finally(()=>{s._tombstonePromise=void 0})}}}if(t.errorPruning&&!o){let s=i.filter(a=>!a.tags.includes("error")||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}if(t.hindsightEvaluation){let s=i.filter((a,l)=>l===i.length-1||a.rank===void 0||a.rank>=t.pruneRank||a.tombstone!=null||a._tombstonePromise!=null);i.length=0,i.push(...s)}}function Xi(i,e){if(i.length===0&&!e.stateSummary)return"";md(i,e);let t=i.map(r=>{if(r.tombstone)return`Action ${r.turn}:
781
- ${r.tombstone}`;switch(r.replayMode){case"omit":return"";case"summary":return`Action ${r.turn}:
782
- ${r.summary??ys(r)}`;default:return`Action ${r.turn}:
821
+ `)}function uc(s){let e=new Set,t=new Set,n=[],r=[],o="Continue from the latest live runtime state.";for(let i of s){yt(i),e.add(i.stepKind??"explore");for(let l of i.producedVars??[])t.add(l);let a=Hn(i.output||"(no output)",140);n.push(`Turn ${i.turn}: ${a}`);let u=i.actorFieldsOutput.replace(/^Actor fields:\s*/i,"").trim();u&&r.push(`Turn ${i.turn}: ${u}`),o=i.stepKind==="finalize"?"Complete the responder handoff.":"Continue from the latest live runtime state."}return[`Objective: ${[...e].join(", ")||"none"}`,`Durable state: ${[...t].join(", ")||"none"}`,`Evidence: ${n.join(" | ")||"none"}`,"Conclusions: Preserve the durable state and evidence above.",`Actor fields: ${r.join(" | ")||"none"}`,`Next step: ${o}`].join(`
822
+ `)}async function hc(s,e,t,n){let r=new Ie("turns:string -> checkpointSummary:string",{...gc(lm,"ax-agent-checkpoint-summary",e)});try{let o=await r.forward(s,{turns:pm(n)},fc(t));return(typeof o.checkpointSummary=="string"?o.checkpointSummary.trim():String(o.checkpointSummary).trim())||uc(n)}catch{return uc(n)}}async function sa(s,e,t,n,r){let o=s[e];if(!o)return;yt(o);let i=o.tags.includes("error");if(t.hindsightEvaluation&&s.length>=2){let a=s[s.length-2];im(a,o)}if(t.errorPruning||t.tombstoning)for(let a of s){if(!a.tags.includes("error"))continue;let u=s.indexOf(a),l=s[u+1];if(!l||l.tags.includes("error")||(t.errorPruning&&!a.tombstone&&(a.tombstone=ta(a,l)),!(!!t.tombstoning&&!!n&&!a._tombstonePromise&&(!a.tombstone||am(a.tombstone)))||!n))continue;let p=typeof t.tombstoning=="object"?t.tombstoning:void 0;a._tombstonePromise=cm(n,p,r,a,l),a._tombstonePromise.then(d=>{a.tombstone=d}).catch(()=>{}).finally(()=>{a._tombstonePromise=void 0})}if(t.errorPruning&&!i){let a=s.filter(u=>!u.tags.includes("error")||u.tombstone!=null||u._tombstonePromise!=null);s.length=0,s.push(...a)}if(t.hindsightEvaluation){let a=s[s.length-1]?.turn??o.turn,u=s.filter((l,c)=>c===s.length-1||l.rank===void 0||!l.tags.includes("error")&&a-l.turn<t.rankPruneGraceTurns||l.rank>=t.pruneRank||l.tombstone!=null||l._tombstonePromise!=null);s.length=0,s.push(...u)}}function dm(s,e){if(e.has(s.turn)&&!s.tags.includes("error")&&s.replayMode!=="full")return"";if(s.tombstone)return`Action ${s.turn}:
823
+ ${s.tombstone}`;switch(s.replayMode){case"omit":return yt(s),`Action ${s.turn}:
824
+ ${s.summary??ra(s)}`;default:return`Action ${s.turn}:
783
825
  \`\`\`javascript
784
- ${r.code}
826
+ ${s.code}
785
827
  \`\`\`
786
828
  Result:
787
- ${r.output}${r.actorFieldsOutput}`}}).filter(Boolean).join(`
788
-
789
- `);if(!e.stateSummary)return t;let n=[`Live Runtime State:
790
- ${e.stateSummary}`];return t&&n.push(t),n.join(`
791
-
792
- `)}function Yu(i,e){let t=i.map(r=>{Qi(r);let o=r.tombstone??r.summary??ys(r);return`- Action ${r.turn}: ${o}`}).join(`
793
- `),n=["Actor stopped without calling final(...). Evidence summary:"];return t?n.push(t):n.push("- No actions were taken."),e?.stateSummary&&n.push(`Current runtime state:
794
- ${e.stateSummary}`),n.join(`
795
- `)}function Xu(i){return`(() => {
796
- const skip = new Set([${i.map(t=>`'${t}'`).join(",")}]);
797
- return Object.entries(globalThis)
798
- .filter(([k]) => !skip.has(k) && !k.startsWith('_'))
799
- .map(([k, v]) => {
800
- const type = Array.isArray(v) ? 'array' : typeof v;
801
- let size = '';
802
- if (typeof v === 'string') size = v.length + ' chars';
803
- else if (Array.isArray(v)) size = v.length + ' items';
804
- else if (v && typeof v === 'object') size = Object.keys(v).length + ' keys';
805
- let preview = '';
829
+ ${s.output}${s.actorFieldsOutput}`}}function xs(s,e){let t=hs(s,{pruneUsedDocs:e.pruneUsedDocs});if(t.length===0)return{promptFacingEntries:t,checkpointEntries:[],historyText:"",historyChars:0};sm(t,e);let n=t.filter(i=>!i.tags.includes("error")&&i.replayMode!=="full"),r=new Set(e.checkpointTurns??[]),o=t.map(i=>dm(i,r)).filter(Boolean).join(`
830
+
831
+ `);return{promptFacingEntries:t,checkpointEntries:n,historyText:o,historyChars:o.length}}function xc(s,e){let t=xs(s,e);if(t.promptFacingEntries.length===0&&!e.stateSummary&&!e.checkpointSummary)return"";let n=[];return e.stateSummary&&n.push(`Live Runtime State:
832
+ ${e.stateSummary}`),t.historyText&&n.push(t.historyText),e.checkpointSummary&&n.push(`Checkpoint Summary:
833
+ ${e.checkpointSummary}`),n.join(`
834
+
835
+ `)}function Ac(s,e){let t=hs(s,{pruneUsedDocs:e?.pruneUsedDocs}),n=new Set(e?.checkpointTurns??[]),r=t.map(i=>{if(n.has(i.turn)&&!i.tags.includes("error"))return"";yt(i);let a=i.tombstone??i.summary??ra(i);return`- Action ${i.turn}: ${a}`}).filter(Boolean).join(`
836
+ `),o=["Actor stopped without calling final(...). Evidence summary:"];return e?.checkpointSummary&&o.push(`Checkpoint summary:
837
+ ${e.checkpointSummary}`),r?o.push(r):e?.checkpointSummary||o.push("- No actions were taken."),e?.stateSummary&&o.push(`Current runtime state:
838
+ ${e.stateSummary}`),o.join(`
839
+ `)}function yc(s,e=[]){return`(() => {
840
+ const skip = new Set([${[...s,...e].map(n=>`'${n}'`).join(",")}]);
841
+ const truncate = (text, maxChars) =>
842
+ text.length <= maxChars ? text : text.slice(0, maxChars - 3) + '...';
843
+ const previewAtom = (value) => {
844
+ if (value === null) return 'null';
845
+ if (value === undefined) return 'undefined';
846
+ const valueType = typeof value;
847
+ if (valueType === 'string') return JSON.stringify(truncate(value, 40));
848
+ if (valueType === 'number' || valueType === 'boolean' || valueType === 'bigint') {
849
+ return String(value);
850
+ }
851
+ if (valueType === 'symbol') return String(value);
852
+ if (valueType === 'function') {
853
+ return '[function ' + (value.name || 'anonymous') + ']';
854
+ }
855
+ if (Array.isArray(value)) return '[array(' + value.length + ')]';
856
+ if (value instanceof Date) {
857
+ return Number.isFinite(value.getTime()) ? value.toISOString() : String(value);
858
+ }
859
+ if (value instanceof Error) {
860
+ return (value.name || 'Error') + ': ' + (value.message || '');
861
+ }
862
+ if (value instanceof Map) return '[map(' + value.size + ')]';
863
+ if (value instanceof Set) return '[set(' + value.size + ')]';
864
+ const ctorName =
865
+ value && value.constructor && typeof value.constructor.name === 'string'
866
+ ? value.constructor.name
867
+ : '';
868
+ return ctorName && ctorName !== 'Object' ? '[' + ctorName + ']' : '[object]';
869
+ };
870
+ const previewValue = (value, type, ctor) => {
871
+ if (type === 'array') {
872
+ const items = value.slice(0, 3).map((item) => previewAtom(item));
873
+ return '[' + items.join(', ') + (value.length > 3 ? ', ...' : '') + ']';
874
+ }
875
+ if (type === 'map') {
876
+ const items = Array.from(value.entries())
877
+ .slice(0, 3)
878
+ .map(([key, item]) => previewAtom(key) + ' => ' + previewAtom(item));
879
+ return 'Map(' + value.size + ') {' + items.join(', ') + (value.size > 3 ? ', ...' : '') + '}';
880
+ }
881
+ if (type === 'set') {
882
+ const items = Array.from(value.values())
883
+ .slice(0, 5)
884
+ .map((item) => previewAtom(item));
885
+ return 'Set(' + value.size + ') {' + items.join(', ') + (value.size > 5 ? ', ...' : '') + '}';
886
+ }
887
+ if (type === 'date') return previewAtom(value);
888
+ if (type === 'error') return previewAtom(value);
889
+ if (type === 'function') return previewAtom(value);
890
+ if (type === 'object') {
891
+ const keys = Object.keys(value);
892
+ const shown = keys.slice(0, 4);
893
+ const prefix = ctor && ctor !== 'Object' ? ctor + ' ' : '';
894
+ return prefix + '{' + shown.join(', ') + (keys.length > shown.length ? ', ...' : '') + '}';
895
+ }
896
+ return previewAtom(value);
897
+ };
898
+ const describeSize = (value, type) => {
899
+ if (type === 'string') return value.length + ' chars';
900
+ if (type === 'array') return value.length + ' items';
901
+ if (type === 'map' || type === 'set') return value.size + ' items';
902
+ if (type === 'object') return Object.keys(value).length + ' keys';
903
+ return undefined;
904
+ };
905
+ const describeType = (value) => {
906
+ if (value === null) return { type: 'null' };
907
+ if (Array.isArray(value)) return { type: 'array', ctor: 'Array' };
908
+ if (value instanceof Map) return { type: 'map', ctor: 'Map' };
909
+ if (value instanceof Set) return { type: 'set', ctor: 'Set' };
910
+ if (value instanceof Date) return { type: 'date', ctor: 'Date' };
911
+ if (value instanceof Error) {
912
+ return {
913
+ type: 'error',
914
+ ctor:
915
+ typeof value.name === 'string' && value.name.trim()
916
+ ? value.name
917
+ : 'Error',
918
+ };
919
+ }
920
+ const type = typeof value;
921
+ if (type !== 'object') return { type };
922
+ const ctor =
923
+ value && value.constructor && typeof value.constructor.name === 'string'
924
+ ? value.constructor.name
925
+ : undefined;
926
+ return { type: 'object', ctor };
927
+ };
928
+ const entries = Object.getOwnPropertyNames(globalThis)
929
+ .filter((name) => !skip.has(name) && !name.startsWith('_'))
930
+ .sort()
931
+ .flatMap((name) => {
806
932
  try {
807
- preview = JSON.stringify(v);
808
- if (preview.length > 80) preview = preview.slice(0, 80) + '...';
809
- } catch { preview = String(v).slice(0, 80); }
810
- return k + ': ' + type + (size ? ' (' + size + ')' : '') + ' = ' + preview;
811
- }).join('\\n') || '(no user variables)';
812
- })()`}function xd(i){let e=i.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function Ad(i){let e=new Set(i);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function Zi(i){if(i.enum)return i.enum.map(n=>`"${n}"`).join(" | ");let e=xd(i);if(e.length===0)return"unknown";if(Ad(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=i.items?Zi(i.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?i.properties&&Object.keys(i.properties).length>0?ec(i):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function ec(i,e){if(!i)return"{}";let t=!!i.properties&&Object.keys(i.properties).length>0,n=i.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(i.required??[]),o=e?.respectRequired??!1,s=Object.entries(i.properties).map(([a,l])=>{let u=Zi(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return i?.additionalProperties===!0&&s.push("[key: string]: unknown"),`{ ${s.join(", ")} }`}function yd(i){return i?Zi(i):"unknown"}function Zu(i){let e=ec(i.parameters,{respectRequired:!0}),t=i.returns?`: Promise<${yd(i.returns)}>`:"";return`- \`${i.qualifiedName}(args: ${e})${t}\``}function bs(i,e,t,n){let r=e.length>0?e.map(d=>{let m=Ln(d.type),g=d.isOptional?"optional":"required",h=d.description?`: ${d.description}`:"";return`- \`${d.name}\` -> \`inputs.${d.name}\` (${m}, ${g})${h}`}).join(`
813
- `):"(none)",o=t.map(d=>`\`${d.name}\``).join(", "),s=[...n.agents??[]].sort((d,m)=>d.name.localeCompare(m.name)),a=[...n.agentFunctions??[]].sort((d,m)=>d.namespace!==m.namespace?d.namespace.localeCompare(m.namespace):d.name.localeCompare(m.name)),l=n.agentModuleNamespace??"agents",u=!!n.discoveryMode,c=n.availableModules?[...new Set(n.availableModules)].sort((d,m)=>d.localeCompare(m)):[...new Set([...a.map(d=>d.namespace),...s.length>0?[l]:[]])].sort((d,m)=>d.localeCompare(m)),p=mt("rlm/actor.md",{contextVarList:r,responderOutputFieldTitles:o,discoveryMode:u,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:!u&&s.length>0,agentModuleNamespace:l,agentFunctionsList:s.map(d=>Zu({qualifiedName:`${l}.${d.name}`,parameters:d.parameters})).join(`
814
- `),hasFunctions:!u&&a.length>0,functionsList:a.map(d=>Zu({qualifiedName:`${d.namespace}.${d.name}`,parameters:d.parameters,returns:d.returns})).join(`
815
- `),hasModules:u&&c.length>0,modulesList:c.map(d=>`- \`${d}\``).join(`
816
- `),runtimeUsageInstructions:String(n.runtimeUsageInstructions),enforceIncrementalConsoleTurns:!!n.enforceIncrementalConsoleTurns}).replace(/\n{3,}/g,`
817
-
818
- `).trim();return i?`${p}
819
-
820
- ${i}`:p}function Is(i,e){let t=e.length>0?e.map(r=>{let o=Ln(r.type),s=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${s})`}).join(`
821
- `):"(none)",n=mt("rlm/responder.md",{contextVarSummary:t}).trim();return i?`${n}
822
-
823
- ${i}`:n}var tc=50,bd=5e3,Id=8,nc=10,Cd=2,Td=1200,Cs="agents",Ts="listModuleFunctions",ws="getFunctionDefinitions",jr=class i{ai;program;actorProgram;responderProgram;agents;agentFunctions;discoveryNamespaces=[];debug;options;rlmConfig;runtime;actorFieldNames;localFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;responderDescription;recursionForwardOptions;actorForwardOptions;responderForwardOptions;inputUpdateCallback;contextPromptConfigByField=new Map;agentModuleNamespace=Cs;functionDiscoveryEnabled=!1;runtimeUsageInstructions="";enforceIncrementalConsoleTurns=!1;activeAbortControllers=new Set;_stopRequested=!1;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;constructor({ai:e,agentIdentity:t,agentModuleNamespace:n,signature:r},o){let{debug:s,contextFields:a,runtime:l,maxSubAgentCalls:u,maxRuntimeChars:c,maxBatchedLlmQueryConcurrency:p,maxTurns:d,trajectoryPruning:m,contextManagement:g,actorFields:h,actorCallback:f,mode:C,recursionOptions:x,actorOptions:I,responderOptions:w,inputUpdateCallback:A}=o;this.ai=e,this.agents=o.agents?.local,this.agentFunctions=o.functions?.local??[],this.functionDiscoveryEnabled=o.functions?.discovery??!1,this.debug=s,this.options=o,this.runtime=l??new Xt,this.runtimeUsageInstructions=this.runtime.getUsageInstructions(),this.enforceIncrementalConsoleTurns=Ed(this.runtimeUsageInstructions);let T=n??t?.namespace??Cs;if(this.agentModuleNamespace=$d(T,{normalize:n===void 0}),new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",Ts,ws]).has(this.agentModuleNamespace))throw new Error(`Agent module namespace "${this.agentModuleNamespace}" is reserved`);let{agents:E,fields:M,functions:$,inputUpdateCallback:B,...V}=o;this.program=new Ee(r,V);let O=this.program.getSignature().getInputFields(),j=vd(a,O,Td);this.contextPromptConfigByField=j.promptConfigByField,this.rlmConfig={contextFields:j.contextFieldNames,sharedFields:o.fields?.shared,runtime:this.runtime,maxSubAgentCalls:u,maxRuntimeChars:c,maxBatchedLlmQueryConcurrency:p,maxTurns:d,trajectoryPruning:m,contextManagement:g,actorFields:h,actorCallback:f,mode:C},this.recursionForwardOptions=x;let{description:F,...W}=I??{},{description:P,...D}=w??{};this.actorDescription=F,this.actorForwardOptions=W,this.responderDescription=P,this.responderForwardOptions=D,this.inputUpdateCallback=A,this.discoveryNamespaces=Ld(o.namespaces,new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",Ts,ws]));let K=this.agents;for(let ie of K??[]){let ce=ie.getFunction().name;this.program.register(ie,ce)}if(t&&(this.func={name:ac(t.name),description:t.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}}),this.program.getSignature().getDescription())throw new Error("AxAgent does not support signature-level descriptions. Use setActorDescription() and/or setResponderDescription() to customize the actor and responder prompts independently.");let N=this.program.getSignature().getOutputFields(),J=h??[];this.actorFieldNames=J;for(let ie of J)if(!N.some(ce=>ce.name===ie))throw new Error(`RLM actorField "${ie}" not found in output signature`);let ye=o.fields?.shared??[];this.sharedFieldNames=ye;for(let ie of ye)if(!O.some(ce=>ce.name===ie))throw new Error(`sharedField "${ie}" not found in signature input fields`);this.excludedSharedFields=o.fields?.excluded??[];let oe=o.fields?.globallyShared??[];this.globalSharedFieldNames=oe;for(let ie of oe)if(!O.some(ce=>ce.name===ie))throw new Error(`globalSharedField "${ie}" not found in signature input fields`);this.localFieldNames=o.fields?.local??[];let re=o.agents?.shared??[],le=o.agents?.globallyShared??[];this.excludedAgents=o.agents?.excluded??[];let we=o.functions?.shared??[],Ue=o.functions?.globallyShared??[];this.excludedAgentFunctions=o.functions?.excluded??[];let se=[...this.agentFunctions,...we,...Ue];for(let ie of se){if(!ie.parameters)throw new Error(`Agent function "${ie.name}" must define parameters schema for agent runtime usage.`);if(ie.examples){for(let[ce,be]of ie.examples.entries())if(!be.code.trim())throw new Error(`Agent function "${ie.name}" example at index ${ce} must define non-empty code`)}}let ve=new Set([Cs,this.agentModuleNamespace,"llmQuery","final","ask_clarification",...this.functionDiscoveryEnabled?[Ts,ws]:[]]);for(let ie of se){let ce=ie.namespace??"utils";if(ve.has(ce))throw new Error(`Agent function namespace "${ce}" is reserved`)}if(ye.length>0&&K){let ie=O.filter(ce=>ye.includes(ce.name));for(let ce of K){if(!(ce instanceof i))continue;let be=new Set(ce.getExcludedSharedFields()),Z=ie.filter(Oe=>!be.has(Oe.name));Z.length!==0&&ce._extendForSharedFields(Z,this.rlmConfig.contextFields)}}if(re.length>0&&K)for(let ie of K)ie instanceof i&&ie._extendForSharedAgents(re);if(oe.length>0&&K){let ie=O.filter(ce=>oe.includes(ce.name));for(let ce of K){if(!(ce instanceof i))continue;let be=new Set(ce.getExcludedSharedFields()),Z=ie.filter(Oe=>!be.has(Oe.name));Z.length!==0&&ce._extendForGlobalSharedFields(Z,this.rlmConfig.contextFields)}}if(le.length>0&&K)for(let ie of K)ie instanceof i&&ie._extendForGlobalSharedAgents(le);if(we.length>0&&K)for(let ie of K)ie instanceof i&&ie._extendForSharedAgentFunctions(we);if(Ue.length>0&&K)for(let ie of K)ie instanceof i&&ie._extendForGlobalSharedAgentFunctions(Ue);this._buildSplitPrograms(),this.program.register(this.actorProgram,"actor"),this.program.register(this.responderProgram,"responder")}_buildSplitPrograms(){let e=this.program.getSignature().getInputFields(),t=this.rlmConfig.contextFields,n=this._getBypassedSharedFieldNames(),r=e.filter(A=>t.includes(A.name)),o=r.filter(A=>this.contextPromptConfigByField.has(A.name)&&!n.has(A.name)).map(A=>({...A,isOptional:!0})),s=e.filter(A=>!t.includes(A.name)&&!n.has(A.name)),a=this.program.getSignature().getOutputFields(),l=a.filter(A=>this.actorFieldNames.includes(A.name)),u=a.filter(A=>!this.actorFieldNames.includes(A.name)),c=xe().addInputFields(s).addInputFields(o).input("contextMetadata",xe.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",xe.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",xe.code("JavaScript code to execute in runtime session"));l.length>0&&(c=c.addOutputFields(l));let p=c.build(),d=xe().addInputFields(s).input("contextData",xe.json("Context data to help synthesize the final answer.")).addOutputFields(u).build(),m=this.rlmConfig.maxSubAgentCalls??tc,g=this.rlmConfig.maxTurns??nc,h=this.agents?.map(A=>{let T=A.getFunction();return{name:T.name,description:T.description,parameters:T.parameters}})??[],f=this.agentFunctions.map(A=>({name:A.name,description:A.description,parameters:A.parameters,returns:A.returns,namespace:A.namespace??"utils"})),C=new Set(f.map(A=>A.namespace??"utils"));h.length>0&&C.add(this.agentModuleNamespace);let x=[...C].sort((A,T)=>A.localeCompare(T)),I=bs(this.actorDescription,r,u,{runtimeUsageInstructions:this.runtimeUsageInstructions,maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:!!this.rlmConfig.contextManagement?.stateInspection,enforceIncrementalConsoleTurns:this.enforceIncrementalConsoleTurns,agentModuleNamespace:this.agentModuleNamespace,discoveryMode:this.functionDiscoveryEnabled,availableModules:x,agents:h,agentFunctions:f}),w=Is(this.responderDescription,r);this.actorProgram=new Ee(p,{...this._genOptions,description:I}),this.responderProgram=new Ee(d,{...this._genOptions,description:w})}_extendForSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters())}_extendForSharedAgents(e){if(e.length===0)return;let t=new Set((this.agents??[]).map(o=>o.getFunction().name)),n=new Set(this.excludedAgents),r=[];for(let o of e){if(o===this)continue;let s=o.getFunction().name;if(!n.has(s)){if(t.has(s)){if(this._parentSharedAgents.has(s))throw new Error(`Duplicate shared agent "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgents.add(s),t.add(s),r.push(o)}}if(r.length!==0){this.agents=[...this.agents??[],...r];for(let o of r){let s=o.getFunction().name;this.program.register(o,s)}this._buildSplitPrograms()}}_extendForGlobalSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let s of e){if(r.some(a=>a.name===s.name)){if(this._parentSharedFields.has(s.name))throw new Error(`Duplicate shared field "${s.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(s.name),n.addInputField(s),o=!0}o&&this.program.setSignature(n);for(let s of e)t.includes(s.name)&&!this.rlmConfig.contextFields.includes(s.name)&&this.rlmConfig.contextFields.push(s.name);for(let s of e)this.sharedFieldNames.includes(s.name)||this.sharedFieldNames.push(s.name);if(this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters()),this.agents)for(let s of this.agents){if(!(s instanceof i))continue;let a=new Set(s.getExcludedSharedFields()),l=e.filter(u=>!a.has(u.name));l.length!==0&&s._extendForGlobalSharedFields(l,t)}}_extendForGlobalSharedAgents(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgents(e);for(let n of t)n._extendForGlobalSharedAgents(e)}_extendForSharedAgentFunctions(e){if(e.length===0)return;let t=new Set(this.agentFunctions.map(o=>`${o.namespace??"utils"}.${o.name}`)),n=new Set(this.excludedAgentFunctions),r=[];for(let o of e){if(n.has(o.name))continue;let s=`${o.namespace??"utils"}.${o.name}`;if(t.has(s)){if(this._parentSharedAgentFunctions.has(s))throw new Error(`Duplicate shared agent function "${s}" \u2014 already propagated from a parent`);continue}this._parentSharedAgentFunctions.add(s),t.add(s),r.push(o)}r.length!==0&&(this.agentFunctions=[...this.agentFunctions,...r],this._buildSplitPrograms())}_extendForGlobalSharedAgentFunctions(e){let t=this.agents?this.agents.filter(n=>n instanceof i):[];this._extendForSharedAgentFunctions(e);for(let n of t)n._extendForGlobalSharedAgentFunctions(e)}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user");this.program.stop(),this.actorProgram.stop(),this.responderProgram.stop()}getId(){return this.program.getId()}setId(e){this.program.setId(e)}namedPrograms(){return this.program.namedPrograms()}getTraces(){return this.program.getTraces()}setDemos(e,t){this.program.setDemos(e,t)}getUsage(){return this.program.getUsage()}resetUsage(){this.program.resetUsage()}getFunction(){if(!this.func)throw new Error("getFunction() requires agentIdentity to be set in the constructor");let e=this.forward.bind(this);return{...this.func,func:async(r,o)=>{let s=this.ai??o?.ai;if(!s)throw new Error("AI service is required to run the agent");let a=await e(s,r,o),u=this.program.getSignature().getOutputFields();return Object.keys(a).map(p=>{let d=u.find(m=>m.name===p);return d?`${d.title}: ${a[p]}`:`${p}: ${a[p]}`}).join(`
824
- `)}}}getExcludedSharedFields(){return this.excludedSharedFields}_getBypassedSharedFieldNames(){let e=new Set([...this.sharedFieldNames,...this.globalSharedFieldNames]);for(let t of this.localFieldNames)e.delete(t);return e}getExcludedAgents(){return this.excludedAgents}getExcludedAgentFunctions(){return this.excludedAgentFunctions}getSignature(){return this.program.getSignature()}setSignature(e){this.program.setSignature(e)}applyOptimization(e){this.program.applyOptimization?.(e)}async _runActorLoop(e,t,n,r){let o=this.rlmConfig,s=this.runtime,a=n?.debug??this.debug??e?.getOptions()?.debug??!1,l;Array.isArray(t)?l=t.filter(v=>v.role==="user").reduce((v,z)=>({...v,...z.values}),{}):l=t;let u={...l},c=new Set(this.program.getSignature().getInputFields().map(v=>v.name)),p=[...this.sharedFieldNames,...this.globalSharedFieldNames],d=this._getBypassedSharedFieldNames(),m={},g={},h={},f={},C="(none)",x=new Set(this.program.getSignature().getInputFields().filter(v=>o.contextFields.includes(v.name)&&v.isOptional).map(v=>v.name)),I=v=>{let z={},Ce={};for(let[ae,Ne]of Object.entries(u))o.contextFields.includes(ae)?z[ae]=Ne:d.has(ae)||(Ce[ae]=Ne);if(v){for(let ae of o.contextFields)if(!x.has(ae)&&(!(ae in z)||z[ae]===void 0))throw new Error(`RLM contextField "${ae}" is missing from input values`)}let ke={};for(let[ae,Ne]of this.contextPromptConfigByField){if(d.has(ae)||!(ae in z))continue;let Fe=Rd(z[ae],Ne);Fe!==void 0&&(ke[ae]=Fe)}g=z,h=Ce,f=ke;for(let ae of Object.keys(m))delete m[ae];for(let ae of p)ae in u&&(m[ae]=u[ae]),ae in g&&(m[ae]=g[ae]);C=Od(g,{promptConfigByField:this.contextPromptConfigByField,inlinedFields:new Set(Object.keys(f))})||"(none)"};I(!1);let w=o.maxSubAgentCalls??tc,A=o.maxRuntimeChars??bd,T=Math.max(1,o.maxBatchedLlmQueryConcurrency??Id),k=o.maxTurns??nc,E=0,M=Math.floor(w*.8),$=this.recursionForwardOptions?.maxDepth??Cd,B=Math.max(0,$),{maxDepth:V,...O}=this.recursionForwardOptions??{},{functions:j,description:F,mem:W,sessionId:P,...D}=n??{},K={...this.recursionForwardOptions??{},maxDepth:Math.max(0,B-1)},N=["context"],J=xe().input("task",xe.string("Task for recursive analysis")).input("context",xe.json("Optional context for the recursive task")).output("answer",xe.string("Answer from recursive analysis")).build(),oe=(o.mode??"simple")==="advanced"&&(K.maxDepth??0)>0?"advanced":"simple",re;B>0&&(oe==="advanced"?re=new i({agentModuleNamespace:this.agentModuleNamespace,signature:J},{debug:a,...o,agents:{local:this.agents},functions:{local:this.agentFunctions,discovery:this.functionDiscoveryEnabled},contextFields:N,actorFields:void 0,recursionOptions:K,actorOptions:this.actorForwardOptions,responderOptions:this.responderForwardOptions}):re=new Ee(J,K));let le=v=>{if(v==null)return"";if(typeof v=="string")return ht(v,A);try{return ht(JSON.stringify(v),A)}catch{return ht(String(v),A)}},we=async(v,z)=>{if(!Array.isArray(v)&&typeof v=="object"&&v!==null&&"query"in v)return we(v.query,v.context??z);if(r?.aborted)throw new Le("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(Array.isArray(v))return Md(v,T,async Ne=>{try{return await we(Ne.query,Ne.context)}catch(Fe){if(Fe instanceof Le)throw Fe;return`[ERROR] ${Fe instanceof Error?Fe.message:String(Fe)}`}});let ae=await(async(Ne,Fe)=>{let Mt=Fe===void 0?void 0:typeof Fe=="string"?ht(Fe,A):Fe;if(E++,E>w)return`[ERROR] Sub-query budget exhausted (${w}/${w}). Use the data you have already accumulated to produce your final answer.`;if(B<=0||!re)return`[ERROR] Recursion depth limit reached (${$}).`;let $e=3,At,Zt=et=>`[ERROR] ${et instanceof Error?et.message:String(et)}`;for(let et=0;et<$e;et++)try{let Ae=await re.forward(e,{task:Ne,...Mt!==void 0?{context:Mt}:oe==="advanced"?{context:""}:{}},{...D,...O,abortSignal:r,debug:a});return le(Ae.answer)}catch(Ae){if(Ae instanceof Le)throw Ae;if(At=Ae,!wd(Ae)||et>=$e-1)return Zt(Ae);let ze=Math.min(6e4,1e3*Math.pow(2,et));await new Promise((Gt,en)=>{let We=!1,Ut,na=()=>{r&&Ut&&r.removeEventListener("abort",Ut)},uc=setTimeout(()=>{We||(We=!0,na(),Gt())},ze);if(r){if(Ut=()=>{We||(We=!0,clearTimeout(uc),na(),en(new Le("rlm-llm-query-retry-backoff",r.reason?String(r.reason):"Aborted during retry backoff")))},r.aborted){Ut();return}r.addEventListener("abort",Ut,{once:!0})}})}return Zt(At)})(v,z);return E===M?`${ae}
825
- [WARNING] ${E}/${w} sub-queries used. Plan to wrap up soon.`:ae},Ue=this.buildRuntimeGlobals(r,m,e),se,ve=(v,z)=>{if(z.length===0)throw new Error(`${v}() requires at least one argument`);se={type:v,args:z}},ie=(...v)=>ve("final",v),ce=(...v)=>ve("ask_clarification",v),be=o.contextManagement,Z={errorPruning:be?.errorPruning??o.trajectoryPruning??!1,hindsightEvaluation:be?.hindsightEvaluation??!1,tombstoning:be?.tombstoning,pruneRank:be?.pruneRank??2,actionReplay:be?.actionReplay??"full",recentFullActions:be?.recentFullActions??1,successSummarization:be?.successSummarization,stateSummary:be?.stateSummary},Oe=[...new Set(this.agentFunctions.map(v=>v.namespace??"utils"))],b={...u},S=new Set(["inputs","llmQuery",Cs,this.agentModuleNamespace,"final","ask_clarification",...Oe,...be?.stateInspection||be?.stateSummary?.enabled?["inspect_runtime"]:[],...Object.keys(Ue)]),R=[...new Set([...Object.keys(b),...c])].filter(v=>!S.has(v)),y={};for(let v of R)y[v]=b[v];let _=()=>{for(let v of Object.keys(b))delete b[v];for(let[v,z]of Object.entries(u))b[v]=z;for(let v of R)y[v]=u[v]},q=[...S],L=[...S,...R],G=be?.stateInspection||be?.stateSummary?.enabled?async()=>{try{let v=Xu(L),z=await ee.execute(v,{signal:r,reservedNames:L});return typeof z=="string"?z:String(z)}catch(v){return`[inspect_runtime error: ${v instanceof Error?v.message:String(v)}]`}}:void 0,U=v=>{let z=Z.stateSummary?.maxEntries&&Z.stateSummary.maxEntries>0?Z.stateSummary.maxEntries:8;return v.split(`
826
- `).map(Ce=>Ce.trim()).filter(Boolean).slice(0,z).join(`
827
- `)},H=async()=>{if(!Z.stateSummary?.enabled||!G)return;let v=await G();return U(v)||"(no user variables)"},Y=()=>s.createSession({...y,inputs:b,llmQuery:we,final:ie,ask_clarification:ce,...G?{inspect_runtime:G}:{},...Ue}),ne="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",ee=Y(),pe=v=>v instanceof Error&&v.message==="Session is closed",fe=v=>v instanceof Error&&v.message==="Execution timed out",Ie=v=>{if(v===void 0)return"(no output)";if(typeof v=="string")return ht(v||"(no output)",A);try{return ht(JSON.stringify(v,null,2),A)}catch{return ht(String(v),A)}},Se=v=>{let z=v,Ce=z?.name??"Error",ke=z?.message??String(v),ae=[`${Ce}: ${ke}`];if(z?.data!==void 0)try{ae.push(`Data: ${JSON.stringify(z.data,null,2)}`)}catch{ae.push(`Data: ${String(z.data)}`)}if(z?.cause!==void 0){let Ne=(Fe,Mt)=>{if(Mt>4)return"[cause chain truncated]";let $e=Fe,At=$e?.name??"Error",Zt=$e?.message??String(Fe),et=[`${At}: ${Zt}`];if($e?.data!==void 0)try{et.push(`Data: ${JSON.stringify($e.data,null,2)}`)}catch{et.push(`Data: ${String($e.data)}`)}return $e?.cause!==void 0&&et.push(`Caused by: ${Ne($e.cause,Mt+1)}`),et.join(`
828
- `)};ae.push(`Caused by: ${Ne(z.cause,1)}`)}return ht(ae.join(`
829
- `),A)},Pe=v=>{let z=sc(v);return/\bfinal\s*\(/.test(z)||/\bask_clarification\s*\(/.test(z)},Q=v=>v&&(typeof v=="object"||typeof v=="function")&&"then"in v&&typeof v.then=="function"?!0:typeof v=="string"&&v.trim()==="[object Promise]",X=async()=>{if(!se)for(let v=0;v<3&&!se;v++)await new Promise(z=>{setTimeout(z,0)})},ue=async v=>{try{let z=await ee.execute(v,{signal:r,reservedNames:q});return Pe(v)&&(se||Q(z))&&(await X(),se)?{output:Ie(void 0),isError:!1}:{output:Ie(z),isError:!1}}catch(z){if(r?.aborted)throw new Le("rlm-session",r.reason??"Aborted");if(z instanceof Error&&(z.name==="AbortError"||z.message.startsWith("Aborted")))throw z;if(fe(z))return{output:ht(`${ne}
830
- ${Se(z)}`,A),isError:!0};if(pe(z))try{ee=Y(),se=void 0;let Ce=await ee.execute(v,{signal:r,reservedNames:q});return{output:ht(`${ne}
831
- ${Ie(Ce)}`,A),isError:!1}}catch(Ce){return{output:ht(`${ne}
832
- ${Se(Ce)}`,A),isError:!0}}return{output:ht(Se(z),A),isError:!0}}},de=async()=>{if(!this.inputUpdateCallback)return;let v=await this.inputUpdateCallback({...u});if(v!==void 0){if(!v||typeof v!="object"||Array.isArray(v))throw new Error("inputUpdateCallback must return an object patch or undefined");for(let[z,Ce]of Object.entries(v))c.has(z)&&(u[z]=Ce)}},Re=v=>{if(typeof v.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required when using inputUpdateCallback");return v},Xe=async()=>{_();let v=async z=>{await Re(z).patchGlobals({inputs:{...b},...y},{signal:r})};try{await v(ee)}catch(z){if(r?.aborted)throw new Le("rlm-session",r.reason??"Aborted");if(z instanceof Error&&(z.name==="AbortError"||z.message.startsWith("Aborted")))throw z;if(pe(z)){ee=Y(),await v(ee);return}throw new Error(`Failed to sync runtime inputs: ${Se(z)}`)}},_e=[],Ze,ft={...this._genOptions,...this.actorForwardOptions,...n,debug:a,abortSignal:r},it={},xt=be?.stateInspection?.contextThreshold;try{for(let v=0;v<k;v++){await de(),I(!0),Ze=await H();let z=Xi(_e,{actionReplay:Z.actionReplay,recentFullActions:Z.recentFullActions,stateSummary:Ze,successSummarization:Z.successSummarization===void 0?Z.actionReplay!=="full":!!Z.successSummarization})||"(no actions yet)";xt&&z.length>xt&&(z+="\n\n[HINT: Action log is large. Call `const state = await inspect_runtime()` for a compact snapshot of current variables instead of re-reading old outputs.]");let Ce=await this.actorProgram.forward(e,{...h,...f,contextMetadata:C,actionLog:z},ft);v===0&&(ft.debugHideSystemPrompt=!0),o.actorCallback&&await o.actorCallback(Ce);for(let $e of this.actorFieldNames)$e in Ce&&(it[$e]=Ce[$e]);let ke=Ce.javascriptCode,ae=ke?.trim();if(!ke||!ae)break;ke=ae;let Ne="";if(this.actorFieldNames.length>0){let $e=this.actorFieldNames.filter(At=>At in Ce).map(At=>`${At}: ${Ce[At]}`).join(`
833
- `);$e&&(Ne=`
933
+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, name);
934
+ if (!descriptor) return [];
935
+ if (
936
+ 'get' in descriptor &&
937
+ typeof descriptor.get === 'function' &&
938
+ !('value' in descriptor)
939
+ ) {
940
+ return [{ name, type: 'accessor', preview: '[getter omitted]' }];
941
+ }
942
+ const value = 'value' in descriptor ? descriptor.value : globalThis[name];
943
+ const meta = describeType(value);
944
+ const size = describeSize(value, meta.type);
945
+ const preview = previewValue(value, meta.type, meta.ctor);
946
+ return [
947
+ {
948
+ name,
949
+ type: meta.type,
950
+ ...(meta.ctor ? { ctor: meta.ctor } : {}),
951
+ ...(size ? { size } : {}),
952
+ ...(preview ? { preview: truncate(preview, 96) } : {}),
953
+ },
954
+ ];
955
+ } catch {
956
+ return [{ name, type: 'unknown', preview: '[unavailable]' }];
957
+ }
958
+ });
959
+ return JSON.stringify({ version: 1, entries });
960
+ })()`}function bc(){return"(() => JSON.stringify(Object.getOwnPropertyNames(globalThis).sort()))()"}function mm(s){let e=s.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function gm(s){let e=new Set(s);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function ia(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=mm(s);if(e.length===0)return"unknown";if(gm(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?ia(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?Cc(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Cc(s,e){if(!s)return"{}";let t=!!s.properties&&Object.keys(s.properties).length>0,n=s.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(s.required??[]),o=e?.respectRequired??!1,i=Object.entries(s.properties).map(([a,u])=>{let l=ia(u),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${l}`});return s?.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function fm(s){return s?ia(s):"unknown"}function Ic(s){let e=Cc(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${fm(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function As(s,e,t,n){let r=e.length>0?e.map(d=>{let m=Rn(d.type),g=d.isOptional?"optional":"required",f=d.description?`: ${d.description}`:"";return`- \`${d.name}\` -> \`inputs.${d.name}\` (${m}, ${g})${f}`}).join(`
961
+ `):"(none)",o=t.map(d=>`\`${d.name}\``).join(", "),i=[...n.agents??[]].sort((d,m)=>d.name.localeCompare(m.name)),a=[...n.agentFunctions??[]].sort((d,m)=>d.namespace!==m.namespace?d.namespace.localeCompare(m.namespace):d.name.localeCompare(m.name)),u=n.agentModuleNamespace??"agents",l=!!n.discoveryMode,c=n.availableModules?[...n.availableModules].sort((d,m)=>d.namespace.localeCompare(m.namespace)):[...new Set([...a.map(d=>d.namespace),...i.length>0?[u]:[]])].sort((d,m)=>d.localeCompare(m)).map(d=>({namespace:d})),p=Ze("rlm/actor.md",{contextVarList:r,responderOutputFieldTitles:o,discoveryMode:l,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:!l&&i.length>0,agentModuleNamespace:u,agentFunctionsList:i.map(d=>Ic({qualifiedName:`${u}.${d.name}`,parameters:d.parameters})).join(`
962
+ `),hasFunctions:!l&&a.length>0,functionsList:a.map(d=>Ic({qualifiedName:`${d.namespace}.${d.name}`,parameters:d.parameters,returns:d.returns})).join(`
963
+ `),hasModules:l&&c.length>0,modulesList:c.map(d=>d.selectionCriteria?.trim()?`- \`${d.namespace}\` - ${d.selectionCriteria.trim()}`:`- \`${d.namespace}\``).join(`
964
+ `),runtimeUsageInstructions:String(n.runtimeUsageInstructions),enforceIncrementalConsoleTurns:!!n.enforceIncrementalConsoleTurns,hasLiveRuntimeState:!!n.hasLiveRuntimeState,hasCompressedActionReplay:!!n.hasCompressedActionReplay}).replace(/\n{3,}/g,`
965
+
966
+ `).trim();return s?`${p}
967
+
968
+ ${s}`:p}function ys(s,e){let t=e.length>0?e.map(r=>{let o=Rn(r.type),i=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${i})`}).join(`
969
+ `):"(none)",n=Ze("rlm/responder.md",{contextVarSummary:t}).trim();return s?`${n}
970
+
971
+ ${s}`:n}var Tc=50,hm=5e3,xm=8,wc=10,Am=2,ym=1200,bs="agents",bm=2,aa="listModuleFunctions",ua="getFunctionDefinitions",Im="AI service is required to use llmQuery(...) in AxAgent.test(). Pass options.ai or configure ai on the agent.",Ts="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",Kn=class extends Error{constructor(t){super(`AxAgent protocol completion: ${t}`);this.type=t;this.name="AxAgentProtocolCompletionSignal"}};function Rc(s){return{finalFunction:(...r)=>{s("final",r)},askClarificationFunction:(...r)=>{s("ask_clarification",r)},protocol:{final:(...r)=>{throw s("final",r),new Kn("final")},askClarification:(...r)=>{throw s("ask_clarification",r),new Kn("ask_clarification")}}}}function Cm(s,e,t){return{model:s?.model,modelConfig:s?.modelConfig,debug:e,verbose:s?.verbose,rateLimiter:s?.rateLimiter,fetch:s?.fetch,tracer:s?.tracer,meter:s?.meter,timeout:s?.timeout,excludeContentFromTrace:s?.excludeContentFromTrace,abortSignal:t,logger:s?.logger,sessionId:s?.sessionId,debugHideSystemPrompt:s?.debugHideSystemPrompt,traceContext:s?.traceContext,thinkingTokenBudget:s?.thinkingTokenBudget,showThoughts:s?.showThoughts,useExpensiveModel:s?.useExpensiveModel,corsProxy:s?.corsProxy,retry:s?.retry,contextCache:s?.contextCache,examplesInSystem:s?.examplesInSystem,customLabels:s?.customLabels}}function kc(s){let e=s?.preset??"full",t=Tm(e),n=s?.expert?.rankPruning,r=n?.enabled??(n?.minRank!==void 0?!0:t.hindsight),o=s?.state?.summary??t.stateSummary,i=s?.state?.inspect??t.inspect,a=s?.checkpoints?.enabled??t.checkpointsEnabled;if(a&&!o&&!i)throw new Error("contextPolicy.checkpoints requires either state.summary or state.inspect to be enabled");return{preset:e,summarizerOptions:s?.summarizerOptions,pruneUsedDocs:s?.pruneUsedDocs??t.pruneUsedDocs,actionReplay:s?.expert?.replay??t.actionReplay,recentFullActions:Math.max(s?.expert?.recentFullActions??t.recentFullActions,0),errorPruning:s?.expert?.pruneErrors??t.errorPruning,hindsightEvaluation:r,pruneRank:n?.minRank??t.pruneRank,rankPruneGraceTurns:bm,tombstoning:s?.expert?.tombstones,stateSummary:{enabled:o,maxEntries:s?.state?.maxEntries??t.maxEntries,maxChars:s?.state?.maxChars??t.maxStateChars},stateInspection:{enabled:i,contextThreshold:s?.state?.inspectThresholdChars??t.inspectThreshold},checkpoints:{enabled:a,triggerChars:s?.checkpoints?.triggerChars??t.checkpointTriggerChars}}}function Tm(s){switch(s){case"adaptive":return{actionReplay:"adaptive",recentFullActions:2,errorPruning:!0,hindsight:!1,pruneRank:2,pruneUsedDocs:!0,stateSummary:!0,inspect:!0,inspectThreshold:8e3,maxEntries:6,maxStateChars:1200,checkpointsEnabled:!0,checkpointTriggerChars:12e3};case"lean":return{actionReplay:"minimal",recentFullActions:1,errorPruning:!0,hindsight:!1,pruneRank:2,pruneUsedDocs:!0,stateSummary:!0,inspect:!0,inspectThreshold:6e3,maxEntries:4,maxStateChars:800,checkpointsEnabled:!0,checkpointTriggerChars:9e3};default:return{actionReplay:"full",recentFullActions:1,errorPruning:!1,hindsight:!1,pruneRank:2,pruneUsedDocs:!1,stateSummary:!1,inspect:!1,inspectThreshold:void 0,maxEntries:void 0,maxStateChars:void 0,checkpointsEnabled:!1,checkpointTriggerChars:void 0}}}var Dr=class s{ai;program;actorProgram;responderProgram;agents;agentFunctions;agentFunctionModuleMetadata=new Map;debug;options;rlmConfig;runtime;actorFieldNames;localFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;responderDescription;recursionForwardOptions;actorForwardOptions;responderForwardOptions;inputUpdateCallback;contextPromptConfigByField=new Map;agentModuleNamespace=bs;functionDiscoveryEnabled=!1;runtimeUsageInstructions="";enforceIncrementalConsoleTurns=!1;activeAbortControllers=new Set;_stopRequested=!1;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;_reservedAgentFunctionNamespaces(){return new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",bs,this.agentModuleNamespace,...this.functionDiscoveryEnabled?[aa,ua]:[]])}_mergeAgentFunctionModuleMetadata(e){let t=!1;for(let n of e){let r=this.agentFunctionModuleMetadata.get(n.namespace);if(!r){this.agentFunctionModuleMetadata.set(n.namespace,n),t=!0;continue}if(r.title!==n.title||r.selectionCriteria!==n.selectionCriteria||r.description!==n.description)throw new Error(`Conflicting agent function group metadata for namespace "${n.namespace}"`)}return t}_validateConfiguredSignature(e){if(e.getDescription())throw new Error("AxAgent does not support signature-level descriptions. Use setActorDescription() and/or setResponderDescription() to customize the actor and responder prompts independently.");let t=new Set(e.getInputFields().map(r=>r.name)),n=new Set(e.getOutputFields().map(r=>r.name));for(let r of this.rlmConfig.contextFields)if(!t.has(r))throw new Error(`RLM contextField "${r}" not found in signature`);for(let r of this.sharedFieldNames)if(!t.has(r))throw new Error(`sharedField "${r}" not found in signature input fields`);for(let r of this.globalSharedFieldNames)if(!t.has(r))throw new Error(`globalSharedField "${r}" not found in signature input fields`);for(let r of this.actorFieldNames)if(!n.has(r))throw new Error(`RLM actorField "${r}" not found in output signature`)}_validateAgentFunctionNamespaces(e){let t=this._reservedAgentFunctionNamespaces();for(let n of e){let r=n.namespace??"utils";if(t.has(r))throw new Error(`Agent function namespace "${r}" conflicts with an AxAgent runtime global and is reserved`)}}constructor({ai:e,agentIdentity:t,agentModuleNamespace:n,signature:r},o){let{debug:i,contextFields:a,runtime:u,maxSubAgentCalls:l,maxRuntimeChars:c,maxBatchedLlmQueryConcurrency:p,maxTurns:d,contextPolicy:m,actorFields:g,actorCallback:f,mode:x,recursionOptions:y,actorOptions:h,responderOptions:A,inputUpdateCallback:b}=o;this.ai=e,this.agents=o.agents?.local,this.functionDiscoveryEnabled=o.functions?.discovery??!1,this.debug=i,this.options=o,this.runtime=u??new Ut,this.runtimeUsageInstructions=this.runtime.getUsageInstructions(),this.enforceIncrementalConsoleTurns=Um(this.runtimeUsageInstructions);let I=n??t?.namespace??bs;if(this.agentModuleNamespace=Hm(I,{normalize:n===void 0}),new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",aa,ua]).has(this.agentModuleNamespace))throw new Error(`Agent module namespace "${this.agentModuleNamespace}" is reserved`);let R=this._reservedAgentFunctionNamespaces(),k=la(o.functions?.local,R),v=la(o.functions?.shared,R),O=la(o.functions?.globallyShared,R);this.agentFunctions=k.functions,this._mergeAgentFunctionModuleMetadata(k.moduleMetadata);let{agents:P,fields:$,functions:E,inputUpdateCallback:F,..._}=o;this.program=new Ie(r,_);let j=this.program.getSignature().getInputFields(),H=$m(a,j,ym);this.contextPromptConfigByField=H.promptConfigByField,this.rlmConfig={contextFields:H.contextFieldNames,sharedFields:o.fields?.shared,runtime:this.runtime,maxSubAgentCalls:l,maxRuntimeChars:c,maxBatchedLlmQueryConcurrency:p,maxTurns:d,contextPolicy:m,actorFields:g,actorCallback:f,mode:x},this.recursionForwardOptions=y;let{description:L,...X}=h??{},{description:U,...se}=A??{};this.actorDescription=L,this.actorForwardOptions=X,this.responderDescription=U,this.responderForwardOptions=se,this.inputUpdateCallback=b;let be=this.agents;for(let ie of be??[]){let pe=ie.getFunction().name;this.program.register(ie,pe)}t&&(this.func={name:$c(t.name),description:t.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}});let G=g??[];this.actorFieldNames=G;let Z=o.fields?.shared??[];this.sharedFieldNames=Z,this.excludedSharedFields=o.fields?.excluded??[];let K=o.fields?.globallyShared??[];this.globalSharedFieldNames=K,this.localFieldNames=o.fields?.local??[];let Y=o.agents?.shared??[],me=o.agents?.globallyShared??[];this.excludedAgents=o.agents?.excluded??[];let V=v.functions,le=O.functions;this.excludedAgentFunctions=o.functions?.excluded??[];let Te=[...this.agentFunctions,...V,...le];for(let ie of Te){if(!ie.parameters)throw new Error(`Agent function "${ie.name}" must define parameters schema for agent runtime usage.`);if(ie.examples){for(let[pe,ne]of ie.examples.entries())if(!ne.code.trim())throw new Error(`Agent function "${ie.name}" example at index ${pe} must define non-empty code`)}}if(this._validateConfiguredSignature(this.program.getSignature()),this._validateAgentFunctionNamespaces(Te),Z.length>0&&be){let ie=j.filter(pe=>Z.includes(pe.name));for(let pe of be){if(!(pe instanceof s))continue;let ne=new Set(pe.getExcludedSharedFields()),he=ie.filter(C=>!ne.has(C.name));he.length!==0&&pe._extendForSharedFields(he,this.rlmConfig.contextFields)}}if(Y.length>0&&be)for(let ie of be)ie instanceof s&&ie._extendForSharedAgents(Y);if(K.length>0&&be){let ie=j.filter(pe=>K.includes(pe.name));for(let pe of be){if(!(pe instanceof s))continue;let ne=new Set(pe.getExcludedSharedFields()),he=ie.filter(C=>!ne.has(C.name));he.length!==0&&pe._extendForGlobalSharedFields(he,this.rlmConfig.contextFields)}}if(me.length>0&&be)for(let ie of be)ie instanceof s&&ie._extendForGlobalSharedAgents(me);if(V.length>0&&be)for(let ie of be)ie instanceof s&&ie._extendForSharedAgentFunctions(v);if(le.length>0&&be)for(let ie of be)ie instanceof s&&ie._extendForGlobalSharedAgentFunctions(O);this._buildSplitPrograms(),this.program.register(this.actorProgram,"actor"),this.program.register(this.responderProgram,"responder")}_buildSplitPrograms(){let e=this.program.getSignature().getInputFields(),t=this.rlmConfig.contextFields,n=this._getBypassedSharedFieldNames(),r=e.filter(T=>t.includes(T.name)),o=r.filter(T=>this.contextPromptConfigByField.has(T.name)&&!n.has(T.name)).map(T=>({...T,isOptional:!0})),i=e.filter(T=>!t.includes(T.name)&&!n.has(T.name)),a=this.program.getSignature().getOutputFields(),u=a.filter(T=>this.actorFieldNames.includes(T.name)),l=a.filter(T=>!this.actorFieldNames.includes(T.name)),c=ue().addInputFields(i).addInputFields(o).input("contextMetadata",ue.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",ue.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",ue.code("JavaScript code to execute in runtime session"));u.length>0&&(c=c.addOutputFields(u));let p=c.build(),d=ue().addInputFields(i).input("contextData",ue.json("Context data to help synthesize the final answer.")).addOutputFields(l).build(),m=this.rlmConfig.maxSubAgentCalls??Tc,g=this.rlmConfig.maxTurns??wc,f=this.agents?.map(T=>{let R=T.getFunction();return{name:R.name,description:R.description,parameters:R.parameters}})??[],x=this.agentFunctions.map(T=>({name:T.name,description:T.description,parameters:T.parameters,returns:T.returns,namespace:T.namespace??"utils"})),y=new Set(x.map(T=>T.namespace??"utils"));f.length>0&&y.add(this.agentModuleNamespace);let h=[...y].sort((T,R)=>T.localeCompare(R)).map(T=>({namespace:T,selectionCriteria:this.agentFunctionModuleMetadata.get(T)?.selectionCriteria})),A=kc(this.rlmConfig.contextPolicy),b=As(this.actorDescription,r,l,{runtimeUsageInstructions:this.runtimeUsageInstructions,maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:A.stateInspection.enabled,hasLiveRuntimeState:A.stateSummary.enabled,hasCompressedActionReplay:A.actionReplay!=="full"||A.checkpoints.enabled||A.errorPruning||!!A.tombstoning||this.functionDiscoveryEnabled&&A.pruneUsedDocs,enforceIncrementalConsoleTurns:this.enforceIncrementalConsoleTurns,agentModuleNamespace:this.agentModuleNamespace,discoveryMode:this.functionDiscoveryEnabled,availableModules:h,agents:f,agentFunctions:x}),I=ys(this.responderDescription,r);this.actorProgram?(this.actorProgram.setSignature(p),this.actorProgram.setDescription(b)):this.actorProgram=new Ie(p,{...this._genOptions,description:b}),this.responderProgram?(this.responderProgram.setSignature(d),this.responderProgram.setDescription(I)):this.responderProgram=new Ie(d,{...this._genOptions,description:I})}_extendForSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let i of e){if(r.some(a=>a.name===i.name)){if(this._parentSharedFields.has(i.name))throw new Error(`Duplicate shared field "${i.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(i.name),n.addInputField(i),o=!0}o&&this.program.setSignature(n);for(let i of e)t.includes(i.name)&&!this.rlmConfig.contextFields.includes(i.name)&&this.rlmConfig.contextFields.push(i.name);this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters())}_extendForSharedAgents(e){if(e.length===0)return;let t=new Set((this.agents??[]).map(o=>o.getFunction().name)),n=new Set(this.excludedAgents),r=[];for(let o of e){if(o===this)continue;let i=o.getFunction().name;if(!n.has(i)){if(t.has(i)){if(this._parentSharedAgents.has(i))throw new Error(`Duplicate shared agent "${i}" \u2014 already propagated from a parent`);continue}this._parentSharedAgents.add(i),t.add(i),r.push(o)}}if(r.length!==0){this.agents=[...this.agents??[],...r];for(let o of r){let i=o.getFunction().name;this.program.register(o,i)}this._buildSplitPrograms()}}_extendForGlobalSharedFields(e,t){let n=this.program.getSignature(),r=n.getInputFields(),o=!1;for(let i of e){if(r.some(a=>a.name===i.name)){if(this._parentSharedFields.has(i.name))throw new Error(`Duplicate shared field "${i.name}" \u2014 already propagated from a parent`);continue}this._parentSharedFields.add(i.name),n.addInputField(i),o=!0}o&&this.program.setSignature(n);for(let i of e)t.includes(i.name)&&!this.rlmConfig.contextFields.includes(i.name)&&this.rlmConfig.contextFields.push(i.name);for(let i of e)this.sharedFieldNames.includes(i.name)||this.sharedFieldNames.push(i.name);if(this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters()),this.agents)for(let i of this.agents){if(!(i instanceof s))continue;let a=new Set(i.getExcludedSharedFields()),u=e.filter(l=>!a.has(l.name));u.length!==0&&i._extendForGlobalSharedFields(u,t)}}_extendForGlobalSharedAgents(e){let t=this.agents?this.agents.filter(n=>n instanceof s):[];this._extendForSharedAgents(e);for(let n of t)n._extendForGlobalSharedAgents(e)}_extendForSharedAgentFunctions(e){if(e.functions.length===0&&e.moduleMetadata.length===0)return;let t=new Set(this.agentFunctions.map(i=>`${i.namespace??"utils"}.${i.name}`)),n=new Set(this.excludedAgentFunctions),r=[],o=this._mergeAgentFunctionModuleMetadata(e.moduleMetadata);for(let i of e.functions){if(n.has(i.name))continue;let a=`${i.namespace??"utils"}.${i.name}`;if(t.has(a)){if(this._parentSharedAgentFunctions.has(a))throw new Error(`Duplicate shared agent function "${a}" \u2014 already propagated from a parent`);continue}this._parentSharedAgentFunctions.add(a),t.add(a),r.push(i)}r.length===0&&!o||(r.length>0&&(this.agentFunctions=[...this.agentFunctions,...r]),this._buildSplitPrograms())}_extendForGlobalSharedAgentFunctions(e){let t=this.agents?this.agents.filter(n=>n instanceof s):[];this._extendForSharedAgentFunctions(e);for(let n of t)n._extendForGlobalSharedAgentFunctions(e)}stop(){this._stopRequested=!0;for(let e of this.activeAbortControllers)e.abort("Stopped by user");this.program.stop(),this.actorProgram.stop(),this.responderProgram.stop()}getId(){return this.program.getId()}setId(e){this.program.setId(e)}namedPrograms(){return this.program.namedPrograms()}namedProgramInstances(){return this.program.namedProgramInstances()}getTraces(){return this.program.getTraces()}setDemos(e,t){this.program.setDemos(e,t)}getUsage(){return this.program.getUsage()}resetUsage(){this.program.resetUsage()}getFunction(){if(!this.func)throw new Error("getFunction() requires agentIdentity to be set in the constructor");let e=this.forward.bind(this);return{...this.func,func:async(r,o)=>{let i=this.ai??o?.ai;if(!i)throw new Error("AI service is required to run the agent");let a=await e(i,r,o),l=this.program.getSignature().getOutputFields();return Object.keys(a).map(p=>{let d=l.find(m=>m.name===p);return d?`${d.title}: ${a[p]}`:`${p}: ${a[p]}`}).join(`
972
+ `)}}}getExcludedSharedFields(){return this.excludedSharedFields}_getBypassedSharedFieldNames(){let e=new Set([...this.sharedFieldNames,...this.globalSharedFieldNames]);for(let t of this.localFieldNames)e.delete(t);return e}_createRuntimeInputState(e,t){let n;Array.isArray(e)?n=e.filter(x=>x.role==="user").reduce((x,y)=>({...x,...y.values}),{}):n=e;let r=t?.allowedFieldNames?new Set(t.allowedFieldNames):void 0;if(r&&t?.validateInputKeys){for(let x of Object.keys(n))if(!r.has(x))throw new Error(`AxAgent.test() only accepts context field values. "${x}" is not configured in contextFields.`)}let o={...n},i=r?new Set(r):new Set(this.program.getSignature().getInputFields().map(x=>x.name)),a=[...this.sharedFieldNames,...this.globalSharedFieldNames],u=this._getBypassedSharedFieldNames(),l={},c={},p={},d={},m="(none)",g=new Set(this.program.getSignature().getInputFields().filter(x=>this.rlmConfig.contextFields.includes(x.name)&&x.isOptional).map(x=>x.name));return{currentInputs:o,signatureInputFieldNames:i,sharedFieldValues:l,recomputeTurnInputs:x=>{let y={},h={};for(let[b,I]of Object.entries(o))this.rlmConfig.contextFields.includes(b)?y[b]=I:u.has(b)||(h[b]=I);if(x){for(let b of this.rlmConfig.contextFields)if(!g.has(b)&&(!(b in y)||y[b]===void 0))throw new Error(`RLM contextField "${b}" is missing from input values`)}let A={};for(let[b,I]of this.contextPromptConfigByField){if(u.has(b)||!(b in y))continue;let T=_m(y[b],I);T!==void 0&&(A[b]=T)}c=y,p=h,d=A;for(let b of Object.keys(l))delete l[b];for(let b of a)b in o&&(l[b]=o[b]),b in c&&(l[b]=c[b]);m=Dm(c,{promptConfigByField:this.contextPromptConfigByField,inlinedFields:new Set(Object.keys(d))})||"(none)"},getNonContextValues:()=>p,getActorInlineContextValues:()=>d,getContextMetadata:()=>m}}_createRuntimeExecutionContext({ai:e,inputState:t,options:n,effectiveAbortSignal:r,debug:o,completionState:i,completionBindings:a,actionLogEntries:u}){let l=this.rlmConfig,c=this.runtime,p=l.maxSubAgentCalls??Tc,d=l.maxRuntimeChars??hm,m=Math.max(1,l.maxBatchedLlmQueryConcurrency??xm),g=this.recursionForwardOptions?.maxDepth??Am,f=Math.max(0,g),x=kc(l.contextPolicy),y=0,h=Math.floor(p*.8),{maxDepth:A,...b}=this.recursionForwardOptions??{},{description:I,mem:T,sessionId:R,...k}=n??{},v={...this.recursionForwardOptions??{},maxDepth:Math.max(0,f-1)},O=["context"],P=ue().input("task",ue.string("Task for recursive analysis")).input("context",ue.json("Optional context for the recursive task")).output("answer",ue.string("Answer from recursive analysis")).build(),E=(l.mode??"simple")==="advanced"&&(v.maxDepth??0)>0?"advanced":"simple",F;f>0&&(E==="advanced"?F=new s({agentModuleNamespace:this.agentModuleNamespace,signature:P},{debug:o,...l,agents:{local:this.agents},functions:{local:this.agentFunctions,discovery:this.functionDiscoveryEnabled},contextFields:O,actorFields:void 0,recursionOptions:v,actorOptions:this.actorForwardOptions,responderOptions:this.responderForwardOptions}):F=new Ie(P,v));let _=async(N,D)=>{if(!Array.isArray(N)&&typeof N=="object"&&N!==null&&"query"in N)return _(N.query,N.context??D);if(r?.aborted)throw new ke("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(Array.isArray(N))return Gm(N,m,async ee=>{try{return await _(ee.query,ee.context)}catch(fe){if(fe instanceof ke)throw fe;return`[ERROR] ${fe instanceof Error?fe.message:String(fe)}`}});if(!e)throw new Error(Im);let q=N,te=ee=>{if(ee==null)return"";if(typeof ee=="string")return tt(ee,d);try{return tt(JSON.stringify(ee),d)}catch{return tt(String(ee),d)}},W=await(async(ee,fe)=>{let we=fe===void 0?void 0:typeof fe=="string"?tt(fe,d):fe;if(y++,y>p)return`[ERROR] Sub-query budget exhausted (${p}/${p}). Use the data you have already accumulated to produce your final answer.`;if(f<=0||!F)return`[ERROR] Recursion depth limit reached (${g}).`;let ve=3,nt,Ee=ge=>`[ERROR] ${ge instanceof Error?ge.message:String(ge)}`;for(let ge=0;ge<ve;ge++)try{let z=await F.forward(e,{task:ee,...we!==void 0?{context:we}:E==="advanced"?{context:""}:{}},{...k,...b,abortSignal:r,debug:o});return te(z.answer)}catch(z){if(z instanceof ke)throw z;if(nt=z,!wm(z)||ge>=ve-1)return Ee(z);let Q=Math.min(6e4,1e3*Math.pow(2,ge));await new Promise((de,Me)=>{let xe=!1,Pe,gt=()=>{r&&Pe&&r.removeEventListener("abort",Pe)},Fe=setTimeout(()=>{xe||(xe=!0,gt(),de())},Q);if(r){if(Pe=()=>{xe||(xe=!0,clearTimeout(Fe),gt(),Me(new ke("rlm-llm-query-retry-backoff",r.reason?String(r.reason):"Aborted during retry backoff")))},r.aborted){Pe();return}r.addEventListener("abort",Pe,{once:!0})}})}return Ee(nt)})(q,D);return y===h?`${W}
973
+ [WARNING] ${y}/${p} sub-queries used. Plan to wrap up soon.`:W},j=this.buildRuntimeGlobals(r,t.sharedFieldValues,e,a.protocol),H=[...new Set(this.agentFunctions.map(N=>N.namespace??"utils"))],L={...t.currentInputs},X=new Set(["inputs","llmQuery",bs,this.agentModuleNamespace,"final","ask_clarification",...H,...x.stateInspection.enabled?["inspect_runtime"]:[],...Object.keys(j)]),U=[...new Set([...Object.keys(L),...t.signatureInputFieldNames])].filter(N=>!X.has(N)),se={};for(let N of U)se[N]=L[N];let be=()=>{for(let N of Object.keys(L))delete L[N];for(let[N,D]of Object.entries(t.currentInputs))L[N]=D;for(let N of U)se[N]=t.currentInputs[N]},G=[...X],Z=[...X,...U],K=u??[],Y,me,V=async()=>{try{let N=await Y.execute(bc(),{signal:r,reservedNames:Z});if(typeof N!="string")return[];let D=JSON.parse(N);return Array.isArray(D)?D.filter(q=>typeof q=="string"):[]}catch{return[]}},le=async()=>(me||(me=await V()),me),Te=async()=>{try{let N=await le(),D=yc(Z,N),q=await Y.execute(D,{signal:r,reservedNames:Z});return typeof q=="string"?q:String(q)}catch(N){return`[inspect_runtime error: ${N instanceof Error?N.message:String(N)}]`}},ie=(N,D)=>{let q=Sm(N);if(!q)return vm(N,D);let te=dc(K);return Mm(q,te,D)},pe=x.stateInspection.enabled?async()=>ie(await Te()):void 0,ne=()=>(me=void 0,c.createSession({...se,inputs:L,llmQuery:_,final:a.finalFunction,ask_clarification:a.askClarificationFunction,...pe?{inspect_runtime:pe}:{},...j}));Y=ne();let he=async()=>{if(!i.payload)for(let N=0;N<3&&!i.payload;N++)await new Promise(D=>{setTimeout(D,0)})},C=async()=>{if(!x.stateSummary.enabled)return;let N=await Te();return ie(N,{maxEntries:x.stateSummary.maxEntries&&x.stateSummary.maxEntries>0?x.stateSummary.maxEntries:8,maxChars:x.stateSummary.maxChars&&x.stateSummary.maxChars>0?x.stateSummary.maxChars:void 0})||"(no user variables)"},S=N=>{if(typeof N.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required when using inputUpdateCallback");return N};return{effectiveContextConfig:x,captureRuntimeStateSummary:C,syncRuntimeInputsToSession:async()=>{be();let N=async D=>{await S(D).patchGlobals({inputs:{...L},...se},{signal:r})};try{await N(Y)}catch(D){if(r?.aborted)throw new ke("rlm-session",r.reason??"Aborted");if(D instanceof Error&&(D.name==="AbortError"||D.message.startsWith("Aborted")))throw D;if(Ec(D)){Y=ne(),await N(Y);return}throw new Error(`Failed to sync runtime inputs: ${Cs(D,d)}`)}},executeActorCode:async N=>{let D={output:Is(void 0,d),isError:!1};try{let q=await Y.execute(N,{signal:r,reservedNames:G});return i.payload||vc(N)&&Oc(q)&&(await he(),i.payload)?D:{output:Is(q,d),isError:!1}}catch(q){if(q instanceof Kn||i.payload)return D;if(r?.aborted)throw new ke("rlm-session",r.reason??"Aborted");if(q instanceof Error&&(q.name==="AbortError"||q.message.startsWith("Aborted")))throw q;if(Pm(q))return{output:tt(`${Ts}
974
+ ${Cs(q,d)}`,d),isError:!0};if(Ec(q))try{Y=ne(),i.payload=void 0;let te=await Y.execute(N,{signal:r,reservedNames:G});return{output:tt(`${Ts}
975
+ ${Is(te,d)}`,d),isError:!1}}catch(te){return{output:tt(`${Ts}
976
+ ${Cs(te,d)}`,d),isError:!0}}return{output:tt(Cs(q,d),d),isError:!0}}},executeTestCode:async N=>{try{let D=await Y.execute(N,{signal:r,reservedNames:G});if(vc(N)&&Oc(D)&&await he(),i.payload)return i.payload;let q=Is(D,d);if(Fm(q))throw new Error(q);return q}catch(D){if((D instanceof Kn||i.payload)&&i.payload)return i.payload;throw D}},close:()=>{Y.close()}}}getExcludedAgents(){return this.excludedAgents}getExcludedAgentFunctions(){return this.excludedAgentFunctions}getSignature(){return this.program.getSignature()}async test(e,t,n){let r=this.ai??n?.ai,o=n?.debug??this.debug??r?.getOptions()?.debug??!1,i=this._createRuntimeInputState(t??{},{allowedFieldNames:this.rlmConfig.contextFields,validateInputKeys:!0});i.recomputeTurnInputs(!1);let a={payload:void 0},u=Rc((c,p)=>{if(p.length===0)throw new Error(`${c}() requires at least one argument`);a.payload={type:c,args:p}}),l=this._createRuntimeExecutionContext({ai:r,inputState:i,options:void 0,effectiveAbortSignal:n?.abortSignal,debug:o,completionState:a,completionBindings:u,actionLogEntries:[]});try{return await l.executeTestCode(e)}finally{l.close()}}setSignature(e){let t=new Re(e);this._validateConfiguredSignature(t);let n=this.program.getSignature();try{this.program.setSignature(t),this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters())}catch(r){throw this.program.setSignature(n),this._buildSplitPrograms(),this.func&&(this.func.parameters=this._buildFuncParameters()),r}}applyOptimization(e){this.program.applyOptimization?.(e)}async _runActorLoop(e,t,n,r){let o=this.rlmConfig,i=n?.debug??this.debug??e?.getOptions()?.debug??!1,a=o.maxTurns??wc,u=this._createRuntimeInputState(t);u.recomputeTurnInputs(!1);let l={payload:void 0},c=Rc((v,O)=>{if(O.length===0)throw new Error(`${v}() requires at least one argument`);l.payload={type:v,args:O}}),p=[],d,m=this._createRuntimeExecutionContext({ai:e,inputState:u,options:n,effectiveAbortSignal:r,debug:i,completionState:l,completionBindings:c,actionLogEntries:p}),g=async()=>{if(!this.inputUpdateCallback)return;let v=await this.inputUpdateCallback({...u.currentInputs});if(v!==void 0){if(!v||typeof v!="object"||Array.isArray(v))throw new Error("inputUpdateCallback must return an object patch or undefined");for(let[O,P]of Object.entries(v))u.signatureInputFieldNames.has(O)&&(u.currentInputs[O]=P)}},f={...this._genOptions,...this.actorForwardOptions,...n,debug:i,abortSignal:r},x={},y=m.effectiveContextConfig.stateInspection.enabled?m.effectiveContextConfig.stateInspection.contextThreshold:void 0,h=Cm(n,i,r),A=this.functionDiscoveryEnabled&&m.effectiveContextConfig.pruneUsedDocs,b,I=()=>hs(p,{pruneUsedDocs:A}),T=()=>xc(I(),{actionReplay:m.effectiveContextConfig.actionReplay,recentFullActions:m.effectiveContextConfig.recentFullActions,stateSummary:d,checkpointSummary:b?.summary,checkpointTurns:b?.turns})||"(no actions yet)",R=async()=>{if(!m.effectiveContextConfig.checkpoints.enabled){b=void 0;return}let v=xs(p,{actionReplay:m.effectiveContextConfig.actionReplay,recentFullActions:m.effectiveContextConfig.recentFullActions,pruneUsedDocs:A}),O=m.effectiveContextConfig.checkpoints.triggerChars;if(!O||v.historyChars<=O){b=void 0;return}let P=v.checkpointEntries;if(P.length===0){b=void 0;return}let $=JSON.stringify(P.map(E=>({turn:E.turn,code:E.code,output:E.output,actorFieldsOutput:E.actorFieldsOutput,tags:E.tags,tombstone:E.tombstone})));b?.fingerprint!==$&&(b={fingerprint:$,turns:P.map(E=>E.turn),summary:await hc(e,m.effectiveContextConfig.summarizerOptions,h,P)})};try{for(let v=0;v<a;v++){await g(),u.recomputeTurnInputs(!0),d=await m.captureRuntimeStateSummary(),await R();let O=T(),P=xs(p,{actionReplay:m.effectiveContextConfig.actionReplay,recentFullActions:m.effectiveContextConfig.recentFullActions,pruneUsedDocs:A,checkpointTurns:b?.turns});y&&P.historyChars>y&&(O+="\n\n[HINT: Action log is large. Call `const state = await inspect_runtime()` for a compact snapshot of current variables instead of re-reading old outputs.]");let $=await this.actorProgram.forward(e,{...u.getNonContextValues(),...u.getActorInlineContextValues(),contextMetadata:u.getContextMetadata(),actionLog:O},f);v===0&&(f.debugHideSystemPrompt=!0),o.actorCallback&&await o.actorCallback($);for(let L of this.actorFieldNames)L in $&&(x[L]=$[L]);let E=$.javascriptCode,F=E?.trim();if(!E||!F)break;E=F;let _="";if(this.actorFieldNames.length>0){let L=this.actorFieldNames.filter(X=>X in $).map(X=>`${X}: ${$[X]}`).join(`
977
+ `);L&&(_=`
834
978
  Actor fields:
835
- ${$e}`)}if(se=void 0,this.enforceIncrementalConsoleTurns){let $e=Pd(ke);if($e){_e.push({turn:v+1,code:ke,output:$e,actorFieldsOutput:Ne,tags:["error"]}),await Yi(_e,_e.length-1,Z,e),Ze=await H();continue}}this.inputUpdateCallback&&await Xe();let{output:Fe,isError:Mt}=await ue(ke);if(_e.push({turn:v+1,code:ke,output:Fe,actorFieldsOutput:Ne,tags:Mt?["error"]:[]}),await Yi(_e,_e.length-1,Z,e),Ze=await H(),se)break}}finally{try{ee.close()}catch{}}let Ot=se??{type:"final",args:[Yu(_e,{stateSummary:Ze})]};return{nonContextValues:h,contextMetadata:C,actionLog:Xi(_e,{actionReplay:Z.actionReplay,recentFullActions:Z.recentFullActions,stateSummary:Ze,successSummarization:Z.successSummarization===void 0?Z.actionReplay!=="full":!!Z.successSummarization})||"(no actions yet)",actorResult:Ot,actorFieldValues:it}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=Ft(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,actorResult:u,actorFieldValues:c}=await this._runActorLoop(s,t,n,o),p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(s,{...l,contextData:u},p),...c}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async*streamingForward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=Ft(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let s=this.ai??e,a=n?.debug??this.debug??s?.getOptions()?.debug??!1,{nonContextValues:l,contextMetadata:u,actorResult:c,actorFieldValues:p}=await this._runActorLoop(s,t,n,o),d={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};for await(let m of this.responderProgram.streamingForward(s,{...l,contextMetadata:u,actorResult:c},d))yield m;Object.keys(p).length>0&&(yield{version:1,index:0,delta:p})}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}static wrapFunction(e,t,n){return async(...r)=>{let o;if(r.length===1&&typeof r[0]=="object"&&r[0]!==null&&!Array.isArray(r[0]))o=r[0];else{let s=e.parameters?.properties?Object.keys(e.parameters.properties):[];o={},s.forEach((a,l)=>{l<r.length&&(o[a]=r[l])})}return await e.func(o,{abortSignal:t,ai:n})}}static wrapFunctionWithSharedFields(e,t,n,r){return typeof n!="function"&&(!n||Object.keys(n).length===0)?i.wrapFunction(e,t,r):async(...o)=>{let s;if(o.length===1&&typeof o[0]=="object"&&o[0]!==null&&!Array.isArray(o[0]))s=o[0];else{let u=e.parameters?.properties?Object.keys(e.parameters.properties):[];s={},u.forEach((c,p)=>{p<o.length&&(s[c]=o[p])})}let a=typeof n=="function"?n():n,l=a?{...a,...s}:s;return await e.func(l,{abortSignal:t,ai:r})}}buildRuntimeGlobals(e,t,n){let r={},o=new Map,s=new Map,a=new Map;for(let u of this.discoveryNamespaces)a.set(u.name,u);let l=(u,c)=>{o.set(c,u),s.has(u.module)||s.set(u.module,[]),s.get(u.module)?.push(c)};for(let u of this.agentFunctions){let c=u.namespace??"utils";(!r[c]||typeof r[c]!="object")&&(r[c]={}),r[c][u.name]=i.wrapFunction(u,e,n),l({module:c,name:u.name,description:u.description,parameters:u.parameters,returns:u.returns,examples:u.examples},`${c}.${u.name}`)}if(this.agents&&this.agents.length>0){let u={};for(let c of this.agents){let p=c.getFunction(),d=new Set(c.getExcludedSharedFields?.()??[]),m=()=>{let g={};if(t)for(let[h,f]of Object.entries(t))d.has(h)||(g[h]=f);return g};u[p.name]=i.wrapFunctionWithSharedFields(p,e,m,n),l({module:this.agentModuleNamespace,name:p.name,description:p.description,parameters:p.parameters},`${this.agentModuleNamespace}.${p.name}`)}r[this.agentModuleNamespace]=u}return this.functionDiscoveryEnabled&&(r[Ts]=async u=>{let c=rc(u,"modules");return jd(c,s,a)},r[ws]=async u=>{let c=rc(u,"functions");return qd(c,o)}),r}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,inputUpdateCallback:r,...o}=this.options;return o}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?Nd(e,this._parentSharedFields):e}};function oc(i,e){let t=typeof i=="string"?Ge.create(i):i,{ai:n,agentIdentity:r,...o}=e;return new jr({ai:n,agentIdentity:r,signature:t},o)}function wd(i){return i instanceof tt&&i.status>=500&&i.status<600?!0:i instanceof He||i instanceof st}function ht(i,e){return i.length<=e?i:`${i.slice(0,e)}
836
- ...[truncated ${i.length-e} chars]`}function Rd(i,e){if(e.kind==="threshold")return kd(i)<=e.promptMaxChars?i:void 0;if(typeof i!="string")return;let t=e.keepInPromptChars;if(i.length<=t)return i;let n=i.length-t;if(e.reverseTruncate){let o=t>0?i.slice(-t):"";return`[truncated ${n} chars]...${o}`}return`${t>0?i.slice(0,t):""}...[truncated ${n} chars]`}function Sd(i,e,t){return e.kind==="threshold"?t?`inline (<=${e.promptMaxChars} chars)`:`runtime-only (>${e.promptMaxChars} chars)`:typeof i!="string"?"runtime-only (keepInPromptChars requires string)":t?i.length<=e.keepInPromptChars?`inline (<=${e.keepInPromptChars} chars)`:e.reverseTruncate?`inline-truncated(last ${e.keepInPromptChars} chars of ${i.length})`:`inline-truncated(first ${e.keepInPromptChars} chars of ${i.length})`:"runtime-only"}function kd(i){if(typeof i=="string")return i.length;try{return JSON.stringify(i).length}catch{return String(i).length}}function vd(i,e,t){let n=new Set(e.map(a=>a.name)),r=new Set,o=[],s=new Map;for(let a of i){let l=typeof a=="string"?a:a.field;if(!n.has(l))throw new Error(`RLM contextField "${l}" not found in signature`);if(r.has(l))throw new Error(`Duplicate contextField "${l}"`);if(r.add(l),o.push(l),typeof a!="string"){let u=a.keepInPromptChars!==void 0,c=a.promptMaxChars!==void 0;if(u&&c)throw new Error(`contextField "${l}" cannot set both promptMaxChars and keepInPromptChars`);if("reverseTruncate"in a&&!u)throw new Error(`contextField "${l}" reverseTruncate requires keepInPromptChars`);if(u){let d=a.keepInPromptChars;if(!Number.isFinite(d)||d===void 0||d<0)throw new Error(`contextField "${l}" keepInPromptChars must be a finite number >= 0`);s.set(l,{kind:"truncate",keepInPromptChars:d,reverseTruncate:a.reverseTruncate===!0});continue}let p=a.promptMaxChars??t;if(!Number.isFinite(p)||p<0)throw new Error(`contextField "${l}" promptMaxChars must be a finite number >= 0`);s.set(l,{kind:"threshold",promptMaxChars:p})}}return{contextFieldNames:o,promptConfigByField:s}}function Od(i,e){let t=[];for(let[n,r]of Object.entries(i)){let o=Array.isArray(r)?"array":typeof r,s=typeof r=="string"?`${r.length} chars`:Array.isArray(r)?`${r.length} items`:r&&typeof r=="object"?`${Object.keys(r).length} keys`:"n/a",a=e?.promptConfigByField?.get(n),l=a===void 0?"runtime-only":Sd(r,a,e?.inlinedFields?.has(n)===!0);t.push(`- ${n}: type=${o}, size=${s}, prompt=${l}`)}return t.join(`
837
- `)}async function Md(i,e,t){if(i.length===0)return[];let n=new Array(i.length),r=0,o=Math.max(1,Math.min(e,i.length)),s=Array.from({length:o},async()=>{for(;;){let a=r++;if(a>=i.length)return;let l=i[a];if(l===void 0)return;n[a]=await t(l,a)}});return await Promise.all(s),n}function Ed(i){return i.includes("console.log")}function Pd(i){let e=sc(i),t=/\bfinal\s*\(/.test(e),n=/\bask_clarification\s*\(/.test(e),r=Number(t)+Number(n),o=Fd(e);if(r>1)return"[POLICY] Use exactly one completion signal per turn: either final(...) or ask_clarification(...), not both.";if(r===1)return o.length>0?"[POLICY] Do not combine console.log(...) with final(...)/ask_clarification(...) in the same turn. Inspect in one turn, then complete in the next turn.":void 0;if(o.length===0)return"[POLICY] Non-final turns must include exactly one console.log(...) so the next turn can reason from its output.";if(o.length>1)return"[POLICY] Use exactly one console.log(...) per non-final turn, then stop.";let s=o[0];if(s===void 0)return"[POLICY] Unable to verify console.log(...) usage. Emit exactly one console.log(...) per non-final turn.";if(s.closeParenIndex===void 0)return"[POLICY] Could not parse console.log(...). Keep a single valid console.log(...) call as the last statement in non-final turns.";if(e.slice(s.closeParenIndex+1).replace(/^[\s;]+/,"").length>0)return"[POLICY] End non-final turns immediately after console.log(...). Do not execute additional statements after logging."}function sc(i){let e="",t=0,n="normal",r=!1;for(;t<i.length;){let o=i[t]??"",s=i[t+1]??"";if(n==="lineComment"){o===`
979
+ ${L}`)}if(l.payload=void 0,this.enforceIncrementalConsoleTurns){let L=Bm(E);if(L){p.push({turn:v+1,code:E,output:L,actorFieldsOutput:_,tags:["error"]}),await sa(p,p.length-1,m.effectiveContextConfig,e,h),d=await m.captureRuntimeStateSummary(),await R();continue}}this.inputUpdateCallback&&await m.syncRuntimeInputsToSession();let{output:j,isError:H}=await m.executeActorCode(E);if(p.push({turn:v+1,code:E,output:j,actorFieldsOutput:_,tags:H?["error"]:[]}),await sa(p,p.length-1,m.effectiveContextConfig,e,h),d=await m.captureRuntimeStateSummary(),await R(),l.payload)break}}finally{try{m.close()}catch{}}await R();let k=l.payload??{type:"final",args:[Ac(p,{stateSummary:d,checkpointSummary:b?.summary,checkpointTurns:b?.turns,pruneUsedDocs:A})]};return{nonContextValues:u.getNonContextValues(),contextMetadata:u.getContextMetadata(),actionLog:T(),actorResult:k,actorFieldValues:x}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=At(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let i=this.ai??e,a=n?.debug??this.debug??i?.getOptions()?.debug??!1,{nonContextValues:u,actorResult:l,actorFieldValues:c}=await this._runActorLoop(i,t,n,o),p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(i,{...u,contextData:l},p),...c}}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}async*streamingForward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=At(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let i=this.ai??e,a=n?.debug??this.debug??i?.getOptions()?.debug??!1,{nonContextValues:u,actorResult:l,actorFieldValues:c}=await this._runActorLoop(i,t,n,o),p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};for await(let d of this.responderProgram.streamingForward(i,{...u,contextData:l},p))yield d;Object.keys(c).length>0&&(yield{version:1,index:0,delta:c})}finally{this.activeAbortControllers.delete(r),this._stopRequested=!1}}static wrapFunction(e,t,n,r){return async(...o)=>{let i;if(o.length===1&&typeof o[0]=="object"&&o[0]!==null&&!Array.isArray(o[0]))i=o[0];else{let a=e.parameters?.properties?Object.keys(e.parameters.properties):[];i={},a.forEach((u,l)=>{l<o.length&&(i[u]=o[l])})}return await e.func(i,{abortSignal:t,ai:n,protocol:r})}}static wrapFunctionWithSharedFields(e,t,n,r,o){return typeof n!="function"&&(!n||Object.keys(n).length===0)?s.wrapFunction(e,t,r,o):async(...i)=>{let a;if(i.length===1&&typeof i[0]=="object"&&i[0]!==null&&!Array.isArray(i[0]))a=i[0];else{let c=e.parameters?.properties?Object.keys(e.parameters.properties):[];a={},c.forEach((p,d)=>{d<i.length&&(a[p]=i[d])})}let u=typeof n=="function"?n():n,l=u?{...u,...a}:a;return await e.func(l,{abortSignal:t,ai:r,protocol:o})}}buildRuntimeGlobals(e,t,n,r){let o={},i=new Map,a=new Map,u=new Map;for(let[c,p]of this.agentFunctionModuleMetadata)u.set(c,p);let l=(c,p)=>{i.set(p,c),a.has(c.module)||a.set(c.module,[]),a.get(c.module)?.push(p)};for(let c of this.agentFunctions){let p=c.namespace??"utils";(!o[p]||typeof o[p]!="object")&&(o[p]={}),o[p][c.name]=s.wrapFunction(c,e,n,r),l({module:p,name:c.name,description:c.description,parameters:c.parameters,returns:c.returns,examples:c.examples},`${p}.${c.name}`)}if(this.agents&&this.agents.length>0){let c={};for(let p of this.agents){let d=p.getFunction(),m=new Set(p.getExcludedSharedFields?.()??[]),g=()=>{let f={};if(t)for(let[x,y]of Object.entries(t))m.has(x)||(f[x]=y);return f};c[d.name]=s.wrapFunctionWithSharedFields(d,e,g,n,r),l({module:this.agentModuleNamespace,name:d.name,description:d.description,parameters:d.parameters},`${this.agentModuleNamespace}.${d.name}`)}o[this.agentModuleNamespace]=c}return this.functionDiscoveryEnabled&&(o[aa]=async c=>{let p=Pc(c,"modules");return Qm(p,a,u)},o[ua]=async c=>{let p=Pc(c,"functions");return Ym(p,i)}),o}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,inputUpdateCallback:r,...o}=this.options;return o}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?qm(e,this._parentSharedFields):e}};function Fc(s,e){let t=typeof s=="string"?Re.create(s):s,{ai:n,agentIdentity:r,...o}=e;return new Dr({ai:n,agentIdentity:r,signature:t},o)}function wm(s){return s instanceof je&&s.status>=500&&s.status<600?!0:s instanceof Le||s instanceof Ke}function tt(s,e){return s.length<=e?s:`${s.slice(0,e)}
980
+ ...[truncated ${s.length-e} chars]`}function Rm(s,e){return e<=0?"":s.length<=e?s:e<=3?s.slice(0,e):`${s.slice(0,e-3)}...`}function km(s){if(!s||typeof s!="object")return!1;let e=s;return typeof e.name=="string"&&typeof e.type=="string"&&(e.ctor===void 0||typeof e.ctor=="string")&&(e.size===void 0||typeof e.size=="string")&&(e.preview===void 0||typeof e.preview=="string")}function Sm(s){let e=s.trim();if(!(!e||e[0]!=="{"&&e[0]!=="["))try{let t=JSON.parse(e),n=Array.isArray(t)?t:t&&typeof t=="object"&&Array.isArray(t.entries)?t.entries:void 0;return n?n.filter(km):void 0}catch{return}}function _c(s,e){let t=e?.maxEntries&&e.maxEntries>0?e.maxEntries:void 0,n=e?.maxChars&&e.maxChars>0?e.maxChars:void 0,r=t?s.slice(0,t):[...s];if(!n)return r.join(`
981
+ `);let o=[],i=0;for(let a of r){let u=o.length>0?1:0,l=n-i-u;if(l<=0)break;if(a.length<=l){o.push(a),i+=u+a.length;continue}o.push(Rm(a,l)),i=n;break}return o.join(`
982
+ `)}function vm(s,e){let t=s.split(`
983
+ `).map(n=>n.trim()).filter(Boolean);return _c(t,e)}function Sc(s,e){let t=0;return e&&(t+=1e6,t+=e.createdTurn*100,t+=(e.lastReadTurn??e.createdTurn)*1e4,e.source&&(t+=25)),s.type==="accessor"?t-=100:s.type==="function"&&(t-=10),t}function Om(s){let e=s.type;return s.type==="object"&&s.ctor&&s.ctor!=="Object"?e=`object<${s.ctor}>`:s.type==="error"&&s.ctor&&s.ctor!=="Error"&&(e=`error<${s.ctor}>`),s.size&&(e+=` (${s.size})`),e}function Em(s){if(!s)return"";let e=[`from t${s.createdTurn}${s.source?` via ${s.source}`:""}`];return s.lastReadTurn!==void 0&&s.lastReadTurn>s.createdTurn&&e.push(`read t${s.lastReadTurn}`),` [${e.join("; ")}]`}function Mm(s,e,t){let n=[...s].sort((r,o)=>{let i=Sc(r,e.get(r.name));return Sc(o,e.get(o.name))-i||r.name.localeCompare(o.name)}).map(r=>{let o=r.preview?` = ${r.preview}`:"",i=Em(e.get(r.name));return`${r.name}: ${Om(r)}${o}${i}`});return n.length===0?"(no user variables)":_c(n,t)}function Is(s,e){if(s===void 0)return"(no output)";if(typeof s=="string")return tt(s||"(no output)",e);try{return tt(JSON.stringify(s,null,2),e)}catch{return tt(String(s),e)}}function Cs(s,e){let t=s,n=t?.name??"Error",r=t?.message??String(s),o=[`${n}: ${r}`];if(t?.data!==void 0)try{o.push(`Data: ${JSON.stringify(t.data,null,2)}`)}catch{o.push(`Data: ${String(t.data)}`)}if(t?.cause!==void 0){let i=(a,u)=>{if(u>4)return"[cause chain truncated]";let l=a,c=l?.name??"Error",p=l?.message??String(a),d=[`${c}: ${p}`];if(l?.data!==void 0)try{d.push(`Data: ${JSON.stringify(l.data,null,2)}`)}catch{d.push(`Data: ${String(l.data)}`)}return l?.cause!==void 0&&d.push(`Caused by: ${i(l.cause,u+1)}`),d.join(`
984
+ `)};o.push(`Caused by: ${i(t.cause,1)}`)}return tt(o.join(`
985
+ `),e)}function vc(s){let e=Nc(s);return/\bfinal\s*\(/.test(e)||/\bask_clarification\s*\(/.test(e)}function Oc(s){return s&&(typeof s=="object"||typeof s=="function")&&"then"in s&&typeof s.then=="function"?!0:typeof s=="string"&&s.trim()==="[object Promise]"}function Ec(s){return s instanceof Error&&s.message==="Session is closed"}function Pm(s){return s instanceof Error&&s.message==="Execution timed out"}function Fm(s){return s.startsWith("[ERROR]")||s.startsWith(Ts)?!0:/^(AggregateError|Error|EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError): /.test(s)}function _m(s,e){if(e.kind==="threshold")return Lm(s)<=e.promptMaxChars?s:void 0;if(typeof s!="string")return;let t=e.keepInPromptChars;if(s.length<=t)return s;let n=s.length-t;if(e.reverseTruncate){let o=t>0?s.slice(-t):"";return`[truncated ${n} chars]...${o}`}return`${t>0?s.slice(0,t):""}...[truncated ${n} chars]`}function Nm(s,e,t){return e.kind==="threshold"?t?`inline (<=${e.promptMaxChars} chars)`:`runtime-only (>${e.promptMaxChars} chars)`:typeof s!="string"?"runtime-only (keepInPromptChars requires string)":t?s.length<=e.keepInPromptChars?`inline (<=${e.keepInPromptChars} chars)`:e.reverseTruncate?`inline-truncated(last ${e.keepInPromptChars} chars of ${s.length})`:`inline-truncated(first ${e.keepInPromptChars} chars of ${s.length})`:"runtime-only"}function Lm(s){if(typeof s=="string")return s.length;try{return JSON.stringify(s).length}catch{return String(s).length}}function $m(s,e,t){let n=new Set(e.map(a=>a.name)),r=new Set,o=[],i=new Map;for(let a of s){let u=typeof a=="string"?a:a.field;if(!n.has(u))throw new Error(`RLM contextField "${u}" not found in signature`);if(r.has(u))throw new Error(`Duplicate contextField "${u}"`);if(r.add(u),o.push(u),typeof a!="string"){let l=a.keepInPromptChars!==void 0,c=a.promptMaxChars!==void 0;if(l&&c)throw new Error(`contextField "${u}" cannot set both promptMaxChars and keepInPromptChars`);if("reverseTruncate"in a&&!l)throw new Error(`contextField "${u}" reverseTruncate requires keepInPromptChars`);if(l){let d=a.keepInPromptChars;if(!Number.isFinite(d)||d===void 0||d<0)throw new Error(`contextField "${u}" keepInPromptChars must be a finite number >= 0`);i.set(u,{kind:"truncate",keepInPromptChars:d,reverseTruncate:a.reverseTruncate===!0});continue}let p=a.promptMaxChars??t;if(!Number.isFinite(p)||p<0)throw new Error(`contextField "${u}" promptMaxChars must be a finite number >= 0`);i.set(u,{kind:"threshold",promptMaxChars:p})}}return{contextFieldNames:o,promptConfigByField:i}}function Dm(s,e){let t=[];for(let[n,r]of Object.entries(s)){let o=Array.isArray(r)?"array":typeof r,i=typeof r=="string"?`${r.length} chars`:Array.isArray(r)?`${r.length} items`:r&&typeof r=="object"?`${Object.keys(r).length} keys`:"n/a",a=e?.promptConfigByField?.get(n),u=a===void 0?"runtime-only":Nm(r,a,e?.inlinedFields?.has(n)===!0);t.push(`- ${n}: type=${o}, size=${i}, prompt=${u}`)}return t.join(`
986
+ `)}async function Gm(s,e,t){if(s.length===0)return[];let n=new Array(s.length),r=0,o=Math.max(1,Math.min(e,s.length)),i=Array.from({length:o},async()=>{for(;;){let a=r++;if(a>=s.length)return;let u=s[a];if(u===void 0)return;n[a]=await t(u,a)}});return await Promise.all(i),n}function Um(s){return s.includes("console.log")}function Bm(s){let e=Nc(s),t=/\bfinal\s*\(/.test(e),n=/\bask_clarification\s*\(/.test(e),r=Number(t)+Number(n),o=jm(e);if(r>1)return"[POLICY] Use exactly one completion signal per turn: either final(...) or ask_clarification(...), not both.";if(r===1)return o.length>0?"[POLICY] Do not combine console.log(...) with final(...)/ask_clarification(...) in the same turn. Inspect in one turn, then complete in the next turn.":void 0;if(o.length===0)return"[POLICY] Non-final turns must include exactly one console.log(...) so the next turn can reason from its output.";if(o.length>1)return"[POLICY] Use exactly one console.log(...) per non-final turn, then stop.";let i=o[0];if(i===void 0)return"[POLICY] Unable to verify console.log(...) usage. Emit exactly one console.log(...) per non-final turn.";if(i.closeParenIndex===void 0)return"[POLICY] Could not parse console.log(...). Keep a single valid console.log(...) call as the last statement in non-final turns.";if(e.slice(i.closeParenIndex+1).replace(/^[\s;]+/,"").length>0)return"[POLICY] End non-final turns immediately after console.log(...). Do not execute additional statements after logging."}function Nc(s){let e="",t=0,n="normal",r=!1;for(;t<s.length;){let o=s[t]??"",i=s[t+1]??"";if(n==="lineComment"){o===`
838
987
  `?(e+=`
839
- `,n="normal"):e+=" ",t++;continue}if(n==="blockComment"){o==="*"&&s==="/"?(e+=" ",t+=2,n="normal"):(e+=o===`
988
+ `,n="normal"):e+=" ",t++;continue}if(n==="blockComment"){o==="*"&&i==="/"?(e+=" ",t+=2,n="normal"):(e+=o===`
840
989
  `?`
841
990
  `:" ",t++);continue}if(n==="single"||n==="double"||n==="template"){let a=n==="single"?"'":n==="double"?'"':"`";if(r){e+=o===`
842
991
  `?`
843
992
  `:" ",r=!1,t++;continue}if(o==="\\"){e+=" ",r=!0,t++;continue}if(o===a){e+=" ",n="normal",t++;continue}e+=o===`
844
993
  `?`
845
- `:" ",t++;continue}if(o==="/"&&s==="/"){e+=" ",t+=2,n="lineComment";continue}if(o==="/"&&s==="*"){e+=" ",t+=2,n="blockComment";continue}if(o==="'"){e+=" ",t++,n="single";continue}if(o==='"'){e+=" ",t++,n="double";continue}if(o==="`"){e+=" ",t++,n="template";continue}e+=o,t++}return e}function Fd(i){let e=i.matchAll(/\bconsole\s*\.\s*log\s*\(/g),t=[];for(let n of e){let r=n[0];if(r===void 0)continue;let o=n.index??-1;if(o<0)continue;let s=r.lastIndexOf("("),a=o+s,l=_d(i,a);t.push({closeParenIndex:l})}return t}function _d(i,e){if(e<0||i[e]!=="(")return;let t=0;for(let n=e;n<i.length;n++){let r=i[n];if(r==="(")t++;else if(r===")"&&(t--,t===0))return n}}function Nd(i,e){if(!i.properties||e.size===0)return i;let t=Object.fromEntries(Object.entries(i.properties).filter(([r])=>!e.has(r))),n=i.required?.filter(r=>!e.has(r));return{...i,properties:t,...n!==void 0?{required:n}:{}}}function $d(i,e){let t=i.trim(),r=e?.normalize??!0?ac(t):t;if(!r)throw new Error("Agent module namespace must contain letters or numbers");return r}function Ld(i,e){if(!i||i.length===0)return[];let t=new Set;return i.map(n=>{let r=n.name.trim(),o=n.title.trim(),s=n.description.trim();if(!r)throw new Error("Agent namespace metadata name must be a non-empty string");if(!o)throw new Error(`Agent namespace "${r}" must define a non-empty title`);if(!s)throw new Error(`Agent namespace "${r}" must define a non-empty description`);if(e.has(r))throw new Error(`Agent namespace "${r}" is reserved`);if(t.has(r))throw new Error(`Duplicate agent namespace "${r}"`);return t.add(r),{name:r,title:o,description:s}})}function rc(i,e){if(typeof i=="string"){let n=i.trim();if(n.length===0)throw new Error(`${e} must be a non-empty string`);return[n]}if(!Array.isArray(i))throw new Error(`${e} must be a string or string[]`);if(!i.every(n=>typeof n=="string"))throw new Error(`${e} must contain only strings`);let t=i.map(n=>n).map(n=>n.trim()).filter(n=>n.length>0);if(t.length===0)throw new Error(`${e} must contain at least one non-empty string`);return[...new Set(t)]}function ea(i){let e=i.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function Dd(i){let e=new Set(i);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function qr(i){if(i.enum)return i.enum.map(n=>`"${n}"`).join(" | ");let e=ea(i);if(e.length===0)return"unknown";if(Dd(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=i.items?qr(i.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?i.properties&&Object.keys(i.properties).length>0?ic(i):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function ic(i,e){if(!i)return"{}";let t=!!i.properties&&Object.keys(i.properties).length>0,n=i.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(i.required??[]),o=e?.respectRequired??!1,s=Object.entries(i.properties).map(([a,l])=>{let u=qr(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return i.additionalProperties===!0&&s.push("[key: string]: unknown"),`{ ${s.join(", ")} }`}function Gd(i){let e=ic(i.parameters,{respectRequired:!0}),t=i.returns?`: Promise<${qr(i.returns)}>`:"";return`- \`${i.qualifiedName}(args: ${e})${t}\``}function ta(i,e="",t=!0){if(!i?.properties)return[];let n=new Set(i.required??[]),r=[];for(let[o,s]of Object.entries(i.properties)){let a=e?`${e}.${o}`:o,l=s.description?.trim();l&&r.push({name:a,type:qr(s),required:t?n.has(o):void 0,description:l});let u=ea(s);if(u.includes("object")&&s.properties&&r.push(...ta(s,a,!1)),u.includes("array")&&s.items){let c=s.items.description?.trim(),p=`${a}[]`;c&&r.push({name:p,type:qr(s.items),description:c}),ea(s.items).includes("object")&&s.items.properties&&r.push(...ta(s.items,p,!1))}}return r}function Ud(i){let e=ta(i);if(e.length!==0)return["#### Arguments",...e.map(t=>{let n=t.required===void 0?`\`${t.type}\``:`\`${t.type}\`, ${t.required?"required":"optional"}`;return`- \`${t.name}\` (${n}): ${t.description}`})].join(`
846
- `)}function Bd(i){return!i||i.length===0?void 0:["#### Examples",i.map(t=>{let n=[];return t.title?.trim()&&n.push(`##### ${t.title.trim()}`),t.description?.trim()&&n.push(t.description.trim()),n.push(`\`\`\`${t.language?.trim()||"typescript"}`),n.push(t.code),n.push("```"),n.join(`
994
+ `:" ",t++;continue}if(o==="/"&&i==="/"){e+=" ",t+=2,n="lineComment";continue}if(o==="/"&&i==="*"){e+=" ",t+=2,n="blockComment";continue}if(o==="'"){e+=" ",t++,n="single";continue}if(o==='"'){e+=" ",t++,n="double";continue}if(o==="`"){e+=" ",t++,n="template";continue}e+=o,t++}return e}function jm(s){let e=s.matchAll(/\bconsole\s*\.\s*log\s*\(/g),t=[];for(let n of e){let r=n[0];if(r===void 0)continue;let o=n.index??-1;if(o<0)continue;let i=r.lastIndexOf("("),a=o+i,u=zm(s,a);t.push({closeParenIndex:u})}return t}function zm(s,e){if(e<0||s[e]!=="(")return;let t=0;for(let n=e;n<s.length;n++){let r=s[n];if(r==="(")t++;else if(r===")"&&(t--,t===0))return n}}function qm(s,e){if(!s.properties||e.size===0)return s;let t=Object.fromEntries(Object.entries(s.properties).filter(([r])=>!e.has(r))),n=s.required?.filter(r=>!e.has(r));return{...s,properties:t,...n!==void 0?{required:n}:{}}}function Hm(s,e){let t=s.trim(),r=e?.normalize??!0?$c(t):t;if(!r)throw new Error("Agent module namespace must contain letters or numbers");return r}function Mc(s){return Array.isArray(s.functions)}function la(s,e){if(!s||s.length===0)return{functions:[],moduleMetadata:[]};let t=s.every(a=>Mc(a)),n=s.every(a=>!Mc(a));if(!t&&!n)throw new Error("Agent functions collections must contain either flat functions or grouped function modules, not both");if(n)return{functions:[...s],moduleMetadata:[]};let r=new Set,o=[],i=[];for(let a of s){let u=a.namespace.trim(),l=a.title.trim(),c=a.selectionCriteria.trim(),p=a.description.trim();if(!u)throw new Error("Agent function group namespace must be a non-empty string");if(!l)throw new Error(`Agent function group "${u}" must define a non-empty title`);if(!c)throw new Error(`Agent function group "${u}" must define a non-empty selectionCriteria`);if(!p)throw new Error(`Agent function group "${u}" must define a non-empty description`);if(e.has(u))throw new Error(`Agent function namespace "${u}" conflicts with an AxAgent runtime global and is reserved`);if(r.has(u))throw new Error(`Duplicate agent function group namespace "${u}"`);if(a.functions.length===0)throw new Error(`Agent function group "${u}" must contain at least one function`);r.add(u),o.push({namespace:u,title:l,selectionCriteria:c,description:p});for(let d of a.functions){if("namespace"in d&&d.namespace!==void 0)throw new Error(`Grouped agent function "${u}.${d.name}" must not define namespace; use the parent group namespace instead`);i.push({...d,namespace:u})}}return{functions:i,moduleMetadata:o}}function Pc(s,e){if(typeof s=="string"){let n=s.trim();if(n.length===0)throw new Error(`${e} must be a non-empty string`);return[n]}if(!Array.isArray(s))throw new Error(`${e} must be a string or string[]`);if(!s.every(n=>typeof n=="string"))throw new Error(`${e} must contain only strings`);let t=s.map(n=>n).map(n=>n.trim()).filter(n=>n.length>0);if(t.length===0)throw new Error(`${e} must contain at least one non-empty string`);return[...new Set(t)]}function ca(s){let e=s.type;return Array.isArray(e)?e.filter(t=>typeof t=="string"):typeof e=="string"?e.includes(",")?e.split(",").map(t=>t.trim()).filter(Boolean):[e]:[]}function Km(s){let e=new Set(s);return e.has("object")&&e.has("array")&&e.has("string")&&e.has("number")&&e.has("boolean")&&e.has("null")}function Gr(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=ca(s);if(e.length===0)return"unknown";if(Km(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?Gr(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?Lc(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Lc(s,e){if(!s)return"{}";let t=!!s.properties&&Object.keys(s.properties).length>0,n=s.additionalProperties===!0;if(!t)return n?"{ [key: string]: unknown }":"{}";let r=new Set(s.required??[]),o=e?.respectRequired??!1,i=Object.entries(s.properties).map(([a,u])=>{let l=Gr(u),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${l}`});return s.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function Wm(s){let e=Lc(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${Gr(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function pa(s,e="",t=!0){if(!s?.properties)return[];let n=new Set(s.required??[]),r=[];for(let[o,i]of Object.entries(s.properties)){let a=e?`${e}.${o}`:o,u=i.description?.trim();u&&r.push({name:a,type:Gr(i),required:t?n.has(o):void 0,description:u});let l=ca(i);if(l.includes("object")&&i.properties&&r.push(...pa(i,a,!1)),l.includes("array")&&i.items){let c=i.items.description?.trim(),p=`${a}[]`;c&&r.push({name:p,type:Gr(i.items),description:c}),ca(i.items).includes("object")&&i.items.properties&&r.push(...pa(i.items,p,!1))}}return r}function Vm(s){let e=pa(s);if(e.length!==0)return["#### Arguments",...e.map(t=>{let n=t.required===void 0?`\`${t.type}\``:`\`${t.type}\`, ${t.required?"required":"optional"}`;return`- \`${t.name}\` (${n}): ${t.description}`})].join(`
995
+ `)}function Jm(s){return!s||s.length===0?void 0:["#### Examples",s.map(t=>{let n=[];return t.title?.trim()&&n.push(`##### ${t.title.trim()}`),t.description?.trim()&&n.push(t.description.trim()),n.push(`\`\`\`${t.language?.trim()||"typescript"}`),n.push(t.code),n.push("```"),n.join(`
847
996
  `)}).join(`
848
997
 
849
998
  `)].join(`
850
- `)}function jd(i,e,t){return i.map(n=>{let r=[...e.get(n)??[]].sort((u,c)=>u.localeCompare(c)),o=r.length>0,s=o?t.get(n):void 0,a=o?r.map(u=>`- \`${u}\``).join(`
851
- `):`- Error: module \`${n}\` does not exist.`,l=[`### Module \`${n}\``];return s&&(l.push(`**${s.title}**`),l.push(s.description)),l.push(a),l.join(`
999
+ `)}function Qm(s,e,t){return s.map(n=>{let r=[...e.get(n)??[]].map(l=>l.split(".").pop()??l).sort((l,c)=>l.localeCompare(c)),o=r.length>0,i=o?t.get(n):void 0,a=o?r.map(l=>`- \`${l}\``).join(`
1000
+ `):`- Error: module \`${n}\` does not exist.`,u=[`### Module \`${n}\``];return i&&(u.push(`**${i.title}**`),u.push(i.description)),u.push(a),u.join(`
852
1001
  `)}).join(`
853
1002
 
854
- `)}function qd(i,e){return i.map(t=>{let n=t.includes(".")?t:`utils.${t}`,r=e.get(n);return r?[`### \`${n}\``,r.description,Gd({qualifiedName:n,parameters:r.parameters,returns:r.returns}),Ud(r.parameters),Bd(r.examples)].filter(o=>!!o).join(`
1003
+ `)}function Ym(s,e){return s.map(t=>{let n=t.includes(".")?t:`utils.${t}`,r=e.get(n);return r?[`### \`${n}\``,r.description,Wm({qualifiedName:n,parameters:r.parameters,returns:r.returns}),Vm(r.parameters),Jm(r.examples)].filter(o=>!!o).join(`
855
1004
  `):`### \`${n}\`
856
1005
  - Not found.`}).join(`
857
1006
 
858
- `)}function ac(i){return i.split(/[^a-zA-Z0-9]/).map((n,r)=>{let o=n.toLowerCase();return r>0&&o&&o[0]?o[0].toUpperCase()+o.slice(1):o}).join("")}var lc=(i,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,s=e?.disableQualityHealing??!1;return cs({logger:e?.logger,debug:e?.debug}).node("queryGenerator","originalQuestion:string, previousContext?:string -> searchQuery:string, queryReasoning:string").node("contextualizer","retrievedDocument:string, accumulatedContext?:string -> enhancedContext:string").node("qualityAssessor","currentContext:string, originalQuestion:string -> completenessScore:number, missingAspects:string[]").node("questionDecomposer","complexQuestion:string -> subQuestions:string[], decompositionReason:string").node("evidenceSynthesizer","collectedEvidence:string[], originalQuestion:string -> synthesizedEvidence:string, evidenceGaps:string[]").node("gapAnalyzer","synthesizedEvidence:string, evidenceGaps:string[], originalQuestion:string -> needsMoreInfo:boolean, focusedQueries:string[]").node("answerGenerator","finalContext:string, originalQuestion:string -> comprehensiveAnswer:string, confidenceLevel:number").node("queryRefiner","originalQuestion:string, currentContext:string, missingAspects:string[] -> refinedQuery:string").node("qualityValidator","generatedAnswer:string, userQuery:string -> qualityScore:number, issues:string[]").node("answerHealer","originalAnswer:string, healingDocument:string, issues?:string[] -> healedAnswer:string").map(a=>({...a,maxHops:t,qualityThreshold:n,maxIterations:r,qualityTarget:o,disableQualityHealing:s,currentHop:0,accumulatedContext:"",retrievedContexts:[],completenessScore:0,searchQuery:a.originalQuestion,shouldContinue:!0,iteration:0,allEvidence:[],evidenceSources:[],needsMoreInfo:!0,healingAttempts:0,currentQuality:0,shouldContinueHealing:!0,currentAnswer:"",currentIssues:[]})).while(a=>a.currentHop<a.maxHops&&a.completenessScore<a.qualityThreshold&&a.shouldContinue).map(a=>({...a,currentHop:a.currentHop+1})).execute("queryGenerator",a=>({originalQuestion:a.originalQuestion,previousContext:a.accumulatedContext||void 0})).map(async a=>{let l=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,u=await i(l);return{...a,retrievalResult:{retrievedDocument:u,retrievalConfidence:.9}}}).execute("contextualizer",a=>({retrievedDocument:a.retrievalResult.retrievedDocument,accumulatedContext:a.accumulatedContext||void 0})).execute("qualityAssessor",a=>({currentContext:a.contextualizerResult.enhancedContext,originalQuestion:a.originalQuestion})).map(a=>({...a,accumulatedContext:a.contextualizerResult.enhancedContext,retrievedContexts:[...a.retrievedContexts,a.retrievalResult.retrievedDocument],completenessScore:a.qualityAssessorResult.completenessScore,searchQuery:a.queryGeneratorResult.searchQuery,shouldContinue:a.qualityAssessorResult.completenessScore<a.qualityThreshold})).branch(a=>a.shouldContinue&&a.currentHop<a.maxHops).when(!0).execute("queryRefiner",a=>({originalQuestion:a.originalQuestion,currentContext:a.accumulatedContext,missingAspects:a.qualityAssessorResult.missingAspects})).map(a=>({...a,searchQuery:a.queryRefinerResult?.refinedQuery||a.searchQuery})).when(!1).map(a=>a).merge().endWhile().map(a=>({...a,allEvidence:a.retrievedContexts.length>0?a.retrievedContexts:[]})).while(a=>a.iteration<a.maxIterations&&a.needsMoreInfo).map(a=>({...a,iteration:a.iteration+1})).branch(a=>a.iteration===1).when(!0).execute("questionDecomposer",a=>({complexQuestion:a.originalQuestion})).map(a=>({...a,currentQueries:a.questionDecomposerResult.subQuestions})).when(!1).map(a=>({...a,currentQueries:a.gapAnalyzerResult?.focusedQueries||[]})).merge().map(async a=>{let l=a.currentQueries||[],u=l.length>0?await Promise.all(l.filter(Boolean).map(c=>i(c))):[];return{...a,retrievalResults:u}}).execute("evidenceSynthesizer",a=>{let l=Array.isArray(a.allEvidence)?a.allEvidence:[],u=Array.isArray(a.retrievalResults)?a.retrievalResults:[],c=[...l,...u].filter(Boolean);return{collectedEvidence:c.length>0?c:["No evidence collected yet"],originalQuestion:a.originalQuestion}}).execute("gapAnalyzer",a=>({synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence,evidenceGaps:a.evidenceSynthesizerResult.evidenceGaps,originalQuestion:a.originalQuestion})).map(a=>({...a,allEvidence:[...Array.isArray(a.allEvidence)?a.allEvidence:[],...Array.isArray(a.retrievalResults)?a.retrievalResults:[]],evidenceSources:[...Array.isArray(a.evidenceSources)?a.evidenceSources:[],`Iteration ${a.iteration} sources`],needsMoreInfo:a.gapAnalyzerResult.needsMoreInfo,synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence})).endWhile().execute("answerGenerator",a=>({finalContext:(()=>{let l=(a.accumulatedContext||"").toString().trim();if(l.length>0)return l;let u=(a.synthesizedEvidence||"").toString().trim();if(u.length>0)return u;let p=(Array.isArray(a.allEvidence)?a.allEvidence.filter(Boolean).join(`
859
- `):"").toString().trim();return p.length>0?p:"No context available."})(),originalQuestion:a.originalQuestion})).branch(a=>!a.disableQualityHealing).when(!0).execute("qualityValidator",a=>({generatedAnswer:a.answerGeneratorResult.comprehensiveAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).while(a=>a.healingAttempts<3&&a.shouldContinueHealing).map(a=>({...a,healingAttempts:a.healingAttempts+1})).map(async a=>{let l=a.currentIssues||[],u=l.length>0?`${a.originalQuestion} addressing issues: ${l.join(", ")}`:`${a.originalQuestion} quality improvement`,c=await i(u);return{...a,healingResult:{healingDocument:c}}}).execute("answerHealer",a=>({originalAnswer:a.currentAnswer,healingDocument:a.healingResult.healingDocument,issues:a.currentIssues})).execute("qualityValidator",a=>({generatedAnswer:a.answerHealerResult.healedAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerHealerResult.healedAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).endWhile().when(!1).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:1,currentIssues:[],shouldContinueHealing:!1})).merge().returns(a=>({finalAnswer:a.currentAnswer,totalHops:a.currentHop,retrievedContexts:a.retrievedContexts,iterationCount:a.iteration,healingAttempts:a.healingAttempts,qualityAchieved:a.currentQuality}))};
1007
+ `)}function $c(s){return s.split(/[^a-zA-Z0-9]/).map((n,r)=>{let o=n.toLowerCase();return r>0&&o&&o[0]?o[0].toUpperCase()+o.slice(1):o}).join("")}var Dc=(s,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,i=e?.disableQualityHealing??!1;return is({logger:e?.logger,debug:e?.debug}).node("queryGenerator","originalQuestion:string, previousContext?:string -> searchQuery:string, queryReasoning:string").node("contextualizer","retrievedDocument:string, accumulatedContext?:string -> enhancedContext:string").node("qualityAssessor","currentContext:string, originalQuestion:string -> completenessScore:number, missingAspects:string[]").node("questionDecomposer","complexQuestion:string -> subQuestions:string[], decompositionReason:string").node("evidenceSynthesizer","collectedEvidence:string[], originalQuestion:string -> synthesizedEvidence:string, evidenceGaps:string[]").node("gapAnalyzer","synthesizedEvidence:string, evidenceGaps:string[], originalQuestion:string -> needsMoreInfo:boolean, focusedQueries:string[]").node("answerGenerator","finalContext:string, originalQuestion:string -> comprehensiveAnswer:string, confidenceLevel:number").node("queryRefiner","originalQuestion:string, currentContext:string, missingAspects:string[] -> refinedQuery:string").node("qualityValidator","generatedAnswer:string, userQuery:string -> qualityScore:number, issues:string[]").node("answerHealer","originalAnswer:string, healingDocument:string, issues?:string[] -> healedAnswer:string").map(a=>({...a,maxHops:t,qualityThreshold:n,maxIterations:r,qualityTarget:o,disableQualityHealing:i,currentHop:0,accumulatedContext:"",retrievedContexts:[],completenessScore:0,searchQuery:a.originalQuestion,shouldContinue:!0,iteration:0,allEvidence:[],evidenceSources:[],needsMoreInfo:!0,healingAttempts:0,currentQuality:0,shouldContinueHealing:!0,currentAnswer:"",currentIssues:[]})).while(a=>a.currentHop<a.maxHops&&a.completenessScore<a.qualityThreshold&&a.shouldContinue).map(a=>({...a,currentHop:a.currentHop+1})).execute("queryGenerator",a=>({originalQuestion:a.originalQuestion,previousContext:a.accumulatedContext||void 0})).map(async a=>{let u=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,l=await s(u);return{...a,retrievalResult:{retrievedDocument:l,retrievalConfidence:.9}}}).execute("contextualizer",a=>({retrievedDocument:a.retrievalResult.retrievedDocument,accumulatedContext:a.accumulatedContext||void 0})).execute("qualityAssessor",a=>({currentContext:a.contextualizerResult.enhancedContext,originalQuestion:a.originalQuestion})).map(a=>({...a,accumulatedContext:a.contextualizerResult.enhancedContext,retrievedContexts:[...a.retrievedContexts,a.retrievalResult.retrievedDocument],completenessScore:a.qualityAssessorResult.completenessScore,searchQuery:a.queryGeneratorResult.searchQuery,shouldContinue:a.qualityAssessorResult.completenessScore<a.qualityThreshold})).branch(a=>a.shouldContinue&&a.currentHop<a.maxHops).when(!0).execute("queryRefiner",a=>({originalQuestion:a.originalQuestion,currentContext:a.accumulatedContext,missingAspects:a.qualityAssessorResult.missingAspects})).map(a=>({...a,searchQuery:a.queryRefinerResult?.refinedQuery||a.searchQuery})).when(!1).map(a=>a).merge().endWhile().map(a=>({...a,allEvidence:a.retrievedContexts.length>0?a.retrievedContexts:[]})).while(a=>a.iteration<a.maxIterations&&a.needsMoreInfo).map(a=>({...a,iteration:a.iteration+1})).branch(a=>a.iteration===1).when(!0).execute("questionDecomposer",a=>({complexQuestion:a.originalQuestion})).map(a=>({...a,currentQueries:a.questionDecomposerResult.subQuestions})).when(!1).map(a=>({...a,currentQueries:a.gapAnalyzerResult?.focusedQueries||[]})).merge().map(async a=>{let u=a.currentQueries||[],l=u.length>0?await Promise.all(u.filter(Boolean).map(c=>s(c))):[];return{...a,retrievalResults:l}}).execute("evidenceSynthesizer",a=>{let u=Array.isArray(a.allEvidence)?a.allEvidence:[],l=Array.isArray(a.retrievalResults)?a.retrievalResults:[],c=[...u,...l].filter(Boolean);return{collectedEvidence:c.length>0?c:["No evidence collected yet"],originalQuestion:a.originalQuestion}}).execute("gapAnalyzer",a=>({synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence,evidenceGaps:a.evidenceSynthesizerResult.evidenceGaps,originalQuestion:a.originalQuestion})).map(a=>({...a,allEvidence:[...Array.isArray(a.allEvidence)?a.allEvidence:[],...Array.isArray(a.retrievalResults)?a.retrievalResults:[]],evidenceSources:[...Array.isArray(a.evidenceSources)?a.evidenceSources:[],`Iteration ${a.iteration} sources`],needsMoreInfo:a.gapAnalyzerResult.needsMoreInfo,synthesizedEvidence:a.evidenceSynthesizerResult.synthesizedEvidence})).endWhile().execute("answerGenerator",a=>({finalContext:(()=>{let u=(a.accumulatedContext||"").toString().trim();if(u.length>0)return u;let l=(a.synthesizedEvidence||"").toString().trim();if(l.length>0)return l;let p=(Array.isArray(a.allEvidence)?a.allEvidence.filter(Boolean).join(`
1008
+ `):"").toString().trim();return p.length>0?p:"No context available."})(),originalQuestion:a.originalQuestion})).branch(a=>!a.disableQualityHealing).when(!0).execute("qualityValidator",a=>({generatedAnswer:a.answerGeneratorResult.comprehensiveAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).while(a=>a.healingAttempts<3&&a.shouldContinueHealing).map(a=>({...a,healingAttempts:a.healingAttempts+1})).map(async a=>{let u=a.currentIssues||[],l=u.length>0?`${a.originalQuestion} addressing issues: ${u.join(", ")}`:`${a.originalQuestion} quality improvement`,c=await s(l);return{...a,healingResult:{healingDocument:c}}}).execute("answerHealer",a=>({originalAnswer:a.currentAnswer,healingDocument:a.healingResult.healingDocument,issues:a.currentIssues})).execute("qualityValidator",a=>({generatedAnswer:a.answerHealerResult.healedAnswer,userQuery:a.originalQuestion})).map(a=>({...a,currentAnswer:a.answerHealerResult.healedAnswer,currentQuality:a.qualityValidatorResult.qualityScore,currentIssues:a.qualityValidatorResult.issues,shouldContinueHealing:a.qualityValidatorResult.qualityScore<a.qualityTarget})).endWhile().when(!1).map(a=>({...a,currentAnswer:a.answerGeneratorResult.comprehensiveAnswer,currentQuality:1,currentIssues:[],shouldContinueHealing:!1})).merge().returns(a=>({finalAnswer:a.currentAnswer,totalHops:a.currentHop,retrievedContexts:a.retrievedContexts,iterationCount:a.iteration,healingAttempts:a.healingAttempts,qualityAchieved:a.currentQuality}))};
860
1009
  //# sourceMappingURL=index.cjs.map