@ax-llm/ax 19.0.20 → 19.0.21

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,34 +1,34 @@
1
- "use strict";var sp=Object.create;var Jr=Object.defineProperty;var ip=Object.getOwnPropertyDescriptor;var ap=Object.getOwnPropertyNames;var lp=Object.getPrototypeOf,up=Object.prototype.hasOwnProperty;var cp=(s,e)=>{for(var t in e)Jr(s,t,{get:e[t],enumerable:!0})},Ia=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ap(e))!up.call(s,r)&&r!==t&&Jr(s,r,{get:()=>e[r],enumerable:!(n=ip(e,r))||n.enumerable});return s};var or=(s,e,t)=>(t=s!=null?sp(lp(s)):{},Ia(e||!s||!s.__esModule?Jr(t,"default",{value:s,enumerable:!0}):t,s)),pp=s=>Ia(Jr({},"__esModule",{value:!0}),s);var Mg={};cp(Mg,{AxACE:()=>Un,AxACEOptimizedProgram:()=>Dr,AxAI:()=>wr,AxAIAnthropic:()=>nn,AxAIAnthropicModel:()=>ar,AxAIAnthropicVertexModel:()=>lr,AxAIAzureOpenAI:()=>ln,AxAICohere:()=>un,AxAICohereEmbedModel:()=>dr,AxAICohereModel:()=>pr,AxAIDeepSeek:()=>cn,AxAIDeepSeekModel:()=>mr,AxAIGoogleGemini:()=>pn,AxAIGoogleGeminiEmbedModel:()=>mo,AxAIGoogleGeminiEmbedTypes:()=>ti,AxAIGoogleGeminiModel:()=>gr,AxAIGoogleGeminiSafetyCategory:()=>go,AxAIGoogleGeminiSafetyThreshold:()=>fo,AxAIGrok:()=>Tn,AxAIGrokEmbedModels:()=>gi,AxAIGrokModel:()=>Tr,AxAIGroq:()=>mn,AxAIGroqModel:()=>fr,AxAIHuggingFace:()=>gn,AxAIHuggingFaceModel:()=>bo,AxAIMistral:()=>fn,AxAIMistralEmbedModels:()=>ii,AxAIMistralModel:()=>hr,AxAIOllama:()=>hn,AxAIOpenAI:()=>an,AxAIOpenAIBase:()=>De,AxAIOpenAIEmbedModel:()=>rn,AxAIOpenAIModel:()=>cr,AxAIOpenAIResponses:()=>An,AxAIOpenAIResponsesBase:()=>xr,AxAIOpenAIResponsesImpl:()=>xn,AxAIOpenAIResponsesModel:()=>on,AxAIOpenRouter:()=>yn,AxAIRefusalError:()=>Pe,AxAIReka:()=>bn,AxAIRekaModel:()=>yr,AxAIServiceAbortedError:()=>$e,AxAIServiceAuthenticationError:()=>Et,AxAIServiceError:()=>Je,AxAIServiceNetworkError:()=>qe,AxAIServiceResponseError:()=>Ot,AxAIServiceStatusError:()=>Xe,AxAIServiceStreamTerminatedError:()=>st,AxAIServiceTimeoutError:()=>nt,AxAITogether:()=>In,AxAITogetherModel:()=>Ir,AxAIWebLLM:()=>Cn,AxAIWebLLMModel:()=>Cr,AxAgent:()=>Kr,AxAgentClarificationError:()=>tr,AxApacheTika:()=>ts,AxAssertionError:()=>wt,AxBalancer:()=>ao,AxBaseAI:()=>Ge,AxBaseOptimizer:()=>pt,AxBootstrapFewShot:()=>zn,AxContentProcessingError:()=>ft,AxDB:()=>_o,AxDBBase:()=>at,AxDBCloudflare:()=>wn,AxDBManager:()=>No,AxDBMemory:()=>_t,AxDBPinecone:()=>Rn,AxDBWeaviate:()=>Sn,AxDefaultCostTracker:()=>$r,AxDefaultResultReranker:()=>es,AxDockerSession:()=>fs,AxEmbeddingAdapter:()=>hs,AxEvalUtil:()=>Lu,AxFlow:()=>jr,AxFlowDependencyAnalyzer:()=>qn,AxFlowExecutionPlanner:()=>Kn,AxFlowSubContextImpl:()=>Jn,AxFlowTypedSubContextImpl:()=>ms,AxFluentFieldType:()=>Be,AxFunctionError:()=>Er,AxFunctionProcessor:()=>Pr,AxGEPA:()=>Ut,AxGen:()=>Se,AxGenerateError:()=>Nr,AxHFDataLoader:()=>cs,AxInstanceRegistry:()=>Pn,AxJSRuntime:()=>Zt,AxJSRuntimePermission:()=>aa,AxJudge:()=>Gt,AxLLMRequestTypeValues:()=>Ds,AxLearn:()=>us,AxMCPClient:()=>ys,AxMCPHTTPSSETransport:()=>Cs,AxMCPStreambleHTTPTransport:()=>Is,AxMediaNotSupportedError:()=>it,AxMemory:()=>kn,AxMiPRO:()=>ds,AxMockAIService:()=>To,AxMultiServiceRouter:()=>wo,AxOptimizedProgramImpl:()=>kt,AxProgram:()=>Dt,AxPromptTemplate:()=>Jt,AxProviderRouter:()=>vo,AxRateLimiterTokenUsage:()=>dn,AxSignature:()=>Fe,AxSignatureBuilder:()=>Fr,AxSimpleClassifier:()=>rs,AxSimpleClassifierClass:()=>ns,AxSpanKindValues:()=>Gs,AxStepContextImpl:()=>Ln,AxStopFunctionCallException:()=>Kt,AxStringUtil:()=>Zo,AxSynth:()=>Bn,AxTestPrompt:()=>os,AxTokenLimitError:()=>sr,AxTraceLogger:()=>Gn,agent:()=>Vc,ai:()=>Fl,ax:()=>et,axAIAnthropicDefaultConfig:()=>Vs,axAIAnthropicVertexDefaultConfig:()=>il,axAIAzureOpenAIBestConfig:()=>cl,axAIAzureOpenAICreativeConfig:()=>ll,axAIAzureOpenAIDefaultConfig:()=>Qs,axAIAzureOpenAIFastConfig:()=>ul,axAICohereCreativeConfig:()=>fl,axAICohereDefaultConfig:()=>Zs,axAIDeepSeekCodeConfig:()=>hl,axAIDeepSeekDefaultConfig:()=>ei,axAIGoogleGeminiDefaultConfig:()=>ri,axAIGoogleGeminiDefaultCreativeConfig:()=>bl,axAIGrokBestConfig:()=>Pl,axAIGrokDefaultConfig:()=>Po,axAIHuggingFaceCreativeConfig:()=>Il,axAIHuggingFaceDefaultConfig:()=>si,axAIMistralBestConfig:()=>Cl,axAIMistralDefaultConfig:()=>Co,axAIOllamaDefaultConfig:()=>ai,axAIOllamaDefaultCreativeConfig:()=>Tl,axAIOpenAIBestConfig:()=>oo,axAIOpenAICreativeConfig:()=>so,axAIOpenAIDefaultConfig:()=>zt,axAIOpenAIFastConfig:()=>io,axAIOpenAIResponsesBestConfig:()=>wl,axAIOpenAIResponsesCreativeConfig:()=>Rl,axAIOpenAIResponsesDefaultConfig:()=>Ar,axAIOpenRouterDefaultConfig:()=>ui,axAIRekaBestConfig:()=>vl,axAIRekaCreativeConfig:()=>Ol,axAIRekaDefaultConfig:()=>br,axAIRekaFastConfig:()=>El,axAITogetherDefaultConfig:()=>pi,axAIWebLLMCreativeConfig:()=>Ml,axAIWebLLMDefaultConfig:()=>mi,axAnalyzeChatPromptRequirements:()=>Sl,axAnalyzeRequestRequirements:()=>qt,axBaseAIDefaultConfig:()=>be,axBaseAIDefaultCreativeConfig:()=>je,axBuildActorDefinition:()=>Ss,axBuildResponderDefinition:()=>ks,axCheckMetricsHealth:()=>_l,axCreateDefaultColorLogger:()=>$s,axCreateDefaultOptimizerColorLogger:()=>qi,axCreateDefaultOptimizerTextLogger:()=>Uu,axCreateDefaultTextLogger:()=>Oa,axCreateFlowColorLogger:()=>Ur,axCreateFlowTextLogger:()=>Ju,axCreateJSRuntime:()=>cc,axDefaultFlowLogger:()=>Qu,axDefaultMetricsConfig:()=>Ai,axDefaultOptimizerLogger:()=>Lr,axDefaultOptimizerMetricsConfig:()=>Hi,axGetCompatibilityReport:()=>pl,axGetFormatCompatibility:()=>ml,axGetMetricsConfig:()=>Ll,axGetOptimizerMetricsConfig:()=>Bu,axGetProvidersWithMediaSupport:()=>dl,axGlobals:()=>ie,axModelInfoAnthropic:()=>ur,axModelInfoCohere:()=>co,axModelInfoDeepSeek:()=>po,axModelInfoGoogleGemini:()=>ho,axModelInfoGrok:()=>Mo,axModelInfoGroq:()=>Ao,axModelInfoHuggingFace:()=>yo,axModelInfoMistral:()=>Io,axModelInfoOpenAI:()=>sn,axModelInfoOpenAIResponses:()=>ro,axModelInfoReka:()=>ko,axModelInfoTogether:()=>Oo,axModelInfoWebLLM:()=>Eo,axProcessContentForProvider:()=>So,axRAG:()=>Zc,axScoreProvidersForRequest:()=>lo,axSelectOptimalProvider:()=>uo,axSpanAttributes:()=>ue,axSpanEvents:()=>ht,axUpdateMetricsConfig:()=>Nl,axUpdateOptimizerMetricsConfig:()=>ju,axValidateChatRequestMessage:()=>en,axValidateChatResponseResult:()=>no,axValidateProviderCapabilities:()=>Ys,axWorkerRuntime:()=>xs,f:()=>pe,flow:()=>gs,fn:()=>Iu,s:()=>Du});module.exports=pp(Mg);function ot({model:s,modelInfo:e,models:t}){let n=t?.find(u=>u.key===s),r=n&&"model"in n?n.model:s,o=e.find(u=>u.name===s);if(o)return o;let i=e.find(u=>u.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+$/,""),l=e.find(u=>u.name===a);return l||null}var _s=(()=>{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 _s.randomUUID()}async function dp(s){let e=new TextEncoder,t=typeof s=="string"?e.encode(s):s,n=await _s.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("")}var Fs=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 dp(this.data)}};function tt(s){if(s!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new Fs}function Ca(){return _s}var Qr=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 up=Object.create;var eo=Object.defineProperty;var cp=Object.getOwnPropertyDescriptor;var pp=Object.getOwnPropertyNames;var dp=Object.getPrototypeOf,mp=Object.prototype.hasOwnProperty;var gp=(s,e)=>{for(var t in e)eo(s,t,{get:e[t],enumerable:!0})},Oa=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of pp(e))!mp.call(s,r)&&r!==t&&eo(s,r,{get:()=>e[r],enumerable:!(n=cp(e,r))||n.enumerable});return s};var ar=(s,e,t)=>(t=s!=null?up(dp(s)):{},Oa(e||!s||!s.__esModule?eo(t,"default",{value:s,enumerable:!0}):t,s)),fp=s=>Oa(eo({},"__esModule",{value:!0}),s);var zg={};gp(zg,{AxACE:()=>zn,AxACEOptimizedProgram:()=>Br,AxAI:()=>kr,AxAIAnthropic:()=>sn,AxAIAnthropicModel:()=>cr,AxAIAnthropicVertexModel:()=>pr,AxAIAzureOpenAI:()=>pn,AxAICohere:()=>dn,AxAICohereEmbedModel:()=>fr,AxAICohereModel:()=>gr,AxAIDeepSeek:()=>mn,AxAIDeepSeekModel:()=>hr,AxAIGoogleGemini:()=>gn,AxAIGoogleGeminiEmbedModel:()=>Ao,AxAIGoogleGeminiEmbedTypes:()=>ii,AxAIGoogleGeminiModel:()=>xr,AxAIGoogleGeminiSafetyCategory:()=>yo,AxAIGoogleGeminiSafetyThreshold:()=>bo,AxAIGrok:()=>Sn,AxAIGrokEmbedModels:()=>yi,AxAIGrokModel:()=>Sr,AxAIGroq:()=>hn,AxAIGroqModel:()=>Ar,AxAIHuggingFace:()=>xn,AxAIHuggingFaceModel:()=>Ro,AxAIMistral:()=>An,AxAIMistralEmbedModels:()=>pi,AxAIMistralModel:()=>yr,AxAIOllama:()=>yn,AxAIOpenAI:()=>cn,AxAIOpenAIBase:()=>De,AxAIOpenAIEmbedModel:()=>an,AxAIOpenAIModel:()=>mr,AxAIOpenAIResponses:()=>In,AxAIOpenAIResponsesBase:()=>br,AxAIOpenAIResponsesImpl:()=>bn,AxAIOpenAIResponsesModel:()=>ln,AxAIOpenRouter:()=>Cn,AxAIRefusalError:()=>Fe,AxAIReka:()=>wn,AxAIRekaModel:()=>Cr,AxAIServiceAbortedError:()=>Ee,AxAIServiceAuthenticationError:()=>Ft,AxAIServiceError:()=>Ye,AxAIServiceNetworkError:()=>Ke,AxAIServiceResponseError:()=>Pt,AxAIServiceStatusError:()=>et,AxAIServiceStreamTerminatedError:()=>dt,AxAIServiceTimeoutError:()=>ct,AxAITogether:()=>Tn,AxAITogetherModel:()=>Tr,AxAIWebLLM:()=>Rn,AxAIWebLLMModel:()=>Rr,AxAgent:()=>Qr,AxAgentClarificationError:()=>it,AxApacheTika:()=>is,AxAssertionError:()=>vt,AxBalancer:()=>mo,AxBaseAI:()=>Ge,AxBaseOptimizer:()=>At,AxBootstrapFewShot:()=>Kn,AxContentProcessingError:()=>It,AxDB:()=>Go,AxDBBase:()=>gt,AxDBCloudflare:()=>kn,AxDBManager:()=>Uo,AxDBMemory:()=>Lt,AxDBPinecone:()=>vn,AxDBWeaviate:()=>On,AxDefaultCostTracker:()=>Ur,AxDefaultResultReranker:()=>ss,AxDockerSession:()=>bs,AxEmbeddingAdapter:()=>Is,AxEvalUtil:()=>qu,AxFlow:()=>qr,AxFlowDependencyAnalyzer:()=>Wn,AxFlowExecutionPlanner:()=>Jn,AxFlowSubContextImpl:()=>Xn,AxFlowTypedSubContextImpl:()=>As,AxFluentFieldType:()=>qe,AxFunctionError:()=>Fr,AxFunctionProcessor:()=>Nr,AxGEPA:()=>jt,AxGen:()=>Se,AxGenerateError:()=>Dr,AxHFDataLoader:()=>fs,AxInstanceRegistry:()=>Nn,AxJSRuntime:()=>nn,AxJSRuntimePermission:()=>da,AxJudge:()=>Bt,AxLLMRequestTypeValues:()=>zs,AxLearn:()=>gs,AxMCPClient:()=>Ts,AxMCPHTTPSSETransport:()=>ks,AxMCPStreambleHTTPTransport:()=>Ss,AxMediaNotSupportedError:()=>mt,AxMemory:()=>En,AxMiPRO:()=>xs,AxMockAIService:()=>vo,AxMultiServiceRouter:()=>Oo,AxOptimizedProgramImpl:()=>Mt,AxProgram:()=>Ut,AxPromptTemplate:()=>Xt,AxProviderRouter:()=>Fo,AxRateLimiterTokenUsage:()=>fn,AxSignature:()=>_e,AxSignatureBuilder:()=>Lr,AxSimpleClassifier:()=>ls,AxSimpleClassifierClass:()=>as,AxSpanKindValues:()=>qs,AxStepContextImpl:()=>Gn,AxStopFunctionCallException:()=>Jt,AxStringUtil:()=>os,AxSynth:()=>Hn,AxTestPrompt:()=>us,AxTokenLimitError:()=>lr,AxTraceLogger:()=>jn,agent:()=>np,ai:()=>Bl,ax:()=>nt,axAIAnthropicDefaultConfig:()=>Zs,axAIAnthropicVertexDefaultConfig:()=>gl,axAIAzureOpenAIBestConfig:()=>Al,axAIAzureOpenAICreativeConfig:()=>hl,axAIAzureOpenAIDefaultConfig:()=>ti,axAIAzureOpenAIFastConfig:()=>xl,axAICohereCreativeConfig:()=>wl,axAICohereDefaultConfig:()=>oi,axAIDeepSeekCodeConfig:()=>Tl,axAIDeepSeekDefaultConfig:()=>si,axAIGoogleGeminiDefaultConfig:()=>li,axAIGoogleGeminiDefaultCreativeConfig:()=>vl,axAIGrokBestConfig:()=>Ul,axAIGrokDefaultConfig:()=>$o,axAIHuggingFaceCreativeConfig:()=>Ol,axAIHuggingFaceDefaultConfig:()=>ci,axAIMistralBestConfig:()=>El,axAIMistralDefaultConfig:()=>ko,axAIOllamaDefaultConfig:()=>di,axAIOllamaDefaultCreativeConfig:()=>Ml,axAIOpenAIBestConfig:()=>uo,axAIOpenAICreativeConfig:()=>co,axAIOpenAIDefaultConfig:()=>Kt,axAIOpenAIFastConfig:()=>po,axAIOpenAIResponsesBestConfig:()=>Pl,axAIOpenAIResponsesCreativeConfig:()=>Fl,axAIOpenAIResponsesDefaultConfig:()=>Ir,axAIOpenRouterDefaultConfig:()=>gi,axAIRekaBestConfig:()=>Ll,axAIRekaCreativeConfig:()=>$l,axAIRekaDefaultConfig:()=>wr,axAIRekaFastConfig:()=>Dl,axAITogetherDefaultConfig:()=>hi,axAIWebLLMCreativeConfig:()=>Gl,axAIWebLLMDefaultConfig:()=>Ai,axAnalyzeChatPromptRequirements:()=>_l,axAnalyzeRequestRequirements:()=>Wt,axBaseAIDefaultConfig:()=>be,axBaseAIDefaultCreativeConfig:()=>ze,axBuildActorDefinition:()=>Ms,axBuildResponderDefinition:()=>Ps,axCheckMetricsHealth:()=>jl,axCreateDefaultColorLogger:()=>js,axCreateDefaultOptimizerColorLogger:()=>Ji,axCreateDefaultOptimizerTextLogger:()=>Vu,axCreateDefaultTextLogger:()=>$a,axCreateFlowColorLogger:()=>zr,axCreateFlowTextLogger:()=>rc,axCreateJSRuntime:()=>Ac,axDefaultFlowLogger:()=>oc,axDefaultMetricsConfig:()=>wi,axDefaultOptimizerLogger:()=>Gr,axDefaultOptimizerMetricsConfig:()=>Qi,axGetCompatibilityReport:()=>yl,axGetFormatCompatibility:()=>Il,axGetMetricsConfig:()=>ql,axGetOptimizerMetricsConfig:()=>Qu,axGetProvidersWithMediaSupport:()=>bl,axGlobals:()=>le,axModelInfoAnthropic:()=>dr,axModelInfoCohere:()=>ho,axModelInfoDeepSeek:()=>xo,axModelInfoGoogleGemini:()=>Io,axModelInfoGrok:()=>Lo,axModelInfoGroq:()=>wo,axModelInfoHuggingFace:()=>To,axModelInfoMistral:()=>So,axModelInfoOpenAI:()=>un,axModelInfoOpenAIResponses:()=>lo,axModelInfoReka:()=>Po,axModelInfoTogether:()=>_o,axModelInfoWebLLM:()=>No,axProcessContentForProvider:()=>Mo,axRAG:()=>ap,axScoreProvidersForRequest:()=>go,axSelectOptimalProvider:()=>fo,axSpanAttributes:()=>pe,axSpanEvents:()=>Ct,axUpdateMetricsConfig:()=>zl,axUpdateOptimizerMetricsConfig:()=>Ju,axValidateChatRequestMessage:()=>rn,axValidateChatResponseResult:()=>ao,axValidateProviderCapabilities:()=>ni,axWorkerRuntime:()=>Cs,f:()=>ae,flow:()=>ys,fn:()=>Ou,s:()=>Ku});module.exports=fp(zg);function pt({model:s,modelInfo:e,models:t}){let n=t?.find(u=>u.key===s),r=n&&"model"in n?n.model:s,o=e.find(u=>u.name===s);if(o)return o;let i=e.find(u=>u.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+$/,""),l=e.find(u=>u.name===a);return l||null}var Gs=(()=>{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 Qe(){return Gs.randomUUID()}async function hp(s){let e=new TextEncoder,t=typeof s=="string"?e.encode(s):s,n=await Gs.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("")}var Ds=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 hp(this.data)}};function ut(s){if(s!=="sha256")throw new Error("Only SHA-256 algorithm is supported");return new Ds}function Ea(){return Gs}var to=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 Ns=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)}},Yr=class extends TransformStream{constructor(){super(new Ns)}};var mp={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},gp=globalThis.TextDecoderStream??Yr,Je=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=Ve(),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()}},Xe=class extends Je{constructor(t,n,r,o,i,a,l,u=!0){let c=l?` (after ${l} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:i,...a},{},u);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},qe=class extends Je{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}},Ot=class extends Je{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},st=class extends Je{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"}},nt=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"}},sr=class extends Xe{constructor(e,t,n,r,o,i,a=!0){super(e,t,n,r,o,i,void 0,a),this.name="AxTokenLimitError"}},$e=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"}},Et=class extends Je{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Pe=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()}},it=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()}},ft=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 Ls(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 Ta(s,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**s)*(.75+Math.random()*.5)}function fp(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 hp(){return{startTime:Date.now(),retryCount:0}}function wa(s){s.retryCount++,s.lastRetryTime=Date.now()}function Ra(s,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:s instanceof qe&&!(s instanceof Et)}var Qe=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={...mp,...s.retry},n=s.timeout,r=hp(),o=s.verbose??!1,i=s.includeRequestBodyInErrors??!0,a,l=new URL(s.url),u=`${[l.pathname,s.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${l.search}`,c=new URL(u,l);if(s.corsProxy){let m=c.href;c=new URL(`${s.corsProxy}?url=${encodeURIComponent(m)}`)}let p=Ve();if(s.validateRequest&&!await s.validateRequest(e))throw new Ot("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 $e(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(`
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 Us=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)}},no=class extends TransformStream{constructor(){super(new Us)}};var xp={maxRetries:3,initialDelayMs:1e3,maxDelayMs:6e4,backoffFactor:2,retryableStatusCodes:[500,408,429,502,503,504]},Ap=globalThis.TextDecoderStream??no,Ye=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=Qe(),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()}},et=class extends Ye{constructor(t,n,r,o,i,a,l,u=!0){let c=l?` (after ${l} retries)`:"";super(`HTTP ${t} - ${n}${c}`,r,o,{httpStatus:t,httpStatusText:n,responseBody:i,...a},{},u);this.status=t;this.statusText=n;this.name="AxAIServiceStatusError"}},Ke=class extends Ye{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}},Pt=class extends Ye{constructor(e,t,n,r,o=!0){super(e,t,n,void 0,r,o),this.name="AxAIServiceResponseError"}},dt=class extends Ye{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"}},ct=class extends Ye{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"}},lr=class extends et{constructor(e,t,n,r,o,i,a=!0){super(e,t,n,r,o,i,void 0,a),this.name="AxTokenLimitError"}},Ee=class extends Ye{constructor(e,t,n,r,o=!0){super(`Request aborted${t?`: ${t}`:""}`,e,n,void 0,{abortReason:t,...r},o),this.name="AxAIServiceAbortedError"}},Ft=class extends Ye{constructor(e,t,n,r,o=!0){super("Authentication failed",e,t,n,r,o),this.name="AxAIServiceAuthenticationError"}},Fe=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=Qe()}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()}},mt=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=Qe()}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()}},It=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=Qe()}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 Bs(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 Ma(s,e){return Math.min(e.maxDelayMs,e.initialDelayMs*e.backoffFactor**s)*(.75+Math.random()*.5)}function yp(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 bp(){return{startTime:Date.now(),retryCount:0}}function Pa(s){s.retryCount++,s.lastRetryTime=Date.now()}function Fa(s,e,t,n){return t>=n.maxRetries?!1:e&&n.retryableStatusCodes.includes(e)?!0:s instanceof Ke&&!(s instanceof Ft)}var Xe=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={...xp,...s.retry},n=s.timeout,r=bp(),o=s.verbose??!1,i=s.includeRequestBodyInErrors??!0,a,l=new URL(s.url),u=`${[l.pathname,s.name].filter(Boolean).join("/").replace(/\/+/g,"/")}${l.search}`,c=new URL(u,l);if(s.corsProxy){let m=c.href;c=new URL(`${s.corsProxy}?url=${encodeURIComponent(m)}`)}let p=Qe();if(s.validateRequest&&!await s.validateRequest(e))throw new Pt("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 Ee(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
14
  `,`Method: ${s.put?"PUT":"POST"}
15
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(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 I=await Ls(g);throw new Et(c.href,e,I,{metrics:r},i)}if(g.status===400){let I=await Ls(g),C=I,R=!1;if(C?.error?.code==="context_length_exceeded")R=!0;else if(C?.type==="invalid_request_error"&&(C?.error?.message?.includes("prompt is too long")||C?.error?.message?.includes("max_tokens")||C?.error?.message?.includes("token limit")))R=!0;else if(C?.error?.code===400&&C?.error?.status==="INVALID_ARGUMENT"&&(C?.error?.message?.includes("token")||C?.error?.message?.includes("limit")))R=!0;else{let P=JSON.stringify(I).toLowerCase();(P.includes("token")&&P.includes("limit")||P.includes("context length")||P.includes("prompt is too long"))&&(R=!0)}if(R)throw new sr(g.status,g.statusText,c.href,e,I,{metrics:r},i)}if(g.status>=400&&Ra(new Error,g.status,d,t)){let I=Ta(d,t),C=fp(g.headers.get("Retry-After"));C!==void 0&&C<=t.maxDelayMs&&(I=C,o&&console.log(`[AxAI] Respecting Retry-After header: ${I}ms`)),d++,wa(r),s.span?.addEvent("retry",{attempt:d,delay:I,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(R=>setTimeout(R,I));continue}if(g.status>=400){let I=await Ls(g);throw new Xe(g.status,g.statusText,c.href,e,I,{metrics:r},d>0?d:void 0,i)}if(!s.stream){let I=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 I=await Bs(g);throw new Ft(c.href,e,I,{metrics:r},i)}if(g.status===400){let I=await Bs(g),C=I,R=!1;if(C?.error?.code==="context_length_exceeded")R=!0;else if(C?.type==="invalid_request_error"&&(C?.error?.message?.includes("prompt is too long")||C?.error?.message?.includes("max_tokens")||C?.error?.message?.includes("token limit")))R=!0;else if(C?.error?.code===400&&C?.error?.status==="INVALID_ARGUMENT"&&(C?.error?.message?.includes("token")||C?.error?.message?.includes("limit")))R=!0;else{let M=JSON.stringify(I).toLowerCase();(M.includes("token")&&M.includes("limit")||M.includes("context length")||M.includes("prompt is too long"))&&(R=!0)}if(R)throw new lr(g.status,g.statusText,c.href,e,I,{metrics:r},i)}if(g.status>=400&&Fa(new Error,g.status,d,t)){let I=Ma(d,t),C=yp(g.headers.get("Retry-After"));C!==void 0&&C<=t.maxDelayMs&&(I=C,o&&console.log(`[AxAI] Respecting Retry-After header: ${I}ms`)),d++,Pa(r),s.span?.addEvent("retry",{attempt:d,delay:I,status:g.status,"metrics.startTime":r.startTime,"metrics.retryCount":r.retryCount,"metrics.lastRetryTime":r.lastRetryTime}),await new Promise(R=>setTimeout(R,I));continue}if(g.status>=400){let I=await Bs(g);throw new et(g.status,g.statusText,c.href,e,I,{metrics:r},d>0?d:void 0,i)}if(!s.stream){let I=await g.json();if(o&&console.log(`
19
19
  --- [AxAI API Response] ---
20
20
  `,`Status: ${g.status} ${g.statusText}
21
21
  `,"Body:",JSON.stringify(I,null,2),`
22
22
  -------------------------
23
- `),s.validateResponse&&!await s.validateResponse(I))throw new Ot("Invalid response data",c.href,e,{validation:"response"},i);return s.span?.setAttributes({"response.time":Date.now()-r.startTime,"response.retries":r.retryCount}),I}if(o&&console.log(`
23
+ `),s.validateResponse&&!await s.validateResponse(I))throw new Pt("Invalid response data",c.href,e,{validation:"response"},i);return s.span?.setAttributes({"response.time":Date.now()-r.startTime,"response.retries":r.retryCount}),I}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 Ot("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(I){let C=g.body.getReader(),R=new TextDecoder,P="";async function v(){try{for(;;){let{done:E,value:k}=await C.read();if(E){y=!0,I.close();break}P+=R.decode(k,{stream:!0});let O=P.split(`
28
+ `),!g.body)throw new Pt("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(I){let C=g.body.getReader(),R=new TextDecoder,M="";async function S(){try{for(;;){let{done:O,value:k}=await C.read();if(O){b=!0,I.close();break}M+=R.decode(k,{stream:!0});let _=M.split(`
29
29
 
30
- `);P=O.pop()||"";for(let L of O){if(!L.trim())continue;let F=L.split(`
31
- `),M="",_="message";for(let j of F)j.startsWith("data: ")?M=j.slice(6):j.startsWith("event: ")&&(_=j.slice(7));if(M){if(M==="[DONE]"){I.close();return}try{let j=JSON.parse(M);f=j,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),I.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:",M,j)}}}}}catch(E){let k=E,O={...r,streamDuration:Date.now()-r.startTime};k.name==="AbortError"||k.message?.includes("aborted")?I.error(new st(c.href,e,f,{streamMetrics:O},i)):I.error(new qe(k,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:O},i))}finally{C.releaseLock()}}v()}});let h=new TransformStream({transform(I,C){f=I,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),C.enqueue(I),s.span?.addEvent("stream.chunk",{"stream.chunks":x,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),y=!1;return new ReadableStream({start(I){let C=g.body.pipeThrough(new gp).pipeThrough(new Qr).pipeThrough(h).getReader();async function R(){try{for(;;){let{done:P,value:v}=await C.read();if(P){y||(y=!0,I.close());break}if(y)break;I.enqueue(v)}}catch(P){let v=P,E={...r,streamDuration:Date.now()-r.startTime};throw v.name==="AbortError"||v.message?.includes("aborted")?I.error(new st(c.href,e,f,{streamMetrics:E},i)):v instanceof TypeError&&v.message.includes("cancelled")?I.error(new st(c.href,e,f,{streamMetrics:E,cancelReason:"Stream cancelled by client"},i)):I.error(new qe(v,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:E},i)),v}finally{a&&clearTimeout(a),C.releaseLock()}}R()},cancel(){y=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw s.abortSignal?.aborted?new $e(c.href,s.abortSignal.reason,e,{metrics:r},i):new nt(c.href,n||0,e,{metrics:r},i);let f=g;if(!(g instanceof Je)&&g instanceof Error&&(f=new qe(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 qe&&Ra(f,void 0,d,t)){let x=Ta(d,t);d++,wa(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(b=>setTimeout(b,x));continue}throw f instanceof Je&&(f.context.metrics=r),f}finally{a!==void 0&&clearTimeout(a)}}};var tn=require("@opentelemetry/api");var ie={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 qg=new Ze,Sa=s=>{console.log(s)},ka=(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")}
30
+ `);M=_.pop()||"";for(let L of _){if(!L.trim())continue;let E=L.split(`
31
+ `),P="",$="message";for(let D of E)D.startsWith("data: ")?P=D.slice(6):D.startsWith("event: ")&&($=D.slice(7));if(P){if(P==="[DONE]"){I.close();return}try{let D=JSON.parse(P);f=D,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),I.enqueue(D),s.span?.addEvent("stream.chunk",{"stream.chunks":x,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount,"sse.event.type":$})}catch(D){o&&console.warn("Skipping non-JSON SSE data:",P,D)}}}}}catch(O){let k=O,_={...r,streamDuration:Date.now()-r.startTime};k.name==="AbortError"||k.message?.includes("aborted")?I.error(new dt(c.href,e,f,{streamMetrics:_},i)):I.error(new Ke(k,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:_},i))}finally{C.releaseLock()}}S()}});let h=new TransformStream({transform(I,C){f=I,x++,r.streamChunks=x,r.lastChunkTime=Date.now(),C.enqueue(I),s.span?.addEvent("stream.chunk",{"stream.chunks":x,"stream.duration":Date.now()-r.startTime,"response.retries":r.retryCount})}}),b=!1;return new ReadableStream({start(I){let C=g.body.pipeThrough(new Ap).pipeThrough(new to).pipeThrough(h).getReader();async function R(){try{for(;;){let{done:M,value:S}=await C.read();if(M){b||(b=!0,I.close());break}if(b)break;I.enqueue(S)}}catch(M){let S=M,O={...r,streamDuration:Date.now()-r.startTime};throw S.name==="AbortError"||S.message?.includes("aborted")?I.error(new dt(c.href,e,f,{streamMetrics:O},i)):S instanceof TypeError&&S.message.includes("cancelled")?I.error(new dt(c.href,e,f,{streamMetrics:O,cancelReason:"Stream cancelled by client"},i)):I.error(new Ke(S,c.href,e,"[ReadableStream - consumed during streaming]",{streamMetrics:O},i)),S}finally{a&&clearTimeout(a),C.releaseLock()}}R()},cancel(){b=!0}})}catch(g){if(g instanceof Error&&g.name==="AbortError")throw s.abortSignal?.aborted?new Ee(c.href,s.abortSignal.reason,e,{metrics:r},i):new ct(c.href,n||0,e,{metrics:r},i);let f=g;if(!(g instanceof Ye)&&g instanceof Error&&(f=new Ke(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 Ke&&Fa(f,void 0,d,t)){let x=Ma(d,t);d++,Pa(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(A=>setTimeout(A,x));continue}throw f instanceof Ye&&(f.context.metrics=r),f}finally{a!==void 0&&clearTimeout(a)}}};var on=require("@opentelemetry/api");var le={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 tt=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 nf=new tt,_a=s=>{console.log(s)},Na=(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
32
  ${n(s.content,"magenta")}`;case"function":return`${n("[ FUNCTION RESULT ]","yellow")}
33
33
  ${n(s.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","greenBright")}
34
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(`
@@ -38,11 +38,11 @@ ${n(s.result??"[No result]","yellowDim")}`;case"user":{let r=`${n("[ USER ]","gr
38
38
  `,s.functionCalls.forEach((i,a)=>{let l=typeof i.function.params=="string"?i.function.params:JSON.stringify(i.function.params,null,2);o+=n(`${a+1}. ${i.function.name}(${l}) [id: ${i.id}]`,"yellowDim"),a<(s.functionCalls?.length??0)-1&&(o+=`
39
39
  `)}),o+=`
40
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")}`}},$s=(s=Sa)=>{let e=new Ze,t=e.gray(`${"\u2500".repeat(60)}
41
+ ${n(JSON.stringify(s),"gray")}`}},js=(s=_a)=>{let e=new tt,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((i,a)=>{o+=ka(i,void 0,e),a<r.value.length-1&&(o+=`
45
+ `,r.value.forEach((i,a)=>{o+=Na(i,void 0,e),a<r.value.length-1&&(o+=`
46
46
  ${t}
47
47
  `)}),o+=`
48
48
  ${t}`;break;case"FunctionResults":o=`
@@ -102,10 +102,10 @@ ${t}
102
102
  [ CHAT RESPONSE CITATIONS ]`)}
103
103
  `,r.value.forEach(i=>{o+=`${e.white("- ")}${e.cyan(i.title||i.url)}
104
104
  `,i.description&&(o+=` ${e.gray(i.description)}
105
- `)}),o+=t;break}default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},va=$s(),Oa=(s=Sa)=>{let e="\u2500".repeat(60);return t=>{let n=t,r="";switch(n.name){case"ChatRequestChatPrompt":r=`
105
+ `)}),o+=t;break}default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},La=js(),$a=(s=_a)=>{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,i)=>{r+=ka(o),i<n.value.length-1&&(r+=`
108
+ `,n.value.forEach((o,i)=>{r+=Na(o),i<n.value.length-1&&(r+=`
109
109
  ${e}
110
110
  `)}),r+=`
111
111
  ${e}`;break;case"FunctionResults":r=`
@@ -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)}s(r)}};var ue={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"},ht={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"},Ds=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(Ds||{}),Gs=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(Gs||{});var Us=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()}},Xr=class extends TransformStream{constructor(e,t){super(new Us(e,t))}};function Zr(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 Ea=(s,e,t,n)=>{let r=n?s.filter(i=>i.role!=="system"):[...s];t({name:"ChatRequestChatPrompt",step:e,value:r})};var Ma=(s,e)=>{if(!s.results)return;let t={name:"ChatResponseResults",value:s.results};e(t)};function Pa(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?Zr(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 Fa=(s,e)=>{e({name:"FunctionResults",value:s})},js=(s,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:s})},_a=(s,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:s})},Na=(s,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:s})},La=(s,e,t)=>{t({name:"RefusalError",index:e,error:s})};var $a=(s,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:s})},Da=(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)},Ga=(s,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:s,selectedIndex:e,latency:t})};var to=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},Bt=(...s)=>{let e={};for(let t of s)t&&Object.assign(e,t);return e},eo,Ua=s=>{if(eo)return eo;if(s)return eo=xp(s),eo};var xp=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)"})}),ja=(s,e,t,n,r,o)=>{try{if(s.latencyHistogram){let i=to({operation:e,ai_service:n,...r?{model:r}:{},...o});s.latencyHistogram.record(t,i)}}catch(i){console.warn("Failed to record latency metric:",i)}},Ba=(s,e,t,n,r,o,i,a)=>{let l={operation:e,ai_service:o,...i?{model:i}:{},...a};s.meanLatencyGauge&&s.meanLatencyGauge.record(t,l),s.p95LatencyGauge&&s.p95LatencyGauge.record(n,l),s.p99LatencyGauge&&s.p99LatencyGauge.record(r,l)},za=(s,e,t,n,r)=>{try{if(s.errorCounter){let o=to({operation:e,ai_service:t,...n?{model:n}:{},...r});s.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},qa=(s,e,t,n,r,o)=>{s.errorRateGauge&&s.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Ha=(s,e,t,n,r)=>{s.requestCounter&&s.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},ir=(s,e,t,n,r,o)=>{try{let i=to({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)}},Ka=(s,e,t,n,r,o)=>{t&&s.streamingRequestsCounter&&s.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Wa=(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)},Bs=(s,e,t,n,r,o)=>{s.requestSizeHistogram&&s.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},zs=(s,e,t,n,r,o)=>{s.responseSizeHistogram&&s.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Va=(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)},qs=(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})},Qa=(s,e,t,n,r)=>{s.contextWindowUsageGauge&&s.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},Ya=(s,e,t,n,r)=>{s.timeoutsCounter&&s.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Xa=(s,e,t,n,r)=>{s.abortsCounter&&s.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},Za=(s,e,t,n,r)=>{s.thinkingBudgetUsageCounter&&s.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},el=(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})},Hs=(s,e,t,n,r,o)=>{try{if(t<=0)return;let i=to({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 tl(s){try{return JSON.stringify(s,null,2)}catch{return String(s)}}function xt(s,e={}){let t=[s];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${tl(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${tl(e.item)}`),new Error(t.join(`
168
- `))}function en(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,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(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,l=Array.isArray(o)&&o.length>0;if(!i&&!a&&!l&&xt("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"&&xt("Assistant message content must be a string",{fieldPath:"content",value:n,item:s}),r!==void 0&&!Array.isArray(r)&&xt("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:s}),Array.isArray(r))for(let u=0;u<r.length;u++){let c=r[u];if((!c||typeof c!="object")&&xt("functionCalls entry must be an object",{fieldPath:`functionCalls[${u}]`,value:c,item:s}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&xt("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${u}].id`,value:c.id,item:s}),(!("type"in c)||c.type!=="function")&&xt("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${u}].type`,value:c.type,item:s}),!("function"in c)||!c.function)xt("functionCalls entry must include a function object",{fieldPath:`functionCalls[${u}].function`,value:c.function,item:s});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&xt("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${u}].function.name`,value:p?.name,item:s}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&xt("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${u}].function.params`,value:p.params,item:s})}}if(s.name!==void 0){let u=s.name;(typeof u!="string"||u.trim()==="")&&xt("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:u,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"&&xt("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 no(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 Ks=new Map;function Ap(s){return`${s.providerName}:${s.contentHash}`}function yp(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 bp(s){let e=tt("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)yp(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var be=()=>structuredClone({temperature:0}),je=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Ge=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:i,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??ie.tracer,this.meter=a.meter??ie.meter,this.modelInfo=o,this.models=u,this.id=Ve();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),u&&Ip(u)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=ie.logger??va;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 Ua(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??ie.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??ie.tracer,this.meter=e.meter??ie.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??ie.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(ie.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);ja(o,e,t,this.name,i,a),Ba(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);Ha(o,e,this.name,i,a),t&&za(o,e,this.name,i,a),qa(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:l,cacheCreationTokens:u}=e.tokens,c=this.getMergedCustomLabels(t);r&&ir(n,"input",r,this.name,e.model,c),o&&ir(n,"output",o,this.name,e.model,c),i&&ir(n,"total",i,this.name,e.model,c),a&&ir(n,"thoughts",a,this.name,e.model,c),l&&Hs(n,"read",l,this.name,e.model,c),u&&Hs(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,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(l=>l.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&&Wa(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;Ya(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;Xa(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),l=i?.stream??!1;Ka(r,"chat",l,this.name,o,a);let{hasImages:u,hasAudio:c}=this.detectMultimodalContent(e);el(r,u,c,this.name,o,a);let p=this.calculatePromptLength(e);Ja(r,p,this.name,o,a),Va(r,i?.temperature,i?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&Za(r,this.modelUsage.tokens.thoughtsTokens,this.name,o,a);let d=this.calculateRequestSize(e);if(Bs(r,"chat",d,this.name,o,a),n&&!l){let m=n,g=this.calculateResponseSize(m);if(zs(r,"chat",g,this.name,o,a),m.results)for(let b of m.results)b.functionCalls&&this.recordFunctionCallMetrics(b.functionCalls,this.lastUsedChatModel,a);let f=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);f>0&&Qa(r,f,this.name,o,a);let x=this.estimateCost(this.lastUsedChatModel,m.modelUsage);x>0&&qs(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);Bs(r,"embed",a,this.name,o,i);let l=this.calculateResponseSize(t);zs(r,"embed",l,this.name,o,i);let u=this.estimateCostByName(o,t.modelUsage);u>0&&qs(r,"embed",u,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,l={...i?{thinkingTokenBudget:a,showThoughts:i.showThoughts,stream:i.stream,debug:i.debug,useExpensiveModel:i.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)en(u);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},i=this.modelInfo.find(u=>u.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(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:tn.SpanKind.SERVER,attributes:{[ue.LLM_SYSTEM]:this.name,[ue.LLM_OPERATION_NAME]:"chat",[ue.LLM_REQUEST_MODEL]:n,[ue.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[ue.LLM_REQUEST_TEMPERATURE]:o.temperature,[ue.LLM_REQUEST_TOP_P]:o.topP??"Not set",[ue.LLM_REQUEST_TOP_K]:o.topK??"Not set",[ue.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[ue.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[ue.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[ue.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??tn.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 i=r?.debug??this.#e,a=r?.verbose??this.#t,l;n.functions&&n.functions.length>0&&(l=n.functions.map(y=>this.cleanupFunctionSchema(y)));let u={...n,model:e,functions:l,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,i&&Ea(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(y=>{if(y.role==="assistant"){let{content:I,name:C,cache:R}=y;return{role:"assistant",content:I,name:C,cache:R}}return y.role==="function"?{role:"user",content:y.result}:y}),functions:[]}:u,g=await this.handleContextCaching(e,m,r,o),f=async()=>{if(g?.preparedRequest){let{apiConfig:R,request:P}=g.preparedRequest;return o?.isRecording()&&nl(n,o,this.excludeContentFromTrace),await Qe({name:R.name,url:this.apiURL,localCall:R.localCall,headers:await this.buildHeaders(R.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},P)}let[y,I]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&nl(n,o,this.excludeContentFromTrace),await Qe({name:y.name,url:this.apiURL,localCall:y.localCall,headers:await this.buildHeaders(y.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},I)},x=r?.rateLimiter??this.rt,b=x?await x(f,{modelUsage:this.modelUsage}):await f();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let y=this.aiImpl.createChatStreamResp.bind(this),I=v=>E=>{let k=y(E,v);if(k.sessionId=r?.sessionId,!k.modelUsage){let O=this.aiImpl.getTokenUsage();O&&(k.modelUsage={ai:this.name,model:e,tokens:O})}return this.modelUsage=k.modelUsage,this.recordTokenUsage(k.modelUsage,r?.customLabels),o?.isRecording()&&rl(k,o,this.excludeContentFromTrace),k},C=async v=>{o?.isRecording()&&o.end(),i&&Pa(v,r?.logger??this.logger)};if(typeof window<"u"){let v=b,E={},k=[],O=r?.abortSignal??this.abortSignal;return new ReadableStream({start:L=>{let F=v.getReader(),M=()=>{try{F.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{L.error(new DOMException("Aborted","AbortError"))}catch{L.error(new Error("Aborted"))}};if(O){if(O.aborted){M();return}O.addEventListener("abort",M,{once:!0})}async function _(){try{for(;;){let{done:j,value:H}=await F.read();if(j){C&&await C(k),L.close();break}let D=I(E)(H);D&&(k.push(D),L.enqueue(D))}}catch(j){if(L.error(j),o?.isRecording())try{o.end()}catch{}}finally{if(F.releaseLock(),O)try{O.removeEventListener("abort",M)}catch{}}}_()}})}return b.pipeThrough(new Xr(I({}),C))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let h=this.aiImpl.createChatResp(b);if(h.sessionId=r?.sessionId,!h.modelUsage){let y=this.aiImpl.getTokenUsage();y&&(h.modelUsage={ai:this.name,model:e,tokens:y})}return h.modelUsage&&(this.modelUsage=h.modelUsage,this.recordTokenUsage(h.modelUsage,r?.customLabels)),o?.isRecording()&&(rl(h,o,this.excludeContentFromTrace),o.end()),i&&Ma(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(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:tn.SpanKind.SERVER,attributes:{[ue.LLM_SYSTEM]:this.name,[ue.LLM_OPERATION_NAME]:"embeddings",[ue.LLM_REQUEST_MODEL]:n}},t?.traceContext??tn.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,l={...t,embedModel:e};this.lastUsedEmbedModel=e,i&&$a(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(ht.GEN_AI_USAGE,{[ue.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[ue.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[ue.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),i&&Da(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 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=bp(t.chatPrompt);if(!p||p===tt("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=Ap(d),g=Date.now(),f=o.registry,x=f?await f.get(m):Ks.get(m);if(x&&x.expiresAt>g){if(x.expiresAt-g<u*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(x.cacheName,l),n,r);let I={cacheName:x.cacheName,expiresAt:g+l*1e3,tokenCount:x.tokenCount};f?await f.set(m,I):Ks.set(m,{...I,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 y=await this.executeCacheOperation(h,n,r);if(y){let I={cacheName:y.name,expiresAt:new Date(y.expiresAt).getTime(),tokenCount:y.tokenCount};return f?await f.set(m,I):Ks.set(m,{...I,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,y.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 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 nl(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(ht.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(ht.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(ht.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let i={id:o.functionId};t||(i.content=o.result),e.addEvent(ht.GEN_AI_TOOL_MESSAGE,i);break}}let r={};t||(r.content=n.join(`
169
- `)),e.addEvent(ht.GEN_AI_USER_MESSAGE,r)}function rl(s,e,t){if(s.modelUsage?.tokens){let n=s.modelUsage.tokens.thoughtsTokens?{[ue.LLM_USAGE_THOUGHTS_TOKENS]:s.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(ht.GEN_AI_USAGE,{[ue.LLM_USAGE_INPUT_TOKENS]:s.modelUsage.tokens.promptTokens,[ue.LLM_USAGE_OUTPUT_TOKENS]:s.modelUsage.tokens.completionTokens??0,[ue.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(ht.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(i,null,2)})}}function ol(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 Ip(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 ar=(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))(ar||{}),lr=(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))(lr||{});var ur=[{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 Ft=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,Ft(r)]))),e.items&&(e.items=Ft(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>Ft(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>Ft(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>Ft(n))),e},Vs=()=>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"},...be()}),il=()=>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"},...be()}),Ws=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),l=e.chatPrompt.filter(_=>_.role==="system"),u=l.map((_,j)=>({type:"text",text:_.content,..._.cache||a&&j===l.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter(_=>_.role!=="system"),p=e.functions?.map((_,j,H)=>{let D={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},Y=_.parameters?Ft(_.parameters):void 0;return Y===void 0||Y&&typeof Y=="object"&&Object.keys(Y).length===0?Y={...D}:Y&&typeof Y=="object"&&Y.type==="object"&&(!("properties"in Y)||!Y.properties||Object.keys(Y.properties).length===0)&&(Y={...Y,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:_.name,description:_.description,input_schema:Y,..._.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?Ft(_.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,b=e.modelConfig?.temperature,h=e.modelConfig?.topP,y=e.modelConfig?.topK??this.config.topK,I=e.modelConfig?.n??this.config.n;if(I&&I>1)throw new Error("Anthropic does not support sampling (n > 1)");let C=_=>_.includes("claude-opus-4-6"),R=_=>_.includes("claude-opus-4-5"),P,v,E=n;if(t?.thinkingTokenBudget){let _=this.config.thinkingTokenBudgetLevels,j=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")P=void 0,v=void 0;else{let H=t.thinkingTokenBudget;if(C(E))P={type:"adaptive"},v={effort:j?.[H]??"medium"};else if(R(E)){P={type:"enabled",budget_tokens:_?.[H]??1e4};let Y=j?.[H]??"medium";Y==="max"&&(Y="high"),v={effort:Y}}else P={type:"enabled",budget_tokens:_?.[H]??1e4}}}let O=Cp(c,!!P);O.some(_=>_.role==="assistant"&&Array.isArray(_.content)&&_.content.length>0&&_.content[0]?.type==="tool_use")&&(P=void 0,v=void 0);let F;if(this.usedStructuredOutput=!1,e.responseFormat&&e.responseFormat.type==="json_schema"&&e.responseFormat.schema){let _=e.responseFormat.schema.schema||e.responseFormat.schema;F={type:"json_schema",schema:Ft(_)},this.usedStructuredOutput=!0}let M={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...f?{max_tokens:f}:{},...x&&x.length>0?{stop_sequences:x}:{},...b!==void 0&&!P?{temperature:b}:{},...h!==void 0&&(!P||h>=.95)?{top_p:h}:{},...y&&!P?{top_k:y}:{},...i,...g?{tools:g}:{},...r?{stream:!0}:{},...u?{system:u}:{},...P?{thinking:P}:{},...v?{output_config:v}:{},...F?{output_format:F}:{},messages:O};return[o,M]};createChatResp=e=>{if(e.type==="error")throw new Pe(e.error.message,void 0,void 0);let t=sl(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 l={index:0,id:e.id,finishReason:t};r&&(l.content=r),i.length>0&&(l.thoughtBlocks=i,l.thought=i.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 Pe(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:sl(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},nn=class s extends Ge{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,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={...Vs(),...r},p=new Ws(c,a),d=g=>{let f=ot({model:g,modelInfo:ur,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 b={};x.maxTokens!==void 0&&(b.maxTokens=x.maxTokens),x.temperature!==void 0&&(b.temperature=x.temperature),x.topP!==void 0&&(b.topP=x.topP),x.topK!==void 0&&(b.topK=x.topK),x.presencePenalty!==void 0&&(b.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(b.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(b.stopSequences=x.stopSequences),x.endSequences!==void 0&&(b.endSequences=x.endSequences),x.stream!==void 0&&(b.stream=x.stream),x.n!==void 0&&(b.n=x.n);let h={...f};Object.keys(b).length>0&&(h.modelConfig={...f.modelConfig??{},...b});let y=x.thinking?.thinkingTokenBudget;if(typeof y=="number"){let I=c.thinkingTokenBudgetLevels,C=[["minimal",I?.minimal??1024],["low",I?.low??5e3],["medium",I?.medium??1e4],["high",I?.high??2e4],["highest",I?.highest??32e3]],R="minimal",P=Number.POSITIVE_INFINITY;for(let[v,E]of C){let k=Math.abs(y-E);k<P&&(P=k,R=v)}h.thinkingTokenBudget=R}return x.thinking?.includeThoughts!==void 0&&(h.showThoughts=!!x.thinking.includeThoughts),h});super(p,{name:"Anthropic",apiURL:l,headers:u,modelInfo:ur,defaults:{model:c.model},options:o,supportFor:d,models:m??i})}};function Cp(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 l of a)l.encrypted?i.push(l.signature?{type:"redacted_thinking",data:l.data,signature:l.signature}:{type:"redacted_thinking",data:l.data}):i.push(l.signature?{type:"thinking",thinking:l.data,signature:l.signature}:{type:"thinking",thinking:l.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(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)&&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 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=Tp(t);return wp(n)}function Tp(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 wp(s){return s.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function sl(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 cr=(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))(cr||{}),rn=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(rn||{});var on=(G=>(G.GPT4="gpt-4",G.GPT41="gpt-4.1",G.GPT41Mini="gpt-4.1-mini",G.GPT41Nano="gpt-4.1-nano",G.GPT4O="gpt-4o",G.GPT4OMini="gpt-4o-mini",G.GPT4ChatGPT4O="chatgpt-4o-latest",G.GPT4Turbo="gpt-4-turbo",G.GPT35Turbo="gpt-3.5-turbo",G.GPT35TurboInstruct="gpt-3.5-turbo-instruct",G.GPT35TextDavinci002="text-davinci-002",G.GPT3TextBabbage002="text-babbage-002",G.GPT3TextAda001="text-ada-001",G.GPT5="gpt-5",G.GPT5Nano="gpt-5-nano",G.GPT5Mini="gpt-5-mini",G.GPT5Chat="gpt-5-chat",G.GPT5ChatLatest="gpt-5-chat-latest",G.GPT5Codex="gpt-5-codex",G.GPT5Pro="gpt-5-pro",G.GPT51="gpt-5.1",G.GPT51ChatLatest="gpt-5.1-chat-latest",G.GPT51Codex="gpt-5.1-codex",G.GPT51CodexMini="gpt-5.1-codex-mini",G.GPT51CodexMax="gpt-5.1-codex-max",G.GPT52="gpt-5.2",G.GPT52ChatLatest="gpt-5.2-chat-latest",G.GPT52Codex="gpt-5.2-codex",G.GPT52Pro="gpt-5.2-pro",G.O1Pro="o1-pro",G.O1="o1",G.O1Mini="o1-mini",G.O3Pro="o3-pro",G.O3="o3",G.O3Mini="o3-mini",G.O4Mini="o4-mini",G))(on||{});var sn=[{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}],ro=[{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 Rp=s=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(s)||e.includes(s)},zt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...be()}),oo=()=>structuredClone({...zt(),model:"gpt-5"}),so=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...je()}),io=()=>({...zt(),model:"gpt-5-nano"}),Js=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=Sp(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,u=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=Rp(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}:{},...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 Pe(a.message.refusal,e.model,e.id);let l=al(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 i=t;return i.indexIdMap||(i.indexIdMap={}),{results:o.map(({index:l,delta:{content:u,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:f})=>{if(p)throw new Pe(p,void 0,n);let x=al(f),b=d?.map(({id:h,index:y,function:{name:I,arguments:C}})=>{typeof h=="string"&&typeof y=="number"&&!i.indexIdMap[y]&&(i.indexIdMap[y]=h);let R=i.indexIdMap[y];return R?{id:R,type:"function",function:{name:I,params:C}}:null}).filter(h=>h!==null);return{index:l,content:u??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:b,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)}}},al=s=>{switch(s){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function Sp(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 De=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:i,chatReqUpdater:a,supportFor:l}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new Js(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:i})}},an=class extends De{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=[...sn,...i??[]];let a=u=>{let c=ot({model:u,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}},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 f=p?.thinking?.thinkingTokenBudget;if(typeof f=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],b="minimal",h=Number.POSITIVE_INFINITY;for(let[y,I]of x){let C=Math.abs(f-I);C<h&&(h=C,b=y)}g.thinkingTokenBudget=b}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...zt(),...n},options:r,modelInfo:i,models:l??o,supportFor:a}),super.setName("OpenAI")}};var kp=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},vp=s=>{let e=s.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},Qs=zt,ll=so,ul=io,cl=oo,ln=class extends De{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:i,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={...Qs(),...o},c=kp(r),p=vp(c);l=[...sn,...l??[]];let d=g=>{let f=ot({model:g,modelInfo:l,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:u,options:i,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 ao=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.");Op(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 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 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,l=e.model;if((n||o||i)&&(a=this.services.filter(p=>{let d=p.getFeatures(l);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 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 Et:throw p;case Xe:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case qe:break;case Ot:break;case st:break;case nt: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 Xe&&![408,429,500,502,503,504].includes(r.status)||r instanceof Et)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 Op(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(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(s){let e=!1,t=!1,n=!1,r=!1,o=!1,i=!1,a=!1,l=new Set,u=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(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 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:l,estimatedTokens:u}}function Ys(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 lo(s,e){return s.map(t=>{let n=t.getFeatures(),r=Ys(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 uo(s,e,t={}){if(e.length===0)throw new Error("No providers available");let n=qt(s),r=lo(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 pl(s,e){let t=qt(s),n=lo(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 dl(s,e){return s.filter(t=>t.getFeatures().media[e].supported)}function ml(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 pr=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(pr||{}),dr=(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))(dr||{});var co=[{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 Ep(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 Zs=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...be()}),fl=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...je()}),Xs=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=Mp(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:Ep(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: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[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}}},un=class extends Ge{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...Zs(),...t},i=new Xs(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(i,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:co,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 Mp(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=gl(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=gl(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 gl(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 mr=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(mr||{});var po=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var ei=()=>structuredClone({model:"deepseek-chat",...be()}),hl=()=>structuredClone({model:"deepseek-coder",...je()}),cn=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 i={...ei(),...t};o=[...po,...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 gr=(I=>(I.Gemini31Pro="gemini-3.1-pro-preview",I.Gemini3FlashLite="gemini-3.1-flash-lite-preview",I.Gemini3Flash="gemini-3-flash-preview",I.Gemini3Pro="gemini-3.1-pro-preview",I.Gemini3ProImage="gemini-3-pro-image-preview",I.Gemini25Pro="gemini-2.5-pro",I.Gemini25Flash="gemini-2.5-flash",I.Gemini25FlashLite="gemini-2.5-flash-lite",I.Gemini20Flash="gemini-2.0-flash",I.Gemini20FlashLite="gemini-2.0-flash-lite",I.Gemini20ProExp="gemini-2.0-pro-exp-02-05",I.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",I.Gemini1Pro="gemini-1.0-pro",I.Gemini15Flash="gemini-1.5-flash",I.Gemini15Flash002="gemini-1.5-flash-002",I.Gemini15Flash8B="gemini-1.5-flash-8b",I.Gemini15Pro="gemini-1.5-pro",I.GeminiFlashLatest="gemini-flash-latest",I.GeminiFlashLiteLatest="gemini-flash-lite-latest",I.GeminiProLatest="gemini-pro-latest",I))(gr||{}),mo=(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))(mo||{}),go=(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))(go||{}),fo=(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))(fo||{}),ti=(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))(ti||{}),xl=["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 ho=[{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 Ht=s=>s.includes("gemini-3"),Al=s=>s.includes("gemini-3")&&s.includes("pro"),xo=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,xo(n)]))),e.items&&(e.items=xo(e.items)),e},yl=[{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"}],ri=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:yl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...be()}),bl=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:yl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...je()}),ni=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(Ht(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(Al(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?"&":"?",y=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${h}key=${y}`}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,l=[],u=e.chatPrompt.filter(h=>h.role!=="system");for(let h=0;h<u.length;h++){let y=u[h];switch(y.role){case"user":{let I=Array.isArray(y.content)?y.content.map((C,R)=>{switch(C.type){case"text":return{text:C.text};case"image":return{inlineData:{mimeType:C.mimeType,data:C.image}};case"audio":return{inlineData:{mimeType:`audio/${C.format??"mp3"}`,data:C.data}};case"file":return"fileUri"in C?{fileData:{mimeType:C.mimeType,fileUri:C.fileUri}}:{inlineData:{mimeType:C.mimeType,data:C.data}};default:throw new Error(`Chat prompt content type not supported (index: ${R})`)}}):[{text:y.content}];l.push({role:"user",parts:I});break}case"assistant":{let I=[],C=y.thoughtBlocks,R=y.functionCalls&&y.functionCalls.length>0,P=C?.[0],v=C?.map(k=>k.data).join("")??"",E=P?.signature;if(v&&I.push({...R?{}:{thought:!0},text:v,...E&&!R?{thought_signature:E}:{}}),y.functionCalls){let k=y.functionCalls.map((O,L)=>{let F;if(typeof O.function.params=="string"){let _=O.function.params;if(_.trim().length===0)F={};else try{F=JSON.parse(_)}catch{throw new Error(`Failed to parse function params JSON: ${_}`)}}else F=O.function.params;let M={functionCall:{name:O.function.name,args:F}};return E&&L===0&&(M.thought_signature=E),M});I.push(...k)}if(y.content&&I.push({text:y.content}),I.length===0)throw new Error("Assistant content is empty");l.push({role:"model",parts:I});break}case"function":{let I=[],C=y,R=h;for(;;){if(!("functionId"in C))throw new Error(`Chat prompt functionId is empty (index: ${R})`);if(I.push({functionResponse:{name:C.functionId,response:{result:C.result}}}),R+1<u.length&&u[R+1].role==="function")R++,C=u[R];else break}h=R,l.push({role:"user",parts:I});break}default:throw new Error(`Invalid role: ${JSON.stringify(y)} (index: ${h})`)}}let c=[];if(e.functions&&e.functions.length>0){let h=e.functions.map(y=>{let I={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},C=y.parameters?xo(y.parameters):void 0;return C===void 0||C&&typeof C=="object"&&Object.keys(C).length===0?C={...I}:C&&typeof C=="object"&&C.type==="object"&&(!("properties"in C)||!C.properties||Object.keys(C.properties).length===0)&&(C={...C,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:y.name,description:y.description,parameters:C}});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,y=h?.enableWidget!==void 0?{enableWidget:h.enableWidget}:{};c.push({google_maps:y})}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&&Ht(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let h=this.getEffectiveMappings(n),y=h.thinkingTokenBudgetLevels;if(Ht(n)){let C=Al(n),R=h.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=R?.minimal??"minimal";else{let P=t.thinkingTokenBudget,v=R?.[P];v||(v=P==="highest"?"high":P),m.thinkingLevel=v}if(C&&m.thinkingLevel){let P=m.thinkingLevel;P!=="low"&&P!=="high"&&(m.thinkingLevel=P==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=y?.minimal??200;break;case"low":m.thinkingBudget=y?.low??800;break;case"medium":m.thinkingBudget=y?.medium??5e3;break;case"high":m.thinkingBudget=y?.high??1e4;break;case"highest":m.thinkingBudget=y?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,Ht(n)||delete m.thinkingLevel,Ht(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(Ht(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=xo(h)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(f.responseMimeType="application/json");let x=this.config.safetySettings;return[o,{contents:l,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 Pe("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Pe("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Pe("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Pe("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Pe("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Pe("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Pe("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Pe("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Pe("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return i;for(let u of o.content.parts){if("text"in u){if("thought"in u&&u.thought||u.thought===!0){i.thought=u.text;let c=u.thoughtSignature||u.thought_signature;i.thoughtBlocks||(i.thoughtBlocks=[]),i.thoughtBlocks.push({data:u.text,encrypted:!1,...c?{signature:c}:{}})}else i.content=u.text;continue}if("functionCall"in u){let c=u.thoughtSignature||u.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: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;i.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&&(i.citations=[...i.citations??[],...u])}typeof l.googleMapsWidgetContextToken=="string"&&(t=l.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 xl.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 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 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),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"};Ht(r)&&(u.temperature===void 0||u.temperature<1)&&(u.temperature=1);let c=this.config.safetySettings,p={contents:i,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 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 l of i.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 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}}},pn=class s extends Ge{static create(e){return new s(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:i,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 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={...ri(),...o},m=new ni(d,u,r,e,i);l=[...ho,...l??[]];let g=x=>{let b=ot({model:x,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:b?.supported?.thinkingBudget??!1,hasShowThoughts:b?.supported?.showThoughts??!1,structuredOutputs:b?.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:b?.supported?.thinkingBudget??!1,multiTurn:!0}},f=a?.map(x=>{let b=x,h=b?.config;if(!h)return x;let y={};h.maxTokens!==void 0&&(y.maxTokens=h.maxTokens),h.temperature!==void 0&&(y.temperature=h.temperature),h.topP!==void 0&&(y.topP=h.topP),h.topK!==void 0&&(y.topK=h.topK),h.presencePenalty!==void 0&&(y.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(y.frequencyPenalty=h.frequencyPenalty),h.stopSequences!==void 0&&(y.stopSequences=h.stopSequences),h.endSequences!==void 0&&(y.endSequences=h.endSequences),h.stream!==void 0&&(y.stream=h.stream),h.n!==void 0&&(y.n=h.n);let I={...b};Object.keys(y).length>0&&(I.modelConfig={...b.modelConfig??{},...y});let C=h.thinking?.thinkingTokenBudget;if(typeof C=="number"){let R=d.thinkingTokenBudgetLevels,P=[["minimal",R?.minimal??200],["low",R?.low??800],["medium",R?.medium??5e3],["high",R?.high??1e4],["highest",R?.highest??24500]],v="minimal",E=Number.POSITIVE_INFINITY;for(let[k,O]of P){let L=Math.abs(C-O);L<E&&(E=L,v=k)}I.thinkingTokenBudget=v}return h.thinking?.includeThoughts!==void 0&&(I.showThoughts=!!h.thinking.includeThoughts),h.thinkingLevelMapping&&(I.thinkingLevelMapping=h.thinkingLevelMapping),h.thinkingTokenBudgetLevels&&(I.thinkingTokenBudgetLevels=h.thinkingTokenBudgetLevels),I});f?m.setModels(f):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:i,supportFor:g,models:f??a})}};var Pp=new Ze,dn=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(Pp.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 fr=(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))(fr||{});var Ao=[{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 Fp=()=>structuredClone({model:"llama-3.3-70b-versatile",...be()}),mn=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 i={...Fp(),...t},a={...n,streamingUsage:!1};o=[...Ao,...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:i,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 dn(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 yo=[];var bo=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(bo||{});var si=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...be()}),Il=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...je()}),oi=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 pe={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"},Ct={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"},zs=(r=>(r.COMPLETION="completion",r.CHAT="chat",r.RERANK="rerank",r.UNKNOWN="unknown",r))(zs||{}),qs=(o=>(o.WORKFLOW="workflow",o.TASK="task",o.AGENT="agent",o.TOOL="tool",o.UNKNOWN="unknown",o))(qs||{});var Hs=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()}},ro=class extends TransformStream{constructor(e,t){super(new Hs(e,t))}};function oo(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 Da=(s,e,t,n)=>{let r=n?s.filter(i=>i.role!=="system"):[...s];t({name:"ChatRequestChatPrompt",step:e,value:r})};var Ga=(s,e)=>{if(!s.results)return;let t={name:"ChatResponseResults",value:s.results};e(t)};function Ua(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?oo(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 Ba=(s,e)=>{e({name:"FunctionResults",value:s})},Ks=(s,e,t,n)=>{n({name:"FunctionError",index:e,fixingInstructions:t,error:s})},ja=(s,e,t,n)=>{n({name:"ValidationError",index:e,fixingInstructions:t,error:s})},za=(s,e,t,n)=>{n({name:"AssertionError",index:e,fixingInstructions:t,error:s})},qa=(s,e,t)=>{t({name:"RefusalError",index:e,error:s})};var Ha=(s,e,t)=>{t({name:"EmbedRequest",embedModel:e,value:s})},Ka=(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)},Wa=(s,e,t,n)=>{n({name:"ResultPickerUsed",sampleCount:s,selectedIndex:e,latency:t})};var io=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},Ht=(...s)=>{let e={};for(let t of s)t&&Object.assign(e,t);return e},so,Va=s=>{if(so)return so;if(s)return so=Ip(s),so};var Ip=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)"})}),Ja=(s,e,t,n,r,o)=>{try{if(s.latencyHistogram){let i=io({operation:e,ai_service:n,...r?{model:r}:{},...o});s.latencyHistogram.record(t,i)}}catch(i){console.warn("Failed to record latency metric:",i)}},Qa=(s,e,t,n,r,o,i,a)=>{let l={operation:e,ai_service:o,...i?{model:i}:{},...a};s.meanLatencyGauge&&s.meanLatencyGauge.record(t,l),s.p95LatencyGauge&&s.p95LatencyGauge.record(n,l),s.p99LatencyGauge&&s.p99LatencyGauge.record(r,l)},Ya=(s,e,t,n,r)=>{try{if(s.errorCounter){let o=io({operation:e,ai_service:t,...n?{model:n}:{},...r});s.errorCounter.add(1,o)}}catch(o){console.warn("Failed to record error metric:",o)}},Xa=(s,e,t,n,r,o)=>{s.errorRateGauge&&s.errorRateGauge.record(t*100,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Za=(s,e,t,n,r)=>{s.requestCounter&&s.requestCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},ur=(s,e,t,n,r,o)=>{try{let i=io({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)}},el=(s,e,t,n,r,o)=>{t&&s.streamingRequestsCounter&&s.streamingRequestsCounter.add(1,{operation:e,ai_service:n,...r?{model:r}:{},...o})},tl=(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)},Ws=(s,e,t,n,r,o)=>{s.requestSizeHistogram&&s.requestSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},Vs=(s,e,t,n,r,o)=>{s.responseSizeHistogram&&s.responseSizeHistogram.record(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},nl=(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)},Js=(s,e,t,n,r,o)=>{s.estimatedCostCounter&&s.estimatedCostCounter.add(t,{operation:e,ai_service:n,...r?{model:r}:{},...o})},rl=(s,e,t,n,r)=>{s.promptLengthHistogram&&s.promptLengthHistogram.record(e,{ai_service:t,...n?{model:n}:{},...r})},ol=(s,e,t,n,r)=>{s.contextWindowUsageGauge&&s.contextWindowUsageGauge.record(e,{ai_service:t,...n?{model:n}:{},...r})},sl=(s,e,t,n,r)=>{s.timeoutsCounter&&s.timeoutsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},il=(s,e,t,n,r)=>{s.abortsCounter&&s.abortsCounter.add(1,{operation:e,ai_service:t,...n?{model:n}:{},...r})},al=(s,e,t,n,r)=>{s.thinkingBudgetUsageCounter&&s.thinkingBudgetUsageCounter.add(e,{ai_service:t,...n?{model:n}:{},...r})},ll=(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})},Qs=(s,e,t,n,r,o)=>{try{if(t<=0)return;let i=io({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 ul(s){try{return JSON.stringify(s,null,2)}catch{return String(s)}}function wt(s,e={}){let t=[s];throw e.fieldPath!==void 0&&t.push(`Field: ${e.fieldPath}`),e.value!==void 0&&t.push(`Value: ${ul(e.value)}`),e.note&&t.push(`Note: ${e.note}`),e.item!==void 0&&t.push(`Chat item: ${ul(e.item)}`),new Error(t.join(`
168
+ `))}function rn(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,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(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,l=Array.isArray(o)&&o.length>0;if(!i&&!a&&!l&&wt("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"&&wt("Assistant message content must be a string",{fieldPath:"content",value:n,item:s}),r!==void 0&&!Array.isArray(r)&&wt("Assistant message functionCalls must be an array when provided",{fieldPath:"functionCalls",value:r,item:s}),Array.isArray(r))for(let u=0;u<r.length;u++){let c=r[u];if((!c||typeof c!="object")&&wt("functionCalls entry must be an object",{fieldPath:`functionCalls[${u}]`,value:c,item:s}),(!("id"in c)||typeof c.id!="string"||c.id.trim()==="")&&wt("functionCalls entry must include a non-empty string id",{fieldPath:`functionCalls[${u}].id`,value:c.id,item:s}),(!("type"in c)||c.type!=="function")&&wt("functionCalls entry must have type 'function'",{fieldPath:`functionCalls[${u}].type`,value:c.type,item:s}),!("function"in c)||!c.function)wt("functionCalls entry must include a function object",{fieldPath:`functionCalls[${u}].function`,value:c.function,item:s});else{let p=c.function;(!("name"in p)||typeof p.name!="string"||p.name.trim()==="")&&wt("functionCalls entry must include a non-empty function name",{fieldPath:`functionCalls[${u}].function.name`,value:p?.name,item:s}),p.params!==void 0&&typeof p.params!="string"&&typeof p.params!="object"&&wt("functionCalls entry params must be a string or object when provided",{fieldPath:`functionCalls[${u}].function.params`,value:p.params,item:s})}}if(s.name!==void 0){let u=s.name;(typeof u!="string"||u.trim()==="")&&wt("Assistant message name must be a non-empty string when provided",{fieldPath:"name",value:u,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"&&wt("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 ao(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 Ys=new Map;function Cp(s){return`${s.providerName}:${s.contentHash}`}function wp(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 Tp(s){let e=ut("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)wp(e,o)}else r.role==="assistant"&&r.content&&e.update(`assistant:${r.content}`)}return e.digest("hex")}var be=()=>structuredClone({temperature:0}),ze=()=>structuredClone({temperature:.4,frequencyPenalty:.2}),Ge=class{constructor(e,{name:t,apiURL:n,headers:r,modelInfo:o,defaults:i,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??le.tracer,this.meter=a.meter??le.meter,this.modelInfo=o,this.models=u,this.id=Qe();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),u&&Rp(u)}#e=!1;#t=!1;rt;fetch;tracer;meter;timeout;excludeContentFromTrace;models;abortSignal;logger=le.logger??La;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 Va(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??le.debug??!1,this.#t=e.verbose??!1,this.rt=e.rateLimiter,this.fetch=e.fetch,this.timeout=e.timeout,this.tracer=e.tracer??le.tracer,this.meter=e.meter??le.meter,this.excludeContentFromTrace=e.excludeContentFromTrace,this.abortSignal=e.abortSignal,this.logger=e.logger??le.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 Ht(le.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);Ja(o,e,t,this.name,i,a),Qa(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);Za(o,e,this.name,i,a),t&&Ya(o,e,this.name,i,a),Xa(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:l,cacheCreationTokens:u}=e.tokens,c=this.getMergedCustomLabels(t);r&&ur(n,"input",r,this.name,e.model,c),o&&ur(n,"output",o,this.name,e.model,c),i&&ur(n,"total",i,this.name,e.model,c),a&&ur(n,"thoughts",a,this.name,e.model,c),l&&Qs(n,"read",l,this.name,e.model,c),u&&Qs(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,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(l=>l.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&&tl(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;sl(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;il(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),l=i?.stream??!1;el(r,"chat",l,this.name,o,a);let{hasImages:u,hasAudio:c}=this.detectMultimodalContent(e);ll(r,u,c,this.name,o,a);let p=this.calculatePromptLength(e);rl(r,p,this.name,o,a),nl(r,i?.temperature,i?.maxTokens,this.name,o,a),t?.thinkingTokenBudget&&this.modelUsage?.tokens?.thoughtsTokens&&al(r,this.modelUsage.tokens.thoughtsTokens,this.name,o,a);let d=this.calculateRequestSize(e);if(Ws(r,"chat",d,this.name,o,a),n&&!l){let m=n,g=this.calculateResponseSize(m);if(Vs(r,"chat",g,this.name,o,a),m.results)for(let A of m.results)A.functionCalls&&this.recordFunctionCallMetrics(A.functionCalls,this.lastUsedChatModel,a);let f=this.calculateContextWindowUsage(this.lastUsedChatModel,m.modelUsage);f>0&&ol(r,f,this.name,o,a);let x=this.estimateCost(this.lastUsedChatModel,m.modelUsage);x>0&&Js(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);Ws(r,"embed",a,this.name,o,i);let l=this.calculateResponseSize(t);Vs(r,"embed",l,this.name,o,i);let u=this.estimateCostByName(o,t.modelUsage);u>0&&Js(r,"embed",u,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,l={...i?{thinkingTokenBudget:a,showThoughts:i.showThoughts,stream:i.stream,debug:i.debug,useExpensiveModel:i.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)rn(u);let r=this.getModelByKey(e.model),o={...this.aiImpl.getModelConfig(),...r?r.modelConfig:void 0,...e.modelConfig},i=this.modelInfo.find(u=>u.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(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:on.SpanKind.SERVER,attributes:{[pe.LLM_SYSTEM]:this.name,[pe.LLM_OPERATION_NAME]:"chat",[pe.LLM_REQUEST_MODEL]:n,[pe.LLM_REQUEST_MAX_TOKENS]:o.maxTokens??"Not set",[pe.LLM_REQUEST_TEMPERATURE]:o.temperature,[pe.LLM_REQUEST_TOP_P]:o.topP??"Not set",[pe.LLM_REQUEST_TOP_K]:o.topK??"Not set",[pe.LLM_REQUEST_FREQUENCY_PENALTY]:o.frequencyPenalty??"Not set",[pe.LLM_REQUEST_PRESENCE_PENALTY]:o.presencePenalty??"Not set",[pe.LLM_REQUEST_STOP_SEQUENCES]:o.stopSequences?.join(", ")??"Not set",[pe.LLM_REQUEST_LLM_IS_STREAMING]:o.stream??"Not set"}},t?.traceContext??on.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 i=r?.debug??this.#e,a=r?.verbose??this.#t,l;n.functions&&n.functions.length>0&&(l=n.functions.map(b=>this.cleanupFunctionSchema(b)));let u={...n,model:e,functions:l,modelConfig:t};this.lastUsedChatModel=e,this.lastUsedModelConfig=t,i&&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(b=>{if(b.role==="assistant"){let{content:I,name:C,cache:R}=b;return{role:"assistant",content:I,name:C,cache:R}}return b.role==="function"?{role:"user",content:b.result}:b}),functions:[]}:u,g=await this.handleContextCaching(e,m,r,o),f=async()=>{if(g?.preparedRequest){let{apiConfig:R,request:M}=g.preparedRequest;return o?.isRecording()&&cl(n,o,this.excludeContentFromTrace),await Xe({name:R.name,url:this.apiURL,localCall:R.localCall,headers:await this.buildHeaders(R.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},M)}let[b,I]=await this.aiImpl.createChatReq(m,r);return o?.isRecording()&&cl(n,o,this.excludeContentFromTrace),await Xe({name:b.name,url:this.apiURL,localCall:b.localCall,headers:await this.buildHeaders(b.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},I)},x=r?.rateLimiter??this.rt,A=x?await x(f,{modelUsage:this.modelUsage}):await f();if(t.stream){if(!this.aiImpl.createChatStreamResp)throw new Error("createChatStreamResp not implemented");let b=this.aiImpl.createChatStreamResp.bind(this),I=S=>O=>{let k=b(O,S);if(k.sessionId=r?.sessionId,!k.modelUsage){let _=this.aiImpl.getTokenUsage();_&&(k.modelUsage={ai:this.name,model:e,tokens:_})}return this.modelUsage=k.modelUsage,this.recordTokenUsage(k.modelUsage,r?.customLabels),o?.isRecording()&&pl(k,o,this.excludeContentFromTrace),k},C=async S=>{o?.isRecording()&&o.end(),i&&Ua(S,r?.logger??this.logger)};if(typeof window<"u"){let S=A,O={},k=[],_=r?.abortSignal??this.abortSignal;return new ReadableStream({start:L=>{let E=S.getReader(),P=()=>{try{E.cancel().catch(()=>{})}catch{}try{this.recordAbortMetric("chat",r?.customLabels)}catch{}try{o?.isRecording()&&o.end()}catch{}try{L.error(new DOMException("Aborted","AbortError"))}catch{L.error(new Error("Aborted"))}};if(_){if(_.aborted){P();return}_.addEventListener("abort",P,{once:!0})}async function $(){try{for(;;){let{done:D,value:z}=await E.read();if(D){C&&await C(k),L.close();break}let U=I(O)(z);U&&(k.push(U),L.enqueue(U))}}catch(D){if(L.error(D),o?.isRecording())try{o.end()}catch{}}finally{if(E.releaseLock(),_)try{_.removeEventListener("abort",P)}catch{}}}$()}})}return A.pipeThrough(new ro(I({}),C))}if(!this.aiImpl.createChatResp)throw new Error("createChatResp not implemented");let h=this.aiImpl.createChatResp(A);if(h.sessionId=r?.sessionId,!h.modelUsage){let b=this.aiImpl.getTokenUsage();b&&(h.modelUsage={ai:this.name,model:e,tokens:b})}return h.modelUsage&&(this.modelUsage=h.modelUsage,this.recordTokenUsage(h.modelUsage,r?.customLabels)),o?.isRecording()&&(pl(h,o,this.excludeContentFromTrace),o.end()),i&&Ga(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(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:on.SpanKind.SERVER,attributes:{[pe.LLM_SYSTEM]:this.name,[pe.LLM_OPERATION_NAME]:"embeddings",[pe.LLM_REQUEST_MODEL]:n}},t?.traceContext??on.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,l={...t,embedModel:e};this.lastUsedEmbedModel=e,i&&Ha(l.texts??[],e,n?.logger??this.logger);let u=async()=>{let[m,g]=await o(l);return await Xe({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(Ct.GEN_AI_USAGE,{[pe.LLM_USAGE_INPUT_TOKENS]:d.modelUsage.tokens.promptTokens,[pe.LLM_USAGE_OUTPUT_TOKENS]:d.modelUsage.tokens.completionTokens??0,[pe.LLM_USAGE_TOTAL_TOKENS]:d.modelUsage.tokens.totalTokens}),i&&Ka(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 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=Tp(t.chatPrompt);if(!p||p===ut("sha256").digest("hex"))return null;let d={providerName:this.getName(),contentHash:p},m=Cp(d),g=Date.now(),f=o.registry,x=f?await f.get(m):Ys.get(m);if(x&&x.expiresAt>g){if(x.expiresAt-g<u*1e3&&this.aiImpl.buildCacheUpdateTTLOp){await this.executeCacheOperation(this.aiImpl.buildCacheUpdateTTLOp(x.cacheName,l),n,r);let I={cacheName:x.cacheName,expiresAt:g+l*1e3,tokenCount:x.tokenCount};f?await f.set(m,I):Ys.set(m,{...I,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 b=await this.executeCacheOperation(h,n,r);if(b){let I={cacheName:b.name,expiresAt:new Date(b.expiresAt).getTime(),tokenCount:b.tokenCount};return f?await f.set(m,I):Ys.set(m,{...I,contentHash:p,lastTouchedAt:g}),this.useCacheByName(e,t,b.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 Xe({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 cl(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(Ct.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(Ct.GEN_AI_ASSISTANT_MESSAGE,a)}else if(o.content){let a={};t||(a.content=o.content),e.addEvent(Ct.GEN_AI_ASSISTANT_MESSAGE,a)}break}case"function":{let i={id:o.functionId};t||(i.content=o.result),e.addEvent(Ct.GEN_AI_TOOL_MESSAGE,i);break}}let r={};t||(r.content=n.join(`
169
+ `)),e.addEvent(Ct.GEN_AI_USER_MESSAGE,r)}function pl(s,e,t){if(s.modelUsage?.tokens){let n=s.modelUsage.tokens.thoughtsTokens?{[pe.LLM_USAGE_THOUGHTS_TOKENS]:s.modelUsage.tokens.thoughtsTokens}:{};e.addEvent(Ct.GEN_AI_USAGE,{[pe.LLM_USAGE_INPUT_TOKENS]:s.modelUsage.tokens.promptTokens,[pe.LLM_USAGE_OUTPUT_TOKENS]:s.modelUsage.tokens.completionTokens??0,[pe.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(Ct.GEN_AI_CHOICE,{finish_reason:r.finishReason,index:n,message:JSON.stringify(i,null,2)})}}function dl(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 Rp(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 cr=(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))(cr||{}),pr=(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))(pr||{});var dr=[{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 Nt=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,Nt(r)]))),e.items&&(e.items=Nt(e.items)),Array.isArray(e.anyOf)&&(e.anyOf=e.anyOf.map(n=>Nt(n))),Array.isArray(e.allOf)&&(e.allOf=e.allOf.map(n=>Nt(n))),Array.isArray(e.oneOf)&&(e.oneOf=e.oneOf.map(n=>Nt(n))),e},Zs=()=>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"},...be()}),gl=()=>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"},...be()}),Xs=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),l=e.chatPrompt.filter($=>$.role==="system"),u=l.map(($,D)=>({type:"text",text:$.content,...$.cache||a&&D===l.length-1?{cache_control:{type:"ephemeral"}}:{}})),c=e.chatPrompt.filter($=>$.role!=="system"),p=e.functions?.map(($,D,z)=>{let U={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},te=$.parameters?Nt($.parameters):void 0;return te===void 0||te&&typeof te=="object"&&Object.keys(te).length===0?te={...U}:te&&typeof te=="object"&&te.type==="object"&&(!("properties"in te)||!te.properties||Object.keys(te.properties).length===0)&&(te={...te,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:$.name,description:$.description,input_schema:te,...$.cache||a&&D===z.length-1?{cache_control:{type:"ephemeral"}}:{}}}),m=(this.config.tools??[]).map($=>$&&typeof $=="object"&&"type"in $?$:{name:$.name,description:$.description,input_schema:$.input_schema?Nt($.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,A=e.modelConfig?.temperature,h=e.modelConfig?.topP,b=e.modelConfig?.topK??this.config.topK,I=e.modelConfig?.n??this.config.n;if(I&&I>1)throw new Error("Anthropic does not support sampling (n > 1)");let C=$=>$.includes("claude-opus-4-6"),R=$=>$.includes("claude-opus-4-5"),M,S,O=n;if(t?.thinkingTokenBudget){let $=this.config.thinkingTokenBudgetLevels,D=this.config.effortLevelMapping;if(t.thinkingTokenBudget==="none")M=void 0,S=void 0;else{let z=t.thinkingTokenBudget;if(C(O))M={type:"adaptive"},S={effort:D?.[z]??"medium"};else if(R(O)){M={type:"enabled",budget_tokens:$?.[z]??1e4};let te=D?.[z]??"medium";te==="max"&&(te="high"),S={effort:te}}else M={type:"enabled",budget_tokens:$?.[z]??1e4}}}let _=Sp(c,!!M);_.some($=>$.role==="assistant"&&Array.isArray($.content)&&$.content.length>0&&$.content[0]?.type==="tool_use")&&(M=void 0,S=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:Nt($)},this.usedStructuredOutput=!0}let P={...this.isVertex?{anthropic_version:"vertex-2023-10-16"}:{model:n},...f?{max_tokens:f}:{},...x&&x.length>0?{stop_sequences:x}:{},...A!==void 0&&!M?{temperature:A}:{},...h!==void 0&&(!M||h>=.95)?{top_p:h}:{},...b&&!M?{top_k:b}:{},...i,...g?{tools:g}:{},...r?{stream:!0}:{},...u?{system:u}:{},...M?{thinking:M}:{},...S?{output_config:S}:{},...E?{output_format:E}:{},messages:_};return[o,P]};createChatResp=e=>{if(e.type==="error")throw new Fe(e.error.message,void 0,void 0);let t=ml(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 l={index:0,id:e.id,finishReason:t};r&&(l.content=r),i.length>0&&(l.thoughtBlocks=i,l.thought=i.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 Fe(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:ml(o.stop_reason)}]}}return{results:[{index:r,content:""}]}};supportsImplicitCaching=()=>!0},sn=class s extends Ge{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,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={...Zs(),...r},p=new Xs(c,a),d=g=>{let f=pt({model:g,modelInfo:dr,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 A={};x.maxTokens!==void 0&&(A.maxTokens=x.maxTokens),x.temperature!==void 0&&(A.temperature=x.temperature),x.topP!==void 0&&(A.topP=x.topP),x.topK!==void 0&&(A.topK=x.topK),x.presencePenalty!==void 0&&(A.presencePenalty=x.presencePenalty),x.frequencyPenalty!==void 0&&(A.frequencyPenalty=x.frequencyPenalty),x.stopSequences!==void 0&&(A.stopSequences=x.stopSequences),x.endSequences!==void 0&&(A.endSequences=x.endSequences),x.stream!==void 0&&(A.stream=x.stream),x.n!==void 0&&(A.n=x.n);let h={...f};Object.keys(A).length>0&&(h.modelConfig={...f.modelConfig??{},...A});let b=x.thinking?.thinkingTokenBudget;if(typeof b=="number"){let I=c.thinkingTokenBudgetLevels,C=[["minimal",I?.minimal??1024],["low",I?.low??5e3],["medium",I?.medium??1e4],["high",I?.high??2e4],["highest",I?.highest??32e3]],R="minimal",M=Number.POSITIVE_INFINITY;for(let[S,O]of C){let k=Math.abs(b-O);k<M&&(M=k,R=S)}h.thinkingTokenBudget=R}return x.thinking?.includeThoughts!==void 0&&(h.showThoughts=!!x.thinking.includeThoughts),h});super(p,{name:"Anthropic",apiURL:l,headers:u,modelInfo:dr,defaults:{model:c.model},options:o,supportFor:d,models:m??i})}};function Sp(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 l of a)l.encrypted?i.push(l.signature?{type:"redacted_thinking",data:l.data,signature:l.signature}:{type:"redacted_thinking",data:l.data}):i.push(l.signature?{type:"thinking",thinking:l.data,signature:l.signature}:{type:"thinking",thinking:l.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(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)&&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 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=kp(t);return vp(n)}function kp(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 vp(s){return s.map(e=>e.role==="assistant"&&typeof e.content=="string"?{...e,content:e.content.replace(/\s+$/,"")}:e)}function ml(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 mr=(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.O1="o1",U.O1Mini="o1-mini",U.O3="o3",U.O3Mini="o3-mini",U.O4Mini="o4-mini",U))(mr||{}),an=(n=>(n.TextEmbeddingAda002="text-embedding-ada-002",n.TextEmbedding3Small="text-embedding-3-small",n.TextEmbedding3Large="text-embedding-3-large",n))(an||{});var ln=(B=>(B.GPT4="gpt-4",B.GPT41="gpt-4.1",B.GPT41Mini="gpt-4.1-mini",B.GPT41Nano="gpt-4.1-nano",B.GPT4O="gpt-4o",B.GPT4OMini="gpt-4o-mini",B.GPT4ChatGPT4O="chatgpt-4o-latest",B.GPT4Turbo="gpt-4-turbo",B.GPT35Turbo="gpt-3.5-turbo",B.GPT35TurboInstruct="gpt-3.5-turbo-instruct",B.GPT35TextDavinci002="text-davinci-002",B.GPT3TextBabbage002="text-babbage-002",B.GPT3TextAda001="text-ada-001",B.GPT5="gpt-5",B.GPT5Nano="gpt-5-nano",B.GPT5Mini="gpt-5-mini",B.GPT5Chat="gpt-5-chat",B.GPT5ChatLatest="gpt-5-chat-latest",B.GPT5Codex="gpt-5-codex",B.GPT5Pro="gpt-5-pro",B.GPT51="gpt-5.1",B.GPT51ChatLatest="gpt-5.1-chat-latest",B.GPT51Codex="gpt-5.1-codex",B.GPT51CodexMini="gpt-5.1-codex-mini",B.GPT51CodexMax="gpt-5.1-codex-max",B.GPT52="gpt-5.2",B.GPT52ChatLatest="gpt-5.2-chat-latest",B.GPT52Codex="gpt-5.2-codex",B.GPT52Pro="gpt-5.2-pro",B.O1Pro="o1-pro",B.O1="o1",B.O1Mini="o1-mini",B.O3Pro="o3-pro",B.O3="o3",B.O3Mini="o3-mini",B.O4Mini="o4-mini",B))(ln||{});var un=[{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}],lo=[{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 Op=s=>{let e=["o1","o1-mini","o3","o3-mini","o4-mini","o1-pro","o3-pro"];return e.includes(s)||e.includes(s)},Kt=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...be()}),uo=()=>structuredClone({...Kt(),model:"gpt-5"}),co=()=>structuredClone({model:"gpt-5-mini",embedModel:"text-embedding-3-small",...ze()}),po=()=>({...Kt(),model:"gpt-5-nano"}),ei=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=Ep(e),l=e.modelConfig?.frequencyPenalty??this.config.frequencyPenalty,u=e.modelConfig?.stream??this.config.stream,c=this.config.store,p=Op(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}:{},...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 Fe(a.message.refusal,e.model,e.id);let l=fl(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 i=t;return i.indexIdMap||(i.indexIdMap={}),{results:o.map(({index:l,delta:{content:u,role:c,refusal:p,tool_calls:d,reasoning_content:m,annotations:g},finish_reason:f})=>{if(p)throw new Fe(p,void 0,n);let x=fl(f),A=d?.map(({id:h,index:b,function:{name:I,arguments:C}})=>{typeof h=="string"&&typeof b=="number"&&!i.indexIdMap[b]&&(i.indexIdMap[b]=h);let R=i.indexIdMap[b];return R?{id:R,type:"function",function:{name:I,params:C}}:null}).filter(h=>h!==null);return{index:l,content:u??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:A,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)}}},fl=s=>{switch(s){case"stop":return"stop";case"length":return"length";case"content_filter":return"error";case"tool_calls":return"function_call"}};function Ep(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 De=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o,models:i,chatReqUpdater:a,supportFor:l}){if(!e||e==="")throw new Error("OpenAI API key not set");let u=new ei(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:i})}},cn=class extends De{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=[...un,...i??[]];let a=u=>{let c=pt({model:u,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}},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 f=p?.thinking?.thinkingTokenBudget;if(typeof f=="number"){let x=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],A="minimal",h=Number.POSITIVE_INFINITY;for(let[b,I]of x){let C=Math.abs(f-I);C<h&&(h=C,A=b)}g.thinkingTokenBudget=A}return p?.thinking?.includeThoughts!==void 0&&(g.showThoughts=!!p.thinking.includeThoughts),g});super({apiKey:e,apiURL:t,config:{...Kt(),...n},options:r,modelInfo:i,models:l??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},Pp=s=>{let e=s.match(/^(\d{4}-\d{2}-\d{2})/);return e?e[1]>="2024-08-01":!1},ti=Kt,hl=co,xl=po,Al=uo,pn=class extends De{constructor({apiKey:e,resourceName:t,deploymentName:n,version:r="api-version=2024-02-15-preview",config:o,options:i,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={...ti(),...o},c=Mp(r),p=Pp(c);l=[...un,...l??[]];let d=g=>{let f=pt({model:g,modelInfo:l,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:u,options:i,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 mo=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 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 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,l=e.model;if((n||o||i)&&(a=this.services.filter(p=>{let d=p.getFeatures(l);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 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 Ye))throw p;switch(p.constructor){case Ft:throw p;case et:{if(![408,429,500,502,503,504].includes(p.status))throw p;break}case Ke:break;case Pt:break;case dt:break;case ct: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 Ye)||r instanceof et&&![408,429,500,502,503,504].includes(r.status)||r instanceof Ft)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(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 Wt(s){let e=!1,t=!1,n=!1,r=!1,o=!1,i=!1,a=!1,l=new Set,u=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(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 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:l,estimatedTokens:u}}function ni(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 go(s,e){return s.map(t=>{let n=t.getFeatures(),r=ni(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 fo(s,e,t={}){if(e.length===0)throw new Error("No providers available");let n=Wt(s),r=go(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 yl(s,e){let t=Wt(s),n=go(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 bl(s,e){return s.filter(t=>t.getFeatures().media[e].supported)}function Il(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 gr=(r=>(r.CommandRPlus="command-r-plus",r.CommandR="command-r",r.Command="command",r.CommandLight="command-light",r))(gr||{}),fr=(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))(fr||{});var ho=[{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 _p(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 oi=()=>structuredClone({model:"command-r-plus",embedModel:"embed-english-v3.0",...be()}),wl=()=>structuredClone({model:"command-r",embedModel:"embed-english-v3.0",...ze()}),ri=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=Np(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:_p(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: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[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}}},dn=class extends Ge{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("Cohere API key not set");let o={...oi(),...t},i=new ri(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(i,{name:"Cohere",apiURL:"https://api.cohere.ai/v1",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:ho,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 Np(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=Cl(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=Cl(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 Cl(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 hr=(n=>(n.DeepSeekChat="deepseek-chat",n.DeepSeekCoder="deepseek-coder",n.DeepSeekReasoner="deepseek-reasoner",n))(hr||{});var xo=[{name:"deepseek-chat",currency:"USD",promptTokenCostPer1M:.27,completionTokenCostPer1M:1.1},{name:"deepseek-reasoner",currency:"USD",promptTokenCostPer1M:.55,completionTokenCostPer1M:2.19}];var si=()=>structuredClone({model:"deepseek-chat",...be()}),Tl=()=>structuredClone({model:"deepseek-coder",...ze()}),mn=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 i={...si(),...t};o=[...xo,...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 xr=(I=>(I.Gemini31Pro="gemini-3.1-pro-preview",I.Gemini3FlashLite="gemini-3.1-flash-lite-preview",I.Gemini3Flash="gemini-3-flash-preview",I.Gemini3Pro="gemini-3.1-pro-preview",I.Gemini3ProImage="gemini-3-pro-image-preview",I.Gemini25Pro="gemini-2.5-pro",I.Gemini25Flash="gemini-2.5-flash",I.Gemini25FlashLite="gemini-2.5-flash-lite",I.Gemini20Flash="gemini-2.0-flash",I.Gemini20FlashLite="gemini-2.0-flash-lite",I.Gemini20ProExp="gemini-2.0-pro-exp-02-05",I.Gemini20FlashThinkingExp="gemini-2.0-flash-thinking-exp-01-21",I.Gemini1Pro="gemini-1.0-pro",I.Gemini15Flash="gemini-1.5-flash",I.Gemini15Flash002="gemini-1.5-flash-002",I.Gemini15Flash8B="gemini-1.5-flash-8b",I.Gemini15Pro="gemini-1.5-pro",I.GeminiFlashLatest="gemini-flash-latest",I.GeminiFlashLiteLatest="gemini-flash-lite-latest",I.GeminiProLatest="gemini-pro-latest",I))(xr||{}),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||{}),yo=(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))(yo||{}),bo=(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))(bo||{}),ii=(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))(ii||{}),Rl=["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 Io=[{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 Vt=s=>s.includes("gemini-3"),Sl=s=>s.includes("gemini-3")&&s.includes("pro"),Co=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,Co(n)]))),e.items&&(e.items=Co(e.items)),e},kl=[{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"}],li=()=>structuredClone({model:"gemini-2.5-flash",embedModel:"text-embedding-005",safetySettings:kl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...be()}),vl=()=>structuredClone({model:"gemini-2.0-flash",embedModel:"text-embedding-005",safetySettings:kl,thinkingTokenBudgetLevels:{minimal:200,low:800,medium:5e3,high:1e4,highest:24500},thinkingLevelMapping:{minimal:"minimal",low:"low",medium:"medium",high:"high",highest:"high"},...ze()}),ai=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(Vt(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(Sl(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?"&":"?",b=typeof this.apiKey=="function"?await this.apiKey():this.apiKey;o.name+=`${h}key=${b}`}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,l=[],u=e.chatPrompt.filter(h=>h.role!=="system");for(let h=0;h<u.length;h++){let b=u[h];switch(b.role){case"user":{let I=Array.isArray(b.content)?b.content.map((C,R)=>{switch(C.type){case"text":return{text:C.text};case"image":return{inlineData:{mimeType:C.mimeType,data:C.image}};case"audio":return{inlineData:{mimeType:`audio/${C.format??"mp3"}`,data:C.data}};case"file":return"fileUri"in C?{fileData:{mimeType:C.mimeType,fileUri:C.fileUri}}:{inlineData:{mimeType:C.mimeType,data:C.data}};default:throw new Error(`Chat prompt content type not supported (index: ${R})`)}}):[{text:b.content}];l.push({role:"user",parts:I});break}case"assistant":{let I=[],C=b.thoughtBlocks,R=b.functionCalls&&b.functionCalls.length>0,M=C?.[0],S=C?.map(k=>k.data).join("")??"",O=M?.signature;if(S&&I.push({...R?{}:{thought:!0},text:S,...O&&!R?{thought_signature:O}:{}}),b.functionCalls){let k=b.functionCalls.map((_,L)=>{let E;if(typeof _.function.params=="string"){let $=_.function.params;if($.trim().length===0)E={};else try{E=JSON.parse($)}catch{throw new Error(`Failed to parse function params JSON: ${$}`)}}else E=_.function.params;let P={functionCall:{name:_.function.name,args:E}};return O&&L===0&&(P.thought_signature=O),P});I.push(...k)}if(b.content&&I.push({text:b.content}),I.length===0)throw new Error("Assistant content is empty");l.push({role:"model",parts:I});break}case"function":{let I=[],C=b,R=h;for(;;){if(!("functionId"in C))throw new Error(`Chat prompt functionId is empty (index: ${R})`);if(I.push({functionResponse:{name:C.functionId,response:{result:C.result}}}),R+1<u.length&&u[R+1].role==="function")R++,C=u[R];else break}h=R,l.push({role:"user",parts:I});break}default:throw new Error(`Invalid role: ${JSON.stringify(b)} (index: ${h})`)}}let c=[];if(e.functions&&e.functions.length>0){let h=e.functions.map(b=>{let I={type:"object",properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]},C=b.parameters?Co(b.parameters):void 0;return C===void 0||C&&typeof C=="object"&&Object.keys(C).length===0?C={...I}:C&&typeof C=="object"&&C.type==="object"&&(!("properties"in C)||!C.properties||Object.keys(C.properties).length===0)&&(C={...C,properties:{dummy:{type:"string",description:"An optional dummy parameter, do not use"}},required:[]}),{name:b.name,description:b.description,parameters:C}});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,b=h?.enableWidget!==void 0?{enableWidget:h.enableWidget}:{};c.push({google_maps:b})}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&&Vt(n)&&(m.thinkingLevel=this.config.thinking.thinkingLevel),t?.thinkingTokenBudget){let h=this.getEffectiveMappings(n),b=h.thinkingTokenBudgetLevels;if(Vt(n)){let C=Sl(n),R=h.thinkingLevelMapping;if(t.thinkingTokenBudget==="none")m.thinkingLevel=R?.minimal??"minimal";else{let M=t.thinkingTokenBudget,S=R?.[M];S||(S=M==="highest"?"high":M),m.thinkingLevel=S}if(C&&m.thinkingLevel){let M=m.thinkingLevel;M!=="low"&&M!=="high"&&(m.thinkingLevel=M==="minimal"?"low":"high")}}else switch(t.thinkingTokenBudget){case"none":m.thinkingBudget=0,m.includeThoughts=!1,delete m.thinkingLevel;break;case"minimal":m.thinkingBudget=b?.minimal??200;break;case"low":m.thinkingBudget=b?.low??800;break;case"medium":m.thinkingBudget=b?.medium??5e3;break;case"high":m.thinkingBudget=b?.high??1e4;break;case"highest":m.thinkingBudget=b?.highest??24500;break}}m.thinkingLevel&&delete m.thinkingBudget,Vt(n)||delete m.thinkingLevel,Vt(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(Vt(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=Co(h)}}else this.config.responseFormat&&this.config.responseFormat==="json_object"&&(f.responseMimeType="application/json");let x=this.config.safetySettings;return[o,{contents:l,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 Fe("Content was blocked due to safety settings",void 0,void 0);case"RECITATION":throw new Fe("Content was blocked due to recitation policy",void 0,void 0);case"MALFORMED_FUNCTION_CALL":throw new Fe("Function call was malformed and blocked",void 0,void 0);case"UNEXPECTED_TOOL_CALL":throw new Fe("Unexpected tool call",void 0,void 0);case"FINISH_REASON_UNSPECIFIED":throw new Fe("Finish reason unspecified",void 0,void 0);case"BLOCKLIST":throw new Fe("Content was blocked due to blocklist",void 0,void 0);case"PROHIBITED_CONTENT":throw new Fe("Content was blocked due to prohibited content",void 0,void 0);case"SPII":throw new Fe("Content was blocked due to SPII",void 0,void 0);case"OTHER":throw new Fe("Other finish reason",void 0,void 0)}if(!o.content||!o.content.parts)return i;for(let u of o.content.parts){if("text"in u){if("thought"in u&&u.thought||u.thought===!0){i.thought=u.text;let c=u.thoughtSignature||u.thought_signature;i.thoughtBlocks||(i.thoughtBlocks=[]),i.thoughtBlocks.push({data:u.text,encrypted:!1,...c?{signature:c}:{}})}else i.content=u.text;continue}if("functionCall"in u){let c=u.thoughtSignature||u.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:Qe(),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;i.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&&(i.citations=[...i.citations??[],...u])}typeof l.googleMapsWidgetContextToken=="string"&&(t=l.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 Rl.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 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 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),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"};Vt(r)&&(u.temperature===void 0||u.temperature<1)&&(u.temperature=1);let c=this.config.safetySettings,p={contents:i,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 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 l of i.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 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}}},gn=class s extends Ge{static create(e){return new s(e)}constructor({apiKey:e,projectId:t,region:n,endpointId:r,config:o,options:i,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 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={...li(),...o},m=new ai(d,u,r,e,i);l=[...Io,...l??[]];let g=x=>{let A=pt({model:x,modelInfo:l,models:a});return{functions:!0,streaming:!0,hasThinkingBudget:A?.supported?.thinkingBudget??!1,hasShowThoughts:A?.supported?.showThoughts??!1,structuredOutputs:A?.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:A?.supported?.thinkingBudget??!1,multiTurn:!0}},f=a?.map(x=>{let A=x,h=A?.config;if(!h)return x;let b={};h.maxTokens!==void 0&&(b.maxTokens=h.maxTokens),h.temperature!==void 0&&(b.temperature=h.temperature),h.topP!==void 0&&(b.topP=h.topP),h.topK!==void 0&&(b.topK=h.topK),h.presencePenalty!==void 0&&(b.presencePenalty=h.presencePenalty),h.frequencyPenalty!==void 0&&(b.frequencyPenalty=h.frequencyPenalty),h.stopSequences!==void 0&&(b.stopSequences=h.stopSequences),h.endSequences!==void 0&&(b.endSequences=h.endSequences),h.stream!==void 0&&(b.stream=h.stream),h.n!==void 0&&(b.n=h.n);let I={...A};Object.keys(b).length>0&&(I.modelConfig={...A.modelConfig??{},...b});let C=h.thinking?.thinkingTokenBudget;if(typeof C=="number"){let R=d.thinkingTokenBudgetLevels,M=[["minimal",R?.minimal??200],["low",R?.low??800],["medium",R?.medium??5e3],["high",R?.high??1e4],["highest",R?.highest??24500]],S="minimal",O=Number.POSITIVE_INFINITY;for(let[k,_]of M){let L=Math.abs(C-_);L<O&&(O=L,S=k)}I.thinkingTokenBudget=S}return h.thinking?.includeThoughts!==void 0&&(I.showThoughts=!!h.thinking.includeThoughts),h.thinkingLevelMapping&&(I.thinkingLevelMapping=h.thinkingLevelMapping),h.thinkingTokenBudgetLevels&&(I.thinkingTokenBudgetLevels=h.thinkingTokenBudgetLevels),I});f?m.setModels(f):a&&m.setModels(a),super(m,{name:"GoogleGeminiAI",apiURL:c,headers:p,modelInfo:l,defaults:{model:d.model,embedModel:d.embedModel},options:i,supportFor:g,models:f??a})}};var Lp=new tt,fn=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(Lp.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 Ar=(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))(Ar||{});var wo=[{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 $p=()=>structuredClone({model:"llama-3.3-70b-versatile",...be()}),hn=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 i={...$p(),...t},a={...n,streamingUsage:!1};o=[...wo,...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:i,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 fn(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 To=[];var Ro=(e=>(e.MetaLlama270BChatHF="meta-llama/Llama-2-70b-chat-hf",e))(Ro||{});var ci=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...be()}),Ol=()=>structuredClone({model:"meta-llama/Llama-2-70b-chat-hf",...ze()}),ui=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
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
172
  `);return u?`Assistant: ${l.content}
173
173
  Functions:
174
174
  ${u}`:`Assistant: ${l.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}]})},gn=class extends Ge{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...si(),...t},i=new oi(o);super(i,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:yo,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 hr=(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))(hr||{}),ii=(e=>(e.MistralEmbed="mistral-embed",e))(ii||{});var Io=[{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 Co=()=>structuredClone({model:"mistral-small-latest",...be(),topP:1}),Cl=()=>structuredClone({...Co(),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 i={...Co(),...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},l=u=>{let{max_completion_tokens:c,messages:p,...d}=u;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: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 To=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 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 wo=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 ai=()=>structuredClone({...be(),model:"nous-hermes2",embedModel:"all-minilm"}),Tl=()=>structuredClone({...je(),model:"nous-hermes2",embedModel:"all-minilm"}),hn=class extends De{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let i={...ai(),...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 _p=s=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(s),xn=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 b of e.chatPrompt)if(b.role==="system"&&typeof b.content=="string"){o=b.content,i=!0;break}}let a=o??this.config.systemPrompt??null,l=e.functions?.map(b=>({type:"function",name:b.name,description:b.description,parameters:b.parameters??{}})),u=[],c=_p(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,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=li(i.content,t),o.finishReason=i.status==="completed"?"stop":"content_filter",o.citations=Ro(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=li(t.item.content,t.item.id),n.citations=Ro(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=li([t.part],t.item_id),n.citations=Ro([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=Ro(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]}},li=(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 Pe(n,void 0,e)}return s.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
- `)};function Ro(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 Ar=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),wl=()=>({...Ar(),model:"gpt-4o",temperature:.5}),Rl=()=>({...Ar(),model:"gpt-4o",temperature:.9}),xr=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:i,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 xn(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 b=m?.thinking?.thinkingTokenBudget;if(typeof b=="number"){let h=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],y="minimal",I=Number.POSITIVE_INFINITY;for(let[C,R]of h){let P=Math.abs(b-R);P<I&&(I=P,y=C)}x.thinkingTokenBudget=y}return m?.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!m.thinking.includeThoughts),x});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??i})}},An=class extends xr{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...ro,...o??[]];let i=a=>{let l=ot({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:{...Ar(),...t},options:n,modelInfo:o,models:r,supportFor:i})}};var ui=()=>structuredClone({model:"openrouter/auto",...be()}),yn=class extends De{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 l={...ui(),...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 i&&(p["HTTP-Referer"]=i),a&&(p["X-Title"]=a),p})}};async function So(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 ft(i,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new it("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 ft(i,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new it("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 ft(i,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new it("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 ft(i,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new it("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 it||i instanceof ft?i:new ft(i,o.type||"unknown","content processing")}return r}function Sl(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 yr=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(yr||{});var ko=[{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 br=()=>structuredClone({model:"reka-core",...be()}),vl=()=>structuredClone({...br(),model:"reka-core"}),Ol=()=>structuredClone({model:"reka-core",...je()}),El=()=>({...br(),model:"reka-flash"}),ci=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=Np(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 l=kl(i.finish_reason),u;return typeof i.message.content=="string"?u=i.message.content:u=i.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((i,a)=>{let l=kl(i.finish_reason),u;return typeof i.chunk.content=="string"?u=i.chunk.content:u=i.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:l}})}}},kl=s=>{switch(s){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function Np(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 bn=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=ko,models:i}){if(!e||e==="")throw new Error("Reka API key not set");let a={...br(),...t},l=new ci(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:i})}};var vo=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 it&&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 So(i.content,t,r);a.every(u=>u.type==="text")&&a.length===1?o.push({...i,content:a[0].text}):o.push({...i,content:a.map(u=>({type:"text",text:u.text}))})}else o.push(i);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=qt(e),r=[],o=[],i=[];try{let a=uo(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&&i.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!l.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=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 Ir=(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))(Ir||{});var Oo=[{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 pi=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...be()}),In=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 i={...pi(),...t};o=[...Oo,...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 Cr=(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))(Cr||{});var Eo=[{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 mi=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...be()}),Ml=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...je()}),di=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}`)}}},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 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 i=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:i,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},Cn=class extends Ge{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...mi(),...t},i=new di(o,e);super(i,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:Eo,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 Tr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(Tr||{}),gi=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(gi||{});var Mo=[{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 Po=()=>structuredClone({model:"grok-3-mini",...be()}),Pl=()=>structuredClone({...Po(),model:"grok-3"}),Tn=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 i={...Po(),...t};o=[...Mo,...o??[]];let a=u=>{let c=ot({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:i,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Grok")}};function Fl(s){return wr.create(s)}var wr=class s{ai;static create(e){return new s(e)}constructor(e){switch(e.name){case"openai":this.ai=new an(e);break;case"openai-responses":this.ai=new An(e);break;case"azure-openai":this.ai=new ln(e);break;case"grok":this.ai=new Tn(e);break;case"huggingface":this.ai=new gn(e);break;case"groq":this.ai=new mn(e);break;case"together":this.ai=new In(e);break;case"openrouter":this.ai=new yn(e);break;case"cohere":this.ai=new un(e);break;case"google-gemini":this.ai=new pn(e);break;case"anthropic":this.ai=new nn(e);break;case"mistral":this.ai=new fn(e);break;case"deepseek":this.ai=new cn(e);break;case"ollama":this.ai=new hn(e);break;case"reka":this.ai=new bn(e);break;case"webllm":this.ai=new Cn(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 Fo=require("@opentelemetry/api");var at=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:Fo.SpanKind.SERVER,attributes:{[ue.DB_SYSTEM]:this.name,[ue.DB_OPERATION_NAME]:"upsert",[ue.DB_TABLE]:e.table,[ue.DB_NAMESPACE]:e.namespace,[ue.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:Fo.SpanKind.SERVER,attributes:{[ue.DB_SYSTEM]:this.name,[ue.DB_OPERATION_NAME]:"upsert",[ue.DB_TABLE]:e[0].table,[ue.DB_NAMESPACE]:e[0].namespace,[ue.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:Fo.SpanKind.SERVER,attributes:{[ue.DB_SYSTEM]:this.name,[ue.DB_OPERATION_NAME]:"upsert",[ue.DB_TABLE]:e.table,[ue.DB_NAMESPACE]:e.namespace,[ue.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var fi="https://api.cloudflare.com/client/v4/accounts/",wn=class extends at{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`,fi),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`,fi),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 Qe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,fi),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:l})=>({id:o,score:i,values:a,metadata:l}))}}};var _t=class extends at{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=Lp(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={}}},Lp=(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),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&&(i=!1);if(o||i)return 1;let u=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(u*c)};var $p=s=>({namespace:s.namespace,topK:s.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:s.values??[],id:s.id}),Rn=class extends at{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: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 Qe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},$p(e))).matches.map(({id:o,score:i,values:a,metadata:l})=>({id:o,score:i,metadata:l,values:a}))}}};var Sn=class extends at{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(i=>({id:i.id,class:i.table,tenant:i.namespace,vector:i.values,properties:i.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: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: {
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}]})},xn=class extends Ge{constructor({apiKey:e,config:t,options:n,models:r}){if(!e||e==="")throw new Error("HuggingFace API key not set");let o={...ci(),...t},i=new ui(o);super(i,{name:"HuggingFace",apiURL:"https://api-inference.huggingface.co",headers:async()=>({Authorization:`Bearer ${e}`}),modelInfo:To,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 yr=(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))(yr||{}),pi=(e=>(e.MistralEmbed="mistral-embed",e))(pi||{});var So=[{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 ko=()=>structuredClone({model:"mistral-small-latest",...be(),topP:1}),El=()=>structuredClone({...ko(),model:"mistral-large-latest"}),An=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 i={...ko(),...t};o=[...So,...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:i,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 vo=class{constructor(e={}){this.config=e;this.config.id=this.config.id??Qe()}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 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 Oo=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 di=()=>structuredClone({...be(),model:"nous-hermes2",embedModel:"all-minilm"}),Ml=()=>structuredClone({...ze(),model:"nous-hermes2",embedModel:"all-minilm"}),yn=class extends De{constructor({apiKey:e="not-set",url:t="http://localhost:11434/v1",config:n,options:r,models:o}){let i={...di(),...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 Dp=s=>["o1","o1-mini","o1-pro","o3","o3-mini","o3-pro","o4-mini"].includes(s),bn=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 A of e.chatPrompt)if(A.role==="system"&&typeof A.content=="string"){o=A.content,i=!0;break}}let a=o??this.config.systemPrompt??null,l=e.functions?.map(A=>({type:"function",name:A.name,description:A.description,parameters:A.parameters??{}})),u=[],c=Dp(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,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=mi(i.content,t),o.finishReason=i.status==="completed"?"stop":"content_filter",o.citations=Eo(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=mi(t.item.content,t.item.id),n.citations=Eo(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=mi([t.part],t.item_id),n.citations=Eo([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=Eo(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]}},mi=(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 Fe(n,void 0,e)}return s.filter(n=>n.type==="output_text").map(n=>n.text).join(`
178
+ `)};function Eo(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 Ir=()=>({model:"gpt-4o",embedModel:"text-embedding-ada-002",temperature:.7,topP:1,stream:!0}),Pl=()=>({...Ir(),model:"gpt-4o",temperature:.5}),Fl=()=>({...Ir(),model:"gpt-4o",temperature:.9}),br=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=[],models:i,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 bn(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 A=m?.thinking?.thinkingTokenBudget;if(typeof A=="number"){let h=[["minimal",200],["low",800],["medium",5e3],["high",1e4],["highest",24500]],b="minimal",I=Number.POSITIVE_INFINITY;for(let[C,R]of h){let M=Math.abs(A-R);M<I&&(I=M,b=C)}x.thinkingTokenBudget=b}return m?.thinking?.includeThoughts!==void 0&&(x.showThoughts=!!m.thinking.includeThoughts),x});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??i})}},In=class extends br{constructor({apiKey:e,config:t,options:n,models:r,modelInfo:o}){if(!e||e==="")throw new Error("OpenAI API key not set");o=[...lo,...o??[]];let i=a=>{let l=pt({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:{...Ir(),...t},options:n,modelInfo:o,models:r,supportFor:i})}};var gi=()=>structuredClone({model:"openrouter/auto",...be()}),Cn=class extends De{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 l={...gi(),...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 i&&(p["HTTP-Referer"]=i),a&&(p["X-Title"]=a),p})}};async function Mo(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 It(i,"image","vision analysis")}else switch(t.fallbackBehavior){case"error":throw new mt("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 It(i,"audio","transcription")}else switch(t.fallbackBehavior){case"error":throw new mt("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 It(i,"file","text extraction")}else switch(t.fallbackBehavior){case"error":throw new mt("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 It(i,"url","content fetching")}else switch(t.fallbackBehavior){case"error":throw new mt("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 mt||i instanceof It?i:new It(i,o.type||"unknown","content processing")}return r}function _l(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 Cr=(n=>(n.RekaCore="reka-core",n.RekaFlash="reka-flash",n.RekaEdge="reka-edge",n))(Cr||{});var Po=[{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 wr=()=>structuredClone({model:"reka-core",...be()}),Ll=()=>structuredClone({...wr(),model:"reka-core"}),$l=()=>structuredClone({model:"reka-core",...ze()}),Dl=()=>({...wr(),model:"reka-flash"}),fi=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=Gp(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 l=Nl(i.finish_reason),u;return typeof i.message.content=="string"?u=i.message.content:u=i.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((i,a)=>{let l=Nl(i.finish_reason),u;return typeof i.chunk.content=="string"?u=i.chunk.content:u=i.chunk.content.text,{index:a,id:`${t}`,content:u,finishReason:l}})}}},Nl=s=>{switch(s){case"stop":return"stop";case"context":return"length";case"length":return"length"}};function Gp(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 wn=class extends Ge{constructor({apiKey:e,config:t,options:n,apiURL:r,modelInfo:o=Po,models:i}){if(!e||e==="")throw new Error("Reka API key not set");let a={...wr(),...t},l=new fi(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:i})}};var Fo=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 mt&&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 Mo(i.content,t,r);a.every(u=>u.type==="text")&&a.length===1?o.push({...i,content:a[0].text}):o.push({...i,content:a.map(u=>({type:"text",text:u.text}))})}else o.push(i);return{...e,chatPrompt:o}}async selectProviderWithDegradation(e,t){let n=Wt(e),r=[],o=[],i=[];try{let a=fo(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&&i.push("Streaming not supported - will use non-streaming mode"),n.requiresCaching&&!l.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=Wt(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 Tr=(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))(Tr||{});var _o=[{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 hi=()=>structuredClone({model:"meta-llama/Llama-3.3-70B-Instruct-Turbo",...be()}),Tn=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 i={...hi(),...t};o=[..._o,...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 Rr=(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))(Rr||{});var No=[{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 Ai=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...be()}),Gl=()=>structuredClone({model:"Llama-3.2-3B-Instruct-q4f32_1-MLC",...ze()}),xi=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}`)}}},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 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 i=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:i,finishReason:o}],remoteId:e.id}};createEmbedResp(e){throw new Error("WebLLM does not support embeddings")}},Rn=class extends Ge{constructor({engine:e,config:t,options:n,models:r}){if(!e)throw new Error("WebLLM engine instance is required");let o={...Ai(),...t},i=new xi(o,e);super(i,{name:"WebLLM",apiURL:void 0,headers:async()=>({}),modelInfo:No,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 Sr=(r=>(r.Grok3="grok-3",r.Grok3Mini="grok-3-mini",r.Grok3Fast="grok-3-fast",r.Grok3MiniFast="grok-3-mini-fast",r))(Sr||{}),yi=(e=>(e.GrokEmbedSmall="grok-embed-small",e))(yi||{});var Lo=[{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 $o=()=>structuredClone({model:"grok-3-mini",...be()}),Ul=()=>structuredClone({...$o(),model:"grok-3"}),Sn=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 i={...$o(),...t};o=[...Lo,...o??[]];let a=u=>{let c=pt({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:i,options:n,apiURL:"https://api.x.ai/v1",modelInfo:o,models:r,supportFor:a,chatReqUpdater:l}),super.setName("Grok")}};function Bl(s){return kr.create(s)}var kr=class s{ai;static create(e){return new s(e)}constructor(e){switch(e.name){case"openai":this.ai=new cn(e);break;case"openai-responses":this.ai=new In(e);break;case"azure-openai":this.ai=new pn(e);break;case"grok":this.ai=new Sn(e);break;case"huggingface":this.ai=new xn(e);break;case"groq":this.ai=new hn(e);break;case"together":this.ai=new Tn(e);break;case"openrouter":this.ai=new Cn(e);break;case"cohere":this.ai=new dn(e);break;case"google-gemini":this.ai=new gn(e);break;case"anthropic":this.ai=new sn(e);break;case"mistral":this.ai=new An(e);break;case"deepseek":this.ai=new mn(e);break;case"ollama":this.ai=new yn(e);break;case"reka":this.ai=new wn(e);break;case"webllm":this.ai=new Rn(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 Do=require("@opentelemetry/api");var gt=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:Do.SpanKind.SERVER,attributes:{[pe.DB_SYSTEM]:this.name,[pe.DB_OPERATION_NAME]:"upsert",[pe.DB_TABLE]:e.table,[pe.DB_NAMESPACE]:e.namespace,[pe.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:Do.SpanKind.SERVER,attributes:{[pe.DB_SYSTEM]:this.name,[pe.DB_OPERATION_NAME]:"upsert",[pe.DB_TABLE]:e[0].table,[pe.DB_NAMESPACE]:e[0].namespace,[pe.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:Do.SpanKind.SERVER,attributes:{[pe.DB_SYSTEM]:this.name,[pe.DB_OPERATION_NAME]:"upsert",[pe.DB_TABLE]:e.table,[pe.DB_NAMESPACE]:e.namespace,[pe.DB_OPERATION_NAME]:"query"}},async t=>{try{return await this._query(e,{span:t})}finally{t.end()}}):await this._query(e)}};var bi="https://api.cloudflare.com/client/v4/accounts/",kn=class extends gt{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 Xe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/upsert`,bi),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 Xe({url:new URL(`${this.accountId}/vectorize/indexes/${r}/upsert`,bi),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 Xe({url:new URL(`${this.accountId}/vectorize/indexes/${e.table}/query`,bi),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:l})=>({id:o,score:i,values:a,metadata:l}))}}};var Lt=class extends gt{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=Up(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={}}},Up=(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),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&&(i=!1);if(o||i)return 1;let u=Math.sqrt(n),c=Math.sqrt(r);return 1-t/(u*c)};var Bp=s=>({namespace:s.namespace,topK:s.limit||10,filter:{},includeValues:!0,includeMetadata:!0,vector:s.values??[],id:s.id}),vn=class extends gt{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 Xe({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 Xe({url:this.apiURL,headers:{Authorization:`Bearer ${this.apiKey}`},name:"/query",fetch:this.fetch,span:t?.span},Bp(e))).matches.map(({id:o,score:i,values:a,metadata:l})=>({id:o,score:i,metadata:l,values:a}))}}};var On=class extends gt{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 Xe({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 Xe({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 Xe({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,9 +190,9 @@ ${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 _o=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new Sn(e);break;case"pinecone":this.db=new Rn(e);break;case"cloudflare":this.db=new wn(e);break;case"memory":this.db=new _t(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 hi="_internal",No=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 Go=class{db;constructor(e){switch(e.name){case"weaviate":this.db=new On(e);break;case"pinecone":this.db=new vn(e);break;case"cloudflare":this.db=new kn(e);break;case"memory":this.db=new Lt(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 Ii="_internal",Uo=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,i=t?.minWordsPerChunk,a=Dp({initialChunks:r,minWordsPerChunk:i,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:hi,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:hi,values:u})):o=r.map(l=>this.db.query({table:hi,values:l}));let i=await Promise.all(o),a=[];for(let{matches:l}of i){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?Gp(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(f=>f.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},Dp=({initialChunks:s,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return s.forEach(i=>{let a=i.split(/\s+/),l=a.length;if(o+l<=e)r+=`${i}
195
+ `):e,r=this.chunker(n).filter(u=>u.length>0),o=t?.maxWordsPerChunk,i=t?.minWordsPerChunk,a=jp({initialChunks:r,minWordsPerChunk:i,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:Ii,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:Ii,values:u})):o=r.map(l=>this.db.query({table:Ii,values:l}));let i=await Promise.all(o),a=[];for(let{matches:l}of i){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?zp(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(f=>f.text===g)).filter(g=>g!==void 0);a.push(m)}else a.push(p)}return a}},jp=({initialChunks:s,maxWordsPerChunk:e=350,minWordsPerChunk:t=250})=>{let n=[],r="",o=0;return s.forEach(i=>{let a=i.split(/\s+/),l=a.length;if(o+l<=e)r+=`${i}
196
196
 
197
197
  `,o+=l;else if(o>0&&o+l<=e*1.5)r+=`${i}
198
198
 
@@ -200,21 +200,21 @@ ${u}`:`Assistant: ${l.content}`}default:throw new Error("Unknown role")}}).join(
200
200
 
201
201
  `,o+=u.length)}else r=`${i}
202
202
 
203
- `,o=l}),(o>t||n.length===0)&&n.push(r.trim()),n},Gp=(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 Dn=require("@opentelemetry/api");var Rr=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 l=a.chat.find(u=>u.index===i);if(!l){a.chat.push({index:i,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(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}},kn=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new Rr}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new Rr),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)en(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){no(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 Rr):this.defaultMemory.reset()}};function Tt(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 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()}},vn=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 wt({message:o});if(!o)throw r?new wt({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},xi=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:l}=i,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 Ai={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},Sr,yi=s=>{if(Sr)return Sr;let e=s??ie.meter;if(e)return Sr=Up(e),Sr};var _l=()=>{let s=[];return ie.meter||s.push("Global meter not initialized"),!Sr&&ie.meter&&s.push("Metrics instruments not created despite available meter"),{healthy:s.length===0,issues:s}},Up=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"})}),Lo=Ai,Nl=s=>{Lo={...Lo,...s}},Ll=()=>({...Lo}),At=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=Lo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var $l=(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)}},$o=(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)}},bi=(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)}},Dl=(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)}},Ii=(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)}},Gl=(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)}},Ul=(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)}},jl=(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)}},Bl=(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)}},zl=(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)}},Do=(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 On=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},He=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()}},ql=s=>{let t=s.map(n=>`'${n.title}' (${On(n.type)})`).join(", ");return new He(`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 ${On(s[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},Go=s=>new He(`Expected (Required) field not found: '${s.title}'. Begin a new section with "${s.title}:" and then provide a valid ${On(s.type)} value directly after.`);var Uo=s=>new He(`Required field is missing: '${s.title}'. After the "${s.title}:" label, provide a non-empty ${On(s.type)}. Do not use null, undefined, or leave it blank.`),Hl=(s,e)=>new He(`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.`),Kl=(s,e)=>new He(`Invalid Array: ${e} for '${s.title}'. Provide a JSON array of ${On(s.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),Wl=(s,e,t)=>new He(`Field '${s.title}' has an invalid value '${e}': ${t}. Provide a ${On(s.type)}. Ensure formatting exactly matches the expected type.`),Vl=(s,e,t)=>new He(`Invalid date for '${s.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),Jl=(s,e,t)=>new He(`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}.`),Ci=(s,e,t)=>new He(`Invalid URL for '${s.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),En=(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 He(r)},Ti=(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 He(r)};var Ql=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let l=s.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";_a(s,e,u,n)}return r&&bi(r,"validation",o,a),i&&i.addEvent("validation.error",{message:s.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},Yl=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let l=s.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";Na(s,e,u,n)}return r&&bi(r,"assertion",o,a),i&&i.addEvent("assertion.error",{message:s.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},Xl=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{t&&n&&La(s,e,n),r&&Dl(r,o,a),i&&i.addEvent("refusal.error",{message:s.toString()})};var Nt=or(require("dayjs"),1),Zl=or(require("dayjs/plugin/customParseFormat.js"),1),eu=or(require("dayjs/plugin/timezone.js"),1),tu=or(require("dayjs/plugin/utc.js"),1);Nt.default.extend(tu.default);Nt.default.extend(eu.default);Nt.default.extend(Zl.default);function nu(s,e,t=!1){try{return jp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw Vl(s,e,r)}}function jp(s){if(!(0,Nt.default)(s,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return Nt.default.utc(s,"YYYY-MM-DD").startOf("day").toDate()}function ru(s,e,t=!1){try{return Bp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw Jl(s,e,r)}}function Bp(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=Nt.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 ou=s=>(0,Nt.default)(s).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var MA=new Ze,kr=(s,e)=>{let t=s.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(s.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 '${s.name}' to be type '${u}' instead got '${e}'`);return}let o=u=>!(!u||typeof u!="object"||!("data"in u));if(s.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 '${s.name}' to be type '${u}' instead got '${e}'`);return}let i=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(s.type?.name==="file"){let u;if(Array.isArray(e)){for(let c of e)if(!i(c)){u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else i(e)||(u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(u)throw new Error(`Validation failed: Expected '${s.name}' to be type '${u}' instead got '${e}'`);return}let a=u=>typeof u=="string"?!0:!(!u||typeof u!="object"||!("url"in u));if(s.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 '${s.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 '${s.name}' to be a ${s.type?.isArray?"an array of ":""}${t.name} instead got '${u}' (${JSON.stringify(e)})`)}};function vr(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 l=new Set(i.map(u=>u.url));for(let u of a)u?.url&&!l.has(u.url)&&(i.push(u),l.add(u.url));r.citations=i}}}return Object.values(e)}var su=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 Ri(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 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 i=s.find(a=>a.index===t);return i&&(i.version=r),s}var wi=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)}},zp=new wi(500);function iu(s,e,t=0,n=zp){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,l)=>e.slice(0,l+1));n.get(e)||n.set(e,o);let i=-1;for(let a=o.length-1;a>=0;a--){let l=o[a];if(s.endsWith(l)){i=a;break}}return i>=0?-2:-1}function Lt(s,e){if(typeof s!="string")throw Ci(e,String(s),"URL must be a string");try{new URL(s)}catch{throw Ci(e,s,"Invalid URL format. Expected a valid URL like https://example.com")}}function lt(s,e){if(typeof s!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&s.length<t.minLength)throw En(e,s,"minLength",t.minLength);if(t.maxLength!==void 0&&s.length>t.maxLength)throw En(e,s,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(s))throw En(e,s,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s))throw En(e,s,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(s)}catch{throw En(e,s,"format","valid URL")}}}function ut(s,e){if(typeof s!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&s<t.minimum)throw Ti(e,s,"minimum",t.minimum);if(t.maximum!==void 0&&s>t.maximum)throw Ti(e,s,"maximum",t.maximum)}}var zo=(s,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,i=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};vi(s,e,a,t,{strictMode:r,skipEarlyFail:i,treatAllFieldsOptional:o}),Oi(s,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let l of s.getOutputFields())l.isInternal&&delete e[l.name]},Si=(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 ql(n)},vi=(s,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let i=s.getOutputFields(),a;for(;;){let l=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&l.add(t.currFieldIndex);let u=i.map((g,f)=>({field:g,index:f})).filter(({index:g})=>!l.has(g)),c,p,d=-1,m=0;for(let{index:g,field:f}of u){let b=`${(t.extractedFields.length===0?"":`
205
- `)+f.title}:`,h=iu(n,b,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=b.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 Go(g)}break}if(a&&p&&a.name!==p.name)throw Go(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=jo(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)}},Oi=(s,e,t,n,r)=>{let o=r?.strictMode??!1,i=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,l=r?.forceFinalize??!1;if(t.currField){let u=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<u&&(u=f)}let p=n.substring(t.s,u).trim(),d=jo(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 Go(p)}if(qp(s,e,n,t),!i){let u=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?Si(t,e,s.getOutputFields()):u||Si(t,e,s.getOutputFields())}},qp=(s,e,t,n)=>{let r=s.getOutputFields();if(r.length===1){let i=r[0];if(i){let a=`${i.title}:`,l=t.indexOf(a);if(l!==-1){let u=l+a.length,c=`
207
- ${i.title}:`,p=t.indexOf(c,u),d=t.substring(u,p===-1?t.length:p).trim();if(d)try{let m=jo(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 l of o){let u=l.trim();if(u.startsWith(a)){let c=u.substring(a.length).trim();if(c)try{let p=jo(i,c);if(p!==void 0){e[i.name]=p;break}}catch(p){if(!i.isOptional)throw p}break}}}},au=(s,e,t=!1)=>{switch(s.type?.name){case"code":return ki(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 nu(s,e,t);case"datetime":return ru(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*lu(s,e,t,n,r,o){let{name:i,isInternal:a}=e,{isArray:l,name:u}=e.type??{};if(a||l||u&&u!=="string"&&u!=="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*Ei(s,e,t,n,r){for(let i of n.prevFields??[]){let{field:a,s:l,e:u}=i;yield*lu(e,a,l,u,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(s.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*lu(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 l=t[i];if(Array.isArray(l)){let c=n.streamedIndex?.[i]??0,p=l.slice(c);p&&p.length>0&&(yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=c+p.length);continue}let u=typeof l=="string"?l:void 0;if(!n.streamedIndex[i])yield{index:r,delta:{[i]:l}},n.streamedIndex[i]=u?u.length:1;else if(u){let c=n.streamedIndex[i];if(u.length>c){let p=u.substring(c);yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=u.length}}}}function jo(s,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(s.isOptional)return;throw Uo(s)}let t;if(s.type?.name==="json"&&!s.type?.isArray)try{let r=ki(e);return t=JSON.parse(r),t}catch(r){throw Hl(s,r.message)}if(s.type?.isArray)try{try{t=JSON.parse(e)}catch{t=su(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 Kl(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=ki(i);i=JSON.parse(a)}catch{}t[r]=au(s,i,!0)}}else t=au(s,e)}catch(r){throw Wl(s,e,r.message)}if(typeof t=="string"&&t==="")return;let n=s.type;if(n&&t!==void 0&&(n.name==="url"&&Lt(t,s),(n.name==="string"||n.name==="code")&&lt(t,s),n.name==="number"&&ut(t,s),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?lt(r,s):n.name==="number"&&ut(r,s));return t}function Mn(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 Uo(r);continue}let i=r.type;if(i){if(i.name==="url"&&Lt(o,r),(i.name==="string"||i.name==="code")&&lt(o,r),i.name==="number"&&ut(o,r),i.isArray&&Array.isArray(o))for(let a of o)a!=null&&(i.name==="url"?Lt(a,r):i.name==="string"||i.name==="code"?lt(a,r):i.name==="number"&&ut(a,r));if(i.name==="object"&&i.fields&&typeof o=="object"&&!Array.isArray(o)&&Bo(r,o,t),i.isArray&&i.fields&&Array.isArray(o)&&i.name==="object")for(let a of o)a&&typeof a=="object"&&Bo(r,a,t)}}}function Bo(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 Uo(i);continue}let l=i.type;if(l){if(l.name==="url"&&Lt(a,i),(l.name==="string"||l.name==="code")&&lt(a,i),l.name==="number"&&ut(a,i),l.isArray&&Array.isArray(a))for(let u of a)u!=null&&(l.name==="url"?Lt(u,i):l.name==="string"||l.name==="code"?lt(u,i):l.name==="number"&&ut(u,i));if(l.name==="object"&&l.fields&&typeof a=="object"&&!Array.isArray(a)&&Bo(i,a,t),l.isArray&&l.fields&&Array.isArray(a)&&l.name==="object")for(let u of a)u&&typeof u=="object"&&Bo(i,u,t)}}}var ki=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 Or(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});uu(r.field,t,i,n)}}async function Mi(s,e,t,n,r,o,i=!1){for(let a of s){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:i});uu(t.currField,n,c,o)}}var uu=(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 cu(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 $t(s,e="Schema"){if("name"in s&&"type"in s)return qo(s);let t={},n=[];for(let r of s){if(r.isInternal)continue;let o=qo(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function qo(s,e=!1){let t=s.type,n=cu(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]=qo(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=cu(t.description||s.description,t);r.items={type:pu(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]=qo(a,!0),i.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=pu(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 pu(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 Ho(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)');Ho(s.items)}else if(s.type==="object"&&s.properties)for(let e of Object.values(s.properties))Ho(e)}var Kt=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},Er=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()}},Mr=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=l}),(o>t||n.length===0)&&n.push(r.trim()),n},zp=(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 Bn=require("@opentelemetry/api");var vr=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 l=a.chat.find(u=>u.index===i);if(!l){a.chat.push({index:i,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(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}},En=class{memories=new Map;defaultMemory;constructor(){this.defaultMemory=new vr}getMemory(e){return e?(this.memories.has(e)||this.memories.set(e,new vr),this.memories.get(e)):this.defaultMemory}addRequest(e,t){for(let n of e)rn(n);this.getMemory(t).addRequest(e,0)}addResponse(e,t){ao(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 vr):this.defaultMemory.reset()}};function Tt(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 vt=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()}},Mn=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 vt({message:o});if(!o)throw r?new vt({message:r}):new Error("Assertion Failed: No message provided for assertion")}}},Ci=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:l}=i,u=await l(o,n);if(u!==void 0){if(typeof u=="string")throw new vt({message:u});if(!u&&a)throw new vt({message:a})}}};var wi={enabled:!0,enabledCategories:["generation","streaming","functions","errors","performance"],maxLabelLength:100,samplingRate:1},Or,Ti=s=>{if(Or)return Or;let e=s??le.meter;if(e)return Or=qp(e),Or};var jl=()=>{let s=[];return le.meter||s.push("Global meter not initialized"),!Or&&le.meter&&s.push("Metrics instruments not created despite available meter"),{healthy:s.length===0,issues:s}},qp=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"})}),Bo=wi,zl=s=>{Bo={...Bo,...s}},ql=()=>({...Bo}),Rt=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=Bo.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e};var Hl=(s,e,t,n,r,o,i)=>{try{let a=Rt({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)}},jo=(s,e,t,n,r)=>{try{let o=Rt({...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)}},Ri=(s,e,t,n)=>{try{let r=Rt({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)}},Kl=(s,e,t)=>{try{let n=Rt({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)}},Si=(s,e,t,n,r,o)=>{try{let i=Rt({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)}},Wl=(s,e,t,n,r=!1,o,i)=>{try{let a=Rt({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)}},Vl=(s,e,t,n,r)=>{try{let o=Rt({...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)}},Jl=(s,e,t,n,r,o)=>{try{let i=Rt({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)}},Ql=(s,e,t,n,r,o)=>{try{let i=Rt({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)}},Yl=(s,e,t,n,r,o,i)=>{try{let a=Rt({...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)}},zo=(s,e,t,n,r)=>{try{let o=Rt({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 Pn=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},We=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()}},Xl=s=>{let t=s.map(n=>`'${n.title}' (${Pn(n.type)})`).join(", ");return new We(`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 ${Pn(s[0]?.type)} value. Keep the output concise and avoid unrelated text.`)},qo=s=>new We(`Expected (Required) field not found: '${s.title}'. Begin a new section with "${s.title}:" and then provide a valid ${Pn(s.type)} value directly after.`);var Ho=s=>new We(`Required field is missing: '${s.title}'. After the "${s.title}:" label, provide a non-empty ${Pn(s.type)}. Do not use null, undefined, or leave it blank.`),Zl=(s,e)=>new We(`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.`),eu=(s,e)=>new We(`Invalid Array: ${e} for '${s.title}'. Provide a JSON array of ${Pn(s.type)} items (e.g., [ ... ]). Markdown lists are also accepted if each item is on its own line starting with a hyphen.`),tu=(s,e,t)=>new We(`Field '${s.title}' has an invalid value '${e}': ${t}. Provide a ${Pn(s.type)}. Ensure formatting exactly matches the expected type.`),nu=(s,e,t)=>new We(`Invalid date for '${s.title}': ${t}. Use the exact format YYYY-MM-DD (e.g., 2024-05-09). You provided: ${e}.`),ru=(s,e,t)=>new We(`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}.`),ki=(s,e,t)=>new We(`Invalid URL for '${s.title}': ${t}. Use a valid URL format (e.g., https://example.com). You provided: ${e}.`),Fn=(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 We(r)},vi=(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 We(r)};var ou=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let l=s.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";ja(s,e,u,n)}return r&&Ri(r,"validation",o,a),i&&i.addEvent("validation.error",{message:s.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},su=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{let l=s.getFixingInstructions();if(t&&n){let u=l?.map(c=>c.title).join(", ")??"";za(s,e,u,n)}return r&&Ri(r,"assertion",o,a),i&&i.addEvent("assertion.error",{message:s.toString(),fixing_instructions:l?.map(u=>u.title).join(", ")??""}),l},iu=({error:s,errCount:e,debug:t,logger:n,metricsInstruments:r,signatureName:o,span:i,customLabels:a})=>{t&&n&&qa(s,e,n),r&&Kl(r,o,a),i&&i.addEvent("refusal.error",{message:s.toString()})};var $t=ar(require("dayjs"),1),au=ar(require("dayjs/plugin/customParseFormat.js"),1),lu=ar(require("dayjs/plugin/timezone.js"),1),uu=ar(require("dayjs/plugin/utc.js"),1);$t.default.extend(uu.default);$t.default.extend(lu.default);$t.default.extend(au.default);function cu(s,e,t=!1){try{return Hp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw nu(s,e,r)}}function Hp(s){if(!(0,$t.default)(s,"YYYY-MM-DD",!0).isValid())throw new Error('Invalid date format. Please provide the date in "YYYY-MM-DD" format.');return $t.default.utc(s,"YYYY-MM-DD").startOf("day").toDate()}function pu(s,e,t=!1){try{return Kp(e)}catch(n){if(s.isOptional&&!t)return;let r=n.message;throw ru(s,e,r)}}function Kp(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=$t.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 du=s=>(0,$t.default)(s).utc().format("YYYY-MM-DD HH:mm:ss [UTC]");var zA=new tt,Er=(s,e)=>{let t=s.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(s.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 '${s.name}' to be type '${u}' instead got '${e}'`);return}let o=u=>!(!u||typeof u!="object"||!("data"in u));if(s.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 '${s.name}' to be type '${u}' instead got '${e}'`);return}let i=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(s.type?.name==="file"){let u;if(Array.isArray(e)){for(let c of e)if(!i(c)){u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })";break}}else i(e)||(u="object ({ mimeType: string; data: string } | { mimeType: string; fileUri: string })");if(u)throw new Error(`Validation failed: Expected '${s.name}' to be type '${u}' instead got '${e}'`);return}let a=u=>typeof u=="string"?!0:!(!u||typeof u!="object"||!("url"in u));if(s.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 '${s.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 '${s.name}' to be a ${s.type?.isArray?"an array of ":""}${t.name} instead got '${u}' (${JSON.stringify(e)})`)}};function Mr(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 l=new Set(i.map(u=>u.url));for(let u of a)u?.url&&!l.has(u.url)&&(i.push(u),l.add(u.url));r.citations=i}}}return Object.values(e)}var mu=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 Ei(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 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 i=s.find(a=>a.index===t);return i&&(i.version=r),s}var Oi=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)}},Wp=new Oi(500);function gu(s,e,t=0,n=Wp){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,l)=>e.slice(0,l+1));n.get(e)||n.set(e,o);let i=-1;for(let a=o.length-1;a>=0;a--){let l=o[a];if(s.endsWith(l)){i=a;break}}return i>=0?-2:-1}function Dt(s,e){if(typeof s!="string")throw ki(e,String(s),"URL must be a string");try{new URL(s)}catch{throw ki(e,s,"Invalid URL format. Expected a valid URL like https://example.com")}}function ft(s,e){if(typeof s!="string")return;let t=e.type;if(t){if(t.minLength!==void 0&&s.length<t.minLength)throw Fn(e,s,"minLength",t.minLength);if(t.maxLength!==void 0&&s.length>t.maxLength)throw Fn(e,s,"maxLength",t.maxLength);if(t.pattern!==void 0&&!new RegExp(t.pattern).test(s))throw Fn(e,s,"pattern",t.pattern);if(t.format==="email"&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s))throw Fn(e,s,"format","valid email address");if(t.format==="uri"||t.format==="url")try{new URL(s)}catch{throw Fn(e,s,"format","valid URL")}}}function ht(s,e){if(typeof s!="number")return;let t=e.type;if(t){if(t.minimum!==void 0&&s<t.minimum)throw vi(e,s,"minimum",t.minimum);if(t.maximum!==void 0&&s>t.maximum)throw vi(e,s,"maximum",t.maximum)}}var Vo=(s,e,t,n)=>{let r=n?.strictMode??!1,o=n?.treatAllFieldsOptional??!1,i=n?.treatAllFieldsOptional??!1,a={extractedFields:[],streamedIndex:{},s:-1};Fi(s,e,a,t,{strictMode:r,skipEarlyFail:i,treatAllFieldsOptional:o}),_i(s,e,a,t,{strictMode:r,treatAllFieldsOptional:o,forceFinalize:!0});for(let l of s.getOutputFields())l.isInternal&&delete e[l.name]},Mi=(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 Xl(n)},Fi=(s,e,t,n,{strictMode:r,skipEarlyFail:o}={})=>{let i=s.getOutputFields(),a;for(;;){let l=new Set;t.currFieldIndex!==void 0&&!t.inAssumedField&&l.add(t.currFieldIndex);let u=i.map((g,f)=>({field:g,index:f})).filter(({index:g})=>!l.has(g)),c,p,d=-1,m=0;for(let{index:g,field:f}of u){let A=`${(t.extractedFields.length===0?"":`
205
+ `)+f.title}:`,h=gu(n,A,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=A.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 qo(g)}break}if(a&&p&&a.name!==p.name)throw qo(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=Ko(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)}},_i=(s,e,t,n,r)=>{let o=r?.strictMode??!1,i=r?.treatAllFieldsOptional??!1,a=r?.deferRequiredCheckForStreaming??!1,l=r?.forceFinalize??!1;if(t.currField){let u=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<u&&(u=f)}let p=n.substring(t.s,u).trim(),d=Ko(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 qo(p)}if(Vp(s,e,n,t),!i){let u=t.currField!==void 0||(t.extractedFields?.length??0)>0;o||l?Mi(t,e,s.getOutputFields()):u||Mi(t,e,s.getOutputFields())}},Vp=(s,e,t,n)=>{let r=s.getOutputFields();if(r.length===1){let i=r[0];if(i){let a=`${i.title}:`,l=t.indexOf(a);if(l!==-1){let u=l+a.length,c=`
207
+ ${i.title}:`,p=t.indexOf(c,u),d=t.substring(u,p===-1?t.length:p).trim();if(d)try{let m=Ko(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 l of o){let u=l.trim();if(u.startsWith(a)){let c=u.substring(a.length).trim();if(c)try{let p=Ko(i,c);if(p!==void 0){e[i.name]=p;break}}catch(p){if(!i.isOptional)throw p}break}}}},fu=(s,e,t=!1)=>{switch(s.type?.name){case"code":return Pi(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 cu(s,e,t);case"datetime":return pu(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*hu(s,e,t,n,r,o){let{name:i,isInternal:a}=e,{isArray:l,name:u}=e.type??{};if(a||l||u&&u!=="string"&&u!=="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*Ni(s,e,t,n,r){for(let i of n.prevFields??[]){let{field:a,s:l,e:u}=i;yield*hu(e,a,l,u,n,r)}if(n.prevFields=void 0,n.inAssumedField&&!(s.getOutputFields().filter(l=>!l.isInternal).length===1)||!n.currField||n.currField.isInternal)return;yield*hu(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 l=t[i];if(Array.isArray(l)){let c=n.streamedIndex?.[i]??0,p=l.slice(c);p&&p.length>0&&(yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=c+p.length);continue}let u=typeof l=="string"?l:void 0;if(!n.streamedIndex[i])yield{index:r,delta:{[i]:l}},n.streamedIndex[i]=u?u.length:1;else if(u){let c=n.streamedIndex[i];if(u.length>c){let p=u.substring(c);yield{index:r,delta:{[i]:p}},n.streamedIndex[i]=u.length}}}}function Ko(s,e){if(!e||e===""||/^(null|undefined)\s*$/i.test(e)){if(s.isOptional)return;throw Ho(s)}let t;if(s.type?.name==="json"&&!s.type?.isArray)try{let r=Pi(e);return t=JSON.parse(r),t}catch(r){throw Zl(s,r.message)}if(s.type?.isArray)try{try{t=JSON.parse(e)}catch{t=mu(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 eu(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=Pi(i);i=JSON.parse(a)}catch{}t[r]=fu(s,i,!0)}}else t=fu(s,e)}catch(r){throw tu(s,e,r.message)}if(typeof t=="string"&&t==="")return;let n=s.type;if(n&&t!==void 0&&(n.name==="url"&&Dt(t,s),(n.name==="string"||n.name==="code")&&ft(t,s),n.name==="number"&&ht(t,s),n.isArray&&Array.isArray(t)))for(let r of t)r!==void 0&&(n.name==="string"||n.name==="code"?ft(r,s):n.name==="number"&&ht(r,s));return t}function _n(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 Ho(r);continue}let i=r.type;if(i){if(i.name==="url"&&Dt(o,r),(i.name==="string"||i.name==="code")&&ft(o,r),i.name==="number"&&ht(o,r),i.isArray&&Array.isArray(o))for(let a of o)a!=null&&(i.name==="url"?Dt(a,r):i.name==="string"||i.name==="code"?ft(a,r):i.name==="number"&&ht(a,r));if(i.name==="object"&&i.fields&&typeof o=="object"&&!Array.isArray(o)&&Wo(r,o,t),i.isArray&&i.fields&&Array.isArray(o)&&i.name==="object")for(let a of o)a&&typeof a=="object"&&Wo(r,a,t)}}}function Wo(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 Ho(i);continue}let l=i.type;if(l){if(l.name==="url"&&Dt(a,i),(l.name==="string"||l.name==="code")&&ft(a,i),l.name==="number"&&ht(a,i),l.isArray&&Array.isArray(a))for(let u of a)u!=null&&(l.name==="url"?Dt(u,i):l.name==="string"||l.name==="code"?ft(u,i):l.name==="number"&&ht(u,i));if(l.name==="object"&&l.fields&&typeof a=="object"&&!Array.isArray(a)&&Wo(i,a,t),l.isArray&&l.fields&&Array.isArray(a)&&l.name==="object")for(let u of a)u&&typeof u=="object"&&Wo(i,u,t)}}}var Pi=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 Pr(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});xu(r.field,t,i,n)}}async function Li(s,e,t,n,r,o,i=!1){for(let a of s){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:i});xu(t.currField,n,c,o)}}var xu=(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 Au(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 Gt(s,e="Schema"){if("name"in s&&"type"in s)return Jo(s);let t={},n=[];for(let r of s){if(r.isInternal)continue;let o=Jo(r);t[r.name]=o,r.isOptional||n.push(r.name)}return{type:"object",title:e,properties:t,required:n,additionalProperties:!1}}function Jo(s,e=!1){let t=s.type,n=Au(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]=Jo(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=Au(t.description||s.description,t);r.items={type:yu(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]=Jo(a,!0),i.isOptional||r.required.push(o)}}else t?.name==="class"&&t.options?(r.type="string",r.enum=t.options):(r.type=yu(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 yu(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 Qo(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)');Qo(s.items)}else if(s.type==="object"&&s.properties)for(let e of Object.values(s.properties))Qo(e)}var Jt=class extends Error{calls;constructor(e){super(`Stop function executed: ${e.map(t=>t.func.name).join(", ")}`),this.name="AxStopFunctionCallException",this.calls=e}},Fr=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()}},_r=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()}},Pr=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,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 l=(n?.functionResultFormatter??ie.functionResultFormatter)(i);return{formatted:String(l),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 Er?new Mr(i.getFields(),o,e.id):i}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Wt=(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{Ho(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()}},Nr=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,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 l=(n?.functionResultFormatter??le.functionResultFormatter)(i);return{formatted:String(l),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 Fr?new _r(i.getFields(),o,e.id):i}};execute=async(e,t)=>(await this.executeWithDetails(e,t)).formatted},Qt=(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{Qo(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]},Ko=async({ai:s,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:i,excludeContentFromTrace:a,index:l,functionResultFormatter:u,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let f=new Pr(e),x=new Set,b=[],h=R=>{let P=k=>k.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),v=P(R),E=e.find(k=>k.name===R);return E||(E=e.find(k=>P(k.name)===v)),E},y=t.map(R=>{if(!R.id)throw new Error(`Function ${R.name} did not return an ID`);let P=s.getOptions().tracer??ie.tracer;return P?P.startActiveSpan(`Tool: ${R.name}`,async v=>{try{v?.setAttributes?.({"tool.name":R.name,"tool.mode":"native","function.id":R.id,"session.id":r??""});let{formatted:E,rawResult:k,parsedArgs:O}=await f.executeWithDetails(R,{sessionId:r,ai:s,functionResultFormatter:u,traceId:v?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(x.add(R.name.toLowerCase()),m?._recordFunctionCall(R.name,O,k),d?.includes(R.name.toLowerCase())){let L=h(R.name);L&&b.push({func:L,args:O,result:k})}if(a?v.addEvent("gen_ai.tool.message",{name:R.name}):v.addEvent("gen_ai.tool.message",{name:R.name,args:R.args,result:E??""}),i){let L={name:R.name};a||(L.args=R.args,L.result=E??""),i.addEvent("function.call",L)}return{result:E??"",role:"function",functionId:R.id,index:l}}catch(E){if(v?.recordException?.(E),E instanceof Mr){let k=E.getFixingInstructions(),O={name:R.name,message:E.toString()};return a||(O.args=R.args,O.fixing_instructions=k),v?.addEvent?.("function.error",O),p&&js(E,l,k,c),{functionId:R.id,isError:!0,index:l,result:k,role:"function"}}throw E}finally{v?.end?.()}}):f.executeWithDetails(R,{sessionId:r,ai:s,functionResultFormatter:u,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:v,rawResult:E,parsedArgs:k})=>{if(x.add(R.name.toLowerCase()),m?._recordFunctionCall(R.name,k,E),d?.includes(R.name.toLowerCase())){let O=h(R.name);O&&b.push({func:O,args:k,result:E})}if(i){let O={name:R.name};a||(O.args=R.args,O.result=v??""),i.addEvent("function.call",O)}return{result:v??"",role:"function",functionId:R.id,index:l}}).catch(v=>{if(!(v instanceof Mr))throw v;let E=v.getFixingInstructions();if(i){let k={name:R.name,message:v.toString()};a||(k.args=R.args,k.fixing_instructions=E),i.addEvent("function.error",k)}return p&&js(v,l,E,c),{functionId:R.id,isError:!0,index:l,result:E,role:"function"}})}),C=(await Promise.all(y)).map(R=>R.result===void 0||R.result===""?{...R,result:"done"}:R);if(n.addFunctionResults(C,r),p){let R=C.filter(P=>!P.isError);R.length>0&&Fa(R,c)}if(b.length>0)throw new Kt(b);return x};function Pi(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 du(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 Hp(s){let e=0,t=!1,n=!1,r=!1,o=!1,i=[];for(let a=0;a<s.length;a++){let l=s[a];if(n){n=!1;continue}if(l==="\\"){n=!0;continue}if(l==='"'){t=!t;continue}t||(l==="{"?(i.push("{"),e++):l==="["?(i.push("["),e++):l==="}"?i.length>0&&i[i.length-1]==="{"&&(i.pop(),e--):l==="]"&&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 mu(s){if(!s.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(s),partialMarker:null}}catch{}let e=Hp(s),t=Kp(s);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function Kp(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*gu({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,l=[],u=s.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 f of m.citations)f?.url&&l.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*Wp({...o,result:m,skipEarlyFail:i,state:g,debug:n})}}}finally{u.releaseLock()}for(let c of t)yield*Vp({...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*Wp({result:s,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:i,signature:a,streamingFieldProcessors:l,thoughtFieldName:u,streamingAsserts:c,asserts:p}){if(s.functionCalls&&s.functionCalls.length>0)Zr(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:{[u]: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}=mu(i.content);if(f&&typeof f=="object"){let b={},h={};for(let y of Object.keys(f))if(d.some(I=>I.name===y)){let I=f[y],C=i.values[y];if(Array.isArray(I)&&I.length>0&&Jp(I,x)&&(I=I.slice(0,-1)),h[y]=I,typeof I=="string"&&typeof C=="string"&&I.startsWith(C)){let R=I.slice(C.length);R&&(b[y]=R)}else Array.isArray(I)&&Array.isArray(C)?I.length>C.length&&(b[y]=I.slice(C.length)):JSON.stringify(I)!==JSON.stringify(C)&&(Array.isArray(I)||(b[y]=I))}try{Mn(a,h,{allowMissingRequired:!0})}catch{}Object.assign(i.values,h),Object.keys(b).length>0&&(yield{index:s.index,delta:b});return}}if(vi(a,i.values,i.xstate,i.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await xi(c,i.xstate,i.content),l.length!==0&&await Mi(l,i.content,i.xstate,e,i.values,t),yield*Ei(a,i.content,i.values,i.xstate,s.index),await vn(p,i.values)}else s.thought&&s.thought.length>0?(i.values[u]=(i.values[u]??"")+s.thought,yield{index:s.index,delta:{[u]: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*Vp({state:s,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:i,traceId:a,span:l,strictMode:u,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:f,signatureToolCallingManager:x,logger:b,debug:h,stopFunctionNames:y,stepContext:I,abortSignal:C}){let R=x?void 0:Pi(t,s.functionCalls,s.values,n);if(R){if(!r)throw new Error("Functions are not defined");let P=await Ko({ai:t,functionList:r,functionCalls:R,mem:o,sessionId:i,traceId:a,span:l,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:b,debug:h,stopFunctionNames:y,step:I,abortSignal:C});s.functionsExecuted=new Set([...s.functionsExecuted,...P]),s.functionCalls=[]}else{let P=e.getOutputFields(),v=e.hasComplexFields(),E=!1;if(v)try{let k=JSON.parse(s.content),O={};for(let L of Object.keys(k))if(P.some(F=>F.name===L)){let F=k[L],M=s.values[L];if(typeof F=="string"&&typeof M=="string"&&F.startsWith(M)){let _=F.slice(M.length);_&&(O[L]=_)}else Array.isArray(F)&&Array.isArray(M)?F.length>M.length&&(O[L]=F.slice(M.length)):JSON.stringify(F)!==JSON.stringify(M)&&(Array.isArray(F)||(O[L]=F))}try{Mn(e,O,{allowMissingRequired:!0})}catch(L){let F=(L.message||"").toLowerCase();if(F.includes("at least")||F.includes("at most")||F.includes("must match pattern")||F.includes("invalid url")||F.includes("required")||F.includes("missing")||F.includes("valid email")||F.includes("number must be"))throw L}for(let L of Object.keys(k))P.some(F=>F.name===L)&&(s.values[L]=k[L]);yield{index:s.index,delta:O},E=!0}catch(k){let O=(k.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 k}if(!E){let k=x!==void 0;Oi(e,s.values,s.xstate,s.content,{strictMode:u,treatAllFieldsOptional:k,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(x){let k=await x.processResults(s.values);if(k&&k.length>0){if(!r)throw new Error("Functions are not defined");let O=await Ko({ai:t,functionList:r,functionCalls:k,mem:o,sessionId:i,traceId:a,span:l,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:b,debug:h,stopFunctionNames:y,step:I,abortSignal:C});s.functionsExecuted=new Set([...s.functionsExecuted,...O]),o.updateResult({name:void 0,content:s.content,functionCalls:k.map(L=>({id:L.id,type:"function",function:{name:L.name,params:L.args}})),index:s.index},i);return}}await xi(p,s.xstate,s.content,!0),await vn(d,s.values),m.length&&await Or(m,s.values,o,i),g.length!==0&&await Mi(g,s.content,s.xstate,o,s.values,i,!0),yield*Ei(e,s.content,s.values,s.xstate,s.index)}}async function*fu({ai:s,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:i,strictMode:a,states:l,usage:u,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:f,logger:x,debug:b,signatureToolCallingManager:h,stopFunctionNames:y,disableMemoryCleanup:I,stepContext:C,abortSignal:R}){let P=e.results??[],v=h!==void 0;t.addResponse(P,n);let E=[];for(let F of P)if(Array.isArray(F?.citations))for(let M of F.citations)M?.url&&E.push({url:M.url,title:M.title,description:M.description,license:M.license,publicationDate:M.publicationDate,snippet:M.snippet});for(let F of P){let M=l[F.index];if(!M)throw new Error(`No state found for result (index: ${F.index})`);if(e.modelUsage){let _=Array.from(new Map(E.filter(H=>H.url).map(H=>[H.url,H])).values()),j={...e.modelUsage,..._.length?{citations:_}:{}};if(u.push(j),b&&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&&F.content){F.thought&&F.thought.length>0&&(M.values[m]=F.thought),zo(g,M.values,F.content,{strictMode:a,treatAllFieldsOptional:v});let j=(await h.processResults(M.values))?.map(H=>({id:H.id,type:"function",function:{name:H.name,params:H.args}}));j&&j.length>0&&t.updateResult({name:F.name,content:F.content,functionCalls:j,index:F.index},n)}if(F.functionCalls?.length){let _=Pi(s,F.functionCalls,M.values);if(_&&_.length>0){if(!o)throw new Error("Functions are not defined");let j;try{j=await Ko({ai:s,functionList:o,functionCalls:_,mem:t,sessionId:n,traceId:r,span:i,excludeContentFromTrace:c,index:F.index,functionResultFormatter:f,logger:x,debug:b,stopFunctionNames:y,step:C,abortSignal:R})}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}M.functionsExecuted=new Set([...M.functionsExecuted,...j])}}else if(F.content){F.thought&&F.thought.length>0&&(M.values[m]=F.thought);let _=g.getOutputFields();if(g.hasComplexFields())try{let H=JSON.parse(F.content),D={};for(let Y of Object.keys(H))_.some(G=>G.name===Y)&&(D[Y]=H[Y]);Mn(g,D),Object.assign(M.values,D)}catch(H){let D=H instanceof SyntaxError;if((H.name?.includes("ValidationError")||H.name?.includes("Error"))&&!D){let G=(H.message||"").toLowerCase();if(G.includes("at least")||G.includes("at most")||G.includes("must match pattern")||G.includes("invalid url")||G.includes("required")||G.includes("missing")||G.includes("valid email")||G.includes("number must be"))throw H}if(D)zo(g,M.values,F.content,{strictMode:a,treatAllFieldsOptional:v});else throw H}else zo(g,M.values,F.content,{strictMode:a,treatAllFieldsOptional:v})}if(await vn(p,M.values),I||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Or(d,M.values,t,n),F.finishReason==="length")throw new Error(`Max tokens reached before completion
216
- Content: ${F.content}`)}let k=l.map(F=>F.values);for(let F of k)for(let M of g.getOutputFields())M.isInternal&&delete F[M.name];let O=g.getOutputFields(),L=k.map((F,M)=>{let _={};for(let j of O)j.isInternal||(_[j.name]=F[j.name]);return F[m]!==void 0&&(_[m]=F[m]),{index:M,delta:_}});for(let F of L)yield F}function Jp(s,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function hu(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 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 Qp=["none","minimal","low","medium","high","highest"];function Fi(s,e,t){let n={};if(e.model!==!1){let a=s.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:[...Qp],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=Wt(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 Pn=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 ae=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},_i=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new ae("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 ae("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 ae('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 ae(`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 ae)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new ae(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new ae(`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 ae(`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 ae(`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 ae("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new ae("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 ae(`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 ae?r:new ae(`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 ae(`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 ae?r:new ae(`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 ae(`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 ae(`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 ae?o:new ae(`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 ae(`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 ae(`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:l}}else try{let i=this.parseTypeNotClass(),a=this.match("[]");if(r={name:i,isArray:a},i==="image"&&a)throw new ae(`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 ae(`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 ae(`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 ae(`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 ae?i:new ae(`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(ie.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 ae(`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 ae(`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 ae(`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 ae(`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 ae(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 ae("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 ae(`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 ae(`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 ae(`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 ae(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function xu(s){return new _i(s).parse()}var Fr=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,Vo(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,Vo(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 Fe(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},Be=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}},pe=Object.assign(()=>new Fr,{string:s=>new Be({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),number:s=>new Be({type:"number",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),boolean:s=>new Be({type:"boolean",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),json:s=>new Be({type:"json",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),datetime:s=>new Be({type:"datetime",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),date:s=>new Be({type:"date",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),class:(s,e)=>new Be({type:"class",isArray:!1,options:s,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:s=>new Be({type:"image",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),audio:s=>new Be({type:"audio",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),file:s=>new Be({type:"file",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),url:s=>new Be({type:"url",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),email:s=>new Be({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(s,e)=>new Be({type:"code",isArray:!1,description:e||s,isOptional:!1,isInternal:!1,isCached:!1}),object:(s,e)=>new Be({type:"object",isArray:!1,fields:s,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function Vo(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,Vo(t)])):void 0}}function Wo(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 Ni(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,Vo(n)])):void 0},description:e.description,isOptional:e.isOptional||void 0,isInternal:e.isInternal||void 0,isCached:e.isCached||void 0}}function Au(s){return s.length===0?{type:"object",properties:{}}:$t(s,"Schema")}function Yp(s){let e=Ni("__value",s);return $t([e],"Schema").properties?.__value??{type:"json"}}var Li=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(Ni(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(Ni(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:Au(this.argFields),...this.returnMode==="single"&&this.returnFieldType?{returns:Yp(this.returnFieldType)}:this.returnMode==="fields"?{returns:Au(this.returnFields)}:{},...this.fnExamples.length>0?{examples:this.fnExamples.map(o=>({...o}))}:{},func:this.fnHandler}}},Iu=s=>new Li(s),ee=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},Fe=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=xu(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 ee(`Invalid Signature: ${n.message}`,void 0,r)}throw new ee(`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 ee("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 ee("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 ee?t:new ee(`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 ee("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 ee("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 ee("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 ee("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 ee(`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 ee(`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 ee?t:new ee(`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 ee(`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 ee(`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 ee?t:new ee(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new ee("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 ee?t:new ee(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new ee("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 ee?t:new ee(`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,...Wo(t)}),n};prependInputField=(e,t)=>{let n=new s(this),r={name:e,...Wo(t)},o=n.parseField(r);Rt(o,"input");for(let i of n.inputFields)if(i.name===o.name)throw new ee(`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 ee(`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,...Wo(t)}),n};prependOutputField=(e,t)=>{let n=new s(this),r={name:e,...Wo(t)},o=n.parseField(r);Rt(o,"output");for(let i of n.outputFields)if(i.name===o.name)throw new ee(`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 ee(`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=tt("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=bu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof ee?e:new ee(`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=tt("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=bu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof ee?e:new ee(`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 ee(`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 ee(`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 ee(`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 ee("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new ee("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 $t(e,this.description??"Schema")};toInputJSONSchema=()=>$t(this.inputFields,this.description??"Schema")};function yu(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 bu(s,e,t){let n=s?`"${s}" `:"",r=e.map(yu).join(", "),o=t.map(yu).join(", ");return`${n}${r} -> ${o}`}function Xp(s){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(s)||t.test(s)}function Rt(s,e){if(!s.name||s.name.length===0)throw new ee("Field name cannot be blank",s.name,"Every field must have a descriptive name");if(!Xp(s.name))throw new ee(`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(ie.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 ee(`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 ee(`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 ee(`Field name '${s.name}' is too long (${s.name.length} characters)`,s.name,"Field names should be 50 characters or less");s.type&&Zp(s,e)}function Zp(s,e){if(!s.type)return;let{type:t}=s;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new ee(`${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 ee("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 ee("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 ee("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 ee(`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 ee("Duplicate class options found",s.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new ee("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 ee("Internal marker (!) is not allowed on input fields",s.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&$i(t.fields,s.name,e)}function $i(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 ee(`${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&&$i(o.fields,i,t,n+1),o.isArray&&o.fields&&$i(o.fields,`${i}[]`,t,n+1)}}var Dt=class s{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new Fe(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 Pn,this.key={id:"root"}}getSignature(){return new Fe(this.signature)}setSignature(e){this.signature=new Fe(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 l of o){let u=i[l.name];u!==void 0&&(kr(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 vr(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 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()],i=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 f=u[g.name];f!==void 0&&(kr(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 l=s._propagating;s._propagating=!0;try{for(let u of Array.from(this.children))u?.setDemos(e,t)}finally{s._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}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 Cu={"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]},Yo=async({ai:s,functionList:e,functionCalls:t,mem:n,sessionId:r,traceId:o,span:i,excludeContentFromTrace:a,index:l,functionResultFormatter:u,logger:c,debug:p,stopFunctionNames:d,step:m,abortSignal:g})=>{let f=new Nr(e),x=new Set,A=[],h=R=>{let M=k=>k.replace(/[^a-zA-Z0-9]/g,"").toLowerCase(),S=M(R),O=e.find(k=>k.name===R);return O||(O=e.find(k=>M(k.name)===S)),O},b=t.map(R=>{if(!R.id)throw new Error(`Function ${R.name} did not return an ID`);let M=s.getOptions().tracer??le.tracer;return M?M.startActiveSpan(`Tool: ${R.name}`,async S=>{try{S?.setAttributes?.({"tool.name":R.name,"tool.mode":"native","function.id":R.id,"session.id":r??""});let{formatted:O,rawResult:k,parsedArgs:_}=await f.executeWithDetails(R,{sessionId:r,ai:s,functionResultFormatter:u,traceId:S?.spanContext?.().traceId??o,stopFunctionNames:d,step:m,abortSignal:g});if(x.add(R.name.toLowerCase()),m?._recordFunctionCall(R.name,_,k),d?.includes(R.name.toLowerCase())){let L=h(R.name);L&&A.push({func:L,args:_,result:k})}if(a?S.addEvent("gen_ai.tool.message",{name:R.name}):S.addEvent("gen_ai.tool.message",{name:R.name,args:R.args,result:O??""}),i){let L={name:R.name};a||(L.args=R.args,L.result=O??""),i.addEvent("function.call",L)}return{result:O??"",role:"function",functionId:R.id,index:l}}catch(O){if(S?.recordException?.(O),O instanceof _r){let k=O.getFixingInstructions(),_={name:R.name,message:O.toString()};return a||(_.args=R.args,_.fixing_instructions=k),S?.addEvent?.("function.error",_),p&&Ks(O,l,k,c),{functionId:R.id,isError:!0,index:l,result:k,role:"function"}}throw O}finally{S?.end?.()}}):f.executeWithDetails(R,{sessionId:r,ai:s,functionResultFormatter:u,traceId:o,stopFunctionNames:d,step:m,abortSignal:g}).then(({formatted:S,rawResult:O,parsedArgs:k})=>{if(x.add(R.name.toLowerCase()),m?._recordFunctionCall(R.name,k,O),d?.includes(R.name.toLowerCase())){let _=h(R.name);_&&A.push({func:_,args:k,result:O})}if(i){let _={name:R.name};a||(_.args=R.args,_.result=S??""),i.addEvent("function.call",_)}return{result:S??"",role:"function",functionId:R.id,index:l}}).catch(S=>{if(!(S instanceof _r))throw S;let O=S.getFixingInstructions();if(i){let k={name:R.name,message:S.toString()};a||(k.args=R.args,k.fixing_instructions=O),i.addEvent("function.error",k)}return p&&Ks(S,l,O,c),{functionId:R.id,isError:!0,index:l,result:O,role:"function"}})}),C=(await Promise.all(b)).map(R=>R.result===void 0||R.result===""?{...R,result:"done"}:R);if(n.addFunctionResults(C,r),p){let R=C.filter(M=>!M.isError);R.length>0&&Ba(R,c)}if(A.length>0)throw new Jt(A);return x};function $i(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 bu(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 Jp(s){let e=0,t=!1,n=!1,r=!1,o=!1,i=[];for(let a=0;a<s.length;a++){let l=s[a];if(n){n=!1;continue}if(l==="\\"){n=!0;continue}if(l==='"'){t=!t;continue}t||(l==="{"?(i.push("{"),e++):l==="["?(i.push("["),e++):l==="}"?i.length>0&&i[i.length-1]==="{"&&(i.pop(),e--):l==="]"&&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 Iu(s){if(!s.trim())return{parsed:null,partialMarker:null};try{return{parsed:JSON.parse(s),partialMarker:null}}catch{}let e=Jp(s),t=Qp(s);try{return{parsed:JSON.parse(t),partialMarker:e}}catch{return{parsed:null,partialMarker:e}}}function Qp(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*Cu({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,l=[],u=s.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 f of m.citations)f?.url&&l.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*Yp({...o,result:m,skipEarlyFail:i,state:g,debug:n})}}}finally{u.releaseLock()}for(let c of t)yield*Xp({...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*Yp({result:s,mem:e,sessionId:t,strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o,state:i,signature:a,streamingFieldProcessors:l,thoughtFieldName:u,streamingAsserts:c,asserts:p}){if(s.functionCalls&&s.functionCalls.length>0)oo(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:{[u]: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}=Iu(i.content);if(f&&typeof f=="object"){let A={},h={};for(let b of Object.keys(f))if(d.some(I=>I.name===b)){let I=f[b],C=i.values[b];if(Array.isArray(I)&&I.length>0&&Zp(I,x)&&(I=I.slice(0,-1)),h[b]=I,typeof I=="string"&&typeof C=="string"&&I.startsWith(C)){let R=I.slice(C.length);R&&(A[b]=R)}else Array.isArray(I)&&Array.isArray(C)?I.length>C.length&&(A[b]=I.slice(C.length)):JSON.stringify(I)!==JSON.stringify(C)&&(Array.isArray(I)||(A[b]=I))}try{_n(a,h,{allowMissingRequired:!0})}catch{}Object.assign(i.values,h),Object.keys(A).length>0&&(yield{index:s.index,delta:A});return}}if(Fi(a,i.values,i.xstate,i.content,{strictMode:n,skipEarlyFail:r,treatAllFieldsOptional:o}))return;c.length!==0&&await Ci(c,i.xstate,i.content),l.length!==0&&await Li(l,i.content,i.xstate,e,i.values,t),yield*Ni(a,i.content,i.values,i.xstate,s.index),await Mn(p,i.values)}else s.thought&&s.thought.length>0?(i.values[u]=(i.values[u]??"")+s.thought,yield{index:s.index,delta:{[u]: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*Xp({state:s,signature:e,ai:t,model:n,functions:r,mem:o,sessionId:i,traceId:a,span:l,strictMode:u,excludeContentFromTrace:c,streamingAsserts:p,asserts:d,fieldProcessors:m,streamingFieldProcessors:g,functionResultFormatter:f,signatureToolCallingManager:x,logger:A,debug:h,stopFunctionNames:b,stepContext:I,abortSignal:C}){let R=x?void 0:$i(t,s.functionCalls,s.values,n);if(R){if(!r)throw new Error("Functions are not defined");let M=await Yo({ai:t,functionList:r,functionCalls:R,mem:o,sessionId:i,traceId:a,span:l,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:A,debug:h,stopFunctionNames:b,step:I,abortSignal:C});s.functionsExecuted=new Set([...s.functionsExecuted,...M]),s.functionCalls=[]}else{let M=e.getOutputFields(),S=e.hasComplexFields(),O=!1;if(S)try{let k=JSON.parse(s.content),_={};for(let L of Object.keys(k))if(M.some(E=>E.name===L)){let E=k[L],P=s.values[L];if(typeof E=="string"&&typeof P=="string"&&E.startsWith(P)){let $=E.slice(P.length);$&&(_[L]=$)}else Array.isArray(E)&&Array.isArray(P)?E.length>P.length&&(_[L]=E.slice(P.length)):JSON.stringify(E)!==JSON.stringify(P)&&(Array.isArray(E)||(_[L]=E))}try{_n(e,_,{allowMissingRequired:!0})}catch(L){let E=(L.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 L}for(let L of Object.keys(k))M.some(E=>E.name===L)&&(s.values[L]=k[L]);yield{index:s.index,delta:_},O=!0}catch(k){let _=(k.message||"").toLowerCase();if(_.includes("at least")||_.includes("at most")||_.includes("must match pattern")||_.includes("invalid url")||_.includes("required")||_.includes("missing")||_.includes("valid email")||_.includes("number must be"))throw k}if(!O){let k=x!==void 0;_i(e,s.values,s.xstate,s.content,{strictMode:u,treatAllFieldsOptional:k,deferRequiredCheckForStreaming:!0,forceFinalize:!0})}if(x){let k=await x.processResults(s.values);if(k&&k.length>0){if(!r)throw new Error("Functions are not defined");let _=await Yo({ai:t,functionList:r,functionCalls:k,mem:o,sessionId:i,traceId:a,span:l,index:s.index,excludeContentFromTrace:c,functionResultFormatter:f,logger:A,debug:h,stopFunctionNames:b,step:I,abortSignal:C});s.functionsExecuted=new Set([...s.functionsExecuted,..._]),o.updateResult({name:void 0,content:s.content,functionCalls:k.map(L=>({id:L.id,type:"function",function:{name:L.name,params:L.args}})),index:s.index},i);return}}await Ci(p,s.xstate,s.content,!0),await Mn(d,s.values),m.length&&await Pr(m,s.values,o,i),g.length!==0&&await Li(g,s.content,s.xstate,o,s.values,i,!0),yield*Ni(e,s.content,s.values,s.xstate,s.index)}}async function*wu({ai:s,res:e,mem:t,sessionId:n,traceId:r,functions:o,span:i,strictMode:a,states:l,usage:u,excludeContentFromTrace:c,asserts:p,fieldProcessors:d,thoughtFieldName:m,signature:g,functionResultFormatter:f,logger:x,debug:A,signatureToolCallingManager:h,stopFunctionNames:b,disableMemoryCleanup:I,stepContext:C,abortSignal:R}){let M=e.results??[],S=h!==void 0;t.addResponse(M,n);let O=[];for(let E of M)if(Array.isArray(E?.citations))for(let P of E.citations)P?.url&&O.push({url:P.url,title:P.title,description:P.description,license:P.license,publicationDate:P.publicationDate,snippet:P.snippet});for(let E of M){let P=l[E.index];if(!P)throw new Error(`No state found for result (index: ${E.index})`);if(e.modelUsage){let $=Array.from(new Map(O.filter(z=>z.url).map(z=>[z.url,z])).values()),D={...e.modelUsage,...$.length?{citations:$}:{}};if(u.push(D),A&&x){let z=structuredClone(D);delete z.citations,x({name:"ChatResponseUsage",value:z}),D.citations&&D.citations.length>0&&x({name:"ChatResponseCitations",value:D.citations})}}if(h&&E.content){E.thought&&E.thought.length>0&&(P.values[m]=E.thought),Vo(g,P.values,E.content,{strictMode:a,treatAllFieldsOptional:S});let D=(await h.processResults(P.values))?.map(z=>({id:z.id,type:"function",function:{name:z.name,params:z.args}}));D&&D.length>0&&t.updateResult({name:E.name,content:E.content,functionCalls:D,index:E.index},n)}if(E.functionCalls?.length){let $=$i(s,E.functionCalls,P.values);if($&&$.length>0){if(!o)throw new Error("Functions are not defined");let D;try{D=await Yo({ai:s,functionList:o,functionCalls:$,mem:t,sessionId:n,traceId:r,span:i,excludeContentFromTrace:c,index:E.index,functionResultFormatter:f,logger:x,debug:A,stopFunctionNames:b,step:C,abortSignal:R})}catch(z){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),z}P.functionsExecuted=new Set([...P.functionsExecuted,...D])}}else if(E.content){E.thought&&E.thought.length>0&&(P.values[m]=E.thought);let $=g.getOutputFields();if(g.hasComplexFields())try{let z=JSON.parse(E.content),U={};for(let te of Object.keys(z))$.some(B=>B.name===te)&&(U[te]=z[te]);_n(g,U),Object.assign(P.values,U)}catch(z){let U=z instanceof SyntaxError;if((z.name?.includes("ValidationError")||z.name?.includes("Error"))&&!U){let B=(z.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 z}if(U)Vo(g,P.values,E.content,{strictMode:a,treatAllFieldsOptional:S});else throw z}else Vo(g,P.values,E.content,{strictMode:a,treatAllFieldsOptional:S})}if(await Mn(p,P.values),I||(t.removeByTag("correction",n),t.removeByTag("error",n)),d.length&&await Pr(d,P.values,t,n),E.finishReason==="length")throw new Error(`Max tokens reached before completion
216
+ Content: ${E.content}`)}let k=l.map(E=>E.values);for(let E of k)for(let P of g.getOutputFields())P.isInternal&&delete E[P.name];let _=g.getOutputFields(),L=k.map((E,P)=>{let $={};for(let D of _)D.isInternal||($[D.name]=E[D.name]);return E[m]!==void 0&&($[m]=E[m]),{index:P,delta:$}});for(let E of L)yield E}function Zp(s,e){return e?!!(e.nestingLevel>0||e.inArray||e.inObject):!1}function Tu(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 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 ed=["none","minimal","low","medium","high","highest"];function Di(s,e,t){let n={};if(e.model!==!1){let a=s.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:[...ed],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=Qt(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 Nn=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 ue=class extends Error{constructor(t,n,r,o){super(t);this.position=n;this.context=r;this.suggestion=o;this.name="SignatureValidationError"}},Gi=class{input;position;currentFieldName=null;currentSection="description";constructor(e){if(this.input=e.trim(),this.position=0,!this.input)throw new ue("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 ue("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 ue('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 ue(`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 ue)throw e;let t=e instanceof Error?e.message:"Unknown error";throw new ue(t,this.position,this.getErrorContext())}}validateParsedSignature(e){let t=new Set;for(let r of e.inputs){if(t.has(r.name))throw new ue(`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 ue(`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 ue(`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 ue("Signature must have at least one input field",0,"",'Add an input field before "->". Example: "userInput:string -> ..."');if(e.outputs.length===0)throw new ue("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 ue(`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 ue?r:new ue(`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 ue(`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 ue?r:new ue(`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 ue(`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 ue(`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 ue?o:new ue(`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 ue(`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 ue(`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:l}}else try{let i=this.parseTypeNotClass(),a=this.match("[]");if(r={name:i,isArray:a},i==="image"&&a)throw new ue(`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 ue(`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 ue(`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 ue(`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 ue?i:new ue(`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(le.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 ue(`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 ue(`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 ue(`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 ue(`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 ue(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 ue("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 ue(`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 ue(`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 ue(`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 ue(`Expected "->" but found "${e}..."`,this.position,this.getErrorContext(),t)}}};function Ru(s){return new Gi(s).parse()}var Lr=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,Zo(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,Zo(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 _e(e);return this._useStructuredOutputs&&(t._forceComplexFields=!0,t._hasComplexFields=void 0),t}},qe=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}},ae=Object.assign(()=>new Lr,{string:s=>new qe({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),number:s=>new qe({type:"number",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),boolean:s=>new qe({type:"boolean",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),json:s=>new qe({type:"json",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),datetime:s=>new qe({type:"datetime",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),date:s=>new qe({type:"date",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),class:(s,e)=>new qe({type:"class",isArray:!1,options:s,description:e,isOptional:!1,isInternal:!1,isCached:!1}),image:s=>new qe({type:"image",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),audio:s=>new qe({type:"audio",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),file:s=>new qe({type:"file",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),url:s=>new qe({type:"url",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1}),email:s=>new qe({type:"string",isArray:!1,description:s,isOptional:!1,isInternal:!1,isCached:!1,format:"email"}),code:(s,e)=>new qe({type:"code",isArray:!1,description:e||s,isOptional:!1,isInternal:!1,isCached:!1}),object:(s,e)=>new qe({type:"object",isArray:!1,fields:s,description:e,isOptional:!1,isInternal:!1,isCached:!1})});function Zo(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,Zo(t)])):void 0}}function Xo(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 Ui(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,Zo(n)])):void 0},description:e.description,isOptional:e.isOptional||void 0,isInternal:e.isInternal||void 0,isCached:e.isCached||void 0}}function Su(s){return s.length===0?{type:"object",properties:{}}:Gt(s,"Schema")}function td(s){let e=Ui("__value",s);return Gt([e],"Schema").properties?.__value??{type:"json"}}var Bi=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(Ui(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(Ui(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:Su(this.argFields),...this.returnMode==="single"&&this.returnFieldType?{returns:td(this.returnFieldType)}:this.returnMode==="fields"?{returns:Su(this.returnFields)}:{},...this.fnExamples.length>0?{examples:this.fnExamples.map(o=>({...o}))}:{},func:this.fnHandler}}},Ou=s=>new Bi(s),ne=class extends Error{constructor(t,n,r){super(t);this.fieldName=n;this.suggestion=r;this.name="AxSignatureValidationError"}},_e=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=Ru(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 ne(`Invalid Signature: ${n.message}`,void 0,r)}throw new ne(`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 ne("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 ne("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 ne?t:new ne(`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 ne("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 ne("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 ne("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 ne("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);Ot(t,"input");for(let n of this.inputFields)if(n.name===t.name)throw new ne(`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 ne(`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 ne?t:new ne(`Failed to add input field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};addOutputField=e=>{try{let t=this.parseField(e);Ot(t,"output");for(let n of this.outputFields)if(n.name===t.name)throw new ne(`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 ne(`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 ne?t:new ne(`Failed to add output field "${e.name}": ${t instanceof Error?t.message:"Unknown error"}`,e.name)}};setInputFields=e=>{if(!Array.isArray(e))throw new ne("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 Ot(r,"input"),r});this.inputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof ne?t:new ne(`Failed to set input fields: ${t instanceof Error?t.message:"Unknown error"}`)}};setOutputFields=e=>{if(!Array.isArray(e))throw new ne("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 Ot(r,"output"),r});this.outputFields=t,this.invalidateValidationCache(),this.updateHashLight()}catch(t){throw t instanceof ne?t:new ne(`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,...Xo(t)}),n};prependInputField=(e,t)=>{let n=new s(this),r={name:e,...Xo(t)},o=n.parseField(r);Ot(o,"input");for(let i of n.inputFields)if(i.name===o.name)throw new ne(`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 ne(`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,...Xo(t)}),n};prependOutputField=(e,t)=>{let n=new s(this),r={name:e,...Xo(t)},o=n.parseField(r);Ot(o,"output");for(let i of n.outputFields)if(i.name===o.name)throw new ne(`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 ne(`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=>{Ot(e,"input")}),this.getOutputFields().forEach(e=>{Ot(e,"output")}),this.sigHash=ut("sha256").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=vu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof ne?e:new ne(`Signature validation failed: ${e instanceof Error?e.message:"Unknown error"}`)}};updateHash=()=>{try{return this.getInputFields().forEach(e=>{Ot(e,"input")}),this.getOutputFields().forEach(e=>{Ot(e,"output")}),this.validateSignatureConsistency(),this.sigHash=ut("sha256").update(this.description??"").update(JSON.stringify(this.inputFields)).update(JSON.stringify(this.outputFields)).digest("hex"),this.sigString=vu(this.description,this.inputFields,this.outputFields),this._hasComplexFields=this.computeHasComplexFields(),[this.sigHash,this.sigString]}catch(e){throw e instanceof ne?e:new ne(`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 ne(`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 ne(`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 ne(`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 ne("Signature must have at least one input field",void 0,'Add an input field. Example: "userInput:string -> ..."');if(this.outputFields.length===0)throw new ne("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 Gt(e,this.description??"Schema")};toInputJSONSchema=()=>Gt(this.inputFields,this.description??"Schema")};function ku(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 vu(s,e,t){let n=s?`"${s}" `:"",r=e.map(ku).join(", "),o=t.map(ku).join(", ");return`${n}${r} -> ${o}`}function nd(s){let e=/^[a-z][a-zA-Z0-9]*$/,t=/^[a-z]+(_[a-z0-9]+)*$/;return e.test(s)||t.test(s)}function Ot(s,e){if(!s.name||s.name.length===0)throw new ne("Field name cannot be blank",s.name,"Every field must have a descriptive name");if(!nd(s.name))throw new ne(`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(le.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 ne(`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 ne(`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 ne(`Field name '${s.name}' is too long (${s.name.length} characters)`,s.name,"Field names should be 50 characters or less");s.type&&rd(s,e)}function rd(s,e){if(!s.type)return;let{type:t}=s;if((t.name==="image"||t.name==="audio"||t.name==="file")&&e==="output")throw new ne(`${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 ne("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 ne("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 ne("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 ne(`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 ne("Duplicate class options found",s.name,"Each class option must be unique (case-insensitive)")}if(t.name==="code"&&t.isArray)throw new ne("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 ne("Internal marker (!) is not allowed on input fields",s.name,"Internal markers are only allowed on output fields");t.name==="object"&&t.fields&&ji(t.fields,s.name,e)}function ji(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 ne(`${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&&ji(o.fields,i,t,n+1),o.isArray&&o.fields&&ji(o.fields,`${i}[]`,t,n+1)}}var Ut=class s{signature;sigHash;examples;examplesOptions;demos;trace;usage=[];traceLabel;key;children;childNames=new Map;childCount=0;constructor(e,t){this.signature=new _e(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 Nn,this.key={id:"root"}}getSignature(){return new _e(this.signature)}setSignature(e){this.signature=new _e(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 l of o){let u=i[l.name];u!==void 0&&(Er(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 Mr(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 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()],i=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 f=u[g.name];f!==void 0&&(Er(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 l=s._propagating;s._propagating=!0;try{for(let u of Array.from(this.children))u?.setDemos(e,t)}finally{s._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}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 Eu={"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.
@@ -287,6 +287,9 @@ Read the descriptions above carefully \u2014 they tell you what each field conta
287
287
 
288
288
  You are working with context fields that may be large, deeply nested, or unfamiliar. **Always explore before you extract.** Follow this progression:
289
289
 
290
+ **Step 0 \u2014 Read descriptions first.**
291
+ If a field description already tells you the type and schema (e.g. "Array of email objects with fields: from, subject, body, date"), skip probing and go straight to Step 3 (narrow with JS). Only probe shape when descriptions are vague or absent.
292
+
290
293
  **Step 1 \u2014 Probe shape. Never dump raw values.**
291
294
  \`\`\`js
292
295
  console.log(typeof inputs.emails, Array.isArray(inputs.emails),
@@ -302,7 +305,7 @@ console.log(JSON.stringify(inputs.emails[0], null, 2));
302
305
  \`\`\`js
303
306
  const matches = inputs.emails.filter(e =>
304
307
  e.subject.toLowerCase().includes('invoice'));
305
- console.log(\`Found \${matches.length} matches\`);
308
+ console.log('Found ' + matches.length + ' matches');
306
309
  console.log(JSON.stringify(matches.slice(0, 2), null, 2));
307
310
  \`\`\`
308
311
 
@@ -410,9 +413,17 @@ console.log(inputs.emails.length);
410
413
  ### When to Use JS vs. \`llmQuery\`
411
414
 
412
415
  - **Use JS** for structural tasks: filtering, counting, sorting, extracting fields, slicing strings, date comparisons, deduplication, regex matching \u2014 anything with clear deterministic logic.
416
+ {{ if llmQueryPromptMode === 'advanced-recursive' }}
417
+ - **Use \`llmQuery\`** for focused delegated subtasks that may need their own semantic reasoning, tool usage, discovery calls, or multiple child turns.
418
+ {{ else }}
413
419
  - **Use \`llmQuery\`** for semantic tasks: summarizing content, classifying tone or intent, extracting meaning from unstructured text, answering subjective questions about content.
420
+ {{ /if }}
414
421
 
422
+ {{ if llmQueryPromptMode === 'advanced-recursive' }}
423
+ **The pattern is: JS narrows first, then \`llmQuery\` delegates a focused child workflow.**
424
+ {{ else }}
415
425
  **The pattern is always: JS narrows first, then \`llmQuery\` interprets.**
426
+ {{ /if }}
416
427
 
417
428
  \`\`\`js
418
429
  // JS narrows to relevant records
@@ -427,14 +438,129 @@ console.log(answer);
427
438
 
428
439
  Never pass raw unsliced \`inputs.*\` fields directly to \`llmQuery\` \u2014 always narrow with JS first.
429
440
 
441
+ {{ if llmQueryPromptMode === 'advanced-recursive' }}
442
+ {{ if promptLevel === 'detailed' }}
443
+ ### Delegation Decision Framework
444
+
445
+ Before writing code, classify each subtask:
446
+
447
+ 1. **JS-only** \u2014 deterministic logic (filter, sort, count, regex, date math) \u2192 do it inline
448
+ 2. **Single-shot semantic** \u2014 needs LLM reasoning but no tools or multi-step exploration \u2192 single \`llmQuery\` with narrow context
449
+ 3. **Full delegation** \u2014 needs its own discovery, tool calls, or >2 turns of exploratory work \u2192 \`llmQuery\` as child agent
450
+ 4. **Parallel fan-out** \u2014 2+ independent subtasks each qualifying for delegation \u2192 batched \`llmQuery([...])\`
451
+
452
+ Heuristics:
453
+ - If you can write it as \`.filter().map()\`, don't delegate
454
+ - If the subtask needs classification/summarization on already-narrowed text, a single \`llmQuery\` suffices
455
+ - If the subtask would need its own exploration loop (probe shapes, call tools, iterate), delegate it
456
+ - If unsure, delegate \u2014 a child that finishes in 1 turn costs little, but a bloated parent action log costs a lot
457
+ - Estimate total calls before fanning out. If 5 children each might spawn 2 more, that's ~15 calls.
458
+ {{ else }}
459
+ ### When to Delegate
460
+
461
+ - \`.filter().map()\` \u2192 JS inline
462
+ - Classify/summarize narrowed text \u2192 single \`llmQuery\`
463
+ - Needs tools, discovery, or multi-step exploration \u2192 delegate as child agent
464
+ - 2+ independent delegations \u2192 batched \`llmQuery([...])\`
465
+ {{ /if }}
466
+
467
+ **In this run, \`llmQuery\` is an advanced delegation primitive:**
468
+
469
+ - Each \`llmQuery(...)\` call runs a focused child agent with its own runtime session and its own action log. Use this when a branch of work would otherwise bloat the parent action log.
470
+ - Use advanced \`llmQuery(...)\` for subtasks that may need discovery, tool usage, or several child turns. Keep tiny deterministic work in local JS instead.
471
+ - Parent runtime variables are NOT visible to the child unless you pass them explicitly in the \`context\` argument.
472
+ - Prefer passing a compact object as \`context\` so the child receives named runtime globals. Safe object keys become child globals, and the full payload is always available as \`context\`.
473
+ - Use serial child calls when later work depends on earlier results. Use batched \`llmQuery([{ query, context }, ...])\` only for independent subtasks.
474
+ - A child can call tools, discovery functions, \`final(...)\`, or \`ask_clarification(...)\`. If a child asks for clarification, that clarification bubbles up and ends the whole run.
475
+ - Recursion is not infinite. When recursion depth runs out, deeper child \`llmQuery(...)\` calls fall back to the simple semantic form, so keep each delegated task scoped and self-contained.
476
+
477
+ **Advanced delegation examples:**
478
+
479
+ \`\`\`js
480
+ // SERIAL: child uses its own focused action log and tools/discovery
481
+ const narrowed = inputs.emails
482
+ .filter(e => e.subject.toLowerCase().includes('refund'))
483
+ .map(e => ({ from: e.from, subject: e.subject, body: e.body.slice(0, 800) }));
484
+
485
+ const refundPlan = await llmQuery(
486
+ 'Use available tools if needed and determine which messages require a refund response. Return a compact plan.',
487
+ {
488
+ emails: narrowed,
489
+ policyNote: 'Prioritize messages that mention duplicate billing or unauthorized charges.',
490
+ }
491
+ );
492
+ console.log(refundPlan);
493
+ \`\`\`
494
+
495
+ \`\`\`js
496
+ // PARALLEL: only for independent delegated subtasks
497
+ const [refunds, cancellations] = await llmQuery([
498
+ {
499
+ query: 'Review these emails and identify refund cases. Return a compact summary.',
500
+ context: { emails: refundCandidates, rubric: 'refund-or-not' }
501
+ },
502
+ {
503
+ query: 'Review these emails and identify cancellation cases. Return a compact summary.',
504
+ context: { emails: cancellationCandidates, rubric: 'cancel-or-not' }
505
+ }
506
+ ]);
507
+ console.log({ refunds, cancellations });
508
+ \`\`\`
509
+
510
+ {{ if promptLevel === 'detailed' }}
511
+ ### Divide-and-Conquer Playbook
512
+
513
+ **Fan-Out / Fan-In** \u2014 parallel independent analysis, then merge:
514
+ \`\`\`js
515
+ const urgent = inputs.emails.filter(e => /* JS criteria */);
516
+ const routine = inputs.emails.filter(e => /* JS criteria */);
517
+ const [urgentResult, routineResult] = await llmQuery([
518
+ { query: 'Draft responses for urgent emails.', context: { emails: urgent.slice(0, 10) } },
519
+ { query: 'Categorize routine emails.', context: { emails: routine.slice(0, 20) } }
520
+ ]);
521
+ \`\`\`
522
+
523
+ **Pipeline** \u2014 each step depends on the prior:
524
+ \`\`\`js
525
+ const sources = await llmQuery('Find relevant sources on this topic.', { topic, tools: 'use search' });
526
+ const analysis = await llmQuery('Analyze these findings.', { sources, criteria });
527
+ \`\`\`
528
+
529
+ **Scout-then-Execute** \u2014 explore before acting:
530
+ \`\`\`js
531
+ const slots = await llmQuery('Check calendar for free slots this week.', { events: inputs.calendar });
532
+ const proposal = await llmQuery('Propose best meeting times.', { freeSlots: slots, attendees });
533
+ \`\`\`
534
+
535
+ Key rules:
536
+ - Always narrow with JS before delegating \u2014 never pass raw \`inputs.*\`
537
+ - Name context keys semantically: \`{ emails: filtered, rubric: 'classify-urgency' }\` not \`{ data: x }\`
538
+ - Include a rubric/criteria string when delegating judgment tasks
539
+ - In batched calls, individual children may return \`[ERROR]\` strings \u2014 check each result before using it
540
+ {{ /if }}
541
+
542
+ ### Resource Awareness
543
+ - Sub-query calls are budget-limited across all recursion levels. The runtime warns when nearing the limit.
544
+ - Recursion depth is finite. At the deepest level, \`llmQuery\` becomes single-shot with no tools. Keep each delegated task scoped to succeed even in that mode.
545
+ {{ else }}
546
+ {{ if llmQueryPromptMode === 'simple-at-terminal-depth' }}
547
+ **In this run, \`llmQuery\` is in terminal simple mode.** You are at the deepest recursion level. \`llmQuery(...)\` here is a direct single-shot LLM call \u2014 it cannot use tools, discovery, or multi-turn code execution. Keep queries self-contained and answerable purely from the context you pass. Do NOT delegate tasks requiring tool usage at this depth.
548
+ {{ /if }}
549
+ {{ /if }}
550
+
430
551
  ---
431
552
 
432
553
  ### Available Functions
433
554
 
434
555
  **Core functions (always available):**
435
556
 
436
- - \`await llmQuery(query: string, context: any): string\` \u2014 Ask a sub-agent one semantic question. Pass the narrowed context slice as the second argument.
437
- - \`await llmQuery([{ query: string, context: any }, ...]): string[]\` \u2014 Batched parallel form for multiple independent questions.
557
+ {{ if llmQueryPromptMode === 'advanced-recursive' }}
558
+ - \`await llmQuery(query: string, context: any): string\` \u2014 Delegate one focused subtask to a child agent with its own runtime and action log. Pass only the explicit context the child needs.
559
+ - \`await llmQuery([{ query: string, context: any }, ...]): string[]\` \u2014 Batched delegated form for multiple independent child subtasks.
560
+ {{ else }}
561
+ - \`await llmQuery(query: string, context: any): string\` \u2014 Ask one focused semantic question. Pass the narrowed context slice as the second argument.
562
+ - \`await llmQuery([{ query: string, context: any }, ...]): string[]\` \u2014 Batched parallel form for multiple independent semantic questions.
563
+ {{ /if }}
438
564
  - \`final(...args)\` \u2014 Signal completion and pass the gathered payload to the responder. Call this ONLY when you have everything the responder needs.
439
565
  - \`ask_clarification(questionOrSpec)\` \u2014 Stop and ask the user for clarification. Pass a non-empty string for free-text, or an object with \`question\` and optional \`type\` (\`'date'\`, \`'number'\`, \`'single_choice'\`, \`'multiple_choice'\`) and \`choices\`.
440
566
  {{ if hasInspectRuntime }}
@@ -509,6 +635,7 @@ When your code throws an error, the error is a signal to explore one level up \u
509
635
 
510
636
  - Reuse existing runtime state. Do not re-declare or recompute values that are already available from prior turns unless you intentionally need to overwrite them or the runtime was restarted.
511
637
  - Think in continuation steps: inspect what already exists, extend it with the next small piece of code, and keep building on prior executed work.
638
+ - If a \`Delegated Context\` block appears, the data has been injected into your JS runtime as named globals. The summary shows types and structure but NOT full values \u2014 explore with code. Access globals directly (e.g. if the summary shows \`emails: array(42)\`, use \`emails\` not \`inputs.emails\`). Read the element-keys hints to skip unnecessary probing, then narrow with JS before using \`llmQuery\` or \`final()\`.
512
639
  {{ if hasInspectRuntime }}
513
640
  - If the conversation is long and you are unsure what state exists, call \`inspect_runtime()\` rather than re-reading old outputs.
514
641
  {{ /if }}
@@ -538,7 +665,7 @@ If a prompt includes \`Runtime Restore\`, the runtime state shown below has alre
538
665
  {{ if promptLevel === 'detailed' }}
539
666
  ### Common Anti-Patterns \u2014 Do NOT Do These
540
667
 
541
- \`\`\`js
668
+ \`\`\`javascript
542
669
  // WRONG: dumping the entire inputs object
543
670
  console.log(inputs);
544
671
  console.log(JSON.stringify(inputs));
@@ -572,7 +699,8 @@ final(result);
572
699
  ---
573
700
 
574
701
  ## JavaScript Runtime Usage Instructions
575
- {{ runtimeUsageInstructions }}`,"rlm/responder.md":`## Answer Synthesis Agent
702
+ {{ runtimeUsageInstructions }}
703
+ `,"rlm/responder.md":`## Answer Synthesis Agent
576
704
 
577
705
  You synthesize a final answer from the provided actorResult payload. In normal \`forward()\` and \`streamingForward()\` flows, you only run after the actor calls \`final(...args)\`. Clarification requests are surfaced directly to the caller before the responder runs. Some internal or evaluation workflows may still pass through an \`ask_clarification(...args)\` payload.
578
706
 
@@ -583,12 +711,12 @@ You synthesize a final answer from the provided actorResult payload. In normal \
583
711
  1. Base your answer ONLY on evidence from actorResult payload arguments.
584
712
  2. If actorResult lacks sufficient information, provide the best possible answer from available evidence.
585
713
  3. If an internal or evaluation workflow provides \`actorResult.type = ask_clarification\`, ask for the missing information clearly in your output fields.
586
- `};var Di=/{{\s*([^}]+?)\s*}}/g,Tu=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,Ru=/^([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*===\s*(?:'([^']*)'|"([^"]*)")$/,wu=new Map;function St(s,e,t,n){let o=e.slice(0,t).split(`
587
- `),i=o.length,a=(o.at(-1)?.length??0)+1;return`${s}:${i}:${a} ${n}`}function ed(s){let e=[],t=0;Di.lastIndex=0;let n=Di.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=Di.exec(s)}return t<s.length&&e.push({type:"text",value:s.slice(t)}),e}function Ui(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 l=a.value;if(r.has(l))return{nodes:o,nextIndex:i,terminator:l};if(l.startsWith("if ")){let u=l.slice(3).trim();if(!Tu.test(u)&&!Ru.test(u))throw new Error(St(t,e,a.index,`Invalid if condition '${u}'`));let c=Ui(s,e,t,i+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=Ui(s,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}),i=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(!Tu.test(l))throw new Error(St(t,e,a.index,`Invalid tag '${l}'`));o.push({type:"var",name:l,index:a.index}),i++}return{nodes:o,nextIndex:i}}function Gi(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(St(n,t,r,`Missing template variable '${e}'`));i=i[a]}return i}function ji(s,e,t,n){let r="";for(let o of s){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let l=Gi(e,o.name,t,n,o.index);if(typeof l!="string"&&typeof l!="number"&&typeof l!="boolean")throw new Error(St(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(l);continue}let i,a=Ru.exec(o.condition);if(a){let[,l,u,c]=a,p=u??c??"";i=Gi(e,l,t,n,o.index)===p}else{let l=Gi(e,o.condition,t,n,o.index);if(typeof l!="boolean")throw new Error(St(n,t,o.index,`Condition '${o.condition}' must be boolean`));i=l}i?r+=ji(o.thenNodes,e,t,n):r+=ji(o.elseNodes,e,t,n)}return r}function td(s,e){let t=ed(s),n=Ui(t,s,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function ct(s,e={}){let t=Cu[s],n=`template:${s}`;if(!t)throw new Error(`Unknown template id: ${String(s)}`);let r=wu.get(s);return r||(r=td(t,n),wu.set(s,r)),ji(r,e,t,n)}var Su=ct("dsp/function-call-instructions.md"),nd=ct("dsp/strict-output-formatting-rules.md"),rd=ct("dsp/example-disclaimer.md"),Fn=ct("dsp/example-separator.md"),Jt=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,ie.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=Jo(this.sig.getInputFields()),r=Jo(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}\`: ${_n(c.description)}`).join(`
714
+ `};var zi=/{{\s*([^}]+?)\s*}}/g,Mu=/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/,Fu=/^([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*===\s*(?:'([^']*)'|"([^"]*)")$/,Pu=new Map;function Et(s,e,t,n){let o=e.slice(0,t).split(`
715
+ `),i=o.length,a=(o.at(-1)?.length??0)+1;return`${s}:${i}:${a} ${n}`}function od(s){let e=[],t=0;zi.lastIndex=0;let n=zi.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=zi.exec(s)}return t<s.length&&e.push({type:"text",value:s.slice(t)}),e}function Hi(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 l=a.value;if(r.has(l))return{nodes:o,nextIndex:i,terminator:l};if(l.startsWith("if ")){let u=l.slice(3).trim();if(!Mu.test(u)&&!Fu.test(u))throw new Error(Et(t,e,a.index,`Invalid if condition '${u}'`));let c=Hi(s,e,t,i+1,new Set(["else","/if"]));if(!c.terminator)throw new Error(Et(t,e,a.index,"Unclosed 'if' block"));let p=[],d=c.nextIndex+1;if(c.terminator==="else"){let m=Hi(s,e,t,c.nextIndex+1,new Set(["/if"]));if(m.terminator!=="/if")throw new Error(Et(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}),i=d;continue}if(l==="else")throw new Error(Et(t,e,a.index,"Unexpected 'else'"));if(l==="/if")throw new Error(Et(t,e,a.index,"Unexpected '/if'"));if(l.startsWith("include "))throw new Error(Et(t,e,a.index,"Unexpected 'include' directive at runtime (includes must be compiled)"));if(!Mu.test(l))throw new Error(Et(t,e,a.index,`Invalid tag '${l}'`));o.push({type:"var",name:l,index:a.index}),i++}return{nodes:o,nextIndex:i}}function qi(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(Et(n,t,r,`Missing template variable '${e}'`));i=i[a]}return i}function Ki(s,e,t,n){let r="";for(let o of s){if(o.type==="text"){r+=o.value;continue}if(o.type==="var"){let l=qi(e,o.name,t,n,o.index);if(typeof l!="string"&&typeof l!="number"&&typeof l!="boolean")throw new Error(Et(n,t,o.index,`Variable '${o.name}' must be string, number, or boolean`));r+=String(l);continue}let i,a=Fu.exec(o.condition);if(a){let[,l,u,c]=a,p=u??c??"";i=qi(e,l,t,n,o.index)===p}else{let l=qi(e,o.condition,t,n,o.index);if(typeof l!="boolean")throw new Error(Et(n,t,o.index,`Condition '${o.condition}' must be boolean`));i=l}i?r+=Ki(o.thenNodes,e,t,n):r+=Ki(o.elseNodes,e,t,n)}return r}function sd(s,e){let t=od(s),n=Hi(t,s,e);if(n.terminator)throw new Error(`Unexpected template terminator '${n.terminator}' in ${e}`);return n.nodes}function xt(s,e={}){let t=Eu[s],n=`template:${s}`;if(!t)throw new Error(`Unknown template id: ${String(s)}`);let r=Pu.get(s);return r||(r=sd(t,n),Pu.set(s,r)),Ki(r,e,t,n)}var _u=xt("dsp/function-call-instructions.md"),id=xt("dsp/strict-output-formatting-rules.md"),ad=xt("dsp/example-disclaimer.md"),Ln=xt("dsp/example-separator.md"),Xt=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,le.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=es(this.sig.getInputFields()),r=es(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}\`: ${$n(c.description)}`).join(`
588
716
  `);i&&i.length>0&&e.push(`## Available Functions
589
- ${i}`);let a=this.getFieldNameToTitleMap(),l=ku(this.sig.getInputFields(),a);if(e.push(`## Input Fields
590
- ${l}`),!t){let c=vu(this.sig.getOutputFields(),a);e.push(`## Output Fields
591
- ${c}`)}i&&i.length>0&&e.push(Su.trim()),t&&this.structuredOutputFunctionName?e.push(ct("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push(nd.trim());let u=this.sig.getDescription();if(u){let c=_n(u);c=Qo(c,a),e.push(c)}return{type:"text",text:e.join(`
717
+ ${i}`);let a=this.getFieldNameToTitleMap(),l=Nu(this.sig.getInputFields(),a);if(e.push(`## Input Fields
718
+ ${l}`),!t){let c=Lu(this.sig.getOutputFields(),a);e.push(`## Output Fields
719
+ ${c}`)}i&&i.length>0&&e.push(_u.trim()),t&&this.structuredOutputFunctionName?e.push(xt("dsp/legacy-formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim()):t||e.push(id.trim());let u=this.sig.getDescription();if(u){let c=$n(u);c=ts(c,a),e.push(c)}return{type:"text",text:e.join(`
592
720
 
593
721
  `)}}buildStructuredPrompt(){let e=[],t=this.sig.hasComplexFields();e.push("<identity>"),e.push(this.buildIdentitySection()),e.push("</identity>");let n=this.buildTaskDefinitionSection();n&&(e.push(`
594
722
  <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(`
@@ -596,32 +724,32 @@ ${c}`)}i&&i.length>0&&e.push(Su.trim()),t&&this.structuredOutputFunctionName?e.p
596
724
  <input_fields>`),e.push(this.buildInputFieldsSection()),e.push("</input_fields>"),t||(e.push(`
597
725
  <output_fields>`),e.push(this.buildOutputFieldsSection()),e.push("</output_fields>")),e.push(`
598
726
  <formatting_rules>`),e.push(this.buildFormattingRulesSection()),e.push("</formatting_rules>"),{type:"text",text:e.join(`
599
- `)}}buildIdentitySection(){let e=Jo(this.sig.getInputFields()),t=Jo(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=Qo(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
600
- `);let n=e.map(r=>`- \`${r.name}\`: ${_n(r.description??"")}`).join(`
727
+ `)}}buildIdentitySection(){let e=es(this.sig.getInputFields()),t=es(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=ts(n,t),n}buildFunctionsSection(e){let t=[];t.push(`**Available Functions**: You can call the following functions to complete the task:
728
+ `);let n=e.map(r=>`- \`${r.name}\`: ${$n(r.description??"")}`).join(`
601
729
  `);return t.push(n),t.push(`
602
- ${Su.trim()}`),t.join(`
730
+ ${_u.trim()}`),t.join(`
603
731
  `)}buildInputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Input Fields**: The following fields will be provided to you:
604
732
 
605
- ${ku(this.sig.getInputFields(),e)}`}buildOutputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Output Fields**: You must generate the following fields:
733
+ ${Nu(this.sig.getInputFields(),e)}`}buildOutputFieldsSection(){let e=this.getFieldNameToTitleMap();return`**Output Fields**: You must generate the following fields:
606
734
 
607
- ${vu(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?ct("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?ct("dsp/formatting-rules-structured-json.md").trim():ct("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),i;if(r)i=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})}i=[...l,...o]}let a=i.filter(l=>l!==void 0);return a.every(l=>l.type==="text")?a.map(l=>l.text).join(`
608
- `):a.reduce(Vt(`
735
+ ${Lu(this.sig.getOutputFields(),e)}`}buildFormattingRulesSection(){let e=this.sig.hasComplexFields();return e&&this.structuredOutputFunctionName?xt("dsp/formatting-rules-structured-function.md",{structuredOutputFunctionName:this.structuredOutputFunctionName}).trim():e?xt("dsp/formatting-rules-structured-json.md").trim():xt("dsp/formatting-rules-plain-text.md").trim()}renderSingleValueUserContent=(e,t,n,r)=>{let o=this.renderInputFields(e),i;if(r)i=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})}i=[...l,...o]}let a=i.filter(l=>l!==void 0);return a.every(l=>l.type==="text")?a.map(l=>l.text).join(`
736
+ `):a.reduce(Yt(`
609
737
  `),[])};render=(e,{examples:t,demos:n})=>{if(!this.examplesInSystem)return this.renderWithMessagePairs(e,{examples:t,demos:n});let r=t?[{type:"text",text:`
610
738
 
611
739
  ## Examples
612
- `},...this.renderExamples(t)]:[],o=n?this.renderDemos(n):[],i=r.every(d=>d.type==="text"),a=o.every(d=>d.type==="text"),l=i&&a,u=this.task.text;if(l){let d=[{type:"text",text:u},...r,...o];d.reduce(Vt(""),[]),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 f of m){let x;if(g?(x=this.renderSingleValueUserContent(f.values,r,o,l),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,l);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}
613
- ${rd}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],l=n?this.renderDemosAsMessages(n):[],u=[];for(let C of[...a,...l])u.push(C.userMessage),u.push(C.assistantMessage),C.functionResultMessage&&u.push(C.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&u.length>0&&p){let C=u.length-1,R=u[C];R?.role==="assistant"&&(u[C]={...R,cache:!0})}if(Array.isArray(e)){let C=[],R=e,P=!0;for(let v of R){let E=this.renderInputFields(v.values),k=E.every(O=>O.type==="text")?E.map(O=>O.text).join(`
614
- `):E.reduce(Vt(`
615
- `),[]);if(v.role==="user"){P&&r&&(typeof k=="string"?k=Fn+k:k=[{type:"text",text:Fn},...k],P=!1),C.push({role:"user",content:k});continue}if(v.role!=="assistant")throw new Error("Invalid message role");if(typeof k!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");C.push({role:"assistant",content:k})}return[i,...u,...C]}let d=this.sig.getInputFields(),m=d.filter(C=>C.isCached),g=d.filter(C=>!C.isCached),f=m.length>0;if(this.contextCache&&f&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let C=m.map(E=>this.renderInField(E,e,void 0)).filter(E=>E!==void 0).flat();C.filter(E=>E.type==="text").forEach(E=>{E.text=`${E.text}
616
- `});let R=C.every(E=>E.type==="text")?C.map(E=>E.text).join(`
617
- `):C.reduce(Vt(`
618
- `),[]);r&&(typeof R=="string"?R=Fn+R:R=[{type:"text",text:Fn},...R]);let P=g.map(E=>this.renderInField(E,e,void 0)).filter(E=>E!==void 0).flat();P.filter(E=>E.type==="text").forEach(E=>{E.text=`${E.text}
619
- `});let v=P.every(E=>E.type==="text")?P.map(E=>E.text).join(`
620
- `):P.reduce(Vt(`
621
- `),[]);return[i,...u,{role:"user",content:R,cache:!0},{role:"user",content:v}]}let h=this.sortFieldsCachedFirst(d).map(C=>this.renderInField(C,e,void 0)).filter(C=>C!==void 0).flat();h.filter(C=>C.type==="text").forEach(C=>{C.text=`${C.text}
622
- `});let y=h.every(C=>C.type==="text")?h.map(C=>C.text).join(`
623
- `):h.reduce(Vt(`
624
- `),[]);r&&(typeof y=="string"?y=Fn+y:y=[{type:"text",text:Fn},...y]);let I=f&&g.length===0&&this.contextCache;return[i,...u,{role:"user",content:y,...I?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((i,a)=>{let l=a.title;return i[l]||(i[l]=[]),i[l].push(a),i},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([i,a])=>{if(a.length===1){let l=a[0];return l.type?.name==="object"||l.type?.isArray&&l.type.fields?{title:i,name:l.name,description:`${l.description}
740
+ `},...this.renderExamples(t)]:[],o=n?this.renderDemos(n):[],i=r.every(d=>d.type==="text"),a=o.every(d=>d.type==="text"),l=i&&a,u=this.task.text;if(l){let d=[{type:"text",text:u},...r,...o];d.reduce(Yt(""),[]),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 f of m){let x;if(g?(x=this.renderSingleValueUserContent(f.values,r,o,l),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,l);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}
741
+ ${ad}`:this.task.text,cache:!!this.contextCache},a=t?this.renderExamplesAsMessages(t):[],l=n?this.renderDemosAsMessages(n):[],u=[];for(let C of[...a,...l])u.push(C.userMessage),u.push(C.assistantMessage),C.functionResultMessage&&u.push(C.functionResultMessage);let c=this.contextCache?.cacheBreakpoint??"after-examples",p=this.ignoreBreakpoints||c==="after-examples";if(this.contextCache&&u.length>0&&p){let C=u.length-1,R=u[C];R?.role==="assistant"&&(u[C]={...R,cache:!0})}if(Array.isArray(e)){let C=[],R=e,M=!0;for(let S of R){let O=this.renderInputFields(S.values),k=O.every(_=>_.type==="text")?O.map(_=>_.text).join(`
742
+ `):O.reduce(Yt(`
743
+ `),[]);if(S.role==="user"){M&&r&&(typeof k=="string"?k=Ln+k:k=[{type:"text",text:Ln},...k],M=!1),C.push({role:"user",content:k});continue}if(S.role!=="assistant")throw new Error("Invalid message role");if(typeof k!="string")throw new Error("Assistant message cannot contain non-text content like images, files, etc");C.push({role:"assistant",content:k})}return[i,...u,...C]}let d=this.sig.getInputFields(),m=d.filter(C=>C.isCached),g=d.filter(C=>!C.isCached),f=m.length>0;if(this.contextCache&&f&&(this.ignoreBreakpoints||c!=="system"&&c!=="after-functions")&&g.length>0){let C=m.map(O=>this.renderInField(O,e,void 0)).filter(O=>O!==void 0).flat();C.filter(O=>O.type==="text").forEach(O=>{O.text=`${O.text}
744
+ `});let R=C.every(O=>O.type==="text")?C.map(O=>O.text).join(`
745
+ `):C.reduce(Yt(`
746
+ `),[]);r&&(typeof R=="string"?R=Ln+R:R=[{type:"text",text:Ln},...R]);let M=g.map(O=>this.renderInField(O,e,void 0)).filter(O=>O!==void 0).flat();M.filter(O=>O.type==="text").forEach(O=>{O.text=`${O.text}
747
+ `});let S=M.every(O=>O.type==="text")?M.map(O=>O.text).join(`
748
+ `):M.reduce(Yt(`
749
+ `),[]);return[i,...u,{role:"user",content:R,cache:!0},{role:"user",content:S}]}let h=this.sortFieldsCachedFirst(d).map(C=>this.renderInField(C,e,void 0)).filter(C=>C!==void 0).flat();h.filter(C=>C.type==="text").forEach(C=>{C.text=`${C.text}
750
+ `});let b=h.every(C=>C.type==="text")?h.map(C=>C.text).join(`
751
+ `):h.reduce(Yt(`
752
+ `),[]);r&&(typeof b=="string"?b=Ln+b:b=[{type:"text",text:Ln},...b]);let I=f&&g.length===0&&this.contextCache;return[i,...u,{role:"user",content:b,...I?{cache:!0}:{}}]};renderExtraFields=e=>{let t=[];if(!e||e.length===0)return t;let n=e.reduce((i,a)=>{let l=a.title;return i[l]||(i[l]=[]),i[l].push(a),i},{}),r=this.sig.hasComplexFields();return Object.entries(n).map(([i,a])=>{if(a.length===1){let l=a[0];return l.type?.name==="object"||l.type?.isArray&&l.type.fields?{title:i,name:l.name,description:`${l.description}
625
753
  IMPORTANT: Provide the FULL JSON object for this field, matching the schema exactly.`}:{title:i,name:l.name,description:l.description}}if(a.length>1){let l=a.map(u=>`- ${u.description}`).join(`
626
754
  `);return{title:i,name:a[0].name,description:l}}}).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
627
755
  ${m}
@@ -638,35 +766,35 @@ ${m}
638
766
  `}].slice(0,-1).forEach(f=>{"text"in f&&(f.text=`${f.text}
639
767
  `),t.push(f)});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}
640
768
  `),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(`
641
- `):a.reduce(Vt(`
769
+ `):a.reduce(Yt(`
642
770
  `),[]);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 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(`
643
771
  `);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}
644
- `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(id(e,r,n))return;e.type&&kr(e,r);let o=od(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,l="fileUri"in i;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 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(`
772
+ `}),n};renderInField=(e,t,n)=>{let r=t[e.name];if(cd(e,r,n))return;e.type&&Er(e,r);let o=ld(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,l="fileUri"in i;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 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(`
645
773
  `),n.push(t.map(r=>`- ${r}`).join(`
646
- `))):n.push(t),[{type:"text",text:n.join("")}]}},Jo=s=>s.map(e=>`\`${e.title}\``).join(", "),ku=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Nn(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 l=_n(n.description);e&&(l=Qo(l,e)),a=` ${l}`}return`${r}: (${i})${a}`.trim()}).join(`
647
- `),vu=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Nn(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 l=n.type?.name==="class"?n.description:_n(n.description);e&&(l=Qo(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}: (${i})${a}`.trim()}).join(`
648
- `),od=(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?ou(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 sd(s){return`{ ${Object.entries(s).map(([t,n])=>{let r=n.isOptional?"?":"",o=Nn({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var Nn=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 ${sd(s.fields)}`:"object";default:return"string"}})();return s?.isArray?`json array of ${e} items`:e};function Vt(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 id=(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 _n(s){let e=s.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function Qo(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 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 ad(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 ld(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 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 Yo(s,e,t,n){if(!e?.resultPicker||s.length<=1)return 0;let r=e.resultPicker;if((t?ad(t,n):!1)&&t){let l=ld(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 i=s.map((l,u)=>({index:u,sample:l.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 Ou(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 Yo(r,t,s,e)}var Ln=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 Xo=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}=ud(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,l]of this.tools.entries())i.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=i.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 He(`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[i,a]of Object.entries(r)){let l=[...o,i];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 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 ud(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,l)=>{for(let[u,c]of Object.entries(i)){let p=a?`${a}.${u}`:u,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=cd(c);e.push({name:dd(d),title:pd(s.name,p),type:m,description:c.description||`${u} parameter for ${s.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function cd(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 pd(s,e){return`${s} ${e.replace(/\./g," ")}`}function dd(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Pu(s,e,t){let n=new Fe(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(u=>u.name===a.name)||n.addOutputField(a);else{let a=Bi(o.name),l=Mu(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:Eu(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 s)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:i,paramFieldMap:a}=md(o);r.set(o.name,a);for(let l of i)n.getOutputFields().some(c=>c.name===l.name)||n.addOutputField(l)}else{let i=Bi(o.name),a=Mu(o.parameters);n.getOutputFields().some(u=>u.name===i)||n.addOutputField({name:i,title:Eu(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function md(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,l)=>{for(let[u,c]of Object.entries(i)){let p=a?`${a}.${u}`:u,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=gd(c),g={name:Bi(d),title:fd(s.name,p),type:m,description:c.description||`${u} parameter for ${s.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function gd(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 fd(s,e){return`${s} ${e.replace(/\./g," ")}`}function Bi(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Eu(s){return s.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function Mu(s){return!s||!s.properties||Object.keys(s.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var _r=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new Xo(e)}processSignature(e){let{signature:t}=Pu(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 $n="__finalResult",Se=class s extends Dt{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??Jt)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Wt(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 yi()}getMergedCustomLabels(e,t){return Bt(ie.customLabels,e?.getOptions?.()?.customLabels,t?.customLabels)}updateMeter(e){yi(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:l,model:u,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await Ou(t,l,{resultPicker:n?.resultPicker}),f=t?.history(g,l)??[];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}:{}},b=this.isDebug(e,n),h=a===0,y=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let I,C=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).`);I={type:"json_schema",schema:{name:"output",strict:!0,schema:$t(C)}}}let P=n?.contextCache?.cacheBreakpoint??"after-examples",v=e.getFeatures?.(u)?.caching?.cacheBreakpoints===!1,E=n?.contextCache&&(v||P==="after-functions"||P==="after-examples"),k=o?.length&&E?o.map((L,F)=>({...L,cache:F===o.length-1})):o;return await e.chat({chatPrompt:f,functions:k,functionCall:i,modelConfig:x,model:u,responseFormat:I},{sessionId:l,rateLimiter:c,stream:p,debug:b,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!h,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??ie.abortSignal,stepIndex:a,logger:y,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:l,stepContext:u}){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,b=this.usage,h=r===0,y=this.isDebug(e,n),I=this.getLogger(e,n),{functions:C,functionCall:R}=du(p,m,h,n);this.structuredOutputFunctionFallback&&C.filter(E=>E.name!==$n).length===0&&(R={type:"function",function:{name:$n}});let P=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:i,functions:C,functionCall:R,stepIndex:r});P instanceof ReadableStream?yield*gu({ai:e,model:x,res:P,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:C,strictMode:f,span:o,states:a,usage:b,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:I,debug:y,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal}):yield*fu({ai:e,model:x,res:P,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:C,span:o,strictMode:f,states:a,usage:b,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:I,debug:y,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,i){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(me=>me.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 kn,d=r.functions?Wt(r.functions):[...this.functions],m=new Ln(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let Ae=e.getModelList()?.filter(U=>"model"in U);if(!Ae||Ae.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 me=Fi(e,g,r.model?String(r.model):void 0);d.push(me)}let f={...r},x=r.stepHooks,b=d&&d.length>0,h=r.functionCallMode??this.options?.functionCallMode??"auto";b&&h==="prompt"&&(this.signatureToolCallingManager=new _r(d)),b&&h==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new _r(d));let y,I,C=this.options?.promptTemplate??Jt;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let R=this.signature.hasComplexFields(),P=e.getFeatures?.(r.model),v=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=R&&(v==="function"||v==="auto"&&!P?.structuredOutputs),this.structuredOutputFunctionFallback){let me={name:$n,description:"Return the final result. Call this function with the complete output data.",parameters:$t(this.signature.getOutputFields()),func:async()=>"done"};d.push(me),l=[...l??[],$n.toLowerCase()]}let E=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,k={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:E,structuredOutputFunctionName:this.structuredOutputFunctionFallback?$n:void 0};this.promptTemplate=new C(this.signature,k);let O,L=performance.now();Array.isArray(t)?(ol(t),O=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):O=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let F=performance.now()-L,M=this.getMetricsInstruments(),_=this.getMergedCustomLabels(e,r);M&&Do(M,"prompt_render",F,this.getSignatureName(),_);let j=performance.now();p.addRequest(O,r.sessionId);let H=performance.now()-j;M&&Do(M,"memory_update",H,this.getSignatureName(),_);let D=new Map;n.forEach(me=>{D.set(me.index,{})});let Y=()=>{let me=m._consumePendingOptions();if(me){let{modelConfig:U,...K}=me;f={...f,...K},U&&(f.modelConfig={...f.modelConfig,...U})}let ge=m._consumeFunctionsToAdd();if(ge){let U=Wt(ge);for(let K of U)d.some(W=>W.name===K.name)||d.push(K)}let Ae=m._consumeFunctionsToRemove();if(Ae){let U=new Set(Ae.map(K=>K.toLowerCase()));for(let K=d.length-1;K>=0;K--)U.has(d[K].name.toLowerCase())&&d.splice(K,1)}},G=r?.abortSignal??ie.abortSignal;e:for(let me=0;me<c;me++){if(m._beginStep(me),Y(),g&&g.model!==!1){let Ae=d.findIndex(U=>U.name==="adjustGeneration");if(Ae!==-1){let U=f.model?String(f.model):void 0;d[Ae]=Fi(e,g,U)}}if(m._isStopRequested)break;if(G?.aborted)throw new $e("between-steps",G.reason??"Aborted between steps");if(x?.beforeStep&&(await x.beforeStep(m),Y(),m._isStopRequested))break;let ge=u;for(let Ae=0;Ae<=ge;Ae++)try{let U=u+1;for(let W=0;W<U;W++){n.forEach(Q=>{Q.content="",Q.values={},Q.functionCalls=[],Q.functionsExecuted=new Set,Q.xstate={extractedFields:[],streamedIndex:{},s:-1}}),W>0&&D.forEach((Q,le)=>{D.set(le,{})});let re=new Map;n.forEach(Q=>{re.set(Q.index,{})});try{let Q=this.forwardCore({options:{...f,functions:d},ai:e,mem:p,stepIndex:me,span:o,traceContext:i,states:n,stopFunctionNames:l,stepContext:m}),le=!1;try{for await(let oe of Q)if(oe!==void 0){let Re=oe.index,q=oe.delta,ce=re.get(Re)??{},fe=D.get(Re)??{},he={},we=!1;for(let ke of Object.keys(q)){let It=q[ke],Ye=ce[ke],rt;typeof It=="string"&&(typeof Ye=="string"||Ye===void 0)?rt=(Ye??"")+It:Array.isArray(It)&&(Array.isArray(Ye)||Ye===void 0)?rt=[...Ye??[],...It]:rt=It,ce[ke]=rt;let ve=rt,Ke=fe[ke];if(typeof ve=="string"&&typeof Ke=="string")if(ve.startsWith(Ke)){let N=ve.slice(Ke.length);N&&(he[ke]=N,we=!0,fe[ke]=ve)}else Ke.startsWith(ve)||ve!==Ke&&(he[ke]=ve,we=!0,fe[ke]=ve);else if(Array.isArray(ve)&&Array.isArray(Ke)){if(ve.length>Ke.length){let N=ve.slice(Ke.length);he[ke]=N,we=!0,fe[ke]=ve}}else JSON.stringify(ve)!==JSON.stringify(Ke)&&(he[ke]=ve,we=!0,fe[ke]=ve)}we&&(yield{version:W,index:oe.index,delta:he})}}catch(oe){if(oe instanceof Kt){if(le=!0,this.structuredOutputFunctionFallback){let Re=oe.calls.find(q=>q.func.name===$n);if(Re?.args){let q=Re.args;Mn(this.signature,q);let ce=this.signature.getOutputFields();for(let fe of n){let he={};for(let we of ce)we.name in q&&!we.isInternal&&(he[we.name]=q[we.name],fe.values[we.name]=q[we.name]);yield{version:W,index:fe.index,delta:he}}for(let fe of n)await vn(this.asserts,fe.values);if(this.fieldProcessors.length>0)for(let fe of n)await Or(this.fieldProcessors,fe.values,p,r.sessionId)}}}else throw oe}if(this.usage.length>0){let oe=this.usage[this.usage.length-1];oe?.tokens&&m._addUsage(oe.tokens.promptTokens??0,oe.tokens.completionTokens??0,oe.tokens.totalTokens??0)}n.some(oe=>oe.functionsExecuted.size>0)&&x?.afterFunctionExecution&&(await x.afterFunctionExecution(m),Y());let Ee=le||m._isStopRequested?!1:hu(p,l,n,f?.sessionId);if(x?.afterStep&&(await x.afterStep(m),Y()),Ee&&!m._isStopRequested&&!G?.aborted){let oe=this.getMetricsInstruments();oe&&$o(oe,me+1,c,this.getSignatureName(),_);continue e}if(G?.aborted)throw new $e("mid-step",G.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let Me=this.getMetricsInstruments();if(Me){$o(Me,me+1,c,this.getSignatureName(),_);let oe=new Set;n.forEach(Re=>{Re.functionsExecuted.forEach(q=>oe.add(q))}),oe.size>0&&Gl(Me,!0,oe.size,!0,!1,this.getSignatureName(),_),Ul(Me,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),_)}return}catch(Q){if(Q instanceof $e)throw Q;I=Q;let le,Ie=this.isDebug(e,r),Ee=this.getLogger(e,r),Me=this.getMetricsInstruments(),oe=this.getSignatureName(),Re={error:Q,errCount:W,logger:Ee,metricsInstruments:Me,signatureName:oe,span:o,debug:Ie,customLabels:_};if(o?.recordException(Q),Q instanceof He)le=Ql(Re),y=Q;else if(Q instanceof wt)le=Yl(Re),y=Q;else if(Q instanceof Pe)Xl(Re);else{if(Q instanceof st)throw Q;{let q=Q,ce=q instanceof Xe&&q.status>=500&&q.status<600,fe=q instanceof qe,he=q instanceof nt;throw ce||fe||he?Q:zi(Q,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 ce of n)ce.content="",ce.values={},ce.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let K=this.getMetricsInstruments();throw K&&Ii(K,u,!1,u,this.getSignatureName(),_),zi(new Error(`Unable to fix validation error: ${(y??I)?.message??(y??I)?.toString()??"unknown error"}
774
+ `))):n.push(t),[{type:"text",text:n.join("")}]}},es=s=>s.map(e=>`\`${e.title}\``).join(", "),Nu=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Dn(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 l=$n(n.description);e&&(l=ts(l,e)),a=` ${l}`}return`${r}: (${i})${a}`.trim()}).join(`
775
+ `),Lu=(s,e)=>s.map(n=>{let r=n.title,o=n.type?.name?Dn(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 l=n.type?.name==="class"?n.description:$n(n.description);e&&(l=ts(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}: (${i})${a}`.trim()}).join(`
776
+ `),ld=(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?du(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 ud(s){return`{ ${Object.entries(s).map(([t,n])=>{let r=n.isOptional?"?":"",o=Dn({name:n.type,isArray:n.isArray,fields:n.fields,options:n.options});return`${t}${r}: ${o}`}).join(", ")} }`}var Dn=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 ${ud(s.fields)}`:"object";default:return"string"}})();return s?.isArray?`json array of ${e} items`:e};function Yt(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 cd=(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 $n(s){let e=s.trim();return e.length>0?`${e.charAt(0).toUpperCase()}${e.slice(1)}${e.endsWith(".")?"":"."}`:""}function ts(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 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 pd(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 dd(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 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 ns(s,e,t,n){if(!e?.resultPicker||s.length<=1)return 0;let r=e.resultPicker;if((t?pd(t,n):!1)&&t){let l=dd(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 i=s.map((l,u)=>({index:u,sample:l.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 $u(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 ns(r,t,s,e)}var Gn=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 rs=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}=md(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,l]of this.tools.entries())i.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=i.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 We(`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[i,a]of Object.entries(r)){let l=[...o,i];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 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 md(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,l)=>{for(let[u,c]of Object.entries(i)){let p=a?`${a}.${u}`:u,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=gd(c);e.push({name:hd(d),title:fd(s.name,p),type:m,description:c.description||`${u} parameter for ${s.name}`,isOptional:!0}),t.set(d,e[e.length-1])}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function gd(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 fd(s,e){return`${s} ${e.replace(/\./g," ")}`}function hd(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Uu(s,e,t){let n=new _e(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(u=>u.name===a.name)||n.addOutputField(a);else{let a=Wi(o.name),l=Gu(o.parameters);n.getOutputFields().some(c=>c.name===a)||n.addOutputField({name:a,title:Du(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 s)if(o.parameters?.properties&&Object.keys(o.parameters.properties).length>0){let{fields:i,paramFieldMap:a}=xd(o);r.set(o.name,a);for(let l of i)n.getOutputFields().some(c=>c.name===l.name)||n.addOutputField(l)}else{let i=Wi(o.name),a=Gu(o.parameters);n.getOutputFields().some(u=>u.name===i)||n.addOutputField({name:i,title:Du(o.name),type:a,description:o.description||`Parameters for ${o.name}`,isOptional:!0})}return{signature:n,toolParamFieldMap:r}}}function xd(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,l)=>{for(let[u,c]of Object.entries(i)){let p=a?`${a}.${u}`:u,d=`${s.name}.${p}`;if(c.type==="object"&&c.properties)o(c.properties,p,c.required||[]);else{let m=Ad(c),g={name:Wi(d),title:yd(s.name,p),type:m,description:c.description||`${u} parameter for ${s.name}`,isOptional:!0};e.push(g),t.set(d,g)}}};return o(n,"",r),{fields:e,paramFieldMap:t}}function Ad(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 yd(s,e){return`${s} ${e.replace(/\./g," ")}`}function Wi(s){return s.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_|_$/g,"").replace(/[^a-z0-9_]/g,"_")}function Du(s){return s.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}function Gu(s){return!s||!s.properties||Object.keys(s.properties).length===0?{name:"string",isArray:!1}:{name:"json",isArray:!1}}var $r=class{tools;router;injectedToolFieldNames=new Set;constructor(e){this.tools=e,this.router=new rs(e)}processSignature(e){let{signature:t}=Uu(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 Un="__finalResult",Se=class s extends Ut{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??Xt)(this.signature,n),this.asserts=this.options?.asserts??[],this.streamingAsserts=this.options?.streamingAsserts??[],this.excludeContentFromTrace=t?.excludeContentFromTrace??!1,this.functions=t?.functions?Qt(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 Ti()}getMergedCustomLabels(e,t){return Ht(le.customLabels,e?.getOptions?.()?.customLabels,t?.customLabels)}updateMeter(e){Ti(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:l,model:u,rateLimiter:c,stream:p,thinkingTokenBudget:d,showThoughts:m}=n??{},g=await $u(t,l,{resultPicker:n?.resultPicker}),f=t?.history(g,l)??[];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}:{}},A=this.isDebug(e,n),h=a===0,b=this.getLogger(e,n);o=this.signatureToolCallingManager?[]:o;let I,C=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).`);I={type:"json_schema",schema:{name:"output",strict:!0,schema:Gt(C)}}}let M=n?.contextCache?.cacheBreakpoint??"after-examples",S=e.getFeatures?.(u)?.caching?.cacheBreakpoints===!1,O=n?.contextCache&&(S||M==="after-functions"||M==="after-examples"),k=o?.length&&O?o.map((L,E)=>({...L,cache:E===o.length-1})):o;return await e.chat({chatPrompt:f,functions:k,functionCall:i,modelConfig:x,model:u,responseFormat:I},{sessionId:l,rateLimiter:c,stream:p,debug:A,debugHideSystemPrompt:n?.debugHideSystemPrompt??this.options?.debugHideSystemPrompt??!h,thinkingTokenBudget:d,showThoughts:m,traceContext:r,abortSignal:n?.abortSignal??le.abortSignal,stepIndex:a,logger:b,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:l,stepContext:u}){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,A=this.usage,h=r===0,b=this.isDebug(e,n),I=this.getLogger(e,n),{functions:C,functionCall:R}=bu(p,m,h,n);this.structuredOutputFunctionFallback&&C.filter(O=>O.name!==Un).length===0&&(R={type:"function",function:{name:Un}});let M=await this.forwardSendRequest({ai:e,mem:t,options:n,traceContext:i,functions:C,functionCall:R,stepIndex:r});M instanceof ReadableStream?yield*Cu({ai:e,model:x,res:M,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:C,strictMode:f,span:o,states:a,usage:A,asserts:this.asserts,streamingAsserts:this.streamingAsserts,fieldProcessors:this.fieldProcessors,streamingFieldProcessors:this.streamingFieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:I,debug:b,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal}):yield*wu({ai:e,model:x,res:M,mem:t,sessionId:c,traceId:o?o.spanContext?.().traceId:void 0,functions:C,span:o,strictMode:f,states:a,usage:A,asserts:this.asserts,fieldProcessors:this.fieldProcessors,thoughtFieldName:this.thoughtFieldName,excludeContentFromTrace:this.excludeContentFromTrace,signature:this.signature,logger:I,debug:b,functionResultFormatter:d,signatureToolCallingManager:g,stopFunctionNames:l,disableMemoryCleanup:n.disableMemoryCleanup,stepContext:u,abortSignal:n.abortSignal})}async*_forward2(e,t,n,r,o,i){this.signatureToolCallingManager=void 0;let a=r?.stopFunction??this.options?.stopFunction,l=Array.isArray(a)?a.map(ge=>ge.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 En,d=r.functions?Qt(r.functions):[...this.functions],m=new Gn(c),g;if(r.selfTuning){if(g=r.selfTuning===!0?{model:!0,thinkingBudget:!0}:r.selfTuning,g.model!==!1){let ke=e.getModelList()?.filter(N=>"model"in N);if(!ke||ke.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 ge=Di(e,g,r.model?String(r.model):void 0);d.push(ge)}let f={...r},x=r.stepHooks,A=d&&d.length>0,h=r.functionCallMode??this.options?.functionCallMode??"auto";A&&h==="prompt"&&(this.signatureToolCallingManager=new $r(d)),A&&h==="auto"&&!e.getFeatures(r.model).functions&&(this.signatureToolCallingManager=new $r(d));let b,I,C=this.options?.promptTemplate??Xt;this.signatureToolCallingManager&&(this.signature=this.signatureToolCallingManager.processSignature(this.signature),this.setSignature(this.signature));let R=this.signature.hasComplexFields(),M=e.getFeatures?.(r.model),S=r.structuredOutputMode??this.options?.structuredOutputMode??"auto";if(this.structuredOutputFunctionFallback=R&&(S==="function"||S==="auto"&&!M?.structuredOutputs),this.structuredOutputFunctionFallback){let ge={name:Un,description:"Return the final result. Call this function with the complete output data.",parameters:Gt(this.signature.getOutputFields()),func:async()=>"done"};d.push(ge),l=[...l??[],Un.toLowerCase()]}let O=e.getFeatures?.(r.model)?.caching?.cacheBreakpoints===!1,k={functions:this.signatureToolCallingManager?[]:d,thoughtFieldName:this.thoughtFieldName,contextCache:r.contextCache,examplesInSystem:r.examplesInSystem,ignoreBreakpoints:O,structuredOutputFunctionName:this.structuredOutputFunctionFallback?Un:void 0};this.promptTemplate=new C(this.signature,k);let _,L=performance.now();Array.isArray(t)?(dl(t),_=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos})):_=this.promptTemplate.render(t,{examples:this.examples,demos:this.demos});let E=performance.now()-L,P=this.getMetricsInstruments(),$=this.getMergedCustomLabels(e,r);P&&zo(P,"prompt_render",E,this.getSignatureName(),$);let D=performance.now();p.addRequest(_,r.sessionId);let z=performance.now()-D;P&&zo(P,"memory_update",z,this.getSignatureName(),$);let U=new Map;n.forEach(ge=>{U.set(ge.index,{})});let te=()=>{let ge=m._consumePendingOptions();if(ge){let{modelConfig:N,...q}=ge;f={...f,...q},N&&(f.modelConfig={...f.modelConfig,...N})}let Ce=m._consumeFunctionsToAdd();if(Ce){let N=Qt(Ce);for(let q of N)d.some(W=>W.name===q.name)||d.push(q)}let ke=m._consumeFunctionsToRemove();if(ke){let N=new Set(ke.map(q=>q.toLowerCase()));for(let q=d.length-1;q>=0;q--)N.has(d[q].name.toLowerCase())&&d.splice(q,1)}},B=r?.abortSignal??le.abortSignal;e:for(let ge=0;ge<c;ge++){if(m._beginStep(ge),te(),g&&g.model!==!1){let ke=d.findIndex(N=>N.name==="adjustGeneration");if(ke!==-1){let N=f.model?String(f.model):void 0;d[ke]=Di(e,g,N)}}if(m._isStopRequested)break;if(B?.aborted)throw new Ee("between-steps",B.reason??"Aborted between steps");if(x?.beforeStep&&(await x.beforeStep(m),te(),m._isStopRequested))break;let Ce=u;for(let ke=0;ke<=Ce;ke++)try{let N=u+1;for(let W=0;W<N;W++){n.forEach(ee=>{ee.content="",ee.values={},ee.functionCalls=[],ee.functionsExecuted=new Set,ee.xstate={extractedFields:[],streamedIndex:{},s:-1}}),W>0&&U.forEach((ee,ie)=>{U.set(ie,{})});let he=new Map;n.forEach(ee=>{he.set(ee.index,{})});try{let ee=this.forwardCore({options:{...f,functions:d},ai:e,mem:p,stepIndex:ge,span:o,traceContext:i,states:n,stopFunctionNames:l,stepContext:m}),ie=!1;try{for await(let oe of ee)if(oe!==void 0){let ve=oe.index,H=oe.delta,re=he.get(ve)??{},de=U.get(ve)??{},ye={},we=!1;for(let Oe of Object.keys(H)){let lt=H[Oe],Ue=re[Oe],rt;typeof lt=="string"&&(typeof Ue=="string"||Ue===void 0)?rt=(Ue??"")+lt:Array.isArray(lt)&&(Array.isArray(Ue)||Ue===void 0)?rt=[...Ue??[],...lt]:rt=lt,re[Oe]=rt;let Te=rt,Be=de[Oe];if(typeof Te=="string"&&typeof Be=="string")if(Te.startsWith(Be)){let se=Te.slice(Be.length);se&&(ye[Oe]=se,we=!0,de[Oe]=Te)}else Be.startsWith(Te)||Te!==Be&&(ye[Oe]=Te,we=!0,de[Oe]=Te);else if(Array.isArray(Te)&&Array.isArray(Be)){if(Te.length>Be.length){let se=Te.slice(Be.length);ye[Oe]=se,we=!0,de[Oe]=Te}}else JSON.stringify(Te)!==JSON.stringify(Be)&&(ye[Oe]=Te,we=!0,de[Oe]=Te)}we&&(yield{version:W,index:oe.index,delta:ye})}}catch(oe){if(oe instanceof Jt){if(ie=!0,this.structuredOutputFunctionFallback){let ve=oe.calls.find(H=>H.func.name===Un);if(ve?.args){let H=ve.args;_n(this.signature,H);let re=this.signature.getOutputFields();for(let de of n){let ye={};for(let we of re)we.name in H&&!we.isInternal&&(ye[we.name]=H[we.name],de.values[we.name]=H[we.name]);yield{version:W,index:de.index,delta:ye}}for(let de of n)await Mn(this.asserts,de.values);if(this.fieldProcessors.length>0)for(let de of n)await Pr(this.fieldProcessors,de.values,p,r.sessionId)}}}else throw oe}if(this.usage.length>0){let oe=this.usage[this.usage.length-1];oe?.tokens&&m._addUsage(oe.tokens.promptTokens??0,oe.tokens.completionTokens??0,oe.tokens.totalTokens??0)}n.some(oe=>oe.functionsExecuted.size>0)&&x?.afterFunctionExecution&&(await x.afterFunctionExecution(m),te());let Pe=ie||m._isStopRequested?!1:Tu(p,l,n,f?.sessionId);if(x?.afterStep&&(await x.afterStep(m),te()),Pe&&!m._isStopRequested&&!B?.aborted){let oe=this.getMetricsInstruments();oe&&jo(oe,ge+1,c,this.getSignatureName(),$);continue e}if(B?.aborted)throw new Ee("mid-step",B.reason??"Aborted");r?.disableMemoryCleanup||(p.removeByTag("invalid-assistant",r.sessionId),p.removeByTag("correction",r.sessionId),p.removeByTag("error",r.sessionId));let Ne=this.getMetricsInstruments();if(Ne){jo(Ne,ge+1,c,this.getSignatureName(),$);let oe=new Set;n.forEach(ve=>{ve.functionsExecuted.forEach(H=>oe.add(H))}),oe.size>0&&Wl(Ne,!0,oe.size,!0,!1,this.getSignatureName(),$),Vl(Ne,this.fieldProcessors.length,this.streamingFieldProcessors.length,this.getSignatureName(),$)}return}catch(ee){if(ee instanceof Ee)throw ee;I=ee;let ie,fe=this.isDebug(e,r),Pe=this.getLogger(e,r),Ne=this.getMetricsInstruments(),oe=this.getSignatureName(),ve={error:ee,errCount:W,logger:Pe,metricsInstruments:Ne,signatureName:oe,span:o,debug:fe,customLabels:$};if(o?.recordException(ee),ee instanceof We)ie=ou(ve),b=ee;else if(ee instanceof vt)ie=su(ve),b=ee;else if(ee instanceof Fe)iu(ve);else{if(ee instanceof dt)throw ee;{let H=ee,re=H instanceof et&&H.status>=500&&H.status<600,de=H instanceof Ke,ye=H instanceof ct;throw re||de||ye?ee:Vi(ee,e,this.signature)}}if(ie&&(p.addTag("error",r.sessionId),p.addRequest([{role:"user",content:this.promptTemplate.renderExtraFields(ie)}],r.sessionId),p.addTag("correction",r.sessionId),this.signature.hasComplexFields()))for(let re of n)re.content="",re.values={},re.xstate={extractedFields:[],streamedIndex:{},s:-1}}}let q=this.getMetricsInstruments();throw q&&Si(q,u,!1,u,this.getSignatureName(),$),Vi(new Error(`Unable to fix validation error: ${(b??I)?.message??(b??I)?.toString()??"unknown error"}
649
777
 
650
778
  LLM Output:
651
779
  ${n.map(W=>W.content).join(`
652
780
  ---
653
- `)}`),e,this.signature)}catch(U){let K=U,W=K instanceof Xe&&K.status>=500&&K.status<600,re=K instanceof qe,Q=K instanceof nt,le=K instanceof st;if((W||re||Q||le)&&Ae<ge){let Ee=this.isDebug(e,r),Me=this.getLogger(e,r),q=Math.min(6e4,1e3*Math.pow(2,Ae));Ee&&Me&&Me({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${Ae+1}/${ge+1}): ${K.message}. Retrying in ${q}ms...`}),o?.addEvent("infrastructure.retry",{attempt:Ae+1,maxRetries:ge,delay:q,errorType:K instanceof Xe?"status_error":K instanceof qe?"network_error":K instanceof nt?"timeout_error":"stream_terminated",errorMessage:K.message}),await new Promise((ce,fe)=>{let he=!1,we,ke=()=>{G&&we&&G.removeEventListener("abort",we)},Ye=setTimeout(()=>{he||(he=!0,ke(),ce())},q);if(G){if(we=()=>{he||(he=!0,clearTimeout(Ye),ke(),fe(new $e("infrastructure-retry-backoff",G.reason?String(G.reason):"Aborted during retry backoff")))},G.aborted){we();return}G.addEventListener("abort",we,{once:!0})}});continue}throw U}}throw M&&$o(M,c,c,this.getSignatureName(),_),zi(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")&&lt(r,n),o.name==="number"&&ut(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"?lt(a,n):o.name==="number"?ut(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"?lt(i,a):o.type==="number"?ut(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 l=0;l<i.length;l++){let u=i[l];o.type==="string"||o.type==="code"?lt(u,a):o.type==="number"?ut(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=Tt(r.signal,n?.abortSignal??ie.abortSignal),i=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&&Do(c,"state_creation",u,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Wt(n.functions,this.functions)),!d){yield*this._forward2(e,t,l,{...i,functions:m});return}let g=m?.map(C=>C.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,b=x?`AxGen > ${x}`:"AxGen",h=d.startSpan(b,{kind:Dn.SpanKind.SERVER,attributes:f}),y=Dn.context.active(),I=Dn.trace.setSpan(y,h);try{if(this.excludeContentFromTrace||h.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,l,{...i,functions:m},h,I),!this.excludeContentFromTrace){let C=l.map(P=>P.values),R=C.length===1?C[0]:C;h.addEvent("output",{content:JSON.stringify(R,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??ie.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(),l=n?.stream??!1,u=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&zl(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,b=0;for await(let v of g)v.version!==x&&(f=[]),x=v.version,f=Ri(f,v),b++;c=x;let h=performance.now();p=!!n?.resultPicker;let y=await Yo(f,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),I=performance.now()-h,R=f[y]?.delta??{},P=Array.isArray(t)?{}:t??{};if(this.trace={...P,...R},p&&this.isDebug(e,n)){let v=this.getLogger(e,n);Ga(f.length,y,I,v)}if(u=!0,d&&(Bl(d,f.length,p,p?I:void 0,a,m),jl(d,l,b,void 0,a,m)),r&&o)try{await r(o,R)}catch{}return R}catch(d){throw u=!1,d}finally{let d=performance.now()-i,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&($l(m,d,u,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&Ii(m,c,u,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??ie.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=[],l=0;for await(let p of i)p.version!==l&&(a=[]),l=p.version,a=Ri(a,p);let u=await Yo(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??ie.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??ie.logger??e.getLogger()}computeCacheKey(e,t){let n=tt("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 l=tt("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 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")}},Nr=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 zi(s,e,t){let n=s instanceof Error?s:new Error(String(s));if(n instanceof $e)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(),l={model:i,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Nr(`Generate failed: ${n.message}`,l,{cause:n})}var hd=s=>s.replace(/^\W+|\W+$/g,""),xd=(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]},Ad=s=>{let e=new Set,t=[];for(let n of s)e.has(n)||(e.add(n),t.push(n));return t},yd=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}},bd=s=>{let e=s.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():s},Id=(s,e)=>{let t=[];for(let n=0;n<s.length;n+=e)t.push(s.slice(n,n+e));return t},Zo={trimNonAlphaNum:hd,splitIntoTwo:xd,dedup:Ad,extractIdAndText:yd,extractIndexPrefixedText:bd,batchArray:Id};var es=class extends Se{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."
654
- 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}=Zo.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 ts=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 lb=new Ze,ns=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},rs=class{ai;db;debug;constructor(e){this.db=new _t,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 i=o.at(0);return i?i.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var Fu=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 _u(s,e){return s.filter(t=>!e.has(t))}function Nu(s){let e={};for(let t of s)e[t]=(e[t]||0)+1;return e}function Qt(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 Cd(s,e){return Qt(s)===Qt(e)?1:0}function Td(s,e){let t=Qt(s).split(" "),n=Qt(e).split(" "),r=Nu(t),o=Nu(n),i=0;for(let u in r){let c=r[u]??0,p=o[u]??0;i+=Math.min(c,p)}if(i===0)return 0;let a=i/t.length,l=i/n.length;return 2*a*l/(a+l)}function wd(s,e,t,n=!1){let r=Qt(s).split(" "),o=Qt(e).split(" "),i=Qt(t).split(" "),a=new Set([...Fu,...r]);o=_u(o,a),i=_u(i,a);let l=0,u=l/o.length,c=l/i.length,p=2*u*c/(u+c);return n?c:p}var Lu={emScore:Cd,f1Score:Td,novelF1ScoreOptimized:wd};var os=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 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 ${i}: ${l instanceof Error?l.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
781
+ `)}`),e,this.signature)}catch(N){let q=N,W=q instanceof et&&q.status>=500&&q.status<600,he=q instanceof Ke,ee=q instanceof ct,ie=q instanceof dt;if((W||he||ee||ie)&&ke<Ce){let Pe=this.isDebug(e,r),Ne=this.getLogger(e,r),H=Math.min(6e4,1e3*Math.pow(2,ke));Pe&&Ne&&Ne({name:"Notification",id:"infrastructure-retry",value:`Infrastructure error (attempt ${ke+1}/${Ce+1}): ${q.message}. Retrying in ${H}ms...`}),o?.addEvent("infrastructure.retry",{attempt:ke+1,maxRetries:Ce,delay:H,errorType:q instanceof et?"status_error":q instanceof Ke?"network_error":q instanceof ct?"timeout_error":"stream_terminated",errorMessage:q.message}),await new Promise((re,de)=>{let ye=!1,we,Oe=()=>{B&&we&&B.removeEventListener("abort",we)},Ue=setTimeout(()=>{ye||(ye=!0,Oe(),re())},H);if(B){if(we=()=>{ye||(ye=!0,clearTimeout(Ue),Oe(),de(new Ee("infrastructure-retry-backoff",B.reason?String(B.reason):"Aborted during retry backoff")))},B.aborted){we();return}B.addEventListener("abort",we,{once:!0})}});continue}throw N}}throw P&&jo(P,c,c,this.getSignatureName(),$),Vi(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"&&Dt(r,n),o.name,o.name,(o.name==="string"||o.name==="code")&&ft(r,n),o.name==="number"&&ht(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"?ft(a,n):o.name==="number"?ht(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"?ft(i,a):o.type==="number"?ht(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 l=0;l<i.length;l++){let u=i[l];o.type==="string"||o.type==="code"?ft(u,a):o.type==="number"?ht(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=Tt(r.signal,n?.abortSignal??le.abortSignal),i=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&&zo(c,"state_creation",u,this.getSignatureName(),p);let d=n?.tracer??this.options?.tracer??e.getOptions().tracer,m=this.functions;if(n?.functions&&(m=Qt(n.functions,this.functions)),!d){yield*this._forward2(e,t,l,{...i,functions:m});return}let g=m?.map(C=>C.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,A=x?`AxGen > ${x}`:"AxGen",h=d.startSpan(A,{kind:Bn.SpanKind.SERVER,attributes:f}),b=Bn.context.active(),I=Bn.trace.setSpan(b,h);try{if(this.excludeContentFromTrace||h.addEvent("input",{content:JSON.stringify(t,null,2)}),yield*this._forward2(e,t,l,{...i,functions:m},h,I),!this.excludeContentFromTrace){let C=l.map(M=>M.values),R=C.length===1?C[0]:C;h.addEvent("output",{content:JSON.stringify(R,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??le.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(),l=n?.stream??!1,u=!1,c=0,p=!1;try{let d=this.getMetricsInstruments(),m=this.getMergedCustomLabels(e,n);d&&Yl(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,A=0;for await(let S of g)S.version!==x&&(f=[]),x=S.version,f=Ei(f,S),A++;c=x;let h=performance.now();p=!!n?.resultPicker;let b=await ns(f,{resultPicker:n?.resultPicker},n?.mem,n?.sessionId),I=performance.now()-h,R=f[b]?.delta??{},M=Array.isArray(t)?{}:t??{};if(this.trace={...M,...R},p&&this.isDebug(e,n)){let S=this.getLogger(e,n);Wa(f.length,b,I,S)}if(u=!0,d&&(Ql(d,f.length,p,p?I:void 0,a,m),Jl(d,l,A,void 0,a,m)),r&&o)try{await r(o,R)}catch{}return R}catch(d){throw u=!1,d}finally{let d=performance.now()-i,m=this.getMetricsInstruments(),g=this.getMergedCustomLabels(e,n);m&&(Hl(m,d,u,a,e.getName(),n?.model?String(n.model):void 0,g),c>0&&Si(m,c,u,n?.maxRetries??this.options?.maxRetries??3,a,g))}}async*streamingForward(e,t,n){let r=n?.cachingFunction??this.options?.cachingFunction??le.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=[],l=0;for await(let p of i)p.version!==l&&(a=[]),l=p.version,a=Ei(a,p);let u=await ns(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??le.debug??e.getOptions().debug??!1}getLogger(e,t){return t?.logger??this.options?.logger??le.logger??e.getLogger()}computeCacheKey(e,t){let n=ut("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 l=ut("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 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")}},Dr=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 Vi(s,e,t){let n=s instanceof Error?s:new Error(String(s));if(n instanceof Ee)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(),l={model:i,maxTokens:a?.maxTokens,streaming:a?.stream??!1,signature:{input:t.getInputFields(),output:t.getOutputFields(),description:t.getDescription()}};return new Dr(`Generate failed: ${n.message}`,l,{cause:n})}var bd=s=>s.replace(/^\W+|\W+$/g,""),Id=(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]},Cd=s=>{let e=new Set,t=[];for(let n of s)e.has(n)||(e.add(n),t.push(n));return t},wd=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}},Td=s=>{let e=s.match(/^(\d+)[.,\s]+(.*)$/);return e&&e[2]!==void 0?e[2].trim():s},Rd=(s,e)=>{let t=[];for(let n=0;n<s.length;n+=e)t.push(s.slice(n,n+e));return t},os={trimNonAlphaNum:bd,splitIntoTwo:Id,dedup:Cd,extractIdAndText:wd,extractIndexPrefixedText:Td,batchArray:Rd};var ss=class extends Se{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."
782
+ 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}=os.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 is=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 bb=new tt,as=class{name;context;constructor(e,t){this.name=e,this.context=t}getName(){return this.name}getContext(){return this.context}},ls=class{ai;db;debug;constructor(e){this.db=new Lt,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 i=o.at(0);return i?i.id:""}setOptions(e){typeof e.debug=="boolean"&&(this.debug=e.debug)}};var Bu=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 ju(s,e){return s.filter(t=>!e.has(t))}function zu(s){let e={};for(let t of s)e[t]=(e[t]||0)+1;return e}function Zt(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 Sd(s,e){return Zt(s)===Zt(e)?1:0}function kd(s,e){let t=Zt(s).split(" "),n=Zt(e).split(" "),r=zu(t),o=zu(n),i=0;for(let u in r){let c=r[u]??0,p=o[u]??0;i+=Math.min(c,p)}if(i===0)return 0;let a=i/t.length,l=i/n.length;return 2*a*l/(a+l)}function vd(s,e,t,n=!1){let r=Zt(s).split(" "),o=Zt(e).split(" "),i=Zt(t).split(" "),a=new Set([...Bu,...r]);o=ju(o,a),i=ju(i,a);let l=0,u=l/o.length,c=l/i.length,p=2*u*c/(u+c);return n?c:p}var qu={emScore:Sd,f1Score:kd,novelF1ScoreOptimized:vd};var us=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 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 ${i}: ${l instanceof Error?l.message:"Unknown error"}`)}}let o=n>0?r/n:0;this.ai.getOptions().debug&&console.log(`
655
783
  Performance: `,r,"/",n,"Average Score: ",o,`
656
- `)}};function ss(s){return s.map(e=>{let t=e.type?.name??"string",n=e.description?`: ${e.description}`:"";return`- ${e.name} (${t})${n}`}).join(`
657
- `)}function Rd(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 Sd(s){return s==null?!1:!!(typeof s=="string"||typeof s=="number"||typeof s=="boolean"||Array.isArray(s)&&s.every(e=>typeof e!="object"))}var Gt=class{signature;options;constructor(e,t){this.signature=e,this.options={...t,randomizeOrder:t.randomizeOrder??!0}}buildForwardOptions(){let{ai:e,criteria:t,description:n,randomizeOrder:r,...o}=this.options;return{...o,maxSteps:1}}buildTaskDescription(){let e=this.signature.getDescription()||"Complete the task based on the input.",t=this.options.description?.trim();return t?`${e}
784
+ `)}};function cs(s){return s.map(e=>{let t=e.type?.name??"string",n=e.description?`: ${e.description}`:"";return`- ${e.name} (${t})${n}`}).join(`
785
+ `)}function Od(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 Ed(s){return s==null?!1:!!(typeof s=="string"||typeof s=="number"||typeof s=="boolean"||Array.isArray(s)&&s.every(e=>typeof e!="object"))}var Bt=class{signature;options;constructor(e,t){this.signature=e,this.options={...t,randomizeOrder:t.randomizeOrder??!0}}buildForwardOptions(){let{ai:e,criteria:t,description:n,randomizeOrder:r,...o}=this.options;return{...o,maxSteps:1}}buildTaskDescription(){let e=this.signature.getDescription()||"Complete the task based on the input.",t=this.options.description?.trim();return t?`${e}
658
786
 
659
787
  Additional Judge Guidance:
660
- ${t}`:e}async evaluate(e,t,n){return n&&Sd(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 u of n){let c=e[u.name],p=t[u.name];p!==void 0&&(o++,Rd(c,p)?r++:i.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: ${i.join("; ")}`;return{score:a,reasoning:l,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getInputFields(),o=this.signature.getOutputFields(),i=this.options.randomizeOrder?Math.random()>.5:!0,a=i?t:n,l=i?n:t,u=`
788
+ ${t}`:e}async evaluate(e,t,n){return n&&Ed(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 u of n){let c=e[u.name],p=t[u.name];p!==void 0&&(o++,Od(c,p)?r++:i.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: ${i.join("; ")}`;return{score:a,reasoning:l,mode:"absolute"}}async runRelativistic(e,t,n){let r=this.signature.getInputFields(),o=this.signature.getOutputFields(),i=this.options.randomizeOrder?Math.random()>.5:!0,a=i?t:n,l=i?n:t,u=`
661
789
  You are an impartial judge comparing two AI system outputs for the same input.
662
790
 
663
791
  **Task Description:** ${this.buildTaskDescription()}
664
792
 
665
793
  **Input Fields:**
666
- ${ss(r)}
794
+ ${cs(r)}
667
795
 
668
796
  **Output Fields:**
669
- ${ss(o)}
797
+ ${cs(o)}
670
798
 
671
799
  **Instructions:**
672
800
  1. Carefully analyze both Response A and Response B
@@ -694,10 +822,10 @@ You are evaluating the quality of an AI system output.
694
822
  **Task Description:** ${this.buildTaskDescription()}
695
823
 
696
824
  **Input Fields:**
697
- ${ss(n)}
825
+ ${cs(n)}
698
826
 
699
827
  **Output Fields:**
700
- ${ss(r)}
828
+ ${cs(r)}
701
829
 
702
830
  **Evaluation Criteria:**
703
831
  ${o}
@@ -716,7 +844,7 @@ First explain your reasoning, then classify the response into one of the quality
716
844
  ->
717
845
  reasoning:string "Detailed explanation for the quality assessment",
718
846
  quality:class "excellent, good, acceptable, poor, unacceptable" "Quality tier"
719
- `);a.setInstruction(i);let l=await a.forward(this.options.ai,{task_input:JSON.stringify(e),system_output:JSON.stringify(t)},this.buildForwardOptions()),u={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},c=l.quality.toLowerCase();return{score:u[c]??.5,reasoning:l.reasoning,mode:"reference-free",qualityTier:c}}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={},i=!1;for(let u of this.signature.getOutputFields())u.name in t&&(o[u.name]=t[u.name],i=!0);let a=n;return(await this.evaluate(r,a,i?o:void 0)).score}}getSignature(){return this.signature}};function $u(){let s=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${s}-${e}`}var Gn=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=$u(),o=new Date,i,a;try{let l=this.gen.streamingForward(e,t,n);for await(let u of l)u.partial?i=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&&i?i:{},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=$u(),o=new Date,i,a;try{return i=await this.gen.forward(e,t,n),i}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&&i?i:{},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 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 Du(s){return Fe.create(s)}function et(s,e){let t=typeof s=="string"?Fe.create(s):s;return new Se(t,e)}var Gu=s=>{console.log(s)},qi=(s=Gu)=>{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=`
847
+ `);a.setInstruction(i);let l=await a.forward(this.options.ai,{task_input:JSON.stringify(e),system_output:JSON.stringify(t)},this.buildForwardOptions()),u={excellent:1,good:.8,acceptable:.5,poor:.2,unacceptable:0},c=l.quality.toLowerCase();return{score:u[c]??.5,reasoning:l.reasoning,mode:"reference-free",qualityTier:c}}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={},i=!1;for(let u of this.signature.getOutputFields())u.name in t&&(o[u.name]=t[u.name],i=!0);let a=n;return(await this.evaluate(r,a,i?o:void 0)).score}}getSignature(){return this.signature}};function Hu(){let s=Date.now().toString(36),e=Math.random().toString(36).substring(2,8);return`trace-${s}-${e}`}var jn=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=Hu(),o=new Date,i,a;try{let l=this.gen.streamingForward(e,t,n);for await(let u of l)u.partial?i=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&&i?i:{},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=Hu(),o=new Date,i,a;try{return i=await this.gen.forward(e,t,n),i}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&&i?i:{},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 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 Ku(s){return _e.create(s)}function nt(s,e){let t=typeof s=="string"?_e.create(s):s;return new Se(t,e)}var Wu=s=>{console.log(s)},Ji=(s=Wu)=>{let e=new tt,t=e.gray("\u2500".repeat(50)),n=e.gray("\u2501".repeat(50));return r=>{let o="";switch(r.name){case"OptimizationStart":o=`
720
848
  ${e.blueBright("\u25CF ")}${e.whiteBright("Optimization Started")}
721
849
  ${t}
722
850
  ${e.white("Optimizer:")} ${e.cyan(r.value.optimizerType)}
@@ -759,7 +887,7 @@ ${t}
759
887
  ${e.white("Config:")} ${e.cyan(JSON.stringify(r.value.config).slice(0,80))}${JSON.stringify(r.value.config).length>80?"...":""}
760
888
  `;break;default:o=`${e.red("\u25CF ")}${e.whiteBright("Unknown Event")}
761
889
  ${e.white(JSON.stringify(r).slice(0,100))}${JSON.stringify(r).length>100?"...":""}
762
- `}s(o)}},Uu=(s=Gu)=>{let e="\u2500".repeat(60);return t=>{let n="";switch(t.name){case"OptimizationStart":n=`[ OPTIMIZATION START: ${t.value.optimizerType} ]
890
+ `}s(o)}},Vu=(s=Wu)=>{let e="\u2500".repeat(60);return t=>{let n="";switch(t.name){case"OptimizationStart":n=`[ OPTIMIZATION START: ${t.value.optimizerType} ]
763
891
  ${e}
764
892
  Config: ${JSON.stringify(t.value.config,null,2)}
765
893
  Examples: ${t.value.exampleCount}, Validation: ${t.value.validationCount}
@@ -785,7 +913,7 @@ Score: ${t.value.score.toFixed(3)}
785
913
  Config: ${JSON.stringify(t.value.config)}
786
914
  ${e}`;break;default:n=`[ UNKNOWN OPTIMIZER EVENT ]
787
915
  ${JSON.stringify(t)}
788
- ${e}`}s(n)}},Lr=qi();var Hi={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},is,kd=s=>{if(is)return is;if(s)return is=vd(s),is};var as=Hi,ju=s=>{as={...as,...s}},Bu=()=>({...as}),vd=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"})}),yt=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=as.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},Od=(s,e,t,n,r,o)=>{try{let i=yt({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)}},Ed=(s,e,t,n,r,o,i)=>{try{let a=yt({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)}},Md=(s,e,t,n)=>{try{let r=yt({reason:e,optimizer_type:t,...n});s.earlyStoppingCounter&&s.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},Pd=(s,e,t,n,r,o)=>{try{let i=yt({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)}},Fd=(s,e,t,n)=>{try{let r=yt({optimizer_type:t,...n});s.optimizationDurationHistogram&&s.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},_d=(s,e,t,n,r)=>{try{let o=yt({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)}},Nd=(s,e,t,n,r,o)=>{try{let i=yt({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)}},Ld=(s,e,t,n,r,o)=>{try{let i=yt({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)}},$d=(s,e,t,n,r,o,i)=>{try{let a=yt({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)}},Dd=(s,e,t,n,r)=>{try{let o=yt({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)}},Gd=(s,e,t,n,r)=>{try{let o=yt({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)}},kt=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)}},$r=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}},pt=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 $r({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=kd(ie.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?Lr:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Bt(ie.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 l,u=(g,f,x,b,h,y,I,C={},R)=>{this.getOptimizerLogger(R)?.({name:"RoundProgress",value:{round:g,totalRounds:R?.maxIterations??0,currentScore:f,bestScore:y,configuration:x}}),this.updateOptimizationProgress(g,f,x,b,h,y,I,C,R)},c=(g,f)=>{l=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),u(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),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,i=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(i),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 i=t[0],a=await e.forward(this.getAIService(!1,r),i),l=await n({prediction:a,example:i}),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:f})=>{let x=await n({prediction:g,example:f}),b=0;for(let[h,y]of Object.entries(x))b+=y*(d[h]||0);return b};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),l=await n({prediction:a,example:i}),u=Object.keys(l);for(let c of u){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),f=g[c]||0,x=0;for(let[b,h]of Object.entries(g))b!==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 Se(e.getSignature());t.demos&&o.setDemos(t.demos);let i=[],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[f,x]of Object.entries(g))u[f]||(u[f]=[]),u[f].push(x)}catch{}let p={};for(let[d,m]of Object.entries(u))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 l=e[a];if(this.dominates(l.scores,r.scores)){o=!0;break}this.dominates(r.scores,l.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,l=t[i]||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,i=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;i+=c*(p-l),l=Math.max(l,p)}return i}}async saveCheckpoint(e,t,n,r,o={},i){let a=i?.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,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,l={},u){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,u)&&await this.saveCheckpoint(r,o,i,a,l,u),this.getOptimizerLogger(u)?.({name:"RoundProgress",value:{round:e,totalRounds:u?.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;$d(this.metricsInstruments,o,i,0,0,e,r)}Gd(this.metricsInstruments,e,this.targetScore,void 0,r)}recordOptimizationComplete(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Od(this.metricsInstruments,e,t,n,r,i),Fd(this.metricsInstruments,e,n,i);let a=this.costTracker?.getCurrentCost()??0,l=this.costTracker?.getTotalTokens()??0;Pd(this.metricsInstruments,l,a,n,void 0,i)}recordConvergenceMetrics(e,t,n,r,o,i){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(i);Ed(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);Md(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);_d(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Nd(this.metricsInstruments,e,t,n,r,i)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Ld(this.metricsInstruments,e,t,n,r,i)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);Dd(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??ie.optimizerLogger??Lr}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)}%.`,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 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 zu=or(require("crypto"),1);function ls(s){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:s}}function bt(s){return JSON.parse(JSON.stringify(s))}function Ud(s){return Math.ceil(s.length/4)}function Ki(s,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:i=!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(!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=Bd(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:u}})}let m=p.bulletId??jd(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 Ji(s),s.updatedAt=u,{updatedBulletIds:n,autoRemoved:r}}function Wi(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(),Ji(s);return}}}function Yt(s){let e=s.description?`## Context Playbook
916
+ ${e}`}s(n)}},Gr=Ji();var Qi={enabled:!0,enabledCategories:["optimization","convergence","resource_usage","teacher_student","checkpointing","pareto"],maxLabelLength:100,samplingRate:1},ps,Md=s=>{if(ps)return ps;if(s)return ps=Pd(s),ps};var ds=Qi,Ju=s=>{ds={...ds,...s}},Qu=()=>({...ds}),Pd=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"})}),St=s=>{let e={};for(let[t,n]of Object.entries(s))if(n!=null){let r=String(n),o=ds.maxLabelLength;e[t]=r.length>o?r.substring(0,o):r}return e},Fd=(s,e,t,n,r,o)=>{try{let i=St({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)}},_d=(s,e,t,n,r,o,i)=>{try{let a=St({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)}},Nd=(s,e,t,n)=>{try{let r=St({reason:e,optimizer_type:t,...n});s.earlyStoppingCounter&&s.earlyStoppingCounter.add(1,r)}catch(r){console.warn("Failed to record early stopping metric:",r)}},Ld=(s,e,t,n,r,o)=>{try{let i=St({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)}},$d=(s,e,t,n)=>{try{let r=St({optimizer_type:t,...n});s.optimizationDurationHistogram&&s.optimizationDurationHistogram.record(e,r)}catch(r){console.warn("Failed to record optimization duration metric:",r)}},Dd=(s,e,t,n,r)=>{try{let o=St({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)}},Gd=(s,e,t,n,r,o)=>{try{let i=St({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)}},Ud=(s,e,t,n,r,o)=>{try{let i=St({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)}},Bd=(s,e,t,n,r,o,i)=>{try{let a=St({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)}},jd=(s,e,t,n,r)=>{try{let o=St({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)}},zd=(s,e,t,n,r)=>{try{let o=St({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)}},Ur=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}},At=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 Ur({maxTokens:1e6});this.costTracker=e.costTracker??t,this.metricsInstruments=Md(le.meter),this.stats=this.initializeStats(),this.debugOptimizer=e.debugOptimizer??!1,this.optimizerLogger=e.optimizerLogger??(this.verbose?Gr:void 0),this.initializeResultExplainer()}getMergedCustomLabels(e){return Ht(le.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 l,u=(g,f,x,A,h,b,I,C={},R)=>{this.getOptimizerLogger(R)?.({name:"RoundProgress",value:{round:g,totalRounds:R?.maxIterations??0,currentScore:f,bestScore:b,configuration:x}}),this.updateOptimizationProgress(g,f,x,A,h,b,I,C,R)},c=(g,f)=>{l=g,this.triggerEarlyStopping(g,this.currentRound,r)},p=g=>{this.onProgress?.(g),u(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),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,i=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(i),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 i=t[0],a=await e.forward(this.getAIService(!1,r),i),l=await n({prediction:a,example:i}),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:f})=>{let x=await n({prediction:g,example:f}),A=0;for(let[h,b]of Object.entries(x))A+=b*(d[h]||0);return A};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),l=await n({prediction:a,example:i}),u=Object.keys(l);for(let c of u){let p=async({prediction:d,example:m})=>{let g=await n({prediction:d,example:m}),f=g[c]||0,x=0;for(let[A,h]of Object.entries(g))A!==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 Se(e.getSignature());t.demos&&o.setDemos(t.demos);let i=[],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[f,x]of Object.entries(g))u[f]||(u[f]=[]),u[f].push(x)}catch{}let p={};for(let[d,m]of Object.entries(u))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 l=e[a];if(this.dominates(l.scores,r.scores)){o=!0;break}this.dominates(r.scores,l.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,l=t[i]||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,i=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;i+=c*(p-l),l=Math.max(l,p)}return i}}async saveCheckpoint(e,t,n,r,o={},i){let a=i?.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,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,l={},u){this.currentRound=e,this.scoreHistory.push(t),this.configurationHistory.push(n),this.shouldSaveCheckpoint(e,u)&&await this.saveCheckpoint(r,o,i,a,l,u),this.getOptimizerLogger(u)?.({name:"RoundProgress",value:{round:e,totalRounds:u?.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;Bd(this.metricsInstruments,o,i,0,0,e,r)}zd(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),$d(this.metricsInstruments,e,n,i);let a=this.costTracker?.getCurrentCost()??0,l=this.costTracker?.getTotalTokens()??0;Ld(this.metricsInstruments,l,a,n,void 0,i)}recordConvergenceMetrics(e,t,n,r,o,i){if(!this.metricsInstruments)return;let a=this.getMergedCustomLabels(i);_d(this.metricsInstruments,e,t,n,r,o,a)}recordEarlyStoppingMetrics(e,t,n){if(!this.metricsInstruments)return;let r=this.getMergedCustomLabels(n);Nd(this.metricsInstruments,e,t,r)}recordTeacherStudentMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);Dd(this.metricsInstruments,e,t,n,o)}recordCheckpointMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Gd(this.metricsInstruments,e,t,n,r,i)}recordParetoMetrics(e,t,n,r,o){if(!this.metricsInstruments)return;let i=this.getMergedCustomLabels(o);Ud(this.metricsInstruments,e,t,n,r,i)}recordPerformanceMetrics(e,t,n,r){if(!this.metricsInstruments)return;let o=this.getMergedCustomLabels(r);jd(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??le.optimizerLogger??Gr}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)}%.`,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 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 Yu=ar(require("crypto"),1);function ms(s){let e=new Date().toISOString();return{version:1,sections:{},stats:{bulletCount:0,helpfulCount:0,harmfulCount:0,tokenEstimate:0},updatedAt:e,description:s}}function kt(s){return JSON.parse(JSON.stringify(s))}function qd(s){return Math.ceil(s.length/4)}function Yi(s,e,t){let n=[],r=[],{maxSectionSize:o=Number.POSITIVE_INFINITY,allowDynamicSections:i=!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(!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=Kd(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:u}})}let m=p.bulletId??Hd(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 ea(s),s.updatedAt=u,{updatedBulletIds:n,autoRemoved:r}}function Xi(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(),ea(s);return}}}function en(s){let e=s.description?`## Context Playbook
789
917
  ${s.description.trim()}
790
918
  `:`## Context Playbook
791
919
  `,t=Object.entries(s.sections).map(([n,r])=>{let o=r.map(i=>`- [${i.id}] ${i.content}`).join(`
@@ -794,45 +922,45 @@ ${o}`:`### ${n}
794
922
  _(empty)_`}).join(`
795
923
 
796
924
  `);return`${e}
797
- ${t}`.trim()}function jd(s){let e=s.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=zu.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function Bd(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,l=i.harmfulCount??0,u=a-l*2,c=Date.parse(i.updatedAt??i.createdAt),p=[u,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),b=[f,m,Number.isFinite(x)?x:Number.POSITIVE_INFINITY];(p[0]<b[0]||p[0]===b[0]&&p[1]<b[1]||p[0]===b[0]&&p[1]===b[1]&&p[2]<b[2])&&(t=o,n=p)}if(t===-1)return;let[r]=s.splice(t,1);return r}function Vi(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(),l=r.get(a);l?(l.helpfulCount+=i.helpfulCount,l.harmfulCount+=i.harmfulCount,l.updatedAt=i.updatedAt):(r.set(a,i),o.push(i))}s.sections[t]=o}Ji(s)}function Ji(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+=Ud(i.content);s.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var zd={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Dr=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=bt(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(),"",Yt(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
925
+ ${t}`.trim()}function Hd(s){let e=s.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,6),t=Yu.default.randomBytes(4).toString("hex");return`${e||"ctx"}-${t}`}function Kd(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,l=i.harmfulCount??0,u=a-l*2,c=Date.parse(i.updatedAt??i.createdAt),p=[u,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),A=[f,m,Number.isFinite(x)?x:Number.POSITIVE_INFINITY];(p[0]<A[0]||p[0]===A[0]&&p[1]<A[1]||p[0]===A[0]&&p[1]===A[1]&&p[2]<A[2])&&(t=o,n=p)}if(t===-1)return;let[r]=s.splice(t,1);return r}function Zi(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(),l=r.get(a);l?(l.helpfulCount+=i.helpfulCount,l.harmfulCount+=i.harmfulCount,l.updatedAt=i.updatedAt):(r.set(a,i),o.push(i))}s.sections[t]=o}ea(s)}function ea(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+=qd(i.content);s.stats={bulletCount:e,helpfulCount:t,harmfulCount:n,tokenEstimate:r}}var Wd={maxEpochs:1,maxReflectorRounds:2,maxSectionSize:25,similarityThreshold:.95,allowDynamicSections:!0},Br=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=kt(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(),"",en(this.playbook)].filter(o=>o&&o.trim().length>0).join(`
798
926
 
799
- `);e.setDescription(r)}},Un=class extends pt{aceConfig;playbook;baseInstruction;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...zd,...t},this.playbook=t?.initialPlaybook!==void 0?bt(t.initialPlaybook):ls()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?bt(this.aceConfig.initialPlaybook):ls(),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?bt(t.playbook):this.aceConfig.initialPlaybook!==void 0?bt(this.aceConfig.initialPlaybook):ls(),this.generatorHistory=[...t?.artifact?.feedback??[]],this.deltaHistory=[...t?.artifact?.history??[]]}getPlaybook(){return bt(this.playbook)}getBaseInstruction(){return this.baseInstruction}getArtifact(){return{playbook:bt(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=bt(o.initialPlaybook)));let i=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.baseInstruction=a??l,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 b=0;b<p;b++)for(let h=0;h<t.length;h++){let y=t[h],I=this.composeInstruction(a??l,this.playbook);e.setDescription?.(I);let C=await e.forward(this.studentAI,y);this.stats.totalCalls+=1;let R=await n({prediction:C,example:y});typeof R=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,R),u=Math.max(u,R));let P=C?.severity,v=y?.severity,E=this.createGeneratorOutput(C,y),k=v&&P&&v!==P,O=await this.runReflectionRounds({example:y,generatorOutput:E,feedback:v&&P&&v!==P?`Expected severity "${v}" but model predicted "${P}".`:void 0}),L=await this.runCurator({program:e,example:y,reflection:O,playbook:this.playbook}),F=this.normalizeCuratorOperations(L?.operations);F.length===0&&k&&(F=this.inferOperationsFromReflection(O)),F=this.resolveCuratorOperationTargets(F,this.playbook,O,E);let M=L||F.length>0?{...L??{},operations:F}:void 0,_=[];if(F.length>0){let G=this.collectProtectedBulletIds(F),me=Ki(this.playbook,F,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:G});_=me.updatedBulletIds,me.autoRemoved.length>0&&(F.push(...me.autoRemoved),M&&(M.operations=F))}if(O?.bulletTags)for(let G of O.bulletTags)Wi(this.playbook,G.id,G.tag);F.length>0&&_.length>0&&Vi(this.playbook,this.aceConfig.similarityThreshold);let j={example:y,prediction:C,score:typeof R=="number"?R:0,generatorOutput:E,reflection:O,curator:M,timestamp:new Date().toISOString()};this.generatorHistory.push(j),_.length>0&&M?.operations?.length&&this.deltaHistory.push({epoch:b,exampleIndex:h,operations:M.operations}),c+=1,this.currentRound=c;let H=typeof R=="number"&&Number.isFinite(R)?R:0,D=Number.isFinite(u)?u:H,Y={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,H,{epoch:b,exampleIndex:h,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},D,{playbookBullets:this.playbook.stats.bulletCount},void 0,Y),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,H)}}finally{e.setDescription?.(l)}let m=Date.now()-i;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(u)?u:0;let g={playbook:bt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},f=new Dr({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:f,playbook:bt(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),l=r&&n&&r!==n;a.length===0&&l&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let u=i||a.length>0?{...i??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)Wi(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=Ki(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)),Vi(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(),"",Yt(t)].filter(r=>r.trim().length>0).join(`
927
+ `);e.setDescription(r)}},zn=class extends At{aceConfig;playbook;baseInstruction;generatorHistory=[];deltaHistory=[];reflectorProgram;curatorProgram;program;constructor(e,t){super(e),this.aceConfig={...Wd,...t},this.playbook=t?.initialPlaybook!==void 0?kt(t.initialPlaybook):ms()}reset(){super.reset(),this.playbook=this.aceConfig.initialPlaybook!==void 0?kt(this.aceConfig.initialPlaybook):ms(),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?kt(t.playbook):this.aceConfig.initialPlaybook!==void 0?kt(this.aceConfig.initialPlaybook):ms(),this.generatorHistory=[...t?.artifact?.feedback??[]],this.deltaHistory=[...t?.artifact?.history??[]]}getPlaybook(){return kt(this.playbook)}getBaseInstruction(){return this.baseInstruction}getArtifact(){return{playbook:kt(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=kt(o.initialPlaybook)));let i=Date.now();this.validateExamples(t),this.program=e;let a=await this.extractProgramInstruction(e),l=e.getSignature().getDescription()??"";this.baseInstruction=a??l,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 A=0;A<p;A++)for(let h=0;h<t.length;h++){let b=t[h],I=this.composeInstruction(a??l,this.playbook);e.setDescription?.(I);let C=await e.forward(this.studentAI,b);this.stats.totalCalls+=1;let R=await n({prediction:C,example:b});typeof R=="number"&&(this.stats.bestScore=Math.max(this.stats.bestScore,R),u=Math.max(u,R));let M=C?.severity,S=b?.severity,O=this.createGeneratorOutput(C,b),k=S&&M&&S!==M,_=await this.runReflectionRounds({example:b,generatorOutput:O,feedback:S&&M&&S!==M?`Expected severity "${S}" but model predicted "${M}".`:void 0}),L=await this.runCurator({program:e,example:b,reflection:_,playbook:this.playbook}),E=this.normalizeCuratorOperations(L?.operations);E.length===0&&k&&(E=this.inferOperationsFromReflection(_)),E=this.resolveCuratorOperationTargets(E,this.playbook,_,O);let P=L||E.length>0?{...L??{},operations:E}:void 0,$=[];if(E.length>0){let B=this.collectProtectedBulletIds(E),ge=Yi(this.playbook,E,{maxSectionSize:this.aceConfig.maxSectionSize,allowDynamicSections:this.aceConfig.allowDynamicSections,enableAutoPrune:!0,protectedBulletIds:B});$=ge.updatedBulletIds,ge.autoRemoved.length>0&&(E.push(...ge.autoRemoved),P&&(P.operations=E))}if(_?.bulletTags)for(let B of _.bulletTags)Xi(this.playbook,B.id,B.tag);E.length>0&&$.length>0&&Zi(this.playbook,this.aceConfig.similarityThreshold);let D={example:b,prediction:C,score:typeof R=="number"?R:0,generatorOutput:O,reflection:_,curator:P,timestamp:new Date().toISOString()};this.generatorHistory.push(D),$.length>0&&P?.operations?.length&&this.deltaHistory.push({epoch:A,exampleIndex:h,operations:P.operations}),c+=1,this.currentRound=c;let z=typeof R=="number"&&Number.isFinite(R)?R:0,U=Number.isFinite(u)?u:z,te={...r??{},maxIterations:d};await this.updateOptimizationProgress(c,z,{epoch:A,exampleIndex:h,playbookBullets:this.playbook.stats.bulletCount},"ACE",{epochs:p,totalRounds:d},U,{playbookBullets:this.playbook.stats.bulletCount},void 0,te),this.stats.convergenceInfo.finalImprovement=Math.max(this.stats.convergenceInfo.finalImprovement,z)}}finally{e.setDescription?.(l)}let m=Date.now()-i;this.stats.resourceUsage.totalTime=m,this.stats.convergenceInfo.converged=!0,this.stats.bestScore=Number.isFinite(u)?u:0;let g={playbook:kt(this.playbook),feedback:[...this.generatorHistory],history:[...this.deltaHistory]},f=new Br({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:f,playbook:kt(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),l=r&&n&&r!==n;a.length===0&&l&&(a=this.inferOperationsFromReflection(o)),a=this.resolveCuratorOperationTargets(a,this.playbook,o,t);let u=i||a.length>0?{...i??{},operations:a}:void 0;if(o?.bulletTags)for(let p of o.bulletTags)Xi(this.playbook,p.id,p.tag);if(a.length>0){let p=this.collectProtectedBulletIds(a),d=Yi(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)),Zi(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(),"",en(t)].filter(r=>r.trim().length>0).join(`
800
928
 
801
- `)}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,l=(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";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,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=u(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",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,f=[a,u,p,m??""].join(":");if(n.has(f))continue;n.add(f);let x={type:a,section:u};a!=="REMOVE"&&(x.content=p),m&&(x.bulletId=m);let b=r.metadata;b&&typeof b=="object"&&(x.metadata={...b}),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 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 i=0;i<r;i++){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(),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:Yt(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,u=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:Yt(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=pe().input("question",pe.string("Original task input serialized as JSON")).input("generator_answer",pe.string("Generator output serialized as JSON")).input("generator_reasoning",pe.string("Generator reasoning trace").optional()).input("playbook",pe.string("Current context playbook rendered as markdown")).input("expected_answer",pe.string("Expected output when ground truth is available").optional()).input("feedback",pe.string("External feedback or reward signal").optional()).input("previous_reflection",pe.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",pe.string("Step-by-step analysis of generator performance")).output("errorIdentification",pe.string("Specific mistakes detected")).output("rootCauseAnalysis",pe.string("Underlying cause of the error")).output("correctApproach",pe.string("What the generator should do differently")).output("keyInsight",pe.string("Reusable insight to remember")).output("bulletTags",pe.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=et(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=pe().input("playbook",pe.string("Current playbook serialized as JSON")).input("reflection",pe.string("Latest reflection output serialized as JSON")).input("question_context",pe.string("Original task input serialized as JSON")).input("token_budget",pe.number("Approximate token budget for curator response").optional()).output("reasoning",pe.string("Justification for the proposed updates")).output("operations",pe.json("List of operations with type/section/content fields")).build();this.curatorProgram=et(e)}return this.curatorProgram}};function qu(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,l=e[i]??0;if(a+t<l){r=!1;break}a>l+t&&(o=!0)}return r&&o}function Gr(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(qu(s[i].scores,s[n].scores,e)){o=!0;break}qu(s[n].scores,s[i].scores,e)&&r++}o||t.push({idx:s[n].idx,scores:s[n].scores,dominated:r})}return t}function jn(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,l)=>(l[t]??0)-(a[t]??0)),o=0,i=0;for(let a of r){let l=a[t]??0,u=a[n]??0,c=Math.max(u-i,0);o+=l*c,i=Math.max(i,u)}return o}function Qi(s){if(s.length===0)return 0;let e=0;for(let t of s)e+=t;return e/s.length}function Hu(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 Yi(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 l=r.filter(c=>!o.has(c)),u=new Set(l);return s.map(c=>{let p=new Set;for(let d of c)u.has(d)&&p.add(d);return p})}function Ku(s,e,t){let n=Yi(s,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 i=typeof t=="function"?t():Math.random(),a=Math.floor(i*o.length);return o[a]}function qd(s){return s.size===0?{score:0}:Object.fromEntries([...s].map(e=>[e,0]))}var Ut=class s extends pt{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 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 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),l=r?.validationExamples,u=r?.feedbackExamples,c=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),p=U=>{let K=Object.keys(U).sort().reduce((W,re)=>(W[re]=U[re],W),{});return JSON.stringify(K)},d=new Set(t.map(U=>p(U))),m=u&&u.length>0?u.filter(U=>d.has(p(U))):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(U=>U.id),b=U=>{for(let K of f){let W=U[K.id];typeof W=="string"&&K.program.setInstruction?.(W)}},h=async(U,K)=>{let W=await n({prediction:U,example:K});if(typeof W=="number")return Number.isFinite(W)?{score:W}:{};if(!W||typeof W!="object")return{};let re={};for(let[Q,le]of Object.entries(W))typeof le=="number"&&Number.isFinite(le)&&(re[Q]=le);return re},y=U=>{let K=r?.paretoMetricKey,W=r?.paretoScalarize;if(typeof W=="function")return W(U);if(K)return Number.isFinite(U[K])?U[K]:0;let re=Object.values(U);return re.length?re.reduce((Q,le)=>Q+le,0)/re.length:0},I=this.getOptimizerLogger(r),C=r?.verbose??this.verbose?U=>console.log(`[GEPA] ${U}`):U=>{},R=new Set,P=async(U,K,W,re=!1)=>{let Q=K.length;if(this.stats.totalCalls+Q>a){if(re)throw new Error(`AxGEPA: options.maxMetricCalls=${a} is too small to evaluate the initial Pareto set; need at least ${Q} metric calls`);return}let le=[];C(`${W}: evaluating ${K.length} example${K.length===1?"":"s"}`);for(let[Ie,Ee]of K.entries()){b(U);let Me,oe;try{Me=await e.forward(this.studentAI,Ee,{sampleCount:this.sampleCount}),oe=await h(Me,Ee);for(let q of Object.keys(oe))R.add(q)}catch(q){let ce=q instanceof Error?q.message:String(q);Me={error:ce},oe=qd(R),C(`Evaluation failed during ${W}; scoring this example as zero. Error: ${ce}`)}this.stats.totalCalls+=1;let Re=y(oe);le.push({input:Ee,prediction:Me,scores:oe,scalar:Re}),C(`${W}: completed ${Ie+1}/${K.length} (score=${Re.toFixed(3)})`)}return{rows:le,avg:Hu(le.map(Ie=>Ie.scores)),scalars:le.map(Ie=>Ie.scalar),sum:le.reduce((Ie,Ee)=>Ie+Ee.scalar,0)}},v={};for(let U of f)v[U.id]=await this.getBaseInstruction(U.program);let E=await P(v,c,"initial Pareto evaluation",!0),k=[{cfg:{...v},parent:void 0,scores:E.avg}],O=[E.scalars];I?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax,tunableCount:f.length}}}),C(`Starting GEPA optimization: ${t.length} train, ${c.length} validation, maxCalls=${a}`);let L=0,F=new Set,M=Gr(k.map((U,K)=>({idx:K,scores:U.scores})),this.tieEpsilon).map(U=>U.idx),_;for(let U=0;U<this.numTrials&&!(a!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(a)));U++){let K=O[0]?.length??0,W=[];for(let N=0;N<K;N++){let V=Number.NEGATIVE_INFINITY,ne=new Set;for(let Te=0;Te<O.length;Te++){let ze=O[Te][N];ze>V+this.tieEpsilon?(V=ze,ne.clear(),ne.add(Te)):Math.abs(ze-V)<=this.tieEpsilon&&ne.add(Te)}W.push(ne)}let re=O.map(N=>Qi(N));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let N=w=>{let S=[],T=w;for(;T!==void 0;)S.push(T),T=k[T]?.parent;return S},V=w=>w.length?w[Math.floor(this.rand()*w.length)]:void 0,ne=Yi(W,re),Te=new Set;for(let w of ne)for(let S of w)Te.add(S);let ze=Array.from(Te),A;for(let w=0;w<10&&!A&&!(ze.length<2);w++){let S=V(ze),T=V(ze);if(S===T)continue;T<S&&([S,T]=[T,S]);let $=new Set(N(S)),z=new Set(N(T));if($.has(T)||z.has(S))continue;let J=[...$].filter(se=>z.has(se));if(J.length===0)continue;let B=[];for(let se of J){let Ce=k[se].cfg,Ne=k[S].cfg,Oe=k[T].cfg,vt=!1,Ct=new Set([...Object.keys(Ce),...Object.keys(Ne),...Object.keys(Oe)]);for(let Pt of Ct){let Z=Ce[Pt],te=Ne[Pt],de=Oe[Pt];if(te===Z&&de!==te||de===Z&&te!==de){vt=!0;break}}vt&&B.push(se)}if(B.length===0)continue;let X=B.map(se=>Math.max(1e-9,re[se])),xe=this.rand()*X.reduce((se,Ce)=>se+Ce,0),_e=B[B.length-1];for(let se=0;se<B.length;se++){if(xe<X[se]){_e=B[se];break}xe-=X[se]}A={i:S,j:T,a:_e}}if(this.lastIterFoundNewProgram=!1,A){let w=!1,{i:S,j:T,a:$}=A,z=re[$],J=re[S],B=re[T];if(z>Math.min(J,B))continue;let X=`${S}|${T}|${$}`;if(this.mergeAttemptKeys.has(X)||(this.mergeAttemptKeys.add(X),F.has(X)))continue;let{cfg:xe,descSig:_e}=this.systemAwareMergeWithSig(k,S,T,(ye,We)=>re[ye]>=re[We]?ye:We),se=`${Math.min(S,T)}|${Math.max(S,T)}|${_e}`;if(this.mergeCompositionKeys.has(se))continue;this.mergeCompositionKeys.add(se);let Ce=O[S],Ne=O[T],Oe=Array.from({length:Ce.length},(ye,We)=>We),vt=Oe.filter(ye=>(Ce[ye]??0)>(Ne[ye]??0)),Ct=Oe.filter(ye=>(Ne[ye]??0)>(Ce[ye]??0)),Pt=Oe.filter(ye=>!(vt.includes(ye)||Ct.includes(ye))),Z=5,te=Math.ceil(Z/3),de=(ye,We)=>{if(We<=0||ye.length===0)return[];if(ye.length<=We)return[...ye];let rr=[],Vr=new Set;for(;rr.length<We;){let gt=Math.floor(this.rand()*ye.length);Vr.has(gt)||(Vr.add(gt),rr.push(ye[gt]))}return rr},Le=[];Le.push(...de(vt,Math.min(te,vt.length))),Le.push(...de(Ct,Math.min(te,Ct.length)));let Ue=Z-Le.length;Le.push(...de(Pt,Math.max(0,Ue)));let jt=Z-Le.length;if(jt>0){let ye=Oe.filter(We=>!Le.includes(We));Le.push(...de(ye,Math.min(jt,ye.length)))}let Ps=Le.slice(0,Math.min(Z,Oe.length)),ep=Ps.map(ye=>c[ye]),ba=await P(xe,ep,"merge subsample");if(!ba)break;let tp=ba.sum,np=Ps.reduce((ye,We)=>ye+(Ce[We]??0),0),rp=Ps.reduce((ye,We)=>ye+(Ne[We]??0),0);if(tp>=Math.max(np,rp)+this.minImprovementThreshold){C(`Iteration ${U+1}: Merge accepted (programs ${S} + ${T} via ancestor ${$})`);let ye=await P(xe,c,"merge validation");if(!ye)break;k.push({cfg:{...xe},parent:$,scores:ye.avg}),O.push(ye.scalars);let We=M.length,rr=jn(M.map(gt=>k[gt].scores))??0;M=Gr(k.map((gt,op)=>({idx:op,scores:gt.scores})),this.tieEpsilon).map(gt=>gt.idx);let Vr=jn(M.map(gt=>k[gt].scores))??0;(M.length>We||Vr>rr+1e-6)&&(L=0),this.mergesDue-=1,this.totalMergesTested+=1,F.add(X),w=!0}if(w)continue}}let Q=Ku(W,re,()=>this.rand());this.lastIterFoundNewProgram=!1;let le=this.minibatch?this.nextMinibatchIndices(g.length,U).map(N=>g[N]):g,Ie=await P(k[Q].cfg,le,"parent minibatch");if(!Ie)break;if(r?.skipPerfectScore??!0){let N=Number(r?.perfectScore??1);if(Ie.scalars.length>0&&Ie.scalars.every(V=>V>=N))continue}let Ee={...k[Q].cfg},Me="reflective_mutation",oe,Re,q=f[U%f.length],ce=k[Q].cfg[q.id],fe=r?.gepaAdapter,he,we=Ie.rows.map(N=>({input:N.input,prediction:N.prediction,score:N.scalar}));if(fe)try{let N=await fe.evaluate(le,{...k[Q].cfg},!0);oe=Array.isArray(N?.scores)?N.scores.reduce((ze,A)=>ze+(Number(A)||0),0):void 0;let V=fe.make_reflective_dataset({...k[Q].cfg},N,[q.id]),ne=await fe.propose_new_texts?.({...k[Q].cfg},V,[q.id]),Te=ne?.[q.id]??(ne?Object.values(ne)[0]:void 0);typeof Te=="string"&&Te.length>0&&(he=Te)}catch{}if(he||(he=await this.reflectTargetInstruction(q.id,ce,e,b,{...k[Q].cfg},le,async({prediction:N,example:V})=>y(await h(N,V)),r,we)),Ee[q.id]=he,fe&&oe!==void 0)try{let N=await fe.evaluate(le,Ee,!1);Re=Array.isArray(N?.scores)?N.scores.reduce((V,ne)=>V+(Number(ne)||0),0):void 0}catch{}let ke=await P(Ee,le,"child minibatch");if(!ke)break;if(this.currentRound=U+1,await this.updateOptimizationProgress(this.currentRound,ke.sum,{instructionLen:he.length,target:q.id,parent:Q,totalRounds:this.numTrials},"GEPA",{strategy:Me,paretoSetSize:c.length,tunableCount:f.length},ke.sum,{instructionLen:ce.length,idx:Q},{...r??{},maxIterations:this.numTrials}),!(ke.sum>Ie.sum+this.minImprovementThreshold&&(oe===void 0||Re===void 0||Re>oe+this.minImprovementThreshold))){if(C(`Iteration ${U+1}: Rejected (child=${ke.sum.toFixed(3)} <= parent=${Ie.sum.toFixed(3)})`),++L>=this.earlyStoppingTrials){C(`Early stopping: ${L} iterations without improvement`);break}continue}C(`Iteration ${U+1}: Accepted (child=${ke.sum.toFixed(3)} > parent=${Ie.sum.toFixed(3)})`);let Ye=await P(Ee,c,"validation evaluation");if(!Ye)break;k.push({cfg:{...Ee},parent:Q,scores:Ye.avg}),O.push(Ye.scalars);let rt=M.length,ve=jn(M.map(N=>k[N].scores))??0;M=Gr(k.map((N,V)=>({idx:V,scores:N.scores})),this.tieEpsilon).map(N=>N.idx);let Ke=jn(M.map(N=>k[N].scores))??0;if(M.length>rt||Ke>ve+1e-6)L=0,C(`Iteration ${U+1}: Archive improved (size=${M.length}, hv=${Ke.toFixed(4)})`);else if(L++,C(`Iteration ${U+1}: Archive unchanged (stagnation=${L}/${this.earlyStoppingTrials})`),L>=this.earlyStoppingTrials){C(`Early stopping: ${L} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let j=Gr(k.map((U,K)=>({idx:K,scores:U.scores})),this.tieEpsilon),H=j.length>0?Math.max(...j.map(U=>y(U.scores))):0,D;if(j.length>0){let U=Number.NEGATIVE_INFINITY;for(let K of j){let W=y(K.scores);W>U&&(U=W,D=K.idx)}}let Y=jn(j.map(U=>U.scores));this.stats.convergenceInfo.converged=!0;let G=this.getMergedCustomLabels(r);this.recordParetoMetrics(j.length,k.length,"GEPA",Y,G);let me=Date.now()-o,ge=typeof D=="number"?new kt({bestScore:H,stats:this.stats,instruction:f.length===1?k[D].cfg[x[0]]:void 0,instructionMap:{...k[D].cfg},demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:me,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,Ae=this.generateOptimizationReport(j,Y,H,k.length);return{demos:[],stats:this.stats,bestScore:H,paretoFront:j.map(U=>({demos:[],scores:U.scores,configuration:{candidate:U.idx,instructionMap:{...k[U.idx].cfg},...f.length===1?{instruction:k[U.idx].cfg[x[0]]}:{}},dominatedSolutions:U.dominated})),paretoFrontSize:j.length,hypervolume:Y,finalConfiguration:{strategy:"gepa",candidates:k.length,tunables:f.length},optimizedProgram:ge,report:Ae}}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?Qi(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,l,u){let c=u?[...u]:[];if(c.length===0)for(let h of i)try{o[e]=t,r(o);let y=await n.forward(this.studentAI,h,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let I=await a({prediction:y,example:h});c.push({input:h,prediction:y,score:typeof I=="number"?I:0})}catch{c.push({input:h,prediction:{},score:0})}let p=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=et('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=[...(l?.feedbackNotes??[]).filter(h=>typeof h=="string"&&h.trim().length>0)],f=l?.feedbackFn;if(typeof f=="function")for(let h of c){let y=f({prediction:h.prediction,example:h.input});y&&(Array.isArray(y)?g.push(...y):g.push(y))}let x="";try{x=(await d.forward(p,{targetId:e,minibatch:c,evalFeedback:g}))?.feedbackSummary?.trim()||""}catch{}let b=et('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 y=(await b.forward(p,{targetId:e,currentInstruction:t,feedbackSummary:x,minibatch:c}))?.newInstruction?.trim();if(y&&y.length>16)return y}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 l=o?.overrideTeacherAI??this.teacherAI??this.studentAI,u=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 b=a[x],h=`# Example ${x+1}
929
+ `)}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,l=(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";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,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=u(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",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,f=[a,u,p,m??""].join(":");if(n.has(f))continue;n.add(f);let x={type:a,section:u};a!=="REMOVE"&&(x.content=p),m&&(x.bulletId=m);let A=r.metadata;A&&typeof A=="object"&&(x.metadata={...A}),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 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 i=0;i<r;i++){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(),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:en(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,u=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:en(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=ae().input("question",ae.string("Original task input serialized as JSON")).input("generator_answer",ae.string("Generator output serialized as JSON")).input("generator_reasoning",ae.string("Generator reasoning trace").optional()).input("playbook",ae.string("Current context playbook rendered as markdown")).input("expected_answer",ae.string("Expected output when ground truth is available").optional()).input("feedback",ae.string("External feedback or reward signal").optional()).input("previous_reflection",ae.string("Most recent reflection JSON when running multi-round refinement").optional()).output("reasoning",ae.string("Step-by-step analysis of generator performance")).output("errorIdentification",ae.string("Specific mistakes detected")).output("rootCauseAnalysis",ae.string("Underlying cause of the error")).output("correctApproach",ae.string("What the generator should do differently")).output("keyInsight",ae.string("Reusable insight to remember")).output("bulletTags",ae.json("Array of {id, tag} entries referencing playbook bullets")).build();this.reflectorProgram=nt(e)}return this.reflectorProgram}getOrCreateCuratorProgram(){if(!this.curatorProgram){let e=ae().input("playbook",ae.string("Current playbook serialized as JSON")).input("reflection",ae.string("Latest reflection output serialized as JSON")).input("question_context",ae.string("Original task input serialized as JSON")).input("token_budget",ae.number("Approximate token budget for curator response").optional()).output("reasoning",ae.string("Justification for the proposed updates")).output("operations",ae.json("List of operations with type/section/content fields")).build();this.curatorProgram=nt(e)}return this.curatorProgram}};function Xu(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,l=e[i]??0;if(a+t<l){r=!1;break}a>l+t&&(o=!0)}return r&&o}function jr(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(Xu(s[i].scores,s[n].scores,e)){o=!0;break}Xu(s[n].scores,s[i].scores,e)&&r++}o||t.push({idx:s[n].idx,scores:s[n].scores,dominated:r})}return t}function qn(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,l)=>(l[t]??0)-(a[t]??0)),o=0,i=0;for(let a of r){let l=a[t]??0,u=a[n]??0,c=Math.max(u-i,0);o+=l*c,i=Math.max(i,u)}return o}function ta(s){if(s.length===0)return 0;let e=0;for(let t of s)e+=t;return e/s.length}function Zu(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 na(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 l=r.filter(c=>!o.has(c)),u=new Set(l);return s.map(c=>{let p=new Set;for(let d of c)u.has(d)&&p.add(d);return p})}function ec(s,e,t){let n=na(s,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 i=typeof t=="function"?t():Math.random(),a=Math.floor(i*o.length);return o[a]}function Vd(s){return s.size===0?{score:0}:Object.fromEntries([...s].map(e=>[e,0]))}var jt=class s extends At{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 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 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),l=r?.validationExamples,u=r?.feedbackExamples,c=(l&&l.length>0?l:t).slice(0,this.paretoSetSize),p=N=>{let q=Object.keys(N).sort().reduce((W,he)=>(W[he]=N[he],W),{});return JSON.stringify(q)},d=new Set(t.map(N=>p(N))),m=u&&u.length>0?u.filter(N=>d.has(p(N))):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(N=>N.id),A=N=>{for(let q of f){let W=N[q.id];typeof W=="string"&&q.program.setInstruction?.(W)}},h=async(N,q)=>{let W=await n({prediction:N,example:q});if(typeof W=="number")return Number.isFinite(W)?{score:W}:{};if(!W||typeof W!="object")return{};let he={};for(let[ee,ie]of Object.entries(W))typeof ie=="number"&&Number.isFinite(ie)&&(he[ee]=ie);return he},b=N=>{let q=r?.paretoMetricKey,W=r?.paretoScalarize;if(typeof W=="function")return W(N);if(q)return Number.isFinite(N[q])?N[q]:0;let he=Object.values(N);return he.length?he.reduce((ee,ie)=>ee+ie,0)/he.length:0},I=this.getOptimizerLogger(r),C=r?.verbose??this.verbose?N=>console.log(`[GEPA] ${N}`):N=>{},R=new Set,M=async(N,q,W,he=!1)=>{let ee=q.length;if(this.stats.totalCalls+ee>a){if(he)throw new Error(`AxGEPA: options.maxMetricCalls=${a} is too small to evaluate the initial Pareto set; need at least ${ee} metric calls`);return}let ie=[];C(`${W}: evaluating ${q.length} example${q.length===1?"":"s"}`);for(let[fe,Pe]of q.entries()){A(N);let Ne,oe;try{Ne=await e.forward(this.studentAI,Pe,{sampleCount:this.sampleCount}),oe=await h(Ne,Pe);for(let H of Object.keys(oe))R.add(H)}catch(H){let re=H instanceof Error?H.message:String(H);Ne={error:re},oe=Vd(R),C(`Evaluation failed during ${W}; scoring this example as zero. Error: ${re}`)}this.stats.totalCalls+=1;let ve=b(oe);ie.push({input:Pe,prediction:Ne,scores:oe,scalar:ve}),C(`${W}: completed ${fe+1}/${q.length} (score=${ve.toFixed(3)})`)}return{rows:ie,avg:Zu(ie.map(fe=>fe.scores)),scalars:ie.map(fe=>fe.scalar),sum:ie.reduce((fe,Pe)=>fe+Pe.scalar,0)}},S={};for(let N of f)S[N.id]=await this.getBaseInstruction(N.program);let O=await M(S,c,"initial Pareto evaluation",!0),k=[{cfg:{...S},parent:void 0,scores:O.avg}],_=[O.scalars];I?.({name:"OptimizationStart",value:{optimizerType:"GEPA",exampleCount:t.length,validationCount:c.length,config:{numTrials:this.numTrials,minibatch:this.minibatch,mergeMax:this.mergeMax,tunableCount:f.length}}}),C(`Starting GEPA optimization: ${t.length} train, ${c.length} validation, maxCalls=${a}`);let L=0,E=new Set,P=jr(k.map((N,q)=>({idx:q,scores:N.scores})),this.tieEpsilon).map(N=>N.idx),$;for(let N=0;N<this.numTrials&&!(a!==void 0&&this.stats.totalCalls>=Math.max(1,Math.floor(a)));N++){let q=_[0]?.length??0,W=[];for(let se=0;se<q;se++){let Le=Number.NEGATIVE_INFINITY,He=new Set;for(let Ve=0;Ve<_.length;Ve++){let ot=_[Ve][se];ot>Le+this.tieEpsilon?(Le=ot,He.clear(),He.add(Ve)):Math.abs(ot-Le)<=this.tieEpsilon&&He.add(Ve)}W.push(He)}let he=_.map(se=>ta(se));if(this.mergeMax>0&&this.mergesDue>0&&this.lastIterFoundNewProgram){let se=T=>{let v=[],w=T;for(;w!==void 0;)v.push(w),w=k[w]?.parent;return v},Le=T=>T.length?T[Math.floor(this.rand()*T.length)]:void 0,He=na(W,he),Ve=new Set;for(let T of He)for(let v of T)Ve.add(v);let ot=Array.from(Ve),y;for(let T=0;T<10&&!y&&!(ot.length<2);T++){let v=Le(ot),w=Le(ot);if(v===w)continue;w<v&&([v,w]=[w,v]);let G=new Set(se(v)),K=new Set(se(w));if(G.has(w)||K.has(v))continue;let Q=[...G].filter(Z=>K.has(Z));if(Q.length===0)continue;let V=[];for(let Z of Q){let xe=k[Z].cfg,$e=k[v].cfg,me=k[w].cfg,je=!1,Me=new Set([...Object.keys(xe),...Object.keys($e),...Object.keys(me)]);for(let Ze of Me){let Y=xe[Ze],J=$e[Ze],ce=me[Ze];if(J===Y&&ce!==J||ce===Y&&J!==ce){je=!0;break}}je&&V.push(Z)}if(V.length===0)continue;let F=V.map(Z=>Math.max(1e-9,he[Z])),j=this.rand()*F.reduce((Z,xe)=>Z+xe,0),X=V[V.length-1];for(let Z=0;Z<V.length;Z++){if(j<F[Z]){X=V[Z];break}j-=F[Z]}y={i:v,j:w,a:X}}if(this.lastIterFoundNewProgram=!1,y){let T=!1,{i:v,j:w,a:G}=y,K=he[G],Q=he[v],V=he[w];if(K>Math.min(Q,V))continue;let F=`${v}|${w}|${G}`;if(this.mergeAttemptKeys.has(F)||(this.mergeAttemptKeys.add(F),E.has(F)))continue;let{cfg:j,descSig:X}=this.systemAwareMergeWithSig(k,v,w,(Ae,Je)=>he[Ae]>=he[Je]?Ae:Je),Z=`${Math.min(v,w)}|${Math.max(v,w)}|${X}`;if(this.mergeCompositionKeys.has(Z))continue;this.mergeCompositionKeys.add(Z);let xe=_[v],$e=_[w],me=Array.from({length:xe.length},(Ae,Je)=>Je),je=me.filter(Ae=>(xe[Ae]??0)>($e[Ae]??0)),Me=me.filter(Ae=>($e[Ae]??0)>(xe[Ae]??0)),Ze=me.filter(Ae=>!(je.includes(Ae)||Me.includes(Ae))),Y=5,J=Math.ceil(Y/3),ce=(Ae,Je)=>{if(Je<=0||Ae.length===0)return[];if(Ae.length<=Je)return[...Ae];let ir=[],Zr=new Set;for(;ir.length<Je;){let bt=Math.floor(this.rand()*Ae.length);Zr.has(bt)||(Zr.add(bt),ir.push(Ae[bt]))}return ir},Ie=[];Ie.push(...ce(je,Math.min(J,je.length))),Ie.push(...ce(Me,Math.min(J,Me.length)));let Re=Y-Ie.length;Ie.push(...ce(Ze,Math.max(0,Re)));let st=Y-Ie.length;if(st>0){let Ae=me.filter(Je=>!Ie.includes(Je));Ie.push(...ce(Ae,Math.min(st,Ae.length)))}let sr=Ie.slice(0,Math.min(Y,me.length)),$s=sr.map(Ae=>c[Ae]),zt=await M(j,$s,"merge subsample");if(!zt)break;let qt=zt.sum,Xr=sr.reduce((Ae,Je)=>Ae+(xe[Je]??0),0),va=sr.reduce((Ae,Je)=>Ae+($e[Je]??0),0);if(qt>=Math.max(Xr,va)+this.minImprovementThreshold){C(`Iteration ${N+1}: Merge accepted (programs ${v} + ${w} via ancestor ${G})`);let Ae=await M(j,c,"merge validation");if(!Ae)break;k.push({cfg:{...j},parent:G,scores:Ae.avg}),_.push(Ae.scalars);let Je=P.length,ir=qn(P.map(bt=>k[bt].scores))??0;P=jr(k.map((bt,lp)=>({idx:lp,scores:bt.scores})),this.tieEpsilon).map(bt=>bt.idx);let Zr=qn(P.map(bt=>k[bt].scores))??0;(P.length>Je||Zr>ir+1e-6)&&(L=0),this.mergesDue-=1,this.totalMergesTested+=1,E.add(F),T=!0}if(T)continue}}let ee=ec(W,he,()=>this.rand());this.lastIterFoundNewProgram=!1;let ie=this.minibatch?this.nextMinibatchIndices(g.length,N).map(se=>g[se]):g,fe=await M(k[ee].cfg,ie,"parent minibatch");if(!fe)break;if(r?.skipPerfectScore??!0){let se=Number(r?.perfectScore??1);if(fe.scalars.length>0&&fe.scalars.every(Le=>Le>=se))continue}let Pe={...k[ee].cfg},Ne="reflective_mutation",oe,ve,H=f[N%f.length],re=k[ee].cfg[H.id],de=r?.gepaAdapter,ye,we=fe.rows.map(se=>({input:se.input,prediction:se.prediction,score:se.scalar}));if(de)try{let se=await de.evaluate(ie,{...k[ee].cfg},!0);oe=Array.isArray(se?.scores)?se.scores.reduce((ot,y)=>ot+(Number(y)||0),0):void 0;let Le=de.make_reflective_dataset({...k[ee].cfg},se,[H.id]),He=await de.propose_new_texts?.({...k[ee].cfg},Le,[H.id]),Ve=He?.[H.id]??(He?Object.values(He)[0]:void 0);typeof Ve=="string"&&Ve.length>0&&(ye=Ve)}catch{}if(ye||(ye=await this.reflectTargetInstruction(H.id,re,e,A,{...k[ee].cfg},ie,async({prediction:se,example:Le})=>b(await h(se,Le)),r,we)),Pe[H.id]=ye,de&&oe!==void 0)try{let se=await de.evaluate(ie,Pe,!1);ve=Array.isArray(se?.scores)?se.scores.reduce((Le,He)=>Le+(Number(He)||0),0):void 0}catch{}let Oe=await M(Pe,ie,"child minibatch");if(!Oe)break;if(this.currentRound=N+1,await this.updateOptimizationProgress(this.currentRound,Oe.sum,{instructionLen:ye.length,target:H.id,parent:ee,totalRounds:this.numTrials},"GEPA",{strategy:Ne,paretoSetSize:c.length,tunableCount:f.length},Oe.sum,{instructionLen:re.length,idx:ee},{...r??{},maxIterations:this.numTrials}),!(Oe.sum>fe.sum+this.minImprovementThreshold&&(oe===void 0||ve===void 0||ve>oe+this.minImprovementThreshold))){if(C(`Iteration ${N+1}: Rejected (child=${Oe.sum.toFixed(3)} <= parent=${fe.sum.toFixed(3)})`),++L>=this.earlyStoppingTrials){C(`Early stopping: ${L} iterations without improvement`);break}continue}C(`Iteration ${N+1}: Accepted (child=${Oe.sum.toFixed(3)} > parent=${fe.sum.toFixed(3)})`);let Ue=await M(Pe,c,"validation evaluation");if(!Ue)break;k.push({cfg:{...Pe},parent:ee,scores:Ue.avg}),_.push(Ue.scalars);let rt=P.length,Te=qn(P.map(se=>k[se].scores))??0;P=jr(k.map((se,Le)=>({idx:Le,scores:se.scores})),this.tieEpsilon).map(se=>se.idx);let Be=qn(P.map(se=>k[se].scores))??0;if(P.length>rt||Be>Te+1e-6)L=0,C(`Iteration ${N+1}: Archive improved (size=${P.length}, hv=${Be.toFixed(4)})`);else if(L++,C(`Iteration ${N+1}: Archive unchanged (stagnation=${L}/${this.earlyStoppingTrials})`),L>=this.earlyStoppingTrials){C(`Early stopping: ${L} iterations without archive improvement`);break}this.lastIterFoundNewProgram=!0,this.mergeMax>0&&this.totalMergesTested<this.mergeMax&&(this.mergesDue+=1)}let D=jr(k.map((N,q)=>({idx:q,scores:N.scores})),this.tieEpsilon),z=D.length>0?Math.max(...D.map(N=>b(N.scores))):0,U;if(D.length>0){let N=Number.NEGATIVE_INFINITY;for(let q of D){let W=b(q.scores);W>N&&(N=W,U=q.idx)}}let te=qn(D.map(N=>N.scores));this.stats.convergenceInfo.converged=!0;let B=this.getMergedCustomLabels(r);this.recordParetoMetrics(D.length,k.length,"GEPA",te,B);let ge=Date.now()-o,Ce=typeof U=="number"?new Mt({bestScore:z,stats:this.stats,instruction:f.length===1?k[U].cfg[x[0]]:void 0,instructionMap:{...k[U].cfg},demos:[],examples:t,modelConfig:void 0,optimizerType:"GEPA",optimizationTime:ge,totalRounds:this.numTrials,converged:this.stats.convergenceInfo.converged}):void 0,ke=this.generateOptimizationReport(D,te,z,k.length);return{demos:[],stats:this.stats,bestScore:z,paretoFront:D.map(N=>({demos:[],scores:N.scores,configuration:{candidate:N.idx,instructionMap:{...k[N.idx].cfg},...f.length===1?{instruction:k[N.idx].cfg[x[0]]}:{}},dominatedSolutions:N.dominated})),paretoFrontSize:D.length,hypervolume:te,finalConfiguration:{strategy:"gepa",candidates:k.length,tunables:f.length},optimizedProgram:Ce,report:ke}}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?ta(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,l,u){let c=u?[...u]:[];if(c.length===0)for(let h of i)try{o[e]=t,r(o);let b=await n.forward(this.studentAI,h,{sampleCount:this.sampleCount});this.stats.totalCalls+=1;let I=await a({prediction:b,example:h});c.push({input:h,prediction:b,score:typeof I=="number"?I:0})}catch{c.push({input:h,prediction:{},score:0})}let p=l?.overrideTeacherAI??this.teacherAI??this.studentAI,d=nt('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=[...(l?.feedbackNotes??[]).filter(h=>typeof h=="string"&&h.trim().length>0)],f=l?.feedbackFn;if(typeof f=="function")for(let h of c){let b=f({prediction:h.prediction,example:h.input});b&&(Array.isArray(b)?g.push(...b):g.push(b))}let x="";try{x=(await d.forward(p,{targetId:e,minibatch:c,evalFeedback:g}))?.feedbackSummary?.trim()||""}catch{}let A=nt('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 b=(await A.forward(p,{targetId:e,currentInstruction:t,feedbackSummary:x,minibatch:c}))?.newInstruction?.trim();if(b&&b.length>16)return b}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 l=o?.overrideTeacherAI??this.teacherAI??this.studentAI,u=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 A=a[x],h=`# Example ${x+1}
802
930
  `;if(h+=`## Inputs
803
- `,typeof b.input=="object"&&b.input!==null)for(let[I,C]of Object.entries(b.input))h+=`### ${I}
931
+ `,typeof A.input=="object"&&A.input!==null)for(let[I,C]of Object.entries(A.input))h+=`### ${I}
804
932
  ${String(C).trim()}
805
933
 
806
- `;else h+=`${String(b.input).trim()}
934
+ `;else h+=`${String(A.input).trim()}
807
935
 
808
936
  `;if(h+=`## Generated Outputs
809
- `,typeof b.prediction=="object"&&b.prediction!==null)for(let[I,C]of Object.entries(b.prediction))h+=`### ${I}
937
+ `,typeof A.prediction=="object"&&A.prediction!==null)for(let[I,C]of Object.entries(A.prediction))h+=`### ${I}
810
938
  ${String(C).trim()}
811
939
 
812
- `;else h+=`${String(b.prediction).trim()}
940
+ `;else h+=`${String(A.prediction).trim()}
813
941
 
814
942
  `;h+=`## Feedback
815
- `;let y=`This trajectory got a score of ${b.score.toFixed(3)}.`;if(typeof u=="function")try{let I=u({prediction:b.prediction,example:b.input});I&&(y=Array.isArray(I)?I.join(`
816
- `):I)}catch{}h+=`${y}
817
- `,g.push(h)}return[...c.map((x,b)=>`# Additional Feedback ${b+1}
943
+ `;let b=`This trajectory got a score of ${A.score.toFixed(3)}.`;if(typeof u=="function")try{let I=u({prediction:A.prediction,example:A.input});I&&(b=Array.isArray(I)?I.join(`
944
+ `):I)}catch{}h+=`${b}
945
+ `,g.push(h)}return[...c.map((x,A)=>`# Additional Feedback ${A+1}
818
946
  ${x}`),...g].join(`
819
947
 
820
- `)},d=s.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",p());try{let g=await l.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 b=this.extractInstructionFromBackticks(x);if(b&&b.length>16){let h=`Iteration feedback: ${a.map(y=>`score=${y.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(h),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),b}}}catch{}let m=et('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(l,{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},(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,i=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=i[l%i.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 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 b=[],h=x;for(;h!==void 0;)b.push(h),h=e[h]?.parent;return b},i=o(t),a=o(n),u=i.find(x=>a.includes(x))??t,c=e[u].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 b=c[x],h=p[x],y=d[x];if(h===b&&y!==h)m[x]=y,g.push("j");else if(y===b&&h!==y)m[x]=h,g.push("i");else if(h!==y&&h!==b&&y!==b){let I=r(t,n);m[x]=I===t?h:y,g.push(I===t?"i":"j")}else m[x]=h??y??b,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 l="good",u=[];if(e.length===1)l="single",u.push("Increase numTrials (current seems low)"),u.push("Add more training examples"),u.push("Adjust earlyStoppingTrials");else if(e.length<3)l="limited",u.push("More optimization trials"),u.push("Larger validation set");else{l="good";let c=Object.keys(e[0]?.scores||{});for(let p of c)u.push(`High ${p}: Choose solution with best ${p} score`);u.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(u.push("Quick run detected - use numTrials: 30+ for production"),u.push("Provide 50+ training examples"),u.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:l,suggestions:u}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=et(`instructionA:string "Parent A instruction",
948
+ `)},d=s.REFLECTION_PROMPT_TEMPLATE.replace("<curr_instructions>",e).replace("<inputs_outputs_feedback>",p());try{let g=await l.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 A=this.extractInstructionFromBackticks(x);if(A&&A.length>16){let h=`Iteration feedback: ${a.map(b=>`score=${b.score.toFixed(2)}`).join(", ")}`;return this.feedbackMemory.unshift(h),this.feedbackMemory.length>this.feedbackMemorySize&&this.feedbackMemory.pop(),A}}}catch{}let m=nt('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(l,{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},(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,i=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=i[l%i.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 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 A=[],h=x;for(;h!==void 0;)A.push(h),h=e[h]?.parent;return A},i=o(t),a=o(n),u=i.find(x=>a.includes(x))??t,c=e[u].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 A=c[x],h=p[x],b=d[x];if(h===A&&b!==h)m[x]=b,g.push("j");else if(b===A&&h!==b)m[x]=h,g.push("i");else if(h!==b&&h!==A&&b!==A){let I=r(t,n);m[x]=I===t?h:b,g.push(I===t?"i":"j")}else m[x]=h??b??A,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 l="good",u=[];if(e.length===1)l="single",u.push("Increase numTrials (current seems low)"),u.push("Add more training examples"),u.push("Adjust earlyStoppingTrials");else if(e.length<3)l="limited",u.push("More optimization trials"),u.push("Larger validation set");else{l="good";let c=Object.keys(e[0]?.scores||{});for(let p of c)u.push(`High ${p}: Choose solution with best ${p} score`);u.push("Balanced: Use provided bestScore (average)")}return this.stats.totalCalls<50&&(u.push("Quick run detected - use numTrials: 30+ for production"),u.push("Provide 50+ training examples"),u.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:l,suggestions:u}}}async mergeInstructions(e,t,n){let r=n?.overrideTeacherAI??this.teacherAI??this.studentAI,o=nt(`instructionA:string "Parent A instruction",
821
949
  instructionB:string "Parent B instruction",
822
950
  recentFeedback?:string[] "Past feedback memory"
823
- -> 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(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(`
824
- `)}function Hd(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(`
825
- `)}var Bn=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 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"}),i++}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"}),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=`
951
+ -> 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 tc(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(`
952
+ `)}function Jd(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(`
953
+ `)}var Hn=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 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"}),i++}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"}),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=`
826
954
  You are generating realistic input data for an AI system.
827
955
 
828
956
  ${r?`Task description: ${r}`:""}
829
957
  ${this.options.domain?`Domain: ${this.options.domain}`:""}
830
958
 
831
959
  The system expects these INPUT fields:
832
- ${Wu(t)}
960
+ ${tc(t)}
833
961
 
834
962
  The system produces these OUTPUT fields:
835
- ${Hd(n)}
963
+ ${Jd(n)}
836
964
 
837
965
  Generate ${e} diverse, realistic input examples as a JSON array.
838
966
  Each example should be an object with the input fields defined above.
@@ -843,7 +971,7 @@ Output ONLY the JSON array, no explanation.
843
971
  You are generating challenging edge case input data to test an AI system's robustness.
844
972
 
845
973
  The system expects these INPUT fields:
846
- ${Wu(t)}
974
+ ${tc(t)}
847
975
 
848
976
  Generate ${e} edge case examples as a JSON array.
849
977
  Focus on these types of edge cases:
@@ -851,11 +979,11 @@ ${n.map(i=>`- ${i}`).join(`
851
979
  `)}
852
980
 
853
981
  Output ONLY the JSON array, no explanation.
854
- `.trim(),o=new Se("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 Se(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var Kd="batch",Xi={feedbackWindowSize:25,maxRecentTraces:100,updateBudget:4},us=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??Kd,continuousOptions:{feedbackWindowSize:t.continuousOptions?.feedbackWindowSize??Xi.feedbackWindowSize,maxRecentTraces:t.continuousOptions?.maxRecentTraces??Xi.maxRecentTraces,updateBudget:t.continuousOptions?.updateBudget??Xi.updateBudget}},this.tracer=new Gn(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 Ut({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 Un({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 Gt(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:l,validationExamples:u}=this.splitExamples(a.examples,a.feedbackScoredExamples,e.validationSplit,e.mode),p=await this.createPromptOptimizer({...e,budget:o},r).compile(this.gen,l,i,{validationExamples:u,feedbackExamples:a.feedbackExamples.length>0?a.feedbackExamples:void 0,feedbackFn:a.feedbackTextByKey.size>0?({example:b})=>a.feedbackTextByKey.get(this.exampleKey(b)):void 0,feedbackNotes:a.feedbackNotes.length>0?a.feedbackNotes:void 0,maxMetricCalls:this.computeMetricBudget(o,u.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:l.length,validationExamples:u.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),l=this.createPlaybookOptimizer(e,n),u=await l.compile(this.gen,i,r,{aceOptions:e.playbookOptions});u.optimizedProgram?.applyTo(this.gen),this.playbookOptimizer=l,this.currentScore=u.bestScore,this.tracer=this.tracer.clone(this.gen);let c=u.playbook,p=u.artifact,d=this.summarizePlaybookArtifact(p),m=this.createPlaybookState(c,d,l.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(),l=this.summarizePlaybookArtifact(i),u=this.createPlaybookState(a,l,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:u,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:u,artifact:{playbook:a,playbookSummary:l,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=[],l=new Set,u=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 b=this.exampleKey(g);m?.includeInExamples!==!1&&!l.has(b)&&(l.add(b),n.push(g));let y=m?.feedback?.trim();if(y){let I=i.get(b);I?I!==y&&i.set(b,`${I}
855
- ${y}`):i.set(b,y),m?.includeInFeedbackSet!==!1&&!u.has(b)&&(r.push(g),u.add(b)),m?.feedbackEligibleForScoring&&!c.has(b)&&(o.push(g),c.add(b))}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 Bn(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 l=new Date(i.endTime??i.startTime).getTime();return new Date(a.endTime??a.startTime).getTime()-l}),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 l=this.exampleKey(a),u=this.feedbackToText(i.feedback),c=o.get(l);if(!c){o.set(l,{example:a,feedback:u});continue}!c.feedback&&u&&(c.feedback=u)}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(`
856
- `)}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 l=new Set(t.map(p=>this.exampleKey(p))),u=[];for(let p of o)!l.has(this.exampleKey(p))&&u.length<a&&u.push(p);for(let p of o){if(u.length>=a)break;u.some(d=>this.exampleKey(d)===this.exampleKey(p))||u.push(p)}let c=new Set(u.map(p=>this.exampleKey(p)));return{trainingExamples:o.filter(p=>!c.has(this.exampleKey(p))),validationExamples:u}}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(),"",Yt(e)].filter(i=>i&&i.trim().length>0).join(`
982
+ `.trim(),o=new Se("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 Se(this.signature).forward(this.options.teacher,e,{model:this.options.model})}getSignature(){return this.signature}getTeacher(){return this.options.teacher}};var Qd="batch",ra={feedbackWindowSize:25,maxRecentTraces:100,updateBudget:4},gs=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??Qd,continuousOptions:{feedbackWindowSize:t.continuousOptions?.feedbackWindowSize??ra.feedbackWindowSize,maxRecentTraces:t.continuousOptions?.maxRecentTraces??ra.maxRecentTraces,updateBudget:t.continuousOptions?.updateBudget??ra.updateBudget}},this.tracer=new jn(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 jt({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 zn({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 Bt(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:l,validationExamples:u}=this.splitExamples(a.examples,a.feedbackScoredExamples,e.validationSplit,e.mode),p=await this.createPromptOptimizer({...e,budget:o},r).compile(this.gen,l,i,{validationExamples:u,feedbackExamples:a.feedbackExamples.length>0?a.feedbackExamples:void 0,feedbackFn:a.feedbackTextByKey.size>0?({example:A})=>a.feedbackTextByKey.get(this.exampleKey(A)):void 0,feedbackNotes:a.feedbackNotes.length>0?a.feedbackNotes:void 0,maxMetricCalls:this.computeMetricBudget(o,u.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:l.length,validationExamples:u.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),l=this.createPlaybookOptimizer(e,n),u=await l.compile(this.gen,i,r,{aceOptions:e.playbookOptions});u.optimizedProgram?.applyTo(this.gen),this.playbookOptimizer=l,this.currentScore=u.bestScore,this.tracer=this.tracer.clone(this.gen);let c=u.playbook,p=u.artifact,d=this.summarizePlaybookArtifact(p),m=this.createPlaybookState(c,d,l.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(),l=this.summarizePlaybookArtifact(i),u=this.createPlaybookState(a,l,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:u,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:u,artifact:{playbook:a,playbookSummary:l,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=[],l=new Set,u=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 A=this.exampleKey(g);m?.includeInExamples!==!1&&!l.has(A)&&(l.add(A),n.push(g));let b=m?.feedback?.trim();if(b){let I=i.get(A);I?I!==b&&i.set(A,`${I}
983
+ ${b}`):i.set(A,b),m?.includeInFeedbackSet!==!1&&!u.has(A)&&(r.push(g),u.add(A)),m?.feedbackEligibleForScoring&&!c.has(A)&&(o.push(g),c.add(A))}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 Hn(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 l=new Date(i.endTime??i.startTime).getTime();return new Date(a.endTime??a.startTime).getTime()-l}),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 l=this.exampleKey(a),u=this.feedbackToText(i.feedback),c=o.get(l);if(!c){o.set(l,{example:a,feedback:u});continue}!c.feedback&&u&&(c.feedback=u)}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(`
984
+ `)}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 l=new Set(t.map(p=>this.exampleKey(p))),u=[];for(let p of o)!l.has(this.exampleKey(p))&&u.length<a&&u.push(p);for(let p of o){if(u.length>=a)break;u.some(d=>this.exampleKey(d)===this.exampleKey(p))||u.push(p)}let c=new Set(u.map(p=>this.exampleKey(p)));return{trainingExamples:o.filter(p=>!c.has(this.exampleKey(p))),validationExamples:u}}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(),"",en(e)].filter(i=>i&&i.trim().length>0).join(`
857
985
 
858
- `);if(typeof this.gen.setDescription=="function"){this.gen.setDescription(o);return}o&&this.gen.setInstruction(o)}};var cs=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 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}`);i[c]=u}),i}).filter(o=>Object.keys(o).length!==0)}};var zn=class extends pt{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,l={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(l.modelConfig.max_tokens=this.maxTokensPerGeneration);let u=Vd([...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 f=t.filter(h=>h!==g);e.setDemos([{traces:f,programId:p}]);let x=this.getTeacherOrStudentAI();this.stats.totalCalls++;let b;try{let h={...l,maxRetries:1};b=await e.forward(x,g,h),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(b).length/4),await r({prediction:b,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"}`),b={}}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 i=Wd(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 Wd(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 Vd=(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 ps=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),l=await fetch(n,{...t,signal:i.signal});return clearTimeout(a),l}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 ds=class extends pt{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 ps(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=`
986
+ `);if(typeof this.gen.setDescription=="function"){this.gen.setDescription(o);return}o&&this.gen.setInstruction(o)}};var fs=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 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}`);i[c]=u}),i}).filter(o=>Object.keys(o).length!==0)}};var Kn=class extends At{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,l={modelConfig:{temperature:.7}};this.maxTokensPerGeneration>0&&(l.modelConfig.max_tokens=this.maxTokensPerGeneration);let u=Xd([...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 f=t.filter(h=>h!==g);e.setDemos([{traces:f,programId:p}]);let x=this.getTeacherOrStudentAI();this.stats.totalCalls++;let A;try{let h={...l,maxRetries:1};A=await e.forward(x,g,h),this.costMonitoring&&(this.stats.estimatedTokenUsage+=JSON.stringify(g).length/4+JSON.stringify(A).length/4),await r({prediction:A,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"}`),A={}}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 i=Yd(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 Yd(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 Xd=(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 hs=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),l=await fetch(n,{...t,signal:i.signal});return clearTimeout(a),l}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 xs=class extends At{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 hs(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=`
859
987
  Analyze this language model program and provide a concise summary of its purpose and structure.
860
988
 
861
989
  Program Signature: ${e.getSignature()}
@@ -895,12 +1023,12 @@ Requirements:
895
1023
  5. Keep it concise but comprehensive
896
1024
 
897
1025
  Generate a single, well-crafted instruction:
898
- Instruction:`;try{let m=(await et('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),i,a;this.programAwareProposer&&(i=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:i,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new zn({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)}`,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: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},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 R=0;R<this.numTrials;R++)try{let P=await this.pythonClient.suggestParameters(a),v=P.params.temperature,E=P.params.bootstrappedDemos,k=P.params.instruction,O=P.params.labeledExamples,L=this.optimizeTopP?P.params.topP:void 0;if(v===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(P)}`);if(E===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(P)}`);if(k===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(P)}`);if(O===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(P)}`);let F=e.clone();F.setInstruction(k);let M=F.getId();F.setDemos([{traces:this.selectLabeledExamples(t).slice(0,O),programId:M}]);let j=!this.minibatch||this.minibatchFullEvalSteps>0&&R%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let Y=Math.min(this.minibatchSize,t.length),G=new Set;for(;G.size<Y;)G.add(Math.floor(Math.random()*t.length));return Array.from(G).map(me=>t[me])})(),H=await this.evaluateConfiguration(F,n,{temperature:v,bootstrappedDemos:E,topP:L},j);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:P.trial_number,value:H}),H>p+this.minImprovementThreshold?(p=H,d={temperature:v,bootstrappedDemos:E,...L!==void 0?{topP:L}:{},trialNumber:P.trial_number},g=0):g+=1,this.currentRound=R+1;let D={temperature:v,bootstrappedDemos:E,...L!==void 0?{topP:L}:{},trialNumber:P.trial_number};if(this.localScoreHistory.push(H),this.localConfigurationHistory.push(D),await this.updateOptimizationProgress(this.currentRound,H,D,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:R+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={},b=[];try{let R=await this.pythonClient.getStudyResults(a);if(f=R.best_value||p,x=R.best_params||{},x&&Object.keys(x).length>0){let P=x.bootstrappedDemos||0;P>0&&(b=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),b=b.slice(0,P))}}catch{}let h;try{let P=await et('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:P.humanExplanation??"",recommendations:P.recommendations??[],performanceAssessment:P.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",f,x,r,h);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=f;let y=new Se(e.getSignature());x.instruction&&y.setInstruction(x.instruction);let I=x.temperature?{temperature:x.temperature}:void 0;y.setDemos(b,{modelConfig:I});let C=new kt({bestScore:f,stats:this.stats,instruction:x.instruction,demos:b,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,b.length),{bestScore:f,demos:b,stats:this.stats,optimizedGen:y,optimizedProgram:C,finalConfiguration:{temperature:x.temperature,bootstrappedDemos:x.bootstrappedDemos||0,...x}}}generateOptimizationReport(e,t){console.log(`
1026
+ Instruction:`;try{let m=(await nt('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),i,a;this.programAwareProposer&&(i=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:i,datasetSummary:a,previousInstructions:r});r.push(d)}return r}async bootstrapFewShotExamples(e,t,n){return(await new Kn({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)}`,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: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},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 R=0;R<this.numTrials;R++)try{let M=await this.pythonClient.suggestParameters(a),S=M.params.temperature,O=M.params.bootstrappedDemos,k=M.params.instruction,_=M.params.labeledExamples,L=this.optimizeTopP?M.params.topP:void 0;if(S===void 0)throw new Error(`Missing temperature parameter in suggestion: ${JSON.stringify(M)}`);if(O===void 0)throw new Error(`Missing bootstrappedDemos parameter in suggestion: ${JSON.stringify(M)}`);if(k===void 0)throw new Error(`Missing instruction parameter in suggestion: ${JSON.stringify(M)}`);if(_===void 0)throw new Error(`Missing labeledExamples parameter in suggestion: ${JSON.stringify(M)}`);let E=e.clone();E.setInstruction(k);let P=E.getId();E.setDemos([{traces:this.selectLabeledExamples(t).slice(0,_),programId:P}]);let D=!this.minibatch||this.minibatchFullEvalSteps>0&&R%this.minibatchFullEvalSteps===this.minibatchFullEvalSteps-1?[...t]:(()=>{let te=Math.min(this.minibatchSize,t.length),B=new Set;for(;B.size<te;)B.add(Math.floor(Math.random()*t.length));return Array.from(B).map(ge=>t[ge])})(),z=await this.evaluateConfiguration(E,n,{temperature:S,bootstrappedDemos:O,topP:L},D);m++,await this.pythonClient.evaluateTrial({study_name:a,trial_number:M.trial_number,value:z}),z>p+this.minImprovementThreshold?(p=z,d={temperature:S,bootstrappedDemos:O,...L!==void 0?{topP:L}:{},trialNumber:M.trial_number},g=0):g+=1,this.currentRound=R+1;let U={temperature:S,bootstrappedDemos:O,...L!==void 0?{topP:L}:{},trialNumber:M.trial_number};if(this.localScoreHistory.push(z),this.localConfigurationHistory.push(U),await this.updateOptimizationProgress(this.currentRound,z,U,"MiPRO (Python)",{sampler:"TPESampler"},p,d),this.onProgress?.({round:R+1,totalRounds:this.numTrials,currentScore:z,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={},A=[];try{let R=await this.pythonClient.getStudyResults(a);if(f=R.best_value||p,x=R.best_params||{},x&&Object.keys(x).length>0){let M=x.bootstrappedDemos||0;M>0&&(A=await this.bootstrapFewShotExamples(e,n,t.slice(0,Math.floor(t.length*.8))),A=A.slice(0,M))}}catch{}let h;try{let M=await nt('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:M.humanExplanation??"",recommendations:M.recommendations??[],performanceAssessment:M.performanceAssessment??""}}catch{}await this.logOptimizationComplete("MiPRO (Python)",f,x,r,h);try{await this.pythonClient.deleteStudy(a)}catch{}this.stats.bestScore=f;let b=new Se(e.getSignature());x.instruction&&b.setInstruction(x.instruction);let I=x.temperature?{temperature:x.temperature}:void 0;b.setDemos(A,{modelConfig:I});let C=new Mt({bestScore:f,stats:this.stats,instruction:x.instruction,demos:A,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,A.length),{bestScore:f,demos:A,stats:this.stats,optimizedGen:b,optimizedProgram:C,finalConfiguration:{temperature:x.temperature,bootstrappedDemos:x.bootstrappedDemos||0,...x}}}generateOptimizationReport(e,t){console.log(`
899
1027
  \u{1F389} MiPRO Optimization Complete!
900
1028
  `),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
901
1029
  `),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
902
1030
  `),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
903
- `),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,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,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 qn=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 Hn(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((l,u)=>{let c=r+u;return e(l,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(i);for(let{result:l,originalIndex:u}of a)n[u]=l}return n}var Kn=class{steps=[];parallelGroups=[];analyzer=new qn;initialFields=new Set;addExecutionStep(e,t,n,r,o,i,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(f=>d.add(f));u=Array.from(d)}else o?u=this.analyzeMapTransformation(o):u=["_parallelMapResult"];l=this.getAllProducedFields()}else if(c==="merge"){if(i?.resultKey)u=[i.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,f)=>f.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(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 Hn(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 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 Qn=require("@opentelemetry/api");var Vu=s=>{console.log(s)},Wn=(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)},Vn=s=>s<1e3?`${s.toFixed(1)}ms`:s<6e4?`${(s/1e3).toFixed(2)}s`:`${(s/6e4).toFixed(2)}min`,Ur=(s=Vu)=>{let e=new Ze,t=e.gray(`${"\u2501".repeat(80)}
1031
+ `),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,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,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 Wn=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 Vn(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((l,u)=>{let c=r+u;return e(l,c).then(p=>({result:p,originalIndex:c}))}),a=await Promise.all(i);for(let{result:l,originalIndex:u}of a)n[u]=l}return n}var Jn=class{steps=[];parallelGroups=[];analyzer=new Wn;initialFields=new Set;addExecutionStep(e,t,n,r,o,i,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(f=>d.add(f));u=Array.from(d)}else o?u=this.analyzeMapTransformation(o):u=["_parallelMapResult"];l=this.getAllProducedFields()}else if(c==="merge"){if(i?.resultKey)u=[i.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,f)=>f.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(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 Vn(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 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 Zn=require("@opentelemetry/api");var nc=s=>{console.log(s)},Qn=(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)},Yn=s=>s<1e3?`${s.toFixed(1)}ms`:s<6e4?`${(s/1e3).toFixed(2)}s`:`${(s/6e4).toFixed(2)}min`,zr=(s=nc)=>{let e=new tt,t=e.gray(`${"\u2501".repeat(80)}
904
1032
  `),n=e.gray(`${"\u2500".repeat(40)}
905
1033
  `);return r=>{let o="";switch(r.name){case"FlowStart":o=`
906
1034
  ${e.blueBright("\u{1F504} [ AXFLOW START ]")}
@@ -912,13 +1040,13 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
912
1040
  `,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+=`
913
1041
  `,r.dependencies.length>0&&(o+=`${e.white("Dependencies:")} ${e.gray(r.dependencies.join(", "))}
914
1042
  `),r.produces.length>0&&(o+=`${e.white("Produces:")} ${e.cyan(r.produces.join(", "))}
915
- `),o+=`${e.white("State:")} ${e.gray(Wn(r.state,!0))}
916
- `,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 ${Vn(r.executionTime)}`)}
1043
+ `),o+=`${e.white("State:")} ${e.gray(Qn(r.state,!0))}
1044
+ `,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 ${Yn(r.executionTime)}`)}
917
1045
  `,r.newFields&&r.newFields.length>0&&(o+=`${e.white("New Fields:")} ${e.green(r.newFields.join(", "))}
918
1046
  `),r.result&&r.nodeName&&(o+=`${e.white("Result:")} ${e.yellow(JSON.stringify(r.result,null,2))}
919
1047
  `),o+=n;break}case"ParallelGroupStart":o=`${e.blueBright("\u2696\uFE0F [ PARALLEL GROUP START ]")} ${e.white(`Level ${r.groupLevel}`)}
920
1048
  `,o+=`${e.white("Steps:")} ${e.yellow(r.stepsCount.toString())} ${e.gray(`(${r.stepTypes.join(", ")})`)}
921
- `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Vn(r.executionTime)}`)}
1049
+ `,o+=n;break;case"ParallelGroupComplete":o=`${e.blueBright("\u2705 [ PARALLEL GROUP COMPLETE ]")} ${e.white(`Level ${r.groupLevel}`)}`,o+=` ${e.magenta(`in ${Yn(r.executionTime)}`)}
922
1050
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsCount.toString())}
923
1051
  `,o+=n;break;case"BranchEvaluation":o=`${e.yellow("\u{1F500} [ BRANCH EVALUATION ]")}
924
1052
  `,o+=`${e.white("Branch Value:")} ${e.cyan(JSON.stringify(r.branchValue))}
@@ -926,16 +1054,16 @@ ${t}`,o+=`${e.white("Input Fields:")} ${e.cyan(r.inputFields.join(", "))}
926
1054
  `,r.hasMatchingBranch&&(o+=`${e.white("Branch Steps:")} ${e.yellow(r.branchStepsCount.toString())}
927
1055
  `),o+=n;break;case"FlowComplete":o=`
928
1056
  ${e.greenBright("\u2705 [ AXFLOW COMPLETE ]")}
929
- ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Vn(r.totalExecutionTime))}
1057
+ ${t}`,o+=`${e.white("Total Time:")} ${e.magenta(Yn(r.totalExecutionTime))}
930
1058
  `,o+=`${e.white("Steps Executed:")} ${e.yellow(r.stepsExecuted.toString())}
931
1059
  `,o+=`${e.white("Output Fields:")} ${e.green(r.outputFields.join(", "))}
932
- `,o+=`${e.white("Final State:")} ${e.gray(Wn(r.finalState,!0))}
1060
+ `,o+=`${e.white("Final State:")} ${e.gray(Qn(r.finalState,!0))}
933
1061
  `,o+=t;break;case"FlowError":o=`
934
1062
  ${e.redBright("\u274C [ AXFLOW ERROR ]")}
935
1063
  ${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+=`
936
1064
  `),o+=`${e.white("Error:")} ${e.red(r.error)}
937
- `,r.state&&(o+=`${e.white("State:")} ${e.gray(Wn(r.state,!0))}
938
- `),o+=t;break;default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},Ju=(s=Vu)=>{let e="=".repeat(80),t="-".repeat(40);return n=>{let r="";switch(n.name){case"FlowStart":r=`
1065
+ `,r.state&&(o+=`${e.white("State:")} ${e.gray(Qn(r.state,!0))}
1066
+ `),o+=t;break;default:o=e.gray(JSON.stringify(r,null,2))}s(o)}},rc=(s=nc)=>{let e="=".repeat(80),t="-".repeat(40);return n=>{let r="";switch(n.name){case"FlowStart":r=`
939
1067
  [ AXFLOW START ]
940
1068
  ${e}
941
1069
  `,r+=`Input Fields: ${n.inputFields.join(", ")}
@@ -947,16 +1075,16 @@ ${e}
947
1075
  `;break;case"StepStart":r=`[ STEP ${n.stepIndex} START ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
948
1076
  `,n.dependencies.length>0&&(r+=`Dependencies: ${n.dependencies.join(", ")}
949
1077
  `),n.produces.length>0&&(r+=`Produces: ${n.produces.join(", ")}
950
- `),r+=`State: ${Wn(n.state,!0)}
1078
+ `),r+=`State: ${Qn(n.state,!0)}
951
1079
  `,r+=`${t}
952
- `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Vn(n.executionTime)}
1080
+ `;break;case"StepComplete":r=`[ STEP ${n.stepIndex} COMPLETE ] (${n.stepType})`,n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=` in ${Yn(n.executionTime)}
953
1081
  `,n.newFields&&n.newFields.length>0&&(r+=`New Fields: ${n.newFields.join(", ")}
954
1082
  `),n.result&&n.nodeName&&(r+=`Result: ${JSON.stringify(n.result,null,2)}
955
1083
  `),r+=`${t}
956
1084
  `;break;case"ParallelGroupStart":r=`[ PARALLEL GROUP START ] Level ${n.groupLevel}
957
1085
  `,r+=`Steps: ${n.stepsCount} (${n.stepTypes.join(", ")})
958
1086
  `,r+=`${t}
959
- `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Vn(n.executionTime)}
1087
+ `;break;case"ParallelGroupComplete":r=`[ PARALLEL GROUP COMPLETE ] Level ${n.groupLevel} in ${Yn(n.executionTime)}
960
1088
  `,r+=`Steps Executed: ${n.stepsCount}
961
1089
  `,r+=`${t}
962
1090
  `;break;case"BranchEvaluation":r=`[ BRANCH EVALUATION ]
@@ -967,21 +1095,21 @@ ${e}
967
1095
  `;break;case"FlowComplete":r=`
968
1096
  [ AXFLOW COMPLETE ]
969
1097
  ${e}
970
- `,r+=`Total Time: ${Vn(n.totalExecutionTime)}
1098
+ `,r+=`Total Time: ${Yn(n.totalExecutionTime)}
971
1099
  `,r+=`Steps Executed: ${n.stepsExecuted}
972
1100
  `,r+=`Output Fields: ${n.outputFields.join(", ")}
973
- `,r+=`Final State: ${Wn(n.finalState,!0)}
1101
+ `,r+=`Final State: ${Qn(n.finalState,!0)}
974
1102
  `,r+=`${e}
975
1103
  `;break;case"FlowError":r=`
976
1104
  [ AXFLOW ERROR ]
977
1105
  ${e}
978
1106
  `,n.stepIndex!==void 0&&(r+=`Step: ${n.stepIndex}`,n.stepType&&(r+=` (${n.stepType})`),n.nodeName&&(r+=` Node: ${n.nodeName}`),r+=`
979
1107
  `),r+=`Error: ${n.error}
980
- `,n.state&&(r+=`State: ${Wn(n.state,!0)}
1108
+ `,n.state&&(r+=`State: ${Qn(n.state,!0)}
981
1109
  `),r+=`${e}
982
- `;break;default:r=JSON.stringify(n,null,2)}s(r)}},Qu=Ur(),Yu=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 Jn=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,l=n?.options??i.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}},ms=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,l=n?.options??i.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 jr=class s{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Kn;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),i=tt("sha256");i.update(n.hash()??"");let a=l=>{let u=typeof l;if(i.update(`|${u}|`),l==null){i.update("null");return}if(u==="string"||u==="number"||u==="boolean"){i.update(String(l));return}if(Array.isArray(l)){i.update("[");for(let c of l)a(c);i.update("]");return}if(typeof l=="object"&&l!==null&&"mimeType"in l&&"data"in l){let c=l;i.update(c.mimeType??"");let p=tt("sha256").update(c.data??"").digest("hex");i.update(p);return}if(typeof l=="object"){let c=l,p=Object.keys(c).sort();for(let d of p)i.update(`{${d}}`),a(c[d]);return}i.update(String(l))};for(let l of r)a(o?.[l]);return i.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},i=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 $e("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),i++;let f=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,b=Object.keys(o).filter(y=>!d.includes(y)),h;if(c==="execute"&&p.nodeName&&b.length>0){let y=`${p.nodeName}Result`;h=o[y]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:f,state:{...o},newFields:b,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 pe().input("userInput",pe.string("User input to the flow")).output("flowOutput",pe.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 b=this.toCamelCase(`${m}_${x.name}`);c.push({name:b,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}for(let x of f.getOutputFields()){let b=this.toCamelCase(`${m}_${x.name}`);p.push({name:b,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}}let d=new Fe;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 Fe,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){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=Ur():this.flowLogger=void 0,this.timingLogger=this.flowLogger?Yu(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 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()}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(`
1110
+ `;break;default:r=JSON.stringify(n,null,2)}s(r)}},oc=zr(),sc=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 Xn=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,l=n?.options??i.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}},As=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,l=n?.options??i.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 qr=class s{static _ctorWarned=!1;nodes=new Map;flowDefinition=[];nodeGenerators=new Map;loopStack=[];stepLabels=new Map;branchContext=null;autoParallelConfig;executionPlanner=new Jn;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),i=ut("sha256");i.update(n.hash()??"");let a=l=>{let u=typeof l;if(i.update(`|${u}|`),l==null){i.update("null");return}if(u==="string"||u==="number"||u==="boolean"){i.update(String(l));return}if(Array.isArray(l)){i.update("[");for(let c of l)a(c);i.update("]");return}if(typeof l=="object"&&l!==null&&"mimeType"in l&&"data"in l){let c=l;i.update(c.mimeType??"");let p=ut("sha256").update(c.data??"").digest("hex");i.update(p);return}if(typeof l=="object"){let c=l,p=Object.keys(c).sort();for(let d of p)i.update(`{${d}}`),a(c[d]);return}i.update(String(l))};for(let l of r)a(o?.[l]);return i.digest("hex")}async executeStepsWithLogging(e,t,n,r){let o={...t},i=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 Ee("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),i++;let f=this.timingLogger?.endTiming(`step-${a}`)??Date.now()-m,A=Object.keys(o).filter(b=>!d.includes(b)),h;if(c==="execute"&&p.nodeName&&A.length>0){let b=`${p.nodeName}Result`;h=o[b]}this.flowLogger&&this.flowLogger({name:"StepComplete",timestamp:Date.now(),stepIndex:a,stepType:c,nodeName:p.nodeName,executionTime:f,state:{...o},newFields:A,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 ae().input("userInput",ae.string("User input to the flow")).output("flowOutput",ae.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 A=this.toCamelCase(`${m}_${x.name}`);c.push({name:A,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}for(let x of f.getOutputFields()){let A=this.toCamelCase(`${m}_${x.name}`);p.push({name:A,type:x.type,description:x.description,isOptional:x.isOptional,isInternal:x.isInternal})}}let d=new _e;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 _e,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){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=zr():this.flowLogger=void 0,this.timingLogger=this.flowLogger?sc(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 Ut(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(`
983
1111
  `)[0]||"axFlow")).replace(/\s+/g,"_"),r=this.toCamelCase(n),o=async(i,a)=>{let l=a?.ai;if(!l)throw new Error("AI service is required to run the flow");let u=await this.forward(l,i??{}),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(`
984
- `)};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 vr(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]=vr(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??ie.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??ie.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={},l,u;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let y=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:i,inputFields:Object.keys(c),totalSteps:y.totalSteps,parallelGroups:y.parallelGroups,maxParallelism:y.maxParallelism,autoParallelEnabled:y.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let y=this.getExecutionPlan(),I=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";l=p.startSpan(I,{kind:Qn.SpanKind.INTERNAL,attributes:{total_steps:y.totalSteps,parallel_groups:y.parallelGroups,max_parallelism:y.maxParallelism,auto_parallel_enabled:y.autoParallelEnabled}});let C=d??Qn.context.active();m=Qn.trace.setSpan(C,l)}u=new AbortController,this.activeAbortControllers.add(u),this._stopRequested&&u.abort("Stopped by user (pre-forward)");let g=Tt(n?.abortSignal,n?.abortController?.signal),f=Tt(u.signal,Tt(g,ie.abortSignal)),x={mainAi:e,mainOptions:(()=>{let y={...this.defaultAIOptions??{},...n};return n?.model&&(y.model=String(n.model)),p&&(y.tracer=p),m&&(y.traceContext=m),f&&(y.abortSignal=f),Object.keys(y).length>0?y:void 0})()},b=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,h=0;if(b){this.executionPlanner.setInitialFields(Object.keys(c));let y=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),I=await this.executeStepsWithLogging(y,a,x,!0);a=I.finalState,h=I.stepsExecuted}else{let y=await this.executeStepsWithLogging(this.flowDefinition,a,x,!1);a=y.finalState,h=y.stepsExecuted}if(this.flowLogger){let y=this.timingLogger?.endTiming("flow-execution")??Date.now()-i;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:y,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{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 Fe){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=et(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 Hn(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(i,a)=>{let l=n?.ai??a.mainAi,u={...a.mainOptions??{},...n?.options??{}},c=t(i),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{...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 l of i)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 Hn(e,async(i,a)=>{let l=new Jn(this.nodeGenerators);return await i(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=i=>{let a=i._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let l=r(...a),u={...i};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(i,a)=>{let l=i,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(i,a)=>{let l=i,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 i=>{let a=i[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 Hn(a,async(c,p)=>n(c,p,i),u)}else l=a.map((u,c)=>n(u,c,i));else l=n(a,void 0,i);return{...i,[e]:l}};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"?Fe.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 gs(s){return jr.create(s)}var fs=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 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 l=await a.json();return this.containerId=l.Id,l}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let i=(await this.listContainers(!0)).filter(l=>l.Labels&&l.Labels["com.example.tag"]===r);if(i&&i.length>0){let l=Math.floor(Math.random()*i.length),u=i[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),i=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of i){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 hs=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 Xt(s){return!!s&&/[A-Za-z0-9_$]/.test(s)}function Xu(s){return!!s&&/[A-Za-z_$]/.test(s)}function Yn(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===`
1112
+ `)};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 Mr(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]=Mr(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??le.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??le.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={},l,u;try{this.resetUsage(),this.resetTraces();let c=this.extractInputValues(t);if(this.nodeGenerators.size>0&&this.ensureProgram(),a={...c},this.flowLogger){let b=this.getExecutionPlan();this.flowLogger({name:"FlowStart",timestamp:i,inputFields:Object.keys(c),totalSteps:b.totalSteps,parallelGroups:b.parallelGroups,maxParallelism:b.maxParallelism,autoParallelEnabled:b.autoParallelEnabled})}let p=n?.tracer??this.defaultAIOptions?.tracer,d=n?.traceContext,m=d;if(p){let b=this.getExecutionPlan(),I=n?.traceLabel?`AxFlow > ${n.traceLabel}`:"AxFlow";l=p.startSpan(I,{kind:Zn.SpanKind.INTERNAL,attributes:{total_steps:b.totalSteps,parallel_groups:b.parallelGroups,max_parallelism:b.maxParallelism,auto_parallel_enabled:b.autoParallelEnabled}});let C=d??Zn.context.active();m=Zn.trace.setSpan(C,l)}u=new AbortController,this.activeAbortControllers.add(u),this._stopRequested&&u.abort("Stopped by user (pre-forward)");let g=Tt(n?.abortSignal,n?.abortController?.signal),f=Tt(u.signal,Tt(g,le.abortSignal)),x={mainAi:e,mainOptions:(()=>{let b={...this.defaultAIOptions??{},...n};return n?.model&&(b.model=String(n.model)),p&&(b.tracer=p),m&&(b.traceContext=m),f&&(b.abortSignal=f),Object.keys(b).length>0?b:void 0})()},A=n?.autoParallel!==!1&&this.autoParallelConfig.enabled,h=0;if(A){this.executionPlanner.setInitialFields(Object.keys(c));let b=this.executionPlanner.createOptimizedExecution(this.autoParallelConfig.batchSize),I=await this.executeStepsWithLogging(b,a,x,!0);a=I.finalState,h=I.stepsExecuted}else{let b=await this.executeStepsWithLogging(this.flowDefinition,a,x,!1);a=b.finalState,h=b.stepsExecuted}if(this.flowLogger){let b=this.timingLogger?.endTiming("flow-execution")??Date.now()-i;this.flowLogger({name:"FlowComplete",timestamp:Date.now(),totalExecutionTime:b,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{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 _e){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=nt(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 Vn(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(i,a)=>{let l=n?.ai??a.mainAi,u={...a.mainOptions??{},...n?.options??{}},c=t(i),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{...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 l of i)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 Vn(e,async(i,a)=>{let l=new Xn(this.nodeGenerators);return await i(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=i=>{let a=i._parallelResults;if(!Array.isArray(a))throw new Error("No parallel results found for merge");let l=r(...a),u={...i};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(i,a)=>{let l=i,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(i,a)=>{let l=i,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 i=>{let a=i[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 Vn(a,async(c,p)=>n(c,p,i),u)}else l=a.map((u,c)=>n(u,c,i));else l=n(a,void 0,i);return{...i,[e]:l}};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"?_e.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 ys(s){return qr.create(s)}var bs=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 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 l=await a.json();return this.containerId=l.Id,l}async findOrCreateContainer({imageName:e,volumes:t=[],doNotPullImage:n,tag:r}){let i=(await this.listContainers(!0)).filter(l=>l.Labels&&l.Labels["com.example.tag"]===r);if(i&&i.length>0){let l=Math.floor(Math.random()*i.length),u=i[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),i=e?o.filter(a=>a.Labels["com.example.tag"]===e):o;for(let a of i){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 Is=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 tn(s){return!!s&&/[A-Za-z0-9_$]/.test(s)}function ic(s){return!!s&&/[A-Za-z_$]/.test(s)}function er(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===`
985
1113
  `?(e+=`
986
1114
  `,n="normal"):e+=" ",t++;continue}if(n==="blockComment"){o==="*"&&i==="/"?(e+=" ",t+=2,n="normal"):(e+=o===`
987
1115
  `?`
@@ -989,66 +1117,66 @@ ${e}
989
1117
  `?`
990
1118
  `:" ",r=!1,t++;continue}if(o==="\\"){e+=" ",r=!0,t++;continue}if(o===a){e+=" ",n="normal",t++;continue}e+=o===`
991
1119
  `?`
992
- `:" ",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 Br(s){let e=[],t=s.length,n=0,r=0,o=0,i=b=>{if(n++,b==="`"){let h=0;for(;n<t;){let y=s[n];if(y==="\\"){n+=2;continue}if(h>0){y==="{"?h++:y==="}"&&h--,n++;continue}if(y==="$"&&n+1<t&&s[n+1]==="{"){h++,n+=2;continue}if(y==="`"){n++;return}n++}return}for(;n<t;){let h=s[n];if(h==="\\"){n+=2;continue}if(h===b){n++;return}n++}},a=()=>{for(n+=2;n<t&&s[n]!==`
993
- `;)n++},l=()=>{for(n+=2;n<t;){if(s[n]==="*"&&n+1<t&&s[n+1]==="/"){n+=2;return}n++}},u=()=>{let b=n;for(;n<t&&Xt(s[n]);)n++;return s.slice(b,n)},c=()=>{let b=n;for(;n<t;){let h=s[n];if(h===" "||h===" "||h===`
994
- `||h==="\r"){n++;continue}if(h==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){l();continue}}break}return n>b},p=b=>{let h=1;for(;n<t&&h>0;){if(c(),n>=t)return;let y=s[n];if(y===b){h--,n++;continue}if(y==="{"||y==="["){let I=y==="{"?"}":"]";n++,p(I);continue}if(y==="."&&n+2<t&&s[n+1]==="."&&s[n+2]==="."){if(n+=3,c(),n<t&&Xt(s[n])){let I=u();I&&e.push(I)}continue}if(y===","){n++;continue}if(y==="="){n++;let I=0;for(;n<t;){let C=s[n];if(C==="'"||C==='"'||C==="`"){i(C);continue}if(C==="("||C==="["||C==="{"){I++,n++;continue}if(C===")"||C==="]"||C==="}"){if(I>0){I--,n++;continue}break}if(C===","&&I===0)break;n++}continue}if(Xt(y)){let I=u();if(c(),n<t&&s[n]===":"){if(n++,c(),n<t){let C=s[n];if(C==="{"||C==="["){let R=C==="{"?"}":"]";n++,p(R)}else if(Xt(C)){let R=u();R&&e.push(R)}}}else I&&e.push(I);continue}n++}},d=()=>{let b=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]==="*"){l();continue}}if(h==="("||h==="["||h==="{"){b++,n++;continue}if(h===")"||h==="]"||h==="}"){if(b>0){b--,n++;continue}return!1}if(h===","&&b===0)return n++,!0;if(h===";"&&b===0)return n++,!1;if(h===`
995
- `&&b===0){let y=n;return n++,c(),n<t&&s[n]===","?(n++,!0):(n=y,!1)}n++}return!1},m=()=>{for(;n<t;){if(c(),n>=t)return;let b=s[n];if(b==="{"){if(n++,p("}"),!d())return;continue}if(b==="["){if(n++,p("]"),!d())return;continue}if(Xt(b)){let h=u();if(h&&e.push(h),!d())return;continue}return}},g=b=>{if(b===0)return!0;let h=b-1;for(;h>=0;){let y=s[h];if(y===" "||y===" "||y==="\r"){h--;continue}return y===`
996
- `||y===";"||y==="{"||y==="}"}return!0};for(;n<t;){let b=s[n];if(b==="'"||b==='"'||b==="`"){i(b);continue}if(b==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){l();continue}}if(b==="{"){r++,n++;continue}if(b==="}"){r--,n++;continue}if(b==="("){o++,n++;continue}if(b===")"){o--,n++;continue}if(r===0&&o===0&&Xt(b)){let h=n,y=u();(y==="var"||y==="let"||y==="const")&&n<t&&(s[n]===" "||s[n]===" "||s[n]===`
997
- `)&&g(h)&&m();continue}n++}let f=new Set,x=[];for(let b of e)f.has(b)||(f.add(b),x.push(b));return x}function Zu(s){let e=new Set(Br(s)),t=Yn(s),n=t.length,r=0,o=0,i=0,a=m=>{let g=m;for(;g<n&&/\s/.test(t[g]??"");)g++;return g},l=m=>{let g=m;for(;g>=0&&/\s/.test(t[g]??"");)g--;return g},u=m=>{let g=t.slice(m,m+3),f=t.slice(m,m+2);return g==="==="||f==="=="||f==="=>"?!1:t[m]==="="||["+=","-=","*=","/=","%=","&=","|=","^=","&&=","||=","??=","**=","<<=",">>=",">>>="].some(x=>t.startsWith(x,m))},c=m=>{let g=m;for(;g<n&&Xt(t[g]);)g++;return{word:t.slice(m,g),nextIndex:g}},p=(m,g,f)=>{let x=l(g-1),b=x>=0?t[x]:void 0,h=a(f);if(b==="."||b==="?")return;let I=t.slice(Math.max(0,g-2),g)==="++"||t.slice(Math.max(0,g-2),g)==="--",C=t.startsWith("++",h)||t.startsWith("--",h);(I||C||u(h))&&e.add(m)},d=(m,g)=>{let f=a(g);if(t[f]!==".")return;let x=a(f+1);if(!Xu(t[x]))return;let{word:b,nextIndex:h}=c(x),y=a(h),I=t.slice(Math.max(0,m-2),m)==="++"||t.slice(Math.max(0,m-2),m)==="--",C=t.startsWith("++",y)||t.startsWith("--",y);b&&(I||C||u(y))&&e.add(b)};for(;r<n;){let m=t[r];if(m==="{"){o++,r++;continue}if(m==="}"){o--,r++;continue}if(m==="("){i++,r++;continue}if(m===")"){i--,r++;continue}if(o===0&&i===0&&Xu(m)){let g=r,{word:f,nextIndex:x}=c(r);if(r=x,!f)continue;if(f==="globalThis"){d(g,x);continue}p(f,g,x);continue}r++}return[...e]}function xs(s){let e=typeof self<"u"?self:globalThis,t=[],n=Object.getPrototypeOf(async()=>{}).constructor,r=s.functionRefKey,o="return",i="stdout",a=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,l=/^\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"]},c=()=>{let A;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(A=process.getBuiltinModule.bind(process)),!A)try{A=new Function('return typeof require==="function"?require:undefined')()}catch{A=void 0}if(!(typeof A=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:A("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:p,parentPort:d}=c(),m=()=>{if(!d&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:S=>{if(d){d.postMessage(S);return}e.postMessage(S)},setOnMessage:S=>{if(d){d.on("message",T=>S({data:T}));return}e.onmessage=S}}},{send:g,setOnMessage:f}=m(),x=A=>A&&(/\r?\n$/.test(A)?A:`${A}
998
- `),b=A=>{let w=A.trim();return w.startsWith("//")||w.startsWith("/*")||w.startsWith("*")},h=A=>{let w=A.length-1;for(;w>=0;){let S=A[w].trim();if(S&&!b(S))break;w-=1}return w},y=A=>{if(!A)return!0;let w=A.split(`
999
- `).find(S=>S.trim().length>0)?.trim();return!w||a.test(w)?!0:w.startsWith("{")&&!w.startsWith("({")||w==="}"||w==="};"||b(w)},I=(A,w)=>{let S=A,T=w.trim().replace(/;\s*$/,"");if(!T)return null;let $=T.lastIndexOf(";");if($!==-1){let z=T.slice($+1).trim(),J=T.slice(0,$).trim();z&&(z.startsWith("//")||z.startsWith("/*")?J&&(T=J):(J&&(S=S?`${S}
1000
- ${J};`:`${J};`),T=z))}return y(T)?null:{head:S,expression:T}},C=(A,w,S)=>{let T=A.slice(0,w).join(`
1001
- `),$=A.slice(w,S+1).join(`
1002
- `).trim();if(!$)return null;if(!$.includes(`
1003
- `)){let z=I(T,$);return z?z.head?`${z.head}
1120
+ `:" ",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 Hr(s){let e=[],t=s.length,n=0,r=0,o=0,i=A=>{if(n++,A==="`"){let h=0;for(;n<t;){let b=s[n];if(b==="\\"){n+=2;continue}if(h>0){b==="{"?h++:b==="}"&&h--,n++;continue}if(b==="$"&&n+1<t&&s[n+1]==="{"){h++,n+=2;continue}if(b==="`"){n++;return}n++}return}for(;n<t;){let h=s[n];if(h==="\\"){n+=2;continue}if(h===A){n++;return}n++}},a=()=>{for(n+=2;n<t&&s[n]!==`
1121
+ `;)n++},l=()=>{for(n+=2;n<t;){if(s[n]==="*"&&n+1<t&&s[n+1]==="/"){n+=2;return}n++}},u=()=>{let A=n;for(;n<t&&tn(s[n]);)n++;return s.slice(A,n)},c=()=>{let A=n;for(;n<t;){let h=s[n];if(h===" "||h===" "||h===`
1122
+ `||h==="\r"){n++;continue}if(h==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){l();continue}}break}return n>A},p=A=>{let h=1;for(;n<t&&h>0;){if(c(),n>=t)return;let b=s[n];if(b===A){h--,n++;continue}if(b==="{"||b==="["){let I=b==="{"?"}":"]";n++,p(I);continue}if(b==="."&&n+2<t&&s[n+1]==="."&&s[n+2]==="."){if(n+=3,c(),n<t&&tn(s[n])){let I=u();I&&e.push(I)}continue}if(b===","){n++;continue}if(b==="="){n++;let I=0;for(;n<t;){let C=s[n];if(C==="'"||C==='"'||C==="`"){i(C);continue}if(C==="("||C==="["||C==="{"){I++,n++;continue}if(C===")"||C==="]"||C==="}"){if(I>0){I--,n++;continue}break}if(C===","&&I===0)break;n++}continue}if(tn(b)){let I=u();if(c(),n<t&&s[n]===":"){if(n++,c(),n<t){let C=s[n];if(C==="{"||C==="["){let R=C==="{"?"}":"]";n++,p(R)}else if(tn(C)){let R=u();R&&e.push(R)}}}else I&&e.push(I);continue}n++}},d=()=>{let A=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]==="*"){l();continue}}if(h==="("||h==="["||h==="{"){A++,n++;continue}if(h===")"||h==="]"||h==="}"){if(A>0){A--,n++;continue}return!1}if(h===","&&A===0)return n++,!0;if(h===";"&&A===0)return n++,!1;if(h===`
1123
+ `&&A===0){let b=n;return n++,c(),n<t&&s[n]===","?(n++,!0):(n=b,!1)}n++}return!1},m=()=>{for(;n<t;){if(c(),n>=t)return;let A=s[n];if(A==="{"){if(n++,p("}"),!d())return;continue}if(A==="["){if(n++,p("]"),!d())return;continue}if(tn(A)){let h=u();if(h&&e.push(h),!d())return;continue}return}},g=A=>{if(A===0)return!0;let h=A-1;for(;h>=0;){let b=s[h];if(b===" "||b===" "||b==="\r"){h--;continue}return b===`
1124
+ `||b===";"||b==="{"||b==="}"}return!0};for(;n<t;){let A=s[n];if(A==="'"||A==='"'||A==="`"){i(A);continue}if(A==="/"&&n+1<t){if(s[n+1]==="/"){a();continue}if(s[n+1]==="*"){l();continue}}if(A==="{"){r++,n++;continue}if(A==="}"){r--,n++;continue}if(A==="("){o++,n++;continue}if(A===")"){o--,n++;continue}if(r===0&&o===0&&tn(A)){let h=n,b=u();(b==="var"||b==="let"||b==="const")&&n<t&&(s[n]===" "||s[n]===" "||s[n]===`
1125
+ `)&&g(h)&&m();continue}n++}let f=new Set,x=[];for(let A of e)f.has(A)||(f.add(A),x.push(A));return x}function ac(s){let e=new Set(Hr(s)),t=er(s),n=t.length,r=0,o=0,i=0,a=m=>{let g=m;for(;g<n&&/\s/.test(t[g]??"");)g++;return g},l=m=>{let g=m;for(;g>=0&&/\s/.test(t[g]??"");)g--;return g},u=m=>{let g=t.slice(m,m+3),f=t.slice(m,m+2);return g==="==="||f==="=="||f==="=>"?!1:t[m]==="="||["+=","-=","*=","/=","%=","&=","|=","^=","&&=","||=","??=","**=","<<=",">>=",">>>="].some(x=>t.startsWith(x,m))},c=m=>{let g=m;for(;g<n&&tn(t[g]);)g++;return{word:t.slice(m,g),nextIndex:g}},p=(m,g,f)=>{let x=l(g-1),A=x>=0?t[x]:void 0,h=a(f);if(A==="."||A==="?")return;let I=t.slice(Math.max(0,g-2),g)==="++"||t.slice(Math.max(0,g-2),g)==="--",C=t.startsWith("++",h)||t.startsWith("--",h);(I||C||u(h))&&e.add(m)},d=(m,g)=>{let f=a(g);if(t[f]!==".")return;let x=a(f+1);if(!ic(t[x]))return;let{word:A,nextIndex:h}=c(x),b=a(h),I=t.slice(Math.max(0,m-2),m)==="++"||t.slice(Math.max(0,m-2),m)==="--",C=t.startsWith("++",b)||t.startsWith("--",b);A&&(I||C||u(b))&&e.add(A)};for(;r<n;){let m=t[r];if(m==="{"){o++,r++;continue}if(m==="}"){o--,r++;continue}if(m==="("){i++,r++;continue}if(m===")"){i--,r++;continue}if(o===0&&i===0&&ic(m)){let g=r,{word:f,nextIndex:x}=c(r);if(r=x,!f)continue;if(f==="globalThis"){d(g,x);continue}p(f,g,x);continue}r++}return[...e]}function Cs(s){let e=typeof self<"u"?self:globalThis,t=[],n=Object.getPrototypeOf(async()=>{}).constructor,r=s.functionRefKey,o="return",i="stdout",a=/^(if|for|while|switch|try|catch|finally|function|class|import|export|throw|return|var|let|const|break|continue|debugger)\b/,l=/^\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"]},c=()=>{let y;if(typeof process<"u"&&typeof process.getBuiltinModule=="function"&&(y=process.getBuiltinModule.bind(process)),!y)try{y=new Function('return typeof require==="function"?require:undefined')()}catch{y=void 0}if(!(typeof y=="function"&&typeof process<"u"&&!!process.versions?.node))return{isNodeWorker:!1,parentPort:null};try{return{isNodeWorker:!0,parentPort:y("node:worker_threads").parentPort??null}}catch{return{isNodeWorker:!0,parentPort:null}}},{isNodeWorker:p,parentPort:d}=c(),m=()=>{if(!d&&typeof e.postMessage!="function")throw new Error("Worker transport unavailable: no postMessage channel");return{send:v=>{if(d){d.postMessage(v);return}e.postMessage(v)},setOnMessage:v=>{if(d){d.on("message",w=>v({data:w}));return}e.onmessage=v}}},{send:g,setOnMessage:f}=m(),x=y=>y&&(/\r?\n$/.test(y)?y:`${y}
1126
+ `),A=y=>{let T=y.trim();return T.startsWith("//")||T.startsWith("/*")||T.startsWith("*")},h=y=>{let T=y.length-1;for(;T>=0;){let v=y[T].trim();if(v&&!A(v))break;T-=1}return T},b=y=>{if(!y)return!0;let T=y.split(`
1127
+ `).find(v=>v.trim().length>0)?.trim();return!T||a.test(T)?!0:T.startsWith("{")&&!T.startsWith("({")||T==="}"||T==="};"||A(T)},I=(y,T)=>{let v=y,w=T.trim().replace(/;\s*$/,"");if(!w)return null;let G=w.lastIndexOf(";");if(G!==-1){let K=w.slice(G+1).trim(),Q=w.slice(0,G).trim();K&&(K.startsWith("//")||K.startsWith("/*")?Q&&(w=Q):(Q&&(v=v?`${v}
1128
+ ${Q};`:`${Q};`),w=K))}return b(w)?null:{head:v,expression:w}},C=(y,T,v)=>{let w=y.slice(0,T).join(`
1129
+ `),G=y.slice(T,v+1).join(`
1130
+ `).trim();if(!G)return null;if(!G.includes(`
1131
+ `)){let K=I(w,G);return K?K.head?`${K.head}
1004
1132
  return (
1005
- ${z.expression}
1133
+ ${K.expression}
1006
1134
  );`:`return (
1007
- ${z.expression}
1008
- );`:null}return y($)?null:T?`${T}
1135
+ ${K.expression}
1136
+ );`:null}return b(G)?null:w?`${w}
1009
1137
  return (
1010
- ${$}
1138
+ ${G}
1011
1139
  );`:`return (
1012
- ${$}
1013
- );`},R=A=>{try{return new n(A),!0}catch{return!1}},P=A=>{let w=A.split(`
1014
- `),S=h(w);if(S<0)return A;let T=new Set;for(let $=S;$>=0;$-=1){let z=w[$]??"";if(!z.trim()||b(z))continue;let J=C(w,$,S);if(J&&!T.has(J)&&(T.add(J),R(J)))return J}return A},v=A=>{let w=l.exec(A);return w&&(w[1]||"").trim()||A},E=A=>{let w=[],S=A.length,T=0,$=0,z=0,J=Z=>Z>="a"&&Z<="z"||Z>="A"&&Z<="Z"||Z>="0"&&Z<="9"||Z==="_"||Z==="$",B=Z=>{if(T++,Z==="`"){let te=0;for(;T<S;){let de=A[T];if(de==="\\"){T+=2;continue}if(te>0){de==="{"?te++:de==="}"&&te--,T++;continue}if(de==="$"&&T+1<S&&A[T+1]==="{"){te++,T+=2;continue}if(de==="`"){T++;return}T++}}else for(;T<S;){let te=A[T];if(te==="\\"){T+=2;continue}if(te===Z){T++;return}T++}},X=()=>{for(T+=2;T<S&&A[T]!==`
1015
- `;)T++},xe=()=>{for(T+=2;T<S;){if(A[T]==="*"&&T+1<S&&A[T+1]==="/"){T+=2;return}T++}},_e=()=>{let Z=T;for(;T<S&&J(A[T]);)T++;return A.slice(Z,T)},se=()=>{let Z=T;for(;T<S;){let te=A[T];if(te===" "||te===" "||te===`
1016
- `||te==="\r"){T++;continue}if(te==="/"&&T+1<S){if(A[T+1]==="/"){X();continue}if(A[T+1]==="*"){xe();continue}}break}return T>Z},Ce=Z=>{let te=1;for(;T<S&&te>0;){if(se(),T>=S)return;let de=A[T];if(de===Z){te--,T++;continue}if(de==="{"||de==="["){let Le=de==="{"?"}":"]";T++,te++,T--,te--,T++,Ce(Le);continue}if(de==="."&&T+2<S&&A[T+1]==="."&&A[T+2]==="."){if(T+=3,se(),T<S&&J(A[T])){let Le=_e();Le&&w.push(Le)}continue}if(de===","){T++;continue}if(de==="="){T++;let Le=0;for(;T<S;){let Ue=A[T];if(Ue==="'"||Ue==='"'||Ue==="`"){B(Ue);continue}if(Ue==="("||Ue==="["||Ue==="{"){Le++,T++;continue}if(Ue===")"||Ue==="]"||Ue==="}"){if(Le>0){Le--,T++;continue}break}if(Ue===","&&Le===0)break;T++}continue}if(J(de)){let Le=_e();if(se(),T<S&&A[T]===":"){if(T++,se(),T<S){let Ue=A[T];if(Ue==="{"||Ue==="["){let jt=Ue==="{"?"}":"]";T++,Ce(jt)}else if(J(Ue)){let jt=_e();jt&&w.push(jt)}}}else Le&&w.push(Le);continue}T++}},Ne=()=>{let Z=0;for(;T<S;){let te=A[T];if(te==="'"||te==='"'||te==="`"){B(te);continue}if(te==="/"&&T+1<S){if(A[T+1]==="/"){X();continue}if(A[T+1]==="*"){xe();continue}}if(te==="("||te==="["||te==="{"){Z++,T++;continue}if(te===")"||te==="]"||te==="}"){if(Z>0){Z--,T++;continue}return!1}if(te===","&&Z===0)return T++,!0;if(te===";"&&Z===0)return T++,!1;if(te===`
1017
- `&&Z===0){let de=T;return T++,se(),T<S&&A[T]===","?(T++,!0):(T=de,!1)}T++}return!1},Oe=()=>{for(;T<S;){if(se(),T>=S)return;let Z=A[T];if(Z==="{"){if(T++,Ce("}"),!Ne())return;continue}if(Z==="["){if(T++,Ce("]"),!Ne())return;continue}if(J(Z)){let te=_e();if(te&&w.push(te),!Ne())return;continue}return}},vt=Z=>{if(Z===0)return!0;let te=Z-1;for(;te>=0;){let de=A[te];if(de===" "||de===" "||de==="\r"){te--;continue}return de===`
1018
- `||de===";"||de==="{"||de==="}"}return!0};for(;T<S;){let Z=A[T];if(Z==="'"||Z==='"'||Z==="`"){B(Z);continue}if(Z==="/"&&T+1<S){if(A[T+1]==="/"){X();continue}if(A[T+1]==="*"){xe();continue}}if(Z==="{"){$++,T++;continue}if(Z==="}"){$--,T++;continue}if(Z==="("){z++,T++;continue}if(Z===")"){z--,T++;continue}if($===0&&z===0&&J(Z)){let te=T,de=_e();if((de==="var"||de==="let"||de==="const")&&T<S&&(A[T]===" "||A[T]===" "||A[T]===`
1019
- `)&&vt(te)){Oe();continue}continue}T++}let Ct=new Set,Pt=[];for(let Z of w)Ct.has(Z)||(Ct.add(Z),Pt.push(Z));return Pt},k=A=>A.length===0?"":`
1020
- try { ${A.map(S=>`globalThis[${JSON.stringify(S)}] = ${S};`).join(" ")} } catch (_ax_e) {} void 0;`,O=A=>{if(typeof A=="string")return A;try{return JSON.stringify(A,null,2)}catch{return String(A)}},L=(A,w)=>{let S=e.console&&typeof e.console=="object"?e.console:null,T=S?.[A],$=typeof T=="function"?(...J)=>T.apply(S,J):null,z=(...J)=>{w.push(J.map(O).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[A]=z,()=>{if(!(!e.console||typeof e.console!="object")){if($){e.console[A]=$;return}try{delete e.console[A]}catch{e.console[A]=void 0}}}},F=A=>{let w=[];return re&&(w.push(L("log",A)),w.push(L("info",A)),w.push(L("warn",A)),w.push(L("error",A))),w},M=()=>{let A=[],w=F(A),S=e.print;return W===i&&(e.print=(...$)=>{A.push($.map(O).join(" "))}),{output:A,cleanup:()=>{for(let $ of w)try{$()}catch{}if(W===i)if(S===void 0)try{delete e.print}catch{e.print=void 0}else e.print=S}}},_=A=>{for(let w of A)try{Object.defineProperty(e,w,{value:void 0,writable:!1,configurable:!1})}catch{}},j=A=>{let w=new Set(Array.isArray(A)?A:[]);for(let[S,T]of Object.entries(u))w.has(S)||_(T)},H=A=>{p&&!A&&_(["process","require"])},D=s.maxErrorCauseDepth,Y=(A,w=0,S=new Set)=>{if(w>D)return{name:"Error",message:"[cause chain truncated]"};if(A&&typeof A=="object"){if(S.has(A))return{name:"Error",message:"[circular]"};S.add(A)}let T=A,$=T?.name!=null?String(T.name):"Error",z=T?.message!=null?String(T.message):ge(A),J=typeof T?.stack=="string"?T.stack:void 0,B;if(typeof T?.cause<"u"&&w<D)try{let xe=T.cause;xe instanceof Error||xe&&typeof xe=="object"&&("message"in xe||"name"in xe)?B=Y(xe,w+1,S):B={name:"Error",message:ge(xe)}}catch{B={name:"Error",message:ge(T.cause)}}let X={name:$,message:z};if(J!==void 0&&(X.stack=J),B!==void 0&&(X.cause=B),typeof T?.data<"u")try{X.data=typeof structuredClone=="function"?structuredClone(T.data):T.data}catch{}return X},G=A=>{if(typeof A=="string")return new Error(A);if(!A||typeof A!="object")return new Error(String(A));let w=A,S=new Error(w.message!=null?String(w.message):"");return S.name=w.name!=null?String(w.name):"Error",typeof w.stack=="string"&&(S.stack=w.stack),w.cause!==void 0&&(S.cause=G(w.cause)),w.data!==void 0&&(S.data=w.data),S},me=A=>{let w=globalThis.AggregateError,S=typeof w=="function"&&A instanceof w;return A instanceof SyntaxError||A instanceof TypeError||A instanceof RangeError||A instanceof ReferenceError||S||A instanceof EvalError||A instanceof URIError},ge=A=>{if(A==null||typeof A!="object")return String(A);try{return JSON.stringify(A,null,2)}catch{return String(A)}},Ae=(A,w,S=0)=>{let T=A,$=T?.name!=null?String(T.name):"Error",z=T?.message!=null?String(T.message):ge(A),J=[`${$}: ${z}`],B;if(typeof T?.stack=="string"){let X=T.stack.match(/<anonymous>:(\d+):(\d+)/);X&&(B=Math.max(1,Number(X[1])-S),J.push(` at line ${B}, column ${X[2]}`))}if(w&&B!==void 0&&B>=1&&B<=w.split(`
1021
- `).length){let X=w.split(`
1022
- `),xe=Math.max(0,B-2),_e=Math.min(X.length,B+1),Ce=X.slice(xe,_e).map((Ne,Oe)=>` ${String(xe+Oe+1).padStart(3)}| ${Ne}`).join(`
1023
- `);J.push(`Source:
1024
- ${Ce}`)}if(T?.data!==void 0&&J.push(`Data: ${ge(T.data)}`),T?.cause!==void 0){let X=(xe,_e)=>{if(_e>4)return"[cause chain truncated]";let se=xe,Ce=se?.name!=null?String(se.name):"Error",Ne=se?.message!=null?String(se.message):ge(xe),Oe=[`${Ce}: ${Ne}`];return se?.data!==void 0&&Oe.push(`Data: ${ge(se.data)}`),se?.cause!==void 0&&Oe.push(`Caused by: ${X(se.cause,_e+1)}`),Oe.join(`
1025
- `)};J.push(`Caused by: ${X(T.cause,1)}`)}return J.join(`
1026
- `)},U=new Map,K=0,W=o,re=!1,Q=[],le=A=>{if(!A||typeof A!="object"||Array.isArray(A))return!1;let w=Object.getPrototypeOf(A);return w===Object.prototype||w===null},Ie=A=>typeof A=="string"&&A.trim().length>0,Ee=A=>{if(!Ie(A)){if(!le(A))throw new Error("ask_clarification() choice entries must be non-empty strings or objects with a non-empty label");if(!Ie(A.label))throw new Error("ask_clarification() choice objects require a non-empty label");if(A.value!==void 0&&!Ie(A.value))throw new Error("ask_clarification() choice object values must be non-empty strings")}},Me=A=>{if(Ie(A))return;if(!le(A))throw new Error("ask_clarification() requires a non-empty string or an object payload");if(!Ie(A.question))throw new Error("ask_clarification() object payload requires a non-empty question");let w=new Set(["text","number","date","single_choice","multiple_choice"]),S;if(A.type===void 0)S=Array.isArray(A.choices)&&A.choices.length>0?"single_choice":void 0;else{if(typeof A.type!="string"||!w.has(A.type))throw new Error("ask_clarification() object payload type must be one of: text, number, date, single_choice, multiple_choice");S=A.type}let T=S==="single_choice"||S==="multiple_choice",$=A.choices;if($!==void 0){if(!Array.isArray($)||$.length===0)throw new Error("ask_clarification() choices must be a non-empty array when provided");for(let z of $)Ee(z)}else if(T)throw new Error("ask_clarification() choice payloads require a non-empty choices array")},oe=(A,w)=>{if(A==="final"){if(w.length===0)throw new Error("final() requires at least one argument");return}if(A==="ask_clarification"){if(w.length!==1)throw new Error("ask_clarification() requires exactly one argument");Me(w[0])}},Re=A=>{if(A==="final"||A==="ask_clarification")return A;let S=/^fn_\d+_(.+)$/.exec(A)?.[1];if(S==="final"||S==="ask_clarification")return S},q=A=>(...w)=>{let S=Re(A);S&&oe(S,w);let T=++K,$=!1,z=new Promise((se,Ce)=>{U.set(T,{resolve:se,reject:Ce}),g({type:"fn-call",id:T,name:A,args:w})}),J=z.then.bind(z),B=z.catch.bind(z),X=z.finally.bind(z),xe=()=>{$=!0},_e=new Proxy(z,{get(se,Ce,Ne){return Ce==="then"?(...Oe)=>(xe(),J(...Oe)):Ce==="catch"?(...Oe)=>(xe(),B(...Oe)):Ce==="finally"?(...Oe)=>(xe(),X(...Oe)):Reflect.get(se,Ce,Ne)}});return z.catch(se=>{$||Q.push(se)}),_e},ce=async()=>{for(let A=0;A<50&&U.size>0;A+=1)await new Promise(w=>{setTimeout(w,0)})},fe=A=>{if(!A||typeof A!="object")return A;if(Array.isArray(A)){for(let S=0;S<A.length;S+=1)A[S]=fe(A[S]);return A}let w=A;if(r in w){let S=w[r];return typeof S=="string"?q(S):void 0}for(let[S,T]of Object.entries(w))w[S]=fe(T);return A},he=A=>{for(let[w,S]of Object.entries(A)){let T=fe(S),$=e[w];if(le($)&&le(T)){for(let z of Object.keys($))Object.hasOwn(T,z)||delete $[z];for(let[z,J]of Object.entries(T))$[z]=J;continue}e[w]=T}},we=A=>{if(A.globals&&typeof A.globals=="object"&&he(A.globals),Array.isArray(A.fnNames))for(let w of A.fnNames)typeof w=="string"&&(e[w]=q(w))},ke=async A=>{let w=x(A),S=[];try{S=E(A)}catch{S=[]}let T=k(S),$=w;try{$=P(w)}catch{$=w}let z=$;if(T){let X=$.lastIndexOf(`
1027
- return (`);X!==-1?z=$.slice(0,X)+T+$.slice(X):z=$+T}let J=R(z)?z:R($)?$:w;return await new n(J)()},It=A=>{let w=v(A),S=[];try{S=E(A)}catch{S=[]}let T=k(S),$=T?w+T:w;return(0,eval)($)},Ye=(A,w)=>{if(W!==i)return A;let S=w.join(`
1028
- `).trim();return S||A},rt=(A,w)=>A.length<=w?A:`${A.slice(0,w-3)}...`,ve=A=>{if(A===null)return"null";if(A===void 0)return"undefined";let w=typeof A;if(typeof A=="string")return JSON.stringify(rt(A,40));if(w==="number"||w==="boolean"||w==="bigint"||w==="symbol")return String(A);if(w==="function")return`[function ${(A.name&&typeof A.name=="string"?A.name??"":"")||"anonymous"}]`;if(Array.isArray(A))return`[array(${A.length})]`;if(A instanceof Date)return Number.isFinite(A.getTime())?A.toISOString():String(A);if(A instanceof Error)return`${A.name||"Error"}: ${A.message||""}`;if(A instanceof Map)return`[map(${A.size})]`;if(A instanceof Set)return`[set(${A.size})]`;let S=A&&typeof A=="object"&&"constructor"in A&&A.constructor&&typeof A.constructor.name=="string"?A.constructor.name??"":"";return S&&S!=="Object"?`[${S}]`:"[object]"},Ke=A=>{if(A===null)return{type:"null"};if(Array.isArray(A))return{type:"array",ctor:"Array"};if(A instanceof Map)return{type:"map",ctor:"Map"};if(A instanceof Set)return{type:"set",ctor:"Set"};if(A instanceof Date)return{type:"date",ctor:"Date"};if(A instanceof Error)return{type:"error",ctor:typeof A.name=="string"&&A.name.trim()?A.name:"Error"};let w=typeof A;return w!=="object"?{type:w}:{type:"object",ctor:A&&typeof A=="object"&&"constructor"in A&&A.constructor&&typeof A.constructor.name=="string"?A.constructor.name??void 0:void 0}},N=(A,w)=>{if(w==="string")return`${A.length} chars`;if(w==="array")return`${A.length} items`;if(w==="map"||w==="set")return`${A.size} items`;if(w==="object"&&A&&typeof A=="object")return`${Object.keys(A).length} keys`},V=(A,w,S)=>{if(w==="array")return"["+A.slice(0,3).map($=>ve($)).join(", ")+(A.length>3?", ...":"")+"]";if(w==="map"){let T=A,$=Array.from(T.entries()).slice(0,3).map(([z,J])=>`${ve(z)} => ${ve(J)}`);return"Map("+T.size+") {"+$.join(", ")+(T.size>3?", ...":"")+"}"}if(w==="set"){let T=A,$=Array.from(T.values()).slice(0,5).map(z=>ve(z));return"Set("+T.size+") {"+$.join(", ")+(T.size>5?", ...":"")+"}"}if(w==="date"||w==="error"||w==="function")return ve(A);if(w==="object"&&A&&typeof A=="object"){let T=Object.keys(A),$=T.slice(0,4);return(S&&S!=="Object"?`${S} `:"")+"{"+$.join(", ")+(T.length>$.length?", ...":"")+"}"}return ve(A)},ne=A=>{let w=new Set([...t,...A??[]]),S=Object.getOwnPropertyNames(e).filter(T=>!w.has(T)&&!T.startsWith("_")).sort().flatMap(T=>{try{let $=Object.getOwnPropertyDescriptor(e,T);if(!$)return[];if("get"in $&&typeof $.get=="function"&&!("value"in $))return[{name:T,type:"accessor",preview:"[getter omitted]"}];let z="value"in $?$.value:e[T],J=Ke(z),B=N(z,J.type),X=V(z,J.type,J.ctor);return[{name:T,type:J.type,...J.ctor?{ctor:J.ctor}:{},...B?{size:B}:{},...X?{preview:rt(X,96)}:{}}]}catch{return[{name:T,type:"unknown",preview:"[unavailable]"}]}});return JSON.stringify({version:1,entries:S})},Te=A=>{if(typeof structuredClone=="function")try{return structuredClone(A),!0}catch{return!1}try{return JSON.stringify(A),!0}catch{return!1}},ze=A=>{let w=new Set([...t,...A??[]]),S={};return{version:1,entries:Object.getOwnPropertyNames(e).filter($=>!w.has($)&&!$.startsWith("_")).sort().flatMap($=>{try{let z=Object.getOwnPropertyDescriptor(e,$);if(!z)return[];if("get"in z&&typeof z.get=="function"&&!("value"in z))return[{name:$,type:"accessor",preview:"[getter omitted]",restorable:!1}];let J="value"in z?z.value:e[$],B=Ke(J),X=N(J,B.type),xe=V(J,B.type,B.ctor),_e=Te(J);return _e&&(S[$]=typeof structuredClone=="function"?structuredClone(J):J),[{name:$,type:B.type,...B.ctor?{ctor:B.ctor}:{},...X?{size:X}:{},...xe?{preview:rt(xe,96)}:{},restorable:_e}]}catch{return[{name:$,type:"unknown",preview:"[unavailable]",restorable:!1}]}}),bindings:S}};f(async A=>{let w=A.data;if(!w||typeof w!="object"||typeof w.type!="string")return;if(w.type==="init"){W=w.outputMode===i?i:o,re=w.captureConsole!==void 0?!!w.captureConsole:W===i;let B=w.allowUnsafeNodeHostAccess===!0;we(w),j(w.permissions),H(B),t=Object.getOwnPropertyNames(e).sort();return}if(w.type==="fn-result"){if(typeof w.id!="number")return;let B=U.get(w.id);B&&(U.delete(w.id),w.error!==void 0?B.reject(G(w.error)):B.resolve(w.value));return}if(w.type==="update-globals"){if(typeof w.id!="number")return;try{w.globals&&typeof w.globals=="object"&&he(w.globals),g({type:"result",id:w.id,value:void 0})}catch(B){g({type:"result",id:w.id,error:Y(B)})}return}if(w.type==="inspect-globals"){if(typeof w.id!="number")return;try{let B=Array.isArray(w.reservedNames)?w.reservedNames.filter(X=>typeof X=="string"):void 0;g({type:"result",id:w.id,value:ne(B)})}catch(B){g({type:"result",id:w.id,error:Y(B)})}return}if(w.type==="snapshot-globals"){if(typeof w.id!="number")return;try{let B=Array.isArray(w.reservedNames)?w.reservedNames.filter(X=>typeof X=="string"):void 0;g({type:"result",id:w.id,value:ze(B)})}catch(B){g({type:"result",id:w.id,error:Y(B)})}return}if(w.type!=="execute"||typeof w.id!="number"||typeof w.code!="string")return;let S=w.id,T=w.code,$=/\bawait\b/.test(T),{output:z,cleanup:J}=M();try{Q.length=0;let B=$?await ke(T):It(T);if(U.size>0&&await ce(),await Promise.resolve(),Q.length>0)throw Q[0];let X=Ye(B,z);try{g({type:"result",id:S,value:X})}catch{g({type:"result",id:S,value:String(X)})}}catch(B){me(B)?g({type:"result",id:S,value:Ae(B,T,$?2:0)}):g({type:"result",id:S,error:Y(B)})}finally{J()}})}var Zi=4,ec=16,ea="__ax_rlm_fn_ref__",ta=16;function tc(){let s={functionRefKey:ea,maxErrorCauseDepth:ta},e=`(${xs.toString()})(${JSON.stringify(s)});
1140
+ ${G}
1141
+ );`},R=y=>{try{return new n(y),!0}catch{return!1}},M=y=>{let T=y.split(`
1142
+ `),v=h(T);if(v<0)return y;let w=new Set;for(let G=v;G>=0;G-=1){let K=T[G]??"";if(!K.trim()||A(K))continue;let Q=C(T,G,v);if(Q&&!w.has(Q)&&(w.add(Q),R(Q)))return Q}return y},S=y=>{let T=l.exec(y);return T&&(T[1]||"").trim()||y},O=y=>{let T=[],v=y.length,w=0,G=0,K=0,Q=Y=>Y>="a"&&Y<="z"||Y>="A"&&Y<="Z"||Y>="0"&&Y<="9"||Y==="_"||Y==="$",V=Y=>{if(w++,Y==="`"){let J=0;for(;w<v;){let ce=y[w];if(ce==="\\"){w+=2;continue}if(J>0){ce==="{"?J++:ce==="}"&&J--,w++;continue}if(ce==="$"&&w+1<v&&y[w+1]==="{"){J++,w+=2;continue}if(ce==="`"){w++;return}w++}}else for(;w<v;){let J=y[w];if(J==="\\"){w+=2;continue}if(J===Y){w++;return}w++}},F=()=>{for(w+=2;w<v&&y[w]!==`
1143
+ `;)w++},j=()=>{for(w+=2;w<v;){if(y[w]==="*"&&w+1<v&&y[w+1]==="/"){w+=2;return}w++}},X=()=>{let Y=w;for(;w<v&&Q(y[w]);)w++;return y.slice(Y,w)},Z=()=>{let Y=w;for(;w<v;){let J=y[w];if(J===" "||J===" "||J===`
1144
+ `||J==="\r"){w++;continue}if(J==="/"&&w+1<v){if(y[w+1]==="/"){F();continue}if(y[w+1]==="*"){j();continue}}break}return w>Y},xe=Y=>{let J=1;for(;w<v&&J>0;){if(Z(),w>=v)return;let ce=y[w];if(ce===Y){J--,w++;continue}if(ce==="{"||ce==="["){let Ie=ce==="{"?"}":"]";w++,J++,w--,J--,w++,xe(Ie);continue}if(ce==="."&&w+2<v&&y[w+1]==="."&&y[w+2]==="."){if(w+=3,Z(),w<v&&Q(y[w])){let Ie=X();Ie&&T.push(Ie)}continue}if(ce===","){w++;continue}if(ce==="="){w++;let Ie=0;for(;w<v;){let Re=y[w];if(Re==="'"||Re==='"'||Re==="`"){V(Re);continue}if(Re==="("||Re==="["||Re==="{"){Ie++,w++;continue}if(Re===")"||Re==="]"||Re==="}"){if(Ie>0){Ie--,w++;continue}break}if(Re===","&&Ie===0)break;w++}continue}if(Q(ce)){let Ie=X();if(Z(),w<v&&y[w]===":"){if(w++,Z(),w<v){let Re=y[w];if(Re==="{"||Re==="["){let st=Re==="{"?"}":"]";w++,xe(st)}else if(Q(Re)){let st=X();st&&T.push(st)}}}else Ie&&T.push(Ie);continue}w++}},$e=()=>{let Y=0;for(;w<v;){let J=y[w];if(J==="'"||J==='"'||J==="`"){V(J);continue}if(J==="/"&&w+1<v){if(y[w+1]==="/"){F();continue}if(y[w+1]==="*"){j();continue}}if(J==="("||J==="["||J==="{"){Y++,w++;continue}if(J===")"||J==="]"||J==="}"){if(Y>0){Y--,w++;continue}return!1}if(J===","&&Y===0)return w++,!0;if(J===";"&&Y===0)return w++,!1;if(J===`
1145
+ `&&Y===0){let ce=w;return w++,Z(),w<v&&y[w]===","?(w++,!0):(w=ce,!1)}w++}return!1},me=()=>{for(;w<v;){if(Z(),w>=v)return;let Y=y[w];if(Y==="{"){if(w++,xe("}"),!$e())return;continue}if(Y==="["){if(w++,xe("]"),!$e())return;continue}if(Q(Y)){let J=X();if(J&&T.push(J),!$e())return;continue}return}},je=Y=>{if(Y===0)return!0;let J=Y-1;for(;J>=0;){let ce=y[J];if(ce===" "||ce===" "||ce==="\r"){J--;continue}return ce===`
1146
+ `||ce===";"||ce==="{"||ce==="}"}return!0};for(;w<v;){let Y=y[w];if(Y==="'"||Y==='"'||Y==="`"){V(Y);continue}if(Y==="/"&&w+1<v){if(y[w+1]==="/"){F();continue}if(y[w+1]==="*"){j();continue}}if(Y==="{"){G++,w++;continue}if(Y==="}"){G--,w++;continue}if(Y==="("){K++,w++;continue}if(Y===")"){K--,w++;continue}if(G===0&&K===0&&Q(Y)){let J=w,ce=X();if((ce==="var"||ce==="let"||ce==="const")&&w<v&&(y[w]===" "||y[w]===" "||y[w]===`
1147
+ `)&&je(J)){me();continue}continue}w++}let Me=new Set,Ze=[];for(let Y of T)Me.has(Y)||(Me.add(Y),Ze.push(Y));return Ze},k=y=>y.length===0?"":`
1148
+ try { ${y.map(v=>`globalThis[${JSON.stringify(v)}] = ${v};`).join(" ")} } catch (_ax_e) {} void 0;`,_=y=>{if(typeof y=="string")return y;try{return JSON.stringify(y,null,2)}catch{return String(y)}},L=(y,T)=>{let v=e.console&&typeof e.console=="object"?e.console:null,w=v?.[y],G=typeof w=="function"?(...Q)=>w.apply(v,Q):null,K=(...Q)=>{T.push(Q.map(_).join(" "))};return(!e.console||typeof e.console!="object")&&(e.console={}),e.console[y]=K,()=>{if(!(!e.console||typeof e.console!="object")){if(G){e.console[y]=G;return}try{delete e.console[y]}catch{e.console[y]=void 0}}}},E=y=>{let T=[];return he&&(T.push(L("log",y)),T.push(L("info",y)),T.push(L("warn",y)),T.push(L("error",y))),T},P=()=>{let y=[],T=E(y),v=e.print;return W===i&&(e.print=(...G)=>{y.push(G.map(_).join(" "))}),{output:y,cleanup:()=>{for(let G of T)try{G()}catch{}if(W===i)if(v===void 0)try{delete e.print}catch{e.print=void 0}else e.print=v}}},$=y=>{for(let T of y)try{Object.defineProperty(e,T,{value:void 0,writable:!1,configurable:!1})}catch{}},D=y=>{let T=new Set(Array.isArray(y)?y:[]);for(let[v,w]of Object.entries(u))T.has(v)||$(w)},z=y=>{p&&!y&&$(["process","require"])},U=s.maxErrorCauseDepth,te=(y,T=0,v=new Set)=>{if(T>U)return{name:"Error",message:"[cause chain truncated]"};if(y&&typeof y=="object"){if(v.has(y))return{name:"Error",message:"[circular]"};v.add(y)}let w=y,G=w?.name!=null?String(w.name):"Error",K=w?.message!=null?String(w.message):Ce(y),Q=typeof w?.stack=="string"?w.stack:void 0,V;if(typeof w?.cause<"u"&&T<U)try{let j=w.cause;j instanceof Error||j&&typeof j=="object"&&("message"in j||"name"in j)?V=te(j,T+1,v):V={name:"Error",message:Ce(j)}}catch{V={name:"Error",message:Ce(w.cause)}}let F={name:G,message:K};if(Q!==void 0&&(F.stack=Q),V!==void 0&&(F.cause=V),typeof w?.data<"u")try{F.data=typeof structuredClone=="function"?structuredClone(w.data):w.data}catch{}return F},B=y=>{if(typeof y=="string")return new Error(y);if(!y||typeof y!="object")return new Error(String(y));let T=y,v=new Error(T.message!=null?String(T.message):"");return v.name=T.name!=null?String(T.name):"Error",typeof T.stack=="string"&&(v.stack=T.stack),T.cause!==void 0&&(v.cause=B(T.cause)),T.data!==void 0&&(v.data=T.data),v},ge=y=>{let T=globalThis.AggregateError,v=typeof T=="function"&&y instanceof T;return y instanceof SyntaxError||y instanceof TypeError||y instanceof RangeError||y instanceof ReferenceError||v||y instanceof EvalError||y instanceof URIError},Ce=y=>{if(y==null||typeof y!="object")return String(y);try{return JSON.stringify(y,null,2)}catch{return String(y)}},ke=(y,T,v=0)=>{let w=y,G=w?.name!=null?String(w.name):"Error",K=w?.message!=null?String(w.message):Ce(y),Q=[`${G}: ${K}`],V;if(typeof w?.stack=="string"){let F=w.stack.match(/<anonymous>:(\d+):(\d+)/);F&&(V=Math.max(1,Number(F[1])-v),Q.push(` at line ${V}, column ${F[2]}`))}if(T&&V!==void 0&&V>=1&&V<=T.split(`
1149
+ `).length){let F=T.split(`
1150
+ `),j=Math.max(0,V-2),X=Math.min(F.length,V+1),xe=F.slice(j,X).map(($e,me)=>` ${String(j+me+1).padStart(3)}| ${$e}`).join(`
1151
+ `);Q.push(`Source:
1152
+ ${xe}`)}if(w?.data!==void 0&&Q.push(`Data: ${Ce(w.data)}`),w?.cause!==void 0){let F=(j,X)=>{if(X>4)return"[cause chain truncated]";let Z=j,xe=Z?.name!=null?String(Z.name):"Error",$e=Z?.message!=null?String(Z.message):Ce(j),me=[`${xe}: ${$e}`];return Z?.data!==void 0&&me.push(`Data: ${Ce(Z.data)}`),Z?.cause!==void 0&&me.push(`Caused by: ${F(Z.cause,X+1)}`),me.join(`
1153
+ `)};Q.push(`Caused by: ${F(w.cause,1)}`)}return Q.join(`
1154
+ `)},N=new Map,q=0,W=o,he=!1,ee=[],ie=y=>{if(!y||typeof y!="object"||Array.isArray(y))return!1;let T=Object.getPrototypeOf(y);return T===Object.prototype||T===null},fe=y=>typeof y=="string"&&y.trim().length>0,Pe=y=>{if(!fe(y)){if(!ie(y))throw new Error("ask_clarification() choice entries must be non-empty strings or objects with a non-empty label");if(!fe(y.label))throw new Error("ask_clarification() choice objects require a non-empty label");if(y.value!==void 0&&!fe(y.value))throw new Error("ask_clarification() choice object values must be non-empty strings")}},Ne=y=>{if(fe(y))return;if(!ie(y))throw new Error("ask_clarification() requires a non-empty string or an object payload");if(!fe(y.question))throw new Error("ask_clarification() object payload requires a non-empty question");let T=new Set(["text","number","date","single_choice","multiple_choice"]),v;if(y.type===void 0)v=Array.isArray(y.choices)&&y.choices.length>0?"single_choice":void 0;else{if(typeof y.type!="string"||!T.has(y.type))throw new Error("ask_clarification() object payload type must be one of: text, number, date, single_choice, multiple_choice");v=y.type}let w=v==="single_choice"||v==="multiple_choice",G=y.choices;if(G!==void 0){if(!Array.isArray(G)||G.length===0)throw new Error("ask_clarification() choices must be a non-empty array when provided");for(let K of G)Pe(K)}else if(w)throw new Error("ask_clarification() choice payloads require a non-empty choices array")},oe=(y,T)=>{if(y==="final"){if(T.length===0)throw new Error("final() requires at least one argument");return}if(y==="ask_clarification"){if(T.length!==1)throw new Error("ask_clarification() requires exactly one argument");Ne(T[0])}},ve=y=>{if(y==="final"||y==="ask_clarification")return y;let v=/^fn_\d+_(.+)$/.exec(y)?.[1];if(v==="final"||v==="ask_clarification")return v},H=y=>(...T)=>{let v=ve(y);v&&oe(v,T);let w=++q,G=!1,K=new Promise((Z,xe)=>{N.set(w,{resolve:Z,reject:xe}),g({type:"fn-call",id:w,name:y,args:T})}),Q=K.then.bind(K),V=K.catch.bind(K),F=K.finally.bind(K),j=()=>{G=!0},X=new Proxy(K,{get(Z,xe,$e){return xe==="then"?(...me)=>(j(),Q(...me)):xe==="catch"?(...me)=>(j(),V(...me)):xe==="finally"?(...me)=>(j(),F(...me)):Reflect.get(Z,xe,$e)}});return K.catch(Z=>{G||ee.push(Z)}),X},re=async()=>{for(let y=0;y<50&&N.size>0;y+=1)await new Promise(T=>{setTimeout(T,0)})},de=y=>{if(!y||typeof y!="object")return y;if(Array.isArray(y)){for(let v=0;v<y.length;v+=1)y[v]=de(y[v]);return y}let T=y;if(r in T){let v=T[r];return typeof v=="string"?H(v):void 0}for(let[v,w]of Object.entries(T))T[v]=de(w);return y},ye=y=>{for(let[T,v]of Object.entries(y)){let w=de(v),G=e[T];if(ie(G)&&ie(w)){for(let K of Object.keys(G))Object.hasOwn(w,K)||delete G[K];for(let[K,Q]of Object.entries(w))G[K]=Q;continue}e[T]=w}},we=y=>{if(y.globals&&typeof y.globals=="object"&&ye(y.globals),Array.isArray(y.fnNames))for(let T of y.fnNames)typeof T=="string"&&(e[T]=H(T))},Oe=async y=>{let T=x(y),v=[];try{v=O(y)}catch{v=[]}let w=k(v),G=T;try{G=M(T)}catch{G=T}let K=G;if(w){let F=G.lastIndexOf(`
1155
+ return (`);F!==-1?K=G.slice(0,F)+w+G.slice(F):K=G+w}let Q=R(K)?K:R(G)?G:T;return await new n(Q)()},lt=y=>{let T=S(y),v=[];try{v=O(y)}catch{v=[]}let w=k(v),G=w?T+w:T;return(0,eval)(G)},Ue=(y,T)=>{if(W!==i)return y;let v=T.join(`
1156
+ `).trim();return v||y},rt=(y,T)=>y.length<=T?y:`${y.slice(0,T-3)}...`,Te=y=>{if(y===null)return"null";if(y===void 0)return"undefined";let T=typeof y;if(typeof y=="string")return JSON.stringify(rt(y,40));if(T==="number"||T==="boolean"||T==="bigint"||T==="symbol")return String(y);if(T==="function")return`[function ${(y.name&&typeof y.name=="string"?y.name??"":"")||"anonymous"}]`;if(Array.isArray(y))return`[array(${y.length})]`;if(y instanceof Date)return Number.isFinite(y.getTime())?y.toISOString():String(y);if(y instanceof Error)return`${y.name||"Error"}: ${y.message||""}`;if(y instanceof Map)return`[map(${y.size})]`;if(y instanceof Set)return`[set(${y.size})]`;let v=y&&typeof y=="object"&&"constructor"in y&&y.constructor&&typeof y.constructor.name=="string"?y.constructor.name??"":"";return v&&v!=="Object"?`[${v}]`:"[object]"},Be=y=>{if(y===null)return{type:"null"};if(Array.isArray(y))return{type:"array",ctor:"Array"};if(y instanceof Map)return{type:"map",ctor:"Map"};if(y instanceof Set)return{type:"set",ctor:"Set"};if(y instanceof Date)return{type:"date",ctor:"Date"};if(y instanceof Error)return{type:"error",ctor:typeof y.name=="string"&&y.name.trim()?y.name:"Error"};let T=typeof y;return T!=="object"?{type:T}:{type:"object",ctor:y&&typeof y=="object"&&"constructor"in y&&y.constructor&&typeof y.constructor.name=="string"?y.constructor.name??void 0:void 0}},se=(y,T)=>{if(T==="string")return`${y.length} chars`;if(T==="array")return`${y.length} items`;if(T==="map"||T==="set")return`${y.size} items`;if(T==="object"&&y&&typeof y=="object")return`${Object.keys(y).length} keys`},Le=(y,T,v)=>{if(T==="array")return"["+y.slice(0,3).map(G=>Te(G)).join(", ")+(y.length>3?", ...":"")+"]";if(T==="map"){let w=y,G=Array.from(w.entries()).slice(0,3).map(([K,Q])=>`${Te(K)} => ${Te(Q)}`);return"Map("+w.size+") {"+G.join(", ")+(w.size>3?", ...":"")+"}"}if(T==="set"){let w=y,G=Array.from(w.values()).slice(0,5).map(K=>Te(K));return"Set("+w.size+") {"+G.join(", ")+(w.size>5?", ...":"")+"}"}if(T==="date"||T==="error"||T==="function")return Te(y);if(T==="object"&&y&&typeof y=="object"){let w=Object.keys(y),G=w.slice(0,4);return(v&&v!=="Object"?`${v} `:"")+"{"+G.join(", ")+(w.length>G.length?", ...":"")+"}"}return Te(y)},He=y=>{let T=new Set([...t,...y??[]]),v=Object.getOwnPropertyNames(e).filter(w=>!T.has(w)&&!w.startsWith("_")).sort().flatMap(w=>{try{let G=Object.getOwnPropertyDescriptor(e,w);if(!G)return[];if("get"in G&&typeof G.get=="function"&&!("value"in G))return[{name:w,type:"accessor",preview:"[getter omitted]"}];let K="value"in G?G.value:e[w],Q=Be(K),V=se(K,Q.type),F=Le(K,Q.type,Q.ctor);return[{name:w,type:Q.type,...Q.ctor?{ctor:Q.ctor}:{},...V?{size:V}:{},...F?{preview:rt(F,96)}:{}}]}catch{return[{name:w,type:"unknown",preview:"[unavailable]"}]}});return JSON.stringify({version:1,entries:v})},Ve=y=>{if(typeof structuredClone=="function")try{return structuredClone(y),!0}catch{return!1}try{return JSON.stringify(y),!0}catch{return!1}},ot=y=>{let T=new Set([...t,...y??[]]),v={};return{version:1,entries:Object.getOwnPropertyNames(e).filter(G=>!T.has(G)&&!G.startsWith("_")).sort().flatMap(G=>{try{let K=Object.getOwnPropertyDescriptor(e,G);if(!K)return[];if("get"in K&&typeof K.get=="function"&&!("value"in K))return[{name:G,type:"accessor",preview:"[getter omitted]",restorable:!1}];let Q="value"in K?K.value:e[G],V=Be(Q),F=se(Q,V.type),j=Le(Q,V.type,V.ctor),X=Ve(Q);return X&&(v[G]=typeof structuredClone=="function"?structuredClone(Q):Q),[{name:G,type:V.type,...V.ctor?{ctor:V.ctor}:{},...F?{size:F}:{},...j?{preview:rt(j,96)}:{},restorable:X}]}catch{return[{name:G,type:"unknown",preview:"[unavailable]",restorable:!1}]}}),bindings:v}};f(async y=>{let T=y.data;if(!T||typeof T!="object"||typeof T.type!="string")return;if(T.type==="init"){W=T.outputMode===i?i:o,he=T.captureConsole!==void 0?!!T.captureConsole:W===i;let V=T.allowUnsafeNodeHostAccess===!0;we(T),D(T.permissions),z(V),t=Object.getOwnPropertyNames(e).sort();return}if(T.type==="fn-result"){if(typeof T.id!="number")return;let V=N.get(T.id);V&&(N.delete(T.id),T.error!==void 0?V.reject(B(T.error)):V.resolve(T.value));return}if(T.type==="update-globals"){if(typeof T.id!="number")return;try{T.globals&&typeof T.globals=="object"&&ye(T.globals),g({type:"result",id:T.id,value:void 0})}catch(V){g({type:"result",id:T.id,error:te(V)})}return}if(T.type==="inspect-globals"){if(typeof T.id!="number")return;try{let V=Array.isArray(T.reservedNames)?T.reservedNames.filter(F=>typeof F=="string"):void 0;g({type:"result",id:T.id,value:He(V)})}catch(V){g({type:"result",id:T.id,error:te(V)})}return}if(T.type==="snapshot-globals"){if(typeof T.id!="number")return;try{let V=Array.isArray(T.reservedNames)?T.reservedNames.filter(F=>typeof F=="string"):void 0;g({type:"result",id:T.id,value:ot(V)})}catch(V){g({type:"result",id:T.id,error:te(V)})}return}if(T.type!=="execute"||typeof T.id!="number"||typeof T.code!="string")return;let v=T.id,w=T.code,G=/\bawait\b/.test(w),{output:K,cleanup:Q}=P();try{ee.length=0;let V=G?await Oe(w):lt(w);if(N.size>0&&await re(),await Promise.resolve(),ee.length>0)throw ee[0];let F=Ue(V,K);try{g({type:"result",id:v,value:F})}catch{g({type:"result",id:v,value:String(F)})}}catch(V){ge(V)?g({type:"result",id:v,value:ke(V,w,G?2:0)}):g({type:"result",id:v,error:te(V)})}finally{Q()}})}var oa=4,lc=16,sa="__ax_rlm_fn_ref__",ia=16;function uc(){let s={functionRefKey:sa,maxErrorCauseDepth:ia},e=`(${Cs.toString()})(${JSON.stringify(s)});
1029
1157
  `,t="";return e.includes("__name")&&(t+=`var __name=(fn,_n)=>fn;
1030
- `),t?`${t}${e}`:e}var nc=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",As=()=>typeof process<"u"&&!!process.versions?.node,Jd=()=>!!globalThis.Deno?.version?.deno,Qd=()=>globalThis.Deno?.version?.deno??null,Yd=s=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(s.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},rc=s=>Number.isFinite(s)?Math.max(1,Math.min(ec,Math.floor(s))):Zi,Xd=()=>{if(!As())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},Zd=s=>{if(s!==void 0)return rc(s);let e=Xd();return e?rc(Math.ceil(e/2)):Zi},em=s=>s?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",tm=s=>{let e=new Set(s),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},nm=(s,e)=>{let t=Qd(),n=t?Yd(t):null;if(n?n.major>=1:!0)try{return new Worker(s,{type:"module",deno:{permissions:tm(e)}})}catch{}return new Worker(s,{type:"module"})},oc=(s,e)=>{let t=new Blob([s],{type:"application/javascript"}),n=URL.createObjectURL(t),r=Jd()?nm(n,e):new Worker(n),o=!1,i=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:l=>r.postMessage(l),terminate:()=>{r.terminate(),i()},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},sa=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",l=>{o?o(l):r=l}),n.on("exit",l=>{if(i=!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 i}};return a},ia=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(As())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,sa(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(),sa(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},sc=new Map,rm=(s,e)=>`${e}:${s}`,om=(s,e)=>{let t=rm(s,e),n=sc.get(t);if(n)return n;let r=new ia(s,e);return sc.set(t,r),r},ic=(s,e)=>{let t={},n=new Map,r=0,o=new WeakMap,i=(a,l)=>{if(typeof a=="function"){let g=`fn_${e?.nextFnId?e.nextFnId():++r}_${l||"root"}`;return n.set(g,a),{[ea]: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],`${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]=i(g,l?`${l}.${m}`:m);return d};if(s)for(let[a,l]of Object.entries(s))t[a]=i(l,a);return{serializableGlobals:t,fnMap:n}},ac=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}`)}},na=s=>!!s&&/[A-Za-z0-9_$]/.test(s),ra=s=>!!s&&/[A-Za-z_$]/.test(s),sm=(s,e)=>{let t=new Set(e);for(let m of Br(s))if(t.has(m))return m;let n=Yn(s),r=n.length,o=0,i=0,a=0,l=m=>{let g=m;for(;g<r&&/\s/.test(n[g]??"");)g++;return g},u=m=>{let g=m;for(;g>=0&&/\s/.test(n[g]??"");)g--;return g},c=m=>{let g=u(m-1);if(g<0)return!0;let f=n[g];return f===`
1031
- `||f===";"||f==="{"||f==="}"},p=()=>{let m=o;for(;o<r&&na(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&&ra(m)){let g=o,f=p();if((f==="function"||f==="class")&&c(g)){let x=l(o);if(ra(n[x])){let b=x+1;for(;b<r&&na(n[b]);)b++;let h=n.slice(x,b);if(t.has(h))return h}continue}if(f==="async"&&c(g)){let x=l(o);if(n.startsWith("function",x)){let b=l(x+8);if(ra(n[b])){let h=b+1;for(;h<r&&na(n[h]);)h++;let y=n.slice(b,h);if(t.has(y))return y}}continue}if(t.has(f)){let x=u(g-1),b=x>=0?n[x]:void 0,h=l(o),y=b==="."||b==="?"||b==="["&&n[h]==="]",I=n.startsWith("++",h)||n.startsWith("--",h)||x>0&&(n.slice(x-1,x+1)==="++"||n.slice(x-1,x+1)==="--");if(!y&&(I||d(h)))return f}continue}o++}};function oa(s){if(s==null||typeof s!="object")return String(s);try{return JSON.stringify(s,null,2)}catch{return String(s)}}function lc(s,e=ta,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):oa(s),i=s!==null&&typeof s=="object"&&typeof s.stack=="string"?s.stack:void 0,a,l=s;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=lc(p,e,t+1,n):a={name:"Error",message:oa(p)}}catch{a={name:"Error",message:oa(l.cause)}}let u={name:r,message:o};i!==void 0&&(u.stack=i),a!==void 0&&(u.cause=a);let c=s;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 uc(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=uc(s.cause)),s.data!==void 0&&(t.data=s.data),t}function im(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")&&(e.restorable===void 0||typeof e.restorable=="boolean")}function am(s){if(!s||typeof s!="object")return{version:1,entries:[],bindings:{}};let e=s,t=Array.isArray(e.entries)?e.entries:[],n=e.bindings&&typeof e.bindings=="object"?e.bindings:{};return{version:1,entries:t.filter(im),bindings:n}}var aa=(i=>(i.NETWORK="network",i.STORAGE="storage",i.CODE_LOADING="code-loading",i.COMMUNICATION="communication",i.TIMING="timing",i.WORKERS="workers",i))(aa||{}),Zt=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=Zd(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=em(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(`
1032
- `)}createSession(e){let t=tc(),n=As()?om(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,l=this.timeout,u=0,{serializableGlobals:c,fnMap:p}=ic(e,{nextFnId:()=>++u});ac(c);let d=new Map,m=0,g=[],f=null,x=v=>{let E=v.data;if(!E||typeof E!="object"||!("type"in E)||E.type===void 0)return;let k=E;if(k.type==="result"){if(typeof k.id!="number")return;let O=d.get(k.id);O&&(d.delete(k.id),k.error!==void 0?O.reject(uc(k.error)):O.resolve(k.value));return}if(k.type==="fn-call"){if(typeof k.id!="number"||typeof k.name!="string")return;let O=p.get(k.name);if(!O){r?.postMessage({type:"fn-result",id:k.id,error:`Function "${k.name}" not found`});return}Promise.resolve().then(()=>O(...k.args??[])).then(L=>{r?.postMessage({type:"fn-result",id:k.id,value:L})}).catch(L=>{r?.postMessage({type:"fn-result",id:k.id,error:lc(L)})})}},b=()=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null),i=null},h=()=>{a=!0,b();for(let v of g)!v.started&&!v.settled&&(v.settled=!0,v.signal&&v.onAbort&&v.signal.removeEventListener("abort",v.onAbort),v.reject(new Error("Worker terminated")));g.length=0;for(let v of d.values())v.reject(new Error("Worker terminated"));d.clear()},y=v=>{b();for(let E of d.values())E.reject(v);d.clear()},I=v=>{v.postMessage({type:"init",globals:c,fnNames:[...p.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})};if(nc()){r=oc(t,this.permissions),o="browser",r.onmessage=x,r.onerror=y;try{I(r)}catch(v){throw h(),v}}let C=async()=>{if(!r){if(a)throw new Error("Session is closed");if(nc()){r=oc(t,this.permissions),o="browser",r.onmessage=x,r.onerror=y;try{I(r)}catch(v){throw h(),v}return}if(!As())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");i||(i=(n?n.acquire():sa(t)).then(v=>{if(a)throw n?n.release(v):v.terminate(),new Error("Session is closed");r=v,o="node",r.onmessage=x,r.onerror=y;try{I(r)}catch(E){throw n?n.release(v):v.terminate(),r=null,o=null,E}})),await i}},R=(v,E)=>{if(a)return Promise.reject(new Error("Session is closed"));let k=E.signal;if(k?.aborted)return Promise.reject(new Error(`Aborted: ${k.reason??"execution aborted"}`));let O=++m;return new Promise((L,F)=>{let M=L,_=F,j,H=()=>{};if(d.set(O,{resolve:D=>{j&&clearTimeout(j),H(),M(D)},reject:D=>{j&&clearTimeout(j),H(),_(D)}}),k){let D=()=>{clearTimeout(j),d.delete(O),h(),_(new Error(`Aborted: ${k.reason??"execution aborted"}`))};k.addEventListener("abort",D,{once:!0}),H=()=>{k.removeEventListener("abort",D)}}C().then(()=>{if(!r)throw new Error("Worker unavailable");j=setTimeout(()=>{d.delete(O),b();for(let D of d.values())D.reject(new Error("Worker terminated"));d.clear(),F(new Error(E.timeoutMessage))},l),r.postMessage({...v,id:O})}).catch(D=>{d.get(O)&&(d.delete(O),clearTimeout(j),H(),_(D))})})},P=(v,E)=>a?Promise.reject(new Error("Session is closed")):v?.aborted?Promise.reject(new Error(`Aborted: ${v.reason??"execution aborted"}`)):new Promise((k,O)=>{let L={started:!1,settled:!1,signal:v,resolve:k,reject:O,operation:E};if(v){let M=()=>{if(L.settled)return;L.settled=!0;let _=g.indexOf(L);_!==-1&&g.splice(_,1),v.removeEventListener("abort",M),O(new Error(`Aborted: ${v.reason??"execution aborted"}`))};L.onAbort=M,v.addEventListener("abort",M,{once:!0})}g.push(L);let F=()=>{if(f)return;let M=g.find(j=>!j.started&&!j.settled);if(!M)return;let _=()=>{f=null,F()};f=(async()=>{if(!M.settled){if(a){M.settled=!0,M.signal&&M.onAbort&&M.signal.removeEventListener("abort",M.onAbort),M.reject(new Error("Worker terminated"));return}if(M.signal?.aborted){M.settled=!0,M.onAbort&&M.signal.removeEventListener("abort",M.onAbort),M.reject(new Error(`Aborted: ${M.signal.reason??"execution aborted"}`));return}M.started=!0;try{let j=await M.operation();if(M.settled)return;M.settled=!0,M.signal&&M.onAbort&&M.signal.removeEventListener("abort",M.onAbort),M.resolve(j)}catch(j){if(M.settled)return;M.settled=!0,M.signal&&M.onAbort&&M.signal.removeEventListener("abort",M.onAbort),M.reject(j)}finally{let j=g.indexOf(M);j!==-1&&g.splice(j,1),_()}}})().catch(()=>{_()})};F()});return{execute(v,E){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(v))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let k=E?.reservedNames;if(k){let O=sm(v,k);if(O)return Promise.resolve(`[ERROR] Cannot assign to, redeclare, or shadow reserved runtime variable '${O}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${O}\`.`)}return P(E?.signal,()=>R({type:"execute",code:v},{signal:E?.signal,timeoutMessage:"Execution timed out"}))},inspectGlobals(v){return a?Promise.reject(new Error("Session is closed")):P(v?.signal,()=>R({type:"inspect-globals",reservedNames:v?.reservedNames},{signal:v?.signal,timeoutMessage:"Global inspection timed out"}).then(E=>typeof E=="string"?E:E===void 0?"":JSON.stringify(E)))},snapshotGlobals(v){return a?Promise.reject(new Error("Session is closed")):P(v?.signal,()=>R({type:"snapshot-globals",reservedNames:v?.reservedNames},{signal:v?.signal,timeoutMessage:"Global snapshot timed out"}).then(am))},async patchGlobals(v,E){if(!v||typeof v!="object"||Array.isArray(v))throw new Error("patchGlobals expects an object");let{serializableGlobals:k,fnMap:O}=ic(v,{nextFnId:()=>++u});if(ac(k),Object.keys(k).length!==0){await P(E?.signal,()=>R({type:"update-globals",globals:k},{signal:E?.signal,timeoutMessage:"Global patch timed out"}));for(let[L,F]of Object.entries(k))c[L]=F;for(let[L,F]of O.entries())p.set(L,F)}},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 cc(s){return new Zt(s)}var ys=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(`
1158
+ `),t?`${t}${e}`:e}var cc=()=>typeof Worker<"u"&&typeof Blob<"u"&&typeof URL<"u"&&typeof URL.createObjectURL=="function",ws=()=>typeof process<"u"&&!!process.versions?.node,Zd=()=>!!globalThis.Deno?.version?.deno,em=()=>globalThis.Deno?.version?.deno??null,tm=s=>{let e=/^(\d+)\.(\d+)\.(\d+)$/.exec(s.trim());return e?{major:Number(e[1]),minor:Number(e[2]),patch:Number(e[3])}:null},pc=s=>Number.isFinite(s)?Math.max(1,Math.min(lc,Math.floor(s))):oa,nm=()=>{if(!ws())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},rm=s=>{if(s!==void 0)return pc(s);let e=nm();return e?pc(Math.ceil(e/2)):oa},om=s=>s?.debugNodeWorkerPool?!0:(globalThis.process?.env??{}).AX_RLM_DEBUG_NODE_POOL==="1",sm=s=>{let e=new Set(s),t={};return e.has("network")&&(t.net=!0),Object.keys(t).length>0?t:"none"},im=(s,e)=>{let t=em(),n=t?tm(t):null;if(n?n.major>=1:!0)try{return new Worker(s,{type:"module",deno:{permissions:sm(e)}})}catch{}return new Worker(s,{type:"module"})},dc=(s,e)=>{let t=new Blob([s],{type:"application/javascript"}),n=URL.createObjectURL(t),r=Zd()?im(n,e):new Worker(n),o=!1,i=()=>{o||(URL.revokeObjectURL(n),o=!0)},a={postMessage:l=>r.postMessage(l),terminate:()=>{r.terminate(),i()},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},ca=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",l=>{o?o(l):r=l}),n.on("exit",l=>{if(i=!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 i}};return a},pa=class{source;maxSize;idle=[];pendingCreates=0;constructor(e,t){this.source=e,this.maxSize=t}warm(){if(ws())for(;this.idle.length+this.pendingCreates<this.maxSize;)this.pendingCreates+=1,ca(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(),ca(this.source)}release(e){e.onmessage=null,e.onerror=null,e.terminate(),this.warm()}},mc=new Map,am=(s,e)=>`${e}:${s}`,lm=(s,e)=>{let t=am(s,e),n=mc.get(t);if(n)return n;let r=new pa(s,e);return mc.set(t,r),r},gc=(s,e)=>{let t={},n=new Map,r=0,o=new WeakMap,i=(a,l)=>{if(typeof a=="function"){let g=`fn_${e?.nextFnId?e.nextFnId():++r}_${l||"root"}`;return n.set(g,a),{[sa]: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],`${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]=i(g,l?`${l}.${m}`:m);return d};if(s)for(let[a,l]of Object.entries(s))t[a]=i(l,a);return{serializableGlobals:t,fnMap:n}},fc=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}`)}},aa=s=>!!s&&/[A-Za-z0-9_$]/.test(s),la=s=>!!s&&/[A-Za-z_$]/.test(s),um=(s,e)=>{let t=new Set(e);for(let m of Hr(s))if(t.has(m))return m;let n=er(s),r=n.length,o=0,i=0,a=0,l=m=>{let g=m;for(;g<r&&/\s/.test(n[g]??"");)g++;return g},u=m=>{let g=m;for(;g>=0&&/\s/.test(n[g]??"");)g--;return g},c=m=>{let g=u(m-1);if(g<0)return!0;let f=n[g];return f===`
1159
+ `||f===";"||f==="{"||f==="}"},p=()=>{let m=o;for(;o<r&&aa(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&&la(m)){let g=o,f=p();if((f==="function"||f==="class")&&c(g)){let x=l(o);if(la(n[x])){let A=x+1;for(;A<r&&aa(n[A]);)A++;let h=n.slice(x,A);if(t.has(h))return h}continue}if(f==="async"&&c(g)){let x=l(o);if(n.startsWith("function",x)){let A=l(x+8);if(la(n[A])){let h=A+1;for(;h<r&&aa(n[h]);)h++;let b=n.slice(A,h);if(t.has(b))return b}}continue}if(t.has(f)){let x=u(g-1),A=x>=0?n[x]:void 0,h=l(o),b=A==="."||A==="?"||A==="["&&n[h]==="]",I=n.startsWith("++",h)||n.startsWith("--",h)||x>0&&(n.slice(x-1,x+1)==="++"||n.slice(x-1,x+1)==="--");if(!b&&(I||d(h)))return f}continue}o++}};function ua(s){if(s==null||typeof s!="object")return String(s);try{return JSON.stringify(s,null,2)}catch{return String(s)}}function hc(s,e=ia,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):ua(s),i=s!==null&&typeof s=="object"&&typeof s.stack=="string"?s.stack:void 0,a,l=s;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=hc(p,e,t+1,n):a={name:"Error",message:ua(p)}}catch{a={name:"Error",message:ua(l.cause)}}let u={name:r,message:o};i!==void 0&&(u.stack=i),a!==void 0&&(u.cause=a);let c=s;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 xc(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=xc(s.cause)),s.data!==void 0&&(t.data=s.data),t}function cm(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")&&(e.restorable===void 0||typeof e.restorable=="boolean")}function pm(s){if(!s||typeof s!="object")return{version:1,entries:[],bindings:{}};let e=s,t=Array.isArray(e.entries)?e.entries:[],n=e.bindings&&typeof e.bindings=="object"?e.bindings:{};return{version:1,entries:t.filter(cm),bindings:n}}var da=(i=>(i.NETWORK="network",i.STORAGE="storage",i.CODE_LOADING="code-loading",i.COMMUNICATION="communication",i.TIMING="timing",i.WORKERS="workers",i))(da||{}),nn=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=rm(e?.nodeWorkerPoolSize),this.debugNodeWorkerPool=om(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(`
1160
+ `)}createSession(e){let t=uc(),n=ws()?lm(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,l=this.timeout,u=0,{serializableGlobals:c,fnMap:p}=gc(e,{nextFnId:()=>++u});fc(c);let d=new Map,m=0,g=[],f=null,x=S=>{let O=S.data;if(!O||typeof O!="object"||!("type"in O)||O.type===void 0)return;let k=O;if(k.type==="result"){if(typeof k.id!="number")return;let _=d.get(k.id);_&&(d.delete(k.id),k.error!==void 0?_.reject(xc(k.error)):_.resolve(k.value));return}if(k.type==="fn-call"){if(typeof k.id!="number"||typeof k.name!="string")return;let _=p.get(k.name);if(!_){r?.postMessage({type:"fn-result",id:k.id,error:`Function "${k.name}" not found`});return}Promise.resolve().then(()=>_(...k.args??[])).then(L=>{r?.postMessage({type:"fn-result",id:k.id,value:L})}).catch(L=>{r?.postMessage({type:"fn-result",id:k.id,error:hc(L)})})}},A=()=>{r&&(o==="node"&&n?n.release(r):r.terminate(),r=null,o=null),i=null},h=()=>{a=!0,A();for(let S of g)!S.started&&!S.settled&&(S.settled=!0,S.signal&&S.onAbort&&S.signal.removeEventListener("abort",S.onAbort),S.reject(new Error("Worker terminated")));g.length=0;for(let S of d.values())S.reject(new Error("Worker terminated"));d.clear()},b=S=>{A();for(let O of d.values())O.reject(S);d.clear()},I=S=>{S.postMessage({type:"init",globals:c,fnNames:[...p.keys()],permissions:[...this.permissions],allowUnsafeNodeHostAccess:this.allowUnsafeNodeHostAccess,outputMode:this.outputMode,captureConsole:this.captureConsole})};if(cc()){r=dc(t,this.permissions),o="browser",r.onmessage=x,r.onerror=b;try{I(r)}catch(S){throw h(),S}}let C=async()=>{if(!r){if(a)throw new Error("Session is closed");if(cc()){r=dc(t,this.permissions),o="browser",r.onmessage=x,r.onerror=b;try{I(r)}catch(S){throw h(),S}return}if(!ws())throw new Error("No worker runtime available: Web Worker is unavailable in this environment");i||(i=(n?n.acquire():ca(t)).then(S=>{if(a)throw n?n.release(S):S.terminate(),new Error("Session is closed");r=S,o="node",r.onmessage=x,r.onerror=b;try{I(r)}catch(O){throw n?n.release(S):S.terminate(),r=null,o=null,O}})),await i}},R=(S,O)=>{if(a)return Promise.reject(new Error("Session is closed"));let k=O.signal;if(k?.aborted)return Promise.reject(new Error(`Aborted: ${k.reason??"execution aborted"}`));let _=++m;return new Promise((L,E)=>{let P=L,$=E,D,z=()=>{};if(d.set(_,{resolve:U=>{D&&clearTimeout(D),z(),P(U)},reject:U=>{D&&clearTimeout(D),z(),$(U)}}),k){let U=()=>{clearTimeout(D),d.delete(_),h(),$(new Error(`Aborted: ${k.reason??"execution aborted"}`))};k.addEventListener("abort",U,{once:!0}),z=()=>{k.removeEventListener("abort",U)}}C().then(()=>{if(!r)throw new Error("Worker unavailable");D=setTimeout(()=>{d.delete(_),A();for(let U of d.values())U.reject(new Error("Worker terminated"));d.clear(),E(new Error(O.timeoutMessage))},l),r.postMessage({...S,id:_})}).catch(U=>{d.get(_)&&(d.delete(_),clearTimeout(D),z(),$(U))})})},M=(S,O)=>a?Promise.reject(new Error("Session is closed")):S?.aborted?Promise.reject(new Error(`Aborted: ${S.reason??"execution aborted"}`)):new Promise((k,_)=>{let L={started:!1,settled:!1,signal:S,resolve:k,reject:_,operation:O};if(S){let P=()=>{if(L.settled)return;L.settled=!0;let $=g.indexOf(L);$!==-1&&g.splice($,1),S.removeEventListener("abort",P),_(new Error(`Aborted: ${S.reason??"execution aborted"}`))};L.onAbort=P,S.addEventListener("abort",P,{once:!0})}g.push(L);let E=()=>{if(f)return;let P=g.find(D=>!D.started&&!D.settled);if(!P)return;let $=()=>{f=null,E()};f=(async()=>{if(!P.settled){if(a){P.settled=!0,P.signal&&P.onAbort&&P.signal.removeEventListener("abort",P.onAbort),P.reject(new Error("Worker terminated"));return}if(P.signal?.aborted){P.settled=!0,P.onAbort&&P.signal.removeEventListener("abort",P.onAbort),P.reject(new Error(`Aborted: ${P.signal.reason??"execution aborted"}`));return}P.started=!0;try{let D=await P.operation();if(P.settled)return;P.settled=!0,P.signal&&P.onAbort&&P.signal.removeEventListener("abort",P.onAbort),P.resolve(D)}catch(D){if(P.settled)return;P.settled=!0,P.signal&&P.onAbort&&P.signal.removeEventListener("abort",P.onAbort),P.reject(D)}finally{let D=g.indexOf(P);D!==-1&&g.splice(D,1),$()}}})().catch(()=>{$()})};E()});return{execute(S,O){if(a)return Promise.reject(new Error("Session is closed"));if(/['"]use strict['"]/.test(S))return Promise.resolve('[ERROR] "use strict" is not allowed in the runtime session. Remove it and try again.');let k=O?.reservedNames;if(k){let _=um(S,k);if(_)return Promise.resolve(`[ERROR] Cannot assign to, redeclare, or shadow reserved runtime variable '${_}'. Use a different local variable name (for example: \`ctx\`) or access the original via \`inputs.${_}\`.`)}return M(O?.signal,()=>R({type:"execute",code:S},{signal:O?.signal,timeoutMessage:"Execution timed out"}))},inspectGlobals(S){return a?Promise.reject(new Error("Session is closed")):M(S?.signal,()=>R({type:"inspect-globals",reservedNames:S?.reservedNames},{signal:S?.signal,timeoutMessage:"Global inspection timed out"}).then(O=>typeof O=="string"?O:O===void 0?"":JSON.stringify(O)))},snapshotGlobals(S){return a?Promise.reject(new Error("Session is closed")):M(S?.signal,()=>R({type:"snapshot-globals",reservedNames:S?.reservedNames},{signal:S?.signal,timeoutMessage:"Global snapshot timed out"}).then(pm))},async patchGlobals(S,O){if(!S||typeof S!="object"||Array.isArray(S))throw new Error("patchGlobals expects an object");let{serializableGlobals:k,fnMap:_}=gc(S,{nextFnId:()=>++u});if(fc(k),Object.keys(k).length!==0){await M(O?.signal,()=>R({type:"update-globals",globals:k},{signal:O?.signal,timeoutMessage:"Global patch timed out"}));for(let[L,E]of Object.entries(k))c[L]=E;for(let[L,E]of _.entries())p.set(L,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 Ac(s){return new nn(s)}var Ts=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(`
1033
1161
 
1034
1162
  `)}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(`
1035
- `)}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=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: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 pc(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 dc(s){let t=new TextEncoder().encode(s),n=await Ca().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function la(){return pc(await dc(Ve()+Math.random().toString(36)))}async function mc(s){return pc(await dc(s))}async function bs(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 gc(s){let e=new URLSearchParams;for(let[t,n]of Object.entries(s))n!==void 0&&e.set(t,n);return e.toString()}function zr(s){return s.endsWith("/")?s.slice(0,-1):s}function lm(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 fc(s,e){let t=lm(e);if(t){let a=await bs(t),l=zr(new URL(s).toString().split("?")[0]),u=zr(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(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 l=await bs(a.url),u=zr(l.resource??""),c=zr(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){i=l}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(i)}`)}async function hc(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 bs(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 Xn=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 hc(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await fc(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",l=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),u=await la(),c=await mc(u),p=await la(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${gc({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:f}=await this.oauth.onAuthCode(m),x=f??a,b=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:u,client:l,redirectUri:x,resource:t});return await this.setStoredToken(t,r,b),{token:b,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 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 Is=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 Xn(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"),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="",i=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(`
1163
+ `)}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=Qe(),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: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 yc(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 bc(s){let t=new TextEncoder().encode(s),n=await Ea().subtle.digest("SHA-256",t);return new Uint8Array(n)}async function ma(){return yc(await bc(Qe()+Math.random().toString(36)))}async function Ic(s){return yc(await bc(s))}async function Rs(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 Cc(s){let e=new URLSearchParams;for(let[t,n]of Object.entries(s))n!==void 0&&e.set(t,n);return e.toString()}function Kr(s){return s.endsWith("/")?s.slice(0,-1):s}function dm(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 wc(s,e){let t=dm(e);if(t){let a=await Rs(t),l=Kr(new URL(s).toString().split("?")[0]),u=Kr(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(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 l=await Rs(a.url),u=Kr(l.resource??""),c=Kr(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){i=l}throw new Error(`Failed to resolve protected resource metadata via well-known endpoints. Last error: ${String(i)}`)}async function Tc(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 Rs(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 tr=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 Tc(e);return this.asMetaCache.set(e,t),t}async ensureAccessToken(e){if(!this.oauth)return null;let{resource:t,issuers:n}=await wc(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",l=this.oauth.clientId?{client_id:this.oauth.clientId,client_secret:this.oauth.clientSecret}:await this.dynamicClientRegistration(o,a),u=await ma(),c=await Ic(u),p=await ma(),d=this.oauth.scopes?.join(" "),m=`${o.authorization_endpoint}?${Cc({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:f}=await this.oauth.onAuthCode(m),x=f??a,A=await this.exchangeCodeForToken({asMeta:o,code:g,codeVerifier:u,client:l,redirectUri:x,resource:t});return await this.setStoredToken(t,r,A),{token:A,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 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 Ss=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 tr(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"),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="",i=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(`
1036
1164
  `);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 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"),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 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="",l=async()=>{try{let{done:u,value:c}=await o.read();if(u){o.releaseLock();return}a+=i.decode(c,{stream:!0});let p=a.split(`
1037
- `);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"),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 Cs=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 Xn(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 l=r.split(`
1038
- `);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"),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 ua(s){return s.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??s.slice(0,80)}function Ac(s){return Br(s)}function um(s){return Zu(s)}var cm=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 yc(s){let e=Yn(s),t=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,n=new Set,r=t.exec(e);for(;r!==null;)r[1]&&!cm.has(r[1])&&n.add(r[1]),r=t.exec(e);return n}function bc(s){let e=yc(s);for(let t of Ac(s))e.delete(t);return e}var pm=new Set(["dead-end","foundational","pivot","superseded"]);function Zn(s,e=120){let t=s.replace(/\s+/g," ").trim();return t.length<=e?t:`${t.slice(0,e-3)}...`}function dm(s){return/\b(final|ask_clarification)\s*\(/.test(s)}function mm(s){return s.tags.includes("error")?"error":dm(s.code)?"finalize":/\b(llmQuery|listModuleFunctions|getFunctionDefinitions)\s*\(/.test(s.code)?"query":(s.producedVars?.length??0)>0?"transform":"explore"}function gm(s){let e=s.producedVars??[];if(e.length>0)return`Updated 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 pa(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=Zn(s.output||"(no output)"),n=Zn(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 fm(s){s.rank=void 0,s.tags=s.tags.filter(e=>!pm.has(e))}function Mt(s){s.producedVars||(s.producedVars=um(s.code)),s.referencedVars||(s.referencedVars=[...yc(s.code)]),s.stepKind||(s.stepKind=mm(s)),s.stateDelta||(s.stateDelta=gm(s)),s.summary||(s.summary=pa(s))}function Ic(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 hm(s){return/\blistModuleFunctions\s*\(/.test(s.code)?Ic(s.output,/^### Module `([^`]+)`/gm).map(e=>({module:e.key,text:e.text})):[]}function xm(s){return/\bgetFunctionDefinitions\s*\(/.test(s.code)?Ic(s.output,/^### `([^`]+)`/gm).map(e=>({qualifiedName:e.key,text:e.text})):[]}function Am(s){let e=Yn(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 da(s){s._discoveryModuleSections||(s._discoveryModuleSections=hm(s)),s._discoveryFunctionSections||(s._discoveryFunctionSections=xm(s)),s._directQualifiedCalls||(s._directQualifiedCalls=Am(s.code))}function ym(s){return da(s),s._directQualifiedCalls?.find(Boolean)}function Ts(s){let e=new Map;for(let t of s){let n=t;Mt(n);let r=ym(n);for(let i of n.producedVars??[])e.set(i,{createdTurn:n.turn,stepKind:n.stepKind,source:r,code:n.code});let o=bc(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 bm(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"))){da(r);for(let o of r._directQualifiedCalls??[])t.add(o)}}return e}function Im(s,e){let t=s;da(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(`
1165
+ `);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"),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 ks=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 tr(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 l=r.split(`
1166
+ `);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"),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 ga(s){return s.match(/^(\w+Error:\s*.{0,60})/m)?.[1]??s.slice(0,80)}function Sc(s){return Hr(s)}function mm(s){return ac(s)}var gm=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 kc(s){let e=er(s),t=/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g,n=new Set,r=t.exec(e);for(;r!==null;)r[1]&&!gm.has(r[1])&&n.add(r[1]),r=t.exec(e);return n}function vc(s){let e=kc(s);for(let t of Sc(s))e.delete(t);return e}var fm=new Set(["dead-end","foundational","pivot","superseded"]);function nr(s,e=120){let t=s.replace(/\s+/g," ").trim();return t.length<=e?t:`${t.slice(0,e-3)}...`}function hm(s){return/\b(final|ask_clarification)\s*\(/.test(s)}function xm(s){return s.tags.includes("error")?"error":hm(s.code)?"finalize":/\b(llmQuery|listModuleFunctions|getFunctionDefinitions)\s*\(/.test(s.code)?"query":(s.producedVars?.length??0)>0?"transform":"explore"}function Am(s){let e=s.producedVars??[];if(e.length>0)return`Updated 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 ha(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=nr(s.output||"(no output)"),n=nr(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 ym(s){s.rank=void 0,s.tags=s.tags.filter(e=>!fm.has(e))}function _t(s){s.producedVars||(s.producedVars=mm(s.code)),s.referencedVars||(s.referencedVars=[...kc(s.code)]),s.stepKind||(s.stepKind=xm(s)),s.stateDelta||(s.stateDelta=Am(s)),s.summary||(s.summary=ha(s))}function Oc(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 bm(s){return/\blistModuleFunctions\s*\(/.test(s.code)?Oc(s.output,/^### Module `([^`]+)`/gm).map(e=>({module:e.key,text:e.text})):[]}function Im(s){return/\bgetFunctionDefinitions\s*\(/.test(s.code)?Oc(s.output,/^### `([^`]+)`/gm).map(e=>({qualifiedName:e.key,text:e.text})):[]}function Cm(s){let e=er(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 xa(s){s._discoveryModuleSections||(s._discoveryModuleSections=bm(s)),s._discoveryFunctionSections||(s._discoveryFunctionSections=Im(s)),s._directQualifiedCalls||(s._directQualifiedCalls=Cm(s.code))}function wm(s){return xa(s),s._directQualifiedCalls?.find(Boolean)}function vs(s){let e=new Map;for(let t of s){let n=t;_t(n);let r=wm(n);for(let i of n.producedVars??[])e.set(i,{createdTurn:n.turn,stepKind:n.stepKind,source:r,code:n.code});let o=vc(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 Tm(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"))){xa(r);for(let o of r._directQualifiedCalls??[])t.add(o)}}return e}function Rm(s,e){let t=s;xa(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(`
1039
1167
 
1040
1168
  `),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(`
1041
1169
 
1042
- `),summary:void 0,_discoveryModuleSections:i}}return t}function ws(s,e){if(!e?.pruneUsedDocs||s.length===0)return[...s];let t=bm(s);return s.flatMap((n,r)=>{let o=Im(n,t[r]??new Set);return o?[o]:[]})}function Cm(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 Tm(s,e){let t=e.actionReplay??"full",n=Math.max(e.recentFullActions??1,0);for(let i of s)Mt(i);let r=Cm(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 l=a>=o,u=i.tags.includes("error"),c=i.output.startsWith("[POLICY]"),p=r[a]??new Set,m=(i.producedVars??[]).some(g=>p.has(g));if(l||u||c){i.replayMode="full";return}if(t==="adaptive"&&m){i.replayMode="full";return}i.replayMode="omit"})}function wm(s,e){Mt(s),Mt(e),fm(s);let t=s.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){s.rank=0,qr(s,"dead-end");return}if(t&&n){let r=ua(s.output),o=ua(e.output);r!==o?(s.rank=3,qr(s,"pivot")):(s.rank=0,qr(s,"dead-end"));return}if(!t&&!n){let r=s.producedVars??Ac(s.code);if(r.length===0||s.stepKind==="explore"||s.stepKind==="query")return;let o=bc(e.code);if(r.filter(a=>o.has(a)).length>0){s.rank=5,qr(s,"foundational");return}s.stepKind==="transform"&&(s.rank=1,qr(s,"superseded"));return}}function qr(s,e){s.tags.includes(e)||s.tags.push(e)}function ca(s,e){let t=s.output.split(`
1170
+ `),summary:void 0,_discoveryModuleSections:i}}return t}function Os(s,e){if(!e?.pruneUsedDocs||s.length===0)return[...s];let t=Tm(s);return s.flatMap((n,r)=>{let o=Rm(n,t[r]??new Set);return o?[o]:[]})}function Sm(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 km(s,e){let t=e.actionReplay??"full",n=Math.max(e.recentFullActions??1,0);for(let i of s)_t(i);let r=Sm(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 l=a>=o,u=i.tags.includes("error"),c=i.output.startsWith("[POLICY]"),p=r[a]??new Set,m=(i.producedVars??[]).some(g=>p.has(g));if(l||u||c){i.replayMode="full";return}if(t==="adaptive"&&m){i.replayMode="full";return}i.replayMode="omit"})}function vm(s,e){_t(s),_t(e),ym(s);let t=s.tags.includes("error"),n=e.tags.includes("error");if(t&&!n){s.rank=0,Wr(s,"dead-end");return}if(t&&n){let r=ga(s.output),o=ga(e.output);r!==o?(s.rank=3,Wr(s,"pivot")):(s.rank=0,Wr(s,"dead-end"));return}if(!t&&!n){let r=s.producedVars??Sc(s.code);if(r.length===0||s.stepKind==="explore"||s.stepKind==="query")return;let o=vc(e.code);if(r.filter(a=>o.has(a)).length>0){s.rank=5,Wr(s,"foundational");return}s.stepKind==="transform"&&(s.rank=1,Wr(s,"superseded"));return}}function Wr(s,e){s.tags.includes(e)||s.tags.push(e)}function fa(s,e){let t=s.output.split(`
1043
1171
  `).map(r=>r.trim()).filter(Boolean).find(r=>/\b\w+Error:/.test(r)&&!r.startsWith("["))??s.output.split(`
1044
- `).map(r=>r.trim()).filter(r=>!!r&&!r.startsWith("[")&&!r.startsWith("...")).at(-1)??ua(s.output);return`[TOMBSTONE]: Resolved ${Zn(t,96)} in turn ${e.turn}.`}function Rm(s){return s.startsWith("[TOMBSTONE]: Resolved ")}var Sm=`You are an internal AxAgent tombstone summarizer.
1172
+ `).map(r=>r.trim()).filter(r=>!!r&&!r.startsWith("[")&&!r.startsWith("...")).at(-1)??ga(s.output);return`[TOMBSTONE]: Resolved ${nr(t,96)} in turn ${e.turn}.`}function Om(s){return s.startsWith("[TOMBSTONE]: Resolved ")}var Em=`You are an internal AxAgent tombstone summarizer.
1045
1173
 
1046
1174
  Write the output field \`tombstone\` as exactly one concise line.
1047
1175
  - Start with \`[TOMBSTONE]:\`
1048
1176
  - Summarize the resolved error and the successful fix.
1049
1177
  - Mention one failed approach to avoid when possible.
1050
1178
  - Do not include code fences, bullet points, or extra prose.
1051
- - Keep it roughly 20-40 tokens.`,km=`You are an internal AxAgent checkpoint summarizer.
1179
+ - Keep it roughly 20-40 tokens.`,Mm=`You are an internal AxAgent checkpoint summarizer.
1052
1180
 
1053
1181
  Write the output field \`checkpointSummary\` as plain text with exactly these labels in this order:
1054
1182
  Objective:
@@ -1064,28 +1192,29 @@ Rules:
1064
1192
  - Keep enough evidence to avoid repeating invalid callable names, invalid query formats, or wrong assumptions.
1065
1193
  - Do not restate raw code or quote large outputs.
1066
1194
  - Use "none" when a section has nothing worth preserving.
1067
- - Be concise and factual, but prefer slightly more detail over losing task-critical specifics.`;function Cc(s){let{mem:e,description:t,maxSteps:n,...r}=s??{};return r}function Tc(s,e,t){let n=Cc(t);return{...n,description:s,traceLabel:n.traceLabel??e,maxSteps:1}}function wc(s){return{...Cc(s),maxSteps:1}}async function vm(s,e,t,n,r){let o=new Se("errorCode:string, errorOutput:string, resolutionCode:string -> tombstone:string",{...Tc(Sm,"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)},wc(t));return(typeof i.tombstone=="string"?i.tombstone.trim():String(i.tombstone).trim())||ca(n,r)}catch{return ca(n,r)}}function Om(s){return s.map(e=>{Mt(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 written: ${(e.producedVars??[]).join(", ")||"none"}`,`State delta: ${e.stateDelta??"none"}`,`Observed result: ${Zn(e.output||"(no output)",360)}`,`Actor fields: ${t||"none"}`,`Code excerpt: ${Zn(e.code||"(no code)",360)}`].join(`
1195
+ - Be concise and factual, but prefer slightly more detail over losing task-critical specifics.`;function Ec(s){let{mem:e,description:t,maxSteps:n,...r}=s??{};return r}function Mc(s,e,t){let n=Ec(t);return{...n,description:s,traceLabel:n.traceLabel??e,maxSteps:1}}function Pc(s){return{...Ec(s),maxSteps:1}}async function Pm(s,e,t,n,r){let o=new Se("errorCode:string, errorOutput:string, resolutionCode:string -> tombstone:string",{...Mc(Em,"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)},Pc(t));return(typeof i.tombstone=="string"?i.tombstone.trim():String(i.tombstone).trim())||fa(n,r)}catch{return fa(n,r)}}function Fm(s){return s.map(e=>{_t(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 written: ${(e.producedVars??[]).join(", ")||"none"}`,`State delta: ${e.stateDelta??"none"}`,`Observed result: ${nr(e.output||"(no output)",360)}`,`Actor fields: ${t||"none"}`,`Code excerpt: ${nr(e.code||"(no code)",360)}`].join(`
1068
1196
  `)}).join(`
1069
1197
 
1070
- `)}function xc(s){let e=new Set,t=new Set,n=[],r=[],o="Continue from the latest live runtime state.";for(let i of s){Mt(i),e.add(i.stepKind??"explore");for(let u of i.producedVars??[])t.add(u);let a=Zn(i.output||"(no output)",200);n.push(`Turn ${i.turn}: ${a}`);let l=i.actorFieldsOutput.replace(/^Actor fields:\s*/i,"").trim();l&&r.push(`Turn ${i.turn}: ${l}`),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(`
1071
- `)}async function Rc(s,e,t,n){let r=new Se("turns:string -> checkpointSummary:string",{...Tc(km,"ax-agent-checkpoint-summary",e)});try{let o=await r.forward(s,{turns:Om(n)},wc(t));return(typeof o.checkpointSummary=="string"?o.checkpointSummary.trim():String(o.checkpointSummary).trim())||xc(n)}catch{return xc(n)}}async function ma(s,e,t,n,r){let o=s[e];if(!o)return;Mt(o);let i=o.tags.includes("error");if(t.hindsightEvaluation&&s.length>=2){let a=s[s.length-2];wm(a,o)}if(t.errorPruning||t.tombstoning)for(let a of s){if(!a.tags.includes("error"))continue;let l=s.indexOf(a),u=s[l+1];if(!u||u.tags.includes("error")||(t.errorPruning&&!a.tombstone&&(a.tombstone=ca(a,u)),!(!!t.tombstoning&&!!n&&!a._tombstonePromise&&(!a.tombstone||Rm(a.tombstone)))||!n))continue;let p=typeof t.tombstoning=="object"?t.tombstoning:void 0;a._tombstonePromise=vm(n,p,r,a,u),a._tombstonePromise.then(d=>{a.tombstone=d}).catch(()=>{}).finally(()=>{a._tombstonePromise=void 0})}if(t.errorPruning&&!i){let a=s.filter(l=>!l.tags.includes("error")||l.tombstone!=null||l._tombstonePromise!=null);s.length=0,s.push(...a)}if(t.hindsightEvaluation){let a=s[s.length-1]?.turn??o.turn,l=s.filter((u,c)=>c===s.length-1||u.rank===void 0||!u.tags.includes("error")&&a-u.turn<t.rankPruneGraceTurns||u.rank>=t.pruneRank||u.tombstone!=null||u._tombstonePromise!=null);s.length=0,s.push(...l)}}function Em(s,e){if(e.has(s.turn)&&!s.tags.includes("error")&&s.replayMode!=="full")return"";if(s.tombstone)return`Action ${s.turn}:
1072
- ${s.tombstone}`;switch(s.replayMode){case"omit":return Mt(s),`Action ${s.turn}:
1073
- ${s.summary??pa(s)}`;default:return`Action ${s.turn}:
1198
+ `)}function Rc(s){let e=new Set,t=new Set,n=[],r=[],o="Continue from the latest live runtime state.";for(let i of s){_t(i),e.add(i.stepKind??"explore");for(let u of i.producedVars??[])t.add(u);let a=nr(i.output||"(no output)",200);n.push(`Turn ${i.turn}: ${a}`);let l=i.actorFieldsOutput.replace(/^Actor fields:\s*/i,"").trim();l&&r.push(`Turn ${i.turn}: ${l}`),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(`
1199
+ `)}async function Fc(s,e,t,n){let r=new Se("turns:string -> checkpointSummary:string",{...Mc(Mm,"ax-agent-checkpoint-summary",e)});try{let o=await r.forward(s,{turns:Fm(n)},Pc(t));return(typeof o.checkpointSummary=="string"?o.checkpointSummary.trim():String(o.checkpointSummary).trim())||Rc(n)}catch{return Rc(n)}}async function Aa(s,e,t,n,r){let o=s[e];if(!o)return;_t(o);let i=o.tags.includes("error");if(t.hindsightEvaluation&&s.length>=2){let a=s[s.length-2];vm(a,o)}if(t.errorPruning||t.tombstoning)for(let a of s){if(!a.tags.includes("error"))continue;let l=s.indexOf(a),u=s[l+1];if(!u||u.tags.includes("error")||(t.errorPruning&&!a.tombstone&&(a.tombstone=fa(a,u)),!(!!t.tombstoning&&!!n&&!a._tombstonePromise&&(!a.tombstone||Om(a.tombstone)))||!n))continue;let p=typeof t.tombstoning=="object"?t.tombstoning:void 0;a._tombstonePromise=Pm(n,p,r,a,u),a._tombstonePromise.then(d=>{a.tombstone=d}).catch(()=>{}).finally(()=>{a._tombstonePromise=void 0})}if(t.errorPruning&&!i){let a=s.filter(l=>!l.tags.includes("error")||l.tombstone!=null||l._tombstonePromise!=null);s.length=0,s.push(...a)}if(t.hindsightEvaluation){let a=s[s.length-1]?.turn??o.turn,l=s.filter((u,c)=>c===s.length-1||u.rank===void 0||!u.tags.includes("error")&&a-u.turn<t.rankPruneGraceTurns||u.rank>=t.pruneRank||u.tombstone!=null||u._tombstonePromise!=null);s.length=0,s.push(...l)}}function _m(s,e){if(e.has(s.turn)&&!s.tags.includes("error")&&s.replayMode!=="full")return"";if(s.tombstone)return`Action ${s.turn}:
1200
+ ${s.tombstone}`;switch(s.replayMode){case"omit":return _t(s),`Action ${s.turn}:
1201
+ ${s.summary??ha(s)}`;default:return`Action ${s.turn}:
1074
1202
  \`\`\`javascript
1075
1203
  ${s.code}
1076
1204
  \`\`\`
1077
1205
  Result:
1078
- ${s.output}${s.actorFieldsOutput}`}}function Rs(s,e){let t=ws(s,{pruneUsedDocs:e.pruneUsedDocs});if(t.length===0)return{promptFacingEntries:t,checkpointEntries:[],historyText:"",historyChars:0};Tm(t,e);let n=t.filter(i=>!i.tags.includes("error")&&i.replayMode!=="full"),r=new Set(e.checkpointTurns??[]),o=t.map(i=>Em(i,r)).filter(Boolean).join(`
1206
+ ${s.output}${s.actorFieldsOutput}`}}function Es(s,e){let t=Os(s,{pruneUsedDocs:e.pruneUsedDocs});if(t.length===0)return{promptFacingEntries:t,checkpointEntries:[],historyText:"",historyChars:0};km(t,e);let n=t.filter(i=>!i.tags.includes("error")&&i.replayMode!=="full"),r=new Set(e.checkpointTurns??[]),o=t.map(i=>_m(i,r)).filter(Boolean).join(`
1079
1207
 
1080
- `);return{promptFacingEntries:t,checkpointEntries:n,historyText:o,historyChars:o.length}}function Sc(s,e){let t=Rs(s,e);if(t.promptFacingEntries.length===0&&!e.stateSummary&&!e.checkpointSummary)return"";let n=[];return e.restoreNotice&&n.push(e.restoreNotice),e.stateSummary&&n.push(`Live Runtime State:
1208
+ `);return{promptFacingEntries:t,checkpointEntries:n,historyText:o,historyChars:o.length}}function _c(s,e){let t=Es(s,e);if(t.promptFacingEntries.length===0&&!e.delegatedContextSummary&&!e.stateSummary&&!e.checkpointSummary)return"";let n=[];return e.restoreNotice&&n.push(e.restoreNotice),e.delegatedContextSummary&&n.push(`Delegated Context (runtime-only \u2014 explore with code):
1209
+ ${e.delegatedContextSummary}`),e.stateSummary&&n.push(`Live Runtime State:
1081
1210
  ${e.stateSummary}`),t.historyText&&n.push(t.historyText),e.checkpointSummary&&n.push(`Checkpoint Summary:
1082
1211
  ${e.checkpointSummary}`),n.join(`
1083
1212
 
1084
- `)}function kc(s,e){let t=ws(s,{pruneUsedDocs:e?.pruneUsedDocs}),n=new Set(e?.checkpointTurns??[]),r=t.map(i=>{if(n.has(i.turn)&&!i.tags.includes("error"))return"";Mt(i);let a=i.tombstone??i.summary??pa(i);return`- Action ${i.turn}: ${a}`}).filter(Boolean).join(`
1213
+ `)}function Nc(s,e){let t=Os(s,{pruneUsedDocs:e?.pruneUsedDocs}),n=new Set(e?.checkpointTurns??[]),r=t.map(i=>{if(n.has(i.turn)&&!i.tags.includes("error"))return"";_t(i);let a=i.tombstone??i.summary??ha(i);return`- Action ${i.turn}: ${a}`}).filter(Boolean).join(`
1085
1214
  `),o=["Actor stopped without calling final(...). Evidence summary:"];return e?.checkpointSummary&&o.push(`Checkpoint summary:
1086
1215
  ${e.checkpointSummary}`),r?o.push(r):e?.checkpointSummary||o.push("- No actions were taken."),e?.stateSummary&&o.push(`Current runtime state:
1087
1216
  ${e.stateSummary}`),o.join(`
1088
- `)}function vc(s,e=[]){return`(() => {
1217
+ `)}function Lc(s,e=[]){return`(() => {
1089
1218
  const skip = new Set([${[...s,...e].map(n=>`'${n}'`).join(",")}]);
1090
1219
  const truncate = (text, maxChars) =>
1091
1220
  text.length <= maxChars ? text : text.slice(0, maxChars - 3) + '...';
@@ -1206,18 +1335,18 @@ ${e.stateSummary}`),o.join(`
1206
1335
  }
1207
1336
  });
1208
1337
  return JSON.stringify({ version: 1, entries });
1209
- })()`}function Oc(){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 Pm(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 ga(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=Mm(s);if(e.length===0)return"unknown";if(Pm(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?ga(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?Mc(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Mc(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,l])=>{let u=ga(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return s?.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function Fm(s){return s?ga(s):"unknown"}function Ec(s){let e=Mc(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${Fm(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function Ss(s,e,t,n){let r=n.promptLevel??"detailed",o=e.length>0?e.map(m=>{let g=Nn(m.type),f=m.isOptional?"optional":"required",x=m.description?`: ${m.description}`:"";return`- \`${m.name}\` -> \`inputs.${m.name}\` (${g}, ${f})${x}`}).join(`
1210
- `):"(none)",i=t.map(m=>`\`${m.name}\``).join(", "),a=[...n.agents??[]].sort((m,g)=>m.name.localeCompare(g.name)),l=[...n.agentFunctions??[]].sort((m,g)=>m.namespace!==g.namespace?m.namespace.localeCompare(g.namespace):m.name.localeCompare(g.name)),u=n.agentModuleNamespace??"agents",c=!!n.discoveryMode,p=n.availableModules?[...n.availableModules].sort((m,g)=>m.namespace.localeCompare(g.namespace)):[...new Set([...l.map(m=>m.namespace),...a.length>0?[u]:[]])].sort((m,g)=>m.localeCompare(g)).map(m=>({namespace:m})),d=ct("rlm/actor.md",{contextVarList:o,responderOutputFieldTitles:i,promptLevel:r,discoveryMode:c,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:!c&&a.length>0,agentModuleNamespace:u,agentFunctionsList:a.map(m=>Ec({qualifiedName:`${u}.${m.name}`,parameters:m.parameters})).join(`
1211
- `),hasFunctions:!c&&l.length>0,functionsList:l.map(m=>Ec({qualifiedName:`${m.namespace}.${m.name}`,parameters:m.parameters,returns:m.returns})).join(`
1338
+ })()`}function $c(){return"(() => JSON.stringify(Object.getOwnPropertyNames(globalThis).sort()))()"}function Nm(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 Lm(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 ya(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=Nm(s);if(e.length===0)return"unknown";if(Lm(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?ya(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?Gc(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Gc(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,l])=>{let u=ya(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return s?.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function $m(s){return s?ya(s):"unknown"}function Dc(s){let e=Gc(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${$m(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function Ms(s,e,t,n){let r=n.promptLevel??"detailed",o=e.length>0?e.map(m=>{let g=Dn(m.type),f=m.isOptional?"optional":"required",x=m.description?`: ${m.description}`:"";return`- \`${m.name}\` -> \`inputs.${m.name}\` (${g}, ${f})${x}`}).join(`
1339
+ `):"(none)",i=t.map(m=>`\`${m.name}\``).join(", "),a=[...n.agents??[]].sort((m,g)=>m.name.localeCompare(g.name)),l=[...n.agentFunctions??[]].sort((m,g)=>m.namespace!==g.namespace?m.namespace.localeCompare(g.namespace):m.name.localeCompare(g.name)),u=n.agentModuleNamespace??"agents",c=!!n.discoveryMode,p=n.availableModules?[...n.availableModules].sort((m,g)=>m.namespace.localeCompare(g.namespace)):[...new Set([...l.map(m=>m.namespace),...a.length>0?[u]:[]])].sort((m,g)=>m.localeCompare(g)).map(m=>({namespace:m})),d=xt("rlm/actor.md",{contextVarList:o,responderOutputFieldTitles:i,promptLevel:r,llmQueryPromptMode:n.llmQueryPromptMode??"simple",discoveryMode:c,hasInspectRuntime:!!n.hasInspectRuntime,hasAgentFunctions:!c&&a.length>0,agentModuleNamespace:u,agentFunctionsList:a.map(m=>Dc({qualifiedName:`${u}.${m.name}`,parameters:m.parameters})).join(`
1340
+ `),hasFunctions:!c&&l.length>0,functionsList:l.map(m=>Dc({qualifiedName:`${m.namespace}.${m.name}`,parameters:m.parameters,returns:m.returns})).join(`
1212
1341
  `),hasModules:c&&p.length>0,modulesList:p.map(m=>m.selectionCriteria?.trim()?`- \`${m.namespace}\` - ${m.selectionCriteria.trim()}`:`- \`${m.namespace}\``).join(`
1213
1342
  `),runtimeUsageInstructions:String(n.runtimeUsageInstructions),enforceIncrementalConsoleTurns:!!n.enforceIncrementalConsoleTurns,hasLiveRuntimeState:!!n.hasLiveRuntimeState,hasCompressedActionReplay:!!n.hasCompressedActionReplay}).replace(/\n{3,}/g,`
1214
1343
 
1215
1344
  `).trim();return s?`${d}
1216
1345
 
1217
- ${s}`:d}function ks(s,e){let t=e.length>0?e.map(r=>{let o=Nn(r.type),i=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${i})`}).join(`
1218
- `):"(none)",n=ct("rlm/responder.md",{contextVarSummary:t}).trim();return s?`${n}
1346
+ ${s}`:d}function Ps(s,e){let t=e.length>0?e.map(r=>{let o=Dn(r.type),i=r.isOptional?"optional":"required";return`- \`${r.name}\` (${o}, ${i})`}).join(`
1347
+ `):"(none)",n=xt("rlm/responder.md",{contextVarSummary:t}).trim();return s?`${n}
1219
1348
 
1220
- ${s}`:n}var tr=class extends Error{question;clarification;stateSnapshot;stateErrorMessage;constructor(e,t){let n=Wc(e);super(n.question),this.name="AxAgentClarificationError",this.question=n.question,this.clarification=n,this.stateSnapshot=t?.state?er(t.state):void 0,this.stateErrorMessage=t?.stateError}getState(){if(this.stateErrorMessage)throw new Error(this.stateErrorMessage);return this.stateSnapshot?er(this.stateSnapshot):void 0}},Pc=50,_m=5e3,Nm=8,Fc=10,Lm=2,$m=1200,vs="agents",Dm=2,Gm=100,fa="listModuleFunctions",ha="getFunctionDefinitions",Um="AI service is required to use llmQuery(...) in AxAgent.test(). Pass options.ai or configure ai on the agent.",Ms="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",jm=new Fe(`
1349
+ ${s}`:n}var it=class extends Error{question;clarification;stateSnapshot;stateErrorMessage;constructor(e,t){let n=tp(e);super(n.question),this.name="AxAgentClarificationError",this.question=n.question,this.clarification=n,this.stateSnapshot=t?.state?rr(t.state):void 0,this.stateErrorMessage=t?.stateError}getState(){if(this.stateErrorMessage)throw new Error(this.stateErrorMessage);return this.stateSnapshot?rr(this.stateSnapshot):void 0}},Uc=50,Bc=5e3,Dm=8,jc=10,zc=2,Gm=1200,Fs="agents",Um=2,Bm=100,jm=/^[$A-Z_a-z][$0-9A-Z_a-z]*$/,zm=new Set(["context","__proto__","prototype","constructor","globalThis","global","self","window","console","JSON","Math","Reflect","Atomics","Array","Object","String","Number","Boolean","BigInt","Symbol","Date","RegExp","Error","EvalError","RangeError","ReferenceError","SyntaxError","TypeError","URIError","AggregateError","Map","Set","WeakMap","WeakSet","Promise","Proxy","Function","Intl","URL","URLSearchParams","TextEncoder","TextDecoder","AbortController","AbortSignal","parseInt","parseFloat","isNaN","isFinite","eval","undefined","Infinity","NaN","await","break","case","catch","class","const","continue","debugger","default","delete","do","else","enum","export","extends","false","finally","for","function","if","import","in","instanceof","new","null","return","super","switch","this","throw","true","try","typeof","var","void","while","with","yield","let","static","implements","interface","package","private","protected","public"]),ba="listModuleFunctions",Ia="getFunctionDefinitions",qm="AI service is required to use llmQuery(...) in AxAgent.test(). Pass options.ai or configure ai on the agent.",Ls="[The JavaScript runtime was restarted; all global state was lost and must be recreated if needed.]",Hm=new _e(`
1221
1350
  taskInput:json "The structured task input passed to the agent",
1222
1351
  criteria:string "Task-specific success criteria",
1223
1352
  expectedOutput?:json "Optional expected final output",
@@ -1233,11 +1362,11 @@ ${s}`:n}var tr=class extends Error{question;clarification;stateSnapshot;stateErr
1233
1362
  toolErrors:string[] "Function-call errors observed during the run",
1234
1363
  turnCount:number "Number of actor turns executed",
1235
1364
  usage:json "Optional usage summary for the run"
1236
- `),Bm=new Fe(`
1365
+ `),Km=new _e(`
1237
1366
  taskRecord:json "Full optimization task record, including the agent input and evaluation criteria"
1238
1367
  ->
1239
1368
  agentRunReport:json "Agent run report containing completion type, clarification or final output, action log, function calls, errors, and turn count"
1240
- `);function zm(s){return"train"in s?{train:s.train,validation:s.validation}:{train:s}}function dt(s){if(s!==void 0){if(s===null)return null;if(typeof s=="string"||typeof s=="number"||typeof s=="boolean")return s;if(Array.isArray(s))try{return JSON.parse(JSON.stringify(s))}catch{return s.map(e=>dt(e))}if(typeof s=="object")try{return JSON.parse(JSON.stringify(s))}catch{return String(s)}return String(s)}}function qm(s){let e=s.trim();for(;;){let t=e;if(e=e.replace(/^```(?:[A-Za-z0-9_-]+)?[ \t]*\r?\n/,""),e=e.replace(/\r?\n?```[ \t]*$/,""),e=e.trim(),e===t)return e}}function Hm(s){let e=`
1369
+ `);function Wm(s){return"train"in s?{train:s.train,validation:s.validation}:{train:s}}function yt(s){if(s!==void 0){if(s===null)return null;if(typeof s=="string"||typeof s=="number"||typeof s=="boolean")return s;if(Array.isArray(s))try{return JSON.parse(JSON.stringify(s))}catch{return s.map(e=>yt(e))}if(typeof s=="object")try{return JSON.parse(JSON.stringify(s))}catch{return String(s)}return String(s)}}function Vm(s){let e=s.trim();for(;;){let t=e;if(e=e.replace(/^```(?:[A-Za-z0-9_-]+)?[ \t]*\r?\n/,""),e=e.replace(/\r?\n?```[ \t]*$/,""),e=e.trim(),e===t)return e}}function Jm(s){let e=`
1241
1370
  Use the input field named "criteria" as the task-specific rubric for success.
1242
1371
  - Reward actual task completion over polished wording.
1243
1372
  - Reward correct tool choice and correct arguments.
@@ -1251,23 +1380,25 @@ Use the input field named "criteria" as the task-specific rubric for success.
1251
1380
  `.trim(),t=s?.trim();return t?`${e}
1252
1381
 
1253
1382
  Additional Evaluation Guidance:
1254
- ${t}`:e}function _c(s,e){return e.qualifiedName===s||e.name===s||e.qualifiedName.endsWith(`.${s}`)}function Km(s,e,t){let n=Math.max(0,Math.min(1,s)),r=e.expectedActions??[];if(r.length>0){let i=r.filter(a=>t.functionCalls.some(l=>_c(a,l))).length;n*=.5+.5*(i/r.length)}return(e.forbiddenActions??[]).some(i=>t.functionCalls.some(a=>_c(i,a)))&&(n*=.2),Math.max(0,Math.min(1,n))}function Wm(s,e){let t=new Set(s.map(r=>r.id));if(e==="actor"){if(!t.has("root.actor"))throw new Error("AxAgent.optimize(): root.actor is not available");return["root.actor"]}if(e==="responder"){if(!t.has("root.responder"))throw new Error("AxAgent.optimize(): root.responder is not available");return["root.responder"]}if(e==="all")return[...t];let n=[...e];for(let r of n)if(!t.has(r))throw new Error(`AxAgent.optimize(): unknown target program ID "${r}"`);return n}var nr=class extends Error{constructor(t){super(`AxAgent protocol completion: ${t}`);this.type=t;this.name="AxAgentProtocolCompletionSignal"}};function Nc(s){return{finalFunction:(...r)=>{s("final",r)},askClarificationFunction:(...r)=>{s("ask_clarification",r)},protocol:{final:(...r)=>{throw s("final",r),new nr("final")},askClarification:(...r)=>{throw s("ask_clarification",r),new nr("ask_clarification")}}}}function Vm(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 Lc(s){let e=s?.preset??"full",t=Jm(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?.pruneErrors??t.errorPruning,hindsightEvaluation:r,pruneRank:n?.minRank??t.pruneRank,rankPruneGraceTurns:Dm,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 Jm(s){switch(s){case"adaptive":return{actionReplay:"adaptive",recentFullActions:3,errorPruning:!0,hindsight:!1,pruneRank:2,pruneUsedDocs:!1,stateSummary:!0,inspect:!0,inspectThreshold:1e4,maxEntries:8,maxStateChars:1600,checkpointsEnabled:!0,checkpointTriggerChars:16e3};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 Kr=class s{ai;judgeAI;program;actorProgram;responderProgram;agents;agentFunctions;agentFunctionModuleMetadata=new Map;debug;options;rlmConfig;runtime;actorFieldNames;localFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;actorPromptLevel;responderDescription;judgeOptions;recursionForwardOptions;actorForwardOptions;responderForwardOptions;inputUpdateCallback;contextPromptConfigByField=new Map;agentModuleNamespace=vs;functionDiscoveryEnabled=!1;runtimeUsageInstructions="";enforceIncrementalConsoleTurns=!1;activeAbortControllers=new Set;_stopRequested=!1;state;stateError;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;_reservedAgentFunctionNamespaces(){return new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",vs,this.agentModuleNamespace,...this.functionDiscoveryEnabled?[fa,ha]:[]])}_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,judgeAI:t,agentIdentity:n,agentModuleNamespace:r,signature:o},i){let{debug:a,contextFields:l,runtime:u,maxSubAgentCalls:c,maxRuntimeChars:p,maxBatchedLlmQueryConcurrency:d,maxTurns:m,contextPolicy:g,actorFields:f,actorTurnCallback:x,mode:b,recursionOptions:h,actorOptions:y,responderOptions:I,judgeOptions:C,inputUpdateCallback:R}=i;this.ai=e,this.judgeAI=t,this.agents=i.agents?.local,this.functionDiscoveryEnabled=i.functions?.discovery??!1,this.debug=a,this.options=i,this.runtime=u??new Zt,this.runtimeUsageInstructions=this.runtime.getUsageInstructions(),this.enforceIncrementalConsoleTurns=yg(this.runtimeUsageInstructions);let P=r??n?.namespace??vs;if(this.agentModuleNamespace=wg(P,{normalize:r===void 0}),new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",fa,ha]).has(this.agentModuleNamespace))throw new Error(`Agent module namespace "${this.agentModuleNamespace}" is reserved`);let E=this._reservedAgentFunctionNamespaces(),k=xa(i.functions?.local,E),O=xa(i.functions?.shared,E),L=xa(i.functions?.globallyShared,E);this.agentFunctions=k.functions,this._mergeAgentFunctionModuleMetadata(k.moduleMetadata);let{agents:F,fields:M,functions:_,judgeOptions:j,inputUpdateCallback:H,...D}=i;this.program=new Se(o,D);let Y=this.program.getSignature().getInputFields(),G=hg(l,Y,$m);this.contextPromptConfigByField=G.promptConfigByField,this.rlmConfig={contextFields:G.contextFieldNames,sharedFields:i.fields?.shared,runtime:this.runtime,maxSubAgentCalls:c,maxRuntimeChars:p,maxBatchedLlmQueryConcurrency:d,maxTurns:m,contextPolicy:g,actorFields:f,actorTurnCallback:x,mode:b},this.recursionForwardOptions=h;let{description:me,promptLevel:ge,...Ae}=y??{},{description:U,...K}=I??{};this.actorDescription=me,this.actorPromptLevel=ge??"detailed",this.actorForwardOptions=Ae,this.responderDescription=U,this.responderForwardOptions=K,this.judgeOptions=C?{...C}:void 0,this.inputUpdateCallback=R;let W=this.agents;for(let q of W??[]){let ce=q.getFunction().name;this.program.register(q,ce)}n&&(this.func={name:Xc(n.name),description:n.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}});let re=f??[];this.actorFieldNames=re;let Q=i.fields?.shared??[];this.sharedFieldNames=Q,this.excludedSharedFields=i.fields?.excluded??[];let le=i.fields?.globallyShared??[];this.globalSharedFieldNames=le,this.localFieldNames=i.fields?.local??[];let Ie=i.agents?.shared??[],Ee=i.agents?.globallyShared??[];this.excludedAgents=i.agents?.excluded??[];let Me=O.functions,oe=L.functions;this.excludedAgentFunctions=i.functions?.excluded??[];let Re=[...this.agentFunctions,...Me,...oe];for(let q of Re){if(!q.parameters)throw new Error(`Agent function "${q.name}" must define parameters schema for agent runtime usage.`);if(q.examples){for(let[ce,fe]of q.examples.entries())if(!fe.code.trim())throw new Error(`Agent function "${q.name}" example at index ${ce} must define non-empty code`)}}if(this._validateConfiguredSignature(this.program.getSignature()),this._validateAgentFunctionNamespaces(Re),Q.length>0&&W){let q=Y.filter(ce=>Q.includes(ce.name));for(let ce of W){if(!(ce instanceof s))continue;let fe=new Set(ce.getExcludedSharedFields()),he=q.filter(we=>!fe.has(we.name));he.length!==0&&ce._extendForSharedFields(he,this.rlmConfig.contextFields)}}if(Ie.length>0&&W)for(let q of W)q instanceof s&&q._extendForSharedAgents(Ie);if(le.length>0&&W){let q=Y.filter(ce=>le.includes(ce.name));for(let ce of W){if(!(ce instanceof s))continue;let fe=new Set(ce.getExcludedSharedFields()),he=q.filter(we=>!fe.has(we.name));he.length!==0&&ce._extendForGlobalSharedFields(he,this.rlmConfig.contextFields)}}if(Ee.length>0&&W)for(let q of W)q instanceof s&&q._extendForGlobalSharedAgents(Ee);if(Me.length>0&&W)for(let q of W)q instanceof s&&q._extendForSharedAgentFunctions(O);if(oe.length>0&&W)for(let q of W)q instanceof s&&q._extendForGlobalSharedAgentFunctions(L);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(R=>t.includes(R.name)),o=r.filter(R=>this.contextPromptConfigByField.has(R.name)&&!n.has(R.name)).map(R=>({...R,isOptional:!0})),i=e.filter(R=>!t.includes(R.name)&&!n.has(R.name)),a=this.program.getSignature().getOutputFields(),l=a.filter(R=>this.actorFieldNames.includes(R.name)),u=a.filter(R=>!this.actorFieldNames.includes(R.name)),c=pe().addInputFields(i).addInputFields(o).input("contextMetadata",pe.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",pe.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",pe.code("JavaScript code to execute in runtime session"));l.length>0&&(c=c.addOutputFields(l));let p=c.build(),d=pe().addInputFields(i).input("contextData",pe.json("Context data to help synthesize the final answer.")).addOutputFields(u).build(),m=this.rlmConfig.maxSubAgentCalls??Pc,g=this.rlmConfig.maxTurns??Fc,f=this.agents?.map(R=>{let P=R.getFunction();return{name:P.name,description:P.description,parameters:P.parameters}})??[],x=this.agentFunctions.map(R=>({name:R.name,description:R.description,parameters:R.parameters,returns:R.returns,namespace:R.namespace??"utils"})),b=new Set(x.map(R=>R.namespace??"utils"));f.length>0&&b.add(this.agentModuleNamespace);let h=[...b].sort((R,P)=>R.localeCompare(P)).map(R=>({namespace:R,selectionCriteria:this.agentFunctionModuleMetadata.get(R)?.selectionCriteria})),y=Lc(this.rlmConfig.contextPolicy),I=Ss(this.actorDescription,r,u,{runtimeUsageInstructions:this.runtimeUsageInstructions,promptLevel:this.actorPromptLevel,maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:y.stateInspection.enabled,hasLiveRuntimeState:y.stateSummary.enabled,hasCompressedActionReplay:y.actionReplay!=="full"||y.checkpoints.enabled||y.errorPruning||!!y.tombstoning||this.functionDiscoveryEnabled&&y.pruneUsedDocs,enforceIncrementalConsoleTurns:this.enforceIncrementalConsoleTurns,agentModuleNamespace:this.agentModuleNamespace,discoveryMode:this.functionDiscoveryEnabled,availableModules:h,agents:f,agentFunctions:x}),C=ks(this.responderDescription,r);this.actorProgram?(this.actorProgram.setSignature(p),this.actorProgram.setDescription(I)):this.actorProgram=new Se(p,{...this._genOptions,description:I}),this.responderProgram?(this.responderProgram.setSignature(d),this.responderProgram.setDescription(C)):this.responderProgram=new Se(d,{...this._genOptions,description:C})}_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()),l=e.filter(u=>!a.has(u.name));l.length!==0&&i._extendForGlobalSharedFields(l,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()}getState(){if(this.stateError)throw new Error(this.stateError);return this.state?er(this.state):void 0}setState(e){if(e&&e.version!==1)throw new Error(`Unsupported AxAgentState version "${String(e.version)}"`);if(e){let t=this.runtime.createSession();try{if(typeof t.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required to restore AxAgent state")}finally{try{t.close()}catch{}}}this.state=e?er(e):void 0,this.stateError=void 0}async optimize(e,t){let n=zm(e);if(n.train.length===0)throw new Error("AxAgent.optimize(): at least one training task is required.");let r=t?.studentAI??this.ai;if(!r)throw new Error("AxAgent.optimize(): studentAI is required when the agent has no default ai.");let o=t?.judgeAI??this.judgeAI??t?.teacherAI??this.ai??r,i={...this.judgeOptions??{},...t?.judgeOptions??{}},a=Wm(this.namedPrograms(),t?.target??"actor"),l=t?.metric??this._createAgentOptimizeMetric(o,i),u=this._createOptimizationProgram(a),c=Math.max(1,Math.floor(t?.maxMetricCalls??Math.max(Gm,n.train.length*4))),d=await new Ut({studentAI:r,teacherAI:t?.teacherAI??o,verbose:t?.verbose,debugOptimizer:t?.debugOptimizer,optimizerLogger:t?.optimizerLogger,onProgress:t?.onProgress,onEarlyStop:t?.onEarlyStop}).compile(u,n.train,l,{validationExamples:n.validation,maxMetricCalls:c,verbose:t?.verbose});return t?.apply!==!1&&d.optimizedProgram&&this.applyOptimization(d.optimizedProgram),d}_createOptimizationProgram(e){return{getId:()=>this.getId(),setId:t=>this.setId(t),getSignature:()=>Bm,forward:async(t,n,r)=>this._forwardForEvaluation(t,n,r),streamingForward:async function*(t,n,r){yield{version:1,index:0,delta:await this.forward(t,n,r)}},getTraces:()=>this.getTraces(),namedProgramInstances:()=>this.namedProgramInstances().filter(t=>e.includes(t.id)),setDemos:(t,n)=>this.setDemos(t,n),applyOptimization:t=>this.applyOptimization(t),getUsage:()=>this.getUsage(),resetUsage:()=>this.resetUsage()}}_createAgentOptimizeMetric(e,t){let n=Hm(t.criteria),r=new Gt(jm,{ai:e,...t,criteria:n});return async({example:o,prediction:i})=>{let a=o,l=i,u={taskInput:dt(a.input),criteria:a.criteria,expectedOutput:a.expectedOutput,expectedActions:a.expectedActions,forbiddenActions:a.forbiddenActions,metadata:a.metadata},c={completionType:l.completionType,clarification:dt(l.clarification),finalOutput:dt(l.output),actionLog:l.actionLog,functionCalls:dt(l.functionCalls),toolErrors:l.toolErrors,turnCount:l.turnCount,usage:dt(l.usage??[])},p=await r.evaluate(u,c);return Km(p.score,a,l)}}async _forwardForEvaluation(e,t,n){let r=this.state?er(this.state):void 0,o=this.stateError;this.state=void 0,this.stateError=void 0;let i=new AbortController;this._stopRequested&&i.abort("Stopped by user (pre-forward)");let a=Tt(i.signal,n?.abortSignal);this.activeAbortControllers.add(i);try{let l=this.ai??e,u=n?.debug??this.debug??l?.getOptions()?.debug??!1,c=[],{nonContextValues:p,actorResult:d,actorFieldValues:m,actionLog:g,turnCount:f}=await this._runActorLoop(l,t.input,n,a,c),x=c.filter(y=>!!y.error).map(y=>`${y.qualifiedName}: ${y.error??"unknown error"}`);if(d.type==="ask_clarification")return{completionType:"ask_clarification",clarification:Wc(d.args[0]),actionLog:g,functionCalls:c,toolErrors:x,turnCount:f};let b={...this._genOptions,...this.responderForwardOptions,...n,debug:u,abortSignal:a,maxSteps:1};return{completionType:"final",output:{...await this.responderProgram.forward(l,{...p,contextData:d},b),...m},actionLog:g,functionCalls:c,toolErrors:x,turnCount:f}}finally{this.state=r?er(r):void 0,this.stateError=o,this.activeAbortControllers.delete(i),this._stopRequested=!1}}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),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(`
1255
- `)}}}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,b)=>({...x,...b.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],l=this._getBypassedSharedFieldNames(),u={},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:u,recomputeTurnInputs:x=>{let b={},h={};for(let[I,C]of Object.entries(o))this.rlmConfig.contextFields.includes(I)?b[I]=C:l.has(I)||(h[I]=C);if(x){for(let I of this.rlmConfig.contextFields)if(!g.has(I)&&(!(I in b)||b[I]===void 0))throw new Error(`RLM contextField "${I}" is missing from input values`)}let y={};for(let[I,C]of this.contextPromptConfigByField){if(l.has(I)||!(I in b))continue;let R=mg(b[I],C);R!==void 0&&(y[I]=R)}c=b,p=h,d=y;for(let I of Object.keys(u))delete u[I];for(let I of a)I in o&&(u[I]=o[I]),I in c&&(u[I]=c[I]);m=xg(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:l,functionCallRecorder:u}){let c=this.rlmConfig,p=this.runtime,d=c.maxSubAgentCalls??Pc,m=c.maxRuntimeChars??_m,g=Math.max(1,c.maxBatchedLlmQueryConcurrency??Nm),f=this.recursionForwardOptions?.maxDepth??Lm,x=Math.max(0,f),b=Lc(c.contextPolicy),h=0,y=Math.floor(d*.8),{maxDepth:I,...C}=this.recursionForwardOptions??{},{description:R,mem:P,sessionId:v,...E}=n??{},k={...this.recursionForwardOptions??{},maxDepth:Math.max(0,x-1)},O=["context"],L=pe().input("task",pe.string("Task for recursive analysis")).input("context",pe.json("Optional context for the recursive task")).output("answer",pe.string("Answer from recursive analysis")).build(),M=(c.mode??"simple")==="advanced"&&(k.maxDepth??0)>0?"advanced":"simple",_;x>0&&(M==="advanced"?_=new s({agentModuleNamespace:this.agentModuleNamespace,signature:L},{debug:o,...c,agents:{local:this.agents},functions:{local:this.agentFunctions,discovery:this.functionDiscoveryEnabled},contextFields:O,actorFields:void 0,recursionOptions:k,actorOptions:this.actorForwardOptions,responderOptions:this.responderForwardOptions}):_=new Se(L,k));let j=async(N,V)=>{if(!Array.isArray(N)&&typeof N=="object"&&N!==null&&"query"in N)return j(N.query,N.context??V);if(r?.aborted)throw new $e("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(Array.isArray(N))return Ag(N,g,async w=>{try{return await j(w.query,w.context)}catch(S){if(S instanceof $e)throw S;return`[ERROR] ${S instanceof Error?S.message:String(S)}`}});if(!e)throw new Error(Um);let ne=N,Te=w=>{if(w==null)return"";if(typeof w=="string")return mt(w,m);try{return mt(JSON.stringify(w),m)}catch{return mt(String(w),m)}},A=await(async(w,S)=>{let T=S===void 0?void 0:typeof S=="string"?mt(S,m):S;if(h++,h>d)return`[ERROR] Sub-query budget exhausted (${d}/${d}). Use the data you have already accumulated to produce your final answer.`;if(x<=0||!_)return`[ERROR] Recursion depth limit reached (${f}).`;let $=3,z,J=B=>`[ERROR] ${B instanceof Error?B.message:String(B)}`;for(let B=0;B<$;B++)try{let X=await _.forward(e,{task:w,...T!==void 0?{context:T}:M==="advanced"?{context:""}:{}},{...E,...C,abortSignal:r,debug:o});return Te(X.answer)}catch(X){if(X instanceof $e)throw X;if(z=X,!og(X)||B>=$-1)return J(X);let xe=Math.min(6e4,1e3*Math.pow(2,B));await new Promise((_e,se)=>{let Ce=!1,Ne,Oe=()=>{r&&Ne&&r.removeEventListener("abort",Ne)},Ct=setTimeout(()=>{Ce||(Ce=!0,Oe(),_e())},xe);if(r){if(Ne=()=>{Ce||(Ce=!0,clearTimeout(Ct),Oe(),se(new $e("rlm-llm-query-retry-backoff",r.reason?String(r.reason):"Aborted during retry backoff")))},r.aborted){Ne();return}r.addEventListener("abort",Ne,{once:!0})}})}return J(z)})(ne,V);return h===y?`${A}
1256
- [WARNING] ${h}/${d} sub-queries used. Plan to wrap up soon.`:A},H=this.buildRuntimeGlobals(r,t.sharedFieldValues,e,a.protocol,u),D=[...new Set(this.agentFunctions.map(N=>N.namespace??"utils"))],Y={...t.currentInputs},G=new Set(["inputs","llmQuery",vs,this.agentModuleNamespace,"final","ask_clarification",...D,...b.stateInspection.enabled?["inspect_runtime"]:[],...Object.keys(H)]),me=[...new Set([...Object.keys(Y),...t.signatureInputFieldNames])].filter(N=>!G.has(N)),ge={};for(let N of me)ge[N]=Y[N];let Ae=()=>{for(let N of Object.keys(Y))delete Y[N];for(let[N,V]of Object.entries(t.currentInputs))Y[N]=V;for(let N of me)ge[N]=t.currentInputs[N]},U=[...G],K=[...G,...me],W=l??[],re,Q,le=N=>typeof N.inspectGlobals=="function"?N:void 0,Ie=async()=>{try{let N=await re.execute(Oc(),{signal:r,reservedNames:K});if(typeof N!="string")return[];let V=JSON.parse(N);return Array.isArray(V)?V.filter(ne=>typeof ne=="string"):[]}catch{return[]}},Ee=async()=>(Q||(Q=await Ie()),Q),Me=async()=>{try{let N=le(re);if(N?.inspectGlobals)return await N.inspectGlobals({signal:r,reservedNames:K});let V=await Ee(),ne=vc(K,V),Te=await re.execute(ne,{signal:r,reservedNames:K});return typeof Te=="string"?Te:String(Te)}catch(N){return`[inspect_runtime error: ${N instanceof Error?N.message:String(N)}]`}},oe=(N,V)=>{let ne=ag(N);if(!ne)return lg(N,V);let Te=Ts(W);return Gc(ne,Te,V)},Re=b.stateInspection.enabled?async()=>oe(await Me()):void 0,q=()=>(Q=void 0,p.createSession({...ge,inputs:Y,llmQuery:j,final:a.finalFunction,ask_clarification:a.askClarificationFunction,...Re?{inspect_runtime:Re}:{},...H}));re=q();let ce=async()=>{if(!i.payload)for(let N=0;N<3&&!i.payload;N++)await new Promise(V=>{setTimeout(V,0)})},fe=async()=>{if(!b.stateSummary.enabled)return;let N=await Me();return oe(N,{maxEntries:b.stateSummary.maxEntries&&b.stateSummary.maxEntries>0?b.stateSummary.maxEntries:8,maxChars:b.stateSummary.maxChars&&b.stateSummary.maxChars>0?b.stateSummary.maxChars:void 0})||"(no user variables)"},he=N=>{if(typeof N.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required when restoring AxAgent state or using inputUpdateCallback");return N},we=N=>{if(typeof N.snapshotGlobals!="function")throw new Error("AxCodeSession.snapshotGlobals() is required to export AxAgent state");return N},ke=N=>{let V=new Set(K),ne={};for(let[ze,A]of Object.entries(N.runtimeBindings??{}))V.has(ze)||(ne[ze]=A);let Te=(N.runtimeEntries??[]).filter(ze=>!V.has(ze.name));return{runtimeBindings:ne,runtimeEntries:Te,actionLogEntries:Zm(N.actionLogEntries),checkpointState:N.checkpointState,provenance:{...N.provenance??{}}}};return{effectiveContextConfig:b,captureRuntimeStateSummary:fe,exportRuntimeState:async()=>{let V=await we(re).snapshotGlobals({signal:r,reservedNames:K}),ne=Ts(W);return{version:1,runtimeBindings:V.bindings,runtimeEntries:V.entries,actionLogEntries:Xm(W),provenance:eg(ne)}},restoreRuntimeState:async N=>{let V=ke(N);return await he(re).patchGlobals(V.runtimeBindings,{signal:r}),V},syncRuntimeInputsToSession:async()=>{Ae();let N=async V=>{await he(V).patchGlobals({inputs:{...Y},...ge},{signal:r})};try{await N(re)}catch(V){if(r?.aborted)throw new $e("rlm-session",r.reason??"Aborted");if(V instanceof Error&&(V.name==="AbortError"||V.message.startsWith("Aborted")))throw V;if(Bc(V)){re=q(),await N(re);return}throw new Error(`Failed to sync runtime inputs: ${Es(V,m)}`)}},executeActorCode:async N=>{let V={result:void 0,output:Os(void 0,m),isError:!1};try{let ne=await re.execute(N,{signal:r,reservedNames:U});return i.payload||Uc(N)&&jc(ne)&&(await ce(),i.payload)?V:{result:ne,output:Os(ne,m),isError:!1}}catch(ne){if(ne instanceof nr||i.payload)return V;if(r?.aborted)throw new $e("rlm-session",r.reason??"Aborted");if(ne instanceof Error&&(ne.name==="AbortError"||ne.message.startsWith("Aborted")))throw ne;if(pg(ne))return{result:void 0,output:mt(`${Ms}
1257
- ${Es(ne,m)}`,m),isError:!0};if(Bc(ne))try{re=q(),i.payload=void 0;let Te=await re.execute(N,{signal:r,reservedNames:U});return{result:Te,output:mt(`${Ms}
1258
- ${Os(Te,m)}`,m),isError:!1}}catch(Te){return{result:void 0,output:mt(`${Ms}
1259
- ${Es(Te,m)}`,m),isError:!0}}return{result:void 0,output:mt(Es(ne,m),m),isError:!0}}},executeTestCode:async N=>{try{let V=await re.execute(N,{signal:r,reservedNames:U});if(Uc(N)&&jc(V)&&await ce(),i.payload)return i.payload;let ne=Os(V,m);if(dg(ne))throw new Error(ne);return ne}catch(V){if((V instanceof nr||i.payload)&&i.payload)return i.payload;throw V}},close:()=>{re.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},l=Nc((c,p)=>{a.payload=$c(c,p)}),u=this._createRuntimeExecutionContext({ai:r,inputState:i,options:void 0,effectiveAbortSignal:n?.abortSignal,debug:o,completionState:a,completionBindings:l,actionLogEntries:[]});try{return await u.executeTestCode(e)}finally{u.close()}}setSignature(e){let t=new Fe(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,o){let i=this.rlmConfig,a=n?.debug??this.debug??e?.getOptions()?.debug??!1,l=i.maxTurns??Fc,u=this._createRuntimeInputState(t);u.recomputeTurnInputs(!1);let c={payload:void 0},p=Nc((O,L)=>{c.payload=$c(O,L)}),d=[],m,g=this._createRuntimeExecutionContext({ai:e,inputState:u,options:n,effectiveAbortSignal:r,debug:a,completionState:c,completionBindings:p,actionLogEntries:d,functionCallRecorder:o?O=>{o.push(O)}:void 0}),f=async()=>{if(!this.inputUpdateCallback)return;let O=await this.inputUpdateCallback({...u.currentInputs});if(O!==void 0){if(!O||typeof O!="object"||Array.isArray(O))throw new Error("inputUpdateCallback must return an object patch or undefined");for(let[L,F]of Object.entries(O))u.signatureInputFieldNames.has(L)&&(u.currentInputs[L]=F)}},x={...this._genOptions,...this.actorForwardOptions,...n,debug:a,abortSignal:r},b={},h=g.effectiveContextConfig.stateInspection.enabled?g.effectiveContextConfig.stateInspection.contextThreshold:void 0,y=Vm(n,a,r),I=this.functionDiscoveryEnabled&&g.effectiveContextConfig.pruneUsedDocs,C,R,P=()=>ws(d,{pruneUsedDocs:I}),v=()=>Sc(P(),{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,restoreNotice:R,stateSummary:m,checkpointSummary:C?.summary,checkpointTurns:C?.turns})||"(no actions yet)",E=async()=>{if(!g.effectiveContextConfig.checkpoints.enabled){C=void 0;return}let O=Rs(d,{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,pruneUsedDocs:I}),L=g.effectiveContextConfig.checkpoints.triggerChars;if(!L||O.historyChars<=L){C=void 0;return}let F=O.checkpointEntries;if(F.length===0){C=void 0;return}let M=JSON.stringify(F.map(_=>({turn:_.turn,code:_.code,output:_.output,actorFieldsOutput:_.actorFieldsOutput,tags:_.tags,tombstone:_.tombstone})));C?.fingerprint!==M&&(C={fingerprint:M,turns:F.map(_=>_.turn),summary:await Rc(e,g.effectiveContextConfig.summarizerOptions,y,F)})};try{if(this.state){let O=await g.restoreRuntimeState(this.state),L=g.effectiveContextConfig.stateSummary.enabled;d.push(...O.actionLogEntries),C=O.checkpointState?{fingerprint:O.checkpointState.fingerprint,turns:[...O.checkpointState.turns],summary:O.checkpointState.summary}:void 0;let F=rg(Ts(d),ng(O.provenance));m=L?Gc(O.runtimeEntries,F,{maxEntries:g.effectiveContextConfig.stateSummary.maxEntries&&g.effectiveContextConfig.stateSummary.maxEntries>0?g.effectiveContextConfig.stateSummary.maxEntries:8,maxChars:g.effectiveContextConfig.stateSummary.maxChars&&g.effectiveContextConfig.stateSummary.maxChars>0?g.effectiveContextConfig.stateSummary.maxChars:1200})||"(no user variables)":void 0,R=tg(O.runtimeEntries,{includeLiveRuntimeState:L})}for(let O=0;O<l;O++){await f(),u.recomputeTurnInputs(!0),await E();let L=v(),F=Rs(d,{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,pruneUsedDocs:I,checkpointTurns:C?.turns});h&&F.historyChars>h&&(L+="\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 M=await this.actorProgram.forward(e,{...u.getNonContextValues(),...u.getActorInlineContextValues(),contextMetadata:u.getContextMetadata(),actionLog:L},x);O===0&&(x.debugHideSystemPrompt=!0,R=void 0);let _=M.javascriptCode,j=_?.trim();if(!_||!j)break;_=qm(j),M.javascriptCode=_;for(let ge of this.actorFieldNames)ge in M&&(b[ge]=M[ge]);let H="";if(this.actorFieldNames.length>0){let ge=this.actorFieldNames.filter(Ae=>Ae in M).map(Ae=>`${Ae}: ${M[Ae]}`).join(`
1260
- `);ge&&(H=`
1383
+ ${t}`:e}function qc(s,e){return e.qualifiedName===s||e.name===s||e.qualifiedName.endsWith(`.${s}`)}function Qm(s,e,t){let n=Math.max(0,Math.min(1,s)),r=e.expectedActions??[];if(r.length>0){let i=r.filter(a=>t.functionCalls.some(l=>qc(a,l))).length;n*=.5+.5*(i/r.length)}return(e.forbiddenActions??[]).some(i=>t.functionCalls.some(a=>qc(i,a)))&&(n*=.2),Math.max(0,Math.min(1,n))}function Ym(s,e){let t=new Set(s.map(r=>r.id));if(e==="actor"){if(!t.has("root.actor"))throw new Error("AxAgent.optimize(): root.actor is not available");return["root.actor"]}if(e==="responder"){if(!t.has("root.responder"))throw new Error("AxAgent.optimize(): root.responder is not available");return["root.responder"]}if(e==="all")return[...t];let n=[...e];for(let r of n)if(!t.has(r))throw new Error(`AxAgent.optimize(): unknown target program ID "${r}"`);return n}var or=class extends Error{constructor(t){super(`AxAgent protocol completion: ${t}`);this.type=t;this.name="AxAgentProtocolCompletionSignal"}};function Hc(s){return{finalFunction:(...r)=>{s("final",r)},askClarificationFunction:(...r)=>{s("ask_clarification",r)},protocol:{final:(...r)=>{throw s("final",r),new or("final")},askClarification:(...r)=>{throw s("ask_clarification",r),new or("ask_clarification")}}}}function Xm(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=Zm(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?.pruneErrors??t.errorPruning,hindsightEvaluation:r,pruneRank:n?.minRank??t.pruneRank,rankPruneGraceTurns:Um,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 Zm(s){switch(s){case"adaptive":return{actionReplay:"adaptive",recentFullActions:3,errorPruning:!0,hindsight:!1,pruneRank:2,pruneUsedDocs:!1,stateSummary:!0,inspect:!0,inspectThreshold:1e4,maxEntries:8,maxStateChars:1600,checkpointsEnabled:!0,checkpointTriggerChars:16e3};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 Qr=class s{ai;judgeAI;program;actorProgram;responderProgram;agents;agentFunctions;agentFunctionModuleMetadata=new Map;debug;options;rlmConfig;runtime;actorFieldNames;localFieldNames;sharedFieldNames;globalSharedFieldNames;excludedSharedFields;excludedAgents;excludedAgentFunctions;actorDescription;actorPromptLevel;responderDescription;judgeOptions;recursionForwardOptions;actorForwardOptions;responderForwardOptions;inputUpdateCallback;contextPromptConfigByField=new Map;agentModuleNamespace=Fs;functionDiscoveryEnabled=!1;runtimeUsageInstructions="";enforceIncrementalConsoleTurns=!1;activeAbortControllers=new Set;_stopRequested=!1;state;stateError;runtimeBootstrapContext=void 0;llmQueryBudgetState;func;_parentSharedFields=new Set;_parentSharedAgents=new Set;_parentSharedAgentFunctions=new Set;_reservedAgentFunctionNamespaces(){return new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",Fs,this.agentModuleNamespace,...this.functionDiscoveryEnabled?[ba,Ia]:[]])}_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,judgeAI:t,agentIdentity:n,agentModuleNamespace:r,signature:o},i){let{debug:a,contextFields:l,runtime:u,maxSubAgentCalls:c,maxRuntimeChars:p,maxBatchedLlmQueryConcurrency:d,maxTurns:m,contextPolicy:g,actorFields:f,actorTurnCallback:x,mode:A,recursionOptions:h,actorOptions:b,responderOptions:I,judgeOptions:C,inputUpdateCallback:R}=i;this.ai=e,this.judgeAI=t,this.agents=i.agents?.local,this.functionDiscoveryEnabled=i.functions?.discovery??!1,this.debug=a,this.options=i,this.runtime=u??new nn,this.runtimeUsageInstructions=this.runtime.getUsageInstructions(),this.enforceIncrementalConsoleTurns=Mg(this.runtimeUsageInstructions);let M=r??n?.namespace??Fs;if(this.agentModuleNamespace=Lg(M,{normalize:r===void 0}),new Set(["inputs","llmQuery","final","ask_clarification","inspect_runtime",ba,Ia]).has(this.agentModuleNamespace))throw new Error(`Agent module namespace "${this.agentModuleNamespace}" is reserved`);let O=this._reservedAgentFunctionNamespaces(),k=Ca(i.functions?.local,O),_=Ca(i.functions?.shared,O),L=Ca(i.functions?.globallyShared,O);this.agentFunctions=k.functions,this._mergeAgentFunctionModuleMetadata(k.moduleMetadata);let{agents:E,fields:P,functions:$,judgeOptions:D,inputUpdateCallback:z,...U}=i;this.program=new Se(o,U);let te=this.program.getSignature().getInputFields(),B=vg(l,te,Gm);this.contextPromptConfigByField=B.promptConfigByField,this.rlmConfig={contextFields:B.contextFieldNames,sharedFields:i.fields?.shared,runtime:this.runtime,maxSubAgentCalls:c,maxRuntimeChars:p,maxBatchedLlmQueryConcurrency:d,maxTurns:m,contextPolicy:g,actorFields:f,actorTurnCallback:x,mode:A},this.recursionForwardOptions=h;let{description:ge,promptLevel:Ce,...ke}=b??{},{description:N,...q}=I??{};this.actorDescription=ge,this.actorPromptLevel=Ce??"detailed",this.actorForwardOptions=ke,this.responderDescription=N,this.responderForwardOptions=q,this.judgeOptions=C?{...C}:void 0,this.inputUpdateCallback=R;let W=this.agents;for(let H of W??[]){let re=H.getFunction().name;this.program.register(H,re)}n&&(this.func={name:ip(n.name),description:n.description,parameters:this._buildFuncParameters(),func:async()=>{throw new Error("Use getFunction() to get a callable wrapper")}});let he=f??[];this.actorFieldNames=he;let ee=i.fields?.shared??[];this.sharedFieldNames=ee,this.excludedSharedFields=i.fields?.excluded??[];let ie=i.fields?.globallyShared??[];this.globalSharedFieldNames=ie,this.localFieldNames=i.fields?.local??[];let fe=i.agents?.shared??[],Pe=i.agents?.globallyShared??[];this.excludedAgents=i.agents?.excluded??[];let Ne=_.functions,oe=L.functions;this.excludedAgentFunctions=i.functions?.excluded??[];let ve=[...this.agentFunctions,...Ne,...oe];for(let H of ve){if(!H.parameters)throw new Error(`Agent function "${H.name}" must define parameters schema for agent runtime usage.`);if(H.examples){for(let[re,de]of H.examples.entries())if(!de.code.trim())throw new Error(`Agent function "${H.name}" example at index ${re} must define non-empty code`)}}if(this._validateConfiguredSignature(this.program.getSignature()),this._validateAgentFunctionNamespaces(ve),ee.length>0&&W){let H=te.filter(re=>ee.includes(re.name));for(let re of W){if(!(re instanceof s))continue;let de=new Set(re.getExcludedSharedFields()),ye=H.filter(we=>!de.has(we.name));ye.length!==0&&re._extendForSharedFields(ye,this.rlmConfig.contextFields)}}if(fe.length>0&&W)for(let H of W)H instanceof s&&H._extendForSharedAgents(fe);if(ie.length>0&&W){let H=te.filter(re=>ie.includes(re.name));for(let re of W){if(!(re instanceof s))continue;let de=new Set(re.getExcludedSharedFields()),ye=H.filter(we=>!de.has(we.name));ye.length!==0&&re._extendForGlobalSharedFields(ye,this.rlmConfig.contextFields)}}if(Pe.length>0&&W)for(let H of W)H instanceof s&&H._extendForGlobalSharedAgents(Pe);if(Ne.length>0&&W)for(let H of W)H instanceof s&&H._extendForSharedAgentFunctions(_);if(oe.length>0&&W)for(let H of W)H instanceof s&&H._extendForGlobalSharedAgentFunctions(L);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(S=>t.includes(S.name)),o=r.filter(S=>this.contextPromptConfigByField.has(S.name)&&!n.has(S.name)).map(S=>({...S,isOptional:!0})),i=e.filter(S=>!t.includes(S.name)&&!n.has(S.name)),a=this.program.getSignature().getOutputFields(),l=a.filter(S=>this.actorFieldNames.includes(S.name)),u=a.filter(S=>!this.actorFieldNames.includes(S.name)),c=ae().addInputFields(i).addInputFields(o).input("contextMetadata",ae.string("Metadata about pre-loaded context variables (type and size)")).input("actionLog",ae.string("Chronological trace of code executions or actions and their outputs so far")).output("javascriptCode",ae.code("JavaScript code to execute in runtime session"));l.length>0&&(c=c.addOutputFields(l));let p=c.build(),d=ae().addInputFields(i).input("contextData",ae.json("Context data to help synthesize the final answer.")).addOutputFields(u).build(),m=this.rlmConfig.maxSubAgentCalls??Uc,g=this.rlmConfig.maxTurns??jc,f=this.recursionForwardOptions?.maxDepth??zc,x=(this.rlmConfig.mode??"simple")==="advanced"?Math.max(0,f)>0?"advanced-recursive":"simple-at-terminal-depth":"simple",A=this.agents?.map(S=>{let O=S.getFunction();return{name:O.name,description:O.description,parameters:O.parameters}})??[],h=this.agentFunctions.map(S=>({name:S.name,description:S.description,parameters:S.parameters,returns:S.returns,namespace:S.namespace??"utils"})),b=new Set(h.map(S=>S.namespace??"utils"));A.length>0&&b.add(this.agentModuleNamespace);let I=[...b].sort((S,O)=>S.localeCompare(O)).map(S=>({namespace:S,selectionCriteria:this.agentFunctionModuleMetadata.get(S)?.selectionCriteria})),C=Kc(this.rlmConfig.contextPolicy),R=Ms(this.actorDescription,r,u,{runtimeUsageInstructions:this.runtimeUsageInstructions,promptLevel:this.actorPromptLevel,maxSubAgentCalls:m,maxTurns:g,hasInspectRuntime:C.stateInspection.enabled,hasLiveRuntimeState:C.stateSummary.enabled,hasCompressedActionReplay:C.actionReplay!=="full"||C.checkpoints.enabled||C.errorPruning||!!C.tombstoning||this.functionDiscoveryEnabled&&C.pruneUsedDocs,llmQueryPromptMode:x,enforceIncrementalConsoleTurns:this.enforceIncrementalConsoleTurns,agentModuleNamespace:this.agentModuleNamespace,discoveryMode:this.functionDiscoveryEnabled,availableModules:I,agents:A,agentFunctions:h}),M=Ps(this.responderDescription,r);this.actorProgram?(this.actorProgram.setSignature(p),this.actorProgram.setDescription(R)):this.actorProgram=new Se(p,{...this._genOptions,description:R}),this.responderProgram?(this.responderProgram.setSignature(d),this.responderProgram.setDescription(M)):this.responderProgram=new Se(d,{...this._genOptions,description:M})}_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()),l=e.filter(u=>!a.has(u.name));l.length!==0&&i._extendForGlobalSharedFields(l,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()}getState(){if(this.stateError)throw new Error(this.stateError);return this.state?rr(this.state):void 0}setState(e){if(e&&e.version!==1)throw new Error(`Unsupported AxAgentState version "${String(e.version)}"`);if(e){let t=this.runtime.createSession();try{if(typeof t.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required to restore AxAgent state")}finally{try{t.close()}catch{}}}this.state=e?rr(e):void 0,this.stateError=void 0}async optimize(e,t){let n=Wm(e);if(n.train.length===0)throw new Error("AxAgent.optimize(): at least one training task is required.");let r=t?.studentAI??this.ai;if(!r)throw new Error("AxAgent.optimize(): studentAI is required when the agent has no default ai.");let o=t?.judgeAI??this.judgeAI??t?.teacherAI??this.ai??r,i={...this.judgeOptions??{},...t?.judgeOptions??{}},a=Ym(this.namedPrograms(),t?.target??"actor"),l=t?.metric??this._createAgentOptimizeMetric(o,i),u=this._createOptimizationProgram(a),c=Math.max(1,Math.floor(t?.maxMetricCalls??Math.max(Bm,n.train.length*4))),d=await new jt({studentAI:r,teacherAI:t?.teacherAI??o,verbose:t?.verbose,debugOptimizer:t?.debugOptimizer,optimizerLogger:t?.optimizerLogger,onProgress:t?.onProgress,onEarlyStop:t?.onEarlyStop}).compile(u,n.train,l,{validationExamples:n.validation,maxMetricCalls:c,verbose:t?.verbose});return t?.apply!==!1&&d.optimizedProgram&&this.applyOptimization(d.optimizedProgram),d}_createOptimizationProgram(e){return{getId:()=>this.getId(),setId:t=>this.setId(t),getSignature:()=>Km,forward:async(t,n,r)=>this._forwardForEvaluation(t,n,r),streamingForward:async function*(t,n,r){yield{version:1,index:0,delta:await this.forward(t,n,r)}},getTraces:()=>this.getTraces(),namedProgramInstances:()=>this.namedProgramInstances().filter(t=>e.includes(t.id)),setDemos:(t,n)=>this.setDemos(t,n),applyOptimization:t=>this.applyOptimization(t),getUsage:()=>this.getUsage(),resetUsage:()=>this.resetUsage()}}_createAgentOptimizeMetric(e,t){let n=Jm(t.criteria),r=new Bt(Hm,{ai:e,...t,criteria:n});return async({example:o,prediction:i})=>{let a=o,l=i,u={taskInput:yt(a.input),criteria:a.criteria,expectedOutput:a.expectedOutput,expectedActions:a.expectedActions,forbiddenActions:a.forbiddenActions,metadata:a.metadata},c={completionType:l.completionType,clarification:yt(l.clarification),finalOutput:yt(l.output),actionLog:l.actionLog,functionCalls:yt(l.functionCalls),toolErrors:l.toolErrors,turnCount:l.turnCount,usage:yt(l.usage??[])},p=await r.evaluate(u,c);return Qm(p.score,a,l)}}async _forwardForEvaluation(e,t,n){let r=this.state?rr(this.state):void 0,o=this.stateError;this.state=void 0,this.stateError=void 0;let i=new AbortController;this._stopRequested&&i.abort("Stopped by user (pre-forward)");let a=Tt(i.signal,n?.abortSignal);this.activeAbortControllers.add(i);let l=this._ensureLlmQueryBudgetState();try{let u=this.ai??e,c=n?.debug??this.debug??u?.getOptions()?.debug??!1,p=[],{nonContextValues:d,actorResult:m,actorFieldValues:g,actionLog:f,turnCount:x}=await this._runActorLoop(u,t.input,n,a,p),A=p.filter(I=>!!I.error).map(I=>`${I.qualifiedName}: ${I.error??"unknown error"}`);if(m.type==="ask_clarification")return{completionType:"ask_clarification",clarification:tp(m.args[0]),actionLog:f,functionCalls:p,toolErrors:A,turnCount:x};let h={...this._genOptions,...this.responderForwardOptions,...n,debug:c,abortSignal:a,maxSteps:1};return{completionType:"final",output:{...await this.responderProgram.forward(u,{...d,contextData:m},h),...g},actionLog:f,functionCalls:p,toolErrors:A,turnCount:x}}finally{this.state=r?rr(r):void 0,this.stateError=o,l&&(this.llmQueryBudgetState=void 0),this.activeAbortControllers.delete(i),this._stopRequested=!1}}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),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(`
1384
+ `)}}}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,A)=>({...x,...A.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],l=this._getBypassedSharedFieldNames(),u={},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:u,recomputeTurnInputs:x=>{let A={},h={};for(let[I,C]of Object.entries(o))this.rlmConfig.contextFields.includes(I)?A[I]=C:l.has(I)||(h[I]=C);if(x){for(let I of this.rlmConfig.contextFields)if(!g.has(I)&&(!(I in A)||A[I]===void 0))throw new Error(`RLM contextField "${I}" is missing from input values`)}let b={};for(let[I,C]of this.contextPromptConfigByField){if(l.has(I)||!(I in A))continue;let R=Rg(A[I],C);R!==void 0&&(b[I]=R)}c=A,p=h,d=b;for(let I of Object.keys(u))delete u[I];for(let I of a)I in o&&(u[I]=o[I]),I in c&&(u[I]=c[I]);m=Og(c,{promptConfigByField:this.contextPromptConfigByField,inlinedFields:new Set(Object.keys(d))})||"(none)"},getNonContextValues:()=>p,getActorInlineContextValues:()=>d,getContextMetadata:()=>m}}_ensureLlmQueryBudgetState(){return this.llmQueryBudgetState?!1:(this.llmQueryBudgetState={used:0},!0)}_createRuntimeExecutionContext({ai:e,inputState:t,options:n,effectiveAbortSignal:r,debug:o,completionState:i,completionBindings:a,actionLogEntries:l,functionCallRecorder:u}){let c=this.rlmConfig,p=this.runtime,d=c.maxSubAgentCalls??Uc,m=c.maxRuntimeChars??Bc,g=Math.max(1,c.maxBatchedLlmQueryConcurrency??Dm),f=this.recursionForwardOptions?.maxDepth??zc,x=Math.max(0,f),A=Kc(c.contextPolicy),h=this.llmQueryBudgetState??{used:0},b=new Set,I=Math.floor(d*.8),{maxDepth:C,...R}=this.recursionForwardOptions??{},{description:M,mem:S,sessionId:O,...k}=n??{},_={...this.recursionForwardOptions??{},maxDepth:Math.max(0,x-1)},L=ae().input("task",ae.string("Task for recursive analysis")).output("answer",ae.string("Answer from recursive analysis")).build(),E=ae().input("task",ae.string("Task for recursive analysis")).input("context",ae.json("Optional context for the recursive task")).output("answer",ae.string("Answer from recursive analysis")).build(),$=(c.mode??"simple")==="advanced"&&x>0,D=this.recursionForwardOptions?.promptLevel??this.actorPromptLevel,z=()=>new s({agentModuleNamespace:this.agentModuleNamespace,signature:L},{debug:o,...c,agents:{local:this.agents},functions:{local:this.agentFunctions,discovery:this.functionDiscoveryEnabled},contextFields:[],actorFields:void 0,recursionOptions:_,actorOptions:{...this.actorForwardOptions,promptLevel:D},responderOptions:this.responderForwardOptions}),U=F=>(F.llmQueryBudgetState=h,F),te=()=>{for(let F of[...b])F.stop()},B=()=>new Se(E,_),ge=async(F,j)=>{if(!Array.isArray(F)&&typeof F=="object"&&F!==null&&"query"in F)return ge(F.query,F.context??j);if(r?.aborted)throw new Ee("rlm-llm-query",r.reason?String(r.reason):"Aborted");if(!e)throw new Error(qm);let X=F,Z=me=>{if(me==null)return"";if(typeof me=="string")return at(me,m);try{return at(JSON.stringify(me),m)}catch{return at(String(me),m)}},xe=async(me,je,Me=r)=>{if(Me?.aborted)throw new Ee("rlm-llm-query",Me.reason?String(Me.reason):"Aborted");let Ze=je===void 0?void 0:typeof je=="string"?at(je,m):je;if(h.used>=d)return`[ERROR] Sub-query budget exhausted (${d}/${d}). Complete the task using data already gathered or handle remaining work directly in JS.`;h.used++;let Y=3,J,ce=Ie=>`[ERROR] ${Ie instanceof Error?Ie.message:String(Ie)}. Retry with a simpler query, handle in JS, or proceed with data already gathered.`;for(let Ie=0;Ie<Y;Ie++)try{let Re=$?await(()=>{let st=U(z());return b.add(st),st.runtimeBootstrapContext=Ze,st.forward(e,{task:me},{...k,...R,abortSignal:Me,debug:o}).finally(()=>{b.delete(st)})})():await B().forward(e,{task:me,...Ze!==void 0?{context:Ze}:{}},{...k,...R,abortSignal:Me,debug:o});return Z(Re.answer)}catch(Re){if(Re instanceof Ee||Re instanceof it)throw Re;if(J=Re,!xg(Re)||Ie>=Y-1)return ce(Re);let st=Math.min(6e4,1e3*Math.pow(2,Ie));await new Promise((sr,$s)=>{let zt=!1,qt,Xr=()=>{Me&&qt&&Me.removeEventListener("abort",qt)},Ae=setTimeout(()=>{zt||(zt=!0,Xr(),sr())},st);if(Me){if(qt=()=>{zt||(zt=!0,clearTimeout(Ae),Xr(),$s(new Ee("rlm-llm-query-retry-backoff",Me.reason?String(Me.reason):"Aborted during retry backoff")))},Me.aborted){qt();return}Me.addEventListener("abort",qt,{once:!0})}})}return ce(J)};if(Array.isArray(F)){let me=new AbortController,je=Tt(r,me.signal)??me.signal,Me,Ze=()=>{te()};je.addEventListener("abort",Ze,{once:!0});try{return await Eg(F,g,async Y=>{try{return await xe(Y.query,Y.context,je)}catch(J){if(J instanceof Ee||J instanceof it)throw(J instanceof it||!Me)&&(Me=J),me.signal.aborted||me.abort(J instanceof it?"Child clarification":J.message),Me;return`[ERROR] ${J instanceof Error?J.message:String(J)}`}},je)}finally{je.removeEventListener("abort",Ze)}}let $e=await xe(X,j);return h.used===I?`${$e}
1385
+ [WARNING] ${h.used}/${d} sub-queries used (${d-h.used} remaining). Consolidate remaining work.`:$e},Ce=this.buildRuntimeGlobals(r,t.sharedFieldValues,e,a.protocol,u),ke=[...new Set(this.agentFunctions.map(F=>F.namespace??"utils"))],N={...t.currentInputs},q=new Set(["inputs","llmQuery",Fs,this.agentModuleNamespace,"final","ask_clarification",...ke,...A.stateInspection.enabled?["inspect_runtime"]:[],...Object.keys(Ce)]),W=[...new Set([...Object.keys(N),...t.signatureInputFieldNames])].filter(F=>!q.has(F)),he={};for(let F of W)he[F]=N[F];let ee=()=>{for(let F of Object.keys(N))delete N[F];for(let[F,j]of Object.entries(t.currentInputs))N[F]=j;for(let F of W)he[F]=t.currentInputs[F]},ie=[...q],fe=[...q,...W],Pe=new Set(fe),Ne=this.runtimeBootstrapContext;this.runtimeBootstrapContext=void 0;let oe=rg(Ne,Pe),ve=new Set(Object.keys(oe)),H=l??[],re,de,ye=F=>typeof F.inspectGlobals=="function"?F:void 0,we=async()=>{try{let F=await re.execute($c(),{signal:r,reservedNames:fe});if(typeof F!="string")return[];let j=JSON.parse(F);return Array.isArray(j)?j.filter(X=>typeof X=="string"&&!ve.has(X)):[]}catch{return[]}},Oe=async()=>(de||(de=await we()),de),lt=async()=>{try{let F=ye(re);if(F?.inspectGlobals)return await F.inspectGlobals({signal:r,reservedNames:fe});let j=await Oe(),X=Lc(fe,j),Z=await re.execute(X,{signal:r,reservedNames:fe});return typeof Z=="string"?Z:String(Z)}catch(F){return`[inspect_runtime error: ${F instanceof Error?F.message:String(F)}]`}},Ue=(F,j)=>{let X=yg(F);if(!X)return bg(F,j);let Z=vs(H);return wa(X,Z,j)},rt=A.stateInspection.enabled?async()=>Ue(await lt()):void 0,Te=()=>(de=void 0,p.createSession({...he,inputs:N,...oe,llmQuery:ge,final:a.finalFunction,ask_clarification:a.askClarificationFunction,...rt?{inspect_runtime:rt}:{},...Ce}));re=Te();let Be=()=>({maxEntries:A.stateSummary.maxEntries&&A.stateSummary.maxEntries>0?A.stateSummary.maxEntries:8,maxChars:A.stateSummary.maxChars&&A.stateSummary.maxChars>0?A.stateSummary.maxChars:void 0}),se=()=>({maxEntries:A.stateSummary.maxEntries&&A.stateSummary.maxEntries>0?A.stateSummary.maxEntries:6,maxChars:A.stateSummary.maxChars&&A.stateSummary.maxChars>0?A.stateSummary.maxChars:Math.min(m,1200)}),Le=Object.keys(oe).length>0?ug(oe,{...se(),budgetRemaining:Math.max(0,d-h.used),budgetTotal:d}):void 0,He=async()=>{if(!i.payload)for(let F=0;F<3&&!i.payload;F++)await new Promise(j=>{setTimeout(j,0)})},Ve=async()=>{if(!A.stateSummary.enabled)return;let F=await lt();return Ue(F,Be())||"(no user variables)"},ot=F=>{if(typeof F.patchGlobals!="function")throw new Error("AxCodeSession.patchGlobals() is required when restoring AxAgent state or using inputUpdateCallback");return F},y=F=>{if(typeof F.snapshotGlobals!="function")throw new Error("AxCodeSession.snapshotGlobals() is required to export AxAgent state");return F},T=F=>{let j=new Set(fe),X={};for(let[xe,$e]of Object.entries(F.runtimeBindings??{}))j.has(xe)||(X[xe]=$e);let Z=(F.runtimeEntries??[]).filter(xe=>!j.has(xe.name));return{runtimeBindings:X,runtimeEntries:Z,actionLogEntries:dg(F.actionLogEntries),checkpointState:F.checkpointState,provenance:{...F.provenance??{}}}};return{effectiveContextConfig:A,bootstrapContextSummary:Le,applyBootstrapRuntimeContext:async()=>{if(Object.keys(oe).length===0||!A.stateSummary.enabled)return;let F=await lt();return Ue(F,Be())||"(no user variables)"},captureRuntimeStateSummary:Ve,exportRuntimeState:async()=>{let j=await y(re).snapshotGlobals({signal:r,reservedNames:fe}),X=vs(H);return{version:1,runtimeBindings:j.bindings,runtimeEntries:j.entries,actionLogEntries:pg(H),provenance:mg(X)}},restoreRuntimeState:async F=>{let j=T(F);return await ot(re).patchGlobals(j.runtimeBindings,{signal:r}),j},syncRuntimeInputsToSession:async()=>{ee();let F=async j=>{await ot(j).patchGlobals({inputs:{...N},...he},{signal:r})};try{await F(re)}catch(j){if(r?.aborted)throw new Ee("rlm-session",r.reason??"Aborted");if(j instanceof Error&&(j.name==="AbortError"||j.message.startsWith("Aborted")))throw j;if(Yc(j)){re=Te(),await F(re);return}throw new Error(`Failed to sync runtime inputs: ${Ns(j,m)}`)}},executeActorCode:async F=>{let j={result:void 0,output:_s(void 0,m),isError:!1};try{let X=await re.execute(F,{signal:r,reservedNames:ie});return i.payload||Jc(F)&&Qc(X)&&(await He(),i.payload)?j:{result:X,output:_s(X,m),isError:!1}}catch(X){if(X instanceof or||i.payload)return j;if(X instanceof it||X instanceof Ee)throw X;if(r?.aborted)throw new Ee("rlm-session",r.reason??"Aborted");if(X instanceof Error&&(X.name==="AbortError"||X.message.startsWith("Aborted")))throw X;if(wg(X))return{result:void 0,output:at(`${Ls}
1386
+ ${Ns(X,m)}`,m),isError:!0};if(Yc(X))try{re=Te(),i.payload=void 0;let Z=await re.execute(F,{signal:r,reservedNames:ie});return{result:Z,output:at(`${Ls}
1387
+ ${_s(Z,m)}`,m),isError:!1}}catch(Z){if(Z instanceof it||Z instanceof Ee)throw Z;return{result:void 0,output:at(`${Ls}
1388
+ ${Ns(Z,m)}`,m),isError:!0}}return{result:void 0,output:at(Ns(X,m),m),isError:!0}}},executeTestCode:async F=>{try{let j=await re.execute(F,{signal:r,reservedNames:ie});if(Jc(F)&&Qc(j)&&await He(),i.payload)return i.payload;let X=_s(j,m);if(Tg(X))throw new Error(X);return X}catch(j){if((j instanceof or||i.payload)&&i.payload)return i.payload;throw j}},close:()=>{re.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},l=Hc((p,d)=>{a.payload=Wc(p,d)}),u=this._ensureLlmQueryBudgetState(),c=this._createRuntimeExecutionContext({ai:r,inputState:i,options:void 0,effectiveAbortSignal:n?.abortSignal,debug:o,completionState:a,completionBindings:l,actionLogEntries:[]});try{return await c.executeTestCode(e)}finally{u&&(this.llmQueryBudgetState=void 0),c.close()}}setSignature(e){let t=new _e(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,o){let i=this.rlmConfig,a=n?.debug??this.debug??e?.getOptions()?.debug??!1,l=i.maxTurns??jc,u=this._createRuntimeInputState(t);u.recomputeTurnInputs(!1);let c={payload:void 0},p=Hc((L,E)=>{c.payload=Wc(L,E)}),d=[],m,g=this._createRuntimeExecutionContext({ai:e,inputState:u,options:n,effectiveAbortSignal:r,debug:a,completionState:c,completionBindings:p,actionLogEntries:d,functionCallRecorder:o?L=>{o.push(L)}:void 0}),f=g.effectiveContextConfig.stateSummary.enabled?void 0:g.bootstrapContextSummary,x=async()=>{if(!this.inputUpdateCallback)return;let L=await this.inputUpdateCallback({...u.currentInputs});if(L!==void 0){if(!L||typeof L!="object"||Array.isArray(L))throw new Error("inputUpdateCallback must return an object patch or undefined");for(let[E,P]of Object.entries(L))u.signatureInputFieldNames.has(E)&&(u.currentInputs[E]=P)}},A={...this._genOptions,...this.actorForwardOptions,...n,debug:a,abortSignal:r},h={},b=g.effectiveContextConfig.stateInspection.enabled?g.effectiveContextConfig.stateInspection.contextThreshold:void 0,I=Xm(n,a,r),C=this.functionDiscoveryEnabled&&g.effectiveContextConfig.pruneUsedDocs,R,M,S=()=>Os(d,{pruneUsedDocs:C}),O=()=>_c(S(),{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,restoreNotice:M,delegatedContextSummary:f,stateSummary:m,checkpointSummary:R?.summary,checkpointTurns:R?.turns})||"(no actions yet)",k=async()=>{if(!g.effectiveContextConfig.checkpoints.enabled){R=void 0;return}let L=Es(d,{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,pruneUsedDocs:C}),E=g.effectiveContextConfig.checkpoints.triggerChars;if(!E||L.historyChars<=E){R=void 0;return}let P=L.checkpointEntries;if(P.length===0){R=void 0;return}let $=JSON.stringify(P.map(D=>({turn:D.turn,code:D.code,output:D.output,actorFieldsOutput:D.actorFieldsOutput,tags:D.tags,tombstone:D.tombstone})));R?.fingerprint!==$&&(R={fingerprint:$,turns:P.map(D=>D.turn),summary:await Fc(e,g.effectiveContextConfig.summarizerOptions,I,P)})};try{if(this.state){let E=await g.restoreRuntimeState(this.state),P=g.effectiveContextConfig.stateSummary.enabled;d.push(...E.actionLogEntries),R=E.checkpointState?{fingerprint:E.checkpointState.fingerprint,turns:[...E.checkpointState.turns],summary:E.checkpointState.summary}:void 0;let $=hg(vs(d),fg(E.provenance));m=P?wa(E.runtimeEntries,$,{maxEntries:g.effectiveContextConfig.stateSummary.maxEntries&&g.effectiveContextConfig.stateSummary.maxEntries>0?g.effectiveContextConfig.stateSummary.maxEntries:8,maxChars:g.effectiveContextConfig.stateSummary.maxChars&&g.effectiveContextConfig.stateSummary.maxChars>0?g.effectiveContextConfig.stateSummary.maxChars:1200})||"(no user variables)":void 0,M=gg(E.runtimeEntries,{includeLiveRuntimeState:P})}let L=await g.applyBootstrapRuntimeContext();L!==void 0&&(m=L);for(let E=0;E<l;E++){await x(),u.recomputeTurnInputs(!0),await k();let P=O(),$=Es(d,{actionReplay:g.effectiveContextConfig.actionReplay,recentFullActions:g.effectiveContextConfig.recentFullActions,pruneUsedDocs:C,checkpointTurns:R?.turns});b&&$.historyChars>b&&(P+="\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 D=await this.actorProgram.forward(e,{...u.getNonContextValues(),...u.getActorInlineContextValues(),contextMetadata:u.getContextMetadata(),actionLog:P},A);E===0&&(A.debugHideSystemPrompt=!0,M=void 0);let z=D.javascriptCode,U=z?.trim();if(!z||!U)break;z=Vm(U),D.javascriptCode=z;for(let N of this.actorFieldNames)N in D&&(h[N]=D[N]);let te="";if(this.actorFieldNames.length>0){let N=this.actorFieldNames.filter(q=>q in D).map(q=>`${q}: ${D[q]}`).join(`
1389
+ `);N&&(te=`
1261
1390
  Actor fields:
1262
- ${ge}`)}if(c.payload=void 0,this.enforceIncrementalConsoleTurns){let ge=bg(_);if(ge){let Ae=d.length+1;d.push({turn:Ae,code:_,output:ge,actorFieldsOutput:H,tags:["error"]}),i.actorTurnCallback&&await i.actorTurnCallback({turn:Ae,actorResult:M,code:_,result:void 0,output:ge,isError:!0,thought:typeof M.thought=="string"?M.thought:void 0}),await ma(d,d.length-1,g.effectiveContextConfig,e,y),await E();continue}}this.inputUpdateCallback&&await g.syncRuntimeInputsToSession();let{result:D,output:Y,isError:G}=await g.executeActorCode(_),me=d.length+1;if(d.push({turn:me,code:_,output:Y,actorFieldsOutput:H,tags:G?["error"]:[]}),i.actorTurnCallback&&await i.actorTurnCallback({turn:me,actorResult:M,code:_,result:D,output:Y,isError:G,thought:typeof M.thought=="string"?M.thought:void 0}),await ma(d,d.length-1,g.effectiveContextConfig,e,y),G||(m=await g.captureRuntimeStateSummary()),await E(),c.payload)break}await E();try{let O=await g.exportRuntimeState();O.checkpointState=C?{fingerprint:C.fingerprint,turns:[...C.turns],summary:C.summary}:void 0,this.state=O,this.stateError=void 0}catch(O){this.state=void 0,this.stateError=O instanceof Error?O.message:`Failed to export AxAgent state: ${String(O)}`}}finally{try{g.close()}catch{}}let k=c.payload??{type:"final",args:[kc(d,{stateSummary:m,checkpointSummary:C?.summary,checkpointTurns:C?.turns,pruneUsedDocs:I})]};return{nonContextValues:u.getNonContextValues(),contextMetadata:u.getContextMetadata(),actionLog:v(),actorResult:k,actorFieldValues:b,turnCount:d.length}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=Tt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let i=this.ai??e,a=n?.debug??this.debug??i?.getOptions()?.debug??!1,{nonContextValues:l,actorResult:u,actorFieldValues:c}=await this._runActorLoop(i,t,n,o);if(u.type==="ask_clarification")throw new tr(u.args[0],{state:this.state,stateError:this.stateError});let p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(i,{...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=Tt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);try{let i=this.ai??e,a=n?.debug??this.debug??i?.getOptions()?.debug??!1,{nonContextValues:l,actorResult:u,actorFieldValues:c}=await this._runActorLoop(i,t,n,o);if(u.type==="ask_clarification")throw new tr(u.args[0],{state:this.state,stateError:this.stateError});let p={...this._genOptions,...this.responderForwardOptions,...n,debug:a,abortSignal:o,maxSteps:1};for await(let d of this.responderProgram.streamingForward(i,{...l,contextData:u},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,o,i){return async(...a)=>{let l;if(a.length===1&&typeof a[0]=="object"&&a[0]!==null&&!Array.isArray(a[0]))l=a[0];else{let u=e.parameters?.properties?Object.keys(e.parameters.properties):[];l={},u.forEach((c,p)=>{p<a.length&&(l[c]=a[p])})}try{let u=await e.func(l,{abortSignal:t,ai:n,protocol:r});return i?.({qualifiedName:o??e.name,name:e.name,arguments:dt(l),result:dt(u)}),u}catch(u){throw i?.({qualifiedName:o??e.name,name:e.name,arguments:dt(l),error:u instanceof Error?u.message:String(u)}),u}}}static wrapFunctionWithSharedFields(e,t,n,r,o,i,a){return typeof n!="function"&&(!n||Object.keys(n).length===0)?s.wrapFunction(e,t,r,o,i,a):async(...l)=>{let u;if(l.length===1&&typeof l[0]=="object"&&l[0]!==null&&!Array.isArray(l[0]))u=l[0];else{let d=e.parameters?.properties?Object.keys(e.parameters.properties):[];u={},d.forEach((m,g)=>{g<l.length&&(u[m]=l[g])})}let c=typeof n=="function"?n():n,p=c?{...c,...u}:u;try{let d=await e.func(p,{abortSignal:t,ai:r,protocol:o});return a?.({qualifiedName:i??e.name,name:e.name,arguments:dt(p),result:dt(d)}),d}catch(d){throw a?.({qualifiedName:i??e.name,name:e.name,arguments:dt(p),error:d instanceof Error?d.message:String(d)}),d}}}buildRuntimeGlobals(e,t,n,r,o){let i={},a=new Map,l=new Map,u=new Map;for(let[p,d]of this.agentFunctionModuleMetadata)u.set(p,d);let c=(p,d)=>{a.set(d,p),l.has(p.module)||l.set(p.module,[]),l.get(p.module)?.push(d)};for(let p of this.agentFunctions){let d=p.namespace??"utils";(!i[d]||typeof i[d]!="object")&&(i[d]={});let m=`${d}.${p.name}`;i[d][p.name]=s.wrapFunction(p,e,n,r,m,o),c({module:d,name:p.name,description:p.description,parameters:p.parameters,returns:p.returns,examples:p.examples},m)}if(this.agents&&this.agents.length>0){let p={};for(let d of this.agents){let m=d.getFunction(),g=new Set(d.getExcludedSharedFields?.()??[]),f=()=>{let b={};if(t)for(let[h,y]of Object.entries(t))g.has(h)||(b[h]=y);return b},x=`${this.agentModuleNamespace}.${m.name}`;p[m.name]=s.wrapFunctionWithSharedFields(m,e,f,n,r,x,o),c({module:this.agentModuleNamespace,name:m.name,description:m.description,parameters:m.parameters},x)}i[this.agentModuleNamespace]=p}return this.functionDiscoveryEnabled&&(i[fa]=async p=>{let d=qc(p,"modules");return Og(d,l,u)},i[ha]=async p=>{let d=qc(p,"functions");return Eg(d,a)}),i}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,judgeOptions:r,inputUpdateCallback:o,...i}=this.options;return i}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?Tg(e,this._parentSharedFields):e}};function $c(s,e){if(e.length===0)throw new Error(`${s}() requires at least one argument`);if(s==="ask_clarification"){if(e.length!==1)throw new Error("ask_clarification() requires exactly one argument");return{type:s,args:[Kc(e[0])]}}return{type:s,args:e}}function Hc(s){return!!s&&typeof s=="object"&&!Array.isArray(s)&&Object.getPrototypeOf(s)===Object.prototype}function Hr(s){return typeof s=="string"&&s.trim().length>0}function Qm(s){if(Hr(s))return s;if(!Hc(s))throw new Error("ask_clarification() choice entries must be non-empty strings or objects with a non-empty label");if(!Hr(s.label))throw new Error("ask_clarification() choice objects require a non-empty label");if(s.value!==void 0&&!Hr(s.value))throw new Error("ask_clarification() choice object values must be non-empty strings");return{label:s.label,...s.value!==void 0?{value:s.value}:{}}}function Kc(s){if(Hr(s))return s;if(!Hc(s))throw new Error("ask_clarification() requires a non-empty string or an object payload");if(!Hr(s.question))throw new Error("ask_clarification() object payload requires a non-empty question");let e=new Set(["text","number","date","single_choice","multiple_choice"]),t;if(s.type===void 0)t=Array.isArray(s.choices)&&s.choices.length>0?"single_choice":void 0;else{if(typeof s.type!="string"||!e.has(s.type))throw new Error("ask_clarification() object payload type must be one of: text, number, date, single_choice, multiple_choice");t=s.type}let n=t==="single_choice"||t==="multiple_choice",r=s.choices;if(r!==void 0){if(!Array.isArray(r)||r.length===0)throw new Error("ask_clarification() choices must be a non-empty array when provided")}else if(n)throw new Error("ask_clarification() choice payloads require a non-empty choices array");return{...s,question:s.question,...t?{type:t}:{},...r?{choices:r.map(Qm)}:{}}}function Wc(s){let e=Kc(s);return typeof e=="string"?{question:e,type:"text"}:{...e,type:e.type??(e.choices&&e.choices.length>0?"single_choice":"text")}}function Ym(s){return typeof structuredClone=="function"?structuredClone(s):JSON.parse(JSON.stringify(s))}function er(s){return Ym(s)}function Xm(s){return s.map(e=>({turn:e.turn,code:e.code,output:e.output,actorFieldsOutput:e.actorFieldsOutput,tags:[...e.tags],...e.summary?{summary:e.summary}:{},...e.producedVars?{producedVars:[...e.producedVars]}:{},...e.referencedVars?{referencedVars:[...e.referencedVars]}:{},...e.stateDelta?{stateDelta:e.stateDelta}:{},...e.stepKind?{stepKind:e.stepKind}:{},...e.replayMode?{replayMode:e.replayMode}:{},...e.rank!==void 0?{rank:e.rank}:{},...e.tombstone?{tombstone:e.tombstone}:{}}))}function Zm(s){return(s??[]).map(e=>({turn:e.turn,code:e.code,output:e.output,actorFieldsOutput:e.actorFieldsOutput,tags:[...e.tags],...e.summary?{summary:e.summary}:{},...e.producedVars?{producedVars:[...e.producedVars]}:{},...e.referencedVars?{referencedVars:[...e.referencedVars]}:{},...e.stateDelta?{stateDelta:e.stateDelta}:{},...e.stepKind?{stepKind:e.stepKind}:{},...e.replayMode?{replayMode:e.replayMode}:{},...e.rank!==void 0?{rank:e.rank}:{},...e.tombstone?{tombstone:e.tombstone}:{}}))}function eg(s){return Object.fromEntries([...s.entries()].map(([e,t])=>[e,{...t}]))}function tg(s,e){let t=s.filter(r=>r.restorable===!1).length,n=["Runtime Restore:","- Runtime state was restored from a previous call.","- Continue from restored values unless recomputation is actually needed."];return e?.includeLiveRuntimeState!==!1?n.splice(2,0,"- Live Runtime State below reflects the restored bindings."):n.splice(2,0,"- Live Runtime State rendering is disabled for this run, but the restored bindings are available in the runtime session."),t>0&&n.push(`- ${t} prior value${t===1?" was":"s were"} snapshot-only and could not be restored.`),n.join(`
1263
- `)}function ng(s){return new Map(Object.entries(s??{}).map(([e,t])=>[e,{...t}]))}function rg(s,e){let t=new Map;for(let[n,r]of e.entries())t.set(n,{...r});for(let[n,r]of s.entries())t.set(n,{...r});return t}function Vc(s,e){let t=typeof s=="string"?Fe.create(s):s,{ai:n,judgeAI:r,agentIdentity:o,...i}=e;return new Kr({ai:n,judgeAI:r,agentIdentity:o,signature:t},i)}function og(s){return s instanceof Xe&&s.status>=500&&s.status<600?!0:s instanceof qe||s instanceof nt}function mt(s,e){return s.length<=e?s:`${s.slice(0,e)}
1264
- ...[truncated ${s.length-e} chars]`}function sg(s,e){return e<=0?"":s.length<=e?s:e<=3?s.slice(0,e):`${s.slice(0,e-3)}...`}function ig(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")&&(e.restorable===void 0||typeof e.restorable=="boolean")}function ag(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(ig):void 0}catch{return}}function Jc(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(`
1265
- `);let o=[],i=0;for(let a of r){let l=o.length>0?1:0,u=n-i-l;if(u<=0)break;if(a.length<=u){o.push(a),i+=l+a.length;continue}o.push(sg(a,u)),i=n;break}return o.join(`
1266
- `)}function lg(s,e){let t=s.split(`
1267
- `).map(n=>n.trim()).filter(Boolean);return Jc(t,e)}function Dc(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 ug(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 cg(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 Gc(s,e,t){let n=[...s].sort((r,o)=>{let i=Dc(r,e.get(r.name));return Dc(o,e.get(o.name))-i||r.name.localeCompare(o.name)}).map(r=>{let o=r.preview?` = ${r.preview}`:"",i=cg(e.get(r.name)),a="restorable"in r&&r.restorable===!1?" [snapshot only]":"";return`${r.name}: ${ug(r)}${o}${i}${a}`});return n.length===0?"(no user variables)":Jc(n,t)}function Os(s,e){if(s===void 0)return"(no output)";if(typeof s=="string")return mt(s||"(no output)",e);try{return mt(JSON.stringify(s,null,2),e)}catch{return mt(String(s),e)}}function Es(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,l)=>{if(l>4)return"[cause chain truncated]";let u=a,c=u?.name??"Error",p=u?.message??String(a),d=[`${c}: ${p}`];if(u?.data!==void 0)try{d.push(`Data: ${JSON.stringify(u.data,null,2)}`)}catch{d.push(`Data: ${String(u.data)}`)}return u?.cause!==void 0&&d.push(`Caused by: ${i(u.cause,l+1)}`),d.join(`
1268
- `)};o.push(`Caused by: ${i(t.cause,1)}`)}return mt(o.join(`
1269
- `),e)}function Uc(s){let e=Qc(s);return/\bfinal\s*\(/.test(e)||/\bask_clarification\s*\(/.test(e)}function jc(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 Bc(s){return s instanceof Error&&s.message==="Session is closed"}function pg(s){return s instanceof Error&&s.message==="Execution timed out"}function dg(s){return s.startsWith("[ERROR]")||s.startsWith(Ms)?!0:/^(AggregateError|Error|EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError): /.test(s)}function mg(s,e){if(e.kind==="threshold")return fg(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 gg(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 fg(s){if(typeof s=="string")return s.length;try{return JSON.stringify(s).length}catch{return String(s).length}}function hg(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 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`);i.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`);i.set(l,{kind:"threshold",promptMaxChars:p})}}return{contextFieldNames:o,promptConfigByField:i}}function xg(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),l=a===void 0?"runtime-only":gg(r,a,e?.inlinedFields?.has(n)===!0);t.push(`- ${n}: type=${o}, size=${i}, prompt=${l}`)}return t.join(`
1270
- `)}async function Ag(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 l=s[a];if(l===void 0)return;n[a]=await t(l,a)}});return await Promise.all(i),n}function yg(s){return s.includes("console.log")}function bg(s){let e=Qc(s),t=/\bfinal\s*\(/.test(e),n=/\bask_clarification\s*\(/.test(e),r=Number(t)+Number(n),o=Ig(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 Qc(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===`
1391
+ ${N}`)}if(c.payload=void 0,this.enforceIncrementalConsoleTurns){let N=Pg(z);if(N){let q=d.length+1;d.push({turn:q,code:z,output:N,actorFieldsOutput:te,tags:["error"]}),i.actorTurnCallback&&await i.actorTurnCallback({turn:q,actorResult:D,code:z,result:void 0,output:N,isError:!0,thought:typeof D.thought=="string"?D.thought:void 0}),await Aa(d,d.length-1,g.effectiveContextConfig,e,I),await k();continue}}this.inputUpdateCallback&&await g.syncRuntimeInputsToSession();let B,ge,Ce;try{let N=await g.executeActorCode(z);B=N.result,ge=N.output,Ce=N.isError}catch(N){throw(N instanceof it||N instanceof Ee)&&i.actorTurnCallback&&await i.actorTurnCallback({turn:d.length+1,actorResult:D,code:z,result:void 0,output:cg(N,i.maxRuntimeChars??Bc),isError:N instanceof Ee,thought:typeof D.thought=="string"?D.thought:void 0}),N}let ke=d.length+1;if(d.push({turn:ke,code:z,output:ge,actorFieldsOutput:te,tags:Ce?["error"]:[]}),i.actorTurnCallback&&await i.actorTurnCallback({turn:ke,actorResult:D,code:z,result:B,output:ge,isError:Ce,thought:typeof D.thought=="string"?D.thought:void 0}),await Aa(d,d.length-1,g.effectiveContextConfig,e,I),Ce||(m=await g.captureRuntimeStateSummary()),await k(),c.payload)break}await k();try{let E=await g.exportRuntimeState();E.checkpointState=R?{fingerprint:R.fingerprint,turns:[...R.turns],summary:R.summary}:void 0,this.state=E,this.stateError=void 0}catch(E){this.state=void 0,this.stateError=E instanceof Error?E.message:`Failed to export AxAgent state: ${String(E)}`}}finally{try{g.close()}catch{}}let _=c.payload??{type:"final",args:[Nc(d,{stateSummary:m,checkpointSummary:R?.summary,checkpointTurns:R?.turns,pruneUsedDocs:C})]};return{nonContextValues:u.getNonContextValues(),contextMetadata:u.getContextMetadata(),actionLog:O(),actorResult:_,actorFieldValues:h,turnCount:d.length}}async forward(e,t,n){let r=new AbortController;this._stopRequested&&r.abort("Stopped by user (pre-forward)");let o=Tt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);let i=this._ensureLlmQueryBudgetState();try{let a=this.ai??e,l=n?.debug??this.debug??a?.getOptions()?.debug??!1,{nonContextValues:u,actorResult:c,actorFieldValues:p}=await this._runActorLoop(a,t,n,o);if(c.type==="ask_clarification")throw new it(c.args[0],{state:this.state,stateError:this.stateError});let d={...this._genOptions,...this.responderForwardOptions,...n,debug:l,abortSignal:o,maxSteps:1};return{...await this.responderProgram.forward(a,{...u,contextData:c},d),...p}}finally{i&&(this.llmQueryBudgetState=void 0),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=Tt(r.signal,n?.abortSignal);this.activeAbortControllers.add(r);let i=this._ensureLlmQueryBudgetState();try{let a=this.ai??e,l=n?.debug??this.debug??a?.getOptions()?.debug??!1,{nonContextValues:u,actorResult:c,actorFieldValues:p}=await this._runActorLoop(a,t,n,o);if(c.type==="ask_clarification")throw new it(c.args[0],{state:this.state,stateError:this.stateError});let d={...this._genOptions,...this.responderForwardOptions,...n,debug:l,abortSignal:o,maxSteps:1};for await(let m of this.responderProgram.streamingForward(a,{...u,contextData:c},d))yield m;Object.keys(p).length>0&&(yield{version:1,index:0,delta:p})}finally{i&&(this.llmQueryBudgetState=void 0),this.activeAbortControllers.delete(r),this._stopRequested=!1}}static wrapFunction(e,t,n,r,o,i){return async(...a)=>{let l;if(a.length===1&&typeof a[0]=="object"&&a[0]!==null&&!Array.isArray(a[0]))l=a[0];else{let u=e.parameters?.properties?Object.keys(e.parameters.properties):[];l={},u.forEach((c,p)=>{p<a.length&&(l[c]=a[p])})}try{let u=await e.func(l,{abortSignal:t,ai:n,protocol:r});return i?.({qualifiedName:o??e.name,name:e.name,arguments:yt(l),result:yt(u)}),u}catch(u){throw i?.({qualifiedName:o??e.name,name:e.name,arguments:yt(l),error:u instanceof Error?u.message:String(u)}),u}}}static wrapFunctionWithSharedFields(e,t,n,r,o,i,a){return typeof n!="function"&&(!n||Object.keys(n).length===0)?s.wrapFunction(e,t,r,o,i,a):async(...l)=>{let u;if(l.length===1&&typeof l[0]=="object"&&l[0]!==null&&!Array.isArray(l[0]))u=l[0];else{let d=e.parameters?.properties?Object.keys(e.parameters.properties):[];u={},d.forEach((m,g)=>{g<l.length&&(u[m]=l[g])})}let c=typeof n=="function"?n():n,p=c?{...c,...u}:u;try{let d=await e.func(p,{abortSignal:t,ai:r,protocol:o});return a?.({qualifiedName:i??e.name,name:e.name,arguments:yt(p),result:yt(d)}),d}catch(d){throw a?.({qualifiedName:i??e.name,name:e.name,arguments:yt(p),error:d instanceof Error?d.message:String(d)}),d}}}buildRuntimeGlobals(e,t,n,r,o){let i={},a=new Map,l=new Map,u=new Map;for(let[p,d]of this.agentFunctionModuleMetadata)u.set(p,d);let c=(p,d)=>{a.set(d,p),l.has(p.module)||l.set(p.module,[]),l.get(p.module)?.push(d)};for(let p of this.agentFunctions){let d=p.namespace??"utils";(!i[d]||typeof i[d]!="object")&&(i[d]={});let m=`${d}.${p.name}`;i[d][p.name]=s.wrapFunction(p,e,n,r,m,o),c({module:d,name:p.name,description:p.description,parameters:p.parameters,returns:p.returns,examples:p.examples},m)}if(this.agents&&this.agents.length>0){let p={};for(let d of this.agents){let m=d.getFunction(),g=new Set(d.getExcludedSharedFields?.()??[]),f=()=>{let A={};if(t)for(let[h,b]of Object.entries(t))g.has(h)||(A[h]=b);return A},x=`${this.agentModuleNamespace}.${m.name}`;p[m.name]=s.wrapFunctionWithSharedFields(m,e,f,n,r,x,o),c({module:this.agentModuleNamespace,name:m.name,description:m.description,parameters:m.parameters},x)}i[this.agentModuleNamespace]=p}return this.functionDiscoveryEnabled&&(i[ba]=async p=>{let d=Zc(p,"modules");return Bg(d,l,u)},i[Ia]=async p=>{let d=Zc(p,"functions");return jg(d,a)}),i}get _genOptions(){if(!this.options)return{};let{agents:e,fields:t,functions:n,judgeOptions:r,inputUpdateCallback:o,...i}=this.options;return i}_buildFuncParameters(){let e=this.program.getSignature().toInputJSONSchema();return this._parentSharedFields.size>0?Ng(e,this._parentSharedFields):e}};function Wc(s,e){if(e.length===0)throw new Error(`${s}() requires at least one argument`);if(s==="ask_clarification"){if(e.length!==1)throw new Error("ask_clarification() requires exactly one argument");return{type:s,args:[ep(e[0])]}}return{type:s,args:e}}function Sa(s){return!!s&&typeof s=="object"&&!Array.isArray(s)&&Object.getPrototypeOf(s)===Object.prototype}function Jr(s){return typeof s=="string"&&s.trim().length>0}function eg(s){if(Jr(s))return s;if(!Sa(s))throw new Error("ask_clarification() choice entries must be non-empty strings or objects with a non-empty label");if(!Jr(s.label))throw new Error("ask_clarification() choice objects require a non-empty label");if(s.value!==void 0&&!Jr(s.value))throw new Error("ask_clarification() choice object values must be non-empty strings");return{label:s.label,...s.value!==void 0?{value:s.value}:{}}}function ep(s){if(Jr(s))return s;if(!Sa(s))throw new Error("ask_clarification() requires a non-empty string or an object payload");if(!Jr(s.question))throw new Error("ask_clarification() object payload requires a non-empty question");let e=new Set(["text","number","date","single_choice","multiple_choice"]),t;if(s.type===void 0)t=Array.isArray(s.choices)&&s.choices.length>0?"single_choice":void 0;else{if(typeof s.type!="string"||!e.has(s.type))throw new Error("ask_clarification() object payload type must be one of: text, number, date, single_choice, multiple_choice");t=s.type}let n=t==="single_choice"||t==="multiple_choice",r=s.choices;if(r!==void 0){if(!Array.isArray(r)||r.length===0)throw new Error("ask_clarification() choices must be a non-empty array when provided")}else if(n)throw new Error("ask_clarification() choice payloads require a non-empty choices array");return{...s,question:s.question,...t?{type:t}:{},...r?{choices:r.map(eg)}:{}}}function tp(s){let e=ep(s);return typeof e=="string"?{question:e,type:"text"}:{...e,type:e.type??(e.choices&&e.choices.length>0?"single_choice":"text")}}function tg(s){return typeof structuredClone=="function"?structuredClone(s):JSON.parse(JSON.stringify(s))}function ng(s,e){return!e.has(s)&&!zm.has(s)&&jm.test(s)}function rg(s,e){if(s===void 0)return{};let t={context:s};if(!Sa(s))return t;for(let[n,r]of Object.entries(s))ng(n,e)&&(t[n]=r);return t}function og(s){if(s===null)return{type:"null"};if(Array.isArray(s))return{type:"array",ctor:"Array"};if(s instanceof Map)return{type:"map",ctor:"Map"};if(s instanceof Set)return{type:"set",ctor:"Set"};if(s instanceof Date)return{type:"date",ctor:"Date"};if(s instanceof Error)return{type:"error",ctor:typeof s.name=="string"&&s.name.trim()?s.name:"Error"};let e=typeof s;return e!=="object"?{type:e}:{type:"object",ctor:s&&s.constructor&&typeof s.constructor?.name=="string"?s.constructor?.name:void 0}}function Vr(s){if(s===null)return"null";if(s===void 0)return"undefined";let e=typeof s;if(e==="string")return JSON.stringify(ka(s,40));if(e==="number"||e==="boolean"||e==="bigint"||e==="symbol")return String(s);if(e==="function")return`[function ${s.name||"anonymous"}]`;if(Array.isArray(s))return`[array(${s.length})]`;if(s instanceof Date)return Number.isFinite(s.getTime())?s.toISOString():String(s);if(s instanceof Error)return`${s.name||"Error"}: ${s.message||""}`;if(s instanceof Map)return`[map(${s.size})]`;if(s instanceof Set)return`[set(${s.size})]`;let t=s&&s.constructor&&typeof s.constructor?.name=="string"?s.constructor?.name:"";return t&&t!=="Object"?`[${t}]`:"[object]"}function sg(s,e,t){if(e==="array"&&Array.isArray(s))return`[${s.slice(0,3).map(r=>Vr(r)).join(", ")}${s.length>3?", ...":""}]`;if(e==="map"&&s instanceof Map){let n=[...s.entries()].slice(0,3).map(([r,o])=>`${Vr(r)} => ${Vr(o)}`);return`Map(${s.size}) {${n.join(", ")}${s.size>3?", ...":""}}`}if(e==="set"&&s instanceof Set){let n=[...s.values()].slice(0,5).map(r=>Vr(r));return`Set(${s.size}) {${n.join(", ")}${s.size>5?", ...":""}}`}if(e==="object"&&s&&typeof s=="object"){let n=Object.keys(s),r=n.slice(0,4);return`${t&&t!=="Object"?`${t} `:""}{${r.join(", ")}${n.length>r.length?", ...":""}}`}return Vr(s)}function ig(s,e){if(e==="string"&&typeof s=="string")return`${s.length} chars`;if(e==="array"&&Array.isArray(s))return`${s.length} items`;if((e==="map"||e==="set")&&s instanceof Map)return`${s.size} items`;if((e==="map"||e==="set")&&s instanceof Set)return`${s.size} items`;if(e==="object"&&s&&typeof s=="object")return`${Object.keys(s).length} keys`}function ag(s){if(s.length===0)return;let e=s[0];if(e&&typeof e=="object"&&!Array.isArray(e)){let t=Object.keys(e).slice(0,8);if(t.length>0)return t.join(", ")}}function lg(s){return Object.entries(s).map(([e,t])=>{try{let n=og(t),r=ig(t,n.type),o=sg(t,n.type,n.ctor),i;n.type==="array"&&Array.isArray(t)&&(i=ag(t));let a=o?ka(o,40):void 0,l=i?a?`${a} \u2014 element keys: ${i}`:`element keys: ${i}`:a;return{name:e,type:n.type,...n.ctor?{ctor:n.ctor}:{},...r?{size:r}:{},...l?{preview:l}:{}}}catch{return{name:e,type:"unknown",preview:"[unavailable]"}}})}function ug(s,e){let t=lg(s),n=wa(t,new Map,e),r=e?.budgetRemaining!==void 0&&e?.budgetTotal!==void 0?`
1392
+ Sub-query budget: ${e.budgetRemaining}/${e.budgetTotal} remaining`:"";return`Explore with code \u2014 do not assume values from these previews.
1393
+ ${n}${r}`}function cg(s,e){return s instanceof it?at(`[CLARIFICATION] ${s.question}`,e):at(`[ABORTED] ${s.message}`,e)}function rr(s){return tg(s)}function pg(s){return s.map(e=>({turn:e.turn,code:e.code,output:e.output,actorFieldsOutput:e.actorFieldsOutput,tags:[...e.tags],...e.summary?{summary:e.summary}:{},...e.producedVars?{producedVars:[...e.producedVars]}:{},...e.referencedVars?{referencedVars:[...e.referencedVars]}:{},...e.stateDelta?{stateDelta:e.stateDelta}:{},...e.stepKind?{stepKind:e.stepKind}:{},...e.replayMode?{replayMode:e.replayMode}:{},...e.rank!==void 0?{rank:e.rank}:{},...e.tombstone?{tombstone:e.tombstone}:{}}))}function dg(s){return(s??[]).map(e=>({turn:e.turn,code:e.code,output:e.output,actorFieldsOutput:e.actorFieldsOutput,tags:[...e.tags],...e.summary?{summary:e.summary}:{},...e.producedVars?{producedVars:[...e.producedVars]}:{},...e.referencedVars?{referencedVars:[...e.referencedVars]}:{},...e.stateDelta?{stateDelta:e.stateDelta}:{},...e.stepKind?{stepKind:e.stepKind}:{},...e.replayMode?{replayMode:e.replayMode}:{},...e.rank!==void 0?{rank:e.rank}:{},...e.tombstone?{tombstone:e.tombstone}:{}}))}function mg(s){return Object.fromEntries([...s.entries()].map(([e,t])=>[e,{...t}]))}function gg(s,e){let t=s.filter(r=>r.restorable===!1).length,n=["Runtime Restore:","- Runtime state was restored from a previous call.","- Continue from restored values unless recomputation is actually needed."];return e?.includeLiveRuntimeState!==!1?n.splice(2,0,"- Live Runtime State below reflects the restored bindings."):n.splice(2,0,"- Live Runtime State rendering is disabled for this run, but the restored bindings are available in the runtime session."),t>0&&n.push(`- ${t} prior value${t===1?" was":"s were"} snapshot-only and could not be restored.`),n.join(`
1394
+ `)}function fg(s){return new Map(Object.entries(s??{}).map(([e,t])=>[e,{...t}]))}function hg(s,e){let t=new Map;for(let[n,r]of e.entries())t.set(n,{...r});for(let[n,r]of s.entries())t.set(n,{...r});return t}function np(s,e){let t=typeof s=="string"?_e.create(s):s,{ai:n,judgeAI:r,agentIdentity:o,...i}=e;return new Qr({ai:n,judgeAI:r,agentIdentity:o,signature:t},i)}function xg(s){return s instanceof et&&s.status>=500&&s.status<600?!0:s instanceof Ke||s instanceof ct}function at(s,e){return s.length<=e?s:`${s.slice(0,e)}
1395
+ ...[truncated ${s.length-e} chars]`}function ka(s,e){return e<=0?"":s.length<=e?s:e<=3?s.slice(0,e):`${s.slice(0,e-3)}...`}function Ag(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")&&(e.restorable===void 0||typeof e.restorable=="boolean")}function yg(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(Ag):void 0}catch{return}}function rp(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(`
1396
+ `);let o=[],i=0;for(let a of r){let l=o.length>0?1:0,u=n-i-l;if(u<=0)break;if(a.length<=u){o.push(a),i+=l+a.length;continue}o.push(ka(a,u)),i=n;break}return o.join(`
1397
+ `)}function bg(s,e){let t=s.split(`
1398
+ `).map(n=>n.trim()).filter(Boolean);return rp(t,e)}function Vc(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 Ig(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 Cg(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 wa(s,e,t){let n=[...s].sort((r,o)=>{let i=Vc(r,e.get(r.name));return Vc(o,e.get(o.name))-i||r.name.localeCompare(o.name)}).map(r=>{let o=r.preview?` = ${r.preview}`:"",i=Cg(e.get(r.name)),a="restorable"in r&&r.restorable===!1?" [snapshot only]":"";return`${r.name}: ${Ig(r)}${o}${i}${a}`});return n.length===0?"(no user variables)":rp(n,t)}function _s(s,e){if(s===void 0)return"(no output)";if(typeof s=="string")return at(s||"(no output)",e);try{return at(JSON.stringify(s,null,2),e)}catch{return at(String(s),e)}}function Ns(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,l)=>{if(l>4)return"[cause chain truncated]";let u=a,c=u?.name??"Error",p=u?.message??String(a),d=[`${c}: ${p}`];if(u?.data!==void 0)try{d.push(`Data: ${JSON.stringify(u.data,null,2)}`)}catch{d.push(`Data: ${String(u.data)}`)}return u?.cause!==void 0&&d.push(`Caused by: ${i(u.cause,l+1)}`),d.join(`
1399
+ `)};o.push(`Caused by: ${i(t.cause,1)}`)}return at(o.join(`
1400
+ `),e)}function Jc(s){let e=op(s);return/\bfinal\s*\(/.test(e)||/\bask_clarification\s*\(/.test(e)}function Qc(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 Yc(s){return s instanceof Error&&s.message==="Session is closed"}function wg(s){return s instanceof Error&&s.message==="Execution timed out"}function Tg(s){return s.startsWith("[ERROR]")||s.startsWith(Ls)?!0:/^(AggregateError|Error|EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError): /.test(s)}function Rg(s,e){if(e.kind==="threshold")return kg(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 Sg(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 kg(s){if(typeof s=="string")return s.length;try{return JSON.stringify(s).length}catch{return String(s).length}}function vg(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 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`);i.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`);i.set(l,{kind:"threshold",promptMaxChars:p})}}return{contextFieldNames:o,promptConfigByField:i}}function Og(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),l=a===void 0?"runtime-only":Sg(r,a,e?.inlinedFields?.has(n)===!0);t.push(`- ${n}: type=${o}, size=${i}, prompt=${l}`)}return t.join(`
1401
+ `)}async function Eg(s,e,t,n){if(s.length===0)return[];let r=new Array(s.length),o=0,i=Math.max(1,Math.min(e,s.length)),a=Array.from({length:i},async()=>{for(;;){if(n?.aborted)return;let l=o++;if(l>=s.length)return;let u=s[l];if(u===void 0)return;r[l]=await t(u,l)}});return await Promise.all(a),r}function Mg(s){return s.includes("console.log")}function Pg(s){let e=op(s),t=/\bfinal\s*\(/.test(e),n=/\bask_clarification\s*\(/.test(e),r=Number(t)+Number(n),o=Fg(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 op(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===`
1271
1402
  `?(e+=`
1272
1403
  `,n="normal"):e+=" ",t++;continue}if(n==="blockComment"){o==="*"&&i==="/"?(e+=" ",t+=2,n="normal"):(e+=o===`
1273
1404
  `?`
@@ -1275,19 +1406,19 @@ ${ge}`)}if(c.payload=void 0,this.enforceIncrementalConsoleTurns){let ge=bg(_);if
1275
1406
  `?`
1276
1407
  `:" ",r=!1,t++;continue}if(o==="\\"){e+=" ",r=!0,t++;continue}if(o===a){e+=" ",n="normal",t++;continue}e+=o===`
1277
1408
  `?`
1278
- `:" ",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 Ig(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,l=Cg(s,a);t.push({closeParenIndex:l})}return t}function Cg(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 Tg(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 wg(s,e){let t=s.trim(),r=e?.normalize??!0?Xc(t):t;if(!r)throw new Error("Agent module namespace must contain letters or numbers");return r}function zc(s){return Array.isArray(s.functions)}function xa(s,e){if(!s||s.length===0)return{functions:[],moduleMetadata:[]};let t=s.every(a=>zc(a)),n=s.every(a=>!zc(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 l=a.namespace.trim(),u=a.title.trim(),c=a.selectionCriteria.trim(),p=a.description.trim();if(!l)throw new Error("Agent function group namespace must be a non-empty string");if(!u)throw new Error(`Agent function group "${l}" must define a non-empty title`);if(!c)throw new Error(`Agent function group "${l}" must define a non-empty selectionCriteria`);if(!p)throw new Error(`Agent function group "${l}" must define a non-empty description`);if(e.has(l))throw new Error(`Agent function namespace "${l}" conflicts with an AxAgent runtime global and is reserved`);if(r.has(l))throw new Error(`Duplicate agent function group namespace "${l}"`);if(a.functions.length===0)throw new Error(`Agent function group "${l}" must contain at least one function`);r.add(l),o.push({namespace:l,title:u,selectionCriteria:c,description:p});for(let d of a.functions){if("namespace"in d&&d.namespace!==void 0)throw new Error(`Grouped agent function "${l}.${d.name}" must not define namespace; use the parent group namespace instead`);i.push({...d,namespace:l})}}return{functions:i,moduleMetadata:o}}function qc(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 Aa(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 Rg(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 Wr(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=Aa(s);if(e.length===0)return"unknown";if(Rg(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?Wr(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?Yc(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function Yc(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,l])=>{let u=Wr(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return s.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function Sg(s){let e=Yc(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${Wr(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function ya(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,l=i.description?.trim();l&&r.push({name:a,type:Wr(i),required:t?n.has(o):void 0,description:l});let u=Aa(i);if(u.includes("object")&&i.properties&&r.push(...ya(i,a,!1)),u.includes("array")&&i.items){let c=i.items.description?.trim(),p=`${a}[]`;c&&r.push({name:p,type:Wr(i.items),description:c}),Aa(i.items).includes("object")&&i.items.properties&&r.push(...ya(i.items,p,!1))}}return r}function kg(s){let e=ya(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(`
1279
- `)}function vg(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(`
1409
+ `:" ",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 Fg(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,l=_g(s,a);t.push({closeParenIndex:l})}return t}function _g(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 Ng(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 Lg(s,e){let t=s.trim(),r=e?.normalize??!0?ip(t):t;if(!r)throw new Error("Agent module namespace must contain letters or numbers");return r}function Xc(s){return Array.isArray(s.functions)}function Ca(s,e){if(!s||s.length===0)return{functions:[],moduleMetadata:[]};let t=s.every(a=>Xc(a)),n=s.every(a=>!Xc(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 l=a.namespace.trim(),u=a.title.trim(),c=a.selectionCriteria.trim(),p=a.description.trim();if(!l)throw new Error("Agent function group namespace must be a non-empty string");if(!u)throw new Error(`Agent function group "${l}" must define a non-empty title`);if(!c)throw new Error(`Agent function group "${l}" must define a non-empty selectionCriteria`);if(!p)throw new Error(`Agent function group "${l}" must define a non-empty description`);if(e.has(l))throw new Error(`Agent function namespace "${l}" conflicts with an AxAgent runtime global and is reserved`);if(r.has(l))throw new Error(`Duplicate agent function group namespace "${l}"`);if(a.functions.length===0)throw new Error(`Agent function group "${l}" must contain at least one function`);r.add(l),o.push({namespace:l,title:u,selectionCriteria:c,description:p});for(let d of a.functions){if("namespace"in d&&d.namespace!==void 0)throw new Error(`Grouped agent function "${l}.${d.name}" must not define namespace; use the parent group namespace instead`);i.push({...d,namespace:l})}}return{functions:i,moduleMetadata:o}}function Zc(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 Ta(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 $g(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 Yr(s){if(s.enum)return s.enum.map(n=>`"${n}"`).join(" | ");let e=Ta(s);if(e.length===0)return"unknown";if($g(e))return"any";let t=[...new Set(e)].map(n=>{if(n==="array"){let r=s.items?Yr(s.items):"unknown";return r.includes(" | ")?`(${r})[]`:`${r}[]`}return n==="object"?s.properties&&Object.keys(s.properties).length>0?sp(s):"object":n});return t.length>1?t.join(" | "):t[0]??"unknown"}function sp(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,l])=>{let u=Yr(l),c=o&&!r.has(a)?"?":"";return`${a}${c}: ${u}`});return s.additionalProperties===!0&&i.push("[key: string]: unknown"),`{ ${i.join(", ")} }`}function Dg(s){let e=sp(s.parameters,{respectRequired:!0}),t=s.returns?`: Promise<${Yr(s.returns)}>`:"";return`- \`${s.qualifiedName}(args: ${e})${t}\``}function Ra(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,l=i.description?.trim();l&&r.push({name:a,type:Yr(i),required:t?n.has(o):void 0,description:l});let u=Ta(i);if(u.includes("object")&&i.properties&&r.push(...Ra(i,a,!1)),u.includes("array")&&i.items){let c=i.items.description?.trim(),p=`${a}[]`;c&&r.push({name:p,type:Yr(i.items),description:c}),Ta(i.items).includes("object")&&i.items.properties&&r.push(...Ra(i.items,p,!1))}}return r}function Gg(s){let e=Ra(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(`
1410
+ `)}function Ug(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(`
1280
1411
  `)}).join(`
1281
1412
 
1282
1413
  `)].join(`
1283
- `)}function Og(s,e,t){return s.map(n=>{let r=[...e.get(n)??[]].map(u=>u.split(".").pop()??u).sort((u,c)=>u.localeCompare(c)),o=r.length>0,i=o?t.get(n):void 0,a=o?r.map(u=>`- \`${u}\``).join(`
1414
+ `)}function Bg(s,e,t){return s.map(n=>{let r=[...e.get(n)??[]].map(u=>u.split(".").pop()??u).sort((u,c)=>u.localeCompare(c)),o=r.length>0,i=o?t.get(n):void 0,a=o?r.map(u=>`- \`${u}\``).join(`
1284
1415
  `):`- Error: module \`${n}\` does not exist.`,l=[`### Module \`${n}\``];return i&&(l.push(`**${i.title}**`),l.push(i.description)),l.push(a),l.join(`
1285
1416
  `)}).join(`
1286
1417
 
1287
- `)}function Eg(s,e){return s.map(t=>{let n=t.includes(".")?t:`utils.${t}`,r=e.get(n);return r?[`### \`${n}\``,r.description,Sg({qualifiedName:n,parameters:r.parameters,returns:r.returns}),kg(r.parameters),vg(r.examples)].filter(o=>!!o).join(`
1418
+ `)}function jg(s,e){return s.map(t=>{let n=t.includes(".")?t:`utils.${t}`,r=e.get(n);return r?[`### \`${n}\``,r.description,Dg({qualifiedName:n,parameters:r.parameters,returns:r.returns}),Gg(r.parameters),Ug(r.examples)].filter(o=>!!o).join(`
1288
1419
  `):`### \`${n}\`
1289
1420
  - Not found.`}).join(`
1290
1421
 
1291
- `)}function Xc(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 Zc=(s,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,i=e?.disableQualityHealing??!1;return gs({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 l=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,u=await s(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=>s(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(`
1422
+ `)}function ip(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 ap=(s,e)=>{let t=e?.maxHops??3,n=e?.qualityThreshold??.8,r=e?.maxIterations??2,o=e?.qualityTarget??.85,i=e?.disableQualityHealing??!1;return ys({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 l=a.queryGeneratorResult?.searchQuery||a.searchQuery||a.originalQuestion,u=await s(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=>s(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(`
1292
1423
  `):"").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 s(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}))};
1293
1424
  //# sourceMappingURL=index.cjs.map